Skip to content

Commit df2a79f

Browse files
moygitster
authored andcommitted
expand_user_path: expand ~ to $HOME, not to the actual homedir.
In 395de25 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 395de25 commit df2a79f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Documentation/config.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ Common unit suffixes of 'k', 'm', or 'g' are supported.
380380
core.excludesfile::
381381
In addition to '.gitignore' (per-directory) and
382382
'.git/info/exclude', git looks into this file for patterns
383-
of files which are not meant to be tracked. "~/" and "~user/"
384-
are expanded to the specified user's home directory. See
385-
linkgit:gitignore[5].
383+
of files which are not meant to be tracked. "~/" is expanded
384+
to the value of `$HOME` and "~user/" to the specified user's
385+
home directory. See linkgit:gitignore[5].
386386

387387
core.editor::
388388
Commands such as `commit` and `tag` that lets you edit
@@ -667,7 +667,8 @@ color.ui::
667667

668668
commit.template::
669669
Specify a file to use as the template for new commit messages.
670-
"~/" and "~user/" are expanded to the specified user's home directory.
670+
"~/" is expanded to the value of `$HOME` and "~user/" to the
671+
specified user's home directory.
671672

672673
diff.autorefreshindex::
673674
When using 'git-diff' to compare with work tree

path.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,15 @@ char *expand_user_path(const char *path)
235235
if (path[0] == '~') {
236236
const char *username = path + 1;
237237
size_t username_len = first_slash - username;
238-
struct passwd *pw = getpw_str(username, username_len);
239-
if (!pw)
240-
goto return_null;
241-
strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
238+
if (username_len == 0) {
239+
const char *home = getenv("HOME");
240+
strbuf_add(&user_path, home, strlen(home));
241+
} else {
242+
struct passwd *pw = getpw_str(username, username_len);
243+
if (!pw)
244+
goto return_null;
245+
strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
246+
}
242247
to_copy = first_slash;
243248
}
244249
strbuf_add(&user_path, to_copy, strlen(to_copy));

0 commit comments

Comments
 (0)