am: add explicit "--retry" option
commit53ce2e3f0abf356fb0d2638783fe06711c5337d6
authorJeff King <peff@peff.net>
Thu, 6 Jun 2024 08:21:14 +0000 (6 04:21 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 6 Jun 2024 17:07:41 +0000 (6 10:07 -0700)
tree1aa4282ea221f0833ca77b6931926fa281f49fdb
parent337b4d400023d22207bcc3c29e9ebab31bf96fc2
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>
Documentation/git-am.txt
builtin/am.c
t/t4153-am-resume-override-opts.sh