tea pr checkout: dont create local branches #314

Merged
lunny merged 8 commits from noerw/tea:checkout-use-remote-tracking-branch into master 2021-03-02 13:50:12 +00:00
3 changed files with 16 additions and 19 deletions
Showing only changes of commit 750f8fa43e - Show all commits

View File

@ -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

View File

@ -11,6 +11,7 @@ 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
@ -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,16 @@ 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
// try to find a matching existing branch, otherwise use the remote tracking branch
localRef := 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",
localRef.String(), pr.Head.Ref)
if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil {
localRef = git_plumbing.NewBranchReferenceName(b.Name)
info = fmt.Sprintf("Found matching local branch %s, checking it out", localRef.Short())
}
return localRepo.TeaCheckout(localBranchName)
fmt.Println(info)
return localRepo.TeaCheckout(localRef)
}

View File

@ -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
}
}