From 6373cb598e1a4e0340583ad75d5abba01ff79774 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Wed, 16 May 2018 16:48:21 -0700 Subject: refspec: consolidate ref-prefix generation logic When using protocol v2 a client constructs a list of ref-prefixes which are sent across the wire so that the server can do server-side filtering of the ref-advertisement. The logic that does this exists for both fetch and push (even though no push support for v2 currently exists yet) and is roughly the same so lets consolidate this logic and make it general enough that it can be used for both the push and fetch cases. Signed-off-by: Brandon Williams Signed-off-by: Junio C Hamano --- refspec.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'refspec.c') diff --git a/refspec.c b/refspec.c index 97e76e8b1d..c59a4ccf1e 100644 --- a/refspec.c +++ b/refspec.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "argv-array.h" #include "refs.h" #include "refspec.h" @@ -192,3 +193,31 @@ int valid_fetch_refspec(const char *fetch_refspec_str) refspec_item_clear(&refspec); return ret; } + +void refspec_ref_prefixes(const struct refspec *rs, + struct argv_array *ref_prefixes) +{ + int i; + for (i = 0; i < rs->nr; i++) { + const struct refspec_item *item = &rs->items[i]; + const char *prefix = NULL; + + if (rs->fetch == REFSPEC_FETCH) + prefix = item->src; + else if (item->dst) + prefix = item->dst; + else if (item->src && !item->exact_sha1) + prefix = item->src; + + if (prefix) { + if (item->pattern) { + const char *glob = strchr(prefix, '*'); + argv_array_pushf(ref_prefixes, "%.*s", + (int)(glob - prefix), + prefix); + } else { + expand_ref_prefix(ref_prefixes, prefix); + } + } + } +} -- cgit v1.2.3