Add git sync, and stop trusting the cached default branch
BuildEmpire/Totara renamed its default branch from main to totara-20. Nothing local noticed, because refs/remotes/origin/HEAD is written once at clone time and remote.<name>.followRemoteHEAD defaults to `create`, which only fills the ref in when it is missing. So `git default-branch` still said main, as did nvim's diff-against-branch prompt. bin/git-sync does the start-of-work sequence for a fork - fast-forward the default branch from upstream, push it to origin - and works out which branch that is by asking the server, not the cache. It sets the cached HEADs from the answer, so everything reading that ref agrees afterwards. It lives in bin/ rather than as an alias because git picks up git-<name> on the $PATH as a subcommand, and this is more shell than a gitconfig alias should hold. followRemoteHEAD = always is set for origin as well, so an ordinary fetch keeps the ref current without running the script. The nvim prompt now prefers origin, then upstream, then the remaining remotes. It took the first remote alphabetically before, which in be-edition means kdog - a colleague's fork - rather than anything authoritative. Verified against a fixture of bare repos whose default branch is totara-20 and whose cached HEAD is stale: the branch is created tracking origin when absent, fast-forwarded when behind, pushed to the fork, refuses to merge when the local copy has diverged, and is a no-op on a second run. The nvim prompt was checked in three real repos.
This commit is contained in:
parent
d4a4c68787
commit
80683b82c1
4 changed files with 137 additions and 11 deletions
|
|
@ -16,18 +16,35 @@ map('<leader>gd', '<Cmd>CodeDiff<CR>', 'Diff working tree')
|
|||
map('<leader>gh', '<Cmd>CodeDiff history %<CR>', 'File history')
|
||||
map('<leader>gH', '<Cmd>CodeDiff history<CR>', 'Branch history')
|
||||
|
||||
-- The base branch name varies per repo (main, master, develop, ...), so ask
|
||||
-- the first remote's HEAD what it is rather than hardcoding one.
|
||||
-- A remote's cached HEAD, or nil if it has none. Kept current by
|
||||
-- remote.origin.followRemoteHEAD in the gitconfig and by `git sync`; without
|
||||
-- those this is whatever the default branch was at clone time.
|
||||
local function remote_head(remote)
|
||||
local prefix = 'refs/remotes/' .. remote .. '/'
|
||||
-- --quiet so a remote with no cached HEAD stays silent rather than having its
|
||||
-- error text come back as the branch name.
|
||||
local ref = vim.fn.systemlist('git symbolic-ref --quiet ' .. prefix .. 'HEAD')[1]
|
||||
if not ref or ref:sub(1, #prefix) ~= prefix then
|
||||
return nil
|
||||
end
|
||||
return ref:sub(#prefix + 1)
|
||||
end
|
||||
|
||||
-- The base branch name varies per repo (main, master, totara-20, ...), so ask a
|
||||
-- remote rather than hardcoding one. origin first: in the fork workflow that is
|
||||
-- my fork, and `git sync` keeps its default branch level with upstream's. Only
|
||||
-- then upstream, then whatever remotes exist - `git remote` sorts them
|
||||
-- alphabetically, so first-available on its own can hand back a colleague's fork.
|
||||
local function default_branch()
|
||||
local remote = vim.fn.systemlist('git remote')[1]
|
||||
if not remote or remote == '' then
|
||||
return ''
|
||||
local candidates = { 'origin', 'upstream' }
|
||||
vim.list_extend(candidates, vim.fn.systemlist('git remote'))
|
||||
for _, remote in ipairs(candidates) do
|
||||
local branch = remote_head(remote)
|
||||
if branch then
|
||||
return branch
|
||||
end
|
||||
end
|
||||
local ref = vim.fn.systemlist('git symbolic-ref refs/remotes/' .. remote .. '/HEAD')[1]
|
||||
if not ref or ref == '' then
|
||||
return ''
|
||||
end
|
||||
return ref:gsub('^refs/remotes/' .. remote .. '/', '')
|
||||
return ''
|
||||
end
|
||||
|
||||
map('<leader>gm', function()
|
||||
|
|
|
|||
Loading…
Reference in a new issue