]> git.ipfire.org Git - thirdparty/git.git/commitdiff
for-each-repo: run subcommands on configured repos
authorDerrick Stolee <dstolee@microsoft.com>
Fri, 11 Sep 2020 17:49:16 +0000 (17:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 25 Sep 2020 17:59:44 +0000 (10:59 -0700)
It can be helpful to store a list of repositories in global or system
config and then iterate Git commands on that list. Create a new builtin
that makes this process simple for experts. We will use this builtin to
run scheduled maintenance on all configured repositories in a future
change.

The test is very simple, but does highlight that the "--" argument is
optional.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
.gitignore
Documentation/git-for-each-repo.txt [new file with mode: 0644]
Makefile
builtin.h
builtin/for-each-repo.c [new file with mode: 0644]
command-list.txt
git.c
t/t0068-for-each-repo.sh [new file with mode: 0755]

index a5808fa30d3e32b0b241783295d4e8094a1a437f..5eb2a2be71dd6ade4ba0461ddda0236ed2c64d47 100644 (file)
@@ -67,6 +67,7 @@
 /git-filter-branch
 /git-fmt-merge-msg
 /git-for-each-ref
+/git-for-each-repo
 /git-format-patch
 /git-fsck
 /git-fsck-objects
diff --git a/Documentation/git-for-each-repo.txt b/Documentation/git-for-each-repo.txt
new file mode 100644 (file)
index 0000000..94bd19d
--- /dev/null
@@ -0,0 +1,59 @@
+git-for-each-repo(1)
+====================
+
+NAME
+----
+git-for-each-repo - Run a Git command on a list of repositories
+
+
+SYNOPSIS
+--------
+[verse]
+'git for-each-repo' --config=<config> [--] <arguments>
+
+
+DESCRIPTION
+-----------
+Run a Git command on a list of repositories. The arguments after the
+known options or `--` indicator are used as the arguments for the Git
+subprocess.
+
+THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
+
+For example, we could run maintenance on each of a list of repositories
+stored in a `maintenance.repo` config variable using
+
+-------------
+git for-each-repo --config=maintenance.repo maintenance run
+-------------
+
+This will run `git -C <repo> maintenance run` for each value `<repo>`
+in the multi-valued config variable `maintenance.repo`.
+
+
+OPTIONS
+-------
+--config=<config>::
+       Use the given config variable as a multi-valued list storing
+       absolute path names. Iterate on that list of paths to run
+       the given arguments.
++
+These config values are loaded from system, global, and local Git config,
+as available. If `git for-each-repo` is run in a directory that is not a
+Git repository, then only the system and global config is used.
+
+
+SUBPROCESS BEHAVIOR
+-------------------
+
+If any `git -C <repo> <arguments>` subprocess returns a non-zero exit code,
+then the `git for-each-repo` process returns that exit code without running
+more subprocesses.
+
+Each `git -C <repo> <arguments>` subprocess inherits the standard file
+descriptors `stdin`, `stdout`, and `stderr`.
+
+
+GIT
+---
+Part of the linkgit:git[1] suite
index 65f8cfb2368d2d6099bdfd845a7beb505702b86f..7c588ff036a20e4665e11dbedce252c58e0f071d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1071,6 +1071,7 @@ BUILTIN_OBJS += builtin/fetch-pack.o
 BUILTIN_OBJS += builtin/fetch.o
 BUILTIN_OBJS += builtin/fmt-merge-msg.o
 BUILTIN_OBJS += builtin/for-each-ref.o
+BUILTIN_OBJS += builtin/for-each-repo.o
 BUILTIN_OBJS += builtin/fsck.o
 BUILTIN_OBJS += builtin/gc.o
 BUILTIN_OBJS += builtin/get-tar-commit-id.o
index 17c1c0ce49d4eabc26b27be26a2a0d981b3107f9..ff7c6e5aa9dc7232ab422cbf2d26b59a2d95df1f 100644 (file)
--- a/builtin.h
+++ b/builtin.h
@@ -150,6 +150,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix);
 int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
 int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
+int cmd_for_each_repo(int argc, const char **argv, const char *prefix);
 int cmd_format_patch(int argc, const char **argv, const char *prefix);
 int cmd_fsck(int argc, const char **argv, const char *prefix);
 int cmd_gc(int argc, const char **argv, const char *prefix);
