aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/git/batch_reader.go14
-rw-r--r--modules/git/repo_base_nogogit.go5
-rw-r--r--modules/git/repo_commit_nogogit.go5
-rw-r--r--modules/indexer/code/bleve.go6
-rw-r--r--modules/indexer/code/elastic_search.go5
5 files changed, 34 insertions, 1 deletions
diff --git a/modules/git/batch_reader.go b/modules/git/batch_reader.go
index 71045adbc9..7f7272c19e 100644
--- a/modules/git/batch_reader.go
+++ b/modules/git/batch_reader.go
@@ -27,6 +27,20 @@ type WriteCloserError interface {
CloseWithError(err error) error
}
+// EnsureValidGitRepository runs git rev-parse in the repository path - thus ensuring that the repository is a valid repository.
+// Run before opening git cat-file.
+// This is needed otherwise the git cat-file will hang for invalid repositories.
+func EnsureValidGitRepository(ctx context.Context, repoPath string) error {
+ stderr := strings.Builder{}
+ err := NewCommandContext(ctx, "rev-parse").
+ SetDescription(fmt.Sprintf("%s rev-parse [repo_path: %s]", GitExecutable, repoPath)).
+ RunInDirFullPipeline(repoPath, nil, &stderr, nil)
+ if err != nil {
+ return ConcatenateError(err, (&stderr).String())
+ }
+ return nil
+}
+
// CatFileBatchCheck opens git cat-file --batch-check in the provided repo and returns a stdin pipe, a stdout reader and cancel function
func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError, *bufio.Reader, func()) {
batchStdinReader, batchStdinWriter := io.Pipe()
diff --git a/modules/git/repo_base_nogogit.go b/modules/git/repo_base_nogogit.go
index 14a6cacb44..e264fd4a14 100644
--- a/modules/git/repo_base_nogogit.go
+++ b/modules/git/repo_base_nogogit.go
@@ -50,6 +50,11 @@ func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error
return nil, errors.New("no such file or directory")
}
+ // 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
+ }
+
repo := &Repository{
Path: repoPath,
tagCache: newObjectCache(),
diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go
index d86e7d3268..c8cd7ec882 100644
--- a/modules/git/repo_commit_nogogit.go
+++ b/modules/git/repo_commit_nogogit.go
@@ -37,7 +37,10 @@ func (repo *Repository) ResolveReference(name string) (string, error) {
func (repo *Repository) GetRefCommitID(name string) (string, error) {
wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
defer cancel()
- _, _ = wr.Write([]byte(name + "\n"))
+ _, err := wr.Write([]byte(name + "\n"))
+ if err != nil {
+ return "", err
+ }
shaBs, _, _, err := ReadBatchLine(rd)
if IsErrNotExist(err) {
return "", ErrNotExist{name, ""}
diff --git a/modules/indexer/code/bleve.go b/modules/indexer/code/bleve.go
index 1affdf73b0..25cb8bf5c9 100644
--- a/modules/indexer/code/bleve.go
+++ b/modules/indexer/code/bleve.go
@@ -275,6 +275,12 @@ func (b *BleveIndexer) Index(repo *repo_model.Repository, sha string, changes *r
batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize)
if len(changes.Updates) > 0 {
+ // 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 := git.EnsureValidGitRepository(git.DefaultContext, repo.RepoPath()); err != nil {
+ log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err)
+ return err
+ }
+
batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath())
defer cancel()
diff --git a/modules/indexer/code/elastic_search.go b/modules/indexer/code/elastic_search.go
index bd5faf3b04..169dffd78b 100644
--- a/modules/indexer/code/elastic_search.go
+++ b/modules/indexer/code/elastic_search.go
@@ -247,6 +247,11 @@ func (b *ElasticSearchIndexer) addDelete(filename string, repo *repo_model.Repos
func (b *ElasticSearchIndexer) Index(repo *repo_model.Repository, sha string, changes *repoChanges) error {
reqs := make([]elastic.BulkableRequest, 0)
if len(changes.Updates) > 0 {
+ // 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 := git.EnsureValidGitRepository(git.DefaultContext, repo.RepoPath()); err != nil {
+ log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err)
+ return err
+ }
batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath())
defer cancel()