2005-06-25 11:23:43 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Copyright (c) 2005 Junio C Hamano.
|
|
|
|
#
|
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
USAGE='[--onto <newbase>] <upstream> [<branch>]'
|
2006-02-22 02:10:12 +01:00
|
|
|
LONG_USAGE='git-rebase applies to <upstream> (or optionally to <newbase>) commits
|
|
|
|
from <branch> that do not appear in <upstream>. When <branch> is not
|
|
|
|
specified it defaults to the current branch (HEAD).
|
|
|
|
|
|
|
|
When git-rebase is complete, <branch> will be updated to point to the
|
|
|
|
newly created line of commit objects, so the previous line will not be
|
|
|
|
accessible unless there are other references to it already.
|
|
|
|
|
|
|
|
Assuming the following history:
|
2006-02-14 23:42:05 +01:00
|
|
|
|
|
|
|
A---B---C topic
|
|
|
|
/
|
|
|
|
D---E---F---G master
|
|
|
|
|
2006-02-22 02:10:12 +01:00
|
|
|
The result of the following command:
|
2006-02-14 23:42:05 +01:00
|
|
|
|
2006-02-22 02:10:12 +01:00
|
|
|
git-rebase --onto master~1 master topic
|
2006-02-14 23:42:05 +01:00
|
|
|
|
2006-02-22 02:10:12 +01:00
|
|
|
would be:
|
2006-02-14 23:42:05 +01:00
|
|
|
|
2006-02-22 02:10:12 +01:00
|
|
|
A'\''--B'\''--C'\'' topic
|
|
|
|
/
|
2006-02-14 23:42:05 +01:00
|
|
|
D---E---F---G master
|
|
|
|
'
|
|
|
|
|
2005-11-24 09:12:11 +01:00
|
|
|
. git-sh-setup
|
2005-08-08 00:51:09 +02:00
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
unset newbase
|
|
|
|
while case "$#" in 0) break ;; esac
|
|
|
|
do
|
|
|
|
case "$1" in
|
|
|
|
--onto)
|
|
|
|
test 2 -le "$#" || usage
|
|
|
|
newbase="$2"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2005-12-14 12:11:37 +01:00
|
|
|
|
2005-11-28 22:00:31 +01:00
|
|
|
# Make sure we do not have .dotest
|
|
|
|
if mkdir .dotest
|
|
|
|
then
|
|
|
|
rmdir .dotest
|
|
|
|
else
|
|
|
|
echo >&2 '
|
|
|
|
It seems that I cannot create a .dotest directory, and I wonder if you
|
|
|
|
are in the middle of patch application or another rebase. If that is not
|
|
|
|
the case, please rm -fr .dotest and run me again. I am stopping in case
|
|
|
|
you still have something valuable there.'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
Rewrite rebase to use git-format-patch piped to git-am.
The current rebase implementation finds commits in our tree but
not in the upstream tree using git-cherry, and tries to apply
them using git-cherry-pick (i.e. always use 3-way) one by one.
Which is fine, but when some of the changes do not apply
cleanly, it punts, and punts badly.
Suppose you have commits A-B-C-D-E since you forked from the
upstream and submitted the changes for inclusion. You fetch
from upstream head U and find that B has been picked up. You
run git-rebase to update your branch, which tries to apply
changes contained in A-C-D-E, in this order, but replaying of C
fails, because the upstream got changes that touch the same area
from elsewhere.
Now what?
It notes that fact, and goes ahead to apply D and E, and at the
very end tells you to deal with C by hand. Even if you somehow
managed to replay C on top of the result, you would now end up
with ...-B-...-U-A-D-E-C.
Breaking the order between B and others was the conscious
decision made by the upstream, so we would not worry about it,
and even if it were worrisome, it is too late for us to fix now.
What D and E do may well depend on having C applied before them,
which is a problem for us.
This rewrites rebase to use git-format-patch piped to git-am,
and when the patch does not apply, have git-am fall back on
3-way merge. The updated diff/patch pair knows how to apply
trivial binary patches as long as the pre- and post-images are
locally available, so this should work on a repository with
binary files as well.
The primary benefit of this change is that it makes rebase
easier to use when some of the changes do not replay cleanly.
In the "unapplicable patch in the middle" case, this "rebase"
works like this:
- A series of patches in e-mail form is created that records
what A-C-D-E do, and is fed to git-am. This is stored in
.dotest/ directory, just like the case you tried to apply
them from your mailbox. Your branch is rewound to the tip of
upstream U, and the original head is kept in .git/ORIG_HEAD,
so you could "git reset --hard ORIG_HEAD" in case the end
result is really messy.
- Patch A applies cleanly. This could either be a clean patch
application on top of rewound head (i.e. same as upstream
head), or git-am might have internally fell back on 3-way
(i.e. it would have done the same thing as git-cherry-pick).
In either case, a rebased commit A is made on top of U.
- Patch C does not apply. git-am stops here, with conflicts to
be resolved in the working tree. Yet-to-be-applied D and E
are still kept in .dotest/ directory at this point. What the
user does is exactly the same as fixing up unapplicable patch
when running git-am:
- Resolve conflict just like any merge conflicts.
- "git am --resolved --3way" to continue applying the patches.
- This applies the fixed-up patch so by definition it had
better apply. "git am" knows the patch after the fixed-up
one is D and then E; it applies them, and you will get the
changes from A-C-D-E commits on top of U, in this order.
I've been using this without noticing any problem, and as people
may know I do a lot of rebases.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-14 09:41:53 +01:00
|
|
|
# The tree must be really really clean.
|
2005-09-08 02:26:23 +02:00
|
|
|
git-update-index --refresh || exit
|
Rewrite rebase to use git-format-patch piped to git-am.
The current rebase implementation finds commits in our tree but
not in the upstream tree using git-cherry, and tries to apply
them using git-cherry-pick (i.e. always use 3-way) one by one.
Which is fine, but when some of the changes do not apply
cleanly, it punts, and punts badly.
Suppose you have commits A-B-C-D-E since you forked from the
upstream and submitted the changes for inclusion. You fetch
from upstream head U and find that B has been picked up. You
run git-rebase to update your branch, which tries to apply
changes contained in A-C-D-E, in this order, but replaying of C
fails, because the upstream got changes that touch the same area
from elsewhere.
Now what?
It notes that fact, and goes ahead to apply D and E, and at the
very end tells you to deal with C by hand. Even if you somehow
managed to replay C on top of the result, you would now end up
with ...-B-...-U-A-D-E-C.
Breaking the order between B and others was the conscious
decision made by the upstream, so we would not worry about it,
and even if it were worrisome, it is too late for us to fix now.
What D and E do may well depend on having C applied before them,
which is a problem for us.
This rewrites rebase to use git-format-patch piped to git-am,
and when the patch does not apply, have git-am fall back on
3-way merge. The updated diff/patch pair knows how to apply
trivial binary patches as long as the pre- and post-images are
locally available, so this should work on a repository with
binary files as well.
The primary benefit of this change is that it makes rebase
easier to use when some of the changes do not replay cleanly.
In the "unapplicable patch in the middle" case, this "rebase"
works like this:
- A series of patches in e-mail form is created that records
what A-C-D-E do, and is fed to git-am. This is stored in
.dotest/ directory, just like the case you tried to apply
them from your mailbox. Your branch is rewound to the tip of
upstream U, and the original head is kept in .git/ORIG_HEAD,
so you could "git reset --hard ORIG_HEAD" in case the end
result is really messy.
- Patch A applies cleanly. This could either be a clean patch
application on top of rewound head (i.e. same as upstream
head), or git-am might have internally fell back on 3-way
(i.e. it would have done the same thing as git-cherry-pick).
In either case, a rebased commit A is made on top of U.
- Patch C does not apply. git-am stops here, with conflicts to
be resolved in the working tree. Yet-to-be-applied D and E
are still kept in .dotest/ directory at this point. What the
user does is exactly the same as fixing up unapplicable patch
when running git-am:
- Resolve conflict just like any merge conflicts.
- "git am --resolved --3way" to continue applying the patches.
- This applies the fixed-up patch so by definition it had
better apply. "git am" knows the patch after the fixed-up
one is D and then E; it applies them, and you will get the
changes from A-C-D-E commits on top of U, in this order.
I've been using this without noticing any problem, and as people
may know I do a lot of rebases.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-14 09:41:53 +01:00
|
|
|
diff=$(git-diff-index --cached --name-status -r HEAD)
|
2005-12-15 00:36:35 +01:00
|
|
|
case "$diff" in
|
Rewrite rebase to use git-format-patch piped to git-am.
The current rebase implementation finds commits in our tree but
not in the upstream tree using git-cherry, and tries to apply
them using git-cherry-pick (i.e. always use 3-way) one by one.
Which is fine, but when some of the changes do not apply
cleanly, it punts, and punts badly.
Suppose you have commits A-B-C-D-E since you forked from the
upstream and submitted the changes for inclusion. You fetch
from upstream head U and find that B has been picked up. You
run git-rebase to update your branch, which tries to apply
changes contained in A-C-D-E, in this order, but replaying of C
fails, because the upstream got changes that touch the same area
from elsewhere.
Now what?
It notes that fact, and goes ahead to apply D and E, and at the
very end tells you to deal with C by hand. Even if you somehow
managed to replay C on top of the result, you would now end up
with ...-B-...-U-A-D-E-C.
Breaking the order between B and others was the conscious
decision made by the upstream, so we would not worry about it,
and even if it were worrisome, it is too late for us to fix now.
What D and E do may well depend on having C applied before them,
which is a problem for us.
This rewrites rebase to use git-format-patch piped to git-am,
and when the patch does not apply, have git-am fall back on
3-way merge. The updated diff/patch pair knows how to apply
trivial binary patches as long as the pre- and post-images are
locally available, so this should work on a repository with
binary files as well.
The primary benefit of this change is that it makes rebase
easier to use when some of the changes do not replay cleanly.
In the "unapplicable patch in the middle" case, this "rebase"
works like this:
- A series of patches in e-mail form is created that records
what A-C-D-E do, and is fed to git-am. This is stored in
.dotest/ directory, just like the case you tried to apply
them from your mailbox. Your branch is rewound to the tip of
upstream U, and the original head is kept in .git/ORIG_HEAD,
so you could "git reset --hard ORIG_HEAD" in case the end
result is really messy.
- Patch A applies cleanly. This could either be a clean patch
application on top of rewound head (i.e. same as upstream
head), or git-am might have internally fell back on 3-way
(i.e. it would have done the same thing as git-cherry-pick).
In either case, a rebased commit A is made on top of U.
- Patch C does not apply. git-am stops here, with conflicts to
be resolved in the working tree. Yet-to-be-applied D and E
are still kept in .dotest/ directory at this point. What the
user does is exactly the same as fixing up unapplicable patch
when running git-am:
- Resolve conflict just like any merge conflicts.
- "git am --resolved --3way" to continue applying the patches.
- This applies the fixed-up patch so by definition it had
better apply. "git am" knows the patch after the fixed-up
one is D and then E; it applies them, and you will get the
changes from A-C-D-E commits on top of U, in this order.
I've been using this without noticing any problem, and as people
may know I do a lot of rebases.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-14 09:41:53 +01:00
|
|
|
?*) echo "$diff"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
2005-08-18 00:19:57 +02:00
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
# The upstream head must be given. Make sure it is valid.
|
|
|
|
upstream_name="$1"
|
|
|
|
upstream=`git rev-parse --verify "${upstream_name}^0"` ||
|
2006-02-21 21:56:14 +01:00
|
|
|
die "invalid upstream $upstream_name"
|
2005-12-15 00:36:35 +01:00
|
|
|
|
2006-02-13 08:17:04 +01:00
|
|
|
# If a hook exists, give it a chance to interrupt
|
|
|
|
if test -x "$GIT_DIR/hooks/pre-rebase"
|
|
|
|
then
|
|
|
|
"$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
|
|
|
|
echo >&2 "The pre-rebase hook refused to rebase."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
Rewrite rebase to use git-format-patch piped to git-am.
The current rebase implementation finds commits in our tree but
not in the upstream tree using git-cherry, and tries to apply
them using git-cherry-pick (i.e. always use 3-way) one by one.
Which is fine, but when some of the changes do not apply
cleanly, it punts, and punts badly.
Suppose you have commits A-B-C-D-E since you forked from the
upstream and submitted the changes for inclusion. You fetch
from upstream head U and find that B has been picked up. You
run git-rebase to update your branch, which tries to apply
changes contained in A-C-D-E, in this order, but replaying of C
fails, because the upstream got changes that touch the same area
from elsewhere.
Now what?
It notes that fact, and goes ahead to apply D and E, and at the
very end tells you to deal with C by hand. Even if you somehow
managed to replay C on top of the result, you would now end up
with ...-B-...-U-A-D-E-C.
Breaking the order between B and others was the conscious
decision made by the upstream, so we would not worry about it,
and even if it were worrisome, it is too late for us to fix now.
What D and E do may well depend on having C applied before them,
which is a problem for us.
This rewrites rebase to use git-format-patch piped to git-am,
and when the patch does not apply, have git-am fall back on
3-way merge. The updated diff/patch pair knows how to apply
trivial binary patches as long as the pre- and post-images are
locally available, so this should work on a repository with
binary files as well.
The primary benefit of this change is that it makes rebase
easier to use when some of the changes do not replay cleanly.
In the "unapplicable patch in the middle" case, this "rebase"
works like this:
- A series of patches in e-mail form is created that records
what A-C-D-E do, and is fed to git-am. This is stored in
.dotest/ directory, just like the case you tried to apply
them from your mailbox. Your branch is rewound to the tip of
upstream U, and the original head is kept in .git/ORIG_HEAD,
so you could "git reset --hard ORIG_HEAD" in case the end
result is really messy.
- Patch A applies cleanly. This could either be a clean patch
application on top of rewound head (i.e. same as upstream
head), or git-am might have internally fell back on 3-way
(i.e. it would have done the same thing as git-cherry-pick).
In either case, a rebased commit A is made on top of U.
- Patch C does not apply. git-am stops here, with conflicts to
be resolved in the working tree. Yet-to-be-applied D and E
are still kept in .dotest/ directory at this point. What the
user does is exactly the same as fixing up unapplicable patch
when running git-am:
- Resolve conflict just like any merge conflicts.
- "git am --resolved --3way" to continue applying the patches.
- This applies the fixed-up patch so by definition it had
better apply. "git am" knows the patch after the fixed-up
one is D and then E; it applies them, and you will get the
changes from A-C-D-E commits on top of U, in this order.
I've been using this without noticing any problem, and as people
may know I do a lot of rebases.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-14 09:41:53 +01:00
|
|
|
# If the branch to rebase is given, first switch to it.
|
2005-06-25 11:23:43 +02:00
|
|
|
case "$#" in
|
Rewrite rebase to use git-format-patch piped to git-am.
The current rebase implementation finds commits in our tree but
not in the upstream tree using git-cherry, and tries to apply
them using git-cherry-pick (i.e. always use 3-way) one by one.
Which is fine, but when some of the changes do not apply
cleanly, it punts, and punts badly.
Suppose you have commits A-B-C-D-E since you forked from the
upstream and submitted the changes for inclusion. You fetch
from upstream head U and find that B has been picked up. You
run git-rebase to update your branch, which tries to apply
changes contained in A-C-D-E, in this order, but replaying of C
fails, because the upstream got changes that touch the same area
from elsewhere.
Now what?
It notes that fact, and goes ahead to apply D and E, and at the
very end tells you to deal with C by hand. Even if you somehow
managed to replay C on top of the result, you would now end up
with ...-B-...-U-A-D-E-C.
Breaking the order between B and others was the conscious
decision made by the upstream, so we would not worry about it,
and even if it were worrisome, it is too late for us to fix now.
What D and E do may well depend on having C applied before them,
which is a problem for us.
This rewrites rebase to use git-format-patch piped to git-am,
and when the patch does not apply, have git-am fall back on
3-way merge. The updated diff/patch pair knows how to apply
trivial binary patches as long as the pre- and post-images are
locally available, so this should work on a repository with
binary files as well.
The primary benefit of this change is that it makes rebase
easier to use when some of the changes do not replay cleanly.
In the "unapplicable patch in the middle" case, this "rebase"
works like this:
- A series of patches in e-mail form is created that records
what A-C-D-E do, and is fed to git-am. This is stored in
.dotest/ directory, just like the case you tried to apply
them from your mailbox. Your branch is rewound to the tip of
upstream U, and the original head is kept in .git/ORIG_HEAD,
so you could "git reset --hard ORIG_HEAD" in case the end
result is really messy.
- Patch A applies cleanly. This could either be a clean patch
application on top of rewound head (i.e. same as upstream
head), or git-am might have internally fell back on 3-way
(i.e. it would have done the same thing as git-cherry-pick).
In either case, a rebased commit A is made on top of U.
- Patch C does not apply. git-am stops here, with conflicts to
be resolved in the working tree. Yet-to-be-applied D and E
are still kept in .dotest/ directory at this point. What the
user does is exactly the same as fixing up unapplicable patch
when running git-am:
- Resolve conflict just like any merge conflicts.
- "git am --resolved --3way" to continue applying the patches.
- This applies the fixed-up patch so by definition it had
better apply. "git am" knows the patch after the fixed-up
one is D and then E; it applies them, and you will get the
changes from A-C-D-E commits on top of U, in this order.
I've been using this without noticing any problem, and as people
may know I do a lot of rebases.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-14 09:41:53 +01:00
|
|
|
2)
|
2006-02-14 23:42:05 +01:00
|
|
|
branch_name="$2"
|
2005-12-13 23:30:32 +01:00
|
|
|
git-checkout "$2" || usage
|
2006-02-14 23:42:05 +01:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
branch_name=`git symbolic-ref HEAD` || die "No current branch"
|
2006-04-14 00:01:24 +02:00
|
|
|
branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
|
2006-02-14 23:42:05 +01:00
|
|
|
;;
|
2005-06-25 11:23:43 +02:00
|
|
|
esac
|
2006-02-14 23:42:05 +01:00
|
|
|
branch=$(git-rev-parse --verify "${branch_name}^0") || exit
|
2005-06-25 11:23:43 +02:00
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
# Make sure the branch to rebase onto is valid.
|
|
|
|
onto_name=${newbase-"$upstream_name"}
|
|
|
|
onto=$(git-rev-parse --verify "${onto_name}^0") || exit
|
2005-12-15 00:36:35 +01:00
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
# Now we are rebasing commits $upstream..$branch on top of $onto
|
|
|
|
|
|
|
|
# Check if we are already based on $onto, but this should be
|
|
|
|
# done only when upstream and onto are the same.
|
|
|
|
if test "$upstream" = "onto"
|
2005-11-28 22:00:31 +01:00
|
|
|
then
|
2006-02-14 23:42:05 +01:00
|
|
|
mb=$(git-merge-base "$onto" "$branch")
|
|
|
|
if test "$mb" = "$onto"
|
|
|
|
then
|
|
|
|
echo >&2 "Current branch $branch_name is up to date."
|
|
|
|
exit 0
|
|
|
|
fi
|
2005-11-28 22:00:31 +01:00
|
|
|
fi
|
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
# Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
|
|
|
|
git-reset --hard "$onto"
|
2005-12-15 00:36:35 +01:00
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
# If the $onto is a proper descendant of the tip of the branch, then
|
2005-12-15 00:36:35 +01:00
|
|
|
# we just fast forwarded.
|
2006-02-14 23:42:05 +01:00
|
|
|
if test "$mb" = "$onto"
|
2005-12-15 00:36:35 +01:00
|
|
|
then
|
2006-02-14 23:42:05 +01:00
|
|
|
echo >&2 "Fast-forwarded $branch to $newbase."
|
2005-12-15 00:36:35 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2006-02-14 23:42:05 +01:00
|
|
|
git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
|
Rewrite rebase to use git-format-patch piped to git-am.
The current rebase implementation finds commits in our tree but
not in the upstream tree using git-cherry, and tries to apply
them using git-cherry-pick (i.e. always use 3-way) one by one.
Which is fine, but when some of the changes do not apply
cleanly, it punts, and punts badly.
Suppose you have commits A-B-C-D-E since you forked from the
upstream and submitted the changes for inclusion. You fetch
from upstream head U and find that B has been picked up. You
run git-rebase to update your branch, which tries to apply
changes contained in A-C-D-E, in this order, but replaying of C
fails, because the upstream got changes that touch the same area
from elsewhere.
Now what?
It notes that fact, and goes ahead to apply D and E, and at the
very end tells you to deal with C by hand. Even if you somehow
managed to replay C on top of the result, you would now end up
with ...-B-...-U-A-D-E-C.
Breaking the order between B and others was the conscious
decision made by the upstream, so we would not worry about it,
and even if it were worrisome, it is too late for us to fix now.
What D and E do may well depend on having C applied before them,
which is a problem for us.
This rewrites rebase to use git-format-patch piped to git-am,
and when the patch does not apply, have git-am fall back on
3-way merge. The updated diff/patch pair knows how to apply
trivial binary patches as long as the pre- and post-images are
locally available, so this should work on a repository with
binary files as well.
The primary benefit of this change is that it makes rebase
easier to use when some of the changes do not replay cleanly.
In the "unapplicable patch in the middle" case, this "rebase"
works like this:
- A series of patches in e-mail form is created that records
what A-C-D-E do, and is fed to git-am. This is stored in
.dotest/ directory, just like the case you tried to apply
them from your mailbox. Your branch is rewound to the tip of
upstream U, and the original head is kept in .git/ORIG_HEAD,
so you could "git reset --hard ORIG_HEAD" in case the end
result is really messy.
- Patch A applies cleanly. This could either be a clean patch
application on top of rewound head (i.e. same as upstream
head), or git-am might have internally fell back on 3-way
(i.e. it would have done the same thing as git-cherry-pick).
In either case, a rebased commit A is made on top of U.
- Patch C does not apply. git-am stops here, with conflicts to
be resolved in the working tree. Yet-to-be-applied D and E
are still kept in .dotest/ directory at this point. What the
user does is exactly the same as fixing up unapplicable patch
when running git-am:
- Resolve conflict just like any merge conflicts.
- "git am --resolved --3way" to continue applying the patches.
- This applies the fixed-up patch so by definition it had
better apply. "git am" knows the patch after the fixed-up
one is D and then E; it applies them, and you will get the
changes from A-C-D-E commits on top of U, in this order.
I've been using this without noticing any problem, and as people
may know I do a lot of rebases.
Signed-off-by: Junio C Hamano <junkio@cox.net>
2005-11-14 09:41:53 +01:00
|
|
|
git am --binary -3 -k
|