diff --git a/builtin/for-each-repo.c b/builtin/for-each-repo.c
new file mode 100644 (file)
index 0000000..5bba623
--- /dev/null
@@ -0,0 +1,58 @@
+#include "cache.h"
+#include "config.h"
+#include "builtin.h"
+#include "parse-options.h"
+#include "run-command.h"
+#include "string-list.h"
+
+static const char * const for_each_repo_usage[] = {
+       N_("git for-each-repo --config=<config> <command-args>"),
+       NULL
+};
+
+static int run_command_on_repo(const char *path,
+                              void *cbdata)
+{
+       int i;
+       struct child_process child = CHILD_PROCESS_INIT;
+       struct strvec *args = (struct strvec *)cbdata;
+
+       child.git_cmd = 1;
+       strvec_pushl(&child.args, "-C", path, NULL);
+
+       for (i = 0; i < args->nr; i++)
+               strvec_push(&child.args, args->v[i]);
+
+       return run_command(&child);
+}
+
+int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
+{
+       static const char *config_key = NULL;
+       int i, result = 0;
+       const struct string_list *values;
+       struct strvec args = STRVEC_INIT;
+
+       const struct option options[] = {
+               OPT_STRING(0, "config", &config_key, N_("config"),
+                          N_("config key storing a list of repository paths")),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
+                            PARSE_OPT_STOP_AT_NON_OPTION);
+
+       if (!config_key)
+               die(_("missing --config=<config>"));
+
+       for (i = 0; i < argc; i++)
+               strvec_push(&args, argv[i]);
+
+       values = repo_config_get_value_multi(the_repository,
+                                            config_key);
+
+       for (i = 0; !result && i < values->nr; i++)
+               result = run_command_on_repo(values->items[i].string, &args);
+
+       return result;
+}
index 0e3204e7d1a08a15e5b7661fd35fd3048a7472ba..581499be82c03da8aa8f2883a5c95a78cb4548d7 100644 (file)
@@ -94,6 +94,7 @@ git-fetch-pack                          synchingrepositories
 git-filter-branch                       ancillarymanipulators
 git-fmt-merge-msg                       purehelpers
 git-for-each-ref                        plumbinginterrogators
+git-for-each-repo                       plumbinginterrogators
 git-format-patch                        mainporcelain
 git-fsck                                ancillaryinterrogators          complete
 git-gc                                  mainporcelain
diff --git a/git.c b/git.c
index 24f250d29a11b3fa8aa4fafddf2b78602d916d88..1cab64b5d1c1bb5d64e2b693b0da09c276a170b8 100644 (file)
--- a/git.c
+++ b/git.c
@@ -511,6 +511,7 @@ static struct cmd_struct commands[] = {
        { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
        { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
        { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
+       { "for-each-repo", cmd_for_each_repo, RUN_SETUP_GENTLY },
        { "format-patch", cmd_format_patch, RUN_SETUP },
        { "fsck", cmd_fsck, RUN_SETUP },
        { "fsck-objects", cmd_fsck, RUN_SETUP },
diff --git a/t/t0068-for-each-repo.sh b/t/t0068-for-each-repo.sh
new file mode 100755 (executable)
index 0000000..136b4ec
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='git for-each-repo builtin'
+
+. ./test-lib.sh
+
+test_expect_success 'run based on configured value' '
+       git init one &&
+       git init two &&
+       git init three &&
+       git -C two commit --allow-empty -m "DID NOT RUN" &&
+       git config run.key "$TRASH_DIRECTORY/one" &&
+       git config --add run.key "$TRASH_DIRECTORY/three" &&
+       git for-each-repo --config=run.key commit --allow-empty -m "ran" &&
+       git -C one log -1 --pretty=format:%s >message &&
+       grep ran message &&
+       git -C two log -1 --pretty=format:%s >message &&
+       ! grep ran message &&
+       git -C three log -1 --pretty=format:%s >message &&
+       grep ran message &&
+       git for-each-repo --config=run.key -- commit --allow-empty -m "ran again" &&
+       git -C one log -1 --pretty=format:%s >message &&
+       grep again message &&
+       git -C two log -1 --pretty=format:%s >message &&
+       ! grep again message &&
+       git -C three log -1 --pretty=format:%s >message &&
+       grep again message
+'
+
+test_done