Class: App::Git

Inherits:
Object
  • Object
show all
Includes:
Columnist
Defined in:
lib/core/git.rb

Constant Summary collapse

DELIMITER =
'*|*'
SORT_DATE =
'date'
SORT_REFNAME =
'refname'
LOCAL =
'local'
REMOTE =
'remote'
CODE =
'code'
DB =
'db'
MERGED_PREFIX =
'MERGED-'
MDASH =
"\xe2\x80\x94"
MASTER =
'master'
REFNAME =
'refname'
OBJECT_TYPE =
'objecttype'
OBJECT_SIZE =
'objectsize'
OBJECT_NAME =
'objectname'
TREE =
'tree'
NUM_PARENT =
'numparent'
COMMITTER_NAME =
'committername'
COMMITTER_EMAIL =
'committeremail'
COMMITTER_DATE =
'committerdate'
SUBJECT =
'subject'
UPSTREAM =
'upstream'
BRANCH_NAME =
'branch_name'
BRANCH_LOCATION =
'branch_location'
BRANCH_EXISTS =
'branch_exists'
BRANCH_EXISTS_LOCALLY =
'branch_exists_locally'
BRANCH_IS_CURRENT =
'branch_is_current'
BRANCH_IS_UP_TO_DATE =
'branch_is_up_to_date'
BRANCH_HAS_STASH =
'branch_has_stash'
BRANCH_HAS_UPSTREAM =
'branch_has_upstream'
SAME_BRANCH_WARNING =
'same_branch_warning'
SAME_BRANCH_ERROR =
'same_branch_error'
RELEASE_BRANCH_REGEX =
/release-\d+\.\d+\.\d+-\d{4}/
GIT_MERGE_DEFAULT_FILE =
'~/tmp/bp-merge.txt'
GIT_LAST_GIT_FETCH_FILE =
'~/tmp/bp-last-git-fetch.tmp'
GIT_LAST_BRANCH_GET_DATA_FILE =
'~/tmp/bp-last-branch-get-data-%s.tmp'
GIT_LAST_BRANCH_GET_TIME_FILE =
'~/tmp/bp-last-branch-get-time-%s.tmp'

Instance Method Summary collapse

Constructor Details

#initializeGit

Returns a new instance of Git.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/core/git.rb', line 56

def initialize

    @origin_updated_code = false
    @origin_updated_db = false
    @branch_upstream_data = false
    @branches_as_array = {
        :local_code => nil,
        :local_db => nil,
        :local_both => nil,
        :remote_code => nil,
        :remote_db => nil,
        :remote_both => nil
    }
    @branches_as_array_all = false

end

Instance Method Details

#ask_to_commit_to_git(commit_message, repo_dir, first_answer = nil, second_answer = nil) ⇒ Object

Shows prompts asking to ‘Commit to GIT’ and ‘Push to [master]’

Returns:

  • void



898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/core/git.rb', line 898

def ask_to_commit_to_git(commit_message, repo_dir, first_answer = nil, second_answer = nil)
    if first_answer == App::Enum::NO
        App::Terminal::warning("Your changes #{App::Terminal::format_highlight('HAVE NOT')} been #{App::Terminal::format_action('committed')} to GIT.", 'You will need to do this manually.', false)
    else
        if first_answer == App::Enum::YES || App::Terminal::prompt_yes_no('Commit to GIT', ["This will commit your changes to GIT with message: #{App::Terminal::format_highlight(commit_message)}"])
            App::Terminal::command(["git commit -m '#{commit_message.gsub("'", "\\'")}'"], repo_dir)
            App::Terminal::info("Your changes have been #{App::Terminal::format_action('committed')} to GIT.")
        else
            App::Terminal::warning("Your changes #{App::Terminal::format_highlight('HAVE NOT')} been #{App::Terminal::format_action('committed')} to GIT.", 'You will need to do this manually.', false)
            return
        end
    end
    if second_answer == App::Enum::NO
        App::Terminal::warning("Your changes #{App::Terminal::format_highlight('HAVE NOT')} been #{App::Terminal::format_action('pushed')} to origin.", 'You will need to do this manually.', false)
    else
        if second_answer == App::Enum::YES || App::Terminal::prompt_yes_no('Push changes to origin')
            App::Terminal::command('git push', repo_dir)
            App::Terminal::info("Your changes have been #{App::Terminal::format_action('pushed')} to origin.")
        else
            App::Terminal::warning("Your changes #{App::Terminal::format_highlight('HAVE NOT')} been #{App::Terminal::format_action('pushed')} to origin.", 'You will need to do this manually.', false)
        end
    end
end

#ask_to_setup_remote_tracking(current_branch, repo_dir) ⇒ Object

Asks if you would like to setup remote tracking. WILL ASSUME that branch doesn’t have this (but won’t actually check).

Returns:

  • void



819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/core/git.rb', line 819

def ask_to_setup_remote_tracking(current_branch, repo_dir)
    tracking_setup_command = "git branch --set-upstream-to=origin/#{current_branch} #{current_branch}"
    if App::Terminal::prompt_yes_no('Setup remote tracking?', ["No tracking information found for #{App::Terminal::format_branch(current_branch)} on #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))}", "By continuing, the script will automatically setup your tracking information with: #{tracking_setup_command}"])
        App::Terminal::output("Setting tracking information for this branch to origin/#{current_branch}")
        if !branch_exists(current_branch, repo_dir, App::Git::REMOTE)
            App::Terminal::command("git push -u origin #{current_branch}", repo_dir)
        end
        App::Terminal::command(tracking_setup_command, repo_dir)
    else
        App::Terminal::warning('No remote tracking setup', "You have not setup remote tracking for branch #{App::Terminal::format_branch(current_branch)}", false)
    end
end

#branch_data(branch_name, code_db = nil, verbose = true) ⇒ Object

Returns an array that include info for BOTH repos. Finds out whether a branch exists and if it’s current or not Only does it locally.

Returns:

  • Array



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/core/git.rb', line 663

