blame: extract find_single_final

Signed-off-by: Max Kirillov <max@max630.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Max Kirillov committed Oct 30, 2015 at 07:01 UTC 1b0d40000a511a8db81a20a6dab00984bdbc0d63
1 file changed +17 -12
builtin/blame.c
+17 -12
@@ -2396,16 +2396,11 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
2396 return commit;
2397 }
2398
2399 -static char *prepare_final(struct scoreboard *sb)
2399 +static struct object_array_entry *find_single_final(struct rev_info *revs)
2400 {
2401 int i;
2402 - const char *final_commit_name = NULL;
2403 - struct rev_info *revs = sb->revs;
2402 + struct object_array_entry *found = NULL;
2403
2405 - /*
2406 - * There must be one and only one positive commit in the
2407 - * revs->pending array.
2408 - */
2404 for (i = 0; i < revs->pending.nr; i++) {
2405 struct object *obj = revs->pending.objects[i].item;
2406 if (obj->flags & UNINTERESTING)
@@ -2414,14 +2409,24 @@ static char *prepare_final(struct scoreboard *sb)
2409 obj = deref_tag(obj, NULL, 0);
2410 if (obj->type != OBJ_COMMIT)
2411 die("Non commit %s?", revs->pending.objects[i].name);
2417 - if (sb->final)
2412 + if (found)
2413 die("More than one commit to dig from %s and %s?",
2414 revs->pending.objects[i].name,
2420 - final_commit_name);
2421 - sb->final = (struct commit *) obj;
2422 - final_commit_name = revs->pending.objects[i].name;
2415 + found->name);
2416 + found = &(revs->pending.objects[i]);
2417 + }
2418 + return found;
2419 +}
2420 +
2421 +static char *prepare_final(struct scoreboard *sb)
2422 +{
2423 + struct object_array_entry *found = find_single_final(sb->revs);
2424 + if (found) {
2425 + sb->final = (struct commit *) found->item;
2426 + return xstrdup(found->name);
2427 + } else {
2428 + return NULL;
2429 }
2424 - return xstrdup_or_null(final_commit_name);
2430 }
2431
2432 static char *prepare_initial(struct scoreboard *sb)