repo.go 11 KB

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. "fmt"
  7. "path"
  8. "strings"
  9. "gopkg.in/macaron.v1"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. func ApiRepoAssignment() macaron.Handler {
  16. return func(ctx *Context) {
  17. ctx.Repo = &RepoContext{}
  18. userName := ctx.Params(":username")
  19. repoName := ctx.Params(":reponame")
  20. var (
  21. owner *models.User
  22. err error
  23. )
  24. // Check if the user is the same as the repository owner.
  25. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  26. owner = ctx.User
  27. } else {
  28. owner, err = models.GetUserByName(userName)
  29. if err != nil {
  30. if models.IsErrUserNotExist(err) {
  31. ctx.Error(404)
  32. } else {
  33. ctx.APIError(500, "GetUserByName", err)
  34. }
  35. return
  36. }
  37. }
  38. ctx.Repo.Owner = owner
  39. // Get repository.
  40. repo, err := models.GetRepositoryByName(owner.Id, repoName)
  41. if err != nil {
  42. if models.IsErrRepoNotExist(err) {
  43. ctx.Error(404)
  44. } else {
  45. ctx.APIError(500, "GetRepositoryByName", err)
  46. }
  47. return
  48. } else if err = repo.GetOwner(); err != nil {
  49. ctx.APIError(500, "GetOwner", err)
  50. return
  51. }
  52. mode, err := models.AccessLevel(ctx.User, repo)
  53. if err != nil {
  54. ctx.APIError(500, "AccessLevel", err)
  55. return
  56. }
  57. ctx.Repo.AccessMode = mode
  58. // Check access.
  59. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
  60. ctx.Error(404)
  61. return
  62. }
  63. ctx.Repo.Repository = repo
  64. }
  65. }
  66. // RepoRef handles repository reference name including those contain `/`.
  67. func RepoRef() macaron.Handler {
  68. return func(ctx *Context) {
  69. // Empty repository does not have reference information.
  70. if ctx.Repo.Repository.IsBare {
  71. return
  72. }
  73. var (
  74. refName string
  75. err error
  76. )
  77. // For API calls.
  78. if ctx.Repo.GitRepo == nil {
  79. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  80. gitRepo, err := git.OpenRepository(repoPath)
  81. if err != nil {
  82. ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
  83. return
  84. }
  85. ctx.Repo.GitRepo = gitRepo
  86. }
  87. // Get default branch.
  88. if len(ctx.Params("*")) == 0 {
  89. refName = ctx.Repo.Repository.DefaultBranch
  90. if !ctx.Repo.GitRepo.IsBranchExist(refName) {
  91. brs, err := ctx.Repo.GitRepo.GetBranches()
  92. if err != nil {
  93. ctx.Handle(500, "GetBranches", err)
  94. return
  95. }
  96. refName = brs[0]
  97. }
  98. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
  99. if err != nil {
  100. ctx.Handle(500, "GetCommitOfBranch", err)
  101. return
  102. }
  103. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  104. ctx.Repo.IsBranch = true
  105. } else {
  106. hasMatched := false
  107. parts := strings.Split(ctx.Params("*"), "/")
  108. for i, part := range parts {
  109. refName = strings.TrimPrefix(refName+"/"+part, "/")
  110. if ctx.Repo.GitRepo.IsBranchExist(refName) ||
  111. ctx.Repo.GitRepo.IsTagExist(refName) {
  112. if i < len(parts)-1 {
  113. ctx.Repo.TreeName = strings.Join(parts[i+1:], "/")
  114. }
  115. hasMatched = true
  116. break
  117. }
  118. }
  119. if !hasMatched && len(parts[0]) == 40 {
  120. refName = parts[0]
  121. ctx.Repo.TreeName = strings.Join(parts[1:], "/")
  122. }
  123. if ctx.Repo.GitRepo.IsBranchExist(refName) {
  124. ctx.Repo.IsBranch = true
  125. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
  126. if err != nil {
  127. ctx.Handle(500, "GetCommitOfBranch", err)
  128. return
  129. }
  130. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  131. } else if ctx.Repo.GitRepo.IsTagExist(refName) {
  132. ctx.Repo.IsTag = true
  133. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfTag(refName)
  134. if err != nil {
  135. ctx.Handle(500, "GetCommitOfTag", err)
  136. return
  137. }
  138. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  139. } else if len(refName) == 40 {
  140. ctx.Repo.IsCommit = true
  141. ctx.Repo.CommitID = refName
  142. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
  143. if err != nil {
  144. ctx.Handle(404, "GetCommit", nil)
  145. return
  146. }
  147. } else {
  148. ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  149. return
  150. }
  151. }
  152. ctx.Repo.BranchName = refName
  153. ctx.Data["BranchName"] = ctx.Repo.BranchName
  154. ctx.Data["CommitID"] = ctx.Repo.CommitID
  155. ctx.Data["IsBranch"] = ctx.Repo.IsBranch
  156. ctx.Data["IsTag"] = ctx.Repo.IsTag
  157. ctx.Data["IsCommit"] = ctx.Repo.IsCommit
  158. ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
  159. if err != nil {
  160. ctx.Handle(500, "CommitsCount", err)
  161. return
  162. }
  163. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  164. }
  165. }
  166. func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
  167. // Non-fork repository will not return error in this method.
  168. if err := repo.GetBaseRepo(); err != nil {
  169. if models.IsErrRepoNotExist(err) {
  170. repo.IsFork = false
  171. repo.ForkID = 0
  172. return
  173. }
  174. ctx.Handle(500, "GetBaseRepo", err)
  175. return
  176. } else if err = repo.BaseRepo.GetOwner(); err != nil {
  177. ctx.Handle(500, "BaseRepo.GetOwner", err)
  178. return
  179. }
  180. bsaeRepo := repo.BaseRepo
  181. baseGitRepo, err := git.OpenRepository(models.RepoPath(bsaeRepo.Owner.Name, bsaeRepo.Name))
  182. if err != nil {
  183. ctx.Handle(500, "OpenRepository", err)
  184. return
  185. }
  186. if len(bsaeRepo.DefaultBranch) > 0 && baseGitRepo.IsBranchExist(bsaeRepo.DefaultBranch) {
  187. ctx.Data["BaseDefaultBranch"] = bsaeRepo.DefaultBranch
  188. } else {
  189. baseBranches, err := baseGitRepo.GetBranches()
  190. if err != nil {
  191. ctx.Handle(500, "GetBranches", err)
  192. return
  193. }
  194. if len(baseBranches) > 0 {
  195. ctx.Data["BaseDefaultBranch"] = baseBranches[0]
  196. }
  197. }
  198. }
  199. func RepoAssignment(args ...bool) macaron.Handler {
  200. return func(ctx *Context) {
  201. ctx.Repo = &RepoContext{}
  202. var (
  203. displayBare bool // To display bare page if it is a bare repo.
  204. )
  205. if len(args) >= 1 {
  206. displayBare = args[0]
  207. }
  208. var (
  209. owner *models.User
  210. err error
  211. )
  212. userName := ctx.Params(":username")
  213. repoName := ctx.Params(":reponame")
  214. refName := ctx.Params(":branchname")
  215. if len(refName) == 0 {
  216. refName = ctx.Params(":path")
  217. }
  218. // Check if the user is the same as the repository owner
  219. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  220. owner = ctx.User
  221. } else {
  222. owner, err = models.GetUserByName(userName)
  223. if err != nil {
  224. if models.IsErrUserNotExist(err) {
  225. ctx.Handle(404, "GetUserByName", err)
  226. } else {
  227. ctx.Handle(500, "GetUserByName", err)
  228. }
  229. return
  230. }
  231. }
  232. ctx.Repo.Owner = owner
  233. // Get repository.
  234. repo, err := models.GetRepositoryByName(owner.Id, repoName)
  235. if err != nil {
  236. if models.IsErrRepoNotExist(err) {
  237. ctx.Handle(404, "GetRepositoryByName", err)
  238. } else {
  239. ctx.Handle(500, "GetRepositoryByName", err)
  240. }
  241. return
  242. } else if err = repo.GetOwner(); err != nil {
  243. ctx.Handle(500, "GetOwner", err)
  244. return
  245. }
  246. // Admin has super access.
  247. if ctx.IsSigned && ctx.User.IsAdmin {
  248. ctx.Repo.AccessMode = models.ACCESS_MODE_OWNER
  249. } else {
  250. mode, err := models.AccessLevel(ctx.User, repo)
  251. if err != nil {
  252. ctx.Handle(500, "AccessLevel", err)
  253. return
  254. }
  255. ctx.Repo.AccessMode = mode
  256. }
  257. // Check access.
  258. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
  259. ctx.Handle(404, "no access right", err)
  260. return
  261. }
  262. ctx.Data["HasAccess"] = true
  263. if repo.IsMirror {
  264. ctx.Repo.Mirror, err = models.GetMirror(repo.ID)
  265. if err != nil {
  266. ctx.Handle(500, "GetMirror", err)
  267. return
  268. }
  269. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  270. }
  271. ctx.Repo.Repository = repo
  272. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  273. gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
  274. if err != nil {
  275. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
  276. return
  277. }
  278. ctx.Repo.GitRepo = gitRepo
  279. ctx.Repo.RepoLink = repo.RepoLink()
  280. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  281. ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
  282. tags, err := ctx.Repo.GitRepo.GetTags()
  283. if err != nil {
  284. ctx.Handle(500, "GetTags", err)
  285. return
  286. }
  287. ctx.Data["Tags"] = tags
  288. ctx.Repo.Repository.NumTags = len(tags)
  289. if repo.IsFork {
  290. RetrieveBaseRepo(ctx, repo)
  291. if ctx.Written() {
  292. return
  293. }
  294. }
  295. ctx.Data["Title"] = owner.Name + "/" + repo.Name
  296. ctx.Data["Repository"] = repo
  297. ctx.Data["Owner"] = ctx.Repo.Repository.Owner
  298. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
  299. ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
  300. ctx.Data["IsRepositoryPusher"] = ctx.Repo.IsPusher()
  301. ctx.Data["DisableSSH"] = setting.DisableSSH
  302. ctx.Data["CloneLink"] = repo.CloneLink()
  303. ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
  304. if ctx.IsSigned {
  305. ctx.Data["IsWatchingRepo"] = models.IsWatching(ctx.User.Id, repo.ID)
  306. ctx.Data["IsStaringRepo"] = models.IsStaring(ctx.User.Id, repo.ID)
  307. }
  308. // repo is bare and display enable
  309. if ctx.Repo.Repository.IsBare {
  310. log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
  311. // NOTE: to prevent templating error
  312. ctx.Data["BranchName"] = ""
  313. if displayBare {
  314. if !ctx.Repo.IsAdmin() {
  315. ctx.Flash.Info(ctx.Tr("repo.repo_is_empty"), true)
  316. }
  317. ctx.HTML(200, "repo/bare")
  318. }
  319. return
  320. }
  321. ctx.Data["TagName"] = ctx.Repo.TagName
  322. brs, err := ctx.Repo.GitRepo.GetBranches()
  323. if err != nil {
  324. ctx.Handle(500, "GetBranches", err)
  325. return
  326. }
  327. ctx.Data["Branches"] = brs
  328. ctx.Data["BrancheCount"] = len(brs)
  329. // If not branch selected, try default one.
  330. // If default branch doesn't exists, fall back to some other branch.
  331. if len(ctx.Repo.BranchName) == 0 {
  332. if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  333. ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
  334. } else if len(brs) > 0 {
  335. ctx.Repo.BranchName = brs[0]
  336. }
  337. }
  338. ctx.Data["BranchName"] = ctx.Repo.BranchName
  339. ctx.Data["CommitID"] = ctx.Repo.CommitID
  340. if ctx.Query("go-get") == "1" {
  341. ctx.Data["GoGetImport"] = path.Join(setting.Domain, setting.AppSubUrl, owner.Name, repo.Name)
  342. prefix := path.Join(setting.AppUrl, owner.Name, repo.Name, "src", ctx.Repo.BranchName)
  343. ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
  344. ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
  345. }
  346. }
  347. }
  348. func RequireRepoAdmin() macaron.Handler {
  349. return func(ctx *Context) {
  350. if !ctx.Repo.IsAdmin() {
  351. ctx.Handle(404, ctx.Req.RequestURI, nil)
  352. return
  353. }
  354. }
  355. }
  356. func RequireRepoPusher() macaron.Handler {
  357. return func(ctx *Context) {
  358. if !ctx.Repo.IsPusher() {
  359. ctx.Handle(404, ctx.Req.RequestURI, nil)
  360. return
  361. }
  362. }
  363. }
  364. // GitHookService checks if repository Git hooks service has been enabled.
  365. func GitHookService() macaron.Handler {
  366. return func(ctx *Context) {
  367. if !ctx.User.CanEditGitHook() {
  368. ctx.Handle(404, "GitHookService", nil)
  369. return
  370. }
  371. }
  372. }