From a6fc39e7981259edb9189e7f59564c65aaba3a11 Mon Sep 17 00:00:00 2001
From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com>
Date: Thu, 21 Aug 2025 10:16:57 +0200
Subject: [PATCH] feat(git-settings): add support for shallow cloning in
application settings
- Introduced a new boolean setting `is_git_shallow_clone_enabled` to the application settings model.
- Updated the `Advanced` component to include a checkbox for enabling shallow cloning.
- Modified the `setGitImportSettings` and `generateGitImportCommands` methods to handle shallow clone logic.
- Created a migration to add the new setting to the database schema.
- Enhanced the deployment process to utilize shallow cloning for improved performance.
---
app/Livewire/Project/Application/Advanced.php | 5 ++++
app/Models/Application.php | 26 +++++++++++++----
app/Models/ApplicationSetting.php | 1 +
...ow_clone_to_application_settings_table.php | 28 +++++++++++++++++++
.../project/application/advanced.blade.php | 2 ++
5 files changed, 57 insertions(+), 5 deletions(-)
create mode 100644 database/migrations/2025_08_21_080234_add_git_shallow_clone_to_application_settings_table.php
diff --git a/app/Livewire/Project/Application/Advanced.php b/app/Livewire/Project/Application/Advanced.php
index bd1388806..6020af715 100644
--- a/app/Livewire/Project/Application/Advanced.php
+++ b/app/Livewire/Project/Application/Advanced.php
@@ -19,6 +19,9 @@ class Advanced extends Component
#[Validate(['boolean'])]
public bool $isGitLfsEnabled = false;
+ #[Validate(['boolean'])]
+ public bool $isGitShallowCloneEnabled = false;
+
#[Validate(['boolean'])]
public bool $isPreviewDeploymentsEnabled = false;
@@ -83,6 +86,7 @@ class Advanced extends Component
$this->application->settings->is_force_https_enabled = $this->isForceHttpsEnabled;
$this->application->settings->is_git_submodules_enabled = $this->isGitSubmodulesEnabled;
$this->application->settings->is_git_lfs_enabled = $this->isGitLfsEnabled;
+ $this->application->settings->is_git_shallow_clone_enabled = $this->isGitShallowCloneEnabled;
$this->application->settings->is_preview_deployments_enabled = $this->isPreviewDeploymentsEnabled;
$this->application->settings->is_auto_deploy_enabled = $this->isAutoDeployEnabled;
$this->application->settings->is_log_drain_enabled = $this->isLogDrainEnabled;
@@ -108,6 +112,7 @@ class Advanced extends Component
$this->isGitSubmodulesEnabled = $this->application->settings->is_git_submodules_enabled;
$this->isGitLfsEnabled = $this->application->settings->is_git_lfs_enabled;
+ $this->isGitShallowCloneEnabled = $this->application->settings->is_git_shallow_clone_enabled ?? false;
$this->isPreviewDeploymentsEnabled = $this->application->settings->is_preview_deployments_enabled;
$this->isAutoDeployEnabled = $this->application->settings->is_auto_deploy_enabled;
$this->isGpuEnabled = $this->application->settings->is_gpu_enabled;
diff --git a/app/Models/Application.php b/app/Models/Application.php
index 1971488c7..c2fec427e 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -980,15 +980,26 @@ class Application extends BaseModel
public function setGitImportSettings(string $deployment_uuid, string $git_clone_command, bool $public = false)
{
$baseDir = $this->generateBaseDir($deployment_uuid);
+ $isShallowCloneEnabled = $this->settings?->is_git_shallow_clone_enabled ?? false;
if ($this->git_commit_sha !== 'HEAD') {
- $git_clone_command = "{$git_clone_command} && cd {$baseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git -c advice.detachedHead=false checkout {$this->git_commit_sha} >/dev/null 2>&1";
+ // If shallow clone is enabled and we need a specific commit,
+ // we need to fetch that specific commit with depth=1
+ if ($isShallowCloneEnabled) {
+ $git_clone_command = "{$git_clone_command} && cd {$baseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git fetch --depth=1 origin {$this->git_commit_sha} && git -c advice.detachedHead=false checkout {$this->git_commit_sha} >/dev/null 2>&1";
+ } else {
+ $git_clone_command = "{$git_clone_command} && cd {$baseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git -c advice.detachedHead=false checkout {$this->git_commit_sha} >/dev/null 2>&1";
+ }
}
if ($this->settings->is_git_submodules_enabled) {
+ // Check if .gitmodules file exists before running submodule commands
+ $git_clone_command = "{$git_clone_command} && cd {$baseDir} && if [ -f .gitmodules ]; then";
if ($public) {
- $git_clone_command = "{$git_clone_command} && cd {$baseDir} && sed -i \"s#git@\(.*\):#https://\\1/#g\" {$baseDir}/.gitmodules || true";
+ $git_clone_command = "{$git_clone_command} sed -i \"s#git@\(.*\):#https://\\1/#g\" {$baseDir}/.gitmodules || true &&";
}
- $git_clone_command = "{$git_clone_command} && cd {$baseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git submodule update --init --recursive";
+ // Add shallow submodules flag if shallow clone is enabled
+ $submoduleFlags = $isShallowCloneEnabled ? '--shallow-submodules' : '';
+ $git_clone_command = "{$git_clone_command} GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git submodule update --init --recursive {$submoduleFlags}; fi";
}
if ($this->settings->is_git_lfs_enabled) {
$git_clone_command = "{$git_clone_command} && cd {$baseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git lfs pull";
@@ -1118,9 +1129,14 @@ class Application extends BaseModel
['repository' => $customRepository, 'port' => $customPort] = $this->customRepository();
$baseDir = $custom_base_dir ?? $this->generateBaseDir($deployment_uuid);
$commands = collect([]);
- $git_clone_command = "git clone -b \"{$this->git_branch}\"";
+
+ // Check if shallow clone is enabled
+ $isShallowCloneEnabled = $this->settings?->is_git_shallow_clone_enabled ?? false;
+ $depthFlag = $isShallowCloneEnabled ? ' --depth=1' : '';
+
+ $git_clone_command = "git clone{$depthFlag} -b \"{$this->git_branch}\"";
if ($only_checkout) {
- $git_clone_command = "git clone --no-checkout -b \"{$this->git_branch}\"";
+ $git_clone_command = "git clone{$depthFlag} --no-checkout -b \"{$this->git_branch}\"";
}
if ($pull_request_id !== 0) {
$pr_branch_name = "pr-{$pull_request_id}-coolify";
diff --git a/app/Models/ApplicationSetting.php b/app/Models/ApplicationSetting.php
index c7624fdaa..d05081d21 100644
--- a/app/Models/ApplicationSetting.php
+++ b/app/Models/ApplicationSetting.php
@@ -15,6 +15,7 @@ class ApplicationSetting extends Model
'is_preview_deployments_enabled' => 'boolean',
'is_git_submodules_enabled' => 'boolean',
'is_git_lfs_enabled' => 'boolean',
+ 'is_git_shallow_clone_enabled' => 'boolean',
];
protected $guarded = [];
diff --git a/database/migrations/2025_08_21_080234_add_git_shallow_clone_to_application_settings_table.php b/database/migrations/2025_08_21_080234_add_git_shallow_clone_to_application_settings_table.php
new file mode 100644
index 000000000..399c49c7f
--- /dev/null
+++ b/database/migrations/2025_08_21_080234_add_git_shallow_clone_to_application_settings_table.php
@@ -0,0 +1,28 @@
+boolean('is_git_shallow_clone_enabled')->default(true)->after('is_git_lfs_enabled');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('application_settings', function (Blueprint $table) {
+ $table->dropColumn('is_git_shallow_clone_enabled');
+ });
+ }
+};
diff --git a/resources/views/livewire/project/application/advanced.blade.php b/resources/views/livewire/project/application/advanced.blade.php
index 82b154bcb..ad5d115a2 100644
--- a/resources/views/livewire/project/application/advanced.blade.php
+++ b/resources/views/livewire/project/application/advanced.blade.php
@@ -67,6 +67,8 @@
helper="Allow Git Submodules during build process." />
+
@endif