Skip to content

Commit 832f5e4

Browse files
committed
refactor
1 parent 5dde21a commit 832f5e4

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

modules/git/batch_command.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ type catFileBatchCommand struct {
2121

2222
var _ CatFileBatch = (*catFileBatchCommand)(nil)
2323

24-
func newBatchCommandCatFile(ctx context.Context, repoPath string) (*catFileBatchCommand, error) { //nolint:unparam // no error
24+
func newBatchCommandCatFile(ctx context.Context, repoPath string) (*catFileBatchCommand, error) {
25+
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
26+
return nil, err
27+
}
2528
return &catFileBatchCommand{
2629
ctx: ctx,
2730
repoPath: repoPath,

modules/git/batch_legacy.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ var _ CatFileBatch = (*catFileBatchLegacy)(nil)
2525

2626
// newBatchCatFileWithCheck creates a new batch and a new batch check for the given repository, the Close must be invoked before release the batch
2727
func newBatchCatFileWithCheck(ctx context.Context, repoPath string) (*catFileBatchLegacy, error) {
28-
// 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!
2928
if err := ensureValidGitRepository(ctx, repoPath); err != nil {
3029
return nil, err
3130
}

modules/git/batch_reader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ func (b *catFileBatchCommunicator) Close() {
4343
// ensureValidGitRepository runs git rev-parse in the repository path - thus ensuring that the repository is a valid repository.
4444
// Run before opening git cat-file.
4545
// This is needed otherwise the git cat-file will hang for invalid repositories.
46-
// FIXME: the comment is from https://github.com/go-gitea/gitea/pull/17991 but it doesn't seem to be true, there must be some bugs in code, not git's problem
46+
// FIXME: the comment is from https://github.com/go-gitea/gitea/pull/17991 but it doesn't seem to be true.
47+
// The real problem is that Golang's Cmd.Wait hangs because it waits for the pipes to be closed, but we can't close the pipes before Wait returns
4748
func ensureValidGitRepository(ctx context.Context, repoPath string) error {
4849
stderr := strings.Builder{}
4950
err := gitcmd.NewCommand("rev-parse").

modules/git/batch_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/modules/test"
12+
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
1415
)
@@ -22,6 +23,12 @@ func TestCatFileBatch(t *testing.T) {
2223
}
2324

2425
func testCatFileBatch(t *testing.T) {
26+
t.Run("CorruptedGitRepo", func(t *testing.T) {
27+
tmpDir := t.TempDir()
28+
_, err := NewBatch(t.Context(), tmpDir)
29+
require.Error(t, err)
30+
})
31+
2532
batch, err := NewBatch(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
2633
require.NoError(t, err)
2734
defer batch.Close()

modules/git/repo_commit_nogogit.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
5555

5656
// IsCommitExist returns true if given commit exists in current repository.
5757
func (repo *Repository) IsCommitExist(name string) bool {
58-
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {
59-
log.Error("IsCommitExist: %v", err)
60-
return false
61-
}
6258
_, _, err := gitcmd.NewCommand("cat-file", "-e").
6359
AddDynamicArguments(name).
6460
WithDir(repo.Path).

0 commit comments

Comments
 (0)