Refactor git command stdio pipe (#36393)

And remove the incorrect `ensureValidGitRepository`
This commit is contained in:
wxiaoguang
2026-01-18 02:11:46 +08:00
committed by GitHub
parent 7a2aac406d
commit 149f7a6f1f
9 changed files with 210 additions and 122 deletions

View File

@@ -6,6 +6,7 @@ package git
import (
"io"
"path/filepath"
"sync"
"testing"
"code.gitea.io/gitea/modules/test"
@@ -25,7 +26,14 @@ func TestCatFileBatch(t *testing.T) {
func testCatFileBatch(t *testing.T) {
t.Run("CorruptedGitRepo", func(t *testing.T) {
tmpDir := t.TempDir()
_, err := NewBatch(t.Context(), tmpDir)
batch, err := NewBatch(t.Context(), tmpDir)
// as long as the directory exists, no error, because we can't really know whether the git repo is valid until we run commands
require.NoError(t, err)
defer batch.Close()
_, err = batch.QueryInfo("e2129701f1a4d54dc44f03c93bca0a2aec7c5449")
require.Error(t, err)
_, err = batch.QueryInfo("e2129701f1a4d54dc44f03c93bca0a2aec7c5449")
require.Error(t, err)
})
@@ -52,4 +60,30 @@ func testCatFileBatch(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "file1\n", string(content))
})
t.Run("QueryTerminated", func(t *testing.T) {
var c *catFileBatchCommunicator
switch b := batch.(type) {
case *catFileBatchLegacy:
c = b.batchCheck
_, _ = c.writer.Write([]byte("in-complete-line-"))
case *catFileBatchCommand:
c = b.batch
_, _ = c.writer.Write([]byte("info"))
default:
t.FailNow()
return
}
wg := sync.WaitGroup{}
wg.Go(func() {
buf := make([]byte, 100)
_, _ = c.reader.Read(buf)
n, errRead := c.reader.Read(buf)
assert.Zero(t, n)
assert.ErrorIs(t, errRead, io.EOF) // the pipe is closed due to command being killed
})
c.debugGitCmd.DebugKill()
wg.Wait()
})
}