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

Commit 152ff1c

Browse files
peffgitster
authored andcommitted
provide helpers to access the commit buffer
Many sites look at commit->buffer to get more detailed information than what is in the parsed commit struct. However, we sometimes drop commit->buffer to save memory, in which case the caller would need to read the object afresh. Some callers do this (leading to duplicated code), and others do not (which opens the possibility of a segfault if somebody else frees the buffer). Let's provide a pair of helpers, "get" and "unuse", that let callers easily get the buffer. They will use the cached buffer when possible, and otherwise load from disk using read_sha1_file. Note that we also need to add a "get_cached" variant which returns NULL when we do not have a cached buffer. At first glance this seems to defeat the purpose of "get", which is to always provide a return value. However, some log code paths actually use the NULL-ness of commit->buffer as a boolean flag to decide whether to try printing the commit. At least for now, we want to continue supporting that use. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66c2827 commit 152ff1c

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

commit.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ void set_commit_buffer(struct commit *commit, void *buffer)
250250
commit->buffer = buffer;
251251
}
252252

253+
const void *get_cached_commit_buffer(const struct commit *commit)
254+
{
255+
return commit->buffer;
256+
}
257+
258+
const void *get_commit_buffer(const struct commit *commit)
259+
{
260+
const void *ret = get_cached_commit_buffer(commit);
261+
if (!ret) {
262+
enum object_type type;
263+
unsigned long size;
264+
ret = read_sha1_file(commit->object.sha1, &type, &size);
265+
if (!ret)
266+
die("cannot read commit object %s",
267+
sha1_to_hex(commit->object.sha1));
268+
if (type != OBJ_COMMIT)
269+
die("expected commit for %s, got %s",
270+
sha1_to_hex(commit->object.sha1), typename(type));
271+
}
272+
return ret;
273+
}
274+
275+
void unuse_commit_buffer(const struct commit *commit, const void *buffer)
276+
{
277+
if (commit->buffer != buffer)
278+
free((void *)buffer);
279+
}
280+
253281
void free_commit_buffer(struct commit *commit)
254282
{
255283
free(commit->buffer);

commit.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ void parse_commit_or_die(struct commit *item);
5757
*/
5858
void set_commit_buffer(struct commit *, void *buffer);
5959

60+
/*
61+
* Get any cached object buffer associated with the commit. Returns NULL
62+
* if none. The resulting memory should not be freed.
63+
*/
64+
const void *get_cached_commit_buffer(const struct commit *);
65+
66+
/*
67+
* Get the commit's object contents, either from cache or by reading the object
68+
* from disk. The resulting memory should not be modified, and must be given
69+
* to unuse_commit_buffer when the caller is done.
70+
*/
71+
const void *get_commit_buffer(const struct commit *);
72+
73+
/*
74+
* Tell the commit subsytem that we are done with a particular commit buffer.
75+
* The commit and buffer should be the input and return value, respectively,
76+
* from an earlier call to get_commit_buffer. The buffer may or may not be
77+
* freed by this call; callers should not access the memory afterwards.
78+
*/
79+
void unuse_commit_buffer(const struct commit *, const void *buffer);
80+
6081
/*
6182
* Free any cached object buffer associated with the commit.
6283
*/

0 commit comments

Comments
 (0)