Skip to content

Commit e9dd751

Browse files
committed
Merge branch 'bc/filter-branch'
* bc/filter-branch: filter-branch.sh: support nearly proper tag name filtering
2 parents e2e2def + 1bf6551 commit e9dd751

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

Documentation/git-filter-branch.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,16 @@ use "--tag-name-filter cat" to simply update the tags. In this
133133
case, be very careful and make sure you have the old tags
134134
backed up in case the conversion has run afoul.
135135
+
136-
Note that there is currently no support for proper rewriting of
137-
tag objects; in layman terms, if the tag has a message or signature
138-
attached, the rewritten tag won't have it. Sorry. (It is by
139-
definition impossible to preserve signatures at any rate.)
136+
Nearly proper rewriting of tag objects is supported. If the tag has
137+
a message attached, a new tag object will be created with the same message,
138+
author, and timestamp. If the tag has a signature attached, the
139+
signature will be stripped. It is by definition impossible to preserve
140+
signatures. The reason this is "nearly" proper, is because ideally if
141+
the tag did not change (points to the same object, has the same name, etc.)
142+
it should retain any signature. That is not the case, signatures will always
143+
be removed, buyer beware. There is also no support for changing the
144+
author or timestamp (or the tag message for that matter). Tags which point
145+
to other tags will be rewritten to point to the underlying commit.
140146

141147
--subdirectory-filter <directory>::
142148
Only look at the history which touches the given subdirectory.

git-filter-branch.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,22 @@ if [ "$filter_tag_name" ]; then
406406
echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
407407

408408
if [ "$type" = "tag" ]; then
409-
# Warn that we are not rewriting the tag object itself.
410-
warn "unreferencing tag object $sha1t"
409+
new_sha1=$(git cat-file tag "$ref" |
410+
sed -n \
411+
-e "1,/^$/{
412+
s/^object .*/object $new_sha1/
413+
s/^type .*/type commit/
414+
s/^tag .*/tag $new_ref/
415+
}" \
416+
-e '/^-----BEGIN PGP SIGNATURE-----/q' \
417+
-e 'p' |
418+
git mktag) ||
419+
die "Could not create new tag object for $ref"
420+
if git cat-file tag "$ref" | \
421+
grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
422+
then
423+
warn "gpg signature stripped from tag object $sha1t"
424+
fi
411425
fi
412426

413427
git update-ref "refs/tags/$new_ref" "$new_sha1" ||

t/t7003-filter-branch.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,36 @@ test_expect_success 'Subdirectory filter with disappearing trees' '
219219
test $(git rev-list master | wc -l) = 3
220220
'
221221

222+
test_expect_success 'Tag name filtering retains tag message' '
223+
git tag -m atag T &&
224+
git cat-file tag T > expect &&
225+
git filter-branch -f --tag-name-filter cat &&
226+
git cat-file tag T > actual &&
227+
git diff expect actual
228+
'
229+
230+
faux_gpg_tag='object XXXXXX
231+
type commit
232+
tag S
233+
tagger T A Gger <[email protected]> 1206026339 -0500
234+
235+
This is a faux gpg signed tag.
236+
-----BEGIN PGP SIGNATURE-----
237+
Version: FauxGPG v0.0.0 (FAUX/Linux)
238+
239+
gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
240+
acmwXaWET20H0GeAGP+7vow=
241+
=agpO
242+
-----END PGP SIGNATURE-----
243+
'
244+
test_expect_success 'Tag name filtering strips gpg signature' '
245+
sha1=$(git rev-parse HEAD) &&
246+
sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
247+
git update-ref "refs/tags/S" "$sha1t" &&
248+
echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
249+
git filter-branch -f --tag-name-filter cat &&
250+
git cat-file tag S > actual &&
251+
git diff expect actual
252+
'
253+
222254
test_done

0 commit comments

Comments
 (0)