Skip to content

Commit 9d166dc

Browse files
committed
managing dev/null file add and removal
1 parent c9402be commit 9d166dc

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

example/example.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import logging
3+
from patch import fromfile, fromstring
4+
5+
class PatchLogHandler(logging.Handler):
6+
def __init__(self):
7+
logging.Handler.__init__(self, logging.DEBUG)
8+
9+
def emit(self, record):
10+
logstr = self.format(record)
11+
print logstr
12+
13+
patchlog = logging.getLogger("patch")
14+
patchlog.handlers = []
15+
patchlog.addHandler(PatchLogHandler())
16+
17+
patch = fromstring("""--- /dev/null
18+
+++ b/newfile
19+
@@ -0,0 +0,3 @@
20+
+New file1
21+
+New file2
22+
+New file3
23+
""")
24+
25+
patch.apply(root=os.getcwd(), strip=0)
26+
27+
28+
with open("newfile", "rb") as f:
29+
newfile = f.read()
30+
assert "New file1\nNew file2\nNew file3\n" == newfile
31+
32+
patch = fromstring("""--- a/newfile
33+
+++ /dev/null
34+
@@ -0,3 +0,0 @@
35+
-New file1
36+
-New file2
37+
-New file3
38+
""")
39+
40+
result = patch.apply(root=os.getcwd(), strip=0)
41+
42+
assert os.path.exists("newfile") is False

patch.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,6 @@ def diffstat(self):
800800
% (len(names), sum(insert), sum(delete), delta))
801801
return output
802802

803-
804803
def findfile(self, old, new):
805804
""" return name of file to be patched or None """
806805
if exists(old):
@@ -820,6 +819,10 @@ def findfile(self, old, new):
820819
return new
821820
return None
822821

822+
def _strip_prefix(self, filename):
823+
if filename.startswith(b'a/') or filename.startswith(b'b/'):
824+
return filename[2:]
825+
return filename
823826

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

859862
if not filename:
860-
warning("source/target file does not exist:\n --- %s\n +++ %s" % (old, new))
861-
errors += 1
862-
continue
863+
if "dev/null" in old:
864+
# this is a file creation
865+
filename = self._strip_prefix(new)
866+
# I wish there would be something more clean to get the full contents
867+
new_file = "".join(s[1:] for s in p.hunks[0].text)
868+
with open(filename, "wb") as f:
869+
f.write(new_file)
870+
continue
871+
elif "dev/null" in new:
872+
# this is a file removal
873+
os.remove(self._strip_prefix(old))
874+
continue
875+
else:
876+
warning("source/target file does not exist:\n --- %s\n +++ %s" % (old, new))
877+
errors += 1
878+
continue
863879
if not isfile(filename):
864880
warning("not a file - %s" % filename)
865881
errors += 1

0 commit comments

Comments
 (0)