mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-23 21:34:43 +03:00
ce3dd04c63
* Fix some mirror bugs * Remove unnecessary code * Fix lint * rename stdard url * Allow more charactors in git ssh protocol url * improve the detection * support ipv6 for git url parse * Fix bug * Fix template * Fix bug * fix template * Fix tmpl * Fix tmpl * Fix parse ssh with interface * Rename functions name Co-authored-by: zeripath <art27@cantab.net>
174 lines
4.5 KiB
Go
174 lines
4.5 KiB
Go
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
// Copyright 2018 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 repo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
)
|
|
|
|
// ErrMirrorNotExist mirror does not exist error
|
|
var ErrMirrorNotExist = errors.New("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 = GetRepositoryByIDCtx(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 {
|
|
return db.GetEngine(db.DefaultContext).
|
|
Where("next_update_unix<=?", time.Now().Unix()).
|
|
And("next_update_unix!=0").
|
|
OrderBy("updated_unix ASC").
|
|
Limit(limit).
|
|
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
|
|
}
|
|
|
|
// MirrorRepositoryList contains the mirror repositories
|
|
type MirrorRepositoryList []*Repository
|
|
|
|
func (repos MirrorRepositoryList) loadAttributes(ctx context.Context) error {
|
|
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))
|
|
if err := db.GetEngine(ctx).
|
|
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]
|
|
repos[i].Mirror.Repo = repos[i]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// LoadAttributes loads the attributes for the given MirrorRepositoryList
|
|
func (repos MirrorRepositoryList) LoadAttributes() error {
|
|
return repos.loadAttributes(db.DefaultContext)
|
|
}
|
|
|
|
// 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)
|
|
}
|