Merge "LDAP-cache to minimize nbr of queries when unnesting groups." into stable-2.5
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java b/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java
index ab3ca56..f07ae0c 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java
@@ -28,6 +28,7 @@
 import com.google.gerrit.reviewdb.client.RevId;
 import com.google.gerrit.reviewdb.client.TrackingId;
 import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.GerritPersonIdent;
 import com.google.gerrit.server.account.AccountCache;
 import com.google.gerrit.server.config.CanonicalWebUrl;
 import com.google.gerrit.server.patch.PatchList;
@@ -41,6 +42,7 @@
 import com.google.inject.Singleton;
 
 import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.PersonIdent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -59,17 +61,20 @@
   private final ApprovalTypes approvalTypes;
   private final PatchListCache patchListCache;
   private final SchemaFactory<ReviewDb> schema;
+  private final PersonIdent myIdent;
 
   @Inject
   EventFactory(AccountCache accountCache,
       @CanonicalWebUrl @Nullable Provider<String> urlProvider,
       ApprovalTypes approvalTypes,
-      PatchListCache patchListCache, SchemaFactory<ReviewDb> schema) {
+      PatchListCache patchListCache, SchemaFactory<ReviewDb> schema,
+      @GerritPersonIdent PersonIdent myIdent) {
     this.accountCache = accountCache;
     this.urlProvider = urlProvider;
     this.approvalTypes = approvalTypes;
     this.patchListCache = patchListCache;
     this.schema = schema;
+    this.myIdent = myIdent;
   }
 
   /**
@@ -390,6 +395,20 @@
   }
 
   /**
+   * Create an AuthorAttribute for the given person ident suitable for
+   * serialization to JSON.
+   *
+   * @param ident
+   * @return object suitable for serialization to JSON
+   */
+  public AccountAttribute asAccountAttribute(PersonIdent ident) {
+    AccountAttribute who = new AccountAttribute();
+    who.name = ident.getName();
+    who.email = ident.getEmailAddress();
+    return who;
+  }
+
+  /**
    * Create an ApprovalAttribute for the given approval suitable for
    * serialization to JSON.
    *
@@ -413,7 +432,9 @@
   public MessageAttribute asMessageAttribute(ChangeMessage message) {
     MessageAttribute a = new MessageAttribute();
     a.timestamp = message.getWrittenOn().getTime() / 1000L;
-    a.reviewer = asAccountAttribute(message.getAuthor());
+    a.reviewer =
+        message.getAuthor() != null ? asAccountAttribute(message.getAuthor())
+            : asAccountAttribute(myIdent);
     a.message = message.getMessage();
     return a;
   }
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/SubmoduleOp.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/SubmoduleOp.java
index 2e70621..ccb91a3 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/SubmoduleOp.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/SubmoduleOp.java
@@ -365,14 +365,17 @@
   private static DirCache readTree(final Repository pdb, final Ref branch)
       throws MissingObjectException, IncorrectObjectTypeException, IOException {
     final RevWalk rw = new RevWalk(pdb);
-
-    final DirCache dc = DirCache.newInCore();
-    final DirCacheBuilder b = dc.builder();
-    b.addTree(new byte[0], // no prefix path
-        DirCacheEntry.STAGE_0, // standard stage
-        pdb.newObjectReader(), rw.parseTree(branch.getObjectId()));
-    b.finish();
-    return dc;
+    try {
+      final DirCache dc = DirCache.newInCore();
+      final DirCacheBuilder b = dc.builder();
+      b.addTree(new byte[0], // no prefix path
+          DirCacheEntry.STAGE_0, // standard stage
+          pdb.newObjectReader(), rw.parseTree(branch.getObjectId()));
+      b.finish();
+      return dc;
+    } finally {
+      rw.release();
+    }
   }
 
   private static void logAndThrowSubmoduleException(final String errorMsg,