*: 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.
|
||||
## An automatic backup is created before deleting that can be used for recovery.
|
||||
|
||||
die() {
|
||||
printf "$1" >&2
|
||||
exit ${2:-1}
|
||||
}
|
||||
# Source die()
|
||||
. "$HOME"/.local/bin/helpers.sh
|
||||
|
||||
[[ $# -eq 1 ]] || die "Specify a history file.\n"
|
||||
[[ -e "$1" ]] || die "File does not exist.\n"
|
||||
[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off.\n"
|
||||
[[ $# -eq 1 ]] || die "Specify a history file."
|
||||
[[ -e "$1" ]] || die "File does not exist."
|
||||
[[ "$(stat -c '%a' "$1")" = "600" ]] || die "File permissions are off."
|
||||
|
||||
# Take a history file per stdin and sort the commands per number of occurrences
|
||||
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
|
||||
|
||||
die() {
|
||||
printf "$1" >&2
|
||||
exit ${2:-1}
|
||||
}
|
||||
# Source die()
|
||||
. "$HOME"/.local/bin/helpers.sh
|
||||
|
||||
remote="$(git remote -v | grep -m1 "druckdev/dotfiles" | cut -f1)"
|
||||
: "${remote:=origin}"
|
||||
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)"
|
||||
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_/_:_')"
|
||||
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".
|
||||
|
||||
die() {
|
||||
printf "$1" >&2
|
||||
exit ${2:-1}
|
||||
}
|
||||
# Source die()
|
||||
. "$HOME"/.local/bin/helpers.sh
|
||||
|
||||
subject="$(head -1 "$1")"
|
||||
# Ignore "fixup! " and "squash! " prefix' added by `git-commit`
|
||||
@@ -39,7 +37,7 @@ body="$(
|
||||
/^[^${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
|
||||
# error messages are printed for the different 'levels' of the pattern.
|
||||
@@ -54,7 +52,7 @@ msg=(
|
||||
"Start subject with a capital letter.\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
|
||||
if ! grep -qE "$(printf "%s" "${pats[@]:0:$i+1}")" <<<"$subject"; then
|
||||
die "${msg[$i]}"
|
||||
@@ -65,6 +63,6 @@ BKP_IFS="$IFS"
|
||||
IFS='
|
||||
'
|
||||
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
|
||||
IFS="$BKP_IFS"
|
||||
|
||||
@@ -6,10 +6,8 @@
|
||||
#
|
||||
# To enable this hook, save this file in ".git/hooks/pre-commit".
|
||||
|
||||
die() {
|
||||
printf "$1" >&2
|
||||
exit ${2:-1}
|
||||
}
|
||||
# Source die()
|
||||
. "$HOME"/.local/bin/helpers.sh
|
||||
|
||||
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
||||
against=HEAD
|
||||
@@ -35,8 +33,7 @@ if [ "$allownonascii" != "true" ]; then
|
||||
| wc -c
|
||||
)
|
||||
if [ $num_nonascii != 0 ]; then
|
||||
printf "Rename files to only include ASCII characters.\n" >&2
|
||||
die "Or set hooks.allownonascii to true.\n"
|
||||
die "Rename files with ASCII characters only, or enable hooks.allownonascii"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user