From 200ebe362cda2a520219f998d4b2c44767992bdb Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 26 Jan 2013 04:42:45 -0500 Subject: commit: drop useless xstrdup of commit message When git-commit is asked to reuse a commit message via "-c", we call read_commit_message, which looks up the commit and hands back either the re-encoded result, or a copy of the original. We make a copy in the latter case so that the ownership semantics of the return value are clear (in either case, it can be freed). However, since we return a "const char *", and since the resulting buffer's lifetime is the same as that of the whole program, we never bother to free it at all. Let's just drop the copy. That saves us a copy in the common case. While it does mean we leak in the re-encode case, it doesn't matter, since we are relying on program exit to free the memory anyway. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/commit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin/commit.c') diff --git a/builtin/commit.c b/builtin/commit.c index 38b9a9cc0..fbbb40fff 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -962,7 +962,7 @@ static const char *read_commit_message(const char *name) * encodings are identical. */ if (out == NULL) - out = xstrdup(commit->buffer); + out = commit->buffer; return out; } -- cgit v1.2.1 From dd0d388c44c28ebc021a24eeddc60287d4ea249c Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 26 Jan 2013 04:44:06 -0500 Subject: logmsg_reencode: never return NULL The logmsg_reencode function will return the reencoded commit buffer, or NULL if reencoding failed or no reencoding was necessary. Since every caller then ends up checking for NULL and just using the commit's original buffer, anyway, we can be a bit more helpful and just return that buffer when we would have returned NULL. Since the resulting string may or may not need to be freed, we introduce a logmsg_free, which checks whether the buffer came from the commit object or not (callers either implemented the same check already, or kept two separate pointers, one to mark the buffer to be used, and one for the to-be-freed string). Pushing this logic into logmsg_* simplifies the callers, and will let future patches lazily load the commit buffer in a single place. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/commit.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'builtin/commit.c') diff --git a/builtin/commit.c b/builtin/commit.c index fbbb40fff..6169f1e2d 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -946,24 +946,14 @@ static void handle_untracked_files_arg(struct wt_status *s) static const char *read_commit_message(const char *name) { - const char *out_enc, *out; + const char *out_enc; struct commit *commit; commit = lookup_commit_reference_by_name(name); if (!commit) die(_("could not lookup commit %s"), name); out_enc = get_commit_output_encoding(); - out = logmsg_reencode(commit, out_enc); - - /* - * If we failed to reencode the buffer, just copy it - * byte for byte so the user can try to fix it up. - * This also handles the case where input and output - * encodings are identical. - */ - if (out == NULL) - out = commit->buffer; - return out; + return logmsg_reencode(commit, out_enc); } static int parse_and_validate_options(int argc, const char *argv[], -- cgit v1.2.1