pull.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "net/http"
  7. "path"
  8. "strings"
  9. "github.com/unknwon/com"
  10. log "unknwon.dev/clog/v2"
  11. "github.com/gogs/git-module"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/context"
  14. "gogs.io/gogs/internal/db"
  15. "gogs.io/gogs/internal/form"
  16. "gogs.io/gogs/internal/gitutil"
  17. )
  18. const (
  19. FORK = "repo/pulls/fork"
  20. COMPARE_PULL = "repo/pulls/compare"
  21. PULL_COMMITS = "repo/pulls/commits"
  22. PULL_FILES = "repo/pulls/files"
  23. PULL_REQUEST_TEMPLATE_KEY = "PullRequestTemplate"
  24. PULL_REQUEST_TITLE_TEMPLATE_KEY = "PullRequestTitleTemplate"
  25. )
  26. var (
  27. PullRequestTemplateCandidates = []string{
  28. "PULL_REQUEST.md",
  29. ".gogs/PULL_REQUEST.md",
  30. ".github/PULL_REQUEST.md",
  31. }
  32. PullRequestTitleTemplateCandidates = []string{
  33. "PULL_REQUEST_TITLE.md",
  34. ".gogs/PULL_REQUEST_TITLE.md",
  35. ".github/PULL_REQUEST_TITLE.md",
  36. }
  37. )
  38. func parseBaseRepository(c *context.Context) *db.Repository {
  39. baseRepo, err := db.GetRepositoryByID(c.ParamsInt64(":repoid"))
  40. if err != nil {
  41. c.NotFoundOrError(err, "get repository by ID")
  42. return nil
  43. }
  44. if !baseRepo.CanBeForked() || !baseRepo.HasAccess(c.User.ID) {
  45. c.NotFound()
  46. return nil
  47. }
  48. c.Data["repo_name"] = baseRepo.Name
  49. c.Data["description"] = baseRepo.Description
  50. c.Data["IsPrivate"] = baseRepo.IsPrivate
  51. c.Data["IsUnlisted"] = baseRepo.IsUnlisted
  52. if err = baseRepo.GetOwner(); err != nil {
  53. c.Error(err, "get owner")
  54. return nil
  55. }
  56. c.Data["ForkFrom"] = baseRepo.Owner.Name + "/" + baseRepo.Name
  57. if err := c.User.GetOrganizations(true); err != nil {
  58. c.Error(err, "get organizations")
  59. return nil
  60. }
  61. c.Data["Orgs"] = c.User.Orgs
  62. return baseRepo
  63. }
  64. func Fork(c *context.Context) {
  65. c.Data["Title"] = c.Tr("new_fork")
  66. parseBaseRepository(c)
  67. if c.Written() {
  68. return
  69. }
  70. c.Data["ContextUser"] = c.User
  71. c.Success(FORK)
  72. }
  73. func ForkPost(c *context.Context, f form.CreateRepo) {
  74. c.Data["Title"] = c.Tr("new_fork")
  75. baseRepo := parseBaseRepository(c)
  76. if c.Written() {
  77. return
  78. }
  79. ctxUser := checkContextUser(c, f.UserID)
  80. if c.Written() {
  81. return
  82. }
  83. c.Data["ContextUser"] = ctxUser
  84. if c.HasError() {
  85. c.Success(FORK)
  86. return
  87. }
  88. repo, has, err := db.HasForkedRepo(ctxUser.ID, baseRepo.ID)
  89. if err != nil {
  90. c.Error(err, "check forked repository")
  91. return
  92. } else if has {
  93. c.Redirect(repo.Link())
  94. return
  95. }
  96. // Check ownership of organization.
  97. if ctxUser.IsOrganization() && !ctxUser.IsOwnedBy(c.User.ID) {
  98. c.Status(http.StatusForbidden)
  99. return
  100. }
  101. // Cannot fork to same owner
  102. if ctxUser.ID == baseRepo.OwnerID {
  103. c.RenderWithErr(c.Tr("repo.settings.cannot_fork_to_same_owner"), FORK, &f)
  104. return
  105. }
  106. repo, err = db.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description)
  107. if err != nil {
  108. c.Data["Err_RepoName"] = true
  109. switch {
  110. case db.IsErrReachLimitOfRepo(err):
  111. c.RenderWithErr(c.Tr("repo.form.reach_limit_of_creation", c.User.RepoCreationNum()), FORK, &f)
  112. case db.IsErrRepoAlreadyExist(err):
  113. c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), FORK, &f)
  114. case db.IsErrNameNotAllowed(err):
  115. c.RenderWithErr(c.Tr("repo.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()), FORK, &f)
  116. default:
  117. c.Error(err, "fork repository")
  118. }
  119. return
  120. }
  121. log.Trace("Repository forked from '%s' -> '%s'", baseRepo.FullName(), repo.FullName())
  122. c.Redirect(repo.Link())
  123. }
  124. func checkPullInfo(c *context.Context) *db.Issue {
  125. issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  126. if err != nil {
  127. c.NotFoundOrError(err, "get issue by index")
  128. return nil
  129. }
  130. c.Data["Title"] = issue.Title
  131. c.Data["Issue"] = issue
  132. if !issue.IsPull {
  133. c.NotFound()
  134. return nil
  135. }
  136. if c.IsLogged {
  137. // Update issue-user.
  138. if err = issue.ReadBy(c.User.ID); err != nil {
  139. c.Error(err, "mark read by")
  140. return nil
  141. }
  142. }
  143. return issue
  144. }
  145. func PrepareMergedViewPullInfo(c *context.Context, issue *db.Issue) {
  146. pull := issue.PullRequest
  147. c.Data["HasMerged"] = true
  148. c.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
  149. c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
  150. var err error
  151. c.Data["NumCommits"], err = c.Repo.GitRepo.RevListCount([]string{pull.MergeBase + "..." + pull.MergedCommitID})
  152. if err != nil {
  153. c.Error(err, "count commits")
  154. return
  155. }
  156. names, err := c.Repo.GitRepo.DiffNameOnly(pull.MergeBase, pull.MergedCommitID, git.DiffNameOnlyOptions{NeedsMergeBase: true})
  157. c.Data["NumFiles"] = len(names)
  158. if err != nil {
  159. c.Error(err, "get changed files")
  160. return
  161. }
  162. }
  163. func PrepareViewPullInfo(c *context.Context, issue *db.Issue) *gitutil.PullRequestMeta {
  164. repo := c.Repo.Repository
  165. pull := issue.PullRequest
  166. c.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  167. c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
  168. var (
  169. headGitRepo *git.Repository
  170. err error
  171. )
  172. if pull.HeadRepo != nil {
  173. headGitRepo, err = git.Open(pull.HeadRepo.RepoPath())
  174. if err != nil {
  175. c.Error(err, "open repository")
  176. return nil
  177. }
  178. }
  179. if pull.HeadRepo == nil || !headGitRepo.HasBranch(pull.HeadBranch) {
  180. c.Data["IsPullReuqestBroken"] = true
  181. c.Data["HeadTarget"] = "deleted"
  182. c.Data["NumCommits"] = 0
  183. c.Data["NumFiles"] = 0
  184. return nil
  185. }
  186. baseRepoPath := db.RepoPath(repo.Owner.Name, repo.Name)
  187. prMeta, err := gitutil.Module.PullRequestMeta(headGitRepo.Path(), baseRepoPath, pull.HeadBranch, pull.BaseBranch)
  188. if err != nil {
  189. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  190. c.Data["IsPullReuqestBroken"] = true
  191. c.Data["BaseTarget"] = "deleted"
  192. c.Data["NumCommits"] = 0
  193. c.Data["NumFiles"] = 0
  194. return nil
  195. }
  196. c.Error(err, "get pull request meta")
  197. return nil
  198. }
  199. c.Data["NumCommits"] = len(prMeta.Commits)
  200. c.Data["NumFiles"] = prMeta.NumFiles
  201. return prMeta
  202. }
  203. func ViewPullCommits(c *context.Context) {
  204. c.Data["PageIsPullList"] = true
  205. c.Data["PageIsPullCommits"] = true
  206. issue := checkPullInfo(c)
  207. if c.Written() {
  208. return
  209. }
  210. pull := issue.PullRequest
  211. if pull.HeadRepo != nil {
  212. c.Data["Username"] = pull.HeadUserName
  213. c.Data["Reponame"] = pull.HeadRepo.Name
  214. }
  215. var commits []*git.Commit
  216. if pull.HasMerged {
  217. PrepareMergedViewPullInfo(c, issue)
  218. if c.Written() {
  219. return
  220. }
  221. startCommit, err := c.Repo.GitRepo.CatFileCommit(pull.MergeBase)
  222. if err != nil {
  223. c.Error(err, "get commit of merge base")
  224. return
  225. }
  226. endCommit, err := c.Repo.GitRepo.CatFileCommit(pull.MergedCommitID)
  227. if err != nil {
  228. c.Error(err, "get merged commit")
  229. return
  230. }
  231. commits, err = c.Repo.GitRepo.RevList([]string{startCommit.ID.String() + "..." + endCommit.ID.String()})
  232. if err != nil {
  233. c.Error(err, "list commits")
  234. return
  235. }
  236. } else {
  237. prInfo := PrepareViewPullInfo(c, issue)
  238. if c.Written() {
  239. return
  240. } else if prInfo == nil {
  241. c.NotFound()
  242. return
  243. }
  244. commits = prInfo.Commits
  245. }
  246. c.Data["Commits"] = db.ValidateCommitsWithEmails(commits)
  247. c.Data["CommitsCount"] = len(commits)
  248. c.Success(PULL_COMMITS)
  249. }
  250. func ViewPullFiles(c *context.Context) {
  251. c.Data["PageIsPullList"] = true
  252. c.Data["PageIsPullFiles"] = true
  253. issue := checkPullInfo(c)
  254. if c.Written() {
  255. return
  256. }
  257. pull := issue.PullRequest
  258. var (
  259. diffGitRepo *git.Repository
  260. startCommitID string
  261. endCommitID string
  262. gitRepo *git.Repository
  263. )
  264. if pull.HasMerged {
  265. PrepareMergedViewPullInfo(c, issue)
  266. if c.Written() {
  267. return
  268. }
  269. diffGitRepo = c.Repo.GitRepo
  270. startCommitID = pull.MergeBase
  271. endCommitID = pull.MergedCommitID
  272. gitRepo = c.Repo.GitRepo
  273. } else {
  274. prInfo := PrepareViewPullInfo(c, issue)
  275. if c.Written() {
  276. return
  277. } else if prInfo == nil {
  278. c.NotFound()
  279. return
  280. }
  281. headRepoPath := db.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  282. headGitRepo, err := git.Open(headRepoPath)
  283. if err != nil {
  284. c.Error(err, "open repository")
  285. return
  286. }
  287. headCommitID, err := headGitRepo.BranchCommitID(pull.HeadBranch)
  288. if err != nil {
  289. c.Error(err, "get head branch commit ID")
  290. return
  291. }
  292. diffGitRepo = headGitRepo
  293. startCommitID = prInfo.MergeBase
  294. endCommitID = headCommitID
  295. gitRepo = headGitRepo
  296. }
  297. diff, err := gitutil.RepoDiff(diffGitRepo,
  298. endCommitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  299. git.DiffOptions{Base: startCommitID},
  300. )
  301. if err != nil {
  302. c.Error(err, "get diff")
  303. return
  304. }
  305. c.Data["Diff"] = diff
  306. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  307. commit, err := gitRepo.CatFileCommit(endCommitID)
  308. if err != nil {
  309. c.Error(err, "get commit")
  310. return
  311. }
  312. setEditorconfigIfExists(c)
  313. if c.Written() {
  314. return
  315. }
  316. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  317. c.Data["IsImageFile"] = commit.IsImageFile
  318. c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
  319. // It is possible head repo has been deleted for merged pull requests
  320. if pull.HeadRepo != nil {
  321. c.Data["Username"] = pull.HeadUserName
  322. c.Data["Reponame"] = pull.HeadRepo.Name
  323. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  324. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", endCommitID)
  325. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", endCommitID)
  326. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", startCommitID)
  327. c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", startCommitID)
  328. }
  329. c.Data["RequireHighlightJS"] = true
  330. c.Success(PULL_FILES)
  331. }
  332. func MergePullRequest(c *context.Context) {
  333. issue := checkPullInfo(c)
  334. if c.Written() {
  335. return
  336. }
  337. if issue.IsClosed {
  338. c.NotFound()
  339. return
  340. }
  341. pr, err := db.GetPullRequestByIssueID(issue.ID)
  342. if err != nil {
  343. c.NotFoundOrError(err, "get pull request by issue ID")
  344. return
  345. }
  346. if !pr.CanAutoMerge() || pr.HasMerged {
  347. c.NotFound()
  348. return
  349. }
  350. pr.Issue = issue
  351. pr.Issue.Repo = c.Repo.Repository
  352. if err = pr.Merge(c.User, c.Repo.GitRepo, db.MergeStyle(c.Query("merge_style")), c.Query("commit_description")); err != nil {
  353. c.Error(err, "merge")
  354. return
  355. }
  356. log.Trace("Pull request merged: %d", pr.ID)
  357. c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  358. }
  359. func ParseCompareInfo(c *context.Context) (*db.User, *db.Repository, *git.Repository, *gitutil.PullRequestMeta, string, string) {
  360. baseRepo := c.Repo.Repository
  361. // Get compared branches information
  362. // format: <base branch>...[<head repo>:]<head branch>
  363. // base<-head: master...head:feature
  364. // same repo: master...feature
  365. infos := strings.Split(c.Params("*"), "...")
  366. if len(infos) != 2 {
  367. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  368. c.NotFound()
  369. return nil, nil, nil, nil, "", ""
  370. }
  371. baseBranch := infos[0]
  372. c.Data["BaseBranch"] = baseBranch
  373. var (
  374. headUser *db.User
  375. headBranch string
  376. isSameRepo bool
  377. err error
  378. )
  379. // If there is no head repository, it means pull request between same repository.
  380. headInfos := strings.Split(infos[1], ":")
  381. if len(headInfos) == 1 {
  382. isSameRepo = true
  383. headUser = c.Repo.Owner
  384. headBranch = headInfos[0]
  385. } else if len(headInfos) == 2 {
  386. headUser, err = db.GetUserByName(headInfos[0])
  387. if err != nil {
  388. c.NotFoundOrError(err, "get user by name")
  389. return nil, nil, nil, nil, "", ""
  390. }
  391. headBranch = headInfos[1]
  392. isSameRepo = headUser.ID == baseRepo.OwnerID
  393. } else {
  394. c.NotFound()
  395. return nil, nil, nil, nil, "", ""
  396. }
  397. c.Data["HeadUser"] = headUser
  398. c.Data["HeadBranch"] = headBranch
  399. c.Repo.PullRequest.SameRepo = isSameRepo
  400. // Check if base branch is valid.
  401. if !c.Repo.GitRepo.HasBranch(baseBranch) {
  402. c.NotFound()
  403. return nil, nil, nil, nil, "", ""
  404. }
  405. var (
  406. headRepo *db.Repository
  407. headGitRepo *git.Repository
  408. )
  409. // In case user included redundant head user name for comparison in same repository,
  410. // no need to check the fork relation.
  411. if !isSameRepo {
  412. var has bool
  413. headRepo, has, err = db.HasForkedRepo(headUser.ID, baseRepo.ID)
  414. if err != nil {
  415. c.Error(err, "get forked repository")
  416. return nil, nil, nil, nil, "", ""
  417. } else if !has {
  418. log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have fork or in same repository", baseRepo.ID)
  419. c.NotFound()
  420. return nil, nil, nil, nil, "", ""
  421. }
  422. headGitRepo, err = git.Open(db.RepoPath(headUser.Name, headRepo.Name))
  423. if err != nil {
  424. c.Error(err, "open repository")
  425. return nil, nil, nil, nil, "", ""
  426. }
  427. } else {
  428. headRepo = c.Repo.Repository
  429. headGitRepo = c.Repo.GitRepo
  430. }
  431. if !c.User.IsWriterOfRepo(headRepo) && !c.User.IsAdmin {
  432. log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have write access or site admin", baseRepo.ID)
  433. c.NotFound()
  434. return nil, nil, nil, nil, "", ""
  435. }
  436. // Check if head branch is valid.
  437. if !headGitRepo.HasBranch(headBranch) {
  438. c.NotFound()
  439. return nil, nil, nil, nil, "", ""
  440. }
  441. headBranches, err := headGitRepo.Branches()
  442. if err != nil {
  443. c.Error(err, "get branches")
  444. return nil, nil, nil, nil, "", ""
  445. }
  446. c.Data["HeadBranches"] = headBranches
  447. baseRepoPath := db.RepoPath(baseRepo.Owner.Name, baseRepo.Name)
  448. meta, err := gitutil.Module.PullRequestMeta(headGitRepo.Path(), baseRepoPath, headBranch, baseBranch)
  449. if err != nil {
  450. if gitutil.IsErrNoMergeBase(err) {
  451. c.Data["IsNoMergeBase"] = true
  452. c.Success(COMPARE_PULL)
  453. } else {
  454. c.Error(err, "get pull request meta")
  455. }
  456. return nil, nil, nil, nil, "", ""
  457. }
  458. c.Data["BeforeCommitID"] = meta.MergeBase
  459. return headUser, headRepo, headGitRepo, meta, baseBranch, headBranch
  460. }
  461. func PrepareCompareDiff(
  462. c *context.Context,
  463. headUser *db.User,
  464. headRepo *db.Repository,
  465. headGitRepo *git.Repository,
  466. meta *gitutil.PullRequestMeta,
  467. headBranch string,
  468. ) bool {
  469. var (
  470. repo = c.Repo.Repository
  471. err error
  472. )
  473. // Get diff information.
  474. c.Data["CommitRepoLink"] = headRepo.Link()
  475. headCommitID, err := headGitRepo.BranchCommitID(headBranch)
  476. if err != nil {
  477. c.Error(err, "get head branch commit ID")
  478. return false
  479. }
  480. c.Data["AfterCommitID"] = headCommitID
  481. if headCommitID == meta.MergeBase {
  482. c.Data["IsNothingToCompare"] = true
  483. return true
  484. }
  485. diff, err := gitutil.RepoDiff(headGitRepo,
  486. headCommitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  487. git.DiffOptions{Base: meta.MergeBase},
  488. )
  489. if err != nil {
  490. c.Error(err, "get repository diff")
  491. return false
  492. }
  493. c.Data["Diff"] = diff
  494. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  495. headCommit, err := headGitRepo.CatFileCommit(headCommitID)
  496. if err != nil {
  497. c.Error(err, "get head commit")
  498. return false
  499. }
  500. c.Data["Commits"] = db.ValidateCommitsWithEmails(meta.Commits)
  501. c.Data["CommitCount"] = len(meta.Commits)
  502. c.Data["Username"] = headUser.Name
  503. c.Data["Reponame"] = headRepo.Name
  504. c.Data["IsImageFile"] = headCommit.IsImageFile
  505. c.Data["IsImageFileByIndex"] = headCommit.IsImageFileByIndex
  506. headTarget := path.Join(headUser.Name, repo.Name)
  507. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", headCommitID)
  508. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", headCommitID)
  509. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", meta.MergeBase)
  510. c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", meta.MergeBase)
  511. return false
  512. }
  513. func CompareAndPullRequest(c *context.Context) {
  514. c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
  515. c.Data["PageIsComparePull"] = true
  516. c.Data["IsDiffCompare"] = true
  517. c.Data["RequireHighlightJS"] = true
  518. setTemplateIfExists(c, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
  519. renderAttachmentSettings(c)
  520. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
  521. if c.Written() {
  522. return
  523. }
  524. pr, err := db.GetUnmergedPullRequest(headRepo.ID, c.Repo.Repository.ID, headBranch, baseBranch)
  525. if err != nil {
  526. if !db.IsErrPullRequestNotExist(err) {
  527. c.Error(err, "get unmerged pull request")
  528. return
  529. }
  530. } else {
  531. c.Data["HasPullRequest"] = true
  532. c.Data["PullRequest"] = pr
  533. c.Success(COMPARE_PULL)
  534. return
  535. }
  536. nothingToCompare := PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, headBranch)
  537. if c.Written() {
  538. return
  539. }
  540. if !nothingToCompare {
  541. // Setup information for new form.
  542. RetrieveRepoMetas(c, c.Repo.Repository)
  543. if c.Written() {
  544. return
  545. }
  546. }
  547. setEditorconfigIfExists(c)
  548. if c.Written() {
  549. return
  550. }
  551. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  552. setTemplateIfExists(c, PULL_REQUEST_TITLE_TEMPLATE_KEY, PullRequestTitleTemplateCandidates)
  553. if c.Data[PULL_REQUEST_TITLE_TEMPLATE_KEY] != nil {
  554. customTitle := c.Data[PULL_REQUEST_TITLE_TEMPLATE_KEY].(string)
  555. r := strings.NewReplacer("{{headBranch}}", headBranch, "{{baseBranch}}", baseBranch)
  556. c.Data["title"] = r.Replace(customTitle)
  557. }
  558. c.Success(COMPARE_PULL)
  559. }
  560. func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
  561. c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
  562. c.Data["PageIsComparePull"] = true
  563. c.Data["IsDiffCompare"] = true
  564. c.Data["RequireHighlightJS"] = true
  565. renderAttachmentSettings(c)
  566. var (
  567. repo = c.Repo.Repository
  568. attachments []string
  569. )
  570. headUser, headRepo, headGitRepo, meta, baseBranch, headBranch := ParseCompareInfo(c)
  571. if c.Written() {
  572. return
  573. }
  574. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(c, f)
  575. if c.Written() {
  576. return
  577. }
  578. if conf.Attachment.Enabled {
  579. attachments = f.Files
  580. }
  581. if c.HasError() {
  582. form.Assign(f, c.Data)
  583. // This stage is already stop creating new pull request, so it does not matter if it has
  584. // something to compare or not.
  585. PrepareCompareDiff(c, headUser, headRepo, headGitRepo, meta, headBranch)
  586. if c.Written() {
  587. return
  588. }
  589. c.Success(COMPARE_PULL)
  590. return
  591. }
  592. patch, err := headGitRepo.DiffBinary(meta.MergeBase, headBranch)
  593. if err != nil {
  594. c.Error(err, "get patch")
  595. return
  596. }
  597. pullIssue := &db.Issue{
  598. RepoID: repo.ID,
  599. Index: repo.NextIssueIndex(),
  600. Title: f.Title,
  601. PosterID: c.User.ID,
  602. Poster: c.User,
  603. MilestoneID: milestoneID,
  604. AssigneeID: assigneeID,
  605. IsPull: true,
  606. Content: f.Content,
  607. }
  608. pullRequest := &db.PullRequest{
  609. HeadRepoID: headRepo.ID,
  610. BaseRepoID: repo.ID,
  611. HeadUserName: headUser.Name,
  612. HeadBranch: headBranch,
  613. BaseBranch: baseBranch,
  614. HeadRepo: headRepo,
  615. BaseRepo: repo,
  616. MergeBase: meta.MergeBase,
  617. Type: db.PULL_REQUEST_GOGS,
  618. }
  619. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  620. // instead of 500.
  621. if err := db.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  622. c.Error(err, "new pull request")
  623. return
  624. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  625. c.Error(err, "push to base repository")
  626. return
  627. }
  628. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  629. c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  630. }