Skip to main content

A patch parsing library.

Project description

What The Patch!?

https://travis-ci.org/cscorley/whatthepatch.svg?style=flat

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.

Features

  • Parsing of almost all diff formats (except forwarded ed):

    • normal (default, –normal)

    • copied context (-c, –context)

    • unified context (-u, –unified)

    • ed script (-e, –ed)

    • rcs ed script (-n, –rcs)

  • Parsing of several SCM patches:

    • CVS

    • SVN

    • Git

Installation

To install What The Patch!?, simply:

$ pip install whatthepatch

Usage

Let us say we have a patch file containing some changes, aptly named ‘somechanges.patch’:

--- lao     2012-12-26 23:16:54.000000000 -0600
+++ tzu     2012-12-26 23:16:50.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 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!

Parsing

Here is how we would use What The Patch!? in Python to get the changeset for each diff in the patch:

>>> import whatthepatch
>>> with open('somechanges.patch') as f:
...     text = f.read()
...
>>> for diff in whatthepatch.parse_patch(text):
...     print(diff)
...
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

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. ( 1, None, ... ) indicates line 1 of the file lao was removed.

  2. ( None, 2, ... ) indicates line 2 of the file tzu was inserted.

  3. ( 5, 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.

Applying

To apply a diff to some lines of text, first read the patch and parse it.

>>> import whatthepatch
>>> with open('somechanges.patch') as f:
...     text = f.read()
...
>>> with open('lao') as f:
...     lao = f.read()
...
>>> diff = [x for x in whatthepatch.parse_patch(text)]
>>> diff = diff[0]
>>> tzu = whatthepatch.apply_diff(diff, lao)

Contribute

  1. Fork this repository

  2. Create a new branch to work on

  3. Commit your tests and/or changes

  4. Push and create a pull request here!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

whatthepatch-0.0.4.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whatthepatch-0.0.4-py2.py3-none-any.whl (12.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file whatthepatch-0.0.4.tar.gz.

File metadata

  • Download URL: whatthepatch-0.0.4.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for whatthepatch-0.0.4.tar.gz
Algorithm Hash digest
SHA256 091cc0111e9361b2ba1925eef41b4b3aeeb83f19b06ff031870f75a4423d0de0
MD5 ff400ca6240e8b5cf9011d08152fd9ad
BLAKE2b-256 12a14b8a0cb58402a790ceda78ef4af9614e164979675bd052230d6326af1b0e

See more details on using hashes here.

File details

Details for the file whatthepatch-0.0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for whatthepatch-0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3112815ed7c5e4d5dd77add7fa0b9f84b2792ac57bc1b8998f4e544344512e30
MD5 9b2d53534b8bc4128d56019be4bf0c53
BLAKE2b-256 fd7209a9ba36f9a77ec136d60f78676d2e91bb2656314f91910e8041b15ededf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page