Skip to content

Commit cafb63a

Browse files
ethantkoenigvdbt
authored andcommitted
Code/repo search (go-gitea#2582)
Indexed search of repository contents (for default branch only)
1 parent fd437ce commit cafb63a

File tree

33 files changed

+1214
-31
lines changed

33 files changed

+1214
-31
lines changed

conf/app.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ ITERATE_BUFFER_SIZE = 50
192192

193193
[indexer]
194194
ISSUE_INDEXER_PATH = indexers/issues.bleve
195+
; repo indexer by default disabled, since it uses a lot of disk space
196+
REPO_INDEXER_ENABLED = false
197+
REPO_INDEXER_PATH = indexers/repos.bleve
195198
UPDATE_BUFFER_LEN = 20
199+
MAX_FILE_SIZE = 1048576
196200

197201
[admin]
198202
; Disable regular (non-admin) users to create organizations

integrations/integration_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func TestMain(m *testing.M) {
6363
fmt.Printf("os.RemoveAll: %v\n", err)
6464
os.Exit(1)
6565
}
66+
if err = os.RemoveAll(setting.Indexer.RepoPath); err != nil {
67+
fmt.Printf("Unable to remove repo indexer: %v\n", err)
68+
os.Exit(1)
69+
}
6670

6771
os.Exit(exitCode)
6872
}

integrations/mysql.ini.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PATH = data/gitea.db
1212

1313
[indexer]
1414
ISSUE_INDEXER_PATH = integrations/indexers-mysql/issues.bleve
15+
REPO_INDEXER_ENABLED = true
16+
REPO_INDEXER_PATH = integrations/indexers-mysql/repos.bleve
1517

1618
[repository]
1719
ROOT = integrations/gitea-integration-mysql/gitea-repositories

integrations/pgsql.ini.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PATH = data/gitea.db
1212

1313
[indexer]
1414
ISSUE_INDEXER_PATH = integrations/indexers-pgsql/issues.bleve
15+
REPO_INDEXER_ENABLED = true
16+
REPO_INDEXER_PATH = integrations/indexers-pgsql/repos.bleve
1517

1618
[repository]
1719
ROOT = integrations/gitea-integration-pgsql/gitea-repositories

integrations/repo_search_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"github.com/PuerkitoBio/goquery"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func resultFilenames(t testing.TB, doc *HTMLDoc) []string {
16+
resultsSelection := doc.doc.Find(".repository.search")
17+
assert.EqualValues(t, 1, resultsSelection.Length(),
18+
"Invalid template (repo search template has changed?)")
19+
filenameSelections := resultsSelection.Find(".repo-search-result").Find(".header").Find("span.file")
20+
result := make([]string, filenameSelections.Length())
21+
filenameSelections.Each(func(i int, selection *goquery.Selection) {
22+
result[i] = selection.Text()
23+
})
24+
return result
25+
}
26+
27+
func TestSearchRepo(t *testing.T) {
28+
prepareTestEnv(t)
29+
30+
req := NewRequestf(t, "GET", "/user2/repo1/search?q=Description&page=1")
31+
resp := MakeRequest(t, req, http.StatusOK)
32+
33+
filenames := resultFilenames(t, NewHTMLParser(t, resp.Body))
34+
assert.EqualValues(t, []string{"README.md"}, filenames)
35+
}

integrations/sqlite.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PATH = :memory:
77

88
[indexer]
99
ISSUE_INDEXER_PATH = integrations/indexers-sqlite/issues.bleve
10+
REPO_INDEXER_ENABLED = true
11+
REPO_INDEXER_PATH = integrations/indexers-sqlite/repos.bleve
1012

1113
[repository]
1214
ROOT = integrations/gitea-integration-sqlite/gitea-repositories
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[] # empty

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ var migrations = []Migration{
144144
NewMigration("remove organization watch repositories", removeOrganizationWatchRepo),
145145
// v47 -> v48
146146
NewMigration("add deleted branches", addDeletedBranch),
147+
// v48 -> v49
148+
NewMigration("add repo indexer status", addRepoIndexerStatus),
147149
}
148150

149151
// Migrate database to current version

models/migrations/v48.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/go-xorm/xorm"
11+
)
12+
13+
func addRepoIndexerStatus(x *xorm.Engine) error {
14+
// RepoIndexerStatus see models/repo_indexer.go
15+
type RepoIndexerStatus struct {
16+
ID int64 `xorm:"pk autoincr"`
17+
RepoID int64 `xorm:"INDEX NOT NULL"`
18+
CommitSha string `xorm:"VARCHAR(40)"`
19+
}
20+
21+
if err := x.Sync2(new(RepoIndexerStatus)); err != nil {
22+
return fmt.Errorf("Sync2: %v", err)
23+
}
24+
return nil
25+
}

models/models.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
"path"
1414
"strings"
1515

