Skip to content

Replace 'show-ref' command with 'branch' and 'tag' #14201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions modules/git/repo_branch_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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:
// <sha> SP <ref> 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the old comment is wrong, right? The position is where the return value is. So i moved it here which makes more sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we should just delete this comment. The old position just does not seem right to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should still be the original position. It means err == io.EOF in fact is not an err. At a normal situation, reader shouldn't return that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunny has the correct interpretation. Please revert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I misunderstanding this? The err == io.EOF seems to be the only way to break out the loop normally, but "at a normal situation, reader shouldn't return that"? How should we break out the loop, though

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)
}
}
54 changes: 53 additions & 1 deletion modules/git/repo_tag_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,64 @@

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)
}

// 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)
}
}