##// END OF EJS Templates
revlog: remove lazy index
Matt Mackall -
r13253:61c9bc3d default
parent child Browse files
Show More
@@ -1,165 +1,164 b''
1 # perf.py - performance test routines
1 # perf.py - performance test routines
2 '''helper extension to measure performance'''
2 '''helper extension to measure performance'''
3
3
4 from mercurial import cmdutil, match, commands
4 from mercurial import cmdutil, match, commands
5 import time, os, sys
5 import time, os, sys
6
6
7 def timer(func, title=None):
7 def timer(func, title=None):
8 results = []
8 results = []
9 begin = time.time()
9 begin = time.time()
10 count = 0
10 count = 0
11 while 1:
11 while 1:
12 ostart = os.times()
12 ostart = os.times()
13 cstart = time.time()
13 cstart = time.time()
14 r = func()
14 r = func()
15 cstop = time.time()
15 cstop = time.time()
16 ostop = os.times()
16 ostop = os.times()
17 count += 1
17 count += 1
18 a, b = ostart, ostop
18 a, b = ostart, ostop
19 results.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
19 results.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
20 if cstop - begin > 3 and count >= 100:
20 if cstop - begin > 3 and count >= 100:
21 break
21 break
22 if cstop - begin > 10 and count >= 3:
22 if cstop - begin > 10 and count >= 3:
23 break
23 break
24 if title:
24 if title:
25 sys.stderr.write("! %s\n" % title)
25 sys.stderr.write("! %s\n" % title)
26 if r:
26 if r:
27 sys.stderr.write("! result: %s\n" % r)
27 sys.stderr.write("! result: %s\n" % r)
28 m = min(results)
28 m = min(results)
29 sys.stderr.write("! wall %f comb %f user %f sys %f (best of %d)\n"
29 sys.stderr.write("! wall %f comb %f user %f sys %f (best of %d)\n"
30 % (m[0], m[1] + m[2], m[1], m[2], count))
30 % (m[0], m[1] + m[2], m[1], m[2], count))
31
31
32 def perfwalk(ui, repo, *pats):
32 def perfwalk(ui, repo, *pats):
33 try:
33 try:
34 m = cmdutil.match(repo, pats, {})
34 m = cmdutil.match(repo, pats, {})
35 timer(lambda: len(list(repo.dirstate.walk(m, [], True, False))))
35 timer(lambda: len(list(repo.dirstate.walk(m, [], True, False))))
36 except:
36 except:
37 try:
37 try:
38 m = cmdutil.match(repo, pats, {})
38 m = cmdutil.match(repo, pats, {})
39 timer(lambda: len([b for a, b, c in repo.dirstate.statwalk([], m)]))
39 timer(lambda: len([b for a, b, c in repo.dirstate.statwalk([], m)]))
40 except:
40 except:
41 timer(lambda: len(list(cmdutil.walk(repo, pats, {}))))
41 timer(lambda: len(list(cmdutil.walk(repo, pats, {}))))
42
42
43 def perfstatus(ui, repo, *pats):
43 def perfstatus(ui, repo, *pats):
44 #m = match.always(repo.root, repo.getcwd())
44 #m = match.always(repo.root, repo.getcwd())
45 #timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False, False))))
45 #timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False, False))))
46 timer(lambda: sum(map(len, repo.status())))
46 timer(lambda: sum(map(len, repo.status())))
47
47
48 def perfheads(ui, repo):
48 def perfheads(ui, repo):
49 timer(lambda: len(repo.changelog.heads()))
49 timer(lambda: len(repo.changelog.heads()))
50
50
51 def perftags(ui, repo):
51 def perftags(ui, repo):
52 import mercurial.changelog, mercurial.manifest
52 import mercurial.changelog, mercurial.manifest
53 def t():
53 def t():
54 repo.changelog = mercurial.changelog.changelog(repo.sopener)
54 repo.changelog = mercurial.changelog.changelog(repo.sopener)
55 repo.manifest = mercurial.manifest.manifest(repo.sopener)
55 repo.manifest = mercurial.manifest.manifest(repo.sopener)
56 repo._tags = None
56 repo._tags = None
57 return len(repo.tags())
57 return len(repo.tags())
58 timer(t)
58 timer(t)
59
59
60 def perfdirstate(ui, repo):
60 def perfdirstate(ui, repo):
61 "a" in repo.dirstate
61 "a" in repo.dirstate
62 def d():
62 def d():
63 repo.dirstate.invalidate()
63 repo.dirstate.invalidate()
64 "a" in repo.dirstate
64 "a" in repo.dirstate
65 timer(d)
65 timer(d)
66
66
67 def perfdirstatedirs(ui, repo):
67 def perfdirstatedirs(ui, repo):
68 "a" in repo.dirstate
68 "a" in repo.dirstate
69 def d():
69 def d():
70 "a" in repo.dirstate._dirs
70 "a" in repo.dirstate._dirs
71 del repo.dirstate._dirs
71 del repo.dirstate._dirs
72 timer(d)
72 timer(d)
73
73
74 def perfmanifest(ui, repo):
74 def perfmanifest(ui, repo):
75 def d():
75 def d():
76 t = repo.manifest.tip()
76 t = repo.manifest.tip()
77 m = repo.manifest.read(t)
77 m = repo.manifest.read(t)
78 repo.manifest.mapcache = None
78 repo.manifest.mapcache = None
79 repo.manifest._cache = None
79 repo.manifest._cache = None
80 timer(d)
80 timer(d)
81
81
82 def perfindex(ui, repo):
82 def perfindex(ui, repo):
83 import mercurial.changelog
83 import mercurial.changelog
84 def d():
84 def d():
85 t = repo.changelog.tip()
85 t = repo.changelog.tip()
86 repo.changelog = mercurial.changelog.changelog(repo.sopener)
86 repo.invalidate()
87 repo.changelog._loadindexmap()
88 timer(d)
87 timer(d)
89
88
90 def perfstartup(ui, repo):
89 def perfstartup(ui, repo):
91 cmd = sys.argv[0]
90 cmd = sys.argv[0]
92 def d():
91 def d():
93 os.system("HGRCPATH= %s version -q > /dev/null" % cmd)
92 os.system("HGRCPATH= %s version -q > /dev/null" % cmd)
94 timer(d)
93 timer(d)
95
94
96 def perfparents(ui, repo):
95 def perfparents(ui, repo):
97 nl = [repo.changelog.node(i) for i in xrange(1000)]
96 nl = [repo.changelog.node(i) for i in xrange(1000)]
98 def d():
97 def d():
99 for n in nl:
98 for n in nl:
100 repo.changelog.parents(n)
99 repo.changelog.parents(n)
101 timer(d)
100 timer(d)
102
101
103 def perflookup(ui, repo, rev):
102 def perflookup(ui, repo, rev):
104 timer(lambda: len(repo.lookup(rev)))
103 timer(lambda: len(repo.lookup(rev)))
105
104
106 def perflog(ui, repo, **opts):
105 def perflog(ui, repo, **opts):
107 ui.pushbuffer()
106 ui.pushbuffer()
108 timer(lambda: commands.log(ui, repo, rev=[], date='', user='',
107 timer(lambda: commands.log(ui, repo, rev=[], date='', user='',
109 copies=opts.get('rename')))
108 copies=opts.get('rename')))
110 ui.popbuffer()
109 ui.popbuffer()
111
110
112 def perftemplating(ui, repo):
111 def perftemplating(ui, repo):
113 ui.pushbuffer()
112 ui.pushbuffer()
114 timer(lambda: commands.log(ui, repo, rev=[], date='', user='',
113 timer(lambda: commands.log(ui, repo, rev=[], date='', user='',
115 template='{date|shortdate} [{rev}:{node|short}]'
114 template='{date|shortdate} [{rev}:{node|short}]'
116 ' {author|person}: {desc|firstline}\n'))
115 ' {author|person}: {desc|firstline}\n'))
117 ui.popbuffer()
116 ui.popbuffer()
118
117
119 def perfdiffwd(ui, repo):
118 def perfdiffwd(ui, repo):
120 """Profile diff of working directory changes"""
119 """Profile diff of working directory changes"""
121 options = {
120 options = {
122 'w': 'ignore_all_space',
121 'w': 'ignore_all_space',
123 'b': 'ignore_space_change',
122 'b': 'ignore_space_change',
124 'B': 'ignore_blank_lines',
123 'B': 'ignore_blank_lines',
125 }
124 }
126
125
127 for diffopt in ('', 'w', 'b', 'B', 'wB'):
126 for diffopt in ('', 'w', 'b', 'B', 'wB'):
128 opts = dict((options[c], '1') for c in diffopt)
127 opts = dict((options[c], '1') for c in diffopt)
129 def d():
128 def d():
130 ui.pushbuffer()
129 ui.pushbuffer()
131 commands.diff(ui, repo, **opts)
130 commands.diff(ui, repo, **opts)
132 ui.popbuffer()
131 ui.popbuffer()
133 title = 'diffopts: %s' % (diffopt and ('-' + diffopt) or 'none')
132 title = 'diffopts: %s' % (diffopt and ('-' + diffopt) or 'none')
134 timer(d, title)
133 timer(d, title)
135
134
136 def perfrevlog(ui, repo, file_, **opts):
135 def perfrevlog(ui, repo, file_, **opts):
137 from mercurial import revlog
136 from mercurial import revlog
138 dist = opts['dist']
137 dist = opts['dist']
139 def d():
138 def d():
140 r = revlog.revlog(lambda fn: open(fn, 'rb'), file_)
139 r = revlog.revlog(lambda fn: open(fn, 'rb'), file_)
141 for x in xrange(0, len(r), dist):
140 for x in xrange(0, len(r), dist):
142 r.revision(r.node(x))
141 r.revision(r.node(x))
143
142
144 timer(d)
143 timer(d)
145
144
146 cmdtable = {
145 cmdtable = {
147 'perflookup': (perflookup, []),
146 'perflookup': (perflookup, []),
148 'perfparents': (perfparents, []),
147 'perfparents': (perfparents, []),
149 'perfstartup': (perfstartup, []),
148 'perfstartup': (perfstartup, []),
150 'perfstatus': (perfstatus, []),
149 'perfstatus': (perfstatus, []),
151 'perfwalk': (perfwalk, []),
150 'perfwalk': (perfwalk, []),
152 'perfmanifest': (perfmanifest, []),
151 'perfmanifest': (perfmanifest, []),
153 'perfindex': (perfindex, []),
152 'perfindex': (perfindex, []),
154 'perfheads': (perfheads, []),
153 'perfheads': (perfheads, []),
155 'perftags': (perftags, []),
154 'perftags': (perftags, []),
156 'perfdirstate': (perfdirstate, []),
155 'perfdirstate': (perfdirstate, []),
157 'perfdirstatedirs': (perfdirstate, []),
156 'perfdirstatedirs': (perfdirstate, []),
158 'perflog': (perflog,
157 'perflog': (perflog,
159 [('', 'rename', False, 'ask log to follow renames')]),
158 [('', 'rename', False, 'ask log to follow renames')]),
160 'perftemplating': (perftemplating, []),
159 'perftemplating': (perftemplating, []),
161 'perfdiffwd': (perfdiffwd, []),
160 'perfdiffwd': (perfdiffwd, []),
162 'perfrevlog': (perfrevlog,
161 'perfrevlog': (perfrevlog,
163 [('d', 'dist', 100, 'distance between the revisions')],
162 [('d', 'dist', 100, 'distance between the revisions')],
164 "[INDEXFILE]"),
163 "[INDEXFILE]"),
165 }
164 }
@@ -1,1938 +1,1935 b''
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from node import bin, hex, nullid, nullrev, short
8 from node import bin, hex, nullid, nullrev, short
9 from i18n import _
9 from i18n import _
10 import repo, changegroup, subrepo, discovery, pushkey
10 import repo, changegroup, subrepo, discovery, pushkey
11 import changelog, dirstate, filelog, manifest, context
11 import changelog, dirstate, filelog, manifest, context
12 import lock, transaction, store, encoding
12 import lock, transaction, store, encoding
13 import util, extensions, hook, error
13 import util, extensions, hook, error
14 import match as matchmod
14 import match as matchmod
15 import merge as mergemod
15 import merge as mergemod
16 import tags as tagsmod
16 import tags as tagsmod
17 import url as urlmod
17 import url as urlmod
18 from lock import release
18 from lock import release
19 import weakref, errno, os, time, inspect
19 import weakref, errno, os, time, inspect
20 propertycache = util.propertycache
20 propertycache = util.propertycache
21
21
22 class localrepository(repo.repository):
22 class localrepository(repo.repository):
23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
24 supportedformats = set(('revlogv1', 'parentdelta'))
24 supportedformats = set(('revlogv1', 'parentdelta'))
25 supported = supportedformats | set(('store', 'fncache', 'shared',
25 supported = supportedformats | set(('store', 'fncache', 'shared',
26 'dotencode'))
26 'dotencode'))
27
27
28 def __init__(self, baseui, path=None, create=0):
28 def __init__(self, baseui, path=None, create=0):
29 repo.repository.__init__(self)
29 repo.repository.__init__(self)
30 self.root = os.path.realpath(util.expandpath(path))
30 self.root = os.path.realpath(util.expandpath(path))
31 self.path = os.path.join(self.root, ".hg")
31 self.path = os.path.join(self.root, ".hg")
32 self.origroot = path
32 self.origroot = path
33 self.auditor = util.path_auditor(self.root, self._checknested)
33 self.auditor = util.path_auditor(self.root, self._checknested)
34 self.opener = util.opener(self.path)
34 self.opener = util.opener(self.path)
35 self.wopener = util.opener(self.root)
35 self.wopener = util.opener(self.root)
36 self.baseui = baseui
36 self.baseui = baseui
37 self.ui = baseui.copy()
37 self.ui = baseui.copy()
38
38
39 try:
39 try:
40 self.ui.readconfig(self.join("hgrc"), self.root)
40 self.ui.readconfig(self.join("hgrc"), self.root)
41 extensions.loadall(self.ui)
41 extensions.loadall(self.ui)
42 except IOError:
42 except IOError:
43 pass
43 pass
44
44
45 if not os.path.isdir(self.path):
45 if not os.path.isdir(self.path):
46 if create:
46 if create:
47 if not os.path.exists(path):
47 if not os.path.exists(path):
48 util.makedirs(path)
48 util.makedirs(path)
49 os.mkdir(self.path)
49 os.mkdir(self.path)
50 requirements = ["revlogv1"]
50 requirements = ["revlogv1"]
51 if self.ui.configbool('format', 'usestore', True):
51 if self.ui.configbool('format', 'usestore', True):
52 os.mkdir(os.path.join(self.path, "store"))
52 os.mkdir(os.path.join(self.path, "store"))
53 requirements.append("store")
53 requirements.append("store")
54 if self.ui.configbool('format', 'usefncache', True):
54 if self.ui.configbool('format', 'usefncache', True):
55 requirements.append("fncache")
55 requirements.append("fncache")
56 if self.ui.configbool('format', 'dotencode', True):
56 if self.ui.configbool('format', 'dotencode', True):
57 requirements.append('dotencode')
57 requirements.append('dotencode')
58 # create an invalid changelog
58 # create an invalid changelog
59 self.opener("00changelog.i", "a").write(
59 self.opener("00changelog.i", "a").write(
60 '\0\0\0\2' # represents revlogv2
60 '\0\0\0\2' # represents revlogv2
61 ' dummy changelog to prevent using the old repo layout'
61 ' dummy changelog to prevent using the old repo layout'
62 )
62 )
63 if self.ui.configbool('format', 'parentdelta', False):
63 if self.ui.configbool('format', 'parentdelta', False):
64 requirements.append("parentdelta")
64 requirements.append("parentdelta")
65 else:
65 else:
66 raise error.RepoError(_("repository %s not found") % path)
66 raise error.RepoError(_("repository %s not found") % path)
67 elif create:
67 elif create:
68 raise error.RepoError(_("repository %s already exists") % path)
68 raise error.RepoError(_("repository %s already exists") % path)
69 else:
69 else:
70 # find requirements
70 # find requirements
71 requirements = set()
71 requirements = set()
72 try:
72 try:
73 requirements = set(self.opener("requires").read().splitlines())
73 requirements = set(self.opener("requires").read().splitlines())
74 except IOError, inst:
74 except IOError, inst:
75 if inst.errno != errno.ENOENT:
75 if inst.errno != errno.ENOENT:
76 raise
76 raise
77 for r in requirements - self.supported:
77 for r in requirements - self.supported:
78 raise error.RepoError(_("requirement '%s' not supported") % r)
78 raise error.RepoError(_("requirement '%s' not supported") % r)
79
79
80 self.sharedpath = self.path
80 self.sharedpath = self.path
81 try:
81 try:
82 s = os.path.realpath(self.opener("sharedpath").read())
82 s = os.path.realpath(self.opener("sharedpath").read())
83 if not os.path.exists(s):
83 if not os.path.exists(s):
84 raise error.RepoError(
84 raise error.RepoError(
85 _('.hg/sharedpath points to nonexistent directory %s') % s)
85 _('.hg/sharedpath points to nonexistent directory %s') % s)
86 self.sharedpath = s
86 self.sharedpath = s
87 except IOError, inst:
87 except IOError, inst:
88 if inst.errno != errno.ENOENT:
88 if inst.errno != errno.ENOENT:
89 raise
89 raise
90
90
91 self.store = store.store(requirements, self.sharedpath, util.opener)
91 self.store = store.store(requirements, self.sharedpath, util.opener)
92 self.spath = self.store.path
92 self.spath = self.store.path
93 self.sopener = self.store.opener
93 self.sopener = self.store.opener
94 self.sjoin = self.store.join
94 self.sjoin = self.store.join
95 self.opener.createmode = self.store.createmode
95 self.opener.createmode = self.store.createmode
96 self._applyrequirements(requirements)
96 self._applyrequirements(requirements)
97 if create:
97 if create:
98 self._writerequirements()
98 self._writerequirements()
99
99
100 # These two define the set of tags for this repository. _tags
100 # These two define the set of tags for this repository. _tags
101 # maps tag name to node; _tagtypes maps tag name to 'global' or
101 # maps tag name to node; _tagtypes maps tag name to 'global' or
102 # 'local'. (Global tags are defined by .hgtags across all
102 # 'local'. (Global tags are defined by .hgtags across all
103 # heads, and local tags are defined in .hg/localtags.) They
103 # heads, and local tags are defined in .hg/localtags.) They
104 # constitute the in-memory cache of tags.
104 # constitute the in-memory cache of tags.
105 self._tags = None
105 self._tags = None
106 self._tagtypes = None
106 self._tagtypes = None
107
107
108 self._branchcache = None
108 self._branchcache = None
109 self._branchcachetip = None
109 self._branchcachetip = None
110 self.nodetagscache = None
110 self.nodetagscache = None
111 self.filterpats = {}
111 self.filterpats = {}
112 self._datafilters = {}
112 self._datafilters = {}
113 self._transref = self._lockref = self._wlockref = None
113 self._transref = self._lockref = self._wlockref = None
114
114
115 def _applyrequirements(self, requirements):
115 def _applyrequirements(self, requirements):
116 self.requirements = requirements
116 self.requirements = requirements
117 self.sopener.options = {}
117 self.sopener.options = {}
118 if 'parentdelta' in requirements:
118 if 'parentdelta' in requirements:
119 self.sopener.options['parentdelta'] = 1
119 self.sopener.options['parentdelta'] = 1
120
120
121 def _writerequirements(self):
121 def _writerequirements(self):
122 reqfile = self.opener("requires", "w")
122 reqfile = self.opener("requires", "w")
123 for r in self.requirements:
123 for r in self.requirements:
124 reqfile.write("%s\n" % r)
124 reqfile.write("%s\n" % r)
125 reqfile.close()
125 reqfile.close()
126
126
127 def _checknested(self, path):
127 def _checknested(self, path):
128 """Determine if path is a legal nested repository."""
128 """Determine if path is a legal nested repository."""
129 if not path.startswith(self.root):
129 if not path.startswith(self.root):
130 return False
130 return False
131 subpath = path[len(self.root) + 1:]
131 subpath = path[len(self.root) + 1:]
132
132
133 # XXX: Checking against the current working copy is wrong in
133 # XXX: Checking against the current working copy is wrong in
134 # the sense that it can reject things like
134 # the sense that it can reject things like
135 #
135 #
136 # $ hg cat -r 10 sub/x.txt
136 # $ hg cat -r 10 sub/x.txt
137 #
137 #
138 # if sub/ is no longer a subrepository in the working copy
138 # if sub/ is no longer a subrepository in the working copy
139 # parent revision.
139 # parent revision.
140 #
140 #
141 # However, it can of course also allow things that would have
141 # However, it can of course also allow things that would have
142 # been rejected before, such as the above cat command if sub/
142 # been rejected before, such as the above cat command if sub/
143 # is a subrepository now, but was a normal directory before.
143 # is a subrepository now, but was a normal directory before.
144 # The old path auditor would have rejected by mistake since it
144 # The old path auditor would have rejected by mistake since it
145 # panics when it sees sub/.hg/.
145 # panics when it sees sub/.hg/.
146 #
146 #
147 # All in all, checking against the working copy seems sensible
147 # All in all, checking against the working copy seems sensible
148 # since we want to prevent access to nested repositories on
148 # since we want to prevent access to nested repositories on
149 # the filesystem *now*.
149 # the filesystem *now*.
150 ctx = self[None]
150 ctx = self[None]
151 parts = util.splitpath(subpath)
151 parts = util.splitpath(subpath)
152 while parts:
152 while parts:
153 prefix = os.sep.join(parts)
153 prefix = os.sep.join(parts)
154 if prefix in ctx.substate:
154 if prefix in ctx.substate:
155 if prefix == subpath:
155 if prefix == subpath:
156 return True
156 return True
157 else:
157 else:
158 sub = ctx.sub(prefix)
158 sub = ctx.sub(prefix)
159 return sub.checknested(subpath[len(prefix) + 1:])
159 return sub.checknested(subpath[len(prefix) + 1:])
160 else:
160 else:
161 parts.pop()
161 parts.pop()
162 return False
162 return False
163
163
164
164
165 @propertycache
165 @propertycache
166 def changelog(self):
166 def changelog(self):
167 c = changelog.changelog(self.sopener)
167 c = changelog.changelog(self.sopener)
168 if 'HG_PENDING' in os.environ:
168 if 'HG_PENDING' in os.environ:
169 p = os.environ['HG_PENDING']
169 p = os.environ['HG_PENDING']
170 if p.startswith(self.root):
170 if p.startswith(self.root):
171 c.readpending('00changelog.i.a')
171 c.readpending('00changelog.i.a')
172 self.sopener.options['defversion'] = c.version
172 self.sopener.options['defversion'] = c.version
173 return c
173 return c
174
174
175 @propertycache
175 @propertycache
176 def manifest(self):
176 def manifest(self):
177 return manifest.manifest(self.sopener)
177 return manifest.manifest(self.sopener)
178
178
179 @propertycache
179 @propertycache
180 def dirstate(self):
180 def dirstate(self):
181 warned = [0]
181 warned = [0]
182 def validate(node):
182 def validate(node):
183 try:
183 try:
184 r = self.changelog.rev(node)
184 r = self.changelog.rev(node)
185 return node
185 return node
186 except error.LookupError:
186 except error.LookupError:
187 if not warned[0]:
187 if not warned[0]:
188 warned[0] = True
188 warned[0] = True
189 self.ui.warn(_("warning: ignoring unknown"
189 self.ui.warn(_("warning: ignoring unknown"
190 " working parent %s!\n") % short(node))
190 " working parent %s!\n") % short(node))
191 return nullid
191 return nullid
192
192
193 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
193 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
194
194
195 def __getitem__(self, changeid):
195 def __getitem__(self, changeid):
196 if changeid is None:
196 if changeid is None:
197 return context.workingctx(self)
197 return context.workingctx(self)
198 return context.changectx(self, changeid)
198 return context.changectx(self, changeid)
199
199
200 def __contains__(self, changeid):
200 def __contains__(self, changeid):
201 try:
201 try:
202 return bool(self.lookup(changeid))
202 return bool(self.lookup(changeid))
203 except error.RepoLookupError:
203 except error.RepoLookupError:
204 return False
204 return False
205
205
206 def __nonzero__(self):
206 def __nonzero__(self):
207 return True
207 return True
208
208
209 def __len__(self):
209 def __len__(self):
210 return len(self.changelog)
210 return len(self.changelog)
211
211
212 def __iter__(self):
212 def __iter__(self):
213 for i in xrange(len(self)):
213 for i in xrange(len(self)):
214 yield i
214 yield i
215
215
216 def url(self):
216 def url(self):
217 return 'file:' + self.root
217 return 'file:' + self.root
218
218
219 def hook(self, name, throw=False, **args):
219 def hook(self, name, throw=False, **args):
220 return hook.hook(self.ui, self, name, throw, **args)
220 return hook.hook(self.ui, self, name, throw, **args)
221
221
222 tag_disallowed = ':\r\n'
222 tag_disallowed = ':\r\n'
223
223
224 def _tag(self, names, node, message, local, user, date, extra={}):
224 def _tag(self, names, node, message, local, user, date, extra={}):
225 if isinstance(names, str):
225 if isinstance(names, str):
226 allchars = names
226 allchars = names
227 names = (names,)
227 names = (names,)
228 else:
228 else:
229 allchars = ''.join(names)
229 allchars = ''.join(names)
230 for c in self.tag_disallowed:
230 for c in self.tag_disallowed:
231 if c in allchars:
231 if c in allchars:
232 raise util.Abort(_('%r cannot be used in a tag name') % c)
232 raise util.Abort(_('%r cannot be used in a tag name') % c)
233
233
234 branches = self.branchmap()
234 branches = self.branchmap()
235 for name in names:
235 for name in names:
236 self.hook('pretag', throw=True, node=hex(node), tag=name,
236 self.hook('pretag', throw=True, node=hex(node), tag=name,
237 local=local)
237 local=local)
238 if name in branches:
238 if name in branches:
239 self.ui.warn(_("warning: tag %s conflicts with existing"
239 self.ui.warn(_("warning: tag %s conflicts with existing"
240 " branch name\n") % name)
240 " branch name\n") % name)
241
241
242 def writetags(fp, names, munge, prevtags):
242 def writetags(fp, names, munge, prevtags):
243 fp.seek(0, 2)
243 fp.seek(0, 2)
244 if prevtags and prevtags[-1] != '\n':
244 if prevtags and prevtags[-1] != '\n':
245 fp.write('\n')
245 fp.write('\n')
246 for name in names:
246 for name in names:
247 m = munge and munge(name) or name
247 m = munge and munge(name) or name
248 if self._tagtypes and name in self._tagtypes:
248 if self._tagtypes and name in self._tagtypes:
249 old = self._tags.get(name, nullid)
249 old = self._tags.get(name, nullid)
250 fp.write('%s %s\n' % (hex(old), m))
250 fp.write('%s %s\n' % (hex(old), m))
251 fp.write('%s %s\n' % (hex(node), m))
251 fp.write('%s %s\n' % (hex(node), m))
252 fp.close()
252 fp.close()
253
253
254 prevtags = ''
254 prevtags = ''
255 if local:
255 if local:
256 try:
256 try:
257 fp = self.opener('localtags', 'r+')
257 fp = self.opener('localtags', 'r+')
258 except IOError:
258 except IOError:
259 fp = self.opener('localtags', 'a')
259 fp = self.opener('localtags', 'a')
260 else:
260 else:
261 prevtags = fp.read()
261 prevtags = fp.read()
262
262
263 # local tags are stored in the current charset
263 # local tags are stored in the current charset
264 writetags(fp, names, None, prevtags)
264 writetags(fp, names, None, prevtags)
265 for name in names:
265 for name in names:
266 self.hook('tag', node=hex(node), tag=name, local=local)
266 self.hook('tag', node=hex(node), tag=name, local=local)
267 return
267 return
268
268
269 try:
269 try:
270 fp = self.wfile('.hgtags', 'rb+')
270 fp = self.wfile('.hgtags', 'rb+')
271 except IOError:
271 except IOError:
272 fp = self.wfile('.hgtags', 'ab')
272 fp = self.wfile('.hgtags', 'ab')
273 else:
273 else:
274 prevtags = fp.read()
274 prevtags = fp.read()
275
275
276 # committed tags are stored in UTF-8
276 # committed tags are stored in UTF-8
277 writetags(fp, names, encoding.fromlocal, prevtags)
277 writetags(fp, names, encoding.fromlocal, prevtags)
278
278
279 if '.hgtags' not in self.dirstate:
279 if '.hgtags' not in self.dirstate:
280 self[None].add(['.hgtags'])
280 self[None].add(['.hgtags'])
281
281
282 m = matchmod.exact(self.root, '', ['.hgtags'])
282 m = matchmod.exact(self.root, '', ['.hgtags'])
283 tagnode = self.commit(message, user, date, extra=extra, match=m)
283 tagnode = self.commit(message, user, date, extra=extra, match=m)
284
284
285 for name in names:
285 for name in names:
286 self.hook('tag', node=hex(node), tag=name, local=local)
286 self.hook('tag', node=hex(node), tag=name, local=local)
287
287
288 return tagnode
288 return tagnode
289
289
290 def tag(self, names, node, message, local, user, date):
290 def tag(self, names, node, message, local, user, date):
291 '''tag a revision with one or more symbolic names.
291 '''tag a revision with one or more symbolic names.
292
292
293 names is a list of strings or, when adding a single tag, names may be a
293 names is a list of strings or, when adding a single tag, names may be a
294 string.
294 string.
295
295
296 if local is True, the tags are stored in a per-repository file.
296 if local is True, the tags are stored in a per-repository file.
297 otherwise, they are stored in the .hgtags file, and a new
297 otherwise, they are stored in the .hgtags file, and a new
298 changeset is committed with the change.
298 changeset is committed with the change.
299
299
300 keyword arguments:
300 keyword arguments:
301
301
302 local: whether to store tags in non-version-controlled file
302 local: whether to store tags in non-version-controlled file
303 (default False)
303 (default False)
304
304
305 message: commit message to use if committing
305 message: commit message to use if committing
306
306
307 user: name of user to use if committing
307 user: name of user to use if committing
308
308
309 date: date tuple to use if committing'''
309 date: date tuple to use if committing'''
310
310
311 if not local:
311 if not local:
312 for x in self.status()[:5]:
312 for x in self.status()[:5]:
313 if '.hgtags' in x:
313 if '.hgtags' in x:
314 raise util.Abort(_('working copy of .hgtags is changed '
314 raise util.Abort(_('working copy of .hgtags is changed '
315 '(please commit .hgtags manually)'))
315 '(please commit .hgtags manually)'))
316
316
317 self.tags() # instantiate the cache
317 self.tags() # instantiate the cache
318 self._tag(names, node, message, local, user, date)
318 self._tag(names, node, message, local, user, date)
319
319
320 def tags(self):
320 def tags(self):
321 '''return a mapping of tag to node'''
321 '''return a mapping of tag to node'''
322 if self._tags is None:
322 if self._tags is None:
323 (self._tags, self._tagtypes) = self._findtags()
323 (self._tags, self._tagtypes) = self._findtags()
324
324
325 return self._tags
325 return self._tags
326
326
327 def _findtags(self):
327 def _findtags(self):
328 '''Do the hard work of finding tags. Return a pair of dicts
328 '''Do the hard work of finding tags. Return a pair of dicts
329 (tags, tagtypes) where tags maps tag name to node, and tagtypes
329 (tags, tagtypes) where tags maps tag name to node, and tagtypes
330 maps tag name to a string like \'global\' or \'local\'.
330 maps tag name to a string like \'global\' or \'local\'.
331 Subclasses or extensions are free to add their own tags, but
331 Subclasses or extensions are free to add their own tags, but
332 should be aware that the returned dicts will be retained for the
332 should be aware that the returned dicts will be retained for the
333 duration of the localrepo object.'''
333 duration of the localrepo object.'''
334
334
335 # XXX what tagtype should subclasses/extensions use? Currently
335 # XXX what tagtype should subclasses/extensions use? Currently
336 # mq and bookmarks add tags, but do not set the tagtype at all.
336 # mq and bookmarks add tags, but do not set the tagtype at all.
337 # Should each extension invent its own tag type? Should there
337 # Should each extension invent its own tag type? Should there
338 # be one tagtype for all such "virtual" tags? Or is the status
338 # be one tagtype for all such "virtual" tags? Or is the status
339 # quo fine?
339 # quo fine?
340
340
341 alltags = {} # map tag name to (node, hist)
341 alltags = {} # map tag name to (node, hist)
342 tagtypes = {}
342 tagtypes = {}
343
343
344 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
344 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
345 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
345 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
346
346
347 # Build the return dicts. Have to re-encode tag names because
347 # Build the return dicts. Have to re-encode tag names because
348 # the tags module always uses UTF-8 (in order not to lose info
348 # the tags module always uses UTF-8 (in order not to lose info
349 # writing to the cache), but the rest of Mercurial wants them in
349 # writing to the cache), but the rest of Mercurial wants them in
350 # local encoding.
350 # local encoding.
351 tags = {}
351 tags = {}
352 for (name, (node, hist)) in alltags.iteritems():
352 for (name, (node, hist)) in alltags.iteritems():
353 if node != nullid:
353 if node != nullid:
354 tags[encoding.tolocal(name)] = node
354 tags[encoding.tolocal(name)] = node
355 tags['tip'] = self.changelog.tip()
355 tags['tip'] = self.changelog.tip()
356 tagtypes = dict([(encoding.tolocal(name), value)
356 tagtypes = dict([(encoding.tolocal(name), value)
357 for (name, value) in tagtypes.iteritems()])
357 for (name, value) in tagtypes.iteritems()])
358 return (tags, tagtypes)
358 return (tags, tagtypes)
359
359
360 def tagtype(self, tagname):
360 def tagtype(self, tagname):
361 '''
361 '''
362 return the type of the given tag. result can be:
362 return the type of the given tag. result can be:
363
363
364 'local' : a local tag
364 'local' : a local tag
365 'global' : a global tag
365 'global' : a global tag
366 None : tag does not exist
366 None : tag does not exist
367 '''
367 '''
368
368
369 self.tags()
369 self.tags()
370
370
371 return self._tagtypes.get(tagname)
371 return self._tagtypes.get(tagname)
372
372
373 def tagslist(self):
373 def tagslist(self):
374 '''return a list of tags ordered by revision'''
374 '''return a list of tags ordered by revision'''
375 l = []
375 l = []
376 for t, n in self.tags().iteritems():
376 for t, n in self.tags().iteritems():
377 try:
377 try:
378 r = self.changelog.rev(n)
378 r = self.changelog.rev(n)
379 except:
379 except:
380 r = -2 # sort to the beginning of the list if unknown
380 r = -2 # sort to the beginning of the list if unknown
381 l.append((r, t, n))
381 l.append((r, t, n))
382 return [(t, n) for r, t, n in sorted(l)]
382 return [(t, n) for r, t, n in sorted(l)]
383
383
384 def nodetags(self, node):
384 def nodetags(self, node):
385 '''return the tags associated with a node'''
385 '''return the tags associated with a node'''
386 if not self.nodetagscache:
386 if not self.nodetagscache:
387 self.nodetagscache = {}
387 self.nodetagscache = {}
388 for t, n in self.tags().iteritems():
388 for t, n in self.tags().iteritems():
389 self.nodetagscache.setdefault(n, []).append(t)
389 self.nodetagscache.setdefault(n, []).append(t)
390 for tags in self.nodetagscache.itervalues():
390 for tags in self.nodetagscache.itervalues():
391 tags.sort()
391 tags.sort()
392 return self.nodetagscache.get(node, [])
392 return self.nodetagscache.get(node, [])
393
393
394 def _branchtags(self, partial, lrev):
394 def _branchtags(self, partial, lrev):
395 # TODO: rename this function?
395 # TODO: rename this function?
396 tiprev = len(self) - 1
396 tiprev = len(self) - 1
397 if lrev != tiprev:
397 if lrev != tiprev:
398 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
398 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
399 self._updatebranchcache(partial, ctxgen)
399 self._updatebranchcache(partial, ctxgen)
400 self._writebranchcache(partial, self.changelog.tip(), tiprev)
400 self._writebranchcache(partial, self.changelog.tip(), tiprev)
401
401
402 return partial
402 return partial
403
403
404 def updatebranchcache(self):
404 def updatebranchcache(self):
405 tip = self.changelog.tip()
405 tip = self.changelog.tip()
406 if self._branchcache is not None and self._branchcachetip == tip:
406 if self._branchcache is not None and self._branchcachetip == tip:
407 return self._branchcache
407 return self._branchcache
408
408
409 oldtip = self._branchcachetip
409 oldtip = self._branchcachetip
410 self._branchcachetip = tip
410 self._branchcachetip = tip
411 if oldtip is None or oldtip not in self.changelog.nodemap:
411 if oldtip is None or oldtip not in self.changelog.nodemap:
412 partial, last, lrev = self._readbranchcache()
412 partial, last, lrev = self._readbranchcache()
413 else:
413 else:
414 lrev = self.changelog.rev(oldtip)
414 lrev = self.changelog.rev(oldtip)
415 partial = self._branchcache
415 partial = self._branchcache
416
416
417 self._branchtags(partial, lrev)
417 self._branchtags(partial, lrev)
418 # this private cache holds all heads (not just tips)
418 # this private cache holds all heads (not just tips)
419 self._branchcache = partial
419 self._branchcache = partial
420
420
421 def branchmap(self):
421 def branchmap(self):
422 '''returns a dictionary {branch: [branchheads]}'''
422 '''returns a dictionary {branch: [branchheads]}'''
423 self.updatebranchcache()
423 self.updatebranchcache()
424 return self._branchcache
424 return self._branchcache
425
425
426 def branchtags(self):
426 def branchtags(self):
427 '''return a dict where branch names map to the tipmost head of
427 '''return a dict where branch names map to the tipmost head of
428 the branch, open heads come before closed'''
428 the branch, open heads come before closed'''
429 bt = {}
429 bt = {}
430 for bn, heads in self.branchmap().iteritems():
430 for bn, heads in self.branchmap().iteritems():
431 tip = heads[-1]
431 tip = heads[-1]
432 for h in reversed(heads):
432 for h in reversed(heads):
433 if 'close' not in self.changelog.read(h)[5]:
433 if 'close' not in self.changelog.read(h)[5]:
434 tip = h
434 tip = h
435 break
435 break
436 bt[bn] = tip
436 bt[bn] = tip
437 return bt
437 return bt
438
438
439 def _readbranchcache(self):
439 def _readbranchcache(self):
440 partial = {}
440 partial = {}
441 try:
441 try:
442 f = self.opener("branchheads.cache")
442 f = self.opener("branchheads.cache")
443 lines = f.read().split('\n')
443 lines = f.read().split('\n')
444 f.close()
444 f.close()
445 except (IOError, OSError):
445 except (IOError, OSError):
446 return {}, nullid, nullrev
446 return {}, nullid, nullrev
447
447
448 try:
448 try:
449 last, lrev = lines.pop(0).split(" ", 1)
449 last, lrev = lines.pop(0).split(" ", 1)
450 last, lrev = bin(last), int(lrev)
450 last, lrev = bin(last), int(lrev)
451 if lrev >= len(self) or self[lrev].node() != last:
451 if lrev >= len(self) or self[lrev].node() != last:
452 # invalidate the cache
452 # invalidate the cache
453 raise ValueError('invalidating branch cache (tip differs)')
453 raise ValueError('invalidating branch cache (tip differs)')
454 for l in lines:
454 for l in lines:
455 if not l:
455 if not l:
456 continue
456 continue
457 node, label = l.split(" ", 1)
457 node, label = l.split(" ", 1)
458 label = encoding.tolocal(label.strip())
458 label = encoding.tolocal(label.strip())
459 partial.setdefault(label, []).append(bin(node))
459 partial.setdefault(label, []).append(bin(node))
460 except KeyboardInterrupt:
460 except KeyboardInterrupt:
461 raise
461 raise
462 except Exception, inst:
462 except Exception, inst:
463 if self.ui.debugflag:
463 if self.ui.debugflag:
464 self.ui.warn(str(inst), '\n')
464 self.ui.warn(str(inst), '\n')
465 partial, last, lrev = {}, nullid, nullrev
465 partial, last, lrev = {}, nullid, nullrev
466 return partial, last, lrev
466 return partial, last, lrev
467
467
468 def _writebranchcache(self, branches, tip, tiprev):
468 def _writebranchcache(self, branches, tip, tiprev):
469 try:
469 try:
470 f = self.opener("branchheads.cache", "w", atomictemp=True)
470 f = self.opener("branchheads.cache", "w", atomictemp=True)
471 f.write("%s %s\n" % (hex(tip), tiprev))
471 f.write("%s %s\n" % (hex(tip), tiprev))
472 for label, nodes in branches.iteritems():
472 for label, nodes in branches.iteritems():
473 for node in nodes:
473 for node in nodes:
474 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
474 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
475 f.rename()
475 f.rename()
476 except (IOError, OSError):
476 except (IOError, OSError):
477 pass
477 pass
478
478
479 def _updatebranchcache(self, partial, ctxgen):
479 def _updatebranchcache(self, partial, ctxgen):
480 # collect new branch entries
480 # collect new branch entries
481 newbranches = {}
481 newbranches = {}
482 for c in ctxgen:
482 for c in ctxgen:
483 newbranches.setdefault(c.branch(), []).append(c.node())
483 newbranches.setdefault(c.branch(), []).append(c.node())
484 # if older branchheads are reachable from new ones, they aren't
484 # if older branchheads are reachable from new ones, they aren't
485 # really branchheads. Note checking parents is insufficient:
485 # really branchheads. Note checking parents is insufficient:
486 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
486 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
487 for branch, newnodes in newbranches.iteritems():
487 for branch, newnodes in newbranches.iteritems():
488 bheads = partial.setdefault(branch, [])
488 bheads = partial.setdefault(branch, [])
489 bheads.extend(newnodes)
489 bheads.extend(newnodes)
490 if len(bheads) <= 1:
490 if len(bheads) <= 1:
491 continue
491 continue
492 # starting from tip means fewer passes over reachable
492 # starting from tip means fewer passes over reachable
493 while newnodes:
493 while newnodes:
494 latest = newnodes.pop()
494 latest = newnodes.pop()
495 if latest not in bheads:
495 if latest not in bheads:
496 continue
496 continue
497 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
497 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
498 reachable = self.changelog.reachable(latest, minbhrev)
498 reachable = self.changelog.reachable(latest, minbhrev)
499 reachable.remove(latest)
499 reachable.remove(latest)
500 bheads = [b for b in bheads if b not in reachable]
500 bheads = [b for b in bheads if b not in reachable]
501 partial[branch] = bheads
501 partial[branch] = bheads
502
502
503 def lookup(self, key):
503 def lookup(self, key):
504 if isinstance(key, int):
504 if isinstance(key, int):
505 return self.changelog.node(key)
505 return self.changelog.node(key)
506 elif key == '.':
506 elif key == '.':
507 return self.dirstate.parents()[0]
507 return self.dirstate.parents()[0]
508 elif key == 'null':
508 elif key == 'null':
509 return nullid
509 return nullid
510 elif key == 'tip':
510 elif key == 'tip':
511 return self.changelog.tip()
511 return self.changelog.tip()
512 n = self.changelog._match(key)
512 n = self.changelog._match(key)
513 if n:
513 if n:
514 return n
514 return n
515 if key in self.tags():
515 if key in self.tags():
516 return self.tags()[key]
516 return self.tags()[key]
517 if key in self.branchtags():
517 if key in self.branchtags():
518 return self.branchtags()[key]
518 return self.branchtags()[key]
519 n = self.changelog._partialmatch(key)
519 n = self.changelog._partialmatch(key)
520 if n:
520 if n:
521 return n
521 return n
522
522
523 # can't find key, check if it might have come from damaged dirstate
523 # can't find key, check if it might have come from damaged dirstate
524 if key in self.dirstate.parents():
524 if key in self.dirstate.parents():
525 raise error.Abort(_("working directory has unknown parent '%s'!")
525 raise error.Abort(_("working directory has unknown parent '%s'!")
526 % short(key))
526 % short(key))
527 try:
527 try:
528 if len(key) == 20:
528 if len(key) == 20:
529 key = hex(key)
529 key = hex(key)
530 except:
530 except:
531 pass
531 pass
532 raise error.RepoLookupError(_("unknown revision '%s'") % key)
532 raise error.RepoLookupError(_("unknown revision '%s'") % key)
533
533
534 def lookupbranch(self, key, remote=None):
534 def lookupbranch(self, key, remote=None):
535 repo = remote or self
535 repo = remote or self
536 if key in repo.branchmap():
536 if key in repo.branchmap():
537 return key
537 return key
538
538
539 repo = (remote and remote.local()) and remote or self
539 repo = (remote and remote.local()) and remote or self
540 return repo[key].branch()
540 return repo[key].branch()
541
541
542 def local(self):
542 def local(self):
543 return True
543 return True
544
544
545 def join(self, f):
545 def join(self, f):
546 return os.path.join(self.path, f)
546 return os.path.join(self.path, f)
547
547
548 def wjoin(self, f):
548 def wjoin(self, f):
549 return os.path.join(self.root, f)
549 return os.path.join(self.root, f)
550
550
551 def file(self, f):
551 def file(self, f):
552 if f[0] == '/':
552 if f[0] == '/':
553 f = f[1:]
553 f = f[1:]
554 return filelog.filelog(self.sopener, f)
554 return filelog.filelog(self.sopener, f)
555
555
556 def changectx(self, changeid):
556 def changectx(self, changeid):
557 return self[changeid]
557 return self[changeid]
558
558
559 def parents(self, changeid=None):
559 def parents(self, changeid=None):
560 '''get list of changectxs for parents of changeid'''
560 '''get list of changectxs for parents of changeid'''
561 return self[changeid].parents()
561 return self[changeid].parents()
562
562
563 def filectx(self, path, changeid=None, fileid=None):
563 def filectx(self, path, changeid=None, fileid=None):
564 """changeid can be a changeset revision, node, or tag.
564 """changeid can be a changeset revision, node, or tag.
565 fileid can be a file revision or node."""
565 fileid can be a file revision or node."""
566 return context.filectx(self, path, changeid, fileid)
566 return context.filectx(self, path, changeid, fileid)
567
567
568 def getcwd(self):
568 def getcwd(self):
569 return self.dirstate.getcwd()
569 return self.dirstate.getcwd()
570
570
571 def pathto(self, f, cwd=None):
571 def pathto(self, f, cwd=None):
572 return self.dirstate.pathto(f, cwd)
572 return self.dirstate.pathto(f, cwd)
573
573
574 def wfile(self, f, mode='r'):
574 def wfile(self, f, mode='r'):
575 return self.wopener(f, mode)
575 return self.wopener(f, mode)
576
576
577 def _link(self, f):
577 def _link(self, f):
578 return os.path.islink(self.wjoin(f))
578 return os.path.islink(self.wjoin(f))
579
579
580 def _loadfilter(self, filter):
580 def _loadfilter(self, filter):
581 if filter not in self.filterpats:
581 if filter not in self.filterpats:
582 l = []
582 l = []
583 for pat, cmd in self.ui.configitems(filter):
583 for pat, cmd in self.ui.configitems(filter):
584 if cmd == '!':
584 if cmd == '!':
585 continue
585 continue
586 mf = matchmod.match(self.root, '', [pat])
586 mf = matchmod.match(self.root, '', [pat])
587 fn = None
587 fn = None
588 params = cmd
588 params = cmd
589 for name, filterfn in self._datafilters.iteritems():
589 for name, filterfn in self._datafilters.iteritems():
590 if cmd.startswith(name):
590 if cmd.startswith(name):
591 fn = filterfn
591 fn = filterfn
592 params = cmd[len(name):].lstrip()
592 params = cmd[len(name):].lstrip()
593 break
593 break
594 if not fn:
594 if not fn:
595 fn = lambda s, c, **kwargs: util.filter(s, c)
595 fn = lambda s, c, **kwargs: util.filter(s, c)
596 # Wrap old filters not supporting keyword arguments
596 # Wrap old filters not supporting keyword arguments
597 if not inspect.getargspec(fn)[2]:
597 if not inspect.getargspec(fn)[2]:
598 oldfn = fn
598 oldfn = fn
599 fn = lambda s, c, **kwargs: oldfn(s, c)
599 fn = lambda s, c, **kwargs: oldfn(s, c)
600 l.append((mf, fn, params))
600 l.append((mf, fn, params))
601 self.filterpats[filter] = l
601 self.filterpats[filter] = l
602 return self.filterpats[filter]
602 return self.filterpats[filter]
603
603
604 def _filter(self, filterpats, filename, data):
604 def _filter(self, filterpats, filename, data):
605 for mf, fn, cmd in filterpats:
605 for mf, fn, cmd in filterpats:
606 if mf(filename):
606 if mf(filename):
607 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
607 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
608 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
608 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
609 break
609 break
610
610
611 return data
611 return data
612
612
613 @propertycache
613 @propertycache
614 def _encodefilterpats(self):
614 def _encodefilterpats(self):
615 return self._loadfilter('encode')
615 return self._loadfilter('encode')
616
616
617 @propertycache
617 @propertycache
618 def _decodefilterpats(self):
618 def _decodefilterpats(self):
619 return self._loadfilter('decode')
619 return self._loadfilter('decode')
620
620
621 def adddatafilter(self, name, filter):
621 def adddatafilter(self, name, filter):
622 self._datafilters[name] = filter
622 self._datafilters[name] = filter
623
623
624 def wread(self, filename):
624 def wread(self, filename):
625 if self._link(filename):
625 if self._link(filename):
626 data = os.readlink(self.wjoin(filename))
626 data = os.readlink(self.wjoin(filename))
627 else:
627 else:
628 data = self.wopener(filename, 'r').read()
628 data = self.wopener(filename, 'r').read()
629 return self._filter(self._encodefilterpats, filename, data)
629 return self._filter(self._encodefilterpats, filename, data)
630
630
631 def wwrite(self, filename, data, flags):
631 def wwrite(self, filename, data, flags):
632 data = self._filter(self._decodefilterpats, filename, data)
632 data = self._filter(self._decodefilterpats, filename, data)
633 if 'l' in flags:
633 if 'l' in flags:
634 self.wopener.symlink(data, filename)
634 self.wopener.symlink(data, filename)
635 else:
635 else:
636 self.wopener(filename, 'w').write(data)
636 self.wopener(filename, 'w').write(data)
637 if 'x' in flags:
637 if 'x' in flags:
638 util.set_flags(self.wjoin(filename), False, True)
638 util.set_flags(self.wjoin(filename), False, True)
639
639
640 def wwritedata(self, filename, data):
640 def wwritedata(self, filename, data):
641 return self._filter(self._decodefilterpats, filename, data)
641 return self._filter(self._decodefilterpats, filename, data)
642
642
643 def transaction(self, desc):
643 def transaction(self, desc):
644 tr = self._transref and self._transref() or None
644 tr = self._transref and self._transref() or None
645 if tr and tr.running():
645 if tr and tr.running():
646 return tr.nest()
646 return tr.nest()
647
647
648 # abort here if the journal already exists
648 # abort here if the journal already exists
649 if os.path.exists(self.sjoin("journal")):
649 if os.path.exists(self.sjoin("journal")):
650 raise error.RepoError(
650 raise error.RepoError(
651 _("abandoned transaction found - run hg recover"))
651 _("abandoned transaction found - run hg recover"))
652
652
653 # save dirstate for rollback
653 # save dirstate for rollback
654 try:
654 try:
655 ds = self.opener("dirstate").read()
655 ds = self.opener("dirstate").read()
656 except IOError:
656 except IOError:
657 ds = ""
657 ds = ""
658 self.opener("journal.dirstate", "w").write(ds)
658 self.opener("journal.dirstate", "w").write(ds)
659 self.opener("journal.branch", "w").write(
659 self.opener("journal.branch", "w").write(
660 encoding.fromlocal(self.dirstate.branch()))
660 encoding.fromlocal(self.dirstate.branch()))
661 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
661 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
662
662
663 renames = [(self.sjoin("journal"), self.sjoin("undo")),
663 renames = [(self.sjoin("journal"), self.sjoin("undo")),
664 (self.join("journal.dirstate"), self.join("undo.dirstate")),
664 (self.join("journal.dirstate"), self.join("undo.dirstate")),
665 (self.join("journal.branch"), self.join("undo.branch")),
665 (self.join("journal.branch"), self.join("undo.branch")),
666 (self.join("journal.desc"), self.join("undo.desc"))]
666 (self.join("journal.desc"), self.join("undo.desc"))]
667 tr = transaction.transaction(self.ui.warn, self.sopener,
667 tr = transaction.transaction(self.ui.warn, self.sopener,
668 self.sjoin("journal"),
668 self.sjoin("journal"),
669 aftertrans(renames),
669 aftertrans(renames),
670 self.store.createmode)
670 self.store.createmode)
671 self._transref = weakref.ref(tr)
671 self._transref = weakref.ref(tr)
672 return tr
672 return tr
673
673
674 def recover(self):
674 def recover(self):
675 lock = self.lock()
675 lock = self.lock()
676 try:
676 try:
677 if os.path.exists(self.sjoin("journal")):
677 if os.path.exists(self.sjoin("journal")):
678 self.ui.status(_("rolling back interrupted transaction\n"))
678 self.ui.status(_("rolling back interrupted transaction\n"))
679 transaction.rollback(self.sopener, self.sjoin("journal"),
679 transaction.rollback(self.sopener, self.sjoin("journal"),
680 self.ui.warn)
680 self.ui.warn)
681 self.invalidate()
681 self.invalidate()
682 return True
682 return True
683 else:
683 else:
684 self.ui.warn(_("no interrupted transaction available\n"))
684 self.ui.warn(_("no interrupted transaction available\n"))
685 return False
685 return False
686 finally:
686 finally:
687 lock.release()
687 lock.release()
688
688
689 def rollback(self, dryrun=False):
689 def rollback(self, dryrun=False):
690 wlock = lock = None
690 wlock = lock = None
691 try:
691 try:
692 wlock = self.wlock()
692 wlock = self.wlock()
693 lock = self.lock()
693 lock = self.lock()
694 if os.path.exists(self.sjoin("undo")):
694 if os.path.exists(self.sjoin("undo")):
695 try:
695 try:
696 args = self.opener("undo.desc", "r").read().splitlines()
696 args = self.opener("undo.desc", "r").read().splitlines()
697 if len(args) >= 3 and self.ui.verbose:
697 if len(args) >= 3 and self.ui.verbose:
698 desc = _("rolling back to revision %s"
698 desc = _("rolling back to revision %s"
699 " (undo %s: %s)\n") % (
699 " (undo %s: %s)\n") % (
700 int(args[0]) - 1, args[1], args[2])
700 int(args[0]) - 1, args[1], args[2])
701 elif len(args) >= 2:
701 elif len(args) >= 2:
702 desc = _("rolling back to revision %s (undo %s)\n") % (
702 desc = _("rolling back to revision %s (undo %s)\n") % (
703 int(args[0]) - 1, args[1])
703 int(args[0]) - 1, args[1])
704 except IOError:
704 except IOError:
705 desc = _("rolling back unknown transaction\n")
705 desc = _("rolling back unknown transaction\n")
706 self.ui.status(desc)
706 self.ui.status(desc)
707 if dryrun:
707 if dryrun:
708 return
708 return
709 transaction.rollback(self.sopener, self.sjoin("undo"),
709 transaction.rollback(self.sopener, self.sjoin("undo"),
710 self.ui.warn)
710 self.ui.warn)
711 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
711 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
712 try:
712 try:
713 branch = self.opener("undo.branch").read()
713 branch = self.opener("undo.branch").read()
714 self.dirstate.setbranch(branch)
714 self.dirstate.setbranch(branch)
715 except IOError:
715 except IOError:
716 self.ui.warn(_("Named branch could not be reset, "
716 self.ui.warn(_("Named branch could not be reset, "
717 "current branch still is: %s\n")
717 "current branch still is: %s\n")
718 % self.dirstate.branch())
718 % self.dirstate.branch())
719 self.invalidate()
719 self.invalidate()
720 self.dirstate.invalidate()
720 self.dirstate.invalidate()
721 self.destroyed()
721 self.destroyed()
722 else:
722 else:
723 self.ui.warn(_("no rollback information available\n"))
723 self.ui.warn(_("no rollback information available\n"))
724 return 1
724 return 1
725 finally:
725 finally:
726 release(lock, wlock)
726 release(lock, wlock)
727
727
728 def invalidatecaches(self):
728 def invalidatecaches(self):
729 self._tags = None
729 self._tags = None
730 self._tagtypes = None
730 self._tagtypes = None
731 self.nodetagscache = None
731 self.nodetagscache = None
732 self._branchcache = None # in UTF-8
732 self._branchcache = None # in UTF-8
733 self._branchcachetip = None
733 self._branchcachetip = None
734
734
735 def invalidate(self):
735 def invalidate(self):
736 for a in ("changelog", "manifest"):
736 for a in ("changelog", "manifest"):
737 if a in self.__dict__:
737 if a in self.__dict__:
738 delattr(self, a)
738 delattr(self, a)
739 self.invalidatecaches()
739 self.invalidatecaches()
740
740
741 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
741 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
742 try:
742 try:
743 l = lock.lock(lockname, 0, releasefn, desc=desc)
743 l = lock.lock(lockname, 0, releasefn, desc=desc)
744 except error.LockHeld, inst:
744 except error.LockHeld, inst:
745 if not wait:
745 if not wait:
746 raise
746 raise
747 self.ui.warn(_("waiting for lock on %s held by %r\n") %
747 self.ui.warn(_("waiting for lock on %s held by %r\n") %
748 (desc, inst.locker))
748 (desc, inst.locker))
749 # default to 600 seconds timeout
749 # default to 600 seconds timeout
750 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
750 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
751 releasefn, desc=desc)
751 releasefn, desc=desc)
752 if acquirefn:
752 if acquirefn:
753 acquirefn()
753 acquirefn()
754 return l
754 return l
755
755
756 def lock(self, wait=True):
756 def lock(self, wait=True):
757 '''Lock the repository store (.hg/store) and return a weak reference
757 '''Lock the repository store (.hg/store) and return a weak reference
758 to the lock. Use this before modifying the store (e.g. committing or
758 to the lock. Use this before modifying the store (e.g. committing or
759 stripping). If you are opening a transaction, get a lock as well.)'''
759 stripping). If you are opening a transaction, get a lock as well.)'''
760 l = self._lockref and self._lockref()
760 l = self._lockref and self._lockref()
761 if l is not None and l.held:
761 if l is not None and l.held:
762 l.lock()
762 l.lock()
763 return l
763 return l
764
764
765 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
765 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
766 _('repository %s') % self.origroot)
766 _('repository %s') % self.origroot)
767 self._lockref = weakref.ref(l)
767 self._lockref = weakref.ref(l)
768 return l
768 return l
769
769
770 def wlock(self, wait=True):
770 def wlock(self, wait=True):
771 '''Lock the non-store parts of the repository (everything under
771 '''Lock the non-store parts of the repository (everything under
772 .hg except .hg/store) and return a weak reference to the lock.
772 .hg except .hg/store) and return a weak reference to the lock.
773 Use this before modifying files in .hg.'''
773 Use this before modifying files in .hg.'''
774 l = self._wlockref and self._wlockref()
774 l = self._wlockref and self._wlockref()
775 if l is not None and l.held:
775 if l is not None and l.held:
776 l.lock()
776 l.lock()
777 return l
777 return l
778
778
779 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
779 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
780 self.dirstate.invalidate, _('working directory of %s') %
780 self.dirstate.invalidate, _('working directory of %s') %
781 self.origroot)
781 self.origroot)
782 self._wlockref = weakref.ref(l)
782 self._wlockref = weakref.ref(l)
783 return l
783 return l
784
784
785 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
785 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
786 """
786 """
787 commit an individual file as part of a larger transaction
787 commit an individual file as part of a larger transaction
788 """
788 """
789
789
790 fname = fctx.path()
790 fname = fctx.path()
791 text = fctx.data()
791 text = fctx.data()
792 flog = self.file(fname)
792 flog = self.file(fname)
793 fparent1 = manifest1.get(fname, nullid)
793 fparent1 = manifest1.get(fname, nullid)
794 fparent2 = fparent2o = manifest2.get(fname, nullid)
794 fparent2 = fparent2o = manifest2.get(fname, nullid)
795
795
796 meta = {}
796 meta = {}
797 copy = fctx.renamed()
797 copy = fctx.renamed()
798 if copy and copy[0] != fname:
798 if copy and copy[0] != fname:
799 # Mark the new revision of this file as a copy of another
799 # Mark the new revision of this file as a copy of another
800 # file. This copy data will effectively act as a parent
800 # file. This copy data will effectively act as a parent
801 # of this new revision. If this is a merge, the first
801 # of this new revision. If this is a merge, the first
802 # parent will be the nullid (meaning "look up the copy data")
802 # parent will be the nullid (meaning "look up the copy data")
803 # and the second one will be the other parent. For example:
803 # and the second one will be the other parent. For example:
804 #
804 #
805 # 0 --- 1 --- 3 rev1 changes file foo
805 # 0 --- 1 --- 3 rev1 changes file foo
806 # \ / rev2 renames foo to bar and changes it
806 # \ / rev2 renames foo to bar and changes it
807 # \- 2 -/ rev3 should have bar with all changes and
807 # \- 2 -/ rev3 should have bar with all changes and
808 # should record that bar descends from
808 # should record that bar descends from
809 # bar in rev2 and foo in rev1
809 # bar in rev2 and foo in rev1
810 #
810 #
811 # this allows this merge to succeed:
811 # this allows this merge to succeed:
812 #
812 #
813 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
813 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
814 # \ / merging rev3 and rev4 should use bar@rev2
814 # \ / merging rev3 and rev4 should use bar@rev2
815 # \- 2 --- 4 as the merge base
815 # \- 2 --- 4 as the merge base
816 #
816 #
817
817
818 cfname = copy[0]
818 cfname = copy[0]
819 crev = manifest1.get(cfname)
819 crev = manifest1.get(cfname)
820 newfparent = fparent2
820 newfparent = fparent2
821
821
822 if manifest2: # branch merge
822 if manifest2: # branch merge
823 if fparent2 == nullid or crev is None: # copied on remote side
823 if fparent2 == nullid or crev is None: # copied on remote side
824 if cfname in manifest2:
824 if cfname in manifest2:
825 crev = manifest2[cfname]
825 crev = manifest2[cfname]
826 newfparent = fparent1
826 newfparent = fparent1
827
827
828 # find source in nearest ancestor if we've lost track
828 # find source in nearest ancestor if we've lost track
829 if not crev:
829 if not crev:
830 self.ui.debug(" %s: searching for copy revision for %s\n" %
830 self.ui.debug(" %s: searching for copy revision for %s\n" %
831 (fname, cfname))
831 (fname, cfname))
832 for ancestor in self[None].ancestors():
832 for ancestor in self[None].ancestors():
833 if cfname in ancestor:
833 if cfname in ancestor:
834 crev = ancestor[cfname].filenode()
834 crev = ancestor[cfname].filenode()
835 break
835 break
836
836
837 if crev:
837 if crev:
838 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
838 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
839 meta["copy"] = cfname
839 meta["copy"] = cfname
840 meta["copyrev"] = hex(crev)
840 meta["copyrev"] = hex(crev)
841 fparent1, fparent2 = nullid, newfparent
841 fparent1, fparent2 = nullid, newfparent
842 else:
842 else:
843 self.ui.warn(_("warning: can't find ancestor for '%s' "
843 self.ui.warn(_("warning: can't find ancestor for '%s' "
844 "copied from '%s'!\n") % (fname, cfname))
844 "copied from '%s'!\n") % (fname, cfname))
845
845
846 elif fparent2 != nullid:
846 elif fparent2 != nullid:
847 # is one parent an ancestor of the other?
847 # is one parent an ancestor of the other?
848 fparentancestor = flog.ancestor(fparent1, fparent2)
848 fparentancestor = flog.ancestor(fparent1, fparent2)
849 if fparentancestor == fparent1:
849 if fparentancestor == fparent1:
850 fparent1, fparent2 = fparent2, nullid
850 fparent1, fparent2 = fparent2, nullid
851 elif fparentancestor == fparent2:
851 elif fparentancestor == fparent2:
852 fparent2 = nullid
852 fparent2 = nullid
853
853
854 # is the file changed?
854 # is the file changed?
855 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
855 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
856 changelist.append(fname)
856 changelist.append(fname)
857 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
857 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
858
858
859 # are just the flags changed during merge?
859 # are just the flags changed during merge?
860 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
860 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
861 changelist.append(fname)
861 changelist.append(fname)
862
862
863 return fparent1
863 return fparent1
864
864
865 def commit(self, text="", user=None, date=None, match=None, force=False,
865 def commit(self, text="", user=None, date=None, match=None, force=False,
866 editor=False, extra={}):
866 editor=False, extra={}):
867 """Add a new revision to current repository.
867 """Add a new revision to current repository.
868
868
869 Revision information is gathered from the working directory,
869 Revision information is gathered from the working directory,
870 match can be used to filter the committed files. If editor is
870 match can be used to filter the committed files. If editor is
871 supplied, it is called to get a commit message.
871 supplied, it is called to get a commit message.
872 """
872 """
873
873
874 def fail(f, msg):
874 def fail(f, msg):
875 raise util.Abort('%s: %s' % (f, msg))
875 raise util.Abort('%s: %s' % (f, msg))
876
876
877 if not match:
877 if not match:
878 match = matchmod.always(self.root, '')
878 match = matchmod.always(self.root, '')
879
879
880 if not force:
880 if not force:
881 vdirs = []
881 vdirs = []
882 match.dir = vdirs.append
882 match.dir = vdirs.append
883 match.bad = fail
883 match.bad = fail
884
884
885 wlock = self.wlock()
885 wlock = self.wlock()
886 try:
886 try:
887 wctx = self[None]
887 wctx = self[None]
888 merge = len(wctx.parents()) > 1
888 merge = len(wctx.parents()) > 1
889
889
890 if (not force and merge and match and
890 if (not force and merge and match and
891 (match.files() or match.anypats())):
891 (match.files() or match.anypats())):
892 raise util.Abort(_('cannot partially commit a merge '
892 raise util.Abort(_('cannot partially commit a merge '
893 '(do not specify files or patterns)'))
893 '(do not specify files or patterns)'))
894
894
895 changes = self.status(match=match, clean=force)
895 changes = self.status(match=match, clean=force)
896 if force:
896 if force:
897 changes[0].extend(changes[6]) # mq may commit unchanged files
897 changes[0].extend(changes[6]) # mq may commit unchanged files
898
898
899 # check subrepos
899 # check subrepos
900 subs = []
900 subs = []
901 removedsubs = set()
901 removedsubs = set()
902 for p in wctx.parents():
902 for p in wctx.parents():
903 removedsubs.update(s for s in p.substate if match(s))
903 removedsubs.update(s for s in p.substate if match(s))
904 for s in wctx.substate:
904 for s in wctx.substate:
905 removedsubs.discard(s)
905 removedsubs.discard(s)
906 if match(s) and wctx.sub(s).dirty():
906 if match(s) and wctx.sub(s).dirty():
907 subs.append(s)
907 subs.append(s)
908 if (subs or removedsubs):
908 if (subs or removedsubs):
909 if (not match('.hgsub') and
909 if (not match('.hgsub') and
910 '.hgsub' in (wctx.modified() + wctx.added())):
910 '.hgsub' in (wctx.modified() + wctx.added())):
911 raise util.Abort(_("can't commit subrepos without .hgsub"))
911 raise util.Abort(_("can't commit subrepos without .hgsub"))
912 if '.hgsubstate' not in changes[0]:
912 if '.hgsubstate' not in changes[0]:
913 changes[0].insert(0, '.hgsubstate')
913 changes[0].insert(0, '.hgsubstate')
914
914
915 # make sure all explicit patterns are matched
915 # make sure all explicit patterns are matched
916 if not force and match.files():
916 if not force and match.files():
917 matched = set(changes[0] + changes[1] + changes[2])
917 matched = set(changes[0] + changes[1] + changes[2])
918
918
919 for f in match.files():
919 for f in match.files():
920 if f == '.' or f in matched or f in wctx.substate:
920 if f == '.' or f in matched or f in wctx.substate:
921 continue
921 continue
922 if f in changes[3]: # missing
922 if f in changes[3]: # missing
923 fail(f, _('file not found!'))
923 fail(f, _('file not found!'))
924 if f in vdirs: # visited directory
924 if f in vdirs: # visited directory
925 d = f + '/'
925 d = f + '/'
926 for mf in matched:
926 for mf in matched:
927 if mf.startswith(d):
927 if mf.startswith(d):
928 break
928 break
929 else:
929 else:
930 fail(f, _("no match under directory!"))
930 fail(f, _("no match under directory!"))
931 elif f not in self.dirstate:
931 elif f not in self.dirstate:
932 fail(f, _("file not tracked!"))
932 fail(f, _("file not tracked!"))
933
933
934 if (not force and not extra.get("close") and not merge
934 if (not force and not extra.get("close") and not merge
935 and not (changes[0] or changes[1] or changes[2])
935 and not (changes[0] or changes[1] or changes[2])
936 and wctx.branch() == wctx.p1().branch()):
936 and wctx.branch() == wctx.p1().branch()):
937 return None
937 return None
938
938
939 ms = mergemod.mergestate(self)
939 ms = mergemod.mergestate(self)
940 for f in changes[0]:
940 for f in changes[0]:
941 if f in ms and ms[f] == 'u':
941 if f in ms and ms[f] == 'u':
942 raise util.Abort(_("unresolved merge conflicts "
942 raise util.Abort(_("unresolved merge conflicts "
943 "(see hg resolve)"))
943 "(see hg resolve)"))
944
944
945 cctx = context.workingctx(self, text, user, date, extra, changes)
945 cctx = context.workingctx(self, text, user, date, extra, changes)
946 if editor:
946 if editor:
947 cctx._text = editor(self, cctx, subs)
947 cctx._text = editor(self, cctx, subs)
948 edited = (text != cctx._text)
948 edited = (text != cctx._text)
949
949
950 # commit subs
950 # commit subs
951 if subs or removedsubs:
951 if subs or removedsubs:
952 state = wctx.substate.copy()
952 state = wctx.substate.copy()
953 for s in sorted(subs):
953 for s in sorted(subs):
954 sub = wctx.sub(s)
954 sub = wctx.sub(s)
955 self.ui.status(_('committing subrepository %s\n') %
955 self.ui.status(_('committing subrepository %s\n') %
956 subrepo.subrelpath(sub))
956 subrepo.subrelpath(sub))
957 sr = sub.commit(cctx._text, user, date)
957 sr = sub.commit(cctx._text, user, date)
958 state[s] = (state[s][0], sr)
958 state[s] = (state[s][0], sr)
959 subrepo.writestate(self, state)
959 subrepo.writestate(self, state)
960
960
961 # Save commit message in case this transaction gets rolled back
961 # Save commit message in case this transaction gets rolled back
962 # (e.g. by a pretxncommit hook). Leave the content alone on
962 # (e.g. by a pretxncommit hook). Leave the content alone on
963 # the assumption that the user will use the same editor again.
963 # the assumption that the user will use the same editor again.
964 msgfile = self.opener('last-message.txt', 'wb')
964 msgfile = self.opener('last-message.txt', 'wb')
965 msgfile.write(cctx._text)
965 msgfile.write(cctx._text)
966 msgfile.close()
966 msgfile.close()
967
967
968 p1, p2 = self.dirstate.parents()
968 p1, p2 = self.dirstate.parents()
969 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
969 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
970 try:
970 try:
971 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
971 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
972 ret = self.commitctx(cctx, True)
972 ret = self.commitctx(cctx, True)
973 except:
973 except:
974 if edited:
974 if edited:
975 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
975 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
976 self.ui.write(
976 self.ui.write(
977 _('note: commit message saved in %s\n') % msgfn)
977 _('note: commit message saved in %s\n') % msgfn)
978 raise
978 raise
979
979
980 # update dirstate and mergestate
980 # update dirstate and mergestate
981 for f in changes[0] + changes[1]:
981 for f in changes[0] + changes[1]:
982 self.dirstate.normal(f)
982 self.dirstate.normal(f)
983 for f in changes[2]:
983 for f in changes[2]:
984 self.dirstate.forget(f)
984 self.dirstate.forget(f)
985 self.dirstate.setparents(ret)
985 self.dirstate.setparents(ret)
986 ms.reset()
986 ms.reset()
987 finally:
987 finally:
988 wlock.release()
988 wlock.release()
989
989
990 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
990 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
991 return ret
991 return ret
992
992
993 def commitctx(self, ctx, error=False):
993 def commitctx(self, ctx, error=False):
994 """Add a new revision to current repository.
994 """Add a new revision to current repository.
995 Revision information is passed via the context argument.
995 Revision information is passed via the context argument.
996 """
996 """
997
997
998 tr = lock = None
998 tr = lock = None
999 removed = list(ctx.removed())
999 removed = list(ctx.removed())
1000 p1, p2 = ctx.p1(), ctx.p2()
1000 p1, p2 = ctx.p1(), ctx.p2()
1001 m1 = p1.manifest().copy()
1001 m1 = p1.manifest().copy()
1002 m2 = p2.manifest()
1002 m2 = p2.manifest()
1003 user = ctx.user()
1003 user = ctx.user()
1004
1004
1005 lock = self.lock()
1005 lock = self.lock()
1006 try:
1006 try:
1007 tr = self.transaction("commit")
1007 tr = self.transaction("commit")
1008 trp = weakref.proxy(tr)
1008 trp = weakref.proxy(tr)
1009
1009
1010 # check in files
1010 # check in files
1011 new = {}
1011 new = {}
1012 changed = []
1012 changed = []
1013 linkrev = len(self)
1013 linkrev = len(self)
1014 for f in sorted(ctx.modified() + ctx.added()):
1014 for f in sorted(ctx.modified() + ctx.added()):
1015 self.ui.note(f + "\n")
1015 self.ui.note(f + "\n")
1016 try:
1016 try:
1017 fctx = ctx[f]
1017 fctx = ctx[f]
1018 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1018 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1019 changed)
1019 changed)
1020 m1.set(f, fctx.flags())
1020 m1.set(f, fctx.flags())
1021 except OSError, inst:
1021 except OSError, inst:
1022 self.ui.warn(_("trouble committing %s!\n") % f)
1022 self.ui.warn(_("trouble committing %s!\n") % f)
1023 raise
1023 raise
1024 except IOError, inst:
1024 except IOError, inst:
1025 errcode = getattr(inst, 'errno', errno.ENOENT)
1025 errcode = getattr(inst, 'errno', errno.ENOENT)
1026 if error or errcode and errcode != errno.ENOENT:
1026 if error or errcode and errcode != errno.ENOENT:
1027 self.ui.warn(_("trouble committing %s!\n") % f)
1027 self.ui.warn(_("trouble committing %s!\n") % f)
1028 raise
1028 raise
1029 else:
1029 else:
1030 removed.append(f)
1030 removed.append(f)
1031
1031
1032 # update manifest
1032 # update manifest
1033 m1.update(new)
1033 m1.update(new)
1034 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1034 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1035 drop = [f for f in removed if f in m1]
1035 drop = [f for f in removed if f in m1]
1036 for f in drop:
1036 for f in drop:
1037 del m1[f]
1037 del m1[f]
1038 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1038 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1039 p2.manifestnode(), (new, drop))
1039 p2.manifestnode(), (new, drop))
1040
1040
1041 # update changelog
1041 # update changelog
1042 self.changelog.delayupdate()
1042 self.changelog.delayupdate()
1043 n = self.changelog.add(mn, changed + removed, ctx.description(),
1043 n = self.changelog.add(mn, changed + removed, ctx.description(),
1044 trp, p1.node(), p2.node(),
1044 trp, p1.node(), p2.node(),
1045 user, ctx.date(), ctx.extra().copy())
1045 user, ctx.date(), ctx.extra().copy())
1046 p = lambda: self.changelog.writepending() and self.root or ""
1046 p = lambda: self.changelog.writepending() and self.root or ""
1047 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1047 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1048 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1048 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1049 parent2=xp2, pending=p)
1049 parent2=xp2, pending=p)
1050 self.changelog.finalize(trp)
1050 self.changelog.finalize(trp)
1051 tr.close()
1051 tr.close()
1052
1052
1053 if self._branchcache:
1053 if self._branchcache:
1054 self.updatebranchcache()
1054 self.updatebranchcache()
1055 return n
1055 return n
1056 finally:
1056 finally:
1057 if tr:
1057 if tr:
1058 tr.release()
1058 tr.release()
1059 lock.release()
1059 lock.release()
1060
1060
1061 def destroyed(self):
1061 def destroyed(self):
1062 '''Inform the repository that nodes have been destroyed.
1062 '''Inform the repository that nodes have been destroyed.
1063 Intended for use by strip and rollback, so there's a common
1063 Intended for use by strip and rollback, so there's a common
1064 place for anything that has to be done after destroying history.'''
1064 place for anything that has to be done after destroying history.'''
1065 # XXX it might be nice if we could take the list of destroyed
1065 # XXX it might be nice if we could take the list of destroyed
1066 # nodes, but I don't see an easy way for rollback() to do that
1066 # nodes, but I don't see an easy way for rollback() to do that
1067
1067
1068 # Ensure the persistent tag cache is updated. Doing it now
1068 # Ensure the persistent tag cache is updated. Doing it now
1069 # means that the tag cache only has to worry about destroyed
1069 # means that the tag cache only has to worry about destroyed
1070 # heads immediately after a strip/rollback. That in turn
1070 # heads immediately after a strip/rollback. That in turn
1071 # guarantees that "cachetip == currenttip" (comparing both rev
1071 # guarantees that "cachetip == currenttip" (comparing both rev
1072 # and node) always means no nodes have been added or destroyed.
1072 # and node) always means no nodes have been added or destroyed.
1073
1073
1074 # XXX this is suboptimal when qrefresh'ing: we strip the current
1074 # XXX this is suboptimal when qrefresh'ing: we strip the current
1075 # head, refresh the tag cache, then immediately add a new head.
1075 # head, refresh the tag cache, then immediately add a new head.
1076 # But I think doing it this way is necessary for the "instant
1076 # But I think doing it this way is necessary for the "instant
1077 # tag cache retrieval" case to work.
1077 # tag cache retrieval" case to work.
1078 self.invalidatecaches()
1078 self.invalidatecaches()
1079
1079
1080 def walk(self, match, node=None):
1080 def walk(self, match, node=None):
1081 '''
1081 '''
1082 walk recursively through the directory tree or a given
1082 walk recursively through the directory tree or a given
1083 changeset, finding all files matched by the match
1083 changeset, finding all files matched by the match
1084 function
1084 function
1085 '''
1085 '''
1086 return self[node].walk(match)
1086 return self[node].walk(match)
1087
1087
1088 def status(self, node1='.', node2=None, match=None,
1088 def status(self, node1='.', node2=None, match=None,
1089 ignored=False, clean=False, unknown=False,
1089 ignored=False, clean=False, unknown=False,
1090 listsubrepos=False):
1090 listsubrepos=False):
1091 """return status of files between two nodes or node and working directory
1091 """return status of files between two nodes or node and working directory
1092
1092
1093 If node1 is None, use the first dirstate parent instead.
1093 If node1 is None, use the first dirstate parent instead.
1094 If node2 is None, compare node1 with working directory.
1094 If node2 is None, compare node1 with working directory.
1095 """
1095 """
1096
1096
1097 def mfmatches(ctx):
1097 def mfmatches(ctx):
1098 mf = ctx.manifest().copy()
1098 mf = ctx.manifest().copy()
1099 for fn in mf.keys():
1099 for fn in mf.keys():
1100 if not match(fn):
1100 if not match(fn):
1101 del mf[fn]
1101 del mf[fn]
1102 return mf
1102 return mf
1103
1103
1104 if isinstance(node1, context.changectx):
1104 if isinstance(node1, context.changectx):
1105 ctx1 = node1
1105 ctx1 = node1
1106 else:
1106 else:
1107 ctx1 = self[node1]
1107 ctx1 = self[node1]
1108 if isinstance(node2, context.changectx):
1108 if isinstance(node2, context.changectx):
1109 ctx2 = node2
1109 ctx2 = node2
1110 else:
1110 else:
1111 ctx2 = self[node2]
1111 ctx2 = self[node2]
1112
1112
1113 working = ctx2.rev() is None
1113 working = ctx2.rev() is None
1114 parentworking = working and ctx1 == self['.']
1114 parentworking = working and ctx1 == self['.']
1115 match = match or matchmod.always(self.root, self.getcwd())
1115 match = match or matchmod.always(self.root, self.getcwd())
1116 listignored, listclean, listunknown = ignored, clean, unknown
1116 listignored, listclean, listunknown = ignored, clean, unknown
1117
1117
1118 # load earliest manifest first for caching reasons
1118 # load earliest manifest first for caching reasons
1119 if not working and ctx2.rev() < ctx1.rev():
1119 if not working and ctx2.rev() < ctx1.rev():
1120 ctx2.manifest()
1120 ctx2.manifest()
1121
1121
1122 if not parentworking:
1122 if not parentworking:
1123 def bad(f, msg):
1123 def bad(f, msg):
1124 if f not in ctx1:
1124 if f not in ctx1:
1125 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1125 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1126 match.bad = bad
1126 match.bad = bad
1127
1127
1128 if working: # we need to scan the working dir
1128 if working: # we need to scan the working dir
1129 subrepos = []
1129 subrepos = []
1130 if '.hgsub' in self.dirstate:
1130 if '.hgsub' in self.dirstate:
1131 subrepos = ctx1.substate.keys()
1131 subrepos = ctx1.substate.keys()
1132 s = self.dirstate.status(match, subrepos, listignored,
1132 s = self.dirstate.status(match, subrepos, listignored,
1133 listclean, listunknown)
1133 listclean, listunknown)
1134 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1134 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1135
1135
1136 # check for any possibly clean files
1136 # check for any possibly clean files
1137 if parentworking and cmp:
1137 if parentworking and cmp:
1138 fixup = []
1138 fixup = []
1139 # do a full compare of any files that might have changed
1139 # do a full compare of any files that might have changed
1140 for f in sorted(cmp):
1140 for f in sorted(cmp):
1141 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1141 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1142 or ctx1[f].cmp(ctx2[f])):
1142 or ctx1[f].cmp(ctx2[f])):
1143 modified.append(f)
1143 modified.append(f)
1144 else:
1144 else:
1145 fixup.append(f)
1145 fixup.append(f)
1146
1146
1147 # update dirstate for files that are actually clean
1147 # update dirstate for files that are actually clean
1148 if fixup:
1148 if fixup:
1149 if listclean:
1149 if listclean:
1150 clean += fixup
1150 clean += fixup
1151
1151
1152 try:
1152 try:
1153 # updating the dirstate is optional
1153 # updating the dirstate is optional
1154 # so we don't wait on the lock
1154 # so we don't wait on the lock
1155 wlock = self.wlock(False)
1155 wlock = self.wlock(False)
1156 try:
1156 try:
1157 for f in fixup:
1157 for f in fixup:
1158 self.dirstate.normal(f)
1158 self.dirstate.normal(f)
1159 finally:
1159 finally:
1160 wlock.release()
1160 wlock.release()
1161 except error.LockError:
1161 except error.LockError:
1162 pass
1162 pass
1163
1163
1164 if not parentworking:
1164 if not parentworking:
1165 mf1 = mfmatches(ctx1)
1165 mf1 = mfmatches(ctx1)
1166 if working:
1166 if working:
1167 # we are comparing working dir against non-parent
1167 # we are comparing working dir against non-parent
1168 # generate a pseudo-manifest for the working dir
1168 # generate a pseudo-manifest for the working dir
1169 mf2 = mfmatches(self['.'])
1169 mf2 = mfmatches(self['.'])
1170 for f in cmp + modified + added:
1170 for f in cmp + modified + added:
1171 mf2[f] = None
1171 mf2[f] = None
1172 mf2.set(f, ctx2.flags(f))
1172 mf2.set(f, ctx2.flags(f))
1173 for f in removed:
1173 for f in removed:
1174 if f in mf2:
1174 if f in mf2:
1175 del mf2[f]
1175 del mf2[f]
1176 else:
1176 else:
1177 # we are comparing two revisions
1177 # we are comparing two revisions
1178 deleted, unknown, ignored = [], [], []
1178 deleted, unknown, ignored = [], [], []
1179 mf2 = mfmatches(ctx2)
1179 mf2 = mfmatches(ctx2)
1180
1180
1181 modified, added, clean = [], [], []
1181 modified, added, clean = [], [], []
1182 for fn in mf2:
1182 for fn in mf2:
1183 if fn in mf1:
1183 if fn in mf1:
1184 if (mf1.flags(fn) != mf2.flags(fn) or
1184 if (mf1.flags(fn) != mf2.flags(fn) or
1185 (mf1[fn] != mf2[fn] and
1185 (mf1[fn] != mf2[fn] and
1186 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1186 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1187 modified.append(fn)
1187 modified.append(fn)
1188 elif listclean:
1188 elif listclean:
1189 clean.append(fn)
1189 clean.append(fn)
1190 del mf1[fn]
1190 del mf1[fn]
1191 else:
1191 else:
1192 added.append(fn)
1192 added.append(fn)
1193 removed = mf1.keys()
1193 removed = mf1.keys()
1194
1194
1195 r = modified, added, removed, deleted, unknown, ignored, clean
1195 r = modified, added, removed, deleted, unknown, ignored, clean
1196
1196
1197 if listsubrepos:
1197 if listsubrepos:
1198 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1198 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1199 if working:
1199 if working:
1200 rev2 = None
1200 rev2 = None
1201 else:
1201 else:
1202 rev2 = ctx2.substate[subpath][1]
1202 rev2 = ctx2.substate[subpath][1]
1203 try:
1203 try:
1204 submatch = matchmod.narrowmatcher(subpath, match)
1204 submatch = matchmod.narrowmatcher(subpath, match)
1205 s = sub.status(rev2, match=submatch, ignored=listignored,
1205 s = sub.status(rev2, match=submatch, ignored=listignored,
1206 clean=listclean, unknown=listunknown,
1206 clean=listclean, unknown=listunknown,
1207 listsubrepos=True)
1207 listsubrepos=True)
1208 for rfiles, sfiles in zip(r, s):
1208 for rfiles, sfiles in zip(r, s):
1209 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1209 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1210 except error.LookupError:
1210 except error.LookupError:
1211 self.ui.status(_("skipping missing subrepository: %s\n")
1211 self.ui.status(_("skipping missing subrepository: %s\n")
1212 % subpath)
1212 % subpath)
1213
1213
1214 [l.sort() for l in r]
1214 [l.sort() for l in r]
1215 return r
1215 return r
1216
1216
1217 def heads(self, start=None):
1217 def heads(self, start=None):
1218 heads = self.changelog.heads(start)
1218 heads = self.changelog.heads(start)
1219 # sort the output in rev descending order
1219 # sort the output in rev descending order
1220 return sorted(heads, key=self.changelog.rev, reverse=True)
1220 return sorted(heads, key=self.changelog.rev, reverse=True)
1221
1221
1222 def branchheads(self, branch=None, start=None, closed=False):
1222 def branchheads(self, branch=None, start=None, closed=False):
1223 '''return a (possibly filtered) list of heads for the given branch
1223 '''return a (possibly filtered) list of heads for the given branch
1224
1224
1225 Heads are returned in topological order, from newest to oldest.
1225 Heads are returned in topological order, from newest to oldest.
1226 If branch is None, use the dirstate branch.
1226 If branch is None, use the dirstate branch.
1227 If start is not None, return only heads reachable from start.
1227 If start is not None, return only heads reachable from start.
1228 If closed is True, return heads that are marked as closed as well.
1228 If closed is True, return heads that are marked as closed as well.
1229 '''
1229 '''
1230 if branch is None:
1230 if branch is None:
1231 branch = self[None].branch()
1231 branch = self[None].branch()
1232 branches = self.branchmap()
1232 branches = self.branchmap()
1233 if branch not in branches:
1233 if branch not in branches:
1234 return []
1234 return []
1235 # the cache returns heads ordered lowest to highest
1235 # the cache returns heads ordered lowest to highest
1236 bheads = list(reversed(branches[branch]))
1236 bheads = list(reversed(branches[branch]))
1237 if start is not None:
1237 if start is not None:
1238 # filter out the heads that cannot be reached from startrev
1238 # filter out the heads that cannot be reached from startrev
1239 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1239 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1240 bheads = [h for h in bheads if h in fbheads]
1240 bheads = [h for h in bheads if h in fbheads]
1241 if not closed:
1241 if not closed:
1242 bheads = [h for h in bheads if
1242 bheads = [h for h in bheads if
1243 ('close' not in self.changelog.read(h)[5])]
1243 ('close' not in self.changelog.read(h)[5])]
1244 return bheads
1244 return bheads
1245
1245
1246 def branches(self, nodes):
1246 def branches(self, nodes):
1247 if not nodes:
1247 if not nodes:
1248 nodes = [self.changelog.tip()]
1248 nodes = [self.changelog.tip()]
1249 b = []
1249 b = []
1250 for n in nodes:
1250 for n in nodes:
1251 t = n
1251 t = n
1252 while 1:
1252 while 1:
1253 p = self.changelog.parents(n)
1253 p = self.changelog.parents(n)
1254 if p[1] != nullid or p[0] == nullid:
1254 if p[1] != nullid or p[0] == nullid:
1255 b.append((t, n, p[0], p[1]))
1255 b.append((t, n, p[0], p[1]))
1256 break
1256 break
1257 n = p[0]
1257 n = p[0]
1258 return b
1258 return b
1259
1259
1260 def between(self, pairs):
1260 def between(self, pairs):
1261 r = []
1261 r = []
1262
1262
1263 for top, bottom in pairs:
1263 for top, bottom in pairs:
1264 n, l, i = top, [], 0
1264 n, l, i = top, [], 0
1265 f = 1
1265 f = 1
1266
1266
1267 while n != bottom and n != nullid:
1267 while n != bottom and n != nullid:
1268 p = self.changelog.parents(n)[0]
1268 p = self.changelog.parents(n)[0]
1269 if i == f:
1269 if i == f:
1270 l.append(n)
1270 l.append(n)
1271 f = f * 2
1271 f = f * 2
1272 n = p
1272 n = p
1273 i += 1
1273 i += 1
1274
1274
1275 r.append(l)
1275 r.append(l)
1276
1276
1277 return r
1277 return r
1278
1278
1279 def pull(self, remote, heads=None, force=False):
1279 def pull(self, remote, heads=None, force=False):
1280 lock = self.lock()
1280 lock = self.lock()
1281 try:
1281 try:
1282 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1282 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1283 force=force)
1283 force=force)
1284 common, fetch, rheads = tmp
1284 common, fetch, rheads = tmp
1285 if not fetch:
1285 if not fetch:
1286 self.ui.status(_("no changes found\n"))
1286 self.ui.status(_("no changes found\n"))
1287 return 0
1287 return 0
1288
1288
1289 if heads is None and fetch == [nullid]:
1289 if heads is None and fetch == [nullid]:
1290 self.ui.status(_("requesting all changes\n"))
1290 self.ui.status(_("requesting all changes\n"))
1291 elif heads is None and remote.capable('changegroupsubset'):
1291 elif heads is None and remote.capable('changegroupsubset'):
1292 # issue1320, avoid a race if remote changed after discovery
1292 # issue1320, avoid a race if remote changed after discovery
1293 heads = rheads
1293 heads = rheads
1294
1294
1295 if heads is None:
1295 if heads is None:
1296 cg = remote.changegroup(fetch, 'pull')
1296 cg = remote.changegroup(fetch, 'pull')
1297 else:
1297 else:
1298 if not remote.capable('changegroupsubset'):
1298 if not remote.capable('changegroupsubset'):
1299 raise util.Abort(_("partial pull cannot be done because "
1299 raise util.Abort(_("partial pull cannot be done because "
1300 "other repository doesn't support "
1300 "other repository doesn't support "
1301 "changegroupsubset."))
1301 "changegroupsubset."))
1302 cg = remote.changegroupsubset(fetch, heads, 'pull')
1302 cg = remote.changegroupsubset(fetch, heads, 'pull')
1303 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1303 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1304 finally:
1304 finally:
1305 lock.release()
1305 lock.release()
1306
1306
1307 def push(self, remote, force=False, revs=None, newbranch=False):
1307 def push(self, remote, force=False, revs=None, newbranch=False):
1308 '''Push outgoing changesets (limited by revs) from the current
1308 '''Push outgoing changesets (limited by revs) from the current
1309 repository to remote. Return an integer:
1309 repository to remote. Return an integer:
1310 - 0 means HTTP error *or* nothing to push
1310 - 0 means HTTP error *or* nothing to push
1311 - 1 means we pushed and remote head count is unchanged *or*
1311 - 1 means we pushed and remote head count is unchanged *or*
1312 we have outgoing changesets but refused to push
1312 we have outgoing changesets but refused to push
1313 - other values as described by addchangegroup()
1313 - other values as described by addchangegroup()
1314 '''
1314 '''
1315 # there are two ways to push to remote repo:
1315 # there are two ways to push to remote repo:
1316 #
1316 #
1317 # addchangegroup assumes local user can lock remote
1317 # addchangegroup assumes local user can lock remote
1318 # repo (local filesystem, old ssh servers).
1318 # repo (local filesystem, old ssh servers).
1319 #
1319 #
1320 # unbundle assumes local user cannot lock remote repo (new ssh
1320 # unbundle assumes local user cannot lock remote repo (new ssh
1321 # servers, http servers).
1321 # servers, http servers).
1322
1322
1323 lock = None
1323 lock = None
1324 unbundle = remote.capable('unbundle')
1324 unbundle = remote.capable('unbundle')
1325 if not unbundle:
1325 if not unbundle:
1326 lock = remote.lock()
1326 lock = remote.lock()
1327 try:
1327 try:
1328 ret = discovery.prepush(self, remote, force, revs, newbranch)
1328 ret = discovery.prepush(self, remote, force, revs, newbranch)
1329 if ret[0] is None:
1329 if ret[0] is None:
1330 # and here we return 0 for "nothing to push" or 1 for
1330 # and here we return 0 for "nothing to push" or 1 for
1331 # "something to push but I refuse"
1331 # "something to push but I refuse"
1332 return ret[1]
1332 return ret[1]
1333
1333
1334 cg, remote_heads = ret
1334 cg, remote_heads = ret
1335 if unbundle:
1335 if unbundle:
1336 # local repo finds heads on server, finds out what revs it must
1336 # local repo finds heads on server, finds out what revs it must
1337 # push. once revs transferred, if server finds it has
1337 # push. once revs transferred, if server finds it has
1338 # different heads (someone else won commit/push race), server
1338 # different heads (someone else won commit/push race), server
1339 # aborts.
1339 # aborts.
1340 if force:
1340 if force:
1341 remote_heads = ['force']
1341 remote_heads = ['force']
1342 # ssh: return remote's addchangegroup()
1342 # ssh: return remote's addchangegroup()
1343 # http: return remote's addchangegroup() or 0 for error
1343 # http: return remote's addchangegroup() or 0 for error
1344 return remote.unbundle(cg, remote_heads, 'push')
1344 return remote.unbundle(cg, remote_heads, 'push')
1345 else:
1345 else:
1346 # we return an integer indicating remote head count change
1346 # we return an integer indicating remote head count change
1347 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1347 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1348 finally:
1348 finally:
1349 if lock is not None:
1349 if lock is not None:
1350 lock.release()
1350 lock.release()
1351
1351
1352 def changegroupinfo(self, nodes, source):
1352 def changegroupinfo(self, nodes, source):
1353 if self.ui.verbose or source == 'bundle':
1353 if self.ui.verbose or source == 'bundle':
1354 self.ui.status(_("%d changesets found\n") % len(nodes))
1354 self.ui.status(_("%d changesets found\n") % len(nodes))
1355 if self.ui.debugflag:
1355 if self.ui.debugflag:
1356 self.ui.debug("list of changesets:\n")
1356 self.ui.debug("list of changesets:\n")
1357 for node in nodes:
1357 for node in nodes:
1358 self.ui.debug("%s\n" % hex(node))
1358 self.ui.debug("%s\n" % hex(node))
1359
1359
1360 def changegroupsubset(self, bases, heads, source, extranodes=None):
1360 def changegroupsubset(self, bases, heads, source, extranodes=None):
1361 """Compute a changegroup consisting of all the nodes that are
1361 """Compute a changegroup consisting of all the nodes that are
1362 descendents of any of the bases and ancestors of any of the heads.
1362 descendents of any of the bases and ancestors of any of the heads.
1363 Return a chunkbuffer object whose read() method will return
1363 Return a chunkbuffer object whose read() method will return
1364 successive changegroup chunks.
1364 successive changegroup chunks.
1365
1365
1366 It is fairly complex as determining which filenodes and which
1366 It is fairly complex as determining which filenodes and which
1367 manifest nodes need to be included for the changeset to be complete
1367 manifest nodes need to be included for the changeset to be complete
1368 is non-trivial.
1368 is non-trivial.
1369
1369
1370 Another wrinkle is doing the reverse, figuring out which changeset in
1370 Another wrinkle is doing the reverse, figuring out which changeset in
1371 the changegroup a particular filenode or manifestnode belongs to.
1371 the changegroup a particular filenode or manifestnode belongs to.
1372
1372
1373 The caller can specify some nodes that must be included in the
1373 The caller can specify some nodes that must be included in the
1374 changegroup using the extranodes argument. It should be a dict
1374 changegroup using the extranodes argument. It should be a dict
1375 where the keys are the filenames (or 1 for the manifest), and the
1375 where the keys are the filenames (or 1 for the manifest), and the
1376 values are lists of (node, linknode) tuples, where node is a wanted
1376 values are lists of (node, linknode) tuples, where node is a wanted
1377 node and linknode is the changelog node that should be transmitted as
1377 node and linknode is the changelog node that should be transmitted as
1378 the linkrev.
1378 the linkrev.
1379 """
1379 """
1380
1380
1381 # Set up some initial variables
1381 # Set up some initial variables
1382 # Make it easy to refer to self.changelog
1382 # Make it easy to refer to self.changelog
1383 cl = self.changelog
1383 cl = self.changelog
1384 # Compute the list of changesets in this changegroup.
1384 # Compute the list of changesets in this changegroup.
1385 # Some bases may turn out to be superfluous, and some heads may be
1385 # Some bases may turn out to be superfluous, and some heads may be
1386 # too. nodesbetween will return the minimal set of bases and heads
1386 # too. nodesbetween will return the minimal set of bases and heads
1387 # necessary to re-create the changegroup.
1387 # necessary to re-create the changegroup.
1388 if not bases:
1388 if not bases:
1389 bases = [nullid]
1389 bases = [nullid]
1390 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1390 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1391
1391
1392 if extranodes is None:
1392 if extranodes is None:
1393 # can we go through the fast path ?
1393 # can we go through the fast path ?
1394 heads.sort()
1394 heads.sort()
1395 allheads = self.heads()
1395 allheads = self.heads()
1396 allheads.sort()
1396 allheads.sort()
1397 if heads == allheads:
1397 if heads == allheads:
1398 return self._changegroup(msng_cl_lst, source)
1398 return self._changegroup(msng_cl_lst, source)
1399
1399
1400 # slow path
1400 # slow path
1401 self.hook('preoutgoing', throw=True, source=source)
1401 self.hook('preoutgoing', throw=True, source=source)
1402
1402
1403 self.changegroupinfo(msng_cl_lst, source)
1403 self.changegroupinfo(msng_cl_lst, source)
1404
1404
1405 # We assume that all ancestors of bases are known
1405 # We assume that all ancestors of bases are known
1406 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1406 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1407
1407
1408 # Make it easy to refer to self.manifest
1408 # Make it easy to refer to self.manifest
1409 mnfst = self.manifest
1409 mnfst = self.manifest
1410 # We don't know which manifests are missing yet
1410 # We don't know which manifests are missing yet
1411 msng_mnfst_set = {}
1411 msng_mnfst_set = {}
1412 # Nor do we know which filenodes are missing.
1412 # Nor do we know which filenodes are missing.
1413 msng_filenode_set = {}
1413 msng_filenode_set = {}
1414
1414
1415 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1416 junk = None
1417
1418 # A changeset always belongs to itself, so the changenode lookup
1415 # A changeset always belongs to itself, so the changenode lookup
1419 # function for a changenode is identity.
1416 # function for a changenode is identity.
1420 def identity(x):
1417 def identity(x):
1421 return x
1418 return x
1422
1419
1423 # A function generating function that sets up the initial environment
1420 # A function generating function that sets up the initial environment
1424 # the inner function.
1421 # the inner function.
1425 def filenode_collector(changedfiles):
1422 def filenode_collector(changedfiles):
1426 # This gathers information from each manifestnode included in the
1423 # This gathers information from each manifestnode included in the
1427 # changegroup about which filenodes the manifest node references
1424 # changegroup about which filenodes the manifest node references
1428 # so we can include those in the changegroup too.
1425 # so we can include those in the changegroup too.
1429 #
1426 #
1430 # It also remembers which changenode each filenode belongs to. It
1427 # It also remembers which changenode each filenode belongs to. It
1431 # does this by assuming the a filenode belongs to the changenode
1428 # does this by assuming the a filenode belongs to the changenode
1432 # the first manifest that references it belongs to.
1429 # the first manifest that references it belongs to.
1433 def collect_msng_filenodes(mnfstnode):
1430 def collect_msng_filenodes(mnfstnode):
1434 r = mnfst.rev(mnfstnode)
1431 r = mnfst.rev(mnfstnode)
1435 if mnfst.deltaparent(r) in mnfst.parentrevs(r):
1432 if mnfst.deltaparent(r) in mnfst.parentrevs(r):
1436 # If the previous rev is one of the parents,
1433 # If the previous rev is one of the parents,
1437 # we only need to see a diff.
1434 # we only need to see a diff.
1438 deltamf = mnfst.readdelta(mnfstnode)
1435 deltamf = mnfst.readdelta(mnfstnode)
1439 # For each line in the delta
1436 # For each line in the delta
1440 for f, fnode in deltamf.iteritems():
1437 for f, fnode in deltamf.iteritems():
1441 # And if the file is in the list of files we care
1438 # And if the file is in the list of files we care
1442 # about.
1439 # about.
1443 if f in changedfiles:
1440 if f in changedfiles:
1444 # Get the changenode this manifest belongs to
1441 # Get the changenode this manifest belongs to
1445 clnode = msng_mnfst_set[mnfstnode]
1442 clnode = msng_mnfst_set[mnfstnode]
1446 # Create the set of filenodes for the file if
1443 # Create the set of filenodes for the file if
1447 # there isn't one already.
1444 # there isn't one already.
1448 ndset = msng_filenode_set.setdefault(f, {})
1445 ndset = msng_filenode_set.setdefault(f, {})
1449 # And set the filenode's changelog node to the
1446 # And set the filenode's changelog node to the
1450 # manifest's if it hasn't been set already.
1447 # manifest's if it hasn't been set already.
1451 ndset.setdefault(fnode, clnode)
1448 ndset.setdefault(fnode, clnode)
1452 else:
1449 else:
1453 # Otherwise we need a full manifest.
1450 # Otherwise we need a full manifest.
1454 m = mnfst.read(mnfstnode)
1451 m = mnfst.read(mnfstnode)
1455 # For every file in we care about.
1452 # For every file in we care about.
1456 for f in changedfiles:
1453 for f in changedfiles:
1457 fnode = m.get(f, None)
1454 fnode = m.get(f, None)
1458 # If it's in the manifest
1455 # If it's in the manifest
1459 if fnode is not None:
1456 if fnode is not None:
1460 # See comments above.
1457 # See comments above.
1461 clnode = msng_mnfst_set[mnfstnode]
1458 clnode = msng_mnfst_set[mnfstnode]
1462 ndset = msng_filenode_set.setdefault(f, {})
1459 ndset = msng_filenode_set.setdefault(f, {})
1463 ndset.setdefault(fnode, clnode)
1460 ndset.setdefault(fnode, clnode)
1464 return collect_msng_filenodes
1461 return collect_msng_filenodes
1465
1462
1466 # If we determine that a particular file or manifest node must be a
1463 # If we determine that a particular file or manifest node must be a
1467 # node that the recipient of the changegroup will already have, we can
1464 # node that the recipient of the changegroup will already have, we can
1468 # also assume the recipient will have all the parents. This function
1465 # also assume the recipient will have all the parents. This function
1469 # prunes them from the set of missing nodes.
1466 # prunes them from the set of missing nodes.
1470 def prune(revlog, missingnodes):
1467 def prune(revlog, missingnodes):
1471 hasset = set()
1468 hasset = set()
1472 # If a 'missing' filenode thinks it belongs to a changenode we
1469 # If a 'missing' filenode thinks it belongs to a changenode we
1473 # assume the recipient must have, then the recipient must have
1470 # assume the recipient must have, then the recipient must have
1474 # that filenode.
1471 # that filenode.
1475 for n in missingnodes:
1472 for n in missingnodes:
1476 clrev = revlog.linkrev(revlog.rev(n))
1473 clrev = revlog.linkrev(revlog.rev(n))
1477 if clrev in commonrevs:
1474 if clrev in commonrevs:
1478 hasset.add(n)
1475 hasset.add(n)
1479 for n in hasset:
1476 for n in hasset:
1480 missingnodes.pop(n, None)
1477 missingnodes.pop(n, None)
1481 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1478 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1482 missingnodes.pop(revlog.node(r), None)
1479 missingnodes.pop(revlog.node(r), None)
1483
1480
1484 # Add the nodes that were explicitly requested.
1481 # Add the nodes that were explicitly requested.
1485 def add_extra_nodes(name, nodes):
1482 def add_extra_nodes(name, nodes):
1486 if not extranodes or name not in extranodes:
1483 if not extranodes or name not in extranodes:
1487 return
1484 return
1488
1485
1489 for node, linknode in extranodes[name]:
1486 for node, linknode in extranodes[name]:
1490 if node not in nodes:
1487 if node not in nodes:
1491 nodes[node] = linknode
1488 nodes[node] = linknode
1492
1489
1493 # Now that we have all theses utility functions to help out and
1490 # Now that we have all theses utility functions to help out and
1494 # logically divide up the task, generate the group.
1491 # logically divide up the task, generate the group.
1495 def gengroup():
1492 def gengroup():
1496 # The set of changed files starts empty.
1493 # The set of changed files starts empty.
1497 changedfiles = set()
1494 changedfiles = set()
1498 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1495 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1499
1496
1500 # Create a changenode group generator that will call our functions
1497 # Create a changenode group generator that will call our functions
1501 # back to lookup the owning changenode and collect information.
1498 # back to lookup the owning changenode and collect information.
1502 group = cl.group(msng_cl_lst, identity, collect)
1499 group = cl.group(msng_cl_lst, identity, collect)
1503 for cnt, chnk in enumerate(group):
1500 for cnt, chnk in enumerate(group):
1504 yield chnk
1501 yield chnk
1505 # revlog.group yields three entries per node, so
1502 # revlog.group yields three entries per node, so
1506 # dividing by 3 gives an approximation of how many
1503 # dividing by 3 gives an approximation of how many
1507 # nodes have been processed.
1504 # nodes have been processed.
1508 self.ui.progress(_('bundling'), cnt / 3,
1505 self.ui.progress(_('bundling'), cnt / 3,
1509 unit=_('changesets'))
1506 unit=_('changesets'))
1510 changecount = cnt / 3
1507 changecount = cnt / 3
1511 self.ui.progress(_('bundling'), None)
1508 self.ui.progress(_('bundling'), None)
1512
1509
1513 prune(mnfst, msng_mnfst_set)
1510 prune(mnfst, msng_mnfst_set)
1514 add_extra_nodes(1, msng_mnfst_set)
1511 add_extra_nodes(1, msng_mnfst_set)
1515 msng_mnfst_lst = msng_mnfst_set.keys()
1512 msng_mnfst_lst = msng_mnfst_set.keys()
1516 # Sort the manifestnodes by revision number.
1513 # Sort the manifestnodes by revision number.
1517 msng_mnfst_lst.sort(key=mnfst.rev)
1514 msng_mnfst_lst.sort(key=mnfst.rev)
1518 # Create a generator for the manifestnodes that calls our lookup
1515 # Create a generator for the manifestnodes that calls our lookup
1519 # and data collection functions back.
1516 # and data collection functions back.
1520 group = mnfst.group(msng_mnfst_lst,
1517 group = mnfst.group(msng_mnfst_lst,
1521 lambda mnode: msng_mnfst_set[mnode],
1518 lambda mnode: msng_mnfst_set[mnode],
1522 filenode_collector(changedfiles))
1519 filenode_collector(changedfiles))
1523 efiles = {}
1520 efiles = {}
1524 for cnt, chnk in enumerate(group):
1521 for cnt, chnk in enumerate(group):
1525 if cnt % 3 == 1:
1522 if cnt % 3 == 1:
1526 mnode = chnk[:20]
1523 mnode = chnk[:20]
1527 efiles.update(mnfst.readdelta(mnode))
1524 efiles.update(mnfst.readdelta(mnode))
1528 yield chnk
1525 yield chnk
1529 # see above comment for why we divide by 3
1526 # see above comment for why we divide by 3
1530 self.ui.progress(_('bundling'), cnt / 3,
1527 self.ui.progress(_('bundling'), cnt / 3,
1531 unit=_('manifests'), total=changecount)
1528 unit=_('manifests'), total=changecount)
1532 self.ui.progress(_('bundling'), None)
1529 self.ui.progress(_('bundling'), None)
1533 efiles = len(efiles)
1530 efiles = len(efiles)
1534
1531
1535 # These are no longer needed, dereference and toss the memory for
1532 # These are no longer needed, dereference and toss the memory for
1536 # them.
1533 # them.
1537 msng_mnfst_lst = None
1534 msng_mnfst_lst = None
1538 msng_mnfst_set.clear()
1535 msng_mnfst_set.clear()
1539
1536
1540 if extranodes:
1537 if extranodes:
1541 for fname in extranodes:
1538 for fname in extranodes:
1542 if isinstance(fname, int):
1539 if isinstance(fname, int):
1543 continue
1540 continue
1544 msng_filenode_set.setdefault(fname, {})
1541 msng_filenode_set.setdefault(fname, {})
1545 changedfiles.add(fname)
1542 changedfiles.add(fname)
1546 # Go through all our files in order sorted by name.
1543 # Go through all our files in order sorted by name.
1547 for idx, fname in enumerate(sorted(changedfiles)):
1544 for idx, fname in enumerate(sorted(changedfiles)):
1548 filerevlog = self.file(fname)
1545 filerevlog = self.file(fname)
1549 if not len(filerevlog):
1546 if not len(filerevlog):
1550 raise util.Abort(_("empty or missing revlog for %s") % fname)
1547 raise util.Abort(_("empty or missing revlog for %s") % fname)
1551 # Toss out the filenodes that the recipient isn't really
1548 # Toss out the filenodes that the recipient isn't really
1552 # missing.
1549 # missing.
1553 missingfnodes = msng_filenode_set.pop(fname, {})
1550 missingfnodes = msng_filenode_set.pop(fname, {})
1554 prune(filerevlog, missingfnodes)
1551 prune(filerevlog, missingfnodes)
1555 add_extra_nodes(fname, missingfnodes)
1552 add_extra_nodes(fname, missingfnodes)
1556 # If any filenodes are left, generate the group for them,
1553 # If any filenodes are left, generate the group for them,
1557 # otherwise don't bother.
1554 # otherwise don't bother.
1558 if missingfnodes:
1555 if missingfnodes:
1559 yield changegroup.chunkheader(len(fname))
1556 yield changegroup.chunkheader(len(fname))
1560 yield fname
1557 yield fname
1561 # Sort the filenodes by their revision # (topological order)
1558 # Sort the filenodes by their revision # (topological order)
1562 nodeiter = list(missingfnodes)
1559 nodeiter = list(missingfnodes)
1563 nodeiter.sort(key=filerevlog.rev)
1560 nodeiter.sort(key=filerevlog.rev)
1564 # Create a group generator and only pass in a changenode
1561 # Create a group generator and only pass in a changenode
1565 # lookup function as we need to collect no information
1562 # lookup function as we need to collect no information
1566 # from filenodes.
1563 # from filenodes.
1567 group = filerevlog.group(nodeiter,
1564 group = filerevlog.group(nodeiter,
1568 lambda fnode: missingfnodes[fnode])
1565 lambda fnode: missingfnodes[fnode])
1569 for chnk in group:
1566 for chnk in group:
1570 # even though we print the same progress on
1567 # even though we print the same progress on
1571 # most loop iterations, put the progress call
1568 # most loop iterations, put the progress call
1572 # here so that time estimates (if any) can be updated
1569 # here so that time estimates (if any) can be updated
1573 self.ui.progress(
1570 self.ui.progress(
1574 _('bundling'), idx, item=fname,
1571 _('bundling'), idx, item=fname,
1575 unit=_('files'), total=efiles)
1572 unit=_('files'), total=efiles)
1576 yield chnk
1573 yield chnk
1577 # Signal that no more groups are left.
1574 # Signal that no more groups are left.
1578 yield changegroup.closechunk()
1575 yield changegroup.closechunk()
1579 self.ui.progress(_('bundling'), None)
1576 self.ui.progress(_('bundling'), None)
1580
1577
1581 if msng_cl_lst:
1578 if msng_cl_lst:
1582 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1579 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1583
1580
1584 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1581 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1585
1582
1586 def changegroup(self, basenodes, source):
1583 def changegroup(self, basenodes, source):
1587 # to avoid a race we use changegroupsubset() (issue1320)
1584 # to avoid a race we use changegroupsubset() (issue1320)
1588 return self.changegroupsubset(basenodes, self.heads(), source)
1585 return self.changegroupsubset(basenodes, self.heads(), source)
1589
1586
1590 def _changegroup(self, nodes, source):
1587 def _changegroup(self, nodes, source):
1591 """Compute the changegroup of all nodes that we have that a recipient
1588 """Compute the changegroup of all nodes that we have that a recipient
1592 doesn't. Return a chunkbuffer object whose read() method will return
1589 doesn't. Return a chunkbuffer object whose read() method will return
1593 successive changegroup chunks.
1590 successive changegroup chunks.
1594
1591
1595 This is much easier than the previous function as we can assume that
1592 This is much easier than the previous function as we can assume that
1596 the recipient has any changenode we aren't sending them.
1593 the recipient has any changenode we aren't sending them.
1597
1594
1598 nodes is the set of nodes to send"""
1595 nodes is the set of nodes to send"""
1599
1596
1600 self.hook('preoutgoing', throw=True, source=source)
1597 self.hook('preoutgoing', throw=True, source=source)
1601
1598
1602 cl = self.changelog
1599 cl = self.changelog
1603 revset = set([cl.rev(n) for n in nodes])
1600 revset = set([cl.rev(n) for n in nodes])
1604 self.changegroupinfo(nodes, source)
1601 self.changegroupinfo(nodes, source)
1605
1602
1606 def identity(x):
1603 def identity(x):
1607 return x
1604 return x
1608
1605
1609 def gennodelst(log):
1606 def gennodelst(log):
1610 for r in log:
1607 for r in log:
1611 if log.linkrev(r) in revset:
1608 if log.linkrev(r) in revset:
1612 yield log.node(r)
1609 yield log.node(r)
1613
1610
1614 def lookuplinkrev_func(revlog):
1611 def lookuplinkrev_func(revlog):
1615 def lookuplinkrev(n):
1612 def lookuplinkrev(n):
1616 return cl.node(revlog.linkrev(revlog.rev(n)))
1613 return cl.node(revlog.linkrev(revlog.rev(n)))
1617 return lookuplinkrev
1614 return lookuplinkrev
1618
1615
1619 def gengroup():
1616 def gengroup():
1620 '''yield a sequence of changegroup chunks (strings)'''
1617 '''yield a sequence of changegroup chunks (strings)'''
1621 # construct a list of all changed files
1618 # construct a list of all changed files
1622 changedfiles = set()
1619 changedfiles = set()
1623 mmfs = {}
1620 mmfs = {}
1624 collect = changegroup.collector(cl, mmfs, changedfiles)
1621 collect = changegroup.collector(cl, mmfs, changedfiles)
1625
1622
1626 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1623 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1627 # revlog.group yields three entries per node, so
1624 # revlog.group yields three entries per node, so
1628 # dividing by 3 gives an approximation of how many
1625 # dividing by 3 gives an approximation of how many
1629 # nodes have been processed.
1626 # nodes have been processed.
1630 self.ui.progress(_('bundling'), cnt / 3, unit=_('changesets'))
1627 self.ui.progress(_('bundling'), cnt / 3, unit=_('changesets'))
1631 yield chnk
1628 yield chnk
1632 changecount = cnt / 3
1629 changecount = cnt / 3
1633 self.ui.progress(_('bundling'), None)
1630 self.ui.progress(_('bundling'), None)
1634
1631
1635 mnfst = self.manifest
1632 mnfst = self.manifest
1636 nodeiter = gennodelst(mnfst)
1633 nodeiter = gennodelst(mnfst)
1637 efiles = {}
1634 efiles = {}
1638 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1635 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1639 lookuplinkrev_func(mnfst))):
1636 lookuplinkrev_func(mnfst))):
1640 if cnt % 3 == 1:
1637 if cnt % 3 == 1:
1641 mnode = chnk[:20]
1638 mnode = chnk[:20]
1642 efiles.update(mnfst.readdelta(mnode))
1639 efiles.update(mnfst.readdelta(mnode))
1643 # see above comment for why we divide by 3
1640 # see above comment for why we divide by 3
1644 self.ui.progress(_('bundling'), cnt / 3,
1641 self.ui.progress(_('bundling'), cnt / 3,
1645 unit=_('manifests'), total=changecount)
1642 unit=_('manifests'), total=changecount)
1646 yield chnk
1643 yield chnk
1647 efiles = len(efiles)
1644 efiles = len(efiles)
1648 self.ui.progress(_('bundling'), None)
1645 self.ui.progress(_('bundling'), None)
1649
1646
1650 for idx, fname in enumerate(sorted(changedfiles)):
1647 for idx, fname in enumerate(sorted(changedfiles)):
1651 filerevlog = self.file(fname)
1648 filerevlog = self.file(fname)
1652 if not len(filerevlog):
1649 if not len(filerevlog):
1653 raise util.Abort(_("empty or missing revlog for %s") % fname)
1650 raise util.Abort(_("empty or missing revlog for %s") % fname)
1654 nodeiter = gennodelst(filerevlog)
1651 nodeiter = gennodelst(filerevlog)
1655 nodeiter = list(nodeiter)
1652 nodeiter = list(nodeiter)
1656 if nodeiter:
1653 if nodeiter:
1657 yield changegroup.chunkheader(len(fname))
1654 yield changegroup.chunkheader(len(fname))
1658 yield fname
1655 yield fname
1659 lookup = lookuplinkrev_func(filerevlog)
1656 lookup = lookuplinkrev_func(filerevlog)
1660 for chnk in filerevlog.group(nodeiter, lookup):
1657 for chnk in filerevlog.group(nodeiter, lookup):
1661 self.ui.progress(
1658 self.ui.progress(
1662 _('bundling'), idx, item=fname,
1659 _('bundling'), idx, item=fname,
1663 total=efiles, unit=_('files'))
1660 total=efiles, unit=_('files'))
1664 yield chnk
1661 yield chnk
1665 self.ui.progress(_('bundling'), None)
1662 self.ui.progress(_('bundling'), None)
1666
1663
1667 yield changegroup.closechunk()
1664 yield changegroup.closechunk()
1668
1665
1669 if nodes:
1666 if nodes:
1670 self.hook('outgoing', node=hex(nodes[0]), source=source)
1667 self.hook('outgoing', node=hex(nodes[0]), source=source)
1671
1668
1672 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1669 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1673
1670
1674 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1671 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1675 """Add the changegroup returned by source.read() to this repo.
1672 """Add the changegroup returned by source.read() to this repo.
1676 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1673 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1677 the URL of the repo where this changegroup is coming from.
1674 the URL of the repo where this changegroup is coming from.
1678
1675
1679 Return an integer summarizing the change to this repo:
1676 Return an integer summarizing the change to this repo:
1680 - nothing changed or no source: 0
1677 - nothing changed or no source: 0
1681 - more heads than before: 1+added heads (2..n)
1678 - more heads than before: 1+added heads (2..n)
1682 - fewer heads than before: -1-removed heads (-2..-n)
1679 - fewer heads than before: -1-removed heads (-2..-n)
1683 - number of heads stays the same: 1
1680 - number of heads stays the same: 1
1684 """
1681 """
1685 def csmap(x):
1682 def csmap(x):
1686 self.ui.debug("add changeset %s\n" % short(x))
1683 self.ui.debug("add changeset %s\n" % short(x))
1687 return len(cl)
1684 return len(cl)
1688
1685
1689 def revmap(x):
1686 def revmap(x):
1690 return cl.rev(x)
1687 return cl.rev(x)
1691
1688
1692 if not source:
1689 if not source:
1693 return 0
1690 return 0
1694
1691
1695 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1692 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1696
1693
1697 changesets = files = revisions = 0
1694 changesets = files = revisions = 0
1698 efiles = set()
1695 efiles = set()
1699
1696
1700 # write changelog data to temp files so concurrent readers will not see
1697 # write changelog data to temp files so concurrent readers will not see
1701 # inconsistent view
1698 # inconsistent view
1702 cl = self.changelog
1699 cl = self.changelog
1703 cl.delayupdate()
1700 cl.delayupdate()
1704 oldheads = len(cl.heads())
1701 oldheads = len(cl.heads())
1705
1702
1706 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1703 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1707 try:
1704 try:
1708 trp = weakref.proxy(tr)
1705 trp = weakref.proxy(tr)
1709 # pull off the changeset group
1706 # pull off the changeset group
1710 self.ui.status(_("adding changesets\n"))
1707 self.ui.status(_("adding changesets\n"))
1711 clstart = len(cl)
1708 clstart = len(cl)
1712 class prog(object):
1709 class prog(object):
1713 step = _('changesets')
1710 step = _('changesets')
1714 count = 1
1711 count = 1
1715 ui = self.ui
1712 ui = self.ui
1716 total = None
1713 total = None
1717 def __call__(self):
1714 def __call__(self):
1718 self.ui.progress(self.step, self.count, unit=_('chunks'),
1715 self.ui.progress(self.step, self.count, unit=_('chunks'),
1719 total=self.total)
1716 total=self.total)
1720 self.count += 1
1717 self.count += 1
1721 pr = prog()
1718 pr = prog()
1722 source.callback = pr
1719 source.callback = pr
1723
1720
1724 if (cl.addgroup(source, csmap, trp) is None
1721 if (cl.addgroup(source, csmap, trp) is None
1725 and not emptyok):
1722 and not emptyok):
1726 raise util.Abort(_("received changelog group is empty"))
1723 raise util.Abort(_("received changelog group is empty"))
1727 clend = len(cl)
1724 clend = len(cl)
1728 changesets = clend - clstart
1725 changesets = clend - clstart
1729 for c in xrange(clstart, clend):
1726 for c in xrange(clstart, clend):
1730 efiles.update(self[c].files())
1727 efiles.update(self[c].files())
1731 efiles = len(efiles)
1728 efiles = len(efiles)
1732 self.ui.progress(_('changesets'), None)
1729 self.ui.progress(_('changesets'), None)
1733
1730
1734 # pull off the manifest group
1731 # pull off the manifest group
1735 self.ui.status(_("adding manifests\n"))
1732 self.ui.status(_("adding manifests\n"))
1736 pr.step = _('manifests')
1733 pr.step = _('manifests')
1737 pr.count = 1
1734 pr.count = 1
1738 pr.total = changesets # manifests <= changesets
1735 pr.total = changesets # manifests <= changesets
1739 # no need to check for empty manifest group here:
1736 # no need to check for empty manifest group here:
1740 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1737 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1741 # no new manifest will be created and the manifest group will
1738 # no new manifest will be created and the manifest group will
1742 # be empty during the pull
1739 # be empty during the pull
1743 self.manifest.addgroup(source, revmap, trp)
1740 self.manifest.addgroup(source, revmap, trp)
1744 self.ui.progress(_('manifests'), None)
1741 self.ui.progress(_('manifests'), None)
1745
1742
1746 needfiles = {}
1743 needfiles = {}
1747 if self.ui.configbool('server', 'validate', default=False):
1744 if self.ui.configbool('server', 'validate', default=False):
1748 # validate incoming csets have their manifests
1745 # validate incoming csets have their manifests
1749 for cset in xrange(clstart, clend):
1746 for cset in xrange(clstart, clend):
1750 mfest = self.changelog.read(self.changelog.node(cset))[0]
1747 mfest = self.changelog.read(self.changelog.node(cset))[0]
1751 mfest = self.manifest.readdelta(mfest)
1748 mfest = self.manifest.readdelta(mfest)
1752 # store file nodes we must see
1749 # store file nodes we must see
1753 for f, n in mfest.iteritems():
1750 for f, n in mfest.iteritems():
1754 needfiles.setdefault(f, set()).add(n)
1751 needfiles.setdefault(f, set()).add(n)
1755
1752
1756 # process the files
1753 # process the files
1757 self.ui.status(_("adding file changes\n"))
1754 self.ui.status(_("adding file changes\n"))
1758 pr.step = 'files'
1755 pr.step = 'files'
1759 pr.count = 1
1756 pr.count = 1
1760 pr.total = efiles
1757 pr.total = efiles
1761 source.callback = None
1758 source.callback = None
1762
1759
1763 while 1:
1760 while 1:
1764 f = source.chunk()
1761 f = source.chunk()
1765 if not f:
1762 if not f:
1766 break
1763 break
1767 self.ui.debug("adding %s revisions\n" % f)
1764 self.ui.debug("adding %s revisions\n" % f)
1768 pr()
1765 pr()
1769 fl = self.file(f)
1766 fl = self.file(f)
1770 o = len(fl)
1767 o = len(fl)
1771 if fl.addgroup(source, revmap, trp) is None:
1768 if fl.addgroup(source, revmap, trp) is None:
1772 raise util.Abort(_("received file revlog group is empty"))
1769 raise util.Abort(_("received file revlog group is empty"))
1773 revisions += len(fl) - o
1770 revisions += len(fl) - o
1774 files += 1
1771 files += 1
1775 if f in needfiles:
1772 if f in needfiles:
1776 needs = needfiles[f]
1773 needs = needfiles[f]
1777 for new in xrange(o, len(fl)):
1774 for new in xrange(o, len(fl)):
1778 n = fl.node(new)
1775 n = fl.node(new)
1779 if n in needs:
1776 if n in needs:
1780 needs.remove(n)
1777 needs.remove(n)
1781 if not needs:
1778 if not needs:
1782 del needfiles[f]
1779 del needfiles[f]
1783 self.ui.progress(_('files'), None)
1780 self.ui.progress(_('files'), None)
1784
1781
1785 for f, needs in needfiles.iteritems():
1782 for f, needs in needfiles.iteritems():
1786 fl = self.file(f)
1783 fl = self.file(f)
1787 for n in needs:
1784 for n in needs:
1788 try:
1785 try:
1789 fl.rev(n)
1786 fl.rev(n)
1790 except error.LookupError:
1787 except error.LookupError:
1791 raise util.Abort(
1788 raise util.Abort(
1792 _('missing file data for %s:%s - run hg verify') %
1789 _('missing file data for %s:%s - run hg verify') %
1793 (f, hex(n)))
1790 (f, hex(n)))
1794
1791
1795 newheads = len(cl.heads())
1792 newheads = len(cl.heads())
1796 heads = ""
1793 heads = ""
1797 if oldheads and newheads != oldheads:
1794 if oldheads and newheads != oldheads:
1798 heads = _(" (%+d heads)") % (newheads - oldheads)
1795 heads = _(" (%+d heads)") % (newheads - oldheads)
1799
1796
1800 self.ui.status(_("added %d changesets"
1797 self.ui.status(_("added %d changesets"
1801 " with %d changes to %d files%s\n")
1798 " with %d changes to %d files%s\n")
1802 % (changesets, revisions, files, heads))
1799 % (changesets, revisions, files, heads))
1803
1800
1804 if changesets > 0:
1801 if changesets > 0:
1805 p = lambda: cl.writepending() and self.root or ""
1802 p = lambda: cl.writepending() and self.root or ""
1806 self.hook('pretxnchangegroup', throw=True,
1803 self.hook('pretxnchangegroup', throw=True,
1807 node=hex(cl.node(clstart)), source=srctype,
1804 node=hex(cl.node(clstart)), source=srctype,
1808 url=url, pending=p)
1805 url=url, pending=p)
1809
1806
1810 # make changelog see real files again
1807 # make changelog see real files again
1811 cl.finalize(trp)
1808 cl.finalize(trp)
1812
1809
1813 tr.close()
1810 tr.close()
1814 finally:
1811 finally:
1815 tr.release()
1812 tr.release()
1816 if lock:
1813 if lock:
1817 lock.release()
1814 lock.release()
1818
1815
1819 if changesets > 0:
1816 if changesets > 0:
1820 # forcefully update the on-disk branch cache
1817 # forcefully update the on-disk branch cache
1821 self.ui.debug("updating the branch cache\n")
1818 self.ui.debug("updating the branch cache\n")
1822 self.updatebranchcache()
1819 self.updatebranchcache()
1823 self.hook("changegroup", node=hex(cl.node(clstart)),
1820 self.hook("changegroup", node=hex(cl.node(clstart)),
1824 source=srctype, url=url)
1821 source=srctype, url=url)
1825
1822
1826 for i in xrange(clstart, clend):
1823 for i in xrange(clstart, clend):
1827 self.hook("incoming", node=hex(cl.node(i)),
1824 self.hook("incoming", node=hex(cl.node(i)),
1828 source=srctype, url=url)
1825 source=srctype, url=url)
1829
1826
1830 # never return 0 here:
1827 # never return 0 here:
1831 if newheads < oldheads:
1828 if newheads < oldheads:
1832 return newheads - oldheads - 1
1829 return newheads - oldheads - 1
1833 else:
1830 else:
1834 return newheads - oldheads + 1
1831 return newheads - oldheads + 1
1835
1832
1836
1833
1837 def stream_in(self, remote, requirements):
1834 def stream_in(self, remote, requirements):
1838 fp = remote.stream_out()
1835 fp = remote.stream_out()
1839 l = fp.readline()
1836 l = fp.readline()
1840 try:
1837 try:
1841 resp = int(l)
1838 resp = int(l)
1842 except ValueError:
1839 except ValueError:
1843 raise error.ResponseError(
1840 raise error.ResponseError(
1844 _('Unexpected response from remote server:'), l)
1841 _('Unexpected response from remote server:'), l)
1845 if resp == 1:
1842 if resp == 1:
1846 raise util.Abort(_('operation forbidden by server'))
1843 raise util.Abort(_('operation forbidden by server'))
1847 elif resp == 2:
1844 elif resp == 2:
1848 raise util.Abort(_('locking the remote repository failed'))
1845 raise util.Abort(_('locking the remote repository failed'))
1849 elif resp != 0:
1846 elif resp != 0:
1850 raise util.Abort(_('the server sent an unknown error code'))
1847 raise util.Abort(_('the server sent an unknown error code'))
1851 self.ui.status(_('streaming all changes\n'))
1848 self.ui.status(_('streaming all changes\n'))
1852 l = fp.readline()
1849 l = fp.readline()
1853 try:
1850 try:
1854 total_files, total_bytes = map(int, l.split(' ', 1))
1851 total_files, total_bytes = map(int, l.split(' ', 1))
1855 except (ValueError, TypeError):
1852 except (ValueError, TypeError):
1856 raise error.ResponseError(
1853 raise error.ResponseError(
1857 _('Unexpected response from remote server:'), l)
1854 _('Unexpected response from remote server:'), l)
1858 self.ui.status(_('%d files to transfer, %s of data\n') %
1855 self.ui.status(_('%d files to transfer, %s of data\n') %
1859 (total_files, util.bytecount(total_bytes)))
1856 (total_files, util.bytecount(total_bytes)))
1860 start = time.time()
1857 start = time.time()
1861 for i in xrange(total_files):
1858 for i in xrange(total_files):
1862 # XXX doesn't support '\n' or '\r' in filenames
1859 # XXX doesn't support '\n' or '\r' in filenames
1863 l = fp.readline()
1860 l = fp.readline()
1864 try:
1861 try:
1865 name, size = l.split('\0', 1)
1862 name, size = l.split('\0', 1)
1866 size = int(size)
1863 size = int(size)
1867 except (ValueError, TypeError):
1864 except (ValueError, TypeError):
1868 raise error.ResponseError(
1865 raise error.ResponseError(
1869 _('Unexpected response from remote server:'), l)
1866 _('Unexpected response from remote server:'), l)
1870 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1867 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1871 # for backwards compat, name was partially encoded
1868 # for backwards compat, name was partially encoded
1872 ofp = self.sopener(store.decodedir(name), 'w')
1869 ofp = self.sopener(store.decodedir(name), 'w')
1873 for chunk in util.filechunkiter(fp, limit=size):
1870 for chunk in util.filechunkiter(fp, limit=size):
1874 ofp.write(chunk)
1871 ofp.write(chunk)
1875 ofp.close()
1872 ofp.close()
1876 elapsed = time.time() - start
1873 elapsed = time.time() - start
1877 if elapsed <= 0:
1874 if elapsed <= 0:
1878 elapsed = 0.001
1875 elapsed = 0.001
1879 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1876 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1880 (util.bytecount(total_bytes), elapsed,
1877 (util.bytecount(total_bytes), elapsed,
1881 util.bytecount(total_bytes / elapsed)))
1878 util.bytecount(total_bytes / elapsed)))
1882
1879
1883 # new requirements = old non-format requirements + new format-related
1880 # new requirements = old non-format requirements + new format-related
1884 # requirements from the streamed-in repository
1881 # requirements from the streamed-in repository
1885 requirements.update(set(self.requirements) - self.supportedformats)
1882 requirements.update(set(self.requirements) - self.supportedformats)
1886 self._applyrequirements(requirements)
1883 self._applyrequirements(requirements)
1887 self._writerequirements()
1884 self._writerequirements()
1888
1885
1889 self.invalidate()
1886 self.invalidate()
1890 return len(self.heads()) + 1
1887 return len(self.heads()) + 1
1891
1888
1892 def clone(self, remote, heads=[], stream=False):
1889 def clone(self, remote, heads=[], stream=False):
1893 '''clone remote repository.
1890 '''clone remote repository.
1894
1891
1895 keyword arguments:
1892 keyword arguments:
1896 heads: list of revs to clone (forces use of pull)
1893 heads: list of revs to clone (forces use of pull)
1897 stream: use streaming clone if possible'''
1894 stream: use streaming clone if possible'''
1898
1895
1899 # now, all clients that can request uncompressed clones can
1896 # now, all clients that can request uncompressed clones can
1900 # read repo formats supported by all servers that can serve
1897 # read repo formats supported by all servers that can serve
1901 # them.
1898 # them.
1902
1899
1903 # if revlog format changes, client will have to check version
1900 # if revlog format changes, client will have to check version
1904 # and format flags on "stream" capability, and use
1901 # and format flags on "stream" capability, and use
1905 # uncompressed only if compatible.
1902 # uncompressed only if compatible.
1906
1903
1907 if stream and not heads:
1904 if stream and not heads:
1908 # 'stream' means remote revlog format is revlogv1 only
1905 # 'stream' means remote revlog format is revlogv1 only
1909 if remote.capable('stream'):
1906 if remote.capable('stream'):
1910 return self.stream_in(remote, set(('revlogv1',)))
1907 return self.stream_in(remote, set(('revlogv1',)))
1911 # otherwise, 'streamreqs' contains the remote revlog format
1908 # otherwise, 'streamreqs' contains the remote revlog format
1912 streamreqs = remote.capable('streamreqs')
1909 streamreqs = remote.capable('streamreqs')
1913 if streamreqs:
1910 if streamreqs:
1914 streamreqs = set(streamreqs.split(','))
1911 streamreqs = set(streamreqs.split(','))
1915 # if we support it, stream in and adjust our requirements
1912 # if we support it, stream in and adjust our requirements
1916 if not streamreqs - self.supportedformats:
1913 if not streamreqs - self.supportedformats:
1917 return self.stream_in(remote, streamreqs)
1914 return self.stream_in(remote, streamreqs)
1918 return self.pull(remote, heads)
1915 return self.pull(remote, heads)
1919
1916
1920 def pushkey(self, namespace, key, old, new):
1917 def pushkey(self, namespace, key, old, new):
1921 return pushkey.push(self, namespace, key, old, new)
1918 return pushkey.push(self, namespace, key, old, new)
1922
1919
1923 def listkeys(self, namespace):
1920 def listkeys(self, namespace):
1924 return pushkey.list(self, namespace)
1921 return pushkey.list(self, namespace)
1925
1922
1926 # used to avoid circular references so destructors work
1923 # used to avoid circular references so destructors work
1927 def aftertrans(files):
1924 def aftertrans(files):
1928 renamefiles = [tuple(t) for t in files]
1925 renamefiles = [tuple(t) for t in files]
1929 def a():
1926 def a():
1930 for src, dest in renamefiles:
1927 for src, dest in renamefiles:
1931 util.rename(src, dest)
1928 util.rename(src, dest)
1932 return a
1929 return a
1933
1930
1934 def instance(ui, path, create):
1931 def instance(ui, path, create):
1935 return localrepository(ui, util.drop_scheme('file', path), create)
1932 return localrepository(ui, util.drop_scheme('file', path), create)
1936
1933
1937 def islocal(path):
1934 def islocal(path):
1938 return True
1935 return True
@@ -1,90 +1,90 b''
1 # parsers.py - Python implementation of parsers.c
1 # parsers.py - Python implementation of parsers.c
2 #
2 #
3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from mercurial.node import bin, nullid, nullrev
8 from mercurial.node import bin, nullid, nullrev
9 from mercurial import util
9 from mercurial import util
10 import struct, zlib
10 import struct, zlib
11
11
12 _pack = struct.pack
12 _pack = struct.pack
13 _unpack = struct.unpack
13 _unpack = struct.unpack
14 _compress = zlib.compress
14 _compress = zlib.compress
15 _decompress = zlib.decompress
15 _decompress = zlib.decompress
16 _sha = util.sha1
16 _sha = util.sha1
17
17
18 def parse_manifest(mfdict, fdict, lines):
18 def parse_manifest(mfdict, fdict, lines):
19 for l in lines.splitlines():
19 for l in lines.splitlines():
20 f, n = l.split('\0')
20 f, n = l.split('\0')
21 if len(n) > 40:
21 if len(n) > 40:
22 fdict[f] = n[40:]
22 fdict[f] = n[40:]
23 mfdict[f] = bin(n[:40])
23 mfdict[f] = bin(n[:40])
24 else:
24 else:
25 mfdict[f] = bin(n)
25 mfdict[f] = bin(n)
26
26
27 def parse_index(data, inline):
27 def parse_index(data, inline):
28 def gettype(q):
28 def gettype(q):
29 return int(q & 0xFFFF)
29 return int(q & 0xFFFF)
30
30
31 def offset_type(offset, type):
31 def offset_type(offset, type):
32 return long(long(offset) << 16 | type)
32 return long(long(offset) << 16 | type)
33
33
34 indexformatng = ">Qiiiiii20s12x"
34 indexformatng = ">Qiiiiii20s12x"
35
35
36 s = struct.calcsize(indexformatng)
36 s = struct.calcsize(indexformatng)
37 index = []
37 index = []
38 cache = None
38 cache = None
39 nodemap = {nullid: nullrev}
39 nodemap = {nullid: nullrev}
40 n = off = 0
40 n = off = 0
41 # if we're not using lazymap, always read the whole index
41
42 l = len(data) - s
42 l = len(data) - s
43 append = index.append
43 append = index.append
44 if inline:
44 if inline:
45 cache = (0, data)
45 cache = (0, data)
46 while off <= l:
46 while off <= l:
47 e = _unpack(indexformatng, data[off:off + s])
47 e = _unpack(indexformatng, data[off:off + s])
48 nodemap[e[7]] = n
48 nodemap[e[7]] = n
49 append(e)
49 append(e)
50 n += 1
50 n += 1
51 if e[1] < 0:
51 if e[1] < 0:
52 break
52 break
53 off += e[1] + s
53 off += e[1] + s
54 else:
54 else:
55 while off <= l:
55 while off <= l:
56 e = _unpack(indexformatng, data[off:off + s])
56 e = _unpack(indexformatng, data[off:off + s])
57 nodemap[e[7]] = n
57 nodemap[e[7]] = n
58 append(e)
58 append(e)
59 n += 1
59 n += 1
60 off += s
60 off += s
61
61
62 e = list(index[0])
62 e = list(index[0])
63 type = gettype(e[0])
63 type = gettype(e[0])
64 e[0] = offset_type(0, type)
64 e[0] = offset_type(0, type)
65 index[0] = tuple(e)
65 index[0] = tuple(e)
66
66
67 # add the magic null revision at -1
67 # add the magic null revision at -1
68 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
68 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
69
69
70 return index, nodemap, cache
70 return index, nodemap, cache
71
71
72 def parse_dirstate(dmap, copymap, st):
72 def parse_dirstate(dmap, copymap, st):
73 parents = [st[:20], st[20: 40]]
73 parents = [st[:20], st[20: 40]]
74 # deref fields so they will be local in loop
74 # deref fields so they will be local in loop
75 format = ">cllll"
75 format = ">cllll"
76 e_size = struct.calcsize(format)
76 e_size = struct.calcsize(format)
77 pos1 = 40
77 pos1 = 40
78 l = len(st)
78 l = len(st)
79
79
80 # the inner loop
80 # the inner loop
81 while pos1 < l:
81 while pos1 < l:
82 pos2 = pos1 + e_size
82 pos2 = pos1 + e_size
83 e = _unpack(">cllll", st[pos1:pos2]) # a literal here is faster
83 e = _unpack(">cllll", st[pos1:pos2]) # a literal here is faster
84 pos1 = pos2 + e[4]
84 pos1 = pos2 + e[4]
85 f = st[pos2:pos1]
85 f = st[pos2:pos1]
86 if '\0' in f:
86 if '\0' in f:
87 f, c = f.split('\0')
87 f, c = f.split('\0')
88 copymap[f] = c
88 copymap[f] = c
89 dmap[f] = e[:4]
89 dmap[f] = e[:4]
90 return parents
90 return parents
@@ -1,1482 +1,1235 b''
1 # revlog.py - storage back-end for mercurial
1 # revlog.py - storage back-end for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """Storage back-end for Mercurial.
8 """Storage back-end for Mercurial.
9
9
10 This provides efficient delta storage with O(1) retrieve and append
10 This provides efficient delta storage with O(1) retrieve and append
11 and O(changes) merge between branches.
11 and O(changes) merge between branches.
12 """
12 """
13
13
14 # import stuff from node for others to import from revlog
14 # import stuff from node for others to import from revlog
15 from node import bin, hex, nullid, nullrev, short #@UnusedImport
15 from node import bin, hex, nullid, nullrev, short #@UnusedImport
16 from i18n import _
16 from i18n import _
17 import changegroup, ancestor, mdiff, parsers, error, util
17 import changegroup, ancestor, mdiff, parsers, error, util
18 import struct, zlib, errno
18 import struct, zlib, errno
19
19
20 _pack = struct.pack
20 _pack = struct.pack
21 _unpack = struct.unpack
21 _unpack = struct.unpack
22 _compress = zlib.compress
22 _compress = zlib.compress
23 _decompress = zlib.decompress
23 _decompress = zlib.decompress
24 _sha = util.sha1
24 _sha = util.sha1
25
25
26 # revlog header flags
26 # revlog header flags
27 REVLOGV0 = 0
27 REVLOGV0 = 0
28 REVLOGNG = 1
28 REVLOGNG = 1
29 REVLOGNGINLINEDATA = (1 << 16)
29 REVLOGNGINLINEDATA = (1 << 16)
30 REVLOGSHALLOW = (1 << 17)
30 REVLOGSHALLOW = (1 << 17)
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGSHALLOW
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGSHALLOW
35
35
36 # revlog index flags
36 # revlog index flags
37 REVIDX_PARENTDELTA = 1
37 REVIDX_PARENTDELTA = 1
38 REVIDX_PUNCHED_FLAG = 2
38 REVIDX_PUNCHED_FLAG = 2
39 REVIDX_KNOWN_FLAGS = REVIDX_PUNCHED_FLAG | REVIDX_PARENTDELTA
39 REVIDX_KNOWN_FLAGS = REVIDX_PUNCHED_FLAG | REVIDX_PARENTDELTA
40
40
41 # amount of data read unconditionally, should be >= 4
42 # when not inline: threshold for using lazy index
43 _prereadsize = 1048576
44 # max size of revlog with inline data
41 # max size of revlog with inline data
45 _maxinline = 131072
42 _maxinline = 131072
43 _chunksize = 1048576
46
44
47 RevlogError = error.RevlogError
45 RevlogError = error.RevlogError
48 LookupError = error.LookupError
46 LookupError = error.LookupError
49
47
50 def getoffset(q):
48 def getoffset(q):
51 return int(q >> 16)
49 return int(q >> 16)
52
50
53 def gettype(q):
51 def gettype(q):
54 return int(q & 0xFFFF)
52 return int(q & 0xFFFF)
55
53
56 def offset_type(offset, type):
54 def offset_type(offset, type):
57 return long(long(offset) << 16 | type)
55 return long(long(offset) << 16 | type)
58
56
59 nullhash = _sha(nullid)
57 nullhash = _sha(nullid)
60
58
61 def hash(text, p1, p2):
59 def hash(text, p1, p2):
62 """generate a hash from the given text and its parent hashes
60 """generate a hash from the given text and its parent hashes
63
61
64 This hash combines both the current file contents and its history
62 This hash combines both the current file contents and its history
65 in a manner that makes it easy to distinguish nodes with the same
63 in a manner that makes it easy to distinguish nodes with the same
66 content in the revision graph.
64 content in the revision graph.
67 """
65 """
68 # As of now, if one of the parent node is null, p2 is null
66 # As of now, if one of the parent node is null, p2 is null
69 if p2 == nullid:
67 if p2 == nullid:
70 # deep copy of a hash is faster than creating one
68 # deep copy of a hash is faster than creating one
71 s = nullhash.copy()
69 s = nullhash.copy()
72 s.update(p1)
70 s.update(p1)
73 else:
71 else:
74 # none of the parent nodes are nullid
72 # none of the parent nodes are nullid
75 l = [p1, p2]
73 l = [p1, p2]
76 l.sort()
74 l.sort()
77 s = _sha(l[0])
75 s = _sha(l[0])
78 s.update(l[1])
76 s.update(l[1])
79 s.update(text)
77 s.update(text)
80 return s.digest()
78 return s.digest()
81
79
82 def compress(text):
80 def compress(text):
83 """ generate a possibly-compressed representation of text """
81 """ generate a possibly-compressed representation of text """
84 if not text:
82 if not text:
85 return ("", text)
83 return ("", text)
86 l = len(text)
84 l = len(text)
87 bin = None
85 bin = None
88 if l < 44:
86 if l < 44:
89 pass
87 pass
90 elif l > 1000000:
88 elif l > 1000000:
91 # zlib makes an internal copy, thus doubling memory usage for
89 # zlib makes an internal copy, thus doubling memory usage for
92 # large files, so lets do this in pieces
90 # large files, so lets do this in pieces
93 z = zlib.compressobj()
91 z = zlib.compressobj()
94 p = []
92 p = []
95 pos = 0
93 pos = 0
96 while pos < l:
94 while pos < l:
97 pos2 = pos + 2**20
95 pos2 = pos + 2**20
98 p.append(z.compress(text[pos:pos2]))
96 p.append(z.compress(text[pos:pos2]))
99 pos = pos2
97 pos = pos2
100 p.append(z.flush())
98 p.append(z.flush())
101 if sum(map(len, p)) < l:
99 if sum(map(len, p)) < l:
102 bin = "".join(p)
100 bin = "".join(p)
103 else:
101 else:
104 bin = _compress(text)
102 bin = _compress(text)
105 if bin is None or len(bin) > l:
103 if bin is None or len(bin) > l:
106 if text[0] == '\0':
104 if text[0] == '\0':
107 return ("", text)
105 return ("", text)
108 return ('u', text)
106 return ('u', text)
109 return ("", bin)
107 return ("", bin)
110
108
111 def decompress(bin):
109 def decompress(bin):
112 """ decompress the given input """
110 """ decompress the given input """
113 if not bin:
111 if not bin:
114 return bin
112 return bin
115 t = bin[0]
113 t = bin[0]
116 if t == '\0':
114 if t == '\0':
117 return bin
115 return bin
118 if t == 'x':
116 if t == 'x':
119 return _decompress(bin)
117 return _decompress(bin)
120 if t == 'u':
118 if t == 'u':
121 return bin[1:]
119 return bin[1:]
122 raise RevlogError(_("unknown compression type %r") % t)
120 raise RevlogError(_("unknown compression type %r") % t)
123
121
124 class lazyparser(object):
125 """
126 this class avoids the need to parse the entirety of large indices
127 """
128
129 # lazyparser is not safe to use on windows if win32 extensions not
130 # available. it keeps file handle open, which make it not possible
131 # to break hardlinks on local cloned repos.
132
133 def __init__(self, dataf):
134 try:
135 size = util.fstat(dataf).st_size
136 except AttributeError:
137 size = 0
138 self.dataf = dataf
139 self.s = struct.calcsize(indexformatng)
140 self.datasize = size
141 self.l = size // self.s
142 self.index = [None] * self.l
143 self.map = {nullid: nullrev}
144 self.allmap = 0
145 self.all = 0
146 self.mapfind_count = 0
147
148 def loadmap(self):
149 """
150 during a commit, we need to make sure the rev being added is
151 not a duplicate. This requires loading the entire index,
152 which is fairly slow. loadmap can load up just the node map,
153 which takes much less time.
154 """
155 if self.allmap:
156 return
157 end = self.datasize
158 self.allmap = 1
159 cur = 0
160 count = 0
161 blocksize = self.s * 256
162 self.dataf.seek(0)
163 while cur < end:
164 data = self.dataf.read(blocksize)
165 off = 0
166 for x in xrange(256):
167 n = data[off + ngshaoffset:off + ngshaoffset + 20]
168 self.map[n] = count
169 count += 1
170 if count >= self.l:
171 break
172 off += self.s
173 cur += blocksize
174
175 def loadblock(self, blockstart, blocksize, data=None):
176 if self.all:
177 return
178 if data is None:
179 self.dataf.seek(blockstart)
180 if blockstart + blocksize > self.datasize:
181 # the revlog may have grown since we've started running,
182 # but we don't have space in self.index for more entries.
183 # limit blocksize so that we don't get too much data.
184 blocksize = max(self.datasize - blockstart, 0)
185 data = self.dataf.read(blocksize)
186 lend = len(data) // self.s
187 i = blockstart // self.s
188 off = 0
189 # lazyindex supports __delitem__
190 if lend > len(self.index) - i:
191 lend = len(self.index) - i
192 for x in xrange(lend):
193 if self.index[i + x] is None:
194 b = data[off : off + self.s]
195 self.index[i + x] = b
196 n = b[ngshaoffset:ngshaoffset + 20]
197 self.map[n] = i + x
198 off += self.s
199
200 def findnode(self, node):
201 """search backwards through the index file for a specific node"""
202 if self.allmap:
203 return None
204
205 # hg log will cause many many searches for the manifest
206 # nodes. After we get called a few times, just load the whole
207 # thing.
208 if self.mapfind_count > 8:
209 self.loadmap()
210 if node in self.map:
211 return node
212 return None
213 self.mapfind_count += 1
214 last = self.l - 1
215 while self.index[last] is not None:
216 if last == 0:
217 self.all = 1
218 self.allmap = 1
219 return None
220 last -= 1
221 end = (last + 1) * self.s
222 blocksize = self.s * 256
223 while end >= 0:
224 start = max(end - blocksize, 0)
225 self.dataf.seek(start)
226 data = self.dataf.read(end - start)
227 findend = end - start
228 while True:
229 # we're searching backwards, so we have to make sure
230 # we don't find a changeset where this node is a parent
231 off = data.find(node, 0, findend)
232 findend = off
233 if off >= 0:
234 i = off / self.s
235 off = i * self.s
236 n = data[off + ngshaoffset:off + ngshaoffset + 20]
237 if n == node:
238 self.map[n] = i + start / self.s
239 return node
240 else:
241 break
242 end -= blocksize
243 return None
244
245 def loadindex(self, i=None, end=None):
246 if self.all:
247 return
248 all = False
249 if i is None:
250 blockstart = 0
251 blocksize = (65536 / self.s) * self.s
252 end = self.datasize
253 all = True
254 else:
255 if end:
256 blockstart = i * self.s
257 end = end * self.s
258 blocksize = end - blockstart
259 else:
260 blockstart = (i & ~1023) * self.s
261 blocksize = self.s * 1024
262 end = blockstart + blocksize
263 while blockstart < end:
264 self.loadblock(blockstart, blocksize)
265 blockstart += blocksize
266 if all:
267 self.all = True
268
269 class lazyindex(object):
270 """a lazy version of the index array"""
271 def __init__(self, parser):
272 self.p = parser
273 def __len__(self):
274 return len(self.p.index)
275 def load(self, pos):
276 if pos < 0:
277 pos += len(self.p.index)
278 self.p.loadindex(pos)
279 return self.p.index[pos]
280 def __getitem__(self, pos):
281 return _unpack(indexformatng, self.p.index[pos] or self.load(pos))
282 def __setitem__(self, pos, item):
283 self.p.index[pos] = _pack(indexformatng, *item)
284 def __delitem__(self, pos):
285 del self.p.index[pos]
286 def insert(self, pos, e):
287 self.p.index.insert(pos, _pack(indexformatng, *e))
288 def append(self, e):
289 self.p.index.append(_pack(indexformatng, *e))
290
291 class lazymap(object):
292 """a lazy version of the node map"""
293 def __init__(self, parser):
294 self.p = parser
295 def load(self, key):
296 n = self.p.findnode(key)
297 if n is None:
298 raise KeyError(key)
299 def __contains__(self, key):
300 if key in self.p.map:
301 return True
302 self.p.loadmap()
303 return key in self.p.map
304 def __iter__(self):
305 yield nullid
306 for i, ret in enumerate(self.p.index):
307 if not ret:
308 self.p.loadindex(i)
309 ret = self.p.index[i]
310 if isinstance(ret, str):
311 ret = _unpack(indexformatng, ret)
312 yield ret[7]
313 def __getitem__(self, key):
314 try:
315 return self.p.map[key]
316 except KeyError:
317 try:
318 self.load(key)
319 return self.p.map[key]
320 except KeyError:
321 raise KeyError("node " + hex(key))
322 def __setitem__(self, key, val):
323 self.p.map[key] = val
324 def __delitem__(self, key):
325 del self.p.map[key]
326
327 indexformatv0 = ">4l20s20s20s"
122 indexformatv0 = ">4l20s20s20s"
328 v0shaoffset = 56
123 v0shaoffset = 56
329
124
330 class revlogoldio(object):
125 class revlogoldio(object):
331 def __init__(self):
126 def __init__(self):
332 self.size = struct.calcsize(indexformatv0)
127 self.size = struct.calcsize(indexformatv0)
333
128
334 def parseindex(self, fp, data, inline):
129 def parseindex(self, fp, data, inline):
335 s = self.size
130 s = self.size
336 index = []
131 index = []
337 nodemap = {nullid: nullrev}
132 nodemap = {nullid: nullrev}
338 n = off = 0
133 n = off = 0
339 if len(data) == _prereadsize:
340 data += fp.read() # read the rest
341 l = len(data)
134 l = len(data)
342 while off + s <= l:
135 while off + s <= l:
343 cur = data[off:off + s]
136 cur = data[off:off + s]
344 off += s
137 off += s
345 e = _unpack(indexformatv0, cur)
138 e = _unpack(indexformatv0, cur)
346 # transform to revlogv1 format
139 # transform to revlogv1 format
347 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
140 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
348 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
141 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
349 index.append(e2)
142 index.append(e2)
350 nodemap[e[6]] = n
143 nodemap[e[6]] = n
351 n += 1
144 n += 1
352
145
353 return index, nodemap, None
146 return index, nodemap, None
354
147
355 def packentry(self, entry, node, version, rev):
148 def packentry(self, entry, node, version, rev):
356 if gettype(entry[0]):
149 if gettype(entry[0]):
357 raise RevlogError(_("index entry flags need RevlogNG"))
150 raise RevlogError(_("index entry flags need RevlogNG"))
358 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
151 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
359 node(entry[5]), node(entry[6]), entry[7])
152 node(entry[5]), node(entry[6]), entry[7])
360 return _pack(indexformatv0, *e2)
153 return _pack(indexformatv0, *e2)
361
154
362 # index ng:
155 # index ng:
363 # 6 bytes: offset
156 # 6 bytes: offset
364 # 2 bytes: flags
157 # 2 bytes: flags
365 # 4 bytes: compressed length
158 # 4 bytes: compressed length
366 # 4 bytes: uncompressed length
159 # 4 bytes: uncompressed length
367 # 4 bytes: base rev
160 # 4 bytes: base rev
368 # 4 bytes: link rev
161 # 4 bytes: link rev
369 # 4 bytes: parent 1 rev
162 # 4 bytes: parent 1 rev
370 # 4 bytes: parent 2 rev
163 # 4 bytes: parent 2 rev
371 # 32 bytes: nodeid
164 # 32 bytes: nodeid
372 indexformatng = ">Qiiiiii20s12x"
165 indexformatng = ">Qiiiiii20s12x"
373 ngshaoffset = 32
166 ngshaoffset = 32
374 versionformat = ">I"
167 versionformat = ">I"
375
168
376 class revlogio(object):
169 class revlogio(object):
377 def __init__(self):
170 def __init__(self):
378 self.size = struct.calcsize(indexformatng)
171 self.size = struct.calcsize(indexformatng)
379
172
380 def parseindex(self, fp, data, inline):
173 def parseindex(self, fp, data, inline):
381 if len(data) == _prereadsize:
382 if util.openhardlinks() and not inline:
383 # big index, let's parse it on demand
384 parser = lazyparser(fp)
385 index = lazyindex(parser)
386 nodemap = lazymap(parser)
387 e = list(index[0])
388 type = gettype(e[0])
389 e[0] = offset_type(0, type)
390 index[0] = e
391 return index, nodemap, None
392 else:
393 data += fp.read()
394
395 # call the C implementation to parse the index data
174 # call the C implementation to parse the index data
396 index, nodemap, cache = parsers.parse_index(data, inline)
175 index, nodemap, cache = parsers.parse_index(data, inline)
397 return index, nodemap, cache
176 return index, nodemap, cache
398
177
399 def packentry(self, entry, node, version, rev):
178 def packentry(self, entry, node, version, rev):
400 p = _pack(indexformatng, *entry)
179 p = _pack(indexformatng, *entry)
401 if rev == 0:
180 if rev == 0:
402 p = _pack(versionformat, version) + p[4:]
181 p = _pack(versionformat, version) + p[4:]
403 return p
182 return p
404
183
405 class revlog(object):
184 class revlog(object):
406 """
185 """
407 the underlying revision storage object
186 the underlying revision storage object
408
187
409 A revlog consists of two parts, an index and the revision data.
188 A revlog consists of two parts, an index and the revision data.
410
189
411 The index is a file with a fixed record size containing
190 The index is a file with a fixed record size containing
412 information on each revision, including its nodeid (hash), the
191 information on each revision, including its nodeid (hash), the
413 nodeids of its parents, the position and offset of its data within
192 nodeids of its parents, the position and offset of its data within
414 the data file, and the revision it's based on. Finally, each entry
193 the data file, and the revision it's based on. Finally, each entry
415 contains a linkrev entry that can serve as a pointer to external
194 contains a linkrev entry that can serve as a pointer to external
416 data.
195 data.
417
196
418 The revision data itself is a linear collection of data chunks.
197 The revision data itself is a linear collection of data chunks.
419 Each chunk represents a revision and is usually represented as a
198 Each chunk represents a revision and is usually represented as a
420 delta against the previous chunk. To bound lookup time, runs of
199 delta against the previous chunk. To bound lookup time, runs of
421 deltas are limited to about 2 times the length of the original
200 deltas are limited to about 2 times the length of the original
422 version data. This makes retrieval of a version proportional to
201 version data. This makes retrieval of a version proportional to
423 its size, or O(1) relative to the number of revisions.
202 its size, or O(1) relative to the number of revisions.
424
203
425 Both pieces of the revlog are written to in an append-only
204 Both pieces of the revlog are written to in an append-only
426 fashion, which means we never need to rewrite a file to insert or
205 fashion, which means we never need to rewrite a file to insert or
427 remove data, and can use some simple techniques to avoid the need
206 remove data, and can use some simple techniques to avoid the need
428 for locking while reading.
207 for locking while reading.
429 """
208 """
430 def __init__(self, opener, indexfile, shallowroot=None):
209 def __init__(self, opener, indexfile, shallowroot=None):
431 """
210 """
432 create a revlog object
211 create a revlog object
433
212
434 opener is a function that abstracts the file opening operation
213 opener is a function that abstracts the file opening operation
435 and can be used to implement COW semantics or the like.
214 and can be used to implement COW semantics or the like.
436 """
215 """
437 self.indexfile = indexfile
216 self.indexfile = indexfile
438 self.datafile = indexfile[:-2] + ".d"
217 self.datafile = indexfile[:-2] + ".d"
439 self.opener = opener
218 self.opener = opener
440 self._cache = None
219 self._cache = None
441 self._chunkcache = (0, '')
220 self._chunkcache = (0, '')
442 self.nodemap = {nullid: nullrev}
221 self.nodemap = {nullid: nullrev}
443 self.index = []
222 self.index = []
444 self._shallowroot = shallowroot
223 self._shallowroot = shallowroot
445 self._parentdelta = 0
224 self._parentdelta = 0
446
225
447 v = REVLOG_DEFAULT_VERSION
226 v = REVLOG_DEFAULT_VERSION
448 if hasattr(opener, 'options') and 'defversion' in opener.options:
227 if hasattr(opener, 'options') and 'defversion' in opener.options:
449 v = opener.options['defversion']
228 v = opener.options['defversion']
450 if v & REVLOGNG:
229 if v & REVLOGNG:
451 v |= REVLOGNGINLINEDATA
230 v |= REVLOGNGINLINEDATA
452 if v & REVLOGNG and 'parentdelta' in opener.options:
231 if v & REVLOGNG and 'parentdelta' in opener.options:
453 self._parentdelta = 1
232 self._parentdelta = 1
454
233
455 if shallowroot:
234 if shallowroot:
456 v |= REVLOGSHALLOW
235 v |= REVLOGSHALLOW
457
236
458 i = ''
237 i = ''
459 try:
238 try:
460 f = self.opener(self.indexfile)
239 f = self.opener(self.indexfile)
461 if "nonlazy" in getattr(self.opener, 'options', {}):
240 i = f.read()
462 i = f.read()
463 else:
464 i = f.read(_prereadsize)
465 if len(i) > 0:
241 if len(i) > 0:
466 v = struct.unpack(versionformat, i[:4])[0]
242 v = struct.unpack(versionformat, i[:4])[0]
467 except IOError, inst:
243 except IOError, inst:
468 if inst.errno != errno.ENOENT:
244 if inst.errno != errno.ENOENT:
469 raise
245 raise
470
246
471 self.version = v
247 self.version = v
472 self._inline = v & REVLOGNGINLINEDATA
248 self._inline = v & REVLOGNGINLINEDATA
473 self._shallow = v & REVLOGSHALLOW
249 self._shallow = v & REVLOGSHALLOW
474 flags = v & ~0xFFFF
250 flags = v & ~0xFFFF
475 fmt = v & 0xFFFF
251 fmt = v & 0xFFFF
476 if fmt == REVLOGV0 and flags:
252 if fmt == REVLOGV0 and flags:
477 raise RevlogError(_("index %s unknown flags %#04x for format v0")
253 raise RevlogError(_("index %s unknown flags %#04x for format v0")
478 % (self.indexfile, flags >> 16))
254 % (self.indexfile, flags >> 16))
479 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
255 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
480 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
256 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
481 % (self.indexfile, flags >> 16))
257 % (self.indexfile, flags >> 16))
482 elif fmt > REVLOGNG:
258 elif fmt > REVLOGNG:
483 raise RevlogError(_("index %s unknown format %d")
259 raise RevlogError(_("index %s unknown format %d")
484 % (self.indexfile, fmt))
260 % (self.indexfile, fmt))
485
261
486 self._io = revlogio()
262 self._io = revlogio()
487 if self.version == REVLOGV0:
263 if self.version == REVLOGV0:
488 self._io = revlogoldio()
264 self._io = revlogoldio()
489 if i:
265 if i:
490 try:
266 try:
491 d = self._io.parseindex(f, i, self._inline)
267 d = self._io.parseindex(f, i, self._inline)
492 except (ValueError, IndexError):
268 except (ValueError, IndexError):
493 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
269 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
494 self.index, self.nodemap, self._chunkcache = d
270 self.index, self.nodemap, self._chunkcache = d
495 if not self._chunkcache:
271 if not self._chunkcache:
496 self._chunkclear()
272 self._chunkclear()
497
273
498 # add the magic null revision at -1 (if it hasn't been done already)
274 # add the magic null revision at -1 (if it hasn't been done already)
499 if (self.index == [] or isinstance(self.index, lazyindex) or
275 if self.index == [] or self.index[-1][7] != nullid:
500 self.index[-1][7] != nullid) :
501 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid))
276 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid))
502
277
503 def _loadindex(self, start, end):
504 """load a block of indexes all at once from the lazy parser"""
505 if isinstance(self.index, lazyindex):
506 self.index.p.loadindex(start, end)
507
508 def _loadindexmap(self):
509 """loads both the map and the index from the lazy parser"""
510 if isinstance(self.index, lazyindex):
511 p = self.index.p
512 p.loadindex()
513 self.nodemap = p.map
514
515 def _loadmap(self):
516 """loads the map from the lazy parser"""
517 if isinstance(self.nodemap, lazymap):
518 self.nodemap.p.loadmap()
519 self.nodemap = self.nodemap.p.map
520
521 def tip(self):
278 def tip(self):
522 return self.node(len(self.index) - 2)
279 return self.node(len(self.index) - 2)
523 def __len__(self):
280 def __len__(self):
524 return len(self.index) - 1
281 return len(self.index) - 1
525 def __iter__(self):
282 def __iter__(self):
526 for i in xrange(len(self)):
283 for i in xrange(len(self)):
527 yield i
284 yield i
528 def rev(self, node):
285 def rev(self, node):
529 try:
286 try:
530 return self.nodemap[node]
287 return self.nodemap[node]
531 except KeyError:
288 except KeyError:
532 raise LookupError(node, self.indexfile, _('no node'))
289 raise LookupError(node, self.indexfile, _('no node'))
533 def node(self, rev):
290 def node(self, rev):
534 return self.index[rev][7]
291 return self.index[rev][7]
535 def linkrev(self, rev):
292 def linkrev(self, rev):
536 return self.index[rev][4]
293 return self.index[rev][4]
537 def parents(self, node):
294 def parents(self, node):
538 i = self.index
295 i = self.index
539 d = i[self.rev(node)]
296 d = i[self.rev(node)]
540 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
297 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
541 def parentrevs(self, rev):
298 def parentrevs(self, rev):
542 return self.index[rev][5:7]
299 return self.index[rev][5:7]
543 def start(self, rev):
300 def start(self, rev):
544 return int(self.index[rev][0] >> 16)
301 return int(self.index[rev][0] >> 16)
545 def end(self, rev):
302 def end(self, rev):
546 return self.start(rev) + self.length(rev)
303 return self.start(rev) + self.length(rev)
547 def length(self, rev):
304 def length(self, rev):
548 return self.index[rev][1]
305 return self.index[rev][1]
549 def base(self, rev):
306 def base(self, rev):
550 return self.index[rev][3]
307 return self.index[rev][3]
551 def flags(self, rev):
308 def flags(self, rev):
552 return self.index[rev][0] & 0xFFFF
309 return self.index[rev][0] & 0xFFFF
553 def rawsize(self, rev):
310 def rawsize(self, rev):
554 """return the length of the uncompressed text for a given revision"""
311 """return the length of the uncompressed text for a given revision"""
555 l = self.index[rev][2]
312 l = self.index[rev][2]
556 if l >= 0:
313 if l >= 0:
557 return l
314 return l
558
315
559 t = self.revision(self.node(rev))
316 t = self.revision(self.node(rev))
560 return len(t)
317 return len(t)
561 size = rawsize
318 size = rawsize
562
319
563 def reachable(self, node, stop=None):
320 def reachable(self, node, stop=None):
564 """return the set of all nodes ancestral to a given node, including
321 """return the set of all nodes ancestral to a given node, including
565 the node itself, stopping when stop is matched"""
322 the node itself, stopping when stop is matched"""
566 reachable = set((node,))
323 reachable = set((node,))
567 visit = [node]
324 visit = [node]
568 if stop:
325 if stop:
569 stopn = self.rev(stop)
326 stopn = self.rev(stop)
570 else:
327 else:
571 stopn = 0
328 stopn = 0
572 while visit:
329 while visit:
573 n = visit.pop(0)
330 n = visit.pop(0)
574 if n == stop:
331 if n == stop:
575 continue
332 continue
576 if n == nullid:
333 if n == nullid:
577 continue
334 continue
578 for p in self.parents(n):
335 for p in self.parents(n):
579 if self.rev(p) < stopn:
336 if self.rev(p) < stopn:
580 continue
337 continue
581 if p not in reachable:
338 if p not in reachable:
582 reachable.add(p)
339 reachable.add(p)
583 visit.append(p)
340 visit.append(p)
584 return reachable
341 return reachable
585
342
586 def ancestors(self, *revs):
343 def ancestors(self, *revs):
587 """Generate the ancestors of 'revs' in reverse topological order.
344 """Generate the ancestors of 'revs' in reverse topological order.
588
345
589 Yield a sequence of revision numbers starting with the parents
346 Yield a sequence of revision numbers starting with the parents
590 of each revision in revs, i.e., each revision is *not* considered
347 of each revision in revs, i.e., each revision is *not* considered
591 an ancestor of itself. Results are in breadth-first order:
348 an ancestor of itself. Results are in breadth-first order:
592 parents of each rev in revs, then parents of those, etc. Result
349 parents of each rev in revs, then parents of those, etc. Result
593 does not include the null revision."""
350 does not include the null revision."""
594 visit = list(revs)
351 visit = list(revs)
595 seen = set([nullrev])
352 seen = set([nullrev])
596 while visit:
353 while visit:
597 for parent in self.parentrevs(visit.pop(0)):
354 for parent in self.parentrevs(visit.pop(0)):
598 if parent not in seen:
355 if parent not in seen:
599 visit.append(parent)
356 visit.append(parent)
600 seen.add(parent)
357 seen.add(parent)
601 yield parent
358 yield parent
602
359
603 def descendants(self, *revs):
360 def descendants(self, *revs):
604 """Generate the descendants of 'revs' in revision order.
361 """Generate the descendants of 'revs' in revision order.
605
362
606 Yield a sequence of revision numbers starting with a child of
363 Yield a sequence of revision numbers starting with a child of
607 some rev in revs, i.e., each revision is *not* considered a
364 some rev in revs, i.e., each revision is *not* considered a
608 descendant of itself. Results are ordered by revision number (a
365 descendant of itself. Results are ordered by revision number (a
609 topological sort)."""
366 topological sort)."""
610 first = min(revs)
367 first = min(revs)
611 if first == nullrev:
368 if first == nullrev:
612 for i in self:
369 for i in self:
613 yield i
370 yield i
614 return
371 return
615
372
616 seen = set(revs)
373 seen = set(revs)
617 for i in xrange(first + 1, len(self)):
374 for i in xrange(first + 1, len(self)):
618 for x in self.parentrevs(i):
375 for x in self.parentrevs(i):
619 if x != nullrev and x in seen:
376 if x != nullrev and x in seen:
620 seen.add(i)
377 seen.add(i)
621 yield i
378 yield i
622 break
379 break
623
380
624 def findmissing(self, common=None, heads=None):
381 def findmissing(self, common=None, heads=None):
625 """Return the ancestors of heads that are not ancestors of common.
382 """Return the ancestors of heads that are not ancestors of common.
626
383
627 More specifically, return a list of nodes N such that every N
384 More specifically, return a list of nodes N such that every N
628 satisfies the following constraints:
385 satisfies the following constraints:
629
386
630 1. N is an ancestor of some node in 'heads'
387 1. N is an ancestor of some node in 'heads'
631 2. N is not an ancestor of any node in 'common'
388 2. N is not an ancestor of any node in 'common'
632
389
633 The list is sorted by revision number, meaning it is
390 The list is sorted by revision number, meaning it is
634 topologically sorted.
391 topologically sorted.
635
392
636 'heads' and 'common' are both lists of node IDs. If heads is
393 'heads' and 'common' are both lists of node IDs. If heads is
637 not supplied, uses all of the revlog's heads. If common is not
394 not supplied, uses all of the revlog's heads. If common is not
638 supplied, uses nullid."""
395 supplied, uses nullid."""
639 if common is None:
396 if common is None:
640 common = [nullid]
397 common = [nullid]
641 if heads is None:
398 if heads is None:
642 heads = self.heads()
399 heads = self.heads()
643
400
644 common = [self.rev(n) for n in common]
401 common = [self.rev(n) for n in common]
645 heads = [self.rev(n) for n in heads]
402 heads = [self.rev(n) for n in heads]
646
403
647 # we want the ancestors, but inclusive
404 # we want the ancestors, but inclusive
648 has = set(self.ancestors(*common))
405 has = set(self.ancestors(*common))
649 has.add(nullrev)
406 has.add(nullrev)
650 has.update(common)
407 has.update(common)
651
408
652 # take all ancestors from heads that aren't in has
409 # take all ancestors from heads that aren't in has
653 missing = set()
410 missing = set()
654 visit = [r for r in heads if r not in has]
411 visit = [r for r in heads if r not in has]
655 while visit:
412 while visit:
656 r = visit.pop(0)
413 r = visit.pop(0)
657 if r in missing:
414 if r in missing:
658 continue
415 continue
659 else:
416 else:
660 missing.add(r)
417 missing.add(r)
661 for p in self.parentrevs(r):
418 for p in self.parentrevs(r):
662 if p not in has:
419 if p not in has:
663 visit.append(p)
420 visit.append(p)
664 missing = list(missing)
421 missing = list(missing)
665 missing.sort()
422 missing.sort()
666 return [self.node(r) for r in missing]
423 return [self.node(r) for r in missing]
667
424
668 def nodesbetween(self, roots=None, heads=None):
425 def nodesbetween(self, roots=None, heads=None):
669 """Return a topological path from 'roots' to 'heads'.
426 """Return a topological path from 'roots' to 'heads'.
670
427
671 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
428 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
672 topologically sorted list of all nodes N that satisfy both of
429 topologically sorted list of all nodes N that satisfy both of
673 these constraints:
430 these constraints:
674
431
675 1. N is a descendant of some node in 'roots'
432 1. N is a descendant of some node in 'roots'
676 2. N is an ancestor of some node in 'heads'
433 2. N is an ancestor of some node in 'heads'
677
434
678 Every node is considered to be both a descendant and an ancestor
435 Every node is considered to be both a descendant and an ancestor
679 of itself, so every reachable node in 'roots' and 'heads' will be
436 of itself, so every reachable node in 'roots' and 'heads' will be
680 included in 'nodes'.
437 included in 'nodes'.
681
438
682 'outroots' is the list of reachable nodes in 'roots', i.e., the
439 'outroots' is the list of reachable nodes in 'roots', i.e., the
683 subset of 'roots' that is returned in 'nodes'. Likewise,
440 subset of 'roots' that is returned in 'nodes'. Likewise,
684 'outheads' is the subset of 'heads' that is also in 'nodes'.
441 'outheads' is the subset of 'heads' that is also in 'nodes'.
685
442
686 'roots' and 'heads' are both lists of node IDs. If 'roots' is
443 'roots' and 'heads' are both lists of node IDs. If 'roots' is
687 unspecified, uses nullid as the only root. If 'heads' is
444 unspecified, uses nullid as the only root. If 'heads' is
688 unspecified, uses list of all of the revlog's heads."""
445 unspecified, uses list of all of the revlog's heads."""
689 nonodes = ([], [], [])
446 nonodes = ([], [], [])
690 if roots is not None:
447 if roots is not None:
691 roots = list(roots)
448 roots = list(roots)
692 if not roots:
449 if not roots:
693 return nonodes
450 return nonodes
694 lowestrev = min([self.rev(n) for n in roots])
451 lowestrev = min([self.rev(n) for n in roots])
695 else:
452 else:
696 roots = [nullid] # Everybody's a descendent of nullid
453 roots = [nullid] # Everybody's a descendent of nullid
697 lowestrev = nullrev
454 lowestrev = nullrev
698 if (lowestrev == nullrev) and (heads is None):
455 if (lowestrev == nullrev) and (heads is None):
699 # We want _all_ the nodes!
456 # We want _all_ the nodes!
700 return ([self.node(r) for r in self], [nullid], list(self.heads()))
457 return ([self.node(r) for r in self], [nullid], list(self.heads()))
701 if heads is None:
458 if heads is None:
702 # All nodes are ancestors, so the latest ancestor is the last
459 # All nodes are ancestors, so the latest ancestor is the last
703 # node.
460 # node.
704 highestrev = len(self) - 1
461 highestrev = len(self) - 1
705 # Set ancestors to None to signal that every node is an ancestor.
462 # Set ancestors to None to signal that every node is an ancestor.
706 ancestors = None
463 ancestors = None
707 # Set heads to an empty dictionary for later discovery of heads
464 # Set heads to an empty dictionary for later discovery of heads
708 heads = {}
465 heads = {}
709 else:
466 else:
710 heads = list(heads)
467 heads = list(heads)
711 if not heads:
468 if not heads:
712 return nonodes
469 return nonodes
713 ancestors = set()
470 ancestors = set()
714 # Turn heads into a dictionary so we can remove 'fake' heads.
471 # Turn heads into a dictionary so we can remove 'fake' heads.
715 # Also, later we will be using it to filter out the heads we can't
472 # Also, later we will be using it to filter out the heads we can't
716 # find from roots.
473 # find from roots.
717 heads = dict.fromkeys(heads, 0)
474 heads = dict.fromkeys(heads, 0)
718 # Start at the top and keep marking parents until we're done.
475 # Start at the top and keep marking parents until we're done.
719 nodestotag = set(heads)
476 nodestotag = set(heads)
720 # Remember where the top was so we can use it as a limit later.
477 # Remember where the top was so we can use it as a limit later.
721 highestrev = max([self.rev(n) for n in nodestotag])
478 highestrev = max([self.rev(n) for n in nodestotag])
722 while nodestotag:
479 while nodestotag:
723 # grab a node to tag
480 # grab a node to tag
724 n = nodestotag.pop()
481 n = nodestotag.pop()
725 # Never tag nullid
482 # Never tag nullid
726 if n == nullid:
483 if n == nullid:
727 continue
484 continue
728 # A node's revision number represents its place in a
485 # A node's revision number represents its place in a
729 # topologically sorted list of nodes.
486 # topologically sorted list of nodes.
730 r = self.rev(n)
487 r = self.rev(n)
731 if r >= lowestrev:
488 if r >= lowestrev:
732 if n not in ancestors:
489 if n not in ancestors:
733 # If we are possibly a descendent of one of the roots
490 # If we are possibly a descendent of one of the roots
734 # and we haven't already been marked as an ancestor
491 # and we haven't already been marked as an ancestor
735 ancestors.add(n) # Mark as ancestor
492 ancestors.add(n) # Mark as ancestor
736 # Add non-nullid parents to list of nodes to tag.
493 # Add non-nullid parents to list of nodes to tag.
737 nodestotag.update([p for p in self.parents(n) if
494 nodestotag.update([p for p in self.parents(n) if
738 p != nullid])
495 p != nullid])
739 elif n in heads: # We've seen it before, is it a fake head?
496 elif n in heads: # We've seen it before, is it a fake head?
740 # So it is, real heads should not be the ancestors of
497 # So it is, real heads should not be the ancestors of
741 # any other heads.
498 # any other heads.
742 heads.pop(n)
499 heads.pop(n)
743 if not ancestors:
500 if not ancestors:
744 return nonodes
501 return nonodes
745 # Now that we have our set of ancestors, we want to remove any
502 # Now that we have our set of ancestors, we want to remove any
746 # roots that are not ancestors.
503 # roots that are not ancestors.
747
504
748 # If one of the roots was nullid, everything is included anyway.
505 # If one of the roots was nullid, everything is included anyway.
749 if lowestrev > nullrev:
506 if lowestrev > nullrev:
750 # But, since we weren't, let's recompute the lowest rev to not
507 # But, since we weren't, let's recompute the lowest rev to not
751 # include roots that aren't ancestors.
508 # include roots that aren't ancestors.
752
509
753 # Filter out roots that aren't ancestors of heads
510 # Filter out roots that aren't ancestors of heads
754 roots = [n for n in roots if n in ancestors]
511 roots = [n for n in roots if n in ancestors]
755 # Recompute the lowest revision
512 # Recompute the lowest revision
756 if roots:
513 if roots:
757 lowestrev = min([self.rev(n) for n in roots])
514 lowestrev = min([self.rev(n) for n in roots])
758 else:
515 else:
759 # No more roots? Return empty list
516 # No more roots? Return empty list
760 return nonodes
517 return nonodes
761 else:
518 else:
762 # We are descending from nullid, and don't need to care about
519 # We are descending from nullid, and don't need to care about
763 # any other roots.
520 # any other roots.
764 lowestrev = nullrev
521 lowestrev = nullrev
765 roots = [nullid]
522 roots = [nullid]
766 # Transform our roots list into a set.
523 # Transform our roots list into a set.
767 descendents = set(roots)
524 descendents = set(roots)
768 # Also, keep the original roots so we can filter out roots that aren't
525 # Also, keep the original roots so we can filter out roots that aren't
769 # 'real' roots (i.e. are descended from other roots).
526 # 'real' roots (i.e. are descended from other roots).
770 roots = descendents.copy()
527 roots = descendents.copy()
771 # Our topologically sorted list of output nodes.
528 # Our topologically sorted list of output nodes.
772 orderedout = []
529 orderedout = []
773 # Don't start at nullid since we don't want nullid in our output list,
530 # Don't start at nullid since we don't want nullid in our output list,
774 # and if nullid shows up in descedents, empty parents will look like
531 # and if nullid shows up in descedents, empty parents will look like
775 # they're descendents.
532 # they're descendents.
776 for r in xrange(max(lowestrev, 0), highestrev + 1):
533 for r in xrange(max(lowestrev, 0), highestrev + 1):
777 n = self.node(r)
534 n = self.node(r)
778 isdescendent = False
535 isdescendent = False
779 if lowestrev == nullrev: # Everybody is a descendent of nullid
536 if lowestrev == nullrev: # Everybody is a descendent of nullid
780 isdescendent = True
537 isdescendent = True
781 elif n in descendents:
538 elif n in descendents:
782 # n is already a descendent
539 # n is already a descendent
783 isdescendent = True
540 isdescendent = True
784 # This check only needs to be done here because all the roots
541 # This check only needs to be done here because all the roots
785 # will start being marked is descendents before the loop.
542 # will start being marked is descendents before the loop.
786 if n in roots:
543 if n in roots:
787 # If n was a root, check if it's a 'real' root.
544 # If n was a root, check if it's a 'real' root.
788 p = tuple(self.parents(n))
545 p = tuple(self.parents(n))
789 # If any of its parents are descendents, it's not a root.
546 # If any of its parents are descendents, it's not a root.
790 if (p[0] in descendents) or (p[1] in descendents):
547 if (p[0] in descendents) or (p[1] in descendents):
791 roots.remove(n)
548 roots.remove(n)
792 else:
549 else:
793 p = tuple(self.parents(n))
550 p = tuple(self.parents(n))
794 # A node is a descendent if either of its parents are
551 # A node is a descendent if either of its parents are
795 # descendents. (We seeded the dependents list with the roots
552 # descendents. (We seeded the dependents list with the roots
796 # up there, remember?)
553 # up there, remember?)
797 if (p[0] in descendents) or (p[1] in descendents):
554 if (p[0] in descendents) or (p[1] in descendents):
798 descendents.add(n)
555 descendents.add(n)
799 isdescendent = True
556 isdescendent = True
800 if isdescendent and ((ancestors is None) or (n in ancestors)):
557 if isdescendent and ((ancestors is None) or (n in ancestors)):
801 # Only include nodes that are both descendents and ancestors.
558 # Only include nodes that are both descendents and ancestors.
802 orderedout.append(n)
559 orderedout.append(n)
803 if (ancestors is not None) and (n in heads):
560 if (ancestors is not None) and (n in heads):
804 # We're trying to figure out which heads are reachable
561 # We're trying to figure out which heads are reachable
805 # from roots.
562 # from roots.
806 # Mark this head as having been reached
563 # Mark this head as having been reached
807 heads[n] = 1
564 heads[n] = 1
808 elif ancestors is None:
565 elif ancestors is None:
809 # Otherwise, we're trying to discover the heads.
566 # Otherwise, we're trying to discover the heads.
810 # Assume this is a head because if it isn't, the next step
567 # Assume this is a head because if it isn't, the next step
811 # will eventually remove it.
568 # will eventually remove it.
812 heads[n] = 1
569 heads[n] = 1
813 # But, obviously its parents aren't.
570 # But, obviously its parents aren't.
814 for p in self.parents(n):
571 for p in self.parents(n):
815 heads.pop(p, None)
572 heads.pop(p, None)
816 heads = [n for n in heads.iterkeys() if heads[n] != 0]
573 heads = [n for n in heads.iterkeys() if heads[n] != 0]
817 roots = list(roots)
574 roots = list(roots)
818 assert orderedout
575 assert orderedout
819 assert roots
576 assert roots
820 assert heads
577 assert heads
821 return (orderedout, roots, heads)
578 return (orderedout, roots, heads)
822
579
823 def heads(self, start=None, stop=None):
580 def heads(self, start=None, stop=None):
824 """return the list of all nodes that have no children
581 """return the list of all nodes that have no children
825
582
826 if start is specified, only heads that are descendants of
583 if start is specified, only heads that are descendants of
827 start will be returned
584 start will be returned
828 if stop is specified, it will consider all the revs from stop
585 if stop is specified, it will consider all the revs from stop
829 as if they had no children
586 as if they had no children
830 """
587 """
831 if start is None and stop is None:
588 if start is None and stop is None:
832 count = len(self)
589 count = len(self)
833 if not count:
590 if not count:
834 return [nullid]
591 return [nullid]
835 ishead = [1] * (count + 1)
592 ishead = [1] * (count + 1)
836 index = self.index
593 index = self.index
837 for r in xrange(count):
594 for r in xrange(count):
838 e = index[r]
595 e = index[r]
839 ishead[e[5]] = ishead[e[6]] = 0
596 ishead[e[5]] = ishead[e[6]] = 0
840 return [self.node(r) for r in xrange(count) if ishead[r]]
597 return [self.node(r) for r in xrange(count) if ishead[r]]
841
598
842 if start is None:
599 if start is None:
843 start = nullid
600 start = nullid
844 if stop is None:
601 if stop is None:
845 stop = []
602 stop = []
846 stoprevs = set([self.rev(n) for n in stop])
603 stoprevs = set([self.rev(n) for n in stop])
847 startrev = self.rev(start)
604 startrev = self.rev(start)
848 reachable = set((startrev,))
605 reachable = set((startrev,))
849 heads = set((startrev,))
606 heads = set((startrev,))
850
607
851 parentrevs = self.parentrevs
608 parentrevs = self.parentrevs
852 for r in xrange(startrev + 1, len(self)):
609 for r in xrange(startrev + 1, len(self)):
853 for p in parentrevs(r):
610 for p in parentrevs(r):
854 if p in reachable:
611 if p in reachable:
855 if r not in stoprevs:
612 if r not in stoprevs:
856 reachable.add(r)
613 reachable.add(r)
857 heads.add(r)
614 heads.add(r)
858 if p in heads and p not in stoprevs:
615 if p in heads and p not in stoprevs:
859 heads.remove(p)
616 heads.remove(p)
860
617
861 return [self.node(r) for r in heads]
618 return [self.node(r) for r in heads]
862
619
863 def children(self, node):
620 def children(self, node):
864 """find the children of a given node"""
621 """find the children of a given node"""
865 c = []
622 c = []
866 p = self.rev(node)
623 p = self.rev(node)
867 for r in range(p + 1, len(self)):
624 for r in range(p + 1, len(self)):
868 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
625 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
869 if prevs:
626 if prevs:
870 for pr in prevs:
627 for pr in prevs:
871 if pr == p:
628 if pr == p:
872 c.append(self.node(r))
629 c.append(self.node(r))
873 elif p == nullrev:
630 elif p == nullrev:
874 c.append(self.node(r))
631 c.append(self.node(r))
875 return c
632 return c
876
633
877 def descendant(self, start, end):
634 def descendant(self, start, end):
878 if start == nullrev:
635 if start == nullrev:
879 return True
636 return True
880 for i in self.descendants(start):
637 for i in self.descendants(start):
881 if i == end:
638 if i == end:
882 return True
639 return True
883 elif i > end:
640 elif i > end:
884 break
641 break
885 return False
642 return False
886
643
887 def ancestor(self, a, b):
644 def ancestor(self, a, b):
888 """calculate the least common ancestor of nodes a and b"""
645 """calculate the least common ancestor of nodes a and b"""
889
646
890 # fast path, check if it is a descendant
647 # fast path, check if it is a descendant
891 a, b = self.rev(a), self.rev(b)
648 a, b = self.rev(a), self.rev(b)
892 start, end = sorted((a, b))
649 start, end = sorted((a, b))
893 if self.descendant(start, end):
650 if self.descendant(start, end):
894 return self.node(start)
651 return self.node(start)
895
652
896 def parents(rev):
653 def parents(rev):
897 return [p for p in self.parentrevs(rev) if p != nullrev]
654 return [p for p in self.parentrevs(rev) if p != nullrev]
898
655
899 c = ancestor.ancestor(a, b, parents)
656 c = ancestor.ancestor(a, b, parents)
900 if c is None:
657 if c is None:
901 return nullid
658 return nullid
902
659
903 return self.node(c)
660 return self.node(c)
904
661
905 def _match(self, id):
662 def _match(self, id):
906 if isinstance(id, (long, int)):
663 if isinstance(id, (long, int)):
907 # rev
664 # rev
908 return self.node(id)
665 return self.node(id)
909 if len(id) == 20:
666 if len(id) == 20:
910 # possibly a binary node
667 # possibly a binary node
911 # odds of a binary node being all hex in ASCII are 1 in 10**25
668 # odds of a binary node being all hex in ASCII are 1 in 10**25
912 try:
669 try:
913 node = id
670 node = id
914 self.rev(node) # quick search the index
671 self.rev(node) # quick search the index
915 return node
672 return node
916 except LookupError:
673 except LookupError:
917 pass # may be partial hex id
674 pass # may be partial hex id
918 try:
675 try:
919 # str(rev)
676 # str(rev)
920 rev = int(id)
677 rev = int(id)
921 if str(rev) != id:
678 if str(rev) != id:
922 raise ValueError
679 raise ValueError
923 if rev < 0:
680 if rev < 0:
924 rev = len(self) + rev
681 rev = len(self) + rev
925 if rev < 0 or rev >= len(self):
682 if rev < 0 or rev >= len(self):
926 raise ValueError
683 raise ValueError
927 return self.node(rev)
684 return self.node(rev)
928 except (ValueError, OverflowError):
685 except (ValueError, OverflowError):
929 pass
686 pass
930 if len(id) == 40:
687 if len(id) == 40:
931 try:
688 try:
932 # a full hex nodeid?
689 # a full hex nodeid?
933 node = bin(id)
690 node = bin(id)
934 self.rev(node)
691 self.rev(node)
935 return node
692 return node
936 except (TypeError, LookupError):
693 except (TypeError, LookupError):
937 pass
694 pass
938
695
939 def _partialmatch(self, id):
696 def _partialmatch(self, id):
940 if len(id) < 40:
697 if len(id) < 40:
941 try:
698 try:
942 # hex(node)[:...]
699 # hex(node)[:...]
943 l = len(id) // 2 # grab an even number of digits
700 l = len(id) // 2 # grab an even number of digits
944 bin_id = bin(id[:l * 2])
701 bin_id = bin(id[:l * 2])
945 nl = [n for n in self.nodemap if n[:l] == bin_id]
702 nl = [n for n in self.nodemap if n[:l] == bin_id]
946 nl = [n for n in nl if hex(n).startswith(id)]
703 nl = [n for n in nl if hex(n).startswith(id)]
947 if len(nl) > 0:
704 if len(nl) > 0:
948 if len(nl) == 1:
705 if len(nl) == 1:
949 return nl[0]
706 return nl[0]
950 raise LookupError(id, self.indexfile,
707 raise LookupError(id, self.indexfile,
951 _('ambiguous identifier'))
708 _('ambiguous identifier'))
952 return None
709 return None
953 except TypeError:
710 except TypeError:
954 pass
711 pass
955
712
956 def lookup(self, id):
713 def lookup(self, id):
957 """locate a node based on:
714 """locate a node based on:
958 - revision number or str(revision number)
715 - revision number or str(revision number)
959 - nodeid or subset of hex nodeid
716 - nodeid or subset of hex nodeid
960 """
717 """
961 n = self._match(id)
718 n = self._match(id)
962 if n is not None:
719 if n is not None:
963 return n
720 return n
964 n = self._partialmatch(id)
721 n = self._partialmatch(id)
965 if n:
722 if n:
966 return n
723 return n
967
724
968 raise LookupError(id, self.indexfile, _('no match found'))
725 raise LookupError(id, self.indexfile, _('no match found'))
969
726
970 def cmp(self, node, text):
727 def cmp(self, node, text):
971 """compare text with a given file revision
728 """compare text with a given file revision
972
729
973 returns True if text is different than what is stored.
730 returns True if text is different than what is stored.
974 """
731 """
975 p1, p2 = self.parents(node)
732 p1, p2 = self.parents(node)
976 return hash(text, p1, p2) != node
733 return hash(text, p1, p2) != node
977
734
978 def _addchunk(self, offset, data):
735 def _addchunk(self, offset, data):
979 o, d = self._chunkcache
736 o, d = self._chunkcache
980 # try to add to existing cache
737 # try to add to existing cache
981 if o + len(d) == offset and len(d) + len(data) < _prereadsize:
738 if o + len(d) == offset and len(d) + len(data) < _chunksize:
982 self._chunkcache = o, d + data
739 self._chunkcache = o, d + data
983 else:
740 else:
984 self._chunkcache = offset, data
741 self._chunkcache = offset, data
985
742
986 def _loadchunk(self, offset, length):
743 def _loadchunk(self, offset, length):
987 if self._inline:
744 if self._inline:
988 df = self.opener(self.indexfile)
745 df = self.opener(self.indexfile)
989 else:
746 else:
990 df = self.opener(self.datafile)
747 df = self.opener(self.datafile)
991
748
992 readahead = max(65536, length)
749 readahead = max(65536, length)
993 df.seek(offset)
750 df.seek(offset)
994 d = df.read(readahead)
751 d = df.read(readahead)
995 self._addchunk(offset, d)
752 self._addchunk(offset, d)
996 if readahead > length:
753 if readahead > length:
997 return d[:length]
754 return d[:length]
998 return d
755 return d
999
756
1000 def _getchunk(self, offset, length):
757 def _getchunk(self, offset, length):
1001 o, d = self._chunkcache
758 o, d = self._chunkcache
1002 l = len(d)
759 l = len(d)
1003
760
1004 # is it in the cache?
761 # is it in the cache?
1005 cachestart = offset - o
762 cachestart = offset - o
1006 cacheend = cachestart + length
763 cacheend = cachestart + length
1007 if cachestart >= 0 and cacheend <= l:
764 if cachestart >= 0 and cacheend <= l:
1008 if cachestart == 0 and cacheend == l:
765 if cachestart == 0 and cacheend == l:
1009 return d # avoid a copy
766 return d # avoid a copy
1010 return d[cachestart:cacheend]
767 return d[cachestart:cacheend]
1011
768
1012 return self._loadchunk(offset, length)
769 return self._loadchunk(offset, length)
1013
770
1014 def _chunkraw(self, startrev, endrev):
771 def _chunkraw(self, startrev, endrev):
1015 start = self.start(startrev)
772 start = self.start(startrev)
1016 length = self.end(endrev) - start
773 length = self.end(endrev) - start
1017 if self._inline:
774 if self._inline:
1018 start += (startrev + 1) * self._io.size
775 start += (startrev + 1) * self._io.size
1019 return self._getchunk(start, length)
776 return self._getchunk(start, length)
1020
777
1021 def _chunk(self, rev):
778 def _chunk(self, rev):
1022 return decompress(self._chunkraw(rev, rev))
779 return decompress(self._chunkraw(rev, rev))
1023
780
1024 def _chunkclear(self):
781 def _chunkclear(self):
1025 self._chunkcache = (0, '')
782 self._chunkcache = (0, '')
1026
783
1027 def deltaparent(self, rev):
784 def deltaparent(self, rev):
1028 """return previous revision or parentrev according to flags"""
785 """return previous revision or parentrev according to flags"""
1029 if self.flags(rev) & REVIDX_PARENTDELTA:
786 if self.flags(rev) & REVIDX_PARENTDELTA:
1030 return self.parentrevs(rev)[0]
787 return self.parentrevs(rev)[0]
1031 else:
788 else:
1032 return rev - 1
789 return rev - 1
1033
790
1034 def revdiff(self, rev1, rev2):
791 def revdiff(self, rev1, rev2):
1035 """return or calculate a delta between two revisions"""
792 """return or calculate a delta between two revisions"""
1036 if self.base(rev2) != rev2 and self.deltaparent(rev2) == rev1:
793 if self.base(rev2) != rev2 and self.deltaparent(rev2) == rev1:
1037 return self._chunk(rev2)
794 return self._chunk(rev2)
1038
795
1039 return mdiff.textdiff(self.revision(self.node(rev1)),
796 return mdiff.textdiff(self.revision(self.node(rev1)),
1040 self.revision(self.node(rev2)))
797 self.revision(self.node(rev2)))
1041
798
1042 def revision(self, node):
799 def revision(self, node):
1043 """return an uncompressed revision of a given node"""
800 """return an uncompressed revision of a given node"""
1044 cachedrev = None
801 cachedrev = None
1045 if node == nullid:
802 if node == nullid:
1046 return ""
803 return ""
1047 if self._cache:
804 if self._cache:
1048 if self._cache[0] == node:
805 if self._cache[0] == node:
1049 return self._cache[2]
806 return self._cache[2]
1050 cachedrev = self._cache[1]
807 cachedrev = self._cache[1]
1051
808
1052 # look up what we need to read
809 # look up what we need to read
1053 text = None
810 text = None
1054 rev = self.rev(node)
811 rev = self.rev(node)
1055 base = self.base(rev)
812 base = self.base(rev)
1056
813
1057 # check rev flags
814 # check rev flags
1058 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
815 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
1059 raise RevlogError(_('incompatible revision flag %x') %
816 raise RevlogError(_('incompatible revision flag %x') %
1060 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
817 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
1061
818
1062 # build delta chain
819 # build delta chain
1063 self._loadindex(base, rev + 1)
1064 chain = []
820 chain = []
1065 index = self.index # for performance
821 index = self.index # for performance
1066 iterrev = rev
822 iterrev = rev
1067 e = index[iterrev]
823 e = index[iterrev]
1068 while iterrev != base and iterrev != cachedrev:
824 while iterrev != base and iterrev != cachedrev:
1069 chain.append(iterrev)
825 chain.append(iterrev)
1070 if e[0] & REVIDX_PARENTDELTA:
826 if e[0] & REVIDX_PARENTDELTA:
1071 iterrev = e[5]
827 iterrev = e[5]
1072 else:
828 else:
1073 iterrev -= 1
829 iterrev -= 1
1074 e = index[iterrev]
830 e = index[iterrev]
1075 chain.reverse()
831 chain.reverse()
1076 base = iterrev
832 base = iterrev
1077
833
1078 if iterrev == cachedrev:
834 if iterrev == cachedrev:
1079 # cache hit
835 # cache hit
1080 text = self._cache[2]
836 text = self._cache[2]
1081
837
1082 # drop cache to save memory
838 # drop cache to save memory
1083 self._cache = None
839 self._cache = None
1084
840
1085 self._chunkraw(base, rev)
841 self._chunkraw(base, rev)
1086 if text is None:
842 if text is None:
1087 text = self._chunk(base)
843 text = self._chunk(base)
1088
844
1089 bins = [self._chunk(r) for r in chain]
845 bins = [self._chunk(r) for r in chain]
1090 text = mdiff.patches(text, bins)
846 text = mdiff.patches(text, bins)
1091
847
1092 text = self._checkhash(text, node)
848 text = self._checkhash(text, node)
1093
849
1094 self._cache = (node, rev, text)
850 self._cache = (node, rev, text)
1095 return text
851 return text
1096
852
1097 def _checkhash(self, text, node):
853 def _checkhash(self, text, node):
1098 p1, p2 = self.parents(node)
854 p1, p2 = self.parents(node)
1099 if (node != hash(text, p1, p2) and
855 if (node != hash(text, p1, p2) and
1100 not (self.flags(rev) & REVIDX_PUNCHED_FLAG)):
856 not (self.flags(rev) & REVIDX_PUNCHED_FLAG)):
1101 raise RevlogError(_("integrity check failed on %s:%d")
857 raise RevlogError(_("integrity check failed on %s:%d")
1102 % (self.indexfile, rev))
858 % (self.indexfile, rev))
1103 return text
859 return text
1104
860
1105 def checkinlinesize(self, tr, fp=None):
861 def checkinlinesize(self, tr, fp=None):
1106 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
862 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
1107 return
863 return
1108
864
1109 trinfo = tr.find(self.indexfile)
865 trinfo = tr.find(self.indexfile)
1110 if trinfo is None:
866 if trinfo is None:
1111 raise RevlogError(_("%s not found in the transaction")
867 raise RevlogError(_("%s not found in the transaction")
1112 % self.indexfile)
868 % self.indexfile)
1113
869
1114 trindex = trinfo[2]
870 trindex = trinfo[2]
1115 dataoff = self.start(trindex)
871 dataoff = self.start(trindex)
1116
872
1117 tr.add(self.datafile, dataoff)
873 tr.add(self.datafile, dataoff)
1118
874
1119 if fp:
875 if fp:
1120 fp.flush()
876 fp.flush()
1121 fp.close()
877 fp.close()
1122
878
1123 df = self.opener(self.datafile, 'w')
879 df = self.opener(self.datafile, 'w')
1124 try:
880 try:
1125 for r in self:
881 for r in self:
1126 df.write(self._chunkraw(r, r))
882 df.write(self._chunkraw(r, r))
1127 finally:
883 finally:
1128 df.close()
884 df.close()
1129
885
1130 fp = self.opener(self.indexfile, 'w', atomictemp=True)
886 fp = self.opener(self.indexfile, 'w', atomictemp=True)
1131 self.version &= ~(REVLOGNGINLINEDATA)
887 self.version &= ~(REVLOGNGINLINEDATA)
1132 self._inline = False
888 self._inline = False
1133 for i in self:
889 for i in self:
1134 e = self._io.packentry(self.index[i], self.node, self.version, i)
890 e = self._io.packentry(self.index[i], self.node, self.version, i)
1135 fp.write(e)
891 fp.write(e)
1136
892
1137 # if we don't call rename, the temp file will never replace the
893 # if we don't call rename, the temp file will never replace the
1138 # real index
894 # real index
1139 fp.rename()
895 fp.rename()
1140
896
1141 tr.replace(self.indexfile, trindex * self._io.size)
897 tr.replace(self.indexfile, trindex * self._io.size)
1142 self._chunkclear()
898 self._chunkclear()
1143
899
1144 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
900 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
1145 """add a revision to the log
901 """add a revision to the log
1146
902
1147 text - the revision data to add
903 text - the revision data to add
1148 transaction - the transaction object used for rollback
904 transaction - the transaction object used for rollback
1149 link - the linkrev data to add
905 link - the linkrev data to add
1150 p1, p2 - the parent nodeids of the revision
906 p1, p2 - the parent nodeids of the revision
1151 cachedelta - an optional precomputed delta
907 cachedelta - an optional precomputed delta
1152 """
908 """
1153 node = hash(text, p1, p2)
909 node = hash(text, p1, p2)
1154 if (node in self.nodemap and
910 if (node in self.nodemap and
1155 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
911 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1156 return node
912 return node
1157
913
1158 dfh = None
914 dfh = None
1159 if not self._inline:
915 if not self._inline:
1160 dfh = self.opener(self.datafile, "a")
916 dfh = self.opener(self.datafile, "a")
1161 ifh = self.opener(self.indexfile, "a+")
917 ifh = self.opener(self.indexfile, "a+")
1162 try:
918 try:
1163 return self._addrevision(node, text, transaction, link, p1, p2,
919 return self._addrevision(node, text, transaction, link, p1, p2,
1164 cachedelta, ifh, dfh)
920 cachedelta, ifh, dfh)
1165 finally:
921 finally:
1166 if dfh:
922 if dfh:
1167 dfh.close()
923 dfh.close()
1168 ifh.close()
924 ifh.close()
1169
925
1170 def _addrevision(self, node, text, transaction, link, p1, p2,
926 def _addrevision(self, node, text, transaction, link, p1, p2,
1171 cachedelta, ifh, dfh):
927 cachedelta, ifh, dfh):
1172
928
1173 btext = [text]
929 btext = [text]
1174 def buildtext():
930 def buildtext():
1175 if btext[0] is not None:
931 if btext[0] is not None:
1176 return btext[0]
932 return btext[0]
1177 # flush any pending writes here so we can read it in revision
933 # flush any pending writes here so we can read it in revision
1178 if dfh:
934 if dfh:
1179 dfh.flush()
935 dfh.flush()
1180 ifh.flush()
936 ifh.flush()
1181 basetext = self.revision(self.node(cachedelta[0]))
937 basetext = self.revision(self.node(cachedelta[0]))
1182 btext[0] = mdiff.patch(basetext, cachedelta[1])
938 btext[0] = mdiff.patch(basetext, cachedelta[1])
1183 chk = hash(btext[0], p1, p2)
939 chk = hash(btext[0], p1, p2)
1184 if chk != node:
940 if chk != node:
1185 raise RevlogError(_("consistency error in delta"))
941 raise RevlogError(_("consistency error in delta"))
1186 return btext[0]
942 return btext[0]
1187
943
1188 def builddelta(rev):
944 def builddelta(rev):
1189 # can we use the cached delta?
945 # can we use the cached delta?
1190 if cachedelta and cachedelta[0] == rev:
946 if cachedelta and cachedelta[0] == rev:
1191 delta = cachedelta[1]
947 delta = cachedelta[1]
1192 else:
948 else:
1193 t = buildtext()
949 t = buildtext()
1194 ptext = self.revision(self.node(rev))
950 ptext = self.revision(self.node(rev))
1195 delta = mdiff.textdiff(ptext, t)
951 delta = mdiff.textdiff(ptext, t)
1196 data = compress(delta)
952 data = compress(delta)
1197 l = len(data[1]) + len(data[0])
953 l = len(data[1]) + len(data[0])
1198 base = self.base(rev)
954 base = self.base(rev)
1199 dist = l + offset - self.start(base)
955 dist = l + offset - self.start(base)
1200 return dist, l, data, base
956 return dist, l, data, base
1201
957
1202 curr = len(self)
958 curr = len(self)
1203 prev = curr - 1
959 prev = curr - 1
1204 base = curr
960 base = curr
1205 offset = self.end(prev)
961 offset = self.end(prev)
1206 flags = 0
962 flags = 0
1207 d = None
963 d = None
1208 p1r, p2r = self.rev(p1), self.rev(p2)
964 p1r, p2r = self.rev(p1), self.rev(p2)
1209
965
1210 # should we try to build a delta?
966 # should we try to build a delta?
1211 if prev != nullrev:
967 if prev != nullrev:
1212 d = builddelta(prev)
968 d = builddelta(prev)
1213 if self._parentdelta and prev != p1r:
969 if self._parentdelta and prev != p1r:
1214 d2 = builddelta(p1r)
970 d2 = builddelta(p1r)
1215 if d2 < d:
971 if d2 < d:
1216 d = d2
972 d = d2
1217 flags = REVIDX_PARENTDELTA
973 flags = REVIDX_PARENTDELTA
1218 dist, l, data, base = d
974 dist, l, data, base = d
1219
975
1220 # full versions are inserted when the needed deltas
976 # full versions are inserted when the needed deltas
1221 # become comparable to the uncompressed text
977 # become comparable to the uncompressed text
1222 # or the base revision is punched
978 # or the base revision is punched
1223 if text is None:
979 if text is None:
1224 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
980 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1225 cachedelta[1])
981 cachedelta[1])
1226 else:
982 else:
1227 textlen = len(text)
983 textlen = len(text)
1228 if (d is None or dist > textlen * 2 or
984 if (d is None or dist > textlen * 2 or
1229 (self.flags(base) & REVIDX_PUNCHED_FLAG)):
985 (self.flags(base) & REVIDX_PUNCHED_FLAG)):
1230 text = buildtext()
986 text = buildtext()
1231 data = compress(text)
987 data = compress(text)
1232 l = len(data[1]) + len(data[0])
988 l = len(data[1]) + len(data[0])
1233 base = curr
989 base = curr
1234
990
1235 e = (offset_type(offset, flags), l, textlen,
991 e = (offset_type(offset, flags), l, textlen,
1236 base, link, p1r, p2r, node)
992 base, link, p1r, p2r, node)
1237 self.index.insert(-1, e)
993 self.index.insert(-1, e)
1238 self.nodemap[node] = curr
994 self.nodemap[node] = curr
1239
995
1240 entry = self._io.packentry(e, self.node, self.version, curr)
996 entry = self._io.packentry(e, self.node, self.version, curr)
1241 if not self._inline:
997 if not self._inline:
1242 transaction.add(self.datafile, offset)
998 transaction.add(self.datafile, offset)
1243 transaction.add(self.indexfile, curr * len(entry))
999 transaction.add(self.indexfile, curr * len(entry))
1244 if data[0]:
1000 if data[0]:
1245 dfh.write(data[0])
1001 dfh.write(data[0])
1246 dfh.write(data[1])
1002 dfh.write(data[1])
1247 dfh.flush()
1003 dfh.flush()
1248 ifh.write(entry)
1004 ifh.write(entry)
1249 else:
1005 else:
1250 offset += curr * self._io.size
1006 offset += curr * self._io.size
1251 transaction.add(self.indexfile, offset, curr)
1007 transaction.add(self.indexfile, offset, curr)
1252 ifh.write(entry)
1008 ifh.write(entry)
1253 ifh.write(data[0])
1009 ifh.write(data[0])
1254 ifh.write(data[1])
1010 ifh.write(data[1])
1255 self.checkinlinesize(transaction, ifh)
1011 self.checkinlinesize(transaction, ifh)
1256
1012
1257 if type(text) == str: # only accept immutable objects
1013 if type(text) == str: # only accept immutable objects
1258 self._cache = (node, curr, text)
1014 self._cache = (node, curr, text)
1259 return node
1015 return node
1260
1016
1261 def group(self, nodelist, lookup, infocollect=None, fullrev=False):
1017 def group(self, nodelist, lookup, infocollect=None, fullrev=False):
1262 """Calculate a delta group, yielding a sequence of changegroup chunks
1018 """Calculate a delta group, yielding a sequence of changegroup chunks
1263 (strings).
1019 (strings).
1264
1020
1265 Given a list of changeset revs, return a set of deltas and
1021 Given a list of changeset revs, return a set of deltas and
1266 metadata corresponding to nodes. The first delta is
1022 metadata corresponding to nodes. The first delta is
1267 first parent(nodelist[0]) -> nodelist[0], the receiver is
1023 first parent(nodelist[0]) -> nodelist[0], the receiver is
1268 guaranteed to have this parent as it has all history before
1024 guaranteed to have this parent as it has all history before
1269 these changesets. In the case firstparent is nullrev the
1025 these changesets. In the case firstparent is nullrev the
1270 changegroup starts with a full revision.
1026 changegroup starts with a full revision.
1271 fullrev forces the insertion of the full revision, necessary
1027 fullrev forces the insertion of the full revision, necessary
1272 in the case of shallow clones where the first parent might
1028 in the case of shallow clones where the first parent might
1273 not exist at the reciever.
1029 not exist at the reciever.
1274 """
1030 """
1275
1031
1276 revs = [self.rev(n) for n in nodelist]
1032 revs = [self.rev(n) for n in nodelist]
1277
1033
1278 # if we don't have any revisions touched by these changesets, bail
1034 # if we don't have any revisions touched by these changesets, bail
1279 if not revs:
1035 if not revs:
1280 yield changegroup.closechunk()
1036 yield changegroup.closechunk()
1281 return
1037 return
1282
1038
1283 # add the parent of the first rev
1039 # add the parent of the first rev
1284 p = self.parentrevs(revs[0])[0]
1040 p = self.parentrevs(revs[0])[0]
1285 revs.insert(0, p)
1041 revs.insert(0, p)
1286 if p == nullrev:
1042 if p == nullrev:
1287 fullrev = True
1043 fullrev = True
1288
1044
1289 # build deltas
1045 # build deltas
1290 for d in xrange(len(revs) - 1):
1046 for d in xrange(len(revs) - 1):
1291 a, b = revs[d], revs[d + 1]
1047 a, b = revs[d], revs[d + 1]
1292 nb = self.node(b)
1048 nb = self.node(b)
1293
1049
1294 if infocollect is not None:
1050 if infocollect is not None:
1295 infocollect(nb)
1051 infocollect(nb)
1296
1052
1297 p = self.parents(nb)
1053 p = self.parents(nb)
1298 meta = nb + p[0] + p[1] + lookup(nb)
1054 meta = nb + p[0] + p[1] + lookup(nb)
1299 if fullrev:
1055 if fullrev:
1300 d = self.revision(nb)
1056 d = self.revision(nb)
1301 meta += mdiff.trivialdiffheader(len(d))
1057 meta += mdiff.trivialdiffheader(len(d))
1302 fullrev = False
1058 fullrev = False
1303 else:
1059 else:
1304 d = self.revdiff(a, b)
1060 d = self.revdiff(a, b)
1305 yield changegroup.chunkheader(len(meta) + len(d))
1061 yield changegroup.chunkheader(len(meta) + len(d))
1306 yield meta
1062 yield meta
1307 yield d
1063 yield d
1308
1064
1309 yield changegroup.closechunk()
1065 yield changegroup.closechunk()
1310
1066
1311 def addgroup(self, bundle, linkmapper, transaction):
1067 def addgroup(self, bundle, linkmapper, transaction):
1312 """
1068 """
1313 add a delta group
1069 add a delta group
1314
1070
1315 given a set of deltas, add them to the revision log. the
1071 given a set of deltas, add them to the revision log. the
1316 first delta is against its parent, which should be in our
1072 first delta is against its parent, which should be in our
1317 log, the rest are against the previous delta.
1073 log, the rest are against the previous delta.
1318 """
1074 """
1319
1075
1320 # track the base of the current delta log
1076 # track the base of the current delta log
1321 node = None
1077 node = None
1322
1078
1323 r = len(self)
1079 r = len(self)
1324 end = 0
1080 end = 0
1325 if r:
1081 if r:
1326 end = self.end(r - 1)
1082 end = self.end(r - 1)
1327 ifh = self.opener(self.indexfile, "a+")
1083 ifh = self.opener(self.indexfile, "a+")
1328 isize = r * self._io.size
1084 isize = r * self._io.size
1329 if self._inline:
1085 if self._inline:
1330 transaction.add(self.indexfile, end + isize, r)
1086 transaction.add(self.indexfile, end + isize, r)
1331 dfh = None
1087 dfh = None
1332 else:
1088 else:
1333 transaction.add(self.indexfile, isize, r)
1089 transaction.add(self.indexfile, isize, r)
1334 transaction.add(self.datafile, end)
1090 transaction.add(self.datafile, end)
1335 dfh = self.opener(self.datafile, "a")
1091 dfh = self.opener(self.datafile, "a")
1336
1092
1337 try:
1093 try:
1338 # loop through our set of deltas
1094 # loop through our set of deltas
1339 chain = None
1095 chain = None
1340 while 1:
1096 while 1:
1341 chunkdata = bundle.parsechunk()
1097 chunkdata = bundle.parsechunk()
1342 if not chunkdata:
1098 if not chunkdata:
1343 break
1099 break
1344 node = chunkdata['node']
1100 node = chunkdata['node']
1345 p1 = chunkdata['p1']
1101 p1 = chunkdata['p1']
1346 p2 = chunkdata['p2']
1102 p2 = chunkdata['p2']
1347 cs = chunkdata['cs']
1103 cs = chunkdata['cs']
1348 delta = chunkdata['data']
1104 delta = chunkdata['data']
1349
1105
1350 link = linkmapper(cs)
1106 link = linkmapper(cs)
1351 if (node in self.nodemap and
1107 if (node in self.nodemap and
1352 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1108 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1353 # this can happen if two branches make the same change
1109 # this can happen if two branches make the same change
1354 chain = node
1110 chain = node
1355 continue
1111 continue
1356
1112
1357 for p in (p1, p2):
1113 for p in (p1, p2):
1358 if not p in self.nodemap:
1114 if not p in self.nodemap:
1359 if self._shallow:
1115 if self._shallow:
1360 # add null entries for missing parents
1116 # add null entries for missing parents
1361 # XXX FIXME
1117 # XXX FIXME
1362 #if base == nullrev:
1118 #if base == nullrev:
1363 # base = len(self)
1119 # base = len(self)
1364 #e = (offset_type(end, REVIDX_PUNCHED_FLAG),
1120 #e = (offset_type(end, REVIDX_PUNCHED_FLAG),
1365 # 0, 0, base, nullrev, nullrev, nullrev, p)
1121 # 0, 0, base, nullrev, nullrev, nullrev, p)
1366 #self.index.insert(-1, e)
1122 #self.index.insert(-1, e)
1367 #self.nodemap[p] = r
1123 #self.nodemap[p] = r
1368 #entry = self._io.packentry(e, self.node,
1124 #entry = self._io.packentry(e, self.node,
1369 # self.version, r)
1125 # self.version, r)
1370 #ifh.write(entry)
1126 #ifh.write(entry)
1371 #t, r = r, r + 1
1127 #t, r = r, r + 1
1372 raise LookupError(p, self.indexfile,
1128 raise LookupError(p, self.indexfile,
1373 _('unknown parent'))
1129 _('unknown parent'))
1374 else:
1130 else:
1375 raise LookupError(p, self.indexfile,
1131 raise LookupError(p, self.indexfile,
1376 _('unknown parent'))
1132 _('unknown parent'))
1377
1133
1378 if not chain:
1134 if not chain:
1379 # retrieve the parent revision of the delta chain
1135 # retrieve the parent revision of the delta chain
1380 chain = p1
1136 chain = p1
1381 if not chain in self.nodemap:
1137 if not chain in self.nodemap:
1382 raise LookupError(chain, self.indexfile, _('unknown base'))
1138 raise LookupError(chain, self.indexfile, _('unknown base'))
1383
1139
1384 chainrev = self.rev(chain)
1140 chainrev = self.rev(chain)
1385 chain = self._addrevision(node, None, transaction, link,
1141 chain = self._addrevision(node, None, transaction, link,
1386 p1, p2, (chainrev, delta), ifh, dfh)
1142 p1, p2, (chainrev, delta), ifh, dfh)
1387 if not dfh and not self._inline:
1143 if not dfh and not self._inline:
1388 # addrevision switched from inline to conventional
1144 # addrevision switched from inline to conventional
1389 # reopen the index
1145 # reopen the index
1390 dfh = self.opener(self.datafile, "a")
1146 dfh = self.opener(self.datafile, "a")
1391 ifh = self.opener(self.indexfile, "a")
1147 ifh = self.opener(self.indexfile, "a")
1392 finally:
1148 finally:
1393 if dfh:
1149 if dfh:
1394 dfh.close()
1150 dfh.close()
1395 ifh.close()
1151 ifh.close()
1396
1152
1397 return node
1153 return node
1398
1154
1399 def strip(self, minlink, transaction):
1155 def strip(self, minlink, transaction):
1400 """truncate the revlog on the first revision with a linkrev >= minlink
1156 """truncate the revlog on the first revision with a linkrev >= minlink
1401
1157
1402 This function is called when we're stripping revision minlink and
1158 This function is called when we're stripping revision minlink and
1403 its descendants from the repository.
1159 its descendants from the repository.
1404
1160
1405 We have to remove all revisions with linkrev >= minlink, because
1161 We have to remove all revisions with linkrev >= minlink, because
1406 the equivalent changelog revisions will be renumbered after the
1162 the equivalent changelog revisions will be renumbered after the
1407 strip.
1163 strip.
1408
1164
1409 So we truncate the revlog on the first of these revisions, and
1165 So we truncate the revlog on the first of these revisions, and
1410 trust that the caller has saved the revisions that shouldn't be
1166 trust that the caller has saved the revisions that shouldn't be
1411 removed and that it'll readd them after this truncation.
1167 removed and that it'll readd them after this truncation.
1412 """
1168 """
1413 if len(self) == 0:
1169 if len(self) == 0:
1414 return
1170 return
1415
1171
1416 if isinstance(self.index, lazyindex):
1417 self._loadindexmap()
1418
1419 for rev in self:
1172 for rev in self:
1420 if self.index[rev][4] >= minlink:
1173 if self.index[rev][4] >= minlink:
1421 break
1174 break
1422 else:
1175 else:
1423 return
1176 return
1424
1177
1425 # first truncate the files on disk
1178 # first truncate the files on disk
1426 end = self.start(rev)
1179 end = self.start(rev)
1427 if not self._inline:
1180 if not self._inline:
1428 transaction.add(self.datafile, end)
1181 transaction.add(self.datafile, end)
1429 end = rev * self._io.size
1182 end = rev * self._io.size
1430 else:
1183 else:
1431 end += rev * self._io.size
1184 end += rev * self._io.size
1432
1185
1433 transaction.add(self.indexfile, end)
1186 transaction.add(self.indexfile, end)
1434
1187
1435 # then reset internal state in memory to forget those revisions
1188 # then reset internal state in memory to forget those revisions
1436 self._cache = None
1189 self._cache = None
1437 self._chunkclear()
1190 self._chunkclear()
1438 for x in xrange(rev, len(self)):
1191 for x in xrange(rev, len(self)):
1439 del self.nodemap[self.node(x)]
1192 del self.nodemap[self.node(x)]
1440
1193
1441 del self.index[rev:-1]
1194 del self.index[rev:-1]
1442
1195
1443 def checksize(self):
1196 def checksize(self):
1444 expected = 0
1197 expected = 0
1445 if len(self):
1198 if len(self):
1446 expected = max(0, self.end(len(self) - 1))
1199 expected = max(0, self.end(len(self) - 1))
1447
1200
1448 try:
1201 try:
1449 f = self.opener(self.datafile)
1202 f = self.opener(self.datafile)
1450 f.seek(0, 2)
1203 f.seek(0, 2)
1451 actual = f.tell()
1204 actual = f.tell()
1452 dd = actual - expected
1205 dd = actual - expected
1453 except IOError, inst:
1206 except IOError, inst:
1454 if inst.errno != errno.ENOENT:
1207 if inst.errno != errno.ENOENT:
1455 raise
1208 raise
1456 dd = 0
1209 dd = 0
1457
1210
1458 try:
1211 try:
1459 f = self.opener(self.indexfile)
1212 f = self.opener(self.indexfile)
1460 f.seek(0, 2)
1213 f.seek(0, 2)
1461 actual = f.tell()
1214 actual = f.tell()
1462 s = self._io.size
1215 s = self._io.size
1463 i = max(0, actual // s)
1216 i = max(0, actual // s)
1464 di = actual - (i * s)
1217 di = actual - (i * s)
1465 if self._inline:
1218 if self._inline:
1466 databytes = 0
1219 databytes = 0
1467 for r in self:
1220 for r in self:
1468 databytes += max(0, self.length(r))
1221 databytes += max(0, self.length(r))
1469 dd = 0
1222 dd = 0
1470 di = actual - len(self) * s - databytes
1223 di = actual - len(self) * s - databytes
1471 except IOError, inst:
1224 except IOError, inst:
1472 if inst.errno != errno.ENOENT:
1225 if inst.errno != errno.ENOENT:
1473 raise
1226 raise
1474 di = 0
1227 di = 0
1475
1228
1476 return (dd, di)
1229 return (dd, di)
1477
1230
1478 def files(self):
1231 def files(self):
1479 res = [self.indexfile]
1232 res = [self.indexfile]
1480 if not self._inline:
1233 if not self._inline:
1481 res.append(self.datafile)
1234 res.append(self.datafile)
1482 return res
1235 return res
@@ -1,146 +1,145 b''
1 # statichttprepo.py - simple http repository class for mercurial
1 # statichttprepo.py - simple http repository class for mercurial
2 #
2 #
3 # This provides read-only repo access to repositories exported via static http
3 # This provides read-only repo access to repositories exported via static http
4 #
4 #
5 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 from i18n import _
10 from i18n import _
11 import changelog, byterange, url, error
11 import changelog, byterange, url, error
12 import localrepo, manifest, util, store
12 import localrepo, manifest, util, store
13 import urllib, urllib2, errno
13 import urllib, urllib2, errno
14
14
15 class httprangereader(object):
15 class httprangereader(object):
16 def __init__(self, url, opener):
16 def __init__(self, url, opener):
17 # we assume opener has HTTPRangeHandler
17 # we assume opener has HTTPRangeHandler
18 self.url = url
18 self.url = url
19 self.pos = 0
19 self.pos = 0
20 self.opener = opener
20 self.opener = opener
21 self.name = url
21 self.name = url
22 def seek(self, pos):
22 def seek(self, pos):
23 self.pos = pos
23 self.pos = pos
24 def read(self, bytes=None):
24 def read(self, bytes=None):
25 req = urllib2.Request(self.url)
25 req = urllib2.Request(self.url)
26 end = ''
26 end = ''
27 if bytes:
27 if bytes:
28 end = self.pos + bytes - 1
28 end = self.pos + bytes - 1
29 req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
29 req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
30
30
31 try:
31 try:
32 f = self.opener.open(req)
32 f = self.opener.open(req)
33 data = f.read()
33 data = f.read()
34 if hasattr(f, 'getcode'):
34 if hasattr(f, 'getcode'):
35 # python 2.6+
35 # python 2.6+
36 code = f.getcode()
36 code = f.getcode()
37 elif hasattr(f, 'code'):
37 elif hasattr(f, 'code'):
38 # undocumented attribute, seems to be set in 2.4 and 2.5
38 # undocumented attribute, seems to be set in 2.4 and 2.5
39 code = f.code
39 code = f.code
40 else:
40 else:
41 # Don't know how to check, hope for the best.
41 # Don't know how to check, hope for the best.
42 code = 206
42 code = 206
43 except urllib2.HTTPError, inst:
43 except urllib2.HTTPError, inst:
44 num = inst.code == 404 and errno.ENOENT or None
44 num = inst.code == 404 and errno.ENOENT or None
45 raise IOError(num, inst)
45 raise IOError(num, inst)
46 except urllib2.URLError, inst:
46 except urllib2.URLError, inst:
47 raise IOError(None, inst.reason[1])
47 raise IOError(None, inst.reason[1])
48
48
49 if code == 200:
49 if code == 200:
50 # HTTPRangeHandler does nothing if remote does not support
50 # HTTPRangeHandler does nothing if remote does not support
51 # Range headers and returns the full entity. Let's slice it.
51 # Range headers and returns the full entity. Let's slice it.
52 if bytes:
52 if bytes:
53 data = data[self.pos:self.pos + bytes]
53 data = data[self.pos:self.pos + bytes]
54 else:
54 else:
55 data = data[self.pos:]
55 data = data[self.pos:]
56 elif bytes:
56 elif bytes:
57 data = data[:bytes]
57 data = data[:bytes]
58 self.pos += len(data)
58 self.pos += len(data)
59 return data
59 return data
60 def __iter__(self):
60 def __iter__(self):
61 return iter(self.read().splitlines(1))
61 return iter(self.read().splitlines(1))
62 def close(self):
62 def close(self):
63 pass
63 pass
64
64
65 def build_opener(ui, authinfo):
65 def build_opener(ui, authinfo):
66 # urllib cannot handle URLs with embedded user or passwd
66 # urllib cannot handle URLs with embedded user or passwd
67 urlopener = url.opener(ui, authinfo)
67 urlopener = url.opener(ui, authinfo)
68 urlopener.add_handler(byterange.HTTPRangeHandler())
68 urlopener.add_handler(byterange.HTTPRangeHandler())
69
69
70 def opener(base):
70 def opener(base):
71 """return a function that opens files over http"""
71 """return a function that opens files over http"""
72 p = base
72 p = base
73 def o(path, mode="r", atomictemp=None):
73 def o(path, mode="r", atomictemp=None):
74 if 'a' in mode or 'w' in mode:
74 if 'a' in mode or 'w' in mode:
75 raise IOError('Permission denied')
75 raise IOError('Permission denied')
76 f = "/".join((p, urllib.quote(path)))
76 f = "/".join((p, urllib.quote(path)))
77 return httprangereader(f, urlopener)
77 return httprangereader(f, urlopener)
78 return o
78 return o
79
79
80 opener.options = {'nonlazy': 1}
81 return opener
80 return opener
82
81
83 class statichttprepository(localrepo.localrepository):
82 class statichttprepository(localrepo.localrepository):
84 def __init__(self, ui, path):
83 def __init__(self, ui, path):
85 self._url = path
84 self._url = path
86 self.ui = ui
85 self.ui = ui
87
86
88 self.root = path
87 self.root = path
89 self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
88 self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
90
89
91 opener = build_opener(ui, authinfo)
90 opener = build_opener(ui, authinfo)
92 self.opener = opener(self.path)
91 self.opener = opener(self.path)
93
92
94 # find requirements
93 # find requirements
95 try:
94 try:
96 requirements = self.opener("requires").read().splitlines()
95 requirements = self.opener("requires").read().splitlines()
97 except IOError, inst:
96 except IOError, inst:
98 if inst.errno != errno.ENOENT:
97 if inst.errno != errno.ENOENT:
99 raise
98 raise
100 # check if it is a non-empty old-style repository
99 # check if it is a non-empty old-style repository
101 try:
100 try:
102 self.opener("00changelog.i").read(1)
101 self.opener("00changelog.i").read(1)
103 except IOError, inst:
102 except IOError, inst:
104 if inst.errno != errno.ENOENT:
103 if inst.errno != errno.ENOENT:
105 raise
104 raise
106 # we do not care about empty old-style repositories here
105 # we do not care about empty old-style repositories here
107 msg = _("'%s' does not appear to be an hg repository") % path
106 msg = _("'%s' does not appear to be an hg repository") % path
108 raise error.RepoError(msg)
107 raise error.RepoError(msg)
109 requirements = []
108 requirements = []
110
109
111 # check them
110 # check them
112 for r in requirements:
111 for r in requirements:
113 if r not in self.supported:
112 if r not in self.supported:
114 raise error.RepoError(_("requirement '%s' not supported") % r)
113 raise error.RepoError(_("requirement '%s' not supported") % r)
115
114
116 # setup store
115 # setup store
117 def pjoin(a, b):
116 def pjoin(a, b):
118 return a + '/' + b
117 return a + '/' + b
119 self.store = store.store(requirements, self.path, opener, pjoin)
118 self.store = store.store(requirements, self.path, opener, pjoin)
120 self.spath = self.store.path
119 self.spath = self.store.path
121 self.sopener = self.store.opener
120 self.sopener = self.store.opener
122 self.sjoin = self.store.join
121 self.sjoin = self.store.join
123
122
124 self.manifest = manifest.manifest(self.sopener)
123 self.manifest = manifest.manifest(self.sopener)
125 self.changelog = changelog.changelog(self.sopener)
124 self.changelog = changelog.changelog(self.sopener)
126 self._tags = None
125 self._tags = None
127 self.nodetagscache = None
126 self.nodetagscache = None
128 self._branchcache = None
127 self._branchcache = None
129 self._branchcachetip = None
128 self._branchcachetip = None
130 self.encodepats = None
129 self.encodepats = None
131 self.decodepats = None
130 self.decodepats = None
132 self.capabilities.remove("pushkey")
131 self.capabilities.remove("pushkey")
133
132
134 def url(self):
133 def url(self):
135 return self._url
134 return self._url
136
135
137 def local(self):
136 def local(self):
138 return False
137 return False
139
138
140 def lock(self, wait=True):
139 def lock(self, wait=True):
141 raise util.Abort(_('cannot lock static-http repository'))
140 raise util.Abort(_('cannot lock static-http repository'))
142
141
143 def instance(ui, path, create):
142 def instance(ui, path, create):
144 if create:
143 if create:
145 raise util.Abort(_('cannot create new static-http repository'))
144 raise util.Abort(_('cannot create new static-http repository'))
146 return statichttprepository(ui, path[7:])
145 return statichttprepository(ui, path[7:])
@@ -1,113 +1,113 b''
1 from mercurial import parsers
1 from mercurial import parsers
2 from mercurial.node import nullid, nullrev
2 from mercurial.node import nullid, nullrev
3 import struct
3 import struct
4
4
5 # This unit test compares the return value of the original Python
5 # This unit test compares the return value of the original Python
6 # implementation of parseindex and the new C implementation for
6 # implementation of parseindex and the new C implementation for
7 # an index file with and without inlined data
7 # an index file with and without inlined data
8
8
9 # original python implementation
9 # original python implementation
10 def gettype(q):
10 def gettype(q):
11 return int(q & 0xFFFF)
11 return int(q & 0xFFFF)
12
12
13 def offset_type(offset, type):
13 def offset_type(offset, type):
14 return long(long(offset) << 16 | type)
14 return long(long(offset) << 16 | type)
15
15
16 indexformatng = ">Qiiiiii20s12x"
16 indexformatng = ">Qiiiiii20s12x"
17
17
18 def py_parseindex(data, inline) :
18 def py_parseindex(data, inline) :
19 s = 64
19 s = 64
20 cache = None
20 cache = None
21 index = []
21 index = []
22 nodemap = {nullid: nullrev}
22 nodemap = {nullid: nullrev}
23 n = off = 0
23 n = off = 0
24 # if we're not using lazymap, always read the whole index
24
25 l = len(data) - s
25 l = len(data) - s
26 append = index.append
26 append = index.append
27 if inline:
27 if inline:
28 cache = (0, data)
28 cache = (0, data)
29 while off <= l:
29 while off <= l:
30 e = struct.unpack(indexformatng, data[off:off + s])
30 e = struct.unpack(indexformatng, data[off:off + s])
31 nodemap[e[7]] = n
31 nodemap[e[7]] = n
32 append(e)
32 append(e)
33 n += 1
33 n += 1
34 if e[1] < 0:
34 if e[1] < 0:
35 break
35 break
36 off += e[1] + s
36 off += e[1] + s
37 else:
37 else:
38 while off <= l:
38 while off <= l:
39 e = struct.unpack(indexformatng, data[off:off + s])
39 e = struct.unpack(indexformatng, data[off:off + s])
40 nodemap[e[7]] = n
40 nodemap[e[7]] = n
41 append(e)
41 append(e)
42 n += 1
42 n += 1
43 off += s
43 off += s
44
44
45 e = list(index[0])
45 e = list(index[0])
46 type = gettype(e[0])
46 type = gettype(e[0])
47 e[0] = offset_type(0, type)
47 e[0] = offset_type(0, type)
48 index[0] = tuple(e)
48 index[0] = tuple(e)
49
49
50 # add the magic null revision at -1
50 # add the magic null revision at -1
51 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
51 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
52
52
53 return index, nodemap, cache
53 return index, nodemap, cache
54
54
55
55
56 data_inlined = '\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c' \
56 data_inlined = '\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c' \
57 '\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff' \
57 '\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff' \
58 '\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b' \
58 '\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b' \
59 'w\xdduR\xda\xc6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
59 'w\xdduR\xda\xc6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
60 'x\x9c\x9d\x93?O\xc30\x10\xc5\xf7|\x8a\xdb\x9a\xa8m\x06\xd8*\x95' \
60 'x\x9c\x9d\x93?O\xc30\x10\xc5\xf7|\x8a\xdb\x9a\xa8m\x06\xd8*\x95' \
61 '\x81B\xa1\xa2\xa2R\xcb\x86Pd\x9a\x0b5$vd_\x04\xfd\xf6\x9c\xff@' \
61 '\x81B\xa1\xa2\xa2R\xcb\x86Pd\x9a\x0b5$vd_\x04\xfd\xf6\x9c\xff@' \
62 '\x11!\x0b\xd9\xec\xf7\xbbw\xe7gG6\xad6\x04\xdaN\xc0\x92\xa0$)' \
62 '\x11!\x0b\xd9\xec\xf7\xbbw\xe7gG6\xad6\x04\xdaN\xc0\x92\xa0$)' \
63 '\xb1\x82\xa2\xd1%\x16\xa4\x8b7\xa9\xca\xd4-\xb2Y\x02\xfc\xc9' \
63 '\xb1\x82\xa2\xd1%\x16\xa4\x8b7\xa9\xca\xd4-\xb2Y\x02\xfc\xc9' \
64 '\xcaS\xf9\xaeX\xed\xb6\xd77Q\x02\x83\xd4\x19\xf5--Y\xea\xe1W' \
64 '\xcaS\xf9\xaeX\xed\xb6\xd77Q\x02\x83\xd4\x19\xf5--Y\xea\xe1W' \
65 '\xab\xed\x10\xceR\x0f_\xdf\xdf\r\xe1,\xf5\xf0\xcb\xf5 \xceR\x0f' \
65 '\xab\xed\x10\xceR\x0f_\xdf\xdf\r\xe1,\xf5\xf0\xcb\xf5 \xceR\x0f' \
66 '_\xdc\x0e\x0e\xc3R\x0f_\xae\x96\x9b!\x9e\xa5\x1e\xbf\xdb,\x06' \
66 '_\xdc\x0e\x0e\xc3R\x0f_\xae\x96\x9b!\x9e\xa5\x1e\xbf\xdb,\x06' \
67 '\xc7q\x9a/\x88\x82\xc3B\xea\xb5\xb4TJ\x93\xb6\x82\x0e\xe16\xe6' \
67 '\xc7q\x9a/\x88\x82\xc3B\xea\xb5\xb4TJ\x93\xb6\x82\x0e\xe16\xe6' \
68 'KQ\xdb\xaf\xecG\xa3\xd1 \x01\xd3\x0b_^\xe8\xaa\xa0\xae\xad\xd1' \
68 'KQ\xdb\xaf\xecG\xa3\xd1 \x01\xd3\x0b_^\xe8\xaa\xa0\xae\xad\xd1' \
69 '&\xbef\x1bz\x08\xb0|\xc9Xz\x06\xf6Z\x91\x90J\xaa\x17\x90\xaa' \
69 '&\xbef\x1bz\x08\xb0|\xc9Xz\x06\xf6Z\x91\x90J\xaa\x17\x90\xaa' \
70 '\xd2\xa6\x11$5C\xcf\xba#\xa0\x03\x02*2\x92-\xfc\xb1\x94\xdf\xe2' \
70 '\xd2\xa6\x11$5C\xcf\xba#\xa0\x03\x02*2\x92-\xfc\xb1\x94\xdf\xe2' \
71 '\xae\xb8\'m\x8ey0^\x85\xd3\x82\xb4\xf0`:\x9c\x00\x8a\xfd\x01' \
71 '\xae\xb8\'m\x8ey0^\x85\xd3\x82\xb4\xf0`:\x9c\x00\x8a\xfd\x01' \
72 '\xb0\xc6\x86\x8b\xdd\xae\x80\xf3\xa9\x9fd\x16\n\x00R%\x1a\x06' \
72 '\xb0\xc6\x86\x8b\xdd\xae\x80\xf3\xa9\x9fd\x16\n\x00R%\x1a\x06' \
73 '\xe9\xd8b\x98\x1d\xf4\xf3+\x9bf\x01\xd8p\x1b\xf3.\xed\x9f^g\xc3' \
73 '\xe9\xd8b\x98\x1d\xf4\xf3+\x9bf\x01\xd8p\x1b\xf3.\xed\x9f^g\xc3' \
74 '^\xd9W81T\xdb\xd5\x04sx|\xf2\xeb\xd6`%?x\xed"\x831\xbf\xf3\xdc' \
74 '^\xd9W81T\xdb\xd5\x04sx|\xf2\xeb\xd6`%?x\xed"\x831\xbf\xf3\xdc' \
75 'b\xeb%gaY\xe1\xad\x9f\xb9f\'1w\xa9\xa5a\x83s\x82J\xb98\xbc4\x8b' \
75 'b\xeb%gaY\xe1\xad\x9f\xb9f\'1w\xa9\xa5a\x83s\x82J\xb98\xbc4\x8b' \
76 '\x83\x00\x9f$z\xb8#\xa5\xb1\xdf\x98\xd9\xec\x1b\x89O\xe3Ts\x9a4' \
76 '\x83\x00\x9f$z\xb8#\xa5\xb1\xdf\x98\xd9\xec\x1b\x89O\xe3Ts\x9a4' \
77 '\x17m\x8b\xfc\x8f\xa5\x95\x9a\xfc\xfa\xed,\xe5|\xa1\xfe\x15\xb9' \
77 '\x17m\x8b\xfc\x8f\xa5\x95\x9a\xfc\xfa\xed,\xe5|\xa1\xfe\x15\xb9' \
78 '\xbc\xb2\x93\x1f\xf2\x95\xff\xdf,\x1a\xc5\xe7\x17*\x93Oz:>\x0e'
78 '\xbc\xb2\x93\x1f\xf2\x95\xff\xdf,\x1a\xc5\xe7\x17*\x93Oz:>\x0e'
79
79
80 data_non_inlined = '\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19' \
80 data_non_inlined = '\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19' \
81 '\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff' \
81 '\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff' \
82 '\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d' \
82 '\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d' \
83 '\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
83 '\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
84 '\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00' \
84 '\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00' \
85 '\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff' \
85 '\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff' \
86 '\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh' \
86 '\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh' \
87 '\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
87 '\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
88 '\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00' \
88 '\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00' \
89 '\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n' \
89 '\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n' \
90 '\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00' \
90 '\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00' \
91 '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F' \
91 '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F' \
92 '\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01' \
92 '\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01' \
93 '\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1' \
93 '\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1' \
94 '\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00' \
94 '\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00' \
95 '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
95 '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
96
96
97 def runtest() :
97 def runtest() :
98
98
99 py_res_1 = py_parseindex(data_inlined, True)
99 py_res_1 = py_parseindex(data_inlined, True)
100 c_res_1 = parsers.parse_index(data_inlined, True)
100 c_res_1 = parsers.parse_index(data_inlined, True)
101
101
102 py_res_2 = py_parseindex(data_non_inlined, False)
102 py_res_2 = py_parseindex(data_non_inlined, False)
103 c_res_2 = parsers.parse_index(data_non_inlined, False)
103 c_res_2 = parsers.parse_index(data_non_inlined, False)
104
104
105 if py_res_1 != c_res_1:
105 if py_res_1 != c_res_1:
106 print "Parse index result (with inlined data) differs!"
106 print "Parse index result (with inlined data) differs!"
107
107
108 if py_res_2 != c_res_2:
108 if py_res_2 != c_res_2:
109 print "Parse index result (no inlined data) differs!"
109 print "Parse index result (no inlined data) differs!"
110
110
111 print "done"
111 print "done"
112
112
113 runtest()
113 runtest()
General Comments 0
You need to be logged in to leave comments. Login now