Merge pull request 'git-grep: skip binary files' (#4927) from yoctozepto/forgejo:git-grep-skip-binary into forgejo
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/valkey/valkey:7.2.5-alpine3.19 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:redis:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4927
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Shiny Nematoda <snematoda@noreply.codeberg.org>
This commit is contained in:
Gusted 2024-08-11 18:58:48 +00:00
commit d2184dd931
2 changed files with 27 additions and 1 deletions

View file

@ -63,8 +63,9 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
2^@10^@repo: go-gitea/gitea 2^@10^@repo: go-gitea/gitea
*/ */
var results []*GrepResult var results []*GrepResult
// -I skips binary files
cmd := NewCommand(ctx, "grep", cmd := NewCommand(ctx, "grep",
"--null", "--break", "--heading", "--column", "-I", "--null", "--break", "--heading", "--column",
"--fixed-strings", "--line-number", "--ignore-case", "--full-name") "--fixed-strings", "--line-number", "--ignore-case", "--full-name")
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber)) cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
if opts.MatchesPerFile > 0 { if opts.MatchesPerFile > 0 {

View file

@ -98,6 +98,31 @@ func TestGrepSearch(t *testing.T) {
assert.Empty(t, res) 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) { func TestGrepLongFiles(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()