#!/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" # sometimes subject lines have linebreaks!? 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 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