From cd2ecfb7b35cb4199bcbc735bd120393e6c4ba8e Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sat, 6 Feb 2010 08:53:46 +1100 Subject: [PATCH] Ugly hack to provide some mechanism for printing documentation about Go source outside $GOROOT. If the argument is a path starting with / or ., disregard $GOROOT. Also, disable the check for package name matching the directory, which is counterproductive in this case. Apologies for the violence to the code but we need some help documenting Go code outside the standard repository. R=gri CC=golang-dev https://golang.org/cl/201064 --- src/cmd/godoc/godoc.go | 30 ++++++++++++++++++++++++++++-- src/cmd/godoc/main.go | 10 ++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/cmd/godoc/godoc.go b/src/cmd/godoc/godoc.go index b49487e5b8..97ed329fe2 100644 --- a/src/cmd/godoc/godoc.go +++ b/src/cmd/godoc/godoc.go @@ -1043,8 +1043,20 @@ type httpHandler struct { // PageInfo.Dirs is nil. // func (h *httpHandler) getPageInfo(path string, try bool) PageInfo { - // the path is relative to h.fsroot - dirname := pathutil.Join(h.fsRoot, path) + var dirname string + // If the path starts with a slash or ., ignore $GOROOT. + // It would be nice to handle "./dir" too, but godoc chdirs to $GOROOT. TODO: fix. + if len(path) > 0 && path[0] == '/' { + dirname = path + // --- Start of hack + } else if len(path) > 0 && path[0] == '.' && workingDir != "" { + path = pathutil.Join(workingDir, path) + dirname = path + // --- End of hack + } else { + // the path is relative to h.fsroot + dirname = pathutil.Join(h.fsRoot, path) + } // the package name is the directory name within its parent // (use dirname instead of path because dirname is clean; i.e. has no trailing '/') @@ -1053,6 +1065,20 @@ func (h *httpHandler) getPageInfo(path string, try bool) PageInfo { // filter function to select the desired .go files filter := func(d *os.Dir) bool { if isPkgFile(d) { + + // --- Start of hack. + // An ugly special case: If the path is rooted, just say + // yes in the hope we'll get some output from a directory + // outside $GOROOT. Ugly but effective for command-line + // output but may not find everything if there are multiple + // packages in the directory, since godoc assumes one + // package per directory. + // TODO: Do this better. + if len(path) > 0 && path[0] == '/' { + return true + } + // --- End of hack. + // Some directories contain main packages: Only accept // files that belong to the expected package so that // parser.ParsePackage doesn't return "multiple packages diff --git a/src/cmd/godoc/main.go b/src/cmd/godoc/main.go index 7a3b9f384a..f475d8fa10 100644 --- a/src/cmd/godoc/main.go +++ b/src/cmd/godoc/main.go @@ -47,6 +47,9 @@ var ( // layout control html = flag.Bool("html", false, "print HTML in command-line mode") + + // --- Hack to remember current directory + workingDir string ) @@ -152,6 +155,13 @@ func main() { log.Exitf("negative tabwidth %d", *tabwidth) } + // --- Start of hack. + // Remember where we were, so "." works as a directory name. + // Error's not worth worrying about; we just check for empty string + // when we need it. + workingDir, _ = os.Getwd() + // --- End of hack. + if err := os.Chdir(goroot); err != nil { log.Exitf("chdir %s: %v", goroot, err) } -- 2.50.0