Browse Source

document update; images

Matthew Wang 11 years ago
parent
commit
f362c08766
8 changed files with 110 additions and 166 deletions
  1. 7
    10
      Makefile
  2. 25
    12
      README.md
  3. BIN
      img/default.png
  4. BIN
      img/side-by-side.png
  5. 47
    0
      tests/git.diff
  6. 0
    30
      tests/multi.udiff
  7. 0
    114
      tests/single.udiff
  8. 31
    0
      tests/svn.diff

+ 7
- 10
Makefile View File

@@ -1,21 +1,18 @@
1 1
 # Makefile for testing
2 2
 
3
-.PHONY: dogfood test single-udiff multi-udiff
3
+.PHONY: dogfood test git svn
4 4
 
5 5
 dogfood:
6 6
 	git diff | src/cdiff.py
7 7
 	git diff | src/cdiff.py -s
8 8
 	git diff | src/cdiff.py -s -w 60
9
-	git diff | src/cdiff.py -s -w 100
9
+	git diff | src/cdiff.py -s -w 90
10 10
 
11
-test: single-udiff multi-udiff
11
+test: git svn
12 12
 
13
-single-udiff:
14
-	src/cdiff.py tests/single.udiff
15
-	src/cdiff.py tests/single.udiff | diff -u tests/single.udiff -
16
-
17
-multi-udiff:
18
-	src/cdiff.py tests/multi.udiff
19
-	src/cdiff.py tests/multi.udiff | diff -u tests/multi.udiff -
13
+git svn:
14
+	src/cdiff.py tests/$@.diff
15
+	src/cdiff.py tests/$@.diff -s
16
+	src/cdiff.py tests/$@.diff | diff -u tests/$@.diff -
20 17
 
21 18
 # vim:set noet ts=8 sw=8:

+ 25
- 12
README.md View File

@@ -1,19 +1,33 @@
1 1
 ## About
2 2
 
3
-Diff viewer, side-by-side, auto pager with `less`.
3
+View **colored** diff in unified-diff format or **side-by-side** with **auto
4
+pager**.  Requires Python (>= 2.5.0) and `less`.
5
+
6
+![Default](img/default.png)
7
+![Side-by-side](img/side-by-side.png)
4 8
 
5 9
 ## Install
6 10
 
