From 8c3e7b2179e922d8eefa6fdcd12bd4c5e6c41d0c Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Mon, 25 Oct 2021 11:29:36 -0400 Subject: [PATCH] Disallow committing empty patches by default `stg commit` now refuses to commit empty patches by default, but also learns the --allow-empty flag, which mirrors `git commit --allow-empty`, to override this behavior. Addresses #158 Signed-off-by: Peter Grayson --- stgit/commands/commit.py | 25 ++++++++++++++++++++++++- t/t1009-gpg.sh | 2 +- t/t1303-commit.sh | 22 +++++++++++++--------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/stgit/commands/commit.py b/stgit/commands/commit.py index 5ed995e..eacd560 100644 --- a/stgit/commands/commit.py +++ b/stgit/commands/commit.py @@ -21,7 +21,12 @@ along with this program; if not, see http://www.gnu.org/licenses/. help = 'Permanently store the applied patches into the stack base' kind = 'stack' -usage = ['', '[--] ', '-n NUM', '--all'] +usage = [ + '[options]', + '[options] [--] ', + '[options] -n NUM', + '[options] --all', +] description = """ Merge one or more patches into the base of the current stack and remove them from the series while advancing the base. This is the @@ -45,6 +50,11 @@ options = [ short='Commit the specified number of patches', ), opt('-a', '--all', action='store_true', short='Commit all applied patches'), + opt( + '--allow-empty', + action='store_true', + short='Allow empty patches to be committed', + ), ] directory = DirectoryHasRepository() @@ -76,6 +86,19 @@ def func(parser, options, args): if not patches: raise CmdException('No patches to commit') + if not options.allow_empty: + empty_patches = [ + pn for pn in patches + if stack.patches[pn].data.tree == stack.patches[pn].data.parent.data.tree + ] + if empty_patches: + raise CmdException( + 'Empty patch%s (override with --allow-empty): %s' % ( + '' if len(empty_patches) == 1 else 'es', + ', ' .join(empty_patches) + ) + ) + iw = stack.repository.default_iw def allow_conflicts(trans): diff --git a/t/t1009-gpg.sh b/t/t1009-gpg.sh index 1606bc8..cd83994 100755 --- a/t/t1009-gpg.sh +++ b/t/t1009-gpg.sh @@ -48,7 +48,7 @@ test_expect_success GPG \ test_expect_success GPG \ 'Patch remains signed after stg commit' ' - stg commit -n 1 && + stg commit --allow-empty -n 1 && stg pop -a && git verify-commit HEAD ' diff --git a/t/t1303-commit.sh b/t/t1303-commit.sh index cc3574f..a9637ea 100755 --- a/t/t1303-commit.sh +++ b/t/t1303-commit.sh @@ -3,27 +3,31 @@ test_description='Test stg commit' . ./test-lib.sh test_expect_success 'Initialize the StGit repository' ' - stg init -' - -test_expect_success 'Commit middle patch' ' + stg init && stg new -m p1 && stg new -m p2 && stg new -m p3 && stg new -m p4 && - stg pop && - stg commit p2 && + stg pop +' + +test_expect_success 'Attempt to commit an empty patch' ' + command_error stg commit p2 2>&1 | grep "Empty patch" +' + +test_expect_success 'Commit middle patch' ' + stg commit --allow-empty p2 && test "$(echo $(stg series))" = "+ p1 > p3 - p4" ' test_expect_success 'Commit first patch' ' - stg commit && + stg commit --allow-empty && test "$(echo $(stg series))" = "> p3 - p4" ' test_expect_success 'Commit all patches' ' stg push && - stg commit -a && + stg commit -a --allow-empty && test "$(echo $(stg series))" = "" ' @@ -33,7 +37,7 @@ test_expect_success 'Commit when top != head (should fail)' ' stg new -m foo && git reset --hard HEAD^ && h=$(git rev-parse HEAD) - command_error stg commit && + command_error stg commit --allow-empty && test "$(git rev-parse HEAD)" = "$h" && test "$(echo $(stg series))" = "> foo" ' -- 2.11.4.GIT