Class: AppCommand::GitMerge

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Defined in:
lib/routes/git_merge.rb

Instance Method Summary collapse

Instance Method Details

#delete_source_branches_locallyObject



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/routes/git_merge.rb', line 338

def delete_source_branches_locally

    # Initial confirmation
    unless App::Terminal::prompt_yes_no("You're about to #{App::Terminal::format_action('delete')} the following branch(es) #{App::Terminal::format_highlight('locally')}:", generate_source_target_text, "Would you like to #{App::Terminal::format_action('CONTINUE')}\x1B[38;5;89m")
        App::Terminal::abort(nil, nil, true, false)
    end

    @source_branches.each do |branch_name|
        App::GitDelete::delete_local(branch_name, false, @git)
    end

end

#delete_source_branches_remotelyObject



351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/routes/git_merge.rb', line 351

def delete_source_branches_remotely

    # Initial confirmation
    unless App::Terminal::prompt_yes_no("You're about to #{App::Terminal::format_action('delete')} the following branch(es) #{App::Terminal::format_highlight('remotely')}:", generate_source_target_text, "Would you like to #{App::Terminal::format_action('CONTINUE')}\x1B[38;5;89m")
        App::Terminal::abort(nil, nil, true, false)
    end

    @source_branches.each do |branch_name|
        App::GitDelete::delete_remote(branch_name, false, @git)
    end

end

#executeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/routes/git_merge.rb', line 5

def execute

    @opts = command_options
    @args = arguments
    @git = App::Git.new

    @source_branches = []
    @target_branch = nil

    opts_validate
    opts_routing

end

#mergeObject



70
71
72
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
# File 'lib/routes/git_merge.rb', line 70

