Skip to content

Commit 4c00e12

Browse files
committed
Merge branch 'ms/refspec-cleanup' into jch
* ms/refspec-cleanup: refspec: relocate apply_refspecs and related funtions refspec: relocate query related functions refspec: relocate omit_name_by_refspec and related functions
2 parents 15d2909 + 3316f95 commit 4c00e12

File tree

4 files changed

+244
-216
lines changed

4 files changed

+244
-216
lines changed

refspec.c

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include "gettext.h"
66
#include "hash.h"
77
#include "hex.h"
8+
#include "string-list.h"
89
#include "strvec.h"
910
#include "refs.h"
1011
#include "refspec.h"
12+
#include "remote.h"
1113
#include "strbuf.h"
1214

1315
/*
@@ -276,3 +278,204 @@ void refspec_ref_prefixes(const struct refspec *rs,
276278
}
277279
}
278280
}
281+
282+
int match_name_with_pattern(const char *key, const char *name,
283+
const char *value, char **result)
284+
{
285+
const char *kstar = strchr(key, '*');
286+
size_t klen;
287+
size_t ksuffixlen;
288+
size_t namelen;
289+
int ret;
290+
if (!kstar)
291+
die(_("key '%s' of pattern had no '*'"), key);
292+
klen = kstar - key;
293+
ksuffixlen = strlen(kstar + 1);
294+
namelen = strlen(name);
295+
ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
296+
!memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
297+
if (ret && value) {
298+
struct strbuf sb = STRBUF_INIT;
299+
const char *vstar = strchr(value, '*');
300+
if (!vstar)
301+
die(_("value '%s' of pattern has no '*'"), value);
302+
strbuf_add(&sb, value, vstar - value);
303+
strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
304+
strbuf_addstr(&sb, vstar + 1);
305+
*result = strbuf_detach(&sb, NULL);
306+
}
307+
return ret;
308+
}
309+
310+
static int refspec_match(const struct refspec_item *refspec,
311+
const char *name)
312+
{
313+
if (refspec->pattern)
314+
return match_name_with_pattern(refspec->src, name, NULL, NULL);
315+
316+
return !strcmp(refspec->src, name);
317+
}
318+
319+
int omit_name_by_refspec(const char *name, struct refspec *rs)
320+
{
321+
int i;
322+
323+
for (i = 0; i < rs->nr; i++) {
324+
if (rs->items[i].negative && refspec_match(&rs->items[i], name))
325+
return 1;
326+
}
327+
return 0;
328+
}
329+
330+
static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
331+
{
332+
int i, matched_negative = 0;
333+
int find_src = !query->src;
334+
struct string_list reversed = STRING_LIST_INIT_DUP;
335+
const char *needle = find_src ? query->dst : query->src;
336+
337+
/*
338+
* Check whether the queried ref matches any negative refpsec. If so,
339+
* then we should ultimately treat this as not matching the query at
340+
* all.
341+
*
342+
* Note that negative refspecs always match the source, but the query
343+
* item uses the destination. To handle this, we apply pattern
344+
* refspecs in reverse to figure out if the query source matches any
345+
* of the negative refspecs.
346+
*
347+
* The first loop finds and expands all positive refspecs
348+
* matched by the queried ref.
349+
*
350+
* The second loop checks if any of the results of the first loop
351+
* match any negative refspec.
352+
*/
353+
for (i = 0; i < rs->nr; i++) {
354+
struct refspec_item *refspec = &rs->items[i];
355+
char *expn_name;
356+
357+
if (refspec->negative)
358+
continue;
359+
360+
/* Note the reversal of src and dst */
361+
if (refspec->pattern) {
362+
const char *key = refspec->dst ? refspec->dst : refspec->src;
363+
const char *value = refspec->src;
364+
365+
if (match_name_with_pattern(key, needle, value, &expn_name))
366+
string_list_append_nodup(&reversed, expn_name);
367+
} else if (refspec->matching) {
368+
/* For the special matching refspec, any query should match */
369+
string_list_append(&reversed, needle);
370+
} else if (!refspec->src) {
371+
BUG("refspec->src should not be null here");
372+
} else if (!strcmp(needle, refspec->src)) {
373+
string_list_append(&reversed, refspec->src);
374+
}
375+
}
376+
377+
for (i = 0; !matched_negative && i < reversed.nr; i++) {
378+
if (omit_name_by_refspec(reversed.items[i].string, rs))
379+
matched_negative = 1;
380+
}
381+
382+
string_list_clear(&reversed, 0);
383+
384+
return matched_negative;
385+
}
386+
387+
void query_refspecs_multiple(struct refspec *rs,
388+
struct refspec_item *query,
389+
struct string_list *results)
390+
{
391+
int i;
392+
int find_src = !query->src;
393+
394+
if (find_src && !query->dst)
395+
BUG("query_refspecs_multiple: need either src or dst");
396+
397+
if (query_matches_negative_refspec(rs, query))
398+
return;
399+
400+
for (i = 0; i < rs->nr; i++) {
401+
struct refspec_item *refspec = &rs->items[i];
402+
const char *key = find_src ? refspec->dst : refspec->src;
403+
const char *value = find_src ? refspec->src : refspec->dst;
404+
const char *needle = find_src ? query->dst : query->src;
405+
char **result = find_src ? &query->src : &query->dst;
406+
407+
if (!refspec->dst || refspec->negative)
408+
continue;
409+
if (refspec->pattern) {
410+
if (match_name_with_pattern(key, needle, value, result))
411+
string_list_append_nodup(results, *result);
412+
} else if (!strcmp(needle, key)) {
413+
string_list_append(results, value);
414+
}
415+
}
416+
}
417+
418+
int query_refspecs(struct refspec *rs, struct refspec_item *query)
419+
{
420+
int i;
421+
int find_src = !query->src;
422+
const char *needle = find_src ? query->dst : query->src;
423+
char **result = find_src ? &query->src : &query->dst;
424+
425+
if (find_src && !query->dst)
426+
BUG("query_refspecs: need either src or dst");
427+
428+
if (query_matches_negative_refspec(rs, query))
429+
return -1;
430+
431+
for (i = 0; i < rs->nr; i++) {
432+
struct refspec_item *refspec = &rs->items[i];
433+
const char *key = find_src ? refspec->dst : refspec->src;
434+
const char *value = find_src ? refspec->src : refspec->dst;
435+
436+
if (!refspec->dst || refspec->negative)
437+
continue;
438+
if (refspec->pattern) {
439+
if (match_name_with_pattern(key, needle, value, result)) {
440+
query->force = refspec->force;
441+
return 0;
442+
}
443+
} else if (!strcmp(needle, key)) {
444+
*result = xstrdup(value);
445+
query->force = refspec->force;
446+
return 0;
447+
}
448+
}
449+
return -1;
450+
}
451+
452+
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
453+
{
454+
struct ref **tail;
455+
456+
for (tail = &ref_map; *tail; ) {
457+
struct ref *ref = *tail;
458+
459+
if (omit_name_by_refspec(ref->name, rs)) {
460+
*tail = ref->next;
461+
free(ref->peer_ref);
462+
free(ref);
463+
} else
464+
tail = &ref->next;
465+
}
466+
467+
return ref_map;
468+
}
469+
470+
char *apply_refspecs(struct refspec *rs, const char *name)
471+
{
472+
struct refspec_item query;
473+
474+
memset(&query, 0, sizeof(struct refspec_item));
475+
query.src = (char *)name;
476+
477+
if (query_refspecs(rs, &query))
478+
return NULL;
479+
480+
return query.dst;
481+
}

