files: Recurse into subdirectories when using git-diff-tree
[guilt.git] / guilt
blobc41f79ad727f1901e2194b66366cdb06aa67c152
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.21"
7 GUILT_NAME="Con Par Raison"
9 # we change directories ourselves
10 SUBDIRECTORY_OK=1
12 . git-sh-setup
14 function guilt_commands
16 local command
17 for command in $0-*
19 if [ -f "$command" -a -x "$command" ]
20 then
21 echo ${command##$0-}
23 done
26 if [ `basename $0` = "guilt" ]; then
27 # being run as standalone
29 # by default, we shouldn't fail
30 cmd=
32 if [ $# -ne 0 ]; then
33 # take first arg, and try to execute it
35 arg="$1"
36 dir=`dirname $0`
38 if [ -x "$dir/guilt-$arg" ]; then
39 cmd=$arg
40 else
41 # might be a short handed
42 for command in $(guilt_commands); do
43 case $command in
44 $arg*)
45 if [ -x "$dir/guilt-$command" ]; then
46 cmd=$command
49 esac
50 done
52 if [ $cmd ]; then
53 shift
54 exec "$dir/guilt-$cmd" "$@"
56 # this is not reached because of the exec
57 die "Exec failed! Something is terribly wrong!"
58 else
59 echo "Command $arg not found" >&2
60 echo "" >&2
64 # no args passed or invalid command entered, just output help summary
66 echo "Guilt v$GUILT_VERSION"
67 echo ""
68 echo "Pick a command:"
69 for x in `dirname $0`/guilt-*; do
70 [ -x $x ] && echo -e ${x##$0-}
71 done | sort | column | column -t | sed -e $'s/^/\t/'
73 echo ""
74 echo "Example:"
75 echo -e "\tguilt-push"
76 echo "or"
77 echo -e "\tguilt push"
79 # now, let's exit
80 exit 1
83 ########
86 # Library goodies
89 function find_git_dir
91 local d=`git-rev-parse --git-dir`
93 if [ $? -ne 0 -o -z "$d" ]; then
94 echo "Not a git repository" >&2
95 return 1
98 echo "$d"
101 function get_branch
103 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
106 function verify_branch
108 local b=$branch
110 [ ! -d "$GIT_DIR/patches" ] &&
111 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
112 return 1
113 [ ! -d "$GIT_DIR/patches/$b" ] &&
114 echo "Branch $b is not initialized, try guilt-init" >&2 &&
115 return 1
116 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
117 echo "Branch $b does not have a series file" >&2 &&
118 return 1
119 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
120 echo "Branch $b does not have a status file" >&2 &&
121 return 1
122 [ -f "$GIT_DIR/patches/$b/applied" ] &&
123 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
124 return 1
126 return 0
129 function get_top
131 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
134 function get_prev
136 local n=`wc -l < $GUILT_DIR/$branch/status`
137 local n=`expr $n - 1`
139 local idx=0
140 for p in `cat $GUILT_DIR/$branch/status`; do
141 idx=`expr $idx + 1`
142 [ $idx -lt $n ] && continue
143 [ $idx -gt $n ] && break
145 echo $p
146 done
149 function get_series
151 # ignore all lines matching:
152 # - empty lines
153 # - whitespace only
154 # - optional whitespace followed by '#' followed by more
155 # optional whitespace
156 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
159 # usage: do_make_header <hash>
160 function do_make_header
162 # which revision do we want to work with?
163 local rev=$1
165 # we should try to work with commit objects only
166 if [ `git-cat-file -t $rev` != "commit" ]; then
167 echo "Hash $rev is not a commit object" >&2
168 echo "Aborting..." >&2
169 exit 2
172 # get the author line from the commit object
173 local author=`git-cat-file -p $rev | grep -e '^author ' | head -1`
175 # strip the timestamp & '^author ' string
176 author=`echo $author | sed -e 's/^author //' -e 's/ [0-9]* [+-]*[0-9][0-9]*$//'`
178 git-cat-file -p $rev | awk "
179 BEGIN{ok=0}
180 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
181 (ok==2){print \$0}
182 /^\$/ && (ok==0){ok=1}
186 # usage: do_get_header patchfile
187 function do_get_header
189 # The complexity arises from the fact that we want to ignore the
190 # From line and the empty line after it if it exists
192 # 2nd line skips the From line
193 # 3rd line skips the empty line right after a From line
194 do_get_full_header $1 | awk '
195 BEGIN{skip=0}
196 /^From:/{skip=1; next}
197 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
198 {print $0}
199 END{}
203 # usage: do_get_full_header patchfile
204 function do_get_full_header
206 # 2nd line checks for the begining of a patch
207 # 3rd line outputs the line if it didn't get pruned by the above rules
208 cat $1 | awk '
209 BEGIN{ok=1}
210 /^(diff|---)/{ok=0}
211 (ok==1){print $0}
212 END{}
216 # usage: assert_head_check
217 function assert_head_check
219 local eh=`tail -1 < $applied | cut -d: -f 1`
221 if ! head_check $eh; then
222 die "aborting..."
225 return 0
228 # usage: head_check <expected hash>
229 function head_check
231 # make sure we're not doing funky things to commits that don't
232 # belong to us
233 local ch=`cat $GIT_DIR/refs/heads/$branch`
235 # if the expected hash is empty, just return
236 [ -z "$1" ] && return 0
238 if [ "$ch" != "$1" ]; then
239 echo "Expected HEAD commit $1" >&2
240 echo " got $ch" >&2
241 return 1
243 return 0
246 # usage: series_insert_patch <patchname>
247 function series_insert_patch
249 local top=`get_top`
251 if [ ! -z "$top" ]; then
252 sed -i -e "s,^$top\$,$top\n$1," $series
253 else
254 echo "$1" > $series.tmp
255 cat $series >> $series.tmp
256 mv $series.tmp $series
260 # usage: pop_patch
261 function pop_patch
263 pop_many_patches HEAD^ 1
266 # usage: pop_many_patches <commitish> <number of patches>
267 function pop_many_patches
269 assert_head_check
271 cd $TOP_DIR
273 git-reset --hard $1 > /dev/null
274 head -n -$2 < $applied > $applied.tmp
275 mv $applied{.tmp,}
277 cd - 2>&1 >/dev/null
280 # usage: push_patch patchname [bail_action]
281 function push_patch
283 local p="$GUILT_DIR/$branch/$1"
284 local pname="$1"
285 local bail_action="$2"
287 local bail=0
288 local reject="--reject"
290 assert_head_check
292 cd $TOP_DIR
294 # apply the patch if and only if there is something to apply
295 if [ `git-apply --numstat $p | wc -l` -gt 0 ]; then
296 if [ "$bail_action" = abort ]; then
297 reject=""
299 git-apply -C$guilt_push_diff_context \
300 $reject $p > /dev/null 2> /tmp/guilt.log.$$
301 bail=$?
303 if [ $bail -ne 0 ]; then
304 cat /tmp/guilt.log.$$ >&2
305 if [ "$bail_action" = abort ]; then
306 return $bail
310 # FIXME: Path munging is being done, we need to convince
311 # git-apply to just give us list of files with \0 as a
312 # delimiter, and pass -z to git-update-index
313 git-apply --numstat $p | cut -f 3- | git-update-index --add --remove --stdin
316 # grab a commit message out of the patch
317 do_get_header $p > /tmp/guilt.msg.$$
319 # make a default commit message if patch doesn't contain one
320 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
322 # extract a From line from the patch header, and set
323 # GIT_AUTHOR_{NAME,EMAIL}
324 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
325 if [ ! -z "$author_str" ]; then
326 local backup_author_name="$GIT_AUTHOR_NAME"
327 local backup_author_email="$GIT_AUTHOR_EMAIL"
328 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
329 GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
331 if [ -z "$GIT_AUTHOR_NAME" ]; then
332 GIT_AUTHOR_NAME=" "
335 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
337 local backup_author_date="$GIT_AUTHOR_DATE"
338 local backup_committer_date="$GIT_COMMITTER_DATE"
339 export GIT_AUTHOR_DATE=`stat -c %y $p`
340 export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE
342 # commit
343 local treeish=`git-write-tree`
344 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
345 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
347 # mark patch as applied
348 echo "$commitish:$pname" >> $applied
350 # restore original GIT_AUTHOR_{NAME,EMAIL}
351 if [ ! -z "$author_str" ]; then
352 if [ ! -z "$backup_author_name" ]; then
353 export GIT_AUTHOR_NAME="$backup_author_name"
354 else
355 unset GIT_AUTHOR_NAME
358 if [ ! -z "$backup_author_name" ]; then
359 export GIT_AUTHOR_EMAIL="$backup_author_email"
360 else
361 unset GIT_AUTHOR_EMAIL
364 if [ ! -z "$backup_author_date" ]; then
365 export GIT_AUTHOR_DATE="$backup_author_date"
366 else
367 unset GIT_AUTHOR_DATE
369 if [ ! -z "$backup_committer_date" ]; then
370 export GIT_COMMITTER_DATE="$backup_committer_date"
371 else
372 unset GIT_COMMITTER_DATE
375 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
377 cd - 2>&1 >/dev/null
379 return $bail
382 # usage: must_commit_first
383 function must_commit_first
385 [ `git-diff-files | wc -l` -eq 0 ]
386 return $?
389 # usage: refresh_patch patchname
390 function refresh_patch
392 local p="$GUILT_DIR/$branch/$1"
394 assert_head_check
396 cd $TOP_DIR
398 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
400 # get the patch header
401 do_get_full_header $p > /tmp/guilt.diff.$$
403 # get the new patch
404 git-diff HEAD^ >> /tmp/guilt.diff.$$
406 # move the new patch in
407 mv $p $p~
408 mv /tmp/guilt.diff.$$ $p
410 cd - 2>&1 >/dev/null
412 # drop the currently applied patch, pop_patch does it's own cd
413 # $TOP_DIR
414 pop_patch
416 # push_patch does it's own cd $TOP_DIR
417 push_patch $1
420 # usage: munge_hash_range <hash range>
422 # this means:
423 # <hash> - one commit
424 # <hash>.. - hash until head (excludes hash, includes head)
425 # ..<hash> - until hash (includes hash)
426 # <hash1>..<hash2> - from hash to hash (inclusive)
428 # The output of this function is suitable to be passed to git-rev-list
429 function munge_hash_range
431 local l=`echo "$1" | sed -e 's/\.\./ /'`
433 local h1=`echo "$l" | cut -s -d' ' -f 1`
434 local h2=`echo "$l" | cut -s -d' ' -f 2`
436 if [ -z "$h1" -a -z "$h2" ]; then
437 # e.g., "v0.19"
438 echo "$l^..$l"
439 elif [ -z "$h1" ]; then
440 # e.g., "..v0.10"
441 echo "$h2"
442 elif [ -z "$h2" ]; then
443 # e.g., "v0.19.."
444 echo "$h1..HEAD"
445 else
446 # e.g., "v0.19-rc1..v0.19"
447 echo "$h1..$h2"
451 # Some constants
453 # used for: git-apply -C <val>
454 guilt_push_diff_context=1
457 # The following gets run every time this file is source'd
460 GIT_DIR=`find_git_dir`
461 [ $? -ne 0 ] && exit 1
463 TOP_DIR=`git-rev-parse --show-cdup`
464 if [ -z "$TOP_DIR" ]; then
465 TOP_DIR="./"
468 GUILT_DIR="$GIT_DIR/patches"
470 branch=`get_branch`
472 # most of the time we want to verify that the repo's branch has been
473 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
474 # we must avoid the checks
475 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
476 verify_branch || exit 1
479 # very useful files
480 series="$GUILT_DIR/$branch/series"
481 applied="$GUILT_DIR/$branch/status"
483 # determine an editor to use for anything interactive (fall back to vi)
484 editor="vi"
485 [ ! -z "$EDITOR" ] && editor="$EDITOR"
487 # determine a pager to use for anything interactive (fall back to more)
488 pager="more"
489 [ -z "$pager" ] && pager="$PAGER"