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 26 kB

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