parse-options: add support for parsing subcommands

Several Git commands have subcommands to implement mutually exclusive
"operation modes", and they usually parse their subcommand argument
with a bunch of if-else if statements.

Teach parse-options to handle subcommands as well, which will result
in shorter and simpler code with consistent error handling and error
messages on unknown or missing subcommand, and it will also make
possible for our Bash completion script to handle subcommands
programmatically.

The approach is guided by the following observations:

  - Most subcommands [1] are implemented in dedicated functions, and
    most of those functions [2] either have a signature matching the
    'int cmd_foo(int argc, const char **argc, const char *prefix)'
    signature of builtin commands or can be trivially converted to
    that signature, because they miss only that last prefix parameter
    or have no parameters at all.

  - Subcommand arguments only have long form, and they have no double
    dash prefix, no negated form, and no description, and they don't
    take any arguments, and can't be abbreviated.

  - There must be exactly one subcommand among the arguments, or zero
    if the command has a default operation mode.

  - All arguments following the subcommand are considered to be
    arguments of the subcommand, and, conversely, arguments meant for
    the subcommand may not preceed the subcommand.

So in the end subcommand declaration and parsing would look something
like this:

    parse_opt_subcommand_fn *fn = NULL;
    struct option builtin_commit_graph_options[] = {
        OPT_STRING(0, "object-dir", &opts.obj_dir, N_("dir"),
                   N_("the object directory to store the graph")),
        OPT_SUBCOMMAND("verify", &fn, graph_verify),
        OPT_SUBCOMMAND("write", &fn, graph_write),
        OPT_END(),
    };
    argc = parse_options(argc, argv, prefix, options,
                         builtin_commit_graph_usage, 0);
    return fn(argc, argv, prefix);

Here each OPT_SUBCOMMAND specifies the name of the subcommand and the
function implementing it, and the address of the same 'fn' subcommand
function pointer.  parse_options() then processes the arguments until
it finds the first argument matching one of the subcommands, sets 'fn'
to the function associated with that subcommand, and returns, leaving
the rest of the arguments unprocessed.  If none of the listed
subcommands is found among the arguments, parse_options() will show
usage and abort.

If a command has a default operation mode, 'fn' should be initialized
to the function implementing that mode, and parse_options() should be
invoked with the PARSE_OPT_SUBCOMMAND_OPTIONAL flag.  In this case
parse_options() won't error out when not finding any subcommands, but
will return leaving 'fn' unchanged.  Note that if that default
operation mode has any --options, then the PARSE_OPT_KEEP_UNKNOWN_OPT
flag is necessary as well (otherwise parse_options() would error out
upon seeing the unknown option meant to the default operation mode).

