-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
52 lines (39 loc) · 2.31 KB
/
test.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
import argparse
import pathlib
from collections import defaultdict
import git
from gitdb.util import hex_to_bin
import timeit
import subprocess
def count_rev_list(repo):
print('Counting with rev-list - this will NOT count dangling objects.')
typecount = defaultdict(int)
for line in repo.git.rev_list('--objects', '--all').split('\n'):
binsha = hex_to_bin(line.split()[0])
oinfo = repo.odb.info(binsha)
typecount[oinfo.type] += 1
print(', '.join('{:s}s: {:d}'.format(k.decode('utf8').capitalize(), v) for k, v in sorted(typecount.items())), 'Total:', sum(typecount.values()))
def count_cat_file(repo):
print('Counting with cat-file - this WILL count dangling objects.')
typecount = defaultdict(int)
for line in repo.git.cat_file('--buffer', '--batch-all-objects', batch_check='%(objectname) %(objecttype)').split('\n'):
type = line.strip().split(' ')[1]
typecount[type] += 1
print(', '.join('{:s}s: {:d}'.format(k.capitalize(), v) for k, v in sorted(typecount.items())), 'Total:', sum(typecount.values()))
def count_cat_file_direct(path):
print('Counting with cat-file DIRECT - this WILL count dangling objects.')
typecount = defaultdict(int)
proc = subprocess.Popen(['git', '-C', str(path), 'cat-file', '--buffer', '--batch-all-objects', '--batch-check=%(objectname) %(objecttype)'], stdout=subprocess.PIPE)
for line in proc.stdout:
type = line.strip().split()[1]
typecount[type] += 1
print(', '.join('{:s}s: {:d}'.format(k.decode('utf8').capitalize(), v) for k, v in sorted(typecount.items())), 'Total:', sum(typecount.values()))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Git x ref.')
parser.add_argument('repository', metavar='repository', type=pathlib.Path,
help='Path to Git repository.')
args = parser.parse_args()
repo = git.Repo(str(args.repository), odbt=git.GitCmdObjectDB)
print(timeit.timeit('count_rev_list(repo)', setup='from __main__ import count_rev_list, repo', number=1), 'seconds.')
print(timeit.timeit('count_cat_file(repo)', setup='from __main__ import count_cat_file, repo', number=1), 'seconds.')
print(timeit.timeit('count_cat_file_direct(args.repository)', setup='from __main__ import count_cat_file_direct, args', number=1), 'seconds.')