Skip to content

Commit 0a3c9b0

Browse files
feat: add --depth to git-clone module to support shallow clones (#197)
## Description <!-- Briefly describe what this PR does and why --> Adds support for a `depth` variable to the git-clone module. If a repo is large, a shallow clone makes the `git clone` a lot faster ## Type of Change - [ ] New module - [ ] Bug fix - [x] Feature/enhancement - [ ] Documentation - [ ] Other ## Module Information <!-- Delete this section if not applicable --> **Path:** `registry/coder/modules/git-clone` **New version:** `v1.0.19` ? **Breaking change:** - [ ] Yes - [x] No ## Testing & Validation - [ ] Tests pass (`bun test`) - [ ] Code formatted (`bun run fmt`) - [ ] Changes tested locally - `bun test` - I don't know if this is expected ```shell ✗ git-clone > fails without git [298.14ms] ✗ git-clone > runs with git [289.14ms] ✗ git-clone > runs with github clone with switch to feat/branch [277.19ms] ✗ git-clone > runs with gitlab clone with switch to feat/branch [293.49ms] ✗ git-clone > runs with github clone with branch_name set to feat/branch [288.07ms] ``` ## Related Issues <!-- Link related issues or write "None" if not applicable --> None --------- Co-authored-by: EdwardAngert <17991901+EdwardAngert@users.noreply.github.com> Co-authored-by: DevelopmentCats <christofer@coder.com>
1 parent 05b24da commit 0a3c9b0

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This module allows you to automatically clone a repository by URL and skip if it
1515
module "git-clone" {
1616
count = data.coder_workspace.me.start_count
1717
source = "registry.coder.com/coder/git-clone/coder"
18-
version = "1.0.18"
18+
version = "1.1.0"
1919
agent_id = coder_agent.example.id
2020
url = "https://github.com/coder/coder"
2121
}
@@ -29,7 +29,7 @@ module "git-clone" {
2929
module "git-clone" {
3030
count = data.coder_workspace.me.start_count
3131
source = "registry.coder.com/coder/git-clone/coder"
32-
version = "1.0.18"
32+
version = "1.1.0"
3333
agent_id = coder_agent.example.id
3434
url = "https://github.com/coder/coder"
3535
base_dir = "~/projects/coder"
@@ -44,7 +44,7 @@ To use with [Git Authentication](https://coder.com/docs/v2/latest/admin/git-prov
4444
module "git-clone" {
4545
count = data.coder_workspace.me.start_count
4646
source = "registry.coder.com/coder/git-clone/coder"
47-
version = "1.0.18"
47+
version = "1.1.0"
4848
agent_id = coder_agent.example.id
4949
url = "https://github.com/coder/coder"
5050
}
@@ -70,7 +70,7 @@ data "coder_parameter" "git_repo" {
7070
module "git_clone" {
7171
count = data.coder_workspace.me.start_count
7272
source = "registry.coder.com/coder/git-clone/coder"
73-
version = "1.0.18"
73+
version = "1.1.0"
7474
agent_id = coder_agent.example.id
7575
url = data.coder_parameter.git_repo.value
7676
}
@@ -104,7 +104,7 @@ Configuring `git-clone` for a self-hosted GitHub Enterprise Server running at `g
104104
module "git-clone" {
105105
count = data.coder_workspace.me.start_count
106106
source = "registry.coder.com/coder/git-clone/coder"
107-
version = "1.0.18"
107+
version = "1.1.0"
108108
agent_id = coder_agent.example.id
109109
url = "https://github.example.com/coder/coder/tree/feat/example"
110110
git_providers = {
@@ -123,7 +123,7 @@ To GitLab clone with a specific branch like `feat/example`
123123
module "git-clone" {
124124
count = data.coder_workspace.me.start_count
125125
source = "registry.coder.com/coder/git-clone/coder"
126-
version = "1.0.18"
126+
version = "1.1.0"
127127
agent_id = coder_agent.example.id
128128
url = "https://gitlab.com/coder/coder/-/tree/feat/example"
129129
}
@@ -135,7 +135,7 @@ Configuring `git-clone` for a self-hosted GitLab running at `gitlab.example.com`
135135
module "git-clone" {
136136
count = data.coder_workspace.me.start_count
137137
source = "registry.coder.com/coder/git-clone/coder"
138-
version = "1.0.18"
138+
version = "1.1.0"
139139
agent_id = coder_agent.example.id
140140
url = "https://gitlab.example.com/coder/coder/-/tree/feat/example"
141141
git_providers = {
@@ -156,7 +156,7 @@ For example, to clone the `feat/example` branch:
156156
module "git-clone" {
157157
count = data.coder_workspace.me.start_count
158158
source = "registry.coder.com/coder/git-clone/coder"
159-
version = "1.0.18"
159+
version = "1.1.0"
160160
agent_id = coder_agent.example.id
161161
url = "https://github.com/coder/coder"
162162
branch_name = "feat/example"
@@ -165,18 +165,37 @@ module "git-clone" {
165165

166166
## Git clone with different destination folder
167167

168-
By default, the repository will be cloned into a folder matching the repository name. You can use the `folder_name` attribute to change the name of the destination folder to something else.
168+
By default, the repository will be cloned into a folder matching the repository name.
169+
You can use the `folder_name` attribute to change the name of the destination folder to something else.
169170

170171
For example, this will clone into the `~/projects/coder/coder-dev` folder:
171172

172173
```tf
173174
module "git-clone" {
174175
count = data.coder_workspace.me.start_count
175176
source = "registry.coder.com/coder/git-clone/coder"
176-
version = "1.0.18"
177+
version = "1.1.0"
177178
agent_id = coder_agent.example.id
178179
url = "https://github.com/coder/coder"
179180
folder_name = "coder-dev"
180181
base_dir = "~/projects/coder"
181182
}
182183
```
184+
185+
## Git shallow clone
186+
187+
Limit the clone history to speed-up workspace startup by setting `depth`.
188+
189+
When `depth` is greater than `0` the module runs `git clone --depth <depth>`.
190+
If not defined, the default, `0`, performs a full clone.
191+
192+
```tf
193+
module "git-clone" {
194+
count = data.coder_workspace.me.start_count
195+
source = "registry.coder.com/modules/git-clone/coder"
196+
version = "1.1.0"
197+
agent_id = coder_agent.example.id
198+
url = "https://github.com/coder/coder"
199+
depth = 1
200+
}
201+
```

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)