Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if remote branch exists before creating new one #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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