def branch_data(branch_name, code_db = nil, verbose = true)
    data = []
    repo_loop.each do |repo_dir|
        unless code_db.nil?
            validate_code_db(code_db)
            case code_db
                when CODE
                    if repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)
                        data << nil
                        next
                    end
                else
                    if repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)
                        data << nil
                        next
                    end
            end
        end
        App::Terminal::output("Retrieving branch data for #{App::Terminal::format_branch(branch_name)} \xe2\x80\x94 (#{get_repo_shorthand(repo_dir)})")
        repo_data = {}
        repo_data
        repo_data[:"#{BRANCH_NAME}"] = branch_name
        repo_data[:"#{BRANCH_LOCATION}"] = repo_dir
        repo_data[:"#{BRANCH_EXISTS}"] = true
        repo_data[:"#{BRANCH_EXISTS_LOCALLY}"] = true
        repo_data[:"#{BRANCH_HAS_STASH}"] = false
        repo_data[:"#{BRANCH_HAS_UPSTREAM}"] = branch_has_upstream(repo_dir, branch_name)
        repo_data[:"#{BRANCH_IS_CURRENT}"] = (current_branch_for_repo(repo_dir) == branch_name ? true : false)
        if repo_data[:"#{BRANCH_HAS_UPSTREAM}"]
            # @todo
            repo_data[:"#{BRANCH_IS_UP_TO_DATE}"] = nil
        else
            # @todo
            repo_data[:"#{BRANCH_IS_UP_TO_DATE}"] = nil
        end
        # Check branch exists
        unless branch_exists(branch_name, repo_dir, App::Git::LOCAL)
            repo_data[:"#{BRANCH_EXISTS_LOCALLY}"] = false
            App::Terminal::output("Branch #{App::Terminal::format_branch(branch_name)} not found #{App::Terminal::format_action('locally')} in #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))}", App::Terminal::MSG_WARNING) if verbose
            App::Terminal::output('Checking origin') if verbose
            unless branch_exists(branch_name, repo_dir, App::Git::REMOTE)
                App::Terminal::output("Branch #{App::Terminal::format_branch(branch_name)} not found #{App::Terminal::format_action('remotely')} in #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))}", App::Terminal::MSG_WARNING) if verbose
                repo_data[:"#{BRANCH_EXISTS}"] = false
                data << repo_data
                next
            end
        end
        # Check for stashed changes
        stashes = `cd #{repo_dir} && git stash list | grep 'WIP on #{branch_name}:'`
        stashes = stashes.split("\n")
        if stashes.any?
            repo_data[:"#{BRANCH_HAS_STASH}"] = stashes.count
        end
        data << repo_data
    end
    data
end

#branch_exists(branch_name, repo_dir, local_remote = LOCAL) ⇒ Object

Returns TRUE if branch exists, FALSE if not

Returns:

  • Array



723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/core/git.rb', line 723

def branch_exists(branch_name, repo_dir, local_remote = LOCAL)
    validate_repo_dir(repo_dir)
    validate_local_remote(local_remote)
    case local_remote
        when LOCAL
            branches = get_branches_as_array(LOCAL, repo_dir)
        else
            branches = get_branches_as_array(REMOTE, repo_dir)
    end
    unless branches.include?(branch_name)
        return false
    end
    true
end

#branch_exists_anywhere(branch_name, case_sensitive = false) ⇒ Object

Checks if branch exists ANYWHERE – Local, Remote, Code, DB. If the branch name is found anywhere, will return FALSE.

Returns:

  • boolean



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/core/git.rb', line 740

def branch_exists_anywhere(branch_name, case_sensitive = false)
    # If new branch is 12345, find possible branches.
    if branch_name =~ /\A\d{4,5}\z/i
        branch_name = resolve_branch_for_jira(branch_name)
    end
    existing_branches = get_all_branches_as_array
    if case_sensitive
        existing_branches.map!(&:downcase)
        branch_name.downcase!
    end
    unless existing_branches.include?(branch_name)
        return false
    end
    true
end

#branch_has_upstream(repo_dir, branch_name) ⇒ Object

Deprecated.

Checks if a branch has an upstream (remote) branch. Returns TRUE or FALSE.

Returns:

  • boolean



759
760
761
762
763
764
# File 'lib/core/git.rb', line 759

def branch_has_upstream(repo_dir, branch_name)
    validate_repo_dir(repo_dir)
    update_origin(repo_dir)
    result = `cd #{repo_dir} && git branch -vv | grep 'origin/#{branch_name}'`
    result == '' ? false : true
end

#branch_is_up_to_date(repo_dir, branch_name) ⇒ Object

Checks if a branch is up-to-date with its remote

Returns:

  • boolean



777
778
779
780
781
782
783
# File 'lib/core/git.rb', line 777

def branch_is_up_to_date(repo_dir, branch_name)
    validate_repo_dir(repo_dir)
    result_local = `cd #{repo_dir} && git rev-parse #{branch_name}`
    result_remote = `cd #{repo_dir} && git rev-parse origin/#{branch_name}`
    result_local == result_remote ? true : false

end

#branch_synced_with_upstream(repo_dir, branch_name) ⇒ Object

Deprecated.

Checks if a branch is at same HEAD as upstream. Returns TRUE or FALSE.

Returns:

  • boolean



769
770
771
772
773
# File 'lib/core/git.rb', line 769

def branch_synced_with_upstream(repo_dir, branch_name)
    validate_repo_dir(repo_dir)
    result = `cd #{repo_dir} && git branch -vv | grep 'origin/#{branch_name}'`
    result == '' ? false : true
end

#changes_exist(repo_dir) ⇒ Object

Returns TRUE if changes exist or FALSE if there is nothing to commit.

Returns:

  • boolean



883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/core/git.rb', line 883

def changes_exist(repo_dir)
    validate_repo_dir(repo_dir)
    git_results = App::Terminal::command_capture('git status', repo_dir)
    changes_exist = true
    git_results = git_results[0].split("\n")
    git_results.each do |line|
        if line =~ /nothing to commit, working directory clean/
            changes_exist = false
        end
    end
    changes_exist
end

#check_branch_does_not_exist_anywhere(branch_name) ⇒ Object

Checks that a branch DOESN’T exist anywhere (local, remote, code and db). If it does, will exit with error.

Returns:

  • void



802
803
804
805
806
807
# File 'lib/core/git.rb', line 802

