Skip to content

Commit 56fdf96

Browse files
committed
Add missed branch.go
1 parent 949fd49 commit 56fdf96

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

services/repository/branch.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repository
6+
7+
import (
8+
"errors"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/log"
13+
repo_module "code.gitea.io/gitea/modules/repository"
14+
pull_service "code.gitea.io/gitea/services/pull"
15+
)
16+
17+
// enmuerates all branch related errors
18+
var (
19+
ErrBranchIsDefault = errors.New("branch is default")
20+
ErrBranchIsProtected = errors.New("branch is protected")
21+
)
22+
23+
// DeleteBranch delete branch
24+
func DeleteBranch(doer *models.User, repo *models.Repository, gitRepo *git.Repository, branchName string) error {
25+
if branchName == repo.DefaultBranch {
26+
return ErrBranchIsDefault
27+
}
28+
29+
isProtected, err := repo.IsProtectedBranch(branchName, doer)
30+
if err != nil {
31+
return err
32+
}
33+
34+
if isProtected {
35+
return ErrBranchIsProtected
36+
}
37+
38+
commit, err := gitRepo.GetBranchCommit(branchName)
39+
if err != nil {
40+
return err
41+
}
42+
43+
if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
44+
Force: true,
45+
}); err != nil {
46+
return err
47+
}
48+
49+
if err := pull_service.CloseBranchPulls(doer, repo.ID, branchName); err != nil {
50+
return err
51+
}
52+
53+
// Don't return error below this
54+
if err := PushUpdate(
55+
&repo_module.PushUpdateOptions{
56+
RefFullName: git.BranchPrefix + branchName,
57+
OldCommitID: commit.ID.String(),
58+
NewCommitID: git.EmptySHA,
59+
PusherID: doer.ID,
60+
PusherName: doer.Name,
61+
RepoUserName: repo.OwnerName,
62+
RepoName: repo.Name,
63+
}); err != nil {
64+
log.Error("Update: %v", err)
65+
}
66+
67+
if err := repo.AddDeletedBranch(branchName, commit.ID.String(), doer.ID); err != nil {
68+
log.Warn("AddDeletedBranch: %v", err)
69+
}
70+
71+
return nil
72+
}

0 commit comments

Comments
 (0)