Skip to content

Commit bb3788c

Browse files
peffgitster
authored andcommitted
add git_path_buf helper function
If you have a function that uses git_path a lot, but would prefer to avoid the static buffers, it's useful to keep a single scratch buffer locally and reuse it for each call. You used to be able to do this with git_snpath: char buf[PATH_MAX]; foo(git_snpath(buf, sizeof(buf), "foo")); bar(git_snpath(buf, sizeof(buf), "bar")); but since 1a83c24, git_snpath has been replaced with strbuf_git_path. This is good, because it removes the arbitrary PATH_MAX limit. But using strbuf_git_path is more awkward for two reasons: 1. It adds to the buffer, rather than replacing it. This is consistent with other strbuf functions, but makes reuse of a single buffer more tedious. 2. It doesn't return the buffer, so you can't format as part of a function's arguments. The new git_path_buf solves both of these, so you can use it like: struct strbuf buf = STRBUF_INIT; foo(git_path_buf(&buf, "foo")); bar(git_path_buf(&buf, "bar")); strbuf_release(&buf); Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7b03c89 commit bb3788c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
723723
__attribute__((format (printf, 3, 4)));
724724
extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
725725
__attribute__((format (printf, 2, 3)));
726+
extern char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
727+
__attribute__((format (printf, 2, 3)));
726728
extern void strbuf_git_path_submodule(struct strbuf *sb, const char *path,
727729
const char *fmt, ...)
728730
__attribute__((format (printf, 3, 4)));

path.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
175175
strbuf_cleanup_path(buf);
176176
}
177177

178+
char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
179+
{
180+
va_list args;
181+
strbuf_reset(buf);
182+
va_start(args, fmt);
183+
do_git_path(buf, fmt, args);
184+
va_end(args);
185+
return buf->buf;
186+
}
187+
178188
void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
179189
{
180190
va_list args;

0 commit comments

Comments
 (0)