1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright 2019 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 migrations
import (
"fmt"
"path/filepath"
"strings"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
type Release struct {
ID int64
RepoID int64
Sha1 string
TagName string
}
type Repository struct {
ID int64
OwnerID int64
Name string
}
type User struct {
ID int64
Name string
}
// 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")
}
// Update release sha1
const batchSize = 100
sess := x.NewSession()
defer sess.Close()
var (
err error
count int
gitRepoCache = make(map[int64]*git.Repository)
repoCache = make(map[int64]*Repository)
userCache = make(map[int64]*User)
)
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 {
repo = new(Repository)
has, err := sess.ID(release.RepoID).Get(repo)
if err != nil {
return err
} else if !has {
return fmt.Errorf("Repository %d is not exist", release.RepoID)
}
repoCache[release.RepoID] = repo
}
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
}
gitRepo, err = git.OpenRepositoryCtx(git.DefaultContext, RepoPath(user.Name, repo.Name))
if err != nil {
return err
}
defer gitRepo.Close()
gitRepoCache[release.RepoID] = gitRepo
}
release.Sha1, err = gitRepo.GetTagCommitID(release.TagName)
if err != nil && !git.IsErrNotExist(err) {
return err
}
if err == nil {
if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
return err
}
}
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()
}
|