~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

Initial commit for russian version of documents.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
import re
17
18
 
18
19
 
19
20
class PatchSyntax(Exception):
92
93
    range = int(range)
93
94
    return (pos, range)
94
95
 
95
 
 
 
96
 
96
97
def hunk_from_header(line):
97
 
    import re
98
98
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
99
99
    if matches is None:
100
100
        raise MalformedHunkHeader("Does not match format.", line)
164
164
        return InsertLine(line[1:])
165
165
    elif line.startswith("-"):
166
166
        return RemoveLine(line[1:])
 
167
    elif line == NO_NL:
 
168
        return NO_NL
167
169
    else:
168
170
        raise MalformedLine("Unknown line type", line)
169
171
__pychecker__=""
266
268
        self.hunks = []
267
269
 
268
270
    def __str__(self):
269
 
        ret = self.get_header()
 
271
        ret = self.get_header() 
270
272
        ret += "".join([str(h) for h in self.hunks])
271
273
        return ret
272
274
 
273
275
    def get_header(self):
274
276
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
275
277
 
276
 
    def stats_values(self):
277
 
        """Calculate the number of inserts and removes."""
 
278
    def stats_str(self):
 
279
        """Return a string of patch statistics"""
278
280
        removes = 0
279
281
        inserts = 0
280
282
        for hunk in self.hunks:
283
285
                     inserts+=1;
284
286
                elif isinstance(line, RemoveLine):
285
287
                     removes+=1;
286
 
        return (inserts, removes, len(self.hunks))
287
 
 
288
 
    def stats_str(self):
289
 
        """Return a string of patch statistics"""
290
288
        return "%i inserts, %i removes in %i hunks" % \
291
 
            self.stats_values()
 
289
            (inserts, removes, len(self.hunks))
292
290
 
293
291
    def pos_in_mod(self, position):
294
292
        newpos = position
298
296
                return None
299
297
            newpos += shift
300
298
        return newpos
301
 
 
 
299
            
302
300
    def iter_inserted(self):
303
301
        """Iteraties through inserted lines
304
 
 
 
302
        
305
303
        :return: Pair of line number, line
306
304
        :rtype: iterator of (int, InsertLine)
307
305
        """
316
314
 
317
315
 
318
316
def parse_patch(iter_lines):
319
 
    iter_lines = iter_lines_handle_nl(iter_lines)
320
317
    (orig_name, mod_name) = get_patch_names(iter_lines)
321
318
    patch = Patch(orig_name, mod_name)
322
319
    for hunk in iter_hunks(iter_lines):
369
366
 
370
367
 
371
368
def parse_patches(iter_lines):
 
369
    iter_lines = iter_lines_handle_nl(iter_lines)
372
370
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
373
371
 
374
372
 
395
393
    """Iterate through a series of lines with a patch applied.
396
394
    This handles a single file, and does exact, not fuzzy patching.
397
395
    """
398
 
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
396
    if orig_lines is not None:
 
397
        orig_lines = orig_lines.__iter__()
 
398
    seen_patch = []
 
399
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
399
400
    get_patch_names(patch_lines)
400
 
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
401
 
 
402
 
 
403
 
def iter_patched_from_hunks(orig_lines, hunks):
404
 
    """Iterate through a series of lines with a patch applied.
405
 
    This handles a single file, and does exact, not fuzzy patching.
406
 
 
407
 
    :param orig_lines: The unpatched lines.
408
 
    :param hunks: An iterable of Hunk instances.
409
 
    """
410
 
    seen_patch = []
411
401
    line_no = 1
412
 
    if orig_lines is not None:
413
 
        orig_lines = iter(orig_lines)
414
 
    for hunk in hunks:
 
402
    for hunk in iter_hunks(patch_lines):
415
403
        while line_no < hunk.orig_pos:
416
404
            orig_line = orig_lines.next()
417
405
            yield orig_line