Skip to content

Commit

Permalink
fix: use fugitive functions to parse fugitive:// url
Browse files Browse the repository at this point in the history
Fixes #686
  • Loading branch information
rosds authored and lewis6991 committed Jan 11, 2023
1 parent 414aa7b commit 9950f48
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 51 deletions.
23 changes: 9 additions & 14 deletions lua/gitsigns.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions lua/gitsigns/git.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions lua/gitsigns/test.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 10 additions & 15 deletions teal/gitsigns.tl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ local record M
detach : function(bufnr: integer, _keep_signs: boolean)
detach_all : function()
attach : function(cbuf: integer, trigger: string)

-- Exposed for tests
parse_fugitive_uri: function(name: string): string, string
end

--- Detach Gitsigns from all buffers it is attached to.
Expand Down Expand Up @@ -73,18 +70,20 @@ function M.detach(bufnr: integer, _keep_signs: boolean)
cache:destroy(bufnr)
end

-- @return (string, string) Tuple of buffer name and commit
local function parse_fugitive_uri(name: string): string, string
local _, _, root_path, sub_module_path, commit, real_path =
name:find([[^fugitive://(.*)/%.git(.*/)/(%x-)/(.*)]])
if vim.g.loaded_fugitive == 0 then
dprint("Fugitive not installed")
return
end

local path = vim.fn.FugitiveReal(name)
local commit = vim.fn.FugitiveParse(name)[1]:match('([^:]+):.*')
if commit == '0' then
-- '0' means the index so cleat commit so we attach normally
-- '0' means the index so clear commit so we attach normally
commit = nil
end
if root_path then
sub_module_path = sub_module_path:gsub("^/modules", "")
name = root_path .. sub_module_path .. real_path
end
return name, commit
return path, commit
end

local function parse_gitsigns_uri(name: string): string, string
Expand Down Expand Up @@ -460,10 +459,6 @@ M.setup = void(function(cfg: Config)
autocmd('DirChanged', debounce_trailing(100, manager.update_cwd_head))
end)

if _TEST then
M.parse_fugitive_uri = parse_fugitive_uri
end

return setmetatable(M, {
__index = function(_, f: string): any
return (require('gitsigns.actions') as {string:function})[f]
Expand Down
11 changes: 8 additions & 3 deletions teal/gitsigns/git.tl
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ end
local git_command = async.create(function(args: {string}, spec: GJobSpec): {string}, string
spec = spec or {}
spec.command = spec.command or 'git'
spec.args = spec.command == 'git' and
{ '--no-pager', '--literal-pathspecs', unpack(args) } or args
spec.args = spec.command == 'git' and {
'--no-pager',
'--literal-pathspecs',
'-c', 'gc.auto=0', -- Disable auto-packing which emits messages to stderr
unpack(args)
} or args

if not spec.cwd and not uv.cwd() then
spec.cwd = vim.env.HOME
Expand All @@ -181,7 +185,8 @@ local git_command = async.create(function(args: {string}, spec: GJobSpec): {stri

if not spec.suppress_stderr then
if stderr then
gsd.eprint(stderr)
local cmd_str = table.concat({spec.command, unpack(args)}, ' ')
gsd.eprintf("Recieved stderr when running command\n'%s':\n%s", cmd_str, stderr)
end
end

Expand Down
8 changes: 0 additions & 8 deletions teal/gitsigns/test.tl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,4 @@ M._tests.test_args = function()
eq(pos_args[1], 'posarg')
end

M._tests.test_name_parse = function()
local gs = require'gitsigns'
local path, commit = gs.parse_fugitive_uri(
'fugitive:///home/path/to/project/.git//1b441b947c4bc9a59db428f229456619051dd133/subfolder/to/a/file.txt')
eq(path, '/home/path/to/project/subfolder/to/a/file.txt')
eq(commit, '1b441b947c4bc9a59db428f229456619051dd133')
end

return M
9 changes: 9 additions & 0 deletions types/vim.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ local record M

record g
gitsigns_head: string
loaded_fugitive: integer
end

record v
Expand Down Expand Up @@ -148,6 +149,14 @@ local record M

pesc: function(string): string

record Regex
userdata

match_str: function(Regex, string): integer, integer
end

regex: function(string): Regex

startswith: function(string, string): boolean
endswith: function(string, string): boolean

Expand Down
3 changes: 3 additions & 0 deletions types/vim/fn.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ local record M
systemlist: function({string}): {string}
tempname: function(): string
type: function(any): integer

FugitiveReal: function(...: any): string
FugitiveParse: function(...: any): {string, string}
end

return M

2 comments on commit 9950f48

@caarlos0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: after this, nvim started failing to start with:

Error executing vim.schedule lua callback: ...cal/share/nvim/lazy/gitsigns.nvim/lua/gitsigns/async.lua:110: The coroutine failed with this message: Vim:E117: Unknown function: FugitiveReal
stack traceback:
        [C]: in function 'FugitiveReal'
        ...os/.local/share/nvim/lazy/gitsigns.nvim/lua/gitsigns.lua:80: in function 'parse_fugitive_uri'
        ...os/.local/share/nvim/lazy/gitsigns.nvim/lua/gitsigns.lua:113: in function 'get_buf_path'
        ...os/.local/share/nvim/lazy/gitsigns.nvim/lua/gitsigns.lua:222: in function 'fn'
        .../share/nvim/lazy/gitsigns.nvim/lua/gitsigns/debounce.lua:78: in function 'attach_throttled'
        ...os/.local/share/nvim/lazy/gitsigns.nvim/lua/gitsigns.lua:322: in function 'attach'
        ...os/.local/share/nvim/lazy/gitsigns.nvim/lua/gitsigns.lua:417: in function <...os/.local/share/nvim/lazy/gitsigns.nvim/lua/gitsigns.lua:376>
stack traceback:
        [C]: in function 'error'
        ...cal/share/nvim/lazy/gitsigns.nvim/lua/gitsigns/async.lua:110: in function 'cb'
        ...cal/share/nvim/lazy/gitsigns.nvim/lua/gitsigns/async.lua:145: in function <...cal/share/nvim/lazy/gitsigns.nvim/lua/gitsigns/async.lua:144>
Press ENTER or type command to continue

removing git-fugitive entirely doesn't help.

locking this dep to 414aa7b works.

@lewis6991
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't comment directly on commits. Raise issues.

Please sign in to comment.