Skip to content

Commit 4aad2f1

Browse files
pcloudsgitster
authored andcommitted
path.c: and an option to call real_path() in expand_user_path()
In the next patch we need the ability to expand '~' to real_path($HOME). But we can't do that from outside because '~' is part of a pattern, not a true path. Add an option to expand_user_path() to do so. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3efd0be commit 4aad2f1

File tree

7 files changed

+17
-12
lines changed

7 files changed

+17
-12
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14041404

14051405
static const char *implicit_ident_advice(void)
14061406
{
1407-
char *user_config = expand_user_path("~/.gitconfig");
1407+
char *user_config = expand_user_path("~/.gitconfig", 0);
14081408
char *xdg_config = xdg_config_home("config");
14091409
int config_exists = file_exists(user_config) || file_exists(xdg_config);
14101410

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
502502
}
503503

504504
if (use_global_config) {
505-
char *user_config = expand_user_path("~/.gitconfig");
505+
char *user_config = expand_user_path("~/.gitconfig", 0);
506506
char *xdg_config = xdg_config_home("config");
507507

508508
if (!user_config)

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ enum scld_error safe_create_leading_directories(char *path);
10981098
enum scld_error safe_create_leading_directories_const(const char *path);
10991099

11001100
int mkdir_in_gitdir(const char *path);
1101-
extern char *expand_user_path(const char *path);
1101+
extern char *expand_user_path(const char *path, int real_home);
11021102
const char *enter_repo(const char *path, int strict);
11031103
static inline int is_absolute_path(const char *path)
11041104
{

config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
135135
if (!path)
136136
return config_error_nonbool("include.path");
137137

138-
expanded = expand_user_path(path);
138+
expanded = expand_user_path(path, 0);
139139
if (!expanded)
140140
return error("could not expand include path '%s'", path);
141141
path = expanded;
@@ -177,7 +177,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
177177
char *expanded;
178178
int prefix = 0;
179179

180-
expanded = expand_user_path(pat->buf);
180+
expanded = expand_user_path(pat->buf, 0);
181181
if (expanded) {
182182
strbuf_reset(pat);
183183
strbuf_addstr(pat, expanded);
@@ -857,7 +857,7 @@ int git_config_pathname(const char **dest, const char *var, const char *value)
857857
{
858858
if (!value)
859859
return config_error_nonbool(var);
860-
*dest = expand_user_path(value);
860+
*dest = expand_user_path(value, 0);
861861
if (!*dest)
862862
die(_("failed to expand user dir in: '%s'"), value);
863863
return 0;
@@ -1407,7 +1407,7 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
14071407
{
14081408
int ret = 0;
14091409
char *xdg_config = xdg_config_home("config");
1410-
char *user_config = expand_user_path("~/.gitconfig");
1410+
char *user_config = expand_user_path("~/.gitconfig", 0);
14111411
char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
14121412

14131413
current_parsing_scope = CONFIG_SCOPE_SYSTEM;

credential-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int cmd_main(int argc, const char **argv)
106106
op = argv[0];
107107

108108
if (!socket_path)
109-
socket_path = expand_user_path("~/.git-credential-cache/socket");
109+
socket_path = expand_user_path("~/.git-credential-cache/socket", 0);
110110
if (!socket_path)
111111
die("unable to find a suitable socket path; use --socket");
112112

credential-store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ int cmd_main(int argc, const char **argv)
168168
if (file) {
169169
string_list_append(&fns, file);
170170
} else {
171-
if ((file = expand_user_path("~/.git-credentials")))
171+
if ((file = expand_user_path("~/.git-credentials", 0)))
172172
string_list_append_nodup(&fns, file);
173173
file = xdg_config_home("credentials");
174174
if (file)

path.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,10 @@ static struct passwd *getpw_str(const char *username, size_t len)
638638
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
639639
* then it is a newly allocated string. Returns NULL on getpw failure or
640640
* if path is NULL.
641+
*
642+
* If real_home is true, real_path($HOME) is used in the expansion.
641643
*/
642-
char *expand_user_path(const char *path)
644+
char *expand_user_path(const char *path, int real_home)
643645
{
644646
struct strbuf user_path = STRBUF_INIT;
645647
const char *to_copy = path;
@@ -654,7 +656,10 @@ char *expand_user_path(const char *path)
654656
const char *home = getenv("HOME");
655657
if (!home)
656658
goto return_null;
657-
strbuf_addstr(&user_path, home);
659+
if (real_home)
660+
strbuf_addstr(&user_path, real_path(home));
661+
else
662+
strbuf_addstr(&user_path, home);
658663
#ifdef GIT_WINDOWS_NATIVE
659664
convert_slashes(user_path.buf);
660665
#endif
@@ -723,7 +728,7 @@ const char *enter_repo(const char *path, int strict)
723728
strbuf_add(&validated_path, path, len);
724729

725730
if (used_path.buf[0] == '~') {
726-
char *newpath = expand_user_path(used_path.buf);
731+
char *newpath = expand_user_path(used_path.buf, 0);
727732
if (!newpath)
728733
return NULL;
729734
strbuf_attach(&used_path, newpath, strlen(newpath),

0 commit comments

Comments
 (0)