Skip to content

Commit fe2d777

Browse files
raalkmlgitster
authored andcommitted
Add git_snpath: a .git path formatting routine with output buffer
The function's purpose is to replace git_path where the buffer of formatted path may not be reused by subsequent calls of the function or will be copied anyway. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9fa03c1 commit fe2d777

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ extern int check_repository_format(void);
482482

483483
extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
484484
__attribute__((format (printf, 3, 4)));
485+
extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
486+
__attribute__((format (printf, 3, 4)));
485487

486488
/* Return a statically allocated filename matching the sha1 signature */
487489
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));

path.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
4747
return cleanup_path(buf);
4848
}
4949

50+
char *git_snpath(char *buf, size_t n, const char *fmt, ...)
51+
{
52+
const char *git_dir = get_git_dir();
53+
va_list args;
54+
size_t len;
55+
56+
len = strlen(git_dir);
57+
if (n < len + 1)
58+
goto bad;
59+
memcpy(buf, git_dir, len);
60+
if (len && !is_dir_sep(git_dir[len-1]))
61+
buf[len++] = '/';
62+
va_start(args, fmt);
63+
len += vsnprintf(buf + len, n - len, fmt, args);
64+
va_end(args);
65+
if (len >= n)
66+
goto bad;
67+
return cleanup_path(buf);
68+
bad:
69+
snprintf(buf, n, bad_path);
70+
return buf;
71+
}
72+
5073
char *mkpath(const char *fmt, ...)
5174
{
5275
va_list args;

0 commit comments

Comments
 (0)