You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo.go 11 kB

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