aboutsummaryrefslogtreecommitdiffstats
path: root/remote.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-05-24 20:12:55 +0100
committerGitHub <noreply@github.com>2023-05-24 20:12:55 +0100
commitb98b813a17d32f4fa29a3ef2e9f4c38c5a97b440 (patch)
tree034e45271df32054b7994356d8358e9ac2f33f36 /remote.go
parent84d9be20ca15d29bebc629e5b6f29dab78cc69ba (diff)
parentcdba5330cd205312443e8553f434829c4c5e66ee (diff)
downloadgo-git-b98b813a17d32f4fa29a3ef2e9f4c38c5a97b440.tar.gz
Merge pull request #778 from AriehSchneier/fix-fetch-after-shallow-v2
git: Fix fetching after shallow clone. Fixes #305
Diffstat (limited to 'remote.go')
-rw-r--r--remote.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/remote.go b/remote.go
index 7b2741a..0e72aad 100644
--- a/remote.go
+++ b/remote.go
@@ -459,7 +459,7 @@ func (r *Remote) fetch(ctx context.Context, o *FetchOptions) (sto storer.Referen
req.Wants, err = getWants(r.s, refs)
if len(req.Wants) > 0 {
- req.Haves, err = getHaves(localRefs, remoteRefs, r.s)
+ req.Haves, err = getHaves(localRefs, remoteRefs, r.s, o.Depth)
if err != nil {
return nil, err
}
@@ -837,6 +837,7 @@ func getHavesFromRef(
remoteRefs map[plumbing.Hash]bool,
s storage.Storer,
haves map[plumbing.Hash]bool,
+ depth int,
) error {
h := ref.Hash()
if haves[h] {
@@ -862,7 +863,13 @@ func getHavesFromRef(
// commits from the history of each ref.
walker := object.NewCommitPreorderIter(commit, haves, nil)
toVisit := maxHavesToVisitPerRef
- return walker.ForEach(func(c *object.Commit) error {
+ // But only need up to the requested depth
+ if depth > 0 && depth < maxHavesToVisitPerRef {
+ toVisit = depth
+ }
+ // It is safe to ignore any error here as we are just trying to find the references that we already have
+ // An example of a legitimate failure is we have a shallow clone and don't have the previous commit(s)
+ _ = walker.ForEach(func(c *object.Commit) error {
haves[c.Hash] = true
toVisit--
// If toVisit starts out at 0 (indicating there is no
@@ -873,12 +880,15 @@ func getHavesFromRef(
}
return nil
})
+
+ return nil
}
func getHaves(
localRefs []*plumbing.Reference,
remoteRefStorer storer.ReferenceStorer,
s storage.Storer,
+ depth int,
) ([]plumbing.Hash, error) {
haves := map[plumbing.Hash]bool{}
@@ -899,7 +909,7 @@ func getHaves(
continue
}
- err = getHavesFromRef(ref, remoteRefs, s, haves)
+ err = getHavesFromRef(ref, remoteRefs, s, haves, depth)
if err != nil {
return nil, err
}