Files
git/Documentation
Christian Couder cc4affc5ee parse-options: add early_scan_options()
Some commands need to look at a few of their options before they can
parse their command line for real, for example because the result
decides whether a repository is needed at all, or how the beginning of
their input should be interpreted.

Such an early scan has to know which options take their value as a
separate argument, or it mistakes such a value for an option. Several
commands get this wrong, as they just walk their arguments comparing
them to the few option names they care about.

Let's add early_scan_options() to help with this. It walks the
arguments using the very same `struct option` array that the command
already passes to parse_options(). It looks up long options with the
lookup_long_opt() helper extracted from parse_long_opt() in a previous
commit, so that negated forms, unambiguous abbreviations and aliases
are recognized the same way, and it uses the
parse_options_takes_argument() helper added in another previous
commit, so that the scan and the actual parsing agree on which options
take a value.

The options the caller wants to be told about are marked with a new
PARSE_OPT_EARLY flag, so that nothing has to be spelled out a second
time, and so that the mark cannot drift away from the option it refers
to.

Using a per-option flag for this is not new as that flag space already
holds flags that the parsing loop itself ignores, like
PARSE_OPT_NOCOMPLETE and PARSE_OPT_COMP_ARG, which only the completion
helper looks at, or PARSE_OPT_HIDDEN and PARSE_OPT_LITERAL_ARGHELP,
which only the usage output looks at.

The scan is deliberately kept much simpler than parse_options(),
instead of teaching the latter to perform a side effect free "dry
run". Such a dry run would have to avoid writing through `opt->value`,
calling option callbacks, dying on an invalid value, handling `--help`
and tracking command mode conflicts, so it would be a much larger
refactoring. If parse_options() learns to do it in the future though,
the commands converted now would keep both their option array and their
PARSE_OPT_EARLY marks, so their conversion would not have to be redone.

One consequence of staying simple is that the scan doesn't try to
handle what it cannot reliably skip, like short options, unknown
options, ambiguous abbreviations or options with a low-level callback,
which may consume any number of arguments. It stops at them instead,
so that it never takes an option's value for an option. This matters,
as for example with:

  git fast-import --quiet --export-pack --allow-unsafe-features

parse_options() takes `--allow-unsafe-features` as the value of
`--export-pack-edges`, which `--export-pack` abbreviates. A scan that
just skipped `--export-pack` as an unknown option would wrongly see
`--allow-unsafe-features` there.

So the scan can fail to see an option that parse_options() would
accept, but it doesn't report an option that parse_options() would
not, and its callers can safely err on the side of not having seen an
option. The differences with parse_options() are documented in
"parse-options.h".

In practice, despite these limitations, early scans using
early_scan_options() should still be safer and cleaner than the
ad-hoc hand-rolled scans they are meant to replace, which don't know
about option values at all and therefore disagree with
parse_options() in ways that create plain bugs.

Helped-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Signed-off-by: Christian Couder <christian.couder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-11-02 11:02:27 -08:00
..
2026-08-31 08:25:00 -07:00
2025-03-01 10:26:15 -08:00
…
2025-12-14 17:04:37 +09:00
2026-03-06 13:02:20 -08:00
2025-02-14 17:53:47 -08:00
2026-05-21 12:06:48 +09:00
2025-12-16 09:42:44 +09:00
…