From fe299ec5ae7b419990bbc3efd4e6bfa3f0773b45 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 30 Mar 2020 10:03:46 -0400 Subject: oid_array: rename source file from sha1-array We renamed the actual data structure in 910650d2f8 (Rename sha1_array to oid_array, 2017-03-31), but the file is still called sha1-array. Besides being slightly confusing, it makes it more annoying to grep for leftover occurrences of "sha1" in various files, because the header is included in so many places. Let's complete the transition by renaming the source and header files (and fixing up a few comment references). I kept the "-" in the name, as that seems to be our style; cf. fc1395f4a4 (sha1_file.c: rename to use dash in file name, 2018-04-10). We also have oidmap.h and oidset.h without any punctuation, but those are "struct oidmap" and "struct oidset" in the code. We _could_ make this "oidarray" to match, but somehow it looks uglier to me because of the length of "array" (plus it would be a very invasive patch for little gain). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/Makefile b/Makefile index 9804a07..30fdfb8 100644 --- a/Makefile +++ b/Makefile @@ -929,6 +929,7 @@ LIB_OBJS += notes-utils.o LIB_OBJS += object.o LIB_OBJS += oidmap.o LIB_OBJS += oidset.o +LIB_OBJS += oid-array.o LIB_OBJS += packfile.o LIB_OBJS += pack-bitmap.o LIB_OBJS += pack-bitmap-write.o @@ -978,7 +979,6 @@ LIB_OBJS += sequencer.o LIB_OBJS += serve.o LIB_OBJS += server-info.o LIB_OBJS += setup.o -LIB_OBJS += sha1-array.o LIB_OBJS += sha1-lookup.o LIB_OBJS += sha1-file.o LIB_OBJS += sha1-name.o diff --git a/bisect.c b/bisect.c index 9154f81..64b579b 100644 --- a/bisect.c +++ b/bisect.c @@ -10,7 +10,7 @@ #include "run-command.h" #include "log-tree.h" #include "bisect.h" -#include "sha1-array.h" +#include "oid-array.h" #include "argv-array.h" #include "commit-slab.h" #include "commit-reach.h" diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 272f9fc..9406763 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -12,7 +12,7 @@ #include "userdiff.h" #include "streaming.h" #include "tree-walk.h" -#include "sha1-array.h" +#include "oid-array.h" #include "packfile.h" #include "object-store.h" #include "promisor-remote.h" diff --git a/builtin/diff.c b/builtin/diff.c index 42ac803..8537b17 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -17,7 +17,7 @@ #include "log-tree.h" #include "builtin.h" #include "submodule.h" -#include "sha1-array.h" +#include "oid-array.h" #define DIFF_NO_INDEX_EXPLICIT 1 #define DIFF_NO_INDEX_IMPLICIT 2 diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c index dc1485c..4771100 100644 --- a/builtin/fetch-pack.c +++ b/builtin/fetch-pack.c @@ -3,7 +3,7 @@ #include "fetch-pack.h" #include "remote.h" #include "connect.h" -#include "sha1-array.h" +#include "oid-array.h" #include "protocol.h" static const char fetch_pack_usage[] = diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index 02aa6ee..2e2e33e 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -26,7 +26,7 @@ #include "pack-bitmap.h" #include "delta-islands.h" #include "reachable.h" -#include "sha1-array.h" +#include "oid-array.h" #include "argv-array.h" #include "list.h" #include "packfile.h" diff --git a/builtin/pull.c b/builtin/pull.c index 3e624d1..3d5edef 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -12,7 +12,7 @@ #include "parse-options.h" #include "exec-cmd.h" #include "run-command.h" -#include "sha1-array.h" +#include "oid-array.h" #include "remote.h" #include "dir.h" #include "rebase.h" diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 2cc18bb..d46147f 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -13,7 +13,7 @@ #include "remote.h" #include "connect.h" #include "string-list.h" -#include "sha1-array.h" +#include "oid-array.h" #include "connected.h" #include "argv-array.h" #include "version.h" diff --git a/builtin/send-pack.c b/builtin/send-pack.c index 098ebf2..f2c5a34 100644 --- a/builtin/send-pack.c +++ b/builtin/send-pack.c @@ -11,7 +11,7 @@ #include "quote.h" #include "transport.h" #include "version.h" -#include "sha1-array.h" +#include "oid-array.h" #include "gpg-interface.h" #include "gettext.h" #include "protocol.h" diff --git a/builtin/tag.c b/builtin/tag.c index e0a4c25..7f6b43b 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -17,7 +17,7 @@ #include "diff.h" #include "revision.h" #include "gpg-interface.h" -#include "sha1-array.h" +#include "oid-array.h" #include "column.h" #include "ref-filter.h" diff --git a/cache.h b/cache.h index 37c899b..cd77f38 100644 --- a/cache.h +++ b/cache.h @@ -14,7 +14,7 @@ #include "pack-revindex.h" #include "hash.h" #include "path.h" -#include "sha1-array.h" +#include "oid-array.h" #include "repository.h" #include "mem-pool.h" diff --git a/combine-diff.c b/combine-diff.c index d5c4d83..002e0e5 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -10,7 +10,7 @@ #include "log-tree.h" #include "refs.h" #include "userdiff.h" -#include "sha1-array.h" +#include "oid-array.h" #include "revision.h" static int compare_paths(const struct combine_diff_path *one, diff --git a/connect.c b/connect.c index b6451ab..23013c6 100644 --- a/connect.c +++ b/connect.c @@ -9,7 +9,7 @@ #include "connect.h" #include "url.h" #include "string-list.h" -#include "sha1-array.h" +#include "oid-array.h" #include "transport.h" #include "strbuf.h" #include "version.h" diff --git a/delta-islands.c b/delta-islands.c index 09dbd3c..aa98b2e 100644 --- a/delta-islands.c +++ b/delta-islands.c @@ -17,7 +17,7 @@ #include "pack-bitmap.h" #include "pack-objects.h" #include "delta-islands.h" -#include "sha1-array.h" +#include "oid-array.h" #include "config.h" KHASH_INIT(str, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) diff --git a/fetch-pack.c b/fetch-pack.c index 1734a57..0b07b3e 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -15,7 +15,7 @@ #include "connect.h" #include "transport.h" #include "version.h" -#include "sha1-array.h" +#include "oid-array.h" #include "oidset.h" #include "packfile.h" #include "object-store.h" diff --git a/object-store.h b/object-store.h index be72fee..d1e490f 100644 --- a/object-store.h +++ b/object-store.h @@ -4,7 +4,7 @@ #include "cache.h" #include "oidmap.h" #include "list.h" -#include "sha1-array.h" +#include "oid-array.h" #include "strbuf.h" #include "thread-utils.h" diff --git a/oid-array.c b/oid-array.c new file mode 100644 index 0000000..8657a5c --- /dev/null +++ b/oid-array.c @@ -0,0 +1,96 @@ +#include "cache.h" +#include "oid-array.h" +#include "sha1-lookup.h" + +void oid_array_append(struct oid_array *array, const struct object_id *oid) +{ + ALLOC_GROW(array->oid, array->nr + 1, array->alloc); + oidcpy(&array->oid[array->nr++], oid); + array->sorted = 0; +} + +static int void_hashcmp(const void *a, const void *b) +{ + return oidcmp(a, b); +} + +static void oid_array_sort(struct oid_array *array) +{ + QSORT(array->oid, array->nr, void_hashcmp); + array->sorted = 1; +} + +static const unsigned char *sha1_access(size_t index, void *table) +{ + struct object_id *array = table; + return array[index].hash; +} + +int oid_array_lookup(struct oid_array *array, const struct object_id *oid) +{ + if (!array->sorted) + oid_array_sort(array); + return sha1_pos(oid->hash, array->oid, array->nr, sha1_access); +} + +void oid_array_clear(struct oid_array *array) +{ + FREE_AND_NULL(array->oid); + array->nr = 0; + array->alloc = 0; + array->sorted = 0; +} + + +int oid_array_for_each(struct oid_array *array, + for_each_oid_fn fn, + void *data) +{ + size_t i; + + /* No oid_array_sort() here! See oid-array.h */ + + for (i = 0; i < array->nr; i++) { + int ret = fn(array->oid + i, data); + if (ret) + return ret; + } + return 0; +} + +int oid_array_for_each_unique(struct oid_array *array, + for_each_oid_fn fn, + void *data) +{ + size_t i; + + if (!array->sorted) + oid_array_sort(array); + + for (i = 0; i < array->nr; i++) { + int ret; + if (i > 0 && oideq(array->oid + i, array->oid + i - 1)) + continue; + ret = fn(array->oid + i, data); + if (ret) + return ret; + } + return 0; +} + +void oid_array_filter(struct oid_array *array, + for_each_oid_fn want, + void *cb_data) +{ + size_t nr = array->nr, src, dst; + struct object_id *oids = array->oid; + + for (src = dst = 0; src < nr; src++) { + if (want(&oids[src], cb_data)) { + if (src != dst) + oidcpy(&oids[dst], &oids[src]); + dst++; + } + } + array->nr = dst; +} diff --git a/oid-array.h b/oid-array.h new file mode 100644 index 0000000..f28d322 --- /dev/null +++ b/oid-array.h @@ -0,0 +1,109 @@ +#ifndef SHA1_ARRAY_H +#define SHA1_ARRAY_H + +/** + * The API provides storage and manipulation of sets of object identifiers. + * The emphasis is on storage and processing efficiency, making them suitable + * for large lists. Note that the ordering of items is not preserved over some + * operations. + * + * Examples + * -------- + * ----------------------------------------- + * int print_callback(const struct object_id *oid, + * void *data) + * { + * printf("%s\n", oid_to_hex(oid)); + * return 0; // always continue + * } + * + * void some_func(void) + * { + * struct oid_array hashes = OID_ARRAY_INIT; + * struct object_id oid; + * + * // Read objects into our set + * while (read_object_from_stdin(oid.hash)) + * oid_array_append(&hashes, &oid); + * + * // Check if some objects are in our set + * while (read_object_from_stdin(oid.hash)) { + * if (oid_array_lookup(&hashes, &oid) >= 0) + * printf("it's in there!\n"); + * + * // Print the unique set of objects. We could also have + * // avoided adding duplicate objects in the first place, + * // but we would end up re-sorting the array repeatedly. + * // Instead, this will sort once and then skip duplicates + * // in linear time. + * + * oid_array_for_each_unique(&hashes, print_callback, NULL); + * } + */ + +/** + * A single array of object IDs. This should be initialized by assignment from + * `OID_ARRAY_INIT`. The `oid` member contains the actual data. The `nr` member + * contains the number of items in the set. The `alloc` and `sorted` members + * are used internally, and should not be needed by API callers. + */ +struct oid_array { + struct object_id *oid; + size_t nr; + size_t alloc; + int sorted; +}; + +#define OID_ARRAY_INIT { NULL, 0, 0, 0 } + +/** + * Add an item to the set. The object ID will be placed at the end of the array + * (but note that some operations below may lose this ordering). + */ +void oid_array_append(struct oid_array *array, const struct object_id *oid); + +/** + * Perform a binary search of the array for a specific object ID. If found, + * returns the offset (in number of elements) of the object ID. If not found, + * returns a negative integer. If the array is not sorted, this function has + * the side effect of sorting it. + */ +int oid_array_lookup(struct oid_array *array, const struct object_id *oid); + +/** + * Free all memory associated with the array and return it to the initial, + * empty state. + */ +void oid_array_clear(struct oid_array *array); + +typedef int (*for_each_oid_fn)(const struct object_id *oid, + void *data); +/** + * Iterate over each element of the list, executing the callback function for + * each one. Does not sort the list, so any custom hash order is retained. + * If the callback returns a non-zero value, the iteration ends immediately + * and the callback's return is propagated; otherwise, 0 is returned. + */ +int oid_array_for_each(struct oid_array *array, + for_each_oid_fn fn, + void *data); + +/** + * Iterate over each unique element of the list in sorted order, but otherwise + * behave like `oid_array_for_each`. If the array is not sorted, this function + * has the side effect of sorting it. + */ +int oid_array_for_each_unique(struct oid_array *array, + for_each_oid_fn fn, + void *data); + +/** + * Apply the callback function `want` to each entry in the array, retaining + * only the entries for which the function returns true. Preserve the order + * of the entries that are retained. + */ +void oid_array_filter(struct oid_array *array, + for_each_oid_fn want, + void *cbdata); + +#endif /* SHA1_ARRAY_H */ diff --git a/parse-options-cb.c b/parse-options-cb.c index a28b55b..86cd393 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -5,7 +5,7 @@ #include "color.h" #include "string-list.h" #include "argv-array.h" -#include "sha1-array.h" +#include "oid-array.h" /*----- some often used options -----*/ diff --git a/ref-filter.h b/ref-filter.h index f1dcff4..64330e9 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -1,7 +1,7 @@ #ifndef REF_FILTER_H #define REF_FILTER_H -#include "sha1-array.h" +#include "oid-array.h" #include "refs.h" #include "commit.h" #include "parse-options.h" diff --git a/remote-curl.c b/remote-curl.c index e4cd321..1c9aa3d 100644 --- a/remote-curl.c +++ b/remote-curl.c @@ -12,7 +12,7 @@ #include "sideband.h" #include "argv-array.h" #include "credential.h" -#include "sha1-array.h" +#include "oid-array.h" #include "send-pack.h" #include "protocol.h" #include "quote.h" diff --git a/send-pack.c b/send-pack.c index 0407841..da4741c 100644 --- a/send-pack.c +++ b/send-pack.c @@ -12,7 +12,7 @@ #include "quote.h" #include "transport.h" #include "version.h" -#include "sha1-array.h" +#include "oid-array.h" #include "gpg-interface.h" #include "cache.h" diff --git a/sha1-array.c b/sha1-array.c deleted file mode 100644 index bada0c4..0000000 --- a/sha1-array.c +++ /dev/null @@ -1,96 +0,0 @@ -#include "cache.h" -#include "sha1-array.h" -#include "sha1-lookup.h" - -void oid_array_append(struct oid_array *array, const struct object_id *oid) -{ - ALLOC_GROW(array->oid, array->nr + 1, array->alloc); - oidcpy(&array->oid[array->nr++], oid); - array->sorted = 0; -} - -static int void_hashcmp(const void *a, const void *b) -{ - return oidcmp(a, b); -} - -static void oid_array_sort(struct oid_array *array) -{ - QSORT(array->oid, array->nr, void_hashcmp); - array->sorted = 1; -} - -static const unsigned char *sha1_access(size_t index, void *table) -{ - struct object_id *array = table; - return array[index].hash; -} - -int oid_array_lookup(struct oid_array *array, const struct object_id *oid) -{ - if (!array->sorted) - oid_array_sort(array); - return sha1_pos(oid->hash, array->oid, array->nr, sha1_access); -} - -void oid_array_clear(struct oid_array *array) -{ - FREE_AND_NULL(array->oid); - array->nr = 0; - array->alloc = 0; - array->sorted = 0; -} - - -int oid_array_for_each(struct oid_array *array, - for_each_oid_fn fn, - void *data) -{ - size_t i; - - /* No oid_array_sort() here! See sha1-array.h */ - - for (i = 0; i < array->nr; i++) { - int ret = fn(array->oid + i, data); - if (ret) - return ret; - } - return 0; -} - -int oid_array_for_each_unique(struct oid_array *array, - for_each_oid_fn fn, - void *data) -{ - size_t i; - - if (!array->sorted) - oid_array_sort(array); - - for (i = 0; i < array->nr; i++) { - int ret; - if (i > 0 && oideq(array->oid + i, array->oid + i - 1)) - continue; - ret = fn(array->oid + i, data); - if (ret) - return ret; - } - return 0; -} - -void oid_array_filter(struct oid_array *array, - for_each_oid_fn want, - void *cb_data) -{ - size_t nr = array->nr, src, dst; - struct object_id *oids = array->oid; - - for (src = dst = 0; src < nr; src++) { - if (want(&oids[src], cb_data)) { - if (src != dst) - oidcpy(&oids[dst], &oids[src]); - dst++; - } - } - array->nr = dst; -} diff --git a/sha1-array.h b/sha1-array.h deleted file mode 100644 index c5e4b93..0000000 --- a/sha1-array.h +++ /dev/null @@ -1,109 +0,0 @@ -#ifndef SHA1_ARRAY_H -#define SHA1_ARRAY_H - -/** - * The API provides storage and manipulation of sets of object identifiers. - * The emphasis is on storage and processing efficiency, making them suitable - * for large lists. Note that the ordering of items is not preserved over some - * operations. - * - * Examples - * -------- - * ----------------------------------------- - * int print_callback(const struct object_id *oid, - * void *data) - * { - * printf("%s\n", oid_to_hex(oid)); - * return 0; // always continue - * } - * - * void some_func(void) - * { - * struct sha1_array hashes = OID_ARRAY_INIT; - * struct object_id oid; - * - * // Read objects into our set - * while (read_object_from_stdin(oid.hash)) - * oid_array_append(&hashes, &oid); - * - * // Check if some objects are in our set - * while (read_object_from_stdin(oid.hash)) { - * if (oid_array_lookup(&hashes, &oid) >= 0) - * printf("it's in there!\n"); - * - * // Print the unique set of objects. We could also have - * // avoided adding duplicate objects in the first place, - * // but we would end up re-sorting the array repeatedly. - * // Instead, this will sort once and then skip duplicates - * // in linear time. - * - * oid_array_for_each_unique(&hashes, print_callback, NULL); - * } - */ - -/** - * A single array of object IDs. This should be initialized by assignment from - * `OID_ARRAY_INIT`. The `oid` member contains the actual data. The `nr` member - * contains the number of items in the set. The `alloc` and `sorted` members - * are used internally, and should not be needed by API callers. - */ -struct oid_array { - struct object_id *oid; - size_t nr; - size_t alloc; - int sorted; -}; - -#define OID_ARRAY_INIT { NULL, 0, 0, 0 } - -/** - * Add an item to the set. The object ID will be placed at the end of the array - * (but note that some operations below may lose this ordering). - */ -void oid_array_append(struct oid_array *array, const struct object_id *oid); - -/** - * Perform a binary search of the array for a specific object ID. If found, - * returns the offset (in number of elements) of the object ID. If not found, - * returns a negative integer. If the array is not sorted, this function has - * the side effect of sorting it. - */ -int oid_array_lookup(struct oid_array *array, const struct object_id *oid); - -/** - * Free all memory associated with the array and return it to the initial, - * empty state. - */ -void oid_array_clear(struct oid_array *array); - -typedef int (*for_each_oid_fn)(const struct object_id *oid, - void *data); -/** - * Iterate over each element of the list, executing the callback function for - * each one. Does not sort the list, so any custom hash order is retained. - * If the callback returns a non-zero value, the iteration ends immediately - * and the callback's return is propagated; otherwise, 0 is returned. - */ -int oid_array_for_each(struct oid_array *array, - for_each_oid_fn fn, - void *data); - -/** - * Iterate over each unique element of the list in sorted order, but otherwise - * behave like `oid_array_for_each`. If the array is not sorted, this function - * has the side effect of sorting it. - */ -int oid_array_for_each_unique(struct oid_array *array, - for_each_oid_fn fn, - void *data); - -/** - * Apply the callback function `want` to each entry in the array, retaining - * only the entries for which the function returns true. Preserve the order - * of the entries that are retained. - */ -void oid_array_filter(struct oid_array *array, - for_each_oid_fn want, - void *cbdata); - -#endif /* SHA1_ARRAY_H */ diff --git a/sha1-name.c b/sha1-name.c index 5bb006e..6561cd9 100644 --- a/sha1-name.c +++ b/sha1-name.c @@ -8,7 +8,7 @@ #include "refs.h" #include "remote.h" #include "dir.h" -#include "sha1-array.h" +#include "oid-array.h" #include "packfile.h" #include "object-store.h" #include "repository.h" diff --git a/shallow.c b/shallow.c index 7fd04af..14f7fa6 100644 --- a/shallow.c +++ b/shallow.c @@ -8,7 +8,7 @@ #include "pkt-line.h" #include "remote.h" #include "refs.h" -#include "sha1-array.h" +#include "oid-array.h" #include "diff.h" #include "revision.h" #include "commit-slab.h" diff --git a/submodule.c b/submodule.c index 31f391d..8b5559c 100644 --- a/submodule.c +++ b/submodule.c @@ -12,7 +12,7 @@ #include "diffcore.h" #include "refs.h" #include "string-list.h" -#include "sha1-array.h" +#include "oid-array.h" #include "argv-array.h" #include "blob.h" #include "thread-utils.h" diff --git a/t/helper/test-sha1-array.c b/t/helper/test-sha1-array.c index ad5e69f..6f7d3b9 100644 --- a/t/helper/test-sha1-array.c +++ b/t/helper/test-sha1-array.c @@ -1,6 +1,6 @@ #include "test-tool.h" #include "cache.h" -#include "sha1-array.h" +#include "oid-array.h" static int print_oid(const struct object_id *oid, void *data) { diff --git a/transport.c b/transport.c index 1fdc7da..471c5bd 100644 --- a/transport.c +++ b/transport.c @@ -16,7 +16,7 @@ #include "url.h" #include "submodule.h" #include "string-list.h" -#include "sha1-array.h" +#include "oid-array.h" #include "sigchain.h" #include "transport-internal.h" #include "protocol.h" -- cgit v0.10.2-6-g49f6