@@ -14,6 +14,7 @@ import (
1414
1515	"code.gitea.io/gitea/models" 
1616	"code.gitea.io/gitea/models/db" 
17+ 	git_model "code.gitea.io/gitea/models/git" 
1718	issues_model "code.gitea.io/gitea/models/issues" 
1819	repo_model "code.gitea.io/gitea/models/repo" 
1920	user_model "code.gitea.io/gitea/models/user" 
@@ -564,7 +565,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(ctx context.Context, prs ...*bas
564565	return  nil 
565566}
566567
567- func  (g  * GiteaLocalUploader ) updateHeadBranchForPullRequest (pr  * base.PullRequest ) (head  string , err  error ) {
568+ func  (g  * GiteaLocalUploader ) updateHeadBranchForPullRequest (ctx  context. Context ,  pr  * base.PullRequest ) (head  string , err  error ) {
568569	// SECURITY: this pr must have been ensured safe 
569570	if  ! pr .EnsuredSafe  {
570571		log .Error ("PR #%d in %s/%s has not been checked for safety." , pr .Number , g .repoOwner , g .repoName )
@@ -576,22 +577,29 @@ func (g *GiteaLocalUploader) updateHeadBranchForPullRequest(pr *base.PullRequest
576577			return  "" , fmt .Errorf ("the PR[%d] does not have a head commit SHA" , pr .Number )
577578		}
578579		// ignore the original branch name because it belongs to the head repository 
579- 		headBranch  :=  fmt .Sprintf ("branch_for_pr_%d " , pr .Number )
580+ 		headBranch  :=  fmt .Sprintf ("%s-%s " , pr .Head . OwnerName ,  pr . Head . Ref )
580581		if  pr .State  !=  "closed"  {
582+ 			exist , err  :=  git_model .IsBranchExist (ctx , g .repo .ID , headBranch )
583+ 			if  err  !=  nil  {
584+ 				return  "" , fmt .Errorf ("failed to check if head branch[%s] exists for PR[%d]: %w" , headBranch , pr .Number , err )
585+ 			}
586+ 			if  exist  {
587+ 				headBranch  =  fmt .Sprintf ("%s-%s-%d" , pr .Head .OwnerName , pr .Head .Ref , pr .Number )
588+ 			}
581589			// create the head branch 
582- 			if  err  :=  g . gitRepo . CreateBranch ( headBranch ,  pr .Head .SHA ); err  !=  nil  {
590+ 			if  err  :=  repo_service . CreateNewBranchFromCommit ( ctx ,  g . doer ,  g . repo ,  g . gitRepo ,  pr .Head .SHA ,  headBranch ); err  !=  nil  {
583591				return  "" , fmt .Errorf ("failed to create head branch[%s] for PR[%d]: %w" , headBranch , pr .Number , err )
584592			}
585593		}
586- 		return  headBranch , nil   // assign a non-exist branch 
594+ 		return  headBranch , nil 
587595	}
588596
589597	if  pr .Head .SHA  !=  ""  {
590598		if  pr .Head .Ref  ==  ""  {
591599			headBranch  :=  fmt .Sprintf ("branch_for_pr_%d" , pr .Number )
592600			if  pr .State  !=  "closed"  {
593601				// create the head branch 
594- 				if  err  :=  g . gitRepo . CreateBranch ( headBranch ,  pr .Head .SHA ); err  !=  nil  {
602+ 				if  err  :=  repo_service . CreateNewBranchFromCommit ( ctx ,  g . doer ,  g . repo ,  g . gitRepo ,  pr .Head .SHA ,  headBranch ); err  !=  nil  {
595603					return  "" , fmt .Errorf ("failed to create head branch[%s] for PR[%d]: %w" , headBranch , pr .Number , err )
596604				}
597605			}
@@ -601,7 +609,7 @@ func (g *GiteaLocalUploader) updateHeadBranchForPullRequest(pr *base.PullRequest
601609		return  pr .Head .Ref , nil 
602610	}
603611
604- 	if  pr .Head .Ref  ==  ""  {
612+ 	if  pr .Head .Ref  ==  ""  {  // both sha and ref are empty 
605613		return  "" , fmt .Errorf ("the PR[%d] does not have a head commit SHA or ref" , pr .Number )
606614	}
607615
@@ -625,7 +633,7 @@ func (g *GiteaLocalUploader) newPullRequest(ctx context.Context, pr *base.PullRe
625633	milestoneID  :=  g .milestones [pr .Milestone ]
626634
627635	// recalculate and create head branch when necessary 
628- 	headBranch , err  :=  g .updateHeadBranchForPullRequest (pr )
636+ 	headBranch , err  :=  g .updateHeadBranchForPullRequest (ctx ,  pr )
629637	if  err  !=  nil  {
630638		return  nil , fmt .Errorf ("updateHeadBranchForPullRequest: %w" , err )
631639	}
0 commit comments