2021-06-07 22:52:59 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-07 22:52:59 +08:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-12-09 03:08:16 +08:00
|
|
|
"context"
|
2021-06-07 22:52:59 +08:00
|
|
|
"errors"
|
2021-11-17 23:17:31 +08:00
|
|
|
"fmt"
|
2021-12-09 03:08:16 +08:00
|
|
|
"strings"
|
2021-06-07 22:52:59 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2024-01-15 06:54:22 +08:00
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
2023-06-29 18:03:20 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2023-06-29 18:03:20 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-07 22:52:59 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2023-06-29 18:03:20 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-06-07 22:52:59 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2023-06-29 18:03:20 +08:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2021-06-07 22:52:59 +08:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
Also sync DB branches on push if necessary (#28361) (#28403)
Fix #28056
Backport #28361
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-11 14:16:56 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2023-06-29 18:03:20 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-01-15 06:54:22 +08:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2023-06-29 18:03:20 +08:00
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
2021-06-07 22:52:59 +08:00
|
|
|
)
|
|
|
|
|
2021-11-17 23:17:31 +08:00
|
|
|
// CreateNewBranch creates a new repository branch
|
Also sync DB branches on push if necessary (#28361) (#28403)
Fix #28056
Backport #28361
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-11 14:16:56 +08: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)
|
|
|
|
if err != nil {
|
2021-11-17 23:17:31 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Also sync DB branches on push if necessary (#28361) (#28403)
Fix #28056
Backport #28361
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-11 14:16:56 +08:00
|
|
|
return CreateNewBranchFromCommit(ctx, doer, repo, gitRepo, branch.CommitID, branchName)
|
2021-11-17 23:17:31 +08:00
|
|
|
}
|
|
|
|
|
2023-06-29 18:03:20 +08: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.
|
2023-09-17 16:24:40 +08:00
|
|
|
func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, isDeletedBranch util.OptionalBool, keyword string, page, pageSize int) (*Branch, []*Branch, int64, error) {
|
2023-06-29 18:03:20 +08: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,
|
|
|
|
IsDeletedBranch: isDeletedBranch,
|
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: pageSize,
|
|
|
|
},
|
2023-09-17 16:24:40 +08:00
|
|
|
Keyword: keyword,
|
2023-06-29 18:03:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
totalNumOfBranches, err := git_model.CountBranches(ctx, branchOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
branchOpts.ExcludeBranchNames = []string{repo.DefaultBranch}
|
|
|
|
|
|
|
|
dbBranches, err := git_model.FindBranches(ctx, branchOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dbBranches.LoadDeletedBy(ctx); err != nil {
|
|
|
|
return nil, nil, 0, err
|
|
|
|
}
|
|
|
|
if err := dbBranches.LoadPusher(ctx); err != nil {
|
|
|
|
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 {
|
|
|
|
return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 22:31:32 +08:00
|
|
|
var divergence *git.DivergeObject
|
2023-06-29 18:03:20 +08: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 {
|
|
|
|
log.Error("CountDivergingCommits: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:31:32 +08:00
|
|
|
if divergence == nil {
|
|
|
|
// tolerate error that we can't get divergence
|
|
|
|
divergence = &git.DivergeObject{Ahead: -1, Behind: -1}
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:03:20 +08:00
|
|
|
pr, err := issues_model.GetLatestPullRequestByHeadInfo(repo.ID, branchName)
|
|
|
|
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 {
|
|
|
|
baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
|
|
|
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 23:17:31 +08:00
|
|
|
}
|
|
|
|
|
2023-05-14 05:59:01 +08:00
|
|
|
func GetBranchCommitID(ctx context.Context, repo *repo_model.Repository, branch string) (string, error) {
|
|
|
|
return git.GetBranchCommitID(ctx, repo.RepoPath(), branch)
|
|
|
|
}
|
|
|
|
|
2021-11-17 23:17:31 +08:00
|
|
|
// checkBranchName validates branch name with existing repository branches
|
2021-12-10 09:27:50 +08:00
|
|
|
func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
|
2022-03-30 01:12:33 +08:00
|
|
|
_, err := git.WalkReferences(ctx, repo.RepoPath(), func(_, refName string) error {
|
2021-12-09 03:08:16 +08:00
|
|
|
branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
|
|
|
|
switch {
|
|
|
|
case branchRefName == name:
|
2023-06-29 18:03:20 +08:00
|
|
|
return git_model.ErrBranchAlreadyExists{
|
2021-12-09 03:08:16 +08:00
|
|
|
BranchName: name,
|
2021-11-17 23:17:31 +08:00
|
|
|
}
|
2021-12-09 03:08:16 +08: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 18:03:20 +08:00
|
|
|
return git_model.ErrBranchNameConflict{
|
2021-12-09 03:08:16 +08: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 18:03:20 +08:00
|
|
|
return git_model.ErrBranchNameConflict{
|
2021-12-09 03:08:16 +08:00
|
|
|
BranchName: branchRefName,
|
|
|
|
}
|
|
|
|
case refName == git.TagPrefix+name:
|
|
|
|
return models.ErrTagAlreadyExists{
|
|
|
|
TagName: name,
|
2021-11-17 23:17:31 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 03:08:16 +08:00
|
|
|
return nil
|
|
|
|
})
|
2021-11-17 23:17:31 +08:00
|
|
|
|
2021-12-09 03:08:16 +08:00
|
|
|
return err
|
2021-11-17 23:17:31 +08:00
|
|
|
}
|
|
|
|
|
Also sync DB branches on push if necessary (#28361) (#28403)
Fix #28056
Backport #28361
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-11 14:16:56 +08:00
|
|
|
// syncBranchToDB sync the branch information in the database. It 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.
|
|
|
|
func syncBranchToDB(ctx context.Context, repoID, pusherID int64, branchName string, commit *git.Commit) error {
|
|
|
|
cnt, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
|
|
|
|
}
|
|
|
|
if cnt > 0 { // This means branch does exist, so it's a normal update. It also means the branch has been synced.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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: util.OptionalBoolFalse,
|
|
|
|
}.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:%s failed: %v", repoID, branchName, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if database have branches but not this branch, it means this is a new branch
|
|
|
|
return db.Insert(ctx, &git_model.Branch{
|
|
|
|
RepoID: repoID,
|
|
|
|
Name: branchName,
|
|
|
|
CommitID: commit.ID.String(),
|
|
|
|
CommitMessage: commit.Summary(),
|
|
|
|
PusherID: pusherID,
|
|
|
|
CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-17 23:17:31 +08:00
|
|
|
// CreateNewBranchFromCommit creates a new repository branch
|
Also sync DB branches on push if necessary (#28361) (#28403)
Fix #28056
Backport #28361
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-11 14:16:56 +08:00
|
|
|
func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, commitID, branchName string) (err error) {
|
|
|
|
err = repo.MustNotBeArchived()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-17 23:17:31 +08:00
|
|
|
// Check if branch name can be used
|
2022-01-20 07:26:57 +08:00
|
|
|
if err := checkBranchName(ctx, repo, branchName); err != nil {
|
2021-11-17 23:17:31 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:47:02 +08: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 23:17:31 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-12-29 16:47:02 +08:00
|
|
|
return fmt.Errorf("push: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-11-17 23:17:31 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 01:03:04 +08:00
|
|
|
// RenameBranch rename a branch
|
2023-03-01 06:17:51 +08:00
|
|
|
func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
|
2021-10-09 01:03:04 +08: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-15 06:54:22 +08:00
|
|
|
if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error {
|
2021-10-09 01:03:04 +08:00
|
|
|
err2 := gitRepo.RenameBranch(from, to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDefault {
|
2024-01-15 06:54:22 +08: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
|
|
|
|
if err := actions_model.CancelRunningJobs(
|
|
|
|
ctx,
|
|
|
|
repo.ID,
|
|
|
|
from,
|
|
|
|
"",
|
|
|
|
webhook_module.HookEventSchedule,
|
|
|
|
); err != nil {
|
|
|
|
log.Error("CancelRunningJobs: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-09 01:03:04 +08:00
|
|
|
err2 = gitRepo.SetDefaultBranch(to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-05-26 09:04:48 +08:00
|
|
|
refNameTo := git.RefNameFromBranch(to)
|
|
|
|
refID, err := gitRepo.GetRefCommitID(refNameTo.String())
|
2022-01-20 07:26:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-09 01:03:04 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.DeleteRef(ctx, doer, repo, git.RefNameFromBranch(from))
|
|
|
|
notify_service.CreateRef(ctx, doer, repo, refNameTo, refID)
|
2021-10-09 01:03:04 +08:00
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:52:59 +08:00
|
|
|
// enmuerates all branch related errors
|
|
|
|
var (
|
2023-01-16 16:00:22 +08:00
|
|
|
ErrBranchIsDefault = errors.New("branch is default")
|
2021-06-07 22:52:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteBranch delete branch
|
2023-03-01 06:17:51 +08:00
|
|
|
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
|
2021-06-07 22:52:59 +08:00
|
|
|
if branchName == repo.DefaultBranch {
|
|
|
|
return ErrBranchIsDefault
|
|
|
|
}
|
|
|
|
|
2023-03-01 06:17:51 +08:00
|
|
|
isProtected, err := git_model.IsBranchProtected(ctx, repo.ID, branchName)
|
2021-06-07 22:52:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isProtected {
|
2023-01-16 16:00:22 +08:00
|
|
|
return git_model.ErrBranchIsProtected
|
2021-06-07 22:52:59 +08:00
|
|
|
}
|
|
|
|
|
2023-06-29 18:03:20 +08:00
|
|
|
rawBranch, err := git_model.GetBranch(ctx, repo.ID, branchName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetBranch: %vc", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rawBranch.IsDeleted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:52:59 +08:00
|
|
|
commit, err := gitRepo.GetBranchCommit(branchName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:03:20 +08:00
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
if err := git_model.AddDeletedBranch(ctx, repo.ID, branchName, doer.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
|
|
|
|
Force: true,
|
|
|
|
})
|
2021-06-07 22:52:59 +08:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't return error below this
|
|
|
|
if err := PushUpdate(
|
|
|
|
&repo_module.PushUpdateOptions{
|
2023-05-26 09:04:48 +08:00
|
|
|
RefFullName: git.RefNameFromBranch(branchName),
|
2021-06-07 22:52:59 +08:00
|
|
|
OldCommitID: commit.ID.String(),
|
|
|
|
NewCommitID: git.EmptySHA,
|
|
|
|
PusherID: doer.ID,
|
|
|
|
PusherName: doer.Name,
|
|
|
|
RepoUserName: repo.OwnerName,
|
|
|
|
RepoName: repo.Name,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("Update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-29 18:03:20 +08: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRepoToBranchSyncQueue(repoID, doerID int64) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddAllRepoBranchesToSyncQueue(ctx context.Context, doerID int64) error {
|
|
|
|
if err := db.Iterate(ctx, builder.Eq{"is_empty": false}, func(ctx context.Context, repo *repo_model.Repository) error {
|
|
|
|
return addRepoToBranchSyncQueue(repo.ID, doerID)
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("run sync all branches failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-15 06:54:22 +08: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
|
|
|
|
if err := actions_model.CancelRunningJobs(
|
|
|
|
ctx,
|
|
|
|
repo.ID,
|
|
|
|
oldDefaultBranchName,
|
|
|
|
"",
|
|
|
|
webhook_module.HookEventSchedule,
|
|
|
|
); err != nil {
|
|
|
|
log.Error("CancelRunningJobs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gitRepo.SetDefaultBranch(newBranchName); err != nil {
|
|
|
|
if !git.IsErrUnsupportedVersion(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_service.ChangeDefaultBranch(ctx, repo)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|