Browse Source

Add protocol v2 support in "jgit daemon"

With this patch, a server spawned by "jgit daemon" can be accessed using
protocol v2 from a Git client that supports it (for example, "git" with
the appropriate patches). This is only activated if the repository's
config has "protocol.version" be 2.

This required a change to the package-private #execute methods in
DaemonService to allow passing of extra parameters.

This has been tested with a patched Git.

Change-Id: Icf043efec7ce956d72b075fc6dc7a87d5a2da82a
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
stable-5.1
Jonathan Tan 7 years ago committed by Jonathan Nieder
parent
commit
2841bab938
  1. 13
      org.eclipse.jgit/src/org/eclipse/jgit/transport/Daemon.java
  2. 12
      org.eclipse.jgit/src/org/eclipse/jgit/transport/DaemonClient.java
  3. 10
      org.eclipse.jgit/src/org/eclipse/jgit/transport/DaemonService.java

13
org.eclipse.jgit/src/org/eclipse/jgit/transport/Daemon.java

@ -53,7 +53,9 @@ import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.SocketException; import java.net.SocketException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Collection;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
@ -153,12 +155,17 @@ public class Daemon {
@Override @Override
protected void execute(final DaemonClient dc, protected void execute(final DaemonClient dc,
final Repository db) throws IOException, final Repository db,
@Nullable Collection<String> extraParameters)
throws IOException,
ServiceNotEnabledException, ServiceNotEnabledException,
ServiceNotAuthorizedException { ServiceNotAuthorizedException {
UploadPack up = uploadPackFactory.create(dc, db); UploadPack up = uploadPackFactory.create(dc, db);
InputStream in = dc.getInputStream(); InputStream in = dc.getInputStream();
OutputStream out = dc.getOutputStream(); OutputStream out = dc.getOutputStream();
if (extraParameters != null) {
up.setExtraParameters(extraParameters);
}
up.upload(in, out, null); up.upload(in, out, null);
} }
}, new DaemonService("receive-pack", "receivepack") { //$NON-NLS-1$ //$NON-NLS-2$ }, new DaemonService("receive-pack", "receivepack") { //$NON-NLS-1$ //$NON-NLS-2$
@ -168,7 +175,9 @@ public class Daemon {
@Override @Override
protected void execute(final DaemonClient dc, protected void execute(final DaemonClient dc,
final Repository db) throws IOException, final Repository db,
@Nullable Collection<String> extraParameters)
throws IOException,
ServiceNotEnabledException, ServiceNotEnabledException,
ServiceNotAuthorizedException { ServiceNotAuthorizedException {
ReceivePack rp = receivePackFactory.create(dc, db); ReceivePack rp = receivePackFactory.create(dc, db);

12
org.eclipse.jgit/src/org/eclipse/jgit/transport/DaemonClient.java

@ -50,6 +50,8 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.util.Arrays;
import java.util.Collection;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException; import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException; import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
@ -118,6 +120,14 @@ public class DaemonClient {
if (0 < daemon.getTimeout()) if (0 < daemon.getTimeout())
sock.setSoTimeout(daemon.getTimeout() * 1000); sock.setSoTimeout(daemon.getTimeout() * 1000);
String cmd = new PacketLineIn(rawIn).readStringRaw(); String cmd = new PacketLineIn(rawIn).readStringRaw();
Collection<String> extraParameters = null;
int nulnul = cmd.indexOf("\0\0"); //$NON-NLS-1$
if (nulnul != -1) {
extraParameters = Arrays.asList(cmd.substring(nulnul + 2).split("\0")); //$NON-NLS-1$
}
final int nul = cmd.indexOf('\0'); final int nul = cmd.indexOf('\0');
if (nul >= 0) { if (nul >= 0) {
// Newer clients hide a "host" header behind this byte. // Newer clients hide a "host" header behind this byte.
@ -131,6 +141,6 @@ public class DaemonClient {
if (srv == null) if (srv == null)
return; return;
sock.setSoTimeout(0); sock.setSoTimeout(0);
srv.execute(this, cmd); srv.execute(this, cmd, extraParameters);
} }
} }

10
org.eclipse.jgit/src/org/eclipse/jgit/transport/DaemonService.java

@ -45,7 +45,9 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Config.SectionParser; import org.eclipse.jgit.lib.Config.SectionParser;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
@ -146,13 +148,14 @@ public abstract class DaemonService {
&& commandLine.startsWith(command); && commandLine.startsWith(command);
} }
void execute(final DaemonClient client, final String commandLine) void execute(DaemonClient client, String commandLine,
@Nullable Collection<String> extraParameters)
throws IOException, ServiceNotEnabledException, throws IOException, ServiceNotEnabledException,
ServiceNotAuthorizedException { ServiceNotAuthorizedException {
final String name = commandLine.substring(command.length() + 1); final String name = commandLine.substring(command.length() + 1);
try (Repository db = client.getDaemon().openRepository(client, name)) { try (Repository db = client.getDaemon().openRepository(client, name)) {
if (isEnabledFor(db)) { if (isEnabledFor(db)) {
execute(client, db); execute(client, db, extraParameters);
} }
} catch (ServiceMayNotContinueException e) { } catch (ServiceMayNotContinueException e) {
// An error when opening the repo means the client is expecting a ref // An error when opening the repo means the client is expecting a ref
@ -168,7 +171,8 @@ public abstract class DaemonService {
return isEnabled(); return isEnabled();
} }
abstract void execute(DaemonClient client, Repository db) abstract void execute(DaemonClient client, Repository db,
@Nullable Collection<String> extraParameters)
throws IOException, ServiceNotEnabledException, throws IOException, ServiceNotEnabledException,
ServiceNotAuthorizedException; ServiceNotAuthorizedException;
} }

Loading…
Cancel
Save