You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

merge.go 22 kB

Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "bufio"
  8. "bytes"
  9. "fmt"
  10. "io/ioutil"
  11. "os"
  12. "path/filepath"
  13. "regexp"
  14. "strings"
  15. "time"
  16. "code.gitea.io/gitea/models"
  17. "code.gitea.io/gitea/modules/cache"
  18. "code.gitea.io/gitea/modules/git"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/notification"
  21. "code.gitea.io/gitea/modules/references"
  22. "code.gitea.io/gitea/modules/setting"
  23. "code.gitea.io/gitea/modules/timeutil"
  24. issue_service "code.gitea.io/gitea/services/issue"
  25. "github.com/mcuadros/go-version"
  26. )
  27. // Merge merges pull request to base repository.
  28. // Caller should check PR is ready to be merged (review and status checks)
  29. // FIXME: add repoWorkingPull make sure two merges does not happen at same time.
  30. func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository, mergeStyle models.MergeStyle, message string) (err error) {
  31. if err = pr.GetHeadRepo(); err != nil {
  32. log.Error("GetHeadRepo: %v", err)
  33. return fmt.Errorf("GetHeadRepo: %v", err)
  34. } else if err = pr.GetBaseRepo(); err != nil {
  35. log.Error("GetBaseRepo: %v", err)
  36. return fmt.Errorf("GetBaseRepo: %v", err)
  37. }
  38. prUnit, err := pr.BaseRepo.GetUnit(models.UnitTypePullRequests)
  39. if err != nil {
  40. log.Error("pr.BaseRepo.GetUnit(models.UnitTypePullRequests): %v", err)
  41. return err
  42. }
  43. prConfig := prUnit.PullRequestsConfig()
  44. // Check if merge style is correct and allowed
  45. if !prConfig.IsMergeStyleAllowed(mergeStyle) {
  46. return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: mergeStyle}
  47. }
  48. defer func() {
  49. go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false, "", "")
  50. }()
  51. if err := rawMerge(pr, doer, mergeStyle, message); err != nil {
  52. return err
  53. }
  54. pr.MergedCommitID, err = baseGitRepo.GetBranchCommitID(pr.BaseBranch)
  55. if err != nil {
  56. return fmt.Errorf("GetBranchCommit: %v", err)
  57. }
  58. pr.MergedUnix = timeutil.TimeStampNow()
  59. pr.Merger = doer
  60. pr.MergerID = doer.ID
  61. if err = pr.SetMerged(); err != nil {
  62. log.Error("setMerged [%d]: %v", pr.ID, err)
  63. }
  64. notification.NotifyMergePullRequest(pr, doer)
  65. // Reset cached commit count
  66. cache.Remove(pr.Issue.Repo.GetCommitsCountCacheKey(pr.BaseBranch, true))
  67. // Resolve cross references
  68. refs, err := pr.ResolveCrossReferences()
  69. if err != nil {
  70. log.Error("ResolveCrossReferences: %v", err)
  71. return nil
  72. }
  73. for _, ref := range refs {
  74. if err = ref.LoadIssue(); err != nil {
  75. return err
  76. }
  77. if err = ref.Issue.LoadRepo(); err != nil {
  78. return err
  79. }
  80. close := (ref.RefAction == references.XRefActionCloses)
  81. if close != ref.Issue.IsClosed {
  82. if err = issue_service.ChangeStatus(ref.Issue, doer, close); err != nil {
  83. return err
  84. }
  85. }
  86. }
  87. return nil
  88. }
  89. // rawMerge perform the merge operation without changing any pull information in database
  90. func rawMerge(pr *models.PullRequest, doer *models.User, mergeStyle models.MergeStyle, message string) (err error) {
  91. binVersion, err := git.BinVersion()
  92. if err != nil {
  93. log.Error("git.BinVersion: %v", err)
  94. return fmt.Errorf("Unable to get git version: %v", err)
  95. }
  96. // Clone base repo.
  97. tmpBasePath, err := createTemporaryRepo(pr)
  98. if err != nil {
  99. log.Error("CreateTemporaryPath: %v", err)
  100. return err
  101. }
  102. defer func() {
  103. if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
  104. log.Error("Merge: RemoveTemporaryPath: %s", err)
  105. }
  106. }()
  107. baseBranch := "base"
  108. trackingBranch := "tracking"
  109. stagingBranch := "staging"
  110. var outbuf, errbuf strings.Builder
  111. // Enable sparse-checkout
  112. sparseCheckoutList, err := getDiffTree(tmpBasePath, baseBranch, trackingBranch)
  113. if err != nil {
  114. log.Error("getDiffTree(%s, %s, %s): %v", tmpBasePath, baseBranch, trackingBranch, err)
  115. return fmt.Errorf("getDiffTree: %v", err)
  116. }
  117. infoPath := filepath.Join(tmpBasePath, ".git", "info")
  118. if err := os.MkdirAll(infoPath, 0700); err != nil {
  119. log.Error("Unable to create .git/info in %s: %v", tmpBasePath, err)
  120. return fmt.Errorf("Unable to create .git/info in tmpBasePath: %v", err)
  121. }
  122. sparseCheckoutListPath := filepath.Join(infoPath, "sparse-checkout")
  123. if err := ioutil.WriteFile(sparseCheckoutListPath, []byte(sparseCheckoutList), 0600); err != nil {
  124. log.Error("Unable to write .git/info/sparse-checkout file in %s: %v", tmpBasePath, err)
  125. return fmt.Errorf("Unable to write .git/info/sparse-checkout file in tmpBasePath: %v", err)
  126. }
  127. var gitConfigCommand func() *git.Command
  128. if version.Compare(binVersion, "1.8.0", ">=") {
  129. gitConfigCommand = func() *git.Command {
  130. return git.NewCommand("config", "--local")
  131. }
  132. } else {
  133. gitConfigCommand = func() *git.Command {
  134. return git.NewCommand("config")
  135. }
  136. }
  137. // Switch off LFS process (set required, clean and smudge here also)
  138. if err := gitConfigCommand().AddArguments("filter.lfs.process", "").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  139. log.Error("git config [filter.lfs.process -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  140. return fmt.Errorf("git config [filter.lfs.process -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  141. }
  142. outbuf.Reset()
  143. errbuf.Reset()
  144. if err := gitConfigCommand().AddArguments("filter.lfs.required", "false").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  145. log.Error("git config [filter.lfs.required -> <false> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  146. return fmt.Errorf("git config [filter.lfs.required -> <false> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  147. }
  148. outbuf.Reset()
  149. errbuf.Reset()
  150. if err := gitConfigCommand().AddArguments("filter.lfs.clean", "").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  151. log.Error("git config [filter.lfs.clean -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  152. return fmt.Errorf("git config [filter.lfs.clean -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  153. }
  154. outbuf.Reset()
  155. errbuf.Reset()
  156. if err := gitConfigCommand().AddArguments("filter.lfs.smudge", "").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  157. log.Error("git config [filter.lfs.smudge -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  158. return fmt.Errorf("git config [filter.lfs.smudge -> <> ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  159. }
  160. outbuf.Reset()
  161. errbuf.Reset()
  162. if err := gitConfigCommand().AddArguments("core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  163. log.Error("git config [core.sparseCheckout -> true ]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  164. return fmt.Errorf("git config [core.sparsecheckout -> true]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  165. }
  166. outbuf.Reset()
  167. errbuf.Reset()
  168. // Read base branch index
  169. if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  170. log.Error("git read-tree HEAD: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  171. return fmt.Errorf("Unable to read base branch in to the index: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
  172. }
  173. outbuf.Reset()
  174. errbuf.Reset()
  175. // Determine if we should sign
  176. signArg := ""
  177. if version.Compare(binVersion, "1.7.9", ">=") {
  178. sign, keyID, _ := pr.SignMerge(doer, tmpBasePath, "HEAD", trackingBranch)
  179. if sign {
  180. signArg = "-S" + keyID
  181. } else if version.Compare(binVersion, "2.0.0", ">=") {
  182. signArg = "--no-gpg-sign"
  183. }
  184. }
  185. sig := doer.NewGitSig()
  186. commitTimeStr := time.Now().Format(time.RFC3339)
  187. // Because this may call hooks we should pass in the environment
  188. env := append(os.Environ(),
  189. "GIT_AUTHOR_NAME="+sig.Name,
  190. "GIT_AUTHOR_EMAIL="+sig.Email,
  191. "GIT_AUTHOR_DATE="+commitTimeStr,
  192. "GIT_COMMITTER_NAME="+sig.Name,
  193. "GIT_COMMITTER_EMAIL="+sig.Email,
  194. "GIT_COMMITTER_DATE="+commitTimeStr,
  195. )
  196. // Merge commits.
  197. switch mergeStyle {
  198. case models.MergeStyleMerge:
  199. cmd := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch)
  200. if err := runMergeCommand(pr, mergeStyle, cmd, tmpBasePath); err != nil {
  201. log.Error("Unable to merge tracking into base: %v", err)
  202. return err
  203. }
  204. if err := commitAndSignNoAuthor(pr, message, signArg, tmpBasePath, env); err != nil {
  205. log.Error("Unable to make final commit: %v", err)
  206. return err
  207. }
  208. case models.MergeStyleRebase:
  209. fallthrough
  210. case models.MergeStyleRebaseMerge:
  211. // Checkout head branch
  212. if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  213. 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())
  214. 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())
  215. }
  216. outbuf.Reset()
  217. errbuf.Reset()
  218. // Rebase before merging
  219. if err := git.NewCommand("rebase", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  220. // Rebase will leave a REBASE_HEAD file in .git if there is a conflict
  221. if _, statErr := os.Stat(filepath.Join(tmpBasePath, ".git", "REBASE_HEAD")); statErr == nil {
  222. // The original commit SHA1 that is failing will be in .git/rebase-apply/original-commit
  223. commitShaBytes, readErr := ioutil.ReadFile(filepath.Join(tmpBasePath, ".git", "rebase-apply", "original-commit"))
  224. if readErr != nil {
  225. // Abandon this attempt to handle the error
  226. 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())
  227. 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())
  228. }
  229. log.Debug("RebaseConflict at %s [%s:%s -> %s:%s]: %v\n%s\n%s", strings.TrimSpace(string(commitShaBytes)), pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
  230. return models.ErrRebaseConflicts{
  231. Style: mergeStyle,
  232. CommitSHA: strings.TrimSpace(string(commitShaBytes)),
  233. StdOut: outbuf.String(),
  234. StdErr: errbuf.String(),
  235. Err: err,
  236. }
  237. }
  238. 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())
  239. 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())
  240. }
  241. outbuf.Reset()
  242. errbuf.Reset()
  243. // Checkout base branch again
  244. if err := git.NewCommand("checkout", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  245. 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())
  246. 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())
  247. }
  248. outbuf.Reset()
  249. errbuf.Reset()
  250. cmd := git.NewCommand("merge")
  251. if mergeStyle == models.MergeStyleRebase {
  252. cmd.AddArguments("--ff-only")
  253. } else {
  254. cmd.AddArguments("--no-ff", "--no-commit")
  255. }
  256. cmd.AddArguments(stagingBranch)
  257. // Prepare merge with commit
  258. if err := runMergeCommand(pr, mergeStyle, cmd, tmpBasePath); err != nil {
  259. log.Error("Unable to merge staging into base: %v", err)
  260. return err
  261. }
  262. if mergeStyle == models.MergeStyleRebaseMerge {
  263. if err := commitAndSignNoAuthor(pr, message, signArg, tmpBasePath, env); err != nil {
  264. log.Error("Unable to make final commit: %v", err)
  265. return err
  266. }
  267. }
  268. case models.MergeStyleSquash:
  269. // Merge with squash
  270. cmd := git.NewCommand("merge", "--squash", trackingBranch)
  271. if err := runMergeCommand(pr, mergeStyle, cmd, tmpBasePath); err != nil {
  272. log.Error("Unable to merge --squash tracking into base: %v", err)
  273. return err
  274. }
  275. sig := pr.Issue.Poster.NewGitSig()
  276. if signArg == "" {
  277. if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
  278. 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())
  279. 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())
  280. }
  281. } else {
  282. 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 {
  283. 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())
  284. 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())
  285. }
  286. }
  287. outbuf.Reset()
  288. errbuf.Reset()
  289. default:
  290. return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: mergeStyle}
  291. }
  292. // OK we should cache our current head and origin/headbranch
  293. mergeHeadSHA, err := git.GetFullCommitID(tmpBasePath, "HEAD")
  294. if err != nil {
  295. return fmt.Errorf("Failed to get full commit id for HEAD: %v", err)
  296. }
  297. mergeBaseSHA, err := git.GetFullCommitID(tmpBasePath, "original_"+baseBranch)
  298. if err != nil {
  299. return fmt.Errorf("Failed to get full commit id for origin/%s: %v", pr.BaseBranch, err)
  300. }
  301. // Now it's questionable about where this should go - either after or before the push
  302. // I think in the interests of data safety - failures to push to the lfs should prevent
  303. // the merge as you can always remerge.
  304. if setting.LFS.StartServer {
  305. if err := LFSPush(tmpBasePath, mergeHeadSHA, mergeBaseSHA, pr); err != nil {
  306. return err
  307. }
  308. }
  309. var headUser *models.User
  310. err = pr.HeadRepo.GetOwner()
  311. if err != nil {
  312. if !models.IsErrUserNotExist(err) {
  313. log.Error("Can't find user: %d for head repository - %v", pr.HeadRepo.OwnerID, err)
  314. return err
  315. }
  316. log.Error("Can't find user: %d for head repository - defaulting to doer: %s - %v", pr.HeadRepo.OwnerID, doer.Name, err)
  317. headUser = doer
  318. } else {
  319. headUser = pr.HeadRepo.Owner
  320. }
  321. env = models.FullPushingEnvironment(
  322. headUser,
  323. doer,
  324. pr.BaseRepo,
  325. pr.BaseRepo.Name,
  326. pr.ID,
  327. )
  328. // Push back to upstream.
  329. if err := git.NewCommand("push", "origin", baseBranch+":"+pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
  330. if strings.Contains(errbuf.String(), "non-fast-forward") {
  331. return models.ErrMergePushOutOfDate{
  332. Style: mergeStyle,
  333. StdOut: outbuf.String(),
  334. StdErr: errbuf.String(),
  335. Err: err,
  336. }
  337. }
  338. return fmt.Errorf("git push: %s", errbuf.String())
  339. }
  340. outbuf.Reset()
  341. errbuf.Reset()
  342. return nil
  343. }
  344. func commitAndSignNoAuthor(pr *models.PullRequest, message, signArg, tmpBasePath string, env []string) error {
  345. var outbuf, errbuf strings.Builder
  346. if signArg == "" {
  347. if err := git.NewCommand("commit", "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
  348. 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())
  349. 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())
  350. }
  351. } else {
  352. if err := git.NewCommand("commit", signArg, "-m", message).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, &outbuf, &errbuf); err != nil {
  353. 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())
  354. 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())
  355. }
  356. }
  357. return nil
  358. }
  359. func runMergeCommand(pr *models.PullRequest, mergeStyle models.MergeStyle, cmd *git.Command, tmpBasePath string) error {
  360. var outbuf, errbuf strings.Builder
  361. if err := cmd.RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
  362. // Merge will leave a MERGE_HEAD file in the .git folder if there is a conflict
  363. if _, statErr := os.Stat(filepath.Join(tmpBasePath, ".git", "MERGE_HEAD")); statErr == nil {
  364. // We have a merge conflict error
  365. 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())
  366. return models.ErrMergeConflicts{
  367. Style: mergeStyle,
  368. StdOut: outbuf.String(),
  369. StdErr: errbuf.String(),
  370. Err: err,
  371. }
  372. } else if strings.Contains(errbuf.String(), "refusing to merge unrelated histories") {
  373. 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())
  374. return models.ErrMergeUnrelatedHistories{
  375. Style: mergeStyle,
  376. StdOut: outbuf.String(),
  377. StdErr: errbuf.String(),
  378. Err: err,
  379. }
  380. }
  381. 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())
  382. 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())
  383. }
  384. return nil
  385. }
  386. var escapedSymbols = regexp.MustCompile(`([*[?! \\])`)
  387. func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
  388. getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
  389. var outbuf, errbuf strings.Builder
  390. // Compute the diff-tree for sparse-checkout
  391. if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
  392. return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
  393. }
  394. return outbuf.String(), nil
  395. }
  396. scanNullTerminatedStrings := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  397. if atEOF && len(data) == 0 {
  398. return 0, nil, nil
  399. }
  400. if i := bytes.IndexByte(data, '\x00'); i >= 0 {
  401. return i + 1, data[0:i], nil
  402. }
  403. if atEOF {
  404. return len(data), data, nil
  405. }
  406. return 0, nil, nil
  407. }
  408. list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
  409. if err != nil {
  410. return "", err
  411. }
  412. // Prefixing '/' for each entry, otherwise all files with the same name in subdirectories would be matched.
  413. out := bytes.Buffer{}
  414. scanner := bufio.NewScanner(strings.NewReader(list))
  415. scanner.Split(scanNullTerminatedStrings)
  416. for scanner.Scan() {
  417. filepath := scanner.Text()
  418. // escape '*', '?', '[', spaces and '!' prefix
  419. filepath = escapedSymbols.ReplaceAllString(filepath, `\$1`)
  420. // no necessary to escape the first '#' symbol because the first symbol is '/'
  421. fmt.Fprintf(&out, "/%s\n", filepath)
  422. }
  423. return out.String(), nil
  424. }
  425. // IsSignedIfRequired check if merge will be signed if required
  426. func IsSignedIfRequired(pr *models.PullRequest, doer *models.User) (bool, error) {
  427. if err := pr.LoadProtectedBranch(); err != nil {
  428. return false, err
  429. }
  430. if pr.ProtectedBranch == nil || !pr.ProtectedBranch.RequireSignedCommits {
  431. return true, nil
  432. }
  433. sign, _, err := pr.SignMerge(doer, pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName())
  434. return sign, err
  435. }
  436. // IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
  437. func IsUserAllowedToMerge(pr *models.PullRequest, p models.Permission, user *models.User) (bool, error) {
  438. if !p.CanWrite(models.UnitTypeCode) {
  439. return false, nil
  440. }
  441. err := pr.LoadProtectedBranch()
  442. if err != nil {
  443. return false, err
  444. }
  445. if pr.ProtectedBranch == nil || pr.ProtectedBranch.IsUserMergeWhitelisted(user.ID) {
  446. return true, nil
  447. }
  448. return false, nil
  449. }
  450. // CheckPRReadyToMerge checks whether the PR is ready to be merged (reviews and status checks)
  451. func CheckPRReadyToMerge(pr *models.PullRequest) (err error) {
  452. if pr.BaseRepo == nil {
  453. if err = pr.GetBaseRepo(); err != nil {
  454. return fmt.Errorf("GetBaseRepo: %v", err)
  455. }
  456. }
  457. if pr.ProtectedBranch == nil {
  458. if err = pr.LoadProtectedBranch(); err != nil {
  459. return fmt.Errorf("LoadProtectedBranch: %v", err)
  460. }
  461. if pr.ProtectedBranch == nil {
  462. return nil
  463. }
  464. }
  465. isPass, err := IsPullCommitStatusPass(pr)
  466. if err != nil {
  467. return err
  468. }
  469. if !isPass {
  470. return models.ErrNotAllowedToMerge{
  471. Reason: "Not all required status checks successful",
  472. }
  473. }
  474. if enoughApprovals := pr.ProtectedBranch.HasEnoughApprovals(pr); !enoughApprovals {
  475. return models.ErrNotAllowedToMerge{
  476. Reason: "Does not have enough approvals",
  477. }
  478. }
  479. if rejected := pr.ProtectedBranch.MergeBlockedByRejectedReview(pr); rejected {
  480. return models.ErrNotAllowedToMerge{
  481. Reason: "There are requested changes",
  482. }
  483. }
  484. return nil
  485. }