guilt: Document push_patch's new argument
[guilt.git] / guilt
blob3e018e71a6eb7608b93d2448fb3ee5e1095485eb
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.20"
7 GUILT_NAME="Buddy Holly"
9 function guilt_commands
11 local command
12 for command in $0-*
14 if [ -f "$command" -a -x "$command" ]
15 then
16 echo ${command##$0-}
18 done
21 if [ `basename $0` = "guilt" ]; then
22 # being run as standalone
24 # by default, we shouldn't fail
25 cmd=
27 if [ $# -ne 0 ]; then
28 # take first arg, and try to execute it
30 arg="$1"
31 dir=`dirname $0`
33 if [ -x "$dir/guilt-$arg" ]; then
34 cmd=$arg
35 else
36 # might be a short handed
37 for command in $(guilt_commands); do
38 case $command in
39 $arg*)
40 if [ -x "$dir/guilt-$command" ]; then
41 cmd=$command
44 esac
45 done
47 if [ $cmd ]; then
48 shift
49 exec "$dir/guilt-$cmd" "$@"
51 # this is not reached because of the exec
52 echo "Exec failed! Something is terribly wrong!" >&2
53 exit 1
54 else
55 echo "Command $arg not found" >&2
56 echo "" >&2
60 # no args passed or invalid command entered, just output help summary
62 echo "Guilt v$GUILT_VERSION"
63 echo ""
64 echo "Pick a command:"
65 for x in `dirname $0`/guilt-*; do
66 [ -x $x ] && echo -e ${x##$0-}
67 done | sort | column | column -t | sed -e $'s/^/\t/'
69 echo ""
70 echo "Example:"
71 echo -e "\tguilt-push"
72 echo "or"
73 echo -e "\tguilt push"
75 # now, let's exit
76 exit 1
79 ########
82 # Library goodies
85 function print_usage
87 echo "$USAGE" >&2
90 function find_git_dir
92 local d=`git-rev-parse --git-dir`
94 if [ $? -ne 0 -o -z "$d" ]; then
95 echo "Not a git repository" >&2
96 return 1
99 echo "$d"
102 function get_branch
104 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
107 function verify_branch
109 local b=$branch
111 [ ! -d "$GIT_DIR/patches" ] &&
112 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
113 return 1
114 [ ! -d "$GIT_DIR/patches/$b" ] &&
115 echo "Branch $b is not initialized, try guilt-init" >&2 &&
116 return 1
117 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
118 echo "Branch $b does not have a series file" >&2 &&
119 return 1
120 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
121 echo "Branch $b does not have a status file" >&2 &&
122 return 1
123 [ -f "$GIT_DIR/patches/$b/applied" ] &&
124 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
125 return 1
127 return 0
130 function get_top
132 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
135 function get_prev
137 local n=`wc -l < $GUILT_DIR/$branch/status`
138 local n=`expr $n - 1`
140 local idx=0
141 for p in `cat $GUILT_DIR/$branch/status`; do
142 idx=`expr $idx + 1`
143 [ $idx -lt $n ] && continue
144 [ $idx -gt $n ] && break
146 echo $p
147 done
150 function get_series
152 # ignore all lines matching:
153 # - empty lines
154 # - whitespace only
155 # - optional whitespace followed by '#' followed by more
156 # optional whitespace
157 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
160 # usage: do_make_header <hash>
161 function do_make_header
163 # which revision do we want to work with?
164 local rev=$1
166 # we should try to work with commit objects only
167 if [ `git-cat-file -t $rev` != "commit" ]; then
168 echo "Hash $rev is not a commit object" >&2
169 echo "Aborting..." >&2
170 exit 2
173 # get the author line from the commit object
174 local author=`git-cat-file -p $rev | grep -e '^author ' | head -1`
176 # strip the timestamp & '^author ' string
177 author=`echo $author | sed -e 's/^author //' -e 's/ [0-9]* [+-]*[0-9][0-9]*$//'`
179 git-cat-file -p $rev | awk "
180 BEGIN{ok=0}
181 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
182 (ok==2){print \$0}
183 /^\$/ && (ok==0){ok=1}
187 # usage: do_get_header patchfile
188 function do_get_header
190 # The complexity arises from the fact that we want to ignore the
191 # From line and the empty line after it if it exists
193 # 2nd line skips the From line
194 # 3rd line skips the empty line right after a From line
195 do_get_full_header $1 | awk '
196 BEGIN{skip=0}
197 /^From:/{skip=1; next}
198 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
199 {print $0}
200 END{}
204 # usage: do_get_full_header patchfile
205 function do_get_full_header
207 # 2nd line checks for the begining of a patch
208 # 3rd line outputs the line if it didn't get pruned by the above rules
209 cat $1 | awk '
210 BEGIN{ok=1}
211 /^(diff|---)/{ok=0}
212 (ok==1){print $0}
213 END{}
217 # usage: assert_head_check
218 function assert_head_check
220 local eh=`tail -1 < $applied | cut -d: -f 1`
222 if ! head_check $eh; then
223 echo "aborting..." >&2
224 exit 1
227 return 0
230 # usage: head_check <expected hash>
231 function head_check
233 # make sure we're not doing funky things to commits that don't
234 # belong to us
235 local ch=`cat $GIT_DIR/refs/heads/$branch`
237 # if the expected hash is empty, just return
238 [ -z "$1" ] && return 0
240 if [ "$ch" != "$1" ]; then
241 echo "Expected HEAD commit $1" >&2
242 echo " got $ch" >&2
243 return 1
245 return 0
248 # usage: series_insert_patch <patchname>
249 function series_insert_patch
251 local top=`get_top`
253 if [ ! -z "$top" ]; then
254 sed -i -e "s,^$top\$,$top\n$1," $series
255 else
256 echo "$1" > $series.tmp
257 cat $series >> $series.tmp
258 mv $series.tmp $series
262 # usage: pop_patch
263 function pop_patch
265 pop_many_patches HEAD^ 1
268 # usage: pop_many_patches <commitish> <number of patches>
269 function pop_many_patches
271 assert_head_check
273 cd $TOP_DIR
275 git-reset --hard $1 > /dev/null
276 head -n -$2 < $applied > $applied.tmp
277 mv $applied{.tmp,}
279 cd - 2>&1 >/dev/null
282 # usage: push_patch patchname [bail_action]
283 function push_patch
285 local p="$GUILT_DIR/$branch/$1"
286 local pname="$1"
287 local bail_action="$2"
289 local bail=0
290 local reject="--reject"
292 assert_head_check
294 cd $TOP_DIR
296 # apply the patch if and only if there is something to apply
297 if [ `git-apply --numstat $p | wc -l` -gt 0 ]; then
298 if [ "$bail_action" = abort ]; then
299 reject=""
301 git-apply -C$guilt_push_diff_context \
302 $reject $p > /dev/null 2> /tmp/guilt.log.$$
303 bail=$?
305 if [ $bail -ne 0 ]; then
306 cat /tmp/guilt.log.$$ >&2
307 if [ "$bail_action" = abort ]; then
308 return $bail
312 # FIXME: Path munging is being done, we need to convince
313 # git-apply to just give us list of files with \0 as a
314 # delimiter, and pass -z to git-update-index
315 git-apply --numstat $p | cut -f 3- | git-update-index --add --remove --stdin
318 # grab a commit message out of the patch
319 do_get_header $p > /tmp/guilt.msg.$$
321 # make a default commit message if patch doesn't contain one
322 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
324 # extract a From line from the patch header, and set
325 # GIT_AUTHOR_{NAME,EMAIL}
326 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
327 if [ ! -z "$author_str" ]; then
328 local backup_author_name="$GIT_AUTHOR_NAME"
329 local backup_author_email="$GIT_AUTHOR_EMAIL"
330 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
331 GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
333 if [ -z "$GIT_AUTHOR_NAME" ]; then
334 GIT_AUTHOR_NAME=" "
337 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
339 local backup_author_date="$GIT_AUTHOR_DATE"
340 local backup_committer_date="$GIT_COMMITTER_DATE"
341 export GIT_AUTHOR_DATE=`stat -c %y $p`
342 export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE
344 # commit
345 local treeish=`git-write-tree`
346 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
347 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
349 # mark patch as applied
350 echo "$commitish:$pname" >> $applied
352 # restore original GIT_AUTHOR_{NAME,EMAIL}
353 if [ ! -z "$author_str" ]; then
354 if [ ! -z "$backup_author_name" ]; then
355 export GIT_AUTHOR_NAME="$backup_author_name"
356 else
357 unset GIT_AUTHOR_NAME
360 if [ ! -z "$backup_author_name" ]; then
361 export GIT_AUTHOR_EMAIL="$backup_author_email"
362 else
363 unset GIT_AUTHOR_EMAIL
366 if [ ! -z "$backup_author_date" ]; then
367 export GIT_AUTHOR_DATE="$backup_author_date"
368 else
369 unset GIT_AUTHOR_DATE
371 if [ ! -z "$backup_committer_date" ]; then
372 export GIT_COMMITTER_DATE="$backup_committer_date"
373 else
374 unset GIT_COMMITTER_DATE
377 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
379 cd - 2>&1 >/dev/null
381 return $bail
384 # usage: must_commit_first
385 function must_commit_first
387 [ `git-diff-files | wc -l` -eq 0 ]
388 return $?
391 # usage: refresh_patch patchname
392 function refresh_patch
394 local p="$GUILT_DIR/$branch/$1"
396 assert_head_check
398 cd $TOP_DIR
400 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
402 # get the patch header
403 do_get_full_header $p > /tmp/guilt.diff.$$
405 # get the new patch
406 git-diff HEAD^ >> /tmp/guilt.diff.$$
408 # move the new patch in
409 mv $p $p.prev
410 mv /tmp/guilt.diff.$$ $p
412 cd - 2>&1 >/dev/null
414 # drop the currently applied patch, pop_patch does it's own cd
415 # $TOP_DIR
416 pop_patch
418 # push_patch does it's own cd $TOP_DIR
419 push_patch $1
422 # usage: munge_hash_range <hash range>
424 # this means:
425 # <hash> - one commit
426 # <hash>.. - hash until head (excludes hash, includes head)
427 # ..<hash> - until hash (includes hash)
428 # <hash1>..<hash2> - from hash to hash (inclusive)
430 # The output of this function is suitable to be passed to git-rev-list
431 function munge_hash_range
433 local l=`echo "$1" | sed -e 's/\.\./ /'`
435 local h1=`echo "$l" | cut -s -d' ' -f 1`
436 local h2=`echo "$l" | cut -s -d' ' -f 2`
438 if [ -z "$h1" -a -z "$h2" ]; then
439 # e.g., "v0.19"
440 echo "$l^..$l"
441 elif [ -z "$h1" ]; then
442 # e.g., "..v0.10"
443 echo "$h2"
444 elif [ -z "$h2" ]; then
445 # e.g., "v0.19.."
446 echo "$h1..HEAD"
447 else
448 # e.g., "v0.19-rc1..v0.19"
449 echo "$h1..$h2"
453 # Some constants
455 # used for: git-apply -C <val>
456 guilt_push_diff_context=1
459 # The following gets run every time this file is source'd
462 GIT_DIR=`find_git_dir`
463 [ $? -ne 0 ] && exit 1
465 TOP_DIR=`git-rev-parse --show-cdup`
466 if [ -z "$TOP_DIR" ]; then
467 TOP_DIR="./"
470 GUILT_DIR="$GIT_DIR/patches"
472 branch=`get_branch`
474 # most of the time we want to verify that the repo's branch has been
475 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
476 # we must avoid the checks
477 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
478 verify_branch || exit 1
481 # very useful files
482 series="$GUILT_DIR/$branch/series"
483 applied="$GUILT_DIR/$branch/status"
485 USAGE="Usage: `basename $0`"
487 # determine an editor to use for anything interactive (fall back to vi)
488 editor="vi"
489 [ ! -z "$EDITOR" ] && editor="$EDITOR"
491 # determine a pager to use for anything interactive (fall back to more)
492 pager="more"
493 [ -z "$pager" ] && pager="$PAGER"