Guilt v0.26
[guilt.git] / guilt-rebase
blobcc8dcf62129ac92f3889e45040b0c62ae3fcab81
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2007
5 # Heavily based on the long removed sh version of git-cherry
8 USAGE="<upstream>"
9 . `dirname $0`/guilt
11 case "$#" in
12 1)
13 upstream=`git-rev-parse --verify "$1"` &&
14 ours=`git-rev-parse --verify HEAD` || usage
15 limit="$upstream"
18 usage
20 esac
22 # make sure that there are no unapplied changes
23 if ! must_commit_first; then
24 die "Uncommited changes detected. Refresh first."
25 elif [ `wc -l < "$applied"` -eq 0 ]; then
26 die "Nothing to rebase. First, you need to push patches onto the stack."
29 # Note that these list commits in reverse order;
30 # not that the order in inup matters...
31 inup=`git-rev-list ^$ours $upstream` &&
32 ours=`git-rev-list $ours ^$limit` || exit
34 rebase_dir="$GUILT_DIR/$branch/.rebase.$$"
35 mkdir "$rebase_dir"
38 # calculate the patch ids for all the commits in upstream
40 for c in $inup ; do
41 git-diff-tree -p $c
42 done | git-patch-id | while read id name ; do
43 echo "$name" >> "$rebase_dir/$id"
44 done
46 # backup the status file, so we don't have to do more work to figure out all
47 # the patches that were pushed before we started rebasing
48 cp "$applied" "$rebase_dir/status"
50 echo "First, poping all patches..."
51 pop_all_patches
52 git-merge --no-commit "" HEAD $upstream > /dev/null 2> /dev/null
54 echo ""
55 log=`git-log -1 --pretty=oneline`
56 echo "HEAD is now at `echo $log | cut -c 1-7`... `echo $log | cut -c 41-`"
59 # For each previously applied patch:
60 # 1) calculate the patchid
61 # 2) if the patchid matches any of the upstream commits' patchids...
62 # a) comment out the patch from the series file
63 # 3) else
64 # a) push the patch onto the stack
66 IFS=":"
67 cat "$rebase_dir/status" | while read hash name; do
68 echo ""
69 IFS=" "
70 cat "$GUILT_DIR/$branch/$name" | git-patch-id |
71 while read patchid commitid ; do
72 echo "Applying '$name'"
73 if [ -f "$rebase_dir/$patchid" ]; then
74 realcommit=`head -1 "$rebase_dir/$patchid"`
75 echo "Matches upstream commit $realcommit"
76 series_rename_patch "$name" "###rebased###$name"
77 echo "Patch removed from series."
78 else
79 # FIXME: use a guilt function instead
80 guilt-push > /dev/null
81 echo "Patch applied."
83 done
84 IFS=":"
85 done
87 rm -rf "$rebase_dir"
89 echo ""
90 echo "Done."