def check_branch_does_not_exist_anywhere(branch_name)
    App::Terminal::info("Checking if branch #{App::Terminal::format_branch(branch_name)} doesn't already exist somewhere...")
    if branch_exists_anywhere(branch_name)
        App::Terminal::error('Branch already exists', "Cannot continue because a branch named #{App::Terminal::format_branch(branch_name)}\x1B[38;5;240m already exists somewhere.", true)
    end
end

#check_branch_exists_somewhere(branch_name) ⇒ Object

Checks that a branch DOES exist somewhere (local, remote, code or db). If it does not, will exit with error.

Returns:

  • void



811
812
813
814
815
# File 'lib/core/git.rb', line 811

def check_branch_exists_somewhere(branch_name)
    unless branch_exists_anywhere(branch_name)
        App::Terminal::error("Branch doesn't exist", "Cannot continue because branch #{App::Terminal::format_branch(branch_name)}\x1B[38;5;240m doesn't exist.", true)
    end
end

#check_for_conflicts(result, repo_dir, additional_info = nil) ⇒ Object

Pass the result of App::Terminal::command() and this should do the rest. Bombs out if you don’t confirm.

Returns:

  • void



924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/core/git.rb', line 924

def check_for_conflicts(result, repo_dir, additional_info = nil)
    validate_repo_dir(repo_dir)
    error_array = []
    unless additional_info.nil?
        error_array = []
        if additional_info.is_a? Array
            additional_info.each do |info|
                error_array << info
            end
        else
            if additional_info.is_a? String
                error_array << additional_info
            else
                raise RuntimeError, "Expected String, got: #{additional_info.class}"
            end
        end
    end
    error_message = "Merge conflict occurred in: #{get_repo_shorthand(repo_dir)}"
    error_array << "Please #{App::Terminal::format_action('open an IDE')} and resolve your conflicts before continuing."
    conflict_resolved = (result == false) ? false : true
    changes_to_be_committed = false
    while conflict_resolved == false
        unless App::Terminal::prompt_yes_no(error_message, error_array, "Have you #{App::Terminal::format_highlight('resolved', true)}\x1B[38;5;89m your conflicts?")
            App::Terminal::abort
        end
        conflict_resolved = true
        changes_to_be_committed = false
        error_message = 'It seems there are still files which need resolving:'
        error_array = []
        git_status = `git status`
        git_status.split("\n").each do |line|
            if line.strip =~ /\Aunmerged\spaths:\z/i
                conflict_resolved = false
            elsif line.strip =~ /\Aboth\smodified:/i
                conflict_resolved = false
                line_split = line.split('modified:')
                error_array << "\x1B[38;5;223m#{line_split[1].strip}\x1B[0m"
            elsif line.strip =~ /\Achanges\sto\sbe\scommitted:/i
                changes_to_be_committed = true
            end
        end
    end
    App::Terminal::command('git commit --no-edit', repo_dir) if changes_to_be_committed
end

#check_for_same_branch(display = SAME_BRANCH_WARNING) ⇒ Object

Checks if both repos are on same branch and displays either ERROR or WARNING.

Returns:

  • boolean – TRUE if same branch, FALSE if not.



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/core/git.rb', line 637

def check_for_same_branch(display = SAME_BRANCH_WARNING)
    branch_code = current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
    branch_db = current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
    if branch_code != branch_db
        case display
            when SAME_BRANCH_WARNING
                App::Terminal::warning("You're code is on #{App::Terminal::format_highlight('2 different branches')}", [
                    "#{App::Terminal::format_directory(get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))} is on #{App::Terminal::format_branch(branch_code)}",
                    "#{App::Terminal::format_directory(get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))}   is on #{App::Terminal::format_branch(branch_db)}"
                ])
            when SAME_BRANCH_ERROR
                App::Terminal::error("You're code is on #{App::Terminal::format_highlight('2 different branches')}", [
                    "#{App::Terminal::format_directory(get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)))} is on #{App::Terminal::format_branch(branch_code)}",
                    "#{App::Terminal::format_directory(get_repo_shorthand(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)))}   is on #{App::Terminal::format_branch(branch_db)}",
                    nil,
                    'You cannot run this command unless both of your repos are on the same branch.'
                ])
        end
        return false
    end
    true
end

#check_for_stash(automatic = false) ⇒ Object

Checks if you currently have stashed changes for current branch and if so, prompt to un-stash them.

Returns:

  • void



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/core/git.rb', line 591

