/
hotfix
executable file
·228 lines (177 loc) · 6.35 KB
/
hotfix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
#!/usr/bin/env ruby
require_relative '../lib/signal_handlers.rb'
require_relative '../lib/github.rb'
require_relative '../lib/git.rb'
require_relative '../lib/helpers.rb'
require_relative '../lib/plugins.rb'
command=ARGV.first
$0 = ARGV.join(" ")
unless command == '--help' or command == '-h'
unless Git::in_a_repo
die("\nSwitch to a git repo. If you need help, use --help or -h.")
end
end
case command
when 'start'
require_argument(:hotfix, :start)
hotfix = hotfix_branch(ARGV[1])
stable_branch = Git::get_branch('stable')
Plugins.invoke :before_start, :hotfix, hotfix
Git::run_safe([
"git checkout #{stable_branch}",
"git fetch",
"git rebase --preserve-merges origin/#{stable_branch}",
"git branch \"#{hotfix}\" #{stable_branch}",
"git checkout \"#{hotfix}\""
])
Git::submodules_update
# Automatically setup remote tracking branch
Git::run_safe([
"git config branch.#{hotfix}.remote origin",
"git config branch.#{hotfix}.merge refs/heads/#{hotfix}",
"git config branch.#{hotfix}.rebase true"
])
when 'url'
require_argument(:hotfix, :url, min=1, max=2)
hotfix = ARGV[1] || Git::current_branch
url = Github::get_url(hotfix)
if url
puts url
else
abort "There is no pull request available for #{hotfix}.\n" +
'You can make one by using `hotfix finish`.'
end
when 'switch'
require_argument(:hotfix, :switch, min=2, max=5)
hotfix = current_hotfix_branch
Git::switch_branch(hotfix)
optional_pull
when 'finish'
hotfix = current_hotfix_branch
if !is_hotfix_branch hotfix
abort "This is a feature branch. Please use feature-finish."
end
# Push commits to origin
Git::run_safe(["git push origin #{hotfix}:#{hotfix}"])
exit 1 unless confirm("Create a pull-request for hotfix branch named: '#{hotfix}' ?")
octokit = Github::api
Plugins.invoke :before_finish, :hotfix, hotfix
description = Github::get_pull_request_description(hotfix)
puts "Pull-request description:"
puts description[:title]
puts "#"
puts description[:body]
response = octokit.create_pull_request(
Github::get_github_repo,
Git::get_branch('stable'),
hotfix,
description[:title],
description[:body]
)
Plugins.invoke :after_finish, :hotfix, hotfix, response
puts "Successfully created pull-request ##{response[:number]}"
puts " " + response[:html_url]
when 'finish-issue'
require_argument(:hotfix, :'finish-issue')
issue = ARGV[1]
hotfix = Git::current_branch
if !is_hotfix_branch hotfix
abort "This is a feature branch. Please use feature-finish-issue."
end
# Push commits to origin
Git::run_safe(["git push origin #{hotfix}:#{hotfix}"])
exit 1 unless confirm("Convert issue ##{issue} into a pull-request using " +
"hotfix branch named '#{hotfix}' ?")
octokit = Github::api
pull = octokit.create_pull_request_for_issue(
Github::get_github_repo,
Git::get_branch('stable'),
hotfix,
issue
)
# We've converted the issue to a pull request, now lets change the
# description to include the last commit message and prompt the user to
# confirm it.
last_commit_message = Git::commit_message(hotfix)
original_title = pull[:title].gsub("\r","")
original_body = pull[:body].gsub("\r","")
initial_message = <<-MESSAGE
#{last_commit_message}
----
Original issue: #{original_title}
----
#{original_body}
MESSAGE
description = Github::open_title_body_editor(initial_message)
updated_pull = octokit.update_pull_request(
Github::get_github_repo,
issue,
description[:title],
description[:body]
)
puts "Successfully converted issue ##{issue} to a pull-request"
puts " " + updated_pull[:html_url]
when 'merge'
fail_on_local_changes
dev_branch = Git::get_branch('development')
stable_branch = Git::get_branch('stable')
hotfix = current_hotfix_branch
if !is_hotfix_branch hotfix
exit 1 unless confirm("It looks like this is a feature branch. Are you sure you want to merge it into #{stable_branch}?")
end
Git::run_safe(["git fetch"])
pull_info = Github::get_pull_request_info_from_api(hotfix, stable_branch)
warning = Github::get_commit_status_warning(pull_info[:status])
unless warning.empty?
puts highlight(warning)
end
Plugins.invoke :before_merge, :hotfix, hotfix
exit 1 unless confirm("Merge hotfix named: '#{hotfix}' ?")
commit_message = Git::get_description_from_user(pull_info[:description])
commit_message_dev = commit_message.sub(stable_branch, dev_branch)
update = Git::submodules_update("get")
Git::run_safe([
# Checkout the branch to make sure we have it locally.
"git checkout #{hotfix.shellescape}",
"git rebase --preserve-merges origin/#{hotfix.shellescape}",
# Merge into stable.
"git checkout #{stable_branch}",
# Pull the latest changes and rebase the unpushed commits if any.
"git rebase --preserve-merges origin/#{stable_branch}",
# Merge the hotfix branch into stable.
"git merge --no-ff --no-edit -m #{commit_message.shellescape} \"#{hotfix}\"",
"#{update}",
# Merge into master.
"git checkout #{dev_branch}",
# Pull the latest changes and rebase the unpushed master commits if any.
"git rebase --preserve-merges origin/#{dev_branch}",
# Merge the hotfix branch into master.
"git merge --no-ff --no-edit -m #{commit_message_dev.shellescape} \"#{hotfix}\"",
# Init any submodules in the master branch. Note: no need to change.
# Directories before calling git submodule since we are already in the
# projects top-level directory.
"#{update}",
# Delete the local hotfix branch.
"git branch -d #{hotfix.shellescape}",
# Checkout stable branch.
"git checkout #{stable_branch}"
])
Plugins.invoke :after_merge, :hotfix, hotfix
puts "Successfully merged hotfix branch: #{hotfix} into #{stable_branch} and #{dev_branch}"
puts "If you are satisfied with the result, do this:\n" + <<CMDS
git push
git checkout #{dev_branch}
git push
CMDS
when 'list'
options = {
:hotfix => Git::hotfix_branches(:unmerged)
}
if ARGV.include?('-v')
options[:merged] = Git::hotfix_branches(:merged)
end
Git.show_branch_list(options)
else
display_hotfix_help
end
log_command("hotfix #{$0}")