2021-06-07 20:22:59 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-12-09 00:38:16 +05:30
|
|
|
"context"
|
2021-06-07 20:22:59 +05:30
|
|
|
"errors"
|
2021-11-17 20:47:31 +05:30
|
|
|
"fmt"
|
2021-12-09 00:38:16 +05:30
|
|
|
"strings"
|
2021-06-07 20:22:59 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-07 20:22:59 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-08 22:33:04 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2021-06-07 20:22:59 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
|
|
)
|
|
|
|
|
2021-11-17 20:47:31 +05:30
|
|
|
// CreateNewBranch creates a new repository branch
|
2022-01-20 04:56:57 +05:30
|
|
|
func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) {
|
2021-11-17 20:47:31 +05:30
|
|
|
// Check if branch name can be used
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := checkBranchName(ctx, repo, branchName); err != nil {
|
2021-11-17 20:47:31 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if !git.IsBranchExist(ctx, repo.RepoPath(), oldBranchName) {
|
2021-11-17 20:47:31 +05:30
|
|
|
return models.ErrBranchDoesNotExist{
|
|
|
|
BranchName: oldBranchName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
|
2021-11-17 20:47:31 +05:30
|
|
|
Remote: repo.RepoPath(),
|
2022-05-01 06:39:59 +05:30
|
|
|
Branch: fmt.Sprintf("%s%s:%s%s", git.BranchPrefix, oldBranchName, git.BranchPrefix, branchName),
|
2021-11-17 20:47:31 +05:30
|
|
|
Env: models.PushingEnvironment(doer, repo),
|
|
|
|
}); err != nil {
|
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Push: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranches returns branches from the repository, skipping skip initial branches and
|
|
|
|
// returning at most limit branches, or all branches if limit is 0.
|
2022-01-20 04:56:57 +05:30
|
|
|
func GetBranches(ctx context.Context, repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) {
|
|
|
|
return git.GetBranchesByPath(ctx, repo.RepoPath(), skip, limit)
|
2021-11-17 20:47:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// checkBranchName validates branch name with existing repository branches
|
2021-12-10 06:57:50 +05:30
|
|
|
func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
|
2022-03-29 22:42:33 +05:30
|
|
|
_, err := git.WalkReferences(ctx, repo.RepoPath(), func(_, refName string) error {
|
2021-12-09 00:38:16 +05:30
|
|
|
branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
|
|
|
|
switch {
|
|
|
|
case branchRefName == name:
|
2021-11-17 20:47:31 +05:30
|
|
|
return models.ErrBranchAlreadyExists{
|
2021-12-09 00:38:16 +05:30
|
|
|
BranchName: name,
|
2021-11-17 20:47:31 +05:30
|
|
|
}
|
2021-12-09 00:38:16 +05:30
|
|
|
// If branchRefName like a/b but we want to create a branch named a then we have a conflict
|
|
|
|
case strings.HasPrefix(branchRefName, name+"/"):
|
2021-11-17 20:47:31 +05:30
|
|
|
return models.ErrBranchNameConflict{
|
2021-12-09 00:38:16 +05:30
|
|
|
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+"/"):
|
|
|
|
return models.ErrBranchNameConflict{
|
|
|
|
BranchName: branchRefName,
|
|
|
|
}
|
|
|
|
case refName == git.TagPrefix+name:
|
|
|
|
return models.ErrTagAlreadyExists{
|
|
|
|
TagName: name,
|
2021-11-17 20:47:31 +05:30
|
|
|
}
|
|
|
|
}
|
2021-12-09 00:38:16 +05:30
|
|
|
return nil
|
|
|
|
})
|
2021-11-17 20:47:31 +05:30
|
|
|
|
2021-12-09 00:38:16 +05:30
|
|
|
return err
|
2021-11-17 20:47:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNewBranchFromCommit creates a new repository branch
|
2022-01-20 04:56:57 +05:30
|
|
|
func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) {
|
2021-11-17 20:47:31 +05:30
|
|
|
// Check if branch name can be used
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := checkBranchName(ctx, repo, branchName); err != nil {
|
2021-11-17 20:47:31 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
|
2021-11-17 20:47:31 +05:30
|
|
|
Remote: repo.RepoPath(),
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
|
|
|
|
Env: models.PushingEnvironment(doer, repo),
|
|
|
|
}); err != nil {
|
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Push: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-08 22:33:04 +05:30
|
|
|
// RenameBranch rename a branch
|
2021-12-10 06:57:50 +05:30
|
|
|
func RenameBranch(repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
|
2021-10-08 22:33:04 +05:30
|
|
|
if from == to {
|
|
|
|
return "target_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitRepo.IsBranchExist(to) {
|
|
|
|
return "target_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gitRepo.IsBranchExist(from) {
|
|
|
|
return "from_not_exist", nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := models.RenameBranch(repo, from, to, func(isDefault bool) error {
|
2021-10-08 22:33:04 +05:30
|
|
|
err2 := gitRepo.RenameBranch(from, to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDefault {
|
|
|
|
err2 = gitRepo.SetDefaultBranch(to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
refID, err := gitRepo.GetRefCommitID(git.BranchPrefix + to)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-08 22:33:04 +05:30
|
|
|
|
2021-12-02 12:58:08 +05:30
|
|
|
notification.NotifyDeleteRef(doer, repo, "branch", git.BranchPrefix+from)
|
2022-01-20 04:56:57 +05:30
|
|
|
notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to, refID)
|
2021-10-08 22:33:04 +05:30
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 20:22:59 +05:30
|
|
|
// enmuerates all branch related errors
|
|
|
|
var (
|
|
|
|
ErrBranchIsDefault = errors.New("branch is default")
|
|
|
|
ErrBranchIsProtected = errors.New("branch is protected")
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteBranch delete branch
|
2021-12-10 06:57:50 +05:30
|
|
|
func DeleteBranch(doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
|
2021-06-07 20:22:59 +05:30
|
|
|
if branchName == repo.DefaultBranch {
|
|
|
|
return ErrBranchIsDefault
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
isProtected, err := models.IsProtectedBranch(repo.ID, branchName)
|
2021-06-07 20:22:59 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isProtected {
|
|
|
|
return ErrBranchIsProtected
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetBranchCommit(branchName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
|
|
|
|
Force: true,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pull_service.CloseBranchPulls(doer, repo.ID, branchName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't return error below this
|
|
|
|
if err := PushUpdate(
|
|
|
|
&repo_module.PushUpdateOptions{
|
|
|
|
RefFullName: git.BranchPrefix + branchName,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := models.AddDeletedBranch(repo.ID, branchName, commit.ID.String(), doer.ID); err != nil {
|
2021-06-07 20:22:59 +05:30
|
|
|
log.Warn("AddDeletedBranch: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|