summaryrefslogtreecommitdiffstats
path: root/modules/git
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-03-04 02:57:01 +0000
committerGitHub <noreply@github.com>2021-03-03 21:57:01 -0500
commit523efa433b61e00e7a14bd31cac315e43842d729 (patch)
tree684139f1eb909e169031fb11eb292fae6307bfce /modules/git
parent0044e804912099a6478b747230cdc37b2c8a3047 (diff)
downloadgitea-523efa433b61e00e7a14bd31cac315e43842d729.tar.gz
gitea-523efa433b61e00e7a14bd31cac315e43842d729.zip
Move Bleve and Elastic code indexers to use a common cat-file --batch (#14781)
* Extract out the common cat-file batch calls Signed-off-by: Andrew Thornton <art27@cantab.net> * Move bleve and elastic indexers to use a common cat-file --batch when indexing Signed-off-by: Andrew Thornton <art27@cantab.net> * move catfilebatch to batch_reader and rename to batch_reader.go Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules/git')
-rw-r--r--modules/git/batch_reader.go (renamed from modules/git/batch_reader_nogogit.go)35
-rw-r--r--modules/git/commit_info_nogogit.go25
-rw-r--r--modules/git/pipeline/lfs_nogogit.go23
-rw-r--r--modules/git/repo_language_stats_nogogit.go27
4 files changed, 39 insertions, 71 deletions
diff --git a/modules/git/batch_reader_nogogit.go b/modules/git/batch_reader.go
index 6a236e5002..6014508b93 100644
--- a/modules/git/batch_reader_nogogit.go
+++ b/modules/git/batch_reader.go
@@ -2,17 +2,48 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
-// +build !gogit
-
package git
import (
"bufio"
"bytes"
+ "io"
"math"
"strconv"
+ "strings"
)
+// CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
+func CatFileBatch(repoPath string) (*io.PipeWriter, *bufio.Reader, func()) {
+ // Next feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
+ // so let's create a batch stdin and stdout
+ batchStdinReader, batchStdinWriter := io.Pipe()
+ batchStdoutReader, batchStdoutWriter := io.Pipe()
+ cancel := func() {
+ _ = batchStdinReader.Close()
+ _ = batchStdinWriter.Close()
+ _ = batchStdoutReader.Close()
+ _ = batchStdoutWriter.Close()
+ }
+
+ go func() {
+ stderr := strings.Builder{}
+ err := NewCommand("cat-file", "--batch").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
+ if err != nil {
+ _ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
+ _ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
+ } else {
+ _ = batchStdoutWriter.Close()
+ _ = batchStdinReader.Close()
+ }
+ }()
+
+ // For simplicities sake we'll us a buffered reader to read from the cat-file --batch
+ batchReader := bufio.NewReader(batchStdoutReader)
+
+ return batchStdinWriter, batchReader, cancel
+}
+
// ReadBatchLine reads the header line from cat-file --batch
// We expect:
// <sha> SP <type> SP <size> LF
diff --git a/modules/git/commit_info_nogogit.go b/modules/git/commit_info_nogogit.go
index ac0c7cff5d..b844468c8c 100644
--- a/modules/git/commit_info_nogogit.go
+++ b/modules/git/commit_info_nogogit.go
@@ -141,29 +141,8 @@ func GetLastCommitForPaths(commit *Commit, treePath string, paths []string) ([]*
}
}()
- // We feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
- // so let's create a batch stdin and stdout
- batchStdinReader, batchStdinWriter := io.Pipe()
- batchStdoutReader, batchStdoutWriter := io.Pipe()
- defer func() {
- _ = batchStdinReader.Close()
- _ = batchStdinWriter.Close()
- _ = batchStdoutReader.Close()
- _ = batchStdoutWriter.Close()
- }()
-
- go func() {
- stderr := strings.Builder{}
- err := NewCommand("cat-file", "--batch").RunInDirFullPipeline(commit.repo.Path, batchStdoutWriter, &stderr, batchStdinReader)
- if err != nil {
- _ = revListWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
- } else {
- _ = revListWriter.Close()
- }
- }()
-
- // For simplicities sake we'll us a buffered reader
- batchReader := bufio.NewReader(batchStdoutReader)
+ batchStdinWriter, batchReader, cancel := CatFileBatch(commit.repo.Path)
+ defer cancel()
mapsize := 4096
if len(paths) > mapsize {
diff --git a/modules/git/pipeline/lfs_nogogit.go b/modules/git/pipeline/lfs_nogogit.go
index 30d33e27e0..f6faa3a48a 100644
--- a/modules/git/pipeline/lfs_nogogit.go
+++ b/modules/git/pipeline/lfs_nogogit.go
@@ -64,27 +64,8 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
// Next feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
// so let's create a batch stdin and stdout
- batchStdinReader, batchStdinWriter := io.Pipe()
- batchStdoutReader, batchStdoutWriter := io.Pipe()
- defer func() {
- _ = batchStdinReader.Close()
- _ = batchStdinWriter.Close()
- _ = batchStdoutReader.Close()
- _ = batchStdoutWriter.Close()
- }()
-
- go func() {
- stderr := strings.Builder{}
- err := git.NewCommand("cat-file", "--batch").RunInDirFullPipeline(repo.Path, batchStdoutWriter, &stderr, batchStdinReader)
- if err != nil {
- _ = revListWriter.CloseWithError(git.ConcatenateError(err, (&stderr).String()))
- } else {
- _ = revListWriter.Close()
- }
- }()
-
- // For simplicities sake we'll us a buffered reader to read from the cat-file --batch
- batchReader := bufio.NewReader(batchStdoutReader)
+ batchStdinWriter, batchReader, cancel := git.CatFileBatch(repo.Path)
+ defer cancel()
// We'll use a scanner for the revList because it's simpler than a bufio.Reader
scan := bufio.NewScanner(revListReader)
diff --git a/modules/git/repo_language_stats_nogogit.go b/modules/git/repo_language_stats_nogogit.go
index 4c6f07f0fb..a929d7953b 100644
--- a/modules/git/repo_language_stats_nogogit.go
+++ b/modules/git/repo_language_stats_nogogit.go
@@ -11,7 +11,6 @@ import (
"bytes"
"io"
"math"
- "strings"
"code.gitea.io/gitea/modules/analyze"
@@ -22,30 +21,8 @@ import (
func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, error) {
// We will feed the commit IDs in order into cat-file --batch, followed by blobs as necessary.
// so let's create a batch stdin and stdout
-
- batchStdinReader, batchStdinWriter := io.Pipe()
- batchStdoutReader, batchStdoutWriter := io.Pipe()
- defer func() {
- _ = batchStdinReader.Close()
- _ = batchStdinWriter.Close()
- _ = batchStdoutReader.Close()
- _ = batchStdoutWriter.Close()
- }()
-
- go func() {
- stderr := strings.Builder{}
- err := NewCommand("cat-file", "--batch").RunInDirFullPipeline(repo.Path, batchStdoutWriter, &stderr, batchStdinReader)
- if err != nil {
- _ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
- _ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
- } else {
- _ = batchStdoutWriter.Close()
- _ = batchStdinReader.Close()
- }
- }()
-
- // For simplicities sake we'll us a buffered reader
- batchReader := bufio.NewReader(batchStdoutReader)
+ batchStdinWriter, batchReader, cancel := CatFileBatch(repo.Path)
+ defer cancel()
writeID := func(id string) error {
_, err := batchStdinWriter.Write([]byte(id))