Skip to content

Commit 236022f

Browse files
authored
feat(git-clone): custom destination folder name (coder#287)
1 parent 4c45d69 commit 236022f

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

git-clone/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,20 @@ module "git-clone" {
153153
branch_name = "feat/example"
154154
}
155155
```
156+
157+
## Git clone with different destination folder
158+
159+
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.
160+
161+
For example, this will clone into the `~/projects/coder/coder-dev` folder:
162+
163+
```tf
164+
module "git-clone" {
165+
source = "registry.coder.com/modules/git-clone/coder"
166+
version = "1.0.12"
167+
agent_id = coder_agent.example.id
168+
url = "https://github.com/coder/coder"
169+
folder_name = "coder-dev"
170+
base_dir = "~/projects/coder"
171+
}
172+
```

git-clone/main.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ describe("git-clone", async () => {
7979
expect(state.outputs.branch_name.value).toEqual("");
8080
});
8181

82+
it("repo_dir should match base_dir/folder_name", async () => {
83+
const url = "git@github.com:coder/coder.git";
84+
const state = await runTerraformApply(import.meta.dir, {
85+
agent_id: "foo",
86+
base_dir: "/tmp",
87+
folder_name: "foo",
88+
url,
89+
});
90+
expect(state.outputs.repo_dir.value).toEqual("/tmp/foo");
91+
expect(state.outputs.folder_name.value).toEqual("foo");
92+
expect(state.outputs.clone_url.value).toEqual(url);
93+
const https_url = "https://github.com/coder/coder.git";
94+
expect(state.outputs.web_url.value).toEqual(https_url);
95+
expect(state.outputs.branch_name.value).toEqual("");
96+
});
97+
8298
it("branch_name should not include query string", async () => {
8399
const state = await runTerraformApply(import.meta.dir, {
84100
agent_id: "foo",

git-clone/main.tf

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ variable "branch_name" {
5050
default = ""
5151
}
5252

53+
variable "folder_name" {
54+
description = "The destination folder to clone the repository into."
55+
type = string
56+
default = ""
57+
}
58+
5359
locals {
5460
# Remove query parameters and fragments from the URL
5561
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
@@ -64,7 +70,7 @@ locals {
6470
# Extract the branch name from the URL
6571
branch_name = var.branch_name == "" && local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : var.branch_name
6672
# Extract the folder name from the URL
67-
folder_name = replace(basename(local.clone_url), ".git", "")
73+
folder_name = var.folder_name == "" ? replace(basename(local.clone_url), ".git", "") : var.folder_name
6874
# Construct the path to clone the repository
6975
clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name])
7076
# Construct the web URL

0 commit comments

Comments
 (0)