* [PATCH v3 1/5] refactor(remote): rename function omit_name_by_refspec
2025-02-01 6:41 ` [PATCH v3 0/5] " Meet Soni
@ 2025-02-01 6:41 ` Meet Soni
2025-02-03 6:45 ` Patrick Steinhardt
2025-02-01 6:41 ` [PATCH v3 2/5] refspec: relocate refname_matches_negative_refspec_item Meet Soni
` (4 subsequent siblings)
5 siblings, 1 reply; 33+ messages in thread
From: Meet Soni @ 2025-02-01 6:41 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Jeff King, Jacob Keller,
Junio C Hamano, Jacob Keller, Matthew Rogers, Patrick Steinhardt,
Pavel Rappo
Rename the function `omit_name_by_refspec()` to
`refname_matches_negative_refspec_item()` to provide clearer intent.
The previous function name was vague and did not accurately describe its
purpose. By using `refname_matches_negative_refspec_item`, make the
function's purpose more intuitive, clarifying that it checks if a
reference name matches any negative refspec.
Rename function parameters for consistency with existing naming
conventions. Use `refname` instead of `name` to align with terminology
in `refs.h`.
Remove the redundant doc comment since the function name is now
self-explanatory.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
builtin/remote.c | 2 +-
remote.c | 8 ++++----
remote.h | 6 +-----
3 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 0435963286..258b8895cd 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -383,7 +383,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
states->remote->fetch.items[i].raw);
for (ref = fetch_map; ref; ref = ref->next) {
- if (omit_name_by_refspec(ref->name, &states->remote->fetch))
+ if (refname_matches_negative_refspec_item(ref->name, &states->remote->fetch))
string_list_append(&states->skipped, abbrev_branch(ref->name));
else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name))
string_list_append(&states->new_refs, abbrev_branch(ref->name));
diff --git a/remote.c b/remote.c
index 0f6fba8562..cb70ce6f3b 100644
--- a/remote.c
+++ b/remote.c
@@ -944,12 +944,12 @@ static int refspec_match(const struct refspec_item *refspec,
return !strcmp(refspec->src, name);
}
-int omit_name_by_refspec(const char *name, struct refspec *rs)
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
{
int i;
for (i = 0; i < rs->nr; i++) {
- if (rs->items[i].negative && refspec_match(&rs->items[i], name))
+ if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
return 1;
}
return 0;
@@ -962,7 +962,7 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
for (tail = &ref_map; *tail; ) {
struct ref *ref = *tail;
- if (omit_name_by_refspec(ref->name, rs)) {
+ if (refname_matches_negative_refspec_item(ref->name, rs)) {
*tail = ref->next;
free(ref->peer_ref);
free(ref);
@@ -1021,7 +1021,7 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
}
for (i = 0; !matched_negative && i < reversed.nr; i++) {
- if (omit_name_by_refspec(reversed.items[i].string, rs))
+ if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
matched_negative = 1;
}
diff --git a/remote.h b/remote.h
index bda10dd5c8..66ee53411d 100644
--- a/remote.h
+++ b/remote.h
@@ -261,11 +261,7 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
*/
struct ref *ref_remove_duplicates(struct ref *ref_map);
-/*
- * Check whether a name matches any negative refspec in rs. Returns 1 if the
- * name matches at least one negative refspec, and 0 otherwise.
- */
-int omit_name_by_refspec(const char *name, struct refspec *rs);
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
/*
* Remove all entries in the input list which match any negative refspec in
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* Re: [PATCH v3 1/5] refactor(remote): rename function omit_name_by_refspec
2025-02-01 6:41 ` [PATCH v3 1/5] refactor(remote): rename function omit_name_by_refspec Meet Soni
@ 2025-02-03 6:45 ` Patrick Steinhardt
0 siblings, 0 replies; 33+ messages in thread
From: Patrick Steinhardt @ 2025-02-03 6:45 UTC (permalink / raw)
To: Meet Soni
Cc: git, shubham.kanodia10, Jeff King, Jacob Keller, Junio C Hamano,
Jacob Keller, Matthew Rogers, Pavel Rappo
On Sat, Feb 01, 2025 at 12:11:58PM +0530, Meet Soni wrote:
Please drop the `refactor()` bit from the commit subject, we don't use
these prefixes here. You also did the same in a later commit.
> Rename the function `omit_name_by_refspec()` to
> `refname_matches_negative_refspec_item()` to provide clearer intent.
> The previous function name was vague and did not accurately describe its
> purpose. By using `refname_matches_negative_refspec_item`, make the
> function's purpose more intuitive, clarifying that it checks if a
> reference name matches any negative refspec.
The new name certainly reads way better, and the changes themselves look
Patrick
^ permalink raw reply [flat|nested] 33+ messages in thread
* [PATCH v3 2/5] refspec: relocate refname_matches_negative_refspec_item
2025-02-01 6:41 ` [PATCH v3 0/5] " Meet Soni
2025-02-01 6:41 ` [PATCH v3 1/5] refactor(remote): rename function omit_name_by_refspec Meet Soni
@ 2025-02-01 6:41 ` Meet Soni
2025-02-01 6:42 ` [PATCH v3 3/5] refactor(remote): rename query_refspecs functions Meet Soni
` (3 subsequent siblings)
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-01 6:41 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Junio C Hamano, Patrick Steinhardt,
Matthew Rogers, Jacob Keller, Jeff King
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 <meetsoni3017@gmail.com>
---
refspec.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
refspec.h | 9 +++++++++
remote.c | 48 ------------------------------------------------
3 files changed, 57 insertions(+), 48 deletions(-)
diff --git a/refspec.c b/refspec.c
index 6d86e04442..b447768304 100644
--- a/refspec.c
+++ b/refspec.c
@@ -276,3 +276,51 @@ void refspec_ref_prefixes(const struct refspec *rs,
}
}
}
+
+int match_name_with_pattern(const char *key, const char *name,
+ const char *value, char **result)
+{
+ const char *kstar = strchr(key, '*');
+ size_t klen;
+ size_t ksuffixlen;
+ size_t namelen;
+ int ret;
+ if (!kstar)
+ die(_("key '%s' of pattern had no '*'"), key);
+ klen = kstar - key;
+ ksuffixlen = strlen(kstar + 1);
+ namelen = strlen(name);
+ ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
+ !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
+ if (ret && value) {
+ struct strbuf sb = STRBUF_INIT;
+ const char *vstar = strchr(value, '*');
+ if (!vstar)
+ die(_("value '%s' of pattern has no '*'"), value);
+ strbuf_add(&sb, value, vstar - value);
+ strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
+ strbuf_addstr(&sb, vstar + 1);
+ *result = strbuf_detach(&sb, NULL);
+ }
+ return ret;
+}
+
+static int refspec_match(const struct refspec_item *refspec,
+ const char *name)
+{
+ if (refspec->pattern)
+ return match_name_with_pattern(refspec->src, name, NULL, NULL);
+
+ return !strcmp(refspec->src, name);
+}
+
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
+{
+ int i;
+
+ for (i = 0; i < rs->nr; i++) {
+ if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
+ return 1;
+ }
+ return 0;
+}
diff --git a/refspec.h b/refspec.h
index 69d693c87d..584d9c9eb5 100644
--- a/refspec.h
+++ b/refspec.h
@@ -71,4 +71,13 @@ struct strvec;
void refspec_ref_prefixes(const struct refspec *rs,
struct strvec *ref_prefixes);
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
+
+/*
+ * Checks whether a name matches a pattern and optionally generates a result.
+ * Returns 1 if the name matches the pattern, 0 otherwise.
+ */
+int match_name_with_pattern(const char *key, const char *name,
+ const char *value, char **result);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index cb70ce6f3b..1da8ec7037 100644
--- a/remote.c
+++ b/remote.c
@@ -907,54 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
}
}
-static int match_name_with_pattern(const char *key, const char *name,
- const char *value, char **result)
-{
- const char *kstar = strchr(key, '*');
- size_t klen;
- size_t ksuffixlen;
- size_t namelen;
- int ret;
- if (!kstar)
- die(_("key '%s' of pattern had no '*'"), key);
- klen = kstar - key;
- ksuffixlen = strlen(kstar + 1);
- namelen = strlen(name);
- ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
- !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
- if (ret && value) {
- struct strbuf sb = STRBUF_INIT;
- const char *vstar = strchr(value, '*');
- if (!vstar)
- die(_("value '%s' of pattern has no '*'"), value);
- strbuf_add(&sb, value, vstar - value);
- strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
- strbuf_addstr(&sb, vstar + 1);
- *result = strbuf_detach(&sb, NULL);
- }
- return ret;
-}
-
-static int refspec_match(const struct refspec_item *refspec,
- const char *name)
-{
- if (refspec->pattern)
- return match_name_with_pattern(refspec->src, name, NULL, NULL);
-
- return !strcmp(refspec->src, name);
-}
-
-int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
-{
- int i;
-
- for (i = 0; i < rs->nr; i++) {
- if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
- return 1;
- }
- return 0;
-}
-
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
{
struct ref **tail;
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v3 3/5] refactor(remote): rename query_refspecs functions
2025-02-01 6:41 ` [PATCH v3 0/5] " Meet Soni
2025-02-01 6:41 ` [PATCH v3 1/5] refactor(remote): rename function omit_name_by_refspec Meet Soni
2025-02-01 6:41 ` [PATCH v3 2/5] refspec: relocate refname_matches_negative_refspec_item Meet Soni
@ 2025-02-01 6:42 ` Meet Soni
2025-02-03 6:46 ` Patrick Steinhardt
2025-02-01 6:42 ` [PATCH v3 4/5] refspec: relocate matching related functions Meet Soni
` (2 subsequent siblings)
5 siblings, 1 reply; 33+ messages in thread
From: Meet Soni @ 2025-02-01 6:42 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Patrick Steinhardt, Junio C Hamano,
Matthew Rogers, René Scharfe, Jacob Keller, Denton Liu
Rename `query_refspecs()` to `find_refspec_match` for clarity, as it
finds a single matching refspec.
Rename `query_refspecs_multiple()` to `find_all_refspec_matches` to
better reflect that it collects all matching refspecs instead of
returning just the first match.
Rename `query_matches_negative_refspec()` to
`find_negative_refspec_match` for consistency with the updated naming
convention.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
builtin/push.c | 2 +-
remote.c | 20 ++++++++++----------
remote.h | 2 +-
3 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index 90de3746b5..e6527b0b04 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -78,7 +78,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
.src = matched->name,
};
- if (!query_refspecs(&remote->push, &query) && query.dst) {
+ if (!find_refspec_match(&remote->push, &query) && query.dst) {
refspec_appendf(refspec, "%s%s:%s",
query.force ? "+" : "",
query.src, query.dst);
diff --git a/remote.c b/remote.c
index 1da8ec7037..4654bce5d4 100644
--- a/remote.c
+++ b/remote.c
@@ -925,7 +925,7 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
return ref_map;
}
-static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
+static int find_negative_refspec_match(struct refspec *rs, struct refspec_item *query)
{
int i, matched_negative = 0;
int find_src = !query->src;
@@ -982,7 +982,7 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
return matched_negative;
}
-static void query_refspecs_multiple(struct refspec *rs,
+static void find_all_refspec_matches(struct refspec *rs,
struct refspec_item *query,
struct string_list *results)
{
@@ -990,9 +990,9 @@ static void query_refspecs_multiple(struct refspec *rs,
int find_src = !query->src;
if (find_src && !query->dst)
- BUG("query_refspecs_multiple: need either src or dst");
+ BUG("find_all_refspec_matches: need either src or dst");
- if (query_matches_negative_refspec(rs, query))
+ if (find_negative_refspec_match(rs, query))
return;
for (i = 0; i < rs->nr; i++) {
@@ -1013,7 +1013,7 @@ static void query_refspecs_multiple(struct refspec *rs,
}
}
-int query_refspecs(struct refspec *rs, struct refspec_item *query)
+int find_refspec_match(struct refspec *rs, struct refspec_item *query)
{
int i;
int find_src = !query->src;
@@ -1021,9 +1021,9 @@ int query_refspecs(struct refspec *rs, struct refspec_item *query)
char **result = find_src ? &query->src : &query->dst;
if (find_src && !query->dst)
- BUG("query_refspecs: need either src or dst");
+ BUG("find_refspec_match: need either src or dst");
- if (query_matches_negative_refspec(rs, query))
+ if (find_negative_refspec_match(rs, query))
return -1;
for (i = 0; i < rs->nr; i++) {
@@ -1054,7 +1054,7 @@ char *apply_refspecs(struct refspec *rs, const char *name)
memset(&query, 0, sizeof(struct refspec_item));
query.src = (char *)name;
- if (query_refspecs(rs, &query))
+ if (find_refspec_match(rs, &query))
return NULL;
return query.dst;
@@ -1062,7 +1062,7 @@ char *apply_refspecs(struct refspec *rs, const char *name)
int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
{
- return query_refspecs(&remote->fetch, refspec);
+ return find_refspec_match(&remote->fetch, refspec);
}
static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
@@ -2487,7 +2487,7 @@ static int get_stale_heads_cb(const char *refname, const char *referent UNUSED,
memset(&query, 0, sizeof(struct refspec_item));
query.dst = (char *)refname;
- query_refspecs_multiple(info->rs, &query, &matches);
+ find_all_refspec_matches(info->rs, &query, &matches);
if (matches.nr == 0)
goto clean_exit; /* No matches */
diff --git a/remote.h b/remote.h
index 66ee53411d..f109310eda 100644
--- a/remote.h
+++ b/remote.h
@@ -269,7 +269,7 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
*/
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
-int query_refspecs(struct refspec *rs, struct refspec_item *query);
+int find_refspec_match(struct refspec *rs, struct refspec_item *query);
char *apply_refspecs(struct refspec *rs, const char *name);
int check_push_refs(struct ref *src, struct refspec *rs);
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* Re: [PATCH v3 3/5] refactor(remote): rename query_refspecs functions
2025-02-01 6:42 ` [PATCH v3 3/5] refactor(remote): rename query_refspecs functions Meet Soni
@ 2025-02-03 6:46 ` Patrick Steinhardt
2025-02-04 3:39 ` Meet Soni
0 siblings, 1 reply; 33+ messages in thread
From: Patrick Steinhardt @ 2025-02-03 6:46 UTC (permalink / raw)
To: Meet Soni
Cc: git, shubham.kanodia10, Junio C Hamano, Matthew Rogers,
René Scharfe, Jacob Keller, Denton Liu
On Sat, Feb 01, 2025 at 12:12:00PM +0530, Meet Soni wrote:
> Rename `query_refspecs()` to `find_refspec_match` for clarity, as it
> finds a single matching refspec.
>
> Rename `query_refspecs_multiple()` to `find_all_refspec_matches` to
> better reflect that it collects all matching refspecs instead of
> returning just the first match.
>
> Rename `query_matches_negative_refspec()` to
> `find_negative_refspec_match` for consistency with the updated naming
> convention.
Okay. The message might've read a tiny bit easier if it was a bulleted
list of renames. E.g.:
We're about to move a couple of functions related to handling of
refspecs from "remote.c" into "refspec.c". In preparation for this
move, rename them to better reflect their intent:
- `query_refspecs()` becomes `find_refspec_match()` for clarity,
as it finds a single matching refspec.
...
I was wondering a bit about why we rename the static functions, as we
wouldn't have to expose them in a subsequent step anyway. Other than
that I think we should adhere to our coding guidelines with the renamed
public functions:
The primary data structure that a subsystem 'S' deals with is called
`struct S`. Functions that operate on `struct S` are named
`S_<verb>()` and should generally receive a pointer to `struct S` as
first parameter. E.g.
So:
- `query_refspecs()` would be renamed to `refspec_find_match()`.
- `query_refspecs_multiple()` would be renamed to
`refspec_find_all_matches()`.
- `find_negative_refspec_match()` would be renamed to
`refspec_find_negative_match()`.
Patrick
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v3 3/5] refactor(remote): rename query_refspecs functions
2025-02-03 6:46 ` Patrick Steinhardt
@ 2025-02-04 3:39 ` Meet Soni
2025-02-04 13:58 ` Junio C Hamano
0 siblings, 1 reply; 33+ messages in thread
From: Meet Soni @ 2025-02-04 3:39 UTC (permalink / raw)
To: Patrick Steinhardt
Cc: git, shubham.kanodia10, Junio C Hamano, Matthew Rogers,
René Scharfe, Jacob Keller, Denton Liu
On Mon, 3 Feb 2025 at 12:16, Patrick Steinhardt <ps@pks.im> wrote:
>
> On Sat, Feb 01, 2025 at 12:12:00PM +0530, Meet Soni wrote:
> > Rename `query_refspecs()` to `find_refspec_match` for clarity, as it
> > finds a single matching refspec.
> >
> > Rename `query_refspecs_multiple()` to `find_all_refspec_matches` to
> > better reflect that it collects all matching refspecs instead of
> > returning just the first match.
> >
> > Rename `query_matches_negative_refspec()` to
> > `find_negative_refspec_match` for consistency with the updated naming
> > convention.
>
> Okay. The message might've read a tiny bit easier if it was a bulleted
> list of renames. E.g.:
>
> We're about to move a couple of functions related to handling of
> refspecs from "remote.c" into "refspec.c". In preparation for this
> move, rename them to better reflect their intent:
>
> - `query_refspecs()` becomes `find_refspec_match()` for clarity,
> as it finds a single matching refspec.
>
> ...
Makes sense.
>
> I was wondering a bit about why we rename the static functions, as we
> wouldn't have to expose them in a subsequent step anyway. Other than
> that I think we should adhere to our coding guidelines with the renamed
> public functions:
Since we were renaming the query_* functions that are exposed, I updated
the static ones as well to maintain naming consistency across related functions.
>
> The primary data structure that a subsystem 'S' deals with is called
> `struct S`. Functions that operate on `struct S` are named
> `S_<verb>()` and should generally receive a pointer to `struct S` as
> first parameter. E.g.
>
> So:
>
> - `query_refspecs()` would be renamed to `refspec_find_match()`.
>
> - `query_refspecs_multiple()` would be renamed to
> `refspec_find_all_matches()`.
>
> - `find_negative_refspec_match()` would be renamed to
> `refspec_find_negative_match()`.
>
> Patrick
Thanks for the review.
Meet
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v3 3/5] refactor(remote): rename query_refspecs functions
2025-02-04 3:39 ` Meet Soni
@ 2025-02-04 13:58 ` Junio C Hamano
0 siblings, 0 replies; 33+ messages in thread
From: Junio C Hamano @ 2025-02-04 13:58 UTC (permalink / raw)
To: Meet Soni
Cc: Patrick Steinhardt, git, shubham.kanodia10, Matthew Rogers,
René Scharfe, Jacob Keller, Denton Liu
Meet Soni <meetsoni3017@gmail.com> writes:
>> So:
>>
>> - `query_refspecs()` would be renamed to `refspec_find_match()`.
>>
>> - `query_refspecs_multiple()` would be renamed to
>> `refspec_find_all_matches()`.
>>
>> - `find_negative_refspec_match()` would be renamed to
>> `refspec_find_negative_match()`.
>>
>> Patrick
>
> Thanks for the review.
> Meet
Yup, using predictable names that follow patterns based on
easy-to-follow rules is a very useful tool to help developers.
Thanks, both.
^ permalink raw reply [flat|nested] 33+ messages in thread
* [PATCH v3 4/5] refspec: relocate matching related functions
2025-02-01 6:41 ` [PATCH v3 0/5] " Meet Soni
` (2 preceding siblings ...)
2025-02-01 6:42 ` [PATCH v3 3/5] refactor(remote): rename query_refspecs functions Meet Soni
@ 2025-02-01 6:42 ` Meet Soni
2025-02-01 6:42 ` [PATCH v3 5/5] refspec: relocate apply_refspecs and related funtions Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-01 6:42 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Jeff King, Elijah Newren,
Nipunn Koorapati, Junio C Hamano
Move the functions `find_refspec_match()`, `find_all_refspec_matches()`
and `find_negative_refspec_match()` from `remote.c` to `refspec.c`.
These functions focus on matching refspecs, so centralizing them in
`refspec.c` improves code organization by keeping refspec-related logic
in one place.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
refspec.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
refspec.h | 16 +++++++
remote.c | 122 -----------------------------------------------------
3 files changed, 139 insertions(+), 122 deletions(-)
diff --git a/refspec.c b/refspec.c
index b447768304..6634e7765d 100644
--- a/refspec.c
+++ b/refspec.c
@@ -5,6 +5,7 @@
#include "gettext.h"
#include "hash.h"
#include "hex.h"
+#include "string-list.h"
#include "strvec.h"
#include "refs.h"
#include "refspec.h"
@@ -324,3 +325,125 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
}
return 0;
}
+
+static int find_negative_refspec_match(struct refspec *rs, struct refspec_item *query)
+{
+ int i, matched_negative = 0;
+ int find_src = !query->src;
+ struct string_list reversed = STRING_LIST_INIT_DUP;
+ const char *needle = find_src ? query->dst : query->src;
+
+ /*
+ * Check whether the queried ref matches any negative refpsec. If so,
+ * then we should ultimately treat this as not matching the query at
+ * all.
+ *
+ * Note that negative refspecs always match the source, but the query
+ * item uses the destination. To handle this, we apply pattern
+ * refspecs in reverse to figure out if the query source matches any
+ * of the negative refspecs.
+ *
+ * The first loop finds and expands all positive refspecs
+ * matched by the queried ref.
+ *
+ * The second loop checks if any of the results of the first loop
+ * match any negative refspec.
+ */
+ for (i = 0; i < rs->nr; i++) {
+ struct refspec_item *refspec = &rs->items[i];
+ char *expn_name;
+
+ if (refspec->negative)
+ continue;
+
+ /* Note the reversal of src and dst */
+ if (refspec->pattern) {
+ const char *key = refspec->dst ? refspec->dst : refspec->src;
+ const char *value = refspec->src;
+
+ if (match_name_with_pattern(key, needle, value, &expn_name))
+ string_list_append_nodup(&reversed, expn_name);
+ } else if (refspec->matching) {
+ /* For the special matching refspec, any query should match */
+ string_list_append(&reversed, needle);
+ } else if (!refspec->src) {
+ BUG("refspec->src should not be null here");
+ } else if (!strcmp(needle, refspec->src)) {
+ string_list_append(&reversed, refspec->src);
+ }
+ }
+
+ for (i = 0; !matched_negative && i < reversed.nr; i++) {
+ if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
+ matched_negative = 1;
+ }
+
+ string_list_clear(&reversed, 0);
+
+ return matched_negative;
+}
+
+void find_all_refspec_matches(struct refspec *rs,
+ struct refspec_item *query,
+ struct string_list *results)
+{
+ int i;
+ int find_src = !query->src;
+
+ if (find_src && !query->dst)
+ BUG("find_all_refspec_matches: need either src or dst");
+
+ if (find_negative_refspec_match(rs, query))
+ return;
+
+ for (i = 0; i < rs->nr; i++) {
+ struct refspec_item *refspec = &rs->items[i];
+ const char *key = find_src ? refspec->dst : refspec->src;
+ const char *value = find_src ? refspec->src : refspec->dst;
+ const char *needle = find_src ? query->dst : query->src;
+ char **result = find_src ? &query->src : &query->dst;
+
+ if (!refspec->dst || refspec->negative)
+ continue;
+ if (refspec->pattern) {
+ if (match_name_with_pattern(key, needle, value, result))
+ string_list_append_nodup(results, *result);
+ } else if (!strcmp(needle, key)) {
+ string_list_append(results, value);
+ }
+ }
+}
+
+int find_refspec_match(struct refspec *rs, struct refspec_item *query)
+{
+ int i;
+ int find_src = !query->src;
+ const char *needle = find_src ? query->dst : query->src;
+ char **result = find_src ? &query->src : &query->dst;
+
+ if (find_src && !query->dst)
+ BUG("find_refspec_match: need either src or dst");
+
+ if (find_negative_refspec_match(rs, query))
+ return -1;
+
+ for (i = 0; i < rs->nr; i++) {
+ struct refspec_item *refspec = &rs->items[i];
+ const char *key = find_src ? refspec->dst : refspec->src;
+ const char *value = find_src ? refspec->src : refspec->dst;
+
+ if (!refspec->dst || refspec->negative)
+ continue;
+ if (refspec->pattern) {
+ if (match_name_with_pattern(key, needle, value, result)) {
+ query->force = refspec->force;
+ return 0;
+ }
+ } else if (!strcmp(needle, key)) {
+ *result = xstrdup(value);
+ query->force = refspec->force;
+ return 0;
+ }
+ }
+ return -1;
+}
diff --git a/refspec.h b/refspec.h
index 584d9c9eb5..0393643bc8 100644
--- a/refspec.h
+++ b/refspec.h
@@ -30,6 +30,8 @@ struct refspec_item {
char *raw;
};
+struct string_list;
+
#define REFSPEC_FETCH 1
#define REFSPEC_PUSH 0
@@ -80,4 +82,18 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
int match_name_with_pattern(const char *key, const char *name,
const char *value, char **result);
+/*
+ * Queries a refspec for a match and updates the query item.
+ * Returns 0 on success, -1 if no match is found or negative refspec matches.
+ */
+int find_refspec_match(struct refspec *rs, struct refspec_item *query);
+
+/*
+ * Queries a refspec for all matches and appends results to the provided string
+ * list.
+ */
+void find_all_refspec_matches(struct refspec *rs,
+ struct refspec_item *query,
+ struct string_list *results);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index 4654bce5d4..858ab39471 100644
--- a/remote.c
+++ b/remote.c
@@ -925,128 +925,6 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
return ref_map;
}
-static int find_negative_refspec_match(struct refspec *rs, struct refspec_item *query)
-{
- int i, matched_negative = 0;
- int find_src = !query->src;
- struct string_list reversed = STRING_LIST_INIT_DUP;
- const char *needle = find_src ? query->dst : query->src;
-
- /*
- * Check whether the queried ref matches any negative refpsec. If so,
- * then we should ultimately treat this as not matching the query at
- * all.
- *
- * Note that negative refspecs always match the source, but the query
- * item uses the destination. To handle this, we apply pattern
- * refspecs in reverse to figure out if the query source matches any
- * of the negative refspecs.
- *
- * The first loop finds and expands all positive refspecs
- * matched by the queried ref.
- *
- * The second loop checks if any of the results of the first loop
- * match any negative refspec.
- */
- for (i = 0; i < rs->nr; i++) {
- struct refspec_item *refspec = &rs->items[i];
- char *expn_name;
-
- if (refspec->negative)
- continue;
-
- /* Note the reversal of src and dst */
- if (refspec->pattern) {
- const char *key = refspec->dst ? refspec->dst : refspec->src;
- const char *value = refspec->src;
-
- if (match_name_with_pattern(key, needle, value, &expn_name))
- string_list_append_nodup(&reversed, expn_name);
- } else if (refspec->matching) {
- /* For the special matching refspec, any query should match */
- string_list_append(&reversed, needle);
- } else if (!refspec->src) {
- BUG("refspec->src should not be null here");
- } else if (!strcmp(needle, refspec->src)) {
- string_list_append(&reversed, refspec->src);
- }
- }
-
- for (i = 0; !matched_negative && i < reversed.nr; i++) {
- if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
- matched_negative = 1;
- }
-
- string_list_clear(&reversed, 0);
-
- return matched_negative;
-}
-
-static void find_all_refspec_matches(struct refspec *rs,
- struct refspec_item *query,
- struct string_list *results)
-{
- int i;
- int find_src = !query->src;
-
- if (find_src && !query->dst)
- BUG("find_all_refspec_matches: need either src or dst");
-
- if (find_negative_refspec_match(rs, query))
- return;
-
- for (i = 0; i < rs->nr; i++) {
- struct refspec_item *refspec = &rs->items[i];
- const char *key = find_src ? refspec->dst : refspec->src;
- const char *value = find_src ? refspec->src : refspec->dst;
- const char *needle = find_src ? query->dst : query->src;
- char **result = find_src ? &query->src : &query->dst;
-
- if (!refspec->dst || refspec->negative)
- continue;
- if (refspec->pattern) {
- if (match_name_with_pattern(key, needle, value, result))
- string_list_append_nodup(results, *result);
- } else if (!strcmp(needle, key)) {
- string_list_append(results, value);
- }
- }
-}
-
-int find_refspec_match(struct refspec *rs, struct refspec_item *query)
-{
- int i;
- int find_src = !query->src;
- const char *needle = find_src ? query->dst : query->src;
- char **result = find_src ? &query->src : &query->dst;
-
- if (find_src && !query->dst)
- BUG("find_refspec_match: need either src or dst");
-
- if (find_negative_refspec_match(rs, query))
- return -1;
-
- for (i = 0; i < rs->nr; i++) {
- struct refspec_item *refspec = &rs->items[i];
- const char *key = find_src ? refspec->dst : refspec->src;
- const char *value = find_src ? refspec->src : refspec->dst;
-
- if (!refspec->dst || refspec->negative)
- continue;
- if (refspec->pattern) {
- if (match_name_with_pattern(key, needle, value, result)) {
- query->force = refspec->force;
- return 0;
- }
- } else if (!strcmp(needle, key)) {
- *result = xstrdup(value);
- query->force = refspec->force;
- return 0;
- }
- }
- return -1;
-}
-
char *apply_refspecs(struct refspec *rs, const char *name)
{
struct refspec_item query;
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v3 5/5] refspec: relocate apply_refspecs and related funtions
2025-02-01 6:41 ` [PATCH v3 0/5] " Meet Soni
` (3 preceding siblings ...)
2025-02-01 6:42 ` [PATCH v3 4/5] refspec: relocate matching related functions Meet Soni
@ 2025-02-01 6:42 ` Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-01 6:42 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Pavel Rappo, Patrick Steinhardt,
Jeff King, Matthew Rogers, Junio C Hamano, Jacob Keller,
Elijah Newren, Jacob Keller
Move the functions `apply_refspecs()` and `apply_negative_refspecs()`
from `remote.c` to `refspec.c`. These functions focus on applying
refspecs, so centralizing them in `refspec.c` improves code organization
by keeping refspec-related logic in one place.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
refspec.c | 32 ++++++++++++++++++++++++++++++++
refspec.h | 12 ++++++++++++
remote.c | 31 -------------------------------
remote.h | 11 -----------
4 files changed, 44 insertions(+), 42 deletions(-)
diff --git a/refspec.c b/refspec.c
index 6634e7765d..47974e86f0 100644
--- a/refspec.c
+++ b/refspec.c
@@ -9,6 +9,7 @@
#include "strvec.h"
#include "refs.h"
#include "refspec.h"
+#include "remote.h"
#include "strbuf.h"
/*
@@ -447,3 +448,34 @@ int find_refspec_match(struct refspec *rs, struct refspec_item *query)
}
return -1;
}
+
+struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
+{
+ struct ref **tail;
+
+ for (tail = &ref_map; *tail; ) {
+ struct ref *ref = *tail;
+
+ if (refname_matches_negative_refspec_item(ref->name, rs)) {
+ *tail = ref->next;
+ free(ref->peer_ref);
+ free(ref);
+ } else
+ tail = &ref->next;
+ }
+
+ return ref_map;
+}
+
+char *apply_refspecs(struct refspec *rs, const char *name)
+{
+ struct refspec_item query;
+
+ memset(&query, 0, sizeof(struct refspec_item));
+ query.src = (char *)name;
+
+ if (find_refspec_match(rs, &query))
+ return NULL;
+
+ return query.dst;
+}
diff --git a/refspec.h b/refspec.h
index 0393643bc8..5cbdc5f622 100644
--- a/refspec.h
+++ b/refspec.h
@@ -96,4 +96,16 @@ void find_all_refspec_matches(struct refspec *rs,
struct refspec_item *query,
struct string_list *results);
+/*
+ * Remove all entries in the input list which match any negative refspec in
+ * the refspec list.
+ */
+struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
+
+/*
+ * Search for a refspec that matches the given name and return the
+ * corresponding destination (dst) if a match is found, NULL otherwise.
+ */
+char *apply_refspecs(struct refspec *rs, const char *name);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index 858ab39471..ad16d2493d 100644
--- a/remote.c
+++ b/remote.c
@@ -907,37 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
}
}
-struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
-{
- struct ref **tail;
-
- for (tail = &ref_map; *tail; ) {
- struct ref *ref = *tail;
-
- if (refname_matches_negative_refspec_item(ref->name, rs)) {
- *tail = ref->next;
- free(ref->peer_ref);
- free(ref);
- } else
- tail = &ref->next;
- }
-
- return ref_map;
-}
-
-char *apply_refspecs(struct refspec *rs, const char *name)
-{
- struct refspec_item query;
-
- memset(&query, 0, sizeof(struct refspec_item));
- query.src = (char *)name;
-
- if (find_refspec_match(rs, &query))
- return NULL;
-
- return query.dst;
-}
-
int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
{
return find_refspec_match(&remote->fetch, refspec);
diff --git a/remote.h b/remote.h
index f109310eda..b4bb16af0e 100644
--- a/remote.h
+++ b/remote.h
@@ -261,17 +261,6 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
*/
struct ref *ref_remove_duplicates(struct ref *ref_map);
-int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
-
-/*
- * Remove all entries in the input list which match any negative refspec in
- * the refspec list.
- */
-struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
-
-int find_refspec_match(struct refspec *rs, struct refspec_item *query);
-char *apply_refspecs(struct refspec *rs, const char *name);
-
int check_push_refs(struct ref *src, struct refspec *rs);
int match_push_refs(struct ref *src, struct ref **dst,
struct refspec *rs, int flags);
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic
2025-02-01 6:41 ` [PATCH v3 0/5] " Meet Soni
` (4 preceding siblings ...)
2025-02-01 6:42 ` [PATCH v3 5/5] refspec: relocate apply_refspecs and related funtions Meet Soni
@ 2025-02-04 4:05 ` Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec Meet Soni
` (5 more replies)
5 siblings, 6 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-04 4:05 UTC (permalink / raw)
To: git; +Cc: shubham.kanodia10, Meet Soni
Changes since v3:
- updated commit message.
- renamed functions as per review.
- added GSoC mark , since the announcement has been made by google and
we've started the discussion regarding the same.
Additional context of the series:
Patrick pointed out in [1], the logic related to refspec is currently
split across multiple headers. This patch series addresses that by
renaming and relocating refspec-related logic from remote to refspec for
improved cohesion.
[1]: https://lore.kernel.org/git/ZysQvUyxgdRqjvj2@pks.im/
Specifically, the following changes have been made:
Refactoring and renaming functions: Functions such as
omit_name_by_refspec() have been renamed to better reflect their
functionality.
Relocation of functions: Functions that are primarily responsible
for refspec related functionality, have been relocated from remote.c
to refspec.c to maintain a clear separation of concerns.
Meet Soni (5):
remote: rename function omit_name_by_refspec
refspec: relocate refname_matches_negative_refspec_item
remote: rename query_refspecs functions
refspec: relocate matching related functions
refspec: relocate apply_refspecs and related funtions
builtin/push.c | 2 +-
builtin/remote.c | 2 +-
refspec.c | 203 ++++++++++++++++++++++++++++++++++++++++++++++
refspec.h | 37 +++++++++
remote.c | 205 +----------------------------------------------
remote.h | 15 ----
6 files changed, 244 insertions(+), 220 deletions(-)
Range-diff against v3:
1: 399e59ff67 ! 1: 1b8606ffcb refactor(remote): rename function omit_name_by_refspec
@@ Metadata
Author: Meet Soni <meetsoni3017@gmail.com>
## Commit message ##
- refactor(remote): rename function omit_name_by_refspec
+ remote: rename function omit_name_by_refspec
Rename the function `omit_name_by_refspec()` to
`refname_matches_negative_refspec_item()` to provide clearer intent.
2: 4109b2bd1c = 2: 3da817839c refspec: relocate refname_matches_negative_refspec_item
3: 559224864f ! 3: bad0c43c96 refactor(remote): rename query_refspecs functions
@@ Metadata
Author: Meet Soni <meetsoni3017@gmail.com>
## Commit message ##
- refactor(remote): rename query_refspecs functions
+ remote: rename query_refspecs functions
- Rename `query_refspecs()` to `find_refspec_match` for clarity, as it
- finds a single matching refspec.
+ Rename functions related to handling refspecs in preparation for their
+ move from `remote.c` to `refspec.c`. Update their names to better
+ reflect their intent:
- Rename `query_refspecs_multiple()` to `find_all_refspec_matches` to
- better reflect that it collects all matching refspecs instead of
- returning just the first match.
+ - `query_refspecs()` -> `refspec_find_match()` for clarity, as it
+ finds a single matching refspec.
- Rename `query_matches_negative_refspec()` to
- `find_negative_refspec_match` for consistency with the updated naming
- convention.
+ - `query_refspecs_multiple()` -> `refspec_find_all_matches()` to
+ better reflect that it collects all matching refspecs instead of
+ returning just the first match.
+
+ - `query_matches_negative_refspec()` ->
+ `refspec_find_negative_match()` for consistency with the
+ updated naming convention, even though this static function
+ didn't strictly require renaming.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
@@ builtin/push.c: static void refspec_append_mapped(struct refspec *refspec, const
};
- if (!query_refspecs(&remote->push, &query) && query.dst) {
-+ if (!find_refspec_match(&remote->push, &query) && query.dst) {
++ if (!refspec_find_match(&remote->push, &query) && query.dst) {
refspec_appendf(refspec, "%s%s:%s",
query.force ? "+" : "",
query.src, query.dst);
@@ remote.c: struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspe
}
-static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
-+static int find_negative_refspec_match(struct refspec *rs, struct refspec_item *query)
++static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
{
int i, matched_negative = 0;
int find_src = !query->src;
@@ remote.c: static int query_matches_negative_refspec(struct refspec *rs, struct r
}
-static void query_refspecs_multiple(struct refspec *rs,
-+static void find_all_refspec_matches(struct refspec *rs,
++static void refspec_find_all_matches(struct refspec *rs,
struct refspec_item *query,
struct string_list *results)
{
@@ remote.c: static void query_refspecs_multiple(struct refspec *rs,
if (find_src && !query->dst)
- BUG("query_refspecs_multiple: need either src or dst");
-+ BUG("find_all_refspec_matches: need either src or dst");
++ BUG("refspec_find_all_matches: need either src or dst");
- if (query_matches_negative_refspec(rs, query))
-+ if (find_negative_refspec_match(rs, query))
++ if (refspec_find_negative_match(rs, query))
return;
for (i = 0; i < rs->nr; i++) {
@@ remote.c: static void query_refspecs_multiple(struct refspec *rs,
}
-int query_refspecs(struct refspec *rs, struct refspec_item *query)
-+int find_refspec_match(struct refspec *rs, struct refspec_item *query)
++int refspec_find_match(struct refspec *rs, struct refspec_item *query)
{
int i;
int find_src = !query->src;
@@ remote.c: int query_refspecs(struct refspec *rs, struct refspec_item *query)
if (find_src && !query->dst)
- BUG("query_refspecs: need either src or dst");
-+ BUG("find_refspec_match: need either src or dst");
++ BUG("refspec_find_match: need either src or dst");
- if (query_matches_negative_refspec(rs, query))
-+ if (find_negative_refspec_match(rs, query))
++ if (refspec_find_negative_match(rs, query))
return -1;
for (i = 0; i < rs->nr; i++) {
@@ remote.c: char *apply_refspecs(struct refspec *rs, const char *name)
query.src = (char *)name;
- if (query_refspecs(rs, &query))
-+ if (find_refspec_match(rs, &query))
++ if (refspec_find_match(rs, &query))
return NULL;
return query.dst;
@@ remote.c: char *apply_refspecs(struct refspec *rs, const char *name)
int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
{
- return query_refspecs(&remote->fetch, refspec);
-+ return find_refspec_match(&remote->fetch, refspec);
++ return refspec_find_match(&remote->fetch, refspec);
}
static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
@@ remote.c: static int get_stale_heads_cb(const char *refname, const char *referen
query.dst = (char *)refname;
- query_refspecs_multiple(info->rs, &query, &matches);
-+ find_all_refspec_matches(info->rs, &query, &matches);
++ refspec_find_all_matches(info->rs, &query, &matches);
if (matches.nr == 0)
goto clean_exit; /* No matches */
@@ remote.h: int refname_matches_negative_refspec_item(const char *refname, struct
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
-int query_refspecs(struct refspec *rs, struct refspec_item *query);
-+int find_refspec_match(struct refspec *rs, struct refspec_item *query);
++int refspec_find_match(struct refspec *rs, struct refspec_item *query);
char *apply_refspecs(struct refspec *rs, const char *name);
int check_push_refs(struct ref *src, struct refspec *rs);
4: 13e49509fc ! 4: 9a5dc26731 refspec: relocate matching related functions
@@ Metadata
## Commit message ##
refspec: relocate matching related functions
- Move the functions `find_refspec_match()`, `find_all_refspec_matches()`
- and `find_negative_refspec_match()` from `remote.c` to `refspec.c`.
+ Move the functions `refspec_find_match()`, `refspec_find_all_matches()`
+ and `refspec_find_negative_match()` from `remote.c` to `refspec.c`.
These functions focus on matching refspecs, so centralizing them in
`refspec.c` improves code organization by keeping refspec-related logic
in one place.
@@ refspec.c: int refname_matches_negative_refspec_item(const char *refname, struct
return 0;
}
+
-+static int find_negative_refspec_match(struct refspec *rs, struct refspec_item *query)
++static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
+{
+ int i, matched_negative = 0;
+ int find_src = !query->src;
@@ refspec.c: int refname_matches_negative_refspec_item(const char *refname, struct
+ return matched_negative;
+}
+
-+void find_all_refspec_matches(struct refspec *rs,
++void refspec_find_all_matches(struct refspec *rs,
+ struct refspec_item *query,
+ struct string_list *results)
+{
@@ refspec.c: int refname_matches_negative_refspec_item(const char *refname, struct
+ int find_src = !query->src;
+
+ if (find_src && !query->dst)
-+ BUG("find_all_refspec_matches: need either src or dst");
++ BUG("refspec_find_all_matches: need either src or dst");
+
-+ if (find_negative_refspec_match(rs, query))
++ if (refspec_find_negative_match(rs, query))
+ return;
+
+ for (i = 0; i < rs->nr; i++) {
@@ refspec.c: int refname_matches_negative_refspec_item(const char *refname, struct
+ }
+}
+
-+int find_refspec_match(struct refspec *rs, struct refspec_item *query)
++int refspec_find_match(struct refspec *rs, struct refspec_item *query)
+{
+ int i;
+ int find_src = !query->src;
@@ refspec.c: int refname_matches_negative_refspec_item(const char *refname, struct
+ char **result = find_src ? &query->src : &query->dst;
+
+ if (find_src && !query->dst)
-+ BUG("find_refspec_match: need either src or dst");
++ BUG("refspec_find_match: need either src or dst");
+
-+ if (find_negative_refspec_match(rs, query))
++ if (refspec_find_negative_match(rs, query))
+ return -1;
+
+ for (i = 0; i < rs->nr; i++) {
@@ refspec.h: int refname_matches_negative_refspec_item(const char *refname, struct
+ * Queries a refspec for a match and updates the query item.
+ * Returns 0 on success, -1 if no match is found or negative refspec matches.
+ */
-+int find_refspec_match(struct refspec *rs, struct refspec_item *query);
++int refspec_find_match(struct refspec *rs, struct refspec_item *query);
+
+/*
+ * Queries a refspec for all matches and appends results to the provided string
+ * list.
+ */
-+void find_all_refspec_matches(struct refspec *rs,
++void refspec_find_all_matches(struct refspec *rs,
+ struct refspec_item *query,
+ struct string_list *results);
+
@@ remote.c: struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspe
return ref_map;
}
--static int find_negative_refspec_match(struct refspec *rs, struct refspec_item *query)
+-static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
-{
- int i, matched_negative = 0;
- int find_src = !query->src;
@@ remote.c: struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspe
- return matched_negative;
-}
-
--static void find_all_refspec_matches(struct refspec *rs,
+-static void refspec_find_all_matches(struct refspec *rs,
- struct refspec_item *query,
- struct string_list *results)
-{
@@ remote.c: struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspe
- int find_src = !query->src;
-
- if (find_src && !query->dst)
-- BUG("find_all_refspec_matches: need either src or dst");
+- BUG("refspec_find_all_matches: need either src or dst");
-
-- if (find_negative_refspec_match(rs, query))
+- if (refspec_find_negative_match(rs, query))
- return;
-
- for (i = 0; i < rs->nr; i++) {
@@ remote.c: struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspe
- }
-}
-
--int find_refspec_match(struct refspec *rs, struct refspec_item *query)
+-int refspec_find_match(struct refspec *rs, struct refspec_item *query)
-{
- int i;
- int find_src = !query->src;
@@ remote.c: struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspe
- char **result = find_src ? &query->src : &query->dst;
-
- if (find_src && !query->dst)
-- BUG("find_refspec_match: need either src or dst");
+- BUG("refspec_find_match: need either src or dst");
-
-- if (find_negative_refspec_match(rs, query))
+- if (refspec_find_negative_match(rs, query))
- return -1;
-
- for (i = 0; i < rs->nr; i++) {
5: 891e01be93 ! 5: f13ac6f11f refspec: relocate apply_refspecs and related funtions
@@ refspec.c
#include "strbuf.h"
/*
-@@ refspec.c: int find_refspec_match(struct refspec *rs, struct refspec_item *query)
+@@ refspec.c: int refspec_find_match(struct refspec *rs, struct refspec_item *query)
}
return -1;
}
@@ refspec.c: int find_refspec_match(struct refspec *rs, struct refspec_item *query
+ memset(&query, 0, sizeof(struct refspec_item));
+ query.src = (char *)name;
+
-+ if (find_refspec_match(rs, &query))
++ if (refspec_find_match(rs, &query))
+ return NULL;
+
+ return query.dst;
+}
## refspec.h ##
-@@ refspec.h: void find_all_refspec_matches(struct refspec *rs,
+@@ refspec.h: void refspec_find_all_matches(struct refspec *rs,
struct refspec_item *query,
struct string_list *results);
@@ remote.c: void ref_push_report_free(struct ref_push_report *report)
- memset(&query, 0, sizeof(struct refspec_item));
- query.src = (char *)name;
-
-- if (find_refspec_match(rs, &query))
+- if (refspec_find_match(rs, &query))
- return NULL;
-
- return query.dst;
@@ remote.c: void ref_push_report_free(struct ref_push_report *report)
-
int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
{
- return find_refspec_match(&remote->fetch, refspec);
+ return refspec_find_match(&remote->fetch, refspec);
## remote.h ##
@@ remote.h: int resolve_remote_symref(struct ref *ref, struct ref *list);
@@ remote.h: int resolve_remote_symref(struct ref *ref, struct ref *list);
- */
-struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
-
--int find_refspec_match(struct refspec *rs, struct refspec_item *query);
+-int refspec_find_match(struct refspec *rs, struct refspec_item *query);
-char *apply_refspecs(struct refspec *rs, const char *name);
-
int check_push_refs(struct ref *src, struct refspec *rs);
--
2.34.1
^ permalink raw reply [flat|nested] 33+ messages in thread
* [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
@ 2025-02-04 4:05 ` Meet Soni
2025-02-04 9:00 ` Karthik Nayak
2025-02-04 4:05 ` [GSoC][PATCH v4 2/5] refspec: relocate refname_matches_negative_refspec_item Meet Soni
` (4 subsequent siblings)
5 siblings, 1 reply; 33+ messages in thread
From: Meet Soni @ 2025-02-04 4:05 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Jacob Keller, Junio C Hamano,
Pavel Rappo, Jacob Keller, Jeff King, Patrick Steinhardt,
Matthew Rogers
Rename the function `omit_name_by_refspec()` to
`refname_matches_negative_refspec_item()` to provide clearer intent.
The previous function name was vague and did not accurately describe its
purpose. By using `refname_matches_negative_refspec_item`, make the
function's purpose more intuitive, clarifying that it checks if a
reference name matches any negative refspec.
Rename function parameters for consistency with existing naming
conventions. Use `refname` instead of `name` to align with terminology
in `refs.h`.
Remove the redundant doc comment since the function name is now
self-explanatory.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
builtin/remote.c | 2 +-
remote.c | 8 ++++----
remote.h | 6 +-----
3 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 0435963286..258b8895cd 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -383,7 +383,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
states->remote->fetch.items[i].raw);
for (ref = fetch_map; ref; ref = ref->next) {
- if (omit_name_by_refspec(ref->name, &states->remote->fetch))
+ if (refname_matches_negative_refspec_item(ref->name, &states->remote->fetch))
string_list_append(&states->skipped, abbrev_branch(ref->name));
else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name))
string_list_append(&states->new_refs, abbrev_branch(ref->name));
diff --git a/remote.c b/remote.c
index 0f6fba8562..cb70ce6f3b 100644
--- a/remote.c
+++ b/remote.c
@@ -944,12 +944,12 @@ static int refspec_match(const struct refspec_item *refspec,
return !strcmp(refspec->src, name);
}
-int omit_name_by_refspec(const char *name, struct refspec *rs)
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
{
int i;
for (i = 0; i < rs->nr; i++) {
- if (rs->items[i].negative && refspec_match(&rs->items[i], name))
+ if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
return 1;
}
return 0;
@@ -962,7 +962,7 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
for (tail = &ref_map; *tail; ) {
struct ref *ref = *tail;
- if (omit_name_by_refspec(ref->name, rs)) {
+ if (refname_matches_negative_refspec_item(ref->name, rs)) {
*tail = ref->next;
free(ref->peer_ref);
free(ref);
@@ -1021,7 +1021,7 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
}
for (i = 0; !matched_negative && i < reversed.nr; i++) {
- if (omit_name_by_refspec(reversed.items[i].string, rs))
+ if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
matched_negative = 1;
}
diff --git a/remote.h b/remote.h
index bda10dd5c8..66ee53411d 100644
--- a/remote.h
+++ b/remote.h
@@ -261,11 +261,7 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
*/
struct ref *ref_remove_duplicates(struct ref *ref_map);
-/*
- * Check whether a name matches any negative refspec in rs. Returns 1 if the
- * name matches at least one negative refspec, and 0 otherwise.
- */
-int omit_name_by_refspec(const char *name, struct refspec *rs);
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
/*
* Remove all entries in the input list which match any negative refspec in
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* Re: [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec
2025-02-04 4:05 ` [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec Meet Soni
@ 2025-02-04 9:00 ` Karthik Nayak
2025-02-04 13:58 ` Meet Soni
0 siblings, 1 reply; 33+ messages in thread
From: Karthik Nayak @ 2025-02-04 9:00 UTC (permalink / raw)
To: Meet Soni, git
Cc: shubham.kanodia10, Jacob Keller, Junio C Hamano, Pavel Rappo,
Jacob Keller, Jeff King, Patrick Steinhardt, Matthew Rogers
[-- Attachment #1: Type: text/plain, Size: 887 bytes --]
Meet Soni <meetsoni3017@gmail.com> writes:
> diff --git a/remote.h b/remote.h
> index bda10dd5c8..66ee53411d 100644
> --- a/remote.h
> +++ b/remote.h
> @@ -261,11 +261,7 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
> */
> struct ref *ref_remove_duplicates(struct ref *ref_map);
>
> -/*
> - * Check whether a name matches any negative refspec in rs. Returns 1 if the
> - * name matches at least one negative refspec, and 0 otherwise.
> - */
> -int omit_name_by_refspec(const char *name, struct refspec *rs);
> +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
>
Nit: The first sentence is now duplicated by the function name as
mentioned in the commit message. But aren't we loosing information by
removing the second sentence?
> /*
> * Remove all entries in the input list which match any negative refspec in
> --
> 2.34.1
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec
2025-02-04 9:00 ` Karthik Nayak
@ 2025-02-04 13:58 ` Meet Soni
2025-02-06 10:13 ` Karthik Nayak
0 siblings, 1 reply; 33+ messages in thread
From: Meet Soni @ 2025-02-04 13:58 UTC (permalink / raw)
To: Karthik Nayak
Cc: git, shubham.kanodia10, Jacob Keller, Junio C Hamano, Pavel Rappo,
Jacob Keller, Jeff King, Patrick Steinhardt, Matthew Rogers
On Tue, 4 Feb 2025 at 14:30, Karthik Nayak <karthik.188@gmail.com> wrote:
>
> Meet Soni <meetsoni3017@gmail.com> writes:
>
> > diff --git a/remote.h b/remote.h
> > index bda10dd5c8..66ee53411d 100644
> > --- a/remote.h
> > +++ b/remote.h
> > @@ -261,11 +261,7 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
> > */
> > struct ref *ref_remove_duplicates(struct ref *ref_map);
> >
> > -/*
> > - * Check whether a name matches any negative refspec in rs. Returns 1 if the
> > - * name matches at least one negative refspec, and 0 otherwise.
> > - */
> > -int omit_name_by_refspec(const char *name, struct refspec *rs);
> > +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
> >
>
> Nit: The first sentence is now duplicated by the function name as
> mentioned in the commit message. But aren't we loosing information by
> removing the second sentence?
>
Correct. I considered keeping the second sentence for clarity, but that other
function signatures in the codebase don’t include comments solely describing
return values. To maintain consistency with the existing style, I
opted to remove
it. Let me know if you think an alternative approach would be better!
> > /*
> > * Remove all entries in the input list which match any negative refspec in
> > --
> > 2.34.1
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec
2025-02-04 13:58 ` Meet Soni
@ 2025-02-06 10:13 ` Karthik Nayak
0 siblings, 0 replies; 33+ messages in thread
From: Karthik Nayak @ 2025-02-06 10:13 UTC (permalink / raw)
To: Meet Soni
Cc: git, shubham.kanodia10, Jacob Keller, Junio C Hamano, Pavel Rappo,
Jacob Keller, Jeff King, Patrick Steinhardt, Matthew Rogers
[-- Attachment #1: Type: text/plain, Size: 1478 bytes --]
Meet Soni <meetsoni3017@gmail.com> writes:
> On Tue, 4 Feb 2025 at 14:30, Karthik Nayak <karthik.188@gmail.com> wrote:
>>
>> Meet Soni <meetsoni3017@gmail.com> writes:
>>
>> > diff --git a/remote.h b/remote.h
>> > index bda10dd5c8..66ee53411d 100644
>> > --- a/remote.h
>> > +++ b/remote.h
>> > @@ -261,11 +261,7 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
>> > */
>> > struct ref *ref_remove_duplicates(struct ref *ref_map);
>> >
>> > -/*
>> > - * Check whether a name matches any negative refspec in rs. Returns 1 if the
>> > - * name matches at least one negative refspec, and 0 otherwise.
>> > - */
>> > -int omit_name_by_refspec(const char *name, struct refspec *rs);
>> > +int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
>> >
>>
>> Nit: The first sentence is now duplicated by the function name as
>> mentioned in the commit message. But aren't we loosing information by
>> removing the second sentence?
>>
> Correct. I considered keeping the second sentence for clarity, but that other
> function signatures in the codebase don’t include comments solely describing
> return values. To maintain consistency with the existing style, I
> opted to remove
> it. Let me know if you think an alternative approach would be better!
I think its okay as-is for now :)
>> > /*
>> > * Remove all entries in the input list which match any negative refspec in
>> > --
>> > 2.34.1
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]
^ permalink raw reply [flat|nested] 33+ messages in thread
* [GSoC][PATCH v4 2/5] refspec: relocate refname_matches_negative_refspec_item
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec Meet Soni
@ 2025-02-04 4:05 ` Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 3/5] remote: rename query_refspecs functions Meet Soni
` (3 subsequent siblings)
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-04 4:05 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Junio C Hamano, Jeff King,
Jacob Keller, Matthew Rogers, Patrick Steinhardt
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 <meetsoni3017@gmail.com>
---
refspec.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
refspec.h | 9 +++++++++
remote.c | 48 ------------------------------------------------
3 files changed, 57 insertions(+), 48 deletions(-)
diff --git a/refspec.c b/refspec.c
index 6d86e04442..b447768304 100644
--- a/refspec.c
+++ b/refspec.c
@@ -276,3 +276,51 @@ void refspec_ref_prefixes(const struct refspec *rs,
}
}
}
+
+int match_name_with_pattern(const char *key, const char *name,
+ const char *value, char **result)
+{
+ const char *kstar = strchr(key, '*');
+ size_t klen;
+ size_t ksuffixlen;
+ size_t namelen;
+ int ret;
+ if (!kstar)
+ die(_("key '%s' of pattern had no '*'"), key);
+ klen = kstar - key;
+ ksuffixlen = strlen(kstar + 1);
+ namelen = strlen(name);
+ ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
+ !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
+ if (ret && value) {
+ struct strbuf sb = STRBUF_INIT;
+ const char *vstar = strchr(value, '*');
+ if (!vstar)
+ die(_("value '%s' of pattern has no '*'"), value);
+ strbuf_add(&sb, value, vstar - value);
+ strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
+ strbuf_addstr(&sb, vstar + 1);
+ *result = strbuf_detach(&sb, NULL);
+ }
+ return ret;
+}
+
+static int refspec_match(const struct refspec_item *refspec,
+ const char *name)
+{
+ if (refspec->pattern)
+ return match_name_with_pattern(refspec->src, name, NULL, NULL);
+
+ return !strcmp(refspec->src, name);
+}
+
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
+{
+ int i;
+
+ for (i = 0; i < rs->nr; i++) {
+ if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
+ return 1;
+ }
+ return 0;
+}
diff --git a/refspec.h b/refspec.h
index 69d693c87d..584d9c9eb5 100644
--- a/refspec.h
+++ b/refspec.h
@@ -71,4 +71,13 @@ struct strvec;
void refspec_ref_prefixes(const struct refspec *rs,
struct strvec *ref_prefixes);
+int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
+
+/*
+ * Checks whether a name matches a pattern and optionally generates a result.
+ * Returns 1 if the name matches the pattern, 0 otherwise.
+ */
+int match_name_with_pattern(const char *key, const char *name,
+ const char *value, char **result);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index cb70ce6f3b..1da8ec7037 100644
--- a/remote.c
+++ b/remote.c
@@ -907,54 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
}
}
-static int match_name_with_pattern(const char *key, const char *name,
- const char *value, char **result)
-{
- const char *kstar = strchr(key, '*');
- size_t klen;
- size_t ksuffixlen;
- size_t namelen;
- int ret;
- if (!kstar)
- die(_("key '%s' of pattern had no '*'"), key);
- klen = kstar - key;
- ksuffixlen = strlen(kstar + 1);
- namelen = strlen(name);
- ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
- !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
- if (ret && value) {
- struct strbuf sb = STRBUF_INIT;
- const char *vstar = strchr(value, '*');
- if (!vstar)
- die(_("value '%s' of pattern has no '*'"), value);
- strbuf_add(&sb, value, vstar - value);
- strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
- strbuf_addstr(&sb, vstar + 1);
- *result = strbuf_detach(&sb, NULL);
- }
- return ret;
-}
-
-static int refspec_match(const struct refspec_item *refspec,
- const char *name)
-{
- if (refspec->pattern)
- return match_name_with_pattern(refspec->src, name, NULL, NULL);
-
- return !strcmp(refspec->src, name);
-}
-
-int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs)
-{
- int i;
-
- for (i = 0; i < rs->nr; i++) {
- if (rs->items[i].negative && refspec_match(&rs->items[i], refname))
- return 1;
- }
- return 0;
-}
-
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
{
struct ref **tail;
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [GSoC][PATCH v4 3/5] remote: rename query_refspecs functions
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 1/5] remote: rename function omit_name_by_refspec Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 2/5] refspec: relocate refname_matches_negative_refspec_item Meet Soni
@ 2025-02-04 4:05 ` Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 4/5] refspec: relocate matching related functions Meet Soni
` (2 subsequent siblings)
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-04 4:05 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Patrick Steinhardt, Denton Liu,
Junio C Hamano, Jacob Keller, René Scharfe, Matthew Rogers
Rename functions related to handling refspecs in preparation for their
move from `remote.c` to `refspec.c`. Update their names to better
reflect their intent:
- `query_refspecs()` -> `refspec_find_match()` for clarity, as it
finds a single matching refspec.
- `query_refspecs_multiple()` -> `refspec_find_all_matches()` to
better reflect that it collects all matching refspecs instead of
returning just the first match.
- `query_matches_negative_refspec()` ->
`refspec_find_negative_match()` for consistency with the
updated naming convention, even though this static function
didn't strictly require renaming.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
builtin/push.c | 2 +-
remote.c | 20 ++++++++++----------
remote.h | 2 +-
3 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/builtin/push.c b/builtin/push.c
index 90de3746b5..92d530e5c4 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -78,7 +78,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
.src = matched->name,
};
- if (!query_refspecs(&remote->push, &query) && query.dst) {
+ if (!refspec_find_match(&remote->push, &query) && query.dst) {
refspec_appendf(refspec, "%s%s:%s",
query.force ? "+" : "",
query.src, query.dst);
diff --git a/remote.c b/remote.c
index 1da8ec7037..b510809a56 100644
--- a/remote.c
+++ b/remote.c
@@ -925,7 +925,7 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
return ref_map;
}
-static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query)
+static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
{
int i, matched_negative = 0;
int find_src = !query->src;
@@ -982,7 +982,7 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
return matched_negative;
}
-static void query_refspecs_multiple(struct refspec *rs,
+static void refspec_find_all_matches(struct refspec *rs,
struct refspec_item *query,
struct string_list *results)
{
@@ -990,9 +990,9 @@ static void query_refspecs_multiple(struct refspec *rs,
int find_src = !query->src;
if (find_src && !query->dst)
- BUG("query_refspecs_multiple: need either src or dst");
+ BUG("refspec_find_all_matches: need either src or dst");
- if (query_matches_negative_refspec(rs, query))
+ if (refspec_find_negative_match(rs, query))
return;
for (i = 0; i < rs->nr; i++) {
@@ -1013,7 +1013,7 @@ static void query_refspecs_multiple(struct refspec *rs,
}
}
-int query_refspecs(struct refspec *rs, struct refspec_item *query)
+int refspec_find_match(struct refspec *rs, struct refspec_item *query)
{
int i;
int find_src = !query->src;
@@ -1021,9 +1021,9 @@ int query_refspecs(struct refspec *rs, struct refspec_item *query)
char **result = find_src ? &query->src : &query->dst;
if (find_src && !query->dst)
- BUG("query_refspecs: need either src or dst");
+ BUG("refspec_find_match: need either src or dst");
- if (query_matches_negative_refspec(rs, query))
+ if (refspec_find_negative_match(rs, query))
return -1;
for (i = 0; i < rs->nr; i++) {
@@ -1054,7 +1054,7 @@ char *apply_refspecs(struct refspec *rs, const char *name)
memset(&query, 0, sizeof(struct refspec_item));
query.src = (char *)name;
- if (query_refspecs(rs, &query))
+ if (refspec_find_match(rs, &query))
return NULL;
return query.dst;
@@ -1062,7 +1062,7 @@ char *apply_refspecs(struct refspec *rs, const char *name)
int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
{
- return query_refspecs(&remote->fetch, refspec);
+ return refspec_find_match(&remote->fetch, refspec);
}
static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
@@ -2487,7 +2487,7 @@ static int get_stale_heads_cb(const char *refname, const char *referent UNUSED,
memset(&query, 0, sizeof(struct refspec_item));
query.dst = (char *)refname;
- query_refspecs_multiple(info->rs, &query, &matches);
+ refspec_find_all_matches(info->rs, &query, &matches);
if (matches.nr == 0)
goto clean_exit; /* No matches */
diff --git a/remote.h b/remote.h
index 66ee53411d..516ba7f398 100644
--- a/remote.h
+++ b/remote.h
@@ -269,7 +269,7 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
*/
struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
-int query_refspecs(struct refspec *rs, struct refspec_item *query);
+int refspec_find_match(struct refspec *rs, struct refspec_item *query);
char *apply_refspecs(struct refspec *rs, const char *name);
int check_push_refs(struct ref *src, struct refspec *rs);
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [GSoC][PATCH v4 4/5] refspec: relocate matching related functions
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
` (2 preceding siblings ...)
2025-02-04 4:05 ` [GSoC][PATCH v4 3/5] remote: rename query_refspecs functions Meet Soni
@ 2025-02-04 4:05 ` Meet Soni
2025-02-04 4:05 ` [GSoC][PATCH v4 5/5] refspec: relocate apply_refspecs and related funtions Meet Soni
2025-02-04 7:16 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Patrick Steinhardt
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-04 4:05 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Elijah Newren, Junio C Hamano,
Jeff King, Nipunn Koorapati
Move the functions `refspec_find_match()`, `refspec_find_all_matches()`
and `refspec_find_negative_match()` from `remote.c` to `refspec.c`.
These functions focus on matching refspecs, so centralizing them in
`refspec.c` improves code organization by keeping refspec-related logic
in one place.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
refspec.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
refspec.h | 16 +++++++
remote.c | 122 -----------------------------------------------------
3 files changed, 139 insertions(+), 122 deletions(-)
diff --git a/refspec.c b/refspec.c
index b447768304..cab0b0d127 100644
--- a/refspec.c
+++ b/refspec.c
@@ -5,6 +5,7 @@
#include "gettext.h"
#include "hash.h"
#include "hex.h"
+#include "string-list.h"
#include "strvec.h"
#include "refs.h"
#include "refspec.h"
@@ -324,3 +325,125 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
}
return 0;
}
+
+static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
+{
+ int i, matched_negative = 0;
+ int find_src = !query->src;
+ struct string_list reversed = STRING_LIST_INIT_DUP;
+ const char *needle = find_src ? query->dst : query->src;
+
+ /*
+ * Check whether the queried ref matches any negative refpsec. If so,
+ * then we should ultimately treat this as not matching the query at
+ * all.
+ *
+ * Note that negative refspecs always match the source, but the query
+ * item uses the destination. To handle this, we apply pattern
+ * refspecs in reverse to figure out if the query source matches any
+ * of the negative refspecs.
+ *
+ * The first loop finds and expands all positive refspecs
+ * matched by the queried ref.
+ *
+ * The second loop checks if any of the results of the first loop
+ * match any negative refspec.
+ */
+ for (i = 0; i < rs->nr; i++) {
+ struct refspec_item *refspec = &rs->items[i];
+ char *expn_name;
+
+ if (refspec->negative)
+ continue;
+
+ /* Note the reversal of src and dst */
+ if (refspec->pattern) {
+ const char *key = refspec->dst ? refspec->dst : refspec->src;
+ const char *value = refspec->src;
+
+ if (match_name_with_pattern(key, needle, value, &expn_name))
+ string_list_append_nodup(&reversed, expn_name);
+ } else if (refspec->matching) {
+ /* For the special matching refspec, any query should match */
+ string_list_append(&reversed, needle);
+ } else if (!refspec->src) {
+ BUG("refspec->src should not be null here");
+ } else if (!strcmp(needle, refspec->src)) {
+ string_list_append(&reversed, refspec->src);
+ }
+ }
+
+ for (i = 0; !matched_negative && i < reversed.nr; i++) {
+ if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
+ matched_negative = 1;
+ }
+
+ string_list_clear(&reversed, 0);
+
+ return matched_negative;
+}
+
+void refspec_find_all_matches(struct refspec *rs,
+ struct refspec_item *query,
+ struct string_list *results)
+{
+ int i;
+ int find_src = !query->src;
+
+ if (find_src && !query->dst)
+ BUG("refspec_find_all_matches: need either src or dst");
+
+ if (refspec_find_negative_match(rs, query))
+ return;
+
+ for (i = 0; i < rs->nr; i++) {
+ struct refspec_item *refspec = &rs->items[i];
+ const char *key = find_src ? refspec->dst : refspec->src;
+ const char *value = find_src ? refspec->src : refspec->dst;
+ const char *needle = find_src ? query->dst : query->src;
+ char **result = find_src ? &query->src : &query->dst;
+
+ if (!refspec->dst || refspec->negative)
+ continue;
+ if (refspec->pattern) {
+ if (match_name_with_pattern(key, needle, value, result))
+ string_list_append_nodup(results, *result);
+ } else if (!strcmp(needle, key)) {
+ string_list_append(results, value);
+ }
+ }
+}
+
+int refspec_find_match(struct refspec *rs, struct refspec_item *query)
+{
+ int i;
+ int find_src = !query->src;
+ const char *needle = find_src ? query->dst : query->src;
+ char **result = find_src ? &query->src : &query->dst;
+
+ if (find_src && !query->dst)
+ BUG("refspec_find_match: need either src or dst");
+
+ if (refspec_find_negative_match(rs, query))
+ return -1;
+
+ for (i = 0; i < rs->nr; i++) {
+ struct refspec_item *refspec = &rs->items[i];
+ const char *key = find_src ? refspec->dst : refspec->src;
+ const char *value = find_src ? refspec->src : refspec->dst;
+
+ if (!refspec->dst || refspec->negative)
+ continue;
+ if (refspec->pattern) {
+ if (match_name_with_pattern(key, needle, value, result)) {
+ query->force = refspec->force;
+ return 0;
+ }
+ } else if (!strcmp(needle, key)) {
+ *result = xstrdup(value);
+ query->force = refspec->force;
+ return 0;
+ }
+ }
+ return -1;
+}
diff --git a/refspec.h b/refspec.h
index 584d9c9eb5..be20ba53ab 100644
--- a/refspec.h
+++ b/refspec.h
@@ -30,6 +30,8 @@ struct refspec_item {
char *raw;
};
+struct string_list;
+
#define REFSPEC_FETCH 1
#define REFSPEC_PUSH 0
@@ -80,4 +82,18 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r
int match_name_with_pattern(const char *key, const char *name,
const char *value, char **result);
+/*
+ * Queries a refspec for a match and updates the query item.
+ * Returns 0 on success, -1 if no match is found or negative refspec matches.
+ */
+int refspec_find_match(struct refspec *rs, struct refspec_item *query);
+
+/*
+ * Queries a refspec for all matches and appends results to the provided string
+ * list.
+ */
+void refspec_find_all_matches(struct refspec *rs,
+ struct refspec_item *query,
+ struct string_list *results);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index b510809a56..4c5940482f 100644
--- a/remote.c
+++ b/remote.c
@@ -925,128 +925,6 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
return ref_map;
}
-static int refspec_find_negative_match(struct refspec *rs, struct refspec_item *query)
-{
- int i, matched_negative = 0;
- int find_src = !query->src;
- struct string_list reversed = STRING_LIST_INIT_DUP;
- const char *needle = find_src ? query->dst : query->src;
-
- /*
- * Check whether the queried ref matches any negative refpsec. If so,
- * then we should ultimately treat this as not matching the query at
- * all.
- *
- * Note that negative refspecs always match the source, but the query
- * item uses the destination. To handle this, we apply pattern
- * refspecs in reverse to figure out if the query source matches any
- * of the negative refspecs.
- *
- * The first loop finds and expands all positive refspecs
- * matched by the queried ref.
- *
- * The second loop checks if any of the results of the first loop
- * match any negative refspec.
- */
- for (i = 0; i < rs->nr; i++) {
- struct refspec_item *refspec = &rs->items[i];
- char *expn_name;
-
- if (refspec->negative)
- continue;
-
- /* Note the reversal of src and dst */
- if (refspec->pattern) {
- const char *key = refspec->dst ? refspec->dst : refspec->src;
- const char *value = refspec->src;
-
- if (match_name_with_pattern(key, needle, value, &expn_name))
- string_list_append_nodup(&reversed, expn_name);
- } else if (refspec->matching) {
- /* For the special matching refspec, any query should match */
- string_list_append(&reversed, needle);
- } else if (!refspec->src) {
- BUG("refspec->src should not be null here");
- } else if (!strcmp(needle, refspec->src)) {
- string_list_append(&reversed, refspec->src);
- }
- }
-
- for (i = 0; !matched_negative && i < reversed.nr; i++) {
- if (refname_matches_negative_refspec_item(reversed.items[i].string, rs))
- matched_negative = 1;
- }
-
- string_list_clear(&reversed, 0);
-
- return matched_negative;
-}
-
-static void refspec_find_all_matches(struct refspec *rs,
- struct refspec_item *query,
- struct string_list *results)
-{
- int i;
- int find_src = !query->src;
-
- if (find_src && !query->dst)
- BUG("refspec_find_all_matches: need either src or dst");
-
- if (refspec_find_negative_match(rs, query))
- return;
-
- for (i = 0; i < rs->nr; i++) {
- struct refspec_item *refspec = &rs->items[i];
- const char *key = find_src ? refspec->dst : refspec->src;
- const char *value = find_src ? refspec->src : refspec->dst;
- const char *needle = find_src ? query->dst : query->src;
- char **result = find_src ? &query->src : &query->dst;
-
- if (!refspec->dst || refspec->negative)
- continue;
- if (refspec->pattern) {
- if (match_name_with_pattern(key, needle, value, result))
- string_list_append_nodup(results, *result);
- } else if (!strcmp(needle, key)) {
- string_list_append(results, value);
- }
- }
-}
-
-int refspec_find_match(struct refspec *rs, struct refspec_item *query)
-{
- int i;
- int find_src = !query->src;
- const char *needle = find_src ? query->dst : query->src;
- char **result = find_src ? &query->src : &query->dst;
-
- if (find_src && !query->dst)
- BUG("refspec_find_match: need either src or dst");
-
- if (refspec_find_negative_match(rs, query))
- return -1;
-
- for (i = 0; i < rs->nr; i++) {
- struct refspec_item *refspec = &rs->items[i];
- const char *key = find_src ? refspec->dst : refspec->src;
- const char *value = find_src ? refspec->src : refspec->dst;
-
- if (!refspec->dst || refspec->negative)
- continue;
- if (refspec->pattern) {
- if (match_name_with_pattern(key, needle, value, result)) {
- query->force = refspec->force;
- return 0;
- }
- } else if (!strcmp(needle, key)) {
- *result = xstrdup(value);
- query->force = refspec->force;
- return 0;
- }
- }
- return -1;
-}
-
char *apply_refspecs(struct refspec *rs, const char *name)
{
struct refspec_item query;
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [GSoC][PATCH v4 5/5] refspec: relocate apply_refspecs and related funtions
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
` (3 preceding siblings ...)
2025-02-04 4:05 ` [GSoC][PATCH v4 4/5] refspec: relocate matching related functions Meet Soni
@ 2025-02-04 4:05 ` Meet Soni
2025-02-04 7:16 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Patrick Steinhardt
5 siblings, 0 replies; 33+ messages in thread
From: Meet Soni @ 2025-02-04 4:05 UTC (permalink / raw)
To: git
Cc: shubham.kanodia10, Meet Soni, Jacob Keller, Junio C Hamano,
Jeff King, Pavel Rappo, Jacob Keller, Elijah Newren,
Matthew Rogers, Patrick Steinhardt
Move the functions `apply_refspecs()` and `apply_negative_refspecs()`
from `remote.c` to `refspec.c`. These functions focus on applying
refspecs, so centralizing them in `refspec.c` improves code organization
by keeping refspec-related logic in one place.
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
---
refspec.c | 32 ++++++++++++++++++++++++++++++++
refspec.h | 12 ++++++++++++
remote.c | 31 -------------------------------
remote.h | 11 -----------
4 files changed, 44 insertions(+), 42 deletions(-)
diff --git a/refspec.c b/refspec.c
index cab0b0d127..0dbbd1e799 100644
--- a/refspec.c
+++ b/refspec.c
@@ -9,6 +9,7 @@
#include "strvec.h"
#include "refs.h"
#include "refspec.h"
+#include "remote.h"
#include "strbuf.h"
/*
@@ -447,3 +448,34 @@ int refspec_find_match(struct refspec *rs, struct refspec_item *query)
}
return -1;
}
+
+struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
+{
+ struct ref **tail;
+
+ for (tail = &ref_map; *tail; ) {
+ struct ref *ref = *tail;
+
+ if (refname_matches_negative_refspec_item(ref->name, rs)) {
+ *tail = ref->next;
+ free(ref->peer_ref);
+ free(ref);
+ } else
+ tail = &ref->next;
+ }
+
+ return ref_map;
+}
+
+char *apply_refspecs(struct refspec *rs, const char *name)
+{
+ struct refspec_item query;
+
+ memset(&query, 0, sizeof(struct refspec_item));
+ query.src = (char *)name;
+
+ if (refspec_find_match(rs, &query))
+ return NULL;
+
+ return query.dst;
+}
diff --git a/refspec.h b/refspec.h
index be20ba53ab..2a28d043be 100644
--- a/refspec.h
+++ b/refspec.h
@@ -96,4 +96,16 @@ void refspec_find_all_matches(struct refspec *rs,
struct refspec_item *query,
struct string_list *results);
+/*
+ * Remove all entries in the input list which match any negative refspec in
+ * the refspec list.
+ */
+struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
+
+/*
+ * Search for a refspec that matches the given name and return the
+ * corresponding destination (dst) if a match is found, NULL otherwise.
+ */
+char *apply_refspecs(struct refspec *rs, const char *name);
+
#endif /* REFSPEC_H */
diff --git a/remote.c b/remote.c
index 4c5940482f..7f27c59a5b 100644
--- a/remote.c
+++ b/remote.c
@@ -907,37 +907,6 @@ void ref_push_report_free(struct ref_push_report *report)
}
}
-struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs)
-{
- struct ref **tail;
-
- for (tail = &ref_map; *tail; ) {
- struct ref *ref = *tail;
-
- if (refname_matches_negative_refspec_item(ref->name, rs)) {
- *tail = ref->next;
- free(ref->peer_ref);
- free(ref);
- } else
- tail = &ref->next;
- }
-
- return ref_map;
-}
-
-char *apply_refspecs(struct refspec *rs, const char *name)
-{
- struct refspec_item query;
-
- memset(&query, 0, sizeof(struct refspec_item));
- query.src = (char *)name;
-
- if (refspec_find_match(rs, &query))
- return NULL;
-
- return query.dst;
-}
-
int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
{
return refspec_find_match(&remote->fetch, refspec);
diff --git a/remote.h b/remote.h
index 516ba7f398..b4bb16af0e 100644
--- a/remote.h
+++ b/remote.h
@@ -261,17 +261,6 @@ int resolve_remote_symref(struct ref *ref, struct ref *list);
*/
struct ref *ref_remove_duplicates(struct ref *ref_map);
-int refname_matches_negative_refspec_item(const char *refname, struct refspec *rs);
-
-/*
- * Remove all entries in the input list which match any negative refspec in
- * the refspec list.
- */
-struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs);
-
-int refspec_find_match(struct refspec *rs, struct refspec_item *query);
-char *apply_refspecs(struct refspec *rs, const char *name);
-
int check_push_refs(struct ref *src, struct refspec *rs);
int match_push_refs(struct ref *src, struct ref **dst,
struct refspec *rs, int flags);
--
2.34.1
^ permalink raw reply related [flat|nested] 33+ messages in thread
* Re: [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic
2025-02-04 4:05 ` [GSoC][PATCH v4 0/5] refspec: centralize refspec-related logic Meet Soni
` (4 preceding siblings ...)
2025-02-04 4:05 ` [GSoC][PATCH v4 5/5] refspec: relocate apply_refspecs and related funtions Meet Soni
@ 2025-02-04 7:16 ` Patrick Steinhardt
5 siblings, 0 replies; 33+ messages in thread
From: Patrick Steinhardt @ 2025-02-04 7:16 UTC (permalink / raw)
To: Meet Soni; +Cc: git, shubham.kanodia10
On Tue, Feb 04, 2025 at 09:35:53AM +0530, Meet Soni wrote:
> Changes since v3:
> - updated commit message.
> - renamed functions as per review.
> - added GSoC mark , since the announcement has been made by google and
> we've started the discussion regarding the same.
Thanks, this version looks good to me!
Patrick
^ permalink raw reply [flat|nested] 33+ messages in thread