provide a helper to free commit buffer

This converts two lines into one at each caller. But more importantly, it abstracts the concept of freeing the buffer, which will make it easier to change later. Note that we also need to provide a "detach" mechanism for a tricky case in index-pack. We are passed a buffer for the object generated by processing the incoming pack. If we are not using --strict, we just calculate the sha1 on that buffer and return, leaving the caller to free it. But if we are using --strict, we actually attach that buffer to an object, pass the object to the fsck functions, and then detach the buffer from the object again (so that the caller can free it as usual). In this case, we don't want to free the buffer ourselves, but just make sure it is no longer associated with the commit. Note that we are making the assumption here that the attach/detach process does not impact the buffer at all (e.g., it is never reallocated or modified). That holds true now, and we have no plans to change that. However, as we abstract the commit_buffer code, this dependency becomes less obvious. So when we detach, let's also make sure that we get back the same buffer that we gave to the commit_buffer code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 12, 2014 at 18:05 UTC 0fb370da9ca972f9571530f95c0dacb31368c280
6 files changed +30 -9
builtin/fsck.c
+1 -2
@@ -310,8 +310,7 @@ static int fsck_obj(struct object *obj)
310 if (obj->type == OBJ_COMMIT) {
311 struct commit *commit = (struct commit *) obj;
312
313 - free(commit->buffer);
314 - commit->buffer = NULL;
313 + free_commit_buffer(commit);
314
315 if (!commit->parents && show_root)
316 printf("root %s\n", sha1_to_hex(commit->object.sha1));
builtin/index-pack.c
+2 -1
@@ -774,7 +774,8 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
774 }
775 if (obj->type == OBJ_COMMIT) {
776 struct commit *commit = (struct commit *) obj;
777 - commit->buffer = NULL;
777 + if (detach_commit_buffer(commit) != data)
778 + die("BUG: parse_object_buffer transmogrified our buffer");
779 }
780 obj->flags |= FLAG_CHECKED;
781 }
builtin/log.c
+2 -4
@@ -349,8 +349,7 @@ static int cmd_log_walk(struct rev_info *rev)
349 rev->max_count++;
350 if (!rev->reflog_info) {
351 /* we allow cycles in reflog ancestry */
352 - free(commit->buffer);
353 - commit->buffer = NULL;
352 + free_commit_buffer(commit);
353 }
354 free_commit_list(commit->parents);
355 commit->parents = NULL;
@@ -1508,8 +1507,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1507 reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
1508 die(_("Failed to create output files"));
1509 shown = log_tree_commit(&rev, commit);
1511 - free(commit->buffer);
1512 - commit->buffer = NULL;
1510 + free_commit_buffer(commit);
1511
1512 /* We put one extra blank line between formatted
1513 * patches and this flag is used by log-tree code
builtin/rev-list.c
+1 -2
@@ -173,8 +173,7 @@ static void finish_commit(struct commit *commit, void *data)
173 free_commit_list(commit->parents);
174 commit->parents = NULL;
175 }
176 - free(commit->buffer);
177 - commit->buffer = NULL;
176 + free_commit_buffer(commit);
177 }
178
179 static void finish_object(struct object *obj,
commit.c
+13
@@ -245,6 +245,19 @@ int unregister_shallow(const unsigned char *sha1)
245 return 0;
246 }
247
248 +void free_commit_buffer(struct commit *commit)
249 +{
250 + free(commit->buffer);
251 + commit->buffer = NULL;
252 +}
253 +
254 +const void *detach_commit_buffer(struct commit *commit)
255 +{
256 + void *ret = commit->buffer;
257 + commit->buffer = NULL;
258 + return ret;
259 +}
260 +
261 int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
262 {
263 const char *tail = buffer;
commit.h
+11
@@ -51,6 +51,17 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
51 int parse_commit(struct commit *item);
52 void parse_commit_or_die(struct commit *item);
53
54 +/*
55 + * Free any cached object buffer associated with the commit.
56 + */
57 +void free_commit_buffer(struct commit *);
58 +
59 +/*
60 + * Disassociate any cached object buffer from the commit, but do not free it.
61 + * The buffer (or NULL, if none) is returned.
62 + */
63 +const void *detach_commit_buffer(struct commit *);
64 +
65 /* Find beginning and length of commit subject. */
66 int find_commit_subject(const char *commit_buffer, const char **subject);
67