forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
b08ff1f
commit 4950b2a
Showing
8 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |