git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] config.c: Expand $HOME and tilde character in core.excludesfile

From
Jari Aalto <jari.aalto@cante.net>
Date
Jan 28, 2008, 21:49 UTC
Message-ID
<y7a9aaem.fsf@blue.sea.net>
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
Next: Johannes Schindelin
Message 1 of 7 in “config.c: Expand $HOME and tilde character in core.excludesfile”
  1. config.c: Expand $HOME and tilde character in core.excludesfileJari Aalto, Jan 28, 2008
  2. Johannes SchindelinJan 28, 2008
  3. Jakub NarebskiJan 28, 2008
  4. Miles BaderJan 29, 2008
  5. David SymondsJan 29, 2008
  6. Miles BaderJan 29, 2008
  7. Wayne DavisonJan 28, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.