7
-Just download the `src/cdiff.py` and save to whatever directory which in your
8
-`$PATH`, for example, `$HOME/bin` is in my `$PATH`, so I save the script there
9
-and name as `cdiff`.
11
+Save [src/cdiff.py](https://raw.github.com/ymattw/cdiff/master/src/cdiff.py) to
12
+whatever directory which is in your `$PATH`, for example, `$HOME/bin` is in my
13
+`$PATH`, so I save the script there and name as `cdiff`.
10 14
 
11 15
     curl -ksS https://raw.github.com/ymattw/cdiff/master/src/cdiff.py > ~/bin/cdiff
16
+    chmod +x ~/bin/cdiff
12 17
     
13 18
 ## Usage
14 19
     
15
-Read diff from svn, use option `-s` for side-by-side view, use option `-w` to
16
-use text width other than default `80`.  You don't need `less`, it's automatic:
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` to use text width other than default `80`.
22
+See examples below
23
+
24
+View a diff (patch) file:
25
+
26
+    cdiff foo.patch           # view colored udiff
27
+    cdiff foo.patch -s        # side-by-side
28
+    cdiff foo.patch -s -w 90  # use text width 90 other than default 80
29
+    
30
+Read diff from svn:
17 31
 
18 32
     svn diff | cdiff
19 33
     svn diff | cdiff -s
@@ -25,12 +39,11 @@ Read diff from git:
25 39
     git log -p -2 | cdiff -s
26 40
     git show <commit> | cdiff -s
27 41
 
28
-View a diff (patch) file:
29
-
30
-    cdiff foo.patch
31
-    cdiff foo.patch -s
32
-    cdiff foo.patch -s -w 90
33
-
34 42
 Redirect output to another patch file is safe:
35 43
 
36 44
     svn diff | cdiff -s > my.patch
45
+
46
+## Known issue
47
+
48
+- Only support unified format for input diff
49
+- Side-by-side mode has alignment problem for wide chars

BIN
img/default.png View File


BIN
img/side-by-side.png View File


+ 47
- 0
tests/git.diff View File

@@ -0,0 +1,47 @@
1
+commit 15bfa564b9db08fb277a343a3d0a01d377800606
2
+Author: Matthew Wang <XXXXXXX@gmail.com>
3
+Date:   Thu Jan 31 15:27:17 2013 +0800
4
+
5
+    Default width is now 80
6
+
7
+diff --git a/src/cdiff.py b/src/cdiff.py
8
+index 13f725f..bf15ef1 100755
9
+--- a/src/cdiff.py
10
++++ b/src/cdiff.py
11
+@@ -128,9 +128,7 @@ class Diff(object):
12
+                     yield self._markup_common(' ' + old[1])
13
+ 
14
+     def markup_side_by_side(self, width):
15
+-        """width of 0 means infinite width, None means auto detect. Returns a
16
+-        generator
17
+-        """
18
++        """Returns a generator"""
19
+         def _normalize(line):
20
+             return line.replace('\t', ' ' * 8).replace('\n', '')
21
+ 
22
+@@ -147,7 +145,8 @@ class Diff(object):
23
+                 return markup
24
+ 
25
+         # Setup line width and number width
26
+-        if not width: width = 80
27
++        if width <= 0:
28
++            width = 80
29
+         (start, offset) = self._hunks[-1].get_old_addr()
30
+         max1 = start + offset - 1
31
+         (start, offset) = self._hunks[-1].get_new_addr()
32
+@@ -430,13 +429,10 @@ if __name__ == '__main__':
33
+     parser = optparse.OptionParser(usage)
34
+     parser.add_option('-s', '--side-by-side', action='store_true',
35
+             help=('show in side-by-side mode'))
36
+-    parser.add_option('-w', '--width', type='int', default=None,
37
+-            help='set line width (side-by-side mode only)')
38
++    parser.add_option('-w', '--width', type='int', default=80,
39
++            help='set line width (side-by-side mode only), default is 80')
40
+     opts, args = parser.parse_args()
41
+ 
42
+-    if opts.width and opts.width < 0:
43
+-        opts.width = 0
44
+-
45
+     if len(args) >= 1:
46
+         diff_hdl = open(args[0], 'r')
47
+     elif sys.stdin.isatty():

+ 0
- 30
tests/multi.udiff View File

@@ -1,30 +0,0 @@
1
-diff --git a/Makefile b/Makefile
2
-index 2dc65aa..4d698fc 100644
3
---- a/Makefile
4
-+++ b/Makefile
5
-@@ -2,9 +2,12 @@
6
- 
7
- .PHONY: test single
8
- 
9
--test: single
10
-+test: single multi
11
- 
12
- single:
13
- 	src/diffview.py -t tests/single.diff
14
- 
15
-+multi:
16
-+	src/diffview.py -t tests/multi.diff
17
-+
18
- # vim:set noet ts=8 sw=8:
19
-diff --git a/README.md b/README.md
20
-index 39b5c84..1da3039 100644
21
---- a/README.md
22
-+++ b/README.md
23
-@@ -1,3 +1,7 @@
24
- ## About
25
- 
26
-+Diff viewer.
27
-+
28
- ## Usage
29
-+
30
-+TODO

+ 0
- 114
tests/single.udiff View File

@@ -1,114 +0,0 @@
1
-diff --git a/src/cdiff.py b/src/cdiff.py
2
-index 1db7807..c17032f 100755
3
---- a/src/cdiff.py
4
-+++ b/src/cdiff.py
5
-@@ -17,7 +17,7 @@ COLORS = {
6
-     'lightred'      : '\x1b[1;31m',
7
-     'lightgreen'    : '\x1b[1;32m',
8
-     'lightyellow'   : '\x1b[1;33m',
9
--    'lightblue  '   : '\x1b[1;34m',
10
-+    'lightblue'     : '\x1b[1;34m',
11
-     'lightmagenta'  : '\x1b[1;35m',
12
-     'lightcyan'     : '\x1b[1;36m',
13
- }
14
-@@ -31,23 +31,13 @@ def colorize(text, start_color, end_color='reset'):
15
- 
16
- class Hunk(object):
17
- 
18
--    def __init__(self, hunk_header, old_addr, old_offset, new_addr, new_offset):
19
-+    def __init__(self, hunk_header):
20
-         self._hunk_header = hunk_header
21
--        self._old_addr = old_addr
22
--        self._old_offset = old_offset
23
--        self._new_addr = new_addr
24
--        self._new_offset = new_offset
25
-         self._hunk_list = []   # 2-element group (attr, line)
26
- 
27
-     def get_header(self):
28
-         return self._hunk_header
29
- 
30
--    def get_old_addr(self):
31
--        return (self._old_addr, self._old_offset)
32
--
33
--    def get_new_addr(self):
34
--        return (self._new_addr, self._new_offset)
35
--
36
-     def append(self, attr, line):
37
-         """attr: '-': old, '+': new, ' ': common"""
38
-         self._hunk_list.append((attr, line))
39
-@@ -117,10 +107,10 @@ class Diff(object):
40
-                         line = from_info[1].strip('\x00\x01')
41
-                         out.append(self._markup_old(line))
42
-                     else:
43
--                        out.append(self._markup_old('-' +
44
--                            self._markup_old_mix(from_info[1])))
45
--                        out.append(self._markup_new('+' +
46
--                            self._markup_new_mix(to_info[1])))
47
-+                        out.append(self._markup_old('-') +
48
-+                            self._markup_old_mix(from_info[1]))
49
-+                        out.append(self._markup_new('+') +
50
-+                            self._markup_new_mix(to_info[1]))
51
-                 else:
52
-                     out.append(self._markup_common(' ' + from_info[1]))
53
-         return ''.join(out)
54
-@@ -139,7 +129,7 @@ class Diff(object):
55
-         return colorize(line, 'yellow')
56
- 
57
-     def _markup_hunk_header(self, line):
58
--        return colorize(line, 'blue')
59
-+        return colorize(line, 'lightblue')
60
- 
61
-     def _markup_common(self, line):
62
-         return colorize(line, 'reset')
63
-@@ -150,18 +140,20 @@ class Diff(object):
64
-     def _markup_new(self, line):
65
-         return colorize(line, 'lightgreen')
66
- 
67
--    def _markup_mix(self, line, end_color):
68
--        line = line.replace('\x00-', ansi_code('red'))
69
--        line = line.replace('\x00+', ansi_code('green'))
70
--        line = line.replace('\x00^', ansi_code('lightyellow'))
71
--        line = line.replace('\x01', ansi_code(end_color))
72
--        return colorize(line, end_color)
73
-+    def _markup_mix(self, line, base_color, del_color, add_color, chg_color):
74
-+        line = line.replace('\x00-', ansi_code(del_color))
75
-+        line = line.replace('\x00+', ansi_code(add_color))
76
-+        line = line.replace('\x00^', ansi_code(chg_color))
77
-+        line = line.replace('\x01', ansi_code(base_color))
78
-+        return colorize(line, base_color)
79
- 
80
-     def _markup_old_mix(self, line):
81
--        return self._markup_mix(line, 'red')
82
-+        return self._markup_mix(line, 'cyan', 'lightred', 'lightgreen',
83
-+                'yellow')
84
- 
85
-     def _markup_new_mix(self, line):
86
--        return self._markup_mix(line, 'green')
87
-+        return self._markup_mix(line, 'lightcyan', 'lightred', 'lightgreen',
88
-+                'lightyellow')
89
- 
90
- 
91
- class Udiff(Diff):
92
-@@ -278,21 +270,7 @@ class DiffParser(object):
93
-                     hunks.append(hunk)
94
-                     hunk = None
95
-                 else:
96
--                    # @@ -3,7 +3,6 @@
97
--                    hunk_header = stream.pop(0)
98
--
99
--                    addr_info = hunk_header.split()[1]
100
--                    assert addr_info.startswith('-')
101
--                    old_addr = addr_info.split(',')[0]
102
--                    old_offset = addr_info.split(',')[1]
103
--
104
--                    addr_info = hunk_header.split()[2]
105
--                    assert addr_info.startswith('+')
106
--                    new_addr = addr_info.split(',')[0]
107
--                    new_offset = addr_info.split(',')[1]
108
--
109
--                    hunk = Hunk(hunk_header, old_addr, old_offset, new_addr,
110
--                            new_offset)
111
-+                    hunk = Hunk(stream.pop(0))
112
- 
113
-             elif Udiff.is_old(stream[0]) or Udiff.is_new(stream[0]) or \
114
-                     Udiff.is_common(stream[0]):

+ 31
- 0
tests/svn.diff View File

@@ -0,0 +1,31 @@
1
+Index: README.md
2
+===================================================================
3
+--- README.md	(revision 41)
4
++++ README.md	(working copy)
5
+@@ -3,7 +3,6 @@
6
+ ## About
7
+ 
8
+ Coderev is a toolkit generates static side-by-side html pages for code review.
9
+-Typical use case is to generate diff pages for local modification in svn/cvs
10
+ workspace and send page link to teammates for code review.
11
+ 
12
+ See [joyus.org/pub/coderev-demo](http://joyus.org/pub/coderev-demo) for a demo.
13
+@@ -18,9 +17,10 @@
14
+ 
15
+ ## Usage of coderev.sh
16
+ 
17
+-Just type `./coderev.sh -h` to see the usage.
18
++Just type `./coderev.sh --help` to see the usage.
19
+ 
20
+     Usage:
21
++
22
+         coderev.sh [-r revision] [-w width] [-o outdir] [-y] [-d name] \
23
+                 [-F comment-file | -m 'comment...'] [file...]
24
+ 
25
+@@ -145,3 +145,5 @@
26
+                             specify column number where lines are broken and
27
+                             wrapped for sdiff, default is no line wrapping
28
+     -y, --yes             do not prompt for overwriting
29
++
30
++# EOF
31
+\ No newline at end of file