From 33ad9ddd0b5398063f0aabea639b5fe569f458ea Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Sat, 25 Feb 2017 17:00:33 +0100 Subject: strbuf: add strbuf_add_real_path() Add a function for appending the canonized absolute pathname of a given path to a strbuf. It keeps the existing contents intact, as expected of a function of the strbuf_add() family, while avoiding copying the result if the given strbuf is empty. It's more consistent with the rest of the strbuf API than strbuf_realpath(), which it's wrapping. Also add a semantic patch demonstrating its intended usage and apply it to the current tree. Using strbuf_add_real_path() instead of calling strbuf_addstr() and real_path() avoids an extra copy to a static buffer. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- strbuf.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index 8fec6579f7..ace58e7367 100644 --- a/strbuf.c +++ b/strbuf.c @@ -707,6 +707,17 @@ void strbuf_add_absolute_path(struct strbuf *sb, const char *path) strbuf_addstr(sb, path); } +void strbuf_add_real_path(struct strbuf *sb, const char *path) +{ + if (sb->len) { + struct strbuf resolved = STRBUF_INIT; + strbuf_realpath(&resolved, path, 1); + strbuf_addbuf(sb, &resolved); + strbuf_release(&resolved); + } else + strbuf_realpath(sb, path, 1); +} + int printf_ln(const char *fmt, ...) { int ret; -- cgit v1.3