Last active
October 24, 2018 14:47
-
-
Save legionus/3db7c7055835e93234101e07790ed82b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -eu | |
. shell-error | |
. shell-signal | |
PROG_VERSION=1.0 | |
show_help() | |
{ | |
cat <<-EOF | |
Usage: $PROG [options] (<mbox> | <Maildir>) | |
Options: | |
-s, --signoff Pass -s flag to git-am. | |
-k, --keep Pass -k flag to git-am. | |
-3, --3way, --no-3way Pass -3 flag to git-am. | |
-V, --version Print program version and exit; | |
-h, --help Show this text and exit. | |
Report bugs to http://bugzilla.altlinux.org/ | |
EOF | |
exit | |
} | |
print_version() | |
{ | |
cat <<-EOF | |
$PROG version $PROG_VERSION | |
Written by Alexey Gladkov <[email protected]> | |
Copyright (C) 20018 Alexey Gladkov <[email protected]> | |
This is free software; see the source for copying conditions. There is NO | |
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
EOF | |
exit | |
} | |
TEMP=`getopt -n $PROG -o '3,s,k,h,v,V' -l '3way,no-3way,signoff,keep,help,verbose,version' -- "$@"` || | |
show_usage | |
eval set -- "$TEMP" | |
git_am_args= | |
while :; do | |
case "$1" in | |
-s|--signoff) | |
git_am_args="$git_am_args \"$1\"" | |
;; | |
-k|--keep) | |
git_am_args="$git_am_args \"$1\"" | |
;; | |
-3|--3way|--no-3way) | |
git_am_args="$git_am_args \"$1\"" | |
;; | |
-h|--help) | |
show_help | |
;; | |
-v|--verbose) | |
verbose=-v | |
;; | |
-V|--version) | |
print_version | |
;; | |
--) shift; break | |
;; | |
esac | |
shift | |
done | |
[ "$#" -gt 0 ] || | |
fatal "more arguments required" | |
workdir= | |
exit_handler() | |
{ | |
[ -z "$workdir" ] || rm -rf -- "$workdir" | |
} | |
workdir="$(mktemp -dt "$PROG.XXXXXXXXX")" | |
set_cleanup_handler exit_handler | |
for mbox in "$@"; do | |
verbose "processing mailbox: $mbox" | |
mkdir -p -- "$workdir/patchset" | |
git mailsplit -o"$workdir/patchset" "$mbox" >/dev/null | |
for f in "$workdir/patchset"/*; do | |
[ -e "$f" ] || | |
break | |
git mailinfo -k /dev/null "$workdir/patch" < "$f" > "$workdir/header" | |
[ -s "$workdir/patch" ] || | |
continue | |
while read -r header data; do | |
if [ "$header" = 'Subject:' ]; then | |
printf '%s\t%s\n' "$f" "$data" | |
break | |
fi | |
done < "$workdir/header" | |
done > "$workdir/patchlist" | |
sort --field-separator=' ' -k2,2 -o "$workdir/patchlist" "$workdir/patchlist" | |
while IFS=' ' read -r f _; do | |
eval git am $git_am_args "$f" | |
done < "$workdir/patchlist" | |
rm -rf -- "$workdir/patchset" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment