-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-meta.py
executable file
·387 lines (335 loc) · 13.1 KB
/
git-meta.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python2
# *-* coding: utf-8 *-*
from sys import stdout, exit
from os import popen, environ, walk
from os.path import join, exists, isfile
import re
from git import Repo, GitCommandError, InvalidGitRepositoryError, GitConfigParser
from ConfigParser import NoOptionError, NoSectionError, ParsingError
from textwrap import wrap
## retrieve the size of the terminal
rows, columns = popen('stty size', 'r').read().split()
class Bcolors:
"""
Enlighten the terminal with fancy colors !
"""
BOLD = '\033[1m'
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
list = ["HEADER",
"OKBLUE",
"OKGREEN",
"WARNING",
"FAIL",
"ENDC"]
def bold(self, color=None):
""" Return the bold balise for color
"""
## Get balise
if color != None:
exec("balise = self.%s"%(color))
else:
balise = ""
## Return bold color
return self.BOLD + balise
def disable(self):
self.BOLD = ''
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
class GitRepo:
"""
Repository object, to retrieve statuses and stuff
"""
def __init__(self,path):
self.bcolors = Bcolors()
self.path = path
self.er_num = 0
self.forward = ""
self.stashed = None
self.status = None
self.forward = None
self.repo = Repo(self.path)
if self.repo.git.rev_parse("--is-bare-repository") == 'true':
print self.path + "is a bare repository"
def get_error(self):
""" Return error number
"""
return self.er_num
def branchStatus(self):
"""
Retrieve all the information needed from the repo
"""
## Set stashed
if self.repo.git.stash('list') != "":
self.stashed = True
else:
self.stashed = False
## Set forward
nb_status_ok = 0
branch_status = {}
branch_forward = {}
verbose = []
active_branch = self.repo.active_branch
for branch in self.repo.branches:
try:
## Checkout the branch (make it active)
self.repo.git.checkout(str(branch))
## Extract the number of forward commit on the local branch
regex = re.compile(r'Your branch .+ by (\d+) commits?\.',re.M)
status = self.repo.git.status().split("\n")[1]
if regex.search(status):
branch_forward[str(branch)] = int(regex.sub(r'\1',status))
## Get the status of the branch
branch_status[str(branch)] = self.repo.git.status('--porcelain')
verbose.append(self.repo.git.status())
except GitCommandError:
branch_status[str(branch)] = 'Error while retrieving branch infos'
## Go back to original branch
self.repo.git.checkout(str(active_branch))
## Set self.forward
self.status = branch_status
self.forward = branch_forward
self.verbose = verbose
def push(self):
"""
Equivalent of a `git push` command
"""
if self.forward:
git = self.repo.git
try:
git.push()
self.forward = "pushed"
except:
self.forward = "push error - "+self.forward
def _get_str_len(self, string, dbg=False):
""" Return the lenght of a string without the bcolors code
"""
## Remove bold balise
string = string.replace(self.bcolors.bold(), "")
## Remove all color
for balise in self.bcolors.list:
exec("string = string.replace(self.bcolors.%s, '')"%(balise))
return len(string)
def globalStatus(self):
"""
Rerturn False if at least one branch is not clean
"""
if self.status == None:
self.branchStatus()
globalStatus = True
for branch in self.status:
if self.status[branch] != '':
globalStatus = False
return globalStatus
def printStatus(self, verbose=False):
"""
Print a one line status of the repo including stashes, forward commits
Display OK if there is nothig to display on <git status> command, NO otherwise
"""
status = ""
display = ""
stash = ""
forward = ""
display = ">> " + self.path
if self.globalStatus():
status = "[ "+self.bcolors.OKGREEN+"OK"+self.bcolors.ENDC+" ]"
else:
status = self.bcolors.bold() + "[ " + self.bcolors.bold("FAIL") +\
"NO" + self.bcolors.bold("ENDC") + self.bcolors.bold() + " ]" +\
self.bcolors.bold("ENDC")
display = self.bcolors.bold() + display + self.bcolors.ENDC
## Get stash
if self.stashed:
stash = "(" + self.bcolors.WARNING + "stash" + self.bcolors.ENDC + ")"
if self.forward:
forward = "("+ ",".join([branch+":"+str(self.forward[branch]) for branch in self.forward ]) + ")"
## Get forward
if verbose:
verboseString = "\n".join(self.verbose)
## Filling with whitespace
# Full string = display + whitespace + stash + status
# Get lenght of string without whitespace
str_len = self._get_str_len(display) + self._get_str_len(forward) + self._get_str_len(stash)\
+ self._get_str_len(status)
nb_whitespace = int(columns) - str_len
whitespace = "".join([" "]*nb_whitespace)
## Show display
if verbose and verbose != "":
display += whitespace + stash + forward + status + "\n" + verboseString
else:
display += whitespace + stash + forward + status
print display
class GitMeta:
""" GitMeta class
"""
def __init__(self, force_scan=False, root=environ['HOME'], followlinks=False):
self.followlinks = followlinks
self.fname = environ['HOME']+'/.gitdb'
self.ignore_fname = environ['HOME']+'/.gitdb_ignore'
self.root = root
self.repos = []
self.gitrepos = []
self.ignore = []
self.update_db(force_scan)
try:
self.animate = self.config('animate')
except (NoSectionError, NoOptionError, ParsingError):
self.animate = False
def config(self,item):
"""
Get a variable from the git config file under the meta section
"""
config = GitConfigParser(environ['HOME']+"/.gitconfig")
return config.get_value('meta', item)
def update_db(self, force_scan):
"""
Keep the list of repositories up to date
"""
## Get ignore repo from ignore_fname
if exists(self.ignore_fname):
f2 = open(self.ignore_fname,'r')
self.ignore = [line.replace('\n', '') for line in f2.readlines()]
f2.close()
else:
self.ignore = []
## Get repos list
if force_scan or not exists(self.fname):
f = open(self.fname, 'w')
self.repos = self.scan(f)
f.close()
else:
f = open(self.fname, 'r')
self.repos = [line.replace('\n', '') for line in f.readlines()]
f.close()
self.clean_db()
def clean_db(self):
"""
Remove repo which not exists on the disk
"""
## init local variable
clean_repos = [repo for repo in self.repos if exists(repo)]
remov_repos = [repo for repo in self.repos if not exists(repo)]
## Remove repos in data base
if remov_repos != []:
## Print warning and which repo will be removed
print "/!\\ Following repo not exist and will be removed in the data base"
for repo in remov_repos:
print "/!\\ '%s' removed from data base"%(repo)
print
## Update data base file
with open(self.fname, 'w') as f:
f.write("\n".join(clean_repos))
## Update self.repos list
self.repos = clean_repos
def scan(self, fname):
"""
Scan the computer to find all repositories
"""
## init local variable
repos = []
## Init scanning animation
base = "=== Scanning"
animation = "|/-\\"
count=0
## Prepare regexp list for verification
ignore = "|".join(self.ignore)
## Walk in repo and get git repo
for root, dirs, files in walk(self.root, followlinks=self.followlinks):
for f2 in files:
fpath = join(root,f2)
## if this is a repo ...
if re.search(r'.git/config$',fpath):
fpath = fpath.replace('/.git/config','')
## ... and is not ignored
if ignore == "" or not re.search(ignore,fpath):
repos.append(fpath)
## Animation
if count % 11 == 0:
stdout.write("\r"+base+" "+animation[count % len(animation)]+" ")
stdout.flush()
count += 1
stdout.write("\r=== "+str(len(repos))+" repos found\n")
## Write repo in fname
for item in repos:
fname.write("%s\n" % item)
return repos
def buildList(self,list_all=False,push_all=False):
"""
Check statuses of all repositories listed in .gitdb file
"""
print "=== Loading"
## Walk in repo and get GitRepo
for repo in self.repos:
if repo not in self.ignore or list_all:
a = GitRepo(repo)
if not self.animate:
a.branchStatus()
if a.globalStatus():
if a.forward and push_all:
a.push()
self.gitrepos.append(a)
print "\r=== "+str(len(self.gitrepos))+" repos scanned"
def printStatuses(self, verbose=False, sortNOOK=True, reverse=True, select=None):
""" Print status for repo
"""
## Sort repo by status NO/OK
if sortNOOK:
self.animate = False
self.gitrepos = sorted(self.gitrepos, key=lambda repo: ~repo.globalStatus(), reverse=reverse)
## Get only 'select' status git repo
if select is not None:
self.animate = False
if select == "no":
self.gitrepos = [ repo for repo in self.gitrepos if repo.globalStatus() == False ]
elif select == "ok":
self.gitrepos = [ repo for repo in self.gitrepos if repo.globalStatus() == True ]
else:
raise ValueError("'%s' not a valid value for --select option"%(select))
## Show full list
if select != None:
print "=== %d repos selected by '%s'\n"%(len(self.gitrepos), select)
else:
print ""
for repo in self.gitrepos:
if self.animate:
repo.branchStatus()
repo.printStatus(verbose=verbose)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Verify all git repository.')
parser.add_argument('-a', '--all', dest='list_all',
action='store_true', default=False,
help='List all the repositories even those ignored')
parser.add_argument('-P', '--push-all', dest='push_all',
action='store_true', default=False,
help='Push all repositories to their remote servers')
parser.add_argument('-s', '--scan', dest='scan',
action='store_true', default=False,
help='Perform a complete tree scan of your data to search for git repositories')
parser.add_argument("--followlinks", dest="followlinks", action="store_true",
help="Follow simlink during the scan process (default false)"
)
parser.add_argument('-v', '--verbose', dest='verbose',
action='store_true', default=False,
help="Active verbose mode: print git status for 'NO' repo")
parser.add_argument('-S', '--sort', dest='sortNOOK',
action='store_true', default=False,
help="Sort git repo by status NO/OK (NO first)")
parser.add_argument('--reverse', dest='reverse',
action='store_true', default=False,
help="Reverse sort function")
parser.add_argument('--select', dest='select', type=str,
action='store', choices=('ok', 'no', 'other'),
help="Select only git repo by status. value=ok|no")
args = parser.parse_args()
verif = GitMeta(args.scan, followlinks=args.followlinks)
verif.buildList(args.list_all,args.push_all)
verif.printStatuses(args.verbose, args.sortNOOK, args.reverse, args.select)