Skip to content

Commit 92a192d

Browse files
committed
Implement Thor-like conflict handler
1 parent e21468f commit 92a192d

File tree

1 file changed

+85
-5
lines changed

1 file changed

+85
-5
lines changed

lib/boxing/generator.rb

+85-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ module Boxing
77
class Generator
88
ASCII_CLEAR = "\e[0m"
99
ASCII_BOLD = "\e[1m"
10+
ASCII_RED = "\e[31m"
1011

1112
CREATED_MESSAGE = "#{ASCII_BOLD}create#{ASCII_CLEAR}\t%s"
1213
IDENTICAL_MESSAGE = "#{ASCII_BOLD}identical#{ASCII_CLEAR}\t%s"
13-
CONFLICT_MESSAGE = "#{ASCII_BOLD}conflict#{ASCII_CLEAR}\t%s"
14+
CONFLICT_MESSAGE = "#{ASCII_BOLD}#{ASCII_RED}conflict#{ASCII_CLEAR}\t%s"
15+
OVERWRITE_MESSAGE = 'Overwrite %s? (enter "h" for help) [Ynqdhm] '
16+
FORCE_MESSAGE = "#{ASCII_BOLD}force#{ASCII_CLEAR}\t%s"
17+
SKIP_MESSAGE = "#{ASCII_BOLD}skip#{ASCII_CLEAR}\t%s"
18+
MERGE_TOOL_NOT_FOUND = 'Please configure merge.tool in your Git config.'
1419

1520
attr_reader :destination, :path
1621

@@ -84,12 +89,87 @@ def with_conflict(&block)
8489
#
8590
# @since 0.11.0
8691
def on_conflict
87-
if identical?
88-
puts format(IDENTICAL_MESSAGE, destination)
89-
return
90-
end
92+
return puts format(IDENTICAL_MESSAGE, destination) if identical?
9193

9294
puts format(CONFLICT_MESSAGE, destination)
95+
return puts format(SKIP_MESSAGE, destination) unless do_overwrite
96+
97+
puts format(FORCE_MESSAGE, destination)
98+
yield if block_given?
99+
end
100+
101+
# Do overwrite
102+
#
103+
# @return [TrueClass|FalseClass]
104+
# @raise [SystemExit] if user quits
105+
#
106+
# @api private
107+
# @since 0.11.0
108+
def do_overwrite # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
109+
loop do
110+
print format(OVERWRITE_MESSAGE, destination)
111+
112+
case $stdin.gets.chomp
113+
when 'Y', 'y', '' then return true
114+
when 'n', 'N' then return false
115+
when 'd', 'D' then show_diff
116+
when 'h', 'H' then show_help
117+
when 'm', 'M'
118+
puts MERGE_TOOL_NOT_FOUND unless do_merge
119+
return false
120+
when 'q', 'Q' then raise SystemExit
121+
end
122+
end
123+
end
124+
125+
# Show diff
126+
#
127+
# @since 0.11.0
128+
# @api private
129+
def show_diff
130+
Tempfile.open(destination) do |tempfile|
131+
tempfile.write(render)
132+
tempfile.rewind
133+
134+
system("diff -u #{tempfile.path} #{path}")
135+
end
136+
end
137+
138+
# Do merge
139+
#
140+
# @return [TrueClass|FalseClass]
141+
#
142+
# @since 0.11.0
143+
# @api private
144+
def do_merge # rubocop:disable Metrics/MethodLength
145+
tool = begin
146+
`git config merge.tool`.rstrip
147+
rescue StandardError
148+
''
149+
end
150+
return false if tool.empty?
151+
152+
Tempfile.open(destination) do |tempfile|
153+
tempfile.write(render)
154+
tempfile.rewind
155+
156+
system("#{tool} #{tempfile.path} #{path}")
157+
end
158+
end
159+
160+
# Show help
161+
#
162+
# @since 0.11.0
163+
# @api private
164+
def show_help
165+
puts <<-HELP
166+
Y - yes, overwrite
167+
n - no, do not overwrite
168+
d - diff, show the differences between the old and the new
169+
h - help, show this help
170+
m - merge, run merge tool
171+
q - quit, exit this program
172+
HELP
93173
end
94174
end
95175
end

0 commit comments

Comments
 (0)