Commit 97276019 authored by Jeff King's avatar Jeff King Committed by Junio C Hamano

filter-branch: return to original dir after filtering

The first thing filter-branch does is to create a temporary
directory, either ".git-rewrite" in the current directory
(which may be the working tree or the repository if bare),
or in a directory specified by "-d". We then chdir to
$tempdir/t as our temporary working directory in which to run
tree filters.

After finishing the filter, we then attempt to go back to
the original directory with "cd ../..". This works in the
.git-rewrite case, but if "-d" is used, we end up in a
random directory. The only thing we do after this chdir is
to run git-read-tree, but that means that:

  1. The working directory is not updated to reflect the
     filtered history.

  2. We dump random files into "$tempdir/.." (e.g., if you
     use "-d /tmp/foo", we dump junk into /tmp).

Fix it by recording the full path to the original directory
and returning there explicitly.
Signed-off-by: default avatarJeff King <>
Signed-off-by: default avatarJunio C Hamano <>
parent 15999998
...@@ -217,6 +217,7 @@ t) ...@@ -217,6 +217,7 @@ t)
test -d "$tempdir" && test -d "$tempdir" &&
die "$tempdir already exists, please remove it" die "$tempdir already exists, please remove it"
esac esac
mkdir -p "$tempdir/t" && mkdir -p "$tempdir/t" &&
tempdir="$(cd "$tempdir"; pwd)" && tempdir="$(cd "$tempdir"; pwd)" &&
cd "$tempdir/t" && cd "$tempdir/t" &&
...@@ -224,7 +225,7 @@ workdir="$(pwd)" || ...@@ -224,7 +225,7 @@ workdir="$(pwd)" ||
die "" die ""
# Remove tempdir on exit # Remove tempdir on exit
trap 'cd ../..; rm -rf "$tempdir"' 0 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
...@@ -489,7 +490,7 @@ if [ "$filter_tag_name" ]; then ...@@ -489,7 +490,7 @@ if [ "$filter_tag_name" ]; then
done done
fi fi
cd ../.. cd "$orig_dir"
rm -rf "$tempdir" rm -rf "$tempdir"
trap - 0 trap - 0
...@@ -64,6 +64,20 @@ test_expect_success 'correct GIT_DIR while using -d' ' ...@@ -64,6 +64,20 @@ test_expect_success 'correct GIT_DIR while using -d' '
grep drepo "$TRASHDIR/backup-refs" grep drepo "$TRASHDIR/backup-refs"
' '
test_expect_success 'tree-filter works with -d' '
git init drepo-tree &&
cd drepo-tree &&
test_commit one &&
git filter-branch -d "$TRASHDIR/dfoo" \
--tree-filter "echo changed >one.t" &&
echo changed >expect &&
git cat-file blob HEAD:one.t >actual &&
test_cmp expect actual &&
test_cmp one.t actual
test_expect_success 'Fail if commit filter fails' ' test_expect_success 'Fail if commit filter fails' '
test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
' '
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment