generated from mattbrictson/gem
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a git commit for each selected gem update when
--commit
is s…
…pecified (#53) Sometimes, updating gems can lead to bugs or regressions. To facilitate troubleshooting, this PR introduces the ability to commit each selected gem update in its own git commit, complete with a descriptive commit message. You can then make use of tools like `git bisect` to more easily find the update that introduced the problem. To enable this behavior, pass the `--commit` option: ``` bundle update-interactive --commit ``` The gems you select to be updated will be applied in separate commits, like this: ``` * c9801382 Update activeadmin 3.2.2 → 3.2.3 * 9957254b Update rexml 3.3.5 → 3.3.6 * 4a4f2072 Update sass 1.77.6 → 1.77.8 ``` Supersedes #50 and #51. Closes #49.
- Loading branch information
1 parent
5fd57b6
commit 035a979
Showing
9 changed files
with
197 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require "shellwords" | ||
|
||
module BundleUpdateInteractive | ||
class GitCommitter | ||
def initialize(updater) | ||
@updater = updater | ||
end | ||
|
||
def apply_updates_as_individual_commits(*gem_names) | ||
assert_git_executable! | ||
assert_working_directory_clean! | ||
|
||
gem_names.flatten.each do |name| | ||
updates = updater.apply_updates(name) | ||
updated_gem = updates[name] || updates.values.first | ||
next if updated_gem.nil? | ||
|
||
commit_message = format_commit_message(updated_gem) | ||
system "git add Gemfile Gemfile.lock", exception: true | ||
system "git commit -m #{commit_message.shellescape}", exception: true | ||
end | ||
end | ||
|
||
def format_commit_message(outdated_gem) | ||
[ | ||
"Update", | ||
outdated_gem.name, | ||
outdated_gem.current_version.to_s, | ||
outdated_gem.current_git_version, | ||
"→", | ||
outdated_gem.updated_version.to_s, | ||
outdated_gem.updated_git_version | ||
].compact.join(" ") | ||
end | ||
|
||
private | ||
|
||
attr_reader :updater | ||
|
||
def assert_git_executable! | ||
success = begin | ||
`git --version` | ||
Process.last_status.success? | ||
rescue SystemCallError | ||
false | ||
end | ||
raise Error, "git could not be executed" unless success | ||
end | ||
|
||
def assert_working_directory_clean! | ||
status = `git status --untracked-files=no --porcelain`.strip | ||
return if status.empty? | ||
|
||
raise Error, "`git status` reports uncommitted changes; please commit or stash them them first!\n#{status}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module BundleUpdateInteractive | ||
class GitCommitterTest < Minitest::Test | ||
def setup | ||
@git_committer = GitCommitter.new(nil) | ||
end | ||
|
||
def test_format_commit_message | ||
gem = build(:outdated_gem, name: "activeadmin", current_version: "3.2.2", updated_version: "3.2.3") | ||
|
||
assert_equal "Update activeadmin 3.2.2 → 3.2.3", @git_committer.format_commit_message(gem) | ||
end | ||
|
||
def test_format_commit_message_with_git_version | ||
gem = build( | ||
:outdated_gem, | ||
name: "rails", | ||
current_version: "7.2.1", | ||
current_git_version: "5a8d894", | ||
updated_version: "7.2.1", | ||
updated_git_version: "77dfa65" | ||
) | ||
|
||
assert_equal "Update rails 7.2.1 5a8d894 → 7.2.1 77dfa65", @git_committer.format_commit_message(gem) | ||
end | ||
|
||
def test_apply_updates_as_individual_commits_raises_if_git_raises | ||
@git_committer.stubs(:`).with("git --version").raises(Errno::ENOENT) | ||
|
||
error = assert_raises(Error) { @git_committer.apply_updates_as_individual_commits } | ||
assert_equal "git could not be executed", error.message | ||
end | ||
|
||
def test_apply_updates_as_individual_commits_raises_if_git_does_not_succeed | ||
@git_committer.stubs(:`).with("git --version").returns("") | ||
Process.stubs(:last_status).returns(stub(success?: false)) | ||
|
||
error = assert_raises(Error) { @git_committer.apply_updates_as_individual_commits } | ||
assert_equal "git could not be executed", error.message | ||
end | ||
|
||
def test_apply_updates_as_individual_commits_raises_if_there_are_uncommitted_files | ||
@git_committer.stubs(:`).with("git --version").returns("") | ||
@git_committer.stubs(:`).with("git status --untracked-files=no --porcelain").returns("M Gemfile.lock") | ||
Process.stubs(:last_status).returns(stub(success?: true)) | ||
|
||
error = assert_raises(Error) { @git_committer.apply_updates_as_individual_commits } | ||
assert_equal <<~MESSAGE.strip, error.message | ||
`git status` reports uncommitted changes; please commit or stash them them first! | ||
M Gemfile.lock | ||
MESSAGE | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters