Skip to content

Commit c7effdb

Browse files
committed
Add a custom git-grep command for my site-lisp
1 parent da3aef5 commit c7effdb

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

etc/elisp.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import subprocess
2+
import os
3+
import re
4+
5+
def shell_command_to_string (cmd):
6+
return subprocess.check_output (cmd.split (" "))
7+
8+
def file_name_directory (f):
9+
return os.path.dirname (f)
10+
11+
def file_exists_p (f):
12+
return os.path.exists (f)
13+
14+
def file_directory_p (f):
15+
return os.path.isdir (f)
16+
17+
def abbreviate_file_name (f, d):
18+
m = re.match (d, f)
19+
if m:
20+
return f[m.end () + 1:]
21+
22+
def expand_file_name (f, directory = None):
23+
if not directory:
24+
directory = os.getcwd ()
25+
return os.path.join (directory, f)
26+
27+
def directory_files (d):
28+
return os.listdir (d)

etc/git-grep

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python
2+
"""This script will grep for a regex in ./site-lisp and ./site-lisp/git/*.
3+
Prints the matches relative to ./site-lisp."""
4+
5+
import os
6+
import sys
7+
from elisp import *
8+
9+
def git_grep (regex):
10+
return "git --no-pager grep --full-name -n --no-color -i -e %s" % regex
11+
12+
def git_p (f):
13+
if file_directory_p (f):
14+
return file_exists_p (expand_file_name (".git", f))
15+
16+
def print_git (cmd, d = ''):
17+
try:
18+
res = shell_command_to_string (cmd)[:-1]
19+
for match in res.split ("\n"):
20+
print d + match
21+
except:
22+
pass
23+
24+
regex = sys.argv[1]
25+
cmd = git_grep (regex)
26+
27+
base_dir = "/home/oleh/Dropbox/source/site-lisp"
28+
git_dir = expand_file_name ("git", base_dir)
29+
dirs = [expand_file_name (f, git_dir) for f in directory_files (git_dir)]
30+
dirs = filter (git_p, dirs)
31+
dirs = [abbreviate_file_name (d, base_dir) for d in dirs]
32+
os.chdir (base_dir)
33+
print_git (cmd)
34+
for d in dirs:
35+
os.chdir (expand_file_name (d, base_dir))
36+
print_git (cmd, d + '/')

modes/ora-ivy.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@
3535
t
3636
'(("I" ivy-insert-action "insert")))
3737

38+
(setq counsel-git-grep-projects-alist
39+
'(("/home/oleh/Dropbox/source/site-lisp/" . "/home/oleh/Dropbox/source/site-lisp/etc/git-grep %S")))
40+
3841
(provide 'ora-ivy)

0 commit comments

Comments
 (0)