From afe9eafba5c1583f045a0a9ccc5de14eec96d2b2 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Wed, 21 Jan 2015 20:37:06 -0500 Subject: [PATCH] Use git-grep instead of a db query Use git-grep to look for the pattern, instead of searching the database. --- aeromancer/cli/grep.py | 7 ++----- aeromancer/project.py | 22 +++++++++++++++------- 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): session = self.app.get_db_session() pm = project.ProjectManager(session) prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args) - for r in pm.grep(parsed_args.pattern, prj_filt): - line_num, content, filename, project_name = r - print('%s/%s:%s:%s' % - (project_name, filename, line_num, content.rstrip()) - ) + for l in pm.grep(parsed_args.pattern, prj_filt): + print(l) 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): # function on the db session here instead of when we connect? # We could pre-compile the regex and not pass it to each # invocation of the function. - query = self.session.query( - Line.number, Line.content, File.name, Project.name, - ).join(File, Project).filter( - Line.content.op('regexp')(pattern) - ) + query = self.session.query(Project) if prj_filter: query = prj_filter.update_query(query) - query = query.order_by(Project.name, File.name, Line.number) - return query.yield_per(20).all() + query = query.order_by(Project.name) + #return query.yield_per(20).all() + for project in query.all(): + cmd = subprocess.Popen( + ['git', 'grep', pattern], + stdout=subprocess.PIPE, + cwd=project.path, + env={'PAGER': ''}, + ) + out, err = cmd.communicate() + if not out: + continue + for line in out.decode('utf-8').splitlines(): + yield project.name + line