tg mail: new command for mailing patches
[topgit/lukasnellen.git] / tg-mail.sh
blobf3abd2c0285493e748fd9809e0cb921b985dccc9
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # GPLv2
5 name=
8 ## Parse options
10 while [ -n "$1" ]; do
11 arg="$1"; shift
12 case "$arg" in
13 -*)
14 echo "Usage: tg [...] mail [NAME]" >&2
15 exit 1;;
17 [ -z "$name" ] || die "name already specified ($name)"
18 name="$arg";;
19 esac
20 done
22 # TODO refactor me into something common?
23 [ -n "$name" ] || name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
24 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
25 die "not a TopGit-controlled branch"
28 patchfile="$(mktemp -t tg-mail.XXXXXX)"
30 $tg patch $name >"$patchfile"
32 hlines=$(grep -n -m 1 '^---' "$patchfile" | sed 's/:---//')
33 header=$(head -$(($hlines - 1)) "$patchfile")
37 from="$(echo "$header" | grep '^From:' | sed 's/From:\s*//')"
38 to="$(echo "$header" | grep '^To:' | sed 's/To:\s*//')"
41 # XXX I can't get quoting right without arrays
42 [ -n "$from" ] && from=(--from "$from")
43 [ -n "$to" ] && to=(--to "$to") # FIXME there could be multimple To
45 people=()
46 [ -n "$from" ] && people=("${people[@]}" "${from[@]}")
47 [ -n "$to" ] && people=("${people[@]}" "${to[@]}")
50 # NOTE git-send-email handles cc itself
51 git send-email "${people[@]}" "$patchfile"
53 rm "$patchfile"