Skip to content

Commit 2e641d5

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 750e8a6 commit 2e641d5

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

path.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "strbuf.h"
66
#include "string-list.h"
77
#include "dir.h"
8+
#include "worktree.h"
89

910
static int get_st_mode_bits(const char *path, int *mode)
1011
{
@@ -383,10 +384,11 @@ static void adjust_git_path(struct strbuf *buf, int git_dir_len)
383384
update_common_dir(buf, git_dir_len, NULL);
384385
}
385386

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)
387389
{
388390
int gitdir_len;
389-
strbuf_addstr(buf, get_git_dir());
391+
strbuf_addstr(buf, get_worktree_git_dir(wt));
390392
if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
391393
strbuf_addch(buf, '/');
392394
gitdir_len = buf->len;
@@ -400,7 +402,7 @@ char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
400402
va_list args;
401403
strbuf_reset(buf);
402404
va_start(args, fmt);
403-
do_git_path(buf, fmt, args);
405+
do_git_path(NULL, buf, fmt, args);
404406
va_end(args);
405407
return buf->buf;
406408
}
@@ -409,7 +411,7 @@ void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
409411
{
410412
va_list args;
411413
va_start(args, fmt);
412-
do_git_path(sb, fmt, args);
414+
do_git_path(NULL, sb, fmt, args);
413415
va_end(args);
414416
}
415417

@@ -418,7 +420,7 @@ const char *git_path(const char *fmt, ...)
418420
struct strbuf *pathname = get_pathname();
419421
va_list args;
420422
va_start(args, fmt);
421-
do_git_path(pathname, fmt, args);
423+
do_git_path(NULL, pathname, fmt, args);
422424
va_end(args);
423425
return pathname->buf;
424426
}
@@ -428,7 +430,7 @@ char *git_pathdup(const char *fmt, ...)
428430
struct strbuf path = STRBUF_INIT;
429431
va_list args;
430432
va_start(args, fmt);
431-
do_git_path(&path, fmt, args);
433+
do_git_path(NULL, &path, fmt, args);
432434
va_end(args);
433435
return strbuf_detach(&path, NULL);
434436
}
@@ -454,6 +456,16 @@ const char *mkpath(const char *fmt, ...)
454456
return cleanup_path(pathname->buf);
455457
}
456458

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+
457469
static void do_submodule_path(struct strbuf *buf, const char *path,
458470
const char *fmt, va_list args)
459471
{

worktree.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ extern void free_worktrees(struct worktree **);
4242
extern const struct worktree *find_shared_symref(const char *symref,
4343
const char *target);
4444

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+
4553
#endif

0 commit comments

Comments
 (0)