73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
# File 'lib/routes/git_merge.rb', line 73
def merge
@git.check_for_uncommitted_files(false, 'Aborting the merge!')
unless @source_branches.any?
code_sb = @git.current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
db_sb = @git.current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
if code_sb != db_sb
App::Terminal::error('Cannot reliably determine source branch', ["You're repos are on two different branches:", nil, "Code \xe2\x86\x92 #{App::Terminal::format_branch(code_sb)}", " DB \xe2\x86\x92 #{App::Terminal::format_branch(db_sb)}", nil, 'In this particular scenario, you must explicitly specify a source branch in order to merge.'], true)
end
@source_branches << code_sb
end
@source_branches.each do |branch|
if branch == App::Git::MASTER
App::Terminal::error("#{App::Terminal::format_branch(App::Git::MASTER)} has been recognized as a #{App::Terminal::format_highlight('source branch')}", ["If your intention is to merge #{App::Terminal::format_branch(App::Git::MASTER)} into #{App::Terminal::format_branch(@target_branch)}, use #{App::Terminal::format_command('bp g u')} instead."], true)
elsif branch =~ App::Git::RELEASE_BRANCH_REGEX
App::Terminal::error("#{App::Terminal::format_branch(branch)} has been recognized as a #{App::Terminal::format_highlight('source branch')}", ["You #{App::Terminal::format_invalid('cannot use this script', true)} to merge from a release branch.", "It's usually best to do this manually."], true)
end
end
if @target_branch =~ App::Git::RELEASE_BRANCH_REGEX
App::Terminal::error("#{App::Terminal::format_branch(@target_branch)} has been recognized as a #{App::Terminal::format_highlight('target branch')}", ["You #{App::Terminal::format_invalid('cannot use this script', true)} to merge to a release branch.", "It's usually best to do this manually."], true)
end
unless App::Terminal::prompt_yes_no("You're about to #{App::Terminal::format_action('initiate a merge')} between the following branch(es):", generate_source_target_text, "Would you like to #{App::Terminal::format_action('CONTINUE')}\x1B[38;5;89m")
App::Terminal::abort
end
atleast_one_branch_found = false
atleast_one_error = false
current_branch_cd = @git.current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
current_branch_db = @git.current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
cd_repo = App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)
db_repo = App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)
non_existent_branches_cd = []
non_existent_branches_db = []
branches_with_stashes_cd = []
branches_with_stashes_db = []
changed_files_code = {}
changed_files_db = {}
ran_db_update = false
target_branch_data = @git.branch_data(@target_branch, nil, false)
if target_branch_data[0][:"#{App::Git::BRANCH_EXISTS}"] == false && target_branch_data[1][:"#{App::Git::BRANCH_EXISTS}"] == false
App::Terminal::error("Target branch #{App::Terminal::format_branch(@target_branch)} doesn't exist", 'Please check your spelling and try again.', true)
end
if target_branch_data[0][:"#{App::Git::BRANCH_EXISTS}"] == false
App::Terminal::warning("Target #{App::Terminal::format_highlight('CODE')} branch doesn't exist")
App::Terminal::output("Creating branch #{App::Terminal::format_branch(@target_branch)} in #{App::Terminal::format_directory(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))}")
App::Terminal::error('Not yet implemented', ["The code which should create branch #{App::Terminal::format_branch(@target_branch)} on #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config::WORKSTATION_PATH_TO_BP_CODE))} hasn't been programmed yet.", "Please speak to #{App::Terminal::format_highlight('Albert')}."], true)
elsif target_branch_data[1][:"#{App::Git::BRANCH_EXISTS}"] == false
App::Terminal::warning("Target #{App::Terminal::format_highlight('DB')} branch doesn't exist")
App::Terminal::output("Creating branch #{App::Terminal::format_branch(@target_branch)} in #{App::Terminal::format_directory(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))}")
App::Terminal::error('Not yet implemented', ["The code which should create branch #{App::Terminal::format_branch(@target_branch)} on #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config::WORKSTATION_PATH_TO_BP_DB))} hasn't been programmed yet.", "Please speak to #{App::Terminal::format_highlight('Albert')}."], true)
end
App::Terminal::output("Updating #{App::Terminal::format_branch(@target_branch)}")
commands = [
"git checkout #{App::Git::MASTER}",
'git pull'
]
if @target_branch != App::Git::MASTER
additional_commands = [
"git checkout #{@target_branch}",
'git pull',
"git merge #{App::Git::MASTER} --no-edit"
]
commands.push(*additional_commands)
end
@git.repo_loop.each do |repo_dir|
App::Terminal::command(commands, repo_dir)
end
branches_data = []
@source_branches.each do |branch_name|
branches_data << @git.branch_data(branch_name, nil, false)
end
branches_data.each_with_index do |branch_data, idx|
if branch_data[0][:"#{App::Git::BRANCH_EXISTS}"] || branch_data[1][:"#{App::Git::BRANCH_EXISTS}"]
atleast_one_branch_found = true
end
if branch_data[0][:"#{App::Git::BRANCH_EXISTS}"] == false
non_existent_branches_cd << branch_data[0][:"#{App::Git::BRANCH_NAME}"]
end
if branch_data[1][:"#{App::Git::BRANCH_EXISTS}"] == false
non_existent_branches_db << branch_data[0][:"#{App::Git::BRANCH_NAME}"]
end
if branch_data[0][:"#{App::Git::BRANCH_HAS_STASH}"]
branches_with_stashes_cd << branch_data[0][:"#{App::Git::BRANCH_NAME}"]
end
if branch_data[1][:"#{App::Git::BRANCH_HAS_STASH}"]
branches_with_stashes_db << branch_data[1][:"#{App::Git::BRANCH_NAME}"]
end
if branch_data[0][:"#{App::Git::BRANCH_EXISTS}"]
commands_cd = [
"git checkout #{branch_data[0][:"#{App::Git::BRANCH_NAME}"]}",
'git pull'
]
commands_cd << "git checkout #{current_branch_cd}" if idx == (branches_data.length - 1)
App::Terminal::command(commands_cd, App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
end
if branch_data[1][:"#{App::Git::BRANCH_EXISTS}"]
commands_db = [
"git checkout #{branch_data[1][:"#{App::Git::BRANCH_NAME}"]}",
'git pull'
]
commands_db << "git checkout #{current_branch_db}" if idx == (branches_data.length - 1)
App::Terminal::command(commands_db, App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
end
end
puts
if non_existent_branches_cd.any?
App::Terminal::warning("The following branches could not be found \xe2\x80\x94 #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))}", non_existent_branches_cd, false)
atleast_one_error = true
end
if branches_with_stashes_cd.any?
App::Terminal::warning("The following branches have #{App::Terminal::format_highlight('stashes', true)} \xe2\x80\x94 #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))}", branches_with_stashes_cd, false)
atleast_one_error = true
end
if non_existent_branches_db.any?
App::Terminal::warning("The following branches could not be found \xe2\x80\x94 #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))}", non_existent_branches_db, false)
atleast_one_error = true
end
if branches_with_stashes_db.any?
App::Terminal::warning("The following branches have #{App::Terminal::format_highlight('stashes', true)} \xe2\x80\x94 #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))}", branches_with_stashes_db, false)
atleast_one_error = true
end
if atleast_one_branch_found == false
App::Terminal::error('Source branches not found', 'Nothing to merge. Aborting script.', true)
end
non_existent_branches_cd_dup = non_existent_branches_cd.dup
non_existent_branches_db_dup = non_existent_branches_db.dup
source_target_text = generate_source_target_text(non_existent_branches_cd_dup.concat(non_existent_branches_db_dup).uniq!)
source_target_text.unshift('')
if atleast_one_error
source_target_text.unshift('Although issues were detected, the script determined that these are non-fatal and can continue with the merge.')
end
source_target_text.unshift("This is officially the \x1B[38;5;196mPOINT OF NO RETURN\x1B[38;5;240m. Please make sure everything is OK before continuing.")
unless App::Terminal::prompt_yes_no("You're about to #{App::Terminal::format_action('merge')} between following branch(es):", source_target_text, "Are you absolutely sure you would like to #{App::Terminal::format_action('CONTINUE')}\x1B[38;5;89m with the merge?")
App::Terminal::abort
end
branches_data.each do |branch_data|
branch_name = branch_data[0][:"#{App::Git::BRANCH_NAME}"]
commands_1 = [
"git checkout #{branch_name}",
'git pull'
]
commands_2 = [
"git merge #{App::Git::MASTER} --no-edit"
]
commands_3 = [
"git diff --name-only #{App::Git::MASTER}"
]
commands_4 = @opts[:push_changes] ? ['git push'] : []
commands_4 << "git checkout #{@target_branch}"
commands_5 = [
"git merge #{branch_name} --no-edit",
]
commands_6 = []
if @target_branch != App::Git::MASTER
commands_6 << 'git push'
end
mc_information_msg = "Please #{App::Terminal::format_action('open an IDE')} and resolve your conflicts before continuing."
mc_confirmation_msg = "Have you #{App::Terminal::format_highlight('resolved your conflicts')}\x1B[38;5;89m and #{App::Terminal::format_action('committed')}\x1B[38;5;89m the changes?"
unless non_existent_branches_cd.include?(branch_name)
App::Terminal::info("Merging: #{App::Terminal::format_branch(branch_name)} \xe2\x80\x94 #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))}")
App::Terminal::command(commands_1, cd_repo)
pom_files_to_fix = App::Pom::get_files_to_change
if pom_files_to_fix.any?
App::Pom::unsnapshot_files(pom_files_to_fix, @target_branch, false, App::Enum::YES, App::Enum::YES, App::Enum::NO)
end
merge_master_result = App::Terminal::command(commands_2, cd_repo)
if merge_master_result[0] == false
unless App::Terminal::prompt_yes_no('Merge conflict occurred', ["Unable to successfully merge #{App::Terminal::format_branch(App::Git::MASTER)} \xe2\x86\x92 #{App::Terminal::format_branch(branch_name)} \xe2\x80\x94 (#{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))})", mc_information_msg], mc_confirmation_msg)
App::Terminal::abort
end
end
changed_files_code["#{branch_name}"] = App::Terminal::command_capture(commands_3, cd_repo)
App::Terminal::command(commands_4, cd_repo)
merge_to_target = App::Terminal::command(commands_5, cd_repo)
if merge_to_target[0] == false
unless App::Terminal::prompt_yes_no('Merge conflict occurred', ["Unable to successfully merge #{App::Terminal::format_branch(branch_name)} \xe2\x86\x92 #{App::Terminal::format_branch(@target_branch)} \xe2\x80\x94 (#{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))})", mc_information_msg], mc_confirmation_msg)
App::Terminal::abort
end
end
if commands_6.any?
App::Terminal::command(commands_6, cd_repo)
end
end
unless non_existent_branches_db.include?(branch_name)
App::Terminal::info("Merging: #{App::Terminal::format_branch(branch_name)} \xe2\x80\x94 #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))}")
App::Terminal::command(commands_1, db_repo)
merge_master_result = App::Terminal::command(commands_2, db_repo)
if merge_master_result[0] == false
unless App::Terminal::prompt_yes_no('Merge conflict occurred', ["Unable to successfully merge #{App::Terminal::format_branch(App::Git::MASTER)} \xe2\x86\x92 #{App::Terminal::format_branch(branch_name)} \xe2\x80\x94 (#{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))})", mc_information_msg], mc_confirmation_msg)
App::Terminal::abort
end
end
changed_files_db["#{branch_name}"] = App::Terminal::command_capture(commands_3, db_repo)
App::Terminal::command(commands_4, db_repo, false)
merge_to_target = App::Terminal::command(commands_5, db_repo)
if merge_to_target[0] == false
unless App::Terminal::prompt_yes_no('Merge conflict occurred', ["Unable to successfully merge #{App::Terminal::format_branch(branch_name)} \xe2\x86\x92 #{App::Terminal::format_branch(@target_branch)} \xe2\x80\x94 (#{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))})", mc_information_msg], mc_confirmation_msg)
App::Terminal::abort
end
end
unless changed_files_db["#{branch_name}"].nil? || changed_files_db["#{branch_name}"] == '' || changed_files_db["#{branch_name}"] == ['']
App::Terminal::warning("Detected SQL Update. Following files were changed in #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_DB)))}", changed_files_db["#{branch_name}"])
begin
App::SqlUpdate::run_sql_update(branch_name)
end
ran_db_update = true
end
if commands_6.any?
App::Terminal::command(commands_6, db_repo)
end
end
end
App::Terminal::success('It seems as if everything ran smoothly', "#{@source_branches.count} #{(@source_branches.count) == 1 ? 'branch has' : 'branches have'} been successfully merged to #{App::Terminal::format_branch(@target_branch)}")
if ran_db_update
App::Terminal::info("You have run a #{App::Terminal::format_highlight('DB Update')}. Don't forget to run the following on your VM:", %W(#{App::Terminal::format_command('update-skeleton')} #{App::Terminal::format_command('update-fitnesse')}), false)
end
build_services(false)
sanity_check(false)
reminders = [
'Run the PHP Unit tests?',
'Run the PHP Behat tests?',
'Built and deployed all service?',
"Run the sonar checker? \xe2\x80\x94 #{App::Terminal::format_command('bp s s')}",
'Re-gulped everything?',
'Updated FitNesse DB?',
'Updated Skeleton DBs?',
'Removed all sandpit files?',
]
puts
App::Terminal::info("#{App::Terminal::format_action('Final Reminders')} \xe2\x80\x94 Have you:", reminders, false)
puts
@git.check_for_stash(true)
end
|