def merge

    @git.check_for_uncommitted_files(false, 'Aborting the merge!')

    # If no 'source branches' are found, select current branch (if both repos on same branch) or throw Error.
    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

    # Check a RELEASE BRANCH or MASTER isn't one of the 'source branches'
    @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

    # Check a RELEASE-BRANCH isn't one of the 'target branches'
    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

    # Initial confirmation
    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(nil, nil, true, false)
    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 = {}

    # UPDATE MASTER & CHECKOUT BRANCH TO MERGE TO
    App::Terminal::output("Updating #{App::Terminal::format_branch(@target_branch)}")
    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

    # CREATE TARGET BRANCH (CODE OR DB) IF ONE OR THE OTHER DOESN'T EXIST
    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

    commands = [
        "git checkout #{App::Git::MASTER}",
        'git pull',
        "git checkout #{@target_branch}",
        'git pull',
        "git merge #{App::Git::MASTER} --no-edit"
    ]
    @git.repo_loop.each do |repo_dir|
        App::Terminal::command(commands, repo_dir)
    end

    # GET DATA FIRST
    branches_data = []
    @source_branches.each do |branch_name|
        branches_data << @git.branch_data(branch_name, nil, false)
    end

    # RUN CHECKS (AND CHECKOUT BRANCHES WHICH DON'T EXIST LOCALLY)
    branches_data.each do |branch_data|
        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_LOCALLY}"] == false && branch_data[0][:"#{App::Git::BRANCH_EXISTS}"]
            commands_cd = [
                "git checkout #{branch_data[0][:"#{App::Git::BRANCH_NAME}"]}",
                'git pull',
                "git checkout #{current_branch_cd}"
            ]
            App::Terminal::command(commands_cd, App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
        end
        if branch_data[1][:"#{App::Git::BRANCH_EXISTS_LOCALLY}"] == false && branch_data[1][:"#{App::Git::BRANCH_EXISTS}"]
            commands_db = [
                "git checkout #{branch_data[1][:"#{App::Git::BRANCH_NAME}"]}",
                'git pull',
                "git checkout #{current_branch_db}"
            ]
            App::Terminal::command(commands_db, App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
        end
    end

    puts # DO NOT REMOVE.

    if non_existent_branches_cd.any? || branches_with_stashes_cd.any?
        puts "\n  #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))}\n\n"
    end
    if non_existent_branches_cd.any?
        App::Terminal::warning("The following branches #{App::Terminal::format_invalid('could not be found', true)} \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 #{App::Terminal::format_invalid('have 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? || branches_with_stashes_db.any?
        puts "\n  #{App::Terminal::format_directory(@git.get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))}\n\n"
    end
    if non_existent_branches_db.any?
        App::Terminal::warning("The following branches #{App::Terminal::format_invalid('could not be found', true)} \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 #{App::Terminal::format_invalid('have 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(nil, nil, true, false)
    end

    # MERGE STARTS HERE !!
    branches_data.each do |branch_data|

        branch_name = branch_data[0][:"#{App::Git::BRANCH_NAME}"]
        App::Terminal::info("Merging: #{App::Terminal::format_branch(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 = [
            'git push',
            "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?"

        # CODE BRANCHES
        unless non_existent_branches_cd.include?(branch_name)
            App::Terminal::command(commands_1, cd_repo)

            # FIND OUT IF WE NEED TO FIX POM FILES
            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)
            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(nil, nil, true, false)
                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(nil, nil, true, false)
                end
            end
            if commands_6.any?
                App::Terminal::command(commands_6, cd_repo)
            end
        end

        # DB BRANCHES
        unless non_existent_branches_db.include?(branch_name)
            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(nil, nil, true, false)
                end
            end

            changed_files_db["#{branch_name}"] = App::Terminal::command_capture(commands_3, db_repo)

            unless changed_files_db["#{branch_name}"].nil? || changed_files_db["#{branch_name}"] == '' || changed_files_db["#{branch_name}"] == ['']

                puts changed_files_db
                puts
                puts
                puts changed_files_db["#{branch_name}"].inspect

                unless App::Terminal::prompt_yes_no('BOMBED OUT ON SQL CHANGES!!', changed_files_db["#{branch_name}"])
                    App::Terminal::abort(nil, nil, true, false)
                end

            end

            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(nil, nil, true, false)
                end
            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)}")

    sanity_check
    puts

    @git.check_for_stash(true)

end

#opts_routingObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/routes/git_merge.rb', line 47

def opts_routing

    retrieve_source_branches unless @opts[:open_file]
    retrieve_target_branches unless @opts[:open_file]

    if @opts[:sanity_check]
        sanity_check
        exit
    elsif @opts[:open_file]
        system("#{App::Config.param(App::Config::PREFERRED_TEXT_EDITOR)} #{App::Enum::GIT_MERGE_DEFAULT_FILE}")
        exit
    elsif @opts[:delete_source_branches_local]
        delete_source_branches_locally
        exit
    elsif @opts[:delete_source_branches_remote]
        delete_source_branches_remotely
        exit
    end

    merge

end

#opts_validateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/routes/git_merge.rb', line 19

def opts_validate

    if @opts[:from_file] && @opts[:open_file]
        App::Terminal::error('Too many flags', "You cannot pass the #{App::Terminal::format_flag('o', false)} and #{App::Terminal::format_flag('f', false)} flags at the same time.", true)
    end

    unless @args.any?
        unless @opts[:from_file] || @opts[:open_file] || @opts[:delete_source_branches_local] || @opts[:delete_source_branches_remote] || @otps[:sanity_check]
            system('bp g m -h')
            exit
        end
    end

    if @args[0] == App::Git::MASTER && !@opts[:from_file]
        App::Terminal::error('Not allowed', "You cannot merge #{App::Terminal::format_branch(App::Git::MASTER)}\x1B[38;5;240m to #{App::Terminal::format_branch(App::Git::MASTER)}", true)
    end

    if @opts[:from_file]
        unless @args[1].nil?
            App::Terminal::error('Too many parameters', ["When using the #{App::Terminal::format_flag('f')}\x1B[38;5;240m the system only expects one #{App::Terminal::format_action('optional')}\x1B[38;5;240m parameter \xe2\x80\x94 the target branch.", "The amount of parameters you passed were: #{App::Terminal::format_highlight(@args.length)}"], true)
        end
        if @opts[:from_file] != '' && File.file?(App::Enum::GIT_MERGE_DEFAULT_FILE) == false
            App::Terminal::error("File not found: #{App::Terminal::format_directory(App::Enum::GIT_MERGE_DEFAULT_FILE)}", ["To specify multiple #{App::Terminal::format_branch('source-branches')} try running #{App::Terminal::format_command('bp g m -o')} first."], true)
        end
    end

end

#sanity_checkObject



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/routes/git_merge.rb', line 364

def sanity_check

    App::Terminal::info("Running sanity check against: #{App::Terminal::format_branch(@target_branch)}")

    @source_branches.each do |source_branch|

        jira_numbers = source_branch.scan(/\d{4,5}/i)
        if jira_numbers.any?
            jira_numbers.each do |jira_number|
                unless jira_number == ''
                    sanity_check_grep(jira_number, source_branch, @target_branch)
                end

            end
        else
            sanity_check_grep(source_branch, source_branch, @target_branch)
        end

    end

    puts

end