Repository: asottile/all-repos

Issue:
Make an `all-repos-sed`
Sometimes I don't want to write the full gamut of a fixer and such and really just want some nice `sed -i`.

I think the interface would be something like:

```
$ all-repos-sed 's/foo/bar/g' -- baz.f
```

And this tool would make a fixer which does essentially (for each repository):

```
git ls-files -- baz.f | xargs sed -i 's/foo/bar/g'
```

Discussion:

Patch:
diff --git a/all_repos/find_files.py b/all_repos/find_files.py
index c6826b8..e230264 100644
--- a/all_repos/find_files.py
+++ b/all_repos/find_files.py
@@ -7,6 +7,7 @@ import sys
 from all_repos import cli
 from all_repos import color
 from all_repos.config import load_config
+from all_repos.util import zsplit
 
 
 def ls_files(config, repo):
@@ -15,7 +16,7 @@ def ls_files(config, repo):
         ('git', '-C', path, 'ls-files', '-z'),
         stdout=subprocess.PIPE, check=True,
     )
-    return path, ret.stdout.rstrip(b'\0').split(b'\0')
+    return path, zsplit(ret.stdout)
 
 
 def find_files(config, regex):
diff --git a/all_repos/sed.py b/all_repos/sed.py
new file mode 100644
index 0000000..b6b685d
--- /dev/null
+++ b/all_repos/sed.py
@@ -0,0 +1,69 @@
+import argparse
+import functools
+import os.path
+import shlex
+import subprocess
+
+from all_repos import autofix_lib
+from all_repos import cli
+from all_repos.util import zsplit
+
+
+def find_repos(config, *, ls_files_cmd):
+    for repo in config.get_cloned_repos():
+        repo_dir = os.path.join(config.output_dir, repo)
+        if subprocess.run(
+            ('git', '-C', repo_dir, *ls_files_cmd[1:]),
+            check=True, stdout=subprocess.PIPE,
+        ).stdout:
+            yield repo_dir
+
+
+def apply_fix(*, ls_files_cmd, sed_cmd):
+    filenames = zsplit(subprocess.check_output(ls_files_cmd))
+    filenames = [f.decode() for f in filenames]
+    autofix_lib.run(*sed_cmd, *filenames)
+
+
+def _quote_cmd(cmd):
+    return ' '.join(shlex.quote(arg) for arg in cmd)
+
+
+def main(argv=None):
+    parser = argparse.ArgumentParser()
+    cli.add_fixer_args(parser)
+    parser.add_argument(
+        '-r', '--regexp-extended',
+        action='store_true',
+        help='use extended regular expressions in the script.',
+    )
+    parser.add_argument('--branch-name', default='all-repos-sed')
+    parser.add_argument('--commit-msg')
+    parser.add_argument('pattern')
+    parser.add_argument('filenames_glob', help='(passed to ls-files)')
+    args = parser.parse_args(argv)
+
+    dash_r = ('-r',) if args.regexp_extended else ()
+    sed_cmd = ('sed', '-i', *dash_r, args.pattern)
+    ls_files_cmd = ('git', 'ls-files', '-z', '--', args.filenames_glob)
+
+    msg = f'{_quote_cmd(ls_files_cmd)} | xargs -0 {_quote_cmd(sed_cmd)}'
+    msg = args.commit_msg or msg
+
+    repos, config, commit, autofix_settings = autofix_lib.from_cli(
+        args,
+        find_repos=functools.partial(find_repos, ls_files_cmd=ls_files_cmd),
+        msg=msg, branch_name=args.branch_name,
+    )
+
+    autofix_lib.fix(
+        repos,
+        apply_fix=functools.partial(
+            apply_fix, ls_files_cmd=ls_files_cmd, sed_cmd=sed_cmd,
+        ),
+        config=config, commit=commit, autofix_settings=autofix_settings,
+    )
+
+
+if __name__ == '__main__':
+    exit(main())
diff --git a/all_repos/util.py b/all_repos/util.py
new file mode 100644
index 0000000..a689acb
--- /dev/null
+++ b/all_repos/util.py
@@ -0,0 +1,5 @@
+def zsplit(bs):
+    if bs:
+        return bs.rstrip(b'\0').split(b'\0')
+    else:
+        return []
diff --git a/setup.py b/setup.py
index 5b79eb7..51c20ae 100644
--- a/setup.py
+++ b/setup.py
@@ -20,6 +20,7 @@ setup(
             'all-repos-clone=all_repos.clone:main',
             'all-repos-find-files=all_repos.find_files:main',
             'all-repos-grep=all_repos.grep:main',
+            'all-repos-sed=all_repos.sed:main',
         ],
     },
 )


Test patch:
diff --git a/tests/sed_test.py b/tests/sed_test.py
new file mode 100644
index 0000000..bee579c
--- /dev/null
+++ b/tests/sed_test.py
@@ -0,0 +1,25 @@
+from all_repos import clone
+from all_repos.sed import main
+from testing.git import write_file_commit
+
+
+def test_main(file_config_files):
+    clone.main(('--config-filename', str(file_config_files.cfg)))
+    assert not main((
+        '--config-filename', str(file_config_files.cfg),
+        's/HAI/BAI/g', '*',
+    ))
+    assert file_config_files.dir1.join('f').read() == 'OBAI\n'
+    assert file_config_files.dir2.join('f').read() == 'OHELLO\n'
+
+
+def test_main_custom_file_pattern(file_config_files):
+    write_file_commit(file_config_files.dir1, 'g', 'OHAI\n')
+    clone.main(('--config-filename', str(file_config_files.cfg)))
+    assert not main((
+        '--config-filename', str(file_config_files.cfg),
+        's/AI/IE/g', 'g',
+    ))
+    assert file_config_files.dir1.join('f').read() == 'OHAI\n'
+    assert file_config_files.dir1.join('g').read() == 'OHIE\n'
+    assert file_config_files.dir2.join('f').read() == 'OHELLO\n'
diff --git a/tests/util_test.py b/tests/util_test.py
new file mode 100644
index 0000000..474efb8
--- /dev/null
+++ b/tests/util_test.py
@@ -0,0 +1,15 @@
+import pytest
+
+from all_repos.util import zsplit
+
+
+@pytest.mark.parametrize(
+    ('bs', 'expected'),
+    (
+        (b'', []),
+        (b'\0', [b'']),
+        (b'a\0b\0', [b'a', b'b']),
+    ),
+)
+def test_zsplit(bs, expected):
+    assert zsplit(bs) == expected