mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
176 lines
3.8 KiB
Go
176 lines
3.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v1_21
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/gitrepo"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
)
|
|
|
|
func AddRemoteAddressToMirrors(x db.EngineMigration) error {
|
|
type Mirror struct {
|
|
RemoteAddress string `xorm:"VARCHAR(2048)"`
|
|
}
|
|
|
|
type PushMirror struct {
|
|
RemoteAddress string `xorm:"VARCHAR(2048)"`
|
|
}
|
|
|
|
if err := x.Sync(new(Mirror), new(PushMirror)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := migratePullMirrors(x); err != nil {
|
|
return err
|
|
}
|
|
|
|
return migratePushMirrors(x)
|
|
}
|
|
|
|
func migratePullMirrors(x db.EngineMigration) error {
|
|
type Mirror struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
RepoID int64 `xorm:"INDEX"`
|
|
RemoteAddress string `xorm:"VARCHAR(2048)"`
|
|
RepoOwner string
|
|
RepoName string
|
|
}
|
|
|
|
sess := x.NewSession()
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
limit := setting.Database.IterateBufferSize
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
|
|
start := 0
|
|
|
|
for {
|
|
var mirrors []Mirror
|
|
if err := sess.Select("mirror.id, mirror.repo_id, mirror.remote_address, repository.owner_name as repo_owner, repository.name as repo_name").
|
|
Join("INNER", "repository", "repository.id = mirror.repo_id").
|
|
Limit(limit, start).Find(&mirrors); err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(mirrors) == 0 {
|
|
break
|
|
}
|
|
start += len(mirrors)
|
|
|
|
for _, m := range mirrors {
|
|
remoteAddress, err := getRemoteAddress(m.RepoOwner, m.RepoName, "origin")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m.RemoteAddress = remoteAddress
|
|
|
|
if _, err = sess.ID(m.ID).Cols("remote_address").Update(m); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if start%1000 == 0 { // avoid a too big transaction
|
|
if err := sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return sess.Commit()
|
|
}
|
|
|
|
func migratePushMirrors(x db.EngineMigration) error {
|
|
type PushMirror struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
RepoID int64 `xorm:"INDEX"`
|
|
RemoteName string
|
|
RemoteAddress string `xorm:"VARCHAR(2048)"`
|
|
RepoOwner string
|
|
RepoName string
|
|
}
|
|
|
|
sess := x.NewSession()
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
limit := setting.Database.IterateBufferSize
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
|
|
start := 0
|
|
|
|
for {
|
|
var mirrors []PushMirror
|
|
if err := sess.Select("push_mirror.id, push_mirror.repo_id, push_mirror.remote_name, push_mirror.remote_address, repository.owner_name as repo_owner, repository.name as repo_name").
|
|
Join("INNER", "repository", "repository.id = push_mirror.repo_id").
|
|
Limit(limit, start).Find(&mirrors); err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(mirrors) == 0 {
|
|
break
|
|
}
|
|
start += len(mirrors)
|
|
|
|
for _, m := range mirrors {
|
|
remoteAddress, err := getRemoteAddress(m.RepoOwner, m.RepoName, m.RemoteName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m.RemoteAddress = remoteAddress
|
|
|
|
if _, err = sess.ID(m.ID).Cols("remote_address").Update(m); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if start%1000 == 0 { // avoid a too big transaction
|
|
if err := sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
if err := sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return sess.Commit()
|
|
}
|
|
|
|
func getRemoteAddress(ownerName, repoName, remoteName string) (string, error) {
|
|
ctx := context.Background()
|
|
relativePath := repo_model.RelativePath(ownerName, repoName)
|
|
if exist, _ := gitrepo.IsRepositoryExist(ctx, repo_model.StorageRepo(relativePath)); !exist {
|
|
return "", nil
|
|
}
|
|
|
|
u, err := gitrepo.GitRemoteGetURL(ctx, repo_model.StorageRepo(relativePath), remoteName)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
u.User = nil
|
|
|
|
return u.String(), nil
|
|
}
|