git/t/helper
René Scharfe 0025dde775 parse-options: make CMDMODE errors more precise
Only a single PARSE_OPT_CMDMODE option can be specified for the same
variable at the same time.  This is enforced by get_value(), but the
error messages are imprecise in three ways:

1. If a non-PARSE_OPT_CMDMODE option changes the value variable of a
PARSE_OPT_CMDMODE option then an ominously vague message is shown:

   $ t/helper/test-tool parse-options --set23 --mode1
   error: option `mode1' : incompatible with something else

Worse: If the order of options is reversed then no error is reported at
all:

   $ t/helper/test-tool parse-options --mode1 --set23
   boolean: 0
   integer: 23
   magnitude: 0
   timestamp: 0
   string: (not set)
   abbrev: 7
   verbose: -1
   quiet: 0
   dry run: no
   file: (not set)

Fortunately this can currently only happen in the test helper; actual
Git commands don't share the same variable for the value of options with
and without the flag PARSE_OPT_CMDMODE.

2. If there are multiple options with the same value (synonyms), then
the one that is defined first is shown rather than the one actually
given on the command line, which is confusing:

   $ git am --resolved --quit
   error: option `quit' is incompatible with --continue

3. Arguments of PARSE_OPT_CMDMODE options are not handled by the
parse-option machinery.  This is left to the callback function.  We
currently only have a single affected option, --show-current-patch of
git am.  Errors for it can show an argument that was not actually given
on the command line:

   $ git am --show-current-patch --show-current-patch=diff
   error: options '--show-current-patch=diff' and '--show-current-patch=raw' cannot be used together

The options --show-current-patch and --show-current-patch=raw are
synonyms, but the error accuses the user of input they did not actually
made.  Or it can awkwardly print a NULL pointer:

   $ git am --show-current-patch=diff --show-current-patch
   error: options '--show-current-patch=(null)' and '--show-current-patch=diff' cannot be used together

The reasons for these shortcomings is that the current code checks
incompatibility only when encountering a PARSE_OPT_CMDMODE option at the
command line, and that it searches the previous incompatible option by
value.

Fix the first two points by checking all PARSE_OPT_CMDMODE variables
after parsing each option and by storing all relevant details if their
value changed.  Do that whether or not the changing options has the flag
PARSE_OPT_CMDMODE set.  Report an incompatibility only if two options
change the variable to different values and at least one of them is a
PARSE_OPT_CMDMODE option.  This changes the output of the first three
examples above to:

   $ t/helper/test-tool parse-options --set23 --mode1
   error: --mode1 is incompatible with --set23
   $ t/helper/test-tool parse-options --mode1 --set23
   error: --set23 is incompatible with --mode1
   $ git am --resolved --quit
   error: --quit is incompatible with --resolved

Store the argument of PARSE_OPT_CMDMODE options of type OPTION_CALLBACK
as well to allow taking over the responsibility for compatibility
checking from the callback function.  The next patch will use this
capability to fix the messages for git am --show-current-patch.

Use a linked list for storing the PARSE_OPT_CMDMODE variables.  This
somewhat outdated data structure is simple and suffices, as the number
of elements per command is currently only zero or one.  We do support
multiple different command modes variables per command, but I don't
expect that we'd ever use a significant number of them.  Once we do we
can switch to a hashmap.

Since we no longer need to search the conflicting option, the all_opts
parameter of get_value() is no longer used.  Remove it.

