mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
If the stderr output is not handled, then it needs to use RunWithStderr to make sure the git error message is included in the returned error. Otherwise, when the git command fails, the error log won't show useful information. Ref: * #39795 : no useful information in log messages * https://github.com/go-gitea/gitea/pull/40160#pullrequestreview-5752720866
148 lines
5.2 KiB
Go
148 lines
5.2 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pull
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
issues_model "gitea.dev/models/issues"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/log"
|
|
)
|
|
|
|
// checkConflictsMergeTree uses git merge-tree to check for conflicts and if none are found checks if the patch is empty
|
|
// return true if there are conflicts otherwise return false
|
|
// pr.Status and pr.ConflictedFiles will be updated as necessary
|
|
func checkConflictsMergeTree(ctx context.Context, pr *issues_model.PullRequest, baseCommitID string) (bool, error) {
|
|
treeHash, conflict, conflictFiles, err := git.MergeTree(ctx, pr.BaseRepo, baseCommitID, pr.HeadCommitID, pr.MergeBase)
|
|
if err != nil {
|
|
return false, fmt.Errorf("MergeTree: %w", err)
|
|
}
|
|
if pr.Index > 0 && !conflict {
|
|
if _, err := updateMergeRef(ctx, pr, baseCommitID, pr.HeadCommitID, treeHash); err != nil {
|
|
log.Error("updateMergeRef for %-v: %v", pr, err)
|
|
}
|
|
}
|
|
if conflict {
|
|
pr.Status = issues_model.PullRequestStatusConflict
|
|
// sometimes git merge-tree will detect conflicts but not list any conflicted files
|
|
// so that pr.ConflictedFiles will be empty
|
|
pr.ConflictedFiles = conflictFiles
|
|
|
|
log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles)
|
|
return true, nil
|
|
}
|
|
|
|
// Detect whether the pull request introduces changes by comparing the merged tree (treeHash)
|
|
// against the current base commit (baseCommitID) using `git diff-tree`. The command returns exit code 0
|
|
// if there is no diff between these trees (empty patch) and exit code 1 if there is a diff.
|
|
gitErr := gitcmd.NewCommand("diff-tree", "-r", "--quiet").
|
|
AddDynamicArguments(treeHash, baseCommitID).WithRepo(pr.BaseRepo).RunWithStderr(ctx)
|
|
switch {
|
|
case gitErr == nil:
|
|
log.Debug("PullRequest[%d]: Patch is empty - ignoring", pr.ID)
|
|
pr.Status = issues_model.PullRequestStatusEmpty
|
|
case gitcmd.IsErrorExitCode(gitErr, 1):
|
|
pr.Status = issues_model.PullRequestStatusMergeable
|
|
default:
|
|
return false, fmt.Errorf("run diff-tree exit abnormally: %w", gitErr)
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func checkPullRequestMergeableByMergeTree(ctx context.Context, pr *issues_model.PullRequest) error {
|
|
// 1. Get head commit
|
|
if err := pr.LoadHeadRepo(ctx); err != nil {
|
|
return err
|
|
}
|
|
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo)
|
|
if err != nil {
|
|
return fmt.Errorf("OpenRepository: %w", err)
|
|
}
|
|
defer headGitRepo.Close()
|
|
|
|
// 2. Get/open base repository
|
|
var baseGitRepo *git.Repository
|
|
if pr.IsSameRepo() {
|
|
baseGitRepo = headGitRepo
|
|
} else {
|
|
baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo)
|
|
if err != nil {
|
|
return fmt.Errorf("OpenRepository: %w", err)
|
|
}
|
|
defer baseGitRepo.Close()
|
|
}
|
|
|
|
// 3. Get head commit id
|
|
if pr.Flow == issues_model.PullRequestFlowGithub {
|
|
pr.HeadCommitID, err = headGitRepo.GetRefCommitID(ctx, git.BranchPrefix+pr.HeadBranch)
|
|
if err != nil {
|
|
return fmt.Errorf("GetBranchCommitID: can't find commit ID for head: %w", err)
|
|
}
|
|
} else {
|
|
if pr.ID > 0 {
|
|
pr.HeadCommitID, err = baseGitRepo.GetRefCommitID(ctx, pr.GetGitHeadRefName())
|
|
if err != nil {
|
|
return fmt.Errorf("GetRefCommitID: can't find commit ID for head: %w", err)
|
|
}
|
|
} else if pr.HeadCommitID == "" { // for new pull request with agit, the head commit id must be provided
|
|
return errors.New("head commit ID is empty for pull request Agit flow")
|
|
}
|
|
}
|
|
|
|
// 4. fetch head commit id into the current repository
|
|
if !pr.IsSameRepo() {
|
|
if !baseGitRepo.IsReferenceExist(ctx, pr.HeadCommitID) {
|
|
if err := git.FetchRemoteTempCommit(ctx, pr.BaseRepo, pr.HeadRepo, pr.HeadCommitID); err != nil {
|
|
return fmt.Errorf("FetchRemoteTempCommit: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 5. update merge base
|
|
baseCommitID, err := baseGitRepo.GetRefCommitID(ctx, git.BranchPrefix+pr.BaseBranch)
|
|
if err != nil {
|
|
return fmt.Errorf("GetBranchCommitID: can't find commit ID for base: %w", err)
|
|
}
|
|
|
|
pr.MergeBase, err = git.MergeBase(ctx, pr.BaseRepo, baseCommitID, pr.HeadCommitID)
|
|
if err != nil {
|
|
// if there is no merge base, then it's empty, still need to allow the pull request to be created
|
|
// not quite right (e.g.: why not reset the fields like below), but no interest to do more investigation at the moment
|
|
log.Error("MergeBase: unable to find merge base between %s and %s: %v", baseCommitID, pr.HeadCommitID, err)
|
|
pr.Status = issues_model.PullRequestStatusEmpty
|
|
return nil
|
|
}
|
|
|
|
// reset conflicted files and changed protected files
|
|
pr.ConflictedFiles = nil
|
|
pr.ChangedProtectedFiles = nil
|
|
|
|
// 6. if base == head, then it's an ancestor
|
|
if pr.HeadCommitID == pr.MergeBase {
|
|
pr.Status = issues_model.PullRequestStatusAncestor
|
|
return nil
|
|
}
|
|
|
|
// 7. Check for conflicts
|
|
conflicted, err := checkConflictsMergeTree(ctx, pr, baseCommitID)
|
|
if err != nil {
|
|
log.Error("checkConflictsMergeTree: %v", err)
|
|
pr.Status = issues_model.PullRequestStatusError
|
|
return fmt.Errorf("checkConflictsMergeTree: %w", err)
|
|
}
|
|
if conflicted || pr.Status == issues_model.PullRequestStatusEmpty {
|
|
return nil
|
|
}
|
|
|
|
// 8. Check for protected files changes
|
|
if err = checkPullFilesProtection(ctx, pr, baseGitRepo, pr.HeadCommitID); err != nil {
|
|
return fmt.Errorf("checkPullFilesProtection: %w", err)
|
|
}
|
|
return nil
|
|
}
|