2019-12-13 17:21:06 -05:00
|
|
|
// Copyright 2019 The Gitea Authors.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-12-18 23:19:25 -05:00
|
|
|
"path/filepath"
|
2019-12-13 17:21:06 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-09 14:57:58 -05:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2019-12-13 17:21:06 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-12-18 23:19:25 -05:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-12-13 17:21:06 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-12-18 23:19:25 -05:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2020-08-11 15:05:34 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-10-13 13:50:57 -05:00
|
|
|
|
|
|
|
"github.com/gobwas/glob"
|
2019-12-13 17:21:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadDiffOrPatch will write the patch for the pr to the writer
|
2022-01-19 18:26:57 -05:00
|
|
|
func DownloadDiffOrPatch(ctx context.Context, pr *models.PullRequest, w io.Writer, patch, binary bool) error {
|
2020-04-03 08:21:41 -05:00
|
|
|
if err := pr.LoadBaseRepo(); err != nil {
|
|
|
|
log.Error("Unable to load base repository ID %d for pr #%d [%d]", pr.BaseRepoID, pr.Index, pr.ID)
|
2019-12-13 17:21:06 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:26:57 -05:00
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath())
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenRepository: %v", err)
|
|
|
|
}
|
2022-01-19 18:26:57 -05:00
|
|
|
defer closer.Close()
|
|
|
|
|
2021-09-27 16:09:49 -05:00
|
|
|
if err := gitRepo.GetDiffOrPatch(pr.MergeBase, pr.GetGitRefName(), w, patch, binary); err != nil {
|
2020-01-12 04:36:21 -05:00
|
|
|
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
|
|
|
return fmt.Errorf("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var patchErrorSuffices = []string{
|
|
|
|
": already exists in index",
|
|
|
|
": patch does not apply",
|
|
|
|
": already exists in working directory",
|
|
|
|
"unrecognized input",
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPatch will test whether a simple patch will apply
|
|
|
|
func TestPatch(pr *models.PullRequest) error {
|
2022-01-19 18:26:57 -05:00
|
|
|
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("TestPatch: Repo[%d]#%d", pr.BaseRepoID, pr.Index))
|
|
|
|
defer finished()
|
|
|
|
|
2019-12-13 17:21:06 -05:00
|
|
|
// Clone base repo.
|
2022-01-19 18:26:57 -05:00
|
|
|
tmpBasePath, err := createTemporaryRepo(ctx, pr)
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("CreateTemporaryPath: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
|
|
|
|
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-19 18:26:57 -05:00
|
|
|
gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath)
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenRepository: %v", err)
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 1. update merge base
|
2022-02-06 14:01:47 -05:00
|
|
|
pr.MergeBase, err = git.NewCommand(ctx, "merge-base", "--", "base", "tracking").RunInDir(tmpBasePath)
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
|
|
|
var err2 error
|
|
|
|
pr.MergeBase, err2 = gitRepo.GetRefCommitID(git.BranchPrefix + "base")
|
|
|
|
if err2 != nil {
|
|
|
|
return fmt.Errorf("GetMergeBase: %v and can't find commit ID for base: %v", err, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pr.MergeBase = strings.TrimSpace(pr.MergeBase)
|
2020-10-13 13:50:57 -05:00
|
|
|
|
|
|
|
// 2. Check for conflicts
|
2021-03-03 22:41:23 -05:00
|
|
|
if conflicts, err := checkConflicts(pr, gitRepo, tmpBasePath); err != nil || conflicts || pr.Status == models.PullRequestStatusEmpty {
|
2020-10-13 13:50:57 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Check for protected files changes
|
|
|
|
if err = checkPullFilesProtection(pr, gitRepo); err != nil {
|
|
|
|
return fmt.Errorf("pr.CheckPullFilesProtection(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pr.ChangedProtectedFiles) > 0 {
|
|
|
|
log.Trace("Found %d protected files changed", len(pr.ChangedProtectedFiles))
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.Status = models.PullRequestStatusMergeable
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-18 23:19:25 -05:00
|
|
|
type errMergeConflict struct {
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errMergeConflict) Error() string {
|
|
|
|
return fmt.Sprintf("conflict detected at: %s", e.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, gitRepo *git.Repository) error {
|
|
|
|
switch {
|
|
|
|
case file.stage1 != nil && (file.stage2 == nil || file.stage3 == nil):
|
|
|
|
// 1. Deleted in one or both:
|
|
|
|
//
|
|
|
|
// Conflict <==> the stage1 !SameAs to the undeleted one
|
|
|
|
if (file.stage2 != nil && !file.stage1.SameAs(file.stage2)) || (file.stage3 != nil && !file.stage1.SameAs(file.stage3)) {
|
|
|
|
// Conflict!
|
|
|
|
return &errMergeConflict{file.stage1.path}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a genuine conflict and we can simply remove the file from the index
|
|
|
|
return gitRepo.RemoveFilesFromIndex(file.stage1.path)
|
|
|
|
case file.stage1 == nil && file.stage2 != nil && (file.stage3 == nil || file.stage2.SameAs(file.stage3)):
|
|
|
|
// 2. Added in ours but not in theirs or identical in both
|
|
|
|
//
|
|
|
|
// Not a genuine conflict just add to the index
|
|
|
|
if err := gitRepo.AddObjectToIndex(file.stage2.mode, git.MustIDFromString(file.stage2.sha), file.stage2.path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case file.stage1 == nil && file.stage2 != nil && file.stage3 != nil && file.stage2.sha == file.stage3.sha && file.stage2.mode != file.stage3.mode:
|
|
|
|
// 3. Added in both with the same sha but the modes are different
|
|
|
|
//
|
|
|
|
// Conflict! (Not sure that this can actually happen but we should handle)
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
case file.stage1 == nil && file.stage2 == nil && file.stage3 != nil:
|
|
|
|
// 4. Added in theirs but not ours:
|
|
|
|
//
|
|
|
|
// Not a genuine conflict just add to the index
|
|
|
|
return gitRepo.AddObjectToIndex(file.stage3.mode, git.MustIDFromString(file.stage3.sha), file.stage3.path)
|
|
|
|
case file.stage1 == nil:
|
|
|
|
// 5. Created by new in both
|
|
|
|
//
|
|
|
|
// Conflict!
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
case file.stage2 != nil && file.stage3 != nil:
|
|
|
|
// 5. Modified in both - we should try to merge in the changes but first:
|
|
|
|
//
|
|
|
|
if file.stage2.mode == "120000" || file.stage3.mode == "120000" {
|
|
|
|
// 5a. Conflicting symbolic link change
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
}
|
|
|
|
if file.stage2.mode == "160000" || file.stage3.mode == "160000" {
|
|
|
|
// 5b. Conflicting submodule change
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
}
|
|
|
|
if file.stage2.mode != file.stage3.mode {
|
|
|
|
// 5c. Conflicting mode change
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to get the objects from the object db to attempt to merge
|
2022-02-06 14:01:47 -05:00
|
|
|
root, err := git.NewCommand(ctx, "unpack-file", file.stage1.sha).RunInDir(tmpBasePath)
|
2021-12-18 23:19:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get root object: %s at path: %s for merging. Error: %w", file.stage1.sha, file.stage1.path, err)
|
|
|
|
}
|
|
|
|
root = strings.TrimSpace(root)
|
|
|
|
defer func() {
|
|
|
|
_ = util.Remove(filepath.Join(tmpBasePath, root))
|
|
|
|
}()
|
|
|
|
|
2022-02-06 14:01:47 -05:00
|
|
|
base, err := git.NewCommand(ctx, "unpack-file", file.stage2.sha).RunInDir(tmpBasePath)
|
2021-12-18 23:19:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get base object: %s at path: %s for merging. Error: %w", file.stage2.sha, file.stage2.path, err)
|
|
|
|
}
|
|
|
|
base = strings.TrimSpace(filepath.Join(tmpBasePath, base))
|
|
|
|
defer func() {
|
|
|
|
_ = util.Remove(base)
|
|
|
|
}()
|
2022-02-06 14:01:47 -05:00
|
|
|
head, err := git.NewCommand(ctx, "unpack-file", file.stage3.sha).RunInDir(tmpBasePath)
|
2021-12-18 23:19:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get head object:%s at path: %s for merging. Error: %w", file.stage3.sha, file.stage3.path, err)
|
|
|
|
}
|
|
|
|
head = strings.TrimSpace(head)
|
|
|
|
defer func() {
|
|
|
|
_ = util.Remove(filepath.Join(tmpBasePath, head))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// now git merge-file annoyingly takes a different order to the merge-tree ...
|
2022-02-06 14:01:47 -05:00
|
|
|
_, conflictErr := git.NewCommand(ctx, "merge-file", base, root, head).RunInDir(tmpBasePath)
|
2021-12-18 23:19:25 -05:00
|
|
|
if conflictErr != nil {
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
}
|
|
|
|
|
|
|
|
// base now contains the merged data
|
2022-02-06 14:01:47 -05:00
|
|
|
hash, err := git.NewCommand(ctx, "hash-object", "-w", "--path", file.stage2.path, base).RunInDir(tmpBasePath)
|
2021-12-18 23:19:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hash = strings.TrimSpace(hash)
|
|
|
|
return gitRepo.AddObjectToIndex(file.stage2.mode, git.MustIDFromString(hash), file.stage2.path)
|
|
|
|
default:
|
|
|
|
if file.stage1 != nil {
|
|
|
|
return &errMergeConflict{file.stage1.path}
|
|
|
|
} else if file.stage2 != nil {
|
|
|
|
return &errMergeConflict{file.stage2.path}
|
|
|
|
} else if file.stage3 != nil {
|
|
|
|
return &errMergeConflict{file.stage3.path}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath string) (bool, error) {
|
2021-12-18 23:19:25 -05:00
|
|
|
ctx, cancel, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("checkConflicts: pr[%d] %s/%s#%d", pr.ID, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Index))
|
|
|
|
defer finished()
|
|
|
|
|
|
|
|
// First we use read-tree to do a simple three-way merge
|
2022-02-06 14:01:47 -05:00
|
|
|
if _, err := git.NewCommand(ctx, "read-tree", "-m", pr.MergeBase, "base", "tracking").RunInDir(tmpBasePath); err != nil {
|
2021-12-18 23:19:25 -05:00
|
|
|
log.Error("Unable to run read-tree -m! Error: %v", err)
|
|
|
|
return false, fmt.Errorf("unable to run read-tree -m! Error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we use git ls-files -u to list the unmerged files and collate the triples in unmergedfiles
|
|
|
|
unmerged := make(chan *unmergedFile)
|
|
|
|
go unmergedFiles(ctx, tmpBasePath, unmerged)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
for range unmerged {
|
|
|
|
// empty the unmerged channel
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
numberOfConflicts := 0
|
2021-12-31 07:33:55 -05:00
|
|
|
pr.ConflictedFiles = make([]string, 0, 5)
|
2021-12-18 23:19:25 -05:00
|
|
|
conflict := false
|
|
|
|
|
|
|
|
for file := range unmerged {
|
|
|
|
if file == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if file.err != nil {
|
|
|
|
cancel()
|
|
|
|
return false, file.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK now we have the unmerged file triplet attempt to merge it
|
|
|
|
if err := attemptMerge(ctx, file, tmpBasePath, gitRepo); err != nil {
|
|
|
|
if conflictErr, ok := err.(*errMergeConflict); ok {
|
|
|
|
log.Trace("Conflict: %s in PR[%d] %s/%s#%d", conflictErr.filename, pr.ID, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Index)
|
|
|
|
conflict = true
|
|
|
|
if numberOfConflicts < 10 {
|
|
|
|
pr.ConflictedFiles = append(pr.ConflictedFiles, conflictErr.filename)
|
|
|
|
}
|
|
|
|
numberOfConflicts++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !conflict {
|
2022-02-06 14:01:47 -05:00
|
|
|
treeHash, err := git.NewCommand(ctx, "write-tree").RunInDir(tmpBasePath)
|
2021-12-18 23:19:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
treeHash = strings.TrimSpace(treeHash)
|
|
|
|
baseTree, err := gitRepo.GetTree("base")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if treeHash == baseTree.ID.String() {
|
|
|
|
log.Debug("PullRequest[%d]: Patch is empty - ignoring", pr.ID)
|
|
|
|
pr.Status = models.PullRequestStatusEmpty
|
|
|
|
pr.ConflictedFiles = []string{}
|
|
|
|
pr.ChangedProtectedFiles = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK read-tree has failed so we need to try a different thing - this might actually succeed where the above fails due to whitespace handling.
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 1. Create a plain patch from head to base
|
2021-09-22 00:38:34 -05:00
|
|
|
tmpPatchFile, err := os.CreateTemp("", "patch")
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to create temporary patch file! Error: %v", err)
|
2021-12-18 23:19:25 -05:00
|
|
|
return false, fmt.Errorf("unable to create temporary patch file! Error: %v", err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
defer func() {
|
2020-08-11 15:05:34 -05:00
|
|
|
_ = util.Remove(tmpPatchFile.Name())
|
2019-12-13 17:21:06 -05:00
|
|
|
}()
|
|
|
|
|
2021-09-27 16:09:49 -05:00
|
|
|
if err := gitRepo.GetDiffBinary(pr.MergeBase, "tracking", tmpPatchFile); err != nil {
|
2019-12-13 17:21:06 -05:00
|
|
|
tmpPatchFile.Close()
|
2020-01-12 04:36:21 -05:00
|
|
|
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
2021-12-18 23:19:25 -05:00
|
|
|
return false, fmt.Errorf("unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
stat, err := tmpPatchFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
tmpPatchFile.Close()
|
2021-12-18 23:19:25 -05:00
|
|
|
return false, fmt.Errorf("unable to stat patch file: %v", err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
patchPath := tmpPatchFile.Name()
|
|
|
|
tmpPatchFile.Close()
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 1a. if the size of that patch is 0 - there can be no conflicts!
|
2019-12-13 17:21:06 -05:00
|
|
|
if stat.Size() == 0 {
|
|
|
|
log.Debug("PullRequest[%d]: Patch is empty - ignoring", pr.ID)
|
2021-03-03 22:41:23 -05:00
|
|
|
pr.Status = models.PullRequestStatusEmpty
|
2019-12-13 17:21:06 -05:00
|
|
|
pr.ConflictedFiles = []string{}
|
2021-03-03 22:41:23 -05:00
|
|
|
pr.ChangedProtectedFiles = []string{}
|
2020-10-13 13:50:57 -05:00
|
|
|
return false, nil
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath)
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 2. preset the pr.Status as checking (this is not save at present)
|
2019-12-13 17:21:06 -05:00
|
|
|
pr.Status = models.PullRequestStatusChecking
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 3. Read the base branch in to the index of the temporary repository
|
2022-02-06 14:01:47 -05:00
|
|
|
_, err = git.NewCommand(gitRepo.Ctx, "read-tree", "base").RunInDir(tmpBasePath)
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
2020-10-13 13:50:57 -05:00
|
|
|
return false, fmt.Errorf("git read-tree %s: %v", pr.BaseBranch, err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 4. Now get the pull request configuration to check if we need to ignore whitespace
|
2021-11-09 14:57:58 -05:00
|
|
|
prUnit, err := pr.BaseRepo.GetUnit(unit.TypePullRequests)
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
2020-10-13 13:50:57 -05:00
|
|
|
return false, err
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
prConfig := prUnit.PullRequestsConfig()
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 5. Prepare the arguments to apply the patch against the index
|
2019-12-13 17:21:06 -05:00
|
|
|
args := []string{"apply", "--check", "--cached"}
|
|
|
|
if prConfig.IgnoreWhitespaceConflicts {
|
|
|
|
args = append(args, "--ignore-whitespace")
|
|
|
|
}
|
2022-02-01 23:46:10 -05:00
|
|
|
is3way := false
|
2021-12-18 23:19:25 -05:00
|
|
|
if git.CheckGitVersionAtLeast("2.32.0") == nil {
|
|
|
|
args = append(args, "--3way")
|
2022-02-01 23:46:10 -05:00
|
|
|
is3way = true
|
2021-12-18 23:19:25 -05:00
|
|
|
}
|
2019-12-13 17:21:06 -05:00
|
|
|
args = append(args, patchPath)
|
|
|
|
pr.ConflictedFiles = make([]string, 0, 5)
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 6. Prep the pipe:
|
|
|
|
// - Here we could do the equivalent of:
|
|
|
|
// `git apply --check --cached patch_file > conflicts`
|
|
|
|
// Then iterate through the conflicts. However, that means storing all the conflicts
|
|
|
|
// in memory - which is very wasteful.
|
|
|
|
// - alternatively we can do the equivalent of:
|
|
|
|
// `git apply --check ... | grep ...`
|
|
|
|
// meaning we don't store all of the conflicts unnecessarily.
|
2019-12-13 17:21:06 -05:00
|
|
|
stderrReader, stderrWriter, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to open stderr pipe: %v", err)
|
2021-12-18 23:19:25 -05:00
|
|
|
return false, fmt.Errorf("unable to open stderr pipe: %v", err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = stderrReader.Close()
|
|
|
|
_ = stderrWriter.Close()
|
|
|
|
}()
|
2020-10-13 13:50:57 -05:00
|
|
|
|
|
|
|
// 7. Run the check command
|
2021-12-18 23:19:25 -05:00
|
|
|
conflict = false
|
2022-02-06 14:01:47 -05:00
|
|
|
err = git.NewCommand(gitRepo.Ctx, args...).
|
2019-12-13 17:21:06 -05:00
|
|
|
RunInDirTimeoutEnvFullPipelineFunc(
|
|
|
|
nil, -1, tmpBasePath,
|
|
|
|
nil, stderrWriter, nil,
|
2020-01-15 03:32:57 -05:00
|
|
|
func(ctx context.Context, cancel context.CancelFunc) error {
|
2020-10-13 13:50:57 -05:00
|
|
|
// Close the writer end of the pipe to begin processing
|
2019-12-13 17:21:06 -05:00
|
|
|
_ = stderrWriter.Close()
|
2020-10-13 13:50:57 -05:00
|
|
|
defer func() {
|
|
|
|
// Close the reader on return to terminate the git command if necessary
|
|
|
|
_ = stderrReader.Close()
|
|
|
|
}()
|
|
|
|
|
2019-12-13 17:21:06 -05:00
|
|
|
const prefix = "error: patch failed:"
|
|
|
|
const errorPrefix = "error: "
|
2022-02-01 23:46:10 -05:00
|
|
|
const threewayFailed = "Failed to perform three-way merge..."
|
|
|
|
const appliedPatchPrefix = "Applied patch to '"
|
|
|
|
const withConflicts = "' with conflicts."
|
2020-10-13 13:50:57 -05:00
|
|
|
|
2019-12-13 17:21:06 -05:00
|
|
|
conflictMap := map[string]bool{}
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// Now scan the output from the command
|
2019-12-13 17:21:06 -05:00
|
|
|
scanner := bufio.NewScanner(stderrReader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, prefix) {
|
|
|
|
conflict = true
|
|
|
|
filepath := strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0])
|
|
|
|
conflictMap[filepath] = true
|
2022-02-01 23:46:10 -05:00
|
|
|
} else if is3way && line == threewayFailed {
|
|
|
|
conflict = true
|
2019-12-13 17:21:06 -05:00
|
|
|
} else if strings.HasPrefix(line, errorPrefix) {
|
|
|
|
conflict = true
|
|
|
|
for _, suffix := range patchErrorSuffices {
|
|
|
|
if strings.HasSuffix(line, suffix) {
|
|
|
|
filepath := strings.TrimSpace(strings.TrimSuffix(line[len(errorPrefix):], suffix))
|
|
|
|
if filepath != "" {
|
|
|
|
conflictMap[filepath] = true
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 23:46:10 -05:00
|
|
|
} else if is3way && strings.HasPrefix(line, appliedPatchPrefix) && strings.HasSuffix(line, withConflicts) {
|
|
|
|
conflict = true
|
|
|
|
filepath := strings.TrimPrefix(strings.TrimSuffix(line, withConflicts), appliedPatchPrefix)
|
|
|
|
if filepath != "" {
|
|
|
|
conflictMap[filepath] = true
|
|
|
|
}
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
|
|
|
// only list 10 conflicted files
|
|
|
|
if len(conflictMap) >= 10 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 13:50:57 -05:00
|
|
|
|
2019-12-13 17:21:06 -05:00
|
|
|
if len(conflictMap) > 0 {
|
|
|
|
pr.ConflictedFiles = make([]string, 0, len(conflictMap))
|
|
|
|
for key := range conflictMap {
|
|
|
|
pr.ConflictedFiles = append(pr.ConflictedFiles, key)
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 13:50:57 -05:00
|
|
|
|
2020-01-15 03:32:57 -05:00
|
|
|
return nil
|
2019-12-13 17:21:06 -05:00
|
|
|
})
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// 8. If there is a conflict the `git apply` command will return a non-zero error code - so there will be a positive error.
|
2019-12-13 17:21:06 -05:00
|
|
|
if err != nil {
|
|
|
|
if conflict {
|
|
|
|
pr.Status = models.PullRequestStatusConflict
|
|
|
|
log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles)
|
2020-10-13 13:50:57 -05:00
|
|
|
|
|
|
|
return true, nil
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
2020-10-13 13:50:57 -05:00
|
|
|
return false, fmt.Errorf("git apply --check: %v", err)
|
2019-12-13 17:21:06 -05:00
|
|
|
}
|
2020-10-13 13:50:57 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckFileProtection check file Protection
|
2022-01-19 18:26:57 -05:00
|
|
|
func CheckFileProtection(repo *git.Repository, oldCommitID, newCommitID string, patterns []glob.Glob, limit int, env []string) ([]string, error) {
|
2020-10-13 13:50:57 -05:00
|
|
|
if len(patterns) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-01-19 18:26:57 -05:00
|
|
|
affectedFiles, err := git.GetAffectedFiles(repo, oldCommitID, newCommitID, env)
|
2020-10-13 13:50:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changedProtectedFiles := make([]string, 0, limit)
|
2021-09-11 09:21:17 -05:00
|
|
|
for _, affectedFile := range affectedFiles {
|
|
|
|
lpath := strings.ToLower(affectedFile)
|
|
|
|
for _, pat := range patterns {
|
|
|
|
if pat.Match(lpath) {
|
|
|
|
changedProtectedFiles = append(changedProtectedFiles, lpath)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(changedProtectedFiles) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(changedProtectedFiles) > 0 {
|
|
|
|
err = models.ErrFilePathProtected{
|
|
|
|
Path: changedProtectedFiles[0],
|
|
|
|
}
|
2020-10-13 13:50:57 -05:00
|
|
|
}
|
|
|
|
return changedProtectedFiles, err
|
|
|
|
}
|
|
|
|
|
2021-09-11 09:21:17 -05:00
|
|
|
// CheckUnprotectedFiles check if the commit only touches unprotected files
|
2022-01-19 18:26:57 -05:00
|
|
|
func CheckUnprotectedFiles(repo *git.Repository, oldCommitID, newCommitID string, patterns []glob.Glob, env []string) (bool, error) {
|
2021-09-11 09:21:17 -05:00
|
|
|
if len(patterns) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
2022-01-19 18:26:57 -05:00
|
|
|
affectedFiles, err := git.GetAffectedFiles(repo, oldCommitID, newCommitID, env)
|
2021-09-11 09:21:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, affectedFile := range affectedFiles {
|
|
|
|
lpath := strings.ToLower(affectedFile)
|
|
|
|
unprotected := false
|
|
|
|
for _, pat := range patterns {
|
|
|
|
if pat.Match(lpath) {
|
|
|
|
unprotected = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !unprotected {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:50:57 -05:00
|
|
|
// checkPullFilesProtection check if pr changed protected files and save results
|
|
|
|
func checkPullFilesProtection(pr *models.PullRequest, gitRepo *git.Repository) error {
|
|
|
|
if err := pr.LoadProtectedBranch(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pr.ProtectedBranch == nil {
|
|
|
|
pr.ChangedProtectedFiles = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-01-19 18:26:57 -05:00
|
|
|
pr.ChangedProtectedFiles, err = CheckFileProtection(gitRepo, pr.MergeBase, "tracking", pr.ProtectedBranch.GetProtectedFilePatterns(), 10, os.Environ())
|
2020-10-13 13:50:57 -05:00
|
|
|
if err != nil && !models.IsErrFilePathProtected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 17:21:06 -05:00
|
|
|
return nil
|
|
|
|
}
|