Extract Kamal::Git as gateway for all git usage
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
19
lib/kamal/git.rb
Normal file
19
lib/kamal/git.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
13
test/git_test.rb
Normal file
13
test/git_test.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user