Skip to content

Commit

Permalink
Add merge action to file colision menu
Browse files Browse the repository at this point in the history
Currently, if want to reflect the changed contents, only have to grasp the
contents of diff and reflect it manually later or overwrite once to check
the difference. This is a little inconvenient.

If merge is selected, the specified merge tool will be executed. By doing
this, can reflect changes while checking.

Merge tool is obtained from environment variables and git config.
I doubt that it is appropriate to get a value from git config, but I am
thinking that there may be useful.
  • Loading branch information
y-yagi committed May 28, 2018
1 parent 84cea9a commit 91229e4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
29 changes: 27 additions & 2 deletions lib/thor/shell/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ def print_wrapped(message, options = {})
#
# ==== Parameters
# destination<String>:: the destination file to solve conflicts
# block<Proc>:: an optional block that returns the value to be used in diff
# block<Proc>:: an optional block that returns the value to be used in diff and merge
#
def file_collision(destination)
return true if @always_force
options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
options = block_given? ? "[Ynaqdhm]" : "[Ynaqh]"

loop do
answer = ask(
Expand All @@ -275,6 +275,13 @@ def file_collision(destination)
when is?(:diff)
show_diff(destination, yield) if block_given?
say "Retrying..."
when is?(:merge)
if block_given? && !merge_tool.empty?
merge(destination, yield)
return nil
end

say "Please specify merge tool to `THOR_MERGE` env."
else
say file_collision_help
end
Expand Down Expand Up @@ -352,6 +359,7 @@ def file_collision_help #:nodoc:
q - quit, abort
d - diff, show the differences between the old and the new
h - help, show this help
m - merge, run merge tool
HELP
end

Expand Down Expand Up @@ -440,6 +448,23 @@ def ask_filtered(statement, color, options)
end
correct_answer
end

def merge(destination, content) #:nodoc:
require "tempfile"
Tempfile.open([File.basename(destination), File.extname(destination)], File.dirname(destination)) do |temp|
temp.write content
temp.rewind
system %(#{merge_tool} "#{temp.path}" "#{destination}")
end
end

def merge_tool #:nodoc:
@merge_tool ||= ENV["THOR_MERGE"] || git_merge_tool
end

def git_merge_tool #:nodoc:
`git config merge.tool`.rstrip rescue ""
end
end
end
end
10 changes: 9 additions & 1 deletion spec/actions/create_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def silence!
it "shows conflict status to the user" do
file = File.join(destination_root, "doc/config.rb")
expect(create_file("doc/config.rb")).not_to be_identical
expect(Thor::LineEditor).to receive(:readline).with("Overwrite #{file}? (enter \"h\" for help) [Ynaqdh] ", anything).and_return("s")
expect(Thor::LineEditor).to receive(:readline).with("Overwrite #{file}? (enter \"h\" for help) [Ynaqdhm] ", anything).and_return("s")

content = invoke!
expect(content).to match(%r{conflict doc/config\.rb})
Expand All @@ -129,6 +129,14 @@ def silence!
expect(@base.shell).to receive(:system).with(/diff -u/)
invoke!
end

it "executes the block given to run merge tool" do
create_file("doc/config.rb")
allow(@base.shell).to receive(:merge_tool).and_return("meld")
expect(Thor::LineEditor).to receive(:readline).and_return("m")
expect(@base.shell).to receive(:system).with(/meld/)
invoke!
end
end
end

Expand Down
26 changes: 24 additions & 2 deletions spec/shell/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ def #456 Lanç...
end

describe "when a block is given" do
it "displays diff options to the user" do
expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqdh] ', :add_to_history => false).and_return("s")
it "displays diff and merge options to the user" do
expect(Thor::LineEditor).to receive(:readline).with('Overwrite foo? (enter "h" for help) [Ynaqdhm] ', :add_to_history => false).and_return("s")
shell.file_collision("foo") {}
end

Expand All @@ -367,6 +367,28 @@ def #456 Lanç...
expect(shell).to receive(:system).with(/diff -u/)
capture(:stdout) { shell.file_collision("foo") {} }
end

it "invokes the merge tool" do
allow(shell).to receive(:merge_tool).and_return("meld")
expect(Thor::LineEditor).to receive(:readline).and_return("m")
expect(shell).to receive(:system).with(/meld/)
capture(:stdout) { shell.file_collision("foo") {} }
end

it "invokes the merge tool that specified at ENV['THOR_MERGE']" do
allow(ENV).to receive(:[]).with("THOR_MERGE").and_return("meld")
expect(Thor::LineEditor).to receive(:readline).and_return("m")
expect(shell).to receive(:system).with(/meld/)
capture(:stdout) { shell.file_collision("foo") {} }
end

it "show warning if user chooses merge but merge tool is not specified" do
allow(shell).to receive(:merge_tool).and_return("")
expect(Thor::LineEditor).to receive(:readline).and_return("m")
expect(Thor::LineEditor).to receive(:readline).and_return("n")
help = capture(:stdout) { shell.file_collision("foo") {} }
expect(help).to match(/Please specify merge tool to `THOR_MERGE` env/)
end
end
end
end

0 comments on commit 91229e4

Please sign in to comment.