Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/commands.py @ 1015:22571b8d35d3
Add automatic binary file detection to diff and export
Based on a patch by Fuming Wang
- add util.binary which decides whether a file is binary if it has any NUL
characters in the first 1K.
- teach mdiff.unidiff to print "binary file <x> has changed" for binary files
- add text flag to cause unidiff and dodiff to treat all files as text
- add -a and --text flags (like diff(1)) to hg diff and export
- update docs
author | mpm@selenic.com |
---|---|
date | Tue, 23 Aug 2005 19:58:46 -0700 |
parents | e37cd99fa909 |
children | 836667830fee |
comparison
equal
deleted
inserted
replaced
1014:e37cd99fa909 | 1015:22571b8d35d3 |
---|---|
139 return pat | 139 return pat |
140 return open(make_filename(repo, r, pat, node, total, seqno, revwidth), | 140 return open(make_filename(repo, r, pat, node, total, seqno, revwidth), |
141 mode) | 141 mode) |
142 | 142 |
143 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always, | 143 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always, |
144 changes=None): | 144 changes=None, text=False): |
145 def date(c): | 145 def date(c): |
146 return time.asctime(time.gmtime(float(c[2].split(' ')[0]))) | 146 return time.asctime(time.gmtime(float(c[2].split(' ')[0]))) |
147 | 147 |
148 if not changes: | 148 if not changes: |
149 (c, a, d, u) = repo.changes(node1, node2, files, match = match) | 149 (c, a, d, u) = repo.changes(node1, node2, files, match = match) |
181 for f in c: | 181 for f in c: |
182 to = None | 182 to = None |
183 if f in mmap: | 183 if f in mmap: |
184 to = repo.file(f).read(mmap[f]) | 184 to = repo.file(f).read(mmap[f]) |
185 tn = read(f) | 185 tn = read(f) |
186 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r)) | 186 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text)) |
187 for f in a: | 187 for f in a: |
188 to = None | 188 to = None |
189 tn = read(f) | 189 tn = read(f) |
190 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r)) | 190 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text)) |
191 for f in d: | 191 for f in d: |
192 to = repo.file(f).read(mmap[f]) | 192 to = repo.file(f).read(mmap[f]) |
193 tn = None | 193 tn = None |
194 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r)) | 194 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text)) |
195 | 195 |
196 def show_changeset(ui, repo, rev=0, changenode=None, filelog=None, brinfo=None): | 196 def show_changeset(ui, repo, rev=0, changenode=None, filelog=None, brinfo=None): |
197 """show a single changeset or file revision""" | 197 """show a single changeset or file revision""" |
198 changelog = repo.changelog | 198 changelog = repo.changelog |
199 if filelog: | 199 if filelog: |
642 for src, abs, rel, exact in items: | 642 for src, abs, rel, exact in items: |
643 print fmt % (src, abs, rel, exactly[exact]) | 643 print fmt % (src, abs, rel, exactly[exact]) |
644 | 644 |
645 def diff(ui, repo, *pats, **opts): | 645 def diff(ui, repo, *pats, **opts): |
646 """diff working directory (or selected files)""" | 646 """diff working directory (or selected files)""" |
647 revs = [] | |
648 if opts['rev']: | |
649 revs = map(lambda x: repo.lookup(x), opts['rev']) | |
650 | |
651 node1, node2 = None, None | 647 node1, node2 = None, None |
648 revs = [repo.lookup(x) for x in opts['rev']] | |
649 | |
652 if len(revs) > 0: | 650 if len(revs) > 0: |
653 node1 = revs[0] | 651 node1 = revs[0] |
654 if len(revs) > 1: | 652 if len(revs) > 1: |
655 node2 = revs[1] | 653 node2 = revs[1] |
656 if len(revs) > 2: | 654 if len(revs) > 2: |
661 if pats: | 659 if pats: |
662 roots, match, results = makewalk(repo, pats, opts) | 660 roots, match, results = makewalk(repo, pats, opts) |
663 for src, abs, rel, exact in results: | 661 for src, abs, rel, exact in results: |
664 files.append(abs) | 662 files.append(abs) |
665 | 663 |
666 dodiff(sys.stdout, ui, repo, node1, node2, files, match=match) | 664 dodiff(sys.stdout, ui, repo, node1, node2, files, match=match, |
665 text=opts['text']) | |
667 | 666 |
668 def doexport(ui, repo, changeset, seqno, total, revwidth, opts): | 667 def doexport(ui, repo, changeset, seqno, total, revwidth, opts): |
669 node = repo.lookup(changeset) | 668 node = repo.lookup(changeset) |
670 prev, other = repo.changelog.parents(node) | 669 prev, other = repo.changelog.parents(node) |
671 change = repo.changelog.read(node) | 670 change = repo.changelog.read(node) |
683 if other != hg.nullid: | 682 if other != hg.nullid: |
684 fp.write("# Parent %s\n" % hg.hex(other)) | 683 fp.write("# Parent %s\n" % hg.hex(other)) |
685 fp.write(change[4].rstrip()) | 684 fp.write(change[4].rstrip()) |
686 fp.write("\n\n") | 685 fp.write("\n\n") |
687 | 686 |
688 dodiff(fp, ui, repo, prev, node) | 687 dodiff(fp, ui, repo, prev, node, text=opts['text']) |
689 if fp != sys.stdout: fp.close() | 688 if fp != sys.stdout: fp.close() |
690 | 689 |
691 def export(ui, repo, *changesets, **opts): | 690 def export(ui, repo, *changesets, **opts): |
692 """dump the header and diffs for one or more changesets""" | 691 """dump the header and diffs for one or more changesets""" |
693 if not changesets: | 692 if not changesets: |
1324 ('X', 'exclude', [], 'exclude path from search')], | 1323 ('X', 'exclude', [], 'exclude path from search')], |
1325 'debugwalk [OPTION]... [FILE]...'), | 1324 'debugwalk [OPTION]... [FILE]...'), |
1326 "^diff": | 1325 "^diff": |
1327 (diff, | 1326 (diff, |
1328 [('r', 'rev', [], 'revision'), | 1327 [('r', 'rev', [], 'revision'), |
1328 ('a', 'text', None, 'treat all files as text'), | |
1329 ('I', 'include', [], 'include path in search'), | 1329 ('I', 'include', [], 'include path in search'), |
1330 ('X', 'exclude', [], 'exclude path from search')], | 1330 ('X', 'exclude', [], 'exclude path from search')], |
1331 'hg diff [-I] [-X] [-r REV1 [-r REV2]] [FILE]...'), | 1331 'hg diff [-I] [-X] [-r REV1 [-r REV2]] [FILE]...'), |
1332 "^export": | 1332 "^export": |
1333 (export, | 1333 (export, |
1334 [('o', 'output', "", 'output to file')], | 1334 [('o', 'output', "", 'output to file'), |
1335 ('a', 'text', None, 'treat all files as text')], | |
1335 "hg export [-o OUTFILE] REV..."), | 1336 "hg export [-o OUTFILE] REV..."), |
1336 "forget": | 1337 "forget": |
1337 (forget, | 1338 (forget, |
1338 [('I', 'include', [], 'include path in search'), | 1339 [('I', 'include', [], 'include path in search'), |
1339 ('X', 'exclude', [], 'exclude path from search')], | 1340 ('X', 'exclude', [], 'exclude path from search')], |