]> source.dussan.org Git - jgit.git/commitdiff
Teach UploadPack "include-tag" in "fetch" 52/119452/14
authorJonathan Tan <jonathantanmy@google.com>
Tue, 13 Mar 2018 18:07:36 +0000 (11:07 -0700)
committerJonathan Nieder <jrn@google.com>
Thu, 17 May 2018 00:16:35 +0000 (17:16 -0700)
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>
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 5073e0ed33c035aa6774682ee0e982c947ccb9c1..5ae93fc12dbcbb05b38ad153b495a10ddc5bdfc9 100644 (file)
@@ -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) {
index b1f4d11865f50304f835ff8a70dc9e6c59f724c7..199a007abe90d7c0ee0443fd297104a7b7cfa3d2 100644 (file)
@@ -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();
        }