@@ -4,8 +4,7 @@ What The Patch!?
4
4
.. image :: https://travis-ci.org/cscorley/whatthepatch.svg?style=flat
5
5
:target: https://travis-ci.org/cscorley/whatthepatch
6
6
7
- What The Patch!? is a library for parsing patch files. Its only purpose is to
8
- read a patch file and get it into some usable form by other programs.
7
+ What The Patch!? is a library for both parsing and applying patch files.
9
8
10
9
Features
11
10
---------
@@ -71,47 +70,41 @@ each diff in the patch:
71
70
.. code-block :: python
72
71
73
72
>> > import whatthepatch
74
- >> > with open (' somechanges.patch' ) as f:
73
+ >> > import pprint
74
+ >> > with open (' tests/casefiles/diff-unified.diff' ) as f:
75
75
... text = f.read()
76
76
...
77
77
>> > for diff in whatthepatch.parse_patch(text):
78
- ... print (diff)
78
+ ... print (diff) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
79
79
...
80
- diff(header = header(
81
- index_path = None ,
82
- old_path = ' lao' ,
83
- old_version = ' 2012-12-26 23:16:54.000000000 -0600' ,
84
- new_path = ' tzu' ,
85
- new_version = ' 2012-12-26 23:16:50.000000000 -0600'
86
- ),
87
- changes = [
88
- (1 , None , ' The Way that can be told of is not the eternal Way;' ),
89
- (2 , None , ' The name that can be named is not the eternal name.' ),
90
- (3 , 1 , ' The Nameless is the origin of Heaven and Earth;' ),
91
- (4 , None , ' The Named is the mother of all things.' ),
92
- (None , 2 , ' The named is the mother of all things.' ),
93
- (None , 3 , ' ' ),
94
- (5 , 4 , ' Therefore let there always be non-being,' ),
95
- (6 , 5 , ' so we may see their subtlety,' ),
96
- (7 , 6 , ' And let there always be being,' ),
97
- (9 , 8 , ' The two are the same,' ),
98
- (10 , 9 , ' But after they are produced,' ),
99
- (11 , 10 , ' they have different names.' ),
100
- (None , 11 , ' They both may be called deep and profound.' ),
101
- (None , 12 , ' Deeper and more profound,' ),
102
- (None , 13 , ' The door of all subtleties!' )
103
- ]
104
- )
105
-
106
- *Edited to show structure of the results *
80
+ diff(header = header(index_path = None ,
81
+ old_path = ' lao' ,
82
+ old_version = ' 2013-01-05 16:56:19.000000000 -0600' ,
83
+ new_path = ' tzu' ,
84
+ new_version = ' 2013-01-05 16:56:35.000000000 -0600' ),
85
+ changes = [Change(old = 1 , new = None , hunk = 1 , line = ' The Way that can be told of is not the eternal Way;' ),
86
+ Change(old = 2 , new = None , hunk = 1 , line = ' The name that can be named is not the eternal name.' ),
87
+ Change(old = 3 , new = 1 , hunk = 1 , line = ' The Nameless is the origin of Heaven and Earth;' ),
88
+ Change(old = 4 , new = None , hunk = 1 , line = ' The Named is the mother of all things.' ),
89
+ Change(old = None , new = 2 , hunk = 1 , line = ' The named is the mother of all things.' ),
90
+ Change(old = None , new = 3 , hunk = 1 , line = ' ' ), Change(old = 5 , new = 4 , hunk = 1 , line = ' Therefore let there always be non-being,' ),
91
+ Change(old = 6 , new = 5 , hunk = 1 , line = ' so we may see their subtlety,' ),
92
+ Change(old = 7 , new = 6 , hunk = 1 , line = ' And let there always be being,' ),
93
+ Change(old = 9 , new = 8 , hunk = 2 , line = ' The two are the same,' ),
94
+ Change(old = 10 , new = 9 , hunk = 2 , line = ' But after they are produced,' ),
95
+ Change(old = 11 , new = 10 , hunk = 2 , line = ' they have different names.' ),
96
+ Change(old = None , new = 11 , hunk = 2 , line = ' They both may be called deep and profound.' ),
97
+ Change(old = None , new = 12 , hunk = 2 , line = ' Deeper and more profound,' ),
98
+ Change(old = None , new = 13 , hunk = 2 , line = ' The door of all subtleties!' )],
99
+ text = ' ...' )
107
100
108
101
The changes are listed as they are in the patch, but instead of the +/- syntax
109
102
of the patch, we get a tuple of two numbers and the text of the line.
110
103
What these numbers indicate are as follows:
111
104
112
- #. ``( 1, None, ... ) `` indicates line 1 of the file lao was **removed **.
113
- #. ``( None, 2, ... ) `` indicates line 2 of the file tzu was **inserted **.
114
- #. ``( 5, 4, ... ) `` indicates that line 5 of lao and line 4 of tzu are **equal **.
105
+ #. ``( old= 1, new= None, ... ) `` indicates line 1 of the file lao was **removed **.
106
+ #. ``( old= None, new= 2, ... ) `` indicates line 2 of the file tzu was **inserted **.
107
+ #. ``( old= 5, new= 4, ... ) `` indicates that line 5 of lao and line 4 of tzu are **equal **.
115
108
116
109
Please note that not all patch formats provide the actual lines modified, so some
117
110
results will have the text portion of the tuple set to ``None ``.
@@ -124,15 +117,29 @@ To apply a diff to some lines of text, first read the patch and parse it.
124
117
.. code-block :: python
125
118
126
119
>> > import whatthepatch
127
- >> > with open (' somechanges.patch ' ) as f:
120
+ >> > with open (' tests/casefiles/diff-default.diff ' ) as f:
128
121
... text = f.read()
129
122
...
130
- >> > with open (' lao' ) as f:
123
+ >> > with open (' tests/casefiles/ lao' ) as f:
131
124
... lao = f.read()
132
125
...
133
126
>> > diff = [x for x in whatthepatch.parse_patch(text)]
134
127
>> > diff = diff[0 ]
135
128
>> > tzu = whatthepatch.apply_diff(diff, lao)
129
+ >> > tzu # doctest: +NORMALIZE_WHITESPACE
130
+ [' The Nameless is the origin of Heaven and Earth;' ,
131
+ ' The named is the mother of all things.' ,
132
+ ' ' ,
133
+ ' Therefore let there always be non-being,' ,
134
+ ' so we may see their subtlety,' ,
135
+ ' And let there always be being,' ,
136
+ ' so we may see their outcome.' ,
137
+ ' The two are the same,' ,
138
+ ' But after they are produced,' ,
139
+ ' they have different names.' ,
140
+ ' They both may be called deep and profound.' ,
141
+ ' Deeper and more profound,' ,
142
+ ' The door of all subtleties!' ]
136
143
137
144
138
145
Contribute
0 commit comments