2021-06-07 08:52:59 -06:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-07 08:52:59 -06:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-12-08 12:08:16 -07:00
|
|
|
"context"
|
2021-06-07 08:52:59 -06:00
|
|
|
"errors"
|
2021-11-17 08:17:31 -07:00
|
|
|
"fmt"
|
2021-12-08 12:08:16 -07:00
|
|
|
"strings"
|
2021-06-07 08:52:59 -06:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-06-12 09:51:54 -06:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-07 08:52:59 -06:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-08 11:03:04 -06:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2021-06-07 08:52:59 -06:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
|
|
)
|
|
|
|
|
2021-11-17 08:17:31 -07:00
|
|
|
// CreateNewBranch creates a new repository branch
|
2022-01-19 16:26:57 -07:00
|
|
|
func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) {
|
2021-11-17 08:17:31 -07:00
|
|
|
// Check if branch name can be used
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := checkBranchName(ctx, repo, branchName); err != nil {
|
2021-11-17 08:17:31 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:26:57 -07:00
|
|
|
if !git.IsBranchExist(ctx, repo.RepoPath(), oldBranchName) {
|
2021-11-17 08:17:31 -07:00
|
|
|
return models.ErrBranchDoesNotExist{
|
|
|
|
BranchName: oldBranchName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
|
2021-11-17 08:17:31 -07:00
|
|
|
Remote: repo.RepoPath(),
|
2022-04-30 19:09:59 -06:00
|
|
|
Branch: fmt.Sprintf("%s%s:%s%s", git.BranchPrefix, oldBranchName, git.BranchPrefix, branchName),
|
2022-05-08 10:46:32 -06:00
|
|
|
Env: repo_module.PushingEnvironment(doer, repo),
|
2021-11-17 08:17:31 -07:00
|
|
|
}); err != nil {
|
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("Push: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-19 16:26:57 -07:00
|
|
|
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 08:17:31 -07:00
|
|
|
}
|
|
|
|
|
2023-05-13 15:59:01 -06:00
|
|
|
func GetBranchCommitID(ctx context.Context, repo *repo_model.Repository, branch string) (string, error) {
|
|
|
|
return git.GetBranchCommitID(ctx, repo.RepoPath(), branch)
|
|
|
|
}
|
|
|
|
|
2021-11-17 08:17:31 -07:00
|
|
|
// checkBranchName validates branch name with existing repository branches
|
2021-12-09 18:27:50 -07:00
|
|
|
func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
|
2022-03-29 11:12:33 -06:00
|
|
|
_, err := git.WalkReferences(ctx, repo.RepoPath(), func(_, refName string) error {
|
2021-12-08 12:08:16 -07:00
|
|
|
branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
|
|
|
|
switch {
|
|
|
|
case branchRefName == name:
|
2021-11-17 08:17:31 -07:00
|
|
|
return models.ErrBranchAlreadyExists{
|
2021-12-08 12:08:16 -07:00
|
|
|
BranchName: name,
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
2021-12-08 12:08:16 -07: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+"/"):
|
2021-11-17 08:17:31 -07:00
|
|
|
return models.ErrBranchNameConflict{
|
2021-12-08 12:08:16 -07: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+"/"):
|
|
|
|
return models.ErrBranchNameConflict{
|
|
|
|
BranchName: branchRefName,
|
|
|
|
}
|
|
|
|
case refName == git.TagPrefix+name:
|
|
|
|
return models.ErrTagAlreadyExists{
|
|
|
|
TagName: name,
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 12:08:16 -07:00
|
|
|
return nil
|
|
|
|
})
|
2021-11-17 08:17:31 -07:00
|
|
|
|
2021-12-08 12:08:16 -07:00
|
|
|
return err
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNewBranchFromCommit creates a new repository branch
|
2022-01-19 16:26:57 -07:00
|
|
|
func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) {
|
2021-11-17 08:17:31 -07:00
|
|
|
// Check if branch name can be used
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := checkBranchName(ctx, repo, branchName); err != nil {
|
2021-11-17 08:17:31 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
|
2021-11-17 08:17:31 -07:00
|
|
|
Remote: repo.RepoPath(),
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
|
2022-05-08 10:46:32 -06:00
|
|
|
Env: repo_module.PushingEnvironment(doer, repo),
|
2021-11-17 08:17:31 -07:00
|
|
|
}); err != nil {
|
|
|
|
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("Push: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-08 11:03:04 -06:00
|
|
|
// RenameBranch rename a branch
|
2023-02-28 15:17:51 -07:00
|
|
|
func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
|
2021-10-08 11:03:04 -06: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
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:17:51 -07:00
|
|
|
if err := git_model.RenameBranch(ctx, repo, from, to, func(isDefault bool) error {
|
2021-10-08 11:03:04 -06:00
|
|
|
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
|
|
|
|
}
|
2023-05-25 19:04:48 -06:00
|
|
|
refNameTo := git.RefNameFromBranch(to)
|
|
|
|
refID, err := gitRepo.GetRefCommitID(refNameTo.String())
|
2022-01-19 16:26:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-08 11:03:04 -06:00
|
|
|
|
2023-05-25 19:04:48 -06:00
|
|
|
notification.NotifyDeleteRef(ctx, doer, repo, git.RefNameFromBranch(from))
|
|
|
|
notification.NotifyCreateRef(ctx, doer, repo, refNameTo, refID)
|
2021-10-08 11:03:04 -06:00
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 08:52:59 -06:00
|
|
|
// enmuerates all branch related errors
|
|
|
|
var (
|
2023-01-16 01:00:22 -07:00
|
|
|
ErrBranchIsDefault = errors.New("branch is default")
|
2021-06-07 08:52:59 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteBranch delete branch
|
2023-02-28 15:17:51 -07:00
|
|
|
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
|
2021-06-07 08:52:59 -06:00
|
|
|
if branchName == repo.DefaultBranch {
|
|
|
|
return ErrBranchIsDefault
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:17:51 -07:00
|
|
|
isProtected, err := git_model.IsBranchProtected(ctx, repo.ID, branchName)
|
2021-06-07 08:52:59 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isProtected {
|
2023-01-16 01:00:22 -07:00
|
|
|
return git_model.ErrBranchIsProtected
|
2021-06-07 08:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetBranchCommit(branchName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
|
|
|
|
Force: true,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't return error below this
|
|
|
|
if err := PushUpdate(
|
|
|
|
&repo_module.PushUpdateOptions{
|
2023-05-25 19:04:48 -06:00
|
|
|
RefFullName: git.RefNameFromBranch(branchName),
|
2021-06-07 08:52:59 -06: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
|
|
|
|
}
|