Skip to contentSkip to conversation

fiter-branch: fix commit map init from state branch

PATCH Superseded Grant Moyer 4 participants 12 messages Aug 1 – Oct 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fiter-branch: fix commit map init from state branch

The commit map dir is populated from the state branch assuming a "to_commit:from_commit" format, but the state branch is written with a "from_commit:to_commit" format, resulting in an inverted mapping when the map is populated from the state branch. This is especially evident when --prune-empty is used and creates commits which map to nothing; when the map dir is populated from this state on subsequent runs, git-filter-branch outputs many errors while trying to create files with empty names, like:

Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This change corrects the population of the commit map dir to match the "from_commit:to_commit" format.

---
 git-filter-branch.sh     | 4 +++-
 t/t7003-filter-branch.sh | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
git-filter-branch.sh +3 -1
index 24fa317aaa..9aa07be6e1 100755
@@ -302,7 +302,9 @@ then
 		do
 			case "$line" in
 			*:*)
-				echo "${line%:*}" >../map/"${line#*:}";;
+				from_commit=${line%:*}
+				to_commit=${line#*:}
+				echo "$to_commit" >../map/"$from_commit";;
 			*)
 				die "Unable to load state from $state_branch:filter.map";;
 			esac
t/t7003-filter-branch.sh +1 -1
index 86011e7b1f..3934cc4a11 100755
@@ -121,7 +121,7 @@ W=$(git rev-parse HEAD)
 test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_when_finished git reset --hard $V &&
 	git reset --hard $V &&
-	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+	git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD &&
 	test_cmp_rev $W HEAD
 '
 

base-commit: a97fcc37c2bc6340a8d7ce78dedf227aac4e9aa7

Signature · 1 line
2.55.0
Tested-by on v1 1/1
#1 lore raw

Hi Grant,

thanks for this patch: I hit the same bug, and I was about to report it.

Cc'ing Patrick and Junio, since the problem was introduced by f6d855091e (filter-branch: stop depending on Perl, 2025-04-16), released in 2.50.0.

I tested your fix with git 2.56.0, running filter-branch incrementally with --state-branch, --prune-empty and --subdirectory-filter:

- without the fix, the new commits are attached to the original parents instead of the rewritten ones, and swapped entries are saved back to the state branch;
- with the fix, the parents are the rewritten commits and the state is saved correctly.

We know that the use of filter-branch is not recommended, but we rely on --state-branch to split a repository incrementally, that is, processing only the new commits instead of the whole history at every run.

It would be great to have this fix merged.

A minor note: there's a typo in the subject ("fiter-branch").

Thanks,
Michele

#2 lore raw
In reply to Grant Moyer

On Fri, Jul 31, 2026 at 11:31:27PM -0400, Grant Moyer wrote:

Sorry, this slipped may radar. Thanks for bumping this thread, Michele.

Nit: pointed out by Michele: the subject has a typo in "fiter-branch".

Quoted 8 lines

The commit map dir is populated from the state branch assuming a "to_commit:from_commit" format, but the state branch is written with a "from_commit:to_commit" format, resulting in an inverted mapping when the map is populated from the state branch. This is especially evident when --prune-empty is used and creates commits which map to nothing; when the map dir is populated from this state on subsequent runs, git-filter-branch outputs many errors while trying to create files with empty names, like:

Quoted 4 lines
Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This change corrects the population of the commit map dir to match the "from_commit:to_commit" format.

So... does this mean that we don't have test coverage for this case at all?

It might make sense to also mention f6d855091e (filter-branch: stop depending on Perl, 2025-04-16) for context, as this is where the issue was introduced.

Quoted 5 lines
git-filter-branch.sh +0 -0
index 24fa317aaa..9aa07be6e1 100755

git-filter-branch.sh then · line 310

Quoted 10 lines
git-filter-branch.sh +3 -1
@@ -302,7 +302,9 @@ then
 		do
 			case "$line" in
 			*:*)
-				echo "${line%:*}" >../map/"${line#*:}";;
+				from_commit=${line%:*}
+				to_commit=${line#*:}
+				echo "$to_commit" >../map/"$from_commit";;
 			*)
 				die "Unable to load state from $state_branch:filter.map";;
 			esac

Okay. A simpler fix could've been the following diff:

- echo "${line%:*}" >../map/"${line#*:}";;
+ echo "${line#*:}" >../map/"${line%:*}";;

