Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the option --repos-without-matches #356

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions all_repos/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def add_repos_with_matches_arg(parser: ParserType) -> None:
)


def add_repos_without_matches_arg(parser: ParserType) -> None:
parser.add_argument(
'--repos-without-matches', action='store_true',
help='only print repositories without matches.',
)


def add_output_paths_arg(parser: ParserType) -> None:
parser.add_argument(
'--output-paths', action='store_true',
Expand Down
1 change: 1 addition & 0 deletions all_repos/find_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def main(argv: Sequence[str] | None = None) -> int:
)
cli.add_common_args(parser)
cli.add_repos_with_matches_arg(parser)
cli.add_repos_without_matches_arg(parser)
cli.add_output_paths_arg(parser)
parser.add_argument('pattern', help='the python regex to match.')
args = parser.parse_args(argv)
Expand Down
20 changes: 20 additions & 0 deletions all_repos/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def repos_matching(config: Config, grep_args: Sequence[str]) -> set[str]:
return set(grep(config, ('--quiet', *grep_args)))


def repos_not_matching(config: Config, grep_args: Sequence[str]) -> set[str]:
grep_ret = set(grep(config, ('--quiet', *grep_args)))
repos = [os.path.join(config.output_dir, repo) for repo in config.get_cloned_repos()]

return set(repos).difference(grep_ret)


def repos_matching_cli(config: Config, grep_args: Sequence[str]) -> int:
try:
matching = repos_matching(config, grep_args)
Expand All @@ -54,6 +61,16 @@ def repos_matching_cli(config: Config, grep_args: Sequence[str]) -> int:
return int(not matching)


def repos_not_matching_cli(config: Config, grep_args: Sequence[str]) -> int:
try:
not_matching = repos_not_matching(config, grep_args)
except GrepError as e:
return e.args[0]
for repo in sorted(not_matching):
print(repo)
return int(not not_matching)


def grep_cli(
config: Config,
grep_args: Sequence[str],
Expand Down Expand Up @@ -92,12 +109,15 @@ def main(argv: Sequence[str] | None = None) -> int:
)
cli.add_common_args(parser)
cli.add_repos_with_matches_arg(parser)
cli.add_repos_without_matches_arg(parser)
cli.add_output_paths_arg(parser)
args, rest = parser.parse_known_args(argv)

config = load_config(args.config_filename)
if args.repos_with_matches:
return repos_matching_cli(config, rest)
elif args.repos_without_matches:
return repos_not_matching_cli(config, rest)
else:
return grep_cli(
config, rest, output_paths=args.output_paths, use_color=args.color,
Expand Down
40 changes: 40 additions & 0 deletions tests/grep_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from all_repos.grep import grep
from all_repos.grep import main
from all_repos.grep import repos_matching
from all_repos.grep import repos_not_matching


def test_repos_matching(file_config_files):
Expand Down Expand Up @@ -50,6 +51,45 @@ def test_repos_matching_cli(file_config_files, capsys):
assert out == ''


def test_repos_not_matching(file_config_files):
config = load_config(file_config_files.cfg)
ret = repos_not_matching(config, ['^OH'])
assert ret == set()
ret = repos_not_matching(config, ['^OHAI'])
assert ret == {file_config_files.output_dir.join('repo2')}
ret = repos_not_matching(config, ['nope'])
assert ret == {
file_config_files.output_dir.join('repo1'),
file_config_files.output_dir.join('repo2'),
}


def test_repos_not_matching_cli(file_config_files, capsys):
ret = main((
'-C', str(file_config_files.cfg), '--repos-without-matches', '^OH',
))
assert ret == 1
out, _ = capsys.readouterr()
assert out == ''

ret = main((
'-C', str(file_config_files.cfg), '--repos-without-matches', 'OHAI',
))
assert ret == 0
out, _ = capsys.readouterr()
assert out == '{}\n'.format(file_config_files.output_dir.join('repo2'))

ret = main((
'-C', str(file_config_files.cfg), '--repos-without-matches', 'nope',
))
assert ret == 0
out, _ = capsys.readouterr()
assert out == '{}\n{}\n'.format(
file_config_files.output_dir.join('repo1'),
file_config_files.output_dir.join('repo2'),
)


def test_grep(file_config_files):
config = load_config(file_config_files.cfg)
ret = grep(config, ['^OH'])
Expand Down
Loading