pull.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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.HeadUserName + "/" + pull.HeadBarcnh
  144. ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
  145. headRepoPath, err := pull.HeadRepo.RepoPath()
  146. if err != nil {
  147. ctx.Handle(500, "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.BaseBranch, pull.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.Data["CommitCount"] = prInfo.Commits.Len()
  178. ctx.Data["Username"] = pull.HeadUserName
  179. ctx.Data["Reponame"] = pull.HeadRepo.Name
  180. ctx.HTML(200, PULL_COMMITS)
  181. }
  182. func ViewPullFiles(ctx *middleware.Context) {
  183. ctx.Data["PageIsPullFiles"] = true
  184. pull := checkPullInfo(ctx)
  185. if ctx.Written() {
  186. return
  187. }
  188. prInfo := PrepareViewPullInfo(ctx, pull)
  189. if ctx.Written() {
  190. return
  191. }
  192. headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
  193. headGitRepo, err := git.OpenRepository(headRepoPath)
  194. if err != nil {
  195. ctx.Handle(500, "OpenRepository", err)
  196. return
  197. }
  198. headCommitID, err := headGitRepo.GetCommitIdOfBranch(pull.HeadBarcnh)
  199. if err != nil {
  200. ctx.Handle(500, "GetCommitIdOfBranch", err)
  201. return
  202. }
  203. diff, err := models.GetDiffRange(headRepoPath,
  204. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  205. if err != nil {
  206. ctx.Handle(500, "GetDiffRange", err)
  207. return
  208. }
  209. ctx.Data["Diff"] = diff
  210. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  211. headCommit, err := headGitRepo.GetCommit(headCommitID)
  212. if err != nil {
  213. ctx.Handle(500, "GetCommit", err)
  214. return
  215. }
  216. headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
  217. ctx.Data["Username"] = pull.HeadUserName
  218. ctx.Data["Reponame"] = pull.HeadRepo.Name
  219. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  220. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  221. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  222. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  223. ctx.HTML(200, PULL_FILES)
  224. }
  225. func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  226. // Get compare branch information.
  227. infos := strings.Split(ctx.Params("*"), "...")
  228. if len(infos) != 2 {
  229. ctx.Handle(404, "CompareAndPullRequest", nil)
  230. return nil, nil, nil, nil, "", ""
  231. }
  232. baseBranch := infos[0]
  233. ctx.Data["BaseBranch"] = baseBranch
  234. headInfos := strings.Split(infos[1], ":")
  235. if len(headInfos) != 2 {
  236. ctx.Handle(404, "CompareAndPullRequest", nil)
  237. return nil, nil, nil, nil, "", ""
  238. }
  239. headUsername := headInfos[0]
  240. headBranch := headInfos[1]
  241. ctx.Data["HeadBranch"] = headBranch
  242. headUser, err := models.GetUserByName(headUsername)
  243. if err != nil {
  244. if models.IsErrUserNotExist(err) {
  245. ctx.Handle(404, "GetUserByName", nil)
  246. } else {
  247. ctx.Handle(500, "GetUserByName", err)
  248. }
  249. return nil, nil, nil, nil, "", ""
  250. }
  251. repo := ctx.Repo.Repository
  252. // Check if base branch is valid.
  253. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  254. ctx.Handle(404, "IsBranchExist", nil)
  255. return nil, nil, nil, nil, "", ""
  256. }
  257. // Check if current user has fork of repository.
  258. headRepo, has := models.HasForkedRepo(headUser.Id, repo.ID)
  259. if !has || !ctx.User.IsAdminOfRepo(headRepo) {
  260. ctx.Handle(404, "HasForkedRepo", nil)
  261. return nil, nil, nil, nil, "", ""
  262. }
  263. headGitRepo, err := git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  264. if err != nil {
  265. ctx.Handle(500, "OpenRepository", err)
  266. return nil, nil, nil, nil, "", ""
  267. }
  268. // Check if head branch is valid.
  269. if !headGitRepo.IsBranchExist(headBranch) {
  270. ctx.Handle(404, "IsBranchExist", nil)
  271. return nil, nil, nil, nil, "", ""
  272. }
  273. headBranches, err := headGitRepo.GetBranches()
  274. if err != nil {
  275. ctx.Handle(500, "GetBranches", err)
  276. return nil, nil, nil, nil, "", ""
  277. }
  278. ctx.Data["HeadBranches"] = headBranches
  279. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  280. if err != nil {
  281. ctx.Handle(500, "GetPullRequestInfo", err)
  282. return nil, nil, nil, nil, "", ""
  283. }
  284. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  285. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  286. }
  287. func PrepareCompareDiff(
  288. ctx *middleware.Context,
  289. headUser *models.User,
  290. headRepo *models.Repository,
  291. headGitRepo *git.Repository,
  292. prInfo *git.PullRequestInfo,
  293. baseBranch, headBranch string) bool {
  294. var (
  295. repo = ctx.Repo.Repository
  296. err error
  297. )
  298. // Get diff information.
  299. ctx.Data["CommitRepoLink"], err = headRepo.RepoLink()
  300. if err != nil {
  301. ctx.Handle(500, "RepoLink", err)
  302. return false
  303. }
  304. headCommitID, err := headGitRepo.GetCommitIdOfBranch(headBranch)
  305. if err != nil {
  306. ctx.Handle(500, "GetCommitIdOfBranch", err)
  307. return false
  308. }
  309. ctx.Data["AfterCommitID"] = headCommitID
  310. if headCommitID == prInfo.MergeBase {
  311. ctx.Data["IsNothingToCompare"] = true
  312. return true
  313. }
  314. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  315. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  316. if err != nil {
  317. ctx.Handle(500, "GetDiffRange", err)
  318. return false
  319. }
  320. ctx.Data["Diff"] = diff
  321. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  322. headCommit, err := headGitRepo.GetCommit(headCommitID)
  323. if err != nil {
  324. ctx.Handle(500, "GetCommit", err)
  325. return false
  326. }
  327. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  328. ctx.Data["Commits"] = prInfo.Commits
  329. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  330. ctx.Data["Username"] = headUser.Name
  331. ctx.Data["Reponame"] = headRepo.Name
  332. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  333. headTarget := path.Join(headUser.Name, repo.Name)
  334. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
  335. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
  336. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
  337. return false
  338. }
  339. func CompareAndPullRequest(ctx *middleware.Context) {
  340. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  341. ctx.Data["PageIsComparePull"] = true
  342. ctx.Data["IsDiffCompare"] = true
  343. renderAttachmentSettings(ctx)
  344. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  345. if ctx.Written() {
  346. return
  347. }
  348. pr, err := models.GetPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  349. if err != nil {
  350. if !models.IsErrPullRequestNotExist(err) {
  351. ctx.Handle(500, "HasPullRequest", err)
  352. return
  353. }
  354. } else {
  355. ctx.Data["HasPullRequest"] = true
  356. ctx.Data["PullRequest"] = pr
  357. ctx.HTML(200, COMPARE_PULL)
  358. return
  359. }
  360. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  361. if ctx.Written() {
  362. return
  363. }
  364. if !nothingToCompare {
  365. // Setup information for new form.
  366. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  367. if ctx.Written() {
  368. return
  369. }
  370. }
  371. ctx.HTML(200, COMPARE_PULL)
  372. }
  373. func CompareAndPullRequestPost(ctx *middleware.Context, form auth.CreateIssueForm) {
  374. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  375. ctx.Data["PageIsComparePull"] = true
  376. ctx.Data["IsDiffCompare"] = true
  377. renderAttachmentSettings(ctx)
  378. var (
  379. repo = ctx.Repo.Repository
  380. attachments []string
  381. )
  382. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  383. if ctx.Written() {
  384. return
  385. }
  386. patch, err := headGitRepo.GetPatch(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  387. if err != nil {
  388. ctx.Handle(500, "GetPatch", err)
  389. return
  390. }
  391. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  392. if ctx.Written() {
  393. return
  394. }
  395. if setting.AttachmentEnabled {
  396. attachments = form.Attachments
  397. }
  398. if ctx.HasError() {
  399. ctx.HTML(200, COMPARE_PULL)
  400. return
  401. }
  402. pull := &models.Issue{
  403. RepoID: repo.ID,
  404. Index: int64(repo.NumIssues) + 1,
  405. Name: form.Title,
  406. PosterID: ctx.User.Id,
  407. Poster: ctx.User,
  408. MilestoneID: milestoneID,
  409. AssigneeID: assigneeID,
  410. IsPull: true,
  411. Content: form.Content,
  412. }
  413. if err := models.NewPullRequest(repo, pull, labelIDs, attachments, &models.PullRequest{
  414. HeadRepoID: headRepo.ID,
  415. BaseRepoID: repo.ID,
  416. HeadUserName: headUser.Name,
  417. HeadBarcnh: headBranch,
  418. BaseBranch: baseBranch,
  419. MergeBase: prInfo.MergeBase,
  420. Type: models.PULL_REQUEST_GOGS,
  421. }, patch); err != nil {
  422. ctx.Handle(500, "NewPullRequest", err)
  423. return
  424. }
  425. log.Trace("Pull request created: %d/%d", repo.ID, pull.ID)
  426. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pull.Index))
  427. }