From a53745e0793a2a2603ade2a9bfc8a488993e01d2 Mon Sep 17 00:00:00 2001 From: Anthony Ryan Date: Sun, 24 Nov 2024 18:34:55 -0500 Subject: [PATCH 1/2] Add a new `pkgdev commit` mangle to remove stable keywords One of the most common maintenance tasks in the tree is a simple version bump. With this mangle, `pkgdev commit` can automatically remove the stable keyword from ebuilds copied from a prior version. This should reduce contributor mistakes, and save developers time on both code review and manually editing keywords for their commits. --- src/pkgdev/mangle.py | 13 ++++++++++--- tests/scripts/test_pkgdev_commit.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/pkgdev/mangle.py b/src/pkgdev/mangle.py index 29b0f91..d031f37 100644 --- a/src/pkgdev/mangle.py +++ b/src/pkgdev/mangle.py @@ -70,16 +70,23 @@ 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 + if change.status in ("A", "R"): + 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..9af47bc 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,23 @@ 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 (no 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 (no 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" + def test_scan(self, capsys, repo, make_git_repo): git_repo = make_git_repo(repo.location) repo.create_ebuild("cat/pkg-0") From 30c7d3d1644b1b4dcc36648ed0d9dd504ed9b1c2 Mon Sep 17 00:00:00 2001 From: Anthony Ryan Date: Sun, 24 Nov 2024 22:35:33 -0500 Subject: [PATCH 2/2] Ensure keywords are not lost if packages are renamed Only redo the keywords if the PV changes over the git mv operation. --- src/pkgdev/mangle.py | 10 +++++++++- tests/scripts/test_pkgdev_commit.py | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/pkgdev/mangle.py b/src/pkgdev/mangle.py index d031f37..74602aa 100644 --- a/src/pkgdev/mangle.py +++ b/src/pkgdev/mangle.py @@ -84,7 +84,15 @@ def keywords_remove_stable(kws): 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 - if change.status in ("A", "R"): + 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"): diff --git a/tests/scripts/test_pkgdev_commit.py b/tests/scripts/test_pkgdev_commit.py index 9af47bc..5747791 100644 --- a/tests/scripts/test_pkgdev_commit.py +++ b/tests/scripts/test_pkgdev_commit.py @@ -1070,7 +1070,7 @@ def commit(args): # Keyword mangling when adding new ebuilds ebuild_path = repo.create_ebuild("cat/pkg-1", keywords=("arm64", "x86", "~amd64", "-sparc")) - commit(["-a", "-m", "version bump (no removal)"]) + 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]) @@ -1079,12 +1079,30 @@ def commit(args): # 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 (no removal)"]) + 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")