pack: move approximate object count to object store
authorStefan Beller <sbeller@google.com>
Fri, 23 Mar 2018 17:21:02 +0000 (18:21 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Mar 2018 17:05:55 +0000 (10:05 -0700)
The approximate_object_count() function maintains a rough count of
objects in a repository to estimate how long object name abbreviates
should be. Object names are scoped to a repository and the
appropriate length may differ by repository, so the object count
should not be global.

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-store.h
packfile.c
index 6a07a14d63b78c18c33f7d3f34b7d1db375c5bee..b53e1259023eecf0151dfe34146f40c96b9f99a3 100644 (file)
@@ -99,6 +99,14 @@ struct raw_object_store {
        /* A most-recently-used ordered version of the packed_git list. */
        struct list_head packed_git_mru;
 
+       /*
+        * A fast, rough count of the number of objects in the repository.
+        * These two fields are not meant for direct access. Use
+        * approximate_object_count() instead.
+        */
+       unsigned long approximate_object_count;
+       unsigned approximate_object_count_valid : 1;
+
        /*
         * Whether packed_git has already been populated with this repository's
         * packs.
index 98162a051332f102762a49ad08c638e8a870ed7e..36922b5872447753480cf095b41376f75a230677 100644 (file)
@@ -803,8 +803,6 @@ static void prepare_packed_git_one(char *objdir, int local)
        strbuf_release(&path);
 }
 
-static int approximate_object_count_valid;
-
 /*
  * Give a fast, rough count of the number of objects in the repository. This
  * ignores loose objects completely. If you have a lot of them, then either
@@ -814,8 +812,8 @@ static int approximate_object_count_valid;
  */
 unsigned long approximate_object_count(void)
 {
-       static unsigned long count;
-       if (!approximate_object_count_valid) {
+       if (!the_repository->objects->approximate_object_count_valid) {
+               unsigned long count;
                struct packed_git *p;
 
                prepare_packed_git();
@@ -825,8 +823,9 @@ unsigned long approximate_object_count(void)
                                continue;
                        count += p->num_objects;
                }
+               the_repository->objects->approximate_object_count = count;
        }
-       return count;
+       return the_repository->objects->approximate_object_count;
 }
 
 static void *get_next_packed_git(const void *p)
@@ -901,7 +900,7 @@ void prepare_packed_git(void)
 
 void reprepare_packed_git(void)
 {
-       approximate_object_count_valid = 0;
+       the_repository->objects->approximate_object_count_valid = 0;
        the_repository->objects->packed_git_initialized = 0;
        prepare_packed_git();
 }