1
0
mirror of https://github.com/git/git synced 2024-06-30 22:54:27 +00:00
git/t/t4153-am-resume-override-opts.sh
Jeff King 53ce2e3f0a am: add explicit "--retry" option
After a patch fails, you can ask "git am" to try applying it again with
new options by running without any of the resume options. E.g.:

  git am <patch
  # oops, it failed; let's try again
  git am --3way

But since this second command has no explicit resume option (like
"--continue"), it looks just like an invocation to read a fresh patch
from stdin. To avoid confusing the two cases, there are some heuristics,
courtesy of 8d18550318 (builtin-am: reject patches when there's a
session in progress, 2015-08-04):

	if (in_progress) {
		/*
		 * Catch user error to feed us patches when there is a session
		 * in progress:
		 *
		 * 1. mbox path(s) are provided on the command-line.
		 * 2. stdin is not a tty: the user is trying to feed us a patch
		 *    from standard input. This is somewhat unreliable -- stdin
		 *    could be /dev/null for example and the caller did not
		 *    intend to feed us a patch but wanted to continue
		 *    unattended.
		 */
		if (argc || (resume_mode == RESUME_FALSE && !isatty(0)))
			die(_("previous rebase directory %s still exists but mbox given."),
				state.dir);

		if (resume_mode == RESUME_FALSE)
			resume_mode = RESUME_APPLY;
		[...]

So if no resume command is given, then we require that stdin be a tty,
and otherwise complain about (potentially) receiving an mbox on stdin.
But of course you might not actually have a terminal available! And
sadly there is no explicit way to hit this same code path; this is the
only place that sets RESUME_APPLY. So you're stuck, and scripts like our
test suite have to bend over backwards to create a pseudo-tty.

Let's provide an explicit option to trigger this mode. The code turns
out to be quite simple; just setting "resume_mode" to RESUME_FALSE is
enough to dodge the tty check, and then our state is the same as it
would be with the heuristic case (which we'll continue to allow).

When we don't have a session in progress, there's already code to
complain when resume_mode is set (but we'll add a new test to cover
that).

To test the new option, we'll convert the existing tests that rely on
the fake stdin tty. That lets us test them on more platforms, and will
let us simplify test_terminal a bit in a future patch.

It does, however, mean we're not testing the tty heuristic at all.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-06-06 10:07:41 -07:00

107 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
test_description='git-am command-line options override saved options'
. ./test-lib.sh
format_patch () {
git format-patch --stdout -1 "$1" >"$1".eml
}
test_expect_success 'setup' '
test_commit initial file &&
test_commit first file &&
git checkout initial &&
git mv file file2 &&
test_tick &&
git commit -m renamed-file &&
git tag renamed-file &&
git checkout -b side initial &&
test_commit side1 file &&
test_commit side2 file &&
format_patch side1 &&
format_patch side2
'
test_expect_success '--retry fails without in-progress operation' '
test_must_fail git am --retry 2>err &&
test_grep "operation not in progress" err
'
test_expect_success '--3way overrides --no-3way' '
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout renamed-file &&
# Applying side1 will fail as the file has been renamed.
test_must_fail git am --no-3way side[12].eml &&
test_path_is_dir .git/rebase-apply &&
test_cmp_rev renamed-file HEAD &&
test -z "$(git ls-files -u)" &&
# Applying side1 with am --3way will succeed due to the threeway-merge.
# Applying side2 will fail as --3way does not apply to it.
test_must_fail git am --retry --3way &&
test_path_is_dir .git/rebase-apply &&
test side1 = "$(cat file2)"
'
test_expect_success '--no-quiet overrides --quiet' '
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout first &&
# Applying side1 will be quiet.
test_must_fail git am --quiet side[123].eml >out &&
test_path_is_dir .git/rebase-apply &&
test_grep ! "^Applying: " out &&
echo side1 >file &&
git add file &&
# Applying side1 will not be quiet.
# Applying side2 will be quiet.
git am --no-quiet --continue >out &&
echo "Applying: side1" >expected &&
test_cmp expected out
'
test_expect_success '--signoff overrides --no-signoff' '
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout first &&
test_must_fail git am --no-signoff side[12].eml &&
test_path_is_dir .git/rebase-apply &&
echo side1 >file &&
git add file &&
git am --signoff --continue &&
# Applied side1 will be signed off
echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >expected &&
git cat-file commit HEAD^ | grep "Signed-off-by:" >actual &&
test_cmp expected actual &&
# Applied side2 will not be signed off
test $(git cat-file commit HEAD | grep -c "Signed-off-by:") -eq 0
'
test_expect_success '--reject overrides --no-reject' '
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout first &&
rm -f file.rej &&
test_must_fail git am --no-reject side1.eml &&
test_path_is_dir .git/rebase-apply &&
test_path_is_missing file.rej &&
test_must_fail git am --retry --reject </dev/zero &&
test_path_is_dir .git/rebase-apply &&
test_path_is_file file.rej
'
test_done