]> Pileus Git - ~andy/git/commitdiff
Add git_snpath: a .git path formatting routine with output buffer
authorAlex Riesen <raa.lkml@gmail.com>
Mon, 27 Oct 2008 09:22:21 +0000 (10:22 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Oct 2008 00:00:14 +0000 (17:00 -0700)
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 <gitster@pobox.com>
cache.h
path.c

diff --git a/cache.h b/cache.h
index aea13b0822d56546f30a02eb366524e6993c55df..3d5a08ea81e927d59d0c50d26ad416d9baa9e7f7 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -482,6 +482,8 @@ extern int check_repository_format(void);
 
 extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
        __attribute__((format (printf, 3, 4)));
+extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
+       __attribute__((format (printf, 3, 4)));
 
 /* Return a statically allocated filename matching the sha1 signature */
 extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
diff --git a/path.c b/path.c
index 8b64878c2147825a3a7ce483e9e6f05a0570a751..85ab28a0f1f0ff6276c587ec6d5be7716d8e0c83 100644 (file)
--- a/path.c
+++ b/path.c
@@ -47,6 +47,29 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
        return cleanup_path(buf);
 }
 
+char *git_snpath(char *buf, size_t n, const char *fmt, ...)
+{
+       const char *git_dir = get_git_dir();
+       va_list args;
+       size_t len;
+
+       len = strlen(git_dir);
+       if (n < len + 1)
+               goto bad;
+       memcpy(buf, git_dir, len);
+       if (len && !is_dir_sep(git_dir[len-1]))
+               buf[len++] = '/';
+       va_start(args, fmt);
+       len += vsnprintf(buf + len, n - len, fmt, args);
+       va_end(args);
+       if (len >= n)
+               goto bad;
+       return cleanup_path(buf);
+bad:
+       snprintf(buf, n, bad_path);
+       return buf;
+}
+
 char *mkpath(const char *fmt, ...)
 {
        va_list args;