2016-08-30 18:18:33 -05:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2018-09-06 21:06:09 -05:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2016-08-30 18:18:33 -05:00
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
package repo
|
2016-08-30 18:18:33 -05:00
|
|
|
|
|
|
|
import (
|
2022-03-27 09:40:17 -05:00
|
|
|
"context"
|
2016-08-30 18:18:33 -05:00
|
|
|
"time"
|
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 09:46:21 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2022-12-31 06:49:37 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2016-08-30 18:18:33 -05:00
|
|
|
)
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
// ErrMirrorNotExist mirror does not exist error
|
2022-12-31 06:49:37 -05:00
|
|
|
var ErrMirrorNotExist = util.NewNotExistErrorf("Mirror does not exist")
|
2021-12-09 20:27:50 -05:00
|
|
|
|
2016-08-30 18:18:33 -05:00
|
|
|
// Mirror represents mirror information of a repository.
|
|
|
|
type Mirror struct {
|
2017-01-06 10:14:33 -05:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX"`
|
2016-08-30 18:18:33 -05:00
|
|
|
Repo *Repository `xorm:"-"`
|
2017-04-08 10:27:26 -05:00
|
|
|
Interval time.Duration
|
|
|
|
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
|
2016-08-30 18:18:33 -05:00
|
|
|
|
2019-08-15 09:46:21 -05:00
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
|
|
|
NextUpdateUnix timeutil.TimeStamp `xorm:"INDEX"`
|
2016-08-30 18:18:33 -05:00
|
|
|
|
2021-04-08 17:25:57 -05:00
|
|
|
LFS bool `xorm:"lfs_enabled NOT NULL DEFAULT false"`
|
|
|
|
LFSEndpoint string `xorm:"lfs_endpoint TEXT"`
|
|
|
|
|
2019-10-01 08:40:17 -05:00
|
|
|
Address string `xorm:"-"`
|
2016-08-30 18:18:33 -05:00
|
|
|
}
|
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Mirror))
|
|
|
|
}
|
|
|
|
|
2016-11-25 19:30:21 -05:00
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
2016-08-30 18:18:33 -05:00
|
|
|
func (m *Mirror) BeforeInsert() {
|
2017-09-13 00:18:22 -05:00
|
|
|
if m != nil {
|
2019-08-15 09:46:21 -05:00
|
|
|
m.UpdatedUnix = timeutil.TimeStampNow()
|
|
|
|
m.NextUpdateUnix = timeutil.TimeStampNow()
|
2017-09-13 00:18:22 -05:00
|
|
|
}
|
2016-08-30 18:18:33 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
// GetRepository returns the repository.
|
|
|
|
func (m *Mirror) GetRepository() *Repository {
|
|
|
|
if m.Repo != nil {
|
|
|
|
return m.Repo
|
2017-09-13 00:18:22 -05:00
|
|
|
}
|
2016-08-30 18:18:33 -05:00
|
|
|
var err error
|
2022-12-02 21:48:26 -05:00
|
|
|
m.Repo, err = GetRepositoryByID(db.DefaultContext, m.RepoID)
|
2017-10-01 11:52:35 -05:00
|
|
|
if err != nil {
|
2019-04-02 02:48:31 -05:00
|
|
|
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
|
2016-08-30 18:18:33 -05:00
|
|
|
}
|
2021-06-14 12:20:43 -05:00
|
|
|
return m.Repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteName returns the name of the remote.
|
|
|
|
func (m *Mirror) GetRemoteName() string {
|
|
|
|
return "origin"
|
|
|
|
}
|
|
|
|
|
2016-08-30 18:18:33 -05:00
|
|
|
// ScheduleNextUpdate calculates and sets next update time.
|
|
|
|
func (m *Mirror) ScheduleNextUpdate() {
|
2018-11-08 18:58:02 -05:00
|
|
|
if m.Interval != 0 {
|
2019-08-15 09:46:21 -05:00
|
|
|
m.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(m.Interval)
|
2018-11-08 18:58:02 -05:00
|
|
|
} else {
|
|
|
|
m.NextUpdateUnix = 0
|
|
|
|
}
|
2016-08-30 18:18:33 -05:00
|
|
|
}
|
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
// GetMirrorByRepoID returns mirror information of a repository.
|
|
|
|
func GetMirrorByRepoID(ctx context.Context, repoID int64) (*Mirror, error) {
|
2016-08-30 18:18:33 -05:00
|
|
|
m := &Mirror{RepoID: repoID}
|
2022-05-20 09:08:52 -05:00
|
|
|
has, err := db.GetEngine(ctx).Get(m)
|
2016-08-30 18:18:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrMirrorNotExist
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2016-11-25 19:30:21 -05:00
|
|
|
// UpdateMirror updates the mirror
|
2022-05-20 09:08:52 -05:00
|
|
|
func UpdateMirror(ctx context.Context, m *Mirror) error {
|
|
|
|
_, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
|
|
|
|
return err
|
2016-08-30 18:18:33 -05:00
|
|
|
}
|
|
|
|
|
2022-03-27 09:40:17 -05: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 19:30:21 -05:00
|
|
|
// DeleteMirrorByRepoID deletes a mirror by repoID
|
2016-08-30 18:18:33 -05:00
|
|
|
func DeleteMirrorByRepoID(repoID int64) error {
|
2021-09-23 10:45:36 -05:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Mirror{RepoID: repoID})
|
2016-08-30 18:18:33 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 08:40:17 -05:00
|
|
|
// MirrorsIterate iterates all mirror repositories.
|
2023-07-04 22:41:32 -05:00
|
|
|
func MirrorsIterate(limit int, f func(idx int, bean any) error) error {
|
2022-08-18 21:12:00 -05:00
|
|
|
sess := db.GetEngine(db.DefaultContext).
|
2016-11-10 10:16:32 -05:00
|
|
|
Where("next_update_unix<=?", time.Now().Unix()).
|
2018-11-08 18:58:02 -05:00
|
|
|
And("next_update_unix!=0").
|
2022-08-18 21:12:00 -05:00
|
|
|
OrderBy("updated_unix ASC")
|
|
|
|
if limit > 0 {
|
|
|
|
sess = sess.Limit(limit)
|
|
|
|
}
|
|
|
|
return sess.Iterate(new(Mirror), f)
|
2016-08-30 18:18:33 -05:00
|
|
|
}
|
2019-12-14 12:30:01 -05:00
|
|
|
|
|
|
|
// InsertMirror inserts a mirror to database
|
2022-06-06 03:01:49 -05:00
|
|
|
func InsertMirror(ctx context.Context, mirror *Mirror) error {
|
|
|
|
_, err := db.GetEngine(ctx).Insert(mirror)
|
2019-12-14 12:30:01 -05:00
|
|
|
return err
|
|
|
|
}
|