Skip to content

Commit 0bd19e1

Browse files
committed
add --depth to git-clone
1 parent d7fdc79 commit 0bd19e1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

registry/coder/modules/git-clone/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,20 @@ module "git-clone" {
180180
base_dir = "~/projects/coder"
181181
}
182182
```
183+
184+
## Git shallow clone
185+
186+
Limit the clone history to speed-up workspace startup by setting `depth`.
187+
188+
When `depth` is greater than `0` the module runs `git clone --depth <depth>`.
189+
If not defined, the default, `0`, performs a full clone.
190+
191+
```tf
192+
module "git-clone" {
193+
count = data.coder_workspace.me.start_count
194+
source = "registry.coder.com/modules/git-clone/coder"
195+
version = "1.0.18"
196+
agent_id = coder_agent.example.id
197+
url = "https://github.com/coder/coder"
198+
depth = 1 # shallow clone
199+
}

registry/coder/modules/git-clone/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ variable "folder_name" {
5656
default = ""
5757
}
5858

59+
variable "depth" {
60+
description = "If > 0, perform a shallow clone using this depth."
61+
type = number
62+
default = 0
63+
}
64+
5965
locals {
6066
# Remove query parameters and fragments from the URL
6167
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
@@ -113,6 +119,7 @@ resource "coder_script" "git_clone" {
113119
CLONE_PATH = local.clone_path,
114120
REPO_URL : local.clone_url,
115121
BRANCH_NAME : local.branch_name,
122+
DEPTH = var.depth,
116123
})
117124
display_name = "Git Clone"
118125
icon = "/icon/git.svg"

registry/coder/modules/git-clone/run.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CLONE_PATH="${CLONE_PATH}"
55
BRANCH_NAME="${BRANCH_NAME}"
66
# Expand home if it's specified!
77
CLONE_PATH="$${CLONE_PATH/#\~/$${HOME}}"
8+
DEPTH="${DEPTH}"
89

910
# Check if the variable is empty...
1011
if [ -z "$REPO_URL" ]; then
@@ -36,10 +37,18 @@ fi
3637
if [ -z "$(ls -A "$CLONE_PATH")" ]; then
3738
if [ -z "$BRANCH_NAME" ]; then
3839
echo "Cloning $REPO_URL to $CLONE_PATH..."
39-
git clone "$REPO_URL" "$CLONE_PATH"
40+
if [ "$DEPTH" -gt 0 ]; then
41+
git clone --depth "$DEPTH" "$REPO_URL" "$CLONE_PATH"
42+
else
43+
git clone "$REPO_URL" "$CLONE_PATH"
44+
fi
4045
else
4146
echo "Cloning $REPO_URL to $CLONE_PATH on branch $BRANCH_NAME..."
42-
git clone "$REPO_URL" -b "$BRANCH_NAME" "$CLONE_PATH"
47+
if [ "$DEPTH" -gt 0 ]; then
48+
git clone --depth "$DEPTH" -b "$BRANCH_NAME" "$REPO_URL" "$CLONE_PATH"
49+
else
50+
git clone "$REPO_URL" -b "$BRANCH_NAME" "$CLONE_PATH"
51+
fi
4352
fi
4453
else
4554
echo "$CLONE_PATH already exists and isn't empty, skipping clone!"

0 commit comments

Comments
 (0)