But I guess it doesn't hurt to have proper naming here.

t/t7003-filter-branch.sh W=$(git rev-parse HEAD) · line 126

Quoted 12 lines
t/t7003-filter-branch.sh +1 -1
index 86011e7b1f..3934cc4a11 100755
@@ -121,7 +121,7 @@ W=$(git rev-parse HEAD)
 test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_when_finished git reset --hard $V &&
 	git reset --hard $V &&
-	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+	git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD &&
 	test_cmp_rev $W HEAD
 '

So does this now detect the issue? If so, it feels somewhat roundabout.

Michele, it seems like you have already invested some time into reproducing the issue. Can this maybe be put into a proper test case that directly exercises your issues (that is, the swapped entries and such)?

Thanks!

Patrick

#3 lore raw
Quoted 4 lines · Patrick Steinhardt

Okay. A simpler fix could've been the following diff:

- echo "${line%:*}" >../map/"${line#*:}";;
+ echo "${line#*:}" >../map/"${line%:*}";;

Yep, that's equivalent and was my first version, but I decided to err on the side of clearer intent. Plus, the more verbose version mirrors how the state branch is constructed further down in the script.

Quoted 2 lines

So... does this mean that we don't have test coverage for this case at all?

Yeah, the test for this case was broken.

Quoted 9 lines
Quoted 7 lines

test_expect_success 'using --state-branch to skip already rewritten commits' ' test_when_finished git reset --hard $V &&
git reset --hard $V &&
- git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+ git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD && test_cmp_rev $W HEAD
'

So does this now detect the issue? If so, it feels somewhat roundabout.

The current test seemingly intends to check if the --tree-filter filter was run on already processed commits, but it only checks that the filter produces the same final result. Since the filter is deterministic, the final result is the same whether or not the filter is re-run on already processed commits.

The proposed change makes the test fail immediately if the tree-filter is re-run. I looked around other tests for a test_* command or conventions to fail with a message, but I didn't find anything. I've checked that the test fails without the filter-branch change, and passes with it.

Quoted 1 line

Nit: pointed out by Michele: the subject has a typo in "fiter-branch".

I'm new here. Is this something I should fix by submitting a new version of the patch, or will the maintainer fix it up if/when they merge the patch?

Grant

#4 lore raw
Quoted 31 lines · Grant Moyer
Quoted 4 lines · Patrick Steinhardt

Okay. A simpler fix could've been the following diff:

- echo "${line%:*}" >../map/"${line#*:}";;
+ echo "${line#*:}" >../map/"${line%:*}";;

Yep, that's equivalent and was my first version, but I decided to err on the side of clearer intent. Plus, the more verbose version mirrors how the state branch is constructed further down in the script.

Quoted 2 lines

So... does this mean that we don't have test coverage for this case at all?

Yeah, the test for this case was broken.

Quoted 9 lines
Quoted 7 lines

test_expect_success 'using --state-branch to skip already rewritten commits' ' test_when_finished git reset --hard $V &&
git reset --hard $V &&
- git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+ git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD && test_cmp_rev $W HEAD
'

So does this now detect the issue? If so, it feels somewhat roundabout.

The current test seemingly intends to check if the --tree-filter filter was run on already processed commits, but it only checks that the filter produces the same final result. Since the filter is deterministic, the final result is the same whether or not the filter is re-run on already processed commits.

Quoted 5 lines

The proposed change makes the test fail immediately if the tree-filter is re-run. I looked around other tests for a test_* command or conventions to fail with a message, but I didn't find anything. I've checked that the test fails without the filter-branch change, and passes with it.

The thing that I'm worried about is that the test seems to only coincidentally exercise the code. I think it may be helpful to have a test that explicitly tests for the scenarios that were reported as broken by Michele. Let's maybe wait for them to respond -- if we're lucky they already have a test we can reuse here that more directly exercises a couple of the reportedly-broken scenarios.

Quoted 4 lines
Quoted 1 line

Nit: pointed out by Michele: the subject has a typo in "fiter-branch".

I'm new here. Is this something I should fix by submitting a new version of the patch, or will the maintainer fix it up if/when they merge the patch?

It depends. For a small fix like this the maintainer may make the change themselves if nothing else needs to be changed. But Junio will often tell you in that case, so if he stays quiet without the series getting merged down, then I'd eventually just send a new version. Also helps to bump up the patch series on the mailing list again :)

