From f2ab4ff83a29855834c920ef188f875a2d5401ad Mon Sep 17 00:00:00 2001
From: Shiny Nematoda <snematoda.751k2@aleeas.com>
Date: Wed, 30 Oct 2024 14:45:18 +0000
Subject: [PATCH] fix(grep): fix git-grep for code search when git version is
 below 2.38

---
 modules/git/grep.go | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/modules/git/grep.go b/modules/git/grep.go
index 8a896bc5aa..1daa3e8fb9 100644
--- a/modules/git/grep.go
+++ b/modules/git/grep.go
@@ -17,6 +17,7 @@ import (
 	"strings"
 	"time"
 
+	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/setting"
 )
 
@@ -38,7 +39,7 @@ const (
 type GrepOptions struct {
 	RefName           string
 	MaxResultLimit    int
-	MatchesPerFile    int
+	MatchesPerFile    int // >= git 2.38
 	ContextLineNumber int
 	Mode              grepMode
 	PathSpec          []setting.Glob
@@ -92,8 +93,16 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
 	} else {
 		cmd.AddArguments("--fixed-strings", "--column")
 	}
+
 	cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
-	cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile))
+
+	// --max-count requires at least git 2.38
+	if CheckGitVersionAtLeast("2.38.0") == nil {
+		cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile))
+	} else {
+		log.Warn("git-grep: --max-count requires at least git 2.38")
+	}
+
 	words := []string{search}
 	if opts.Mode == FixedAnyGrepMode {
 		words = strings.Fields(search)