Skip to content
Merged
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: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ script:
- coverage run tests/run_tests.py
- coverage report -m
# test distribution packaging
- python -m pypack patch.py
- python -m pypack patch_ng.py
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ Things that don't work out of the box:

### Usage

Download **patch.py** and run it with Python. It is a self-contained
Download **patch_ng.py** and run it with Python. It is a self-contained
module without external dependencies.

patch.py diff.patch
patch_ng.py diff.patch

You can also run the .zip file.

python patch-1.17.zip diff.patch
python patch-ng-1.17.zip diff.patch

### Installation

**patch.py** is self sufficient. You can copy it into your repository
**patch_ng.py** is self sufficient. You can copy it into your repository
and use it from here. This setup will always be repeatable. But if
you need to add `patch` module as a dependency, make sure to use strict
specifiers to avoid hitting an API break when version 2 is released:
Expand Down
4 changes: 2 additions & 2 deletions patch.py → patch_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def tostr(b):

#------------------------------------------------
# Logging is controlled by logger named after the
# module name (e.g. 'patch' for patch.py module)
# module name (e.g. 'patch' for patch_ng.py module)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1247,7 +1247,7 @@ def main():
else:
patch.apply(options.strip, root=options.directory) or sys.exit(-1)

# todo: document and test line ends handling logic - patch.py detects proper line-endings
# todo: document and test line ends handling logic - patch_ng.py detects proper line-endings
# for inserted hunks and issues a warning if patched file has incosistent line ends


Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_requires(filename):
def load_version():
"""Loads a file content"""
filename = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"patch.py"))
"patch_ng.py"))
with open(filename, "rt") as version_file:
content = version_file.read()
version = re.search('__version__ = "([0-9a-z.-]+)"', content).group(1)
Expand Down Expand Up @@ -86,7 +86,7 @@ def load_version():

# Alternatively, if you want to distribute just a my_module.py, uncomment
# this:
py_modules=["patch"],
py_modules=["patch_ng"],

# List run-time dependencies here. These will be installed by pip when
# your project is installed. For an analysis of "install_requires" vs pip's
Expand Down Expand Up @@ -120,7 +120,7 @@ def load_version():
# pip to create the appropriate form of executable for the target platform.
#entry_points={
# 'console_scripts': [
# 'patch.py=patch',
# 'patch_ng.py=patch',
# ],
#},
)
18 changes: 9 additions & 9 deletions tests/05hg_change.from
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ if "-v" in sys.argv or "--verbose" in sys.argv:
tests_dir = dirname(abspath(__file__))


# import patch.py from parent directory
# import patch_ng.py from parent directory
save_path = sys.path
sys.path.insert(0, dirname(tests_dir))
import patch
import patch_ng
sys.path = save_path


Expand All @@ -68,7 +68,7 @@ class TestPatchFiles(unittest.TestCase):
f2.close()
if f1:
f1.close()

def _assert_dirs_equal(self, dir1, dir2, ignore=[]):
""" compare dir1 with reference dir2
.svn dirs are ignored
Expand All @@ -94,23 +94,23 @@ class TestPatchFiles(unittest.TestCase):
continue
self.fail("extra file or directory: %s" % e2)


def _run_test(self, testname):
"""
boilerplate for running *.patch file tests
"""

# 1. create temp test directory
# 2. copy files
# 3. execute file-based patch
# 3. execute file-based patch
# 4. compare results
# 5. cleanup on success

tmpdir = mkdtemp(prefix="%s."%testname)

patch_file = join(tmpdir, "%s.patch" % testname)
shutil.copy(join(tests_dir, "%s.patch" % testname), patch_file)

from_src = join(tests_dir, "%s.from" % testname)
from_tgt = join(tmpdir, "%s.from" % testname)

Expand All @@ -129,7 +129,7 @@ class TestPatchFiles(unittest.TestCase):

# 3.
# test utility as a whole
patch_tool = join(dirname(tests_dir), "patch.py")
patch_tool = join(dirname(tests_dir), "patch_ng.py")
save_cwd = os.getcwdu()
os.chdir(tmpdir)
if verbose:
Expand All @@ -150,7 +150,7 @@ class TestPatchFiles(unittest.TestCase):
# need recursive compare
self._assert_dirs_equal(join(tests_dir, "%s.to" % testname), tmpdir, "%s.patch" % testname)



shutil.rmtree(tmpdir)
return 0
Expand Down Expand Up @@ -200,7 +200,7 @@ class TestCheckPatched(unittest.TestCase):
pto3 = patch.fromfile("03trail_fname.patch")
self.assertEqual(None, pto3.can_patch("03trail_fname.to"))
self.assertEqual(None, pto3.can_patch("not_in_source.also"))

def test_multiline_false_on_other_file(self):
pto = patch.fromfile("01uni_multi.patch")
os.chdir(join(tests_dir, "01uni_multi.from"))
Expand Down
18 changes: 9 additions & 9 deletions tests/05hg_change.to
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ if "-v" in sys.argv or "--verbose" in sys.argv:
tests_dir = dirname(abspath(__file__))


# import patch.py from parent directory
# import patch_ng.py from parent directory
save_path = sys.path
sys.path.insert(0, dirname(tests_dir))
import patch
import patch_ng
sys.path = save_path


Expand All @@ -68,7 +68,7 @@ class TestPatchFiles(unittest.TestCase):
f2.close()
if f1:
f1.close()

def _assert_dirs_equal(self, dir1, dir2, ignore=[]):
""" compare dir1 with reference dir2
.svn dirs are ignored
Expand All @@ -94,23 +94,23 @@ class TestPatchFiles(unittest.TestCase):
continue
self.fail("extra file or directory: %s" % e2)


