Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

repo.go 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package context
  5. import (
  6. "context"
  7. "fmt"
  8. "html"
  9. "net/http"
  10. "net/url"
  11. "path"
  12. "strings"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/models/db"
  15. git_model "code.gitea.io/gitea/models/git"
  16. issues_model "code.gitea.io/gitea/models/issues"
  17. access_model "code.gitea.io/gitea/models/perm/access"
  18. repo_model "code.gitea.io/gitea/models/repo"
  19. unit_model "code.gitea.io/gitea/models/unit"
  20. user_model "code.gitea.io/gitea/models/user"
  21. "code.gitea.io/gitea/modules/cache"
  22. "code.gitea.io/gitea/modules/git"
  23. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  24. "code.gitea.io/gitea/modules/log"
  25. repo_module "code.gitea.io/gitea/modules/repository"
  26. "code.gitea.io/gitea/modules/setting"
  27. "code.gitea.io/gitea/modules/util"
  28. asymkey_service "code.gitea.io/gitea/services/asymkey"
  29. "github.com/editorconfig/editorconfig-core-go/v2"
  30. )
  31. // PullRequest contains information to make a pull request
  32. type PullRequest struct {
  33. BaseRepo *repo_model.Repository
  34. Allowed bool
  35. SameRepo bool
  36. HeadInfoSubURL string // [<user>:]<branch> url segment
  37. }
  38. // Repository contains information to operate a repository
  39. type Repository struct {
  40. access_model.Permission
  41. IsWatching bool
  42. IsViewBranch bool
  43. IsViewTag bool
  44. IsViewCommit bool
  45. Repository *repo_model.Repository
  46. Owner *user_model.User
  47. Commit *git.Commit
  48. Tag *git.Tag
  49. GitRepo *git.Repository
  50. RefName string
  51. BranchName string
  52. TagName string
  53. TreePath string
  54. CommitID string
  55. RepoLink string
  56. CloneLink repo_model.CloneLink
  57. CommitsCount int64
  58. PullRequest *PullRequest
  59. }
  60. // CanWriteToBranch checks if the branch is writable by the user
  61. func (r *Repository) CanWriteToBranch(ctx context.Context, user *user_model.User, branch string) bool {
  62. return issues_model.CanMaintainerWriteToBranch(ctx, r.Permission, branch, user)
  63. }
  64. // CanEnableEditor returns true if repository is editable and user has proper access level.
  65. func (r *Repository) CanEnableEditor(ctx context.Context, user *user_model.User) bool {
  66. return r.IsViewBranch && r.CanWriteToBranch(ctx, user, r.BranchName) && r.Repository.CanEnableEditor() && !r.Repository.IsArchived
  67. }
  68. // CanCreateBranch returns true if repository is editable and user has proper access level.
  69. func (r *Repository) CanCreateBranch() bool {
  70. return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanCreateBranch()
  71. }
  72. // RepoMustNotBeArchived checks if a repo is archived
  73. func RepoMustNotBeArchived() func(ctx *Context) {
  74. return func(ctx *Context) {
  75. if ctx.Repo.Repository.IsArchived {
  76. ctx.NotFound("IsArchived", fmt.Errorf(ctx.Tr("repo.archive.title")))
  77. }
  78. }
  79. }
  80. // CanCommitToBranchResults represents the results of CanCommitToBranch
  81. type CanCommitToBranchResults struct {
  82. CanCommitToBranch bool
  83. EditorEnabled bool
  84. UserCanPush bool
  85. RequireSigned bool
  86. WillSign bool
  87. SigningKey string
  88. WontSignReason string
  89. }
  90. // CanCommitToBranch returns true if repository is editable and user has proper access level
  91. //
  92. // and branch is not protected for push
  93. func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.User) (CanCommitToBranchResults, error) {
  94. protectedBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, r.Repository.ID, r.BranchName)
  95. if err != nil {
  96. return CanCommitToBranchResults{}, err
  97. }
  98. userCanPush := true
  99. requireSigned := false
  100. if protectedBranch != nil {
  101. protectedBranch.Repo = r.Repository
  102. userCanPush = protectedBranch.CanUserPush(ctx, doer)
  103. requireSigned = protectedBranch.RequireSignedCommits
  104. }
  105. sign, keyID, _, err := asymkey_service.SignCRUDAction(ctx, r.Repository.RepoPath(), doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName)
  106. canCommit := r.CanEnableEditor(ctx, doer) && userCanPush
  107. if requireSigned {
  108. canCommit = canCommit && sign
  109. }
  110. wontSignReason := ""
  111. if err != nil {
  112. if asymkey_service.IsErrWontSign(err) {
  113. wontSignReason = string(err.(*asymkey_service.ErrWontSign).Reason)
  114. err = nil
  115. } else {
  116. wontSignReason = "error"
  117. }
  118. }
  119. return CanCommitToBranchResults{
  120. CanCommitToBranch: canCommit,
  121. EditorEnabled: r.CanEnableEditor(ctx, doer),
  122. UserCanPush: userCanPush,
  123. RequireSigned: requireSigned,
  124. WillSign: sign,
  125. SigningKey: keyID,
  126. WontSignReason: wontSignReason,
  127. }, err
  128. }
  129. // CanUseTimetracker returns whether or not a user can use the timetracker.
  130. func (r *Repository) CanUseTimetracker(issue *issues_model.Issue, user *user_model.User) bool {
  131. // Checking for following:
  132. // 1. Is timetracker enabled
  133. // 2. Is the user a contributor, admin, poster or assignee and do the repository policies require this?
  134. isAssigned, _ := issues_model.IsUserAssignedToIssue(db.DefaultContext, issue, user)
  135. return r.Repository.IsTimetrackerEnabled(db.DefaultContext) && (!r.Repository.AllowOnlyContributorsToTrackTime(db.DefaultContext) ||
  136. r.Permission.CanWriteIssuesOrPulls(issue.IsPull) || issue.IsPoster(user.ID) || isAssigned)
  137. }
  138. // CanCreateIssueDependencies returns whether or not a user can create dependencies.
  139. func (r *Repository) CanCreateIssueDependencies(user *user_model.User, isPull bool) bool {
  140. return r.Repository.IsDependenciesEnabled(db.DefaultContext) && r.Permission.CanWriteIssuesOrPulls(isPull)
  141. }
  142. // GetCommitsCount returns cached commit count for current view
  143. func (r *Repository) GetCommitsCount() (int64, error) {
  144. if r.Commit == nil {
  145. return 0, nil
  146. }
  147. var contextName string
  148. if r.IsViewBranch {
  149. contextName = r.BranchName
  150. } else if r.IsViewTag {
  151. contextName = r.TagName
  152. } else {
  153. contextName = r.CommitID
  154. }
  155. return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, r.IsViewBranch || r.IsViewTag), func() (int64, error) {
  156. return r.Commit.CommitsCount()
  157. })
  158. }
  159. // GetCommitGraphsCount returns cached commit count for current view
  160. func (r *Repository) GetCommitGraphsCount(ctx context.Context, hidePRRefs bool, branches, files []string) (int64, error) {
  161. cacheKey := fmt.Sprintf("commits-count-%d-graph-%t-%s-%s", r.Repository.ID, hidePRRefs, branches, files)
  162. return cache.GetInt64(cacheKey, func() (int64, error) {
  163. if len(branches) == 0 {
  164. return git.AllCommitsCount(ctx, r.Repository.RepoPath(), hidePRRefs, files...)
  165. }
  166. return git.CommitsCount(ctx,
  167. git.CommitsCountOptions{
  168. RepoPath: r.Repository.RepoPath(),
  169. Revision: branches,
  170. RelPath: files,
  171. })
  172. })
  173. }
  174. // BranchNameSubURL sub-URL for the BranchName field
  175. func (r *Repository) BranchNameSubURL() string {
  176. switch {
  177. case r.IsViewBranch:
  178. return "branch/" + util.PathEscapeSegments(r.BranchName)
  179. case r.IsViewTag:
  180. return "tag/" + util.PathEscapeSegments(r.TagName)
  181. case r.IsViewCommit:
  182. return "commit/" + util.PathEscapeSegments(r.CommitID)
  183. }
  184. log.Error("Unknown view type for repo: %v", r)
  185. return ""
  186. }
  187. // FileExists returns true if a file exists in the given repo branch
  188. func (r *Repository) FileExists(path, branch string) (bool, error) {
  189. if branch == "" {
  190. branch = r.Repository.DefaultBranch
  191. }
  192. commit, err := r.GitRepo.GetBranchCommit(branch)
  193. if err != nil {
  194. return false, err
  195. }
  196. if _, err := commit.GetTreeEntryByPath(path); err != nil {
  197. return false, err
  198. }
  199. return true, nil
  200. }
  201. // GetEditorconfig returns the .editorconfig definition if found in the
  202. // HEAD of the default repo branch.
  203. func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (cfg *editorconfig.Editorconfig, warning, err error) {
  204. if r.GitRepo == nil {
  205. return nil, nil, nil
  206. }
  207. var commit *git.Commit
  208. if len(optCommit) != 0 {
  209. commit = optCommit[0]
  210. } else {
  211. commit, err = r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
  212. if err != nil {
  213. return nil, nil, err
  214. }
  215. }
  216. treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
  217. if err != nil {
  218. return nil, nil, err
  219. }
  220. if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
  221. return nil, nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"}
  222. }
  223. reader, err := treeEntry.Blob().DataAsync()
  224. if err != nil {
  225. return nil, nil, err
  226. }
  227. defer reader.Close()
  228. return editorconfig.ParseGraceful(reader)
  229. }
  230. // RetrieveBaseRepo retrieves base repository
  231. func RetrieveBaseRepo(ctx *Context, repo *repo_model.Repository) {
  232. // Non-fork repository will not return error in this method.
  233. if err := repo.GetBaseRepo(ctx); err != nil {
  234. if repo_model.IsErrRepoNotExist(err) {
  235. repo.IsFork = false
  236. repo.ForkID = 0
  237. return
  238. }
  239. ctx.ServerError("GetBaseRepo", err)
  240. return
  241. } else if err = repo.BaseRepo.LoadOwner(ctx); err != nil {
  242. ctx.ServerError("BaseRepo.LoadOwner", err)
  243. return
  244. }
  245. }
  246. // RetrieveTemplateRepo retrieves template repository used to generate this repository
  247. func RetrieveTemplateRepo(ctx *Context, repo *repo_model.Repository) {
  248. // Non-generated repository will not return error in this method.
  249. templateRepo, err := repo_model.GetTemplateRepo(ctx, repo)
  250. if err != nil {
  251. if repo_model.IsErrRepoNotExist(err) {
  252. repo.TemplateID = 0
  253. return
  254. }
  255. ctx.ServerError("GetTemplateRepo", err)
  256. return
  257. } else if err = templateRepo.LoadOwner(ctx); err != nil {
  258. ctx.ServerError("TemplateRepo.LoadOwner", err)
  259. return
  260. }
  261. perm, err := access_model.GetUserRepoPermission(ctx, templateRepo, ctx.Doer)
  262. if err != nil {
  263. ctx.ServerError("GetUserRepoPermission", err)
  264. return
  265. }
  266. if !perm.CanRead(unit_model.TypeCode) {
  267. repo.TemplateID = 0
  268. }
  269. }
  270. // ComposeGoGetImport returns go-get-import meta content.
  271. func ComposeGoGetImport(owner, repo string) string {
  272. /// setting.AppUrl is guaranteed to be parse as url
  273. appURL, _ := url.Parse(setting.AppURL)
  274. return path.Join(appURL.Host, setting.AppSubURL, url.PathEscape(owner), url.PathEscape(repo))
  275. }
  276. // EarlyResponseForGoGetMeta responses appropriate go-get meta with status 200
  277. // if user does not have actual access to the requested repository,
  278. // or the owner or repository does not exist at all.
  279. // This is particular a workaround for "go get" command which does not respect
  280. // .netrc file.
  281. func EarlyResponseForGoGetMeta(ctx *Context) {
  282. username := ctx.Params(":username")
  283. reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
  284. if username == "" || reponame == "" {
  285. ctx.PlainText(http.StatusBadRequest, "invalid repository path")
  286. return
  287. }
  288. var cloneURL string
  289. if setting.Repository.GoGetCloneURLProtocol == "ssh" {
  290. cloneURL = repo_model.ComposeSSHCloneURL(username, reponame)
  291. } else {
  292. cloneURL = repo_model.ComposeHTTPSCloneURL(username, reponame)
  293. }
  294. goImportContent := fmt.Sprintf("%s git %s", ComposeGoGetImport(username, reponame), cloneURL)
  295. htmlMeta := fmt.Sprintf(`<meta name="go-import" content="%s">`, html.EscapeString(goImportContent))
  296. ctx.PlainText(http.StatusOK, htmlMeta)
  297. }
  298. // RedirectToRepo redirect to a differently-named repository
  299. func RedirectToRepo(ctx *Base, redirectRepoID int64) {
  300. ownerName := ctx.Params(":username")
  301. previousRepoName := ctx.Params(":reponame")
  302. repo, err := repo_model.GetRepositoryByID(ctx, redirectRepoID)
  303. if err != nil {
  304. log.Error("GetRepositoryByID: %v", err)
  305. ctx.Error(http.StatusInternalServerError, "GetRepositoryByID")
  306. return
  307. }
  308. redirectPath := strings.Replace(
  309. ctx.Req.URL.EscapedPath(),
  310. url.PathEscape(ownerName)+"/"+url.PathEscape(previousRepoName),
  311. url.PathEscape(repo.OwnerName)+"/"+url.PathEscape(repo.Name),
  312. 1,
  313. )
  314. if ctx.Req.URL.RawQuery != "" {
  315. redirectPath += "?" + ctx.Req.URL.RawQuery
  316. }
  317. ctx.Redirect(path.Join(setting.AppSubURL, redirectPath), http.StatusTemporaryRedirect)
  318. }
  319. func repoAssignment(ctx *Context, repo *repo_model.Repository) {
  320. var err error
  321. if err = repo.LoadOwner(ctx); err != nil {
  322. ctx.ServerError("LoadOwner", err)
  323. return
  324. }
  325. ctx.Repo.Permission, err = access_model.GetUserRepoPermission(ctx, repo, ctx.Doer)
  326. if err != nil {
  327. ctx.ServerError("GetUserRepoPermission", err)
  328. return
  329. }
  330. // Check access.
  331. if !ctx.Repo.Permission.HasAccess() {
  332. if ctx.FormString("go-get") == "1" {
  333. EarlyResponseForGoGetMeta(ctx)
  334. return
  335. }
  336. ctx.NotFound("no access right", nil)
  337. return
  338. }
  339. ctx.Data["HasAccess"] = true
  340. ctx.Data["Permission"] = &ctx.Repo.Permission
  341. if repo.IsMirror {
  342. pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID)
  343. if err == nil {
  344. ctx.Data["PullMirror"] = pullMirror
  345. } else if err != repo_model.ErrMirrorNotExist {
  346. ctx.ServerError("GetMirrorByRepoID", err)
  347. return
  348. }
  349. }
  350. pushMirrors, _, err := repo_model.GetPushMirrorsByRepoID(ctx, repo.ID, db.ListOptions{})
  351. if err != nil {
  352. ctx.ServerError("GetPushMirrorsByRepoID", err)
  353. return
  354. }
  355. ctx.Repo.Repository = repo
  356. ctx.Data["PushMirrors"] = pushMirrors
  357. ctx.Data["RepoName"] = ctx.Repo.Repository.Name
  358. ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty
  359. }
  360. // RepoIDAssignment returns a handler which assigns the repo to the context.
  361. func RepoIDAssignment() func(ctx *Context) {
  362. return func(ctx *Context) {
  363. repoID := ctx.ParamsInt64(":repoid")
  364. // Get repository.
  365. repo, err := repo_model.GetRepositoryByID(ctx, repoID)
  366. if err != nil {
  367. if repo_model.IsErrRepoNotExist(err) {
  368. ctx.NotFound("GetRepositoryByID", nil)
  369. } else {
  370. ctx.ServerError("GetRepositoryByID", err)
  371. }
  372. return
  373. }
  374. repoAssignment(ctx, repo)
  375. }
  376. }
  377. // RepoAssignment returns a middleware to handle repository assignment
  378. func RepoAssignment(ctx *Context) context.CancelFunc {
  379. if _, repoAssignmentOnce := ctx.Data["repoAssignmentExecuted"]; repoAssignmentOnce {
  380. log.Trace("RepoAssignment was exec already, skipping second call ...")
  381. return nil
  382. }
  383. ctx.Data["repoAssignmentExecuted"] = true
  384. var (
  385. owner *user_model.User
  386. err error
  387. )
  388. userName := ctx.Params(":username")
  389. repoName := ctx.Params(":reponame")
  390. repoName = strings.TrimSuffix(repoName, ".git")
  391. if setting.Other.EnableFeed {
  392. repoName = strings.TrimSuffix(repoName, ".rss")
  393. repoName = strings.TrimSuffix(repoName, ".atom")
  394. }
  395. // Check if the user is the same as the repository owner
  396. if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) {
  397. owner = ctx.Doer
  398. } else {
  399. owner, err = user_model.GetUserByName(ctx, userName)
  400. if err != nil {
  401. if user_model.IsErrUserNotExist(err) {
  402. // go-get does not support redirects
  403. // https://github.com/golang/go/issues/19760
  404. if ctx.FormString("go-get") == "1" {
  405. EarlyResponseForGoGetMeta(ctx)
  406. return nil
  407. }
  408. if redirectUserID, err := user_model.LookupUserRedirect(ctx, userName); err == nil {
  409. RedirectToUser(ctx.Base, userName, redirectUserID)
  410. } else if user_model.IsErrUserRedirectNotExist(err) {
  411. ctx.NotFound("GetUserByName", nil)
  412. } else {
  413. ctx.ServerError("LookupUserRedirect", err)
  414. }
  415. } else {
  416. ctx.ServerError("GetUserByName", err)
  417. }
  418. return nil
  419. }
  420. }
  421. ctx.Repo.Owner = owner
  422. ctx.ContextUser = owner
  423. ctx.Data["ContextUser"] = ctx.ContextUser
  424. ctx.Data["Username"] = ctx.Repo.Owner.Name
  425. // redirect link to wiki
  426. if strings.HasSuffix(repoName, ".wiki") {
  427. // ctx.Req.URL.Path does not have the preceding appSubURL - any redirect must have this added
  428. // Now we happen to know that all of our paths are: /:username/:reponame/whatever_else
  429. originalRepoName := ctx.Params(":reponame")
  430. redirectRepoName := strings.TrimSuffix(repoName, ".wiki")
  431. redirectRepoName += originalRepoName[len(redirectRepoName)+5:]
  432. redirectPath := strings.Replace(
  433. ctx.Req.URL.EscapedPath(),
  434. url.PathEscape(userName)+"/"+url.PathEscape(originalRepoName),
  435. url.PathEscape(userName)+"/"+url.PathEscape(redirectRepoName)+"/wiki",
  436. 1,
  437. )
  438. if ctx.Req.URL.RawQuery != "" {
  439. redirectPath += "?" + ctx.Req.URL.RawQuery
  440. }
  441. ctx.Redirect(path.Join(setting.AppSubURL, redirectPath))
  442. return nil
  443. }
  444. // Get repository.
  445. repo, err := repo_model.GetRepositoryByName(owner.ID, repoName)
  446. if err != nil {
  447. if repo_model.IsErrRepoNotExist(err) {
  448. redirectRepoID, err := repo_model.LookupRedirect(owner.ID, repoName)
  449. if err == nil {
  450. RedirectToRepo(ctx.Base, redirectRepoID)
  451. } else if repo_model.IsErrRedirectNotExist(err) {
  452. if ctx.FormString("go-get") == "1" {
  453. EarlyResponseForGoGetMeta(ctx)
  454. return nil
  455. }
  456. ctx.NotFound("GetRepositoryByName", nil)
  457. } else {
  458. ctx.ServerError("LookupRepoRedirect", err)
  459. }
  460. } else {
  461. ctx.ServerError("GetRepositoryByName", err)
  462. }
  463. return nil
  464. }
  465. repo.Owner = owner
  466. repoAssignment(ctx, repo)
  467. if ctx.Written() {
  468. return nil
  469. }
  470. ctx.Repo.RepoLink = repo.Link()
  471. ctx.Data["RepoLink"] = ctx.Repo.RepoLink
  472. ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
  473. if setting.Other.EnableFeed {
  474. ctx.Data["EnableFeed"] = true
  475. ctx.Data["FeedURL"] = ctx.Repo.RepoLink
  476. }
  477. unit, err := ctx.Repo.Repository.GetUnit(ctx, unit_model.TypeExternalTracker)
  478. if err == nil {
  479. ctx.Data["RepoExternalIssuesLink"] = unit.ExternalTrackerConfig().ExternalTrackerURL
  480. }
  481. ctx.Data["NumTags"], err = repo_model.GetReleaseCountByRepoID(ctx, ctx.Repo.Repository.ID, repo_model.FindReleasesOptions{
  482. IncludeDrafts: true,
  483. IncludeTags: true,
  484. HasSha1: util.OptionalBoolTrue, // only draft releases which are created with existing tags
  485. })
  486. if err != nil {
  487. ctx.ServerError("GetReleaseCountByRepoID", err)
  488. return nil
  489. }
  490. ctx.Data["NumReleases"], err = repo_model.GetReleaseCountByRepoID(ctx, ctx.Repo.Repository.ID, repo_model.FindReleasesOptions{})
  491. if err != nil {
  492. ctx.ServerError("GetReleaseCountByRepoID", err)
  493. return nil
  494. }
  495. ctx.Data["Title"] = owner.Name + "/" + repo.Name
  496. ctx.Data["Repository"] = repo
  497. ctx.Data["Owner"] = ctx.Repo.Repository.Owner
  498. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
  499. ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
  500. ctx.Data["RepoOwnerIsOrganization"] = repo.Owner.IsOrganization()
  501. ctx.Data["CanWriteCode"] = ctx.Repo.CanWrite(unit_model.TypeCode)
  502. ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(unit_model.TypeIssues)
  503. ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(unit_model.TypePullRequests)
  504. canSignedUserFork, err := repo_module.CanUserForkRepo(ctx, ctx.Doer, ctx.Repo.Repository)
  505. if err != nil {
  506. ctx.ServerError("CanUserForkRepo", err)
  507. return nil
  508. }
  509. ctx.Data["CanSignedUserFork"] = canSignedUserFork
  510. userAndOrgForks, err := repo_model.GetForksByUserAndOrgs(ctx, ctx.Doer, ctx.Repo.Repository)
  511. if err != nil {
  512. ctx.ServerError("GetForksByUserAndOrgs", err)
  513. return nil
  514. }
  515. ctx.Data["UserAndOrgForks"] = userAndOrgForks
  516. // canSignedUserFork is true if the current user doesn't have a fork of this repo yet or
  517. // if he owns an org that doesn't have a fork of this repo yet
  518. // If multiple forks are available or if the user can fork to another account, but there is already a fork: open selection dialog
  519. ctx.Data["ShowForkModal"] = len(userAndOrgForks) > 1 || (canSignedUserFork && len(userAndOrgForks) > 0)
  520. ctx.Data["RepoCloneLink"] = repo.CloneLink()
  521. cloneButtonShowHTTPS := !setting.Repository.DisableHTTPGit
  522. cloneButtonShowSSH := !setting.SSH.Disabled && (ctx.IsSigned || setting.SSH.ExposeAnonymous)
  523. if !cloneButtonShowHTTPS && !cloneButtonShowSSH {
  524. // We have to show at least one link, so we just show the HTTPS
  525. cloneButtonShowHTTPS = true
  526. }
  527. ctx.Data["CloneButtonShowHTTPS"] = cloneButtonShowHTTPS
  528. ctx.Data["CloneButtonShowSSH"] = cloneButtonShowSSH
  529. ctx.Data["CloneButtonOriginLink"] = ctx.Data["RepoCloneLink"] // it may be rewritten to the WikiCloneLink by the router middleware
  530. ctx.Data["RepoSearchEnabled"] = setting.Indexer.RepoIndexerEnabled
  531. if setting.Indexer.RepoIndexerEnabled {
  532. ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx)
  533. }
  534. if ctx.IsSigned {
  535. ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, repo.ID)
  536. ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, repo.ID)
  537. }
  538. if repo.IsFork {
  539. RetrieveBaseRepo(ctx, repo)
  540. if ctx.Written() {
  541. return nil
  542. }
  543. }
  544. if repo.IsGenerated() {
  545. RetrieveTemplateRepo(ctx, repo)
  546. if ctx.Written() {
  547. return nil
  548. }
  549. }
  550. isHomeOrSettings := ctx.Link == ctx.Repo.RepoLink || ctx.Link == ctx.Repo.RepoLink+"/settings" || strings.HasPrefix(ctx.Link, ctx.Repo.RepoLink+"/settings/")
  551. // Disable everything when the repo is being created
  552. if ctx.Repo.Repository.IsBeingCreated() || ctx.Repo.Repository.IsBroken() {
  553. ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
  554. if !isHomeOrSettings {
  555. ctx.Redirect(ctx.Repo.RepoLink)
  556. }
  557. return nil
  558. }
  559. gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(userName, repoName))
  560. if err != nil {
  561. if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") {
  562. log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err)
  563. ctx.Repo.Repository.MarkAsBrokenEmpty()
  564. ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
  565. // Only allow access to base of repo or settings
  566. if !isHomeOrSettings {
  567. ctx.Redirect(ctx.Repo.RepoLink)
  568. }
  569. return nil
  570. }
  571. ctx.ServerError("RepoAssignment Invalid repo "+repo_model.RepoPath(userName, repoName), err)
  572. return nil
  573. }
  574. if ctx.Repo.GitRepo != nil {
  575. ctx.Repo.GitRepo.Close()
  576. }
  577. ctx.Repo.GitRepo = gitRepo
  578. // We opened it, we should close it
  579. cancel := func() {
  580. // If it's been set to nil then assume someone else has closed it.
  581. if ctx.Repo.GitRepo != nil {
  582. ctx.Repo.GitRepo.Close()
  583. }
  584. }
  585. // Stop at this point when the repo is empty.
  586. if ctx.Repo.Repository.IsEmpty {
  587. ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
  588. return cancel
  589. }
  590. branchOpts := git_model.FindBranchOptions{
  591. RepoID: ctx.Repo.Repository.ID,
  592. IsDeletedBranch: util.OptionalBoolFalse,
  593. ListOptions: db.ListOptions{
  594. ListAll: true,
  595. },
  596. }
  597. branchesTotal, err := git_model.CountBranches(ctx, branchOpts)
  598. if err != nil {
  599. ctx.ServerError("CountBranches", err)
  600. return cancel
  601. }
  602. // non-empty repo should have at least 1 branch, so this repository's branches haven't been synced yet
  603. if branchesTotal == 0 { // fallback to do a sync immediately
  604. branchesTotal, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
  605. if err != nil {
  606. ctx.ServerError("SyncRepoBranches", err)
  607. return cancel
  608. }
  609. }
  610. ctx.Data["BranchesCount"] = branchesTotal
  611. // If no branch is set in the request URL, try to guess a default one.
  612. if len(ctx.Repo.BranchName) == 0 {
  613. if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
  614. ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
  615. } else {
  616. ctx.Repo.BranchName, _ = gitRepo.GetDefaultBranch()
  617. if ctx.Repo.BranchName == "" {
  618. // If it still can't get a default branch, fall back to default branch from setting.
  619. // Something might be wrong. Either site admin should fix the repo sync or Gitea should fix a potential bug.
  620. ctx.Repo.BranchName = setting.Repository.DefaultBranch
  621. }
  622. }
  623. ctx.Repo.RefName = ctx.Repo.BranchName
  624. }
  625. ctx.Data["BranchName"] = ctx.Repo.BranchName
  626. // People who have push access or have forked repository can propose a new pull request.
  627. canPush := ctx.Repo.CanWrite(unit_model.TypeCode) ||
  628. (ctx.IsSigned && repo_model.HasForkedRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID))
  629. canCompare := false
  630. // Pull request is allowed if this is a fork repository
  631. // and base repository accepts pull requests.
  632. if repo.BaseRepo != nil && repo.BaseRepo.AllowsPulls() {
  633. canCompare = true
  634. ctx.Data["BaseRepo"] = repo.BaseRepo
  635. ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
  636. ctx.Repo.PullRequest.Allowed = canPush
  637. ctx.Repo.PullRequest.HeadInfoSubURL = url.PathEscape(ctx.Repo.Owner.Name) + ":" + util.PathEscapeSegments(ctx.Repo.BranchName)
  638. } else if repo.AllowsPulls() {
  639. // Or, this is repository accepts pull requests between branches.
  640. canCompare = true
  641. ctx.Data["BaseRepo"] = repo
  642. ctx.Repo.PullRequest.BaseRepo = repo
  643. ctx.Repo.PullRequest.Allowed = canPush
  644. ctx.Repo.PullRequest.SameRepo = true
  645. ctx.Repo.PullRequest.HeadInfoSubURL = util.PathEscapeSegments(ctx.Repo.BranchName)
  646. }
  647. ctx.Data["CanCompareOrPull"] = canCompare
  648. ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
  649. if ctx.Repo.Repository.Status == repo_model.RepositoryPendingTransfer {
  650. repoTransfer, err := models.GetPendingRepositoryTransfer(ctx, ctx.Repo.Repository)
  651. if err != nil {
  652. ctx.ServerError("GetPendingRepositoryTransfer", err)
  653. return cancel
  654. }
  655. if err := repoTransfer.LoadAttributes(ctx); err != nil {
  656. ctx.ServerError("LoadRecipient", err)
  657. return cancel
  658. }
  659. ctx.Data["RepoTransfer"] = repoTransfer
  660. if ctx.Doer != nil {
  661. ctx.Data["CanUserAcceptTransfer"] = repoTransfer.CanUserAcceptTransfer(ctx, ctx.Doer)
  662. }
  663. }
  664. if ctx.FormString("go-get") == "1" {
  665. ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
  666. fullURLPrefix := repo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(ctx.Repo.BranchName)
  667. ctx.Data["GoDocDirectory"] = fullURLPrefix + "{/dir}"
  668. ctx.Data["GoDocFile"] = fullURLPrefix + "{/dir}/{file}#L{line}"
  669. }
  670. return cancel
  671. }
  672. // RepoRefType type of repo reference
  673. type RepoRefType int
  674. const (
  675. // RepoRefLegacy unknown type, make educated guess and redirect.
  676. // for backward compatibility with previous URL scheme
  677. RepoRefLegacy RepoRefType = iota
  678. // RepoRefAny is for usage where educated guess is needed
  679. // but redirect can not be made
  680. RepoRefAny
  681. // RepoRefBranch branch
  682. RepoRefBranch
  683. // RepoRefTag tag
  684. RepoRefTag
  685. // RepoRefCommit commit
  686. RepoRefCommit
  687. // RepoRefBlob blob
  688. RepoRefBlob
  689. )
  690. // RepoRef handles repository reference names when the ref name is not
  691. // explicitly given
  692. func RepoRef() func(*Context) context.CancelFunc {
  693. // since no ref name is explicitly specified, ok to just use branch
  694. return RepoRefByType(RepoRefBranch)
  695. }
  696. // RefTypeIncludesBranches returns true if ref type can be a branch
  697. func (rt RepoRefType) RefTypeIncludesBranches() bool {
  698. if rt == RepoRefLegacy || rt == RepoRefAny || rt == RepoRefBranch {
  699. return true
  700. }
  701. return false
  702. }
  703. // RefTypeIncludesTags returns true if ref type can be a tag
  704. func (rt RepoRefType) RefTypeIncludesTags() bool {
  705. if rt == RepoRefLegacy || rt == RepoRefAny || rt == RepoRefTag {
  706. return true
  707. }
  708. return false
  709. }
  710. func getRefNameFromPath(ctx *Base, repo *Repository, path string, isExist func(string) bool) string {
  711. refName := ""
  712. parts := strings.Split(path, "/")
  713. for i, part := range parts {
  714. refName = strings.TrimPrefix(refName+"/"+part, "/")
  715. if isExist(refName) {
  716. repo.TreePath = strings.Join(parts[i+1:], "/")
  717. return refName
  718. }
  719. }
  720. return ""
  721. }
  722. func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
  723. path := ctx.Params("*")
  724. switch pathType {
  725. case RepoRefLegacy, RepoRefAny:
  726. if refName := getRefName(ctx, repo, RepoRefBranch); len(refName) > 0 {
  727. return refName
  728. }
  729. if refName := getRefName(ctx, repo, RepoRefTag); len(refName) > 0 {
  730. return refName
  731. }
  732. // For legacy and API support only full commit sha
  733. parts := strings.Split(path, "/")
  734. if len(parts) > 0 && len(parts[0]) == git.SHAFullLength {
  735. repo.TreePath = strings.Join(parts[1:], "/")
  736. return parts[0]
  737. }
  738. if refName := getRefName(ctx, repo, RepoRefBlob); len(refName) > 0 {
  739. return refName
  740. }
  741. repo.TreePath = path
  742. return repo.Repository.DefaultBranch
  743. case RepoRefBranch:
  744. ref := getRefNameFromPath(ctx, repo, path, repo.GitRepo.IsBranchExist)
  745. if len(ref) == 0 {
  746. // maybe it's a renamed branch
  747. return getRefNameFromPath(ctx, repo, path, func(s string) bool {
  748. b, exist, err := git_model.FindRenamedBranch(ctx, repo.Repository.ID, s)
  749. if err != nil {
  750. log.Error("FindRenamedBranch", err)
  751. return false
  752. }
  753. if !exist {
  754. return false
  755. }
  756. ctx.Data["IsRenamedBranch"] = true
  757. ctx.Data["RenamedBranchName"] = b.To
  758. return true
  759. })
  760. }
  761. return ref
  762. case RepoRefTag:
  763. return getRefNameFromPath(ctx, repo, path, repo.GitRepo.IsTagExist)
  764. case RepoRefCommit:
  765. parts := strings.Split(path, "/")
  766. if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength {
  767. repo.TreePath = strings.Join(parts[1:], "/")
  768. return parts[0]
  769. }
  770. case RepoRefBlob:
  771. _, err := repo.GitRepo.GetBlob(path)
  772. if err != nil {
  773. return ""
  774. }
  775. return path
  776. default:
  777. log.Error("Unrecognized path type: %v", path)
  778. }
  779. return ""
  780. }
  781. // RepoRefByType handles repository reference name for a specific type
  782. // of repository reference
  783. func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context) context.CancelFunc {
  784. return func(ctx *Context) (cancel context.CancelFunc) {
  785. // Empty repository does not have reference information.
  786. if ctx.Repo.Repository.IsEmpty {
  787. // assume the user is viewing the (non-existent) default branch
  788. ctx.Repo.IsViewBranch = true
  789. ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
  790. ctx.Data["TreePath"] = ""
  791. return nil
  792. }
  793. var (
  794. refName string
  795. err error
  796. )
  797. if ctx.Repo.GitRepo == nil {
  798. repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  799. ctx.Repo.GitRepo, err = git.OpenRepository(ctx, repoPath)
  800. if err != nil {
  801. ctx.ServerError("RepoRef Invalid repo "+repoPath, err)
  802. return nil
  803. }
  804. // We opened it, we should close it
  805. cancel = func() {
  806. // If it's been set to nil then assume someone else has closed it.
  807. if ctx.Repo.GitRepo != nil {
  808. ctx.Repo.GitRepo.Close()
  809. }
  810. }
  811. }
  812. // Get default branch.
  813. if len(ctx.Params("*")) == 0 {
  814. refName = ctx.Repo.Repository.DefaultBranch
  815. if !ctx.Repo.GitRepo.IsBranchExist(refName) {
  816. brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 1)
  817. if err == nil && len(brs) != 0 {
  818. refName = brs[0].Name
  819. } else if len(brs) == 0 {
  820. log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path)
  821. ctx.Repo.Repository.MarkAsBrokenEmpty()
  822. } else {
  823. log.Error("GetBranches error: %v", err)
  824. ctx.Repo.Repository.MarkAsBrokenEmpty()
  825. }
  826. }
  827. ctx.Repo.RefName = refName
  828. ctx.Repo.BranchName = refName
  829. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
  830. if err == nil {
  831. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  832. } else if strings.Contains(err.Error(), "fatal: not a git repository") || strings.Contains(err.Error(), "object does not exist") {
  833. // if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users
  834. log.Error("GetBranchCommit: %v", err)
  835. ctx.Repo.Repository.MarkAsBrokenEmpty()
  836. } else {
  837. ctx.ServerError("GetBranchCommit", err)
  838. return cancel
  839. }
  840. ctx.Repo.IsViewBranch = true
  841. } else {
  842. refName = getRefName(ctx.Base, ctx.Repo, refType)
  843. ctx.Repo.RefName = refName
  844. isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)
  845. if isRenamedBranch && has {
  846. renamedBranchName := ctx.Data["RenamedBranchName"].(string)
  847. ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refName, renamedBranchName))
  848. link := setting.AppSubURL + strings.Replace(ctx.Req.URL.EscapedPath(), util.PathEscapeSegments(refName), util.PathEscapeSegments(renamedBranchName), 1)
  849. ctx.Redirect(link)
  850. return cancel
  851. }
  852. if refType.RefTypeIncludesBranches() && ctx.Repo.GitRepo.IsBranchExist(refName) {
  853. ctx.Repo.IsViewBranch = true
  854. ctx.Repo.BranchName = refName
  855. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
  856. if err != nil {
  857. ctx.ServerError("GetBranchCommit", err)
  858. return cancel
  859. }
  860. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  861. } else if refType.RefTypeIncludesTags() && ctx.Repo.GitRepo.IsTagExist(refName) {
  862. ctx.Repo.IsViewTag = true
  863. ctx.Repo.TagName = refName
  864. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName)
  865. if err != nil {
  866. if git.IsErrNotExist(err) {
  867. ctx.NotFound("GetTagCommit", err)
  868. return cancel
  869. }
  870. ctx.ServerError("GetTagCommit", err)
  871. return cancel
  872. }
  873. ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
  874. } else if len(refName) >= 7 && len(refName) <= git.SHAFullLength {
  875. ctx.Repo.IsViewCommit = true
  876. ctx.Repo.CommitID = refName
  877. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
  878. if err != nil {
  879. ctx.NotFound("GetCommit", err)
  880. return cancel
  881. }
  882. // If short commit ID add canonical link header
  883. if len(refName) < git.SHAFullLength {
  884. ctx.RespHeader().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
  885. util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), util.PathEscapeSegments(refName), url.PathEscape(ctx.Repo.Commit.ID.String()), 1))))
  886. }
  887. } else {
  888. if len(ignoreNotExistErr) > 0 && ignoreNotExistErr[0] {
  889. return cancel
  890. }
  891. ctx.NotFound("RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
  892. return cancel
  893. }
  894. if refType == RepoRefLegacy {
  895. // redirect from old URL scheme to new URL scheme
  896. prefix := strings.TrimPrefix(setting.AppSubURL+strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path, ctx.Params("*"))), strings.ToLower(ctx.Repo.RepoLink))
  897. ctx.Redirect(path.Join(
  898. ctx.Repo.RepoLink,
  899. util.PathEscapeSegments(prefix),
  900. ctx.Repo.BranchNameSubURL(),
  901. util.PathEscapeSegments(ctx.Repo.TreePath)))
  902. return cancel
  903. }
  904. }
  905. ctx.Data["BranchName"] = ctx.Repo.BranchName
  906. ctx.Data["RefName"] = ctx.Repo.RefName
  907. ctx.Data["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL()
  908. ctx.Data["TagName"] = ctx.Repo.TagName
  909. ctx.Data["CommitID"] = ctx.Repo.CommitID
  910. ctx.Data["TreePath"] = ctx.Repo.TreePath
  911. ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch
  912. ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag
  913. ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
  914. ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()
  915. ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
  916. if err != nil {
  917. ctx.ServerError("GetCommitsCount", err)
  918. return cancel
  919. }
  920. ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
  921. ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache())
  922. return cancel
  923. }
  924. }
  925. // GitHookService checks if repository Git hooks service has been enabled.
  926. func GitHookService() func(ctx *Context) {
  927. return func(ctx *Context) {
  928. if !ctx.Doer.CanEditGitHook() {
  929. ctx.NotFound("GitHookService", nil)
  930. return
  931. }
  932. }
  933. }
  934. // UnitTypes returns a middleware to set unit types to context variables.
  935. func UnitTypes() func(ctx *Context) {
  936. return func(ctx *Context) {
  937. ctx.Data["UnitTypeCode"] = unit_model.TypeCode
  938. ctx.Data["UnitTypeIssues"] = unit_model.TypeIssues
  939. ctx.Data["UnitTypePullRequests"] = unit_model.TypePullRequests
  940. ctx.Data["UnitTypeReleases"] = unit_model.TypeReleases
  941. ctx.Data["UnitTypeWiki"] = unit_model.TypeWiki
  942. ctx.Data["UnitTypeExternalWiki"] = unit_model.TypeExternalWiki
  943. ctx.Data["UnitTypeExternalTracker"] = unit_model.TypeExternalTracker
  944. ctx.Data["UnitTypeProjects"] = unit_model.TypeProjects
  945. ctx.Data["UnitTypePackages"] = unit_model.TypePackages
  946. ctx.Data["UnitTypeActions"] = unit_model.TypeActions
  947. }
  948. }