Some thoughts about the implementation:

  - The same pointer to 'fn' must be specified as 'value' for each
    OPT_SUBCOMMAND, because there can be only one set of mutually
    exclusive subcommands; parse_options() will BUG() otherwise.

    There are other ways to tell parse_options() where to put the
    function associated with the subcommand given on the command line,
    but I didn't like them:

      - Change parse_options()'s signature by adding a pointer to
        subcommand function to be set to the function associated with
        the given subcommand, affecting all callsites, even those that
        don't have subcommands.

      - Introduce a specific parse_options_and_subcommand() variant
        with that extra funcion parameter.

  - I decided against automatically calling the subcommand function
    from within parse_options(), because:

      - There are commands that have to perform additional actions
        after option parsing but before calling the function
        implementing the specified subcommand.

      - The return code of the subcommand is usually the return code
        of the git command, but preserving the return code of the
        automatically called subcommand function would have made the
        API awkward.

  - Also add a OPT_SUBCOMMAND_F() variant to allow specifying an
    option flag: we have two subcommands that are purposefully
    excluded from completion ('git remote rm' and 'git stash save'),
    so they'll have to be specified with the PARSE_OPT_NOCOMPLETE
    flag.

  - Some of the 'parse_opt_flags' don't make sense with subcommands,
    and using them is probably just an oversight or misunderstanding.
    Therefore parse_options() will BUG() when invoked with any of the
    following flags while the options array contains at least one
    OPT_SUBCOMMAND:

      - PARSE_OPT_KEEP_DASHDASH: parse_options() stops parsing
        arguments when encountering a "--" argument, so it doesn't
        make sense to expect and keep one before a subcommand, because
        it would prevent the parsing of the subcommand.

        However, this flag is allowed in combination with the
        PARSE_OPT_SUBCOMMAND_OPTIONAL flag, because the double dash
        might be meaningful for the command's default operation mode,
        e.g. to disambiguate refs and pathspecs.

      - PARSE_OPT_STOP_AT_NON_OPTION: As its name suggests, this flag
        tells parse_options() to stop as soon as it encouners a
        non-option argument, but subcommands are by definition not
        options...  so how could they be parsed, then?!

      - PARSE_OPT_KEEP_UNKNOWN: This flag can be used to collect any
        unknown --options and then pass them to a different command or
        subsystem.  Surely if a command has subcommands, then this
        functionality should rather be delegated to one of those
        subcommands, and not performed by the command itself.

        However, this flag is allowed in combination with the
        PARSE_OPT_SUBCOMMAND_OPTIONAL flag, making possible to pass
        --options to the default operation mode.

  - If the command with subcommands has a default operation mode, then
    all arguments to the command must preceed the arguments of the
    subcommand.

    AFAICT we don't have any commands where this makes a difference,
    because in those commands either only the command accepts any
    arguments ('notes' and 'remote'), or only the default subcommand
    ('reflog' and 'stash'), but never both.

  - The 'argv' array passed to subcommand functions currently starts
    with the name of the subcommand.  Keep this behavior.  AFAICT no
    subcommand functions depend on the actual content of 'argv[0]',
    but the parse_options() call handling their options expects that
    the options start at argv[1].

  - To support handling subcommands programmatically in our Bash
    completion script, 'git cmd --git-completion-helper' will now list
    both subcommands and regular --options, if any.  This means that
    the completion script will have to separate subcommands (i.e.
    words without a double dash prefix) from --options on its own, but
    that's rather easy to do, and it's not much work either, because
    the number of subcommands a command might have is rather low, and
    those commands accept only a single --option or none at all.  An
    alternative would be to introduce a separate option that lists
    only subcommands, but then the completion script would need not
    one but two git invocations and command substitutions for commands
    with subcommands.

    Note that this change doesn't affect the behavior of our Bash
    completion script, because when completing the --option of a
    command with subcommands, e.g. for 'git notes --<TAB>', then all
    subcommands will be filtered out anyway, as none of them will
    match the word to be completed starting with that double dash
    prefix.

[1] Except 'git rerere', because many of its subcommands are
    implemented in the bodies of the if-else if statements parsing the
    command's subcommand argument.

[2] Except 'credential', 'credential-store' and 'fsmonitor--daemon',
    because some of the functions implementing their subcommands take
    special parameters.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
