From: Duy Nguyen <pclo...@gmail.com>

This function was added in 10c4c881c4 (Allow add_path() to add
non-existent directories to the path - 2008-07-21) because getcwd()
may fail on non-existing cwd and we want to construct non-existing
absolute paths sometimes.

The function was merged back in strbuf_add_absolute_path() some time
later. Move it out again because it will have another caller shortly.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 strbuf.c | 37 ++++++++++++++++++++++---------------
 strbuf.h |  6 ++++++
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index d5b7cda61e..aed4bec856 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -746,27 +746,34 @@ void strbuf_humanise_bytes(struct strbuf *buf, off_t 
bytes)
        }
 }
 
+void strbuf_get_pwd_cwd(struct strbuf *sb)
+{
+       struct stat cwd_stat, pwd_stat;
+       char *cwd = xgetcwd();
+       char *pwd = getenv("PWD");
+
+       if (pwd && strcmp(pwd, cwd) &&
+           !stat(cwd, &cwd_stat) &&
+           (cwd_stat.st_dev || cwd_stat.st_ino) &&
+           !stat(pwd, &pwd_stat) &&
+           pwd_stat.st_dev == cwd_stat.st_dev &&
+           pwd_stat.st_ino == cwd_stat.st_ino)
+               strbuf_addstr(sb, pwd);
+       else
+               strbuf_addstr(sb, cwd);
+       free(cwd);
+}
+
 void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
 {
        if (!*path)
                die("The empty string is not a valid path");
        if (!is_absolute_path(path)) {
-               struct stat cwd_stat, pwd_stat;
                size_t orig_len = sb->len;
-               char *cwd = xgetcwd();
-               char *pwd = getenv("PWD");
-               if (pwd && strcmp(pwd, cwd) &&
-                   !stat(cwd, &cwd_stat) &&
-                   (cwd_stat.st_dev || cwd_stat.st_ino) &&
-                   !stat(pwd, &pwd_stat) &&
-                   pwd_stat.st_dev == cwd_stat.st_dev &&
-                   pwd_stat.st_ino == cwd_stat.st_ino)
-                       strbuf_addstr(sb, pwd);
-               else
-                       strbuf_addstr(sb, cwd);
-               if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
-                       strbuf_addch(sb, '/');
-               free(cwd);
+
+               strbuf_get_pwd_cwd(sb);
+               if (sb->len > orig_len)
+                       strbuf_ensure_trailing_dir_sep(sb);
        }
        strbuf_addstr(sb, path);
 }
diff --git a/strbuf.h b/strbuf.h
index 62dc7f16fa..f712c4ff92 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -458,6 +458,12 @@ extern int strbuf_getwholeline_fd(struct strbuf *, int, 
int);
  */
 extern int strbuf_getcwd(struct strbuf *sb);
 
+/**
+ * Return the current directory, fall back to $PWD if the
+ * current directory does not exist.
+ */
+extern void strbuf_get_pwd_cwd(struct strbuf *sb);
+
 /**
  * Add a path to a buffer, converting a relative path to an
  * absolute one in the process.  Symbolic links are not
-- 
2.17.0.rc1.439.gca064e2955

Reply via email to