Skip to content

Commit

Permalink
Final commands and doc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tulioz committed Dec 5, 2024
1 parent 8332f0b commit ed973ef
Show file tree
Hide file tree
Showing 77 changed files with 367 additions and 181 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ Create a new branch and make some changes:
```sh
$ av branch feature-1
$ echo "Hello, world!" > hello.txt
$ git add hello.txt
$ git commit -m "Add hello.txt"
$ av commit -A -m "Add hello.txt"
```

Create a PR:
Expand All @@ -56,8 +55,7 @@ previous PR:
```sh
$ av branch feature-2
$ echo "Another feature" >> hello.txt
$ git add hello.txt
$ git commit -m "Update hello.txt"
$ av commit -a -m "Update hello.txt"
$ av pr
```

Expand Down Expand Up @@ -253,14 +251,14 @@ yum update

| Command | Description |
| ------------------- | ---------------------------------------------------- |
| `av adopt` | Adopt a branch that is not created from `av branch`. |
| `av branch` | Create a new child branch from the current branch. |
| `av commit --amend` | Amend the last commit and rebase the children. |
| `av pr` | Create or update a PR. |
| `av reorder` | Reorder the branches. |
| `av reparent` | Change the parent of the current branch. |
| `av restack` | Rebase the branches to their parents. |
| `av split-commit` | Split the last commit. |
| `av stack adopt` | Adopt a branch that is not created from `av branch`. |
| `av stack restack` | Rebase the branches to their parents. |
| `av switch` | Check out branches interactively. |
| `av sync --all` | Fetch and rebase all branches. |
| `av tree` | Visualize the PRs. |
Expand Down
69 changes: 35 additions & 34 deletions cmd/av/stack_adopt.go → cmd/av/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ import (
"github.com/spf13/cobra"
)

