Skip to content

Instantly share code, notes, and snippets.

@maxking
Created October 18, 2016 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxking/2e02dde509920196485251aec4dc5801 to your computer and use it in GitHub Desktop.
Save maxking/2e02dde509920196485251aec4dc5801 to your computer and use it in GitHub Desktop.
# To run this script first install the 'gitpython' package from pip.
# $ pip install gitpython
#
# Usage:
# python git_grep.py repo_path filename regex
#
import os
import sys
import subprocess
import time
from git import Repo
def search(file_path, regex):
try:
start = time.time()
result = subprocess.check_output(['grep', regex, file_path])
total_time = time.time() - start
except subprocess.CalledProcessError:
return None
return total_time
if __name__ == '__main__':
_, repo_url, filename, regex = sys.argv
repo = Repo(repo_url)
file_path = os.path.join(repo_url, filename)
total_time = 0
for commit in repo.iter_commits('master'):
repo.git.checkout(commit)
result_time = search(file_path, regex)
if result_time is not None:
total_time += result_time
print("Total time for file {} is {} seconds.".format(
file_path, total_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment