Debian Codegraph @

lua/lualine/components/branch/git_branch.lua

raw
1 local M = {}
2
3 local lualine_require = require('lualine_require')
4 local modules = lualine_require.lazy_require {
5 utils = 'lualine.utils.utils',
6 Job = 'lualine.utils.job',
7 }
8
9 -- vars
10 local current_git_branch = ''
11 local current_git_dir = ''
12 local current_git_dir_is_reftable = false
13 local branch_cache = {} -- stores last known branch for a buffer
14 local active_bufnr = '0'
15 -- os specific path separator
16 local sep = package.config:sub(1, 1)
17 -- event watcher to watch head file
18 -- Use file watch for non Windows and poll for Windows.
19 -- Windows doesn't like file watch for some reason.
20 local file_changed = sep ~= '\\' and vim.loop.new_fs_event() or vim.loop.new_fs_poll()
21 local git_dir_cache = {} -- Stores git paths that we already know of
22 ---job handle for async git commands (reftable repos)
23 local branch_job = nil
24
25 ---checks if git directory uses reftable format
26 ---@param git_dir string full path to .git directory
27 ---@return boolean
28 local function is_reftable_repo(git_dir)
29 local reftable_dir = git_dir .. sep .. 'reftable'
30 local stat = vim.loop.fs_stat(reftable_dir)
31 return stat ~= nil and stat.type == 'directory'
32 end
33
34 ---sets git_branch variable to branch name or commit hash if not on branch
35 ---@param head_file string full path of .git/HEAD file
36 local function get_git_head(head_file)
37 local f_head = io.open(head_file)
38 if f_head then
39 local HEAD = f_head:read()
40 f_head:close()
41 local branch = HEAD:match('ref: refs/heads/(.+)$')
42 if branch then
43 current_git_branch = branch
44 else
45 current_git_branch = HEAD:sub(1, 6)
46 end
47 end
48 return nil
49 end
50
51 ---gets short commit hash for detached HEAD in reftable repos (async)
52 ---@param git_dir string full path to .git directory
53 ---@param bufnr number buffer number to update cache for
54 local function get_branch_reftable_hash(git_dir, bufnr)
55 if branch_job then
56 branch_job:stop()
57 end
58
59 local output = {}
60 branch_job = modules.Job {
61 cmd = { 'git', '--git-dir=' .. git_dir, 'rev-parse', '--short', 'HEAD' },
62 on_stdout = function(_, data)
63 if data then
64 output = vim.list_extend(output, data)
65 end
66 end,
67 on_exit = function(_, code)
68 if code == -1 then
69 return
70 end
71 local branch = ''
72 if code == 0 and #output > 0 then
73 branch = vim.trim(table.concat(output, ''))
74 end
75 if current_git_dir == git_dir then
76 current_git_branch = branch
77 end
78 branch_cache[bufnr] = branch
79 vim.schedule(function()
80 require('lualine').refresh()
81 end)
82 end,
83 }
84 branch_job:start()
85 end
86
87 ---gets branch name for reftable repos using git command (async)
88 ---@param git_dir string full path to .git directory
89 ---@param bufnr number buffer number to update cache for
90 local function get_branch_reftable(git_dir, bufnr)
91 if branch_job then
92 branch_job:stop()
93 end
94
95 local output = {}
96 branch_job = modules.Job {
97 cmd = { 'git', '--git-dir=' .. git_dir, 'symbolic-ref', '--short', 'HEAD' },
98 on_stdout = function(_, data)
99 if data then
100 output = vim.list_extend(output, data)
101 end
102 end,
103 on_exit = function(_, code)
104 if code == -1 then
105 return
106 end
107 if code == 0 and #output > 0 then
108 local branch = vim.trim(table.concat(output, ''))
109 if #branch > 0 then
110 if current_git_dir == git_dir then
111 current_git_branch = branch
112 end
113 branch_cache[bufnr] = branch
114 vim.schedule(function()
115 require('lualine').refresh()
116 end)
117 return
118 end
119 end
120 -- Detached HEAD or error - try rev-parse for short commit hash
121 get_branch_reftable_hash(git_dir, bufnr)
122 end,
123 }
124 branch_job:start()
125 end
126
127 ---updates the current value of git_branch and sets up file watch on HEAD file
128 local function update_branch()
129 local bufnr = vim.api.nvim_get_current_buf()
130 active_bufnr = tostring(bufnr)
131 file_changed:stop()
132 local git_dir = current_git_dir
133 if git_dir and #git_dir > 0 then
134 local watch_file
135 if current_git_dir_is_reftable then
136 -- Reftable format: use git command to get branch name
137 get_branch_reftable(git_dir, bufnr)
138 watch_file = git_dir .. sep .. 'reftable' .. sep .. 'tables.list'
139 else
140 -- Normal git format: read HEAD file directly
141 local head_file = git_dir .. sep .. 'HEAD'
142 get_git_head(head_file)
143 watch_file = head_file
144 branch_cache[bufnr] = current_git_branch
145 end
146 file_changed:start(
147 watch_file,
148 sep ~= '\\' and {} or 1000,
149 vim.schedule_wrap(function()
150 -- reset file-watch
151 update_branch()
152 end)
153 )
154 else
155 -- set to '' when git dir was not found
156 current_git_branch = ''
157 branch_cache[bufnr] = current_git_branch
158 end
159 end
160
161 ---updates the current value of current_git_branch and sets up file watch on HEAD file if value changed
162 local function update_current_git_dir(git_dir)
163 if current_git_dir ~= git_dir then
164 current_git_dir = git_dir
165 current_git_dir_is_reftable = git_dir and is_reftable_repo(git_dir) or false
166 update_branch()
167 end
168 end
169
170 ---returns full path to git directory for dir_path or current directory
171 ---@param dir_path string|nil
172 ---@return string|nil
173 function M.find_git_dir(dir_path)
174 local git_dir = vim.env.GIT_DIR
175 if git_dir then
176 update_current_git_dir(git_dir)
177 return git_dir
178 end
179
180 -- get file dir so we can search from that dir
181 local file_dir = dir_path or vim.fn.expand('%:p:h')
182
183 if package.loaded.oil then
184 local oil = require('oil')
185 local ok, dir = pcall(oil.get_current_dir)
186 if ok and dir and dir ~= '' then
187 file_dir = vim.fn.fnamemodify(dir, ':p:h')
188 end
189 end
190
191 -- extract correct file dir from terminals
192 if file_dir and file_dir:match('term://.*') then
193 file_dir = vim.fn.expand(file_dir:gsub('term://(.+)//.+', '%1'))
194 end
195
196 local root_dir = file_dir
197 -- Search upward for .git file or folder
198 while root_dir do
199 if git_dir_cache[root_dir] then
200 git_dir = git_dir_cache[root_dir]
201 break
202 end
203 local git_path = root_dir .. sep .. '.git'
204 local git_file_stat = vim.loop.fs_stat(git_path)
205 if git_file_stat then
206 if git_file_stat.type == 'directory' then
207 git_dir = git_path
208 elseif git_file_stat.type == 'file' then
209 -- separate git-dir or submodule is used
210 local file = io.open(git_path)
211 if file then
212 git_dir = file:read()
213 git_dir = git_dir and git_dir:match('gitdir: (.+)$')
214 file:close()
215 end
216 -- submodule / relative file path
217 if git_dir and git_dir:sub(1, 1) ~= sep and not git_dir:match('^%a:.*$') then
218 git_dir = git_path:match('(.*).git') .. git_dir
219 end
220 end
221 if git_dir then
222 -- Check for traditional HEAD file or reftable format
223 local head_file_stat = vim.loop.fs_stat(git_dir .. sep .. 'HEAD')
224 if head_file_stat and head_file_stat.type == 'file' then
225 break
226 end
227 -- Also accept reftable repos (they may not have a traditional HEAD file)
228 local reftable_stat = vim.loop.fs_stat(git_dir .. sep .. 'reftable' .. sep .. 'tables.list')
229 if reftable_stat and reftable_stat.type == 'file' then
230 break
231 end
232 git_dir = nil
233 end
234 end
235 root_dir = root_dir:match('(.*)' .. sep .. '.-')
236 end
237
238 git_dir_cache[file_dir] = git_dir
239 if dir_path == nil then
240 update_current_git_dir(git_dir)
241 end
242 return git_dir
243 end
244
245 ---initializes git_branch module
246 function M.init()
247 -- run watch head on load so branch is present when component is loaded
248 M.find_git_dir()
249 -- update branch state of BufEnter as different Buffer may be on different repos
250 modules.utils.define_autocmd('BufEnter', "lua require'lualine.components.branch.git_branch'.find_git_dir()")
251 end
252 function M.get_branch(bufnr)
253 if vim.g.actual_curbuf ~= nil and active_bufnr ~= vim.g.actual_curbuf then
254 -- Workaround for https://github.com/nvim-lualine/lualine.nvim/issues/286
255 -- See upstream issue https://github.com/neovim/neovim/issues/15300
256 -- Diff is out of sync re sync it.
257 M.find_git_dir()
258 end
259 if bufnr then
260 return branch_cache[bufnr] or ''
261 else
262 branch_cache[vim.api.nvim_get_current_buf()] = current_git_branch
263 end
264 return current_git_branch
265 end
266
267 return M