filter-branch: Big syntax change; support rewriting multiple refs
We used to take the first non-option argument as the name for the new
branch. This syntax is not extensible to support rewriting more than just
HEAD.
Instead, we now have the following syntax:
git filter-branch [<filter options>...] [<rev-list options>]
All positive refs given in <rev-list options> are rewritten. Yes,
in-place. If a ref was changed, the original head is stored in
refs/original/$ref now, for your inspecting pleasure, in addition to the
reflogs (since it is easier to inspect "git show-ref | grep original" than
to inspect all the reflogs).
This commit also adds the --force option to remove .git-rewrite/ and all
refs from refs/original/ before filtering.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Johannes Schindelin18 years agocommitted byJunio C Hamano
@ -26,10 +26,9 @@ information) will be preserved.
@@ -26,10 +26,9 @@ information) will be preserved.
The command takes the new branch name as a mandatory argument and
the filters as optional arguments. If you specify no filters, the
commits will be recommitted without any changes, which would normally
have no effect and result in the new branch pointing to the same
branch as your current branch. Nevertheless, this may be useful in
the future for compensating for some git bugs or such, therefore
such a usage is permitted.
have no effect. Nevertheless, this may be useful in the future for
compensating for some git bugs or such, therefore such a usage is
permitted.
*WARNING*! The rewritten history will have different object names for all
the objects and will not converge with the original branch. You will not
@ -38,8 +37,9 @@ original branch. Please do not use this command if you do not know the
@@ -38,8 +37,9 @@ original branch. Please do not use this command if you do not know the
full implications, and avoid using it anyway, if a simple single commit
would suffice to fix your problem.
Always verify that the rewritten version is correct before disposing
the original branch.
Always verify that the rewritten version is correct: The original refs,
if different from the rewritten ones, will be stored in the namespace
'refs/original/'.
Note that since this operation is extensively I/O expensive, it might
be a good idea to redirect the temporary directory off-disk, e.g. on
@ -142,6 +142,11 @@ definition impossible to preserve signatures at any rate.)
@@ -142,6 +142,11 @@ definition impossible to preserve signatures at any rate.)
does this in the '.git-rewrite/' directory but you can override
that choice by this parameter.
-f\|--force::
`git filter-branch` refuses to start with an existing temporary
directory or when there are already refs starting with
'refs/original/', unless forced.
<rev-list-options>::
When options are given after the new branch name, they will
be passed to gitlink:git-rev-list[1]. Only commits in the resulting
@ -156,14 +161,14 @@ Suppose you want to remove a file (containing confidential information
@@ -156,14 +161,14 @@ Suppose you want to remove a file (containing confidential information
Now, you will get the rewritten history saved in the branch 'newbranch'
(your current branch is left untouched).
@ -172,25 +177,25 @@ To set a commit (which typically is at the tip of another
@@ -172,25 +177,25 @@ To set a commit (which typically is at the tip of another
history) to be the parent of the current initial commit, in
order to paste the other history behind the current history:
die "Could not update $dstbranch with $target_head"
if [ $(wc -l <../map/$src_head) -gt 1 ]; then
echo "WARNING: Your commit filter caused the head commit to expand to several rewritten commits. Only the first such commit was recorded as the current $dstbranch head but you will need to resolve the situation now (probably by manually merging the other commits). These are all the commits:" >&2
sed 's/^/ /' ../map/$src_head >&2
ret=1
fi
$_x40)
echo "Ref '$ref' was rewritten"
git update-ref -m "filter-branch: rewrite" \
"$ref" $rewritten $sha1 ||
die "Could not rewrite $ref"
;;
esac
*)
# NEEDSWORK: possibly add -Werror, making this an error
warn "WARNING: '$ref' was rewritten into multiple commits:"
warn "$rewritten"
warn "WARNING: Ref '$ref' points to the first one now."
rewritten=$(echo "$rewritten" | head -n 1)
git update-ref -m "filter-branch: rewrite to first" \
test_expect_success 'subdirectory filter result looks okay' '
@ -89,7 +90,8 @@ test_expect_success 'setup and filter history that requires --full-history' '
@@ -89,7 +90,8 @@ test_expect_success 'setup and filter history that requires --full-history' '
test_expect_success 'use index-filter to move into a subdirectory' '
git-filter-branch --index-filter \
git branch directorymoved &&
git-filter-branch -f --index-filter \
"git ls-files -s | sed \"s-\\t-&newsubdir/-\" |
GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
git update-index --index-info &&
@ -108,9 +111,10 @@ test_expect_success 'use index-filter to move into a subdirectory' '
@@ -108,9 +111,10 @@ test_expect_success 'use index-filter to move into a subdirectory' '
test -z "$(git diff HEAD directorymoved:newsubdir)"'
test_expect_success 'stops when msg filter fails' '
! git-filter-branch --msg-filter false nonono &&
rm -rf .git-rewrite &&
! git rev-parse nonono
old=$(git rev-parse HEAD) &&
! git-filter-branch -f --msg-filter false &&
test $old = $(git rev-parse HEAD) &&
rm -rf .git-rewrite
'
test_expect_success 'author information is preserved' '
@ -118,7 +122,8 @@ test_expect_success 'author information is preserved' '
@@ -118,7 +122,8 @@ test_expect_success 'author information is preserved' '
git add i &&
test_tick &&
GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
git-filter-branch --msg-filter "cat; \
git branch preserved-author &&
git-filter-branch -f --msg-filter "cat; \
test \$GIT_COMMIT != $(git rev-parse master) || \
echo Hallo" \
preserved-author &&
@ -129,7 +134,8 @@ test_expect_success "remove a certain author's commits" '
@@ -129,7 +134,8 @@ test_expect_success "remove a certain author's commits" '
echo i > i &&
test_tick &&
git commit -m i i &&
git-filter-branch --commit-filter "\
git branch removed-author &&
git-filter-branch -f --commit-filter "\
if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
then\
shift;\
@ -148,4 +154,9 @@ test_expect_success "remove a certain author's commits" '
@@ -148,4 +154,9 @@ test_expect_success "remove a certain author's commits" '
test 0 = $(git rev-list --author="B V Uips" removed-author | wc -l)