2019-06-22 23:05:34 +05:30
// 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"
"os"
"path/filepath"
2019-11-01 06:00:02 +05:30
"regexp"
2019-06-22 23:05:34 +05:30
"strings"
2019-10-16 19:12:42 +05:30
"time"
2019-06-22 23:05:34 +05:30
"code.gitea.io/gitea/models"
2021-12-10 06:57:50 +05:30
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
2021-11-10 01:27:58 +05:30
"code.gitea.io/gitea/models/unit"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2019-06-22 23:05:34 +05:30
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
2019-11-05 16:34:08 +05:30
"code.gitea.io/gitea/modules/notification"
2019-11-18 18:43:07 +05:30
"code.gitea.io/gitea/modules/references"
2019-06-22 23:05:34 +05:30
"code.gitea.io/gitea/modules/setting"
2019-08-15 20:16:21 +05:30
"code.gitea.io/gitea/modules/timeutil"
2021-12-10 13:44:24 +05:30
asymkey_service "code.gitea.io/gitea/services/asymkey"
2019-11-18 18:43:07 +05:30
issue_service "code.gitea.io/gitea/services/issue"
2019-06-22 23:05:34 +05:30
)
// Merge merges pull request to base repository.
2020-01-11 12:59:34 +05:30
// Caller should check PR is ready to be merged (review and status checks)
2019-06-22 23:05:34 +05:30
// FIXME: add repoWorkingPull make sure two merges does not happen at same time.
2021-12-10 06:57:50 +05:30
func Merge ( pr * models . PullRequest , doer * user_model . User , baseGitRepo * git . Repository , mergeStyle repo_model . MergeStyle , message string ) ( err error ) {
2020-03-03 04:01:55 +05:30
if err = pr . LoadHeadRepo ( ) ; err != nil {
log . Error ( "LoadHeadRepo: %v" , err )
return fmt . Errorf ( "LoadHeadRepo: %v" , err )
} else if err = pr . LoadBaseRepo ( ) ; err != nil {
log . Error ( "LoadBaseRepo: %v" , err )
return fmt . Errorf ( "LoadBaseRepo: %v" , err )
2019-06-22 23:05:34 +05:30
}
2021-11-10 01:27:58 +05:30
prUnit , err := pr . BaseRepo . GetUnit ( unit . TypePullRequests )
2019-06-22 23:05:34 +05:30
if err != nil {
2021-11-10 01:27:58 +05:30
log . Error ( "pr.BaseRepo.GetUnit(unit.TypePullRequests): %v" , err )
2019-06-22 23:05:34 +05:30
return err
}
prConfig := prUnit . PullRequestsConfig ( )
// Check if merge style is correct and allowed
if ! prConfig . IsMergeStyleAllowed ( mergeStyle ) {
return models . ErrInvalidMergeStyle { ID : pr . BaseRepo . ID , Style : mergeStyle }
}
defer func ( ) {
2020-01-09 07:17:45 +05:30
go AddTestPullRequestTask ( doer , pr . BaseRepo . ID , pr . BaseBranch , false , "" , "" )
2019-06-22 23:05:34 +05:30
} ( )
2020-02-10 04:39:31 +05:30
pr . MergedCommitID , err = rawMerge ( pr , doer , mergeStyle , message )
2020-01-17 11:33:40 +05:30
if err != nil {
2020-02-10 04:39:31 +05:30
return err
2020-01-17 11:33:40 +05:30
}
pr . MergedUnix = timeutil . TimeStampNow ( )
pr . Merger = doer
pr . MergerID = doer . ID
2021-04-10 13:57:29 +05:30
if _ , err := pr . SetMerged ( ) ; err != nil {
2020-01-17 11:33:40 +05:30
log . Error ( "setMerged [%d]: %v" , pr . ID , err )
}
2020-02-10 04:39:31 +05:30
if err := pr . LoadIssue ( ) ; err != nil {
log . Error ( "loadIssue [%d]: %v" , pr . ID , err )
}
if err := pr . Issue . LoadRepo ( ) ; err != nil {
log . Error ( "loadRepo for issue [%d]: %v" , pr . ID , err )
}
2021-12-10 06:57:50 +05:30
if err := pr . Issue . Repo . GetOwner ( db . DefaultContext ) ; err != nil {
2020-02-10 04:39:31 +05:30
log . Error ( "GetOwner for issue repo [%d]: %v" , pr . ID , err )
}
2020-01-17 11:33:40 +05:30
notification . NotifyMergePullRequest ( pr , doer )
// Reset cached commit count
cache . Remove ( pr . Issue . Repo . GetCommitsCountCacheKey ( pr . BaseBranch , true ) )
// Resolve cross references
refs , err := pr . ResolveCrossReferences ( )
if err != nil {
log . Error ( "ResolveCrossReferences: %v" , err )
return nil
}
for _ , ref := range refs {
if err = ref . LoadIssue ( ) ; err != nil {
return err
}
if err = ref . Issue . LoadRepo ( ) ; err != nil {
return err
}
2021-04-09 13:10:34 +05:30
close := ref . RefAction == references . XRefActionCloses
2020-01-17 11:33:40 +05:30
if close != ref . Issue . IsClosed {
if err = issue_service . ChangeStatus ( ref . Issue , doer , close ) ; err != nil {
return err
}
}
}
return nil
}
// rawMerge perform the merge operation without changing any pull information in database
2021-12-10 06:57:50 +05:30
func rawMerge ( pr * models . PullRequest , doer * user_model . User , mergeStyle repo_model . MergeStyle , message string ) ( string , error ) {
2020-09-05 22:12:58 +05:30
err := git . LoadGitVersion ( )
2020-01-17 11:33:40 +05:30
if err != nil {
2020-09-05 22:12:58 +05:30
log . Error ( "git.LoadGitVersion: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "Unable to get git version: %v" , err )
2020-01-17 11:33:40 +05:30
}
2019-06-22 23:05:34 +05:30
// Clone base repo.
2019-12-14 03:51:06 +05:30
tmpBasePath , err := createTemporaryRepo ( pr )
2019-06-22 23:05:34 +05:30
if err != nil {
2019-11-10 14:12:51 +05:30
log . Error ( "CreateTemporaryPath: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-06-22 23:05:34 +05:30
}
defer func ( ) {
if err := models . RemoveTemporaryPath ( tmpBasePath ) ; err != nil {
log . Error ( "Merge: RemoveTemporaryPath: %s" , err )
}
} ( )
2019-10-12 05:43:27 +05:30
baseBranch := "base"
trackingBranch := "tracking"
stagingBranch := "staging"
2019-06-22 23:05:34 +05:30
2019-12-14 03:51:06 +05:30
var outbuf , errbuf strings . Builder
2019-06-22 23:05:34 +05:30
// Enable sparse-checkout
2019-10-12 05:43:27 +05:30
sparseCheckoutList , err := getDiffTree ( tmpBasePath , baseBranch , trackingBranch )
2019-06-22 23:05:34 +05:30
if err != nil {
2019-11-10 14:12:51 +05:30
log . Error ( "getDiffTree(%s, %s, %s): %v" , tmpBasePath , baseBranch , trackingBranch , err )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "getDiffTree: %v" , err )
2019-06-22 23:05:34 +05:30
}
infoPath := filepath . Join ( tmpBasePath , ".git" , "info" )
if err := os . MkdirAll ( infoPath , 0700 ) ; err != nil {
2019-11-10 14:12:51 +05:30
log . Error ( "Unable to create .git/info in %s: %v" , tmpBasePath , err )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "Unable to create .git/info in tmpBasePath: %v" , err )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
2019-06-22 23:05:34 +05:30
sparseCheckoutListPath := filepath . Join ( infoPath , "sparse-checkout" )
2021-09-22 11:08:34 +05:30
if err := os . WriteFile ( sparseCheckoutListPath , [ ] byte ( sparseCheckoutList ) , 0600 ) ; err != nil {
2019-11-10 14:12:51 +05:30
log . Error ( "Unable to write .git/info/sparse-checkout file in %s: %v" , tmpBasePath , err )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "Unable to write .git/info/sparse-checkout file in tmpBasePath: %v" , err )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
var gitConfigCommand func ( ) * git . Command
2020-10-21 21:12:08 +05:30
if git . CheckGitVersionAtLeast ( "1.8.0" ) == nil {
2019-11-10 14:12:51 +05:30
gitConfigCommand = func ( ) * git . Command {
return git . NewCommand ( "config" , "--local" )
2019-10-12 05:43:27 +05:30
}
2019-11-10 14:12:51 +05:30
} else {
gitConfigCommand = func ( ) * git . Command {
2019-10-12 05:43:27 +05:30
return git . NewCommand ( "config" )
}
2019-11-10 14:12:51 +05:30
}
2019-10-12 05:43:27 +05:30
2019-06-22 23:05:34 +05:30
// Switch off LFS process (set required, clean and smudge here also)
2019-11-10 14:12:51 +05:30
if err := gitConfigCommand ( ) . AddArguments ( "filter.lfs.process" , "" ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git config [filter.lfs.process -> <> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git config [filter.lfs.process -> <> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
if err := gitConfigCommand ( ) . AddArguments ( "filter.lfs.required" , "false" ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git config [filter.lfs.required -> <false> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git config [filter.lfs.required -> <false> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
if err := gitConfigCommand ( ) . AddArguments ( "filter.lfs.clean" , "" ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git config [filter.lfs.clean -> <> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git config [filter.lfs.clean -> <> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
if err := gitConfigCommand ( ) . AddArguments ( "filter.lfs.smudge" , "" ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git config [filter.lfs.smudge -> <> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git config [filter.lfs.smudge -> <> ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
2019-11-10 14:12:51 +05:30
if err := gitConfigCommand ( ) . AddArguments ( "core.sparseCheckout" , "true" ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git config [core.sparseCheckout -> true ]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git config [core.sparsecheckout -> true]: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
// Read base branch index
2019-11-10 14:12:51 +05:30
if err := git . NewCommand ( "read-tree" , "HEAD" ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git read-tree HEAD: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "Unable to read base branch in to the index: %v\n%s\n%s" , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
2020-09-19 22:14:55 +05:30
sig := doer . NewGitSig ( )
committer := sig
2019-10-16 19:12:42 +05:30
// Determine if we should sign
signArg := ""
2020-10-21 21:12:08 +05:30
if git . CheckGitVersionAtLeast ( "1.7.9" ) == nil {
2021-12-10 13:44:24 +05:30
sign , keyID , signer , _ := asymkey_service . SignMerge ( pr , doer , tmpBasePath , "HEAD" , trackingBranch )
2019-10-16 19:12:42 +05:30
if sign {
signArg = "-S" + keyID
2021-12-10 06:57:50 +05:30
if pr . BaseRepo . GetTrustModel ( ) == repo_model . CommitterTrustModel || pr . BaseRepo . GetTrustModel ( ) == repo_model . CollaboratorCommitterTrustModel {
2020-09-19 22:14:55 +05:30
committer = signer
}
2020-10-21 21:12:08 +05:30
} else if git . CheckGitVersionAtLeast ( "2.0.0" ) == nil {
2019-10-16 19:12:42 +05:30
signArg = "--no-gpg-sign"
}
}
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 ,
2020-09-19 22:14:55 +05:30
"GIT_COMMITTER_NAME=" + committer . Name ,
"GIT_COMMITTER_EMAIL=" + committer . Email ,
2019-10-16 19:12:42 +05:30
"GIT_COMMITTER_DATE=" + commitTimeStr ,
)
2019-06-22 23:05:34 +05:30
// Merge commits.
switch mergeStyle {
2021-12-10 06:57:50 +05:30
case repo_model . MergeStyleMerge :
2019-11-10 14:12:51 +05:30
cmd := git . NewCommand ( "merge" , "--no-ff" , "--no-commit" , trackingBranch )
if err := runMergeCommand ( pr , mergeStyle , cmd , tmpBasePath ) ; err != nil {
log . Error ( "Unable to merge tracking into base: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
if err := commitAndSignNoAuthor ( pr , message , signArg , tmpBasePath , env ) ; err != nil {
log . Error ( "Unable to make final commit: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-06-22 23:05:34 +05:30
}
2021-12-10 06:57:50 +05:30
case repo_model . MergeStyleRebase :
2019-11-10 14:12:51 +05:30
fallthrough
2021-12-10 06:57:50 +05:30
case repo_model . MergeStyleRebaseUpdate :
2021-08-31 19:33:45 +05:30
fallthrough
2021-12-10 06:57:50 +05:30
case repo_model . MergeStyleRebaseMerge :
2019-06-22 23:05:34 +05:30
// Checkout head branch
2019-11-10 14:12:51 +05:30
if err := git . NewCommand ( "checkout" , "-b" , stagingBranch , trackingBranch ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
// Rebase before merging
2019-11-10 14:12:51 +05:30
if err := git . NewCommand ( "rebase" , baseBranch ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
// Rebase will leave a REBASE_HEAD file in .git if there is a conflict
if _ , statErr := os . Stat ( filepath . Join ( tmpBasePath , ".git" , "REBASE_HEAD" ) ) ; statErr == nil {
2020-04-03 21:30:41 +05:30
var commitSha string
ok := false
failingCommitPaths := [ ] string {
filepath . Join ( tmpBasePath , ".git" , "rebase-apply" , "original-commit" ) , // Git < 2.26
filepath . Join ( tmpBasePath , ".git" , "rebase-merge" , "stopped-sha" ) , // Git >= 2.26
}
for _ , failingCommitPath := range failingCommitPaths {
2021-11-15 11:32:53 +05:30
if _ , statErr := os . Stat ( failingCommitPath ) ; statErr == nil {
commitShaBytes , readErr := os . ReadFile ( failingCommitPath )
2020-04-03 21:30:41 +05:30
if readErr != nil {
// Abandon this attempt to handle the error
log . Error ( "git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
return "" , fmt . Errorf ( "git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
}
commitSha = strings . TrimSpace ( string ( commitShaBytes ) )
ok = true
break
}
}
if ! ok {
log . Error ( "Unable to determine failing commit sha for this rebase message. Cannot cast as models.ErrRebaseConflicts." )
2019-11-10 14:12:51 +05:30
log . Error ( "git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2019-11-10 14:12:51 +05:30
}
2020-04-03 21:30:41 +05:30
log . Debug ( "RebaseConflict at %s [%s:%s -> %s:%s]: %v\n%s\n%s" , commitSha , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , models . ErrRebaseConflicts {
2019-11-10 14:12:51 +05:30
Style : mergeStyle ,
2020-04-03 21:30:41 +05:30
CommitSHA : commitSha ,
2019-11-10 14:12:51 +05:30
StdOut : outbuf . String ( ) ,
StdErr : errbuf . String ( ) ,
Err : err ,
}
}
log . Error ( "git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2021-08-31 19:33:45 +05:30
// not need merge, just update by rebase. so skip
2021-12-10 06:57:50 +05:30
if mergeStyle == repo_model . MergeStyleRebaseUpdate {
2021-08-31 19:33:45 +05:30
break
}
2019-06-22 23:05:34 +05:30
// Checkout base branch again
2019-11-10 14:12:51 +05:30
if err := git . NewCommand ( "checkout" , baseBranch ) . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git checkout base prior to merge post staging rebase [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
2019-11-10 14:12:51 +05:30
cmd := git . NewCommand ( "merge" )
2021-12-10 06:57:50 +05:30
if mergeStyle == repo_model . MergeStyleRebase {
2019-11-10 14:12:51 +05:30
cmd . AddArguments ( "--ff-only" )
2019-10-16 19:12:42 +05:30
} else {
2019-11-10 14:12:51 +05:30
cmd . AddArguments ( "--no-ff" , "--no-commit" )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
cmd . AddArguments ( stagingBranch )
2019-06-22 23:05:34 +05:30
2019-11-10 14:12:51 +05:30
// Prepare merge with commit
if err := runMergeCommand ( pr , mergeStyle , cmd , tmpBasePath ) ; err != nil {
log . Error ( "Unable to merge staging into base: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-11-10 14:12:51 +05:30
}
2021-12-10 06:57:50 +05:30
if mergeStyle == repo_model . MergeStyleRebaseMerge {
2019-11-10 14:12:51 +05:30
if err := commitAndSignNoAuthor ( pr , message , signArg , tmpBasePath , env ) ; err != nil {
log . Error ( "Unable to make final commit: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-11-10 14:12:51 +05:30
}
}
2021-12-10 06:57:50 +05:30
case repo_model . MergeStyleSquash :
2019-06-22 23:05:34 +05:30
// Merge with squash
2019-11-10 14:12:51 +05:30
cmd := git . NewCommand ( "merge" , "--squash" , trackingBranch )
if err := runMergeCommand ( pr , mergeStyle , cmd , tmpBasePath ) ; err != nil {
log . Error ( "Unable to merge --squash tracking into base: %v" , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
2020-04-10 16:10:36 +05:30
if err = pr . Issue . LoadPoster ( ) ; err != nil {
log . Error ( "LoadPoster: %v" , err )
return "" , fmt . Errorf ( "LoadPoster: %v" , err )
}
2019-06-22 23:05:34 +05:30
sig := pr . Issue . Poster . NewGitSig ( )
2019-10-16 19:12:42 +05:30
if signArg == "" {
2019-11-10 14:12:51 +05:30
if err := git . NewCommand ( "commit" , fmt . Sprintf ( "--author='%s <%s>'" , sig . Name , sig . Email ) , "-m" , message ) . RunInDirTimeoutEnvPipeline ( env , - 1 , tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2019-10-16 19:12:42 +05:30
}
} else {
2021-11-29 12:39:55 +05:30
if setting . Repository . PullRequest . AddCoCommitterTrailers && committer . String ( ) != sig . String ( ) {
2020-09-19 22:14:55 +05:30
// add trailer
2020-12-22 07:49:33 +05:30
message += fmt . Sprintf ( "\nCo-authored-by: %s\nCo-committed-by: %s\n" , sig . String ( ) , sig . String ( ) )
2020-09-19 22:14:55 +05:30
}
2019-11-10 14:12:51 +05:30
if err := git . NewCommand ( "commit" , signArg , fmt . Sprintf ( "--author='%s <%s>'" , sig . Name , sig . Email ) , "-m" , message ) . RunInDirTimeoutEnvPipeline ( env , - 1 , tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
2019-10-16 19:12:42 +05:30
}
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
default :
2020-02-10 04:39:31 +05:30
return "" , models . ErrInvalidMergeStyle { ID : pr . BaseRepo . ID , Style : mergeStyle }
2019-06-22 23:05:34 +05:30
}
// OK we should cache our current head and origin/headbranch
mergeHeadSHA , err := git . GetFullCommitID ( tmpBasePath , "HEAD" )
if err != nil {
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "Failed to get full commit id for HEAD: %v" , err )
2019-06-22 23:05:34 +05:30
}
2019-10-12 05:43:27 +05:30
mergeBaseSHA , err := git . GetFullCommitID ( tmpBasePath , "original_" + baseBranch )
2019-06-22 23:05:34 +05:30
if err != nil {
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "Failed to get full commit id for origin/%s: %v" , pr . BaseBranch , err )
}
mergeCommitID , err := git . GetFullCommitID ( tmpBasePath , baseBranch )
if err != nil {
return "" , fmt . Errorf ( "Failed to get full commit id for the new merge: %v" , err )
2019-06-22 23:05:34 +05:30
}
// 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 {
2020-02-10 04:39:31 +05:30
return "" , err
2019-06-22 23:05:34 +05:30
}
}
2021-11-24 15:19:20 +05:30
var headUser * user_model . User
2021-12-10 06:57:50 +05:30
err = pr . HeadRepo . GetOwner ( db . DefaultContext )
2019-07-01 06:48:13 +05:30
if err != nil {
2021-11-24 15:19:20 +05:30
if ! user_model . IsErrUserNotExist ( err ) {
2019-10-18 16:43:31 +05:30
log . Error ( "Can't find user: %d for head repository - %v" , pr . HeadRepo . OwnerID , err )
2020-02-10 04:39:31 +05:30
return "" , err
2019-07-01 06:48:13 +05:30
}
2019-10-18 16:43:31 +05:30
log . Error ( "Can't find user: %d for head repository - defaulting to doer: %s - %v" , pr . HeadRepo . OwnerID , doer . Name , err )
2019-07-01 06:48:13 +05:30
headUser = doer
2019-10-18 16:43:31 +05:30
} else {
headUser = pr . HeadRepo . Owner
2019-07-01 06:48:13 +05:30
}
2019-10-16 19:12:42 +05:30
env = models . FullPushingEnvironment (
2019-07-26 03:20:20 +05:30
headUser ,
doer ,
pr . BaseRepo ,
pr . BaseRepo . Name ,
pr . ID ,
)
2019-06-22 23:05:34 +05:30
2021-08-31 19:33:45 +05:30
var pushCmd * git . Command
2021-12-10 06:57:50 +05:30
if mergeStyle == repo_model . MergeStyleRebaseUpdate {
2021-08-31 19:33:45 +05:30
// force push the rebase result to head brach
2021-12-02 12:58:08 +05:30
pushCmd = git . NewCommand ( "push" , "-f" , "head_repo" , stagingBranch + ":" + git . BranchPrefix + pr . HeadBranch )
2021-08-31 19:33:45 +05:30
} else {
2021-12-02 12:58:08 +05:30
pushCmd = git . NewCommand ( "push" , "origin" , baseBranch + ":" + git . BranchPrefix + pr . BaseBranch )
2021-08-31 19:33:45 +05:30
}
2019-06-22 23:05:34 +05:30
// Push back to upstream.
2021-08-31 19:33:45 +05:30
if err := pushCmd . RunInDirTimeoutEnvPipeline ( env , - 1 , tmpBasePath , & outbuf , & errbuf ) ; err != nil {
2019-11-10 14:12:51 +05:30
if strings . Contains ( errbuf . String ( ) , "non-fast-forward" ) {
2020-03-28 09:43:18 +05:30
return "" , & git . ErrPushOutOfDate {
2019-11-10 14:12:51 +05:30
StdOut : outbuf . String ( ) ,
StdErr : errbuf . String ( ) ,
Err : err ,
}
2020-02-22 18:38:48 +05:30
} else if strings . Contains ( errbuf . String ( ) , "! [remote rejected]" ) {
2020-03-28 09:43:18 +05:30
err := & git . ErrPushRejected {
2020-02-22 18:38:48 +05:30
StdOut : outbuf . String ( ) ,
StdErr : errbuf . String ( ) ,
Err : err ,
}
err . GenerateMessage ( )
return "" , err
2019-11-10 14:12:51 +05:30
}
2020-02-10 04:39:31 +05:30
return "" , fmt . Errorf ( "git push: %s" , errbuf . String ( ) )
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
outbuf . Reset ( )
errbuf . Reset ( )
2019-06-22 23:05:34 +05:30
2020-02-10 04:39:31 +05:30
return mergeCommitID , nil
2019-06-22 23:05:34 +05:30
}
2019-11-10 14:12:51 +05:30
func commitAndSignNoAuthor ( pr * models . PullRequest , message , signArg , tmpBasePath string , env [ ] string ) error {
var outbuf , errbuf strings . Builder
if signArg == "" {
if err := git . NewCommand ( "commit" , "-m" , message ) . RunInDirTimeoutEnvPipeline ( env , - 1 , tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
return fmt . Errorf ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
}
} else {
if err := git . NewCommand ( "commit" , signArg , "-m" , message ) . RunInDirTimeoutEnvPipeline ( env , - 1 , tmpBasePath , & outbuf , & errbuf ) ; err != nil {
log . Error ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
return fmt . Errorf ( "git commit [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
}
}
return nil
}
2021-12-10 06:57:50 +05:30
func runMergeCommand ( pr * models . PullRequest , mergeStyle repo_model . MergeStyle , cmd * git . Command , tmpBasePath string ) error {
2019-11-10 14:12:51 +05:30
var outbuf , errbuf strings . Builder
if err := cmd . RunInDirPipeline ( tmpBasePath , & outbuf , & errbuf ) ; err != nil {
// Merge will leave a MERGE_HEAD file in the .git folder if there is a conflict
if _ , statErr := os . Stat ( filepath . Join ( tmpBasePath , ".git" , "MERGE_HEAD" ) ) ; statErr == nil {
// We have a merge conflict error
log . Debug ( "MergeConflict [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
return models . ErrMergeConflicts {
Style : mergeStyle ,
StdOut : outbuf . String ( ) ,
StdErr : errbuf . String ( ) ,
Err : err ,
}
} else if strings . Contains ( errbuf . String ( ) , "refusing to merge unrelated histories" ) {
log . Debug ( "MergeUnrelatedHistories [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
return models . ErrMergeUnrelatedHistories {
Style : mergeStyle ,
StdOut : outbuf . String ( ) ,
StdErr : errbuf . String ( ) ,
Err : err ,
}
}
log . Error ( "git merge [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
return fmt . Errorf ( "git merge [%s:%s -> %s:%s]: %v\n%s\n%s" , pr . HeadRepo . FullName ( ) , pr . HeadBranch , pr . BaseRepo . FullName ( ) , pr . BaseBranch , err , outbuf . String ( ) , errbuf . String ( ) )
}
return nil
}
2019-11-01 06:00:02 +05:30
var escapedSymbols = regexp . MustCompile ( ` ([*[?! \\]) ` )
2019-06-22 23:05:34 +05:30
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 06:00:02 +05:30
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 23:05:34 +05:30
return "" , fmt . Errorf ( "git diff-tree [%s base:%s head:%s]: %s" , repoPath , baseBranch , headBranch , errbuf . String ( ) )
}
return outbuf . String ( ) , nil
}
2019-11-01 06:00:02 +05:30
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 23:05:34 +05:30
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 06:00:02 +05:30
scanner . Split ( scanNullTerminatedStrings )
2019-06-22 23:05:34 +05:30
for scanner . Scan ( ) {
2019-11-01 06:00:02 +05:30
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 23:05:34 +05:30
}
2019-11-01 06:00:02 +05:30
2019-06-22 23:05:34 +05:30
return out . String ( ) , nil
}
2020-01-11 12:59:34 +05:30
2020-01-15 14:02:57 +05:30
// IsSignedIfRequired check if merge will be signed if required
2021-11-24 15:19:20 +05:30
func IsSignedIfRequired ( pr * models . PullRequest , doer * user_model . User ) ( bool , error ) {
2020-01-15 14:02:57 +05:30
if err := pr . LoadProtectedBranch ( ) ; err != nil {
return false , err
}
if pr . ProtectedBranch == nil || ! pr . ProtectedBranch . RequireSignedCommits {
return true , nil
}
2021-12-10 13:44:24 +05:30
sign , _ , _ , err := asymkey_service . SignMerge ( pr , doer , pr . BaseRepo . RepoPath ( ) , pr . BaseBranch , pr . GetGitRefName ( ) )
2020-01-15 14:02:57 +05:30
return sign , err
}
2020-01-11 12:59:34 +05:30
// IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
2021-11-24 15:19:20 +05:30
func IsUserAllowedToMerge ( pr * models . PullRequest , p models . Permission , user * user_model . User ) ( bool , error ) {
2020-04-14 21:59:31 +05:30
if user == nil {
return false , nil
}
2020-01-11 12:59:34 +05:30
err := pr . LoadProtectedBranch ( )
if err != nil {
return false , err
}
2021-12-10 06:57:50 +05:30
if ( p . CanWrite ( unit . TypeCode ) && pr . ProtectedBranch == nil ) || ( pr . ProtectedBranch != nil && models . IsUserMergeWhitelisted ( pr . ProtectedBranch , user . ID , p ) ) {
2020-01-11 12:59:34 +05:30
return true , nil
}
return false , nil
}
// CheckPRReadyToMerge checks whether the PR is ready to be merged (reviews and status checks)
2020-10-14 00:20:57 +05:30
func CheckPRReadyToMerge ( pr * models . PullRequest , skipProtectedFilesCheck bool ) ( err error ) {
2020-03-03 04:01:55 +05:30
if err = pr . LoadBaseRepo ( ) ; err != nil {
return fmt . Errorf ( "LoadBaseRepo: %v" , err )
}
if err = pr . LoadProtectedBranch ( ) ; err != nil {
return fmt . Errorf ( "LoadProtectedBranch: %v" , err )
2020-01-11 12:59:34 +05:30
}
if pr . ProtectedBranch == nil {
2020-03-03 04:01:55 +05:30
return nil
2020-01-11 12:59:34 +05:30
}
isPass , err := IsPullCommitStatusPass ( pr )
if err != nil {
return err
}
if ! isPass {
return models . ErrNotAllowedToMerge {
Reason : "Not all required status checks successful" ,
}
}
2020-04-17 06:30:36 +05:30
if ! pr . ProtectedBranch . HasEnoughApprovals ( pr ) {
2020-01-11 12:59:34 +05:30
return models . ErrNotAllowedToMerge {
Reason : "Does not have enough approvals" ,
}
}
2020-04-17 06:30:36 +05:30
if pr . ProtectedBranch . MergeBlockedByRejectedReview ( pr ) {
2020-01-11 12:59:34 +05:30
return models . ErrNotAllowedToMerge {
Reason : "There are requested changes" ,
}
}
2020-11-29 01:00:46 +05:30
if pr . ProtectedBranch . MergeBlockedByOfficialReviewRequests ( pr ) {
return models . ErrNotAllowedToMerge {
Reason : "There are official review requests" ,
}
}
2020-01-11 12:59:34 +05:30
2020-04-17 06:30:36 +05:30
if pr . ProtectedBranch . MergeBlockedByOutdatedBranch ( pr ) {
return models . ErrNotAllowedToMerge {
Reason : "The head branch is behind the base branch" ,
}
}
2020-10-14 00:20:57 +05:30
if skipProtectedFilesCheck {
return nil
}
if pr . ProtectedBranch . MergeBlockedByProtectedFiles ( pr ) {
return models . ErrNotAllowedToMerge {
Reason : "Changed protected files" ,
}
}
2020-01-11 12:59:34 +05:30
return nil
}
2021-03-04 09:11:23 +05:30
// MergedManually mark pr as merged manually
2021-11-24 15:19:20 +05:30
func MergedManually ( pr * models . PullRequest , doer * user_model . User , baseGitRepo * git . Repository , commitID string ) ( err error ) {
2021-11-10 01:27:58 +05:30
prUnit , err := pr . BaseRepo . GetUnit ( unit . TypePullRequests )
2021-03-04 09:11:23 +05:30
if err != nil {
return
}
prConfig := prUnit . PullRequestsConfig ( )
// Check if merge style is correct and allowed
2021-12-10 06:57:50 +05:30
if ! prConfig . IsMergeStyleAllowed ( repo_model . MergeStyleManuallyMerged ) {
return models . ErrInvalidMergeStyle { ID : pr . BaseRepo . ID , Style : repo_model . MergeStyleManuallyMerged }
2021-03-04 09:11:23 +05:30
}
if len ( commitID ) < 40 {
return fmt . Errorf ( "Wrong commit ID" )
}
commit , err := baseGitRepo . GetCommit ( commitID )
if err != nil {
if git . IsErrNotExist ( err ) {
return fmt . Errorf ( "Wrong commit ID" )
}
return
}
ok , err := baseGitRepo . IsCommitInBranch ( commitID , pr . BaseBranch )
if err != nil {
return
}
if ! ok {
return fmt . Errorf ( "Wrong commit ID" )
}
pr . MergedCommitID = commitID
pr . MergedUnix = timeutil . TimeStamp ( commit . Author . When . Unix ( ) )
pr . Status = models . PullRequestStatusManuallyMerged
pr . Merger = doer
pr . MergerID = doer . ID
merged := false
if merged , err = pr . SetMerged ( ) ; err != nil {
return
} else if ! merged {
return fmt . Errorf ( "SetMerged failed" )
}
notification . NotifyMergePullRequest ( pr , doer )
log . Info ( "manuallyMerged[%d]: Marked as manually merged into %s/%s by commit id: %s" , pr . ID , pr . BaseRepo . Name , pr . BaseBranch , commit . ID . String ( ) )
return nil
}