Skip to content

git: Fix fetching after shallow clone. Fixes #305#766

Closed
AriehSchneier wants to merge 1 commit intogo-git:masterfrom
AriehSchneier:fix-fetch-after-shallow
Closed

git: Fix fetching after shallow clone. Fixes #305#766
AriehSchneier wants to merge 1 commit intogo-git:masterfrom
AriehSchneier:fix-fetch-after-shallow

Conversation

@AriehSchneier
Copy link
Contributor

@AriehSchneier AriehSchneier commented May 10, 2023

Also fixes #207

@@ -864,6 +865,21 @@ func getHavesFromRef(
toVisit := maxHavesToVisitPerRef
return walker.ForEach(func(c *object.Commit) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be just to completely ignore any errors returned from this walker.ForEach call.

Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boolean is redundant, so I would use struct{} instead for a slight smaller memory profile.

@AriehSchneier AriehSchneier force-pushed the fix-fetch-after-shallow branch from 8336a5d to 973deae Compare May 11, 2023 12:05
Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had another go at it and found a few other nits below.

haves[c.Hash] = true

// initialise the shallows map
if *shallows == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferencing a nil pointer leads to panic.

Suggested change
if *shallows == nil {
if shallows == nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private function that we only call in 1 location, it will also contain a value (I would have passed by reference if go had that feature but it doesn't). We need to check whether the location that this variable is pointing to is nil or not. I could change this to if shallows != nil && *shallows != nil { but IMHO that would add an extra unnecessary check.

remoteRefs map[plumbing.Hash]bool,
s storage.Storer,
haves map[plumbing.Hash]bool,
shallows *map[plumbing.Hash]struct{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will inevitably be passed "by reference".

Suggested change
shallows *map[plumbing.Hash]struct{},
shallows map[plumbing.Hash]struct{},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS if you test your suggested change you will find that nil will always be passed in and we will recalculate the map for every iteration of range localRefs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely right, thanks for sharing the link. 👍

// initialise the shallows map
if *shallows == nil {
shallowList, _ := s.Shallow()
*shallows = make(map[plumbing.Hash]struct{}, len(shallowList))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*shallows = make(map[plumbing.Hash]struct{}, len(shallowList))
shallows = make(map[plumbing.Hash]struct{}, len(shallowList))

shallowList, _ := s.Shallow()
*shallows = make(map[plumbing.Hash]struct{}, len(shallowList))
for _, sh := range shallowList {
(*shallows)[sh] = struct{}{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(*shallows)[sh] = struct{}{}
shallows[sh] = struct{}{}

}

// If this is a shallow reference don't try to iterate further
if _, ok := (*shallows)[c.Hash]; ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if _, ok := (*shallows)[c.Hash]; ok {
if _, ok := shallows[c.Hash]; ok {

}

err = getHavesFromRef(ref, remoteRefs, s, haves)
err = getHavesFromRef(ref, remoteRefs, s, haves, &shallows)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err = getHavesFromRef(ref, remoteRefs, s, haves, &shallows)
err = getHavesFromRef(ref, remoteRefs, s, haves, shallows)

Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had another go at it and found a few other nits below.

@AriehSchneier AriehSchneier force-pushed the fix-fetch-after-shallow branch from 973deae to 254c9e4 Compare May 12, 2023 04:08
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
@pjbgf
Copy link
Member

pjbgf commented May 24, 2023

Closing this in favour of #778

@pjbgf pjbgf closed this May 24, 2023
@AriehSchneier AriehSchneier deleted the fix-fetch-after-shallow branch May 27, 2023 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pull returns object not found

2 participants