Skip to content

Commit f5895fd

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 69ddd23 commit f5895fd

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

cache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,10 +724,15 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
724724
__attribute__((format (printf, 3, 4)));
725725
extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
726726
__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)));
727730
extern char *git_pathdup(const char *fmt, ...)
728731
__attribute__((format (printf, 1, 2)));
729732
extern char *mkpathdup(const char *fmt, ...)
730733
__attribute__((format (printf, 1, 2)));
734+
extern char *git_pathdup_submodule(const char *path, const char *fmt, ...)
735+
__attribute__((format (printf, 2, 3)));
731736

732737
extern void report_linked_checkout_garbage(void);
733738

path.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,10 @@ const char *mkpath(const char *fmt, ...)
224224
return cleanup_path(pathname->buf);
225225
}
226226

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)
228229
{
229-
struct strbuf *buf = get_pathname();
230230
const char *git_dir;
231-
va_list args;
232231

233232
strbuf_addstr(buf, path);
234233
if (buf->len && buf->buf[buf->len - 1] != '/')
@@ -242,13 +241,39 @@ const char *git_path_submodule(const char *path, const char *fmt, ...)
242241
}
243242
strbuf_addch(buf, '/');
244243

245-
va_start(args, fmt);
246244
strbuf_vaddf(buf, fmt, args);
247-
va_end(args);
248245
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);
249255
return buf->buf;
250256
}
251257

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+
252277
int validate_headref(const char *path)
253278
{
254279
struct stat st;

0 commit comments

Comments
 (0)