Skip to content

Commit 1e1a095

Browse files
committed
cmd/server/commands: use relative path as id instead of last part
Fixes #681 Signed-off-by: Miguel Molina <[email protected]>
1 parent 7dfd1e0 commit 1e1a095

File tree

4 files changed

+132
-10
lines changed

4 files changed

+132
-10
lines changed

cmd/gitbase/command/server.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (c *Server) addDirectory(directory string) error {
285285
}
286286

287287
for _, match := range matches {
288-
if err := c.addMatch(match); err != nil {
288+
if err := c.addMatch(directory, match); err != nil {
289289
logrus.WithFields(logrus.Fields{
290290
"path": match,
291291
"error": err,
@@ -296,11 +296,12 @@ func (c *Server) addDirectory(directory string) error {
296296
return nil
297297
}
298298

299-
func (c *Server) addMatch(match string) error {
299+
func (c *Server) addMatch(prefix, match string) error {
300300
root, err := filepath.Abs(match)
301301
if err != nil {
302302
return err
303303
}
304+
304305
root, err = filepath.EvalSymlinks(root)
305306
if err != nil {
306307
return err
@@ -316,7 +317,7 @@ func (c *Server) addMatch(match string) error {
316317
}
317318

318319
if info.IsDir() {
319-
if err := c.addIfGitRepo(path); err != nil {
320+
if err := c.addIfGitRepo(prefix, path); err != nil {
320321
return err
321322
}
322323

@@ -329,8 +330,14 @@ func (c *Server) addMatch(match string) error {
329330
}
330331

331332
if !c.DisableSiva &&
332-
info.Mode().IsRegular() && gitbase.IsSivaFile(info.Name()) {
333-
if err := c.pool.AddSivaFileWithID(info.Name(), path); err != nil {
333+
info.Mode().IsRegular() &&
334+
gitbase.IsSivaFile(info.Name()) {
335+
id, err := gitbase.StripPrefix(prefix, path)
336+
if err != nil {
337+
return err
338+
}
339+
340+
if err := c.pool.AddSivaFileWithID(id, path); err != nil {
334341
logrus.WithFields(logrus.Fields{
335342
"path": path,
336343
"error": err,
@@ -346,7 +353,7 @@ func (c *Server) addMatch(match string) error {
346353
})
347354
}
348355

349-
func (c *Server) addIfGitRepo(path string) error {
356+
func (c *Server) addIfGitRepo(prefix, path string) error {
350357
ok, err := gitbase.IsGitRepo(path)
351358
if err != nil {
352359
logrus.WithFields(logrus.Fields{
@@ -359,10 +366,14 @@ func (c *Server) addIfGitRepo(path string) error {
359366

360367
if ok {
361368
if !c.DisableGit {
362-
base := filepath.Base(path)
363-
if err := c.pool.AddGitWithID(base, path); err != nil {
369+
id, err := gitbase.StripPrefix(prefix, path)
370+
if err != nil {
371+
return err
372+
}
373+
374+
if err := c.pool.AddGitWithID(id, path); err != nil {
364375
logrus.WithFields(logrus.Fields{
365-
"id": base,
376+
"id": id,
366377
"path": path,
367378
"error": err,
368379
}).Error("repository could not be added")

cmd/gitbase/command/server_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package command
22

33
import (
4+
"io"
45
"os"
6+
"sort"
57
"testing"
68

79
"github.com/src-d/gitbase"
@@ -31,6 +33,33 @@ func TestAddMatch(t *testing.T) {
3133
}
3234
c := &Server{pool: gitbase.NewRepositoryPool(0)}
3335
for _, e := range expected {
34-
e.err(c.addMatch(e.path))
36+
e.err(c.addMatch("../../../_testdata", e.path))
3537
}
3638
}
39+
40+
func TestAddDirectory(t *testing.T) {
41+
require := require.New(t)
42+
c := &Server{pool: gitbase.NewRepositoryPool(0), Depth: 5}
43+
require.NoError(c.addDirectory("../../../_testdata/*"))
44+
i, err := c.pool.RepoIter()
45+
require.NoError(err)
46+
47+
var repositories []string
48+
for {
49+
r, err := i.Next()
50+
if err == io.EOF {
51+
require.NoError(i.Close())
52+
break
53+
}
54+
55+
repositories = append(repositories, r.ID)
56+
}
57+
58+
sort.Strings(repositories)
59+
expected := []string{
60+
"05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
61+
"ff/fff840f8784ef162dc83a1465fc5763d890b68ba.siva",
62+
"fff7062de8474d10a67d417ccea87ba6f58ca81d.siva",
63+
}
64+
require.Equal(expected, repositories)
65+
}

path_utils.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@ import (
1111
// RegMatchChars matches a string with a glob expression inside.
1212
var RegMatchChars = regexp.MustCompile(`(^|[^\\])([*[?])`)
1313

14+
// StripPrefix removes the root path from the given path. Root may be a glob.
15+
func StripPrefix(root, path string) (string, error) {
16+
var err error
17+
root, err = filepath.Abs(cleanGlob(root))
18+
if err != nil {
19+
return "", err
20+
}
21+
22+
if !strings.HasSuffix(root, string(filepath.Separator)) {
23+
root += string(filepath.Separator)
24+
}
25+
26+
path, err = filepath.Abs(path)
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
return strings.TrimPrefix(path, root), nil
32+
}
33+
34+
// cleanGlob removes all the parts of a glob that are not fixed.
35+
func cleanGlob(pattern string) string {
36+
var parts []string
37+
for _, part := range strings.Split(pattern, string(filepath.Separator)) {
38+
if strings.ContainsAny(part, "*?[\\") {
39+
break
40+
}
41+
42+
parts = append(parts, part)
43+
}
44+
45+
return strings.Join(parts, string(filepath.Separator))
46+
}
47+
1448
// PatternMatches returns the paths matched and any error found.
1549
func PatternMatches(pattern string) ([]string, error) {
1650
abs, err := filepath.Abs(pattern)

path_utils_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,51 @@ func TestIsSivaFile(t *testing.T) {
6161
require.True(IsSivaFile("is.siva"))
6262
require.False(IsSivaFile("not-siva"))
6363
}
64+
65+
func TestStripPrefix(t *testing.T) {
66+
testCases := []struct {
67+
root string
68+
path string
69+
expected string
70+
}{
71+
{
72+
"_testdata/*",
73+
"_testdata/05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
74+
"05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
75+
},
76+
{
77+
"_testdata/*",
78+
"_testdata/foo/05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
79+
"foo/05893125684f2d3943cd84a7ab2b75e53668fba1.siva",
80+
},
81+
}
82+
83+
for _, tt := range testCases {
84+
t.Run(tt.path, func(t *testing.T) {
85+
output, err := StripPrefix(tt.root, tt.path)
86+
require.NoError(t, err)
87+
require.Equal(t, tt.expected, output)
88+
})
89+
}
90+
}
91+
92+
func TestCleanGlob(t *testing.T) {
93+
testCases := []struct {
94+
pattern string
95+
expected string
96+
}{
97+
{"../../../_testdata/?epositories", "../../../_testdata"},
98+
{"../../../_testdata/**/repositories", "../../../_testdata"},
99+
{"../../../_testdata/*/repositories", "../../../_testdata"},
100+
{"../../../_testdata/*", "../../../_testdata"},
101+
{"../../../_testdata/\\*/foo", "../../../_testdata"},
102+
{"../../../_testdata/[a-z]/foo", "../../../_testdata"},
103+
}
104+
105+
for _, tt := range testCases {
106+
t.Run(tt.pattern, func(t *testing.T) {
107+
output := cleanGlob(tt.pattern)
108+
require.Equal(t, tt.expected, output)
109+
})
110+
}
111+
}

0 commit comments

Comments
 (0)