MCPcopy
hub / github.com/go-git/go-git / checkReferenceAndTruncate

Method checkReferenceAndTruncate

storage/filesystem/dotgit/dotgit.go:682–706  ·  view source on GitHub ↗

checkReferenceAndTruncate reads the reference from the given file, or the `pack-refs` file if the file was empty. Then it checks that the old reference matches the stored reference and truncates the file.

(f billy.File, old *plumbing.Reference)

Source from the content-addressed store, hash-verified

680// the file was empty. Then it checks that the old reference matches the stored reference and
681// truncates the file.
682func (d *DotGit) checkReferenceAndTruncate(f billy.File, old *plumbing.Reference) error {
683 if old == nil {
684 return nil
685 }
686
687 ref, err := d.readReferenceFrom(f, old.Name().String())
688 if errors.Is(err, ErrEmptyRefFile) {
689 // This may happen if the reference is being read from a newly created file.
690 // In that case, try getting the reference from the packed refs file.
691 ref, err = d.packedRef(old.Name())
692 }
693
694 if err != nil {
695 return err
696 }
697
698 if ref.Hash() != old.Hash() {
699 return storage.ErrReferenceHasChanged
700 }
701 _, err = f.Seek(0, io.SeekStart)
702 if err != nil {
703 return err
704 }
705 return f.Truncate(0)
706}
707
708func (d *DotGit) SetRef(r, old *plumbing.Reference) error {
709 var content string

Callers 1

setRefRwfsMethod · 0.95

Calls 7

readReferenceFromMethod · 0.95
packedRefMethod · 0.95
IsMethod · 0.80
StringMethod · 0.65
NameMethod · 0.65
HashMethod · 0.65
SeekMethod · 0.45

Tested by

no test coverage detected