summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-09-04 13:44:16 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-09-04 13:44:16 +0000
commit3f0e3e78228e533abd7e051e5103a282a46a645f (patch)
tree0047eeb3031dc0af808fc8ce62891cad2420f0dd
parentbe67d3d4c416394588678f67cbc69c879bfc4137 (diff)
downloadmorph-3f0e3e78228e533abd7e051e5103a282a46a645f.tar.gz
b&m: Re-implement morph foreach
The previous version attempted to separate the output of the commands run with newlines by printing a newline at the beginning if it wasn't the first repository. The check it used was for the first repository was flawed by assuming that the root repository was the first repository processed. Since the complication was not worth keeping the output identical, it has been changed to separate by printing a newline after every command and the test output has been amended to include the extra trailing newline.
-rw-r--r--morphlib/plugins/branch_and_merge_new_plugin.py58
-rw-r--r--morphlib/plugins/branch_and_merge_plugin.py2
-rw-r--r--tests.branching/edit-checkouts-existing-chunk.stdout1
-rw-r--r--tests.branching/edit-clones-chunk.stdout2
-rw-r--r--tests.branching/foreach-handles-full-urls.stdout1
-rw-r--r--tests.branching/tag-creates-commit-and-tag.stdout2
-rw-r--r--tests.branching/tag-tag-works-as-expected.stdout3
7 files changed, 68 insertions, 1 deletions
diff --git a/morphlib/plugins/branch_and_merge_new_plugin.py b/morphlib/plugins/branch_and_merge_new_plugin.py
index 7c326e48..98e675f1 100644
--- a/morphlib/plugins/branch_and_merge_new_plugin.py
+++ b/morphlib/plugins/branch_and_merge_new_plugin.py
@@ -47,6 +47,8 @@ class SimpleBranchAndMergePlugin(cliapp.Plugin):
'show-system-branch', self.show_system_branch, arg_synopsis='')
self.app.add_subcommand(
'show-branch-root', self.show_branch_root, arg_synopsis='')
+ self.app.add_subcommand('foreach', self.foreach,
+ arg_synopsis='-- COMMAND [ARGS...]')
def disable(self):
pass
@@ -546,3 +548,59 @@ class SimpleBranchAndMergePlugin(cliapp.Plugin):
return True
return False
+ def foreach(self, args):
+ '''Run a command in each repository checked out in a system branch.
+
+ Use -- before specifying the command to separate its arguments from
+ Morph's own arguments.
+
+ Command line arguments:
+
+ * `--` indicates the end of option processing for Morph.
+ * `COMMAND` is a command to run.
+ * `ARGS` is a list of arguments or options to be passed onto
+ `COMMAND`.
+
+ This runs the given `COMMAND` in each git repository belonging
+ to the current system branch that exists locally in the current
+ workspace. This can be a handy way to do the same thing in all
+ the local git repositories.
+
+ For example:
+
+ morph foreach -- git push
+
+ The above command would push any committed changes in each
+ repository to the git server.
+
+ '''
+
+ if not args:
+ raise cliapp.AppException('morph foreach expects a command to run')
+
+ ws = morphlib.workspace.open('.')
+ sb = morphlib.sysbranchdir.open_from_within('.')
+
+ for gd in sb.list_git_directories():
+ # Get the repository's original name
+ # Continue in the case of error, since the previous iteration
+ # worked in the case of the user cloning a repository in the
+ # system branch's directory.
+ try:
+ repo = gd.get_config('morph.repository')
+ except cliapp.AppException:
+ continue
+
+ self.app.output.write('%s\n' % repo)
+ status, output, error = self.app.runcmd_unchecked(
+ args, cwd=gd.dirname)
+ self.app.output.write(output)
+ if status != 0:
+ self.app.output.write(error)
+ pretty_command = ' '.join(cliapp.shell_quote(arg)
+ for arg in args)
+ raise cliapp.AppException(
+ 'Command failed at repo %s: %s'
+ % (repo, pretty_command))
+ self.app.output.write('\n')
+ self.app.output.flush()
diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py
index 85e74501..c1549a5d 100644
--- a/morphlib/plugins/branch_and_merge_plugin.py
+++ b/morphlib/plugins/branch_and_merge_plugin.py
@@ -79,7 +79,7 @@ class BranchAndMergePlugin(cliapp.Plugin):
group=group_branch)
# Advanced commands
- self.app.add_subcommand('foreach', self.foreach,
+ self.app.add_subcommand('old-foreach', self.foreach,
arg_synopsis='-- COMMAND [ARGS...]')
def disable(self):
diff --git a/tests.branching/edit-checkouts-existing-chunk.stdout b/tests.branching/edit-checkouts-existing-chunk.stdout
index acfd1c0a..5fbea686 100644
--- a/tests.branching/edit-checkouts-existing-chunk.stdout
+++ b/tests.branching/edit-checkouts-existing-chunk.stdout
@@ -6,5 +6,6 @@ test:hello
* alfred
master
+
Files in hello:
hello.morph
diff --git a/tests.branching/edit-clones-chunk.stdout b/tests.branching/edit-clones-chunk.stdout
index b694233b..dd3f00ef 100644
--- a/tests.branching/edit-clones-chunk.stdout
+++ b/tests.branching/edit-clones-chunk.stdout
@@ -7,6 +7,7 @@ test:hello
master
* newbranch
+
Current origins:
test:morphs
* remote origin
@@ -35,3 +36,4 @@ test:hello
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
+
diff --git a/tests.branching/foreach-handles-full-urls.stdout b/tests.branching/foreach-handles-full-urls.stdout
index 1ef0b5aa..3abae62c 100644
--- a/tests.branching/foreach-handles-full-urls.stdout
+++ b/tests.branching/foreach-handles-full-urls.stdout
@@ -1,3 +1,4 @@
file://TMP/morphs
# On branch master
nothing to commit (working directory clean)
+
diff --git a/tests.branching/tag-creates-commit-and-tag.stdout b/tests.branching/tag-creates-commit-and-tag.stdout
index 68623be7..598e28bf 100644
--- a/tests.branching/tag-creates-commit-and-tag.stdout
+++ b/tests.branching/tag-creates-commit-and-tag.stdout
@@ -38,6 +38,7 @@ index b0fed3b..4c4ee3e 100644
repo: test:morphs
+ unpetrify-ref: master
system-kind: rootfs-tarball
+
test:morphs
commit 9509c9c379f8ba643b2ad9a6ec50ecf96993cbb5
Author: developer <developer@example.com>
@@ -54,3 +55,4 @@ Commit: developer <developer@example.com>
CommitDate: Tue Jul 31 16:51:54 2012 +0000
initial
+
diff --git a/tests.branching/tag-tag-works-as-expected.stdout b/tests.branching/tag-tag-works-as-expected.stdout
index 878eefc1..242b2d4e 100644
--- a/tests.branching/tag-tag-works-as-expected.stdout
+++ b/tests.branching/tag-tag-works-as-expected.stdout
@@ -1,6 +1,7 @@
test:morphs
example-tag
tagged-tag
+
test:morphs
tag tagged-tag
Tagger: developer <developer@example.com>
@@ -41,6 +42,7 @@ index b0fed3b..875d73a 100644
repo: test:morphs
+ unpetrify-ref: master
system-kind: rootfs-tarball
+
test:morphs
commit ca7594f436da55bda2cfff2c6484d11aa0ea4cbc
Author: developer <developer@example.com>
@@ -57,3 +59,4 @@ Commit: developer <developer@example.com>
CommitDate: Tue Jul 31 16:51:54 2012 +0000
initial
+