diff --git a/src/git-lfs/devcontainer-feature.json b/src/git-lfs/devcontainer-feature.json index fe23e02cf..a8d65b714 100644 --- a/src/git-lfs/devcontainer-feature.json +++ b/src/git-lfs/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "git-lfs", - "version": "1.2.5", + "version": "1.2.6", "name": "Git Large File Support (LFS)", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/git-lfs", "description": "Installs Git Large File Support (Git LFS) along with needed dependencies. Useful for base Dockerfiles that often are missing required install dependencies like git and curl.", diff --git a/src/git-lfs/install.sh b/src/git-lfs/install.sh index 71066c7b3..75f9e7daa 100755 --- a/src/git-lfs/install.sh +++ b/src/git-lfs/install.sh @@ -265,11 +265,18 @@ if [ "${AUTO_PULL}" != "true" ]; then exit 0 fi -# Check if repo is a git lfs repo. -if ! git lfs ls-files > /dev/null 2>&1; then +# Check if repo is a git lfs enabled repo by running 'git lfs ls-files' +if ! ls_files_output=$(git lfs ls-files 2>&1); then + echo "(!) Skipping automatic 'git lfs pull' because 'git lfs ls-files' failed" + exit 0 +fi + +# Check if 'git lfs ls-files' output is empty +if [ -z "$ls_files_output" ]; then echo "(!) Skipping automatic 'git lfs pull' because no git lfs files were detected" exit 0 fi + git lfs install git lfs pull EOF diff --git a/test/git-lfs/noLfsFiles.sh b/test/git-lfs/noLfsFiles.sh new file mode 100755 index 000000000..3099762f1 --- /dev/null +++ b/test/git-lfs/noLfsFiles.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +set -e + +# Test for git-lfs behavior with repos that have no LFS files +# This tests the fix for the issue where git lfs install was running unnecessarily + +# Optional: Import test library +source dev-container-features-test-lib + +# Test that git-lfs is installed +check "git-lfs version" git-lfs --version + +# Test the generated script exists +check "pull script exists" test -f /usr/local/share/pull-git-lfs-artifacts.sh + +# We should already be in a git repository created by initializeCommand +# Verify we're in a git repository +check "in git repository" git rev-parse --is-inside-work-tree + +# Test that git lfs ls-files returns empty output +check "git lfs ls-files returns empty output" test -z "$(git lfs ls-files 2>/dev/null)" + +# Verify no LFS hooks exist initially (the script should have already run during postCreateCommand) +HOOKS_COUNT=$(find .git/hooks -type f ! -name '*.sample' | wc -l) +check "no git hooks installed after postCreateCommand" test "$HOOKS_COUNT" -eq 0 + +# Double check: specifically look for the hooks that git lfs install would create +check "no post-merge hook" test ! -f .git/hooks/post-merge +check "no pre-push hook" test ! -f .git/hooks/pre-push +check "no post-commit hook" test ! -f .git/hooks/post-commit +check "no post-checkout hook" test ! -f .git/hooks/post-checkout + +# Report results +reportResults \ No newline at end of file diff --git a/test/git-lfs/scenarios.json b/test/git-lfs/scenarios.json index 009dfbb64..e35a96cf7 100644 --- a/test/git-lfs/scenarios.json +++ b/test/git-lfs/scenarios.json @@ -19,6 +19,14 @@ } } }, + "noLfsFiles": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "initializeCommand": "git init && git config user.email 'test@example.com' && git config user.name 'Test User' && echo 'regular file content' > regular.txt && git add regular.txt && git commit -m 'Initial commit without LFS'", + "remoteUser": "vscode", + "features": { + "git-lfs": {} + } + }, "use_github": { "image": "mcr.microsoft.com/devcontainers/base:ubuntu", "remoteUser": "vscode",