Skip to content

Implement reference pruning on push. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2020
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
45 changes: 33 additions & 12 deletions internal/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"path/filepath"
"strings"

"github.com/go-git/go-git/v5/plumbing"

"github.com/github/codeql-action-sync/internal/githubapiutil"

log "github.com/sirupsen/logrus"
Expand All @@ -22,6 +24,7 @@ import (
"github.com/github/codeql-action-sync/internal/version"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/transport"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v32/github"
"github.com/mitchellh/ioprogress"
Expand Down Expand Up @@ -142,35 +145,53 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
}

refSpecBatches := [][]config.RefSpec{}
remoteReferences, err := remote.List(&git.ListOptions{Auth: credentials})
if err != nil && err != transport.ErrEmptyRemoteRepository {
return errors.Wrap(err, "Error listing remote references.")
}
deleteRefSpecs := []config.RefSpec{}
for _, remoteReference := range remoteReferences {
_, err := gitRepository.Reference(remoteReference.Name(), false)
if err != nil && err != plumbing.ErrReferenceNotFound {
return errors.Wrapf(err, "Error finding local reference %s.", remoteReference.Name())
}
if err == plumbing.ErrReferenceNotFound {
deleteRefSpecs = append(deleteRefSpecs, config.RefSpec(":"+remoteReference.Name().String()))
}
}
refSpecBatches = append(refSpecBatches, deleteRefSpecs)

if initialPush {
releasePathStats, err := ioutil.ReadDir(pushService.cacheDirectory.ReleasesPath())
if err != nil {
return errors.Wrap(err, "Error reading releases.")
}
refSpecBatches = append(refSpecBatches, []config.RefSpec{})
initialRefSpecs := []config.RefSpec{}
for _, releasePathStat := range releasePathStats {
refSpecBatches[0] = append(refSpecBatches[0], config.RefSpec("+refs/tags/"+releasePathStat.Name()+":refs/tags/"+releasePathStat.Name()))
initialRefSpecs = append(initialRefSpecs, config.RefSpec("+refs/tags/"+releasePathStat.Name()+":refs/tags/"+releasePathStat.Name()))
}
refSpecBatches = append(refSpecBatches, initialRefSpecs)
} else {
// We've got to push `main` on its own, so that it will be made the default branch if the repository has just been created. We then push everything else afterwards.
refSpecBatches = [][]config.RefSpec{
refSpecBatches = append(refSpecBatches,
[]config.RefSpec{
config.RefSpec("+refs/heads/main:refs/heads/main"),
},
[]config.RefSpec{
config.RefSpec("+refs/*:refs/*"),
},
}
)
}
for _, refSpecs := range refSpecBatches {
err = remote.PushContext(pushService.ctx, &git.PushOptions{
RefSpecs: refSpecs,
Auth: credentials,
Progress: os.Stderr,
Force: true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had to remove Force: true here due to go-git/go-git#158, but it shouldn't matter anyway because the refspecs we are using are already forces.

})
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
if len(refSpecs) != 0 {
err = remote.PushContext(pushService.ctx, &git.PushOptions{
RefSpecs: refSpecs,
Auth: credentials,
Progress: os.Stderr,
})
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions internal/push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ func TestPushGit(t *testing.T) {
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning",
})

pushService = getTestPushService(t, "./push_test/action-cache-modified/", "")
err = pushService.pushGit(&repository, true)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
})

err = pushService.pushGit(&repository, false)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
"26936381e619a01122ea33993e3cebc474496805 refs/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
1 change: 1 addition & 0 deletions internal/push/push_test/action-cache-modified/git/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/main
4 changes: 4 additions & 0 deletions internal/push/push_test/action-cache-modified/git/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions internal/push/push_test/action-cache-modified/git/packed-refs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch
bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too
26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101
26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630
26936381e619a01122ea33993e3cebc474496805 refs/tags/v2
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This isn't really a CodeQL bundle either!
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tag_name": "codeql-bundle-20200101",
"target_commitish": "main"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This isn't really a CodeQL bundle!
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tag_name": "codeql-bundle-20200630",
"target_commitish": "main"
}