Skip to content

Commit 7926f91

Browse files
authored
Merge pull request #39 from github/tidy-git
Tidy up handling of Git references.
2 parents befa39e + 3d8a044 commit 7926f91

File tree

9 files changed

+37
-63
lines changed

9 files changed

+37
-63
lines changed

internal/cachedirectory/cachedirectory.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"path"
99
"path/filepath"
1010

11-
"github.com/go-git/go-git/v5"
1211
"github.com/pkg/errors"
1312
)
1413

@@ -18,8 +17,6 @@ const errorCacheParentDoesNotExist = "Cannot create cache directory because its
1817
const errorPushNonCache = "The cache directory you have provided does not appear to be valid. Please check it exists and that you have run the `pull` command to populate it."
1918
const errorCacheLocked = "The cache directory is locked, likely due to a `pull` command being interrupted. Please run `pull` again to ensure all required data is downloaded."
2019

21-
const CacheReferencePrefix = "refs/remotes/" + git.DefaultRemoteName + "/"
22-
2320
type CacheDirectory struct {
2421
path string
2522
}

internal/pull/pull.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const sourceOwner = "github"
3232
const sourceRepository = "codeql-action"
3333
const sourceURL = "https://github.com/" + sourceOwner + "/" + sourceRepository + ".git"
3434

35-
var relevantReferences = regexp.MustCompile("^refs/remotes/" + git.DefaultRemoteName + "/(heads|tags)/(main|v\\d+)$")
35+
var relevantReferences = regexp.MustCompile("^refs/(heads|tags)/(main|v\\d+)$")
3636

3737
const defaultConfigurationPath = "src/defaults.json"
3838

@@ -75,13 +75,10 @@ func (pullService *pullService) pullGit(fresh bool) error {
7575
return errors.Wrap(err, "Error removing existing Git remote.")
7676
}
7777

