aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xgit-format-named-patches.sh28
1 files changed, 28 insertions, 0 deletions
diff --git a/git-format-named-patches.sh b/git-format-named-patches.sh
index a5e4f8b..4cdcb4e 100755
--- a/git-format-named-patches.sh
+++ b/git-format-named-patches.sh
@@ -2,18 +2,46 @@
set -eu
+SUBDIR=""
+CLEAN=1
+
fail() {
printf "%s\n" "$1" >>/dev/stderr
exit 32
}
+while getopts "cd:" opt; do
+ case $opt in
+ c)
+ CLEAN=0
+ ;;
+ d)
+ SUBDIR="$(readlink -f "${OPTARG}")"
+ ;;
+ \?)
+ echo "Invalid option: -$OPTARG"
+ exit 1
+ ;;
+ :)
+ echo "Option -$OPTARG requires an argument."
+ exit 1
+ ;;
+ esac
+done
+
+# Shift past the last option parsed by getopts
+shift $((OPTIND-1))
+
[ "$#" -lt 1 ] && exit 64
+[ $CLEAN -eq 1 ] && [ -n "$SUBDIR" ] && rm -fv "$SUBDIR/*.patch"
+
IDSstr="$(git log --format="%H" "$1" 2>/dev/null)"
mapfile -t IDS <<< "$IDSstr"
for id in "${IDS[@]}" ; do
PATCH="$(git format-patch -1 --stdout "${id}")"
FN="$(echo "$PATCH" | git interpret-trailers --parse | awk '/^Patch: / {print $2}')"
[ -z "$FN" ] && fail "Patch filename not defined"
+ [ -n "$SUBDIR" ] && FN="$(readlink -f "$SUBDIR"/"$FN")"
printf "%s" "$PATCH" > "$FN"
done