git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jari Aalto <jari.aalto@cante.net>
To: git@vger.kernel.org
Subject: [PATCH] config.c: Expand $HOME and tilde character in core.excludesfile
Date: Mon, 28 Jan 2008 23:49:05 +0200	[thread overview]
Message-ID: <y7a9aaem.fsf@blue.sea.net> (raw)

c* str_replace(): New function. Generic replace command.
* str_replace_home(): New funtion. Substitute $HOME and tilde(~) in string.
* git_default_config(): Pass core.excludesfile to str_replace_home().

Signed-off-by: Jari Aalto <jari.aalto AT cante.net>
---

 From ac6941f5055b2acdded59627d228bbf03ba0d9fc

 config.c |   44 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/config.c b/config.c
index 526a3f4..7c91689 100644
--- a/config.c
+++ b/config.c
@@ -309,6 +309,46 @@ int git_config_bool(const char *name, const char *value)
 	return git_config_int(name, value) != 0;
 }
 
+char *str_replace(const char *str, const char *find, const char *replace)
+{
+        int maxlen   = strlen(str) + strlen(replace) + 1;
+        char *start  = strstr(str, find);
+        char *ptr    = (char *)malloc(maxlen);
+        int len      = strlen(find);
+        int llen, rlen;         /* left, right portion length */
+
+        if (start == (char *)NULL) {
+                strcpy( ptr, str);
+        }
+        else{
+                rlen = strlen(start) - strlen(find);
+                llen = strlen(str) - strlen(start);
+                strncpy( ptr, str, llen);
+                strcat( ptr, replace);
+                strncat( ptr, start + len, rlen); /* Does not add  '\0' */
+                strcat( ptr, "");   /* Terminate with null string */
+        }
+
+        return ptr;
+}
+
+char *str_replace_home(const char *str)
+{
+        char *ret  = xstrdup(str);
+        char *home = getenv("HOME");
+
+        if (home != (char *)NULL ) {
+                if (strstr(str, "~") != NULL) {
+                        ret = str_replace(str, "~", home);
+                }
+                else if (strstr(str, "$HOME") != NULL) {
+                        ret = str_replace(str, "$HOME", home);
+                }
+        }
+
+        return ret;
+}
+
 int git_default_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -447,7 +487,9 @@ int git_default_config(const char *var, const char *value)
 		if (!value)
 			die("core.excludesfile without value");
 		excludes_file = xstrdup(value);
-		return 0;
+                /* expand $HOME and tilde(~) */
+                excludes_file = str_replace_home(excludes_file);
+                return 0;
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
-- 
1.5.4-rc3.GIT

             reply	other threads:[~2008-01-28 21:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-28 21:49 Jari Aalto [this message]
2008-01-28 22:28 ` [PATCH] config.c: Expand $HOME and tilde character in core.excludesfile Johannes Schindelin
2008-01-28 22:32 ` Jakub Narebski
2008-01-29  5:59   ` Miles Bader
2008-01-29  7:25     ` David Symonds
2008-01-29  7:51       ` Miles Bader
2008-01-28 23:52 ` Wayne Davison

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=y7a9aaem.fsf@blue.sea.net \
    --to=jari.aalto@cante.net \
    --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).