def check_for_stash(automatic = false)
    repo_loop.each do |repo_dir|
        results = check_for_stash_get_stashes(repo_dir)
        if results.any?
            results_output = Array.new
            results.each do |result|
                results_output << "#{result}"
            end
            stashes_to_skip = 0
            while results.any?
                stash_name = results[0].split(/:/).first
                if automatic || App::Terminal::prompt_yes_no("Found the following stash in #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))} on branch #{App::Terminal::format_branch(current_branch_for_repo(repo_dir))}", "#{results[0]}", "Would you like to #{App::Terminal::format_action('apply')}\x1B[38;5;89m this stash now?")
                    App::Terminal::output('Un-stashing working changes', App::Terminal::MSG_AUTOMATIC) if automatic
                    stash_apply_successful = App::Terminal::command(["git stash apply #{stash_name}"], repo_dir)
                    if stash_apply_successful[0] == true
                        App::Terminal::command("git stash drop #{stash_name}", repo_dir)
                        results = check_for_stash_get_stashes(repo_dir)
                    else
                        App::Terminal::error("Stash could not be applied successfully (and therefore wasn't dropped).", ["#{results[0]}", nil, "Please #{App::Terminal::format_action('open an IDE')} and resolve your conflicts before continuing.", "Don't forget to drop your stash afterwards with: #{App::Terminal::format_command("git stash drop #{stash_name}")}"], false)
                        results = []
                    end
                else
                    stashes_to_skip = stashes_to_skip + 1
                    results = check_for_stash_get_stashes(repo_dir)
                    a = (stashes_to_skip + 0)
                    b = 0
                    while b < a do
                        results.shift
                        b += 1
                    end
                end
            end
            App::Terminal::command('git add .', repo_dir)
        end
    end
end

#check_for_stash_get_stashes(repo_dir) ⇒ Object

Sub-method for ‘show_branches’. Don’t call independently.

Returns:

  • Array



630
631
632
633
# File 'lib/core/git.rb', line 630

def check_for_stash_get_stashes(repo_dir)
    results = `cd #{repo_dir} && git stash list | grep 'WIP on #{current_branch_for_repo(repo_dir)}:'`
    results.split("\n")
end

#check_for_uncommitted_files(automatic = false, force_stash_message = false) ⇒ Object

Checks if you have staged changes in current repo and if so, prompts to stash them.

Parameters:

  • force_stash_message (defaults to: false)

    – If a string is passed, will be displayed as the “FORCE STASH WARNING” and a stash will be enforced.

Returns:

  • void



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/core/git.rb', line 566

def check_for_uncommitted_files(automatic = false, force_stash_message = false)
    repo_loop.each do |repo_dir|
        results = `cd #{repo_dir} && git status | grep 'Changes '`
        results = results.split("\n")
        if results.any?
            App::Terminal::command(['git status'], repo_dir)
            current_branch = current_branch_for_repo(repo_dir)
            if automatic || App::Terminal::prompt_yes_no("Found uncommitted files in #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))} on branch #{App::Terminal::format_branch(current_branch)}", nil, "Would you like to #{App::Terminal::format_action('stash')}\x1B[38;5;89m these changes before continuing?", false)
                App::Terminal::output('Stashing working changes', App::Terminal::MSG_AUTOMATIC) if automatic
                App::Terminal::command('git stash', repo_dir)
            else
                if force_stash_message != false
                    abort_message = force_stash_message.is_a?(String) ? force_stash_message : nil
                    App::Terminal::abort(abort_message, ["In this particular scenario, you #{App::Terminal::format_invalid('cannot continue', true)} unless you stash your changes."], true, false)
                end
                unless App::Terminal::prompt_yes_no('Continue without stashing?', "By selecting #{App::Terminal::format_action('Yes')}\x1B[38;5;240m, you're changes will be carried over to the next branch.", nil, false)
                    App::Terminal::abort
                end
            end
        end
    end
end

#current_branch_for_repo(repo_dir) ⇒ Object

Get current branch for repo.

Returns:

  • String



476
477
478
479
# File 'lib/core/git.rb', line 476

def current_branch_for_repo(repo_dir)
    validate_repo_dir(repo_dir)
    `cd #{repo_dir} && git symbolic-ref --short -q HEAD`.strip
end

#epoch_currentObject

Returns current time in epoch

Returns:

  • Integer



1015
1016
1017
# File 'lib/core/git.rb', line 1015

def epoch_current
    Time.now.strftime('%s').to_i
end

#get_all_branches_as_arrayObject

Gets EVERY SINGLE BRANCH that exists – local & remote, code & db – and puts them in a single, unique, array.

Returns:

  • Array



315
316
317
318
319
320
321
322
323
324
# File 'lib/core/git.rb', line 315

def get_all_branches_as_array
    unless @branches_as_array_all
        local_branches = get_branches_as_array(App::Git::LOCAL)
        remote_branches = get_branches_as_array(App::Git::REMOTE)
        @branches_as_array_all = local_branches[0].concat(local_branches[1]).concat(remote_branches[0]).concat(remote_branches[1])
        @branches_as_array_all.uniq!
        @branches_as_array_all.sort_by! { |m| m.downcase }
    end
    @branches_as_array_all
end

#get_branches_as_array(local_remote = LOCAL, repo_dir = nil) ⇒ Object

Gets branch names as array.

Returns:

  • Array



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
# File 'lib/core/git.rb', line 238

def get_branches_as_array(local_remote = LOCAL, repo_dir = nil)
    validate_local_remote(local_remote)

    # Check if result is already cached.
    if repo_dir.nil?
        if local_remote == LOCAL
            return @branches_as_array[:local_both] if @branches_as_array[:local_both] != nil
        else
            return @branches_as_array[:remote_both] if @branches_as_array[:remote_both] != nil
        end
    else
        if repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)
            if local_remote == LOCAL
                return @branches_as_array[:local_code] if @branches_as_array[:local_code] != nil
            else
                return @branches_as_array[:remote_code] if @branches_as_array[:remote_code] != nil
            end
        else
            if local_remote == LOCAL
                return @branches_as_array[:local_db] if @branches_as_array[:local_db] != nil
            else
                return @branches_as_array[:remote_db] if @branches_as_array[:remote_db] != nil
            end
        end
    end

    case local_remote
        when LOCAL
            if repo_dir.nil?
                brightpearl_code, brightpearl_db = get_local_branches
                repos_branches = [brightpearl_code, brightpearl_db]
            else
                branches = get_local_branches(SORT_REFNAME, repo_dir)
                repos_branches = [branches]
            end
        else
            if repo_dir.nil?
                brightpearl_code, brightpearl_db = get_remote_branches
                repos_branches = [brightpearl_code, brightpearl_db]
            else
                branches = get_remote_branches(SORT_REFNAME, repo_dir)
                repos_branches = [branches]
            end
    end
    repos_to_return = []
    repos_branches.each do |repo_data|
        branch_names = []
        repo_data.each do |branch_data|
            branch_names << branch_data[:"#{REFNAME}"]
        end
        repos_to_return << branch_names
    end
    if repo_dir.nil?
        if local_remote == LOCAL
            return @branches_as_array[:local_both] = repos_to_return[0], repos_to_return[1]
        else
            return @branches_as_array[:remote_both] = repos_to_return[0], repos_to_return[1]
        end
    else
        if repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)
            if local_remote == LOCAL
                return @branches_as_array[:local_code] = repos_to_return[0]
            else
                return @branches_as_array[:remote_code] = repos_to_return[0]
            end
        else
            if local_remote == LOCAL
                return @branches_as_array[:local_db] = repos_to_return[0]
            else
                return @branches_as_array[:remote_db] = repos_to_return[0]
            end
        end
    end
end

#get_changed_files(repo_dir, branch_to_compare_to = "origin/#{App::Git::MASTER}") ⇒ Object

Get list of files that have changed between 2 branches

