Skip to content

Commit

Permalink
Fix using -l and -i in qisrc grep options aldebaran#91
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Barbaresi committed Oct 14, 2018
1 parent 5dabc37 commit cdfb730
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion python/qisrc/actions/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def do(args):
git_projects = qisrc.parsers.get_git_projects(git_worktree, args, default_all=True,
use_build_deps=args.use_deps)
git_grep_opts = args.git_grep_opts
git_grep_cmd = ["grep"]

# -l and -i must come before non-option arguments
# Remove them from the arguments and put them right after grep command
git_special_options = ("-l", "--files-with-matches", "-i", "--ignore-case")
if "-l" in git_grep_opts or "--files-with-matches" in git_grep_opts:
git_grep_cmd.append("-l")
if "-i" in git_grep_opts or "--ignore-case" in git_grep_opts:
git_grep_cmd.append("-i")
git_grep_opts = [x for x in git_grep_opts if x not in git_special_options]

if args.path == 'none':
git_grep_opts.insert(0, "-h")
else:
Expand All @@ -47,6 +58,7 @@ def do(args):
qisrc.worktree.on_no_matching_projects(git_worktree, groups=args.groups)
sys.exit(0)

git_cmd_line = git_grep_cmd + git_grep_opts
max_src = max(len(x.src) for x in git_projects)
retcode = 1
for i, project in enumerate(git_projects):
Expand All @@ -55,7 +67,7 @@ def do(args):
ui.blue, project.src.ljust(max_src),
end="\r")
git = qisrc.git.Git(project.path)
(status, out) = git.call("grep", *git_grep_opts, raises=False)
(status, out) = git.call(*git_cmd_line, raises=False)
if out != "":
if args.path == 'absolute' or args.path == 'worktree':
lines = out.splitlines()
Expand Down
4 changes: 4 additions & 0 deletions python/qisrc/test/test_qisrc_grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def test_using_git_grep_options(qisrc_action, record_messages):
rc = qisrc_action("grep", "--", "-i", "-l", "Spam", retcode=True)
assert rc == 0
assert record_messages.find("a.txt")
# Simple use of -l or -i args (GitHub issue #91)
rc = qisrc_action("grep", "Spam", "--", "-i", "-l", retcode=True)
assert rc == 0
assert record_messages.find("a.txt")

def test_worktree_paths(qisrc_action, record_messages):
setup_projects(qisrc_action)
Expand Down

0 comments on commit cdfb730

Please sign in to comment.