Documentation/TODO: Mark guilt-rm as done
[guilt.git] / guilt
blob3fb52c97425aa49ebbb5a6655bd3da19b55ab91f
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.15"
8 if [ `basename $0` = "guilt" ]; then
9 # being run as standalone
11 # by default, we shouldn't fail
12 fail=0
14 if [ $# -ne 0 ]; then
15 # take first arg, and try to execute it
17 cmd="$1"
18 dir=`dirname $0`
20 if [ ! -x "$dir/guilt-$cmd" ]; then
21 echo "Command $cmd not found" >&2
22 echo "" >&2
23 fail=1
24 else
25 shift
26 exec "$dir/guilt-$cmd" "$@"
28 # this is not reached because of the exec
29 echo "Exec failed! Something is terribly wrong!" >&2
30 exit 1
34 # no args passed or invalid command entered, just output help summary
36 echo "Guilt v$GUILT_VERSION"
37 echo ""
38 echo "Pick a command:"
39 for x in `dirname $0`/guilt-*; do
40 [ -x $x ] && echo -e "\t`basename $x`"
41 done
43 echo ""
44 echo "Example:"
45 echo -e "\tguilt-push"
46 echo "or"
47 echo -e "\tguilt push"
49 # now, let's exit
50 exit $fail
53 ########
56 # Library goodies
59 function print_usage
61 echo "$USAGE" >&2
64 function find_git_dir
66 local d=`git-rev-parse --git-dir`
68 if [ $? -ne 0 -o -z "$d" ]; then
69 echo "Not a git repository" >&2
70 return 1
73 echo "$d"
76 function get_branch
78 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
81 function verify_branch
83 local b=$branch
85 [ ! -d "$GIT_DIR/patches" ] &&
86 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
87 return 1
88 [ ! -d "$GIT_DIR/patches/$b" ] &&
89 echo "Branch $b is not initialized, try guilt-init" >&2 &&
90 return 1
91 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
92 echo "Branch $b does not have a series file" >&2 &&
93 return 1
94 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
95 echo "Branch $b does not have a status file" >&2 &&
96 return 1
97 [ -f "$GIT_DIR/patches/$b/applied" ] &&
98 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
99 return 1
101 return 0
104 function get_top
106 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
109 function get_prev
111 local n=`wc -l < $GUILT_DIR/$branch/status`
112 local n=`expr $n - 1`
114 local idx=0
115 for p in `cat $GUILT_DIR/$branch/status`; do
116 idx=`expr $idx + 1`
117 [ $idx -lt $n ] && continue
118 [ $idx -gt $n ] && break
120 echo $p
121 done
124 function get_series
126 # ignore all lines matching:
127 # - empty lines
128 # - whitespace only
129 # - optional whitespace followed by '#' followed by more
130 # optional whitespace
131 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
134 # usage: index_update_magic
135 function index_update_magic
137 while read l; do
138 fil=`echo $l | cut -d: -f 2`
139 git-update-index --add --remove "$fil"
140 done
143 # usage: do_get_header patchfile
144 function do_get_header
146 # The complexity arises from the fact that we want to ignore the
147 # From line and the empty line after it if it exists
149 # 2nd line skips the From line
150 # 3rd line skips the empty line right after a From line
151 do_get_full_header $1 | awk '
152 BEGIN{skip=0}
153 /^From:/{skip=1; next}
154 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
155 {print $0}
156 END{}
160 # usage: do_get_full_header patchfile
161 function do_get_full_header
163 # 2nd line checks for the begining of a patch
164 # 3rd line outputs the line if it didn't get pruned by the above rules
165 cat $1 | awk '
166 BEGIN{ok=1}
167 /^(diff|---)/{ok=0}
168 (ok==1){print $0}
169 END{}
173 # usage: pop_patch
174 function pop_patch
176 cd $TOP_DIR
178 git reset --hard HEAD^
180 head -n -1 < $applied > $applied.tmp
181 mv $applied{.tmp,}
183 cd - 2>&1 >/dev/null
186 # usage: push_patch patchname
187 function push_patch
189 local p="$GUILT_DIR/$branch/$1"
190 local pname="$1"
192 local bail=0
194 cd $TOP_DIR
196 # apply the patch if and only if there is something to apply
197 if [ `wc -l < $p` -gt 0 ]; then
198 git-apply --reject $p > /dev/null 2> /tmp/guilt.log.$$
199 bail=$?
201 [ $bail -ne 0 ] && cat /tmp/guilt.log.$$ >&2
203 ( git-apply --numstat $p | awk '{print "changed:" $3}';
204 #git-apply --summary $p | awk '
205 # /^ (create|delete)/{print $1 ":" $4}
206 # /^ mode change/{print "mode:" $6}'
207 )| index_update_magic
210 # grab a commit message out of the patch
211 do_get_header $p > /tmp/guilt.msg.$$
213 # make a default commit message if patch doesn't contain one
214 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
216 # extract a From line from the patch header, and set
217 # GIT_AUTHOR_{NAME,EMAIL}
218 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
219 if [ ! -z "$author_str" ]; then
220 local backup_author_name="$GIT_AUTHOR_NAME"
221 local backup_author_email="$GIT_AUTHOR_EMAIL"
222 export GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
223 export GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
226 # commit
227 local treeish=`git-write-tree`
228 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
229 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
231 # mark patch as applied
232 echo "$commitish:$pname" >> $applied
234 # restore original GIT_AUTHOR_{NAME,EMAIL}
235 if [ ! -z "$author_str" ]; then
236 if [ ! -z "$backup_author_name" ]; then
237 export GIT_AUTHOR_NAME="$backup_author_name"
238 else
239 unset GIT_AUTHOR_NAME
242 if [ ! -z "$backup_author_name" ]; then
243 export GIT_AUTHOR_EMAIL="$backup_author_email"
244 else
245 unset GIT_AUTHOR_EMAIL
249 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
251 cd - 2>&1 >/dev/null
253 return $bail
256 # usage: must_commit_first
257 function must_commit_first
259 [ `git-diff-files | wc -l` -eq 0 ]
260 return $?
263 # usage: refresh_patch patchname
264 function refresh_patch
266 local p="$GUILT_DIR/$branch/$1"
268 cd $TOP_DIR
270 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
272 # get the patch header
273 do_get_full_header $p > /tmp/guilt.diff.$$
275 # get the new patch
276 git-diff HEAD^ >> /tmp/guilt.diff.$$
278 # move the new patch in
279 mv $p $p.prev
280 mv /tmp/guilt.diff.$$ $p
282 cd - 2>&1 >/dev/null
284 # drop the currently applied patch, pop_patch does it's own cd
285 # $TOP_DIR
286 pop_patch
288 # push_patch does it's own cd $TOP_DIR
289 push_patch $1
293 # The following gets run every time this file is source'd
296 GIT_DIR=`find_git_dir`
297 [ $? -ne 0 ] && exit 1
299 TOP_DIR=`git-rev-parse --show-cdup`
300 if [ -z "$TOP_DIR" ]; then
301 TOP_DIR="./"
304 GUILT_DIR="$GIT_DIR/patches"
306 branch=`get_branch`
308 # most of the time we want to verify that the repo's branch has been
309 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
310 # we must avoid the checks
311 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
312 verify_branch || exit 1
315 # very useful files
316 series="$GUILT_DIR/$branch/series"
317 applied="$GUILT_DIR/$branch/status"
319 USAGE="Usage: `basename $0`"