Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

managing dev/null file add and removal #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
42 changes: 42 additions & 0 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import logging
from patch import fromfile, fromstring

class PatchLogHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self, logging.DEBUG)

def emit(self, record):
logstr = self.format(record)
print logstr

patchlog = logging.getLogger("patch")
patchlog.handlers = []
patchlog.addHandler(PatchLogHandler())

patch = fromstring("""--- /dev/null
+++ b/newfile
@@ -0,0 +0,3 @@
+New file1
+New file2
+New file3
""")

patch.apply(root=os.getcwd(), strip=0)


with open("newfile", "rb") as f:
newfile = f.read()
assert "New file1\nNew file2\nNew file3\n" == newfile

patch = fromstring("""--- a/newfile
+++ /dev/null
@@ -0,3 +0,0 @@
-New file1
-New file2
-New file3
""")

result = patch.apply(root=os.getcwd(), strip=0)

assert os.path.exists("newfile") is False
24 changes: 20 additions & 4 deletions patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,6 @@ def diffstat(self):
% (len(names), sum(insert), sum(delete), delta))
return output


def findfile(self, old, new):
""" return name of file to be patched or None """
if exists(old):
Expand All @@ -820,6 +819,10 @@ def findfile(self, old, new):
return new
return None

def _strip_prefix(self, filename):
if filename.startswith(b'a/') or filename.startswith(b'b/'):
return filename[2:]
return filename

def apply(self, strip=0, root=None):
""" Apply parsed patch, optionally stripping leading components
Expand Down Expand Up @@ -857,9 +860,22 @@ def apply(self, strip=0, root=None):
filename = self.findfile(old, new)

if not filename:
warning("source/target file does not exist:\n --- %s\n +++ %s" % (old, new))
errors += 1
continue
if "dev/null" in old:
# this is a file creation
filename = self._strip_prefix(new)
# I wish there would be something more clean to get the full contents
new_file = "".join(s[1:] for s in p.hunks[0].text)
with open(filename, "wb") as f:
f.write(new_file)
continue
elif "dev/null" in new:
# this is a file removal
os.remove(self._strip_prefix(old))
continue
else:
warning("source/target file does not exist:\n --- %s\n +++ %s" % (old, new))
errors += 1
continue
if not isfile(filename):
warning("not a file - %s" % filename)
errors += 1
Expand Down