Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 middleware
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/Unknwon/macaron"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/git"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. // RepoRef handles repository reference name including those contain `/`.
  17. func RepoRef() macaron.Handler {
  18. return func(ctx *Context) {
  19. var (
  20. refName string
  21. err error
  22. )
  23. // Get default branch.
  24. if len(ctx.Params("*")) == 0 {
  25. refName = ctx.Repo.Repository.DefaultBranch
  26. if !ctx.Repo.GitRepo.IsBranchExist(refName) {
  27. brs, err := ctx.Repo.GitRepo.GetBranches()
  28. if err != nil {
  29. ctx.Handle(500, "GetBranches", err)
  30. return
  31. }
  32. refName = brs[0]
  33. }
  34. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
  35. if err != nil {
  36. ctx.Handle(500, "GetCommitOfBranch", err)
  37. return
  38. }
  39. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  40. ctx.Repo.IsBranch = true
  41. ctx.Repo.BranchName = refName
  42. } else {
  43. hasMatched := false
  44. parts := strings.Split(ctx.Params("*"), "/")
  45. for i, part := range parts {
  46. refName = strings.TrimPrefix(refName+"/"+part, "/")
  47. if ctx.Repo.GitRepo.IsBranchExist(refName) ||
  48. ctx.Repo.GitRepo.IsTagExist(refName) {
  49. if i < len(parts)-1 {
  50. ctx.Repo.TreeName = strings.Join(parts[i+1:], "/")
  51. }
  52. hasMatched = true
  53. break
  54. }
  55. }
  56. if !hasMatched && len(parts[0]) == 40 {
  57. refName = parts[0]
  58. ctx.Repo.TreeName = strings.Join(parts[1:], "/")
  59. }
  60. if ctx.Repo.GitRepo.IsBranchExist(refName) {
  61. ctx.Repo.IsBranch = true
  62. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
  63. if err != nil {
  64. ctx.Handle(500, "GetCommitOfBranch", err)
  65. return
  66. }
  67. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  68. } else if ctx.Repo.GitRepo.IsTagExist(refName) {
  69. ctx.Repo.IsTag = true
  70. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfTag(refName)
  71. if err != nil {
  72. ctx.Handle(500, "GetCommitOfTag", err)
  73. return
  74. }
  75. ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  76. } else if len(refName) == 40 {
  77. ctx.Repo.IsCommit = true
  78. ctx.Repo.CommitId = refName
  79. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
  80. if err != nil {
  81. ctx.Handle(404, "GetCommit", nil)
  82. return
  83. }
  84. } else {
  85. ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  86. return
  87. }
  88. }
  89. ctx.Repo.BranchName = refName
  90. ctx.Data["BranchName"] = ctx.Repo.BranchName
  91. ctx.Data["CommitId"] = ctx.Repo.CommitId
  92. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  93. ctx.Data["IsTag"] = ctx.Repo.IsTag
  94. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  95. ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
  96. if err != nil {
  97. ctx.Handle(500, "CommitsCount", err)
  98. return
  99. }
  100. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  101. }
  102. }
  103. func RepoAssignment(redirect bool, args ...bool) macaron.Handler {
  104. return func(ctx *Context) {
  105. var (
  106. displayBare bool // To display bare page if it is a bare repo.
  107. )
  108. if len(args) >= 1 {
  109. displayBare = args[0]
  110. }
  111. var (
  112. u *models.User
  113. err error
  114. )
  115. userName := ctx.Params(":username")
  116. repoName := ctx.Params(":reponame")
  117. refName := ctx.Params(":branchname")
  118. if len(refName) == 0 {
  119. refName = ctx.Params(":path")
  120. }
  121. // Collaborators who have write access can be seen as owners.
  122. if ctx.IsSigned {
  123. ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.WRITABLE)
  124. if err != nil {
  125. ctx.Handle(500, "HasAccess", err)
  126. return
  127. }
  128. ctx.Repo.IsTrueOwner = ctx.User.LowerName == strings.ToLower(userName)
  129. }
  130. if !ctx.Repo.IsTrueOwner {
  131. u, err = models.GetUserByName(userName)
  132. if err != nil {
  133. if err == models.ErrUserNotExist {
  134. ctx.Handle(404, "GetUserByName", err)
  135. } else if redirect {
  136. log.Error(4, "GetUserByName", err)
  137. ctx.Redirect(setting.AppSubUrl + "/")
  138. } else {
  139. ctx.Handle(500, "GetUserByName", err)
  140. }
  141. return
  142. }
  143. } else {
  144. u = ctx.User
  145. }
  146. if u == nil {
  147. if redirect {
  148. ctx.Redirect(setting.AppSubUrl + "/")
  149. return
  150. }
  151. ctx.Handle(404, "RepoAssignment", errors.New("invliad user account for single repository"))
  152. return
  153. }
  154. ctx.Repo.Owner = u
  155. // Organization owner team members are true owners as well.
  156. if ctx.IsSigned && ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) {
  157. ctx.Repo.IsTrueOwner = true
  158. }
  159. // Get repository.
  160. repo, err := models.GetRepositoryByName(u.Id, repoName)
  161. if err != nil {
  162. if err == models.ErrRepoNotExist {
  163. ctx.Handle(404, "GetRepositoryByName", err)
  164. return
  165. } else if redirect {
  166. ctx.Redirect(setting.AppSubUrl + "/")
  167. return
  168. }
  169. ctx.Handle(500, "GetRepositoryByName", err)
  170. return
  171. } else if err = repo.GetOwner(); err != nil {
  172. ctx.Handle(500, "GetOwner", err)
  173. return
  174. }
  175. // Check if the mirror repository owner(mirror repository doesn't have access).
  176. if ctx.IsSigned && !ctx.Repo.IsOwner {
  177. if repo.OwnerId == ctx.User.Id {
  178. ctx.Repo.IsOwner = true
  179. }
  180. // Check if current user has admin permission to repository.
  181. if u.IsOrganization() {
  182. auth, err := models.GetHighestAuthorize(u.Id, ctx.User.Id, repo.Id, 0)
  183. if err != nil {
  184. ctx.Handle(500, "GetHighestAuthorize", err)
  185. return
  186. }
  187. if auth == models.ORG_ADMIN {
  188. ctx.Repo.IsOwner = true
  189. ctx.Repo.IsAdmin = true
  190. }
  191. }
  192. }
  193. // Check access.
  194. if repo.IsPrivate && !ctx.Repo.IsOwner {
  195. if ctx.User == nil {
  196. ctx.Handle(404, "HasAccess", nil)
  197. return
  198. }
  199. hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.READABLE)
  200. if err != nil {
  201. ctx.Handle(500, "HasAccess", err)
  202. return
  203. } else if !hasAccess {
  204. ctx.Handle(404, "HasAccess", nil)
  205. return
  206. }
  207. }
  208. ctx.Repo.HasAccess = true
  209. ctx.Data["HasAccess"] = true
  210. if repo.IsMirror {
  211. ctx.Repo.Mirror, err = models.GetMirror(repo.Id)
  212. if err != nil {
  213. ctx.Handle(500, "GetMirror", err)
  214. return
  215. }
  216. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  217. }
  218. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  219. repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
  220. ctx.Repo.Repository = repo
  221. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  222. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  223. if err != nil {
  224. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  225. return
  226. }
  227. ctx.Repo.GitRepo = gitRepo
  228. ctx.Repo.RepoLink, err = repo.RepoLink()
  229. if err != nil {
  230. ctx.Handle(500, "RepoLink", err)
  231. return
  232. }
  233. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  234. tags, err := ctx.Repo.GitRepo.GetTags()
  235. if err != nil {
  236. ctx.Handle(500, "GetTags", err)
  237. return
  238. }
  239. ctx.Data["Tags"] = tags
  240. ctx.Repo.Repository.NumTags = len(tags)
  241. // Non-fork repository will not return error in this method.
  242. if err = repo.GetForkRepo(); err != nil {
  243. ctx.Handle(500, "GetForkRepo", err)
  244. return
  245. }
  246. ctx.Data["Title"] = u.Name + "/" + repo.Name
  247. ctx.Data["Repository"] = repo
  248. ctx.Data["Owner"] = ctx.Repo.Repository.Owner
  249. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  250. ctx.Data["IsRepositoryTrueOwner"] = ctx.Repo.IsTrueOwner
  251. if setting.SshPort != 22 {
  252. ctx.Repo.CloneLink.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.Domain, setting.SshPort, u.LowerName, repo.LowerName)
  253. } else {
  254. ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.Domain, u.LowerName, repo.LowerName)
  255. }
  256. ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, u.LowerName, repo.LowerName)
  257. ctx.Data["CloneLink"] = ctx.Repo.CloneLink
  258. if ctx.Repo.Repository.IsGoget {
  259. ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", setting.AppUrl, u.LowerName, repo.LowerName)
  260. ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", setting.Domain, u.LowerName, repo.LowerName)
  261. }
  262. // when repo is bare, not valid branch
  263. // if !ctx.Repo.Repository.IsBare && validBranch {
  264. // detect:
  265. // if len(refName) > 0 {
  266. // if gitRepo.IsBranchExist(refName) {
  267. // ctx.Repo.IsBranch = true
  268. // ctx.Repo.BranchName = refName
  269. // ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(refName)
  270. // if err != nil {
  271. // ctx.Handle(500, "RepoAssignment invalid branch", err)
  272. // return
  273. // }
  274. // ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  275. // } else if gitRepo.IsTagExist(refName) {
  276. // ctx.Repo.IsTag = true
  277. // ctx.Repo.BranchName = refName
  278. // ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfTag(refName)
  279. // if err != nil {
  280. // ctx.Handle(500, "Fail to get tag commit", err)
  281. // return
  282. // }
  283. // ctx.Repo.CommitId = ctx.Repo.Commit.Id.String()
  284. // } else if len(refName) == 40 {
  285. // ctx.Repo.IsCommit = true
  286. // ctx.Repo.CommitId = refName
  287. // ctx.Repo.BranchName = refName
  288. // ctx.Repo.Commit, err = gitRepo.GetCommit(refName)
  289. // if err != nil {
  290. // ctx.Handle(404, "RepoAssignment invalid commit", nil)
  291. // return
  292. // }
  293. // } else {
  294. // ctx.Handle(404, "RepoAssignment invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  295. // return
  296. // }
  297. // } else {
  298. // if len(refName) == 0 {
  299. // if gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  300. // refName = ctx.Repo.Repository.DefaultBranch
  301. // } else {
  302. // brs, err := gitRepo.GetBranches()
  303. // if err != nil {
  304. // ctx.Handle(500, "GetBranches", err)
  305. // return
  306. // }
  307. // refName = brs[0]
  308. // }
  309. // }
  310. // goto detect
  311. // }
  312. // ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  313. // ctx.Data["IsTag"] = ctx.Repo.IsTag
  314. // ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  315. // ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
  316. // if err != nil {
  317. // ctx.Handle(500, "CommitsCount", err)
  318. // return
  319. // }
  320. // ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  321. // }
  322. // repo is bare and display enable
  323. if ctx.Repo.Repository.IsBare {
  324. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  325. if displayBare {
  326. ctx.HTML(200, "repo/bare")
  327. }
  328. return
  329. }
  330. if ctx.IsSigned {
  331. ctx.Data["IsWatchingRepo"] = models.IsWatching(ctx.User.Id, repo.Id)
  332. ctx.Data["IsStaringRepo"] = models.IsStaring(ctx.User.Id, repo.Id)
  333. }
  334. ctx.Data["TagName"] = ctx.Repo.TagName
  335. brs, err := ctx.Repo.GitRepo.GetBranches()
  336. if err != nil {
  337. ctx.Handle(500, "GetBranches", err)
  338. return
  339. }
  340. ctx.Data["Branches"] = brs
  341. ctx.Data["BrancheCount"] = len(brs)
  342. // If not branch selected, try default one.
  343. // If default branch doesn't exists, fall back to some other branch.
  344. if ctx.Repo.BranchName == "" {
  345. if ctx.Repo.Repository.DefaultBranch != "" && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  346. ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
  347. } else if len(brs) > 0 {
  348. ctx.Repo.BranchName = brs[0]
  349. }
  350. }
  351. ctx.Data["BranchName"] = ctx.Repo.BranchName
  352. ctx.Data["CommitId"] = ctx.Repo.CommitId
  353. }
  354. }
  355. func RequireTrueOwner() macaron.Handler {
  356. return func(ctx *Context) {
  357. if !ctx.Repo.IsTrueOwner && !ctx.Repo.IsAdmin {
  358. if !ctx.IsSigned {
  359. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(setting.AppSubUrl+ctx.Req.RequestURI), 0, setting.AppSubUrl)
  360. ctx.Redirect(setting.AppSubUrl + "/user/login")
  361. return
  362. }
  363. ctx.Handle(404, ctx.Req.RequestURI, nil)
  364. return
  365. }
  366. }
  367. }
  368. // GitHookService checks if repsitory Git hooks service has been enabled.
  369. func GitHookService() macaron.Handler {
  370. return func(ctx *Context) {
  371. if !setting.Service.EnableGitHooks {
  372. ctx.Handle(404, "GitHookService", nil)
  373. return
  374. }
  375. }
  376. }