2021-06-24 06:12:38 +09:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-09-24 00:45:36 +09:00
|
|
|
"context"
|
2021-06-24 06:12:38 +09:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-06-24 06:12:38 +09:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RepoArchiverStatus represents repo archive status
|
|
|
|
type RepoArchiverStatus int
|
|
|
|
|
|
|
|
// enumerate all repo archive statuses
|
|
|
|
const (
|
|
|
|
RepoArchiverGenerating = iota // the archiver is generating
|
|
|
|
RepoArchiverReady // it's ready
|
|
|
|
)
|
|
|
|
|
|
|
|
// RepoArchiver represents all archivers
|
|
|
|
type RepoArchiver struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"index unique(s)"`
|
|
|
|
Repo *Repository `xorm:"-"`
|
|
|
|
Type git.ArchiveType `xorm:"unique(s)"`
|
|
|
|
Status RepoArchiverStatus
|
|
|
|
CommitID string `xorm:"VARCHAR(40) unique(s)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL created"`
|
|
|
|
}
|
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(RepoArchiver))
|
|
|
|
}
|
|
|
|
|
2021-06-24 06:12:38 +09:00
|
|
|
// LoadRepo loads repository
|
|
|
|
func (archiver *RepoArchiver) LoadRepo() (*Repository, error) {
|
|
|
|
if archiver.Repo != nil {
|
|
|
|
return archiver.Repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var repo Repository
|
2021-09-24 00:45:36 +09:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).ID(archiver.RepoID).Get(&repo)
|
2021-06-24 06:12:38 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, ErrRepoNotExist{
|
|
|
|
ID: archiver.RepoID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelativePath returns relative path
|
|
|
|
func (archiver *RepoArchiver) RelativePath() (string, error) {
|
2021-07-13 21:16:31 +09:00
|
|
|
return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil
|
2021-06-24 06:12:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoArchiver get an archiver
|
2021-09-24 00:45:36 +09:00
|
|
|
func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
|
2021-06-24 06:12:38 +09:00
|
|
|
var archiver RepoArchiver
|
2021-09-24 00:45:36 +09:00
|
|
|
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver)
|
2021-06-24 06:12:38 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return &archiver, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRepoArchiver adds an archiver
|
2021-09-24 00:45:36 +09:00
|
|
|
func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
|
|
|
|
_, err := db.GetEngine(ctx).Insert(archiver)
|
2021-06-24 06:12:38 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRepoArchiverStatus updates archiver's status
|
2021-09-24 00:45:36 +09:00
|
|
|
func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error {
|
|
|
|
_, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver)
|
2021-06-24 06:12:38 +09:00
|
|
|
return err
|
|
|
|
}
|
2021-07-13 21:16:31 +09:00
|
|
|
|
|
|
|
// DeleteAllRepoArchives deletes all repo archives records
|
|
|
|
func DeleteAllRepoArchives() error {
|
2021-09-24 00:45:36 +09:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Where("1=1").Delete(new(RepoArchiver))
|
2021-07-13 21:16:31 +09:00
|
|
|
return err
|
|
|
|
}
|