Skip to content

Commit

Permalink
fix(git): show status for git submodules (#356)
Browse files Browse the repository at this point in the history
Need to compute the git toplevel directory upon changing directories
since calling `git status` inside a git submodule will give the status
relative to the toplevel directory of the submodule, not the parent
toplevel directory. This was causing a bug with bad paths being created
for git status changes in submodules.

closes #353
  • Loading branch information
jamestrew committed Jan 29, 2024
1 parent 6f735a6 commit 6dd6522
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lua/telescope/_extensions/file_browser/finders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ 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.git_root)
local git_root = fb_git.find_root(opts.path)
if git_root ~= nil 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, git_root)
end
end
if opts.path ~= os_sep and not opts.hide_parent_dir then
table.insert(data, 1, parent_path)
Expand Down Expand Up @@ -192,11 +195,11 @@ fb_finders.finder = function(opts)
hidden = vim.tbl_extend("keep", hidden, hidden_default)
end

local git_root = Job:new({ command = "git", args = { "rev-parse", "--show-toplevel" } }):sync()[1]
local cwd = opts.cwd_to_path and opts.path or opts.cwd

return setmetatable({
cwd_to_path = opts.cwd_to_path,
cwd = opts.cwd_to_path and opts.path or opts.cwd, -- nvim cwd
cwd = cwd,
path = vim.F.if_nil(opts.path, opts.cwd), -- current path for file browser
add_dirs = vim.F.if_nil(opts.add_dirs, true),
hidden = hidden,
Expand All @@ -211,8 +214,7 @@ fb_finders.finder = function(opts)
select_buffer = vim.F.if_nil(opts.select_buffer, false),
hide_parent_dir = vim.F.if_nil(opts.hide_parent_dir, false),
collapse_dirs = vim.F.if_nil(opts.collapse_dirs, false),
git_status = vim.F.if_nil(opts.git_status, git_root ~= nil),
git_root = git_root,
git_status = vim.F.if_nil(opts.git_status, fb_git.find_root(cwd) ~= nil),
-- ensure we forward make_entry opts adequately
entry_maker = vim.F.if_nil(opts.entry_maker, function(local_opts)
return fb_make_entry(vim.tbl_extend("force", opts, local_opts))
Expand Down
7 changes: 7 additions & 0 deletions lua/telescope/_extensions/file_browser/git.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
local Path = require "plenary.path"
local Job = require "plenary.job"

local M = {}

---@param cwd string?
---@return string?
M.find_root = function(cwd)
return Job:new({ cwd = cwd, command = "git", args = { "rev-parse", "--show-toplevel" } }):sync()[1]
end

-- icon defaults are taken from Telescope git_status icons
local icon_defaults = {
added = "+",
Expand Down

0 comments on commit 6dd6522

Please sign in to comment.