Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 333ea75

Browse files
committed
Refactor xargs.partition: _command_length usage
1 parent bb6b1c3 commit 333ea75

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

pre_commit/xargs.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ def _get_platform_max_length():
1313
return 4 * 1024
1414

1515

16-
def _get_command_length(command, arg):
17-
parts = command + (arg,)
18-
full_cmd = ' '.join(parts)
16+
def _command_length(*cmd):
17+
full_cmd = ' '.join(cmd)
1918

2019
# win32 uses the amount of characters, more details at:
2120
# https://blogs.msdn.microsoft.com/oldnewthing/20031210-00/?p=41553/
@@ -38,17 +37,21 @@ def partition(cmd, varargs, _max_length=None):
3837
# Reversed so arguments are in order
3938
varargs = list(reversed(varargs))
4039

40+
total_length = _command_length(*cmd)
4141
while varargs:
4242
arg = varargs.pop()
4343

44-
if _get_command_length(cmd + tuple(ret_cmd), arg) <= _max_length:
44+
arg_length = _command_length(arg) + 1
45+
if total_length + arg_length <= _max_length:
4546
ret_cmd.append(arg)
47+
total_length += arg_length
4648
elif not ret_cmd:
4749
raise ArgumentTooLongError(arg)
4850
else:
4951
# We've exceeded the length, yield a command
5052
ret.append(cmd + tuple(ret_cmd))
5153
ret_cmd = []
54+
total_length = _command_length(*cmd)
5255
varargs.append(arg)
5356

5457
ret.append(cmd + tuple(ret_cmd))

0 commit comments

Comments
 (0)