Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Last active August 29, 2015 14:00
Show Gist options
  • Save sangheestyle/11187957 to your computer and use it in GitHub Desktop.
Save sangheestyle/11187957 to your computer and use it in GitHub Desktop.
git filter among repos
import os
from subprocess import check_output, CalledProcessError
def get_repo_paths(root=None):
repo_paths = []
for dir, _, _ in os.walk(root):
if dir.endswith('/.git'):
repo_paths.append(dir[:-5])
return repo_paths
def get_commit_ids_by_permission(repo_path=None, permission=None):
if permission is None:
permission = '.*'
current_path = os.getcwd()
os.chdir(repo_path)
template_git_command = ["git", "log", "--pretty=format:%h"]
permission_rex = "<.*uses-permission.*\." + permission + ".*/>"
template_permission = ["-G", permission_rex]
template_path = ["--", "*AndroidManifest.xml"]
command = []
command.extend(template_git_command)
command.extend(template_permission)
command.extend(template_path)
output = check_output(command)
try:
output = check_output(command)
except CalledProcessError:
commit_ids = []
os.chdir(current_path)
commit_ids = output.split()
return commit_ids
root = '../repo'
for repo_path in get_repo_paths(root):
print ">>>", repo_path
print len(get_commit_ids_by_permission(repo_path))
@sangheestyle
Copy link
Author

The following result represent that `../repo/github/android' repo has 10 commits related to all permisissions in all AndroidManifest.xml.

$ python repo.py 
>>> ../repo/gihub/android
10
>>> ../repo/johnkil/Android-AppMsg
0
>>> ../repo/owncloud/android
6

FIXME:
However, some repositories make fatal error due to no head which means that the repository is not well formed. I can't handle that kind of error even I used try and except.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment