2020-12-17 09:00:47 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 09:00:47 -05:00
|
|
|
|
2021-08-24 12:47:09 -04:00
|
|
|
//go:build !gogit
|
2020-12-17 09:00:47 -05:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRefsFiltered returns all references of the repository that matches patterm exactly or starting with.
|
|
|
|
func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
|
|
|
|
stdoutReader, stdoutWriter := io.Pipe()
|
|
|
|
defer func() {
|
|
|
|
_ = stdoutReader.Close()
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stderrBuilder := &strings.Builder{}
|
2022-03-31 22:55:30 -04:00
|
|
|
err := NewCommand(repo.Ctx, "for-each-ref").Run(&RunOpts{
|
|
|
|
Dir: repo.Path,
|
|
|
|
Stdout: stdoutWriter,
|
|
|
|
Stderr: stderrBuilder,
|
2022-02-11 07:47:22 -05:00
|
|
|
})
|
2020-12-17 09:00:47 -05:00
|
|
|
if err != nil {
|
|
|
|
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
|
|
|
|
} else {
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
refs := make([]*Reference, 0)
|
|
|
|
bufReader := bufio.NewReader(stdoutReader)
|
|
|
|
for {
|
|
|
|
// The output of for-each-ref is simply a list:
|
|
|
|
// <sha> SP <type> TAB <ref> LF
|
|
|
|
sha, err := bufReader.ReadString(' ')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sha = sha[:len(sha)-1]
|
|
|
|
|
|
|
|
typ, err := bufReader.ReadString('\t')
|
|
|
|
if err == io.EOF {
|
|
|
|
// This should not happen, but we'll tolerate it
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
typ = typ[:len(typ)-1]
|
|
|
|
|
|
|
|
refName, err := bufReader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
// This should not happen, but we'll tolerate it
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
refName = refName[:len(refName)-1]
|
|
|
|
|
|
|
|
// refName cannot be HEAD but can be remotes or stash
|
2021-12-02 02:28:08 -05:00
|
|
|
if strings.HasPrefix(refName, RemotePrefix) || refName == "/refs/stash" {
|
2020-12-17 09:00:47 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if pattern == "" || strings.HasPrefix(refName, pattern) {
|
|
|
|
r := &Reference{
|
|
|
|
Name: refName,
|
2023-12-19 02:20:47 -05:00
|
|
|
Object: MustIDFromString(sha),
|
2020-12-17 09:00:47 -05:00
|
|
|
Type: typ,
|
|
|
|
repo: repo,
|
|
|
|
}
|
|
|
|
refs = append(refs, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs, nil
|
|
|
|
}
|