Several Git commands scan their arguments before the real option parse, for example to see whether a repository is needed or how to interpret the start of their input. Christian Couder says these scans are often fragile. They fail to account for options that take a value as a separate argument, so they can disagree with parse_options() and treat a value as -- or miss an option entirely.

The series adds an "early-scan" sub-API to parse-options. It walks the arguments using the same struct option array the command already passes to parse_options(). Two preparatory patches support it. One extracts a side-effect-free lookup_long_opt() from parse_long_opt(), so the scan can resolve abbreviations and negated forms without setting values or calling die() or error(). The other factors the "does this option take a value" rule out of show_gitcomp() into parse_options_takes_argument().

The example fix is fast-import. It must know early whether --allow-unsafe-features was given, because "feature" lines in the stream are processed before options are parsed. The existing scan stops at the first argument not starting with a dash. With "--depth 5 --allow-unsafe-features" it stops at "5" and never sees the option, so unsafe features are refused even though the user asked for them.

Couder says v3 responds to review from Kaartic Sivaraam. Abbreviations are now handled with the same code parse_options() uses. The scan errs on the safe side, stopping as soon as it meets something it does not recognise. Sivaraam's question about options parse_options() would reject showed a real problem, in which the early scan reported such an option. In v3 the scan stops at it instead, with a stated exception for an option marked PARSE_OPT_LASTARG_DEFAULT that is the last argument.