Skip to content

The Unified and Autonomous Development Platform

Fix issue #736 - Git protocol 2 support

Robin Shenf81cf4de1e47
diff --git a/server-core/src/main/java/io/onedev/server/git/GitFilter.java b/server-core/src/main/java/io/onedev/server/git/GitFilter.java
index 86383ce35e..3853b6e925 100644
--- a/server-core/src/main/java/io/onedev/server/git/GitFilter.java
+++ b/server-core/src/main/java/io/onedev/server/git/GitFilter.java
@@ -180,6 +180,8 @@ public class GitFilter implements Filter {
};
+ String protocol = request.getHeader("Git-Protocol");
+
if (upload) {
workExecutor.submit(new PrioritizedRunnable(PRIORITY) {
@@ -204,7 +206,7 @@ public class GitFilter implements Filter {
ExecutionResult result;
UploadPackCommand upload = new UploadPackCommand(gitDir, environments);
- upload.stdin(stdin).stdout(stdout).stderr(stderr).statelessRpc(true);
+ upload.stdin(stdin).stdout(stdout).stderr(stderr).statelessRpc(true).protocol(protocol);
result = upload.call();
result.setStderr(stderr.getMessage());
@@ -229,7 +231,7 @@ public class GitFilter implements Filter {
};
ReceivePackCommand receive = new ReceivePackCommand(gitDir, environments);
- receive.stdin(stdin).stdout(stdout).stderr(stderr).statelessRpc(true);
+ receive.stdin(stdin).stdout(stdout).stderr(stderr).statelessRpc(true).protocol(protocol);
ExecutionResult result = receive.call();
result.setStderr(stderr.getMessage());
result.checkReturnCode();
@@ -266,7 +268,7 @@ public class GitFilter implements Filter {
protected void processRefs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File gitDir;
boolean upload;
-
+
sessionManager.openSession();
try {
String pathInfo = request.getRequestURI().substring(request.getContextPath().length());
@@ -299,10 +301,13 @@ public class GitFilter implements Filter {
}
};
+
+ String protocolVersion = request.getHeader("Git-Protocol");
+
if (upload)
- new AdvertiseUploadRefsCommand(gitDir).output(os).call();
+ new AdvertiseUploadRefsCommand(gitDir).protocol(protocolVersion).output(os).call();
else
- new AdvertiseReceiveRefsCommand(gitDir).output(os).call();
+ new AdvertiseReceiveRefsCommand(gitDir).protocol(protocolVersion).output(os).call();
}
@Override
diff --git a/server-core/src/main/java/io/onedev/server/git/GitSshCommandCreator.java b/server-core/src/main/java/io/onedev/server/git/GitSshCommandCreator.java
index 8b3b6524e2..71869ebfb6 100644
--- a/server-core/src/main/java/io/onedev/server/git/GitSshCommandCreator.java
+++ b/server-core/src/main/java/io/onedev/server/git/GitSshCommandCreator.java
@@ -23,7 +23,8 @@ public class GitSshCommandCreator implements SshCommandCreator {
static final Logger logger = LoggerFactory.getLogger(GitSshCommandCreator.class);
@Override
- public Command createCommand(String command) {
+ public Command createCommand(String command, Map<String, String> env) {
+ String protocol = env.get("GIT_PROTOCOL");
if (command.startsWith(RemoteConfig.DEFAULT_UPLOAD_PACK)) {
return new GitSshCommand(command) {
@@ -33,6 +34,7 @@ public class GitSshCommandCreator implements SshCommandCreator {
.stdin(in)
.stdout(out)
.stderr(err)
+ .protocol(protocol)
.call();
}
@@ -54,6 +56,7 @@ public class GitSshCommandCreator implements SshCommandCreator {
.stdin(in)
.stdout(out)
.stderr(err)
+ .protocol(protocol)
.call();
}
diff --git a/server-core/src/main/java/io/onedev/server/git/command/AdvertiseReceiveRefsCommand.java b/server-core/src/main/java/io/onedev/server/git/command/AdvertiseReceiveRefsCommand.java
index 20a337555f..366dbb1648 100644
--- a/server-core/src/main/java/io/onedev/server/git/command/AdvertiseReceiveRefsCommand.java
+++ b/server-core/src/main/java/io/onedev/server/git/command/AdvertiseReceiveRefsCommand.java
@@ -3,6 +3,8 @@ package io.onedev.server.git.command;
import java.io.File;
import java.io.OutputStream;
+import javax.annotation.Nullable;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -17,6 +19,8 @@ public class AdvertiseReceiveRefsCommand extends GitCommand<Void> {
private OutputStream output;
+ private String protocol;
+
public AdvertiseReceiveRefsCommand(File gitDir) {
super(gitDir);
}
@@ -26,11 +30,20 @@ public class AdvertiseReceiveRefsCommand extends GitCommand<Void> {
return this;
}
+ public AdvertiseReceiveRefsCommand protocol(@Nullable String protocol) {
+ this.protocol = protocol;
+ return this;
+ }
+
@Override
public Void call() {
Preconditions.checkNotNull(output);
Commandline cmd = cmd();
+
+ if (protocol != null)
+ cmd.environments().put("GIT_PROTOCOL", protocol);
+
cmd.addArgs("receive-pack", "--stateless-rpc", "--advertise-refs", ".");
cmd.execute(output, new LineConsumer() {
diff --git a/server-core/src/main/java/io/onedev/server/git/command/AdvertiseUploadRefsCommand.java b/server-core/src/main/java/io/onedev/server/git/command/AdvertiseUploadRefsCommand.java
index 57f0390ca4..b3e5af35ae 100644
--- a/server-core/src/main/java/io/onedev/server/git/command/AdvertiseUploadRefsCommand.java
+++ b/server-core/src/main/java/io/onedev/server/git/command/AdvertiseUploadRefsCommand.java
@@ -17,6 +17,8 @@ public class AdvertiseUploadRefsCommand extends GitCommand<Void> {
private OutputStream output;
+ private String protocol;
+
public AdvertiseUploadRefsCommand(File gitDir) {
super(gitDir);
}
@@ -25,12 +27,21 @@ public class AdvertiseUploadRefsCommand extends GitCommand<Void> {
this.output = output;
return this;
}
+
+ public AdvertiseUploadRefsCommand protocol(String protocol) {
+ this.protocol = protocol;
+ return this;
+ }
@Override
public Void call() {
Preconditions.checkNotNull(output);
Commandline cmd = cmd();
+
+ if (protocol != null)
+ cmd.environments().put("GIT_PROTOCOL", protocol);
+
cmd.addArgs("upload-pack", "--stateless-rpc", "--advertise-refs", ".");
cmd.execute(output, new LineConsumer() {
diff --git a/server-core/src/main/java/io/onedev/server/git/command/ReceivePackCommand.java b/server-core/src/main/java/io/onedev/server/git/command/ReceivePackCommand.java
index 5dc2ef4f57..6b3c3d5e48 100644
--- a/server-core/src/main/java/io/onedev/server/git/command/ReceivePackCommand.java
+++ b/server-core/src/main/java/io/onedev/server/git/command/ReceivePackCommand.java
@@ -5,6 +5,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
+import javax.annotation.Nullable;
+
import com.google.common.base.Preconditions;
import io.onedev.commons.utils.command.Commandline;
@@ -20,6 +22,8 @@ public class ReceivePackCommand extends GitCommand<ExecutionResult> {
private boolean statelessRpc;
+ private String protocol;
+
public ReceivePackCommand(File gitDir, Map<String, String> environments) {
super(gitDir, environments);
}
@@ -44,6 +48,11 @@ public class ReceivePackCommand extends GitCommand<ExecutionResult> {
return this;
}
+ public ReceivePackCommand protocol(@Nullable String protocol) {
+ this.protocol = protocol;
+ return this;
+ }
+
@Override
public ExecutionResult call() {
Preconditions.checkNotNull(stdin);
@@ -51,6 +60,10 @@ public class ReceivePackCommand extends GitCommand<ExecutionResult> {
Preconditions.checkNotNull(stderr);
Commandline cmd = cmd();
+
+ if (protocol != null)
+ cmd.environments().put("GIT_PROTOCOL", protocol);
+
cmd.addArgs("receive-pack");
if (statelessRpc)
cmd.addArgs("--stateless-rpc");
diff --git a/server-core/src/main/java/io/onedev/server/git/command/UploadPackCommand.java b/server-core/src/main/java/io/onedev/server/git/command/UploadPackCommand.java
index 660322bae6..6e58870d63 100644
--- a/server-core/src/main/java/io/onedev/server/git/command/UploadPackCommand.java
+++ b/server-core/src/main/java/io/onedev/server/git/command/UploadPackCommand.java
@@ -5,6 +5,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
+import javax.annotation.Nullable;
+
import com.google.common.base.Preconditions;
import io.onedev.commons.utils.command.Commandline;
@@ -20,6 +22,8 @@ public class UploadPackCommand extends GitCommand<ExecutionResult> {
private boolean statelessRpc;
+ private String protocol;
+
public UploadPackCommand(File gitDir, Map<String, String> environments) {
super(gitDir, environments);
}
@@ -44,6 +48,11 @@ public class UploadPackCommand extends GitCommand<ExecutionResult> {
return this;
}
+ public UploadPackCommand protocol(@Nullable String protocol) {
+ this.protocol = protocol;
+ return this;
+ }
+
@Override
public ExecutionResult call() {
Preconditions.checkNotNull(stdin);
@@ -51,6 +60,10 @@ public class UploadPackCommand extends GitCommand<ExecutionResult> {
Preconditions.checkNotNull(stderr);
Commandline cmd = cmd();
+
+ if (protocol != null)
+ cmd.environments().put("GIT_PROTOCOL", protocol);
+
cmd.addArgs("upload-pack");
if (statelessRpc)
cmd.addArgs("--stateless-rpc");
diff --git a/server-core/src/main/java/io/onedev/server/ssh/SshCommandCreator.java b/server-core/src/main/java/io/onedev/server/ssh/SshCommandCreator.java
index 2bf64b7cc6..5f97353ac5 100644
--- a/server-core/src/main/java/io/onedev/server/ssh/SshCommandCreator.java
+++ b/server-core/src/main/java/io/onedev/server/ssh/SshCommandCreator.java
@@ -1,5 +1,7 @@
package io.onedev.server.ssh;
+import java.util.Map;
+
import javax.annotation.Nullable;
import org.apache.sshd.server.command.Command;
@@ -7,6 +9,6 @@ import org.apache.sshd.server.command.Command;
public interface SshCommandCreator {
@Nullable
- Command createCommand(String command);
+ Command createCommand(String command, Map<String, String> env);
}
diff --git a/server-core/src/main/java/io/onedev/server/ssh/SshServerLauncher.java b/server-core/src/main/java/io/onedev/server/ssh/SshServerLauncher.java
index 84b31657a7..38dafe5cc4 100644
--- a/server-core/src/main/java/io/onedev/server/ssh/SshServerLauncher.java
+++ b/server-core/src/main/java/io/onedev/server/ssh/SshServerLauncher.java
@@ -53,7 +53,7 @@ public class SshServerLauncher {
server.setCommandFactory((channel, command) -> {
for (SshCommandCreator creator: commandCreators) {
- Command sshCommand = creator.createCommand(command);
+ Command sshCommand = creator.createCommand(command, channel.getEnvironment().getEnv());
if (sshCommand != null)
return sshCommand;
}