summaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2014-07-28 20:33:55 +0200
committerJunio C Hamano <gitster@pobox.com>2014-08-26 11:06:06 -0700
commit679eebe24d4c2120f8c01fb4524fcc489718fc9c (patch)
tree0dffe663fbce093ec9c07ab1c621b3d143995db3 /strbuf.c
parent4d3ab44d26c47d100cec39d0ef9ed9746eb7e454 (diff)
downloadgit-679eebe24d4c2120f8c01fb4524fcc489718fc9c.tar.gz
abspath: convert absolute_path() to strbuf
Move most of the code of absolute_path() into the new function strbuf_add_absolute_path() and in the process transform it to use struct strbuf and xgetcwd() instead of a PATH_MAX-sized buffer, which can be too small on some file systems. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index f3af203ec7..bf4c31b157 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -568,6 +568,31 @@ void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
}
}
+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_addstr(sb, path);
+}
+
int printf_ln(const char *fmt, ...)
{
int ret;