Skip to content

Commit d367eb4

Browse files
authored
fix(git_branches): use the quoted fields instead of json-formatting and fix regressions with #695 (#704)
1 parent 0b2c801 commit d367eb4

1 file changed

Lines changed: 38 additions & 26 deletions

File tree

lua/telescope/builtin/git.lua

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,12 @@ git.bcommits = function(opts)
7878
end
7979

8080
git.branches = function(opts)
81-
local format = '{'
82-
.. '"head":%(if:equals=*)%(HEAD)%(then)true%(else)false%(end)'
83-
.. ',"refname":"%(refname)"'
84-
.. ',"authorname":"%(authorname)"'
85-
.. '%(if)%(upstream)%(then)'
86-
.. ',"upstream":"%(upstream:lstrip=2)"'
87-
.. '%(else)'
88-
.. ',"upstream":""'
89-
.. '%(end)'
90-
.. ',"committerdate":"%(committerdate:format-local:%Y/%m/%d %H:%M:%S)"'
91-
.. '}'
92-
local output = utils.get_os_command_output({ 'git', 'for-each-ref', '--format', format }, opts.cwd)
81+
local format = '%(HEAD)'
82+
.. '%(refname)'
83+
.. '%(authorname)'
84+
.. '%(upstream:lstrip=2)'
85+
.. '%(committerdate:format-local:%Y/%m/%d%H:%M:%S)'
86+
local output = utils.get_os_command_output({ 'git', 'for-each-ref', '--perl', '--format', format }, opts.cwd)
9387

9488
local results = {}
9589
local widths = {
@@ -98,26 +92,42 @@ git.branches = function(opts)
9892
upstream = 0,
9993
committerdate = 0,
10094
}
101-
local register_entry = function(entry, trim_refname_prefix)
102-
entry.name = string.sub(entry.refname, string.len(trim_refname_prefix)+1)
95+
local unescape_single_quote = function(v)
96+
return string.gsub(v, "\\([\\'])", "%1")
97+
end
98+
local parse_line = function(line)
99+
local fields = vim.split(string.sub(line, 2, -2), "''", true)
100+
local entry = {
101+
head = fields[1],
102+
refname = unescape_single_quote(fields[2]),
103+
authorname = unescape_single_quote(fields[3]),
104+
upstream = unescape_single_quote(fields[4]),
105+
committerdate = fields[5],
106+
}
107+
local prefix
108+
if vim.startswith(entry.refname, 'refs/remotes/') then
109+
prefix = 'refs/remotes/'
110+
elseif vim.startswith(entry.refname, 'refs/heads/') then
111+
prefix = 'refs/heads/'
112+
else
113+
return
114+
end
115+
local index = 1
116+
if entry.head ~= '*' then
117+
index = #results + 1
118+
end
119+
120+
entry.name = string.sub(entry.refname, string.len(prefix)+1)
103121
for key, value in pairs(widths) do
104-
widths[key] = math.max(value, vim.fn.strdisplaywidth(entry[key]))
122+
widths[key] = math.max(value, utils.strdisplaywidth(entry[key] or ''))
105123
end
106124
if string.len(entry.upstream) > 0 then
107125
widths.upstream_indicator = 2
108126
end
109-
table.insert(results, entry)
127+
table.insert(results, index, entry)
110128
end
111-
for _, v in ipairs(output) do
112-
local entry = vim.fn.json_decode(v)
113-
if entry.head then
114-
goto continue
115-
elseif vim.startswith(entry.refname, 'refs/remotes/') then
116-
register_entry(entry, 'refs/remotes/')
117-
elseif vim.startswith(entry.refname, 'refs/heads/') then
118-
register_entry(entry, 'refs/heads/')
119-
end
120-
::continue::
129+
for _, line in ipairs(output) do
130+
parse_line(line)
121131
end
122132
if #results == 0 then
123133
return
@@ -126,6 +136,7 @@ git.branches = function(opts)
126136
local displayer = entry_display.create {
127137
separator = " ",
128138
items = {
139+
{ width = 1 },
129140
{ width = widths.name },
130141
{ width = widths.authorname },
131142
{ width = widths.upstream_indicator },
@@ -136,6 +147,7 @@ git.branches = function(opts)
136147

137148
local make_display = function(entry)
138149
return displayer {
150+
{entry.head},
139151
{entry.name, 'TelescopeResultsIdentifier'},
140152
{entry.authorname},
141153
{string.len(entry.upstream) > 0 and '=>' or ''},

0 commit comments

Comments
 (0)