repo.go 10 KB

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