From 0840fdf0dd451d2f4a667a476f46ece4d0d4df55 Mon Sep 17 00:00:00 2001 From: David Stosik Date: Fri, 4 Oct 2024 22:03:46 +0900 Subject: [PATCH 1/4] Support spaces in git repository path See https://github.com/basecamp/kamal/issues/1036 --- lib/kamal/commands/builder/clone.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/kamal/commands/builder/clone.rb b/lib/kamal/commands/builder/clone.rb index 17d9c931b..7345ba147 100644 --- a/lib/kamal/commands/builder/clone.rb +++ b/lib/kamal/commands/builder/clone.rb @@ -6,12 +6,12 @@ module Kamal::Commands::Builder::Clone end def clone - git :clone, Kamal::Git.root, "--recurse-submodules", path: clone_directory + git :clone, escaped_root, "--recurse-submodules", path: clone_directory end def clone_reset_steps [ - git(:remote, "set-url", :origin, Kamal::Git.root, path: build_directory), + git(:remote, "set-url", :origin, escaped_root, path: build_directory), git(:fetch, :origin, path: build_directory), git(:reset, "--hard", Kamal::Git.revision, path: build_directory), git(:clean, "-fdx", path: build_directory), @@ -26,4 +26,8 @@ def clone_status def clone_revision git :"rev-parse", :HEAD, path: build_directory end + + def escaped_root + Kamal::Git.root.shellescape + end end From d40057286d1e09f136e7b6c50948afbc927e84d7 Mon Sep 17 00:00:00 2001 From: David Stosik Date: Mon, 7 Oct 2024 15:42:19 +0900 Subject: [PATCH 2/4] Escape more paths and write a test --- lib/kamal/commands/builder/clone.rb | 26 ++++++++++++-------------- test/commands/builder_test.rb | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/kamal/commands/builder/clone.rb b/lib/kamal/commands/builder/clone.rb index 7345ba147..a186c7b26 100644 --- a/lib/kamal/commands/builder/clone.rb +++ b/lib/kamal/commands/builder/clone.rb @@ -1,33 +1,31 @@ module Kamal::Commands::Builder::Clone - extend ActiveSupport::Concern - - included do - delegate :clone_directory, :build_directory, to: :"config.builder" - end - def clone - git :clone, escaped_root, "--recurse-submodules", path: clone_directory + git :clone, escaped_root, "--recurse-submodules", path: config.builder.clone_directory.shellescape end def clone_reset_steps [ - git(:remote, "set-url", :origin, escaped_root, path: build_directory), - git(:fetch, :origin, path: build_directory), - git(:reset, "--hard", Kamal::Git.revision, path: build_directory), - git(:clean, "-fdx", path: build_directory), - git(:submodule, :update, "--init", path: build_directory) + git(:remote, "set-url", :origin, escaped_root, path: escaped_build_directory), + git(:fetch, :origin, path: escaped_build_directory), + git(:reset, "--hard", Kamal::Git.revision, path: escaped_build_directory), + git(:clean, "-fdx", path: escaped_build_directory), + git(:submodule, :update, "--init", path: escaped_build_directory) ] end def clone_status - git :status, "--porcelain", path: build_directory + git :status, "--porcelain", path: escaped_build_directory end def clone_revision - git :"rev-parse", :HEAD, path: build_directory + git :"rev-parse", :HEAD, path: escaped_build_directory end def escaped_root Kamal::Git.root.shellescape end + + def escaped_build_directory + config.builder.build_directory.shellescape + end end diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index e5daddfd2..52b794b8e 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -149,6 +149,21 @@ class CommandsBuilderTest < ActiveSupport::TestCase assert_equal "docker info --format '{{index .RegistryConfig.Mirrors 0}}'", command.first_mirror.join(" ") end + test "clone path with spaces" do + command = new_builder_command + Kamal::Git.stubs(:root).returns("/absolute/path with spaces") + clone_command = command.clone.join(" ") + clone_reset_commands = command.clone_reset_steps.map { |a| a.join(" ") } + + assert_match(%r{path\\ with\\ space}, clone_command) + refute_match(%r{path with spaces}, clone_command) + + clone_reset_commands.each do |command| + assert_match(%r{path\\ with\\ space}, command) + refute_match(%r{path with spaces}, command) + end + end + private def new_builder_command(additional_config = {}) Kamal::Commands::Builder.new(Kamal::Configuration.new(@config.deep_merge(additional_config), version: "123")) From f45c754e535fa93cfa59e99da265fb405b607b74 Mon Sep 17 00:00:00 2001 From: David Stosik Date: Mon, 7 Oct 2024 15:42:33 +0900 Subject: [PATCH 3/4] Remove unnecessary method --- test/commands/builder_test.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index 52b794b8e..e8d48e90a 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -169,10 +169,6 @@ def new_builder_command(additional_config = {}) Kamal::Commands::Builder.new(Kamal::Configuration.new(@config.deep_merge(additional_config), version: "123")) end - def build_directory - "#{Dir.tmpdir}/kamal-clones/app/kamal/" - end - def local_arch Kamal::Utils.docker_arch end From 8d6d7ffed0cadfc251b830d6379b99c373ae88ef Mon Sep 17 00:00:00 2001 From: David Stosik Date: Tue, 8 Oct 2024 07:10:08 +0900 Subject: [PATCH 4/4] s/refute_match/assert_no_match/ --- test/commands/builder_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index e8d48e90a..1fc224da7 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -156,11 +156,11 @@ class CommandsBuilderTest < ActiveSupport::TestCase clone_reset_commands = command.clone_reset_steps.map { |a| a.join(" ") } assert_match(%r{path\\ with\\ space}, clone_command) - refute_match(%r{path with spaces}, clone_command) + assert_no_match(%r{path with spaces}, clone_command) clone_reset_commands.each do |command| assert_match(%r{path\\ with\\ space}, command) - refute_match(%r{path with spaces}, command) + assert_no_match(%r{path with spaces}, command) end end