Skip to content

Commit 998c5f0

Browse files
committed
Merge branch 'ms/refspec-cleanup'
Code clean-up. cf. <[email protected]> * ms/refspec-cleanup: refspec: relocate apply_refspecs and related funtions refspec: relocate matching related functions remote: rename query_refspecs functions refspec: relocate refname_matches_negative_refspec_item remote: rename function omit_name_by_refspec
2 parents 791677a + d549b6c commit 998c5f0

File tree

6 files changed

+244
-220
lines changed

6 files changed

+244
-220
lines changed

builtin/push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
7878
.src = matched->name,
7979
};
8080

81-
if (!query_refspecs(&remote->push, &query) && query.dst) {
81+
if (!refspec_find_match(&remote->push, &query) && query.dst) {
8282
refspec_appendf(refspec, "%s%s:%s",
8383
query.force ? "+" : "",
8484
query.src, query.dst);

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
383383
states->remote->fetch.items[i].raw);
384384

385385
for (ref = fetch_map; ref; ref = ref->next) {
386-
if (omit_name_by_refspec(ref->name, &states->remote->fetch))
386+
if (refname_matches_negative_refspec_item(ref->name, &states->remote->fetch))
387387
string_list_append(&states->skipped, abbrev_branch(ref->name));
388388
else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name))
389389
string_list_append(&states->new_refs, abbrev_branch(ref->name));

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 refname_matches_negative_refspec_item(const char *refname, 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], refname))
325+
return 1;
326+
}
327+
return 0;
328+
}
329+
330+
static int refspec_find_negative_match(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 (refname_matches_negative_refspec_item(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 refspec_find_all_matches(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("refspec_find_all_matches: need either src or dst");
396+
397+
if (refspec_find_negative_match(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 refspec_find_match(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("refspec_find_match: need either src or dst");
427+
428+
if (refspec_find_negative_match(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 (refname_matches_negative_refspec_item(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 (refspec_find_match(rs, &query))
478+
return NULL;
479+
480+
return query.dst;
481+
}

refspec.h

Lines changed: 37 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,39 @@ struct strvec;
7173
void refspec_ref_prefixes(const struct refspec *rs,
7274
struct strvec *ref_prefixes);
7375

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

0 commit comments

Comments
 (0)