*: Move die() into .local/bin/helpers.sh
The function `die` was redundantly implemented in various files. Move the function into .local/bin/helpers.sh and source that where previously implemented. Also prepend the program's name to the message and always terminate the message with a newline. The newline was previously needed for a small but unnecessary hack that prevented the need of the `[ -z "$1" ]` test.
This commit is contained in:
@@ -8,14 +8,12 @@
|
|||||||
## format of zshs extended history.
|
## format of zshs extended history.
|
||||||
## An automatic backup is created before deleting that can be used for recovery.
|
## An automatic backup is created before deleting that can be used for recovery.
|
||||||
|
|
||||||
die() {
|
# Source die()
|
||||||
printf "$1" >&2
|
. "$HOME"/.local/bin/helpers.sh
|
||||||
exit ${2:-1}
|
|
||||||
}
|
|
||||||
|
|
||||||
[[ $# -eq 1 ]] || die "Specify a history file.\n"
|
[[ $# -eq 1 ]] || die "Specify a history file."
|
||||||
[[ -e "$1" ]] || die "File does not exist.\n"
|
[[ -e "$1" ]] || die "File does not exist."
|
||||||
[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off.\n"
|
[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off."
|
||||||
|
|
||||||
# Take a history file per stdin and sort the commands per number of occurrences
|
# Take a history file per stdin and sort the commands per number of occurrences
|
||||||
hist-sort() {
|
hist-sort() {
|
||||||
|
|||||||
13
.local/bin/helpers.sh
Executable file
13
.local/bin/helpers.sh
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# A collection of useful shell functions. This file should be sourced, rather
|
||||||
|
# than executed.
|
||||||
|
|
||||||
|
|
||||||
|
# Print error message, prepended by the programs name, and then exit
|
||||||
|
#
|
||||||
|
# Usage: die [<MESSAGE>] [<EXIT_CODE>]
|
||||||
|
die() {
|
||||||
|
[ -z "$1" ] || >&2 printf "%s: %s\n" "$0" "$1"
|
||||||
|
exit ${2:-1}
|
||||||
|
}
|
||||||
@@ -1,18 +1,16 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
die() {
|
# Source die()
|
||||||
printf "$1" >&2
|
. "$HOME"/.local/bin/helpers.sh
|
||||||
exit ${2:-1}
|
|
||||||
}
|
|
||||||
|
|
||||||
remote="$(git remote -v | grep -m1 "druckdev/dotfiles" | cut -f1)"
|
remote="$(git remote -v | grep -m1 "druckdev/dotfiles" | cut -f1)"
|
||||||
: "${remote:=origin}"
|
: "${remote:=origin}"
|
||||||
url="$(git remote get-url "$remote")"
|
url="$(git remote get-url "$remote")"
|
||||||
! printf "$url" | grep -q '^git@' || die "Using ssh already.\n" 0
|
! printf "$url" | grep -q '^git@' || die "Using ssh already." 0
|
||||||
|
|
||||||
host="$(printf "$url" | cut -d/ -f3)"
|
host="$(printf "$url" | cut -d/ -f3)"
|
||||||
grep -q "$host" "$HOME"/.ssh/known_hosts || die "No ssh key for $host found.\n"
|
grep -q "$host" "$HOME"/.ssh/known_hosts || die "No ssh key for $host found."
|
||||||
|
|
||||||
ssh_url="$(printf "$url" | sed 's_^https\?://_git@_;s_/_:_')"
|
ssh_url="$(printf "$url" | sed 's_^https\?://_git@_;s_/_:_')"
|
||||||
git remote set-url "$remote" "$ssh_url"
|
git remote set-url "$remote" "$ssh_url"
|
||||||
die "Set $remote remote url to $ssh_url\n" $?
|
die "Set $remote remote url to $ssh_url" $?
|
||||||
|
|||||||
@@ -9,10 +9,8 @@
|
|||||||
#
|
#
|
||||||
# To enable this hook, save this file in ".git/hooks/commit-msg".
|
# To enable this hook, save this file in ".git/hooks/commit-msg".
|
||||||
|
|
||||||
die() {
|
# Source die()
|
||||||
printf "$1" >&2
|
. "$HOME"/.local/bin/helpers.sh
|
||||||
exit ${2:-1}
|
|
||||||
}
|
|
||||||
|
|
||||||
subject="$(head -1 "$1")"
|
subject="$(head -1 "$1")"
|
||||||
# Ignore "fixup! " and "squash! " prefix' added by `git-commit`
|
# Ignore "fixup! " and "squash! " prefix' added by `git-commit`
|
||||||
@@ -39,7 +37,7 @@ body="$(
|
|||||||
/^[^${git_comment_char/auto/\#}]/p"
|
/^[^${git_comment_char/auto/\#}]/p"
|
||||||
)"
|
)"
|
||||||
|
|
||||||
[[ ${#subject} -le 50 ]] || die "Subject too long. (<= 50)\n"
|
[[ ${#subject} -le 50 ]] || die "Subject too long. (<= 50)"
|
||||||
|
|
||||||
# The subject line has to match "${pats[@]}", but to be more verbose different
|
# The subject line has to match "${pats[@]}", but to be more verbose different
|
||||||
# error messages are printed for the different 'levels' of the pattern.
|
# error messages are printed for the different 'levels' of the pattern.
|
||||||
@@ -54,7 +52,7 @@ msg=(
|
|||||||
"Start subject with a capital letter.\n"
|
"Start subject with a capital letter.\n"
|
||||||
"Remove punctuation mark from end.\n"
|
"Remove punctuation mark from end.\n"
|
||||||
)
|
)
|
||||||
[[ ${#msg[@]} -ge ${#pats[@]} ]] || die "Something went wrong internally.\n"
|
[[ ${#msg[@]} -ge ${#pats[@]} ]] || die "Something went wrong internally."
|
||||||
for ((i = 0; i < ${#pats[@]}; i++)); do
|
for ((i = 0; i < ${#pats[@]}; i++)); do
|
||||||
if ! grep -qE "$(printf "%s" "${pats[@]:0:$i+1}")" <<<"$subject"; then
|
if ! grep -qE "$(printf "%s" "${pats[@]:0:$i+1}")" <<<"$subject"; then
|
||||||
die "${msg[$i]}"
|
die "${msg[$i]}"
|
||||||
@@ -65,6 +63,6 @@ BKP_IFS="$IFS"
|
|||||||
IFS='
|
IFS='
|
||||||
'
|
'
|
||||||
for line in $body; do
|
for line in $body; do
|
||||||
[[ ${#line} -le 72 ]] || die "Body lines too long. (<= 72)\n"
|
[[ ${#line} -le 72 ]] || die "Body lines too long. (<= 72)"
|
||||||
done
|
done
|
||||||
IFS="$BKP_IFS"
|
IFS="$BKP_IFS"
|
||||||
|
|||||||
@@ -6,10 +6,8 @@
|
|||||||
#
|
#
|
||||||
# To enable this hook, save this file in ".git/hooks/pre-commit".
|
# To enable this hook, save this file in ".git/hooks/pre-commit".
|
||||||
|
|
||||||
die() {
|
# Source die()
|
||||||
printf "$1" >&2
|
. "$HOME"/.local/bin/helpers.sh
|
||||||
exit ${2:-1}
|
|
||||||
}
|
|
||||||
|
|
||||||
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
||||||
against=HEAD
|
against=HEAD
|
||||||
@@ -35,8 +33,7 @@ if [ "$allownonascii" != "true" ]; then
|
|||||||
| wc -c
|
| wc -c
|
||||||
)
|
)
|
||||||
if [ $num_nonascii != 0 ]; then
|
if [ $num_nonascii != 0 ]; then
|
||||||
printf "Rename files to only include ASCII characters.\n" >&2
|
die "Rename files with ASCII characters only, or enable hooks.allownonascii"
|
||||||
die "Or set hooks.allownonascii to true.\n"
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user