threads / patch / 64559

patchrepo: add -z as an alias for --format=nul to git-repo-structure

Subject: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

## tl;dr

15 messages between Nov 30, 2025 and Dec 18, 2025. Diffs are folded; open one to read it.

replies: 14people: 4as markdown or json

Lucas Seiki Oshiro· Nov 30, 2025, 19:59 UTC · lore

Other Git commands that have nul-terminated output, such as git-config, git-status, git-ls-files, and git-repo-info have a flag `-z` for using the null character as the record separator.

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

Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
---
Hello, again!

Since git-repo-info and git-repo-structure share the same format parser, this could be done by only copying the OPT_CALLBACK_F from repo_info.

I'm cc'ing Justin here, as he was the author of git-repo-structure.

This patch is based on top of master b31ab939fe (The fourth batch, 2025-11-26) with lo/repo-info-all merged.

 Documentation/git-repo.adoc | 5 +++--
 builtin/repo.c              | 6 +++++-
 t/t1901-repo-structure.sh   | 6 ++++++
 3 files changed, 14 insertions(+), 3 deletions(-)
Show changes to 3 files +14 −3

Documentation/git-repo.adoc, builtin/repo.c, t/t1901-repo-structure.sh

diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc
index 70f0a6d2e4..8820954f7e 100644
--- a/Documentation/git-repo.adoc
+++ b/Documentation/git-repo.adoc
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [synopsis]
 git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]
-git repo structure [--format=(table|keyvalue|nul)]
+git repo structure [--format=(table|keyvalue|nul)] [-z]
 
 DESCRIPTION
 -----------
@@ -44,7 +44,7 @@ supported:
 +
 `-z` is an alias for `--format=nul`.
 
-`structure [--format=(table|keyvalue|nul)]`::
+`structure [--format=(table|keyvalue|nul)] [-z]`::
 	Retrieve statistics about the current repository structure. The
 	following kinds of information are reported:
 +
@@ -72,6 +72,7 @@ supported:
 	the delimiter between the key and value instead of '='. Unlike the
 	`keyvalue` format, values containing "unusual" characters are never
 	quoted.
+`-z` is an alias for `--format=nul`.
 
 INFO KEYS
 ---------
diff --git a/builtin/repo.c b/builtin/repo.c
index 2a653bd3ea..1c06207a39 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -16,7 +16,7 @@
 
 static const char *const repo_usage[] = {
 	"git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]",
-	"git repo structure [--format=(table|keyvalue|nul)]",
+	"git repo structure [--format=(table|keyvalue|nul)] [-z]",
 	NULL
 };
 
@@ -529,6 +529,10 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
 		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_BOOL(0, "progress", &show_progress, N_("show progress")),
 		OPT_END()
 	};
diff --git a/t/t1901-repo-structure.sh b/t/t1901-repo-structure.sh
index 36a71a144e..5a50acf345 100755
--- a/t/t1901-repo-structure.sh
+++ b/t/t1901-repo-structure.sh
@@ -101,6 +101,12 @@ test_expect_success 'keyvalue and nul format' '
 		tr "\n=" "\0\n" <expect >expect_nul &&
 		git repo structure --format=nul >out 2>err &&
 
+		test_cmp expect_nul out &&
+		test_line_count = 0 err &&
+
+		# Check -z
+		git repo structure --format=nul >out 2>err &&
+
 		test_cmp expect_nul out &&
 		test_line_count = 0 err
 	)
-- 
2.50.1 (Apple Git-155)
Junio C Hamano· Dec 1, 2025, 02:21 UTC · re: Lucas Seiki Oshiro · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> writes:
> -git repo structure [--format=(table|keyvalue|nul)]
> +git repo structure [--format=(table|keyvalue|nul)] [-z]
It's a minor thing but I wonder if this should be more like
    git repo structure [--format=(table|keyvalue|nul) | -z]
