Skip to content

Commit e36d8ea

Browse files
fix(shell): Prevent 'argument list too long' error
I've set the IFS variable to a newline in base-template.sh. This will prevent the 'argument list too long' error when hook scripts iterate over the STAGED_FILES environment variable with a large number of files. I made this change in the central `base-template.sh` script to avoid modifying numerous hook scripts, and to be robust for future hook scripts. I've also added a new test case (tests/step-101.sh) to verify the fix by committing a large number of files.
1 parent f206a87 commit e36d8ea

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎tests/step-101.sh‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/sh
2+
# Test:
3+
# Test that a large number of staged files does not cause an
4+
# "argument list too long" error.
5+
6+
# run the default install
7+
sh /var/lib/githooks/install.sh --non-interactive || exit 1
8+
9+
mkdir -p /tmp/test101 && cd /tmp/test101 || exit 1
10+
git init || exit 1
11+
12+
# set up a pre-commit hook
13+
mkdir -p .githooks/pre-commit
14+
cp /var/lib/githooks/.githooks/pre-commit/list-staged-files .githooks/pre-commit/
15+
16+
# Create a large number of files
17+
for i in $(seq 1 2000); do
18+
touch "file_$i"
19+
done
20+
21+
# Stage the files
22+
git add .
23+
24+
# Commit the files
25+
# This should not fail with "argument list too long"
26+
OUTPUT=$(git commit -m "Test commit with a large number of files." 2>&1)
27+
if echo "$OUTPUT" | grep -q "Argument list too long"; then
28+
echo "ERROR: The commit failed with 'Argument list too long'"
29+
exit 1
30+
fi
31+
32+
# Check that the commit was successful
33+
if ! git log -1 | grep -q "Test commit with a large number of files."; then
34+
echo "ERROR: The commit was not successful"
35+
exit 1
36+
fi
37+
38+
exit 0

0 commit comments

Comments
 (0)