Show More
@@ -1,1890 +1,1890 b'' | |||
|
1 | 1 | # patch.py - patch file parsing routines |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | import cStringIO, email.Parser, os, errno, re | |
|
9 | import cStringIO, email.Parser, os, errno, re, posixpath | |
|
10 | 10 | import tempfile, zlib, shutil |
|
11 | 11 | |
|
12 | 12 | from i18n import _ |
|
13 | 13 | from node import hex, nullid, short |
|
14 | 14 | import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error |
|
15 | 15 | import context |
|
16 | 16 | |
|
17 | 17 | gitre = re.compile('diff --git a/(.*) b/(.*)') |
|
18 | 18 | |
|
19 | 19 | class PatchError(Exception): |
|
20 | 20 | pass |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | # public functions |
|
24 | 24 | |
|
25 | 25 | def split(stream): |
|
26 | 26 | '''return an iterator of individual patches from a stream''' |
|
27 | 27 | def isheader(line, inheader): |
|
28 | 28 | if inheader and line[0] in (' ', '\t'): |
|
29 | 29 | # continuation |
|
30 | 30 | return True |
|
31 | 31 | if line[0] in (' ', '-', '+'): |
|
32 | 32 | # diff line - don't check for header pattern in there |
|
33 | 33 | return False |
|
34 | 34 | l = line.split(': ', 1) |
|
35 | 35 | return len(l) == 2 and ' ' not in l[0] |
|
36 | 36 | |
|
37 | 37 | def chunk(lines): |
|
38 | 38 | return cStringIO.StringIO(''.join(lines)) |
|
39 | 39 | |
|
40 | 40 | def hgsplit(stream, cur): |
|
41 | 41 | inheader = True |
|
42 | 42 | |
|
43 | 43 | for line in stream: |
|
44 | 44 | if not line.strip(): |
|
45 | 45 | inheader = False |
|
46 | 46 | if not inheader and line.startswith('# HG changeset patch'): |
|
47 | 47 | yield chunk(cur) |
|
48 | 48 | cur = [] |
|
49 | 49 | inheader = True |
|
50 | 50 | |
|
51 | 51 | cur.append(line) |
|
52 | 52 | |
|
53 | 53 | if cur: |
|
54 | 54 | yield chunk(cur) |
|
55 | 55 | |
|
56 | 56 | def mboxsplit(stream, cur): |
|
57 | 57 | for line in stream: |
|
58 | 58 | if line.startswith('From '): |
|
59 | 59 | for c in split(chunk(cur[1:])): |
|
60 | 60 | yield c |
|
61 | 61 | cur = [] |
|
62 | 62 | |
|
63 | 63 | cur.append(line) |
|
64 | 64 | |
|
65 | 65 | if cur: |
|
66 | 66 | for c in split(chunk(cur[1:])): |
|
67 | 67 | yield c |
|
68 | 68 | |
|
69 | 69 | def mimesplit(stream, cur): |
|
70 | 70 | def msgfp(m): |
|
71 | 71 | fp = cStringIO.StringIO() |
|
72 | 72 | g = email.Generator.Generator(fp, mangle_from_=False) |
|
73 | 73 | g.flatten(m) |
|
74 | 74 | fp.seek(0) |
|
75 | 75 | return fp |
|
76 | 76 | |
|
77 | 77 | for line in stream: |
|
78 | 78 | cur.append(line) |
|
79 | 79 | c = chunk(cur) |
|
80 | 80 | |
|
81 | 81 | m = email.Parser.Parser().parse(c) |
|
82 | 82 | if not m.is_multipart(): |
|
83 | 83 | yield msgfp(m) |
|
84 | 84 | else: |
|
85 | 85 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
86 | 86 | for part in m.walk(): |
|
87 | 87 | ct = part.get_content_type() |
|
88 | 88 | if ct not in ok_types: |
|
89 | 89 | continue |
|
90 | 90 | yield msgfp(part) |
|
91 | 91 | |
|
92 | 92 | def headersplit(stream, cur): |
|
93 | 93 | inheader = False |
|
94 | 94 | |
|
95 | 95 | for line in stream: |
|
96 | 96 | if not inheader and isheader(line, inheader): |
|
97 | 97 | yield chunk(cur) |
|
98 | 98 | cur = [] |
|
99 | 99 | inheader = True |
|
100 | 100 | if inheader and not isheader(line, inheader): |
|
101 | 101 | inheader = False |
|
102 | 102 | |
|
103 | 103 | cur.append(line) |
|
104 | 104 | |
|
105 | 105 | if cur: |
|
106 | 106 | yield chunk(cur) |
|
107 | 107 | |
|
108 | 108 | def remainder(cur): |
|
109 | 109 | yield chunk(cur) |
|
110 | 110 | |
|
111 | 111 | class fiter(object): |
|
112 | 112 | def __init__(self, fp): |
|
113 | 113 | self.fp = fp |
|
114 | 114 | |
|
115 | 115 | def __iter__(self): |
|
116 | 116 | return self |
|
117 | 117 | |
|
118 | 118 | def next(self): |
|
119 | 119 | l = self.fp.readline() |
|
120 | 120 | if not l: |
|
121 | 121 | raise StopIteration |
|
122 | 122 | return l |
|
123 | 123 | |
|
124 | 124 | inheader = False |
|
125 | 125 | cur = [] |
|
126 | 126 | |
|
127 | 127 | mimeheaders = ['content-type'] |
|
128 | 128 | |
|
129 | 129 | if not util.safehasattr(stream, 'next'): |
|
130 | 130 | # http responses, for example, have readline but not next |
|
131 | 131 | stream = fiter(stream) |
|
132 | 132 | |
|
133 | 133 | for line in stream: |
|
134 | 134 | cur.append(line) |
|
135 | 135 | if line.startswith('# HG changeset patch'): |
|
136 | 136 | return hgsplit(stream, cur) |
|
137 | 137 | elif line.startswith('From '): |
|
138 | 138 | return mboxsplit(stream, cur) |
|
139 | 139 | elif isheader(line, inheader): |
|
140 | 140 | inheader = True |
|
141 | 141 | if line.split(':', 1)[0].lower() in mimeheaders: |
|
142 | 142 | # let email parser handle this |
|
143 | 143 | return mimesplit(stream, cur) |
|
144 | 144 | elif line.startswith('--- ') and inheader: |
|
145 | 145 | # No evil headers seen by diff start, split by hand |
|
146 | 146 | return headersplit(stream, cur) |
|
147 | 147 | # Not enough info, keep reading |
|
148 | 148 | |
|
149 | 149 | # if we are here, we have a very plain patch |
|
150 | 150 | return remainder(cur) |
|
151 | 151 | |
|
152 | 152 | def extract(ui, fileobj): |
|
153 | 153 | '''extract patch from data read from fileobj. |
|
154 | 154 | |
|
155 | 155 | patch can be a normal patch or contained in an email message. |
|
156 | 156 | |
|
157 | 157 | return tuple (filename, message, user, date, branch, node, p1, p2). |
|
158 | 158 | Any item in the returned tuple can be None. If filename is None, |
|
159 | 159 | fileobj did not contain a patch. Caller must unlink filename when done.''' |
|
160 | 160 | |
|
161 | 161 | # attempt to detect the start of a patch |
|
162 | 162 | # (this heuristic is borrowed from quilt) |
|
163 | 163 | diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' |
|
164 | 164 | r'retrieving revision [0-9]+(\.[0-9]+)*$|' |
|
165 | 165 | r'---[ \t].*?^\+\+\+[ \t]|' |
|
166 | 166 | r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL) |
|
167 | 167 | |
|
168 | 168 | fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') |
|
169 | 169 | tmpfp = os.fdopen(fd, 'w') |
|
170 | 170 | try: |
|
171 | 171 | msg = email.Parser.Parser().parse(fileobj) |
|
172 | 172 | |
|
173 | 173 | subject = msg['Subject'] |
|
174 | 174 | user = msg['From'] |
|
175 | 175 | if not subject and not user: |
|
176 | 176 | # Not an email, restore parsed headers if any |
|
177 | 177 | subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n' |
|
178 | 178 | |
|
179 | 179 | gitsendmail = 'git-send-email' in msg.get('X-Mailer', '') |
|
180 | 180 | # should try to parse msg['Date'] |
|
181 | 181 | date = None |
|
182 | 182 | nodeid = None |
|
183 | 183 | branch = None |
|
184 | 184 | parents = [] |
|
185 | 185 | |
|
186 | 186 | if subject: |
|
187 | 187 | if subject.startswith('[PATCH'): |
|
188 | 188 | pend = subject.find(']') |
|
189 | 189 | if pend >= 0: |
|
190 | 190 | subject = subject[pend + 1:].lstrip() |
|
191 | 191 | subject = re.sub(r'\n[ \t]+', ' ', subject) |
|
192 | 192 | ui.debug('Subject: %s\n' % subject) |
|
193 | 193 | if user: |
|
194 | 194 | ui.debug('From: %s\n' % user) |
|
195 | 195 | diffs_seen = 0 |
|
196 | 196 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
197 | 197 | message = '' |
|
198 | 198 | for part in msg.walk(): |
|
199 | 199 | content_type = part.get_content_type() |
|
200 | 200 | ui.debug('Content-Type: %s\n' % content_type) |
|
201 | 201 | if content_type not in ok_types: |
|
202 | 202 | continue |
|
203 | 203 | payload = part.get_payload(decode=True) |
|
204 | 204 | m = diffre.search(payload) |
|
205 | 205 | if m: |
|
206 | 206 | hgpatch = False |
|
207 | 207 | hgpatchheader = False |
|
208 | 208 | ignoretext = False |
|
209 | 209 | |
|
210 | 210 | ui.debug('found patch at byte %d\n' % m.start(0)) |
|
211 | 211 | diffs_seen += 1 |
|
212 | 212 | cfp = cStringIO.StringIO() |
|
213 | 213 | for line in payload[:m.start(0)].splitlines(): |
|
214 | 214 | if line.startswith('# HG changeset patch') and not hgpatch: |
|
215 | 215 | ui.debug('patch generated by hg export\n') |
|
216 | 216 | hgpatch = True |
|
217 | 217 | hgpatchheader = True |
|
218 | 218 | # drop earlier commit message content |
|
219 | 219 | cfp.seek(0) |
|
220 | 220 | cfp.truncate() |
|
221 | 221 | subject = None |
|
222 | 222 | elif hgpatchheader: |
|
223 | 223 | if line.startswith('# User '): |
|
224 | 224 | user = line[7:] |
|
225 | 225 | ui.debug('From: %s\n' % user) |
|
226 | 226 | elif line.startswith("# Date "): |
|
227 | 227 | date = line[7:] |
|
228 | 228 | elif line.startswith("# Branch "): |
|
229 | 229 | branch = line[9:] |
|
230 | 230 | elif line.startswith("# Node ID "): |
|
231 | 231 | nodeid = line[10:] |
|
232 | 232 | elif line.startswith("# Parent "): |
|
233 | 233 | parents.append(line[9:].lstrip()) |
|
234 | 234 | elif not line.startswith("# "): |
|
235 | 235 | hgpatchheader = False |
|
236 | 236 | elif line == '---' and gitsendmail: |
|
237 | 237 | ignoretext = True |
|
238 | 238 | if not hgpatchheader and not ignoretext: |
|
239 | 239 | cfp.write(line) |
|
240 | 240 | cfp.write('\n') |
|
241 | 241 | message = cfp.getvalue() |
|
242 | 242 | if tmpfp: |
|
243 | 243 | tmpfp.write(payload) |
|
244 | 244 | if not payload.endswith('\n'): |
|
245 | 245 | tmpfp.write('\n') |
|
246 | 246 | elif not diffs_seen and message and content_type == 'text/plain': |
|
247 | 247 | message += '\n' + payload |
|
248 | 248 | except: # re-raises |
|
249 | 249 | tmpfp.close() |
|
250 | 250 | os.unlink(tmpname) |
|
251 | 251 | raise |
|
252 | 252 | |
|
253 | 253 | if subject and not message.startswith(subject): |
|
254 | 254 | message = '%s\n%s' % (subject, message) |
|
255 | 255 | tmpfp.close() |
|
256 | 256 | if not diffs_seen: |
|
257 | 257 | os.unlink(tmpname) |
|
258 | 258 | return None, message, user, date, branch, None, None, None |
|
259 | 259 | p1 = parents and parents.pop(0) or None |
|
260 | 260 | p2 = parents and parents.pop(0) or None |
|
261 | 261 | return tmpname, message, user, date, branch, nodeid, p1, p2 |
|
262 | 262 | |
|
263 | 263 | class patchmeta(object): |
|
264 | 264 | """Patched file metadata |
|
265 | 265 | |
|
266 | 266 | 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY |
|
267 | 267 | or COPY. 'path' is patched file path. 'oldpath' is set to the |
|
268 | 268 | origin file when 'op' is either COPY or RENAME, None otherwise. If |
|
269 | 269 | file mode is changed, 'mode' is a tuple (islink, isexec) where |
|
270 | 270 | 'islink' is True if the file is a symlink and 'isexec' is True if |
|
271 | 271 | the file is executable. Otherwise, 'mode' is None. |
|
272 | 272 | """ |
|
273 | 273 | def __init__(self, path): |
|
274 | 274 | self.path = path |
|
275 | 275 | self.oldpath = None |
|
276 | 276 | self.mode = None |
|
277 | 277 | self.op = 'MODIFY' |
|
278 | 278 | self.binary = False |
|
279 | 279 | |
|
280 | 280 | def setmode(self, mode): |
|
281 | 281 | islink = mode & 020000 |
|
282 | 282 | isexec = mode & 0100 |
|
283 | 283 | self.mode = (islink, isexec) |
|
284 | 284 | |
|
285 | 285 | def copy(self): |
|
286 | 286 | other = patchmeta(self.path) |
|
287 | 287 | other.oldpath = self.oldpath |
|
288 | 288 | other.mode = self.mode |
|
289 | 289 | other.op = self.op |
|
290 | 290 | other.binary = self.binary |
|
291 | 291 | return other |
|
292 | 292 | |
|
293 | 293 | def _ispatchinga(self, afile): |
|
294 | 294 | if afile == '/dev/null': |
|
295 | 295 | return self.op == 'ADD' |
|
296 | 296 | return afile == 'a/' + (self.oldpath or self.path) |
|
297 | 297 | |
|
298 | 298 | def _ispatchingb(self, bfile): |
|
299 | 299 | if bfile == '/dev/null': |
|
300 | 300 | return self.op == 'DELETE' |
|
301 | 301 | return bfile == 'b/' + self.path |
|
302 | 302 | |
|
303 | 303 | def ispatching(self, afile, bfile): |
|
304 | 304 | return self._ispatchinga(afile) and self._ispatchingb(bfile) |
|
305 | 305 | |
|
306 | 306 | def __repr__(self): |
|
307 | 307 | return "<patchmeta %s %r>" % (self.op, self.path) |
|
308 | 308 | |
|
309 | 309 | def readgitpatch(lr): |
|
310 | 310 | """extract git-style metadata about patches from <patchname>""" |
|
311 | 311 | |
|
312 | 312 | # Filter patch for git information |
|
313 | 313 | gp = None |
|
314 | 314 | gitpatches = [] |
|
315 | 315 | for line in lr: |
|
316 | 316 | line = line.rstrip(' \r\n') |
|
317 | 317 | if line.startswith('diff --git'): |
|
318 | 318 | m = gitre.match(line) |
|
319 | 319 | if m: |
|
320 | 320 | if gp: |
|
321 | 321 | gitpatches.append(gp) |
|
322 | 322 | dst = m.group(2) |
|
323 | 323 | gp = patchmeta(dst) |
|
324 | 324 | elif gp: |
|
325 | 325 | if line.startswith('--- '): |
|
326 | 326 | gitpatches.append(gp) |
|
327 | 327 | gp = None |
|
328 | 328 | continue |
|
329 | 329 | if line.startswith('rename from '): |
|
330 | 330 | gp.op = 'RENAME' |
|
331 | 331 | gp.oldpath = line[12:] |
|
332 | 332 | elif line.startswith('rename to '): |
|
333 | 333 | gp.path = line[10:] |
|
334 | 334 | elif line.startswith('copy from '): |
|
335 | 335 | gp.op = 'COPY' |
|
336 | 336 | gp.oldpath = line[10:] |
|
337 | 337 | elif line.startswith('copy to '): |
|
338 | 338 | gp.path = line[8:] |
|
339 | 339 | elif line.startswith('deleted file'): |
|
340 | 340 | gp.op = 'DELETE' |
|
341 | 341 | elif line.startswith('new file mode '): |
|
342 | 342 | gp.op = 'ADD' |
|
343 | 343 | gp.setmode(int(line[-6:], 8)) |
|
344 | 344 | elif line.startswith('new mode '): |
|
345 | 345 | gp.setmode(int(line[-6:], 8)) |
|
346 | 346 | elif line.startswith('GIT binary patch'): |
|
347 | 347 | gp.binary = True |
|
348 | 348 | if gp: |
|
349 | 349 | gitpatches.append(gp) |
|
350 | 350 | |
|
351 | 351 | return gitpatches |
|
352 | 352 | |
|
353 | 353 | class linereader(object): |
|
354 | 354 | # simple class to allow pushing lines back into the input stream |
|
355 | 355 | def __init__(self, fp): |
|
356 | 356 | self.fp = fp |
|
357 | 357 | self.buf = [] |
|
358 | 358 | |
|
359 | 359 | def push(self, line): |
|
360 | 360 | if line is not None: |
|
361 | 361 | self.buf.append(line) |
|
362 | 362 | |
|
363 | 363 | def readline(self): |
|
364 | 364 | if self.buf: |
|
365 | 365 | l = self.buf[0] |
|
366 | 366 | del self.buf[0] |
|
367 | 367 | return l |
|
368 | 368 | return self.fp.readline() |
|
369 | 369 | |
|
370 | 370 | def __iter__(self): |
|
371 | 371 | while True: |
|
372 | 372 | l = self.readline() |
|
373 | 373 | if not l: |
|
374 | 374 | break |
|
375 | 375 | yield l |
|
376 | 376 | |
|
377 | 377 | class abstractbackend(object): |
|
378 | 378 | def __init__(self, ui): |
|
379 | 379 | self.ui = ui |
|
380 | 380 | |
|
381 | 381 | def getfile(self, fname): |
|
382 | 382 | """Return target file data and flags as a (data, (islink, |
|
383 | 383 | isexec)) tuple. |
|
384 | 384 | """ |
|
385 | 385 | raise NotImplementedError |
|
386 | 386 | |
|
387 | 387 | def setfile(self, fname, data, mode, copysource): |
|
388 | 388 | """Write data to target file fname and set its mode. mode is a |
|
389 | 389 | (islink, isexec) tuple. If data is None, the file content should |
|
390 | 390 | be left unchanged. If the file is modified after being copied, |
|
391 | 391 | copysource is set to the original file name. |
|
392 | 392 | """ |
|
393 | 393 | raise NotImplementedError |
|
394 | 394 | |
|
395 | 395 | def unlink(self, fname): |
|
396 | 396 | """Unlink target file.""" |
|
397 | 397 | raise NotImplementedError |
|
398 | 398 | |
|
399 | 399 | def writerej(self, fname, failed, total, lines): |
|
400 | 400 | """Write rejected lines for fname. total is the number of hunks |
|
401 | 401 | which failed to apply and total the total number of hunks for this |
|
402 | 402 | files. |
|
403 | 403 | """ |
|
404 | 404 | pass |
|
405 | 405 | |
|
406 | 406 | def exists(self, fname): |
|
407 | 407 | raise NotImplementedError |
|
408 | 408 | |
|
409 | 409 | class fsbackend(abstractbackend): |
|
410 | 410 | def __init__(self, ui, basedir): |
|
411 | 411 | super(fsbackend, self).__init__(ui) |
|
412 | 412 | self.opener = scmutil.opener(basedir) |
|
413 | 413 | |
|
414 | 414 | def _join(self, f): |
|
415 | 415 | return os.path.join(self.opener.base, f) |
|
416 | 416 | |
|
417 | 417 | def getfile(self, fname): |
|
418 | 418 | path = self._join(fname) |
|
419 | 419 | if os.path.islink(path): |
|
420 | 420 | return (os.readlink(path), (True, False)) |
|
421 | 421 | isexec = False |
|
422 | 422 | try: |
|
423 | 423 | isexec = os.lstat(path).st_mode & 0100 != 0 |
|
424 | 424 | except OSError, e: |
|
425 | 425 | if e.errno != errno.ENOENT: |
|
426 | 426 | raise |
|
427 | 427 | return (self.opener.read(fname), (False, isexec)) |
|
428 | 428 | |
|
429 | 429 | def setfile(self, fname, data, mode, copysource): |
|
430 | 430 | islink, isexec = mode |
|
431 | 431 | if data is None: |
|
432 | 432 | util.setflags(self._join(fname), islink, isexec) |
|
433 | 433 | return |
|
434 | 434 | if islink: |
|
435 | 435 | self.opener.symlink(data, fname) |
|
436 | 436 | else: |
|
437 | 437 | self.opener.write(fname, data) |
|
438 | 438 | if isexec: |
|
439 | 439 | util.setflags(self._join(fname), False, True) |
|
440 | 440 | |
|
441 | 441 | def unlink(self, fname): |
|
442 | 442 | try: |
|
443 | 443 | util.unlinkpath(self._join(fname)) |
|
444 | 444 | except OSError, inst: |
|
445 | 445 | if inst.errno != errno.ENOENT: |
|
446 | 446 | raise |
|
447 | 447 | |
|
448 | 448 | def writerej(self, fname, failed, total, lines): |
|
449 | 449 | fname = fname + ".rej" |
|
450 | 450 | self.ui.warn( |
|
451 | 451 | _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % |
|
452 | 452 | (failed, total, fname)) |
|
453 | 453 | fp = self.opener(fname, 'w') |
|
454 | 454 | fp.writelines(lines) |
|
455 | 455 | fp.close() |
|
456 | 456 | |
|
457 | 457 | def exists(self, fname): |
|
458 | 458 | return os.path.lexists(self._join(fname)) |
|
459 | 459 | |
|
460 | 460 | class workingbackend(fsbackend): |
|
461 | 461 | def __init__(self, ui, repo, similarity): |
|
462 | 462 | super(workingbackend, self).__init__(ui, repo.root) |
|
463 | 463 | self.repo = repo |
|
464 | 464 | self.similarity = similarity |
|
465 | 465 | self.removed = set() |
|
466 | 466 | self.changed = set() |
|
467 | 467 | self.copied = [] |
|
468 | 468 | |
|
469 | 469 | def _checkknown(self, fname): |
|
470 | 470 | if self.repo.dirstate[fname] == '?' and self.exists(fname): |
|
471 | 471 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) |
|
472 | 472 | |
|
473 | 473 | def setfile(self, fname, data, mode, copysource): |
|
474 | 474 | self._checkknown(fname) |
|
475 | 475 | super(workingbackend, self).setfile(fname, data, mode, copysource) |
|
476 | 476 | if copysource is not None: |
|
477 | 477 | self.copied.append((copysource, fname)) |
|
478 | 478 | self.changed.add(fname) |
|
479 | 479 | |
|
480 | 480 | def unlink(self, fname): |
|
481 | 481 | self._checkknown(fname) |
|
482 | 482 | super(workingbackend, self).unlink(fname) |
|
483 | 483 | self.removed.add(fname) |
|
484 | 484 | self.changed.add(fname) |
|
485 | 485 | |
|
486 | 486 | def close(self): |
|
487 | 487 | wctx = self.repo[None] |
|
488 | 488 | addremoved = set(self.changed) |
|
489 | 489 | for src, dst in self.copied: |
|
490 | 490 | scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) |
|
491 | 491 | if self.removed: |
|
492 | 492 | wctx.forget(sorted(self.removed)) |
|
493 | 493 | for f in self.removed: |
|
494 | 494 | if f not in self.repo.dirstate: |
|
495 | 495 | # File was deleted and no longer belongs to the |
|
496 | 496 | # dirstate, it was probably marked added then |
|
497 | 497 | # deleted, and should not be considered by |
|
498 | 498 | # addremove(). |
|
499 | 499 | addremoved.discard(f) |
|
500 | 500 | if addremoved: |
|
501 | 501 | cwd = self.repo.getcwd() |
|
502 | 502 | if cwd: |
|
503 | 503 | addremoved = [util.pathto(self.repo.root, cwd, f) |
|
504 | 504 | for f in addremoved] |
|
505 | 505 | scmutil.addremove(self.repo, addremoved, similarity=self.similarity) |
|
506 | 506 | return sorted(self.changed) |
|
507 | 507 | |
|
508 | 508 | class filestore(object): |
|
509 | 509 | def __init__(self, maxsize=None): |
|
510 | 510 | self.opener = None |
|
511 | 511 | self.files = {} |
|
512 | 512 | self.created = 0 |
|
513 | 513 | self.maxsize = maxsize |
|
514 | 514 | if self.maxsize is None: |
|
515 | 515 | self.maxsize = 4*(2**20) |
|
516 | 516 | self.size = 0 |
|
517 | 517 | self.data = {} |
|
518 | 518 | |
|
519 | 519 | def setfile(self, fname, data, mode, copied=None): |
|
520 | 520 | if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize: |
|
521 | 521 | self.data[fname] = (data, mode, copied) |
|
522 | 522 | self.size += len(data) |
|
523 | 523 | else: |
|
524 | 524 | if self.opener is None: |
|
525 | 525 | root = tempfile.mkdtemp(prefix='hg-patch-') |
|
526 | 526 | self.opener = scmutil.opener(root) |
|
527 | 527 | # Avoid filename issues with these simple names |
|
528 | 528 | fn = str(self.created) |
|
529 | 529 | self.opener.write(fn, data) |
|
530 | 530 | self.created += 1 |
|
531 | 531 | self.files[fname] = (fn, mode, copied) |
|
532 | 532 | |
|
533 | 533 | def getfile(self, fname): |
|
534 | 534 | if fname in self.data: |
|
535 | 535 | return self.data[fname] |
|
536 | 536 | if not self.opener or fname not in self.files: |
|
537 | 537 | raise IOError |
|
538 | 538 | fn, mode, copied = self.files[fname] |
|
539 | 539 | return self.opener.read(fn), mode, copied |
|
540 | 540 | |
|
541 | 541 | def close(self): |
|
542 | 542 | if self.opener: |
|
543 | 543 | shutil.rmtree(self.opener.base) |
|
544 | 544 | |
|
545 | 545 | class repobackend(abstractbackend): |
|
546 | 546 | def __init__(self, ui, repo, ctx, store): |
|
547 | 547 | super(repobackend, self).__init__(ui) |
|
548 | 548 | self.repo = repo |
|
549 | 549 | self.ctx = ctx |
|
550 | 550 | self.store = store |
|
551 | 551 | self.changed = set() |
|
552 | 552 | self.removed = set() |
|
553 | 553 | self.copied = {} |
|
554 | 554 | |
|
555 | 555 | def _checkknown(self, fname): |
|
556 | 556 | if fname not in self.ctx: |
|
557 | 557 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) |
|
558 | 558 | |
|
559 | 559 | def getfile(self, fname): |
|
560 | 560 | try: |
|
561 | 561 | fctx = self.ctx[fname] |
|
562 | 562 | except error.LookupError: |
|
563 | 563 | raise IOError |
|
564 | 564 | flags = fctx.flags() |
|
565 | 565 | return fctx.data(), ('l' in flags, 'x' in flags) |
|
566 | 566 | |
|
567 | 567 | def setfile(self, fname, data, mode, copysource): |
|
568 | 568 | if copysource: |
|
569 | 569 | self._checkknown(copysource) |
|
570 | 570 | if data is None: |
|
571 | 571 | data = self.ctx[fname].data() |
|
572 | 572 | self.store.setfile(fname, data, mode, copysource) |
|
573 | 573 | self.changed.add(fname) |
|
574 | 574 | if copysource: |
|
575 | 575 | self.copied[fname] = copysource |
|
576 | 576 | |
|
577 | 577 | def unlink(self, fname): |
|
578 | 578 | self._checkknown(fname) |
|
579 | 579 | self.removed.add(fname) |
|
580 | 580 | |
|
581 | 581 | def exists(self, fname): |
|
582 | 582 | return fname in self.ctx |
|
583 | 583 | |
|
584 | 584 | def close(self): |
|
585 | 585 | return self.changed | self.removed |
|
586 | 586 | |
|
587 | 587 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
|
588 | 588 | unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') |
|
589 | 589 | contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') |
|
590 | 590 | eolmodes = ['strict', 'crlf', 'lf', 'auto'] |
|
591 | 591 | |
|
592 | 592 | class patchfile(object): |
|
593 | 593 | def __init__(self, ui, gp, backend, store, eolmode='strict'): |
|
594 | 594 | self.fname = gp.path |
|
595 | 595 | self.eolmode = eolmode |
|
596 | 596 | self.eol = None |
|
597 | 597 | self.backend = backend |
|
598 | 598 | self.ui = ui |
|
599 | 599 | self.lines = [] |
|
600 | 600 | self.exists = False |
|
601 | 601 | self.missing = True |
|
602 | 602 | self.mode = gp.mode |
|
603 | 603 | self.copysource = gp.oldpath |
|
604 | 604 | self.create = gp.op in ('ADD', 'COPY', 'RENAME') |
|
605 | 605 | self.remove = gp.op == 'DELETE' |
|
606 | 606 | try: |
|
607 | 607 | if self.copysource is None: |
|
608 | 608 | data, mode = backend.getfile(self.fname) |
|
609 | 609 | self.exists = True |
|
610 | 610 | else: |
|
611 | 611 | data, mode = store.getfile(self.copysource)[:2] |
|
612 | 612 | self.exists = backend.exists(self.fname) |
|
613 | 613 | self.missing = False |
|
614 | 614 | if data: |
|
615 | 615 | self.lines = mdiff.splitnewlines(data) |
|
616 | 616 | if self.mode is None: |
|
617 | 617 | self.mode = mode |
|
618 | 618 | if self.lines: |
|
619 | 619 | # Normalize line endings |
|
620 | 620 | if self.lines[0].endswith('\r\n'): |
|
621 | 621 | self.eol = '\r\n' |
|
622 | 622 | elif self.lines[0].endswith('\n'): |
|
623 | 623 | self.eol = '\n' |
|
624 | 624 | if eolmode != 'strict': |
|
625 | 625 | nlines = [] |
|
626 | 626 | for l in self.lines: |
|
627 | 627 | if l.endswith('\r\n'): |
|
628 | 628 | l = l[:-2] + '\n' |
|
629 | 629 | nlines.append(l) |
|
630 | 630 | self.lines = nlines |
|
631 | 631 | except IOError: |
|
632 | 632 | if self.create: |
|
633 | 633 | self.missing = False |
|
634 | 634 | if self.mode is None: |
|
635 | 635 | self.mode = (False, False) |
|
636 | 636 | if self.missing: |
|
637 | 637 | self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) |
|
638 | 638 | |
|
639 | 639 | self.hash = {} |
|
640 | 640 | self.dirty = 0 |
|
641 | 641 | self.offset = 0 |
|
642 | 642 | self.skew = 0 |
|
643 | 643 | self.rej = [] |
|
644 | 644 | self.fileprinted = False |
|
645 | 645 | self.printfile(False) |
|
646 | 646 | self.hunks = 0 |
|
647 | 647 | |
|
648 | 648 | def writelines(self, fname, lines, mode): |
|
649 | 649 | if self.eolmode == 'auto': |
|
650 | 650 | eol = self.eol |
|
651 | 651 | elif self.eolmode == 'crlf': |
|
652 | 652 | eol = '\r\n' |
|
653 | 653 | else: |
|
654 | 654 | eol = '\n' |
|
655 | 655 | |
|
656 | 656 | if self.eolmode != 'strict' and eol and eol != '\n': |
|
657 | 657 | rawlines = [] |
|
658 | 658 | for l in lines: |
|
659 | 659 | if l and l[-1] == '\n': |
|
660 | 660 | l = l[:-1] + eol |
|
661 | 661 | rawlines.append(l) |
|
662 | 662 | lines = rawlines |
|
663 | 663 | |
|
664 | 664 | self.backend.setfile(fname, ''.join(lines), mode, self.copysource) |
|
665 | 665 | |
|
666 | 666 | def printfile(self, warn): |
|
667 | 667 | if self.fileprinted: |
|
668 | 668 | return |
|
669 | 669 | if warn or self.ui.verbose: |
|
670 | 670 | self.fileprinted = True |
|
671 | 671 | s = _("patching file %s\n") % self.fname |
|
672 | 672 | if warn: |
|
673 | 673 | self.ui.warn(s) |
|
674 | 674 | else: |
|
675 | 675 | self.ui.note(s) |
|
676 | 676 | |
|
677 | 677 | |
|
678 | 678 | def findlines(self, l, linenum): |
|
679 | 679 | # looks through the hash and finds candidate lines. The |
|
680 | 680 | # result is a list of line numbers sorted based on distance |
|
681 | 681 | # from linenum |
|
682 | 682 | |
|
683 | 683 | cand = self.hash.get(l, []) |
|
684 | 684 | if len(cand) > 1: |
|
685 | 685 | # resort our list of potentials forward then back. |
|
686 | 686 | cand.sort(key=lambda x: abs(x - linenum)) |
|
687 | 687 | return cand |
|
688 | 688 | |
|
689 | 689 | def write_rej(self): |
|
690 | 690 | # our rejects are a little different from patch(1). This always |
|
691 | 691 | # creates rejects in the same form as the original patch. A file |
|
692 | 692 | # header is inserted so that you can run the reject through patch again |
|
693 | 693 | # without having to type the filename. |
|
694 | 694 | if not self.rej: |
|
695 | 695 | return |
|
696 | 696 | base = os.path.basename(self.fname) |
|
697 | 697 | lines = ["--- %s\n+++ %s\n" % (base, base)] |
|
698 | 698 | for x in self.rej: |
|
699 | 699 | for l in x.hunk: |
|
700 | 700 | lines.append(l) |
|
701 | 701 | if l[-1] != '\n': |
|
702 | 702 | lines.append("\n\ No newline at end of file\n") |
|
703 | 703 | self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) |
|
704 | 704 | |
|
705 | 705 | def apply(self, h): |
|
706 | 706 | if not h.complete(): |
|
707 | 707 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
|
708 | 708 | (h.number, h.desc, len(h.a), h.lena, len(h.b), |
|
709 | 709 | h.lenb)) |
|
710 | 710 | |
|
711 | 711 | self.hunks += 1 |
|
712 | 712 | |
|
713 | 713 | if self.missing: |
|
714 | 714 | self.rej.append(h) |
|
715 | 715 | return -1 |
|
716 | 716 | |
|
717 | 717 | if self.exists and self.create: |
|
718 | 718 | if self.copysource: |
|
719 | 719 | self.ui.warn(_("cannot create %s: destination already " |
|
720 | 720 | "exists\n" % self.fname)) |
|
721 | 721 | else: |
|
722 | 722 | self.ui.warn(_("file %s already exists\n") % self.fname) |
|
723 | 723 | self.rej.append(h) |
|
724 | 724 | return -1 |
|
725 | 725 | |
|
726 | 726 | if isinstance(h, binhunk): |
|
727 | 727 | if self.remove: |
|
728 | 728 | self.backend.unlink(self.fname) |
|
729 | 729 | else: |
|
730 | 730 | self.lines[:] = h.new() |
|
731 | 731 | self.offset += len(h.new()) |
|
732 | 732 | self.dirty = True |
|
733 | 733 | return 0 |
|
734 | 734 | |
|
735 | 735 | horig = h |
|
736 | 736 | if (self.eolmode in ('crlf', 'lf') |
|
737 | 737 | or self.eolmode == 'auto' and self.eol): |
|
738 | 738 | # If new eols are going to be normalized, then normalize |
|
739 | 739 | # hunk data before patching. Otherwise, preserve input |
|
740 | 740 | # line-endings. |
|
741 | 741 | h = h.getnormalized() |
|
742 | 742 | |
|
743 | 743 | # fast case first, no offsets, no fuzz |
|
744 | 744 | old, oldstart, new, newstart = h.fuzzit(0, False) |
|
745 | 745 | oldstart += self.offset |
|
746 | 746 | orig_start = oldstart |
|
747 | 747 | # if there's skew we want to emit the "(offset %d lines)" even |
|
748 | 748 | # when the hunk cleanly applies at start + skew, so skip the |
|
749 | 749 | # fast case code |
|
750 | 750 | if (self.skew == 0 and |
|
751 | 751 | diffhelpers.testhunk(old, self.lines, oldstart) == 0): |
|
752 | 752 | if self.remove: |
|
753 | 753 | self.backend.unlink(self.fname) |
|
754 | 754 | else: |
|
755 | 755 | self.lines[oldstart:oldstart + len(old)] = new |
|
756 | 756 | self.offset += len(new) - len(old) |
|
757 | 757 | self.dirty = True |
|
758 | 758 | return 0 |
|
759 | 759 | |
|
760 | 760 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it |
|
761 | 761 | self.hash = {} |
|
762 | 762 | for x, s in enumerate(self.lines): |
|
763 | 763 | self.hash.setdefault(s, []).append(x) |
|
764 | 764 | |
|
765 | 765 | for fuzzlen in xrange(3): |
|
766 | 766 | for toponly in [True, False]: |
|
767 | 767 | old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) |
|
768 | 768 | oldstart = oldstart + self.offset + self.skew |
|
769 | 769 | oldstart = min(oldstart, len(self.lines)) |
|
770 | 770 | if old: |
|
771 | 771 | cand = self.findlines(old[0][1:], oldstart) |
|
772 | 772 | else: |
|
773 | 773 | # Only adding lines with no or fuzzed context, just |
|
774 | 774 | # take the skew in account |
|
775 | 775 | cand = [oldstart] |
|
776 | 776 | |
|
777 | 777 | for l in cand: |
|
778 | 778 | if not old or diffhelpers.testhunk(old, self.lines, l) == 0: |
|
779 | 779 | self.lines[l : l + len(old)] = new |
|
780 | 780 | self.offset += len(new) - len(old) |
|
781 | 781 | self.skew = l - orig_start |
|
782 | 782 | self.dirty = True |
|
783 | 783 | offset = l - orig_start - fuzzlen |
|
784 | 784 | if fuzzlen: |
|
785 | 785 | msg = _("Hunk #%d succeeded at %d " |
|
786 | 786 | "with fuzz %d " |
|
787 | 787 | "(offset %d lines).\n") |
|
788 | 788 | self.printfile(True) |
|
789 | 789 | self.ui.warn(msg % |
|
790 | 790 | (h.number, l + 1, fuzzlen, offset)) |
|
791 | 791 | else: |
|
792 | 792 | msg = _("Hunk #%d succeeded at %d " |
|
793 | 793 | "(offset %d lines).\n") |
|
794 | 794 | self.ui.note(msg % (h.number, l + 1, offset)) |
|
795 | 795 | return fuzzlen |
|
796 | 796 | self.printfile(True) |
|
797 | 797 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) |
|
798 | 798 | self.rej.append(horig) |
|
799 | 799 | return -1 |
|
800 | 800 | |
|
801 | 801 | def close(self): |
|
802 | 802 | if self.dirty: |
|
803 | 803 | self.writelines(self.fname, self.lines, self.mode) |
|
804 | 804 | self.write_rej() |
|
805 | 805 | return len(self.rej) |
|
806 | 806 | |
|
807 | 807 | class hunk(object): |
|
808 | 808 | def __init__(self, desc, num, lr, context): |
|
809 | 809 | self.number = num |
|
810 | 810 | self.desc = desc |
|
811 | 811 | self.hunk = [desc] |
|
812 | 812 | self.a = [] |
|
813 | 813 | self.b = [] |
|
814 | 814 | self.starta = self.lena = None |
|
815 | 815 | self.startb = self.lenb = None |
|
816 | 816 | if lr is not None: |
|
817 | 817 | if context: |
|
818 | 818 | self.read_context_hunk(lr) |
|
819 | 819 | else: |
|
820 | 820 | self.read_unified_hunk(lr) |
|
821 | 821 | |
|
822 | 822 | def getnormalized(self): |
|
823 | 823 | """Return a copy with line endings normalized to LF.""" |
|
824 | 824 | |
|
825 | 825 | def normalize(lines): |
|
826 | 826 | nlines = [] |
|
827 | 827 | for line in lines: |
|
828 | 828 | if line.endswith('\r\n'): |
|
829 | 829 | line = line[:-2] + '\n' |
|
830 | 830 | nlines.append(line) |
|
831 | 831 | return nlines |
|
832 | 832 | |
|
833 | 833 | # Dummy object, it is rebuilt manually |
|
834 | 834 | nh = hunk(self.desc, self.number, None, None) |
|
835 | 835 | nh.number = self.number |
|
836 | 836 | nh.desc = self.desc |
|
837 | 837 | nh.hunk = self.hunk |
|
838 | 838 | nh.a = normalize(self.a) |
|
839 | 839 | nh.b = normalize(self.b) |
|
840 | 840 | nh.starta = self.starta |
|
841 | 841 | nh.startb = self.startb |
|
842 | 842 | nh.lena = self.lena |
|
843 | 843 | nh.lenb = self.lenb |
|
844 | 844 | return nh |
|
845 | 845 | |
|
846 | 846 | def read_unified_hunk(self, lr): |
|
847 | 847 | m = unidesc.match(self.desc) |
|
848 | 848 | if not m: |
|
849 | 849 | raise PatchError(_("bad hunk #%d") % self.number) |
|
850 | 850 | self.starta, self.lena, self.startb, self.lenb = m.groups() |
|
851 | 851 | if self.lena is None: |
|
852 | 852 | self.lena = 1 |
|
853 | 853 | else: |
|
854 | 854 | self.lena = int(self.lena) |
|
855 | 855 | if self.lenb is None: |
|
856 | 856 | self.lenb = 1 |
|
857 | 857 | else: |
|
858 | 858 | self.lenb = int(self.lenb) |
|
859 | 859 | self.starta = int(self.starta) |
|
860 | 860 | self.startb = int(self.startb) |
|
861 | 861 | diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, |
|
862 | 862 | self.b) |
|
863 | 863 | # if we hit eof before finishing out the hunk, the last line will |
|
864 | 864 | # be zero length. Lets try to fix it up. |
|
865 | 865 | while len(self.hunk[-1]) == 0: |
|
866 | 866 | del self.hunk[-1] |
|
867 | 867 | del self.a[-1] |
|
868 | 868 | del self.b[-1] |
|
869 | 869 | self.lena -= 1 |
|
870 | 870 | self.lenb -= 1 |
|
871 | 871 | self._fixnewline(lr) |
|
872 | 872 | |
|
873 | 873 | def read_context_hunk(self, lr): |
|
874 | 874 | self.desc = lr.readline() |
|
875 | 875 | m = contextdesc.match(self.desc) |
|
876 | 876 | if not m: |
|
877 | 877 | raise PatchError(_("bad hunk #%d") % self.number) |
|
878 | 878 | self.starta, aend = m.groups() |
|
879 | 879 | self.starta = int(self.starta) |
|
880 | 880 | if aend is None: |
|
881 | 881 | aend = self.starta |
|
882 | 882 | self.lena = int(aend) - self.starta |
|
883 | 883 | if self.starta: |
|
884 | 884 | self.lena += 1 |
|
885 | 885 | for x in xrange(self.lena): |
|
886 | 886 | l = lr.readline() |
|
887 | 887 | if l.startswith('---'): |
|
888 | 888 | # lines addition, old block is empty |
|
889 | 889 | lr.push(l) |
|
890 | 890 | break |
|
891 | 891 | s = l[2:] |
|
892 | 892 | if l.startswith('- ') or l.startswith('! '): |
|
893 | 893 | u = '-' + s |
|
894 | 894 | elif l.startswith(' '): |
|
895 | 895 | u = ' ' + s |
|
896 | 896 | else: |
|
897 | 897 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
898 | 898 | (self.number, x)) |
|
899 | 899 | self.a.append(u) |
|
900 | 900 | self.hunk.append(u) |
|
901 | 901 | |
|
902 | 902 | l = lr.readline() |
|
903 | 903 | if l.startswith('\ '): |
|
904 | 904 | s = self.a[-1][:-1] |
|
905 | 905 | self.a[-1] = s |
|
906 | 906 | self.hunk[-1] = s |
|
907 | 907 | l = lr.readline() |
|
908 | 908 | m = contextdesc.match(l) |
|
909 | 909 | if not m: |
|
910 | 910 | raise PatchError(_("bad hunk #%d") % self.number) |
|
911 | 911 | self.startb, bend = m.groups() |
|
912 | 912 | self.startb = int(self.startb) |
|
913 | 913 | if bend is None: |
|
914 | 914 | bend = self.startb |
|
915 | 915 | self.lenb = int(bend) - self.startb |
|
916 | 916 | if self.startb: |
|
917 | 917 | self.lenb += 1 |
|
918 | 918 | hunki = 1 |
|
919 | 919 | for x in xrange(self.lenb): |
|
920 | 920 | l = lr.readline() |
|
921 | 921 | if l.startswith('\ '): |
|
922 | 922 | # XXX: the only way to hit this is with an invalid line range. |
|
923 | 923 | # The no-eol marker is not counted in the line range, but I |
|
924 | 924 | # guess there are diff(1) out there which behave differently. |
|
925 | 925 | s = self.b[-1][:-1] |
|
926 | 926 | self.b[-1] = s |
|
927 | 927 | self.hunk[hunki - 1] = s |
|
928 | 928 | continue |
|
929 | 929 | if not l: |
|
930 | 930 | # line deletions, new block is empty and we hit EOF |
|
931 | 931 | lr.push(l) |
|
932 | 932 | break |
|
933 | 933 | s = l[2:] |
|
934 | 934 | if l.startswith('+ ') or l.startswith('! '): |
|
935 | 935 | u = '+' + s |
|
936 | 936 | elif l.startswith(' '): |
|
937 | 937 | u = ' ' + s |
|
938 | 938 | elif len(self.b) == 0: |
|
939 | 939 | # line deletions, new block is empty |
|
940 | 940 | lr.push(l) |
|
941 | 941 | break |
|
942 | 942 | else: |
|
943 | 943 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
944 | 944 | (self.number, x)) |
|
945 | 945 | self.b.append(s) |
|
946 | 946 | while True: |
|
947 | 947 | if hunki >= len(self.hunk): |
|
948 | 948 | h = "" |
|
949 | 949 | else: |
|
950 | 950 | h = self.hunk[hunki] |
|
951 | 951 | hunki += 1 |
|
952 | 952 | if h == u: |
|
953 | 953 | break |
|
954 | 954 | elif h.startswith('-'): |
|
955 | 955 | continue |
|
956 | 956 | else: |
|
957 | 957 | self.hunk.insert(hunki - 1, u) |
|
958 | 958 | break |
|
959 | 959 | |
|
960 | 960 | if not self.a: |
|
961 | 961 | # this happens when lines were only added to the hunk |
|
962 | 962 | for x in self.hunk: |
|
963 | 963 | if x.startswith('-') or x.startswith(' '): |
|
964 | 964 | self.a.append(x) |
|
965 | 965 | if not self.b: |
|
966 | 966 | # this happens when lines were only deleted from the hunk |
|
967 | 967 | for x in self.hunk: |
|
968 | 968 | if x.startswith('+') or x.startswith(' '): |
|
969 | 969 | self.b.append(x[1:]) |
|
970 | 970 | # @@ -start,len +start,len @@ |
|
971 | 971 | self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, |
|
972 | 972 | self.startb, self.lenb) |
|
973 | 973 | self.hunk[0] = self.desc |
|
974 | 974 | self._fixnewline(lr) |
|
975 | 975 | |
|
976 | 976 | def _fixnewline(self, lr): |
|
977 | 977 | l = lr.readline() |
|
978 | 978 | if l.startswith('\ '): |
|
979 | 979 | diffhelpers.fix_newline(self.hunk, self.a, self.b) |
|
980 | 980 | else: |
|
981 | 981 | lr.push(l) |
|
982 | 982 | |
|
983 | 983 | def complete(self): |
|
984 | 984 | return len(self.a) == self.lena and len(self.b) == self.lenb |
|
985 | 985 | |
|
986 | 986 | def _fuzzit(self, old, new, fuzz, toponly): |
|
987 | 987 | # this removes context lines from the top and bottom of list 'l'. It |
|
988 | 988 | # checks the hunk to make sure only context lines are removed, and then |
|
989 | 989 | # returns a new shortened list of lines. |
|
990 | 990 | fuzz = min(fuzz, len(old)) |
|
991 | 991 | if fuzz: |
|
992 | 992 | top = 0 |
|
993 | 993 | bot = 0 |
|
994 | 994 | hlen = len(self.hunk) |
|
995 | 995 | for x in xrange(hlen - 1): |
|
996 | 996 | # the hunk starts with the @@ line, so use x+1 |
|
997 | 997 | if self.hunk[x + 1][0] == ' ': |
|
998 | 998 | top += 1 |
|
999 | 999 | else: |
|
1000 | 1000 | break |
|
1001 | 1001 | if not toponly: |
|
1002 | 1002 | for x in xrange(hlen - 1): |
|
1003 | 1003 | if self.hunk[hlen - bot - 1][0] == ' ': |
|
1004 | 1004 | bot += 1 |
|
1005 | 1005 | else: |
|
1006 | 1006 | break |
|
1007 | 1007 | |
|
1008 | 1008 | bot = min(fuzz, bot) |
|
1009 | 1009 | top = min(fuzz, top) |
|
1010 | 1010 | return old[top:len(old)-bot], new[top:len(new)-bot], top |
|
1011 | 1011 | return old, new, 0 |
|
1012 | 1012 | |
|
1013 | 1013 | def fuzzit(self, fuzz, toponly): |
|
1014 | 1014 | old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly) |
|
1015 | 1015 | oldstart = self.starta + top |
|
1016 | 1016 | newstart = self.startb + top |
|
1017 | 1017 | # zero length hunk ranges already have their start decremented |
|
1018 | 1018 | if self.lena and oldstart > 0: |
|
1019 | 1019 | oldstart -= 1 |
|
1020 | 1020 | if self.lenb and newstart > 0: |
|
1021 | 1021 | newstart -= 1 |
|
1022 | 1022 | return old, oldstart, new, newstart |
|
1023 | 1023 | |
|
1024 | 1024 | class binhunk(object): |
|
1025 | 1025 | 'A binary patch file. Only understands literals so far.' |
|
1026 | 1026 | def __init__(self, lr, fname): |
|
1027 | 1027 | self.text = None |
|
1028 | 1028 | self.hunk = ['GIT binary patch\n'] |
|
1029 | 1029 | self._fname = fname |
|
1030 | 1030 | self._read(lr) |
|
1031 | 1031 | |
|
1032 | 1032 | def complete(self): |
|
1033 | 1033 | return self.text is not None |
|
1034 | 1034 | |
|
1035 | 1035 | def new(self): |
|
1036 | 1036 | return [self.text] |
|
1037 | 1037 | |
|
1038 | 1038 | def _read(self, lr): |
|
1039 | 1039 | def getline(lr, hunk): |
|
1040 | 1040 | l = lr.readline() |
|
1041 | 1041 | hunk.append(l) |
|
1042 | 1042 | return l.rstrip('\r\n') |
|
1043 | 1043 | |
|
1044 | 1044 | while True: |
|
1045 | 1045 | line = getline(lr, self.hunk) |
|
1046 | 1046 | if not line: |
|
1047 | 1047 | raise PatchError(_('could not extract "%s" binary data') |
|
1048 | 1048 | % self._fname) |
|
1049 | 1049 | if line.startswith('literal '): |
|
1050 | 1050 | break |
|
1051 | 1051 | size = int(line[8:].rstrip()) |
|
1052 | 1052 | dec = [] |
|
1053 | 1053 | line = getline(lr, self.hunk) |
|
1054 | 1054 | while len(line) > 1: |
|
1055 | 1055 | l = line[0] |
|
1056 | 1056 | if l <= 'Z' and l >= 'A': |
|
1057 | 1057 | l = ord(l) - ord('A') + 1 |
|
1058 | 1058 | else: |
|
1059 | 1059 | l = ord(l) - ord('a') + 27 |
|
1060 | 1060 | try: |
|
1061 | 1061 | dec.append(base85.b85decode(line[1:])[:l]) |
|
1062 | 1062 | except ValueError, e: |
|
1063 | 1063 | raise PatchError(_('could not decode "%s" binary patch: %s') |
|
1064 | 1064 | % (self._fname, str(e))) |
|
1065 | 1065 | line = getline(lr, self.hunk) |
|
1066 | 1066 | text = zlib.decompress(''.join(dec)) |
|
1067 | 1067 | if len(text) != size: |
|
1068 | 1068 | raise PatchError(_('"%s" length is %d bytes, should be %d') |
|
1069 | 1069 | % (self._fname, len(text), size)) |
|
1070 | 1070 | self.text = text |
|
1071 | 1071 | |
|
1072 | 1072 | def parsefilename(str): |
|
1073 | 1073 | # --- filename \t|space stuff |
|
1074 | 1074 | s = str[4:].rstrip('\r\n') |
|
1075 | 1075 | i = s.find('\t') |
|
1076 | 1076 | if i < 0: |
|
1077 | 1077 | i = s.find(' ') |
|
1078 | 1078 | if i < 0: |
|
1079 | 1079 | return s |
|
1080 | 1080 | return s[:i] |
|
1081 | 1081 | |
|
1082 | 1082 | def pathstrip(path, strip): |
|
1083 | 1083 | pathlen = len(path) |
|
1084 | 1084 | i = 0 |
|
1085 | 1085 | if strip == 0: |
|
1086 | 1086 | return '', path.rstrip() |
|
1087 | 1087 | count = strip |
|
1088 | 1088 | while count > 0: |
|
1089 | 1089 | i = path.find('/', i) |
|
1090 | 1090 | if i == -1: |
|
1091 | 1091 | raise PatchError(_("unable to strip away %d of %d dirs from %s") % |
|
1092 | 1092 | (count, strip, path)) |
|
1093 | 1093 | i += 1 |
|
1094 | 1094 | # consume '//' in the path |
|
1095 | 1095 | while i < pathlen - 1 and path[i] == '/': |
|
1096 | 1096 | i += 1 |
|
1097 | 1097 | count -= 1 |
|
1098 | 1098 | return path[:i].lstrip(), path[i:].rstrip() |
|
1099 | 1099 | |
|
1100 | 1100 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip): |
|
1101 | 1101 | nulla = afile_orig == "/dev/null" |
|
1102 | 1102 | nullb = bfile_orig == "/dev/null" |
|
1103 | 1103 | create = nulla and hunk.starta == 0 and hunk.lena == 0 |
|
1104 | 1104 | remove = nullb and hunk.startb == 0 and hunk.lenb == 0 |
|
1105 | 1105 | abase, afile = pathstrip(afile_orig, strip) |
|
1106 | 1106 | gooda = not nulla and backend.exists(afile) |
|
1107 | 1107 | bbase, bfile = pathstrip(bfile_orig, strip) |
|
1108 | 1108 | if afile == bfile: |
|
1109 | 1109 | goodb = gooda |
|
1110 | 1110 | else: |
|
1111 | 1111 | goodb = not nullb and backend.exists(bfile) |
|
1112 | 1112 | missing = not goodb and not gooda and not create |
|
1113 | 1113 | |
|
1114 | 1114 | # some diff programs apparently produce patches where the afile is |
|
1115 | 1115 | # not /dev/null, but afile starts with bfile |
|
1116 | 1116 | abasedir = afile[:afile.rfind('/') + 1] |
|
1117 | 1117 | bbasedir = bfile[:bfile.rfind('/') + 1] |
|
1118 | 1118 | if (missing and abasedir == bbasedir and afile.startswith(bfile) |
|
1119 | 1119 | and hunk.starta == 0 and hunk.lena == 0): |
|
1120 | 1120 | create = True |
|
1121 | 1121 | missing = False |
|
1122 | 1122 | |
|
1123 | 1123 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the |
|
1124 | 1124 | # diff is between a file and its backup. In this case, the original |
|
1125 | 1125 | # file should be patched (see original mpatch code). |
|
1126 | 1126 | isbackup = (abase == bbase and bfile.startswith(afile)) |
|
1127 | 1127 | fname = None |
|
1128 | 1128 | if not missing: |
|
1129 | 1129 | if gooda and goodb: |
|
1130 | 1130 | fname = isbackup and afile or bfile |
|
1131 | 1131 | elif gooda: |
|
1132 | 1132 | fname = afile |
|
1133 | 1133 | |
|
1134 | 1134 | if not fname: |
|
1135 | 1135 | if not nullb: |
|
1136 | 1136 | fname = isbackup and afile or bfile |
|
1137 | 1137 | elif not nulla: |
|
1138 | 1138 | fname = afile |
|
1139 | 1139 | else: |
|
1140 | 1140 | raise PatchError(_("undefined source and destination files")) |
|
1141 | 1141 | |
|
1142 | 1142 | gp = patchmeta(fname) |
|
1143 | 1143 | if create: |
|
1144 | 1144 | gp.op = 'ADD' |
|
1145 | 1145 | elif remove: |
|
1146 | 1146 | gp.op = 'DELETE' |
|
1147 | 1147 | return gp |
|
1148 | 1148 | |
|
1149 | 1149 | def scangitpatch(lr, firstline): |
|
1150 | 1150 | """ |
|
1151 | 1151 | Git patches can emit: |
|
1152 | 1152 | - rename a to b |
|
1153 | 1153 | - change b |
|
1154 | 1154 | - copy a to c |
|
1155 | 1155 | - change c |
|
1156 | 1156 | |
|
1157 | 1157 | We cannot apply this sequence as-is, the renamed 'a' could not be |
|
1158 | 1158 | found for it would have been renamed already. And we cannot copy |
|
1159 | 1159 | from 'b' instead because 'b' would have been changed already. So |
|
1160 | 1160 | we scan the git patch for copy and rename commands so we can |
|
1161 | 1161 | perform the copies ahead of time. |
|
1162 | 1162 | """ |
|
1163 | 1163 | pos = 0 |
|
1164 | 1164 | try: |
|
1165 | 1165 | pos = lr.fp.tell() |
|
1166 | 1166 | fp = lr.fp |
|
1167 | 1167 | except IOError: |
|
1168 | 1168 | fp = cStringIO.StringIO(lr.fp.read()) |
|
1169 | 1169 | gitlr = linereader(fp) |
|
1170 | 1170 | gitlr.push(firstline) |
|
1171 | 1171 | gitpatches = readgitpatch(gitlr) |
|
1172 | 1172 | fp.seek(pos) |
|
1173 | 1173 | return gitpatches |
|
1174 | 1174 | |
|
1175 | 1175 | def iterhunks(fp): |
|
1176 | 1176 | """Read a patch and yield the following events: |
|
1177 | 1177 | - ("file", afile, bfile, firsthunk): select a new target file. |
|
1178 | 1178 | - ("hunk", hunk): a new hunk is ready to be applied, follows a |
|
1179 | 1179 | "file" event. |
|
1180 | 1180 | - ("git", gitchanges): current diff is in git format, gitchanges |
|
1181 | 1181 | maps filenames to gitpatch records. Unique event. |
|
1182 | 1182 | """ |
|
1183 | 1183 | afile = "" |
|
1184 | 1184 | bfile = "" |
|
1185 | 1185 | state = None |
|
1186 | 1186 | hunknum = 0 |
|
1187 | 1187 | emitfile = newfile = False |
|
1188 | 1188 | gitpatches = None |
|
1189 | 1189 | |
|
1190 | 1190 | # our states |
|
1191 | 1191 | BFILE = 1 |
|
1192 | 1192 | context = None |
|
1193 | 1193 | lr = linereader(fp) |
|
1194 | 1194 | |
|
1195 | 1195 | while True: |
|
1196 | 1196 | x = lr.readline() |
|
1197 | 1197 | if not x: |
|
1198 | 1198 | break |
|
1199 | 1199 | if state == BFILE and ( |
|
1200 | 1200 | (not context and x[0] == '@') |
|
1201 | 1201 | or (context is not False and x.startswith('***************')) |
|
1202 | 1202 | or x.startswith('GIT binary patch')): |
|
1203 | 1203 | gp = None |
|
1204 | 1204 | if (gitpatches and |
|
1205 | 1205 | gitpatches[-1].ispatching(afile, bfile)): |
|
1206 | 1206 | gp = gitpatches.pop() |
|
1207 | 1207 | if x.startswith('GIT binary patch'): |
|
1208 | 1208 | h = binhunk(lr, gp.path) |
|
1209 | 1209 | else: |
|
1210 | 1210 | if context is None and x.startswith('***************'): |
|
1211 | 1211 | context = True |
|
1212 | 1212 | h = hunk(x, hunknum + 1, lr, context) |
|
1213 | 1213 | hunknum += 1 |
|
1214 | 1214 | if emitfile: |
|
1215 | 1215 | emitfile = False |
|
1216 | 1216 | yield 'file', (afile, bfile, h, gp and gp.copy() or None) |
|
1217 | 1217 | yield 'hunk', h |
|
1218 | 1218 | elif x.startswith('diff --git'): |
|
1219 | 1219 | m = gitre.match(x.rstrip(' \r\n')) |
|
1220 | 1220 | if not m: |
|
1221 | 1221 | continue |
|
1222 | 1222 | if gitpatches is None: |
|
1223 | 1223 | # scan whole input for git metadata |
|
1224 | 1224 | gitpatches = scangitpatch(lr, x) |
|
1225 | 1225 | yield 'git', [g.copy() for g in gitpatches |
|
1226 | 1226 | if g.op in ('COPY', 'RENAME')] |
|
1227 | 1227 | gitpatches.reverse() |
|
1228 | 1228 | afile = 'a/' + m.group(1) |
|
1229 | 1229 | bfile = 'b/' + m.group(2) |
|
1230 | 1230 | while gitpatches and not gitpatches[-1].ispatching(afile, bfile): |
|
1231 | 1231 | gp = gitpatches.pop() |
|
1232 | 1232 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) |
|
1233 | 1233 | if not gitpatches: |
|
1234 | 1234 | raise PatchError(_('failed to synchronize metadata for "%s"') |
|
1235 | 1235 | % afile[2:]) |
|
1236 | 1236 | gp = gitpatches[-1] |
|
1237 | 1237 | newfile = True |
|
1238 | 1238 | elif x.startswith('---'): |
|
1239 | 1239 | # check for a unified diff |
|
1240 | 1240 | l2 = lr.readline() |
|
1241 | 1241 | if not l2.startswith('+++'): |
|
1242 | 1242 | lr.push(l2) |
|
1243 | 1243 | continue |
|
1244 | 1244 | newfile = True |
|
1245 | 1245 | context = False |
|
1246 | 1246 | afile = parsefilename(x) |
|
1247 | 1247 | bfile = parsefilename(l2) |
|
1248 | 1248 | elif x.startswith('***'): |
|
1249 | 1249 | # check for a context diff |
|
1250 | 1250 | l2 = lr.readline() |
|
1251 | 1251 | if not l2.startswith('---'): |
|
1252 | 1252 | lr.push(l2) |
|
1253 | 1253 | continue |
|
1254 | 1254 | l3 = lr.readline() |
|
1255 | 1255 | lr.push(l3) |
|
1256 | 1256 | if not l3.startswith("***************"): |
|
1257 | 1257 | lr.push(l2) |
|
1258 | 1258 | continue |
|
1259 | 1259 | newfile = True |
|
1260 | 1260 | context = True |
|
1261 | 1261 | afile = parsefilename(x) |
|
1262 | 1262 | bfile = parsefilename(l2) |
|
1263 | 1263 | |
|
1264 | 1264 | if newfile: |
|
1265 | 1265 | newfile = False |
|
1266 | 1266 | emitfile = True |
|
1267 | 1267 | state = BFILE |
|
1268 | 1268 | hunknum = 0 |
|
1269 | 1269 | |
|
1270 | 1270 | while gitpatches: |
|
1271 | 1271 | gp = gitpatches.pop() |
|
1272 | 1272 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) |
|
1273 | 1273 | |
|
1274 | 1274 | def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'): |
|
1275 | 1275 | """Reads a patch from fp and tries to apply it. |
|
1276 | 1276 | |
|
1277 | 1277 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if |
|
1278 | 1278 | there was any fuzz. |
|
1279 | 1279 | |
|
1280 | 1280 | If 'eolmode' is 'strict', the patch content and patched file are |
|
1281 | 1281 | read in binary mode. Otherwise, line endings are ignored when |
|
1282 | 1282 | patching then normalized according to 'eolmode'. |
|
1283 | 1283 | """ |
|
1284 | 1284 | return _applydiff(ui, fp, patchfile, backend, store, strip=strip, |
|
1285 | 1285 | eolmode=eolmode) |
|
1286 | 1286 | |
|
1287 | 1287 | def _applydiff(ui, fp, patcher, backend, store, strip=1, |
|
1288 | 1288 | eolmode='strict'): |
|
1289 | 1289 | |
|
1290 | 1290 | def pstrip(p): |
|
1291 | 1291 | return pathstrip(p, strip - 1)[1] |
|
1292 | 1292 | |
|
1293 | 1293 | rejects = 0 |
|
1294 | 1294 | err = 0 |
|
1295 | 1295 | current_file = None |
|
1296 | 1296 | |
|
1297 | 1297 | for state, values in iterhunks(fp): |
|
1298 | 1298 | if state == 'hunk': |
|
1299 | 1299 | if not current_file: |
|
1300 | 1300 | continue |
|
1301 | 1301 | ret = current_file.apply(values) |
|
1302 | 1302 | if ret > 0: |
|
1303 | 1303 | err = 1 |
|
1304 | 1304 | elif state == 'file': |
|
1305 | 1305 | if current_file: |
|
1306 | 1306 | rejects += current_file.close() |
|
1307 | 1307 | current_file = None |
|
1308 | 1308 | afile, bfile, first_hunk, gp = values |
|
1309 | 1309 | if gp: |
|
1310 | 1310 | gp.path = pstrip(gp.path) |
|
1311 | 1311 | if gp.oldpath: |
|
1312 | 1312 | gp.oldpath = pstrip(gp.oldpath) |
|
1313 | 1313 | else: |
|
1314 | 1314 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip) |
|
1315 | 1315 | if gp.op == 'RENAME': |
|
1316 | 1316 | backend.unlink(gp.oldpath) |
|
1317 | 1317 | if not first_hunk: |
|
1318 | 1318 | if gp.op == 'DELETE': |
|
1319 | 1319 | backend.unlink(gp.path) |
|
1320 | 1320 | continue |
|
1321 | 1321 | data, mode = None, None |
|
1322 | 1322 | if gp.op in ('RENAME', 'COPY'): |
|
1323 | 1323 | data, mode = store.getfile(gp.oldpath)[:2] |
|
1324 | 1324 | if gp.mode: |
|
1325 | 1325 | mode = gp.mode |
|
1326 | 1326 | if gp.op == 'ADD': |
|
1327 | 1327 | # Added files without content have no hunk and |
|
1328 | 1328 | # must be created |
|
1329 | 1329 | data = '' |
|
1330 | 1330 | if data or mode: |
|
1331 | 1331 | if (gp.op in ('ADD', 'RENAME', 'COPY') |
|
1332 | 1332 | and backend.exists(gp.path)): |
|
1333 | 1333 | raise PatchError(_("cannot create %s: destination " |
|
1334 | 1334 | "already exists") % gp.path) |
|
1335 | 1335 | backend.setfile(gp.path, data, mode, gp.oldpath) |
|
1336 | 1336 | continue |
|
1337 | 1337 | try: |
|
1338 | 1338 | current_file = patcher(ui, gp, backend, store, |
|
1339 | 1339 | eolmode=eolmode) |
|
1340 | 1340 | except PatchError, inst: |
|
1341 | 1341 | ui.warn(str(inst) + '\n') |
|
1342 | 1342 | current_file = None |
|
1343 | 1343 | rejects += 1 |
|
1344 | 1344 | continue |
|
1345 | 1345 | elif state == 'git': |
|
1346 | 1346 | for gp in values: |
|
1347 | 1347 | path = pstrip(gp.oldpath) |
|
1348 | 1348 | try: |
|
1349 | 1349 | data, mode = backend.getfile(path) |
|
1350 | 1350 | except IOError, e: |
|
1351 | 1351 | if e.errno != errno.ENOENT: |
|
1352 | 1352 | raise |
|
1353 | 1353 | # The error ignored here will trigger a getfile() |
|
1354 | 1354 | # error in a place more appropriate for error |
|
1355 | 1355 | # handling, and will not interrupt the patching |
|
1356 | 1356 | # process. |
|
1357 | 1357 | else: |
|
1358 | 1358 | store.setfile(path, data, mode) |
|
1359 | 1359 | else: |
|
1360 | 1360 | raise util.Abort(_('unsupported parser state: %s') % state) |
|
1361 | 1361 | |
|
1362 | 1362 | if current_file: |
|
1363 | 1363 | rejects += current_file.close() |
|
1364 | 1364 | |
|
1365 | 1365 | if rejects: |
|
1366 | 1366 | return -1 |
|
1367 | 1367 | return err |
|
1368 | 1368 | |
|
1369 | 1369 | def _externalpatch(ui, repo, patcher, patchname, strip, files, |
|
1370 | 1370 | similarity): |
|
1371 | 1371 | """use <patcher> to apply <patchname> to the working directory. |
|
1372 | 1372 | returns whether patch was applied with fuzz factor.""" |
|
1373 | 1373 | |
|
1374 | 1374 | fuzz = False |
|
1375 | 1375 | args = [] |
|
1376 | 1376 | cwd = repo.root |
|
1377 | 1377 | if cwd: |
|
1378 | 1378 | args.append('-d %s' % util.shellquote(cwd)) |
|
1379 | 1379 | fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, |
|
1380 | 1380 | util.shellquote(patchname))) |
|
1381 | 1381 | try: |
|
1382 | 1382 | for line in fp: |
|
1383 | 1383 | line = line.rstrip() |
|
1384 | 1384 | ui.note(line + '\n') |
|
1385 | 1385 | if line.startswith('patching file '): |
|
1386 | 1386 | pf = util.parsepatchoutput(line) |
|
1387 | 1387 | printed_file = False |
|
1388 | 1388 | files.add(pf) |
|
1389 | 1389 | elif line.find('with fuzz') >= 0: |
|
1390 | 1390 | fuzz = True |
|
1391 | 1391 | if not printed_file: |
|
1392 | 1392 | ui.warn(pf + '\n') |
|
1393 | 1393 | printed_file = True |
|
1394 | 1394 | ui.warn(line + '\n') |
|
1395 | 1395 | elif line.find('saving rejects to file') >= 0: |
|
1396 | 1396 | ui.warn(line + '\n') |
|
1397 | 1397 | elif line.find('FAILED') >= 0: |
|
1398 | 1398 | if not printed_file: |
|
1399 | 1399 | ui.warn(pf + '\n') |
|
1400 | 1400 | printed_file = True |
|
1401 | 1401 | ui.warn(line + '\n') |
|
1402 | 1402 | finally: |
|
1403 | 1403 | if files: |
|
1404 | 1404 | cfiles = list(files) |
|
1405 | 1405 | cwd = repo.getcwd() |
|
1406 | 1406 | if cwd: |
|
1407 | 1407 | cfiles = [util.pathto(repo.root, cwd, f) |
|
1408 | 1408 | for f in cfiles] |
|
1409 | 1409 | scmutil.addremove(repo, cfiles, similarity=similarity) |
|
1410 | 1410 | code = fp.close() |
|
1411 | 1411 | if code: |
|
1412 | 1412 | raise PatchError(_("patch command failed: %s") % |
|
1413 | 1413 | util.explainexit(code)[0]) |
|
1414 | 1414 | return fuzz |
|
1415 | 1415 | |
|
1416 | 1416 | def patchbackend(ui, backend, patchobj, strip, files=None, eolmode='strict'): |
|
1417 | 1417 | if files is None: |
|
1418 | 1418 | files = set() |
|
1419 | 1419 | if eolmode is None: |
|
1420 | 1420 | eolmode = ui.config('patch', 'eol', 'strict') |
|
1421 | 1421 | if eolmode.lower() not in eolmodes: |
|
1422 | 1422 | raise util.Abort(_('unsupported line endings type: %s') % eolmode) |
|
1423 | 1423 | eolmode = eolmode.lower() |
|
1424 | 1424 | |
|
1425 | 1425 | store = filestore() |
|
1426 | 1426 | try: |
|
1427 | 1427 | fp = open(patchobj, 'rb') |
|
1428 | 1428 | except TypeError: |
|
1429 | 1429 | fp = patchobj |
|
1430 | 1430 | try: |
|
1431 | 1431 | ret = applydiff(ui, fp, backend, store, strip=strip, |
|
1432 | 1432 | eolmode=eolmode) |
|
1433 | 1433 | finally: |
|
1434 | 1434 | if fp != patchobj: |
|
1435 | 1435 | fp.close() |
|
1436 | 1436 | files.update(backend.close()) |
|
1437 | 1437 | store.close() |
|
1438 | 1438 | if ret < 0: |
|
1439 | 1439 | raise PatchError(_('patch failed to apply')) |
|
1440 | 1440 | return ret > 0 |
|
1441 | 1441 | |
|
1442 | 1442 | def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict', |
|
1443 | 1443 | similarity=0): |
|
1444 | 1444 | """use builtin patch to apply <patchobj> to the working directory. |
|
1445 | 1445 | returns whether patch was applied with fuzz factor.""" |
|
1446 | 1446 | backend = workingbackend(ui, repo, similarity) |
|
1447 | 1447 | return patchbackend(ui, backend, patchobj, strip, files, eolmode) |
|
1448 | 1448 | |
|
1449 | 1449 | def patchrepo(ui, repo, ctx, store, patchobj, strip, files=None, |
|
1450 | 1450 | eolmode='strict'): |
|
1451 | 1451 | backend = repobackend(ui, repo, ctx, store) |
|
1452 | 1452 | return patchbackend(ui, backend, patchobj, strip, files, eolmode) |
|
1453 | 1453 | |
|
1454 | 1454 | def makememctx(repo, parents, text, user, date, branch, files, store, |
|
1455 | 1455 | editor=None): |
|
1456 | 1456 | def getfilectx(repo, memctx, path): |
|
1457 | 1457 | data, (islink, isexec), copied = store.getfile(path) |
|
1458 | 1458 | return context.memfilectx(path, data, islink=islink, isexec=isexec, |
|
1459 | 1459 | copied=copied) |
|
1460 | 1460 | extra = {} |
|
1461 | 1461 | if branch: |
|
1462 | 1462 | extra['branch'] = encoding.fromlocal(branch) |
|
1463 | 1463 | ctx = context.memctx(repo, parents, text, files, getfilectx, user, |
|
1464 | 1464 | date, extra) |
|
1465 | 1465 | if editor: |
|
1466 | 1466 | ctx._text = editor(repo, ctx, []) |
|
1467 | 1467 | return ctx |
|
1468 | 1468 | |
|
1469 | 1469 | def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict', |
|
1470 | 1470 | similarity=0): |
|
1471 | 1471 | """Apply <patchname> to the working directory. |
|
1472 | 1472 | |
|
1473 | 1473 | 'eolmode' specifies how end of lines should be handled. It can be: |
|
1474 | 1474 | - 'strict': inputs are read in binary mode, EOLs are preserved |
|
1475 | 1475 | - 'crlf': EOLs are ignored when patching and reset to CRLF |
|
1476 | 1476 | - 'lf': EOLs are ignored when patching and reset to LF |
|
1477 | 1477 | - None: get it from user settings, default to 'strict' |
|
1478 | 1478 | 'eolmode' is ignored when using an external patcher program. |
|
1479 | 1479 | |
|
1480 | 1480 | Returns whether patch was applied with fuzz factor. |
|
1481 | 1481 | """ |
|
1482 | 1482 | patcher = ui.config('ui', 'patch') |
|
1483 | 1483 | if files is None: |
|
1484 | 1484 | files = set() |
|
1485 | 1485 | try: |
|
1486 | 1486 | if patcher: |
|
1487 | 1487 | return _externalpatch(ui, repo, patcher, patchname, strip, |
|
1488 | 1488 | files, similarity) |
|
1489 | 1489 | return internalpatch(ui, repo, patchname, strip, files, eolmode, |
|
1490 | 1490 | similarity) |
|
1491 | 1491 | except PatchError, err: |
|
1492 | 1492 | raise util.Abort(str(err)) |
|
1493 | 1493 | |
|
1494 | 1494 | def changedfiles(ui, repo, patchpath, strip=1): |
|
1495 | 1495 | backend = fsbackend(ui, repo.root) |
|
1496 | 1496 | fp = open(patchpath, 'rb') |
|
1497 | 1497 | try: |
|
1498 | 1498 | changed = set() |
|
1499 | 1499 | for state, values in iterhunks(fp): |
|
1500 | 1500 | if state == 'file': |
|
1501 | 1501 | afile, bfile, first_hunk, gp = values |
|
1502 | 1502 | if gp: |
|
1503 | 1503 | gp.path = pathstrip(gp.path, strip - 1)[1] |
|
1504 | 1504 | if gp.oldpath: |
|
1505 | 1505 | gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1] |
|
1506 | 1506 | else: |
|
1507 | 1507 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip) |
|
1508 | 1508 | changed.add(gp.path) |
|
1509 | 1509 | if gp.op == 'RENAME': |
|
1510 | 1510 | changed.add(gp.oldpath) |
|
1511 | 1511 | elif state not in ('hunk', 'git'): |
|
1512 | 1512 | raise util.Abort(_('unsupported parser state: %s') % state) |
|
1513 | 1513 | return changed |
|
1514 | 1514 | finally: |
|
1515 | 1515 | fp.close() |
|
1516 | 1516 | |
|
1517 | 1517 | class GitDiffRequired(Exception): |
|
1518 | 1518 | pass |
|
1519 | 1519 | |
|
1520 | 1520 | def diffopts(ui, opts=None, untrusted=False, section='diff'): |
|
1521 | 1521 | def get(key, name=None, getter=ui.configbool): |
|
1522 | 1522 | return ((opts and opts.get(key)) or |
|
1523 | 1523 | getter(section, name or key, None, untrusted=untrusted)) |
|
1524 | 1524 | return mdiff.diffopts( |
|
1525 | 1525 | text=opts and opts.get('text'), |
|
1526 | 1526 | git=get('git'), |
|
1527 | 1527 | nodates=get('nodates'), |
|
1528 | 1528 | showfunc=get('show_function', 'showfunc'), |
|
1529 | 1529 | ignorews=get('ignore_all_space', 'ignorews'), |
|
1530 | 1530 | ignorewsamount=get('ignore_space_change', 'ignorewsamount'), |
|
1531 | 1531 | ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'), |
|
1532 | 1532 | context=get('unified', getter=ui.config)) |
|
1533 | 1533 | |
|
1534 | 1534 | def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None, |
|
1535 | 1535 | losedatafn=None, prefix=''): |
|
1536 | 1536 | '''yields diff of changes to files between two nodes, or node and |
|
1537 | 1537 | working directory. |
|
1538 | 1538 | |
|
1539 | 1539 | if node1 is None, use first dirstate parent instead. |
|
1540 | 1540 | if node2 is None, compare node1 with working directory. |
|
1541 | 1541 | |
|
1542 | 1542 | losedatafn(**kwarg) is a callable run when opts.upgrade=True and |
|
1543 | 1543 | every time some change cannot be represented with the current |
|
1544 | 1544 | patch format. Return False to upgrade to git patch format, True to |
|
1545 | 1545 | accept the loss or raise an exception to abort the diff. It is |
|
1546 | 1546 | called with the name of current file being diffed as 'fn'. If set |
|
1547 | 1547 | to None, patches will always be upgraded to git format when |
|
1548 | 1548 | necessary. |
|
1549 | 1549 | |
|
1550 | 1550 | prefix is a filename prefix that is prepended to all filenames on |
|
1551 | 1551 | display (used for subrepos). |
|
1552 | 1552 | ''' |
|
1553 | 1553 | |
|
1554 | 1554 | if opts is None: |
|
1555 | 1555 | opts = mdiff.defaultopts |
|
1556 | 1556 | |
|
1557 | 1557 | if not node1 and not node2: |
|
1558 | 1558 | node1 = repo.dirstate.p1() |
|
1559 | 1559 | |
|
1560 | 1560 | def lrugetfilectx(): |
|
1561 | 1561 | cache = {} |
|
1562 | 1562 | order = util.deque() |
|
1563 | 1563 | def getfilectx(f, ctx): |
|
1564 | 1564 | fctx = ctx.filectx(f, filelog=cache.get(f)) |
|
1565 | 1565 | if f not in cache: |
|
1566 | 1566 | if len(cache) > 20: |
|
1567 | 1567 | del cache[order.popleft()] |
|
1568 | 1568 | cache[f] = fctx.filelog() |
|
1569 | 1569 | else: |
|
1570 | 1570 | order.remove(f) |
|
1571 | 1571 | order.append(f) |
|
1572 | 1572 | return fctx |
|
1573 | 1573 | return getfilectx |
|
1574 | 1574 | getfilectx = lrugetfilectx() |
|
1575 | 1575 | |
|
1576 | 1576 | ctx1 = repo[node1] |
|
1577 | 1577 | ctx2 = repo[node2] |
|
1578 | 1578 | |
|
1579 | 1579 | if not changes: |
|
1580 | 1580 | changes = repo.status(ctx1, ctx2, match=match) |
|
1581 | 1581 | modified, added, removed = changes[:3] |
|
1582 | 1582 | |
|
1583 | 1583 | if not modified and not added and not removed: |
|
1584 | 1584 | return [] |
|
1585 | 1585 | |
|
1586 | 1586 | revs = None |
|
1587 | 1587 | hexfunc = repo.ui.debugflag and hex or short |
|
1588 | 1588 | revs = [hexfunc(node) for node in [node1, node2] if node] |
|
1589 | 1589 | |
|
1590 | 1590 | copy = {} |
|
1591 | 1591 | if opts.git or opts.upgrade: |
|
1592 | 1592 | copy = copies.pathcopies(ctx1, ctx2) |
|
1593 | 1593 | |
|
1594 | 1594 | def difffn(opts, losedata): |
|
1595 | 1595 | return trydiff(repo, revs, ctx1, ctx2, modified, added, removed, |
|
1596 | 1596 | copy, getfilectx, opts, losedata, prefix) |
|
1597 | 1597 | if opts.upgrade and not opts.git: |
|
1598 | 1598 | try: |
|
1599 | 1599 | def losedata(fn): |
|
1600 | 1600 | if not losedatafn or not losedatafn(fn=fn): |
|
1601 | 1601 | raise GitDiffRequired |
|
1602 | 1602 | # Buffer the whole output until we are sure it can be generated |
|
1603 | 1603 | return list(difffn(opts.copy(git=False), losedata)) |
|
1604 | 1604 | except GitDiffRequired: |
|
1605 | 1605 | return difffn(opts.copy(git=True), None) |
|
1606 | 1606 | else: |
|
1607 | 1607 | return difffn(opts, None) |
|
1608 | 1608 | |
|
1609 | 1609 | def difflabel(func, *args, **kw): |
|
1610 | 1610 | '''yields 2-tuples of (output, label) based on the output of func()''' |
|
1611 | 1611 | headprefixes = [('diff', 'diff.diffline'), |
|
1612 | 1612 | ('copy', 'diff.extended'), |
|
1613 | 1613 | ('rename', 'diff.extended'), |
|
1614 | 1614 | ('old', 'diff.extended'), |
|
1615 | 1615 | ('new', 'diff.extended'), |
|
1616 | 1616 | ('deleted', 'diff.extended'), |
|
1617 | 1617 | ('---', 'diff.file_a'), |
|
1618 | 1618 | ('+++', 'diff.file_b')] |
|
1619 | 1619 | textprefixes = [('@', 'diff.hunk'), |
|
1620 | 1620 | ('-', 'diff.deleted'), |
|
1621 | 1621 | ('+', 'diff.inserted')] |
|
1622 | 1622 | head = False |
|
1623 | 1623 | for chunk in func(*args, **kw): |
|
1624 | 1624 | lines = chunk.split('\n') |
|
1625 | 1625 | for i, line in enumerate(lines): |
|
1626 | 1626 | if i != 0: |
|
1627 | 1627 | yield ('\n', '') |
|
1628 | 1628 | if head: |
|
1629 | 1629 | if line.startswith('@'): |
|
1630 | 1630 | head = False |
|
1631 | 1631 | else: |
|
1632 | 1632 | if line and line[0] not in ' +-@\\': |
|
1633 | 1633 | head = True |
|
1634 | 1634 | stripline = line |
|
1635 | 1635 | if not head and line and line[0] in '+-': |
|
1636 | 1636 | # highlight trailing whitespace, but only in changed lines |
|
1637 | 1637 | stripline = line.rstrip() |
|
1638 | 1638 | prefixes = textprefixes |
|
1639 | 1639 | if head: |
|
1640 | 1640 | prefixes = headprefixes |
|
1641 | 1641 | for prefix, label in prefixes: |
|
1642 | 1642 | if stripline.startswith(prefix): |
|
1643 | 1643 | yield (stripline, label) |
|
1644 | 1644 | break |
|
1645 | 1645 | else: |
|
1646 | 1646 | yield (line, '') |
|
1647 | 1647 | if line != stripline: |
|
1648 | 1648 | yield (line[len(stripline):], 'diff.trailingwhitespace') |
|
1649 | 1649 | |
|
1650 | 1650 | def diffui(*args, **kw): |
|
1651 | 1651 | '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' |
|
1652 | 1652 | return difflabel(diff, *args, **kw) |
|
1653 | 1653 | |
|
1654 | 1654 | def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, |
|
1655 | 1655 | copy, getfilectx, opts, losedatafn, prefix): |
|
1656 | 1656 | |
|
1657 | 1657 | def join(f): |
|
1658 |
return |
|
|
1658 | return posixpath.join(prefix, f) | |
|
1659 | 1659 | |
|
1660 | 1660 | def addmodehdr(header, omode, nmode): |
|
1661 | 1661 | if omode != nmode: |
|
1662 | 1662 | header.append('old mode %s\n' % omode) |
|
1663 | 1663 | header.append('new mode %s\n' % nmode) |
|
1664 | 1664 | |
|
1665 | 1665 | def addindexmeta(meta, revs): |
|
1666 | 1666 | if opts.git: |
|
1667 | 1667 | i = len(revs) |
|
1668 | 1668 | if i==2: |
|
1669 | 1669 | meta.append('index %s..%s\n' % tuple(revs)) |
|
1670 | 1670 | elif i==3: |
|
1671 | 1671 | meta.append('index %s,%s..%s\n' % tuple(revs)) |
|
1672 | 1672 | |
|
1673 | 1673 | def gitindex(text): |
|
1674 | 1674 | if not text: |
|
1675 | 1675 | return hex(nullid) |
|
1676 | 1676 | l = len(text) |
|
1677 | 1677 | s = util.sha1('blob %d\0' % l) |
|
1678 | 1678 | s.update(text) |
|
1679 | 1679 | return s.hexdigest() |
|
1680 | 1680 | |
|
1681 | 1681 | def diffline(a, b, revs): |
|
1682 | 1682 | if opts.git: |
|
1683 | 1683 | line = 'diff --git a/%s b/%s\n' % (a, b) |
|
1684 | 1684 | elif not repo.ui.quiet: |
|
1685 | 1685 | if revs: |
|
1686 | 1686 | revinfo = ' '.join(["-r %s" % rev for rev in revs]) |
|
1687 | 1687 | line = 'diff %s %s\n' % (revinfo, a) |
|
1688 | 1688 | else: |
|
1689 | 1689 | line = 'diff %s\n' % a |
|
1690 | 1690 | else: |
|
1691 | 1691 | line = '' |
|
1692 | 1692 | return line |
|
1693 | 1693 | |
|
1694 | 1694 | date1 = util.datestr(ctx1.date()) |
|
1695 | 1695 | man1 = ctx1.manifest() |
|
1696 | 1696 | |
|
1697 | 1697 | gone = set() |
|
1698 | 1698 | gitmode = {'l': '120000', 'x': '100755', '': '100644'} |
|
1699 | 1699 | |
|
1700 | 1700 | copyto = dict([(v, k) for k, v in copy.items()]) |
|
1701 | 1701 | |
|
1702 | 1702 | if opts.git: |
|
1703 | 1703 | revs = None |
|
1704 | 1704 | |
|
1705 | 1705 | for f in sorted(modified + added + removed): |
|
1706 | 1706 | to = None |
|
1707 | 1707 | tn = None |
|
1708 | 1708 | dodiff = True |
|
1709 | 1709 | header = [] |
|
1710 | 1710 | if f in man1: |
|
1711 | 1711 | to = getfilectx(f, ctx1).data() |
|
1712 | 1712 | if f not in removed: |
|
1713 | 1713 | tn = getfilectx(f, ctx2).data() |
|
1714 | 1714 | a, b = f, f |
|
1715 | 1715 | if opts.git or losedatafn: |
|
1716 | 1716 | if f in added: |
|
1717 | 1717 | mode = gitmode[ctx2.flags(f)] |
|
1718 | 1718 | if f in copy or f in copyto: |
|
1719 | 1719 | if opts.git: |
|
1720 | 1720 | if f in copy: |
|
1721 | 1721 | a = copy[f] |
|
1722 | 1722 | else: |
|
1723 | 1723 | a = copyto[f] |
|
1724 | 1724 | omode = gitmode[man1.flags(a)] |
|
1725 | 1725 | addmodehdr(header, omode, mode) |
|
1726 | 1726 | if a in removed and a not in gone: |
|
1727 | 1727 | op = 'rename' |
|
1728 | 1728 | gone.add(a) |
|
1729 | 1729 | else: |
|
1730 | 1730 | op = 'copy' |
|
1731 | 1731 | header.append('%s from %s\n' % (op, join(a))) |
|
1732 | 1732 | header.append('%s to %s\n' % (op, join(f))) |
|
1733 | 1733 | to = getfilectx(a, ctx1).data() |
|
1734 | 1734 | else: |
|
1735 | 1735 | losedatafn(f) |
|
1736 | 1736 | else: |
|
1737 | 1737 | if opts.git: |
|
1738 | 1738 | header.append('new file mode %s\n' % mode) |
|
1739 | 1739 | elif ctx2.flags(f): |
|
1740 | 1740 | losedatafn(f) |
|
1741 | 1741 | # In theory, if tn was copied or renamed we should check |
|
1742 | 1742 | # if the source is binary too but the copy record already |
|
1743 | 1743 | # forces git mode. |
|
1744 | 1744 | if util.binary(tn): |
|
1745 | 1745 | if opts.git: |
|
1746 | 1746 | dodiff = 'binary' |
|
1747 | 1747 | else: |
|
1748 | 1748 | losedatafn(f) |
|
1749 | 1749 | if not opts.git and not tn: |
|
1750 | 1750 | # regular diffs cannot represent new empty file |
|
1751 | 1751 | losedatafn(f) |
|
1752 | 1752 | elif f in removed: |
|
1753 | 1753 | if opts.git: |
|
1754 | 1754 | # have we already reported a copy above? |
|
1755 | 1755 | if ((f in copy and copy[f] in added |
|
1756 | 1756 | and copyto[copy[f]] == f) or |
|
1757 | 1757 | (f in copyto and copyto[f] in added |
|
1758 | 1758 | and copy[copyto[f]] == f)): |
|
1759 | 1759 | dodiff = False |
|
1760 | 1760 | else: |
|
1761 | 1761 | header.append('deleted file mode %s\n' % |
|
1762 | 1762 | gitmode[man1.flags(f)]) |
|
1763 | 1763 | elif not to or util.binary(to): |
|
1764 | 1764 | # regular diffs cannot represent empty file deletion |
|
1765 | 1765 | losedatafn(f) |
|
1766 | 1766 | else: |
|
1767 | 1767 | oflag = man1.flags(f) |
|
1768 | 1768 | nflag = ctx2.flags(f) |
|
1769 | 1769 | binary = util.binary(to) or util.binary(tn) |
|
1770 | 1770 | if opts.git: |
|
1771 | 1771 | addmodehdr(header, gitmode[oflag], gitmode[nflag]) |
|
1772 | 1772 | if binary: |
|
1773 | 1773 | dodiff = 'binary' |
|
1774 | 1774 | elif binary or nflag != oflag: |
|
1775 | 1775 | losedatafn(f) |
|
1776 | 1776 | |
|
1777 | 1777 | if dodiff: |
|
1778 | 1778 | if opts.git or revs: |
|
1779 | 1779 | header.insert(0, diffline(join(a), join(b), revs)) |
|
1780 | 1780 | if dodiff == 'binary': |
|
1781 | 1781 | text = mdiff.b85diff(to, tn) |
|
1782 | 1782 | if text: |
|
1783 | 1783 | addindexmeta(header, [gitindex(to), gitindex(tn)]) |
|
1784 | 1784 | else: |
|
1785 | 1785 | text = mdiff.unidiff(to, date1, |
|
1786 | 1786 | # ctx2 date may be dynamic |
|
1787 | 1787 | tn, util.datestr(ctx2.date()), |
|
1788 | 1788 | join(a), join(b), opts=opts) |
|
1789 | 1789 | if header and (text or len(header) > 1): |
|
1790 | 1790 | yield ''.join(header) |
|
1791 | 1791 | if text: |
|
1792 | 1792 | yield text |
|
1793 | 1793 | |
|
1794 | 1794 | def diffstatsum(stats): |
|
1795 | 1795 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False |
|
1796 | 1796 | for f, a, r, b in stats: |
|
1797 | 1797 | maxfile = max(maxfile, encoding.colwidth(f)) |
|
1798 | 1798 | maxtotal = max(maxtotal, a + r) |
|
1799 | 1799 | addtotal += a |
|
1800 | 1800 | removetotal += r |
|
1801 | 1801 | binary = binary or b |
|
1802 | 1802 | |
|
1803 | 1803 | return maxfile, maxtotal, addtotal, removetotal, binary |
|
1804 | 1804 | |
|
1805 | 1805 | def diffstatdata(lines): |
|
1806 | 1806 | diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$') |
|
1807 | 1807 | |
|
1808 | 1808 | results = [] |
|
1809 | 1809 | filename, adds, removes, isbinary = None, 0, 0, False |
|
1810 | 1810 | |
|
1811 | 1811 | def addresult(): |
|
1812 | 1812 | if filename: |
|
1813 | 1813 | results.append((filename, adds, removes, isbinary)) |
|
1814 | 1814 | |
|
1815 | 1815 | for line in lines: |
|
1816 | 1816 | if line.startswith('diff'): |
|
1817 | 1817 | addresult() |
|
1818 | 1818 | # set numbers to 0 anyway when starting new file |
|
1819 | 1819 | adds, removes, isbinary = 0, 0, False |
|
1820 | 1820 | if line.startswith('diff --git'): |
|
1821 | 1821 | filename = gitre.search(line).group(1) |
|
1822 | 1822 | elif line.startswith('diff -r'): |
|
1823 | 1823 | # format: "diff -r ... -r ... filename" |
|
1824 | 1824 | filename = diffre.search(line).group(1) |
|
1825 | 1825 | elif line.startswith('+') and not line.startswith('+++ '): |
|
1826 | 1826 | adds += 1 |
|
1827 | 1827 | elif line.startswith('-') and not line.startswith('--- '): |
|
1828 | 1828 | removes += 1 |
|
1829 | 1829 | elif (line.startswith('GIT binary patch') or |
|
1830 | 1830 | line.startswith('Binary file')): |
|
1831 | 1831 | isbinary = True |
|
1832 | 1832 | addresult() |
|
1833 | 1833 | return results |
|
1834 | 1834 | |
|
1835 | 1835 | def diffstat(lines, width=80, git=False): |
|
1836 | 1836 | output = [] |
|
1837 | 1837 | stats = diffstatdata(lines) |
|
1838 | 1838 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) |
|
1839 | 1839 | |
|
1840 | 1840 | countwidth = len(str(maxtotal)) |
|
1841 | 1841 | if hasbinary and countwidth < 3: |
|
1842 | 1842 | countwidth = 3 |
|
1843 | 1843 | graphwidth = width - countwidth - maxname - 6 |
|
1844 | 1844 | if graphwidth < 10: |
|
1845 | 1845 | graphwidth = 10 |
|
1846 | 1846 | |
|
1847 | 1847 | def scale(i): |
|
1848 | 1848 | if maxtotal <= graphwidth: |
|
1849 | 1849 | return i |
|
1850 | 1850 | # If diffstat runs out of room it doesn't print anything, |
|
1851 | 1851 | # which isn't very useful, so always print at least one + or - |
|
1852 | 1852 | # if there were at least some changes. |
|
1853 | 1853 | return max(i * graphwidth // maxtotal, int(bool(i))) |
|
1854 | 1854 | |
|
1855 | 1855 | for filename, adds, removes, isbinary in stats: |
|
1856 | 1856 | if isbinary: |
|
1857 | 1857 | count = 'Bin' |
|
1858 | 1858 | else: |
|
1859 | 1859 | count = adds + removes |
|
1860 | 1860 | pluses = '+' * scale(adds) |
|
1861 | 1861 | minuses = '-' * scale(removes) |
|
1862 | 1862 | output.append(' %s%s | %*s %s%s\n' % |
|
1863 | 1863 | (filename, ' ' * (maxname - encoding.colwidth(filename)), |
|
1864 | 1864 | countwidth, count, pluses, minuses)) |
|
1865 | 1865 | |
|
1866 | 1866 | if stats: |
|
1867 | 1867 | output.append(_(' %d files changed, %d insertions(+), ' |
|
1868 | 1868 | '%d deletions(-)\n') |
|
1869 | 1869 | % (len(stats), totaladds, totalremoves)) |
|
1870 | 1870 | |
|
1871 | 1871 | return ''.join(output) |
|
1872 | 1872 | |
|
1873 | 1873 | def diffstatui(*args, **kw): |
|
1874 | 1874 | '''like diffstat(), but yields 2-tuples of (output, label) for |
|
1875 | 1875 | ui.write() |
|
1876 | 1876 | ''' |
|
1877 | 1877 | |
|
1878 | 1878 | for line in diffstat(*args, **kw).splitlines(): |
|
1879 | 1879 | if line and line[-1] in '+-': |
|
1880 | 1880 | name, graph = line.rsplit(' ', 1) |
|
1881 | 1881 | yield (name + ' ', '') |
|
1882 | 1882 | m = re.search(r'\++', graph) |
|
1883 | 1883 | if m: |
|
1884 | 1884 | yield (m.group(0), 'diffstat.inserted') |
|
1885 | 1885 | m = re.search(r'-+', graph) |
|
1886 | 1886 | if m: |
|
1887 | 1887 | yield (m.group(0), 'diffstat.deleted') |
|
1888 | 1888 | else: |
|
1889 | 1889 | yield (line, '') |
|
1890 | 1890 | yield ('\n', '') |
@@ -1,1281 +1,1281 b'' | |||
|
1 | 1 | # subrepo.py - sub-repository handling for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2009-2010 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import errno, os, re, xml.dom.minidom, shutil, posixpath |
|
9 | 9 | import stat, subprocess, tarfile |
|
10 | 10 | from i18n import _ |
|
11 | 11 | import config, scmutil, util, node, error, cmdutil, bookmarks, match as matchmod |
|
12 | 12 | hg = None |
|
13 | 13 | propertycache = util.propertycache |
|
14 | 14 | |
|
15 | 15 | nullstate = ('', '', 'empty') |
|
16 | 16 | |
|
17 | 17 | def state(ctx, ui): |
|
18 | 18 | """return a state dict, mapping subrepo paths configured in .hgsub |
|
19 | 19 | to tuple: (source from .hgsub, revision from .hgsubstate, kind |
|
20 | 20 | (key in types dict)) |
|
21 | 21 | """ |
|
22 | 22 | p = config.config() |
|
23 | 23 | def read(f, sections=None, remap=None): |
|
24 | 24 | if f in ctx: |
|
25 | 25 | try: |
|
26 | 26 | data = ctx[f].data() |
|
27 | 27 | except IOError, err: |
|
28 | 28 | if err.errno != errno.ENOENT: |
|
29 | 29 | raise |
|
30 | 30 | # handle missing subrepo spec files as removed |
|
31 | 31 | ui.warn(_("warning: subrepo spec file %s not found\n") % f) |
|
32 | 32 | return |
|
33 | 33 | p.parse(f, data, sections, remap, read) |
|
34 | 34 | else: |
|
35 | 35 | raise util.Abort(_("subrepo spec file %s not found") % f) |
|
36 | 36 | |
|
37 | 37 | if '.hgsub' in ctx: |
|
38 | 38 | read('.hgsub') |
|
39 | 39 | |
|
40 | 40 | for path, src in ui.configitems('subpaths'): |
|
41 | 41 | p.set('subpaths', path, src, ui.configsource('subpaths', path)) |
|
42 | 42 | |
|
43 | 43 | rev = {} |
|
44 | 44 | if '.hgsubstate' in ctx: |
|
45 | 45 | try: |
|
46 | 46 | for i, l in enumerate(ctx['.hgsubstate'].data().splitlines()): |
|
47 | 47 | l = l.lstrip() |
|
48 | 48 | if not l: |
|
49 | 49 | continue |
|
50 | 50 | try: |
|
51 | 51 | revision, path = l.split(" ", 1) |
|
52 | 52 | except ValueError: |
|
53 | 53 | raise util.Abort(_("invalid subrepository revision " |
|
54 | 54 | "specifier in .hgsubstate line %d") |
|
55 | 55 | % (i + 1)) |
|
56 | 56 | rev[path] = revision |
|
57 | 57 | except IOError, err: |
|
58 | 58 | if err.errno != errno.ENOENT: |
|
59 | 59 | raise |
|
60 | 60 | |
|
61 | 61 | def remap(src): |
|
62 | 62 | for pattern, repl in p.items('subpaths'): |
|
63 | 63 | # Turn r'C:\foo\bar' into r'C:\\foo\\bar' since re.sub |
|
64 | 64 | # does a string decode. |
|
65 | 65 | repl = repl.encode('string-escape') |
|
66 | 66 | # However, we still want to allow back references to go |
|
67 | 67 | # through unharmed, so we turn r'\\1' into r'\1'. Again, |
|
68 | 68 | # extra escapes are needed because re.sub string decodes. |
|
69 | 69 | repl = re.sub(r'\\\\([0-9]+)', r'\\\1', repl) |
|
70 | 70 | try: |
|
71 | 71 | src = re.sub(pattern, repl, src, 1) |
|
72 | 72 | except re.error, e: |
|
73 | 73 | raise util.Abort(_("bad subrepository pattern in %s: %s") |
|
74 | 74 | % (p.source('subpaths', pattern), e)) |
|
75 | 75 | return src |
|
76 | 76 | |
|
77 | 77 | state = {} |
|
78 | 78 | for path, src in p[''].items(): |
|
79 | 79 | kind = 'hg' |
|
80 | 80 | if src.startswith('['): |
|
81 | 81 | if ']' not in src: |
|
82 | 82 | raise util.Abort(_('missing ] in subrepo source')) |
|
83 | 83 | kind, src = src.split(']', 1) |
|
84 | 84 | kind = kind[1:] |
|
85 | 85 | src = src.lstrip() # strip any extra whitespace after ']' |
|
86 | 86 | |
|
87 | 87 | if not util.url(src).isabs(): |
|
88 | 88 | parent = _abssource(ctx._repo, abort=False) |
|
89 | 89 | if parent: |
|
90 | 90 | parent = util.url(parent) |
|
91 | 91 | parent.path = posixpath.join(parent.path or '', src) |
|
92 | 92 | parent.path = posixpath.normpath(parent.path) |
|
93 | 93 | joined = str(parent) |
|
94 | 94 | # Remap the full joined path and use it if it changes, |
|
95 | 95 | # else remap the original source. |
|
96 | 96 | remapped = remap(joined) |
|
97 | 97 | if remapped == joined: |
|
98 | 98 | src = remap(src) |
|
99 | 99 | else: |
|
100 | 100 | src = remapped |
|
101 | 101 | |
|
102 | 102 | src = remap(src) |
|
103 | 103 | state[util.pconvert(path)] = (src.strip(), rev.get(path, ''), kind) |
|
104 | 104 | |
|
105 | 105 | return state |
|
106 | 106 | |
|
107 | 107 | def writestate(repo, state): |
|
108 | 108 | """rewrite .hgsubstate in (outer) repo with these subrepo states""" |
|
109 | 109 | lines = ['%s %s\n' % (state[s][1], s) for s in sorted(state)] |
|
110 | 110 | repo.wwrite('.hgsubstate', ''.join(lines), '') |
|
111 | 111 | |
|
112 | 112 | def submerge(repo, wctx, mctx, actx, overwrite): |
|
113 | 113 | """delegated from merge.applyupdates: merging of .hgsubstate file |
|
114 | 114 | in working context, merging context and ancestor context""" |
|
115 | 115 | if mctx == actx: # backwards? |
|
116 | 116 | actx = wctx.p1() |
|
117 | 117 | s1 = wctx.substate |
|
118 | 118 | s2 = mctx.substate |
|
119 | 119 | sa = actx.substate |
|
120 | 120 | sm = {} |
|
121 | 121 | |
|
122 | 122 | repo.ui.debug("subrepo merge %s %s %s\n" % (wctx, mctx, actx)) |
|
123 | 123 | |
|
124 | 124 | def debug(s, msg, r=""): |
|
125 | 125 | if r: |
|
126 | 126 | r = "%s:%s:%s" % r |
|
127 | 127 | repo.ui.debug(" subrepo %s: %s %s\n" % (s, msg, r)) |
|
128 | 128 | |
|
129 | 129 | for s, l in s1.items(): |
|
130 | 130 | a = sa.get(s, nullstate) |
|
131 | 131 | ld = l # local state with possible dirty flag for compares |
|
132 | 132 | if wctx.sub(s).dirty(): |
|
133 | 133 | ld = (l[0], l[1] + "+") |
|
134 | 134 | if wctx == actx: # overwrite |
|
135 | 135 | a = ld |
|
136 | 136 | |
|
137 | 137 | if s in s2: |
|
138 | 138 | r = s2[s] |
|
139 | 139 | if ld == r or r == a: # no change or local is newer |
|
140 | 140 | sm[s] = l |
|
141 | 141 | continue |
|
142 | 142 | elif ld == a: # other side changed |
|
143 | 143 | debug(s, "other changed, get", r) |
|
144 | 144 | wctx.sub(s).get(r, overwrite) |
|
145 | 145 | sm[s] = r |
|
146 | 146 | elif ld[0] != r[0]: # sources differ |
|
147 | 147 | if repo.ui.promptchoice( |
|
148 | 148 | _(' subrepository sources for %s differ\n' |
|
149 | 149 | 'use (l)ocal source (%s) or (r)emote source (%s)?') |
|
150 | 150 | % (s, l[0], r[0]), |
|
151 | 151 | (_('&Local'), _('&Remote')), 0): |
|
152 | 152 | debug(s, "prompt changed, get", r) |
|
153 | 153 | wctx.sub(s).get(r, overwrite) |
|
154 | 154 | sm[s] = r |
|
155 | 155 | elif ld[1] == a[1]: # local side is unchanged |
|
156 | 156 | debug(s, "other side changed, get", r) |
|
157 | 157 | wctx.sub(s).get(r, overwrite) |
|
158 | 158 | sm[s] = r |
|
159 | 159 | else: |
|
160 | 160 | debug(s, "both sides changed, merge with", r) |
|
161 | 161 | wctx.sub(s).merge(r) |
|
162 | 162 | sm[s] = l |
|
163 | 163 | elif ld == a: # remote removed, local unchanged |
|
164 | 164 | debug(s, "remote removed, remove") |
|
165 | 165 | wctx.sub(s).remove() |
|
166 | 166 | elif a == nullstate: # not present in remote or ancestor |
|
167 | 167 | debug(s, "local added, keep") |
|
168 | 168 | sm[s] = l |
|
169 | 169 | continue |
|
170 | 170 | else: |
|
171 | 171 | if repo.ui.promptchoice( |
|
172 | 172 | _(' local changed subrepository %s which remote removed\n' |
|
173 | 173 | 'use (c)hanged version or (d)elete?') % s, |
|
174 | 174 | (_('&Changed'), _('&Delete')), 0): |
|
175 | 175 | debug(s, "prompt remove") |
|
176 | 176 | wctx.sub(s).remove() |
|
177 | 177 | |
|
178 | 178 | for s, r in sorted(s2.items()): |
|
179 | 179 | if s in s1: |
|
180 | 180 | continue |
|
181 | 181 | elif s not in sa: |
|
182 | 182 | debug(s, "remote added, get", r) |
|
183 | 183 | mctx.sub(s).get(r) |
|
184 | 184 | sm[s] = r |
|
185 | 185 | elif r != sa[s]: |
|
186 | 186 | if repo.ui.promptchoice( |
|
187 | 187 | _(' remote changed subrepository %s which local removed\n' |
|
188 | 188 | 'use (c)hanged version or (d)elete?') % s, |
|
189 | 189 | (_('&Changed'), _('&Delete')), 0) == 0: |
|
190 | 190 | debug(s, "prompt recreate", r) |
|
191 | 191 | wctx.sub(s).get(r) |
|
192 | 192 | sm[s] = r |
|
193 | 193 | |
|
194 | 194 | # record merged .hgsubstate |
|
195 | 195 | writestate(repo, sm) |
|
196 | 196 | |
|
197 | 197 | def _updateprompt(ui, sub, dirty, local, remote): |
|
198 | 198 | if dirty: |
|
199 | 199 | msg = (_(' subrepository sources for %s differ\n' |
|
200 | 200 | 'use (l)ocal source (%s) or (r)emote source (%s)?\n') |
|
201 | 201 | % (subrelpath(sub), local, remote)) |
|
202 | 202 | else: |
|
203 | 203 | msg = (_(' subrepository sources for %s differ (in checked out ' |
|
204 | 204 | 'version)\n' |
|
205 | 205 | 'use (l)ocal source (%s) or (r)emote source (%s)?\n') |
|
206 | 206 | % (subrelpath(sub), local, remote)) |
|
207 | 207 | return ui.promptchoice(msg, (_('&Local'), _('&Remote')), 0) |
|
208 | 208 | |
|
209 | 209 | def reporelpath(repo): |
|
210 | 210 | """return path to this (sub)repo as seen from outermost repo""" |
|
211 | 211 | parent = repo |
|
212 | 212 | while util.safehasattr(parent, '_subparent'): |
|
213 | 213 | parent = parent._subparent |
|
214 | 214 | p = parent.root.rstrip(os.sep) |
|
215 | 215 | return repo.root[len(p) + 1:] |
|
216 | 216 | |
|
217 | 217 | def subrelpath(sub): |
|
218 | 218 | """return path to this subrepo as seen from outermost repo""" |
|
219 | 219 | if util.safehasattr(sub, '_relpath'): |
|
220 | 220 | return sub._relpath |
|
221 | 221 | if not util.safehasattr(sub, '_repo'): |
|
222 | 222 | return sub._path |
|
223 | 223 | return reporelpath(sub._repo) |
|
224 | 224 | |
|
225 | 225 | def _abssource(repo, push=False, abort=True): |
|
226 | 226 | """return pull/push path of repo - either based on parent repo .hgsub info |
|
227 | 227 | or on the top repo config. Abort or return None if no source found.""" |
|
228 | 228 | if util.safehasattr(repo, '_subparent'): |
|
229 | 229 | source = util.url(repo._subsource) |
|
230 | 230 | if source.isabs(): |
|
231 | 231 | return str(source) |
|
232 | 232 | source.path = posixpath.normpath(source.path) |
|
233 | 233 | parent = _abssource(repo._subparent, push, abort=False) |
|
234 | 234 | if parent: |
|
235 | 235 | parent = util.url(util.pconvert(parent)) |
|
236 | 236 | parent.path = posixpath.join(parent.path or '', source.path) |
|
237 | 237 | parent.path = posixpath.normpath(parent.path) |
|
238 | 238 | return str(parent) |
|
239 | 239 | else: # recursion reached top repo |
|
240 | 240 | if util.safehasattr(repo, '_subtoppath'): |
|
241 | 241 | return repo._subtoppath |
|
242 | 242 | if push and repo.ui.config('paths', 'default-push'): |
|
243 | 243 | return repo.ui.config('paths', 'default-push') |
|
244 | 244 | if repo.ui.config('paths', 'default'): |
|
245 | 245 | return repo.ui.config('paths', 'default') |
|
246 | 246 | if abort: |
|
247 | 247 | raise util.Abort(_("default path for subrepository %s not found") % |
|
248 | 248 | reporelpath(repo)) |
|
249 | 249 | |
|
250 | 250 | def itersubrepos(ctx1, ctx2): |
|
251 | 251 | """find subrepos in ctx1 or ctx2""" |
|
252 | 252 | # Create a (subpath, ctx) mapping where we prefer subpaths from |
|
253 | 253 | # ctx1. The subpaths from ctx2 are important when the .hgsub file |
|
254 | 254 | # has been modified (in ctx2) but not yet committed (in ctx1). |
|
255 | 255 | subpaths = dict.fromkeys(ctx2.substate, ctx2) |
|
256 | 256 | subpaths.update(dict.fromkeys(ctx1.substate, ctx1)) |
|
257 | 257 | for subpath, ctx in sorted(subpaths.iteritems()): |
|
258 | 258 | yield subpath, ctx.sub(subpath) |
|
259 | 259 | |
|
260 | 260 | def subrepo(ctx, path): |
|
261 | 261 | """return instance of the right subrepo class for subrepo in path""" |
|
262 | 262 | # subrepo inherently violates our import layering rules |
|
263 | 263 | # because it wants to make repo objects from deep inside the stack |
|
264 | 264 | # so we manually delay the circular imports to not break |
|
265 | 265 | # scripts that don't use our demand-loading |
|
266 | 266 | global hg |
|
267 | 267 | import hg as h |
|
268 | 268 | hg = h |
|
269 | 269 | |
|
270 | 270 | scmutil.pathauditor(ctx._repo.root)(path) |
|
271 | 271 | state = ctx.substate[path] |
|
272 | 272 | if state[2] not in types: |
|
273 | 273 | raise util.Abort(_('unknown subrepo type %s') % state[2]) |
|
274 | 274 | return types[state[2]](ctx, path, state[:2]) |
|
275 | 275 | |
|
276 | 276 | # subrepo classes need to implement the following abstract class: |
|
277 | 277 | |
|
278 | 278 | class abstractsubrepo(object): |
|
279 | 279 | |
|
280 | 280 | def dirty(self, ignoreupdate=False): |
|
281 | 281 | """returns true if the dirstate of the subrepo is dirty or does not |
|
282 | 282 | match current stored state. If ignoreupdate is true, only check |
|
283 | 283 | whether the subrepo has uncommitted changes in its dirstate. |
|
284 | 284 | """ |
|
285 | 285 | raise NotImplementedError |
|
286 | 286 | |
|
287 | 287 | def basestate(self): |
|
288 | 288 | """current working directory base state, disregarding .hgsubstate |
|
289 | 289 | state and working directory modifications""" |
|
290 | 290 | raise NotImplementedError |
|
291 | 291 | |
|
292 | 292 | def checknested(self, path): |
|
293 | 293 | """check if path is a subrepository within this repository""" |
|
294 | 294 | return False |
|
295 | 295 | |
|
296 | 296 | def commit(self, text, user, date): |
|
297 | 297 | """commit the current changes to the subrepo with the given |
|
298 | 298 | log message. Use given user and date if possible. Return the |
|
299 | 299 | new state of the subrepo. |
|
300 | 300 | """ |
|
301 | 301 | raise NotImplementedError |
|
302 | 302 | |
|
303 | 303 | def remove(self): |
|
304 | 304 | """remove the subrepo |
|
305 | 305 | |
|
306 | 306 | (should verify the dirstate is not dirty first) |
|
307 | 307 | """ |
|
308 | 308 | raise NotImplementedError |
|
309 | 309 | |
|
310 | 310 | def get(self, state, overwrite=False): |
|
311 | 311 | """run whatever commands are needed to put the subrepo into |
|
312 | 312 | this state |
|
313 | 313 | """ |
|
314 | 314 | raise NotImplementedError |
|
315 | 315 | |
|
316 | 316 | def merge(self, state): |
|
317 | 317 | """merge currently-saved state with the new state.""" |
|
318 | 318 | raise NotImplementedError |
|
319 | 319 | |
|
320 | 320 | def push(self, opts): |
|
321 | 321 | """perform whatever action is analogous to 'hg push' |
|
322 | 322 | |
|
323 | 323 | This may be a no-op on some systems. |
|
324 | 324 | """ |
|
325 | 325 | raise NotImplementedError |
|
326 | 326 | |
|
327 | 327 | def add(self, ui, match, dryrun, listsubrepos, prefix, explicitonly): |
|
328 | 328 | return [] |
|
329 | 329 | |
|
330 | 330 | def status(self, rev2, **opts): |
|
331 | 331 | return [], [], [], [], [], [], [] |
|
332 | 332 | |
|
333 | 333 | def diff(self, diffopts, node2, match, prefix, **opts): |
|
334 | 334 | pass |
|
335 | 335 | |
|
336 | 336 | def outgoing(self, ui, dest, opts): |
|
337 | 337 | return 1 |
|
338 | 338 | |
|
339 | 339 | def incoming(self, ui, source, opts): |
|
340 | 340 | return 1 |
|
341 | 341 | |
|
342 | 342 | def files(self): |
|
343 | 343 | """return filename iterator""" |
|
344 | 344 | raise NotImplementedError |
|
345 | 345 | |
|
346 | 346 | def filedata(self, name): |
|
347 | 347 | """return file data""" |
|
348 | 348 | raise NotImplementedError |
|
349 | 349 | |
|
350 | 350 | def fileflags(self, name): |
|
351 | 351 | """return file flags""" |
|
352 | 352 | return '' |
|
353 | 353 | |
|
354 | 354 | def archive(self, ui, archiver, prefix, match=None): |
|
355 | 355 | if match is not None: |
|
356 | 356 | files = [f for f in self.files() if match(f)] |
|
357 | 357 | else: |
|
358 | 358 | files = self.files() |
|
359 | 359 | total = len(files) |
|
360 | 360 | relpath = subrelpath(self) |
|
361 | 361 | ui.progress(_('archiving (%s)') % relpath, 0, |
|
362 | 362 | unit=_('files'), total=total) |
|
363 | 363 | for i, name in enumerate(files): |
|
364 | 364 | flags = self.fileflags(name) |
|
365 | 365 | mode = 'x' in flags and 0755 or 0644 |
|
366 | 366 | symlink = 'l' in flags |
|
367 | 367 | archiver.addfile(os.path.join(prefix, self._path, name), |
|
368 | 368 | mode, symlink, self.filedata(name)) |
|
369 | 369 | ui.progress(_('archiving (%s)') % relpath, i + 1, |
|
370 | 370 | unit=_('files'), total=total) |
|
371 | 371 | ui.progress(_('archiving (%s)') % relpath, None) |
|
372 | 372 | |
|
373 | 373 | def walk(self, match): |
|
374 | 374 | ''' |
|
375 | 375 | walk recursively through the directory tree, finding all files |
|
376 | 376 | matched by the match function |
|
377 | 377 | ''' |
|
378 | 378 | pass |
|
379 | 379 | |
|
380 | 380 | def forget(self, ui, match, prefix): |
|
381 | 381 | return ([], []) |
|
382 | 382 | |
|
383 | 383 | def revert(self, ui, substate, *pats, **opts): |
|
384 | 384 | ui.warn('%s: reverting %s subrepos is unsupported\n' \ |
|
385 | 385 | % (substate[0], substate[2])) |
|
386 | 386 | return [] |
|
387 | 387 | |
|
388 | 388 | class hgsubrepo(abstractsubrepo): |
|
389 | 389 | def __init__(self, ctx, path, state): |
|
390 | 390 | self._path = path |
|
391 | 391 | self._state = state |
|
392 | 392 | r = ctx._repo |
|
393 | 393 | root = r.wjoin(path) |
|
394 | 394 | create = False |
|
395 | 395 | if not os.path.exists(os.path.join(root, '.hg')): |
|
396 | 396 | create = True |
|
397 | 397 | util.makedirs(root) |
|
398 | 398 | self._repo = hg.repository(r.baseui, root, create=create) |
|
399 | 399 | for s, k in [('ui', 'commitsubrepos')]: |
|
400 | 400 | v = r.ui.config(s, k) |
|
401 | 401 | if v: |
|
402 | 402 | self._repo.ui.setconfig(s, k, v) |
|
403 | 403 | self._initrepo(r, state[0], create) |
|
404 | 404 | |
|
405 | 405 | def _initrepo(self, parentrepo, source, create): |
|
406 | 406 | self._repo._subparent = parentrepo |
|
407 | 407 | self._repo._subsource = source |
|
408 | 408 | |
|
409 | 409 | if create: |
|
410 | 410 | fp = self._repo.opener("hgrc", "w", text=True) |
|
411 | 411 | fp.write('[paths]\n') |
|
412 | 412 | |
|
413 | 413 | def addpathconfig(key, value): |
|
414 | 414 | if value: |
|
415 | 415 | fp.write('%s = %s\n' % (key, value)) |
|
416 | 416 | self._repo.ui.setconfig('paths', key, value) |
|
417 | 417 | |
|
418 | 418 | defpath = _abssource(self._repo, abort=False) |
|
419 | 419 | defpushpath = _abssource(self._repo, True, abort=False) |
|
420 | 420 | addpathconfig('default', defpath) |
|
421 | 421 | if defpath != defpushpath: |
|
422 | 422 | addpathconfig('default-push', defpushpath) |
|
423 | 423 | fp.close() |
|
424 | 424 | |
|
425 | 425 | def add(self, ui, match, dryrun, listsubrepos, prefix, explicitonly): |
|
426 | 426 | return cmdutil.add(ui, self._repo, match, dryrun, listsubrepos, |
|
427 | 427 | os.path.join(prefix, self._path), explicitonly) |
|
428 | 428 | |
|
429 | 429 | def status(self, rev2, **opts): |
|
430 | 430 | try: |
|
431 | 431 | rev1 = self._state[1] |
|
432 | 432 | ctx1 = self._repo[rev1] |
|
433 | 433 | ctx2 = self._repo[rev2] |
|
434 | 434 | return self._repo.status(ctx1, ctx2, **opts) |
|
435 | 435 | except error.RepoLookupError, inst: |
|
436 | 436 | self._repo.ui.warn(_('warning: error "%s" in subrepository "%s"\n') |
|
437 | 437 | % (inst, subrelpath(self))) |
|
438 | 438 | return [], [], [], [], [], [], [] |
|
439 | 439 | |
|
440 | 440 | def diff(self, diffopts, node2, match, prefix, **opts): |
|
441 | 441 | try: |
|
442 | 442 | node1 = node.bin(self._state[1]) |
|
443 | 443 | # We currently expect node2 to come from substate and be |
|
444 | 444 | # in hex format |
|
445 | 445 | if node2 is not None: |
|
446 | 446 | node2 = node.bin(node2) |
|
447 | 447 | cmdutil.diffordiffstat(self._repo.ui, self._repo, diffopts, |
|
448 | 448 | node1, node2, match, |
|
449 |
prefix= |
|
|
449 | prefix=posixpath.join(prefix, self._path), | |
|
450 | 450 | listsubrepos=True, **opts) |
|
451 | 451 | except error.RepoLookupError, inst: |
|
452 | 452 | self._repo.ui.warn(_('warning: error "%s" in subrepository "%s"\n') |
|
453 | 453 | % (inst, subrelpath(self))) |
|
454 | 454 | |
|
455 | 455 | def archive(self, ui, archiver, prefix, match=None): |
|
456 | 456 | self._get(self._state + ('hg',)) |
|
457 | 457 | abstractsubrepo.archive(self, ui, archiver, prefix, match) |
|
458 | 458 | |
|
459 | 459 | rev = self._state[1] |
|
460 | 460 | ctx = self._repo[rev] |
|
461 | 461 | for subpath in ctx.substate: |
|
462 | 462 | s = subrepo(ctx, subpath) |
|
463 | 463 | submatch = matchmod.narrowmatcher(subpath, match) |
|
464 | 464 | s.archive(ui, archiver, os.path.join(prefix, self._path), submatch) |
|
465 | 465 | |
|
466 | 466 | def dirty(self, ignoreupdate=False): |
|
467 | 467 | r = self._state[1] |
|
468 | 468 | if r == '' and not ignoreupdate: # no state recorded |
|
469 | 469 | return True |
|
470 | 470 | w = self._repo[None] |
|
471 | 471 | if r != w.p1().hex() and not ignoreupdate: |
|
472 | 472 | # different version checked out |
|
473 | 473 | return True |
|
474 | 474 | return w.dirty() # working directory changed |
|
475 | 475 | |
|
476 | 476 | def basestate(self): |
|
477 | 477 | return self._repo['.'].hex() |
|
478 | 478 | |
|
479 | 479 | def checknested(self, path): |
|
480 | 480 | return self._repo._checknested(self._repo.wjoin(path)) |
|
481 | 481 | |
|
482 | 482 | def commit(self, text, user, date): |
|
483 | 483 | # don't bother committing in the subrepo if it's only been |
|
484 | 484 | # updated |
|
485 | 485 | if not self.dirty(True): |
|
486 | 486 | return self._repo['.'].hex() |
|
487 | 487 | self._repo.ui.debug("committing subrepo %s\n" % subrelpath(self)) |
|
488 | 488 | n = self._repo.commit(text, user, date) |
|
489 | 489 | if not n: |
|
490 | 490 | return self._repo['.'].hex() # different version checked out |
|
491 | 491 | return node.hex(n) |
|
492 | 492 | |
|
493 | 493 | def remove(self): |
|
494 | 494 | # we can't fully delete the repository as it may contain |
|
495 | 495 | # local-only history |
|
496 | 496 | self._repo.ui.note(_('removing subrepo %s\n') % subrelpath(self)) |
|
497 | 497 | hg.clean(self._repo, node.nullid, False) |
|
498 | 498 | |
|
499 | 499 | def _get(self, state): |
|
500 | 500 | source, revision, kind = state |
|
501 | 501 | if revision not in self._repo: |
|
502 | 502 | self._repo._subsource = source |
|
503 | 503 | srcurl = _abssource(self._repo) |
|
504 | 504 | other = hg.peer(self._repo, {}, srcurl) |
|
505 | 505 | if len(self._repo) == 0: |
|
506 | 506 | self._repo.ui.status(_('cloning subrepo %s from %s\n') |
|
507 | 507 | % (subrelpath(self), srcurl)) |
|
508 | 508 | parentrepo = self._repo._subparent |
|
509 | 509 | shutil.rmtree(self._repo.path) |
|
510 | 510 | other, cloned = hg.clone(self._repo._subparent.baseui, {}, |
|
511 | 511 | other, self._repo.root, |
|
512 | 512 | update=False) |
|
513 | 513 | self._repo = cloned.local() |
|
514 | 514 | self._initrepo(parentrepo, source, create=True) |
|
515 | 515 | else: |
|
516 | 516 | self._repo.ui.status(_('pulling subrepo %s from %s\n') |
|
517 | 517 | % (subrelpath(self), srcurl)) |
|
518 | 518 | self._repo.pull(other) |
|
519 | 519 | bookmarks.updatefromremote(self._repo.ui, self._repo, other, |
|
520 | 520 | srcurl) |
|
521 | 521 | |
|
522 | 522 | def get(self, state, overwrite=False): |
|
523 | 523 | self._get(state) |
|
524 | 524 | source, revision, kind = state |
|
525 | 525 | self._repo.ui.debug("getting subrepo %s\n" % self._path) |
|
526 | 526 | hg.updaterepo(self._repo, revision, overwrite) |
|
527 | 527 | |
|
528 | 528 | def merge(self, state): |
|
529 | 529 | self._get(state) |
|
530 | 530 | cur = self._repo['.'] |
|
531 | 531 | dst = self._repo[state[1]] |
|
532 | 532 | anc = dst.ancestor(cur) |
|
533 | 533 | |
|
534 | 534 | def mergefunc(): |
|
535 | 535 | if anc == cur and dst.branch() == cur.branch(): |
|
536 | 536 | self._repo.ui.debug("updating subrepo %s\n" % subrelpath(self)) |
|
537 | 537 | hg.update(self._repo, state[1]) |
|
538 | 538 | elif anc == dst: |
|
539 | 539 | self._repo.ui.debug("skipping subrepo %s\n" % subrelpath(self)) |
|
540 | 540 | else: |
|
541 | 541 | self._repo.ui.debug("merging subrepo %s\n" % subrelpath(self)) |
|
542 | 542 | hg.merge(self._repo, state[1], remind=False) |
|
543 | 543 | |
|
544 | 544 | wctx = self._repo[None] |
|
545 | 545 | if self.dirty(): |
|
546 | 546 | if anc != dst: |
|
547 | 547 | if _updateprompt(self._repo.ui, self, wctx.dirty(), cur, dst): |
|
548 | 548 | mergefunc() |
|
549 | 549 | else: |
|
550 | 550 | mergefunc() |
|
551 | 551 | else: |
|
552 | 552 | mergefunc() |
|
553 | 553 | |
|
554 | 554 | def push(self, opts): |
|
555 | 555 | force = opts.get('force') |
|
556 | 556 | newbranch = opts.get('new_branch') |
|
557 | 557 | ssh = opts.get('ssh') |
|
558 | 558 | |
|
559 | 559 | # push subrepos depth-first for coherent ordering |
|
560 | 560 | c = self._repo[''] |
|
561 | 561 | subs = c.substate # only repos that are committed |
|
562 | 562 | for s in sorted(subs): |
|
563 | 563 | if c.sub(s).push(opts) == 0: |
|
564 | 564 | return False |
|
565 | 565 | |
|
566 | 566 | dsturl = _abssource(self._repo, True) |
|
567 | 567 | self._repo.ui.status(_('pushing subrepo %s to %s\n') % |
|
568 | 568 | (subrelpath(self), dsturl)) |
|
569 | 569 | other = hg.peer(self._repo, {'ssh': ssh}, dsturl) |
|
570 | 570 | return self._repo.push(other, force, newbranch=newbranch) |
|
571 | 571 | |
|
572 | 572 | def outgoing(self, ui, dest, opts): |
|
573 | 573 | return hg.outgoing(ui, self._repo, _abssource(self._repo, True), opts) |
|
574 | 574 | |
|
575 | 575 | def incoming(self, ui, source, opts): |
|
576 | 576 | return hg.incoming(ui, self._repo, _abssource(self._repo, False), opts) |
|
577 | 577 | |
|
578 | 578 | def files(self): |
|
579 | 579 | rev = self._state[1] |
|
580 | 580 | ctx = self._repo[rev] |
|
581 | 581 | return ctx.manifest() |
|
582 | 582 | |
|
583 | 583 | def filedata(self, name): |
|
584 | 584 | rev = self._state[1] |
|
585 | 585 | return self._repo[rev][name].data() |
|
586 | 586 | |
|
587 | 587 | def fileflags(self, name): |
|
588 | 588 | rev = self._state[1] |
|
589 | 589 | ctx = self._repo[rev] |
|
590 | 590 | return ctx.flags(name) |
|
591 | 591 | |
|
592 | 592 | def walk(self, match): |
|
593 | 593 | ctx = self._repo[None] |
|
594 | 594 | return ctx.walk(match) |
|
595 | 595 | |
|
596 | 596 | def forget(self, ui, match, prefix): |
|
597 | 597 | return cmdutil.forget(ui, self._repo, match, |
|
598 | 598 | os.path.join(prefix, self._path), True) |
|
599 | 599 | |
|
600 | 600 | def revert(self, ui, substate, *pats, **opts): |
|
601 | 601 | # reverting a subrepo is a 2 step process: |
|
602 | 602 | # 1. if the no_backup is not set, revert all modified |
|
603 | 603 | # files inside the subrepo |
|
604 | 604 | # 2. update the subrepo to the revision specified in |
|
605 | 605 | # the corresponding substate dictionary |
|
606 | 606 | ui.status(_('reverting subrepo %s\n') % substate[0]) |
|
607 | 607 | if not opts.get('no_backup'): |
|
608 | 608 | # Revert all files on the subrepo, creating backups |
|
609 | 609 | # Note that this will not recursively revert subrepos |
|
610 | 610 | # We could do it if there was a set:subrepos() predicate |
|
611 | 611 | opts = opts.copy() |
|
612 | 612 | opts['date'] = None |
|
613 | 613 | opts['rev'] = substate[1] |
|
614 | 614 | |
|
615 | 615 | pats = [] |
|
616 | 616 | if not opts['all']: |
|
617 | 617 | pats = ['set:modified()'] |
|
618 | 618 | self.filerevert(ui, *pats, **opts) |
|
619 | 619 | |
|
620 | 620 | # Update the repo to the revision specified in the given substate |
|
621 | 621 | self.get(substate, overwrite=True) |
|
622 | 622 | |
|
623 | 623 | def filerevert(self, ui, *pats, **opts): |
|
624 | 624 | ctx = self._repo[opts['rev']] |
|
625 | 625 | parents = self._repo.dirstate.parents() |
|
626 | 626 | if opts['all']: |
|
627 | 627 | pats = ['set:modified()'] |
|
628 | 628 | else: |
|
629 | 629 | pats = [] |
|
630 | 630 | cmdutil.revert(ui, self._repo, ctx, parents, *pats, **opts) |
|
631 | 631 | |
|
632 | 632 | class svnsubrepo(abstractsubrepo): |
|
633 | 633 | def __init__(self, ctx, path, state): |
|
634 | 634 | self._path = path |
|
635 | 635 | self._state = state |
|
636 | 636 | self._ctx = ctx |
|
637 | 637 | self._ui = ctx._repo.ui |
|
638 | 638 | self._exe = util.findexe('svn') |
|
639 | 639 | if not self._exe: |
|
640 | 640 | raise util.Abort(_("'svn' executable not found for subrepo '%s'") |
|
641 | 641 | % self._path) |
|
642 | 642 | |
|
643 | 643 | def _svncommand(self, commands, filename='', failok=False): |
|
644 | 644 | cmd = [self._exe] |
|
645 | 645 | extrakw = {} |
|
646 | 646 | if not self._ui.interactive(): |
|
647 | 647 | # Making stdin be a pipe should prevent svn from behaving |
|
648 | 648 | # interactively even if we can't pass --non-interactive. |
|
649 | 649 | extrakw['stdin'] = subprocess.PIPE |
|
650 | 650 | # Starting in svn 1.5 --non-interactive is a global flag |
|
651 | 651 | # instead of being per-command, but we need to support 1.4 so |
|
652 | 652 | # we have to be intelligent about what commands take |
|
653 | 653 | # --non-interactive. |
|
654 | 654 | if commands[0] in ('update', 'checkout', 'commit'): |
|
655 | 655 | cmd.append('--non-interactive') |
|
656 | 656 | cmd.extend(commands) |
|
657 | 657 | if filename is not None: |
|
658 | 658 | path = os.path.join(self._ctx._repo.origroot, self._path, filename) |
|
659 | 659 | cmd.append(path) |
|
660 | 660 | env = dict(os.environ) |
|
661 | 661 | # Avoid localized output, preserve current locale for everything else. |
|
662 | 662 | lc_all = env.get('LC_ALL') |
|
663 | 663 | if lc_all: |
|
664 | 664 | env['LANG'] = lc_all |
|
665 | 665 | del env['LC_ALL'] |
|
666 | 666 | env['LC_MESSAGES'] = 'C' |
|
667 | 667 | p = subprocess.Popen(cmd, bufsize=-1, close_fds=util.closefds, |
|
668 | 668 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
|
669 | 669 | universal_newlines=True, env=env, **extrakw) |
|
670 | 670 | stdout, stderr = p.communicate() |
|
671 | 671 | stderr = stderr.strip() |
|
672 | 672 | if not failok: |
|
673 | 673 | if p.returncode: |
|
674 | 674 | raise util.Abort(stderr or 'exited with code %d' % p.returncode) |
|
675 | 675 | if stderr: |
|
676 | 676 | self._ui.warn(stderr + '\n') |
|
677 | 677 | return stdout, stderr |
|
678 | 678 | |
|
679 | 679 | @propertycache |
|
680 | 680 | def _svnversion(self): |
|
681 | 681 | output, err = self._svncommand(['--version', '--quiet'], filename=None) |
|
682 | 682 | m = re.search(r'^(\d+)\.(\d+)', output) |
|
683 | 683 | if not m: |
|
684 | 684 | raise util.Abort(_('cannot retrieve svn tool version')) |
|
685 | 685 | return (int(m.group(1)), int(m.group(2))) |
|
686 | 686 | |
|
687 | 687 | def _wcrevs(self): |
|
688 | 688 | # Get the working directory revision as well as the last |
|
689 | 689 | # commit revision so we can compare the subrepo state with |
|
690 | 690 | # both. We used to store the working directory one. |
|
691 | 691 | output, err = self._svncommand(['info', '--xml']) |
|
692 | 692 | doc = xml.dom.minidom.parseString(output) |
|
693 | 693 | entries = doc.getElementsByTagName('entry') |
|
694 | 694 | lastrev, rev = '0', '0' |
|
695 | 695 | if entries: |
|
696 | 696 | rev = str(entries[0].getAttribute('revision')) or '0' |
|
697 | 697 | commits = entries[0].getElementsByTagName('commit') |
|
698 | 698 | if commits: |
|
699 | 699 | lastrev = str(commits[0].getAttribute('revision')) or '0' |
|
700 | 700 | return (lastrev, rev) |
|
701 | 701 | |
|
702 | 702 | def _wcrev(self): |
|
703 | 703 | return self._wcrevs()[0] |
|
704 | 704 | |
|
705 | 705 | def _wcchanged(self): |
|
706 | 706 | """Return (changes, extchanges, missing) where changes is True |
|
707 | 707 | if the working directory was changed, extchanges is |
|
708 | 708 | True if any of these changes concern an external entry and missing |
|
709 | 709 | is True if any change is a missing entry. |
|
710 | 710 | """ |
|
711 | 711 | output, err = self._svncommand(['status', '--xml']) |
|
712 | 712 | externals, changes, missing = [], [], [] |
|
713 | 713 | doc = xml.dom.minidom.parseString(output) |
|
714 | 714 | for e in doc.getElementsByTagName('entry'): |
|
715 | 715 | s = e.getElementsByTagName('wc-status') |
|
716 | 716 | if not s: |
|
717 | 717 | continue |
|
718 | 718 | item = s[0].getAttribute('item') |
|
719 | 719 | props = s[0].getAttribute('props') |
|
720 | 720 | path = e.getAttribute('path') |
|
721 | 721 | if item == 'external': |
|
722 | 722 | externals.append(path) |
|
723 | 723 | elif item == 'missing': |
|
724 | 724 | missing.append(path) |
|
725 | 725 | if (item not in ('', 'normal', 'unversioned', 'external') |
|
726 | 726 | or props not in ('', 'none', 'normal')): |
|
727 | 727 | changes.append(path) |
|
728 | 728 | for path in changes: |
|
729 | 729 | for ext in externals: |
|
730 | 730 | if path == ext or path.startswith(ext + os.sep): |
|
731 | 731 | return True, True, bool(missing) |
|
732 | 732 | return bool(changes), False, bool(missing) |
|
733 | 733 | |
|
734 | 734 | def dirty(self, ignoreupdate=False): |
|
735 | 735 | if not self._wcchanged()[0]: |
|
736 | 736 | if self._state[1] in self._wcrevs() or ignoreupdate: |
|
737 | 737 | return False |
|
738 | 738 | return True |
|
739 | 739 | |
|
740 | 740 | def basestate(self): |
|
741 | 741 | lastrev, rev = self._wcrevs() |
|
742 | 742 | if lastrev != rev: |
|
743 | 743 | # Last committed rev is not the same than rev. We would |
|
744 | 744 | # like to take lastrev but we do not know if the subrepo |
|
745 | 745 | # URL exists at lastrev. Test it and fallback to rev it |
|
746 | 746 | # is not there. |
|
747 | 747 | try: |
|
748 | 748 | self._svncommand(['list', '%s@%s' % (self._state[0], lastrev)]) |
|
749 | 749 | return lastrev |
|
750 | 750 | except error.Abort: |
|
751 | 751 | pass |
|
752 | 752 | return rev |
|
753 | 753 | |
|
754 | 754 | def commit(self, text, user, date): |
|
755 | 755 | # user and date are out of our hands since svn is centralized |
|
756 | 756 | changed, extchanged, missing = self._wcchanged() |
|
757 | 757 | if not changed: |
|
758 | 758 | return self.basestate() |
|
759 | 759 | if extchanged: |
|
760 | 760 | # Do not try to commit externals |
|
761 | 761 | raise util.Abort(_('cannot commit svn externals')) |
|
762 | 762 | if missing: |
|
763 | 763 | # svn can commit with missing entries but aborting like hg |
|
764 | 764 | # seems a better approach. |
|
765 | 765 | raise util.Abort(_('cannot commit missing svn entries')) |
|
766 | 766 | commitinfo, err = self._svncommand(['commit', '-m', text]) |
|
767 | 767 | self._ui.status(commitinfo) |
|
768 | 768 | newrev = re.search('Committed revision ([0-9]+).', commitinfo) |
|
769 | 769 | if not newrev: |
|
770 | 770 | if not commitinfo.strip(): |
|
771 | 771 | # Sometimes, our definition of "changed" differs from |
|
772 | 772 | # svn one. For instance, svn ignores missing files |
|
773 | 773 | # when committing. If there are only missing files, no |
|
774 | 774 | # commit is made, no output and no error code. |
|
775 | 775 | raise util.Abort(_('failed to commit svn changes')) |
|
776 | 776 | raise util.Abort(commitinfo.splitlines()[-1]) |
|
777 | 777 | newrev = newrev.groups()[0] |
|
778 | 778 | self._ui.status(self._svncommand(['update', '-r', newrev])[0]) |
|
779 | 779 | return newrev |
|
780 | 780 | |
|
781 | 781 | def remove(self): |
|
782 | 782 | if self.dirty(): |
|
783 | 783 | self._ui.warn(_('not removing repo %s because ' |
|
784 | 784 | 'it has changes.\n' % self._path)) |
|
785 | 785 | return |
|
786 | 786 | self._ui.note(_('removing subrepo %s\n') % self._path) |
|
787 | 787 | |
|
788 | 788 | def onerror(function, path, excinfo): |
|
789 | 789 | if function is not os.remove: |
|
790 | 790 | raise |
|
791 | 791 | # read-only files cannot be unlinked under Windows |
|
792 | 792 | s = os.stat(path) |
|
793 | 793 | if (s.st_mode & stat.S_IWRITE) != 0: |
|
794 | 794 | raise |
|
795 | 795 | os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE) |
|
796 | 796 | os.remove(path) |
|
797 | 797 | |
|
798 | 798 | path = self._ctx._repo.wjoin(self._path) |
|
799 | 799 | shutil.rmtree(path, onerror=onerror) |
|
800 | 800 | try: |
|
801 | 801 | os.removedirs(os.path.dirname(path)) |
|
802 | 802 | except OSError: |
|
803 | 803 | pass |
|
804 | 804 | |
|
805 | 805 | def get(self, state, overwrite=False): |
|
806 | 806 | if overwrite: |
|
807 | 807 | self._svncommand(['revert', '--recursive']) |
|
808 | 808 | args = ['checkout'] |
|
809 | 809 | if self._svnversion >= (1, 5): |
|
810 | 810 | args.append('--force') |
|
811 | 811 | # The revision must be specified at the end of the URL to properly |
|
812 | 812 | # update to a directory which has since been deleted and recreated. |
|
813 | 813 | args.append('%s@%s' % (state[0], state[1])) |
|
814 | 814 | status, err = self._svncommand(args, failok=True) |
|
815 | 815 | if not re.search('Checked out revision [0-9]+.', status): |
|
816 | 816 | if ('is already a working copy for a different URL' in err |
|
817 | 817 | and (self._wcchanged()[:2] == (False, False))): |
|
818 | 818 | # obstructed but clean working copy, so just blow it away. |
|
819 | 819 | self.remove() |
|
820 | 820 | self.get(state, overwrite=False) |
|
821 | 821 | return |
|
822 | 822 | raise util.Abort((status or err).splitlines()[-1]) |
|
823 | 823 | self._ui.status(status) |
|
824 | 824 | |
|
825 | 825 | def merge(self, state): |
|
826 | 826 | old = self._state[1] |
|
827 | 827 | new = state[1] |
|
828 | 828 | wcrev = self._wcrev() |
|
829 | 829 | if new != wcrev: |
|
830 | 830 | dirty = old == wcrev or self._wcchanged()[0] |
|
831 | 831 | if _updateprompt(self._ui, self, dirty, wcrev, new): |
|
832 | 832 | self.get(state, False) |
|
833 | 833 | |
|
834 | 834 | def push(self, opts): |
|
835 | 835 | # push is a no-op for SVN |
|
836 | 836 | return True |
|
837 | 837 | |
|
838 | 838 | def files(self): |
|
839 | 839 | output = self._svncommand(['list', '--recursive', '--xml'])[0] |
|
840 | 840 | doc = xml.dom.minidom.parseString(output) |
|
841 | 841 | paths = [] |
|
842 | 842 | for e in doc.getElementsByTagName('entry'): |
|
843 | 843 | kind = str(e.getAttribute('kind')) |
|
844 | 844 | if kind != 'file': |
|
845 | 845 | continue |
|
846 | 846 | name = ''.join(c.data for c |
|
847 | 847 | in e.getElementsByTagName('name')[0].childNodes |
|
848 | 848 | if c.nodeType == c.TEXT_NODE) |
|
849 | 849 | paths.append(name.encode('utf-8')) |
|
850 | 850 | return paths |
|
851 | 851 | |
|
852 | 852 | def filedata(self, name): |
|
853 | 853 | return self._svncommand(['cat'], name)[0] |
|
854 | 854 | |
|
855 | 855 | |
|
856 | 856 | class gitsubrepo(abstractsubrepo): |
|
857 | 857 | def __init__(self, ctx, path, state): |
|
858 | 858 | self._state = state |
|
859 | 859 | self._ctx = ctx |
|
860 | 860 | self._path = path |
|
861 | 861 | self._relpath = os.path.join(reporelpath(ctx._repo), path) |
|
862 | 862 | self._abspath = ctx._repo.wjoin(path) |
|
863 | 863 | self._subparent = ctx._repo |
|
864 | 864 | self._ui = ctx._repo.ui |
|
865 | 865 | self._ensuregit() |
|
866 | 866 | |
|
867 | 867 | def _ensuregit(self): |
|
868 | 868 | try: |
|
869 | 869 | self._gitexecutable = 'git' |
|
870 | 870 | out, err = self._gitnodir(['--version']) |
|
871 | 871 | except OSError, e: |
|
872 | 872 | if e.errno != 2 or os.name != 'nt': |
|
873 | 873 | raise |
|
874 | 874 | self._gitexecutable = 'git.cmd' |
|
875 | 875 | out, err = self._gitnodir(['--version']) |
|
876 | 876 | m = re.search(r'^git version (\d+)\.(\d+)\.(\d+)', out) |
|
877 | 877 | if not m: |
|
878 | 878 | self._ui.warn(_('cannot retrieve git version')) |
|
879 | 879 | return |
|
880 | 880 | version = (int(m.group(1)), m.group(2), m.group(3)) |
|
881 | 881 | # git 1.4.0 can't work at all, but 1.5.X can in at least some cases, |
|
882 | 882 | # despite the docstring comment. For now, error on 1.4.0, warn on |
|
883 | 883 | # 1.5.0 but attempt to continue. |
|
884 | 884 | if version < (1, 5, 0): |
|
885 | 885 | raise util.Abort(_('git subrepo requires at least 1.6.0 or later')) |
|
886 | 886 | elif version < (1, 6, 0): |
|
887 | 887 | self._ui.warn(_('git subrepo requires at least 1.6.0 or later')) |
|
888 | 888 | |
|
889 | 889 | def _gitcommand(self, commands, env=None, stream=False): |
|
890 | 890 | return self._gitdir(commands, env=env, stream=stream)[0] |
|
891 | 891 | |
|
892 | 892 | def _gitdir(self, commands, env=None, stream=False): |
|
893 | 893 | return self._gitnodir(commands, env=env, stream=stream, |
|
894 | 894 | cwd=self._abspath) |
|
895 | 895 | |
|
896 | 896 | def _gitnodir(self, commands, env=None, stream=False, cwd=None): |
|
897 | 897 | """Calls the git command |
|
898 | 898 | |
|
899 | 899 | The methods tries to call the git command. versions prior to 1.6.0 |
|
900 | 900 | are not supported and very probably fail. |
|
901 | 901 | """ |
|
902 | 902 | self._ui.debug('%s: git %s\n' % (self._relpath, ' '.join(commands))) |
|
903 | 903 | # unless ui.quiet is set, print git's stderr, |
|
904 | 904 | # which is mostly progress and useful info |
|
905 | 905 | errpipe = None |
|
906 | 906 | if self._ui.quiet: |
|
907 | 907 | errpipe = open(os.devnull, 'w') |
|
908 | 908 | p = subprocess.Popen([self._gitexecutable] + commands, bufsize=-1, |
|
909 | 909 | cwd=cwd, env=env, close_fds=util.closefds, |
|
910 | 910 | stdout=subprocess.PIPE, stderr=errpipe) |
|
911 | 911 | if stream: |
|
912 | 912 | return p.stdout, None |
|
913 | 913 | |
|
914 | 914 | retdata = p.stdout.read().strip() |
|
915 | 915 | # wait for the child to exit to avoid race condition. |
|
916 | 916 | p.wait() |
|
917 | 917 | |
|
918 | 918 | if p.returncode != 0 and p.returncode != 1: |
|
919 | 919 | # there are certain error codes that are ok |
|
920 | 920 | command = commands[0] |
|
921 | 921 | if command in ('cat-file', 'symbolic-ref'): |
|
922 | 922 | return retdata, p.returncode |
|
923 | 923 | # for all others, abort |
|
924 | 924 | raise util.Abort('git %s error %d in %s' % |
|
925 | 925 | (command, p.returncode, self._relpath)) |
|
926 | 926 | |
|
927 | 927 | return retdata, p.returncode |
|
928 | 928 | |
|
929 | 929 | def _gitmissing(self): |
|
930 | 930 | return not os.path.exists(os.path.join(self._abspath, '.git')) |
|
931 | 931 | |
|
932 | 932 | def _gitstate(self): |
|
933 | 933 | return self._gitcommand(['rev-parse', 'HEAD']) |
|
934 | 934 | |
|
935 | 935 | def _gitcurrentbranch(self): |
|
936 | 936 | current, err = self._gitdir(['symbolic-ref', 'HEAD', '--quiet']) |
|
937 | 937 | if err: |
|
938 | 938 | current = None |
|
939 | 939 | return current |
|
940 | 940 | |
|
941 | 941 | def _gitremote(self, remote): |
|
942 | 942 | out = self._gitcommand(['remote', 'show', '-n', remote]) |
|
943 | 943 | line = out.split('\n')[1] |
|
944 | 944 | i = line.index('URL: ') + len('URL: ') |
|
945 | 945 | return line[i:] |
|
946 | 946 | |
|
947 | 947 | def _githavelocally(self, revision): |
|
948 | 948 | out, code = self._gitdir(['cat-file', '-e', revision]) |
|
949 | 949 | return code == 0 |
|
950 | 950 | |
|
951 | 951 | def _gitisancestor(self, r1, r2): |
|
952 | 952 | base = self._gitcommand(['merge-base', r1, r2]) |
|
953 | 953 | return base == r1 |
|
954 | 954 | |
|
955 | 955 | def _gitisbare(self): |
|
956 | 956 | return self._gitcommand(['config', '--bool', 'core.bare']) == 'true' |
|
957 | 957 | |
|
958 | 958 | def _gitupdatestat(self): |
|
959 | 959 | """This must be run before git diff-index. |
|
960 | 960 | diff-index only looks at changes to file stat; |
|
961 | 961 | this command looks at file contents and updates the stat.""" |
|
962 | 962 | self._gitcommand(['update-index', '-q', '--refresh']) |
|
963 | 963 | |
|
964 | 964 | def _gitbranchmap(self): |
|
965 | 965 | '''returns 2 things: |
|
966 | 966 | a map from git branch to revision |
|
967 | 967 | a map from revision to branches''' |
|
968 | 968 | branch2rev = {} |
|
969 | 969 | rev2branch = {} |
|
970 | 970 | |
|
971 | 971 | out = self._gitcommand(['for-each-ref', '--format', |
|
972 | 972 | '%(objectname) %(refname)']) |
|
973 | 973 | for line in out.split('\n'): |
|
974 | 974 | revision, ref = line.split(' ') |
|
975 | 975 | if (not ref.startswith('refs/heads/') and |
|
976 | 976 | not ref.startswith('refs/remotes/')): |
|
977 | 977 | continue |
|
978 | 978 | if ref.startswith('refs/remotes/') and ref.endswith('/HEAD'): |
|
979 | 979 | continue # ignore remote/HEAD redirects |
|
980 | 980 | branch2rev[ref] = revision |
|
981 | 981 | rev2branch.setdefault(revision, []).append(ref) |
|
982 | 982 | return branch2rev, rev2branch |
|
983 | 983 | |
|
984 | 984 | def _gittracking(self, branches): |
|
985 | 985 | 'return map of remote branch to local tracking branch' |
|
986 | 986 | # assumes no more than one local tracking branch for each remote |
|
987 | 987 | tracking = {} |
|
988 | 988 | for b in branches: |
|
989 | 989 | if b.startswith('refs/remotes/'): |
|
990 | 990 | continue |
|
991 | 991 | bname = b.split('/', 2)[2] |
|
992 | 992 | remote = self._gitcommand(['config', 'branch.%s.remote' % bname]) |
|
993 | 993 | if remote: |
|
994 | 994 | ref = self._gitcommand(['config', 'branch.%s.merge' % bname]) |
|
995 | 995 | tracking['refs/remotes/%s/%s' % |
|
996 | 996 | (remote, ref.split('/', 2)[2])] = b |
|
997 | 997 | return tracking |
|
998 | 998 | |
|
999 | 999 | def _abssource(self, source): |
|
1000 | 1000 | if '://' not in source: |
|
1001 | 1001 | # recognize the scp syntax as an absolute source |
|
1002 | 1002 | colon = source.find(':') |
|
1003 | 1003 | if colon != -1 and '/' not in source[:colon]: |
|
1004 | 1004 | return source |
|
1005 | 1005 | self._subsource = source |
|
1006 | 1006 | return _abssource(self) |
|
1007 | 1007 | |
|
1008 | 1008 | def _fetch(self, source, revision): |
|
1009 | 1009 | if self._gitmissing(): |
|
1010 | 1010 | source = self._abssource(source) |
|
1011 | 1011 | self._ui.status(_('cloning subrepo %s from %s\n') % |
|
1012 | 1012 | (self._relpath, source)) |
|
1013 | 1013 | self._gitnodir(['clone', source, self._abspath]) |
|
1014 | 1014 | if self._githavelocally(revision): |
|
1015 | 1015 | return |
|
1016 | 1016 | self._ui.status(_('pulling subrepo %s from %s\n') % |
|
1017 | 1017 | (self._relpath, self._gitremote('origin'))) |
|
1018 | 1018 | # try only origin: the originally cloned repo |
|
1019 | 1019 | self._gitcommand(['fetch']) |
|
1020 | 1020 | if not self._githavelocally(revision): |
|
1021 | 1021 | raise util.Abort(_("revision %s does not exist in subrepo %s\n") % |
|
1022 | 1022 | (revision, self._relpath)) |
|
1023 | 1023 | |
|
1024 | 1024 | def dirty(self, ignoreupdate=False): |
|
1025 | 1025 | if self._gitmissing(): |
|
1026 | 1026 | return self._state[1] != '' |
|
1027 | 1027 | if self._gitisbare(): |
|
1028 | 1028 | return True |
|
1029 | 1029 | if not ignoreupdate and self._state[1] != self._gitstate(): |
|
1030 | 1030 | # different version checked out |
|
1031 | 1031 | return True |
|
1032 | 1032 | # check for staged changes or modified files; ignore untracked files |
|
1033 | 1033 | self._gitupdatestat() |
|
1034 | 1034 | out, code = self._gitdir(['diff-index', '--quiet', 'HEAD']) |
|
1035 | 1035 | return code == 1 |
|
1036 | 1036 | |
|
1037 | 1037 | def basestate(self): |
|
1038 | 1038 | return self._gitstate() |
|
1039 | 1039 | |
|
1040 | 1040 | def get(self, state, overwrite=False): |
|
1041 | 1041 | source, revision, kind = state |
|
1042 | 1042 | if not revision: |
|
1043 | 1043 | self.remove() |
|
1044 | 1044 | return |
|
1045 | 1045 | self._fetch(source, revision) |
|
1046 | 1046 | # if the repo was set to be bare, unbare it |
|
1047 | 1047 | if self._gitisbare(): |
|
1048 | 1048 | self._gitcommand(['config', 'core.bare', 'false']) |
|
1049 | 1049 | if self._gitstate() == revision: |
|
1050 | 1050 | self._gitcommand(['reset', '--hard', 'HEAD']) |
|
1051 | 1051 | return |
|
1052 | 1052 | elif self._gitstate() == revision: |
|
1053 | 1053 | if overwrite: |
|
1054 | 1054 | # first reset the index to unmark new files for commit, because |
|
1055 | 1055 | # reset --hard will otherwise throw away files added for commit, |
|
1056 | 1056 | # not just unmark them. |
|
1057 | 1057 | self._gitcommand(['reset', 'HEAD']) |
|
1058 | 1058 | self._gitcommand(['reset', '--hard', 'HEAD']) |
|
1059 | 1059 | return |
|
1060 | 1060 | branch2rev, rev2branch = self._gitbranchmap() |
|
1061 | 1061 | |
|
1062 | 1062 | def checkout(args): |
|
1063 | 1063 | cmd = ['checkout'] |
|
1064 | 1064 | if overwrite: |
|
1065 | 1065 | # first reset the index to unmark new files for commit, because |
|
1066 | 1066 | # the -f option will otherwise throw away files added for |
|
1067 | 1067 | # commit, not just unmark them. |
|
1068 | 1068 | self._gitcommand(['reset', 'HEAD']) |
|
1069 | 1069 | cmd.append('-f') |
|
1070 | 1070 | self._gitcommand(cmd + args) |
|
1071 | 1071 | |
|
1072 | 1072 | def rawcheckout(): |
|
1073 | 1073 | # no branch to checkout, check it out with no branch |
|
1074 | 1074 | self._ui.warn(_('checking out detached HEAD in subrepo %s\n') % |
|
1075 | 1075 | self._relpath) |
|
1076 | 1076 | self._ui.warn(_('check out a git branch if you intend ' |
|
1077 | 1077 | 'to make changes\n')) |
|
1078 | 1078 | checkout(['-q', revision]) |
|
1079 | 1079 | |
|
1080 | 1080 | if revision not in rev2branch: |
|
1081 | 1081 | rawcheckout() |
|
1082 | 1082 | return |
|
1083 | 1083 | branches = rev2branch[revision] |
|
1084 | 1084 | firstlocalbranch = None |
|
1085 | 1085 | for b in branches: |
|
1086 | 1086 | if b == 'refs/heads/master': |
|
1087 | 1087 | # master trumps all other branches |
|
1088 | 1088 | checkout(['refs/heads/master']) |
|
1089 | 1089 | return |
|
1090 | 1090 | if not firstlocalbranch and not b.startswith('refs/remotes/'): |
|
1091 | 1091 | firstlocalbranch = b |
|
1092 | 1092 | if firstlocalbranch: |
|
1093 | 1093 | checkout([firstlocalbranch]) |
|
1094 | 1094 | return |
|
1095 | 1095 | |
|
1096 | 1096 | tracking = self._gittracking(branch2rev.keys()) |
|
1097 | 1097 | # choose a remote branch already tracked if possible |
|
1098 | 1098 | remote = branches[0] |
|
1099 | 1099 | if remote not in tracking: |
|
1100 | 1100 | for b in branches: |
|
1101 | 1101 | if b in tracking: |
|
1102 | 1102 | remote = b |
|
1103 | 1103 | break |
|
1104 | 1104 | |
|
1105 | 1105 | if remote not in tracking: |
|
1106 | 1106 | # create a new local tracking branch |
|
1107 | 1107 | local = remote.split('/', 2)[2] |
|
1108 | 1108 | checkout(['-b', local, remote]) |
|
1109 | 1109 | elif self._gitisancestor(branch2rev[tracking[remote]], remote): |
|
1110 | 1110 | # When updating to a tracked remote branch, |
|
1111 | 1111 | # if the local tracking branch is downstream of it, |
|
1112 | 1112 | # a normal `git pull` would have performed a "fast-forward merge" |
|
1113 | 1113 | # which is equivalent to updating the local branch to the remote. |
|
1114 | 1114 | # Since we are only looking at branching at update, we need to |
|
1115 | 1115 | # detect this situation and perform this action lazily. |
|
1116 | 1116 | if tracking[remote] != self._gitcurrentbranch(): |
|
1117 | 1117 | checkout([tracking[remote]]) |
|
1118 | 1118 | self._gitcommand(['merge', '--ff', remote]) |
|
1119 | 1119 | else: |
|
1120 | 1120 | # a real merge would be required, just checkout the revision |
|
1121 | 1121 | rawcheckout() |
|
1122 | 1122 | |
|
1123 | 1123 | def commit(self, text, user, date): |
|
1124 | 1124 | if self._gitmissing(): |
|
1125 | 1125 | raise util.Abort(_("subrepo %s is missing") % self._relpath) |
|
1126 | 1126 | cmd = ['commit', '-a', '-m', text] |
|
1127 | 1127 | env = os.environ.copy() |
|
1128 | 1128 | if user: |
|
1129 | 1129 | cmd += ['--author', user] |
|
1130 | 1130 | if date: |
|
1131 | 1131 | # git's date parser silently ignores when seconds < 1e9 |
|
1132 | 1132 | # convert to ISO8601 |
|
1133 | 1133 | env['GIT_AUTHOR_DATE'] = util.datestr(date, |
|
1134 | 1134 | '%Y-%m-%dT%H:%M:%S %1%2') |
|
1135 | 1135 | self._gitcommand(cmd, env=env) |
|
1136 | 1136 | # make sure commit works otherwise HEAD might not exist under certain |
|
1137 | 1137 | # circumstances |
|
1138 | 1138 | return self._gitstate() |
|
1139 | 1139 | |
|
1140 | 1140 | def merge(self, state): |
|
1141 | 1141 | source, revision, kind = state |
|
1142 | 1142 | self._fetch(source, revision) |
|
1143 | 1143 | base = self._gitcommand(['merge-base', revision, self._state[1]]) |
|
1144 | 1144 | self._gitupdatestat() |
|
1145 | 1145 | out, code = self._gitdir(['diff-index', '--quiet', 'HEAD']) |
|
1146 | 1146 | |
|
1147 | 1147 | def mergefunc(): |
|
1148 | 1148 | if base == revision: |
|
1149 | 1149 | self.get(state) # fast forward merge |
|
1150 | 1150 | elif base != self._state[1]: |
|
1151 | 1151 | self._gitcommand(['merge', '--no-commit', revision]) |
|
1152 | 1152 | |
|
1153 | 1153 | if self.dirty(): |
|
1154 | 1154 | if self._gitstate() != revision: |
|
1155 | 1155 | dirty = self._gitstate() == self._state[1] or code != 0 |
|
1156 | 1156 | if _updateprompt(self._ui, self, dirty, |
|
1157 | 1157 | self._state[1][:7], revision[:7]): |
|
1158 | 1158 | mergefunc() |
|
1159 | 1159 | else: |
|
1160 | 1160 | mergefunc() |
|
1161 | 1161 | |
|
1162 | 1162 | def push(self, opts): |
|
1163 | 1163 | force = opts.get('force') |
|
1164 | 1164 | |
|
1165 | 1165 | if not self._state[1]: |
|
1166 | 1166 | return True |
|
1167 | 1167 | if self._gitmissing(): |
|
1168 | 1168 | raise util.Abort(_("subrepo %s is missing") % self._relpath) |
|
1169 | 1169 | # if a branch in origin contains the revision, nothing to do |
|
1170 | 1170 | branch2rev, rev2branch = self._gitbranchmap() |
|
1171 | 1171 | if self._state[1] in rev2branch: |
|
1172 | 1172 | for b in rev2branch[self._state[1]]: |
|
1173 | 1173 | if b.startswith('refs/remotes/origin/'): |
|
1174 | 1174 | return True |
|
1175 | 1175 | for b, revision in branch2rev.iteritems(): |
|
1176 | 1176 | if b.startswith('refs/remotes/origin/'): |
|
1177 | 1177 | if self._gitisancestor(self._state[1], revision): |
|
1178 | 1178 | return True |
|
1179 | 1179 | # otherwise, try to push the currently checked out branch |
|
1180 | 1180 | cmd = ['push'] |
|
1181 | 1181 | if force: |
|
1182 | 1182 | cmd.append('--force') |
|
1183 | 1183 | |
|
1184 | 1184 | current = self._gitcurrentbranch() |
|
1185 | 1185 | if current: |
|
1186 | 1186 | # determine if the current branch is even useful |
|
1187 | 1187 | if not self._gitisancestor(self._state[1], current): |
|
1188 | 1188 | self._ui.warn(_('unrelated git branch checked out ' |
|
1189 | 1189 | 'in subrepo %s\n') % self._relpath) |
|
1190 | 1190 | return False |
|
1191 | 1191 | self._ui.status(_('pushing branch %s of subrepo %s\n') % |
|
1192 | 1192 | (current.split('/', 2)[2], self._relpath)) |
|
1193 | 1193 | self._gitcommand(cmd + ['origin', current]) |
|
1194 | 1194 | return True |
|
1195 | 1195 | else: |
|
1196 | 1196 | self._ui.warn(_('no branch checked out in subrepo %s\n' |
|
1197 | 1197 | 'cannot push revision %s\n') % |
|
1198 | 1198 | (self._relpath, self._state[1])) |
|
1199 | 1199 | return False |
|
1200 | 1200 | |
|
1201 | 1201 | def remove(self): |
|
1202 | 1202 | if self._gitmissing(): |
|
1203 | 1203 | return |
|
1204 | 1204 | if self.dirty(): |
|
1205 | 1205 | self._ui.warn(_('not removing repo %s because ' |
|
1206 | 1206 | 'it has changes.\n') % self._relpath) |
|
1207 | 1207 | return |
|
1208 | 1208 | # we can't fully delete the repository as it may contain |
|
1209 | 1209 | # local-only history |
|
1210 | 1210 | self._ui.note(_('removing subrepo %s\n') % self._relpath) |
|
1211 | 1211 | self._gitcommand(['config', 'core.bare', 'true']) |
|
1212 | 1212 | for f in os.listdir(self._abspath): |
|
1213 | 1213 | if f == '.git': |
|
1214 | 1214 | continue |
|
1215 | 1215 | path = os.path.join(self._abspath, f) |
|
1216 | 1216 | if os.path.isdir(path) and not os.path.islink(path): |
|
1217 | 1217 | shutil.rmtree(path) |
|
1218 | 1218 | else: |
|
1219 | 1219 | os.remove(path) |
|
1220 | 1220 | |
|
1221 | 1221 | def archive(self, ui, archiver, prefix, match=None): |
|
1222 | 1222 | source, revision = self._state |
|
1223 | 1223 | if not revision: |
|
1224 | 1224 | return |
|
1225 | 1225 | self._fetch(source, revision) |
|
1226 | 1226 | |
|
1227 | 1227 | # Parse git's native archive command. |
|
1228 | 1228 | # This should be much faster than manually traversing the trees |
|
1229 | 1229 | # and objects with many subprocess calls. |
|
1230 | 1230 | tarstream = self._gitcommand(['archive', revision], stream=True) |
|
1231 | 1231 | tar = tarfile.open(fileobj=tarstream, mode='r|') |
|
1232 | 1232 | relpath = subrelpath(self) |
|
1233 | 1233 | ui.progress(_('archiving (%s)') % relpath, 0, unit=_('files')) |
|
1234 | 1234 | for i, info in enumerate(tar): |
|
1235 | 1235 | if info.isdir(): |
|
1236 | 1236 | continue |
|
1237 | 1237 | if match and not match(info.name): |
|
1238 | 1238 | continue |
|
1239 | 1239 | if info.issym(): |
|
1240 | 1240 | data = info.linkname |
|
1241 | 1241 | else: |
|
1242 | 1242 | data = tar.extractfile(info).read() |
|
1243 | 1243 | archiver.addfile(os.path.join(prefix, self._path, info.name), |
|
1244 | 1244 | info.mode, info.issym(), data) |
|
1245 | 1245 | ui.progress(_('archiving (%s)') % relpath, i + 1, |
|
1246 | 1246 | unit=_('files')) |
|
1247 | 1247 | ui.progress(_('archiving (%s)') % relpath, None) |
|
1248 | 1248 | |
|
1249 | 1249 | |
|
1250 | 1250 | def status(self, rev2, **opts): |
|
1251 | 1251 | rev1 = self._state[1] |
|
1252 | 1252 | if self._gitmissing() or not rev1: |
|
1253 | 1253 | # if the repo is missing, return no results |
|
1254 | 1254 | return [], [], [], [], [], [], [] |
|
1255 | 1255 | modified, added, removed = [], [], [] |
|
1256 | 1256 | self._gitupdatestat() |
|
1257 | 1257 | if rev2: |
|
1258 | 1258 | command = ['diff-tree', rev1, rev2] |
|
1259 | 1259 | else: |
|
1260 | 1260 | command = ['diff-index', rev1] |
|
1261 | 1261 | out = self._gitcommand(command) |
|
1262 | 1262 | for line in out.split('\n'): |
|
1263 | 1263 | tab = line.find('\t') |
|
1264 | 1264 | if tab == -1: |
|
1265 | 1265 | continue |
|
1266 | 1266 | status, f = line[tab - 1], line[tab + 1:] |
|
1267 | 1267 | if status == 'M': |
|
1268 | 1268 | modified.append(f) |
|
1269 | 1269 | elif status == 'A': |
|
1270 | 1270 | added.append(f) |
|
1271 | 1271 | elif status == 'D': |
|
1272 | 1272 | removed.append(f) |
|
1273 | 1273 | |
|
1274 | 1274 | deleted = unknown = ignored = clean = [] |
|
1275 | 1275 | return modified, added, removed, deleted, unknown, ignored, clean |
|
1276 | 1276 | |
|
1277 | 1277 | types = { |
|
1278 | 1278 | 'hg': hgsubrepo, |
|
1279 | 1279 | 'svn': svnsubrepo, |
|
1280 | 1280 | 'git': gitsubrepo, |
|
1281 | 1281 | } |
General Comments 0
You need to be logged in to leave comments.
Login now