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

Commit 0fb370d

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

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

builtin/fsck.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ static int fsck_obj(struct object *obj)
310310
if (obj->type == OBJ_COMMIT) {
311311
struct commit *commit = (struct commit *) obj;
312312

313-
free(commit->buffer);
314-
commit->buffer = NULL;
313+
free_commit_buffer(commit);
315314

316315
if (!commit->parents && show_root)
317316
printf("root %s\n", sha1_to_hex(commit->object.sha1));

builtin/index-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
774774
}
775775
if (obj->type == OBJ_COMMIT) {
776776
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");
778779
}
779780
obj->flags |= FLAG_CHECKED;
780781
}

builtin/log.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ static int cmd_log_walk(struct rev_info *rev)
349349
rev->max_count++;
350350
if (!rev->reflog_info) {
351351
/* we allow cycles in reflog ancestry */
352-
free(commit->buffer);
353-
commit->buffer = NULL;
352+
free_commit_buffer(commit);
354353
}
355354
free_commit_list(commit->parents);
356355
commit->parents = NULL;
@@ -1508,8 +1507,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
15081507
reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
15091508
die(_("Failed to create output files"));
15101509
shown = log_tree_commit(&rev, commit);
1511-
free(commit->buffer);
1512-
commit->buffer = NULL;
1510+
free_commit_buffer(commit);
15131511

15141512
/* We put one extra blank line between formatted
15151513
* patches and this flag is used by log-tree code

builtin/rev-list.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ static void finish_commit(struct commit *commit, void *data)
173173
free_commit_list(commit->parents);
174174
commit->parents = NULL;
175175
}
176-
free(commit->buffer);
177-
commit->buffer = NULL;
176+
free_commit_buffer(commit);
178177
}
179178

180179
static void finish_object(struct object *obj,

commit.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ int unregister_shallow(const unsigned char *sha1)
245245
return 0;
246246
}
247247

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+
248261
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
249262
{
250263
const char *tail = buffer;

commit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
5151
int parse_commit(struct commit *item);
5252
void parse_commit_or_die(struct commit *item);
5353

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+
5465
/* Find beginning and length of commit subject. */
5566
int find_commit_subject(const char *commit_buffer, const char **subject);
5667

0 commit comments

Comments
 (0)