LogServlet: Handle HEAD symref to non-existent branch The ObjectId of a Ref can be null. If HEAD is a symbolic ref to a non-existent branch, headRef.getObjectId() will return null. The current LogServlet code does not handle this, causing NullPointerException in RevWalk. This issue is particularly common when a repo is created but nothing is pushed yet; so a HEAD symmetry is created to point to refs/heads/main by default but lacks a commit SHA. Treat a HEAD ref with a null ObjectId as if the ref itself were null, returning no view instead of throwing an internal error. Change-Id: I4fe85010db0694403770923fcdf957854e72cd34 Signed-off-by: Xing Huang <[email protected]>
diff --git a/java/com/google/gitiles/LogServlet.java b/java/com/google/gitiles/LogServlet.java index 71f7384..368fd0b 100644 --- a/java/com/google/gitiles/LogServlet.java +++ b/java/com/google/gitiles/LogServlet.java
@@ -191,10 +191,14 @@ if (headRef == null) { return null; } + ObjectId id = headRef.getObjectId(); + if (id == null) { + return null; + } try (RevWalk walk = new RevWalk(repo)) { return GitilesView.log() .copyFrom(view) - .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk)) + .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(id), walk)) .build(); } }