From cfbfb37e235663de57e632e38fc4c39f76409258 Mon Sep 17 00:00:00 2001 From: dhh Date: Sat, 16 Sep 2023 11:30:29 -0700 Subject: [PATCH] Extract Kamal::Git as gateway for all git usage --- lib/kamal/cli/build.rb | 2 +- lib/kamal/commands/lock.rb | 2 +- lib/kamal/configuration.rb | 6 ++---- lib/kamal/git.rb | 19 +++++++++++++++++++ lib/kamal/utils.rb | 4 ---- test/configuration_test.rb | 4 ++-- test/git_test.rb | 13 +++++++++++++ test/utils_test.rb | 10 ---------- 8 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 lib/kamal/git.rb create mode 100644 test/git_test.rb diff --git a/lib/kamal/cli/build.rb b/lib/kamal/cli/build.rb index 34531058..8eae4dd7 100644 --- a/lib/kamal/cli/build.rb +++ b/lib/kamal/cli/build.rb @@ -19,7 +19,7 @@ class Kamal::Cli::Build < Kamal::Cli::Base verify_local_dependencies run_hook "pre-build" - if (uncommitted_changes = Kamal::Utils.uncommitted_changes).present? + if (uncommitted_changes = Kamal::Git.uncommitted_changes).present? say "The following paths have uncommitted changes:\n #{uncommitted_changes}", :yellow end diff --git a/lib/kamal/commands/lock.rb b/lib/kamal/commands/lock.rb index c5d216ac..82340714 100644 --- a/lib/kamal/commands/lock.rb +++ b/lib/kamal/commands/lock.rb @@ -56,7 +56,7 @@ class Kamal::Commands::Lock < Kamal::Commands::Base end def locked_by - `git config user.name`.strip + Kamal::Git.user_name rescue Errno::ENOENT "Unknown" end diff --git a/lib/kamal/configuration.rb b/lib/kamal/configuration.rb index aaa35155..89530baf 100644 --- a/lib/kamal/configuration.rb +++ b/lib/kamal/configuration.rb @@ -278,10 +278,8 @@ class Kamal::Configuration def git_version @git_version ||= - if system("git rev-parse") - uncommitted_suffix = Kamal::Utils.uncommitted_changes.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : "" - - "#{`git rev-parse HEAD`.strip}#{uncommitted_suffix}" + if Kamal::Git.used? + [ Kamal::Git.revision, Kamal::Git.uncommitted_changes.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : "" ].join else raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}" end diff --git a/lib/kamal/git.rb b/lib/kamal/git.rb new file mode 100644 index 00000000..8d59827a --- /dev/null +++ b/lib/kamal/git.rb @@ -0,0 +1,19 @@ +module Kamal::Git + extend self + + def used? + system("git rev-parse") + end + + def user_name + `git config user.name`.strip + end + + def revision + `git rev-parse HEAD`.strip + end + + def uncommitted_changes + `git status --porcelain`.strip + end +end diff --git a/lib/kamal/utils.rb b/lib/kamal/utils.rb index 540f576b..2eb215ac 100644 --- a/lib/kamal/utils.rb +++ b/lib/kamal/utils.rb @@ -86,10 +86,6 @@ module Kamal::Utils value.to_s.dump[1..-2].gsub(/\\"/, "\"") end - def uncommitted_changes - `git status --porcelain`.strip - end - def docker_env_file_line(key, value) "#{key.to_s}=#{escape_docker_env_file_value(value)}\n" end diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 5e6eab7d..3802f033 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -84,7 +84,7 @@ class ConfigurationTest < ActiveSupport::TestCase ENV.delete("VERSION") @config.expects(:`).with("git rev-parse HEAD").returns("git-version") - Kamal::Utils.expects(:uncommitted_changes).returns("") + Kamal::Git.expects(:uncommitted_changes).returns("") assert_equal "git-version", @config.version end @@ -92,7 +92,7 @@ class ConfigurationTest < ActiveSupport::TestCase ENV.delete("VERSION") @config.expects(:`).with("git rev-parse HEAD").returns("git-version") - Kamal::Utils.expects(:uncommitted_changes).returns("M file\n") + Kamal::Git.expects(:uncommitted_changes).returns("M file\n") assert_match /^git-version_uncommitted_[0-9a-f]{16}$/, @config.version end diff --git a/test/git_test.rb b/test/git_test.rb new file mode 100644 index 00000000..6f6be320 --- /dev/null +++ b/test/git_test.rb @@ -0,0 +1,13 @@ +require "test_helper" + +class GitTest < ActiveSupport::TestCase + test "uncommitted changes exist" do + Kamal::Git.expects(:`).with("git status --porcelain").returns("M file\n") + assert_equal "M file", Kamal::Git.uncommitted_changes + end + + test "uncommitted changes do not exist" do + Kamal::Git.expects(:`).with("git status --porcelain").returns("") + assert_equal "", Kamal::Git.uncommitted_changes + end +end diff --git a/test/utils_test.rb b/test/utils_test.rb index b268fb0f..f99a5d2e 100644 --- a/test/utils_test.rb +++ b/test/utils_test.rb @@ -141,14 +141,4 @@ class UtilsTest < ActiveSupport::TestCase assert_equal "\"https://example.com/\\$2\"", Kamal::Utils.escape_shell_value("https://example.com/$2") end - - test "uncommitted changes exist" do - Kamal::Utils.expects(:`).with("git status --porcelain").returns("M file\n") - assert_equal "M file", Kamal::Utils.uncommitted_changes - end - - test "uncommitted changes do not exist" do - Kamal::Utils.expects(:`).with("git status --porcelain").returns("") - assert_equal "", Kamal::Utils.uncommitted_changes - end end