cache.h: complete set of git_path_submodule helpers
The git_path function has "git_pathdup" and "strbuf_git_path" variants, but git_submodule_path only comes in the dangerous, static-buffer variant. That makes refactoring callers to use the safer functions hard (since they don't exist). Since we're already using a strbuf behind the scenes, it's easy to expose all three of these interfaces with thin wrappers. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 10, 2015 at 05:32 UTC
f5895fd399855e782ed0f86f1ac0531517efa0d9
2 files changed
+35
-5
cache.h
+5
@@ -724,10 +724,15 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
724
__attribute__((format (printf, 3, 4)));
725
extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
726
__attribute__((format (printf, 2, 3)));
727
+extern void strbuf_git_path_submodule(struct strbuf *sb, const char *path,
728
+ const char *fmt, ...)
729
+ __attribute__((format (printf, 3, 4)));
730
extern char *git_pathdup(const char *fmt, ...)
731
__attribute__((format (printf, 1, 2)));
732
extern char *mkpathdup(const char *fmt, ...)
733
__attribute__((format (printf, 1, 2)));
734
+extern char *git_pathdup_submodule(const char *path, const char *fmt, ...)
735
+ __attribute__((format (printf, 2, 3)));
736
737
extern void report_linked_checkout_garbage(void);
738
path.c
+30
-5
@@ -224,11 +224,10 @@ const char *mkpath(const char *fmt, ...)
224
return cleanup_path(pathname->buf);
225
}
226
227
-const char *git_path_submodule(const char *path, const char *fmt, ...)
227
+static void do_submodule_path(struct strbuf *buf, const char *path,
228
+ const char *fmt, va_list args)
229
{
229
- struct strbuf *buf = get_pathname();
230
const char *git_dir;
231
- va_list args;
231
232
strbuf_addstr(buf, path);
233
if (buf->len && buf->buf[buf->len - 1] != '/')
@@ -242,13 +241,39 @@ const char *git_path_submodule(const char *path, const char *fmt, ...)
241
}
242
strbuf_addch(buf, '/');
243
245
- va_start(args, fmt);
244
strbuf_vaddf(buf, fmt, args);
247
- va_end(args);
245
strbuf_cleanup_path(buf);
246
+}
247
+
248
+const char *git_path_submodule(const char *path, const char *fmt, ...)
249
+{
250
+ va_list args;
251
+ struct strbuf *buf = get_pathname();
252
+ va_start(args, fmt);
253
+ do_submodule_path(buf, path, fmt, args);
254
+ va_end(args);
255
return buf->buf;
256
}
257
258
+char *git_pathdup_submodule(const char *path, const char *fmt, ...)
259
+{
260
+ va_list args;
261
+ struct strbuf buf = STRBUF_INIT;
262
+ va_start(args, fmt);
263
+ do_submodule_path(&buf, path, fmt, args);
264
+ va_end(args);
265
+ return strbuf_detach(&buf, NULL);
266
+}
267
+
268
+void strbuf_git_path_submodule(struct strbuf *buf, const char *path,
269
+ const char *fmt, ...)
270
+{
271
+ va_list args;
272
+ va_start(args, fmt);
273
+ do_submodule_path(buf, path, fmt, args);
274
+ va_end(args);
275
+}
276
+
277
int validate_headref(const char *path)
278
{
279
struct stat st;