From 53df97a29d78070f3dfaf3e4d9a5ae61f33d7906 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 6 Apr 2018 14:58:32 -0400 Subject: [PATCH 1/3] ref-filter: use "struct object_id" consistently Internally we store a "struct object_id", and all of our callers have one to pass us. But we insist that they peel it to its bare-sha1 hash, which we then hashcpy() into place. Let's pass it around as an object_id, which future-proofs us for a post-sha1 world. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/tag.c | 2 +- builtin/verify-tag.c | 2 +- ref-filter.c | 10 +++++----- ref-filter.h | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/builtin/tag.c b/builtin/tag.c index da186691ed..42278f5167 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -117,7 +117,7 @@ static int verify_tag(const char *name, const char *ref, return -1; if (format->format) - pretty_print_ref(name, oid->hash, format); + pretty_print_ref(name, oid, format); return 0; } diff --git a/builtin/verify-tag.c b/builtin/verify-tag.c index ad7b79fa5c..6fa04b751a 100644 --- a/builtin/verify-tag.c +++ b/builtin/verify-tag.c @@ -72,7 +72,7 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix) } if (format.format) - pretty_print_ref(name, oid.hash, &format); + pretty_print_ref(name, &oid, &format); } return had_error; } diff --git a/ref-filter.c b/ref-filter.c index 45fc56216a..ade97a8486 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1826,12 +1826,12 @@ static const struct object_id *match_points_at(struct oid_array *points_at, /* Allocate space for a new ref_array_item and copy the objectname and flag to it */ static struct ref_array_item *new_ref_array_item(const char *refname, - const unsigned char *objectname, + const struct object_id *oid, int flag) { struct ref_array_item *ref; FLEX_ALLOC_STR(ref, refname, refname); - hashcpy(ref->objectname.hash, objectname); + oidcpy(&ref->objectname, oid); ref->flag = flag; return ref; @@ -1927,7 +1927,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid, * to do its job and the resulting list may yet to be pruned * by maxcount logic. */ - ref = new_ref_array_item(refname, oid->hash, flag); + ref = new_ref_array_item(refname, oid, flag); ref->commit = commit; REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1); @@ -2165,11 +2165,11 @@ void show_ref_array_item(struct ref_array_item *info, putchar('\n'); } -void pretty_print_ref(const char *name, const unsigned char *sha1, +void pretty_print_ref(const char *name, const struct object_id *oid, const struct ref_format *format) { struct ref_array_item *ref_item; - ref_item = new_ref_array_item(name, sha1, 0); + ref_item = new_ref_array_item(name, oid, 0); ref_item->kind = ref_kind_from_refname(name); show_ref_array_item(ref_item, format); free_array_item(ref_item); diff --git a/ref-filter.h b/ref-filter.h index 0d98342b34..68268f9ebc 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -132,7 +132,7 @@ void setup_ref_filter_porcelain_msg(void); * Print a single ref, outside of any ref-filter. Note that the * name must be a fully qualified refname. */ -void pretty_print_ref(const char *name, const unsigned char *sha1, +void pretty_print_ref(const char *name, const struct object_id *oid, const struct ref_format *format); #endif /* REF_FILTER_H */ From 0ffaa00f453978bffc4ff0f8a45ea0a52549e7cd Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 6 Apr 2018 14:59:26 -0400 Subject: [PATCH 2/3] ref-filter: make ref_array_item allocation more consistent We have a helper function to allocate ref_array_item structs, but it only takes a subset of the possible fields in the struct as initializers. We could have it accept an argument for _every_ field, but that becomes a pain for the fields which some callers don't want to set initially. Instead, let's be explicit that it takes only the minimum required to create the ref, and that callers should then fill in the rest themselves. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- ref-filter.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index ade97a8486..c1c3cc9480 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1824,15 +1824,18 @@ static const struct object_id *match_points_at(struct oid_array *points_at, return NULL; } -/* Allocate space for a new ref_array_item and copy the objectname and flag to it */ +/* + * Allocate space for a new ref_array_item and copy the name and oid to it. + * + * Callers can then fill in other struct members at their leisure. + */ static struct ref_array_item *new_ref_array_item(const char *refname, - const struct object_id *oid, - int flag) + const struct object_id *oid) { struct ref_array_item *ref; + FLEX_ALLOC_STR(ref, refname, refname); oidcpy(&ref->objectname, oid); - ref->flag = flag; return ref; } @@ -1927,12 +1930,13 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid, * to do its job and the resulting list may yet to be pruned * by maxcount logic. */ - ref = new_ref_array_item(refname, oid, flag); + ref = new_ref_array_item(refname, oid); ref->commit = commit; + ref->flag = flag; + ref->kind = kind; REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1); ref_cbdata->array->items[ref_cbdata->array->nr++] = ref; - ref->kind = kind; return 0; } @@ -2169,7 +2173,7 @@ void pretty_print_ref(const char *name, const struct object_id *oid, const struct ref_format *format) { struct ref_array_item *ref_item; - ref_item = new_ref_array_item(name, oid, 0); + ref_item = new_ref_array_item(name, oid); ref_item->kind = ref_kind_from_refname(name); show_ref_array_item(ref_item, format); free_array_item(ref_item); From 427cbc9dbfeb7c96bb1d5d9ace722353e2a5438e Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 6 Apr 2018 14:59:45 -0400 Subject: [PATCH 3/3] ref-filter: factor ref_array pushing into its own function In preparation for callers constructing their own ref_array structs, let's move our own internal push operation into its own function. While we're at it, we can replace REALLOC_ARRAY() with ALLOC_GROW(), which should give the growth operation amortized linear complexity (as opposed to growing by one, which is potentially quadratic, though in-place realloc growth often makes this faster in practice). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- ref-filter.c | 16 +++++++++++++--- ref-filter.h | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index c1c3cc9480..6e9328b274 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1840,6 +1840,18 @@ static struct ref_array_item *new_ref_array_item(const char *refname, return ref; } +struct ref_array_item *ref_array_push(struct ref_array *array, + const char *refname, + const struct object_id *oid) +{ + struct ref_array_item *ref = new_ref_array_item(refname, oid); + + ALLOC_GROW(array->items, array->nr + 1, array->alloc); + array->items[array->nr++] = ref; + + return ref; +} + static int ref_kind_from_refname(const char *refname) { unsigned int i; @@ -1930,13 +1942,11 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid, * to do its job and the resulting list may yet to be pruned * by maxcount logic. */ - ref = new_ref_array_item(refname, oid); + ref = ref_array_push(ref_cbdata->array, refname, oid); ref->commit = commit; ref->flag = flag; ref->kind = kind; - REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1); - ref_cbdata->array->items[ref_cbdata->array->nr++] = ref; return 0; } diff --git a/ref-filter.h b/ref-filter.h index 68268f9ebc..76cf87cb6c 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -135,4 +135,12 @@ void setup_ref_filter_porcelain_msg(void); void pretty_print_ref(const char *name, const struct object_id *oid, const struct ref_format *format); +/* + * Push a single ref onto the array; this can be used to construct your own + * ref_array without using filter_refs(). + */ +struct ref_array_item *ref_array_push(struct ref_array *array, + const char *refname, + const struct object_id *oid); + #endif /* REF_FILTER_H */