repo: include patchset number in commands

Since we don't include the ps number, repo will look up the latest
one and use that.  If the user is viewing an older ps, that means
the repo command won't actually download the version they have
selected which can be quite confusing.

The git version already includes the fully change id + ps num, so
this brings repo up to parity.

Change-Id: I6a4cd56cf9960d626770f98837028119aeaed4d4
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/BranchCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/BranchCommand.java
index fd36718..6592d5d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/BranchCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/BranchCommand.java
@@ -43,12 +43,13 @@
   }
 
   @Override
-  String getRepoCommand(String url, String id) {
+  String getRepoCommand(String url, String id, String ps) {
     return "repo download -b change-"
         + id.replaceAll("/", "-")
         + " "
         + QuoteUtil.quote(url)
         + " "
-        + id;
+        + id
+        + (ps.isEmpty() ? "" : "/" + ps);
   }
 }
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 bc3ede6..9e36970 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
@@ -37,7 +37,8 @@
   }
 
   @Override
-  String getRepoCommand(String url, String id) {
-    return "repo download " + QuoteUtil.quote(url) + " " + id;
+  String getRepoCommand(String url, String id, String ps) {
+    return "repo download " + QuoteUtil.quote(url) + " " + id
+        + (ps.isEmpty() ? "" : "/" + ps);
   }
 }
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 4856e83..d5a85fe 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
@@ -37,7 +37,8 @@
   }
 
   @Override
-  String getRepoCommand(String url, String id) {
-    return "repo download -c " + QuoteUtil.quote(url) + " " + id;
+  String getRepoCommand(String url, String id, String ps) {
+    return "repo download -c " + QuoteUtil.quote(url) + " " + id
+        + (ps.isEmpty() ? "" : "/" + ps);
   }
 }
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 e106510..1da40ac 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
@@ -14,8 +14,10 @@
 
 package com.googlesource.gerrit.plugins.download.command;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.entities.Change;
+import com.google.gerrit.entities.PatchSet;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.entities.RefNames;
 import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
@@ -74,7 +76,10 @@
       }
 
       if (scheme instanceof RepoScheme) {
-        return getRepoCommand(url, id);
+        PatchSet.Id psIdFromRef = PatchSet.Id.fromRef(ref);
+        checkNotNull(psIdFromRef);
+        String ps = psIdFromRef.getId();
+        return getRepoCommand(url, id, ps);
       }
       if (isValidUrl(url)) {
         if (checkForHiddenChangeRefs) {
@@ -137,9 +142,10 @@
 
   /**
    * @param url The project URL this change is for.
-   * @param id The change/PS numbers.
+   * @param id The change number.
+   * @param ps The patchset (PS) number.
    */
-  String getRepoCommand(String url, String id) {
+  String getRepoCommand(String url, String id, String ps) {
     // Most commands don't support this, so default it to nothing.
     return null;
   }