Skip to content

Commit 05e625e

Browse files
committed
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 <[email protected]>
1 parent 4933910 commit 05e625e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

builtin/mailinfo.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
struct mailinfo {
1313
FILE *input;
1414
FILE *output;
15-
FILE *cmitmsg;
1615
FILE *patchfile;
1716

1817
struct strbuf name;
@@ -36,6 +35,8 @@ struct mailinfo {
3635
int header_stage; /* still checking in-body headers? */
3736
struct strbuf **p_hdr_data;
3837
struct strbuf **s_hdr_data;
38+
39+
struct strbuf log_message;
3940
};
4041

4142
static void cleanup_space(struct strbuf *sb)
@@ -676,10 +677,8 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
676677

677678
if (mi->use_scissors && is_scissors_line(line)) {
678679
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);
683682
mi->header_stage = 1;
684683

685684
/*
@@ -696,13 +695,12 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
696695

697696
if (patchbreak(line)) {
698697
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);
702700
return 1;
703701
}
704702

705-
fputs(line->buf, mi->cmitmsg);
703+
strbuf_addbuf(&mi->log_message, line);
706704
return 0;
707705
}
708706

@@ -968,18 +966,19 @@ static void handle_info(struct mailinfo *mi)
968966

969967
static int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
970968
{
969+
FILE *cmitmsg;
971970
int peek;
972971
struct strbuf line = STRBUF_INIT;
973972

974-
mi->cmitmsg = fopen(msg, "w");
975-
if (!mi->cmitmsg) {
973+
cmitmsg = fopen(msg, "w");
974+
if (!cmitmsg) {
976975
perror(msg);
977976
return -1;
978977
}
979978
mi->patchfile = fopen(patch, "w");
980979
if (!mi->patchfile) {
981980
perror(patch);
982-
fclose(mi->cmitmsg);
981+
fclose(cmitmsg);
983982
return -1;
984983
}
985984

@@ -996,6 +995,8 @@ static int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
996995
check_header(mi, &line, mi->p_hdr_data, 1);
997996

998997
handle_body(mi, &line);
998+
fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);
999+
fclose(cmitmsg);
9991000
fclose(mi->patchfile);
10001001

10011002
handle_info(mi);
@@ -1023,6 +1024,7 @@ static void setup_mailinfo(struct mailinfo *mi)
10231024
strbuf_init(&mi->name, 0);
10241025
strbuf_init(&mi->email, 0);
10251026
strbuf_init(&mi->charset, 0);
1027+
strbuf_init(&mi->log_message, 0);
10261028
mi->header_stage = 1;
10271029
mi->use_inbody_headers = 1;
10281030
mi->content_top = mi->content;
@@ -1049,6 +1051,8 @@ static void clear_mailinfo(struct mailinfo *mi)
10491051
free(*(mi->content_top));
10501052
mi->content_top--;
10511053
}
1054+
1055+
strbuf_release(&mi->log_message);
10521056
}
10531057

10541058
static const char mailinfo_usage[] =

0 commit comments

Comments
 (0)