2023-06-29 18:03:20 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/modules/container"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2023-06-29 18:03:20 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
2023-06-29 18:03:20 +08:00
|
|
|
if err != nil {
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
log.Error("OpenRepository[%s]: %w", repo.FullName(), err)
|
2023-06-29 18:03:20 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
|
|
|
return SyncRepoBranchesWithRepo(ctx, repo, gitRepo, doerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, doerID int64) (int64, error) {
|
|
|
|
allBranches := container.Set[string]{}
|
|
|
|
{
|
|
|
|
branches, _, err := gitRepo.GetBranchNames(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 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)
|
|
|
|
{
|
2023-12-11 16:56:48 +08:00
|
|
|
branches, err := db.Find[git_model.Branch](ctx, git_model.FindBranchOptions{
|
|
|
|
ListOptions: db.ListOptionsAll,
|
|
|
|
RepoID: repo.ID,
|
2023-06-29 18:03:20 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
for _, branch := range branches {
|
|
|
|
dbBranches[branch.Name] = branch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var toAdd []*git_model.Branch
|
|
|
|
var toUpdate []*git_model.Branch
|
|
|
|
var toRemove []int64
|
|
|
|
for branch := range allBranches {
|
|
|
|
dbb := dbBranches[branch]
|
|
|
|
commit, err := gitRepo.GetBranchCommit(branch)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if dbb == nil {
|
|
|
|
toAdd = append(toAdd, &git_model.Branch{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Name: branch,
|
|
|
|
CommitID: commit.ID.String(),
|
2023-06-30 17:03:05 +08:00
|
|
|
CommitMessage: commit.Summary(),
|
2023-06-29 18:03:20 +08:00
|
|
|
PusherID: doerID,
|
2023-06-30 17:03:05 +08:00
|
|
|
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
|
2023-06-29 18:03:20 +08:00
|
|
|
})
|
|
|
|
} else if commit.ID.String() != dbb.CommitID {
|
|
|
|
toUpdate = append(toUpdate, &git_model.Branch{
|
|
|
|
ID: dbb.ID,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Name: branch,
|
|
|
|
CommitID: commit.ID.String(),
|
2023-06-30 17:03:05 +08:00
|
|
|
CommitMessage: commit.Summary(),
|
2023-06-29 18:03:20 +08:00
|
|
|
PusherID: doerID,
|
2023-06-30 17:03:05 +08:00
|
|
|
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
|
2023-06-29 18:03:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dbBranch := range dbBranches {
|
|
|
|
if !allBranches.Contains(dbBranch.Name) && !dbBranch.IsDeleted {
|
|
|
|
toRemove = append(toRemove, dbBranch.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-28 12:06:47 +08:00
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
2023-06-29 18:03:20 +08:00
|
|
|
if len(toAdd) > 0 {
|
2023-08-28 12:06:47 +08:00
|
|
|
if err := git_model.AddBranches(ctx, toAdd); err != nil {
|
2023-06-29 18:03:20 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range toUpdate {
|
2023-08-28 12:06:47 +08:00
|
|
|
if _, err := db.GetEngine(ctx).ID(b.ID).
|
2023-06-29 18:03:20 +08:00
|
|
|
Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted").
|
|
|
|
Update(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toRemove) > 0 {
|
2023-08-28 12:06:47 +08:00
|
|
|
if err := git_model.DeleteBranches(ctx, repo.ID, doerID, toRemove); err != nil {
|
2023-06-29 18:03:20 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int64(len(allBranches)), nil
|
|
|
|
}
|