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: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 2, 2013 at 10:22 UTC 97276019bb20829c97528b53dc453a37177c35bb
2 files changed +17 -2
git-filter-branch.sh
+3 -2
@@ -217,6 +217,7 @@ t)
217 test -d "$tempdir" &&
218 die "$tempdir already exists, please remove it"
219 esac
220 +orig_dir=$(pwd)
221 mkdir -p "$tempdir/t" &&
222 tempdir="$(cd "$tempdir"; pwd)" &&
223 cd "$tempdir/t" &&
@@ -224,7 +225,7 @@ workdir="$(pwd)" ||
225 die ""
226
227 # Remove tempdir on exit
227 -trap 'cd ../..; rm -rf "$tempdir"' 0
228 +trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
229
230 ORIG_GIT_DIR="$GIT_DIR"
231 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
@@ -489,7 +490,7 @@ if [ "$filter_tag_name" ]; then
490 done
491 fi
492
492 -cd ../..
493 +cd "$orig_dir"
494 rm -rf "$tempdir"
495
496 trap - 0
t/t7003-filter-branch.sh
+14
@@ -64,6 +64,20 @@ test_expect_success 'correct GIT_DIR while using -d' '
64 grep drepo "$TRASHDIR/backup-refs"
65 '
66
67 +test_expect_success 'tree-filter works with -d' '
68 + git init drepo-tree &&
69 + (
70 + cd drepo-tree &&
71 + test_commit one &&
72 + git filter-branch -d "$TRASHDIR/dfoo" \
73 + --tree-filter "echo changed >one.t" &&
74 + echo changed >expect &&
75 + git cat-file blob HEAD:one.t >actual &&
76 + test_cmp expect actual &&
77 + test_cmp one.t actual
78 + )
79 +'
80 +
81 test_expect_success 'Fail if commit filter fails' '
82 test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
83 '