transport: Consolidate status reporting code

BaseReceivePack#sendStatusReport anyway needs to know
CAPABILITY_REPORT_STATUS. By moving this flag to BaseReceivePack,
simplify the status reporting code.

Change-Id: Iaa0878b1fc13057b687a7f01d25c85fd78c0423e
Signed-off-by: Masaya Suzuki <masayasuzuki@google.com>
This commit is contained in:
Masaya Suzuki 2019-11-07 18:52:17 -08:00
parent a91489f4a8
commit 4fcc43a1de
1 changed files with 96 additions and 96 deletions

View File

@ -1853,23 +1853,28 @@ private void executeCommands() {
/** /**
* Send a status report. * Send a status report.
* *
* @param forClient
* true if this report is for a Git client, false if it is for an
* end-user.
* @param unpackError * @param unpackError
* an error that occurred during unpacking, or {@code null} * an error that occurred during unpacking, or {@code null}
* @param out
* the reporter for sending the status strings.
* @throws java.io.IOException * @throws java.io.IOException
* an error occurred writing the status report. * an error occurred writing the status report.
* @since 5.6 * @since 5.6
*/ */
private void sendStatusReport(final boolean forClient, private void sendStatusReport(Throwable unpackError) throws IOException {
final Throwable unpackError, final Reporter out) Reporter out = new Reporter() {
throws IOException { @Override
void sendString(String s) throws IOException {
if (reportStatus) {
pckOut.writeString(s + "\n"); //$NON-NLS-1$
} else if (msgOut != null) {
msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
}
}
};
try {
if (unpackError != null) { if (unpackError != null) {
out.sendString("unpack error " + unpackError.getMessage()); //$NON-NLS-1$ out.sendString("unpack error " + unpackError.getMessage()); //$NON-NLS-1$
if (forClient) { if (reportStatus) {
for (ReceiveCommand cmd : commands) { for (ReceiveCommand cmd : commands) {
out.sendString("ng " + cmd.getRefName() //$NON-NLS-1$ out.sendString("ng " + cmd.getRefName() //$NON-NLS-1$
+ " n/a (unpacker error)"); //$NON-NLS-1$ + " n/a (unpacker error)"); //$NON-NLS-1$
@ -1878,21 +1883,24 @@ private void sendStatusReport(final boolean forClient,
return; return;
} }
if (forClient) if (reportStatus) {
out.sendString("unpack ok"); //$NON-NLS-1$ out.sendString("unpack ok"); //$NON-NLS-1$
}
for (ReceiveCommand cmd : commands) { for (ReceiveCommand cmd : commands) {
if (cmd.getResult() == Result.OK) { if (cmd.getResult() == Result.OK) {
if (forClient) if (reportStatus) {
out.sendString("ok " + cmd.getRefName()); //$NON-NLS-1$ out.sendString("ok " + cmd.getRefName()); //$NON-NLS-1$
}
continue; continue;
} }
final StringBuilder r = new StringBuilder(); final StringBuilder r = new StringBuilder();
if (forClient) if (reportStatus) {
r.append("ng ").append(cmd.getRefName()).append(" "); //$NON-NLS-1$ //$NON-NLS-2$ r.append("ng ").append(cmd.getRefName()).append(" "); //$NON-NLS-1$ //$NON-NLS-2$
else } else {
r.append(" ! [rejected] ").append(cmd.getRefName()) //$NON-NLS-1$ r.append(" ! [rejected] ").append(cmd.getRefName()) //$NON-NLS-1$
.append(" ("); //$NON-NLS-1$ .append(" ("); //$NON-NLS-1$
}
switch (cmd.getResult()) { switch (cmd.getResult()) {
case NOT_ATTEMPTED: case NOT_ATTEMPTED:
@ -1939,13 +1947,20 @@ else if (cmd.getMessage()
break; break;
case OK: case OK:
// We shouldn't have reached this case (see 'ok' case above). // We shouldn't have reached this case (see 'ok' case
// above).
continue; continue;
} }
if (!forClient) if (!reportStatus) {
r.append(")"); //$NON-NLS-1$ r.append(")"); //$NON-NLS-1$
}
out.sendString(r.toString()); out.sendString(r.toString());
} }
} finally {
if (reportStatus) {
pckOut.end();
}
}
} }
/** /**
@ -2228,22 +2243,7 @@ private void service() throws IOException {
unlockPack(); unlockPack();
} }
if (reportStatus) { sendStatusReport(unpackError);
sendStatusReport(true, unpackError, new Reporter() {
@Override
void sendString(String s) throws IOException {
pckOut.writeString(s + "\n"); //$NON-NLS-1$
}
});
pckOut.end();
} else if (msgOut != null) {
sendStatusReport(false, unpackError, new Reporter() {
@Override
void sendString(String s) throws IOException {
msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
}
});
}
if (unpackError != null) { if (unpackError != null) {
// we already know which exception to throw. Ignore // we already know which exception to throw. Ignore