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

Commit ba41c1c

Browse files
peffgitster
authored andcommitted
use get_commit_buffer to avoid duplicate code
For both of these sites, we already do the "fallback to read_sha1_file" trick. But we can shorten the code by just using get_commit_buffer. Note that the error cases are slightly different when read_sha1_file fails. get_commit_buffer will die() if the object cannot be loaded, or is a non-commit. For get_sha1_oneline, this will almost certainly never happen, as we will have just called parse_object (and if it does, it's probably worth complaining about). For record_author_date, the new behavior is probably better; we notify the user of the error instead of silently ignoring it. And because it's used only for sorting by author-date, somebody examining a corrupt repo can fallback to the regular traversal order. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a97934d commit ba41c1c

File tree

2 files changed

+7
-27
lines changed

2 files changed

+7
-27
lines changed

commit.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -583,22 +583,12 @@ static void record_author_date(struct author_date_slab *author_date,
583583
struct commit *commit)
584584
{
585585
const char *buf, *line_end, *ident_line;
586-
char *buffer = NULL;
586+
const char *buffer = get_commit_buffer(commit);
587587
struct ident_split ident;
588588
char *date_end;
589589
unsigned long date;
590590

591-
if (!commit->buffer) {
592-
unsigned long size;
593-
enum object_type type;
594-
buffer = read_sha1_file(commit->object.sha1, &type, &size);
595-
if (!buffer)
596-
return;
597-
}
598-
599-
for (buf = commit->buffer ? commit->buffer : buffer;
600-
buf;
601-
buf = line_end + 1) {
591+
for (buf = buffer; buf; buf = line_end + 1) {
602592
line_end = strchrnul(buf, '\n');
603593
ident_line = skip_prefix(buf, "author ");
604594
if (!ident_line) {
@@ -619,7 +609,7 @@ static void record_author_date(struct author_date_slab *author_date,
619609
*(author_date_slab_at(author_date, commit)) = date;
620610

621611
fail_exit:
622-
free(buffer);
612+
unuse_commit_buffer(commit, buffer);
623613
}
624614

625615
static int compare_commits_by_author_date(const void *a_, const void *b_,

sha1_name.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -862,27 +862,17 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
862862
commit_list_insert(l->item, &backup);
863863
}
864864
while (list) {
865-
char *p, *to_free = NULL;
865+
const char *p, *buf;
866866
struct commit *commit;
867-
enum object_type type;
868-
unsigned long size;
869867
int matches;
870868

871869
commit = pop_most_recent_commit(&list, ONELINE_SEEN);
872870
if (!parse_object(commit->object.sha1))
873871
continue;
874-
if (commit->buffer)
875-
p = commit->buffer;
876-
else {
877-
p = read_sha1_file(commit->object.sha1, &type, &size);
878-
if (!p)
879-
continue;
880-
to_free = p;
881-
}
882-
883-
p = strstr(p, "\n\n");
872+
buf = get_commit_buffer(commit);
873+
p = strstr(buf, "\n\n");
884874
matches = p && !regexec(&regex, p + 2, 0, NULL, 0);
885-
free(to_free);
875+
unuse_commit_buffer(commit, buf);
886876

887877
if (matches) {
888878
hashcpy(sha1, commit->object.sha1);

0 commit comments

Comments
 (0)