Skip to content

Commit 9727601

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1599999 commit 9727601

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

git-filter-branch.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,15 @@ t)
217217
test -d "$tempdir" &&
218218
die "$tempdir already exists, please remove it"
219219
esac
220+
orig_dir=$(pwd)
220221
mkdir -p "$tempdir/t" &&
221222
tempdir="$(cd "$tempdir"; pwd)" &&
222223
cd "$tempdir/t" &&
223224
workdir="$(pwd)" ||
224225
die ""
225226

226227
# Remove tempdir on exit
227-
trap 'cd ../..; rm -rf "$tempdir"' 0
228+
trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
228229

229230
ORIG_GIT_DIR="$GIT_DIR"
230231
ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
@@ -489,7 +490,7 @@ if [ "$filter_tag_name" ]; then
489490
done
490491
fi
491492

492-
cd ../..
493+
cd "$orig_dir"
493494
rm -rf "$tempdir"
494495

495496
trap - 0

t/t7003-filter-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ test_expect_success 'correct GIT_DIR while using -d' '
6464
grep drepo "$TRASHDIR/backup-refs"
6565
'
6666

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+
6781
test_expect_success 'Fail if commit filter fails' '
6882
test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
6983
'

0 commit comments

Comments
 (0)