Skip to content

Commit 15cdfea

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent ba0897e commit 15cdfea

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,14 @@ extern void check_repository_format(void);
799799
*/
800800
extern const char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
801801
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)));
802803

803804
extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
804805
__attribute__((format (printf, 3, 4)));
805806
extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
806807
__attribute__((format (printf, 2, 3)));
808+
extern void strbuf_git_common_path(struct strbuf *sb, const char *fmt, ...)
809+
__attribute__((format (printf, 2, 3)));
807810
extern char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
808811
__attribute__((format (printf, 2, 3)));
809812
extern void strbuf_git_path_submodule(struct strbuf *sb, const char *path,

path.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,35 @@ void strbuf_git_path_submodule(struct strbuf *buf, const char *path,
503503
va_end(args);
504504
}
505505

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+
506535
int validate_headref(const char *path)
507536
{
508537
struct stat st;

0 commit comments

Comments
 (0)