var stackAdoptFlags struct {
var adoptFlags struct {
Parent string
DryRun bool
}

var stackAdoptCmd = &cobra.Command{
var adoptCmd = &cobra.Command{
Use: "adopt",
Short: "Adopt branches that are not managed by av",
Long: `Adopt branches that are not managed by av.
Long: strings.TrimSpace(`
Adopt branches that are not managed by av.
This command will show a list of branches that are not managed by av. You can choose which branches
should be adopted to av.
If you want to adopt the current branch, you can use the --parent flag to specify the parent branch.
For example, "av stack adopt --parent main" will adopt the current branch with the main branch as
the parent.`,
For example, "av adopt --parent main" will adopt the current branch with the main branch as
the parent.`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
repo, err := getRepo()
if err != nil {
return err
Expand All @@ -56,11 +57,11 @@ the parent.`,
}

currentBranch := status.CurrentBranch
if stackAdoptFlags.Parent != "" {
return stackAdoptForceAdoption(repo, db, currentBranch, stackAdoptFlags.Parent)
if adoptFlags.Parent != "" {
return adoptForceAdoption(repo, db, currentBranch, adoptFlags.Parent)
}

return uiutils.RunBubbleTea(&stackAdoptViewModel{
return uiutils.RunBubbleTea(&adoptViewModel{
repo: repo,
db: db,
currentHEADBranch: plumbing.NewBranchReferenceName(currentBranch),
Expand All @@ -72,7 +73,7 @@ the parent.`,
},
}

func stackAdoptForceAdoption(repo *git.Repo, db meta.DB, currentBranch, parent string) error {
func adoptForceAdoption(repo *git.Repo, db meta.DB, currentBranch, parent string) error {
if currentBranch == "" {
return errors.New("the current repository state is at a detached HEAD")
}
Expand Down Expand Up @@ -120,19 +121,19 @@ func stackAdoptForceAdoption(repo *git.Repo, db meta.DB, currentBranch, parent s
}
tx.SetBranch(branch)
}
if stackAdoptFlags.DryRun {
if adoptFlags.DryRun {
return nil
}
return tx.Commit()
}

type stackAdoptTreeInfo struct {
type adoptTreeInfo struct {
branches map[plumbing.ReferenceName]*treedetector.BranchPiece
rootNodes []*stackutils.StackTreeNode
adoptionTargets []plumbing.ReferenceName
}

type stackAdoptViewModel struct {
type adoptViewModel struct {
repo *git.Repo
db meta.DB
currentHEADBranch plumbing.ReferenceName
Expand All @@ -141,23 +142,23 @@ type stackAdoptViewModel struct {
spinner spinner.Model
currentCursor plumbing.ReferenceName
chosenTargets map[plumbing.ReferenceName]bool
treeInfo *stackAdoptTreeInfo
treeInfo *adoptTreeInfo
adoptionComplete bool
adoptionInProgress bool

err error
}

func (vm stackAdoptViewModel) Init() tea.Cmd {
func (vm adoptViewModel) Init() tea.Cmd {
return tea.Batch(vm.spinner.Tick, vm.initCmd)
}

func (vm stackAdoptViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (vm adoptViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case error:
vm.err = msg
return vm, tea.Quit
case *stackAdoptTreeInfo:
case *adoptTreeInfo:
vm.treeInfo = msg
if len(vm.treeInfo.adoptionTargets) == 0 {
return vm, tea.Quit
Expand All @@ -167,7 +168,7 @@ func (vm stackAdoptViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
vm.chosenTargets[branch] = true
}
vm.currentCursor = vm.treeInfo.adoptionTargets[0]
if stackAdoptFlags.DryRun {
if adoptFlags.DryRun {
return vm, tea.Quit
}
case adoptionCompleteMsg:
Expand Down Expand Up @@ -199,7 +200,7 @@ func (vm stackAdoptViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return vm, nil
}

func (vm stackAdoptViewModel) initCmd() tea.Msg {
func (vm adoptViewModel) initCmd() tea.Msg {
unmanagedBranches, err := vm.getUnmanagedBranches()
if err != nil {
return err
Expand All @@ -209,14 +210,14 @@ func (vm stackAdoptViewModel) initCmd() tea.Msg {
return err
}
nodes := treedetector.ConvertToStackTree(vm.db, pieces, plumbing.HEAD, false)
return &stackAdoptTreeInfo{
return &adoptTreeInfo{
branches: pieces,
rootNodes: nodes,
adoptionTargets: vm.getAdoptionTargets(nodes[0]),
}
}

func (vm *stackAdoptViewModel) getUnmanagedBranches() ([]plumbing.ReferenceName, error) {
func (vm *adoptViewModel) getUnmanagedBranches() ([]plumbing.ReferenceName, error) {
tx := vm.db.ReadTx()
adoptedBranches := tx.AllBranches()
branches, err := vm.repo.GoGitRepo().Branches()
Expand All @@ -238,7 +239,7 @@ func (vm *stackAdoptViewModel) getUnmanagedBranches() ([]plumbing.ReferenceName,
return ret, nil
}

func (vm stackAdoptViewModel) getAdoptionTargets(
func (vm adoptViewModel) getAdoptionTargets(
node *stackutils.StackTreeNode,
) []plumbing.ReferenceName {
var ret []plumbing.ReferenceName
Expand All @@ -254,7 +255,7 @@ func (vm stackAdoptViewModel) getAdoptionTargets(
return ret
}

func (vm stackAdoptViewModel) getPreviousBranch() plumbing.ReferenceName {
func (vm adoptViewModel) getPreviousBranch() plumbing.ReferenceName {
if vm.treeInfo == nil {
return vm.currentCursor
}
Expand All @@ -269,7 +270,7 @@ func (vm stackAdoptViewModel) getPreviousBranch() plumbing.ReferenceName {
return vm.currentCursor
}

func (vm stackAdoptViewModel) getNextBranch() plumbing.ReferenceName {
func (vm adoptViewModel) getNextBranch() plumbing.ReferenceName {
if vm.treeInfo == nil {
return vm.currentCursor
}
Expand All @@ -284,7 +285,7 @@ func (vm stackAdoptViewModel) getNextBranch() plumbing.ReferenceName {
return vm.currentCursor
}

func (vm *stackAdoptViewModel) toggleAdoption(branch plumbing.ReferenceName) {
func (vm *adoptViewModel) toggleAdoption(branch plumbing.ReferenceName) {
if vm.treeInfo == nil {
return
}
Expand Down Expand Up @@ -313,7 +314,7 @@ func (vm *stackAdoptViewModel) toggleAdoption(branch plumbing.ReferenceName) {

type adoptionCompleteMsg struct{}

func (vm stackAdoptViewModel) adoptBranches() tea.Msg {
func (vm adoptViewModel) adoptBranches() tea.Msg {
tx := vm.db.WriteTx()
for branch := range vm.chosenTargets {
piece := vm.treeInfo.branches[branch]
Expand All @@ -333,7 +334,7 @@ func (vm stackAdoptViewModel) adoptBranches() tea.Msg {
return adoptionCompleteMsg{}
}

func (vm stackAdoptViewModel) View() string {
func (vm adoptViewModel) View() string {
var ss []string
if vm.treeInfo != nil {
choosing := false
Expand Down Expand Up @@ -410,14 +411,14 @@ func (vm stackAdoptViewModel) View() string {
return ret
}

func (vm stackAdoptViewModel) ExitError() error {
func (vm adoptViewModel) ExitError() error {
if vm.err != nil {
return actions.ErrExitSilently{ExitCode: 1}
}
return nil
}

func (vm stackAdoptViewModel) renderBranch(branch plumbing.ReferenceName, isTrunk bool) string {
func (vm adoptViewModel) renderBranch(branch plumbing.ReferenceName, isTrunk bool) string {
if isTrunk {
return branch.Short()
}
Expand Down Expand Up @@ -451,16 +452,16 @@ func (vm stackAdoptViewModel) renderBranch(branch plumbing.ReferenceName, isTrun
}

func init() {
stackAdoptCmd.Flags().StringVar(
&stackAdoptFlags.Parent, "parent", "",
adoptCmd.Flags().StringVar(
&adoptFlags.Parent, "parent", "",
"force specifying the parent branch",
)
stackAdoptCmd.Flags().BoolVar(
&stackAdoptFlags.DryRun, "dry-run", false,
adoptCmd.Flags().BoolVar(
&adoptFlags.DryRun, "dry-run", false,
"dry-run adoption",
)

_ = stackAdoptCmd.RegisterFlagCompletionFunc(
_ = adoptCmd.RegisterFlagCompletionFunc(
"parent",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
branches, _ := allBranches()
Expand Down
12 changes: 7 additions & 5 deletions cmd/av/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var authCmd = &cobra.Command{
Short: "Check user authentication status",
SilenceUsage: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
if err := checkAviatorAuthStatus(); err != nil {
fmt.Fprintln(os.Stderr, colors.Warning(err.Error()))
}
Expand All @@ -36,10 +36,12 @@ func checkAviatorAuthStatus() error {

var query struct{ avgql.ViewerSubquery }
if err := avClient.Query(context.Background(), &query, nil); err != nil {
return err
}
if err := query.CheckViewer(); err != nil {
return err
if avgql.IsHTTPUnauthorized(err) {
return errors.New(
"You are not logged in to Aviator. Please verify that your API token is correct.",
)
}
return errors.Wrap(err, "Failed to query Aviator")
}

fmt.Fprint(os.Stderr,
Expand Down
3 changes: 2 additions & 1 deletion cmd/av/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Generates the diff between the working tree and the parent branch
(i.e., the diff between the current branch and the previous branch in the stack).
`),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
repo, err := getRepo()
if err != nil {
return err
Expand Down
11 changes: 6 additions & 5 deletions cmd/av/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "Fetch latest repository state from GitHub",
RunE: func(cmd *cobra.Command, args []string) (reterr error) {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) (reterr error) {
repo, err := getRepo()
if err != nil {
return err
Expand Down Expand Up @@ -60,7 +61,7 @@ var fetchCmd = &cobra.Command{
}
if cursor == "" {
// only do this once at the start
_, _ = fmt.Fprint(
fmt.Fprint(
os.Stderr,
"Fetching ", colors.UserInput(prsPage.TotalCount),
" open pull requests from GitHub...",
Expand All @@ -79,7 +80,7 @@ var fetchCmd = &cobra.Command{
logrus.WithField("branch", pr.HeadBranchName()).
Debug("found PR for known local branch")
if branchMeta.PullRequest == nil {
_, _ = fmt.Fprint(
fmt.Fprint(
os.Stderr,
" - Found pull request ", colors.UserInput(pr.Number),
" for branch ", colors.UserInput(pr.HeadBranchName()),
Expand All @@ -89,7 +90,7 @@ var fetchCmd = &cobra.Command{
// This shouldn't usually ever happen, not sure what the
// best thing to do here, but this handling allows you to
// close a PR then open a new one and then run `av fetch`
_, _ = fmt.Fprint(
fmt.Fprint(
os.Stderr,
" - ", color.RedString("WARNING: "),
"found new pull request ", colors.UserInput("#", pr.Number, " ", pr.Title),
Expand Down Expand Up @@ -122,7 +123,7 @@ var fetchCmd = &cobra.Command{
if err := tx.Commit(); err != nil {
return err
}
_, _ = fmt.Fprint(
fmt.Fprint(
os.Stderr,
"Updated ", color.GreenString("%d", updatedCount), " pull requests",
"\n",
Expand Down
7 changes: 6 additions & 1 deletion cmd/av/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aviator-co/av/internal/meta"
"github.com/aviator-co/av/internal/meta/jsonfiledb"
"github.com/aviator-co/av/internal/meta/refmeta"
"github.com/aviator-co/av/internal/utils/colors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -139,7 +140,11 @@ func deprecateCommand(
}

deprecatedCommand.PreRun = func(cmd *cobra.Command, args []string) {
fmt.Printf("This command is deprecated. Please use '%s' instead.\n", newCmd)
fmt.Print(
colors.Warning("This command is deprecated. Please use "),
colors.CliCmd("'", newCmd, "'"),
colors.Warning(" instead.\n"),
)
}

return &deprecatedCommand
Expand Down
5 changes: 3 additions & 2 deletions cmd/av/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize the repository for Aviator CLI",
RunE: func(cmd *cobra.Command, args []string) (reterr error) {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) (reterr error) {
repo, err := getRepo()
if err != nil {
return err
Expand Down Expand Up @@ -55,7 +56,7 @@ var initCmd = &cobra.Command{
if err := tx.Commit(); err != nil {
return err
}
_, _ = fmt.Println("Successfully initialized repository for use with av!")
fmt.Println("Successfully initialized repository for use with av!")
return nil
},
}
Loading

0 comments on commit ed973ef

Please sign in to comment.