Browse Source

Use git-grep instead of a db query

Use git-grep to look for the pattern, instead of searching the database.
Doug Hellmann 4 years ago
parent
commit
afe9eafba5
2 changed files with 17 additions and 12 deletions
  1. 2
    5
      aeromancer/cli/grep.py
  2. 15
    7
      aeromancer/project.py

+ 2
- 5
aeromancer/cli/grep.py View File

@@ -27,8 +27,5 @@ class Grep(Command):
27 27
         session = self.app.get_db_session()
28 28
         pm = project.ProjectManager(session)
29 29
         prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args)
30
-        for r in pm.grep(parsed_args.pattern, prj_filt):
31
-            line_num, content, filename, project_name = r
32
-            print('%s/%s:%s:%s' %
33
-                  (project_name, filename, line_num, content.rstrip())
34
-            )
30
+        for l in pm.grep(parsed_args.pattern, prj_filt):
31
+            print(l)

+ 15
- 7
aeromancer/project.py View File

@@ -205,12 +205,20 @@ class ProjectManager(object):
205 205
         # function on the db session here instead of when we connect?
206 206
         # We could pre-compile the regex and not pass it to each
207 207
         # invocation of the function.
208
-        query = self.session.query(
209
-            Line.number, Line.content, File.name, Project.name,
210
-        ).join(File, Project).filter(
211
-            Line.content.op('regexp')(pattern)
212
-        )
208
+        query = self.session.query(Project)
213 209
         if prj_filter:
214 210
             query = prj_filter.update_query(query)
215
-        query = query.order_by(Project.name, File.name, Line.number)
216
-        return query.yield_per(20).all()
211
+        query = query.order_by(Project.name)
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

Loading…
Cancel
Save