Show 13 quoted lines
> diff --git a/t/t1901-repo-structure.sh b/t/t1901-repo-structure.sh
> index 36a71a144e..5a50acf345 100755
> --- a/t/t1901-repo-structure.sh
> +++ b/t/t1901-repo-structure.sh
> @@ -101,6 +101,12 @@ test_expect_success 'keyvalue and nul format' '
>  		tr "\n=" "\0\n" <expect >expect_nul &&
>  		git repo structure --format=nul >out 2>err &&
>  
> +		test_cmp expect_nul out &&
> +		test_line_count = 0 err &&
> +
> +		# Check -z
> +		git repo structure --format=nul >out 2>err &&

Didn't you want to check "-z", not "--format=nul"? In addition, perhaps test

	# "-z", as a synonym to "--format=nul", participates in the
	# usual "last one wins" rule.
	git repo structure --format=table -z >out 2>err &&

as well, or is it too obvious (by knowing how parse-options API is used) that it is pointless to test?

>  		test_cmp expect_nul out &&
>  		test_line_count = 0 err
>  	)
Patrick Steinhardt· Dec 1, 2025, 08:28 UTC · re: Junio C Hamano · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

On Sun, Nov 30, 2025 at 06:21:21PM -0800, Junio C Hamano wrote:
Show 8 quoted lines
> Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> writes:
> 
> > -git repo structure [--format=(table|keyvalue|nul)]
> > +git repo structure [--format=(table|keyvalue|nul)] [-z]
> 
> It's a minor thing but I wonder if this should be more like
> 
>     git repo structure [--format=(table|keyvalue|nul) | -z]

This discussion rings a bell -- didn't we already discuss this for `git repo info`? Yup, indeed [1]. Initial versions of `git repo info `-z` also had it formatted like the above, but the discussion eventually led to a version that is the same as the one proposed in this patch here.

I think this ultimately stems from [2], where you argue that it _is_ possible to say `git repo info --format=table -z`, and the last option wins. It's the same for `git repo structure`, so I'd argue we should be consistent.

[1]: <20250820144247.79197-1-lucasseikioshiro@gmail.com> [2]: <xmqqcy8frqn2.fsf@gitster.g>

Show 23 quoted lines
> > diff --git a/t/t1901-repo-structure.sh b/t/t1901-repo-structure.sh
> > index 36a71a144e..5a50acf345 100755
> > --- a/t/t1901-repo-structure.sh
> > +++ b/t/t1901-repo-structure.sh
> > @@ -101,6 +101,12 @@ test_expect_success 'keyvalue and nul format' '
> >  		tr "\n=" "\0\n" <expect >expect_nul &&
> >  		git repo structure --format=nul >out 2>err &&
> >  
> > +		test_cmp expect_nul out &&
> > +		test_line_count = 0 err &&
> > +
> > +		# Check -z
> > +		git repo structure --format=nul >out 2>err &&
> 
> Didn't you want to check "-z", not "--format=nul"?  In addition,
> perhaps test
> 
> 	# "-z", as a synonym to "--format=nul", participates in the
> 	# usual "last one wins" rule.
> 	git repo structure --format=table -z >out 2>err &&
> 
> as well, or is it too obvious (by knowing how parse-options API is
> used) that it is pointless to test?

I think it wouldn't be pointless given the above -- we should verify that the last one wins, regardless of whether you use the alias or the long option.

Patrick
Lucas Seiki Oshiro· Dec 1, 2025, 14:34 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

> I think this ultimately stems from [2], where you argue that it _is_
> possible to say `git repo info --format=table -z`, and the last option
> wins. It's the same for `git repo structure`, so I'd argue we should be
> consistent.

Yeah, I agree. I'm also ok with `[--format ... | -z]`, but given that this argument parsing git-repo-stucture is the same from git-repo-info, it wouldn't make sense that their synopses differ.

Junio C Hamano· Dec 2, 2025, 03:45 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

Patrick Steinhardt <ps@pks.im> writes:
> I think this ultimately stems from [2], where you argue that it _is_
> possible to say `git repo info --format=table -z`, and the last option
> wins. It's the same for `git repo structure`, so I'd argue we should be
> consistent.

