Improve the handling of orphan peeled lines
Author: rngassa-stackCreated Sep 25, 2026Updated Sep 25, 2026
Labelsbug
Bug Description
When dotgit.DotGit.RemoveReference (via rewritePackedRefsWithoutRef) deletes a reference from packed-refs that has a corresponding peeled line (^<sha>, such as an annotated tag in a # pack-refs with: peeled fully-peeled sorted file), it skips writing the reference line <tag-sha> refs/tags/<name>, but does not skip the immediately following ^<peeled-sha> line.
In storage/filesystem/dotgit/dotgit.go:
rewritePackedRefsWithoutRefscanspacked-refsline by line and callsd.processLine(line).- When
ref != nil && ref.Name() == name, it setsfound = trueand executescontinue, omitting the ref line from the rewrittenpacked-refsfile. - On the very next line (
^<target-sha>),d.processLine(line)hitscase '^': return nil, nil. - Because
ref == nil,rewritePackedRefsWithoutRefwrites the^<target-sha>line into the temporarypacked-refsfile.
This causes two severe forms of corruption in packed-refs:
- Total repository corruption when the deleted tag is the first entry in
packed-refs: The^<target-sha>line appears immediately after the# pack-refs with: peeled fully-peeled sortedheader with no preceding reference record. Nativegitimmediately aborts on all commands (git status,git fetch,git rev-list, etc.) with:fatal: unexpected line in .git/packed-refs: ^<sha> - Silent metadata corruption when the deleted tag is NOT the first entry: The
^<target-sha>line becomes attached as the peeled value of whatever reference happened to precede the deleted tag inpacked-refs.
go-git Version
v5.19.2
Steps to Reproduce
- Create a Git repository with two annotated tags (
v1andv2) and rungit pack-refs --all:git init repo && cd repo git commit --allow-empty -m "init" git tag -a v1 -m "tag v1" git tag -a v2 -m "tag v2" git pack-refs --all.git/packed-refsnow contains:# pack-refs with: peeled fully-peeled sorted c157a79031e1c40f85931829bc5fc552c157a790 refs/heads/main 6c2d0f77dd6e2544771de0f3397ac387185ac351 refs/tags/v1 ^c157a79031e1c40f85931829bc5fc552c157a790 a1b2c3d4e5f60718293a4b5c6d7e8f9012345678 refs/tags/v2 ^c157a79031e1c40f85931829bc5fc552c157a790 - Open the repository with
go-gitand removerefs/tags/v2:r, _ := git.PlainOpen("repo") _ = r.Storer.RemoveReference(plumbing.NewTagReferenceName("v2")) - Inspect
.git/packed-refs(refs/tags/v2is removed, but its^<sha>peeled line remains):# pack-refs with: peeled fully-peeled sorted c157a79031e1c40f85931829bc5fc552c157a790 refs/heads/main 6c2d0f77dd6e2544771de0f3397ac387185ac351 refs/tags/v1 ^c157a79031e1c40f85931829bc5fc552c157a790 ^c157a79031e1c40f85931829bc5fc552c157a790 - Run
git for-each-reforgit status:fatal: packed-refs line 5: unexpected peeled line: '^c157a79031e1c40f85931829bc5fc552c157a790'
Additional Information
No response
Source: go-git/go-git