Extend the tests to check for both conflicting option names, but don't
insist on a particular order.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-10-29 09:15:18 +09:00
..
.gitignore
test-advise.c treewide: remove cache.h inclusion due to setup.h changes 2023-03-21 10:56:54 -07:00
test-bitmap.c setup.h: move declarations for setup.c functions from cache.h 2023-03-21 10:56:54 -07:00
test-bloom.c hash-ll.h: split out of hash.h to remove dependency on repository.h 2023-04-24 12:47:32 -07:00
test-bundle-uri.c treewide: remove unnecessary cache.h inclusion from a few headers 2023-03-21 10:56:50 -07:00
test-cache-tree.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-chmtime.c t/helper: allow chmtime to print verbosely without modifying mtime 2023-04-14 10:27:52 -07:00
test-config.c config: pass kvi to die_bad_number() 2023-06-28 14:06:40 -07:00
test-crontab.c treewide: remove unnecessary cache.h includes in source files 2023-02-23 17:25:28 -08:00
test-csprng.c wrapper: add a helper to generate numbers from a CSPRNG 2022-01-17 14:17:48 -08:00
test-ctype.c Merge branch 'rs/test-ctype-eof' 2023-05-10 10:23:27 -07:00
test-date.c Merge branch 'en/header-split-cache-h' 2023-04-25 13:56:20 -07:00
test-delta.c treewide: remove unnecessary includes for wrapper.h 2023-07-05 11:41:59 -07:00
test-dir-iterator.c dir-iterator: drop unused DIR_ITERATOR_FOLLOW_SYMLINKS 2023-02-16 16:21:56 -08:00
test-drop-caches.c t/helper: mark unused argv/argc arguments 2023-03-28 14:11:24 -07:00
test-dump-cache-tree.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-dump-fsmonitor.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-dump-split-index.c t/helper: mark unused callback void data parameters 2023-07-13 17:24:00 -07:00
test-dump-untracked-cache.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-env-helper.c parse: separate out parsing functions from config.h 2023-09-29 15:14:57 -07:00
test-example-decorate.c decorate: add clear_decoration() function 2023-10-05 14:54:55 -07:00
test-fake-ssh.c Merge branch 'ab/various-leak-fixes' 2022-12-14 15:55:46 +09:00
test-fast-rebase.c diff.h: remove unnecessary include of oidset.h 2023-06-21 13:39:53 -07:00
test-find-pack.c t/helper: add 'find-pack' test-tool 2023-10-02 14:54:29 -07:00
test-fsmonitor-client.c treewide: remove unnecessary includes for wrapper.h 2023-07-05 11:41:59 -07:00
test-genrandom.c
test-genzeros.c test-genzeros: avoid raw write(2) 2023-02-16 08:30:38 -08:00
test-getcwd.c t0001: fix broken not-quite getcwd(3) test in bed67874e2 2021-07-30 10:18:27 -07:00
test-hash-speed.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-hash.c treewide: remove unnecessary cache.h inclusion from several sources 2023-03-21 10:56:51 -07:00
test-hashmap.c t/helper/test-hashmap.c: avoid using strtok() 2023-04-24 16:01:28 -07:00
test-hexdump.c t/helper: mark unused argv/argc arguments 2023-03-28 14:11:24 -07:00
test-json-writer.c t/helper/test-json-writer.c: avoid using strtok() 2023-04-24 16:01:28 -07:00
test-lazy-init-name-hash.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-match-trees.c Merge branch 'en/header-split-cache-h-part-2' 2023-05-09 16:45:46 -07:00
test-mergesort.c treewide: remove cache.h inclusion due to previous changes 2023-04-24 12:47:33 -07:00
test-mktemp.c
test-oid-array.c t/helper: mark unused callback void data parameters 2023-07-13 17:24:00 -07:00
test-oidmap.c Merge branch 'en/header-split-cache-h-part-2' 2023-05-09 16:45:46 -07:00
test-oidtree.c Merge branch 'en/header-split-cache-h-part-2' 2023-05-09 16:45:46 -07:00
test-online-cpus.c t/helper: mark unused argv/argc arguments 2023-03-28 14:11:24 -07:00
test-pack-mtimes.c object-store-ll.h: split this header out of object-store.h 2023-06-21 13:39:54 -07:00
test-parse-options.c parse-options: make CMDMODE errors more precise 2023-10-29 09:15:18 +09:00
test-parse-pathspec-file.c treewide: remove unnecessary inclusion of gettext.h 2023-03-21 10:56:51 -07:00
test-partial-clone.c object-store-ll.h: split this header out of object-store.h 2023-06-21 13:39:54 -07:00
test-path-utils.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-pcre2-config.c treewide: remove unnecessary cache.h includes in source files 2023-02-23 17:25:28 -08:00
test-pkt-line.c write-or-die.h: move declarations for write-or-die.c functions from cache.h 2023-03-21 10:56:54 -07:00
test-prio-queue.c t/helper: mark unused argv/argc arguments 2023-03-28 14:11:24 -07:00
test-proc-receive.c treewide: remove cache.h inclusion due to setup.h changes 2023-03-21 10:56:54 -07:00
test-progress.c treewide: remove unnecessary inclusion of gettext.h 2023-03-21 10:56:51 -07:00
test-reach.c Merge branch 'tb/refs-exclusion-and-packed-refs' 2023-07-21 13:47:26 -07:00
test-read-cache.c treewide: remove unnecessary includes for wrapper.h 2023-07-05 11:41:59 -07:00
test-read-graph.c object-store-ll.h: split this header out of object-store.h 2023-06-21 13:39:54 -07:00
test-read-midx.c object-store-ll.h: split this header out of object-store.h 2023-06-21 13:39:54 -07:00
test-ref-store.c Merge branch 'jk/unused-parameter' 2023-07-25 12:05:24 -07:00
test-reftable.c reftable: ensure git-compat-util.h is the first (indirect) include 2023-04-24 12:47:33 -07:00
test-regex.c test-tool regex: call regfree(), fix memory leaks 2022-07-01 13:38:50 -07:00
test-repository.c object-store-ll.h: split this header out of object-store.h 2023-06-21 13:39:54 -07:00
test-revision-walking.c diff.h: remove unnecessary include of oidset.h 2023-06-21 13:39:53 -07:00
test-rot13-filter.c t0021: implementation the rot13-filter.pl script in C 2022-08-14 22:57:12 -07:00
test-run-command.c treewide: remove unnecessary inclusion of gettext.h 2023-03-21 10:56:51 -07:00
test-scrap-cache-tree.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-serve-v2.c treewide: remove cache.h inclusion due to setup.h changes 2023-03-21 10:56:54 -07:00
test-sha1.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-sha1.sh
test-sha256.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-sigchain.c t/helper: mark unused argv/argc arguments 2023-03-28 14:11:24 -07:00
test-simple-ipc.c run-command: mark unused parameters in start_bg_wait callbacks 2023-09-18 15:56:15 -07:00
test-strcmp-offset.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-string-list.c Merge branch 'en/header-split-cache-h-part-2' 2023-05-09 16:45:46 -07:00
test-submodule-config.c Merge branch 'en/header-split-cache-h-part-2' 2023-05-09 16:45:46 -07:00
test-submodule-nested-repo-config.c hash-ll.h: split out of hash.h to remove dependency on repository.h 2023-04-24 12:47:32 -07:00
test-submodule.c Merge branch 'en/header-split-cache-h-part-2' 2023-05-09 16:45:46 -07:00
test-subprocess.c treewide: remove cache.h inclusion due to setup.h changes 2023-03-21 10:56:54 -07:00
test-tool-utils.h submodule--helper: move "is-active" to a test-tool 2022-09-02 09:16:23 -07:00
test-tool.c Merge branch 'bc/racy-4gb-files' 2023-10-23 13:56:37 -07:00
test-tool.h Merge branch 'bc/racy-4gb-files' 2023-10-23 13:56:37 -07:00
test-trace2.c test-trace2: mark unused argv/argc parameters 2023-08-29 17:56:25 -07:00
test-truncate.c t: add a test helper to truncate files 2023-10-13 13:33:35 -07:00
test-urlmatch-normalization.c test-tool urlmatch-normalization: fix a memory leak 2022-07-01 13:38:49 -07:00
test-userdiff.c config: add ctx arg to config_fn_t 2023-06-28 14:06:39 -07:00
test-wildmatch.c git-compat-util.h: remove unneccessary include of wildmatch.h 2023-06-21 13:39:54 -07:00
test-windows-named-pipe.c
test-write-cache.c cache.h: remove this no-longer-used header 2023-06-21 13:39:53 -07:00
test-xml-encode.c t/helper: mark unused argv/argc arguments 2023-03-28 14:11:24 -07:00