From 5953d0c8bcf0583a4fc7da68ac90dd9aee4a26c0 Mon Sep 17 00:00:00 2001 From: Tim Miller Date: Mon, 20 Oct 2025 10:00:37 -0600 Subject: [PATCH] Rework extend_permute_identifier for more combinations - First, add a single char if maxlen allows - Next, walk backwards through identifier modifying one character at a time until a unique combination can be found --- carthage/utils.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/carthage/utils.py b/carthage/utils.py index 19bcf2cf..ae64d7ab 100644 --- a/carthage/utils.py +++ b/carthage/utils.py @@ -193,15 +193,25 @@ async def async_resolve(self): def permute_identifier(id, maxlen): - "Add to or replace the last character of the identifier; use as generator and stop consuming when a unique one is found" + """ + Use as a generator to yield a unique identifier. + + First attempt to yield the provided identifier, then just add one character if maxlen allows, + otherwise walk backwards through chars in id replacing one at a time until a unique combination is found. + + Raises ValueError if no unique combination can be found. + """ yield id if len(id) < maxlen: for i in range(26): yield id + chr(97 + i) else: - id = id[:-1] - for i in range(26): - yield id + chr(97 + i) + for i in range(len(id) - 1, -1, -1): + original_char = id[i] + for t in range(26): + if original_char == chr(97+t): + continue + yield id[:i] + chr(97+t) + id[i+1:] raise ValueError("No unique combination found")