in java/com/google/gitiles/GitwebRedirectFilter.java [64:138]
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws IOException, ServletException {
GitilesView gitwebView = ViewFilter.getView(req);
if (!isGitwebStyleQuery(req)) {
chain.doFilter(req, res);
return;
}
ListMultimap<String, String> params = parse(req.getQueryString());
String action = getFirst(params, "a");
String project = getFirst(params, "p");
String path = Strings.nullToEmpty(getFirst(params, "f"));
// According to gitweb's perl source code, the primary parameters are these
// short abbreviated names. When pointing to blob or subtree hash,hashParent
// are the blob or subtree SHA-1s and hashBase,hashParentBase are commits.
// When pointing to commits or tags, hash is the commit/tag. Its messy.
Revision hash = toRevision(getFirst(params, "h"));
Revision hashBase = toRevision(getFirst(params, "hb"));
Revision hashParent = toRevision(getFirst(params, "hp"));
Revision hashParentBase = toRevision(getFirst(params, "hpb"));
GitilesView.Builder view;
if ("project_index".equals(action)) {
view = GitilesView.hostIndex();
project = null;
} else if ("summary".equals(action) || "tags".equals(action)) {
view = GitilesView.repositoryIndex();
} else if (("commit".equals(action) || "tag".equals(action)) && hash != null) {
view = GitilesView.revision().setRevision(hash);
} else if ("log".equals(action) || "shortlog".equals(action)) {
view = GitilesView.log().setRevision(firstNonNull(hash, Revision.HEAD));
} else if ("tree".equals(action)) {
view =
GitilesView.path().setRevision(firstNonNull(hashBase, Revision.HEAD)).setPathPart(path);
} else if (("blob".equals(action) || "blob_plain".equals(action))
&& hashBase != null
&& !path.isEmpty()) {
view = GitilesView.path().setRevision(hashBase).setPathPart(path);
} else if ("commitdiff".equals(action) && hash != null) {
view =
GitilesView.diff()
.setOldRevision(firstNonNull(hashParent, Revision.NULL))
.setRevision(hash)
.setPathPart("");
} else if ("blobdiff".equals(action)
&& !path.isEmpty()
&& hashParentBase != null
&& hashBase != null) {
view =
GitilesView.diff().setOldRevision(hashParentBase).setRevision(hashBase).setPathPart(path);
} else if ("history".equals(action) && !path.isEmpty()) {
view = GitilesView.log().setRevision(firstNonNull(hashBase, Revision.HEAD)).setPathPart(path);
} else {
// Gitiles does not provide an RSS feed (a=rss,atom,opml)
// Any other URL is out of date and not valid anymore.
throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_GITWEB_URL);
}
if (!Strings.isNullOrEmpty(project)) {
view.setRepositoryName(cleanProjectName(project));
}
String url;
try {
url =
view.setHostName(gitwebView.getHostName())
.setServletPath(gitwebView.getServletPath())
.toUrl();
} catch (InvalidViewException e) {
throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_GITWEB_URL, e);
}
res.setStatus(SC_MOVED_PERMANENTLY);
res.setHeader(LOCATION, url);
}