Skip to content

Commit 66f6c18

Browse files
committed
Merge branch 'jh/p4-human-unit-numbers'
The way "git p4" shows file sizes in its output has been updated to use human-readable units. * jh/p4-human-unit-numbers: git-p4: show progress as an integer git-p4: print size values in appropriate units
2 parents 09481fe + 0f82962 commit 66f6c18

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

git-p4.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959
re_ko_keywords = re.compile(br'\$(Id|Header)(:[^$\n]+)?\$')
6060
re_k_keywords = re.compile(br'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)(:[^$\n]+)?\$')
6161

62+
def format_size_human_readable(num):
63+
""" Returns a number of units (typically bytes) formatted as a human-readable
64+
string.
65+
"""
66+
if num < 1024:
67+
return '{:d} B'.format(num)
68+
for unit in ["Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
69+
num /= 1024.0
70+
if num < 1024.0:
71+
return "{:3.1f} {}B".format(num, unit)
72+
return "{:.1f} YiB".format(num)
73+
6274
def p4_build_cmd(cmd):
6375
"""Build a suitable p4 command line.
6476
@@ -2957,7 +2969,8 @@ def streamOneP4File(self, file, contents):
29572969
size = int(self.stream_file['fileSize'])
29582970
else:
29592971
size = 0 # deleted files don't get a fileSize apparently
2960-
sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
2972+
sys.stdout.write('\r%s --> %s (%s)\n' % (
2973+
file_path, relPath, format_size_human_readable(size)))
29612974
sys.stdout.flush()
29622975

29632976
(type_base, type_mods) = split_p4_type(file["type"])
@@ -3052,9 +3065,8 @@ def streamP4FilesCb(self, marshalled):
30523065
if not err and 'fileSize' in self.stream_file:
30533066
required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
30543067
if required_bytes > 0:
3055-
err = 'Not enough space left on %s! Free at least %i MB.' % (
3056-
os.getcwd(), required_bytes/1024/1024
3057-
)
3068+
err = 'Not enough space left on %s! Free at least %s.' % (
3069+
os.getcwd(), format_size_human_readable(required_bytes))
30583070

30593071
if err:
30603072
f = None
@@ -3098,7 +3110,9 @@ def streamP4FilesCb(self, marshalled):
30983110
size = int(self.stream_file["fileSize"])
30993111
if size > 0:
31003112
progress = 100*self.stream_file['streamContentSize']/size
3101-
sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
3113+
sys.stdout.write('\r%s %d%% (%s)' % (
3114+
self.stream_file['depotFile'], progress,
3115+
format_size_human_readable(size)))
31023116
sys.stdout.flush()
31033117

31043118
self.stream_have_file_info = True
@@ -3611,7 +3625,8 @@ def importChanges(self, changes, origin_revision=0):
36113625
self.updateOptionDict(description)
36123626

36133627
if not self.silent:
3614-
sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
3628+
sys.stdout.write("\rImporting revision %s (%d%%)" % (
3629+
change, (cnt * 100) // len(changes)))
36153630
sys.stdout.flush()
36163631
cnt = cnt + 1
36173632

0 commit comments

Comments
 (0)