mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Use complete SHA to create and query commit status (#22244)
Fix #13485. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		@@ -199,19 +199,19 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com
 | 
			
		||||
		return nil, fmt.Errorf("ReadFile(%s): %w", headFile, err)
 | 
			
		||||
	}
 | 
			
		||||
	commitID := string(commitIDBytes)
 | 
			
		||||
	if len(commitID) < 40 {
 | 
			
		||||
	if len(commitID) < git.SHAFullLength {
 | 
			
		||||
		return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID)
 | 
			
		||||
	}
 | 
			
		||||
	cmd := commitID[:40] + ".." + pr.BaseBranch
 | 
			
		||||
	cmd := commitID[:git.SHAFullLength] + ".." + pr.BaseBranch
 | 
			
		||||
 | 
			
		||||
	// Get the commit from BaseBranch where the pull request got merged
 | 
			
		||||
	mergeCommit, _, err := git.NewCommand(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse").AddDynamicArguments(cmd).
 | 
			
		||||
		RunStdString(&git.RunOpts{Dir: "", Env: []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %w", err)
 | 
			
		||||
	} else if len(mergeCommit) < 40 {
 | 
			
		||||
	} else if len(mergeCommit) < git.SHAFullLength {
 | 
			
		||||
		// PR was maybe fast-forwarded, so just use last commit of PR
 | 
			
		||||
		mergeCommit = commitID[:40]
 | 
			
		||||
		mergeCommit = commitID[:git.SHAFullLength]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
 | 
			
		||||
@@ -220,9 +220,9 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com
 | 
			
		||||
	}
 | 
			
		||||
	defer gitRepo.Close()
 | 
			
		||||
 | 
			
		||||
	commit, err := gitRepo.GetCommit(mergeCommit[:40])
 | 
			
		||||
	commit, err := gitRepo.GetCommit(mergeCommit[:git.SHAFullLength])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("GetMergeCommit[%v]: %w", mergeCommit[:40], err)
 | 
			
		||||
		return nil, fmt.Errorf("GetMergeCommit[%v]: %w", mergeCommit[:git.SHAFullLength], err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return commit, nil
 | 
			
		||||
 
 | 
			
		||||
@@ -839,7 +839,7 @@ func MergedManually(pr *issues_model.PullRequest, doer *user_model.User, baseGit
 | 
			
		||||
			return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: repo_model.MergeStyleManuallyMerged}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if len(commitID) < 40 {
 | 
			
		||||
		if len(commitID) < git.SHAFullLength {
 | 
			
		||||
			return fmt.Errorf("Wrong commit ID")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -166,7 +166,7 @@ func createTemporaryRepo(ctx context.Context, pr *issues_model.PullRequest) (str
 | 
			
		||||
	var headBranch string
 | 
			
		||||
	if pr.Flow == issues_model.PullRequestFlowGithub {
 | 
			
		||||
		headBranch = git.BranchPrefix + pr.HeadBranch
 | 
			
		||||
	} else if len(pr.HeadCommitID) == 40 { // for not created pull request
 | 
			
		||||
	} else if len(pr.HeadCommitID) == git.SHAFullLength { // for not created pull request
 | 
			
		||||
		headBranch = pr.HeadCommitID
 | 
			
		||||
	} else {
 | 
			
		||||
		headBranch = pr.GetGitRefName()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user