-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathbatch_cat_file.go
126 lines (105 loc) · 3.11 KB
/
batch_cat_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/util"
)
type BatchCatFile struct {
cmd *exec.Cmd
startTime time.Time
stdin io.WriteCloser
stdout io.ReadCloser
cancel context.CancelFunc
finished process.FinishedFunc
}
// NewBatchCatFile opens git cat-file --batch or --batch-check in the provided repo and returns a stdin pipe, a stdout reader and cancel function
// isCheck is true for --batch-check, false for --batch isCheck will only get metadata, --batch will get metadata and content
func NewBatchCatFile(ctx context.Context, repoPath string, isCheck bool) (*BatchCatFile, error) {
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
return nil, err
}
callerInfo := util.CallerFuncName(1 /* util */ + 1 /* this */ + 1 /* parent */)
if pos := strings.LastIndex(callerInfo, "/"); pos >= 0 {
callerInfo = callerInfo[pos+1:]
}
batchArg := util.Iif(isCheck, "--batch-check", "--batch")
a := make([]string, 0, 4)
a = append(a, debugQuote(GitExecutable))
if len(globalCommandArgs) > 0 {
a = append(a, "...global...")
}
a = append(a, "cat-file", batchArg)
cmdLogString := strings.Join(a, " ")
// these logs are for debugging purposes only, so no guarantee of correctness or stability
desc := fmt.Sprintf("git.Run(by:%s, repo:%s): %s", callerInfo, logArgSanitize(repoPath), cmdLogString)
log.Debug("git.BatchCatFile: %s", desc)
ctx, cancel, finished := process.GetManager().AddContext(ctx, desc)
args := make([]string, 0, len(globalCommandArgs)+2)
for _, arg := range globalCommandArgs {
args = append(args, string(arg))
}
args = append(args, "cat-file", batchArg)
cmd := exec.CommandContext(ctx, GitExecutable, args...)
cmd.Env = append(os.Environ(), CommonGitCmdEnvs()...)
cmd.Dir = repoPath
process.SetSysProcAttribute(cmd)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
return &BatchCatFile{
cmd: cmd,
startTime: time.Now(),
stdin: stdin,
stdout: stdout,
cancel: cancel,
finished: finished,
}, nil
}
func (b *BatchCatFile) Input(refs ...string) error {
var buf bytes.Buffer
for _, ref := range refs {
if _, err := buf.WriteString(ref + "\n"); err != nil {
return err
}
}
_, err := b.stdin.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
func (b *BatchCatFile) Reader() *bufio.Reader {
return bufio.NewReader(b.stdout)
}
func (b *BatchCatFile) Escaped() time.Duration {
return time.Since(b.startTime)
}
func (b *BatchCatFile) Cancel() {
b.cancel()
}
func (b *BatchCatFile) Close() error {
b.finished()
_ = b.stdin.Close()
log.Debug("git.BatchCatFile: %v", b.Escaped())
return b.cmd.Wait()
}