Subject: [PATCH] filter-branch: cd to the correct directory when -d is used

From: Per Cederqvist <[email-masked]>
Date: Tue, 14 Feb 2012 10:49:37 +0100

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.

Signed-off-by: Per Cederqvist <[email-masked]>
---
  git-filter-branch.sh     |    3 ++-
  t/t7003-filter-branch.sh |   38 ++++++++++++++++++++++++++++++++++++++
  2 files changed, 40 insertions(+), 1 deletions(-)

diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index add2c02..a58b50b 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -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
diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
index e022773..fa464a2 100755
--- a/t/t7003-filter-branch.sh
+++ b/t/t7003-filter-branch.sh
@@ -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
-- 
1.7.9