Refactor commit messge handling.
- Move handle_info into main so it is called once after everything has been parsed. This allows the removal of a static variable and removes two duplicate calls. - Move parsing of inbody headers into handle_commit. This means we parse the in-body headers after we have decoded the character set, and it removes code duplication between handle_multipart_one_part and handle_body. - Change the flag indicating that we have seen an in body prefix header into another bit in seen. This is a little more general and allows the possibility of parsing in body headers after the body message has begun. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric W. Biederman committed
May 23, 2006 at 13:47 UTC
8b4525fb3c6d79bd3a64b8f441237a4095db4e22
1 file changed
+22
-36
mailinfo.c
+22
-36
@@ -237,38 +237,41 @@ static int eatspace(char *line)
237
#define SEEN_FROM 01
238
#define SEEN_DATE 02
239
#define SEEN_SUBJECT 04
240
+#define SEEN_PREFIX 0x08
241
242
/* First lines of body can have From:, Date:, and Subject: */
242
-static int handle_inbody_header(int *seen, char *line)
243
+static void handle_inbody_header(int *seen, char *line)
244
{
245
+ if (*seen & SEEN_PREFIX)
246
+ return;
247
if (!memcmp("From:", line, 5) && isspace(line[5])) {
248
if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
249
*seen |= SEEN_FROM;
247
- return 1;
250
+ return;
251
}
252
}
253
if (!memcmp("Date:", line, 5) && isspace(line[5])) {
254
if (!(*seen & SEEN_DATE)) {
255
handle_date(line+6);
256
*seen |= SEEN_DATE;
254
- return 1;
257
+ return;
258
}
259
}
260
if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
261
if (!(*seen & SEEN_SUBJECT)) {
262
handle_subject(line+9);
263
*seen |= SEEN_SUBJECT;
261
- return 1;
264
+ return;
265
}
266
}
267
if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
268
if (!(*seen & SEEN_SUBJECT)) {
269
handle_subject(line);
270
*seen |= SEEN_SUBJECT;
268
- return 1;
271
+ return;
272
}
273
}
271
- return 0;
274
+ *seen |= SEEN_PREFIX;
275
}
276
277
static char *cleanup_subject(char *subject)
@@ -590,12 +593,7 @@ static void decode_transfer_encoding(char *line)
593
static void handle_info(void)
594
{
595
char *sub;
593
- static int done_info = 0;
594
-
595
- if (done_info)
596
- return;
596
598
- done_info = 1;
597
sub = cleanup_subject(subject);
598
cleanup_space(name);
599
cleanup_space(date);
@@ -609,7 +607,7 @@ static void handle_info(void)
607
/* We are inside message body and have read line[] already.
608
* Spit out the commit log.
609
*/
612
-static int handle_commit_msg(void)
610
+static int handle_commit_msg(int *seen)
611
{
612
if (!cmitmsg)
613
return 0;
@@ -633,6 +631,11 @@ static int handle_commit_msg(void)
631
decode_transfer_encoding(line);
632
if (metainfo_charset)
633
convert_to_utf8(line, charset);
634
+
635
+ handle_inbody_header(seen, line);
636
+ if (!(*seen & SEEN_PREFIX))
637
+ continue;
638
+
639
fputs(line, cmitmsg);
640
} while (fgets(line, sizeof(line), stdin) != NULL);
641
fclose(cmitmsg);
@@ -664,26 +667,16 @@ static void handle_patch(void)
667
* that the first part to contain commit message and a patch, and
668
* handle other parts as pure patches.
669
*/
667
-static int handle_multipart_one_part(void)
670
+static int handle_multipart_one_part(int *seen)
671
{
669
- int seen = 0;
672
int n = 0;
671
- int len;
673
674
while (fgets(line, sizeof(line), stdin) != NULL) {
675
again:
675
- len = eatspace(line);
676
n++;
677
- if (!len)
678
- continue;
677
if (is_multipart_boundary(line))
678
break;
681
- if (0 <= seen && handle_inbody_header(&seen, line))
682
- continue;
683
- seen = -1; /* no more inbody headers */
684
- line[len] = '\n';
685
- handle_info();
686
- if (handle_commit_msg())
679
+ if (handle_commit_msg(seen))
680
goto again;
681
handle_patch();
682
break;
@@ -695,6 +688,7 @@ static int handle_multipart_one_part(void)
688
689
static void handle_multipart_body(void)
690
{
691
+ int seen = 0;
692
int part_num = 0;
693
694
/* Skip up to the first boundary */
@@ -709,7 +703,7 @@ static void handle_multipart_body(void)
703
while (1) {
704
int hdr = read_one_header_line(line, sizeof(line), stdin);
705
if (!hdr) {
712
- if (handle_multipart_one_part() < 0)
706
+ if (handle_multipart_one_part(&seen) < 0)
707
return;
708
/* Reset per part headers */
709
transfer_encoding = TE_DONTCARE;
@@ -730,18 +724,9 @@ static void handle_body(void)
724
{
725
int seen = 0;
726
733
- while (fgets(line, sizeof(line), stdin) != NULL) {
734
- int len = eatspace(line);
735
- if (!len)
736
- continue;
737
- if (0 <= seen && handle_inbody_header(&seen, line))
738
- continue;
739
- seen = -1; /* no more inbody headers */
740
- line[len] = '\n';
741
- handle_info();
742
- handle_commit_msg();
727
+ if (fgets(line, sizeof(line), stdin) != NULL) {
728
+ handle_commit_msg(&seen);
729
handle_patch();
744
- break;
730
}
731
fclose(patchfile);
732
if (!patch_lines) {
@@ -791,6 +776,7 @@ int main(int argc, char **argv)
776
handle_multipart_body();
777
else
778
handle_body();
779
+ handle_info();
780
break;
781
}
782
check_header_line(line);