Subject: [PATCH v3] for-each-repo: do nothing on empty config

From: "Derrick Stolee via GitGitGadget" <[email-masked]>
Date: Fri, 08 Jan 2021 02:30:46 +0000

From: Derrick Stolee <[email-masked]>

'git for-each-repo --config=X' should return success without calling any
subcommands when the config key 'X' has no value. The current
implementation instead segfaults.

A user could run into this issue if they used 'git maintenance start' to
initialize their cron schedule using 'git for-each-repo
--config=maintenance.repo ...' but then using 'git maintenance
unregister' to remove the config option. (Note: 'git maintenance stop'
would remove the config _and_ remove the cron schedule.)

Add a simple test to ensure this works. Use 'git help --no-such-option'
as the potential subcommand to ensure that we will hit a failure if the
subcommand is ever run.

Reported-by: Andreas Bühmann <[email-masked]>
Helped-by: Eric Sunshine <[email-masked]>
Helped-by: Junio C Hamano <[email-masked]>
Signed-off-by: Derrick Stolee <[email-masked]>
---
    for-each-repo: do nothing on empty config
    
    Thanks, Andreas, for drawing my attention to this bug.
    
    V3: update comment in test and use git help --no-such-option as the
    subcommand to fail.
    
    [1] https://github.com/gitgitgadget/git/issues/833

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-834%2Fderrickstolee%2Ffor-each-repo-empty-config-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-834/derrickstolee/for-each-repo-empty-config-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/834

Range-diff vs v2:

 1:  a1f1300bacb ! 1:  31350c7845c for-each-repo: do nothing on empty config
     @@ Commit message
          unregister' to remove the config option. (Note: 'git maintenance stop'
          would remove the config _and_ remove the cron schedule.)
      
     -    Add a simple test to ensure this works.
     +    Add a simple test to ensure this works. Use 'git help --no-such-option'
     +    as the potential subcommand to ensure that we will hit a failure if the
     +    subcommand is ever run.
      
          Reported-by: Andreas Bühmann <[email-masked]>
          Helped-by: Eric Sunshine <[email-masked]>
     +    Helped-by: Junio C Hamano <[email-masked]>
          Signed-off-by: Derrick Stolee <[email-masked]>
      
       ## builtin/for-each-repo.c ##
     @@ t/t0068-for-each-repo.sh: test_expect_success 'run based on configured value' '
       '
       
      +test_expect_success 'do nothing on empty config' '
     -+	# will fail if any subcommand is run
     -+	git for-each-repo --config=bogus.config -- false
     ++	# the whole thing would fail if for-each-ref iterated even
     ++	# once, because "git help --no-such-option" would fail
     ++	git for-each-repo --config=bogus.config -- help --no-such-option
      +'
      +
       test_done

 builtin/for-each-repo.c  | 7 +++++++
 t/t0068-for-each-repo.sh | 6 ++++++
 2 files changed, 13 insertions(+)

diff --git a/builtin/for-each-repo.c b/builtin/for-each-repo.c
index 5bba623ff12..52be64a4373 100644
--- a/builtin/for-each-repo.c
+++ b/builtin/for-each-repo.c
@@ -51,6 +51,13 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
 	values = repo_config_get_value_multi(the_repository,
 					     config_key);
 
+	/*
+	 * Do nothing on an empty list, which is equivalent to the case
+	 * where the config variable does not exist at all.
+	 */
+	if (!values)
+		return 0;
+
 	for (i = 0; !result && i < values->nr; i++)
 		result = run_command_on_repo(values->items[i].string, &args);
 
diff --git a/t/t0068-for-each-repo.sh b/t/t0068-for-each-repo.sh
index 136b4ec8392..4675e852517 100755
--- a/t/t0068-for-each-repo.sh
+++ b/t/t0068-for-each-repo.sh
@@ -27,4 +27,10 @@ test_expect_success 'run based on configured value' '
 	grep again message
 '
 
+test_expect_success 'do nothing on empty config' '
+	# the whole thing would fail if for-each-ref iterated even
+	# once, because "git help --no-such-option" would fail
+	git for-each-repo --config=bogus.config -- help --no-such-option
+'
+
 test_done

base-commit: 4950b2a2b5c4731e4c9d5b803739a6979b23fed6
-- 
gitgitgadget