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