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