branch: new command to create a new branch with duplicated patches directory
[guilt.git] / guilt
blob814f755bc8f7950f0ee79b6e8a1d1652335149d2
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.26-rc1"
7 GUILT_NAME="Lone Wolf"
9 # If the first argument is one of the below, display the man page instead of
10 # the rather silly and mostly useless usage string
11 case $1 in
12 -h|--h|--he|--hel|--help)
13 shift
14 exec "guilt-help" "$@"
15 exit
17 -V|--ver|--versi|--versio|--version)
18 echo "Guilt version $GUILT_VERSION"
19 exit
21 esac
23 # we change directories ourselves
24 SUBDIRECTORY_OK=1
26 . git-sh-setup
29 # Shell library
32 # echo -e is a bashism, fallback to /bin/echo if the builtin does not supports it
33 echo()
35 /bin/echo "$@"
38 noerr()
40 "$@" 2>/dev/null
43 silent()
45 "$@" >/dev/null 2>/dev/null
48 ########
50 guilt_commands()
52 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f -perm /111 | sed -e "s/.*\\/`basename $0`-//"
55 if [ "`basename $0`" = "guilt" ]; then
56 # being run as standalone
58 # by default, we shouldn't fail
59 cmd=
61 if [ $# -ne 0 ]; then
62 # take first arg, and try to execute it
64 arg="$1"
65 dir=`dirname $0`
67 if [ -x "$dir/guilt-$arg" ]; then
68 cmd=$arg
69 else
70 # might be a short handed
71 for command in $(guilt_commands); do
72 case $command in
73 $arg*)
74 if [ -x "$dir/guilt-$command" ]; then
75 cmd=$command
78 esac
79 done
81 if [ -n "$cmd" ]; then
82 shift
83 exec "$dir/guilt-$cmd" "$@"
85 # this is not reached because of the exec
86 die "Exec failed! Something is terribly wrong!"
87 else
88 echo "Command $arg not found" >&2
89 echo "" >&2
93 # no args passed or invalid command entered, just output help summary
95 echo "Guilt v$GUILT_VERSION"
96 echo ""
97 echo "Pick a command:"
98 guilt_commands | sort | column | column -t | sed -e 's/^/\t/'
100 echo ""
101 echo "Example:"
102 echo -e "\tguilt-push"
103 echo "or"
104 echo -e "\tguilt push"
106 # now, let's exit
107 exit 1
110 ########
113 # Library goodies
116 # usage: valid_patchname <patchname>
117 valid_patchname()
119 case "$1" in
120 /*|./*|../*|*/./*|*/../*|*/.|*/..|*/)
121 return 1;;
123 return 0;;
124 esac
127 get_branch()
129 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
132 verify_branch()
134 [ ! -d "$GIT_DIR/patches" ] &&
135 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
136 return 1
137 [ ! -d "$GIT_DIR/patches/$branch" ] &&
138 echo "Branch $branch is not initialized, try guilt-init" >&2 &&
139 return 1
140 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
141 echo "Branch $branch does not have a series file" >&2 &&
142 return 1
143 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
144 echo "Branch $branch does not have a status file" >&2 &&
145 return 1
146 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
147 echo "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit" >&2 &&
148 return 1
150 return 0
153 get_top()
155 tail -1 "$GUILT_DIR/$branch/status" | cut -d: -f 2-
158 get_prev()
160 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
161 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
165 get_series()
167 # ignore all lines matching:
168 # - empty lines
169 # - whitespace only
170 # - optional whitespace followed by '#' followed by more
171 # optional whitespace
172 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
175 # usage: do_make_header <hash>
176 do_make_header()
178 # we should try to work with commit objects only
179 if [ `git-cat-file -t "$1"` != "commit" ]; then
180 echo "Hash $1 is not a commit object" >&2
181 echo "Aborting..." >&2
182 exit 2
185 git-cat-file -p "$1" | awk '
186 BEGIN{headers=1; firstline=1}
187 /^author / && headers {
188 sub(/^author +/, "");
189 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
190 author=$0
192 !headers {
193 print
194 if (firstline) {
195 firstline = 0;
196 print "\nFrom: " author;
199 /^$/ && headers { headers = 0 }
203 # usage: do_get_header patchfile
204 do_get_header()
206 # The complexity arises from the fact that we want to ignore the
207 # From line and the empty line after it if it exists
209 # 2nd line skips the From line
210 # 3rd line skips the empty line right after a From line
211 do_get_full_header "$1" | awk '
212 BEGIN{skip=0}
213 /^Subject:/ && (NR==1){print substr($0, 10); next}
214 /^From:/{skip=1; next}
215 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
216 {print $0}
217 END{}
221 # usage: do_get_full_header patchfile
222 do_get_full_header()
224 # 2nd line checks for the begining of a patch
225 # 3rd line outputs the line if it didn't get pruned by the above rules
226 cat "$1" | awk '
227 BEGIN{ok=1}
228 /^(diff|---)/{ok=0}
229 (ok==1){print $0}
230 END{}
234 # usage: assert_head_check
235 assert_head_check()
237 if ! head_check "`tail -1 < "$applied" | cut -d: -f 1`"; then
238 die "aborting..."
242 # usage: head_check <expected hash>
243 head_check()
245 # make sure we're not doing funky things to commits that don't
246 # belong to us
247 # if the expected hash is empty, just return
248 [ -z "$1" ] && return 0
250 if [ "`cat "$GIT_DIR/refs/heads/$branch"`" != "$1" ]; then
251 echo "Expected HEAD commit $1" >&2
252 echo " got `cat "$GIT_DIR/refs/heads/$branch"`" >&2
253 return 1
255 return 0
258 # usage: series_insert_patch <patchname>
259 series_insert_patch()
261 awk -v top="`get_top`" -v new="$1" \
262 'BEGIN{if (top == "") print new;}
264 print $0;
265 if (top != "" && top == $0) print new;
266 }' "$series" > "$series.tmp"
267 mv "$series.tmp" "$series"
270 # usage: series_remove_patch <patchname>
271 series_remove_patch()
273 grep -v "^$1\$" < "$series" > "$series.tmp"
274 mv "$series.tmp" "$series"
277 # usage: series_rename_patch <oldname> <newname>
278 series_rename_patch()
280 awk -v old="$1" -v new="$2" \
281 '{ if ($0 == old) print new; else print $0 }' \
282 "$series" > "$series.tmp"
284 mv "$series.tmp" "$series"
287 # Beware! This is one of the few (only?) places where we modify the applied
288 # file directly
290 # usage: applied_rename_patch <oldname> <newname>
291 applied_rename_patch()
293 awk -v old="$1" -v new="$2" \
294 'BEGIN{FS=":"}
295 { if ($1 ~ /^[0-9a-f]*$/ && length($1) == 40 && substr($0, 42) == old)
296 print substr($0, 0, 41) new;
297 else
298 print;
299 }' "$applied" > "$applied.tmp"
301 mv "$applied.tmp" "$applied"
304 # usage: pop_many_patches <commitish> <number of patches>
305 pop_many_patches()
307 assert_head_check
310 cd "$TOP_DIR"
312 git-reset --hard "$1" > /dev/null
313 head -n "-$2" < "$applied" > "$applied.tmp"
314 mv "$applied.tmp" "$applied"
317 # update references to top, bottom, and base
318 update_stack_tags
321 # usage: pop_all_patches
322 pop_all_patches()
324 pop_many_patches \
325 `head -1 "$applied" | cut -d: -f1`^ \
326 `wc -l < "$applied"`
329 # usage: update_stack_tags
330 update_stack_tags()
332 # bail if autotagging is not enabled
333 if [ $autotag -eq 0 ]; then
334 return 0
337 if [ `wc -l < $applied` -gt 0 ]; then
338 # there are patches applied, therefore we must get the top,
339 # bottom and base hashes, and update the tags
341 git-rev-parse HEAD > "$GIT_DIR/refs/tags/${branch}_top"
342 head -1 < $applied | cut -d: -f1 > "$GIT_DIR/refs/tags/${branch}_bottom"
343 git-rev-parse $(head -1 < $applied | cut -d: -f1)^ > "$GIT_DIR/refs/tags/${branch}_base"
344 else
345 # there are no patches applied, therefore we must remove the
346 # tags to old top, bottom, and base
348 rm -f "$GIT_DIR/refs/tags/${branch}_top"
349 rm -f "$GIT_DIR/refs/tags/${branch}_bottom"
350 rm -f "$GIT_DIR/refs/tags/${branch}_base"
354 # usage: push_patch patchname [bail_action]
355 push_patch()
357 __push_patch_bail=0
360 p="$GUILT_DIR/$branch/$1"
361 pname="$1"
362 bail_action="$2"
363 reject="--reject"
365 assert_head_check
366 cd "$TOP_DIR"
368 # apply the patch if and only if there is something to apply
369 if [ `git-apply --numstat "$p" | wc -l` -gt 0 ]; then
370 if [ "$bail_action" = abort ]; then
371 reject=""
373 git-apply -C$guilt_push_diff_context \
374 $reject "$p" > /dev/null 2> /tmp/guilt.log.$$
375 __push_patch_bail=$?
377 if [ $__push_patch_bail -ne 0 ]; then
378 cat /tmp/guilt.log.$$ >&2
379 if [ "$bail_action" = "abort" ]; then
380 rm -f /tmp/guilt.log.$$ /tmp/guilt.msg.$$
381 return $__push_patch_bail
385 # FIXME: Path munging is being done, we need to convince
386 # git-apply to just give us list of files with \0 as a
387 # delimiter, and pass -z to git-update-index
388 git-apply --numstat "$p" | cut -f 3- | git-update-index --add --remove --stdin
391 # grab a commit message out of the patch
392 do_get_header "$p" > /tmp/guilt.msg.$$
394 # make a default commit message if patch doesn't contain one
395 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
397 # extract a From line from the patch header, and set
398 # GIT_AUTHOR_{NAME,EMAIL}
399 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q }' "$p"`
400 if [ ! -z "$author_str" ]; then
401 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
402 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
403 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
405 export GIT_AUTHOR_DATE="`stat -c %y "$p"`"
406 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
408 # commit
409 treeish=`git-write-tree`
410 commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
411 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
413 # mark patch as applied
414 echo "$commitish:$pname" >> $applied
417 # update references to top, bottom, and base of the stack
418 update_stack_tags
420 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
421 return $__push_patch_bail
424 # usage: must_commit_first
425 must_commit_first()
427 [ `git-diff-files | wc -l` -eq 0 ]
428 return $?
431 # usage: fold_patch patchname
432 fold_patch()
434 set -- "$1" "`get_top`"
436 assert_head_check
438 push_patch "$1"
440 __refresh_patch "$2" HEAD^^ 2 ""
442 series_remove_patch "$1"
445 # usage: refresh_patch patchname gengitdiff
446 refresh_patch()
448 __refresh_patch "$1" HEAD^ 1 "$2"
451 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
452 __refresh_patch()
454 assert_head_check
457 cd "$TOP_DIR"
458 p="$GUILT_DIR/$branch/$1"
460 git-diff-files --name-only | (while read n; do git-update-index "$n" ; done)
462 # get the patch header
463 do_get_full_header "$p" > /tmp/guilt.diff.$$
465 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
467 # get the new patch
468 git-diff $diffopts "$2" >> /tmp/guilt.diff.$$
470 # move the new patch in
471 mv "$p" "$p~"
472 mv /tmp/guilt.diff.$$ $p
475 # drop the currently applied patch, pop_many_patches does it's own
476 # cd $TOP_DIR
477 pop_many_patches "$2" "$3"
479 # push_patch does it's own cd $TOP_DIR
480 push_patch "$1"
483 # usage: munge_hash_range <hash range>
485 # this means:
486 # <hash> - one commit
487 # <hash>.. - hash until head (excludes hash, includes head)
488 # ..<hash> - until hash (includes hash)
489 # <hash1>..<hash2> - from hash to hash (inclusive)
491 # The output of this function is suitable to be passed to git-rev-list
492 munge_hash_range()
494 case "$1" in
495 *..*..*|*\ *)
496 # double .. or space is illegal
497 return 1;;
498 ..*)
499 # e.g., "..v0.10"
500 echo ${1#..};;
501 *..)
502 # e.g., "v0.19.."
503 echo ${1%..}..HEAD;;
504 *..*)
505 # e.g., "v0.19-rc1..v0.19"
506 echo ${1%%..*}..${1#*..};;
508 # e.g., "v0.19"
509 echo $1^..$1;;
510 *) # empty
511 return 1;;
512 esac
513 return 0
517 # Some constants
520 # used for: git-apply -C <val>
521 guilt_push_diff_context=1
524 # Parse any part of .git/config that belongs to us
527 # autotag?
528 autotag=`git-config guilt.autotag`
529 [ -z "$autotag" ] && autotag=1
532 # The following gets run every time this file is source'd
535 TOP_DIR=`git-rev-parse --show-cdup`
536 if [ -z "$TOP_DIR" ]; then
537 TOP_DIR="./"
540 GUILT_DIR="$GIT_DIR/patches"
542 branch=`get_branch`
544 # most of the time we want to verify that the repo's branch has been
545 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
546 # we must avoid the checks
547 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
548 verify_branch || exit 1
551 # very useful files
552 series="$GUILT_DIR/$branch/series"
553 applied="$GUILT_DIR/$branch/status"
555 # determine an editor to use for anything interactive (fall back to vi)
556 editor="vi"
557 [ ! -z "$EDITOR" ] && editor="$EDITOR"
559 # determine a pager to use for anything interactive (fall back to more)
560 pager="more"
561 [ ! -z "$PAGER" ] && pager="$PAGER"