##// END OF EJS Templates
patch: isolate patchfile filesystem calls into methods...
Patrick Mezard -
r7392:564326a6 default
parent child Browse files
Show More
@@ -237,9 +237,7 b' class patchfile:'
237 self.missing = missing
237 self.missing = missing
238 if not missing:
238 if not missing:
239 try:
239 try:
240 fp = self.opener(fname, 'r')
240 self.lines = self.readlines(fname)
241 self.lines = fp.readlines()
242 fp.close()
243 self.exists = True
241 self.exists = True
244 except IOError:
242 except IOError:
245 pass
243 pass
@@ -254,6 +252,23 b' class patchfile:'
254 self.printfile(False)
252 self.printfile(False)
255 self.hunks = 0
253 self.hunks = 0
256
254
255 def readlines(self, fname):
256 fp = self.opener(fname, 'r')
257 try:
258 return fp.readlines()
259 finally:
260 fp.close()
261
262 def writelines(self, fname, lines):
263 fp = self.opener(fname, 'w')
264 try:
265 fp.writelines(lines)
266 finally:
267 fp.close()
268
269 def unlink(self, fname):
270 os.unlink(fname)
271
257 def printfile(self, warn):
272 def printfile(self, warn):
258 if self.fileprinted:
273 if self.fileprinted:
259 return
274 return
@@ -304,25 +319,24 b' class patchfile:'
304 self.ui.warn(
319 self.ui.warn(
305 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
320 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
306 (len(self.rej), self.hunks, fname))
321 (len(self.rej), self.hunks, fname))
322
323 def rejlines():
307 base = os.path.basename(self.fname)
324 base = os.path.basename(self.fname)
308 fp = self.opener(fname, 'w')
325 yield "--- %s\n+++ %s\n" % (base, base)
309 fp.write("--- %s\n+++ %s\n" % (base, base))
310 for x in self.rej:
326 for x in self.rej:
311 for l in x.hunk:
327 for l in x.hunk:
312 fp.write(l)
328 yield l
313 if l[-1] != '\n':
329 if l[-1] != '\n':
314 fp.write("\n\ No newline at end of file\n")
330 yield "\n\ No newline at end of file\n"
315 fp.close()
331
332 self.writelines(fname, rejlines())
316
333
317 def write(self, dest=None):
334 def write(self, dest=None):
318 if not self.dirty:
335 if not self.dirty:
319 return
336 return
320 if not dest:
337 if not dest:
321 dest = self.fname
338 dest = self.fname
322 fp = self.opener(dest, 'w')
339 self.writelines(dest, self.lines)
323 for l in self.lines:
324 fp.write(l)
325 fp.close()
326
340
327 def close(self):
341 def close(self):
328 self.write()
342 self.write()
@@ -349,7 +363,7 b' class patchfile:'
349
363
350 if isinstance(h, binhunk):
364 if isinstance(h, binhunk):
351 if h.rmfile():
365 if h.rmfile():
352 os.unlink(self.fname)
366 self.unlink(self.fname)
353 else:
367 else:
354 self.lines[:] = h.new()
368 self.lines[:] = h.new()
355 self.offset += len(h.new())
369 self.offset += len(h.new())
@@ -366,7 +380,7 b' class patchfile:'
366 orig_start = start
380 orig_start = start
367 if diffhelpers.testhunk(old, self.lines, start) == 0:
381 if diffhelpers.testhunk(old, self.lines, start) == 0:
368 if h.rmfile():
382 if h.rmfile():
369 os.unlink(self.fname)
383 self.unlink(self.fname)
370 else:
384 else:
371 self.lines[start : start + h.lena] = h.new()
385 self.lines[start : start + h.lena] = h.new()
372 self.offset += h.lenb - h.lena
386 self.offset += h.lenb - h.lena
General Comments 0
You need to be logged in to leave comments. Login now