Skip to content

Commit

Permalink
Speed up conflict checking in pull request creation
Browse files Browse the repository at this point in the history
In order to check conflicts of a PR, we set up a working tree by
cloning the base branch, which is quite time-consuming when the repository is huge.
Instead, this PR uses `git read-tree` and `git apply --check --cached` to check conflicts.

For go-gitea#258
  • Loading branch information
typeless committed Dec 29, 2016
1 parent 799d0c2 commit c39e0b3
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -428,17 +430,22 @@ func (pr *PullRequest) testPatch() (err error) {

log.Trace("PullRequest[%d].testPatch (patchPath): %s", pr.ID, patchPath)

// Delete old temp local copy before we create a new temp local copy
RemoveAllWithNotice("Deleting old local copy", pr.BaseRepo.LocalCopyPath())
pr.Status = PullRequestStatusChecking

indexTmpPath := filepath.Join(os.TempDir(), "-gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
defer os.Remove(indexTmpPath)

if err := pr.BaseRepo.UpdateLocalCopyBranch(pr.BaseBranch); err != nil {
return fmt.Errorf("UpdateLocalCopy: %v", err)
var stderr string
_, stderr, err = process.ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git read-tree): %d", pr.BaseRepo.ID),
[]string{"GIT_DIR=" + pr.BaseRepo.RepoPath()},
"git", "read-tree", "--index-output", indexTmpPath, pr.BaseBranch)
if err != nil {
return fmt.Errorf("git read-tree --index-output=%s %s: %v - %s", indexTmpPath, pr.BaseBranch, err, stderr)
}

pr.Status = PullRequestStatusChecking
_, stderr, err := process.ExecDir(-1, pr.BaseRepo.LocalCopyPath(),
fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID),
"git", "apply", "--check", patchPath)
_, stderr, err = process.ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID),
[]string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
"git", "apply", "--check", "--cached", patchPath)
if err != nil {
for i := range patchConflicts {
if strings.Contains(stderr, patchConflicts[i]) {
Expand Down

0 comments on commit c39e0b3

Please sign in to comment.