for-each-repo: run subcommands on configured repos
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>
Derrick Stolee committed
Sep 11, 2020 at 17:49 UTC
4950b2a2b5c4731e4c9d5b803739a6979b23fed6
8 files changed
+152
.gitignore
+1
@@ -67,6 +67,7 @@
67
/git-filter-branch
68
/git-fmt-merge-msg
69
/git-for-each-ref
70
+/git-for-each-repo
71
/git-format-patch
72
/git-fsck
73
/git-fsck-objects
Documentation/git-for-each-repo.txt
new
+59
@@ -0,0 +1,59 @@
1
+git-for-each-repo(1)
2
+====================
3
+
4
+NAME
5
+----
6
+git-for-each-repo - Run a Git command on a list of repositories
7
+
8
+
9
+SYNOPSIS
10
+--------
11
+[verse]
12
+'git for-each-repo' --config=<config> [--] <arguments>
13
+
14
+
15
+DESCRIPTION
16
+-----------
17
+Run a Git command on a list of repositories. The arguments after the
18
+known options or `--` indicator are used as the arguments for the Git
19
+subprocess.
20
+
21
+THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
22
+
23
+For example, we could run maintenance on each of a list of repositories
24
+stored in a `maintenance.repo` config variable using
25
+
26
+-------------
27
+git for-each-repo --config=maintenance.repo maintenance run
28
+-------------
29
+
30
+This will run `git -C <repo> maintenance run` for each value `<repo>`
31
+in the multi-valued config variable `maintenance.repo`.
32
+
33
+
34
+OPTIONS
35
+-------
36
+--config=<config>::
37
+ Use the given config variable as a multi-valued list storing
38
+ absolute path names. Iterate on that list of paths to run
39
+ the given arguments.
40
++
41
+These config values are loaded from system, global, and local Git config,
42
+as available. If `git for-each-repo` is run in a directory that is not a
43
+Git repository, then only the system and global config is used.
44
+
45
+
46
+SUBPROCESS BEHAVIOR
47
+-------------------
48
+
49
+If any `git -C <repo> <arguments>` subprocess returns a non-zero exit code,
50
+then the `git for-each-repo` process returns that exit code without running
51
+more subprocesses.
52
+
53
+Each `git -C <repo> <arguments>` subprocess inherits the standard file
54
+descriptors `stdin`, `stdout`, and `stderr`.
55
+
56
+
57
+GIT
58
+---
59
+Part of the linkgit:git[1] suite
Makefile
+1
@@ -1071,6 +1071,7 @@ BUILTIN_OBJS += builtin/fetch-pack.o
1071
BUILTIN_OBJS += builtin/fetch.o
1072
BUILTIN_OBJS += builtin/fmt-merge-msg.o
1073
BUILTIN_OBJS += builtin/for-each-ref.o
1074
+BUILTIN_OBJS += builtin/for-each-repo.o
1075
BUILTIN_OBJS += builtin/fsck.o
1076
BUILTIN_OBJS += builtin/gc.o
1077
BUILTIN_OBJS += builtin/get-tar-commit-id.o
builtin.h
+1
@@ -150,6 +150,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix);
150
int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
151
int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
152
int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
153
+int cmd_for_each_repo(int argc, const char **argv, const char *prefix);
154
int cmd_format_patch(int argc, const char **argv, const char *prefix);
155
int cmd_fsck(int argc, const char **argv, const char *prefix);
156
int cmd_gc(int argc, const char **argv, const char *prefix);
builtin/for-each-repo.c
new
+58
@@ -0,0 +1,58 @@
1
+#include "cache.h"
2
+#include "config.h"
3
+#include "builtin.h"
4
+#include "parse-options.h"
5
+#include "run-command.h"
6
+#include "string-list.h"
7
+
8
+static const char * const for_each_repo_usage[] = {
9
+ N_("git for-each-repo --config=<config> <command-args>"),
10
+ NULL
11
+};
12
+
13
+static int run_command_on_repo(const char *path,
14
+ void *cbdata)
15
+{
16
+ int i;
17
+ struct child_process child = CHILD_PROCESS_INIT;
18
+ struct strvec *args = (struct strvec *)cbdata;
19
+
20
+ child.git_cmd = 1;
21
+ strvec_pushl(&child.args, "-C", path, NULL);
22
+
23
+ for (i = 0; i < args->nr; i++)
24
+ strvec_push(&child.args, args->v[i]);
25
+
26
+ return run_command(&child);
27
+}
28
+
29
+int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
30
+{
31
+ static const char *config_key = NULL;
32
+ int i, result = 0;
33
+ const struct string_list *values;
34
+ struct strvec args = STRVEC_INIT;
35
+
36
+ const struct option options[] = {
37
+ OPT_STRING(0, "config", &config_key, N_("config"),
38
+ N_("config key storing a list of repository paths")),
39
+ OPT_END()
40
+ };
41
+
42
+ argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
43
+ PARSE_OPT_STOP_AT_NON_OPTION);
44
+
45
+ if (!config_key)
46
+ die(_("missing --config=<config>"));
47
+
48
+ for (i = 0; i < argc; i++)
49
+ strvec_push(&args, argv[i]);
50
+
51
+ values = repo_config_get_value_multi(the_repository,
52
+ config_key);
53
+
54
+ for (i = 0; !result && i < values->nr; i++)
55
+ result = run_command_on_repo(values->items[i].string, &args);
56
+
57
+ return result;
58
+}
command-list.txt
+1
@@ -94,6 +94,7 @@ git-fetch-pack synchingrepositories
94
git-filter-branch ancillarymanipulators
95
git-fmt-merge-msg purehelpers
96
git-for-each-ref plumbinginterrogators
97
+git-for-each-repo plumbinginterrogators
98
git-format-patch mainporcelain
99
git-fsck ancillaryinterrogators complete
100
git-gc mainporcelain
git.c
+1
@@ -511,6 +511,7 @@ static struct cmd_struct commands[] = {
511
{ "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
512
{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
513
{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
514
+ { "for-each-repo", cmd_for_each_repo, RUN_SETUP_GENTLY },
515
{ "format-patch", cmd_format_patch, RUN_SETUP },
516
{ "fsck", cmd_fsck, RUN_SETUP },
517
{ "fsck-objects", cmd_fsck, RUN_SETUP },
t/t0068-for-each-repo.sh
new
+30
@@ -0,0 +1,30 @@
1
+#!/bin/sh
2
+
3
+test_description='git for-each-repo builtin'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success 'run based on configured value' '
8
+ git init one &&
9
+ git init two &&
10
+ git init three &&
11
+ git -C two commit --allow-empty -m "DID NOT RUN" &&
12
+ git config run.key "$TRASH_DIRECTORY/one" &&
13
+ git config --add run.key "$TRASH_DIRECTORY/three" &&
14
+ git for-each-repo --config=run.key commit --allow-empty -m "ran" &&
15
+ git -C one log -1 --pretty=format:%s >message &&
16
+ grep ran message &&
17
+ git -C two log -1 --pretty=format:%s >message &&
18
+ ! grep ran message &&
19
+ git -C three log -1 --pretty=format:%s >message &&
20
+ grep ran message &&
21
+ git for-each-repo --config=run.key -- commit --allow-empty -m "ran again" &&
22
+ git -C one log -1 --pretty=format:%s >message &&
23
+ grep again message &&
24
+ git -C two log -1 --pretty=format:%s >message &&
25
+ ! grep again message &&
26
+ git -C three log -1 --pretty=format:%s >message &&
27
+ grep again message
28
+'
29
+
30
+test_done