1717#include "run-command.h"
1818#include "shortlog.h"
1919#include "remote.h"
20+ #include "string-list.h"
2021
2122/* Set a default date-time format for git log ("log.date" config variable) */
2223static const char * default_date_mode = NULL ;
@@ -461,6 +462,10 @@ static void add_header(const char *value)
461462 extra_hdr [extra_hdr_nr ++ ] = xstrndup (value , len );
462463}
463464
465+ #define THREAD_SHALLOW 1
466+ #define THREAD_DEEP 2
467+ static int thread = 0 ;
468+
464469static int git_format_config (const char * var , const char * value , void * cb )
465470{
466471 if (!strcmp (var , "format.headers" )) {
@@ -497,7 +502,18 @@ static int git_format_config(const char *var, const char *value, void *cb)
497502 default_attach = xstrdup (git_version_string );
498503 return 0 ;
499504 }
500-
505+ if (!strcmp (var , "format.thread" )) {
506+ if (value && !strcasecmp (value , "deep" )) {
507+ thread = THREAD_DEEP ;
508+ return 0 ;
509+ }
510+ if (value && !strcasecmp (value , "shallow" )) {
511+ thread = THREAD_SHALLOW ;
512+ return 0 ;
513+ }
514+ thread = git_config_bool (var , value ) && THREAD_SHALLOW ;
515+ return 0 ;
516+ }
501517
502518 return git_log_config (var , value , cb );
503519}
@@ -776,7 +792,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
776792 int numbered_files = 0 ; /* _just_ numbers */
777793 int subject_prefix = 0 ;
778794 int ignore_if_in_upstream = 0 ;
779- int thread = 0 ;
780795 int cover_letter = 0 ;
781796 int boundary_count = 0 ;
782797 int no_binary_diff = 0 ;
@@ -878,8 +893,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
878893 }
879894 else if (!strcmp (argv [i ], "--ignore-if-in-upstream" ))
880895 ignore_if_in_upstream = 1 ;
881- else if (!strcmp (argv [i ], "--thread" ))
882- thread = 1 ;
896+ else if (!strcmp (argv [i ], "--thread" )
897+ || !strcmp (argv [i ], "--thread=shallow" ))
898+ thread = THREAD_SHALLOW ;
899+ else if (!strcmp (argv [i ], "--thread=deep" ))
900+ thread = THREAD_DEEP ;
901+ else if (!strcmp (argv [i ], "--no-thread" ))
902+ thread = 0 ;
883903 else if (!prefixcmp (argv [i ], "--in-reply-to=" ))
884904 in_reply_to = argv [i ] + 14 ;
885905 else if (!strcmp (argv [i ], "--in-reply-to" )) {
@@ -1030,8 +1050,12 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10301050 numbered = 1 ;
10311051 if (numbered )
10321052 rev .total = total + start_number - 1 ;
1033- if (in_reply_to )
1034- rev .ref_message_id = clean_message_id (in_reply_to );
1053+ if (in_reply_to || thread || cover_letter )
1054+ rev .ref_message_ids = xcalloc (1 , sizeof (struct string_list ));
1055+ if (in_reply_to ) {
1056+ const char * msgid = clean_message_id (in_reply_to );
1057+ string_list_append (msgid , rev .ref_message_ids );
1058+ }
10351059 if (cover_letter ) {
10361060 if (thread )
10371061 gen_message_id (& rev , "cover" );
@@ -1050,15 +1074,33 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10501074 /* Have we already had a message ID? */
10511075 if (rev .message_id ) {
10521076 /*
1053- * If we've got the ID to be a reply
1054- * to, discard the current ID;
1055- * otherwise, make everything a reply
1056- * to that.
1077+ * For deep threading: make every mail
1078+ * a reply to the previous one, no
1079+ * matter what other options are set.
1080+ *
1081+ * For shallow threading:
1082+ *
1083+ * Without --cover-letter and
1084+ * --in-reply-to, make every mail a
1085+ * reply to the one before.
1086+ *
1087+ * With --in-reply-to but no
1088+ * --cover-letter, make every mail a
1089+ * reply to the <reply-to>.
1090+ *
1091+ * With --cover-letter, make every
1092+ * mail but the cover letter a reply
1093+ * to the cover letter. The cover
1094+ * letter is a reply to the
1095+ * --in-reply-to, if specified.
10571096 */
1058- if (rev .ref_message_id )
1097+ if (thread == THREAD_SHALLOW
1098+ && rev .ref_message_ids -> nr > 0
1099+ && (!cover_letter || rev .nr > 1 ))
10591100 free (rev .message_id );
10601101 else
1061- rev .ref_message_id = rev .message_id ;
1102+ string_list_append (rev .message_id ,
1103+ rev .ref_message_ids );
10621104 }
10631105 gen_message_id (& rev , sha1_to_hex (commit -> object .sha1 ));
10641106 }
0 commit comments