feat(git): support detached working trees (#2597)

* feat(git): support detached working trees

closes #2595

* [docgen] Update doc/telescope.txt
skip-checks: true

* fix: use_file_path

---------

Co-authored-by: Github Actions <actions@github>
This commit is contained in:
James Trew
2023-07-14 13:12:03 -04:00
committed by GitHub
parent 276362a802
commit 2ea8dcd17b
5 changed files with 124 additions and 38 deletions

View File

@@ -10,9 +10,14 @@ local strings = require "plenary.strings"
local Path = require "plenary.path"
local conf = require("telescope.config").values
local git_command = utils.__git_command
local git = {}
local get_git_command_output = function(args, opts)
return utils.get_os_command_output(git_command(args, opts), opts.cwd)
end
git.files = function(opts)
if opts.is_bare then
utils.notify("builtin.git_files", {
@@ -35,14 +40,14 @@ git.files = function(opts)
-- By creating the entry maker after the cwd options,
-- we ensure the maker uses the cwd options when being created.
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_file(opts))
local git_command = vim.F.if_nil(opts.git_command, { "git", "ls-files", "--exclude-standard", "--cached" })
opts.git_command = vim.F.if_nil(opts.git_command, git_command({ "ls-files", "--exclude-standard", "--cached" }, opts))
pickers
.new(opts, {
prompt_title = "Git Files",
finder = finders.new_oneshot_job(
vim.tbl_flatten {
git_command,
opts.git_command,
show_untracked and "--others" or nil,
recurse_submodules and "--recurse-submodules" or nil,
},
@@ -56,12 +61,13 @@ end
git.commits = function(opts)
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_commits(opts))
local git_command = vim.F.if_nil(opts.git_command, { "git", "log", "--pretty=oneline", "--abbrev-commit", "--", "." })
opts.git_command =
vim.F.if_nil(opts.git_command, git_command({ "log", "--pretty=oneline", "--abbrev-commit", "--", "." }, opts))
pickers
.new(opts, {
prompt_title = "Git Commits",
finder = finders.new_oneshot_job(git_command, opts),
finder = finders.new_oneshot_job(opts.git_command, opts),
previewer = {
previewers.git_commit_diff_to_parent.new(opts),
previewers.git_commit_diff_to_head.new(opts),
@@ -83,19 +89,12 @@ end
git.stash = function(opts)
opts.show_branch = vim.F.if_nil(opts.show_branch, true)
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_stash(opts))
opts.git_command = vim.F.if_nil(opts.git_command, git_command({ "--no-pager", "stash", "list" }, opts))
pickers
.new(opts, {
prompt_title = "Git Stash",
finder = finders.new_oneshot_job(
vim.tbl_flatten {
"git",
"--no-pager",
"stash",
"list",
},
opts
),
finder = finders.new_oneshot_job(opts.git_command, opts),
previewer = previewers.git_stash_diff.new(opts),
sorter = conf.file_sorter(opts),
attach_mappings = function()
@@ -115,15 +114,15 @@ git.bcommits = function(opts)
opts.current_line = (opts.current_file == nil) and get_current_buf_line(opts.winnr) or nil
opts.current_file = vim.F.if_nil(opts.current_file, vim.api.nvim_buf_get_name(opts.bufnr))
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_commits(opts))
local git_command =
vim.F.if_nil(opts.git_command, { "git", "log", "--pretty=oneline", "--abbrev-commit", "--follow" })
opts.git_command =
vim.F.if_nil(opts.git_command, git_command({ "log", "--pretty=oneline", "--abbrev-commit", "--follow" }, opts))
pickers
.new(opts, {
prompt_title = "Git BCommits",
finder = finders.new_oneshot_job(
vim.tbl_flatten {
git_command,
opts.git_command,
opts.current_file,
},
opts
@@ -200,10 +199,12 @@ git.branches = function(opts)
.. "%(authorname)"
.. "%(upstream:lstrip=2)"
.. "%(committerdate:format-local:%Y/%m/%d %H:%M:%S)"
local output = utils.get_os_command_output(
{ "git", "for-each-ref", "--perl", "--format", format, "--sort", "-authordate", opts.pattern },
opts.cwd
local output = get_git_command_output(
{ "for-each-ref", "--perl", "--format", format, "--sort", "-authordate", opts.pattern },
opts
)
local show_remote_tracking_branches = vim.F.if_nil(opts.show_remote_tracking_branches, true)
local results = {}
@@ -217,7 +218,7 @@ git.branches = function(opts)
return string.gsub(v, "\\([\\'])", "%1")
end
local parse_line = function(line)
local fields = vim.split(string.sub(line, 2, -2), "''", true)
local fields = vim.split(string.sub(line, 2, -2), "''")
local entry = {
head = fields[1],
refname = unescape_single_quote(fields[2]),
@@ -320,7 +321,7 @@ git.status = function(opts)
local gen_new_finder = function()
local expand_dir = vim.F.if_nil(opts.expand_dir, true)
local git_cmd = { "git", "status", "-z", "--", "." }
local git_cmd = git_command({ "status", "-z", "--", "." }, opts)
if expand_dir then
table.insert(git_cmd, #git_cmd - 1, "-u")
@@ -329,7 +330,6 @@ git.status = function(opts)
local output = utils.get_os_command_output(git_cmd, opts.cwd)
if #output == 0 then
print "No changes found"
utils.notify("builtin.git_status", {
msg = "No changes found",
level = "WARN",
@@ -379,31 +379,62 @@ git.status = function(opts)
:find()
end
local try_worktrees = function(opts)
local worktrees = conf.git_worktrees
if vim.tbl_isarray(worktrees) then
for _, wt in ipairs(worktrees) do
if vim.startswith(opts.cwd, wt.toplevel) then
opts.toplevel = wt.toplevel
opts.gitdir = wt.gitdir
if opts.use_git_root then
opts.cwd = wt.toplevel
end
return
end
end
end
error(opts.cwd .. " is not a git directory")
end
local current_path_toplevel = function()
local gitdir = vim.fn.finddir(".git", vim.fn.expand "%:p" .. ";")
if gitdir == "" then
return nil
end
return Path:new(gitdir):parent():absolute()
end
local set_opts_cwd = function(opts)
opts.use_git_root = vim.F.if_nil(opts.use_git_root, true)
if opts.cwd then
opts.cwd = vim.fn.expand(opts.cwd)
elseif opts.use_file_path then
opts.cwd = vim.fn.finddir(".git", vim.fn.expand "%:p" .. ";")
opts.cwd = current_path_toplevel()
if not opts.cwd then
opts.cwd = vim.fn.expand "%:p:h"
try_worktrees(opts)
return
end
else
opts.cwd = vim.loop.cwd()
end
-- Find root of git directory and remove trailing newline characters
local git_root, ret = utils.get_os_command_output({ "git", "rev-parse", "--show-toplevel" }, opts.cwd)
local use_git_root = vim.F.if_nil(opts.use_git_root, true)
local toplevel, ret = utils.get_os_command_output({ "git", "rev-parse", "--show-toplevel" }, opts.cwd)
if ret ~= 0 then
local in_worktree = utils.get_os_command_output({ "git", "rev-parse", "--is-inside-work-tree" }, opts.cwd)
local in_bare = utils.get_os_command_output({ "git", "rev-parse", "--is-bare-repository" }, opts.cwd)
if in_worktree[1] ~= "true" and in_bare[1] ~= "true" then
error(opts.cwd .. " is not a git directory")
try_worktrees(opts)
elseif in_worktree[1] ~= "true" and in_bare[1] == "true" then
opts.is_bare = true
end
else
if use_git_root then
opts.cwd = git_root[1]
if opts.use_git_root then
opts.cwd = toplevel[1]
end
end
end