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>

Jeff King committed Jun 10, 2014 at 17:41 UTC ba41c1c93fd9109eae954f75a8cb8e32c3e29530
2 files changed +7 -27
commit.c
+3 -13
@@ -583,22 +583,12 @@ static void record_author_date(struct author_date_slab *author_date,
583 struct commit *commit)
584 {
585 const char *buf, *line_end, *ident_line;
586 - char *buffer = NULL;
586 + const char *buffer = get_commit_buffer(commit);
587 struct ident_split ident;
588 char *date_end;
589 unsigned long date;
590
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) {
592 line_end = strchrnul(buf, '\n');
593 ident_line = skip_prefix(buf, "author ");
594 if (!ident_line) {
@@ -619,7 +609,7 @@ static void record_author_date(struct author_date_slab *author_date,
609 *(author_date_slab_at(author_date, commit)) = date;
610
611 fail_exit:
622 - free(buffer);
612 + unuse_commit_buffer(commit, buffer);
613 }
614
615 static int compare_commits_by_author_date(const void *a_, const void *b_,
sha1_name.c
+4 -14
@@ -862,27 +862,17 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
862 commit_list_insert(l->item, &backup);
863 }
864 while (list) {
865 - char *p, *to_free = NULL;
865 + const char *p, *buf;
866 struct commit *commit;
867 - enum object_type type;
868 - unsigned long size;
867 int matches;
868
869 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
870 if (!parse_object(commit->object.sha1))
871 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");
874 matches = p && !regexec(&regex, p + 2, 0, NULL, 0);
885 - free(to_free);
875 + unuse_commit_buffer(commit, buf);
876
877 if (matches) {
878 hashcpy(sha1, commit->object.sha1);