Fix replaying of revisions when the old and new ref are the same

In the unlikely case when the imported change gets the same number as in
the source system the create/delete of the new/old patch set ref would
fail in the create step as this ref already exists.

Implement a proper ref rename which takes care about this special case.

Change-Id: I889543de11815b171674f170620c43bf8ced54b5
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProjectTask.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProjectTask.java
index 1308d4b..db23899 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProjectTask.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProjectTask.java
@@ -368,8 +368,7 @@
 
         ChangeUtil.insertAncestors(db, ps.getId(), commit);
 
-        createRef(repo, ps);
-        deleteRef(repo, ps, origRef);
+        renameRef(repo, origRef, ps);
       }
 
       db.patchSets().insert(patchSets);
@@ -452,6 +451,16 @@
         input, ts);
   }
 
+  private void renameRef(Repository repo, String origRef, PatchSet ps)
+      throws IOException {
+    String ref = ps.getId().toRefName();
+    if (ref.equals(origRef)) {
+      return;
+    }
+
+    createRef(repo, ps);
+    deleteRef(repo, ps, origRef);
+  }
 
   private void createRef(Repository repo, PatchSet ps) throws IOException {
     String ref = ps.getId().toRefName();