MCPcopy Create free account
hub / github.com/go-git/go-git / PackRefs

Method PackRefs

storage/filesystem/dotgit/dotgit.go:1046–1109  ·  view source on GitHub ↗

PackRefs packs all loose refs into the packed-refs file. This implementation only works under the assumption that the view of the file system won't be updated during this operation. This strategy would not work on a general file system though, without locking each loose reference and checking it a

()

Source from the content-addressed store, hash-verified

1044// When `all` is false, it would only pack refs that have already been
1045// packed, plus all tags.
1046func (d *DotGit) PackRefs() (err error) {
1047 // Lock packed-refs, and create it if it doesn't exist yet.
1048 f, err := d.openAndLockPackedRefs(true)
1049 if err != nil {
1050 return err
1051 }
1052 defer ioutil.CheckClose(f, &err)
1053
1054 // Gather all refs using addRefsFromRefDir and addRefsFromPackedRefs.
1055 var refs []*plumbing.Reference
1056 seen := make(map[plumbing.ReferenceName]bool)
1057 if err = d.addRefsFromRefDir(&refs, seen); err != nil {
1058 return err
1059 }
1060 if len(refs) == 0 {
1061 // Nothing to do!
1062 return nil
1063 }
1064 numLooseRefs := len(refs)
1065 if err = d.addRefsFromPackedRefsFile(&refs, f, seen); err != nil {
1066 return err
1067 }
1068
1069 // Write them all to a new temp packed-refs file.
1070 tmp, err := d.fs.TempFile("", tmpPackedRefsPrefix)
1071 if err != nil {
1072 return err
1073 }
1074 tmpName := tmp.Name()
1075 defer func() {
1076 ioutil.CheckClose(tmp, &err)
1077 _ = d.fs.Remove(tmpName) // don't check err, we might have renamed it
1078 }()
1079
1080 w := bufio.NewWriter(tmp)
1081 for _, ref := range refs {
1082 _, err = w.WriteString(ref.String() + "\n")
1083 if err != nil {
1084 return err
1085 }
1086 }
1087 err = w.Flush()
1088 if err != nil {
1089 return err
1090 }
1091
1092 // Rename the temp packed-refs file.
1093 err = d.rewritePackedRefsWhileLocked(tmp, f)
1094 if err != nil {
1095 return err
1096 }
1097
1098 // Delete all the loose refs, while still holding the packed-refs
1099 // lock.
1100 for _, ref := range refs[:numLooseRefs] {
1101 path := d.fs.Join(".", ref.Name().String())
1102 err = d.fs.Remove(path)
1103 if err != nil && !os.IsNotExist(err) {

Callers

nothing calls this directly

Calls 11

openAndLockPackedRefsMethod · 0.95
addRefsFromRefDirMethod · 0.95
CheckCloseFunction · 0.92
TempFileMethod · 0.80
JoinMethod · 0.80
NameMethod · 0.65
StringMethod · 0.65
RemoveMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected