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