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

[PATCH v7 1/2] send-email: fix bug resulting in broken threads if a message is edited

From
Aditya Garg <gargaditya08@live.com>
Date
Jun 4, 2025, 12:55 UTC
Message-ID
<PN3PR01MB95977671AA4500599872A1A3B86CA@PN3PR01MB9597.INDPRD01.PROD.OUTLOOK.COM>
In-Reply-To
<PN3PR01MB95979AA8114CA26405BE02CFB86CA@PN3PR01MB9597.INDPRD01.PROD.OUTLOOK.COM>

Whenever we send a thread of emails using send-email, a message number is internally assigned to each email. This number is used to track the order of the emails in the thread. Whenever a new message is processed in a thread, the current script logic increments the message number by one, which is intended.

But, if a message is edited and then resent, its message number again gets incremented. This is because the script uses the same logic to process the edited message, which it uses to send the next message.

This minor bug is usually harmless, unless a special situations arises. That situation is when the first message in a thread is edited and resent, and an `--in-reply-to` argument is also passed to send-email. In this case, if the user has chosen shallow threading, the threading does not work as expected, and all messages become replies to the Message-ID specified in the `--in-reply-to` argument.

The reason for this bug is hidden in the code for threading itself.
if ($thread) {
	if ($message_was_sent &&
	  ($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
	  $message_num == 1)) {
		$in_reply_to = $message_id;
		if (length $references > 0) {
			$references .= "\n $message_id";
		} else {
			$references = "$message_id";
		}
	}
}

Here `$message_num` is the current message number, and `$in_reply_to` is the Message-ID of the message to which the current message is a reply. In case `--in-reply-to` is specified, the `$in_reply_to` variable is set to the value of the `--in-reply-to` argument.

Whenever this whole set of conditions is true, the script sets the `$in_reply_to` variable to the current message's ID. This is done to ensure that the next message in the thread is a reply to this message.

In case we specify an `--in-reply-to` argument, and have shallow threading, the only condition that can make this true is `$message_num == 1`, which is true for the first message in a thread. Thus, the `$in_reply_to` variable gets set to the first message's ID. For subsequent messages, the `$message_num` variable is always greater than 1, and the whole set of conditions is false. Therefore, the `$in_reply_to` variable remains as the first message's ID. This is what we expect in shallow threading. But if the user edits the first message and resends it, the `$message_num` variable gets incremented by 1, and thus the condition `$message_num == 1` becomes false. This means that the `$in_reply_to` variable is not set to the first message's ID. As a result the next message in the thread is not a reply to the first message, but to the `--in-reply-to` argument, effectively breaking the threading.

In case the user does not specify an `--in-reply-to` argument, the `!defined $in_reply_to` condition is true, and thus the `$in_reply_to` variable is set to the first message's ID, and the threading works as expected, regardless of the message number.

To fix this bug, we need to ensure that the `$message_num` variable is not incremented by 1 when a message is edited and resent. We do this by decreasing the `$message_num` variable by 1 whenever the request to edit a message is received. This way, the next message in the thread will have the same message number as the edited message. Therefore the threading will work as expected.

The same logic has also been applied in case the user drops a single message from the thread by choosing the "[n]o" option during confirmation. By doing this, the next message in the thread is assigned the message number of the dropped message, and thus the threading works as expected.

Signed-off-by: Aditya Garg <gargaditya08@live.com>
---
 git-send-email.perl | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff --git a/git-send-email.perl b/git-send-email.perl
index 55b7e00d29..ac1d5b7070 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -1639,8 +1639,18 @@ sub send_message {
 		         default => $ask_default);
 		die __("Send this email reply required") unless defined $_;
 		if (/^n/i) {
+			# If we are skipping a message, we should make sure that
+			# the next message is treated as the successor to the
+			# previously sent message, and not the skipped message.
+			$message_num--;
 			return 0;
 		} elsif (/^e/i) {
+			# Since the same message will be sent again, we need to
+			# decrement the message number to the previous message.
+			# Otherwise, the edited message will be treated as a
+			# different message sent after the original non-edited
+			# message.
+			$message_num--;
 			return -1;
 		} elsif (/^q/i) {
 			cleanup_compose_files();
-- 
2.49.0.windows.1
Previous: Aditya GargNext: Junio C Hamano
Message 13 of 14 in “send-email: fix threads breaking in case user edits emails and improvements to outlook ID fix.”
  1. 0/2 send-email: fix threads breaking in case user edits emails and improvements to outlook ID fix.Aditya Garg, May 26, 2025
  2. 1/2 send-email: fix bug resulting in increased message number if a message is editedAditya Garg, May 26, 2025
  3. 2/2 send-email: show the new message id assigned by outlook in the logsAditya Garg, May 26, 2025
  4. 0/2 send-email: fix threads breaking in case user edits emails and improvements to outlook ID fix.Aditya Garg, May 29, 2025
  5. 1/2 send-email: fix bug resulting in broken threads if a message is editedAditya Garg, May 29, 2025
  6. 2/2 send-email: show the new message id assigned by outlook in the logsAditya Garg, May 29, 2025
  7. Junio C HamanoMay 29, 2025
  8. Aditya GargMay 29, 2025
  9. Junio C HamanoJun 4, 2025
  10. Aditya GargJun 4, 2025
  11. 0/2 send-email: fix threads breaking in case user edits emails and improvements to outlook ID fix.Aditya Garg, Jun 4, 2025
  12. 2/2 send-email: show the new message id assigned by outlook in the logsAditya Garg, Jun 4, 2025
  13. 1/2 send-email: fix bug resulting in broken threads if a message is editedAditya Garg, Jun 4, 2025
  14. Junio C HamanoJun 4, 2025

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.