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

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