💅 pr checkout --force

This commit is contained in:
Mislav Marohnić 2021-01-22 22:29:14 +01:00
parent a89fa1ebed
commit 874375f01e

View file

@ -61,8 +61,8 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
},
}
cmd.Flags().BoolVarP(&opts.RecurseSubmodules, "recurse-submodules", "", false, "Update all active submodules (recursively)")
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Force merge into local branch")
cmd.Flags().BoolVarP(&opts.RecurseSubmodules, "recurse-submodules", "", false, "Update all submodules after checkout")
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Reset the existing local branch to the latest state of the pull request")
return cmd
}
@ -118,10 +118,10 @@ func checkoutRun(opts *CheckoutOptions) error {
// local branch already exists
if _, err := git.ShowRefs("refs/heads/" + newBranchName); err == nil {
cmdQueue = append(cmdQueue, []string{"git", "checkout", newBranchName})
// If forced reset to remote
if opts.Force {
cmdQueue = append(cmdQueue, []string{"git", "reset", "--hard", fmt.Sprintf("refs/remotes/%s", remoteBranch)})
} else {
// TODO: check if non-fast-forward and suggest to use `--force`
cmdQueue = append(cmdQueue, []string{"git", "merge", "--ff-only", fmt.Sprintf("refs/remotes/%s", remoteBranch)})
}
} else {
@ -149,19 +149,19 @@ func checkoutRun(opts *CheckoutOptions) error {
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseURLOrName, ref})
// If forced reset to remote
if opts.Force {
cmdQueue = append(cmdQueue, []string{"git", "reset", "--hard", "FETCH_HEAD"})
} else {
// TODO: check if non-fast-forward and suggest to use `--force`
cmdQueue = append(cmdQueue, []string{"git", "merge", "--ff-only", "FETCH_HEAD"})
}
} else {
// create a new branch
// If forced reset to remote
if opts.Force {
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseURLOrName, fmt.Sprintf("%s:%s", ref, newBranchName), "--force"})
} else {
// TODO: check if non-fast-forward and suggest to use `--force`
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseURLOrName, fmt.Sprintf("%s:%s", ref, newBranchName)})
}
cmdQueue = append(cmdQueue, []string{"git", "checkout", newBranchName})