-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-rflow
executable file
·412 lines (345 loc) · 10.3 KB
/
git-rflow
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
229
230
231
232
233
234
235
236
237
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
#!/usr/bin/env bash
# https://github.com/thewoolleyman/gitrflow
# Copyright (c) 2014 Chad Woolley - The MIT License
###
### Bash setup
###
# http://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
function handle_gitrflow_bash_xtrace() {
gitrflow_bash_xtrace=${GITRFLOW_BASH_XTRACE:-false}
if [[ ${gitrflow_bash_xtrace} == 'true' ]]; then
export PS4="+(\${BASH_SOURCE}:\${LINENO}): \${FUNCNAME[0]:+\${FUNCNAME[0]}(): }"
set -o xtrace
fi
}
handle_gitrflow_bash_xtrace
gitrflow_bash_verbose=${GITRFLOW_BASH_VERBOSE:-false}
if [[ ${gitrflow_bash_verbose} == 'true' ]]; then
set -o verbose
fi
set -o errexit # AKA -e - exit immediately on errors (http://mywiki.wooledge.org/BashFAQ/105)
set -o errtrace # AKA -E - any trap on ERR is inherited by subshell
set -o noclobber # AKA -C - disallow '>' to overwrite file (see http://mywiki.wooledge.org/NoClobber)
set -o nounset # AKA -u - guard against unused variables (see http://mywiki.wooledge.org/BashFAQ/035)
set -o pipefail # fail when pipelines contain an error (see http://www.gnu.org/software/bash/manual/html_node/Pipelines.html)
_log_prefix="[gitrflow] ${BASH_SOURCE}:"
function onexit() {
local exit_status=${1:-$?}
if [[ ${exit_status} != 0 ]]; then
_error_line="error trapped."
else
_error_line=''
fi
if [[ $(type -t onexit_hook) = 'function' ]]; then
onexit_hook
fi
echo "$_log_prefix $_error_line Exiting $0 with exit status $exit_status"
exit "${exit_status}"
}
function disable_error_checking() {
trap - ERR
set +o errexit
}
function enable_error_checking() {
trap onexit ERR
set -o errexit
}
trap onexit HUP INT QUIT TERM ERR
###
### Git Processing
###
function exec_git() {
unset git_output # reset output from any previous git command
git_command="${*}"
git_output=$(${git_command} 2>&1)
git_command_exit_status=$?
if ${print_git_commands}; then
echo "${git_command}"
fi
if [[ -n ${git_output} && ${print_git_output} == true ]]; then
echo "${git_output}" # print git output if option is set
git_output_to_print=true
fi
if [[ ! ${git_command_exit_status} == 0 ]]; then
return "${git_command_exit_status}"
fi
}
###
### Common Utility Functions
###
function fail_unless_local_repo_is_clean_and_unmodified() {
fail_unless_repo_clean
fail_if_repo_gone
exec_git "git rev-parse --abbrev-ref HEAD"
starting_branch=${git_output}
current_branch=${starting_branch}
fetch_current_branch
exec_git 'git status --porcelain --branch'
git_status_output=${git_output}
fail_if_unpushed_changes
}
function fail_unless_repo_clean() {
exec_git 'git status --porcelain'
if [[ -n ${git_output} ]]; then
print_error_and_exit 'ERROR: Local repo is not clean. Please fix and retry.'
fi
}
function fail_if_repo_gone() {
exec_git 'git status --porcelain --branch'
if [[ ${git_output} == *'[gone]'* ]]; then
print_error_and_exit 'ERROR: Local repo is "gone". Please fix and retry.'
fi
}
function fetch_current_branch() {
exec_git "git fetch -q ${remote} ${current_branch}"
}
function fail_if_unpushed_changes() {
if [[ ${git_status_output} =~ \[(ahead) ]]; then
print_error_and_exit 'ERROR: Local repo has unpushed changes. Please fix and retry.'
fi
}
function fail_if_unpulled_changes() {
if [[ ${git_status_output} =~ \[(behind) ]]; then
print_error_and_exit "ERROR: Local repo is behind remote. Please run 'git rflow update' to pull remote updates, then retry."
fi
}
###
### Command Processing
###
function run_command() {
local current_branch
local starting_branch
local git_command
local git_output
local git_status_output
if ${do_update}; then
run_update_command
else
run_branch_command
fi
}
###
### Update Command Processing
###
function run_update_command() {
fail_unless_local_repo_is_clean_and_unmodified
if [[ ${git_status_output} =~ \[(behind) ]]; then
printf "Rebasing local branch '%s' onto remote branch '%s/%s'...\n" "${current_branch}" "${remote}" "${current_branch}"
exec_git "git rebase ${remote}/${current_branch}"
printf "Rebase complete! Local branch '%s' is now up-to-date.\n" "${current_branch}"
else
printf "Up to date! No changes to pull from remote branch '%s/%s'.\n" "${remote}" "${current_branch}"
fi
}
###
### Branch Command Processing
###
function run_branch_command() {
# declare variables with local scope to this function and all subfunctions
local prefixed_branch_name
fail_unless_local_repo_is_clean_and_unmodified
fail_if_unpulled_changes
prefix="${branch_type}_prefix" # e.g. 'feature_prefix
# bash indirect parameter expansion: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
prefixed_branch_name="${!prefix}${branch_name}" # e.g. 'feat/mybranch'
"${branch_type}_${branch_command}" # Invoke function, e.g. 'feature_start'
}
###
### Feature Branch Command Processing
###
function feature_start() {
if [[ ! ${current_branch} == 'master' ]]; then
print_error_and_exit 'ERROR: Local branch is not master. Currently, git-rflow only supports feature branches created directly off of master.'
fi
exec_git "git checkout -b ${prefixed_branch_name}"
exec_git "git push ${remote} ${prefixed_branch_name}"
if ${git_output_to_print}; then
printf "\n"
fi
printf "Summary of actions:\n"
printf "%s A new Feature branch '%s' was created, based on 'master'\n" \
'-' "${prefixed_branch_name}"
printf "%s It is pushed to the remote '%s', with a tracking branch of 'origin/%s'\n" \
'-' "${remote}" "${prefixed_branch_name}"
printf "%s You are now on branch '%s'\n\n" '-' "${prefixed_branch_name}"
printf "Now, start committing on your feature. When done, use:\n\n"
printf " git flow feature finish %s\n" "${branch_name}"
}
###
### Config handling
###
function load_config_or_fail() {
local config_loaded='true'
load_config
if [[ ! ${config_loaded} == 'true' ]]; then
print_error_and_exit \
'ERROR: Not a gitrflow-enabled repo yet. Please run "git rflow init [--defaults]" first.'
fi
}
function load_config() {
load_prefixes
}
function load_prefixes() {
# Note: disable shellcheck SC2034 unused variable warning, because the [branch_type]_prefix
# variables are used in run_branch_command() via bash indirect parameter expansion
if ! exec_git 'git config --get gitrflow.prefix.feature'; then
config_loaded='false'
else
# shellcheck disable=SC2034
feature_prefix="${git_output}"
fi
}
###
### Init handling
###
function run_init_command() {
prefix='feat/'
if ${do_init_use_defaults}; then
printf "Using default branch prefixes:\n"
printf " Feature branches: '%s'\n" ${prefix}
else
printf "What prefix will you use for feature branches? [%s]\n" ${prefix}
read input
if [[ ! ${input} == '' ]]; then
prefix=${input}
fi
fi
exec_git "git config gitrflow.prefix.feature ${prefix}"
}
###
### Option handling
###
function print_version() {
echo 'git-rflow, version 0.0.1'
}
function print_usage_and_exit() {
printf "Usage:\n"
printf " gitrflow [global options] init [--defaults]\n"
printf " gitrflow [global options] update\n"
printf " gitrflow [global options] <branch type> <command> [command options]\n"
printf "\n"
printf "'init' command options:\n"
printf " --defaults Use defaults for init config without prompting\n"
printf "\n"
printf "Branch Types:\n"
printf " feature\n"
printf "\n"
printf "'feature' branch type commands and options:\n"
printf " feature start <branch_name>\n"
printf "\n"
printf "Global options:\n"
printf " -c, --print-git-commands Print git [c]ommands as they are run\n"
printf " -d, --debug Debug git-rflow script with bash xtrace\n"
printf " -h, --help Display this [h]elp\n"
printf " -o, --print-git-output Print [o]utput from git commands\n"
printf " -V, --version Display the program [v]ersion\n"
printf " -- Ignore all following options\n"
printf "\nSee https://github.com/thewoolleyman/gitrflow for more information.\n"
exit 1
}
function print_error_and_exit() {
echo "${1}" >&2
printf "'git-rflow --help' for usage.\n"
exit 1
}
function parse_options() {
set +o nounset
# see http://stackoverflow.com/a/13864829/25192
if [[ -z "${1+x}" ]]; then
print_usage_and_exit
fi
if [[ "${1}" == '--' ]]; then
print_usage_and_exit
fi
# flags
local do_init=false
local do_init_use_defaults=false
local do_update=false
local git_output_to_print=false
local print_git_commands=false
local print_git_output=false
local remote='origin'
# config
local feature_prefix
# from http://mywiki.wooledge.org/BashFAQ/035
while :; do
case ${1} in
init)
do_init=true
shift
if [[ "${1}" == '--defaults' ]]; then
do_init_use_defaults=true
shift
fi
continue
;;
update)
do_update=true
shift
continue
;;
feature) # Takes an option arguments, ensuring they have been specified.
local branch_type=${1}
if [[ "${2}" ]]; then
local branch_command=$2
if [[ "${3}" ]]; then
local branch_name=$3
shift 3
continue
else
print_error_and_exit 'ERROR: The feature branch name is required.'
fi
else
print_error_and_exit 'ERROR: The feature branch command is required.'
fi
;;
-c|--print-git-commands)
print_git_commands=true
;;
-d|--debug)
GITRFLOW_BASH_XTRACE='true'
handle_gitrflow_bash_xtrace
;;
-h|--help)
print_usage_and_exit
;;
-o|--print-git-output)
print_git_output=true
;;
-V|--version)
print_version
exit 0
;;
--) # ignore all following options
shift
break
;;
*)
# see http://stackoverflow.com/a/13864829/25192
if [[ -z "${1+x}" ]]; then
break
else
print_error_and_exit "ERROR: Unrecognized parameter '${1}'"
fi
esac
shift
done
set -o nounset
if ${do_init}; then
run_init_command
exit 0
fi
load_config_or_fail
run_command
}
function invoke() {
set +o nounset
parse_options "${@}"
set -o nounset
}
###
### invoke script
###
set +o nounset
invoke "${@}"
set -o nounset