Skip to content

Commit a5a4b3f

Browse files
jamessangitster
authored andcommitted
filter-branch: remove multi-line headers in msg filter
df06201 (filter-branch: avoid passing commit message through sed) introduced a regression when filtering commits with multi-line headers, if the header contains a blank line. An example of this is a gpg-signed commit: $ git cat-file commit signed-commit tree 3d4038e029712da9fc59a72afbfcc90418451630 parent 110eac945dc1713b27bdf49e74e5805db66971f0 author A U Thor <[email protected]> 1112912413 -0700 committer C O Mitter <[email protected]> 1112912413 -0700 gpgsig -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iEYEABECAAYFAlYXADwACgkQE7b1Hs3eQw23CACgldB/InRyDgQwyiFyMMm3zFpj pUsAnA+f3aMUsd9mNroloSmlOgL6jIMO =0Hgm -----END PGP SIGNATURE----- Adding gpg As a consequence, "filter-branch --msg-filter cat" (which should leave the commit message unchanged) spills the signature (after the internal blank line) into the original commit message. The reason is that although the signature is indented, making the line a whitespace only line, the "read" call is splitting the line based on the shell's IFS, which defaults to <space><tab><newline>. The leading space is consumed and $header_line is empty, causing the "skip header lines" loop to exit. The rest of the commit object is then re-used as the rewritten commit message, causing the new message to include the signature of the original commit. Set IFS to an empty string for the "read" call, thus disabling the word splitting, which causes $header_line to be set to the non-empty value ' '. This allows the loop to fully consume the header lines before emitting the original, intact commit message. [jc: this is literally based on MJG's suggestion] Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: James McCoy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent df06201 commit a5a4b3f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

git-filter-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ while read commit parents; do
347347
fi
348348

349349
{
350-
while read -r header_line && test -n "$header_line"
350+
while IFS='' read -r header_line && test -n "$header_line"
351351
do
352352
# skip header lines...
353353
:;

t/t7003-filter-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git filter-branch'
44
. ./test-lib.sh
5+
. "$TEST_DIRECTORY/lib-gpg.sh"
56

67
test_expect_success 'setup' '
78
test_commit A &&
@@ -292,6 +293,19 @@ test_expect_success 'Tag name filtering strips gpg signature' '
292293
test_cmp expect actual
293294
'
294295

296+
test_expect_success GPG 'Filtering retains message of gpg signed commit' '
297+
mkdir gpg &&
298+
touch gpg/foo &&
299+
git add gpg &&
300+
test_tick &&
301+
git commit -S -m "Adding gpg" &&
302+
303+
git log -1 --format="%s" > expect &&
304+
git filter-branch -f --msg-filter "cat" &&
305+
git log -1 --format="%s" > actual &&
306+
test_cmp expect actual
307+
'
308+
295309
test_expect_success 'Tag name filtering allows slashes in tag names' '
296310
git tag -m tag-with-slash X/1 &&
297311
git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&

0 commit comments

Comments
 (0)