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 log = require('telescope.log')
local path = require('telescope.path') local path = require('telescope.path')
local state = require('telescope.state') local state = require('telescope.state')
local utils = require('telescope.utils')
local transform_mod = require('telescope.actions.mt').transform_mod local transform_mod = require('telescope.actions.mt').transform_mod
@@ -262,22 +263,23 @@ actions.insert_value = function(prompt_bufnr)
end end
actions.git_checkout = function(prompt_bufnr) 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) actions.close(prompt_bufnr)
local val = selection.value utils.get_os_command_output({ 'git', 'checkout', selection.value }, cwd)
os.execute('git checkout ' .. val)
end end
actions.git_staging_toggle = function(prompt_bufnr) 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 -- 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, -- changes. Else toggle between staged and unstaged if the file is tracked,
-- and between added and untracked if the file is untracked. -- and between added and untracked if the file is untracked.
if selection.status:sub(2) == ' ' then 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 else
os.execute('git add ' .. selection.value) utils.get_os_command_output({ 'git', 'add', selection.value }, cwd)
end end
do_close(prompt_bufnr, true) do_close(prompt_bufnr, true)
require('telescope.builtin').git_status() require('telescope.builtin').git_status()

View File

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

View File

@@ -927,7 +927,7 @@ function make_entry.gen_from_autocommands(_)
end end
end end
function make_entry.gen_from_git_status(_) function make_entry.gen_from_git_status(opts)
local displayer = entry_display.create { local displayer = entry_display.create {
separator = " ", separator = " ",
items = { items = {
@@ -956,11 +956,13 @@ function make_entry.gen_from_git_status(_)
return function (entry) return function (entry)
if entry == '' then return nil end if entry == '' then return nil end
local mod, file = string.match(entry, '(..).*%s[->%s]?(.+)') local mod, file = string.match(entry, '(..).*%s[->%s]?(.+)')
return { return {
value = file, value = file,
status = mod, status = mod,
ordinal = entry, ordinal = entry,
display = make_display, display = make_display,
path = opts.cwd .. path.separator .. file
} }
end end
end end

View File

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

View File

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

View File

@@ -34,6 +34,7 @@ utils.job_maker = function(cmd, bufnr, opts)
command = command, command = command,
args = cmd, args = cmd,
env = opts.env, env = opts.env,
cwd = opts.cwd,
on_exit = vim.schedule_wrap(function(j) on_exit = vim.schedule_wrap(function(j)
if not vim.api.nvim_buf_is_valid(bufnr) then return end if not vim.api.nvim_buf_is_valid(bufnr) then return end
if opts.mode == "append" then 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>") return str:gsub(string.char(9), "<TAB>"):gsub("", "<C-F>"):gsub(" ", "<Space>")
end end
function utils.get_os_command_output(cmd) function utils.get_os_command_output(cmd, cwd)
if type(cmd) ~= "table" then if type(cmd) ~= "table" then
print('Telescope: [get_os_command_output]: cmd has to be a table') print('Telescope: [get_os_command_output]: cmd has to be a table')
return {} return {}
end end
local command = table.remove(cmd, 1) 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 end
utils.strdisplaywidth = (function() utils.strdisplaywidth = (function()