# git-filter-branch behavior

4 messages from 2008-08-13 to 2008-08-15. Participants: David Neu, Jonathan Nieder, Michael J Gruber.
Thread: https://gitlist.dev/t/14986

## David Neu, 2008-08-13 16:14

Subject: git-filter-branch behavior
Message-ID: <e0b44a890808130914oced739cy2aaf54aebfcfcbce@mail.gmail.com>
URL: https://gitlist.dev/e/e0b44a890808130914oced739cy2aaf54aebfcfcbce%40mail.gmail.com

```
All,

Running

git-filter-branch --tree-filter 'rm -rf subdir/' -- --all

as shown below seems to leave empty commits
corresponding to subdir/ in the tree.  Is this the expected
behavior?  If so is there a command to remove the empty
commits?  Using git-rebase -i to edit the commit history
works, but is a bit tedious on a large tree.

Many thanks!

Cheers,
David

***** ***** ***** ***** *****

mkdir test-filter-branch
cd test-filter-branch

echo 'base1' > base.txt
mkdir subdir
echo 'sub1' > subdir/sub.txt
git-init
git-add .
git-commit -a -m "Commit 1"

echo 'base2' >> base.txt
git-commit -a -m "Commit 2"

echo 'sub3' > subdir/sub.txt
git-commit -a -m "Commit 3"

echo 'base4' >> base.txt
echo 'sub4' > subdir/sub.txt
git-commit -a -m "Commit 4"

git-filter-branch --tree-filter 'rm -rf subdir/' -- --all

gitk &

```

## Jonathan Nieder, 2008-08-13 20:41

Subject: Re: git-filter-branch behavior
Message-ID: <Pine.GSO.4.62.0808131533320.10800@harper.uchicago.edu>
URL: https://gitlist.dev/e/Pine.GSO.4.62.0808131533320.10800%40harper.uchicago.edu
In-Reply-To: <e0b44a890808130914oced739cy2aaf54aebfcfcbce@mail.gmail.com>

```
Hello,

David Neu wrote:

> Running
> 
> git-filter-branch --tree-filter 'rm -rf subdir/' -- --all
> 
> as shown below seems to leave empty commits
> corresponding to subdir/ in the tree.  Is this the expected
> behavior?  If so is there a command to remove the empty
> commits?

The following is probably overkill, but it is what I would do.
It's completely untested.  If you'd prefer to do things by hand
instead, my only advice is that using grafts with filter-branch
might be easier than rebase -i.

-- snipsnip --
# prune-empty-commits - filter-branch filter to avoid boring commits
#
# Usage: git-filter-branch --tree-filter <something> \
# 		--commit-filter 'prune-empty-commits "$@"' -- <refs>
# Public domain.

interesting=

test "$#" -eq 1 && interesting=t

committree=$1
shift

for sha1 in "$@"
do
	test z"$sha1" = z-p && continue
	map "$sha1" | while read parent
	do
		parenttree=$(git log -1 --pretty=format:%T "$parent")
		test "$committree" != "$parenttree" &&
			interesting=t
		test -n "$interesting" && break
	done
	test "-n interesting" && break
done

test -n "$interesting" && git commit-tree "$@" || skip_commit "$@"

```

## Jonathan Nieder, 2008-08-13 21:00

Subject: Re: git-filter-branch behavior
Message-ID: <Pine.GSO.4.62.0808131552130.11537@harper.uchicago.edu>
URL: https://gitlist.dev/e/Pine.GSO.4.62.0808131552130.11537%40harper.uchicago.edu
In-Reply-To: <Pine.GSO.4.62.0808131533320.10800@harper.uchicago.edu>

```
Jonathan Nieder wrote:

> # prune-empty-commits - filter-branch filter to avoid boring commits
> #
> # Usage: git-filter-branch --tree-filter <something> \
> # 		--commit-filter 'prune-empty-commits "$@"' -- <refs>
[...]
> 	map "$sha1" | while read parent

Oops - the 'map' function is not passed on to scripts, so one should
use this script with "--commit-filter '. prune-empty-commits'".

> 	test "-n interesting" && break

s/"-n interesting"/-n "$interesting"/

Sorry about that.

```

## Michael J Gruber, 2008-08-15 15:32

Subject: Re: git-filter-branch behavior
Message-ID: <g847i6$cd4$1@ger.gmane.org>
URL: https://gitlist.dev/e/g847i6%24cd4%241%40ger.gmane.org
In-Reply-To: <Pine.GSO.4.62.0808131533320.10800@harper.uchicago.edu>

```
Jonathan Nieder venit, vidit, dixit 13.08.2008 22:41:
> Hello,
> 
> David Neu wrote:
> 
>> Running
>>
>> git-filter-branch --tree-filter 'rm -rf subdir/' -- --all
>>
>> as shown below seems to leave empty commits
>> corresponding to subdir/ in the tree.  Is this the expected
>> behavior?  If so is there a command to remove the empty
>> commits?
> 
> The following is probably overkill, but it is what I would do.
> It's completely untested.  If you'd prefer to do things by hand
> instead, my only advice is that using grafts with filter-branch
> might be easier than rebase -i.
> 
> -- snipsnip --
> # prune-empty-commits - filter-branch filter to avoid boring commits
> #
> # Usage: git-filter-branch --tree-filter <something> \
> # 		--commit-filter 'prune-empty-commits "$@"' -- <refs>
> # Public domain.
> 
> interesting=
> 
> test "$#" -eq 1 && interesting=t
> 
> committree=$1
> shift
> 
> for sha1 in "$@"
> do
> 	test z"$sha1" = z-p && continue
> 	map "$sha1" | while read parent
> 	do
> 		parenttree=$(git log -1 --pretty=format:%T "$parent")
> 		test "$committree" != "$parenttree" &&
> 			interesting=t
> 		test -n "$interesting" && break
> 	done
> 	test "-n interesting" && break
> done
> 
> test -n "$interesting" && git commit-tree "$@" || skip_commit "$@"

You may want to pass the tree to be committed here ;)
I.e.:
git commit-tree $committree "$@"
etc.

Michael

```
