#!/bin/bash temp_message=`mktemp` temp_patch=`mktemp` cleanup () { rm -f "$temp_message" "$temp_patch" } trap cleanup 0 handle_patch () { git mailinfo -b --scissors "$temp_message" "$temp_patch" | { read -r git_author read -r git_email read -r git_subject read -r git_datex printf 'From %s <%s>\n' \ "${git_author#Author: }" "${git_email#Email: }" printf '%s\n%s\n' "$git_subject" "$git_date" cat "$temp_message" echo # git mailinfo does not normalize the patch, and there are # typically indented patch statistics before the first diff. awk '/^diff / { printing=1 } /^[^- ]/ { if (printing) { print } else { print "error: unrecognized input before patch: " $0 > "/dev/stderr"; exit(1) } } /^[- ]/ { if (printing) { print } }' "$temp_patch" } } if test $# -eq 0 ; then handle_patch else for patch in "$@" ; do handle_patch < "$path" echo done fi