Mercurial > hg
comparison mercurial/patch.py @ 23998:b65637247c69
trydiff: collect header-writing in one place
This is the first step towards simplifying the big loop in
trydiff(). This will make both the header code and the non-header code
clearer, and it prepares for further simplification of the many nested
if-statements in the body of the loop.
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 22 Jan 2015 17:00:54 -0800 |
parents | 8b88870cbd1e |
children | e02888efc5aa |
comparison
equal
deleted
inserted
replaced
23997:8b88870cbd1e | 23998:b65637247c69 |
---|---|
1793 # modifications during merges | 1793 # modifications during merges |
1794 for f in modified: | 1794 for f in modified: |
1795 if f not in ctx1: | 1795 if f not in ctx1: |
1796 addedset.add(f) | 1796 addedset.add(f) |
1797 for f in sorted(modified + added + removed): | 1797 for f in sorted(modified + added + removed): |
1798 flag1 = None | |
1799 flag2 = None | |
1798 content1 = None | 1800 content1 = None |
1799 content2 = None | 1801 content2 = None |
1800 binarydiff = False | 1802 binarydiff = False |
1801 header = [] | 1803 op = None |
1802 if f not in addedset: | 1804 if f not in addedset: |
1803 content1 = getfilectx(f, ctx1).data() | 1805 content1 = getfilectx(f, ctx1).data() |
1804 if f not in removedset: | 1806 if f not in removedset: |
1805 content2 = getfilectx(f, ctx2).data() | 1807 content2 = getfilectx(f, ctx2).data() |
1806 f1, f2 = f, f | 1808 f1, f2 = f, f |
1807 if opts.git or losedatafn: | 1809 if opts.git or losedatafn: |
1808 if f in addedset: | 1810 if f in addedset: |
1809 mode2 = gitmode[ctx2.flags(f)] | 1811 flag2 = ctx2.flags(f) |
1810 if f in copy: | 1812 if f in copy: |
1811 if opts.git: | 1813 if opts.git: |
1812 f1 = copy[f] | 1814 f1 = copy[f] |
1813 mode1 = gitmode[ctx1.flags(f1)] | 1815 flag1 = ctx1.flags(f1) |
1814 addmodehdr(header, mode1, mode2) | |
1815 if f1 in removedset and f1 not in gone: | 1816 if f1 in removedset and f1 not in gone: |
1816 op = 'rename' | 1817 op = 'rename' |
1817 gone.add(f1) | 1818 gone.add(f1) |
1818 else: | 1819 else: |
1819 op = 'copy' | 1820 op = 'copy' |
1820 header.append('%s from %s\n' % (op, join(f1))) | |
1821 header.append('%s to %s\n' % (op, join(f))) | |
1822 content1 = getfilectx(f1, ctx1).data() | 1821 content1 = getfilectx(f1, ctx1).data() |
1823 else: | 1822 else: |
1824 losedatafn(f) | 1823 losedatafn(f) |
1825 else: | 1824 else: |
1826 if opts.git: | 1825 if not opts.git and flag2: |
1827 header.append('new file mode %s\n' % mode2) | |
1828 elif ctx2.flags(f): | |
1829 losedatafn(f) | 1826 losedatafn(f) |
1830 if util.binary(content1) or util.binary(content2): | 1827 if util.binary(content1) or util.binary(content2): |
1831 if opts.git: | 1828 if opts.git: |
1832 binarydiff = True | 1829 binarydiff = True |
1833 else: | 1830 else: |
1840 # have we already reported a copy above? | 1837 # have we already reported a copy above? |
1841 if (f in copyto and copyto[f] in addedset | 1838 if (f in copyto and copyto[f] in addedset |
1842 and copy[copyto[f]] == f): | 1839 and copy[copyto[f]] == f): |
1843 continue | 1840 continue |
1844 else: | 1841 else: |
1845 header.append('deleted file mode %s\n' % | 1842 flag1 = ctx1.flags(f) |
1846 gitmode[ctx1.flags(f)]) | |
1847 if util.binary(content1): | 1843 if util.binary(content1): |
1848 binarydiff = True | 1844 binarydiff = True |
1849 elif not content1 or util.binary(content1): | 1845 elif not content1 or util.binary(content1): |
1850 # regular diffs cannot represent empty file deletion | 1846 # regular diffs cannot represent empty file deletion |
1851 losedatafn(f) | 1847 losedatafn(f) |
1852 else: | 1848 else: |
1853 flag1 = ctx1.flags(f) | 1849 flag1 = ctx1.flags(f) |
1854 flag2 = ctx2.flags(f) | 1850 flag2 = ctx2.flags(f) |
1855 binary = util.binary(content1) or util.binary(content2) | 1851 binary = util.binary(content1) or util.binary(content2) |
1856 if opts.git: | 1852 if opts.git: |
1857 addmodehdr(header, gitmode[flag1], gitmode[flag2]) | |
1858 if binary: | 1853 if binary: |
1859 binarydiff = True | 1854 binarydiff = True |
1860 elif binary or flag2 != flag1: | 1855 elif binary or flag2 != flag1: |
1861 losedatafn(f) | 1856 losedatafn(f) |
1857 | |
1858 header = [] | |
1859 if opts.git: | |
1860 if content1 is None: # added | |
1861 header.append('new file mode %s\n' % gitmode[flag2]) | |
1862 elif content2 is None: # removed | |
1863 header.append('deleted file mode %s\n' % gitmode[flag1]) | |
1864 else: # modified/copied/renamed | |
1865 addmodehdr(header, gitmode[flag1], gitmode[flag2]) | |
1866 if op is not None: | |
1867 header.append('%s from %s\n' % (op, join(f1))) | |
1868 header.append('%s to %s\n' % (op, join(f2))) | |
1862 | 1869 |
1863 if opts.git or revs: | 1870 if opts.git or revs: |
1864 header.insert(0, diffline(join(f1), join(f2), revs)) | 1871 header.insert(0, diffline(join(f1), join(f2), revs)) |
1865 if binarydiff and not opts.nobinary: | 1872 if binarydiff and not opts.nobinary: |
1866 text = mdiff.b85diff(content1, content2) | 1873 text = mdiff.b85diff(content1, content2) |