Skip to content

Commit f20c448

Browse files
committed
Fix git-lfs unnecessary hook installation
Fix an issue where git lfs install was running unnecessarily in repositories without LFS files, causing hooks to be installed when they shouldn't be. Changes: - Update src/git-lfs/install.sh to properly check for empty git lfs ls-files output - Add comprehensive test (test/git-lfs/noLfsFiles.sh) to verify hooks are not installed when no LFS files are present - Update test scenarios to include the new test case The fix changes the condition from checking command exit status to checking if the output is empty, preventing unnecessary hook installation in non-LFS repos.
1 parent e3e3ed7 commit f20c448

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/git-lfs/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ if [ "${AUTO_PULL}" != "true" ]; then
266266
fi
267267
268268
# Check if repo is a git lfs repo.
269-
if ! git lfs ls-files > /dev/null 2>&1; then
269+
if [ -z "$(git lfs ls-files 2>/dev/null)" ]; then
270270
echo "(!) Skipping automatic 'git lfs pull' because no git lfs files were detected"
271271
exit 0
272272
fi

test/git-lfs/noLfsFiles.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Test for git-lfs behavior with repos that have no LFS files
6+
# This tests the fix for the issue where git lfs install was running unnecessarily
7+
8+
# Optional: Import test library
9+
source dev-container-features-test-lib
10+
11+
# Test that git-lfs is installed
12+
check "git-lfs version" git-lfs --version
13+
14+
# Test the generated script exists
15+
check "pull script exists" test -f /usr/local/share/pull-git-lfs-artifacts.sh
16+
17+
# Create a mock git repo without LFS files to test the script
18+
mkdir -p /tmp/test-no-lfs-repo
19+
cd /tmp/test-no-lfs-repo
20+
git init
21+
git config user.email "[email protected]"
22+
git config user.name "Test User"
23+
echo "regular file content" > regular.txt
24+
git add regular.txt
25+
git commit -m "Initial commit without LFS"
26+
27+
# Test that git lfs ls-files returns empty output (our fix checks this)
28+
check "git lfs ls-files returns empty output" test -z "$(git lfs ls-files 2>/dev/null)"
29+
30+
# Verify no hooks exist before running the script
31+
HOOKS_BEFORE=$(find .git/hooks -type f ! -name '*.sample' | wc -l)
32+
check "no git hooks before script" test "$HOOKS_BEFORE" -eq 0
33+
34+
# Run the actual pull-git-lfs-artifacts.sh script to test the real behavior
35+
echo "Running pull-git-lfs-artifacts.sh script..."
36+
/usr/local/share/pull-git-lfs-artifacts.sh
37+
38+
# Verify that git lfs install was NOT executed by checking no hooks were installed
39+
HOOKS_AFTER=$(find .git/hooks -type f ! -name '*.sample' | wc -l)
40+
check "no git hooks installed after script" test "$HOOKS_AFTER" -eq 0
41+
42+
# Double check: specifically look for the hooks that git lfs install would create
43+
check "no post-merge hook" test ! -f .git/hooks/post-merge
44+
check "no pre-push hook" test ! -f .git/hooks/pre-push
45+
check "no post-commit hook" test ! -f .git/hooks/post-commit
46+
check "no post-checkout hook" test ! -f .git/hooks/post-checkout
47+
48+
# Clean up
49+
cd /tmp
50+
rm -rf /tmp/test-no-lfs-repo
51+
52+
reportResults

test/git-lfs/scenarios.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
}
2020
}
2121
},
22+
"noLfsFiles": {
23+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
24+
"remoteUser": "vscode",
25+
"features": {
26+
"git-lfs": {}
27+
}
28+
},
2229
"use_github": {
2330
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
2431
"remoteUser": "vscode",

0 commit comments

Comments
 (0)