Skip to content

Commit 4dca1aa

Browse files
drafnelgitster
authored andcommitted
git-submodule.sh: preserve stdin for the command spawned by foreach
The user-supplied command spawned by 'submodule foreach' loses its connection to the original standard input. Instead, it is connected to the output of a pipe within the git-submodule script. The user-supplied command supplied to 'submodule foreach' is spawned within a while loop which is being piped into. Due to the way shells implement piping output to a while loop, a subshell is created with its standard input attached to the output of the pipe. This results in all of the commands executed within the while loop to have their stdins modified in the same way, including the user-supplied command. This can cause a problem if the command requires reading from stdin or if it changes its behavior based on whether stdin is a tty or not. For example, this problem was noticed when trying to execute the following: git submodule foreach git shortlog --since=two.weeks.ago which printed a message about entering the first submodule and produced no further output and exited with a status of zero. In this case, shortlog detected that it was not connected to a tty, and since no revision was supplied as an argument, it attempted to read the list of revisions from standard input. Instead, it slurped up the list of submodules that was being piped to the enclosing while loop and caused that loop to end early without processing the remaining submodules. Work around this behavior by saving the original standard input file descriptor before the while loop, and restoring it when spawning the user-supplied command. This fixes the tests in t7407. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 91cd7e4 commit 4dca1aa

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

git-submodule.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ cmd_foreach()
285285

286286
toplevel=$(pwd)
287287

288+
# dup stdin so that it can be restored when running the external
289+
# command in the subshell (and a recursive call to this function)
290+
exec 3<&0
291+
288292
module_list |
289293
while read mode sha1 stage path
290294
do
@@ -301,7 +305,7 @@ cmd_foreach()
301305
then
302306
cmd_foreach "--recursive" "$@"
303307
fi
304-
) ||
308+
) <&3 3<&- ||
305309
die "Stopping at '$path'; script returned non-zero status."
306310
fi
307311
done

t/t7407-submodule-foreach.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ test_expect_success 'use "update --recursive nested1" to checkout all submodules
288288
)
289289
'
290290

291-
test_expect_failure 'command passed to foreach retains notion of stdin' '
291+
test_expect_success 'command passed to foreach retains notion of stdin' '
292292
(
293293
cd super &&
294294
git submodule foreach echo success >../expected &&
@@ -297,7 +297,7 @@ test_expect_failure 'command passed to foreach retains notion of stdin' '
297297
test_cmp expected actual
298298
'
299299

300-
test_expect_failure 'command passed to foreach --recursive retains notion of stdin' '
300+
test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
301301
(
302302
cd clone2 &&
303303
git submodule foreach --recursive echo success >../expected &&

0 commit comments

Comments
 (0)