##// END OF EJS Templates
[extendedchangelog] encode/decode function...
Benoit Boissinot -
r3232:394ac87f default
parent child Browse files
Show More
@@ -0,0 +1,10 b''
1 #!/usr/bin/env python
2 #
3
4 import doctest
5
6 import mercurial.changelog
7 # test doctest from changelog
8
9 doctest.testmod(mercurial.changelog)
10
@@ -1,56 +1,73 b''
1 # changelog.py - changelog class for mercurial
1 # changelog.py - changelog class for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 from revlog import *
8 from revlog import *
9 from i18n import gettext as _
9 from i18n import gettext as _
10 from demandload import demandload
10 from demandload import demandload
11 demandload(globals(), "os time util")
11 demandload(globals(), "os time util")
12
12
13 def _string_escape(text):
14 """
15 >>> d = {'nl': chr(10), 'bs': chr(92), 'cr': chr(13), 'nul': chr(0)}
16 >>> s = "ab%(nl)scd%(bs)s%(bs)sn%(nul)sab%(cr)scd%(bs)s%(nl)s" % d
17 >>> s
18 'ab\\ncd\\\\\\\\n\\x00ab\\rcd\\\\\\n'
19 >>> res = _string_escape(s)
20 >>> s == _string_unescape(res)
21 True
22 """
23 # subset of the string_escape codec
24 text = text.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '\\r')
25 return text.replace('\0', '\\0')
26
27 def _string_unescape(text):
28 return text.decode('string_escape')
29
13 class changelog(revlog):
30 class changelog(revlog):
14 def __init__(self, opener, defversion=REVLOGV0):
31 def __init__(self, opener, defversion=REVLOGV0):
15 revlog.__init__(self, opener, "00changelog.i", "00changelog.d",
32 revlog.__init__(self, opener, "00changelog.i", "00changelog.d",
16 defversion)
33 defversion)
17
34
18 def extract(self, text):
35 def extract(self, text):
19 """
36 """
20 format used:
37 format used:
21 nodeid\n : manifest node in ascii
38 nodeid\n : manifest node in ascii
22 user\n : user, no \n or \r allowed
39 user\n : user, no \n or \r allowed
23 time tz\n : date (time is int or float, timezone is int)
40 time tz\n : date (time is int or float, timezone is int)
24 files\n\n : files modified by the cset, no \n or \r allowed
41 files\n\n : files modified by the cset, no \n or \r allowed
25 (.*) : comment (free text, ideally utf-8)
42 (.*) : comment (free text, ideally utf-8)
26 """
43 """
27 if not text:
44 if not text:
28 return (nullid, "", (0, 0), [], "")
45 return (nullid, "", (0, 0), [], "")
29 last = text.index("\n\n")
46 last = text.index("\n\n")
30 desc = text[last + 2:]
47 desc = text[last + 2:]
31 l = text[:last].splitlines()
48 l = text[:last].splitlines()
32 manifest = bin(l[0])
49 manifest = bin(l[0])
33 user = l[1]
50 user = l[1]
34 date = l[2].split(' ')
51 date = l[2].split(' ')
35 time = float(date.pop(0))
52 time = float(date.pop(0))
36 try:
53 try:
37 # various tools did silly things with the time zone field.
54 # various tools did silly things with the time zone field.
38 timezone = int(date[0])
55 timezone = int(date[0])
39 except:
56 except:
40 timezone = 0
57 timezone = 0
41 files = l[3:]
58 files = l[3:]
42 return (manifest, user, (time, timezone), files, desc)
59 return (manifest, user, (time, timezone), files, desc)
43
60
44 def read(self, node):
61 def read(self, node):
45 return self.extract(self.revision(node))
62 return self.extract(self.revision(node))
46
63
47 def add(self, manifest, list, desc, transaction, p1=None, p2=None,
64 def add(self, manifest, list, desc, transaction, p1=None, p2=None,
48 user=None, date=None):
65 user=None, date=None):
49 if date:
66 if date:
50 parseddate = "%d %d" % util.parsedate(date)
67 parseddate = "%d %d" % util.parsedate(date)
51 else:
68 else:
52 parseddate = "%d %d" % util.makedate()
69 parseddate = "%d %d" % util.makedate()
53 list.sort()
70 list.sort()
54 l = [hex(manifest), user, parseddate] + list + ["", desc]
71 l = [hex(manifest), user, parseddate] + list + ["", desc]
55 text = "\n".join(l)
72 text = "\n".join(l)
56 return self.addrevision(text, transaction, self.count(), p1, p2)
73 return self.addrevision(text, transaction, self.count(), p1, p2)
General Comments 0
You need to be logged in to leave comments. Login now