From 0cfafd1d257ef1cf303bb7101c58355725d2f8ff Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Fri, 21 Jul 2023 18:37:45 +0400 Subject: [PATCH 1/4] Log uncommitted changes during deploy --- lib/mrsk/cli/build.rb | 8 +++++++- lib/mrsk/configuration.rb | 2 +- lib/mrsk/utils.rb | 4 ++++ test/configuration_test.rb | 5 +++-- test/utils_test.rb | 10 ++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index e52b3358..a1261fe3 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -19,7 +19,13 @@ def push run_locally do begin - MRSK.with_verbosity(:debug) { execute *MRSK.builder.push } + MRSK.with_verbosity(:debug) do + if (uncommitted_changes = Mrsk::Utils.uncommitted_changes).present? + say "The following paths have uncommitted changes (check your .gitignore file):\n #{uncommitted_changes}", :yellow + end + + execute *MRSK.builder.push + end rescue SSHKit::Command::Failed => e if e.message =~ /(no builder)|(no such file or directory)/ error "Missing compatible builder, so creating a new one first" diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index e570de55..1473728b 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -262,7 +262,7 @@ def role_names def git_version @git_version ||= if system("git rev-parse") - uncommitted_suffix = `git status --porcelain`.strip.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : "" + uncommitted_suffix = Mrsk::Utils.uncommitted_changes.present? ? "_uncommitted_#{SecureRandom.hex(8)}" : "" "#{`git rev-parse HEAD`.strip}#{uncommitted_suffix}" else diff --git a/lib/mrsk/utils.rb b/lib/mrsk/utils.rb index e463f547..939c92dd 100644 --- a/lib/mrsk/utils.rb +++ b/lib/mrsk/utils.rb @@ -93,4 +93,8 @@ def abbreviate_version(version) end end end + + def uncommitted_changes + `git status --porcelain`.strip + end end diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 3d362293..695f8e72 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") - @config.expects(:`).with("git status --porcelain").returns("") + Mrsk::Utils.expects(:uncommitted_changes).returns("") assert_equal "git-version", @config.version end @@ -92,7 +92,8 @@ class ConfigurationTest < ActiveSupport::TestCase ENV.delete("VERSION") @config.expects(:`).with("git rev-parse HEAD").returns("git-version") - @config.expects(:`).with("git status --porcelain").returns("M file\n") + # @config.expects(:`).with("git status --porcelain").returns("M file\n") + Mrsk::Utils.expects(:uncommitted_changes).returns("M file\n") assert_match /^git-version_uncommitted_[0-9a-f]{16}$/, @config.version end diff --git a/test/utils_test.rb b/test/utils_test.rb index 24a1b96b..0c42d72b 100644 --- a/test/utils_test.rb +++ b/test/utils_test.rb @@ -61,4 +61,14 @@ class UtilsTest < ActiveSupport::TestCase assert_equal "\"https://example.com/\\$2\"", Mrsk::Utils.escape_shell_value("https://example.com/$2") end + + test "uncommitted changes exist" do + Mrsk::Utils.expects(:`).with("git status --porcelain").returns("M file\n") + assert_equal "M file", Mrsk::Utils.uncommitted_changes + end + + test "uncommitted changes do not exist" do + Mrsk::Utils.expects(:`).with("git status --porcelain").returns("") + assert_equal "", Mrsk::Utils.uncommitted_changes + end end From 9c2a1dc7cd472b3b048afd6de3e448c4c686301a Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Fri, 21 Jul 2023 18:44:01 +0400 Subject: [PATCH 2/4] Removed commented code in tests --- test/configuration_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 695f8e72..81b3523f 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -92,7 +92,6 @@ class ConfigurationTest < ActiveSupport::TestCase ENV.delete("VERSION") @config.expects(:`).with("git rev-parse HEAD").returns("git-version") - # @config.expects(:`).with("git status --porcelain").returns("M file\n") Mrsk::Utils.expects(:uncommitted_changes).returns("M file\n") assert_match /^git-version_uncommitted_[0-9a-f]{16}$/, @config.version end From ea941f33f9c9f1d8680e20990ae2fe6204fa9404 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Fri, 21 Jul 2023 22:45:23 +0400 Subject: [PATCH 3/4] Moved uncommitted changes message out of run_locally block --- lib/mrsk/cli/build.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index a1261fe3..a46e03f4 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -17,13 +17,13 @@ def push verify_local_dependencies run_hook "pre-build" + if (uncommitted_changes = Mrsk::Utils.uncommitted_changes).present? + say "The following paths have uncommitted changes (check your .gitignore file):\n #{uncommitted_changes}", :yellow + end + run_locally do begin MRSK.with_verbosity(:debug) do - if (uncommitted_changes = Mrsk::Utils.uncommitted_changes).present? - say "The following paths have uncommitted changes (check your .gitignore file):\n #{uncommitted_changes}", :yellow - end - execute *MRSK.builder.push end rescue SSHKit::Command::Failed => e From d414253393a27c0000e1f8f654419819fd6d0ce4 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Mon, 24 Jul 2023 20:12:22 +0400 Subject: [PATCH 4/4] Updated uncommitted notification text --- lib/mrsk/cli/build.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index a46e03f4..e9aeec66 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -18,7 +18,7 @@ def push run_hook "pre-build" if (uncommitted_changes = Mrsk::Utils.uncommitted_changes).present? - say "The following paths have uncommitted changes (check your .gitignore file):\n #{uncommitted_changes}", :yellow + say "The following paths have uncommitted changes:\n #{uncommitted_changes}", :yellow end run_locally do