Turn `git serve` into a test helper
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Thu, 18 Apr 2019 13:16:51 +0000 (06:16 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Apr 2019 05:03:24 +0000 (14:03 +0900)
The `git serve` built-in was introduced in ed10cb952d31 (serve:
introduce git-serve, 2018-03-15) as a backend to serve Git protocol v2,
probably originally intended to be spawned by `git upload-pack`.

However, in the version that the protocol v2 patches made it into core
Git, `git upload-pack` calls the `serve()` function directly instead of
spawning `git serve`; The only reason in life for `git serve` to survive
as a built-in command is to provide a way to test the protocol v2
functionality.

Meaning that it does not even have to be a built-in that is installed
with end-user facing Git installations, but it can be a test helper
instead.

Let's make it so.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
builtin.h
builtin/serve.c [deleted file]
git.c
t/helper/test-serve-v2.c [new file with mode: 0644]
t/helper/test-tool.c
t/helper/test-tool.h
t/t5701-git-serve.sh
t/t5702-protocol-v2.sh
t/t5703-upload-pack-ref-in-want.sh
index f5fc977ee8f3d2da9c07b27147fd93589d9d0c53..9ce62fa2958b9fe3aefcf739d1b1ab56d0a9d3d8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -758,6 +758,7 @@ TEST_BUILTINS_OBJS += test-repository.o
 TEST_BUILTINS_OBJS += test-revision-walking.o
 TEST_BUILTINS_OBJS += test-run-command.o
 TEST_BUILTINS_OBJS += test-scrap-cache-tree.o
+TEST_BUILTINS_OBJS += test-serve-v2.o
 TEST_BUILTINS_OBJS += test-sha1.o
 TEST_BUILTINS_OBJS += test-sha1-array.o
 TEST_BUILTINS_OBJS += test-sha256.o
@@ -1127,7 +1128,6 @@ BUILTIN_OBJS += builtin/rev-parse.o
 BUILTIN_OBJS += builtin/revert.o
 BUILTIN_OBJS += builtin/rm.o
 BUILTIN_OBJS += builtin/send-pack.o
-BUILTIN_OBJS += builtin/serve.o
 BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-index.o
index 6538932e99a72f1be97c41806ca6e394f18f9a0c..c48636e2448d8b7f6da3ae171766682dba0f4f1a 100644 (file)
--- a/builtin.h
+++ b/builtin.h
@@ -219,7 +219,6 @@ extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
 extern int cmd_revert(int argc, const char **argv, const char *prefix);
 extern int cmd_rm(int argc, const char **argv, const char *prefix);
 extern int cmd_send_pack(int argc, const char **argv, const char *prefix);
-extern int cmd_serve(int argc, const char **argv, const char *prefix);
 extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
 extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
diff --git a/builtin/serve.c b/builtin/serve.c
deleted file mode 100644 (file)
index d3fd240..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "cache.h"
-#include "builtin.h"
-#include "parse-options.h"
-#include "serve.h"
-
-static char const * const serve_usage[] = {
-       N_("git serve [<options>]"),
-       NULL
-};
-
-int cmd_serve(int argc, const char **argv, const char *prefix)
-{
-       struct serve_options opts = SERVE_OPTIONS_INIT;
-
-       struct option options[] = {
-               OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc,
-                        N_("quit after a single request/response exchange")),
-               OPT_BOOL(0, "advertise-capabilities", &opts.advertise_capabilities,
-                        N_("exit immediately after advertising capabilities")),
-               OPT_END()
-       };
-
-       /* ignore all unknown cmdline switches for now */
-       argc = parse_options(argc, argv, prefix, options, serve_usage,
-                            PARSE_OPT_KEEP_DASHDASH |
-                            PARSE_OPT_KEEP_UNKNOWN);
-       serve(&opts);
-
-       return 0;
-}
diff --git a/git.c b/git.c
index 2dd588674f621e2df2af1a3833c2cb3fa8417de4..c58b067771cfd92593e4812ee373fd0e201b72a0 100644 (file)
--- a/git.c
+++ b/git.c
@@ -548,7 +548,6 @@ static struct cmd_struct commands[] = {
        { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
        { "rm", cmd_rm, RUN_SETUP },
        { "send-pack", cmd_send_pack, RUN_SETUP },
-       { "serve", cmd_serve, RUN_SETUP },
        { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
        { "show", cmd_show, RUN_SETUP },
        { "show-branch", cmd_show_branch, RUN_SETUP },
diff --git a/t/helper/test-serve-v2.c b/t/helper/test-serve-v2.c
new file mode 100644 (file)
index 0000000..aee35e5
--- /dev/null
@@ -0,0 +1,31 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "parse-options.h"
+#include "serve.h"
+
+static char const * const serve_usage[] = {
+       N_("test-tool serve-v2 [<options>]"),
+       NULL
+};
+
+int cmd__serve_v2(int argc, const char **argv)
+{
+       struct serve_options opts = SERVE_OPTIONS_INIT;
+
+       struct option options[] = {
+               OPT_BOOL(0, "stateless-rpc", &opts.stateless_rpc,
+                        N_("quit after a single request/response exchange")),
+               OPT_BOOL(0, "advertise-capabilities", &opts.advertise_capabilities,
+                        N_("exit immediately after advertising capabilities")),
+               OPT_END()
+       };
+       const char *prefix = setup_git_directory();
+
+       /* ignore all unknown cmdline switches for now */
+       argc = parse_options(argc, argv, prefix, options, serve_usage,
+                            PARSE_OPT_KEEP_DASHDASH |
+                            PARSE_OPT_KEEP_UNKNOWN);
+       serve(&opts);
+
+       return 0;
+}
index 2b21943f9352a62e3929fed5b345e8822db63ad2..4bf3666b43d318820d847e9969d00105f41c5972 100644 (file)
@@ -48,6 +48,7 @@ static struct test_cmd cmds[] = {
        { "revision-walking", cmd__revision_walking },
        { "run-command", cmd__run_command },
        { "scrap-cache-tree", cmd__scrap_cache_tree },
+       { "serve-v2", cmd__serve_v2 },
        { "sha1", cmd__sha1 },
        { "sha1-array", cmd__sha1_array },
        { "sha256", cmd__sha256 },
index 25abed1cf2acc9e84cf1ed78eebe97c7c4d728ca..bc72370916e44e79eeae81d79bf16c036b77a763 100644 (file)
@@ -39,6 +39,7 @@ int cmd__repository(int argc, const char **argv);
 int cmd__revision_walking(int argc, const char **argv);
 int cmd__run_command(int argc, const char **argv);
 int cmd__scrap_cache_tree(int argc, const char **argv);
+int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_array(int argc, const char **argv);
 int cmd__sha256(int argc, const char **argv);
index fe45bf828d09cd9d95a66742ef938837d35141a3..ffb9613885904eaf474a726f6124032d13db9753 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-test_description='test git-serve and server commands'
+test_description='test protocol v2 server commands'
 
 . ./test-lib.sh
 
@@ -14,7 +14,8 @@ test_expect_success 'test capability advertisement' '
        0000
        EOF
 
-       GIT_TEST_SIDEBAND_ALL=0 git serve --advertise-capabilities >out &&
+       GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
+               --advertise-capabilities >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -24,11 +25,11 @@ test_expect_success 'stateless-rpc flag does not list capabilities' '
        test-tool pkt-line pack >in <<-EOF &&
        0000
        EOF
-       git serve --stateless-rpc >out <in &&
+       test-tool serve-v2 --stateless-rpc >out <in &&
        test_must_be_empty out &&
 
        # EOF
-       git serve --stateless-rpc >out &&
+       test-tool serve-v2 --stateless-rpc >out &&
        test_must_be_empty out
 '
 
@@ -37,7 +38,7 @@ test_expect_success 'request invalid capability' '
        foobar
        0000
        EOF
-       test_must_fail git serve --stateless-rpc 2>err <in &&
+       test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
        test_i18ngrep "unknown capability" err
 '
 
@@ -46,7 +47,7 @@ test_expect_success 'request with no command' '
        agent=git/test
        0000
        EOF
-       test_must_fail git serve --stateless-rpc 2>err <in &&
+       test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
        test_i18ngrep "no command requested" err
 '
 
@@ -56,7 +57,7 @@ test_expect_success 'request invalid command' '
        agent=git/test
        0000
        EOF
-       test_must_fail git serve --stateless-rpc 2>err <in &&
+       test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
        test_i18ngrep "invalid command" err
 '
 
@@ -87,7 +88,7 @@ test_expect_success 'basics of ls-refs' '
        0000
        EOF
 
-       git serve --stateless-rpc <in >out &&
+       test-tool serve-v2 --stateless-rpc <in >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -107,7 +108,7 @@ test_expect_success 'basic ref-prefixes' '
        0000
        EOF
 
-       git serve --stateless-rpc <in >out &&
+       test-tool serve-v2 --stateless-rpc <in >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -127,7 +128,7 @@ test_expect_success 'refs/heads prefix' '
        0000
        EOF
 
-       git serve --stateless-rpc <in >out &&
+       test-tool serve-v2 --stateless-rpc <in >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -148,7 +149,7 @@ test_expect_success 'peel parameter' '
        0000
        EOF
 
-       git serve --stateless-rpc <in >out &&
+       test-tool serve-v2 --stateless-rpc <in >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -169,7 +170,7 @@ test_expect_success 'symrefs parameter' '
        0000
        EOF
 
-       git serve --stateless-rpc <in >out &&
+       test-tool serve-v2 --stateless-rpc <in >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -189,7 +190,7 @@ test_expect_success 'sending server-options' '
        0000
        EOF
 
-       git serve --stateless-rpc <in >out &&
+       test-tool serve-v2 --stateless-rpc <in >out &&
        test-tool pkt-line unpack <out >actual &&
        test_cmp expect actual
 '
@@ -204,7 +205,10 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
        0000
        EOF
 
-       test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
+       (
+               cd server &&
+               test_must_fail test-tool serve-v2 --stateless-rpc
+       ) <in >/dev/null 2>err &&
        grep "unexpected line: .this-is-not-a-command." err
 '
 
index db4ae09f2f81ff77b9c29b99c21363e66c81f222..8691bfc52dafa545f5987b0d240ad965064f00cc 100755 (executable)
@@ -359,12 +359,13 @@ test_expect_success 'even with handcrafted request, filter does not work if not
        0000
        EOF
 
-       test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
+       test_must_fail test-tool -C server serve-v2 --stateless-rpc \
+               <in >/dev/null 2>err &&
        grep "unexpected line: .filter blob:none." err &&
 
        # Exercise to ensure that if advertised, filter works
        git -C server config uploadpack.allowfilter 1 &&
-       git -C server serve --stateless-rpc <in >/dev/null
+       test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
 '
 
 test_expect_success 'default refspec is used to filter ref when fetchcing' '
index f87b2f6df329975e243ab9c00b510ee4cc588d0b..dd1cbd0dd67e307d497c3b6d3695ecb9b0a4c5a8 100755 (executable)
@@ -48,15 +48,15 @@ test_expect_success 'setup repository' '
 '
 
 test_expect_success 'config controls ref-in-want advertisement' '
-       git serve --advertise-capabilities >out &&
+       test-tool serve-v2 --advertise-capabilities >out &&
        ! grep -a ref-in-want out &&
 
        git config uploadpack.allowRefInWant false &&
-       git serve --advertise-capabilities >out &&
+       test-tool serve-v2 --advertise-capabilities >out &&
        ! grep -a ref-in-want out &&
 
        git config uploadpack.allowRefInWant true &&
-       git serve --advertise-capabilities >out &&
+       test-tool serve-v2 --advertise-capabilities >out &&
        grep -a ref-in-want out
 '
 
@@ -70,7 +70,7 @@ test_expect_success 'invalid want-ref line' '
        0000
        EOF
 
-       test_must_fail git serve --stateless-rpc 2>out <in &&
+       test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
        grep "unknown ref" out
 '
 
@@ -90,7 +90,7 @@ test_expect_success 'basic want-ref' '
        0000
        EOF
 
-       git serve --stateless-rpc >out <in &&
+       test-tool serve-v2 --stateless-rpc >out <in &&
        check_output
 '
 
@@ -112,7 +112,7 @@ test_expect_success 'multiple want-ref lines' '
        0000
        EOF
 
-       git serve --stateless-rpc >out <in &&
+       test-tool serve-v2 --stateless-rpc >out <in &&
        check_output
 '
 
@@ -133,7 +133,7 @@ test_expect_success 'mix want and want-ref' '
        0000
        EOF
 
-       git serve --stateless-rpc >out <in &&
+       test-tool serve-v2 --stateless-rpc >out <in &&
        check_output
 '
 
@@ -153,7 +153,7 @@ test_expect_success 'want-ref with ref we already have commit for' '
        0000
        EOF
 
-       git serve --stateless-rpc >out <in &&
+       test-tool serve-v2 --stateless-rpc >out <in &&
        check_output
 '