Linux maintainer tooling and workflows
 help / color / mirror / Atom feed
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 2/3] lore: fake-am series that touch submodule gitlinks
Date: Fri, 25 Sep 2026 14:50:27 +0200	[thread overview]
Message-ID: <20260925-b4-lore-fixes-v1-2-103fd9507e67@baylibre.com> (raw)
In-Reply-To: <20260925-b4-lore-fixes-v1-0-103fd9507e67@baylibre.com>

make_fake_am_range() binds every preimage through a blob lookup, but a
gitlink's old value is a commit in the submodule's object store, which
this repository cannot resolve, so a series bumping a submodule could
never be fake-am'd at all.  "b4 diff" on such a series gives up with
"Could not create fake-am range", and every other caller that needs the
preimage tree loses it the same way.

The diff itself has both halves, so for mode 160000 bind that sha with
"update-index --cacheinfo", which records a gitlink without the object:

  index 1a2b3c..5d6e7f 160000   <- gitlink mode
  -Subproject commit <40 hex>   <- the preimage, in full

Assisted-by: claude-fable-5
Signed-off-by: Clément Le Goffic <clegoffic@baylibre.com>
---
 src/b4/__init__.py         |  38 +++++++++++++++++
 src/tests/test___init__.py | 100 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)

diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index 3e9145f4a6a5..f55e9d9e73aa 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -1540,6 +1540,44 @@ class LoreSeries:
                         # renamed file, make sure to not add the new name later on
                         logger.debug('  Renamed file: %s -> %s', ofn, nfn)
                         seenfiles.add(nfn)
+                    if fmod == '160000':
+                        # A gitlink's preimage lives in the submodule's object
+                        # store, not ours; --cacheinfo binds it without the object.
+                        section = re.search(
+                            r'^diff --git \w/'
+                            + re.escape(ofn)
+                            + r'\s.*?(?=^diff --git |\Z)',
+                            lmsg.body,
+                            flags=re.M | re.S,
+                        )
+                        glink = (
+                            re.search(
+                                r'^-Subproject commit ([0-9a-f]{40,64})$',
+                                section.group(0),
+                                flags=re.M,
+                            )
+                            if section
+                            else None
+                        )
+                        if glink:
+                            bound_hash = glink.group(1)
+                            logger.debug('  Gitlink preimage for: %s', ofn)
+                            gitargs = [
+                                'update-index',
+                                '--add',
+                                '--cacheinfo',
+                                f'{fmod},{bound_hash},{ofn}',
+                            ]
+                            ecode, out = git_run_command(dfn, gitargs)
+                            if ecode > 0:
+                                logger.critical(
+                                    '  ERROR: Could not run update-index for %s (%s)',
+                                    ofn,
+                                    bound_hash,
+                                )
+                                return None, None
+                            continue
+                        logger.debug('  No gitlink preimage for %s', ofn)
                     # Try to grab full ref_id of this hash
                     try:
                         bound_hash = git_revparse_obj(ofi, dfn)
diff --git a/src/tests/test___init__.py b/src/tests/test___init__.py
index 980082e26a49..18de9ab0f114 100644
--- a/src/tests/test___init__.py
+++ b/src/tests/test___init__.py
@@ -1881,3 +1881,103 @@ class TestGetIndexesPerFileModes:
         assert b4.LoreMessage.get_indexes(diff) == {
             ('run.sh', '8888888', 'run.sh', '100644')
         }
