fix(git_branches): use the quoted fields instead of json-formatting and fix regressions with #695 (#704)

This commit is contained in:
Kyoichiro Yamada
2021-04-08 00:00:38 +09:00
committed by GitHub
parent 0b2c801978
commit d367eb4fd8

View File

@@ -78,18 +78,12 @@ git.bcommits = function(opts)
end end
git.branches = function(opts) git.branches = function(opts)
local format = '{' local format = '%(HEAD)'
.. '"head":%(if:equals=*)%(HEAD)%(then)true%(else)false%(end)' .. '%(refname)'
.. ',"refname":"%(refname)"' .. '%(authorname)'
.. ',"authorname":"%(authorname)"' .. '%(upstream:lstrip=2)'
.. '%(if)%(upstream)%(then)' .. '%(committerdate:format-local:%Y/%m/%d%H:%M:%S)'
.. ',"upstream":"%(upstream:lstrip=2)"' local output = utils.get_os_command_output({ 'git', 'for-each-ref', '--perl', '--format', format }, opts.cwd)
.. '%(else)'
.. ',"upstream":""'
.. '%(end)'
.. ',"committerdate":"%(committerdate:format-local:%Y/%m/%d %H:%M:%S)"'
.. '}'
local output = utils.get_os_command_output({ 'git', 'for-each-ref', '--format', format }, opts.cwd)
local results = {} local results = {}
local widths = { local widths = {
@@ -98,26 +92,42 @@ git.branches = function(opts)
upstream = 0, upstream = 0,
committerdate = 0, committerdate = 0,
} }
local register_entry = function(entry, trim_refname_prefix) local unescape_single_quote = function(v)
entry.name = string.sub(entry.refname, string.len(trim_refname_prefix)+1) return string.gsub(v, "\\([\\'])", "%1")
end
local parse_line = function(line)
local fields = vim.split(string.sub(line, 2, -2), "''", true)
local entry = {
head = fields[1],
refname = unescape_single_quote(fields[2]),
authorname = unescape_single_quote(fields[3]),
upstream = unescape_single_quote(fields[4]),
committerdate = fields[5],
}
local prefix
if vim.startswith(entry.refname, 'refs/remotes/') then
prefix = 'refs/remotes/'
elseif vim.startswith(entry.refname, 'refs/heads/') then
prefix = 'refs/heads/'
else
return
end
local index = 1
if entry.head ~= '*' then
index = #results + 1
end
entry.name = string.sub(entry.refname, string.len(prefix)+1)
for key, value in pairs(widths) do for key, value in pairs(widths) do
widths[key] = math.max(value, vim.fn.strdisplaywidth(entry[key])) widths[key] = math.max(value, utils.strdisplaywidth(entry[key] or ''))
end end
if string.len(entry.upstream) > 0 then if string.len(entry.upstream) > 0 then
widths.upstream_indicator = 2 widths.upstream_indicator = 2
end end
table.insert(results, entry) table.insert(results, index, entry)
end end
for _, v in ipairs(output) do for _, line in ipairs(output) do
local entry = vim.fn.json_decode(v) parse_line(line)
if entry.head then
goto continue
elseif vim.startswith(entry.refname, 'refs/remotes/') then
register_entry(entry, 'refs/remotes/')
elseif vim.startswith(entry.refname, 'refs/heads/') then
register_entry(entry, 'refs/heads/')
end
::continue::
end end
if #results == 0 then if #results == 0 then
return return
@@ -126,6 +136,7 @@ git.branches = function(opts)
local displayer = entry_display.create { local displayer = entry_display.create {
separator = " ", separator = " ",
items = { items = {
{ width = 1 },
{ width = widths.name }, { width = widths.name },
{ width = widths.authorname }, { width = widths.authorname },
{ width = widths.upstream_indicator }, { width = widths.upstream_indicator },
@@ -136,6 +147,7 @@ git.branches = function(opts)
local make_display = function(entry) local make_display = function(entry)
return displayer { return displayer {
{entry.head},
{entry.name, 'TelescopeResultsIdentifier'}, {entry.name, 'TelescopeResultsIdentifier'},
{entry.authorname}, {entry.authorname},
{string.len(entry.upstream) > 0 and '=>' or ''}, {string.len(entry.upstream) > 0 and '=>' or ''},