Class: BrightpearlCommand::GitCheckout

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

Instance Method Summary collapse

Instance Method Details

#checkout_branchObject

Checks out a branch for both /code & /db.

Returns:

  • void



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
# File 'lib/routes/git_checkout.rb', line 99

def checkout_branch
    branch_to_checkout = @args[0]
    branch_data = @git.branch_data(branch_to_checkout)
    # If branch is already checked out.
    if branch_data[0][:"#{Brightpearl::Git::BRANCH_IS_CURRENT}"] && branch_data[1][:"#{Brightpearl::Git::BRANCH_IS_CURRENT}"]
        Brightpearl::Terminal::info("You are already on branch #{Brightpearl::Terminal::format_branch(branch_to_checkout)}")
        if @opts[:update] || @opts[:updatePush]
            Brightpearl::Terminal::info("To update the branch, run: #{Brightpearl::Terminal::format_command('bp g u')}", nil, false)
        end
        @git.check_for_stash
        exit
    end
    # If branch doesn't exist.
    if branch_data[0][:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false && branch_data[1][:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false
        Brightpearl::Terminal::error("The branch you're trying to checkout #{Brightpearl::Terminal::format_branch(branch_to_checkout)} doesn't exist", 'Please check your spelling and try again.', true)
    end
    @git.check_for_uncommitted_files
    count = 0
    branch_data.each do |branch_data_inner|
        repo_dir = branch_data_inner[:"#{Brightpearl::Git::BRANCH_LOCATION}"]
        if branch_data_inner[:"#{Brightpearl::Git::BRANCH_EXISTS}"] == false
            next
        end
        commands = []
        if @opts[:update] || @opts[:updatePush]
            commands << "git checkout #{Brightpearl::Git::MASTER}"
            commands << 'git pull'
        end
        unless branch_data_inner[:"#{Brightpearl::Git::BRANCH_IS_CURRENT}"]
            commands << "git checkout #{branch_to_checkout}"
        end
        if @opts[:update] || @opts[:updatePush]
            if branch_data[count][:"#{Brightpearl::Git::BRANCH_HAS_UPSTREAM}"]
                commands << 'git pull'
            else
                @git.ask_to_setup_remote_tracking(@git.current_branch_for_repo(repo_dir), repo_dir)
            end
            commands << "git merge #{Brightpearl::Git::MASTER}"
        end
        if @opts[:updatePush]
            commands << 'git push'
        end
        Brightpearl::Terminal::command(commands, repo_dir)
        count = count + 1
    end
    @git.check_for_same_branch
    @git.check_for_stash
end

#create_branch(push_to_origin = false) ⇒ Object

Creates a branch on both the ‘code’ & ‘db’ repos.

Returns:

  • void



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/routes/git_checkout.rb', line 44

def create_branch(push_to_origin = false)

    new_branch_name = @args[0]
    source_branch_name = @args[1].nil? ? Brightpearl::Git::MASTER : @args[1]

    # Validate that new branch doesn't already exist.
    @git.check_branch_does_not_exist_anywhere(new_branch_name)

    # Validate that source branch (if not MASTER) exists locally.
    if source_branch_name != Brightpearl::Git::MASTER
        @git.check_branch_exists_somewhere(source_branch_name)
    end

    @git.check_for_uncommitted_files
    if Brightpearl::Terminal::prompt_yes_no('Create new branch?', "You're about to cut branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m from #{Brightpearl::Terminal::format_branch(source_branch_name)}\x1B[38;5;240m#{push_to_origin ? ' and push it to origin.' : ''}")
        source_branch_final = []
        source_branch_data = @git.branch_data(source_branch_name)
        source_branch_data.each do |branch_data|
            if branch_data[:"#{Brightpearl::Git::BRANCH_EXISTS}"]
                sb = source_branch_name
                source_branch_final << source_branch_name
            else
                sb = Brightpearl::Git::MASTER
                source_branch_final << Brightpearl::Git::MASTER
                if source_branch_name != Brightpearl::Git::MASTER
                    Brightpearl::Terminal::warning('Branch not found', ["The repo: #{Brightpearl::Terminal::format_directory(branch_data[:"#{Brightpearl::Git::BRANCH_LOCATION}"])}\x1B[38;5;240m doesn't have have a branch called #{Brightpearl::Terminal::format_branch(source_branch_name)}", "Because of this, branch for this repo will be cut from #{Brightpearl::Terminal::format_branch(Brightpearl::Git::MASTER)}\x1B[38;5;240m instead."])
                end
            end
            commands = [
                "git checkout #{sb}"
            ]
            if branch_data[:"#{Brightpearl::Git::BRANCH_HAS_UPSTREAM}"]
                commands << 'git pull'
            end
            commands << "git checkout -b #{new_branch_name}"
            if push_to_origin
                commands << "git push --set-upstream origin #{new_branch_name}"
            end
            Brightpearl::Terminal::command(commands, branch_data[:"#{Brightpearl::Git::BRANCH_LOCATION}"])
        end

        if source_branch_final[0] == source_branch_final[1]
            source_branch_final = source_branch_final[0]
        else
            source_branch_final = "#{source_branch_final[0]}/#{source_branch_final[1]}"
        end
        Brightpearl::Terminal::success('Branch created', "Branch #{Brightpearl::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m has been successfully cut from #{Brightpearl::Terminal::format_branch(source_branch_final)}")
    else
        Brightpearl::Terminal::abort(nil, nil, true, false)
    end

end

#executeObject



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

def execute

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

    opts_validate
    opts_routing

end

#opts_routingObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/routes/git_checkout.rb', line 30

def opts_routing

    if @opts[:branch]
        create_branch(false)
    elsif @opts[:branch_origin]
        create_branch(true)
    else
        checkout_branch
    end

end

#opts_validateObject



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

def opts_validate

    unless @args.any?
        if @opts[:branch] || @opts[:branch_origin]
            Brightpearl::Terminal::error('You must specify a branch name')
        else
            puts "\n\x1B[41m ERROR \x1B[0m You must specify a branch to checkout. Here is a list of your \x1B[35mLOCAL BRANCHES\x1B[0m"
            Brightpearl::Git.new.show_branches(Brightpearl::Git::SORT_REFNAME, Brightpearl::Git::LOCAL)
        end
        exit
    end

end