@@ -9,13 +9,10 @@ import (
9
9
"fmt"
10
10
"io"
11
11
"strings"
12
- "time"
13
12
14
13
"code.gitea.io/gitea/models/db"
15
14
git_model "code.gitea.io/gitea/models/git"
16
15
repo_model "code.gitea.io/gitea/models/repo"
17
- user_model "code.gitea.io/gitea/models/user"
18
- "code.gitea.io/gitea/modules/container"
19
16
"code.gitea.io/gitea/modules/git"
20
17
"code.gitea.io/gitea/modules/gitrepo"
21
18
"code.gitea.io/gitea/modules/lfs"
@@ -59,118 +56,6 @@ func SyncRepoTags(ctx context.Context, repoID int64) error {
59
56
return SyncReleasesWithTags (ctx , repo , gitRepo )
60
57
}
61
58
62
- // SyncReleasesWithTags synchronizes release table with repository tags
63
- func SyncReleasesWithTags (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository ) error {
64
- log .Debug ("SyncReleasesWithTags: in Repo[%d:%s/%s]" , repo .ID , repo .OwnerName , repo .Name )
65
-
66
- // optimized procedure for pull-mirrors which saves a lot of time (in
67
- // particular for repos with many tags).
68
- if repo .IsMirror {
69
- return pullMirrorReleaseSync (ctx , repo , gitRepo )
70
- }
71
-
72
- existingRelTags := make (container.Set [string ])
73
- opts := repo_model.FindReleasesOptions {
74
- IncludeDrafts : true ,
75
- IncludeTags : true ,
76
- ListOptions : db.ListOptions {PageSize : 50 },
77
- RepoID : repo .ID ,
78
- }
79
- for page := 1 ; ; page ++ {
80
- opts .Page = page
81
- rels , err := db .Find [repo_model.Release ](gitRepo .Ctx , opts )
82
- if err != nil {
83
- return fmt .Errorf ("unable to GetReleasesByRepoID in Repo[%d:%s/%s]: %w" , repo .ID , repo .OwnerName , repo .Name , err )
84
- }
85
- if len (rels ) == 0 {
86
- break
87
- }
88
- for _ , rel := range rels {
89
- if rel .IsDraft {
90
- continue
91
- }
92
- commitID , err := gitRepo .GetTagCommitID (rel .TagName )
93
- if err != nil && ! git .IsErrNotExist (err ) {
94
- return fmt .Errorf ("unable to GetTagCommitID for %q in Repo[%d:%s/%s]: %w" , rel .TagName , repo .ID , repo .OwnerName , repo .Name , err )
95
- }
96
- if git .IsErrNotExist (err ) || commitID != rel .Sha1 {
97
- if err := repo_model .PushUpdateDeleteTag (ctx , repo , rel .TagName ); err != nil {
98
- return fmt .Errorf ("unable to PushUpdateDeleteTag: %q in Repo[%d:%s/%s]: %w" , rel .TagName , repo .ID , repo .OwnerName , repo .Name , err )
99
- }
100
- } else {
101
- existingRelTags .Add (strings .ToLower (rel .TagName ))
102
- }
103
- }
104
- }
105
-
106
- _ , err := gitRepo .WalkReferences (git .ObjectTag , 0 , 0 , func (sha1 , refname string ) error {
107
- tagName := strings .TrimPrefix (refname , git .TagPrefix )
108
- if existingRelTags .Contains (strings .ToLower (tagName )) {
109
- return nil
110
- }
111
-
112
- if err := PushUpdateAddTag (ctx , repo , gitRepo , tagName , sha1 , refname ); err != nil {
113
- // sometimes, some tags will be sync failed. i.e. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tag/?h=v2.6.11
114
- // this is a tree object, not a tag object which created before git
115
- log .Error ("unable to PushUpdateAddTag: %q to Repo[%d:%s/%s]: %v" , tagName , repo .ID , repo .OwnerName , repo .Name , err )
116
- }
117
-
118
- return nil
119
- })
120
- return err
121
- }
122
-
123
- // PushUpdateAddTag must be called for any push actions to add tag
124
- func PushUpdateAddTag (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , tagName , sha1 , refname string ) error {
125
- tag , err := gitRepo .GetTagWithID (sha1 , tagName )
126
- if err != nil {
127
- return fmt .Errorf ("unable to GetTag: %w" , err )
128
- }
129
- commit , err := gitRepo .GetTagCommit (tag .Name )
130
- if err != nil {
131
- return fmt .Errorf ("unable to get tag Commit: %w" , err )
132
- }
133
-
134
- sig := tag .Tagger
135
- if sig == nil {
136
- sig = commit .Author
137
- }
138
- if sig == nil {
139
- sig = commit .Committer
140
- }
141
-
142
- var author * user_model.User
143
- createdAt := time .Unix (1 , 0 )
144
-
145
- if sig != nil {
146
- author , err = user_model .GetUserByEmail (ctx , sig .Email )
147
- if err != nil && ! user_model .IsErrUserNotExist (err ) {
148
- return fmt .Errorf ("unable to GetUserByEmail for %q: %w" , sig .Email , err )
149
- }
150
- createdAt = sig .When
151
- }
152
-
153
- commitsCount , err := commit .CommitsCount ()
154
- if err != nil {
155
- return fmt .Errorf ("unable to get CommitsCount: %w" , err )
156
- }
157
-
158
- rel := repo_model.Release {
159
- RepoID : repo .ID ,
160
- TagName : tagName ,
161
- LowerTagName : strings .ToLower (tagName ),
162
- Sha1 : commit .ID .String (),
163
- NumCommits : commitsCount ,
164
- CreatedUnix : timeutil .TimeStamp (createdAt .Unix ()),
165
- IsTag : true ,
166
- }
167
- if author != nil {
168
- rel .PublisherID = author .ID
169
- }
170
-
171
- return repo_model .SaveOrUpdateTag (ctx , repo , & rel )
172
- }
173
-
174
59
// StoreMissingLfsObjectsInRepository downloads missing LFS objects
175
60
func StoreMissingLfsObjectsInRepository (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , lfsClient lfs.Client ) error {
176
61
contentStore := lfs .NewContentStore ()
@@ -286,18 +171,19 @@ func (shortRelease) TableName() string {
286
171
return "release"
287
172
}
288
173
289
- // pullMirrorReleaseSync is a pull-mirror specific tag<->release table
174
+ // SyncReleasesWithTags is a tag<->release table
290
175
// synchronization which overwrites all Releases from the repository tags. This
291
176
// can be relied on since a pull-mirror is always identical to its
292
- // upstream. Hence, after each sync we want the pull-mirror release set to be
177
+ // upstream. Hence, after each sync we want the release set to be
293
178
// identical to the upstream tag set. This is much more efficient for
294
179
// repositories like https://github.com/vim/vim (with over 13000 tags).
295
- func pullMirrorReleaseSync (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository ) error {
296
- log .Trace ( "pullMirrorReleaseSync: rebuilding releases for pull-mirror Repo[%d:%s/%s]" , repo .ID , repo .OwnerName , repo .Name )
297
- tags , numTags , err := gitRepo .GetTagInfos (0 , 0 )
180
+ func SyncReleasesWithTags (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository ) error {
181
+ log .Debug ( "SyncReleasesWithTags: in Repo[%d:%s/%s]" , repo .ID , repo .OwnerName , repo .Name )
182
+ tags , _ , err := gitRepo .GetTagInfos (0 , 0 )
298
183
if err != nil {
299
184
return fmt .Errorf ("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w" , repo .ID , repo .OwnerName , repo .Name , err )
300
185
}
186
+ var added , deleted , updated int
301
187
err = db .WithTx (ctx , func (ctx context.Context ) error {
302
188
dbReleases , err := db .Find [shortRelease ](ctx , repo_model.FindReleasesOptions {
303
189
RepoID : repo .ID ,
@@ -318,9 +204,7 @@ func pullMirrorReleaseSync(ctx context.Context, repo *repo_model.Repository, git
318
204
TagName : tag .Name ,
319
205
LowerTagName : strings .ToLower (tag .Name ),
320
206
Sha1 : tag .Object .String (),
321
- // NOTE: ignored, since NumCommits are unused
322
- // for pull-mirrors (only relevant when
323
- // displaying releases, IsTag: false)
207
+ // NOTE: ignored, The NumCommits value is calculated and cached on demand when the UI requires it.
324
208
NumCommits : - 1 ,
325
209
CreatedUnix : timeutil .TimeStamp (tag .Tagger .When .Unix ()),
326
210
IsTag : true ,
@@ -349,13 +233,14 @@ func pullMirrorReleaseSync(ctx context.Context, repo *repo_model.Repository, git
349
233
return fmt .Errorf ("unable to update tag %s for pull-mirror Repo[%d:%s/%s]: %w" , tag .Name , repo .ID , repo .OwnerName , repo .Name , err )
350
234
}
351
235
}
236
+ added , deleted , updated = len (deletes ), len (updates ), len (inserts )
352
237
return nil
353
238
})
354
239
if err != nil {
355
240
return fmt .Errorf ("unable to rebuild release table for pull-mirror Repo[%d:%s/%s]: %w" , repo .ID , repo .OwnerName , repo .Name , err )
356
241
}
357
242
358
- log .Trace ("pullMirrorReleaseSync: done rebuilding %d releases " , numTags )
243
+ log .Trace ("SyncReleasesWithTags: %d tags added, %d tags deleted, %d tags updated " , added , deleted , updated )
359
244
return nil
360
245
}
361
246
0 commit comments