Skip to content

Commit 5bd18e0

Browse files
authored
Merge pull request #40 from github/pruning
Implement reference pruning on push.
2 parents 7926f91 + d888c9f commit 5bd18e0

25 files changed

+88
-12
lines changed

internal/push/push.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"path/filepath"
1515
"strings"
1616

17+
"github.com/go-git/go-git/v5/plumbing"
18+
1719
"github.com/github/codeql-action-sync/internal/githubapiutil"
1820

1921
log "github.com/sirupsen/logrus"
@@ -22,6 +24,7 @@ import (
2224
"github.com/github/codeql-action-sync/internal/version"
2325
"github.com/go-git/go-git/v5"
2426
"github.com/go-git/go-git/v5/config"
27+
"github.com/go-git/go-git/v5/plumbing/transport"
2528
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
2629
"github.com/google/go-github/v32/github"
2730
"github.com/mitchellh/ioprogress"
@@ -142,35 +145,53 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
142145
}
143146

144147
refSpecBatches := [][]config.RefSpec{}
148+
remoteReferences, err := remote.List(&git.ListOptions{Auth: credentials})
149+
if err != nil && err != transport.ErrEmptyRemoteRepository {
150+
return errors.Wrap(err, "Error listing remote references.")
151+
}
152+
deleteRefSpecs := []config.RefSpec{}
153+
for _, remoteReference := range remoteReferences {
154+
_, err := gitRepository.Reference(remoteReference.Name(), false)
155+
if err != nil && err != plumbing.ErrReferenceNotFound {
156+
return errors.Wrapf(err, "Error finding local reference %s.", remoteReference.Name())
157+
}
158+
if err == plumbing.ErrReferenceNotFound {
159+
deleteRefSpecs = append(deleteRefSpecs, config.RefSpec(":"+remoteReference.Name().String()))
160+
}
161+
}
162+
refSpecBatches = append(refSpecBatches, deleteRefSpecs)
163+
145164
if initialPush {
146165
releasePathStats, err := ioutil.ReadDir(pushService.cacheDirectory.ReleasesPath())
147166
if err != nil {
148167
return errors.Wrap(err, "Error reading releases.")
149168
}
150-
refSpecBatches = append(refSpecBatches, []config.RefSpec{})
169+
initialRefSpecs := []config.RefSpec{}
151170
for _, releasePathStat := range releasePathStats {
152-
refSpecBatches[0] = append(refSpecBatches[0], config.RefSpec("+refs/tags/"+releasePathStat.Name()+":refs/tags/"+releasePathStat.Name()))
171+
initialRefSpecs = append(initialRefSpecs, config.RefSpec("+refs/tags/"+releasePathStat.Name()+":refs/tags/"+releasePathStat.Name()))
153172
}
173+
refSpecBatches = append(refSpecBatches, initialRefSpecs)
154174
} else {
155175
// 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.
156-
refSpecBatches = [][]config.RefSpec{
176+
refSpecBatches = append(refSpecBatches,
157177
[]config.RefSpec{
158178
config.RefSpec("+refs/heads/main:refs/heads/main"),
159179
},
160180
[]config.RefSpec{
161181
config.RefSpec("+refs/*:refs/*"),
162182
},
163-
}
183+
)
164184
}
165185
for _, refSpecs := range refSpecBatches {
166-
err = remote.PushContext(pushService.ctx, &git.PushOptions{
167-
RefSpecs: refSpecs,
168-
Auth: credentials,
169-
Progress: os.Stderr,
170-
Force: true,
171-
})
172-
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
173-
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
186+
if len(refSpecs) != 0 {
187+
err = remote.PushContext(pushService.ctx, &git.PushOptions{
188+
RefSpecs: refSpecs,
189+
Auth: credentials,
190+
Progress: os.Stderr,
191+
})
192+
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
193+
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
194+
}
174195
}
175196
}
176197

internal/push/push_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,35 @@ func TestPushGit(t *testing.T) {
150150
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
151151
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
152152
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
153+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning",
154+
})
155+
156+
pushService = getTestPushService(t, "./push_test/action-cache-modified/", "")
157+
err = pushService.pushGit(&repository, true)
158+
require.NoError(t, err)
159+
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
160+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
161+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
162+
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
163+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
164+
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
165+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
166+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
167+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
168+
})
169+
170+
err = pushService.pushGit(&repository, false)
171+
require.NoError(t, err)
172+
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
173+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
174+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
175+
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
176+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
177+
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
178+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
179+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
180+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
181+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit",
153182
})
154183
}
155184

internal/push/push_test/action-cache-initial/git/packed-refs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pack-refs with: peeled fully-peeled sorted
2+
26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning
23
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
34
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
45
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/main
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# pack-refs with: peeled fully-peeled sorted
2+
26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit
3+
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
4+
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
5+
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
6+
bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch
7+
bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too
8+
26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101
9+
26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630
10+
26936381e619a01122ea33993e3cebc474496805 refs/tags/v2

internal/push/push_test/action-cache-modified/git/refs/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This isn't really a CodeQL bundle either!
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"tag_name": "codeql-bundle-20200101",
3+
"target_commitish": "main"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This isn't really a CodeQL bundle!
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"tag_name": "codeql-bundle-20200630",
3+
"target_commitish": "main"
4+
}

0 commit comments

Comments
 (0)