Skip to content

Commit

Permalink
fix(git): git status use porcelain flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestrew committed Mar 19, 2023
1 parent 94fe37a commit 8900f48
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .luarc.json
@@ -0,0 +1,7 @@
{
"diagnostics.globals": [
"vim",
"describe",
"it"
]
}
4 changes: 2 additions & 2 deletions lua/telescope/_extensions/file_browser/finders.lua
Expand Up @@ -46,7 +46,7 @@ end
local function git_args()
-- use dot here to also catch renames which also require the old filename
-- to properly show it as a rename.
local args = { "status", "--short", "--", "." }
local args = { "status", "--porcelain", "--", "." }
return args
end

Expand Down Expand Up @@ -89,7 +89,7 @@ fb_finders.browse_files = function(opts)
local git_file_status = {}
if opts.git_status then
local git_status, _ = Job:new({ cwd = opts.path, command = "git", args = git_args() }):sync()
git_file_status = fb_git.parse_status_output(git_status, opts.path)
git_file_status = fb_git.parse_status_output(git_status, opts.cwd)
end
if opts.path ~= os_sep and not opts.hide_parent_dir then
table.insert(data, 1, parent_path)
Expand Down
13 changes: 3 additions & 10 deletions lua/telescope/_extensions/file_browser/git.lua
@@ -1,7 +1,5 @@
local Path = require "plenary.path"

local os_sep = Path.path.sep

local M = {}

-- icon defaults are taken from Telescope git_status icons
Expand Down Expand Up @@ -91,18 +89,13 @@ end

--- Returns a map of absolute file path to file status
---@param output table: lines of the git status output
---@param cwd string: the path from which the command was triggered
---@param cwd string: cwd of the picker
---@return table: map from absolute file paths to files status
M.parse_status_output = function(output, cwd)
local parsed = {}
for _, value in ipairs(output) do
local status = value:sub(1, 2)
-- make sure to only get the last file name in the output to catch renames
-- which mention first the old and then the new file name. The old filename
-- won't be visible in the file browser so we only want the new name.
local file = value:reverse():match("([^ ]+)"):reverse()
local abs_file = cwd .. os_sep .. file
parsed[abs_file] = status
local mod, file = value:match "^(..) (.+)$"
parsed[Path:new({ cwd, file }):absolute()] = mod
end
return parsed
end
Expand Down
34 changes: 34 additions & 0 deletions lua/tests/fb_git_spec.lua
@@ -0,0 +1,34 @@
local fb_git = require "telescope._extensions.file_browser.git"

describe("parse_status_output", function()
local cwd = "/project/root/dir"
it("works in the root dir", function()
local git_status = {
"M .gitignore",
" M README.md",
" M lua/telescope/_extensions/file_browser/finders.lua",
"?? lua/tests/",
}
local expect = {
[cwd .. "/.gitignore"] = "M ",
[cwd .. "/README.md"] = " M",
[cwd .. "/lua/telescope/_extensions/file_browser/finders.lua"] = " M",
[cwd .. "/lua/tests/"] = "??",
}
local actual = fb_git.parse_status_output(git_status, cwd)
assert.are.same(expect, actual)
end)

it("works in a sub dir", function()
local git_status = {
" M lua/telescope/_extensions/file_browser/finders.lua",
"?? lua/tests/",
}
local expect = {
[cwd .. "/lua/telescope/_extensions/file_browser/finders.lua"] = " M",
[cwd .. "/lua/tests/"] = "??",
}
local actual = fb_git.parse_status_output(git_status, cwd)
assert.are.same(expect, actual)
end)
end)

0 comments on commit 8900f48

Please sign in to comment.