`tea pr checkout`: dont create local branches (#314)
This avoids creation of local branches, to avoid cluttering the local repo: - if the commit already exists on the tip of a local branch, check that one out - otherwise check out the remote tracking branch (`refs/remotes/<remote>/<head>`), and suggest what to do if you want to make changes. I'm not certain this behaviour is actually better, I suggest leaving this open for a while for people to try out the new behaviour: ``` tea pr checkout 314 make install ``` fixes #293 Co-authored-by: Norwin Roosen <git@nroo.de> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/314 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Norwin <noerw@noreply.gitea.io> Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
parent
3c1efd33e2
commit
e96cfdbbe7
|
@ -24,7 +24,13 @@ var CmdPullsCheckout = cli.Command{
|
|||
Description: `Locally check out the given PR`,
|
||||
Action: runPullsCheckout,
|
||||
ArgsUsage: "<pull index>",
|
||||
Flags: flags.AllDefaultFlags,
|
||||
Flags: append([]cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "branch",
|
||||
Aliases: []string{"b"},
|
||||
Usage: "Create a local branch if it doesn't exist yet",
|
||||
},
|
||||
}, flags.AllDefaultFlags...),
|
||||
}
|
||||
|
||||
func runPullsCheckout(cmd *cli.Context) error {
|
||||
|
@ -38,5 +44,5 @@ func runPullsCheckout(cmd *cli.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
return task.PullCheckout(ctx.Login, ctx.Owner, ctx.Repo, idx, interact.PromptPassword)
|
||||
return task.PullCheckout(ctx.Login, ctx.Owner, ctx.Repo, ctx.Bool("branch"), idx, interact.PromptPassword)
|
||||
}
|
||||
|
|
|
@ -38,13 +38,12 @@ func (r TeaRepo) TeaCreateBranch(localBranchName, remoteBranchName, remoteName s
|
|||
}
|
||||
|
||||
// TeaCheckout checks out the given branch in the worktree.
|
||||
func (r TeaRepo) TeaCheckout(branchName string) error {
|
||||
func (r TeaRepo) TeaCheckout(ref git_plumbing.ReferenceName) error {
|
||||
tree, err := r.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
localBranchRefName := git_plumbing.NewBranchReferenceName(branchName)
|
||||
return tree.Checkout(&git.CheckoutOptions{Branch: localBranchRefName})
|
||||
return tree.Checkout(&git.CheckoutOptions{Branch: ref})
|
||||
}
|
||||
|
||||
// TeaDeleteLocalBranch removes the given branch locally
|
||||
|
|
|
@ -11,10 +11,11 @@ import (
|
|||
local_git "code.gitea.io/tea/modules/git"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
git_plumbing "github.com/go-git/go-git/v5/plumbing"
|
||||
)
|
||||
|
||||
// PullCheckout checkout current workdir to the head branch of specified pull request
|
||||
func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, callback func(string) (string, error)) error {
|
||||
func PullCheckout(login *config.Login, repoOwner, repoName string, forceCreateBranch bool, index int64, callback func(string) (string, error)) error {
|
||||
client := login.Client()
|
||||
|
||||
localRepo, err := local_git.RepoForWorkdir()
|
||||
|
@ -40,12 +41,6 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64,
|
|||
remoteURL = pr.Head.Repository.SSHURL
|
||||
}
|
||||
|
||||
// try to find a matching existing branch, otherwise return branch in pulls/ namespace
|
||||
localBranchName := fmt.Sprintf("pulls/%v-%v", index, pr.Head.Ref)
|
||||
if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil {
|
||||
localBranchName = b.Name
|
||||
}
|
||||
|
||||
newRemoteName := fmt.Sprintf("pulls/%v", pr.Head.Repository.Owner.UserName)
|
||||
|
||||
// verify related remote is in local repo, otherwise add it
|
||||
|
@ -72,15 +67,32 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64,
|
|||
return err
|
||||
}
|
||||
|
||||
// checkout local branch
|
||||
err = localRepo.TeaCreateBranch(localBranchName, pr.Head.Ref, localRemoteName)
|
||||
if err == nil {
|
||||
fmt.Printf("Created branch '%s'\n", localBranchName)
|
||||
} else if err == git.ErrBranchExists {
|
||||
fmt.Println("There may be changes since you last checked out, run `git pull` to get them.")
|
||||
} else if err != nil {
|
||||
return err
|
||||
var info string
|
||||
var checkoutRef git_plumbing.ReferenceName
|
||||
|
||||
if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil {
|
||||
// if a matching branch exists, use that
|
||||
checkoutRef = git_plumbing.NewBranchReferenceName(b.Name)
|
||||
info = fmt.Sprintf("Found matching local branch %s, checking it out", checkoutRef.Short())
|
||||
} else if forceCreateBranch {
|
||||
// create a branch if wanted
|
||||
localBranchName := fmt.Sprintf("pulls/%v-%v", index, pr.Head.Ref)
|
||||
checkoutRef = git_plumbing.NewBranchReferenceName(localBranchName)
|
||||
if err = localRepo.TeaCreateBranch(localBranchName, pr.Head.Ref, localRemoteName); err == nil {
|
||||
info = fmt.Sprintf("Created branch '%s'\n", localBranchName)
|
||||
} else if err == git.ErrBranchExists {
|
||||
info = "There may be changes since you last checked out, run `git pull` to get them."
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// use the remote tracking branch
|
||||
checkoutRef = git_plumbing.NewRemoteReferenceName(localRemoteName, pr.Head.Ref)
|
||||
info = fmt.Sprintf(
|
||||
"Checking out remote tracking branch %s. To make changes, create a new branch:\n git checkout %s",
|
||||
checkoutRef.String(), pr.Head.Ref)
|
||||
}
|
||||
|
||||
return localRepo.TeaCheckout(localBranchName)
|
||||
fmt.Println(info)
|
||||
return localRepo.TeaCheckout(checkoutRef)
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
git_config "github.com/go-git/go-git/v5/config"
|
||||
git_plumbing "github.com/go-git/go-git/v5/plumbing"
|
||||
)
|
||||
|
||||
// PullClean deletes local & remote feature-branches for a closed pull
|
||||
|
@ -76,7 +77,8 @@ call me again with the --ignore-sha flag`, remoteBranch)
|
|||
}
|
||||
if headRef.Name().Short() == branch.Name {
|
||||
fmt.Printf("Checking out '%s' to delete local branch '%s'\n", defaultBranch, branch.Name)
|
||||
if err = r.TeaCheckout(defaultBranch); err != nil {
|
||||
ref := git_plumbing.NewBranchReferenceName(defaultBranch)
|
||||
if err = r.TeaCheckout(ref); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue