Skip to content

Commit 84297fd

Browse files
committed
restart from scratch
1 parent 95b18eb commit 84297fd

17 files changed

+249
-248
lines changed

modules/git/batch.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

modules/git/blob_nogogit.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package git
77

88
import (
9-
"bufio"
10-
"bytes"
119
"io"
1210

1311
"code.gitea.io/gitea/modules/log"
@@ -25,35 +23,27 @@ type Blob struct {
2523

2624
// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
2725
// Calling the Close function on the result will discard all unread output.
28-
func (b *Blob) DataAsync() (io.ReadCloser, error) {
29-
wr, rd, cancel, err := b.repo.CatFileBatch(b.repo.Ctx)
26+
func (b *Blob) DataAsync() (_ io.ReadCloser, retErr error) {
27+
batch, cancel, err := b.repo.CatFileBatch(b.repo.Ctx)
3028
if err != nil {
3129
return nil, err
3230
}
31+
defer func() {
32+
if retErr != nil {
33+
cancel()
34+
}
35+
}()
3336

34-
_, err = wr.Write([]byte(b.ID.String() + "\n"))
37+
rd, err := batch.QueryContent(b.ID.String())
3538
if err != nil {
36-
cancel()
3739
return nil, err
3840
}
3941
_, _, size, err := ReadBatchLine(rd)
4042
if err != nil {
41-
cancel()
4243
return nil, err
4344
}
4445
b.gotSize = true
4546
b.size = size
46-
47-
if size < 4096 {
48-
bs, err := io.ReadAll(io.LimitReader(rd, size))
49-
defer cancel()
50-
if err != nil {
51-
return nil, err
52-
}
53-
_, err = rd.Discard(1)
54-
return io.NopCloser(bytes.NewReader(bs)), err
55-
}
56-
5747
return &blobReader{
5848
rd: rd,
5949
n: size,
@@ -67,13 +57,13 @@ func (b *Blob) Size() int64 {
6757
return b.size
6858
}
6959

70-
wr, rd, cancel, err := b.repo.CatFileBatchCheck(b.repo.Ctx)
60+
batch, cancel, err := b.repo.CatFileBatch(b.repo.Ctx)
7161
if err != nil {
7262
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
7363
return 0
7464
}
7565
defer cancel()
76-
_, err = wr.Write([]byte(b.ID.String() + "\n"))
66+
rd, err := batch.QueryInfo(b.ID.String())
7767
if err != nil {
7868
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
7969
return 0
@@ -85,12 +75,11 @@ func (b *Blob) Size() int64 {
8575
}
8676

8777
b.gotSize = true
88-
8978
return b.size
9079
}
9180

9281
type blobReader struct {
93-
rd *bufio.Reader
82+
rd BufferedReader
9483
n int64
9584
cancel func()
9685
}

modules/git/catfile_batch.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"context"
8+
"io"
9+
)
10+
11+
type BufferedReader interface {
12+
io.Reader
13+
Buffered() int
14+
Peek(n int) ([]byte, error)
15+
Discard(n int) (int, error)
16+
ReadString(sep byte) (string, error)
17+
ReadSlice(sep byte) ([]byte, error)
18+
ReadBytes(sep byte) ([]byte, error)
19+
}
20+
21+
type CatFileBatchContent interface {
22+
QueryContent(obj string) (BufferedReader, error)
23+
}
24+
25+
type CatFileBatchInfo interface {
26+
QueryInfo(obj string) (BufferedReader, error)
27+
}
28+
29+
type CatFileBatch interface {
30+
CatFileBatchInfo
31+
CatFileBatchContent
32+
}
33+
34+
type CatFileBatchCloser interface {
35+
CatFileBatch
36+
Close()
37+
}
38+
39+
func NewBatch(ctx context.Context, repoPath string) (CatFileBatchCloser, error) {
40+
return newCatFileBatchLegacy(ctx, repoPath)
41+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
import (
7+
"bufio"
8+
"context"
9+
"io"
10+
11+
"code.gitea.io/gitea/modules/git/gitcmd"
12+
)
13+
14+
// catFileBatchLegacy implements the CatFileBatch interface using the "cat-file --batch" command and "cat-file --batch-check" command
15+
// for git version < 2.36
16+
// to align with "--batch-command", it creates the two commands for querying object contents and object info separately
17+
// ref: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt---batch
18+
type catFileBatchLegacy struct {
19+
ctx context.Context
20+
repoPath string
21+
batchContent *catFileBatchCommunicator
22+
batchCheck *catFileBatchCommunicator
23+
}
24+
25+
var _ CatFileBatchCloser = (*catFileBatchLegacy)(nil)
26+
27+
// newCatFileBatchLegacy creates a new batch and a new batch check for the given repository, the Close must be invoked before release the batch
28+
func newCatFileBatchLegacy(ctx context.Context, repoPath string) (*catFileBatchLegacy, error) {
29+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
30+
return nil, err
31+
}
32+
33+
return &catFileBatchLegacy{
34+
ctx: ctx,
35+
repoPath: repoPath,
36+
}, nil
37+
}
38+
39+
func (b *catFileBatchLegacy) getBatchContent() *catFileBatchCommunicator {
40+
if b.batchContent != nil {
41+
return b.batchContent
42+
}
43+
b.batchContent = newCatFileBatch(b.ctx, b.repoPath, gitcmd.NewCommand("cat-file", "--batch"))
44+
return b.batchContent
45+
}
46+
47+
func (b *catFileBatchLegacy) getBatchCheck() *catFileBatchCommunicator {
48+
if b.batchCheck != nil {
49+
return b.batchCheck
50+
}
51+
b.batchCheck = newCatFileBatch(b.ctx, b.repoPath, gitcmd.NewCommand("cat-file", "--batch-check"))
52+
return b.batchCheck
53+
}
54+
55+
func (b *catFileBatchLegacy) QueryContent(obj string) (BufferedReader, error) {
56+
_, err := io.WriteString(b.getBatchContent().writer, obj+"\n")
57+
return b.getBatchContent().reader, err
58+
}
59+
60+
func (b *catFileBatchLegacy) ContentReader() *bufio.Reader {
61+
return b.getBatchContent().reader
62+
}
63+
64+
func (b *catFileBatchLegacy) QueryInfo(obj string) (BufferedReader, error) {
65+
_, err := io.WriteString(b.getBatchCheck().writer, obj+"\n")
66+
return b.getBatchCheck().reader, err
67+
}
68+
69+
func (b *catFileBatchLegacy) InfoReader() *bufio.Reader {
70+
return b.getBatchCheck().reader
71+
}
72+
73+
func (b *catFileBatchLegacy) Close() {
74+
if b.batchContent != nil {
75+
b.batchContent.Close()
76+
b.batchContent = nil
77+
}
78+
if b.batchCheck != nil {
79+
b.batchCheck.Close()
80+
b.batchCheck = nil
81+
}
82+
}

0 commit comments

Comments
 (0)