78-
_, err = localRepository.CreateRemote(&config.RemoteConfig{
78+
remote := git.NewRemote(localRepository.Storer, &config.RemoteConfig{
7979
Name: git.DefaultRemoteName,
8080
URLs: []string{pullService.gitCloneURL},
8181
})
82-
if err != nil {
83-
return errors.Wrap(err, "Error setting Git remote.")
84-
}
8582

8683
var credentials *githttp.BasicAuth
8784
if pullService.sourceToken != "" {
@@ -91,10 +88,6 @@ func (pullService *pullService) pullGit(fresh bool) error {
9188
}
9289
}
9390

94-
remote, err := localRepository.Remote(git.DefaultRemoteName)
95-
if err != nil {
96-
return errors.Wrap(err, "Error getting remote.")
97-
}
9891
remoteReferences, err := remote.List(&git.ListOptions{Auth: credentials})
9992
if err != nil {
10093
return errors.Wrap(err, "Error listing remote references.")
@@ -108,8 +101,7 @@ func (pullService *pullService) pullGit(fresh bool) error {
108101
return nil
109102
}
110103
for _, remoteReference := range remoteReferences {
111-
remoteReferenceName := strings.TrimPrefix(remoteReference.Name().String(), "refs/")
112-
if cachedirectory.CacheReferencePrefix+remoteReferenceName == localReference.Name().String() {
104+
if remoteReference.Name().String() == localReference.Name().String() {
113105
return nil
114106
}
115107
}
@@ -120,11 +112,11 @@ func (pullService *pullService) pullGit(fresh bool) error {
120112
return nil
121113
})
122114

123-
err = localRepository.FetchContext(pullService.ctx, &git.FetchOptions{
115+
err = remote.FetchContext(pullService.ctx, &git.FetchOptions{
124116
RemoteName: git.DefaultRemoteName,
125117
RefSpecs: []config.RefSpec{
126-
config.RefSpec("+refs/heads/*:" + cachedirectory.CacheReferencePrefix + "heads/*"),
127-
config.RefSpec("+refs/tags/*:" + cachedirectory.CacheReferencePrefix + "tags/*"),
118+
config.RefSpec("+refs/heads/*:refs/heads/*"),
119+
config.RefSpec("+refs/tags/*:refs/tags/*"),
128120
},
129121
Progress: os.Stderr,
130122
Tags: git.NoTags,

internal/pull/pull_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ func TestPullGitFresh(t *testing.T) {
8787
err := pullService.pullGit(true)
8888
require.NoError(t, err)
8989
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
90-
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
91-
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
92-
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/remotes/origin/heads/v3",
93-
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/tags/v2",
90+
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
91+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
92+
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
93+
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
9494
// It is expected that we still pull these even though they don't match the expected pattern. We just ignore them later on.
95-
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/heads/very-ignored-branch",
96-
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/tags/an-ignored-tag-too",
97-
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/a-ref-that-will-need-pruning",
95+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
96+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
97+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning",
9898
})
9999
}
100100

@@ -123,12 +123,12 @@ func TestPullGitNotFreshWithChanges(t *testing.T) {
123123
err = pullService.pullGit(false)
124124
require.NoError(t, err)
125125
test.CheckExpectedReferencesInRepository(t, pullService.cacheDirectory.GitPath(), []string{
126-
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main",
127-
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1",
128-
"33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/remotes/origin/heads/v3", // v3 was force-pushed, and should have been force-pulled too.
129-
"42d077b4730d1ba413f7bb7e0fa7c98653fb0c78 refs/remotes/origin/heads/v4", // v4 is a new branch.
130-
"bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/tags/an-ignored-tag-too",
131-
"26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit",
126+
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
127+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
128+
"33d42021633d74bcd0bf9c95e3d3159131a5faa7 refs/heads/v3", // v3 was force-pushed, and should have been force-pulled too.
129+
"42d077b4730d1ba413f7bb7e0fa7c98653fb0c78 refs/heads/v4", // v4 is a new branch.
130+
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
131+
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit",
132132
})
133133
}
134134

internal/push/push.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"golang.org/x/oauth2"
3030
)
3131

32-
const remoteName = "enterprise"
3332
const repositoryHomepage = "https://github.com/github/codeql-action-sync-tool/"
3433

3534
const errorAlreadyExists = "The destination repository already exists, but it was not created with the CodeQL Action sync tool. If you are sure you want to push the CodeQL Action to it, re-run this command with the `--force` flag."
@@ -132,14 +131,10 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
132131
return errors.Wrap(err, "Error reading Git repository from cache.")
133132
}
134133

135-
_ = gitRepository.DeleteRemote(remoteName)
136-
_, err = gitRepository.CreateRemote(&config.RemoteConfig{
137-
Name: remoteName,
134+
remote := git.NewRemote(gitRepository.Storer, &config.RemoteConfig{
135+
Name: git.DefaultRemoteName,
138136
URLs: []string{remoteURL},
139137
})
140-
if err != nil {
141-
return errors.Wrap(err, "Error adding repository remote.")
142-
}
143138

144139
credentials := &githttp.BasicAuth{
145140
Username: "x-access-token",
@@ -154,37 +149,31 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
154149
}
155150
refSpecBatches = append(refSpecBatches, []config.RefSpec{})
156151
for _, releasePathStat := range releasePathStats {
157-
refSpecBatches[0] = append(refSpecBatches[0], config.RefSpec("+"+cachedirectory.CacheReferencePrefix+"tags/"+releasePathStat.Name()+":refs/tags/"+releasePathStat.Name()))
152+
refSpecBatches[0] = append(refSpecBatches[0], config.RefSpec("+refs/tags/"+releasePathStat.Name()+":refs/tags/"+releasePathStat.Name()))
158153
}
159154
} else {
160155
// 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.
161156
refSpecBatches = [][]config.RefSpec{
162157
[]config.RefSpec{
163-
config.RefSpec("+" + cachedirectory.CacheReferencePrefix + "heads/main:refs/heads/main"),
158+
config.RefSpec("+refs/heads/main:refs/heads/main"),
164159
},
165160
[]config.RefSpec{
166-
config.RefSpec("+" + cachedirectory.CacheReferencePrefix + "heads/*:refs/heads/*"),
167-
config.RefSpec("+" + cachedirectory.CacheReferencePrefix + "tags/*:refs/tags/*"),
161+
config.RefSpec("+refs/*:refs/*"),
168162
},
169163
}
170164
}
171165
for _, refSpecs := range refSpecBatches {
172-
err = gitRepository.PushContext(pushService.ctx, &git.PushOptions{
173-
RemoteName: remoteName,
174-
RefSpecs: refSpecs,
175-
Auth: credentials,
176-
Progress: os.Stderr,
177-
Force: true,
166+
err = remote.PushContext(pushService.ctx, &git.PushOptions{
167+
RefSpecs: refSpecs,
168+
Auth: credentials,
169+
Progress: os.Stderr,
170+
Force: true,
178171
})
179172
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
180173
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
181174
}
182175
}
183176

184-
err = gitRepository.DeleteRemote(remoteName)
185-
if err != nil {
186-
return errors.Wrap(err, "Error removing repository remote.")
187-
}
188177
return nil
189178
}
190179

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# pack-refs with: peeled fully-peeled sorted
2-
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/remotes/origin/heads/main
3-
26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/heads/v1
4-
e529a54fad10a936308b2220e05f7f00757f8e7c refs/remotes/origin/heads/v3
5-
bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/heads/very-ignored-branch
6-
bd82b85707bc13904e3526517677039d4da4a9bb refs/remotes/origin/tags/an-ignored-tag-too
7-
26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/tags/codeql-bundle-20200101
8-
26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/tags/codeql-bundle-20200630
9-
26936381e619a01122ea33993e3cebc474496805 refs/remotes/origin/tags/v2
2+
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
3+
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
4+
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
5+
bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch
6+
bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too
7+
26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101
8+
26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630
9+
26936381e619a01122ea33993e3cebc474496805 refs/tags/v2

internal/push/push_test/action-cache-initial/git/refs/remotes/enterprise/main

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/push/push_test/action-cache-initial/git/refs/remotes/enterprise/v1

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/push/push_test/action-cache-initial/git/refs/remotes/enterprise/v3

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/push/push_test/action-cache-initial/git/refs/remotes/enterprise/very-ignored-branch

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)