]> git.feebdaed.xyz Git - 0xmirror/git.git/commitdiff
oidset: introduce `oidset_equal()`
authorPatrick Steinhardt <ps@pks.im>
Wed, 19 Nov 2025 07:50:54 +0000 (08:50 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 Nov 2025 20:15:59 +0000 (12:15 -0800)
Introduce a new function that allows the caller to verify whether two
oidsets contain the exact same object IDs.

Note that this change requires us to change `oidset_iter_init()` to
accept a `const struct oidset`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
oidset.c
oidset.h

index 8d36aef8dca4fca8aa979692a536687a21f2b90d..c8ff0b385c58acf73c6d1c8bb7f3d658abe16a22 100644 (file)
--- a/oidset.c
+++ b/oidset.c
@@ -16,6 +16,22 @@ int oidset_contains(const struct oidset *set, const struct object_id *oid)
        return pos != kh_end(&set->set);
 }
 
+bool oidset_equal(const struct oidset *a, const struct oidset *b)
+{
+       struct oidset_iter iter;
+       struct object_id *a_oid;
+
+       if (oidset_size(a) != oidset_size(b))
+               return false;
+
+       oidset_iter_init(a, &iter);
+       while ((a_oid = oidset_iter_next(&iter)))
+               if (!oidset_contains(b, a_oid))
+                       return false;
+
+       return true;
+}
+
 int oidset_insert(struct oidset *set, const struct object_id *oid)
 {
        int added;
index 0106b6f2787f0e4a5feff560554f5bd25748313b..e0f1a6ff4ff20328dff91fdfddafa44990a88f02 100644 (file)
--- a/oidset.h
+++ b/oidset.h
@@ -38,6 +38,11 @@ void oidset_init(struct oidset *set, size_t initial_size);
  */
 int oidset_contains(const struct oidset *set, const struct object_id *oid);
 
+/**
+ * Returns true iff `a` and `b` contain the exact same OIDs.
+ */
+bool oidset_equal(const struct oidset *a, const struct oidset *b);
+
 /**
  * Insert the oid into the set; a copy is made, so "oid" does not need
  * to persist after this function is called.
@@ -94,11 +99,11 @@ void oidset_parse_file_carefully(struct oidset *set, const char *path,
                                 oidset_parse_tweak_fn fn, void *cbdata);
 
 struct oidset_iter {
-       kh_oid_set_t *set;
+       const kh_oid_set_t *set;
        khiter_t iter;
 };
 
-static inline void oidset_iter_init(struct oidset *set,
+static inline void oidset_iter_init(const struct oidset *set,
                                    struct oidset_iter *iter)
 {
        iter->set = &set->set;