mailinfo: keep the parsed log message in a strbuf

When mailinfo() is eventually libified, the calling "git am" still will have to write out the log message in the "msg" file for hooks and other users of the information, but it does not have to reopen and reread what it wrote earlier if the function kept it in a strbuf. This also removes the need for seeking and truncating the output file when we see a scissors mark in the input, which in turn allows us to lose two callsites of die_errno(). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 17:43 UTC 05e625e5bf5231af984f17b82be9c48063b37242
1 file changed +16 -12
builtin/mailinfo.c
+16 -12
@@ -12,7 +12,6 @@
12 struct mailinfo {
13 FILE *input;
14 FILE *output;
15 - FILE *cmitmsg;
15 FILE *patchfile;
16
17 struct strbuf name;
@@ -36,6 +35,8 @@ struct mailinfo {
35 int header_stage; /* still checking in-body headers? */
36 struct strbuf **p_hdr_data;
37 struct strbuf **s_hdr_data;
38 +
39 + struct strbuf log_message;
40 };
41
42 static void cleanup_space(struct strbuf *sb)
@@ -676,10 +677,8 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
677
678 if (mi->use_scissors && is_scissors_line(line)) {
679 int i;
679 - if (fseek(mi->cmitmsg, 0L, SEEK_SET))
680 - die_errno("Could not rewind output message file");
681 - if (ftruncate(fileno(mi->cmitmsg), 0))
682 - die_errno("Could not truncate output message file at scissors");
680 +
681 + strbuf_setlen(&mi->log_message, 0);
682 mi->header_stage = 1;
683
684 /*
@@ -696,13 +695,12 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
695
696 if (patchbreak(line)) {
697 if (mi->message_id)
699 - fprintf(mi->cmitmsg, "Message-Id: %s\n", mi->message_id);
700 - fclose(mi->cmitmsg);
701 - mi->cmitmsg = NULL;
698 + strbuf_addf(&mi->log_message,
699 + "Message-Id: %s\n", mi->message_id);
700 return 1;
701 }
702
705 - fputs(line->buf, mi->cmitmsg);
703 + strbuf_addbuf(&mi->log_message, line);
704 return 0;
705 }
706
@@ -968,18 +966,19 @@ static void handle_info(struct mailinfo *mi)
966
967 static int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
968 {
969 + FILE *cmitmsg;
970 int peek;
971 struct strbuf line = STRBUF_INIT;
972
974 - mi->cmitmsg = fopen(msg, "w");
975 - if (!mi->cmitmsg) {
973 + cmitmsg = fopen(msg, "w");
974 + if (!cmitmsg) {
975 perror(msg);
976 return -1;
977 }
978 mi->patchfile = fopen(patch, "w");
979 if (!mi->patchfile) {
980 perror(patch);
982 - fclose(mi->cmitmsg);
981 + fclose(cmitmsg);
982 return -1;
983 }
984
@@ -996,6 +995,8 @@ static int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
995 check_header(mi, &line, mi->p_hdr_data, 1);
996
997 handle_body(mi, &line);
998 + fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);
999 + fclose(cmitmsg);
1000 fclose(mi->patchfile);
1001
1002 handle_info(mi);
@@ -1023,6 +1024,7 @@ static void setup_mailinfo(struct mailinfo *mi)
1024 strbuf_init(&mi->name, 0);
1025 strbuf_init(&mi->email, 0);
1026 strbuf_init(&mi->charset, 0);
1027 + strbuf_init(&mi->log_message, 0);
1028 mi->header_stage = 1;
1029 mi->use_inbody_headers = 1;
1030 mi->content_top = mi->content;
@@ -1049,6 +1051,8 @@ static void clear_mailinfo(struct mailinfo *mi)
1051 free(*(mi->content_top));
1052 mi->content_top--;
1053 }
1054 +
1055 + strbuf_release(&mi->log_message);
1056 }
1057
1058 static const char mailinfo_usage[] =