Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit b66103c

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ba41c1c commit b66103c

File tree

6 files changed

+16
-35
lines changed

6 files changed

+16
-35
lines changed

builtin/blame.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ static void get_commit_info(struct commit *commit,
14161416
&ret->author_time, &ret->author_tz);
14171417

14181418
if (!detailed) {
1419-
logmsg_free(message, commit);
1419+
unuse_commit_buffer(commit, message);
14201420
return;
14211421
}
14221422

@@ -1430,7 +1430,7 @@ static void get_commit_info(struct commit *commit,
14301430
else
14311431
strbuf_addf(&ret->summary, "(%s)", sha1_to_hex(commit->object.sha1));
14321432

1433-
logmsg_free(message, commit);
1433+
unuse_commit_buffer(commit, message);
14341434
}
14351435

14361436
/*

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static void print_new_head_line(struct commit *commit)
109109
}
110110
else
111111
printf("\n");
112-
logmsg_free(msg, commit);
112+
unuse_commit_buffer(commit, msg);
113113
}
114114

115115
static void update_index_from_diff(struct diff_queue_struct *q,

commit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
156156
extern const char *logmsg_reencode(const struct commit *commit,
157157
char **commit_encoding,
158158
const char *output_encoding);
159-
extern void logmsg_free(const char *msg, const struct commit *commit);
160159
extern void get_commit_format(const char *arg, struct rev_info *);
161160
extern const char *format_subject(struct strbuf *sb, const char *msg,
162161
const char *line_separator);

pretty.c

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -613,22 +613,9 @@ const char *logmsg_reencode(const struct commit *commit,
613613
static const char *utf8 = "UTF-8";
614614
const char *use_encoding;
615615
char *encoding;
616-
char *msg = commit->buffer;
616+
const char *msg = get_commit_buffer(commit);
617617
char *out;
618618

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-
632619
if (!output_encoding || !*output_encoding) {
633620
if (commit_encoding)
634621
*commit_encoding =
@@ -652,12 +639,13 @@ const char *logmsg_reencode(const struct commit *commit,
652639
* Otherwise, we still want to munge the encoding header in the
653640
* result, which will be done by modifying the buffer. If we
654641
* 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.
657644
*/
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;
661649
}
662650
else {
663651
/*
@@ -667,8 +655,8 @@ const char *logmsg_reencode(const struct commit *commit,
667655
* copy, we can free it.
668656
*/
669657
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);
672660
}
673661

674662
/*
@@ -687,12 +675,6 @@ const char *logmsg_reencode(const struct commit *commit,
687675
return out ? out : msg;
688676
}
689677

690-
void logmsg_free(const char *msg, const struct commit *commit)
691-
{
692-
if (msg != commit->buffer)
693-
free((void *)msg);
694-
}
695-
696678
static int mailmap_name(const char **email, size_t *email_len,
697679
const char **name, size_t *name_len)
698680
{
@@ -1531,7 +1513,7 @@ void format_commit_message(const struct commit *commit,
15311513
}
15321514

15331515
free(context.commit_encoding);
1534-
logmsg_free(context.message, commit);
1516+
unuse_commit_buffer(commit, context.message);
15351517
free(context.signature_check.gpg_output);
15361518
free(context.signature_check.signer);
15371519
}
@@ -1767,7 +1749,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
17671749
if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
17681750
strbuf_addch(sb, '\n');
17691751

1770-
logmsg_free(reencoded, commit);
1752+
unuse_commit_buffer(commit, reencoded);
17711753
}
17721754

17731755
void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
28442844
retval = grep_buffer(&opt->grep_filter,
28452845
(char *)message, strlen(message));
28462846
strbuf_release(&buf);
2847-
logmsg_free(message, commit);
2847+
unuse_commit_buffer(commit, message);
28482848
return retval;
28492849
}
28502850

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static int get_message(struct commit *commit, struct commit_message *out)
154154
static void free_message(struct commit *commit, struct commit_message *msg)
155155
{
156156
free(msg->parent_label);
157-
logmsg_free(msg->message, commit);
157+
unuse_commit_buffer(commit, msg->message);
158158
}
159159

160160
static void write_cherry_pick_head(struct commit *commit, const char *pseudoref)

0 commit comments

Comments
 (0)