pull.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. "path"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/git"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. FORK base.TplName = "repo/pulls/fork"
  19. COMPARE_PULL base.TplName = "repo/pulls/compare"
  20. PULLS base.TplName = "repo/pulls"
  21. PULL_COMMITS base.TplName = "repo/pulls/commits"
  22. PULL_FILES base.TplName = "repo/pulls/files"
  23. )
  24. func getForkRepository(ctx *middleware.Context) *models.Repository {
  25. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  26. if err != nil {
  27. if models.IsErrRepoNotExist(err) {
  28. ctx.Handle(404, "GetRepositoryByID", nil)
  29. } else {
  30. ctx.Handle(500, "GetRepositoryByID", err)
  31. }
  32. return nil
  33. }
  34. // Cannot fork bare repo.
  35. if forkRepo.IsBare {
  36. ctx.Handle(404, "", nil)
  37. return nil
  38. }
  39. ctx.Data["repo_name"] = forkRepo.Name
  40. ctx.Data["desc"] = forkRepo.Description
  41. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  42. if err = forkRepo.GetOwner(); err != nil {
  43. ctx.Handle(500, "GetOwner", err)
  44. return nil
  45. }
  46. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  47. if err := ctx.User.GetOrganizations(); err != nil {
  48. ctx.Handle(500, "GetOrganizations", err)
  49. return nil
  50. }
  51. ctx.Data["Orgs"] = ctx.User.Orgs
  52. return forkRepo
  53. }
  54. func Fork(ctx *middleware.Context) {
  55. ctx.Data["Title"] = ctx.Tr("new_fork")
  56. getForkRepository(ctx)
  57. if ctx.Written() {
  58. return
  59. }
  60. ctx.Data["ContextUser"] = ctx.User
  61. ctx.HTML(200, FORK)
  62. }
  63. func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
  64. ctx.Data["Title"] = ctx.Tr("new_fork")
  65. forkRepo := getForkRepository(ctx)
  66. if ctx.Written() {
  67. return
  68. }
  69. ctxUser := checkContextUser(ctx, form.Uid)
  70. if ctx.Written() {
  71. return
  72. }
  73. ctx.Data["ContextUser"] = ctxUser
  74. if ctx.HasError() {
  75. ctx.HTML(200, FORK)
  76. return
  77. }
  78. repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID)
  79. if has {
  80. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  81. return
  82. }
  83. // Check ownership of organization.
  84. if ctxUser.IsOrganization() {
  85. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  86. ctx.Error(403)
  87. return
  88. }
  89. }
  90. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  91. if err != nil {
  92. ctx.Data["Err_RepoName"] = true
  93. switch {
  94. case models.IsErrRepoAlreadyExist(err):
  95. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  96. case models.IsErrNameReserved(err):
  97. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  98. case models.IsErrNamePatternNotAllowed(err):
  99. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  100. default:
  101. ctx.Handle(500, "ForkPost", err)
  102. }
  103. return
  104. }
  105. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  106. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  107. }
  108. func Pulls(ctx *middleware.Context) {
  109. ctx.Data["IsRepoToolbarPulls"] = true
  110. ctx.HTML(200, PULLS)
  111. }
  112. func checkPullInfo(ctx *middleware.Context) *models.Issue {
  113. pull, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  114. if err != nil {
  115. if models.IsErrIssueNotExist(err) {
  116. ctx.Handle(404, "GetIssueByIndex", err)
  117. } else {
  118. ctx.Handle(500, "GetIssueByIndex", err)
  119. }
  120. return nil
  121. }
  122. ctx.Data["Title"] = pull.Name
  123. ctx.Data["Issue"] = pull
  124. if !pull.IsPull {
  125. ctx.Handle(404, "ViewPullCommits", nil)
  126. return nil
  127. }
  128. if err = pull.GetPoster(); err != nil {
  129. ctx.Handle(500, "GetPoster", err)
  130. return nil
  131. }
  132. if ctx.IsSigned {
  133. // Update issue-user.
  134. if err = pull.ReadBy(ctx.User.Id); err != nil {
  135. ctx.Handle(500, "ReadBy", err)
  136. return nil
  137. }
  138. }
  139. return pull
  140. }
  141. func PrepareViewPullInfo(ctx *middleware.Context, pull *models.Issue) *git.PullRequestInfo {
  142. repo := ctx.Repo.Repository
  143. ctx.Data["HeadTarget"] = pull.PullRepo.HeadUserName + "/" + pull.PullRepo.HeadBarcnh
  144. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.PullRepo.BaseBranch
  145. headRepoPath, err := pull.PullRepo.HeadRepo.RepoPath()
  146. if err != nil {
  147. ctx.Handle(500, "PullRepo.HeadRepo.RepoPath", err)
  148. return nil
  149. }
  150. headGitRepo, err := git.OpenRepository(headRepoPath)
  151. if err != nil {
  152. ctx.Handle(500, "OpenRepository", err)
  153. return nil
  154. }
  155. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
  156. pull.PullRepo.BaseBranch, pull.PullRepo.HeadBarcnh)
  157. if err != nil {
  158. ctx.Handle(500, "GetPullRequestInfo", err)
  159. return nil
  160. }
  161. ctx.Data["NumCommits"] = prInfo.Commits.Len()
  162. ctx.Data["NumFiles"] = prInfo.NumFiles
  163. return prInfo
  164. }
  165. func ViewPullCommits(ctx *middleware.Context) {
  166. ctx.Data["PageIsPullCommits"] = true
  167. pull := checkPullInfo(ctx)
  168. if ctx.Written() {
  169. return
  170. }
  171. prInfo := PrepareViewPullInfo(ctx, pull)
  172. if ctx.Written() {
  173. return
  174. }
  175. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  176. ctx.Data["Commits"] = prInfo.Commits
  177. ctx.HTML(200, PULL_COMMITS)
  178. }
  179. func ViewPullFiles(ctx *middleware.Context) {
  180. ctx.Data["PageIsPullFiles"] = true
  181. pull := checkPullInfo(ctx)
  182. if ctx.Written() {
  183. return
  184. }
  185. prInfo := PrepareViewPullInfo(ctx, pull)
  186. if ctx.Written() {
  187. return
  188. }
  189. _ = prInfo
  190. headRepoPath := models.RepoPath(pull.PullRepo.HeadUserName, pull.PullRepo.HeadRepo.Name)
  191. headGitRepo, err := git.OpenRepository(headRepoPath)
  192. if err != nil {
  193. ctx.Handle(500, "OpenRepository", err)
  194. return
  195. }
  196. headCommitID, err := headGitRepo.GetCommitIdOfBranch(pull.PullRepo.HeadBarcnh)
  197. if err != nil {
  198. ctx.Handle(500, "GetCommitIdOfBranch", err)
  199. return
  200. }
  201. diff, err := models.GetDiffRange(headRepoPath,
  202. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  203. if err != nil {
  204. ctx.Handle(500, "GetDiffRange", err)
  205. return
  206. }
  207. ctx.Data["Diff"] = diff
  208. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  209. headCommit, err := headGitRepo.GetCommit(headCommitID)
  210. if err != nil {
  211. ctx.Handle(500, "GetCommit", err)
  212. return
  213. }
  214. headTarget := path.Join(pull.PullRepo.HeadUserName, pull.PullRepo.HeadRepo.Name)
  215. ctx.Data["Username"] = pull.PullRepo.HeadUserName
  216. ctx.Data["Reponame"] = pull.PullRepo.HeadRepo.Name
  217. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  218. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  219. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  220. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  221. ctx.HTML(200, PULL_FILES)
  222. }
  223. func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  224. // Get compare branch information.
  225. infos := strings.Split(ctx.Params("*"), "...")
  226. if len(infos) != 2 {
  227. ctx.Handle(404, "CompareAndPullRequest", nil)
  228. return nil, nil, nil, nil, "", ""
  229. }
  230. baseBranch := infos[0]
  231. ctx.Data["BaseBranch"] = baseBranch
  232. headInfos := strings.Split(infos[1], ":")
  233. if len(headInfos) != 2 {
  234. ctx.Handle(404, "CompareAndPullRequest", nil)
  235. return nil, nil, nil, nil, "", ""
  236. }
  237. headUsername := headInfos[0]
  238. headBranch := headInfos[1]
  239. ctx.Data["HeadBranch"] = headBranch
  240. headUser, err := models.GetUserByName(headUsername)
  241. if err != nil {
  242. if models.IsErrUserNotExist(err) {
  243. ctx.Handle(404, "GetUserByName", nil)
  244. } else {
  245. ctx.Handle(500, "GetUserByName", err)
  246. }
  247. return nil, nil, nil, nil, "", ""
  248. }
  249. repo := ctx.Repo.Repository
  250. // Check if base branch is valid.
  251. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  252. ctx.Handle(404, "IsBranchExist", nil)
  253. return nil, nil, nil, nil, "", ""
  254. }
  255. // Check if current user has fork of repository.
  256. headRepo, has := models.HasForkedRepo(headUser.Id, repo.ID)
  257. if !has || !ctx.User.IsAdminOfRepo(headRepo) {
  258. ctx.Handle(404, "HasForkedRepo", nil)
  259. return nil, nil, nil, nil, "", ""
  260. }
  261. headGitRepo, err := git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  262. if err != nil {
  263. ctx.Handle(500, "OpenRepository", err)
  264. return nil, nil, nil, nil, "", ""
  265. }
  266. // Check if head branch is valid.
  267. if !headGitRepo.IsBranchExist(headBranch) {
  268. ctx.Handle(404, "IsBranchExist", nil)
  269. return nil, nil, nil, nil, "", ""
  270. }
  271. headBranches, err := headGitRepo.GetBranches()
  272. if err != nil {
  273. ctx.Handle(500, "GetBranches", err)
  274. return nil, nil, nil, nil, "", ""
  275. }
  276. ctx.Data["HeadBranches"] = headBranches
  277. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  278. if err != nil {
  279. ctx.Handle(500, "GetPullRequestInfo", err)
  280. return nil, nil, nil, nil, "", ""
  281. }
  282. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  283. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  284. }
  285. func PrepareCompareDiff(
  286. ctx *middleware.Context,
  287. headUser *models.User,
  288. headRepo *models.Repository,
  289. headGitRepo *git.Repository,
  290. prInfo *git.PullRequestInfo,
  291. baseBranch, headBranch string) {
  292. var (
  293. repo = ctx.Repo.Repository
  294. err error
  295. )
  296. // Get diff information.
  297. ctx.Data["CommitRepoLink"], err = headRepo.RepoLink()
  298. if err != nil {
  299. ctx.Handle(500, "RepoLink", err)
  300. return
  301. }
  302. headCommitID, err := headGitRepo.GetCommitIdOfBranch(headBranch)
  303. if err != nil {
  304. ctx.Handle(500, "GetCommitIdOfBranch", err)
  305. return
  306. }
  307. ctx.Data["AfterCommitID"] = headCommitID
  308. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  309. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  310. if err != nil {
  311. ctx.Handle(500, "GetDiffRange", err)
  312. return
  313. }
  314. ctx.Data["Diff"] = diff
  315. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  316. headCommit, err := headGitRepo.GetCommit(headCommitID)
  317. if err != nil {
  318. ctx.Handle(500, "GetCommit", err)
  319. return
  320. }
  321. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  322. ctx.Data["Commits"] = prInfo.Commits
  323. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  324. ctx.Data["Username"] = headUser.Name
  325. ctx.Data["Reponame"] = headRepo.Name
  326. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  327. headTarget := path.Join(headUser.Name, repo.Name)
  328. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  329. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  330. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  331. }
  332. func CompareAndPullRequest(ctx *middleware.Context) {
  333. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  334. ctx.Data["PageIsComparePull"] = true
  335. ctx.Data["IsDiffCompare"] = true
  336. renderAttachmentSettings(ctx)
  337. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  338. if ctx.Written() {
  339. return
  340. }
  341. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  342. if ctx.Written() {
  343. return
  344. }
  345. // Setup information for new form.
  346. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  347. if ctx.Written() {
  348. return
  349. }
  350. ctx.HTML(200, COMPARE_PULL)
  351. }
  352. func CompareAndPullRequestPost(ctx *middleware.Context, form auth.CreateIssueForm) {
  353. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  354. ctx.Data["PageIsComparePull"] = true
  355. ctx.Data["IsDiffCompare"] = true
  356. renderAttachmentSettings(ctx)
  357. var (
  358. repo = ctx.Repo.Repository
  359. attachments []string
  360. )
  361. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  362. if ctx.Written() {
  363. return
  364. }
  365. patch, err := headGitRepo.GetPatch(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  366. if err != nil {
  367. ctx.Handle(500, "GetPatch", err)
  368. return
  369. }
  370. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  371. if ctx.Written() {
  372. return
  373. }
  374. if setting.AttachmentEnabled {
  375. attachments = form.Attachments
  376. }
  377. if ctx.HasError() {
  378. ctx.HTML(200, COMPARE_PULL)
  379. return
  380. }
  381. pr := &models.Issue{
  382. RepoID: repo.ID,
  383. Index: int64(repo.NumIssues) + 1,
  384. Name: form.Title,
  385. PosterID: ctx.User.Id,
  386. Poster: ctx.User,
  387. MilestoneID: milestoneID,
  388. AssigneeID: assigneeID,
  389. IsPull: true,
  390. Content: form.Content,
  391. }
  392. if err := models.NewPullRequest(repo, pr, labelIDs, attachments, &models.PullRepo{
  393. HeadRepoID: headRepo.ID,
  394. BaseRepoID: repo.ID,
  395. HeadUserName: headUser.Name,
  396. HeadBarcnh: headBranch,
  397. BaseBranch: baseBranch,
  398. MergeBase: prInfo.MergeBase,
  399. Type: models.PULL_REQUEST_GOGS,
  400. }, patch); err != nil {
  401. ctx.Handle(500, "NewPullRequest", err)
  402. return
  403. }
  404. log.Trace("Pull request created: %d/%d", repo.ID, pr.ID)
  405. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  406. }