2021-06-07 10:52:59 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-07 10:52:59 -04:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-12-08 14:08:16 -05:00
|
|
|
"context"
|
2021-06-07 10:52:59 -04:00
|
|
|
"errors"
|
2021-11-17 10:17:31 -05:00
|
|
|
"fmt"
|
2021-12-08 14:08:16 -05:00
|
|
|
"strings"
|
2021-06-07 10:52:59 -04:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2024-01-12 16:50:38 -05:00
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
2023-06-29 06:03:20 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 11:51:54 -04:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2023-06-29 06:03:20 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2024-10-22 18:48:46 -04:00
|
|
|
"code.gitea.io/gitea/models/perm/access"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2024-10-22 18:48:46 -04:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-07 10:52:59 -04:00
|
|
|
"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-27 15:09:51 -05:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2023-06-29 06:03:20 -04:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-06-07 10:52:59 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-22 21:18:33 -05:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2023-06-29 06:03:20 -04:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2021-06-07 10:52:59 -04:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2024-10-22 18:48:46 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-01-12 16:50:38 -05:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2023-09-05 14:37:47 -04:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2024-10-22 18:48:46 -04:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
2023-06-29 06:03:20 -04:00
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
2021-06-07 10:52:59 -04:00
|
|
|
)
|
|
|
|
|
2021-11-17 10:17:31 -05:00
|
|
|
// CreateNewBranch creates a new repository branch
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, oldBranchName, branchName string) (err error) {
|
|
|
|
branch, err := git_model.GetBranch(ctx, repo.ID, oldBranchName)
|
2023-09-21 19:43:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
return CreateNewBranchFromCommit(ctx, doer, repo, gitRepo, branch.CommitID, branchName)
|
2021-11-17 10:17:31 -05:00
|
|
|
}
|
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
// Branch contains the branch information
|
|
|
|
type Branch struct {
|
|
|
|
DBBranch *git_model.Branch
|
|
|
|
IsProtected bool
|
|
|
|
IsIncluded bool
|
|
|
|
CommitsAhead int
|
|
|
|
CommitsBehind int
|
|
|
|
LatestPullRequest *issues_model.PullRequest
|
|
|
|
MergeMovedOn bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBranches loads branches from the repository limited by page & pageSize.
|
2024-03-02 10:42:31 -05:00
|
|
|
func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, isDeletedBranch optional.Option[bool], keyword string, page, pageSize int) (*Branch, []*Branch, int64, error) {
|
2023-06-29 06:03:20 -04:00
|
|
|
defaultDBBranch, err := git_model.GetBranch(ctx, repo.ID, repo.DefaultBranch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
branchOpts := git_model.FindBranchOptions{
|
|
|
|
RepoID: repo.ID,
|
2024-03-02 10:42:31 -05:00
|
|
|
IsDeletedBranch: isDeletedBranch,
|
2023-06-29 06:03:20 -04:00
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: pageSize,
|
|
|
|
},
|
2024-01-21 09:08:31 -05:00
|
|
|
Keyword: keyword,
|
|
|
|
ExcludeBranchNames: []string{repo.DefaultBranch},
|
2023-06-29 06:03:20 -04:00
|
|
|
}
|
|
|
|
|
2023-12-11 03:56:48 -05:00
|
|
|
dbBranches, totalNumOfBranches, err := db.FindAndCount[git_model.Branch](ctx, branchOpts)
|
2023-06-29 06:03:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
|
2023-12-11 03:56:48 -05:00
|
|
|
if err := git_model.BranchList(dbBranches).LoadDeletedBy(ctx); err != nil {
|
2023-06-29 06:03:20 -04:00
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
2023-12-11 03:56:48 -05:00
|
|
|
if err := git_model.BranchList(dbBranches).LoadPusher(ctx); err != nil {
|
2023-06-29 06:03:20 -04:00
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rules, err := git_model.FindRepoProtectedBranchRules(ctx, repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDToRepo := map[int64]*repo_model.Repository{}
|
|
|
|
repoIDToRepo[repo.ID] = repo
|
|
|
|
|
|
|
|
repoIDToGitRepo := map[int64]*git.Repository{}
|
|
|
|
repoIDToGitRepo[repo.ID] = gitRepo
|
|
|
|
|
|
|
|
branches := make([]*Branch, 0, len(dbBranches))
|
|
|
|
for i := range dbBranches {
|
|
|
|
branch, err := loadOneBranch(ctx, repo, dbBranches[i], &rules, repoIDToRepo, repoIDToGitRepo)
|
|
|
|
if err != nil {
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 15:02:49 -05:00
|
|
|
log.Error("loadOneBranch() on repo #%d, branch '%s' failed: %v", repo.ID, dbBranches[i].Name, err)
|
|
|
|
|
|
|
|
// TODO: Ideally, we would only do this if the branch doesn't exist
|
|
|
|
// anymore. That is not practical to check here currently, so we do
|
|
|
|
// this for all kinds of errors.
|
|
|
|
totalNumOfBranches--
|
|
|
|
continue
|
2023-06-29 06:03:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
branches = append(branches, branch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always add the default branch
|
|
|
|
log.Debug("loadOneBranch: load default: '%s'", defaultDBBranch.Name)
|
|
|
|
defaultBranch, err := loadOneBranch(ctx, repo, defaultDBBranch, &rules, repoIDToRepo, repoIDToGitRepo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultBranch, branches, totalNumOfBranches, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *git_model.Branch, protectedBranches *git_model.ProtectedBranchRules,
|
|
|
|
repoIDToRepo map[int64]*repo_model.Repository,
|
|
|
|
repoIDToGitRepo map[int64]*git.Repository,
|
|
|
|
) (*Branch, error) {
|
|
|
|
log.Trace("loadOneBranch: '%s'", dbBranch.Name)
|
|
|
|
|
|
|
|
branchName := dbBranch.Name
|
|
|
|
p := protectedBranches.GetFirstMatched(branchName)
|
|
|
|
isProtected := p != nil
|
|
|
|
|
2024-03-20 11:38:22 -04:00
|
|
|
var divergence *git.DivergeObject
|
2023-06-29 06:03:20 -04:00
|
|
|
|
|
|
|
// it's not default branch
|
|
|
|
if repo.DefaultBranch != dbBranch.Name && !dbBranch.IsDeleted {
|
|
|
|
var err error
|
|
|
|
divergence, err = files_service.CountDivergingCommits(ctx, repo, git.BranchPrefix+branchName)
|
|
|
|
if err != nil {
|
[GITEA] services: Gracefully handle missing branches
services: in loadOneBranch, return if CountDivergingCommits fail
If we can't count the number of diverging commits for one reason or
another (such as the branch being in the database, but missing from
disk), rather than logging an error and continuing into a crash (because
`divergence` will be nil), return an error instead.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8266105f24eb76b1dfb4c79d9bfde2ef9a98417a)
services: Gracefully handle missing branches
When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.
Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e552a8fd629b11503569f605c824c1c0b01eeab2)
tests: Add a testcase for missing branches
This tests the scenario reported in Codeberg/Community#1408: a branch
that is recorded in the database, but missing on disk was causing
internal server errors. With recent changes, that is no longer the case,
the error is logged and then ignored.
This test case tests this behaviour, that the repo's branches page on
the web UI functions even if the git branch is missing.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit e20eb7b3853e25ab29d4ca63b015517b44e4954f)
tests: More testing in TestDatabaseMissingABranch
In the `TestDatabaseMissingABranch` testcase, make sure that the
branches are in sync between the db and git before deleting a branch via
git, then compare the branch count from the web UI, making sure that it
returns an out-of-sync value first, and the correct one after another
sync.
This is currently tested by scraping the UI, and relies on the fact that
the branch counter is out of date before syncing. If that issue gets
resolved, we'll have to adjust the test to verify the sync another way.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit 8c2ccfcecec6182dd80d463f58223acbf16b039b)
(cherry picked from commit 439fadf5635c47c2a1be9cc83614b60f76ac05d0)
(cherry picked from commit 44dd80552ca63c6d22f4a139a0297486f1a2e655)
(cherry picked from commit 37b91fe6f2f05feee0f8db8f44c3eaf1ff060af9)
2024-01-13 15:02:49 -05:00
|
|
|
return nil, fmt.Errorf("CountDivergingCommits: %v", err)
|
2023-06-29 06:03:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 11:38:22 -04:00
|
|
|
if divergence == nil {
|
|
|
|
// tolerate the error that we cannot get divergence
|
|
|
|
divergence = &git.DivergeObject{Ahead: -1, Behind: -1}
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:24:07 -04:00
|
|
|
pr, err := issues_model.GetLatestPullRequestByHeadInfo(ctx, repo.ID, branchName)
|
2023-06-29 06:03:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err)
|
|
|
|
}
|
|
|
|
headCommit := dbBranch.CommitID
|
|
|
|
|
|
|
|
mergeMovedOn := false
|
|
|
|
if pr != nil {
|
|
|
|
pr.HeadRepo = repo
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("LoadIssue: %v", err)
|
|
|
|
}
|
|
|
|
if repo, ok := repoIDToRepo[pr.BaseRepoID]; ok {
|
|
|
|
pr.BaseRepo = repo
|
|
|
|
} else if err := pr.LoadBaseRepo(ctx); err != nil {
|
|
|
|
return nil, fmt.Errorf("LoadBaseRepo: %v", err)
|
|
|
|
} else {
|
|
|
|
repoIDToRepo[pr.BaseRepoID] = pr.BaseRepo
|
|
|
|
}
|
|
|
|
pr.Issue.Repo = pr.BaseRepo
|
|
|
|
|
|
|
|
if pr.HasMerged {
|
|
|
|
baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
|
|
|
|
if !ok {
|
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-27 15:09:51 -05:00
|
|
|
baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
2023-06-29 06:03:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("OpenRepository: %v", err)
|
|
|
|
}
|
|
|
|
defer baseGitRepo.Close()
|
|
|
|
repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
|
|
|
|
}
|
|
|
|
pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("GetBranchCommitID: %v", err)
|
|
|
|
}
|
|
|
|
if err == nil && headCommit != pullCommit {
|
|
|
|
// the head has moved on from the merge - we shouldn't delete
|
|
|
|
mergeMovedOn = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isIncluded := divergence.Ahead == 0 && repo.DefaultBranch != branchName
|
|
|
|
return &Branch{
|
|
|
|
DBBranch: dbBranch,
|
|
|
|
IsProtected: isProtected,
|
|
|
|
IsIncluded: isIncluded,
|
|
|
|
CommitsAhead: divergence.Ahead,
|
|
|
|
CommitsBehind: divergence.Behind,
|
|
|
|
LatestPullRequest: pr,
|
|
|
|
MergeMovedOn: mergeMovedOn,
|
|
|
|
}, nil
|
2021-11-17 10:17:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkBranchName validates branch name with existing repository branches
|
2021-12-09 20:27:50 -05:00
|
|
|
func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
|
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-27 15:09:51 -05:00
|
|
|
_, err := gitrepo.WalkReferences(ctx, repo, func(_, refName string) error {
|
2021-12-08 14:08:16 -05:00
|
|
|
branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
|
|
|
|
switch {
|
|
|
|
case branchRefName == name:
|
2023-06-29 06:03:20 -04:00
|
|
|
return git_model.ErrBranchAlreadyExists{
|
2021-12-08 14:08:16 -05:00
|
|
|
BranchName: name,
|
2021-11-17 10:17:31 -05:00
|
|
|
}
|
2021-12-08 14:08:16 -05:00
|
|
|
// If branchRefName like a/b but we want to create a branch named a then we have a conflict
|
|
|
|
case strings.HasPrefix(branchRefName, name+"/"):
|
2023-06-29 06:03:20 -04:00
|
|
|
return git_model.ErrBranchNameConflict{
|
2021-12-08 14:08:16 -05:00
|
|
|
BranchName: branchRefName,
|
|
|
|
}
|
|
|
|
// Conversely if branchRefName like a but we want to create a branch named a/b then we also have a conflict
|
|
|
|
case strings.HasPrefix(name, branchRefName+"/"):
|
2023-06-29 06:03:20 -04:00
|
|
|
return git_model.ErrBranchNameConflict{
|
2021-12-08 14:08:16 -05:00
|
|
|
BranchName: branchRefName,
|
|
|
|
}
|
|
|
|
case refName == git.TagPrefix+name:
|
|
|
|
return models.ErrTagAlreadyExists{
|
|
|
|
TagName: name,
|
2021-11-17 10:17:31 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 14:08:16 -05:00
|
|
|
return nil
|
|
|
|
})
|
2021-11-17 10:17:31 -05:00
|
|
|
|
2021-12-08 14:08:16 -05:00
|
|
|
return err
|
2021-11-17 10:17:31 -05:00
|
|
|
}
|
|
|
|
|
2024-03-06 03:47:52 -05:00
|
|
|
// SyncBranchesToDB sync the branch information in the database.
|
|
|
|
// It will check whether the branches of the repository have never been synced before.
|
|
|
|
// If so, it will sync all branches of the repository.
|
|
|
|
// Otherwise, it will sync the branches that need to be updated.
|
|
|
|
func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames, commitIDs []string, getCommit func(commitID string) (*git.Commit, error)) error {
|
|
|
|
// Some designs that make the code look strange but are made for performance optimization purposes:
|
|
|
|
// 1. Sync branches in a batch to reduce the number of DB queries.
|
|
|
|
// 2. Lazy load commit information since it may be not necessary.
|
|
|
|
// 3. Exit early if synced all branches of git repo when there's no branch in DB.
|
|
|
|
// 4. Check the branches in DB if they are already synced.
|
|
|
|
//
|
|
|
|
// If the user pushes many branches at once, the Git hook will call the internal API in batches, rather than all at once.
|
|
|
|
// See https://github.com/go-gitea/gitea/blob/cb52b17f92e2d2293f7c003649743464492bca48/cmd/hook.go#L27
|
|
|
|
// For the first batch, it will hit optimization 3.
|
|
|
|
// For other batches, it will hit optimization 4.
|
|
|
|
|
|
|
|
if len(branchNames) != len(commitIDs) {
|
|
|
|
return fmt.Errorf("branchNames and commitIDs length not match")
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
}
|
|
|
|
|
2024-03-06 03:47:52 -05:00
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
branches, err := git_model.GetBranches(ctx, repoID, branchNames)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("git_model.GetBranches: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(branches) == 0 {
|
|
|
|
// if user haven't visit UI but directly push to a branch after upgrading from 1.20 -> 1.21,
|
|
|
|
// we cannot simply insert the branch but need to check we have branches or not
|
|
|
|
hasBranch, err := db.Exist[git_model.Branch](ctx, git_model.FindBranchOptions{
|
|
|
|
RepoID: repoID,
|
|
|
|
IsDeletedBranch: optional.Some(false),
|
|
|
|
}.ToConds())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !hasBranch {
|
|
|
|
if _, err = repo_module.SyncRepoBranches(ctx, repoID, pusherID); err != nil {
|
|
|
|
return fmt.Errorf("repo_module.SyncRepoBranches %d failed: %v", repoID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
}
|
|
|
|
|
2024-03-06 03:47:52 -05:00
|
|
|
branchMap := make(map[string]*git_model.Branch, len(branches))
|
|
|
|
for _, branch := range branches {
|
|
|
|
branchMap[branch.Name] = branch
|
|
|
|
}
|
|
|
|
|
|
|
|
newBranches := make([]*git_model.Branch, 0, len(branchNames))
|
|
|
|
|
|
|
|
for i, branchName := range branchNames {
|
|
|
|
commitID := commitIDs[i]
|
|
|
|
branch, exist := branchMap[branchName]
|
2024-03-18 07:26:49 -04:00
|
|
|
if exist && branch.CommitID == commitID && !branch.IsDeleted {
|
2024-03-06 03:47:52 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-18 07:26:38 -04:00
|
|
|
commit, err := getCommit(commitID)
|
2024-03-06 03:47:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get commit of %s failed: %v", branchName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
|
|
|
if _, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit); err != nil {
|
|
|
|
return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
|
|
|
|
}
|
2024-05-29 02:43:02 -04:00
|
|
|
continue
|
2024-03-06 03:47:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if database have branches but not this branch, it means this is a new branch
|
|
|
|
newBranches = append(newBranches, &git_model.Branch{
|
|
|
|
RepoID: repoID,
|
|
|
|
Name: branchName,
|
|
|
|
CommitID: commit.ID.String(),
|
|
|
|
CommitMessage: commit.Summary(),
|
|
|
|
PusherID: pusherID,
|
|
|
|
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newBranches) > 0 {
|
|
|
|
return db.Insert(ctx, newBranches)
|
|
|
|
}
|
|
|
|
return nil
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-17 10:17:31 -05:00
|
|
|
// CreateNewBranchFromCommit creates a new repository branch
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, commitID, branchName string) (err error) {
|
2023-09-21 19:43:29 -04:00
|
|
|
err = repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-17 10:17:31 -05:00
|
|
|
// Check if branch name can be used
|
2022-01-19 18:26:57 -05:00
|
|
|
if err := checkBranchName(ctx, repo, branchName); err != nil {
|
2021-11-17 10:17:31 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-28 02:28:57 -05:00
|
|
|
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
|
|
|
|
Remote: repo.RepoPath(),
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
|
|
|
|
Env: repo_module.PushingEnvironment(doer, repo),
|
|
|
|
}); err != nil {
|
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
2021-11-17 10:17:31 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-12-28 02:28:57 -05:00
|
|
|
return fmt.Errorf("push: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-11-17 10:17:31 -05:00
|
|
|
}
|
|
|
|
|
2021-10-08 13:03:04 -04:00
|
|
|
// RenameBranch rename a branch
|
2023-02-28 17:17:51 -05:00
|
|
|
func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
|
2023-09-21 19:43:29 -04:00
|
|
|
err := repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-10-08 13:03:04 -04:00
|
|
|
if from == to {
|
|
|
|
return "target_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitRepo.IsBranchExist(to) {
|
|
|
|
return "target_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gitRepo.IsBranchExist(from) {
|
|
|
|
return "from_not_exist", nil
|
|
|
|
}
|
|
|
|
|
2024-01-12 16:50:38 -05:00
|
|
|
if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error {
|
2021-10-08 13:03:04 -04:00
|
|
|
err2 := gitRepo.RenameBranch(from, to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDefault {
|
2024-01-12 16:50:38 -05:00
|
|
|
// if default branch changed, we need to delete all schedules and cron jobs
|
|
|
|
if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
|
|
|
|
log.Error("DeleteCronTaskByRepo: %v", err)
|
|
|
|
}
|
|
|
|
// cancel running cron jobs of this repository and delete old schedules
|
2024-03-21 03:01:35 -04:00
|
|
|
if err := actions_model.CancelPreviousJobs(
|
2024-01-12 16:50:38 -05:00
|
|
|
ctx,
|
|
|
|
repo.ID,
|
|
|
|
from,
|
|
|
|
"",
|
|
|
|
webhook_module.HookEventSchedule,
|
|
|
|
); err != nil {
|
2024-03-21 03:01:35 -04:00
|
|
|
log.Error("CancelPreviousJobs: %v", err)
|
2024-01-12 16:50:38 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 02:30:10 -05:00
|
|
|
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
|
2021-10-08 13:03:04 -04:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-05-25 21:04:48 -04:00
|
|
|
refNameTo := git.RefNameFromBranch(to)
|
|
|
|
refID, err := gitRepo.GetRefCommitID(refNameTo.String())
|
2022-01-19 18:26:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-08 13:03:04 -04:00
|
|
|
|
2023-09-05 14:37:47 -04:00
|
|
|
notify_service.DeleteRef(ctx, doer, repo, git.RefNameFromBranch(from))
|
|
|
|
notify_service.CreateRef(ctx, doer, repo, refNameTo, refID)
|
2021-10-08 13:03:04 -04:00
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:52:59 -04:00
|
|
|
// enmuerates all branch related errors
|
|
|
|
var (
|
2023-01-16 03:00:22 -05:00
|
|
|
ErrBranchIsDefault = errors.New("branch is default")
|
2021-06-07 10:52:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteBranch delete branch
|
2023-02-28 17:17:51 -05:00
|
|
|
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
|
2023-09-21 19:43:29 -04:00
|
|
|
err := repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:52:59 -04:00
|
|
|
if branchName == repo.DefaultBranch {
|
|
|
|
return ErrBranchIsDefault
|
|
|
|
}
|
|
|
|
|
2023-02-28 17:17:51 -05:00
|
|
|
isProtected, err := git_model.IsBranchProtected(ctx, repo.ID, branchName)
|
2021-06-07 10:52:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isProtected {
|
2023-01-16 03:00:22 -05:00
|
|
|
return git_model.ErrBranchIsProtected
|
2021-06-07 10:52:59 -04:00
|
|
|
}
|
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
rawBranch, err := git_model.GetBranch(ctx, repo.ID, branchName)
|
2024-09-24 03:42:08 -04:00
|
|
|
if err != nil && !git_model.IsErrBranchNotExist(err) {
|
2024-01-22 15:59:07 -05:00
|
|
|
return fmt.Errorf("GetBranch: %v", err)
|
2023-06-29 06:03:20 -04:00
|
|
|
}
|
|
|
|
|
2024-09-24 03:42:08 -04:00
|
|
|
// database branch record not exist or it's a deleted branch
|
|
|
|
notExist := git_model.IsErrBranchNotExist(err) || rawBranch.IsDeleted
|
2023-06-29 06:03:20 -04:00
|
|
|
|
2021-06-07 10:52:59 -04:00
|
|
|
commit, err := gitRepo.GetBranchCommit(branchName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
2024-09-24 03:42:08 -04:00
|
|
|
if !notExist {
|
|
|
|
if err := git_model.AddDeletedBranch(ctx, repo.ID, branchName, doer.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-29 06:03:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
2021-06-07 10:52:59 -04:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:55:19 -05:00
|
|
|
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
|
|
|
|
|
2021-06-07 10:52:59 -04:00
|
|
|
// Don't return error below this
|
|
|
|
if err := PushUpdate(
|
|
|
|
&repo_module.PushUpdateOptions{
|
2023-05-25 21:04:48 -04:00
|
|
|
RefFullName: git.RefNameFromBranch(branchName),
|
2021-06-07 10:52:59 -04:00
|
|
|
OldCommitID: commit.ID.String(),
|
2023-12-17 06:56:08 -05:00
|
|
|
NewCommitID: objectFormat.EmptyObjectID().String(),
|
2021-06-07 10:52:59 -04:00
|
|
|
PusherID: doer.ID,
|
|
|
|
PusherName: doer.Name,
|
|
|
|
RepoUserName: repo.OwnerName,
|
|
|
|
RepoName: repo.Name,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("Update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-29 06:03:20 -04:00
|
|
|
|
2024-10-22 18:48:46 -04:00
|
|
|
// DeleteBranchAfterMerge deletes the head branch after a PR was merged assiociated with the head branch.
|
|
|
|
func DeleteBranchAfterMerge(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, headRepo *git.Repository) error {
|
|
|
|
// Don't cleanup when there are other PR's that use this branch as head branch.
|
|
|
|
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pr.HeadRepoID, pr.HeadBranch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the doer has write permissions to the head repository of the branch it wants to delete.
|
|
|
|
perm, err := access.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !perm.CanWrite(unit.TypeCode) {
|
|
|
|
return util.NewPermissionDeniedErrorf("Must have write permission to the head repository")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pull_service.RetargetChildrenOnMerge(ctx, doer, pr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := DeleteBranch(ctx, doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
|
|
|
|
// Do not fail here as branch has already been deleted
|
|
|
|
log.Error("DeleteBranchAfterMerge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
type BranchSyncOptions struct {
|
|
|
|
RepoID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// branchSyncQueue represents a queue to handle branch sync jobs.
|
|
|
|
var branchSyncQueue *queue.WorkerPoolQueue[*BranchSyncOptions]
|
|
|
|
|
|
|
|
func handlerBranchSync(items ...*BranchSyncOptions) []*BranchSyncOptions {
|
|
|
|
for _, opts := range items {
|
|
|
|
_, err := repo_module.SyncRepoBranches(graceful.GetManager().ShutdownContext(), opts.RepoID, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("syncRepoBranches [%d] failed: %v", opts.RepoID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-29 04:47:56 -04:00
|
|
|
func addRepoToBranchSyncQueue(repoID int64) error {
|
2023-06-29 06:03:20 -04:00
|
|
|
return branchSyncQueue.Push(&BranchSyncOptions{
|
|
|
|
RepoID: repoID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBranchSyncQueue(ctx context.Context) error {
|
|
|
|
branchSyncQueue = queue.CreateUniqueQueue(ctx, "branch_sync", handlerBranchSync)
|
|
|
|
if branchSyncQueue == nil {
|
|
|
|
return errors.New("unable to create branch_sync queue")
|
|
|
|
}
|
|
|
|
go graceful.GetManager().RunWithCancel(branchSyncQueue)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-29 04:47:56 -04:00
|
|
|
func AddAllRepoBranchesToSyncQueue(ctx context.Context) error {
|
2023-06-29 06:03:20 -04:00
|
|
|
if err := db.Iterate(ctx, builder.Eq{"is_empty": false}, func(ctx context.Context, repo *repo_model.Repository) error {
|
2024-04-29 04:47:56 -04:00
|
|
|
return addRepoToBranchSyncQueue(repo.ID)
|
2023-06-29 06:03:20 -04:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("run sync all branches failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-12 16:50:38 -05:00
|
|
|
|
|
|
|
func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, newBranchName string) error {
|
|
|
|
if repo.DefaultBranch == newBranchName {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gitRepo.IsBranchExist(newBranchName) {
|
|
|
|
return git_model.ErrBranchNotExist{
|
|
|
|
BranchName: newBranchName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oldDefaultBranchName := repo.DefaultBranch
|
|
|
|
repo.DefaultBranch = newBranchName
|
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
|
|
|
|
log.Error("DeleteCronTaskByRepo: %v", err)
|
|
|
|
}
|
|
|
|
// cancel running cron jobs of this repository and delete old schedules
|
2024-03-21 03:01:35 -04:00
|
|
|
if err := actions_model.CancelPreviousJobs(
|
2024-01-12 16:50:38 -05:00
|
|
|
ctx,
|
|
|
|
repo.ID,
|
|
|
|
oldDefaultBranchName,
|
|
|
|
"",
|
|
|
|
webhook_module.HookEventSchedule,
|
|
|
|
); err != nil {
|
2024-03-21 03:01:35 -04:00
|
|
|
log.Error("CancelPreviousJobs: %v", err)
|
2024-01-12 16:50:38 -05:00
|
|
|
}
|
|
|
|
|
2024-03-25 11:54:38 -04:00
|
|
|
if err := gitrepo.SetDefaultBranch(ctx, repo, newBranchName); err != nil {
|
2024-01-12 16:50:38 -05:00
|
|
|
if !git.IsErrUnsupportedVersion(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_service.ChangeDefaultBranch(ctx, repo)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|