path.c: refactor and add worktree_git_path()

do_git_path(), which is the common code for all git_path* functions, is modified to take a worktree struct and can produce paths for any worktree. worktree_git_path() is the first function that makes use of this. It can be used to write code that can examine any worktree. For example, wt_status_get_state() will be converted using this to take am/rebase/... state of any worktree. 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 2e641d5825673c7dca384bc12fcaf1681d731bb6
2 files changed +26 -6
path.c
+18 -6
@@ -5,6 +5,7 @@
5 #include "strbuf.h"
6 #include "string-list.h"
7 #include "dir.h"
8 +#include "worktree.h"
9
10 static int get_st_mode_bits(const char *path, int *mode)
11 {
@@ -383,10 +384,11 @@ static void adjust_git_path(struct strbuf *buf, int git_dir_len)
384 update_common_dir(buf, git_dir_len, NULL);
385 }
386
386 -static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
387 +static void do_git_path(const struct worktree *wt, struct strbuf *buf,
388 + const char *fmt, va_list args)
389 {
390 int gitdir_len;
389 - strbuf_addstr(buf, get_git_dir());
391 + strbuf_addstr(buf, get_worktree_git_dir(wt));
392 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
393 strbuf_addch(buf, '/');
394 gitdir_len = buf->len;
@@ -400,7 +402,7 @@ char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
402 va_list args;
403 strbuf_reset(buf);
404 va_start(args, fmt);
403 - do_git_path(buf, fmt, args);
405 + do_git_path(NULL, buf, fmt, args);
406 va_end(args);
407 return buf->buf;
408 }
@@ -409,7 +411,7 @@ void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
411 {
412 va_list args;
413 va_start(args, fmt);
412 - do_git_path(sb, fmt, args);
414 + do_git_path(NULL, sb, fmt, args);
415 va_end(args);
416 }
417
@@ -418,7 +420,7 @@ const char *git_path(const char *fmt, ...)
420 struct strbuf *pathname = get_pathname();
421 va_list args;
422 va_start(args, fmt);
421 - do_git_path(pathname, fmt, args);
423 + do_git_path(NULL, pathname, fmt, args);
424 va_end(args);
425 return pathname->buf;
426 }
@@ -428,7 +430,7 @@ char *git_pathdup(const char *fmt, ...)
430 struct strbuf path = STRBUF_INIT;
431 va_list args;
432 va_start(args, fmt);
431 - do_git_path(&path, fmt, args);
433 + do_git_path(NULL, &path, fmt, args);
434 va_end(args);
435 return strbuf_detach(&path, NULL);
436 }
@@ -454,6 +456,16 @@ const char *mkpath(const char *fmt, ...)
456 return cleanup_path(pathname->buf);
457 }
458
459 +const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
460 +{
461 + struct strbuf *pathname = get_pathname();
462 + va_list args;
463 + va_start(args, fmt);
464 + do_git_path(wt, pathname, fmt, args);
465 + va_end(args);
466 + return pathname->buf;
467 +}
468 +
469 static void do_submodule_path(struct strbuf *buf, const char *path,
470 const char *fmt, va_list args)
471 {
worktree.h
+8
@@ -42,4 +42,12 @@ extern void free_worktrees(struct worktree **);
42 extern const struct worktree *find_shared_symref(const char *symref,
43 const char *target);
44
45 +/*
46 + * Similar to git_path() but can produce paths for a specified
47 + * worktree instead of current one
48 + */
49 +extern const char *worktree_git_path(const struct worktree *wt,
50 + const char *fmt, ...)
51 + __attribute__((format (printf, 2, 3)));
52 +
53 #endif