#2427·go-git

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:

  1. rewritePackedRefsWithoutRef scans packed-refs line by line and calls d.processLine(line).
  2. When ref != nil && ref.Name() == name, it sets found = true and executes continue, omitting the ref line from the rewritten packed-refs file.
  3. On the very next line (^<target-sha>), d.processLine(line) hits case '^': return nil, nil.
  4. Because ref == nil, rewritePackedRefsWithoutRef writes the ^<target-sha> line into the temporary packed-refs file.

This causes two severe forms of corruption in packed-refs:

  1. 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 sorted header with no preceding reference record. Native git immediately aborts on all commands (git status, git fetch, git rev-list, etc.) with: fatal: unexpected line in .git/packed-refs: ^<sha>
  2. 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 in packed-refs.

go-git Version

v5.19.2

Steps to Reproduce

  1. Create a Git repository with two annotated tags (v1 and v2) and run git pack-refs --all:
    bash
    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-refs now contains:
    # pack-refs with: peeled fully-peeled sorted 
    c157a79031e1c40f85931829bc5fc552c157a790 refs/heads/main
    6c2d0f77dd6e2544771de0f3397ac387185ac351 refs/tags/v1
    ^c157a79031e1c40f85931829bc5fc552c157a790
    a1b2c3d4e5f60718293a4b5c6d7e8f9012345678 refs/tags/v2
    ^c157a79031e1c40f85931829bc5fc552c157a790
  2. Open the repository with go-git and remove refs/tags/v2:
    go
    r, _ := git.PlainOpen("repo")
    _ = r.Storer.RemoveReference(plumbing.NewTagReferenceName("v2"))
  3. Inspect .git/packed-refs (refs/tags/v2 is removed, but its ^<sha> peeled line remains):
    # pack-refs with: peeled fully-peeled sorted 
    c157a79031e1c40f85931829bc5fc552c157a790 refs/heads/main
    6c2d0f77dd6e2544771de0f3397ac387185ac351 refs/tags/v1
    ^c157a79031e1c40f85931829bc5fc552c157a790
    ^c157a79031e1c40f85931829bc5fc552c157a790
  4. Run git for-each-ref or git status:
    fatal: packed-refs line 5: unexpected peeled line: '^c157a79031e1c40f85931829bc5fc552c157a790'

Additional Information

No response