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

[PATCH] expand_user_path: expand ~ to $HOME, not to the actual homedir.

From
Matthieu Moy <matthieu.moy@imag.fr>
Date
Nov 19, 2009, 15:21 UTC
Message-ID
<1258644075-16191-1-git-send-email-Matthieu.Moy@imag.fr>
In-Reply-To
<1258534693-7220-1-git-send-email-Matthieu.Moy@imag.fr>

In 395de250d (Expand ~ and ~user in core.excludesfile, commit.template), we introduced the mechanism. But expanding ~ using getpw is not what people overriding $HOME would usually expect. In particular, git looks for the user's .gitconfig using $HOME, so it's better to be consistent.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
This is basically my patch v4, but since an earlier version is already
in next, I resend the last bits of it, based on next.
 Documentation/config.txt |    9 +++++----
 path.c                   |   13 +++++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 468e285..39d1226 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -381,9 +381,9 @@ Common unit suffixes of 'k', 'm', or 'g' are supported.
 core.excludesfile::
 	In addition to '.gitignore' (per-directory) and
 	'.git/info/exclude', git looks into this file for patterns
-	of files which are not meant to be tracked.  "~/" and "~user/"
-	are expanded to the specified user's home directory.  See
-	linkgit:gitignore[5].
+	of files which are not meant to be tracked.  "~/" is expanded
+	to the value of `$HOME` and "~user/" to the specified user's
+	home directory.  See linkgit:gitignore[5].
 
 core.editor::
 	Commands such as `commit` and `tag` that lets you edit
@@ -683,7 +683,8 @@ color.ui::
 
 commit.template::
 	Specify a file to use as the template for new commit messages.
-	"~/" and "~user/" are expanded to the specified user's home directory.
+	"~/" is expanded to the value of `$HOME` and "~user/" to the
+	specified user's home directory.
 
 diff.autorefreshindex::
 	When using 'git-diff' to compare with work tree
diff --git a/path.c b/path.c
index b4a8075..2ec950b 100644
--- a/path.c
+++ b/path.c
@@ -235,10 +235,15 @@ char *expand_user_path(const char *path)
 	if (path[0] == '~') {
 		const char *username = path + 1;
 		size_t username_len = first_slash - username;
-		struct passwd *pw = getpw_str(username, username_len);
-		if (!pw)
-			goto return_null;
-		strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
+		if (username_len == 0) {
+			const char *home = getenv("HOME");
+			strbuf_add(&user_path, home, strlen(home));
+		} else {
+			struct passwd *pw = getpw_str(username, username_len);
+			if (!pw)
+				goto return_null;
+			strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
+		}
 		to_copy = first_slash;
 	}
 	strbuf_add(&user_path, to_copy, strlen(to_copy));
-- 
1.6.5.2.152.gbbe9e
Previous: Matthieu MoyNext: Jeff King
Message 21 of 24 in “Expand ~ and ~user in core.excludesfile, commit.template”
  1. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 16, 2009
  2. Junio C HamanoNov 16, 2009
  3. Junio C HamanoNov 17, 2009
  4. Matthieu MoyNov 17, 2009
  5. Jakub NarebskiNov 16, 2009
  6. Junio C HamanoNov 17, 2009
  7. Matthieu MoyNov 17, 2009
  8. Jakub NarebskiNov 17, 2009
  9. Jakub NarebskiNov 17, 2009
  10. Jeff KingNov 17, 2009
  11. Mike HommeyNov 17, 2009
  12. Andreas SchwabNov 17, 2009
  13. Junio C HamanoNov 17, 2009
  14. Andreas SchwabNov 18, 2009
  15. Matthieu MoyNov 18, 2009
  16. Matthieu MoyNov 17, 2009
  17. Jeff KingNov 17, 2009
  18. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 17, 2009
  19. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 18, 2009
  20. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 18, 2009
  21. expand_user_path: expand ~ to $HOME, not to the actual homedir.Matthieu Moy, Nov 19, 2009
  22. Jeff KingNov 19, 2009
  23. Matthieu MoyNov 19, 2009
  24. Junio C HamanoNov 19, 2009

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.