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

[PATCH v2] Support "core.excludesfile = ~/.gitignore"

From
KCKarl Chen <quarl@cs.berkeley.edu>
Date
Aug 25, 2008, 19:07 UTC
Message-ID
<quack.20080825T1207.lthk5e46hi4_-_@roar.cs.berkeley.edu>
In-Reply-To
<7vhc9aj82i.fsf@gitster.siamese.dyndns.org>

The config variable core.excludesfile is parsed to substitute ~ and ~user with getpw entries.

Signed-off-by: Karl Chen <quarl@quarl.org>
---
 config.c |   41 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 39 insertions(+), 2 deletions(-)

Based on the discussion it sounds like there are complications to supporting relative paths (due to worktree config), and "$HOME" (when generalized, due to bootstrapping issues with $GIT_*).

Since ~ and ~user are orthogonal to these, can I suggest going forward with this, without blocking on those two?

I have reworked the patch to use getpw to support ~user. $HOME can eventually be supported via $ENVVARs.

diff --git a/config.c b/config.c
index 53f04a0..6a83c64 100644
--- a/config.c
+++ b/config.c
@@ -334,6 +334,42 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+/*
+ * Expand ~ and ~user.  Returns a newly malloced string.  (If input does not
+ * start with "~", equivalent to xstrdup.)
+ */
+static char *expand_userdir(const char *value) {
+	if (value[0] == '~') {
+		struct passwd *pw;
+		char *expanded_dir;
+		const char *slash = strchr(value+1, '/');
+		const char *after_username = slash ? slash : value+strlen(value);
+		if (after_username == value+1) {
+			pw = getpwuid(getuid());
+			if (!pw) die("You don't exist!");
+		} else {
+			char save = *after_username;
+			*(char*)after_username = '\0';
+			pw = getpwnam(value+1);
+			if (!pw) die("No such user: '%s'", value+1);
+			*(char*)after_username = save;
+		}
+		expanded_dir = xmalloc(strlen(pw->pw_dir) + strlen(after_username) + 1);
+		strcpy(expanded_dir, pw->pw_dir);
+		strcat(expanded_dir, after_username);
+		return expanded_dir;
+	} else {
+		return xstrdup(value);
+	}
+}
+
+int git_config_userdir(const char **dest, const char *var, const char *value) {
+	if (!value)
+		return config_error_nonbool(var);
+	*dest = expand_userdir(value);
+	return 0;
+}
+
 static int git_default_core_config(const char *var, const char *value)
 {
 	/* This needs a better name */
@@ -456,8 +492,9 @@ static int git_default_core_config(const char *var, const char *value)
 	if (!strcmp(var, "core.editor"))
 		return git_config_string(&editor_program, var, value);
 
-	if (!strcmp(var, "core.excludesfile"))
-		return git_config_string(&excludes_file, var, value);
+	if (!strcmp(var, "core.excludesfile")) {
+		return git_config_userdir(&excludes_file, var, value);
+	}
 
 	if (!strcmp(var, "core.whitespace")) {
 		if (!value)
-- 
1.5.6.2
Previous: Jeff KingNext: Johannes Sixt
Message 28 of 45 in “Support "core.excludesfile = ~/.gitignore"”
  1. Support "core.excludesfile = ~/.gitignore"Karl Chen, Aug 22, 2008
  2. Eric RaibleAug 22, 2008
  3. Bert WesargAug 22, 2008
  4. Junio C HamanoAug 22, 2008
  5. Karl ChenAug 24, 2008
  6. Junio C HamanoAug 24, 2008
  7. Jeff KingAug 24, 2008
  8. Junio C HamanoAug 24, 2008
  9. Jeff KingAug 24, 2008
  10. Junio C HamanoAug 24, 2008
  11. limiting relationship of git dir and worktree (was Re: [PATCH] Support "core.excludesfile = ~/.gitignore")Jeff King, Aug 24, 2008
  12. Dropping core.worktree and GIT_WORK_TREE support (was Re: limiting relationship of git dir and worktree)Junio C Hamano, Aug 25, 2008
  13. Miklos VajnaAug 25, 2008
  14. Junio C HamanoAug 25, 2008
  15. Miklos VajnaAug 25, 2008
  16. Nguyen Thai Ngoc DuyAug 25, 2008
  17. git diff/diff-index/diff-files: call setup_work_tree()Miklos Vajna, Aug 25, 2008
  18. Nguyen Thai Ngoc DuyAug 25, 2008
  19. Miklos VajnaAug 25, 2008
  20. git diff/diff-index/diff-files: call setup_work_tree()Miklos Vajna, Aug 25, 2008
  21. Nguyen Thai Ngoc DuyAug 25, 2008
  22. Junio C HamanoAug 26, 2008
  23. diff*: fix worktree setupNguyễn Thái Ngọc Duy, Aug 28, 2008
  24. Junio C HamanoAug 25, 2008
  25. Miklos VajnaAug 25, 2008
  26. Michael J GruberAug 26, 2008
  27. Jeff KingAug 27, 2008
  28. Support "core.excludesfile = ~/.gitignore"Karl Chen, Aug 25, 2008
  29. Johannes SixtAug 26, 2008
  30. Jeff KingAug 27, 2008
  31. Karl ChenAug 27, 2008
  32. Junio C HamanoAug 27, 2008
  33. Expand ~ and ~user in core.excludesfile, commit.templateKarl Chen, Aug 28, 2008
  34. Jeff KingAug 29, 2008
  35. Junio C HamanoAug 29, 2008
  36. Expand ~ and ~user in core.excludesfile, commit.templateKarl Chen, Aug 29, 2008
  37. Junio C HamanoAug 29, 2008
  38. Karl ChenAug 29, 2008
  39. Junio C HamanoAug 29, 2008
  40. Karl ChenAug 29, 2008
  41. Junio C HamanoAug 30, 2008
  42. Jeff KingAug 30, 2008
  43. Johannes SixtAug 29, 2008
  44. Karl ChenAug 27, 2008
  45. Junio C HamanoAug 27, 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.