Re: [PATCH v2] rev-parse --namespace

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ilari Liusvaara <ilari.liusvaara@xxxxxxxxxxx> writes:

> ..., if matching power would be extended,
> probably the easiest extension would be full-blown extended regular
> expressions.

As refs behave like a filesystem path and we try to use fnmatch() for
anything that behave like a filesystem path, that would break consistency.

But a patch to add fnmatch() shouldn't be too bad; something like this
(not even compile tested)?

 refs.c |   45 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/refs.c b/refs.c
index 5583f4b..0494c75 100644
--- a/refs.c
+++ b/refs.c
@@ -674,19 +674,48 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 	return do_for_each_ref("refs/replace/", fn, 13, 0, cb_data);
 }
 
+struct ref_glob_filter {
+	each_ref_fn *user_callback;
+	void *user_cb_data;
+	const char *pattern;
+	int pattern_len;
+};
+
+static int ref_glob_filter_cb(const char *refname,
+			      const unsigned char *sha1,
+			      int flags, void *cb_data)
+{
+	struct ref_glob_filter *cb = cb_data;
+
+	/*
+	 * Reject if it does not produce a prefix match and
+	 * it doesn't pass fnmatch().
+	 */
+	if (!(cb->pattern_len <= strlen(refname)
+	      && !memcmp(cb->pattern, refname, cb->pattern_len)) &&
+	    fnmatch(cb->pattern, refname, 0))
+		return 0;
+	return cb->user_callback(refname, sha1, flags, cb->user_cb_data);
+}
+
 int for_each_namespace_ref(each_ref_fn fn, const char *ns_name, void *cb_data)
 {
-	struct strbuf real_prefix = STRBUF_INIT;
+	struct ref_glob_filter filter;
+	struct strbuf pattern = STRBUF_INIT;
 	int ret;
 
 	if (prefixcmp(ns_name, "refs/"))
-		strbuf_addstr(&real_prefix, "refs/");
-	strbuf_addstr(&real_prefix, ns_name);
-	if (real_prefix.buf[real_prefix.len - 1] != '/')
-		strbuf_addch(&real_prefix, '/');
-
-	ret = for_each_ref_in(real_prefix.buf, fn, cb_data);
-	strbuf_release(&real_prefix);
+		strbuf_addstr(&pattern, "refs/");
+	strbuf_addstr(&pattern, ns_name);
+	if (pattern.buf[pattern.len - 1] != '/')
+		strbuf_addch(&pattern, '/');
+	filter.pattern = pattern.buf;
+	filter.pattern_len = pattern.len;
+	filter.user_callback = fn;
+	filter.user_cb_data = cb_data;
+
+	ret = for_each_ref(ref_glob_filter_cb, &filter);
+	strbuf_release(&pattern);
 	return ret;
 }
 
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]