mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-01 14:44:35 -04:00
Merge pull request 'feat: support regexp in git-grep search' (#4968) from yoctozepto/git-grep-regexp into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4968 Reviewed-by: Shiny Nematoda <snematoda@noreply.codeberg.org>
This commit is contained in:
commit
f7f7800460
14 changed files with 182 additions and 56 deletions
|
@ -27,12 +27,20 @@ type GrepResult struct {
|
|||
HighlightedRanges [][3]int
|
||||
}
|
||||
|
||||
type grepMode int
|
||||
|
||||
const (
|
||||
FixedGrepMode grepMode = iota
|
||||
FixedAnyGrepMode
|
||||
RegExpGrepMode
|
||||
)
|
||||
|
||||
type GrepOptions struct {
|
||||
RefName string
|
||||
MaxResultLimit int
|
||||
MatchesPerFile int
|
||||
ContextLineNumber int
|
||||
IsFuzzy bool
|
||||
Mode grepMode
|
||||
PathSpec []setting.Glob
|
||||
}
|
||||
|
||||
|
@ -74,12 +82,20 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
|||
var results []*GrepResult
|
||||
// -I skips binary files
|
||||
cmd := NewCommand(ctx, "grep",
|
||||
"-I", "--null", "--break", "--heading", "--column",
|
||||
"--fixed-strings", "--line-number", "--ignore-case", "--full-name")
|
||||
"-I", "--null", "--break", "--heading",
|
||||
"--line-number", "--ignore-case", "--full-name")
|
||||
if opts.Mode == RegExpGrepMode {
|
||||
// No `--column` -- regexp mode does not support highlighting in the
|
||||
// current implementation as the length of the match is unknown from
|
||||
// `grep` but required for highlighting.
|
||||
cmd.AddArguments("--perl-regexp")
|
||||
} else {
|
||||
cmd.AddArguments("--fixed-strings", "--column")
|
||||
}
|
||||
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
|
||||
cmd.AddOptionValues("--max-count", fmt.Sprint(opts.MatchesPerFile))
|
||||
words := []string{search}
|
||||
if opts.IsFuzzy {
|
||||
if opts.Mode == FixedAnyGrepMode {
|
||||
words = strings.Fields(search)
|
||||
}
|
||||
for _, word := range words {
|
||||
|
@ -148,6 +164,7 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
|
|||
if lineNum, lineCode, ok := strings.Cut(line, "\x00"); ok {
|
||||
lineNumInt, _ := strconv.Atoi(lineNum)
|
||||
res.LineNumbers = append(res.LineNumbers, lineNumInt)
|
||||
// We support highlighting only when `--column` parameter is used.
|
||||
if lineCol, lineCode2, ok := strings.Cut(lineCode, "\x00"); ok {
|
||||
lineColInt, _ := strconv.Atoi(lineCol)
|
||||
start := lineColInt - 1
|
||||
|
|
|
@ -201,3 +201,34 @@ func TestGrepRefs(t *testing.T) {
|
|||
assert.Len(t, res, 1)
|
||||
assert.Equal(t, "A", res[0].LineCodes[0])
|
||||
}
|
||||
|
||||
func TestGrepCanHazRegexOnDemand(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, "matching"), []byte("It's a match!"), 0o666))
|
||||
require.NoError(t, os.WriteFile(path.Join(tmpDir, "not-matching"), []byte("Orisitamatch?"), 0o666))
|
||||
|
||||
err = AddChanges(tmpDir, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = CommitChanges(tmpDir, CommitChangesOptions{Message: "Add fixtures for regexp test"})
|
||||
require.NoError(t, err)
|
||||
|
||||
// should find nothing by default...
|
||||
res, err := GrepSearch(context.Background(), gitRepo, "\\bmatch\\b", GrepOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, res)
|
||||
|
||||
// ... unless configured explicitly
|
||||
res, err = GrepSearch(context.Background(), gitRepo, "\\bmatch\\b", GrepOptions{Mode: RegExpGrepMode})
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, res, 1)
|
||||
assert.Equal(t, "matching", res[0].Filename)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue