2015-09-01 19:07:02 -04:00
|
|
|
// Copyright 2015 The Gogs 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 git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PullRequestInfo struct {
|
|
|
|
MergeBase string
|
|
|
|
Commits *list.List
|
|
|
|
// Diff *Diff
|
|
|
|
NumFiles int
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:25:39 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-09-01 19:07:02 -04:00
|
|
|
// GetPullRequestInfo generates and returns pull request information
|
|
|
|
// between base and head branches of repositories.
|
2015-11-03 17:25:39 -05:00
|
|
|
func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (_ *PullRequestInfo, err error) {
|
2015-09-01 19:07:02 -04:00
|
|
|
// Add a temporary remote.
|
|
|
|
tmpRemote := com.ToStr(time.Now().UnixNano())
|
2015-11-03 17:25:39 -05:00
|
|
|
if err = repo.AddRemote(tmpRemote, basePath); err != nil {
|
|
|
|
return nil, fmt.Errorf("AddRemote: %v", err)
|
2015-09-01 19:07:02 -04:00
|
|
|
}
|
|
|
|
defer func() {
|
2015-11-03 17:25:39 -05:00
|
|
|
repo.RemoveRemote(tmpRemote)
|
2015-09-01 19:07:02 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch
|
2015-11-03 17:25:39 -05:00
|
|
|
|
|
|
|
prInfo := new(PullRequestInfo)
|
|
|
|
prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch)
|
2015-09-01 19:07:02 -04:00
|
|
|
if err != nil {
|
2015-11-03 17:25:39 -05:00
|
|
|
return nil, fmt.Errorf("GetMergeBase: %v", err)
|
2015-09-01 19:07:02 -04:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:25:39 -05:00
|
|
|
stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat)
|
2015-09-01 19:07:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr))
|
|
|
|
}
|
|
|
|
prInfo.Commits, err = parsePrettyFormatLog(repo, []byte(stdout))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsePrettyFormatLog: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count number of changed files.
|
|
|
|
stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "diff", "--name-only", remoteBranch+"..."+headBranch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("list changed files: %v", concatenateError(err, stderr))
|
|
|
|
}
|
|
|
|
prInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
|
|
|
|
|
|
|
|
return prInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPatch generates and returns patch data between given branches.
|
2015-10-04 20:54:06 -04:00
|
|
|
func (repo *Repository) GetPatch(mergeBase, headBranch string) ([]byte, error) {
|
2015-11-16 21:18:04 -05:00
|
|
|
stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "diff", "-p", "--binary", mergeBase, headBranch)
|
2015-09-01 19:07:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, concatenateError(err, string(stderr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, nil
|
|
|
|
}
|
2015-09-02 09:26:56 -04:00
|
|
|
|
|
|
|
// Merge merges pull request from head repository and branch.
|
|
|
|
func (repo *Repository) Merge(headRepoPath string, baseBranch, headBranch string) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|