git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Per Cederqvist <cederp@opera.com>
Date
Feb 14, 2012, 09:49 UTC
Message-ID
<4F3A2E31.9040006@opera.com>

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 <cederp@opera.com>
---
  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
Message 1 of 1 in “filter-branch: cd to the correct directory when -d is used”
  1. filter-branch: cd to the correct directory when -d is usedPer Cederqvist, Feb 14, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.