16+
"code.gitea.io/gitea/modules/log"
17+
"code.gitea.io/gitea/modules/setting"
18+
"code.gitea.io/gitea/modules/util"
19+
1620
// Needed for the MySQL driver
1721
_ "github.com/go-sql-driver/mysql"
1822
"github.com/go-xorm/core"
@@ -23,9 +27,6 @@ import (
2327

2428
// Needed for the MSSSQL driver
2529
_ "github.com/denisenkom/go-mssqldb"
26-
27-
"code.gitea.io/gitea/modules/log"
28-
"code.gitea.io/gitea/modules/setting"
2930
)
3031

3132
// Engine represents a xorm engine or session.
@@ -115,6 +116,7 @@ func init() {
115116
new(Stopwatch),
116117
new(TrackedTime),
117118
new(DeletedBranch),
119+
new(RepoIndexerStatus),
118120
)
119121

120122
gonicNames := []string{"SSL", "UID"}
@@ -150,8 +152,13 @@ func LoadConfigs() {
150152
DbCfg.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
151153

152154
sec = setting.Cfg.Section("indexer")
153-
setting.Indexer.IssuePath = sec.Key("ISSUE_INDEXER_PATH").MustString("indexers/issues.bleve")
155+
setting.Indexer.IssuePath = absolutePath(
156+
sec.Key("ISSUE_INDEXER_PATH").MustString("indexers/issues.bleve"))
157+
setting.Indexer.RepoIndexerEnabled = sec.Key("REPO_INDEXER_ENABLED").MustBool(false)
158+
setting.Indexer.RepoPath = absolutePath(
159+
sec.Key("REPO_INDEXER_PATH").MustString("indexers/repos.bleve"))
154160
setting.Indexer.UpdateQueueLength = sec.Key("UPDATE_BUFFER_LEN").MustInt(20)
161+
setting.Indexer.MaxIndexerFileSize = sec.Key("MAX_FILE_SIZE").MustInt64(512 * 1024 * 1024)
155162
}
156163

157164
// parsePostgreSQLHostPort parses given input in various forms defined in
@@ -336,3 +343,12 @@ func DumpDatabase(filePath string, dbType string) error {
336343
}
337344
return x.DumpTablesToFile(tbs, filePath)
338345
}
346+
347+
// absolutePath make path absolute if it is relative
348+
func absolutePath(path string) string {
349+
workDir, err := setting.WorkDir()
350+
if err != nil {
351+
log.Fatal(4, "Failed to get work directory: %v", err)
352+
}
353+
return util.EnsureAbsolutePath(path, workDir)
354+
}

models/repo.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,11 @@ type Repository struct {
205205
ExternalMetas map[string]string `xorm:"-"`
206206
Units []*RepoUnit `xorm:"-"`
207207

208-
IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
209-
ForkID int64 `xorm:"INDEX"`
210-
BaseRepo *Repository `xorm:"-"`
211-
Size int64 `xorm:"NOT NULL DEFAULT 0"`
208+
IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
209+
ForkID int64 `xorm:"INDEX"`
210+
BaseRepo *Repository `xorm:"-"`
211+
Size int64 `xorm:"NOT NULL DEFAULT 0"`
212+
IndexerStatus *RepoIndexerStatus `xorm:"-"`
212213

213214
Created time.Time `xorm:"-"`
214215
CreatedUnix int64 `xorm:"INDEX created"`
@@ -782,8 +783,10 @@ func UpdateLocalCopyBranch(repoPath, localPath, branch string) error {
782783
if err != nil {
783784
return fmt.Errorf("git fetch origin: %v", err)
784785
}
785-
if err := git.ResetHEAD(localPath, true, "origin/"+branch); err != nil {
786-
return fmt.Errorf("git reset --hard origin/%s: %v", branch, err)
786+
if len(branch) > 0 {
787+
if err := git.ResetHEAD(localPath, true, "origin/"+branch); err != nil {
788+
return fmt.Errorf("git reset --hard origin/%s: %v", branch, err)
789+
}
787790
}
788791
}
789792
return nil
@@ -989,6 +992,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
989992
if err = SyncReleasesWithTags(repo, gitRepo); err != nil {
990993
log.Error(4, "Failed to synchronize tags to releases for repository: %v", err)
991994
}
995+
UpdateRepoIndexer(repo)
992996
}
993997

994998
if err = repo.UpdateSize(); err != nil {
@@ -1883,6 +1887,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
18831887
go HookQueue.Add(repo.ID)
18841888
}
18851889

1890+
DeleteRepoFromIndexer(repo)
18861891
return nil
18871892
}
18881893

models/repo_editor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
178178
if err != nil {
179179
return fmt.Errorf("PushUpdate: %v", err)
180180
}
181+
UpdateRepoIndexer(repo)
182+
181183
return nil
182184
}
183185

0 commit comments

Comments
 (0)