@@ -14,6 +14,8 @@ import (
14
14
"path/filepath"
15
15
"strings"
16
16
17
+ "github.com/go-git/go-git/v5/plumbing"
18
+
17
19
"github.com/github/codeql-action-sync/internal/githubapiutil"
18
20
19
21
log "github.com/sirupsen/logrus"
@@ -22,6 +24,7 @@ import (
22
24
"github.com/github/codeql-action-sync/internal/version"
23
25
"github.com/go-git/go-git/v5"
24
26
"github.com/go-git/go-git/v5/config"
27
+ "github.com/go-git/go-git/v5/plumbing/transport"
25
28
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
26
29
"github.com/google/go-github/v32/github"
27
30
"github.com/mitchellh/ioprogress"
@@ -142,35 +145,53 @@ func (pushService *pushService) pushGit(repository *github.Repository, initialPu
142
145
}
143
146
144
147
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
+
145
164
if initialPush {
146
165
releasePathStats , err := ioutil .ReadDir (pushService .cacheDirectory .ReleasesPath ())
147
166
if err != nil {
148
167
return errors .Wrap (err , "Error reading releases." )
149
168
}
150
- refSpecBatches = append ( refSpecBatches , []config.RefSpec {})
169
+ initialRefSpecs := []config.RefSpec {}
151
170
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 ()))
153
172
}
173
+ refSpecBatches = append (refSpecBatches , initialRefSpecs )
154
174
} else {
155
175
// 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 ,
157
177
[]config.RefSpec {
158
178
config .RefSpec ("+refs/heads/main:refs/heads/main" ),
159
179
},
160
180
[]config.RefSpec {
161
181
config .RefSpec ("+refs/*:refs/*" ),
162
182
},
163
- }
183
+ )
164
184
}
165
185
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
+ }
174
195
}
175
196
}
176
197
0 commit comments