I do not think so; [2] was about the explicit "--format=nul and -z are incompatible" code. Even without this '-z" thing, if you have

    [--opt=(a|b|c)]

I _think_ our intention is to allow "git cmd --opt=a --opt=b" from the user and let the last one win. If you add "-z",

    [--format=(table|keyvalue|nul)] [-z]
    [--format=(table|keyvalue|nul) | -z]

I think both would mean the same thing; I just was wonering if grouping them together in one bracket makes it more obvious that these are all options to control the output format.

There was also an inquiry about writing it like
    [(--format=(table|keyvalue|nul) | -z)]
or even
    [(--format=(table|keyvalue|nul) | -z)...]

as these can be repeated and let the last-one-wins rule decide the final outcome, but we do not do that. I thought the final comment on this was in

https://lore.kernel.org/git/6186055.lOV4Wx5bFT@cayenne/
where the message said
    In fact the correct formatting is:
    [--format=(keyvalue|nul) | -z] [<key>...]
    As stated in "CodingGuidelines:
     Use spacing around "|" token(s), but not immediately after opening or
     before closing a [] or () pair:
       Do: [-q | --quiet]
       Don't: [-q|--quiet]
     Don't use spacing around "|" tokens when they're used to separate the
     alternate arguments of an option:
        Do: --track[=(direct|inherit)]
        Don't: --track[=(direct | inherit)]
and that was where the thread ended, I think.
> [1]: <20250820144247.79197-1-lucasseikioshiro@gmail.com>
> [2]: <xmqqcy8frqn2.fsf@gitster.g>
Lucas Seiki Oshiro· Dec 2, 2025, 22:05 UTC · re: Junio C Hamano · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

> I thought the final comment on this was in
> 
> https://lore.kernel.org/git/6186055.lOV4Wx5bFT@cayenne/

Actually, the final comment was [1], after the version where I use `[--format=...] [-z]`.

But actually, I don't have a strong opinion on using `| -z` vs using `[-z]`, since both work here, but if `| -z` looks clearer, I can change it in the next version. However, I'll do that for both repo-info and repo-structure, since --format is parsed by the same function in both of them.

[1]: <xmqqms7cltak.fsf@gitster.g <mailto:xmqqms7cltak.fsf@gitster.g>>
Justin Tobler· Dec 1, 2025, 15:11 UTC · re: Lucas Seiki Oshiro · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

On 25/11/30 04:59PM, Lucas Seiki Oshiro wrote:
Show 16 quoted lines
> Other Git commands that have nul-terminated output, such as git-config,
> git-status, git-ls-files, and git-repo-info have a flag `-z` for using
> the null character as the record separator.
> 
> Add the `-z` flag to git-repo-structure as an alias for `--format=nul`,
> making it consistent with the behavior of the other commands.
> 
> Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
> ---
> 
> Hello, again!
> 
> Since git-repo-info and git-repo-structure share the same format parser, this
> could be done by only copying the OPT_CALLBACK_F from repo_info.
> 
> I'm cc'ing Justin here, as he was the author of git-repo-structure.
Thanks Lucas, looks like you beat me to this change. :)
> This patch is based on top of master b31ab939fe (The fourth batch, 2025-11-26)
> with lo/repo-info-all merged.

Any reason this isn't just based on master? It looks like this patch should apply just fine as I don't see any dependencies.

Show 32 quoted lines
>  Documentation/git-repo.adoc | 5 +++--
>  builtin/repo.c              | 6 +++++-
>  t/t1901-repo-structure.sh   | 6 ++++++
>  3 files changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc
> index 70f0a6d2e4..8820954f7e 100644
> --- a/Documentation/git-repo.adoc
> +++ b/Documentation/git-repo.adoc
> @@ -9,7 +9,7 @@ SYNOPSIS
>  --------
>  [synopsis]
>  git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]
> -git repo structure [--format=(table|keyvalue|nul)]
> +git repo structure [--format=(table|keyvalue|nul)] [-z]
>  
>  DESCRIPTION
>  -----------
> @@ -44,7 +44,7 @@ supported:
>  +
>  `-z` is an alias for `--format=nul`.
>  
> -`structure [--format=(table|keyvalue|nul)]`::
> +`structure [--format=(table|keyvalue|nul)] [-z]`::
>  	Retrieve statistics about the current repository structure. The
>  	following kinds of information are reported:
>  +
> @@ -72,6 +72,7 @@ supported:
>  	the delimiter between the key and value instead of '='. Unlike the
>  	`keyvalue` format, values containing "unusual" characters are never
>  	quoted.
> +`-z` is an alias for `--format=nul`.

