env--helper: new undocumented builtin wrapping git_env_*()

We have many GIT_TEST_* variables that accept a <boolean> because they're implemented in C, and then some that take <non-empty?> because they're implemented at least partially in shellscript. Add a helper that wraps git_env_bool() and git_env_ulong() as the first step in fixing this. This isn't being added as a test-tool mode because some of these are used outside the test suite. Part of what this tool does can be done via a trick with "git config" added in 83d842dc8c ("tests: turn on network daemon tests by default", 2014-02-10) for test_tristate(), i.e.: git -c magic.variable="$1" config --bool magic.variable 2>/dev/null But as subsequent changes will show being able to pass along the default value makes all the difference, and we'll be able to replace test_tristate() itself with that. The --type=bool option will be used by subsequent patches, but not --type=ulong. I figured it was easy enough to add it & test for it so I left it in so we'd have wrappers for both git_env_*() functions, and to have a template to make it obvious how we'd add --type=int etc. if it's needed in the future. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 21, 2019 at 12:18 UTC b4f207f339469e604260bdf6da8673db9c9c9105
6 files changed +182
.gitignore
+1
@@ -58,6 +58,7 @@
58 /git-difftool
59 /git-difftool--helper
60 /git-describe
61 +/git-env--helper
62 /git-fast-export
63 /git-fast-import
64 /git-fetch
Makefile
+1
@@ -1059,6 +1059,7 @@ BUILTIN_OBJS += builtin/diff-index.o
1059 BUILTIN_OBJS += builtin/diff-tree.o
1060 BUILTIN_OBJS += builtin/diff.o
1061 BUILTIN_OBJS += builtin/difftool.o
1062 +BUILTIN_OBJS += builtin/env--helper.o
1063 BUILTIN_OBJS += builtin/fast-export.o
1064 BUILTIN_OBJS += builtin/fetch-pack.o
1065 BUILTIN_OBJS += builtin/fetch.o
builtin.h
+1
@@ -160,6 +160,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix);
160 int cmd_diff(int argc, const char **argv, const char *prefix);
161 int cmd_diff_tree(int argc, const char **argv, const char *prefix);
162 int cmd_difftool(int argc, const char **argv, const char *prefix);
163 +int cmd_env__helper(int argc, const char **argv, const char *prefix);
164 int cmd_fast_export(int argc, const char **argv, const char *prefix);
165 int cmd_fetch(int argc, const char **argv, const char *prefix);
166 int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
builtin/env--helper.c new
+95
@@ -0,0 +1,95 @@
1 +#include "builtin.h"
2 +#include "config.h"
3 +#include "parse-options.h"
4 +
5 +static char const * const env__helper_usage[] = {
6 + N_("git env--helper --type=[bool|ulong] <options> <env-var>"),
7 + NULL
8 +};
9 +
10 +enum {
11 + ENV_HELPER_TYPE_BOOL = 1,
12 + ENV_HELPER_TYPE_ULONG
13 +} cmdmode = 0;
14 +
15 +static int option_parse_type(const struct option *opt, const char *arg,
16 + int unset)
17 +{
18 + if (!strcmp(arg, "bool"))
19 + cmdmode = ENV_HELPER_TYPE_BOOL;
20 + else if (!strcmp(arg, "ulong"))
21 + cmdmode = ENV_HELPER_TYPE_ULONG;
22 + else
23 + die(_("unrecognized --type argument, %s"), arg);
24 +
25 + return 0;
26 +}
27 +
28 +int cmd_env__helper(int argc, const char **argv, const char *prefix)
29 +{
30 + int exit_code = 0;
31 + const char *env_variable = NULL;
32 + const char *env_default = NULL;
33 + int ret;
34 + int ret_int, default_int;
35 + unsigned long ret_ulong, default_ulong;
36 + struct option opts[] = {
37 + OPT_CALLBACK_F(0, "type", &cmdmode, N_("type"),
38 + N_("value is given this type"), PARSE_OPT_NONEG,
39 + option_parse_type),
40 + OPT_STRING(0, "default", &env_default, N_("value"),
41 + N_("default for git_env_*(...) to fall back on")),
42 + OPT_BOOL(0, "exit-code", &exit_code,
43 + N_("be quiet only use git_env_*() value as exit code")),
44 + OPT_END(),
45 + };
46 +
47 + argc = parse_options(argc, argv, prefix, opts, env__helper_usage,
48 + PARSE_OPT_KEEP_UNKNOWN);
49 + if (env_default && !*env_default)
50 + usage_with_options(env__helper_usage, opts);
51 + if (!cmdmode)
52 + usage_with_options(env__helper_usage, opts);
53 + if (argc != 1)
54 + usage_with_options(env__helper_usage, opts);
55 + env_variable = argv[0];
56 +
57 + switch (cmdmode) {
58 + case ENV_HELPER_TYPE_BOOL:
59 + if (env_default) {
60 + default_int = git_parse_maybe_bool(env_default);
61 + if (default_int == -1) {
62 + error(_("option `--default' expects a boolean value with `--type=bool`, not `%s`"),
63 + env_default);
64 + usage_with_options(env__helper_usage, opts);
65 + }
66 + } else {
67 + default_int = 0;
68 + }
69 + ret_int = git_env_bool(env_variable, default_int);
70 + if (!exit_code)
71 + puts(ret_int ? "true" : "false");
72 + ret = ret_int;
73 + break;
74 + case ENV_HELPER_TYPE_ULONG:
75 + if (env_default) {
76 + if (!git_parse_ulong(env_default, &default_ulong)) {
77 + error(_("option `--default' expects an unsigned long value with `--type=ulong`, not `%s`"),
78 + env_default);
79 + usage_with_options(env__helper_usage, opts);
80 + }
81 + } else {
82 + default_ulong = 0;
83 + }
84 + ret_ulong = git_env_ulong(env_variable, default_ulong);
85 + if (!exit_code)
86 + printf("%lu\n", ret_ulong);
87 + ret = ret_ulong;
88 + break;
89 + default:
90 + BUG("unknown <type> value");
91 + break;
92 + }
93 +
94 + return !ret;
95 +}
git.c
+1
@@ -500,6 +500,7 @@ static struct cmd_struct commands[] = {
500 { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
501 { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
502 { "difftool", cmd_difftool, RUN_SETUP_GENTLY },
503 + { "env--helper", cmd_env__helper },
504 { "fast-export", cmd_fast_export, RUN_SETUP },
505 { "fetch", cmd_fetch, RUN_SETUP },
506 { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
t/t0017-env-helper.sh new
+83
@@ -0,0 +1,83 @@
1 +#!/bin/sh
2 +
3 +test_description='test env--helper'
4 +
5 +. ./test-lib.sh
6 +
7 +
8 +test_expect_success 'env--helper usage' '
9 + test_must_fail git env--helper &&
10 + test_must_fail git env--helper --type=bool &&
11 + test_must_fail git env--helper --type=ulong &&
12 + test_must_fail git env--helper --type=bool &&
13 + test_must_fail git env--helper --type=bool --default &&
14 + test_must_fail git env--helper --type=bool --default= &&
15 + test_must_fail git env--helper --defaultxyz
16 +'
17 +
18 +test_expect_success 'env--helper bad default values' '
19 + test_must_fail git env--helper --type=bool --default=1xyz MISSING &&
20 + test_must_fail git env--helper --type=ulong --default=1xyz MISSING
21 +'
22 +
23 +test_expect_success 'env--helper --type=bool' '
24 + # Test various --default bool values
25 + echo true >expected &&
26 + git env--helper --type=bool --default=1 MISSING >actual &&
27 + test_cmp expected actual &&
28 + git env--helper --type=bool --default=yes MISSING >actual &&
29 + test_cmp expected actual &&
30 + git env--helper --type=bool --default=true MISSING >actual &&
31 + test_cmp expected actual &&
32 + echo false >expected &&
33 + test_must_fail git env--helper --type=bool --default=0 MISSING >actual &&
34 + test_cmp expected actual &&
35 + test_must_fail git env--helper --type=bool --default=no MISSING >actual &&
36 + test_cmp expected actual &&
37 + test_must_fail git env--helper --type=bool --default=false MISSING >actual &&
38 + test_cmp expected actual &&
39 +
40 + # No output with --exit-code
41 + git env--helper --type=bool --default=true --exit-code MISSING >actual.out 2>actual.err &&
42 + test_must_be_empty actual.out &&
43 + test_must_be_empty actual.err &&
44 + test_must_fail git env--helper --type=bool --default=false --exit-code MISSING >actual.out 2>actual.err &&
45 + test_must_be_empty actual.out &&
46 + test_must_be_empty actual.err &&
47 +
48 + # Existing variable
49 + EXISTS=true git env--helper --type=bool --default=false --exit-code EXISTS >actual.out 2>actual.err &&
50 + test_must_be_empty actual.out &&
51 + test_must_be_empty actual.err &&
52 + test_must_fail \
53 + env EXISTS=false \
54 + git env--helper --type=bool --default=true --exit-code EXISTS >actual.out 2>actual.err &&
55 + test_must_be_empty actual.out &&
56 + test_must_be_empty actual.err
57 +'
58 +
59 +test_expect_success 'env--helper --type=ulong' '
60 + echo 1234567890 >expected &&
61 + git env--helper --type=ulong --default=1234567890 MISSING >actual.out 2>actual.err &&
62 + test_cmp expected actual.out &&
63 + test_must_be_empty actual.err &&
64 +
65 + echo 0 >expected &&
66 + test_must_fail git env--helper --type=ulong --default=0 MISSING >actual &&
67 + test_cmp expected actual &&
68 +
69 + git env--helper --type=ulong --default=1234567890 --exit-code MISSING >actual.out 2>actual.err &&
70 + test_must_be_empty actual.out &&
71 + test_must_be_empty actual.err &&
72 +
73 + EXISTS=1234567890 git env--helper --type=ulong --default=0 EXISTS --exit-code >actual.out 2>actual.err &&
74 + test_must_be_empty actual.out &&
75 + test_must_be_empty actual.err &&
76 +
77 + echo 1234567890 >expected &&
78 + EXISTS=1234567890 git env--helper --type=ulong --default=0 EXISTS >actual.out 2>actual.err &&
79 + test_cmp expected actual.out &&
80 + test_must_be_empty actual.err
81 +'
82 +
83 +test_done