docs: Commands should be on a separate line
[guilt.git] / guilt
blob9d38f5e8cbf65963e21bfe5279a9154eadc52396
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.20"
7 GUILT_NAME="Buddy Holly"
9 . git-sh-setup
11 function guilt_commands
13 local command
14 for command in $0-*
16 if [ -f "$command" -a -x "$command" ]
17 then
18 echo ${command##$0-}
20 done
23 if [ `basename $0` = "guilt" ]; then
24 # being run as standalone
26 # by default, we shouldn't fail
27 cmd=
29 if [ $# -ne 0 ]; then
30 # take first arg, and try to execute it
32 arg="$1"
33 dir=`dirname $0`
35 if [ -x "$dir/guilt-$arg" ]; then
36 cmd=$arg
37 else
38 # might be a short handed
39 for command in $(guilt_commands); do
40 case $command in
41 $arg*)
42 if [ -x "$dir/guilt-$command" ]; then
43 cmd=$command
46 esac
47 done
49 if [ $cmd ]; then
50 shift
51 exec "$dir/guilt-$cmd" "$@"
53 # this is not reached because of the exec
54 die "Exec failed! Something is terribly wrong!"
55 else
56 echo "Command $arg not found" >&2
57 echo "" >&2
61 # no args passed or invalid command entered, just output help summary
63 echo "Guilt v$GUILT_VERSION"
64 echo ""
65 echo "Pick a command:"
66 for x in `dirname $0`/guilt-*; do
67 [ -x $x ] && echo -e ${x##$0-}
68 done | sort | column | column -t | sed -e $'s/^/\t/'
70 echo ""
71 echo "Example:"
72 echo -e "\tguilt-push"
73 echo "or"
74 echo -e "\tguilt push"
76 # now, let's exit
77 exit 1
80 ########
83 # Library goodies
86 function print_usage
88 echo "$USAGE" >&2
91 function find_git_dir
93 local d=`git-rev-parse --git-dir`
95 if [ $? -ne 0 -o -z "$d" ]; then
96 echo "Not a git repository" >&2
97 return 1
100 echo "$d"
103 function get_branch
105 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
108 function verify_branch
110 local b=$branch
112 [ ! -d "$GIT_DIR/patches" ] &&
113 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
114 return 1
115 [ ! -d "$GIT_DIR/patches/$b" ] &&
116 echo "Branch $b is not initialized, try guilt-init" >&2 &&
117 return 1
118 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
119 echo "Branch $b does not have a series file" >&2 &&
120 return 1
121 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
122 echo "Branch $b does not have a status file" >&2 &&
123 return 1
124 [ -f "$GIT_DIR/patches/$b/applied" ] &&
125 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
126 return 1
128 return 0
131 function get_top
133 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
136 function get_prev
138 local n=`wc -l < $GUILT_DIR/$branch/status`
139 local n=`expr $n - 1`
141 local idx=0
142 for p in `cat $GUILT_DIR/$branch/status`; do
143 idx=`expr $idx + 1`
144 [ $idx -lt $n ] && continue
145 [ $idx -gt $n ] && break
147 echo $p
148 done
151 function get_series
153 # ignore all lines matching:
154 # - empty lines
155 # - whitespace only
156 # - optional whitespace followed by '#' followed by more
157 # optional whitespace
158 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
161 # usage: do_make_header <hash>
162 function do_make_header
164 # which revision do we want to work with?
165 local rev=$1
167 # we should try to work with commit objects only
168 if [ `git-cat-file -t $rev` != "commit" ]; then
169 echo "Hash $rev is not a commit object" >&2
170 echo "Aborting..." >&2
171 exit 2
174 # get the author line from the commit object
175 local author=`git-cat-file -p $rev | grep -e '^author ' | head -1`
177 # strip the timestamp & '^author ' string
178 author=`echo $author | sed -e 's/^author //' -e 's/ [0-9]* [+-]*[0-9][0-9]*$//'`
180 git-cat-file -p $rev | awk "
181 BEGIN{ok=0}
182 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
183 (ok==2){print \$0}
184 /^\$/ && (ok==0){ok=1}
188 # usage: do_get_header patchfile
189 function do_get_header
191 # The complexity arises from the fact that we want to ignore the
192 # From line and the empty line after it if it exists
194 # 2nd line skips the From line
195 # 3rd line skips the empty line right after a From line
196 do_get_full_header $1 | awk '
197 BEGIN{skip=0}
198 /^From:/{skip=1; next}
199 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
200 {print $0}
201 END{}
205 # usage: do_get_full_header patchfile
206 function do_get_full_header
208 # 2nd line checks for the begining of a patch
209 # 3rd line outputs the line if it didn't get pruned by the above rules
210 cat $1 | awk '
211 BEGIN{ok=1}
212 /^(diff|---)/{ok=0}
213 (ok==1){print $0}
214 END{}
218 # usage: assert_head_check
219 function assert_head_check
221 local eh=`tail -1 < $applied | cut -d: -f 1`
223 if ! head_check $eh; then
224 die "aborting..."
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 # determine an editor to use for anything interactive (fall back to vi)
486 editor="vi"
487 [ ! -z "$EDITOR" ] && editor="$EDITOR"
489 # determine a pager to use for anything interactive (fall back to more)
490 pager="more"
491 [ -z "$pager" ] && pager="$PAGER"