]> source.dussan.org Git - jgit.git/commitdiff
Move ls-refs parsing code to the ProtocolV2Parser 99/128399/12
authorIvan Frade <ifrade@google.com>
Tue, 28 Aug 2018 20:57:52 +0000 (13:57 -0700)
committerIvan Frade <ifrade@google.com>
Fri, 14 Sep 2018 15:52:23 +0000 (08:52 -0700)
Fetch code has been moved to a ProtocolV2Parser, but ls-refs code is
still in UploadPack.

Moving it to the parser makes it easier to test, keeps the parsing
together and makes the two commands follow similar structure.

Change-Id: I573ce543e804ddeb9f83303b4af250b7cddc8cad
Signed-off-by: Ivan Frade <ifrade@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/ProtocolV2ParserTest.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV2Parser.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 4d1150844cbfe250eceb07e381c21f1a471f9b53..c2e8b5b6213d7ff00316b5841c917439c6595b75 100644 (file)
@@ -45,6 +45,7 @@ package org.eclipse.jgit.transport;
 import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.hasItems;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
@@ -333,4 +334,60 @@ public class ProtocolV2ParserTest {
                                testRepo.getRepository().getRefDatabase());
        }
 
+       @Test
+       public void testLsRefsMinimalReq() throws IOException {
+               PacketLineIn pckIn = formatAsPacketLine(PacketLineIn.DELIM,
+                               PacketLineIn.END);
+
+               ProtocolV2Parser parser = new ProtocolV2Parser(
+                               ConfigBuilder.getDefault());
+               LsRefsV2Request req = parser.parseLsRefsRequest(pckIn);
+               assertFalse(req.getPeel());
+               assertFalse(req.getSymrefs());
+               assertEquals(0, req.getRefPrefixes().size());
+       }
+
+       @Test
+       public void testLsRefsSymrefs() throws IOException {
+               PacketLineIn pckIn = formatAsPacketLine(PacketLineIn.DELIM, "symrefs",
+                               PacketLineIn.END);
+
+               ProtocolV2Parser parser = new ProtocolV2Parser(
+                               ConfigBuilder.getDefault());
+               LsRefsV2Request req = parser.parseLsRefsRequest(pckIn);
+               assertFalse(req.getPeel());
+               assertTrue(req.getSymrefs());
+               assertEquals(0, req.getRefPrefixes().size());
+
+       }
+
+       @Test
+       public void testLsRefsPeel() throws IOException {
+               PacketLineIn pckIn = formatAsPacketLine(
+                               PacketLineIn.DELIM,
+                               "peel",
+                               PacketLineIn.END);
+
+               ProtocolV2Parser parser = new ProtocolV2Parser(
+                               ConfigBuilder.getDefault());
+               LsRefsV2Request req = parser.parseLsRefsRequest(pckIn);
+               assertTrue(req.getPeel());
+               assertFalse(req.getSymrefs());
+               assertEquals(0, req.getRefPrefixes().size());
+       }
+
+       @Test
+       public void testLsRefsRefPrefixes() throws IOException {
+               PacketLineIn pckIn = formatAsPacketLine(PacketLineIn.DELIM,
+                               "ref-prefix refs/for", "ref-prefix refs/heads",
+                               PacketLineIn.END);
+
+               ProtocolV2Parser parser = new ProtocolV2Parser(
+                               ConfigBuilder.getDefault());
+               LsRefsV2Request req = parser.parseLsRefsRequest(pckIn);
+               assertFalse(req.getPeel());
+               assertFalse(req.getSymrefs());
+               assertEquals(2, req.getRefPrefixes().size());
+               assertThat(req.getRefPrefixes(), hasItems("refs/for", "refs/heads"));
+       }
 }
index eae2c6edb92344e934816cace0b2de896adf78cd..da0fb9c4474c7b6ba70174926e67470d2276cfea 100644 (file)
@@ -53,6 +53,8 @@ import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_WANT_REF;
 
 import java.io.IOException;
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.eclipse.jgit.errors.PackProtocolException;
 import org.eclipse.jgit.internal.JGitText;
