mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 02:33:11 +00:00
3fef47b41c
Related to https://github.com/go-gitea/gitea/pull/22262#discussion_r1059010774 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv>
123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// ErrMirrorNotExist mirror does not exist error
|
|
var ErrMirrorNotExist = util.NewNotExistErrorf("Mirror does not exist")
|
|
|
|
// Mirror represents mirror information of a repository.
|
|
type Mirror struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
RepoID int64 `xorm:"INDEX"`
|
|
Repo *Repository `xorm:"-"`
|
|
Interval time.Duration
|
|
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
|
NextUpdateUnix timeutil.TimeStamp `xorm:"INDEX"`
|
|
|
|
LFS bool `xorm:"lfs_enabled NOT NULL DEFAULT false"`
|
|
LFSEndpoint string `xorm:"lfs_endpoint TEXT"`
|
|
|
|
Address string `xorm:"-"`
|
|
}
|
|
|
|
func init() {
|
|
db.RegisterModel(new(Mirror))
|
|
}
|
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
|
func (m *Mirror) BeforeInsert() {
|
|
if m != nil {
|
|
m.UpdatedUnix = timeutil.TimeStampNow()
|
|
m.NextUpdateUnix = timeutil.TimeStampNow()
|
|
}
|
|
}
|
|
|
|
// GetRepository returns the repository.
|
|
func (m *Mirror) GetRepository() *Repository {
|
|
if m.Repo != nil {
|
|
return m.Repo
|
|
}
|
|
var err error
|
|
m.Repo, err = GetRepositoryByID(db.DefaultContext, m.RepoID)
|
|
if err != nil {
|
|
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
|
|
}
|
|
return m.Repo
|
|
}
|
|
|
|
// GetRemoteName returns the name of the remote.
|
|
func (m *Mirror) GetRemoteName() string {
|
|
return "origin"
|
|
}
|
|
|
|
// ScheduleNextUpdate calculates and sets next update time.
|
|
func (m *Mirror) ScheduleNextUpdate() {
|
|
if m.Interval != 0 {
|
|
m.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(m.Interval)
|
|
} else {
|
|
m.NextUpdateUnix = 0
|
|
}
|
|
}
|
|
|
|
// GetMirrorByRepoID returns mirror information of a repository.
|
|
func GetMirrorByRepoID(ctx context.Context, repoID int64) (*Mirror, error) {
|
|
m := &Mirror{RepoID: repoID}
|
|
has, err := db.GetEngine(ctx).Get(m)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, ErrMirrorNotExist
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// UpdateMirror updates the mirror
|
|
func UpdateMirror(ctx context.Context, m *Mirror) error {
|
|
_, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
|
|
return err
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// DeleteMirrorByRepoID deletes a mirror by repoID
|
|
func DeleteMirrorByRepoID(repoID int64) error {
|
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Mirror{RepoID: repoID})
|
|
return err
|
|
}
|
|
|
|
// MirrorsIterate iterates all mirror repositories.
|
|
func MirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
|
|
sess := db.GetEngine(db.DefaultContext).
|
|
Where("next_update_unix<=?", time.Now().Unix()).
|
|
And("next_update_unix!=0").
|
|
OrderBy("updated_unix ASC")
|
|
if limit > 0 {
|
|
sess = sess.Limit(limit)
|
|
}
|
|
return sess.Iterate(new(Mirror), f)
|
|
}
|
|
|
|
// InsertMirror inserts a mirror to database
|
|
func InsertMirror(ctx context.Context, mirror *Mirror) error {
|
|
_, err := db.GetEngine(ctx).Insert(mirror)
|
|
return err
|
|
}
|