comparison mercurial/patch.py @ 17946:1e13b1184292

diff: move index header generation to patch In an upcoming patch, we will add index information to all git diffs, not only binary diffs, so this code needs to be moved to a more appropriate place. Also, since this information is used for patch headers, it makes more sense to be in the patch module, along with other patch-related metadata.
author Guillermo P?rez <bisho@fb.com>
date Thu, 15 Nov 2012 15:16:41 -0800
parents 45766e2a7384
children a9f4a6076740
comparison
equal deleted inserted replaced
17945:45766e2a7384 17946:1e13b1184292
8 8
9 import cStringIO, email.Parser, os, errno, re 9 import cStringIO, email.Parser, os, errno, re
10 import tempfile, zlib, shutil 10 import tempfile, zlib, shutil
11 11
12 from i18n import _ 12 from i18n import _
13 from node import hex, short 13 from node import hex, nullid, short
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error 14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
15 import context 15 import context
16 16
17 gitre = re.compile('diff --git a/(.*) b/(.*)') 17 gitre = re.compile('diff --git a/(.*) b/(.*)')
18 18
1659 1659
1660 def addmodehdr(header, omode, nmode): 1660 def addmodehdr(header, omode, nmode):
1661 if omode != nmode: 1661 if omode != nmode:
1662 header.append('old mode %s\n' % omode) 1662 header.append('old mode %s\n' % omode)
1663 header.append('new mode %s\n' % nmode) 1663 header.append('new mode %s\n' % nmode)
1664
1665 def addindexmeta(meta, revs):
1666 if opts.git:
1667 i = len(revs)
1668 if i==2:
1669 meta.append('index %s..%s\n' % tuple(revs))
1670 elif i==3:
1671 meta.append('index %s,%s..%s\n' % tuple(revs))
1672
1673 def gitindex(text):
1674 if not text:
1675 return hex(nullid)
1676 l = len(text)
1677 s = util.sha1('blob %d\0' % l)
1678 s.update(text)
1679 return s.hexdigest()
1664 1680
1665 def diffline(a, b, revs): 1681 def diffline(a, b, revs):
1666 if opts.git: 1682 if opts.git:
1667 line = 'diff --git a/%s b/%s\n' % (a, b) 1683 line = 'diff --git a/%s b/%s\n' % (a, b)
1668 elif not repo.ui.quiet: 1684 elif not repo.ui.quiet:
1761 if dodiff: 1777 if dodiff:
1762 if opts.git or revs: 1778 if opts.git or revs:
1763 header.insert(0, diffline(join(a), join(b), revs)) 1779 header.insert(0, diffline(join(a), join(b), revs))
1764 if dodiff == 'binary': 1780 if dodiff == 'binary':
1765 text = mdiff.b85diff(to, tn) 1781 text = mdiff.b85diff(to, tn)
1782 if text:
1783 addindexmeta(header, [gitindex(to), gitindex(tn)])
1766 else: 1784 else:
1767 text = mdiff.unidiff(to, date1, 1785 text = mdiff.unidiff(to, date1,
1768 # ctx2 date may be dynamic 1786 # ctx2 date may be dynamic
1769 tn, util.datestr(ctx2.date()), 1787 tn, util.datestr(ctx2.date()),
1770 join(a), join(b), opts=opts) 1788 join(a), join(b), opts=opts)