Mercurial > hg-stable
comparison mercurial/patch.py @ 24106:9cf9432a505b
trydiff: extract function that generates filename pairs
The code that identifies copies/renames, as well as the filenames
before and after, is now isolated and we can extract it to a function
so it can be overridden by extensions (in particular the narrow clone
extension).
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 22 Jan 2015 23:29:00 -0800 |
parents | 0f8baebcdbea |
children | 32e8d94b9473 |
comparison
equal
deleted
inserted
replaced
24105:0f8baebcdbea | 24106:9cf9432a505b |
---|---|
1734 | 1734 |
1735 def diffui(*args, **kw): | 1735 def diffui(*args, **kw): |
1736 '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' | 1736 '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' |
1737 return difflabel(diff, *args, **kw) | 1737 return difflabel(diff, *args, **kw) |
1738 | 1738 |
1739 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, | 1739 def _filepairs(ctx1, modified, added, removed, copy, opts): |
1740 copy, getfilectx, opts, losedatafn, prefix): | 1740 '''generates tuples (f1, f2, copyop), where f1 is the name of the file |
1741 | 1741 before and f2 is the the name after. For added files, f1 will be None, |
1742 def gitindex(text): | 1742 and for removed files, f2 will be None. copyop may be set to None, 'copy' |
1743 if not text: | 1743 or 'rename' (the latter two only if opts.git is set).''' |
1744 text = "" | |
1745 l = len(text) | |
1746 s = util.sha1('blob %d\0' % l) | |
1747 s.update(text) | |
1748 return s.hexdigest() | |
1749 | |
1750 if opts.noprefix: | |
1751 aprefix = bprefix = '' | |
1752 else: | |
1753 aprefix = 'a/' | |
1754 bprefix = 'b/' | |
1755 | |
1756 def diffline(f, revs): | |
1757 revinfo = ' '.join(["-r %s" % rev for rev in revs]) | |
1758 return 'diff %s %s' % (revinfo, f) | |
1759 | |
1760 date1 = util.datestr(ctx1.date()) | |
1761 date2 = util.datestr(ctx2.date()) | |
1762 | |
1763 gone = set() | 1744 gone = set() |
1764 gitmode = {'l': '120000', 'x': '100755', '': '100644'} | |
1765 | 1745 |
1766 copyto = dict([(v, k) for k, v in copy.items()]) | 1746 copyto = dict([(v, k) for k, v in copy.items()]) |
1767 | 1747 |
1768 addedset, removedset = set(added), set(removed) | 1748 addedset, removedset = set(added), set(removed) |
1769 # Fix up added, since merged-in additions appear as | 1749 # Fix up added, since merged-in additions appear as |
1770 # modifications during merges | 1750 # modifications during merges |
1771 for f in modified: | 1751 for f in modified: |
1772 if f not in ctx1: | 1752 if f not in ctx1: |
1773 addedset.add(f) | 1753 addedset.add(f) |
1754 | |
1774 for f in sorted(modified + added + removed): | 1755 for f in sorted(modified + added + removed): |
1775 copyop = None | 1756 copyop = None |
1776 f1, f2 = f, f | 1757 f1, f2 = f, f |
1777 if f in addedset: | 1758 if f in addedset: |
1778 f1 = None | 1759 f1 = None |
1789 if opts.git: | 1770 if opts.git: |
1790 # have we already reported a copy above? | 1771 # have we already reported a copy above? |
1791 if (f in copyto and copyto[f] in addedset | 1772 if (f in copyto and copyto[f] in addedset |
1792 and copy[copyto[f]] == f): | 1773 and copy[copyto[f]] == f): |
1793 continue | 1774 continue |
1794 | 1775 yield f1, f2, copyop |
1776 | |
1777 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, | |
1778 copy, getfilectx, opts, losedatafn, prefix): | |
1779 | |
1780 def gitindex(text): | |
1781 if not text: | |
1782 text = "" | |
1783 l = len(text) | |
1784 s = util.sha1('blob %d\0' % l) | |
1785 s.update(text) | |
1786 return s.hexdigest() | |
1787 | |
1788 if opts.noprefix: | |
1789 aprefix = bprefix = '' | |
1790 else: | |
1791 aprefix = 'a/' | |
1792 bprefix = 'b/' | |
1793 | |
1794 def diffline(f, revs): | |
1795 revinfo = ' '.join(["-r %s" % rev for rev in revs]) | |
1796 return 'diff %s %s' % (revinfo, f) | |
1797 | |
1798 date1 = util.datestr(ctx1.date()) | |
1799 date2 = util.datestr(ctx2.date()) | |
1800 | |
1801 gitmode = {'l': '120000', 'x': '100755', '': '100644'} | |
1802 | |
1803 for f1, f2, copyop in _filepairs( | |
1804 ctx1, modified, added, removed, copy, opts): | |
1795 content1 = None | 1805 content1 = None |
1796 content2 = None | 1806 content2 = None |
1797 if f1: | 1807 if f1: |
1798 content1 = getfilectx(f1, ctx1).data() | 1808 content1 = getfilectx(f1, ctx1).data() |
1799 if f2: | 1809 if f2: |
1809 binary = util.binary(content1) or util.binary(content2) | 1819 binary = util.binary(content1) or util.binary(content2) |
1810 | 1820 |
1811 if losedatafn and not opts.git: | 1821 if losedatafn and not opts.git: |
1812 if (binary or | 1822 if (binary or |
1813 # copy/rename | 1823 # copy/rename |
1814 f in copy or | 1824 f2 in copy or |
1815 # empty file creation | 1825 # empty file creation |
1816 (not f1 and not content2) or | 1826 (not f1 and not content2) or |
1817 # empty file deletion | 1827 # empty file deletion |
1818 (not content1 and not f2) or | 1828 (not content1 and not f2) or |
1819 # create with flags | 1829 # create with flags |
1820 (not f1 and flag2) or | 1830 (not f1 and flag2) or |
1821 # change flags | 1831 # change flags |
1822 (f1 and f2 and flag1 != flag2)): | 1832 (f1 and f2 and flag1 != flag2)): |
1823 losedatafn(f) | 1833 losedatafn(f2 or f1) |
1824 | 1834 |
1825 path1 = posixpath.join(prefix, f1 or f2) | 1835 path1 = posixpath.join(prefix, f1 or f2) |
1826 path2 = posixpath.join(prefix, f2 or f1) | 1836 path2 = posixpath.join(prefix, f2 or f1) |
1827 header = [] | 1837 header = [] |
1828 if opts.git: | 1838 if opts.git: |