Returns:

  • Array



971
972
973
974
975
# File 'lib/core/git.rb', line 971

def get_changed_files(repo_dir, branch_to_compare_to = "origin/#{App::Git::MASTER}")
    validate_repo_dir(repo_dir)
    command = "git diff #{branch_to_compare_to}..#{current_branch_for_repo(repo_dir)} --name-only"
    App::Terminal::command_capture(command, repo_dir)
end

#get_local_branches(sort = SORT_REFNAME, repo_dir = nil) ⇒ Object

Gets detailed info about local branches as Array.

Returns:

  • Array



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
# File 'lib/core/git.rb', line 75

def get_local_branches(sort = SORT_REFNAME, repo_dir = nil)
    repos = []
    repos_to_return = []
    validate_sort(sort)
    if repo_dir.nil?
        repo_loop.each do |repo_dir_inner|
            repos << repo_dir_inner
        end
    else
        validate_repo_dir(repo_dir)
        repos << repo_dir
    end
    repos.each do |repo_dir_inner|
        unless File.directory?(File.expand_path(repo_dir_inner))
            App::Terminal::error('Directory not found', "Directory doesn't exist: #{App::Terminal::format_directory(repo_dir_inner)}", true)
        end
        raw_terminal_output = `
            cd #{repo_dir_inner} &&
            git for-each-ref refs/heads/ --format='%(#{REFNAME})#{DELIMITER}%(#{OBJECT_TYPE})#{DELIMITER}%(#{OBJECT_SIZE})#{DELIMITER}%(#{OBJECT_NAME})#{DELIMITER}%(tree)#{DELIMITER}%(#{NUM_PARENT})#{DELIMITER}%(#{COMMITTER_NAME})#{DELIMITER}%(#{COMMITTER_EMAIL})#{DELIMITER}%(#{COMMITTER_DATE})#{DELIMITER}%(#{SUBJECT})#{DELIMITER}%(#{UPSTREAM})#{DELIMITER}' | sed 's/refs\\/heads\\///g'
        `
        branches = {}
        branch_count = 0
        raw_terminal_output.split("\n").each do |line|
            line_split = line.split(DELIMITER)
            data = {}
            data[:"#{REFNAME}"] = line_split[0]
            data[:"#{OBJECT_TYPE}"] = line_split[1]
            data[:"#{OBJECT_SIZE}"] = line_split[2]
            data[:"#{OBJECT_NAME}"] = line_split[3]
            data[:"#{TREE}"] = line_split[4]
            data[:"#{NUM_PARENT}"] = line_split[5]
            data[:"#{COMMITTER_NAME}"] = line_split[6]
            data[:"#{COMMITTER_EMAIL}"] = line_split[7][1..-2]
            data[:"#{COMMITTER_DATE}"] = DateTime.parse(line_split[8])
            data[:"#{SUBJECT}"] = line_split[9]
            data[:"#{UPSTREAM}"] = line_split[10]
            case sort
                when SORT_REFNAME
                    branches[data[:"#{REFNAME}"]] = data
                when SORT_DATE
                    branches["#{data[:"#{COMMITTER_DATE}"].strftime('%Y-%m-%d-%H-%I-%S')}-#{branch_count}"] = data
                    branch_count = branch_count + 1
                else
                    App::Terminal::error('Sort not supported', "The following sorting option is not yet supported: #{sort}", true)
            end
        end
        branch_keys = branches.keys
        case sort
            when SORT_REFNAME
                branch_keys.sort_by!(&:downcase)
            when SORT_DATE
                branch_keys.sort_by! { |value| value }
                branch_keys = branch_keys.reverse
            else
                App::Terminal::error('Sort not supported', "The following sorting option is not yet supported: #{sort}", true)
        end
        branch_data = []
        branch_keys.each do |key|
            branch_data << branches["#{key}"]
        end
        repos_to_return << branch_data
    end
    if repo_dir.nil?
        return repos_to_return[0], repos_to_return[1]
    else
        repos_to_return[0]
    end
end

#get_remote_branches(sort = SORT_REFNAME, repo_dir = nil) ⇒ Object

Gets detailed info about remote branches as Array.

Returns:

  • Array



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
# File 'lib/core/git.rb', line 146

def get_remote_branches(sort = SORT_REFNAME, repo_dir = nil)
    repos = []
    repos_to_return = []
    validate_sort(sort)
    if repo_dir.nil?
        repo_loop.each do |repo_dir_inner|
            repos << repo_dir_inner
        end
    else
        validate_repo_dir(repo_dir)
        repos << repo_dir
    end
    repos.each do |repo_dir_inner|
        unless File.directory?(File.expand_path(repo_dir_inner))
            App::Terminal::error('Directory not found', "Directory doesn't exist: #{App::Terminal::format_directory(repo_dir)}", true)
        end
        update_origin(repo_dir_inner)

        data_file = GIT_LAST_BRANCH_GET_DATA_FILE.gsub('%s', get_repo_shorthand(repo_dir_inner))
        time_file = GIT_LAST_BRANCH_GET_TIME_FILE.gsub('%s', get_repo_shorthand(repo_dir_inner))

        unless App::UtilsFiles::file_exists(time_file)
            App::UtilsFiles::write_file(time_file, [epoch_current])
        end
        unless App::UtilsFiles::file_exists(data_file)
            App::UtilsFiles::write_file(data_file, [''])
        end

        data_file_data = App::UtilsFiles::read_file(data_file)

        # IF THIS DATA WAS FETCHED IN THE LAST HOUR, DON'T DO IT AGAIN
        epoch_last_fetch = App::UtilsFiles::read_file(time_file)[0]
        time_difference = epoch_current - epoch_last_fetch.to_i

        if time_difference < 86400 && !data_file_data[0].nil?
            App::Terminal::output("Branch-data already up to date for #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))} \xe2\x80\x94 \x1B[38;5;240m[#{epoch_current}\xe2\x80\x94#{epoch_last_fetch}\xe2\x80\x94#{time_difference}]\x1B[0m")
            raw_terminal_output = data_file_data.join('')
        else
            App::Terminal::output("Getting branch-data for #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))} \xe2\x80\x94 \x1B[38;5;240m[#{epoch_current}\xe2\x80\x94#{epoch_last_fetch}\xe2\x80\x94#{time_difference}]\x1B[0m")
            App::UtilsFiles::write_file(time_file, [epoch_current])
            raw_terminal_output = `
                cd #{repo_dir_inner} &&
                for branch in \`git branch -r | grep -v HEAD\`; do echo \`git show --format="%cn#{DELIMITER}%ce#{DELIMITER}%cd#{DELIMITER}%s#{DELIMITER}" $branch | head -n 1\` $branch; done | sort -r | sed 's/origin\\///g'
            `
        end
        App::UtilsFiles::write_file_string(data_file, raw_terminal_output)

        branches = {}
        branch_count = 0
        raw_terminal_output.split("\n").each do |line|
            line_split = line.split(DELIMITER)
            data = {}
            data[:"#{COMMITTER_NAME}"] = line_split[0]
            data[:"#{COMMITTER_EMAIL}"] = line_split[1]
            data[:"#{COMMITTER_DATE}"] = DateTime.parse(line_split[2])
            data[:"#{SUBJECT}"] = line_split[3]
            data[:"#{REFNAME}"] = line_split[4].to_s.strip
            case sort
                when SORT_REFNAME
                    branches[data[:"#{REFNAME}"]] = data
                when SORT_DATE
                    branches["#{data[:"#{COMMITTER_DATE}"].strftime('%Y-%m-%d-%H-%I-%S')}-#{branch_count}"] = data
                    branch_count = branch_count + 1
                else
                    App::Terminal::error('Sort not supported', "The following sorting option is not yet supported: #{sort}", true)
            end
        end
        branch_keys = branches.keys
        case sort
            when SORT_REFNAME
                branch_keys.sort_by!(&:downcase)
            when SORT_DATE
                branch_keys.sort_by! { |value| value }
                branch_keys = branch_keys.reverse
            else
                App::Terminal::error('Sort not supported', "The following sorting option is not yet supported: #{sort}", true)
        end
        branch_data = []
        branch_keys.each do |key|
            branch_data << branches["#{key}"]
        end
        repos_to_return << branch_data
    end
    if repo_dir.nil?
        return repos_to_return[0], repos_to_return[1]
    else
        repos_to_return[0]
    end
end

#get_repo_shorthand(repo_dir) ⇒ Object

Returns short-hand string for repo.

Returns:

  • String



796
797
798
# File 'lib/core/git.rb', line 796

def get_repo_shorthand(repo_dir)
    repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE) ? 'brightpearl-code' : 'brightpearl-db'
end

#merge_master_into_current_branch(repo_dir, push_to_origin = true) ⇒ Object

Deprecated.

Merges master into current branch (for repo) and pushes changes to origin.

Returns:

  • void



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/core/git.rb', line 522

def merge_master_into_current_branch(repo_dir, push_to_origin = true)
    current_branch = current_branch_for_repo(repo_dir)
    if current_branch == App::Git::MASTER
        return
    end
    commands = Array.new
    commands << 'git checkout master'
    commands << 'git pull'
    if current_branch != App::Git::MASTER
        commands << "git checkout #{current_branch}"
        commands << 'git pull'
        commands << 'git merge master'
    end
    if push_to_origin
        commands << 'git push'
    end
    App::Terminal::command(commands, repo_dir)
end

#repo_loopObject

Returns array with the paths to both repos.

Returns:

  • Array



787
788
789
790
791
792
# File 'lib/core/git.rb', line 787

def repo_loop
    Array[
        App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE),
        App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)
    ]
