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.

pull.go 20 kB

11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
9 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. "container/list"
  7. "fmt"
  8. "path"
  9. "strings"
  10. "code.gitea.io/git"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "github.com/Unknwon/com"
  18. )
  19. const (
  20. tplFork base.TplName = "repo/pulls/fork"
  21. tplComparePull base.TplName = "repo/pulls/compare"
  22. tplPullCommits base.TplName = "repo/pulls/commits"
  23. tplPullFiles base.TplName = "repo/pulls/files"
  24. pullRequestTemplateKey = "PullRequestTemplate"
  25. )
  26. var (
  27. pullRequestTemplateCandidates = []string{
  28. "PULL_REQUEST.md",
  29. ".gogs/PULL_REQUEST.md",
  30. ".github/PULL_REQUEST.md",
  31. }
  32. )
  33. func getForkRepository(ctx *context.Context) *models.Repository {
  34. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  35. if err != nil {
  36. if models.IsErrRepoNotExist(err) {
  37. ctx.Handle(404, "GetRepositoryByID", nil)
  38. } else {
  39. ctx.Handle(500, "GetRepositoryByID", err)
  40. }
  41. return nil
  42. }
  43. if !forkRepo.CanBeForked() {
  44. ctx.Handle(404, "getForkRepository", nil)
  45. return nil
  46. }
  47. ctx.Data["repo_name"] = forkRepo.Name
  48. ctx.Data["description"] = forkRepo.Description
  49. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  50. if err = forkRepo.GetOwner(); err != nil {
  51. ctx.Handle(500, "GetOwner", err)
  52. return nil
  53. }
  54. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  55. if err := ctx.User.GetOrganizations(true); err != nil {
  56. ctx.Handle(500, "GetOrganizations", err)
  57. return nil
  58. }
  59. ctx.Data["Orgs"] = ctx.User.Orgs
  60. return forkRepo
  61. }
  62. // Fork render repository fork page
  63. func Fork(ctx *context.Context) {
  64. ctx.Data["Title"] = ctx.Tr("new_fork")
  65. getForkRepository(ctx)
  66. if ctx.Written() {
  67. return
  68. }
  69. ctx.Data["ContextUser"] = ctx.User
  70. ctx.HTML(200, tplFork)
  71. }
  72. // ForkPost response for forking a repository
  73. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  74. ctx.Data["Title"] = ctx.Tr("new_fork")
  75. forkRepo := getForkRepository(ctx)
  76. if ctx.Written() {
  77. return
  78. }
  79. ctxUser := checkContextUser(ctx, form.UID)
  80. if ctx.Written() {
  81. return
  82. }
  83. ctx.Data["ContextUser"] = ctxUser
  84. if ctx.HasError() {
  85. ctx.HTML(200, tplFork)
  86. return
  87. }
  88. repo, has := models.HasForkedRepo(ctxUser.ID, forkRepo.ID)
  89. if has {
  90. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  91. return
  92. }
  93. // Check ownership of organization.
  94. if ctxUser.IsOrganization() {
  95. if !ctxUser.IsOwnedBy(ctx.User.ID) {
  96. ctx.Error(403)
  97. return
  98. }
  99. }
  100. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  101. if err != nil {
  102. ctx.Data["Err_RepoName"] = true
  103. switch {
  104. case models.IsErrRepoAlreadyExist(err):
  105. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form)
  106. case models.IsErrNameReserved(err):
  107. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplFork, &form)
  108. case models.IsErrNamePatternNotAllowed(err):
  109. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplFork, &form)
  110. default:
  111. ctx.Handle(500, "ForkPost", err)
  112. }
  113. return
  114. }
  115. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  116. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  117. }
  118. func checkPullInfo(ctx *context.Context) *models.Issue {
  119. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  120. if err != nil {
  121. if models.IsErrIssueNotExist(err) {
  122. ctx.Handle(404, "GetIssueByIndex", err)
  123. } else {
  124. ctx.Handle(500, "GetIssueByIndex", err)
  125. }
  126. return nil
  127. }
  128. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  129. ctx.Data["Issue"] = issue
  130. if !issue.IsPull {
  131. ctx.Handle(404, "ViewPullCommits", nil)
  132. return nil
  133. }
  134. if err = issue.PullRequest.GetHeadRepo(); err != nil {
  135. ctx.Handle(500, "GetHeadRepo", err)
  136. return nil
  137. }
  138. if ctx.IsSigned {
  139. // Update issue-user.
  140. if err = issue.ReadBy(ctx.User.ID); err != nil {
  141. ctx.Handle(500, "ReadBy", err)
  142. return nil
  143. }
  144. }
  145. return issue
  146. }
  147. // PrepareMergedViewPullInfo show meta information for a merged pull request view page
  148. func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) {
  149. pull := issue.PullRequest
  150. ctx.Data["HasMerged"] = true
  151. ctx.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
  152. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  153. var err error
  154. ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
  155. if err != nil {
  156. ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
  157. return
  158. }
  159. ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
  160. if err != nil {
  161. ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
  162. return
  163. }
  164. }
  165. // PrepareViewPullInfo show meta information for a pull request preview page
  166. func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
  167. repo := ctx.Repo.Repository
  168. pull := issue.PullRequest
  169. ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
  170. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  171. var (
  172. headGitRepo *git.Repository
  173. err error
  174. )
  175. if err = pull.GetHeadRepo(); err != nil {
  176. ctx.Handle(500, "GetHeadRepo", err)
  177. return nil
  178. }
  179. if pull.HeadRepo != nil {
  180. headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
  181. if err != nil {
  182. ctx.Handle(500, "OpenRepository", err)
  183. return nil
  184. }
  185. }
  186. if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
  187. ctx.Data["IsPullReuqestBroken"] = true
  188. ctx.Data["HeadTarget"] = "deleted"
  189. ctx.Data["NumCommits"] = 0
  190. ctx.Data["NumFiles"] = 0
  191. return nil
  192. }
  193. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  194. pull.BaseBranch, pull.HeadBranch)
  195. if err != nil {
  196. if strings.Contains(err.Error(), "fatal: Not a valid object name") {
  197. ctx.Data["IsPullReuqestBroken"] = true
  198. ctx.Data["BaseTarget"] = "deleted"
  199. ctx.Data["NumCommits"] = 0
  200. ctx.Data["NumFiles"] = 0
  201. return nil
  202. }
  203. ctx.Handle(500, "GetPullRequestInfo", err)
  204. return nil
  205. }
  206. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  207. ctx.Data["NumFiles"] = prInfo.NumFiles
  208. return prInfo
  209. }
  210. // ViewPullCommits show commits for a pull request
  211. func ViewPullCommits(ctx *context.Context) {
  212. ctx.Data["PageIsPullList"] = true
  213. ctx.Data["PageIsPullCommits"] = true
  214. issue := checkPullInfo(ctx)
  215. if ctx.Written() {
  216. return
  217. }
  218. pull := issue.PullRequest
  219. ctx.Data["Username"] = pull.HeadUserName
  220. ctx.Data["Reponame"] = pull.HeadRepo.Name
  221. var commits *list.List
  222. if pull.HasMerged {
  223. PrepareMergedViewPullInfo(ctx, issue)
  224. if ctx.Written() {
  225. return
  226. }
  227. startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
  228. if err != nil {
  229. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  230. return
  231. }
  232. endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
  233. if err != nil {
  234. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  235. return
  236. }
  237. commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
  238. if err != nil {
  239. ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
  240. return
  241. }
  242. } else {
  243. prInfo := PrepareViewPullInfo(ctx, issue)
  244. if ctx.Written() {
  245. return
  246. } else if prInfo == nil {
  247. ctx.Handle(404, "ViewPullCommits", nil)
  248. return
  249. }
  250. commits = prInfo.Commits
  251. }
  252. commits = models.ValidateCommitsWithEmails(commits)
  253. ctx.Data["Commits"] = commits
  254. ctx.Data["CommitCount"] = commits.Len()
  255. ctx.HTML(200, tplPullCommits)
  256. }
  257. // ViewPullFiles render pull request changed files list page
  258. func ViewPullFiles(ctx *context.Context) {
  259. ctx.Data["PageIsPullList"] = true
  260. ctx.Data["PageIsPullFiles"] = true
  261. issue := checkPullInfo(ctx)
  262. if ctx.Written() {
  263. return
  264. }
  265. pull := issue.PullRequest
  266. var (
  267. diffRepoPath string
  268. startCommitID string
  269. endCommitID string
  270. gitRepo *git.Repository
  271. )
  272. if pull.HasMerged {
  273. PrepareMergedViewPullInfo(ctx, issue)
  274. if ctx.Written() {
  275. return
  276. }
  277. diffRepoPath = ctx.Repo.GitRepo.Path
  278. startCommitID = pull.MergeBase
  279. endCommitID = pull.MergedCommitID
  280. gitRepo = ctx.Repo.GitRepo
  281. } else {
  282. prInfo := PrepareViewPullInfo(ctx, issue)
  283. if ctx.Written() {
  284. return
  285. } else if prInfo == nil {
  286. ctx.Handle(404, "ViewPullFiles", nil)
  287. return
  288. }
  289. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  290. headGitRepo, err := git.OpenRepository(headRepoPath)
  291. if err != nil {
  292. ctx.Handle(500, "OpenRepository", err)
  293. return
  294. }
  295. headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
  296. if err != nil {
  297. ctx.Handle(500, "GetBranchCommitID", err)
  298. return
  299. }
  300. diffRepoPath = headRepoPath
  301. startCommitID = prInfo.MergeBase
  302. endCommitID = headCommitID
  303. gitRepo = headGitRepo
  304. }
  305. diff, err := models.GetDiffRange(diffRepoPath,
  306. startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
  307. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  308. if err != nil {
  309. ctx.Handle(500, "GetDiffRange", err)
  310. return
  311. }
  312. ctx.Data["Diff"] = diff
  313. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  314. commit, err := gitRepo.GetCommit(endCommitID)
  315. if err != nil {
  316. ctx.Handle(500, "GetCommit", err)
  317. return
  318. }
  319. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  320. ctx.Data["Username"] = pull.HeadUserName
  321. ctx.Data["Reponame"] = pull.HeadRepo.Name
  322. ctx.Data["IsImageFile"] = commit.IsImageFile
  323. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
  324. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
  325. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
  326. ctx.Data["RequireHighlightJS"] = true
  327. ctx.HTML(200, tplPullFiles)
  328. }
  329. // MergePullRequest response for merging pull request
  330. func MergePullRequest(ctx *context.Context) {
  331. issue := checkPullInfo(ctx)
  332. if ctx.Written() {
  333. return
  334. }
  335. if issue.IsClosed {
  336. ctx.Handle(404, "MergePullRequest", nil)
  337. return
  338. }
  339. pr, err := models.GetPullRequestByIssueID(issue.ID)
  340. if err != nil {
  341. if models.IsErrPullRequestNotExist(err) {
  342. ctx.Handle(404, "GetPullRequestByIssueID", nil)
  343. } else {
  344. ctx.Handle(500, "GetPullRequestByIssueID", err)
  345. }
  346. return
  347. }
  348. if !pr.CanAutoMerge() || pr.HasMerged {
  349. ctx.Handle(404, "MergePullRequest", nil)
  350. return
  351. }
  352. pr.Issue = issue
  353. pr.Issue.Repo = ctx.Repo.Repository
  354. if err = pr.Merge(ctx.User, ctx.Repo.GitRepo); err != nil {
  355. ctx.Handle(500, "Merge", err)
  356. return
  357. }
  358. log.Trace("Pull request merged: %d", pr.ID)
  359. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  360. }
  361. // ParseCompareInfo parse compare info between two commit for preparing pull request
  362. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  363. baseRepo := ctx.Repo.Repository
  364. // Get compared branches information
  365. // format: <base branch>...[<head repo>:]<head branch>
  366. // base<-head: master...head:feature
  367. // same repo: master...feature
  368. infos := strings.Split(ctx.Params("*"), "...")
  369. if len(infos) != 2 {
  370. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  371. ctx.Handle(404, "CompareAndPullRequest", nil)
  372. return nil, nil, nil, nil, "", ""
  373. }
  374. baseBranch := infos[0]
  375. ctx.Data["BaseBranch"] = baseBranch
  376. var (
  377. headUser *models.User
  378. headBranch string
  379. isSameRepo bool
  380. err error
  381. )
  382. // If there is no head repository, it means pull request between same repository.
  383. headInfos := strings.Split(infos[1], ":")
  384. if len(headInfos) == 1 {
  385. isSameRepo = true
  386. headUser = ctx.Repo.Owner
  387. headBranch = headInfos[0]
  388. } else if len(headInfos) == 2 {
  389. headUser, err = models.GetUserByName(headInfos[0])
  390. if err != nil {
  391. if models.IsErrUserNotExist(err) {
  392. ctx.Handle(404, "GetUserByName", nil)
  393. } else {
  394. ctx.Handle(500, "GetUserByName", err)
  395. }
  396. return nil, nil, nil, nil, "", ""
  397. }
  398. headBranch = headInfos[1]
  399. } else {
  400. ctx.Handle(404, "CompareAndPullRequest", nil)
  401. return nil, nil, nil, nil, "", ""
  402. }
  403. ctx.Data["HeadUser"] = headUser
  404. ctx.Data["HeadBranch"] = headBranch
  405. ctx.Repo.PullRequest.SameRepo = isSameRepo
  406. // Check if base branch is valid.
  407. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  408. ctx.Handle(404, "IsBranchExist", nil)
  409. return nil, nil, nil, nil, "", ""
  410. }
  411. // Check if current user has fork of repository or in the same repository.
  412. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  413. if !has && !isSameRepo {
  414. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  415. ctx.Handle(404, "ParseCompareInfo", nil)
  416. return nil, nil, nil, nil, "", ""
  417. }
  418. var headGitRepo *git.Repository
  419. if isSameRepo {
  420. headRepo = ctx.Repo.Repository
  421. headGitRepo = ctx.Repo.GitRepo
  422. } else {
  423. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  424. if err != nil {
  425. ctx.Handle(500, "OpenRepository", err)
  426. return nil, nil, nil, nil, "", ""
  427. }
  428. }
  429. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  430. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  431. ctx.Handle(404, "ParseCompareInfo", nil)
  432. return nil, nil, nil, nil, "", ""
  433. }
  434. // Check if head branch is valid.
  435. if !headGitRepo.IsBranchExist(headBranch) {
  436. ctx.Handle(404, "IsBranchExist", nil)
  437. return nil, nil, nil, nil, "", ""
  438. }
  439. headBranches, err := headGitRepo.GetBranches()
  440. if err != nil {
  441. ctx.Handle(500, "GetBranches", err)
  442. return nil, nil, nil, nil, "", ""
  443. }
  444. ctx.Data["HeadBranches"] = headBranches
  445. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  446. if err != nil {
  447. ctx.Handle(500, "GetPullRequestInfo", err)
  448. return nil, nil, nil, nil, "", ""
  449. }
  450. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  451. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  452. }
  453. // PrepareCompareDiff render pull request preview diff page
  454. func PrepareCompareDiff(
  455. ctx *context.Context,
  456. headUser *models.User,
  457. headRepo *models.Repository,
  458. headGitRepo *git.Repository,
  459. prInfo *git.PullRequestInfo,
  460. baseBranch, headBranch string) bool {
  461. var (
  462. repo = ctx.Repo.Repository
  463. err error
  464. )
  465. // Get diff information.
  466. ctx.Data["CommitRepoLink"] = headRepo.Link()
  467. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  468. if err != nil {
  469. ctx.Handle(500, "GetBranchCommitID", err)
  470. return false
  471. }
  472. ctx.Data["AfterCommitID"] = headCommitID
  473. if headCommitID == prInfo.MergeBase {
  474. ctx.Data["IsNothingToCompare"] = true
  475. return true
  476. }
  477. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  478. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  479. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  480. if err != nil {
  481. ctx.Handle(500, "GetDiffRange", err)
  482. return false
  483. }
  484. ctx.Data["Diff"] = diff
  485. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  486. headCommit, err := headGitRepo.GetCommit(headCommitID)
  487. if err != nil {
  488. ctx.Handle(500, "GetCommit", err)
  489. return false
  490. }
  491. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  492. ctx.Data["Commits"] = prInfo.Commits
  493. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  494. ctx.Data["Username"] = headUser.Name
  495. ctx.Data["Reponame"] = headRepo.Name
  496. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  497. headTarget := path.Join(headUser.Name, repo.Name)
  498. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  499. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  500. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  501. return false
  502. }
  503. // CompareAndPullRequest render pull request preview page
  504. func CompareAndPullRequest(ctx *context.Context) {
  505. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  506. ctx.Data["PageIsComparePull"] = true
  507. ctx.Data["IsDiffCompare"] = true
  508. ctx.Data["RequireHighlightJS"] = true
  509. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  510. renderAttachmentSettings(ctx)
  511. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  512. if ctx.Written() {
  513. return
  514. }
  515. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  516. if err != nil {
  517. if !models.IsErrPullRequestNotExist(err) {
  518. ctx.Handle(500, "GetUnmergedPullRequest", err)
  519. return
  520. }
  521. } else {
  522. ctx.Data["HasPullRequest"] = true
  523. ctx.Data["PullRequest"] = pr
  524. ctx.HTML(200, tplComparePull)
  525. return
  526. }
  527. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  528. if ctx.Written() {
  529. return
  530. }
  531. if !nothingToCompare {
  532. // Setup information for new form.
  533. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  534. if ctx.Written() {
  535. return
  536. }
  537. }
  538. ctx.HTML(200, tplComparePull)
  539. }
  540. // CompareAndPullRequestPost response for creating pull request
  541. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  542. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  543. ctx.Data["PageIsComparePull"] = true
  544. ctx.Data["IsDiffCompare"] = true
  545. ctx.Data["RequireHighlightJS"] = true
  546. renderAttachmentSettings(ctx)
  547. var (
  548. repo = ctx.Repo.Repository
  549. attachments []string
  550. )
  551. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  552. if ctx.Written() {
  553. return
  554. }
  555. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  556. if ctx.Written() {
  557. return
  558. }
  559. if setting.AttachmentEnabled {
  560. attachments = form.Files
  561. }
  562. if ctx.HasError() {
  563. auth.AssignForm(form, ctx.Data)
  564. // This stage is already stop creating new pull request, so it does not matter if it has
  565. // something to compare or not.
  566. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  567. if ctx.Written() {
  568. return
  569. }
  570. ctx.HTML(200, tplComparePull)
  571. return
  572. }
  573. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  574. if err != nil {
  575. ctx.Handle(500, "GetPatch", err)
  576. return
  577. }
  578. pullIssue := &models.Issue{
  579. RepoID: repo.ID,
  580. Index: repo.NextIssueIndex(),
  581. Title: form.Title,
  582. PosterID: ctx.User.ID,
  583. Poster: ctx.User,
  584. MilestoneID: milestoneID,
  585. AssigneeID: assigneeID,
  586. IsPull: true,
  587. Content: form.Content,
  588. }
  589. pullRequest := &models.PullRequest{
  590. HeadRepoID: headRepo.ID,
  591. BaseRepoID: repo.ID,
  592. HeadUserName: headUser.Name,
  593. HeadBranch: headBranch,
  594. BaseBranch: baseBranch,
  595. HeadRepo: headRepo,
  596. BaseRepo: repo,
  597. MergeBase: prInfo.MergeBase,
  598. Type: models.PullRequestGitea,
  599. }
  600. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  601. // instead of 500.
  602. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  603. ctx.Handle(500, "NewPullRequest", err)
  604. return
  605. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  606. ctx.Handle(500, "PushToBaseRepo", err)
  607. return
  608. }
  609. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  610. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  611. }
  612. // TriggerTask response for a trigger task request
  613. func TriggerTask(ctx *context.Context) {
  614. pusherID := ctx.QueryInt64("pusher")
  615. branch := ctx.Query("branch")
  616. secret := ctx.Query("secret")
  617. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  618. ctx.Error(404)
  619. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  620. return
  621. }
  622. owner, repo := parseOwnerAndRepo(ctx)
  623. if ctx.Written() {
  624. return
  625. }
  626. if secret != base.EncodeMD5(owner.Salt) {
  627. ctx.Error(404)
  628. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  629. return
  630. }
  631. pusher, err := models.GetUserByID(pusherID)
  632. if err != nil {
  633. if models.IsErrUserNotExist(err) {
  634. ctx.Error(404)
  635. } else {
  636. ctx.Handle(500, "GetUserByID", err)
  637. }
  638. return
  639. }
  640. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  641. go models.HookQueue.Add(repo.ID)
  642. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  643. ctx.Status(202)
  644. }