path.c: add git_common_path() and strbuf_git_common_path()

These are mostly convenient functions to reduce code duplication. Most of the time, we should be able to get by with git_path() which handles $GIT_COMMON_DIR internally. However there are a few cases where we need to construct paths manually, for example some paths from a specific worktree. These functions will enable that. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 22, 2016 at 20:01 UTC 15cdfea734ffce99d930e8e8016787b57ac47386
2 files changed +32
cache.h
+3
@@ -799,11 +799,14 @@ extern void check_repository_format(void);
799 */
800 extern const char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
801 extern const char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
802 +extern const char *git_common_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
803
804 extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
805 __attribute__((format (printf, 3, 4)));
806 extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
807 __attribute__((format (printf, 2, 3)));
808 +extern void strbuf_git_common_path(struct strbuf *sb, const char *fmt, ...)
809 + __attribute__((format (printf, 2, 3)));
810 extern char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
811 __attribute__((format (printf, 2, 3)));
812 extern void strbuf_git_path_submodule(struct strbuf *sb, const char *path,
path.c
+29
@@ -503,6 +503,35 @@ void strbuf_git_path_submodule(struct strbuf *buf, const char *path,
503 va_end(args);
504 }
505
506 +static void do_git_common_path(struct strbuf *buf,
507 + const char *fmt,
508 + va_list args)
509 +{
510 + strbuf_addstr(buf, get_git_common_dir());
511 + if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
512 + strbuf_addch(buf, '/');
513 + strbuf_vaddf(buf, fmt, args);
514 + strbuf_cleanup_path(buf);
515 +}
516 +
517 +const char *git_common_path(const char *fmt, ...)
518 +{
519 + struct strbuf *pathname = get_pathname();
520 + va_list args;
521 + va_start(args, fmt);
522 + do_git_common_path(pathname, fmt, args);
523 + va_end(args);
524 + return pathname->buf;
525 +}
526 +
527 +void strbuf_git_common_path(struct strbuf *sb, const char *fmt, ...)
528 +{
529 + va_list args;
530 + va_start(args, fmt);
531 + do_git_common_path(sb, fmt, args);
532 + va_end(args);
533 +}
534 +
535 int validate_headref(const char *path)
536 {
537 struct stat st;