diff --git a/src/pkgdev/mangle.py b/src/pkgdev/mangle.py index 29b0f91..74602aa 100644 --- a/src/pkgdev/mangle.py +++ b/src/pkgdev/mangle.py @@ -70,16 +70,31 @@ def _eof(self, change): @mangle("keywords") def _keywords(self, change): - """Fix keywords order.""" + """Fix keywords order and destalbilize new versions.""" def keywords_sort_key(kw): return tuple(reversed(kw.lstrip("-~").partition("-"))) + def keywords_remove_stable(kws): + return [kw if kw.startswith("~") or kw.startswith("-") else "~" + kw for kw in kws] + lines = change.data.splitlines() for i, line in enumerate(lines): if mo := keywords_regex.match(line): - kw = sorted(mo.group("keywords").split(), key=keywords_sort_key) - new_kw = " ".join(kw) + kws = sorted(mo.group("keywords").split(), key=keywords_sort_key) + # Only remove stable keywords on new ebuild creations + # For our purposes, renames are also new ebuild creations + print(change.atom.version) + # print(change.atom.revision) + if change.status == "A": + kws = keywords_remove_stable(kws) + if ( + change.status == "R" + and change.old is not None + and change.old.version != change.atom.version + ): + kws = keywords_remove_stable(kws) + new_kw = " ".join(kws) if not mo.group("quote"): new_kw = f'"{new_kw}"' lines[i] = f'{mo.group("pre")}{new_kw}{mo.group("post")}' diff --git a/tests/scripts/test_pkgdev_commit.py b/tests/scripts/test_pkgdev_commit.py index 04fc16a..5747791 100644 --- a/tests/scripts/test_pkgdev_commit.py +++ b/tests/scripts/test_pkgdev_commit.py @@ -1045,12 +1045,14 @@ def commit(args): assert mo.group("begin") == years[:4] + "-" assert mo.group("holder") == "Gentoo Authors" + # Keyword mangling when modifying existing ebuilds for original, expected in ( ('"arm64 amd64 x86"', "amd64 arm64 x86"), ('"arm64 amd64 ~x86"', "amd64 arm64 ~x86"), ('"arm64 ~x86 amd64"', "amd64 arm64 ~x86"), ('"arm64 ~x86 ~amd64"', "~amd64 arm64 ~x86"), ("arm64 ~x86 ~amd64", "~amd64 arm64 ~x86"), + ("arm64 ~x86 ~amd64 -sparc", "~amd64 arm64 -sparc ~x86"), ): # munge the keywords with open(ebuild_path, "r+") as f: @@ -1066,6 +1068,41 @@ def commit(args): mo = keywords_regex.match(lines[-1]) assert mo.group("keywords") == expected + # Keyword mangling when adding new ebuilds + ebuild_path = repo.create_ebuild("cat/pkg-1", keywords=("arm64", "x86", "~amd64", "-sparc")) + commit(["-a", "-m", "version bump (type A, without removal)"]) + with open(ebuild_path) as f: + lines = f.read().splitlines() + mo = keywords_regex.match(lines[-1]) + assert mo.group("keywords") == "~amd64 ~arm64 -sparc ~x86" + + # Keyword mangling when adding and removing ebuilds simultaniously (git interpreted as rename) + git_repo.remove(ebuild_path, commit=False) + ebuild_path = repo.create_ebuild("cat/pkg-2", keywords=("arm64", "x86", "~amd64", "-sparc")) + commit(["-a", "-m", "version bump (type R, with removal)"]) + with open(ebuild_path) as f: + lines = f.read().splitlines() + mo = keywords_regex.match(lines[-1]) + assert mo.group("keywords") == "~amd64 ~arm64 -sparc ~x86" + + # Keyword mangling when moving a package to another category + ebuild_path = repo.create_ebuild( + "oldcat/oldname-0", keywords=("arm64", "x86", "~amd64", "-sparc") + ) + git_repo.add_all("oldcat/oldname-0") + os.mkdir(os.path.join(git_repo.path, "newcat")) + os.mkdir(os.path.join(git_repo.path, "newcat/newname")) + git_repo.move( + pjoin(git_repo.path, "oldcat/oldname/oldname-0.ebuild"), + pjoin(git_repo.path, "newcat/newname/newname-0.ebuild"), + commit=False, + ) + commit(["-a", "-m", "rename package (type R, but no PV change)"]) + with open(os.path.join(git_repo.path, "newcat/newname/newname-0.ebuild")) as f: + lines = f.read().splitlines() + mo = keywords_regex.match(lines[-1]) + assert mo.group("keywords") == "~amd64 arm64 -sparc x86" + def test_scan(self, capsys, repo, make_git_repo): git_repo = make_git_repo(repo.location) repo.create_ebuild("cat/pkg-0")