Skip to content

Commit c47b536

Browse files
authored
Avoid pushing empty branches to remote (#320)
1 parent 3d89a12 commit c47b536

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

commands/createfixpullrequests.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (cfp *CreateFixPullRequestsCmd) openFixingPullRequest(impactedPackage, fixB
226226
}
227227

228228
log.Info("Pushing branch:", fixBranchName, "...")
229-
if err = cfp.gitManager.Push(false); err != nil {
229+
if err = cfp.gitManager.Push(false, fixBranchName); err != nil {
230230
return
231231
}
232232

@@ -257,7 +257,7 @@ func (cfp *CreateFixPullRequestsCmd) openAggregatedPullRequest(fixBranchName str
257257
return
258258
}
259259
log.Info("Pushing branch:", fixBranchName, "...")
260-
if err = cfp.gitManager.Push(true); err != nil {
260+
if err = cfp.gitManager.Push(true, fixBranchName); err != nil {
261261
return
262262
}
263263
if !exists {

commands/scanandfixrepos.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func (saf *ScanAndFixRepositories) downloadAndRunScanAndFix(repository *utils.Fr
8888

8989
func (saf ScanAndFixRepositories) setCommitBuildStatus(client vcsclient.VcsClient, repoConfig *utils.FrogbotRepoConfig, state vcsclient.CommitStatus, commitHash, description string) error {
9090
if err := client.SetCommitStatus(context.Background(), state, repoConfig.RepoOwner, repoConfig.RepoName, commitHash, utils.FrogbotCreatorName, description, utils.CommitStatusDetailsUrl); err != nil {
91-
return fmt.Errorf("failed to mark last commit as scanned due to: %s", err.Error())
91+
return fmt.Errorf("failed to mark last commit build status due to: %s", err.Error())
9292
}
93-
log.Info("Commit '%s' in repo '%s', has successfully marked as scanned", commitHash, repoConfig.RepoName)
93+
log.Info("Commit'", commitHash, "'in repo '", repoConfig.RepoName, "', has successfully marked as scanned")
9494
return nil
9595
}
9696

commands/utils/git.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"errors"
55
"fmt"
6+
"github.com/go-git/go-git/v5/config"
67
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
78
"github.com/go-git/go-git/v5/plumbing/transport"
89
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
@@ -19,6 +20,8 @@ import (
1920
"github.com/jfrog/jfrog-client-go/utils/log"
2021
)
2122

23+
const refFormat = "refs/heads/%s:refs/heads/%[1]s"
24+
2225
type GitManager struct {
2326
// repository represents a git repository as a .git dir.
2427
repository *git.Repository
@@ -200,21 +203,21 @@ func (gm *GitManager) BranchExistsInRemote(branchName string) (bool, error) {
200203
return false, nil
201204
}
202205

203-
func (gm *GitManager) Push(force bool) error {
206+
func (gm *GitManager) Push(force bool, branchName string) error {
204207
if gm.dryRun {
205208
// On dry run do not push to any remote
206209
return nil
207210
}
208211
// Pushing to remote
209-
err := gm.repository.Push(&git.PushOptions{
212+
if err := gm.repository.Push(&git.PushOptions{
210213
RemoteName: gm.remoteName,
211214
Auth: gm.auth,
212215
Force: force,
213-
})
214-
if err != nil {
215-
err = fmt.Errorf("git push failed with error: %s", err.Error())
216+
RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf(refFormat, branchName))},
217+
}); err != nil {
218+
return fmt.Errorf("git push failed with error: %s", err.Error())
216219
}
217-
return err
220+
return nil
218221
}
219222

220223
// IsClean returns true if all the files are in Unmodified status.

0 commit comments

Comments
 (0)