-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-du.py
executable file
·270 lines (183 loc) · 7.5 KB
/
git-du.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
#!/usr/bin/env python3
from __future__ import print_function
import datetime
import math
import re
import subprocess
import sys
packed_objects = {}
seen_objects = {}
### Logging functions #########################################################
is_nln = False
nln_len = 0
def log_write (logstr):
global is_nln, nln_len
if (is_nln): sys.stderr.write ("\r" + (" " * nln_len) + "\r")
print (logstr, file = sys.stderr)
sys.stderr.flush()
is_nln = False
def log_write_nln (logstr):
global is_nln, nln_len
sys.stderr.write ("\r" + logstr)
sys.stderr.flush()
nln_len = len (logstr)
is_nln = True
###############################################################################
### Git functions #############################################################
def get_git_dir (working_dir):
git_dir = "";
hnd = subprocess.Popen (["git", "-C", working_dir, "rev-parse", "--show-toplevel"], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ();
if (hnd.returncode == 0):
for line in streams[0].splitlines ():
git_dir = line.decode() + "/.git"
break
return git_dir
def get_pack_files (git_dir):
packs = []
hnd = subprocess.Popen (["find", git_dir + "/objects/pack", "-iname", "pack-*.idx"], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ();
if (hnd.returncode == 0):
for line in streams[0].splitlines ():
packs.append (line)
return packs
def get_commits (git_dir):
commits = []
hnd = subprocess.Popen (["git", "-C", git_dir, "rev-list", "--all", "--reverse", "--timestamp"], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ();
if (hnd.returncode == 0):
for line in streams[0].splitlines ():
parts = line.split (None)
commits.append ({
'tstamp': parts[0],
'id': parts[1]
})
return commits
def get_packed_objects (git_dir, packs):
global packed_objects
packed_objects = {}
m = re.compile (b"[0-9a-f]{40}")
cmd = ["git", "-C", git_dir, "verify-pack", "-v"]
cmd.extend (packs)
hnd = subprocess.Popen (cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ()
if (hnd.returncode == 0):
for line in streams[0].splitlines ():
parts = line.split (None)
if (m.match (parts[0])):
if (parts[0] not in packed_objects):
packed_objects[parts[0]] = parts
def get_unpacked_size (git_dir, object_id):
object_size = 0
hnd = subprocess.Popen (["git", "-C", git_dir, "cat-file", "-s", object_id], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ();
if (hnd.returncode == 0):
object_size = int (streams[0])
else:
log_write ("# ERROR: git-cat-file failed (object_id: " + object_id.decode() + ")")
return object_size
def get_commit_tree (git_dir, commit_id):
objects = []
hnd = subprocess.Popen (["git", "-C", git_dir, "cat-file", "-p", commit_id], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ();
if (hnd.returncode == 0):
line = streams[0].splitlines ().pop (0)
parts = line.split (None)
if (parts[0] == b"tree"):
objects.append ({
'type': parts[0],
'id': parts[1]
})
else:
log_write ("# ERROR: cannot find a tree from commit")
else:
log_write ("# ERROR: git-cat-file failed (commit_id: " + commit_id.decode() + ")")
return objects
def get_tree_objects (git_dir, tree_id):
objects = []
hnd = subprocess.Popen (["git", "-C", git_dir, "cat-file", "-p", tree_id], stdout = subprocess.PIPE, stderr = subprocess.PIPE, close_fds = True)
streams = hnd.communicate ();
if (hnd.returncode == 0):
for line in streams[0].splitlines ():
parts = line.split (None)
objects.append ({
'type': parts[1],
'id': parts[2]
})
else:
log_write ("# ERROR: git-cat-file failed (tree_id: " + tree_id.decode() + ")")
return objects
###############################################################################
### Parsing functions ######################################################
def get_object_size (size, git_dir, object_id):
object_size = 0
if (object_id in packed_objects):
object_size = int (packed_objects[object_id][3])
size['packed'] = size['packed'] + object_size
else:
object_size = get_unpacked_size (git_dir, object_id)
size['unpacked'] = size['unpacked'] + object_size
return object_size
def object_seen (object_id):
global seen_objects
if (object_id in seen_objects):
return True
else:
seen_objects[object_id] = 1
return False
def get_recursive_size (size, git_dir, object_type, object_id, state):
if (object_seen (object_id) == False):
get_object_size (size, git_dir, object_id)
state['objects'] = state['objects'] + 1
# log_write_nln ("# " + str (state['commits']) + "/" + str (state['total_commits']) + " commits done (" + str (state['objects']) + " object(s) found)")
sub_objects = []
if (object_type == b"commit"):
sub_objects = get_commit_tree (git_dir, object_id)
elif (object_type == b"tree"):
sub_objects = get_tree_objects (git_dir, object_id)
elif (object_type != b"blob"):
log_write ("# WARNING: unknown object type (" + object_type.decode() + ")")
for sub_object in sub_objects:
get_recursive_size (size, git_dir, sub_object['type'], sub_object['id'], state)
return size
###############################################################################
if (len (sys.argv) > 1):
working_dir = sys.argv[1]
else:
working_dir = "."
# Git directory...
git_dir = get_git_dir (working_dir);
if (git_dir != ""):
log_write ("# Repository directory: " + git_dir)
else:
log_write ("# ERROR: cannot detect repository root directory!")
sys.exit (1)
# Packed objects...
packs = get_pack_files (git_dir)
if (len (packs) != 0):
log_write ("# Fetching all packed object sizes...")
get_packed_objects (git_dir, get_pack_files (git_dir))
else:
log_write ("# WARNING: cannot find any pack files!")
# Commits...
log_write ("# Fetching all commits...")
commits = get_commits (git_dir)
if (len (commits) == 0):
log_write ("# ERROR: cannot find any commits!")
sys.exit (1)
# Hard work...
log_write ("# Fetching all related objects...")
state = { 'objects': 0, 'commits': 0, 'total_commits': len (commits) }
total_packed_size = 0
total_unpacked_size = 0
for commit in commits:
state['commits'] = state['commits'] + 1
size = { 'packed': 0, 'unpacked': 0 }
get_recursive_size (size, git_dir, b"commit", commit['id'], state)
total_packed_size = total_packed_size + size['packed']
total_unpacked_size = total_unpacked_size + size['unpacked']
print (commit['tstamp'].decode() + " " + commit['id'].decode() + " " + str (size['packed'] + size['unpacked']))
# Some stats...
log_write ("# Total packed size: " + str (total_packed_size))
log_write ("# Total unpacked size: " + str (total_unpacked_size))
log_write ("# Total size: " + str (total_packed_size + total_unpacked_size))