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>
This commit is contained in:
parent
3efd0bedc6
commit
4aad2f1627
@ -1404,7 +1404,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
|
|||||||
|
|
||||||
static const char *implicit_ident_advice(void)
|
static const char *implicit_ident_advice(void)
|
||||||
{
|
{
|
||||||
char *user_config = expand_user_path("~/.gitconfig");
|
char *user_config = expand_user_path("~/.gitconfig", 0);
|
||||||
char *xdg_config = xdg_config_home("config");
|
char *xdg_config = xdg_config_home("config");
|
||||||
int config_exists = file_exists(user_config) || file_exists(xdg_config);
|
int config_exists = file_exists(user_config) || file_exists(xdg_config);
|
||||||
|
|
||||||
|
@ -502,7 +502,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (use_global_config) {
|
if (use_global_config) {
|
||||||
char *user_config = expand_user_path("~/.gitconfig");
|
char *user_config = expand_user_path("~/.gitconfig", 0);
|
||||||
char *xdg_config = xdg_config_home("config");
|
char *xdg_config = xdg_config_home("config");
|
||||||
|
|
||||||
if (!user_config)
|
if (!user_config)
|
||||||
|
2
cache.h
2
cache.h
@ -1098,7 +1098,7 @@ enum scld_error safe_create_leading_directories(char *path);
|
|||||||
enum scld_error safe_create_leading_directories_const(const char *path);
|
enum scld_error safe_create_leading_directories_const(const char *path);
|
||||||
|
|
||||||
int mkdir_in_gitdir(const char *path);
|
int mkdir_in_gitdir(const char *path);
|
||||||
extern char *expand_user_path(const char *path);
|
extern char *expand_user_path(const char *path, int real_home);
|
||||||
const char *enter_repo(const char *path, int strict);
|
const char *enter_repo(const char *path, int strict);
|
||||||
static inline int is_absolute_path(const char *path)
|
static inline int is_absolute_path(const char *path)
|
||||||
{
|
{
|
||||||
|
8
config.c
8
config.c
@ -135,7 +135,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
|
|||||||
if (!path)
|
if (!path)
|
||||||
return config_error_nonbool("include.path");
|
return config_error_nonbool("include.path");
|
||||||
|
|
||||||
expanded = expand_user_path(path);
|
expanded = expand_user_path(path, 0);
|
||||||
if (!expanded)
|
if (!expanded)
|
||||||
return error("could not expand include path '%s'", path);
|
return error("could not expand include path '%s'", path);
|
||||||
path = expanded;
|
path = expanded;
|
||||||
@ -177,7 +177,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
|
|||||||
char *expanded;
|
char *expanded;
|
||||||
int prefix = 0;
|
int prefix = 0;
|
||||||
|
|
||||||
expanded = expand_user_path(pat->buf);
|
expanded = expand_user_path(pat->buf, 0);
|
||||||
if (expanded) {
|
if (expanded) {
|
||||||
strbuf_reset(pat);
|
strbuf_reset(pat);
|
||||||
strbuf_addstr(pat, expanded);
|
strbuf_addstr(pat, expanded);
|
||||||
@ -857,7 +857,7 @@ int git_config_pathname(const char **dest, const char *var, const char *value)
|
|||||||
{
|
{
|
||||||
if (!value)
|
if (!value)
|
||||||
return config_error_nonbool(var);
|
return config_error_nonbool(var);
|
||||||
*dest = expand_user_path(value);
|
*dest = expand_user_path(value, 0);
|
||||||
if (!*dest)
|
if (!*dest)
|
||||||
die(_("failed to expand user dir in: '%s'"), value);
|
die(_("failed to expand user dir in: '%s'"), value);
|
||||||
return 0;
|
return 0;
|
||||||
@ -1407,7 +1407,7 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
|
|||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char *xdg_config = xdg_config_home("config");
|
char *xdg_config = xdg_config_home("config");
|
||||||
char *user_config = expand_user_path("~/.gitconfig");
|
char *user_config = expand_user_path("~/.gitconfig", 0);
|
||||||
char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
|
char *repo_config = have_git_dir() ? git_pathdup("config") : NULL;
|
||||||
|
|
||||||
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
|
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
|
||||||
|
@ -106,7 +106,7 @@ int cmd_main(int argc, const char **argv)
|
|||||||
op = argv[0];
|
op = argv[0];
|
||||||
|
|
||||||
if (!socket_path)
|
if (!socket_path)
|
||||||
socket_path = expand_user_path("~/.git-credential-cache/socket");
|
socket_path = expand_user_path("~/.git-credential-cache/socket", 0);
|
||||||
if (!socket_path)
|
if (!socket_path)
|
||||||
die("unable to find a suitable socket path; use --socket");
|
die("unable to find a suitable socket path; use --socket");
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ int cmd_main(int argc, const char **argv)
|
|||||||
if (file) {
|
if (file) {
|
||||||
string_list_append(&fns, file);
|
string_list_append(&fns, file);
|
||||||
} else {
|
} else {
|
||||||
if ((file = expand_user_path("~/.git-credentials")))
|
if ((file = expand_user_path("~/.git-credentials", 0)))
|
||||||
string_list_append_nodup(&fns, file);
|
string_list_append_nodup(&fns, file);
|
||||||
file = xdg_config_home("credentials");
|
file = xdg_config_home("credentials");
|
||||||
if (file)
|
if (file)
|
||||||
|
9
path.c
9
path.c
@ -638,8 +638,10 @@ static struct passwd *getpw_str(const char *username, size_t len)
|
|||||||
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
|
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
|
||||||
* then it is a newly allocated string. Returns NULL on getpw failure or
|
* then it is a newly allocated string. Returns NULL on getpw failure or
|
||||||
* if path is NULL.
|
* if path is NULL.
|
||||||
|
*
|
||||||
|
* If real_home is true, real_path($HOME) is used in the expansion.
|
||||||
*/
|
*/
|
||||||
char *expand_user_path(const char *path)
|
char *expand_user_path(const char *path, int real_home)
|
||||||
{
|
{
|
||||||
struct strbuf user_path = STRBUF_INIT;
|
struct strbuf user_path = STRBUF_INIT;
|
||||||
const char *to_copy = path;
|
const char *to_copy = path;
|
||||||
@ -654,6 +656,9 @@ char *expand_user_path(const char *path)
|
|||||||
const char *home = getenv("HOME");
|
const char *home = getenv("HOME");
|
||||||
if (!home)
|
if (!home)
|
||||||
goto return_null;
|
goto return_null;
|
||||||
|
if (real_home)
|
||||||
|
strbuf_addstr(&user_path, real_path(home));
|
||||||
|
else
|
||||||
strbuf_addstr(&user_path, home);
|
strbuf_addstr(&user_path, home);
|
||||||
#ifdef GIT_WINDOWS_NATIVE
|
#ifdef GIT_WINDOWS_NATIVE
|
||||||
convert_slashes(user_path.buf);
|
convert_slashes(user_path.buf);
|
||||||
@ -723,7 +728,7 @@ const char *enter_repo(const char *path, int strict)
|
|||||||
strbuf_add(&validated_path, path, len);
|
strbuf_add(&validated_path, path, len);
|
||||||
|
|
||||||
if (used_path.buf[0] == '~') {
|
if (used_path.buf[0] == '~') {
|
||||||
char *newpath = expand_user_path(used_path.buf);
|
char *newpath = expand_user_path(used_path.buf, 0);
|
||||||
if (!newpath)
|
if (!newpath)
|
||||||
return NULL;
|
return NULL;
|
||||||
strbuf_attach(&used_path, newpath, strlen(newpath),
|
strbuf_attach(&used_path, newpath, strlen(newpath),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user