Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func runRepoSyncReleases(ctx context.Context, _ *cli.Command) error {
}
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)

if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
if _, err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
log.Warn(" SyncReleasesWithTags: %v", err)
gitRepo.Close()
continue
Expand Down
46 changes: 35 additions & 11 deletions modules/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (
"code.gitea.io/gitea/modules/timeutil"
)

// SyncResult describes a reference update detected during sync.
type SyncResult struct {
RefName git.RefName
OldCommitID string
NewCommitID string
}

// SyncRepoBranches synchronizes branch table with repository branches
func SyncRepoBranches(ctx context.Context, repoID, doerID int64) (int64, error) {
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
Expand All @@ -33,26 +40,27 @@ func SyncRepoBranches(ctx context.Context, repoID, doerID int64) (int64, error)
}
defer gitRepo.Close()

return SyncRepoBranchesWithRepo(ctx, repo, gitRepo, doerID)
count, _, err := SyncRepoBranchesWithRepo(ctx, repo, gitRepo, doerID)
return count, err
}

func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, doerID int64) (int64, error) {
func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, doerID int64) (int64, []*SyncResult, error) {
objFmt, err := gitRepo.GetObjectFormat()
if err != nil {
return 0, fmt.Errorf("GetObjectFormat: %w", err)
return 0, nil, fmt.Errorf("GetObjectFormat: %w", err)
}
if objFmt.Name() != repo.ObjectFormatName {
repo.ObjectFormatName = objFmt.Name()
if err = repo_model.UpdateRepositoryColsWithAutoTime(ctx, repo, "object_format_name"); err != nil {
return 0, fmt.Errorf("UpdateRepositoryColsWithAutoTime: %w", err)
return 0, nil, fmt.Errorf("UpdateRepositoryColsWithAutoTime: %w", err)
}
}

allBranches := container.Set[string]{}
{
branches, _, err := gitRepo.GetBranchNames(0, 0)
if err != nil {
return 0, err
return 0, nil, err
}
log.Trace("SyncRepoBranches[%s]: branches[%d]: %v", repo.FullName(), len(branches), branches)
for _, branch := range branches {
Expand All @@ -67,7 +75,7 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository,
RepoID: repo.ID,
})
if err != nil {
return 0, err
return 0, nil, err
}
for _, branch := range branches {
dbBranches[branch.Name] = branch
Expand All @@ -77,11 +85,12 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository,
var toAdd []*git_model.Branch
var toUpdate []*git_model.Branch
var toRemove []int64
var syncResults []*SyncResult
for branch := range allBranches {
dbb := dbBranches[branch]
commit, err := gitRepo.GetBranchCommit(branch)
if err != nil {
return 0, err
return 0, nil, err
}
if dbb == nil {
toAdd = append(toAdd, &git_model.Branch{
Expand All @@ -92,7 +101,12 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository,
PusherID: doerID,
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
})
} else if commit.ID.String() != dbb.CommitID {
syncResults = append(syncResults, &SyncResult{
RefName: git.RefNameFromBranch(branch),
OldCommitID: "",
NewCommitID: commit.ID.String(),
})
} else if commit.ID.String() != dbb.CommitID || dbb.IsDeleted {
toUpdate = append(toUpdate, &git_model.Branch{
ID: dbb.ID,
RepoID: repo.ID,
Expand All @@ -102,19 +116,29 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository,
PusherID: doerID,
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
})
syncResults = append(syncResults, &SyncResult{
RefName: git.RefNameFromBranch(branch),
OldCommitID: dbb.CommitID,
NewCommitID: commit.ID.String(),
})
}
}

for _, dbBranch := range dbBranches {
if !allBranches.Contains(dbBranch.Name) && !dbBranch.IsDeleted {
toRemove = append(toRemove, dbBranch.ID)
syncResults = append(syncResults, &SyncResult{
RefName: git.RefNameFromBranch(dbBranch.Name),
OldCommitID: dbBranch.CommitID,
NewCommitID: "",
})
}
}

log.Trace("SyncRepoBranches[%s]: toAdd: %v, toUpdate: %v, toRemove: %v", repo.FullName(), toAdd, toUpdate, toRemove)

if len(toAdd) == 0 && len(toRemove) == 0 && len(toUpdate) == 0 {
return int64(len(allBranches)), nil
return int64(len(allBranches)), syncResults, nil
}

if err := db.WithTx(ctx, func(ctx context.Context) error {
Expand All @@ -140,7 +164,7 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository,

return nil
}); err != nil {
return 0, err
return 0, nil, err
}
return int64(len(allBranches)), nil
return int64(len(allBranches)), syncResults, nil
}
50 changes: 45 additions & 5 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func SyncRepoTags(ctx context.Context, repoID int64) error {
}
defer gitRepo.Close()

return SyncReleasesWithTags(ctx, repo, gitRepo)
_, err = SyncReleasesWithTags(ctx, repo, gitRepo)
return err
}

