mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-10-31 01:05:26 +01:00 
			
		
		
		
	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:
		| @@ -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) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Norwin
					Norwin