2020-12-17 22:00:47 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build !gogit
|
2020-12-17 22:00:47 +08:00
|
|
|
// +build !gogit
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-06-21 06:39:12 +08:00
|
|
|
"bytes"
|
2021-12-01 04:06:32 +08:00
|
|
|
"context"
|
2020-12-17 22:00:47 +08:00
|
|
|
"io"
|
|
|
|
"strings"
|
2021-06-26 00:54:08 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 22:00:47 +08:00
|
|
|
)
|
|
|
|
|
2021-06-21 06:39:12 +08:00
|
|
|
// IsObjectExist returns true if given reference exists in the repository.
|
|
|
|
func (repo *Repository) IsObjectExist(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-01 04:06:32 +08:00
|
|
|
wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
|
2021-06-21 06:39:12 +08:00
|
|
|
defer cancel()
|
|
|
|
_, err := wr.Write([]byte(name + "\n"))
|
|
|
|
if err != nil {
|
2021-06-26 00:54:08 +08:00
|
|
|
log.Debug("Error writing to CatFileBatchCheck %v", err)
|
2021-06-21 06:39:12 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
sha, _, _, err := ReadBatchLine(rd)
|
|
|
|
return err == nil && bytes.HasPrefix(sha, []byte(strings.TrimSpace(name)))
|
|
|
|
}
|
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
// IsReferenceExist returns true if given reference exists in the repository.
|
|
|
|
func (repo *Repository) IsReferenceExist(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-01 04:06:32 +08:00
|
|
|
wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
|
2021-05-10 09:27:03 +08:00
|
|
|
defer cancel()
|
|
|
|
_, err := wr.Write([]byte(name + "\n"))
|
|
|
|
if err != nil {
|
2021-06-26 00:54:08 +08:00
|
|
|
log.Debug("Error writing to CatFileBatchCheck %v", err)
|
2021-05-10 09:27:03 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, _, _, err = ReadBatchLine(rd)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:00:47 +08:00
|
|
|
// IsBranchExist returns true if given branch exists in current repository.
|
|
|
|
func (repo *Repository) IsBranchExist(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-10 09:27:03 +08:00
|
|
|
|
|
|
|
return repo.IsReferenceExist(BranchPrefix + name)
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-09 03:08:16 +08:00
|
|
|
// GetBranchNames returns branches from the repository, skipping skip initial branches and
|
2021-02-04 03:06:13 +08:00
|
|
|
// returning at most limit branches, or all branches if limit is 0.
|
2021-12-09 03:08:16 +08:00
|
|
|
func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
|
2021-12-01 04:06:32 +08:00
|
|
|
return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "--heads", skip, limit)
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-09 03:08:16 +08:00
|
|
|
// WalkReferences walks all the references from the repository
|
|
|
|
func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) error) (int, error) {
|
|
|
|
return walkShowRef(ctx, repoPath, "", 0, 0, walkfn)
|
|
|
|
}
|
|
|
|
|
2021-02-04 03:06:13 +08:00
|
|
|
// callShowRef return refs, if limit = 0 it will not limit
|
2021-12-01 04:06:32 +08:00
|
|
|
func callShowRef(ctx context.Context, repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) {
|
2021-12-09 03:08:16 +08:00
|
|
|
countAll, err = walkShowRef(ctx, repoPath, arg, skip, limit, func(branchName string) error {
|
|
|
|
branchName = strings.TrimPrefix(branchName, prefix)
|
|
|
|
branchNames = append(branchNames, branchName)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, walkfn func(string) error) (countAll int, err error) {
|
2020-12-17 22:00:47 +08:00
|
|
|
stdoutReader, stdoutWriter := io.Pipe()
|
|
|
|
defer func() {
|
|
|
|
_ = stdoutReader.Close()
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stderrBuilder := &strings.Builder{}
|
2021-12-09 03:08:16 +08:00
|
|
|
args := []string{"show-ref"}
|
|
|
|
if arg != "" {
|
|
|
|
args = append(args, arg)
|
|
|
|
}
|
2022-02-11 20:47:22 +08:00
|
|
|
err := NewCommand(ctx, args...).RunWithContext(&RunContext{
|
|
|
|
Timeout: -1,
|
|
|
|
Dir: repoPath,
|
|
|
|
Stdout: stdoutWriter,
|
|
|
|
Stderr: stderrBuilder,
|
|
|
|
})
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
|
|
|
if stderrBuilder.Len() == 0 {
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = stdoutWriter.CloseWithError(ConcatenateError(err, stderrBuilder.String()))
|
|
|
|
} else {
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-04 03:06:13 +08:00
|
|
|
i := 0
|
2020-12-17 22:00:47 +08:00
|
|
|
bufReader := bufio.NewReader(stdoutReader)
|
2021-02-04 03:06:13 +08:00
|
|
|
for i < skip {
|
|
|
|
_, isPrefix, err := bufReader.ReadLine()
|
|
|
|
if err == io.EOF {
|
2021-12-09 03:08:16 +08:00
|
|
|
return i, nil
|
2021-02-04 03:06:13 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-12-09 03:08:16 +08:00
|
|
|
return 0, err
|
2021-02-04 03:06:13 +08:00
|
|
|
}
|
|
|
|
if !isPrefix {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for limit == 0 || i < skip+limit {
|
2020-12-17 22:00:47 +08:00
|
|
|
// 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 {
|
2021-12-09 03:08:16 +08:00
|
|
|
return i, nil
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-12-09 03:08:16 +08:00
|
|
|
return 0, err
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
branchName, err := bufReader.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
// This shouldn't happen... but we'll tolerate it for the sake of peace
|
2021-12-09 03:08:16 +08:00
|
|
|
return i, nil
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-12-09 03:08:16 +08:00
|
|
|
return i, err
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
2021-12-09 03:08:16 +08:00
|
|
|
|
2020-12-17 22:00:47 +08:00
|
|
|
if len(branchName) > 0 {
|
|
|
|
branchName = branchName[:len(branchName)-1]
|
|
|
|
}
|
2021-12-09 03:08:16 +08:00
|
|
|
err = walkfn(branchName)
|
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
2021-02-04 03:06:13 +08:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
// count all refs
|
|
|
|
for limit != 0 {
|
|
|
|
_, isPrefix, err := bufReader.ReadLine()
|
|
|
|
if err == io.EOF {
|
2021-12-09 03:08:16 +08:00
|
|
|
return i, nil
|
2021-02-04 03:06:13 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-12-09 03:08:16 +08:00
|
|
|
return 0, err
|
2021-02-04 03:06:13 +08:00
|
|
|
}
|
|
|
|
if !isPrefix {
|
|
|
|
i++
|
|
|
|
}
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
2021-12-09 03:08:16 +08:00
|
|
|
return i, nil
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|