Skip to content

Commit a2774d7

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 a2774d7

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/git-lfs/install.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,18 @@ if [ "${AUTO_PULL}" != "true" ]; then
265265
exit 0
266266
fi
267267
268-
# Check if repo is a git lfs repo.
269-
if ! git lfs ls-files > /dev/null 2>&1; then
268+
# Check if repo is a git lfs enabled repo by running 'git lfs ls-files'
269+
if ! ls_files_output=$(git lfs ls-files 2>&1); then
270+
echo "(!) Skipping automatic 'git lfs pull' because 'git lfs ls-files' failed"
271+
exit 0
272+
fi
273+
274+
# Check if 'git lfs ls-files' output is empty
275+
if [ -z "$ls_files_output" ]; then
270276
echo "(!) Skipping automatic 'git lfs pull' because no git lfs files were detected"
271277
exit 0
272278
fi
279+
273280
git lfs install
274281
git lfs pull
275282
EOF

test/git-lfs/noLfsFiles.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
# We should already be in a git repository created by initializeCommand
18+
# Verify we're in a git repository
19+
check "in git repository" git rev-parse --is-inside-work-tree
20+
21+
# Test that git lfs ls-files returns empty output
22+
check "git lfs ls-files returns empty output" test -z "$(git lfs ls-files 2>/dev/null)"
23+
24+
# Verify no LFS hooks exist initially (the script should have already run during postCreateCommand)
25+
HOOKS_COUNT=$(find .git/hooks -type f ! -name '*.sample' | wc -l)
26+
check "no git hooks installed after postCreateCommand" test "$HOOKS_COUNT" -eq 0
27+
28+
# Double check: specifically look for the hooks that git lfs install would create
29+
check "no post-merge hook" test ! -f .git/hooks/post-merge
30+
check "no pre-push hook" test ! -f .git/hooks/pre-push
31+
check "no post-commit hook" test ! -f .git/hooks/post-commit
32+
check "no post-checkout hook" test ! -f .git/hooks/post-checkout
33+
34+
# Report results
35+
reportResults

test/git-lfs/scenarios.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
}
2020
}
2121
},
22+
"noLfsFiles": {
23+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
24+
"initializeCommand": "git init && git config user.email '[email protected]' && git config user.name 'Test User' && echo 'regular file content' > regular.txt && git add regular.txt && git commit -m 'Initial commit without LFS'",
25+
"remoteUser": "vscode",
26+
"features": {
27+
"git-lfs": {}
28+
}
29+
},
2230
"use_github": {
2331
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
2432
"remoteUser": "vscode",

0 commit comments

Comments
 (0)