Skip to content

Commit

Permalink
fix: Handle the original parent is a trunk case (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
draftcode authored May 16, 2023
1 parent 9f019ac commit 94e375a
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions internal/actions/sync_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,21 @@ func syncBranchRebase(
}, nil
}

var origUpstream string
if origParent.Trunk {
// We do not know the original trunk commit hashes. Use the current one as
// an approximation.
origUpstream = newUpstreamCommitHash
} else {
origUpstream = origParent.Head
}

continuation := SyncBranchContinuation{
NewParentName: parentState.Name,
}
rebase, err := repo.RebaseParse(git.RebaseOpts{
Branch: branch.Name,
Upstream: origParent.Head,
Upstream: origUpstream,
Onto: newUpstreamCommitHash,
})
if err != nil {
Expand Down Expand Up @@ -248,6 +257,16 @@ func syncBranchRebase(
// Note that we've introduced B into the history of stacked-2, but
// not C or D since those commits come after M.
newUpstreamCommitHash := origParentBranch.MergeCommit
var origUpstream string
if origParent.Trunk {
var err error
origUpstream, err = repo.RevParse(&git.RevParse{Rev: "origin/" + origParent.Name})
if err != nil {
return nil, errors.WrapIff(err, "failed to get HEAD of %q", origParent.Name)
}
} else {
origUpstream = origParent.Head
}
continuation := SyncBranchContinuation{
NewParentName: parentState.Name,
}
Expand All @@ -260,7 +279,7 @@ func syncBranchRebase(
}
rebase, err := repo.RebaseParse(git.RebaseOpts{
Branch: branch.Name,
Upstream: origParent.Head,
Upstream: origUpstream,
Onto: newUpstreamCommitHash,
})
if err != nil {
Expand Down Expand Up @@ -305,6 +324,18 @@ func syncBranchRebase(
" of parent branch ", colors.UserInput(parentState.Name),
"\n",
)
var origUpstream string
if origParent.Trunk {
// This can happen if the branch is originally a stack root and reparented to
// another branch (and became non-stack-root).
var err error
origUpstream, err = repo.RevParse(&git.RevParse{Rev: "origin/" + origParent.Name})
if err != nil {
return nil, errors.WrapIff(err, "failed to get HEAD of %q", origParent.Name)
}
} else {
origUpstream = origParent.Head
}
// We need to use `rebase --onto` here and be very careful about how we
// determine the commits that are being rebased on top of parentHead.
// Suppose we have a history like
Expand Down Expand Up @@ -339,7 +370,7 @@ func syncBranchRebase(
}
rebase, err := repo.RebaseParse(git.RebaseOpts{
Branch: branch.Name,
Upstream: origParent.Head,
Upstream: origUpstream,
Onto: parentHead,
})
if err != nil {
Expand Down

0 comments on commit 94e375a

Please sign in to comment.