tg-patch.sh: allow git diff-tree options to be passed through
[topgit/pro.git] / tg-patch.sh
blobf82f70901acb9b82dfc62b586a01e3f9bd46880b
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 head_from=
8 binary=
10 ## Parse options
12 while [ -n "$1" ]; do
13 arg="$1"
14 case "$arg" in
15 --)
16 case "$2" in
17 -*)
18 shift; break;;
20 break;;
21 esac;;
22 -|-h|--help)
23 echo "Usage: ${tgname:-tg} [...] patch [-i | -w] [--binary] [<name>] [--] [<git-diff-tree-option>...]" >&2
24 exit 1;;
25 -i|-w)
26 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
27 head_from="$arg";;
28 --binary)
29 binary=1;;
30 -?*)
31 if test="$(verify_topgit_branch "$arg" -f)"; then
32 [ -z "$name" ] || die "name already specified ($name)"
33 name="$arg"
34 else
35 break
36 fi;;
38 [ -z "$name" ] || die "name already specified ($name)"
39 name="$arg";;
40 esac
41 shift
42 done
44 quotearg() {
45 printf '%s' "$1" | sed 's/\(['\''!]\)/'\'\\\\\\1\''/g'
48 head="$(git symbolic-ref HEAD)"
49 head="${head#refs/heads/}"
51 [ -n "$name" ] ||
52 name="${head:-HEAD}"
53 name="$(verify_topgit_branch "$name")"
54 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
55 die "not a TopGit-controlled branch"
57 if [ -n "$head_from" ] && [ "$name" != "$head" ]; then
58 die "$head_from makes only sense for the current branch"
63 USE_PAGER_TYPE=diff
64 setup_pager
65 # We now collect the rest of the code in this file into a block
66 # so we can redirect the output to the pager.
69 # put out the commit message
70 # and put an empty line out, if the last one in the message was not an empty line
71 # and put out "---" if the commit message does not have one yet
72 cat_file "$name:.topmsg" $head_from |
73 awk '
74 /^---/ {
75 has_3dash=1;
78 need_empty = 1;
79 if ($0 == "")
80 need_empty = 0;
81 print;
83 END {
84 if (need_empty)
85 print "";
86 if (!has_3dash)
87 print "---";
91 b_tree=$(pretty_tree "$name" -b)
92 t_tree=$(pretty_tree "$name" $head_from)
94 if [ $b_tree = $t_tree ]; then
95 echo "No changes."
96 else
97 hasdd=
98 for a; do
99 [ "$a" != "--" ] || { hasdd=1; break; }
100 done
101 if [ -z "$hasdd" ]; then
102 git diff-tree -p --stat --summary ${binary:+--binary} "$@" $b_tree $t_tree
103 else
104 cmd="git diff-tree -p --stat --summary ${binary:+--binary}"
105 while [ $# -gt 0 -a "$1" != "--" ]; do
106 cmd="$cmd '$(quotearg "$1")'"
107 shift
108 done
109 cmd="$cmd '$(quotearg "$b_tree")' '$(quotearg "$t_tree")'"
110 while [ $# -gt 0 ]; do
111 cmd="$cmd '$(quotearg "$1")'"
112 shift
113 done
114 eval "$cmd"
118 echo '-- '
119 echo "$tgname: ($base_rev..) $name (depends on: $(cat_file "$name:.topdeps" $head_from 2>/dev/null | paste -s -d ' ' -))"
120 branch_contains "$name" "$base_rev" ||
121 echo "$tgname: The patch is out-of-date wrt. the base! Run \`$tgdisplay update\`."
123 } | eval "$TG_PAGER"
124 # ... and then we pipe all the output through the pager
126 # vim:noet