git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v1] builtin/mktree: remove USE_THE_REPOSITORY_VARIABLE

From
Tian Yuchen <cat@malon.dev>
Date
Mar 13, 2026, 17:15 UTC
Message-ID
<4fb9c915-7246-4c55-b7c6-b4ef7ca91230@malon.dev>
In-Reply-To
<xmqqpl577m3y.fsf@gitster.g>
On 3/14/26 00:03, Junio C Hamano wrote:
Show 5 quoted lines
> There is one corner case; upon "git foo -h", your cmd_foo() will get
> repo==NULL when the command is run outside a repository.  As long as
> your cmd_foo() asks parse_options() to react to "-h" (which gives
> the help message and then exits) before it uses repo assuming it
> cannot be NULL, you are safe.
I was completely blown away Σ( ° △ °)

Thanks for pointing that out. Otherwise, I wouldn’t have been able to figure it out no matter how hard I try.

I just took a quick look at the code:
Show 12 quoted lines
> 	const struct option option[] = {
> 		OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
> 		OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
> 		OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
> 		OPT_END()
> 	};
> 
> 	ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
> 	getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
> 
> 	while (!got_eof) {
> 		while (1) { ...

I think if there's a '-h' parameter, it gets intercepted in parse_options() and the process exits before repo is called. So there’s nothing to worry about, right?

By the way, I find it a bit confusing that the'`-h' parameter — which is solely used for documentation query — is parsed and intercepted within a function that handles actual business logic. Wouldn’t it be more appropriate to intercept it at a higher level? Is this a technical debt? I don’t intend to write a patch to fix this, but this parameter has made me realize that this odd way of coding (at least in my understanding) is highly unpredictable for someone with limited experience like me. I don't know if there are any troubleshooting methods other than asking someone with experience.

Regards,
Yuchen
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 10 in “builtin/mktree: remove USE_THE_REPOSITORY_VARIABLE”
  1. builtin/mktree: remove USE_THE_REPOSITORY_VARIABLETian Yuchen, Mar 11, 2026
  2. Patrick SteinhardtMar 12, 2026
  3. Tian YuchenMar 12, 2026
  4. Patrick SteinhardtMar 13, 2026
  5. Junio C HamanoMar 13, 2026
  6. Tian YuchenMar 13, 2026
  7. Junio C HamanoMar 13, 2026
  8. Tian YuchenMar 13, 2026
  9. Junio C HamanoMar 13, 2026
  10. Tian YuchenMar 14, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.