If we want to match the format in the section prior, we should add a '+' character on the prior line to separate the sections. With how it is written now, this line will just be appeneded to the previous section.

Show 23 quoted lines
>  INFO KEYS
>  ---------
> diff --git a/builtin/repo.c b/builtin/repo.c
> index 2a653bd3ea..1c06207a39 100644
> --- a/builtin/repo.c
> +++ b/builtin/repo.c
> @@ -16,7 +16,7 @@
>  
>  static const char *const repo_usage[] = {
>  	"git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]",
> -	"git repo structure [--format=(table|keyvalue|nul)]",
> +	"git repo structure [--format=(table|keyvalue|nul)] [-z]",
>  	NULL
>  };
>  
> @@ -529,6 +529,10 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
>  		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),

Reusing the same `parse_format_cb()` makes sense and also matches what we do with git-repo-info. Looks good.

Show 16 quoted lines
>  		OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
>  		OPT_END()
>  	};
> diff --git a/t/t1901-repo-structure.sh b/t/t1901-repo-structure.sh
> index 36a71a144e..5a50acf345 100755
> --- a/t/t1901-repo-structure.sh
> +++ b/t/t1901-repo-structure.sh
> @@ -101,6 +101,12 @@ test_expect_success 'keyvalue and nul format' '
>  		tr "\n=" "\0\n" <expect >expect_nul &&
>  		git repo structure --format=nul >out 2>err &&
>  
> +		test_cmp expect_nul out &&
> +		test_line_count = 0 err &&
> +
> +		# Check -z
> +		git repo structure --format=nul >out 2>err &&
Did you mean to use the -z option here?
-Justin
Lucas Seiki Oshiro· Dec 2, 2025, 00:29 UTC · re: Justin Tobler · lore

Re: [PATCH] repo: add -z as an alias for --format=nul to git-repo-structure

Hi!
Show 5 quoted lines
>> This patch is based on top of master b31ab939fe (The fourth batch, 2025-11-26)
>> with lo/repo-info-all merged.
> 
> Any reason this isn't just based on master? It looks like this patch
> should apply just fine as I don't see any dependencies.
Yesterday, lo/repo-info-all wasn't merged yet to master :-)
> Reusing the same `parse_format_cb()` makes sense and also matches what
> we do with git-repo-info. Looks good.

Yes, this callback is used by both subcommands, this was a really straightforward change.

> Did you mean to use the -z option here?
Ooops, yes, sorry. I'll fix it.
Lucas Seiki Oshiro· Dec 4, 2025, 20:10 UTC · re: Lucas Seiki Oshiro · lore

[PATCH v2 0/3] repo: add -z as an alias for --format=nul to git-repo-structure

Hi!

The main objective of this patchset is to add -z to git-repo-structure as an alias for --format=nul. I'm including two other related patches here:

- a patch removing an extra line in the documentation, which was leaking an + to
  the generated files
- a patch replacing `[--format=...] [-z]` by `[--format=... | -z]` in
  git-repo-info's synopsis
Lucas Seiki Oshiro (3):
  repo: remove blank line from Documentation/git-repo.adoc
  repo: use [--format=... | -z] instead of [-z] in git-repo-info
    synopsis
  repo: add -z as an alias for --format=nul to git-repo-structure
 Documentation/git-repo.adoc | 11 ++++++-----
 builtin/repo.c              |  8 ++++++--
 t/t1901-repo-structure.sh   |  7 +++++++
 3 files changed, 19 insertions(+), 7 deletions(-)
