Fix NPE when target branch doesn't exist
Do not try to read the Git commit associated
to a branch that doesn't exist.
Change-Id: Ifc42897644c9d833b6ce086bbc9662f2031b236e
diff --git a/owners-common/src/main/java/com/googlesource/gerrit/owners/common/JgitWrapper.java b/owners-common/src/main/java/com/googlesource/gerrit/owners/common/JgitWrapper.java
index 9668af8..c1a7368 100644
--- a/owners-common/src/main/java/com/googlesource/gerrit/owners/common/JgitWrapper.java
+++ b/owners-common/src/main/java/com/googlesource/gerrit/owners/common/JgitWrapper.java
@@ -34,9 +34,14 @@
public static Optional<byte[]> getBlobAsBytes(Repository repository, String revision, String path)
throws IOException {
+ ObjectId objectId = repository.resolve(revision);
+ if (objectId == null) {
+ return Optional.empty();
+ }
+
try (final TreeWalk w =
TreeWalk.forPath(
- repository, path, parseCommit(repository, repository.resolve(revision)).getTree())) {
+ repository, path, parseCommit(repository, objectId).getTree())) {
return Optional.ofNullable(w)
.filter(walk -> (walk.getRawMode(0) & TYPE_MASK) == TYPE_FILE)