| 1 | package lfs |
| 2 | |
| 3 | import ( |
| 4 | "github.com/git-lfs/git-lfs/v3/git" |
| 5 | "github.com/git-lfs/git-lfs/v3/tools" |
| 6 | ) |
| 7 | |
| 8 | // calcSkippedRefs checks that locally cached versions of remote refs are still |
| 9 | // present on the remote before they are used as a 'from' point. If the server |
| 10 | // implements garbage collection and a remote branch had been deleted since we |
| 11 | // last did 'git fetch --prune', then the objects in that branch may have also |
| 12 | // been deleted on the server if unreferenced. If some refs are missing on the |
| 13 | // remote, use a more explicit diff command. |
| 14 | func calcSkippedRefs(remote string) []string { |
| 15 | cachedRemoteRefs, _ := git.CachedRemoteRefs(remote) |
| 16 | |
| 17 | // Since CachedRemoteRefs() only returns branches, request that |
| 18 | // RemoteRefs() ignore tags and also return only branches. |
| 19 | actualRemoteRefs, _ := git.RemoteRefs(remote, false) |
| 20 | |
| 21 | // The list of remote refs can be very large, so convert them to |
| 22 | // a set for faster lookups in the skip calculation loop. |
| 23 | actualRemoteRefsSet := tools.NewStringSet() |
| 24 | for _, ref := range actualRemoteRefs { |
| 25 | actualRemoteRefsSet.Add(ref.Name) |
| 26 | } |
| 27 | |
| 28 | // Only check for missing refs on remote; if the ref is different it has moved |
| 29 | // forward probably, and if not and the ref has changed to a non-descendant |
| 30 | // (force push) then that will cause a re-evaluation in a subsequent command. |
| 31 | var skippedRefs []string |
| 32 | for _, cachedRef := range cachedRemoteRefs { |
| 33 | if actualRemoteRefsSet.Contains(cachedRef.Name) { |
| 34 | skippedRefs = append(skippedRefs, "^"+cachedRef.Sha) |
| 35 | } |
| 36 | } |
| 37 | return skippedRefs |
| 38 | } |