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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Feb 25, 2017 at 17:00 UTC
33ad9ddd0b5398063f0aabea639b5fe569f458ea
4 files changed
+32
-1
contrib/coccinelle/strbuf.cocci
+6
@@ -38,3 +38,9 @@ expression E1, E2, E3;
38
@@
39
- strbuf_addstr(E1, find_unique_abbrev(E2, E3));
40
+ strbuf_add_unique_abbrev(E1, E2, E3);
41
+
42
+@@
43
+expression E1, E2;
44
+@@
45
+- strbuf_addstr(E1, real_path(E2));
46
++ strbuf_add_real_path(E1, E2);
setup.c
+1
-1
@@ -254,7 +254,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
254
if (!is_absolute_path(data.buf))
255
strbuf_addf(&path, "%s/", gitdir);
256
strbuf_addbuf(&path, &data);
257
- strbuf_addstr(sb, real_path(path.buf));
257
+ strbuf_add_real_path(sb, path.buf);
258
ret = 1;
259
} else
260
strbuf_addstr(sb, gitdir);
strbuf.c
+11
@@ -707,6 +707,17 @@ void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
707
strbuf_addstr(sb, path);
708
}
709
710
+void strbuf_add_real_path(struct strbuf *sb, const char *path)
711
+{
712
+ if (sb->len) {
713
+ struct strbuf resolved = STRBUF_INIT;
714
+ strbuf_realpath(&resolved, path, 1);
715
+ strbuf_addbuf(sb, &resolved);
716
+ strbuf_release(&resolved);
717
+ } else
718
+ strbuf_realpath(sb, path, 1);
719
+}
720
+
721
int printf_ln(const char *fmt, ...)
722
{
723
int ret;
strbuf.h
+14
@@ -443,6 +443,20 @@ extern int strbuf_getcwd(struct strbuf *sb);
443
*/
444
extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
445
446
+/**
447
+ * Canonize `path` (make it absolute, resolve symlinks, remove extra
448
+ * slashes) and append it to `sb`. Die with an informative error
449
+ * message if there is a problem.
450
+ *
451
+ * The directory part of `path` (i.e., everything up to the last
452
+ * dir_sep) must denote a valid, existing directory, but the last
453
+ * component need not exist.
454
+ *
455
+ * Callers that don't mind links should use the more lightweight
456
+ * strbuf_add_absolute_path() instead.
457
+ */
458
+extern void strbuf_add_real_path(struct strbuf *sb, const char *path);
459
+
460
461
/**
462
* Normalize in-place the path contained in the strbuf. See