Thanks!

Patrick

#5 lore raw
In reply to Patrick Steinhardt

Hi Patrick,

Quoted 4 lines

Michele, it seems like you have already invested some time into reproducing the issue. Can this maybe be put into a proper test case that directly exercises your issues (that is, the swapped entries and such)?

Sure. The test below performs an incremental rewrite in two runs:
the second run must attach the new commit to the parent rewritten by the first run, and the saved state must contain "original:rewritten" entries. Without the fix the new commit is attached to the original parent, and the test fails at test_cmp_rev.

I checked it against master (a018953): it fails without the fix and passes with it, and the other tests of t7003 still pass. It also passes with v2.49.1, the last version before the regression.

Grant, feel free to squash it into your patch.

t/t7003-filter-branch.sh +22 -0
@@ -126,6 +126,27 @@ test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_cmp_rev $W HEAD
 '
 
+test_expect_success '--state-branch incremental rewrite uses the rewritten parents' '
+	git init incremental &&
+	(
+		cd incremental &&
+		mkdir sub &&
+		test_commit first sub/file &&
+		test_commit outside root-file &&
+		git filter-branch --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- HEAD &&
+		rewritten_first=$(git rev-parse HEAD) &&
+		git reset --hard outside &&
+		test_commit second sub/file &&
+		git filter-branch -f --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- outside..HEAD &&
+		test_cmp_rev $rewritten_first HEAD^ &&
+		git show refs/state:filter.map >map &&
+		echo "$(git rev-parse second):$(git rev-parse HEAD)" >expect &&
+		grep "^$(git rev-parse second):" map >actual &&
+		test_cmp expect actual
+	)
+'
+
 git tag oldD HEAD~4
 test_expect_success 'rewrite one branch, keeping a side branch' '

git branch modD oldD &&

Thanks,
Michele

#6 lore raw

PATCH v2 1/1 filter-branch: fix commit map init from state branch

In reply to Grant Moyer

The commit map dir is populated from the state branch assuming a "to_commit:from_commit" format, but the state branch is written with a "from_commit:to_commit" format, resulting in an inverted mapping when the map is populated from the state branch. This is especially evident when --prune-empty is used and creates commits which map to nothing; when the map dir is populated from this state on subsequent runs, git-filter-branch outputs many errors while trying to create files with empty names, like:

Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This change corrects the population of the commit map dir to match the "from_commit:to_commit" format and adds/updates tests to check that the state branch is written correctly.

---
 git-filter-branch.sh     |  4 +++-
 t/t7003-filter-branch.sh | 24 +++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
git-filter-branch.sh +3 -1
index 24fa317aaa..9aa07be6e1 100755
@@ -302,7 +302,9 @@ then
 		do
 			case "$line" in
 			*:*)
-				echo "${line%:*}" >../map/"${line#*:}";;
+				from_commit=${line%:*}
+				to_commit=${line#*:}
+				echo "$to_commit" >../map/"$from_commit";;
 			*)
 				die "Unable to load state from $state_branch:filter.map";;
 			esac
t/t7003-filter-branch.sh +23 -1
index 86011e7b1f..cf225b0f0f 100755
@@ -121,10 +121,32 @@ W=$(git rev-parse HEAD)
 test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_when_finished git reset --hard $V &&
 	git reset --hard $V &&
-	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+	git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD &&
 	test_cmp_rev $W HEAD
 '
 
+test_expect_success '--state-branch incremental rewrite uses the rewritten parents' '
+	git init incremental &&
+	(
+		cd incremental &&
+		mkdir sub &&
+		test_commit first sub/file &&
+		test_commit outside root-file &&
+		git filter-branch --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- HEAD &&
+		rewritten_first=$(git rev-parse HEAD) &&
+		git reset --hard outside &&
+		test_commit second sub/file &&
+		git filter-branch -f --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- outside..HEAD &&
+		test_cmp_rev $rewritten_first HEAD^ &&
+		git show refs/state:filter.map >map &&
+		echo "$(git rev-parse second):$(git rev-parse HEAD)" >expect &&
+		grep "^$(git rev-parse second):" map >actual &&
+		test_cmp expect actual
+	)
+'
+
 git tag oldD HEAD~4
 test_expect_success 'rewrite one branch, keeping a side branch' '
 	git branch modD oldD &&
Signature · 1 line
2.55.0
Tested-by Michele Locati on v2 1/1
#7 lore raw

