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,98 +1853,113 @@ 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
if (unpackError != null) { void sendString(String s) throws IOException {
out.sendString("unpack error " + unpackError.getMessage()); //$NON-NLS-1$ if (reportStatus) {
if (forClient) { pckOut.writeString(s + "\n"); //$NON-NLS-1$
for (ReceiveCommand cmd : commands) { } else if (msgOut != null) {
out.sendString("ng " + cmd.getRefName() //$NON-NLS-1$ msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
+ " n/a (unpacker error)"); //$NON-NLS-1$
} }
} }
return; };
}
if (forClient) try {
out.sendString("unpack ok"); //$NON-NLS-1$ if (unpackError != null) {
for (ReceiveCommand cmd : commands) { out.sendString("unpack error " + unpackError.getMessage()); //$NON-NLS-1$
if (cmd.getResult() == Result.OK) { if (reportStatus) {
if (forClient) for (ReceiveCommand cmd : commands) {
out.sendString("ok " + cmd.getRefName()); //$NON-NLS-1$ out.sendString("ng " + cmd.getRefName() //$NON-NLS-1$
continue; + " n/a (unpacker error)"); //$NON-NLS-1$
}
}
return;
} }
final StringBuilder r = new StringBuilder(); if (reportStatus) {
if (forClient) out.sendString("unpack ok"); //$NON-NLS-1$
r.append("ng ").append(cmd.getRefName()).append(" "); //$NON-NLS-1$ //$NON-NLS-2$ }
else for (ReceiveCommand cmd : commands) {
r.append(" ! [rejected] ").append(cmd.getRefName()) //$NON-NLS-1$ if (cmd.getResult() == Result.OK) {
.append(" ("); //$NON-NLS-1$ if (reportStatus) {
out.sendString("ok " + cmd.getRefName()); //$NON-NLS-1$
switch (cmd.getResult()) { }
case NOT_ATTEMPTED: continue;
r.append("server bug; ref not processed"); //$NON-NLS-1$ }
break;
final StringBuilder r = new StringBuilder();
case REJECTED_NOCREATE: if (reportStatus) {
r.append("creation prohibited"); //$NON-NLS-1$ r.append("ng ").append(cmd.getRefName()).append(" "); //$NON-NLS-1$ //$NON-NLS-2$
break; } else {
r.append(" ! [rejected] ").append(cmd.getRefName()) //$NON-NLS-1$
case REJECTED_NODELETE: .append(" ("); //$NON-NLS-1$
r.append("deletion prohibited"); //$NON-NLS-1$ }
break;
switch (cmd.getResult()) {
case REJECTED_NONFASTFORWARD: case NOT_ATTEMPTED:
r.append("non-fast forward"); //$NON-NLS-1$ r.append("server bug; ref not processed"); //$NON-NLS-1$
break; break;
case REJECTED_CURRENT_BRANCH: case REJECTED_NOCREATE:
r.append("branch is currently checked out"); //$NON-NLS-1$ r.append("creation prohibited"); //$NON-NLS-1$
break; break;
case REJECTED_MISSING_OBJECT: case REJECTED_NODELETE:
if (cmd.getMessage() == null) r.append("deletion prohibited"); //$NON-NLS-1$
r.append("missing object(s)"); //$NON-NLS-1$ break;
else if (cmd.getMessage()
.length() == Constants.OBJECT_ID_STRING_LENGTH) { case REJECTED_NONFASTFORWARD:
r.append("object "); //$NON-NLS-1$ r.append("non-fast forward"); //$NON-NLS-1$
r.append(cmd.getMessage()); break;
r.append(" missing"); //$NON-NLS-1$
} else case REJECTED_CURRENT_BRANCH:
r.append(cmd.getMessage()); r.append("branch is currently checked out"); //$NON-NLS-1$
break; break;
case REJECTED_OTHER_REASON: case REJECTED_MISSING_OBJECT:
if (cmd.getMessage() == null) if (cmd.getMessage() == null)
r.append("unspecified reason"); //$NON-NLS-1$ r.append("missing object(s)"); //$NON-NLS-1$
else else if (cmd.getMessage()
r.append(cmd.getMessage()); .length() == Constants.OBJECT_ID_STRING_LENGTH) {
break; r.append("object "); //$NON-NLS-1$
r.append(cmd.getMessage());
case LOCK_FAILURE: r.append(" missing"); //$NON-NLS-1$
r.append("failed to lock"); //$NON-NLS-1$ } else
break; r.append(cmd.getMessage());
break;
case OK:
// We shouldn't have reached this case (see 'ok' case above). case REJECTED_OTHER_REASON:
continue; if (cmd.getMessage() == null)
r.append("unspecified reason"); //$NON-NLS-1$
else
r.append(cmd.getMessage());
break;
case LOCK_FAILURE:
r.append("failed to lock"); //$NON-NLS-1$
break;
case OK:
// We shouldn't have reached this case (see 'ok' case
// above).
continue;
}
if (!reportStatus) {
r.append(")"); //$NON-NLS-1$
}
out.sendString(r.toString());
}
} finally {
if (reportStatus) {
pckOut.end();
} }
if (!forClient)
r.append(")"); //$NON-NLS-1$
out.sendString(r.toString());
} }
} }
@ -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