diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-02-05 19:54:49 +0800 |
---|---|---|
committer | techknowlogick <matti@mdranta.net> | 2019-02-05 06:54:49 -0500 |
commit | bf4badad1d68c18d7ffb92c69e09e4e8aa252935 (patch) | |
tree | 9e8c3a41eba604738e632d921a2de97334e35e03 /models | |
parent | 680a57ce921f6d8c8752fa27aad6c95865fe8127 (diff) | |
download | gitea-bf4badad1d68c18d7ffb92c69e09e4e8aa252935.tar.gz gitea-bf4badad1d68c18d7ffb92c69e09e4e8aa252935.zip |
Pull request conflict files detection (#5951)
* add conflict detection
* test pull request conflict files
* fix detection files number
* fix comments
Diffstat (limited to 'models')
-rw-r--r-- | models/pull.go | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/models/pull.go b/models/pull.go index d84be139c9..35fbc41788 100644 --- a/models/pull.go +++ b/models/pull.go @@ -54,9 +54,10 @@ const ( // PullRequest represents relation between pull request and repositories. type PullRequest struct { - ID int64 `xorm:"pk autoincr"` - Type PullRequestType - Status PullRequestStatus + ID int64 `xorm:"pk autoincr"` + Type PullRequestType + Status PullRequestStatus + ConflictedFiles []string `xorm:"TEXT JSON"` IssueID int64 `xorm:"INDEX"` Issue *Issue `xorm:"-"` @@ -843,6 +844,7 @@ func (pr *PullRequest) testPatch(e Engine) (err error) { args = append(args, "--ignore-whitespace") } args = append(args, patchPath) + pr.ConflictedFiles = []string{} _, stderr, err = process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}, @@ -851,8 +853,26 @@ func (pr *PullRequest) testPatch(e Engine) (err error) { for i := range patchConflicts { if strings.Contains(stderr, patchConflicts[i]) { log.Trace("PullRequest[%d].testPatch (apply): has conflict", pr.ID) - fmt.Println(stderr) + const prefix = "error: patch failed:" pr.Status = PullRequestStatusConflict + pr.ConflictedFiles = make([]string, 0, 5) + scanner := bufio.NewScanner(strings.NewReader(stderr)) + for scanner.Scan() { + line := scanner.Text() + + if strings.HasPrefix(line, prefix) { + pr.ConflictedFiles = append(pr.ConflictedFiles, strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0])) + } + // only list 10 conflicted files + if len(pr.ConflictedFiles) >= 10 { + break + } + } + + if len(pr.ConflictedFiles) > 0 { + log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles) + } + return nil } } @@ -1375,7 +1395,7 @@ func (pr *PullRequest) checkAndUpdateStatus() { // Make sure there is no waiting test to process before leaving the checking status. if !pullRequestQueue.Exist(pr.ID) { - if err := pr.UpdateCols("status"); err != nil { + if err := pr.UpdateCols("status, conflicted_files"); err != nil { log.Error(4, "Update[%d]: %v", pr.ID, err) } } @@ -1396,6 +1416,11 @@ func (pr *PullRequest) IsWorkInProgress() bool { return false } +// IsFilesConflicted determines if the Pull Request has changes conflicting with the target branch. +func (pr *PullRequest) IsFilesConflicted() bool { + return len(pr.ConflictedFiles) > 0 +} + // GetWorkInProgressPrefix returns the prefix used to mark the pull request as a work in progress. // It returns an empty string when none were found func (pr *PullRequest) GetWorkInProgressPrefix() string { |