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 21 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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/notification"
  17. "code.gitea.io/gitea/modules/setting"
  18. "github.com/Unknwon/com"
  19. )
  20. const (
  21. tplFork base.TplName = "repo/pulls/fork"
  22. tplComparePull base.TplName = "repo/pulls/compare"
  23. tplPullCommits base.TplName = "repo/pulls/commits"
  24. tplPullFiles base.TplName = "repo/pulls/files"
  25. pullRequestTemplateKey = "PullRequestTemplate"
  26. )
  27. var (
  28. pullRequestTemplateCandidates = []string{
  29. "PULL_REQUEST_TEMPLATE.md",
  30. "pull_request_template.md",
  31. ".gitea/PULL_REQUEST_TEMPLATE.md",
  32. ".gitea/pull_request_template.md",
  33. ".github/PULL_REQUEST_TEMPLATE.md",
  34. ".github/pull_request_template.md",
  35. }
  36. )
  37. func getForkRepository(ctx *context.Context) *models.Repository {
  38. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  39. if err != nil {
  40. if models.IsErrRepoNotExist(err) {
  41. ctx.Handle(404, "GetRepositoryByID", nil)
  42. } else {
  43. ctx.Handle(500, "GetRepositoryByID", err)
  44. }
  45. return nil
  46. }
  47. if !forkRepo.CanBeForked() || !forkRepo.HasAccess(ctx.User) {
  48. ctx.Handle(404, "getForkRepository", nil)
  49. return nil
  50. }
  51. ctx.Data["repo_name"] = forkRepo.Name
  52. ctx.Data["description"] = forkRepo.Description
  53. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  54. if err = forkRepo.GetOwner(); err != nil {
  55. ctx.Handle(500, "GetOwner", err)
  56. return nil
  57. }
  58. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  59. ctx.Data["ForkFromOwnerID"] = forkRepo.Owner.ID
  60. if err := ctx.User.GetOrganizations(true); err != nil {
  61. ctx.Handle(500, "GetOrganizations", err)
  62. return nil
  63. }
  64. ctx.Data["Orgs"] = ctx.User.Orgs
  65. return forkRepo
  66. }
  67. // Fork render repository fork page
  68. func Fork(ctx *context.Context) {
  69. ctx.Data["Title"] = ctx.Tr("new_fork")
  70. getForkRepository(ctx)
  71. if ctx.Written() {
  72. return
  73. }
  74. ctx.Data["ContextUser"] = ctx.User
  75. ctx.HTML(200, tplFork)
  76. }
  77. // ForkPost response for forking a repository
  78. func ForkPost(ctx *context.Context, form auth.CreateRepoForm) {
  79. ctx.Data["Title"] = ctx.Tr("new_fork")
  80. forkRepo := getForkRepository(ctx)
  81. if ctx.Written() {
  82. return
  83. }
  84. ctxUser := checkContextUser(ctx, form.UID)
  85. if ctx.Written() {
  86. return
  87. }
  88. ctx.Data["ContextUser"] = ctxUser
  89. if ctx.HasError() {
  90. ctx.HTML(200, tplFork)
  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. notification.Service.NotifyIssue(pr.Issue, ctx.User.ID)
  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. notification.Service.NotifyIssue(pullIssue, ctx.User.ID)
  611. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  612. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  613. }
  614. // TriggerTask response for a trigger task request
  615. func TriggerTask(ctx *context.Context) {
  616. pusherID := ctx.QueryInt64("pusher")
  617. branch := ctx.Query("branch")
  618. secret := ctx.Query("secret")
  619. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  620. ctx.Error(404)
  621. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  622. return
  623. }
  624. owner, repo := parseOwnerAndRepo(ctx)
  625. if ctx.Written() {
  626. return
  627. }
  628. if secret != base.EncodeMD5(owner.Salt) {
  629. ctx.Error(404)
  630. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  631. return
  632. }
  633. pusher, err := models.GetUserByID(pusherID)
  634. if err != nil {
  635. if models.IsErrUserNotExist(err) {
  636. ctx.Error(404)
  637. } else {
  638. ctx.Handle(500, "GetUserByID", err)
  639. }
  640. return
  641. }
  642. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  643. go models.HookQueue.Add(repo.ID)
  644. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  645. ctx.Status(202)
  646. }