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

Mark-PK Tsai <[email protected]>
Newsgroups org.openembedded.lists.bitbake-devel
Message-ID <[email protected]>
From: Mark-Pk Tsai <[email protected]>

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 <[email protected]>
---
 .../bitbake-user-manual-fetching.rst              | 15 ---------------
 lib/bb/fetch2/git.py                              |  6 ++++++
 2 files changed, 6 insertions(+), 15 deletions(-)

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, \
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.