Browse Source

Allow PreUploadHook.onSendPack to send messages to the client

Before transmitting to the client a hook may want to send along
a text message ahead of the pack, such as a "message of the day".
Enable this usage by mirroring the message sending API from
ReceivePack on the UploadPack instance, using the side band.

Change-Id: I31cd254a4ddb816641397a3e9c2c20212471c37f
tags/v3.1.0.201309270735-rc1
Shawn Pearce 11 years ago
parent
commit
6e896ba66b
1 changed files with 30 additions and 2 deletions
  1. 30
    2
      org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

+ 30
- 2
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java View File

import org.eclipse.jgit.storage.pack.PackConfig; import org.eclipse.jgit.storage.pack.PackConfig;
import org.eclipse.jgit.transport.BasePackFetchConnection.MultiAck; import org.eclipse.jgit.transport.BasePackFetchConnection.MultiAck;
import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser; import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import org.eclipse.jgit.util.io.InterruptTimer; import org.eclipse.jgit.util.io.InterruptTimer;
import org.eclipse.jgit.util.io.TimeoutInputStream; import org.eclipse.jgit.util.io.TimeoutInputStream;
import org.eclipse.jgit.util.io.TimeoutOutputStream; import org.eclipse.jgit.util.io.TimeoutOutputStream;


private PacketLineOut pckOut; private PacketLineOut pckOut;


private OutputStream msgOut = DisabledOutputStream.INSTANCE;

/** The refs we advertised as existing at the start of the connection. */ /** The refs we advertised as existing at the start of the connection. */
private Map<String, Ref> refs; private Map<String, Ref> refs;


try { try {
rawIn = input; rawIn = input;
rawOut = output; rawOut = output;
if (messages != null)
msgOut = messages;


if (timeout > 0) { if (timeout > 0) {
final Thread caller = Thread.currentThread(); final Thread caller = Thread.currentThread();
pckOut = new PacketLineOut(rawOut); pckOut = new PacketLineOut(rawOut);
service(); service();
} finally { } finally {
msgOut = DisabledOutputStream.INSTANCE;
walk.release(); walk.release();
if (timer != null) { if (timer != null) {
try { try {
adv.end(); adv.end();
} }


/**
* Send a message to the client, if it supports receiving them.
* <p>
* If the client doesn't support receiving messages, the message will be
* discarded, with no other indication to the caller or to the client.
*
* @param what
* string describing the problem identified by the hook. The
* string must not end with an LF, and must not contain an LF.
*/
public void sendMessage(String what) {
try {
msgOut.write(Constants.encode(what + "\n")); //$NON-NLS-1$
} catch (IOException e) {
// Ignore write failures.
}
}

/** @return an underlying stream for sending messages to the client, or null. */
public OutputStream getMessageOutputStream() {
return msgOut;
}

private void recvWants() throws IOException { private void recvWants() throws IOException {
boolean isFirst = true; boolean isFirst = true;
for (;;) { for (;;) {
private void sendPack(final boolean sideband) throws IOException { private void sendPack(final boolean sideband) throws IOException {
ProgressMonitor pm = NullProgressMonitor.INSTANCE; ProgressMonitor pm = NullProgressMonitor.INSTANCE;
OutputStream packOut = rawOut; OutputStream packOut = rawOut;
SideBandOutputStream msgOut = null;


if (sideband) { if (sideband) {
int bufsz = SideBandOutputStream.SMALL_BUF; int bufsz = SideBandOutputStream.SMALL_BUF;
pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut); pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
statistics = pw.getStatistics(); statistics = pw.getStatistics();


if (msgOut != null) {
if (msgOut != DisabledOutputStream.INSTANCE) {
String msg = pw.getStatistics().getMessage() + '\n'; String msg = pw.getStatistics().getMessage() + '\n';
msgOut.write(Constants.encode(msg)); msgOut.write(Constants.encode(msg));
msgOut.flush(); msgOut.flush();

Loading…
Cancel
Save