git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin Waitz <tali@admingilde.org>
To: git@vger.kernel.org
Subject: [PATCH] Support environment variables in config file
Date: Mon, 4 Jun 2007 00:37:27 +0200	[thread overview]
Message-ID: <20070603223727.GA16637@admingilde.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 4641 bytes --]

Environment variables (e.g. $HOME) can be helpful for the GIT configuration.
With this change you can use them with the normal shell "$" syntax.
If you want to insert a plain "$" in a variable, it can be escaped as \$
or put inside quotes (").

Signed-off-by: Martin Waitz <tali@admingilde.org>
---

I found this particularly useful to be able to specify my own global
gitignore list via core.excludesfile.

 Documentation/config.txt |   23 +++++++++++++++--------
 config.c                 |   23 +++++++++++++++++++++--
 t/t1300-repo-config.sh   |   13 +++++++++++++
 3 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 5868d58..042a354 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -65,17 +65,24 @@ a string, an integer, or a boolean.  Boolean values may be given as yes/no,
 converting value to the canonical form using '--bool' type specifier;
 `git-config` will ensure that the output is "true" or "false".
 
+You can use the $VARIABLE shell syntax to use environment variables
+in the configuration file.  All alpha-numeric characters after the
+leading '`$`' will be interpreted as name of an environment variable
+and the value of this variable will be used instead.
+If the variable does not exist it will be treated as an empty string.
+
 String values may be entirely or partially enclosed in double quotes.
 You need to enclose variable value in double quotes if you want to
 preserve leading or trailing whitespace, or if variable value contains
-beginning of comment characters (if it contains '#' or ';').
-Double quote '`"`' and backslash '`\`' characters in variable value must
-be escaped: use '`\"`' for '`"`' and '`\\`' for '`\`'.
-
-The following escape sequences (beside '`\"`' and '`\\`') are recognized:
-'`\n`' for newline character (NL), '`\t`' for horizontal tabulation (HT, TAB)
-and '`\b`' for backspace (BS).  No other char escape sequence, nor octal
-char sequences are valid.
+beginning of comment characters (if it contains '#' or ';') or a dollar
+sign which would be interpreted as a variable.
+Double quote '`"`', backslash '`\`' and dollar '`$`' characters in variable
+value must be escaped: use '`\"`' for '`"`' and '`\\`' for '`\`'.
+
+The following escape sequences (beside '`\"`', '`\$"`' and '`\\`') are
+recognized: '`\n`' for newline character (NL), '`\t`' for horizontal tabulation
+(HT, TAB) and '`\b`' for backspace (BS).  No other char escape sequence, nor
+octal char sequences are valid.
 
 Variable value ending in a '`\`' is continued on the next line in the
 customary UNIX fashion.
diff --git a/config.c b/config.c
index 0614c2b..058c0df 100644
--- a/config.c
+++ b/config.c
@@ -43,12 +43,27 @@ static int get_next_char(void)
 static char *parse_value(void)
 {
 	static char value[1024];
-	int quote = 0, comment = 0, len = 0, space = 0;
+	int quote = 0, comment = 0, envvar = -1, len = 0, space = 0;
 
 	for (;;) {
 		int c = get_next_char();
 		if (len >= sizeof(value))
 			return NULL;
+		if ((envvar >= 0) && !isalnum(c)) {
+			const char *var;
+			value[len] = 0;
+			var = getenv(&value[envvar]);
+			if (var) {
+				strncpy(&value[envvar], var,
+				        sizeof(value) - envvar);
+				if (value[sizeof(value)-1])
+					return NULL;
+				len = envvar + strlen(&value[envvar]);
+			} else {
+				len = envvar;
+			}
+			envvar = -1;
+		}
 		if (c == '\n') {
 			if (quote)
 				return NULL;
@@ -66,6 +81,10 @@ static char *parse_value(void)
 				comment = 1;
 				continue;
 			}
+			if (c == '$') {
+				envvar = len;
+				continue;
+			}
 		}
 		if (space) {
 			if (len)
@@ -87,7 +106,7 @@ static char *parse_value(void)
 				c = '\n';
 				break;
 			/* Some characters escape as themselves */
-			case '\\': case '"':
+			case '\\': case '"': case '$':
 				break;
 			/* Reject unknown escape sequences */
 			default:
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 3f3fd2d..eb84437 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -513,5 +513,18 @@ git config --list > result
 
 test_expect_success 'value continued on next line' 'cmp result expect'
 
+cat > .git/config <<\EOF
+[quoted]
+	withvar = "$HOME/foo"
+[unquoted]
+	withvar = $HOME/foo
+EOF
+
+test_expect_success 'quoted $VAR' \
+	'test x"\$HOME/foo" = x$(git config quoted.withvar)'
+
+test_expect_success 'unquoted $VAR' \
+	'test x"$HOME/foo" = x$(git config unquoted.withvar)'
+
 test_done
 
-- 
1.5.2.1.112.gdb0c


-- 
Martin Waitz

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

             reply	other threads:[~2007-06-03 22:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-03 22:37 Martin Waitz [this message]
2007-06-04  5:34 ` [PATCH] Support environment variables in config file Matthias Lederhofer
2007-06-04  7:27   ` Martin Waitz
2007-06-04  8:44     ` Matthias Lederhofer
2007-06-04 15:57     ` Johannes Schindelin
2007-06-04 17:47       ` Martin Waitz
2007-06-04 17:59         ` Johannes Schindelin

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=20070603223727.GA16637@admingilde.org \
    --to=tali@admingilde.org \
    --cc=git@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).