comparison mercurial/patch.py @ 2907:8b02af865990

Add diff --git option
author Brendan Cully <brendan@kublai.com>
date Mon, 14 Aug 2006 22:48:03 -0700
parents eab07a7b7491
children b70740aefa4d
comparison
equal deleted inserted replaced
2906:453097750fbf 2907:8b02af865990
296 _date2 = util.datestr(change[2]) 296 _date2 = util.datestr(change[2])
297 def date2(f): 297 def date2(f):
298 return _date2 298 return _date2
299 def read(f): 299 def read(f):
300 return repo.file(f).read(mmap2[f]) 300 return repo.file(f).read(mmap2[f])
301 def renamed(f):
302 src = repo.file(f).renamed(mmap2[f])
303 return src and src[0] or None
301 else: 304 else:
302 tz = util.makedate()[1] 305 tz = util.makedate()[1]
303 _date2 = util.datestr() 306 _date2 = util.datestr()
304 def date2(f): 307 def date2(f):
305 try: 308 try:
307 except OSError, err: 310 except OSError, err:
308 if err.errno != errno.ENOENT: raise 311 if err.errno != errno.ENOENT: raise
309 return _date2 312 return _date2
310 def read(f): 313 def read(f):
311 return repo.wread(f) 314 return repo.wread(f)
315 def renamed(f):
316 return repo.dirstate.copies.get(f)
312 317
313 if repo.ui.quiet: 318 if repo.ui.quiet:
314 r = None 319 r = None
315 else: 320 else:
316 hexfunc = repo.ui.verbose and hex or short 321 hexfunc = repo.ui.verbose and hex or short
317 r = [hexfunc(node) for node in [node1, node2] if node] 322 r = [hexfunc(node) for node in [node1, node2] if node]
318 323
324 if opts.git:
325 copied = {}
326 for f in added:
327 src = renamed(f)
328 if src:
329 copied[f] = src
330 srcs = [x[1] for x in copied.items()]
331
319 all = modified + added + removed 332 all = modified + added + removed
320 all.sort() 333 all.sort()
321 for f in all: 334 for f in all:
322 to = None 335 to = None
323 tn = None 336 tn = None
337 dodiff = True
324 if f in mmap: 338 if f in mmap:
325 to = repo.file(f).read(mmap[f]) 339 to = repo.file(f).read(mmap[f])
326 if f not in removed: 340 if f not in removed:
327 tn = read(f) 341 tn = read(f)
328 fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, opts=opts)) 342 if opts.git:
343 def gitmode(x):
344 return x and '100755' or '100644'
345 def addmodehdr(header, omode, nmode):
346 if omode != nmode:
347 header.append('old mode %s\n' % omode)
348 header.append('new mode %s\n' % nmode)
349
350 a, b = f, f
351 header = []
352 if f in added:
353 if node2:
354 mode = gitmode(mmap2.execf(f))
355 else:
356 mode = gitmode(util.is_exec(repo.wjoin(f), None))
357 if f in copied:
358 a = copied[f]
359 omode = gitmode(mmap.execf(a))
360 addmodehdr(header, omode, mode)
361 op = a in removed and 'rename' or 'copy'
362 header.append('%s from %s\n' % (op, a))
363 header.append('%s to %s\n' % (op, f))
364 to = repo.file(a).read(mmap[a])
365 else:
366 header.append('new file mode %s\n' % mode)
367 elif f in removed:
368 if f in srcs:
369 dodiff = False
370 else:
371 mode = gitmode(mmap.execf(f))
372 header.append('deleted file mode %s\n' % mode)
373 else:
374 omode = gitmode(mmap.execf(f))
375 nmode = gitmode(util.is_exec(repo.wjoin(f), mmap.execf(f)))
376 addmodehdr(header, omode, nmode)
377 r = None
378 if dodiff:
379 header.insert(0, 'diff --git a/%s b/%s\n' % (a, b))
380 fp.write(''.join(header))
381 if dodiff:
382 fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, opts=opts))
329 383
330 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False, 384 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
331 opts=None): 385 opts=None):
332 '''export changesets as hg patches.''' 386 '''export changesets as hg patches.'''
333 387