am: don't infloop for an empty input file
PATCH 1/1 am: don't infloop for an empty input file
Today, "git am" surprised me.
I mistakenly ran it on an empty file and it went into an infinite loop.
: > e && git am e
To fix it, I made a failing bourne shell "read" break out of the offending loop. Looking through git-am.sh for other instances, I did find one, but didn't try to address it here.
action=again
while test "$action" = again
do
gettextln "Commit Body is:"
echo "--------------------------"
cat "$dotest/final-commit"
echo "--------------------------"
# TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a] # in your translation. The program will only accept English # input at this point.
gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
may infloop-> read reply
case "$reply" in
In that case (when someone hits ^D in response to that prompt?), you may want to exit altogether.
In the test addition, I didn't try to handle potentially-inflooping code. In coreutils tests, it's easy (since it includes the timeout program):
I would just prefix the command with something like "timeout 10", but the timeout command is not universally available. And besides, the infloop is supposed to be fixed, now.
Here's a patch for the infloop I triggered:
[Noticed with and tested against master v1.7.9.2-262-gba998d3,
but seems to apply also to maint. ]
-- >8 --
git-am.sh's check_patch_format function would attempt to preview the patch to guess its format, but would go into an infinite loop when the patch file happened to be empty. The solution: exit the loop when "read" fails, not when the line var, "$l1" becomes empty.
--- git-am.sh | 2 +- t/t4150-am.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
git-am.sh +1 -1
@@ -202,7 +202,7 @@ check_patch_format () { l1= while test -z "$l1" do - read l1 + read l1 || break done read l2 read l3
t/t4150-am.sh +10 -0
@@ -505,4 +505,14 @@ test_expect_success 'am -q is quiet' ' ! test -s output.out ' +test_expect_success 'am empty-file does not infloop' ' + rm -fr .git/rebase-apply && + git reset --hard && + touch empty-file && + test_tick && + { git am empty-file > actual 2>&1 && false || :; } && + echo Patch format detection failed. >expected && + test_cmp expected actual +' + test_done
--
1.7.9.2.263.g9be8b7
Quoted 2 lines · Jim Meyering
Today, "git am" surprised me.
I mistakenly ran it on an empty file and it went into an infinite loop.
Yeah, that is an embarrassing regression in 1.7.7.
Thanks for noticing.