end

#resolve_branch_for_jira(jira_number, ask_to_create = false) ⇒ Object

Resolves possible branch names from jira numbers (IE: BP-12345 from 12345)

Returns:

  • String



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/core/git.rb', line 834

def resolve_branch_for_jira(jira_number, ask_to_create = false)
    resolved_branch = jira_number
    possible_branches = []
    if jira_number =~ /\A\d{4,5}\z/
        App::Terminal::automatic("Attempting to resolve branch for: #{App::Terminal::format_highlight(jira_number)}")
        get_all_branches_as_array.each do |possible_branch|
            if possible_branch =~ /#{jira_number}/i
                # If more than one possible branch exists, IE: bug-14145 & bp-14145
                if possible_branches.any?
                    App::Terminal::error("More than one possible branch found for jira number: #{App::Terminal::format_highlight(jira_number)}", [App::Terminal::format_branch(possible_branches[0]), App::Terminal::format_branch(possible_branch), nil, "In this case, you must specify the branch name #{App::Terminal::format_highlight('explicitly')}."], true)
                end
                possible_branches << possible_branch
                App::Terminal::output("Resolved branch #{App::Terminal::format_branch(possible_branch)} from jira number: #{App::Terminal::format_highlight(jira_number)}", App::Terminal::MSG_AUTOMATIC)
            end
        end
        unless possible_branches.any?
            if ask_to_create
                App::Terminal::error("No branch found for jira number: #{App::Terminal::format_highlight(jira_number)}", ['Please check your input and try again.'], true)

                # unless App::Terminal::prompt_yes_no("No branch found for jira number: #{App::Terminal::format_highlight(jira_number)}", nil, "Would you like to #{App::Terminal::format_action('CREATE')} branch ")
                #     App::Terminal::abort
                # end
                #
                # # todo
                # # INSERT CODE TO CREATE NEW BRANCH HERE !!
                # App::Terminal::abort

            else
                App::Terminal::error("No branch found for jira number: #{App::Terminal::format_highlight(jira_number)}", ['Please check your input and try again.'], true)
            end
        end
        resolved_branch = possible_branches[0]
    end
    resolved_branch
end

#resolve_jira_for_branch(branch_name, exit_script = true) ⇒ Object

Resolves jira number from branch (IE: 12345 from BP-12345)

Returns:

  • String|boolean



872
873
874
875
876
877
878
879
# File 'lib/core/git.rb', line 872

def resolve_jira_for_branch(branch_name, exit_script = true)
    if branch_name =~ /\d{4,5}/i
        match = branch_name.match(/\d{4,5}/i)
        match.to_s
    else
        branch_name
    end
end

