Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

repo_archiver.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2021 The Gitea 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 models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. )
  10. // RepoArchiverStatus represents repo archive status
  11. type RepoArchiverStatus int
  12. // enumerate all repo archive statuses
  13. const (
  14. RepoArchiverGenerating = iota // the archiver is generating
  15. RepoArchiverReady // it's ready
  16. )
  17. // RepoArchiver represents all archivers
  18. type RepoArchiver struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. RepoID int64 `xorm:"index unique(s)"`
  21. Repo *Repository `xorm:"-"`
  22. Type git.ArchiveType `xorm:"unique(s)"`
  23. Status RepoArchiverStatus
  24. CommitID string `xorm:"VARCHAR(40) unique(s)"`
  25. CreatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL created"`
  26. }
  27. // LoadRepo loads repository
  28. func (archiver *RepoArchiver) LoadRepo() (*Repository, error) {
  29. if archiver.Repo != nil {
  30. return archiver.Repo, nil
  31. }
  32. var repo Repository
  33. has, err := x.ID(archiver.RepoID).Get(&repo)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if !has {
  38. return nil, ErrRepoNotExist{
  39. ID: archiver.RepoID,
  40. }
  41. }
  42. return &repo, nil
  43. }
  44. // RelativePath returns relative path
  45. func (archiver *RepoArchiver) RelativePath() (string, error) {
  46. return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil
  47. }
  48. // GetRepoArchiver get an archiver
  49. func GetRepoArchiver(ctx DBContext, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
  50. var archiver RepoArchiver
  51. has, err := ctx.e.Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if has {
  56. return &archiver, nil
  57. }
  58. return nil, nil
  59. }
  60. // AddRepoArchiver adds an archiver
  61. func AddRepoArchiver(ctx DBContext, archiver *RepoArchiver) error {
  62. _, err := ctx.e.Insert(archiver)
  63. return err
  64. }
  65. // UpdateRepoArchiverStatus updates archiver's status
  66. func UpdateRepoArchiverStatus(ctx DBContext, archiver *RepoArchiver) error {
  67. _, err := ctx.e.ID(archiver.ID).Cols("status").Update(archiver)
  68. return err
  69. }
  70. // DeleteAllRepoArchives deletes all repo archives records
  71. func DeleteAllRepoArchives() error {
  72. _, err := x.Where("1=1").Delete(new(RepoArchiver))
  73. return err
  74. }