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

[Proof of concept PATCH] implement --prune-empty switch for filter-branch

From
Pierre Habouzit <madcoder@debian.org>
Date
Oct 30, 2008, 16:18 UTC
Message-ID
<1225383538-23666-3-git-send-email-madcoder@debian.org>
In-Reply-To
<1225383538-23666-2-git-send-email-madcoder@debian.org>

This is not a real patch (lacks a test at least) and is absolutely not tested, though should basically work as expected.

This is only meant as a basis for discussion.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 Documentation/git-filter-branch.txt |   14 ++++++++++++++
 git-filter-branch.sh                |   26 +++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 1 deletions(-)
diff --git a/Documentation/git-filter-branch.txt b/Documentation/git-filter-branch.txt
index fed6de6..451950b 100644
--- a/Documentation/git-filter-branch.txt
+++ b/Documentation/git-filter-branch.txt
@@ -122,6 +122,10 @@ You can use the 'map' convenience function in this filter, and other
 convenience functions, too.  For example, calling 'skip_commit "$@"'
 will leave out the current commit (but not its changes! If you want
 that, use 'git-rebase' instead).
++
+You can also use the 'git_commit_non_empty_tree "$@"' instead of
+'git commit-tree "$@"' if you don't wish to keep commits with a single parent
+and that makes no change to the tree.
 
 --tag-name-filter <command>::
 	This is the filter for rewriting tag names. When passed,
@@ -151,6 +155,16 @@ to other tags will be rewritten to point to the underlying commit.
 	The result will contain that directory (and only that) as its
 	project root.
 
+--prune-empty::
+	Some kind of filters will generate empty commits, that left the tree
+	untouched.  This switch allow git-filter-branch to ignore such
+	commits.  Though, this switch only applies for commits that have one
+	and only one parent, it will hence keep merges points. Also, this
+	option is not compatible with the use of '--commit-filter'. Though you
+	just need to use the function 'git_commit_non_empty_tree "$@"' instead
+	of the 'git commit-tree "$@"' idiom in your commit filter to make that
+	happen.
+
 --original <namespace>::
 	Use this option to set the namespace where the original commits
 	will be stored. The default value is 'refs/original'.
diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index 8af3126..9c83402 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -49,6 +49,15 @@ die()
 	echo "$*" >&2
 	exit 1
 }
+
+git_commit_non_empty_tree()
+{
+	if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
+		map "$3"
+	else
+		git commit-tree "$@"
+	fi
+}
 EOF
 )
 
@@ -95,6 +104,7 @@ d=                      temporary path to use for rewriting
 f,force                 force filter-branch to run
 subdirectory-filter=    only look at the history touching that specific subdirectory
 original=               namespace where the original commits will be stored (default: refs/original)
+prune-empty             use this if you want to automatically prune empty commits
 
  Filters that you can run:
 
@@ -121,11 +131,12 @@ filter_tree=
 filter_index=
 filter_parent=
 filter_msg=cat
-filter_commit='git commit-tree "$@"'
+filter_commit=
 filter_tag_name=
 filter_subdir=
 orig_namespace=refs/original/
 force=
+prune_empty=
 while :
 do
 	case "$1" in
@@ -138,6 +149,8 @@ do
 	--original)
 		orig_namespace=$(expr "$2/" : '\(.*[^/]\)/*$')/
 		shift;;
+	--prune-empty)
+		prune_empty=t;;
 
 	--env-filter)
 		filter_env="$2"; shift;;
@@ -161,6 +174,17 @@ do
 	shift
 done
 
+case "$prune_empty,$filter_commit" in
+',')
+	filter_commit='git commit-tree "$@"';;
+'t,')
+	filter_commit='git_commit_non_empty_tree "$@"';;
+','*)
+	;;
+*)
+	die "Cannot set --prune-empty and --filter-commit at the same time"
+esac
+
 case "$force" in
 t)
 	rm -rf "$tempdir"
-- 
1.6.0.3.758.gc29b0
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 9 of 25 in “git-filter-branch: Add an example on how to remove empty commits”
  1. git-filter-branch: Add an example on how to remove empty commitsPetr Baudis, Oct 30, 2008
  2. Sam VilainOct 30, 2008
  3. Johannes SchindelinOct 30, 2008
  4. Pierre HabouzitOct 30, 2008
  5. Deskin MillerOct 30, 2008
  6. Pierre HabouzitOct 30, 2008
  7. filter-branch enhancementsPierre Habouzit, Oct 30, 2008
  8. make git-filter-branch use parse-options.Pierre Habouzit, Oct 30, 2008
  9. implement --prune-empty switch for filter-branchPierre Habouzit, Oct 30, 2008
  10. Pierre HabouzitOct 31, 2008
  11. filter-branch: add git_commit_non_empty_tree and --prune-empty.Pierre Habouzit, Oct 31, 2008
  12. Johannes SchindelinOct 31, 2008
  13. Pierre HabouzitOct 31, 2008
  14. Junio C HamanoNov 3, 2008
  15. Pierre HabouzitNov 3, 2008
  16. Pierre HabouzitNov 3, 2008
  17. Jay SoffianJan 9, 2009
  18. Pierre HabouzitJan 11, 2009
  19. Johannes SchindelinJan 11, 2009
  20. Pierre HabouzitJan 11, 2009
  21. Sverre RabbelierJan 11, 2009
  22. Pierre HabouzitJan 11, 2009
  23. Sverre RabbelierJan 11, 2009
  24. Junio C HamanoJan 11, 2009
  25. Johannes SchindelinJan 11, 2009

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.