Created
October 18, 2016 23:59
-
-
Save maxking/2e02dde509920196485251aec4dc5801 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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