Git fork

repo: add the flag -z as an alias for --format=nul

Other Git commands that have nul-terminated output (e.g. git-config,
git-status, git-ls-files) have a flag `-z` for using the null character
as the record separator.

Add the `-z` flag to git-repo-info as an alias for `--format=nul`,
making it consistent with the behavior of the other commands.

Mentored-by: Karthik Nayak <karthik.188@gmail.com>
Mentored-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Lucas Seiki Oshiro and committed by
Junio C Hamano
a92f5ca0 c8f660a7

+42 -14
+4 -2
Documentation/git-repo.adoc
··· 8 SYNOPSIS 9 -------- 10 [synopsis] 11 - git repo info [--format=(keyvalue|nul)] [<key>...] 12 13 DESCRIPTION 14 ----------- ··· 18 19 COMMANDS 20 -------- 21 - `info [--format=(keyvalue|nul)] [<key>...]`:: 22 Retrieve metadata-related information about the current repository. Only 23 the requested data will be returned based on their keys (see "INFO KEYS" 24 section below). ··· 40 between the key and the value and using a NUL character after each value. 41 This format is better suited for being parsed by another applications than 42 `keyvalue`. Unlike in the `keyvalue` format, the values are never quoted. 43 44 INFO KEYS 45 ---------
··· 8 SYNOPSIS 9 -------- 10 [synopsis] 11 + git repo info [--format=(keyvalue|nul)] [-z] [<key>...] 12 13 DESCRIPTION 14 ----------- ··· 18 19 COMMANDS 20 -------- 21 + `info [--format=(keyvalue|nul)] [-z] [<key>...]`:: 22 Retrieve metadata-related information about the current repository. Only 23 the requested data will be returned based on their keys (see "INFO KEYS" 24 section below). ··· 40 between the key and the value and using a NUL character after each value. 41 This format is better suited for being parsed by another applications than 42 `keyvalue`. Unlike in the `keyvalue` format, the values are never quoted. 43 + + 44 + `-z` is an alias for `--format=nul`. 45 46 INFO KEYS 47 ---------
+26 -12
builtin/repo.c
··· 9 #include "shallow.h" 10 11 static const char *const repo_usage[] = { 12 - "git repo info [--format=(keyvalue|nul)] [<key>...]", 13 NULL 14 }; 15 ··· 112 return ret; 113 } 114 115 static int repo_info(int argc, const char **argv, const char *prefix, 116 struct repository *repo) 117 { 118 - const char *format_str = "keyvalue"; 119 - enum output_format format; 120 struct option options[] = { 121 - OPT_STRING(0, "format", &format_str, N_("format"), 122 - N_("output format")), 123 OPT_END() 124 }; 125 126 argc = parse_options(argc, argv, prefix, options, repo_usage, 0); 127 - 128 - if (!strcmp(format_str, "keyvalue")) 129 - format = FORMAT_KEYVALUE; 130 - else if (!strcmp(format_str, "nul")) 131 - format = FORMAT_NUL_TERMINATED; 132 - else 133 - die(_("invalid format '%s'"), format_str); 134 135 return print_fields(argc, argv, repo, format); 136 }
··· 9 #include "shallow.h" 10 11 static const char *const repo_usage[] = { 12 + "git repo info [--format=(keyvalue|nul)] [-z] [<key>...]", 13 NULL 14 }; 15 ··· 112 return ret; 113 } 114 115 + static int parse_format_cb(const struct option *opt, 116 + const char *arg, int unset UNUSED) 117 + { 118 + enum output_format *format = opt->value; 119 + 120 + if (opt->short_name == 'z') 121 + *format = FORMAT_NUL_TERMINATED; 122 + else if (!strcmp(arg, "nul")) 123 + *format = FORMAT_NUL_TERMINATED; 124 + else if (!strcmp(arg, "keyvalue")) 125 + *format = FORMAT_KEYVALUE; 126 + else 127 + die(_("invalid format '%s'"), arg); 128 + 129 + return 0; 130 + } 131 + 132 static int repo_info(int argc, const char **argv, const char *prefix, 133 struct repository *repo) 134 { 135 + enum output_format format = FORMAT_KEYVALUE; 136 struct option options[] = { 137 + OPT_CALLBACK_F(0, "format", &format, N_("format"), 138 + N_("output format"), 139 + PARSE_OPT_NONEG, parse_format_cb), 140 + OPT_CALLBACK_F('z', NULL, &format, NULL, 141 + N_("synonym for --format=nul"), 142 + PARSE_OPT_NONEG | PARSE_OPT_NOARG, 143 + parse_format_cb), 144 OPT_END() 145 }; 146 147 argc = parse_options(argc, argv, prefix, options, repo_usage, 0); 148 149 return print_fields(argc, argv, repo, format); 150 }
+12
t/t1900-repo.sh
··· 92 test_cmp expect actual 93 ' 94 95 test_done
··· 92 test_cmp expect actual 93 ' 94 95 + test_expect_success '-z uses nul-terminated format' ' 96 + printf "layout.bare\nfalse\0layout.shallow\nfalse\0" >expected && 97 + git repo info -z layout.bare layout.shallow >actual && 98 + test_cmp expected actual 99 + ' 100 + 101 + test_expect_success 'git repo info uses the last requested format' ' 102 + echo "layout.bare=false" >expected && 103 + git repo info --format=nul -z --format=keyvalue layout.bare >actual && 104 + test_cmp expected actual 105 + ' 106 + 107 test_done