The Unified and Autonomous Development Platform
fix: Preserve '+' in git receive-hook ref names
Robin Shend85fca1cf00d
diff --git a/server-core/src/main/java/io/onedev/server/git/hook/GitPostReceiveCallback.java b/server-core/src/main/java/io/onedev/server/git/hook/GitPostReceiveCallback.javaindex f7ae743d2f..6ea680b1a7 100644--- a/server-core/src/main/java/io/onedev/server/git/hook/GitPostReceiveCallback.java+++ b/server-core/src/main/java/io/onedev/server/git/hook/GitPostReceiveCallback.java@@ -5,7 +5,6 @@ import static io.onedev.server.security.SecurityUtils.asSubject;import java.io.IOException;import java.util.ArrayList;-import java.util.Enumeration;import java.util.List;import javax.inject.Inject;@@ -83,23 +82,14 @@ public class GitPostReceiveCallback extends HttpServlet {ThreadContext.bind(asSubject(asPrincipals(principal)));- String refUpdateInfo = null;- Enumeration<String> paramNames = request.getParameterNames();- while (paramNames.hasMoreElements()) {- String paramName = paramNames.nextElement();- if (paramName.contains(" ")) {- refUpdateInfo = paramName;- }- }+ String refUpdateInfo = request.getParameter(HookUtils.PARAM_REF_UPDATES);Preconditions.checkState(refUpdateInfo != null, "Git ref update information is not available");Output output = new Output(response.getOutputStream());/*- * If multiple refs are updated, the hook stdin will put each ref update info into- * a separate line, however the line breaks is omitted when forward the hook stdin- * to curl via "@-", below logic is used to parse these info correctly even- * without line breaks.+ * If multiple refs are updated, the hook stdin puts each ref update on a separate+ * line. Parse correctly even if those line breaks are missing.*/refUpdateInfo = StringUtils.reverse(StringUtils.remove(refUpdateInfo, '\n'));diff --git a/server-core/src/main/java/io/onedev/server/git/hook/GitPreReceiveCallback.java b/server-core/src/main/java/io/onedev/server/git/hook/GitPreReceiveCallback.javaindex 444ec3984f..285cb00227 100644--- a/server-core/src/main/java/io/onedev/server/git/hook/GitPreReceiveCallback.java+++ b/server-core/src/main/java/io/onedev/server/git/hook/GitPreReceiveCallback.java@@ -91,7 +91,7 @@ public class GitPreReceiveCallback extends HttpServlet {ThreadContext.bind(asSubject(asPrincipals(principal)));Project project = projectService.load(Long.valueOf(fields.get(0)));- String refUpdateInfo = null;+ String refUpdateInfo = request.getParameter(HookUtils.PARAM_REF_UPDATES);/** Since git 2.11, pushed commits will be placed in to a QUARANTINE directory when pre-receive hook@@ -102,9 +102,7 @@ public class GitPreReceiveCallback extends HttpServlet {Enumeration<String> paramNames = request.getParameterNames();while (paramNames.hasMoreElements()) {String paramName = paramNames.nextElement();- if (paramName.contains(" ")) {- refUpdateInfo = paramName;- } else if (paramName.startsWith("ENV_")) {+ if (paramName.startsWith("ENV_")) {String paramValue = request.getParameter(paramName);if (StringUtils.isNotBlank(paramValue))gitEnvs.put(paramName.substring("ENV_".length()), paramValue);@@ -116,10 +114,8 @@ public class GitPreReceiveCallback extends HttpServlet {Output output = new Output(response.getOutputStream());/*- * If multiple refs are updated, the hook stdin will put each ref update info into- * a separate line, however the line breaks is omitted when forward the hook stdin- * to curl via "@-", below logic is used to parse these info correctly even- * without line breaks.+ * If multiple refs are updated, the hook stdin puts each ref update on a separate+ * line. Parse correctly even if those line breaks are missing.*/refUpdateInfo = StringUtils.reverse(StringUtils.remove(refUpdateInfo, '\n'));fields = StringUtils.splitAndTrim(refUpdateInfo, " ");diff --git a/server-core/src/main/java/io/onedev/server/git/hook/HookUtils.java b/server-core/src/main/java/io/onedev/server/git/hook/HookUtils.javaindex ba07a46ce9..a942a6255c 100644--- a/server-core/src/main/java/io/onedev/server/git/hook/HookUtils.java+++ b/server-core/src/main/java/io/onedev/server/git/hook/HookUtils.java@@ -18,7 +18,9 @@ import java.util.Map;public class HookUtils {- public static final String RECEIVE_HOOK_TOKEN = CryptoUtils.generateSecret();+ public static final String RECEIVE_HOOK_TOKEN = CryptoUtils.generateSecret();++ public static final String PARAM_REF_UPDATES = "REF_UPDATES";private static final String gitReceiveHook;@@ -61,37 +63,24 @@ public class HookUtils {return envs;}- public static boolean isReceiveHookValid(File gitDir, String hookName) {- File hookFile = new File(gitDir, "hooks/" + hookName);- if (!hookFile.exists())- return false;-- try {- String content = FileUtils.readFileToString(hookFile, Charset.defaultCharset());- if (!content.contains("ONEDEV_HOOK_TOKEN"))- return false;- } catch (IOException e) {- throw new RuntimeException(e);- }-- if (!hookFile.canExecute())- return false;-- return true;- }-public static void checkReceiveHooks(File gitDir) {- if (!isReceiveHookValid(gitDir, "pre-receive") || !isReceiveHookValid(gitDir, "post-receive")) {- File hooksDir = new File(gitDir, "hooks");+ File hooksDir = new File(gitDir, "hooks");+ installReceiveHook(new File(hooksDir, "pre-receive"), "git-prereceive-callback");+ installReceiveHook(new File(hooksDir, "post-receive"), "git-postreceive-callback");+ }- File gitPreReceiveHookFile = new File(hooksDir, "pre-receive");- FileUtils.writeFile(gitPreReceiveHookFile, String.format(gitReceiveHook, "git-prereceive-callback"));- gitPreReceiveHookFile.setExecutable(true);-- File gitPostReceiveHookFile = new File(hooksDir, "post-receive");- FileUtils.writeFile(gitPostReceiveHookFile, String.format(gitReceiveHook, "git-postreceive-callback"));- gitPostReceiveHookFile.setExecutable(true);- }+ private static void installReceiveHook(File hookFile, String callback) {+ String content = String.format(gitReceiveHook, callback);+ if (hookFile.exists() && hookFile.canExecute()) {+ try {+ if (content.equals(FileUtils.readFileToString(hookFile, Charset.defaultCharset())))+ return;+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ FileUtils.writeFile(hookFile, content);+ hookFile.setExecutable(true);}}diff --git a/server-core/src/main/resources/git-receive-hook b/server-core/src/main/resources/git-receive-hookindex 974738d1aa..5f2085f23c 100644--- a/server-core/src/main/resources/git-receive-hook+++ b/server-core/src/main/resources/git-receive-hook@@ -2,7 +2,7 @@unset http_proxyunset https_proxyIFS=$'\r\n';-lines=($(${ONEDEV_CURL} -k -s -S -f -X POST --data-urlencode "ENV_GIT_ALTERNATE_OBJECT_DIRECTORIES=${GIT_ALTERNATE_OBJECT_DIRECTORIES}" --data-urlencode "ENV_GIT_OBJECT_DIRECTORY=${GIT_OBJECT_DIRECTORY}" --data-urlencode "ENV_GIT_QUARANTINE_PATH=${GIT_QUARANTINE_PATH}" -d @- ${ONEDEV_URL}/%s/${ONEDEV_REPOSITORY_ID}/${ONEDEV_USER_ID}/${ONEDEV_HOOK_TOKEN} 2>&1))+lines=($(${ONEDEV_CURL} -k -s -S -f -X POST --data-urlencode "ENV_GIT_ALTERNATE_OBJECT_DIRECTORIES=${GIT_ALTERNATE_OBJECT_DIRECTORIES}" --data-urlencode "ENV_GIT_OBJECT_DIRECTORY=${GIT_OBJECT_DIRECTORY}" --data-urlencode "ENV_GIT_QUARANTINE_PATH=${GIT_QUARANTINE_PATH}" --data-urlencode "REF_UPDATES@-" ${ONEDEV_URL}/%s/${ONEDEV_REPOSITORY_ID}/${ONEDEV_USER_ID}/${ONEDEV_HOOK_TOKEN} 2>&1))returnCode=0;