Class: BrightpearlCommand::GitUpdate

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

Instance Method Summary collapse

Instance Method Details

#executeObject



5
6
7
8
9
10
# File 'lib/routes/git_update.rb', line 5

def execute
    @git = Brightpearl::Git.new
    @opts = command_options
    opts_validate
    opts_routing
end

#opts_routingObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/routes/git_update.rb', line 15

def opts_routing

    if @opts[:all]

        Brightpearl::Terminal::error('Note yet implemented', "Must write code to update ALL branches.\nCould get complicated as there will likely be merge conflicts.\nMust think about this...", true)

        # update_branch_all
    else
        update_branch_single
    end

end

#opts_validateObject



12
13
# File 'lib/routes/git_update.rb', line 12

def opts_validate
end

#update_branch_singleObject

Updates the current branch.

Returns:

  • void



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/routes/git_update.rb', line 30

def update_branch_single
    @git.check_for_uncommitted_files(true)
    @git.repo_loop.each do |repo_dir|
        current_branch = @git.current_branch_for_repo(repo_dir)
        commands = Array.new
        if current_branch != Brightpearl::Git::MASTER
            commands << 'git checkout master'
        end
        commands << 'git pull'
        if current_branch != Brightpearl::Git::MASTER
            commands << "git checkout #{current_branch}"
            commands << 'git pull'
            commands << 'git merge master'
        end
        results = Brightpearl::Terminal::command(commands, repo_dir)
        if current_branch != Brightpearl::Git::MASTER
            if results[3] == false
                @git.ask_to_setup_remote_tracking(current_branch, repo_dir)
            end
            if results[4] == false
                Brightpearl::Terminal::error('Merge conflict occurred', ["Unable to successfully merge #{Brightpearl::Terminal::format_branch(Brightpearl::Git::MASTER)} into #{Brightpearl::Terminal::format_branch(current_branch)} on #{Brightpearl::Terminal::format_directory(@git.get_repo_shorthand(repo_dir))}", "Please #{Brightpearl::Terminal::format_action('open an IDE')}\x1B[38;5;240m and resolve your conflicts as soon as this script has finished executing."])
            end
        end
        if @opts[:push] && current_branch != Brightpearl::Git::MASTER
            Brightpearl::Terminal::command("git push origin #{current_branch}", repo_dir)
        elsif @opts[:push] && current_branch == Brightpearl::Git::MASTER
            Brightpearl::Terminal::warning("#{Brightpearl::Terminal::format_action('push')} to #{Brightpearl::Terminal::format_branch(Brightpearl::Git::MASTER)} not allowed!", ["Your #{Brightpearl::Terminal::format_directory(@git.get_repo_shorthand(repo_dir))} repo is currently on #{Brightpearl::Terminal::format_branch(Brightpearl::Git::MASTER)}","The script will not perform a #{Brightpearl::Terminal::format_action('push')} as requested for safety reasons."])
        end

    end
    @git.check_for_stash(true)
end