Skip to content

Commit d0da008

Browse files
changelog: ignore entries with wrong day padding (#508)
changelog: ignore entries with wrong day padding This first version of this PR was completely implemented by Claude Code and Ambient Code. After Nikola's amazing review, I fixed the problems myself, especially tinkering with padding fixups and updating the regex that parses day of month to properly account for double-digit days (and don't stop at the first digit - thanks Claude for that one). Fixes #216 RELEASE NOTES BEGIN Changelog entries that have incorrect padding set for a day of a month are now fixed if possible or ignored. RELEASE NOTES END Reviewed-by: gemini-code-assist[bot] Reviewed-by: Nikola Forró Reviewed-by: Tomas Tomecek <tomas@tomecek.net>
2 parents 04b3cdc + 1517a87 commit d0da008

4 files changed

Lines changed: 52 additions & 8 deletions

File tree

specfile/changelog.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,33 @@ def extended_timestamp(self) -> bool:
167167
return m is not None
168168

169169
@property
170-
def day_of_month_padding(self) -> str:
171-
"""Padding of day of month in the entry header timestamp"""
170+
def sanitized_day_of_month_padding(self) -> str:
171+
"""
172+
Padding of day of month in the entry header timestamp. Nonsensical padding
173+
(i.e. multiple spaces before double-digit day of month) is corrected.
174+
"""
172175
weekdays = "|".join(WEEKDAYS)
173176
months = "|".join(MONTHS)
177+
174178
m = re.search(
175179
rf"""
176180
({weekdays}) # weekday
177181
[ ]
178182
({months}) # month
179183
[ ]
180184
(?P<wsp>[ ]*) # optional whitespace padding
181-
((?P<zp>0)?\d|[12]\d|3[01]) # possibly zero-padded day of month
185+
(?P<zp>0)? # optional zero-padding
186+
(?P<day>[12]\d|3[01]|[1-9]) # day of month (1-31)
182187
""",
183188
self.header,
184189
re.VERBOSE,
185190
)
186191
if not m:
187192
return ""
188-
return m.group("wsp") + (m.group("zp") or "")
193+
# remove extra padding for double-digit day numbers
194+
whitespace_padding = "" if len(m.group("day")) == 2 else m.group("wsp")
195+
zero_padding = m.group("zp") or ""
196+
return whitespace_padding + zero_padding
189197

190198
@property
191199
def style(self) -> ChangelogStyle:

specfile/specfile.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,13 @@ def add_changelog_entry(
641641
elif email is not None:
642642
author += f" <{email}>"
643643
if changelog:
644-
# try to preserve padding of day of month
644+
# compute day of month padding from the longest valid entry
645+
# incorrect padding is stripped; invalid entries are ignored
645646
padding = max(
646-
(e.day_of_month_padding for e in reversed(changelog)),
647+
(
648+
e.sanitized_day_of_month_padding
649+
for e in reversed(changelog)
650+
),
647651
key=len,
648652
)
649653
else:

tests/integration/test_specfile.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,33 @@ def test_add_changelog_entry(
226226
assert sections.changelog[: len(result)] == result
227227

228228

229+
def test_add_changelog_entry_ignores_invalid_padding(specfile_factory, spec_minimal):
230+
"""Test that add_changelog_entry ignores entries with invalid day padding (issue #216)."""
231+
spec = specfile_factory(spec_minimal)
232+
233+
# Simulate scipy scenario: malformed entry with space before double-digit day
234+
with spec.sections() as sections:
235+
sections.changelog.data = [
236+
"* Mon Dec 11 2021 Author <email> - 0.5.1-5", # INVALID
237+
"- Initial version",
238+
"",
239+
"* Tue May 04 2020 Another <email> - 0.1-0", # VALID (zero-padded)
240+
"- First version",
241+
]
242+
243+
# Add new entry with single-digit day - should use zero-padding from valid entry
244+
flexmock(specfile.specfile).should_receive("guess_packager").and_return(
245+
"Test User <test@example.com>"
246+
).once()
247+
spec.add_changelog_entry("test entry", timestamp=datetime.date(2024, 1, 5))
248+
249+
with spec.sections() as sections:
250+
new_entry = sections.changelog[0]
251+
# Should use zero-padding style "Jan 05", not space-padding "Jan 5"
252+
assert "Jan 05" in new_entry
253+
assert "Jan 5" not in new_entry
254+
255+
229256
@pytest.mark.parametrize(
230257
"version, release",
231258
[

tests/unit/test_changelog.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,17 @@ def test_entry_has_extended_timestamp(header, extended):
9292
),
9393
(
9494
"* Mon Oct 18 12:34:45 CEST 2021 Nikola Forró <nforro@redhat.com> - 0.2-1",
95-
" ",
95+
"", # Invalid: double-digit day with space padding, returns ""
9696
),
97+
(
98+
"* Mon Dec 11 2006 Author <email> - 1.0",
99+
"",
100+
), # Invalid: space before double-digit
101+
("* Invalid header", ""), # Invalid: unparsable
97102
],
98103
)
99104
def test_entry_day_of_month_padding(header, padding):
100-
assert ChangelogEntry(header, [""]).day_of_month_padding == padding
105+
assert ChangelogEntry(header, [""]).sanitized_day_of_month_padding == padding
101106

102107

103108
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)