You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

755 lines
20 KiB

10 years ago
10 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
8 years ago
10 years ago
  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. isSameRepo = headUser.ID == ctx.Repo.Owner.ID
  401. } else {
  402. ctx.Handle(404, "CompareAndPullRequest", nil)
  403. return nil, nil, nil, nil, "", ""
  404. }
  405. ctx.Data["HeadUser"] = headUser
  406. ctx.Data["HeadBranch"] = headBranch
  407. ctx.Repo.PullRequest.SameRepo = isSameRepo
  408. // Check if base branch is valid.
  409. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  410. ctx.Handle(404, "IsBranchExist", nil)
  411. return nil, nil, nil, nil, "", ""
  412. }
  413. // Check if current user has fork of repository or in the same repository.
  414. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  415. if !has && !isSameRepo {
  416. log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
  417. ctx.Handle(404, "ParseCompareInfo", nil)
  418. return nil, nil, nil, nil, "", ""
  419. }
  420. var headGitRepo *git.Repository
  421. if isSameRepo {
  422. headRepo = ctx.Repo.Repository
  423. headGitRepo = ctx.Repo.GitRepo
  424. } else {
  425. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  426. if err != nil {
  427. ctx.Handle(500, "OpenRepository", err)
  428. return nil, nil, nil, nil, "", ""
  429. }
  430. }
  431. if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin {
  432. log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID)
  433. ctx.Handle(404, "ParseCompareInfo", nil)
  434. return nil, nil, nil, nil, "", ""
  435. }
  436. // Check if head branch is valid.
  437. if !headGitRepo.IsBranchExist(headBranch) {
  438. ctx.Handle(404, "IsBranchExist", nil)
  439. return nil, nil, nil, nil, "", ""
  440. }
  441. headBranches, err := headGitRepo.GetBranches()
  442. if err != nil {
  443. ctx.Handle(500, "GetBranches", err)
  444. return nil, nil, nil, nil, "", ""
  445. }
  446. ctx.Data["HeadBranches"] = headBranches
  447. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  448. if err != nil {
  449. ctx.Handle(500, "GetPullRequestInfo", err)
  450. return nil, nil, nil, nil, "", ""
  451. }
  452. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  453. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  454. }
  455. // PrepareCompareDiff render pull request preview diff page
  456. func PrepareCompareDiff(
  457. ctx *context.Context,
  458. headUser *models.User,
  459. headRepo *models.Repository,
  460. headGitRepo *git.Repository,
  461. prInfo *git.PullRequestInfo,
  462. baseBranch, headBranch string) bool {
  463. var (
  464. repo = ctx.Repo.Repository
  465. err error
  466. )
  467. // Get diff information.
  468. ctx.Data["CommitRepoLink"] = headRepo.Link()
  469. headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
  470. if err != nil {
  471. ctx.Handle(500, "GetBranchCommitID", err)
  472. return false
  473. }
  474. ctx.Data["AfterCommitID"] = headCommitID
  475. if headCommitID == prInfo.MergeBase {
  476. ctx.Data["IsNothingToCompare"] = true
  477. return true
  478. }
  479. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  480. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  481. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  482. if err != nil {
  483. ctx.Handle(500, "GetDiffRange", err)
  484. return false
  485. }
  486. ctx.Data["Diff"] = diff
  487. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  488. headCommit, err := headGitRepo.GetCommit(headCommitID)
  489. if err != nil {
  490. ctx.Handle(500, "GetCommit", err)
  491. return false
  492. }
  493. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  494. ctx.Data["Commits"] = prInfo.Commits
  495. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  496. ctx.Data["Username"] = headUser.Name
  497. ctx.Data["Reponame"] = headRepo.Name
  498. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  499. headTarget := path.Join(headUser.Name, repo.Name)
  500. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", headCommitID)
  501. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  502. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", headCommitID)
  503. return false
  504. }
  505. // CompareAndPullRequest render pull request preview page
  506. func CompareAndPullRequest(ctx *context.Context) {
  507. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  508. ctx.Data["PageIsComparePull"] = true
  509. ctx.Data["IsDiffCompare"] = true
  510. ctx.Data["RequireHighlightJS"] = true
  511. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  512. renderAttachmentSettings(ctx)
  513. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  514. if ctx.Written() {
  515. return
  516. }
  517. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  518. if err != nil {
  519. if !models.IsErrPullRequestNotExist(err) {
  520. ctx.Handle(500, "GetUnmergedPullRequest", err)
  521. return
  522. }
  523. } else {
  524. ctx.Data["HasPullRequest"] = true
  525. ctx.Data["PullRequest"] = pr
  526. ctx.HTML(200, tplComparePull)
  527. return
  528. }
  529. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  530. if ctx.Written() {
  531. return
  532. }
  533. if !nothingToCompare {
  534. // Setup information for new form.
  535. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  536. if ctx.Written() {
  537. return
  538. }
  539. }
  540. ctx.HTML(200, tplComparePull)
  541. }
  542. // CompareAndPullRequestPost response for creating pull request
  543. func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm) {
  544. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  545. ctx.Data["PageIsComparePull"] = true
  546. ctx.Data["IsDiffCompare"] = true
  547. ctx.Data["RequireHighlightJS"] = true
  548. renderAttachmentSettings(ctx)
  549. var (
  550. repo = ctx.Repo.Repository
  551. attachments []string
  552. )
  553. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  554. if ctx.Written() {
  555. return
  556. }
  557. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  558. if ctx.Written() {
  559. return
  560. }
  561. if setting.AttachmentEnabled {
  562. attachments = form.Files
  563. }
  564. if ctx.HasError() {
  565. auth.AssignForm(form, ctx.Data)
  566. // This stage is already stop creating new pull request, so it does not matter if it has
  567. // something to compare or not.
  568. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  569. if ctx.Written() {
  570. return
  571. }
  572. ctx.HTML(200, tplComparePull)
  573. return
  574. }
  575. patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
  576. if err != nil {
  577. ctx.Handle(500, "GetPatch", err)
  578. return
  579. }
  580. pullIssue := &models.Issue{
  581. RepoID: repo.ID,
  582. Index: repo.NextIssueIndex(),
  583. Title: form.Title,
  584. PosterID: ctx.User.ID,
  585. Poster: ctx.User,
  586. MilestoneID: milestoneID,
  587. AssigneeID: assigneeID,
  588. IsPull: true,
  589. Content: form.Content,
  590. }
  591. pullRequest := &models.PullRequest{
  592. HeadRepoID: headRepo.ID,
  593. BaseRepoID: repo.ID,
  594. HeadUserName: headUser.Name,
  595. HeadBranch: headBranch,
  596. BaseBranch: baseBranch,
  597. HeadRepo: headRepo,
  598. BaseRepo: repo,
  599. MergeBase: prInfo.MergeBase,
  600. Type: models.PullRequestGitea,
  601. }
  602. // FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
  603. // instead of 500.
  604. if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
  605. ctx.Handle(500, "NewPullRequest", err)
  606. return
  607. } else if err := pullRequest.PushToBaseRepo(); err != nil {
  608. ctx.Handle(500, "PushToBaseRepo", err)
  609. return
  610. }
  611. notification.Service.NotifyIssue(pullIssue, ctx.User.ID)
  612. log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
  613. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
  614. }
  615. // TriggerTask response for a trigger task request
  616. func TriggerTask(ctx *context.Context) {
  617. pusherID := ctx.QueryInt64("pusher")
  618. branch := ctx.Query("branch")
  619. secret := ctx.Query("secret")
  620. if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
  621. ctx.Error(404)
  622. log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
  623. return
  624. }
  625. owner, repo := parseOwnerAndRepo(ctx)
  626. if ctx.Written() {
  627. return
  628. }
  629. if secret != base.EncodeMD5(owner.Salt) {
  630. ctx.Error(404)
  631. log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
  632. return
  633. }
  634. pusher, err := models.GetUserByID(pusherID)
  635. if err != nil {
  636. if models.IsErrUserNotExist(err) {
  637. ctx.Error(404)
  638. } else {
  639. ctx.Handle(500, "GetUserByID", err)
  640. }
  641. return
  642. }
  643. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  644. go models.HookQueue.Add(repo.ID)
  645. go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  646. ctx.Status(202)
  647. }