refspec.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct refspec_item {
3030
char *raw;
3131
};
3232

33+
struct string_list;
34+
3335
#define REFSPEC_FETCH 1
3436
#define REFSPEC_PUSH 0
3537

@@ -71,4 +73,43 @@ struct strvec;
7173
void refspec_ref_prefixes(const struct refspec *rs,
7274
struct strvec *ref_prefixes);
7375

76+
/*
77+
* Check whether a name matches any negative refspec in rs. Returns 1 if the
78+
* name matches at least one negative refspec, and 0 otherwise.
79+
*/
80+
int omit_name_by_refspec(const char *name, struct refspec *rs);
81+
82+
/*
83+
* Checks whether a name matches a pattern and optionally generates a result.
84+
* Returns 1 if the name matches the pattern, 0 otherwise.
85+
*/
86+
int match_name_with_pattern(const char *key, const char *name,
87+
const char *value, char **result);
88+
89+
/*
90+
* Queries a refspec for a match and updates the query item.
91+
* Returns 0 on success, -1 if no match is found or negative refspec matches.
92+
*/
93+
int query_refspecs(struct refspec *rs, struct refspec_item *query);
94+
95+
/*
96+
* Queries a refspec for all matches and appends results to the provided string
97+
* list.
98+
*/
99+
void query_refspecs_multiple(struct refspec *rs,
100+
struct refspec_item *query,
101+
struct string_list *results);
102+
103+
/*
104+
* Remove all entries in the input list which match any negative refspec in
105+
* the refspec list.
106+
*/
107+
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
108+
109+
/*
110+
* Applies refspecs to a name and returns the corresponding destination.
111+
* Returns the destination string if a match is found, NULL otherwise.
112+
*/
113+
char *apply_refspecs(struct refspec *rs, const char *name);
114+
74115
#endif /* REFSPEC_H */

0 commit comments

Comments
 (0)