Skip to content

Commit 154c3dc

Browse files
committed
*: remove unused code after add new flags to load repositories
Signed-off-by: Manuel Carmona <[email protected]>
1 parent 2ea4527 commit 154c3dc

File tree

7 files changed

+65
-310
lines changed

7 files changed

+65
-310
lines changed

cmd/gitbase/command/server.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (c *Server) addDirectories() error {
228228
}
229229

230230
func (c *Server) addDirectory(directory string) error {
231-
_, matches, err := gitbase.PatternMatches(directory)
231+
matches, err := gitbase.PatternMatches(directory)
232232
if err != nil {
233233
return err
234234
}
@@ -270,7 +270,7 @@ func (c *Server) addMatch(match string) error {
270270

271271
if ok {
272272
if !c.DisableGit {
273-
if _, err := c.pool.AddGitWithID(info.Name(), path); err != nil {
273+
if err := c.pool.AddGitWithID(info.Name(), path); err != nil {
274274
logrus.WithFields(logrus.Fields{
275275
"id": info.Name(),
276276
"path": path,
@@ -294,7 +294,15 @@ func (c *Server) addMatch(match string) error {
294294

295295
if !c.DisableSiva &&
296296
info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".siva") {
297-
c.pool.AddSivaFile(path, path)
297+
if err := c.pool.AddSivaFile(path); err != nil {
298+
logrus.WithFields(logrus.Fields{
299+
"path": path,
300+
"error": err,
301+
}).Error("repository could not be addded")
302+
303+
return nil
304+
}
305+
298306
logrus.WithField("path", path).Debug("repository added")
299307
}
300308

common_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ func buildSession(t *testing.T, repos fixtures.Fixtures,
3838
pool := NewRepositoryPool()
3939
for _, fixture := range repos {
4040
path := fixture.Worktree().Root()
41-
_, err := pool.AddGit(path)
42-
if err == nil {
41+
if err := pool.AddGit(path); err == nil {
4342
_, err := pool.GetRepo(path)
4443
require.NoError(err)
4544
paths = append(paths, path)

integration_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func TestIntegration(t *testing.T) {
3333
path := fixtures.ByTag("worktree").One().Worktree().Root()
3434

3535
pool := gitbase.NewRepositoryPool()
36-
_, err := pool.AddGitWithID("worktree", path)
37-
require.NoError(t, err)
36+
require.NoError(t, pool.AddGitWithID("worktree", path))
3837

3938
testCases := []struct {
4039
query string
@@ -357,8 +356,8 @@ func TestSquashCorrectness(t *testing.T) {
357356
`SELECT repository_id, num_files FROM (
358357
SELECT COUNT(f.*) num_files, f.repository_id
359358
FROM ref_commits r
360-
INNER JOIN commit_files cf
361-
ON r.commit_hash = cf.commit_hash
359+
INNER JOIN commit_files cf
360+
ON r.commit_hash = cf.commit_hash
362361
AND r.repository_id = cf.repository_id
363362
INNER JOIN files f
364363
ON cf.repository_id = f.repository_id
@@ -414,7 +413,20 @@ func TestMissingHeadRefs(t *testing.T) {
414413
)
415414

416415
pool := gitbase.NewRepositoryPool()
417-
require.NoError(pool.AddSivaDir(path))
416+
require.NoError(
417+
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
418+
if err != nil {
419+
return err
420+
}
421+
422+
err = pool.AddSivaFile(path)
423+
if err != nil {
424+
require.EqualError(err, "the repository is not: siva")
425+
}
426+
427+
return nil
428+
}),
429+
)
418430

419431
engine := newBaseEngine()
420432

path_utils.go

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
package gitbase
22

33
import (
4-
"os"
54
"path/filepath"
65
"regexp"
7-
"strings"
86
)
97

108
// RegMatchChars matches a string with a glob expression inside.
119
var RegMatchChars = regexp.MustCompile(`(^|[^\\])([*[?])`)
1210

13-
// PatternMatches returns the depth of the fixed part of a patters, the paths
14-
// matched and any error found.
15-
func PatternMatches(pattern string) (int, []string, error) {
11+
// PatternMatches returns the paths matched and any error found.
12+
func PatternMatches(pattern string) ([]string, error) {
1613
abs, err := filepath.Abs(pattern)
1714
if err != nil {
18-
return 0, nil, err
15+
return nil, err
1916
}
2017

2118
matches, err := filepath.Glob(abs)
2219
if err != nil {
23-
return 0, nil, err
20+
return nil, err
2421
}
2522

26-
depth := PatternPrefixDepth(abs)
27-
28-
return depth, removeDsStore(matches), nil
23+
return removeDsStore(matches), nil
2924
}
3025

3126
func removeDsStore(matches []string) []string {
@@ -37,47 +32,3 @@ func removeDsStore(matches []string) []string {
3732
}
3833
return result
3934
}
40-
41-
// PatternPrefixDepth returns the number of directories before the first
42-
// glob expression is found.
43-
func PatternPrefixDepth(pattern string) int {
44-
if pattern == "" {
45-
return 0
46-
}
47-
48-
parts := SplitPath(pattern)
49-
50-
for i, part := range parts {
51-
if RegMatchChars.MatchString(part) {
52-
return i
53-
}
54-
}
55-
56-
return len(parts)
57-
}
58-
59-
// IDFromPath returns a repository ID from a path stripping a number of
60-
// directories from it.
61-
func IDFromPath(prefix int, path string) string {
62-
parts := SplitPath(path)
63-
64-
if prefix >= len(parts) {
65-
return path
66-
}
67-
68-
return filepath.Join(parts[prefix:]...)
69-
}
70-
71-
// SplitPath slices a path in its components.
72-
func SplitPath(path string) []string {
73-
parts := strings.Split(path, string(os.PathSeparator))
74-
saneParts := make([]string, 0, len(parts))
75-
76-
for _, p := range parts {
77-
if p != "" {
78-
saneParts = append(saneParts, p)
79-
}
80-
}
81-
82-
return saneParts
83-
}

path_utils_test.go

Lines changed: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -12,124 +12,31 @@ func TestPatternMatches(t *testing.T) {
1212
wd, err := os.Getwd()
1313
require.NoError(t, err)
1414

15-
wdParts := SplitPath(wd)
16-
wdLen := len(wdParts)
17-
1815
testCases := []struct {
1916
path string
20-
prefix int
2117
expected []string
2218
}{
23-
{"cmd", wdLen + 1, []string{
19+
{"cmd", []string{
2420
filepath.Join(wd, "cmd"),
2521
}},
26-
{"cmd/*", wdLen + 1, []string{
22+
{"cmd/*", []string{
2723
filepath.Join(wd, "cmd/gitbase"),
2824
}},
29-
{"cmd/gitbase/*", wdLen + 2, []string{
25+
{"cmd/gitbase/*", []string{
3026
filepath.Join(wd, "cmd/gitbase/command"),
3127
filepath.Join(wd, "cmd/gitbase/main.go"),
3228
}},
33-
{"cmd/../cmd/gitbase/*", wdLen + 2, []string{
29+
{"cmd/../cmd/gitbase/*", []string{
3430
filepath.Join(wd, "cmd/gitbase/command"),
3531
filepath.Join(wd, "cmd/gitbase/main.go"),
3632
}},
3733
}
3834

3935
for _, test := range testCases {
4036
t.Run(test.path, func(t *testing.T) {
41-
prefix, files, err := PatternMatches(test.path)
37+
files, err := PatternMatches(test.path)
4238
require.NoError(t, err)
43-
require.Equal(t, test.prefix, prefix)
4439
require.Exactly(t, test.expected, files)
4540
})
4641
}
4742
}
48-
49-
func TestPatternPrefixDepth(t *testing.T) {
50-
testCases := []struct {
51-
path string
52-
expected int
53-
}{
54-
{"", 0},
55-
{"root", 1},
56-
57-
{"/root", 1},
58-
{"/root/*", 1},
59-
{"/root/*/tmp", 1},
60-
{"/root/*/tmp/borges", 1},
61-
{"/var/lib/gitbase", 3},
62-
{"/var/lib/gitbase/*", 3},
63-
{"/var/lib/gitbase/*/repos/a", 3},
64-
{"/var/lib/gitbase/[aeiou]/repos/a", 3},
65-
{"/var/lib/gitbase/??/repos/a", 3},
66-
{"/var/lib/gitbase/??/repos/a", 3},
67-
68-
// escaped globs
69-
{"/var/lib/gitbase/\\*/repos/a", 6},
70-
{"/var/lib/gitbase/\\[/repos/a", 6},
71-
{"/var/lib/gitbase/\\?/repos/a", 6},
72-
73-
// relative
74-
{"var/lib/gitbase/*/repos/a", 3},
75-
{"var/lib/gitbase/[aeiou]/repos/a", 3},
76-
{"var/lib/gitbase/??/repos/a", 3},
77-
{"var/lib/gitbase/??/repos/a", 3},
78-
}
79-
80-
for _, test := range testCases {
81-
t.Run(test.path, func(t *testing.T) {
82-
num := PatternPrefixDepth(test.path)
83-
require.Equal(t, test.expected, num)
84-
})
85-
}
86-
}
87-
88-
func TestIDFromPath(t *testing.T) {
89-
testCases := []struct {
90-
prefix int
91-
path string
92-
expected string
93-
}{
94-
{0, "/path", "path"},
95-
{0, "/path/", "path"},
96-
{0, "/path/one.git", "path/one.git"},
97-
{1, "/path/one.git", "one.git"},
98-
{1, "/path/00/one.git", "00/one.git"},
99-
{2, "/path/00/one.git", "one.git"},
100-
{2, "/path/00/two.git", "two.git"},
101-
{2, "/path/00/three.git", "three.git"},
102-
{2, "path/00/three.git", "three.git"},
103-
}
104-
105-
for _, test := range testCases {
106-
t.Run(test.path, func(t *testing.T) {
107-
id := IDFromPath(test.prefix, test.path)
108-
require.Exactly(t, test.expected, id)
109-
})
110-
}
111-
}
112-
113-
func TestSplitPath(t *testing.T) {
114-
testCases := []struct {
115-
path string
116-
expected []string
117-
}{
118-
{"", []string{}},
119-
{"root", []string{"root"}},
120-
{"/root", []string{"root"}},
121-
{"/root/", []string{"root"}},
122-
{"root/", []string{"root"}},
123-
{"root/other", []string{"root", "other"}},
124-
{"root//other", []string{"root", "other"}},
125-
{"/root//other", []string{"root", "other"}},
126-
{"/root//other/", []string{"root", "other"}},
127-
}
128-
129-
for _, test := range testCases {
130-
t.Run(test.path, func(t *testing.T) {
131-
path := SplitPath(test.path)
132-
require.Exactly(t, test.expected, path)
133-
})
134-
}
135-
}

0 commit comments

Comments
 (0)