Skip to content

Commit

Permalink
feat: improve av stack reorder UX (#207)
Browse files Browse the repository at this point in the history
This was originally going to be just fixing an issue that occurs where running `av stack reorder` on a stack that has a branch with no commits.

That ended up being comparatively easy to fix (it's essentially just the two line change in `git.go`), but realized that we didn't actually include any instructions when running `av stack reorder`, so I added that.

Now, when editing a reorder plan, it looks like:
```
stack-branch 2023-10-20-blegg-1 --trunk main@49331c9878615a5227fe56ede8521f7e5d8462e7  # this branch has no commits

# Instructions:
#
# Commands:
# sb, stack-branch <branch-name> [--parent <parent-branch-name> | --trunk <trunk-branch-name>]
#         Create a new branch as part of a stack. If parent is not specified,
#         the previous branch in the stack is used (if any). If trunk is
#         specified, the branch is rooted from the given branch.
#         trunk-branch-name can be either a branch name or a branch name with a
#         commit ID in the format "<branch-name>@<commit-id>".
# p, pick <commit-id>
#         Pick a commit to be included in the stack. Only valid after a
#         stack-branch command.
```
  • Loading branch information
twavv authored Oct 23, 2023
1 parent bd00516 commit 2056291
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/actions/sync_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ func syncBranchRebase(

// Scenario 3: the branch is not up-to-date with its parent.
_, _ = fmt.Fprint(os.Stderr,
" - synching branch ", colors.UserInput(branch.Name),
" on latest commit ", git.ShortSha(parentHead),
" - syncing branch ", colors.UserInput(branch.Name),
" on latest commit ", colors.UserInput(git.ShortSha(parentHead)),
" of parent branch ", colors.UserInput(parentState.Name),
"\n",
)
Expand Down
3 changes: 3 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type Output struct {

func (o Output) Lines() []string {
s := strings.TrimSpace(string(o.Stdout))
if s == "" {
return nil
}
return strings.Split(s, "\n")
}

Expand Down
16 changes: 16 additions & 0 deletions internal/reorder/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func EditPlan(repo *git.Repo, plan []Cmd) ([]Cmd, error) {
text.WriteString(cmd.String())
text.WriteString("\n")
}
text.WriteString(instructionsText)

res, err := editor.Launch(repo, editor.Config{
Text: text.String(),
Expand Down Expand Up @@ -76,3 +77,18 @@ func Diff(old []Cmd, new []Cmd) PlanDiff {
AddedBranches: sliceutils.Subtract(newBranches, oldBranches),
}
}

const instructionsText = `
# Instructions:
#
# Commands:
# sb, stack-branch <branch-name> [--parent <parent-branch-name> | --trunk <trunk-branch-name>]
# Create a new branch as part of a stack. If parent is not specified,
# the previous branch in the stack is used (if any). If trunk is
# specified, the branch is rooted from the given branch.
# trunk-branch-name can be either a branch name or a branch name with a
# commit ID in the format "<branch-name>@<commit-id>".
# p, pick <commit-id>
# Pick a commit to be included in the stack. Only valid after a
# stack-branch command.
`
14 changes: 12 additions & 2 deletions internal/reorder/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func CreatePlan(repo *git.Repo, tx meta.ReadTx, rootBranch string) ([]Cmd, error
branchCmd.Trunk = branch.Parent.Name + "@" + trunkCommit
upstreamCommit = trunkCommit
}
cmds = append(cmds, branchCmd)

// Add a pick command for every commit associated with this branch
// Figure out the commits that belong to this branch.
// We'll use this to generate a "pick" command for each commit.
commitIDs, err := repo.RevList(git.RevListOpts{
Specifiers: []string{branchName, "^" + upstreamCommit},
Reverse: true,
Expand All @@ -48,13 +48,23 @@ func CreatePlan(repo *git.Repo, tx meta.ReadTx, rootBranch string) ([]Cmd, error
return nil, err
}

// If no commits associated with this branch, bail out early and add a
// helpful comment for the user.
if len(commitIDs) == 0 {
branchCmd.Comment = "this branch has no commits"
cmds = append(cmds, branchCmd)
continue
}

commitObjects, err := repo.GetRefs(&git.GetRefs{
Revisions: commitIDs,
})
if err != nil {
return nil, err
}

// Append the "stack-branch" command and each "pick" command to the plan
cmds = append(cmds, branchCmd)
for _, object := range commitObjects {
commit, err := git.ParseCommitContents(object.Contents)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/reorder/stackbranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type StackBranchCmd struct {
// The branch can be rooted at a given commit by appending "@<commit>" to the
// branch name.
Trunk string
// An optional comment to include in the reorder plan for this command.
Comment string
}

func (b StackBranchCmd) Execute(ctx *Context) error {
Expand Down Expand Up @@ -98,6 +100,10 @@ func (b StackBranchCmd) String() string {
sb.WriteString(" --trunk ")
sb.WriteString(b.Trunk)
}
if b.Comment != "" {
sb.WriteString(" # ")
sb.WriteString(b.Comment)
}
return sb.String()
}

Expand Down

0 comments on commit 2056291

Please sign in to comment.