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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Apr 5, 2017 at 17:24 UTC
4aad2f1627bb74948874c0f31e8ce256bf236aa6
7 files changed
+17
-12
builtin/commit.c
+1
-1
@@ -1404,7 +1404,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1404
1405
static const char *implicit_ident_advice(void)
1406
{
1407
- char *user_config = expand_user_path("~/.gitconfig");
1407
+ char *user_config = expand_user_path("~/.gitconfig", 0);
1408
char *xdg_config = xdg_config_home("config");
1409
int config_exists = file_exists(user_config) || file_exists(xdg_config);
1410
builtin/config.c
+1
-1
@@ -502,7 +502,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
502
}
503
504
if (use_global_config) {
505
- char *user_config = expand_user_path("~/.gitconfig");
505
+ char *user_config = expand_user_path("~/.gitconfig", 0);
506
char *xdg_config = xdg_config_home("config");
507
508
if (!user_config)
cache.h
+1
-1
@@ -1098,7 +1098,7 @@ enum scld_error safe_create_leading_directories(char *path);
1098
enum scld_error safe_create_leading_directories_const(const char *path);
1099
1100
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);
1102
const char *enter_repo(const char *path, int strict);
1103
static inline int is_absolute_path(const char *path)
1104
{
config.c
+4
-4
@@ -135,7 +135,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
135
if (!path)
136
return config_error_nonbool("include.path");
137
138
- expanded = expand_user_path(path);
138
+ expanded = expand_user_path(path, 0);
139
if (!expanded)
140
return error("could not expand include path '%s'", path);
141
path = expanded;
@@ -177,7 +177,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
177
char *expanded;
178
int prefix = 0;
179
180
- expanded = expand_user_path(pat->buf);
180
+ expanded = expand_user_path(pat->buf, 0);
181
if (expanded) {
182
strbuf_reset(pat);
183
strbuf_addstr(pat, expanded);
@@ -857,7 +857,7 @@ int git_config_pathname(const char **dest, const char *var, const char *value)
857
{
858
if (!value)
859
return config_error_nonbool(var);
860
- *dest = expand_user_path(value);
860
+ *dest = expand_user_path(value, 0);
861
if (!*dest)
862
die(_("failed to expand user dir in: '%s'"), value);
863
return 0;
@@ -1407,7 +1407,7 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
1407
{
1408
int ret = 0;
1409
char *xdg_config = xdg_config_home("config");
1410
- char *user_config = expand_user_path("~/.gitconfig");
1410
+ char *user_config = expand_user_path("~/.gitconfig", 0);
1411
char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
1412
1413
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
credential-cache.c
+1
-1
@@ -106,7 +106,7 @@ int cmd_main(int argc, const char **argv)
106
op = argv[0];
107
108
if (!socket_path)
109
- socket_path = expand_user_path("~/.git-credential-cache/socket");
109
+ socket_path = expand_user_path("~/.git-credential-cache/socket", 0);
110
if (!socket_path)
111
die("unable to find a suitable socket path; use --socket");
112
credential-store.c
+1
-1
@@ -168,7 +168,7 @@ int cmd_main(int argc, const char **argv)
168
if (file) {
169
string_list_append(&fns, file);
170
} else {
171
- if ((file = expand_user_path("~/.git-credentials")))
171
+ if ((file = expand_user_path("~/.git-credentials", 0)))
172
string_list_append_nodup(&fns, file);
173
file = xdg_config_home("credentials");
174
if (file)
path.c
+8
-3
@@ -638,8 +638,10 @@ static struct passwd *getpw_str(const char *username, size_t len)
638
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
639
* then it is a newly allocated string. Returns NULL on getpw failure or
640
* if path is NULL.
641
+ *
642
+ * If real_home is true, real_path($HOME) is used in the expansion.
643
*/
642
-char *expand_user_path(const char *path)
644
+char *expand_user_path(const char *path, int real_home)
645
{
646
struct strbuf user_path = STRBUF_INIT;
647
const char *to_copy = path;
@@ -654,7 +656,10 @@ char *expand_user_path(const char *path)
656
const char *home = getenv("HOME");
657
if (!home)
658
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);
663
#ifdef GIT_WINDOWS_NATIVE
664
convert_slashes(user_path.buf);
665
#endif
@@ -723,7 +728,7 @@ const char *enter_repo(const char *path, int strict)
728
strbuf_add(&validated_path, path, len);
729
730
if (used_path.buf[0] == '~') {
726
- char *newpath = expand_user_path(used_path.buf);
731
+ char *newpath = expand_user_path(used_path.buf, 0);
732
if (!newpath)
733
return NULL;
734
strbuf_attach(&used_path, newpath, strlen(newpath),