Skip to content

Commit

Permalink
Fix bug (when pushing a new branch)
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanbanken committed Apr 20, 2021
1 parent 0e5f3f8 commit c88bbb0
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ func Main() {
orFatal(err, fmt.Sprintf("base branch %q does not exist, check your inputs", *outputBase))

headRef, err := outputRepo.Reference(headRefName, true)
var beforeRefspecs []config.RefSpec = nil
if err == nil && !headRef.Hash().IsZero() {
// Reuse existing head branch
log.Printf("Using %s as existing head", headRefName)
err = w.Checkout(&git.CheckoutOptions{
Branch: headRefName,
})
orFatal(err, fmt.Sprintf("worktree checkout to %s (%s)", headRefName, headRef.Hash()))
// Store current head for safe push
beforeRefspecs = []config.RefSpec{config.RefSpec(fmt.Sprintf("%s:%s", headRef.Hash(), headRefName))}
} else if err == plumbing.ErrReferenceNotFound {
// Create new head branch
log.Printf("Creating head branch %s from base %s", headRefName, baseRefName)
Expand All @@ -137,11 +140,6 @@ func Main() {
} else {
orFatal(err, "worktree checkout failed")
}

// Store head for safe push
head, err := outputRepo.Head()
orFatal(err, "determining head")
beforeRefspec := config.RefSpec(fmt.Sprintf("%s:%s", head.Hash(), headRefName))
log.Println()

// Commit options
Expand Down Expand Up @@ -175,7 +173,7 @@ func Main() {
log.Printf("Pushing %s", refspec)
err = outputRepo.Push(&git.PushOptions{
RefSpecs: []config.RefSpec{refspec},
RequireRemoteRefs: []config.RefSpec{beforeRefspec},
RequireRemoteRefs: beforeRefspecs,
Auth: gitAuth,
Progress: prefixw.New(os.Stderr, "> "),
})
Expand Down

0 comments on commit c88bbb0

Please sign in to comment.