// StoreMissingLfsObjectsInRepository downloads missing LFS objects
Expand Down Expand Up @@ -178,13 +179,14 @@ func (shortRelease) TableName() string {
// upstream. Hence, after each sync we want the release set to be
// identical to the upstream tag set. This is much more efficient for
// repositories like https://github.com/vim/vim (with over 13000 tags).
func SyncReleasesWithTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository) error {
func SyncReleasesWithTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository) ([]*SyncResult, error) {
log.Debug("SyncReleasesWithTags: in Repo[%d:%s/%s]", repo.ID, repo.OwnerName, repo.Name)
tags, _, err := gitRepo.GetTagInfos(0, 0)
if err != nil {
return fmt.Errorf("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
return nil, fmt.Errorf("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
}
var added, deleted, updated int
var syncResults []*SyncResult
err = db.WithTx(ctx, func(ctx context.Context) error {
dbReleases, err := db.Find[shortRelease](ctx, repo_model.FindReleasesOptions{
RepoID: repo.ID,
Expand All @@ -195,7 +197,45 @@ func SyncReleasesWithTags(ctx context.Context, repo *repo_model.Repository, gitR
return fmt.Errorf("unable to FindReleases in pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
}

dbReleasesByID := make(map[int64]*shortRelease, len(dbReleases))
dbReleasesByTag := make(map[string]*shortRelease, len(dbReleases))
for _, release := range dbReleases {
dbReleasesByID[release.ID] = release
dbReleasesByTag[release.TagName] = release
}

inserts, deletes, updates := calcSync(tags, dbReleases)
syncResults = make([]*SyncResult, 0, len(inserts)+len(deletes)+len(updates))
for _, tag := range inserts {
syncResults = append(syncResults, &SyncResult{
RefName: git.RefNameFromTag(tag.Name),
OldCommitID: "",
NewCommitID: tag.Object.String(),
})
}
for _, deleteID := range deletes {
release := dbReleasesByID[deleteID]
if release == nil {
continue
}
syncResults = append(syncResults, &SyncResult{
RefName: git.RefNameFromTag(release.TagName),
OldCommitID: release.Sha1,
NewCommitID: "",
})
}
for _, tag := range updates {
release := dbReleasesByTag[tag.Name]
oldSha := ""
if release != nil {
oldSha = release.Sha1
}
syncResults = append(syncResults, &SyncResult{
RefName: git.RefNameFromTag(tag.Name),
OldCommitID: oldSha,
NewCommitID: tag.Object.String(),
})
}
//
// make release set identical to upstream tags
//
Expand Down Expand Up @@ -238,11 +278,11 @@ func SyncReleasesWithTags(ctx context.Context, repo *repo_model.Repository, gitR
return nil
})
if err != nil {
return fmt.Errorf("unable to rebuild release table for pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
return nil, fmt.Errorf("unable to rebuild release table for pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err)
}

log.Trace("SyncReleasesWithTags: %d tags added, %d tags deleted, %d tags updated", added, deleted, updated)
return nil
return syncResults, nil
}

func calcSync(destTags []*git.Tag, dbTags []*shortRelease) ([]*git.Tag, []int64, []*git.Tag) {
Expand Down
6 changes: 3 additions & 3 deletions routers/web/feed/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ func feedActionsToFeedItems(ctx *context.Context, actions activities_model.Actio
if link.Href == "#" {
link.Href = srcLink
}
titleExtra = ctx.Locale.Tr("action.mirror_sync_push", act.GetRepoAbsoluteLink(ctx), srcLink, act.GetBranch(), act.ShortRepoPath(ctx))
titleExtra = ctx.Locale.Tr("action.mirror_sync_push", act.GetRepoAbsoluteLink(ctx), srcLink, act.RefName, act.ShortRepoPath(ctx))
case activities_model.ActionMirrorSyncCreate:
srcLink := toSrcLink(ctx, act)
if link.Href == "#" {
link.Href = srcLink
}
titleExtra = ctx.Locale.Tr("action.mirror_sync_create", act.GetRepoAbsoluteLink(ctx), srcLink, act.GetBranch(), act.ShortRepoPath(ctx))
titleExtra = ctx.Locale.Tr("action.mirror_sync_create", act.GetRepoAbsoluteLink(ctx), srcLink, act.RefName, act.ShortRepoPath(ctx))
case activities_model.ActionMirrorSyncDelete:
link.Href = act.GetRepoAbsoluteLink(ctx)
titleExtra = ctx.Locale.Tr("action.mirror_sync_delete", act.GetRepoAbsoluteLink(ctx), act.GetBranch(), act.ShortRepoPath(ctx))
titleExtra = ctx.Locale.Tr("action.mirror_sync_delete", act.GetRepoAbsoluteLink(ctx), act.RefName, act.ShortRepoPath(ctx))
case activities_model.ActionApprovePullRequest:
pullLink := toPullLink(ctx, act)
titleExtra = ctx.Locale.Tr("action.approve_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(ctx))
Expand Down
5 changes: 3 additions & 2 deletions services/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,12 @@ func (g *GiteaLocalUploader) CreateReleases(ctx context.Context, releases ...*ba

// SyncTags syncs releases with tags in the database
func (g *GiteaLocalUploader) SyncTags(ctx context.Context) error {
return repo_module.SyncReleasesWithTags(ctx, g.repo, g.gitRepo)
_, err := repo_module.SyncReleasesWithTags(ctx, g.repo, g.gitRepo)
return err
}

func (g *GiteaLocalUploader) SyncBranches(ctx context.Context) error {
_, err := repo_module.SyncRepoBranchesWithRepo(ctx, g.repo, g.gitRepo, g.doer.ID)
_, _, err := repo_module.SyncRepoBranchesWithRepo(ctx, g.repo, g.gitRepo, g.doer.ID)
return err
}

Expand Down
Loading