Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// <meta name="go-import" content="9fans.net/go git https://github.com/9fans/go">
// <meta http-equiv="refresh" content="0; url=https://godoc.org/9fans.net/go/acme/editinacme">
//
// If both <import> and <repo> end in /*, the corresponding path element
// If both <import> and <repo> end in one or more /*, the corresponding path element
// is taken from the import path and substituted in repo on each request.
// For example, if invoked as:
//
Expand Down Expand Up @@ -76,7 +76,7 @@ var (
vcs = flag.String("vcs", "git", "set version control `system`")
importPath string
repoPath string
wildcard bool
wildcards int
)

func usage() {
Expand Down Expand Up @@ -105,8 +105,8 @@ func main() {
if strings.HasSuffix(importPath, "/*") != strings.HasSuffix(repoPath, "/*") {
log.Fatal("either both import and repo must have /* or neither")
}
if strings.HasSuffix(importPath, "/*") {
wildcard = true
for strings.HasSuffix(importPath, "/*") {
wildcards++
importPath = strings.TrimSuffix(importPath, "/*")
repoPath = strings.TrimSuffix(repoPath, "/*")
}
Expand Down Expand Up @@ -146,7 +146,7 @@ type data struct {
func redirect(w http.ResponseWriter, req *http.Request) {
path := strings.TrimSuffix(req.Host+req.URL.Path, "/")
var importRoot, repoRoot, suffix string
if wildcard {
if wildcards > 0 {
if path == importPath {
http.Redirect(w, req, "https://godoc.org/"+importPath, 302)
return
Expand All @@ -156,8 +156,9 @@ func redirect(w http.ResponseWriter, req *http.Request) {
return
}
elem := path[len(importPath)+1:]
if i := strings.Index(elem, "/"); i >= 0 {
elem, suffix = elem[:i], elem[i:]
if parts := strings.Split(elem, "/"); len(parts) >= wildcards {
elem = strings.Join(parts[:wildcards], "/")
suffix = strings.Join(parts[wildcards:], "/")
}
importRoot = importPath + "/" + elem
repoRoot = repoPath + "/" + elem
Expand Down