fix: all git builtin respect cwd now (#517)

This commit is contained in:
Simon Hauser
2021-02-09 18:25:57 +01:00
committed by GitHub
parent 993e383dd5
commit 3a7fa41857
7 changed files with 41 additions and 37 deletions

View File

@@ -5,6 +5,7 @@ local a = vim.api
local log = require('telescope.log')
local path = require('telescope.path')
local state = require('telescope.state')
local utils = require('telescope.utils')
local transform_mod = require('telescope.actions.mt').transform_mod
@@ -262,22 +263,23 @@ actions.insert_value = function(prompt_bufnr)
end
actions.git_checkout = function(prompt_bufnr)
local selection = actions.get_selected_entry(prompt_bufnr)
local cwd = actions.get_current_picker(prompt_bufnr).cwd
local selection = actions.get_selected_entry()
actions.close(prompt_bufnr)
local val = selection.value
os.execute('git checkout ' .. val)
utils.get_os_command_output({ 'git', 'checkout', selection.value }, cwd)
end
actions.git_staging_toggle = function(prompt_bufnr)
local selection = actions.get_selected_entry(prompt_bufnr)
local cwd = actions.get_current_picker(prompt_bufnr).cwd
local selection = actions.get_selected_entry()
-- If parts of the file are staged and unstaged at the same time, stage
-- changes. Else toggle between staged and unstaged if the file is tracked,
-- and between added and untracked if the file is untracked.
if selection.status:sub(2) == ' ' then
os.execute('git restore --staged ' .. selection.value)
utils.get_os_command_output({ 'git', 'restore', '--staged', selection.value }, cwd)
else
os.execute('git add ' .. selection.value)
utils.get_os_command_output({ 'git', 'add', selection.value }, cwd)
end
do_close(prompt_bufnr, true)
require('telescope.builtin').git_status()

View File

@@ -36,7 +36,7 @@ git.files = function(opts)
end
git.commits = function(opts)
local results = utils.get_os_command_output({ 'git', 'log', '--pretty=oneline', '--abbrev-commit' })
local results = utils.get_os_command_output({ 'git', 'log', '--pretty=oneline', '--abbrev-commit' }, opts.cwd)
pickers.new(opts, {
prompt_title = 'Git Commits',
@@ -56,7 +56,7 @@ end
git.bcommits = function(opts)
local results = utils.get_os_command_output({
'git', 'log', '--pretty=oneline', '--abbrev-commit', vim.fn.expand('%')
})
}, opts.cwd)
pickers.new(opts, {
prompt_title = 'Git BCommits',
@@ -74,9 +74,7 @@ git.bcommits = function(opts)
end
git.branches = function(opts)
-- Does this command in lua (hopefully):
-- 'git branch --all | grep -v HEAD | sed "s/.* //;s#remotes/[^/]*/##" | sort -u'
local output = utils.get_os_command_output({ 'git', 'branch', '--all' })
local output = utils.get_os_command_output({ 'git', 'branch', '--all' }, opts.cwd)
local tmp_results = {}
for _, v in ipairs(output) do
@@ -97,11 +95,7 @@ git.branches = function(opts)
finder = finders.new_table {
results = results,
entry_maker = function(entry)
return {
value = entry,
ordinal = entry,
display = entry,
}
return { value = entry, ordinal = entry, display = entry, }
end
},
previewer = previewers.git_branch_log.new(opts),
@@ -114,7 +108,7 @@ git.branches = function(opts)
end
git.status = function(opts)
local output = utils.get_os_command_output{ 'git', 'status', '-s' }
local output = utils.get_os_command_output({ 'git', 'status', '-s' }, opts.cwd)
if table.getn(output) == 0 then
print('No changes found')

View File

@@ -927,12 +927,12 @@ function make_entry.gen_from_autocommands(_)
end
end
function make_entry.gen_from_git_status(_)
function make_entry.gen_from_git_status(opts)
local displayer = entry_display.create {
separator = " ",
items = {
{ width = 1},
{ width = 1},
{ width = 1 },
{ width = 1 },
{ remaining = true },
}
}
@@ -947,8 +947,8 @@ function make_entry.gen_from_git_status(_)
end
return displayer {
{ string.sub(entry.status, 1, 1), staged},
{ string.sub(entry.status, -1), modified},
{ string.sub(entry.status, 1, 1), staged },
{ string.sub(entry.status, -1), modified },
entry.value,
}
end
@@ -956,12 +956,14 @@ function make_entry.gen_from_git_status(_)
return function (entry)
if entry == '' then return nil end
local mod, file = string.match(entry, '(..).*%s[->%s]?(.+)')
return {
value = file,
status = mod,
ordinal = entry,
display = make_display,
}
return {
value = file,
status = mod,
ordinal = entry,
display = make_display,
path = opts.cwd .. path.separator .. file
}
end
end

View File

@@ -83,6 +83,8 @@ function Picker:new(opts)
previewer = opts.previewer,
default_selection_index = opts.default_selection_index,
cwd = opts.cwd,
_completion_callbacks = {},
track = get_default(opts.track, false),

View File

@@ -393,7 +393,7 @@ previewers.man = defaulter(function(opts)
}
end)
previewers.git_branch_log = defaulter(function(_)
previewers.git_branch_log = defaulter(function(opts)
local highlight_buffer = function(bufnr, content)
for i = 1, #content do
local line = content[i]
@@ -418,7 +418,7 @@ previewers.git_branch_log = defaulter(function(_)
end
end
local remotes = utils.get_os_command_output{ 'git', 'remote' }
local remotes = utils.get_os_command_output({ 'git', 'remote' }, opts.cwd)
return previewers.new_buffer_previewer {
get_buffer_by_name = function(_, entry)
return entry.value
@@ -450,13 +450,14 @@ previewers.git_branch_log = defaulter(function(_)
putils.job_maker(gen_cmd(entry.value), self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname,
cwd = opts.cwd,
callback = handle_results
})
end
}
end, {})
previewers.git_commit_diff = defaulter(function(_)
previewers.git_commit_diff = defaulter(function(opts)
return previewers.new_buffer_previewer {
get_buffer_by_name = function(_, entry)
return entry.value
@@ -465,21 +466,22 @@ previewers.git_commit_diff = defaulter(function(_)
define_preview = function(self, entry, status)
putils.job_maker({ 'git', '-P', 'diff', entry.value .. '^!' }, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname
bufname = self.state.bufname,
cwd = opts.cwd
})
putils.regex_highlighter(self.state.bufnr, 'diff')
end
}
end, {})
previewers.git_file_diff = defaulter(function(_)
previewers.git_file_diff = defaulter(function(opts)
return previewers.new_buffer_previewer {
get_buffer_by_name = function(_, entry)
return entry.value
end,
define_preview = function(self, entry, status)
if entry.status and entry.status == '??' then
if entry.status and (entry.status == '??' or entry.status == 'A ') then
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
conf.buffer_previewer_maker(p, self.state.bufnr, {
@@ -488,7 +490,8 @@ previewers.git_file_diff = defaulter(function(_)
else
putils.job_maker({ 'git', '-P', 'diff', entry.value }, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname
bufname = self.state.bufname,
cwd = opts.cwd
})
putils.regex_highlighter(self.state.bufnr, 'diff')
end

View File

@@ -34,6 +34,7 @@ utils.job_maker = function(cmd, bufnr, opts)
command = command,
args = cmd,
env = opts.env,
cwd = opts.cwd,
on_exit = vim.schedule_wrap(function(j)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
if opts.mode == "append" then

View File

@@ -193,13 +193,13 @@ function utils.display_termcodes(str)
return str:gsub(string.char(9), "<TAB>"):gsub("", "<C-F>"):gsub(" ", "<Space>")
end
function utils.get_os_command_output(cmd)
function utils.get_os_command_output(cmd, cwd)
if type(cmd) ~= "table" then
print('Telescope: [get_os_command_output]: cmd has to be a table')
return {}
end
local command = table.remove(cmd, 1)
return Job:new({ command = command, args = cmd }):sync()
return Job:new({ command = command, args = cmd, cwd = cwd }):sync()
end
utils.strdisplaywidth = (function()