Skip to content

Commit 1389ab4

Browse files
committed
Replace 'show-ref' command with 'branch' and 'tag'
1 parent 632800e commit 1389ab4

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

modules/git/repo_branch_nogogit.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func (repo *Repository) IsBranchExist(name string) bool {
2323

2424
// GetBranches returns all branches of the repository.
2525
func (repo *Repository) GetBranches() ([]string, error) {
26-
return callShowRef(repo.Path, BranchPrefix, "--heads")
26+
return callBranch(repo.Path, "--list")
2727
}
2828

29-
func callShowRef(repoPath, prefix, arg string) ([]string, error) {
29+
func callBranch(repoPath, arg string) ([]string, error) {
3030
var branchNames []string
3131

3232
stdoutReader, stdoutWriter := io.Pipe()
@@ -37,7 +37,7 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
3737

3838
go func() {
3939
stderrBuilder := &strings.Builder{}
40-
err := NewCommand("show-ref", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
40+
err := NewCommand("branch", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
4141
if err != nil {
4242
if stderrBuilder.Len() == 0 {
4343
_ = stdoutWriter.Close()
@@ -51,32 +51,19 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
5151

5252
bufReader := bufio.NewReader(stdoutReader)
5353
for {
54-
// The output of show-ref is simply a list:
55-
// <sha> SP <ref> LF
56-
_, err := bufReader.ReadSlice(' ')
57-
for err == bufio.ErrBufferFull {
58-
// This shouldn't happen but we'll tolerate it for the sake of peace
59-
_, err = bufReader.ReadSlice(' ')
60-
}
61-
if err == io.EOF {
62-
return branchNames, nil
63-
}
64-
if err != nil {
65-
return nil, err
66-
}
67-
54+
// The output of branch is simply a list:
55+
// LF
6856
branchName, err := bufReader.ReadString('\n')
6957
if err == io.EOF {
70-
// This shouldn't happen... but we'll tolerate it for the sake of peace
7158
return branchNames, nil
7259
}
7360
if err != nil {
61+
// This shouldn't happen... but we'll tolerate it for the sake of peace
7462
return nil, err
7563
}
76-
branchName = strings.TrimPrefix(branchName, prefix)
77-
if len(branchName) > 0 {
78-
branchName = branchName[:len(branchName)-1]
79-
}
64+
// Current branch will have '*' as prefix.
65+
branchName = strings.TrimPrefix(branchName, "*")
66+
branchName = strings.TrimSpace(branchName)
8067
branchNames = append(branchNames, branchName)
8168
}
8269
}

modules/git/repo_tag_nogogit.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,64 @@
77

88
package git
99

10+
import (
11+
"bufio"
12+
"io"
13+
"strings"
14+
)
15+
1016
// IsTagExist returns true if given tag exists in the repository.
1117
func (repo *Repository) IsTagExist(name string) bool {
1218
return IsReferenceExist(repo.Path, TagPrefix+name)
1319
}
1420

1521
// GetTags returns all tags of the repository.
1622
func (repo *Repository) GetTags() ([]string, error) {
17-
return callShowRef(repo.Path, TagPrefix, "--tags")
23+
return callTag(repo.Path, "--list")
24+
}
25+
26+
func callTag(repoPath, arg string) ([]string, error) {
27+
var tagNames []string
28+
29+
stdoutReader, stdoutWriter := io.Pipe()
30+
defer func() {
31+
_ = stdoutReader.Close()
32+
_ = stdoutWriter.Close()
33+
}()
34+
35+
go func() {
36+
stderrBuilder := &strings.Builder{}
37+
err := NewCommand("tag", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
38+
if err != nil {
39+
if stderrBuilder.Len() == 0 {
40+
_ = stdoutWriter.Close()
41+
return
42+
}
43+
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
44+
} else {
45+
_ = stdoutWriter.Close()
46+
}
47+
}()
48+
49+
bufReader := bufio.NewReader(stdoutReader)
50+
for {
51+
// The output of tag is simply a list:
52+
// LF
53+
tagName, err := bufReader.ReadString('\n')
54+
if err == io.EOF {
55+
// Reverse order
56+
for i := 0; i < len(tagNames)/2; i++ {
57+
j := len(tagNames) - i - 1
58+
tagNames[i], tagNames[j] = tagNames[j], tagNames[i]
59+
}
60+
61+
return tagNames, nil
62+
}
63+
if err != nil {
64+
// This shouldn't happen... but we'll tolerate it for the sake of peace
65+
return nil, err
66+
}
67+
tagName = strings.TrimSpace(tagName)
68+
tagNames = append(tagNames, tagName)
69+
}
1870
}

0 commit comments

Comments
 (0)