#show_branches(sort = SORT_REFNAME, local_remote = LOCAL) ⇒ Object

Outputs list of branches to terminal.

Returns:

  • void



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/core/git.rb', line 328

def show_branches(sort = SORT_REFNAME, local_remote = LOCAL)
    # todo
    puts
    puts "\x1B[31mTODO!\x1B[0m Must do something with 'terminal_width' parameter."
    puts
    validate_sort(sort)
    validate_local_remote(local_remote)
    # terminal_width = HighLine::SystemExtensions.terminal_size
    # terminal_width = terminal_width[0]
    case local_remote
        when LOCAL
            brightpearl_code, brightpearl_db = get_local_branches(sort)
        else
            brightpearl_code, brightpearl_db = get_remote_branches(sort)
    end
    show_branches_draw_table(brightpearl_code, brightpearl_db)
end

#show_branches_divider(column_width, color = 'white') ⇒ Object

Sub-method for ‘show_branches’. Don’t call independently.

Returns:

  • void



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/core/git.rb', line 458

def show_branches_divider(column_width, color = 'white')
    row do
        column(App::Terminal::fill(column_width[0], MDASH), :color => color)
        column(App::Terminal::fill(column_width[1], MDASH), :color => color)
        column(App::Terminal::fill(column_width[2], MDASH), :color => color)
        column(App::Terminal::fill(column_width[3], MDASH), :color => color)
        column(App::Terminal::fill(column_width[4], MDASH), :color => color)
        column(App::Terminal::fill(column_width[5], MDASH), :color => color)
        column(App::Terminal::fill(column_width[6], MDASH), :color => color)
        column(App::Terminal::fill(column_width[7], MDASH), :color => color)
        column(App::Terminal::fill(column_width[8], MDASH), :color => color)
        column(App::Terminal::fill(column_width[9], MDASH), :color => color)
        column(App::Terminal::fill(column_width[10], MDASH), :color => color)
    end
end

#show_branches_draw_table(brightpearl_code, brightpearl_db) ⇒ Object

Draws the actual table of branches.

Returns:

  • void



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/core/git.rb', line 348

def show_branches_draw_table(brightpearl_code, brightpearl_db)
    current_code_branch = current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
    current_db_branch = current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
    column_width = [2, 20, 23, 15, 34, 1, 34, 15, 23, 20, 1]
    App::Tools::validate_report_width(column_width)
    puts "\n"
    table(:border => false) do
        row do
            column('', :align => 'right', :bold => 'true', :width => column_width[0])
            column('Last Activity', :align => 'right', :bold => 'true', :width => column_width[1])
            column('Developer', :align => 'right', :bold => 'true', :width => column_width[2])
            column('Last Commit', :align => 'right', :bold => 'true', :width => column_width[3])
            column('BRIGHTPEARL-CODE', :align => 'right', :bold => 'true', :width => column_width[4], :color => 32)
            column('|', :align => 'center', :bold => 'true', :width => column_width[5])
            column('BRIGHTPEARL-DB', :align => 'left', :bold => 'true', :width => column_width[6], :color => 160)
            column('Last Commit', :align => 'left', :bold => 'true', :width => column_width[7])
            column('Developer', :align => 'left', :bold => 'true', :width => column_width[8])
            column('Last Activity', :align => 'left', :bold => 'true', :width => column_width[9])
            column('', :align => 'left', :bold => 'true', :width => column_width[10])
        end
        show_branches_divider(column_width)
        db_branches_shown = []
        brightpearl_code.each do |code_branch|
            db_branch = show_branches_get_db_branch(brightpearl_db, code_branch)
            if db_branch[:"#{REFNAME}"] != MDASH
                db_branches_shown << db_branch[:"#{REFNAME}"]
            end
            code_time_adjusted = code_branch[:"#{COMMITTER_DATE}"] - (1 / 24.0)
            row do
                code_accent, code_color, code_star, db_accent, db_color, db_star = show_branches_get_colors(code_branch, current_code_branch, current_db_branch, db_branch)
                column("#{code_star}", :color => code_color)
                column(App::Tools::time_passed_since(code_time_adjusted), :color => code_color)
                column(code_branch[:"#{COMMITTER_NAME}"], :color => code_color)
                column(code_branch[:"#{COMMITTER_DATE}"].strftime('%d %^b %H:%M'), :color => code_color)
                column(code_branch[:"#{REFNAME}"], :color => code_accent)
                column('|')
                column(db_branch[:"#{REFNAME}"], :color => db_accent)
                column((db_branch[:"#{COMMITTER_DATE}"] == nil) ? MDASH : db_branch[:"#{COMMITTER_DATE}"].strftime('%d %^b %H:%M'), :color => db_color)
                column(db_branch[:"#{COMMITTER_NAME}"], :color => db_color)
                column((db_branch[:"#{COMMITTER_DATE}"] == nil) ? MDASH : App::Tools::time_passed_since(db_branch[:"#{COMMITTER_DATE}"] - (1 / 24.0)), :color => db_color)
                column("#{db_star}", :color => db_color)
            end
        end
        brightpearl_db.each do |db_branch|
            unless db_branches_shown.include? db_branch[:"#{REFNAME}"]
                row do
                    code_accent, code_color, code_star, db_accent, db_color, db_star = show_branches_get_colors(nil, current_code_branch, current_db_branch, db_branch)
                    column('', :color => code_color)
                    column(MDASH, :color => code_color)
                    column(MDASH, :color => code_color)
                    column(MDASH, :color => code_color)
                    column(MDASH, :color => code_accent)
                    column('|')
                    column(db_branch[:"#{REFNAME}"], :color => db_accent)
                    column((db_branch[:"#{COMMITTER_DATE}"] == nil) ? MDASH : db_branch[:"#{COMMITTER_DATE}"].strftime('%d %^b %H:%M'), :color => db_color)
                    column(db_branch[:"#{COMMITTER_NAME}"], :color => db_color)
                    column((db_branch[:"#{COMMITTER_DATE}"] == nil) ? MDASH : App::Tools::time_passed_since(db_branch[:"#{COMMITTER_DATE}"]), :color => db_color)
                    column('')
                end
            end
        end
        show_branches_divider(column_width)
    end
    puts "\n"
