@@ -48,9 +48,9 @@ Quick start
4848 -- - a/ unidiff/ utils.py
4949 ++ + b/ unidiff/ utils.py
5050 @@ - 37 ,4 + 37 ,3 @@
51- # - deleted line
52- # \ No newline case (ignore)
53- RE_HUNK_BODY_LINE = re.compile(r ' ^ ( [- \+\\ ]) ' )
51+ # - deleted line
52+ # \ No newline case (ignore)
53+ RE_HUNK_BODY_LINE = re.compile(r ' ^ ( [- \+\\ ]) ' )
5454 -
5555
5656
@@ -62,11 +62,6 @@ you can get stats (if it is a new, removed or modified file; the source/target
6262lines; etc), besides having access to each hunk (also like a list) and its
6363respective info.
6464
65- For git diffs, the file mode is exposed through the :code: `source_mode ` and
66- :code: `target_mode ` attributes (e.g. :code: `'100644' `, :code: `'100755' `,
67- :code: `'120000' `), or :code: `None ` when unknown. The :code: `is_symlink `
68- property is a shortcut to detect symbolic links (mode :code: `120000 `).
69-
7065At any point you can get the string representation of the current object, and
7166that will return the unified diff data of it.
7267
@@ -132,6 +127,91 @@ parsing more efficient:
132127 >> > patch = PatchSet.from_filename(' tests/samples/bzr.diff' , encoding = ' utf-8' , metadata_only = True )
133128
134129
130+ Inspecting files, hunks and lines
131+ ---------------------------------
132+
133+ .. code-block :: python
134+
135+ >> > from unidiff import PatchSet
136+ >> > patch = PatchSet.from_string(
137+ ... ' --- a/story.txt\n '
138+ ... ' +++ b/story.txt\n '
139+ ... ' @@ -1,4 +1,4 @@\n '
140+ ... ' Once upon a time\n '
141+ ... ' -there was a bug\n '
142+ ... ' +there was a fix\n '
143+ ... ' the end\n '
144+ ... ' really\n ' )
145+ >> > patched_file = patch[0 ]
146+ >> > patched_file.path
147+ ' story.txt'
148+ >> > patched_file.is_modified_file
149+ True
150+ >> > patched_file.added, patched_file.removed
151+ (1 , 1 )
152+ >> > hunk = patched_file[0 ]
153+ >> > hunk.source_start, hunk.target_start
154+ (1 , 1 )
155+ >> > removed = [line for line in hunk if line.is_removed]
156+ >> > len (removed)
157+ 1
158+ >> > removed[0 ].value
159+ ' there was a bug\n '
160+ >> > removed[0 ].source_line_no
161+ 2
162+ >> > added = [line for line in hunk if line.is_added]
163+ >> > added[0 ].value, added[0 ].target_line_no
164+ (' there was a fix\n ' , 2 )
165+
166+
167+ Git file modes, symlinks and line numbers
168+ ------------------------------------------
169+
170+ For git diffs, the file mode is exposed through the :code: `source_mode ` and
171+ :code: `target_mode ` attributes (e.g. :code: `'100644' `, :code: `'100755' `,
172+ :code: `'120000' `), or :code: `None ` when unknown. The :code: `is_symlink `
173+ property is a shortcut to detect symbolic links (mode :code: `120000 `):
174+
175+ .. code-block :: python
176+
177+ >> > from unidiff import PatchSet
178+ >> > patch = PatchSet.from_filename(' tests/samples/git_symlink.diff' )
179+ >> > patched_file = patch[0 ]
180+ >> > patched_file.path
181+ ' bin/check'
182+ >> > patched_file.is_added_file
183+ True
184+ >> > patched_file.target_mode
185+ ' 120000'
186+ >> > patched_file.is_symlink
187+ True
188+
189+ Each :code: `PatchedFile ` also exposes :code: `diff_line_no `, the 1-based line
190+ number in the diff where its entry starts. This is useful to locate files that
191+ have no hunks, such as binary changes:
192+
193+ .. code-block :: python
194+
195+ >> > from unidiff import PatchSet
196+ >> > patch = PatchSet.from_filename(' tests/samples/debdiff.diff' )
197+ >> > [(f.path, f.is_binary_file, f.diff_line_no) for f in patch]
198+ [(' new/added.txt' , False , 3 ), (' /t/p2/a.png' , True , 6 ), (' /t/p2/b.png' , True , 7 )]
199+
200+
201+ Parsing from bytes
202+ ------------------
203+
204+ :code: `PatchSet ` and :code: `PatchSet.from_string ` also accept :code: `bytes `,
205+ which are decoded using the given :code: `encoding ` (defaulting to UTF-8):
206+
207+ .. code-block :: python
208+
209+ >> > from unidiff import PatchSet
210+ >> > patch = PatchSet(b ' --- a/f\n +++ b/f\n @@ -1,2 +1,2 @@\n hola\n -mundo\n +world\n ' )
211+ >> > patch.added, patch.removed
212+ (1 , 1 )
213+
214+
135215 Diffs with embedded carriage returns or control characters
136216----------------------------------------------------------
137217
0 commit comments