mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-09 17:37:20 -06:00
`pull_request` workflow runs checked out the PR head, so a PR could pass CI and still break the base branch once merged. Gitea now maintains `refs/pull/<index>/merge`, a test merge of the head into the base branch, for open pull requests. It is updated on creation and mergeability checks and removed on close or merge. - `pull_request` runs read their workflows from the test merge and expose it as `github.ref`, `github.sha` and `github.event.pull_request.merge_commit_sha`. Commit statuses, the API's `head_sha`, `workflow_run` payloads and `paths` filters stay on the head. - Runs fall back to the head when the merge conflicts, the pull request is closed or git is older than 2.40. - Hidden refs keep each run's test merge (`refs/gitea/run/<id>`) and merged commits (`refs/gitea/pull/<index>/merged`) from `git gc`, for reruns and after the base branch is deleted or force-pushed. Fixes: https://github.com/go-gitea/gitea/issues/27039 Fixes: https://github.com/go-gitea/gitea/issues/13134 Fixes: https://github.com/go-gitea/gitea/issues/30972 Docs: https://gitea.com/gitea/docs/pulls/617 --------- Co-authored-by: silverwind <me@silverwind.io>
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.dev/modules/git/gitcmd"
|
|
"gitea.dev/modules/util"
|
|
)
|
|
|
|
const MaxConflictedDetectFiles = 10
|
|
|
|
// MergeTree performs a merge between two commits (baseRef and headRef) with an optional merge base.
|
|
// It returns the resulting tree hash, a list of conflicted files (if any), and an error if the operation fails.
|
|
// If there are no conflicts, the list of conflicted files will be nil.
|
|
func MergeTree(ctx context.Context, repo RepositoryFacade, baseRef, headRef, mergeBase string) (treeID string, isErrHasConflicts bool, conflictFiles []string, _ error) {
|
|
cmd := gitcmd.NewCommand("merge-tree", "--write-tree", "-z", "--name-only", "--no-messages")
|
|
if mergeBase != "" {
|
|
cmd.AddOptionFormat("--merge-base=%s", mergeBase)
|
|
}
|
|
cmd.AddDynamicArguments(baseRef, headRef)
|
|
|
|
stdout, stdoutClose := cmd.MakeStdoutPipe()
|
|
defer stdoutClose()
|
|
cmd.WithPipelineFunc(func(ctx gitcmd.Context) error {
|
|
// https://git-scm.com/docs/git-merge-tree/2.38.0#OUTPUT
|
|
// For a conflicted merge, the output is:
|
|
// <OID of toplevel tree>NUL
|
|
// <Conflicted file name 1>NUL
|
|
// <Conflicted file name 2>NUL
|
|
// ...
|
|
scanner := bufio.NewScanner(stdout)
|
|
scanner.Split(util.BufioScannerSplit(0))
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if treeID == "" { // first line is tree ID
|
|
treeID = line
|
|
continue
|
|
}
|
|
conflictFiles = append(conflictFiles, line)
|
|
if len(conflictFiles) >= MaxConflictedDetectFiles {
|
|
break
|
|
}
|
|
}
|
|
return scanner.Err()
|
|
})
|
|
|
|
err := cmd.WithRepo(repo).RunWithStderr(ctx)
|
|
// For a successful, non-conflicted merge, the exit status is 0. When the merge has conflicts, the exit status is 1.
|
|
// A merge can have conflicts without having individual files conflict
|
|
// https://git-scm.com/docs/git-merge-tree/2.38.0#_mistakes_to_avoid
|
|
isErrHasConflicts = gitcmd.IsErrorExitCode(err, 1)
|
|
if err == nil || isErrHasConflicts {
|
|
return treeID, isErrHasConflicts, conflictFiles, nil
|
|
}
|
|
return "", false, nil, fmt.Errorf("run merge-tree failed: %w", err)
|
|
}
|