Compare commits

...

1 Commits

Author SHA1 Message Date
  lewis 86517e1a36 test 3 years ago
1 changed files with 29 additions and 12 deletions
Split View
  1. +29
    -12
      modules/git/repo_branch.go

+ 29
- 12
modules/git/repo_branch.go View File

@@ -175,6 +175,16 @@ func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) {

// callShowRef return refs, if limit = 0 it will not limit
func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) {
countAll, err = walkShowRef(repoPath, arg, skip, limit, func(branchName string) error {
branchName = strings.TrimPrefix(branchName, prefix)
branchNames = append(branchNames, branchName)

return nil
})
return
}

func walkShowRef(repoPath, arg string, skip, limit int, walkfn func(string) error) (countAll int, err error) {
stdoutReader, stdoutWriter := io.Pipe()
defer func() {
_ = stdoutReader.Close()
@@ -183,7 +193,11 @@ func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []s

go func() {
stderrBuilder := &strings.Builder{}
err := NewCommand("show-ref", arg).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
args := []string{"show-ref"}
if arg != "" {
args = append(args, arg)
}
err := NewCommand(args...).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder)
if err != nil {
if stderrBuilder.Len() == 0 {
_ = stdoutWriter.Close()
@@ -200,10 +214,10 @@ func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []s
for i < skip {
_, isPrefix, err := bufReader.ReadLine()
if err == io.EOF {
return branchNames, i, nil
return i, nil
}
if err != nil {
return nil, 0, err
return 0, err
}
if !isPrefix {
i++
@@ -218,39 +232,42 @@ func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []s
_, err = bufReader.ReadSlice(' ')
}
if err == io.EOF {
return branchNames, i, nil
return i, nil
}
if err != nil {
return nil, 0, err
return 0, err
}

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, i, nil
return i, nil
}
if err != nil {
return nil, i, err
return i, err
}
branchName = strings.TrimPrefix(branchName, prefix)
if len(branchName) > 0 {
branchName = branchName[:len(branchName)-1]
}
branchNames = append(branchNames, branchName)
err = walkfn(branchName)
if err != nil {
return i, err
}
i++
}
// count all refs
for limit != 0 {
_, isPrefix, err := bufReader.ReadLine()
if err == io.EOF {
return branchNames, i, nil
return i, nil
}
if err != nil {
return nil, 0, err
return 0, err
}
if !isPrefix {
i++
}
}
return branchNames, i, nil
return i, nil
}

Loading…
Cancel
Save