Re: [PATCH v2] filter-branch: fix commit map init from state branch

Quoted 7 lines · Grant Moyer

The commit map dir is populated from the state branch assuming a "to_commit:from_commit" format, but the state branch is written with a "from_commit:to_commit" format, resulting in an inverted mapping when the map is populated from the state branch. This is especially evident when --prune-empty is used and creates commits which map to nothing; when the map dir is populated from this state on subsequent runs, git-filter-branch outputs many errors while trying to create files with empty names, like:

This still doesn't mention the commit that introduced the regression.

Quoted 5 lines
Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This change corrects the population of the commit map dir to match the "from_commit:to_commit" format and adds/updates tests to check that the state branch is written correctly.

Nit: we typically write commit messages in imperative mood, as if you were instructing the code to change.

Quoted 3 lines

Your Signed-off-by should always go last because you're signing off on everything that comes before it.

How about this message:

The "--state-branch" option asks git-filter-branch(1) to write a mapping from old to new objects into the branch. This map is a simple blob stored in "$state_branch:filter.map" with the format "$to_commit:$from_commit".

In f6d855091e (filter-branch: stop depending on Perl, 2025-04-16), we have refactored git-filter-branch(1) to no longer require Perl. But as part of that change we accidentally started to interpret the above format in reverse when populating the map directory. This of course breaks incremental rewrites that use the commit map multiple times. But we don't seem to have any tests for this feature, and as a consequence we didn't notice the regression.

Fix this bug by interpreting the mappings in the correct order again.

Quoted 5 lines
t/t7003-filter-branch.sh +0 -0
index 86011e7b1f..cf225b0f0f 100755

t/t7003-filter-branch.sh W=$(git rev-parse HEAD) · line 128

Quoted 9 lines
t/t7003-filter-branch.sh +2 -1
@@ -121,10 +121,32 @@ W=$(git rev-parse HEAD)
 test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_when_finished git reset --hard $V &&
 	git reset --hard $V &&
-	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+	git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD &&
 	test_cmp_rev $W HEAD
 '
 
+test_expect_success '--state-branch incremental rewrite uses the rewritten parents' '

We could add test_when_finished rm -rf incremental here.

Quoted 10 lines
t/t7003-filter-branch.sh +10 -0
+	git init incremental &&
+	(
+		cd incremental &&
+		mkdir sub &&
+		test_commit first sub/file &&
+		test_commit outside root-file &&
+		git filter-branch --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- HEAD &&
+		rewritten_first=$(git rev-parse HEAD) &&
+		git reset --hard outside &&

t/t7003-filter-branch.sh W=$(git rev-parse HEAD) · line 148

Quoted 10 lines
t/t7003-filter-branch.sh +10 -0
+		test_commit second sub/file &&
+		git filter-branch -f --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- outside..HEAD &&
+		test_cmp_rev $rewritten_first HEAD^ &&
+		git show refs/state:filter.map >map &&
+		echo "$(git rev-parse second):$(git rev-parse HEAD)" >expect &&
+		grep "^$(git rev-parse second):" map >actual &&
+		test_cmp expect actual
+	)
+'

Thanks!

Patrick

#8 lore raw

Re: [PATCH v2] filter-branch: fix commit map init from state branch

Hi Patrick,

Quoted 2 lines

simple blob stored in "$state_branch:filter.map" with the format "$to_commit:$from_commit".

Small correction: the format is "$from_commit:$to_commit" (the original commit first, then the rewritten one), that's what the fix restores.

The test_when_finished cleanup sounds good to me.

Since I'm listed as co-author:

Thanks,
Michele

#9 lore raw

Re: [PATCH v2] filter-branch: fix commit map init from state branch

In reply to Grant Moyer
Quoted 27 lines · Grant Moyer

The commit map dir is populated from the state branch assuming a "to_commit:from_commit" format, but the state branch is written with a "from_commit:to_commit" format, resulting in an inverted mapping when the map is populated from the state branch. This is especially evident when --prune-empty is used and creates commits which map to nothing; when the map dir is populated from this state on subsequent runs, git-filter-branch outputs many errors while trying to create files with empty names, like:

Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This change corrects the population of the commit map dir to match the "from_commit:to_commit" format and adds/updates tests to check that the state branch is written correctly.

---
 git-filter-branch.sh     |  4 +++-
 t/t7003-filter-branch.sh | 24 +++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 2 deletions(-)
