Skip to content

Commit 6622d9c

Browse files
bebarinogitster
authored andcommitted
format-patch: Add a signature option (--signature)
By default, git uses the version string as the signature for all patches output by format-patch. Many employers (mine included) require the use of a signature on all outgoing mails. In a format-patch | send-email workflow there isn't an easy way to modify the signature without breaking the pipe and manually replacing the version string with the signature required. Instead of doing all that work, add an option (--signature) and a config variable (format.signature) to replace the default git version signature when formatting patches. This does modify the original behavior of format-patch a bit. First off the version string is now placed in the cover letter by default. Secondly, once the configuration variable format.signature is added to the .config file there is no way to revert back to the default git version signature. Instead, specifying the --no-signature option will remove the signature from the patches entirely. Signed-off-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6068cdc commit 6622d9c

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,12 @@ format.subjectprefix::
880880
The default for format-patch is to output files with the '[PATCH]'
881881
subject prefix. Use this variable to change that prefix.
882882

883+
format.signature::
884+
The default for format-patch is to output a signature containing
885+
the git version number. Use this variable to change that default.
886+
Set this variable to the empty string ("") to suppress
887+
signature generation.
888+
883889
format.suffix::
884890
The default for format-patch is to output files with the suffix
885891
`.patch`. Use this variable to change that suffix (make sure to

Documentation/git-format-patch.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SYNOPSIS
1313
[--no-thread | --thread[=<style>]]
1414
[(--attach|--inline)[=<boundary>] | --no-attach]
1515
[-s | --signoff]
16+
[--signature=<signature> | --no-signature]
1617
[-n | --numbered | -N | --no-numbered]
1718
[--start-number <n>] [--numbered-files]
1819
[--in-reply-to=Message-Id] [--suffix=.<sfx>]
@@ -180,6 +181,12 @@ will want to ensure that threading is disabled for `git send-email`.
180181
containing the shortlog and the overall diffstat. You can
181182
fill in a description in the file before sending it out.
182183

184+
--[no]-signature=<signature>::
185+
Add a signature to each message produced. Per RFC 3676 the signature
186+
is separated from the body by a line with '-- ' on it. If the
187+
signature option is omitted the signature defaults to the git version
188+
number.
189+
183190
--suffix=.<sfx>::
184191
Instead of using `.patch` as the suffix for generated
185192
filenames, use specified suffix. A common alternative is

builtin/log.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,9 @@ static void add_header(const char *value)
549549

550550
#define THREAD_SHALLOW 1
551551
#define THREAD_DEEP 2
552-
static int thread = 0;
553-
static int do_signoff = 0;
552+
static int thread;
553+
static int do_signoff;
554+
static const char *signature = git_version_string;
554555

555556
static int git_format_config(const char *var, const char *value, void *cb)
556557
{
@@ -609,6 +610,8 @@ static int git_format_config(const char *var, const char *value, void *cb)
609610
do_signoff = git_config_bool(var, value);
610611
return 0;
611612
}
613+
if (!strcmp(var, "format.signature"))
614+
return git_config_string(&signature, var, value);
612615

613616
return git_log_config(var, value, cb);
614617
}
@@ -703,6 +706,12 @@ static void gen_message_id(struct rev_info *info, char *base)
703706
info->message_id = strbuf_detach(&buf, NULL);
704707
}
705708

709+
static void print_signature(void)
710+
{
711+
if (signature && *signature)
712+
printf("-- \n%s\n\n", signature);
713+
}
714+
706715
static void make_cover_letter(struct rev_info *rev, int use_stdout,
707716
int numbered, int numbered_files,
708717
struct commit *origin,
@@ -796,6 +805,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
796805
diff_flush(&opts);
797806

798807
printf("\n");
808+
print_signature();
799809
}
800810

801811
static const char *clean_message_id(const char *msg_id)
@@ -1035,6 +1045,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10351045
{ OPTION_CALLBACK, 0, "thread", &thread, "style",
10361046
"enable message threading, styles: shallow, deep",
10371047
PARSE_OPT_OPTARG, thread_callback },
1048+
OPT_STRING(0, "signature", &signature, "signature",
1049+
"add a signature"),
10381050
OPT_END()
10391051
};
10401052

@@ -1313,7 +1325,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13131325
mime_boundary_leader,
13141326
rev.mime_boundary);
13151327
else
1316-
printf("-- \n%s\n\n", git_version_string);
1328+
print_signature();
13171329
}
13181330
if (!use_stdout)
13191331
fclose(stdout);

t/t4013/diff.format-patch_--stdout_--cover-letter_-n_initial..master^

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ A U Thor (2):
1818
create mode 100644 file1
1919
delete mode 100644 file2
2020

21+
--
22+
g-i-t--v-e-r-s-i-o-n
23+
2124
From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
2225
From: A U Thor <author@example.com>
2326
Date: Mon, 26 Jun 2006 00:01:00 +0000

t/t4014-format-patch.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,4 +613,56 @@ test_expect_success 'format-patch --ignore-if-in-upstream HEAD' '
613613
git format-patch --ignore-if-in-upstream HEAD
614614
'
615615

616+
test_expect_success 'format-patch --signature' '
617+
git format-patch --stdout --signature="my sig" -1 >output &&
618+
grep "my sig" output
619+
'
620+
621+
test_expect_success 'format-patch with format.signature config' '
622+
git config format.signature "config sig" &&
623+
git format-patch --stdout -1 >output &&
624+
grep "config sig" output
625+
'
626+
627+
test_expect_success 'format-patch --signature overrides format.signature' '
628+
git config format.signature "config sig" &&
629+
git format-patch --stdout --signature="overrides" -1 >output &&
630+
! grep "config sig" output &&
631+
grep "overrides" output
632+
'
633+
634+
test_expect_success 'format-patch --no-signature ignores format.signature' '
635+
git config format.signature "config sig" &&
636+
git format-patch --stdout --signature="my sig" --no-signature \
637+
-1 >output &&
638+
! grep "config sig" output &&
639+
! grep "my sig" output &&
640+
! grep "^-- \$" output
641+
'
642+
643+
test_expect_success 'format-patch --signature --cover-letter' '
644+
git config --unset-all format.signature &&
645+
git format-patch --stdout --signature="my sig" --cover-letter \
646+
-1 >output &&
647+
grep "my sig" output &&
648+
test 2 = $(grep "my sig" output | wc -l)
649+
'
650+
651+
test_expect_success 'format.signature="" supresses signatures' '
652+
git config format.signature "" &&
653+
git format-patch --stdout -1 >output &&
654+
! grep "^-- \$" output
655+
'
656+
657+
test_expect_success 'format-patch --no-signature supresses signatures' '
658+
git config --unset-all format.signature &&
659+
git format-patch --stdout --no-signature -1 >output &&
660+
! grep "^-- \$" output
661+
'
662+
663+
test_expect_success 'format-patch --signature="" supresses signatures' '
664+
git format-patch --signature="" -1 >output &&
665+
! grep "^-- \$" output
666+
'
667+
616668
test_done

0 commit comments

Comments
 (0)