9 files changed
tree: 5cfd6d0b8f5221956498a669cf9066201707ef64
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. negotiator/
  14. perl/
  15. po/
  16. ppc/
  17. refs/
  18. reftable/
  19. sha1dc/
  20. sha256/
  21. t/
  22. templates/
  23. trace2/
  24. xdiff/
  25. .cirrus.yml
  26. .clang-format
  27. .editorconfig
  28. .gitattributes
  29. .gitignore
  30. .gitmodules
  31. .mailmap
  32. .tsan-suppressions
  33. abspath.c
  34. aclocal.m4
  35. add-interactive.c
  36. add-interactive.h
  37. add-patch.c
  38. advice.c
  39. advice.h
  40. alias.c
  41. alias.h
  42. alloc.c
  43. alloc.h
  44. apply.c
  45. apply.h
  46. archive-tar.c
  47. archive-zip.c
  48. archive.c
  49. archive.h
  50. attr.c
  51. attr.h
  52. banned.h
  53. base85.c
  54. bisect.c
  55. bisect.h
  56. blame.c
  57. blame.h
  58. blob.c
  59. blob.h
  60. bloom.c
  61. bloom.h
  62. branch.c
  63. branch.h
  64. builtin.h
  65. bulk-checkin.c
  66. bulk-checkin.h
  67. bundle.c
  68. bundle.h
  69. cache-tree.c
  70. cache-tree.h
  71. cache.h
  72. cbtree.c
  73. cbtree.h
  74. chdir-notify.c
  75. chdir-notify.h
  76. check-builtins.sh
  77. checkout.c
  78. checkout.h
  79. chunk-format.c
  80. chunk-format.h
  81. CODE_OF_CONDUCT.md
  82. color.c
  83. color.h
  84. column.c
  85. column.h
  86. combine-diff.c
  87. command-list.txt
  88. commit-graph.c
  89. commit-graph.h
  90. commit-reach.c
  91. commit-reach.h
  92. commit-slab-decl.h
  93. commit-slab-impl.h
  94. commit-slab.h
  95. commit.c
  96. commit.h
  97. common-main.c
  98. config.c
  99. config.h
  100. config.mak.dev
  101. config.mak.in
  102. config.mak.uname
  103. configure.ac
  104. connect.c
  105. connect.h
  106. connected.c
  107. connected.h
  108. convert.c
  109. convert.h
  110. copy.c
  111. COPYING
  112. credential.c
  113. credential.h
  114. csum-file.c
  115. csum-file.h
  116. ctype.c
  117. daemon.c
  118. date.c
  119. date.h
  120. decorate.c
  121. decorate.h
  122. delta-islands.c
  123. delta-islands.h
  124. delta.h
  125. detect-compiler
  126. diff-delta.c
  127. diff-lib.c
  128. diff-merges.c
  129. diff-merges.h
  130. diff-no-index.c
  131. diff.c
  132. diff.h
  133. diffcore-break.c
  134. diffcore-delta.c
  135. diffcore-order.c
  136. diffcore-pickaxe.c
  137. diffcore-rename.c
  138. diffcore-rotate.c
  139. diffcore.h
  140. dir-iterator.c
  141. dir-iterator.h
  142. dir.c
  143. dir.h
  144. editor.c
  145. entry.c
  146. entry.h
  147. environment.c
  148. environment.h
  149. exec-cmd.c
  150. exec-cmd.h
  151. fetch-negotiator.c
  152. fetch-negotiator.h
  153. fetch-pack.c
  154. fetch-pack.h
  155. fmt-merge-msg.c
  156. fmt-merge-msg.h
  157. fsck.c
  158. fsck.h
  159. fsmonitor--daemon.h
  160. fsmonitor-ipc.c
  161. fsmonitor-ipc.h
  162. fsmonitor-settings.c
  163. fsmonitor-settings.h
  164. fsmonitor.c
  165. fsmonitor.h
  166. fuzz-commit-graph.c
  167. fuzz-pack-headers.c
  168. fuzz-pack-idx.c
  169. generate-cmdlist.sh
  170. generate-configlist.sh
  171. generate-hooklist.sh
  172. gettext.c
  173. gettext.h
  174. git-add--interactive.perl
  175. git-archimport.perl
  176. git-bisect.sh
  177. git-compat-util.h
  178. git-curl-compat.h
  179. git-cvsexportcommit.perl
  180. git-cvsimport.perl
  181. git-cvsserver.perl
  182. git-difftool--helper.sh
  183. git-filter-branch.sh
  184. git-instaweb.sh
  185. git-merge-octopus.sh
  186. git-merge-one-file.sh
  187. git-merge-resolve.sh
  188. git-mergetool--lib.sh
  189. git-mergetool.sh
  190. git-p4.py
  191. git-quiltimport.sh
  192. git-request-pull.sh
  193. git-send-email.perl
  194. git-sh-i18n.sh
  195. git-sh-setup.sh
  196. git-submodule.sh
  197. git-svn.perl
  198. GIT-VERSION-GEN
  199. git-web--browse.sh
  200. git.c
  201. git.rc
  202. gpg-interface.c
  203. gpg-interface.h
  204. graph.c
  205. graph.h
  206. grep.c
  207. grep.h
  208. hash-lookup.c
  209. hash-lookup.h
  210. hash.h
  211. hashmap.c
  212. hashmap.h
  213. help.c
  214. help.h
  215. hex.c
  216. hook.c
  217. hook.h
  218. http-backend.c
  219. http-fetch.c
  220. http-push.c
  221. http-walker.c
  222. http.c
  223. http.h
  224. ident.c
  225. imap-send.c
  226. INSTALL
  227. iterator.h
  228. json-writer.c
  229. json-writer.h
  230. khash.h
  231. kwset.c
  232. kwset.h
  233. levenshtein.c
  234. levenshtein.h
  235. LGPL-2.1
  236. line-log.c
  237. line-log.h
  238. line-range.c
  239. line-range.h
  240. linear-assignment.c
  241. linear-assignment.h
  242. list-objects-filter-options.c
  243. list-objects-filter-options.h
  244. list-objects-filter.c
  245. list-objects-filter.h
  246. list-objects.c
  247. list-objects.h
  248. list.h
  249. ll-merge.c
  250. ll-merge.h
  251. lockfile.c
  252. lockfile.h
  253. log-tree.c
  254. log-tree.h
  255. ls-refs.c
  256. ls-refs.h
  257. mailinfo.c
  258. mailinfo.h
  259. mailmap.c
  260. mailmap.h
  261. Makefile
  262. match-trees.c
  263. mem-pool.c
  264. mem-pool.h
  265. merge-blobs.c
  266. merge-blobs.h
  267. merge-ort-wrappers.c
  268. merge-ort-wrappers.h
  269. merge-ort.c
  270. merge-ort.h
  271. merge-recursive.c
  272. merge-recursive.h
  273. merge.c
  274. mergesort.c
  275. mergesort.h
  276. midx.c
  277. midx.h
  278. name-hash.c
  279. notes-cache.c
  280. notes-cache.h
  281. notes-merge.c
  282. notes-merge.h
  283. notes-utils.c
  284. notes-utils.h
  285. notes.c
  286. notes.h
  287. object-file.c
  288. object-name.c
  289. object-store.h
  290. object.c
  291. object.h
  292. oid-array.c
  293. oid-array.h
  294. oidmap.c
  295. oidmap.h
  296. oidset.c
  297. oidset.h
  298. oidtree.c
  299. oidtree.h
  300. pack-bitmap-write.c
  301. pack-bitmap.c
  302. pack-bitmap.h
  303. pack-check.c
  304. pack-mtimes.c
  305. pack-mtimes.h
  306. pack-objects.c
  307. pack-objects.h
  308. pack-revindex.c
  309. pack-revindex.h
  310. pack-write.c
  311. pack.h
  312. packfile.c
  313. packfile.h
  314. pager.c
  315. parallel-checkout.c
  316. parallel-checkout.h
  317. parse-options-cb.c
  318. parse-options.c
  319. parse-options.h
  320. patch-delta.c
  321. patch-ids.c
  322. patch-ids.h
  323. path.c
  324. path.h
  325. pathspec.c
  326. pathspec.h
  327. pkt-line.c
  328. pkt-line.h
  329. preload-index.c
  330. pretty.c
  331. pretty.h
  332. prio-queue.c
  333. prio-queue.h
  334. progress.c
  335. progress.h
  336. promisor-remote.c
  337. promisor-remote.h
  338. prompt.c
  339. prompt.h
  340. protocol-caps.c
  341. protocol-caps.h
  342. protocol.c
  343. protocol.h
  344. prune-packed.c
  345. prune-packed.h
  346. quote.c
  347. quote.h
  348. range-diff.c
  349. range-diff.h
  350. reachable.c
  351. reachable.h
  352. read-cache.c
  353. README.md
  354. rebase-interactive.c
  355. rebase-interactive.h
  356. rebase.c
  357. rebase.h
  358. ref-filter.c
  359. ref-filter.h
  360. reflog-walk.c
  361. reflog-walk.h
  362. reflog.c
  363. reflog.h
  364. refs.c
  365. refs.h
  366. refspec.c
  367. refspec.h
  368. remote-curl.c
  369. remote.c
  370. remote.h
  371. replace-object.c
  372. replace-object.h
  373. repo-settings.c
  374. repository.c
  375. repository.h
  376. rerere.c
  377. rerere.h
  378. reset.c
  379. reset.h
  380. resolve-undo.c
  381. resolve-undo.h
  382. revision.c
  383. revision.h
  384. run-command.c
  385. run-command.h
  386. SECURITY.md
  387. send-pack.c
  388. send-pack.h
  389. sequencer.c
  390. sequencer.h
  391. serve.c
  392. serve.h
  393. server-info.c
  394. setup.c
  395. sh-i18n--envsubst.c
  396. sha1dc_git.c
  397. sha1dc_git.h
  398. shallow.c
  399. shallow.h
  400. shared.mak
  401. shell.c
  402. shortlog.h
  403. sideband.c
  404. sideband.h
  405. sigchain.c
  406. sigchain.h
  407. simple-ipc.h
  408. sparse-index.c
  409. sparse-index.h
  410. split-index.c
  411. split-index.h
  412. stable-qsort.c
  413. strbuf.c
  414. strbuf.h
  415. streaming.c
  416. streaming.h
  417. string-list.c
  418. string-list.h
  419. strmap.c
  420. strmap.h
  421. strvec.c
  422. strvec.h
  423. sub-process.c
  424. sub-process.h
  425. submodule-config.c
  426. submodule-config.h
  427. submodule.c
  428. submodule.h
  429. symlinks.c
  430. tag.c
  431. tag.h
  432. tar.h
  433. tempfile.c
  434. tempfile.h
  435. thread-utils.c
  436. thread-utils.h
  437. tmp-objdir.c
  438. tmp-objdir.h
  439. trace.c
  440. trace.h
  441. trace2.c
  442. trace2.h
  443. trailer.c
  444. trailer.h
  445. transport-helper.c
  446. transport-internal.h
  447. transport.c
  448. transport.h
  449. tree-diff.c
  450. tree-walk.c
  451. tree-walk.h
  452. tree.c
  453. tree.h
  454. unicode-width.h
  455. unimplemented.sh
  456. unix-socket.c
  457. unix-socket.h
  458. unix-stream-server.c
  459. unix-stream-server.h
  460. unpack-trees.c
  461. unpack-trees.h
  462. upload-pack.c
  463. upload-pack.h
  464. url.c
  465. url.h
  466. urlmatch.c
  467. urlmatch.h
  468. usage.c
  469. userdiff.c
  470. userdiff.h
  471. utf8.c
  472. utf8.h
  473. varint.c
  474. varint.h
  475. version.c
  476. version.h
  477. versioncmp.c
  478. walker.c
  479. walker.h
  480. wildmatch.c
  481. wildmatch.h
  482. worktree.c
  483. worktree.h
  484. wrap-for-bin.sh
  485. wrapper.c
  486. write-or-die.c
  487. ws.c
  488. wt-status.c
  489. wt-status.h
  490. xdiff-interface.c
  491. xdiff-interface.h
  492. zlib.c
README.md

Build status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-<commandname>.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission and Documentation/CodingGuidelines).

Those wishing to help with error message, usage and informational message string translations (localization l10) should see po/README.md (a po file is a Portable Object file that holds the translations).

To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org (not the Git list). The mailing list archives are available at https://lore.kernel.org/git/, http://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks