Skip to content

Commit 3f33c47

Browse files
authored
feat(command): add 'file' and 'rev' parameters (#238)
1 parent 30ee162 commit 3f33c47

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
- [ ] Copy git link in a symlink directory of git repo.
2424
- [ ] Copy git link in an un-pushed git branch, and receive an expected error.
2525
- [ ] Copy git link in a pushed git branch but edited file, and receive a warning says the git link could be wrong.
26+
- [ ] Copy git link with 'file' and 'rev' parameters.

.github/workflows/ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ jobs:
2929
token: ${{ secrets.GITHUB_TOKEN }}
3030
version: latest
3131
args: --config-path .stylua.toml ./lua ./spec
32-
- uses: stevearc/nvim-typecheck-action@v1
32+
- uses: mrcjkb/lua-typecheck-action@v0
3333
with:
34-
path: lua
35-
level: Warning
34+
directories: lua
3635
configpath: ".luarc.json"
37-
neodev-version: stable
3836
- uses: cargo-bins/cargo-binstall@main
3937
- name: Selene
4038
run: |

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
.luacheckcache
2+
service.log
3+
stderr.txt
4+
stdout.txt
5+
*nvim_gitlinker.nvim_lua.log

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,25 @@ There're several **router types**:
134134
135135
To specify the remote when there're multiple git remotes, add `remote=xxx` parameter, for example:
136136

137-
- `GitLink remote=upstream`: copy `blob` url to clipboard for the `upstream` remote.
138-
- `GitLink! blame remote=upstream`: open `blame` url in browser for the `upstream` remote.
137+
- `GitLink remote=upstream`: copy url for the `upstream` remote.
138+
- `GitLink! blame remote=upstream`: open blame url for the `upstream` remote.
139139

140140
> By default `GitLink` will use the first detected remote (usually it's `origin`).
141141
142+
To specify the relative file path when current buffer's file path is not a normal file name, add `file=xxx` parameter, for example:
143+
144+
- `GitLink file=lua/gitlinker.lua`: copy url for the `lua/gitlinker.lua` file.
145+
- `GitLink! blame file=README.md`: open blame url for the `README.md` file.
146+
147+
> By default `GitLink` will use the current buffer's file name.
148+
149+
To specify the git commit ID when current repository's commit ID is not on your needs, add `rev=xxx` parameter, for example:
150+
151+
- `GitLink rev=00b3f9a1`: copy url for the `00b3f9a1` commit ID.
152+
- `GitLink! blame rev=00b3f9a1`: open blame url for the `00b3f9a1` commit ID.
153+
154+
> By default `GitLink` will use the current repository's commit ID.
155+
142156
### API
143157

144158
> [!NOTE]
@@ -155,7 +169,7 @@ You can also use the `link` API to generate git permlink:
155169
--- @alias gitlinker.Linker {remote_url:string,protocol:string?,username:string?,password:string?,host:string,port:string?,org:string?,user:string?,repo:string,rev:string,file:string,lstart:integer,lend:integer,file_changed:boolean,default_branch:string?,current_branch:string?}
156170
--- @alias gitlinker.Router fun(lk:gitlinker.Linker):string?
157171
--- @alias gitlinker.Action fun(url:string):any
158-
--- @param opts {router_type:string?,router:gitlinker.Router?,action:gitlinker.Action?,lstart:integer?,lend:integer?,message:boolean?,highlight_duration:integer?,remote:string?}?
172+
--- @param opts {router_type:string?,router:gitlinker.Router?,action:gitlinker.Action?,lstart:integer?,lend:integer?,message:boolean?,highlight_duration:integer?,remote:string?,file:string?,rev:string?}?
159173
require("gitlinker").link(opts)
160174
```
161175

@@ -187,6 +201,8 @@ require("gitlinker").link(opts)
187201
- `message`: Whether print message in nvim command line. By default it uses the configured value while this plugin is been setup (see [Configuration](#configuration)). You can also overwrite this field to change the configured behavior.
188202
- `highlight_duration`: How long (milliseconds) to highlight the line range. By default it uses the configured value while this plugin is been setup (see [Configuration](#configuration)). You can also overwrite this field to change the configured behavior.
189203
- `remote`: Specify the git remote. By default is `nil`, it uses the first detected git remote (usually it's `origin`).
204+
- `file`: Specify the relative file path. By default is `nil`, it uses the current buffer's file name.
205+
- `rev`: Specify the git commit ID. By default is `nil`, it uses the current repository's git commit ID.
190206

191207
##### `gitlinker.Router`
192208

lua/gitlinker.lua

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ local function _blame(lk)
200200
return _router("blame", lk)
201201
end
202202

203-
--- @param opts {action:gitlinker.Action|boolean,router:gitlinker.Router,lstart:integer,lend:integer,message:boolean?,highlight_duration:integer?,remote:string?}
203+
--- @param opts {action:gitlinker.Action|boolean,router:gitlinker.Router,lstart:integer,lend:integer,message:boolean?,highlight_duration:integer?,remote:string?,file:string?,rev:string?}
204204
local _link = function(opts)
205205
local confs = configs.get()
206206
local logger = logging.get("gitlinker")
@@ -213,6 +213,14 @@ local _link = function(opts)
213213
lk.lstart = opts.lstart
214214
lk.lend = opts.lend
215215

216+
if str.not_empty(opts.file) then
217+
lk.file = opts.file
218+
lk.file_changed = false
219+
end
220+
if str.not_empty(opts.rev) then
221+
lk.rev = opts.rev
222+
end
223+
216224
async.scheduler()
217225
local ok, url = pcall(opts.router, lk, true)
218226
-- logger:debug(
@@ -264,30 +272,36 @@ local _link = function(opts)
264272
return url
265273
end
266274

267-
--- @type fun(opts:{action:gitlinker.Action?,router:gitlinker.Router,lstart:integer,lend:integer,remote:string?}):string?
275+
--- @type fun(opts:{action:gitlinker.Action?,router:gitlinker.Router,lstart:integer,lend:integer,remote:string?,file:string?,rev:string?}):string?
268276
local _void_link = async.void(_link)
269277

270278
--- @param args string?
271-
--- @return {router_type:string,remote:string?}
279+
--- @return {router_type:string,remote:string?,file:string?,rev:string?}
272280
local function _parse_args(args)
273281
args = args or ""
274282

275283
local router_type = "browse"
276284
local remote = nil
285+
local file = nil
286+
local rev = nil
277287
if string.len(args) == 0 then
278-
return { router_type = router_type, remote = remote }
288+
return { router_type = router_type, remote = remote, file = file, rev = rev }
279289
end
280290
local args_splits = vim.split(args, " ", { plain = true, trimempty = true })
281291
for _, a in ipairs(args_splits) do
282292
if string.len(a) > 0 then
283293
if str.startswith(a, "remote=") then
284294
remote = a:sub(8)
295+
elseif str.startswith(a, "file=") then
296+
file = a:sub(6)
297+
elseif str.startswith(a, "rev=") then
298+
rev = a:sub(5)
285299
else
286300
router_type = a
287301
end
288302
end
289303
end
290-
return { router_type = router_type, remote = remote }
304+
return { router_type = router_type, remote = remote, file = file, rev = rev }
291305
end
292306

293307
--- @param opts gitlinker.Options?
@@ -327,6 +341,8 @@ local function setup(opts)
327341
lstart = lstart,
328342
lend = lend,
329343
remote = parsed.remote,
344+
file = parsed.file,
345+
rev = parsed.rev,
330346
})
331347
end, {
332348
nargs = "*",
@@ -354,7 +370,7 @@ local function setup(opts)
354370
end
355371
end
356372

357-
--- @param opts {router_type:string?,router:gitlinker.Router?,action:gitlinker.Action?,lstart:integer?,lend:integer?,message:boolean?,highlight_duration:integer?,remote:string?}?
373+
--- @param opts {router_type:string?,router:gitlinker.Router?,action:gitlinker.Action?,lstart:integer?,lend:integer?,message:boolean?,highlight_duration:integer?,remote:string?,file:string?,rev:string?}?
358374
local function link_api(opts)
359375
opts = opts
360376
or {
@@ -384,6 +400,8 @@ local function link_api(opts)
384400
message = opts.message,
385401
highlight_duration = opts.highlight_duration,
386402
remote = opts.remote,
403+
file = opts.file,
404+
rev = opts.rev,
387405
})
388406
end
389407

lua/gitlinker/commons/_system.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local uv = vim.uv or vim.loop
1919
--- @field stderr? string
2020

2121
--- @class vim.SystemState
22+
--- @field cmd string[]
2223
--- @field handle? uv.uv_process_t
2324
--- @field timer? uv.uv_timer_t
2425
--- @field pid? integer
@@ -57,6 +58,7 @@ local function close_handles(state)
5758
end
5859

5960
--- @class vim.SystemObj
61+
--- @field cmd string[]
6062
--- @field pid integer
6163
--- @field private _state vim.SystemState
6264
--- @field wait fun(self: vim.SystemObj, timeout?: integer): vim.SystemCompleted
@@ -69,6 +71,7 @@ local SystemObj = {}
6971
--- @return vim.SystemObj
7072
local function new_systemobj(state)
7173
return setmetatable({
74+
cmd = state.cmd,
7275
pid = state.pid,
7376
_state = state,
7477
}, { __index = SystemObj })

0 commit comments

Comments
 (0)