@@ -9,13 +9,10 @@ import (
99 "fmt"
1010 "io"
1111 "strings"
12- "time"
1312
1413 "code.gitea.io/gitea/models/db"
1514 git_model "code.gitea.io/gitea/models/git"
1615 repo_model "code.gitea.io/gitea/models/repo"
17- user_model "code.gitea.io/gitea/models/user"
18- "code.gitea.io/gitea/modules/container"
1916 "code.gitea.io/gitea/modules/git"
2017 "code.gitea.io/gitea/modules/gitrepo"
2118 "code.gitea.io/gitea/modules/lfs"
@@ -59,118 +56,6 @@ func SyncRepoTags(ctx context.Context, repoID int64) error {
5956 return SyncReleasesWithTags (ctx , repo , gitRepo )
6057}
6158
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-
17459// StoreMissingLfsObjectsInRepository downloads missing LFS objects
17560func StoreMissingLfsObjectsInRepository (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , lfsClient lfs.Client ) error {
17661 contentStore := lfs .NewContentStore ()
@@ -286,18 +171,19 @@ func (shortRelease) TableName() string {
286171 return "release"
287172}
288173
289- // pullMirrorReleaseSync is a pull-mirror specific tag<->release table
174+ // SyncReleasesWithTags is a tag<->release table
290175// synchronization which overwrites all Releases from the repository tags. This
291176// 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
293178// identical to the upstream tag set. This is much more efficient for
294179// 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 )
298183 if err != nil {
299184 return fmt .Errorf ("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w" , repo .ID , repo .OwnerName , repo .Name , err )
300185 }
186+ var added , deleted , updated int
301187 err = db .WithTx (ctx , func (ctx context.Context ) error {
302188 dbReleases , err := db .Find [shortRelease ](ctx , repo_model.FindReleasesOptions {
303189 RepoID : repo .ID ,
@@ -318,9 +204,7 @@ func pullMirrorReleaseSync(ctx context.Context, repo *repo_model.Repository, git
318204 TagName : tag .Name ,
319205 LowerTagName : strings .ToLower (tag .Name ),
320206 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.
324208 NumCommits : - 1 ,
325209 CreatedUnix : timeutil .TimeStamp (tag .Tagger .When .Unix ()),
326210 IsTag : true ,
@@ -349,13 +233,14 @@ func pullMirrorReleaseSync(ctx context.Context, repo *repo_model.Repository, git
349233 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 )
350234 }
351235 }
236+ added , deleted , updated = len (deletes ), len (updates ), len (inserts )
352237 return nil
353238 })
354239 if err != nil {
355240 return fmt .Errorf ("unable to rebuild release table for pull-mirror Repo[%d:%s/%s]: %w" , repo .ID , repo .OwnerName , repo .Name , err )
356241 }
357242
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 )
359244 return nil
360245}
361246
0 commit comments