@@ -204,6 +206,51 @@ final class ProtocolV2Parser {
        }
 
        /**
+        * Parse the incoming ls-refs request arguments from the wire. This is meant
+        * for calling immediately after the caller has consumed a "command=ls-refs"
+        * line indicating the beginning of a ls-refs request.
+        *
+        * The incoming PacketLineIn is consumed until an END line, but the caller
+        * is responsible for closing it (if needed)
+        *
+        * @param pckIn
+        *            incoming lines. This method will read until an END line.
+        * @return a LsRefsV2Request object with the data received in the wire.
+        * @throws PackProtocolException
+        *             for inconsistencies in the protocol (e.g. unexpected lines)
+        * @throws IOException
+        *             reporting problems reading the incoming messages from the
+        *             wire
+        */
+       LsRefsV2Request parseLsRefsRequest(PacketLineIn pckIn)
+                       throws PackProtocolException, IOException {
+               LsRefsV2Request.Builder builder = LsRefsV2Request.builder();
+               List<String> prefixes = new ArrayList<>();
+               String line = pckIn.readString();
+               // Currently, we do not support any capabilities, so the next
+               // line is DELIM if there are arguments or END if not.
+               if (line == PacketLineIn.DELIM) {
+                       while ((line = pckIn.readString()) != PacketLineIn.END) {
+                               if (line.equals("peel")) { //$NON-NLS-1$
+                                       builder.setPeel(true);
+                               } else if (line.equals("symrefs")) { //$NON-NLS-1$
+                                       builder.setSymrefs(true);
+                               } else if (line.startsWith("ref-prefix ")) { //$NON-NLS-1$
+                                       prefixes.add(line.substring("ref-prefix ".length())); //$NON-NLS-1$
+                               } else {
+                                       throw new PackProtocolException(MessageFormat
+                                                       .format(JGitText.get().unexpectedPacketLine, line));
+                               }
+                       }
+               } else if (line != PacketLineIn.END) {
+                       throw new PackProtocolException(MessageFormat
+                                       .format(JGitText.get().unexpectedPacketLine, line));
+               }
+
+               return builder.setRefPrefixes(prefixes).build();
+       }
+
+       /*
         * Process the content of "filter" line from the protocol. It has a shape
         * like "blob:none" or "blob:limit=N", with limit a positive number.
         *
index 6c6b44792e5cd5b88d68c5840ff0e6d193a7b911..3e54e2a5da0cab3623409c88c01931906790235c 100644 (file)
@@ -907,30 +907,8 @@ public class UploadPack {
        }
 
        private void lsRefsV2() throws IOException {
-               LsRefsV2Request.Builder builder = LsRefsV2Request.builder();
-               List<String> prefixes = new ArrayList<>();
-               String line = pckIn.readString();
-               // Currently, we do not support any capabilities, so the next
-               // line is DELIM if there are arguments or END if not.
-               if (line == PacketLineIn.DELIM) {
-                       while ((line = pckIn.readString()) != PacketLineIn.END) {
-                               if (line.equals("peel")) { //$NON-NLS-1$
-                                       builder.setPeel(true);
-                               } else if (line.equals("symrefs")) { //$NON-NLS-1$
-                                       builder.setSymrefs(true);
-                               } else if (line.startsWith("ref-prefix ")) { //$NON-NLS-1$
-                                       prefixes.add(line.substring("ref-prefix ".length())); //$NON-NLS-1$
-                               } else {
-                                       throw new PackProtocolException(MessageFormat
-                                                       .format(JGitText.get().unexpectedPacketLine, line));
-                               }
-                       }
-               } else if (line != PacketLineIn.END) {
-                       throw new PackProtocolException(MessageFormat
-                                       .format(JGitText.get().unexpectedPacketLine, line));
-               }
-               LsRefsV2Request req = builder.setRefPrefixes(prefixes).build();
-
+               ProtocolV2Parser parser = new ProtocolV2Parser(transferConfig);
+               LsRefsV2Request req = parser.parseLsRefsRequest(pckIn);
                protocolV2Hook.onLsRefs(req);
 
                rawOut.stopBuffering();