Skip to content

Commit 8900f48

Browse files
committedMar 19, 2023
fix(git): git status use porcelain flag
1 parent 94fe37a commit 8900f48

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed
 

‎.luarc.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"diagnostics.globals": [
3+
"vim",
4+
"describe",
5+
"it"
6+
]
7+
}

‎lua/telescope/_extensions/file_browser/finders.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ end
4646
local function git_args()
4747
-- use dot here to also catch renames which also require the old filename
4848
-- to properly show it as a rename.
49-
local args = { "status", "--short", "--", "." }
49+
local args = { "status", "--porcelain", "--", "." }
5050
return args
5151
end
5252

@@ -89,7 +89,7 @@ fb_finders.browse_files = function(opts)
8989
local git_file_status = {}
9090
if opts.git_status then
9191
local git_status, _ = Job:new({ cwd = opts.path, command = "git", args = git_args() }):sync()
92-
git_file_status = fb_git.parse_status_output(git_status, opts.path)
92+
git_file_status = fb_git.parse_status_output(git_status, opts.cwd)
9393
end
9494
if opts.path ~= os_sep and not opts.hide_parent_dir then
9595
table.insert(data, 1, parent_path)

‎lua/telescope/_extensions/file_browser/git.lua

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
local Path = require "plenary.path"
22

3-
local os_sep = Path.path.sep
4-
53
local M = {}
64

75
-- icon defaults are taken from Telescope git_status icons
@@ -91,18 +89,13 @@ end
9189

9290
--- Returns a map of absolute file path to file status
9391
---@param output table: lines of the git status output
94-
---@param cwd string: the path from which the command was triggered
92+
---@param cwd string: cwd of the picker
9593
---@return table: map from absolute file paths to files status
9694
M.parse_status_output = function(output, cwd)
9795
local parsed = {}
9896
for _, value in ipairs(output) do
99-
local status = value:sub(1, 2)
100-
-- make sure to only get the last file name in the output to catch renames
101-
-- which mention first the old and then the new file name. The old filename
102-
-- won't be visible in the file browser so we only want the new name.
103-
local file = value:reverse():match("([^ ]+)"):reverse()
104-
local abs_file = cwd .. os_sep .. file
105-
parsed[abs_file] = status
97+
local mod, file = value:match "^(..) (.+)$"
98+
parsed[Path:new({ cwd, file }):absolute()] = mod
10699
end
107100
return parsed
108101
end

‎lua/tests/fb_git_spec.lua

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local fb_git = require "telescope._extensions.file_browser.git"
2+
3+
describe("parse_status_output", function()
4+
local cwd = "/project/root/dir"
5+
it("works in the root dir", function()
6+
local git_status = {
7+
"M .gitignore",
8+
" M README.md",
9+
" M lua/telescope/_extensions/file_browser/finders.lua",
10+
"?? lua/tests/",
11+
}
12+
local expect = {
13+
[cwd .. "/.gitignore"] = "M ",
14+
[cwd .. "/README.md"] = " M",
15+
[cwd .. "/lua/telescope/_extensions/file_browser/finders.lua"] = " M",
16+
[cwd .. "/lua/tests/"] = "??",
17+
}
18+
local actual = fb_git.parse_status_output(git_status, cwd)
19+
assert.are.same(expect, actual)
20+
end)
21+
22+
it("works in a sub dir", function()
23+
local git_status = {
24+
" M lua/telescope/_extensions/file_browser/finders.lua",
25+
"?? lua/tests/",
26+
}
27+
local expect = {
28+
[cwd .. "/lua/telescope/_extensions/file_browser/finders.lua"] = " M",
29+
[cwd .. "/lua/tests/"] = "??",
30+
}
31+
local actual = fb_git.parse_status_output(git_status, cwd)
32+
assert.are.same(expect, actual)
33+
end)
34+
end)

0 commit comments

Comments
 (0)
Please sign in to comment.