Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pip-log.txt
.coverage
.tox
nosetests.xml
.noseids
.hypothesis

# Translations
*.mo
Expand Down
31 changes: 22 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
sudo: false
language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "3.5"
python: "3.5"
env:
- TOX_ENV=py27
- TOX_ENV=py34
- TOX_ENV=py35
matrix:
include:
- python: 3.6
env: TOX_ENV=py36
include:
- python: 3.7
env: TOX_ENV=py37
include:
- python: 3.7
env: TOX_ENV=lint
include:
- python: 2.7
env: TOX_ENV=lint
addons:
apt:
packages:
- ed
install:
- pip install .
script: nosetests
install: pip install tox
script: tox -e $TOX_ENV
cache: pip
79 changes: 43 additions & 36 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ What The Patch!?
.. image:: https://travis-ci.org/cscorley/whatthepatch.svg?style=flat
:target: https://travis-ci.org/cscorley/whatthepatch

What The Patch!? is a library for parsing patch files. Its only purpose is to
read a patch file and get it into some usable form by other programs.
What The Patch!? is a library for both parsing and applying patch files.

Features
---------
Expand Down Expand Up @@ -71,47 +70,41 @@ each diff in the patch:
.. code-block:: python

>>> import whatthepatch
>>> with open('somechanges.patch') as f:
>>> import pprint
>>> with open('tests/casefiles/diff-unified.diff') as f:
... text = f.read()
...
>>> for diff in whatthepatch.parse_patch(text):
... print(diff)
... print(diff) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
...
diff(header=header(
index_path=None,
old_path='lao',
old_version='2012-12-26 23:16:54.000000000 -0600',
new_path='tzu',
new_version='2012-12-26 23:16:50.000000000 -0600'
),
changes=[
(1, None, 'The Way that can be told of is not the eternal Way;'),
(2, None, 'The name that can be named is not the eternal name.'),
(3, 1, 'The Nameless is the origin of Heaven and Earth;'),
(4, None, 'The Named is the mother of all things.'),
(None, 2, 'The named is the mother of all things.'),
(None, 3, ''),
(5, 4, 'Therefore let there always be non-being,'),
(6, 5, ' so we may see their subtlety,'),
(7, 6, 'And let there always be being,'),
(9, 8, 'The two are the same,'),
(10, 9, 'But after they are produced,'),
(11, 10, ' they have different names.'),
(None, 11, 'They both may be called deep and profound.'),
(None, 12, 'Deeper and more profound,'),
(None, 13, 'The door of all subtleties!')
]
)

*Edited to show structure of the results*
diff(header=header(index_path=None,
old_path='lao',
old_version='2013-01-05 16:56:19.000000000 -0600',
new_path='tzu',
new_version='2013-01-05 16:56:35.000000000 -0600'),
changes=[Change(old=1, new=None, hunk=1, line='The Way that can be told of is not the eternal Way;'),
Change(old=2, new=None, hunk=1, line='The name that can be named is not the eternal name.'),
Change(old=3, new=1, hunk=1, line='The Nameless is the origin of Heaven and Earth;'),
Change(old=4, new=None, hunk=1, line='The Named is the mother of all things.'),
Change(old=None, new=2, hunk=1, line='The named is the mother of all things.'),
Change(old=None, new=3, hunk=1, line=''), Change(old=5, new=4, hunk=1, line='Therefore let there always be non-being,'),
Change(old=6, new=5, hunk=1, line=' so we may see their subtlety,'),
Change(old=7, new=6, hunk=1, line='And let there always be being,'),
Change(old=9, new=8, hunk=2, line='The two are the same,'),
Change(old=10, new=9, hunk=2, line='But after they are produced,'),
Change(old=11, new=10, hunk=2, line=' they have different names.'),
Change(old=None, new=11, hunk=2, line='They both may be called deep and profound.'),
Change(old=None, new=12, hunk=2, line='Deeper and more profound,'),
Change(old=None, new=13, hunk=2, line='The door of all subtleties!')],
text='...')

The changes are listed as they are in the patch, but instead of the +/- syntax
of the patch, we get a tuple of two numbers and the text of the line.
What these numbers indicate are as follows:

#. ``( 1, None, ... )`` indicates line 1 of the file lao was **removed**.
#. ``( None, 2, ... )`` indicates line 2 of the file tzu was **inserted**.
#. ``( 5, 4, ... )`` indicates that line 5 of lao and line 4 of tzu are **equal**.
#. ``( old=1, new=None, ... )`` indicates line 1 of the file lao was **removed**.
#. ``( old=None, new=2, ... )`` indicates line 2 of the file tzu was **inserted**.
#. ``( old=5, new=4, ... )`` indicates that line 5 of lao and line 4 of tzu are **equal**.

Please note that not all patch formats provide the actual lines modified, so some
results will have the text portion of the tuple set to ``None``.
Expand All @@ -124,15 +117,29 @@ To apply a diff to some lines of text, first read the patch and parse it.
.. code-block:: python

>>> import whatthepatch
>>> with open('somechanges.patch') as f:
>>> with open('tests/casefiles/diff-default.diff') as f:
... text = f.read()
...
>>> with open('lao') as f:
>>> with open('tests/casefiles/lao') as f:
... lao = f.read()
...
>>> diff = [x for x in whatthepatch.parse_patch(text)]
>>> diff = diff[0]
>>> tzu = whatthepatch.apply_diff(diff, lao)
>>> tzu # doctest: +NORMALIZE_WHITESPACE
['The Nameless is the origin of Heaven and Earth;',
'The named is the mother of all things.',
'',
'Therefore let there always be non-being,',
' so we may see their subtlety,',
'And let there always be being,',
' so we may see their outcome.',
'The two are the same,',
'But after they are produced,',
' they have different names.',
'They both may be called deep and profound.',
'Deeper and more profound,',
'The door of all subtleties!']


Contribute
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@
"Topic :: Text Processing",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
],
)

19 changes: 19 additions & 0 deletions tests/casefiles/diff-unified-bad.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- lao 2013-01-05 16:56:19.000000000 -0600
+++ tzu 2013-01-05 16:56:35.000000000 -0600
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all tings.
+The named is the mother of all things.
+
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
@@ -9,3 +8,6 @@
The two are the same,
But after they are produced,
they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!
19 changes: 19 additions & 0 deletions tests/casefiles/diff-unified-bad2.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- lao 2013-01-05 16:56:19.000000000 -0600
+++ tzu 2013-01-05 16:56:35.000000000 -0600
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+The named is the mother of all things.
+
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
@@ -9,3 +8,6 @@
The two are te same,
But after they are produced,
they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!
Loading