diff mbox series

[v2] fetch2/git: fix HEAD and branch ref when subpath is used

Message ID 20260415075514.2461883-1-mark-pk.tsai@mediatek.com
State New
Headers show
Series [v2] fetch2/git: fix HEAD and branch ref when subpath is used | expand

Commit Message

Mark-PK Tsai April 15, 2026, 7:55 a.m. UTC
From: Mark-Pk Tsai <mark-pk.tsai@mediatek.com>

When subpath is set the checkout code uses read-tree + checkout-index,
but never updates HEAD. It stays on whatever branch the clone defaulted
to instead of pointing to the requested revision.

Fix by pointing HEAD at the correct revision after checkout-index, and
creating the branch ref when branch= is specified. Also remove the
warning note in the documentation that described this limitation.

Signed-off-by: Mark-Pk Tsai <mark-pk.tsai@mediatek.com>
---
 .../bitbake-user-manual-fetching.rst              | 15 ---------------
 lib/bb/fetch2/git.py                              |  6 ++++++
 2 files changed, 6 insertions(+), 15 deletions(-)
diff mbox series

Patch

diff --git a/doc/bitbake-user-manual/bitbake-user-manual-fetching.rst b/doc/bitbake-user-manual/bitbake-user-manual-fetching.rst
index 1f7583ff3..c2747c401 100644
--- a/doc/bitbake-user-manual/bitbake-user-manual-fetching.rst
+++ b/doc/bitbake-user-manual/bitbake-user-manual-fetching.rst
@@ -425,21 +425,6 @@  This fetcher supports the following parameters:
 -  *"subpath":* Limits the checkout to a specific subpath of the tree.
    By default, the whole tree is checked out.
 
-   .. warning::
-
-      When using this option, the value of :term:`SRCREV` may not be reflected
-      in the checked out repository.
-
-      To achieve a partial checkout of the repository with the ``subpath``
-      option, the BitBake Git fetcher makes use of Git "plumbing" commands: `git
-      read-tree <https://git-scm.com/docs/git-read-tree>`__ and `git
-      checkout-index <https://git-scm.com/docs/git-checkout-index>`__. However,
-      these commands only update the **files** within the repository, and do not
-      update the current ``HEAD`` to point to the commit specified by
-      :term:`SRCREV`. Instead, the value of ``HEAD`` will always point to the
-      tip of the branch specified by the ``branch`` parameter, which may or may
-      not correspond to :term:`SRCREV`.
-
 -  *"destsuffix":* The name of the path in which to place the checkout.
    By default, the path is ``git/``.
 
diff --git a/lib/bb/fetch2/git.py b/lib/bb/fetch2/git.py
index 6ed14005b..21f5f28a0 100644
--- a/lib/bb/fetch2/git.py
+++ b/lib/bb/fetch2/git.py
@@ -787,6 +787,12 @@  class Git(FetchMethod):
                 runfetchcmd("%s read-tree %s%s" % (ud.basecmd, ud.revision, readpathspec), d,
                             workdir=destdir)
                 runfetchcmd("%s checkout-index -q -f -a" % ud.basecmd, d, workdir=destdir)
+                runfetchcmd("%s update-ref --no-deref HEAD %s" % (ud.basecmd, ud.revision),
+                            d, workdir=destdir)
+                if not ud.nobranch:
+                    branchname = ud.branch
+                    runfetchcmd("%s update-ref refs/heads/%s %s" % (ud.basecmd, branchname,
+                                ud.revision), d, workdir=destdir)
             elif not ud.nobranch:
                 branchname =  ud.branch
                 runfetchcmd("%s checkout -B %s %s" % (ud.basecmd, branchname, \