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
207205func explainAddr (addr string ) string {
0 commit comments