path: add repo_worktree_path and strbuf_repo_worktree_path

Introduce 'repo_worktree_path' and 'strbuf_repo_worktree_path' which take a repository struct and constructs a path relative to the repository's worktree. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jun 22, 2017 at 11:43 UTC b42b0c09199db794b2a34ae9ce293d6711fb6a4f
2 files changed +49
path.c
+41
@@ -506,6 +506,47 @@ const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
506 return pathname->buf;
507 }
508
509 +static void do_worktree_path(const struct repository *repo,
510 + struct strbuf *buf,
511 + const char *fmt, va_list args)
512 +{
513 + strbuf_addstr(buf, repo->worktree);
514 + if(buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
515 + strbuf_addch(buf, '/');
516 +
517 + strbuf_vaddf(buf, fmt, args);
518 + strbuf_cleanup_path(buf);
519 +}
520 +
521 +char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
522 +{
523 + struct strbuf path = STRBUF_INIT;
524 + va_list args;
525 +
526 + if (!repo->worktree)
527 + return NULL;
528 +
529 + va_start(args, fmt);
530 + do_worktree_path(repo, &path, fmt, args);
531 + va_end(args);
532 +
533 + return strbuf_detach(&path, NULL);
534 +}
535 +
536 +void strbuf_repo_worktree_path(struct strbuf *sb,
537 + const struct repository *repo,
538 + const char *fmt, ...)
539 +{
540 + va_list args;
541 +
542 + if (!repo->worktree)
543 + return;
544 +
545 + va_start(args, fmt);
546 + do_worktree_path(repo, sb, fmt, args);
547 + va_end(args);
548 +}
549 +
550 /* Returns 0 on success, negative on failure. */
551 static int do_submodule_path(struct strbuf *buf, const char *path,
552 const char *fmt, va_list args)
path.h
+8
@@ -43,6 +43,14 @@ extern void strbuf_repo_git_path(struct strbuf *sb,
43 const char *fmt, ...)
44 __attribute__((format (printf, 3, 4)));
45
46 +extern char *repo_worktree_path(const struct repository *repo,
47 + const char *fmt, ...)
48 + __attribute__((format (printf, 2, 3)));
49 +extern void strbuf_repo_worktree_path(struct strbuf *sb,
50 + const struct repository *repo,
51 + const char *fmt, ...)
52 + __attribute__((format (printf, 3, 4)));
53 +
54 extern void report_linked_checkout_garbage(void);
55
56 /*