Use constants for git packet protocol line identifiers

Introduce named constants for packet line headers and use them instead
of direct string literals everywhere. This not only makes the code more
readable because we don't need NON-NLS markers, it also makes it more
robust since we can use the length of these constants instead of magic
numbers.

Change-Id: Ie4b7239e0b479a68a2dc23e6e05f25061d481a31
Signed-off-by: Thomas Wolf <twolf@apache.org>
This commit is contained in:
Thomas Wolf 2022-07-19 10:13:48 +02:00
parent 673007d529
commit eef4da5dac
6 changed files with 234 additions and 79 deletions

View File

@ -12,6 +12,18 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DELIM;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_NOT;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_SINCE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DONE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_END;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_ERR;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_HAVE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_UNSHALLOW;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_WANT;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -483,7 +495,7 @@ private void doFetchV2(ProgressMonitor monitor, Collection<Ref> want,
clearState(); clearState();
String line = pckIn.readString(); String line = pckIn.readString();
// If we sent a done, we may have an error reply here. // If we sent a done, we may have an error reply here.
if (sentDone && line.startsWith("ERR ")) { //$NON-NLS-1$ if (sentDone && line.startsWith(PACKET_ERR)) {
throw new RemoteRepositoryException(uri, line.substring(4)); throw new RemoteRepositoryException(uri, line.substring(4));
} }
@ -491,7 +503,8 @@ private void doFetchV2(ProgressMonitor monitor, Collection<Ref> want,
line = handleShallowUnshallow(shallowCommits, pckIn); line = handleShallowUnshallow(shallowCommits, pckIn);
if (!PacketLineIn.isDelimiter(line)) { if (!PacketLineIn.isDelimiter(line)) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().expectedGot, "0001", line)); //$NON-NLS-1$ .format(JGitText.get().expectedGot, PACKET_DELIM,
line));
} }
line = pckIn.readString(); line = pckIn.readString();
} }
@ -532,7 +545,7 @@ private boolean sendNextHaveBatch(FetchStateV2 fetchState,
if (c == null) { if (c == null) {
break; break;
} }
output.writeString("have " + c.getId().name() + '\n'); //$NON-NLS-1$ output.writeString(PACKET_HAVE + c.getId().name() + '\n');
n++; n++;
if (n % 10 == 0 && monitor.isCancelled()) { if (n % 10 == 0 && monitor.isCancelled()) {
throw new CancelledException(); throw new CancelledException();
@ -543,7 +556,7 @@ private boolean sendNextHaveBatch(FetchStateV2 fetchState,
|| (fetchState.hadAcks || (fetchState.hadAcks
&& fetchState.havesWithoutAck > MAX_HAVES) && fetchState.havesWithoutAck > MAX_HAVES)
|| fetchState.havesTotal > maxHaves) { || fetchState.havesTotal > maxHaves) {
output.writeString("done\n"); //$NON-NLS-1$ output.writeString(PACKET_DONE + '\n');
output.end(); output.end();
return true; return true;
} }
@ -610,11 +623,12 @@ private boolean readAcknowledgments(FetchStateV2 fetchState,
if (gotReady) { if (gotReady) {
if (!PacketLineIn.isDelimiter(line)) { if (!PacketLineIn.isDelimiter(line)) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().expectedGot, "0001", line)); //$NON-NLS-1$ .format(JGitText.get().expectedGot, PACKET_DELIM,
line));
} }
} else if (!PacketLineIn.isEnd(line)) { } else if (!PacketLineIn.isEnd(line)) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().expectedGot, "0000", line)); //$NON-NLS-1$ .format(JGitText.get().expectedGot, PACKET_END, line));
} }
return gotReady; return gotReady;
} }
@ -726,8 +740,7 @@ private boolean sendWants(Collection<Ref> want, PacketLineOut p)
} }
final StringBuilder line = new StringBuilder(46); final StringBuilder line = new StringBuilder(46);
line.append("want "); //$NON-NLS-1$ line.append(PACKET_WANT).append(objectId.name());
line.append(objectId.name());
if (first && TransferConfig.ProtocolVersion.V0 if (first && TransferConfig.ProtocolVersion.V0
.equals(getProtocolVersion())) { .equals(getProtocolVersion())) {
line.append(enableCapabilities()); line.append(enableCapabilities());
@ -836,7 +849,7 @@ private void negotiate(ProgressMonitor monitor, boolean mayHaveShallow, Set<Obje
} }
ObjectId o = c.getId(); ObjectId o = c.getId();
pckOut.writeString("have " + o.name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ pckOut.writeString(PACKET_HAVE + o.name() + '\n');
havesSent++; havesSent++;
havesSinceLastContinue++; havesSinceLastContinue++;
@ -939,7 +952,7 @@ private void negotiate(ProgressMonitor monitor, boolean mayHaveShallow, Set<Obje
// loop above while in the middle of a request. This allows us // loop above while in the middle of a request. This allows us
// to just write done immediately. // to just write done immediately.
// //
pckOut.writeString("done\n"); //$NON-NLS-1$ pckOut.writeString(PACKET_DONE + '\n');
pckOut.flush(); pckOut.flush();
} }
@ -956,7 +969,7 @@ private void negotiate(ProgressMonitor monitor, boolean mayHaveShallow, Set<Obje
String line = handleShallowUnshallow(shallowCommits, pckIn); String line = handleShallowUnshallow(shallowCommits, pckIn);
if (!PacketLineIn.isEnd(line)) { if (!PacketLineIn.isEnd(line)) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().expectedGot, "0000", line)); //$NON-NLS-1$ .format(JGitText.get().expectedGot, PACKET_END, line));
} }
} }
@ -1041,7 +1054,7 @@ private void markAdvertised(AnyObjectId id) {
private void markCommon(RevObject obj, AckNackResult anr, boolean useState) private void markCommon(RevObject obj, AckNackResult anr, boolean useState)
throws IOException { throws IOException {
if (useState && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) { if (useState && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) {
pckState.writeString("have " + obj.name() + '\n'); //$NON-NLS-1$ pckState.writeString(PACKET_HAVE + obj.name() + '\n');
obj.add(STATE); obj.add(STATE);
} }
obj.add(COMMON); obj.add(COMMON);
@ -1074,41 +1087,46 @@ private void receivePack(final ProgressMonitor monitor,
} }
} }
private void sendShallow(Set<ObjectId> shallowCommits, PacketLineOut output) throws IOException { private void sendShallow(Set<ObjectId> shallowCommits, PacketLineOut output)
throws IOException {
for (ObjectId shallowCommit : shallowCommits) { for (ObjectId shallowCommit : shallowCommits) {
output.writeString("shallow " + shallowCommit.name()); //$NON-NLS-1$ output.writeString(PACKET_SHALLOW + shallowCommit.name());
} }
if (depth != null) { if (depth != null) {
output.writeString("deepen " + depth); //$NON-NLS-1$ output.writeString(PACKET_DEEPEN + depth);
} }
if (deepenSince != null) { if (deepenSince != null) {
output.writeString("deepen-since " + deepenSince.getEpochSecond()); //$NON-NLS-1$ output.writeString(
PACKET_DEEPEN_SINCE + deepenSince.getEpochSecond());
} }
if (deepenNots != null) { if (deepenNots != null) {
for (String deepenNotRef : deepenNots) { for (String deepenNotRef : deepenNots) {
output.writeString("deepen-not " + deepenNotRef); //$NON-NLS-1$ output.writeString(PACKET_DEEPEN_NOT + deepenNotRef);
} }
} }
} }
private String handleShallowUnshallow(Set<ObjectId> advertisedShallowCommits, PacketLineIn input) private String handleShallowUnshallow(
Set<ObjectId> advertisedShallowCommits, PacketLineIn input)
throws IOException { throws IOException {
String line = input.readString(); String line = input.readString();
ObjectDatabase objectDatabase = local.getObjectDatabase(); ObjectDatabase objectDatabase = local.getObjectDatabase();
HashSet<ObjectId> newShallowCommits = new HashSet<>(advertisedShallowCommits); HashSet<ObjectId> newShallowCommits = new HashSet<>(
advertisedShallowCommits);
while (!PacketLineIn.isDelimiter(line) && !PacketLineIn.isEnd(line)) { while (!PacketLineIn.isDelimiter(line) && !PacketLineIn.isEnd(line)) {
if (line.startsWith("shallow ")) { //$NON-NLS-1$ if (line.startsWith(PACKET_SHALLOW)) {
newShallowCommits.add(ObjectId newShallowCommits.add(ObjectId
.fromString(line.substring("shallow ".length()))); //$NON-NLS-1$ .fromString(line.substring(PACKET_SHALLOW.length())));
} else if (line.startsWith("unshallow ")) { //$NON-NLS-1$ } else if (line.startsWith(PACKET_UNSHALLOW)) {
ObjectId unshallow = ObjectId ObjectId unshallow = ObjectId
.fromString(line.substring("unshallow ".length())); //$NON-NLS-1$ .fromString(line.substring(PACKET_UNSHALLOW.length()));
if (!advertisedShallowCommits.contains(unshallow)) { if (!advertisedShallowCommits.contains(unshallow)) {
throw new PackProtocolException(MessageFormat.format(JGitText.get() throw new PackProtocolException(MessageFormat.format(
.notShallowedUnshallow, unshallow.name())); JGitText.get().notShallowedUnshallow,
unshallow.name()));
} }
newShallowCommits.remove(unshallow); newShallowCommits.remove(unshallow);
} }

View File

@ -343,6 +343,106 @@ public final class GitProtocolConstants {
*/ */
public static final String VERSION_2_REQUEST = "version=2"; //$NON-NLS-1$ public static final String VERSION_2_REQUEST = "version=2"; //$NON-NLS-1$
/**
* The flush packet.
*
* @since 6.3
*/
public static final String PACKET_FLUSH = "0000"; //$NON-NLS-1$
/**
* An alias for {@link #PACKET_FLUSH}. "Flush" is the name used in the C git
* documentation; the Java implementation calls this "end" in several
* places.
*
* @since 6.3
*/
public static final String PACKET_END = PACKET_FLUSH;
/**
* The delimiter packet in protocol V2.
*
* @since 6.3
*/
public static final String PACKET_DELIM = "0001"; //$NON-NLS-1$
/**
* A "deepen" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_DEEPEN = "deepen "; //$NON-NLS-1$
/**
* A "deepen-not" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_DEEPEN_NOT = "deepen-not "; //$NON-NLS-1$
/**
* A "deepen-since" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_DEEPEN_SINCE = "deepen-since "; //$NON-NLS-1$
/**
* An "ACK" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_ACK = "ACK "; //$NON-NLS-1$
/**
* A "done" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_DONE = "done"; //$NON-NLS-1$
/**
* A "ERR" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_ERR = "ERR "; //$NON-NLS-1$
/**
* A "have" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_HAVE = "have "; //$NON-NLS-1$
/**
* A "shallow" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_SHALLOW = OPTION_SHALLOW + ' ';
/**
* A "shallow" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_UNSHALLOW = "unshallow "; //$NON-NLS-1$
/**
* A "want" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_WANT = "want "; //$NON-NLS-1$
/**
* A "want-ref" packet beginning.
*
* @since 6.3
*/
public static final String PACKET_WANT_REF = OPTION_WANT_REF + ' ';
enum MultiAck { enum MultiAck {
OFF, CONTINUE, DETAILED; OFF, CONTINUE, DETAILED;
} }

View File

@ -10,6 +10,11 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_NOT;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_SINCE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_WANT;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
@ -70,8 +75,9 @@ FetchV0Request recvWants(PacketLineIn pckIn)
break; break;
} }
if (line.startsWith("deepen ")) { //$NON-NLS-1$ if (line.startsWith(PACKET_DEEPEN)) {
int depth = Integer.parseInt(line.substring(7)); int depth = Integer
.parseInt(line.substring(PACKET_DEEPEN.length()));
if (depth <= 0) { if (depth <= 0) {
throw new PackProtocolException( throw new PackProtocolException(
MessageFormat.format(JGitText.get().invalidDepth, MessageFormat.format(JGitText.get().invalidDepth,
@ -89,8 +95,9 @@ FetchV0Request recvWants(PacketLineIn pckIn)
continue; continue;
} }
if (line.startsWith("deepen-not ")) { //$NON-NLS-1$ if (line.startsWith(PACKET_DEEPEN_NOT)) {
reqBuilder.addDeepenNot(line.substring(11)); reqBuilder.addDeepenNot(
line.substring(PACKET_DEEPEN_NOT.length()));
if (reqBuilder.getDepth() != 0) { if (reqBuilder.getDepth() != 0) {
throw new PackProtocolException( throw new PackProtocolException(
JGitText.get().deepenNotWithDeepen); JGitText.get().deepenNotWithDeepen);
@ -98,9 +105,10 @@ FetchV0Request recvWants(PacketLineIn pckIn)
continue; continue;
} }
if (line.startsWith("deepen-since ")) { //$NON-NLS-1$ if (line.startsWith(PACKET_DEEPEN_SINCE)) {
// TODO: timestamps should be long // TODO: timestamps should be long
int ts = Integer.parseInt(line.substring(13)); int ts = Integer
.parseInt(line.substring(PACKET_DEEPEN_SINCE.length()));
if (ts <= 0) { if (ts <= 0) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().invalidTimestamp, line)); .format(JGitText.get().invalidTimestamp, line));
@ -113,9 +121,10 @@ FetchV0Request recvWants(PacketLineIn pckIn)
continue; continue;
} }
if (line.startsWith("shallow ")) { //$NON-NLS-1$ if (line.startsWith(PACKET_SHALLOW)) {
reqBuilder.addClientShallowCommit( reqBuilder.addClientShallowCommit(
ObjectId.fromString(line.substring(8))); ObjectId.fromString(
line.substring(PACKET_SHALLOW.length())));
continue; continue;
} }
@ -133,7 +142,7 @@ FetchV0Request recvWants(PacketLineIn pckIn)
continue; continue;
} }
if (!line.startsWith("want ") || line.length() < 45) { //$NON-NLS-1$ if (!line.startsWith(PACKET_WANT) || line.length() < 45) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$ .format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
} }
@ -147,7 +156,8 @@ FetchV0Request recvWants(PacketLineIn pckIn)
} }
} }
reqBuilder.addWantId(ObjectId.fromString(line.substring(5))); reqBuilder.addWantId(
ObjectId.fromString(line.substring(PACKET_WANT.length())));
isFirst = false; isFirst = false;
} }

