repo.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package context
  2. import (
  3. "fmt"
  4. "gitote/gitote/models"
  5. "gitote/gitote/models/errors"
  6. "gitote/gitote/pkg/setting"
  7. "io/ioutil"
  8. "strings"
  9. "gitlab.com/gitote/git-module"
  10. "gopkg.in/editorconfig/editorconfig-core-go.v1"
  11. "gopkg.in/macaron.v1"
  12. )
  13. type PullRequest struct {
  14. BaseRepo *models.Repository
  15. Allowed bool
  16. SameRepo bool
  17. HeadInfo string // [<user>:]<branch>
  18. }
  19. type Repository struct {
  20. AccessMode models.AccessMode
  21. IsWatching bool
  22. IsViewBranch bool
  23. IsViewTag bool
  24. IsViewCommit bool
  25. Repository *models.Repository
  26. Owner *models.User
  27. Commit *git.Commit
  28. Tag *git.Tag
  29. GitRepo *git.Repository
  30. BranchName string
  31. TagName string
  32. TreePath string
  33. CommitID string
  34. RepoLink string
  35. CloneLink models.CloneLink
  36. CommitsCount int64
  37. Mirror *models.Mirror
  38. PullRequest *PullRequest
  39. }
  40. // IsOwner returns true if current user is the owner of repository.
  41. func (r *Repository) IsOwner() bool {
  42. return r.AccessMode >= models.ACCESS_MODE_OWNER
  43. }
  44. // IsAdmin returns true if current user has admin or higher access of repository.
  45. func (r *Repository) IsAdmin() bool {
  46. return r.AccessMode >= models.ACCESS_MODE_ADMIN
  47. }
  48. // IsWriter returns true if current user has write or higher access of repository.
  49. func (r *Repository) IsWriter() bool {
  50. return r.AccessMode >= models.ACCESS_MODE_WRITE
  51. }
  52. // HasAccess returns true if the current user has at least read access for this repository
  53. func (r *Repository) HasAccess() bool {
  54. return r.AccessMode >= models.ACCESS_MODE_READ
  55. }
  56. // CanEnableEditor returns true if repository is editable and user has proper access level.
  57. func (r *Repository) CanEnableEditor() bool {
  58. return r.Repository.CanEnableEditor() && r.IsViewBranch && r.IsWriter() && !r.Repository.IsBranchRequirePullRequest(r.BranchName)
  59. }
  60. // GetEditorconfig returns the .editorconfig definition if found in the
  61. // HEAD of the default repo branch.
  62. func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
  63. commit, err := r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
  64. if err != nil {
  65. return nil, err
  66. }
  67. treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
  68. if err != nil {
  69. return nil, err
  70. }
  71. reader, err := treeEntry.Blob().Data()
  72. if err != nil {
  73. return nil, err
  74. }
  75. data, err := ioutil.ReadAll(reader)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return editorconfig.ParseBytes(data)
  80. }
  81. // PullRequestURL returns URL for composing a pull request.
  82. // This function does not check if the repository can actually compose a pull request.
  83. func (r *Repository) PullRequestURL(baseBranch, headBranch string) string {
  84. repoLink := r.RepoLink
  85. if r.PullRequest.BaseRepo != nil {
  86. repoLink = r.PullRequest.BaseRepo.Link()
  87. }
  88. return fmt.Sprintf("%s/compare/%s...%s:%s", repoLink, baseBranch, r.Owner.Name, headBranch)
  89. }
  90. // [0]: issues, [1]: wiki
  91. func RepoAssignment(pages ...bool) macaron.Handler {
  92. return func(c *Context) {
  93. var (
  94. owner *models.User
  95. err error
  96. isIssuesPage bool
  97. isWikiPage bool
  98. )
  99. if len(pages) > 0 {
  100. isIssuesPage = pages[0]
  101. }
  102. if len(pages) > 1 {
  103. isWikiPage = pages[1]
  104. }
  105. ownerName := c.Params(":username")
  106. repoName := strings.TrimSuffix(c.Params(":reponame"), ".git")
  107. refName := c.Params(":branchname")
  108. if len(refName) == 0 {
  109. refName = c.Params(":path")
  110. }
  111. // Check if the user is the same as the repository owner
  112. if c.IsLogged && c.User.LowerName == strings.ToLower(ownerName) {
  113. owner = c.User
  114. } else {
  115. owner, err = models.GetUserByName(ownerName)
  116. if err != nil {
  117. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  118. return
  119. }
  120. }
  121. c.Repo.Owner = owner
  122. c.Data["Username"] = c.Repo.Owner.Name
  123. repo, err := models.GetRepositoryByName(owner.ID, repoName)
  124. if err != nil {
  125. c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
  126. return
  127. }
  128. c.Repo.Repository = repo
  129. c.Data["RepoName"] = c.Repo.Repository.Name
  130. c.Data["IsBareRepo"] = c.Repo.Repository.IsBare
  131. c.Repo.RepoLink = repo.Link()
  132. c.Data["RepoLink"] = c.Repo.RepoLink
  133. c.Data["RepoRelPath"] = c.Repo.Owner.Name + "/" + c.Repo.Repository.Name
  134. // Admin has super access.
  135. if c.IsLogged && c.User.IsAdmin {
  136. c.Repo.AccessMode = models.ACCESS_MODE_OWNER
  137. } else {
  138. mode, err := models.AccessLevel(c.UserID(), repo)
  139. if err != nil {
  140. c.ServerError("AccessLevel", err)
  141. return
  142. }
  143. c.Repo.AccessMode = mode
  144. }
  145. // Check access
  146. if c.Repo.AccessMode == models.ACCESS_MODE_NONE {
  147. // Redirect to any accessible page if not yet on it
  148. if repo.IsPartialPublic() &&
  149. (!(isIssuesPage || isWikiPage) ||
  150. (isIssuesPage && !repo.CanGuestViewIssues()) ||
  151. (isWikiPage && !repo.CanGuestViewWiki())) {
  152. switch {
  153. case repo.CanGuestViewIssues():
  154. c.Redirect(repo.Link() + "/issues")
  155. case repo.CanGuestViewWiki():
  156. c.Redirect(repo.Link() + "/wiki")
  157. default:
  158. c.NotFound()
  159. }
  160. return
  161. }
  162. // Response 404 if user is on completely private repository or possible accessible page but owner doesn't enabled
  163. if !repo.IsPartialPublic() ||
  164. (isIssuesPage && !repo.CanGuestViewIssues()) ||
  165. (isWikiPage && !repo.CanGuestViewWiki()) {
  166. c.NotFound()
  167. return
  168. }
  169. c.Repo.Repository.EnableIssues = repo.CanGuestViewIssues()
  170. c.Repo.Repository.EnableWiki = repo.CanGuestViewWiki()
  171. }
  172. if repo.IsMirror {
  173. c.Repo.Mirror, err = models.GetMirrorByRepoID(repo.ID)
  174. if err != nil {
  175. c.ServerError("GetMirror", err)
  176. return
  177. }
  178. c.Data["MirrorEnablePrune"] = c.Repo.Mirror.EnablePrune
  179. c.Data["MirrorInterval"] = c.Repo.Mirror.Interval
  180. c.Data["Mirror"] = c.Repo.Mirror
  181. }
  182. gitRepo, err := git.OpenRepository(models.RepoPath(ownerName, repoName))
  183. if err != nil {
  184. c.ServerError(fmt.Sprintf("RepoAssignment Invalid repo '%s'", c.Repo.Repository.RepoPath()), err)
  185. return
  186. }
  187. c.Repo.GitRepo = gitRepo
  188. tags, err := c.Repo.GitRepo.GetTags()
  189. if err != nil {
  190. c.ServerError(fmt.Sprintf("GetTags '%s'", c.Repo.Repository.RepoPath()), err)
  191. return
  192. }
  193. c.Data["Tags"] = tags
  194. c.Repo.Repository.NumTags = len(tags)
  195. c.Data["Title"] = owner.Name + "/" + repo.Name
  196. c.Data["Repository"] = repo
  197. c.Data["Owner"] = c.Repo.Repository.Owner
  198. c.Data["IsRepositoryOwner"] = c.Repo.IsOwner()
  199. c.Data["IsRepositoryAdmin"] = c.Repo.IsAdmin()
  200. c.Data["IsRepositoryWriter"] = c.Repo.IsWriter()
  201. c.Data["DisableSSH"] = setting.SSH.Disabled
  202. c.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
  203. c.Data["CloneLink"] = repo.CloneLink()
  204. c.Data["WikiCloneLink"] = repo.WikiCloneLink()
  205. if c.IsLogged {
  206. c.Data["IsWatchingRepo"] = models.IsWatching(c.User.ID, repo.ID)
  207. c.Data["IsStaringRepo"] = models.IsStaring(c.User.ID, repo.ID)
  208. }
  209. // repo is bare and display enable
  210. if c.Repo.Repository.IsBare {
  211. return
  212. }
  213. c.Data["TagName"] = c.Repo.TagName
  214. brs, err := c.Repo.GitRepo.GetBranches()
  215. if err != nil {
  216. c.ServerError("GetBranches", err)
  217. return
  218. }
  219. c.Data["Branches"] = brs
  220. c.Data["BrancheCount"] = len(brs)
  221. // If not branch selected, try default one.
  222. // If default branch doesn't exists, fall back to some other branch.
  223. if len(c.Repo.BranchName) == 0 {
  224. if len(c.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(c.Repo.Repository.DefaultBranch) {
  225. c.Repo.BranchName = c.Repo.Repository.DefaultBranch
  226. } else if len(brs) > 0 {
  227. c.Repo.BranchName = brs[0]
  228. }
  229. }
  230. c.Data["BranchName"] = c.Repo.BranchName
  231. c.Data["CommitID"] = c.Repo.CommitID
  232. c.Data["IsGuest"] = !c.Repo.HasAccess()
  233. }
  234. }
  235. // RepoRef handles repository reference name including those contain `/`.
  236. func RepoRef() macaron.Handler {
  237. return func(c *Context) {
  238. // Empty repository does not have reference information.
  239. if c.Repo.Repository.IsBare {
  240. return
  241. }
  242. var (
  243. refName string
  244. err error
  245. )
  246. // For API calls.
  247. if c.Repo.GitRepo == nil {
  248. repoPath := models.RepoPath(c.Repo.Owner.Name, c.Repo.Repository.Name)
  249. c.Repo.GitRepo, err = git.OpenRepository(repoPath)
  250. if err != nil {
  251. c.Handle(500, "RepoRef Invalid repo "+repoPath, err)
  252. return
  253. }
  254. }
  255. // Get default branch.
  256. if len(c.Params("*")) == 0 {
  257. refName = c.Repo.Repository.DefaultBranch
  258. if !c.Repo.GitRepo.IsBranchExist(refName) {
  259. brs, err := c.Repo.GitRepo.GetBranches()
  260. if err != nil {
  261. c.Handle(500, "GetBranches", err)
  262. return
  263. }
  264. refName = brs[0]
  265. }
  266. c.Repo.Commit, err = c.Repo.GitRepo.GetBranchCommit(refName)
  267. if err != nil {
  268. c.Handle(500, "GetBranchCommit", err)
  269. return
  270. }
  271. c.Repo.CommitID = c.Repo.Commit.ID.String()
  272. c.Repo.IsViewBranch = true
  273. } else {
  274. hasMatched := false
  275. parts := strings.Split(c.Params("*"), "/")
  276. for i, part := range parts {
  277. refName = strings.TrimPrefix(refName+"/"+part, "/")
  278. if c.Repo.GitRepo.IsBranchExist(refName) ||
  279. c.Repo.GitRepo.IsTagExist(refName) {
  280. if i < len(parts)-1 {
  281. c.Repo.TreePath = strings.Join(parts[i+1:], "/")
  282. }
  283. hasMatched = true
  284. break
  285. }
  286. }
  287. if !hasMatched && len(parts[0]) == 40 {
  288. refName = parts[0]
  289. c.Repo.TreePath = strings.Join(parts[1:], "/")
  290. }
  291. if c.Repo.GitRepo.IsBranchExist(refName) {
  292. c.Repo.IsViewBranch = true
  293. c.Repo.Commit, err = c.Repo.GitRepo.GetBranchCommit(refName)
  294. if err != nil {
  295. c.Handle(500, "GetBranchCommit", err)
  296. return
  297. }
  298. c.Repo.CommitID = c.Repo.Commit.ID.String()
  299. } else if c.Repo.GitRepo.IsTagExist(refName) {
  300. c.Repo.IsViewTag = true
  301. c.Repo.Commit, err = c.Repo.GitRepo.GetTagCommit(refName)
  302. if err != nil {
  303. c.Handle(500, "GetTagCommit", err)
  304. return
  305. }
  306. c.Repo.CommitID = c.Repo.Commit.ID.String()
  307. } else if len(refName) == 40 {
  308. c.Repo.IsViewCommit = true
  309. c.Repo.CommitID = refName
  310. c.Repo.Commit, err = c.Repo.GitRepo.GetCommit(refName)
  311. if err != nil {
  312. c.NotFound()
  313. return
  314. }
  315. } else {
  316. c.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  317. return
  318. }
  319. }
  320. c.Repo.BranchName = refName
  321. c.Data["BranchName"] = c.Repo.BranchName
  322. c.Data["CommitID"] = c.Repo.CommitID
  323. c.Data["TreePath"] = c.Repo.TreePath
  324. c.Data["IsViewBranch"] = c.Repo.IsViewBranch
  325. c.Data["IsViewTag"] = c.Repo.IsViewTag
  326. c.Data["IsViewCommit"] = c.Repo.IsViewCommit
  327. // People who have push access or have fored repository can propose a new pull request.
  328. if c.Repo.IsWriter() || (c.IsLogged && c.User.HasForkedRepo(c.Repo.Repository.ID)) {
  329. // Pull request is allowed if this is a fork repository
  330. // and base repository accepts pull requests.
  331. if c.Repo.Repository.BaseRepo != nil {
  332. if c.Repo.Repository.BaseRepo.AllowsPulls() {
  333. c.Repo.PullRequest.Allowed = true
  334. // In-repository pull requests has higher priority than cross-repository if user is viewing
  335. // base repository and 1) has write access to it 2) has forked it.
  336. if c.Repo.IsWriter() {
  337. c.Data["BaseRepo"] = c.Repo.Repository.BaseRepo
  338. c.Repo.PullRequest.BaseRepo = c.Repo.Repository.BaseRepo
  339. c.Repo.PullRequest.HeadInfo = c.Repo.Owner.Name + ":" + c.Repo.BranchName
  340. } else {
  341. c.Data["BaseRepo"] = c.Repo.Repository
  342. c.Repo.PullRequest.BaseRepo = c.Repo.Repository
  343. c.Repo.PullRequest.HeadInfo = c.User.Name + ":" + c.Repo.BranchName
  344. }
  345. }
  346. } else {
  347. // Or, this is repository accepts pull requests between branches.
  348. if c.Repo.Repository.AllowsPulls() {
  349. c.Data["BaseRepo"] = c.Repo.Repository
  350. c.Repo.PullRequest.BaseRepo = c.Repo.Repository
  351. c.Repo.PullRequest.Allowed = true
  352. c.Repo.PullRequest.SameRepo = true
  353. c.Repo.PullRequest.HeadInfo = c.Repo.BranchName
  354. }
  355. }
  356. }
  357. c.Data["PullRequestCtx"] = c.Repo.PullRequest
  358. }
  359. }
  360. func RequireRepoAdmin() macaron.Handler {
  361. return func(c *Context) {
  362. if !c.IsLogged || (!c.Repo.IsAdmin() && !c.User.IsAdmin) {
  363. c.NotFound()
  364. return
  365. }
  366. }
  367. }
  368. func RequireRepoWriter() macaron.Handler {
  369. return func(c *Context) {
  370. if !c.IsLogged || (!c.Repo.IsWriter() && !c.User.IsAdmin) {
  371. c.NotFound()
  372. return
  373. }
  374. }
  375. }
  376. // GitHookService checks if repository Git hooks service has been enabled.
  377. func GitHookService() macaron.Handler {
  378. return func(c *Context) {
  379. if !c.User.CanEditGitHook() {
  380. c.NotFound()
  381. return
  382. }
  383. }
  384. }