]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase.sh
Merge branch 'fix'
[thirdparty/git.git] / git-rebase.sh
CommitLineData
59e6b23a
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
e646c9c8 6USAGE='[--onto <newbase>] <upstream> [<branch>]'
031321c6
SE
7LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8same name. When the --onto option is provided the new branch starts
9out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
10It then attempts to create a new commit for each commit from the original
11<branch> that does not exist in the <upstream> branch.
69a60af5 12
031321c6
SE
13It is possible that a merge failure will prevent this process from being
14completely automatic. You will have to resolve any such merge failure
15and run git-rebase --continue. If you can not resolve the merge failure,
16running git-rebase --abort will restore the original <branch> and remove
17the working files found in the .dotest directory.
69a60af5 18
031321c6
SE
19Note that if <branch> is not specified on the command line, the
20currently checked out branch is used. You must be in the top
21directory of your project to start (or continue) a rebase.
e646c9c8 22
031321c6 23Example: git-rebase master~1 topic
e646c9c8 24
031321c6
SE
25 A---B---C topic A'\''--B'\''--C'\'' topic
26 / --> /
27 D---E---F---G master D---E---F---G master
e646c9c8 28'
ae2b0f15 29. git-sh-setup
4282c4fb 30
e646c9c8
JH
31unset newbase
32while case "$#" in 0) break ;; esac
33do
34 case "$1" in
031321c6
SE
35 --continue)
36 diff=$(git-diff-files)
37 case "$diff" in
38 ?*) echo "You must edit all merge conflicts and then"
39 echo "mark them as resolved using git update-index"
40 exit 1
41 ;;
42 esac
43 git am --resolved --3way
44 exit
45 ;;
46 --abort)
47 [ -d .dotest ] || die "No rebase in progress?"
48 git reset --hard ORIG_HEAD
49 rm -r .dotest
50 exit
51 ;;
e646c9c8
JH
52 --onto)
53 test 2 -le "$#" || usage
54 newbase="$2"
55 shift
56 ;;
57 -*)
58 usage
59 ;;
60 *)
61 break
62 ;;
63 esac
64 shift
65done
2db8aaec 66
7f4bd5d8
JH
67# Make sure we do not have .dotest
68if mkdir .dotest
69then
70 rmdir .dotest
71else
72 echo >&2 '
73It seems that I cannot create a .dotest directory, and I wonder if you
74are in the middle of patch application or another rebase. If that is not
75the case, please rm -fr .dotest and run me again. I am stopping in case
76you still have something valuable there.'
77 exit 1
78fi
79
7f59dbbb 80# The tree must be really really clean.
215a7ad1 81git-update-index --refresh || exit
7f59dbbb 82diff=$(git-diff-index --cached --name-status -r HEAD)
32d99544 83case "$diff" in
7f59dbbb
JH
84?*) echo "$diff"
85 exit 1
86 ;;
87esac
99a92f92 88
e646c9c8
JH
89# The upstream head must be given. Make sure it is valid.
90upstream_name="$1"
91upstream=`git rev-parse --verify "${upstream_name}^0"` ||
d0080b3c 92 die "invalid upstream $upstream_name"
32d99544 93
9a111c91
JH
94# If a hook exists, give it a chance to interrupt
95if test -x "$GIT_DIR/hooks/pre-rebase"
96then
97 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
98 echo >&2 "The pre-rebase hook refused to rebase."
99 exit 1
100 }
101fi
102
7f59dbbb 103# If the branch to rebase is given, first switch to it.
59e6b23a 104case "$#" in
7f59dbbb 1052)
e646c9c8 106 branch_name="$2"
3ae39ab2 107 git-checkout "$2" || usage
e646c9c8
JH
108 ;;
109*)
110 branch_name=`git symbolic-ref HEAD` || die "No current branch"
f327dbce 111 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
e646c9c8 112 ;;
59e6b23a 113esac
e646c9c8 114branch=$(git-rev-parse --verify "${branch_name}^0") || exit
59e6b23a 115
e646c9c8
JH
116# Make sure the branch to rebase onto is valid.
117onto_name=${newbase-"$upstream_name"}
118onto=$(git-rev-parse --verify "${onto_name}^0") || exit
32d99544 119
e646c9c8
JH
120# Now we are rebasing commits $upstream..$branch on top of $onto
121
122# Check if we are already based on $onto, but this should be
123# done only when upstream and onto are the same.
b176e6ba 124if test "$upstream" = "$onto"
7f4bd5d8 125then
e646c9c8
JH
126 mb=$(git-merge-base "$onto" "$branch")
127 if test "$mb" = "$onto"
128 then
129 echo >&2 "Current branch $branch_name is up to date."
130 exit 0
131 fi
7f4bd5d8
JH
132fi
133
e646c9c8
JH
134# Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
135git-reset --hard "$onto"
32d99544 136
e646c9c8 137# If the $onto is a proper descendant of the tip of the branch, then
32d99544 138# we just fast forwarded.
e646c9c8 139if test "$mb" = "$onto"
32d99544 140then
e646c9c8 141 echo >&2 "Fast-forwarded $branch to $newbase."
32d99544
LS
142 exit 0
143fi
144
e646c9c8 145git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
7f59dbbb 146git am --binary -3 -k