citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

mail-prpatch-insert (2020B)


      1 #!/usr/bin/env bash
      2 
      3 set -e
      4 
      5 function new_msgid() {
      6 	echo "<$(sha256sum "$1" | awk '{ print $1 }')-$(date +%s)>"
      7 }
      8 
      9 TO="jb55@jb55.com"
     10 PRPATCH="$HOME/var/prpatch"
     11 MBOX="$PRPATCH/out.mbox"
     12 PATCHES="$PRPATCH/out"
     13 
     14 function build_coverletter() {
     15 	<"$1" awk '/^$/ {exit} {print}' > "$PRPATCH"/header
     16 	<"$PRPATCH/mail" awk 'BEGIN {print_it=0;} skip > 0 {skip -= 1; next;} /^----==_mimepart/ {print_it=!print_it;skip=3;next;} print_it==1 {print;}' |
     17 		cat "$PRPATCH/header" - |
     18 		sed -E 's,(PATCH[^1]+)1,\10,' > "$PATCHES/0000"
     19 }
     20 
     21 rm -rf "$PRPATCH"
     22 mkdir -p "$PATCHES"
     23 mail="$(cat -)"
     24 prlink="$(mail-prlink <<<"$mail")"
     25 #threadid=$(<<<"$mail" grep -i '^message-id:' | cut -d: -f2-)
     26 pr="$(<<<"$prlink" sed -En 's,.*github.com/([^/]+)/([^/]+)/pull/([0-9]+).*,\3,p')"
     27 target="$(<<<"$prlink" sed -En 's,.*github.com/([^/]+)/([^/]+)/pull/([0-9]+).*,\2,p')"
     28 replyto="$(grep -i '^reply-to:' <<<"$mail" | cut -d":" -f2- | tr -d '\n' || :)"
     29 echo "$mail" > "$PRPATCH/mail"
     30 <<<"$mail" mail-prpatch > "$MBOX"
     31 
     32 # pull out the cover letter
     33 
     34 git mailsplit -o"$PATCHES" "$MBOX"
     35 
     36 $(build_coverletter "$PATCHES/0001")
     37 
     38 file="$PATCHES"/0000
     39 msgid=$(new_msgid "$file")
     40 threadid=$msgid
     41 for file in "$PATCHES/"*
     42 do
     43 	# mailsplit introduces linebreaks from some reason
     44 	awk '
     45 	  /^Subject:/ { subj = 1; printf "%s", $0; next }
     46 	  subj && !/^$/ { printf " %s", $0; next }
     47 	  /^$/ && subj { subj = 0; print ""; next }
     48 	  { print }
     49 	' < "$file" > "$file".2
     50 	mv "$file".2 "$file"
     51 
     52 	if [ -n "$replyto" ]; then
     53 		sed -i"" "1 a Cc: $replyto" "$file" 
     54 	fi
     55 
     56 	if [ -n "$prlink" ]; then
     57 		sed -i"" "0,/^---/{s@^---@\nCloses: $prlink\n---@}" "$file" 
     58 	fi
     59 
     60 	if [ -n "$target" ]; then
     61 		sed -i"" "s,\[PATCH,\[PATCH $target," "$file"
     62 	fi
     63 
     64 	if [[ "$prlink" =~ "damus-io" ]]; then
     65 		TO="patches@damus.io"
     66 	fi
     67 
     68 	sed -i"" "1 a To: $TO" "$file" 
     69 	sed -i"" "1 a Message-ID: $msgid" "$file" 
     70 
     71 	# build thread
     72 	sed -i"" "1 a In-Reply-To: $threadid" "$file" 
     73 	sed -i"" "1 a References: $threadid" "$file" 
     74 
     75 	notmuch insert +inbox < "$file"
     76 
     77 	msgid=$(new_msgid "$file")
     78 done