Skip to content

Commit a53745e

Browse files
committed
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.
1 parent f9cd758 commit a53745e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/pkgdev/mangle.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,23 @@ def _eof(self, change):
7070

7171
@mangle("keywords")
7272
def _keywords(self, change):
73-
"""Fix keywords order."""
73+
"""Fix keywords order and destalbilize new versions."""
7474

7575
def keywords_sort_key(kw):
7676
return tuple(reversed(kw.lstrip("-~").partition("-")))
7777

78+
def keywords_remove_stable(kws):
79+
return [kw if kw.startswith("~") or kw.startswith("-") else "~" + kw for kw in kws]
80+
7881
lines = change.data.splitlines()
7982
for i, line in enumerate(lines):
8083
if mo := keywords_regex.match(line):
81-
kw = sorted(mo.group("keywords").split(), key=keywords_sort_key)
82-
new_kw = " ".join(kw)
84+
kws = sorted(mo.group("keywords").split(), key=keywords_sort_key)
85+
# Only remove stable keywords on new ebuild creations
86+
# For our purposes, renames are also new ebuild creations
87+
if change.status in ("A", "R"):
88+
kws = keywords_remove_stable(kws)
89+
new_kw = " ".join(kws)
8390
if not mo.group("quote"):
8491
new_kw = f'"{new_kw}"'
8592
lines[i] = f'{mo.group("pre")}{new_kw}{mo.group("post")}'

tests/scripts/test_pkgdev_commit.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,12 +1045,14 @@ def commit(args):
10451045
assert mo.group("begin") == years[:4] + "-"
10461046
assert mo.group("holder") == "Gentoo Authors"
10471047

1048+
# Keyword mangling when modifying existing ebuilds
10481049
for original, expected in (
10491050
('"arm64 amd64 x86"', "amd64 arm64 x86"),
10501051
('"arm64 amd64 ~x86"', "amd64 arm64 ~x86"),
10511052
('"arm64 ~x86 amd64"', "amd64 arm64 ~x86"),
10521053
('"arm64 ~x86 ~amd64"', "~amd64 arm64 ~x86"),
10531054
("arm64 ~x86 ~amd64", "~amd64 arm64 ~x86"),
1055+
("arm64 ~x86 ~amd64 -sparc", "~amd64 arm64 -sparc ~x86"),
10541056
):
10551057
# munge the keywords
10561058
with open(ebuild_path, "r+") as f:
@@ -1066,6 +1068,23 @@ def commit(args):
10661068
mo = keywords_regex.match(lines[-1])
10671069
assert mo.group("keywords") == expected
10681070

1071+
# Keyword mangling when adding new ebuilds
1072+
ebuild_path = repo.create_ebuild("cat/pkg-1", keywords=("arm64", "x86", "~amd64", "-sparc"))
1073+
commit(["-a", "-m", "version bump (no removal)"])
1074+
with open(ebuild_path) as f:
1075+
lines = f.read().splitlines()
1076+
mo = keywords_regex.match(lines[-1])
1077+
assert mo.group("keywords") == "~amd64 ~arm64 -sparc ~x86"
1078+
1079+
# Keyword mangling when adding and removing ebuilds simultaniously (git interpreted as rename)
1080+
git_repo.remove(ebuild_path, commit=False)
1081+
ebuild_path = repo.create_ebuild("cat/pkg-2", keywords=("arm64", "x86", "~amd64", "-sparc"))
1082+
commit(["-a", "-m", "version bump (no removal)"])
1083+
with open(ebuild_path) as f:
1084+
lines = f.read().splitlines()
1085+
mo = keywords_regex.match(lines[-1])
1086+
assert mo.group("keywords") == "~amd64 ~arm64 -sparc ~x86"
1087+
10691088
def test_scan(self, capsys, repo, make_git_repo):
10701089
git_repo = make_git_repo(repo.location)
10711090
repo.create_ebuild("cat/pkg-0")

0 commit comments

Comments
 (0)