summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2021-02-24 12:47:01 -0500
committerMike Frysinger <vapier@google.com>2021-02-25 20:13:33 +0000
commit45ad1541c5cd20e2947edae76264b40413b3fd8d (patch)
tree849620bd404cc274f7566613eff8595dcb139073
parent7b586f231ba116d16b89639e04d940ae008ffff2 (diff)
downloadgit-repo-45ad1541c5cd20e2947edae76264b40413b3fd8d.tar.gz
grep: move nested func out to the class
This is in preparation for adding jobs support. The nested function is referenced in the options object which can't be pickled, so pull it out into a static method instead. Change-Id: I280ed2bf26390a0203925517a0d17c13053becaa Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/297983 Reviewed-by: Michael Mortensen <mmortensen@google.com> Tested-by: Mike Frysinger <vapier@google.com>
-rw-r--r--subcmds/grep.py72
1 files changed, 35 insertions, 37 deletions
diff --git a/subcmds/grep.py b/subcmds/grep.py
index e3628c6a..c16d4185 100644
--- a/subcmds/grep.py
+++ b/subcmds/grep.py
@@ -62,29 +62,27 @@ contain a line that matches both expressions:
62 62
63""" 63"""
64 64
65 def _Options(self, p): 65 @staticmethod
66 def carry(option, 66 def _carry_option(_option, opt_str, value, parser):
67 opt_str, 67 pt = getattr(parser.values, 'cmd_argv', None)
68 value, 68 if pt is None:
69 parser): 69 pt = []
70 pt = getattr(parser.values, 'cmd_argv', None) 70 setattr(parser.values, 'cmd_argv', pt)
71 if pt is None: 71
72 pt = [] 72 if opt_str == '-(':
73 setattr(parser.values, 'cmd_argv', pt) 73 pt.append('(')
74 74 elif opt_str == '-)':
75 if opt_str == '-(': 75 pt.append(')')
76 pt.append('(') 76 else:
77 elif opt_str == '-)': 77 pt.append(opt_str)
78 pt.append(')')
79 else:
80 pt.append(opt_str)
81 78
82 if value is not None: 79 if value is not None:
83 pt.append(value) 80 pt.append(value)
84 81
82 def _Options(self, p):
85 g = p.add_option_group('Sources') 83 g = p.add_option_group('Sources')
86 g.add_option('--cached', 84 g.add_option('--cached',
87 action='callback', callback=carry, 85 action='callback', callback=self._carry_option,
88 help='Search the index, instead of the work tree') 86 help='Search the index, instead of the work tree')
89 g.add_option('-r', '--revision', 87 g.add_option('-r', '--revision',
90 dest='revision', action='append', metavar='TREEish', 88 dest='revision', action='append', metavar='TREEish',
@@ -92,66 +90,66 @@ contain a line that matches both expressions:
92 90
93 g = p.add_option_group('Pattern') 91 g = p.add_option_group('Pattern')
94 g.add_option('-e', 92 g.add_option('-e',
95 action='callback', callback=carry, 93 action='callback', callback=self._carry_option,
96 metavar='PATTERN', type='str', 94 metavar='PATTERN', type='str',
97 help='Pattern to search for') 95 help='Pattern to search for')
98 g.add_option('-i', '--ignore-case', 96 g.add_option('-i', '--ignore-case',
99 action='callback', callback=carry, 97 action='callback', callback=self._carry_option,
100 help='Ignore case differences') 98 help='Ignore case differences')
101 g.add_option('-a', '--text', 99 g.add_option('-a', '--text',
102 action='callback', callback=carry, 100 action='callback', callback=self._carry_option,
103 help="Process binary files as if they were text") 101 help="Process binary files as if they were text")
104 g.add_option('-I', 102 g.add_option('-I',
105 action='callback', callback=carry, 103 action='callback', callback=self._carry_option,
106 help="Don't match the pattern in binary files") 104 help="Don't match the pattern in binary files")
107 g.add_option('-w', '--word-regexp', 105 g.add_option('-w', '--word-regexp',
108 action='callback', callback=carry, 106 action='callback', callback=self._carry_option,
109 help='Match the pattern only at word boundaries') 107 help='Match the pattern only at word boundaries')
110 g.add_option('-v', '--invert-match', 108 g.add_option('-v', '--invert-match',
111 action='callback', callback=carry, 109 action='callback', callback=self._carry_option,
112 help='Select non-matching lines') 110 help='Select non-matching lines')
113 g.add_option('-G', '--basic-regexp', 111 g.add_option('-G', '--basic-regexp',
114 action='callback', callback=carry, 112 action='callback', callback=self._carry_option,
115 help='Use POSIX basic regexp for patterns (default)') 113 help='Use POSIX basic regexp for patterns (default)')
116 g.add_option('-E', '--extended-regexp', 114 g.add_option('-E', '--extended-regexp',
117 action='callback', callback=carry, 115 action='callback', callback=self._carry_option,
118 help='Use POSIX extended regexp for patterns') 116 help='Use POSIX extended regexp for patterns')
119 g.add_option('-F', '--fixed-strings', 117 g.add_option('-F', '--fixed-strings',
120 action='callback', callback=carry, 118 action='callback', callback=self._carry_option,
121 help='Use fixed strings (not regexp) for pattern') 119 help='Use fixed strings (not regexp) for pattern')
122 120
123 g = p.add_option_group('Pattern Grouping') 121 g = p.add_option_group('Pattern Grouping')
124 g.add_option('--all-match', 122 g.add_option('--all-match',
125 action='callback', callback=carry, 123 action='callback', callback=self._carry_option,
126 help='Limit match to lines that have all patterns') 124 help='Limit match to lines that have all patterns')
127 g.add_option('--and', '--or', '--not', 125 g.add_option('--and', '--or', '--not',
128 action='callback', callback=carry, 126 action='callback', callback=self._carry_option,
129 help='Boolean operators to combine patterns') 127 help='Boolean operators to combine patterns')
130 g.add_option('-(', '-)', 128 g.add_option('-(', '-)',
131 action='callback', callback=carry, 129 action='callback', callback=self._carry_option,
132 help='Boolean operator grouping') 130 help='Boolean operator grouping')
133 131
134 g = p.add_option_group('Output') 132 g = p.add_option_group('Output')
135 g.add_option('-n', 133 g.add_option('-n',
136 action='callback', callback=carry, 134 action='callback', callback=self._carry_option,
137 help='Prefix the line number to matching lines') 135 help='Prefix the line number to matching lines')
138 g.add_option('-C', 136 g.add_option('-C',
139 action='callback', callback=carry, 137 action='callback', callback=self._carry_option,
140 metavar='CONTEXT', type='str', 138 metavar='CONTEXT', type='str',
141 help='Show CONTEXT lines around match') 139 help='Show CONTEXT lines around match')
142 g.add_option('-B', 140 g.add_option('-B',
143 action='callback', callback=carry, 141 action='callback', callback=self._carry_option,
144 metavar='CONTEXT', type='str', 142 metavar='CONTEXT', type='str',
145 help='Show CONTEXT lines before match') 143 help='Show CONTEXT lines before match')
146 g.add_option('-A', 144 g.add_option('-A',
147 action='callback', callback=carry, 145 action='callback', callback=self._carry_option,
148 metavar='CONTEXT', type='str', 146 metavar='CONTEXT', type='str',
149 help='Show CONTEXT lines after match') 147 help='Show CONTEXT lines after match')
150 g.add_option('-l', '--name-only', '--files-with-matches', 148 g.add_option('-l', '--name-only', '--files-with-matches',
151 action='callback', callback=carry, 149 action='callback', callback=self._carry_option,
152 help='Show only file names containing matching lines') 150 help='Show only file names containing matching lines')
153 g.add_option('-L', '--files-without-match', 151 g.add_option('-L', '--files-without-match',
154 action='callback', callback=carry, 152 action='callback', callback=self._carry_option,
155 help='Show only file names not containing matching lines') 153 help='Show only file names not containing matching lines')
156 154
157 def Execute(self, opt, args): 155 def Execute(self, opt, args):