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

Re: [PATCH v2 11/12] fast-import: use parse_options() for command line options

From
Elijah Newren <newren@gmail.com>
Date
Aug 8, 2026, 07:25 UTC
Message-ID
<CABPp-BFfF+Vd6RY3pG=FVUH_93YZULfhcXdWCv6zcRSABfGBQQ@mail.gmail.com>
In-Reply-To
<20260804100355.1299498-12-christian.couder@gmail.com>

On Tue, Aug 4, 2026 at 3:04 AM Christian Couder <christian.couder@gmail.com> wrote:

Show 29 quoted lines
>
> Previous commits have started to use the parse-options API to display
> output from `git fast-import -h` and `git fast-import --help-all` and
> to prepare for parsing the command line options using this API.
>
> Let's now actually use the API to parse command line options.
>
> This brings a number of changes that are mostly beneficial:
>
>   - The `--alias`, `--get-mark`, `--cat-blob`, `--ls` and `--notes`
>     options are no longer accepted on the command line. They were
>     previously accepted as no-ops because parse_argv() fell through to
>     parse_one_feature(). They are not documented in the OPTIONS section
>     and are only meaningful as in-stream feature assertions, so
>     accepting them on the command line was an accident of code sharing
>     dating back to 9c8398f0c9 (fast-import: add option command,
>     2009-12-04).
>
>   - Abbreviated options like `--dep=5` now work since parse_options()
>     allows unambiguous prefixes.
>
>   - As `--cat-blob` is an abbreviation of `--cat-blob-fd`, using the
>     former on the command line will fail with "option `cat-blob-fd'
>     requires a value" unlike the other four options that are not
>     accepted anymore on the command line (see above).
>
>   - The error messages for some options might differ a bit.
>
>   - The code is shorter and more standard.
I think the list might be missing three behavioral changes:
1) A bare "--" (or a trailing "--") is now accepted and the command
reads the stream normally, whereas the base treated it as a usage
error:
        printf '' | git fast-import --   # before: 129 (usage), after: 0

The old parse_argv() broke on "--" and then did "if (i != state->argc) usage_with_options(...)"; parse_options() instead consumes "--" as the end-of-options marker and returns just argv0. Harmless / conventional, just unlisted.

2) Value-taking options now also accept the space-separated "--opt
value" form (e.g. "--depth 5", "--max-pack-size 1m", "--date-format
raw"), not just "--opt=value".   Also expected parse_options()
behavior and a nice improvement.
3) The handling of "--allow-unsafe-features" has changed and might
trip users up.

Because the "feature" lines at the top of the stream are processed before parse_argv() runs, cmd_fast_import() does an early scan just to learn whether unsafe features are permitted:

        for (int i = 1; i < argc; i++) {
                const char *arg = argv[i];
                if (*arg != '-' || !strcmp(arg, "--"))
                        break;
                if (!strcmp(arg, "--allow-unsafe-features"))
                        state.allow_unsafe_features = 1;
        }

That scan (a) matches only the exact spelling and (b) stops at the first token not starting with '-'. In the base that was fine, because the old parse_argv loop was equally strict (exact spelling, "--opt=value" only). But now that parse_options() also accepts unambiguous abbreviations and space-separated values, the two passes disagree. With an unsafe feature line in the stream:

    # (A) old-style spelling, still fine:
    printf 'feature import-marks-if-exists=/nope\n' |
      git fast-import --depth=5 --allow-unsafe-features
    # -> exit 0
    # (B) space-separated value, newly accepted by parse_options():
    printf 'feature import-marks-if-exists=/nope\n' |
      git fast-import --depth 5 --allow-unsafe-features
    # -> fatal: feature 'import-marks-if-exists' forbidden ... (128)
    # (C) abbreviation, newly accepted by parse_options():
    printf 'feature import-marks-if-exists=/nope\n' |
      git fast-import --allow-unsafe
    # -> fatal: feature 'import-marks-if-exists' forbidden ... (128)

In (B) the early scan breaks on the bare "5" (it can't tell "5" is --depth's argument) and never reaches --allow-unsafe-features; in (C) the abbreviation isn't recognized by the strcmp(). Yet in both cases parse_options() itself accepts the option ("git fast-import --allow-unsafe" alone exits 0), so it's only the in-stream feature that gets rejected.

This errs on the safe side (it refuses an unsafe feature rather than allowing one), and it's a minor inconsistency, but it might surprise users. At a minimum, it should probably be documented as a shortcoming or TODO or something. One possible solution is a dedicated parse_options() pass for just --allow-unsafe-features; another might be just requiring --allow-unsafe-features to be the *first* argument. Thoughts?

Anyway, other than the above list of three additional behavioral changes, the rest of the patch looks right. Thanks for tackling modernizing this command.

