Extract the git fetch logic into own import step

Change-Id: I542fcd7e824a610e0b8dadd279a1ddc4a965c26f
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/GitFetchStep.java b/src/main/java/com/googlesource/gerrit/plugins/importer/GitFetchStep.java
new file mode 100644
index 0000000..2ca637e
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/GitFetchStep.java
@@ -0,0 +1,46 @@
+//Copyright (C) 2015 The Android Open Source Project
+//
+//Licensed under the Apache License, Version 2.0 (the "License");
+//you may not use this file except in compliance with the License.
+//You may obtain a copy of the License at
+//
+//http://www.apache.org/licenses/LICENSE-2.0
+//
+//Unless required by applicable law or agreed to in writing, software
+//distributed under the License is distributed on an "AS IS" BASIS,
+//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//See the License for the specific language governing permissions and
+//limitations under the License.
+
+package com.googlesource.gerrit.plugins.importer;
+
+import static java.lang.String.format;
+
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.inject.Singleton;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.api.errors.InvalidRemoteException;
+import org.eclipse.jgit.api.errors.TransportException;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.transport.CredentialsProvider;
+import org.eclipse.jgit.transport.FetchResult;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+
+@Singleton
+class GitFetchStep {
+
+  void fetch(String user, String password, Repository repo,
+      Project.NameKey name, StringBuffer out)
+      throws InvalidRemoteException, TransportException, GitAPIException {
+    CredentialsProvider cp =
+        new UsernamePasswordCredentialsProvider(user, password);
+    FetchResult fetchResult = Git.wrap(repo).fetch()
+          .setCredentialsProvider(cp)
+          .setRemote("origin")
+          .call();
+    out.append(format("[INFO] Project '%s' fetched: %s",
+        name.get(), fetchResult.getMessages()));
+  }
+}
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 578884f..7dba0f3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProjectTask.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProjectTask.java
@@ -71,7 +71,6 @@
 import com.google.inject.Provider;
 import com.google.inject.assistedinject.Assisted;
 
-import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.internal.storage.file.LockFile;
 import org.eclipse.jgit.lib.Constants;
@@ -80,9 +79,6 @@
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.jgit.transport.CredentialsProvider;
-import org.eclipse.jgit.transport.FetchResult;
-import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 import org.eclipse.jgit.util.FS;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -115,6 +111,7 @@
 
   private final OpenRepositoryStep openRepoStep;
   private final ConfigureRepositoryStep configRepoStep;
+  private final GitFetchStep gitFetchStep;
   private final File lockRoot;
   private final ReviewDb db;
   private final AccountCache accountCache;
@@ -142,6 +139,7 @@
   @Inject
   ImportProjectTask(OpenRepositoryStep openRepoStep,
       ConfigureRepositoryStep configRepoStep,
+      GitFetchStep gitFetchStep,
       @PluginData File data,
       ReviewDb db,
       AccountCache accountCache,
@@ -163,6 +161,7 @@
       @Assisted StringBuffer messages) {
     this.openRepoStep = openRepoStep;
     this.configRepoStep = configRepoStep;
+    this.gitFetchStep = gitFetchStep;
     this.lockRoot = data;
     this.db = db;
     this.accountCache = accountCache;
@@ -201,7 +200,7 @@
 
       try {
         configRepoStep.configure(repo, name, fromGerrit);
-        gitFetch();
+        gitFetchStep.fetch(user, password, repo, name, messages);
         replayChanges();
       } catch (IOException | GitAPIException | OrmException
           | NoSuchAccountException | NoSuchChangeException | RestApiException
@@ -239,17 +238,6 @@
     }
   }
 
-  private void gitFetch() throws GitAPIException {
-    CredentialsProvider cp =
-        new UsernamePasswordCredentialsProvider(user, password);
-    FetchResult fetchResult = Git.wrap(repo).fetch()
-          .setCredentialsProvider(cp)
-          .setRemote("origin")
-          .call();
-    messages.append(format("[INFO] Project '%s' fetched: %s",
-        name.get(), fetchResult.getMessages()));
-  }
-
   private void replayChanges() throws IOException, OrmException,
       NoSuchAccountException, NoSuchChangeException, RestApiException,
       ValidationException {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java b/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java
index 9685d80..04f7bf8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java
@@ -42,5 +42,6 @@
     install(new FactoryModuleBuilder().build(ImportProjectTask.Factory.class));
     bind(OpenRepositoryStep.class);
     bind(ConfigureRepositoryStep.class);
+    bind(GitFetchStep.class);
   }
 }