improve redirection to canonical import path

Problem:

Our GitHub organisation is named Teamwork, but for various hard to
change reasons we import packages as teamwork (lower case t). To enforce
this we add an import enforcement for our packages:

	package duck // import "github.com/teamwork/duck"

This works well, but godoc.org enforces users to use the canonical
GitHub path (Teamwork), but with the import statement the Go compiler
enforces a different path, leading to an error.

Fix:

Instead of immediately redirecting in the gosrc package, it will now
only store the canonical import path there. This is before the Go source
code is actually scanned, so we don't *actually* know what the real
canonical path should be.

In the doc.newPackage() function it will check for the canonical import
path, taking both the Go import path as well as the reported path from
the gosrc package in to account, and redirect as needed.

This seems to work for all the cases I can think of:

    github.com/Teamwork/validate    -> github.com/teamwork/validate
    github.com/pkg/ERRORS           -> github.com/pkg/errors
    github.com/Carpetsmoker/sconfig -> arp242.net/sconfig
    arp242.net/SCONFIG              -> not found (expected)
    bitbucket.org/pkg/inflect       -> works
    bitbucket.org/pkg/INFLECT       -> works (should probably redirect too)
    github.com/docker/docker/client -> works (#534)
    github.com/moby/moby/client     -> redirects (on master this actually seems to error out?)

It should also be easy to add a similar check for for some other repo
providers, such as BitBucket, GitLab, etc.

Fixes #507

Change-Id: I49c5ccb328694f95dd7a0e5d577297d56d88893f
GitHub-Last-Rev: 28efc3756488fdd07649986f26afcbe85385bd5d
GitHub-Pull-Request: golang/gddo#560
Reviewed-on: https://go-review.googlesource.com/120059
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
3 files changed