git-filter-branch.sh +0 -0
index 24fa317aaa..9aa07be6e1 100755

git-filter-branch.sh then · line 310

Quoted 10 lines
git-filter-branch.sh +3 -1
@@ -302,7 +302,9 @@ then
 		do
 			case "$line" in
 			*:*)
-				echo "${line%:*}" >../map/"${line#*:}";;
+				from_commit=${line%:*}
+				to_commit=${line#*:}
+				echo "$to_commit" >../map/"$from_commit";;
 			*)
 				die "Unable to load state from $state_branch:filter.map";;
 			esac

I very much appreciate the clear description in the proposed log message above that makes what this change corrects very easy to understand.

t/t7003-filter-branch.sh W=$(git rev-parse HEAD) · line 126

Quoted 12 lines
t/t7003-filter-branch.sh +1 -1
index 86011e7b1f..cf225b0f0f 100755
@@ -121,10 +121,32 @@ W=$(git rev-parse HEAD)
 test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_when_finished git reset --hard $V &&
 	git reset --hard $V &&
-	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+	git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD &&
 	test_cmp_rev $W HEAD
 '

I am not sure what this change is about. Care to explain in the commit log?

t/t7003-filter-branch.sh W=$(git rev-parse HEAD) · line 128

Quoted 1 line
t/t7003-filter-branch.sh +1 -0
+test_expect_success '--state-branch incremental rewrite uses the rewritten parents' '

It is a minor nit, but do we want to have

	test_when_finished "rm -fr incremental" &&

here, before creating a new repository that is used only for this test, or do we expect that in the future we will add more pieces of tests that work inside this new repository (in which case leaving it there may indeed be a better choice)?

Quoted 14 lines
t/t7003-filter-branch.sh +14 -0
+	git init incremental &&
+	(
+		cd incremental &&
+		mkdir sub &&
+		test_commit first sub/file &&
+		test_commit outside root-file &&
+		git filter-branch --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- HEAD &&
+		rewritten_first=$(git rev-parse HEAD) &&
+		git reset --hard outside &&
+		test_commit second sub/file &&
+		git filter-branch -f --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- outside..HEAD &&
+		test_cmp_rev $rewritten_first HEAD^ &&

t/t7003-filter-branch.sh W=$(git rev-parse HEAD) · line 152

Quoted 10 lines
t/t7003-filter-branch.sh +7 -0
+		git show refs/state:filter.map >map &&
+		echo "$(git rev-parse second):$(git rev-parse HEAD)" >expect &&
+		grep "^$(git rev-parse second):" map >actual &&
+		test_cmp expect actual
+	)
+'
+
 git tag oldD HEAD~4
 test_expect_success 'rewrite one branch, keeping a side branch' '
 	git branch modD oldD &&

Thanks.

#10 lore raw

PATCH v3 1/1 filter-branch: fix commit map init from state branch

In reply to Grant Moyer

The "--state-branch" option asks git-filter-branch(1) to write a mapping from old to new objects into a branch to enable incremental processing of large histories. This object mapping is stored as a simple blob at "$state_branch:filter.map" with one object pair per line in the format "$from_commit:$to_commit". Before processing commits in a subsequent run, the state branch is used to populate an object mapping directory, where each file is named "map/$from_commit" and has contents "$to_commit".

In f6d855091e (filter-branch: stop depending on Perl, 2025-04-16), we refactored git-filter-branch(1) to no longer require Perl, but accidentally started to interpret object pairs in reverse (as "$to_commit:$from_commit") when populating the map directory from the state branch. This can cause all kinds of bad behavior from the state-branch being effectively ignored to previously filtered objects accidentally being mapped back to unfiltered objects. One especially evident case occurs when "git filter-branch --prune-empty ..." maps some commits to nothing, then on subsequent runs outputs many errors while trying to create files with empty names, like:

Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This regression went unnoticed because, since the introduction of the only test for "--state-branch" in 709cfe848a (filter-branch: skip commits present on --state-branch, 2018-06-26), the test accidentally passes even if commits are not skipped. The test checks that after populating a state branch with git-filter-branch(1), then running it again with that state branch, the resulting filtered commits for the first run and second run match. However since the filter used is deterministic, the commits always match, even if the commits in the state branch are re-filtered.

