From patchwork Thu Dec 27 15:56:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10743823 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 68662924 for ; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5743528A55 for ; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48EB628A99; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3CE8328A55 for ; Thu, 27 Dec 2018 15:56:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728604AbeL0P4Y (ORCPT ); Thu, 27 Dec 2018 10:56:24 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:33227 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728345AbeL0P4Y (ORCPT ); Thu, 27 Dec 2018 10:56:24 -0500 Received: by mail-lj1-f196.google.com with SMTP id v1-v6so16624374ljd.0 for ; Thu, 27 Dec 2018 07:56:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mZ0Ofw8sweD/19NW/wZFG0bPF1tm+6oPRGzNIZ2chJY=; b=UKMRChEEAJhxV1GbCv50jiSNax2ysC2tGqmvXAcknknoP8mGV9bMLdnNcfkMy/UnuV +F+Dcu+gke/bQlTxya0QNe5GsDwXHIbVcV94h6yM4NPPTQ7gUbO96oCo9cGelHvL6Bux mmHZ3V2Zwsa4hlcqj7BwhYheWFtK3TZ1qIwniNjV30aCI/em9qcXkA7LAnCEGHdefd7V 62fpSAU/ncFSsF0BWArLlArk+viX0Pb6zHkcIjjM6OEGhTeOUdtALEoANih7kj89sUsa sQhbFtu41DtT4CW6E21sts7aiccEtjJKOVCIsQLAda4EfVI0O1JMBQPqMw8XP9xEjW0q iXlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mZ0Ofw8sweD/19NW/wZFG0bPF1tm+6oPRGzNIZ2chJY=; b=BXucd2t1GCWkyTr2AlLljhsbKjhyphz6aBYXbUD82SOw5PIPyeT7AcGJQhZ6OsDuWY 8t4lp2KVDR/uNPGg1zRZR11sKdoefDYvq0aqOJUrwTjnUDgmyqjyNkBY6J/Eviyn05or xro/yFVKqxqBVWO1eKm9yec6lfR59gkGQn+lLdDhaaikh51NYtTinmoL37ipmdKeRslC j7+1Tq7vTfK9gtNZjk7SBjY8oDjgcfPW9tMfM7IGiS8VQzLEJJQSgs90MP1///LPd8L0 yIVuJKESLg2poQ1Drz/9BWWwHCn6By3nmaL0Q5A+4FRLfB/G+RPNnzAumSCxBy+h2EWX 1y2Q== X-Gm-Message-State: AJcUukfGNu2gQ8YeIQhm9nT5LEt+7vwDLMz02R38gxfooUz7rN8ktcGJ H1gjbXZ3tb1DO8X4KCRDBJ9yHTCe X-Google-Smtp-Source: ALg8bN6W/I6/Sd7fm2CldcmAA96AiRrMndjaw+ZX0pb2dKNP3DZ7NXL8TA8T+XYQqMwkw0fA6JtLuA== X-Received: by 2002:a2e:3a04:: with SMTP id h4-v6mr13739773lja.81.1545926182132; Thu, 27 Dec 2018 07:56:22 -0800 (PST) Received: from localhost.localdomain (c80-216-2-165.bredband.comhem.se. [80.216.2.165]) by smtp.gmail.com with ESMTPSA id p77-v6sm9095367lja.0.2018.12.27.07.56.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 07:56:21 -0800 (PST) From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= Subject: [PATCH 1/6] config.c: avoid git_path() in do_git_config_sequence() Date: Thu, 27 Dec 2018 16:56:06 +0100 Message-Id: <20181227155611.10585-2-pclouds@gmail.com> X-Mailer: git-send-email 2.20.0.482.g66447595a7 In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com> References: <20181227155611.10585-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This function has both $GIT_COMMON_DIR and $GIT_DIR in "opts". Use it to construct config.worktree path instead because git_pathdup() is tied to the current worktree, but the given $GIT_DIR could be from another one. Signed-off-by: Nguyễn Thái Ngọc Duy --- config.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/config.c b/config.c index ff521eb27a..79fbe65da8 100644 --- a/config.c +++ b/config.c @@ -1665,6 +1665,7 @@ static int do_git_config_sequence(const struct config_options *opts, char *xdg_config = xdg_config_home("config"); char *user_config = expand_user_path("~/.gitconfig", 0); char *repo_config; + char *worktree_config; if (opts->commondir) repo_config = mkpathdup("%s/config", opts->commondir); @@ -1672,6 +1673,10 @@ static int do_git_config_sequence(const struct config_options *opts, BUG("git_dir without commondir"); else repo_config = NULL; + if (repository_format_worktree_config) + worktree_config = mkpathdup("%s/config.worktree", opts->git_dir); + else + worktree_config = NULL; current_parsing_scope = CONFIG_SCOPE_SYSTEM; if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) @@ -1693,12 +1698,8 @@ static int do_git_config_sequence(const struct config_options *opts, * Note: this should have a new scope, CONFIG_SCOPE_WORKTREE. * But let's not complicate things before it's actually needed. */ - if (repository_format_worktree_config) { - char *path = git_pathdup("config.worktree"); - if (!access_or_die(path, R_OK, 0)) - ret += git_config_from_file(fn, path, data); - free(path); - } + if (worktree_config && !access_or_die(worktree_config, R_OK, 0)) + ret += git_config_from_file(fn, worktree_config, data); current_parsing_scope = CONFIG_SCOPE_CMDLINE; if (git_config_from_parameters(fn, data) < 0) @@ -1708,6 +1709,7 @@ static int do_git_config_sequence(const struct config_options *opts, free(xdg_config); free(user_config); free(repo_config); + free(worktree_config); return ret; } From patchwork Thu Dec 27 15:56:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10743825 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A7E9814E2 for ; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 997C528AA0 for ; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8DE8628A99; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5CC4B28A5C for ; Thu, 27 Dec 2018 15:56:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728672AbeL0P40 (ORCPT ); Thu, 27 Dec 2018 10:56:26 -0500 Received: from mail-lf1-f68.google.com ([209.85.167.68]:41076 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727119AbeL0P40 (ORCPT ); Thu, 27 Dec 2018 10:56:26 -0500 Received: by mail-lf1-f68.google.com with SMTP id c16so12939233lfj.8 for ; Thu, 27 Dec 2018 07:56:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yylg1JvDqyyibpABxJekLON5IaqWfcRcRAWlsUaJ3ug=; b=fsIOlgRH8vc4YpAobYK2FtIgPOo2z5SCF5PZMYUn4SQYlmrQr1pNCfhBgUP6WQZhLb XfcsHK5VurWMBVPJbHjjNnzqxlHJM1bFRlJKHCUcp9yjZYJTp9cOi8L8GNxT+yd94vTd lhzn6WdI2HcSbcWIStlJxr0QlsPupVqYCjjMeXWNbqRun/tvQIhOrSi+BwHZHyNLyxFb ZJ9p1KLWFYAdLS3xXM2q9HGWEEE8sdocOClGT6E9L5rGDAKyM6lNBuSo/vFwtxd/tvXY QswjRZdrePgkFfvfgiGj2XYO2xNskjaZnmu0uN0Tohoopky/uVFgo0qcgHTD8elcMfyg iaEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yylg1JvDqyyibpABxJekLON5IaqWfcRcRAWlsUaJ3ug=; b=RykIg235R4oNfDNndlYlgy6PIMIDirfptAO7N1iQONswJ9SxWkEMBBVaVKn+/imTPa 1sGm7TbKZIJsnQwTElZW3TmFUn1RuGQuYAaUhgfyTmwYbcXUcO213zrWrALjfKH/9Fne XA1NfF07p/mvH7mgmopiM6hPvsw+z0OyP/TDGlbB0+YloN8Pr6Id4TTiloM7L2jCsUJl sR97l8hS150Xmg7XohP/OEUm8j3u8YhpGZtwp2dvxIUsW5HiHtNy43d2exRXbvVWMKU0 EnOphnWy44lRr3jJkNZl8hpDjGqIkadYDmmlqjZtMNvnlfvWSJMXNAimxECBge0jlFUx Vf3w== X-Gm-Message-State: AA+aEWbOL/aveqdL6uHEiUsFldloDU8b6SCT1xXsdydp5Ar7NY4kZR8L Tiqj56VrRcgIosj5UqwEo+DHLhLF X-Google-Smtp-Source: AFSGD/Vnx4m46aX4VEseDLRm8Hcmo7eurJytfgDl1q11+LX9EKeqZtRjZ3tMCnneoFxuHurxqUorpQ== X-Received: by 2002:a19:982:: with SMTP id 124mr11774246lfj.138.1545926183310; Thu, 27 Dec 2018 07:56:23 -0800 (PST) Received: from localhost.localdomain (c80-216-2-165.bredband.comhem.se. [80.216.2.165]) by smtp.gmail.com with ESMTPSA id p77-v6sm9095367lja.0.2018.12.27.07.56.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 07:56:22 -0800 (PST) From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= Subject: [PATCH 2/6] worktree.c: add get_worktree_config() Date: Thu, 27 Dec 2018 16:56:07 +0100 Message-Id: <20181227155611.10585-3-pclouds@gmail.com> X-Mailer: git-send-email 2.20.0.482.g66447595a7 In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com> References: <20181227155611.10585-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP "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 --- 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 From patchwork Thu Dec 27 15:56:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10743831 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 333E2924 for ; Thu, 27 Dec 2018 15:56:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 23CF128A5C for ; Thu, 27 Dec 2018 15:56:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1842D28A70; Thu, 27 Dec 2018 15:56:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7D00728A6A for ; Thu, 27 Dec 2018 15:56:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728697AbeL0P41 (ORCPT ); Thu, 27 Dec 2018 10:56:27 -0500 Received: from mail-lf1-f65.google.com ([209.85.167.65]:32996 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728345AbeL0P40 (ORCPT ); Thu, 27 Dec 2018 10:56:26 -0500 Received: by mail-lf1-f65.google.com with SMTP id i26so12969305lfc.0 for ; Thu, 27 Dec 2018 07:56:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xoNspx1SVuQq/CMHLtJnq85EztydcnPT5Zt8xd+1QvE=; b=ONrVmn/DmV93siU8qwK2S+nGMDJLUW8iAff67E87mMcOyMOjyVdfq5nev3OhzojcdW Ax/W74BedivrrZOBiP+vUV4NqSQ4DURCOBDWcNSRbPZZ2RPBi+Y+3UtXltq9FTMLvBJl NQtekz351GenQ6IV/imrhZgYgJ8LcJj6NbK9HLPndGLPwozbu3xzgJqP9DqpMxbD5Iqz ZKh8EVGQSG8TjN/jFokALDL0YfKdsWQLHZIYEksq3mSaMAdw1V1eFUYSXvO4q3ylscd7 sLMy4EvJuRlSFNYRswl22XLnNmQ/0WJnbuzCWeSoXuK2B5E+5sLxzKkOFK86HAfW4Jee /URA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xoNspx1SVuQq/CMHLtJnq85EztydcnPT5Zt8xd+1QvE=; b=Rylovl17z2dF9lnf2dHOld9nOZIXj6JiLjgB130deaMzZugDZ01M0abpS4CCcdmmRN YZ9MglP8p1eabCE4j+c15wvOpw4XYOLMZXx9vquchzQTD5u1ltBSLsI75bWZRJ3cNoHD L2jWO99y3XjaYI1iLuE9Pc72IUwi0cJzQkG/873CDv5HioH56RbfIyEXv3BZsw9tis9s lCjqHoxbJ+C5zC1JgmAp8g8RlRdiVbW0yxXVF4dXurslxWAEFacQqSYVQ4xE9fG42fcQ n0dlfAIX9ecvmj1wy29UFKIXOO1oPw5LSy2kY2/7CGDFtF+f03jxt1/l1Yg5JueBSq6O PkKA== X-Gm-Message-State: AA+aEWZc3Lxq5bmxArNDbRsP/Pc4vzhP5it08rv+CfserRZljlQWV/iM bxPvVPWu9ZDf5NaMESQbQZv3p0MF X-Google-Smtp-Source: AFSGD/UrvCYNg8V0422qFN8zaqRJ/ts1Sw3jjigJv4uOX/YDrNC4gMdRwIM2Ei1DGNmTBbBa313gqA== X-Received: by 2002:a19:c4cc:: with SMTP id u195mr11878055lff.141.1545926184447; Thu, 27 Dec 2018 07:56:24 -0800 (PST) Received: from localhost.localdomain (c80-216-2-165.bredband.comhem.se. [80.216.2.165]) by smtp.gmail.com with ESMTPSA id p77-v6sm9095367lja.0.2018.12.27.07.56.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 07:56:23 -0800 (PST) From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= Subject: [PATCH 3/6] config.c: add repo_config_set_worktree_gently() Date: Thu, 27 Dec 2018 16:56:08 +0100 Message-Id: <20181227155611.10585-4-pclouds@gmail.com> X-Mailer: git-send-email 2.20.0.482.g66447595a7 In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com> References: <20181227155611.10585-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is C equivalent of "git config --set --worktree". In other words, it will - write to $GIT_DIR/config in single-worktree setup - write to $GIT_COMMON_DIR/worktrees//config.worktree or $GIT_COMMON_DIR/config.worktree (for main worktree) if extensions.worktreeConfig is enabled - return error in multiple-worktree setup if extensions.worktreeConfig is not enabled. While at there, also add repo_config_set*() for writing to $GIT_COMMON_DIR/config. Note, since git_config_set_multivar_in_file_gently() only invalidates the config set of the_repository, anybody who uses these functions on submodules must do additional invalidation if needed. Signed-off-by: Nguyễn Thái Ngọc Duy --- config.c | 41 ++++++++++++++++++++++++++++++++++++++++- config.h | 3 +++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index 79fbe65da8..151d28664e 100644 --- a/config.c +++ b/config.c @@ -19,6 +19,7 @@ #include "utf8.h" #include "dir.h" #include "color.h" +#include "worktree.h" struct config_source { struct config_source *prev; @@ -2137,6 +2138,39 @@ int repo_config_get_pathname(struct repository *repo, return ret; } +int repo_config_set_gently(struct repository *r, + const char *key, const char *value) +{ + char *path = repo_git_path(r, "config"); + int ret = git_config_set_multivar_in_file_gently(path, key, value, NULL, 0); + free(path); + return ret; +} + +void repo_config_set(struct repository *r, const char *key, const char *value) +{ + if (!repo_config_set_gently(r, key, value)) + return; + if (value) + die(_("could not set '%s' to '%s'"), key, value); + else + die(_("could not unset '%s'"), key); +} + +int repo_config_set_worktree_gently(struct repository *r, + const char *key, const char *value) +{ + char *path; + int ret; + + path = get_worktree_config(r); + if (!path) + return CONFIG_INVALID_FILE; + ret = git_config_set_multivar_in_file_gently(path, key, value, NULL, 0); + free(path); + return ret; +} + /* Functions used historically to read configuration from 'the_repository' */ void git_config(config_fn_t fn, void *data) { @@ -2912,7 +2946,12 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, ret = 0; - /* Invalidate the config cache */ + /* + * Invalidate the config cache + * + * NEEDSWORK: invalidate _all_ existing config caches, not + * just one from the_repository + */ git_config_clear(); out_free: diff --git a/config.h b/config.h index ee5d3fa7b4..62204dc252 100644 --- a/config.h +++ b/config.h @@ -103,6 +103,9 @@ extern int git_config_color(char *, const char *, const char *); extern int git_config_set_in_file_gently(const char *, const char *, const char *); extern void git_config_set_in_file(const char *, const char *, const char *); extern int git_config_set_gently(const char *, const char *); +extern int repo_config_set_gently(struct repository *, const char *, const char *); +extern void repo_config_set(struct repository *, const char *, const char *); +extern int repo_config_set_worktree_gently(struct repository *, const char *, const char *); extern void git_config_set(const char *, const char *); extern int git_config_parse_key(const char *, char **, int *); extern int git_config_key_is_valid(const char *key); From patchwork Thu Dec 27 15:56:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10743827 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BE5EA6C5 for ; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC86128A5C for ; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A10E228AA5; Thu, 27 Dec 2018 15:56:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9D48B28A70 for ; Thu, 27 Dec 2018 15:56:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728737AbeL0P43 (ORCPT ); Thu, 27 Dec 2018 10:56:29 -0500 Received: from mail-lf1-f67.google.com ([209.85.167.67]:37520 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727119AbeL0P42 (ORCPT ); Thu, 27 Dec 2018 10:56:28 -0500 Received: by mail-lf1-f67.google.com with SMTP id y11so12935914lfj.4 for ; Thu, 27 Dec 2018 07:56:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o64bVCKDcbO/ZCi3MFM78wduVi4qIRQzD91TRghdnck=; b=oZXHOliVg2xmTTB51EltBJv5L0G1fvv6nxviXNH7r4uIorJnH5DYHmpAsBUlUyP7+I pB8YVvuDXMOvcKgEDB0OZdhuvNpna47g8dDOOhfq15J5M2LqkWXGFEBkZbgGr6n4zD9V s/xdzro8o7aviOxP2J/qnEKwzb8pTWU6Wjvbp2MC8F7p0q45v5YakQQ+T/O515uj19nh 64XatrxvHUZJAGxYmFrWlWwnmlrz/GIbBFm5K7QOsvDEhiEKf0kUvg7FO1qHNQkW46B5 6YSZ7kg4o5BoFqJ3oV6tEKF68ZZVCuSlRs43nMSYuKMUeF/UODpgXg0dQVp8gsZNcqmg Nq6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o64bVCKDcbO/ZCi3MFM78wduVi4qIRQzD91TRghdnck=; b=P3OGD9ii6F5ku/l8NbKCvNxfbnnpKVp8cLkTu1jpqh518n1yS3doTnDmC+wM65W1iF 9wGo4uzOAW6mPR0QbifzRLUgOztLBuA2IUjus9UnEruINU92PqZIcOpENPuzP4TTZRxX w1eWpCN7SorWfoDqE++TiTqaJN/vH0CW4bFfu/m28+mBJYkUXrc86oE4AGJxfgXf/hkL ZN72vLwslEvoXulx3JTlfTseHbYqQTohBeq5qsVrgl6nKHGploMW3nfYFI2ungcFNoOJ m0luKjpfnhxonIHEORbOE4izEmN9HMbUREum4PFYhxvfEiXv72lvXZF3hEZr8ZPif5XI Ewtw== X-Gm-Message-State: AA+aEWa3Qi/wC5b8aqs8YbETn5OaJ2A9a2n0ZTKR5Wilr345HgotVoFu MgNr/h8RN8OKeyL04W65gVWyqDoU X-Google-Smtp-Source: AFSGD/X1tjcJ/YNTnlrm16LNxj4B1SQdbDZEDsFWTZSE/wopkZZA/zvqQi1KA3QCqUwTtvNluqGDAQ== X-Received: by 2002:a19:5ad0:: with SMTP id y77mr13057739lfk.109.1545926185577; Thu, 27 Dec 2018 07:56:25 -0800 (PST) Received: from localhost.localdomain (c80-216-2-165.bredband.comhem.se. [80.216.2.165]) by smtp.gmail.com with ESMTPSA id p77-v6sm9095367lja.0.2018.12.27.07.56.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 07:56:24 -0800 (PST) From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= Subject: [PATCH 4/6] config: use OPT_FILENAME() Date: Thu, 27 Dec 2018 16:56:09 +0100 Message-Id: <20181227155611.10585-5-pclouds@gmail.com> X-Mailer: git-send-email 2.20.0.482.g66447595a7 In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com> References: <20181227155611.10585-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Do not handle prefix directly. It's simpler to use OPT_FILENAME() instead. The othe reason for doing this is because this code (where the deleted code is) will be factored out and called when "prefix" is not available. Signed-off-by: Nguyễn Thái Ngọc Duy --- builtin/config.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index 771cfa54bd..c22d25de12 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -126,7 +126,7 @@ static struct option builtin_config_options[] = { OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")), - OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), + OPT_FILENAME('f', "file", &given_config_source.file, N_("use given config file")), OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")), OPT_GROUP(N_("Action")), OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), @@ -657,10 +657,6 @@ int cmd_config(int argc, const char **argv, const char *prefix) "extension worktreeConfig is enabled. " "Please read \"CONFIGURATION FILE\"\n" "section in \"git help worktree\" for details")); - } else if (given_config_source.file) { - if (!is_absolute_path(given_config_source.file) && prefix) - given_config_source.file = - prefix_filename(prefix, given_config_source.file); } if (respect_includes_opt == -1) From patchwork Thu Dec 27 15:56:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10743833 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 47CC4924 for ; Thu, 27 Dec 2018 15:56:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3756A28A5C for ; Thu, 27 Dec 2018 15:56:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2BEE828A78; Thu, 27 Dec 2018 15:56:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A468628A5C for ; Thu, 27 Dec 2018 15:56:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728799AbeL0P4a (ORCPT ); Thu, 27 Dec 2018 10:56:30 -0500 Received: from mail-lf1-f68.google.com ([209.85.167.68]:41078 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728698AbeL0P43 (ORCPT ); Thu, 27 Dec 2018 10:56:29 -0500 Received: by mail-lf1-f68.google.com with SMTP id c16so12939323lfj.8 for ; Thu, 27 Dec 2018 07:56:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/gLdF5h7tDbN4ZmeD3YbmvkF14vugxwX36anj2TBgPs=; b=AFo5r+492hGm5+wii5e7/PK22LmyuxCM58EBfckBTck4BiWizI9bRrKFtZcmpuZdTp 0QK3tC0gtiZ2GNfbiJmw+hVvDGnlpkTomaaf7Yw9FLqDm28jnTeX97lDiBXXlXuJD9rK 0OV2nJLgSHJcOltY3Am/OdWtJDzFJsOQ2wg7QW4fclK924y607PsnkWNePeJoOEhSna1 9W657lYLbmc50AW2m8VCpXpy3Mqp24dLpZRhOU+RSBt5p2PIVuzmIRYWetGScOl0fiDr l5TcQunr0iH90krGQI+rbhWkmhG3JOuZt7QQ+NF8K1sjLCWClWLHqjzYXf8bdbm1vbcN 7dNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/gLdF5h7tDbN4ZmeD3YbmvkF14vugxwX36anj2TBgPs=; b=frKEgBVFB7K4cgMetbvqKHJeORFDFvJRrMd6WTW1u4oVw6/BJHrprdxX/jMqBBT188 UAQ6rhG8sqhVclne0NxFJAYm5ASvwSq33h1B1vZpd0GjeIWN+pLpreC3sfQUuXWdpxLp YNnaTdcqjhnslcAAhHg7Prze/V+un09k0FpUzS0ctx9GDEvFvdD2dU8GlhO1Tep3n3UL gGgp+vHhcJ4SPVC50gyAomezoyJkzVbVf2GcBMjxALjDJuBO5rKP5jPtzZkIh7dY/fv6 YYy8P/ckmqhZTFLTHfDWLOZFqCNS9ehWd2lec5oDbOfiyZOe3jL+7BidkQ1iJWQAbNrt /cvQ== X-Gm-Message-State: AA+aEWaPooREoBRkNUkg1i0glEW5E28EixpFXN3IHUPJDsdr2NWzyW3d WZ9lIc6O/wz+uqITWwiGWgnHs52a X-Google-Smtp-Source: AFSGD/UF1y8w/vMHBZwQlIVjpBc9CAJw6LHQj82B3ey0KCtrCDKrGl8RAKsraHQdOm553YJS4r/d1A== X-Received: by 2002:a19:1d0d:: with SMTP id d13mr11866518lfd.74.1545926186711; Thu, 27 Dec 2018 07:56:26 -0800 (PST) Received: from localhost.localdomain (c80-216-2-165.bredband.comhem.se. [80.216.2.165]) by smtp.gmail.com with ESMTPSA id p77-v6sm9095367lja.0.2018.12.27.07.56.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 07:56:25 -0800 (PST) From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= Subject: [PATCH 5/6] config: factor out set_config_source_file() Date: Thu, 27 Dec 2018 16:56:10 +0100 Message-Id: <20181227155611.10585-6-pclouds@gmail.com> X-Mailer: git-send-email 2.20.0.482.g66447595a7 In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com> References: <20181227155611.10585-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In the next patch, "config" is taught to move some config variables from one file to another. We need to specify two locations, source and destination, and the plan is reusing the same --global/--local/... option. Factor this code out for reuse later. Signed-off-by: Nguyễn Thái Ngọc Duy --- builtin/config.c | 113 +++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index c22d25de12..61a6a5a0e1 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -71,6 +71,64 @@ static int show_origin; static NORETURN void usage_builtin_config(void); +static void set_config_source_file(void) +{ + int nongit = !startup_info->have_repository; + + if (use_global_config + use_system_config + use_local_config + + use_worktree_config + + !!given_config_source.file + !!given_config_source.blob > 1) + die(_("only one config file at a time")); + + if (use_local_config && nongit) + die(_("--local can only be used inside a git repository")); + + if (given_config_source.blob && nongit) + die(_("--blob can only be used inside a git repository")); + + if (given_config_source.file && + !strcmp(given_config_source.file, "-")) { + given_config_source.file = NULL; + given_config_source.use_stdin = 1; + } + + if (use_global_config) { + char *user_config = expand_user_path("~/.gitconfig", 0); + char *xdg_config = xdg_config_home("config"); + + if (!user_config) + /* + * It is unknown if HOME/.gitconfig exists, so + * we do not know if we should write to XDG + * location; error out even if XDG_CONFIG_HOME + * is set and points at a sane location. + */ + die(_("$HOME not set")); + + if (access_or_warn(user_config, R_OK, 0) && + xdg_config && !access_or_warn(xdg_config, R_OK, 0)) { + given_config_source.file = xdg_config; + free(user_config); + } else { + given_config_source.file = user_config; + free(xdg_config); + } + } + else if (use_system_config) + given_config_source.file = git_etc_gitconfig(); + else if (use_local_config) + given_config_source.file = git_pathdup("config"); + else if (use_worktree_config) { + 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")); + } +} + static int option_parse_type(const struct option *opt, const char *arg, int unset) { @@ -604,60 +662,7 @@ int cmd_config(int argc, const char **argv, const char *prefix) builtin_config_usage, PARSE_OPT_STOP_AT_NON_OPTION); - if (use_global_config + use_system_config + use_local_config + - use_worktree_config + - !!given_config_source.file + !!given_config_source.blob > 1) { - error(_("only one config file at a time")); - usage_builtin_config(); - } - - if (use_local_config && nongit) - die(_("--local can only be used inside a git repository")); - - if (given_config_source.blob && nongit) - die(_("--blob can only be used inside a git repository")); - - if (given_config_source.file && - !strcmp(given_config_source.file, "-")) { - given_config_source.file = NULL; - given_config_source.use_stdin = 1; - } - - if (use_global_config) { - char *user_config = expand_user_path("~/.gitconfig", 0); - char *xdg_config = xdg_config_home("config"); - - if (!user_config) - /* - * It is unknown if HOME/.gitconfig exists, so - * we do not know if we should write to XDG - * location; error out even if XDG_CONFIG_HOME - * is set and points at a sane location. - */ - die(_("$HOME not set")); - - if (access_or_warn(user_config, R_OK, 0) && - xdg_config && !access_or_warn(xdg_config, R_OK, 0)) { - given_config_source.file = xdg_config; - free(user_config); - } else { - given_config_source.file = user_config; - free(xdg_config); - } - } - else if (use_system_config) - given_config_source.file = git_etc_gitconfig(); - else if (use_local_config) - given_config_source.file = git_pathdup("config"); - else if (use_worktree_config) { - 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")); - } + set_config_source_file(); if (respect_includes_opt == -1) config_options.respect_includes = !given_config_source.file; From patchwork Thu Dec 27 15:56:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Duy Nguyen X-Patchwork-Id: 10743835 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E348A6C5 for ; Thu, 27 Dec 2018 15:56:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D27AB28A49 for ; Thu, 27 Dec 2018 15:56:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C68E228A55; Thu, 27 Dec 2018 15:56:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF60428A6A for ; Thu, 27 Dec 2018 15:56:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728840AbeL0P4d (ORCPT ); Thu, 27 Dec 2018 10:56:33 -0500 Received: from mail-lj1-f194.google.com ([209.85.208.194]:39747 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727119AbeL0P4c (ORCPT ); Thu, 27 Dec 2018 10:56:32 -0500 Received: by mail-lj1-f194.google.com with SMTP id t9-v6so16596229ljh.6 for ; Thu, 27 Dec 2018 07:56:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=42pZY4VLjNgy7X3rKRZvSf7imxZdopgZILKrP3V7QlI=; b=r6XOwlT9nS6OtjSBE9+UDdxJQi1DbJpryAja8SReVm1KKHKFk95wQKflvKSXa2ExYa RvGpK0oIcnyUCuNZS3DLrXstweVBzajjETHSjJN2OYunjFrb+mJR6lBlaiuEqOT8srVY YY8zV2bECgoCFOIC5KILwyssLnXqK6cNF38jkvUC/A8AiXLYOnYSBBLUSWkeFQMzwHko RWkF1UC6rmsKX7/60GKQk+xlkCThDUsaPYReLzJgjiytiKQiYw5sUkWE2rrQ4mKC0ljv H4Hf9RsSQBo5T20gOrClOG3EPV2KFMEnTadk9Fy9ZTSfT5+YG+z0y1WsRbFquiIIGVIH I0KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=42pZY4VLjNgy7X3rKRZvSf7imxZdopgZILKrP3V7QlI=; b=qWgi+yYKlH3U0HCvndGe9bK+McnQzBFEZyDCvh6FRTusU1PvdKA+1r6f44gpOSt9EG L1mvdke2VD6k/deq/iz1PqBqPuqEPwhG6r95EfVjSi47vYGRbndhIIvLyfPfNDx5ED9y DeOXMijDnERMlpq1P79TwWpkkqMQbbipaPkSxyl84tlpp9PbThjouZs91W3eaEtgiUGC CMEdAuH5ib/fF0eremlelwACOqi7IuJ4CR7fSk9KBG97C599moRSdOAsN/tYmYoMUnRZ 0sMNCrJmefXs+ugcte5rBoYp3Pd4CvfYaeVUx50ogaKOCqR+Mb/8C55UL6qNu8h984Pv Tapw== X-Gm-Message-State: AJcUuketWReIrDhUiO2z+jqaonTcRaS5UmDQuokq4+3Je2PpCYrugBEq I+Mok7nRau+fSr7ylssiYx/FDS9o X-Google-Smtp-Source: ALg8bN54uKRoNwfCNg6TIpps/2ekI4nfEGU7N7mxdKU21gMsomdXzml3+neX3uoIpDGHHAPhPd/M+A== X-Received: by 2002:a2e:2d4:: with SMTP id y81-v6mr14102390lje.62.1545926188126; Thu, 27 Dec 2018 07:56:28 -0800 (PST) Received: from localhost.localdomain (c80-216-2-165.bredband.comhem.se. [80.216.2.165]) by smtp.gmail.com with ESMTPSA id p77-v6sm9095367lja.0.2018.12.27.07.56.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Dec 2018 07:56:27 -0800 (PST) From: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= To: git@vger.kernel.org Cc: =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmfhu41jIER1eQ==?= Subject: [PATCH 6/6] config: add --move-to Date: Thu, 27 Dec 2018 16:56:11 +0100 Message-Id: <20181227155611.10585-7-pclouds@gmail.com> X-Mailer: git-send-email 2.20.0.482.g66447595a7 In-Reply-To: <20181227155611.10585-1-pclouds@gmail.com> References: <20181227155611.10585-1-pclouds@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This option can be used to move one or multiple variables from one place to another, e.g. to move some aliases from repo config to user config, or from repo config to per-worktree config. This will be useful for moving config variables around when extensions.worktreeConfig is enabled. E.g. git config --local --move-to --worktree core.worktree git config --local --move-glob-to --worktree 'submodule.*.*' The implementation is definitely not the best. We could for example lock both source and destination files before doing any update, and perhaps edit these files just once instead of once per key. But it adds a lot more complication to config update code. Let's stay with something simple for now. It's not worse than scripting using "git config". Optimization could be done later. Signed-off-by: Nguyễn Thái Ngọc Duy --- Documentation/git-config.txt | 12 ++++ Documentation/git-worktree.txt | 16 +++-- builtin/config.c | 120 +++++++++++++++++++++++++++++++++ t/t1300-config.sh | 54 +++++++++++++++ 4 files changed, 196 insertions(+), 6 deletions(-) diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 1bfe9f56a7..495bb57416 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -20,6 +20,9 @@ SYNOPSIS 'git config' [] --unset-all name [value_regex] 'git config' [] --rename-section old_name new_name 'git config' [] --remove-section name +'git config' [] --move-to name +'git config' [] --move-regexp-to name-regexp +'git config' [] --move-glob-to name-glob 'git config' [] [--show-origin] [-z|--null] [--name-only] -l | --list 'git config' [] --get-color name [default] 'git config' [] --get-colorbool name [stdout-is-tty] @@ -161,6 +164,15 @@ See also <>. --unset-all:: Remove all lines matching the key from config file. +--move-to:: +--move-regexp-to:: +--move-glob-to:: + Move a config variable (or multiple variables matching the + given regular expression or glob pattern) to a new file. Any + option about the config file location after `--move-to` or + `--move-to-regexp` specifies the move destination. Existing + config of the same name remains. + -l:: --list:: List all variables set in config file, along with their values. diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index cb86318f3e..aae8e1d8b2 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -252,13 +252,17 @@ rev-parse --git-path config.worktree`. You can add or update configuration in this file with `git config --worktree`. Older Git versions will refuse to access repositories with this extension. -Note that in this file, the exception for `core.bare` and `core.worktree` -is gone. If you have them in $GIT_DIR/config before, you must move -them to the `config.worktree` of the main working tree. You may also -take this opportunity to review and move other configuration that you -do not want to share to all working trees: +Note that in this file, the exception for `core.bare` and +`core.worktree` is gone. If you have them in $GIT_DIR/config before, +you must move them to the `config.worktree` of the main working tree. - - `core.worktree` and `core.bare` should never be shared +------------ +$ git config --local --move-to --worktree core.bare +$ git config --local --move-to --worktree core.worktree +------------ + +You may also take this opportunity to review and move other +configuration that you do not want to share to all working trees: - `core.sparseCheckout` is recommended per working tree, unless you are sure you always use sparse checkout for all working trees. diff --git a/builtin/config.c b/builtin/config.c index 61a6a5a0e1..20266824f3 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -26,6 +26,7 @@ static char term = '\n'; static int use_global_config, use_system_config, use_local_config; static int use_worktree_config; +static struct git_config_source move_source; static struct git_config_source given_config_source; static int actions, type; static char *default_value; @@ -50,6 +51,9 @@ static int show_origin; #define ACTION_GET_COLOR (1<<13) #define ACTION_GET_COLORBOOL (1<<14) #define ACTION_GET_URLMATCH (1<<15) +#define ACTION_MOVE (1<<16) +#define ACTION_MOVE_REGEXP (1<<17) +#define ACTION_MOVE_GLOB (1<<18) /* * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than @@ -178,6 +182,25 @@ static int option_parse_type(const struct option *opt, const char *arg, return 0; } +static int option_move_cb(const struct option *opt, + const char *arg, int unset) +{ + BUG_ON_OPT_NEG(unset); + BUG_ON_OPT_ARG(arg); + + set_config_source_file(); + memcpy(&move_source, &given_config_source, sizeof(move_source)); + + memset(&given_config_source, 0, sizeof(given_config_source)); + use_global_config = 0; + use_system_config = 0; + use_local_config = 0; + use_worktree_config = 0; + + actions = opt->defval; + return 0; +} + static struct option builtin_config_options[] = { OPT_GROUP(N_("Config file location")), OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), @@ -197,6 +220,18 @@ static struct option builtin_config_options[] = { OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), + { OPTION_CALLBACK, 0, "move-to", NULL, NULL, + N_("move a variable to a different config file"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + option_move_cb, ACTION_MOVE }, + { OPTION_CALLBACK, 0, "move-regexp-to", NULL, NULL, + N_("move matching variables to a different config file"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + option_move_cb, ACTION_MOVE_REGEXP }, + { OPTION_CALLBACK, 0, "move-glob-to", NULL, NULL, + N_("move matching variables to a different config file"), + PARSE_OPT_NONEG | PARSE_OPT_NOARG, + option_move_cb, ACTION_MOVE_GLOB }, OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR), @@ -426,6 +461,84 @@ static int get_value(const char *key_, const char *regex_) return ret; } +struct move_config_cb { + struct string_list keys; + const char *key; + regex_t key_re; +}; + +static int collect_move_config(const char *key, const char *value, void *cb) +{ + struct move_config_cb *data = cb; + + switch (actions) { + case ACTION_MOVE: + if (strcasecmp(data->key, key)) + return 0; + break; + case ACTION_MOVE_REGEXP: + if (regexec(&data->key_re, key, 0, NULL, 0)) + return 0; + break; + case ACTION_MOVE_GLOB: + if (wildmatch(data->key, key, WM_CASEFOLD)) + return 0; + break; + default: + BUG("action %d cannot get here", actions); + } + + string_list_append(&data->keys, key)->util = xstrdup(value); + return 0; +} + +static int move_config(const char *key) +{ + struct move_config_cb cb; + int i, ret = 0; + + config_options.respect_includes = 0; + if (!move_source.file && !move_source.use_stdin && !move_source.blob) + die(_("unknown config source")); + + string_list_init(&cb.keys, 1); + cb.key = key; + if (actions == ACTION_MOVE_REGEXP && + regcomp(&cb.key_re, key, REG_EXTENDED | REG_ICASE)) + die(_("invalid key pattern: %s"), key); + + config_with_options(collect_move_config, &cb, + &move_source, &config_options); + + for (i = 0; i < cb.keys.nr && !ret; i++) { + const char *key = cb.keys.items[i].string; + const char *value = cb.keys.items[i].util; + const char *dest = given_config_source.file; + + ret = git_config_set_multivar_in_file_gently( + dest, key, value, CONFIG_REGEX_NONE, 0); + } + + /* + * OK all keys have been copied successfully, time to delete + * old ones + */ + if (!ret && move_source.file) { + for (i = 0; i < cb.keys.nr; i++) { + const char *key = cb.keys.items[i].string; + const char *src = move_source.file; + + git_config_set_multivar_in_file_gently( + src, key, NULL, NULL, 1); + } + } + + string_list_clear(&cb.keys, 1); + if (actions == ACTION_MOVE_REGEXP) + regfree(&cb.key_re); + return ret; +} + static char *normalize_value(const char *key, const char *value) { if (!value) @@ -862,6 +975,13 @@ int cmd_config(int argc, const char **argv, const char *prefix) color_stdout_is_tty = git_config_bool("command line", argv[1]); return get_colorbool(argv[0], argc == 2); } + else if (actions == ACTION_MOVE || + actions == ACTION_MOVE_REGEXP || + actions == ACTION_MOVE_GLOB) { + check_write(); + check_argc(argc, 1, 1); + return move_config(argv[0]); + } return 0; } diff --git a/t/t1300-config.sh b/t/t1300-config.sh index 9652b241c7..6969e6092b 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -1844,4 +1844,58 @@ test_expect_success '--replace-all does not invent newlines' ' test_cmp expect .git/config ' +test_expect_success '--move-to moves keys' ' + test_when_finished rm -f dest && + git config single.foo bar && + git config multi.foo bar1 && + git config --add multi.foo bar2 && + git config --local --move-to -f dest SINGLE.foo && + ! git config single.foo && + test_cmp_config bar -f dest single.foo && + git config --local --move-to -f dest multi.FOO && + ! git config multi.foo && + git config -f dest --get-all multi.foo | sort >actual && + cat >expected <<-\EOF && + bar1 + bar2 + EOF + test_cmp expected actual +' + +test_expect_success '--move-regexp-to moves keys' ' + test_when_finished rm -f dest && + git config single.foo bar && + git config multi.foo bar1 && + git config --add multi.foo bar2 && + git config --local --move-regexp-to -f dest "S.*OO" && + ! git config single.foo && + test_cmp_config bar -f dest single.foo && + git config --local --move-regexp-to -f dest ^multi && + ! git config multi.foo && + git config -f dest --get-all multi.foo | sort >actual && + cat >expected <<-\EOF && + bar1 + bar2 + EOF + test_cmp expected actual +' + +test_expect_success '--move-glob-to moves keys' ' + test_when_finished rm -f dest && + git config single.foo bar && + git config multi.foo bar1 && + git config --add multi.foo bar2 && + git config --local --move-glob-to -f dest "SINGLE.*" && + ! git config single.foo && + test_cmp_config bar -f dest single.foo && + git config --local --move-glob-to -f dest "m*.foo" && + ! git config multi.foo && + git config -f dest --get-all multi.foo | sort >actual && + cat >expected <<-\EOF && + bar1 + bar2 + EOF + test_cmp expected actual +' + test_done