diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 5ec46d725e4b4..c95f6953ae091 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -23,10 +23,10 @@ func (repo *Repository) IsBranchExist(name string) bool { // GetBranches returns all branches of the repository. func (repo *Repository) GetBranches() ([]string, error) { - return callShowRef(repo.Path, BranchPrefix, "--heads") + return callBranch(repo.Path, "--list") } -func callShowRef(repoPath, prefix, arg string) ([]string, error) { +func callBranch(repoPath, arg string) ([]string, error) { var branchNames []string stdoutReader, stdoutWriter := io.Pipe() @@ -37,7 +37,7 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) { go func() { stderrBuilder := &strings.Builder{} - err := NewCommand("show-ref", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder) + err := NewCommand("branch", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder) if err != nil { if stderrBuilder.Len() == 0 { _ = stdoutWriter.Close() @@ -51,32 +51,19 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) { bufReader := bufio.NewReader(stdoutReader) for { - // The output of show-ref is simply a list: - // SP LF - _, err := bufReader.ReadSlice(' ') - for err == bufio.ErrBufferFull { - // This shouldn't happen but we'll tolerate it for the sake of peace - _, err = bufReader.ReadSlice(' ') - } - if err == io.EOF { - return branchNames, nil - } - if err != nil { - return nil, err - } - + // The output of branch is simply a list: + // LF branchName, err := bufReader.ReadString('\n') if err == io.EOF { - // This shouldn't happen... but we'll tolerate it for the sake of peace return branchNames, nil } if err != nil { + // This shouldn't happen... but we'll tolerate it for the sake of peace return nil, err } - branchName = strings.TrimPrefix(branchName, prefix) - if len(branchName) > 0 { - branchName = branchName[:len(branchName)-1] - } + // Current branch will have '*' as prefix. + branchName = strings.TrimPrefix(branchName, "*") + branchName = strings.TrimSpace(branchName) branchNames = append(branchNames, branchName) } } diff --git a/modules/git/repo_tag_nogogit.go b/modules/git/repo_tag_nogogit.go index 83cbc58e342ee..abe6017393ef4 100644 --- a/modules/git/repo_tag_nogogit.go +++ b/modules/git/repo_tag_nogogit.go @@ -7,6 +7,12 @@ package git +import ( + "bufio" + "io" + "strings" +) + // IsTagExist returns true if given tag exists in the repository. func (repo *Repository) IsTagExist(name string) bool { return IsReferenceExist(repo.Path, TagPrefix+name) @@ -14,5 +20,51 @@ func (repo *Repository) IsTagExist(name string) bool { // GetTags returns all tags of the repository. func (repo *Repository) GetTags() ([]string, error) { - return callShowRef(repo.Path, TagPrefix, "--tags") + return callTag(repo.Path, "--list") +} + +func callTag(repoPath, arg string) ([]string, error) { + var tagNames []string + + stdoutReader, stdoutWriter := io.Pipe() + defer func() { + _ = stdoutReader.Close() + _ = stdoutWriter.Close() + }() + + go func() { + stderrBuilder := &strings.Builder{} + err := NewCommand("tag", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder) + if err != nil { + if stderrBuilder.Len() == 0 { + _ = stdoutWriter.Close() + return + } + _ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String())) + } else { + _ = stdoutWriter.Close() + } + }() + + bufReader := bufio.NewReader(stdoutReader) + for { + // The output of tag is simply a list: + // LF + tagName, err := bufReader.ReadString('\n') + if err == io.EOF { + // Reverse order + for i := 0; i < len(tagNames)/2; i++ { + j := len(tagNames) - i - 1 + tagNames[i], tagNames[j] = tagNames[j], tagNames[i] + } + + return tagNames, nil + } + if err != nil { + // This shouldn't happen... but we'll tolerate it for the sake of peace + return nil, err + } + tagName = strings.TrimSpace(tagName) + tagNames = append(tagNames, tagName) + } }