From 25ec20d5251511ebd0b9e6b963e189b860c39704 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 3 Nov 2015 17:25:39 -0500 Subject: [PATCH] #1838 update merge base before generate new patch --- README.md | 2 +- gogs.go | 2 +- models/git_diff.go | 4 +-- models/pull.go | 24 +++++++++++++++--- modules/git/repo_pull.go | 54 +++++++++++++++++++++++++++++----------- templates/.VERSION | 2 +- 6 files changed, 64 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 61f11c66ba..a80f79f09f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra ![](public/img/gogs-large-resize.png) -##### Current version: 0.6.22 Beta +##### Current version: 0.6.23 Beta diff --git a/gogs.go b/gogs.go index 343ee5aa4e..a1e32a5735 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.6.22.1103 Beta" +const APP_VER = "0.6.23.1103 Beta" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/git_diff.go b/models/git_diff.go index 3bf938ae40..c1568a5759 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -97,7 +97,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff var i int for scanner.Scan() { line := scanner.Text() - // fmt.Println(i, line) + if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") { continue } @@ -180,8 +180,6 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff b = b[1 : len(b)-1] } - fmt.Println(a, b) - curFile = &DiffFile{ Name: a, Index: len(diff.Files) + 1, diff --git a/models/pull.go b/models/pull.go index 0300c083d7..56af5f828d 100644 --- a/models/pull.go +++ b/models/pull.go @@ -385,16 +385,18 @@ func (pr *PullRequest) UpdateCols(cols ...string) error { var PullRequestQueue = NewUniqueQueue(setting.Repository.PullRequestQueueLength) // UpdatePatch generates and saves a new patch. -func (pr *PullRequest) UpdatePatch() error { - if err := pr.GetHeadRepo(); err != nil { +func (pr *PullRequest) UpdatePatch() (err error) { + if err = pr.GetHeadRepo(); err != nil { return fmt.Errorf("GetHeadRepo: %v", err) } else if pr.HeadRepo == nil { log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID) return nil } - if err := pr.GetBaseRepo(); err != nil { + if err = pr.GetBaseRepo(); err != nil { return fmt.Errorf("GetBaseRepo: %v", err) + } else if err = pr.BaseRepo.GetOwner(); err != nil { + return fmt.Errorf("GetOwner: %v", err) } headRepoPath, err := pr.HeadRepo.RepoPath() @@ -407,6 +409,22 @@ func (pr *PullRequest) UpdatePatch() error { return fmt.Errorf("OpenRepository: %v", err) } + // Add a temporary remote. + tmpRemote := com.ToStr(time.Now().UnixNano()) + if err = headGitRepo.AddRemote(tmpRemote, RepoPath(pr.BaseRepo.Owner.Name, pr.BaseRepo.Name)); err != nil { + return fmt.Errorf("AddRemote: %v", err) + } + defer func() { + headGitRepo.RemoveRemote(tmpRemote) + }() + remoteBranch := "remotes/" + tmpRemote + "/" + pr.BaseBranch + pr.MergeBase, err = headGitRepo.GetMergeBase(remoteBranch, pr.HeadBranch) + if err != nil { + return fmt.Errorf("GetMergeBase: %v", err) + } else if err = pr.Update(); err != nil { + return fmt.Errorf("Update: %v", err) + } + patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch) if err != nil { return fmt.Errorf("GetPatch: %v", err) diff --git a/modules/git/repo_pull.go b/modules/git/repo_pull.go index a9cc33a1d4..16b9536fe6 100644 --- a/modules/git/repo_pull.go +++ b/modules/git/repo_pull.go @@ -20,31 +20,55 @@ type PullRequestInfo struct { NumFiles int } +// GetMergeBase checks and returns merge base of two branches. +func (repo *Repository) GetMergeBase(remoteBranch, headBranch string) (string, error) { + // Get merge base commit. + stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "merge-base", remoteBranch, headBranch) + if err != nil { + return "", fmt.Errorf("get merge base: %v", concatenateError(err, stderr)) + } + return strings.TrimSpace(stdout), nil +} + +// AddRemote adds a remote to repository. +func (repo *Repository) AddRemote(name, path string) error { + _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", name, path) + if err != nil { + return fmt.Errorf("add remote(%s - %s): %v", name, path, concatenateError(err, stderr)) + } + return nil +} + +// RemoveRemote removes a remote from repository. +func (repo *Repository) RemoveRemote(name string) error { + _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "remove", name) + if err != nil { + return fmt.Errorf("remove remote(%s): %v", name, concatenateError(err, stderr)) + } + return nil +} + // GetPullRequestInfo generates and returns pull request information // between base and head branches of repositories. -func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (*PullRequestInfo, error) { +func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (_ *PullRequestInfo, err error) { // Add a temporary remote. tmpRemote := com.ToStr(time.Now().UnixNano()) - _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath) - if err != nil { - return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, stderr)) + if err = repo.AddRemote(tmpRemote, basePath); err != nil { + return nil, fmt.Errorf("AddRemote: %v", err) } defer func() { - com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote) + repo.RemoveRemote(tmpRemote) }() - prInfo := new(PullRequestInfo) - - var stdout string remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch - // Get merge base commit. - stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "merge-base", remoteBranch, headBranch) - if err != nil { - return nil, fmt.Errorf("get merge base: %v", concatenateError(err, stderr)) - } - prInfo.MergeBase = strings.TrimSpace(stdout) - stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat) + prInfo := new(PullRequestInfo) + prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch) + if err != nil { + return nil, fmt.Errorf("GetMergeBase: %v", err) + } + + stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat) if err != nil { return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr)) } diff --git a/templates/.VERSION b/templates/.VERSION index 364e36fc76..815df4fe64 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.6.22.1103 Beta \ No newline at end of file +0.6.23.1103 Beta \ No newline at end of file