website: support /gw/ redirects for more paths

If it doesn't look like a commit hash, assume it's a directory and
redirect accordingly. This won't support top-level directories that are
entirely hex characters, but those weren't being handled before anyway.
None exist today, and if they ever do, they could be manually supported
the way "client" and "server" were previously.

Change-Id: Id65187ab86e76f1121e18f09d5b2a93b5b37d79a
This commit is contained in:
Will Norris 2018-01-01 20:49:27 +00:00
parent 7bbda1509c
commit ce00f4cac4
2 changed files with 9 additions and 7 deletions

View File

@ -252,14 +252,15 @@ func serveError(w http.ResponseWriter, r *http.Request, relpath string, err erro
const gerritURLPrefix = "https://camlistore.googlesource.com/camlistore/+/"
var commitHash = regexp.MustCompile(`^p=camlistore.git;a=commit;h=([0-9a-f]+)$`)
var commitHash = regexp.MustCompile(`^(?i)[0-9a-f]+$`)
var gitwebCommit = regexp.MustCompile(`^p=camlistore.git;a=commit;h=([0-9a-f]+)$`)
// empty return value means don't redirect.
func redirectPath(u *url.URL) string {
// Example:
// Redirect old gitweb URLs to gerrit. Example:
// /code/?p=camlistore.git;a=commit;h=b0d2a8f0e5f27bbfc025a96ec3c7896b42d198ed
if strings.HasPrefix(u.Path, "/code/") {
m := commitHash.FindStringSubmatch(u.RawQuery)
m := gitwebCommit.FindStringSubmatch(u.RawQuery)
if len(m) == 2 {
return gerritURLPrefix + m[1]
}
@ -267,11 +268,11 @@ func redirectPath(u *url.URL) string {
if strings.HasPrefix(u.Path, "/gw/") {
path := strings.TrimPrefix(u.Path, "/gw/")
if strings.HasPrefix(path, "clients") || strings.HasPrefix(path, "server") {
return gerritURLPrefix + "master/" + path
if commitHash.MatchString(path) {
// Assume it's a commit
return gerritURLPrefix + path
}
// Assume it's a commit
return gerritURLPrefix + path
return gerritURLPrefix + "master/" + path
}
if strings.HasPrefix(u.Path, "/docs/") {

View File

@ -28,6 +28,7 @@ func TestRedirect(t *testing.T) {
}{
{"/foo", ""},
{"/gw/502aff1fd522c454e39a3723b596aca43d206d4e", "https://camlistore.googlesource.com/camlistore/+/502aff1fd522c454e39a3723b596aca43d206d4e"},
{"/gw/502AFF", "https://camlistore.googlesource.com/camlistore/+/502AFF"},
{"/gw/server", "https://camlistore.googlesource.com/camlistore/+/master/server"},
{"/code/?p=camlistore.git;a=commit;h=b0d2a8f0e5f27bbfc025a96ec3c7896b42d198ed", "https://camlistore.googlesource.com/camlistore/+/b0d2a8f0e5f27bbfc025a96ec3c7896b42d198ed"},
{"/docs/schema/", "/doc/schema/"},