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

[PATCH v4] filter-branch.sh: support nearly proper tag name filtering

From
Brandon Casey <casey@nrlssc.navy.mil>
Date
Mar 26, 2008, 15:47 UTC
Message-ID
<47EA6FFD.1090805@nrlssc.navy.mil>
In-Reply-To
<7vmyolgbor.fsf@gitster.siamese.dyndns.org>

Add support for creating a new tag object and retaining the tag message, author, and date when rewriting tags. The gpg signature, if one exists, will be stripped.

This adds nearly proper tag name filtering to filter-branch. Proper tag name filtering would include the ability to change the tagger, tag date, tag message, and _not_ strip a gpg signature if the tag did not change.

Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
---
Junio C Hamano wrote:
Show 11 quoted lines
> Brandon Casey <casey@nrlssc.navy.mil> writes:
>> Of course, if we use ranges, we may have to worry about matching against
>> the tag body if the header gets any longer.
> 
> I do not think you have to worry if you did this:
> 
>         1,/^$/{
>                 s/object .*/object $newobject/
>                 s/type .*/type $newtype/
>                 ...
>         }
I am a sed light-weight. Thanks for teaching.
-brandon
 Documentation/git-filter-branch.txt |   14 ++++++++++----
 git-filter-branch.sh                |   18 ++++++++++++++++--
 t/t7003-filter-branch.sh            |   32 ++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 6 deletions(-)
diff --git a/Documentation/git-filter-branch.txt b/Documentation/git-filter-branch.txt
index 2a78549..9d11b41 100644
--- a/Documentation/git-filter-branch.txt
+++ b/Documentation/git-filter-branch.txt
@@ -133,10 +133,16 @@ use "--tag-name-filter cat" to simply update the tags.  In this
 case, be very careful and make sure you have the old tags
 backed up in case the conversion has run afoul.
 +
-Note that there is currently no support for proper rewriting of
-tag objects; in layman terms, if the tag has a message or signature
-attached, the rewritten tag won't have it.  Sorry.  (It is by
-definition impossible to preserve signatures at any rate.)
+Nearly proper rewriting of tag objects is supported. If the tag has
+a message attached, a new tag object will be created with the same message,
+author, and timestamp. If the tag has a signature attached, the
+signature will be stripped. It is by definition impossible to preserve
+signatures. The reason this is "nearly" proper, is because ideally if
+the tag did not change (points to the same object, has the same name, etc.)
+it should retain any signature. That is not the case, signatures will always
+be removed, buyer beware. There is also no support for changing the
+author or timestamp (or the tag message for that matter). Tags which point
+to other tags will be rewritten to point to the underlying commit.
 
 --subdirectory-filter <directory>::
 	Only look at the history which touches the given subdirectory.
diff --git a/git-filter-branch.sh b/git-filter-branch.sh
index 22b6ed4..ee90660 100755
--- a/git-filter-branch.sh
+++ b/git-filter-branch.sh
@@ -406,8 +406,22 @@ if [ "$filter_tag_name" ]; then
 		echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
 
 		if [ "$type" = "tag" ]; then
-			# Warn that we are not rewriting the tag object itself.
-			warn "unreferencing tag object $sha1t"
+			new_sha1=$(git cat-file tag "$ref" |
+				sed -n \
+				    -e "1,/^$/{
+					  s/^object .*/object $new_sha1/
+					  s/^type .*/type commit/
+					  s/^tag .*/tag $new_ref/
+					}" \
+				    -e '/^-----BEGIN PGP SIGNATURE-----/q' \
+				    -e 'p' |
+				git mktag) ||
+				die "Could not create new tag object for $ref"
+			if git cat-file tag "$ref" | \
+			   grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
+			then
+				warn "gpg signature stripped from tag object $sha1t"
+			fi
 		fi
 
 		git update-ref "refs/tags/$new_ref" "$new_sha1" ||
diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
index 6827249..1daaf54 100755
--- a/t/t7003-filter-branch.sh
+++ b/t/t7003-filter-branch.sh
@@ -203,4 +203,36 @@ test_expect_success 'Subdirectory filter with disappearing trees' '
 	test $(git rev-list master | wc -l) = 3
 '
 
+test_expect_success 'Tag name filtering retains tag message' '
+	git tag -m atag T &&
+	git cat-file tag T > expect &&
+	git filter-branch -f --tag-name-filter cat &&
+	git cat-file tag T > actual &&
+	git diff expect actual
+'
+
+faux_gpg_tag='object XXXXXX
+type commit
+tag S
+tagger T A Gger <tagger@example.com> 1206026339 -0500
+
+This is a faux gpg signed tag.
+-----BEGIN PGP SIGNATURE-----
+Version: FauxGPG v0.0.0 (FAUX/Linux)
+
+gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
+acmwXaWET20H0GeAGP+7vow=
+=agpO
+-----END PGP SIGNATURE-----
+'
+test_expect_success 'Tag name filtering strips gpg signature' '
+	sha1=$(git rev-parse HEAD) &&
+	sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
+	git update-ref "refs/tags/S" "$sha1t" &&
+	echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
+	git filter-branch -f --tag-name-filter cat &&
+	git cat-file tag S > actual &&
+	git diff expect actual
+'
+
 test_done
-- 
1.5.4.4.481.g5075
Previous: Junio C Hamano
Message 23 of 23 in “filter-branch.sh: support nearly proper tag name filtering”
  1. 2/2 filter-branch.sh: support nearly proper tag name filteringBrandon Casey, Mar 20, 2008
  2. Johannes SchindelinMar 20, 2008
  3. Brandon CaseyMar 20, 2008
  4. Junio C HamanoMar 24, 2008
  5. Johannes SchindelinMar 24, 2008
  6. Junio C HamanoMar 24, 2008
  7. Johannes SchindelinMar 24, 2008
  8. Brandon CaseyMar 24, 2008
  9. Brandon CaseyMar 24, 2008
  10. Johannes SchindelinMar 24, 2008
  11. Brandon CaseyMar 24, 2008
  12. Johannes SchindelinMar 24, 2008
  13. Brandon CaseyMar 24, 2008
  14. filter-branch --all?Eyvind Bernhardsen, Mar 24, 2008
  15. Brandon CaseyMar 24, 2008
  16. Eyvind BernhardsenMar 24, 2008
  17. Brandon CaseyMar 24, 2008
  18. filter-branch.sh: support nearly proper tag name filteringBrandon Casey, Mar 24, 2008
  19. Brandon CaseyMar 24, 2008
  20. Junio C HamanoMar 25, 2008
  21. filter-branch.sh: support nearly proper tag name filteringBrandon Casey, Mar 25, 2008
  22. Junio C HamanoMar 26, 2008
  23. filter-branch.sh: support nearly proper tag name filteringBrandon Casey, Mar 26, 2008

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.