# [PATCH] filter-branch: skip commits present on --state-branch

5 messages from 2018-06-23 to 2018-06-26. Participants: Michael Barabanov, Junio C Hamano, Ian Campbell.
Thread: https://gitlist.dev/t/48767

## Michael Barabanov, 2018-06-23 04:36

Subject: [PATCH] filter-branch: skip commits present on --state-branch
Message-ID: <20180623043639.7727-1-michael.barabanov@gmail.com>
URL: https://gitlist.dev/e/20180623043639.7727-1-michael.barabanov%40gmail.com

```
The commits in state:filter.map have already been processed, so don't
filter them again. This makes incremental git filter-branch much faster.

Also add tests for --state-branch option.

Signed-off-by: Michael Barabanov <michael.barabanov@gmail.com>
---
 git-filter-branch.sh     |  3 +++
 t/t7003-filter-branch.sh | 15 +++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index ccceaf19a..2df7ed107 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -372,6 +372,9 @@ while read commit parents; do
 	git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
 
 	report_progress
+	if test -r "$workdir/../map/$commit"; then
+		continue
+	fi
 
 	case "$filter_subdir" in
 	"")
diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
index ec4b160dd..e23de7d0b 100755
--- a/t/t7003-filter-branch.sh
+++ b/t/t7003-filter-branch.sh
@@ -107,6 +107,21 @@ test_expect_success 'test that the directory was renamed' '
 	test dir/D = "$(cat diroh/D.t)"
 '
 
+V=$(git rev-parse HEAD)
+
+test_expect_success 'populate --state-branch' '
+	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD
+'
+
+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 &&
+	test_cmp_rev $W HEAD
+'
+
 git tag oldD HEAD~4
 test_expect_success 'rewrite one branch, keeping a side branch' '
 	git branch modD oldD &&
-- 
2.17.1


```

## Junio C Hamano, 2018-06-25 22:14

Subject: Re: [PATCH] filter-branch: skip commits present on --state-branch
Message-ID: <xmqqmuvims38.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqmuvims38.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20180623043639.7727-1-michael.barabanov@gmail.com>

```
Michael Barabanov <michael.barabanov@gmail.com> writes:

> The commits in state:filter.map have already been processed, so don't
> filter them again. This makes incremental git filter-branch much faster.
>
> Also add tests for --state-branch option.
>
> Signed-off-by: Michael Barabanov <michael.barabanov@gmail.com>
> ---
>  git-filter-branch.sh     |  3 +++
>  t/t7003-filter-branch.sh | 15 +++++++++++++++
>  2 files changed, 18 insertions(+)
>
> diff --git a/git-filter-branch.sh b/git-filter-branch.sh
> index ccceaf19a..2df7ed107 100755
> --- a/git-filter-branch.sh
> +++ b/git-filter-branch.sh
> @@ -372,6 +372,9 @@ while read commit parents; do
>  	git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
>  
>  	report_progress
> +	if test -r "$workdir/../map/$commit"; then
> +		continue
> +	fi

The original script is so much of a mess that I needed quite some
time to find enough evidence to convince myself that this change is
in line with what is already happening in the program.  We have

	test -f "$workdir"/../map/$sha1 && continue

in the codepath for remap-to-ancestor prostprocessing to do
pretty-much the same skipping.

I think the new code should follow suit, i.e.

	if test -f "$workdir/../map/$commit"
	then
		continue
	fi

to check just the existence for consistency.

It would have been reviewer friendly if the proposed commit log
message said how this change does *not* break the progress output
and count.  A possible alternative optimization could be not to add
these already mapped commits in ../revs file in the first place (so
they are not even counted as part of $commits), and such a change
would give different meaning to the progress output (which may or
may not be a good change).  Instead, the posted patch counts the
commits to be filtered the same way as before, and merely pretends
that it filtered those commits to their mapped counterparts without
spending any cycle (simply because we already _know_ what they are
mapped to), so the meaning of the numbers in the progress display
does not change at all---just they appear to progress much faster,
which is a welcome change ;-)




```

## Michael Barabanov, 2018-06-26 04:07

Subject: [PATCH v2] filter-branch: skip commits present on --state-branch
Message-ID: <20180626040733.30757-1-michael.barabanov@gmail.com>
URL: https://gitlist.dev/e/20180626040733.30757-1-michael.barabanov%40gmail.com
In-Reply-To: <20180623043639.7727-1-michael.barabanov@gmail.com>

```
The commits in state:filter.map have already been processed, so don't
filter them again. This makes incremental git filter-branch much faster.

Also add tests for --state-branch option.

Signed-off-by: Michael Barabanov <michael.barabanov@gmail.com>
---
 git-filter-branch.sh     |  1 +
 t/t7003-filter-branch.sh | 15 +++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index ccceaf19a..5c5afa2b9 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -372,6 +372,7 @@ while read commit parents; do
 	git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
 
 	report_progress
+	test -f "$workdir"/../map/$commit && continue
 
 	case "$filter_subdir" in
 	"")
diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
index ec4b160dd..e23de7d0b 100755
--- a/t/t7003-filter-branch.sh
+++ b/t/t7003-filter-branch.sh
@@ -107,6 +107,21 @@ test_expect_success 'test that the directory was renamed' '
 	test dir/D = "$(cat diroh/D.t)"
 '
 
+V=$(git rev-parse HEAD)
+
+test_expect_success 'populate --state-branch' '
+	git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD
+'
+
+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 &&
+	test_cmp_rev $W HEAD
+'
+
 git tag oldD HEAD~4
 test_expect_success 'rewrite one branch, keeping a side branch' '
 	git branch modD oldD &&
-- 
2.18.0


```

