repo.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 context
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "path"
  9. "strings"
  10. "github.com/Unknwon/com"
  11. log "gopkg.in/clog.v1"
  12. "gopkg.in/editorconfig/editorconfig-core-go.v1"
  13. "gopkg.in/macaron.v1"
  14. "github.com/gogits/git-module"
  15. "github.com/gogits/gogs/models"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. type PullRequest struct {
  19. BaseRepo *models.Repository
  20. Allowed bool
  21. SameRepo bool
  22. HeadInfo string // [<user>:]<branch>
  23. }
  24. type Repository struct {
  25. AccessMode models.AccessMode
  26. IsWatching bool
  27. IsViewBranch bool
  28. IsViewTag bool
  29. IsViewCommit bool
  30. Repository *models.Repository
  31. Owner *models.User
  32. Commit *git.Commit
  33. Tag *git.Tag
  34. GitRepo *git.Repository
  35. BranchName string
  36. TagName string
  37. TreePath string
  38. CommitID string
  39. RepoLink string
  40. CloneLink models.CloneLink
  41. CommitsCount int64
  42. Mirror *models.Mirror
  43. PullRequest *PullRequest
  44. }
  45. // IsOwner returns true if current user is the owner of repository.
  46. func (r *Repository) IsOwner() bool {
  47. return r.AccessMode >= models.ACCESS_MODE_OWNER
  48. }
  49. // IsAdmin returns true if current user has admin or higher access of repository.
  50. func (r *Repository) IsAdmin() bool {
  51. return r.AccessMode >= models.ACCESS_MODE_ADMIN
  52. }
  53. // IsWriter returns true if current user has write or higher access of repository.
  54. func (r *Repository) IsWriter() bool {
  55. return r.AccessMode >= models.ACCESS_MODE_WRITE
  56. }
  57. // HasAccess returns true if the current user has at least read access for this repository
  58. func (r *Repository) HasAccess() bool {
  59. return r.AccessMode >= models.ACCESS_MODE_READ
  60. }
  61. // CanEnableEditor returns true if repository is editable and user has proper access level.
  62. func (r *Repository) CanEnableEditor() bool {
  63. return r.Repository.CanEnableEditor() && r.IsViewBranch && r.IsWriter() && !r.Repository.IsBranchRequirePullRequest(r.BranchName)
  64. }
  65. // GetEditorconfig returns the .editorconfig definition if found in the
  66. // HEAD of the default repo branch.
  67. func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
  68. commit, err := r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
  69. if err != nil {
  70. return nil, err
  71. }
  72. treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
  73. if err != nil {
  74. return nil, err
  75. }
  76. reader, err := treeEntry.Blob().Data()
  77. if err != nil {
  78. return nil, err
  79. }
  80. data, err := ioutil.ReadAll(reader)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return editorconfig.ParseBytes(data)
  85. }
  86. // PullRequestURL returns URL for composing a pull request.
  87. // This function does not check if the repository can actually compose a pull request.
  88. func (r *Repository) PullRequestURL(baseBranch, headBranch string) string {
  89. repoLink := r.RepoLink
  90. if r.PullRequest.BaseRepo != nil {
  91. repoLink = r.PullRequest.BaseRepo.Link()
  92. }
  93. return fmt.Sprintf("%s/compare/%s...%s:%s", repoLink, baseBranch, r.Owner.Name, headBranch)
  94. }
  95. func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
  96. // Non-fork repository will not return error in this method.
  97. if err := repo.GetBaseRepo(); err != nil {
  98. if models.IsErrRepoNotExist(err) {
  99. repo.IsFork = false
  100. repo.ForkID = 0
  101. return
  102. }
  103. ctx.Handle(500, "GetBaseRepo", err)
  104. return
  105. } else if err = repo.BaseRepo.GetOwner(); err != nil {
  106. ctx.Handle(500, "BaseRepo.GetOwner", err)
  107. return
  108. }
  109. }
  110. // composeGoGetImport returns go-get-import meta content.
  111. func composeGoGetImport(owner, repo string) string {
  112. return path.Join(setting.Domain, setting.AppSubUrl, owner, repo)
  113. }
  114. // earlyResponseForGoGetMeta responses appropriate go-get meta with status 200
  115. // if user does not have actual access to the requested repository,
  116. // or the owner or repository does not exist at all.
  117. // This is particular a workaround for "go get" command which does not respect
  118. // .netrc file.
  119. func earlyResponseForGoGetMeta(ctx *Context) {
  120. ctx.PlainText(200, []byte(com.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`,
  121. map[string]string{
  122. "GoGetImport": composeGoGetImport(ctx.Params(":username"), ctx.Params(":reponame")),
  123. "CloneLink": models.ComposeHTTPSCloneURL(ctx.Params(":username"), ctx.Params(":reponame")),
  124. })))
  125. }
  126. func RepoAssignment(args ...bool) macaron.Handler {
  127. return func(ctx *Context) {
  128. var (
  129. displayBare bool // To display bare page if it is a bare repo.
  130. )
  131. if len(args) >= 1 {
  132. displayBare = args[0]
  133. }
  134. var (
  135. owner *models.User
  136. err error
  137. )
  138. ownerName := ctx.Params(":username")
  139. repoName := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
  140. refName := ctx.Params(":branchname")
  141. if len(refName) == 0 {
  142. refName = ctx.Params(":path")
  143. }
  144. // Check if the user is the same as the repository owner
  145. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(ownerName) {
  146. owner = ctx.User
  147. } else {
  148. owner, err = models.GetUserByName(ownerName)
  149. if err != nil {
  150. if models.IsErrUserNotExist(err) {
  151. if ctx.Query("go-get") == "1" {
  152. earlyResponseForGoGetMeta(ctx)
  153. return
  154. }
  155. ctx.NotFound()
  156. } else {
  157. ctx.Handle(500, "GetUserByName", err)
  158. }
  159. return
  160. }
  161. }
  162. ctx.Repo.Owner = owner
  163. ctx.Data["Username"] = ctx.Repo.Owner.Name
  164. // Get repository.
  165. repo, err := models.GetRepositoryByName(owner.ID, repoName)
  166. if err != nil {
  167. if models.IsErrRepoNotExist(err) {
  168. if ctx.Query("go-get") == "1" {
  169. earlyResponseForGoGetMeta(ctx)
  170. return
  171. }
  172. ctx.NotFound()
  173. } else {
  174. ctx.Handle(500, "GetRepositoryByName", err)
  175. }
  176. return
  177. } else if err = repo.GetOwner(); err != nil {
  178. ctx.Handle(500, "GetOwner", err)
  179. return
  180. }
  181. // Admin has super access.
  182. if ctx.IsSigned && ctx.User.IsAdmin {
  183. ctx.Repo.AccessMode = models.ACCESS_MODE_OWNER
  184. } else {
  185. var userID int64
  186. if ctx.IsSigned {
  187. userID = ctx.User.ID
  188. }
  189. mode, err := models.AccessLevel(userID, repo)
  190. if err != nil {
  191. ctx.Handle(500, "AccessLevel", err)
  192. return
  193. }
  194. ctx.Repo.AccessMode = mode
  195. }
  196. // Check access.
  197. if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
  198. if ctx.Query("go-get") == "1" {
  199. earlyResponseForGoGetMeta(ctx)
  200. return
  201. }
  202. ctx.NotFound()
  203. return
  204. }
  205. ctx.Data["HasAccess"] = true
  206. if repo.IsMirror {
  207. ctx.Repo.Mirror, err = models.GetMirrorByRepoID(repo.ID)
  208. if err != nil {
  209. ctx.Handle(500, "GetMirror", err)
  210. return
  211. }
  212. ctx.Data["MirrorEnablePrune"] = ctx.Repo.Mirror.EnablePrune
  213. ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
  214. ctx.Data["Mirror"] = ctx.Repo.Mirror
  215. }
  216. ctx.Repo.Repository = repo
  217. ctx.Data["RepoName"] = ctx.Repo.Repository.Name
  218. ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
  219. gitRepo, err := git.OpenRepository(models.RepoPath(ownerName, repoName))
  220. if err != nil {
  221. ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(ownerName, repoName), err)
  222. return
  223. }
  224. ctx.Repo.GitRepo = gitRepo
  225. ctx.Repo.RepoLink = repo.Link()
  226. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  227. ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
  228. tags, err := ctx.Repo.GitRepo.GetTags()
  229. if err != nil {
  230. ctx.Handle(500, fmt.Sprintf("GetTags '%s'", ctx.Repo.Repository.RepoPath()), err)
  231. return
  232. }
  233. ctx.Data["Tags"] = tags
  234. ctx.Repo.Repository.NumTags = len(tags)
  235. ctx.Data["Title"] = owner.Name + "/" + repo.Name
  236. ctx.Data["Repository"] = repo
  237. ctx.Data["Owner"] = ctx.Repo.Repository.Owner
  238. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
  239. ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
  240. ctx.Data["IsRepositoryWriter"] = ctx.Repo.IsWriter()
  241. ctx.Data["DisableSSH"] = setting.SSH.Disabled
  242. ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
  243. ctx.Data["CloneLink"] = repo.CloneLink()
  244. ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
  245. if ctx.IsSigned {
  246. ctx.Data["IsWatchingRepo"] = models.IsWatching(ctx.User.ID, repo.ID)
  247. ctx.Data["IsStaringRepo"] = models.IsStaring(ctx.User.ID, repo.ID)
  248. }
  249. // repo is bare and display enable
  250. if ctx.Repo.Repository.IsBare {
  251. log.Trace("Bare repository: %s", ctx.Repo.RepoLink)
  252. // NOTE: to prevent templating error
  253. ctx.Data["BranchName"] = ""
  254. if displayBare {
  255. if !ctx.Repo.IsAdmin() {
  256. ctx.Flash.Info(ctx.Tr("repo.repo_is_empty"), true)
  257. }
  258. ctx.HTML(200, "repo/bare")
  259. }
  260. return
  261. }
  262. ctx.Data["TagName"] = ctx.Repo.TagName
  263. brs, err := ctx.Repo.GitRepo.GetBranches()
  264. if err != nil {
  265. ctx.Handle(500, "GetBranches", err)
  266. return
  267. }
  268. ctx.Data["Branches"] = brs
  269. ctx.Data["BrancheCount"] = len(brs)
  270. // If not branch selected, try default one.
  271. // If default branch doesn't exists, fall back to some other branch.
  272. if len(ctx.Repo.BranchName) == 0 {
  273. if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  274. ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
  275. } else if len(brs) > 0 {
  276. ctx.Repo.BranchName = brs[0]
  277. }
  278. }
  279. ctx.Data["BranchName"] = ctx.Repo.BranchName
  280. ctx.Data["CommitID"] = ctx.Repo.CommitID
  281. if ctx.Query("go-get") == "1" {
  282. ctx.Data["GoGetImport"] = composeGoGetImport(owner.Name, repo.Name)
  283. prefix := setting.AppUrl + path.Join(owner.Name, repo.Name, "src", ctx.Repo.BranchName)
  284. ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
  285. ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
  286. }
  287. }
  288. }
  289. // RepoRef handles repository reference name including those contain `/`.
  290. func RepoRef() macaron.Handler {
  291. return func(ctx *Context) {
  292. // Empty repository does not have reference information.
  293. if ctx.Repo.Repository.IsBare {
  294. return
  295. }
  296. var (
  297. refName string
  298. err error
  299. )
  300. // For API calls.
  301. if ctx.Repo.GitRepo == nil {
  302. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  303. ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
  304. if err != nil {
  305. ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
  306. return
  307. }
  308. }
  309. // Get default branch.
  310. if len(ctx.Params("*")) == 0 {
  311. refName = ctx.Repo.Repository.DefaultBranch
  312. if !ctx.Repo.GitRepo.IsBranchExist(refName) {
  313. brs, err := ctx.Repo.GitRepo.GetBranches()
  314. if err != nil {
  315. ctx.Handle(500, "GetBranches", err)
  316. return
  317. }
  318. refName = brs[0]
  319. }
  320. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
  321. if err != nil {
  322. ctx.Handle(500, "GetBranchCommit", err)
  323. return
  324. }
  325. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  326. ctx.Repo.IsViewBranch = true
  327. } else {
  328. hasMatched := false
  329. parts := strings.Split(ctx.Params("*"), "/")
  330. for i, part := range parts {
  331. refName = strings.TrimPrefix(refName+"/"+part, "/")
  332. if ctx.Repo.GitRepo.IsBranchExist(refName) ||
  333. ctx.Repo.GitRepo.IsTagExist(refName) {
  334. if i < len(parts)-1 {
  335. ctx.Repo.TreePath = strings.Join(parts[i+1:], "/")
  336. }
  337. hasMatched = true
  338. break
  339. }
  340. }
  341. if !hasMatched && len(parts[0]) == 40 {
  342. refName = parts[0]
  343. ctx.Repo.TreePath = strings.Join(parts[1:], "/")
  344. }
  345. if ctx.Repo.GitRepo.IsBranchExist(refName) {
  346. ctx.Repo.IsViewBranch = true
  347. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
  348. if err != nil {
  349. ctx.Handle(500, "GetBranchCommit", err)
  350. return
  351. }
  352. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  353. } else if ctx.Repo.GitRepo.IsTagExist(refName) {
  354. ctx.Repo.IsViewTag = true
  355. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName)
  356. if err != nil {
  357. ctx.Handle(500, "GetTagCommit", err)
  358. return
  359. }
  360. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  361. } else if len(refName) == 40 {
  362. ctx.Repo.IsViewCommit = true
  363. ctx.Repo.CommitID = refName
  364. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
  365. if err != nil {
  366. ctx.NotFound()
  367. return
  368. }
  369. } else {
  370. ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  371. return
  372. }
  373. }
  374. ctx.Repo.BranchName = refName
  375. ctx.Data["BranchName"] = ctx.Repo.BranchName
  376. ctx.Data["CommitID"] = ctx.Repo.CommitID
  377. ctx.Data["TreePath"] = ctx.Repo.TreePath
  378. ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch
  379. ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag
  380. ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
  381. if ctx.Repo.Repository.IsFork {
  382. RetrieveBaseRepo(ctx, ctx.Repo.Repository)
  383. if ctx.Written() {
  384. return
  385. }
  386. }
  387. // People who have push access or have fored repository can propose a new pull request.
  388. if ctx.Repo.IsWriter() || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID)) {
  389. // Pull request is allowed if this is a fork repository
  390. // and base repository accepts pull requests.
  391. if ctx.Repo.Repository.BaseRepo != nil {
  392. if ctx.Repo.Repository.BaseRepo.AllowsPulls() {
  393. ctx.Data["BaseRepo"] = ctx.Repo.Repository.BaseRepo
  394. ctx.Repo.PullRequest.BaseRepo = ctx.Repo.Repository.BaseRepo
  395. ctx.Repo.PullRequest.Allowed = true
  396. ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
  397. }
  398. } else {
  399. // Or, this is repository accepts pull requests between branches.
  400. if ctx.Repo.Repository.AllowsPulls() {
  401. ctx.Data["BaseRepo"] = ctx.Repo.Repository
  402. ctx.Repo.PullRequest.BaseRepo = ctx.Repo.Repository
  403. ctx.Repo.PullRequest.Allowed = true
  404. ctx.Repo.PullRequest.SameRepo = true
  405. ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
  406. }
  407. }
  408. }
  409. ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
  410. ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
  411. if err != nil {
  412. ctx.Handle(500, "CommitsCount", err)
  413. return
  414. }
  415. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  416. }
  417. }
  418. func RequireRepoAdmin() macaron.Handler {
  419. return func(ctx *Context) {
  420. if !ctx.IsSigned || (!ctx.Repo.IsAdmin() && !ctx.User.IsAdmin) {
  421. ctx.NotFound()
  422. return
  423. }
  424. }
  425. }
  426. func RequireRepoWriter() macaron.Handler {
  427. return func(ctx *Context) {
  428. if !ctx.IsSigned || (!ctx.Repo.IsWriter() && !ctx.User.IsAdmin) {
  429. ctx.NotFound()
  430. return
  431. }
  432. }
  433. }
  434. // GitHookService checks if repository Git hooks service has been enabled.
  435. func GitHookService() macaron.Handler {
  436. return func(ctx *Context) {
  437. if !ctx.User.CanEditGitHook() {
  438. ctx.NotFound()
  439. return
  440. }
  441. }
  442. }
PANIC: session(release): write data/sessions/6/d/6dec5bc8f9d85aeb: no space left on device

PANIC

session(release): write data/sessions/6/d/6dec5bc8f9d85aeb: no space left on device
github.com/go-macaron/session@v0.0.0-20190805070824-1a3cdc6f5659/session.go:199 (0x8b2934)
gopkg.in/macaron.v1@v1.3.9/context.go:79 (0x83d0a0)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:157 (0x80ab07)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:135 (0x80a8a8)
gopkg.in/macaron.v1@v1.3.9/context.go:121 (0x83d1f8)
gopkg.in/macaron.v1@v1.3.9/context.go:112 (0x84fdb5)
gopkg.in/macaron.v1@v1.3.9/recovery.go:161 (0x84fda8)
gopkg.in/macaron.v1@v1.3.9/logger.go:40 (0x840c73)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:157 (0x80ab07)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:135 (0x80a8a8)
gopkg.in/macaron.v1@v1.3.9/context.go:121 (0x83d1f8)
gopkg.in/macaron.v1@v1.3.9/router.go:187 (0x850fc6)
gopkg.in/macaron.v1@v1.3.9/router.go:303 (0x8493e5)
gopkg.in/macaron.v1@v1.3.9/macaron.go:220 (0x841fca)
net/http/server.go:2836 (0x7a79b2)
net/http/server.go:1924 (0x7a341b)
runtime/asm_amd64.s:1373 (0x46f9f0)