2019-03-11 09:14:58 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-03-11 09:14:58 +05:30
|
|
|
|
2022-11-02 14:24:36 +05:30
|
|
|
package v1_9 // nolint
|
2019-03-11 09:14:58 +05:30
|
|
|
|
|
|
|
import (
|
2019-03-20 22:07:50 +05:30
|
|
|
"fmt"
|
2020-07-22 19:57:22 +05:30
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-03-20 22:07:50 +05:30
|
|
|
|
2019-03-27 15:03:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2020-07-22 19:57:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-03-20 22:07:50 +05:30
|
|
|
|
2019-10-17 14:56:49 +05:30
|
|
|
"xorm.io/xorm"
|
2019-03-11 09:14:58 +05:30
|
|
|
)
|
|
|
|
|
2022-11-02 14:24:36 +05:30
|
|
|
func FixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
|
2019-03-11 09:14:58 +05:30
|
|
|
type Release struct {
|
|
|
|
ID int64
|
|
|
|
RepoID int64
|
|
|
|
Sha1 string
|
|
|
|
TagName string
|
|
|
|
}
|
|
|
|
|
2019-03-20 22:07:50 +05:30
|
|
|
type Repository struct {
|
|
|
|
ID int64
|
|
|
|
OwnerID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2020-07-22 19:57:22 +05:30
|
|
|
// UserPath returns the path absolute path of user repositories.
|
|
|
|
UserPath := func(userName string) string {
|
|
|
|
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepoPath returns repository path by given user and repository name.
|
|
|
|
RepoPath := func(userName, repoName string) string {
|
|
|
|
return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
|
|
|
|
}
|
|
|
|
|
2019-03-11 09:14:58 +05:30
|
|
|
// Update release sha1
|
|
|
|
const batchSize = 100
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
count int
|
|
|
|
gitRepoCache = make(map[int64]*git.Repository)
|
2019-03-20 22:07:50 +05:30
|
|
|
repoCache = make(map[int64]*Repository)
|
|
|
|
userCache = make(map[int64]*User)
|
2019-03-11 09:14:58 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for start := 0; ; start += batchSize {
|
|
|
|
releases := make([]*Release, 0, batchSize)
|
|
|
|
if err = sess.Limit(batchSize, start).Asc("id").Where("is_tag=?", false).Find(&releases); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(releases) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
|
|
|
gitRepo, ok := gitRepoCache[release.RepoID]
|
|
|
|
if !ok {
|
|
|
|
repo, ok := repoCache[release.RepoID]
|
|
|
|
if !ok {
|
2019-03-20 22:07:50 +05:30
|
|
|
repo = new(Repository)
|
|
|
|
has, err := sess.ID(release.RepoID).Get(repo)
|
2019-03-11 09:14:58 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-20 22:07:50 +05:30
|
|
|
} else if !has {
|
|
|
|
return fmt.Errorf("Repository %d is not exist", release.RepoID)
|
2019-03-11 09:14:58 +05:30
|
|
|
}
|
2019-03-20 22:07:50 +05:30
|
|
|
|
2019-03-11 09:14:58 +05:30
|
|
|
repoCache[release.RepoID] = repo
|
|
|
|
}
|
|
|
|
|
2019-03-20 22:07:50 +05:30
|
|
|
user, ok := userCache[repo.OwnerID]
|
|
|
|
if !ok {
|
|
|
|
user = new(User)
|
|
|
|
has, err := sess.ID(repo.OwnerID).Get(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return fmt.Errorf("User %d is not exist", repo.OwnerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
userCache[repo.OwnerID] = user
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err = git.OpenRepository(git.DefaultContext, RepoPath(user.Name, repo.Name))
|
2019-03-11 09:14:58 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-13 12:31:19 +05:30
|
|
|
defer gitRepo.Close()
|
2019-03-11 09:14:58 +05:30
|
|
|
gitRepoCache[release.RepoID] = gitRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
release.Sha1, err = gitRepo.GetTagCommitID(release.TagName)
|
2019-03-20 22:07:50 +05:30
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
2019-03-11 09:14:58 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-20 22:07:50 +05:30
|
|
|
if err == nil {
|
|
|
|
if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-11 09:14:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
if count >= 1000 {
|
|
|
|
if err = sess.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|