summaryrefslogtreecommitdiff
path: root/git-p4.py
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2022-01-10 11:52:49 -0800
committerJunio C Hamano <gitster@pobox.com>2022-01-10 11:52:49 -0800
commit66f6c18e5b8706f07a9a8cbd06942d4c7835f7d7 (patch)
tree01bb5d8b523c4f03edada22ffd6fb2be18a41783 /git-p4.py
parent09481fec21f339d980fca369be3936964003d38b (diff)
parent0f829620e6f2ccc441aef1ffd6c0a546b949ee39 (diff)
downloadgit-66f6c18e5b8706f07a9a8cbd06942d4c7835f7d7.tar.gz
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
Diffstat (limited to 'git-p4.py')
-rwxr-xr-xgit-p4.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/git-p4.py b/git-p4.py
index 986595bef0..1b5694500a 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -59,6 +59,18 @@ p4_access_checked = False
re_ko_keywords = re.compile(br'\$(Id|Header)(:[^$\n]+)?\$')
re_k_keywords = re.compile(br'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)(:[^$\n]+)?\$')
+def format_size_human_readable(num):
+ """ Returns a number of units (typically bytes) formatted as a human-readable
+ string.
+ """
+ if num < 1024:
+ return '{:d} B'.format(num)
+ for unit in ["Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
+ num /= 1024.0
+ if num < 1024.0:
+ return "{:3.1f} {}B".format(num, unit)
+ return "{:.1f} YiB".format(num)
+
def p4_build_cmd(cmd):
"""Build a suitable p4 command line.
@@ -2957,7 +2969,8 @@ class P4Sync(Command, P4UserMap):
size = int(self.stream_file['fileSize'])
else:
size = 0 # deleted files don't get a fileSize apparently
- sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
+ sys.stdout.write('\r%s --> %s (%s)\n' % (
+ file_path, relPath, format_size_human_readable(size)))
sys.stdout.flush()
(type_base, type_mods) = split_p4_type(file["type"])
@@ -3052,9 +3065,8 @@ class P4Sync(Command, P4UserMap):
if not err and 'fileSize' in self.stream_file:
required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
if required_bytes > 0:
- err = 'Not enough space left on %s! Free at least %i MB.' % (
- os.getcwd(), required_bytes/1024/1024
- )
+ err = 'Not enough space left on %s! Free at least %s.' % (
+ os.getcwd(), format_size_human_readable(required_bytes))
if err:
f = None
@@ -3098,7 +3110,9 @@ class P4Sync(Command, P4UserMap):
size = int(self.stream_file["fileSize"])
if size > 0:
progress = 100*self.stream_file['streamContentSize']/size
- sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
+ sys.stdout.write('\r%s %d%% (%s)' % (
+ self.stream_file['depotFile'], progress,
+ format_size_human_readable(size)))
sys.stdout.flush()
self.stream_have_file_info = True
@@ -3611,7 +3625,8 @@ class P4Sync(Command, P4UserMap):
self.updateOptionDict(description)
if not self.silent:
- sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
+ sys.stdout.write("\rImporting revision %s (%d%%)" % (
+ change, (cnt * 100) // len(changes)))
sys.stdout.flush()
cnt = cnt + 1