3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
7 GUILT_NAME
="Con Par Raison"
9 # we change directories ourselves
14 function guilt_commands
19 if [ -f "$command" -a -x "$command" ]
26 if [ `basename $0` = "guilt" ]; then
27 # being run as standalone
29 # by default, we shouldn't fail
33 # take first arg, and try to execute it
38 if [ -x "$dir/guilt-$arg" ]; then
41 # might be a short handed
42 for command in $
(guilt_commands
); do
45 if [ -x "$dir/guilt-$command" ]; then
54 exec "$dir/guilt-$cmd" "$@"
56 # this is not reached because of the exec
57 die
"Exec failed! Something is terribly wrong!"
59 echo "Command $arg not found" >&2
64 # no args passed or invalid command entered, just output help summary
66 echo "Guilt v$GUILT_VERSION"
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/'
75 echo -e "\tguilt-push"
77 echo -e "\tguilt push"
91 local d
=`git-rev-parse --git-dir`
93 if [ $?
-ne 0 -o -z "$d" ]; then
94 echo "Not a git repository" >&2
103 git-symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
106 function verify_branch
110 [ ! -d "$GIT_DIR/patches" ] &&
111 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
113 [ ! -d "$GIT_DIR/patches/$b" ] &&
114 echo "Branch $b is not initialized, try guilt-init" >&2 &&
116 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
117 echo "Branch $b does not have a series file" >&2 &&
119 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
120 echo "Branch $b does not have a status file" >&2 &&
122 [ -f "$GIT_DIR/patches/$b/applied" ] &&
123 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
131 tail -1 $GUILT_DIR/$branch/status | cut
-d: -f 2-
136 local n
=`wc -l < $GUILT_DIR/$branch/status`
137 local n
=`expr $n - 1`
140 for p
in `cat $GUILT_DIR/$branch/status`; do
142 [ $idx -lt $n ] && continue
143 [ $idx -gt $n ] && break
151 # ignore all lines matching:
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?
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
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 "
180 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
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 '
196 /^From:/{skip=1; next}
197 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
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
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
228 # usage: head_check <expected hash>
231 # make sure we're not doing funky things to commits that don't
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
246 # usage: series_insert_patch <patchname>
247 function series_insert_patch
251 if [ ! -z "$top" ]; then
252 sed -i -e "s,^$top\$,$top\n$1," $series
254 echo "$1" > $series.tmp
255 cat $series >> $series.tmp
256 mv $series.tmp
$series
263 pop_many_patches HEAD^
1
266 # usage: pop_many_patches <commitish> <number of patches>
267 function pop_many_patches
273 git-reset
--hard $1 > /dev
/null
274 head -n -$2 < $applied > $applied.tmp
280 # usage: push_patch patchname [bail_action]
283 local p
="$GUILT_DIR/$branch/$1"
285 local bail_action
="$2"
288 local reject
="--reject"
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
299 git-apply
-C$guilt_push_diff_context \
300 $reject $p > /dev
/null
2> /tmp
/guilt.log.$$
303 if [ $bail -ne 0 ]; then
304 cat /tmp
/guilt.log.$$
>&2
305 if [ "$bail_action" = abort
]; then
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
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
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"
355 unset GIT_AUTHOR_NAME
358 if [ ! -z "$backup_author_name" ]; then
359 export GIT_AUTHOR_EMAIL
="$backup_author_email"
361 unset GIT_AUTHOR_EMAIL
364 if [ ! -z "$backup_author_date" ]; then
365 export GIT_AUTHOR_DATE
="$backup_author_date"
367 unset GIT_AUTHOR_DATE
369 if [ ! -z "$backup_committer_date" ]; then
370 export GIT_COMMITTER_DATE
="$backup_committer_date"
372 unset GIT_COMMITTER_DATE
375 rm -f /tmp
/guilt.msg.$$
/tmp
/guilt.log.$$
382 # usage: must_commit_first
383 function must_commit_first
385 [ `git-diff-files | wc -l` -eq 0 ]
389 # usage: refresh_patch patchname
390 function refresh_patch
392 local p
="$GUILT_DIR/$branch/$1"
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.$$
404 git-diff HEAD^
>> /tmp
/guilt.
diff.$$
406 # move the new patch in
408 mv /tmp
/guilt.
diff.$$
$p
412 # drop the currently applied patch, pop_patch does it's own cd
416 # push_patch does it's own cd $TOP_DIR
420 # usage: munge_hash_range <hash range>
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
439 elif [ -z "$h1" ]; then
442 elif [ -z "$h2" ]; then
446 # e.g., "v0.19-rc1..v0.19"
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
468 GUILT_DIR
="$GIT_DIR/patches"
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
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)
485 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
487 # determine a pager to use for anything interactive (fall back to more)
489 [ -z "$pager" ] && pager
="$PAGER"