@@ -15,6 +15,28 @@ type Branch struct {
15
15
AllBranchList []* string
16
16
}
17
17
18
+ // isBranchNameValid function checks if the branch name is valid to be return as an eligible branch
19
+ //
20
+ // The function filters out Tags and stashes returned as references
21
+ func isBranchNameValid (branchName string ) bool {
22
+ var branchNameValid bool
23
+ branchNameValid = true
24
+
25
+ if branchName == "HEAD" {
26
+ branchNameValid = false
27
+ }
28
+
29
+ if ! strings .Contains (branchName , "refs/" ) {
30
+ branchNameValid = false
31
+ }
32
+
33
+ if strings .Contains (branchName , "tags/" ) {
34
+ branchNameValid = false
35
+ }
36
+
37
+ return branchNameValid
38
+ }
39
+
18
40
// GetBranchList fetches all the branches from the target repository
19
41
// The result will be returned as a struct with the current branch and all the available branches
20
42
func GetBranchList (repo * git.Repository , branchChan chan Branch ) {
@@ -50,7 +72,8 @@ func GetBranchList(repo *git.Repository, branchChan chan Branch) {
50
72
refNamePtr * string
51
73
)
52
74
if ref != nil {
53
- if reference .Name ().String () != "HEAD" && strings .Contains (reference .Name ().String (), "refs/" ) {
75
+ referenceName := reference .Name ().String ()
76
+ if isBranchNameValid (referenceName ) {
54
77
refNameSplit := strings .Split (reference .Name ().String (), "refs/" )
55
78
if len (refNameSplit ) == 2 && strings .Contains (refNameSplit [1 ], "/" ) && ! strings .Contains (refNameSplit [1 ], "remotes/" + git .DefaultRemoteName + "/HEAD" ) {
56
79
logger .Log (fmt .Sprintf ("Available Branch : %v" , refNameSplit [1 ]), global .StatusInfo )
@@ -74,12 +97,15 @@ func GetBranchList(repo *git.Repository, branchChan chan Branch) {
74
97
75
98
if bIter != nil {
76
99
_ = bIter .ForEach (func (reference * plumbing.Reference ) error {
77
- if reference != nil {
78
- localBranch := reference .String ()
79
- splitBranch := strings .Split (localBranch , "/" )
80
- localBranch = splitBranch [len (splitBranch )- 1 ]
100
+ if reference != nil && ! strings .Contains (reference .Name ().String (), "tags/" ) {
101
+ localBranch := reference .Name ().String ()
102
+ if isBranchNameValid (localBranch ) {
103
+ splitBranch := strings .Split (localBranch , "/" )
104
+ localBranch = splitBranch [len (splitBranch )- 1 ]
81
105
82
- branches = append (branches , & localBranch )
106
+ logger .Log ("Available Branch : " + localBranch , global .StatusInfo )
107
+ branches = append (branches , & localBranch )
108
+ }
83
109
return nil
84
110
} else {
85
111
return types.Error {Msg : "Empty reference" }
0 commit comments