From patchwork Tue Sep 27 13:56:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12990639 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29F60C6FA8E for ; Tue, 27 Sep 2022 13:57:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232160AbiI0N5I (ORCPT ); Tue, 27 Sep 2022 09:57:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232058AbiI0N5G (ORCPT ); Tue, 27 Sep 2022 09:57:06 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 393C713F292 for ; Tue, 27 Sep 2022 06:57:05 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id s14so15225734wro.0 for ; Tue, 27 Sep 2022 06:57:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date; bh=cEZKmdCKUkErF4ZW3Az02OGHYG92oEAhtCIoWG4zI0s=; b=Lt6T/llAWR0qtjw1S0YNXYkO0rLNrSkRXLZ4MgNmMxVpwS4XlD4yte90se1vT6po0o 0IaTzQ4phJG+EuaUBSCky0Z0X+RAoF6fC8ubrymTKn7r82lE/YcqipAy6N3cUMZPvT71 INh/Ppna7bai/ZYOlIQUcVSlrZ8vB2xr44gyc+ceWAkJUQWcOHcBP+qgLYyQXYTtTOoS RSYwWfP83/aFT1FNFvKVtGLSSoJirMKyzmBcNYXKsTW+28U83XQeX/cFyXtjlVi3huu7 QTJnikSiSjAqO6We7XBae6CH/gGeqeOSIKSE/n8HdQW4/u3odJq4FkoZLicvO9Od0g9f +Qug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date; bh=cEZKmdCKUkErF4ZW3Az02OGHYG92oEAhtCIoWG4zI0s=; b=LyIKu07hfp+ZjytteRlCL9QlLv1AALeQ5Bbl6eAqSzYHZ2GfOfQj4DX/SWoDf1r3BM nuyJtIsPuxaTlMi9/PUMOppREXn0P3alTFoguF6JPK40DXCwzp7fppCMZtQoObpG/TVd YuKi1a7+oeorHsb3Sj4dl412+9GkKugvjbWHx4WvHAa6+0eIUUbiOrJHRw/05VAmN4R3 TL9a2bjOTmFT8KV8FmdNYpnZ8ROSS4I7HzejGD+R1WtzJS4Jqys+F8DpoWojVRirDTYK Lj5AiJ80qKX1/Flr3Em0/aF26X9vdIh0KJ9vNqmotl2Oz5g6jp/OtJPSeOSZedgyaKer 4fwA== X-Gm-Message-State: ACrzQf0GTAhK0Wtr2AWIpikrT6J9Dq/hmTmJ0xysX3zJMih9uJctmPGO 0nlNpubYqz18xLHDcesdbOBRlSKH8LY= X-Google-Smtp-Source: AMsMyM60yq9IVMk80gFJEtaQhBYkqk65JiRgs9N5gU1T25OFYNBBZPkT9oWI6bau5b4RwaFhPphIrg== X-Received: by 2002:a05:6000:186d:b0:22a:f6ce:72d9 with SMTP id d13-20020a056000186d00b0022af6ce72d9mr16776122wri.426.1664287023415; Tue, 27 Sep 2022 06:57:03 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e18-20020adffd12000000b0022878c0cc5esm1828863wrr.69.2022.09.27.06.57.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 06:57:03 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 27 Sep 2022 13:56:58 +0000 Subject: [PATCH v4 1/4] maintenance: add 'unregister --force' Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: gitster@pobox.com, vdye@github.com, SZEDER =?utf-8?b?R8OhYm9y?= , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee The 'git maintenance unregister' subcommand has a step that removes the current repository from the multi-valued maitenance.repo config key. This fails if the repository is not listed in that key. This makes running 'git maintenance unregister' twice result in a failure in the second instance. This failure exit code is helpful, but its message is not. Add a new die() message that explicitly calls out the failure due to the repository not being registered. In some cases, users may want to run 'git maintenance unregister' just to make sure that background jobs will not start on this repository, but they do not want to check to see if it is registered first. Add a new '--force' option that will siltently succeed if the repository is not already registered. Also add an extra test of 'git maintenance unregister' at a point where there are no registered repositories. This should fail without --force. Signed-off-by: Derrick Stolee --- Documentation/git-maintenance.txt | 6 +++++- builtin/gc.c | 35 +++++++++++++++++++++++++------ t/t7900-maintenance.sh | 11 +++++++++- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/Documentation/git-maintenance.txt b/Documentation/git-maintenance.txt index 9c630efe19c..bb888690e4d 100644 --- a/Documentation/git-maintenance.txt +++ b/Documentation/git-maintenance.txt @@ -11,7 +11,7 @@ SYNOPSIS [verse] 'git maintenance' run [] 'git maintenance' start [--scheduler=] -'git maintenance' (stop|register|unregister) +'git maintenance' (stop|register|unregister) [] DESCRIPTION @@ -79,6 +79,10 @@ unregister:: Remove the current repository from background maintenance. This only removes the repository from the configured list. It does not stop the background maintenance processes from running. ++ +The `unregister` subcommand will report an error if the current repository +is not already registered. Use the `--force` option to return success even +when the current repository is not registered. TASKS ----- diff --git a/builtin/gc.c b/builtin/gc.c index 2753bd15a5e..dc0ba9e3648 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1519,18 +1519,26 @@ done: } static char const * const builtin_maintenance_unregister_usage[] = { - "git maintenance unregister", + "git maintenance unregister [--force]", NULL }; static int maintenance_unregister(int argc, const char **argv, const char *prefix) { + int force = 0; struct option options[] = { + OPT__FORCE(&force, + N_("return success even if repository was not registered"), + PARSE_OPT_NOCOMPLETE), OPT_END(), }; - int rc; + const char *key = "maintenance.repo"; + int rc = 0; struct child_process config_unset = CHILD_PROCESS_INIT; char *maintpath = get_maintpath(); + int found = 0; + struct string_list_item *item; + const struct string_list *list; argc = parse_options(argc, argv, prefix, options, builtin_maintenance_unregister_usage, 0); @@ -1538,11 +1546,26 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi usage_with_options(builtin_maintenance_unregister_usage, options); - config_unset.git_cmd = 1; - strvec_pushl(&config_unset.args, "config", "--global", "--unset", - "--fixed-value", "maintenance.repo", maintpath, NULL); + list = git_config_get_value_multi(key); + if (list) { + for_each_string_list_item(item, list) { + if (!strcmp(maintpath, item->string)) { + found = 1; + break; + } + } + } + + if (found) { + config_unset.git_cmd = 1; + strvec_pushl(&config_unset.args, "config", "--global", "--unset", + "--fixed-value", key, maintpath, NULL); + + rc = run_command(&config_unset); + } else if (!force) { + die(_("repository '%s' is not registered"), maintpath); + } - rc = run_command(&config_unset); free(maintpath); return rc; } diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh index 2724a44fe3e..96bdd420456 100755 --- a/t/t7900-maintenance.sh +++ b/t/t7900-maintenance.sh @@ -480,6 +480,11 @@ test_expect_success 'maintenance.strategy inheritance' ' test_expect_success 'register and unregister' ' test_when_finished git config --global --unset-all maintenance.repo && + + test_must_fail git maintenance unregister 2>err && + grep "is not registered" err && + git maintenance unregister --force && + git config --global --add maintenance.repo /existing1 && git config --global --add maintenance.repo /existing2 && git config --global --get-all maintenance.repo >before && @@ -493,7 +498,11 @@ test_expect_success 'register and unregister' ' git maintenance unregister && git config --global --get-all maintenance.repo >actual && - test_cmp before actual + test_cmp before actual && + + test_must_fail git maintenance unregister 2>err && + grep "is not registered" err && + git maintenance unregister --force ' test_expect_success !MINGW 'register and unregister with regex metacharacters' ' From patchwork Tue Sep 27 13:56:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12990640 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25973C6FA83 for ; Tue, 27 Sep 2022 13:57:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232248AbiI0N5L (ORCPT ); Tue, 27 Sep 2022 09:57:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232182AbiI0N5H (ORCPT ); Tue, 27 Sep 2022 09:57:07 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 613EC13EAD0 for ; Tue, 27 Sep 2022 06:57:06 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id v28so2301798wrd.3 for ; Tue, 27 Sep 2022 06:57:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date; bh=+wfgInJm6w/BE/avCK/39XOq++2Nf2TlMT5gggZ1o1U=; b=qUGXZWINOtbksg5hfwpSctgavHbBEju8FNrCKNxG26MQemUzPx/3j9oPpxR4NGLCPd bFq+6iBbMMGKW972nifZaVk52yKAqLUxOJWVYMa96pJnAVZHh97cDjB6so9i5637UWps nGP41lL559zAgsyJ5AWyNt7luXYdoG6OfucJCCgxwJsThfxLdcrAC/fCQaPidVHzSlaM r/D4FVWuhi/kPxOgfKrYnUCw/nkOo4uvao/WPrGgsR67pKbnJveSQDQSP/hm9ZAVRFR9 H2ql3P8/CFr1dZElaCNIlnMHwJI2s9kI9wXBSRHpLvs7TEzArLi2/y/7cy+EtnRZ1ttP NOgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date; bh=+wfgInJm6w/BE/avCK/39XOq++2Nf2TlMT5gggZ1o1U=; b=FyRKEg4CG+uhBxBlTanCTbx4fNg5U9ryzlIP3i+A8kMC/oOrLMZuEUExaAXskVN+4w S/iXtd9U1gIzP6KVMdxRnyCapII+wwCoZVkaYFRQDq4i6EkKw8rJJyhQPDi/sGzjl4Uc antYa8qfwST7tbNCJTzDENWmImf/3ewFW5VyZqB/uFD6tQwM3zvHN7n7zD3L+KUn5hUf HLNLx3NkOGseM/1LI+XMnq4l5SyzfuuyNZ+7myfSwPjucE38XPqKDrramT6Z3Des0ZXL x/+iPN2MgOUJrP0owaSp8UKjpdaqkx/truLPhUKBY7PAEvN+V4Rmr8AjEDe9QA5/komb jA4g== X-Gm-Message-State: ACrzQf0Uq+AoD22dEsn7AIQc74DKl+NAIggACoXH56e0vKvilADxDxY7 qDLl7Af+50sUg7rvrOmT1Nw7DkBqHbY= X-Google-Smtp-Source: AMsMyM4EcNV022F7e2L5z9X0GYZ88AfHVKnHl9yIrOeEX31YRo6oQKzlHZPoc4ifoBTun0VGfjK1uQ== X-Received: by 2002:adf:db0d:0:b0:22a:eeed:5145 with SMTP id s13-20020adfdb0d000000b0022aeeed5145mr17095465wri.590.1664287024499; Tue, 27 Sep 2022 06:57:04 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p32-20020a05600c1da000b003a4efb794d7sm2014603wms.36.2022.09.27.06.57.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 06:57:04 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 27 Sep 2022 13:56:59 +0000 Subject: [PATCH v4 2/4] scalar: make 'unregister' idempotent Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: gitster@pobox.com, vdye@github.com, SZEDER =?utf-8?b?R8OhYm9y?= , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee The 'scalar unregister' command removes a repository from the list of registered Scalar repositories and removes it from the list of repositories registered for background maintenance. If the repository was not already registered for background maintenance, then the command fails, even if the repository was still registered as a Scalar repository. After using 'scalar clone' or 'scalar register', the repository would be enrolled in background maintenance since those commands run 'git maintenance start'. If the user runs 'git maintenance unregister' on that repository, then it is still in the list of repositories which get new config updates from 'scalar reconfigure'. The 'scalar unregister' command would fail since 'git maintenance unregister' would fail. Further, the add_or_remove_enlistment() method in scalar.c already has this idempotent nature built in as an expectation since it returns zero when the scalar.repo list already has the proper containment of the repository. The previous change added the 'git maintenance unregister --force' option, so use it within 'scalar unregister' to make it idempotent. Signed-off-by: Derrick Stolee --- scalar.c | 5 ++++- t/t9210-scalar.sh | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/scalar.c b/scalar.c index c5c1ce68919..6de9c0ee523 100644 --- a/scalar.c +++ b/scalar.c @@ -207,7 +207,10 @@ static int set_recommended_config(int reconfigure) static int toggle_maintenance(int enable) { - return run_git("maintenance", enable ? "start" : "unregister", NULL); + return run_git("maintenance", + enable ? "start" : "unregister", + enable ? NULL : "--force", + NULL); } static int add_or_remove_enlistment(int add) diff --git a/t/t9210-scalar.sh b/t/t9210-scalar.sh index 14ca575a214..be51a8bb7a4 100755 --- a/t/t9210-scalar.sh +++ b/t/t9210-scalar.sh @@ -116,7 +116,10 @@ test_expect_success 'scalar unregister' ' test_must_fail git config --get --global --fixed-value \ maintenance.repo "$(pwd)/vanish/src" && scalar list >scalar.repos && - ! grep -F "$(pwd)/vanish/src" scalar.repos + ! grep -F "$(pwd)/vanish/src" scalar.repos && + + # scalar unregister should be idempotent + scalar unregister vanish ' test_expect_success 'set up repository to clone' ' From patchwork Tue Sep 27 13:57:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12990641 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 693BBC6FA83 for ; Tue, 27 Sep 2022 13:57:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232403AbiI0N5U (ORCPT ); Tue, 27 Sep 2022 09:57:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231906AbiI0N5I (ORCPT ); Tue, 27 Sep 2022 09:57:08 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 456A813D84E for ; Tue, 27 Sep 2022 06:57:07 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id r7so15170943wrm.2 for ; Tue, 27 Sep 2022 06:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:fcc:content-transfer-encoding:mime-version:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date; bh=6zWWUieNznYFGW+w3HU3LbxlnW1fIMIzih90XGeB7WI=; b=PremNVakkPlkcciJIterT+ijoxoFyh7ioHOSicwLoAITggLekWzDPIjSBTEjChBVYl 2cNsLX4zQ06MCVGyxiLkCgrpi6yngFrDwkf3+ifgOKoRY0wfKEA3JDyly4KGhVBUlSj0 /5w3GmGuQgmH97sg+bbl0I7dXSfqPZvAcV6xwUsDXXexc00BwiIn9GByIrPvAnSBeeBI h2wEdz5zGE6FtlwTr7R4gssZx0gflnPjBV/RRXdFw8gCtp2uqwjIXWUAIvBIL6L7uDZL l9cI6/rFrzlYW7ziYAKibYvVzvwzWACnk9Iep8GFDZ/svTdTRraQC55fNiweILvyZDeS v2EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:fcc:content-transfer-encoding:mime-version:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date; bh=6zWWUieNznYFGW+w3HU3LbxlnW1fIMIzih90XGeB7WI=; b=skY7Y21yg779Ewt0gkKRA3hVDUmO0+baz298pBHBCpa1fuv1srxz/UyOfafrMr8LsR cMM4MJYVW8FXuiLn6vzJQRUw59Tj1M+Oy3UueUxLGFQBY8Yl7PsEQRmuCWfuEwIS0x1y C6XwhBD+CsNb/mwU24QZoCeG+uFfqPLEgRj8yAbVu5eKGMgkyDJ4jnxaDOYJnBel1FIk Yugdr/NGxOap2smQz+lU/yccxefRt4e7fY89S4L0T9swtXTckxDSeTJHWnFIyt9HRTrV L1QnLEheTDSMSfWhyRP4Td0+o/lzrGiYpKYsNn/tykWu5cys5Cs+PBAttIneIGKEjlb3 YUEw== X-Gm-Message-State: ACrzQf03yC4Vd8eSGcpVq3JGZQI40eBXXdk1B6/LfK0YeoTLJF2T5qtZ iRw/JLNNA98WeFCjohRRo5S3RmlT5fU= X-Google-Smtp-Source: AMsMyM74MaK1dAZYl8hDPnpDE8K+ltBuAsGwgzjM2iDBiYrPAo8ONaKh2g/9/D/ackfeo+xHOWIGSw== X-Received: by 2002:a5d:5846:0:b0:22a:f7d2:9045 with SMTP id i6-20020a5d5846000000b0022af7d29045mr17687819wrf.250.1664287025389; Tue, 27 Sep 2022 06:57:05 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h6-20020a05600c350600b003b491f99a25sm15799023wmq.22.2022.09.27.06.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 06:57:04 -0700 (PDT) Message-Id: <5aa9cc1d6b93b5ad3d66ac01a4518a91ced39bcb.1664287021.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 27 Sep 2022 13:57:00 +0000 Subject: [PATCH v4 3/4] gc: replace config subprocesses with API calls MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: gitster@pobox.com, vdye@github.com, SZEDER =?utf-8?b?R8OhYm9y?= , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee The 'git maintenance [un]register' commands set or unset the multi- valued maintenance.repo config key with the absolute path of the current repository. These are set in the global config file. Instead of calling a subcommand and creating a new process, create the proper API calls to git_config_set_multivar_in_file_gently(). It requires loading the filename for the global config file (and erroring out if now $HOME value is set). We also need to be careful about using CONFIG_REGEX_NONE when adding the value and using CONFIG_FLAGS_FIXED_VALUE when removing the value. In both cases, we check that the value already exists (this check already existed for 'unregister'). Also, remove the transparent translation of the error code from the config API to the exit code of 'git maintenance'. Instead, use die() to recover from failures at that level. In the case of 'unregister --force', allow the CONFIG_NOTHING_SET error code to be a success. This allows a possible race where another process removes the config value. The end result is that the config value is not set anymore, so we can treat this as a success. Reported-by: SZEDER Gábor Signed-off-by: Derrick Stolee --- builtin/gc.c | 75 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/builtin/gc.c b/builtin/gc.c index dc0ba9e3648..7a585f0b71d 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1470,11 +1470,12 @@ static int maintenance_register(int argc, const char **argv, const char *prefix) struct option options[] = { OPT_END(), }; - int rc; + int found = 0; + const char *key = "maintenance.repo"; char *config_value; - struct child_process config_set = CHILD_PROCESS_INIT; - struct child_process config_get = CHILD_PROCESS_INIT; char *maintpath = get_maintpath(); + struct string_list_item *item; + const struct string_list *list; argc = parse_options(argc, argv, prefix, options, builtin_maintenance_register_usage, 0); @@ -1491,31 +1492,35 @@ static int maintenance_register(int argc, const char **argv, const char *prefix) else git_config_set("maintenance.strategy", "incremental"); - config_get.git_cmd = 1; - strvec_pushl(&config_get.args, "config", "--global", "--get", - "--fixed-value", "maintenance.repo", maintpath, NULL); - config_get.out = -1; - - if (start_command(&config_get)) { - rc = error(_("failed to run 'git config'")); - goto done; + list = git_config_get_value_multi(key); + if (list) { + for_each_string_list_item(item, list) { + if (!strcmp(maintpath, item->string)) { + found = 1; + break; + } + } } - /* We already have this value in our config! */ - if (!finish_command(&config_get)) { - rc = 0; - goto done; + if (!found) { + int rc; + char *user_config, *xdg_config; + git_global_config(&user_config, &xdg_config); + if (!user_config) + die(_("$HOME not set")); + rc = git_config_set_multivar_in_file_gently( + user_config, "maintenance.repo", maintpath, + CONFIG_REGEX_NONE, 0); + free(user_config); + free(xdg_config); + + if (rc) + die(_("unable to add '%s' value of '%s'"), + key, maintpath); } - config_set.git_cmd = 1; - strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo", - maintpath, NULL); - - rc = run_command(&config_set); - -done: free(maintpath); - return rc; + return 0; } static char const * const builtin_maintenance_unregister_usage[] = { @@ -1533,8 +1538,6 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi OPT_END(), }; const char *key = "maintenance.repo"; - int rc = 0; - struct child_process config_unset = CHILD_PROCESS_INIT; char *maintpath = get_maintpath(); int found = 0; struct string_list_item *item; @@ -1557,17 +1560,27 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi } if (found) { - config_unset.git_cmd = 1; - strvec_pushl(&config_unset.args, "config", "--global", "--unset", - "--fixed-value", key, maintpath, NULL); - - rc = run_command(&config_unset); + int rc; + char *user_config, *xdg_config; + git_global_config(&user_config, &xdg_config); + if (!user_config) + die(_("$HOME not set")); + rc = git_config_set_multivar_in_file_gently( + user_config, key, NULL, maintpath, + CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE); + free(user_config); + free(xdg_config); + + if (rc && + (!force || rc == CONFIG_NOTHING_SET)) + die(_("unable to unset '%s' value of '%s'"), + key, maintpath); } else if (!force) { die(_("repository '%s' is not registered"), maintpath); } free(maintpath); - return rc; + return 0; } static const char *get_frequency(enum schedule_priority schedule) From patchwork Tue Sep 27 13:57:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12990642 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48B05C6FA83 for ; Tue, 27 Sep 2022 13:57:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232182AbiI0N51 (ORCPT ); Tue, 27 Sep 2022 09:57:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232221AbiI0N5J (ORCPT ); Tue, 27 Sep 2022 09:57:09 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5776142E11 for ; Tue, 27 Sep 2022 06:57:07 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id z6so15174551wrq.1 for ; Tue, 27 Sep 2022 06:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date; bh=WlC4mH82+qETJwt7cFxNUGNvLYc7p3t8aRFyeWV8RWg=; b=SZMSxPHvYqMIzEf8s+Jd+04QJ9uT/jFYiwAILAwQ0w6N7L1KE+6JLMTXHJAcqMaqq8 KavbBa2f4BN/MHAm7JPKGUrhcaU7ji05puCijhXuD2MSseAYckOUbOWo19OrkCEBEqpk PROg0bfEdkNz5iMf3vtVQ3c4Ue0bOTrtgkvV/t8x3JUVmEYYZ/aXHr3uNPe+353GA2VS S3rXmBzDFDjtnGKXsuXPlvyPeApBKnR3elBvrWwLBd2PToo48y3Q3Hia244lIJF29c1K m7fLDH0GN88OB+wyiQuYvc9CpWDm0fzoyN+wOcHBsJ0z2WXFNfckCTfTF1pcGpXzTcC0 ZuwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date; bh=WlC4mH82+qETJwt7cFxNUGNvLYc7p3t8aRFyeWV8RWg=; b=x6VS+OJMZ3AIIfSv4SHvOuEI1MKS+ymuJfCiHjKu4fifjJfQ3+DLdx5TkSw+fG1qQN Mqy8R235cPn7qxNUw18Dz2o8grzBS2h4tO0JXDNzd56YsgkUWEj2/7f87v2orn8SYMHh ong0Hi0HH9CPU1l/2RdFLa+Ip5W8FO729wgB7XDe+FpT21bhEH9URUwyM2POaG/Bd7HR mLkgqjdFofEsVRN7HYOb1uhC5Dv8dTu1+QBCNjDP8SoFbhH2l28LWscxqW9jPP4Uh+0s 0cKv6NqCMhpXvvGymHqqTk0Olc31KV3QXB7ScuRgDLcsfxYX7oG3DR0djHkZe7c2mQcg zhbQ== X-Gm-Message-State: ACrzQf1AC0A0oRIiF2zZ6QW0vEIjnhSpQ4ZjsUHbYJDQrMK1fkeoZ9RI 8WtSqfwch/n0S0jopQlc3wCinRnOmm4= X-Google-Smtp-Source: AMsMyM5FVBqznYsl/ZOIfaSHRMybDCQwDhNZqeOS7PluMIQIQ+JyT31bKy7O6FmVWRYInc7NqOkLGQ== X-Received: by 2002:a5d:4fc7:0:b0:22a:dce9:5f0f with SMTP id h7-20020a5d4fc7000000b0022adce95f0fmr17083800wrw.451.1664287026128; Tue, 27 Sep 2022 06:57:06 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u15-20020a05600c210f00b003a845621c5bsm1765899wml.34.2022.09.27.06.57.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 06:57:05 -0700 (PDT) Message-Id: <73a262cdca46a45aeeda6f47ea3357aaeb937e7b.1664287021.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 27 Sep 2022 13:57:01 +0000 Subject: [PATCH v4 4/4] string-list: document iterator behavior on NULL input Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: gitster@pobox.com, vdye@github.com, SZEDER =?utf-8?b?R8OhYm9y?= , =?utf-8?b?w4Z2YXIgQXJu?= =?utf-8?b?ZmrDtnLDsA==?= Bjarmason , Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee The for_each_string_list_item() macro takes a string_list and automatically constructs a for loop to iterate over its contents. This macro will segfault if the list is non-NULL. We cannot change the macro to be careful around NULL values because there are many callers that use the address of a local variable, which will never be NULL and will cause compile errors with -Werror=address. For now, leave a documentation comment to try to avoid mistakes in the future where a caller does not check for a NULL list. Signed-off-by: Derrick Stolee --- string-list.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/string-list.h b/string-list.h index d5a744e1438..c7b0d5d0008 100644 --- a/string-list.h +++ b/string-list.h @@ -141,7 +141,12 @@ void string_list_clear_func(struct string_list *list, string_list_clear_func_t c int for_each_string_list(struct string_list *list, string_list_each_func_t func, void *cb_data); -/** Iterate over each item, as a macro. */ +/** + * Iterate over each item, as a macro. + * + * Be sure that 'list' is non-NULL. The macro cannot perform NULL + * checks due to -Werror=address errors. + */ #define for_each_string_list_item(item,list) \ for (item = (list)->items; \ item && item < (list)->items + (list)->nr; \