Skip to content


Add a custom git-grep command for my site-lisp
Browse files Browse the repository at this point in the history
  • Loading branch information
abo-abo committed Aug 4, 2016
1 parent da3aef5 commit c7effdb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions etc/
@@ -0,0 +1,28 @@
import subprocess
import os
import re

def shell_command_to_string (cmd):
return subprocess.check_output (cmd.split (" "))

def file_name_directory (f):
return os.path.dirname (f)

def file_exists_p (f):
return os.path.exists (f)

def file_directory_p (f):
return os.path.isdir (f)

def abbreviate_file_name (f, d):
m = re.match (d, f)
if m:
return f[m.end () + 1:]

def expand_file_name (f, directory = None):
if not directory:
directory = os.getcwd ()
return os.path.join (directory, f)

def directory_files (d):
return os.listdir (d)
36 changes: 36 additions & 0 deletions etc/git-grep
@@ -0,0 +1,36 @@
"""This script will grep for a regex in ./site-lisp and ./site-lisp/git/*.
Prints the matches relative to ./site-lisp."""

import os
import sys
from elisp import *

def git_grep (regex):
return "git --no-pager grep --full-name -n --no-color -i -e %s" % regex

def git_p (f):
if file_directory_p (f):
return file_exists_p (expand_file_name (".git", f))

def print_git (cmd, d = ''):
res = shell_command_to_string (cmd)[:-1]
for match in res.split ("\n"):
print d + match

regex = sys.argv[1]
cmd = git_grep (regex)

base_dir = "/home/oleh/Dropbox/source/site-lisp"
git_dir = expand_file_name ("git", base_dir)
dirs = [expand_file_name (f, git_dir) for f in directory_files (git_dir)]
dirs = filter (git_p, dirs)
dirs = [abbreviate_file_name (d, base_dir) for d in dirs]
os.chdir (base_dir)
print_git (cmd)
for d in dirs:
os.chdir (expand_file_name (d, base_dir))
print_git (cmd, d + '/')
3 changes: 3 additions & 0 deletions modes/ora-ivy.el
Expand Up @@ -35,4 +35,7 @@
'(("I" ivy-insert-action "insert")))

(setq counsel-git-grep-projects-alist
'(("/home/oleh/Dropbox/source/site-lisp/" . "/home/oleh/Dropbox/source/site-lisp/etc/git-grep %S")))

(provide 'ora-ivy)

0 comments on commit c7effdb

Please sign in to comment.