convert logmsg_reencode to get_commit_buffer

Like the callsites in the previous commit, logmsg_reencode already falls back to read_sha1_file when necessary. However, I split its conversion out into its own commit because it's a bit more complex. We return either: 1. The original commit->buffer 2. A newly allocated buffer from read_sha1_file 3. A reencoded buffer (based on either 1 or 2 above). while trying to do as few extra reads/allocations as possible. Callers currently free the result with logmsg_free, but we can simplify this by pointing them straight to unuse_commit_buffer. This is a slight layering violation, in that we may be passing a buffer from (3). However, since the end result is to free() anything except (1), which is unlikely to change, and because this makes the interface much simpler, it's a reasonable bending of the rules. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 10, 2014 at 17:41 UTC b66103c3baa593a39b8b0751213b9fce60e94de4
6 files changed +16 -35
builtin/blame.c
+2 -2
@@ -1416,7 +1416,7 @@ static void get_commit_info(struct commit *commit,
1416 &ret->author_time, &ret->author_tz);
1417
1418 if (!detailed) {
1419 - logmsg_free(message, commit);
1419 + unuse_commit_buffer(commit, message);
1420 return;
1421 }
1422
@@ -1430,7 +1430,7 @@ static void get_commit_info(struct commit *commit,
1430 else
1431 strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
1432
1433 - logmsg_free(message, commit);
1433 + unuse_commit_buffer(commit, message);
1434 }
1435
1436 /*
builtin/reset.c
+1 -1
@@ -109,7 +109,7 @@ static void print_new_head_line(struct commit *commit)
109 }
110 else
111 printf("\n");
112 - logmsg_free(msg, commit);
112 + unuse_commit_buffer(commit, msg);
113 }
114
115 static void update_index_from_diff(struct diff_queue_struct *q,
commit.h
-1
@@ -156,7 +156,6 @@ struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
156 extern const char *logmsg_reencode(const struct commit *commit,
157 char **commit_encoding,
158 const char *output_encoding);
159 -extern void logmsg_free(const char *msg, const struct commit *commit);
159 extern void get_commit_format(const char *arg, struct rev_info *);
160 extern const char *format_subject(struct strbuf *sb, const char *msg,
161 const char *line_separator);
pretty.c
+11 -29
@@ -613,22 +613,9 @@ const char *logmsg_reencode(const struct commit *commit,
613 static const char *utf8 = "UTF-8";
614 const char *use_encoding;
615 char *encoding;
616 - char *msg = commit->buffer;
616 + const char *msg = get_commit_buffer(commit);
617 char *out;
618
619 - if (!msg) {
620 - enum object_type type;
621 - unsigned long size;
622 -
623 - msg = read_sha1_file(commit->object.sha1, &type, &size);
624 - if (!msg)
625 - die("Cannot read commit object %s",
626 - sha1_to_hex(commit->object.sha1));
627 - if (type != OBJ_COMMIT)
628 - die("Expected commit for '%s', got %s",
629 - sha1_to_hex(commit->object.sha1), typename(type));
630 - }
631 -
619 if (!output_encoding || !*output_encoding) {
620 if (commit_encoding)
621 *commit_encoding =
@@ -652,12 +639,13 @@ const char *logmsg_reencode(const struct commit *commit,
639 * Otherwise, we still want to munge the encoding header in the
640 * result, which will be done by modifying the buffer. If we
641 * are using a fresh copy, we can reuse it. But if we are using
655 - * the cached copy from commit->buffer, we need to duplicate it
656 - * to avoid munging commit->buffer.
642 + * the cached copy from get_commit_buffer, we need to duplicate it
643 + * to avoid munging the cached copy.
644 */
658 - out = msg;
659 - if (out == commit->buffer)
660 - out = xstrdup(out);
645 + if (msg == get_cached_commit_buffer(commit))
646 + out = xstrdup(msg);
647 + else
648 + out = (char *)msg;
649 }
650 else {
651 /*
@@ -667,8 +655,8 @@ const char *logmsg_reencode(const struct commit *commit,
655 * copy, we can free it.
656 */
657 out = reencode_string(msg, output_encoding, use_encoding);
670 - if (out && msg != commit->buffer)
671 - free(msg);
658 + if (out)
659 + unuse_commit_buffer(commit, msg);
660 }
661
662 /*
@@ -687,12 +675,6 @@ const char *logmsg_reencode(const struct commit *commit,
675 return out ? out : msg;
676 }
677
690 -void logmsg_free(const char *msg, const struct commit *commit)
691 -{
692 - if (msg != commit->buffer)
693 - free((void *)msg);
694 -}
695 -
678 static int mailmap_name(const char **email, size_t *email_len,
679 const char **name, size_t *name_len)
680 {
@@ -1531,7 +1513,7 @@ void format_commit_message(const struct commit *commit,
1513 }
1514
1515 free(context.commit_encoding);
1534 - logmsg_free(context.message, commit);
1516 + unuse_commit_buffer(commit, context.message);
1517 free(context.signature_check.gpg_output);
1518 free(context.signature_check.signer);
1519 }
@@ -1767,7 +1749,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
1749 if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1750 strbuf_addch(sb, '\n');
1751
1770 - logmsg_free(reencoded, commit);
1752 + unuse_commit_buffer(commit, reencoded);
1753 }
1754
1755 void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
revision.c
+1 -1
@@ -2844,7 +2844,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
2844 retval = grep_buffer(&opt->grep_filter,
2845 (char *)message, strlen(message));
2846 strbuf_release(&buf);
2847 - logmsg_free(message, commit);
2847 + unuse_commit_buffer(commit, message);
2848 return retval;
2849 }
2850
sequencer.c
+1 -1
@@ -154,7 +154,7 @@ static int get_message(struct commit *commit, struct commit_message *out)
154 static void free_message(struct commit *commit, struct commit_message *msg)
155 {
156 free(msg->parent_label);
157 - logmsg_free(msg->message, commit);
157 + unuse_commit_buffer(commit, msg->message);
158 }
159
160 static void write_cherry_pick_head(struct commit *commit, const char *pseudoref)