#1838 update merge base before generate new patch
This commit is contained in:
parent
8e262f3ec4
commit
25ec20d525
6 changed files with 64 additions and 24 deletions
|
@ -5,7 +5,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
|
||||||
|
|
||||||
![](public/img/gogs-large-resize.png)
|
![](public/img/gogs-large-resize.png)
|
||||||
|
|
||||||
##### Current version: 0.6.22 Beta
|
##### Current version: 0.6.23 Beta
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
2
gogs.go
2
gogs.go
|
@ -17,7 +17,7 @@ import (
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
const APP_VER = "0.6.22.1103 Beta"
|
const APP_VER = "0.6.23.1103 Beta"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
|
|
@ -97,7 +97,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
|
||||||
var i int
|
var i int
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
// fmt.Println(i, line)
|
|
||||||
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
|
if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -180,8 +180,6 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
|
||||||
b = b[1 : len(b)-1]
|
b = b[1 : len(b)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(a, b)
|
|
||||||
|
|
||||||
curFile = &DiffFile{
|
curFile = &DiffFile{
|
||||||
Name: a,
|
Name: a,
|
||||||
Index: len(diff.Files) + 1,
|
Index: len(diff.Files) + 1,
|
||||||
|
|
|
@ -385,16 +385,18 @@ func (pr *PullRequest) UpdateCols(cols ...string) error {
|
||||||
var PullRequestQueue = NewUniqueQueue(setting.Repository.PullRequestQueueLength)
|
var PullRequestQueue = NewUniqueQueue(setting.Repository.PullRequestQueueLength)
|
||||||
|
|
||||||
// UpdatePatch generates and saves a new patch.
|
// UpdatePatch generates and saves a new patch.
|
||||||
func (pr *PullRequest) UpdatePatch() error {
|
func (pr *PullRequest) UpdatePatch() (err error) {
|
||||||
if err := pr.GetHeadRepo(); err != nil {
|
if err = pr.GetHeadRepo(); err != nil {
|
||||||
return fmt.Errorf("GetHeadRepo: %v", err)
|
return fmt.Errorf("GetHeadRepo: %v", err)
|
||||||
} else if pr.HeadRepo == nil {
|
} else if pr.HeadRepo == nil {
|
||||||
log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID)
|
log.Trace("PullRequest[%d].UpdatePatch: ignored cruppted data", pr.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := pr.GetBaseRepo(); err != nil {
|
if err = pr.GetBaseRepo(); err != nil {
|
||||||
return fmt.Errorf("GetBaseRepo: %v", err)
|
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()
|
headRepoPath, err := pr.HeadRepo.RepoPath()
|
||||||
|
@ -407,6 +409,22 @@ func (pr *PullRequest) UpdatePatch() error {
|
||||||
return fmt.Errorf("OpenRepository: %v", err)
|
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)
|
patch, err := headGitRepo.GetPatch(pr.MergeBase, pr.HeadBranch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("GetPatch: %v", err)
|
return fmt.Errorf("GetPatch: %v", err)
|
||||||
|
|
|
@ -20,31 +20,55 @@ type PullRequestInfo struct {
|
||||||
NumFiles int
|
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
|
// GetPullRequestInfo generates and returns pull request information
|
||||||
// between base and head branches of repositories.
|
// 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.
|
// Add a temporary remote.
|
||||||
tmpRemote := com.ToStr(time.Now().UnixNano())
|
tmpRemote := com.ToStr(time.Now().UnixNano())
|
||||||
_, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath)
|
if err = repo.AddRemote(tmpRemote, basePath); err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("AddRemote: %v", err)
|
||||||
return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, stderr))
|
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote)
|
repo.RemoveRemote(tmpRemote)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
prInfo := new(PullRequestInfo)
|
|
||||||
|
|
||||||
var stdout string
|
|
||||||
remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr))
|
return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
0.6.22.1103 Beta
|
0.6.23.1103 Beta
|
Loading…
Reference in a new issue