pull.go 19 KB

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