Skip to contentSkip to conversation

format-patch: teach format.useAutoBase "whenAble" option

PATCH Superseded Jacob Keller 2 participants 2 messages Sep 25, 2020 – Sep 26, 2020 Documentation

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 format-patch: teach format.useAutoBase "whenAble" option

From: Jacob Keller <[email protected]>

The format.useAutoBase configuration option exists to allow users to enable '--base=auto' for format-patch by default.

This can sometimes lead to poor workflow, due to unexpected failures when attempting to format an ancient patch:

    $ git format-patch -1 <an old commit>
    fatal: base commit shouldn't be in revision list

This can be very confusing, as it is not necessarily immediately obvious that the user requested a --base (since this was in the configuration, not on the command line).

We do want --base=auto to fail when it cannot provide a suitable base, as it would be equally confusing if a formatted patch did not include the base information when it was requested.

Teach format.useAutoBase a new mode, "whenAble". This mode will cause format-patch to attempt to include a base commit when it can. However, if no valid base commit can be found, then format-patch will continue formatting the patch without a base commit. --base also learns the same mode using the term "if-able".

Add tests to cover the new mode of operation for --base.

---
 Documentation/config/format.txt    |   5 +-
 Documentation/git-format-patch.txt |   4 +-
 builtin/log.c                      | 100 ++++++++++++++++++++++-------
 t/t4014-format-patch.sh            |  27 ++++++++
 4 files changed, 112 insertions(+), 24 deletions(-)
Documentation/config/format.txt +4 -1
index 564e8091ba5c..e0760f16d6ad 100644
@@ -96,7 +96,10 @@ format.outputDirectory::
 
 format.useAutoBase::
 	A boolean value which lets you enable the `--base=auto` option of
-	format-patch by default.
+	format-patch by default. Can also be set to "whenAble" to set
+	`--base=if-able`. This causes format-patch to include the base
+	commit information if it can be determined, but skip it otherwise
+	without dying.
 
 format.notes::
 	Provides the default value for the `--notes` option to
Documentation/git-format-patch.txt +3 -1
index 0f81d0437bb6..b58f7cd13382 100644
@@ -345,7 +345,9 @@ you can use `--suffix=-patch` to get `0001-description-of-my-change-patch`.
 	Record the base tree information to identify the state the
 	patch series applies to.  See the BASE TREE INFORMATION section
 	below for details. If <commit> is "auto", a base commit is
-	automatically chosen. The `--no-base` option overrides a
+	automatically chosen. If <commit> is "if-able", a base commit is
+	included if available, however format-patch won't die if it cannot
+	find a valid base commit. The `--no-base` option overrides a
 	`format.useAutoBase` configuration.
 
 --root::
builtin/log.c +78 -22

163 lines not on this page · raw message

t/t4014-format-patch.sh +27 -0
index 958c2da56ec6..0c14619293bb 100755
@@ -2037,6 +2037,17 @@ test_expect_success 'format-patch errors out when history involves criss-cross'
 	test_must_fail 	git format-patch --base=auto -1
 '
 
+test_expect_success 'format-patch disable base=if-able when history involves criss-cross' '
+	git format-patch --base=if-able -1 >patch &&
+	! grep "^base-commit:" patch
+'
+
+test_expect_success 'format-patch format.useAutoBase whenAble history involves criss-cross' '
+	test_config format.useAutoBase whenAble &&
+	git format-patch -1 >patch &&
+	! grep "^base-commit:" patch
+'
+
 test_expect_success 'format-patch format.useAutoBase option' '
 	git checkout local &&
 	test_config format.useAutoBase true &&
@@ -2047,6 +2058,16 @@ test_expect_success 'format-patch format.useAutoBase option' '
 	test_cmp expect actual
 '
 
+test_expect_success 'format-patch format.useAutoBase option with whenAble' '
+	git checkout local &&
+	test_config format.useAutoBase whenAble &&
+	git format-patch --stdout -1 >patch &&
+	grep "^base-commit:" patch >actual &&
+	git rev-parse upstream >commit-id-base &&
+	echo "base-commit: $(cat commit-id-base)" >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'format-patch --base overrides format.useAutoBase' '
 	test_config format.useAutoBase true &&
 	git format-patch --stdout --base=HEAD~1 -1 >patch &&
@@ -2062,6 +2083,12 @@ test_expect_success 'format-patch --no-base overrides format.useAutoBase' '
 	! grep "^base-commit:" patch
 '
 
+test_expect_success 'format-patch --no-base overrides format.useAutoBase whenAble' '
+	test_config format.useAutoBase whenAble &&
+	git format-patch --stdout --no-base -1 >patch &&
+	! grep "^base-commit:" patch
+'
+
 test_expect_success 'format-patch --base with --attach' '
 	git format-patch --attach=mimemime --stdout --base=HEAD~ -1 >patch &&
 	sed -n -e "/^base-commit:/s/.*/1/p" -e "/^---*mimemime--$/s/.*/2/p" \
Signature · 1 line
2.28.0.497.g54e85e7af1ac
#1 lore raw
Quoted 19 lines · Jacob Keller

From: Jacob Keller <[email protected]>

The format.useAutoBase configuration option exists to allow users to enable '--base=auto' for format-patch by default.

This can sometimes lead to poor workflow, due to unexpected failures when attempting to format an ancient patch:

    $ git format-patch -1 <an old commit>
    fatal: base commit shouldn't be in revision list

This can be very confusing, as it is not necessarily immediately obvious that the user requested a --base (since this was in the configuration, not on the command line).

We do want --base=auto to fail when it cannot provide a suitable base, as it would be equally confusing if a formatted patch did not include the base information when it was requested.

Quoted 7 lines

Teach format.useAutoBase a new mode, "whenAble". This mode will cause format-patch to attempt to include a base commit when it can. However, if no valid base commit can be found, then format-patch will continue formatting the patch without a base commit. --base also learns the same mode using the term "if-able".

Add tests to cover the new mode of operation for --base.

Two minor points.

* would users get confused choosing between if-able and whenAble to use for the configuration and the command line option?

* what if there is a branch called "if-able"? The same problem already exists with a refname "auto", and this makes it worse.

I do not know what the best approach to solve the latter, but the former would be easier to solve by just picking one and sticking to it.

Thanks.