patch: support of git patches when the source uri contained subpath parameter

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 conditions, 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 directories which was not in subpath are suppressed)
and so "git am" refuse to apply patches.

That would not be 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 prevents 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 patching during devtool, the presence of subpath and
the dirtyness of the repo are checked. If both conditions are
met, we directly call PatchTree like it was already done
in case of error during git apply.

(From OE-Core rev: d86cac2759cf7e91f4ff240833385e28e729ab79)

Signed-off-by: Frederic Martinsons <frederic.martinsons@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Frederic Martinsons
2023-03-31 07:45:25 +02:00
committed by Richard Purdie
parent da1bcf0808
commit 11180fd528

View File

@@ -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)