Skip to content

Commit 2ec5633

Browse files
pcloudsgitster
authored andcommitted
worktree.c: add get_worktree_config()
"git config --worktree" can write to the right file whether extensions.worktreeConfig is enabled or not. In order to do the same using config API, we need to determine the right file to write to. Add this function for that purpose. This is the basis for the coming repo_config_set_worktree() Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0680023 commit 2ec5633

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

builtin/config.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -650,18 +650,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
650650
else if (use_local_config)
651651
given_config_source.file = git_pathdup("config");
652652
else if (use_worktree_config) {
653-
struct worktree **worktrees = get_worktrees(0);
654-
if (repository_format_worktree_config)
655-
given_config_source.file = git_pathdup("config.worktree");
656-
else if (worktrees[0] && worktrees[1])
653+
given_config_source.file = get_worktree_config(the_repository);
654+
if (!given_config_source.file)
657655
die(_("--worktree cannot be used with multiple "
658656
"working trees unless the config\n"
659657
"extension worktreeConfig is enabled. "
660658
"Please read \"CONFIGURATION FILE\"\n"
661659
"section in \"git help worktree\" for details"));
662-
else
663-
given_config_source.file = git_pathdup("config");
664-
free_worktrees(worktrees);
665660
} else if (given_config_source.file) {
666661
if (!is_absolute_path(given_config_source.file) && prefix)
667662
given_config_source.file =

worktree.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,19 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
581581
free_worktrees(worktrees);
582582
return ret;
583583
}
584+
585+
char *get_worktree_config(struct repository *r)
586+
{
587+
struct worktree **worktrees = get_worktrees(0);
588+
char *path;
589+
590+
if (repository_format_worktree_config)
591+
path = repo_git_path(r, "config.worktree");
592+
else if (worktrees[0] && worktrees[1])
593+
path = NULL;
594+
else
595+
path = repo_git_path(r, "config");
596+
597+
free_worktrees(worktrees);
598+
return path;
599+
}

worktree.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,11 @@ void strbuf_worktree_ref(const struct worktree *wt,
132132
const char *worktree_ref(const struct worktree *wt,
133133
const char *refname);
134134

135+
/*
136+
* Return the path to config file that can contain worktree-specific
137+
* config (or NULL in unsupported setups). The caller must free the
138+
* return value.
139+
*/
140+
char *get_worktree_config(struct repository *r);
141+
135142
#endif

0 commit comments

Comments
 (0)