This repository was archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitrep.py
More file actions
executable file
·78 lines (59 loc) · 1.92 KB
/
Copy pathgitrep.py
File metadata and controls
executable file
·78 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
import os
import sys
from blessings import Terminal
from git import Repo
t = Terminal()
def parse(directory):
"""Find paths to all the git repos in the given directory."""
repos = []
os.path.walk(directory, visit, repos)
return repos
def visit(repos, dirname, names):
"""Process a directory encountered while walking the hierarchy. Skip module
directories on the fly. If a git repo is detected, add its path to `repos`
and skip the .git directory."""
# Skip module directories
SKIP_DIRS = ['node_modules', 'bower_components']
for skip_dir in SKIP_DIRS:
try:
names.remove(skip_dir)
except:
pass
# Detect git repo
if '.git' in names:
repos.append(dirname)
names.remove('.git')
def repos(paths):
"""Generate a list of Repo objects from the given paths."""
return [Repo(path) for path in paths]
def print_repo_info(repo):
"""Formats and prints the number of remotes and the repo's path."""
# Get absolute path to the parent directory of .git
abspath = os.path.split(repo.git_dir)[0]
# Get and the relative path and split it
path = os.path.split(os.path.relpath(abspath))
# Make the basename bold
title = path[0] + '/' + t.bold(path[1])
# See if it has any remotes
n = len(repo.remotes)
if n > 1:
symbol = t.bold_blue(' ' + str(n) + ' ')
elif n == 1:
symbol = ' 1 '
else:
symbol = t.yellow(' 0 ')
print symbol, title
if __name__ == '__main__':
# Check presence of arguments
if len(sys.argv) < 2:
# Use current directory path
path = os.curdir
else:
# Get directory path from arguments
path = sys.argv[1]
# Check that the path is indeed a directory
if not os.path.isdir(path):
print 'error:', path, 'is not a directory.'
for repo in repos(parse(path)):
print_repo_info(repo)