Skip to content

Commit f4facb0

Browse files
authored
servegit: list flag to list found repositories (#252)
This is useful functionality when setting up serve-git to understand what it does.
1 parent 1fd473a commit f4facb0

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed

cmd/src/servegit.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ func init() {
1717
fmt.Fprintf(flag.CommandLine.Output(), `'src serve-git' serves your local git repositories over HTTP for Sourcegraph to pull.
1818
1919
USAGE
20-
src [-v] serve-git [-addr :3434] [path/to/dir]
20+
src [-v] serve-git [-list] [-addr :3434] [path/to/dir]
2121
2222
By default 'src serve-git' will recursively serve your current directory on the address ':3434'.
23+
24+
'src serve-git -list' will not start up the server. Instead it will write to stdout a list of
25+
repository names it would serve.
2326
`)
2427
}
2528
var (
2629
addrFlag = flagSet.String("addr", ":3434", "Address on which to serve (end with : for unused port)")
30+
listFlag = flagSet.Bool("list", false, "list found repository names")
2731
)
2832

2933
handler := func(args []string) error {
@@ -58,11 +62,24 @@ By default 'src serve-git' will recursively serve your current directory on the
5862
Info: log.New(os.Stderr, "serve-git: ", log.LstdFlags),
5963
Debug: dbug,
6064
}
65+
66+
if *listFlag {
67+
repos, err := s.Repos()
68+
if err != nil {
69+
return err
70+
}
71+
for _, r := range repos {
72+
fmt.Println(r.Name)
73+
}
74+
return nil
75+
}
76+
6177
return s.Start()
6278
}
6379

6480
// Register the command.
6581
commands = append(commands, &command{
82+
aliases: []string{"servegit"},
6683
flagSet: flagSet,
6784
handler: handler,
6885
usageFunc: usageFunc,

internal/servegit/serve.go

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"os/exec"
12-
"path"
12+
pathpkg "path"
1313
"path/filepath"
1414
"strings"
1515

@@ -79,32 +79,10 @@ func (s *Serve) handler() http.Handler {
7979
})
8080

8181
mux.HandleFunc("/v1/list-repos", func(w http.ResponseWriter, r *http.Request) {
82-
var repos []Repo
83-
var reposRootIsRepo bool
84-
for _, name := range s.repos() {
85-
if name == "." {
86-
reposRootIsRepo = true
87-
}
88-
89-
repos = append(repos, Repo{
90-
Name: name,
91-
URI: path.Join("/repos", name),
92-
})
93-
}
94-
95-
if reposRootIsRepo {
96-
// Update all names to be relative to the parent of
97-
// reposRoot. This is to give a better name than "." for repos
98-
// root
99-
abs, err := filepath.Abs(s.Root)
100-
if err != nil {
101-
http.Error(w, "failed to get the absolute path of reposRoot: "+err.Error(), http.StatusInternalServerError)
102-
return
103-
}
104-
rootName := filepath.Base(abs)
105-
for i := range repos {
106-
repos[i].Name = path.Join(rootName, repos[i].Name)
107-
}
82+
repos, err := s.Repos()
83+
if err != nil {
84+
http.Error(w, err.Error(), http.StatusInternalServerError)
85+
return
10886
}
10987

11088
resp := struct {
@@ -142,10 +120,10 @@ func (s *Serve) handler() http.Handler {
142120
})
143121
}
144122

145-
// repos returns a slice of all the git directories it finds. The paths are
146-
// relative to root.
147-
func (s *Serve) repos() []string {
148-
var gitDirs []string
123+
// Repos returns a slice of all the git repositories it finds.
124+
func (s *Serve) Repos() ([]Repo, error) {
125+
var repos []Repo
126+
var reposRootIsRepo bool
149127

150128
err := filepath.Walk(s.Root, func(path string, fi os.FileInfo, fileErr error) error {
151129
if fileErr != nil {
@@ -178,7 +156,13 @@ func (s *Serve) repos() []string {
178156
// subpath). So Rel should always work.
179157
s.Info.Fatalf("filepath.Walk returned %s which is not relative to %s: %v", path, s.Root, err)
180158
}
181-
gitDirs = append(gitDirs, filepath.ToSlash(subpath))
159+
160+
name := filepath.ToSlash(subpath)
161+
reposRootIsRepo = reposRootIsRepo || name == "."
162+
repos = append(repos, Repo{
163+
Name: name,
164+
URI: pathpkg.Join("/repos", name),
165+
})
182166

183167
// Check whether a repository is a bare repository or not.
184168
//
@@ -197,11 +181,25 @@ func (s *Serve) repos() []string {
197181
})
198182

199183
if err != nil {
200-
// Our WalkFunc doesn't return any errors, so neither should filepath.Walk
201-
panic(err)
184+
return nil, err
185+
}
186+
187+
if !reposRootIsRepo {
188+
return repos, nil
189+
}
190+
191+
// Update all names to be relative to the parent of reposRoot. This is to
192+
// give a better name than "." for repos root
193+
abs, err := filepath.Abs(s.Root)
194+
if err != nil {
195+
return nil, fmt.Errorf("failed to get the absolute path of reposRoot: %w", err)
196+
}
197+
rootName := filepath.Base(abs)
198+
for i := range repos {
199+
repos[i].Name = pathpkg.Join(rootName, repos[i].Name)
202200
}
203201

204-
return gitDirs
202+
return repos, nil
205203
}
206204

207205
func explainAddr(addr string) string {

internal/servegit/serve_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,14 @@ func TestIgnoreGitSubmodules(t *testing.T) {
175175
t.Fatal(err)
176176
}
177177

178-
repos := (&Serve{
178+
repos, err := (&Serve{
179179
Info: testLogger(t),
180180
Debug: discardLogger,
181181
Root: root,
182-
}).repos()
182+
}).Repos()
183+
if err != nil {
184+
t.Fatal(err)
185+
}
183186
if len(repos) != 0 {
184187
t.Fatalf("expected no repos, got %v", repos)
185188
}

0 commit comments

Comments
 (0)