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:

committed by
Junio C Hamano

parent
3efd0bedc6
commit
4aad2f1627
11
path.c
11
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,
|
||||
* then it is a newly allocated string. Returns NULL on getpw failure or
|
||||
* 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;
|
||||
const char *to_copy = path;
|
||||
@ -654,7 +656,10 @@ char *expand_user_path(const char *path)
|
||||
const char *home = getenv("HOME");
|
||||
if (!home)
|
||||
goto return_null;
|
||||
strbuf_addstr(&user_path, home);
|
||||
if (real_home)
|
||||
strbuf_addstr(&user_path, real_path(home));
|
||||
else
|
||||
strbuf_addstr(&user_path, home);
|
||||
#ifdef GIT_WINDOWS_NATIVE
|
||||
convert_slashes(user_path.buf);
|
||||
#endif
|
||||
@ -723,7 +728,7 @@ const char *enter_repo(const char *path, int strict)
|
||||
strbuf_add(&validated_path, path, len);
|
||||
|
||||
if (used_path.buf[0] == '~') {
|
||||
char *newpath = expand_user_path(used_path.buf);
|
||||
char *newpath = expand_user_path(used_path.buf, 0);
|
||||
if (!newpath)
|
||||
return NULL;
|
||||
strbuf_attach(&used_path, newpath, strlen(newpath),
|
||||
|
Reference in New Issue
Block a user