Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stack reorder): Open editor to customize reorder plan (#167)
This is kind of the bare minimum to make `av stack reorder` work. There are still a few issues that need to be resolved before it can be released, but at the very least, reordering commits and moving them between branches works. # Test plan Created a two stack structure like ``` * 2023-06-06-two-b d11e918 (HEAD -> 2023-06-06-two) | * 2023-06-06-two-a 442caa6 | * 2023-06-06-one-b 4867243 (2023-06-06-one) | * 2023-06-06-one-a f9b4731 | * 2023-05-30-one-a (#957) 87232f0 (origin/main, origin/HEAD, main) ``` Running `av stack reorder` generates the default (noop) plan: ``` stack-branch 2023-06-06-one --trunk main@87232f0dbf7bf64004ad9e053ae75e6d096c416c pick f9b4731 # 2023-06-06-one-a pick 4867243 # 2023-06-06-one-b stack-branch 2023-06-06-two --parent 2023-06-06-one pick 442caa6 # 2023-06-06-two-a pick d11e918 # 2023-06-06-two-b ``` Can edit that to move a commit from `two` to `one`: ``` stack-branch 2023-06-06-one --trunk main@87232f0dbf7bf64004ad9e053ae75e6d096c416c pick f9b4731 # 2023-06-06-one-a pick 4867243 # 2023-06-06-one-b pick 442caa6 # 2023-06-06-two-a stack-branch 2023-06-06-two --parent 2023-06-06-one pick d11e918 # 2023-06-06-two-b ``` Running the reorder then outputs: ``` Starting branch 2023-06-06-one at 87232f0 - applied commit f9b4731 without conflict (HEAD is now at f9b4731) - applied commit 4867243 without conflict (HEAD is now at 4867243) - applied commit 442caa6 without conflict (HEAD is now at 442caa6) Starting branch 2023-06-06-two at 442caa6 - applied commit d11e918 without conflict (HEAD is now at d11e918) Reorder complete! The stack was reordered successfully. ``` and the branches are updated accordingly: ``` * 2023-06-06-two-b d11e918 (HEAD -> 2023-06-06-two) | * 2023-06-06-two-a 442caa6 (2023-06-06-one) | * 2023-06-06-one-b 4867243 | * 2023-06-06-one-a f9b4731 | * 2023-05-30-one-a (#957) 87232f0 (origin/main, origin/HEAD, main) ```
- Loading branch information
Showing
8 changed files
with
288 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package reorder | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/aviator-co/av/internal/editor" | ||
"github.com/aviator-co/av/internal/git" | ||
"github.com/aviator-co/av/internal/utils/typeutils" | ||
) | ||
|
||
// EditPlan opens the user's editor and allows them to edit the plan. | ||
func EditPlan(repo *git.Repo, plan []Cmd) ([]Cmd, error) { | ||
text := strings.Builder{} | ||
for i, cmd := range plan { | ||
if i > 0 && typeutils.Is[StackBranchCmd](cmd) { | ||
// Write an extra newline at the start of each branch command | ||
// (other than the first) to create a visual separation between | ||
// branches. | ||
text.WriteString("\n") | ||
} | ||
text.WriteString(cmd.String()) | ||
text.WriteString("\n") | ||
} | ||
|
||
res, err := editor.Launch(repo, editor.Config{ | ||
Text: text.String(), | ||
CommentPrefix: "#", | ||
EndOfLineComments: true, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var newPlan []Cmd | ||
lines := strings.Split(res, "\n") | ||
for _, line := range lines { | ||
line = strings.TrimSpace(line) | ||
if line == "" { | ||
continue | ||
} | ||
cmd, err := ParseCmd(line) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newPlan = append(newPlan, cmd) | ||
} | ||
|
||
return newPlan, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.