abspath: add absolute_pathdup()

Add a function that returns a buffer containing the absolute path of its argument and a semantic patch for its intended use. It avoids an extra string 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 Jan 26, 2017 at 18:47 UTC b1edb40f255a900154a9e7d34dcb9e0219427bd9
3 files changed +14
abspath.c
+7
@@ -152,6 +152,13 @@ const char *absolute_path(const char *path)
152 return sb.buf;
153 }
154
155 +char *absolute_pathdup(const char *path)
156 +{
157 + struct strbuf sb = STRBUF_INIT;
158 + strbuf_add_absolute_path(&sb, path);
159 + return strbuf_detach(&sb, NULL);
160 +}
161 +
162 /*
163 * Unlike prefix_path, this should be used if the named file does
164 * not have to interact with index entry; i.e. name of a random file
cache.h
+1
@@ -1033,6 +1033,7 @@ int is_directory(const char *);
1033 const char *real_path(const char *path);
1034 const char *real_path_if_valid(const char *path);
1035 const char *absolute_path(const char *path);
1036 +char *absolute_pathdup(const char *path);
1037 const char *remove_leading_path(const char *in, const char *prefix);
1038 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
1039 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
contrib/coccinelle/xstrdup_or_null.cocci
+6
@@ -5,3 +5,9 @@ expression V;
5 - if (E)
6 - V = xstrdup(E);
7 + V = xstrdup_or_null(E);
8 +
9 +@@
10 +expression E;
11 +@@
12 +- xstrdup(absolute_path(E))
13 ++ absolute_pathdup(E)