Skip to content

Commit

Permalink
Match prefixes if available
Browse files Browse the repository at this point in the history
  • Loading branch information
cskr committed Jun 21, 2013
1 parent f7c5d4a commit a64348f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
24 changes: 19 additions & 5 deletions gorepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
}

type PackageList struct {
packages map[string]Package
packages map[string]*Package
mx sync.RWMutex
file string
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (pl *PackageList) loadPackages() error {
defer f.Close()
in := bufio.NewReader(f)

pkgs := make(map[string]Package)
pkgs := make(map[string]*Package)
for {
ln, _, err := in.ReadLine()
if err == io.EOF {
Expand Down Expand Up @@ -154,7 +154,7 @@ func (pl *PackageList) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"pkgs": pl.packages,
})
} else {
if pkg, ok := pl.packages[r.URL.Path]; ok {
if pkg, ok := pl.getPackage(r.URL.Path); ok {
if r.FormValue("go-get") == "1" || pkg.Doc == "" {
pkgTmpl.Execute(w, map[string]interface{}{
"host": r.Host,
Expand All @@ -169,16 +169,30 @@ func (pl *PackageList) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (pl *PackageList) getPackage(path string) (*Package, bool) {
if pkg, ok := pl.packages[path]; ok {
return pkg, ok
}

for prefix := path; prefix != ""; prefix = prefix[:strings.LastIndex(prefix, "/")] {
if pkg, ok := pl.packages[prefix]; ok {
return pkg, ok
}
}

return nil, false
}

type Package struct {
Path, Vcs, Repo, Doc string
}

func NewPackage(line string) Package {
func NewPackage(line string) *Package {
fields := strings.Fields(line)
doc := ""
if len(fields) > 3 {
doc = fields[3]
}

return Package{fields[0], fields[1], fields[2], doc}
return &Package{fields[0], fields[1], fields[2], doc}
}
33 changes: 26 additions & 7 deletions gorepos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,43 @@ func TestPkg(t *testing.T) {
return
}

body, expected := invokePkg(pl, "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", true)
body, expected := invokePkg(pl, "lib1", "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
return
}

body, expected = invokePkg(pl, "lib2", "hg", "ssh://hg@bitbucket.org/user2/lib2", true)
body, expected = invokePkg(pl, "lib2", "lib2", "hg", "ssh://hg@bitbucket.org/user2/lib2", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
return
}

body, expected = invokePkg(pl, "lib3", "git", "ssh://git@go.mydomain.com/lib3", true)
body, expected = invokePkg(pl, "lib3", "lib3", "git", "ssh://git@go.mydomain.com/lib3", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
}
}

func TestPrefix(t *testing.T) {
list, err := generateList()
if err != nil {
t.Errorf("Error generating package list: %s", err)
}

pl, err := NewPackageList(list)
if err != nil {
t.Errorf("Error reading package list: %s", err)
return
}

body, expected := invokePkg(pl, "lib1/subdir", "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
return
}
}

func TestReload(t *testing.T) {
list, err := generateList()
if err != nil {
Expand All @@ -113,7 +132,7 @@ func TestReload(t *testing.T) {
}

time.Sleep(100 * time.Millisecond)
body, expected := invokePkg(pl, "lib4", "git", "ssh://git@go.mydomain.com/lib4", true)
body, expected := invokePkg(pl, "lib4", "lib4", "git", "ssh://git@go.mydomain.com/lib4", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
}
Expand All @@ -131,7 +150,7 @@ func TestRedirect(t *testing.T) {
return
}

body, expected := invokePkg(pl, "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", false)
body, expected := invokePkg(pl, "lib1", "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", false)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
return
Expand Down Expand Up @@ -173,14 +192,14 @@ func appendList(list, line string) error {
return nil
}

func invokePkg(pl *PackageList, pkg, vcs, repo string, includeParam bool) (body, expected string) {
func invokePkg(pl *PackageList, pkg, root, vcs, repo string, includeParam bool) (body, expected string) {
w := recordPkg(pl, pkg, vcs, repo, includeParam)

b := new(bytes.Buffer)
pkgTmpl.Execute(b, map[string]interface{}{
"host": "example.com",
"pkg": map[string]string{
"Path": "/" + pkg,
"Path": "/" + root,
"Vcs": vcs,
"Repo": repo,
},
Expand Down

0 comments on commit a64348f

Please sign in to comment.