From cdfb730a5cd3cccd238d6832f50d4cad6195e787 Mon Sep 17 00:00:00 2001 From: Vincent Barbaresi Date: Sun, 14 Oct 2018 14:49:48 +0200 Subject: [PATCH] Fix using -l and -i in qisrc grep options #91 --- python/qisrc/actions/grep.py | 14 +++++++++++++- python/qisrc/test/test_qisrc_grep.py | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python/qisrc/actions/grep.py b/python/qisrc/actions/grep.py index f17589c0f..4a741934a 100644 --- a/python/qisrc/actions/grep.py +++ b/python/qisrc/actions/grep.py @@ -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: @@ -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): @@ -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() diff --git a/python/qisrc/test/test_qisrc_grep.py b/python/qisrc/test/test_qisrc_grep.py index 11bc4f340..49bcb9a48 100644 --- a/python/qisrc/test/test_qisrc_grep.py +++ b/python/qisrc/test/test_qisrc_grep.py @@ -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)