diff --git a/beets/util/__init__.py b/beets/util/__init__.py index fd998ddef..e12264b9d 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -644,7 +644,7 @@ def unique_path(path: AnyStr) -> AnyStr: byte_path = os.fsencode(path) base, ext = os.path.splitext(byte_path) - match = re.search(rb"\.(\d)+$", base) + match = re.search(rb"\.(\d+)$", base) if match: num = int(match.group(1)) base = base[: match.start()] diff --git a/docs/changelog.rst b/docs/changelog.rst index 75bb94499..bfb97468d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -19,6 +19,10 @@ Bug fixes - A date range query written back to front (for example ``added:2024..2020``) no longer crashes with an uncaught ``ValueError``. The endpoints are now swapped, so such a range means the same as ``added:2020..2024``. +- Deduplicating a file whose name already ends in a counter of two or more + digits no longer restarts the numbering: ``track.10.mp3`` now yields + ``track.11.mp3`` instead of ``track.1.mp3``. The counter was matched with + ``\.(\d)+$``, which captures only the final digit. .. For plugin developers diff --git a/test/test_util.py b/test/test_util.py index f2c9f5c4b..f08d9b47a 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -428,6 +428,11 @@ class UniquePathTest(BeetsTestCase): path = util.unique_path(self.base / "x.1.mp3") assert path == str(self.base / "x.3.mp3") + def test_conflicting_file_with_multi_digit_number_increases_number(self): + (self.base / "w.10.mp3").touch() + path = util.unique_path(self.base / "w.10.mp3") + assert path == str(self.base / "w.11.mp3") + class MkDirAllTest(BeetsTestCase): def test_mkdirall(self):