def _run_test(self, testname):
"""
boilerplate for running *.patch file tests
"""

# 1. create temp test directory
# 2. copy files
# 3. execute file-based patch
# 3. execute file-based patch
# 4. compare results
# 5. cleanup on success

tmpdir = mkdtemp(prefix="%s."%testname)

patch_file = join(tmpdir, "%s.patch" % testname)
shutil.copy(join(tests_dir, "%s.patch" % testname), patch_file)

from_src = join(tests_dir, "%s.from" % testname)
from_tgt = join(tmpdir, "%s.from" % testname)

Expand All @@ -129,7 +129,7 @@ class TestPatchFiles(unittest.TestCase):

# 3.
# test utility as a whole
patch_tool = join(dirname(tests_dir), "patch.py")
patch_tool = join(dirname(tests_dir), "patch_ng.py")
save_cwd = os.getcwdu()
os.chdir(tmpdir)
if verbose:
Expand All @@ -150,7 +150,7 @@ class TestPatchFiles(unittest.TestCase):
# need recursive compare
self._assert_dirs_equal(join(tests_dir, "%s.to" % testname), tmpdir, "%s.patch" % testname)



shutil.rmtree(tmpdir)
return 0
Expand Down Expand Up @@ -200,7 +200,7 @@ class TestCheckPatched(unittest.TestCase):
pto3 = patch.fromfile("03trail_fname.patch")
self.assertEqual(None, pto3.can_patch("03trail_fname.to"))
self.assertEqual(None, pto3.can_patch("not_in_source.also"))

def test_multiline_false_on_other_file(self):
pto = patch.fromfile("01uni_multi.patch")
os.chdir(join(tests_dir, "01uni_multi.from"))
Expand Down
40 changes: 20 additions & 20 deletions tests/data/autofix/absolute-path.diff
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Index: c:/winnt/tests/run_tests.py
===================================================================
--- c:/winnt/tests/run_tests.py (revision 132)
+++ c:/winnt/tests/run_tests.py (working copy)
@@ -240,6 +240,12 @@
Index: c:/winnt/tests/run_tests.py
===================================================================
--- c:/winnt/tests/run_tests.py (revision 132)
+++ c:/winnt/tests/run_tests.py (working copy)
@@ -240,6 +240,12 @@
self.assertNotEqual(pto.parse(fp), True)
fp.close()

+ def test_fail_absolute_path(self):
+ fp = open(join(tests_dir, "data/failing/absolute-path.diff"))
+ res = patch.PatchSet().parse(fp)
Expand All @@ -15,36 +15,36 @@ Index: c:/winnt/tests/run_tests.py
class TestPatchApply(unittest.TestCase):
def setUp(self):
self.save_cwd = os.getcwdu()
Index: c:/winnt/patch.py
===================================================================
--- c:/winnt/patch.py (revision 132)
+++ c:/winnt/patch.py (working copy)
@@ -22,7 +22,7 @@
Index: c:/winnt/patch_ng.py
===================================================================
--- c:/winnt/patch_ng.py (revision 132)
+++ c:/winnt/patch_ng.py (working copy)
@@ -22,7 +22,7 @@
from StringIO import StringIO
import urllib2

-from os.path import exists, isfile, abspath
+from os.path import exists, isabs, isfile, abspath
from os import unlink
@@ -439,7 +439,21 @@


@@ -439,7 +439,21 @@

return (errors == 0)

+ def process_filenames():
+ """ sanitize filenames
+ return True on success
+ """
+ errors = 0

+ for i,p in enumerate(self.items):
+ #
+ #
+
+ # absolute paths are not allowed
+ if isabs(p.source) or isabs(p.target):
+ warning("error: absolute paths are not allowed for patch no.%d" % i)
+
+
+ return (errors == 0)
+
def apply(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/data/autofix/parent-path.diff
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Index: patch.py
Index: patch_ng.py
===================================================================
--- ../patch.py (revision 151)
+++ ../patch.py (working copy)
--- ../patch_ng.py (revision 151)
+++ ../patch_ng.py (working copy)
@@ -13,7 +13,7 @@
"""

__author__ = "techtonik.rainforce.org"
-__version__ = "1.11.10-dev"
+__version__ = "1.11.11-dev"

import copy
import logging
Loading