## Ian Campbell, 2018-06-26 18:58

Subject: Re: [PATCH v2] filter-branch: skip commits present on --state-branch
Message-ID: <1530039484.9819.8.camel@hellion.org.uk>
URL: https://gitlist.dev/e/1530039484.9819.8.camel%40hellion.org.uk
In-Reply-To: <20180626040733.30757-1-michael.barabanov@gmail.com>

```
On Mon, 2018-06-25 at 21:07 -0700, Michael Barabanov wrote:
> The commits in state:filter.map have already been processed, so don't
> filter them again. This makes incremental git filter-branch much
> faster.
> 
> Also add tests for --state-branch option.
> 
> Signed-off-by: Michael Barabanov <michael.barabanov@gmail.com>

Acked-by: Ian Campbell <ijc@hellion.org.uk>

> ---
>  git-filter-branch.sh     |  1 +
>  t/t7003-filter-branch.sh | 15 +++++++++++++++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/git-filter-branch.sh b/git-filter-branch.sh
> index ccceaf19a..5c5afa2b9 100755
> --- a/git-filter-branch.sh
> +++ b/git-filter-branch.sh
> @@ -372,6 +372,7 @@ while read commit parents; do
>  	git_filter_branch__commit_count=$(($git_filter_branch__commi
> t_count+1))
>  
>  	report_progress
> +	test -f "$workdir"/../map/$commit && continue
>  
>  	case "$filter_subdir" in
>  	"")
> diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
> index ec4b160dd..e23de7d0b 100755
> --- a/t/t7003-filter-branch.sh
> +++ b/t/t7003-filter-branch.sh
> @@ -107,6 +107,21 @@ test_expect_success 'test that the directory was
> renamed' '
>  	test dir/D = "$(cat diroh/D.t)"
>  '
>  
> +V=$(git rev-parse HEAD)
> +
> +test_expect_success 'populate --state-branch' '
> +	git filter-branch --state-branch state -f --tree-filter
> "touch file || :" HEAD
> +'
> +
> +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 &&
> +	test_cmp_rev $W HEAD
> +'
> +
>  git tag oldD HEAD~4
>  test_expect_success 'rewrite one branch, keeping a side branch' '
>  	git branch modD oldD &&

```

## Junio C Hamano, 2018-06-26 22:44

Subject: Re: [PATCH v2] filter-branch: skip commits present on --state-branch
Message-ID: <xmqq1scti2wb.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq1scti2wb.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <1530039484.9819.8.camel@hellion.org.uk>

```
Ian Campbell <ijc@hellion.org.uk> writes:

> On Mon, 2018-06-25 at 21:07 -0700, Michael Barabanov wrote:
>> The commits in state:filter.map have already been processed, so don't
>> filter them again. This makes incremental git filter-branch much
>> faster.
>> 
>> Also add tests for --state-branch option.
>> 
>> Signed-off-by: Michael Barabanov <michael.barabanov@gmail.com>
>
> Acked-by: Ian Campbell <ijc@hellion.org.uk>

Thanks.

>
>> ---
>>  git-filter-branch.sh     |  1 +
>>  t/t7003-filter-branch.sh | 15 +++++++++++++++
>>  2 files changed, 16 insertions(+)
>> 
>> diff --git a/git-filter-branch.sh b/git-filter-branch.sh
>> index ccceaf19a..5c5afa2b9 100755
>> --- a/git-filter-branch.sh
>> +++ b/git-filter-branch.sh
>> @@ -372,6 +372,7 @@ while read commit parents; do
>>  	git_filter_branch__commit_count=$(($git_filter_branch__commi
>> t_count+1))
>>  
>>  	report_progress
>> +	test -f "$workdir"/../map/$commit && continue
>>  
>>  	case "$filter_subdir" in
>>  	"")
>> diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
>> index ec4b160dd..e23de7d0b 100755
>> --- a/t/t7003-filter-branch.sh
>> +++ b/t/t7003-filter-branch.sh
>> @@ -107,6 +107,21 @@ test_expect_success 'test that the directory was
>> renamed' '
>>  	test dir/D = "$(cat diroh/D.t)"
>>  '
>>  
>> +V=$(git rev-parse HEAD)
>> +
>> +test_expect_success 'populate --state-branch' '
>> +	git filter-branch --state-branch state -f --tree-filter
>> "touch file || :" HEAD
>> +'
>> +
>> +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 &&
>> +	test_cmp_rev $W HEAD
>> +'
>> +
>>  git tag oldD HEAD~4
>>  test_expect_success 'rewrite one branch, keeping a side branch' '
>>  	git branch modD oldD &&

```
