2016-08-30 17:18:33 -06:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2018-09-06 20:06:09 -06:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2016-08-30 17:18:33 -06:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
package repo
|
2016-08-30 17:18:33 -06:00
|
|
|
|
|
|
|
import (
|
2022-03-27 08:40:17 -06:00
|
|
|
"context"
|
2021-12-09 18:27:50 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-08-30 17:18:33 -06:00
|
|
|
"time"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 08:46:21 -06:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2016-08-30 17:18:33 -06:00
|
|
|
)
|
|
|
|
|
2022-01-20 10:46:10 -07:00
|
|
|
// ErrMirrorNotExist mirror does not exist error
|
|
|
|
var ErrMirrorNotExist = errors.New("Mirror does not exist")
|
2021-12-09 18:27:50 -07:00
|
|
|
|
2016-08-30 17:18:33 -06:00
|
|
|
// Mirror represents mirror information of a repository.
|
|
|
|
type Mirror struct {
|
2017-01-06 08:14:33 -07:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX"`
|
2016-08-30 17:18:33 -06:00
|
|
|
Repo *Repository `xorm:"-"`
|
2017-04-08 09:27:26 -06:00
|
|
|
Interval time.Duration
|
|
|
|
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
|
2016-08-30 17:18:33 -06:00
|
|
|
|
2019-08-15 08:46:21 -06:00
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
|
|
|
NextUpdateUnix timeutil.TimeStamp `xorm:"INDEX"`
|
2016-08-30 17:18:33 -06:00
|
|
|
|
2021-04-08 16:25:57 -06:00
|
|
|
LFS bool `xorm:"lfs_enabled NOT NULL DEFAULT false"`
|
|
|
|
LFSEndpoint string `xorm:"lfs_endpoint TEXT"`
|
|
|
|
|
2019-10-01 07:40:17 -06:00
|
|
|
Address string `xorm:"-"`
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Mirror))
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:30:21 -07:00
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
2016-08-30 17:18:33 -06:00
|
|
|
func (m *Mirror) BeforeInsert() {
|
2017-09-12 23:18:22 -06:00
|
|
|
if m != nil {
|
2019-08-15 08:46:21 -06:00
|
|
|
m.UpdatedUnix = timeutil.TimeStampNow()
|
|
|
|
m.NextUpdateUnix = timeutil.TimeStampNow()
|
2017-09-12 23:18:22 -06:00
|
|
|
}
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
// GetRepository returns the repository.
|
|
|
|
func (m *Mirror) GetRepository() *Repository {
|
|
|
|
if m.Repo != nil {
|
|
|
|
return m.Repo
|
2017-09-12 23:18:22 -06:00
|
|
|
}
|
2016-08-30 17:18:33 -06:00
|
|
|
var err error
|
2022-05-20 08:08:52 -06:00
|
|
|
m.Repo, err = GetRepositoryByIDCtx(db.DefaultContext, m.RepoID)
|
2017-10-01 10:52:35 -06:00
|
|
|
if err != nil {
|
2019-04-02 01:48:31 -06:00
|
|
|
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
2021-06-14 11:20:43 -06:00
|
|
|
return m.Repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteName returns the name of the remote.
|
|
|
|
func (m *Mirror) GetRemoteName() string {
|
|
|
|
return "origin"
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:18:33 -06:00
|
|
|
// ScheduleNextUpdate calculates and sets next update time.
|
|
|
|
func (m *Mirror) ScheduleNextUpdate() {
|
2018-11-08 16:58:02 -07:00
|
|
|
if m.Interval != 0 {
|
2019-08-15 08:46:21 -06:00
|
|
|
m.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(m.Interval)
|
2018-11-08 16:58:02 -07:00
|
|
|
} else {
|
|
|
|
m.NextUpdateUnix = 0
|
|
|
|
}
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
// GetMirrorByRepoID returns mirror information of a repository.
|
|
|
|
func GetMirrorByRepoID(ctx context.Context, repoID int64) (*Mirror, error) {
|
2016-08-30 17:18:33 -06:00
|
|
|
m := &Mirror{RepoID: repoID}
|
2022-05-20 08:08:52 -06:00
|
|
|
has, err := db.GetEngine(ctx).Get(m)
|
2016-08-30 17:18:33 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrMirrorNotExist
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:30:21 -07:00
|
|
|
// UpdateMirror updates the mirror
|
2022-05-20 08:08:52 -06:00
|
|
|
func UpdateMirror(ctx context.Context, m *Mirror) error {
|
|
|
|
_, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
|
|
|
|
return err
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
2022-03-27 08:40:17 -06:00
|
|
|
// TouchMirror updates the mirror updatedUnix
|
|
|
|
func TouchMirror(ctx context.Context, m *Mirror) error {
|
|
|
|
m.UpdatedUnix = timeutil.TimeStampNow()
|
|
|
|
_, err := db.GetEngine(ctx).ID(m.ID).Cols("updated_unix").Update(m)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:30:21 -07:00
|
|
|
// DeleteMirrorByRepoID deletes a mirror by repoID
|
2016-08-30 17:18:33 -06:00
|
|
|
func DeleteMirrorByRepoID(repoID int64) error {
|
2021-09-23 09:45:36 -06:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Mirror{RepoID: repoID})
|
2016-08-30 17:18:33 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 07:40:17 -06:00
|
|
|
// MirrorsIterate iterates all mirror repositories.
|
2022-02-28 12:41:06 -07:00
|
|
|
func MirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
|
2021-09-23 09:45:36 -06:00
|
|
|
return db.GetEngine(db.DefaultContext).
|
2016-11-10 08:16:32 -07:00
|
|
|
Where("next_update_unix<=?", time.Now().Unix()).
|
2018-11-08 16:58:02 -07:00
|
|
|
And("next_update_unix!=0").
|
2021-11-22 20:09:35 -07:00
|
|
|
OrderBy("updated_unix ASC").
|
2022-02-28 12:41:06 -07:00
|
|
|
Limit(limit).
|
2019-10-01 07:40:17 -06:00
|
|
|
Iterate(new(Mirror), f)
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
2019-12-14 10:30:01 -07:00
|
|
|
|
|
|
|
// InsertMirror inserts a mirror to database
|
2022-06-06 02:01:49 -06:00
|
|
|
func InsertMirror(ctx context.Context, mirror *Mirror) error {
|
|
|
|
_, err := db.GetEngine(ctx).Insert(mirror)
|
2019-12-14 10:30:01 -07:00
|
|
|
return err
|
|
|
|
}
|
2021-12-09 18:27:50 -07:00
|
|
|
|
|
|
|
// MirrorRepositoryList contains the mirror repositories
|
|
|
|
type MirrorRepositoryList []*Repository
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
func (repos MirrorRepositoryList) loadAttributes(ctx context.Context) error {
|
2021-12-09 18:27:50 -07:00
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load mirrors.
|
|
|
|
repoIDs := make([]int64, 0, len(repos))
|
|
|
|
for i := range repos {
|
|
|
|
if !repos[i].IsMirror {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDs = append(repoIDs, repos[i].ID)
|
|
|
|
}
|
|
|
|
mirrors := make([]*Mirror, 0, len(repoIDs))
|
2022-05-20 08:08:52 -06:00
|
|
|
if err := db.GetEngine(ctx).
|
2021-12-09 18:27:50 -07:00
|
|
|
Where("id > 0").
|
|
|
|
In("repo_id", repoIDs).
|
|
|
|
Find(&mirrors); err != nil {
|
|
|
|
return fmt.Errorf("find mirrors: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
set := make(map[int64]*Mirror)
|
|
|
|
for i := range mirrors {
|
|
|
|
set[mirrors[i].RepoID] = mirrors[i]
|
|
|
|
}
|
|
|
|
for i := range repos {
|
|
|
|
repos[i].Mirror = set[repos[i].ID]
|
2022-05-20 08:08:52 -06:00
|
|
|
repos[i].Mirror.Repo = repos[i]
|
2021-12-09 18:27:50 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAttributes loads the attributes for the given MirrorRepositoryList
|
|
|
|
func (repos MirrorRepositoryList) LoadAttributes() error {
|
2022-05-20 08:08:52 -06:00
|
|
|
return repos.loadAttributes(db.DefaultContext)
|
2021-12-09 18:27:50 -07:00
|
|
|
}
|
2022-06-06 02:01:49 -06:00
|
|
|
|
|
|
|
// GetUserMirrorRepositories returns a list of mirror repositories of given user.
|
|
|
|
func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
|
|
|
|
repos := make([]*Repository, 0, 10)
|
|
|
|
return repos, db.GetEngine(db.DefaultContext).
|
|
|
|
Where("owner_id = ?", userID).
|
|
|
|
And("is_mirror = ?", true).
|
|
|
|
Find(&repos)
|
|
|
|
}
|