]> git.ipfire.org Git - thirdparty/git.git/blobdiff - upload-pack.c
upload-pack.c: avoid enumerating hidden refs where possible
[thirdparty/git.git] / upload-pack.c
index 99d216938c2577b1b37126cae298fdb04045c887..ed0126de494f09550581803bdeb3ed6ea2eaaa11 100644 (file)
@@ -602,11 +602,36 @@ static int get_common_commits(struct upload_pack_data *data,
        }
 }
 
+static int allow_hidden_refs(enum allow_uor allow_uor)
+{
+       if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
+               return 1;
+       return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
+}
+
+static void for_each_namespaced_ref_1(each_ref_fn fn,
+                                     struct upload_pack_data *data)
+{
+       const char **excludes = NULL;
+       /*
+        * If `data->allow_uor` allows fetching hidden refs, we need to
+        * mark all references (including hidden ones), to check in
+        * `is_our_ref()` below.
+        *
+        * Otherwise, we only care about whether each reference's object
+        * has the OUR_REF bit set or not, so do not need to visit
+        * hidden references.
+        */
+       if (allow_hidden_refs(data->allow_uor))
+               excludes = hidden_refs_to_excludes(&data->hidden_refs);
+
+       for_each_namespaced_ref(excludes, fn, data);
+}
+
+
 static int is_our_ref(struct object *o, enum allow_uor allow_uor)
 {
-       int allow_hidden_ref = (allow_uor &
-                               (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
-       return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
+       return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
 }
 
 /*
@@ -855,7 +880,7 @@ static void deepen(struct upload_pack_data *data, int depth)
                 * marked with OUR_REF.
                 */
                head_ref_namespaced(check_ref, data);
-               for_each_namespaced_ref(NULL, check_ref, data);
+               for_each_namespaced_ref_1(check_ref, data);
 
                get_reachable_list(data, &reachable_shallows);
                result = get_shallow_commits(&reachable_shallows,
@@ -1386,7 +1411,7 @@ void upload_pack(const int advertise_refs, const int stateless_rpc,
                if (advertise_refs)
                        data.no_done = 1;
                head_ref_namespaced(send_ref, &data);
-               for_each_namespaced_ref(NULL, send_ref, &data);
+               for_each_namespaced_ref_1(send_ref, &data);
                if (!data.sent_capabilities) {
                        const char *refname = "capabilities^{}";
                        write_v0_ref(&data, refname, refname, null_oid());
@@ -1400,7 +1425,7 @@ void upload_pack(const int advertise_refs, const int stateless_rpc,
                packet_flush(1);
        } else {
                head_ref_namespaced(check_ref, &data);
-               for_each_namespaced_ref(NULL, check_ref, &data);
+               for_each_namespaced_ref_1(check_ref, &data);
        }
 
        if (!advertise_refs) {