commit c802160087c864f79ed74042e73625672b6fe1f1
parent 501d63e5c35f5e4f266dae41204796bc8313d4c8
Author: William Casarin <jb55@jb55.com>
Date: Sat, 19 Aug 2023 10:01:59 -0700
mail-prpatch-insert
Diffstat:
1 file changed, 66 insertions(+), 0 deletions(-)
diff --git a/bin/mail-prpatch-insert b/bin/mail-prpatch-insert
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+
+set -e
+
+function new_msgid() {
+ echo "<$(sha256sum "$file" | awk '{ print $1 }')-$(date +%s)>"
+}
+
+TO="jb55@jb55.com"
+PRPATCH="$HOME/var/prpatch"
+MBOX="$PRPATCH/out.mbox"
+PATCHES="$PRPATCH/out"
+
+rm -rf "$PRPATCH"
+mkdir -p "$PATCHES"
+mail="$(cat -)"
+prlink="$(mail-prlink <<<"$mail")"
+#threadid=$(<<<"$mail" grep -i '^message-id:' | cut -d: -f2-)
+pr="$(<<<"$prlink" sed -En 's,.*github.com/([^/]+)/([^/]+)/pull/([0-9]+).*,\3,p')"
+target="$(<<<"$prlink" sed -En 's,.*github.com/([^/]+)/([^/]+)/pull/([0-9]+).*,\2,p')"
+replyto="$(grep -i '^reply-to:' <<<"$mail" | cut -d":" -f2- | tr -d '\n' || :)"
+echo "$mail" > "$PRPATCH/mail"
+<<<"$mail" mail-prpatch > "$MBOX"
+
+#sed -i"" '/^Subject:/ {N; s/\n / /}' "$MBOX"
+
+git mailsplit -o"$PATCHES" "$MBOX"
+
+file="$PATCHES"/0001
+
+msgid=$(new_msgid)
+threadid=$msgid
+for file in "$PATCHES/"*
+do
+ # mailsplit introduces linebreaks from some reason
+ awk '
+ /^Subject:/ { subj = 1; printf "%s", $0; next }
+ subj && !/^$/ { printf " %s", $0; next }
+ /^$/ && subj { subj = 0; print ""; next }
+ { print }
+ ' < "$file" > "$file".2
+ mv "$file".2 "$file"
+
+ if [ -n "$replyto" ]; then
+ sed -i"" "1 a Cc: $replyto" "$file"
+ fi
+
+ if [ -n "$prlink" ]; then
+ sed -i"" "0,/^---/{s@^---@\nCloses: $prlink\n---@}" "$file"
+ fi
+
+ if [ -n "$target" ]; then
+ sed -i"" "s,\[PATCH,\[PATCH $target," "$file"
+ fi
+
+ sed -i"" "1 a To: $TO" "$file"
+ sed -i"" "1 a Message-ID: $msgid" "$file"
+
+ # build thread
+ sed -i"" "1 a In-Reply-To: $threadid" "$file"
+ sed -i"" "1 a References: $threadid" "$file"
+
+ notmuch insert +inbox < "$file"
+
+ msgid=$(new_msgid)
+done