@@ -5,6 +5,8 @@ package gitrepo
5
5
6
6
import (
7
7
"context"
8
+ "errors"
9
+ "strings"
8
10
9
11
"code.gitea.io/gitea/modules/git"
10
12
"code.gitea.io/gitea/modules/git/gitcmd"
@@ -34,23 +36,61 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
34
36
35
37
// SetDefaultBranch sets default branch of repository.
36
38
func SetDefaultBranch (ctx context.Context , repo Repository , name string ) error {
37
- _ , _ , err := gitcmd .NewCommand ("symbolic-ref" , "HEAD" ).
38
- AddDynamicArguments (git .BranchPrefix + name ).
39
- RunStdString (ctx , & gitcmd.RunOpts {Dir : repoPath (repo )})
39
+ _ , err := runCmdString (ctx , repo , gitcmd .NewCommand ("symbolic-ref" , "HEAD" ).
40
+ AddDynamicArguments (git .BranchPrefix + name ))
40
41
return err
41
42
}
42
43
43
44
// GetDefaultBranch gets default branch of repository.
44
45
func GetDefaultBranch (ctx context.Context , repo Repository ) (string , error ) {
45
- return git .GetDefaultBranch (ctx , repoPath (repo ))
46
+ stdout , err := runCmdString (ctx , repo , gitcmd .NewCommand ("symbolic-ref" , "HEAD" ))
47
+ if err != nil {
48
+ return "" , err
49
+ }
50
+ stdout = strings .TrimSpace (stdout )
51
+ if ! strings .HasPrefix (stdout , git .BranchPrefix ) {
52
+ return "" , errors .New ("the HEAD is not a branch: " + stdout )
53
+ }
54
+ return strings .TrimPrefix (stdout , git .BranchPrefix ), nil
46
55
}
47
56
48
57
// IsReferenceExist returns true if given reference exists in the repository.
49
58
func IsReferenceExist (ctx context.Context , repo Repository , name string ) bool {
50
- return git .IsReferenceExist (ctx , repoPath (repo ), name )
59
+ _ , err := runCmdString (ctx , repo , gitcmd .NewCommand ("show-ref" , "--verify" ).AddDashesAndList (name ))
60
+ return err == nil
51
61
}
52
62
53
63
// IsBranchExist returns true if given branch exists in the repository.
54
64
func IsBranchExist (ctx context.Context , repo Repository , name string ) bool {
55
65
return IsReferenceExist (ctx , repo , git .BranchPrefix + name )
56
66
}
67
+
68
+ // DeleteBranch delete a branch by name on repository.
69
+ func DeleteBranch (ctx context.Context , repo Repository , name string , force bool ) error {
70
+ cmd := gitcmd .NewCommand ("branch" )
71
+
72
+ if force {
73
+ cmd .AddArguments ("-D" )
74
+ } else {
75
+ cmd .AddArguments ("-d" )
76
+ }
77
+
78
+ cmd .AddDashesAndList (name )
79
+ _ , err := runCmdString (ctx , repo , cmd )
80
+ return err
81
+ }
82
+
83
+ // CreateBranch create a new branch
84
+ func CreateBranch (ctx context.Context , repo Repository , branch , oldbranchOrCommit string ) error {
85
+ cmd := gitcmd .NewCommand ("branch" )
86
+ cmd .AddDashesAndList (branch , oldbranchOrCommit )
87
+
88
+ _ , err := runCmdString (ctx , repo , cmd )
89
+ return err
90
+ }
91
+
92
+ // RenameBranch rename a branch
93
+ func RenameBranch (ctx context.Context , repo Repository , from , to string ) error {
94
+ _ , err := runCmdString (ctx , repo , gitcmd .NewCommand ("branch" , "-m" ).AddDynamicArguments (from , to ))
95
+ return err
96
+ }
0 commit comments