Range-diff against v1:
-:  ---------- > 1:  bfd4ff5db0 repo: remove blank line from Documentation/git-repo.adoc
-:  ---------- > 2:  e5eac8713c repo: use [--format=... | -z] instead of [-z] in git-repo-info synopsis
1:  f051cd41de ! 3:  0ccec7037d repo: add -z as an alias for --format=nul to git-repo-structure
    @@ Documentation/git-repo.adoc
     @@ Documentation/git-repo.adoc: SYNOPSIS
      --------
      [synopsis]
    - git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]
    + git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]
     -git repo structure [--format=(table|keyvalue|nul)]
    -+git repo structure [--format=(table|keyvalue|nul)] [-z]
    ++git repo structure [--format=(table|keyvalue|nul) | -z]
      
      DESCRIPTION
      -----------
    @@ Documentation/git-repo.adoc: supported:
      `-z` is an alias for `--format=nul`.
      
     -`structure [--format=(table|keyvalue|nul)]`::
    -+`structure [--format=(table|keyvalue|nul)] [-z]`::
    ++`structure [--format=(table|keyvalue|nul) | -z]`::
      	Retrieve statistics about the current repository structure. The
      	following kinds of information are reported:
      +
    @@ Documentation/git-repo.adoc: supported:
      	the delimiter between the key and value instead of '='. Unlike the
      	`keyvalue` format, values containing "unusual" characters are never
      	quoted.
    +++
     +`-z` is an alias for `--format=nul`.
      
      INFO KEYS
    @@ builtin/repo.c
     @@
      
      static const char *const repo_usage[] = {
    - 	"git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]",
    + 	"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
     -	"git repo structure [--format=(table|keyvalue|nul)]",
    -+	"git repo structure [--format=(table|keyvalue|nul)] [-z]",
    ++	"git repo structure [--format=(table|keyvalue|nul) | -z]",
      	NULL
      };
      
    @@ t/t1901-repo-structure.sh: test_expect_success 'keyvalue and nul format' '
     +		test_cmp expect_nul out &&
     +		test_line_count = 0 err &&
     +
    -+		# Check -z
    -+		git repo structure --format=nul >out 2>err &&
    ++		# "-z", as a synonym to "--format=nul", participates in the
    ++		# usual "last one wins" rule.
    ++		git repo structure --format=table -z >out 2>err &&
     +
      		test_cmp expect_nul out &&
      		test_line_count = 0 err
-- 
2.50.1 (Apple Git-155)
Lucas Seiki Oshiro· Dec 4, 2025, 20:10 UTC · re: Lucas Seiki Oshiro · lore

[PATCH v2 1/3] repo: remove blank line from Documentation/git-repo.adoc

There was an extra blank line in git-repo-structure documentation, which led to an unwawnted '+' character after generating an HTML or PDF from that page. This can be seen, for example, in Git 2.52.0 online docs [1].

Remove that extra line.
[1] https://git-scm.com/docs/git-repo/2.52.0
Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
---
 Documentation/git-repo.adoc | 1 -
 1 file changed, 1 deletion(-)
Show changes to Documentation/git-repo.adoc +0 −1
diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc
index 70f0a6d2e4..5d9c7641c2 100644
--- a/Documentation/git-repo.adoc
+++ b/Documentation/git-repo.adoc
@@ -50,7 +50,6 @@ supported:
 +
 * Reference counts categorized by type
 * Reachable object counts categorized by type
-
 +
 The output format can be chosen through the flag `--format`. Three formats are
 supported:
-- 
2.50.1 (Apple Git-155)
Lucas Seiki Oshiro· Dec 4, 2025, 20:10 UTC · re: Lucas Seiki Oshiro · lore

[PATCH v2 2/3] repo: use [--format=... | -z] instead of [-z] in git-repo-info synopsis

The flag -z is only an alias for --format=null and even though --format and -z can be used together and repeated, only the last one is considered.

