summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2015-01-21 20:37:06 -0500
committerDoug Hellmann <doug@doughellmann.com>2015-01-21 20:37:06 -0500
commitafe9eafba5c1583f045a0a9ccc5de14eec96d2b2 (patch)
tree0fa972df4b7ba01d288ccda3938f8efb1da90b17
parent80d5fc4ec2711dde9bd7cf775e9376190c46f9f7 (diff)
Use git-grep instead of a db query
Use git-grep to look for the pattern, instead of searching the database.
-rw-r--r--aeromancer/cli/grep.py7
-rw-r--r--aeromancer/project.py22
2 files changed, 17 insertions, 12 deletions
diff --git a/aeromancer/cli/grep.py b/aeromancer/cli/grep.py
index d7cb92d..6c79b26 100644
--- a/aeromancer/cli/grep.py
+++ b/aeromancer/cli/grep.py
@@ -27,8 +27,5 @@ class Grep(Command):
27 session = self.app.get_db_session() 27 session = self.app.get_db_session()
28 pm = project.ProjectManager(session) 28 pm = project.ProjectManager(session)
29 prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args) 29 prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args)
30 for r in pm.grep(parsed_args.pattern, prj_filt): 30 for l in pm.grep(parsed_args.pattern, prj_filt):
31 line_num, content, filename, project_name = r 31 print(l)
32 print('%s/%s:%s:%s' %
33 (project_name, filename, line_num, content.rstrip())
34 )
diff --git a/aeromancer/project.py b/aeromancer/project.py
index f9d8d4f..e1bf9a7 100644
--- a/aeromancer/project.py
+++ b/aeromancer/project.py
@@ -205,12 +205,20 @@ class ProjectManager(object):
205 # function on the db session here instead of when we connect? 205 # function on the db session here instead of when we connect?
206 # We could pre-compile the regex and not pass it to each 206 # We could pre-compile the regex and not pass it to each
207 # invocation of the function. 207 # invocation of the function.
208 query = self.session.query( 208 query = self.session.query(Project)
209 Line.number, Line.content, File.name, Project.name,
210 ).join(File, Project).filter(
211 Line.content.op('regexp')(pattern)
212 )
213 if prj_filter: 209 if prj_filter:
214 query = prj_filter.update_query(query) 210 query = prj_filter.update_query(query)
215 query = query.order_by(Project.name, File.name, Line.number) 211 query = query.order_by(Project.name)
216 return query.yield_per(20).all() 212 #return query.yield_per(20).all()
213 for project in query.all():
214 cmd = subprocess.Popen(
215 ['git', 'grep', pattern],
216 stdout=subprocess.PIPE,
217 cwd=project.path,
218 env={'PAGER': ''},
219 )
220 out, err = cmd.communicate()
221 if not out:
222 continue
223 for line in out.decode('utf-8').splitlines():
224 yield project.name + line