Pārlūkot izejas kodu

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

Matthew Wang 12 gadus atpakaļ
vecāks
revīzija
fd87da44b3
4 mainītis faili ar 37 papildinājumiem un 11 dzēšanām
  1. 1
    0
      Makefile
  2. 12
    5
      README.md
  3. 17
    6
      src/cdiff.py
  4. 7
    0
      tests/strange.diff

+ 1
- 0
Makefile Parādīt failu

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

+ 12
- 5
README.md Parādīt failu

17
 
17
 
18
 ## Usage
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
 View a diff (patch) file:
29
 View a diff (patch) file:
25
 
30
 
27
     cdiff foo.patch -s          # view in side by side mode
32
     cdiff foo.patch -s          # view in side by side mode
28
     cdiff foo.patch -s -w 90    # use text width 90 other than default 80
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
     cdiff
38
     cdiff
33
     cdiff -s
39
     cdiff -s
34
     cdiff -s -w 90
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
     git log -p -2 | cdiff -s
45
     git log -p -2 | cdiff -s
39
     git show <commit> | cdiff -s
46
     git show <commit> | cdiff -s
40
 
47
 

+ 17
- 6
src/cdiff.py Parādīt failu

11
 import sys
11
 import sys
12
 
12
 
13
 if sys.hexversion < 0x02050000:
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
     sys.exit(1)
15
     sys.exit(1)
16
 IS_PY3 = sys.hexversion >= 0x03000000
16
 IS_PY3 = sys.hexversion >= 0x03000000
17
 
17
 
404
                     # @@ -3,7 +3,6 @@
404
                     # @@ -3,7 +3,6 @@
405
                     hunk_header = stream.pop(0)
405
                     hunk_header = stream.pop(0)
406
                     a = hunk_header.split()[1].split(',')   # -3 7
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
                     b = hunk_header.split()[2].split(',')   # +3 6
413
                     b = hunk_header.split()[2].split(',')   # +3 6
409
                     if len(b) > 1:
414
                     if len(b) > 1:
410
                         new_addr = (int(b[0][1:]), int(b[1]))
415
                         new_addr = (int(b[0][1:]), int(b[1]))
508
 if __name__ == '__main__':
513
 if __name__ == '__main__':
509
     import optparse
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
     parser = optparse.OptionParser(usage=usage, description=description)
525
     parser = optparse.OptionParser(usage=usage, description=description)
517
     parser.add_option('-s', '--side-by-side', action='store_true',
526
     parser.add_option('-s', '--side-by-side', action='store_true',
529
     elif sys.stdin.isatty():
538
     elif sys.stdin.isatty():
530
         diff_hdl = revision_control_diff()
539
         diff_hdl = revision_control_diff()
531
         if not diff_hdl:
540
         if not diff_hdl:
541
+            sys.stderr.write(('*** Not in a supported workspace, supported '
542
+                              'are: %s\n\n') % ', '.join(supported_vcs))
532
             parser.print_help()
543
             parser.print_help()
533
             sys.exit(1)
544
             sys.exit(1)
534
     else:
545
     else:

+ 7
- 0
tests/strange.diff Parādīt failu

11
 +++ b/.gitignore
11
 +++ b/.gitignore
12
 @@ -0,0 +1 @@
12
 @@ -0,0 +1 @@
13
 +*.pyc
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