From 49129230a142a876689a0a98c8a78dfba03d18c1 Mon Sep 17 00:00:00 2001 From: Wendy Wang Date: Fri, 11 Mar 2022 12:45:06 +0100 Subject: Show HTTP(S) message in Missing Change ID Error message The assumption that the existence of the SSH host key means that all users use SSH protocol is incorrect, since some might use HTTP protocol. Since a CommitValidationListener doesn't know which protocol was used, provide both SSH and HTTP variant of the command to install the commit message hook. Release-Notes: skip Change-Id: I10ae721315de8afb964e720033e2d6498bce3e55 --- .../server/git/validators/CommitValidators.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/java/com/google/gerrit/server/git/validators/CommitValidators.java b/java/com/google/gerrit/server/git/validators/CommitValidators.java index c67df8be90..f84b696d04 100644 --- a/java/com/google/gerrit/server/git/validators/CommitValidators.java +++ b/java/com/google/gerrit/server/git/validators/CommitValidators.java @@ -266,6 +266,12 @@ public class CommitValidators { "multiple Change-Id lines in message footer"; private static final String INVALID_CHANGE_ID_MSG = "invalid Change-Id line format in message footer"; + private static final String HTTP_INSTALL_HOOK = + "f=\"$(git rev-parse --git-dir)/hooks/commit-msg\"; curl -o \"$f\"" + + " %stools/hooks/commit-msg ; chmod +x \"$f\""; + private static final String SSH_INSTALL_HOOK = + "gitdir=$(git rev-parse --git-dir); scp -p -P %d %s@%s:hooks/commit-msg ${gitdir}/hooks/"; + private static final String CHANGE_ID_MISSING_INSTALL_HOOKS = " %s\nor, for http(s):\n %s"; @VisibleForTesting public static final String CHANGE_ID_MISMATCH_MSG = @@ -372,14 +378,15 @@ public class CommitValidators { // If there are no SSH keys, the commit-msg hook must be installed via // HTTP(S) Optional webUrl = urlFormatter.getWebUrl(); + + String httpHook = String.format(HTTP_INSTALL_HOOK, webUrl.get()); + if (hostKeys.isEmpty()) { checkState(webUrl.isPresent()); - return String.format( - " f=\"$(git rev-parse --git-dir)/hooks/commit-msg\"; curl -o \"$f\" %stools/hooks/commit-msg ; chmod +x \"$f\"", - webUrl.get()); + return httpHook; } - // SSH keys exist, so the hook can be installed with scp. + // SSH keys exist, so the hook might be able to be installed with scp. String sshHost; int sshPort; String host = hostKeys.get(0).getHost(); @@ -397,9 +404,10 @@ public class CommitValidators { sshPort = 22; } - return String.format( - " gitdir=$(git rev-parse --git-dir); scp -p -P %d %s@%s:hooks/commit-msg ${gitdir}/hooks/", - sshPort, user.getUserName().orElse(""), sshHost); + String sshHook = + String.format( + SSH_INSTALL_HOOK, sshPort, user.getUserName().orElse(""), sshHost); + return String.format(CHANGE_ID_MISSING_INSTALL_HOOKS, sshHook, httpHook); } } -- cgit v1.2.3