Mark ALREADY_MERGED changes as merged in the DB

If a change is marked ALREADY_MERGED, likely a result of a bug in the
merge code, it does not end up in the toSubmit list and so was never
being marked as merged, despite the branch head already having
advanced. Fix this so ALREADY_MERGED changes are updated to match the
branch state.

This does not fix any bugs in the merge code proper, but at least
allows recovery from a certain kind of bad state.

Works around (does not fix) bug 1985 and perhaps bug 600.

Change-Id: I44548cce039e303a86d579b0b0d76b12ad9bdcfb
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/ b/gerrit-server/src/main/java/com/google/gerrit/server/git/
index a59a216..9715d9c4 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/
@@ -598,6 +598,11 @@
         try {
           if (rw.isMergedInto(commit, branchTip)) {
             commit.statusCode = CommitMergeStatus.ALREADY_MERGED;
+            try {
+              setMergedPatchSet(chg.getId(), ps.getId());
+            } catch (OrmException e) {
+              log.error("Cannot mark change " + chg.getId() + " merged", e);
+            }
         } catch (IOException err) {