end

#show_branches_get_colors(code_branch, current_code_branch, current_db_branch, db_branch) ⇒ Object

Sub-method for ‘show_branches’. Don’t call independently.



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/core/git.rb', line 416

def show_branches_get_colors(code_branch, current_code_branch, current_db_branch, db_branch)
    code_star = nil
    code_color = 240
    code_accent = nil
    db_star = nil
    db_color = 240
    db_accent = nil
    current_branch_color = 46
    unless code_branch.nil?
        if current_code_branch == code_branch[:"#{REFNAME}"]
            code_star = "\xe2\x98\x85"
            code_color = current_branch_color
            code_accent = current_branch_color
        end
    end
    unless db_branch.nil?
        if current_db_branch == db_branch[:"#{REFNAME}"]
            db_star = "\xe2\x98\x85"
            db_color = current_branch_color
            db_accent = current_branch_color
        end
    end
    return code_accent, code_color, code_star, db_accent, db_color, db_star
end

#show_branches_get_db_branch(brightpearl_db, code_branch) ⇒ Object

Sub-method for ‘show_branches’. Don’t call independently.

Returns:

  • void



443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/core/git.rb', line 443

def show_branches_get_db_branch(brightpearl_db, code_branch)
    brightpearl_db.each do |db_branch|
        if db_branch[:"#{REFNAME}"] == code_branch[:"#{REFNAME}"]
            return db_branch
        end
    end
    db_branch = {}
    db_branch[:"#{REFNAME}"] = MDASH
    db_branch[:"#{COMMITTER_NAME}"] = MDASH
    db_branch[:"#{COMMITTER_DATE}"] = nil
    db_branch
end

#update_origin(repo_dir = nil) ⇒ Object

Updates origin (IE: remote branches)

Returns:

  • void



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/core/git.rb', line 483

def update_origin(repo_dir = nil)
    validate_repo_dir(repo_dir)
    unless App::UtilsFiles::file_exists(GIT_LAST_GIT_FETCH_FILE)
        App::UtilsFiles::write_file(GIT_LAST_GIT_FETCH_FILE, [epoch_current])
    end
    unless @origin_updated_code && repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE) ||
        @origin_updated_db && repo_dir == App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB) ||
        @origin_updated_code && @origin_updated_db && repo_dir.nil?

        # IF ORIGIN WAS UPDATED IN THE LAST HOUR, DON'T DO IT AGAIN
        epoch_last_fetch = App::UtilsFiles::read_file(GIT_LAST_GIT_FETCH_FILE)[0]
        time_difference = epoch_current - epoch_last_fetch.to_i
        if time_difference < 3600
            App::Terminal::output("Origin already up to date \xe2\x80\x94 #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))} \xe2\x80\x94 \x1B[38;5;240m[#{epoch_current}\xe2\x80\x94#{epoch_last_fetch}\xe2\x80\x94#{time_difference}]\x1B[0m")
            return
        end
        App::UtilsFiles::write_file(GIT_LAST_GIT_FETCH_FILE, [epoch_current])

        App::Terminal::output("Updating origin \xe2\x80\x94 #{App::Terminal::format_directory(get_repo_shorthand(repo_dir))}")
        case repo_dir
            when App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)
                @origin_updated_code = true
                App::Terminal::command(['git fetch -p'], App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
            when App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)
                @origin_updated_db = true
                App::Terminal::command(['git fetch -p'], App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB))
            else
                @origin_updated_code = true
                @origin_updated_db = true
                repo_loop.each do |repo_dir_inner|
                    App::Terminal::command(['git fetch -p'], repo_dir_inner)
                end
        end
    end
end

#validate_code_db(code_db) ⇒ Object

Validates that a parameter is either ‘code’ or ‘db’

Returns:

  • void



988
989
990
991
992
# File 'lib/core/git.rb', line 988

def validate_code_db(code_db)
    unless Array[CODE, DB].include?(code_db)
        abort("'#{code_db}' is not a valid parameter. Must be CODE or DB.")
    end
end

#validate_local_remote(local_remote) ⇒ Object

Validates that a parameter is either ‘local’ or ‘remote’

Returns:

  • void



980
981
982
983
984
# File 'lib/core/git.rb', line 980

def validate_local_remote(local_remote)
    unless Array[LOCAL, REMOTE].include?(local_remote)
        ("'#{local_remote}' is not a valid parameter. Must be LOCAL or REMOTE.")
    end
end

#validate_repo_dir(repo_dir) ⇒ Object

Makes sure the repo directory is valid.

Returns:

  • void



996
997
998
999
1000
1001
1002
1003
# File 'lib/core/git.rb', line 996

def validate_repo_dir(repo_dir)
    unless Array[
        App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE),
        App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_DB)
    ].include?(repo_dir)
        abort("'#{repo_dir}' is not a valid directory.")
    end
end

#validate_sort(sort_option) ⇒ Object

Validates if sort option is supported.

Returns:

  • void



1007
1008
1009
1010
1011
# File 'lib/core/git.rb', line 1007

def validate_sort(sort_option)
    unless Array[SORT_DATE, SORT_REFNAME].include?(sort_option)
        abort("'#{sort_option}' is not a valid sort option.")
    end
end

#verify_same_branchObject

Deprecated.

Makes sure that both BP-CODE & BP-DB are on the same branch.

Returns:

  • void



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/core/git.rb', line 544

def verify_same_branch
    branch_names = Array.new
    repo_loop.each do |repo_dir|
        branch_names << current_branch_for_repo(repo_dir)
    end
    if branch_names[0] != branch_names[1]
        puts
        puts "\x1B[41m ERROR \x1B[0m Cannot continue because your repos are not on the same branch."
        puts
        puts "\x1B[33m/brightpearl-code\x1B[0m is on: \x1B[32m#{branch_names[0]}\x1B[0m"
        puts "\x1B[33m/brightpearl-db  \x1B[0m is on: \x1B[32m#{branch_names[1]}\x1B[0m"
        puts
        puts "Use \x1B[33mbrightpearl checkout\x1B[0m \x1B[32m[branch-name]\x1B[0m to switch both repos to the same branch."
        puts
        exit
    end
    branch_names[0]
end