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