Consistently use `git rev-parse` to find hooks-dir

Change-Id: I2ce560a2d9d4492e0c9352d723d6cd205815c5cb
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
index ccdfeda..76d4166 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
@@ -29,11 +29,11 @@
 
 public class CloneWithCommitMsgHook extends CloneCommand {
   @VisibleForTesting protected static final String HOOK_COMMAND_KEY = "installCommitMsgHookCommand";
+  @VisibleForTesting protected static final String HOOKS_DIR = "`git rev-parse --git-dir`/hooks/";
   @VisibleForTesting protected static final String EXTRA_COMMAND_KEY = "installCommitExtraCommand";
 
   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
-  private static final String HOOK = "hooks/commit-msg";
-  private static final String TARGET = " `git rev-parse --git-dir`/";
+  private static final String HOOK = "commit-msg";
 
   private final String configCommand;
   private final String extraCommand;
@@ -51,7 +51,6 @@
   @Override
   public String getCommand(DownloadScheme scheme, String project) {
     String projectName = getBaseName(project);
-
     StringBuilder command = null;
 
     if (configCommand != null) {
@@ -71,14 +70,15 @@
               .append(super.getCommand(scheme, project))
               .append(" && (cd ")
               .append(QuoteUtil.quote(projectName))
-              .append(" && mkdir -p .git/hooks")
-              .append(" && curl -Lo")
-              .append(TARGET)
+              .append(" && mkdir -p ")
+              .append(HOOKS_DIR)
+              .append(" && curl -Lo ")
+              .append(HOOKS_DIR)
               .append(HOOK)
               .append(" ")
               .append(getHookUrl())
-              .append("; chmod +x")
-              .append(TARGET)
+              .append("; chmod +x ")
+              .append(HOOKS_DIR)
               .append(HOOK)
               .append(")");
     }
@@ -101,7 +101,7 @@
       if (!canonicalWebUrl.endsWith("/")) {
         hookUrl.append("/");
       }
-      hookUrl.append("tools/").append(HOOK);
+      hookUrl.append("tools/hooks/").append(HOOK);
     } else {
       logger.atWarning().log(
           "Cannot add commit-msg hook URL since gerrit.canonicalWebUrl isn't configured.");
diff --git a/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java b/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java
index 3d1f810..d4ca37c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java
@@ -15,6 +15,7 @@
 
 import static com.google.common.truth.Truth.assertThat;
 import static com.googlesource.gerrit.plugins.download.command.CloneWithCommitMsgHook.EXTRA_COMMAND_KEY;
+import static com.googlesource.gerrit.plugins.download.command.CloneWithCommitMsgHook.HOOKS_DIR;
 import static com.googlesource.gerrit.plugins.download.command.CloneWithCommitMsgHook.HOOK_COMMAND_KEY;
 
 import com.googlesource.gerrit.plugins.download.DownloadCommandTest;
@@ -116,8 +117,8 @@
 
   private String getDefaultHookCommand() {
     return String.format(
-        "(cd %s && mkdir -p .git/hooks && curl -Lo `git rev-parse --git-dir`/hooks/commit-msg https://%s/tools/hooks/commit-msg; chmod +x `git rev-parse --git-dir`/hooks/commit-msg)",
-        baseName(ENV.projectName), ENV.fqdn);
+        "(cd %s && mkdir -p %s && curl -Lo %scommit-msg https://%s/tools/hooks/commit-msg; chmod +x %scommit-msg)",
+        baseName(ENV.projectName), HOOKS_DIR, HOOKS_DIR, ENV.fqdn, HOOKS_DIR);
   }
 
   private CloneCommand getCloneCommand(String hookCommand, String extraCommand) {