+
+
+class TestFakeAmGitlinkPreimage:
+    """A series bumping a submodule has to fake-am in either hash format.
+
+    The preimage of a gitlink is a commit in the submodule's object store,
+    which the superproject cannot resolve, so the only copy of it anywhere
+    on this side is the one written out in the diff -- as 64 hex digits in a
+    sha256 repository.
+    """
+
+    @staticmethod
+    def _bump_repo(
+        tmp_path: pathlib.Path, object_format: str
+    ) -> Tuple[str, str, int, b4.LoreSeries]:
+        """Build a repo whose gitlink bump names objects nobody has.
+
+        Returns (repo, base commit, hash length, series).  master keeps no
+        gitlink at all, so the index lookup make_fake_am_range falls back to
+        has nothing to find either -- exactly like a submodule that was never
+        cloned here.  The bump edits README as well, so the gitlink is not
+        the first file in the diff: a mode is per-file state, and one left
+        over from the file before would bind this one as a regular blob.
+        """
+        repo = str(tmp_path / f'gitlink-{object_format}')
+        ecode, out = b4.git_run_command(
+            None, ['init', f'--object-format={object_format}', '-b', 'master', repo]
+        )
+        assert ecode == 0, out
+        assert b4.git_set_config(repo, 'user.name', 'Gitlink Tester') == 0
+        assert b4.git_set_config(repo, 'user.email', 'gitlink@example.com') == 0
+        (pathlib.Path(repo) / 'README').write_text('readme\n')
+        for args in (['add', 'README'], ['commit', '-q', '-m', 'initial']):
+            assert b4.git_run_command(repo, args, rundir=repo)[0] == 0, args
+
+        hexlen = 64 if object_format == 'sha256' else 40
+        old, new = '1' * hexlen, '2' * hexlen
+        parent = 'master'
+        commits = list()
+        for idx, gitlink in enumerate((old, new)):
+            ecode, _out = b4.git_run_command(
+                repo,
+                ['update-index', '--add', '--cacheinfo', f'160000,{gitlink},sub'],
+                rundir=repo,
+            )
+            assert ecode == 0
+            if idx:
+                (pathlib.Path(repo) / 'README').write_text('readme v2\n')
+                assert b4.git_run_command(repo, ['add', 'README'], rundir=repo)[0] == 0
+            ecode, tree = b4.git_run_command(repo, ['write-tree'], rundir=repo)
+            assert ecode == 0
+            ecode, commit = b4.git_run_command(
+                repo,
+                ['commit-tree', tree.strip(), '-p', parent],
+                stdin=f'point sub at {gitlink[:7]}\n'.encode(),
+            )
+            assert ecode == 0
+            parent = commit.strip()
+            commits.append(parent)
+        base, bump = commits
+
+        ecode, mbox = b4.git_run_command(
+            repo, ['format-patch', '-1', '--stdout', bump], decode=False
+        )
+        assert ecode == 0
+        assert f'-Subproject commit {old}'.encode() in mbox
+        assert mbox.index(b'diff --git a/README') < mbox.index(b'diff --git a/sub')
+        # Back to a master that never heard of the submodule.
+        assert (
+            b4.git_run_command(repo, ['reset', '--hard', 'master'], rundir=repo)[0] == 0
+        )
+
+        lmbx = b4.LoreMailbox()
+        for idx, msg in enumerate(b4.mailsplit_bytes(mbox)):
+            if not msg['Message-Id']:
+                msg['Message-Id'] = f'<{object_format}-gitlink-{idx}@test.local>'
+            lmbx.add_message(msg)
+        lser = lmbx.get_series()
+        assert lser is not None
+        return repo, base, hexlen, lser
+
+    @pytest.mark.parametrize('object_format', ['sha1', 'sha256'])
+    def test_gitlink_preimage_is_bound_without_the_object(
+        self,
+        tmp_path: pathlib.Path,
+        monkeypatch: pytest.MonkeyPatch,
+        object_format: str,
+    ) -> None:
+        """Both ends of the range carry the gitlink the diff named."""
+        repo, base, hexlen, lser = self._bump_repo(tmp_path, object_format)
+        monkeypatch.chdir(repo)
+
+        start, end = lser.make_fake_am_range(gitdir=repo, at_base=base)
+
+        assert start, 'the gitlink preimage did not make it into a fake-am range'
+        assert end
+        for commit, gitlink in ((start, '1' * hexlen), (end, '2' * hexlen)):
+            ecode, entry = b4.git_run_command(repo, ['ls-tree', commit, 'sub'])
+            assert ecode == 0
+            assert entry.split()[:3] == ['160000', 'commit', gitlink]

-- 
2.55.0


  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 ` [PATCH b4 1/3] lore: parse per-file modes in get_indexes Clément Le Goffic
2026-09-25 12:50 ` Clément Le Goffic [this message]
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-2-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