From 4d8141504db51a8f416148b164882ea3b39f751b Mon Sep 17 00:00:00 2001 From: "G.raud" Date: Sat, 28 Apr 2018 13:23:38 +0200 Subject: [PATCH] archives2git: warn(), fail(): new func to factorize --- archives2git | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/archives2git b/archives2git index 3a825fb..0f6a581 100755 --- a/archives2git +++ b/archives2git @@ -124,6 +124,8 @@ get_version () { _version=${_version#"-"}; _version=${_version#"."}; _version=${_version#"_"} printf "%s\n" "${_version}" } +warn () { echo >&2 "$@"; } +fail () { _rc=$1; shift; warn "$PROGRAM_NAME:" "$@"; exit $_rc; } strip_extension () { printf "%s\n" "$1" | LC_ALL=C sed -r \ -e '$s;^([^/]*/)*([^/]{1,})/{0,1}$;\2;' \ @@ -224,8 +226,7 @@ do echo "$PROGRAM_NAME version $PROGRAM_VERSION" exit ;; --?*) - echo >&2 "$PROGRAM_NAME: internal error: invalid option $1" - exit 255 ;; + fail 255 "internal error: invalid option $1" ;; --) shift; break ;; *) @@ -236,24 +237,15 @@ done # setup [ -z "$KEEP_TEMPDIR" ] && trap 'rm -rf "$TMPDIR"' EXIT INT QUIT TERM TMPDIR=$(mktemp -d) || -{ - echo >&2 "$PROGRAM_NAME: error: cannot create a temporary directory" - exit 253 -} +fail 253 "error: cannot create a temporary directory" WRKDIR=$(pwd) # git repository check cd "$GIT_WORK_TREE" && { test -d .git || git rev-parse --git-dir >/dev/null; } || -{ - echo >&2 "$PROGRAM_NAME: error: not in a git repository ($(pwd))" - exit 255 -} +fail 255 "error: not in a git repository ($(pwd))" test -z "$(git status --porcelain)" && git update-index -q --refresh || -{ - echo >&2 "$PROGRAM_NAME: error: unstaged files or dirty index" - exit 254 -} +fail 254 "error: unstaged files or dirty index" # main set -e @@ -294,7 +286,7 @@ do then # remove conflicting file (previously kept or renamed to the same name) rm -R ./"$name" - echo >&2 "WARNING: conflicting file removed: \`$name'" + warn "Warning: conflicting file removed: \`$name'" fi mv "$archdir"/"$file" ./"$name" IFS=$NL; set -f -- 2.11.4.GIT