@@ -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
1921log "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"
2528githttp "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
144147refSpecBatches := [][]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+
145164if initialPush {
146165releasePathStats , err := ioutil .ReadDir (pushService .cacheDirectory .ReleasesPath ())
147166if err != nil {
148167return errors .Wrap (err , "Error reading releases." )
149168}
150- refSpecBatches = append ( refSpecBatches , []config.RefSpec {})
169+ initialRefSpecs := []config.RefSpec {}
151170for _ , 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 {
158178config .RefSpec ("+refs/heads/main:refs/heads/main" ),
159179},
160180[]config.RefSpec {
161181config .RefSpec ("+refs/*:refs/*" ),
162182},
163- }
183+ )
164184}
165185for _ , 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
0 commit comments