Browse Source

Document/usage/dogfood test update after merge for issue #4 (Revision control support); more robust on parsing strange diff

Matthew Wang 11 years ago
parent
commit
fd87da44b3
4 changed files with 37 additions and 11 deletions
  1. 1
    0
      Makefile
  2. 12
    5
      README.md
  3. 17
    6
      src/cdiff.py
  4. 7
    0
      tests/strange.diff

+ 1
- 0
Makefile View File

@@ -5,6 +5,7 @@ TESTS = git svn crlf strange
5 5
 .PHONY: dogfood test $(TESTS)
6 6
 
7 7
 dogfood:
8
+	src/cdiff.py -s
8 9
 	git diff | src/cdiff.py
9 10
 	git diff | src/cdiff.py -s
10 11
 

+ 12
- 5
README.md View File

@@ -17,9 +17,14 @@ whatever directory which is in your `$PATH`, for example, `$HOME/bin` is in my
17 17
 
18 18
 ## Usage
19 19
 
20
-Just give it a diff (patch) file or pipe a diff to it.  Use option `-s` for
21
-side-by-side view, and option `-w N` to set a text width other than default
22
-`80`.  See examples below
20
+Cdiff reads diff from diff (patch) file if given, or stdin if redirected, or
21
+diff produced by revision tool if in a git/svn/hg workspace.  Use option `-s`
22
+to enable side by side view, and option `-w N` to set a text width other than
23
+default `80`.  See examples below.
24
+
25
+Show usage:
26
+
27
+    cdiff -h
23 28
 
24 29
 View a diff (patch) file:
25 30
 
@@ -27,14 +32,16 @@ View a diff (patch) file:
27 32
     cdiff foo.patch -s          # view in side by side mode
28 33
     cdiff foo.patch -s -w 90    # use text width 90 other than default 80
29 34
 
30
-Read diff from svn, git, or hg:
35
+Read diff from local modification in a svn, git, or hg workspace:
31 36
 
37
+    cd proj-workspace
32 38
     cdiff
33 39
     cdiff -s
34 40
     cdiff -s -w 90
35 41
 
36
-Read diffs in git logs:
42
+Pipe in a diff:
37 43
 
44
+    svn diff -r PREV | cdiff -s
38 45
     git log -p -2 | cdiff -s
39 46
     git show <commit> | cdiff -s
40 47
 

+ 17
- 6
src/cdiff.py View File

@@ -11,7 +11,7 @@ See demo at homepage: https://github.com/ymattw/cdiff
11 11
 import sys
12 12
 
13 13
 if sys.hexversion < 0x02050000:
14
-    sys.stderr.write("ERROR: requires python >= 2.5.0\n")
14
+    sys.stderr.write("*** Requires python >= 2.5.0\n")
15 15
     sys.exit(1)
16 16
 IS_PY3 = sys.hexversion >= 0x03000000
17 17
 
@@ -404,7 +404,12 @@ class DiffParser(object):
404 404
                     # @@ -3,7 +3,6 @@
405 405
                     hunk_header = stream.pop(0)
406 406
                     a = hunk_header.split()[1].split(',')   # -3 7
407
-                    old_addr = (int(a[0][1:]), int(a[1]))
407
+                    if len(a) > 1:
408
+                        old_addr = (int(a[0][1:]), int(a[1]))
409
+                    else:
410
+                        # @@ -1 +1,2 @@
411
+                        old_addr = (int(a[0][1:]), 0)
412
+
408 413
                     b = hunk_header.split()[2].split(',')   # +3 6
409 414
                     if len(b) > 1:
410 415
                         new_addr = (int(b[0][1:]), int(b[1]))
@@ -508,10 +513,14 @@ def decode(line):
508 513
 if __name__ == '__main__':
509 514
     import optparse
510 515
 
511
-    usage = '''%s [options] [diff]''' % os.path.basename(sys.argv[0])
512
-    description= ('''View incremental, colored diff in unified format or '''
513
-                  '''in side by side mode with auto pager, read stdin if '''
514
-                  '''diff (patch) file is not given''')
516
+    supported_vcs = [check[0] for check, _ in REVISION_CONTROL]
517
+
518
+    usage = '%s [options] [diff]' % os.path.basename(sys.argv[0])
519
+    description= ('View incremental, colored diff in unified format or '
520
+                  'side by side with auto pager.  Read diff from diff '
521
+                  '(patch) file if given, or stdin if redirected, or '
522
+                  'diff produced by revision tool if in a %s workspace') \
523
+            % '/'.join(supported_vcs)
515 524
 
516 525
     parser = optparse.OptionParser(usage=usage, description=description)
517 526
     parser.add_option('-s', '--side-by-side', action='store_true',
@@ -529,6 +538,8 @@ if __name__ == '__main__':
529 538
     elif sys.stdin.isatty():
530 539
         diff_hdl = revision_control_diff()
531 540
         if not diff_hdl:
541
+            sys.stderr.write(('*** Not in a supported workspace, supported '
542
+                              'are: %s\n\n') % ', '.join(supported_vcs))
532 543
             parser.print_help()
533 544
             sys.exit(1)
534 545
     else:

+ 7
- 0
tests/strange.diff View File

@@ -11,3 +11,10 @@ index 0000000..0d20b64
11 11
 +++ b/.gitignore
12 12
 @@ -0,0 +1 @@
13 13
 +*.pyc
14
+diff --git a/README b/README
15
+index 7e70850..b5eb369 100644
16
+--- a/README
17
++++ b/README
18
+@@ -1 +1,2 @@
19
+ Sun Feb  3 13:57:05 CST 2013
20
++Sun Feb  3 13:57:15 CST 2013