Class: AppCommand::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



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

def checkout_branch

    branch_to_checkout = @args[0]

    if branch_to_checkout =~ /\A\d{4,5}\z/i
        branch_to_checkout = @git.resolve_branch_for_jira(branch_to_checkout, true)
    end

    branch_data = @git.branch_data(branch_to_checkout)

    # If branch is already checked out.
    if branch_data[0][:"#{App::Git::BRANCH_IS_CURRENT}"] && branch_data[1][:"#{App::Git::BRANCH_IS_CURRENT}"]
        App::Terminal::info("You are already on branch #{App::Terminal::format_branch(branch_to_checkout)}")
        if @opts[:update] || @opts[:updatePush]
            App::Terminal::info("To update the branch, run: #{App::Terminal::format_command('bp g u')}", nil, false)
        end
        @git.check_for_stash
        exit
    end

    # If branch doesn't exist.
    if branch_data[0][:"#{App::Git::BRANCH_EXISTS}"] == false && branch_data[1][:"#{App::Git::BRANCH_EXISTS}"] == false
        App::Terminal::error("The branch you're trying to checkout #{App::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[:"#{App::Git::BRANCH_LOCATION}"]
        if branch_data_inner[:"#{App::Git::BRANCH_EXISTS}"] == false
            next
        end
        commands = []
        if @opts[:update] || @opts[:updatePush]
            commands << "git checkout #{App::Git::MASTER}"
            commands << 'git pull'
        end
        unless branch_data_inner[:"#{App::Git::BRANCH_IS_CURRENT}"]
            commands << "git checkout #{branch_to_checkout}"
        end
        App::Terminal::command(commands, repo_dir)
        if @opts[:update] || @opts[:updatePush]
            commands = [
                'git pull',
                "git merge #{App::Git::MASTER} --no-edit"
            ]
            results = App::Terminal::command(commands, repo_dir, true, false)
            @git.check_for_conflicts(results[1], repo_dir, "Unable to successfully merge #{App::Terminal::format_branch(App::Git::MASTER)} into #{App::Terminal::format_branch(branch_to_checkout)} on #{App::Terminal::format_directory(@git.get_repo_shorthand(repo_dir))}")
        end
        if @opts[:updatePush]
            commands =[
                'git push'
            ]
            App::Terminal::command(commands, repo_dir, true, false)
        end
        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



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

def create_branch(push_to_origin = false)

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

    # If new branch is 12345, add 'bp-' prefix.
    if new_branch_name =~ /\A\d{4,5}\z/i
        new_branch_name = "bp-#{new_branch_name}"
    end

    # 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 != App::Git::MASTER
        @git.check_branch_exists_somewhere(source_branch_name)
    end

    @git.check_for_uncommitted_files

    if App::Terminal::prompt_yes_no('Create new branch?', "You're about to cut branch #{App::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m from #{App::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[:"#{App::Git::BRANCH_EXISTS}"]
                sb = source_branch_name
                source_branch_final << source_branch_name
            else
                sb = App::Git::MASTER
                source_branch_final << App::Git::MASTER
                if source_branch_name != App::Git::MASTER
                    App::Terminal::warning('Branch not found', ["The repo: #{App::Terminal::format_directory(branch_data[:"#{App::Git::BRANCH_LOCATION}"])}\x1B[38;5;240m doesn't have have a branch called #{App::Terminal::format_branch(source_branch_name)}", "Because of this, branch for this repo will be cut from #{App::Terminal::format_branch(App::Git::MASTER)}\x1B[38;5;240m instead."])
                end
            end
            commands = [
                "git checkout #{sb}"
            ]
            if branch_data[:"#{App::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
            App::Terminal::command(commands, branch_data[:"#{App::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
        App::Terminal::success('Branch created', "Branch #{App::Terminal::format_branch(new_branch_name)}\x1B[38;5;240m has been successfully cut from #{App::Terminal::format_branch(source_branch_final)}")
    else
        App::Terminal::abort
    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 = App::Git.new

    opts_validate
    opts_routing

end

#opts_routingObject



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

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

def opts_validate

    unless @args.any?
        if @opts[:branch] || @opts[:branch_origin]
            App::Terminal::error('You must specify a branch name')
        else
            system('bp g co -h')
        end
        exit
    end

end