Extract Kamal::Git as gateway for all git usage

This commit is contained in:
dhh
2023-09-16 11:30:29 -07:00
parent 12a82a6c58
commit cfbfb37e23
8 changed files with 38 additions and 22 deletions

View File

@@ -19,7 +19,7 @@ class Kamal::Cli::Build < Kamal::Cli::Base
verify_local_dependencies verify_local_dependencies
run_hook "pre-build" 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 say "The following paths have uncommitted changes:\n #{uncommitted_changes}", :yellow
end end

View File

@@ -56,7 +56,7 @@ class Kamal::Commands::Lock < Kamal::Commands::Base
end end
def locked_by def locked_by
`git config user.name`.strip Kamal::Git.user_name
rescue Errno::ENOENT rescue Errno::ENOENT
"Unknown" "Unknown"
end end

View File

@@ -278,10 +278,8 @@ class Kamal::Configuration
def git_version def git_version
@git_version ||= @git_version ||=
if system("git rev-parse") if Kamal::Git.used?
uncommitted_suffix = Kamal::Utils.uncommitted_changes.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : "" [ Kamal::Git.revision, Kamal::Git.uncommitted_changes.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : "" ].join
"#{`git rev-parse HEAD`.strip}#{uncommitted_suffix}"
else else
raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}" raise "Can't use commit hash as version, no git repository found in #{Dir.pwd}"
end end

19
lib/kamal/git.rb Normal file
View File

@@ -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

View File

@@ -86,10 +86,6 @@ module Kamal::Utils
value.to_s.dump[1..-2].gsub(/\\"/, "\"") value.to_s.dump[1..-2].gsub(/\\"/, "\"")
end end
def uncommitted_changes
`git status --porcelain`.strip
end
def docker_env_file_line(key, value) def docker_env_file_line(key, value)
"#{key.to_s}=#{escape_docker_env_file_value(value)}\n" "#{key.to_s}=#{escape_docker_env_file_value(value)}\n"
end end

View File

@@ -84,7 +84,7 @@ class ConfigurationTest < ActiveSupport::TestCase
ENV.delete("VERSION") ENV.delete("VERSION")
@config.expects(:`).with("git rev-parse HEAD").returns("git-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 assert_equal "git-version", @config.version
end end
@@ -92,7 +92,7 @@ class ConfigurationTest < ActiveSupport::TestCase
ENV.delete("VERSION") ENV.delete("VERSION")
@config.expects(:`).with("git rev-parse HEAD").returns("git-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 assert_match /^git-version_uncommitted_[0-9a-f]{16}$/, @config.version
end end

13
test/git_test.rb Normal file
View File

@@ -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

View File

@@ -141,14 +141,4 @@ class UtilsTest < ActiveSupport::TestCase
assert_equal "\"https://example.com/\\$2\"", assert_equal "\"https://example.com/\\$2\"",
Kamal::Utils.escape_shell_value("https://example.com/$2") Kamal::Utils.escape_shell_value("https://example.com/$2")
end 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 end