##// END OF EJS Templates
patch: display a nice error for invalid base85 data...
Patrick Mezard -
r16522:a8065323 stable
parent child Browse files
Show More
@@ -1,180 +1,180 b''
1 /*
1 /*
2 base85 codec
2 base85 codec
3
3
4 Copyright 2006 Brendan Cully <brendan@kublai.com>
4 Copyright 2006 Brendan Cully <brendan@kublai.com>
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8
8
9 Largely based on git's implementation
9 Largely based on git's implementation
10 */
10 */
11
11
12 #include <Python.h>
12 #include <Python.h>
13
13
14 #include "util.h"
14 #include "util.h"
15
15
16 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
17 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
17 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
18 static char b85dec[256];
18 static char b85dec[256];
19
19
20 static void
20 static void
21 b85prep(void)
21 b85prep(void)
22 {
22 {
23 int i;
23 int i;
24
24
25 memset(b85dec, 0, sizeof(b85dec));
25 memset(b85dec, 0, sizeof(b85dec));
26 for (i = 0; i < sizeof(b85chars); i++)
26 for (i = 0; i < sizeof(b85chars); i++)
27 b85dec[(int)(b85chars[i])] = i + 1;
27 b85dec[(int)(b85chars[i])] = i + 1;
28 }
28 }
29
29
30 static PyObject *
30 static PyObject *
31 b85encode(PyObject *self, PyObject *args)
31 b85encode(PyObject *self, PyObject *args)
32 {
32 {
33 const unsigned char *text;
33 const unsigned char *text;
34 PyObject *out;
34 PyObject *out;
35 char *dst;
35 char *dst;
36 int len, olen, i;
36 int len, olen, i;
37 unsigned int acc, val, ch;
37 unsigned int acc, val, ch;
38 int pad = 0;
38 int pad = 0;
39
39
40 if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
40 if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
41 return NULL;
41 return NULL;
42
42
43 if (pad)
43 if (pad)
44 olen = ((len + 3) / 4 * 5) - 3;
44 olen = ((len + 3) / 4 * 5) - 3;
45 else {
45 else {
46 olen = len % 4;
46 olen = len % 4;
47 if (olen)
47 if (olen)
48 olen++;
48 olen++;
49 olen += len / 4 * 5;
49 olen += len / 4 * 5;
50 }
50 }
51 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
51 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
52 return NULL;
52 return NULL;
53
53
54 dst = PyBytes_AsString(out);
54 dst = PyBytes_AsString(out);
55
55
56 while (len) {
56 while (len) {
57 acc = 0;
57 acc = 0;
58 for (i = 24; i >= 0; i -= 8) {
58 for (i = 24; i >= 0; i -= 8) {
59 ch = *text++;
59 ch = *text++;
60 acc |= ch << i;
60 acc |= ch << i;
61 if (--len == 0)
61 if (--len == 0)
62 break;
62 break;
63 }
63 }
64 for (i = 4; i >= 0; i--) {
64 for (i = 4; i >= 0; i--) {
65 val = acc % 85;
65 val = acc % 85;
66 acc /= 85;
66 acc /= 85;
67 dst[i] = b85chars[val];
67 dst[i] = b85chars[val];
68 }
68 }
69 dst += 5;
69 dst += 5;
70 }
70 }
71
71
72 if (!pad)
72 if (!pad)
73 _PyBytes_Resize(&out, olen);
73 _PyBytes_Resize(&out, olen);
74
74
75 return out;
75 return out;
76 }
76 }
77
77
78 static PyObject *
78 static PyObject *
79 b85decode(PyObject *self, PyObject *args)
79 b85decode(PyObject *self, PyObject *args)
80 {
80 {
81 PyObject *out;
81 PyObject *out;
82 const char *text;
82 const char *text;
83 char *dst;
83 char *dst;
84 int len, i, j, olen, c, cap;
84 int len, i, j, olen, c, cap;
85 unsigned int acc;
85 unsigned int acc;
86
86
87 if (!PyArg_ParseTuple(args, "s#", &text, &len))
87 if (!PyArg_ParseTuple(args, "s#", &text, &len))
88 return NULL;
88 return NULL;
89
89
90 olen = len / 5 * 4;
90 olen = len / 5 * 4;
91 i = len % 5;
91 i = len % 5;
92 if (i)
92 if (i)
93 olen += i - 1;
93 olen += i - 1;
94 if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
94 if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
95 return NULL;
95 return NULL;
96
96
97 dst = PyBytes_AsString(out);
97 dst = PyBytes_AsString(out);
98
98
99 i = 0;
99 i = 0;
100 while (i < len)
100 while (i < len)
101 {
101 {
102 acc = 0;
102 acc = 0;
103 cap = len - i - 1;
103 cap = len - i - 1;
104 if (cap > 4)
104 if (cap > 4)
105 cap = 4;
105 cap = 4;
106 for (j = 0; j < cap; i++, j++)
106 for (j = 0; j < cap; i++, j++)
107 {
107 {
108 c = b85dec[(int)*text++] - 1;
108 c = b85dec[(int)*text++] - 1;
109 if (c < 0)
109 if (c < 0)
110 return PyErr_Format(
110 return PyErr_Format(
111 PyExc_ValueError,
111 PyExc_ValueError,
112 "Bad base85 character at position %d", i);
112 "bad base85 character at position %d", i);
113 acc = acc * 85 + c;
113 acc = acc * 85 + c;
114 }
114 }
115 if (i++ < len)
115 if (i++ < len)
116 {
116 {
117 c = b85dec[(int)*text++] - 1;
117 c = b85dec[(int)*text++] - 1;
118 if (c < 0)
118 if (c < 0)
119 return PyErr_Format(
119 return PyErr_Format(
120 PyExc_ValueError,
120 PyExc_ValueError,
121 "Bad base85 character at position %d", i);
121 "bad base85 character at position %d", i);
122 /* overflow detection: 0xffffffff == "|NsC0",
122 /* overflow detection: 0xffffffff == "|NsC0",
123 * "|NsC" == 0x03030303 */
123 * "|NsC" == 0x03030303 */
124 if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
124 if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
125 return PyErr_Format(
125 return PyErr_Format(
126 PyExc_ValueError,
126 PyExc_ValueError,
127 "Bad base85 sequence at position %d", i);
127 "bad base85 sequence at position %d", i);
128 acc += c;
128 acc += c;
129 }
129 }
130
130
131 cap = olen < 4 ? olen : 4;
131 cap = olen < 4 ? olen : 4;
132 olen -= cap;
132 olen -= cap;
133 for (j = 0; j < 4 - cap; j++)
133 for (j = 0; j < 4 - cap; j++)
134 acc *= 85;
134 acc *= 85;
135 if (cap && cap < 4)
135 if (cap && cap < 4)
136 acc += 0xffffff >> (cap - 1) * 8;
136 acc += 0xffffff >> (cap - 1) * 8;
137 for (j = 0; j < cap; j++)
137 for (j = 0; j < cap; j++)
138 {
138 {
139 acc = (acc << 8) | (acc >> 24);
139 acc = (acc << 8) | (acc >> 24);
140 *dst++ = acc;
140 *dst++ = acc;
141 }
141 }
142 }
142 }
143
143
144 return out;
144 return out;
145 }
145 }
146
146
147 static char base85_doc[] = "Base85 Data Encoding";
147 static char base85_doc[] = "Base85 Data Encoding";
148
148
149 static PyMethodDef methods[] = {
149 static PyMethodDef methods[] = {
150 {"b85encode", b85encode, METH_VARARGS,
150 {"b85encode", b85encode, METH_VARARGS,
151 "Encode text in base85.\n\n"
151 "Encode text in base85.\n\n"
152 "If the second parameter is true, pad the result to a multiple of "
152 "If the second parameter is true, pad the result to a multiple of "
153 "five characters.\n"},
153 "five characters.\n"},
154 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
154 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
155 {NULL, NULL}
155 {NULL, NULL}
156 };
156 };
157
157
158 #ifdef IS_PY3K
158 #ifdef IS_PY3K
159 static struct PyModuleDef base85_module = {
159 static struct PyModuleDef base85_module = {
160 PyModuleDef_HEAD_INIT,
160 PyModuleDef_HEAD_INIT,
161 "base85",
161 "base85",
162 base85_doc,
162 base85_doc,
163 -1,
163 -1,
164 methods
164 methods
165 };
165 };
166
166
167 PyMODINIT_FUNC PyInit_base85(void)
167 PyMODINIT_FUNC PyInit_base85(void)
168 {
168 {
169 b85prep();
169 b85prep();
170
170
171 return PyModule_Create(&base85_module);
171 return PyModule_Create(&base85_module);
172 }
172 }
173 #else
173 #else
174 PyMODINIT_FUNC initbase85(void)
174 PyMODINIT_FUNC initbase85(void)
175 {
175 {
176 Py_InitModule3("base85", methods, base85_doc);
176 Py_InitModule3("base85", methods, base85_doc);
177
177
178 b85prep();
178 b85prep();
179 }
179 }
180 #endif
180 #endif
@@ -1,1879 +1,1883 b''
1 # patch.py - patch file parsing routines
1 # patch.py - patch file parsing routines
2 #
2 #
3 # Copyright 2006 Brendan Cully <brendan@kublai.com>
3 # Copyright 2006 Brendan Cully <brendan@kublai.com>
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com>
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 import cStringIO, email.Parser, os, errno, re
9 import cStringIO, email.Parser, os, errno, re
10 import tempfile, zlib, shutil
10 import tempfile, zlib, shutil
11
11
12 from i18n import _
12 from i18n import _
13 from node import hex, nullid, short
13 from node import hex, nullid, short
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
15 import context
15 import context
16
16
17 gitre = re.compile('diff --git a/(.*) b/(.*)')
17 gitre = re.compile('diff --git a/(.*) b/(.*)')
18
18
19 class PatchError(Exception):
19 class PatchError(Exception):
20 pass
20 pass
21
21
22
22
23 # public functions
23 # public functions
24
24
25 def split(stream):
25 def split(stream):
26 '''return an iterator of individual patches from a stream'''
26 '''return an iterator of individual patches from a stream'''
27 def isheader(line, inheader):
27 def isheader(line, inheader):
28 if inheader and line[0] in (' ', '\t'):
28 if inheader and line[0] in (' ', '\t'):
29 # continuation
29 # continuation
30 return True
30 return True
31 if line[0] in (' ', '-', '+'):
31 if line[0] in (' ', '-', '+'):
32 # diff line - don't check for header pattern in there
32 # diff line - don't check for header pattern in there
33 return False
33 return False
34 l = line.split(': ', 1)
34 l = line.split(': ', 1)
35 return len(l) == 2 and ' ' not in l[0]
35 return len(l) == 2 and ' ' not in l[0]
36
36
37 def chunk(lines):
37 def chunk(lines):
38 return cStringIO.StringIO(''.join(lines))
38 return cStringIO.StringIO(''.join(lines))
39
39
40 def hgsplit(stream, cur):
40 def hgsplit(stream, cur):
41 inheader = True
41 inheader = True
42
42
43 for line in stream:
43 for line in stream:
44 if not line.strip():
44 if not line.strip():
45 inheader = False
45 inheader = False
46 if not inheader and line.startswith('# HG changeset patch'):
46 if not inheader and line.startswith('# HG changeset patch'):
47 yield chunk(cur)
47 yield chunk(cur)
48 cur = []
48 cur = []
49 inheader = True
49 inheader = True
50
50
51 cur.append(line)
51 cur.append(line)
52
52
53 if cur:
53 if cur:
54 yield chunk(cur)
54 yield chunk(cur)
55
55
56 def mboxsplit(stream, cur):
56 def mboxsplit(stream, cur):
57 for line in stream:
57 for line in stream:
58 if line.startswith('From '):
58 if line.startswith('From '):
59 for c in split(chunk(cur[1:])):
59 for c in split(chunk(cur[1:])):
60 yield c
60 yield c
61 cur = []
61 cur = []
62
62
63 cur.append(line)
63 cur.append(line)
64
64
65 if cur:
65 if cur:
66 for c in split(chunk(cur[1:])):
66 for c in split(chunk(cur[1:])):
67 yield c
67 yield c
68
68
69 def mimesplit(stream, cur):
69 def mimesplit(stream, cur):
70 def msgfp(m):
70 def msgfp(m):
71 fp = cStringIO.StringIO()
71 fp = cStringIO.StringIO()
72 g = email.Generator.Generator(fp, mangle_from_=False)
72 g = email.Generator.Generator(fp, mangle_from_=False)
73 g.flatten(m)
73 g.flatten(m)
74 fp.seek(0)
74 fp.seek(0)
75 return fp
75 return fp
76
76
77 for line in stream:
77 for line in stream:
78 cur.append(line)
78 cur.append(line)
79 c = chunk(cur)
79 c = chunk(cur)
80
80
81 m = email.Parser.Parser().parse(c)
81 m = email.Parser.Parser().parse(c)
82 if not m.is_multipart():
82 if not m.is_multipart():
83 yield msgfp(m)
83 yield msgfp(m)
84 else:
84 else:
85 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
85 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
86 for part in m.walk():
86 for part in m.walk():
87 ct = part.get_content_type()
87 ct = part.get_content_type()
88 if ct not in ok_types:
88 if ct not in ok_types:
89 continue
89 continue
90 yield msgfp(part)
90 yield msgfp(part)
91
91
92 def headersplit(stream, cur):
92 def headersplit(stream, cur):
93 inheader = False
93 inheader = False
94
94
95 for line in stream:
95 for line in stream:
96 if not inheader and isheader(line, inheader):
96 if not inheader and isheader(line, inheader):
97 yield chunk(cur)
97 yield chunk(cur)
98 cur = []
98 cur = []
99 inheader = True
99 inheader = True
100 if inheader and not isheader(line, inheader):
100 if inheader and not isheader(line, inheader):
101 inheader = False
101 inheader = False
102
102
103 cur.append(line)
103 cur.append(line)
104
104
105 if cur:
105 if cur:
106 yield chunk(cur)
106 yield chunk(cur)
107
107
108 def remainder(cur):
108 def remainder(cur):
109 yield chunk(cur)
109 yield chunk(cur)
110
110
111 class fiter(object):
111 class fiter(object):
112 def __init__(self, fp):
112 def __init__(self, fp):
113 self.fp = fp
113 self.fp = fp
114
114
115 def __iter__(self):
115 def __iter__(self):
116 return self
116 return self
117
117
118 def next(self):
118 def next(self):
119 l = self.fp.readline()
119 l = self.fp.readline()
120 if not l:
120 if not l:
121 raise StopIteration
121 raise StopIteration
122 return l
122 return l
123
123
124 inheader = False
124 inheader = False
125 cur = []
125 cur = []
126
126
127 mimeheaders = ['content-type']
127 mimeheaders = ['content-type']
128
128
129 if not util.safehasattr(stream, 'next'):
129 if not util.safehasattr(stream, 'next'):
130 # http responses, for example, have readline but not next
130 # http responses, for example, have readline but not next
131 stream = fiter(stream)
131 stream = fiter(stream)
132
132
133 for line in stream:
133 for line in stream:
134 cur.append(line)
134 cur.append(line)
135 if line.startswith('# HG changeset patch'):
135 if line.startswith('# HG changeset patch'):
136 return hgsplit(stream, cur)
136 return hgsplit(stream, cur)
137 elif line.startswith('From '):
137 elif line.startswith('From '):
138 return mboxsplit(stream, cur)
138 return mboxsplit(stream, cur)
139 elif isheader(line, inheader):
139 elif isheader(line, inheader):
140 inheader = True
140 inheader = True
141 if line.split(':', 1)[0].lower() in mimeheaders:
141 if line.split(':', 1)[0].lower() in mimeheaders:
142 # let email parser handle this
142 # let email parser handle this
143 return mimesplit(stream, cur)
143 return mimesplit(stream, cur)
144 elif line.startswith('--- ') and inheader:
144 elif line.startswith('--- ') and inheader:
145 # No evil headers seen by diff start, split by hand
145 # No evil headers seen by diff start, split by hand
146 return headersplit(stream, cur)
146 return headersplit(stream, cur)
147 # Not enough info, keep reading
147 # Not enough info, keep reading
148
148
149 # if we are here, we have a very plain patch
149 # if we are here, we have a very plain patch
150 return remainder(cur)
150 return remainder(cur)
151
151
152 def extract(ui, fileobj):
152 def extract(ui, fileobj):
153 '''extract patch from data read from fileobj.
153 '''extract patch from data read from fileobj.
154
154
155 patch can be a normal patch or contained in an email message.
155 patch can be a normal patch or contained in an email message.
156
156
157 return tuple (filename, message, user, date, branch, node, p1, p2).
157 return tuple (filename, message, user, date, branch, node, p1, p2).
158 Any item in the returned tuple can be None. If filename is None,
158 Any item in the returned tuple can be None. If filename is None,
159 fileobj did not contain a patch. Caller must unlink filename when done.'''
159 fileobj did not contain a patch. Caller must unlink filename when done.'''
160
160
161 # attempt to detect the start of a patch
161 # attempt to detect the start of a patch
162 # (this heuristic is borrowed from quilt)
162 # (this heuristic is borrowed from quilt)
163 diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |'
163 diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |'
164 r'retrieving revision [0-9]+(\.[0-9]+)*$|'
164 r'retrieving revision [0-9]+(\.[0-9]+)*$|'
165 r'---[ \t].*?^\+\+\+[ \t]|'
165 r'---[ \t].*?^\+\+\+[ \t]|'
166 r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL)
166 r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL)
167
167
168 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
168 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
169 tmpfp = os.fdopen(fd, 'w')
169 tmpfp = os.fdopen(fd, 'w')
170 try:
170 try:
171 msg = email.Parser.Parser().parse(fileobj)
171 msg = email.Parser.Parser().parse(fileobj)
172
172
173 subject = msg['Subject']
173 subject = msg['Subject']
174 user = msg['From']
174 user = msg['From']
175 if not subject and not user:
175 if not subject and not user:
176 # Not an email, restore parsed headers if any
176 # Not an email, restore parsed headers if any
177 subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n'
177 subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n'
178
178
179 gitsendmail = 'git-send-email' in msg.get('X-Mailer', '')
179 gitsendmail = 'git-send-email' in msg.get('X-Mailer', '')
180 # should try to parse msg['Date']
180 # should try to parse msg['Date']
181 date = None
181 date = None
182 nodeid = None
182 nodeid = None
183 branch = None
183 branch = None
184 parents = []
184 parents = []
185
185
186 if subject:
186 if subject:
187 if subject.startswith('[PATCH'):
187 if subject.startswith('[PATCH'):
188 pend = subject.find(']')
188 pend = subject.find(']')
189 if pend >= 0:
189 if pend >= 0:
190 subject = subject[pend + 1:].lstrip()
190 subject = subject[pend + 1:].lstrip()
191 subject = re.sub(r'\n[ \t]+', ' ', subject)
191 subject = re.sub(r'\n[ \t]+', ' ', subject)
192 ui.debug('Subject: %s\n' % subject)
192 ui.debug('Subject: %s\n' % subject)
193 if user:
193 if user:
194 ui.debug('From: %s\n' % user)
194 ui.debug('From: %s\n' % user)
195 diffs_seen = 0
195 diffs_seen = 0
196 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
196 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
197 message = ''
197 message = ''
198 for part in msg.walk():
198 for part in msg.walk():
199 content_type = part.get_content_type()
199 content_type = part.get_content_type()
200 ui.debug('Content-Type: %s\n' % content_type)
200 ui.debug('Content-Type: %s\n' % content_type)
201 if content_type not in ok_types:
201 if content_type not in ok_types:
202 continue
202 continue
203 payload = part.get_payload(decode=True)
203 payload = part.get_payload(decode=True)
204 m = diffre.search(payload)
204 m = diffre.search(payload)
205 if m:
205 if m:
206 hgpatch = False
206 hgpatch = False
207 hgpatchheader = False
207 hgpatchheader = False
208 ignoretext = False
208 ignoretext = False
209
209
210 ui.debug('found patch at byte %d\n' % m.start(0))
210 ui.debug('found patch at byte %d\n' % m.start(0))
211 diffs_seen += 1
211 diffs_seen += 1
212 cfp = cStringIO.StringIO()
212 cfp = cStringIO.StringIO()
213 for line in payload[:m.start(0)].splitlines():
213 for line in payload[:m.start(0)].splitlines():
214 if line.startswith('# HG changeset patch') and not hgpatch:
214 if line.startswith('# HG changeset patch') and not hgpatch:
215 ui.debug('patch generated by hg export\n')
215 ui.debug('patch generated by hg export\n')
216 hgpatch = True
216 hgpatch = True
217 hgpatchheader = True
217 hgpatchheader = True
218 # drop earlier commit message content
218 # drop earlier commit message content
219 cfp.seek(0)
219 cfp.seek(0)
220 cfp.truncate()
220 cfp.truncate()
221 subject = None
221 subject = None
222 elif hgpatchheader:
222 elif hgpatchheader:
223 if line.startswith('# User '):
223 if line.startswith('# User '):
224 user = line[7:]
224 user = line[7:]
225 ui.debug('From: %s\n' % user)
225 ui.debug('From: %s\n' % user)
226 elif line.startswith("# Date "):
226 elif line.startswith("# Date "):
227 date = line[7:]
227 date = line[7:]
228 elif line.startswith("# Branch "):
228 elif line.startswith("# Branch "):
229 branch = line[9:]
229 branch = line[9:]
230 elif line.startswith("# Node ID "):
230 elif line.startswith("# Node ID "):
231 nodeid = line[10:]
231 nodeid = line[10:]
232 elif line.startswith("# Parent "):
232 elif line.startswith("# Parent "):
233 parents.append(line[9:].lstrip())
233 parents.append(line[9:].lstrip())
234 elif not line.startswith("# "):
234 elif not line.startswith("# "):
235 hgpatchheader = False
235 hgpatchheader = False
236 elif line == '---' and gitsendmail:
236 elif line == '---' and gitsendmail:
237 ignoretext = True
237 ignoretext = True
238 if not hgpatchheader and not ignoretext:
238 if not hgpatchheader and not ignoretext:
239 cfp.write(line)
239 cfp.write(line)
240 cfp.write('\n')
240 cfp.write('\n')
241 message = cfp.getvalue()
241 message = cfp.getvalue()
242 if tmpfp:
242 if tmpfp:
243 tmpfp.write(payload)
243 tmpfp.write(payload)
244 if not payload.endswith('\n'):
244 if not payload.endswith('\n'):
245 tmpfp.write('\n')
245 tmpfp.write('\n')
246 elif not diffs_seen and message and content_type == 'text/plain':
246 elif not diffs_seen and message and content_type == 'text/plain':
247 message += '\n' + payload
247 message += '\n' + payload
248 except:
248 except:
249 tmpfp.close()
249 tmpfp.close()
250 os.unlink(tmpname)
250 os.unlink(tmpname)
251 raise
251 raise
252
252
253 if subject and not message.startswith(subject):
253 if subject and not message.startswith(subject):
254 message = '%s\n%s' % (subject, message)
254 message = '%s\n%s' % (subject, message)
255 tmpfp.close()
255 tmpfp.close()
256 if not diffs_seen:
256 if not diffs_seen:
257 os.unlink(tmpname)
257 os.unlink(tmpname)
258 return None, message, user, date, branch, None, None, None
258 return None, message, user, date, branch, None, None, None
259 p1 = parents and parents.pop(0) or None
259 p1 = parents and parents.pop(0) or None
260 p2 = parents and parents.pop(0) or None
260 p2 = parents and parents.pop(0) or None
261 return tmpname, message, user, date, branch, nodeid, p1, p2
261 return tmpname, message, user, date, branch, nodeid, p1, p2
262
262
263 class patchmeta(object):
263 class patchmeta(object):
264 """Patched file metadata
264 """Patched file metadata
265
265
266 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
266 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
267 or COPY. 'path' is patched file path. 'oldpath' is set to the
267 or COPY. 'path' is patched file path. 'oldpath' is set to the
268 origin file when 'op' is either COPY or RENAME, None otherwise. If
268 origin file when 'op' is either COPY or RENAME, None otherwise. If
269 file mode is changed, 'mode' is a tuple (islink, isexec) where
269 file mode is changed, 'mode' is a tuple (islink, isexec) where
270 'islink' is True if the file is a symlink and 'isexec' is True if
270 'islink' is True if the file is a symlink and 'isexec' is True if
271 the file is executable. Otherwise, 'mode' is None.
271 the file is executable. Otherwise, 'mode' is None.
272 """
272 """
273 def __init__(self, path):
273 def __init__(self, path):
274 self.path = path
274 self.path = path
275 self.oldpath = None
275 self.oldpath = None
276 self.mode = None
276 self.mode = None
277 self.op = 'MODIFY'
277 self.op = 'MODIFY'
278 self.binary = False
278 self.binary = False
279
279
280 def setmode(self, mode):
280 def setmode(self, mode):
281 islink = mode & 020000
281 islink = mode & 020000
282 isexec = mode & 0100
282 isexec = mode & 0100
283 self.mode = (islink, isexec)
283 self.mode = (islink, isexec)
284
284
285 def copy(self):
285 def copy(self):
286 other = patchmeta(self.path)
286 other = patchmeta(self.path)
287 other.oldpath = self.oldpath
287 other.oldpath = self.oldpath
288 other.mode = self.mode
288 other.mode = self.mode
289 other.op = self.op
289 other.op = self.op
290 other.binary = self.binary
290 other.binary = self.binary
291 return other
291 return other
292
292
293 def _ispatchinga(self, afile):
293 def _ispatchinga(self, afile):
294 if afile == '/dev/null':
294 if afile == '/dev/null':
295 return self.op == 'ADD'
295 return self.op == 'ADD'
296 return afile == 'a/' + (self.oldpath or self.path)
296 return afile == 'a/' + (self.oldpath or self.path)
297
297
298 def _ispatchingb(self, bfile):
298 def _ispatchingb(self, bfile):
299 if bfile == '/dev/null':
299 if bfile == '/dev/null':
300 return self.op == 'DELETE'
300 return self.op == 'DELETE'
301 return bfile == 'b/' + self.path
301 return bfile == 'b/' + self.path
302
302
303 def ispatching(self, afile, bfile):
303 def ispatching(self, afile, bfile):
304 return self._ispatchinga(afile) and self._ispatchingb(bfile)
304 return self._ispatchinga(afile) and self._ispatchingb(bfile)
305
305
306 def __repr__(self):
306 def __repr__(self):
307 return "<patchmeta %s %r>" % (self.op, self.path)
307 return "<patchmeta %s %r>" % (self.op, self.path)
308
308
309 def readgitpatch(lr):
309 def readgitpatch(lr):
310 """extract git-style metadata about patches from <patchname>"""
310 """extract git-style metadata about patches from <patchname>"""
311
311
312 # Filter patch for git information
312 # Filter patch for git information
313 gp = None
313 gp = None
314 gitpatches = []
314 gitpatches = []
315 for line in lr:
315 for line in lr:
316 line = line.rstrip(' \r\n')
316 line = line.rstrip(' \r\n')
317 if line.startswith('diff --git'):
317 if line.startswith('diff --git'):
318 m = gitre.match(line)
318 m = gitre.match(line)
319 if m:
319 if m:
320 if gp:
320 if gp:
321 gitpatches.append(gp)
321 gitpatches.append(gp)
322 dst = m.group(2)
322 dst = m.group(2)
323 gp = patchmeta(dst)
323 gp = patchmeta(dst)
324 elif gp:
324 elif gp:
325 if line.startswith('--- '):
325 if line.startswith('--- '):
326 gitpatches.append(gp)
326 gitpatches.append(gp)
327 gp = None
327 gp = None
328 continue
328 continue
329 if line.startswith('rename from '):
329 if line.startswith('rename from '):
330 gp.op = 'RENAME'
330 gp.op = 'RENAME'
331 gp.oldpath = line[12:]
331 gp.oldpath = line[12:]
332 elif line.startswith('rename to '):
332 elif line.startswith('rename to '):
333 gp.path = line[10:]
333 gp.path = line[10:]
334 elif line.startswith('copy from '):
334 elif line.startswith('copy from '):
335 gp.op = 'COPY'
335 gp.op = 'COPY'
336 gp.oldpath = line[10:]
336 gp.oldpath = line[10:]
337 elif line.startswith('copy to '):
337 elif line.startswith('copy to '):
338 gp.path = line[8:]
338 gp.path = line[8:]
339 elif line.startswith('deleted file'):
339 elif line.startswith('deleted file'):
340 gp.op = 'DELETE'
340 gp.op = 'DELETE'
341 elif line.startswith('new file mode '):
341 elif line.startswith('new file mode '):
342 gp.op = 'ADD'
342 gp.op = 'ADD'
343 gp.setmode(int(line[-6:], 8))
343 gp.setmode(int(line[-6:], 8))
344 elif line.startswith('new mode '):
344 elif line.startswith('new mode '):
345 gp.setmode(int(line[-6:], 8))
345 gp.setmode(int(line[-6:], 8))
346 elif line.startswith('GIT binary patch'):
346 elif line.startswith('GIT binary patch'):
347 gp.binary = True
347 gp.binary = True
348 if gp:
348 if gp:
349 gitpatches.append(gp)
349 gitpatches.append(gp)
350
350
351 return gitpatches
351 return gitpatches
352
352
353 class linereader(object):
353 class linereader(object):
354 # simple class to allow pushing lines back into the input stream
354 # simple class to allow pushing lines back into the input stream
355 def __init__(self, fp):
355 def __init__(self, fp):
356 self.fp = fp
356 self.fp = fp
357 self.buf = []
357 self.buf = []
358
358
359 def push(self, line):
359 def push(self, line):
360 if line is not None:
360 if line is not None:
361 self.buf.append(line)
361 self.buf.append(line)
362
362
363 def readline(self):
363 def readline(self):
364 if self.buf:
364 if self.buf:
365 l = self.buf[0]
365 l = self.buf[0]
366 del self.buf[0]
366 del self.buf[0]
367 return l
367 return l
368 return self.fp.readline()
368 return self.fp.readline()
369
369
370 def __iter__(self):
370 def __iter__(self):
371 while True:
371 while True:
372 l = self.readline()
372 l = self.readline()
373 if not l:
373 if not l:
374 break
374 break
375 yield l
375 yield l
376
376
377 class abstractbackend(object):
377 class abstractbackend(object):
378 def __init__(self, ui):
378 def __init__(self, ui):
379 self.ui = ui
379 self.ui = ui
380
380
381 def getfile(self, fname):
381 def getfile(self, fname):
382 """Return target file data and flags as a (data, (islink,
382 """Return target file data and flags as a (data, (islink,
383 isexec)) tuple.
383 isexec)) tuple.
384 """
384 """
385 raise NotImplementedError
385 raise NotImplementedError
386
386
387 def setfile(self, fname, data, mode, copysource):
387 def setfile(self, fname, data, mode, copysource):
388 """Write data to target file fname and set its mode. mode is a
388 """Write data to target file fname and set its mode. mode is a
389 (islink, isexec) tuple. If data is None, the file content should
389 (islink, isexec) tuple. If data is None, the file content should
390 be left unchanged. If the file is modified after being copied,
390 be left unchanged. If the file is modified after being copied,
391 copysource is set to the original file name.
391 copysource is set to the original file name.
392 """
392 """
393 raise NotImplementedError
393 raise NotImplementedError
394
394
395 def unlink(self, fname):
395 def unlink(self, fname):
396 """Unlink target file."""
396 """Unlink target file."""
397 raise NotImplementedError
397 raise NotImplementedError
398
398
399 def writerej(self, fname, failed, total, lines):
399 def writerej(self, fname, failed, total, lines):
400 """Write rejected lines for fname. total is the number of hunks
400 """Write rejected lines for fname. total is the number of hunks
401 which failed to apply and total the total number of hunks for this
401 which failed to apply and total the total number of hunks for this
402 files.
402 files.
403 """
403 """
404 pass
404 pass
405
405
406 def exists(self, fname):
406 def exists(self, fname):
407 raise NotImplementedError
407 raise NotImplementedError
408
408
409 class fsbackend(abstractbackend):
409 class fsbackend(abstractbackend):
410 def __init__(self, ui, basedir):
410 def __init__(self, ui, basedir):
411 super(fsbackend, self).__init__(ui)
411 super(fsbackend, self).__init__(ui)
412 self.opener = scmutil.opener(basedir)
412 self.opener = scmutil.opener(basedir)
413
413
414 def _join(self, f):
414 def _join(self, f):
415 return os.path.join(self.opener.base, f)
415 return os.path.join(self.opener.base, f)
416
416
417 def getfile(self, fname):
417 def getfile(self, fname):
418 path = self._join(fname)
418 path = self._join(fname)
419 if os.path.islink(path):
419 if os.path.islink(path):
420 return (os.readlink(path), (True, False))
420 return (os.readlink(path), (True, False))
421 isexec = False
421 isexec = False
422 try:
422 try:
423 isexec = os.lstat(path).st_mode & 0100 != 0
423 isexec = os.lstat(path).st_mode & 0100 != 0
424 except OSError, e:
424 except OSError, e:
425 if e.errno != errno.ENOENT:
425 if e.errno != errno.ENOENT:
426 raise
426 raise
427 return (self.opener.read(fname), (False, isexec))
427 return (self.opener.read(fname), (False, isexec))
428
428
429 def setfile(self, fname, data, mode, copysource):
429 def setfile(self, fname, data, mode, copysource):
430 islink, isexec = mode
430 islink, isexec = mode
431 if data is None:
431 if data is None:
432 util.setflags(self._join(fname), islink, isexec)
432 util.setflags(self._join(fname), islink, isexec)
433 return
433 return
434 if islink:
434 if islink:
435 self.opener.symlink(data, fname)
435 self.opener.symlink(data, fname)
436 else:
436 else:
437 self.opener.write(fname, data)
437 self.opener.write(fname, data)
438 if isexec:
438 if isexec:
439 util.setflags(self._join(fname), False, True)
439 util.setflags(self._join(fname), False, True)
440
440
441 def unlink(self, fname):
441 def unlink(self, fname):
442 try:
442 try:
443 util.unlinkpath(self._join(fname))
443 util.unlinkpath(self._join(fname))
444 except OSError, inst:
444 except OSError, inst:
445 if inst.errno != errno.ENOENT:
445 if inst.errno != errno.ENOENT:
446 raise
446 raise
447
447
448 def writerej(self, fname, failed, total, lines):
448 def writerej(self, fname, failed, total, lines):
449 fname = fname + ".rej"
449 fname = fname + ".rej"
450 self.ui.warn(
450 self.ui.warn(
451 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
451 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
452 (failed, total, fname))
452 (failed, total, fname))
453 fp = self.opener(fname, 'w')
453 fp = self.opener(fname, 'w')
454 fp.writelines(lines)
454 fp.writelines(lines)
455 fp.close()
455 fp.close()
456
456
457 def exists(self, fname):
457 def exists(self, fname):
458 return os.path.lexists(self._join(fname))
458 return os.path.lexists(self._join(fname))
459
459
460 class workingbackend(fsbackend):
460 class workingbackend(fsbackend):
461 def __init__(self, ui, repo, similarity):
461 def __init__(self, ui, repo, similarity):
462 super(workingbackend, self).__init__(ui, repo.root)
462 super(workingbackend, self).__init__(ui, repo.root)
463 self.repo = repo
463 self.repo = repo
464 self.similarity = similarity
464 self.similarity = similarity
465 self.removed = set()
465 self.removed = set()
466 self.changed = set()
466 self.changed = set()
467 self.copied = []
467 self.copied = []
468
468
469 def _checkknown(self, fname):
469 def _checkknown(self, fname):
470 if self.repo.dirstate[fname] == '?' and self.exists(fname):
470 if self.repo.dirstate[fname] == '?' and self.exists(fname):
471 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
471 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
472
472
473 def setfile(self, fname, data, mode, copysource):
473 def setfile(self, fname, data, mode, copysource):
474 self._checkknown(fname)
474 self._checkknown(fname)
475 super(workingbackend, self).setfile(fname, data, mode, copysource)
475 super(workingbackend, self).setfile(fname, data, mode, copysource)
476 if copysource is not None:
476 if copysource is not None:
477 self.copied.append((copysource, fname))
477 self.copied.append((copysource, fname))
478 self.changed.add(fname)
478 self.changed.add(fname)
479
479
480 def unlink(self, fname):
480 def unlink(self, fname):
481 self._checkknown(fname)
481 self._checkknown(fname)
482 super(workingbackend, self).unlink(fname)
482 super(workingbackend, self).unlink(fname)
483 self.removed.add(fname)
483 self.removed.add(fname)
484 self.changed.add(fname)
484 self.changed.add(fname)
485
485
486 def close(self):
486 def close(self):
487 wctx = self.repo[None]
487 wctx = self.repo[None]
488 addremoved = set(self.changed)
488 addremoved = set(self.changed)
489 for src, dst in self.copied:
489 for src, dst in self.copied:
490 scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
490 scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
491 if self.removed:
491 if self.removed:
492 wctx.forget(sorted(self.removed))
492 wctx.forget(sorted(self.removed))
493 for f in self.removed:
493 for f in self.removed:
494 if f not in self.repo.dirstate:
494 if f not in self.repo.dirstate:
495 # File was deleted and no longer belongs to the
495 # File was deleted and no longer belongs to the
496 # dirstate, it was probably marked added then
496 # dirstate, it was probably marked added then
497 # deleted, and should not be considered by
497 # deleted, and should not be considered by
498 # addremove().
498 # addremove().
499 addremoved.discard(f)
499 addremoved.discard(f)
500 if addremoved:
500 if addremoved:
501 cwd = self.repo.getcwd()
501 cwd = self.repo.getcwd()
502 if cwd:
502 if cwd:
503 addremoved = [util.pathto(self.repo.root, cwd, f)
503 addremoved = [util.pathto(self.repo.root, cwd, f)
504 for f in addremoved]
504 for f in addremoved]
505 scmutil.addremove(self.repo, addremoved, similarity=self.similarity)
505 scmutil.addremove(self.repo, addremoved, similarity=self.similarity)
506 return sorted(self.changed)
506 return sorted(self.changed)
507
507
508 class filestore(object):
508 class filestore(object):
509 def __init__(self, maxsize=None):
509 def __init__(self, maxsize=None):
510 self.opener = None
510 self.opener = None
511 self.files = {}
511 self.files = {}
512 self.created = 0
512 self.created = 0
513 self.maxsize = maxsize
513 self.maxsize = maxsize
514 if self.maxsize is None:
514 if self.maxsize is None:
515 self.maxsize = 4*(2**20)
515 self.maxsize = 4*(2**20)
516 self.size = 0
516 self.size = 0
517 self.data = {}
517 self.data = {}
518
518
519 def setfile(self, fname, data, mode, copied=None):
519 def setfile(self, fname, data, mode, copied=None):
520 if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize:
520 if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize:
521 self.data[fname] = (data, mode, copied)
521 self.data[fname] = (data, mode, copied)
522 self.size += len(data)
522 self.size += len(data)
523 else:
523 else:
524 if self.opener is None:
524 if self.opener is None:
525 root = tempfile.mkdtemp(prefix='hg-patch-')
525 root = tempfile.mkdtemp(prefix='hg-patch-')
526 self.opener = scmutil.opener(root)
526 self.opener = scmutil.opener(root)
527 # Avoid filename issues with these simple names
527 # Avoid filename issues with these simple names
528 fn = str(self.created)
528 fn = str(self.created)
529 self.opener.write(fn, data)
529 self.opener.write(fn, data)
530 self.created += 1
530 self.created += 1
531 self.files[fname] = (fn, mode, copied)
531 self.files[fname] = (fn, mode, copied)
532
532
533 def getfile(self, fname):
533 def getfile(self, fname):
534 if fname in self.data:
534 if fname in self.data:
535 return self.data[fname]
535 return self.data[fname]
536 if not self.opener or fname not in self.files:
536 if not self.opener or fname not in self.files:
537 raise IOError()
537 raise IOError()
538 fn, mode, copied = self.files[fname]
538 fn, mode, copied = self.files[fname]
539 return self.opener.read(fn), mode, copied
539 return self.opener.read(fn), mode, copied
540
540
541 def close(self):
541 def close(self):
542 if self.opener:
542 if self.opener:
543 shutil.rmtree(self.opener.base)
543 shutil.rmtree(self.opener.base)
544
544
545 class repobackend(abstractbackend):
545 class repobackend(abstractbackend):
546 def __init__(self, ui, repo, ctx, store):
546 def __init__(self, ui, repo, ctx, store):
547 super(repobackend, self).__init__(ui)
547 super(repobackend, self).__init__(ui)
548 self.repo = repo
548 self.repo = repo
549 self.ctx = ctx
549 self.ctx = ctx
550 self.store = store
550 self.store = store
551 self.changed = set()
551 self.changed = set()
552 self.removed = set()
552 self.removed = set()
553 self.copied = {}
553 self.copied = {}
554
554
555 def _checkknown(self, fname):
555 def _checkknown(self, fname):
556 if fname not in self.ctx:
556 if fname not in self.ctx:
557 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
557 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
558
558
559 def getfile(self, fname):
559 def getfile(self, fname):
560 try:
560 try:
561 fctx = self.ctx[fname]
561 fctx = self.ctx[fname]
562 except error.LookupError:
562 except error.LookupError:
563 raise IOError()
563 raise IOError()
564 flags = fctx.flags()
564 flags = fctx.flags()
565 return fctx.data(), ('l' in flags, 'x' in flags)
565 return fctx.data(), ('l' in flags, 'x' in flags)
566
566
567 def setfile(self, fname, data, mode, copysource):
567 def setfile(self, fname, data, mode, copysource):
568 if copysource:
568 if copysource:
569 self._checkknown(copysource)
569 self._checkknown(copysource)
570 if data is None:
570 if data is None:
571 data = self.ctx[fname].data()
571 data = self.ctx[fname].data()
572 self.store.setfile(fname, data, mode, copysource)
572 self.store.setfile(fname, data, mode, copysource)
573 self.changed.add(fname)
573 self.changed.add(fname)
574 if copysource:
574 if copysource:
575 self.copied[fname] = copysource
575 self.copied[fname] = copysource
576
576
577 def unlink(self, fname):
577 def unlink(self, fname):
578 self._checkknown(fname)
578 self._checkknown(fname)
579 self.removed.add(fname)
579 self.removed.add(fname)
580
580
581 def exists(self, fname):
581 def exists(self, fname):
582 return fname in self.ctx
582 return fname in self.ctx
583
583
584 def close(self):
584 def close(self):
585 return self.changed | self.removed
585 return self.changed | self.removed
586
586
587 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
587 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
588 unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
588 unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
589 contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
589 contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
590 eolmodes = ['strict', 'crlf', 'lf', 'auto']
590 eolmodes = ['strict', 'crlf', 'lf', 'auto']
591
591
592 class patchfile(object):
592 class patchfile(object):
593 def __init__(self, ui, gp, backend, store, eolmode='strict'):
593 def __init__(self, ui, gp, backend, store, eolmode='strict'):
594 self.fname = gp.path
594 self.fname = gp.path
595 self.eolmode = eolmode
595 self.eolmode = eolmode
596 self.eol = None
596 self.eol = None
597 self.backend = backend
597 self.backend = backend
598 self.ui = ui
598 self.ui = ui
599 self.lines = []
599 self.lines = []
600 self.exists = False
600 self.exists = False
601 self.missing = True
601 self.missing = True
602 self.mode = gp.mode
602 self.mode = gp.mode
603 self.copysource = gp.oldpath
603 self.copysource = gp.oldpath
604 self.create = gp.op in ('ADD', 'COPY', 'RENAME')
604 self.create = gp.op in ('ADD', 'COPY', 'RENAME')
605 self.remove = gp.op == 'DELETE'
605 self.remove = gp.op == 'DELETE'
606 try:
606 try:
607 if self.copysource is None:
607 if self.copysource is None:
608 data, mode = backend.getfile(self.fname)
608 data, mode = backend.getfile(self.fname)
609 self.exists = True
609 self.exists = True
610 else:
610 else:
611 data, mode = store.getfile(self.copysource)[:2]
611 data, mode = store.getfile(self.copysource)[:2]
612 self.exists = backend.exists(self.fname)
612 self.exists = backend.exists(self.fname)
613 self.missing = False
613 self.missing = False
614 if data:
614 if data:
615 self.lines = mdiff.splitnewlines(data)
615 self.lines = mdiff.splitnewlines(data)
616 if self.mode is None:
616 if self.mode is None:
617 self.mode = mode
617 self.mode = mode
618 if self.lines:
618 if self.lines:
619 # Normalize line endings
619 # Normalize line endings
620 if self.lines[0].endswith('\r\n'):
620 if self.lines[0].endswith('\r\n'):
621 self.eol = '\r\n'
621 self.eol = '\r\n'
622 elif self.lines[0].endswith('\n'):
622 elif self.lines[0].endswith('\n'):
623 self.eol = '\n'
623 self.eol = '\n'
624 if eolmode != 'strict':
624 if eolmode != 'strict':
625 nlines = []
625 nlines = []
626 for l in self.lines:
626 for l in self.lines:
627 if l.endswith('\r\n'):
627 if l.endswith('\r\n'):
628 l = l[:-2] + '\n'
628 l = l[:-2] + '\n'
629 nlines.append(l)
629 nlines.append(l)
630 self.lines = nlines
630 self.lines = nlines
631 except IOError:
631 except IOError:
632 if self.create:
632 if self.create:
633 self.missing = False
633 self.missing = False
634 if self.mode is None:
634 if self.mode is None:
635 self.mode = (False, False)
635 self.mode = (False, False)
636 if self.missing:
636 if self.missing:
637 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
637 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
638
638
639 self.hash = {}
639 self.hash = {}
640 self.dirty = 0
640 self.dirty = 0
641 self.offset = 0
641 self.offset = 0
642 self.skew = 0
642 self.skew = 0
643 self.rej = []
643 self.rej = []
644 self.fileprinted = False
644 self.fileprinted = False
645 self.printfile(False)
645 self.printfile(False)
646 self.hunks = 0
646 self.hunks = 0
647
647
648 def writelines(self, fname, lines, mode):
648 def writelines(self, fname, lines, mode):
649 if self.eolmode == 'auto':
649 if self.eolmode == 'auto':
650 eol = self.eol
650 eol = self.eol
651 elif self.eolmode == 'crlf':
651 elif self.eolmode == 'crlf':
652 eol = '\r\n'
652 eol = '\r\n'
653 else:
653 else:
654 eol = '\n'
654 eol = '\n'
655
655
656 if self.eolmode != 'strict' and eol and eol != '\n':
656 if self.eolmode != 'strict' and eol and eol != '\n':
657 rawlines = []
657 rawlines = []
658 for l in lines:
658 for l in lines:
659 if l and l[-1] == '\n':
659 if l and l[-1] == '\n':
660 l = l[:-1] + eol
660 l = l[:-1] + eol
661 rawlines.append(l)
661 rawlines.append(l)
662 lines = rawlines
662 lines = rawlines
663
663
664 self.backend.setfile(fname, ''.join(lines), mode, self.copysource)
664 self.backend.setfile(fname, ''.join(lines), mode, self.copysource)
665
665
666 def printfile(self, warn):
666 def printfile(self, warn):
667 if self.fileprinted:
667 if self.fileprinted:
668 return
668 return
669 if warn or self.ui.verbose:
669 if warn or self.ui.verbose:
670 self.fileprinted = True
670 self.fileprinted = True
671 s = _("patching file %s\n") % self.fname
671 s = _("patching file %s\n") % self.fname
672 if warn:
672 if warn:
673 self.ui.warn(s)
673 self.ui.warn(s)
674 else:
674 else:
675 self.ui.note(s)
675 self.ui.note(s)
676
676
677
677
678 def findlines(self, l, linenum):
678 def findlines(self, l, linenum):
679 # looks through the hash and finds candidate lines. The
679 # looks through the hash and finds candidate lines. The
680 # result is a list of line numbers sorted based on distance
680 # result is a list of line numbers sorted based on distance
681 # from linenum
681 # from linenum
682
682
683 cand = self.hash.get(l, [])
683 cand = self.hash.get(l, [])
684 if len(cand) > 1:
684 if len(cand) > 1:
685 # resort our list of potentials forward then back.
685 # resort our list of potentials forward then back.
686 cand.sort(key=lambda x: abs(x - linenum))
686 cand.sort(key=lambda x: abs(x - linenum))
687 return cand
687 return cand
688
688
689 def write_rej(self):
689 def write_rej(self):
690 # our rejects are a little different from patch(1). This always
690 # our rejects are a little different from patch(1). This always
691 # creates rejects in the same form as the original patch. A file
691 # creates rejects in the same form as the original patch. A file
692 # header is inserted so that you can run the reject through patch again
692 # header is inserted so that you can run the reject through patch again
693 # without having to type the filename.
693 # without having to type the filename.
694 if not self.rej:
694 if not self.rej:
695 return
695 return
696 base = os.path.basename(self.fname)
696 base = os.path.basename(self.fname)
697 lines = ["--- %s\n+++ %s\n" % (base, base)]
697 lines = ["--- %s\n+++ %s\n" % (base, base)]
698 for x in self.rej:
698 for x in self.rej:
699 for l in x.hunk:
699 for l in x.hunk:
700 lines.append(l)
700 lines.append(l)
701 if l[-1] != '\n':
701 if l[-1] != '\n':
702 lines.append("\n\ No newline at end of file\n")
702 lines.append("\n\ No newline at end of file\n")
703 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines)
703 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines)
704
704
705 def apply(self, h):
705 def apply(self, h):
706 if not h.complete():
706 if not h.complete():
707 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
707 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
708 (h.number, h.desc, len(h.a), h.lena, len(h.b),
708 (h.number, h.desc, len(h.a), h.lena, len(h.b),
709 h.lenb))
709 h.lenb))
710
710
711 self.hunks += 1
711 self.hunks += 1
712
712
713 if self.missing:
713 if self.missing:
714 self.rej.append(h)
714 self.rej.append(h)
715 return -1
715 return -1
716
716
717 if self.exists and self.create:
717 if self.exists and self.create:
718 if self.copysource:
718 if self.copysource:
719 self.ui.warn(_("cannot create %s: destination already "
719 self.ui.warn(_("cannot create %s: destination already "
720 "exists\n" % self.fname))
720 "exists\n" % self.fname))
721 else:
721 else:
722 self.ui.warn(_("file %s already exists\n") % self.fname)
722 self.ui.warn(_("file %s already exists\n") % self.fname)
723 self.rej.append(h)
723 self.rej.append(h)
724 return -1
724 return -1
725
725
726 if isinstance(h, binhunk):
726 if isinstance(h, binhunk):
727 if self.remove:
727 if self.remove:
728 self.backend.unlink(self.fname)
728 self.backend.unlink(self.fname)
729 else:
729 else:
730 self.lines[:] = h.new()
730 self.lines[:] = h.new()
731 self.offset += len(h.new())
731 self.offset += len(h.new())
732 self.dirty = True
732 self.dirty = True
733 return 0
733 return 0
734
734
735 horig = h
735 horig = h
736 if (self.eolmode in ('crlf', 'lf')
736 if (self.eolmode in ('crlf', 'lf')
737 or self.eolmode == 'auto' and self.eol):
737 or self.eolmode == 'auto' and self.eol):
738 # If new eols are going to be normalized, then normalize
738 # If new eols are going to be normalized, then normalize
739 # hunk data before patching. Otherwise, preserve input
739 # hunk data before patching. Otherwise, preserve input
740 # line-endings.
740 # line-endings.
741 h = h.getnormalized()
741 h = h.getnormalized()
742
742
743 # fast case first, no offsets, no fuzz
743 # fast case first, no offsets, no fuzz
744 old, oldstart, new, newstart = h.fuzzit(0, False)
744 old, oldstart, new, newstart = h.fuzzit(0, False)
745 oldstart += self.offset
745 oldstart += self.offset
746 orig_start = oldstart
746 orig_start = oldstart
747 # if there's skew we want to emit the "(offset %d lines)" even
747 # if there's skew we want to emit the "(offset %d lines)" even
748 # when the hunk cleanly applies at start + skew, so skip the
748 # when the hunk cleanly applies at start + skew, so skip the
749 # fast case code
749 # fast case code
750 if (self.skew == 0 and
750 if (self.skew == 0 and
751 diffhelpers.testhunk(old, self.lines, oldstart) == 0):
751 diffhelpers.testhunk(old, self.lines, oldstart) == 0):
752 if self.remove:
752 if self.remove:
753 self.backend.unlink(self.fname)
753 self.backend.unlink(self.fname)
754 else:
754 else:
755 self.lines[oldstart:oldstart + len(old)] = new
755 self.lines[oldstart:oldstart + len(old)] = new
756 self.offset += len(new) - len(old)
756 self.offset += len(new) - len(old)
757 self.dirty = True
757 self.dirty = True
758 return 0
758 return 0
759
759
760 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
760 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
761 self.hash = {}
761 self.hash = {}
762 for x, s in enumerate(self.lines):
762 for x, s in enumerate(self.lines):
763 self.hash.setdefault(s, []).append(x)
763 self.hash.setdefault(s, []).append(x)
764
764
765 for fuzzlen in xrange(3):
765 for fuzzlen in xrange(3):
766 for toponly in [True, False]:
766 for toponly in [True, False]:
767 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly)
767 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly)
768 oldstart = oldstart + self.offset + self.skew
768 oldstart = oldstart + self.offset + self.skew
769 oldstart = min(oldstart, len(self.lines))
769 oldstart = min(oldstart, len(self.lines))
770 if old:
770 if old:
771 cand = self.findlines(old[0][1:], oldstart)
771 cand = self.findlines(old[0][1:], oldstart)
772 else:
772 else:
773 # Only adding lines with no or fuzzed context, just
773 # Only adding lines with no or fuzzed context, just
774 # take the skew in account
774 # take the skew in account
775 cand = [oldstart]
775 cand = [oldstart]
776
776
777 for l in cand:
777 for l in cand:
778 if not old or diffhelpers.testhunk(old, self.lines, l) == 0:
778 if not old or diffhelpers.testhunk(old, self.lines, l) == 0:
779 self.lines[l : l + len(old)] = new
779 self.lines[l : l + len(old)] = new
780 self.offset += len(new) - len(old)
780 self.offset += len(new) - len(old)
781 self.skew = l - orig_start
781 self.skew = l - orig_start
782 self.dirty = True
782 self.dirty = True
783 offset = l - orig_start - fuzzlen
783 offset = l - orig_start - fuzzlen
784 if fuzzlen:
784 if fuzzlen:
785 msg = _("Hunk #%d succeeded at %d "
785 msg = _("Hunk #%d succeeded at %d "
786 "with fuzz %d "
786 "with fuzz %d "
787 "(offset %d lines).\n")
787 "(offset %d lines).\n")
788 self.printfile(True)
788 self.printfile(True)
789 self.ui.warn(msg %
789 self.ui.warn(msg %
790 (h.number, l + 1, fuzzlen, offset))
790 (h.number, l + 1, fuzzlen, offset))
791 else:
791 else:
792 msg = _("Hunk #%d succeeded at %d "
792 msg = _("Hunk #%d succeeded at %d "
793 "(offset %d lines).\n")
793 "(offset %d lines).\n")
794 self.ui.note(msg % (h.number, l + 1, offset))
794 self.ui.note(msg % (h.number, l + 1, offset))
795 return fuzzlen
795 return fuzzlen
796 self.printfile(True)
796 self.printfile(True)
797 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
797 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
798 self.rej.append(horig)
798 self.rej.append(horig)
799 return -1
799 return -1
800
800
801 def close(self):
801 def close(self):
802 if self.dirty:
802 if self.dirty:
803 self.writelines(self.fname, self.lines, self.mode)
803 self.writelines(self.fname, self.lines, self.mode)
804 self.write_rej()
804 self.write_rej()
805 return len(self.rej)
805 return len(self.rej)
806
806
807 class hunk(object):
807 class hunk(object):
808 def __init__(self, desc, num, lr, context):
808 def __init__(self, desc, num, lr, context):
809 self.number = num
809 self.number = num
810 self.desc = desc
810 self.desc = desc
811 self.hunk = [desc]
811 self.hunk = [desc]
812 self.a = []
812 self.a = []
813 self.b = []
813 self.b = []
814 self.starta = self.lena = None
814 self.starta = self.lena = None
815 self.startb = self.lenb = None
815 self.startb = self.lenb = None
816 if lr is not None:
816 if lr is not None:
817 if context:
817 if context:
818 self.read_context_hunk(lr)
818 self.read_context_hunk(lr)
819 else:
819 else:
820 self.read_unified_hunk(lr)
820 self.read_unified_hunk(lr)
821
821
822 def getnormalized(self):
822 def getnormalized(self):
823 """Return a copy with line endings normalized to LF."""
823 """Return a copy with line endings normalized to LF."""
824
824
825 def normalize(lines):
825 def normalize(lines):
826 nlines = []
826 nlines = []
827 for line in lines:
827 for line in lines:
828 if line.endswith('\r\n'):
828 if line.endswith('\r\n'):
829 line = line[:-2] + '\n'
829 line = line[:-2] + '\n'
830 nlines.append(line)
830 nlines.append(line)
831 return nlines
831 return nlines
832
832
833 # Dummy object, it is rebuilt manually
833 # Dummy object, it is rebuilt manually
834 nh = hunk(self.desc, self.number, None, None)
834 nh = hunk(self.desc, self.number, None, None)
835 nh.number = self.number
835 nh.number = self.number
836 nh.desc = self.desc
836 nh.desc = self.desc
837 nh.hunk = self.hunk
837 nh.hunk = self.hunk
838 nh.a = normalize(self.a)
838 nh.a = normalize(self.a)
839 nh.b = normalize(self.b)
839 nh.b = normalize(self.b)
840 nh.starta = self.starta
840 nh.starta = self.starta
841 nh.startb = self.startb
841 nh.startb = self.startb
842 nh.lena = self.lena
842 nh.lena = self.lena
843 nh.lenb = self.lenb
843 nh.lenb = self.lenb
844 return nh
844 return nh
845
845
846 def read_unified_hunk(self, lr):
846 def read_unified_hunk(self, lr):
847 m = unidesc.match(self.desc)
847 m = unidesc.match(self.desc)
848 if not m:
848 if not m:
849 raise PatchError(_("bad hunk #%d") % self.number)
849 raise PatchError(_("bad hunk #%d") % self.number)
850 self.starta, self.lena, self.startb, self.lenb = m.groups()
850 self.starta, self.lena, self.startb, self.lenb = m.groups()
851 if self.lena is None:
851 if self.lena is None:
852 self.lena = 1
852 self.lena = 1
853 else:
853 else:
854 self.lena = int(self.lena)
854 self.lena = int(self.lena)
855 if self.lenb is None:
855 if self.lenb is None:
856 self.lenb = 1
856 self.lenb = 1
857 else:
857 else:
858 self.lenb = int(self.lenb)
858 self.lenb = int(self.lenb)
859 self.starta = int(self.starta)
859 self.starta = int(self.starta)
860 self.startb = int(self.startb)
860 self.startb = int(self.startb)
861 diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b)
861 diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b)
862 # if we hit eof before finishing out the hunk, the last line will
862 # if we hit eof before finishing out the hunk, the last line will
863 # be zero length. Lets try to fix it up.
863 # be zero length. Lets try to fix it up.
864 while len(self.hunk[-1]) == 0:
864 while len(self.hunk[-1]) == 0:
865 del self.hunk[-1]
865 del self.hunk[-1]
866 del self.a[-1]
866 del self.a[-1]
867 del self.b[-1]
867 del self.b[-1]
868 self.lena -= 1
868 self.lena -= 1
869 self.lenb -= 1
869 self.lenb -= 1
870 self._fixnewline(lr)
870 self._fixnewline(lr)
871
871
872 def read_context_hunk(self, lr):
872 def read_context_hunk(self, lr):
873 self.desc = lr.readline()
873 self.desc = lr.readline()
874 m = contextdesc.match(self.desc)
874 m = contextdesc.match(self.desc)
875 if not m:
875 if not m:
876 raise PatchError(_("bad hunk #%d") % self.number)
876 raise PatchError(_("bad hunk #%d") % self.number)
877 self.starta, aend = m.groups()
877 self.starta, aend = m.groups()
878 self.starta = int(self.starta)
878 self.starta = int(self.starta)
879 if aend is None:
879 if aend is None:
880 aend = self.starta
880 aend = self.starta
881 self.lena = int(aend) - self.starta
881 self.lena = int(aend) - self.starta
882 if self.starta:
882 if self.starta:
883 self.lena += 1
883 self.lena += 1
884 for x in xrange(self.lena):
884 for x in xrange(self.lena):
885 l = lr.readline()
885 l = lr.readline()
886 if l.startswith('---'):
886 if l.startswith('---'):
887 # lines addition, old block is empty
887 # lines addition, old block is empty
888 lr.push(l)
888 lr.push(l)
889 break
889 break
890 s = l[2:]
890 s = l[2:]
891 if l.startswith('- ') or l.startswith('! '):
891 if l.startswith('- ') or l.startswith('! '):
892 u = '-' + s
892 u = '-' + s
893 elif l.startswith(' '):
893 elif l.startswith(' '):
894 u = ' ' + s
894 u = ' ' + s
895 else:
895 else:
896 raise PatchError(_("bad hunk #%d old text line %d") %
896 raise PatchError(_("bad hunk #%d old text line %d") %
897 (self.number, x))
897 (self.number, x))
898 self.a.append(u)
898 self.a.append(u)
899 self.hunk.append(u)
899 self.hunk.append(u)
900
900
901 l = lr.readline()
901 l = lr.readline()
902 if l.startswith('\ '):
902 if l.startswith('\ '):
903 s = self.a[-1][:-1]
903 s = self.a[-1][:-1]
904 self.a[-1] = s
904 self.a[-1] = s
905 self.hunk[-1] = s
905 self.hunk[-1] = s
906 l = lr.readline()
906 l = lr.readline()
907 m = contextdesc.match(l)
907 m = contextdesc.match(l)
908 if not m:
908 if not m:
909 raise PatchError(_("bad hunk #%d") % self.number)
909 raise PatchError(_("bad hunk #%d") % self.number)
910 self.startb, bend = m.groups()
910 self.startb, bend = m.groups()
911 self.startb = int(self.startb)
911 self.startb = int(self.startb)
912 if bend is None:
912 if bend is None:
913 bend = self.startb
913 bend = self.startb
914 self.lenb = int(bend) - self.startb
914 self.lenb = int(bend) - self.startb
915 if self.startb:
915 if self.startb:
916 self.lenb += 1
916 self.lenb += 1
917 hunki = 1
917 hunki = 1
918 for x in xrange(self.lenb):
918 for x in xrange(self.lenb):
919 l = lr.readline()
919 l = lr.readline()
920 if l.startswith('\ '):
920 if l.startswith('\ '):
921 # XXX: the only way to hit this is with an invalid line range.
921 # XXX: the only way to hit this is with an invalid line range.
922 # The no-eol marker is not counted in the line range, but I
922 # The no-eol marker is not counted in the line range, but I
923 # guess there are diff(1) out there which behave differently.
923 # guess there are diff(1) out there which behave differently.
924 s = self.b[-1][:-1]
924 s = self.b[-1][:-1]
925 self.b[-1] = s
925 self.b[-1] = s
926 self.hunk[hunki - 1] = s
926 self.hunk[hunki - 1] = s
927 continue
927 continue
928 if not l:
928 if not l:
929 # line deletions, new block is empty and we hit EOF
929 # line deletions, new block is empty and we hit EOF
930 lr.push(l)
930 lr.push(l)
931 break
931 break
932 s = l[2:]
932 s = l[2:]
933 if l.startswith('+ ') or l.startswith('! '):
933 if l.startswith('+ ') or l.startswith('! '):
934 u = '+' + s
934 u = '+' + s
935 elif l.startswith(' '):
935 elif l.startswith(' '):
936 u = ' ' + s
936 u = ' ' + s
937 elif len(self.b) == 0:
937 elif len(self.b) == 0:
938 # line deletions, new block is empty
938 # line deletions, new block is empty
939 lr.push(l)
939 lr.push(l)
940 break
940 break
941 else:
941 else:
942 raise PatchError(_("bad hunk #%d old text line %d") %
942 raise PatchError(_("bad hunk #%d old text line %d") %
943 (self.number, x))
943 (self.number, x))
944 self.b.append(s)
944 self.b.append(s)
945 while True:
945 while True:
946 if hunki >= len(self.hunk):
946 if hunki >= len(self.hunk):
947 h = ""
947 h = ""
948 else:
948 else:
949 h = self.hunk[hunki]
949 h = self.hunk[hunki]
950 hunki += 1
950 hunki += 1
951 if h == u:
951 if h == u:
952 break
952 break
953 elif h.startswith('-'):
953 elif h.startswith('-'):
954 continue
954 continue
955 else:
955 else:
956 self.hunk.insert(hunki - 1, u)
956 self.hunk.insert(hunki - 1, u)
957 break
957 break
958
958
959 if not self.a:
959 if not self.a:
960 # this happens when lines were only added to the hunk
960 # this happens when lines were only added to the hunk
961 for x in self.hunk:
961 for x in self.hunk:
962 if x.startswith('-') or x.startswith(' '):
962 if x.startswith('-') or x.startswith(' '):
963 self.a.append(x)
963 self.a.append(x)
964 if not self.b:
964 if not self.b:
965 # this happens when lines were only deleted from the hunk
965 # this happens when lines were only deleted from the hunk
966 for x in self.hunk:
966 for x in self.hunk:
967 if x.startswith('+') or x.startswith(' '):
967 if x.startswith('+') or x.startswith(' '):
968 self.b.append(x[1:])
968 self.b.append(x[1:])
969 # @@ -start,len +start,len @@
969 # @@ -start,len +start,len @@
970 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
970 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
971 self.startb, self.lenb)
971 self.startb, self.lenb)
972 self.hunk[0] = self.desc
972 self.hunk[0] = self.desc
973 self._fixnewline(lr)
973 self._fixnewline(lr)
974
974
975 def _fixnewline(self, lr):
975 def _fixnewline(self, lr):
976 l = lr.readline()
976 l = lr.readline()
977 if l.startswith('\ '):
977 if l.startswith('\ '):
978 diffhelpers.fix_newline(self.hunk, self.a, self.b)
978 diffhelpers.fix_newline(self.hunk, self.a, self.b)
979 else:
979 else:
980 lr.push(l)
980 lr.push(l)
981
981
982 def complete(self):
982 def complete(self):
983 return len(self.a) == self.lena and len(self.b) == self.lenb
983 return len(self.a) == self.lena and len(self.b) == self.lenb
984
984
985 def _fuzzit(self, old, new, fuzz, toponly):
985 def _fuzzit(self, old, new, fuzz, toponly):
986 # this removes context lines from the top and bottom of list 'l'. It
986 # this removes context lines from the top and bottom of list 'l'. It
987 # checks the hunk to make sure only context lines are removed, and then
987 # checks the hunk to make sure only context lines are removed, and then
988 # returns a new shortened list of lines.
988 # returns a new shortened list of lines.
989 fuzz = min(fuzz, len(old))
989 fuzz = min(fuzz, len(old))
990 if fuzz:
990 if fuzz:
991 top = 0
991 top = 0
992 bot = 0
992 bot = 0
993 hlen = len(self.hunk)
993 hlen = len(self.hunk)
994 for x in xrange(hlen - 1):
994 for x in xrange(hlen - 1):
995 # the hunk starts with the @@ line, so use x+1
995 # the hunk starts with the @@ line, so use x+1
996 if self.hunk[x + 1][0] == ' ':
996 if self.hunk[x + 1][0] == ' ':
997 top += 1
997 top += 1
998 else:
998 else:
999 break
999 break
1000 if not toponly:
1000 if not toponly:
1001 for x in xrange(hlen - 1):
1001 for x in xrange(hlen - 1):
1002 if self.hunk[hlen - bot - 1][0] == ' ':
1002 if self.hunk[hlen - bot - 1][0] == ' ':
1003 bot += 1
1003 bot += 1
1004 else:
1004 else:
1005 break
1005 break
1006
1006
1007 bot = min(fuzz, bot)
1007 bot = min(fuzz, bot)
1008 top = min(fuzz, top)
1008 top = min(fuzz, top)
1009 return old[top:len(old)-bot], new[top:len(new)-bot], top
1009 return old[top:len(old)-bot], new[top:len(new)-bot], top
1010 return old, new, 0
1010 return old, new, 0
1011
1011
1012 def fuzzit(self, fuzz, toponly):
1012 def fuzzit(self, fuzz, toponly):
1013 old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly)
1013 old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly)
1014 oldstart = self.starta + top
1014 oldstart = self.starta + top
1015 newstart = self.startb + top
1015 newstart = self.startb + top
1016 # zero length hunk ranges already have their start decremented
1016 # zero length hunk ranges already have their start decremented
1017 if self.lena:
1017 if self.lena:
1018 oldstart -= 1
1018 oldstart -= 1
1019 if self.lenb:
1019 if self.lenb:
1020 newstart -= 1
1020 newstart -= 1
1021 return old, oldstart, new, newstart
1021 return old, oldstart, new, newstart
1022
1022
1023 class binhunk(object):
1023 class binhunk(object):
1024 'A binary patch file. Only understands literals so far.'
1024 'A binary patch file. Only understands literals so far.'
1025 def __init__(self, lr):
1025 def __init__(self, lr):
1026 self.text = None
1026 self.text = None
1027 self.hunk = ['GIT binary patch\n']
1027 self.hunk = ['GIT binary patch\n']
1028 self._read(lr)
1028 self._read(lr)
1029
1029
1030 def complete(self):
1030 def complete(self):
1031 return self.text is not None
1031 return self.text is not None
1032
1032
1033 def new(self):
1033 def new(self):
1034 return [self.text]
1034 return [self.text]
1035
1035
1036 def _read(self, lr):
1036 def _read(self, lr):
1037 line = lr.readline()
1037 line = lr.readline()
1038 self.hunk.append(line)
1038 self.hunk.append(line)
1039 while line and not line.startswith('literal '):
1039 while line and not line.startswith('literal '):
1040 line = lr.readline()
1040 line = lr.readline()
1041 self.hunk.append(line)
1041 self.hunk.append(line)
1042 if not line:
1042 if not line:
1043 raise PatchError(_('could not extract binary patch'))
1043 raise PatchError(_('could not extract binary patch'))
1044 size = int(line[8:].rstrip())
1044 size = int(line[8:].rstrip())
1045 dec = []
1045 dec = []
1046 line = lr.readline()
1046 line = lr.readline()
1047 self.hunk.append(line)
1047 self.hunk.append(line)
1048 while len(line) > 1:
1048 while len(line) > 1:
1049 l = line[0]
1049 l = line[0]
1050 if l <= 'Z' and l >= 'A':
1050 if l <= 'Z' and l >= 'A':
1051 l = ord(l) - ord('A') + 1
1051 l = ord(l) - ord('A') + 1
1052 else:
1052 else:
1053 l = ord(l) - ord('a') + 27
1053 l = ord(l) - ord('a') + 27
1054 dec.append(base85.b85decode(line[1:-1])[:l])
1054 try:
1055 dec.append(base85.b85decode(line[1:-1])[:l])
1056 except ValueError, e:
1057 raise PatchError(_('could not decode binary patch: %s')
1058 % str(e))
1055 line = lr.readline()
1059 line = lr.readline()
1056 self.hunk.append(line)
1060 self.hunk.append(line)
1057 text = zlib.decompress(''.join(dec))
1061 text = zlib.decompress(''.join(dec))
1058 if len(text) != size:
1062 if len(text) != size:
1059 raise PatchError(_('binary patch is %d bytes, not %d') %
1063 raise PatchError(_('binary patch is %d bytes, not %d') %
1060 len(text), size)
1064 len(text), size)
1061 self.text = text
1065 self.text = text
1062
1066
1063 def parsefilename(str):
1067 def parsefilename(str):
1064 # --- filename \t|space stuff
1068 # --- filename \t|space stuff
1065 s = str[4:].rstrip('\r\n')
1069 s = str[4:].rstrip('\r\n')
1066 i = s.find('\t')
1070 i = s.find('\t')
1067 if i < 0:
1071 if i < 0:
1068 i = s.find(' ')
1072 i = s.find(' ')
1069 if i < 0:
1073 if i < 0:
1070 return s
1074 return s
1071 return s[:i]
1075 return s[:i]
1072
1076
1073 def pathstrip(path, strip):
1077 def pathstrip(path, strip):
1074 pathlen = len(path)
1078 pathlen = len(path)
1075 i = 0
1079 i = 0
1076 if strip == 0:
1080 if strip == 0:
1077 return '', path.rstrip()
1081 return '', path.rstrip()
1078 count = strip
1082 count = strip
1079 while count > 0:
1083 while count > 0:
1080 i = path.find('/', i)
1084 i = path.find('/', i)
1081 if i == -1:
1085 if i == -1:
1082 raise PatchError(_("unable to strip away %d of %d dirs from %s") %
1086 raise PatchError(_("unable to strip away %d of %d dirs from %s") %
1083 (count, strip, path))
1087 (count, strip, path))
1084 i += 1
1088 i += 1
1085 # consume '//' in the path
1089 # consume '//' in the path
1086 while i < pathlen - 1 and path[i] == '/':
1090 while i < pathlen - 1 and path[i] == '/':
1087 i += 1
1091 i += 1
1088 count -= 1
1092 count -= 1
1089 return path[:i].lstrip(), path[i:].rstrip()
1093 return path[:i].lstrip(), path[i:].rstrip()
1090
1094
1091 def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip):
1095 def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip):
1092 nulla = afile_orig == "/dev/null"
1096 nulla = afile_orig == "/dev/null"
1093 nullb = bfile_orig == "/dev/null"
1097 nullb = bfile_orig == "/dev/null"
1094 create = nulla and hunk.starta == 0 and hunk.lena == 0
1098 create = nulla and hunk.starta == 0 and hunk.lena == 0
1095 remove = nullb and hunk.startb == 0 and hunk.lenb == 0
1099 remove = nullb and hunk.startb == 0 and hunk.lenb == 0
1096 abase, afile = pathstrip(afile_orig, strip)
1100 abase, afile = pathstrip(afile_orig, strip)
1097 gooda = not nulla and backend.exists(afile)
1101 gooda = not nulla and backend.exists(afile)
1098 bbase, bfile = pathstrip(bfile_orig, strip)
1102 bbase, bfile = pathstrip(bfile_orig, strip)
1099 if afile == bfile:
1103 if afile == bfile:
1100 goodb = gooda
1104 goodb = gooda
1101 else:
1105 else:
1102 goodb = not nullb and backend.exists(bfile)
1106 goodb = not nullb and backend.exists(bfile)
1103 missing = not goodb and not gooda and not create
1107 missing = not goodb and not gooda and not create
1104
1108
1105 # some diff programs apparently produce patches where the afile is
1109 # some diff programs apparently produce patches where the afile is
1106 # not /dev/null, but afile starts with bfile
1110 # not /dev/null, but afile starts with bfile
1107 abasedir = afile[:afile.rfind('/') + 1]
1111 abasedir = afile[:afile.rfind('/') + 1]
1108 bbasedir = bfile[:bfile.rfind('/') + 1]
1112 bbasedir = bfile[:bfile.rfind('/') + 1]
1109 if (missing and abasedir == bbasedir and afile.startswith(bfile)
1113 if (missing and abasedir == bbasedir and afile.startswith(bfile)
1110 and hunk.starta == 0 and hunk.lena == 0):
1114 and hunk.starta == 0 and hunk.lena == 0):
1111 create = True
1115 create = True
1112 missing = False
1116 missing = False
1113
1117
1114 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the
1118 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the
1115 # diff is between a file and its backup. In this case, the original
1119 # diff is between a file and its backup. In this case, the original
1116 # file should be patched (see original mpatch code).
1120 # file should be patched (see original mpatch code).
1117 isbackup = (abase == bbase and bfile.startswith(afile))
1121 isbackup = (abase == bbase and bfile.startswith(afile))
1118 fname = None
1122 fname = None
1119 if not missing:
1123 if not missing:
1120 if gooda and goodb:
1124 if gooda and goodb:
1121 fname = isbackup and afile or bfile
1125 fname = isbackup and afile or bfile
1122 elif gooda:
1126 elif gooda:
1123 fname = afile
1127 fname = afile
1124
1128
1125 if not fname:
1129 if not fname:
1126 if not nullb:
1130 if not nullb:
1127 fname = isbackup and afile or bfile
1131 fname = isbackup and afile or bfile
1128 elif not nulla:
1132 elif not nulla:
1129 fname = afile
1133 fname = afile
1130 else:
1134 else:
1131 raise PatchError(_("undefined source and destination files"))
1135 raise PatchError(_("undefined source and destination files"))
1132
1136
1133 gp = patchmeta(fname)
1137 gp = patchmeta(fname)
1134 if create:
1138 if create:
1135 gp.op = 'ADD'
1139 gp.op = 'ADD'
1136 elif remove:
1140 elif remove:
1137 gp.op = 'DELETE'
1141 gp.op = 'DELETE'
1138 return gp
1142 return gp
1139
1143
1140 def scangitpatch(lr, firstline):
1144 def scangitpatch(lr, firstline):
1141 """
1145 """
1142 Git patches can emit:
1146 Git patches can emit:
1143 - rename a to b
1147 - rename a to b
1144 - change b
1148 - change b
1145 - copy a to c
1149 - copy a to c
1146 - change c
1150 - change c
1147
1151
1148 We cannot apply this sequence as-is, the renamed 'a' could not be
1152 We cannot apply this sequence as-is, the renamed 'a' could not be
1149 found for it would have been renamed already. And we cannot copy
1153 found for it would have been renamed already. And we cannot copy
1150 from 'b' instead because 'b' would have been changed already. So
1154 from 'b' instead because 'b' would have been changed already. So
1151 we scan the git patch for copy and rename commands so we can
1155 we scan the git patch for copy and rename commands so we can
1152 perform the copies ahead of time.
1156 perform the copies ahead of time.
1153 """
1157 """
1154 pos = 0
1158 pos = 0
1155 try:
1159 try:
1156 pos = lr.fp.tell()
1160 pos = lr.fp.tell()
1157 fp = lr.fp
1161 fp = lr.fp
1158 except IOError:
1162 except IOError:
1159 fp = cStringIO.StringIO(lr.fp.read())
1163 fp = cStringIO.StringIO(lr.fp.read())
1160 gitlr = linereader(fp)
1164 gitlr = linereader(fp)
1161 gitlr.push(firstline)
1165 gitlr.push(firstline)
1162 gitpatches = readgitpatch(gitlr)
1166 gitpatches = readgitpatch(gitlr)
1163 fp.seek(pos)
1167 fp.seek(pos)
1164 return gitpatches
1168 return gitpatches
1165
1169
1166 def iterhunks(fp):
1170 def iterhunks(fp):
1167 """Read a patch and yield the following events:
1171 """Read a patch and yield the following events:
1168 - ("file", afile, bfile, firsthunk): select a new target file.
1172 - ("file", afile, bfile, firsthunk): select a new target file.
1169 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1173 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1170 "file" event.
1174 "file" event.
1171 - ("git", gitchanges): current diff is in git format, gitchanges
1175 - ("git", gitchanges): current diff is in git format, gitchanges
1172 maps filenames to gitpatch records. Unique event.
1176 maps filenames to gitpatch records. Unique event.
1173 """
1177 """
1174 afile = ""
1178 afile = ""
1175 bfile = ""
1179 bfile = ""
1176 state = None
1180 state = None
1177 hunknum = 0
1181 hunknum = 0
1178 emitfile = newfile = False
1182 emitfile = newfile = False
1179 gitpatches = None
1183 gitpatches = None
1180
1184
1181 # our states
1185 # our states
1182 BFILE = 1
1186 BFILE = 1
1183 context = None
1187 context = None
1184 lr = linereader(fp)
1188 lr = linereader(fp)
1185
1189
1186 while True:
1190 while True:
1187 x = lr.readline()
1191 x = lr.readline()
1188 if not x:
1192 if not x:
1189 break
1193 break
1190 if state == BFILE and (
1194 if state == BFILE and (
1191 (not context and x[0] == '@')
1195 (not context and x[0] == '@')
1192 or (context is not False and x.startswith('***************'))
1196 or (context is not False and x.startswith('***************'))
1193 or x.startswith('GIT binary patch')):
1197 or x.startswith('GIT binary patch')):
1194 gp = None
1198 gp = None
1195 if (gitpatches and
1199 if (gitpatches and
1196 gitpatches[-1].ispatching(afile, bfile)):
1200 gitpatches[-1].ispatching(afile, bfile)):
1197 gp = gitpatches.pop()
1201 gp = gitpatches.pop()
1198 if x.startswith('GIT binary patch'):
1202 if x.startswith('GIT binary patch'):
1199 h = binhunk(lr)
1203 h = binhunk(lr)
1200 else:
1204 else:
1201 if context is None and x.startswith('***************'):
1205 if context is None and x.startswith('***************'):
1202 context = True
1206 context = True
1203 h = hunk(x, hunknum + 1, lr, context)
1207 h = hunk(x, hunknum + 1, lr, context)
1204 hunknum += 1
1208 hunknum += 1
1205 if emitfile:
1209 if emitfile:
1206 emitfile = False
1210 emitfile = False
1207 yield 'file', (afile, bfile, h, gp and gp.copy() or None)
1211 yield 'file', (afile, bfile, h, gp and gp.copy() or None)
1208 yield 'hunk', h
1212 yield 'hunk', h
1209 elif x.startswith('diff --git'):
1213 elif x.startswith('diff --git'):
1210 m = gitre.match(x)
1214 m = gitre.match(x)
1211 if not m:
1215 if not m:
1212 continue
1216 continue
1213 if gitpatches is None:
1217 if gitpatches is None:
1214 # scan whole input for git metadata
1218 # scan whole input for git metadata
1215 gitpatches = scangitpatch(lr, x)
1219 gitpatches = scangitpatch(lr, x)
1216 yield 'git', [g.copy() for g in gitpatches
1220 yield 'git', [g.copy() for g in gitpatches
1217 if g.op in ('COPY', 'RENAME')]
1221 if g.op in ('COPY', 'RENAME')]
1218 gitpatches.reverse()
1222 gitpatches.reverse()
1219 afile = 'a/' + m.group(1)
1223 afile = 'a/' + m.group(1)
1220 bfile = 'b/' + m.group(2)
1224 bfile = 'b/' + m.group(2)
1221 while gitpatches and not gitpatches[-1].ispatching(afile, bfile):
1225 while gitpatches and not gitpatches[-1].ispatching(afile, bfile):
1222 gp = gitpatches.pop()
1226 gp = gitpatches.pop()
1223 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1227 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1224 if not gitpatches:
1228 if not gitpatches:
1225 raise PatchError(_('failed to synchronize metadata for "%s"')
1229 raise PatchError(_('failed to synchronize metadata for "%s"')
1226 % afile[2:])
1230 % afile[2:])
1227 gp = gitpatches[-1]
1231 gp = gitpatches[-1]
1228 newfile = True
1232 newfile = True
1229 elif x.startswith('---'):
1233 elif x.startswith('---'):
1230 # check for a unified diff
1234 # check for a unified diff
1231 l2 = lr.readline()
1235 l2 = lr.readline()
1232 if not l2.startswith('+++'):
1236 if not l2.startswith('+++'):
1233 lr.push(l2)
1237 lr.push(l2)
1234 continue
1238 continue
1235 newfile = True
1239 newfile = True
1236 context = False
1240 context = False
1237 afile = parsefilename(x)
1241 afile = parsefilename(x)
1238 bfile = parsefilename(l2)
1242 bfile = parsefilename(l2)
1239 elif x.startswith('***'):
1243 elif x.startswith('***'):
1240 # check for a context diff
1244 # check for a context diff
1241 l2 = lr.readline()
1245 l2 = lr.readline()
1242 if not l2.startswith('---'):
1246 if not l2.startswith('---'):
1243 lr.push(l2)
1247 lr.push(l2)
1244 continue
1248 continue
1245 l3 = lr.readline()
1249 l3 = lr.readline()
1246 lr.push(l3)
1250 lr.push(l3)
1247 if not l3.startswith("***************"):
1251 if not l3.startswith("***************"):
1248 lr.push(l2)
1252 lr.push(l2)
1249 continue
1253 continue
1250 newfile = True
1254 newfile = True
1251 context = True
1255 context = True
1252 afile = parsefilename(x)
1256 afile = parsefilename(x)
1253 bfile = parsefilename(l2)
1257 bfile = parsefilename(l2)
1254
1258
1255 if newfile:
1259 if newfile:
1256 newfile = False
1260 newfile = False
1257 emitfile = True
1261 emitfile = True
1258 state = BFILE
1262 state = BFILE
1259 hunknum = 0
1263 hunknum = 0
1260
1264
1261 while gitpatches:
1265 while gitpatches:
1262 gp = gitpatches.pop()
1266 gp = gitpatches.pop()
1263 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1267 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1264
1268
1265 def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'):
1269 def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'):
1266 """Reads a patch from fp and tries to apply it.
1270 """Reads a patch from fp and tries to apply it.
1267
1271
1268 Returns 0 for a clean patch, -1 if any rejects were found and 1 if
1272 Returns 0 for a clean patch, -1 if any rejects were found and 1 if
1269 there was any fuzz.
1273 there was any fuzz.
1270
1274
1271 If 'eolmode' is 'strict', the patch content and patched file are
1275 If 'eolmode' is 'strict', the patch content and patched file are
1272 read in binary mode. Otherwise, line endings are ignored when
1276 read in binary mode. Otherwise, line endings are ignored when
1273 patching then normalized according to 'eolmode'.
1277 patching then normalized according to 'eolmode'.
1274 """
1278 """
1275 return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
1279 return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
1276 eolmode=eolmode)
1280 eolmode=eolmode)
1277
1281
1278 def _applydiff(ui, fp, patcher, backend, store, strip=1,
1282 def _applydiff(ui, fp, patcher, backend, store, strip=1,
1279 eolmode='strict'):
1283 eolmode='strict'):
1280
1284
1281 def pstrip(p):
1285 def pstrip(p):
1282 return pathstrip(p, strip - 1)[1]
1286 return pathstrip(p, strip - 1)[1]
1283
1287
1284 rejects = 0
1288 rejects = 0
1285 err = 0
1289 err = 0
1286 current_file = None
1290 current_file = None
1287
1291
1288 for state, values in iterhunks(fp):
1292 for state, values in iterhunks(fp):
1289 if state == 'hunk':
1293 if state == 'hunk':
1290 if not current_file:
1294 if not current_file:
1291 continue
1295 continue
1292 ret = current_file.apply(values)
1296 ret = current_file.apply(values)
1293 if ret > 0:
1297 if ret > 0:
1294 err = 1
1298 err = 1
1295 elif state == 'file':
1299 elif state == 'file':
1296 if current_file:
1300 if current_file:
1297 rejects += current_file.close()
1301 rejects += current_file.close()
1298 current_file = None
1302 current_file = None
1299 afile, bfile, first_hunk, gp = values
1303 afile, bfile, first_hunk, gp = values
1300 if gp:
1304 if gp:
1301 gp.path = pstrip(gp.path)
1305 gp.path = pstrip(gp.path)
1302 if gp.oldpath:
1306 if gp.oldpath:
1303 gp.oldpath = pstrip(gp.oldpath)
1307 gp.oldpath = pstrip(gp.oldpath)
1304 else:
1308 else:
1305 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip)
1309 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip)
1306 if gp.op == 'RENAME':
1310 if gp.op == 'RENAME':
1307 backend.unlink(gp.oldpath)
1311 backend.unlink(gp.oldpath)
1308 if not first_hunk:
1312 if not first_hunk:
1309 if gp.op == 'DELETE':
1313 if gp.op == 'DELETE':
1310 backend.unlink(gp.path)
1314 backend.unlink(gp.path)
1311 continue
1315 continue
1312 data, mode = None, None
1316 data, mode = None, None
1313 if gp.op in ('RENAME', 'COPY'):
1317 if gp.op in ('RENAME', 'COPY'):
1314 data, mode = store.getfile(gp.oldpath)[:2]
1318 data, mode = store.getfile(gp.oldpath)[:2]
1315 if gp.mode:
1319 if gp.mode:
1316 mode = gp.mode
1320 mode = gp.mode
1317 if gp.op == 'ADD':
1321 if gp.op == 'ADD':
1318 # Added files without content have no hunk and
1322 # Added files without content have no hunk and
1319 # must be created
1323 # must be created
1320 data = ''
1324 data = ''
1321 if data or mode:
1325 if data or mode:
1322 if (gp.op in ('ADD', 'RENAME', 'COPY')
1326 if (gp.op in ('ADD', 'RENAME', 'COPY')
1323 and backend.exists(gp.path)):
1327 and backend.exists(gp.path)):
1324 raise PatchError(_("cannot create %s: destination "
1328 raise PatchError(_("cannot create %s: destination "
1325 "already exists") % gp.path)
1329 "already exists") % gp.path)
1326 backend.setfile(gp.path, data, mode, gp.oldpath)
1330 backend.setfile(gp.path, data, mode, gp.oldpath)
1327 continue
1331 continue
1328 try:
1332 try:
1329 current_file = patcher(ui, gp, backend, store,
1333 current_file = patcher(ui, gp, backend, store,
1330 eolmode=eolmode)
1334 eolmode=eolmode)
1331 except PatchError, inst:
1335 except PatchError, inst:
1332 ui.warn(str(inst) + '\n')
1336 ui.warn(str(inst) + '\n')
1333 current_file = None
1337 current_file = None
1334 rejects += 1
1338 rejects += 1
1335 continue
1339 continue
1336 elif state == 'git':
1340 elif state == 'git':
1337 for gp in values:
1341 for gp in values:
1338 path = pstrip(gp.oldpath)
1342 path = pstrip(gp.oldpath)
1339 data, mode = backend.getfile(path)
1343 data, mode = backend.getfile(path)
1340 store.setfile(path, data, mode)
1344 store.setfile(path, data, mode)
1341 else:
1345 else:
1342 raise util.Abort(_('unsupported parser state: %s') % state)
1346 raise util.Abort(_('unsupported parser state: %s') % state)
1343
1347
1344 if current_file:
1348 if current_file:
1345 rejects += current_file.close()
1349 rejects += current_file.close()
1346
1350
1347 if rejects:
1351 if rejects:
1348 return -1
1352 return -1
1349 return err
1353 return err
1350
1354
1351 def _externalpatch(ui, repo, patcher, patchname, strip, files,
1355 def _externalpatch(ui, repo, patcher, patchname, strip, files,
1352 similarity):
1356 similarity):
1353 """use <patcher> to apply <patchname> to the working directory.
1357 """use <patcher> to apply <patchname> to the working directory.
1354 returns whether patch was applied with fuzz factor."""
1358 returns whether patch was applied with fuzz factor."""
1355
1359
1356 fuzz = False
1360 fuzz = False
1357 args = []
1361 args = []
1358 cwd = repo.root
1362 cwd = repo.root
1359 if cwd:
1363 if cwd:
1360 args.append('-d %s' % util.shellquote(cwd))
1364 args.append('-d %s' % util.shellquote(cwd))
1361 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip,
1365 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip,
1362 util.shellquote(patchname)))
1366 util.shellquote(patchname)))
1363 try:
1367 try:
1364 for line in fp:
1368 for line in fp:
1365 line = line.rstrip()
1369 line = line.rstrip()
1366 ui.note(line + '\n')
1370 ui.note(line + '\n')
1367 if line.startswith('patching file '):
1371 if line.startswith('patching file '):
1368 pf = util.parsepatchoutput(line)
1372 pf = util.parsepatchoutput(line)
1369 printed_file = False
1373 printed_file = False
1370 files.add(pf)
1374 files.add(pf)
1371 elif line.find('with fuzz') >= 0:
1375 elif line.find('with fuzz') >= 0:
1372 fuzz = True
1376 fuzz = True
1373 if not printed_file:
1377 if not printed_file:
1374 ui.warn(pf + '\n')
1378 ui.warn(pf + '\n')
1375 printed_file = True
1379 printed_file = True
1376 ui.warn(line + '\n')
1380 ui.warn(line + '\n')
1377 elif line.find('saving rejects to file') >= 0:
1381 elif line.find('saving rejects to file') >= 0:
1378 ui.warn(line + '\n')
1382 ui.warn(line + '\n')
1379 elif line.find('FAILED') >= 0:
1383 elif line.find('FAILED') >= 0:
1380 if not printed_file:
1384 if not printed_file:
1381 ui.warn(pf + '\n')
1385 ui.warn(pf + '\n')
1382 printed_file = True
1386 printed_file = True
1383 ui.warn(line + '\n')
1387 ui.warn(line + '\n')
1384 finally:
1388 finally:
1385 if files:
1389 if files:
1386 cfiles = list(files)
1390 cfiles = list(files)
1387 cwd = repo.getcwd()
1391 cwd = repo.getcwd()
1388 if cwd:
1392 if cwd:
1389 cfiles = [util.pathto(repo.root, cwd, f)
1393 cfiles = [util.pathto(repo.root, cwd, f)
1390 for f in cfiles]
1394 for f in cfiles]
1391 scmutil.addremove(repo, cfiles, similarity=similarity)
1395 scmutil.addremove(repo, cfiles, similarity=similarity)
1392 code = fp.close()
1396 code = fp.close()
1393 if code:
1397 if code:
1394 raise PatchError(_("patch command failed: %s") %
1398 raise PatchError(_("patch command failed: %s") %
1395 util.explainexit(code)[0])
1399 util.explainexit(code)[0])
1396 return fuzz
1400 return fuzz
1397
1401
1398 def patchbackend(ui, backend, patchobj, strip, files=None, eolmode='strict'):
1402 def patchbackend(ui, backend, patchobj, strip, files=None, eolmode='strict'):
1399 if files is None:
1403 if files is None:
1400 files = set()
1404 files = set()
1401 if eolmode is None:
1405 if eolmode is None:
1402 eolmode = ui.config('patch', 'eol', 'strict')
1406 eolmode = ui.config('patch', 'eol', 'strict')
1403 if eolmode.lower() not in eolmodes:
1407 if eolmode.lower() not in eolmodes:
1404 raise util.Abort(_('unsupported line endings type: %s') % eolmode)
1408 raise util.Abort(_('unsupported line endings type: %s') % eolmode)
1405 eolmode = eolmode.lower()
1409 eolmode = eolmode.lower()
1406
1410
1407 store = filestore()
1411 store = filestore()
1408 try:
1412 try:
1409 fp = open(patchobj, 'rb')
1413 fp = open(patchobj, 'rb')
1410 except TypeError:
1414 except TypeError:
1411 fp = patchobj
1415 fp = patchobj
1412 try:
1416 try:
1413 ret = applydiff(ui, fp, backend, store, strip=strip,
1417 ret = applydiff(ui, fp, backend, store, strip=strip,
1414 eolmode=eolmode)
1418 eolmode=eolmode)
1415 finally:
1419 finally:
1416 if fp != patchobj:
1420 if fp != patchobj:
1417 fp.close()
1421 fp.close()
1418 files.update(backend.close())
1422 files.update(backend.close())
1419 store.close()
1423 store.close()
1420 if ret < 0:
1424 if ret < 0:
1421 raise PatchError(_('patch failed to apply'))
1425 raise PatchError(_('patch failed to apply'))
1422 return ret > 0
1426 return ret > 0
1423
1427
1424 def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict',
1428 def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict',
1425 similarity=0):
1429 similarity=0):
1426 """use builtin patch to apply <patchobj> to the working directory.
1430 """use builtin patch to apply <patchobj> to the working directory.
1427 returns whether patch was applied with fuzz factor."""
1431 returns whether patch was applied with fuzz factor."""
1428 backend = workingbackend(ui, repo, similarity)
1432 backend = workingbackend(ui, repo, similarity)
1429 return patchbackend(ui, backend, patchobj, strip, files, eolmode)
1433 return patchbackend(ui, backend, patchobj, strip, files, eolmode)
1430
1434
1431 def patchrepo(ui, repo, ctx, store, patchobj, strip, files=None,
1435 def patchrepo(ui, repo, ctx, store, patchobj, strip, files=None,
1432 eolmode='strict'):
1436 eolmode='strict'):
1433 backend = repobackend(ui, repo, ctx, store)
1437 backend = repobackend(ui, repo, ctx, store)
1434 return patchbackend(ui, backend, patchobj, strip, files, eolmode)
1438 return patchbackend(ui, backend, patchobj, strip, files, eolmode)
1435
1439
1436 def makememctx(repo, parents, text, user, date, branch, files, store,
1440 def makememctx(repo, parents, text, user, date, branch, files, store,
1437 editor=None):
1441 editor=None):
1438 def getfilectx(repo, memctx, path):
1442 def getfilectx(repo, memctx, path):
1439 data, (islink, isexec), copied = store.getfile(path)
1443 data, (islink, isexec), copied = store.getfile(path)
1440 return context.memfilectx(path, data, islink=islink, isexec=isexec,
1444 return context.memfilectx(path, data, islink=islink, isexec=isexec,
1441 copied=copied)
1445 copied=copied)
1442 extra = {}
1446 extra = {}
1443 if branch:
1447 if branch:
1444 extra['branch'] = encoding.fromlocal(branch)
1448 extra['branch'] = encoding.fromlocal(branch)
1445 ctx = context.memctx(repo, parents, text, files, getfilectx, user,
1449 ctx = context.memctx(repo, parents, text, files, getfilectx, user,
1446 date, extra)
1450 date, extra)
1447 if editor:
1451 if editor:
1448 ctx._text = editor(repo, ctx, [])
1452 ctx._text = editor(repo, ctx, [])
1449 return ctx
1453 return ctx
1450
1454
1451 def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict',
1455 def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict',
1452 similarity=0):
1456 similarity=0):
1453 """Apply <patchname> to the working directory.
1457 """Apply <patchname> to the working directory.
1454
1458
1455 'eolmode' specifies how end of lines should be handled. It can be:
1459 'eolmode' specifies how end of lines should be handled. It can be:
1456 - 'strict': inputs are read in binary mode, EOLs are preserved
1460 - 'strict': inputs are read in binary mode, EOLs are preserved
1457 - 'crlf': EOLs are ignored when patching and reset to CRLF
1461 - 'crlf': EOLs are ignored when patching and reset to CRLF
1458 - 'lf': EOLs are ignored when patching and reset to LF
1462 - 'lf': EOLs are ignored when patching and reset to LF
1459 - None: get it from user settings, default to 'strict'
1463 - None: get it from user settings, default to 'strict'
1460 'eolmode' is ignored when using an external patcher program.
1464 'eolmode' is ignored when using an external patcher program.
1461
1465
1462 Returns whether patch was applied with fuzz factor.
1466 Returns whether patch was applied with fuzz factor.
1463 """
1467 """
1464 patcher = ui.config('ui', 'patch')
1468 patcher = ui.config('ui', 'patch')
1465 if files is None:
1469 if files is None:
1466 files = set()
1470 files = set()
1467 try:
1471 try:
1468 if patcher:
1472 if patcher:
1469 return _externalpatch(ui, repo, patcher, patchname, strip,
1473 return _externalpatch(ui, repo, patcher, patchname, strip,
1470 files, similarity)
1474 files, similarity)
1471 return internalpatch(ui, repo, patchname, strip, files, eolmode,
1475 return internalpatch(ui, repo, patchname, strip, files, eolmode,
1472 similarity)
1476 similarity)
1473 except PatchError, err:
1477 except PatchError, err:
1474 raise util.Abort(str(err))
1478 raise util.Abort(str(err))
1475
1479
1476 def changedfiles(ui, repo, patchpath, strip=1):
1480 def changedfiles(ui, repo, patchpath, strip=1):
1477 backend = fsbackend(ui, repo.root)
1481 backend = fsbackend(ui, repo.root)
1478 fp = open(patchpath, 'rb')
1482 fp = open(patchpath, 'rb')
1479 try:
1483 try:
1480 changed = set()
1484 changed = set()
1481 for state, values in iterhunks(fp):
1485 for state, values in iterhunks(fp):
1482 if state == 'file':
1486 if state == 'file':
1483 afile, bfile, first_hunk, gp = values
1487 afile, bfile, first_hunk, gp = values
1484 if gp:
1488 if gp:
1485 gp.path = pathstrip(gp.path, strip - 1)[1]
1489 gp.path = pathstrip(gp.path, strip - 1)[1]
1486 if gp.oldpath:
1490 if gp.oldpath:
1487 gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
1491 gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
1488 else:
1492 else:
1489 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip)
1493 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip)
1490 changed.add(gp.path)
1494 changed.add(gp.path)
1491 if gp.op == 'RENAME':
1495 if gp.op == 'RENAME':
1492 changed.add(gp.oldpath)
1496 changed.add(gp.oldpath)
1493 elif state not in ('hunk', 'git'):
1497 elif state not in ('hunk', 'git'):
1494 raise util.Abort(_('unsupported parser state: %s') % state)
1498 raise util.Abort(_('unsupported parser state: %s') % state)
1495 return changed
1499 return changed
1496 finally:
1500 finally:
1497 fp.close()
1501 fp.close()
1498
1502
1499 def b85diff(to, tn):
1503 def b85diff(to, tn):
1500 '''print base85-encoded binary diff'''
1504 '''print base85-encoded binary diff'''
1501 def gitindex(text):
1505 def gitindex(text):
1502 if not text:
1506 if not text:
1503 return hex(nullid)
1507 return hex(nullid)
1504 l = len(text)
1508 l = len(text)
1505 s = util.sha1('blob %d\0' % l)
1509 s = util.sha1('blob %d\0' % l)
1506 s.update(text)
1510 s.update(text)
1507 return s.hexdigest()
1511 return s.hexdigest()
1508
1512
1509 def fmtline(line):
1513 def fmtline(line):
1510 l = len(line)
1514 l = len(line)
1511 if l <= 26:
1515 if l <= 26:
1512 l = chr(ord('A') + l - 1)
1516 l = chr(ord('A') + l - 1)
1513 else:
1517 else:
1514 l = chr(l - 26 + ord('a') - 1)
1518 l = chr(l - 26 + ord('a') - 1)
1515 return '%c%s\n' % (l, base85.b85encode(line, True))
1519 return '%c%s\n' % (l, base85.b85encode(line, True))
1516
1520
1517 def chunk(text, csize=52):
1521 def chunk(text, csize=52):
1518 l = len(text)
1522 l = len(text)
1519 i = 0
1523 i = 0
1520 while i < l:
1524 while i < l:
1521 yield text[i:i + csize]
1525 yield text[i:i + csize]
1522 i += csize
1526 i += csize
1523
1527
1524 tohash = gitindex(to)
1528 tohash = gitindex(to)
1525 tnhash = gitindex(tn)
1529 tnhash = gitindex(tn)
1526 if tohash == tnhash:
1530 if tohash == tnhash:
1527 return ""
1531 return ""
1528
1532
1529 # TODO: deltas
1533 # TODO: deltas
1530 ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' %
1534 ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' %
1531 (tohash, tnhash, len(tn))]
1535 (tohash, tnhash, len(tn))]
1532 for l in chunk(zlib.compress(tn)):
1536 for l in chunk(zlib.compress(tn)):
1533 ret.append(fmtline(l))
1537 ret.append(fmtline(l))
1534 ret.append('\n')
1538 ret.append('\n')
1535 return ''.join(ret)
1539 return ''.join(ret)
1536
1540
1537 class GitDiffRequired(Exception):
1541 class GitDiffRequired(Exception):
1538 pass
1542 pass
1539
1543
1540 def diffopts(ui, opts=None, untrusted=False, section='diff'):
1544 def diffopts(ui, opts=None, untrusted=False, section='diff'):
1541 def get(key, name=None, getter=ui.configbool):
1545 def get(key, name=None, getter=ui.configbool):
1542 return ((opts and opts.get(key)) or
1546 return ((opts and opts.get(key)) or
1543 getter(section, name or key, None, untrusted=untrusted))
1547 getter(section, name or key, None, untrusted=untrusted))
1544 return mdiff.diffopts(
1548 return mdiff.diffopts(
1545 text=opts and opts.get('text'),
1549 text=opts and opts.get('text'),
1546 git=get('git'),
1550 git=get('git'),
1547 nodates=get('nodates'),
1551 nodates=get('nodates'),
1548 showfunc=get('show_function', 'showfunc'),
1552 showfunc=get('show_function', 'showfunc'),
1549 ignorews=get('ignore_all_space', 'ignorews'),
1553 ignorews=get('ignore_all_space', 'ignorews'),
1550 ignorewsamount=get('ignore_space_change', 'ignorewsamount'),
1554 ignorewsamount=get('ignore_space_change', 'ignorewsamount'),
1551 ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'),
1555 ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'),
1552 context=get('unified', getter=ui.config))
1556 context=get('unified', getter=ui.config))
1553
1557
1554 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
1558 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
1555 losedatafn=None, prefix=''):
1559 losedatafn=None, prefix=''):
1556 '''yields diff of changes to files between two nodes, or node and
1560 '''yields diff of changes to files between two nodes, or node and
1557 working directory.
1561 working directory.
1558
1562
1559 if node1 is None, use first dirstate parent instead.
1563 if node1 is None, use first dirstate parent instead.
1560 if node2 is None, compare node1 with working directory.
1564 if node2 is None, compare node1 with working directory.
1561
1565
1562 losedatafn(**kwarg) is a callable run when opts.upgrade=True and
1566 losedatafn(**kwarg) is a callable run when opts.upgrade=True and
1563 every time some change cannot be represented with the current
1567 every time some change cannot be represented with the current
1564 patch format. Return False to upgrade to git patch format, True to
1568 patch format. Return False to upgrade to git patch format, True to
1565 accept the loss or raise an exception to abort the diff. It is
1569 accept the loss or raise an exception to abort the diff. It is
1566 called with the name of current file being diffed as 'fn'. If set
1570 called with the name of current file being diffed as 'fn'. If set
1567 to None, patches will always be upgraded to git format when
1571 to None, patches will always be upgraded to git format when
1568 necessary.
1572 necessary.
1569
1573
1570 prefix is a filename prefix that is prepended to all filenames on
1574 prefix is a filename prefix that is prepended to all filenames on
1571 display (used for subrepos).
1575 display (used for subrepos).
1572 '''
1576 '''
1573
1577
1574 if opts is None:
1578 if opts is None:
1575 opts = mdiff.defaultopts
1579 opts = mdiff.defaultopts
1576
1580
1577 if not node1 and not node2:
1581 if not node1 and not node2:
1578 node1 = repo.dirstate.p1()
1582 node1 = repo.dirstate.p1()
1579
1583
1580 def lrugetfilectx():
1584 def lrugetfilectx():
1581 cache = {}
1585 cache = {}
1582 order = []
1586 order = []
1583 def getfilectx(f, ctx):
1587 def getfilectx(f, ctx):
1584 fctx = ctx.filectx(f, filelog=cache.get(f))
1588 fctx = ctx.filectx(f, filelog=cache.get(f))
1585 if f not in cache:
1589 if f not in cache:
1586 if len(cache) > 20:
1590 if len(cache) > 20:
1587 del cache[order.pop(0)]
1591 del cache[order.pop(0)]
1588 cache[f] = fctx.filelog()
1592 cache[f] = fctx.filelog()
1589 else:
1593 else:
1590 order.remove(f)
1594 order.remove(f)
1591 order.append(f)
1595 order.append(f)
1592 return fctx
1596 return fctx
1593 return getfilectx
1597 return getfilectx
1594 getfilectx = lrugetfilectx()
1598 getfilectx = lrugetfilectx()
1595
1599
1596 ctx1 = repo[node1]
1600 ctx1 = repo[node1]
1597 ctx2 = repo[node2]
1601 ctx2 = repo[node2]
1598
1602
1599 if not changes:
1603 if not changes:
1600 changes = repo.status(ctx1, ctx2, match=match)
1604 changes = repo.status(ctx1, ctx2, match=match)
1601 modified, added, removed = changes[:3]
1605 modified, added, removed = changes[:3]
1602
1606
1603 if not modified and not added and not removed:
1607 if not modified and not added and not removed:
1604 return []
1608 return []
1605
1609
1606 revs = None
1610 revs = None
1607 if not repo.ui.quiet:
1611 if not repo.ui.quiet:
1608 hexfunc = repo.ui.debugflag and hex or short
1612 hexfunc = repo.ui.debugflag and hex or short
1609 revs = [hexfunc(node) for node in [node1, node2] if node]
1613 revs = [hexfunc(node) for node in [node1, node2] if node]
1610
1614
1611 copy = {}
1615 copy = {}
1612 if opts.git or opts.upgrade:
1616 if opts.git or opts.upgrade:
1613 copy = copies.pathcopies(ctx1, ctx2)
1617 copy = copies.pathcopies(ctx1, ctx2)
1614
1618
1615 difffn = lambda opts, losedata: trydiff(repo, revs, ctx1, ctx2,
1619 difffn = lambda opts, losedata: trydiff(repo, revs, ctx1, ctx2,
1616 modified, added, removed, copy, getfilectx, opts, losedata, prefix)
1620 modified, added, removed, copy, getfilectx, opts, losedata, prefix)
1617 if opts.upgrade and not opts.git:
1621 if opts.upgrade and not opts.git:
1618 try:
1622 try:
1619 def losedata(fn):
1623 def losedata(fn):
1620 if not losedatafn or not losedatafn(fn=fn):
1624 if not losedatafn or not losedatafn(fn=fn):
1621 raise GitDiffRequired()
1625 raise GitDiffRequired()
1622 # Buffer the whole output until we are sure it can be generated
1626 # Buffer the whole output until we are sure it can be generated
1623 return list(difffn(opts.copy(git=False), losedata))
1627 return list(difffn(opts.copy(git=False), losedata))
1624 except GitDiffRequired:
1628 except GitDiffRequired:
1625 return difffn(opts.copy(git=True), None)
1629 return difffn(opts.copy(git=True), None)
1626 else:
1630 else:
1627 return difffn(opts, None)
1631 return difffn(opts, None)
1628
1632
1629 def difflabel(func, *args, **kw):
1633 def difflabel(func, *args, **kw):
1630 '''yields 2-tuples of (output, label) based on the output of func()'''
1634 '''yields 2-tuples of (output, label) based on the output of func()'''
1631 headprefixes = [('diff', 'diff.diffline'),
1635 headprefixes = [('diff', 'diff.diffline'),
1632 ('copy', 'diff.extended'),
1636 ('copy', 'diff.extended'),
1633 ('rename', 'diff.extended'),
1637 ('rename', 'diff.extended'),
1634 ('old', 'diff.extended'),
1638 ('old', 'diff.extended'),
1635 ('new', 'diff.extended'),
1639 ('new', 'diff.extended'),
1636 ('deleted', 'diff.extended'),
1640 ('deleted', 'diff.extended'),
1637 ('---', 'diff.file_a'),
1641 ('---', 'diff.file_a'),
1638 ('+++', 'diff.file_b')]
1642 ('+++', 'diff.file_b')]
1639 textprefixes = [('@', 'diff.hunk'),
1643 textprefixes = [('@', 'diff.hunk'),
1640 ('-', 'diff.deleted'),
1644 ('-', 'diff.deleted'),
1641 ('+', 'diff.inserted')]
1645 ('+', 'diff.inserted')]
1642 head = False
1646 head = False
1643 for chunk in func(*args, **kw):
1647 for chunk in func(*args, **kw):
1644 lines = chunk.split('\n')
1648 lines = chunk.split('\n')
1645 for i, line in enumerate(lines):
1649 for i, line in enumerate(lines):
1646 if i != 0:
1650 if i != 0:
1647 yield ('\n', '')
1651 yield ('\n', '')
1648 if head:
1652 if head:
1649 if line.startswith('@'):
1653 if line.startswith('@'):
1650 head = False
1654 head = False
1651 else:
1655 else:
1652 if line and not line[0] in ' +-@\\':
1656 if line and not line[0] in ' +-@\\':
1653 head = True
1657 head = True
1654 stripline = line
1658 stripline = line
1655 if not head and line and line[0] in '+-':
1659 if not head and line and line[0] in '+-':
1656 # highlight trailing whitespace, but only in changed lines
1660 # highlight trailing whitespace, but only in changed lines
1657 stripline = line.rstrip()
1661 stripline = line.rstrip()
1658 prefixes = textprefixes
1662 prefixes = textprefixes
1659 if head:
1663 if head:
1660 prefixes = headprefixes
1664 prefixes = headprefixes
1661 for prefix, label in prefixes:
1665 for prefix, label in prefixes:
1662 if stripline.startswith(prefix):
1666 if stripline.startswith(prefix):
1663 yield (stripline, label)
1667 yield (stripline, label)
1664 break
1668 break
1665 else:
1669 else:
1666 yield (line, '')
1670 yield (line, '')
1667 if line != stripline:
1671 if line != stripline:
1668 yield (line[len(stripline):], 'diff.trailingwhitespace')
1672 yield (line[len(stripline):], 'diff.trailingwhitespace')
1669
1673
1670 def diffui(*args, **kw):
1674 def diffui(*args, **kw):
1671 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
1675 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
1672 return difflabel(diff, *args, **kw)
1676 return difflabel(diff, *args, **kw)
1673
1677
1674
1678
1675 def _addmodehdr(header, omode, nmode):
1679 def _addmodehdr(header, omode, nmode):
1676 if omode != nmode:
1680 if omode != nmode:
1677 header.append('old mode %s\n' % omode)
1681 header.append('old mode %s\n' % omode)
1678 header.append('new mode %s\n' % nmode)
1682 header.append('new mode %s\n' % nmode)
1679
1683
1680 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
1684 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
1681 copy, getfilectx, opts, losedatafn, prefix):
1685 copy, getfilectx, opts, losedatafn, prefix):
1682
1686
1683 def join(f):
1687 def join(f):
1684 return os.path.join(prefix, f)
1688 return os.path.join(prefix, f)
1685
1689
1686 date1 = util.datestr(ctx1.date())
1690 date1 = util.datestr(ctx1.date())
1687 man1 = ctx1.manifest()
1691 man1 = ctx1.manifest()
1688
1692
1689 gone = set()
1693 gone = set()
1690 gitmode = {'l': '120000', 'x': '100755', '': '100644'}
1694 gitmode = {'l': '120000', 'x': '100755', '': '100644'}
1691
1695
1692 copyto = dict([(v, k) for k, v in copy.items()])
1696 copyto = dict([(v, k) for k, v in copy.items()])
1693
1697
1694 if opts.git:
1698 if opts.git:
1695 revs = None
1699 revs = None
1696
1700
1697 for f in sorted(modified + added + removed):
1701 for f in sorted(modified + added + removed):
1698 to = None
1702 to = None
1699 tn = None
1703 tn = None
1700 dodiff = True
1704 dodiff = True
1701 header = []
1705 header = []
1702 if f in man1:
1706 if f in man1:
1703 to = getfilectx(f, ctx1).data()
1707 to = getfilectx(f, ctx1).data()
1704 if f not in removed:
1708 if f not in removed:
1705 tn = getfilectx(f, ctx2).data()
1709 tn = getfilectx(f, ctx2).data()
1706 a, b = f, f
1710 a, b = f, f
1707 if opts.git or losedatafn:
1711 if opts.git or losedatafn:
1708 if f in added:
1712 if f in added:
1709 mode = gitmode[ctx2.flags(f)]
1713 mode = gitmode[ctx2.flags(f)]
1710 if f in copy or f in copyto:
1714 if f in copy or f in copyto:
1711 if opts.git:
1715 if opts.git:
1712 if f in copy:
1716 if f in copy:
1713 a = copy[f]
1717 a = copy[f]
1714 else:
1718 else:
1715 a = copyto[f]
1719 a = copyto[f]
1716 omode = gitmode[man1.flags(a)]
1720 omode = gitmode[man1.flags(a)]
1717 _addmodehdr(header, omode, mode)
1721 _addmodehdr(header, omode, mode)
1718 if a in removed and a not in gone:
1722 if a in removed and a not in gone:
1719 op = 'rename'
1723 op = 'rename'
1720 gone.add(a)
1724 gone.add(a)
1721 else:
1725 else:
1722 op = 'copy'
1726 op = 'copy'
1723 header.append('%s from %s\n' % (op, join(a)))
1727 header.append('%s from %s\n' % (op, join(a)))
1724 header.append('%s to %s\n' % (op, join(f)))
1728 header.append('%s to %s\n' % (op, join(f)))
1725 to = getfilectx(a, ctx1).data()
1729 to = getfilectx(a, ctx1).data()
1726 else:
1730 else:
1727 losedatafn(f)
1731 losedatafn(f)
1728 else:
1732 else:
1729 if opts.git:
1733 if opts.git:
1730 header.append('new file mode %s\n' % mode)
1734 header.append('new file mode %s\n' % mode)
1731 elif ctx2.flags(f):
1735 elif ctx2.flags(f):
1732 losedatafn(f)
1736 losedatafn(f)
1733 # In theory, if tn was copied or renamed we should check
1737 # In theory, if tn was copied or renamed we should check
1734 # if the source is binary too but the copy record already
1738 # if the source is binary too but the copy record already
1735 # forces git mode.
1739 # forces git mode.
1736 if util.binary(tn):
1740 if util.binary(tn):
1737 if opts.git:
1741 if opts.git:
1738 dodiff = 'binary'
1742 dodiff = 'binary'
1739 else:
1743 else:
1740 losedatafn(f)
1744 losedatafn(f)
1741 if not opts.git and not tn:
1745 if not opts.git and not tn:
1742 # regular diffs cannot represent new empty file
1746 # regular diffs cannot represent new empty file
1743 losedatafn(f)
1747 losedatafn(f)
1744 elif f in removed:
1748 elif f in removed:
1745 if opts.git:
1749 if opts.git:
1746 # have we already reported a copy above?
1750 # have we already reported a copy above?
1747 if ((f in copy and copy[f] in added
1751 if ((f in copy and copy[f] in added
1748 and copyto[copy[f]] == f) or
1752 and copyto[copy[f]] == f) or
1749 (f in copyto and copyto[f] in added
1753 (f in copyto and copyto[f] in added
1750 and copy[copyto[f]] == f)):
1754 and copy[copyto[f]] == f)):
1751 dodiff = False
1755 dodiff = False
1752 else:
1756 else:
1753 header.append('deleted file mode %s\n' %
1757 header.append('deleted file mode %s\n' %
1754 gitmode[man1.flags(f)])
1758 gitmode[man1.flags(f)])
1755 elif not to or util.binary(to):
1759 elif not to or util.binary(to):
1756 # regular diffs cannot represent empty file deletion
1760 # regular diffs cannot represent empty file deletion
1757 losedatafn(f)
1761 losedatafn(f)
1758 else:
1762 else:
1759 oflag = man1.flags(f)
1763 oflag = man1.flags(f)
1760 nflag = ctx2.flags(f)
1764 nflag = ctx2.flags(f)
1761 binary = util.binary(to) or util.binary(tn)
1765 binary = util.binary(to) or util.binary(tn)
1762 if opts.git:
1766 if opts.git:
1763 _addmodehdr(header, gitmode[oflag], gitmode[nflag])
1767 _addmodehdr(header, gitmode[oflag], gitmode[nflag])
1764 if binary:
1768 if binary:
1765 dodiff = 'binary'
1769 dodiff = 'binary'
1766 elif binary or nflag != oflag:
1770 elif binary or nflag != oflag:
1767 losedatafn(f)
1771 losedatafn(f)
1768 if opts.git:
1772 if opts.git:
1769 header.insert(0, mdiff.diffline(revs, join(a), join(b), opts))
1773 header.insert(0, mdiff.diffline(revs, join(a), join(b), opts))
1770
1774
1771 if dodiff:
1775 if dodiff:
1772 if dodiff == 'binary':
1776 if dodiff == 'binary':
1773 text = b85diff(to, tn)
1777 text = b85diff(to, tn)
1774 else:
1778 else:
1775 text = mdiff.unidiff(to, date1,
1779 text = mdiff.unidiff(to, date1,
1776 # ctx2 date may be dynamic
1780 # ctx2 date may be dynamic
1777 tn, util.datestr(ctx2.date()),
1781 tn, util.datestr(ctx2.date()),
1778 join(a), join(b), revs, opts=opts)
1782 join(a), join(b), revs, opts=opts)
1779 if header and (text or len(header) > 1):
1783 if header and (text or len(header) > 1):
1780 yield ''.join(header)
1784 yield ''.join(header)
1781 if text:
1785 if text:
1782 yield text
1786 yield text
1783
1787
1784 def diffstatsum(stats):
1788 def diffstatsum(stats):
1785 maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False
1789 maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False
1786 for f, a, r, b in stats:
1790 for f, a, r, b in stats:
1787 maxfile = max(maxfile, encoding.colwidth(f))
1791 maxfile = max(maxfile, encoding.colwidth(f))
1788 maxtotal = max(maxtotal, a + r)
1792 maxtotal = max(maxtotal, a + r)
1789 addtotal += a
1793 addtotal += a
1790 removetotal += r
1794 removetotal += r
1791 binary = binary or b
1795 binary = binary or b
1792
1796
1793 return maxfile, maxtotal, addtotal, removetotal, binary
1797 return maxfile, maxtotal, addtotal, removetotal, binary
1794
1798
1795 def diffstatdata(lines):
1799 def diffstatdata(lines):
1796 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
1800 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
1797
1801
1798 results = []
1802 results = []
1799 filename, adds, removes, isbinary = None, 0, 0, False
1803 filename, adds, removes, isbinary = None, 0, 0, False
1800
1804
1801 def addresult():
1805 def addresult():
1802 if filename:
1806 if filename:
1803 results.append((filename, adds, removes, isbinary))
1807 results.append((filename, adds, removes, isbinary))
1804
1808
1805 for line in lines:
1809 for line in lines:
1806 if line.startswith('diff'):
1810 if line.startswith('diff'):
1807 addresult()
1811 addresult()
1808 # set numbers to 0 anyway when starting new file
1812 # set numbers to 0 anyway when starting new file
1809 adds, removes, isbinary = 0, 0, False
1813 adds, removes, isbinary = 0, 0, False
1810 if line.startswith('diff --git'):
1814 if line.startswith('diff --git'):
1811 filename = gitre.search(line).group(1)
1815 filename = gitre.search(line).group(1)
1812 elif line.startswith('diff -r'):
1816 elif line.startswith('diff -r'):
1813 # format: "diff -r ... -r ... filename"
1817 # format: "diff -r ... -r ... filename"
1814 filename = diffre.search(line).group(1)
1818 filename = diffre.search(line).group(1)
1815 elif line.startswith('+') and not line.startswith('+++ '):
1819 elif line.startswith('+') and not line.startswith('+++ '):
1816 adds += 1
1820 adds += 1
1817 elif line.startswith('-') and not line.startswith('--- '):
1821 elif line.startswith('-') and not line.startswith('--- '):
1818 removes += 1
1822 removes += 1
1819 elif (line.startswith('GIT binary patch') or
1823 elif (line.startswith('GIT binary patch') or
1820 line.startswith('Binary file')):
1824 line.startswith('Binary file')):
1821 isbinary = True
1825 isbinary = True
1822 addresult()
1826 addresult()
1823 return results
1827 return results
1824
1828
1825 def diffstat(lines, width=80, git=False):
1829 def diffstat(lines, width=80, git=False):
1826 output = []
1830 output = []
1827 stats = diffstatdata(lines)
1831 stats = diffstatdata(lines)
1828 maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats)
1832 maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats)
1829
1833
1830 countwidth = len(str(maxtotal))
1834 countwidth = len(str(maxtotal))
1831 if hasbinary and countwidth < 3:
1835 if hasbinary and countwidth < 3:
1832 countwidth = 3
1836 countwidth = 3
1833 graphwidth = width - countwidth - maxname - 6
1837 graphwidth = width - countwidth - maxname - 6
1834 if graphwidth < 10:
1838 if graphwidth < 10:
1835 graphwidth = 10
1839 graphwidth = 10
1836
1840
1837 def scale(i):
1841 def scale(i):
1838 if maxtotal <= graphwidth:
1842 if maxtotal <= graphwidth:
1839 return i
1843 return i
1840 # If diffstat runs out of room it doesn't print anything,
1844 # If diffstat runs out of room it doesn't print anything,
1841 # which isn't very useful, so always print at least one + or -
1845 # which isn't very useful, so always print at least one + or -
1842 # if there were at least some changes.
1846 # if there were at least some changes.
1843 return max(i * graphwidth // maxtotal, int(bool(i)))
1847 return max(i * graphwidth // maxtotal, int(bool(i)))
1844
1848
1845 for filename, adds, removes, isbinary in stats:
1849 for filename, adds, removes, isbinary in stats:
1846 if isbinary:
1850 if isbinary:
1847 count = 'Bin'
1851 count = 'Bin'
1848 else:
1852 else:
1849 count = adds + removes
1853 count = adds + removes
1850 pluses = '+' * scale(adds)
1854 pluses = '+' * scale(adds)
1851 minuses = '-' * scale(removes)
1855 minuses = '-' * scale(removes)
1852 output.append(' %s%s | %*s %s%s\n' %
1856 output.append(' %s%s | %*s %s%s\n' %
1853 (filename, ' ' * (maxname - encoding.colwidth(filename)),
1857 (filename, ' ' * (maxname - encoding.colwidth(filename)),
1854 countwidth, count, pluses, minuses))
1858 countwidth, count, pluses, minuses))
1855
1859
1856 if stats:
1860 if stats:
1857 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
1861 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
1858 % (len(stats), totaladds, totalremoves))
1862 % (len(stats), totaladds, totalremoves))
1859
1863
1860 return ''.join(output)
1864 return ''.join(output)
1861
1865
1862 def diffstatui(*args, **kw):
1866 def diffstatui(*args, **kw):
1863 '''like diffstat(), but yields 2-tuples of (output, label) for
1867 '''like diffstat(), but yields 2-tuples of (output, label) for
1864 ui.write()
1868 ui.write()
1865 '''
1869 '''
1866
1870
1867 for line in diffstat(*args, **kw).splitlines():
1871 for line in diffstat(*args, **kw).splitlines():
1868 if line and line[-1] in '+-':
1872 if line and line[-1] in '+-':
1869 name, graph = line.rsplit(' ', 1)
1873 name, graph = line.rsplit(' ', 1)
1870 yield (name + ' ', '')
1874 yield (name + ' ', '')
1871 m = re.search(r'\++', graph)
1875 m = re.search(r'\++', graph)
1872 if m:
1876 if m:
1873 yield (m.group(0), 'diffstat.inserted')
1877 yield (m.group(0), 'diffstat.inserted')
1874 m = re.search(r'-+', graph)
1878 m = re.search(r'-+', graph)
1875 if m:
1879 if m:
1876 yield (m.group(0), 'diffstat.deleted')
1880 yield (m.group(0), 'diffstat.deleted')
1877 else:
1881 else:
1878 yield (line, '')
1882 yield (line, '')
1879 yield ('\n', '')
1883 yield ('\n', '')
@@ -1,510 +1,529 b''
1 $ "$TESTDIR/hghave" symlink || exit 80
1 $ "$TESTDIR/hghave" symlink || exit 80
2
2
3 $ hg init
3 $ hg init
4
4
5 New file:
5 New file:
6
6
7 $ hg import -d "1000000 0" -mnew - <<EOF
7 $ hg import -d "1000000 0" -mnew - <<EOF
8 > diff --git a/new b/new
8 > diff --git a/new b/new
9 > new file mode 100644
9 > new file mode 100644
10 > index 0000000..7898192
10 > index 0000000..7898192
11 > --- /dev/null
11 > --- /dev/null
12 > +++ b/new
12 > +++ b/new
13 > @@ -0,0 +1 @@
13 > @@ -0,0 +1 @@
14 > +a
14 > +a
15 > EOF
15 > EOF
16 applying patch from stdin
16 applying patch from stdin
17
17
18 $ hg tip -q
18 $ hg tip -q
19 0:ae3ee40d2079
19 0:ae3ee40d2079
20
20
21 New empty file:
21 New empty file:
22
22
23 $ hg import -d "1000000 0" -mempty - <<EOF
23 $ hg import -d "1000000 0" -mempty - <<EOF
24 > diff --git a/empty b/empty
24 > diff --git a/empty b/empty
25 > new file mode 100644
25 > new file mode 100644
26 > EOF
26 > EOF
27 applying patch from stdin
27 applying patch from stdin
28
28
29 $ hg tip -q
29 $ hg tip -q
30 1:ab199dc869b5
30 1:ab199dc869b5
31
31
32 $ hg locate empty
32 $ hg locate empty
33 empty
33 empty
34
34
35 chmod +x:
35 chmod +x:
36
36
37 $ hg import -d "1000000 0" -msetx - <<EOF
37 $ hg import -d "1000000 0" -msetx - <<EOF
38 > diff --git a/new b/new
38 > diff --git a/new b/new
39 > old mode 100644
39 > old mode 100644
40 > new mode 100755
40 > new mode 100755
41 > EOF
41 > EOF
42 applying patch from stdin
42 applying patch from stdin
43
43
44 $ hg tip -q
44 $ hg tip -q
45 2:3a34410f282e
45 2:3a34410f282e
46
46
47 $ test -x new
47 $ test -x new
48
48
49 Copy:
49 Copy:
50
50
51 $ hg import -d "1000000 0" -mcopy - <<EOF
51 $ hg import -d "1000000 0" -mcopy - <<EOF
52 > diff --git a/new b/copy
52 > diff --git a/new b/copy
53 > old mode 100755
53 > old mode 100755
54 > new mode 100644
54 > new mode 100644
55 > similarity index 100%
55 > similarity index 100%
56 > copy from new
56 > copy from new
57 > copy to copy
57 > copy to copy
58 > diff --git a/new b/copyx
58 > diff --git a/new b/copyx
59 > similarity index 100%
59 > similarity index 100%
60 > copy from new
60 > copy from new
61 > copy to copyx
61 > copy to copyx
62 > EOF
62 > EOF
63 applying patch from stdin
63 applying patch from stdin
64
64
65 $ hg tip -q
65 $ hg tip -q
66 3:37bacb7ca14d
66 3:37bacb7ca14d
67
67
68 $ if "$TESTDIR/hghave" -q execbit; then
68 $ if "$TESTDIR/hghave" -q execbit; then
69 > test -f copy -a ! -x copy || echo bad
69 > test -f copy -a ! -x copy || echo bad
70 > test -x copyx || echo bad
70 > test -x copyx || echo bad
71 > else
71 > else
72 > test -f copy || echo bad
72 > test -f copy || echo bad
73 > fi
73 > fi
74
74
75 $ cat copy
75 $ cat copy
76 a
76 a
77
77
78 $ hg cat copy
78 $ hg cat copy
79 a
79 a
80
80
81 Rename:
81 Rename:
82
82
83 $ hg import -d "1000000 0" -mrename - <<EOF
83 $ hg import -d "1000000 0" -mrename - <<EOF
84 > diff --git a/copy b/rename
84 > diff --git a/copy b/rename
85 > similarity index 100%
85 > similarity index 100%
86 > rename from copy
86 > rename from copy
87 > rename to rename
87 > rename to rename
88 > EOF
88 > EOF
89 applying patch from stdin
89 applying patch from stdin
90
90
91 $ hg tip -q
91 $ hg tip -q
92 4:47b81a94361d
92 4:47b81a94361d
93
93
94 $ hg locate
94 $ hg locate
95 copyx
95 copyx
96 empty
96 empty
97 new
97 new
98 rename
98 rename
99
99
100 Delete:
100 Delete:
101
101
102 $ hg import -d "1000000 0" -mdelete - <<EOF
102 $ hg import -d "1000000 0" -mdelete - <<EOF
103 > diff --git a/copyx b/copyx
103 > diff --git a/copyx b/copyx
104 > deleted file mode 100755
104 > deleted file mode 100755
105 > index 7898192..0000000
105 > index 7898192..0000000
106 > --- a/copyx
106 > --- a/copyx
107 > +++ /dev/null
107 > +++ /dev/null
108 > @@ -1 +0,0 @@
108 > @@ -1 +0,0 @@
109 > -a
109 > -a
110 > EOF
110 > EOF
111 applying patch from stdin
111 applying patch from stdin
112
112
113 $ hg tip -q
113 $ hg tip -q
114 5:d9b001d98336
114 5:d9b001d98336
115
115
116 $ hg locate
116 $ hg locate
117 empty
117 empty
118 new
118 new
119 rename
119 rename
120
120
121 $ test -f copyx
121 $ test -f copyx
122 [1]
122 [1]
123
123
124 Regular diff:
124 Regular diff:
125
125
126 $ hg import -d "1000000 0" -mregular - <<EOF
126 $ hg import -d "1000000 0" -mregular - <<EOF
127 > diff --git a/rename b/rename
127 > diff --git a/rename b/rename
128 > index 7898192..72e1fe3 100644
128 > index 7898192..72e1fe3 100644
129 > --- a/rename
129 > --- a/rename
130 > +++ b/rename
130 > +++ b/rename
131 > @@ -1 +1,5 @@
131 > @@ -1 +1,5 @@
132 > a
132 > a
133 > +a
133 > +a
134 > +a
134 > +a
135 > +a
135 > +a
136 > +a
136 > +a
137 > EOF
137 > EOF
138 applying patch from stdin
138 applying patch from stdin
139
139
140 $ hg tip -q
140 $ hg tip -q
141 6:ebe901e7576b
141 6:ebe901e7576b
142
142
143 Copy and modify:
143 Copy and modify:
144
144
145 $ hg import -d "1000000 0" -mcopymod - <<EOF
145 $ hg import -d "1000000 0" -mcopymod - <<EOF
146 > diff --git a/rename b/copy2
146 > diff --git a/rename b/copy2
147 > similarity index 80%
147 > similarity index 80%
148 > copy from rename
148 > copy from rename
149 > copy to copy2
149 > copy to copy2
150 > index 72e1fe3..b53c148 100644
150 > index 72e1fe3..b53c148 100644
151 > --- a/rename
151 > --- a/rename
152 > +++ b/copy2
152 > +++ b/copy2
153 > @@ -1,5 +1,5 @@
153 > @@ -1,5 +1,5 @@
154 > a
154 > a
155 > a
155 > a
156 > -a
156 > -a
157 > +b
157 > +b
158 > a
158 > a
159 > a
159 > a
160 > EOF
160 > EOF
161 applying patch from stdin
161 applying patch from stdin
162
162
163 $ hg tip -q
163 $ hg tip -q
164 7:18f368958ecd
164 7:18f368958ecd
165
165
166 $ hg cat copy2
166 $ hg cat copy2
167 a
167 a
168 a
168 a
169 b
169 b
170 a
170 a
171 a
171 a
172
172
173 Rename and modify:
173 Rename and modify:
174
174
175 $ hg import -d "1000000 0" -mrenamemod - <<EOF
175 $ hg import -d "1000000 0" -mrenamemod - <<EOF
176 > diff --git a/copy2 b/rename2
176 > diff --git a/copy2 b/rename2
177 > similarity index 80%
177 > similarity index 80%
178 > rename from copy2
178 > rename from copy2
179 > rename to rename2
179 > rename to rename2
180 > index b53c148..8f81e29 100644
180 > index b53c148..8f81e29 100644
181 > --- a/copy2
181 > --- a/copy2
182 > +++ b/rename2
182 > +++ b/rename2
183 > @@ -1,5 +1,5 @@
183 > @@ -1,5 +1,5 @@
184 > a
184 > a
185 > a
185 > a
186 > b
186 > b
187 > -a
187 > -a
188 > +c
188 > +c
189 > a
189 > a
190 > EOF
190 > EOF
191 applying patch from stdin
191 applying patch from stdin
192
192
193 $ hg tip -q
193 $ hg tip -q
194 8:c32b0d7e6f44
194 8:c32b0d7e6f44
195
195
196 $ hg locate copy2
196 $ hg locate copy2
197 [1]
197 [1]
198 $ hg cat rename2
198 $ hg cat rename2
199 a
199 a
200 a
200 a
201 b
201 b
202 c
202 c
203 a
203 a
204
204
205 One file renamed multiple times:
205 One file renamed multiple times:
206
206
207 $ hg import -d "1000000 0" -mmultirenames - <<EOF
207 $ hg import -d "1000000 0" -mmultirenames - <<EOF
208 > diff --git a/rename2 b/rename3
208 > diff --git a/rename2 b/rename3
209 > rename from rename2
209 > rename from rename2
210 > rename to rename3
210 > rename to rename3
211 > diff --git a/rename2 b/rename3-2
211 > diff --git a/rename2 b/rename3-2
212 > rename from rename2
212 > rename from rename2
213 > rename to rename3-2
213 > rename to rename3-2
214 > EOF
214 > EOF
215 applying patch from stdin
215 applying patch from stdin
216
216
217 $ hg tip -q
217 $ hg tip -q
218 9:034a6bf95330
218 9:034a6bf95330
219
219
220 $ hg log -vr. --template '{rev} {files} / {file_copies}\n'
220 $ hg log -vr. --template '{rev} {files} / {file_copies}\n'
221 9 rename2 rename3 rename3-2 / rename3 (rename2)rename3-2 (rename2)
221 9 rename2 rename3 rename3-2 / rename3 (rename2)rename3-2 (rename2)
222
222
223 $ hg locate rename2 rename3 rename3-2
223 $ hg locate rename2 rename3 rename3-2
224 rename3
224 rename3
225 rename3-2
225 rename3-2
226
226
227 $ hg cat rename3
227 $ hg cat rename3
228 a
228 a
229 a
229 a
230 b
230 b
231 c
231 c
232 a
232 a
233
233
234 $ hg cat rename3-2
234 $ hg cat rename3-2
235 a
235 a
236 a
236 a
237 b
237 b
238 c
238 c
239 a
239 a
240
240
241 $ echo foo > foo
241 $ echo foo > foo
242 $ hg add foo
242 $ hg add foo
243 $ hg ci -m 'add foo'
243 $ hg ci -m 'add foo'
244
244
245 Binary files and regular patch hunks:
245 Binary files and regular patch hunks:
246
246
247 $ hg import -d "1000000 0" -m binaryregular - <<EOF
247 $ hg import -d "1000000 0" -m binaryregular - <<EOF
248 > diff --git a/binary b/binary
248 > diff --git a/binary b/binary
249 > new file mode 100644
249 > new file mode 100644
250 > index 0000000000000000000000000000000000000000..593f4708db84ac8fd0f5cc47c634f38c013fe9e4
250 > index 0000000000000000000000000000000000000000..593f4708db84ac8fd0f5cc47c634f38c013fe9e4
251 > GIT binary patch
251 > GIT binary patch
252 > literal 4
252 > literal 4
253 > Lc\${NkU|;|M00aO5
253 > Lc\${NkU|;|M00aO5
254 >
254 >
255 > diff --git a/foo b/foo2
255 > diff --git a/foo b/foo2
256 > rename from foo
256 > rename from foo
257 > rename to foo2
257 > rename to foo2
258 > EOF
258 > EOF
259 applying patch from stdin
259 applying patch from stdin
260
260
261 $ hg tip -q
261 $ hg tip -q
262 11:c39bce63e786
262 11:c39bce63e786
263
263
264 $ cat foo2
264 $ cat foo2
265 foo
265 foo
266
266
267 $ hg manifest --debug | grep binary
267 $ hg manifest --debug | grep binary
268 045c85ba38952325e126c70962cc0f9d9077bc67 644 binary
268 045c85ba38952325e126c70962cc0f9d9077bc67 644 binary
269
269
270 Multiple binary files:
270 Multiple binary files:
271
271
272 $ hg import -d "1000000 0" -m multibinary - <<EOF
272 $ hg import -d "1000000 0" -m multibinary - <<EOF
273 > diff --git a/mbinary1 b/mbinary1
273 > diff --git a/mbinary1 b/mbinary1
274 > new file mode 100644
274 > new file mode 100644
275 > index 0000000000000000000000000000000000000000..593f4708db84ac8fd0f5cc47c634f38c013fe9e4
275 > index 0000000000000000000000000000000000000000..593f4708db84ac8fd0f5cc47c634f38c013fe9e4
276 > GIT binary patch
276 > GIT binary patch
277 > literal 4
277 > literal 4
278 > Lc\${NkU|;|M00aO5
278 > Lc\${NkU|;|M00aO5
279 >
279 >
280 > diff --git a/mbinary2 b/mbinary2
280 > diff --git a/mbinary2 b/mbinary2
281 > new file mode 100644
281 > new file mode 100644
282 > index 0000000000000000000000000000000000000000..112363ac1917b417ffbd7f376ca786a1e5fa7490
282 > index 0000000000000000000000000000000000000000..112363ac1917b417ffbd7f376ca786a1e5fa7490
283 > GIT binary patch
283 > GIT binary patch
284 > literal 5
284 > literal 5
285 > Mc\${NkU|\`?^000jF3jhEB
285 > Mc\${NkU|\`?^000jF3jhEB
286 >
286 >
287 > EOF
287 > EOF
288 applying patch from stdin
288 applying patch from stdin
289
289
290 $ hg tip -q
290 $ hg tip -q
291 12:30b530085242
291 12:30b530085242
292
292
293 $ hg manifest --debug | grep mbinary
293 $ hg manifest --debug | grep mbinary
294 045c85ba38952325e126c70962cc0f9d9077bc67 644 mbinary1
294 045c85ba38952325e126c70962cc0f9d9077bc67 644 mbinary1
295 a874b471193996e7cb034bb301cac7bdaf3e3f46 644 mbinary2
295 a874b471193996e7cb034bb301cac7bdaf3e3f46 644 mbinary2
296
296
297 Filenames with spaces:
297 Filenames with spaces:
298
298
299 $ hg import -d "1000000 0" -m spaces - <<EOF
299 $ hg import -d "1000000 0" -m spaces - <<EOF
300 > diff --git a/foo bar b/foo bar
300 > diff --git a/foo bar b/foo bar
301 > new file mode 100644
301 > new file mode 100644
302 > index 0000000..257cc56
302 > index 0000000..257cc56
303 > --- /dev/null
303 > --- /dev/null
304 > +++ b/foo bar
304 > +++ b/foo bar
305 > @@ -0,0 +1 @@
305 > @@ -0,0 +1 @@
306 > +foo
306 > +foo
307 > EOF
307 > EOF
308 applying patch from stdin
308 applying patch from stdin
309
309
310 $ hg tip -q
310 $ hg tip -q
311 13:04750ef42fb3
311 13:04750ef42fb3
312
312
313 $ cat "foo bar"
313 $ cat "foo bar"
314 foo
314 foo
315
315
316 Copy then modify the original file:
316 Copy then modify the original file:
317
317
318 $ hg import -d "1000000 0" -m copy-mod-orig - <<EOF
318 $ hg import -d "1000000 0" -m copy-mod-orig - <<EOF
319 > diff --git a/foo2 b/foo2
319 > diff --git a/foo2 b/foo2
320 > index 257cc56..fe08ec6 100644
320 > index 257cc56..fe08ec6 100644
321 > --- a/foo2
321 > --- a/foo2
322 > +++ b/foo2
322 > +++ b/foo2
323 > @@ -1 +1,2 @@
323 > @@ -1 +1,2 @@
324 > foo
324 > foo
325 > +new line
325 > +new line
326 > diff --git a/foo2 b/foo3
326 > diff --git a/foo2 b/foo3
327 > similarity index 100%
327 > similarity index 100%
328 > copy from foo2
328 > copy from foo2
329 > copy to foo3
329 > copy to foo3
330 > EOF
330 > EOF
331 applying patch from stdin
331 applying patch from stdin
332
332
333 $ hg tip -q
333 $ hg tip -q
334 14:c4cd9cdeaa74
334 14:c4cd9cdeaa74
335
335
336 $ cat foo3
336 $ cat foo3
337 foo
337 foo
338
338
339 Move text file and patch as binary
339 Move text file and patch as binary
340
340
341 $ echo a > text2
341 $ echo a > text2
342 $ hg ci -Am0
342 $ hg ci -Am0
343 adding text2
343 adding text2
344 $ hg import -d "1000000 0" -m rename-as-binary - <<"EOF"
344 $ hg import -d "1000000 0" -m rename-as-binary - <<"EOF"
345 > diff --git a/text2 b/binary2
345 > diff --git a/text2 b/binary2
346 > rename from text2
346 > rename from text2
347 > rename to binary2
347 > rename to binary2
348 > index 78981922613b2afb6025042ff6bd878ac1994e85..10efcb362e9f3b3420fcfbfc0e37f3dc16e29757
348 > index 78981922613b2afb6025042ff6bd878ac1994e85..10efcb362e9f3b3420fcfbfc0e37f3dc16e29757
349 > GIT binary patch
349 > GIT binary patch
350 > literal 5
350 > literal 5
351 > Mc$`b*O5$Pw00T?_*Z=?k
351 > Mc$`b*O5$Pw00T?_*Z=?k
352 >
352 >
353 > EOF
353 > EOF
354 applying patch from stdin
354 applying patch from stdin
355
355
356 $ cat binary2
356 $ cat binary2
357 a
357 a
358 b
358 b
359 \x00 (no-eol) (esc)
359 \x00 (no-eol) (esc)
360
360
361 $ hg st --copies --change .
361 $ hg st --copies --change .
362 A binary2
362 A binary2
363 text2
363 text2
364 R text2
364 R text2
365
366 Invalid base85 content
367 $ hg rollback
368 repository tip rolled back to revision 15 (undo import)
369 working directory now based on revision 15
370 $ hg revert -aq
371 $ hg import -d "1000000 0" -m invalid-binary - <<"EOF"
372 > diff --git a/text2 b/binary2
373 > rename from text2
374 > rename to binary2
375 > index 78981922613b2afb6025042ff6bd878ac1994e85..10efcb362e9f3b3420fcfbfc0e37f3dc16e29757
376 > GIT binary patch
377 > literal 5
378 > Mc$`b*O.$Pw00T?_*Z=?k
379 >
380 > EOF
381 applying patch from stdin
382 abort: could not decode binary patch: bad base85 character at position 6
383 [255]
365 $ cd ..
384 $ cd ..
366
385
367 Consecutive import with renames (issue2459)
386 Consecutive import with renames (issue2459)
368
387
369 $ hg init issue2459
388 $ hg init issue2459
370 $ cd issue2459
389 $ cd issue2459
371 $ hg import --no-commit --force - <<EOF
390 $ hg import --no-commit --force - <<EOF
372 > diff --git a/a b/a
391 > diff --git a/a b/a
373 > new file mode 100644
392 > new file mode 100644
374 > EOF
393 > EOF
375 applying patch from stdin
394 applying patch from stdin
376 $ hg import --no-commit --force - <<EOF
395 $ hg import --no-commit --force - <<EOF
377 > diff --git a/a b/b
396 > diff --git a/a b/b
378 > rename from a
397 > rename from a
379 > rename to b
398 > rename to b
380 > EOF
399 > EOF
381 applying patch from stdin
400 applying patch from stdin
382 a has not been committed yet, so no copy data will be stored for b.
401 a has not been committed yet, so no copy data will be stored for b.
383 $ hg debugstate
402 $ hg debugstate
384 a 0 -1 unset b
403 a 0 -1 unset b
385 $ hg ci -m done
404 $ hg ci -m done
386 $ cd ..
405 $ cd ..
387
406
388 Renames and strip
407 Renames and strip
389
408
390 $ hg init renameandstrip
409 $ hg init renameandstrip
391 $ cd renameandstrip
410 $ cd renameandstrip
392 $ echo a > a
411 $ echo a > a
393 $ hg ci -Am adda
412 $ hg ci -Am adda
394 adding a
413 adding a
395 $ hg import --no-commit -p2 - <<EOF
414 $ hg import --no-commit -p2 - <<EOF
396 > diff --git a/foo/a b/foo/b
415 > diff --git a/foo/a b/foo/b
397 > rename from foo/a
416 > rename from foo/a
398 > rename to foo/b
417 > rename to foo/b
399 > EOF
418 > EOF
400 applying patch from stdin
419 applying patch from stdin
401 $ hg st --copies
420 $ hg st --copies
402 A b
421 A b
403 a
422 a
404 R a
423 R a
405
424
406 Renames, similarity and git diff
425 Renames, similarity and git diff
407
426
408 $ hg revert -aC
427 $ hg revert -aC
409 undeleting a
428 undeleting a
410 forgetting b
429 forgetting b
411 $ rm b
430 $ rm b
412 $ hg import --similarity 90 --no-commit - <<EOF
431 $ hg import --similarity 90 --no-commit - <<EOF
413 > diff --git a/a b/b
432 > diff --git a/a b/b
414 > rename from a
433 > rename from a
415 > rename to b
434 > rename to b
416 > EOF
435 > EOF
417 applying patch from stdin
436 applying patch from stdin
418 $ hg st --copies
437 $ hg st --copies
419 A b
438 A b
420 a
439 a
421 R a
440 R a
422 $ cd ..
441 $ cd ..
423
442
424 Pure copy with existing destination
443 Pure copy with existing destination
425
444
426 $ hg init copytoexisting
445 $ hg init copytoexisting
427 $ cd copytoexisting
446 $ cd copytoexisting
428 $ echo a > a
447 $ echo a > a
429 $ echo b > b
448 $ echo b > b
430 $ hg ci -Am add
449 $ hg ci -Am add
431 adding a
450 adding a
432 adding b
451 adding b
433 $ hg import --no-commit - <<EOF
452 $ hg import --no-commit - <<EOF
434 > diff --git a/a b/b
453 > diff --git a/a b/b
435 > copy from a
454 > copy from a
436 > copy to b
455 > copy to b
437 > EOF
456 > EOF
438 applying patch from stdin
457 applying patch from stdin
439 abort: cannot create b: destination already exists
458 abort: cannot create b: destination already exists
440 [255]
459 [255]
441 $ cat b
460 $ cat b
442 b
461 b
443
462
444 Copy and changes with existing destination
463 Copy and changes with existing destination
445
464
446 $ hg import --no-commit - <<EOF
465 $ hg import --no-commit - <<EOF
447 > diff --git a/a b/b
466 > diff --git a/a b/b
448 > copy from a
467 > copy from a
449 > copy to b
468 > copy to b
450 > --- a/a
469 > --- a/a
451 > +++ b/b
470 > +++ b/b
452 > @@ -1,1 +1,2 @@
471 > @@ -1,1 +1,2 @@
453 > a
472 > a
454 > +b
473 > +b
455 > EOF
474 > EOF
456 applying patch from stdin
475 applying patch from stdin
457 cannot create b: destination already exists
476 cannot create b: destination already exists
458 1 out of 1 hunks FAILED -- saving rejects to file b.rej
477 1 out of 1 hunks FAILED -- saving rejects to file b.rej
459 abort: patch failed to apply
478 abort: patch failed to apply
460 [255]
479 [255]
461 $ cat b
480 $ cat b
462 b
481 b
463
482
464 $ ln -s b linkb
483 $ ln -s b linkb
465 $ hg add linkb
484 $ hg add linkb
466 $ hg ci -m addlinkb
485 $ hg ci -m addlinkb
467 $ hg import --no-commit - <<EOF
486 $ hg import --no-commit - <<EOF
468 > diff --git a/linkb b/linkb
487 > diff --git a/linkb b/linkb
469 > deleted file mode 120000
488 > deleted file mode 120000
470 > --- a/linkb
489 > --- a/linkb
471 > +++ /dev/null
490 > +++ /dev/null
472 > @@ -1,1 +0,0 @@
491 > @@ -1,1 +0,0 @@
473 > -badhunk
492 > -badhunk
474 > \ No newline at end of file
493 > \ No newline at end of file
475 > EOF
494 > EOF
476 applying patch from stdin
495 applying patch from stdin
477 patching file linkb
496 patching file linkb
478 Hunk #1 FAILED at 0
497 Hunk #1 FAILED at 0
479 1 out of 1 hunks FAILED -- saving rejects to file linkb.rej
498 1 out of 1 hunks FAILED -- saving rejects to file linkb.rej
480 abort: patch failed to apply
499 abort: patch failed to apply
481 [255]
500 [255]
482 $ hg st
501 $ hg st
483 ? b.rej
502 ? b.rej
484 ? linkb.rej
503 ? linkb.rej
485
504
486 Test corner case involving copies and multiple hunks (issue3384)
505 Test corner case involving copies and multiple hunks (issue3384)
487
506
488 $ hg revert -qa
507 $ hg revert -qa
489 $ hg import --no-commit - <<EOF
508 $ hg import --no-commit - <<EOF
490 > diff --git a/a b/c
509 > diff --git a/a b/c
491 > copy from a
510 > copy from a
492 > copy to c
511 > copy to c
493 > --- a/a
512 > --- a/a
494 > +++ b/c
513 > +++ b/c
495 > @@ -1,1 +1,2 @@
514 > @@ -1,1 +1,2 @@
496 > a
515 > a
497 > +a
516 > +a
498 > @@ -2,1 +2,2 @@
517 > @@ -2,1 +2,2 @@
499 > a
518 > a
500 > +a
519 > +a
501 > diff --git a/a b/a
520 > diff --git a/a b/a
502 > --- a/a
521 > --- a/a
503 > +++ b/a
522 > +++ b/a
504 > @@ -1,1 +1,2 @@
523 > @@ -1,1 +1,2 @@
505 > a
524 > a
506 > +b
525 > +b
507 > EOF
526 > EOF
508 applying patch from stdin
527 applying patch from stdin
509
528
510 $ cd ..
529 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now