Skip to content

Commit 230d022

Browse files
inosmeetgitster
authored andcommitted
refspec: relocate refname_matches_negative_refspec_item
Move the functions `refname_matches_negative_refspec_item()`, `refspec_match()`, and `match_name_with_pattern()` from `remote.c` to `refspec.c`. These functions focus on refspec matching, so placing them in `refspec.c` aligns with the separation of concerns. Keep refspec-related logic in `refspec.c` and remote-specific logic in `remote.c` for better code organization. Signed-off-by: Meet Soni <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4f6ab0 commit 230d022

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

refspec.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,51 @@ void refspec_ref_prefixes(const struct refspec *rs,
276276
}
277277
}
278278
}
279+
280+
int match_name_with_pattern(const char *key, const char *name,
281+
const char *value, char **result)
282+
{
283+
const char *kstar = strchr(key, '*');
284+
size_t klen;
285+
size_t ksuffixlen;
286+
size_t namelen;
287+
int ret;
288+
if (!kstar)
289+
die(_("key '%s' of pattern had no '*'"), key);
290+
klen = kstar - key;
291+
ksuffixlen = strlen(kstar + 1);
292+
namelen = strlen(name);
293+
ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
294+
!memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
295+
if (ret && value) {
296+
struct strbuf sb = STRBUF_INIT;
297+
const char *vstar = strchr(value, '*');
298+
if (!vstar)
299+
die(_("value '%s' of pattern has no '*'"), value);
300+
strbuf_add(&sb, value, vstar - value);
301+
strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
302+
strbuf_addstr(&sb, vstar + 1);
303+
*result = strbuf_detach(&sb, NULL);
304+
}
305+
return ret;
306+
}
307+
308+
static int refspec_match(const struct refspec_item *refspec,
309+
const char *name)
310+
{
311+
if (refspec->pattern)
312+
return match_name_with_pattern(refspec->src, name, NULL, NULL);
313+
314+
return !strcmp(refspec->src, name);
315+
}
316+
317+
int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
318+
{
319+
int i;
320+
321+
for (i = 0; i < rs->nr; i++) {
322+
if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
323+
return 1;
324+
}
325+
return 0;
326+
}

refspec.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,13 @@ struct strvec;
7171
void refspec_ref_prefixes(const struct refspec *rs,
7272
struct strvec *ref_prefixes);
7373

74+
int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
75+
76+
/*
77+
* Checks whether a name matches a pattern and optionally generates a result.
78+
* Returns 1 if the name matches the pattern, 0 otherwise.
79+
*/
80+
int match_name_with_pattern(const char *key, const char *name,
81+
const char *value, char **result);
82+
7483
#endif /* REFSPEC_H */

remote.c

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -907,54 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
907907
}
908908
}
909909

910-
static int match_name_with_pattern(const char *key, const char *name,
911-
const char *value, char **result)
912-
{
913-
const char *kstar = strchr(key, '*');
914-
size_t klen;
915-
size_t ksuffixlen;
916-
size_t namelen;
917-
int ret;
918-
if (!kstar)
919-
die(_("key '%s' of pattern had no '*'"), key);
920-
klen = kstar - key;
921-
ksuffixlen = strlen(kstar + 1);
922-
namelen = strlen(name);
923-
ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
924-
!memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
925-
if (ret && value) {
926-
struct strbuf sb = STRBUF_INIT;
927-
const char *vstar = strchr(value, '*');
928-
if (!vstar)
929-
die(_("value '%s' of pattern has no '*'"), value);
930-
strbuf_add(&sb, value, vstar - value);
931-
strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
932-
strbuf_addstr(&sb, vstar + 1);
933-
*result = strbuf_detach(&sb, NULL);
934-
}
935-
return ret;
936-
}
937-
938-
static int refspec_match(const struct refspec_item *refspec,
939-
const char *name)
940-
{
941-
if (refspec->pattern)
942-
return match_name_with_pattern(refspec->src, name, NULL, NULL);
943-
944-
return !strcmp(refspec->src, name);
945-
}
946-
947-
int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
948-
{
949-
int i;
950-
951-
for (i = 0; i < rs->nr; i++) {
952-
if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
953-
return 1;
954-
}
955-
return 0;
956-
}
957-
958910
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
959911
{
960912
struct ref **tail;

0 commit comments

Comments
 (0)