Replace `[-z]` in the synopsis of git-repo-info by `[--format=... | -z]`, expliciting that the use of one of those flags replace the other.

Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
---
 Documentation/git-repo.adoc | 4 ++--
 builtin/repo.c              | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
Show changes to 2 files +3 −3

Documentation/git-repo.adoc, builtin/repo.c

diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc
index 5d9c7641c2..f24514deaa 100644
--- a/Documentation/git-repo.adoc
+++ b/Documentation/git-repo.adoc
@@ -8,7 +8,7 @@ git-repo - Retrieve information about the repository
 SYNOPSIS
 --------
 [synopsis]
-git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]
+git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]
 git repo structure [--format=(table|keyvalue|nul)]
 
 DESCRIPTION
@@ -19,7 +19,7 @@ THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
 
 COMMANDS
 --------
-`info [--format=(keyvalue|nul)] [-z] [--all | <key>...]`::
+`info [--format=(keyvalue|nul) | -z] [--all | <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).
diff --git a/builtin/repo.c b/builtin/repo.c
index 2a653bd3ea..cc97dd1836 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -15,7 +15,7 @@
 #include "utf8.h"
 
 static const char *const repo_usage[] = {
-	"git repo info [--format=(keyvalue|nul)] [-z] [--all | <key>...]",
+	"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
 	"git repo structure [--format=(table|keyvalue|nul)]",
 	NULL
 };
-- 
2.50.1 (Apple Git-155)
Lucas Seiki Oshiro· Dec 4, 2025, 20:10 UTC · re: Lucas Seiki Oshiro · lore

[PATCH v2 3/3] repo: add -z as an alias for --format=nul to git-repo-structure

Other Git commands that have nul-terminated output, such as git-config, git-status, git-ls-files, and git-repo-info have a flag `-z` for using the null character as the record separator.

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

Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com>
---
 Documentation/git-repo.adoc | 6 ++++--
 builtin/repo.c              | 6 +++++-
 t/t1901-repo-structure.sh   | 7 +++++++
 3 files changed, 16 insertions(+), 3 deletions(-)
Show changes to 3 files +16 −3

Documentation/git-repo.adoc, builtin/repo.c, t/t1901-repo-structure.sh

diff --git a/Documentation/git-repo.adoc b/Documentation/git-repo.adoc
index f24514deaa..c4a78277df 100644
--- a/Documentation/git-repo.adoc
+++ b/Documentation/git-repo.adoc
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [synopsis]
 git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]
-git repo structure [--format=(table|keyvalue|nul)]
+git repo structure [--format=(table|keyvalue|nul) | -z]
 
 DESCRIPTION
 -----------
@@ -44,7 +44,7 @@ supported:
 +
 `-z` is an alias for `--format=nul`.
 
-`structure [--format=(table|keyvalue|nul)]`::
+`structure [--format=(table|keyvalue|nul) | -z]`::
 	Retrieve statistics about the current repository structure. The
 	following kinds of information are reported:
 +
@@ -71,6 +71,8 @@ supported:
 	the delimiter between the key and value instead of '='. Unlike the
 	`keyvalue` format, values containing "unusual" characters are never
 	quoted.
++
+`-z` is an alias for `--format=nul`.
 
 INFO KEYS
 ---------
diff --git a/builtin/repo.c b/builtin/repo.c
index cc97dd1836..0dd41b1778 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -16,7 +16,7 @@
 
 static const char *const repo_usage[] = {
 	"git repo info [--format=(keyvalue|nul) | -z] [--all | <key>...]",
-	"git repo structure [--format=(table|keyvalue|nul)]",
+	"git repo structure [--format=(table|keyvalue|nul) | -z]",
 	NULL
 };
 
@@ -529,6 +529,10 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix,
 		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_BOOL(0, "progress", &show_progress, N_("show progress")),
 		OPT_END()
 	};
diff --git a/t/t1901-repo-structure.sh b/t/t1901-repo-structure.sh
index 36a71a144e..df7d4ea524 100755
--- a/t/t1901-repo-structure.sh
+++ b/t/t1901-repo-structure.sh
@@ -101,6 +101,13 @@ test_expect_success 'keyvalue and nul format' '
 		tr "\n=" "\0\n" <expect >expect_nul &&
 		git repo structure --format=nul >out 2>err &&
 
+		test_cmp expect_nul out &&
+		test_line_count = 0 err &&
+
+		# "-z", as a synonym to "--format=nul", participates in the
+		# usual "last one wins" rule.
+		git repo structure --format=table -z >out 2>err &&
+
 		test_cmp expect_nul out &&
 		test_line_count = 0 err
 	)
-- 
2.50.1 (Apple Git-155)
Junio C Hamano· Dec 11, 2025, 05:23 UTC · re: Lucas Seiki Oshiro · lore

Re: [PATCH v2 3/3] repo: add -z as an alias for --format=nul to git-repo-structure

Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> writes:
Show 9 quoted lines
> diff --git a/t/t1901-repo-structure.sh b/t/t1901-repo-structure.sh
> index 36a71a144e..df7d4ea524 100755
> --- a/t/t1901-repo-structure.sh
> +++ b/t/t1901-repo-structure.sh
> @@ -101,6 +101,13 @@ test_expect_success 'keyvalue and nul format' '
>  		tr "\n=" "\0\n" <expect >expect_nul &&
>  		git repo structure --format=nul >out 2>err &&
> +		test_cmp expect_nul out &&
> +		test_line_count = 0 err &&
Not limited to this step, but I have a couple of comments.
 * Instead of munging the expected file so that it contains a NUL,
   and compare the actual output with it, munge the NUL terminated
   outout to make it text and compare with the expected file in text
   format.  This matters when tests start to fail as test_cmp will
   show the "diff" output when it fails, and comparing NUL
   terminated files, which are "binary" in the eyes of the "diff"
   utility.
 * I see your -z output is "<key> LF <value> NUL", but was there a
   particular reason why "<key> NUL <value> NUL" was not chosen?
   Unless there is a compelling reason not to, it would be a lot
   more future-proof to use NUL for both, primarily because it would
   allow future developers to include arbitrary non-NUL bytes in the
   <key> part in the future (and we wouldn't know what end-user
   controlled substring they may want to add).
Show 8 quoted lines
> +
> +		# "-z", as a synonym to "--format=nul", participates in the
> +		# usual "last one wins" rule.
> +		git repo structure --format=table -z >out 2>err &&
> +
>  		test_cmp expect_nul out &&
>  		test_line_count = 0 err
>  	)
Lucas Seiki Oshiro· Dec 18, 2025, 23:02 UTC · re: Junio C Hamano · lore

Re: [PATCH v2 3/3] repo: add -z as an alias for --format=nul to git-repo-structure

Hi, Junio!
> * Instead of munging the expected file so that it contains a NUL,
>   and compare the actual output with it, munge the NUL terminated
>   outout to make it text and compare with the expected file in text
>   format.
Ok! I'll do that in the next version.
> * I see your -z output is "<key> LF <value> NUL", but was there a
>   particular reason why "<key> NUL <value> NUL" was not chosen?

This was a suggestions by Phillip Wood [1]. We've chosen this format following the format of git-config.

[1] https://lore.kernel.org/git/223c7cbd-610e-49e2-90e2-5914cbc0f1d7@gmail.com/
Patrick Steinhardt· Dec 5, 2025, 11:09 UTC · re: Lucas Seiki Oshiro · lore

Re: [PATCH v2 0/3] repo: add -z as an alias for --format=nul to git-repo-structure

On Thu, Dec 04, 2025 at 05:10:09PM -0300, Lucas Seiki Oshiro wrote:
Show 10 quoted lines
> Hi!
> 
> The main objective of this patchset is to add -z to git-repo-structure as
> an alias for --format=nul. I'm including two other related patches here:
> 
> - a patch removing an extra line in the documentation, which was leaking an + to
>   the generated files
> 
> - a patch replacing `[--format=...] [-z]` by `[--format=... | -z]` in
>   git-repo-info's synopsis
This version looks good to me, thanks!
Patrick

← back to recent threads