pull.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. )
  22. func getForkRepository(ctx *middleware.Context) *models.Repository {
  23. forkRepo, err := models.GetRepositoryByID(ctx.ParamsInt64(":repoid"))
  24. if err != nil {
  25. if models.IsErrRepoNotExist(err) {
  26. ctx.Handle(404, "GetRepositoryByID", nil)
  27. } else {
  28. ctx.Handle(500, "GetRepositoryByID", err)
  29. }
  30. return nil
  31. }
  32. // Cannot fork bare repo.
  33. if forkRepo.IsBare {
  34. ctx.Handle(404, "", nil)
  35. return nil
  36. }
  37. ctx.Data["repo_name"] = forkRepo.Name
  38. ctx.Data["desc"] = forkRepo.Description
  39. ctx.Data["IsPrivate"] = forkRepo.IsPrivate
  40. if err = forkRepo.GetOwner(); err != nil {
  41. ctx.Handle(500, "GetOwner", err)
  42. return nil
  43. }
  44. ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name
  45. if err := ctx.User.GetOrganizations(); err != nil {
  46. ctx.Handle(500, "GetOrganizations", err)
  47. return nil
  48. }
  49. ctx.Data["Orgs"] = ctx.User.Orgs
  50. return forkRepo
  51. }
  52. func Fork(ctx *middleware.Context) {
  53. ctx.Data["Title"] = ctx.Tr("new_fork")
  54. getForkRepository(ctx)
  55. if ctx.Written() {
  56. return
  57. }
  58. ctx.Data["ContextUser"] = ctx.User
  59. ctx.HTML(200, FORK)
  60. }
  61. func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) {
  62. ctx.Data["Title"] = ctx.Tr("new_fork")
  63. forkRepo := getForkRepository(ctx)
  64. if ctx.Written() {
  65. return
  66. }
  67. ctxUser := checkContextUser(ctx, form.Uid)
  68. if ctx.Written() {
  69. return
  70. }
  71. ctx.Data["ContextUser"] = ctxUser
  72. if ctx.HasError() {
  73. ctx.HTML(200, FORK)
  74. return
  75. }
  76. repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID)
  77. if has {
  78. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  79. return
  80. }
  81. // Check ownership of organization.
  82. if ctxUser.IsOrganization() {
  83. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  84. ctx.Error(403)
  85. return
  86. }
  87. }
  88. repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description)
  89. if err != nil {
  90. ctx.Data["Err_RepoName"] = true
  91. switch {
  92. case models.IsErrRepoAlreadyExist(err):
  93. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form)
  94. case models.IsErrNameReserved(err):
  95. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form)
  96. case models.IsErrNamePatternNotAllowed(err):
  97. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form)
  98. default:
  99. ctx.Handle(500, "ForkPost", err)
  100. }
  101. return
  102. }
  103. log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name)
  104. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  105. }
  106. func Pulls(ctx *middleware.Context) {
  107. ctx.Data["IsRepoToolbarPulls"] = true
  108. ctx.HTML(200, PULLS)
  109. }
  110. // func ViewPull
  111. func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
  112. // Get compare branch information.
  113. infos := strings.Split(ctx.Params("*"), "...")
  114. if len(infos) != 2 {
  115. ctx.Handle(404, "CompareAndPullRequest", nil)
  116. return nil, nil, nil, nil, "", ""
  117. }
  118. baseBranch := infos[0]
  119. ctx.Data["BaseBranch"] = baseBranch
  120. headInfos := strings.Split(infos[1], ":")
  121. if len(headInfos) != 2 {
  122. ctx.Handle(404, "CompareAndPullRequest", nil)
  123. return nil, nil, nil, nil, "", ""
  124. }
  125. headUsername := headInfos[0]
  126. headBranch := headInfos[1]
  127. ctx.Data["HeadBranch"] = headBranch
  128. headUser, err := models.GetUserByName(headUsername)
  129. if err != nil {
  130. if models.IsErrUserNotExist(err) {
  131. ctx.Handle(404, "GetUserByName", nil)
  132. } else {
  133. ctx.Handle(500, "GetUserByName", err)
  134. }
  135. return nil, nil, nil, nil, "", ""
  136. }
  137. repo := ctx.Repo.Repository
  138. // Check if base branch is valid.
  139. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) {
  140. ctx.Handle(404, "IsBranchExist", nil)
  141. return nil, nil, nil, nil, "", ""
  142. }
  143. // Check if current user has fork of repository.
  144. headRepo, has := models.HasForkedRepo(headUser.Id, repo.ID)
  145. if !has || !ctx.User.IsAdminOfRepo(headRepo) {
  146. ctx.Handle(404, "HasForkedRepo", nil)
  147. return nil, nil, nil, nil, "", ""
  148. }
  149. headGitRepo, err := git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  150. if err != nil {
  151. ctx.Handle(500, "OpenRepository", err)
  152. return nil, nil, nil, nil, "", ""
  153. }
  154. // Check if head branch is valid.
  155. if !headGitRepo.IsBranchExist(headBranch) {
  156. ctx.Handle(404, "IsBranchExist", nil)
  157. return nil, nil, nil, nil, "", ""
  158. }
  159. headBranches, err := headGitRepo.GetBranches()
  160. if err != nil {
  161. ctx.Handle(500, "GetBranches", err)
  162. return nil, nil, nil, nil, "", ""
  163. }
  164. ctx.Data["HeadBranches"] = headBranches
  165. prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  166. if err != nil {
  167. ctx.Handle(500, "GetPullRequestInfo", err)
  168. return nil, nil, nil, nil, "", ""
  169. }
  170. ctx.Data["BeforeCommitID"] = prInfo.MergeBase
  171. return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
  172. }
  173. func PrepareCompareDiff(
  174. ctx *middleware.Context,
  175. headUser *models.User,
  176. headRepo *models.Repository,
  177. headGitRepo *git.Repository,
  178. prInfo *git.PullRequestInfo,
  179. baseBranch, headBranch string) {
  180. var (
  181. repo = ctx.Repo.Repository
  182. err error
  183. )
  184. // Get diff information.
  185. ctx.Data["CommitRepoLink"], err = headRepo.RepoLink()
  186. if err != nil {
  187. ctx.Handle(500, "RepoLink", err)
  188. return
  189. }
  190. headCommitID, err := headGitRepo.GetCommitIdOfBranch(headBranch)
  191. if err != nil {
  192. ctx.Handle(500, "GetCommitIdOfBranch", err)
  193. return
  194. }
  195. ctx.Data["AfterCommitID"] = headCommitID
  196. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  197. prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
  198. if err != nil {
  199. ctx.Handle(500, "GetDiffRange", err)
  200. return
  201. }
  202. ctx.Data["Diff"] = diff
  203. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  204. headCommit, err := headGitRepo.GetCommit(headCommitID)
  205. if err != nil {
  206. ctx.Handle(500, "GetCommit", err)
  207. return
  208. }
  209. isImageFile := func(name string) bool {
  210. blob, err := headCommit.GetBlobByPath(name)
  211. if err != nil {
  212. return false
  213. }
  214. dataRc, err := blob.Data()
  215. if err != nil {
  216. return false
  217. }
  218. buf := make([]byte, 1024)
  219. n, _ := dataRc.Read(buf)
  220. if n > 0 {
  221. buf = buf[:n]
  222. }
  223. _, isImage := base.IsImageFile(buf)
  224. return isImage
  225. }
  226. prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
  227. ctx.Data["Commits"] = prInfo.Commits
  228. ctx.Data["CommitCount"] = prInfo.Commits.Len()
  229. ctx.Data["Username"] = headUser.Name
  230. ctx.Data["Reponame"] = headRepo.Name
  231. ctx.Data["IsImageFile"] = isImageFile
  232. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headUser.Name, repo.Name, "src", headCommitID)
  233. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headUser.Name, repo.Name, "src", prInfo.MergeBase)
  234. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headUser.Name, repo.Name, "raw", headCommitID)
  235. }
  236. func CompareAndPullRequest(ctx *middleware.Context) {
  237. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  238. ctx.Data["PageIsComparePull"] = true
  239. ctx.Data["IsDiffCompare"] = true
  240. renderAttachmentSettings(ctx)
  241. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  242. if ctx.Written() {
  243. return
  244. }
  245. PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
  246. if ctx.Written() {
  247. return
  248. }
  249. // Setup information for new form.
  250. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  251. if ctx.Written() {
  252. return
  253. }
  254. ctx.HTML(200, COMPARE_PULL)
  255. }
  256. func CompareAndPullRequestPost(ctx *middleware.Context, form auth.CreateIssueForm) {
  257. ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
  258. ctx.Data["PageIsComparePull"] = true
  259. ctx.Data["IsDiffCompare"] = true
  260. renderAttachmentSettings(ctx)
  261. var (
  262. repo = ctx.Repo.Repository
  263. attachments []string
  264. )
  265. headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  266. if ctx.Written() {
  267. return
  268. }
  269. patch, err := headGitRepo.GetPatch(models.RepoPath(repo.Owner.Name, repo.Name), baseBranch, headBranch)
  270. if err != nil {
  271. ctx.Handle(500, "GetPatch", err)
  272. return
  273. }
  274. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  275. if ctx.Written() {
  276. return
  277. }
  278. if setting.AttachmentEnabled {
  279. attachments = form.Attachments
  280. }
  281. if ctx.HasError() {
  282. ctx.HTML(200, COMPARE_PULL)
  283. return
  284. }
  285. pr := &models.Issue{
  286. RepoID: repo.ID,
  287. Index: int64(repo.NumIssues) + 1,
  288. Name: form.Title,
  289. PosterID: ctx.User.Id,
  290. Poster: ctx.User,
  291. MilestoneID: milestoneID,
  292. AssigneeID: assigneeID,
  293. IsPull: true,
  294. Content: form.Content,
  295. }
  296. if err := models.NewPullRequest(repo, pr, labelIDs, attachments, &models.PullRepo{
  297. HeadRepoID: headRepo.ID,
  298. BaseRepoID: repo.ID,
  299. HeadUserName: headUser.Name,
  300. HeadBarcnh: headBranch,
  301. BaseBranch: baseBranch,
  302. MergeBase: prInfo.MergeBase,
  303. Type: models.PULL_REQUEST_GOGS,
  304. }, patch); err != nil {
  305. ctx.Handle(500, "NewPullRequest", err)
  306. return
  307. }
  308. log.Trace("Pull request created: %d/%d", repo.ID, pr.ID)
  309. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
  310. }