From: "Clément Le Goffic" <clegoffic@baylibre.com>
To: "Kernel.org Tools" <tools@kernel.org>
Cc: "Konstantin Ryabitsev" <konstantin@linuxfoundation.org>,
"Clément Le Goffic" <clegoffic@baylibre.com>
Subject: [PATCH b4 1/3] lore: parse per-file modes in get_indexes
Date: Fri, 25 Sep 2026 14:50:26 +0200 [thread overview]
Message-ID: <20260925-b4-lore-fixes-v1-1-103fd9507e67@baylibre.com> (raw)
In-Reply-To: <20260925-b4-lore-fixes-v1-0-103fd9507e67@baylibre.com>
A mode is per-file state, but get_indexes() never reset it between
files. A file's own mode is taken from its index line only when none
is held yet, so once one file had set a mode, every later file that did
not announce a new one kept it instead of reading its own -- a gitlink
or symlink after a regular file came out as 100644.
Reset it on each 'diff --git', and while there parse 'deleted file mode'
and 'old mode', which were never matched at all, and fix the five-digit
'10644' fallback to 100644.
Assisted-by: claude-fable-5
Signed-off-by: Clément Le Goffic <clegoffic@baylibre.com>
---
src/b4/__init__.py | 21 +++++++++++--
src/tests/test___init__.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 91 insertions(+), 3 deletions(-)
diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index 584755a556b4..3e9145f4a6a5 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -2906,14 +2906,29 @@ class LoreMessage:
newfile = None
fmod = None
for line in diff.split('\n'):
- if not (line.startswith(('diff ', 'index ', 'new file mode '))):
+ if not (
+ line.startswith(
+ (
+ 'diff ',
+ 'index ',
+ 'new file mode ',
+ 'deleted file mode ',
+ 'old mode ',
+ )
+ )
+ ):
continue
matches = re.search(r'^diff\s+--git\s+\w/(.*)\s+\w/(.*)$', line)
if matches:
oldfile = matches.groups()[0]
newfile = matches.groups()[1]
+ # mode is per-file state; a value left over from the
+ # previous file must not leak onto this one
+ fmod = None
continue
- matches = re.search(r'^new file mode (\d+)', line)
+ matches = re.search(
+ r'^(?:new file mode|deleted file mode|old mode) (\d+)$', line
+ )
if matches:
fmod = matches.groups()[0]
matches = re.search(r'^index\s+([\da-f]+)\.\.[\da-f]+.*$', line)
@@ -2925,7 +2940,7 @@ class LoreMessage:
fmod = matches.groups()[0]
if not fmod:
# fall back if we can't figure it out
- fmod = '10644'
+ fmod = '100644'
indexes.add((oldfile, ohash, newfile, fmod))
return indexes
diff --git a/src/tests/test___init__.py b/src/tests/test___init__.py
index 11504bba02c8..980082e26a49 100644
--- a/src/tests/test___init__.py
+++ b/src/tests/test___init__.py
@@ -1808,3 +1808,76 @@ class TestUnicodeControlChars:
def test_plain_ascii_body_is_not_flagged(self) -> None:
lmsg = self._get_lmsg('This adds a fancy widget.')
lmsg.get_am_message(add_trailers=False)
+
+
+class TestGetIndexesPerFileModes:
+ """A mode belongs to the file whose diff header carried it.
+
+ make_fake_am_range() binds every preimage by (hash, mode), so a mode
+ read off the file before it binds a gitlink or a symlink as a regular
+ blob, and the series stops fake-am'ing at all.
+ """
+
+ def test_a_mode_does_not_leak_onto_the_next_file(self) -> None:
+ """A gitlink and a symlink after a regular file keep their own.
+
+ The deleted file states its mode nowhere but on its own
+ "deleted file mode" line, which the index line does not repeat.
+ """
+ diff = (
+ 'diff --git a/README b/README\n'
+ 'index 1111111..2222222 100644\n'
+ '--- a/README\n'
+ '+++ b/README\n'
+ '@@ -1 +1 @@\n'
+ '-readme\n'
+ '+readme v2\n'
+ 'diff --git a/sub b/sub\n'
+ 'index 3333333..4444444 160000\n'
+ '--- a/sub\n'
+ '+++ b/sub\n'
+ '@@ -1 +1 @@\n'
+ '-Subproject commit 3333333333333333333333333333333333333333\n'
+ '+Subproject commit 4444444444444444444444444444444444444444\n'
+ 'diff --git a/link b/link\n'
+ 'index 5555555..6666666 120000\n'
+ '--- a/link\n'
+ '+++ b/link\n'
+ '@@ -1 +1 @@\n'
+ '-README\n'
+ '+sub\n'
+ 'diff --git a/gone b/gone\n'
+ 'deleted file mode 100755\n'
+ 'index 7777777..0000000\n'
+ '--- a/gone\n'
+ '+++ /dev/null\n'
+ '@@ -1 +0,0 @@\n'
+ '-#!/bin/sh\n'
+ )
+ assert b4.LoreMessage.get_indexes(diff) == {
+ ('README', '1111111', 'README', '100644'),
+ ('sub', '3333333', 'sub', '160000'),
+ ('link', '5555555', 'link', '120000'),
+ ('gone', '7777777', 'gone', '100755'),
+ }
+
+ def test_a_mode_change_names_the_preimage_mode(self) -> None:
+ """A chmod alongside an edit: the index line carries no mode.
+
+ get_indexes describes the preimage, so "old mode" is the answer --
+ and nothing anywhere is mode 10644.
+ """
+ diff = (
+ 'diff --git a/run.sh b/run.sh\n'
+ 'old mode 100644\n'
+ 'new mode 100755\n'
+ 'index 8888888..9999999\n'
+ '--- a/run.sh\n'
+ '+++ b/run.sh\n'
+ '@@ -1 +1 @@\n'
+ '-echo old\n'
+ '+echo new\n'
+ )
+ assert b4.LoreMessage.get_indexes(diff) == {
+ ('run.sh', '8888888', 'run.sh', '100644')
+ }
--
2.55.0
next prev parent reply other threads:[~2026-09-25 12:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 12:50 [PATCH b4 0/3] lore: three fixes between a posted serie and the reader Clément Le Goffic
2026-09-25 12:50 ` Clément Le Goffic [this message]
2026-09-25 12:50 ` [PATCH b4 2/3] lore: fake-am series that touch submodule gitlinks Clément Le Goffic
2026-09-25 12:50 ` [PATCH b4 3/3] lore: stop a less pager quitting on output that fits one screen Clément Le Goffic
2026-10-05 10:09 ` Konstantin Ryabitsev
2026-10-05 12:37 ` Clément Le Goffic
2026-10-06 7:15 ` Konstantin Ryabitsev
2026-10-06 8:27 ` Clément Le Goffic
2026-10-05 10:09 ` [PATCH b4 0/3] lore: three fixes between a posted serie and the reader Konstantin Ryabitsev
2026-10-05 10:11 ` (subset) " Konstantin Ryabitsev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260925-b4-lore-fixes-v1-1-103fd9507e67@baylibre.com \
--to=clegoffic@baylibre.com \
--cc=konstantin@linuxfoundation.org \
--cc=tools@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox