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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
a97934d820
commit
ba41c1c93f
16
commit.c
16
commit.c
@ -583,22 +583,12 @@ static void record_author_date(struct author_date_slab *author_date,
|
|||||||
struct commit *commit)
|
struct commit *commit)
|
||||||
{
|
{
|
||||||
const char *buf, *line_end, *ident_line;
|
const char *buf, *line_end, *ident_line;
|
||||||
char *buffer = NULL;
|
const char *buffer = get_commit_buffer(commit);
|
||||||
struct ident_split ident;
|
struct ident_split ident;
|
||||||
char *date_end;
|
char *date_end;
|
||||||
unsigned long date;
|
unsigned long date;
|
||||||
|
|
||||||
if (!commit->buffer) {
|
for (buf = buffer; buf; buf = line_end + 1) {
|
||||||
unsigned long size;
|
|
||||||
enum object_type type;
|
|
||||||
buffer = read_sha1_file(commit->object.sha1, &type, &size);
|
|
||||||
if (!buffer)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (buf = commit->buffer ? commit->buffer : buffer;
|
|
||||||
buf;
|
|
||||||
buf = line_end + 1) {
|
|
||||||
line_end = strchrnul(buf, '\n');
|
line_end = strchrnul(buf, '\n');
|
||||||
ident_line = skip_prefix(buf, "author ");
|
ident_line = skip_prefix(buf, "author ");
|
||||||
if (!ident_line) {
|
if (!ident_line) {
|
||||||
@ -619,7 +609,7 @@ static void record_author_date(struct author_date_slab *author_date,
|
|||||||
*(author_date_slab_at(author_date, commit)) = date;
|
*(author_date_slab_at(author_date, commit)) = date;
|
||||||
|
|
||||||
fail_exit:
|
fail_exit:
|
||||||
free(buffer);
|
unuse_commit_buffer(commit, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int compare_commits_by_author_date(const void *a_, const void *b_,
|
static int compare_commits_by_author_date(const void *a_, const void *b_,
|
||||||
|
18
sha1_name.c
18
sha1_name.c
@ -862,27 +862,17 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
|
|||||||
commit_list_insert(l->item, &backup);
|
commit_list_insert(l->item, &backup);
|
||||||
}
|
}
|
||||||
while (list) {
|
while (list) {
|
||||||
char *p, *to_free = NULL;
|
const char *p, *buf;
|
||||||
struct commit *commit;
|
struct commit *commit;
|
||||||
enum object_type type;
|
|
||||||
unsigned long size;
|
|
||||||
int matches;
|
int matches;
|
||||||
|
|
||||||
commit = pop_most_recent_commit(&list, ONELINE_SEEN);
|
commit = pop_most_recent_commit(&list, ONELINE_SEEN);
|
||||||
if (!parse_object(commit->object.sha1))
|
if (!parse_object(commit->object.sha1))
|
||||||
continue;
|
continue;
|
||||||
if (commit->buffer)
|
buf = get_commit_buffer(commit);
|
||||||
p = commit->buffer;
|
p = strstr(buf, "\n\n");
|
||||||
else {
|
|
||||||
p = read_sha1_file(commit->object.sha1, &type, &size);
|
|
||||||
if (!p)
|
|
||||||
continue;
|
|
||||||
to_free = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
p = strstr(p, "\n\n");
|
|
||||||
matches = p && !regexec(®ex, p + 2, 0, NULL, 0);
|
matches = p && !regexec(®ex, p + 2, 0, NULL, 0);
|
||||||
free(to_free);
|
unuse_commit_buffer(commit, buf);
|
||||||
|
|
||||||
if (matches) {
|
if (matches) {
|
||||||
hashcpy(sha1, commit->object.sha1);
|
hashcpy(sha1, commit->object.sha1);
|
||||||
|
Loading…
Reference in New Issue
Block a user