Skip to content

Commit

Permalink
Check if remote branch exists before creating new one
Browse files Browse the repository at this point in the history
  • Loading branch information
michalschott committed Dec 12, 2022
1 parent b6affb2 commit 7b7f802
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ext/git/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,28 @@ func (m *nativeGitClient) Branch(sourceBranch string, targetBranch string) error
}
}

_, err := m.runCmd("branch", targetBranch)
// Check if remote branch already exist
_, err := m.runCmd("ls-remote", "--exit-code", "--heads", "origin", targetBranch)
if err != nil {
log.Infof("branch already exists")

// If so - checkout
_, err = m.runCmd("checkout", targetBranch)
if err != nil {
return fmt.Errorf("could not checkout target branch: %v", err)
}

// Rebase against source branch
_, err = m.runCmd("rebase", sourceBranch)
if err != nil {
return fmt.Errorf("could not rebase against source branch: %v", err)
}

return nil
}

// If remote branch doesn't exist, create new branch
_, err = m.runCmd("branch", targetBranch)
if err != nil {
return fmt.Errorf("could not create new branch: %v", err)
}
Expand Down

0 comments on commit 7b7f802

Please sign in to comment.