3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
7 GUILT_NAME
="Space Rock"
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"
89 # usage: valid_patchname <patchname>
90 function valid_patchname
92 [ `echo "$1" | grep -e '^/' | wc -l` -gt 0 ] && return 1
93 [ `echo "$1" | grep -e '^\./' | wc -l` -gt 0 ] && return 1
94 [ `echo "$1" | grep -e '^\.\./' | wc -l` -gt 0 ] && return 1
95 [ `echo "$1" | grep -e '/\./' | wc -l` -gt 0 ] && return 1
96 [ `echo "$1" | grep -e '/\.\./' | wc -l` -gt 0 ] && return 1
97 [ `echo "$1" | grep -e '/\.$' | wc -l` -gt 0 ] && return 1
98 [ `echo "$1" | grep -e '/\.\.$' | wc -l` -gt 0 ] && return 1
99 [ `echo "$1" | grep -e '/$' | wc -l` -gt 0 ] && return 1
105 git-symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
108 function verify_branch
112 [ ! -d "$GIT_DIR/patches" ] &&
113 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
115 [ ! -d "$GIT_DIR/patches/$b" ] &&
116 echo "Branch $b is not initialized, try guilt-init" >&2 &&
118 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
119 echo "Branch $b does not have a series file" >&2 &&
121 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
122 echo "Branch $b does not have a status file" >&2 &&
124 [ -f "$GIT_DIR/patches/$b/applied" ] &&
125 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
133 tail -1 "$GUILT_DIR/$branch/status" | cut
-d: -f 2-
138 local n
=`wc -l < "$GUILT_DIR/$branch/status"`
139 local n
=`expr $n - 1`
142 cat "$GUILT_DIR/$branch/status" |
while read p
; do
144 [ $idx -lt $n ] && continue
145 [ $idx -gt $n ] && break
153 # ignore all lines matching:
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?
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
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 "
182 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
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 '
198 /^From:/{skip=1; next}
199 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
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
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
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
251 local top
=`get_top | sed -e 's,/,\\\\/,g'`
252 local new
=`echo "$1" | sed -e 's,/,\\\\/,g'`
254 if [ ! -z "$top" ]; then
255 sed -i -e "s/^$top\$/$top\n$new/" "$series"
257 # don't use $new here as it would only complicate things
258 echo "$1" > "$series.tmp"
259 cat "$series" >> "$series.tmp"
260 mv "$series.tmp" "$series"
264 # usage: series_remove_patch <patchname>
265 function series_remove_patch
267 grep -v "^$1\$" < "$series" > "$series.tmp"
268 mv "$series.tmp" "$series"
271 # usage: series_rename_patch <oldname> <newname>
272 function series_rename_patch
274 local old
=`echo "$1" | sed -e 's,/,\\\\/,g'`
275 local new
=`echo "$2" | sed -e 's,/,\\\\/,g'`
277 sed -i -e "s/^$old\$/$new/" "$series"
280 # Beware! This is one of the few (only?) places where we modify the applied
283 # usage: applied_rename_patch <oldname> <newname>
284 function applied_rename_patch
286 local old
=`echo "$1" | sed -e 's,/,\\\\/,g'`
287 local new
=`echo "$2" | sed -e 's,/,\\\\/,g'`
289 sed -i -e "s/^\\([0-9a-f]\\{40\\}\\):$old\$/\\1:$new/" "$applied"
292 # usage: pop_many_patches <commitish> <number of patches>
293 function pop_many_patches
299 git-reset
--hard "$1" > /dev
/null
300 head -n "-$2" < "$applied" > "$applied.tmp"
301 mv "$applied.tmp" "$applied"
305 # update references to top, bottom, and base
309 # usage: update_stack_tags
310 function update_stack_tags
312 # bail if autotagging is not enabled
313 if [ $autotag -eq 0 ]; then
317 if [ `wc -l < $applied` -gt 0 ]; then
318 # there are patches applied, therefore we must get the top,
319 # bottom and base hashes, and update the tags
321 local top_hash
=`git-rev-parse HEAD`
322 local bottom_hash
=`head -1 < $applied | cut -d: -f1`
323 local base_hash
=`git-rev-parse $bottom_hash^`
325 echo $top_hash > "$GIT_DIR/refs/tags/${branch}_top"
326 echo $bottom_hash > "$GIT_DIR/refs/tags/${branch}_bottom"
327 echo $base_hash > "$GIT_DIR/refs/tags/${branch}_base"
329 # there are no patches applied, therefore we must remove the
330 # tags to old top, bottom, and base
332 rm -f "$GIT_DIR/refs/tags/${branch}_top"
333 rm -f "$GIT_DIR/refs/tags/${branch}_bottom"
334 rm -f "$GIT_DIR/refs/tags/${branch}_base"
338 # usage: push_patch patchname [bail_action]
341 local p
="$GUILT_DIR/$branch/$1"
343 local bail_action
="$2"
346 local reject
="--reject"
352 # apply the patch if and only if there is something to apply
353 if [ `git-apply --numstat "$p" | wc -l` -gt 0 ]; then
354 if [ "$bail_action" = abort
]; then
357 git-apply
-C$guilt_push_diff_context \
358 $reject "$p" > /dev
/null
2> /tmp
/guilt.log.$$
361 if [ $bail -ne 0 ]; then
362 cat /tmp
/guilt.log.$$
>&2
363 if [ "$bail_action" = abort
]; then
368 # FIXME: Path munging is being done, we need to convince
369 # git-apply to just give us list of files with \0 as a
370 # delimiter, and pass -z to git-update-index
371 git-apply
--numstat "$p" | cut
-f 3- | git-update-index
--add --remove --stdin
374 # grab a commit message out of the patch
375 do_get_header
"$p" > /tmp
/guilt.msg.$$
377 # make a default commit message if patch doesn't contain one
378 [ ! -s /tmp
/guilt.msg.$$
] && echo "patch $pname" > /tmp
/guilt.msg.$$
380 # extract a From line from the patch header, and set
381 # GIT_AUTHOR_{NAME,EMAIL}
382 local author_str
=`cat "$p" | grep -e '^From: ' | sed -e 's/^From: //'`
383 if [ ! -z "$author_str" ]; then
384 local backup_author_name
="$GIT_AUTHOR_NAME"
385 local backup_author_email
="$GIT_AUTHOR_EMAIL"
386 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
387 GIT_AUTHOR_EMAIL
=`echo $author_str | sed -e 's/[^<]*//'`
389 if [ -z "$GIT_AUTHOR_NAME" ]; then
393 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
395 local backup_author_date
="$GIT_AUTHOR_DATE"
396 local backup_committer_date
="$GIT_COMMITTER_DATE"
397 export GIT_AUTHOR_DATE
=`stat -c %y "$p"`
398 export GIT_COMMITTER_DATE
=$GIT_AUTHOR_DATE
401 local treeish
=`git-write-tree`
402 local commitish
=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
403 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
405 # mark patch as applied
406 echo "$commitish:$pname" >> $applied
410 # update references to top, bottom, and base of the stack
413 # restore original GIT_AUTHOR_{NAME,EMAIL}
414 if [ ! -z "$author_str" ]; then
415 if [ ! -z "$backup_author_name" ]; then
416 export GIT_AUTHOR_NAME
="$backup_author_name"
418 unset GIT_AUTHOR_NAME
421 if [ ! -z "$backup_author_name" ]; then
422 export GIT_AUTHOR_EMAIL
="$backup_author_email"
424 unset GIT_AUTHOR_EMAIL
427 if [ ! -z "$backup_author_date" ]; then
428 export GIT_AUTHOR_DATE
="$backup_author_date"
430 unset GIT_AUTHOR_DATE
432 if [ ! -z "$backup_committer_date" ]; then
433 export GIT_COMMITTER_DATE
="$backup_committer_date"
435 unset GIT_COMMITTER_DATE
438 rm -f /tmp
/guilt.msg.$$
/tmp
/guilt.log.$$
443 # usage: must_commit_first
444 function must_commit_first
446 [ `git-diff-files | wc -l` -eq 0 ]
450 # usage: fold_patch patchname
453 local top_patch
=`get_top`
459 __refresh_patch
"$top_patch" HEAD^^
2
461 series_remove_patch
"$1"
464 # usage: refresh_patch patchname
465 function refresh_patch
467 __refresh_patch
"$1" HEAD^
1
470 # usage: __refresh_patch patchname commitish number_of_commits
471 function __refresh_patch
473 local p
="$GUILT_DIR/$branch/$1"
479 git-diff-files
--name-only |
(while read n
; do git-update-index
"$n" ; done)
481 # get the patch header
482 do_get_full_header
"$p" > /tmp
/guilt.
diff.$$
485 git-diff
"$2" >> /tmp
/guilt.
diff.$$
487 # move the new patch in
489 mv /tmp
/guilt.
diff.$$
$p
493 # drop the currently applied patch, pop_many_patches does it's own
495 pop_many_patches
"$2" "$3"
497 # push_patch does it's own cd $TOP_DIR
501 # usage: munge_hash_range <hash range>
504 # <hash> - one commit
505 # <hash>.. - hash until head (excludes hash, includes head)
506 # ..<hash> - until hash (includes hash)
507 # <hash1>..<hash2> - from hash to hash (inclusive)
509 # The output of this function is suitable to be passed to git-rev-list
510 function munge_hash_range
512 local l
=`echo "$1" | sed -e 's/\.\./ /'`
514 local h1
=`echo "$l" | cut -s -d' ' -f 1`
515 local h2
=`echo "$l" | cut -s -d' ' -f 2`
517 if [ -z "$h1" -a -z "$h2" ]; then
520 elif [ -z "$h1" ]; then
523 elif [ -z "$h2" ]; then
527 # e.g., "v0.19-rc1..v0.19"
536 # used for: git-apply -C <val>
537 guilt_push_diff_context
=1
540 # Parse any part of .git/config that belongs to us
544 autotag
=`git-config guilt.autotag`
545 [ -z "$autotag" ] && autotag
=1
548 # The following gets run every time this file is source'd
551 TOP_DIR
=`git-rev-parse --show-cdup`
552 if [ -z "$TOP_DIR" ]; then
556 GUILT_DIR
="$GIT_DIR/patches"
560 # most of the time we want to verify that the repo's branch has been
561 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
562 # we must avoid the checks
563 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
564 verify_branch ||
exit 1
568 series
="$GUILT_DIR/$branch/series"
569 applied
="$GUILT_DIR/$branch/status"
571 # determine an editor to use for anything interactive (fall back to vi)
573 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
575 # determine a pager to use for anything interactive (fall back to more)
577 [ -z "$pager" ] && pager
="$PAGER"