@@ -5,6 +5,8 @@ package gitrepo
55
66import  (
77	"context" 
8+ 	"errors" 
9+ 	"strings" 
810
911	"code.gitea.io/gitea/modules/git" 
1012	"code.gitea.io/gitea/modules/git/gitcmd" 
@@ -34,23 +36,61 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
3436
3537// SetDefaultBranch sets default branch of repository. 
3638func  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 ))
4041	return  err 
4142}
4243
4344// GetDefaultBranch gets default branch of repository. 
4445func  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 
4655}
4756
4857// IsReferenceExist returns true if given reference exists in the repository. 
4958func  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 
5161}
5262
5363// IsBranchExist returns true if given branch exists in the repository. 
5464func  IsBranchExist (ctx  context.Context , repo  Repository , name  string ) bool  {
5565	return  IsReferenceExist (ctx , repo , git .BranchPrefix + name )
5666}
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