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>
This commit is contained in:
Eric W. Biederman 2006-05-23 13:47:28 -06:00 committed by Junio C Hamano
parent 3350453014
commit 8b4525fb3c

View File

@ -237,38 +237,41 @@ static int eatspace(char *line)
#define SEEN_FROM 01 #define SEEN_FROM 01
#define SEEN_DATE 02 #define SEEN_DATE 02
#define SEEN_SUBJECT 04 #define SEEN_SUBJECT 04
#define SEEN_PREFIX 0x08
/* First lines of body can have From:, Date:, and Subject: */ /* First lines of body can have From:, Date:, and Subject: */
static int handle_inbody_header(int *seen, char *line) static void handle_inbody_header(int *seen, char *line)
{ {
if (*seen & SEEN_PREFIX)
return;
if (!memcmp("From:", line, 5) && isspace(line[5])) { if (!memcmp("From:", line, 5) && isspace(line[5])) {
if (!(*seen & SEEN_FROM) && handle_from(line+6)) { if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
*seen |= SEEN_FROM; *seen |= SEEN_FROM;
return 1; return;
} }
} }
if (!memcmp("Date:", line, 5) && isspace(line[5])) { if (!memcmp("Date:", line, 5) && isspace(line[5])) {
if (!(*seen & SEEN_DATE)) { if (!(*seen & SEEN_DATE)) {
handle_date(line+6); handle_date(line+6);
*seen |= SEEN_DATE; *seen |= SEEN_DATE;
return 1; return;
} }
} }
if (!memcmp("Subject:", line, 8) && isspace(line[8])) { if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
if (!(*seen & SEEN_SUBJECT)) { if (!(*seen & SEEN_SUBJECT)) {
handle_subject(line+9); handle_subject(line+9);
*seen |= SEEN_SUBJECT; *seen |= SEEN_SUBJECT;
return 1; return;
} }
} }
if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) { if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
if (!(*seen & SEEN_SUBJECT)) { if (!(*seen & SEEN_SUBJECT)) {
handle_subject(line); handle_subject(line);
*seen |= SEEN_SUBJECT; *seen |= SEEN_SUBJECT;
return 1; return;
} }
} }
return 0; *seen |= SEEN_PREFIX;
} }
static char *cleanup_subject(char *subject) static char *cleanup_subject(char *subject)
@ -590,12 +593,7 @@ static void decode_transfer_encoding(char *line)
static void handle_info(void) static void handle_info(void)
{ {
char *sub; char *sub;
static int done_info = 0;
if (done_info)
return;
done_info = 1;
sub = cleanup_subject(subject); sub = cleanup_subject(subject);
cleanup_space(name); cleanup_space(name);
cleanup_space(date); cleanup_space(date);
@ -609,7 +607,7 @@ static void handle_info(void)
/* We are inside message body and have read line[] already. /* We are inside message body and have read line[] already.
* Spit out the commit log. * Spit out the commit log.
*/ */
static int handle_commit_msg(void) static int handle_commit_msg(int *seen)
{ {
if (!cmitmsg) if (!cmitmsg)
return 0; return 0;
@ -633,6 +631,11 @@ static int handle_commit_msg(void)
decode_transfer_encoding(line); decode_transfer_encoding(line);
if (metainfo_charset) if (metainfo_charset)
convert_to_utf8(line, charset); convert_to_utf8(line, charset);
handle_inbody_header(seen, line);
if (!(*seen & SEEN_PREFIX))
continue;
fputs(line, cmitmsg); fputs(line, cmitmsg);
} while (fgets(line, sizeof(line), stdin) != NULL); } while (fgets(line, sizeof(line), stdin) != NULL);
fclose(cmitmsg); fclose(cmitmsg);
@ -664,26 +667,16 @@ static void handle_patch(void)
* that the first part to contain commit message and a patch, and * that the first part to contain commit message and a patch, and
* handle other parts as pure patches. * handle other parts as pure patches.
*/ */
static int handle_multipart_one_part(void) static int handle_multipart_one_part(int *seen)
{ {
int seen = 0;
int n = 0; int n = 0;
int len;
while (fgets(line, sizeof(line), stdin) != NULL) { while (fgets(line, sizeof(line), stdin) != NULL) {
again: again:
len = eatspace(line);
n++; n++;
if (!len)
continue;
if (is_multipart_boundary(line)) if (is_multipart_boundary(line))
break; break;
if (0 <= seen && handle_inbody_header(&seen, line)) if (handle_commit_msg(seen))
continue;
seen = -1; /* no more inbody headers */
line[len] = '\n';
handle_info();
if (handle_commit_msg())
goto again; goto again;
handle_patch(); handle_patch();
break; break;
@ -695,6 +688,7 @@ static int handle_multipart_one_part(void)
static void handle_multipart_body(void) static void handle_multipart_body(void)
{ {
int seen = 0;
int part_num = 0; int part_num = 0;
/* Skip up to the first boundary */ /* Skip up to the first boundary */
@ -709,7 +703,7 @@ static void handle_multipart_body(void)
while (1) { while (1) {
int hdr = read_one_header_line(line, sizeof(line), stdin); int hdr = read_one_header_line(line, sizeof(line), stdin);
if (!hdr) { if (!hdr) {
if (handle_multipart_one_part() < 0) if (handle_multipart_one_part(&seen) < 0)
return; return;
/* Reset per part headers */ /* Reset per part headers */
transfer_encoding = TE_DONTCARE; transfer_encoding = TE_DONTCARE;
@ -730,18 +724,9 @@ static void handle_body(void)
{ {
int seen = 0; int seen = 0;
while (fgets(line, sizeof(line), stdin) != NULL) { if (fgets(line, sizeof(line), stdin) != NULL) {
int len = eatspace(line); handle_commit_msg(&seen);
if (!len)
continue;
if (0 <= seen && handle_inbody_header(&seen, line))
continue;
seen = -1; /* no more inbody headers */
line[len] = '\n';
handle_info();
handle_commit_msg();
handle_patch(); handle_patch();
break;
} }
fclose(patchfile); fclose(patchfile);
if (!patch_lines) { if (!patch_lines) {
@ -791,6 +776,7 @@ int main(int argc, char **argv)
handle_multipart_body(); handle_multipart_body();
else else
handle_body(); handle_body();
handle_info();
break; break;
} }
check_header_line(line); check_header_line(line);