Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add force flag to stack branch rename #177

Merged
merged 5 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/av/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
)

var prCmd = &cobra.Command{
Use: "pr",
Use: "pr",
Short: "manage pull requests",
}

func init() {
prCmd.AddCommand(
prCreateCmd,

)
}
19 changes: 17 additions & 2 deletions cmd/av/stack_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ var stackBranchFlags struct {
Parent string
// If true, rename the current branch ("move" in Git parlance, though we
// avoid that language here since we're not changing the branch's position
// within the stack).
// within the stack). The branch can only be renamed if a pull request does
// not exist.
Rename bool
// If true, rename the current branch even if a pull request exists.
Force bool
}
var stackBranchCmd = &cobra.Command{
Use: "branch [flags] <branch-name>",
Expand Down Expand Up @@ -46,7 +49,7 @@ internal tracking metadata that defines the order of branches within a stack.`,
}

if stackBranchFlags.Rename {
return stackBranchMove(repo, db, branchName)
return stackBranchMove(repo, db, branchName, stackBranchFlags.Force)
}

tx := db.WriteTx()
Expand Down Expand Up @@ -159,12 +162,15 @@ func init() {
// See the comment on stackBranchFlags.Rename.
stackBranchCmd.Flags().
BoolVarP(&stackBranchFlags.Rename, "rename", "m", false, "rename the current branch")
stackBranchCmd.Flags().
BoolVar(&stackBranchFlags.Force, "force", false, "force rename the current branch")
}

func stackBranchMove(
repo *git.Repo,
db meta.DB,
newBranch string,
force bool,
) (reterr error) {
oldBranch, err := repo.CurrentBranchName()
if err != nil {
Expand Down Expand Up @@ -193,9 +199,18 @@ func stackBranchMove(
Trunk: true,
}
}
currentMeta.PullRequest = nil
currentMeta.Name = newBranch
tx.SetBranch(currentMeta)

if !force {
if currentMeta.PullRequest != nil {
doratzeng marked this conversation as resolved.
Show resolved Hide resolved
return errors.New(
"cannot rename branch because a pull request already exists (bypass with the `--force` flag, but this will require closing this pull request and opening a new one using `av pr create`)",
doratzeng marked this conversation as resolved.
Show resolved Hide resolved
)
}
}

// Update all child branches to refer to the correct (renamed) parent.
children := meta.Children(tx, oldBranch)
for _, child := range children {
Expand Down
7 changes: 5 additions & 2 deletions docs/av-stack-branch.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ av-stack-branch - Create or rename a branch in the stack

# SYNOPSIS

`av stack branch [-m | --rename] [--parent <parent_branch>] <branch-name>`
`av stack branch [-m | --rename] [--force] [--parent <parent_branch>] <branch-name>`

# DESCRIPTION

Expand All @@ -25,4 +25,7 @@ internal tracking metadata that defines the order of branches within a stack.

`-m, --rename`
: Rename the current branch to the provided `<branch_name>` instead of
creating a new one.
creating a new one, only if a pull request does not exist.

`--force`
: Force rename the branch, even if a pull request exists.