Fix the population of the object mapping dir by interpreting object pairs as "$from_commit:$to_commit". Also fix the existing "--state-branch" test by directly exiting with a non-zero code if any commits from the state branch aren't skipped. Finally, add a new "--state-branch" test which directly checks that a commit from the state branch is used when incrementally filtering a repo.

---
 git-filter-branch.sh     |  4 +++-
 t/t7003-filter-branch.sh | 25 ++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)
git-filter-branch.sh +3 -1
index 24fa317aaa..9aa07be6e1 100755
@@ -302,7 +302,9 @@ then
 		do
 			case "$line" in
 			*:*)
-				echo "${line%:*}" >../map/"${line#*:}";;
+				from_commit=${line%:*}
+				to_commit=${line#*:}
+				echo "$to_commit" >../map/"$from_commit";;
 			*)
 				die "Unable to load state from $state_branch:filter.map";;
 			esac
t/t7003-filter-branch.sh +24 -1
index 86011e7b1f..801cc83e5e 100755
@@ -121,10 +121,33 @@ W=$(git rev-parse HEAD)
 test_expect_success 'using --state-branch to skip already rewritten commits' '
 	test_when_finished git reset --hard $V &&
 	git reset --hard $V &&
-	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+	git filter-branch --state-branch state -f --tree-filter "exit 1" HEAD &&
 	test_cmp_rev $W HEAD
 '
 
+test_expect_success '--state-branch incremental rewrite uses the rewritten parents' '
+	test_when_finished "rm -fr incremental" &&
+	git init incremental &&
+	(
+		cd incremental &&
+		mkdir sub &&
+		test_commit first sub/file &&
+		test_commit outside root-file &&
+		git filter-branch --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- HEAD &&
+		rewritten_first=$(git rev-parse HEAD) &&
+		git reset --hard outside &&
+		test_commit second sub/file &&
+		git filter-branch -f --state-branch refs/state \
+			--prune-empty --subdirectory-filter sub -- outside..HEAD &&
+		test_cmp_rev $rewritten_first HEAD^ &&
+		git show refs/state:filter.map >map &&
+		echo "$(git rev-parse second):$(git rev-parse HEAD)" >expect &&
+		grep "^$(git rev-parse second):" map >actual &&
+		test_cmp expect actual
+	)
+'
+
 git tag oldD HEAD~4
 test_expect_success 'rewrite one branch, keeping a side branch' '
 	git branch modD oldD &&
Signature · 1 line
2.55.0
#11 lore raw

Re: [PATCH v3] filter-branch: fix commit map init from state branch

Quoted 39 lines · Grant Moyer

The "--state-branch" option asks git-filter-branch(1) to write a mapping from old to new objects into a branch to enable incremental processing of large histories. This object mapping is stored as a simple blob at "$state_branch:filter.map" with one object pair per line in the format "$from_commit:$to_commit". Before processing commits in a subsequent run, the state branch is used to populate an object mapping directory, where each file is named "map/$from_commit" and has contents "$to_commit".

In f6d855091e (filter-branch: stop depending on Perl, 2025-04-16), we refactored git-filter-branch(1) to no longer require Perl, but accidentally started to interpret object pairs in reverse (as "$to_commit:$from_commit") when populating the map directory from the state branch. This can cause all kinds of bad behavior from the state-branch being effectively ignored to previously filtered objects accidentally being mapped back to unfiltered objects. One especially evident case occurs when "git filter-branch --prune-empty ..." maps some commits to nothing, then on subsequent runs outputs many errors while trying to create files with empty names, like:

Quoted 1 line

/usr/lib/git-core/git-filter-branch: line 305: ../map/: Is a directory

This regression went unnoticed because, since the introduction of the only test for "--state-branch" in 709cfe848a (filter-branch: skip commits present on --state-branch, 2018-06-26), the test accidentally passes even if commits are not skipped. The test checks that after populating a state branch with git-filter-branch(1), then running it again with that state branch, the resulting filtered commits for the first run and second run match. However since the filter used is deterministic, the commits always match, even if the commits in the state branch are re-filtered.

Fix the population of the object mapping dir by interpreting object pairs as "$from_commit:$to_commit". Also fix the existing "--state-branch" test by directly exiting with a non-zero code if any commits from the state branch aren't skipped. Finally, add a new "--state-branch" test which directly checks that a commit from the state branch is used when incrementally filtering a repo.

Quoted 4 lines

Thanks, I'm happy with this version.

Patrick