All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Sebastian Schuberth <sschuberth@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: [RFC] ident: support per-path configs by matching the path against a pattern
Date: Fri, 10 Jul 2015 12:46:52 -0400	[thread overview]
Message-ID: <20150710164652.GA30113@peff.net> (raw)
In-Reply-To: <20150710154308.GA29395@peff.net>

On Fri, Jul 10, 2015 at 11:43:08AM -0400, Jeff King wrote:

> But something like:
> 
>   [include "gitdir:bar"]
>   path = foo
> 
> could do so only when the "gitdir:bar" conditional is satisfied (where
> that is just a syntax I made up to mean fnmatch("bar", $GIT_DIR)). So
> like user.<pattern>.*, we still put our section-specific hack into one
> special section, but that one place is capable of chaining to multiple
> other config keys. :)

Here's a sketch if anybody is inclined to pick it up and run with it.
Note that I did not think too hard about little things like the
de-anchoring.

---
diff --git a/config.c b/config.c
index 29fa012..47b01f0 100644
--- a/config.c
+++ b/config.c
@@ -139,9 +139,45 @@ static int handle_path_include(const char *path, struct config_include_data *inc
 	return ret;
 }
 
+static int include_condition_is_true(const char *cond, int cond_len)
+{
+	const char *value;
+
+	/* no condition (i.e., "include.path") is always true */
+	if (!cond)
+		return 1;
+
+	/*
+	 * It's OK to run over cond_len in our checks here, as that just pushes
+	 * us past the final ".", which cannot match any of our prefixes.
+	 */
+	if (skip_prefix(cond, "gitdir:", &value)) {
+		struct strbuf text = STRBUF_INIT;
+		struct strbuf pattern = STRBUF_INIT;
+		int ret;
+
+		strbuf_add_absolute_path(&text, get_git_dir());
+
+		/* de-anchor match for convenience */
+		strbuf_addstr(&pattern, "**");
+		strbuf_add(&pattern, value, cond_len - (value - cond));
+		strbuf_addstr(&pattern, "**");
+
+		ret = !wildmatch(pattern.buf, text.buf, 0, NULL);
+		strbuf_release(&pattern);
+		strbuf_release(&text);
+		return ret;
+	}
+
+	/* unknown conditionals are always false */
+	return 0;
+}
+
 int git_config_include(const char *var, const char *value, void *data)
 {
 	struct config_include_data *inc = data;
+	const char *cond, *key;
+	int cond_len;
 	int ret;
 
 	/*
@@ -152,8 +188,12 @@ int git_config_include(const char *var, const char *value, void *data)
 	if (ret < 0)
 		return ret;
 
-	if (!strcmp(var, "include.path"))
-		ret = handle_path_include(value, inc);
+	if (!parse_config_key(var, "include", &cond, &cond_len, &key) &&
+	    include_condition_is_true(cond, cond_len)) {
+		if (!strcmp(key, "path"))
+			ret = handle_path_include(value, inc);
+		/* else we do not know about this type of include; ignore */
+	}
 	return ret;
 }
 

  reply	other threads:[~2015-07-10 16:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-10  9:36 [RFC] ident: support per-path configs by matching the path against a pattern Sebastian Schuberth
2015-07-10 15:10 ` Junio C Hamano
2015-07-10 15:43   ` Jeff King
2015-07-10 16:46     ` Jeff King [this message]
2015-07-10 20:23       ` Junio C Hamano
2015-07-10 20:58         ` Jeff King
2015-07-10 21:07           ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150710164652.GA30113@peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sschuberth@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.