Implement reference pruning on push.
This commit is contained in:
Родитель
7926f91bc1
Коммит
d888c9f324
|
@ -14,6 +14,8 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
|
|
||||||
"github.com/github/codeql-action-sync/internal/githubapiutil"
|
"github.com/github/codeql-action-sync/internal/githubapiutil"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -22,6 +24,7 @@ import (
|
||||||
"github.com/github/codeql-action-sync/internal/version"
|
"github.com/github/codeql-action-sync/internal/version"
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/config"
|
"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"
|
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||||
"github.com/google/go-github/v32/github"
|
"github.com/google/go-github/v32/github"
|
||||||
"github.com/mitchellh/ioprogress"
|
"github.com/mitchellh/ioprogress"
|
||||||
|
@ -142,35 +145,53 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
|
||||||
}
|
}
|
||||||
|
|
||||||
refSpecBatches := [][]config.RefSpec{}
|
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 {
|
if initialPush {
|
||||||
releasePathStats, err := ioutil.ReadDir(pushService.cacheDirectory.ReleasesPath())
|
releasePathStats, err := ioutil.ReadDir(pushService.cacheDirectory.ReleasesPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Error reading releases.")
|
return errors.Wrap(err, "Error reading releases.")
|
||||||
}
|
}
|
||||||
refSpecBatches = append(refSpecBatches, []config.RefSpec{})
|
initialRefSpecs := []config.RefSpec{}
|
||||||
for _, releasePathStat := range releasePathStats {
|
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 {
|
} 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.
|
// 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{
|
||||||
config.RefSpec("+refs/heads/main:refs/heads/main"),
|
config.RefSpec("+refs/heads/main:refs/heads/main"),
|
||||||
},
|
},
|
||||||
[]config.RefSpec{
|
[]config.RefSpec{
|
||||||
config.RefSpec("+refs/*:refs/*"),
|
config.RefSpec("+refs/*:refs/*"),
|
||||||
},
|
},
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
for _, refSpecs := range refSpecBatches {
|
for _, refSpecs := range refSpecBatches {
|
||||||
err = remote.PushContext(pushService.ctx, &git.PushOptions{
|
if len(refSpecs) != 0 {
|
||||||
RefSpecs: refSpecs,
|
err = remote.PushContext(pushService.ctx, &git.PushOptions{
|
||||||
Auth: credentials,
|
RefSpecs: refSpecs,
|
||||||
Progress: os.Stderr,
|
Auth: credentials,
|
||||||
Force: true,
|
Progress: os.Stderr,
|
||||||
})
|
})
|
||||||
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
|
if err != nil && errors.Cause(err) != git.NoErrAlreadyUpToDate {
|
||||||
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
|
return errors.Wrap(err, "Error pushing Action to GitHub Enterprise Server.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -150,6 +150,35 @@ func TestPushGit(t *testing.T) {
|
||||||
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
|
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
|
||||||
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
|
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
|
||||||
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
|
"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",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# pack-refs with: peeled fully-peeled sorted
|
# pack-refs with: peeled fully-peeled sorted
|
||||||
|
26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning
|
||||||
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
|
b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main
|
||||||
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
|
26936381e619a01122ea33993e3cebc474496805 refs/heads/v1
|
||||||
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
|
e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
ref: refs/heads/main
|
|
@ -0,0 +1,4 @@
|
||||||
|
[core]
|
||||||
|
repositoryformatversion = 0
|
||||||
|
filemode = true
|
||||||
|
bare = true
|
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/26/936381e619a01122ea33993e3cebc474496805
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/26/936381e619a01122ea33993e3cebc474496805
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/28/509716746a9e77e3f752e9df740cb443bf9960
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/28/509716746a9e77e3f752e9df740cb443bf9960
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/61/8dc8ffee7af67e4e2dfdbe416c53cca6099fab
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/61/8dc8ffee7af67e4e2dfdbe416c53cca6099fab
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/80/8d4d2d6b7aa39089b99ca0d89f1fc6a2bc63ec
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/80/8d4d2d6b7aa39089b99ca0d89f1fc6a2bc63ec
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/86/e83156c07aea03c86a7cd7c5f6b0ee1453c2d7
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/86/e83156c07aea03c86a7cd7c5f6b0ee1453c2d7
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/8e/38374296a8ca208f5e526da24c5b09f3f48624
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/8e/38374296a8ca208f5e526da24c5b09f3f48624
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/b9/f01aa2c50f49898d4c7845a66be8824499fe9d
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/b9/f01aa2c50f49898d4c7845a66be8824499fe9d
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/bd/82b85707bc13904e3526517677039d4da4a9bb
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/bd/82b85707bc13904e3526517677039d4da4a9bb
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/cd/0f803f72414d81157588de0cb622f47f4bb9bc
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/cd/0f803f72414d81157588de0cb622f47f4bb9bc
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/d1/231c269d15582e06ded28babdb8c224ba7e6ed
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/d1/231c269d15582e06ded28babdb8c224ba7e6ed
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/e5/29a54fad10a936308b2220e05f7f00757f8e7c
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/e5/29a54fad10a936308b2220e05f7f00757f8e7c
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/e5/f2bcf5a8815c924448ff3a54455983df118ee4
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/e5/f2bcf5a8815c924448ff3a54455983df118ee4
Normal file
Двоичный файл не отображается.
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/e7/014e2af66d5b702946763455de2dcf9eb93c4f
Normal file
Двоичные данные
internal/push/push_test/action-cache-modified/git/objects/e7/014e2af66d5b702946763455de2dcf9eb93c4f
Normal file
Двоичный файл не отображается.
|
@ -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
|
|
@ -0,0 +1 @@
|
||||||
|
This isn't really a CodeQL bundle either!
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"tag_name": "codeql-bundle-20200101",
|
||||||
|
"target_commitish": "main"
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
This isn't really a CodeQL bundle!
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"tag_name": "codeql-bundle-20200630",
|
||||||
|
"target_commitish": "main"
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче