public inbox for openembedded-core@lists.openembedded.org
 help / color / mirror / Atom feed
From: frederic.martinsons@gmail.com
To: openembedded-core@lists.openembedded.org
Cc: alex.kiernan@gmail.com
Subject: [PATCH 5/5] patch: support of git patches when the source uri contained subpath param
Date: Sat, 25 Mar 2023 10:00:35 +0100	[thread overview]
Message-ID: <b89768b016e793fb02feeebae2a8d334ef99b7f7.1679734210.git.frederic.martinsons@gmail.com> (raw)
In-Reply-To: <cover.1679734210.git.frederic.martinsons@gmail.com>

From: Frederic Martinsons <frederic.martinsons@gmail.com>

This is for a specific case where:
  - A recipe use a subpath on a git repo (e.g. git://repo.git/projects;subpath=subproject)
  - The recipe contains a patch to apply
  - a devtool modify is used on this recipe

With these condition, the patch cannot be applied at all.
GitApplyTree class is used for handling patch under devtool, but
when subpath is present in SRC_URI, the resulting git tree
is dirty (every files and dir which was not in subpath are suppressed)
and so "git am" refuse to apply patches. That would not be such
an issue since the GitApplyTree have a fallback to PatchTree in case
of error, but during this error management, there is a git reset --hard HEAD
call which suppress the subpath operation and finally prevent
the patch to be applied even with PatchTree.

When devtool is not involved, only PatchTree class is used and the
above problem is irrelevant.

To support git patch during devtool, the presence of subpath and
the dirtyness of the repo is checked. If these both condition are
met , we simply fallback to PatchTree like it was already done
in case of error during git apply.

Signed-off-by: Frederic Martinsons <frederic.martinsons@gmail.com>
---
 meta/lib/oe/patch.py | 57 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 44 insertions(+), 13 deletions(-)

diff --git a/meta/lib/oe/patch.py b/meta/lib/oe/patch.py
index b2dc8d0a90..d047b3b947 100644
--- a/meta/lib/oe/patch.py
+++ b/meta/lib/oe/patch.py
@@ -499,6 +499,36 @@ class GitApplyTree(PatchTree):
         finally:
             shutil.rmtree(tempdir)
 
+    def _need_dirty_check(self):
+        fetch = bb.fetch2.Fetch([], self.d)
+        check_dirtyness = False
+        for url in fetch.urls:
+            url_data = fetch.ud[url]
+            parm = url_data.parm
+            # a git url with subpath param will surely be dirty
+            # since the git tree from which we clone will be emptied
+            # from all files that are not in the subpath
+            if url_data.type == 'git' and parm.get('subpath'):
+                check_dirtyness = True
+        return check_dirtyness
+
+    def _commitpatch(self, patch, patchfilevar):
+        output = ""
+        # Add all files
+        shellcmd = ["git", "add", "-f", "-A", "."]
+        output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
+        # Exclude the patches directory
+        shellcmd = ["git", "reset", "HEAD", self.patchdir]
+        output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
+        # Commit the result
+        (tmpfile, shellcmd) = self.prepareCommit(patch['file'], self.commituser, self.commitemail)
+        try:
+            shellcmd.insert(0, patchfilevar)
+            output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
+        finally:
+            os.remove(tmpfile)
+        return output
+
     def _applypatch(self, patch, force = False, reverse = False, run = True):
         import shutil
 
@@ -534,6 +564,19 @@ class GitApplyTree(PatchTree):
         shutil.copy2(commithook, applyhook)
         try:
             patchfilevar = 'PATCHFILE="%s"' % os.path.basename(patch['file'])
+            if self._need_dirty_check():
+                # Check dirtyness of the tree
+                try:
+                    output = runcmd(["git", "--work-tree=%s" % reporoot, "status", "--short"])
+                except CmdError:
+                    pass
+                else:
+                    if output:
+                        # The tree is dirty, not need to try to apply patches with git anymore
+                        # since they fail, fallback directly to patch
+                        output = PatchTree._applypatch(self, patch, force, reverse, run)
+                        output += self._commitpatch(patch, patchfilevar)
+                        return output
             try:
                 shellcmd = [patchfilevar, "git", "--work-tree=%s" % reporoot]
                 self.gitCommandUserOptions(shellcmd, self.commituser, self.commitemail)
@@ -560,19 +603,7 @@ class GitApplyTree(PatchTree):
                 except CmdError:
                     # Fall back to patch
                     output = PatchTree._applypatch(self, patch, force, reverse, run)
-                # Add all files
-                shellcmd = ["git", "add", "-f", "-A", "."]
-                output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
-                # Exclude the patches directory
-                shellcmd = ["git", "reset", "HEAD", self.patchdir]
-                output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
-                # Commit the result
-                (tmpfile, shellcmd) = self.prepareCommit(patch['file'], self.commituser, self.commitemail)
-                try:
-                    shellcmd.insert(0, patchfilevar)
-                    output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
-                finally:
-                    os.remove(tmpfile)
+                output += self._commitpatch(patch, patchfilevar)
                 return output
         finally:
             shutil.rmtree(hooks_dir)
-- 
2.34.1



  parent reply	other threads:[~2023-03-25  9:01 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-25  9:00 [PATCH 0/5] Extend cargo based recip support frederic.martinsons
2023-03-25  9:00 ` [PATCH 1/5] cargo_common.bbclass: Support local github repos frederic.martinsons
2023-03-25  9:00 ` [PATCH 2/5] cargo_common.bbclass: add support of user in url for patch frederic.martinsons
2023-03-25  9:00 ` [PATCH 3/5] rust-example: provide a recipe for zvariant frederic.martinsons
2023-03-25 18:51   ` [OE-core] " Alexander Kanavin
2023-03-25 20:43     ` Frédéric Martinsons
2023-03-26 16:55       ` Alexander Kanavin
2023-03-27  4:09         ` Frédéric Martinsons
2023-03-27  6:40           ` Alexander Kanavin
2023-03-27  8:18             ` Alex Kiernan
2023-03-27  8:30               ` Frédéric Martinsons
2023-03-27  8:42                 ` Alex Kiernan
2023-03-27  8:52                   ` Frédéric Martinsons
2023-03-27 10:24                     ` Alex Kiernan
2023-03-25  9:00 ` [PATCH 4/5] devtool: add support for multiple git url inside a cargo based recipe frederic.martinsons
2023-03-25 18:55   ` [OE-core] " Alexander Kanavin
2023-03-26  5:15     ` Frédéric Martinsons
2023-03-25 19:02   ` Alexander Kanavin
2023-03-26  5:15     ` Frédéric Martinsons
2023-03-26 14:58       ` Frédéric Martinsons
2023-03-26 18:01         ` Alexander Kanavin
2023-03-27  4:09           ` Frédéric Martinsons
2023-03-25  9:00 ` frederic.martinsons [this message]
2023-03-27 14:20 ` [OE-core] [PATCH 0/5] Extend cargo based recip support Richard Purdie
2023-03-27 14:25   ` Alexander Kanavin
2023-03-27 15:01     ` Frédéric Martinsons
2023-03-27 17:19       ` Alexander Kanavin
2023-03-27 18:07         ` Frédéric Martinsons
2023-03-27 18:30           ` Alex Kiernan
2023-03-27 18:40             ` Frédéric Martinsons
2023-03-27 18:52               ` Alex Kiernan
2023-03-27 19:00                 ` Alexander Kanavin
2023-03-27 19:20                   ` Frédéric Martinsons
2023-03-27 22:13                     ` Alexander Kanavin
2023-03-28  7:11                       ` Alex Kiernan
2023-03-28  8:37                         ` Alexander Kanavin
2023-03-28  9:01                           ` Alex Kiernan
2023-03-29 12:35                       ` Alex Kiernan

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=b89768b016e793fb02feeebae2a8d334ef99b7f7.1679734210.git.frederic.martinsons@gmail.com \
    --to=frederic.martinsons@gmail.com \
    --cc=alex.kiernan@gmail.com \
    --cc=openembedded-core@lists.openembedded.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