Refactor git command stdio pipe (#36422)

Most potential deadlock problems should have been fixed, and new code is
unlikely to cause new problems with the new design.

Also raise the minimum Git version required to 2.6.0 (released in 2015)
This commit is contained in:
wxiaoguang
2026-01-22 14:04:26 +08:00
committed by GitHub
parent 2a56c4ec3b
commit 3a09d7aa8d
63 changed files with 767 additions and 1016 deletions

View File

@@ -5,7 +5,6 @@
package git
import (
"io"
"strings"
"code.gitea.io/gitea/modules/git/gitcmd"
@@ -32,18 +31,12 @@ func (o ObjectType) Bytes() []byte {
return []byte(o)
}
type EmptyReader struct{}
func (EmptyReader) Read(p []byte) (int, error) {
return 0, io.EOF
}
func (repo *Repository) GetObjectFormat() (ObjectFormat, error) {
if repo != nil && repo.objectFormat != nil {
return repo.objectFormat, nil
}
str, err := repo.hashObject(EmptyReader{}, false)
str, err := repo.hashObjectBytes(nil, false)
if err != nil {
return nil, err
}
@@ -57,16 +50,16 @@ func (repo *Repository) GetObjectFormat() (ObjectFormat, error) {
return repo.objectFormat, nil
}
// HashObject takes a reader and returns hash for that reader
func (repo *Repository) HashObject(reader io.Reader) (ObjectID, error) {
idStr, err := repo.hashObject(reader, true)
// HashObjectBytes returns hash for the content
func (repo *Repository) HashObjectBytes(buf []byte) (ObjectID, error) {
idStr, err := repo.hashObjectBytes(buf, true)
if err != nil {
return nil, err
}
return NewIDFromString(idStr)
}
func (repo *Repository) hashObject(reader io.Reader, save bool) (string, error) {
func (repo *Repository) hashObjectBytes(buf []byte, save bool) (string, error) {
var cmd *gitcmd.Command
if save {
cmd = gitcmd.NewCommand("hash-object", "-w", "--stdin")
@@ -75,7 +68,7 @@ func (repo *Repository) hashObject(reader io.Reader, save bool) (string, error)
}
stdout, _, err := cmd.
WithDir(repo.Path).
WithStdin(reader).
WithStdinBytes(buf).
RunStdString(repo.Ctx)
if err != nil {
return "", err