Previous: Christian CouderNext: Christian Couder
Message 31 of 48 in “fast-import: standardize usage string and SYNOPSIS”
  1. 0/7 fast-import: standardize usage string and SYNOPSISChristian Couder, Jul 16, 2026
  2. 1/7 parse-options: introduce OPT_HIDDEN_GROUPChristian Couder, Jul 16, 2026
  3. Junio C HamanoJul 16, 2026
  4. .mailmap etiquette (was "Re: [PATCH 1/7] parse-options: introduce OPT_HIDDEN_GROUP")D. Ben Knoble, Jul 16, 2026
  5. Junio C HamanoJul 16, 2026
  6. Christian CouderAug 3, 2026
  7. 2/7 api-parse-options.adoc: document per-option flagsChristian Couder, Jul 16, 2026
  8. 3/7 api-parse-options.adoc: document hidden and OPT_*_F option macrosChristian Couder, Jul 16, 2026
  9. 4/7 fast-import: localize 'i' into the 'for' loops using itChristian Couder, Jul 16, 2026
  10. 5/7 fast-import: introduce 'struct fast_import_state'Christian Couder, Jul 16, 2026
  11. 6/7 fast-import: move command state globals into 'struct fast_import_state'Christian Couder, Jul 16, 2026
  12. 7/7 fast-import: use struct option for usage stringChristian Couder, Jul 16, 2026
  13. Junio C HamanoJul 16, 2026
  14. Christian CouderAug 3, 2026
  15. 00/12 fast-import: standardize usage string and SYNOPSISChristian Couder, Aug 4, 2026
  16. 01/12 parse-options: introduce OPT_HIDDEN_GROUPChristian Couder, Aug 4, 2026
  17. 02/12 api-parse-options.adoc: document per-option flagsChristian Couder, Aug 4, 2026
  18. Elijah NewrenAug 8, 2026
  19. Christian CouderAug 11, 2026
  20. 03/12 api-parse-options.adoc: document hidden and OPT_*_F option macrosChristian Couder, Aug 4, 2026
  21. 04/12 fast-import: localize 'i' into the 'for' loops using itChristian Couder, Aug 4, 2026
  22. 05/12 fast-import: use int for some bool flagsChristian Couder, Aug 4, 2026
  23. 06/12 fast-import: factor out option_*() functionsChristian Couder, Aug 4, 2026
  24. 07/12 fast-import: introduce 'struct fast_import_state'Christian Couder, Aug 4, 2026
  25. Elijah NewrenAug 8, 2026
  26. Christian CouderAug 11, 2026
  27. 08/12 fast-import: move command state globals into 'struct fast_import_state'Christian Couder, Aug 4, 2026
  28. 09/12 fast-import: use struct option for usage stringChristian Couder, Aug 4, 2026
  29. 10/12 fast-import: use callbacks to parse some optionsChristian Couder, Aug 4, 2026
  30. 11/12 fast-import: use parse_options() for command line optionsChristian Couder, Aug 4, 2026
  31. Elijah NewrenAug 8, 2026
  32. Christian CouderAug 11, 2026
  33. 12/12 fast-import: remove useless from_stream argumentChristian Couder, Aug 4, 2026
  34. Elijah NewrenAug 8, 2026
  35. 00/12 fast-import: standardize usage string and SYNOPSISChristian Couder, Aug 11, 2026
  36. 02/12 api-parse-options.adoc: document per-option flagsChristian Couder, Aug 11, 2026
  37. 03/12 api-parse-options.adoc: document hidden and OPT_*_F option macrosChristian Couder, Aug 11, 2026
  38. 04/12 fast-import: localize 'i' into the 'for' loops using itChristian Couder, Aug 11, 2026
  39. 05/12 fast-import: use int for some bool flagsChristian Couder, Aug 11, 2026
  40. 06/12 fast-import: factor out option_*() functionsChristian Couder, Aug 11, 2026
  41. 07/12 fast-import: introduce 'struct fast_import_state'Christian Couder, Aug 11, 2026
  42. 08/12 fast-import: move command state globals into 'struct fast_import_state'Christian Couder, Aug 11, 2026
  43. 09/12 fast-import: use struct option for usage stringChristian Couder, Aug 11, 2026
  44. 10/12 fast-import: use callbacks to parse some optionsChristian Couder, Aug 11, 2026
  45. 11/12 fast-import: use parse_options() for command line optionsChristian Couder, Aug 11, 2026
  46. 12/12 fast-import: remove useless from_stream argumentChristian Couder, Aug 11, 2026
  47. 01/12 parse-options: introduce OPT_HIDDEN_GROUPChristian Couder, Aug 11, 2026
  48. Elijah NewrenAug 12, 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.