Add git-for-each-repo
Subsystems (2)
v3 cover Add git-for-each-repo
Lars Hjemli (2):
for-each-repo: new command used for multi-repo operations
git: rewrite git -a to become a git-for-each-repo command
.gitignore | 1 + Documentation/git-for-each-repo.txt | 62 +++++++++++++++++++ Makefile | 1 + builtin.h | 1 + builtin/for-each-repo.c | 119 ++++++++++++++++++++++++++++++++++++ git.c | 37 +++++++++++ t/t6400-for-each-repo.sh | 54 ++++++++++++++++ 7 files changed, 275 insertions(+) create mode 100644 Documentation/git-for-each-repo.txt create mode 100644 builtin/for-each-repo.c create mode 100755 t/t6400-for-each-repo.sh
Signature · 1 line
1.8.1.1.350.g3346805
PATCH v3 1/2 for-each-repo: new command used for multi-repo operations
When working with multiple, unrelated (or loosly related) git repos, there is often a need to locate all repos with uncommitted work and perform some action on them (say, commit and push). Before this patch, such tasks would require manually visiting all repositories, running git status within each one and then decide what to do next.
This mundane task can now be automated by e.g. git for-each-repo --dirty status, which will find all git repositories below the current directory (even nested ones), check if they are dirty (as defined by git diff --quiet && git diff --cached --quiet), and for each dirty repo print the path to the repo and then execute git status within the repo.
The command also honours the option '--clean' which restricts the set of repos to those which '--dirty' would skip.
Finally, the command to execute within each repo is optional. If none is given, git-for-each-repo will just print the path to each repo found.
--- .gitignore | 1 + Documentation/git-for-each-repo.txt | 62 +++++++++++++++++++ Makefile | 1 + builtin.h | 1 + builtin/for-each-repo.c | 119 ++++++++++++++++++++++++++++++++++++ git.c | 1 + t/t6400-for-each-repo.sh | 48 +++++++++++++++ 7 files changed, 233 insertions(+) create mode 100644 Documentation/git-for-each-repo.txt create mode 100644 builtin/for-each-repo.c create mode 100755 t/t6400-for-each-repo.sh
.gitignore +1 -0
@@ -56,6 +56,7 @@ /git-filter-branch /git-fmt-merge-msg /git-for-each-ref +/git-for-each-repo /git-format-patch /git-fsck /git-fsck-objects
Documentation/git-for-each-repo.txt new file +62 -0
62 lines not on this page · raw message
Makefile +1 -0
@@ -870,6 +870,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/grep.o
builtin.h +1 -0
@@ -73,6 +73,7 @@ extern int cmd_fetch(int argc, const char **argv, const char *prefix);extern int cmd_fetch_pack (int argc,const char **argv,const char *prefix);extern int cmd_fmt_merge_msg (int argc,const char **argv,const char *prefix);extern int cmd_for_each_ref (int argc,const char **argv,const char *prefix); +extern int cmd_for_each_repo (int argc,const char **argv,const char *prefix);extern int cmd_format_patch (int argc,const char **argv,const char *prefix);extern int cmd_fsck (int argc,const char **argv,const char *prefix);extern int cmd_gc (int argc,const char **argv,const char *prefix);
builtin/for-each-repo.c new file +119 -0
119 lines not on this page · raw message
git.c +1 -0
@@ -337,6 +337,7 @@ static void handle_internal_command(int argc, const char **argv) { "fetch-pack", cmd_fetch_pack, RUN_SETUP }, { "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 }, { "format-patch", cmd_format_patch, RUN_SETUP }, { "fsck", cmd_fsck, RUN_SETUP }, { "fsck-objects", cmd_fsck, RUN_SETUP },
t/t6400-for-each-repo.sh new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
1.8.1.1.350.g3346805
Re: [PATCH v3 1/2] for-each-repo: new command used for multi-repo operations
Documentation/git-for-each-repo.txt line 6
Quoted 12 lines · Lars Hjemli
Documentation/git-for-each-repo.txt new file +6
-0@@ -0,0 +1,62 @@ +git-for-each-repo(1) +==================== + +NAME +---- +git-for-each-repo - Execute a git command in multiple repositories
"multiple non-bare repositories", I think.
Documentation/git-for-each-repo.txt line 15
Quoted 9 lines
Documentation/git-for-each-repo.txt new file +9
-0+ +SYNOPSIS +-------- +[verse] +'git for-each-repo' [--all|--clean|--dirty] [command] + +DESCRIPTION +----------- +The git-for-each-repo command is used to locate all git repositoris
Likewise; "all non-bare Git repositories".
Quoted 42 lines
t/t6400-for-each-repo.sh new file +36
-0@@ -0,0 +1,48 @@ +#!/bin/sh +# +# Copyright (c) 2013 Lars Hjemli +# + +test_description='Test the git-for-each-repo command' + +. ./test-lib.sh + +test_expect_success "setup" ' + test_create_repo clean && + (cd clean && test_commit foo) && + git init --separate-git-dir=.cleansub clean/gitfile && + (cd clean/gitfile && test_commit foo && echo bar >>foo.t) && + test_create_repo dirty-wt && + (cd dirty-wt && mv .git .linkedgit && ln -s .linkedgit .git && + test_commit foo && rm foo.t) && + test_create_repo dirty-idx && + (cd dirty-idx && test_commit foo && git rm foo.t) && + mkdir fakedir && mkdir fakedir/.git +' + +test_expect_success "without flags, all repos are included" ' + echo "." >expect && + echo "clean" >>expect && + echo "clean/gitfile" >>expect && + echo "dirty-idx" >>expect && + echo "dirty-wt" >>expect && + git for-each-repo | sort >actual && + test_cmp expect actual +' + +test_expect_success "--dirty only includes dirty repos" ' + echo "clean/gitfile" >expect && + echo "dirty-idx" >>expect && + echo "dirty-wt" >>expect &&
t/t6400-for-each-repo.sh line 46
Quoted 10 lines
t/t6400-for-each-repo.sh new file +10
-0+ git for-each-repo --dirty | sort >actual && + test_cmp expect actual +' + +test_expect_success "--clean only includes clean repos" ' + echo "." >expect && + echo "clean" >>expect && + git for-each-repo --clean | sort >actual && + test_cmp expect actual +'
Please add tests to show some command executions (e.g. test output from "git ls-files", or something).
t/t6400-for-each-repo.sh
Quoted 9 lines
t/t6400-for-each-repo.sh new file +9
-0+static void handle_repo(char *path, const char **argv) +{ + if (path[0] == '.' && path[1] == '/') + path += 2; + if (match != ALL && match != get_repo_state()) + return; + if (*argv) { + color_fprintf_ln(stdout, GIT_COLOR_YELLOW, "[%s]", path); + run_command_v_opt(argv, RUN_GIT_CMD);
This seems to allow people to run only a single Git subcommand, which is probably not what most people want to see. Don't we want to support something as simple as this?
git for-each-repository sh -c "ls *.c"
t/t6400-for-each-repo.sh
Quoted 2 lines
t/t6400-for-each-repo.sh new file +2
-0+ } else + printf("%s\n", path);
Assuming that the non *argv case is for consumption by programs and scripts (similar to the way "ls-files" output is piped to downstream), we prefer to (1) support "-z" so that "xargs -0" can read paths with funny characters, and (2) use quote_c_style() from quote.c when "-z" is not in effect.
t/t6400-for-each-repo.sh
Quoted 6 lines
t/t6400-for-each-repo.sh new file +6
-0+} + ... + setenv(GIT_DIR_ENVIRONMENT, gitdir, 1); + strbuf_setlen(path, len - 1); + setenv(GIT_WORK_TREE_ENVIRONMENT, path->buf, 1); + handle_repo(path->buf, argv);
When you are only showing the path to a repository, I do not think you want setenv() or chdir() at all. Shouldn't these be done inside handle_repo() function? As you are only dealing with non-bare repositories (and that is what you print in "listing only" mode anyway), handle_repo() can borrow path (not path->buf) and append and strip "/.git" as needed.
Also, while it is a good idea to protect this program from stray GIT_DIR/GIT_WORK_TREE the user may have in the environment when this program is started, I think this is not enough, if you allow the *argv commands to run worktree related operations in each repository you discover. You would need to chdir() to the top of the working tree.
The run-command API lets you specify custom environment only for the child process without affecting yourself by setting .env member of the child_process structure, so we may want to use that instead of doing setenv() on ourselves (and letting it inherited by the child).
PATCH v3 2/2 git: rewrite `git -a` to become a git-for-each-repo command
In reply to Lars HjemliWith this rewriting, it is now possible to run e.g. git -ad gui to start up git-gui in each repo within the current directory which contains uncommited work.
--- git.c | 36 ++++++++++++++++++++++++++++++++++++ t/t6400-for-each-repo.sh | 6 ++++++ 2 files changed, 42 insertions(+)
git.c +36 -0
@@ -31,8 +31,42 @@ static void commit_pager_choice(void) { } } +/* +* Rewrite 'git -ad status' to 'git for-each-repo -d status' +*/ +static int rewrite_foreach_repo (const char ***orig_argv, +const char **curr_argv, +int *curr_argc) +{ +const char **new_argv; +char *tmp; +int new_argc, curr_pos, i, j; + + curr_pos = curr_argv - *orig_argv; +if (strlen (curr_argv[0 ]) ==2 ) { + curr_argv[0 ] ="for-each-repo" ; +return curr_pos -1 ; + } + + new_argc = curr_pos + *curr_argc +1 ; + new_argv =xmalloc (new_argc *sizeof (void *)); +for (i = j =0 ; j < new_argc; i++, j++) { +if (i == curr_pos) { +asprintf (&tmp,"-%s" , (*orig_argv)[i] +2 ); + new_argv[j] ="for-each-repo" ; + new_argv[++j] = tmp; + }else { + new_argv[j] = (*orig_argv)[i]; + } + } + *orig_argv = new_argv; + (*curr_argc)++; +return curr_pos; +} +static int handle_options (const char ***argv,int *argc,int *envchanged) { +const char ***pargv = argv;const char **orig_argv = *argv;while (*argc >0 ) {
@@ -143,6 +177,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)setenv (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,"0" ,1 );if (envchanged) *envchanged =1 ; + }else if (!strncmp (cmd,"-a" ,2 )) { +return rewrite_foreach_repo (pargv, *argv, argc); }else {fprintf (stderr,"Unknown option: %s\n" , cmd);usage (git_usage_string);
t/t6400-for-each-repo.sh +6 -0
@@ -27,6 +27,8 @@ test_expect_success "without flags, all repos are included" ' echo "dirty-idx" >>expect && echo "dirty-wt" >>expect && git for-each-repo | sort >actual && + test_cmp expect actual && + git -a | sort >actual && test_cmp expect actual '
@@ -35,6 +37,8 @@ test_expect_success "--dirty only includes dirty repos" ' echo "dirty-idx" >>expect && echo "dirty-wt" >>expect && git for-each-repo --dirty | sort >actual && + test_cmp expect actual && + git -ad | sort >actual && test_cmp expect actual '
@@ -42,6 +46,8 @@ test_expect_success "--clean only includes clean repos" ' echo "." >expect && echo "clean" >>expect && git for-each-repo --clean | sort >actual && + test_cmp expect actual && + git -ac | sort >actual && test_cmp expect actual '
Signature · 1 line
1.8.1.1.350.g3346805