Skip to content

Commit be510cf

Browse files
committed
send-email: make message-id generation a bit more robust
Earlier code took Unix time and appended a few random digits. If you are firing off many messages within a second, you could issue the same id to different messages, which is a no-no. If you send out 31 messages within a single second, with random integer taken out of rand(4200), you have about 10% chance of producing the same message ID. This fixes the problem by uses a prefix string which is constant-per-invocation (time and pid), with a serial number for each message generated by the process appended at the end. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d7416ec commit be510cf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

git-send-email.perl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,17 @@ sub extract_valid_address {
437437

438438
# We'll setup a template for the message id, using the "from" address:
439439

440+
my ($message_id_stamp, $message_id_serial);
440441
sub make_message_id
441442
{
442-
my $date = time;
443-
my $pseudo_rand = int (rand(4200));
443+
my $uniq;
444+
if (!defined $message_id_stamp) {
445+
$message_id_stamp = sprintf("%s-%s", time, $$);
446+
$message_id_serial = 0;
447+
}
448+
$message_id_serial++;
449+
$uniq = "$message_id_stamp-$message_id_serial";
450+
444451
my $du_part;
445452
for ($sender, $repocommitter, $repoauthor) {
446453
$du_part = extract_valid_address(sanitize_address($_));
@@ -450,8 +457,8 @@ sub make_message_id
450457
use Sys::Hostname qw();
451458
$du_part = 'user@' . Sys::Hostname::hostname();
452459
}
453-
my $message_id_template = "<%s-git-send-email-$du_part>";
454-
$message_id = sprintf $message_id_template, "$date$pseudo_rand";
460+
my $message_id_template = "<%s-git-send-email-%s>";
461+
$message_id = sprintf($message_id_template, $uniq, $du_part);
455462
#print "new message id = $message_id\n"; # Was useful for debugging
456463
}
457464

0 commit comments

Comments
 (0)