git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 2/6] worktree.c: add get_worktree_config()
Date: Thu, 27 Dec 2018 16:56:07 +0100	[thread overview]
Message-ID: <20181227155611.10585-3-pclouds@gmail.com> (raw)
In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com>

"git config --worktree" can write to the write 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 <pclouds@gmail.com>
---
 builtin/config.c |  9 ++-------
 worktree.c       | 16 ++++++++++++++++
 worktree.h       |  7 +++++++
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 84385ef165..771cfa54bd 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -650,18 +650,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 	else if (use_local_config)
 		given_config_source.file = git_pathdup("config");
 	else if (use_worktree_config) {
-		struct worktree **worktrees = get_worktrees(0);
-		if (repository_format_worktree_config)
-			given_config_source.file = git_pathdup("config.worktree");
-		else if (worktrees[0] && worktrees[1])
+		given_config_source.file = get_worktree_config(the_repository);
+		if (!given_config_source.file)
 			die(_("--worktree cannot be used with multiple "
 			      "working trees unless the config\n"
 			      "extension worktreeConfig is enabled. "
 			      "Please read \"CONFIGURATION FILE\"\n"
 			      "section in \"git help worktree\" for details"));
-		else
-			given_config_source.file = git_pathdup("config");
-		free_worktrees(worktrees);
 	} else if (given_config_source.file) {
 		if (!is_absolute_path(given_config_source.file) && prefix)
 			given_config_source.file =
diff --git a/worktree.c b/worktree.c
index d6a0ee7f73..d335bdf28a 100644
--- a/worktree.c
+++ b/worktree.c
@@ -581,3 +581,19 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
 	free_worktrees(worktrees);
 	return ret;
 }
+
+char *get_worktree_config(struct repository *r)
+{
+	struct worktree **worktrees = get_worktrees(0);
+	char *path;
+
+	if (repository_format_worktree_config)
+		path = repo_git_path(r, "config.worktree");
+	else if (worktrees[0] && worktrees[1])
+		path = NULL;
+	else
+		path = repo_git_path(r, "config");
+
+	free_worktrees(worktrees);
+	return path;
+}
diff --git a/worktree.h b/worktree.h
index 9e3b0b7b6f..4c41002d31 100644
--- a/worktree.h
+++ b/worktree.h
@@ -132,4 +132,11 @@ void strbuf_worktree_ref(const struct worktree *wt,
 const char *worktree_ref(const struct worktree *wt,
 			 const char *refname);
 
+/*
+ * Return the path to config file that can contain worktree-specific
+ * config (or NULL in unsupported setups). The caller must free the
+ * return value.
+ */
+char *get_worktree_config(struct repository *r);
+
 #endif
-- 
2.20.0.482.g66447595a7


  parent reply	other threads:[~2018-12-27 15:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-27 15:56 [PATCH 0/6] Add "git config --move-to" Nguyễn Thái Ngọc Duy
2018-12-27 15:56 ` [PATCH 1/6] config.c: avoid git_path() in do_git_config_sequence() Nguyễn Thái Ngọc Duy
2019-01-10 21:38   ` Junio C Hamano
2018-12-27 15:56 ` Nguyễn Thái Ngọc Duy [this message]
2018-12-27 20:36   ` [PATCH 2/6] worktree.c: add get_worktree_config() Eric Sunshine
2018-12-27 15:56 ` [PATCH 3/6] config.c: add repo_config_set_worktree_gently() Nguyễn Thái Ngọc Duy
2019-04-30 16:40   ` Derrick Stolee
2019-05-01  9:55     ` Duy Nguyen
2018-12-27 15:56 ` [PATCH 4/6] config: use OPT_FILENAME() Nguyễn Thái Ngọc Duy
2018-12-27 20:40   ` Eric Sunshine
2018-12-27 15:56 ` [PATCH 5/6] config: factor out set_config_source_file() Nguyễn Thái Ngọc Duy
2018-12-27 15:56 ` [PATCH 6/6] config: add --move-to Nguyễn Thái Ngọc Duy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181227155611.10585-3-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).