diff options
author | Unknwon <u@gogs.io> | 2015-11-03 17:25:39 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-11-03 17:25:39 -0500 |
commit | 25ec20d5251511ebd0b9e6b963e189b860c39704 (patch) | |
tree | 7c771f0a8403fd62811fdadcbe8d118d89edec20 /modules/git | |
parent | 8e262f3ec4c961487fe56b6b64573a2f9b928c70 (diff) | |
download | gitea-25ec20d5251511ebd0b9e6b963e189b860c39704.tar.gz gitea-25ec20d5251511ebd0b9e6b963e189b860c39704.zip |
#1838 update merge base before generate new patch
Diffstat (limited to 'modules/git')
-rw-r--r-- | modules/git/repo_pull.go | 50 |
1 files changed, 37 insertions, 13 deletions
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) + + prInfo := new(PullRequestInfo) + prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch) if err != nil { - return nil, fmt.Errorf("get merge base: %v", concatenateError(err, stderr)) + return nil, fmt.Errorf("GetMergeBase: %v", err) } - prInfo.MergeBase = strings.TrimSpace(stdout) - stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat) + 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)) } |