diff --git a/modules/git/grep.go b/modules/git/grep.go index 1de739107a..6ca8456cdb 100644 --- a/modules/git/grep.go +++ b/modules/git/grep.go @@ -29,6 +29,7 @@ type GrepOptions struct { MaxResultLimit int ContextLineNumber int IsFuzzy bool + PathSpec []setting.Glob } func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepOptions) ([]*GrepResult, error) { @@ -61,15 +62,20 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO } else { cmd.AddOptionValues("-e", strings.TrimLeft(search, "-")) } + // pathspec - files := make([]string, 0, len(setting.Indexer.IncludePatterns)+len(setting.Indexer.ExcludePatterns)) - for _, expr := range setting.Indexer.IncludePatterns { - files = append(files, expr.Pattern()) + files := make([]string, 0, + len(setting.Indexer.IncludePatterns)+ + len(setting.Indexer.ExcludePatterns)+ + len(opts.PathSpec)) + for _, expr := range append(setting.Indexer.IncludePatterns, opts.PathSpec...) { + files = append(files, ":"+expr.Pattern()) } for _, expr := range setting.Indexer.ExcludePatterns { files = append(files, ":^"+expr.Pattern()) } cmd.AddDynamicArguments(cmp.Or(opts.RefName, "HEAD")).AddDashesAndList(files...) + opts.MaxResultLimit = cmp.Or(opts.MaxResultLimit, 50) stderr := bytes.Buffer{} err = cmd.Run(&RunOpts{ diff --git a/modules/git/grep_test.go b/modules/git/grep_test.go index a321b11452..15dc9e9d50 100644 --- a/modules/git/grep_test.go +++ b/modules/git/grep_test.go @@ -76,3 +76,33 @@ func TestGrepLongFiles(t *testing.T) { assert.Len(t, res, 1) assert.Len(t, res[0].LineCodes[0], 65*1024) } + +func TestGrepRefs(t *testing.T) { + tmpDir := t.TempDir() + + err := InitRepository(DefaultContext, tmpDir, false, Sha1ObjectFormat.Name()) + assert.NoError(t, err) + + gitRepo, err := openRepositoryWithDefaultContext(tmpDir) + assert.NoError(t, err) + defer gitRepo.Close() + + assert.NoError(t, os.WriteFile(path.Join(tmpDir, "README.md"), []byte{'A'}, 0o666)) + assert.NoError(t, AddChanges(tmpDir, true)) + + err = CommitChanges(tmpDir, CommitChangesOptions{Message: "add A"}) + assert.NoError(t, err) + + assert.NoError(t, gitRepo.CreateTag("v1", "HEAD")) + + assert.NoError(t, os.WriteFile(path.Join(tmpDir, "README.md"), []byte{'A', 'B', 'C', 'D'}, 0o666)) + assert.NoError(t, AddChanges(tmpDir, true)) + + err = CommitChanges(tmpDir, CommitChangesOptions{Message: "add BCD"}) + assert.NoError(t, err) + + res, err := GrepSearch(context.Background(), gitRepo, "a", GrepOptions{RefName: "v1"}) + assert.NoError(t, err) + assert.Len(t, res, 1) + assert.Equal(t, res[0].LineCodes[0], "A") +} diff --git a/release-notes/8.0.0/feat/3654.md b/release-notes/8.0.0/feat/3654.md new file mode 100644 index 0000000000..7545599e51 --- /dev/null +++ b/release-notes/8.0.0/feat/3654.md @@ -0,0 +1 @@ +Code Search for non-default branches and tags when repository indexer is disabled diff --git a/routers/web/repo/search.go b/routers/web/repo/search.go index 02df17a7e6..be03c7bded 100644 --- a/routers/web/repo/search.go +++ b/routers/web/repo/search.go @@ -64,7 +64,11 @@ func Search(ctx *context.Context) { ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx) } } else { - res, err := git.GrepSearch(ctx, ctx.Repo.GitRepo, keyword, git.GrepOptions{ContextLineNumber: 3, IsFuzzy: isFuzzy}) + res, err := git.GrepSearch(ctx, ctx.Repo.GitRepo, keyword, git.GrepOptions{ + ContextLineNumber: 1, + IsFuzzy: isFuzzy, + RefName: ctx.Repo.RefName, + }) if err != nil { ctx.ServerError("GrepSearch", err) return diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 2644b27229..5b0f4940d1 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -1140,6 +1140,7 @@ PostRecentBranchCheck: ctx.Data["TreeLink"] = treeLink ctx.Data["TreeNames"] = treeNames ctx.Data["BranchLink"] = branchLink + ctx.Data["CodeIndexerDisabled"] = !setting.Indexer.RepoIndexerEnabled ctx.HTML(http.StatusOK, tplRepoHome) } diff --git a/routers/web/web.go b/routers/web/web.go index 48f9102749..77857d32be 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1570,11 +1570,17 @@ func registerRoutes(m *web.Route) { m.Group("/{username}/{reponame}", func() { if !setting.Repository.DisableStars { - m.Get("/stars", repo.Stars) + m.Get("/stars", context.RepoRef(), repo.Stars) } - m.Get("/watchers", repo.Watchers) - m.Get("/search", reqRepoCodeReader, repo.Search) - }, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes()) + m.Get("/watchers", context.RepoRef(), repo.Watchers) + m.Group("/search", func() { + m.Get("", context.RepoRef(), repo.Search) + if !setting.Indexer.RepoIndexerEnabled { + m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Search) + m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Search) + } + }, reqRepoCodeReader) + }, ignSignIn, context.RepoAssignment, context.UnitTypes()) m.Group("/{username}", func() { m.Group("/{reponame}", func() { diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 235a7e4503..aa52e91a49 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -8,10 +8,10 @@