Skip to contentSkip to conversation

filter-branch: cd to the correct directory when -d is used

PATCH New Per Cederqvist 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 filter-branch: cd to the correct directory when -d is used

git-filter-branch changes directory to $tempdir/t, which by default is .git-rewrite/t. Before doing the read-tree to update the working tree it uses "cd ../.." to get back to the working tree. This breaks if you use something like "-d /tmp/tempdir", as the read-tree will be executed in /tmp instead of in your working tree.

Fixed by adding a variable that holds the original value of $(pwd), and cd back to that value.

Added tests that demonstrates some issues that can happen without this fix.

---
 git-filter-branch.sh     |    3 ++-
 t/t7003-filter-branch.sh |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletions(-)
git-filter-branch.sh +2 -1
index add2c02..a58b50b 100755
@@ -217,6 +217,7 @@ t)
 	test -d "$tempdir" &&
 		die "$tempdir already exists, please remove it"
 esac
+oldpwd="$(pwd)"
 mkdir -p "$tempdir/t" &&
 tempdir="$(cd "$tempdir"; pwd)" &&
 cd "$tempdir/t" &&
@@ -489,7 +490,7 @@ if [ "$filter_tag_name" ]; then
 	done
 fi
 
-cd ../..
+cd "$oldpwd"
 rm -rf "$tempdir"
 
 trap - 0
t/t7003-filter-branch.sh +38 -0
index e022773..fa464a2 100755
@@ -367,4 +367,42 @@ test_expect_success 'replace submodule revision' '
 	test $orig_head != `git show-ref --hash --head HEAD`
 '
 
+test_expect_success 'get a fresh tree' '
+	rm -fr ?* .git .gitmodules
+'
+
+deep_tree() {
+	test_expect_success 'setup deep tree' '
+		rm -fr drepo &&
+		mkdir drepo &&
+		( cd drepo &&
+		git init &&
+		mkdir kom++ &&
+		test_commit authors kom++/AUTHORS "Fake authors file" &&
+		test_commit changelog kom++/ChangeLog "Fake ChangeLog file"
+		)
+	'
+
+	test_expect_success '-d tempdir with --subdirectory-filter' '
+		(cd drepo &&
+		git filter-branch -d "$TRASHDIR/tmpdir" --subdirectory-filter kom++ --tag-name-filter cat -- --all
+		)
+	'
+
+	test_expect_success 'content after -d with --subdirectory-filter' '
+		test_path_is_missing AUTHORS "AUTHORS created in tmpdir/.."
+		test_path_is_missing ChangeLog "ChangeLog created in tmpdir/.."
+		test_path_is_missing drepo/kom++ "kom++ remains"
+		test_path_is_file drepo/AUTHORS "AUTHORS not moved to top-level"
+		test_path_is_file drepo/ChangeLog "ChangeLog not moved to top-level"
+	'
+
+	test_expect_success 'Clean workdir' '
+		cd drepo&&git diff --quiet
+	'
+}
+
+deep_tree
+deep_tree # In Git 1.7.9 and earlier filter-branch fails the second time.
+
 test_done
Signature · 1 line
1.7.9