From f4a7bf6d2a75a4d5932bdaa6a9a3932bf1687ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Piliszek?= Date: Sun, 11 Aug 2024 14:24:40 +0200 Subject: [PATCH] git-grep: skip binary files It is a waste of resources to scan them looking for matches because they are never returned back - they appear as empty lines in the current format. Notably, even if they were returned, it is unlikely that matching in binary files makes sense when the goal is "code search". --- modules/git/grep.go | 3 ++- modules/git/grep_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/git/grep.go b/modules/git/grep.go index ba870e054..6449b465b 100644 --- a/modules/git/grep.go +++ b/modules/git/grep.go @@ -63,8 +63,9 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO 2^@10^@repo: go-gitea/gitea */ var results []*GrepResult + // -I skips binary files cmd := NewCommand(ctx, "grep", - "--null", "--break", "--heading", "--column", + "-I", "--null", "--break", "--heading", "--column", "--fixed-strings", "--line-number", "--ignore-case", "--full-name") cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber)) if opts.MatchesPerFile > 0 { diff --git a/modules/git/grep_test.go b/modules/git/grep_test.go index bb7db7d58..5d0d343ac 100644 --- a/modules/git/grep_test.go +++ b/modules/git/grep_test.go @@ -98,6 +98,31 @@ func TestGrepSearch(t *testing.T) { assert.Empty(t, res) } +func TestGrepNoBinary(t *testing.T) { + tmpDir := t.TempDir() + + err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name()) + require.NoError(t, err) + + gitRepo, err := openRepositoryWithDefaultContext(tmpDir) + require.NoError(t, err) + defer gitRepo.Close() + + require.NoError(t, os.WriteFile(path.Join(tmpDir, "BINARY"), []byte("I AM BINARY\n\x00\nYOU WON'T SEE ME"), 0o666)) + require.NoError(t, os.WriteFile(path.Join(tmpDir, "TEXT"), []byte("I AM NOT BINARY\nYOU WILL SEE ME"), 0o666)) + + err = AddChanges(tmpDir, true) + require.NoError(t, err) + + err = CommitChanges(tmpDir, CommitChangesOptions{Message: "Binary and text files"}) + require.NoError(t, err) + + res, err := GrepSearch(context.Background(), gitRepo, "BINARY", GrepOptions{}) + require.NoError(t, err) + assert.Len(t, res, 1) + assert.Equal(t, "TEXT", res[0].Filename) +} + func TestGrepLongFiles(t *testing.T) { tmpDir := t.TempDir()