Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions internal/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"regexp"
"strings"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -90,6 +91,35 @@ func (pullService *pullService) pullGit(fresh bool) error {
}
}

remote, err := localRepository.Remote(git.DefaultRemoteName)
if err != nil {
return errors.Wrap(err, "Error getting remote.")
}
remoteReferences, err := remote.List(&git.ListOptions{Auth: credentials})
if err != nil {
return errors.Wrap(err, "Error listing remote references.")
}
localReferences, err := localRepository.References()
if err != nil {
return errors.Wrap(err, "Error listing local references.")
}
localReferences.ForEach(func(localReference *plumbing.Reference) error {
if !strings.HasPrefix(localReference.Name().String(), "refs/") {
return nil
}
for _, remoteReference := range remoteReferences {
remoteReferenceName := strings.TrimPrefix(remoteReference.Name().String(), "refs/")
if cachedirectory.CacheReferencePrefix+remoteReferenceName == localReference.Name().String() {
return nil
}
}
err := localRepository.Storer.RemoveReference(localReference.Name())
if err != nil {
return errors.Wrap(err, "Error pruning reference.")
}
return nil
})

err = localRepository.FetchContext(pullService.ctx, &git.FetchOptions{
RemoteName: git.DefaultRemoteName,
RefSpecs: []config.RefSpec{
Expand Down
5 changes: 2 additions & 3 deletions internal/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestPullGitFresh(t *testing.T) {
// It is expected that we still pull these even though they don't match the expected pattern. We just ignore them later on.
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/a-ref-that-will-need-pruning",
})
}

Expand Down Expand Up @@ -125,11 +126,9 @@ func TestPullGitNotFreshWithChanges(t *testing.T) {
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
"33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/remotes/origin/heads/v3", // v3 was force-pushed, and should have been force-pulled too.
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/tags/v2",
"42d077b4730d1ba413f7bb7e0fa7c98653fb0c78 refs/remotes/origin/heads/v4", // v4 is a new branch.
// We deleted these, but we don't currently do any pruning.
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit",
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pack-refs with: peeled fully-peeled sorted
26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pack-refs with: peeled fully-peeled sorted
26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/heads/v3
42d077b4730d1ba413f7bb7e0fa7c98653fb0c78 refs/heads/v4
26936381e619a01122ea33993e3cebc474496805 refs/tags/v2
bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too