Skip to content

Commit 46eb449

Browse files
dschospearce
authored andcommitted
filter-branch: update current branch when rewritten
Earlier, "git filter-branch --<options> HEAD" would not update the working tree after rewriting the branch. This commit fixes it. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 8ef4451 commit 46eb449

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

git-filter-branch.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ USAGE="[--env-filter <command>] [--tree-filter <command>] \
9494

9595
. git-sh-setup
9696

97+
git diff-files --quiet &&
98+
git diff-index --cached --quiet HEAD ||
99+
die "Cannot rewrite branch(es) with a dirty working directory."
100+
97101
tempdir=.git-rewrite
98102
filter_env=
99103
filter_tree=
@@ -196,6 +200,9 @@ do
196200
esac
197201
done < "$tempdir"/backup-refs
198202

203+
ORIG_GIT_DIR="$GIT_DIR"
204+
ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
205+
ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
199206
export GIT_DIR GIT_WORK_TREE=.
200207

201208
# These refs should be updated if their heads were rewritten
@@ -413,4 +420,12 @@ echo
413420
test $count -gt 0 && echo "These refs were rewritten:"
414421
git show-ref | grep ^"$orig_namespace"
415422

423+
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
424+
test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
425+
test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
426+
export GIT_WORK_TREE
427+
test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
428+
export GIT_INDEX_FILE
429+
git read-tree -u -m HEAD
430+
416431
exit $ret

t/t7003-filter-branch.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ test_expect_success 'rewrite, renaming a specific file' '
4141
'
4242

4343
test_expect_success 'test that the file was renamed' '
44-
test d = $(git show HEAD:doh)
44+
test d = $(git show HEAD:doh) &&
45+
test -f doh &&
46+
test d = $(cat doh)
4547
'
4648

4749
git tag oldD HEAD~4

0 commit comments

Comments
 (0)