-
Notifications
You must be signed in to change notification settings - Fork 53
/
cask-repair
executable file
·654 lines (528 loc) · 22 KB
/
cask-repair
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#!/bin/bash
readonly program="$(basename "${0}")"
declare -rx MACOS_VERSION='11' # Latest macOS version, so commands like `fetch` are not dependent on the contributor’s OS
readonly submit_pr_to='homebrew:master'
readonly caskroom_origin_remote_regex='(https://|(ssh://)?git@)github.com[/:]Homebrew/homebrew-cask'
readonly caskroom_taps=(cask cask-versions cask-fonts cask-drivers)
readonly caskroom_taps_dir="$(brew --repository)/Library/Taps/homebrew"
readonly user_agent=(--user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10) https://brew.sh')
readonly hub_config="${HOME}/.config/hub"
readonly github_username="${GITHUB_USER:-$(awk '/user:/{print $(NF)}' "${hub_config}" 2>/dev/null | head -1)}"
readonly cask_repair_remote_name="${github_username}"
readonly cask_repair_branch_prefix='cask_repair_update'
readonly submission_error_log="$(mktemp)"
show_home='false' # By default, do not open the cask's homepage
show_appcast='false' # By default, do not open the cask's appcast
warning_messages=()
has_errors=''
function color_message {
local color="${1}"
local message="${2}"
local -r all_colors=('black' 'red' 'green' 'yellow' 'blue' 'magenta' 'cyan' 'white')
for i in "${!all_colors[@]}"; do
if [[ "${all_colors[${i}]}" == "${color}" ]]; then
local -r color_index="${i}"
echo -e "$(tput setaf "${i}")${message}$(tput sgr0)"
break
fi
done
if [[ -z "${color_index}" ]]; then
echo "${FUNCNAME[0]}: '${color}' is not a valid color."
exit 1
fi
}
function failure_message {
color_message 'red' "${1}" >&2
}
function success_message {
color_message 'green' "${1}"
}
function warning_message {
color_message 'yellow' "${1}"
}
function push_failure_message {
warning_message 'There were errors while pushing:'
echo "${1}"
abort 'Please fix the errors and try again. If the issue persists, open a bug report on the repo for this script (https://github.com/vitorgalvao/tiny-scripts).'
}
function require_hub {
if ! hash 'hub' 2> /dev/null; then
warning_message '`hub` was not found. Installing it…'
brew install hub
fi
if [[ -z "${github_username}" ]] || [[ -z "${GITHUB_TOKEN}" && ! $(grep 'oauth_token:' "${hub_config}" 2>/dev/null) ]]; then
abort '`hub` is not configured.\nTo do it, run `(cd $(brew --repository) && hub issue)`. Your Github password will be required, but is never stored.'
fi
}
function usage {
echo "
Usage:
${program} [options] <cask_name>
Options:
-o, --open-home Open the homepage for the given cask.
-a, --open-appcast Open the appcast for the given cask.
-v, --cask-version Give a version directly, instead of being prompted for it.
-s, --cask-sha Give a sha256 directly, skipping the download fetch.
-u, --cask-url Give a URL directly, instead of being prompted for it.
-e, --edit-cask Opens cask for editing before trying first download.
-c <number>, --closes-issue <number> Adds 'Closes #<number>.' to the pull request.
-m <message>, --message <message> Adds '<message>' to the pull request.
-r, --reword Open commit message editor before committing.
-b, --blind-submit Submit cask without asking for confirmation, if there are no errors.
-f, --fail-on-error If there are any errors with the submission, abort.
-w, --fail-on-warning If there are any warnings or errors with the submission, abort.
-i, --install-cask Installs your updated cask after submission.
-d, --delete-branches Deletes all local and remote branches named like ${cask_repair_branch_prefix}-<word>.
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
function current_origin {
git remote get-url origin
}
function current_tap {
basename "$(current_origin)" '.git'
}
function ensure_caskroom_repos {
local current_caskroom_taps
current_caskroom_taps=($(HOMEBREW_NO_AUTO_UPDATE=1 brew tap | grep '^homebrew/cask' | sed 's|^homebrew/|homebrew-|'))
for repo in "${caskroom_taps[@]}"; do
if grep --silent "${repo}" <<< "${current_caskroom_taps[@]}"; then
continue
else
warning_message "\`homebrew/${repo}\` not tapped. Tapping…"
HOMEBREW_NO_AUTO_UPDATE=1 brew tap "homebrew/${repo}"
fi
done
}
function cd_to_cask_tap {
local cask_file cask_file_location
cask_file="${1}"
cask_file_location="$(find "${caskroom_taps_dir}" -path "*/Casks/${cask_file}")"
[[ -z "${cask_file_location}" ]] && abort "No such cask was found in any official repo (${cask_name})."
cd "$(dirname "${cask_file_location}")" || abort "Failed to change to directory of ${cask_file}."
}
function require_correct_origin {
local origin_remote
origin_remote="$(current_origin)"
grep --silent --ignore-case --extended-regexp "^${caskroom_origin_remote_regex}" <<< "${origin_remote}" || abort "\`origin\` is pointing to an incorrect remote (${origin_remote}). Its beginning must match ${caskroom_origin_remote_regex}."
}
function ensure_cask_repair_remote {
if ! git remote | grep --silent "${cask_repair_remote_name}"; then
warning_message "A \`${cask_repair_remote_name}\` remote does not exist. Creating it now…"
hub fork
fi
}
function http_status_code {
local url follow_redirects
url="${1}"
[[ "${2}" == 'follow_redirects' ]] && follow_redirects='--location' || follow_redirects='--no-location'
curl --silent --head "${follow_redirects}" "${user_agent[@]}" --write-out '%{http_code}' "${url}" --output '/dev/null'
}
function has_interpolation {
[[ "${1}" =~ \#{version.*} ]]
}
function is_version_latest {
local cask_file="${1}"
[[ "$(brew cask _stanza version "${cask_file}")" == 'latest' ]]
}
function has_block_url {
local cask_file="${1}"
grep --silent 'url do' "${cask_file}"
}
function has_language_stanza {
local cask_file="${1}"
brew cask _stanza language "${cask_file}" 2>/dev/null
}
function modify_stanza {
local stanza_to_modify new_stanza_value cask_file stanza_match_regex last_stanza_match stanza_start ending_comma
stanza_to_modify="${1}"
new_stanza_value="${2}"
cask_file="${3}"
stanza_match_regex="^\s*${stanza_to_modify} "
last_stanza_match="$(grep "${stanza_match_regex}" "${cask_file}" | tail -1)"
stanza_start="$(/usr/bin/perl -pe "s/(${stanza_match_regex}).*/\1/" <<< "${last_stanza_match}")"
if grep --quiet ',$' <<< "${last_stanza_match}"; then
ending_comma=','
fi
/usr/bin/perl -0777 -i -e'
$last_stanza_match = shift(@ARGV);
$stanza_start = shift(@ARGV);
$new_stanza_value = shift(@ARGV);
$ending_comma = shift(@ARGV);
print <> =~ s|\Q$last_stanza_match\E|$stanza_start$new_stanza_value$ending_comma|r;
' "${last_stanza_match}" "${stanza_start}" "${new_stanza_value}" "${ending_comma}" "${cask_file}"
}
function modify_url {
local url cask_file
url="${1}"
cask_file="${2}"
modify_stanza 'url' "\"${url}\"" "${cask_file}"
}
function appcast_url {
local cask_file="${1}"
brew cask _stanza appcast "${cask_file}"
}
function has_appcast {
local cask_file="${1}"
[[ -n "$(appcast_url "${cask_file}" 2>/dev/null)" ]]
}
function sha_change {
local package_sha cask_file given_sha
cask_file="${1}"
given_sha="${2}"
# If there is no checksum despite the cask being versioned, assume it is on purpose
if [[ "$(brew cask _stanza sha256 "${cask_file}")" == ':no_check' ]] && ! is_version_latest "${cask_file}"; then
return
fi
# If a sha256 was given, use that instead of fetching
if [[ -n "${given_sha}" ]]; then
modify_stanza 'sha256' "\"${given_sha}\"" "${cask_file}"
return
fi
# Set sha256 as :no_check temporarily, to prevent mismatch errors when fetching
modify_stanza 'sha256' ':no_check' "${cask_file}"
if ! brew fetch --force "${cask_file}"; then
clean
abort "There was an error fetching ${cask_file}. Please check your connection and try again."
fi
package_sha="$(HOMEBREW_NO_COLOR=1 brew fetch "${cask_file}" 2>/dev/null | tail -1 | sed 's/SHA256: //')"
modify_stanza 'sha256' "\"${package_sha}\"" "${cask_file}"
}
function delete_created_branches {
local local_branches remote_branches
for dir in "${caskroom_taps_dir}/homebrew-cask"*; do
cd "${dir}" || abort "Failed to delete branches. ${dir} does not exist."
if git remote | grep --silent "${cask_repair_remote_name}"; then # Proceed only if the correct remote exists
# Delete local branches
local_branches=$(git branch --all | grep --extended-regexp "^ *${cask_repair_branch_prefix}-.+$" | /usr/bin/perl -pe 's|^ *||;s|\n| |')
[[ -n "${local_branches}" ]] && git branch -D ${local_branches}
# Delete remote branches
git fetch --prune "${cask_repair_remote_name}"
remote_branches=$(git branch --all | grep --extended-regexp "remotes/${cask_repair_remote_name}/${cask_repair_branch_prefix}-.+$" | /usr/bin/perl -pe 's|.*/||;s|\n| |')
[[ -n "${remote_branches}" ]] && git push "${cask_repair_remote_name}" --delete ${remote_branches}
fi
cd ..
done
}
function edit_cask {
local cask_file found_editor
cask_file="${1}"
echo 'Opening cask in default editor. If it is a GUI editor, you will need to completely quit it (⌘Q) before the script can continue.'
for text_editor in {"${HOMEBREW_EDITOR}","${EDITOR}","${GIT_EDITOR}"}; do
if [[ -n "${text_editor}" ]]; then
eval "${text_editor}" "${cask_file}"
found_editor='true'
break
fi
done
[[ -n "${found_editor}" ]] || open -W "${cask_file}"
}
function add_warning {
local message severity color
severity="${1}"
message="$(sed '/./,$!d' <<< "${2}")" # Remove leading blank lines, so audit errors related to ruby still show
if [[ "${severity}" == 'warning' ]]; then
color="$(tput setaf 3)•$(tput sgr0)"
else
color="$(tput setaf 1)•$(tput sgr0)"
has_errors='true'
fi
warning_messages+=("${color} ${message}")
}
function show_warnings {
if [[ "${#warning_messages[@]}" -gt 0 ]]; then
printf '%s\n' "${warning_messages[@]}" >&2
divide
fi
}
function clear_warnings {
warning_messages=()
unset has_errors
}
function lock {
local lock_file action
readonly lock_file='/tmp/cask-repair.lock'
readonly action="${1}"
if [[ "${action}" == 'create' ]]; then
touch "${lock_file}"
elif [[ "${action}" == 'exists?' ]]; then
[[ -f "${lock_file}" ]] && return 0 || return 1
elif [[ "${action}" == 'remove' ]]; then
[[ -f "${lock_file}" ]] && rm "${lock_file}"
fi
}
function clean {
local current_branch
lock 'remove'
[[ "$(dirname "$(dirname "${PWD}")")" == "${caskroom_taps_dir}" ]] || return # Do not try to clean if not in a tap dir (e.g. if script was manually aborted too fast)
# current_branch="$(git branch --show-current)" # Only available from git 2.22 (newer than the deault in Mojave)
current_branch="$(git rev-parse --abbrev-ref HEAD)"
git reset HEAD --hard --quiet
git checkout master --quiet
git branch -D "${current_branch}" --quiet
[[ -f "${submission_error_log}" ]] && rm "${submission_error_log}"
unset given_cask_version given_cask_url cask_updated
}
function skip {
clean
echo -e "${1}"
}
function abort {
clean
failure_message "\n${1}\n"
exit 1
}
trap 'abort "You aborted."' SIGINT
function divide {
command -v 'hr' &>/dev/null && hr - || echo '--------------------'
}
# Options
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-o | --open-home)
show_home='true'
;;
-a | --open-appcast)
show_appcast='true'
;;
-v | --cask-version)
given_cask_version="${2}"
shift
;;
-s | --cask-sha)
given_cask_sha="${2}"
shift
;;
-u | --cask-url)
given_cask_url="${2}"
shift
;;
-e | --edit-cask)
edit_on_start='true'
;;
-c | --closes-issue)
issue_to_close="${2}"
shift
;;
-m | --message)
extra_message="${2}"
shift
;;
-r | --reword)
reword_commit='true'
;;
-b | --blind-submit)
updated_blindly='true'
;;
-f | --fail-on-error)
abort_on_error='true'
;;
-w | --fail-on-warning)
abort_on_error='true'
abort_on_warning='true'
;;
-i | --install-cask)
install_now='true'
;;
-d | --delete-branches)
can_run_without_arguments='true'
delete_created_branches='true'
;;
--)
shift
args+=("${@}")
break
;;
-*)
echo "Unrecognised option: ${1}"
exit 1
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
# DEPRECATION MESSAGE
warning_message '`cask-repair` is deprecated. I will accept PRs to fix bugs, but spending more time on it is difficult to justify. I recommend using `brew bump-cask-pr`. It doesn’t do as much in niche cases, but it does more in other slightly more relevant cases. It covers the vast majority of bump needs better. It ships with Homebrew so there’s nothing to install. Do `brew bump-cask-pr --help` to see how to use it.'
# Exit if no argument or more than one argument was given
if [[ -z "${1}" && "${can_run_without_arguments}" != 'true' ]]; then
usage
exit 1
fi
if [[ "${delete_created_branches}" == 'true' ]]; then
delete_created_branches
exit 0
fi
# Only allow one instance at a time
if lock 'exists?'; then
# We want this to be different from abort, so as to not remove the lock file
failure_message "Only one ${program} instance can be run at once."
exit 1
else
lock 'create'
fi
require_hub
ensure_caskroom_repos
if [[ -z "${HOMEBREW_NO_AUTO_UPDATE}" ]]; then
brew update
echo -n 'Updating taps… '
else
warning_message "You have set 'HOMEBREW_NO_AUTO_UPDATE'. If ${program} fails, unset it and retry your command before submitting a bug report."
fi
for cask in "${@}"; do
# Clean the cask's name, and check if it is valid
cask_name="${cask%.rb}" # Remove '.rb' extension, if present
cask_file="./${cask_name}.rb"
cask_branch="${cask_repair_branch_prefix}-${cask_name}"
cd_to_cask_tap "${cask_name}.rb"
require_correct_origin
ensure_cask_repair_remote
has_language_stanza "${cask_file}" && abort "${cask_name} has a language stanza. It cannot be updated via this script. Try update_multilangual_casks: https://github.com/Homebrew/homebrew-cask/blob/master/developer/bin/update_multilangual_casks"
git rev-parse --verify "${cask_branch}" &>/dev/null && git checkout "${cask_branch}" master --quiet || git checkout -b "${cask_branch}" master --quiet # Create branch or checkout if it already exists
# Open home and appcast
[[ "${show_home}" == 'true' ]] && brew home "${cask_file}"
if has_appcast "${cask_file}"; then
cask_appcast_url="$(appcast_url "${cask_file}")"
if [[ "${show_appcast}" == 'true' ]]; then
[[ "${cask_appcast_url}" =~ ^https://github.com.*releases.atom$ ]] && open "${cask_appcast_url%.atom}" || open "${cask_appcast_url}" # if appcast is from github releases, open the page instead of the feed
fi
fi
# Show cask's current state
divide
cat "${cask_file}"
divide
# Save old cask version
old_cask_version="$(brew cask _stanza version "${cask_file}")"
# Set cask version
if [[ -z "${given_cask_version}" ]]; then
read -rp $'Type the new version (or leave blank to use current one, or use `s` to skip)\n> ' given_cask_version # Ask for cask version, if not given previously
if [[ "${given_cask_version}" == 's' ]]; then
skip 'Skipping…'
continue
fi
[[ -z "${given_cask_version}" ]] && given_cask_version="${old_cask_version}"
fi
if [[ "${given_cask_version}" == ':latest' || "${given_cask_version}" == 'latest' ]]; then # Allow both ':latest' and 'latest' to be given
modify_stanza 'version' ':latest' "${cask_file}"
else
modify_stanza 'version' "\"${given_cask_version}\"" "${cask_file}"
fi
if [[ -n "${given_cask_url}" ]]; then
if has_block_url "${cask_file}"; then
warning_message 'Cask has block url, so it can only be modified manually (choose `[e]dit` when prompted).'
else
modify_url "${given_cask_url}" "${cask_file}"
fi
else
# If url does not use interpolation, is not block, and not updating blindly, ask for it
cask_bare_url=$(grep "url ['\"].*['\"]" "${cask_file}" | sed -E "s|.*url ['\"](.*)['\"].*|\1|")
if ! has_interpolation "${cask_bare_url}" && ! has_block_url "${cask_file}" && [[ -z "${updated_blindly}" ]]; then
read -rp $'Paste the new URL (or leave blank to use the current one)\n> ' given_cask_url
[[ -n "${given_cask_url}" ]] && modify_url "${given_cask_url}" "${cask_file}"
fi
cask_url=$(brew cask _stanza url "${cask_file}")
# Check if the URL sends a 200 HTTP code, else abort
cask_url_status=$(http_status_code "${cask_url}" 'follow_redirects')
[[ "${cask_url}" =~ (github.com|bitbucket.org) ]] && cask_url_status='200' # If the download URL is from github or bitbucket, fake the status code
if [[ "${cask_url_status}" != '200' ]] && [[ -z "${updated_blindly}" ]]; then
[[ -z "${cask_url_status}" ]] && add_warning warning 'you need to use a valid URL' || add_warning warning "url is probably incorrect, as a non-200 (OK) HTTP response code was returned (${cask_url_status})"
fi
fi
[[ "${edit_on_start}" == 'true' ]] && edit_cask "${cask_file}"
if is_version_latest "${cask_file}"; then
modify_stanza 'sha256' ':no_check' "${cask_file}"
else
sha_change "${cask_file}" "${given_cask_sha}"
fi
# Check if everything is alright, else abort
[[ -z "${cask_updated}" ]] && cask_updated='false'
until [[ "${cask_updated}" =~ ^[yne]$ ]]; do
# fix style errors and check for style and audit errors
style_message=$(brew style --fix "${cask_file}" 2>/dev/null)
style_result="${?}"
[[ "${style_result}" -ne 0 ]] && add_warning error "${style_message}"
audit_message=$(brew audit "${cask_file}" 2>/dev/null)
audit_result="${?}"
[[ "${audit_result}" -ne 0 ]] && add_warning error "${audit_message}"
git --no-pager diff
divide
show_warnings
[[ -n "${abort_on_error}" && "${has_errors}" == 'true' ]] && abort 'The submission has errors and you elected to abort on those cases.'
[[ -n "${abort_on_warning}" && "${#warning_messages[@]}" -gt 0 ]] && abort 'The submission has warnings and you elected to abort on those cases.'
if [[ -n "${updated_blindly}" && "${#warning_messages[@]}" -eq 0 ]]; then
cask_updated='y'
else
read -rn1 -p 'Is everything correct? ([y]es / [n]o / [e]dit) ' cask_updated
echo # Add an empty line
fi
if [[ "${cask_updated}" == 'y' ]]; then
if [[ "${style_result}" -ne 0 || "${audit_result}" -ne 0 ]]; then
cask_updated='false'
else
break
fi
elif [[ "${cask_updated}" == 'e' ]]; then
edit_cask "${cask_file}"
if ! is_version_latest "${cask_file}"; then # Recheck sha256 values if version isn't :latest
sha_change "${cask_file}"
fi
cask_updated='false'
clear_warnings
elif [[ "${cask_updated}" == 'n' ]]; then
abort 'You decided to abort.'
fi
done
# Skip if no changes were made, submit otherwise
if git diff-index --quiet HEAD --; then
skip 'No changes made to the cask. Skipping…'
continue
else
echo 'Submitting…'
fi
# Grab version as it ended up in the cask
cask_version="$(brew cask _stanza version "${cask_file}")"
# Commit, push, submit pull request, clean
[[ "${old_cask_version}" == "${cask_version}" ]] && commit_message="Update ${cask_name}" || commit_message="Update ${cask_name} from ${old_cask_version} to ${cask_version}"
if [[ -n "${reword_commit}" ]]; then
git commit "${cask_file}" --message "${commit_message}" --edit --quiet
commit_message="$(git log --format=%B -n 1 HEAD | head -n 1)"
else
git commit "${cask_file}" --message "${commit_message}" --quiet
fi
pr_message="${commit_message}\n\n**Important:** *Do not tick a checkbox if you haven’t performed its action.* Honesty is indispensable for a smooth review process.\n\nAfter making all changes to a cask, verify:\n\n- [ ] The submission is for [a stable version](https://github.com/Homebrew/homebrew-cask/blob/master/doc/development/adding_a_cask.md#stable-versions) or [documented exception](https://github.com/Homebrew/homebrew-cask/blob/master/doc/development/adding_a_cask.md#but-there-is-no-stable-version).\n- [x] \`brew audit --cask {{cask_file}}\` is error-free.\n- [x] \`brew style --fix {{cask_file}}\` reports no offenses."
[[ -n "${issue_to_close}" ]] && pr_message+="\n\nCloses #${issue_to_close}."
[[ -n "${extra_message}" ]] && pr_message+="\n\n${extra_message}"
submit_pr_from="${github_username}:${cask_branch}"
git push --force "${cask_repair_remote_name}" "${cask_branch}" --quiet 2> "${submission_error_log}"
if [[ "${?}" -ne 0 ]]; then
# Fix common push errors
if grep --quiet 'shallow update not allowed' "${submission_error_log}"; then
echo 'Push failed due to shallow repo. Unshallowing…'
HOMEBREW_NO_AUTO_UPDATE=1 brew tap --full "homebrew/$(current_tap)"
git push --force "${cask_repair_remote_name}" "${cask_branch}" --quiet 2> "${submission_error_log}"
[[ "${?}" -ne 0 ]] && push_failure_message "$(< "${submission_error_log}")"
else
push_failure_message "$(< "${submission_error_log}")"
fi
fi
pr_link=$(hub pull-request -b "${submit_pr_to}" -h "${submit_pr_from}" -m "$(echo -e "${pr_message}")")
if [[ -n "${pr_link}" ]]; then
if [[ -n "${install_now}" ]]; then
success_message 'Updating cask locally…'
brew reinstall "${cask_file}"
else
echo -e "\nYou can upgrade the cask right now from your personal branch:\n brew reinstall https://raw.githubusercontent.com/${github_username}/$(current_tap)/${cask_branch}/Casks/${cask_name}.rb"
fi
clean
success_message "\nSubmitted (${pr_link})\n"
else
abort 'There was an error submitting the pull request. Please open a bug report on the repo for this script (https://github.com/vitorgalvao/tiny-scripts).'
fi
done