Skip to content

Commit 2952d87

Browse files
authored
add a postCreateCommand to git-lfs Feature (devcontainers#511)
* add a postCreateCommand to git-lfs Feature * debug passing locally but not in CI * update scenario * add GIT_LFS_SKIP_SMUDGE=1 to prevent fetching lfs arifacts in github actions * arguments should be booleans * move example repo to devcontainers org * place script literally anywhere other than /tmp
1 parent 1118b99 commit 2952d87

5 files changed

Lines changed: 83 additions & 1 deletion

File tree

src/git-lfs/devcontainer-feature.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "git-lfs",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"name": "Git Large File Support (LFS)",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/git-lfs",
66
"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.",
@@ -13,8 +13,14 @@
1313
],
1414
"default": "latest",
1515
"description": "Select version of Git LFS to install"
16+
},
17+
"autoPull": {
18+
"type": "boolean",
19+
"default": true,
20+
"description": "Automatically pull LFS files when creating the container. When false, running 'git lfs pull' in the container will have the same effect."
1621
}
1722
},
23+
"postCreateCommand": "/usr/local/share/pull-git-lfs-artifacts.sh",
1824
"installsAfter": [
1925
"ghcr.io/devcontainers/features/common-utils"
2026
]

src/git-lfs/install.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# Maintainer: The VS Code and Codespaces Teams
99

1010
GIT_LFS_VERSION=${VERSION:-"latest"}
11+
AUTO_PULL=${AUTOPULL:="true"}
1112

1213
GIT_LFS_ARCHIVE_GPG_KEY_URI="https://packagecloud.io/github/git-lfs/gpgkey"
1314
GIT_LFS_ARCHIVE_ARCHITECTURES="amd64 arm64"
@@ -185,6 +186,37 @@ if [ "${use_github}" = "true" ]; then
185186
install_using_github
186187
fi
187188

189+
# --- Generate a 'pull-git-lfs-artifacts.sh' script to be executed by the 'postCreateCommand' lifecycle hook
190+
PULL_GIT_LFS_SCRIPT_PATH="/usr/local/share/pull-git-lfs-artifacts.sh"
191+
192+
tee "$PULL_GIT_LFS_SCRIPT_PATH" > /dev/null \
193+
<< EOF
194+
#!/bin/sh
195+
set -e
196+
AUTO_PULL=${AUTO_PULL}
197+
EOF
198+
199+
tee -a "$PULL_GIT_LFS_SCRIPT_PATH" > /dev/null \
200+
<< 'EOF'
201+
202+
echo "Fetching git lfs artifacts..."
203+
204+
if [ "${AUTO_PULL}" != "true" ]; then
205+
echo "(!) Skipping 'git lfs pull' because 'autoPull' is not set to 'true'"
206+
exit 0
207+
fi
208+
209+
# Check if repo is a git lfs repo.
210+
if ! git lfs ls-files > /dev/null 2>&1; then
211+
echo "(!) Skipping automatic 'git lfs pull' because no git lfs files were detected"
212+
exit 0
213+
fi
214+
215+
git lfs pull
216+
EOF
217+
218+
chmod 755 "$PULL_GIT_LFS_SCRIPT_PATH"
219+
188220
# Clean up
189221
rm -rf /var/lib/apt/lists/*
190222

test/git-lfs/autoPullDisabled.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
check "target file exists" cat big-file-1.txt
9+
check "lfs file has not been expanded" cat "big-file-1.txt" | grep "git-lfs\.github\.com"
10+
11+
reportResults

test/git-lfs/autoPullEnabled.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
check "target file exists" cat big-file-1.txt
9+
check "lfs file has been expanded" cat "big-file-1.txt" | grep "this is test file 1"
10+
11+
reportResults

test/git-lfs/scenarios.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"autoPullEnabled": {
3+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
4+
"initializeCommand": "(GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/devcontainers/git-lfs-example /tmp/testRepo-1 || true) && (cp -r /tmp/testRepo-1/.git* . && cp -r /tmp/testRepo-1/* .)",
5+
"remoteUser": "vscode",
6+
"features": {
7+
"git-lfs": {
8+
"autoPull": true
9+
}
10+
}
11+
},
12+
"autoPullDisabled": {
13+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
14+
"initializeCommand": "(GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/devcontainers/git-lfs-example /tmp/testRepo-2 || true) && (cp -r /tmp/testRepo-2/.git* . && cp -r /tmp/testRepo-2/* .)",
15+
"remoteUser": "vscode",
16+
"features": {
17+
"git-lfs": {
18+
"autoPull": false
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)