Skip to content

Commit 4ac0d4c

Browse files
committed
more durable fetch by trying all first and falling back to individual fetches
1 parent ca9ec72 commit 4ac0d4c

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

internal/git/gitops.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,30 @@ func (d *defaultOps) Fetch(remote string) error {
108108
}
109109

110110
func (d *defaultOps) FetchBranches(remote string, branches []string) error {
111-
if len(branches) == 0 {
111+
// Only fetch branches that already have a remote tracking ref.
112+
var tracked []string
113+
for _, b := range branches {
114+
ref := fmt.Sprintf("refs/remotes/%s/%s", remote, b)
115+
if err := runSilent("rev-parse", "--verify", "--quiet", ref); err == nil {
116+
tracked = append(tracked, b)
117+
}
118+
}
119+
if len(tracked) == 0 {
112120
return nil
113121
}
122+
// Fast path: fetch all tracked branches in a single call.
114123
args := []string{"fetch", remote}
115-
args = append(args, branches...)
116-
return runSilent(args...)
124+
args = append(args, tracked...)
125+
if err := runSilent(args...); err == nil {
126+
return nil
127+
}
128+
// Fallback: a ref may have been deleted on the remote while the
129+
// local tracking ref still exists. Fetch branches individually so
130+
// one missing ref doesn't block the others.
131+
for _, b := range tracked {
132+
_ = runSilent("fetch", remote, b)
133+
}
134+
return nil
117135
}
118136

119137
func (d *defaultOps) DefaultBranch() (string, error) {

0 commit comments

Comments
 (0)