From 0a3c9b01b888d8814ffc4576bf0bc994f506b413 Mon Sep 17 00:00:00 2001 From: Edward Angert Date: Wed, 9 Jul 2025 23:27:58 -0400 Subject: [PATCH] feat: add `--depth` to git-clone module to support shallow clones (#197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description 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 **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 None --------- Co-authored-by: EdwardAngert <17991901+EdwardAngert@users.noreply.github.com> Co-authored-by: DevelopmentCats --- registry/coder/modules/git-clone/README.md | 39 ++++++++++++++++------ registry/coder/modules/git-clone/main.tf | 7 ++++ registry/coder/modules/git-clone/run.sh | 13 ++++++-- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/registry/coder/modules/git-clone/README.md b/registry/coder/modules/git-clone/README.md index d2d3ddaf..fc014586 100644 --- a/registry/coder/modules/git-clone/README.md +++ b/registry/coder/modules/git-clone/README.md @@ -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" } @@ -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" @@ -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" } @@ -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 } @@ -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 = { @@ -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" } @@ -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 = { @@ -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" @@ -165,7 +165,8 @@ 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: @@ -173,10 +174,28 @@ For example, this will clone into the `~/projects/coder/coder-dev` folder: 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 `. +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 +} +``` diff --git a/registry/coder/modules/git-clone/main.tf b/registry/coder/modules/git-clone/main.tf index 0295444d..d03a59d7 100644 --- a/registry/coder/modules/git-clone/main.tf +++ b/registry/coder/modules/git-clone/main.tf @@ -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, "/\\?.*/", ""), "/#.*/", "") @@ -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" diff --git a/registry/coder/modules/git-clone/run.sh b/registry/coder/modules/git-clone/run.sh index bd807177..282c667a 100644 --- a/registry/coder/modules/git-clone/run.sh +++ b/registry/coder/modules/git-clone/run.sh @@ -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 @@ -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!"