View File

@ -20,7 +20,14 @@
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WAIT_FOR_DONE; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WAIT_FOR_DONE;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WANT_REF; import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_NOT;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DEEPEN_SINCE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DONE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_HAVE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_WANT;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_WANT_REF;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
@ -115,15 +122,17 @@ FetchV2Request parseFetchRequest(PacketLineIn pckIn)
boolean filterReceived = false; boolean filterReceived = false;
for (String line2 : pckIn.readStrings()) { for (String line2 : pckIn.readStrings()) {
if (line2.startsWith("want ")) { //$NON-NLS-1$ if (line2.startsWith(PACKET_WANT)) {
reqBuilder.addWantId(ObjectId.fromString(line2.substring(5))); reqBuilder.addWantId(ObjectId
.fromString(line2.substring(PACKET_WANT.length())));
} else if (transferConfig.isAllowRefInWant() } else if (transferConfig.isAllowRefInWant()
&& line2.startsWith(OPTION_WANT_REF + " ")) { //$NON-NLS-1$ && line2.startsWith(PACKET_WANT_REF)) {
reqBuilder.addWantedRef( reqBuilder.addWantedRef(
line2.substring(OPTION_WANT_REF.length() + 1)); line2.substring(PACKET_WANT_REF.length()));
} else if (line2.startsWith("have ")) { //$NON-NLS-1$ } else if (line2.startsWith(PACKET_HAVE)) {
reqBuilder.addPeerHas(ObjectId.fromString(line2.substring(5))); reqBuilder.addPeerHas(ObjectId
} else if (line2.equals("done")) { //$NON-NLS-1$ .fromString(line2.substring(PACKET_HAVE.length())));
} else if (line2.equals(PACKET_DONE)) {
reqBuilder.setDoneReceived(); reqBuilder.setDoneReceived();
} else if (line2.equals(OPTION_WAIT_FOR_DONE)) { } else if (line2.equals(OPTION_WAIT_FOR_DONE)) {
reqBuilder.setWaitForDone(); reqBuilder.setWaitForDone();
@ -135,11 +144,13 @@ FetchV2Request parseFetchRequest(PacketLineIn pckIn)
reqBuilder.addClientCapability(OPTION_INCLUDE_TAG); reqBuilder.addClientCapability(OPTION_INCLUDE_TAG);
} else if (line2.equals(OPTION_OFS_DELTA)) { } else if (line2.equals(OPTION_OFS_DELTA)) {
reqBuilder.addClientCapability(OPTION_OFS_DELTA); reqBuilder.addClientCapability(OPTION_OFS_DELTA);
} else if (line2.startsWith("shallow ")) { //$NON-NLS-1$ } else if (line2.startsWith(PACKET_SHALLOW)) {
reqBuilder.addClientShallowCommit( reqBuilder.addClientShallowCommit(
ObjectId.fromString(line2.substring(8))); ObjectId.fromString(
} else if (line2.startsWith("deepen ")) { //$NON-NLS-1$ line2.substring(PACKET_SHALLOW.length())));
int parsedDepth = Integer.parseInt(line2.substring(7)); } else if (line2.startsWith(PACKET_DEEPEN)) {
int parsedDepth = Integer
.parseInt(line2.substring(PACKET_DEEPEN.length()));
if (parsedDepth <= 0) { if (parsedDepth <= 0) {
throw new PackProtocolException( throw new PackProtocolException(
MessageFormat.format(JGitText.get().invalidDepth, MessageFormat.format(JGitText.get().invalidDepth,
@ -154,16 +165,18 @@ FetchV2Request parseFetchRequest(PacketLineIn pckIn)
JGitText.get().deepenNotWithDeepen); JGitText.get().deepenNotWithDeepen);
} }
reqBuilder.setDepth(parsedDepth); reqBuilder.setDepth(parsedDepth);
} else if (line2.startsWith("deepen-not ")) { //$NON-NLS-1$ } else if (line2.startsWith(PACKET_DEEPEN_NOT)) {
reqBuilder.addDeepenNot(line2.substring(11)); reqBuilder.addDeepenNot(
line2.substring(PACKET_DEEPEN_NOT.length()));
if (reqBuilder.getDepth() != 0) { if (reqBuilder.getDepth() != 0) {
throw new PackProtocolException( throw new PackProtocolException(
JGitText.get().deepenNotWithDeepen); JGitText.get().deepenNotWithDeepen);
} }
} else if (line2.equals(OPTION_DEEPEN_RELATIVE)) { } else if (line2.equals(OPTION_DEEPEN_RELATIVE)) {
reqBuilder.addClientCapability(OPTION_DEEPEN_RELATIVE); reqBuilder.addClientCapability(OPTION_DEEPEN_RELATIVE);
} else if (line2.startsWith("deepen-since ")) { //$NON-NLS-1$ } else if (line2.startsWith(PACKET_DEEPEN_SINCE)) {
int ts = Integer.parseInt(line2.substring(13)); int ts = Integer.parseInt(
line2.substring(PACKET_DEEPEN_SINCE.length()));
if (ts <= 0) { if (ts <= 0) {
throw new PackProtocolException(MessageFormat throw new PackProtocolException(MessageFormat
.format(JGitText.get().invalidTimestamp, line2)); .format(JGitText.get().invalidTimestamp, line2));

View File

@ -20,6 +20,8 @@
import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS;
import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K; import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_ERR;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW;
import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR;
import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS; import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
@ -1247,7 +1249,7 @@ private void unlockPack() throws IOException {
public void sendAdvertisedRefs(RefAdvertiser adv) public void sendAdvertisedRefs(RefAdvertiser adv)
throws IOException, ServiceMayNotContinueException { throws IOException, ServiceMayNotContinueException {
if (advertiseError != null) { if (advertiseError != null) {
adv.writeOne("ERR " + advertiseError); //$NON-NLS-1$ adv.writeOne(PACKET_ERR + advertiseError);
return; return;
} }
@ -1255,7 +1257,7 @@ public void sendAdvertisedRefs(RefAdvertiser adv)
advertiseRefsHook.advertiseRefs(this); advertiseRefsHook.advertiseRefs(this);
} catch (ServiceMayNotContinueException fail) { } catch (ServiceMayNotContinueException fail) {
if (fail.getMessage() != null) { if (fail.getMessage() != null) {
adv.writeOne("ERR " + fail.getMessage()); //$NON-NLS-1$ adv.writeOne(PACKET_ERR + fail.getMessage());
fail.setOutput(); fail.setOutput();
} }
throw fail; throw fail;
@ -1339,8 +1341,9 @@ private void recvCommands() throws IOException {
break; break;
} }
if (line.length() >= 48 && line.startsWith("shallow ")) { //$NON-NLS-1$ int len = PACKET_SHALLOW.length() + 40;
parseShallow(line.substring(8, 48)); if (line.length() >= len && line.startsWith(PACKET_SHALLOW)) {
parseShallow(line.substring(PACKET_SHALLOW.length(), len));
continue; continue;
} }
@ -1795,9 +1798,9 @@ private void sendStatusReport(Throwable unpackError) throws IOException {
@Override @Override
void sendString(String s) throws IOException { void sendString(String s) throws IOException {
if (reportStatus) { if (reportStatus) {
pckOut.writeString(s + "\n"); //$NON-NLS-1$ pckOut.writeString(s + '\n');
} else if (msgOut != null) { } else if (msgOut != null) {
msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$ msgOut.write(Constants.encode(s + '\n'));
} }
} }
}; };

View File

@ -36,6 +36,12 @@
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SIDE_BAND_64K;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_THIN_PACK;
import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WAIT_FOR_DONE; import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WAIT_FOR_DONE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_ACK;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_DONE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_ERR;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_HAVE;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_SHALLOW;
import static org.eclipse.jgit.transport.GitProtocolConstants.PACKET_UNSHALLOW;
import static org.eclipse.jgit.transport.GitProtocolConstants.VERSION_2_REQUEST; import static org.eclipse.jgit.transport.GitProtocolConstants.VERSION_2_REQUEST;
import static org.eclipse.jgit.util.RefMap.toRefMap; import static org.eclipse.jgit.util.RefMap.toRefMap;
@ -1076,9 +1082,10 @@ else if (requestValidator instanceof AnyRequestValidator)
deepenNots = parseDeepenNots(req.getDeepenNots()); deepenNots = parseDeepenNots(req.getDeepenNots());
if (req.getDepth() != 0 || req.getDeepenSince() != 0 || !req.getDeepenNots().isEmpty()) { if (req.getDepth() != 0 || req.getDeepenSince() != 0 || !req.getDeepenNots().isEmpty()) {
computeShallowsAndUnshallows(req, shallow -> { computeShallowsAndUnshallows(req, shallow -> {
pckOut.writeString("shallow " + shallow.name() + '\n'); //$NON-NLS-1$ pckOut.writeString(PACKET_SHALLOW + shallow.name() + '\n');
}, unshallow -> { }, unshallow -> {
pckOut.writeString("unshallow " + unshallow.name() + '\n'); //$NON-NLS-1$ pckOut.writeString(
PACKET_UNSHALLOW + unshallow.name() + '\n');
unshallowCommits.add(unshallow); unshallowCommits.add(unshallow);
}, deepenNots); }, deepenNots);
pckOut.end(); pckOut.end();
@ -1227,7 +1234,7 @@ private void fetchV2(PacketLineOut pckOut) throws IOException {
GitProtocolConstants.SECTION_ACKNOWLEDGMENTS + '\n'); GitProtocolConstants.SECTION_ACKNOWLEDGMENTS + '\n');
for (ObjectId id : req.getPeerHas()) { for (ObjectId id : req.getPeerHas()) {
if (walk.getObjectReader().has(id)) { if (walk.getObjectReader().has(id)) {
pckOut.writeString("ACK " + id.getName() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ pckOut.writeString(PACKET_ACK + id.getName() + '\n');
} }
} }
processHaveLines(req.getPeerHas(), ObjectId.zeroId(), processHaveLines(req.getPeerHas(), ObjectId.zeroId(),
@ -1245,12 +1252,13 @@ private void fetchV2(PacketLineOut pckOut) throws IOException {
if (mayHaveShallow) { if (mayHaveShallow) {
if (sectionSent) if (sectionSent)
pckOut.writeDelim(); pckOut.writeDelim();
pckOut.writeString("shallow-info\n"); //$NON-NLS-1$ pckOut.writeString(
GitProtocolConstants.SECTION_SHALLOW_INFO + '\n');
for (ObjectId o : shallowCommits) { for (ObjectId o : shallowCommits) {
pckOut.writeString("shallow " + o.getName() + '\n'); //$NON-NLS-1$ pckOut.writeString(PACKET_SHALLOW + o.getName() + '\n');
} }
for (ObjectId o : unshallowCommits) { for (ObjectId o : unshallowCommits) {
pckOut.writeString("unshallow " + o.getName() + '\n'); //$NON-NLS-1$ pckOut.writeString(PACKET_UNSHALLOW + o.getName() + '\n');
} }
sectionSent = true; sectionSent = true;
} }
@ -1314,7 +1322,7 @@ private void objectInfo(PacketLineOut pckOut) throws IOException {
.format(JGitText.get().missingObject, oid.name()), e); .format(JGitText.get().missingObject, oid.name()), e);
} }
pckOut.writeString(oid.getName() + " " + size); //$NON-NLS-1$ pckOut.writeString(oid.getName() + ' ' + size);
} }
pckOut.end(); pckOut.end();
@ -1386,7 +1394,7 @@ private void serviceV2(PacketLineOut pckOut) throws IOException {
protocolV2Hook protocolV2Hook
.onCapabilities(CapabilitiesV2Request.builder().build()); .onCapabilities(CapabilitiesV2Request.builder().build());
for (String s : getV2CapabilityAdvertisement()) { for (String s : getV2CapabilityAdvertisement()) {
pckOut.writeString(s + "\n"); //$NON-NLS-1$ pckOut.writeString(s + '\n');
} }
pckOut.end(); pckOut.end();
@ -1613,7 +1621,7 @@ public void sendAdvertisedRefs(RefAdvertiser adv,
*/ */
public void sendMessage(String what) { public void sendMessage(String what) {
try { try {
msgOut.write(Constants.encode(what + "\n")); //$NON-NLS-1$ msgOut.write(Constants.encode(what + '\n'));
} catch (IOException e) { } catch (IOException e) {
// Ignore write failures. // Ignore write failures.
} }
@ -1720,24 +1728,26 @@ private boolean negotiate(FetchRequest req,
if (commonBase.isEmpty() || multiAck != MultiAck.OFF) if (commonBase.isEmpty() || multiAck != MultiAck.OFF)
pckOut.writeString("NAK\n"); //$NON-NLS-1$ pckOut.writeString("NAK\n"); //$NON-NLS-1$
if (noDone && sentReady) { if (noDone && sentReady) {
pckOut.writeString("ACK " + last.name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ pckOut.writeString(PACKET_ACK + last.name() + '\n');
return true; return true;
} }
if (!biDirectionalPipe) if (!biDirectionalPipe)
return false; return false;
pckOut.flush(); pckOut.flush();
} else if (line.startsWith("have ") && line.length() == 45) { //$NON-NLS-1$ } else if (line.startsWith(PACKET_HAVE)
peerHas.add(ObjectId.fromString(line.substring(5))); && line.length() == PACKET_HAVE.length() + 40) {
peerHas.add(ObjectId
.fromString(line.substring(PACKET_HAVE.length())));
accumulator.haves++; accumulator.haves++;
} else if (line.equals("done")) { //$NON-NLS-1$ } else if (line.equals(PACKET_DONE)) {
last = processHaveLines(peerHas, last, pckOut, accumulator, Option.NONE); last = processHaveLines(peerHas, last, pckOut, accumulator, Option.NONE);
if (commonBase.isEmpty()) if (commonBase.isEmpty())
pckOut.writeString("NAK\n"); //$NON-NLS-1$ pckOut.writeString("NAK\n"); //$NON-NLS-1$
else if (multiAck != MultiAck.OFF) else if (multiAck != MultiAck.OFF)
pckOut.writeString("ACK " + last.name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ pckOut.writeString(PACKET_ACK + last.name() + '\n');
return true; return true;
@ -1798,14 +1808,15 @@ private ObjectId processHaveLines(List<ObjectId> peerHas, ObjectId last,
// //
switch (multiAck) { switch (multiAck) {
case OFF: case OFF:
if (commonBase.size() == 1) if (commonBase.size() == 1) {
out.writeString("ACK " + obj.name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ out.writeString(PACKET_ACK + obj.name() + '\n');
}
break; break;
case CONTINUE: case CONTINUE:
out.writeString("ACK " + obj.name() + " continue\n"); //$NON-NLS-1$ //$NON-NLS-2$ out.writeString(PACKET_ACK + obj.name() + " continue\n"); //$NON-NLS-1$
break; break;
case DETAILED: case DETAILED:
out.writeString("ACK " + obj.name() + " common\n"); //$NON-NLS-1$ //$NON-NLS-2$ out.writeString(PACKET_ACK + obj.name() + " common\n"); //$NON-NLS-1$
break; break;
} }
} }
@ -1844,11 +1855,11 @@ private boolean shouldGiveUp(List<ObjectId> peerHas, PacketLineOut out, int miss
break; break;
case CONTINUE: case CONTINUE:
out.writeString( out.writeString(
"ACK " + id.name() + " continue\n"); //$NON-NLS-1$ //$NON-NLS-2$ PACKET_ACK + id.name() + " continue\n"); //$NON-NLS-1$
break; break;
case DETAILED: case DETAILED:
out.writeString( out.writeString(
"ACK " + id.name() + " ready\n"); //$NON-NLS-1$ //$NON-NLS-2$ PACKET_ACK + id.name() + " ready\n"); //$NON-NLS-1$
readySent = true; readySent = true;
break; break;
} }
@ -1861,7 +1872,7 @@ private boolean shouldGiveUp(List<ObjectId> peerHas, PacketLineOut out, int miss
if (multiAck == MultiAck.DETAILED && !didOkToGiveUp if (multiAck == MultiAck.DETAILED && !didOkToGiveUp
&& okToGiveUp()) { && okToGiveUp()) {
ObjectId id = peerHas.get(peerHas.size() - 1); ObjectId id = peerHas.get(peerHas.size() - 1);
out.writeString("ACK " + id.name() + " ready\n"); //$NON-NLS-1$ //$NON-NLS-2$ out.writeString(PACKET_ACK + id.name() + " ready\n"); //$NON-NLS-1$
readySent = true; readySent = true;
} }
@ -2552,7 +2563,7 @@ private class PackProtocolErrorWriter implements ErrorWriter {
@Override @Override
public void writeError(String message) throws IOException { public void writeError(String message) throws IOException {
new PacketLineOut(requireNonNull(rawOut)) new PacketLineOut(requireNonNull(rawOut))
.writeString("ERR " + message + '\n'); //$NON-NLS-1$ .writeString(PACKET_ERR + message + '\n');
} }
} }
} }