]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repo: add the flag -z as an alias for --format=nul
authorLucas Seiki Oshiro <lucasseikioshiro@gmail.com>
Thu, 4 Sep 2025 13:40:16 +0000 (10:40 -0300)
committerJunio C Hamano <gitster@pobox.com>
Thu, 4 Sep 2025 18:36:39 +0000 (11:36 -0700)
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>
Documentation/git-repo.adoc
builtin/repo.c
t/t1900-repo.sh

index 2870828d936192c7aa5613bbd86c57a573c7b1ee..6f5ee882157c1657495a3397dd9f4af0374a0c88 100644 (file)
@@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository
 SYNOPSIS
 --------
 [synopsis]
-git repo info [--format=(keyvalue|nul)] [<key>...]
+git repo info [--format=(keyvalue|nul)] [-z] [<key>...]
 
 DESCRIPTION
 -----------
@@ -18,7 +18,7 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
 
 COMMANDS
 --------
-`info [--format=(keyvalue|nul)] [<key>...]`::
+`info [--format=(keyvalue|nul)] [-z] [<key>...]`::
        Retrieve metadata-related information about the current repository. Only
        the requested data will be returned based on their keys (see "INFO KEYS"
        section below).
@@ -40,6 +40,8 @@ supported:
        between the key and the value and using a NUL character after each value.
        This format is better suited for being parsed by another applications than
        `keyvalue`. Unlike in the `keyvalue` format, the values are never quoted.
++
+`-z` is an alias for `--format=nul`.
 
 INFO KEYS
 ---------
index 8c6e7f42aba1070d0d4f14208cf35b0dbf74f970..dc9a2674694667f0743703178a1bf21654b72657 100644 (file)
@@ -9,7 +9,7 @@
 #include "shallow.h"
 
 static const char *const repo_usage[] = {
-       "git repo info [--format=(keyvalue|nul)] [<key>...]",
+       "git repo info [--format=(keyvalue|nul)] [-z] [<key>...]",
        NULL
 };
 
@@ -112,26 +112,40 @@ static int print_fields(int argc, const char **argv,
        return ret;
 }
 
+static int parse_format_cb(const struct option *opt,
+                          const char *arg, int unset UNUSED)
+{
+       enum output_format *format = opt->value;
+
+       if (opt->short_name == 'z')
+               *format = FORMAT_NUL_TERMINATED;
+       else if (!strcmp(arg, "nul"))
+               *format = FORMAT_NUL_TERMINATED;
+       else if (!strcmp(arg, "keyvalue"))
+               *format = FORMAT_KEYVALUE;
+       else
+               die(_("invalid format '%s'"), arg);
+
+       return 0;
+}
+
 static int repo_info(int argc, const char **argv, const char *prefix,
                     struct repository *repo)
 {
-       const char *format_str = "keyvalue";
-       enum output_format format;
+       enum output_format format = FORMAT_KEYVALUE;
        struct option options[] = {
-               OPT_STRING(0, "format", &format_str, N_("format"),
-                          N_("output format")),
+               OPT_CALLBACK_F(0, "format", &format, N_("format"),
+                              N_("output format"),
+                              PARSE_OPT_NONEG, parse_format_cb),
+               OPT_CALLBACK_F('z', NULL, &format, NULL,
+                              N_("synonym for --format=nul"),
+                              PARSE_OPT_NONEG | PARSE_OPT_NOARG,
+                              parse_format_cb),
                OPT_END()
        };
 
        argc = parse_options(argc, argv, prefix, options, repo_usage, 0);
 
-       if (!strcmp(format_str, "keyvalue"))
-               format = FORMAT_KEYVALUE;
-       else if (!strcmp(format_str, "nul"))
-               format = FORMAT_NUL_TERMINATED;
-       else
-               die(_("invalid format '%s'"), format_str);
-
        return print_fields(argc, argv, repo, format);
 }
 
index a69c715357e26b222a242bf2faee20a855b48d5e..ddf788d5a263181aeba81cf7892a56efa570cf77 100755 (executable)
@@ -92,4 +92,16 @@ test_expect_success 'git-repo-info aborts when requesting an invalid format' '
        test_cmp expect actual
 '
 
+test_expect_success '-z uses nul-terminated format' '
+       printf "layout.bare\nfalse\0layout.shallow\nfalse\0" >expected &&
+       git repo info -z layout.bare layout.shallow >actual &&
+       test_cmp expected actual
+'
+
+test_expect_success 'git repo info uses the last requested format' '
+       echo "layout.bare=false" >expected &&
+       git repo info --format=nul -z --format=keyvalue layout.bare >actual &&
+       test_cmp expected actual
+'
+
 test_done