Skip to content

feat: add --depth to git-clone module to support shallow clones #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions registry/coder/modules/git-clone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This module allows you to automatically clone a repository by URL and skip if it
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.com/coder/coder"
}
Expand All @@ -29,7 +29,7 @@ module "git-clone" {
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.com/coder/coder"
base_dir = "~/projects/coder"
Expand All @@ -44,7 +44,7 @@ To use with [Git Authentication](https://coder.com/docs/v2/latest/admin/git-prov
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.com/coder/coder"
}
Expand All @@ -70,7 +70,7 @@ data "coder_parameter" "git_repo" {
module "git_clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = data.coder_parameter.git_repo.value
}
Expand Down Expand Up @@ -104,7 +104,7 @@ Configuring `git-clone` for a self-hosted GitHub Enterprise Server running at `g
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.example.com/coder/coder/tree/feat/example"
git_providers = {
Expand All @@ -123,7 +123,7 @@ To GitLab clone with a specific branch like `feat/example`
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://gitlab.com/coder/coder/-/tree/feat/example"
}
Expand All @@ -135,7 +135,7 @@ Configuring `git-clone` for a self-hosted GitLab running at `gitlab.example.com`
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://gitlab.example.com/coder/coder/-/tree/feat/example"
git_providers = {
Expand All @@ -156,7 +156,7 @@ For example, to clone the `feat/example` branch:
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.com/coder/coder"
branch_name = "feat/example"
Expand All @@ -165,18 +165,37 @@ module "git-clone" {

## Git clone with different destination folder

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.
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.

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

```tf
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-clone/coder"
version = "1.0.18"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.com/coder/coder"
folder_name = "coder-dev"
base_dir = "~/projects/coder"
}
```

## Git shallow clone

Limit the clone history to speed-up workspace startup by setting `depth`.

When `depth` is greater than `0` the module runs `git clone --depth <depth>`.
If not defined, the default, `0`, performs a full clone.

```tf
module "git-clone" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/modules/git-clone/coder"
version = "1.1.0"
agent_id = coder_agent.example.id
url = "https://github.com/coder/coder"
depth = 1
}
```
7 changes: 7 additions & 0 deletions registry/coder/modules/git-clone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ variable "folder_name" {
default = ""
}

variable "depth" {
description = "If > 0, perform a shallow clone using this depth."
type = number
default = 0
}

locals {
# Remove query parameters and fragments from the URL
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
Expand Down Expand Up @@ -113,6 +119,7 @@ resource "coder_script" "git_clone" {
CLONE_PATH = local.clone_path,
REPO_URL : local.clone_url,
BRANCH_NAME : local.branch_name,
DEPTH = var.depth,
})
display_name = "Git Clone"
icon = "/icon/git.svg"
Expand Down
13 changes: 11 additions & 2 deletions registry/coder/modules/git-clone/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CLONE_PATH="${CLONE_PATH}"
BRANCH_NAME="${BRANCH_NAME}"
# Expand home if it's specified!
CLONE_PATH="$${CLONE_PATH/#\~/$${HOME}}"
DEPTH="${DEPTH}"

# Check if the variable is empty...
if [ -z "$REPO_URL" ]; then
Expand Down Expand Up @@ -36,10 +37,18 @@ fi
if [ -z "$(ls -A "$CLONE_PATH")" ]; then
if [ -z "$BRANCH_NAME" ]; then
echo "Cloning $REPO_URL to $CLONE_PATH..."
git clone "$REPO_URL" "$CLONE_PATH"
if [ "$DEPTH" -gt 0 ]; then
git clone --depth "$DEPTH" "$REPO_URL" "$CLONE_PATH"
else
git clone "$REPO_URL" "$CLONE_PATH"
fi
else
echo "Cloning $REPO_URL to $CLONE_PATH on branch $BRANCH_NAME..."
git clone "$REPO_URL" -b "$BRANCH_NAME" "$CLONE_PATH"
if [ "$DEPTH" -gt 0 ]; then
git clone --depth "$DEPTH" -b "$BRANCH_NAME" "$REPO_URL" "$CLONE_PATH"
else
git clone "$REPO_URL" -b "$BRANCH_NAME" "$CLONE_PATH"
fi
fi
else
echo "$CLONE_PATH already exists and isn't empty, skipping clone!"
Expand Down
Loading