From 2c73a25e2797c7fed69804055cf74433a65ad082 Mon Sep 17 00:00:00 2001 From: YAEGASHI Takeshi Date: Sun, 20 Jul 2025 07:02:24 +0000 Subject: [PATCH 1/2] 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. --- src/git-lfs/install.sh | 11 +++++++++-- test/git-lfs/noLfsFiles.sh | 35 +++++++++++++++++++++++++++++++++++ test/git-lfs/scenarios.json | 8 ++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100755 test/git-lfs/noLfsFiles.sh 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", From 20f9b788dcee164a223e5e6d746671c5ce5f016f Mon Sep 17 00:00:00 2001 From: YAEGASHI Takeshi Date: Mon, 21 Jul 2025 14:25:25 +0000 Subject: [PATCH 2/2] Bump git-lfs patch version --- src/git-lfs/devcontainer-feature.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.",