Skip to content

Commit

Permalink
Merge pull request #40 from github/pruning
Browse files Browse the repository at this point in the history
Implement reference pruning on push.
  • Loading branch information
chrisgavin authored Aug 28, 2020
2 parents 7926f91 + d888c9f commit 5bd18e0
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 12 deletions.
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,
})
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"
}

0 comments on commit 5bd18e0

Please sign in to comment.