@@ -5,45 +5,55 @@ package gitrepo
55
66import  (
77	"context" 
8+ 	"errors" 
9+ 	"strings" 
810
911	"code.gitea.io/gitea/modules/git" 
1012)
1113
12- // GetBranchesByPath  returns a  branch by its path  
14+ // GetBranches  returns branch names  by repository  
1315// if limit = 0 it will not limit 
14- func  GetBranchesByPath (ctx  context.Context , repo  Repository , skip , limit  int ) ([]* git.Branch , int , error ) {
15- 	gitRepo , err  :=  OpenRepository (ctx , repo )
16- 	if  err  !=  nil  {
17- 		return  nil , 0 , err 
18- 	}
19- 	defer  gitRepo .Close ()
20- 
21- 	return  gitRepo .GetBranches (skip , limit )
16+ func  GetBranches (ctx  context.Context , repo  Repository , skip , limit  int ) ([]string , int , error ) {
17+ 	branchNames  :=  []string {}
18+ 	countAll , err  :=  curService .WalkShowRef (ctx , repo , git.TrustedCmdArgs {git .BranchPrefix , "--sort=-committerdate" }, skip , limit , func (_ , branchName  string ) error  {
19+ 		branchName  =  strings .TrimPrefix (branchName , git .BranchPrefix )
20+ 		branchNames  =  append (branchNames , branchName )
21+ 
22+ 		return  nil 
23+ 	})
24+ 	return  branchNames , countAll , err 
2225}
2326
2427func  GetBranchCommitID (ctx  context.Context , repo  Repository , branch  string ) (string , error ) {
25- 	gitRepo , err  :=  OpenRepository (ctx , repo )
28+ 	gitRepo , err  :=  curService . OpenRepository (ctx , repo )
2629	if  err  !=  nil  {
2730		return  "" , err 
2831	}
2932	defer  gitRepo .Close ()
30- 
31- 	return  gitRepo .GetBranchCommitID (branch )
33+ 	return  gitRepo .GetRefCommitID (branch )
3234}
3335
3436// SetDefaultBranch sets default branch of repository. 
3537func  SetDefaultBranch (ctx  context.Context , repo  Repository , name  string ) error  {
36- 	_ , _ , err  :=  git .NewCommand (ctx , "symbolic-ref" , "HEAD" ).
37- 		AddDynamicArguments (git .BranchPrefix  +  name ).
38- 		RunStdString (& git.RunOpts {Dir : repoPath (repo )})
39- 	return  err 
38+ 	cmd  :=  git .NewCommand (ctx , "symbolic-ref" , "HEAD" ).
39+ 		AddDynamicArguments (git .BranchPrefix  +  name )
40+ 	return  RunGitCmd (ctx , repo , cmd , & git.RunOpts {})
4041}
4142
4243// GetDefaultBranch gets default branch of repository. 
4344func  GetDefaultBranch (ctx  context.Context , repo  Repository ) (string , error ) {
44- 	return  git .GetDefaultBranch (ctx , repoPath (repo ))
45+ 	cmd  :=  git .NewCommand (ctx , "symbolic-ref" , "HEAD" )
46+ 	stdout , _ , err  :=  RunGitCmdStdString (ctx , repo , cmd , & git.RunOpts {})
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 
4555}
4656
4757func  GetWikiDefaultBranch (ctx  context.Context , repo  Repository ) (string , error ) {
48- 	return  git . GetDefaultBranch (ctx , wikiPath (repo ))
58+ 	return  GetDefaultBranch (ctx , wikiRepo (repo ))
4959}
0 commit comments