Skip to content

Commit ab6b6f8

Browse files
committed
support diffing for svn
* Changed `--version` to use the optparse builtin support for it; * Changed `--numlines` to `--unified` because most diff tools use the latter for specifing number of context lines; * Added `-u` and `-L` to support svn calls to external diff tools. The `-u` is a dummy option; it's there so that option parsing doesn't choke whenever it is used. This is compatible with svn and git. Squash-merge of pharaujo's work in jeffkaufman#28.
1 parent bd84230 commit ab6b6f8

File tree

2 files changed

+75
-34
lines changed

2 files changed

+75
-34
lines changed

README.md

+36-20
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,32 @@ Show differences between files in a two column view.
1616

1717
### Options
1818
```
19-
-h, --help show this help message and exit
20-
--cols=COLS specify the width of the screen. Autodetection is Unix
21-
only
22-
--head=HEAD consider only the first N lines of each file
23-
--highlight color by changing the background color instead of the
24-
foreground color. Very fast, ugly, displays all
25-
changes
26-
--line-numbers generate output with line numbers
27-
--no-bold use non-bold colors; recommended for with solarized
28-
--no-headers don't label the left and right sides with their file
29-
names
30-
--numlines=NUMLINES how many lines of context to print; can't be combined
31-
with --whole-file
32-
--recursive recursively compare subdirectories
33-
--show-all-spaces color all non-matching whitespace including that which
34-
is not needed for drawing the eye to changes. Slow,
35-
ugly, displays all changes
36-
--version print version and exit
37-
--whole-file show the whole file instead of just changed lines and
38-
context
19+
--version show program's version number and exit
20+
-h, --help show this help message and exit
21+
-u, --patch generate patch. This is always true, and only exists
22+
for compatibility
23+
--cols=COLS specify the width of the screen. Autodetection is
24+
Unix only
25+
--head=HEAD consider only the first N lines of each file
26+
--highlight color by changing the background color instead of the
27+
foreground color. Very fast, ugly, displays all
28+
changes
29+
--line-numbers generate output with line numbers
30+
--no-bold use non-bold colors; recommended for with solarized
31+
--no-headers don't label the left and right sides with their file
32+
names
33+
-U NUM, --unified=NUM, --numlines=NUM
34+
how many lines of context to print; can't be combined
35+
with --whole-file
36+
--recursive recursively compare subdirectories
37+
--show-all-spaces color all non-matching whitespace including that which
38+
is not needed for drawing the eye to changes. Slow,
39+
ugly, displays all changes
40+
--whole-file show the whole file instead of just changed lines and
41+
context
42+
-L LABELS, --label=LABELS
43+
override file labels with arbitrary tags. Use twice,
44+
one for each file
3945
```
4046

4147

@@ -54,6 +60,16 @@ To install this as a tool you can use with git, copy
5460
git icdiff
5561
```
5662

63+
64+
## Using with subversion
65+
66+
To try it out, run:
67+
68+
```sh
69+
svn diff --diff-cmd icdiff
70+
```
71+
72+
5773
## License
5874

5975
This file is derived from `difflib.HtmlDiff` which is under the [license](http://www.python.org/download/releases/2.6.2/license/).

icdiff

+39-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import os
1313
import sys
1414
import errno
1515
import difflib
16-
import optparse
16+
from optparse import Option, OptionParser
1717
import re
1818
import filecmp
1919
import unicodedata
@@ -386,12 +386,31 @@ class ConsoleDiff(object):
386386
def simple_colorize(s, chosen_color):
387387
return "%s%s%s" % (color_codes[chosen_color], s, color_codes["none"])
388388

389+
390+
class MultipleOption(Option):
391+
ACTIONS = Option.ACTIONS + ("extend",)
392+
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
393+
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
394+
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
395+
396+
def take_action(self, action, dest, opt, value, values, parser):
397+
if action == "extend":
398+
values.ensure_value(dest, []).append(value)
399+
else:
400+
Option.take_action(self, action, dest, opt, value, values, parser)
401+
402+
389403
def start():
390404
# If you change any of these, also update README.
391-
parser = optparse.OptionParser(usage="usage: %prog [options] left_file right_file",
392-
description="Show differences between files in a two column view.")
405+
parser = OptionParser(usage="usage: %prog [options] left_file right_file",
406+
version="icdiff version %s" % __version__,
407+
description="Show differences between files in a two column view.",
408+
option_class=MultipleOption)
409+
parser.add_option("-u", "--patch", default=True,
410+
action="store_true",
411+
help="generate patch. This is always true, and only exists for compatibility")
393412
parser.add_option("--cols", default=None,
394-
help="specify the width of the screen. Autodetection is Linux only")
413+
help="specify the width of the screen. Autodetection is Unix only")
395414
parser.add_option("--head", default=0,
396415
help="consider only the first N lines of each file")
397416
parser.add_option("--highlight", default=False,
@@ -406,27 +425,26 @@ def start():
406425
parser.add_option("--no-headers", default=False,
407426
action="store_true",
408427
help="don't label the left and right sides with their file names")
409-
parser.add_option("--numlines", default=5,
428+
parser.add_option("-U", "--unified", "--numlines", default=5,
429+
metavar="NUM",
410430
help="how many lines of context to print; can't be combined with --whole-file")
411431
parser.add_option("--recursive", default=False,
412432
action="store_true",
413433
help="recursively compare subdirectories")
414434
parser.add_option("--show-all-spaces", default=False,
415435
action="store_true",
416436
help="color all non-matching whitespace including that which is not needed for drawing the eye to changes. Slow, ugly, displays all changes")
417-
parser.add_option("--version", default=False,
418-
action="store_true",
419-
help="print version and exit")
420437
parser.add_option("--whole-file", default=False,
421438
action="store_true",
422439
help="show the whole file instead of just changed lines and context")
440+
parser.add_option("-L", "--label",
441+
action="extend",
442+
type="string",
443+
dest='labels',
444+
help="override file labels with arbitrary tags. Use twice, one for each file")
423445

424446
(options, args) = parser.parse_args()
425447

426-
if options.version:
427-
print("icdiff version %s" % __version__)
428-
sys.exit()
429-
430448
if len(args) != 2:
431449
parser.print_help()
432450
sys.exit()
@@ -481,7 +499,14 @@ def diff_recursively(options, a, b):
481499
print_meta("File %s is a file while %s is a directory" % (a, b))
482500

483501
def diff_files(options, a, b):
484-
headers = a, b
502+
if options.labels:
503+
if len(options.labels) == 2:
504+
headers = options.labels
505+
else:
506+
sys.stderr.write("error: to use arbitrary file labels, specify -L twice.\n")
507+
sys.exit(1)
508+
else:
509+
headers = a, b
485510
if options.no_headers:
486511
headers = None, None
487512

@@ -506,7 +531,7 @@ def diff_files(options, a, b):
506531
for line in cd.make_table(
507532
lines_a, lines_b, headers[0], headers[1],
508533
context=(not options.whole_file),
509-
numlines=int(options.numlines)):
534+
numlines=int(options.unified)):
510535
print(line)
511536
sys.stdout.flush()
512537

0 commit comments

Comments
 (0)