Browse Source

Teach UploadPack "include-tag" in "fetch"

Add support for the "include-tag" parameter in the "fetch" command in
the fetch-pack/upload-pack protocol v2.

In order to determine which tags to include, only objects pointed to by
refs starting with "refs/tags/" are checked. This restriction is for
performance reasons and to match the behavior of Git (see add_ref_tag()
in builtin/pack-objects.c).

Change-Id: I7d70aa09bcc8a525218ff1559e286c2a610258ca
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Jonathan Nieder <jrn@google.com>
stable-5.1
Jonathan Tan 7 years ago committed by Jonathan Nieder
parent
commit
5a87d50408
  1. 33
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java
  2. 9
      org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

33
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java

@ -805,6 +805,39 @@ public class UploadPackTest {
assertTrue(sw.toString().isEmpty());
}
@Test
public void testV2FetchIncludeTag() throws Exception {
RevCommit commit = remote.commit().message("x").create();
RevTag tag = remote.tag("tag", commit);
remote.update("branch1", commit);
remote.update("refs/tags/tag", tag);
// Without include-tag.
ByteArrayInputStream recvStream = uploadPackV2(
"command=fetch\n",
PacketLineIn.DELIM,
"want " + commit.toObjectId().getName() + "\n",
"done\n",
PacketLineIn.END);
PacketLineIn pckIn = new PacketLineIn(recvStream);
assertThat(pckIn.readString(), is("packfile"));
parsePack(recvStream);
assertFalse(client.hasObject(tag.toObjectId()));
// With tag.
recvStream = uploadPackV2(
"command=fetch\n",
PacketLineIn.DELIM,
"want " + commit.toObjectId().getName() + "\n",
"include-tag\n",
"done\n",
PacketLineIn.END);
pckIn = new PacketLineIn(recvStream);
assertThat(pckIn.readString(), is("packfile"));
parsePack(recvStream);
assertTrue(client.hasObject(tag.toObjectId()));
}
private static class RejectAllRefFilter implements RefFilter {
@Override
public Map<String, Ref> filter(Map<String, Ref> refs) {

9
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

@ -43,6 +43,7 @@
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.Constants.R_TAGS;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import static org.eclipse.jgit.transport.GitProtocolConstants.COMMAND_FETCH;
import static org.eclipse.jgit.transport.GitProtocolConstants.COMMAND_LS_REFS;
@ -951,6 +952,7 @@ public class UploadPack {
.format(JGitText.get().unexpectedPacketLine, line));
}
boolean includeTag = false;
while ((line = pckIn.readString()) != PacketLineIn.END) {
if (line.startsWith("want ")) { //$NON-NLS-1$
wantIds.add(ObjectId.fromString(line.substring(5)));
@ -962,6 +964,9 @@ public class UploadPack {
options.add(OPTION_THIN_PACK);
} else if (line.equals(OPTION_NO_PROGRESS)) {
options.add(OPTION_NO_PROGRESS);
} else if (line.equals(OPTION_INCLUDE_TAG)) {
options.add(OPTION_INCLUDE_TAG);
includeTag = true;
}
// else ignore it
}
@ -990,7 +995,9 @@ public class UploadPack {
pckOut.writeDelim();
pckOut.writeString("packfile\n"); //$NON-NLS-1$
sendPack(new PackStatistics.Accumulator(),
refs == null ? null : refs.values());
includeTag
? db.getRefDatabase().getRefsByPrefix(R_TAGS)
: null);
}
pckOut.end();
}

Loading…
Cancel
Save