|
@@ -173,6 +173,106 @@ func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) { |
|
|
return callShowRef(repo.Path, BranchPrefix, "--heads", skip, limit) |
|
|
return callShowRef(repo.Path, BranchPrefix, "--heads", skip, limit) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { |
|
|
|
|
|
return callShowRefNew(repo.Path, BranchPrefix, []string{BranchPrefix, "--sort=-committerdate"}, skip, limit) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func callShowRefNew(repoPath, trimPrefix string, extraArgs []string, skip, limit int) (branchNames []string, countAll int, err error) { |
|
|
|
|
|
countAll, err = walkShowRef(repoPath, extraArgs, skip, limit, func(_, branchName string) error { |
|
|
|
|
|
branchName = strings.TrimPrefix(branchName, trimPrefix) |
|
|
|
|
|
branchNames = append(branchNames, branchName) |
|
|
|
|
|
return nil |
|
|
|
|
|
}) |
|
|
|
|
|
return branchNames, countAll, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func walkShowRef(repoPath string, extraArgs []string, skip, limit int, walkfn func(sha1, refname string) error) (countAll int, err error) { |
|
|
|
|
|
stdoutReader, stdoutWriter := io.Pipe() |
|
|
|
|
|
defer func() { |
|
|
|
|
|
_ = stdoutReader.Close() |
|
|
|
|
|
_ = stdoutWriter.Close() |
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
stderrBuilder := &strings.Builder{} |
|
|
|
|
|
args := []string{"for-each-ref", "--format=%(objectname) %(refname)"} |
|
|
|
|
|
args = append(args, extraArgs...) |
|
|
|
|
|
err := NewCommand(args...).RunInDirPipeline(repoPath, stdoutWriter, stderrBuilder) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
if stderrBuilder.Len() == 0 { |
|
|
|
|
|
_ = stdoutWriter.Close() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String())) |
|
|
|
|
|
} else { |
|
|
|
|
|
_ = stdoutWriter.Close() |
|
|
|
|
|
} |
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
i := 0 |
|
|
|
|
|
bufReader := bufio.NewReader(stdoutReader) |
|
|
|
|
|
for i < skip { |
|
|
|
|
|
_, isPrefix, err := bufReader.ReadLine() |
|
|
|
|
|
if err == io.EOF { |
|
|
|
|
|
return i, nil |
|
|
|
|
|
} |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return 0, err |
|
|
|
|
|
} |
|
|
|
|
|
if !isPrefix { |
|
|
|
|
|
i++ |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
for limit == 0 || i < skip+limit { |
|
|
|
|
|
// The output of show-ref is simply a list: |
|
|
|
|
|
// <sha> SP <ref> LF |
|
|
|
|
|
sha, err := bufReader.ReadString(' ') |
|
|
|
|
|
if err == io.EOF { |
|
|
|
|
|
return i, nil |
|
|
|
|
|
} |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
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 i, nil |
|
|
|
|
|
} |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return i, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(branchName) > 0 { |
|
|
|
|
|
branchName = branchName[:len(branchName)-1] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(sha) > 0 { |
|
|
|
|
|
sha = sha[:len(sha)-1] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = walkfn(sha, branchName) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return i, err |
|
|
|
|
|
} |
|
|
|
|
|
i++ |
|
|
|
|
|
} |
|
|
|
|
|
// count all refs |
|
|
|
|
|
for limit != 0 { |
|
|
|
|
|
_, isPrefix, err := bufReader.ReadLine() |
|
|
|
|
|
if err == io.EOF { |
|
|
|
|
|
return i, nil |
|
|
|
|
|
} |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return 0, err |
|
|
|
|
|
} |
|
|
|
|
|
if !isPrefix { |
|
|
|
|
|
i++ |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return i, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// callShowRef return refs, if limit = 0 it will not limit |
|
|
// 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) { |
|
|
func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) { |
|
|
stdoutReader, stdoutWriter := io.Pipe() |
|
|
stdoutReader, stdoutWriter := io.Pipe() |
|
|