3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
7 GUILT_NAME
="Buddy Holly"
9 function guilt_commands
14 if [ -f "$command" -a -x "$command" ]
21 if [ `basename $0` = "guilt" ]; then
22 # being run as standalone
24 # by default, we shouldn't fail
28 # take first arg, and try to execute it
33 if [ -x "$dir/guilt-$arg" ]; then
36 # might be a short handed
37 for command in $
(guilt_commands
); do
40 if [ -x "$dir/guilt-$command" ]; then
49 exec "$dir/guilt-$cmd" "$@"
51 # this is not reached because of the exec
52 echo "Exec failed! Something is terribly wrong!" >&2
55 echo "Command $arg not found" >&2
60 # no args passed or invalid command entered, just output help summary
62 echo "Guilt v$GUILT_VERSION"
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/'
71 echo -e "\tguilt-push"
73 echo -e "\tguilt push"
92 local d
=`git-rev-parse --git-dir`
94 if [ $?
-ne 0 -o -z "$d" ]; then
95 echo "Not a git repository" >&2
104 git-symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
107 function verify_branch
111 [ ! -d "$GIT_DIR/patches" ] &&
112 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
114 [ ! -d "$GIT_DIR/patches/$b" ] &&
115 echo "Branch $b is not initialized, try guilt-init" >&2 &&
117 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
118 echo "Branch $b does not have a series file" >&2 &&
120 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
121 echo "Branch $b does not have a status file" >&2 &&
123 [ -f "$GIT_DIR/patches/$b/applied" ] &&
124 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
132 tail -1 $GUILT_DIR/$branch/status | cut
-d: -f 2-
137 local n
=`wc -l < $GUILT_DIR/$branch/status`
138 local n
=`expr $n - 1`
141 for p
in `cat $GUILT_DIR/$branch/status`; do
143 [ $idx -lt $n ] && continue
144 [ $idx -gt $n ] && break
152 # ignore all lines matching:
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?
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
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 "
181 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
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 '
197 /^From:/{skip=1; next}
198 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
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
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
230 # usage: head_check <expected hash>
233 # make sure we're not doing funky things to commits that don't
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
248 # usage: series_insert_patch <patchname>
249 function series_insert_patch
253 if [ ! -z "$top" ]; then
254 sed -i -e "s,^$top\$,$top\n$1," $series
256 echo "$1" > $series.tmp
257 cat $series >> $series.tmp
258 mv $series.tmp
$series
265 pop_many_patches HEAD^
1
268 # usage: pop_many_patches <commitish> <number of patches>
269 function pop_many_patches
275 git-reset
--hard $1 > /dev
/null
276 head -n -$2 < $applied > $applied.tmp
282 # usage: push_patch patchname [bail_action]
285 local p
="$GUILT_DIR/$branch/$1"
287 local bail_action
="$2"
290 local reject
="--reject"
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
301 git-apply
-C$guilt_push_diff_context \
302 $reject $p > /dev
/null
2> /tmp
/guilt.log.$$
305 if [ $bail -ne 0 ]; then
306 cat /tmp
/guilt.log.$$
>&2
307 if [ "$bail_action" = abort
]; then
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
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
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"
357 unset GIT_AUTHOR_NAME
360 if [ ! -z "$backup_author_name" ]; then
361 export GIT_AUTHOR_EMAIL
="$backup_author_email"
363 unset GIT_AUTHOR_EMAIL
366 if [ ! -z "$backup_author_date" ]; then
367 export GIT_AUTHOR_DATE
="$backup_author_date"
369 unset GIT_AUTHOR_DATE
371 if [ ! -z "$backup_committer_date" ]; then
372 export GIT_COMMITTER_DATE
="$backup_committer_date"
374 unset GIT_COMMITTER_DATE
377 rm -f /tmp
/guilt.msg.$$
/tmp
/guilt.log.$$
384 # usage: must_commit_first
385 function must_commit_first
387 [ `git-diff-files | wc -l` -eq 0 ]
391 # usage: refresh_patch patchname
392 function refresh_patch
394 local p
="$GUILT_DIR/$branch/$1"
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.$$
406 git-diff HEAD^
>> /tmp
/guilt.
diff.$$
408 # move the new patch in
410 mv /tmp
/guilt.
diff.$$
$p
414 # drop the currently applied patch, pop_patch does it's own cd
418 # push_patch does it's own cd $TOP_DIR
422 # usage: munge_hash_range <hash range>
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
441 elif [ -z "$h1" ]; then
444 elif [ -z "$h2" ]; then
448 # e.g., "v0.19-rc1..v0.19"
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
470 GUILT_DIR
="$GIT_DIR/patches"
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
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)
489 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
491 # determine a pager to use for anything interactive (fall back to more)
493 [ -z "$pager" ] && pager
="$PAGER"