tg-revert.sh: convert any top-bases in TOPGIT REFS
[topgit/pro.git] / tg-patch.sh
blob0bc1a810ccdf844c494c3bfd3878154ee804ea06
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) Petr Baudis <pasky@suse.cz> 2008
4 # All rights reserved.
5 # GPLv2
7 name=
8 head_from=
9 binary=
11 ## Parse options
13 while [ -n "$1" ]; do
14 arg="$1"
15 case "$arg" in
16 --)
17 case "$2" in
18 -*)
19 shift; break;;
21 break;;
22 esac;;
23 -|-h|--help)
24 echo "Usage: ${tgname:-tg} [...] patch [-i | -w] [--binary] [<name>] [--] [<git-diff-tree-option>...]" >&2
25 exit 1;;
26 -i|-w)
27 [ -z "$head_from" ] || die "-i and -w are mutually exclusive"
28 head_from="$arg";;
29 --binary)
30 binary=1;;
31 -?*)
32 if test="$(verify_topgit_branch "$arg" -f)"; then
33 [ -z "$name" ] || die "name already specified ($name)"
34 name="$arg"
35 else
36 break
37 fi;;
39 [ -z "$name" ] || die "name already specified ($name)"
40 name="$arg";;
41 esac
42 shift
43 done
45 quotearg() {
46 printf '%s' "$1" | sed 's/\(['\''!]\)/'\'\\\\\\1\''/g'
49 head="$(git symbolic-ref -q HEAD || :)"
50 head="${head#refs/heads/}"
52 [ -n "$name" ] ||
53 name="${head:-HEAD}"
54 name="$(verify_topgit_branch "$name")"
55 base_rev="$(git rev-parse --short --verify "refs/$topbases/$name" -- 2>/dev/null)" ||
56 die "not a TopGit-controlled branch"
58 if [ -n "$head_from" ] && [ "$name" != "$head" ]; then
59 die "$head_from makes only sense for the current branch"
64 # We now collect the rest of the code in this file into a function
65 # so we can redirect the output to the pager.
66 output()
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 "refs/heads/$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 depon="$(cat_file "refs/heads/$name:.topdeps" $head_from 2>/dev/null | paste -s -d ' ' -)"
120 echo "$tgname: ($base_rev..) $name${depon:+ (depends on: $depon)}"
121 branch_contains "refs/heads/$name" "refs/$topbases/$name" ||
122 echo "$tgname: The patch is out-of-date wrt. the base! Run \`$tgdisplay update\`."
125 USE_PAGER_TYPE=diff
126 page output "$@"
127 # ... and then we run it through the pager with the page function
129 # vim:noet