Commit 9d484b9
committed
diff: fix interaction between the "-s" option and other options
Sergey Organov noticed and reported "--patch --no-patch --raw"
behaves differently from just "--raw". It turns out that there are
a few interesting bugs in the implementation and documentation.
* First, the documentation for "--no-patch" was unclear that it
could be read to mean "--no-patch" countermands an earlier
"--patch" but not other things. The intention of "--no-patch"
ever since it was introduced at d09cd15 (diff: allow --no-patch
as synonym for -s, 2013-07-16) was to serve as a synonym for
"-s", so "--raw --patch --no-patch" should have produced no
output, but it can be (mis)read to allow showing only "--raw"
output.
* Then the interaction between "-s" and other format options were
poorly implemented. Modern versions of Git uses one bit each to
represent formatting options like "--patch", "--stat" in a single
output_format word, but for historical reasons, "-s" also is
represented as another bit in the same word. This allows two
interesting bugs to happen, and we have both X-<.
(1) After setting a format bit, then setting NO_OUTPUT with "-s",
the code to process another "--<format>" option drops the
NO_OUTPUT bit to allow output to be shown again. However,
the code to handle "-s" only set NO_OUTPUT without unsetting
format bits set earlier, so the earlier format bit got
revealed upon seeing the second "--<format>" option. This is
the problem Sergey observed.
(2) After setting NO_OUTPUT with "-s", code to process
"--<format>" option can forget to unset NO_OUTPUT, leaving
the command still silent.
It is tempting to change the meaning of "--no-patch" to mean
"disable only the patch format output" and reimplement "-s" as "not
showing anything", but it would be an end-user visible change in
behavior. Let's fix the interactions of these bits to first make
"-s" work as intended.
The fix is conceptually very simple.
* Whenever we set DIFF_FORMAT_FOO because we saw the "--foo"
option (e.g. DIFF_FORMAT_RAW is set when the "--raw" option is
given), we make sure we drop DIFF_FORMAT_NO_OUTPUT. We forgot to
do so in some of the options and caused (2) above.
* When processing "-s" option, we should not just set
DIFF_FORMAT_NO_OUTPUT bit, but clear other DIFF_FORMAT_* bits.
We didn't do so and retained format bits set by options
previously seen, causing (1) above.
It is even more tempting to lose NO_OUTPUT bit and instead take
output_format word being 0 as its replacement, but that would break
the mechanism "git show" uses to default to "--patch" output, where
the distinction between telling the command to be silent with "-s"
and having no output format specified on the command line matters,
and an explicit output format given on the command line should not
be "combined" with the default "--patch" format.
So, while we cannot lose the NO_OUTPUT bit, as a follow-up work, we
may want to replace it with OPTION_GIVEN bit, and
* make "--patch", "--raw", etc. set DIFF_FORMAT_$format bit and
DIFF_FORMAT_OPTION_GIVEN bit on for each format. "--no-raw",
etc. will set off DIFF_FORMAT_$format bit but still record the
fact that we saw an option from the command line by setting
DIFF_FORMAT_OPTION_GIVEN bit.
* make "-s" (and its synonym "--no-patch") clear all other bits
and set only the DIFF_FORMAT_OPTION_GIVEN bit on.
which I suspect would make the code much cleaner without breaking
any end-user expectations.
Once that is in place, transitioning "--no-patch" to mean the
counterpart of "--patch", just like "--no-raw" only defeats an
earlier "--raw", would be quite simple at the code level. The
social cost of migrating the end-user expectations might be too
great for it to be worth, but at least the "GIVEN" bit clean-up
alone may be worth it.
Signed-off-by: Junio C Hamano <[email protected]>1 parent 8397398 commit 9d484b9
3 files changed
+51
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4874 | 4874 | | |
4875 | 4875 | | |
4876 | 4876 | | |
| 4877 | + | |
4877 | 4878 | | |
4878 | 4879 | | |
4879 | 4880 | | |
| |||
4893 | 4894 | | |
4894 | 4895 | | |
4895 | 4896 | | |
| 4897 | + | |
4896 | 4898 | | |
4897 | 4899 | | |
4898 | 4900 | | |
| |||
5092 | 5094 | | |
5093 | 5095 | | |
5094 | 5096 | | |
| 5097 | + | |
5095 | 5098 | | |
5096 | 5099 | | |
5097 | 5100 | | |
| |||
5410 | 5413 | | |
5411 | 5414 | | |
5412 | 5415 | | |
5413 | | - | |
5414 | | - | |
5415 | | - | |
| 5416 | + | |
| 5417 | + | |
5416 | 5418 | | |
5417 | 5419 | | |
5418 | 5420 | | |
| |||
5421 | 5423 | | |
5422 | 5424 | | |
5423 | 5425 | | |
5424 | | - | |
| 5426 | + | |
5425 | 5427 | | |
5426 | | - | |
| 5428 | + | |
5427 | 5429 | | |
5428 | 5430 | | |
5429 | 5431 | | |
| |||
5432 | 5434 | | |
5433 | 5435 | | |
5434 | 5436 | | |
5435 | | - | |
| 5437 | + | |
5436 | 5438 | | |
5437 | | - | |
5438 | | - | |
| 5439 | + | |
| 5440 | + | |
5439 | 5441 | | |
5440 | | - | |
| 5442 | + | |
5441 | 5443 | | |
5442 | 5444 | | |
5443 | 5445 | | |
| |||
5453 | 5455 | | |
5454 | 5456 | | |
5455 | 5457 | | |
5456 | | - | |
| 5458 | + | |
5457 | 5459 | | |
5458 | | - | |
| 5460 | + | |
5459 | 5461 | | |
5460 | 5462 | | |
5461 | 5463 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
| |||
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
21 | | - | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| |||
91 | 96 | | |
92 | 97 | | |
93 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
94 | 126 | | |
0 commit comments