Refactor repo commands

We've had a single "repo command" stuffed in the download UI which
doesn't make a lot of sense.  The command types are more about how
they mutate your local checkout, not the commands used to download.

Lets merge the repo-vs-git logic into GitDownloadCommand directly.
We create separate "getRepoCommand" and "getCommand" methods for
downstream classes to extend with the default "getRepoCommand"
defaulting to null.

Bug: Issue 11594
Change-Id: I6848c431975ec4db3aa09a160af5198b93ce5bb9
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java
index 32ebb3d..34ca19c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java
@@ -35,4 +35,9 @@
   String getCommand(String url, String ref) {
     return "git fetch " + QuoteUtil.quote(url) + " " + ref + " && git checkout FETCH_HEAD";
   }
+
+  @Override
+  String getRepoCommand(String url, String id) {
+    return "repo download " + QuoteUtil.quote(url) + " " + id;
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java
index 3aea2ee..0e964be 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java
@@ -35,4 +35,9 @@
   String getCommand(String url, String ref) {
     return "git fetch " + QuoteUtil.quote(url) + " " + ref + " && git cherry-pick FETCH_HEAD";
   }
+
+  @Override
+  String getRepoCommand(String url, String id) {
+    return "repo download -c " + QuoteUtil.quote(url) + " " + id;
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/DownloadCommandsModule.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/DownloadCommandsModule.java
index c2a4289..7ab47f2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/DownloadCommandsModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/DownloadCommandsModule.java
@@ -32,7 +32,5 @@
         .to(FormatPatchCommand.class);
 
     bind(DownloadCommand.class).annotatedWith(Exports.named("Pull")).to(PullCommand.class);
-
-    bind(DownloadCommand.class).annotatedWith(Exports.named("repo")).to(RepoCommand.class);
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
index 61bc11f..62f01a3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
@@ -60,24 +60,27 @@
 
   @Override
   public final String getCommand(DownloadScheme scheme, String project, String ref) {
-    if (commandAllowed && isRecognizedScheme(scheme)) {
+    if (commandAllowed) {
       String url = scheme.getUrl(project);
-      if (url != null && isValidUrl(url)) {
-        if (checkForHiddenChangeRefs) {
-          ref = resolveRef(project, ref);
+      if (scheme instanceof RepoScheme) {
+        String id = refToId(ref);
+        if (id != null) {
+          return getRepoCommand(url, id);
         }
-        if (ref != null) {
-          return getCommand(url, ref);
+      } else {
+        if (url != null && isValidUrl(url)) {
+          if (checkForHiddenChangeRefs) {
+            ref = resolveRef(project, ref);
+          }
+          if (ref != null) {
+            return getCommand(url, ref);
+          }
         }
       }
     }
     return null;
   }
 
-  private static boolean isRecognizedScheme(DownloadScheme scheme) {
-    return !(scheme instanceof RepoScheme);
-  }
-
   private static boolean isValidUrl(String url) {
     try {
       new URIish(url);
@@ -87,6 +90,19 @@
     }
   }
 
+  private static String refToId(String ref) {
+    if (ref.startsWith(RefNames.REFS_CHANGES)) {
+      int s1 = ref.lastIndexOf('/');
+      if (s1 > 0) {
+        int s2 = ref.lastIndexOf('/', s1 - 1);
+        if (s2 > 0) {
+          return ref.substring(s2 + 1);
+        }
+      }
+    }
+    return null;
+  }
+
   private String resolveRef(String project, String ref) {
     if (project.startsWith("$") || ref.startsWith("$")) {
       // No real value but placeholders are being used.
@@ -119,4 +135,9 @@
   }
 
   abstract String getCommand(String url, String ref);
+
+  // Most commands don't support this, so default it to nothing.
+  String getRepoCommand(String url, String id) {
+    return null;
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java
deleted file mode 100644
index 430dc7b..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright (C) 2013 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.download.command;
-
-import com.google.gerrit.entities.RefNames;
-import com.google.gerrit.extensions.config.DownloadCommand;
-import com.google.gerrit.extensions.config.DownloadScheme;
-import com.googlesource.gerrit.plugins.download.scheme.RepoScheme;
-
-public class RepoCommand extends DownloadCommand {
-  @Override
-  public String getCommand(DownloadScheme scheme, String project, String ref) {
-    if (scheme instanceof RepoScheme) {
-      String id = trim(ref);
-      if (id != null) {
-        return "repo download " + QuoteUtil.quote(scheme.getUrl(project)) + " " + id;
-      }
-    }
-    return null;
-  }
-
-  private static String trim(String ref) {
-    if (ref.startsWith(RefNames.REFS_CHANGES)) {
-      int s1 = ref.lastIndexOf('/');
-      if (s1 > 0) {
-        int s2 = ref.lastIndexOf('/', s1 - 1);
-        if (s2 > 0) {
-          return ref.substring(s2 + 1);
-        }
-      }
-    }
-    return null;
-  }
-}