From 4aad2f1627bb74948874c0f31e8ce256bf236aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 5 Apr 2017 17:24:38 +0700 Subject: path.c: and an option to call real_path() in expand_user_path() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- path.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'path.c') diff --git a/path.c b/path.c index efcedafba6..8b7687d53a 100644 --- a/path.c +++ b/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), -- cgit v1.2.3