mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
171 lines
4.8 KiB
Go
171 lines
4.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
git_model "github.com/hanzoai/git/models/git"
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/container"
|
|
"github.com/hanzoai/git/modules/git"
|
|
"github.com/hanzoai/git/modules/gitrepo"
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/modules/timeutil"
|
|
)
|
|
|
|
// SyncResult describes a reference update detected during sync.
|
|
type SyncResult struct {
|
|
RefName git.RefName
|
|
OldCommitID git.RefName
|
|
NewCommitID git.RefName
|
|
}
|
|
|
|
// SyncRepoBranches synchronizes branch table with repository branches
|
|
func SyncRepoBranches(ctx context.Context, repoID, doerID int64) (int64, error) {
|
|
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
log.Debug("SyncRepoBranches: in Repo[%d:%s]", repo.ID, repo.FullName())
|
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
|
if err != nil {
|
|
log.Error("OpenRepository[%s]: %w", repo.FullName(), err)
|
|
return 0, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
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, []*SyncResult, error) {
|
|
objFmt, err := gitRepo.GetObjectFormat()
|
|
if err != nil {
|
|
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, nil, fmt.Errorf("UpdateRepositoryColsWithAutoTime: %w", err)
|
|
}
|
|
}
|
|
|
|
allBranches := container.Set[string]{}
|
|
{
|
|
branches, _, err := gitRepo.GetBranchNames(0, 0)
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
log.Trace("SyncRepoBranches[%s]: branches[%d]: %v", repo.FullName(), len(branches), branches)
|
|
for _, branch := range branches {
|
|
allBranches.Add(branch)
|
|
}
|
|
}
|
|
|
|
dbBranches := make(map[string]*git_model.Branch)
|
|
{
|
|
branches, err := db.Find[git_model.Branch](ctx, git_model.FindBranchOptions{
|
|
ListOptions: db.ListOptionsAll,
|
|
RepoID: repo.ID,
|
|
})
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
for _, branch := range branches {
|
|
dbBranches[branch.Name] = branch
|
|
}
|
|
}
|
|
|
|
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, nil, err
|
|
}
|
|
if dbb == nil {
|
|
toAdd = append(toAdd, &git_model.Branch{
|
|
RepoID: repo.ID,
|
|
Name: branch,
|
|
CommitID: commit.ID.String(),
|
|
CommitMessage: commit.MessageTitle(),
|
|
PusherID: doerID,
|
|
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
|
|
})
|
|
syncResults = append(syncResults, &SyncResult{
|
|
RefName: git.RefNameFromBranch(branch),
|
|
OldCommitID: "",
|
|
NewCommitID: commit.ID.RefName(),
|
|
})
|
|
} else if commit.ID.String() != dbb.CommitID || dbb.IsDeleted {
|
|
toUpdate = append(toUpdate, &git_model.Branch{
|
|
ID: dbb.ID,
|
|
RepoID: repo.ID,
|
|
Name: branch,
|
|
CommitID: commit.ID.String(),
|
|
CommitMessage: commit.MessageTitle(),
|
|
PusherID: doerID,
|
|
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
|
|
})
|
|
syncResults = append(syncResults, &SyncResult{
|
|
RefName: git.RefNameFromBranch(branch),
|
|
OldCommitID: git.RefNameFromCommit(dbb.CommitID),
|
|
NewCommitID: commit.ID.RefName(),
|
|
})
|
|
}
|
|
}
|
|
|
|
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: git.RefNameFromCommit(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)), syncResults, nil
|
|
}
|
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
if len(toAdd) > 0 {
|
|
if err := git_model.AddBranches(ctx, toAdd); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, b := range toUpdate {
|
|
if _, err := db.GetEngine(ctx).ID(b.ID).
|
|
Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted").
|
|
Update(b); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if len(toRemove) > 0 {
|
|
if err := git_model.DeleteBranches(ctx, repo.ID, doerID, toRemove); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}); err != nil {
|
|
return 0, nil, err
|
|
}
|
|
return int64(len(allBranches)), syncResults, nil
|
|
}
|