]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rev-list: make --count work with --objects
authorJeff King <peff@peff.net>
Fri, 14 Feb 2020 18:22:20 +0000 (13:22 -0500)
committerJunio C Hamano <gitster@pobox.com>
Fri, 14 Feb 2020 18:46:22 +0000 (10:46 -0800)
The current behavior from "rev-list --count --objects" is nonsensical:
we enumerate all of the objects except commits, but then give a count of
commits. This wasn't planned, and is just what the code happens to do.

Instead, let's give the answer the user almost certainly wanted: the
full count of objects.

Note that there are more complicated cases around cherry-marking, etc.
We'll punt on those for now, but let the user know that we can't produce
an answer (rather than giving them something useless).

We'll test both the new feature as well as a vanilla --count of commits,
since that surprisingly doesn't seem to be covered in the existing
tests.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-list.c
t/t6000-rev-list-misc.sh

index 38c5ca5603bf2f70986fcd9903fb75d6ace4735d..94521239884f82b2da33fa1ed6837a2b916368af 100644 (file)
@@ -253,11 +253,19 @@ static int finish_object(struct object *obj, const char *name, void *cb_data)
 static void show_object(struct object *obj, const char *name, void *cb_data)
 {
        struct rev_list_info *info = cb_data;
+       struct rev_info *revs = info->revs;
+
        if (finish_object(obj, name, cb_data))
                return;
        display_progress(progress, ++progress_counter);
        if (info->flags & REV_LIST_QUIET)
                return;
+
+       if (revs->count) {
+               revs->count_right++;
+               return;
+       }
+
        if (arg_show_object_names)
                show_object_with_name(stdout, obj, name);
        else
@@ -584,6 +592,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
        if (revs.show_notes)
                die(_("rev-list does not support display of notes"));
 
+       if (revs.count &&
+           (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
+           (revs.left_right || revs.cherry_mark))
+               die(_("marked counting is incompatible with --objects"));
+
        if (filter_options.choice)
                use_bitmap_index = 0;
 
index b8cf82349b1d6d0405342cf98ead948d2e81c2a5..383f2c457d4137ad16f5b49424f094aded12e74c 100755 (executable)
@@ -148,4 +148,16 @@ test_expect_success 'rev-list --end-of-options' '
        test_cmp expect actual
 '
 
+test_expect_success 'rev-list --count' '
+       count=$(git rev-list --count HEAD) &&
+       git rev-list HEAD >actual &&
+       test_line_count = $count actual
+'
+
+test_expect_success 'rev-list --count --objects' '
+       count=$(git rev-list --count --objects HEAD) &&
+       git rev-list --objects HEAD >actual &&
+       test_line_count = $count actual
+'
+
 test_done