blob: f43cf047c069cf386a8c038e0022f99f4c05a90d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001import oe.path
Brad Bishopd7bf8c12018-02-25 22:55:05 -05002import oe.types
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003
4class NotFoundError(bb.BBHandledException):
5 def __init__(self, path):
6 self.path = path
7
8 def __str__(self):
9 return "Error: %s not found." % self.path
10
11class CmdError(bb.BBHandledException):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050012 def __init__(self, command, exitstatus, output):
13 self.command = command
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014 self.status = exitstatus
15 self.output = output
16
17 def __str__(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050018 return "Command Error: '%s' exited with %d Output:\n%s" % \
19 (self.command, self.status, self.output)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020
21
22def runcmd(args, dir = None):
23 import pipes
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080024 import subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
26 if dir:
27 olddir = os.path.abspath(os.curdir)
28 if not os.path.exists(dir):
29 raise NotFoundError(dir)
30 os.chdir(dir)
31 # print("cwd: %s -> %s" % (olddir, dir))
32
33 try:
34 args = [ pipes.quote(str(arg)) for arg in args ]
35 cmd = " ".join(args)
36 # print("cmd: %s" % cmd)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080037 (exitstatus, output) = subprocess.getstatusoutput(cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 if exitstatus != 0:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039 raise CmdError(cmd, exitstatus >> 8, output)
Brad Bishop316dfdd2018-06-25 12:45:53 -040040 if " fuzz " in output:
Brad Bishopd89cb5f2019-04-10 09:02:41 -040041 # Drop patch fuzz info with header and footer to log file so
42 # insane.bbclass can handle to throw error/warning
43 bb.note("--- Patch fuzz start ---\n%s\n--- Patch fuzz end ---" % format(output))
Brad Bishop316dfdd2018-06-25 12:45:53 -040044
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 return output
46
47 finally:
48 if dir:
49 os.chdir(olddir)
50
51class PatchError(Exception):
52 def __init__(self, msg):
53 self.msg = msg
54
55 def __str__(self):
56 return "Patch Error: %s" % self.msg
57
58class PatchSet(object):
59 defaults = {
60 "strippath": 1
61 }
62
63 def __init__(self, dir, d):
64 self.dir = dir
65 self.d = d
66 self.patches = []
67 self._current = None
68
69 def current(self):
70 return self._current
71
72 def Clean(self):
73 """
74 Clean out the patch set. Generally includes unapplying all
75 patches and wiping out all associated metadata.
76 """
77 raise NotImplementedError()
78
79 def Import(self, patch, force):
80 if not patch.get("file"):
81 if not patch.get("remote"):
82 raise PatchError("Patch file must be specified in patch import.")
83 else:
84 patch["file"] = bb.fetch2.localpath(patch["remote"], self.d)
85
86 for param in PatchSet.defaults:
87 if not patch.get(param):
88 patch[param] = PatchSet.defaults[param]
89
90 if patch.get("remote"):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 patch["file"] = self.d.expand(bb.fetch2.localpath(patch["remote"], self.d))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
93 patch["filemd5"] = bb.utils.md5_file(patch["file"])
94
95 def Push(self, force):
96 raise NotImplementedError()
97
98 def Pop(self, force):
99 raise NotImplementedError()
100
101 def Refresh(self, remote = None, all = None):
102 raise NotImplementedError()
103
104 @staticmethod
105 def getPatchedFiles(patchfile, striplevel, srcdir=None):
106 """
107 Read a patch file and determine which files it will modify.
108 Params:
109 patchfile: the patch file to read
110 striplevel: the strip level at which the patch is going to be applied
111 srcdir: optional path to join onto the patched file paths
112 Returns:
113 A list of tuples of file path and change mode ('A' for add,
114 'D' for delete or 'M' for modify)
115 """
116
117 def patchedpath(patchline):
118 filepth = patchline.split()[1]
119 if filepth.endswith('/dev/null'):
120 return '/dev/null'
121 filesplit = filepth.split(os.sep)
122 if striplevel > len(filesplit):
123 bb.error('Patch %s has invalid strip level %d' % (patchfile, striplevel))
124 return None
125 return os.sep.join(filesplit[striplevel:])
126
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600127 for encoding in ['utf-8', 'latin-1']:
128 try:
129 copiedmode = False
130 filelist = []
131 with open(patchfile) as f:
132 for line in f:
133 if line.startswith('--- '):
134 patchpth = patchedpath(line)
135 if not patchpth:
136 break
137 if copiedmode:
138 addedfile = patchpth
139 else:
140 removedfile = patchpth
141 elif line.startswith('+++ '):
142 addedfile = patchedpath(line)
143 if not addedfile:
144 break
145 elif line.startswith('*** '):
146 copiedmode = True
147 removedfile = patchedpath(line)
148 if not removedfile:
149 break
150 else:
151 removedfile = None
152 addedfile = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600154 if addedfile and removedfile:
155 if removedfile == '/dev/null':
156 mode = 'A'
157 elif addedfile == '/dev/null':
158 mode = 'D'
159 else:
160 mode = 'M'
161 if srcdir:
162 fullpath = os.path.abspath(os.path.join(srcdir, addedfile))
163 else:
164 fullpath = addedfile
165 filelist.append((fullpath, mode))
166 except UnicodeDecodeError:
167 continue
168 break
169 else:
170 raise PatchError('Unable to decode %s' % patchfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171
172 return filelist
173
174
175class PatchTree(PatchSet):
176 def __init__(self, dir, d):
177 PatchSet.__init__(self, dir, d)
178 self.patchdir = os.path.join(self.dir, 'patches')
179 self.seriespath = os.path.join(self.dir, 'patches', 'series')
180 bb.utils.mkdirhier(self.patchdir)
181
182 def _appendPatchFile(self, patch, strippath):
183 with open(self.seriespath, 'a') as f:
184 f.write(os.path.basename(patch) + "," + strippath + "\n")
185 shellcmd = ["cat", patch, ">" , self.patchdir + "/" + os.path.basename(patch)]
186 runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
187
188 def _removePatch(self, p):
189 patch = {}
190 patch['file'] = p.split(",")[0]
191 patch['strippath'] = p.split(",")[1]
192 self._applypatch(patch, False, True)
193
194 def _removePatchFile(self, all = False):
195 if not os.path.exists(self.seriespath):
196 return
197 with open(self.seriespath, 'r+') as f:
198 patches = f.readlines()
199 if all:
200 for p in reversed(patches):
201 self._removePatch(os.path.join(self.patchdir, p.strip()))
202 patches = []
203 else:
204 self._removePatch(os.path.join(self.patchdir, patches[-1].strip()))
205 patches.pop()
206 with open(self.seriespath, 'w') as f:
207 for p in patches:
208 f.write(p)
209
210 def Import(self, patch, force = None):
211 """"""
212 PatchSet.Import(self, patch, force)
213
214 if self._current is not None:
215 i = self._current + 1
216 else:
217 i = 0
218 self.patches.insert(i, patch)
219
220 def _applypatch(self, patch, force = False, reverse = False, run = True):
Brad Bishop316dfdd2018-06-25 12:45:53 -0400221 shellcmd = ["cat", patch['file'], "|", "patch", "--no-backup-if-mismatch", "-p", patch['strippath']]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 if reverse:
223 shellcmd.append('-R')
224
225 if not run:
226 return "sh" + "-c" + " ".join(shellcmd)
227
228 if not force:
229 shellcmd.append('--dry-run')
230
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500231 try:
232 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500234 if force:
235 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237 shellcmd.pop(len(shellcmd) - 1)
238 output = runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
239 except CmdError as err:
240 raise bb.BBHandledException("Applying '%s' failed:\n%s" %
241 (os.path.basename(patch['file']), err.output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500242
243 if not reverse:
244 self._appendPatchFile(patch['file'], patch['strippath'])
245
246 return output
247
248 def Push(self, force = False, all = False, run = True):
249 bb.note("self._current is %s" % self._current)
250 bb.note("patches is %s" % self.patches)
251 if all:
252 for i in self.patches:
253 bb.note("applying patch %s" % i)
254 self._applypatch(i, force)
255 self._current = i
256 else:
257 if self._current is not None:
258 next = self._current + 1
259 else:
260 next = 0
261
262 bb.note("applying patch %s" % self.patches[next])
263 ret = self._applypatch(self.patches[next], force)
264
265 self._current = next
266 return ret
267
268 def Pop(self, force = None, all = None):
269 if all:
270 self._removePatchFile(True)
271 self._current = None
272 else:
273 self._removePatchFile(False)
274
275 if self._current == 0:
276 self._current = None
277
278 if self._current is not None:
279 self._current = self._current - 1
280
281 def Clean(self):
282 """"""
283 self.Pop(all=True)
284
285class GitApplyTree(PatchTree):
286 patch_line_prefix = '%% original patch'
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500287 ignore_commit_prefix = '%% ignore'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288
289 def __init__(self, dir, d):
290 PatchTree.__init__(self, dir, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500291 self.commituser = d.getVar('PATCH_GIT_USER_NAME')
292 self.commitemail = d.getVar('PATCH_GIT_USER_EMAIL')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
294 @staticmethod
295 def extractPatchHeader(patchfile):
296 """
297 Extract just the header lines from the top of a patch file
298 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600299 for encoding in ['utf-8', 'latin-1']:
300 lines = []
301 try:
302 with open(patchfile, 'r', encoding=encoding) as f:
303 for line in f:
304 if line.startswith('Index: ') or line.startswith('diff -') or line.startswith('---'):
305 break
306 lines.append(line)
307 except UnicodeDecodeError:
308 continue
309 break
310 else:
311 raise PatchError('Unable to find a character encoding to decode %s' % patchfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500312 return lines
313
314 @staticmethod
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500315 def decodeAuthor(line):
316 from email.header import decode_header
317 authorval = line.split(':', 1)[1].strip().replace('"', '')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600318 result = decode_header(authorval)[0][0]
319 if hasattr(result, 'decode'):
320 result = result.decode('utf-8')
321 return result
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500322
323 @staticmethod
324 def interpretPatchHeader(headerlines):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500325 import re
Brad Bishop19323692019-04-05 15:28:33 -0400326 author_re = re.compile(r'[\S ]+ <\S+@\S+\.\S+>')
327 from_commit_re = re.compile(r'^From [a-z0-9]{40} .*')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500328 outlines = []
329 author = None
330 date = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500331 subject = None
332 for line in headerlines:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333 if line.startswith('Subject: '):
334 subject = line.split(':', 1)[1]
335 # Remove any [PATCH][oe-core] etc.
336 subject = re.sub(r'\[.+?\]\s*', '', subject)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500337 continue
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500338 elif line.startswith('From: ') or line.startswith('Author: '):
339 authorval = GitApplyTree.decodeAuthor(line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500340 # git is fussy about author formatting i.e. it must be Name <email@domain>
341 if author_re.match(authorval):
342 author = authorval
343 continue
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500344 elif line.startswith('Date: '):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500345 if date is None:
346 dateval = line.split(':', 1)[1].strip()
347 # Very crude check for date format, since git will blow up if it's not in the right
348 # format. Without e.g. a python-dateutils dependency we can't do a whole lot more
349 if len(dateval) > 12:
350 date = dateval
351 continue
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500352 elif not author and line.lower().startswith('signed-off-by: '):
353 authorval = GitApplyTree.decodeAuthor(line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354 # git is fussy about author formatting i.e. it must be Name <email@domain>
355 if author_re.match(authorval):
356 author = authorval
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600357 elif from_commit_re.match(line):
358 # We don't want the From <commit> line - if it's present it will break rebasing
359 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500360 outlines.append(line)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600361
362 if not subject:
363 firstline = None
364 for line in headerlines:
365 line = line.strip()
366 if firstline:
367 if line:
368 # Second line is not blank, the first line probably isn't usable
369 firstline = None
370 break
371 elif line:
372 firstline = line
373 if firstline and not firstline.startswith(('#', 'Index:', 'Upstream-Status:')) and len(firstline) < 100:
374 subject = firstline
375
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500376 return outlines, author, date, subject
377
378 @staticmethod
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600379 def gitCommandUserOptions(cmd, commituser=None, commitemail=None, d=None):
380 if d:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500381 commituser = d.getVar('PATCH_GIT_USER_NAME')
382 commitemail = d.getVar('PATCH_GIT_USER_EMAIL')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600383 if commituser:
384 cmd += ['-c', 'user.name="%s"' % commituser]
385 if commitemail:
386 cmd += ['-c', 'user.email="%s"' % commitemail]
387
388 @staticmethod
389 def prepareCommit(patchfile, commituser=None, commitemail=None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500390 """
391 Prepare a git commit command line based on the header from a patch file
392 (typically this is useful for patches that cannot be applied with "git am" due to formatting)
393 """
394 import tempfile
395 # Process patch header and extract useful information
396 lines = GitApplyTree.extractPatchHeader(patchfile)
397 outlines, author, date, subject = GitApplyTree.interpretPatchHeader(lines)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600398 if not author or not subject or not date:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500399 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600400 shellcmd = ["git", "log", "--format=email", "--follow", "--diff-filter=A", "--", patchfile]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500401 out = runcmd(["sh", "-c", " ".join(shellcmd)], os.path.dirname(patchfile))
402 except CmdError:
403 out = None
404 if out:
405 _, newauthor, newdate, newsubject = GitApplyTree.interpretPatchHeader(out.splitlines())
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600406 if not author:
407 # If we're setting the author then the date should be set as well
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500408 author = newauthor
409 date = newdate
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600410 elif not date:
411 # If we don't do this we'll get the current date, at least this will be closer
412 date = newdate
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500413 if not subject:
414 subject = newsubject
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600415 if subject and outlines and not outlines[0].strip() == subject:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500416 outlines.insert(0, '%s\n\n' % subject.strip())
417
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500418 # Write out commit message to a file
419 with tempfile.NamedTemporaryFile('w', delete=False) as tf:
420 tmpfile = tf.name
421 for line in outlines:
422 tf.write(line)
423 # Prepare git command
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600424 cmd = ["git"]
425 GitApplyTree.gitCommandUserOptions(cmd, commituser, commitemail)
426 cmd += ["commit", "-F", tmpfile]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500427 # git doesn't like plain email addresses as authors
428 if author and '<' in author:
429 cmd.append('--author="%s"' % author)
430 if date:
431 cmd.append('--date="%s"' % date)
432 return (tmpfile, cmd)
433
434 @staticmethod
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500435 def extractPatches(tree, startcommit, outdir, paths=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500436 import tempfile
437 import shutil
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500438 import re
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500439 tempdir = tempfile.mkdtemp(prefix='oepatch')
440 try:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400441 shellcmd = ["git", "format-patch", "--no-signature", "--no-numbered", startcommit, "-o", tempdir]
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500442 if paths:
443 shellcmd.append('--')
444 shellcmd.extend(paths)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500445 out = runcmd(["sh", "-c", " ".join(shellcmd)], tree)
446 if out:
447 for srcfile in out.split():
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600448 for encoding in ['utf-8', 'latin-1']:
449 patchlines = []
450 outfile = None
451 try:
452 with open(srcfile, 'r', encoding=encoding) as f:
453 for line in f:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500454 checkline = line
455 if checkline.startswith('Subject: '):
456 checkline = re.sub(r'\[.+?\]\s*', '', checkline[9:])
457 if checkline.startswith(GitApplyTree.patch_line_prefix):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600458 outfile = line.split()[-1].strip()
459 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500460 if checkline.startswith(GitApplyTree.ignore_commit_prefix):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600461 continue
462 patchlines.append(line)
463 except UnicodeDecodeError:
464 continue
465 break
466 else:
467 raise PatchError('Unable to find a character encoding to decode %s' % srcfile)
468
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500469 if not outfile:
470 outfile = os.path.basename(srcfile)
471 with open(os.path.join(outdir, outfile), 'w') as of:
472 for line in patchlines:
473 of.write(line)
474 finally:
475 shutil.rmtree(tempdir)
476
477 def _applypatch(self, patch, force = False, reverse = False, run = True):
478 import shutil
479
480 def _applypatchhelper(shellcmd, patch, force = False, reverse = False, run = True):
481 if reverse:
482 shellcmd.append('-R')
483
484 shellcmd.append(patch['file'])
485
486 if not run:
487 return "sh" + "-c" + " ".join(shellcmd)
488
489 return runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
490
491 # Add hooks which add a pointer to the original patch file name in the commit message
492 reporoot = (runcmd("git rev-parse --show-toplevel".split(), self.dir) or '').strip()
493 if not reporoot:
494 raise Exception("Cannot get repository root for directory %s" % self.dir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500495 hooks_dir = os.path.join(reporoot, '.git', 'hooks')
496 hooks_dir_backup = hooks_dir + '.devtool-orig'
497 if os.path.lexists(hooks_dir_backup):
498 raise Exception("Git hooks backup directory already exists: %s" % hooks_dir_backup)
499 if os.path.lexists(hooks_dir):
500 shutil.move(hooks_dir, hooks_dir_backup)
501 os.mkdir(hooks_dir)
502 commithook = os.path.join(hooks_dir, 'commit-msg')
503 applyhook = os.path.join(hooks_dir, 'applypatch-msg')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500504 with open(commithook, 'w') as f:
505 # NOTE: the formatting here is significant; if you change it you'll also need to
506 # change other places which read it back
507 f.write('echo >> $1\n')
508 f.write('echo "%s: $PATCHFILE" >> $1\n' % GitApplyTree.patch_line_prefix)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600509 os.chmod(commithook, 0o755)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500510 shutil.copy2(commithook, applyhook)
511 try:
512 patchfilevar = 'PATCHFILE="%s"' % os.path.basename(patch['file'])
513 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600514 shellcmd = [patchfilevar, "git", "--work-tree=%s" % reporoot]
515 self.gitCommandUserOptions(shellcmd, self.commituser, self.commitemail)
516 shellcmd += ["am", "-3", "--keep-cr", "-p%s" % patch['strippath']]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500517 return _applypatchhelper(shellcmd, patch, force, reverse, run)
518 except CmdError:
519 # Need to abort the git am, or we'll still be within it at the end
520 try:
521 shellcmd = ["git", "--work-tree=%s" % reporoot, "am", "--abort"]
522 runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
523 except CmdError:
524 pass
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500525 # git am won't always clean up after itself, sadly, so...
526 shellcmd = ["git", "--work-tree=%s" % reporoot, "reset", "--hard", "HEAD"]
527 runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
528 # Also need to take care of any stray untracked files
529 shellcmd = ["git", "--work-tree=%s" % reporoot, "clean", "-f"]
530 runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
531
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500532 # Fall back to git apply
533 shellcmd = ["git", "--git-dir=%s" % reporoot, "apply", "-p%s" % patch['strippath']]
534 try:
535 output = _applypatchhelper(shellcmd, patch, force, reverse, run)
536 except CmdError:
537 # Fall back to patch
538 output = PatchTree._applypatch(self, patch, force, reverse, run)
539 # Add all files
540 shellcmd = ["git", "add", "-f", "-A", "."]
541 output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
542 # Exclude the patches directory
543 shellcmd = ["git", "reset", "HEAD", self.patchdir]
544 output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
545 # Commit the result
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600546 (tmpfile, shellcmd) = self.prepareCommit(patch['file'], self.commituser, self.commitemail)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500547 try:
548 shellcmd.insert(0, patchfilevar)
549 output += runcmd(["sh", "-c", " ".join(shellcmd)], self.dir)
550 finally:
551 os.remove(tmpfile)
552 return output
553 finally:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500554 shutil.rmtree(hooks_dir)
555 if os.path.lexists(hooks_dir_backup):
556 shutil.move(hooks_dir_backup, hooks_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500557
558
559class QuiltTree(PatchSet):
560 def _runcmd(self, args, run = True):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500561 quiltrc = self.d.getVar('QUILTRCFILE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500562 if not run:
563 return ["quilt"] + ["--quiltrc"] + [quiltrc] + args
564 runcmd(["quilt"] + ["--quiltrc"] + [quiltrc] + args, self.dir)
565
566 def _quiltpatchpath(self, file):
567 return os.path.join(self.dir, "patches", os.path.basename(file))
568
569
570 def __init__(self, dir, d):
571 PatchSet.__init__(self, dir, d)
572 self.initialized = False
573 p = os.path.join(self.dir, 'patches')
574 if not os.path.exists(p):
575 os.makedirs(p)
576
577 def Clean(self):
578 try:
579 self._runcmd(["pop", "-a", "-f"])
580 oe.path.remove(os.path.join(self.dir, "patches","series"))
581 except Exception:
582 pass
583 self.initialized = True
584
585 def InitFromDir(self):
586 # read series -> self.patches
587 seriespath = os.path.join(self.dir, 'patches', 'series')
588 if not os.path.exists(self.dir):
589 raise NotFoundError(self.dir)
590 if os.path.exists(seriespath):
591 with open(seriespath, 'r') as f:
592 for line in f.readlines():
593 patch = {}
594 parts = line.strip().split()
595 patch["quiltfile"] = self._quiltpatchpath(parts[0])
596 patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"])
597 if len(parts) > 1:
598 patch["strippath"] = parts[1][2:]
599 self.patches.append(patch)
600
601 # determine which patches are applied -> self._current
602 try:
603 output = runcmd(["quilt", "applied"], self.dir)
604 except CmdError:
605 import sys
606 if sys.exc_value.output.strip() == "No patches applied":
607 return
608 else:
609 raise
610 output = [val for val in output.split('\n') if not val.startswith('#')]
611 for patch in self.patches:
612 if os.path.basename(patch["quiltfile"]) == output[-1]:
613 self._current = self.patches.index(patch)
614 self.initialized = True
615
616 def Import(self, patch, force = None):
617 if not self.initialized:
618 self.InitFromDir()
619 PatchSet.Import(self, patch, force)
620 oe.path.symlink(patch["file"], self._quiltpatchpath(patch["file"]), force=True)
621 with open(os.path.join(self.dir, "patches", "series"), "a") as f:
622 f.write(os.path.basename(patch["file"]) + " -p" + patch["strippath"] + "\n")
623 patch["quiltfile"] = self._quiltpatchpath(patch["file"])
624 patch["quiltfilemd5"] = bb.utils.md5_file(patch["quiltfile"])
625
626 # TODO: determine if the file being imported:
627 # 1) is already imported, and is the same
628 # 2) is already imported, but differs
629
630 self.patches.insert(self._current or 0, patch)
631
632
633 def Push(self, force = False, all = False, run = True):
634 # quilt push [-f]
635
636 args = ["push"]
637 if force:
638 args.append("-f")
639 if all:
640 args.append("-a")
641 if not run:
642 return self._runcmd(args, run)
643
644 self._runcmd(args)
645
646 if self._current is not None:
647 self._current = self._current + 1
648 else:
649 self._current = 0
650
651 def Pop(self, force = None, all = None):
652 # quilt pop [-f]
653 args = ["pop"]
654 if force:
655 args.append("-f")
656 if all:
657 args.append("-a")
658
659 self._runcmd(args)
660
661 if self._current == 0:
662 self._current = None
663
664 if self._current is not None:
665 self._current = self._current - 1
666
667 def Refresh(self, **kwargs):
668 if kwargs.get("remote"):
669 patch = self.patches[kwargs["patch"]]
670 if not patch:
671 raise PatchError("No patch found at index %s in patchset." % kwargs["patch"])
672 (type, host, path, user, pswd, parm) = bb.fetch.decodeurl(patch["remote"])
673 if type == "file":
674 import shutil
675 if not patch.get("file") and patch.get("remote"):
676 patch["file"] = bb.fetch2.localpath(patch["remote"], self.d)
677
678 shutil.copyfile(patch["quiltfile"], patch["file"])
679 else:
680 raise PatchError("Unable to do a remote refresh of %s, unsupported remote url scheme %s." % (os.path.basename(patch["quiltfile"]), type))
681 else:
682 # quilt refresh
683 args = ["refresh"]
684 if kwargs.get("quiltfile"):
685 args.append(os.path.basename(kwargs["quiltfile"]))
686 elif kwargs.get("patch"):
687 args.append(os.path.basename(self.patches[kwargs["patch"]]["quiltfile"]))
688 self._runcmd(args)
689
690class Resolver(object):
691 def __init__(self, patchset, terminal):
692 raise NotImplementedError()
693
694 def Resolve(self):
695 raise NotImplementedError()
696
697 def Revert(self):
698 raise NotImplementedError()
699
700 def Finalize(self):
701 raise NotImplementedError()
702
703class NOOPResolver(Resolver):
704 def __init__(self, patchset, terminal):
705 self.patchset = patchset
706 self.terminal = terminal
707
708 def Resolve(self):
709 olddir = os.path.abspath(os.curdir)
710 os.chdir(self.patchset.dir)
711 try:
712 self.patchset.Push()
713 except Exception:
714 import sys
715 os.chdir(olddir)
716 raise
717
718# Patch resolver which relies on the user doing all the work involved in the
719# resolution, with the exception of refreshing the remote copy of the patch
720# files (the urls).
721class UserResolver(Resolver):
722 def __init__(self, patchset, terminal):
723 self.patchset = patchset
724 self.terminal = terminal
725
726 # Force a push in the patchset, then drop to a shell for the user to
727 # resolve any rejected hunks
728 def Resolve(self):
729 olddir = os.path.abspath(os.curdir)
730 os.chdir(self.patchset.dir)
731 try:
732 self.patchset.Push(False)
733 except CmdError as v:
734 # Patch application failed
735 patchcmd = self.patchset.Push(True, False, False)
736
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500737 t = self.patchset.d.getVar('T')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500738 if not t:
739 bb.msg.fatal("Build", "T not set")
740 bb.utils.mkdirhier(t)
741 import random
742 rcfile = "%s/bashrc.%s.%s" % (t, str(os.getpid()), random.random())
743 with open(rcfile, "w") as f:
744 f.write("echo '*** Manual patch resolution mode ***'\n")
745 f.write("echo 'Dropping to a shell, so patch rejects can be fixed manually.'\n")
746 f.write("echo 'Run \"quilt refresh\" when patch is corrected, press CTRL+D to exit.'\n")
747 f.write("echo ''\n")
748 f.write(" ".join(patchcmd) + "\n")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600749 os.chmod(rcfile, 0o775)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500750
751 self.terminal("bash --rcfile " + rcfile, 'Patch Rejects: Please fix patch rejects manually', self.patchset.d)
752
753 # Construct a new PatchSet after the user's changes, compare the
754 # sets, checking patches for modifications, and doing a remote
755 # refresh on each.
756 oldpatchset = self.patchset
757 self.patchset = oldpatchset.__class__(self.patchset.dir, self.patchset.d)
758
759 for patch in self.patchset.patches:
760 oldpatch = None
761 for opatch in oldpatchset.patches:
762 if opatch["quiltfile"] == patch["quiltfile"]:
763 oldpatch = opatch
764
765 if oldpatch:
766 patch["remote"] = oldpatch["remote"]
767 if patch["quiltfile"] == oldpatch["quiltfile"]:
768 if patch["quiltfilemd5"] != oldpatch["quiltfilemd5"]:
769 bb.note("Patch %s has changed, updating remote url %s" % (os.path.basename(patch["quiltfile"]), patch["remote"]))
770 # user change? remote refresh
771 self.patchset.Refresh(remote=True, patch=self.patchset.patches.index(patch))
772 else:
773 # User did not fix the problem. Abort.
774 raise PatchError("Patch application failed, and user did not fix and refresh the patch.")
775 except Exception:
776 os.chdir(olddir)
777 raise
778 os.chdir(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500779
780
781def patch_path(url, fetch, workdir, expand=True):
Brad Bishop19323692019-04-05 15:28:33 -0400782 """Return the local path of a patch, or return nothing if this isn't a patch"""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500783
784 local = fetch.localpath(url)
Brad Bishop19323692019-04-05 15:28:33 -0400785 if os.path.isdir(local):
786 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500787 base, ext = os.path.splitext(os.path.basename(local))
788 if ext in ('.gz', '.bz2', '.xz', '.Z'):
789 if expand:
790 local = os.path.join(workdir, base)
791 ext = os.path.splitext(base)[1]
792
793 urldata = fetch.ud[url]
794 if "apply" in urldata.parm:
795 apply = oe.types.boolean(urldata.parm["apply"])
796 if not apply:
797 return
798 elif ext not in (".diff", ".patch"):
799 return
800
801 return local
802
803def src_patches(d, all=False, expand=True):
804 workdir = d.getVar('WORKDIR')
805 fetch = bb.fetch2.Fetch([], d)
806 patches = []
807 sources = []
808 for url in fetch.urls:
809 local = patch_path(url, fetch, workdir, expand)
810 if not local:
811 if all:
812 local = fetch.localpath(url)
813 sources.append(local)
814 continue
815
816 urldata = fetch.ud[url]
817 parm = urldata.parm
818 patchname = parm.get('pname') or os.path.basename(local)
819
820 apply, reason = should_apply(parm, d)
821 if not apply:
822 if reason:
823 bb.note("Patch %s %s" % (patchname, reason))
824 continue
825
826 patchparm = {'patchname': patchname}
827 if "striplevel" in parm:
828 striplevel = parm["striplevel"]
829 elif "pnum" in parm:
830 #bb.msg.warn(None, "Deprecated usage of 'pnum' url parameter in '%s', please use 'striplevel'" % url)
831 striplevel = parm["pnum"]
832 else:
833 striplevel = '1'
834 patchparm['striplevel'] = striplevel
835
836 patchdir = parm.get('patchdir')
837 if patchdir:
838 patchparm['patchdir'] = patchdir
839
840 localurl = bb.fetch.encodeurl(('file', '', local, '', '', patchparm))
841 patches.append(localurl)
842
843 if all:
844 return sources
845
846 return patches
847
848
849def should_apply(parm, d):
850 if "mindate" in parm or "maxdate" in parm:
851 pn = d.getVar('PN')
852 srcdate = d.getVar('SRCDATE_%s' % pn)
853 if not srcdate:
854 srcdate = d.getVar('SRCDATE')
855
856 if srcdate == "now":
857 srcdate = d.getVar('DATE')
858
859 if "maxdate" in parm and parm["maxdate"] < srcdate:
860 return False, 'is outdated'
861
862 if "mindate" in parm and parm["mindate"] > srcdate:
863 return False, 'is predated'
864
865
866 if "minrev" in parm:
867 srcrev = d.getVar('SRCREV')
868 if srcrev and srcrev < parm["minrev"]:
869 return False, 'applies to later revisions'
870
871 if "maxrev" in parm:
872 srcrev = d.getVar('SRCREV')
873 if srcrev and srcrev > parm["maxrev"]:
874 return False, 'applies to earlier revisions'
875
876 if "rev" in parm:
877 srcrev = d.getVar('SRCREV')
878 if srcrev and parm["rev"] not in srcrev:
879 return False, "doesn't apply to revision"
880
881 if "notrev" in parm:
882 srcrev = d.getVar('SRCREV')
883 if srcrev and parm["notrev"] in srcrev:
884 return False, "doesn't apply to revision"
885
886 return True, None
887