2019-06-22 19:35:34 +02:00
|
|
|
// Copyright 2019 The Gitea 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 pull
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-11-01 01:30:02 +01:00
|
|
|
"regexp"
|
2019-06-22 19:35:34 +02:00
|
|
|
"strings"
|
2019-10-16 15:42:42 +02:00
|
|
|
"time"
|
2019-06-22 19:35:34 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-11-05 12:04:08 +01:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2019-06-22 19:35:34 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-15 16:46:21 +02:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-10-12 02:13:27 +02:00
|
|
|
|
|
|
|
"github.com/mcuadros/go-version"
|
2019-06-22 19:35:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Merge merges pull request to base repository.
|
|
|
|
// FIXME: add repoWorkingPull make sure two merges does not happen at same time.
|
|
|
|
func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository, mergeStyle models.MergeStyle, message string) (err error) {
|
2019-10-16 15:42:42 +02:00
|
|
|
binVersion, err := git.BinVersion()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to get git version: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
if err = pr.GetHeadRepo(); err != nil {
|
|
|
|
return fmt.Errorf("GetHeadRepo: %v", err)
|
|
|
|
} else if err = pr.GetBaseRepo(); err != nil {
|
|
|
|
return fmt.Errorf("GetBaseRepo: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
prUnit, err := pr.BaseRepo.GetUnit(models.UnitTypePullRequests)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
prConfig := prUnit.PullRequestsConfig()
|
|
|
|
|
|
|
|
if err := pr.CheckUserAllowedToMerge(doer); err != nil {
|
|
|
|
return fmt.Errorf("CheckUserAllowedToMerge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if merge style is correct and allowed
|
|
|
|
if !prConfig.IsMergeStyleAllowed(mergeStyle) {
|
|
|
|
return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: mergeStyle}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2019-10-15 05:28:40 +02:00
|
|
|
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false)
|
2019-06-22 19:35:34 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Clone base repo.
|
|
|
|
tmpBasePath, err := models.CreateTemporaryPath("merge")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
|
|
|
|
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-10-18 13:13:31 +02:00
|
|
|
headRepoPath := pr.HeadRepo.RepoPath()
|
2019-06-22 19:35:34 +02:00
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.InitRepository(tmpBasePath, false); err != nil {
|
|
|
|
return fmt.Errorf("git init: %v", err)
|
2019-06-22 19:35:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteRepoName := "head_repo"
|
2019-10-12 02:13:27 +02:00
|
|
|
baseBranch := "base"
|
2019-06-22 19:35:34 +02:00
|
|
|
|
|
|
|
// Add head repo remote.
|
|
|
|
addCacheRepo := func(staging, cache string) error {
|
|
|
|
p := filepath.Join(staging, ".git", "objects", "info", "alternates")
|
2019-10-12 02:13:27 +02:00
|
|
|
f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
2019-06-22 19:35:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
data := filepath.Join(cache, "objects")
|
|
|
|
if _, err := fmt.Fprintln(f, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := addCacheRepo(tmpBasePath, baseGitRepo.Path); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errbuf strings.Builder
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseGitRepo.Path).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git remote add [%s -> %s]: %s", baseGitRepo.Path, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.NewCommand("fetch", "origin", "--no-tags", pr.BaseBranch+":"+baseBranch, pr.BaseBranch+":original_"+baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git symbolic-ref HEAD base [%s]: %s", tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
|
|
|
|
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
trackingBranch := "tracking"
|
2019-06-22 19:35:34 +02:00
|
|
|
// Fetch head branch
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("fetch", "--no-tags", remoteRepoName, pr.HeadBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
stagingBranch := "staging"
|
2019-06-22 19:35:34 +02:00
|
|
|
|
|
|
|
// Enable sparse-checkout
|
2019-10-12 02:13:27 +02:00
|
|
|
sparseCheckoutList, err := getDiffTree(tmpBasePath, baseBranch, trackingBranch)
|
2019-06-22 19:35:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getDiffTree: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
infoPath := filepath.Join(tmpBasePath, ".git", "info")
|
|
|
|
if err := os.MkdirAll(infoPath, 0700); err != nil {
|
|
|
|
return fmt.Errorf("creating directory failed [%s]: %v", infoPath, err)
|
|
|
|
}
|
|
|
|
sparseCheckoutListPath := filepath.Join(infoPath, "sparse-checkout")
|
|
|
|
if err := ioutil.WriteFile(sparseCheckoutListPath, []byte(sparseCheckoutList), 0600); err != nil {
|
|
|
|
return fmt.Errorf("Writing sparse-checkout file to %s: %v", sparseCheckoutListPath, err)
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
gitConfigCommand := func() func() *git.Command {
|
|
|
|
binVersion, err := git.BinVersion()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error retrieving git version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if version.Compare(binVersion, "1.8.0", ">=") {
|
|
|
|
return func() *git.Command {
|
|
|
|
return git.NewCommand("config", "--local")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return func() *git.Command {
|
|
|
|
return git.NewCommand("config")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
// Switch off LFS process (set required, clean and smudge here also)
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := gitConfigCommand().AddArguments("filter.lfs.process", "").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git config [filter.lfs.process -> <> ]: %v", errbuf.String())
|
|
|
|
}
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := gitConfigCommand().AddArguments("filter.lfs.required", "false").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git config [filter.lfs.required -> <false> ]: %v", errbuf.String())
|
|
|
|
}
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := gitConfigCommand().AddArguments("filter.lfs.clean", "").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git config [filter.lfs.clean -> <> ]: %v", errbuf.String())
|
|
|
|
}
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := gitConfigCommand().AddArguments("filter.lfs.smudge", "").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git config [filter.lfs.smudge -> <> ]: %v", errbuf.String())
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := gitConfigCommand().AddArguments("core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read base branch index
|
|
|
|
if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git read-tree HEAD: %s", errbuf.String())
|
|
|
|
}
|
|
|
|
|
2019-10-16 15:42:42 +02:00
|
|
|
// Determine if we should sign
|
|
|
|
signArg := ""
|
|
|
|
if version.Compare(binVersion, "1.7.9", ">=") {
|
|
|
|
sign, keyID := pr.BaseRepo.SignMerge(doer, tmpBasePath, "HEAD", trackingBranch)
|
|
|
|
if sign {
|
|
|
|
signArg = "-S" + keyID
|
|
|
|
} else if version.Compare(binVersion, "2.0.0", ">=") {
|
|
|
|
signArg = "--no-gpg-sign"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sig := doer.NewGitSig()
|
|
|
|
commitTimeStr := time.Now().Format(time.RFC3339)
|
|
|
|
|
|
|
|
// Because this may call hooks we should pass in the environment
|
|
|
|
env := append(os.Environ(),
|
|
|
|
"GIT_AUTHOR_NAME="+sig.Name,
|
|
|
|
"GIT_AUTHOR_EMAIL="+sig.Email,
|
|
|
|
"GIT_AUTHOR_DATE="+commitTimeStr,
|
|
|
|
"GIT_COMMITTER_NAME="+sig.Name,
|
|
|
|
"GIT_COMMITTER_EMAIL="+sig.Email,
|
|
|
|
"GIT_COMMITTER_DATE="+commitTimeStr,
|
|
|
|
)
|
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
// Merge commits.
|
|
|
|
switch mergeStyle {
|
|
|
|
case models.MergeStyleMerge:
|
|
|
|
if err := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
|
|
|
|
2019-10-16 15:42:42 +02:00
|
|
|
if signArg == "" {
|
|
|
|
if err := git.NewCommand("commit", "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := git.NewCommand("commit", signArg, "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
2019-06-22 19:35:34 +02:00
|
|
|
}
|
|
|
|
case models.MergeStyleRebase:
|
|
|
|
// Checkout head branch
|
|
|
|
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
|
|
|
}
|
|
|
|
// Rebase before merging
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("rebase", "-q", baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
// Checkout base branch again
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("checkout", baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
|
|
|
}
|
|
|
|
// Merge fast forward
|
|
|
|
if err := git.NewCommand("merge", "--ff-only", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
case models.MergeStyleRebaseMerge:
|
|
|
|
// Checkout head branch
|
|
|
|
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
|
|
|
}
|
|
|
|
// Rebase before merging
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("rebase", "-q", baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
// Checkout base branch again
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("checkout", baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
|
|
|
}
|
|
|
|
// Prepare merge with commit
|
|
|
|
if err := git.NewCommand("merge", "--no-ff", "--no-commit", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set custom message and author and create merge commit
|
2019-10-16 15:42:42 +02:00
|
|
|
if signArg == "" {
|
|
|
|
if err := git.NewCommand("commit", "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := git.NewCommand("commit", signArg, "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
2019-06-22 19:35:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case models.MergeStyleSquash:
|
|
|
|
// Merge with squash
|
|
|
|
if err := git.NewCommand("merge", "-q", "--squash", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
|
|
|
}
|
|
|
|
sig := pr.Issue.Poster.NewGitSig()
|
2019-10-16 15:42:42 +02:00
|
|
|
if signArg == "" {
|
|
|
|
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := git.NewCommand("commit", signArg, fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
|
|
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
|
|
|
}
|
2019-06-22 19:35:34 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: mergeStyle}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK we should cache our current head and origin/headbranch
|
|
|
|
mergeHeadSHA, err := git.GetFullCommitID(tmpBasePath, "HEAD")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get full commit id for HEAD: %v", err)
|
|
|
|
}
|
2019-10-12 02:13:27 +02:00
|
|
|
mergeBaseSHA, err := git.GetFullCommitID(tmpBasePath, "original_"+baseBranch)
|
2019-06-22 19:35:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get full commit id for origin/%s: %v", pr.BaseBranch, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now it's questionable about where this should go - either after or before the push
|
|
|
|
// I think in the interests of data safety - failures to push to the lfs should prevent
|
|
|
|
// the merge as you can always remerge.
|
|
|
|
if setting.LFS.StartServer {
|
|
|
|
if err := LFSPush(tmpBasePath, mergeHeadSHA, mergeBaseSHA, pr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 13:13:31 +02:00
|
|
|
var headUser *models.User
|
|
|
|
err = pr.HeadRepo.GetOwner()
|
2019-07-01 03:18:13 +02:00
|
|
|
if err != nil {
|
|
|
|
if !models.IsErrUserNotExist(err) {
|
2019-10-18 13:13:31 +02:00
|
|
|
log.Error("Can't find user: %d for head repository - %v", pr.HeadRepo.OwnerID, err)
|
2019-07-01 03:18:13 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-10-18 13:13:31 +02:00
|
|
|
log.Error("Can't find user: %d for head repository - defaulting to doer: %s - %v", pr.HeadRepo.OwnerID, doer.Name, err)
|
2019-07-01 03:18:13 +02:00
|
|
|
headUser = doer
|
2019-10-18 13:13:31 +02:00
|
|
|
} else {
|
|
|
|
headUser = pr.HeadRepo.Owner
|
2019-07-01 03:18:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-16 15:42:42 +02:00
|
|
|
env = models.FullPushingEnvironment(
|
2019-07-25 23:50:20 +02:00
|
|
|
headUser,
|
|
|
|
doer,
|
|
|
|
pr.BaseRepo,
|
|
|
|
pr.BaseRepo.Name,
|
|
|
|
pr.ID,
|
|
|
|
)
|
2019-06-22 19:35:34 +02:00
|
|
|
|
|
|
|
// Push back to upstream.
|
2019-10-12 02:13:27 +02:00
|
|
|
if err := git.NewCommand("push", "origin", baseBranch+":"+pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return fmt.Errorf("git push: %s", errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.MergedCommitID, err = baseGitRepo.GetBranchCommitID(pr.BaseBranch)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetBranchCommit: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:46:21 +02:00
|
|
|
pr.MergedUnix = timeutil.TimeStampNow()
|
2019-06-22 19:35:34 +02:00
|
|
|
pr.Merger = doer
|
|
|
|
pr.MergerID = doer.ID
|
|
|
|
|
|
|
|
if err = pr.SetMerged(); err != nil {
|
|
|
|
log.Error("setMerged [%d]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = models.MergePullRequestAction(doer, pr.Issue.Repo, pr.Issue); err != nil {
|
|
|
|
log.Error("MergePullRequestAction [%d]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset cached commit count
|
|
|
|
cache.Remove(pr.Issue.Repo.GetCommitsCountCacheKey(pr.BaseBranch, true))
|
|
|
|
|
|
|
|
// Reload pull request information.
|
|
|
|
if err = pr.LoadAttributes(); err != nil {
|
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:04:08 +01:00
|
|
|
notification.NotifyIssueChangeStatus(doer, pr.Issue, true)
|
2019-06-22 19:35:34 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 01:30:02 +01:00
|
|
|
var escapedSymbols = regexp.MustCompile(`([*[?! \\])`)
|
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
|
|
|
|
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
|
|
|
|
var outbuf, errbuf strings.Builder
|
|
|
|
// Compute the diff-tree for sparse-checkout
|
2019-11-01 01:30:02 +01:00
|
|
|
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
|
2019-06-22 19:35:34 +02:00
|
|
|
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
|
|
|
|
}
|
|
|
|
return outbuf.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 01:30:02 +01:00
|
|
|
scanNullTerminatedStrings := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|
|
|
if atEOF && len(data) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
if i := bytes.IndexByte(data, '\x00'); i >= 0 {
|
|
|
|
return i + 1, data[0:i], nil
|
|
|
|
}
|
|
|
|
if atEOF {
|
|
|
|
return len(data), data, nil
|
|
|
|
}
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefixing '/' for each entry, otherwise all files with the same name in subdirectories would be matched.
|
|
|
|
out := bytes.Buffer{}
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(list))
|
2019-11-01 01:30:02 +01:00
|
|
|
scanner.Split(scanNullTerminatedStrings)
|
2019-06-22 19:35:34 +02:00
|
|
|
for scanner.Scan() {
|
2019-11-01 01:30:02 +01:00
|
|
|
filepath := scanner.Text()
|
|
|
|
// escape '*', '?', '[', spaces and '!' prefix
|
|
|
|
filepath = escapedSymbols.ReplaceAllString(filepath, `\$1`)
|
|
|
|
// no necessary to escape the first '#' symbol because the first symbol is '/'
|
|
|
|
fmt.Fprintf(&out, "/%s\n", filepath)
|
2019-06-22 19:35:34 +02:00
|
|
|
}
|
2019-11-01 01:30:02 +01:00
|
|
|
|
2019-06-22 19:35:34 +02:00
|
|
|
return out.String(), nil
|
|
|
|
}
|