##// END OF EJS Templates
match: change all users of util.matcher to match.match
Matt Mackall -
r8566:744d6322 default
parent child Browse files
Show More
@@ -1,90 +1,91 b''
1 # acl.py - changeset access control for mercurial
1 # acl.py - changeset access control for mercurial
2 #
2 #
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.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, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7 #
7 #
8 # this hook allows to allow or deny access to parts of a repo when
8 # this hook allows to allow or deny access to parts of a repo when
9 # taking incoming changesets.
9 # taking incoming changesets.
10 #
10 #
11 # authorization is against local user name on system where hook is
11 # authorization is against local user name on system where hook is
12 # run, not committer of original changeset (since that is easy to
12 # run, not committer of original changeset (since that is easy to
13 # spoof).
13 # spoof).
14 #
14 #
15 # acl hook is best to use if you use hgsh to set up restricted shells
15 # acl hook is best to use if you use hgsh to set up restricted shells
16 # for authenticated users to only push to / pull from. not safe if
16 # for authenticated users to only push to / pull from. not safe if
17 # user has interactive shell access, because they can disable hook.
17 # user has interactive shell access, because they can disable hook.
18 # also not safe if remote users share one local account, because then
18 # also not safe if remote users share one local account, because then
19 # no way to tell remote users apart.
19 # no way to tell remote users apart.
20 #
20 #
21 # to use, configure acl extension in hgrc like this:
21 # to use, configure acl extension in hgrc like this:
22 #
22 #
23 # [extensions]
23 # [extensions]
24 # hgext.acl =
24 # hgext.acl =
25 #
25 #
26 # [hooks]
26 # [hooks]
27 # pretxnchangegroup.acl = python:hgext.acl.hook
27 # pretxnchangegroup.acl = python:hgext.acl.hook
28 #
28 #
29 # [acl]
29 # [acl]
30 # sources = serve # check if source of incoming changes in this list
30 # sources = serve # check if source of incoming changes in this list
31 # # ("serve" == ssh or http, "push", "pull", "bundle")
31 # # ("serve" == ssh or http, "push", "pull", "bundle")
32 #
32 #
33 # allow and deny lists have subtree pattern (default syntax is glob)
33 # allow and deny lists have subtree pattern (default syntax is glob)
34 # on left, user names on right. deny list checked before allow list.
34 # on left, user names on right. deny list checked before allow list.
35 #
35 #
36 # [acl.allow]
36 # [acl.allow]
37 # # if acl.allow not present, all users allowed by default
37 # # if acl.allow not present, all users allowed by default
38 # # empty acl.allow = no users allowed
38 # # empty acl.allow = no users allowed
39 # docs/** = doc_writer
39 # docs/** = doc_writer
40 # .hgtags = release_engineer
40 # .hgtags = release_engineer
41 #
41 #
42 # [acl.deny]
42 # [acl.deny]
43 # # if acl.deny not present, no users denied by default
43 # # if acl.deny not present, no users denied by default
44 # # empty acl.deny = all users allowed
44 # # empty acl.deny = all users allowed
45 # glob pattern = user4, user5
45 # glob pattern = user4, user5
46 # ** = user6
46 # ** = user6
47
47
48 from mercurial.i18n import _
48 from mercurial.i18n import _
49 from mercurial import util
49 from mercurial import util, match
50 import getpass
50 import getpass
51
51
52 def buildmatch(ui, repo, user, key):
52 def buildmatch(ui, repo, user, key):
53 '''return tuple of (match function, list enabled).'''
53 '''return tuple of (match function, list enabled).'''
54 if not ui.has_section(key):
54 if not ui.has_section(key):
55 ui.debug(_('acl: %s not enabled\n') % key)
55 ui.debug(_('acl: %s not enabled\n') % key)
56 return None
56 return None
57
57
58 pats = [pat for pat, users in ui.configitems(key)
58 pats = [pat for pat, users in ui.configitems(key)
59 if user in users.replace(',', ' ').split()]
59 if user in users.replace(',', ' ').split()]
60 ui.debug(_('acl: %s enabled, %d entries for user %s\n') %
60 ui.debug(_('acl: %s enabled, %d entries for user %s\n') %
61 (key, len(pats), user))
61 (key, len(pats), user))
62 if pats:
62 if pats:
63 return util.matcher(repo.root, names=pats)[1]
63 return match.match(repo.root, '', pats, [], [], 'glob')
64 return util.never
64 return match.never(repo.root, '')
65
65
66
66 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
67 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
67 if hooktype != 'pretxnchangegroup':
68 if hooktype != 'pretxnchangegroup':
68 raise util.Abort(_('config error - hook type "%s" cannot stop '
69 raise util.Abort(_('config error - hook type "%s" cannot stop '
69 'incoming changesets') % hooktype)
70 'incoming changesets') % hooktype)
70 if source not in ui.config('acl', 'sources', 'serve').split():
71 if source not in ui.config('acl', 'sources', 'serve').split():
71 ui.debug(_('acl: changes have source "%s" - skipping\n') % source)
72 ui.debug(_('acl: changes have source "%s" - skipping\n') % source)
72 return
73 return
73
74
74 user = getpass.getuser()
75 user = getpass.getuser()
75 cfg = ui.config('acl', 'config')
76 cfg = ui.config('acl', 'config')
76 if cfg:
77 if cfg:
77 ui.readconfig(cfg, sections = ['acl.allow', 'acl.deny'])
78 ui.readconfig(cfg, sections = ['acl.allow', 'acl.deny'])
78 allow = buildmatch(ui, repo, user, 'acl.allow')
79 allow = buildmatch(ui, repo, user, 'acl.allow')
79 deny = buildmatch(ui, repo, user, 'acl.deny')
80 deny = buildmatch(ui, repo, user, 'acl.deny')
80
81
81 for rev in xrange(repo[node], len(repo)):
82 for rev in xrange(repo[node], len(repo)):
82 ctx = repo[rev]
83 ctx = repo[rev]
83 for f in ctx.files():
84 for f in ctx.files():
84 if deny and deny(f):
85 if deny and deny(f):
85 ui.debug(_('acl: user %s denied on %s\n') % (user, f))
86 ui.debug(_('acl: user %s denied on %s\n') % (user, f))
86 raise util.Abort(_('acl: access denied for changeset %s') % ctx)
87 raise util.Abort(_('acl: access denied for changeset %s') % ctx)
87 if allow and not allow(f):
88 if allow and not allow(f):
88 ui.debug(_('acl: user %s not allowed on %s\n') % (user, f))
89 ui.debug(_('acl: user %s not allowed on %s\n') % (user, f))
89 raise util.Abort(_('acl: access denied for changeset %s') % ctx)
90 raise util.Abort(_('acl: access denied for changeset %s') % ctx)
90 ui.debug(_('acl: allowing changeset %s\n') % ctx)
91 ui.debug(_('acl: allowing changeset %s\n') % ctx)
@@ -1,534 +1,534 b''
1 # keyword.py - $Keyword$ expansion for Mercurial
1 # keyword.py - $Keyword$ expansion for Mercurial
2 #
2 #
3 # Copyright 2007, 2008 Christian Ebert <blacktrash@gmx.net>
3 # Copyright 2007, 2008 Christian Ebert <blacktrash@gmx.net>
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, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7 #
7 #
8 # $Id$
8 # $Id$
9 #
9 #
10 # Keyword expansion hack against the grain of a DSCM
10 # Keyword expansion hack against the grain of a DSCM
11 #
11 #
12 # There are many good reasons why this is not needed in a distributed
12 # There are many good reasons why this is not needed in a distributed
13 # SCM, still it may be useful in very small projects based on single
13 # SCM, still it may be useful in very small projects based on single
14 # files (like LaTeX packages), that are mostly addressed to an
14 # files (like LaTeX packages), that are mostly addressed to an
15 # audience not running a version control system.
15 # audience not running a version control system.
16 #
16 #
17 # For in-depth discussion refer to
17 # For in-depth discussion refer to
18 # <http://www.selenic.com/mercurial/wiki/index.cgi/KeywordPlan>.
18 # <http://www.selenic.com/mercurial/wiki/index.cgi/KeywordPlan>.
19 #
19 #
20 # Keyword expansion is based on Mercurial's changeset template mappings.
20 # Keyword expansion is based on Mercurial's changeset template mappings.
21 #
21 #
22 # Binary files are not touched.
22 # Binary files are not touched.
23 #
23 #
24 # Setup in hgrc:
24 # Setup in hgrc:
25 #
25 #
26 # [extensions]
26 # [extensions]
27 # # enable extension
27 # # enable extension
28 # hgext.keyword =
28 # hgext.keyword =
29 #
29 #
30 # Files to act upon/ignore are specified in the [keyword] section.
30 # Files to act upon/ignore are specified in the [keyword] section.
31 # Customized keyword template mappings in the [keywordmaps] section.
31 # Customized keyword template mappings in the [keywordmaps] section.
32 #
32 #
33 # Run "hg help keyword" and "hg kwdemo" to get info on configuration.
33 # Run "hg help keyword" and "hg kwdemo" to get info on configuration.
34
34
35 '''keyword expansion in local repositories
35 '''keyword expansion in local repositories
36
36
37 This extension expands RCS/CVS-like or self-customized $Keywords$ in
37 This extension expands RCS/CVS-like or self-customized $Keywords$ in
38 tracked text files selected by your configuration.
38 tracked text files selected by your configuration.
39
39
40 Keywords are only expanded in local repositories and not stored in the
40 Keywords are only expanded in local repositories and not stored in the
41 change history. The mechanism can be regarded as a convenience for the
41 change history. The mechanism can be regarded as a convenience for the
42 current user or for archive distribution.
42 current user or for archive distribution.
43
43
44 Configuration is done in the [keyword] and [keywordmaps] sections of
44 Configuration is done in the [keyword] and [keywordmaps] sections of
45 hgrc files.
45 hgrc files.
46
46
47 Example:
47 Example:
48
48
49 [keyword]
49 [keyword]
50 # expand keywords in every python file except those matching "x*"
50 # expand keywords in every python file except those matching "x*"
51 **.py =
51 **.py =
52 x* = ignore
52 x* = ignore
53
53
54 Note: the more specific you are in your filename patterns
54 Note: the more specific you are in your filename patterns
55 the less you lose speed in huge repositories.
55 the less you lose speed in huge repositories.
56
56
57 For [keywordmaps] template mapping and expansion demonstration and
57 For [keywordmaps] template mapping and expansion demonstration and
58 control run "hg kwdemo".
58 control run "hg kwdemo".
59
59
60 An additional date template filter {date|utcdate} is provided.
60 An additional date template filter {date|utcdate} is provided.
61
61
62 The default template mappings (view with "hg kwdemo -d") can be
62 The default template mappings (view with "hg kwdemo -d") can be
63 replaced with customized keywords and templates. Again, run "hg
63 replaced with customized keywords and templates. Again, run "hg
64 kwdemo" to control the results of your config changes.
64 kwdemo" to control the results of your config changes.
65
65
66 Before changing/disabling active keywords, run "hg kwshrink" to avoid
66 Before changing/disabling active keywords, run "hg kwshrink" to avoid
67 the risk of inadvertedly storing expanded keywords in the change
67 the risk of inadvertedly storing expanded keywords in the change
68 history.
68 history.
69
69
70 To force expansion after enabling it, or a configuration change, run
70 To force expansion after enabling it, or a configuration change, run
71 "hg kwexpand".
71 "hg kwexpand".
72
72
73 Also, when committing with the record extension or using mq's qrecord,
73 Also, when committing with the record extension or using mq's qrecord,
74 be aware that keywords cannot be updated. Again, run "hg kwexpand" on
74 be aware that keywords cannot be updated. Again, run "hg kwexpand" on
75 the files in question to update keyword expansions after all changes
75 the files in question to update keyword expansions after all changes
76 have been checked in.
76 have been checked in.
77
77
78 Expansions spanning more than one line and incremental expansions,
78 Expansions spanning more than one line and incremental expansions,
79 like CVS' $Log$, are not supported. A keyword template map
79 like CVS' $Log$, are not supported. A keyword template map
80 "Log = {desc}" expands to the first line of the changeset description.
80 "Log = {desc}" expands to the first line of the changeset description.
81 '''
81 '''
82
82
83 from mercurial import commands, cmdutil, dispatch, filelog, revlog, extensions
83 from mercurial import commands, cmdutil, dispatch, filelog, revlog, extensions
84 from mercurial import patch, localrepo, templater, templatefilters, util
84 from mercurial import patch, localrepo, templater, templatefilters, util, match
85 from mercurial.hgweb import webcommands
85 from mercurial.hgweb import webcommands
86 from mercurial.lock import release
86 from mercurial.lock import release
87 from mercurial.node import nullid, hex
87 from mercurial.node import nullid, hex
88 from mercurial.i18n import _
88 from mercurial.i18n import _
89 import re, shutil, tempfile, time
89 import re, shutil, tempfile, time
90
90
91 commands.optionalrepo += ' kwdemo'
91 commands.optionalrepo += ' kwdemo'
92
92
93 # hg commands that do not act on keywords
93 # hg commands that do not act on keywords
94 nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
94 nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
95 ' log outgoing push rename rollback tip verify'
95 ' log outgoing push rename rollback tip verify'
96 ' convert email glog')
96 ' convert email glog')
97
97
98 # hg commands that trigger expansion only when writing to working dir,
98 # hg commands that trigger expansion only when writing to working dir,
99 # not when reading filelog, and unexpand when reading from working dir
99 # not when reading filelog, and unexpand when reading from working dir
100 restricted = 'merge record resolve qfold qimport qnew qpush qrefresh qrecord'
100 restricted = 'merge record resolve qfold qimport qnew qpush qrefresh qrecord'
101
101
102 def utcdate(date):
102 def utcdate(date):
103 '''Returns hgdate in cvs-like UTC format.'''
103 '''Returns hgdate in cvs-like UTC format.'''
104 return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
104 return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
105
105
106 # make keyword tools accessible
106 # make keyword tools accessible
107 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']}
107 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']}
108
108
109
109
110 class kwtemplater(object):
110 class kwtemplater(object):
111 '''
111 '''
112 Sets up keyword templates, corresponding keyword regex, and
112 Sets up keyword templates, corresponding keyword regex, and
113 provides keyword substitution functions.
113 provides keyword substitution functions.
114 '''
114 '''
115 templates = {
115 templates = {
116 'Revision': '{node|short}',
116 'Revision': '{node|short}',
117 'Author': '{author|user}',
117 'Author': '{author|user}',
118 'Date': '{date|utcdate}',
118 'Date': '{date|utcdate}',
119 'RCSFile': '{file|basename},v',
119 'RCSFile': '{file|basename},v',
120 'Source': '{root}/{file},v',
120 'Source': '{root}/{file},v',
121 'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
121 'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
122 'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
122 'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
123 }
123 }
124
124
125 def __init__(self, ui, repo):
125 def __init__(self, ui, repo):
126 self.ui = ui
126 self.ui = ui
127 self.repo = repo
127 self.repo = repo
128 self.matcher = util.matcher(repo.root,
128 self.matcher = match.match(repo.root, '', [],
129 inc=kwtools['inc'], exc=kwtools['exc'])[1]
129 kwtools['inc'], kwtools['exc'], 'glob')
130 self.restrict = kwtools['hgcmd'] in restricted.split()
130 self.restrict = kwtools['hgcmd'] in restricted.split()
131
131
132 kwmaps = self.ui.configitems('keywordmaps')
132 kwmaps = self.ui.configitems('keywordmaps')
133 if kwmaps: # override default templates
133 if kwmaps: # override default templates
134 kwmaps = [(k, templater.parsestring(v, False))
134 kwmaps = [(k, templater.parsestring(v, False))
135 for (k, v) in kwmaps]
135 for (k, v) in kwmaps]
136 self.templates = dict(kwmaps)
136 self.templates = dict(kwmaps)
137 escaped = map(re.escape, self.templates.keys())
137 escaped = map(re.escape, self.templates.keys())
138 kwpat = r'\$(%s)(: [^$\n\r]*? )??\$' % '|'.join(escaped)
138 kwpat = r'\$(%s)(: [^$\n\r]*? )??\$' % '|'.join(escaped)
139 self.re_kw = re.compile(kwpat)
139 self.re_kw = re.compile(kwpat)
140
140
141 templatefilters.filters['utcdate'] = utcdate
141 templatefilters.filters['utcdate'] = utcdate
142 self.ct = cmdutil.changeset_templater(self.ui, self.repo,
142 self.ct = cmdutil.changeset_templater(self.ui, self.repo,
143 False, None, '', False)
143 False, None, '', False)
144
144
145 def substitute(self, data, path, ctx, subfunc):
145 def substitute(self, data, path, ctx, subfunc):
146 '''Replaces keywords in data with expanded template.'''
146 '''Replaces keywords in data with expanded template.'''
147 def kwsub(mobj):
147 def kwsub(mobj):
148 kw = mobj.group(1)
148 kw = mobj.group(1)
149 self.ct.use_template(self.templates[kw])
149 self.ct.use_template(self.templates[kw])
150 self.ui.pushbuffer()
150 self.ui.pushbuffer()
151 self.ct.show(ctx, root=self.repo.root, file=path)
151 self.ct.show(ctx, root=self.repo.root, file=path)
152 ekw = templatefilters.firstline(self.ui.popbuffer())
152 ekw = templatefilters.firstline(self.ui.popbuffer())
153 return '$%s: %s $' % (kw, ekw)
153 return '$%s: %s $' % (kw, ekw)
154 return subfunc(kwsub, data)
154 return subfunc(kwsub, data)
155
155
156 def expand(self, path, node, data):
156 def expand(self, path, node, data):
157 '''Returns data with keywords expanded.'''
157 '''Returns data with keywords expanded.'''
158 if not self.restrict and self.matcher(path) and not util.binary(data):
158 if not self.restrict and self.matcher(path) and not util.binary(data):
159 ctx = self.repo.filectx(path, fileid=node).changectx()
159 ctx = self.repo.filectx(path, fileid=node).changectx()
160 return self.substitute(data, path, ctx, self.re_kw.sub)
160 return self.substitute(data, path, ctx, self.re_kw.sub)
161 return data
161 return data
162
162
163 def iskwfile(self, path, flagfunc):
163 def iskwfile(self, path, flagfunc):
164 '''Returns true if path matches [keyword] pattern
164 '''Returns true if path matches [keyword] pattern
165 and is not a symbolic link.
165 and is not a symbolic link.
166 Caveat: localrepository._link fails on Windows.'''
166 Caveat: localrepository._link fails on Windows.'''
167 return self.matcher(path) and not 'l' in flagfunc(path)
167 return self.matcher(path) and not 'l' in flagfunc(path)
168
168
169 def overwrite(self, node, expand, files):
169 def overwrite(self, node, expand, files):
170 '''Overwrites selected files expanding/shrinking keywords.'''
170 '''Overwrites selected files expanding/shrinking keywords.'''
171 ctx = self.repo[node]
171 ctx = self.repo[node]
172 mf = ctx.manifest()
172 mf = ctx.manifest()
173 if node is not None: # commit
173 if node is not None: # commit
174 files = [f for f in ctx.files() if f in mf]
174 files = [f for f in ctx.files() if f in mf]
175 notify = self.ui.debug
175 notify = self.ui.debug
176 else: # kwexpand/kwshrink
176 else: # kwexpand/kwshrink
177 notify = self.ui.note
177 notify = self.ui.note
178 candidates = [f for f in files if self.iskwfile(f, ctx.flags)]
178 candidates = [f for f in files if self.iskwfile(f, ctx.flags)]
179 if candidates:
179 if candidates:
180 self.restrict = True # do not expand when reading
180 self.restrict = True # do not expand when reading
181 msg = (expand and _('overwriting %s expanding keywords\n')
181 msg = (expand and _('overwriting %s expanding keywords\n')
182 or _('overwriting %s shrinking keywords\n'))
182 or _('overwriting %s shrinking keywords\n'))
183 for f in candidates:
183 for f in candidates:
184 fp = self.repo.file(f)
184 fp = self.repo.file(f)
185 data = fp.read(mf[f])
185 data = fp.read(mf[f])
186 if util.binary(data):
186 if util.binary(data):
187 continue
187 continue
188 if expand:
188 if expand:
189 if node is None:
189 if node is None:
190 ctx = self.repo.filectx(f, fileid=mf[f]).changectx()
190 ctx = self.repo.filectx(f, fileid=mf[f]).changectx()
191 data, found = self.substitute(data, f, ctx,
191 data, found = self.substitute(data, f, ctx,
192 self.re_kw.subn)
192 self.re_kw.subn)
193 else:
193 else:
194 found = self.re_kw.search(data)
194 found = self.re_kw.search(data)
195 if found:
195 if found:
196 notify(msg % f)
196 notify(msg % f)
197 self.repo.wwrite(f, data, mf.flags(f))
197 self.repo.wwrite(f, data, mf.flags(f))
198 self.repo.dirstate.normal(f)
198 self.repo.dirstate.normal(f)
199 self.restrict = False
199 self.restrict = False
200
200
201 def shrinktext(self, text):
201 def shrinktext(self, text):
202 '''Unconditionally removes all keyword substitutions from text.'''
202 '''Unconditionally removes all keyword substitutions from text.'''
203 return self.re_kw.sub(r'$\1$', text)
203 return self.re_kw.sub(r'$\1$', text)
204
204
205 def shrink(self, fname, text):
205 def shrink(self, fname, text):
206 '''Returns text with all keyword substitutions removed.'''
206 '''Returns text with all keyword substitutions removed.'''
207 if self.matcher(fname) and not util.binary(text):
207 if self.matcher(fname) and not util.binary(text):
208 return self.shrinktext(text)
208 return self.shrinktext(text)
209 return text
209 return text
210
210
211 def shrinklines(self, fname, lines):
211 def shrinklines(self, fname, lines):
212 '''Returns lines with keyword substitutions removed.'''
212 '''Returns lines with keyword substitutions removed.'''
213 if self.matcher(fname):
213 if self.matcher(fname):
214 text = ''.join(lines)
214 text = ''.join(lines)
215 if not util.binary(text):
215 if not util.binary(text):
216 return self.shrinktext(text).splitlines(True)
216 return self.shrinktext(text).splitlines(True)
217 return lines
217 return lines
218
218
219 def wread(self, fname, data):
219 def wread(self, fname, data):
220 '''If in restricted mode returns data read from wdir with
220 '''If in restricted mode returns data read from wdir with
221 keyword substitutions removed.'''
221 keyword substitutions removed.'''
222 return self.restrict and self.shrink(fname, data) or data
222 return self.restrict and self.shrink(fname, data) or data
223
223
224 class kwfilelog(filelog.filelog):
224 class kwfilelog(filelog.filelog):
225 '''
225 '''
226 Subclass of filelog to hook into its read, add, cmp methods.
226 Subclass of filelog to hook into its read, add, cmp methods.
227 Keywords are "stored" unexpanded, and processed on reading.
227 Keywords are "stored" unexpanded, and processed on reading.
228 '''
228 '''
229 def __init__(self, opener, kwt, path):
229 def __init__(self, opener, kwt, path):
230 super(kwfilelog, self).__init__(opener, path)
230 super(kwfilelog, self).__init__(opener, path)
231 self.kwt = kwt
231 self.kwt = kwt
232 self.path = path
232 self.path = path
233
233
234 def read(self, node):
234 def read(self, node):
235 '''Expands keywords when reading filelog.'''
235 '''Expands keywords when reading filelog.'''
236 data = super(kwfilelog, self).read(node)
236 data = super(kwfilelog, self).read(node)
237 return self.kwt.expand(self.path, node, data)
237 return self.kwt.expand(self.path, node, data)
238
238
239 def add(self, text, meta, tr, link, p1=None, p2=None):
239 def add(self, text, meta, tr, link, p1=None, p2=None):
240 '''Removes keyword substitutions when adding to filelog.'''
240 '''Removes keyword substitutions when adding to filelog.'''
241 text = self.kwt.shrink(self.path, text)
241 text = self.kwt.shrink(self.path, text)
242 return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
242 return super(kwfilelog, self).add(text, meta, tr, link, p1, p2)
243
243
244 def cmp(self, node, text):
244 def cmp(self, node, text):
245 '''Removes keyword substitutions for comparison.'''
245 '''Removes keyword substitutions for comparison.'''
246 text = self.kwt.shrink(self.path, text)
246 text = self.kwt.shrink(self.path, text)
247 if self.renamed(node):
247 if self.renamed(node):
248 t2 = super(kwfilelog, self).read(node)
248 t2 = super(kwfilelog, self).read(node)
249 return t2 != text
249 return t2 != text
250 return revlog.revlog.cmp(self, node, text)
250 return revlog.revlog.cmp(self, node, text)
251
251
252 def _status(ui, repo, kwt, unknown, *pats, **opts):
252 def _status(ui, repo, kwt, unknown, *pats, **opts):
253 '''Bails out if [keyword] configuration is not active.
253 '''Bails out if [keyword] configuration is not active.
254 Returns status of working directory.'''
254 Returns status of working directory.'''
255 if kwt:
255 if kwt:
256 matcher = cmdutil.match(repo, pats, opts)
256 matcher = cmdutil.match(repo, pats, opts)
257 return repo.status(match=matcher, unknown=unknown, clean=True)
257 return repo.status(match=matcher, unknown=unknown, clean=True)
258 if ui.configitems('keyword'):
258 if ui.configitems('keyword'):
259 raise util.Abort(_('[keyword] patterns cannot match'))
259 raise util.Abort(_('[keyword] patterns cannot match'))
260 raise util.Abort(_('no [keyword] patterns configured'))
260 raise util.Abort(_('no [keyword] patterns configured'))
261
261
262 def _kwfwrite(ui, repo, expand, *pats, **opts):
262 def _kwfwrite(ui, repo, expand, *pats, **opts):
263 '''Selects files and passes them to kwtemplater.overwrite.'''
263 '''Selects files and passes them to kwtemplater.overwrite.'''
264 if repo.dirstate.parents()[1] != nullid:
264 if repo.dirstate.parents()[1] != nullid:
265 raise util.Abort(_('outstanding uncommitted merge'))
265 raise util.Abort(_('outstanding uncommitted merge'))
266 kwt = kwtools['templater']
266 kwt = kwtools['templater']
267 status = _status(ui, repo, kwt, False, *pats, **opts)
267 status = _status(ui, repo, kwt, False, *pats, **opts)
268 modified, added, removed, deleted = status[:4]
268 modified, added, removed, deleted = status[:4]
269 if modified or added or removed or deleted:
269 if modified or added or removed or deleted:
270 raise util.Abort(_('outstanding uncommitted changes'))
270 raise util.Abort(_('outstanding uncommitted changes'))
271 wlock = lock = None
271 wlock = lock = None
272 try:
272 try:
273 wlock = repo.wlock()
273 wlock = repo.wlock()
274 lock = repo.lock()
274 lock = repo.lock()
275 kwt.overwrite(None, expand, status[6])
275 kwt.overwrite(None, expand, status[6])
276 finally:
276 finally:
277 release(lock, wlock)
277 release(lock, wlock)
278
278
279 def demo(ui, repo, *args, **opts):
279 def demo(ui, repo, *args, **opts):
280 '''print [keywordmaps] configuration and an expansion example
280 '''print [keywordmaps] configuration and an expansion example
281
281
282 Show current, custom, or default keyword template maps and their
282 Show current, custom, or default keyword template maps and their
283 expansion.
283 expansion.
284
284
285 Extend current configuration by specifying maps as arguments and
285 Extend current configuration by specifying maps as arguments and
286 optionally by reading from an additional hgrc file.
286 optionally by reading from an additional hgrc file.
287
287
288 Override current keyword template maps with "default" option.
288 Override current keyword template maps with "default" option.
289 '''
289 '''
290 def demostatus(stat):
290 def demostatus(stat):
291 ui.status(_('\n\t%s\n') % stat)
291 ui.status(_('\n\t%s\n') % stat)
292
292
293 def demoitems(section, items):
293 def demoitems(section, items):
294 ui.write('[%s]\n' % section)
294 ui.write('[%s]\n' % section)
295 for k, v in items:
295 for k, v in items:
296 ui.write('%s = %s\n' % (k, v))
296 ui.write('%s = %s\n' % (k, v))
297
297
298 msg = 'hg keyword config and expansion example'
298 msg = 'hg keyword config and expansion example'
299 kwstatus = 'current'
299 kwstatus = 'current'
300 fn = 'demo.txt'
300 fn = 'demo.txt'
301 branchname = 'demobranch'
301 branchname = 'demobranch'
302 tmpdir = tempfile.mkdtemp('', 'kwdemo.')
302 tmpdir = tempfile.mkdtemp('', 'kwdemo.')
303 ui.note(_('creating temporary repository at %s\n') % tmpdir)
303 ui.note(_('creating temporary repository at %s\n') % tmpdir)
304 repo = localrepo.localrepository(ui, tmpdir, True)
304 repo = localrepo.localrepository(ui, tmpdir, True)
305 ui.setconfig('keyword', fn, '')
305 ui.setconfig('keyword', fn, '')
306 if args or opts.get('rcfile'):
306 if args or opts.get('rcfile'):
307 kwstatus = 'custom'
307 kwstatus = 'custom'
308 if opts.get('rcfile'):
308 if opts.get('rcfile'):
309 ui.readconfig(opts.get('rcfile'))
309 ui.readconfig(opts.get('rcfile'))
310 if opts.get('default'):
310 if opts.get('default'):
311 kwstatus = 'default'
311 kwstatus = 'default'
312 kwmaps = kwtemplater.templates
312 kwmaps = kwtemplater.templates
313 if ui.configitems('keywordmaps'):
313 if ui.configitems('keywordmaps'):
314 # override maps from optional rcfile
314 # override maps from optional rcfile
315 for k, v in kwmaps.iteritems():
315 for k, v in kwmaps.iteritems():
316 ui.setconfig('keywordmaps', k, v)
316 ui.setconfig('keywordmaps', k, v)
317 elif args:
317 elif args:
318 # simulate hgrc parsing
318 # simulate hgrc parsing
319 rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
319 rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
320 fp = repo.opener('hgrc', 'w')
320 fp = repo.opener('hgrc', 'w')
321 fp.writelines(rcmaps)
321 fp.writelines(rcmaps)
322 fp.close()
322 fp.close()
323 ui.readconfig(repo.join('hgrc'))
323 ui.readconfig(repo.join('hgrc'))
324 if not opts.get('default'):
324 if not opts.get('default'):
325 kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
325 kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
326 uisetup(ui)
326 uisetup(ui)
327 reposetup(ui, repo)
327 reposetup(ui, repo)
328 for k, v in ui.configitems('extensions'):
328 for k, v in ui.configitems('extensions'):
329 if k.endswith('keyword'):
329 if k.endswith('keyword'):
330 extension = '%s = %s' % (k, v)
330 extension = '%s = %s' % (k, v)
331 break
331 break
332 demostatus('config using %s keyword template maps' % kwstatus)
332 demostatus('config using %s keyword template maps' % kwstatus)
333 ui.write('[extensions]\n%s\n' % extension)
333 ui.write('[extensions]\n%s\n' % extension)
334 demoitems('keyword', ui.configitems('keyword'))
334 demoitems('keyword', ui.configitems('keyword'))
335 demoitems('keywordmaps', kwmaps.iteritems())
335 demoitems('keywordmaps', kwmaps.iteritems())
336 keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
336 keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
337 repo.wopener(fn, 'w').write(keywords)
337 repo.wopener(fn, 'w').write(keywords)
338 repo.add([fn])
338 repo.add([fn])
339 path = repo.wjoin(fn)
339 path = repo.wjoin(fn)
340 ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
340 ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
341 ui.note(keywords)
341 ui.note(keywords)
342 ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
342 ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
343 # silence branch command if not verbose
343 # silence branch command if not verbose
344 quiet = ui.quiet
344 quiet = ui.quiet
345 ui.quiet = not ui.verbose
345 ui.quiet = not ui.verbose
346 commands.branch(ui, repo, branchname)
346 commands.branch(ui, repo, branchname)
347 ui.quiet = quiet
347 ui.quiet = quiet
348 for name, cmd in ui.configitems('hooks'):
348 for name, cmd in ui.configitems('hooks'):
349 if name.split('.', 1)[0].find('commit') > -1:
349 if name.split('.', 1)[0].find('commit') > -1:
350 repo.ui.setconfig('hooks', name, '')
350 repo.ui.setconfig('hooks', name, '')
351 ui.note(_('unhooked all commit hooks\n'))
351 ui.note(_('unhooked all commit hooks\n'))
352 ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
352 ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
353 repo.commit(text=msg)
353 repo.commit(text=msg)
354 fmt = ui.verbose and ' in %s' % path or ''
354 fmt = ui.verbose and ' in %s' % path or ''
355 demostatus('%s keywords expanded%s' % (kwstatus, fmt))
355 demostatus('%s keywords expanded%s' % (kwstatus, fmt))
356 ui.write(repo.wread(fn))
356 ui.write(repo.wread(fn))
357 ui.debug(_('\nremoving temporary repository %s\n') % tmpdir)
357 ui.debug(_('\nremoving temporary repository %s\n') % tmpdir)
358 shutil.rmtree(tmpdir, ignore_errors=True)
358 shutil.rmtree(tmpdir, ignore_errors=True)
359
359
360 def expand(ui, repo, *pats, **opts):
360 def expand(ui, repo, *pats, **opts):
361 '''expand keywords in working directory
361 '''expand keywords in working directory
362
362
363 Run after (re)enabling keyword expansion.
363 Run after (re)enabling keyword expansion.
364
364
365 kwexpand refuses to run if given files contain local changes.
365 kwexpand refuses to run if given files contain local changes.
366 '''
366 '''
367 # 3rd argument sets expansion to True
367 # 3rd argument sets expansion to True
368 _kwfwrite(ui, repo, True, *pats, **opts)
368 _kwfwrite(ui, repo, True, *pats, **opts)
369
369
370 def files(ui, repo, *pats, **opts):
370 def files(ui, repo, *pats, **opts):
371 '''print files currently configured for keyword expansion
371 '''print files currently configured for keyword expansion
372
372
373 Crosscheck which files in working directory are potential targets
373 Crosscheck which files in working directory are potential targets
374 for keyword expansion. That is, files matched by [keyword] config
374 for keyword expansion. That is, files matched by [keyword] config
375 patterns but not symlinks.
375 patterns but not symlinks.
376 '''
376 '''
377 kwt = kwtools['templater']
377 kwt = kwtools['templater']
378 status = _status(ui, repo, kwt, opts.get('untracked'), *pats, **opts)
378 status = _status(ui, repo, kwt, opts.get('untracked'), *pats, **opts)
379 modified, added, removed, deleted, unknown, ignored, clean = status
379 modified, added, removed, deleted, unknown, ignored, clean = status
380 files = sorted(modified + added + clean + unknown)
380 files = sorted(modified + added + clean + unknown)
381 wctx = repo[None]
381 wctx = repo[None]
382 kwfiles = [f for f in files if kwt.iskwfile(f, wctx.flags)]
382 kwfiles = [f for f in files if kwt.iskwfile(f, wctx.flags)]
383 cwd = pats and repo.getcwd() or ''
383 cwd = pats and repo.getcwd() or ''
384 kwfstats = not opts.get('ignore') and (('K', kwfiles),) or ()
384 kwfstats = not opts.get('ignore') and (('K', kwfiles),) or ()
385 if opts.get('all') or opts.get('ignore'):
385 if opts.get('all') or opts.get('ignore'):
386 kwfstats += (('I', [f for f in files if f not in kwfiles]),)
386 kwfstats += (('I', [f for f in files if f not in kwfiles]),)
387 for char, filenames in kwfstats:
387 for char, filenames in kwfstats:
388 fmt = (opts.get('all') or ui.verbose) and '%s %%s\n' % char or '%s\n'
388 fmt = (opts.get('all') or ui.verbose) and '%s %%s\n' % char or '%s\n'
389 for f in filenames:
389 for f in filenames:
390 ui.write(fmt % repo.pathto(f, cwd))
390 ui.write(fmt % repo.pathto(f, cwd))
391
391
392 def shrink(ui, repo, *pats, **opts):
392 def shrink(ui, repo, *pats, **opts):
393 '''revert expanded keywords in working directory
393 '''revert expanded keywords in working directory
394
394
395 Run before changing/disabling active keywords or if you experience
395 Run before changing/disabling active keywords or if you experience
396 problems with "hg import" or "hg merge".
396 problems with "hg import" or "hg merge".
397
397
398 kwshrink refuses to run if given files contain local changes.
398 kwshrink refuses to run if given files contain local changes.
399 '''
399 '''
400 # 3rd argument sets expansion to False
400 # 3rd argument sets expansion to False
401 _kwfwrite(ui, repo, False, *pats, **opts)
401 _kwfwrite(ui, repo, False, *pats, **opts)
402
402
403
403
404 def uisetup(ui):
404 def uisetup(ui):
405 '''Collects [keyword] config in kwtools.
405 '''Collects [keyword] config in kwtools.
406 Monkeypatches dispatch._parse if needed.'''
406 Monkeypatches dispatch._parse if needed.'''
407
407
408 for pat, opt in ui.configitems('keyword'):
408 for pat, opt in ui.configitems('keyword'):
409 if opt != 'ignore':
409 if opt != 'ignore':
410 kwtools['inc'].append(pat)
410 kwtools['inc'].append(pat)
411 else:
411 else:
412 kwtools['exc'].append(pat)
412 kwtools['exc'].append(pat)
413
413
414 if kwtools['inc']:
414 if kwtools['inc']:
415 def kwdispatch_parse(orig, ui, args):
415 def kwdispatch_parse(orig, ui, args):
416 '''Monkeypatch dispatch._parse to obtain running hg command.'''
416 '''Monkeypatch dispatch._parse to obtain running hg command.'''
417 cmd, func, args, options, cmdoptions = orig(ui, args)
417 cmd, func, args, options, cmdoptions = orig(ui, args)
418 kwtools['hgcmd'] = cmd
418 kwtools['hgcmd'] = cmd
419 return cmd, func, args, options, cmdoptions
419 return cmd, func, args, options, cmdoptions
420
420
421 extensions.wrapfunction(dispatch, '_parse', kwdispatch_parse)
421 extensions.wrapfunction(dispatch, '_parse', kwdispatch_parse)
422
422
423 def reposetup(ui, repo):
423 def reposetup(ui, repo):
424 '''Sets up repo as kwrepo for keyword substitution.
424 '''Sets up repo as kwrepo for keyword substitution.
425 Overrides file method to return kwfilelog instead of filelog
425 Overrides file method to return kwfilelog instead of filelog
426 if file matches user configuration.
426 if file matches user configuration.
427 Wraps commit to overwrite configured files with updated
427 Wraps commit to overwrite configured files with updated
428 keyword substitutions.
428 keyword substitutions.
429 Monkeypatches patch and webcommands.'''
429 Monkeypatches patch and webcommands.'''
430
430
431 try:
431 try:
432 if (not repo.local() or not kwtools['inc']
432 if (not repo.local() or not kwtools['inc']
433 or kwtools['hgcmd'] in nokwcommands.split()
433 or kwtools['hgcmd'] in nokwcommands.split()
434 or '.hg' in util.splitpath(repo.root)
434 or '.hg' in util.splitpath(repo.root)
435 or repo._url.startswith('bundle:')):
435 or repo._url.startswith('bundle:')):
436 return
436 return
437 except AttributeError:
437 except AttributeError:
438 pass
438 pass
439
439
440 kwtools['templater'] = kwt = kwtemplater(ui, repo)
440 kwtools['templater'] = kwt = kwtemplater(ui, repo)
441
441
442 class kwrepo(repo.__class__):
442 class kwrepo(repo.__class__):
443 def file(self, f):
443 def file(self, f):
444 if f[0] == '/':
444 if f[0] == '/':
445 f = f[1:]
445 f = f[1:]
446 return kwfilelog(self.sopener, kwt, f)
446 return kwfilelog(self.sopener, kwt, f)
447
447
448 def wread(self, filename):
448 def wread(self, filename):
449 data = super(kwrepo, self).wread(filename)
449 data = super(kwrepo, self).wread(filename)
450 return kwt.wread(filename, data)
450 return kwt.wread(filename, data)
451
451
452 def commit(self, files=None, text='', user=None, date=None,
452 def commit(self, files=None, text='', user=None, date=None,
453 match=None, force=False, editor=None, extra={}):
453 match=None, force=False, editor=None, extra={}):
454 wlock = lock = None
454 wlock = lock = None
455 _p1 = _p2 = None
455 _p1 = _p2 = None
456 try:
456 try:
457 wlock = self.wlock()
457 wlock = self.wlock()
458 lock = self.lock()
458 lock = self.lock()
459 # store and postpone commit hooks
459 # store and postpone commit hooks
460 commithooks = {}
460 commithooks = {}
461 for name, cmd in ui.configitems('hooks'):
461 for name, cmd in ui.configitems('hooks'):
462 if name.split('.', 1)[0] == 'commit':
462 if name.split('.', 1)[0] == 'commit':
463 commithooks[name] = cmd
463 commithooks[name] = cmd
464 ui.setconfig('hooks', name, None)
464 ui.setconfig('hooks', name, None)
465 if commithooks:
465 if commithooks:
466 # store parents for commit hook environment
466 # store parents for commit hook environment
467 _p1, _p2 = repo.dirstate.parents()
467 _p1, _p2 = repo.dirstate.parents()
468 _p1 = hex(_p1)
468 _p1 = hex(_p1)
469 if _p2 == nullid:
469 if _p2 == nullid:
470 _p2 = ''
470 _p2 = ''
471 else:
471 else:
472 _p2 = hex(_p2)
472 _p2 = hex(_p2)
473
473
474 n = super(kwrepo, self).commit(files, text, user, date, match,
474 n = super(kwrepo, self).commit(files, text, user, date, match,
475 force, editor, extra)
475 force, editor, extra)
476
476
477 # restore commit hooks
477 # restore commit hooks
478 for name, cmd in commithooks.iteritems():
478 for name, cmd in commithooks.iteritems():
479 ui.setconfig('hooks', name, cmd)
479 ui.setconfig('hooks', name, cmd)
480 if n is not None:
480 if n is not None:
481 kwt.overwrite(n, True, None)
481 kwt.overwrite(n, True, None)
482 repo.hook('commit', node=n, parent1=_p1, parent2=_p2)
482 repo.hook('commit', node=n, parent1=_p1, parent2=_p2)
483 return n
483 return n
484 finally:
484 finally:
485 release(lock, wlock)
485 release(lock, wlock)
486
486
487 # monkeypatches
487 # monkeypatches
488 def kwpatchfile_init(orig, self, ui, fname, opener, missing=False):
488 def kwpatchfile_init(orig, self, ui, fname, opener, missing=False):
489 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
489 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
490 rejects or conflicts due to expanded keywords in working dir.'''
490 rejects or conflicts due to expanded keywords in working dir.'''
491 orig(self, ui, fname, opener, missing)
491 orig(self, ui, fname, opener, missing)
492 # shrink keywords read from working dir
492 # shrink keywords read from working dir
493 self.lines = kwt.shrinklines(self.fname, self.lines)
493 self.lines = kwt.shrinklines(self.fname, self.lines)
494
494
495 def kw_diff(orig, repo, node1=None, node2=None, match=None, changes=None,
495 def kw_diff(orig, repo, node1=None, node2=None, match=None, changes=None,
496 opts=None):
496 opts=None):
497 '''Monkeypatch patch.diff to avoid expansion except when
497 '''Monkeypatch patch.diff to avoid expansion except when
498 comparing against working dir.'''
498 comparing against working dir.'''
499 if node2 is not None:
499 if node2 is not None:
500 kwt.matcher = util.never
500 kwt.matcher = util.never
501 elif node1 is not None and node1 != repo['.'].node():
501 elif node1 is not None and node1 != repo['.'].node():
502 kwt.restrict = True
502 kwt.restrict = True
503 return orig(repo, node1, node2, match, changes, opts)
503 return orig(repo, node1, node2, match, changes, opts)
504
504
505 def kwweb_skip(orig, web, req, tmpl):
505 def kwweb_skip(orig, web, req, tmpl):
506 '''Wraps webcommands.x turning off keyword expansion.'''
506 '''Wraps webcommands.x turning off keyword expansion.'''
507 kwt.matcher = util.never
507 kwt.matcher = util.never
508 return orig(web, req, tmpl)
508 return orig(web, req, tmpl)
509
509
510 repo.__class__ = kwrepo
510 repo.__class__ = kwrepo
511
511
512 extensions.wrapfunction(patch.patchfile, '__init__', kwpatchfile_init)
512 extensions.wrapfunction(patch.patchfile, '__init__', kwpatchfile_init)
513 extensions.wrapfunction(patch, 'diff', kw_diff)
513 extensions.wrapfunction(patch, 'diff', kw_diff)
514 for c in 'annotate changeset rev filediff diff'.split():
514 for c in 'annotate changeset rev filediff diff'.split():
515 extensions.wrapfunction(webcommands, c, kwweb_skip)
515 extensions.wrapfunction(webcommands, c, kwweb_skip)
516
516
517 cmdtable = {
517 cmdtable = {
518 'kwdemo':
518 'kwdemo':
519 (demo,
519 (demo,
520 [('d', 'default', None, _('show default keyword template maps')),
520 [('d', 'default', None, _('show default keyword template maps')),
521 ('f', 'rcfile', [], _('read maps from rcfile'))],
521 ('f', 'rcfile', [], _('read maps from rcfile'))],
522 _('hg kwdemo [-d] [-f RCFILE] [TEMPLATEMAP]...')),
522 _('hg kwdemo [-d] [-f RCFILE] [TEMPLATEMAP]...')),
523 'kwexpand': (expand, commands.walkopts,
523 'kwexpand': (expand, commands.walkopts,
524 _('hg kwexpand [OPTION]... [FILE]...')),
524 _('hg kwexpand [OPTION]... [FILE]...')),
525 'kwfiles':
525 'kwfiles':
526 (files,
526 (files,
527 [('a', 'all', None, _('show keyword status flags of all files')),
527 [('a', 'all', None, _('show keyword status flags of all files')),
528 ('i', 'ignore', None, _('show files excluded from expansion')),
528 ('i', 'ignore', None, _('show files excluded from expansion')),
529 ('u', 'untracked', None, _('additionally show untracked files')),
529 ('u', 'untracked', None, _('additionally show untracked files')),
530 ] + commands.walkopts,
530 ] + commands.walkopts,
531 _('hg kwfiles [OPTION]... [FILE]...')),
531 _('hg kwfiles [OPTION]... [FILE]...')),
532 'kwshrink': (shrink, commands.walkopts,
532 'kwshrink': (shrink, commands.walkopts,
533 _('hg kwshrink [OPTION]... [FILE]...')),
533 _('hg kwshrink [OPTION]... [FILE]...')),
534 }
534 }
@@ -1,222 +1,222 b''
1 # filemerge.py - file-level merge handling for Mercurial
1 # filemerge.py - file-level merge handling for Mercurial
2 #
2 #
3 # Copyright 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006, 2007, 2008 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, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7
7
8 from node import short
8 from node import short
9 from i18n import _
9 from i18n import _
10 import util, simplemerge
10 import util, simplemerge, match
11 import os, tempfile, re, filecmp
11 import os, tempfile, re, filecmp
12
12
13 def _toolstr(ui, tool, part, default=""):
13 def _toolstr(ui, tool, part, default=""):
14 return ui.config("merge-tools", tool + "." + part, default)
14 return ui.config("merge-tools", tool + "." + part, default)
15
15
16 def _toolbool(ui, tool, part, default=False):
16 def _toolbool(ui, tool, part, default=False):
17 return ui.configbool("merge-tools", tool + "." + part, default)
17 return ui.configbool("merge-tools", tool + "." + part, default)
18
18
19 def _findtool(ui, tool):
19 def _findtool(ui, tool):
20 if tool in ("internal:fail", "internal:local", "internal:other"):
20 if tool in ("internal:fail", "internal:local", "internal:other"):
21 return tool
21 return tool
22 k = _toolstr(ui, tool, "regkey")
22 k = _toolstr(ui, tool, "regkey")
23 if k:
23 if k:
24 p = util.lookup_reg(k, _toolstr(ui, tool, "regname"))
24 p = util.lookup_reg(k, _toolstr(ui, tool, "regname"))
25 if p:
25 if p:
26 p = util.find_exe(p + _toolstr(ui, tool, "regappend"))
26 p = util.find_exe(p + _toolstr(ui, tool, "regappend"))
27 if p:
27 if p:
28 return p
28 return p
29 return util.find_exe(_toolstr(ui, tool, "executable", tool))
29 return util.find_exe(_toolstr(ui, tool, "executable", tool))
30
30
31 def _picktool(repo, ui, path, binary, symlink):
31 def _picktool(repo, ui, path, binary, symlink):
32 def check(tool, pat, symlink, binary):
32 def check(tool, pat, symlink, binary):
33 tmsg = tool
33 tmsg = tool
34 if pat:
34 if pat:
35 tmsg += " specified for " + pat
35 tmsg += " specified for " + pat
36 if not _findtool(ui, tool):
36 if not _findtool(ui, tool):
37 if pat: # explicitly requested tool deserves a warning
37 if pat: # explicitly requested tool deserves a warning
38 ui.warn(_("couldn't find merge tool %s\n") % tmsg)
38 ui.warn(_("couldn't find merge tool %s\n") % tmsg)
39 else: # configured but non-existing tools are more silent
39 else: # configured but non-existing tools are more silent
40 ui.note(_("couldn't find merge tool %s\n") % tmsg)
40 ui.note(_("couldn't find merge tool %s\n") % tmsg)
41 elif symlink and not _toolbool(ui, tool, "symlink"):
41 elif symlink and not _toolbool(ui, tool, "symlink"):
42 ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
42 ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
43 elif binary and not _toolbool(ui, tool, "binary"):
43 elif binary and not _toolbool(ui, tool, "binary"):
44 ui.warn(_("tool %s can't handle binary\n") % tmsg)
44 ui.warn(_("tool %s can't handle binary\n") % tmsg)
45 elif not util.gui() and _toolbool(ui, tool, "gui"):
45 elif not util.gui() and _toolbool(ui, tool, "gui"):
46 ui.warn(_("tool %s requires a GUI\n") % tmsg)
46 ui.warn(_("tool %s requires a GUI\n") % tmsg)
47 else:
47 else:
48 return True
48 return True
49 return False
49 return False
50
50
51 # HGMERGE takes precedence
51 # HGMERGE takes precedence
52 hgmerge = os.environ.get("HGMERGE")
52 hgmerge = os.environ.get("HGMERGE")
53 if hgmerge:
53 if hgmerge:
54 return (hgmerge, hgmerge)
54 return (hgmerge, hgmerge)
55
55
56 # then patterns
56 # then patterns
57 for pat, tool in ui.configitems("merge-patterns"):
57 for pat, tool in ui.configitems("merge-patterns"):
58 mf = util.matcher(repo.root, "", [pat], [], [])[1]
58 mf = match.match(repo.root, '', [pat], [], [], 'glob')
59 if mf(path) and check(tool, pat, symlink, False):
59 if mf(path) and check(tool, pat, symlink, False):
60 toolpath = _findtool(ui, tool)
60 toolpath = _findtool(ui, tool)
61 return (tool, '"' + toolpath + '"')
61 return (tool, '"' + toolpath + '"')
62
62
63 # then merge tools
63 # then merge tools
64 tools = {}
64 tools = {}
65 for k,v in ui.configitems("merge-tools"):
65 for k,v in ui.configitems("merge-tools"):
66 t = k.split('.')[0]
66 t = k.split('.')[0]
67 if t not in tools:
67 if t not in tools:
68 tools[t] = int(_toolstr(ui, t, "priority", "0"))
68 tools[t] = int(_toolstr(ui, t, "priority", "0"))
69 names = tools.keys()
69 names = tools.keys()
70 tools = sorted([(-p,t) for t,p in tools.items()])
70 tools = sorted([(-p,t) for t,p in tools.items()])
71 uimerge = ui.config("ui", "merge")
71 uimerge = ui.config("ui", "merge")
72 if uimerge:
72 if uimerge:
73 if uimerge not in names:
73 if uimerge not in names:
74 return (uimerge, uimerge)
74 return (uimerge, uimerge)
75 tools.insert(0, (None, uimerge)) # highest priority
75 tools.insert(0, (None, uimerge)) # highest priority
76 tools.append((None, "hgmerge")) # the old default, if found
76 tools.append((None, "hgmerge")) # the old default, if found
77 for p,t in tools:
77 for p,t in tools:
78 if check(t, None, symlink, binary):
78 if check(t, None, symlink, binary):
79 toolpath = _findtool(ui, t)
79 toolpath = _findtool(ui, t)
80 return (t, '"' + toolpath + '"')
80 return (t, '"' + toolpath + '"')
81 # internal merge as last resort
81 # internal merge as last resort
82 return (not (symlink or binary) and "internal:merge" or None, None)
82 return (not (symlink or binary) and "internal:merge" or None, None)
83
83
84 def _eoltype(data):
84 def _eoltype(data):
85 "Guess the EOL type of a file"
85 "Guess the EOL type of a file"
86 if '\0' in data: # binary
86 if '\0' in data: # binary
87 return None
87 return None
88 if '\r\n' in data: # Windows
88 if '\r\n' in data: # Windows
89 return '\r\n'
89 return '\r\n'
90 if '\r' in data: # Old Mac
90 if '\r' in data: # Old Mac
91 return '\r'
91 return '\r'
92 if '\n' in data: # UNIX
92 if '\n' in data: # UNIX
93 return '\n'
93 return '\n'
94 return None # unknown
94 return None # unknown
95
95
96 def _matcheol(file, origfile):
96 def _matcheol(file, origfile):
97 "Convert EOL markers in a file to match origfile"
97 "Convert EOL markers in a file to match origfile"
98 tostyle = _eoltype(open(origfile, "rb").read())
98 tostyle = _eoltype(open(origfile, "rb").read())
99 if tostyle:
99 if tostyle:
100 data = open(file, "rb").read()
100 data = open(file, "rb").read()
101 style = _eoltype(data)
101 style = _eoltype(data)
102 if style:
102 if style:
103 newdata = data.replace(style, tostyle)
103 newdata = data.replace(style, tostyle)
104 if newdata != data:
104 if newdata != data:
105 open(file, "wb").write(newdata)
105 open(file, "wb").write(newdata)
106
106
107 def filemerge(repo, mynode, orig, fcd, fco, fca):
107 def filemerge(repo, mynode, orig, fcd, fco, fca):
108 """perform a 3-way merge in the working directory
108 """perform a 3-way merge in the working directory
109
109
110 mynode = parent node before merge
110 mynode = parent node before merge
111 orig = original local filename before merge
111 orig = original local filename before merge
112 fco = other file context
112 fco = other file context
113 fca = ancestor file context
113 fca = ancestor file context
114 fcd = local file context for current/destination file
114 fcd = local file context for current/destination file
115 """
115 """
116
116
117 def temp(prefix, ctx):
117 def temp(prefix, ctx):
118 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
118 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
119 (fd, name) = tempfile.mkstemp(prefix=pre)
119 (fd, name) = tempfile.mkstemp(prefix=pre)
120 data = repo.wwritedata(ctx.path(), ctx.data())
120 data = repo.wwritedata(ctx.path(), ctx.data())
121 f = os.fdopen(fd, "wb")
121 f = os.fdopen(fd, "wb")
122 f.write(data)
122 f.write(data)
123 f.close()
123 f.close()
124 return name
124 return name
125
125
126 def isbin(ctx):
126 def isbin(ctx):
127 try:
127 try:
128 return util.binary(ctx.data())
128 return util.binary(ctx.data())
129 except IOError:
129 except IOError:
130 return False
130 return False
131
131
132 if not fco.cmp(fcd.data()): # files identical?
132 if not fco.cmp(fcd.data()): # files identical?
133 return None
133 return None
134
134
135 ui = repo.ui
135 ui = repo.ui
136 fd = fcd.path()
136 fd = fcd.path()
137 binary = isbin(fcd) or isbin(fco) or isbin(fca)
137 binary = isbin(fcd) or isbin(fco) or isbin(fca)
138 symlink = 'l' in fcd.flags() + fco.flags()
138 symlink = 'l' in fcd.flags() + fco.flags()
139 tool, toolpath = _picktool(repo, ui, fd, binary, symlink)
139 tool, toolpath = _picktool(repo, ui, fd, binary, symlink)
140 ui.debug(_("picked tool '%s' for %s (binary %s symlink %s)\n") %
140 ui.debug(_("picked tool '%s' for %s (binary %s symlink %s)\n") %
141 (tool, fd, binary, symlink))
141 (tool, fd, binary, symlink))
142
142
143 if not tool:
143 if not tool:
144 tool = "internal:local"
144 tool = "internal:local"
145 if ui.prompt(_(" no tool found to merge %s\n"
145 if ui.prompt(_(" no tool found to merge %s\n"
146 "keep (l)ocal or take (o)ther?") % fd,
146 "keep (l)ocal or take (o)ther?") % fd,
147 (_("&Local"), _("&Other")), _("l")) != _("l"):
147 (_("&Local"), _("&Other")), _("l")) != _("l"):
148 tool = "internal:other"
148 tool = "internal:other"
149 if tool == "internal:local":
149 if tool == "internal:local":
150 return 0
150 return 0
151 if tool == "internal:other":
151 if tool == "internal:other":
152 repo.wwrite(fd, fco.data(), fco.flags())
152 repo.wwrite(fd, fco.data(), fco.flags())
153 return 0
153 return 0
154 if tool == "internal:fail":
154 if tool == "internal:fail":
155 return 1
155 return 1
156
156
157 # do the actual merge
157 # do the actual merge
158 a = repo.wjoin(fd)
158 a = repo.wjoin(fd)
159 b = temp("base", fca)
159 b = temp("base", fca)
160 c = temp("other", fco)
160 c = temp("other", fco)
161 out = ""
161 out = ""
162 back = a + ".orig"
162 back = a + ".orig"
163 util.copyfile(a, back)
163 util.copyfile(a, back)
164
164
165 if orig != fco.path():
165 if orig != fco.path():
166 repo.ui.status(_("merging %s and %s to %s\n") % (orig, fco.path(), fd))
166 repo.ui.status(_("merging %s and %s to %s\n") % (orig, fco.path(), fd))
167 else:
167 else:
168 repo.ui.status(_("merging %s\n") % fd)
168 repo.ui.status(_("merging %s\n") % fd)
169
169
170 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcd, fco, fca))
170 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcd, fco, fca))
171
171
172 # do we attempt to simplemerge first?
172 # do we attempt to simplemerge first?
173 if _toolbool(ui, tool, "premerge", not (binary or symlink)):
173 if _toolbool(ui, tool, "premerge", not (binary or symlink)):
174 r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
174 r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
175 if not r:
175 if not r:
176 ui.debug(_(" premerge successful\n"))
176 ui.debug(_(" premerge successful\n"))
177 os.unlink(back)
177 os.unlink(back)
178 os.unlink(b)
178 os.unlink(b)
179 os.unlink(c)
179 os.unlink(c)
180 return 0
180 return 0
181 util.copyfile(back, a) # restore from backup and try again
181 util.copyfile(back, a) # restore from backup and try again
182
182
183 env = dict(HG_FILE=fd,
183 env = dict(HG_FILE=fd,
184 HG_MY_NODE=short(mynode),
184 HG_MY_NODE=short(mynode),
185 HG_OTHER_NODE=str(fco.changectx()),
185 HG_OTHER_NODE=str(fco.changectx()),
186 HG_MY_ISLINK='l' in fcd.flags(),
186 HG_MY_ISLINK='l' in fcd.flags(),
187 HG_OTHER_ISLINK='l' in fco.flags(),
187 HG_OTHER_ISLINK='l' in fco.flags(),
188 HG_BASE_ISLINK='l' in fca.flags())
188 HG_BASE_ISLINK='l' in fca.flags())
189
189
190 if tool == "internal:merge":
190 if tool == "internal:merge":
191 r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
191 r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
192 else:
192 else:
193 args = _toolstr(ui, tool, "args", '$local $base $other')
193 args = _toolstr(ui, tool, "args", '$local $base $other')
194 if "$output" in args:
194 if "$output" in args:
195 out, a = a, back # read input from backup, write to original
195 out, a = a, back # read input from backup, write to original
196 replace = dict(local=a, base=b, other=c, output=out)
196 replace = dict(local=a, base=b, other=c, output=out)
197 args = re.sub("\$(local|base|other|output)",
197 args = re.sub("\$(local|base|other|output)",
198 lambda x: '"%s"' % replace[x.group()[1:]], args)
198 lambda x: '"%s"' % replace[x.group()[1:]], args)
199 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env)
199 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env)
200
200
201 if not r and _toolbool(ui, tool, "checkconflicts"):
201 if not r and _toolbool(ui, tool, "checkconflicts"):
202 if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcd.data()):
202 if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcd.data()):
203 r = 1
203 r = 1
204
204
205 if not r and _toolbool(ui, tool, "checkchanged"):
205 if not r and _toolbool(ui, tool, "checkchanged"):
206 if filecmp.cmp(repo.wjoin(fd), back):
206 if filecmp.cmp(repo.wjoin(fd), back):
207 if ui.prompt(_(" output file %s appears unchanged\n"
207 if ui.prompt(_(" output file %s appears unchanged\n"
208 "was merge successful (yn)?") % fd,
208 "was merge successful (yn)?") % fd,
209 (_("&Yes"), _("&No")), _("n")) != _("y"):
209 (_("&Yes"), _("&No")), _("n")) != _("y"):
210 r = 1
210 r = 1
211
211
212 if _toolbool(ui, tool, "fixeol"):
212 if _toolbool(ui, tool, "fixeol"):
213 _matcheol(repo.wjoin(fd), back)
213 _matcheol(repo.wjoin(fd), back)
214
214
215 if r:
215 if r:
216 repo.ui.warn(_("merging %s failed!\n") % fd)
216 repo.ui.warn(_("merging %s failed!\n") % fd)
217 else:
217 else:
218 os.unlink(back)
218 os.unlink(back)
219
219
220 os.unlink(b)
220 os.unlink(b)
221 os.unlink(c)
221 os.unlink(c)
222 return r
222 return r
@@ -1,91 +1,92 b''
1 # ignore.py - ignored file handling for mercurial
1 # ignore.py - ignored file handling for mercurial
2 #
2 #
3 # Copyright 2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 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, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7
7
8 from i18n import _
8 from i18n import _
9 import util
9 import util, match
10 import re
10 import re
11
11
12 _commentre = None
12 _commentre = None
13
13
14 def _parselines(fp):
14 def _parselines(fp):
15 for line in fp:
15 for line in fp:
16 if "#" in line:
16 if "#" in line:
17 global _commentre
17 global _commentre
18 if not _commentre:
18 if not _commentre:
19 _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
19 _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
20 # remove comments prefixed by an even number of escapes
20 # remove comments prefixed by an even number of escapes
21 line = _commentre.sub(r'\1', line)
21 line = _commentre.sub(r'\1', line)
22 # fixup properly escaped comments that survived the above
22 # fixup properly escaped comments that survived the above
23 line = line.replace("\\#", "#")
23 line = line.replace("\\#", "#")
24 line = line.rstrip()
24 line = line.rstrip()
25 if line:
25 if line:
26 yield line
26 yield line
27
27
28 def ignore(root, files, warn):
28 def ignore(root, files, warn):
29 '''return the contents of .hgignore files as a list of patterns.
29 '''return the contents of .hgignore files as a list of patterns.
30
30
31 the files parsed for patterns include:
31 the files parsed for patterns include:
32 .hgignore in the repository root
32 .hgignore in the repository root
33 any additional files specified in the [ui] section of ~/.hgrc
33 any additional files specified in the [ui] section of ~/.hgrc
34
34
35 trailing white space is dropped.
35 trailing white space is dropped.
36 the escape character is backslash.
36 the escape character is backslash.
37 comments start with #.
37 comments start with #.
38 empty lines are skipped.
38 empty lines are skipped.
39
39
40 lines can be of the following formats:
40 lines can be of the following formats:
41
41
42 syntax: regexp # defaults following lines to non-rooted regexps
42 syntax: regexp # defaults following lines to non-rooted regexps
43 syntax: glob # defaults following lines to non-rooted globs
43 syntax: glob # defaults following lines to non-rooted globs
44 re:pattern # non-rooted regular expression
44 re:pattern # non-rooted regular expression
45 glob:pattern # non-rooted glob
45 glob:pattern # non-rooted glob
46 pattern # pattern of the current default type'''
46 pattern # pattern of the current default type'''
47
47
48 syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
48 syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
49 pats = {}
49 pats = {}
50 for f in files:
50 for f in files:
51 try:
51 try:
52 pats[f] = []
52 pats[f] = []
53 fp = open(f)
53 fp = open(f)
54 syntax = 'relre:'
54 syntax = 'relre:'
55 for line in _parselines(fp):
55 for line in _parselines(fp):
56 if line.startswith('syntax:'):
56 if line.startswith('syntax:'):
57 s = line[7:].strip()
57 s = line[7:].strip()
58 try:
58 try:
59 syntax = syntaxes[s]
59 syntax = syntaxes[s]
60 except KeyError:
60 except KeyError:
61 warn(_("%s: ignoring invalid syntax '%s'\n") % (f, s))
61 warn(_("%s: ignoring invalid syntax '%s'\n") % (f, s))
62 continue
62 continue
63 pat = syntax + line
63 pat = syntax + line
64 for s, rels in syntaxes.iteritems():
64 for s, rels in syntaxes.iteritems():
65 if line.startswith(rels):
65 if line.startswith(rels):
66 pat = line
66 pat = line
67 break
67 break
68 elif line.startswith(s+':'):
68 elif line.startswith(s+':'):
69 pat = rels + line[len(s)+1:]
69 pat = rels + line[len(s)+1:]
70 break
70 break
71 pats[f].append(pat)
71 pats[f].append(pat)
72 except IOError, inst:
72 except IOError, inst:
73 if f != files[0]:
73 if f != files[0]:
74 warn(_("skipping unreadable ignore file '%s': %s\n") %
74 warn(_("skipping unreadable ignore file '%s': %s\n") %
75 (f, inst.strerror))
75 (f, inst.strerror))
76
76
77 allpats = []
77 allpats = []
78 [allpats.extend(patlist) for patlist in pats.values()]
78 [allpats.extend(patlist) for patlist in pats.values()]
79 if not allpats:
79 if not allpats:
80 return util.never
80 return util.never
81
81
82 try:
82 try:
83 files, ignorefunc, anypats = (
83 ignorefunc = match.match(root, '', [], allpats, [], 'glob')
84 util.matcher(root, inc=allpats, src='.hgignore'))
85 except util.Abort:
84 except util.Abort:
86 # Re-raise an exception where the src is the right file
85 # Re-raise an exception where the src is the right file
87 for f, patlist in pats.iteritems():
86 for f, patlist in pats.iteritems():
88 files, ignorefunc, anypats = (
87 try:
89 util.matcher(root, inc=patlist, src=f))
88 match.match(root, '', [], patlist, [], 'glob')
89 except util.Abort, inst:
90 raise util.Abort('%s: %s' % (f, inst[0]))
90
91
91 return ignorefunc
92 return ignorefunc
@@ -1,2129 +1,2129 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, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
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
10 import repo, changegroup
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 match_
14 import match as match_
15 import merge as merge_
15 import merge as merge_
16 from lock import release
16 from lock import release
17 import weakref, stat, errno, os, time, inspect
17 import weakref, stat, errno, os, time, inspect
18 propertycache = util.propertycache
18 propertycache = util.propertycache
19
19
20 class localrepository(repo.repository):
20 class localrepository(repo.repository):
21 capabilities = set(('lookup', 'changegroupsubset', 'branchmap'))
21 capabilities = set(('lookup', 'changegroupsubset', 'branchmap'))
22 supported = set('revlogv1 store fncache'.split())
22 supported = set('revlogv1 store fncache'.split())
23
23
24 def __init__(self, baseui, path=None, create=0):
24 def __init__(self, baseui, path=None, create=0):
25 repo.repository.__init__(self)
25 repo.repository.__init__(self)
26 self.root = os.path.realpath(path)
26 self.root = os.path.realpath(path)
27 self.path = os.path.join(self.root, ".hg")
27 self.path = os.path.join(self.root, ".hg")
28 self.origroot = path
28 self.origroot = path
29 self.opener = util.opener(self.path)
29 self.opener = util.opener(self.path)
30 self.wopener = util.opener(self.root)
30 self.wopener = util.opener(self.root)
31
31
32 if not os.path.isdir(self.path):
32 if not os.path.isdir(self.path):
33 if create:
33 if create:
34 if not os.path.exists(path):
34 if not os.path.exists(path):
35 os.mkdir(path)
35 os.mkdir(path)
36 os.mkdir(self.path)
36 os.mkdir(self.path)
37 requirements = ["revlogv1"]
37 requirements = ["revlogv1"]
38 if baseui.configbool('format', 'usestore', True):
38 if baseui.configbool('format', 'usestore', True):
39 os.mkdir(os.path.join(self.path, "store"))
39 os.mkdir(os.path.join(self.path, "store"))
40 requirements.append("store")
40 requirements.append("store")
41 if baseui.configbool('format', 'usefncache', True):
41 if baseui.configbool('format', 'usefncache', True):
42 requirements.append("fncache")
42 requirements.append("fncache")
43 # create an invalid changelog
43 # create an invalid changelog
44 self.opener("00changelog.i", "a").write(
44 self.opener("00changelog.i", "a").write(
45 '\0\0\0\2' # represents revlogv2
45 '\0\0\0\2' # represents revlogv2
46 ' dummy changelog to prevent using the old repo layout'
46 ' dummy changelog to prevent using the old repo layout'
47 )
47 )
48 reqfile = self.opener("requires", "w")
48 reqfile = self.opener("requires", "w")
49 for r in requirements:
49 for r in requirements:
50 reqfile.write("%s\n" % r)
50 reqfile.write("%s\n" % r)
51 reqfile.close()
51 reqfile.close()
52 else:
52 else:
53 raise error.RepoError(_("repository %s not found") % path)
53 raise error.RepoError(_("repository %s not found") % path)
54 elif create:
54 elif create:
55 raise error.RepoError(_("repository %s already exists") % path)
55 raise error.RepoError(_("repository %s already exists") % path)
56 else:
56 else:
57 # find requirements
57 # find requirements
58 requirements = set()
58 requirements = set()
59 try:
59 try:
60 requirements = set(self.opener("requires").read().splitlines())
60 requirements = set(self.opener("requires").read().splitlines())
61 except IOError, inst:
61 except IOError, inst:
62 if inst.errno != errno.ENOENT:
62 if inst.errno != errno.ENOENT:
63 raise
63 raise
64 for r in requirements - self.supported:
64 for r in requirements - self.supported:
65 raise error.RepoError(_("requirement '%s' not supported") % r)
65 raise error.RepoError(_("requirement '%s' not supported") % r)
66
66
67 self.store = store.store(requirements, self.path, util.opener)
67 self.store = store.store(requirements, self.path, util.opener)
68 self.spath = self.store.path
68 self.spath = self.store.path
69 self.sopener = self.store.opener
69 self.sopener = self.store.opener
70 self.sjoin = self.store.join
70 self.sjoin = self.store.join
71 self.opener.createmode = self.store.createmode
71 self.opener.createmode = self.store.createmode
72
72
73 self.baseui = baseui
73 self.baseui = baseui
74 self.ui = baseui.copy()
74 self.ui = baseui.copy()
75 try:
75 try:
76 self.ui.readconfig(self.join("hgrc"), self.root)
76 self.ui.readconfig(self.join("hgrc"), self.root)
77 extensions.loadall(self.ui)
77 extensions.loadall(self.ui)
78 except IOError:
78 except IOError:
79 pass
79 pass
80
80
81 self.tagscache = None
81 self.tagscache = None
82 self._tagstypecache = None
82 self._tagstypecache = None
83 self.branchcache = None
83 self.branchcache = None
84 self._ubranchcache = None # UTF-8 version of branchcache
84 self._ubranchcache = None # UTF-8 version of branchcache
85 self._branchcachetip = None
85 self._branchcachetip = None
86 self.nodetagscache = None
86 self.nodetagscache = None
87 self.filterpats = {}
87 self.filterpats = {}
88 self._datafilters = {}
88 self._datafilters = {}
89 self._transref = self._lockref = self._wlockref = None
89 self._transref = self._lockref = self._wlockref = None
90
90
91 @propertycache
91 @propertycache
92 def changelog(self):
92 def changelog(self):
93 c = changelog.changelog(self.sopener)
93 c = changelog.changelog(self.sopener)
94 if 'HG_PENDING' in os.environ:
94 if 'HG_PENDING' in os.environ:
95 p = os.environ['HG_PENDING']
95 p = os.environ['HG_PENDING']
96 if p.startswith(self.root):
96 if p.startswith(self.root):
97 c.readpending('00changelog.i.a')
97 c.readpending('00changelog.i.a')
98 self.sopener.defversion = c.version
98 self.sopener.defversion = c.version
99 return c
99 return c
100
100
101 @propertycache
101 @propertycache
102 def manifest(self):
102 def manifest(self):
103 return manifest.manifest(self.sopener)
103 return manifest.manifest(self.sopener)
104
104
105 @propertycache
105 @propertycache
106 def dirstate(self):
106 def dirstate(self):
107 return dirstate.dirstate(self.opener, self.ui, self.root)
107 return dirstate.dirstate(self.opener, self.ui, self.root)
108
108
109 def __getitem__(self, changeid):
109 def __getitem__(self, changeid):
110 if changeid is None:
110 if changeid is None:
111 return context.workingctx(self)
111 return context.workingctx(self)
112 return context.changectx(self, changeid)
112 return context.changectx(self, changeid)
113
113
114 def __nonzero__(self):
114 def __nonzero__(self):
115 return True
115 return True
116
116
117 def __len__(self):
117 def __len__(self):
118 return len(self.changelog)
118 return len(self.changelog)
119
119
120 def __iter__(self):
120 def __iter__(self):
121 for i in xrange(len(self)):
121 for i in xrange(len(self)):
122 yield i
122 yield i
123
123
124 def url(self):
124 def url(self):
125 return 'file:' + self.root
125 return 'file:' + self.root
126
126
127 def hook(self, name, throw=False, **args):
127 def hook(self, name, throw=False, **args):
128 return hook.hook(self.ui, self, name, throw, **args)
128 return hook.hook(self.ui, self, name, throw, **args)
129
129
130 tag_disallowed = ':\r\n'
130 tag_disallowed = ':\r\n'
131
131
132 def _tag(self, names, node, message, local, user, date, extra={}):
132 def _tag(self, names, node, message, local, user, date, extra={}):
133 if isinstance(names, str):
133 if isinstance(names, str):
134 allchars = names
134 allchars = names
135 names = (names,)
135 names = (names,)
136 else:
136 else:
137 allchars = ''.join(names)
137 allchars = ''.join(names)
138 for c in self.tag_disallowed:
138 for c in self.tag_disallowed:
139 if c in allchars:
139 if c in allchars:
140 raise util.Abort(_('%r cannot be used in a tag name') % c)
140 raise util.Abort(_('%r cannot be used in a tag name') % c)
141
141
142 for name in names:
142 for name in names:
143 self.hook('pretag', throw=True, node=hex(node), tag=name,
143 self.hook('pretag', throw=True, node=hex(node), tag=name,
144 local=local)
144 local=local)
145
145
146 def writetags(fp, names, munge, prevtags):
146 def writetags(fp, names, munge, prevtags):
147 fp.seek(0, 2)
147 fp.seek(0, 2)
148 if prevtags and prevtags[-1] != '\n':
148 if prevtags and prevtags[-1] != '\n':
149 fp.write('\n')
149 fp.write('\n')
150 for name in names:
150 for name in names:
151 m = munge and munge(name) or name
151 m = munge and munge(name) or name
152 if self._tagstypecache and name in self._tagstypecache:
152 if self._tagstypecache and name in self._tagstypecache:
153 old = self.tagscache.get(name, nullid)
153 old = self.tagscache.get(name, nullid)
154 fp.write('%s %s\n' % (hex(old), m))
154 fp.write('%s %s\n' % (hex(old), m))
155 fp.write('%s %s\n' % (hex(node), m))
155 fp.write('%s %s\n' % (hex(node), m))
156 fp.close()
156 fp.close()
157
157
158 prevtags = ''
158 prevtags = ''
159 if local:
159 if local:
160 try:
160 try:
161 fp = self.opener('localtags', 'r+')
161 fp = self.opener('localtags', 'r+')
162 except IOError:
162 except IOError:
163 fp = self.opener('localtags', 'a')
163 fp = self.opener('localtags', 'a')
164 else:
164 else:
165 prevtags = fp.read()
165 prevtags = fp.read()
166
166
167 # local tags are stored in the current charset
167 # local tags are stored in the current charset
168 writetags(fp, names, None, prevtags)
168 writetags(fp, names, None, prevtags)
169 for name in names:
169 for name in names:
170 self.hook('tag', node=hex(node), tag=name, local=local)
170 self.hook('tag', node=hex(node), tag=name, local=local)
171 return
171 return
172
172
173 try:
173 try:
174 fp = self.wfile('.hgtags', 'rb+')
174 fp = self.wfile('.hgtags', 'rb+')
175 except IOError:
175 except IOError:
176 fp = self.wfile('.hgtags', 'ab')
176 fp = self.wfile('.hgtags', 'ab')
177 else:
177 else:
178 prevtags = fp.read()
178 prevtags = fp.read()
179
179
180 # committed tags are stored in UTF-8
180 # committed tags are stored in UTF-8
181 writetags(fp, names, encoding.fromlocal, prevtags)
181 writetags(fp, names, encoding.fromlocal, prevtags)
182
182
183 if '.hgtags' not in self.dirstate:
183 if '.hgtags' not in self.dirstate:
184 self.add(['.hgtags'])
184 self.add(['.hgtags'])
185
185
186 tagnode = self.commit(['.hgtags'], message, user, date, extra=extra)
186 tagnode = self.commit(['.hgtags'], message, user, date, extra=extra)
187
187
188 for name in names:
188 for name in names:
189 self.hook('tag', node=hex(node), tag=name, local=local)
189 self.hook('tag', node=hex(node), tag=name, local=local)
190
190
191 return tagnode
191 return tagnode
192
192
193 def tag(self, names, node, message, local, user, date):
193 def tag(self, names, node, message, local, user, date):
194 '''tag a revision with one or more symbolic names.
194 '''tag a revision with one or more symbolic names.
195
195
196 names is a list of strings or, when adding a single tag, names may be a
196 names is a list of strings or, when adding a single tag, names may be a
197 string.
197 string.
198
198
199 if local is True, the tags are stored in a per-repository file.
199 if local is True, the tags are stored in a per-repository file.
200 otherwise, they are stored in the .hgtags file, and a new
200 otherwise, they are stored in the .hgtags file, and a new
201 changeset is committed with the change.
201 changeset is committed with the change.
202
202
203 keyword arguments:
203 keyword arguments:
204
204
205 local: whether to store tags in non-version-controlled file
205 local: whether to store tags in non-version-controlled file
206 (default False)
206 (default False)
207
207
208 message: commit message to use if committing
208 message: commit message to use if committing
209
209
210 user: name of user to use if committing
210 user: name of user to use if committing
211
211
212 date: date tuple to use if committing'''
212 date: date tuple to use if committing'''
213
213
214 for x in self.status()[:5]:
214 for x in self.status()[:5]:
215 if '.hgtags' in x:
215 if '.hgtags' in x:
216 raise util.Abort(_('working copy of .hgtags is changed '
216 raise util.Abort(_('working copy of .hgtags is changed '
217 '(please commit .hgtags manually)'))
217 '(please commit .hgtags manually)'))
218
218
219 self.tags() # instantiate the cache
219 self.tags() # instantiate the cache
220 self._tag(names, node, message, local, user, date)
220 self._tag(names, node, message, local, user, date)
221
221
222 def tags(self):
222 def tags(self):
223 '''return a mapping of tag to node'''
223 '''return a mapping of tag to node'''
224 if self.tagscache:
224 if self.tagscache:
225 return self.tagscache
225 return self.tagscache
226
226
227 globaltags = {}
227 globaltags = {}
228 tagtypes = {}
228 tagtypes = {}
229
229
230 def readtags(lines, fn, tagtype):
230 def readtags(lines, fn, tagtype):
231 filetags = {}
231 filetags = {}
232 count = 0
232 count = 0
233
233
234 def warn(msg):
234 def warn(msg):
235 self.ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
235 self.ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
236
236
237 for l in lines:
237 for l in lines:
238 count += 1
238 count += 1
239 if not l:
239 if not l:
240 continue
240 continue
241 s = l.split(" ", 1)
241 s = l.split(" ", 1)
242 if len(s) != 2:
242 if len(s) != 2:
243 warn(_("cannot parse entry"))
243 warn(_("cannot parse entry"))
244 continue
244 continue
245 node, key = s
245 node, key = s
246 key = encoding.tolocal(key.strip()) # stored in UTF-8
246 key = encoding.tolocal(key.strip()) # stored in UTF-8
247 try:
247 try:
248 bin_n = bin(node)
248 bin_n = bin(node)
249 except TypeError:
249 except TypeError:
250 warn(_("node '%s' is not well formed") % node)
250 warn(_("node '%s' is not well formed") % node)
251 continue
251 continue
252 if bin_n not in self.changelog.nodemap:
252 if bin_n not in self.changelog.nodemap:
253 warn(_("tag '%s' refers to unknown node") % key)
253 warn(_("tag '%s' refers to unknown node") % key)
254 continue
254 continue
255
255
256 h = []
256 h = []
257 if key in filetags:
257 if key in filetags:
258 n, h = filetags[key]
258 n, h = filetags[key]
259 h.append(n)
259 h.append(n)
260 filetags[key] = (bin_n, h)
260 filetags[key] = (bin_n, h)
261
261
262 for k, nh in filetags.iteritems():
262 for k, nh in filetags.iteritems():
263 if k not in globaltags:
263 if k not in globaltags:
264 globaltags[k] = nh
264 globaltags[k] = nh
265 tagtypes[k] = tagtype
265 tagtypes[k] = tagtype
266 continue
266 continue
267
267
268 # we prefer the global tag if:
268 # we prefer the global tag if:
269 # it supercedes us OR
269 # it supercedes us OR
270 # mutual supercedes and it has a higher rank
270 # mutual supercedes and it has a higher rank
271 # otherwise we win because we're tip-most
271 # otherwise we win because we're tip-most
272 an, ah = nh
272 an, ah = nh
273 bn, bh = globaltags[k]
273 bn, bh = globaltags[k]
274 if (bn != an and an in bh and
274 if (bn != an and an in bh and
275 (bn not in ah or len(bh) > len(ah))):
275 (bn not in ah or len(bh) > len(ah))):
276 an = bn
276 an = bn
277 ah.extend([n for n in bh if n not in ah])
277 ah.extend([n for n in bh if n not in ah])
278 globaltags[k] = an, ah
278 globaltags[k] = an, ah
279 tagtypes[k] = tagtype
279 tagtypes[k] = tagtype
280
280
281 # read the tags file from each head, ending with the tip
281 # read the tags file from each head, ending with the tip
282 f = None
282 f = None
283 for rev, node, fnode in self._hgtagsnodes():
283 for rev, node, fnode in self._hgtagsnodes():
284 f = (f and f.filectx(fnode) or
284 f = (f and f.filectx(fnode) or
285 self.filectx('.hgtags', fileid=fnode))
285 self.filectx('.hgtags', fileid=fnode))
286 readtags(f.data().splitlines(), f, "global")
286 readtags(f.data().splitlines(), f, "global")
287
287
288 try:
288 try:
289 data = encoding.fromlocal(self.opener("localtags").read())
289 data = encoding.fromlocal(self.opener("localtags").read())
290 # localtags are stored in the local character set
290 # localtags are stored in the local character set
291 # while the internal tag table is stored in UTF-8
291 # while the internal tag table is stored in UTF-8
292 readtags(data.splitlines(), "localtags", "local")
292 readtags(data.splitlines(), "localtags", "local")
293 except IOError:
293 except IOError:
294 pass
294 pass
295
295
296 self.tagscache = {}
296 self.tagscache = {}
297 self._tagstypecache = {}
297 self._tagstypecache = {}
298 for k, nh in globaltags.iteritems():
298 for k, nh in globaltags.iteritems():
299 n = nh[0]
299 n = nh[0]
300 if n != nullid:
300 if n != nullid:
301 self.tagscache[k] = n
301 self.tagscache[k] = n
302 self._tagstypecache[k] = tagtypes[k]
302 self._tagstypecache[k] = tagtypes[k]
303 self.tagscache['tip'] = self.changelog.tip()
303 self.tagscache['tip'] = self.changelog.tip()
304 return self.tagscache
304 return self.tagscache
305
305
306 def tagtype(self, tagname):
306 def tagtype(self, tagname):
307 '''
307 '''
308 return the type of the given tag. result can be:
308 return the type of the given tag. result can be:
309
309
310 'local' : a local tag
310 'local' : a local tag
311 'global' : a global tag
311 'global' : a global tag
312 None : tag does not exist
312 None : tag does not exist
313 '''
313 '''
314
314
315 self.tags()
315 self.tags()
316
316
317 return self._tagstypecache.get(tagname)
317 return self._tagstypecache.get(tagname)
318
318
319 def _hgtagsnodes(self):
319 def _hgtagsnodes(self):
320 last = {}
320 last = {}
321 ret = []
321 ret = []
322 for node in reversed(self.heads()):
322 for node in reversed(self.heads()):
323 c = self[node]
323 c = self[node]
324 rev = c.rev()
324 rev = c.rev()
325 try:
325 try:
326 fnode = c.filenode('.hgtags')
326 fnode = c.filenode('.hgtags')
327 except error.LookupError:
327 except error.LookupError:
328 continue
328 continue
329 ret.append((rev, node, fnode))
329 ret.append((rev, node, fnode))
330 if fnode in last:
330 if fnode in last:
331 ret[last[fnode]] = None
331 ret[last[fnode]] = None
332 last[fnode] = len(ret) - 1
332 last[fnode] = len(ret) - 1
333 return [item for item in ret if item]
333 return [item for item in ret if item]
334
334
335 def tagslist(self):
335 def tagslist(self):
336 '''return a list of tags ordered by revision'''
336 '''return a list of tags ordered by revision'''
337 l = []
337 l = []
338 for t, n in self.tags().iteritems():
338 for t, n in self.tags().iteritems():
339 try:
339 try:
340 r = self.changelog.rev(n)
340 r = self.changelog.rev(n)
341 except:
341 except:
342 r = -2 # sort to the beginning of the list if unknown
342 r = -2 # sort to the beginning of the list if unknown
343 l.append((r, t, n))
343 l.append((r, t, n))
344 return [(t, n) for r, t, n in sorted(l)]
344 return [(t, n) for r, t, n in sorted(l)]
345
345
346 def nodetags(self, node):
346 def nodetags(self, node):
347 '''return the tags associated with a node'''
347 '''return the tags associated with a node'''
348 if not self.nodetagscache:
348 if not self.nodetagscache:
349 self.nodetagscache = {}
349 self.nodetagscache = {}
350 for t, n in self.tags().iteritems():
350 for t, n in self.tags().iteritems():
351 self.nodetagscache.setdefault(n, []).append(t)
351 self.nodetagscache.setdefault(n, []).append(t)
352 return self.nodetagscache.get(node, [])
352 return self.nodetagscache.get(node, [])
353
353
354 def _branchtags(self, partial, lrev):
354 def _branchtags(self, partial, lrev):
355 # TODO: rename this function?
355 # TODO: rename this function?
356 tiprev = len(self) - 1
356 tiprev = len(self) - 1
357 if lrev != tiprev:
357 if lrev != tiprev:
358 self._updatebranchcache(partial, lrev+1, tiprev+1)
358 self._updatebranchcache(partial, lrev+1, tiprev+1)
359 self._writebranchcache(partial, self.changelog.tip(), tiprev)
359 self._writebranchcache(partial, self.changelog.tip(), tiprev)
360
360
361 return partial
361 return partial
362
362
363 def branchmap(self):
363 def branchmap(self):
364 tip = self.changelog.tip()
364 tip = self.changelog.tip()
365 if self.branchcache is not None and self._branchcachetip == tip:
365 if self.branchcache is not None and self._branchcachetip == tip:
366 return self.branchcache
366 return self.branchcache
367
367
368 oldtip = self._branchcachetip
368 oldtip = self._branchcachetip
369 self._branchcachetip = tip
369 self._branchcachetip = tip
370 if self.branchcache is None:
370 if self.branchcache is None:
371 self.branchcache = {} # avoid recursion in changectx
371 self.branchcache = {} # avoid recursion in changectx
372 else:
372 else:
373 self.branchcache.clear() # keep using the same dict
373 self.branchcache.clear() # keep using the same dict
374 if oldtip is None or oldtip not in self.changelog.nodemap:
374 if oldtip is None or oldtip not in self.changelog.nodemap:
375 partial, last, lrev = self._readbranchcache()
375 partial, last, lrev = self._readbranchcache()
376 else:
376 else:
377 lrev = self.changelog.rev(oldtip)
377 lrev = self.changelog.rev(oldtip)
378 partial = self._ubranchcache
378 partial = self._ubranchcache
379
379
380 self._branchtags(partial, lrev)
380 self._branchtags(partial, lrev)
381 # this private cache holds all heads (not just tips)
381 # this private cache holds all heads (not just tips)
382 self._ubranchcache = partial
382 self._ubranchcache = partial
383
383
384 # the branch cache is stored on disk as UTF-8, but in the local
384 # the branch cache is stored on disk as UTF-8, but in the local
385 # charset internally
385 # charset internally
386 for k, v in partial.iteritems():
386 for k, v in partial.iteritems():
387 self.branchcache[encoding.tolocal(k)] = v
387 self.branchcache[encoding.tolocal(k)] = v
388 return self.branchcache
388 return self.branchcache
389
389
390
390
391 def branchtags(self):
391 def branchtags(self):
392 '''return a dict where branch names map to the tipmost head of
392 '''return a dict where branch names map to the tipmost head of
393 the branch, open heads come before closed'''
393 the branch, open heads come before closed'''
394 bt = {}
394 bt = {}
395 for bn, heads in self.branchmap().iteritems():
395 for bn, heads in self.branchmap().iteritems():
396 head = None
396 head = None
397 for i in range(len(heads)-1, -1, -1):
397 for i in range(len(heads)-1, -1, -1):
398 h = heads[i]
398 h = heads[i]
399 if 'close' not in self.changelog.read(h)[5]:
399 if 'close' not in self.changelog.read(h)[5]:
400 head = h
400 head = h
401 break
401 break
402 # no open heads were found
402 # no open heads were found
403 if head is None:
403 if head is None:
404 head = heads[-1]
404 head = heads[-1]
405 bt[bn] = head
405 bt[bn] = head
406 return bt
406 return bt
407
407
408
408
409 def _readbranchcache(self):
409 def _readbranchcache(self):
410 partial = {}
410 partial = {}
411 try:
411 try:
412 f = self.opener("branchheads.cache")
412 f = self.opener("branchheads.cache")
413 lines = f.read().split('\n')
413 lines = f.read().split('\n')
414 f.close()
414 f.close()
415 except (IOError, OSError):
415 except (IOError, OSError):
416 return {}, nullid, nullrev
416 return {}, nullid, nullrev
417
417
418 try:
418 try:
419 last, lrev = lines.pop(0).split(" ", 1)
419 last, lrev = lines.pop(0).split(" ", 1)
420 last, lrev = bin(last), int(lrev)
420 last, lrev = bin(last), int(lrev)
421 if lrev >= len(self) or self[lrev].node() != last:
421 if lrev >= len(self) or self[lrev].node() != last:
422 # invalidate the cache
422 # invalidate the cache
423 raise ValueError('invalidating branch cache (tip differs)')
423 raise ValueError('invalidating branch cache (tip differs)')
424 for l in lines:
424 for l in lines:
425 if not l: continue
425 if not l: continue
426 node, label = l.split(" ", 1)
426 node, label = l.split(" ", 1)
427 partial.setdefault(label.strip(), []).append(bin(node))
427 partial.setdefault(label.strip(), []).append(bin(node))
428 except KeyboardInterrupt:
428 except KeyboardInterrupt:
429 raise
429 raise
430 except Exception, inst:
430 except Exception, inst:
431 if self.ui.debugflag:
431 if self.ui.debugflag:
432 self.ui.warn(str(inst), '\n')
432 self.ui.warn(str(inst), '\n')
433 partial, last, lrev = {}, nullid, nullrev
433 partial, last, lrev = {}, nullid, nullrev
434 return partial, last, lrev
434 return partial, last, lrev
435
435
436 def _writebranchcache(self, branches, tip, tiprev):
436 def _writebranchcache(self, branches, tip, tiprev):
437 try:
437 try:
438 f = self.opener("branchheads.cache", "w", atomictemp=True)
438 f = self.opener("branchheads.cache", "w", atomictemp=True)
439 f.write("%s %s\n" % (hex(tip), tiprev))
439 f.write("%s %s\n" % (hex(tip), tiprev))
440 for label, nodes in branches.iteritems():
440 for label, nodes in branches.iteritems():
441 for node in nodes:
441 for node in nodes:
442 f.write("%s %s\n" % (hex(node), label))
442 f.write("%s %s\n" % (hex(node), label))
443 f.rename()
443 f.rename()
444 except (IOError, OSError):
444 except (IOError, OSError):
445 pass
445 pass
446
446
447 def _updatebranchcache(self, partial, start, end):
447 def _updatebranchcache(self, partial, start, end):
448 for r in xrange(start, end):
448 for r in xrange(start, end):
449 c = self[r]
449 c = self[r]
450 b = c.branch()
450 b = c.branch()
451 bheads = partial.setdefault(b, [])
451 bheads = partial.setdefault(b, [])
452 bheads.append(c.node())
452 bheads.append(c.node())
453 for p in c.parents():
453 for p in c.parents():
454 pn = p.node()
454 pn = p.node()
455 if pn in bheads:
455 if pn in bheads:
456 bheads.remove(pn)
456 bheads.remove(pn)
457
457
458 def lookup(self, key):
458 def lookup(self, key):
459 if isinstance(key, int):
459 if isinstance(key, int):
460 return self.changelog.node(key)
460 return self.changelog.node(key)
461 elif key == '.':
461 elif key == '.':
462 return self.dirstate.parents()[0]
462 return self.dirstate.parents()[0]
463 elif key == 'null':
463 elif key == 'null':
464 return nullid
464 return nullid
465 elif key == 'tip':
465 elif key == 'tip':
466 return self.changelog.tip()
466 return self.changelog.tip()
467 n = self.changelog._match(key)
467 n = self.changelog._match(key)
468 if n:
468 if n:
469 return n
469 return n
470 if key in self.tags():
470 if key in self.tags():
471 return self.tags()[key]
471 return self.tags()[key]
472 if key in self.branchtags():
472 if key in self.branchtags():
473 return self.branchtags()[key]
473 return self.branchtags()[key]
474 n = self.changelog._partialmatch(key)
474 n = self.changelog._partialmatch(key)
475 if n:
475 if n:
476 return n
476 return n
477 try:
477 try:
478 if len(key) == 20:
478 if len(key) == 20:
479 key = hex(key)
479 key = hex(key)
480 except:
480 except:
481 pass
481 pass
482 raise error.RepoError(_("unknown revision '%s'") % key)
482 raise error.RepoError(_("unknown revision '%s'") % key)
483
483
484 def local(self):
484 def local(self):
485 return True
485 return True
486
486
487 def join(self, f):
487 def join(self, f):
488 return os.path.join(self.path, f)
488 return os.path.join(self.path, f)
489
489
490 def wjoin(self, f):
490 def wjoin(self, f):
491 return os.path.join(self.root, f)
491 return os.path.join(self.root, f)
492
492
493 def rjoin(self, f):
493 def rjoin(self, f):
494 return os.path.join(self.root, util.pconvert(f))
494 return os.path.join(self.root, util.pconvert(f))
495
495
496 def file(self, f):
496 def file(self, f):
497 if f[0] == '/':
497 if f[0] == '/':
498 f = f[1:]
498 f = f[1:]
499 return filelog.filelog(self.sopener, f)
499 return filelog.filelog(self.sopener, f)
500
500
501 def changectx(self, changeid):
501 def changectx(self, changeid):
502 return self[changeid]
502 return self[changeid]
503
503
504 def parents(self, changeid=None):
504 def parents(self, changeid=None):
505 '''get list of changectxs for parents of changeid'''
505 '''get list of changectxs for parents of changeid'''
506 return self[changeid].parents()
506 return self[changeid].parents()
507
507
508 def filectx(self, path, changeid=None, fileid=None):
508 def filectx(self, path, changeid=None, fileid=None):
509 """changeid can be a changeset revision, node, or tag.
509 """changeid can be a changeset revision, node, or tag.
510 fileid can be a file revision or node."""
510 fileid can be a file revision or node."""
511 return context.filectx(self, path, changeid, fileid)
511 return context.filectx(self, path, changeid, fileid)
512
512
513 def getcwd(self):
513 def getcwd(self):
514 return self.dirstate.getcwd()
514 return self.dirstate.getcwd()
515
515
516 def pathto(self, f, cwd=None):
516 def pathto(self, f, cwd=None):
517 return self.dirstate.pathto(f, cwd)
517 return self.dirstate.pathto(f, cwd)
518
518
519 def wfile(self, f, mode='r'):
519 def wfile(self, f, mode='r'):
520 return self.wopener(f, mode)
520 return self.wopener(f, mode)
521
521
522 def _link(self, f):
522 def _link(self, f):
523 return os.path.islink(self.wjoin(f))
523 return os.path.islink(self.wjoin(f))
524
524
525 def _filter(self, filter, filename, data):
525 def _filter(self, filter, filename, data):
526 if filter not in self.filterpats:
526 if filter not in self.filterpats:
527 l = []
527 l = []
528 for pat, cmd in self.ui.configitems(filter):
528 for pat, cmd in self.ui.configitems(filter):
529 if cmd == '!':
529 if cmd == '!':
530 continue
530 continue
531 mf = util.matcher(self.root, "", [pat], [], [])[1]
531 mf = match_.match(self.root, '', [pat], [], [], 'glob')
532 fn = None
532 fn = None
533 params = cmd
533 params = cmd
534 for name, filterfn in self._datafilters.iteritems():
534 for name, filterfn in self._datafilters.iteritems():
535 if cmd.startswith(name):
535 if cmd.startswith(name):
536 fn = filterfn
536 fn = filterfn
537 params = cmd[len(name):].lstrip()
537 params = cmd[len(name):].lstrip()
538 break
538 break
539 if not fn:
539 if not fn:
540 fn = lambda s, c, **kwargs: util.filter(s, c)
540 fn = lambda s, c, **kwargs: util.filter(s, c)
541 # Wrap old filters not supporting keyword arguments
541 # Wrap old filters not supporting keyword arguments
542 if not inspect.getargspec(fn)[2]:
542 if not inspect.getargspec(fn)[2]:
543 oldfn = fn
543 oldfn = fn
544 fn = lambda s, c, **kwargs: oldfn(s, c)
544 fn = lambda s, c, **kwargs: oldfn(s, c)
545 l.append((mf, fn, params))
545 l.append((mf, fn, params))
546 self.filterpats[filter] = l
546 self.filterpats[filter] = l
547
547
548 for mf, fn, cmd in self.filterpats[filter]:
548 for mf, fn, cmd in self.filterpats[filter]:
549 if mf(filename):
549 if mf(filename):
550 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
550 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
551 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
551 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
552 break
552 break
553
553
554 return data
554 return data
555
555
556 def adddatafilter(self, name, filter):
556 def adddatafilter(self, name, filter):
557 self._datafilters[name] = filter
557 self._datafilters[name] = filter
558
558
559 def wread(self, filename):
559 def wread(self, filename):
560 if self._link(filename):
560 if self._link(filename):
561 data = os.readlink(self.wjoin(filename))
561 data = os.readlink(self.wjoin(filename))
562 else:
562 else:
563 data = self.wopener(filename, 'r').read()
563 data = self.wopener(filename, 'r').read()
564 return self._filter("encode", filename, data)
564 return self._filter("encode", filename, data)
565
565
566 def wwrite(self, filename, data, flags):
566 def wwrite(self, filename, data, flags):
567 data = self._filter("decode", filename, data)
567 data = self._filter("decode", filename, data)
568 try:
568 try:
569 os.unlink(self.wjoin(filename))
569 os.unlink(self.wjoin(filename))
570 except OSError:
570 except OSError:
571 pass
571 pass
572 if 'l' in flags:
572 if 'l' in flags:
573 self.wopener.symlink(data, filename)
573 self.wopener.symlink(data, filename)
574 else:
574 else:
575 self.wopener(filename, 'w').write(data)
575 self.wopener(filename, 'w').write(data)
576 if 'x' in flags:
576 if 'x' in flags:
577 util.set_flags(self.wjoin(filename), False, True)
577 util.set_flags(self.wjoin(filename), False, True)
578
578
579 def wwritedata(self, filename, data):
579 def wwritedata(self, filename, data):
580 return self._filter("decode", filename, data)
580 return self._filter("decode", filename, data)
581
581
582 def transaction(self):
582 def transaction(self):
583 tr = self._transref and self._transref() or None
583 tr = self._transref and self._transref() or None
584 if tr and tr.running():
584 if tr and tr.running():
585 return tr.nest()
585 return tr.nest()
586
586
587 # abort here if the journal already exists
587 # abort here if the journal already exists
588 if os.path.exists(self.sjoin("journal")):
588 if os.path.exists(self.sjoin("journal")):
589 raise error.RepoError(_("journal already exists - run hg recover"))
589 raise error.RepoError(_("journal already exists - run hg recover"))
590
590
591 # save dirstate for rollback
591 # save dirstate for rollback
592 try:
592 try:
593 ds = self.opener("dirstate").read()
593 ds = self.opener("dirstate").read()
594 except IOError:
594 except IOError:
595 ds = ""
595 ds = ""
596 self.opener("journal.dirstate", "w").write(ds)
596 self.opener("journal.dirstate", "w").write(ds)
597 self.opener("journal.branch", "w").write(self.dirstate.branch())
597 self.opener("journal.branch", "w").write(self.dirstate.branch())
598
598
599 renames = [(self.sjoin("journal"), self.sjoin("undo")),
599 renames = [(self.sjoin("journal"), self.sjoin("undo")),
600 (self.join("journal.dirstate"), self.join("undo.dirstate")),
600 (self.join("journal.dirstate"), self.join("undo.dirstate")),
601 (self.join("journal.branch"), self.join("undo.branch"))]
601 (self.join("journal.branch"), self.join("undo.branch"))]
602 tr = transaction.transaction(self.ui.warn, self.sopener,
602 tr = transaction.transaction(self.ui.warn, self.sopener,
603 self.sjoin("journal"),
603 self.sjoin("journal"),
604 aftertrans(renames),
604 aftertrans(renames),
605 self.store.createmode)
605 self.store.createmode)
606 self._transref = weakref.ref(tr)
606 self._transref = weakref.ref(tr)
607 return tr
607 return tr
608
608
609 def recover(self):
609 def recover(self):
610 lock = self.lock()
610 lock = self.lock()
611 try:
611 try:
612 if os.path.exists(self.sjoin("journal")):
612 if os.path.exists(self.sjoin("journal")):
613 self.ui.status(_("rolling back interrupted transaction\n"))
613 self.ui.status(_("rolling back interrupted transaction\n"))
614 transaction.rollback(self.sopener, self.sjoin("journal"), self.ui.warn)
614 transaction.rollback(self.sopener, self.sjoin("journal"), self.ui.warn)
615 self.invalidate()
615 self.invalidate()
616 return True
616 return True
617 else:
617 else:
618 self.ui.warn(_("no interrupted transaction available\n"))
618 self.ui.warn(_("no interrupted transaction available\n"))
619 return False
619 return False
620 finally:
620 finally:
621 lock.release()
621 lock.release()
622
622
623 def rollback(self):
623 def rollback(self):
624 wlock = lock = None
624 wlock = lock = None
625 try:
625 try:
626 wlock = self.wlock()
626 wlock = self.wlock()
627 lock = self.lock()
627 lock = self.lock()
628 if os.path.exists(self.sjoin("undo")):
628 if os.path.exists(self.sjoin("undo")):
629 self.ui.status(_("rolling back last transaction\n"))
629 self.ui.status(_("rolling back last transaction\n"))
630 transaction.rollback(self.sopener, self.sjoin("undo"), self.ui.warn)
630 transaction.rollback(self.sopener, self.sjoin("undo"), self.ui.warn)
631 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
631 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
632 try:
632 try:
633 branch = self.opener("undo.branch").read()
633 branch = self.opener("undo.branch").read()
634 self.dirstate.setbranch(branch)
634 self.dirstate.setbranch(branch)
635 except IOError:
635 except IOError:
636 self.ui.warn(_("Named branch could not be reset, "
636 self.ui.warn(_("Named branch could not be reset, "
637 "current branch still is: %s\n")
637 "current branch still is: %s\n")
638 % encoding.tolocal(self.dirstate.branch()))
638 % encoding.tolocal(self.dirstate.branch()))
639 self.invalidate()
639 self.invalidate()
640 self.dirstate.invalidate()
640 self.dirstate.invalidate()
641 else:
641 else:
642 self.ui.warn(_("no rollback information available\n"))
642 self.ui.warn(_("no rollback information available\n"))
643 finally:
643 finally:
644 release(lock, wlock)
644 release(lock, wlock)
645
645
646 def invalidate(self):
646 def invalidate(self):
647 for a in "changelog manifest".split():
647 for a in "changelog manifest".split():
648 if a in self.__dict__:
648 if a in self.__dict__:
649 delattr(self, a)
649 delattr(self, a)
650 self.tagscache = None
650 self.tagscache = None
651 self._tagstypecache = None
651 self._tagstypecache = None
652 self.nodetagscache = None
652 self.nodetagscache = None
653 self.branchcache = None
653 self.branchcache = None
654 self._ubranchcache = None
654 self._ubranchcache = None
655 self._branchcachetip = None
655 self._branchcachetip = None
656
656
657 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
657 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
658 try:
658 try:
659 l = lock.lock(lockname, 0, releasefn, desc=desc)
659 l = lock.lock(lockname, 0, releasefn, desc=desc)
660 except error.LockHeld, inst:
660 except error.LockHeld, inst:
661 if not wait:
661 if not wait:
662 raise
662 raise
663 self.ui.warn(_("waiting for lock on %s held by %r\n") %
663 self.ui.warn(_("waiting for lock on %s held by %r\n") %
664 (desc, inst.locker))
664 (desc, inst.locker))
665 # default to 600 seconds timeout
665 # default to 600 seconds timeout
666 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
666 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
667 releasefn, desc=desc)
667 releasefn, desc=desc)
668 if acquirefn:
668 if acquirefn:
669 acquirefn()
669 acquirefn()
670 return l
670 return l
671
671
672 def lock(self, wait=True):
672 def lock(self, wait=True):
673 l = self._lockref and self._lockref()
673 l = self._lockref and self._lockref()
674 if l is not None and l.held:
674 if l is not None and l.held:
675 l.lock()
675 l.lock()
676 return l
676 return l
677
677
678 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
678 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
679 _('repository %s') % self.origroot)
679 _('repository %s') % self.origroot)
680 self._lockref = weakref.ref(l)
680 self._lockref = weakref.ref(l)
681 return l
681 return l
682
682
683 def wlock(self, wait=True):
683 def wlock(self, wait=True):
684 l = self._wlockref and self._wlockref()
684 l = self._wlockref and self._wlockref()
685 if l is not None and l.held:
685 if l is not None and l.held:
686 l.lock()
686 l.lock()
687 return l
687 return l
688
688
689 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
689 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
690 self.dirstate.invalidate, _('working directory of %s') %
690 self.dirstate.invalidate, _('working directory of %s') %
691 self.origroot)
691 self.origroot)
692 self._wlockref = weakref.ref(l)
692 self._wlockref = weakref.ref(l)
693 return l
693 return l
694
694
695 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
695 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
696 """
696 """
697 commit an individual file as part of a larger transaction
697 commit an individual file as part of a larger transaction
698 """
698 """
699
699
700 fname = fctx.path()
700 fname = fctx.path()
701 text = fctx.data()
701 text = fctx.data()
702 flog = self.file(fname)
702 flog = self.file(fname)
703 fparent1 = manifest1.get(fname, nullid)
703 fparent1 = manifest1.get(fname, nullid)
704 fparent2 = fparent2o = manifest2.get(fname, nullid)
704 fparent2 = fparent2o = manifest2.get(fname, nullid)
705
705
706 meta = {}
706 meta = {}
707 copy = fctx.renamed()
707 copy = fctx.renamed()
708 if copy and copy[0] != fname:
708 if copy and copy[0] != fname:
709 # Mark the new revision of this file as a copy of another
709 # Mark the new revision of this file as a copy of another
710 # file. This copy data will effectively act as a parent
710 # file. This copy data will effectively act as a parent
711 # of this new revision. If this is a merge, the first
711 # of this new revision. If this is a merge, the first
712 # parent will be the nullid (meaning "look up the copy data")
712 # parent will be the nullid (meaning "look up the copy data")
713 # and the second one will be the other parent. For example:
713 # and the second one will be the other parent. For example:
714 #
714 #
715 # 0 --- 1 --- 3 rev1 changes file foo
715 # 0 --- 1 --- 3 rev1 changes file foo
716 # \ / rev2 renames foo to bar and changes it
716 # \ / rev2 renames foo to bar and changes it
717 # \- 2 -/ rev3 should have bar with all changes and
717 # \- 2 -/ rev3 should have bar with all changes and
718 # should record that bar descends from
718 # should record that bar descends from
719 # bar in rev2 and foo in rev1
719 # bar in rev2 and foo in rev1
720 #
720 #
721 # this allows this merge to succeed:
721 # this allows this merge to succeed:
722 #
722 #
723 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
723 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
724 # \ / merging rev3 and rev4 should use bar@rev2
724 # \ / merging rev3 and rev4 should use bar@rev2
725 # \- 2 --- 4 as the merge base
725 # \- 2 --- 4 as the merge base
726 #
726 #
727
727
728 cfname = copy[0]
728 cfname = copy[0]
729 crev = manifest1.get(cfname)
729 crev = manifest1.get(cfname)
730 newfparent = fparent2
730 newfparent = fparent2
731
731
732 if manifest2: # branch merge
732 if manifest2: # branch merge
733 if fparent2 == nullid or crev is None: # copied on remote side
733 if fparent2 == nullid or crev is None: # copied on remote side
734 if cfname in manifest2:
734 if cfname in manifest2:
735 crev = manifest2[cfname]
735 crev = manifest2[cfname]
736 newfparent = fparent1
736 newfparent = fparent1
737
737
738 # find source in nearest ancestor if we've lost track
738 # find source in nearest ancestor if we've lost track
739 if not crev:
739 if not crev:
740 self.ui.debug(_(" %s: searching for copy revision for %s\n") %
740 self.ui.debug(_(" %s: searching for copy revision for %s\n") %
741 (fname, cfname))
741 (fname, cfname))
742 for ancestor in self['.'].ancestors():
742 for ancestor in self['.'].ancestors():
743 if cfname in ancestor:
743 if cfname in ancestor:
744 crev = ancestor[cfname].filenode()
744 crev = ancestor[cfname].filenode()
745 break
745 break
746
746
747 self.ui.debug(_(" %s: copy %s:%s\n") % (fname, cfname, hex(crev)))
747 self.ui.debug(_(" %s: copy %s:%s\n") % (fname, cfname, hex(crev)))
748 meta["copy"] = cfname
748 meta["copy"] = cfname
749 meta["copyrev"] = hex(crev)
749 meta["copyrev"] = hex(crev)
750 fparent1, fparent2 = nullid, newfparent
750 fparent1, fparent2 = nullid, newfparent
751 elif fparent2 != nullid:
751 elif fparent2 != nullid:
752 # is one parent an ancestor of the other?
752 # is one parent an ancestor of the other?
753 fparentancestor = flog.ancestor(fparent1, fparent2)
753 fparentancestor = flog.ancestor(fparent1, fparent2)
754 if fparentancestor == fparent1:
754 if fparentancestor == fparent1:
755 fparent1, fparent2 = fparent2, nullid
755 fparent1, fparent2 = fparent2, nullid
756 elif fparentancestor == fparent2:
756 elif fparentancestor == fparent2:
757 fparent2 = nullid
757 fparent2 = nullid
758
758
759 # is the file changed?
759 # is the file changed?
760 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
760 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
761 changelist.append(fname)
761 changelist.append(fname)
762 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
762 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
763
763
764 # are just the flags changed during merge?
764 # are just the flags changed during merge?
765 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
765 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
766 changelist.append(fname)
766 changelist.append(fname)
767
767
768 return fparent1
768 return fparent1
769
769
770 def commit(self, files=None, text="", user=None, date=None, match=None,
770 def commit(self, files=None, text="", user=None, date=None, match=None,
771 force=False, editor=False, extra={}):
771 force=False, editor=False, extra={}):
772 """Add a new revision to current repository.
772 """Add a new revision to current repository.
773
773
774 Revision information is gathered from the working directory, files and
774 Revision information is gathered from the working directory, files and
775 match can be used to filter the committed files.
775 match can be used to filter the committed files.
776 If editor is supplied, it is called to get a commit message.
776 If editor is supplied, it is called to get a commit message.
777 """
777 """
778 wlock = self.wlock()
778 wlock = self.wlock()
779 try:
779 try:
780 p1, p2 = self.dirstate.parents()
780 p1, p2 = self.dirstate.parents()
781
781
782 if (not force and p2 != nullid and match and
782 if (not force and p2 != nullid and match and
783 (match.files() or match.anypats())):
783 (match.files() or match.anypats())):
784 raise util.Abort(_('cannot partially commit a merge '
784 raise util.Abort(_('cannot partially commit a merge '
785 '(do not specify files or patterns)'))
785 '(do not specify files or patterns)'))
786
786
787 if files:
787 if files:
788 modified, removed = [], []
788 modified, removed = [], []
789 for f in sorted(set(files)):
789 for f in sorted(set(files)):
790 s = self.dirstate[f]
790 s = self.dirstate[f]
791 if s in 'nma':
791 if s in 'nma':
792 modified.append(f)
792 modified.append(f)
793 elif s == 'r':
793 elif s == 'r':
794 removed.append(f)
794 removed.append(f)
795 else:
795 else:
796 self.ui.warn(_("%s not tracked!\n") % f)
796 self.ui.warn(_("%s not tracked!\n") % f)
797 changes = [modified, [], removed, [], []]
797 changes = [modified, [], removed, [], []]
798 else:
798 else:
799 changes = self.status(match=match)
799 changes = self.status(match=match)
800
800
801 if (not force and not extra.get("close") and p2 == nullid
801 if (not force and not extra.get("close") and p2 == nullid
802 and not (changes[0] or changes[1] or changes[2])
802 and not (changes[0] or changes[1] or changes[2])
803 and self[None].branch() == self['.'].branch()):
803 and self[None].branch() == self['.'].branch()):
804 self.ui.status(_("nothing changed\n"))
804 self.ui.status(_("nothing changed\n"))
805 return None
805 return None
806
806
807 ms = merge_.mergestate(self)
807 ms = merge_.mergestate(self)
808 for f in changes[0]:
808 for f in changes[0]:
809 if f in ms and ms[f] == 'u':
809 if f in ms and ms[f] == 'u':
810 raise util.Abort(_("unresolved merge conflicts "
810 raise util.Abort(_("unresolved merge conflicts "
811 "(see hg resolve)"))
811 "(see hg resolve)"))
812
812
813 wctx = context.workingctx(self, (p1, p2), text, user, date,
813 wctx = context.workingctx(self, (p1, p2), text, user, date,
814 extra, changes)
814 extra, changes)
815 if editor:
815 if editor:
816 wctx._text = editor(self, wctx,
816 wctx._text = editor(self, wctx,
817 changes[1], changes[0], changes[2])
817 changes[1], changes[0], changes[2])
818 ret = self.commitctx(wctx, True)
818 ret = self.commitctx(wctx, True)
819
819
820 # update dirstate and mergestate
820 # update dirstate and mergestate
821 for f in changes[0] + changes[1]:
821 for f in changes[0] + changes[1]:
822 self.dirstate.normal(f)
822 self.dirstate.normal(f)
823 for f in changes[2]:
823 for f in changes[2]:
824 self.dirstate.forget(f)
824 self.dirstate.forget(f)
825 self.dirstate.setparents(ret)
825 self.dirstate.setparents(ret)
826 ms.reset()
826 ms.reset()
827
827
828 return ret
828 return ret
829
829
830 finally:
830 finally:
831 wlock.release()
831 wlock.release()
832
832
833 def commitctx(self, ctx, error=False):
833 def commitctx(self, ctx, error=False):
834 """Add a new revision to current repository.
834 """Add a new revision to current repository.
835
835
836 Revision information is passed via the context argument.
836 Revision information is passed via the context argument.
837 """
837 """
838
838
839 tr = lock = None
839 tr = lock = None
840 removed = ctx.removed()
840 removed = ctx.removed()
841 p1, p2 = ctx.p1(), ctx.p2()
841 p1, p2 = ctx.p1(), ctx.p2()
842 m1 = p1.manifest().copy()
842 m1 = p1.manifest().copy()
843 m2 = p2.manifest()
843 m2 = p2.manifest()
844 user = ctx.user()
844 user = ctx.user()
845
845
846 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
846 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
847 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
847 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
848
848
849 lock = self.lock()
849 lock = self.lock()
850 try:
850 try:
851 tr = self.transaction()
851 tr = self.transaction()
852 trp = weakref.proxy(tr)
852 trp = weakref.proxy(tr)
853
853
854 # check in files
854 # check in files
855 new = {}
855 new = {}
856 changed = []
856 changed = []
857 linkrev = len(self)
857 linkrev = len(self)
858 for f in sorted(ctx.modified() + ctx.added()):
858 for f in sorted(ctx.modified() + ctx.added()):
859 self.ui.note(f + "\n")
859 self.ui.note(f + "\n")
860 try:
860 try:
861 fctx = ctx[f]
861 fctx = ctx[f]
862 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
862 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
863 changed)
863 changed)
864 m1.set(f, fctx.flags())
864 m1.set(f, fctx.flags())
865 except (OSError, IOError):
865 except (OSError, IOError):
866 if error:
866 if error:
867 self.ui.warn(_("trouble committing %s!\n") % f)
867 self.ui.warn(_("trouble committing %s!\n") % f)
868 raise
868 raise
869 else:
869 else:
870 removed.append(f)
870 removed.append(f)
871
871
872 # update manifest
872 # update manifest
873 m1.update(new)
873 m1.update(new)
874 removed = [f for f in sorted(removed) if f in m1 or f in m2]
874 removed = [f for f in sorted(removed) if f in m1 or f in m2]
875 drop = [f for f in removed if f in m1]
875 drop = [f for f in removed if f in m1]
876 for f in drop:
876 for f in drop:
877 del m1[f]
877 del m1[f]
878 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
878 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
879 p2.manifestnode(), (new, drop))
879 p2.manifestnode(), (new, drop))
880
880
881 # update changelog
881 # update changelog
882 self.changelog.delayupdate()
882 self.changelog.delayupdate()
883 n = self.changelog.add(mn, changed + removed, ctx.description(),
883 n = self.changelog.add(mn, changed + removed, ctx.description(),
884 trp, p1.node(), p2.node(),
884 trp, p1.node(), p2.node(),
885 user, ctx.date(), ctx.extra().copy())
885 user, ctx.date(), ctx.extra().copy())
886 p = lambda: self.changelog.writepending() and self.root or ""
886 p = lambda: self.changelog.writepending() and self.root or ""
887 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
887 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
888 parent2=xp2, pending=p)
888 parent2=xp2, pending=p)
889 self.changelog.finalize(trp)
889 self.changelog.finalize(trp)
890 tr.close()
890 tr.close()
891
891
892 if self.branchcache:
892 if self.branchcache:
893 self.branchtags()
893 self.branchtags()
894
894
895 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
895 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
896 return n
896 return n
897 finally:
897 finally:
898 del tr
898 del tr
899 lock.release()
899 lock.release()
900
900
901 def walk(self, match, node=None):
901 def walk(self, match, node=None):
902 '''
902 '''
903 walk recursively through the directory tree or a given
903 walk recursively through the directory tree or a given
904 changeset, finding all files matched by the match
904 changeset, finding all files matched by the match
905 function
905 function
906 '''
906 '''
907 return self[node].walk(match)
907 return self[node].walk(match)
908
908
909 def status(self, node1='.', node2=None, match=None,
909 def status(self, node1='.', node2=None, match=None,
910 ignored=False, clean=False, unknown=False):
910 ignored=False, clean=False, unknown=False):
911 """return status of files between two nodes or node and working directory
911 """return status of files between two nodes or node and working directory
912
912
913 If node1 is None, use the first dirstate parent instead.
913 If node1 is None, use the first dirstate parent instead.
914 If node2 is None, compare node1 with working directory.
914 If node2 is None, compare node1 with working directory.
915 """
915 """
916
916
917 def mfmatches(ctx):
917 def mfmatches(ctx):
918 mf = ctx.manifest().copy()
918 mf = ctx.manifest().copy()
919 for fn in mf.keys():
919 for fn in mf.keys():
920 if not match(fn):
920 if not match(fn):
921 del mf[fn]
921 del mf[fn]
922 return mf
922 return mf
923
923
924 if isinstance(node1, context.changectx):
924 if isinstance(node1, context.changectx):
925 ctx1 = node1
925 ctx1 = node1
926 else:
926 else:
927 ctx1 = self[node1]
927 ctx1 = self[node1]
928 if isinstance(node2, context.changectx):
928 if isinstance(node2, context.changectx):
929 ctx2 = node2
929 ctx2 = node2
930 else:
930 else:
931 ctx2 = self[node2]
931 ctx2 = self[node2]
932
932
933 working = ctx2.rev() is None
933 working = ctx2.rev() is None
934 parentworking = working and ctx1 == self['.']
934 parentworking = working and ctx1 == self['.']
935 match = match or match_.always(self.root, self.getcwd())
935 match = match or match_.always(self.root, self.getcwd())
936 listignored, listclean, listunknown = ignored, clean, unknown
936 listignored, listclean, listunknown = ignored, clean, unknown
937
937
938 # load earliest manifest first for caching reasons
938 # load earliest manifest first for caching reasons
939 if not working and ctx2.rev() < ctx1.rev():
939 if not working and ctx2.rev() < ctx1.rev():
940 ctx2.manifest()
940 ctx2.manifest()
941
941
942 if not parentworking:
942 if not parentworking:
943 def bad(f, msg):
943 def bad(f, msg):
944 if f not in ctx1:
944 if f not in ctx1:
945 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
945 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
946 return False
946 return False
947 match.bad = bad
947 match.bad = bad
948
948
949 if working: # we need to scan the working dir
949 if working: # we need to scan the working dir
950 s = self.dirstate.status(match, listignored, listclean, listunknown)
950 s = self.dirstate.status(match, listignored, listclean, listunknown)
951 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
951 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
952
952
953 # check for any possibly clean files
953 # check for any possibly clean files
954 if parentworking and cmp:
954 if parentworking and cmp:
955 fixup = []
955 fixup = []
956 # do a full compare of any files that might have changed
956 # do a full compare of any files that might have changed
957 for f in sorted(cmp):
957 for f in sorted(cmp):
958 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
958 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
959 or ctx1[f].cmp(ctx2[f].data())):
959 or ctx1[f].cmp(ctx2[f].data())):
960 modified.append(f)
960 modified.append(f)
961 else:
961 else:
962 fixup.append(f)
962 fixup.append(f)
963
963
964 if listclean:
964 if listclean:
965 clean += fixup
965 clean += fixup
966
966
967 # update dirstate for files that are actually clean
967 # update dirstate for files that are actually clean
968 if fixup:
968 if fixup:
969 wlock = None
969 wlock = None
970 try:
970 try:
971 try:
971 try:
972 # updating the dirstate is optional
972 # updating the dirstate is optional
973 # so we don't wait on the lock
973 # so we don't wait on the lock
974 wlock = self.wlock(False)
974 wlock = self.wlock(False)
975 for f in fixup:
975 for f in fixup:
976 self.dirstate.normal(f)
976 self.dirstate.normal(f)
977 except error.LockError:
977 except error.LockError:
978 pass
978 pass
979 finally:
979 finally:
980 release(wlock)
980 release(wlock)
981
981
982 if not parentworking:
982 if not parentworking:
983 mf1 = mfmatches(ctx1)
983 mf1 = mfmatches(ctx1)
984 if working:
984 if working:
985 # we are comparing working dir against non-parent
985 # we are comparing working dir against non-parent
986 # generate a pseudo-manifest for the working dir
986 # generate a pseudo-manifest for the working dir
987 mf2 = mfmatches(self['.'])
987 mf2 = mfmatches(self['.'])
988 for f in cmp + modified + added:
988 for f in cmp + modified + added:
989 mf2[f] = None
989 mf2[f] = None
990 mf2.set(f, ctx2.flags(f))
990 mf2.set(f, ctx2.flags(f))
991 for f in removed:
991 for f in removed:
992 if f in mf2:
992 if f in mf2:
993 del mf2[f]
993 del mf2[f]
994 else:
994 else:
995 # we are comparing two revisions
995 # we are comparing two revisions
996 deleted, unknown, ignored = [], [], []
996 deleted, unknown, ignored = [], [], []
997 mf2 = mfmatches(ctx2)
997 mf2 = mfmatches(ctx2)
998
998
999 modified, added, clean = [], [], []
999 modified, added, clean = [], [], []
1000 for fn in mf2:
1000 for fn in mf2:
1001 if fn in mf1:
1001 if fn in mf1:
1002 if (mf1.flags(fn) != mf2.flags(fn) or
1002 if (mf1.flags(fn) != mf2.flags(fn) or
1003 (mf1[fn] != mf2[fn] and
1003 (mf1[fn] != mf2[fn] and
1004 (mf2[fn] or ctx1[fn].cmp(ctx2[fn].data())))):
1004 (mf2[fn] or ctx1[fn].cmp(ctx2[fn].data())))):
1005 modified.append(fn)
1005 modified.append(fn)
1006 elif listclean:
1006 elif listclean:
1007 clean.append(fn)
1007 clean.append(fn)
1008 del mf1[fn]
1008 del mf1[fn]
1009 else:
1009 else:
1010 added.append(fn)
1010 added.append(fn)
1011 removed = mf1.keys()
1011 removed = mf1.keys()
1012
1012
1013 r = modified, added, removed, deleted, unknown, ignored, clean
1013 r = modified, added, removed, deleted, unknown, ignored, clean
1014 [l.sort() for l in r]
1014 [l.sort() for l in r]
1015 return r
1015 return r
1016
1016
1017 def add(self, list):
1017 def add(self, list):
1018 wlock = self.wlock()
1018 wlock = self.wlock()
1019 try:
1019 try:
1020 rejected = []
1020 rejected = []
1021 for f in list:
1021 for f in list:
1022 p = self.wjoin(f)
1022 p = self.wjoin(f)
1023 try:
1023 try:
1024 st = os.lstat(p)
1024 st = os.lstat(p)
1025 except:
1025 except:
1026 self.ui.warn(_("%s does not exist!\n") % f)
1026 self.ui.warn(_("%s does not exist!\n") % f)
1027 rejected.append(f)
1027 rejected.append(f)
1028 continue
1028 continue
1029 if st.st_size > 10000000:
1029 if st.st_size > 10000000:
1030 self.ui.warn(_("%s: files over 10MB may cause memory and"
1030 self.ui.warn(_("%s: files over 10MB may cause memory and"
1031 " performance problems\n"
1031 " performance problems\n"
1032 "(use 'hg revert %s' to unadd the file)\n")
1032 "(use 'hg revert %s' to unadd the file)\n")
1033 % (f, f))
1033 % (f, f))
1034 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1034 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1035 self.ui.warn(_("%s not added: only files and symlinks "
1035 self.ui.warn(_("%s not added: only files and symlinks "
1036 "supported currently\n") % f)
1036 "supported currently\n") % f)
1037 rejected.append(p)
1037 rejected.append(p)
1038 elif self.dirstate[f] in 'amn':
1038 elif self.dirstate[f] in 'amn':
1039 self.ui.warn(_("%s already tracked!\n") % f)
1039 self.ui.warn(_("%s already tracked!\n") % f)
1040 elif self.dirstate[f] == 'r':
1040 elif self.dirstate[f] == 'r':
1041 self.dirstate.normallookup(f)
1041 self.dirstate.normallookup(f)
1042 else:
1042 else:
1043 self.dirstate.add(f)
1043 self.dirstate.add(f)
1044 return rejected
1044 return rejected
1045 finally:
1045 finally:
1046 wlock.release()
1046 wlock.release()
1047
1047
1048 def forget(self, list):
1048 def forget(self, list):
1049 wlock = self.wlock()
1049 wlock = self.wlock()
1050 try:
1050 try:
1051 for f in list:
1051 for f in list:
1052 if self.dirstate[f] != 'a':
1052 if self.dirstate[f] != 'a':
1053 self.ui.warn(_("%s not added!\n") % f)
1053 self.ui.warn(_("%s not added!\n") % f)
1054 else:
1054 else:
1055 self.dirstate.forget(f)
1055 self.dirstate.forget(f)
1056 finally:
1056 finally:
1057 wlock.release()
1057 wlock.release()
1058
1058
1059 def remove(self, list, unlink=False):
1059 def remove(self, list, unlink=False):
1060 wlock = None
1060 wlock = None
1061 try:
1061 try:
1062 if unlink:
1062 if unlink:
1063 for f in list:
1063 for f in list:
1064 try:
1064 try:
1065 util.unlink(self.wjoin(f))
1065 util.unlink(self.wjoin(f))
1066 except OSError, inst:
1066 except OSError, inst:
1067 if inst.errno != errno.ENOENT:
1067 if inst.errno != errno.ENOENT:
1068 raise
1068 raise
1069 wlock = self.wlock()
1069 wlock = self.wlock()
1070 for f in list:
1070 for f in list:
1071 if unlink and os.path.exists(self.wjoin(f)):
1071 if unlink and os.path.exists(self.wjoin(f)):
1072 self.ui.warn(_("%s still exists!\n") % f)
1072 self.ui.warn(_("%s still exists!\n") % f)
1073 elif self.dirstate[f] == 'a':
1073 elif self.dirstate[f] == 'a':
1074 self.dirstate.forget(f)
1074 self.dirstate.forget(f)
1075 elif f not in self.dirstate:
1075 elif f not in self.dirstate:
1076 self.ui.warn(_("%s not tracked!\n") % f)
1076 self.ui.warn(_("%s not tracked!\n") % f)
1077 else:
1077 else:
1078 self.dirstate.remove(f)
1078 self.dirstate.remove(f)
1079 finally:
1079 finally:
1080 release(wlock)
1080 release(wlock)
1081
1081
1082 def undelete(self, list):
1082 def undelete(self, list):
1083 manifests = [self.manifest.read(self.changelog.read(p)[0])
1083 manifests = [self.manifest.read(self.changelog.read(p)[0])
1084 for p in self.dirstate.parents() if p != nullid]
1084 for p in self.dirstate.parents() if p != nullid]
1085 wlock = self.wlock()
1085 wlock = self.wlock()
1086 try:
1086 try:
1087 for f in list:
1087 for f in list:
1088 if self.dirstate[f] != 'r':
1088 if self.dirstate[f] != 'r':
1089 self.ui.warn(_("%s not removed!\n") % f)
1089 self.ui.warn(_("%s not removed!\n") % f)
1090 else:
1090 else:
1091 m = f in manifests[0] and manifests[0] or manifests[1]
1091 m = f in manifests[0] and manifests[0] or manifests[1]
1092 t = self.file(f).read(m[f])
1092 t = self.file(f).read(m[f])
1093 self.wwrite(f, t, m.flags(f))
1093 self.wwrite(f, t, m.flags(f))
1094 self.dirstate.normal(f)
1094 self.dirstate.normal(f)
1095 finally:
1095 finally:
1096 wlock.release()
1096 wlock.release()
1097
1097
1098 def copy(self, source, dest):
1098 def copy(self, source, dest):
1099 p = self.wjoin(dest)
1099 p = self.wjoin(dest)
1100 if not (os.path.exists(p) or os.path.islink(p)):
1100 if not (os.path.exists(p) or os.path.islink(p)):
1101 self.ui.warn(_("%s does not exist!\n") % dest)
1101 self.ui.warn(_("%s does not exist!\n") % dest)
1102 elif not (os.path.isfile(p) or os.path.islink(p)):
1102 elif not (os.path.isfile(p) or os.path.islink(p)):
1103 self.ui.warn(_("copy failed: %s is not a file or a "
1103 self.ui.warn(_("copy failed: %s is not a file or a "
1104 "symbolic link\n") % dest)
1104 "symbolic link\n") % dest)
1105 else:
1105 else:
1106 wlock = self.wlock()
1106 wlock = self.wlock()
1107 try:
1107 try:
1108 if self.dirstate[dest] in '?r':
1108 if self.dirstate[dest] in '?r':
1109 self.dirstate.add(dest)
1109 self.dirstate.add(dest)
1110 self.dirstate.copy(source, dest)
1110 self.dirstate.copy(source, dest)
1111 finally:
1111 finally:
1112 wlock.release()
1112 wlock.release()
1113
1113
1114 def heads(self, start=None, closed=True):
1114 def heads(self, start=None, closed=True):
1115 heads = self.changelog.heads(start)
1115 heads = self.changelog.heads(start)
1116 def display(head):
1116 def display(head):
1117 if closed:
1117 if closed:
1118 return True
1118 return True
1119 extras = self.changelog.read(head)[5]
1119 extras = self.changelog.read(head)[5]
1120 return ('close' not in extras)
1120 return ('close' not in extras)
1121 # sort the output in rev descending order
1121 # sort the output in rev descending order
1122 heads = [(-self.changelog.rev(h), h) for h in heads if display(h)]
1122 heads = [(-self.changelog.rev(h), h) for h in heads if display(h)]
1123 return [n for (r, n) in sorted(heads)]
1123 return [n for (r, n) in sorted(heads)]
1124
1124
1125 def branchheads(self, branch=None, start=None, closed=True):
1125 def branchheads(self, branch=None, start=None, closed=True):
1126 if branch is None:
1126 if branch is None:
1127 branch = self[None].branch()
1127 branch = self[None].branch()
1128 branches = self.branchmap()
1128 branches = self.branchmap()
1129 if branch not in branches:
1129 if branch not in branches:
1130 return []
1130 return []
1131 bheads = branches[branch]
1131 bheads = branches[branch]
1132 # the cache returns heads ordered lowest to highest
1132 # the cache returns heads ordered lowest to highest
1133 bheads.reverse()
1133 bheads.reverse()
1134 if start is not None:
1134 if start is not None:
1135 # filter out the heads that cannot be reached from startrev
1135 # filter out the heads that cannot be reached from startrev
1136 bheads = self.changelog.nodesbetween([start], bheads)[2]
1136 bheads = self.changelog.nodesbetween([start], bheads)[2]
1137 if not closed:
1137 if not closed:
1138 bheads = [h for h in bheads if
1138 bheads = [h for h in bheads if
1139 ('close' not in self.changelog.read(h)[5])]
1139 ('close' not in self.changelog.read(h)[5])]
1140 return bheads
1140 return bheads
1141
1141
1142 def branches(self, nodes):
1142 def branches(self, nodes):
1143 if not nodes:
1143 if not nodes:
1144 nodes = [self.changelog.tip()]
1144 nodes = [self.changelog.tip()]
1145 b = []
1145 b = []
1146 for n in nodes:
1146 for n in nodes:
1147 t = n
1147 t = n
1148 while 1:
1148 while 1:
1149 p = self.changelog.parents(n)
1149 p = self.changelog.parents(n)
1150 if p[1] != nullid or p[0] == nullid:
1150 if p[1] != nullid or p[0] == nullid:
1151 b.append((t, n, p[0], p[1]))
1151 b.append((t, n, p[0], p[1]))
1152 break
1152 break
1153 n = p[0]
1153 n = p[0]
1154 return b
1154 return b
1155
1155
1156 def between(self, pairs):
1156 def between(self, pairs):
1157 r = []
1157 r = []
1158
1158
1159 for top, bottom in pairs:
1159 for top, bottom in pairs:
1160 n, l, i = top, [], 0
1160 n, l, i = top, [], 0
1161 f = 1
1161 f = 1
1162
1162
1163 while n != bottom and n != nullid:
1163 while n != bottom and n != nullid:
1164 p = self.changelog.parents(n)[0]
1164 p = self.changelog.parents(n)[0]
1165 if i == f:
1165 if i == f:
1166 l.append(n)
1166 l.append(n)
1167 f = f * 2
1167 f = f * 2
1168 n = p
1168 n = p
1169 i += 1
1169 i += 1
1170
1170
1171 r.append(l)
1171 r.append(l)
1172
1172
1173 return r
1173 return r
1174
1174
1175 def findincoming(self, remote, base=None, heads=None, force=False):
1175 def findincoming(self, remote, base=None, heads=None, force=False):
1176 """Return list of roots of the subsets of missing nodes from remote
1176 """Return list of roots of the subsets of missing nodes from remote
1177
1177
1178 If base dict is specified, assume that these nodes and their parents
1178 If base dict is specified, assume that these nodes and their parents
1179 exist on the remote side and that no child of a node of base exists
1179 exist on the remote side and that no child of a node of base exists
1180 in both remote and self.
1180 in both remote and self.
1181 Furthermore base will be updated to include the nodes that exists
1181 Furthermore base will be updated to include the nodes that exists
1182 in self and remote but no children exists in self and remote.
1182 in self and remote but no children exists in self and remote.
1183 If a list of heads is specified, return only nodes which are heads
1183 If a list of heads is specified, return only nodes which are heads
1184 or ancestors of these heads.
1184 or ancestors of these heads.
1185
1185
1186 All the ancestors of base are in self and in remote.
1186 All the ancestors of base are in self and in remote.
1187 All the descendants of the list returned are missing in self.
1187 All the descendants of the list returned are missing in self.
1188 (and so we know that the rest of the nodes are missing in remote, see
1188 (and so we know that the rest of the nodes are missing in remote, see
1189 outgoing)
1189 outgoing)
1190 """
1190 """
1191 return self.findcommonincoming(remote, base, heads, force)[1]
1191 return self.findcommonincoming(remote, base, heads, force)[1]
1192
1192
1193 def findcommonincoming(self, remote, base=None, heads=None, force=False):
1193 def findcommonincoming(self, remote, base=None, heads=None, force=False):
1194 """Return a tuple (common, missing roots, heads) used to identify
1194 """Return a tuple (common, missing roots, heads) used to identify
1195 missing nodes from remote.
1195 missing nodes from remote.
1196
1196
1197 If base dict is specified, assume that these nodes and their parents
1197 If base dict is specified, assume that these nodes and their parents
1198 exist on the remote side and that no child of a node of base exists
1198 exist on the remote side and that no child of a node of base exists
1199 in both remote and self.
1199 in both remote and self.
1200 Furthermore base will be updated to include the nodes that exists
1200 Furthermore base will be updated to include the nodes that exists
1201 in self and remote but no children exists in self and remote.
1201 in self and remote but no children exists in self and remote.
1202 If a list of heads is specified, return only nodes which are heads
1202 If a list of heads is specified, return only nodes which are heads
1203 or ancestors of these heads.
1203 or ancestors of these heads.
1204
1204
1205 All the ancestors of base are in self and in remote.
1205 All the ancestors of base are in self and in remote.
1206 """
1206 """
1207 m = self.changelog.nodemap
1207 m = self.changelog.nodemap
1208 search = []
1208 search = []
1209 fetch = set()
1209 fetch = set()
1210 seen = set()
1210 seen = set()
1211 seenbranch = set()
1211 seenbranch = set()
1212 if base is None:
1212 if base is None:
1213 base = {}
1213 base = {}
1214
1214
1215 if not heads:
1215 if not heads:
1216 heads = remote.heads()
1216 heads = remote.heads()
1217
1217
1218 if self.changelog.tip() == nullid:
1218 if self.changelog.tip() == nullid:
1219 base[nullid] = 1
1219 base[nullid] = 1
1220 if heads != [nullid]:
1220 if heads != [nullid]:
1221 return [nullid], [nullid], list(heads)
1221 return [nullid], [nullid], list(heads)
1222 return [nullid], [], []
1222 return [nullid], [], []
1223
1223
1224 # assume we're closer to the tip than the root
1224 # assume we're closer to the tip than the root
1225 # and start by examining the heads
1225 # and start by examining the heads
1226 self.ui.status(_("searching for changes\n"))
1226 self.ui.status(_("searching for changes\n"))
1227
1227
1228 unknown = []
1228 unknown = []
1229 for h in heads:
1229 for h in heads:
1230 if h not in m:
1230 if h not in m:
1231 unknown.append(h)
1231 unknown.append(h)
1232 else:
1232 else:
1233 base[h] = 1
1233 base[h] = 1
1234
1234
1235 heads = unknown
1235 heads = unknown
1236 if not unknown:
1236 if not unknown:
1237 return base.keys(), [], []
1237 return base.keys(), [], []
1238
1238
1239 req = set(unknown)
1239 req = set(unknown)
1240 reqcnt = 0
1240 reqcnt = 0
1241
1241
1242 # search through remote branches
1242 # search through remote branches
1243 # a 'branch' here is a linear segment of history, with four parts:
1243 # a 'branch' here is a linear segment of history, with four parts:
1244 # head, root, first parent, second parent
1244 # head, root, first parent, second parent
1245 # (a branch always has two parents (or none) by definition)
1245 # (a branch always has two parents (or none) by definition)
1246 unknown = remote.branches(unknown)
1246 unknown = remote.branches(unknown)
1247 while unknown:
1247 while unknown:
1248 r = []
1248 r = []
1249 while unknown:
1249 while unknown:
1250 n = unknown.pop(0)
1250 n = unknown.pop(0)
1251 if n[0] in seen:
1251 if n[0] in seen:
1252 continue
1252 continue
1253
1253
1254 self.ui.debug(_("examining %s:%s\n")
1254 self.ui.debug(_("examining %s:%s\n")
1255 % (short(n[0]), short(n[1])))
1255 % (short(n[0]), short(n[1])))
1256 if n[0] == nullid: # found the end of the branch
1256 if n[0] == nullid: # found the end of the branch
1257 pass
1257 pass
1258 elif n in seenbranch:
1258 elif n in seenbranch:
1259 self.ui.debug(_("branch already found\n"))
1259 self.ui.debug(_("branch already found\n"))
1260 continue
1260 continue
1261 elif n[1] and n[1] in m: # do we know the base?
1261 elif n[1] and n[1] in m: # do we know the base?
1262 self.ui.debug(_("found incomplete branch %s:%s\n")
1262 self.ui.debug(_("found incomplete branch %s:%s\n")
1263 % (short(n[0]), short(n[1])))
1263 % (short(n[0]), short(n[1])))
1264 search.append(n[0:2]) # schedule branch range for scanning
1264 search.append(n[0:2]) # schedule branch range for scanning
1265 seenbranch.add(n)
1265 seenbranch.add(n)
1266 else:
1266 else:
1267 if n[1] not in seen and n[1] not in fetch:
1267 if n[1] not in seen and n[1] not in fetch:
1268 if n[2] in m and n[3] in m:
1268 if n[2] in m and n[3] in m:
1269 self.ui.debug(_("found new changeset %s\n") %
1269 self.ui.debug(_("found new changeset %s\n") %
1270 short(n[1]))
1270 short(n[1]))
1271 fetch.add(n[1]) # earliest unknown
1271 fetch.add(n[1]) # earliest unknown
1272 for p in n[2:4]:
1272 for p in n[2:4]:
1273 if p in m:
1273 if p in m:
1274 base[p] = 1 # latest known
1274 base[p] = 1 # latest known
1275
1275
1276 for p in n[2:4]:
1276 for p in n[2:4]:
1277 if p not in req and p not in m:
1277 if p not in req and p not in m:
1278 r.append(p)
1278 r.append(p)
1279 req.add(p)
1279 req.add(p)
1280 seen.add(n[0])
1280 seen.add(n[0])
1281
1281
1282 if r:
1282 if r:
1283 reqcnt += 1
1283 reqcnt += 1
1284 self.ui.debug(_("request %d: %s\n") %
1284 self.ui.debug(_("request %d: %s\n") %
1285 (reqcnt, " ".join(map(short, r))))
1285 (reqcnt, " ".join(map(short, r))))
1286 for p in xrange(0, len(r), 10):
1286 for p in xrange(0, len(r), 10):
1287 for b in remote.branches(r[p:p+10]):
1287 for b in remote.branches(r[p:p+10]):
1288 self.ui.debug(_("received %s:%s\n") %
1288 self.ui.debug(_("received %s:%s\n") %
1289 (short(b[0]), short(b[1])))
1289 (short(b[0]), short(b[1])))
1290 unknown.append(b)
1290 unknown.append(b)
1291
1291
1292 # do binary search on the branches we found
1292 # do binary search on the branches we found
1293 while search:
1293 while search:
1294 newsearch = []
1294 newsearch = []
1295 reqcnt += 1
1295 reqcnt += 1
1296 for n, l in zip(search, remote.between(search)):
1296 for n, l in zip(search, remote.between(search)):
1297 l.append(n[1])
1297 l.append(n[1])
1298 p = n[0]
1298 p = n[0]
1299 f = 1
1299 f = 1
1300 for i in l:
1300 for i in l:
1301 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1301 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1302 if i in m:
1302 if i in m:
1303 if f <= 2:
1303 if f <= 2:
1304 self.ui.debug(_("found new branch changeset %s\n") %
1304 self.ui.debug(_("found new branch changeset %s\n") %
1305 short(p))
1305 short(p))
1306 fetch.add(p)
1306 fetch.add(p)
1307 base[i] = 1
1307 base[i] = 1
1308 else:
1308 else:
1309 self.ui.debug(_("narrowed branch search to %s:%s\n")
1309 self.ui.debug(_("narrowed branch search to %s:%s\n")
1310 % (short(p), short(i)))
1310 % (short(p), short(i)))
1311 newsearch.append((p, i))
1311 newsearch.append((p, i))
1312 break
1312 break
1313 p, f = i, f * 2
1313 p, f = i, f * 2
1314 search = newsearch
1314 search = newsearch
1315
1315
1316 # sanity check our fetch list
1316 # sanity check our fetch list
1317 for f in fetch:
1317 for f in fetch:
1318 if f in m:
1318 if f in m:
1319 raise error.RepoError(_("already have changeset ")
1319 raise error.RepoError(_("already have changeset ")
1320 + short(f[:4]))
1320 + short(f[:4]))
1321
1321
1322 if base.keys() == [nullid]:
1322 if base.keys() == [nullid]:
1323 if force:
1323 if force:
1324 self.ui.warn(_("warning: repository is unrelated\n"))
1324 self.ui.warn(_("warning: repository is unrelated\n"))
1325 else:
1325 else:
1326 raise util.Abort(_("repository is unrelated"))
1326 raise util.Abort(_("repository is unrelated"))
1327
1327
1328 self.ui.debug(_("found new changesets starting at ") +
1328 self.ui.debug(_("found new changesets starting at ") +
1329 " ".join([short(f) for f in fetch]) + "\n")
1329 " ".join([short(f) for f in fetch]) + "\n")
1330
1330
1331 self.ui.debug(_("%d total queries\n") % reqcnt)
1331 self.ui.debug(_("%d total queries\n") % reqcnt)
1332
1332
1333 return base.keys(), list(fetch), heads
1333 return base.keys(), list(fetch), heads
1334
1334
1335 def findoutgoing(self, remote, base=None, heads=None, force=False):
1335 def findoutgoing(self, remote, base=None, heads=None, force=False):
1336 """Return list of nodes that are roots of subsets not in remote
1336 """Return list of nodes that are roots of subsets not in remote
1337
1337
1338 If base dict is specified, assume that these nodes and their parents
1338 If base dict is specified, assume that these nodes and their parents
1339 exist on the remote side.
1339 exist on the remote side.
1340 If a list of heads is specified, return only nodes which are heads
1340 If a list of heads is specified, return only nodes which are heads
1341 or ancestors of these heads, and return a second element which
1341 or ancestors of these heads, and return a second element which
1342 contains all remote heads which get new children.
1342 contains all remote heads which get new children.
1343 """
1343 """
1344 if base is None:
1344 if base is None:
1345 base = {}
1345 base = {}
1346 self.findincoming(remote, base, heads, force=force)
1346 self.findincoming(remote, base, heads, force=force)
1347
1347
1348 self.ui.debug(_("common changesets up to ")
1348 self.ui.debug(_("common changesets up to ")
1349 + " ".join(map(short, base.keys())) + "\n")
1349 + " ".join(map(short, base.keys())) + "\n")
1350
1350
1351 remain = set(self.changelog.nodemap)
1351 remain = set(self.changelog.nodemap)
1352
1352
1353 # prune everything remote has from the tree
1353 # prune everything remote has from the tree
1354 remain.remove(nullid)
1354 remain.remove(nullid)
1355 remove = base.keys()
1355 remove = base.keys()
1356 while remove:
1356 while remove:
1357 n = remove.pop(0)
1357 n = remove.pop(0)
1358 if n in remain:
1358 if n in remain:
1359 remain.remove(n)
1359 remain.remove(n)
1360 for p in self.changelog.parents(n):
1360 for p in self.changelog.parents(n):
1361 remove.append(p)
1361 remove.append(p)
1362
1362
1363 # find every node whose parents have been pruned
1363 # find every node whose parents have been pruned
1364 subset = []
1364 subset = []
1365 # find every remote head that will get new children
1365 # find every remote head that will get new children
1366 updated_heads = set()
1366 updated_heads = set()
1367 for n in remain:
1367 for n in remain:
1368 p1, p2 = self.changelog.parents(n)
1368 p1, p2 = self.changelog.parents(n)
1369 if p1 not in remain and p2 not in remain:
1369 if p1 not in remain and p2 not in remain:
1370 subset.append(n)
1370 subset.append(n)
1371 if heads:
1371 if heads:
1372 if p1 in heads:
1372 if p1 in heads:
1373 updated_heads.add(p1)
1373 updated_heads.add(p1)
1374 if p2 in heads:
1374 if p2 in heads:
1375 updated_heads.add(p2)
1375 updated_heads.add(p2)
1376
1376
1377 # this is the set of all roots we have to push
1377 # this is the set of all roots we have to push
1378 if heads:
1378 if heads:
1379 return subset, list(updated_heads)
1379 return subset, list(updated_heads)
1380 else:
1380 else:
1381 return subset
1381 return subset
1382
1382
1383 def pull(self, remote, heads=None, force=False):
1383 def pull(self, remote, heads=None, force=False):
1384 lock = self.lock()
1384 lock = self.lock()
1385 try:
1385 try:
1386 common, fetch, rheads = self.findcommonincoming(remote, heads=heads,
1386 common, fetch, rheads = self.findcommonincoming(remote, heads=heads,
1387 force=force)
1387 force=force)
1388 if fetch == [nullid]:
1388 if fetch == [nullid]:
1389 self.ui.status(_("requesting all changes\n"))
1389 self.ui.status(_("requesting all changes\n"))
1390
1390
1391 if not fetch:
1391 if not fetch:
1392 self.ui.status(_("no changes found\n"))
1392 self.ui.status(_("no changes found\n"))
1393 return 0
1393 return 0
1394
1394
1395 if heads is None and remote.capable('changegroupsubset'):
1395 if heads is None and remote.capable('changegroupsubset'):
1396 heads = rheads
1396 heads = rheads
1397
1397
1398 if heads is None:
1398 if heads is None:
1399 cg = remote.changegroup(fetch, 'pull')
1399 cg = remote.changegroup(fetch, 'pull')
1400 else:
1400 else:
1401 if not remote.capable('changegroupsubset'):
1401 if not remote.capable('changegroupsubset'):
1402 raise util.Abort(_("Partial pull cannot be done because other repository doesn't support changegroupsubset."))
1402 raise util.Abort(_("Partial pull cannot be done because other repository doesn't support changegroupsubset."))
1403 cg = remote.changegroupsubset(fetch, heads, 'pull')
1403 cg = remote.changegroupsubset(fetch, heads, 'pull')
1404 return self.addchangegroup(cg, 'pull', remote.url())
1404 return self.addchangegroup(cg, 'pull', remote.url())
1405 finally:
1405 finally:
1406 lock.release()
1406 lock.release()
1407
1407
1408 def push(self, remote, force=False, revs=None):
1408 def push(self, remote, force=False, revs=None):
1409 # there are two ways to push to remote repo:
1409 # there are two ways to push to remote repo:
1410 #
1410 #
1411 # addchangegroup assumes local user can lock remote
1411 # addchangegroup assumes local user can lock remote
1412 # repo (local filesystem, old ssh servers).
1412 # repo (local filesystem, old ssh servers).
1413 #
1413 #
1414 # unbundle assumes local user cannot lock remote repo (new ssh
1414 # unbundle assumes local user cannot lock remote repo (new ssh
1415 # servers, http servers).
1415 # servers, http servers).
1416
1416
1417 if remote.capable('unbundle'):
1417 if remote.capable('unbundle'):
1418 return self.push_unbundle(remote, force, revs)
1418 return self.push_unbundle(remote, force, revs)
1419 return self.push_addchangegroup(remote, force, revs)
1419 return self.push_addchangegroup(remote, force, revs)
1420
1420
1421 def prepush(self, remote, force, revs):
1421 def prepush(self, remote, force, revs):
1422 common = {}
1422 common = {}
1423 remote_heads = remote.heads()
1423 remote_heads = remote.heads()
1424 inc = self.findincoming(remote, common, remote_heads, force=force)
1424 inc = self.findincoming(remote, common, remote_heads, force=force)
1425
1425
1426 update, updated_heads = self.findoutgoing(remote, common, remote_heads)
1426 update, updated_heads = self.findoutgoing(remote, common, remote_heads)
1427 if revs is not None:
1427 if revs is not None:
1428 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1428 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1429 else:
1429 else:
1430 bases, heads = update, self.changelog.heads()
1430 bases, heads = update, self.changelog.heads()
1431
1431
1432 def checkbranch(lheads, rheads, updatelh):
1432 def checkbranch(lheads, rheads, updatelh):
1433 '''
1433 '''
1434 check whether there are more local heads than remote heads on
1434 check whether there are more local heads than remote heads on
1435 a specific branch.
1435 a specific branch.
1436
1436
1437 lheads: local branch heads
1437 lheads: local branch heads
1438 rheads: remote branch heads
1438 rheads: remote branch heads
1439 updatelh: outgoing local branch heads
1439 updatelh: outgoing local branch heads
1440 '''
1440 '''
1441
1441
1442 warn = 0
1442 warn = 0
1443
1443
1444 if not revs and len(lheads) > len(rheads):
1444 if not revs and len(lheads) > len(rheads):
1445 warn = 1
1445 warn = 1
1446 else:
1446 else:
1447 updatelheads = [self.changelog.heads(x, lheads)
1447 updatelheads = [self.changelog.heads(x, lheads)
1448 for x in updatelh]
1448 for x in updatelh]
1449 newheads = set(sum(updatelheads, [])) & set(lheads)
1449 newheads = set(sum(updatelheads, [])) & set(lheads)
1450
1450
1451 if not newheads:
1451 if not newheads:
1452 return True
1452 return True
1453
1453
1454 for r in rheads:
1454 for r in rheads:
1455 if r in self.changelog.nodemap:
1455 if r in self.changelog.nodemap:
1456 desc = self.changelog.heads(r, heads)
1456 desc = self.changelog.heads(r, heads)
1457 l = [h for h in heads if h in desc]
1457 l = [h for h in heads if h in desc]
1458 if not l:
1458 if not l:
1459 newheads.add(r)
1459 newheads.add(r)
1460 else:
1460 else:
1461 newheads.add(r)
1461 newheads.add(r)
1462 if len(newheads) > len(rheads):
1462 if len(newheads) > len(rheads):
1463 warn = 1
1463 warn = 1
1464
1464
1465 if warn:
1465 if warn:
1466 if not rheads: # new branch requires --force
1466 if not rheads: # new branch requires --force
1467 self.ui.warn(_("abort: push creates new"
1467 self.ui.warn(_("abort: push creates new"
1468 " remote branch '%s'!\n" %
1468 " remote branch '%s'!\n" %
1469 self[updatelh[0]].branch()))
1469 self[updatelh[0]].branch()))
1470 else:
1470 else:
1471 self.ui.warn(_("abort: push creates new remote heads!\n"))
1471 self.ui.warn(_("abort: push creates new remote heads!\n"))
1472
1472
1473 self.ui.status(_("(did you forget to merge?"
1473 self.ui.status(_("(did you forget to merge?"
1474 " use push -f to force)\n"))
1474 " use push -f to force)\n"))
1475 return False
1475 return False
1476 return True
1476 return True
1477
1477
1478 if not bases:
1478 if not bases:
1479 self.ui.status(_("no changes found\n"))
1479 self.ui.status(_("no changes found\n"))
1480 return None, 1
1480 return None, 1
1481 elif not force:
1481 elif not force:
1482 # Check for each named branch if we're creating new remote heads.
1482 # Check for each named branch if we're creating new remote heads.
1483 # To be a remote head after push, node must be either:
1483 # To be a remote head after push, node must be either:
1484 # - unknown locally
1484 # - unknown locally
1485 # - a local outgoing head descended from update
1485 # - a local outgoing head descended from update
1486 # - a remote head that's known locally and not
1486 # - a remote head that's known locally and not
1487 # ancestral to an outgoing head
1487 # ancestral to an outgoing head
1488 #
1488 #
1489 # New named branches cannot be created without --force.
1489 # New named branches cannot be created without --force.
1490
1490
1491 if remote_heads != [nullid]:
1491 if remote_heads != [nullid]:
1492 if remote.capable('branchmap'):
1492 if remote.capable('branchmap'):
1493 localhds = {}
1493 localhds = {}
1494 if not revs:
1494 if not revs:
1495 localhds = self.branchmap()
1495 localhds = self.branchmap()
1496 else:
1496 else:
1497 for n in heads:
1497 for n in heads:
1498 branch = self[n].branch()
1498 branch = self[n].branch()
1499 if branch in localhds:
1499 if branch in localhds:
1500 localhds[branch].append(n)
1500 localhds[branch].append(n)
1501 else:
1501 else:
1502 localhds[branch] = [n]
1502 localhds[branch] = [n]
1503
1503
1504 remotehds = remote.branchmap()
1504 remotehds = remote.branchmap()
1505
1505
1506 for lh in localhds:
1506 for lh in localhds:
1507 if lh in remotehds:
1507 if lh in remotehds:
1508 rheads = remotehds[lh]
1508 rheads = remotehds[lh]
1509 else:
1509 else:
1510 rheads = []
1510 rheads = []
1511 lheads = localhds[lh]
1511 lheads = localhds[lh]
1512 updatelh = [upd for upd in update
1512 updatelh = [upd for upd in update
1513 if self[upd].branch() == lh]
1513 if self[upd].branch() == lh]
1514 if not updatelh:
1514 if not updatelh:
1515 continue
1515 continue
1516 if not checkbranch(lheads, rheads, updatelh):
1516 if not checkbranch(lheads, rheads, updatelh):
1517 return None, 0
1517 return None, 0
1518 else:
1518 else:
1519 if not checkbranch(heads, remote_heads, update):
1519 if not checkbranch(heads, remote_heads, update):
1520 return None, 0
1520 return None, 0
1521
1521
1522 if inc:
1522 if inc:
1523 self.ui.warn(_("note: unsynced remote changes!\n"))
1523 self.ui.warn(_("note: unsynced remote changes!\n"))
1524
1524
1525
1525
1526 if revs is None:
1526 if revs is None:
1527 # use the fast path, no race possible on push
1527 # use the fast path, no race possible on push
1528 cg = self._changegroup(common.keys(), 'push')
1528 cg = self._changegroup(common.keys(), 'push')
1529 else:
1529 else:
1530 cg = self.changegroupsubset(update, revs, 'push')
1530 cg = self.changegroupsubset(update, revs, 'push')
1531 return cg, remote_heads
1531 return cg, remote_heads
1532
1532
1533 def push_addchangegroup(self, remote, force, revs):
1533 def push_addchangegroup(self, remote, force, revs):
1534 lock = remote.lock()
1534 lock = remote.lock()
1535 try:
1535 try:
1536 ret = self.prepush(remote, force, revs)
1536 ret = self.prepush(remote, force, revs)
1537 if ret[0] is not None:
1537 if ret[0] is not None:
1538 cg, remote_heads = ret
1538 cg, remote_heads = ret
1539 return remote.addchangegroup(cg, 'push', self.url())
1539 return remote.addchangegroup(cg, 'push', self.url())
1540 return ret[1]
1540 return ret[1]
1541 finally:
1541 finally:
1542 lock.release()
1542 lock.release()
1543
1543
1544 def push_unbundle(self, remote, force, revs):
1544 def push_unbundle(self, remote, force, revs):
1545 # local repo finds heads on server, finds out what revs it
1545 # local repo finds heads on server, finds out what revs it
1546 # must push. once revs transferred, if server finds it has
1546 # must push. once revs transferred, if server finds it has
1547 # different heads (someone else won commit/push race), server
1547 # different heads (someone else won commit/push race), server
1548 # aborts.
1548 # aborts.
1549
1549
1550 ret = self.prepush(remote, force, revs)
1550 ret = self.prepush(remote, force, revs)
1551 if ret[0] is not None:
1551 if ret[0] is not None:
1552 cg, remote_heads = ret
1552 cg, remote_heads = ret
1553 if force: remote_heads = ['force']
1553 if force: remote_heads = ['force']
1554 return remote.unbundle(cg, remote_heads, 'push')
1554 return remote.unbundle(cg, remote_heads, 'push')
1555 return ret[1]
1555 return ret[1]
1556
1556
1557 def changegroupinfo(self, nodes, source):
1557 def changegroupinfo(self, nodes, source):
1558 if self.ui.verbose or source == 'bundle':
1558 if self.ui.verbose or source == 'bundle':
1559 self.ui.status(_("%d changesets found\n") % len(nodes))
1559 self.ui.status(_("%d changesets found\n") % len(nodes))
1560 if self.ui.debugflag:
1560 if self.ui.debugflag:
1561 self.ui.debug(_("list of changesets:\n"))
1561 self.ui.debug(_("list of changesets:\n"))
1562 for node in nodes:
1562 for node in nodes:
1563 self.ui.debug("%s\n" % hex(node))
1563 self.ui.debug("%s\n" % hex(node))
1564
1564
1565 def changegroupsubset(self, bases, heads, source, extranodes=None):
1565 def changegroupsubset(self, bases, heads, source, extranodes=None):
1566 """This function generates a changegroup consisting of all the nodes
1566 """This function generates a changegroup consisting of all the nodes
1567 that are descendents of any of the bases, and ancestors of any of
1567 that are descendents of any of the bases, and ancestors of any of
1568 the heads.
1568 the heads.
1569
1569
1570 It is fairly complex as determining which filenodes and which
1570 It is fairly complex as determining which filenodes and which
1571 manifest nodes need to be included for the changeset to be complete
1571 manifest nodes need to be included for the changeset to be complete
1572 is non-trivial.
1572 is non-trivial.
1573
1573
1574 Another wrinkle is doing the reverse, figuring out which changeset in
1574 Another wrinkle is doing the reverse, figuring out which changeset in
1575 the changegroup a particular filenode or manifestnode belongs to.
1575 the changegroup a particular filenode or manifestnode belongs to.
1576
1576
1577 The caller can specify some nodes that must be included in the
1577 The caller can specify some nodes that must be included in the
1578 changegroup using the extranodes argument. It should be a dict
1578 changegroup using the extranodes argument. It should be a dict
1579 where the keys are the filenames (or 1 for the manifest), and the
1579 where the keys are the filenames (or 1 for the manifest), and the
1580 values are lists of (node, linknode) tuples, where node is a wanted
1580 values are lists of (node, linknode) tuples, where node is a wanted
1581 node and linknode is the changelog node that should be transmitted as
1581 node and linknode is the changelog node that should be transmitted as
1582 the linkrev.
1582 the linkrev.
1583 """
1583 """
1584
1584
1585 if extranodes is None:
1585 if extranodes is None:
1586 # can we go through the fast path ?
1586 # can we go through the fast path ?
1587 heads.sort()
1587 heads.sort()
1588 allheads = self.heads()
1588 allheads = self.heads()
1589 allheads.sort()
1589 allheads.sort()
1590 if heads == allheads:
1590 if heads == allheads:
1591 common = []
1591 common = []
1592 # parents of bases are known from both sides
1592 # parents of bases are known from both sides
1593 for n in bases:
1593 for n in bases:
1594 for p in self.changelog.parents(n):
1594 for p in self.changelog.parents(n):
1595 if p != nullid:
1595 if p != nullid:
1596 common.append(p)
1596 common.append(p)
1597 return self._changegroup(common, source)
1597 return self._changegroup(common, source)
1598
1598
1599 self.hook('preoutgoing', throw=True, source=source)
1599 self.hook('preoutgoing', throw=True, source=source)
1600
1600
1601 # Set up some initial variables
1601 # Set up some initial variables
1602 # Make it easy to refer to self.changelog
1602 # Make it easy to refer to self.changelog
1603 cl = self.changelog
1603 cl = self.changelog
1604 # msng is short for missing - compute the list of changesets in this
1604 # msng is short for missing - compute the list of changesets in this
1605 # changegroup.
1605 # changegroup.
1606 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1606 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1607 self.changegroupinfo(msng_cl_lst, source)
1607 self.changegroupinfo(msng_cl_lst, source)
1608 # Some bases may turn out to be superfluous, and some heads may be
1608 # Some bases may turn out to be superfluous, and some heads may be
1609 # too. nodesbetween will return the minimal set of bases and heads
1609 # too. nodesbetween will return the minimal set of bases and heads
1610 # necessary to re-create the changegroup.
1610 # necessary to re-create the changegroup.
1611
1611
1612 # Known heads are the list of heads that it is assumed the recipient
1612 # Known heads are the list of heads that it is assumed the recipient
1613 # of this changegroup will know about.
1613 # of this changegroup will know about.
1614 knownheads = set()
1614 knownheads = set()
1615 # We assume that all parents of bases are known heads.
1615 # We assume that all parents of bases are known heads.
1616 for n in bases:
1616 for n in bases:
1617 knownheads.update(cl.parents(n))
1617 knownheads.update(cl.parents(n))
1618 knownheads.discard(nullid)
1618 knownheads.discard(nullid)
1619 knownheads = list(knownheads)
1619 knownheads = list(knownheads)
1620 if knownheads:
1620 if knownheads:
1621 # Now that we know what heads are known, we can compute which
1621 # Now that we know what heads are known, we can compute which
1622 # changesets are known. The recipient must know about all
1622 # changesets are known. The recipient must know about all
1623 # changesets required to reach the known heads from the null
1623 # changesets required to reach the known heads from the null
1624 # changeset.
1624 # changeset.
1625 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1625 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1626 junk = None
1626 junk = None
1627 # Transform the list into a set.
1627 # Transform the list into a set.
1628 has_cl_set = set(has_cl_set)
1628 has_cl_set = set(has_cl_set)
1629 else:
1629 else:
1630 # If there were no known heads, the recipient cannot be assumed to
1630 # If there were no known heads, the recipient cannot be assumed to
1631 # know about any changesets.
1631 # know about any changesets.
1632 has_cl_set = set()
1632 has_cl_set = set()
1633
1633
1634 # Make it easy to refer to self.manifest
1634 # Make it easy to refer to self.manifest
1635 mnfst = self.manifest
1635 mnfst = self.manifest
1636 # We don't know which manifests are missing yet
1636 # We don't know which manifests are missing yet
1637 msng_mnfst_set = {}
1637 msng_mnfst_set = {}
1638 # Nor do we know which filenodes are missing.
1638 # Nor do we know which filenodes are missing.
1639 msng_filenode_set = {}
1639 msng_filenode_set = {}
1640
1640
1641 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1641 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1642 junk = None
1642 junk = None
1643
1643
1644 # A changeset always belongs to itself, so the changenode lookup
1644 # A changeset always belongs to itself, so the changenode lookup
1645 # function for a changenode is identity.
1645 # function for a changenode is identity.
1646 def identity(x):
1646 def identity(x):
1647 return x
1647 return x
1648
1648
1649 # A function generating function. Sets up an environment for the
1649 # A function generating function. Sets up an environment for the
1650 # inner function.
1650 # inner function.
1651 def cmp_by_rev_func(revlog):
1651 def cmp_by_rev_func(revlog):
1652 # Compare two nodes by their revision number in the environment's
1652 # Compare two nodes by their revision number in the environment's
1653 # revision history. Since the revision number both represents the
1653 # revision history. Since the revision number both represents the
1654 # most efficient order to read the nodes in, and represents a
1654 # most efficient order to read the nodes in, and represents a
1655 # topological sorting of the nodes, this function is often useful.
1655 # topological sorting of the nodes, this function is often useful.
1656 def cmp_by_rev(a, b):
1656 def cmp_by_rev(a, b):
1657 return cmp(revlog.rev(a), revlog.rev(b))
1657 return cmp(revlog.rev(a), revlog.rev(b))
1658 return cmp_by_rev
1658 return cmp_by_rev
1659
1659
1660 # If we determine that a particular file or manifest node must be a
1660 # If we determine that a particular file or manifest node must be a
1661 # node that the recipient of the changegroup will already have, we can
1661 # node that the recipient of the changegroup will already have, we can
1662 # also assume the recipient will have all the parents. This function
1662 # also assume the recipient will have all the parents. This function
1663 # prunes them from the set of missing nodes.
1663 # prunes them from the set of missing nodes.
1664 def prune_parents(revlog, hasset, msngset):
1664 def prune_parents(revlog, hasset, msngset):
1665 haslst = list(hasset)
1665 haslst = list(hasset)
1666 haslst.sort(cmp_by_rev_func(revlog))
1666 haslst.sort(cmp_by_rev_func(revlog))
1667 for node in haslst:
1667 for node in haslst:
1668 parentlst = [p for p in revlog.parents(node) if p != nullid]
1668 parentlst = [p for p in revlog.parents(node) if p != nullid]
1669 while parentlst:
1669 while parentlst:
1670 n = parentlst.pop()
1670 n = parentlst.pop()
1671 if n not in hasset:
1671 if n not in hasset:
1672 hasset.add(n)
1672 hasset.add(n)
1673 p = [p for p in revlog.parents(n) if p != nullid]
1673 p = [p for p in revlog.parents(n) if p != nullid]
1674 parentlst.extend(p)
1674 parentlst.extend(p)
1675 for n in hasset:
1675 for n in hasset:
1676 msngset.pop(n, None)
1676 msngset.pop(n, None)
1677
1677
1678 # This is a function generating function used to set up an environment
1678 # This is a function generating function used to set up an environment
1679 # for the inner function to execute in.
1679 # for the inner function to execute in.
1680 def manifest_and_file_collector(changedfileset):
1680 def manifest_and_file_collector(changedfileset):
1681 # This is an information gathering function that gathers
1681 # This is an information gathering function that gathers
1682 # information from each changeset node that goes out as part of
1682 # information from each changeset node that goes out as part of
1683 # the changegroup. The information gathered is a list of which
1683 # the changegroup. The information gathered is a list of which
1684 # manifest nodes are potentially required (the recipient may
1684 # manifest nodes are potentially required (the recipient may
1685 # already have them) and total list of all files which were
1685 # already have them) and total list of all files which were
1686 # changed in any changeset in the changegroup.
1686 # changed in any changeset in the changegroup.
1687 #
1687 #
1688 # We also remember the first changenode we saw any manifest
1688 # We also remember the first changenode we saw any manifest
1689 # referenced by so we can later determine which changenode 'owns'
1689 # referenced by so we can later determine which changenode 'owns'
1690 # the manifest.
1690 # the manifest.
1691 def collect_manifests_and_files(clnode):
1691 def collect_manifests_and_files(clnode):
1692 c = cl.read(clnode)
1692 c = cl.read(clnode)
1693 for f in c[3]:
1693 for f in c[3]:
1694 # This is to make sure we only have one instance of each
1694 # This is to make sure we only have one instance of each
1695 # filename string for each filename.
1695 # filename string for each filename.
1696 changedfileset.setdefault(f, f)
1696 changedfileset.setdefault(f, f)
1697 msng_mnfst_set.setdefault(c[0], clnode)
1697 msng_mnfst_set.setdefault(c[0], clnode)
1698 return collect_manifests_and_files
1698 return collect_manifests_and_files
1699
1699
1700 # Figure out which manifest nodes (of the ones we think might be part
1700 # Figure out which manifest nodes (of the ones we think might be part
1701 # of the changegroup) the recipient must know about and remove them
1701 # of the changegroup) the recipient must know about and remove them
1702 # from the changegroup.
1702 # from the changegroup.
1703 def prune_manifests():
1703 def prune_manifests():
1704 has_mnfst_set = set()
1704 has_mnfst_set = set()
1705 for n in msng_mnfst_set:
1705 for n in msng_mnfst_set:
1706 # If a 'missing' manifest thinks it belongs to a changenode
1706 # If a 'missing' manifest thinks it belongs to a changenode
1707 # the recipient is assumed to have, obviously the recipient
1707 # the recipient is assumed to have, obviously the recipient
1708 # must have that manifest.
1708 # must have that manifest.
1709 linknode = cl.node(mnfst.linkrev(mnfst.rev(n)))
1709 linknode = cl.node(mnfst.linkrev(mnfst.rev(n)))
1710 if linknode in has_cl_set:
1710 if linknode in has_cl_set:
1711 has_mnfst_set.add(n)
1711 has_mnfst_set.add(n)
1712 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1712 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1713
1713
1714 # Use the information collected in collect_manifests_and_files to say
1714 # Use the information collected in collect_manifests_and_files to say
1715 # which changenode any manifestnode belongs to.
1715 # which changenode any manifestnode belongs to.
1716 def lookup_manifest_link(mnfstnode):
1716 def lookup_manifest_link(mnfstnode):
1717 return msng_mnfst_set[mnfstnode]
1717 return msng_mnfst_set[mnfstnode]
1718
1718
1719 # A function generating function that sets up the initial environment
1719 # A function generating function that sets up the initial environment
1720 # the inner function.
1720 # the inner function.
1721 def filenode_collector(changedfiles):
1721 def filenode_collector(changedfiles):
1722 next_rev = [0]
1722 next_rev = [0]
1723 # This gathers information from each manifestnode included in the
1723 # This gathers information from each manifestnode included in the
1724 # changegroup about which filenodes the manifest node references
1724 # changegroup about which filenodes the manifest node references
1725 # so we can include those in the changegroup too.
1725 # so we can include those in the changegroup too.
1726 #
1726 #
1727 # It also remembers which changenode each filenode belongs to. It
1727 # It also remembers which changenode each filenode belongs to. It
1728 # does this by assuming the a filenode belongs to the changenode
1728 # does this by assuming the a filenode belongs to the changenode
1729 # the first manifest that references it belongs to.
1729 # the first manifest that references it belongs to.
1730 def collect_msng_filenodes(mnfstnode):
1730 def collect_msng_filenodes(mnfstnode):
1731 r = mnfst.rev(mnfstnode)
1731 r = mnfst.rev(mnfstnode)
1732 if r == next_rev[0]:
1732 if r == next_rev[0]:
1733 # If the last rev we looked at was the one just previous,
1733 # If the last rev we looked at was the one just previous,
1734 # we only need to see a diff.
1734 # we only need to see a diff.
1735 deltamf = mnfst.readdelta(mnfstnode)
1735 deltamf = mnfst.readdelta(mnfstnode)
1736 # For each line in the delta
1736 # For each line in the delta
1737 for f, fnode in deltamf.iteritems():
1737 for f, fnode in deltamf.iteritems():
1738 f = changedfiles.get(f, None)
1738 f = changedfiles.get(f, None)
1739 # And if the file is in the list of files we care
1739 # And if the file is in the list of files we care
1740 # about.
1740 # about.
1741 if f is not None:
1741 if f is not None:
1742 # Get the changenode this manifest belongs to
1742 # Get the changenode this manifest belongs to
1743 clnode = msng_mnfst_set[mnfstnode]
1743 clnode = msng_mnfst_set[mnfstnode]
1744 # Create the set of filenodes for the file if
1744 # Create the set of filenodes for the file if
1745 # there isn't one already.
1745 # there isn't one already.
1746 ndset = msng_filenode_set.setdefault(f, {})
1746 ndset = msng_filenode_set.setdefault(f, {})
1747 # And set the filenode's changelog node to the
1747 # And set the filenode's changelog node to the
1748 # manifest's if it hasn't been set already.
1748 # manifest's if it hasn't been set already.
1749 ndset.setdefault(fnode, clnode)
1749 ndset.setdefault(fnode, clnode)
1750 else:
1750 else:
1751 # Otherwise we need a full manifest.
1751 # Otherwise we need a full manifest.
1752 m = mnfst.read(mnfstnode)
1752 m = mnfst.read(mnfstnode)
1753 # For every file in we care about.
1753 # For every file in we care about.
1754 for f in changedfiles:
1754 for f in changedfiles:
1755 fnode = m.get(f, None)
1755 fnode = m.get(f, None)
1756 # If it's in the manifest
1756 # If it's in the manifest
1757 if fnode is not None:
1757 if fnode is not None:
1758 # See comments above.
1758 # See comments above.
1759 clnode = msng_mnfst_set[mnfstnode]
1759 clnode = msng_mnfst_set[mnfstnode]
1760 ndset = msng_filenode_set.setdefault(f, {})
1760 ndset = msng_filenode_set.setdefault(f, {})
1761 ndset.setdefault(fnode, clnode)
1761 ndset.setdefault(fnode, clnode)
1762 # Remember the revision we hope to see next.
1762 # Remember the revision we hope to see next.
1763 next_rev[0] = r + 1
1763 next_rev[0] = r + 1
1764 return collect_msng_filenodes
1764 return collect_msng_filenodes
1765
1765
1766 # We have a list of filenodes we think we need for a file, lets remove
1766 # We have a list of filenodes we think we need for a file, lets remove
1767 # all those we know the recipient must have.
1767 # all those we know the recipient must have.
1768 def prune_filenodes(f, filerevlog):
1768 def prune_filenodes(f, filerevlog):
1769 msngset = msng_filenode_set[f]
1769 msngset = msng_filenode_set[f]
1770 hasset = set()
1770 hasset = set()
1771 # If a 'missing' filenode thinks it belongs to a changenode we
1771 # If a 'missing' filenode thinks it belongs to a changenode we
1772 # assume the recipient must have, then the recipient must have
1772 # assume the recipient must have, then the recipient must have
1773 # that filenode.
1773 # that filenode.
1774 for n in msngset:
1774 for n in msngset:
1775 clnode = cl.node(filerevlog.linkrev(filerevlog.rev(n)))
1775 clnode = cl.node(filerevlog.linkrev(filerevlog.rev(n)))
1776 if clnode in has_cl_set:
1776 if clnode in has_cl_set:
1777 hasset.add(n)
1777 hasset.add(n)
1778 prune_parents(filerevlog, hasset, msngset)
1778 prune_parents(filerevlog, hasset, msngset)
1779
1779
1780 # A function generator function that sets up the a context for the
1780 # A function generator function that sets up the a context for the
1781 # inner function.
1781 # inner function.
1782 def lookup_filenode_link_func(fname):
1782 def lookup_filenode_link_func(fname):
1783 msngset = msng_filenode_set[fname]
1783 msngset = msng_filenode_set[fname]
1784 # Lookup the changenode the filenode belongs to.
1784 # Lookup the changenode the filenode belongs to.
1785 def lookup_filenode_link(fnode):
1785 def lookup_filenode_link(fnode):
1786 return msngset[fnode]
1786 return msngset[fnode]
1787 return lookup_filenode_link
1787 return lookup_filenode_link
1788
1788
1789 # Add the nodes that were explicitly requested.
1789 # Add the nodes that were explicitly requested.
1790 def add_extra_nodes(name, nodes):
1790 def add_extra_nodes(name, nodes):
1791 if not extranodes or name not in extranodes:
1791 if not extranodes or name not in extranodes:
1792 return
1792 return
1793
1793
1794 for node, linknode in extranodes[name]:
1794 for node, linknode in extranodes[name]:
1795 if node not in nodes:
1795 if node not in nodes:
1796 nodes[node] = linknode
1796 nodes[node] = linknode
1797
1797
1798 # Now that we have all theses utility functions to help out and
1798 # Now that we have all theses utility functions to help out and
1799 # logically divide up the task, generate the group.
1799 # logically divide up the task, generate the group.
1800 def gengroup():
1800 def gengroup():
1801 # The set of changed files starts empty.
1801 # The set of changed files starts empty.
1802 changedfiles = {}
1802 changedfiles = {}
1803 # Create a changenode group generator that will call our functions
1803 # Create a changenode group generator that will call our functions
1804 # back to lookup the owning changenode and collect information.
1804 # back to lookup the owning changenode and collect information.
1805 group = cl.group(msng_cl_lst, identity,
1805 group = cl.group(msng_cl_lst, identity,
1806 manifest_and_file_collector(changedfiles))
1806 manifest_and_file_collector(changedfiles))
1807 for chnk in group:
1807 for chnk in group:
1808 yield chnk
1808 yield chnk
1809
1809
1810 # The list of manifests has been collected by the generator
1810 # The list of manifests has been collected by the generator
1811 # calling our functions back.
1811 # calling our functions back.
1812 prune_manifests()
1812 prune_manifests()
1813 add_extra_nodes(1, msng_mnfst_set)
1813 add_extra_nodes(1, msng_mnfst_set)
1814 msng_mnfst_lst = msng_mnfst_set.keys()
1814 msng_mnfst_lst = msng_mnfst_set.keys()
1815 # Sort the manifestnodes by revision number.
1815 # Sort the manifestnodes by revision number.
1816 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1816 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1817 # Create a generator for the manifestnodes that calls our lookup
1817 # Create a generator for the manifestnodes that calls our lookup
1818 # and data collection functions back.
1818 # and data collection functions back.
1819 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1819 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1820 filenode_collector(changedfiles))
1820 filenode_collector(changedfiles))
1821 for chnk in group:
1821 for chnk in group:
1822 yield chnk
1822 yield chnk
1823
1823
1824 # These are no longer needed, dereference and toss the memory for
1824 # These are no longer needed, dereference and toss the memory for
1825 # them.
1825 # them.
1826 msng_mnfst_lst = None
1826 msng_mnfst_lst = None
1827 msng_mnfst_set.clear()
1827 msng_mnfst_set.clear()
1828
1828
1829 if extranodes:
1829 if extranodes:
1830 for fname in extranodes:
1830 for fname in extranodes:
1831 if isinstance(fname, int):
1831 if isinstance(fname, int):
1832 continue
1832 continue
1833 msng_filenode_set.setdefault(fname, {})
1833 msng_filenode_set.setdefault(fname, {})
1834 changedfiles[fname] = 1
1834 changedfiles[fname] = 1
1835 # Go through all our files in order sorted by name.
1835 # Go through all our files in order sorted by name.
1836 for fname in sorted(changedfiles):
1836 for fname in sorted(changedfiles):
1837 filerevlog = self.file(fname)
1837 filerevlog = self.file(fname)
1838 if not len(filerevlog):
1838 if not len(filerevlog):
1839 raise util.Abort(_("empty or missing revlog for %s") % fname)
1839 raise util.Abort(_("empty or missing revlog for %s") % fname)
1840 # Toss out the filenodes that the recipient isn't really
1840 # Toss out the filenodes that the recipient isn't really
1841 # missing.
1841 # missing.
1842 if fname in msng_filenode_set:
1842 if fname in msng_filenode_set:
1843 prune_filenodes(fname, filerevlog)
1843 prune_filenodes(fname, filerevlog)
1844 add_extra_nodes(fname, msng_filenode_set[fname])
1844 add_extra_nodes(fname, msng_filenode_set[fname])
1845 msng_filenode_lst = msng_filenode_set[fname].keys()
1845 msng_filenode_lst = msng_filenode_set[fname].keys()
1846 else:
1846 else:
1847 msng_filenode_lst = []
1847 msng_filenode_lst = []
1848 # If any filenodes are left, generate the group for them,
1848 # If any filenodes are left, generate the group for them,
1849 # otherwise don't bother.
1849 # otherwise don't bother.
1850 if len(msng_filenode_lst) > 0:
1850 if len(msng_filenode_lst) > 0:
1851 yield changegroup.chunkheader(len(fname))
1851 yield changegroup.chunkheader(len(fname))
1852 yield fname
1852 yield fname
1853 # Sort the filenodes by their revision #
1853 # Sort the filenodes by their revision #
1854 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1854 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1855 # Create a group generator and only pass in a changenode
1855 # Create a group generator and only pass in a changenode
1856 # lookup function as we need to collect no information
1856 # lookup function as we need to collect no information
1857 # from filenodes.
1857 # from filenodes.
1858 group = filerevlog.group(msng_filenode_lst,
1858 group = filerevlog.group(msng_filenode_lst,
1859 lookup_filenode_link_func(fname))
1859 lookup_filenode_link_func(fname))
1860 for chnk in group:
1860 for chnk in group:
1861 yield chnk
1861 yield chnk
1862 if fname in msng_filenode_set:
1862 if fname in msng_filenode_set:
1863 # Don't need this anymore, toss it to free memory.
1863 # Don't need this anymore, toss it to free memory.
1864 del msng_filenode_set[fname]
1864 del msng_filenode_set[fname]
1865 # Signal that no more groups are left.
1865 # Signal that no more groups are left.
1866 yield changegroup.closechunk()
1866 yield changegroup.closechunk()
1867
1867
1868 if msng_cl_lst:
1868 if msng_cl_lst:
1869 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1869 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1870
1870
1871 return util.chunkbuffer(gengroup())
1871 return util.chunkbuffer(gengroup())
1872
1872
1873 def changegroup(self, basenodes, source):
1873 def changegroup(self, basenodes, source):
1874 # to avoid a race we use changegroupsubset() (issue1320)
1874 # to avoid a race we use changegroupsubset() (issue1320)
1875 return self.changegroupsubset(basenodes, self.heads(), source)
1875 return self.changegroupsubset(basenodes, self.heads(), source)
1876
1876
1877 def _changegroup(self, common, source):
1877 def _changegroup(self, common, source):
1878 """Generate a changegroup of all nodes that we have that a recipient
1878 """Generate a changegroup of all nodes that we have that a recipient
1879 doesn't.
1879 doesn't.
1880
1880
1881 This is much easier than the previous function as we can assume that
1881 This is much easier than the previous function as we can assume that
1882 the recipient has any changenode we aren't sending them.
1882 the recipient has any changenode we aren't sending them.
1883
1883
1884 common is the set of common nodes between remote and self"""
1884 common is the set of common nodes between remote and self"""
1885
1885
1886 self.hook('preoutgoing', throw=True, source=source)
1886 self.hook('preoutgoing', throw=True, source=source)
1887
1887
1888 cl = self.changelog
1888 cl = self.changelog
1889 nodes = cl.findmissing(common)
1889 nodes = cl.findmissing(common)
1890 revset = set([cl.rev(n) for n in nodes])
1890 revset = set([cl.rev(n) for n in nodes])
1891 self.changegroupinfo(nodes, source)
1891 self.changegroupinfo(nodes, source)
1892
1892
1893 def identity(x):
1893 def identity(x):
1894 return x
1894 return x
1895
1895
1896 def gennodelst(log):
1896 def gennodelst(log):
1897 for r in log:
1897 for r in log:
1898 if log.linkrev(r) in revset:
1898 if log.linkrev(r) in revset:
1899 yield log.node(r)
1899 yield log.node(r)
1900
1900
1901 def changed_file_collector(changedfileset):
1901 def changed_file_collector(changedfileset):
1902 def collect_changed_files(clnode):
1902 def collect_changed_files(clnode):
1903 c = cl.read(clnode)
1903 c = cl.read(clnode)
1904 changedfileset.update(c[3])
1904 changedfileset.update(c[3])
1905 return collect_changed_files
1905 return collect_changed_files
1906
1906
1907 def lookuprevlink_func(revlog):
1907 def lookuprevlink_func(revlog):
1908 def lookuprevlink(n):
1908 def lookuprevlink(n):
1909 return cl.node(revlog.linkrev(revlog.rev(n)))
1909 return cl.node(revlog.linkrev(revlog.rev(n)))
1910 return lookuprevlink
1910 return lookuprevlink
1911
1911
1912 def gengroup():
1912 def gengroup():
1913 # construct a list of all changed files
1913 # construct a list of all changed files
1914 changedfiles = set()
1914 changedfiles = set()
1915
1915
1916 for chnk in cl.group(nodes, identity,
1916 for chnk in cl.group(nodes, identity,
1917 changed_file_collector(changedfiles)):
1917 changed_file_collector(changedfiles)):
1918 yield chnk
1918 yield chnk
1919
1919
1920 mnfst = self.manifest
1920 mnfst = self.manifest
1921 nodeiter = gennodelst(mnfst)
1921 nodeiter = gennodelst(mnfst)
1922 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1922 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1923 yield chnk
1923 yield chnk
1924
1924
1925 for fname in sorted(changedfiles):
1925 for fname in sorted(changedfiles):
1926 filerevlog = self.file(fname)
1926 filerevlog = self.file(fname)
1927 if not len(filerevlog):
1927 if not len(filerevlog):
1928 raise util.Abort(_("empty or missing revlog for %s") % fname)
1928 raise util.Abort(_("empty or missing revlog for %s") % fname)
1929 nodeiter = gennodelst(filerevlog)
1929 nodeiter = gennodelst(filerevlog)
1930 nodeiter = list(nodeiter)
1930 nodeiter = list(nodeiter)
1931 if nodeiter:
1931 if nodeiter:
1932 yield changegroup.chunkheader(len(fname))
1932 yield changegroup.chunkheader(len(fname))
1933 yield fname
1933 yield fname
1934 lookup = lookuprevlink_func(filerevlog)
1934 lookup = lookuprevlink_func(filerevlog)
1935 for chnk in filerevlog.group(nodeiter, lookup):
1935 for chnk in filerevlog.group(nodeiter, lookup):
1936 yield chnk
1936 yield chnk
1937
1937
1938 yield changegroup.closechunk()
1938 yield changegroup.closechunk()
1939
1939
1940 if nodes:
1940 if nodes:
1941 self.hook('outgoing', node=hex(nodes[0]), source=source)
1941 self.hook('outgoing', node=hex(nodes[0]), source=source)
1942
1942
1943 return util.chunkbuffer(gengroup())
1943 return util.chunkbuffer(gengroup())
1944
1944
1945 def addchangegroup(self, source, srctype, url, emptyok=False):
1945 def addchangegroup(self, source, srctype, url, emptyok=False):
1946 """add changegroup to repo.
1946 """add changegroup to repo.
1947
1947
1948 return values:
1948 return values:
1949 - nothing changed or no source: 0
1949 - nothing changed or no source: 0
1950 - more heads than before: 1+added heads (2..n)
1950 - more heads than before: 1+added heads (2..n)
1951 - less heads than before: -1-removed heads (-2..-n)
1951 - less heads than before: -1-removed heads (-2..-n)
1952 - number of heads stays the same: 1
1952 - number of heads stays the same: 1
1953 """
1953 """
1954 def csmap(x):
1954 def csmap(x):
1955 self.ui.debug(_("add changeset %s\n") % short(x))
1955 self.ui.debug(_("add changeset %s\n") % short(x))
1956 return len(cl)
1956 return len(cl)
1957
1957
1958 def revmap(x):
1958 def revmap(x):
1959 return cl.rev(x)
1959 return cl.rev(x)
1960
1960
1961 if not source:
1961 if not source:
1962 return 0
1962 return 0
1963
1963
1964 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1964 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1965
1965
1966 changesets = files = revisions = 0
1966 changesets = files = revisions = 0
1967
1967
1968 # write changelog data to temp files so concurrent readers will not see
1968 # write changelog data to temp files so concurrent readers will not see
1969 # inconsistent view
1969 # inconsistent view
1970 cl = self.changelog
1970 cl = self.changelog
1971 cl.delayupdate()
1971 cl.delayupdate()
1972 oldheads = len(cl.heads())
1972 oldheads = len(cl.heads())
1973
1973
1974 tr = self.transaction()
1974 tr = self.transaction()
1975 try:
1975 try:
1976 trp = weakref.proxy(tr)
1976 trp = weakref.proxy(tr)
1977 # pull off the changeset group
1977 # pull off the changeset group
1978 self.ui.status(_("adding changesets\n"))
1978 self.ui.status(_("adding changesets\n"))
1979 clstart = len(cl)
1979 clstart = len(cl)
1980 chunkiter = changegroup.chunkiter(source)
1980 chunkiter = changegroup.chunkiter(source)
1981 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok:
1981 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok:
1982 raise util.Abort(_("received changelog group is empty"))
1982 raise util.Abort(_("received changelog group is empty"))
1983 clend = len(cl)
1983 clend = len(cl)
1984 changesets = clend - clstart
1984 changesets = clend - clstart
1985
1985
1986 # pull off the manifest group
1986 # pull off the manifest group
1987 self.ui.status(_("adding manifests\n"))
1987 self.ui.status(_("adding manifests\n"))
1988 chunkiter = changegroup.chunkiter(source)
1988 chunkiter = changegroup.chunkiter(source)
1989 # no need to check for empty manifest group here:
1989 # no need to check for empty manifest group here:
1990 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1990 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1991 # no new manifest will be created and the manifest group will
1991 # no new manifest will be created and the manifest group will
1992 # be empty during the pull
1992 # be empty during the pull
1993 self.manifest.addgroup(chunkiter, revmap, trp)
1993 self.manifest.addgroup(chunkiter, revmap, trp)
1994
1994
1995 # process the files
1995 # process the files
1996 self.ui.status(_("adding file changes\n"))
1996 self.ui.status(_("adding file changes\n"))
1997 while 1:
1997 while 1:
1998 f = changegroup.getchunk(source)
1998 f = changegroup.getchunk(source)
1999 if not f:
1999 if not f:
2000 break
2000 break
2001 self.ui.debug(_("adding %s revisions\n") % f)
2001 self.ui.debug(_("adding %s revisions\n") % f)
2002 fl = self.file(f)
2002 fl = self.file(f)
2003 o = len(fl)
2003 o = len(fl)
2004 chunkiter = changegroup.chunkiter(source)
2004 chunkiter = changegroup.chunkiter(source)
2005 if fl.addgroup(chunkiter, revmap, trp) is None:
2005 if fl.addgroup(chunkiter, revmap, trp) is None:
2006 raise util.Abort(_("received file revlog group is empty"))
2006 raise util.Abort(_("received file revlog group is empty"))
2007 revisions += len(fl) - o
2007 revisions += len(fl) - o
2008 files += 1
2008 files += 1
2009
2009
2010 newheads = len(cl.heads())
2010 newheads = len(cl.heads())
2011 heads = ""
2011 heads = ""
2012 if oldheads and newheads != oldheads:
2012 if oldheads and newheads != oldheads:
2013 heads = _(" (%+d heads)") % (newheads - oldheads)
2013 heads = _(" (%+d heads)") % (newheads - oldheads)
2014
2014
2015 self.ui.status(_("added %d changesets"
2015 self.ui.status(_("added %d changesets"
2016 " with %d changes to %d files%s\n")
2016 " with %d changes to %d files%s\n")
2017 % (changesets, revisions, files, heads))
2017 % (changesets, revisions, files, heads))
2018
2018
2019 if changesets > 0:
2019 if changesets > 0:
2020 p = lambda: cl.writepending() and self.root or ""
2020 p = lambda: cl.writepending() and self.root or ""
2021 self.hook('pretxnchangegroup', throw=True,
2021 self.hook('pretxnchangegroup', throw=True,
2022 node=hex(cl.node(clstart)), source=srctype,
2022 node=hex(cl.node(clstart)), source=srctype,
2023 url=url, pending=p)
2023 url=url, pending=p)
2024
2024
2025 # make changelog see real files again
2025 # make changelog see real files again
2026 cl.finalize(trp)
2026 cl.finalize(trp)
2027
2027
2028 tr.close()
2028 tr.close()
2029 finally:
2029 finally:
2030 del tr
2030 del tr
2031
2031
2032 if changesets > 0:
2032 if changesets > 0:
2033 # forcefully update the on-disk branch cache
2033 # forcefully update the on-disk branch cache
2034 self.ui.debug(_("updating the branch cache\n"))
2034 self.ui.debug(_("updating the branch cache\n"))
2035 self.branchtags()
2035 self.branchtags()
2036 self.hook("changegroup", node=hex(cl.node(clstart)),
2036 self.hook("changegroup", node=hex(cl.node(clstart)),
2037 source=srctype, url=url)
2037 source=srctype, url=url)
2038
2038
2039 for i in xrange(clstart, clend):
2039 for i in xrange(clstart, clend):
2040 self.hook("incoming", node=hex(cl.node(i)),
2040 self.hook("incoming", node=hex(cl.node(i)),
2041 source=srctype, url=url)
2041 source=srctype, url=url)
2042
2042
2043 # never return 0 here:
2043 # never return 0 here:
2044 if newheads < oldheads:
2044 if newheads < oldheads:
2045 return newheads - oldheads - 1
2045 return newheads - oldheads - 1
2046 else:
2046 else:
2047 return newheads - oldheads + 1
2047 return newheads - oldheads + 1
2048
2048
2049
2049
2050 def stream_in(self, remote):
2050 def stream_in(self, remote):
2051 fp = remote.stream_out()
2051 fp = remote.stream_out()
2052 l = fp.readline()
2052 l = fp.readline()
2053 try:
2053 try:
2054 resp = int(l)
2054 resp = int(l)
2055 except ValueError:
2055 except ValueError:
2056 raise error.ResponseError(
2056 raise error.ResponseError(
2057 _('Unexpected response from remote server:'), l)
2057 _('Unexpected response from remote server:'), l)
2058 if resp == 1:
2058 if resp == 1:
2059 raise util.Abort(_('operation forbidden by server'))
2059 raise util.Abort(_('operation forbidden by server'))
2060 elif resp == 2:
2060 elif resp == 2:
2061 raise util.Abort(_('locking the remote repository failed'))
2061 raise util.Abort(_('locking the remote repository failed'))
2062 elif resp != 0:
2062 elif resp != 0:
2063 raise util.Abort(_('the server sent an unknown error code'))
2063 raise util.Abort(_('the server sent an unknown error code'))
2064 self.ui.status(_('streaming all changes\n'))
2064 self.ui.status(_('streaming all changes\n'))
2065 l = fp.readline()
2065 l = fp.readline()
2066 try:
2066 try:
2067 total_files, total_bytes = map(int, l.split(' ', 1))
2067 total_files, total_bytes = map(int, l.split(' ', 1))
2068 except (ValueError, TypeError):
2068 except (ValueError, TypeError):
2069 raise error.ResponseError(
2069 raise error.ResponseError(
2070 _('Unexpected response from remote server:'), l)
2070 _('Unexpected response from remote server:'), l)
2071 self.ui.status(_('%d files to transfer, %s of data\n') %
2071 self.ui.status(_('%d files to transfer, %s of data\n') %
2072 (total_files, util.bytecount(total_bytes)))
2072 (total_files, util.bytecount(total_bytes)))
2073 start = time.time()
2073 start = time.time()
2074 for i in xrange(total_files):
2074 for i in xrange(total_files):
2075 # XXX doesn't support '\n' or '\r' in filenames
2075 # XXX doesn't support '\n' or '\r' in filenames
2076 l = fp.readline()
2076 l = fp.readline()
2077 try:
2077 try:
2078 name, size = l.split('\0', 1)
2078 name, size = l.split('\0', 1)
2079 size = int(size)
2079 size = int(size)
2080 except (ValueError, TypeError):
2080 except (ValueError, TypeError):
2081 raise error.ResponseError(
2081 raise error.ResponseError(
2082 _('Unexpected response from remote server:'), l)
2082 _('Unexpected response from remote server:'), l)
2083 self.ui.debug(_('adding %s (%s)\n') % (name, util.bytecount(size)))
2083 self.ui.debug(_('adding %s (%s)\n') % (name, util.bytecount(size)))
2084 # for backwards compat, name was partially encoded
2084 # for backwards compat, name was partially encoded
2085 ofp = self.sopener(store.decodedir(name), 'w')
2085 ofp = self.sopener(store.decodedir(name), 'w')
2086 for chunk in util.filechunkiter(fp, limit=size):
2086 for chunk in util.filechunkiter(fp, limit=size):
2087 ofp.write(chunk)
2087 ofp.write(chunk)
2088 ofp.close()
2088 ofp.close()
2089 elapsed = time.time() - start
2089 elapsed = time.time() - start
2090 if elapsed <= 0:
2090 if elapsed <= 0:
2091 elapsed = 0.001
2091 elapsed = 0.001
2092 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2092 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2093 (util.bytecount(total_bytes), elapsed,
2093 (util.bytecount(total_bytes), elapsed,
2094 util.bytecount(total_bytes / elapsed)))
2094 util.bytecount(total_bytes / elapsed)))
2095 self.invalidate()
2095 self.invalidate()
2096 return len(self.heads()) + 1
2096 return len(self.heads()) + 1
2097
2097
2098 def clone(self, remote, heads=[], stream=False):
2098 def clone(self, remote, heads=[], stream=False):
2099 '''clone remote repository.
2099 '''clone remote repository.
2100
2100
2101 keyword arguments:
2101 keyword arguments:
2102 heads: list of revs to clone (forces use of pull)
2102 heads: list of revs to clone (forces use of pull)
2103 stream: use streaming clone if possible'''
2103 stream: use streaming clone if possible'''
2104
2104
2105 # now, all clients that can request uncompressed clones can
2105 # now, all clients that can request uncompressed clones can
2106 # read repo formats supported by all servers that can serve
2106 # read repo formats supported by all servers that can serve
2107 # them.
2107 # them.
2108
2108
2109 # if revlog format changes, client will have to check version
2109 # if revlog format changes, client will have to check version
2110 # and format flags on "stream" capability, and use
2110 # and format flags on "stream" capability, and use
2111 # uncompressed only if compatible.
2111 # uncompressed only if compatible.
2112
2112
2113 if stream and not heads and remote.capable('stream'):
2113 if stream and not heads and remote.capable('stream'):
2114 return self.stream_in(remote)
2114 return self.stream_in(remote)
2115 return self.pull(remote, heads)
2115 return self.pull(remote, heads)
2116
2116
2117 # used to avoid circular references so destructors work
2117 # used to avoid circular references so destructors work
2118 def aftertrans(files):
2118 def aftertrans(files):
2119 renamefiles = [tuple(t) for t in files]
2119 renamefiles = [tuple(t) for t in files]
2120 def a():
2120 def a():
2121 for src, dest in renamefiles:
2121 for src, dest in renamefiles:
2122 util.rename(src, dest)
2122 util.rename(src, dest)
2123 return a
2123 return a
2124
2124
2125 def instance(ui, path, create):
2125 def instance(ui, path, create):
2126 return localrepository(ui, util.drop_scheme('file', path), create)
2126 return localrepository(ui, util.drop_scheme('file', path), create)
2127
2127
2128 def islocal(path):
2128 def islocal(path):
2129 return True
2129 return True
@@ -1,54 +1,54 b''
1 # match.py - file name matching
1 # match.py - file name matching
2 #
2 #
3 # Copyright 2008, 2009 Matt Mackall <mpm@selenic.com> and others
3 # Copyright 2008, 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, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7
7
8 import util
8 import util
9
9
10 class _match(object):
10 class _match(object):
11 def __init__(self, root, cwd, files, mf, ap):
11 def __init__(self, root, cwd, files, mf, ap):
12 self._root = root
12 self._root = root
13 self._cwd = cwd
13 self._cwd = cwd
14 self._files = files
14 self._files = files
15 self._fmap = set(files)
15 self._fmap = set(files)
16 self.matchfn = mf
16 self.matchfn = mf
17 self._anypats = ap
17 self._anypats = ap
18 def __call__(self, fn):
18 def __call__(self, fn):
19 return self.matchfn(fn)
19 return self.matchfn(fn)
20 def __iter__(self):
20 def __iter__(self):
21 for f in self._files:
21 for f in self._files:
22 yield f
22 yield f
23 def bad(self, f, msg):
23 def bad(self, f, msg):
24 return True
24 return True
25 def dir(self, f):
25 def dir(self, f):
26 pass
26 pass
27 def missing(self, f):
27 def missing(self, f):
28 pass
28 pass
29 def exact(self, f):
29 def exact(self, f):
30 return f in self._fmap
30 return f in self._fmap
31 def rel(self, f):
31 def rel(self, f):
32 return util.pathto(self._root, self._cwd, f)
32 return util.pathto(self._root, self._cwd, f)
33 def files(self):
33 def files(self):
34 return self._files
34 return self._files
35 def anypats(self):
35 def anypats(self):
36 return self._anypats
36 return self._anypats
37
37
38 class always(_match):
38 class always(_match):
39 def __init__(self, root, cwd):
39 def __init__(self, root, cwd):
40 _match.__init__(self, root, cwd, [], lambda f: True, False)
40 _match.__init__(self, root, cwd, [], lambda f: True, False)
41
41
42 class never(_match):
42 class never(_match):
43 def __init__(self, root, cwd):
43 def __init__(self, root, cwd):
44 _match.__init__(self, root, cwd, [], lambda f: False, False)
44 _match.__init__(self, root, cwd, [], lambda f: False, False)
45
45
46 class exact(_match):
46 class exact(_match):
47 def __init__(self, root, cwd, files):
47 def __init__(self, root, cwd, files):
48 _match.__init__(self, root, cwd, files, self.exact, False)
48 _match.__init__(self, root, cwd, files, self.exact, False)
49
49
50 class match(_match):
50 class match(_match):
51 def __init__(self, root, cwd, patterns, include, exclude, default):
51 def __init__(self, root, cwd, patterns, include, exclude, default):
52 f, mf, ap = util.matcher(root, cwd, patterns, include, exclude,
52 f, mf, ap = util.matcher(root, cwd, patterns, include, exclude,
53 None, default)
53 default)
54 _match.__init__(self, root, cwd, f, mf, ap)
54 _match.__init__(self, root, cwd, f, mf, ap)
@@ -1,1460 +1,1455 b''
1 # util.py - Mercurial utility functions and platform specfic implementations
1 # util.py - Mercurial utility functions and platform specfic implementations
2 #
2 #
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.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, incorporated herein by reference.
8 # GNU General Public License version 2, incorporated herein by reference.
9
9
10 """Mercurial utility functions and platform specfic implementations.
10 """Mercurial utility functions and platform specfic implementations.
11
11
12 This contains helper routines that are independent of the SCM core and
12 This contains helper routines that are independent of the SCM core and
13 hide platform-specific details from the core.
13 hide platform-specific details from the core.
14 """
14 """
15
15
16 from i18n import _
16 from i18n import _
17 import error, osutil
17 import error, osutil
18 import cStringIO, errno, re, shutil, sys, tempfile, traceback
18 import cStringIO, errno, re, shutil, sys, tempfile, traceback
19 import os, stat, time, calendar, glob, random
19 import os, stat, time, calendar, glob, random
20 import imp
20 import imp
21
21
22 # Python compatibility
22 # Python compatibility
23
23
24 def sha1(s):
24 def sha1(s):
25 return _fastsha1(s)
25 return _fastsha1(s)
26
26
27 def _fastsha1(s):
27 def _fastsha1(s):
28 # This function will import sha1 from hashlib or sha (whichever is
28 # This function will import sha1 from hashlib or sha (whichever is
29 # available) and overwrite itself with it on the first call.
29 # available) and overwrite itself with it on the first call.
30 # Subsequent calls will go directly to the imported function.
30 # Subsequent calls will go directly to the imported function.
31 try:
31 try:
32 from hashlib import sha1 as _sha1
32 from hashlib import sha1 as _sha1
33 except ImportError:
33 except ImportError:
34 from sha import sha as _sha1
34 from sha import sha as _sha1
35 global _fastsha1, sha1
35 global _fastsha1, sha1
36 _fastsha1 = sha1 = _sha1
36 _fastsha1 = sha1 = _sha1
37 return _sha1(s)
37 return _sha1(s)
38
38
39 import subprocess
39 import subprocess
40 closefds = os.name == 'posix'
40 closefds = os.name == 'posix'
41 def popen2(cmd):
41 def popen2(cmd):
42 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
42 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
43 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
43 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
44 return p.stdin, p.stdout
44 return p.stdin, p.stdout
45 def popen3(cmd):
45 def popen3(cmd):
46 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
46 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
47 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
47 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
48 stderr=subprocess.PIPE)
48 stderr=subprocess.PIPE)
49 return p.stdin, p.stdout, p.stderr
49 return p.stdin, p.stdout, p.stderr
50
50
51 def version():
51 def version():
52 """Return version information if available."""
52 """Return version information if available."""
53 try:
53 try:
54 import __version__
54 import __version__
55 return __version__.version
55 return __version__.version
56 except ImportError:
56 except ImportError:
57 return 'unknown'
57 return 'unknown'
58
58
59 # used by parsedate
59 # used by parsedate
60 defaultdateformats = (
60 defaultdateformats = (
61 '%Y-%m-%d %H:%M:%S',
61 '%Y-%m-%d %H:%M:%S',
62 '%Y-%m-%d %I:%M:%S%p',
62 '%Y-%m-%d %I:%M:%S%p',
63 '%Y-%m-%d %H:%M',
63 '%Y-%m-%d %H:%M',
64 '%Y-%m-%d %I:%M%p',
64 '%Y-%m-%d %I:%M%p',
65 '%Y-%m-%d',
65 '%Y-%m-%d',
66 '%m-%d',
66 '%m-%d',
67 '%m/%d',
67 '%m/%d',
68 '%m/%d/%y',
68 '%m/%d/%y',
69 '%m/%d/%Y',
69 '%m/%d/%Y',
70 '%a %b %d %H:%M:%S %Y',
70 '%a %b %d %H:%M:%S %Y',
71 '%a %b %d %I:%M:%S%p %Y',
71 '%a %b %d %I:%M:%S%p %Y',
72 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
72 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
73 '%b %d %H:%M:%S %Y',
73 '%b %d %H:%M:%S %Y',
74 '%b %d %I:%M:%S%p %Y',
74 '%b %d %I:%M:%S%p %Y',
75 '%b %d %H:%M:%S',
75 '%b %d %H:%M:%S',
76 '%b %d %I:%M:%S%p',
76 '%b %d %I:%M:%S%p',
77 '%b %d %H:%M',
77 '%b %d %H:%M',
78 '%b %d %I:%M%p',
78 '%b %d %I:%M%p',
79 '%b %d %Y',
79 '%b %d %Y',
80 '%b %d',
80 '%b %d',
81 '%H:%M:%S',
81 '%H:%M:%S',
82 '%I:%M:%SP',
82 '%I:%M:%SP',
83 '%H:%M',
83 '%H:%M',
84 '%I:%M%p',
84 '%I:%M%p',
85 )
85 )
86
86
87 extendeddateformats = defaultdateformats + (
87 extendeddateformats = defaultdateformats + (
88 "%Y",
88 "%Y",
89 "%Y-%m",
89 "%Y-%m",
90 "%b",
90 "%b",
91 "%b %Y",
91 "%b %Y",
92 )
92 )
93
93
94 def cachefunc(func):
94 def cachefunc(func):
95 '''cache the result of function calls'''
95 '''cache the result of function calls'''
96 # XXX doesn't handle keywords args
96 # XXX doesn't handle keywords args
97 cache = {}
97 cache = {}
98 if func.func_code.co_argcount == 1:
98 if func.func_code.co_argcount == 1:
99 # we gain a small amount of time because
99 # we gain a small amount of time because
100 # we don't need to pack/unpack the list
100 # we don't need to pack/unpack the list
101 def f(arg):
101 def f(arg):
102 if arg not in cache:
102 if arg not in cache:
103 cache[arg] = func(arg)
103 cache[arg] = func(arg)
104 return cache[arg]
104 return cache[arg]
105 else:
105 else:
106 def f(*args):
106 def f(*args):
107 if args not in cache:
107 if args not in cache:
108 cache[args] = func(*args)
108 cache[args] = func(*args)
109 return cache[args]
109 return cache[args]
110
110
111 return f
111 return f
112
112
113 class propertycache(object):
113 class propertycache(object):
114 def __init__(self, func):
114 def __init__(self, func):
115 self.func = func
115 self.func = func
116 self.name = func.__name__
116 self.name = func.__name__
117 def __get__(self, obj, type=None):
117 def __get__(self, obj, type=None):
118 result = self.func(obj)
118 result = self.func(obj)
119 setattr(obj, self.name, result)
119 setattr(obj, self.name, result)
120 return result
120 return result
121
121
122 def pipefilter(s, cmd):
122 def pipefilter(s, cmd):
123 '''filter string S through command CMD, returning its output'''
123 '''filter string S through command CMD, returning its output'''
124 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
124 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
125 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
125 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
126 pout, perr = p.communicate(s)
126 pout, perr = p.communicate(s)
127 return pout
127 return pout
128
128
129 def tempfilter(s, cmd):
129 def tempfilter(s, cmd):
130 '''filter string S through a pair of temporary files with CMD.
130 '''filter string S through a pair of temporary files with CMD.
131 CMD is used as a template to create the real command to be run,
131 CMD is used as a template to create the real command to be run,
132 with the strings INFILE and OUTFILE replaced by the real names of
132 with the strings INFILE and OUTFILE replaced by the real names of
133 the temporary files generated.'''
133 the temporary files generated.'''
134 inname, outname = None, None
134 inname, outname = None, None
135 try:
135 try:
136 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
136 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
137 fp = os.fdopen(infd, 'wb')
137 fp = os.fdopen(infd, 'wb')
138 fp.write(s)
138 fp.write(s)
139 fp.close()
139 fp.close()
140 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
140 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
141 os.close(outfd)
141 os.close(outfd)
142 cmd = cmd.replace('INFILE', inname)
142 cmd = cmd.replace('INFILE', inname)
143 cmd = cmd.replace('OUTFILE', outname)
143 cmd = cmd.replace('OUTFILE', outname)
144 code = os.system(cmd)
144 code = os.system(cmd)
145 if sys.platform == 'OpenVMS' and code & 1:
145 if sys.platform == 'OpenVMS' and code & 1:
146 code = 0
146 code = 0
147 if code: raise Abort(_("command '%s' failed: %s") %
147 if code: raise Abort(_("command '%s' failed: %s") %
148 (cmd, explain_exit(code)))
148 (cmd, explain_exit(code)))
149 return open(outname, 'rb').read()
149 return open(outname, 'rb').read()
150 finally:
150 finally:
151 try:
151 try:
152 if inname: os.unlink(inname)
152 if inname: os.unlink(inname)
153 except: pass
153 except: pass
154 try:
154 try:
155 if outname: os.unlink(outname)
155 if outname: os.unlink(outname)
156 except: pass
156 except: pass
157
157
158 filtertable = {
158 filtertable = {
159 'tempfile:': tempfilter,
159 'tempfile:': tempfilter,
160 'pipe:': pipefilter,
160 'pipe:': pipefilter,
161 }
161 }
162
162
163 def filter(s, cmd):
163 def filter(s, cmd):
164 "filter a string through a command that transforms its input to its output"
164 "filter a string through a command that transforms its input to its output"
165 for name, fn in filtertable.iteritems():
165 for name, fn in filtertable.iteritems():
166 if cmd.startswith(name):
166 if cmd.startswith(name):
167 return fn(s, cmd[len(name):].lstrip())
167 return fn(s, cmd[len(name):].lstrip())
168 return pipefilter(s, cmd)
168 return pipefilter(s, cmd)
169
169
170 def binary(s):
170 def binary(s):
171 """return true if a string is binary data"""
171 """return true if a string is binary data"""
172 return bool(s and '\0' in s)
172 return bool(s and '\0' in s)
173
173
174 def increasingchunks(source, min=1024, max=65536):
174 def increasingchunks(source, min=1024, max=65536):
175 '''return no less than min bytes per chunk while data remains,
175 '''return no less than min bytes per chunk while data remains,
176 doubling min after each chunk until it reaches max'''
176 doubling min after each chunk until it reaches max'''
177 def log2(x):
177 def log2(x):
178 if not x:
178 if not x:
179 return 0
179 return 0
180 i = 0
180 i = 0
181 while x:
181 while x:
182 x >>= 1
182 x >>= 1
183 i += 1
183 i += 1
184 return i - 1
184 return i - 1
185
185
186 buf = []
186 buf = []
187 blen = 0
187 blen = 0
188 for chunk in source:
188 for chunk in source:
189 buf.append(chunk)
189 buf.append(chunk)
190 blen += len(chunk)
190 blen += len(chunk)
191 if blen >= min:
191 if blen >= min:
192 if min < max:
192 if min < max:
193 min = min << 1
193 min = min << 1
194 nmin = 1 << log2(blen)
194 nmin = 1 << log2(blen)
195 if nmin > min:
195 if nmin > min:
196 min = nmin
196 min = nmin
197 if min > max:
197 if min > max:
198 min = max
198 min = max
199 yield ''.join(buf)
199 yield ''.join(buf)
200 blen = 0
200 blen = 0
201 buf = []
201 buf = []
202 if buf:
202 if buf:
203 yield ''.join(buf)
203 yield ''.join(buf)
204
204
205 Abort = error.Abort
205 Abort = error.Abort
206
206
207 def always(fn): return True
207 def always(fn): return True
208 def never(fn): return False
208 def never(fn): return False
209
209
210 def patkind(name, default):
210 def patkind(name, default):
211 """Split a string into an optional pattern kind prefix and the
211 """Split a string into an optional pattern kind prefix and the
212 actual pattern."""
212 actual pattern."""
213 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
213 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
214 if name.startswith(prefix + ':'): return name.split(':', 1)
214 if name.startswith(prefix + ':'): return name.split(':', 1)
215 return default, name
215 return default, name
216
216
217 def globre(pat, head='^', tail='$'):
217 def globre(pat, head='^', tail='$'):
218 "convert a glob pattern into a regexp"
218 "convert a glob pattern into a regexp"
219 i, n = 0, len(pat)
219 i, n = 0, len(pat)
220 res = ''
220 res = ''
221 group = 0
221 group = 0
222 def peek(): return i < n and pat[i]
222 def peek(): return i < n and pat[i]
223 while i < n:
223 while i < n:
224 c = pat[i]
224 c = pat[i]
225 i = i+1
225 i = i+1
226 if c == '*':
226 if c == '*':
227 if peek() == '*':
227 if peek() == '*':
228 i += 1
228 i += 1
229 res += '.*'
229 res += '.*'
230 else:
230 else:
231 res += '[^/]*'
231 res += '[^/]*'
232 elif c == '?':
232 elif c == '?':
233 res += '.'
233 res += '.'
234 elif c == '[':
234 elif c == '[':
235 j = i
235 j = i
236 if j < n and pat[j] in '!]':
236 if j < n and pat[j] in '!]':
237 j += 1
237 j += 1
238 while j < n and pat[j] != ']':
238 while j < n and pat[j] != ']':
239 j += 1
239 j += 1
240 if j >= n:
240 if j >= n:
241 res += '\\['
241 res += '\\['
242 else:
242 else:
243 stuff = pat[i:j].replace('\\','\\\\')
243 stuff = pat[i:j].replace('\\','\\\\')
244 i = j + 1
244 i = j + 1
245 if stuff[0] == '!':
245 if stuff[0] == '!':
246 stuff = '^' + stuff[1:]
246 stuff = '^' + stuff[1:]
247 elif stuff[0] == '^':
247 elif stuff[0] == '^':
248 stuff = '\\' + stuff
248 stuff = '\\' + stuff
249 res = '%s[%s]' % (res, stuff)
249 res = '%s[%s]' % (res, stuff)
250 elif c == '{':
250 elif c == '{':
251 group += 1
251 group += 1
252 res += '(?:'
252 res += '(?:'
253 elif c == '}' and group:
253 elif c == '}' and group:
254 res += ')'
254 res += ')'
255 group -= 1
255 group -= 1
256 elif c == ',' and group:
256 elif c == ',' and group:
257 res += '|'
257 res += '|'
258 elif c == '\\':
258 elif c == '\\':
259 p = peek()
259 p = peek()
260 if p:
260 if p:
261 i += 1
261 i += 1
262 res += re.escape(p)
262 res += re.escape(p)
263 else:
263 else:
264 res += re.escape(c)
264 res += re.escape(c)
265 else:
265 else:
266 res += re.escape(c)
266 res += re.escape(c)
267 return head + res + tail
267 return head + res + tail
268
268
269 _globchars = set('[{*?')
269 _globchars = set('[{*?')
270
270
271 def pathto(root, n1, n2):
271 def pathto(root, n1, n2):
272 '''return the relative path from one place to another.
272 '''return the relative path from one place to another.
273 root should use os.sep to separate directories
273 root should use os.sep to separate directories
274 n1 should use os.sep to separate directories
274 n1 should use os.sep to separate directories
275 n2 should use "/" to separate directories
275 n2 should use "/" to separate directories
276 returns an os.sep-separated path.
276 returns an os.sep-separated path.
277
277
278 If n1 is a relative path, it's assumed it's
278 If n1 is a relative path, it's assumed it's
279 relative to root.
279 relative to root.
280 n2 should always be relative to root.
280 n2 should always be relative to root.
281 '''
281 '''
282 if not n1: return localpath(n2)
282 if not n1: return localpath(n2)
283 if os.path.isabs(n1):
283 if os.path.isabs(n1):
284 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
284 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
285 return os.path.join(root, localpath(n2))
285 return os.path.join(root, localpath(n2))
286 n2 = '/'.join((pconvert(root), n2))
286 n2 = '/'.join((pconvert(root), n2))
287 a, b = splitpath(n1), n2.split('/')
287 a, b = splitpath(n1), n2.split('/')
288 a.reverse()
288 a.reverse()
289 b.reverse()
289 b.reverse()
290 while a and b and a[-1] == b[-1]:
290 while a and b and a[-1] == b[-1]:
291 a.pop()
291 a.pop()
292 b.pop()
292 b.pop()
293 b.reverse()
293 b.reverse()
294 return os.sep.join((['..'] * len(a)) + b) or '.'
294 return os.sep.join((['..'] * len(a)) + b) or '.'
295
295
296 def canonpath(root, cwd, myname):
296 def canonpath(root, cwd, myname):
297 """return the canonical path of myname, given cwd and root"""
297 """return the canonical path of myname, given cwd and root"""
298 if root == os.sep:
298 if root == os.sep:
299 rootsep = os.sep
299 rootsep = os.sep
300 elif endswithsep(root):
300 elif endswithsep(root):
301 rootsep = root
301 rootsep = root
302 else:
302 else:
303 rootsep = root + os.sep
303 rootsep = root + os.sep
304 name = myname
304 name = myname
305 if not os.path.isabs(name):
305 if not os.path.isabs(name):
306 name = os.path.join(root, cwd, name)
306 name = os.path.join(root, cwd, name)
307 name = os.path.normpath(name)
307 name = os.path.normpath(name)
308 audit_path = path_auditor(root)
308 audit_path = path_auditor(root)
309 if name != rootsep and name.startswith(rootsep):
309 if name != rootsep and name.startswith(rootsep):
310 name = name[len(rootsep):]
310 name = name[len(rootsep):]
311 audit_path(name)
311 audit_path(name)
312 return pconvert(name)
312 return pconvert(name)
313 elif name == root:
313 elif name == root:
314 return ''
314 return ''
315 else:
315 else:
316 # Determine whether `name' is in the hierarchy at or beneath `root',
316 # Determine whether `name' is in the hierarchy at or beneath `root',
317 # by iterating name=dirname(name) until that causes no change (can't
317 # by iterating name=dirname(name) until that causes no change (can't
318 # check name == '/', because that doesn't work on windows). For each
318 # check name == '/', because that doesn't work on windows). For each
319 # `name', compare dev/inode numbers. If they match, the list `rel'
319 # `name', compare dev/inode numbers. If they match, the list `rel'
320 # holds the reversed list of components making up the relative file
320 # holds the reversed list of components making up the relative file
321 # name we want.
321 # name we want.
322 root_st = os.stat(root)
322 root_st = os.stat(root)
323 rel = []
323 rel = []
324 while True:
324 while True:
325 try:
325 try:
326 name_st = os.stat(name)
326 name_st = os.stat(name)
327 except OSError:
327 except OSError:
328 break
328 break
329 if samestat(name_st, root_st):
329 if samestat(name_st, root_st):
330 if not rel:
330 if not rel:
331 # name was actually the same as root (maybe a symlink)
331 # name was actually the same as root (maybe a symlink)
332 return ''
332 return ''
333 rel.reverse()
333 rel.reverse()
334 name = os.path.join(*rel)
334 name = os.path.join(*rel)
335 audit_path(name)
335 audit_path(name)
336 return pconvert(name)
336 return pconvert(name)
337 dirname, basename = os.path.split(name)
337 dirname, basename = os.path.split(name)
338 rel.append(basename)
338 rel.append(basename)
339 if dirname == name:
339 if dirname == name:
340 break
340 break
341 name = dirname
341 name = dirname
342
342
343 raise Abort('%s not under root' % myname)
343 raise Abort('%s not under root' % myname)
344
344
345 def matcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None, dflt_pat='glob'):
345 def matcher(canonroot, cwd='', names=[], inc=[], exc=[], dflt_pat='glob'):
346 """build a function to match a set of file patterns
346 """build a function to match a set of file patterns
347
347
348 arguments:
348 arguments:
349 canonroot - the canonical root of the tree you're matching against
349 canonroot - the canonical root of the tree you're matching against
350 cwd - the current working directory, if relevant
350 cwd - the current working directory, if relevant
351 names - patterns to find
351 names - patterns to find
352 inc - patterns to include
352 inc - patterns to include
353 exc - patterns to exclude
353 exc - patterns to exclude
354 dflt_pat - if a pattern in names has no explicit type, assume this one
354 dflt_pat - if a pattern in names has no explicit type, assume this one
355 src - where these patterns came from (e.g. .hgignore)
356
355
357 a pattern is one of:
356 a pattern is one of:
358 'glob:<glob>' - a glob relative to cwd
357 'glob:<glob>' - a glob relative to cwd
359 're:<regexp>' - a regular expression
358 're:<regexp>' - a regular expression
360 'path:<path>' - a path relative to canonroot
359 'path:<path>' - a path relative to canonroot
361 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
360 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
362 'relpath:<path>' - a path relative to cwd
361 'relpath:<path>' - a path relative to cwd
363 'relre:<regexp>' - a regexp that doesn't have to match the start of a name
362 'relre:<regexp>' - a regexp that doesn't have to match the start of a name
364 '<something>' - one of the cases above, selected by the dflt_pat argument
363 '<something>' - one of the cases above, selected by the dflt_pat argument
365
364
366 returns:
365 returns:
367 a 3-tuple containing
366 a 3-tuple containing
368 - list of roots (places where one should start a recursive walk of the fs);
367 - list of roots (places where one should start a recursive walk of the fs);
369 this often matches the explicit non-pattern names passed in, but also
368 this often matches the explicit non-pattern names passed in, but also
370 includes the initial part of glob: patterns that has no glob characters
369 includes the initial part of glob: patterns that has no glob characters
371 - a bool match(filename) function
370 - a bool match(filename) function
372 - a bool indicating if any patterns were passed in
371 - a bool indicating if any patterns were passed in
373 """
372 """
374
373
375 # a common case: no patterns at all
374 # a common case: no patterns at all
376 if not names and not inc and not exc:
375 if not names and not inc and not exc:
377 return [], always, False
376 return [], always, False
378
377
379 def contains_glob(name):
378 def contains_glob(name):
380 for c in name:
379 for c in name:
381 if c in _globchars: return True
380 if c in _globchars: return True
382 return False
381 return False
383
382
384 def regex(kind, name, tail):
383 def regex(kind, name, tail):
385 '''convert a pattern into a regular expression'''
384 '''convert a pattern into a regular expression'''
386 if not name:
385 if not name:
387 return ''
386 return ''
388 if kind == 're':
387 if kind == 're':
389 return name
388 return name
390 elif kind == 'path':
389 elif kind == 'path':
391 return '^' + re.escape(name) + '(?:/|$)'
390 return '^' + re.escape(name) + '(?:/|$)'
392 elif kind == 'relglob':
391 elif kind == 'relglob':
393 return globre(name, '(?:|.*/)', tail)
392 return globre(name, '(?:|.*/)', tail)
394 elif kind == 'relpath':
393 elif kind == 'relpath':
395 return re.escape(name) + '(?:/|$)'
394 return re.escape(name) + '(?:/|$)'
396 elif kind == 'relre':
395 elif kind == 'relre':
397 if name.startswith('^'):
396 if name.startswith('^'):
398 return name
397 return name
399 return '.*' + name
398 return '.*' + name
400 return globre(name, '', tail)
399 return globre(name, '', tail)
401
400
402 def matchfn(pats, tail):
401 def matchfn(pats, tail):
403 """build a matching function from a set of patterns"""
402 """build a matching function from a set of patterns"""
404 if not pats:
403 if not pats:
405 return
404 return
406 try:
405 try:
407 pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats])
406 pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats])
408 if len(pat) > 20000:
407 if len(pat) > 20000:
409 raise OverflowError()
408 raise OverflowError()
410 return re.compile(pat).match
409 return re.compile(pat).match
411 except OverflowError:
410 except OverflowError:
412 # We're using a Python with a tiny regex engine and we
411 # We're using a Python with a tiny regex engine and we
413 # made it explode, so we'll divide the pattern list in two
412 # made it explode, so we'll divide the pattern list in two
414 # until it works
413 # until it works
415 l = len(pats)
414 l = len(pats)
416 if l < 2:
415 if l < 2:
417 raise
416 raise
418 a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail)
417 a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail)
419 return lambda s: a(s) or b(s)
418 return lambda s: a(s) or b(s)
420 except re.error:
419 except re.error:
421 for k, p in pats:
420 for k, p in pats:
422 try:
421 try:
423 re.compile('(?:%s)' % regex(k, p, tail))
422 re.compile('(?:%s)' % regex(k, p, tail))
424 except re.error:
423 except re.error:
425 if src:
424 raise Abort("invalid pattern (%s): %s" % (k, p))
426 raise Abort("%s: invalid pattern (%s): %s" %
427 (src, k, p))
428 else:
429 raise Abort("invalid pattern (%s): %s" % (k, p))
430 raise Abort("invalid pattern")
425 raise Abort("invalid pattern")
431
426
432 def globprefix(pat):
427 def globprefix(pat):
433 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
428 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
434 root = []
429 root = []
435 for p in pat.split('/'):
430 for p in pat.split('/'):
436 if contains_glob(p): break
431 if contains_glob(p): break
437 root.append(p)
432 root.append(p)
438 return '/'.join(root) or '.'
433 return '/'.join(root) or '.'
439
434
440 def normalizepats(names, default):
435 def normalizepats(names, default):
441 pats = []
436 pats = []
442 roots = []
437 roots = []
443 anypats = False
438 anypats = False
444 for kind, name in [patkind(p, default) for p in names]:
439 for kind, name in [patkind(p, default) for p in names]:
445 if kind in ('glob', 'relpath'):
440 if kind in ('glob', 'relpath'):
446 name = canonpath(canonroot, cwd, name)
441 name = canonpath(canonroot, cwd, name)
447 elif kind in ('relglob', 'path'):
442 elif kind in ('relglob', 'path'):
448 name = normpath(name)
443 name = normpath(name)
449
444
450 pats.append((kind, name))
445 pats.append((kind, name))
451
446
452 if kind in ('glob', 're', 'relglob', 'relre'):
447 if kind in ('glob', 're', 'relglob', 'relre'):
453 anypats = True
448 anypats = True
454
449
455 if kind == 'glob':
450 if kind == 'glob':
456 root = globprefix(name)
451 root = globprefix(name)
457 roots.append(root)
452 roots.append(root)
458 elif kind in ('relpath', 'path'):
453 elif kind in ('relpath', 'path'):
459 roots.append(name or '.')
454 roots.append(name or '.')
460 elif kind == 'relglob':
455 elif kind == 'relglob':
461 roots.append('.')
456 roots.append('.')
462 return roots, pats, anypats
457 return roots, pats, anypats
463
458
464 roots, pats, anypats = normalizepats(names, dflt_pat)
459 roots, pats, anypats = normalizepats(names, dflt_pat)
465
460
466 patmatch = matchfn(pats, '$') or always
461 patmatch = matchfn(pats, '$') or always
467 incmatch = always
462 incmatch = always
468 if inc:
463 if inc:
469 dummy, inckinds, dummy = normalizepats(inc, 'glob')
464 dummy, inckinds, dummy = normalizepats(inc, 'glob')
470 incmatch = matchfn(inckinds, '(?:/|$)')
465 incmatch = matchfn(inckinds, '(?:/|$)')
471 excmatch = never
466 excmatch = never
472 if exc:
467 if exc:
473 dummy, exckinds, dummy = normalizepats(exc, 'glob')
468 dummy, exckinds, dummy = normalizepats(exc, 'glob')
474 excmatch = matchfn(exckinds, '(?:/|$)')
469 excmatch = matchfn(exckinds, '(?:/|$)')
475
470
476 if not names and inc and not exc:
471 if not names and inc and not exc:
477 # common case: hgignore patterns
472 # common case: hgignore patterns
478 match = incmatch
473 match = incmatch
479 else:
474 else:
480 match = lambda fn: incmatch(fn) and not excmatch(fn) and patmatch(fn)
475 match = lambda fn: incmatch(fn) and not excmatch(fn) and patmatch(fn)
481
476
482 return (roots, match, (inc or exc or anypats) and True)
477 return (roots, match, (inc or exc or anypats) and True)
483
478
484 _hgexecutable = None
479 _hgexecutable = None
485
480
486 def main_is_frozen():
481 def main_is_frozen():
487 """return True if we are a frozen executable.
482 """return True if we are a frozen executable.
488
483
489 The code supports py2exe (most common, Windows only) and tools/freeze
484 The code supports py2exe (most common, Windows only) and tools/freeze
490 (portable, not much used).
485 (portable, not much used).
491 """
486 """
492 return (hasattr(sys, "frozen") or # new py2exe
487 return (hasattr(sys, "frozen") or # new py2exe
493 hasattr(sys, "importers") or # old py2exe
488 hasattr(sys, "importers") or # old py2exe
494 imp.is_frozen("__main__")) # tools/freeze
489 imp.is_frozen("__main__")) # tools/freeze
495
490
496 def hgexecutable():
491 def hgexecutable():
497 """return location of the 'hg' executable.
492 """return location of the 'hg' executable.
498
493
499 Defaults to $HG or 'hg' in the search path.
494 Defaults to $HG or 'hg' in the search path.
500 """
495 """
501 if _hgexecutable is None:
496 if _hgexecutable is None:
502 hg = os.environ.get('HG')
497 hg = os.environ.get('HG')
503 if hg:
498 if hg:
504 set_hgexecutable(hg)
499 set_hgexecutable(hg)
505 elif main_is_frozen():
500 elif main_is_frozen():
506 set_hgexecutable(sys.executable)
501 set_hgexecutable(sys.executable)
507 else:
502 else:
508 set_hgexecutable(find_exe('hg') or 'hg')
503 set_hgexecutable(find_exe('hg') or 'hg')
509 return _hgexecutable
504 return _hgexecutable
510
505
511 def set_hgexecutable(path):
506 def set_hgexecutable(path):
512 """set location of the 'hg' executable"""
507 """set location of the 'hg' executable"""
513 global _hgexecutable
508 global _hgexecutable
514 _hgexecutable = path
509 _hgexecutable = path
515
510
516 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
511 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
517 '''enhanced shell command execution.
512 '''enhanced shell command execution.
518 run with environment maybe modified, maybe in different dir.
513 run with environment maybe modified, maybe in different dir.
519
514
520 if command fails and onerr is None, return status. if ui object,
515 if command fails and onerr is None, return status. if ui object,
521 print error message and return status, else raise onerr object as
516 print error message and return status, else raise onerr object as
522 exception.'''
517 exception.'''
523 def py2shell(val):
518 def py2shell(val):
524 'convert python object into string that is useful to shell'
519 'convert python object into string that is useful to shell'
525 if val is None or val is False:
520 if val is None or val is False:
526 return '0'
521 return '0'
527 if val is True:
522 if val is True:
528 return '1'
523 return '1'
529 return str(val)
524 return str(val)
530 oldenv = {}
525 oldenv = {}
531 for k in environ:
526 for k in environ:
532 oldenv[k] = os.environ.get(k)
527 oldenv[k] = os.environ.get(k)
533 if cwd is not None:
528 if cwd is not None:
534 oldcwd = os.getcwd()
529 oldcwd = os.getcwd()
535 origcmd = cmd
530 origcmd = cmd
536 if os.name == 'nt':
531 if os.name == 'nt':
537 cmd = '"%s"' % cmd
532 cmd = '"%s"' % cmd
538 try:
533 try:
539 for k, v in environ.iteritems():
534 for k, v in environ.iteritems():
540 os.environ[k] = py2shell(v)
535 os.environ[k] = py2shell(v)
541 os.environ['HG'] = hgexecutable()
536 os.environ['HG'] = hgexecutable()
542 if cwd is not None and oldcwd != cwd:
537 if cwd is not None and oldcwd != cwd:
543 os.chdir(cwd)
538 os.chdir(cwd)
544 rc = os.system(cmd)
539 rc = os.system(cmd)
545 if sys.platform == 'OpenVMS' and rc & 1:
540 if sys.platform == 'OpenVMS' and rc & 1:
546 rc = 0
541 rc = 0
547 if rc and onerr:
542 if rc and onerr:
548 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
543 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
549 explain_exit(rc)[0])
544 explain_exit(rc)[0])
550 if errprefix:
545 if errprefix:
551 errmsg = '%s: %s' % (errprefix, errmsg)
546 errmsg = '%s: %s' % (errprefix, errmsg)
552 try:
547 try:
553 onerr.warn(errmsg + '\n')
548 onerr.warn(errmsg + '\n')
554 except AttributeError:
549 except AttributeError:
555 raise onerr(errmsg)
550 raise onerr(errmsg)
556 return rc
551 return rc
557 finally:
552 finally:
558 for k, v in oldenv.iteritems():
553 for k, v in oldenv.iteritems():
559 if v is None:
554 if v is None:
560 del os.environ[k]
555 del os.environ[k]
561 else:
556 else:
562 os.environ[k] = v
557 os.environ[k] = v
563 if cwd is not None and oldcwd != cwd:
558 if cwd is not None and oldcwd != cwd:
564 os.chdir(oldcwd)
559 os.chdir(oldcwd)
565
560
566 def checksignature(func):
561 def checksignature(func):
567 '''wrap a function with code to check for calling errors'''
562 '''wrap a function with code to check for calling errors'''
568 def check(*args, **kwargs):
563 def check(*args, **kwargs):
569 try:
564 try:
570 return func(*args, **kwargs)
565 return func(*args, **kwargs)
571 except TypeError:
566 except TypeError:
572 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
567 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
573 raise error.SignatureError
568 raise error.SignatureError
574 raise
569 raise
575
570
576 return check
571 return check
577
572
578 # os.path.lexists is not available on python2.3
573 # os.path.lexists is not available on python2.3
579 def lexists(filename):
574 def lexists(filename):
580 "test whether a file with this name exists. does not follow symlinks"
575 "test whether a file with this name exists. does not follow symlinks"
581 try:
576 try:
582 os.lstat(filename)
577 os.lstat(filename)
583 except:
578 except:
584 return False
579 return False
585 return True
580 return True
586
581
587 def rename(src, dst):
582 def rename(src, dst):
588 """forcibly rename a file"""
583 """forcibly rename a file"""
589 try:
584 try:
590 os.rename(src, dst)
585 os.rename(src, dst)
591 except OSError, err: # FIXME: check err (EEXIST ?)
586 except OSError, err: # FIXME: check err (EEXIST ?)
592
587
593 # On windows, rename to existing file is not allowed, so we
588 # On windows, rename to existing file is not allowed, so we
594 # must delete destination first. But if a file is open, unlink
589 # must delete destination first. But if a file is open, unlink
595 # schedules it for delete but does not delete it. Rename
590 # schedules it for delete but does not delete it. Rename
596 # happens immediately even for open files, so we rename
591 # happens immediately even for open files, so we rename
597 # destination to a temporary name, then delete that. Then
592 # destination to a temporary name, then delete that. Then
598 # rename is safe to do.
593 # rename is safe to do.
599 # The temporary name is chosen at random to avoid the situation
594 # The temporary name is chosen at random to avoid the situation
600 # where a file is left lying around from a previous aborted run.
595 # where a file is left lying around from a previous aborted run.
601 # The usual race condition this introduces can't be avoided as
596 # The usual race condition this introduces can't be avoided as
602 # we need the name to rename into, and not the file itself. Due
597 # we need the name to rename into, and not the file itself. Due
603 # to the nature of the operation however, any races will at worst
598 # to the nature of the operation however, any races will at worst
604 # lead to the rename failing and the current operation aborting.
599 # lead to the rename failing and the current operation aborting.
605
600
606 def tempname(prefix):
601 def tempname(prefix):
607 for tries in xrange(10):
602 for tries in xrange(10):
608 temp = '%s-%08x' % (prefix, random.randint(0, 0xffffffff))
603 temp = '%s-%08x' % (prefix, random.randint(0, 0xffffffff))
609 if not os.path.exists(temp):
604 if not os.path.exists(temp):
610 return temp
605 return temp
611 raise IOError, (errno.EEXIST, "No usable temporary filename found")
606 raise IOError, (errno.EEXIST, "No usable temporary filename found")
612
607
613 temp = tempname(dst)
608 temp = tempname(dst)
614 os.rename(dst, temp)
609 os.rename(dst, temp)
615 os.unlink(temp)
610 os.unlink(temp)
616 os.rename(src, dst)
611 os.rename(src, dst)
617
612
618 def unlink(f):
613 def unlink(f):
619 """unlink and remove the directory if it is empty"""
614 """unlink and remove the directory if it is empty"""
620 os.unlink(f)
615 os.unlink(f)
621 # try removing directories that might now be empty
616 # try removing directories that might now be empty
622 try:
617 try:
623 os.removedirs(os.path.dirname(f))
618 os.removedirs(os.path.dirname(f))
624 except OSError:
619 except OSError:
625 pass
620 pass
626
621
627 def copyfile(src, dest):
622 def copyfile(src, dest):
628 "copy a file, preserving mode and atime/mtime"
623 "copy a file, preserving mode and atime/mtime"
629 if os.path.islink(src):
624 if os.path.islink(src):
630 try:
625 try:
631 os.unlink(dest)
626 os.unlink(dest)
632 except:
627 except:
633 pass
628 pass
634 os.symlink(os.readlink(src), dest)
629 os.symlink(os.readlink(src), dest)
635 else:
630 else:
636 try:
631 try:
637 shutil.copyfile(src, dest)
632 shutil.copyfile(src, dest)
638 shutil.copystat(src, dest)
633 shutil.copystat(src, dest)
639 except shutil.Error, inst:
634 except shutil.Error, inst:
640 raise Abort(str(inst))
635 raise Abort(str(inst))
641
636
642 def copyfiles(src, dst, hardlink=None):
637 def copyfiles(src, dst, hardlink=None):
643 """Copy a directory tree using hardlinks if possible"""
638 """Copy a directory tree using hardlinks if possible"""
644
639
645 if hardlink is None:
640 if hardlink is None:
646 hardlink = (os.stat(src).st_dev ==
641 hardlink = (os.stat(src).st_dev ==
647 os.stat(os.path.dirname(dst)).st_dev)
642 os.stat(os.path.dirname(dst)).st_dev)
648
643
649 if os.path.isdir(src):
644 if os.path.isdir(src):
650 os.mkdir(dst)
645 os.mkdir(dst)
651 for name, kind in osutil.listdir(src):
646 for name, kind in osutil.listdir(src):
652 srcname = os.path.join(src, name)
647 srcname = os.path.join(src, name)
653 dstname = os.path.join(dst, name)
648 dstname = os.path.join(dst, name)
654 copyfiles(srcname, dstname, hardlink)
649 copyfiles(srcname, dstname, hardlink)
655 else:
650 else:
656 if hardlink:
651 if hardlink:
657 try:
652 try:
658 os_link(src, dst)
653 os_link(src, dst)
659 except (IOError, OSError):
654 except (IOError, OSError):
660 hardlink = False
655 hardlink = False
661 shutil.copy(src, dst)
656 shutil.copy(src, dst)
662 else:
657 else:
663 shutil.copy(src, dst)
658 shutil.copy(src, dst)
664
659
665 class path_auditor(object):
660 class path_auditor(object):
666 '''ensure that a filesystem path contains no banned components.
661 '''ensure that a filesystem path contains no banned components.
667 the following properties of a path are checked:
662 the following properties of a path are checked:
668
663
669 - under top-level .hg
664 - under top-level .hg
670 - starts at the root of a windows drive
665 - starts at the root of a windows drive
671 - contains ".."
666 - contains ".."
672 - traverses a symlink (e.g. a/symlink_here/b)
667 - traverses a symlink (e.g. a/symlink_here/b)
673 - inside a nested repository'''
668 - inside a nested repository'''
674
669
675 def __init__(self, root):
670 def __init__(self, root):
676 self.audited = set()
671 self.audited = set()
677 self.auditeddir = set()
672 self.auditeddir = set()
678 self.root = root
673 self.root = root
679
674
680 def __call__(self, path):
675 def __call__(self, path):
681 if path in self.audited:
676 if path in self.audited:
682 return
677 return
683 normpath = os.path.normcase(path)
678 normpath = os.path.normcase(path)
684 parts = splitpath(normpath)
679 parts = splitpath(normpath)
685 if (os.path.splitdrive(path)[0]
680 if (os.path.splitdrive(path)[0]
686 or parts[0].lower() in ('.hg', '.hg.', '')
681 or parts[0].lower() in ('.hg', '.hg.', '')
687 or os.pardir in parts):
682 or os.pardir in parts):
688 raise Abort(_("path contains illegal component: %s") % path)
683 raise Abort(_("path contains illegal component: %s") % path)
689 if '.hg' in path.lower():
684 if '.hg' in path.lower():
690 lparts = [p.lower() for p in parts]
685 lparts = [p.lower() for p in parts]
691 for p in '.hg', '.hg.':
686 for p in '.hg', '.hg.':
692 if p in lparts[1:]:
687 if p in lparts[1:]:
693 pos = lparts.index(p)
688 pos = lparts.index(p)
694 base = os.path.join(*parts[:pos])
689 base = os.path.join(*parts[:pos])
695 raise Abort(_('path %r is inside repo %r') % (path, base))
690 raise Abort(_('path %r is inside repo %r') % (path, base))
696 def check(prefix):
691 def check(prefix):
697 curpath = os.path.join(self.root, prefix)
692 curpath = os.path.join(self.root, prefix)
698 try:
693 try:
699 st = os.lstat(curpath)
694 st = os.lstat(curpath)
700 except OSError, err:
695 except OSError, err:
701 # EINVAL can be raised as invalid path syntax under win32.
696 # EINVAL can be raised as invalid path syntax under win32.
702 # They must be ignored for patterns can be checked too.
697 # They must be ignored for patterns can be checked too.
703 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
698 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
704 raise
699 raise
705 else:
700 else:
706 if stat.S_ISLNK(st.st_mode):
701 if stat.S_ISLNK(st.st_mode):
707 raise Abort(_('path %r traverses symbolic link %r') %
702 raise Abort(_('path %r traverses symbolic link %r') %
708 (path, prefix))
703 (path, prefix))
709 elif (stat.S_ISDIR(st.st_mode) and
704 elif (stat.S_ISDIR(st.st_mode) and
710 os.path.isdir(os.path.join(curpath, '.hg'))):
705 os.path.isdir(os.path.join(curpath, '.hg'))):
711 raise Abort(_('path %r is inside repo %r') %
706 raise Abort(_('path %r is inside repo %r') %
712 (path, prefix))
707 (path, prefix))
713 parts.pop()
708 parts.pop()
714 prefixes = []
709 prefixes = []
715 for n in range(len(parts)):
710 for n in range(len(parts)):
716 prefix = os.sep.join(parts)
711 prefix = os.sep.join(parts)
717 if prefix in self.auditeddir:
712 if prefix in self.auditeddir:
718 break
713 break
719 check(prefix)
714 check(prefix)
720 prefixes.append(prefix)
715 prefixes.append(prefix)
721 parts.pop()
716 parts.pop()
722
717
723 self.audited.add(path)
718 self.audited.add(path)
724 # only add prefixes to the cache after checking everything: we don't
719 # only add prefixes to the cache after checking everything: we don't
725 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
720 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
726 self.auditeddir.update(prefixes)
721 self.auditeddir.update(prefixes)
727
722
728 def nlinks(pathname):
723 def nlinks(pathname):
729 """Return number of hardlinks for the given file."""
724 """Return number of hardlinks for the given file."""
730 return os.lstat(pathname).st_nlink
725 return os.lstat(pathname).st_nlink
731
726
732 if hasattr(os, 'link'):
727 if hasattr(os, 'link'):
733 os_link = os.link
728 os_link = os.link
734 else:
729 else:
735 def os_link(src, dst):
730 def os_link(src, dst):
736 raise OSError(0, _("Hardlinks not supported"))
731 raise OSError(0, _("Hardlinks not supported"))
737
732
738 def lookup_reg(key, name=None, scope=None):
733 def lookup_reg(key, name=None, scope=None):
739 return None
734 return None
740
735
741 if os.name == 'nt':
736 if os.name == 'nt':
742 from windows import *
737 from windows import *
743 def expand_glob(pats):
738 def expand_glob(pats):
744 '''On Windows, expand the implicit globs in a list of patterns'''
739 '''On Windows, expand the implicit globs in a list of patterns'''
745 ret = []
740 ret = []
746 for p in pats:
741 for p in pats:
747 kind, name = patkind(p, None)
742 kind, name = patkind(p, None)
748 if kind is None:
743 if kind is None:
749 globbed = glob.glob(name)
744 globbed = glob.glob(name)
750 if globbed:
745 if globbed:
751 ret.extend(globbed)
746 ret.extend(globbed)
752 continue
747 continue
753 # if we couldn't expand the glob, just keep it around
748 # if we couldn't expand the glob, just keep it around
754 ret.append(p)
749 ret.append(p)
755 return ret
750 return ret
756 else:
751 else:
757 from posix import *
752 from posix import *
758
753
759 def makelock(info, pathname):
754 def makelock(info, pathname):
760 try:
755 try:
761 return os.symlink(info, pathname)
756 return os.symlink(info, pathname)
762 except OSError, why:
757 except OSError, why:
763 if why.errno == errno.EEXIST:
758 if why.errno == errno.EEXIST:
764 raise
759 raise
765 except AttributeError: # no symlink in os
760 except AttributeError: # no symlink in os
766 pass
761 pass
767
762
768 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
763 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
769 os.write(ld, info)
764 os.write(ld, info)
770 os.close(ld)
765 os.close(ld)
771
766
772 def readlock(pathname):
767 def readlock(pathname):
773 try:
768 try:
774 return os.readlink(pathname)
769 return os.readlink(pathname)
775 except OSError, why:
770 except OSError, why:
776 if why.errno not in (errno.EINVAL, errno.ENOSYS):
771 if why.errno not in (errno.EINVAL, errno.ENOSYS):
777 raise
772 raise
778 except AttributeError: # no symlink in os
773 except AttributeError: # no symlink in os
779 pass
774 pass
780 return posixfile(pathname).read()
775 return posixfile(pathname).read()
781
776
782 def fstat(fp):
777 def fstat(fp):
783 '''stat file object that may not have fileno method.'''
778 '''stat file object that may not have fileno method.'''
784 try:
779 try:
785 return os.fstat(fp.fileno())
780 return os.fstat(fp.fileno())
786 except AttributeError:
781 except AttributeError:
787 return os.stat(fp.name)
782 return os.stat(fp.name)
788
783
789 # File system features
784 # File system features
790
785
791 def checkcase(path):
786 def checkcase(path):
792 """
787 """
793 Check whether the given path is on a case-sensitive filesystem
788 Check whether the given path is on a case-sensitive filesystem
794
789
795 Requires a path (like /foo/.hg) ending with a foldable final
790 Requires a path (like /foo/.hg) ending with a foldable final
796 directory component.
791 directory component.
797 """
792 """
798 s1 = os.stat(path)
793 s1 = os.stat(path)
799 d, b = os.path.split(path)
794 d, b = os.path.split(path)
800 p2 = os.path.join(d, b.upper())
795 p2 = os.path.join(d, b.upper())
801 if path == p2:
796 if path == p2:
802 p2 = os.path.join(d, b.lower())
797 p2 = os.path.join(d, b.lower())
803 try:
798 try:
804 s2 = os.stat(p2)
799 s2 = os.stat(p2)
805 if s2 == s1:
800 if s2 == s1:
806 return False
801 return False
807 return True
802 return True
808 except:
803 except:
809 return True
804 return True
810
805
811 _fspathcache = {}
806 _fspathcache = {}
812 def fspath(name, root):
807 def fspath(name, root):
813 '''Get name in the case stored in the filesystem
808 '''Get name in the case stored in the filesystem
814
809
815 The name is either relative to root, or it is an absolute path starting
810 The name is either relative to root, or it is an absolute path starting
816 with root. Note that this function is unnecessary, and should not be
811 with root. Note that this function is unnecessary, and should not be
817 called, for case-sensitive filesystems (simply because it's expensive).
812 called, for case-sensitive filesystems (simply because it's expensive).
818 '''
813 '''
819 # If name is absolute, make it relative
814 # If name is absolute, make it relative
820 if name.lower().startswith(root.lower()):
815 if name.lower().startswith(root.lower()):
821 l = len(root)
816 l = len(root)
822 if name[l] == os.sep or name[l] == os.altsep:
817 if name[l] == os.sep or name[l] == os.altsep:
823 l = l + 1
818 l = l + 1
824 name = name[l:]
819 name = name[l:]
825
820
826 if not os.path.exists(os.path.join(root, name)):
821 if not os.path.exists(os.path.join(root, name)):
827 return None
822 return None
828
823
829 seps = os.sep
824 seps = os.sep
830 if os.altsep:
825 if os.altsep:
831 seps = seps + os.altsep
826 seps = seps + os.altsep
832 # Protect backslashes. This gets silly very quickly.
827 # Protect backslashes. This gets silly very quickly.
833 seps.replace('\\','\\\\')
828 seps.replace('\\','\\\\')
834 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
829 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
835 dir = os.path.normcase(os.path.normpath(root))
830 dir = os.path.normcase(os.path.normpath(root))
836 result = []
831 result = []
837 for part, sep in pattern.findall(name):
832 for part, sep in pattern.findall(name):
838 if sep:
833 if sep:
839 result.append(sep)
834 result.append(sep)
840 continue
835 continue
841
836
842 if dir not in _fspathcache:
837 if dir not in _fspathcache:
843 _fspathcache[dir] = os.listdir(dir)
838 _fspathcache[dir] = os.listdir(dir)
844 contents = _fspathcache[dir]
839 contents = _fspathcache[dir]
845
840
846 lpart = part.lower()
841 lpart = part.lower()
847 for n in contents:
842 for n in contents:
848 if n.lower() == lpart:
843 if n.lower() == lpart:
849 result.append(n)
844 result.append(n)
850 break
845 break
851 else:
846 else:
852 # Cannot happen, as the file exists!
847 # Cannot happen, as the file exists!
853 result.append(part)
848 result.append(part)
854 dir = os.path.join(dir, lpart)
849 dir = os.path.join(dir, lpart)
855
850
856 return ''.join(result)
851 return ''.join(result)
857
852
858 def checkexec(path):
853 def checkexec(path):
859 """
854 """
860 Check whether the given path is on a filesystem with UNIX-like exec flags
855 Check whether the given path is on a filesystem with UNIX-like exec flags
861
856
862 Requires a directory (like /foo/.hg)
857 Requires a directory (like /foo/.hg)
863 """
858 """
864
859
865 # VFAT on some Linux versions can flip mode but it doesn't persist
860 # VFAT on some Linux versions can flip mode but it doesn't persist
866 # a FS remount. Frequently we can detect it if files are created
861 # a FS remount. Frequently we can detect it if files are created
867 # with exec bit on.
862 # with exec bit on.
868
863
869 try:
864 try:
870 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
865 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
871 fh, fn = tempfile.mkstemp("", "", path)
866 fh, fn = tempfile.mkstemp("", "", path)
872 try:
867 try:
873 os.close(fh)
868 os.close(fh)
874 m = os.stat(fn).st_mode & 0777
869 m = os.stat(fn).st_mode & 0777
875 new_file_has_exec = m & EXECFLAGS
870 new_file_has_exec = m & EXECFLAGS
876 os.chmod(fn, m ^ EXECFLAGS)
871 os.chmod(fn, m ^ EXECFLAGS)
877 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
872 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
878 finally:
873 finally:
879 os.unlink(fn)
874 os.unlink(fn)
880 except (IOError, OSError):
875 except (IOError, OSError):
881 # we don't care, the user probably won't be able to commit anyway
876 # we don't care, the user probably won't be able to commit anyway
882 return False
877 return False
883 return not (new_file_has_exec or exec_flags_cannot_flip)
878 return not (new_file_has_exec or exec_flags_cannot_flip)
884
879
885 def checklink(path):
880 def checklink(path):
886 """check whether the given path is on a symlink-capable filesystem"""
881 """check whether the given path is on a symlink-capable filesystem"""
887 # mktemp is not racy because symlink creation will fail if the
882 # mktemp is not racy because symlink creation will fail if the
888 # file already exists
883 # file already exists
889 name = tempfile.mktemp(dir=path)
884 name = tempfile.mktemp(dir=path)
890 try:
885 try:
891 os.symlink(".", name)
886 os.symlink(".", name)
892 os.unlink(name)
887 os.unlink(name)
893 return True
888 return True
894 except (OSError, AttributeError):
889 except (OSError, AttributeError):
895 return False
890 return False
896
891
897 def needbinarypatch():
892 def needbinarypatch():
898 """return True if patches should be applied in binary mode by default."""
893 """return True if patches should be applied in binary mode by default."""
899 return os.name == 'nt'
894 return os.name == 'nt'
900
895
901 def endswithsep(path):
896 def endswithsep(path):
902 '''Check path ends with os.sep or os.altsep.'''
897 '''Check path ends with os.sep or os.altsep.'''
903 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
898 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
904
899
905 def splitpath(path):
900 def splitpath(path):
906 '''Split path by os.sep.
901 '''Split path by os.sep.
907 Note that this function does not use os.altsep because this is
902 Note that this function does not use os.altsep because this is
908 an alternative of simple "xxx.split(os.sep)".
903 an alternative of simple "xxx.split(os.sep)".
909 It is recommended to use os.path.normpath() before using this
904 It is recommended to use os.path.normpath() before using this
910 function if need.'''
905 function if need.'''
911 return path.split(os.sep)
906 return path.split(os.sep)
912
907
913 def gui():
908 def gui():
914 '''Are we running in a GUI?'''
909 '''Are we running in a GUI?'''
915 return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY")
910 return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY")
916
911
917 def mktempcopy(name, emptyok=False, createmode=None):
912 def mktempcopy(name, emptyok=False, createmode=None):
918 """Create a temporary file with the same contents from name
913 """Create a temporary file with the same contents from name
919
914
920 The permission bits are copied from the original file.
915 The permission bits are copied from the original file.
921
916
922 If the temporary file is going to be truncated immediately, you
917 If the temporary file is going to be truncated immediately, you
923 can use emptyok=True as an optimization.
918 can use emptyok=True as an optimization.
924
919
925 Returns the name of the temporary file.
920 Returns the name of the temporary file.
926 """
921 """
927 d, fn = os.path.split(name)
922 d, fn = os.path.split(name)
928 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
923 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
929 os.close(fd)
924 os.close(fd)
930 # Temporary files are created with mode 0600, which is usually not
925 # Temporary files are created with mode 0600, which is usually not
931 # what we want. If the original file already exists, just copy
926 # what we want. If the original file already exists, just copy
932 # its mode. Otherwise, manually obey umask.
927 # its mode. Otherwise, manually obey umask.
933 try:
928 try:
934 st_mode = os.lstat(name).st_mode & 0777
929 st_mode = os.lstat(name).st_mode & 0777
935 except OSError, inst:
930 except OSError, inst:
936 if inst.errno != errno.ENOENT:
931 if inst.errno != errno.ENOENT:
937 raise
932 raise
938 st_mode = createmode
933 st_mode = createmode
939 if st_mode is None:
934 if st_mode is None:
940 st_mode = ~umask
935 st_mode = ~umask
941 st_mode &= 0666
936 st_mode &= 0666
942 os.chmod(temp, st_mode)
937 os.chmod(temp, st_mode)
943 if emptyok:
938 if emptyok:
944 return temp
939 return temp
945 try:
940 try:
946 try:
941 try:
947 ifp = posixfile(name, "rb")
942 ifp = posixfile(name, "rb")
948 except IOError, inst:
943 except IOError, inst:
949 if inst.errno == errno.ENOENT:
944 if inst.errno == errno.ENOENT:
950 return temp
945 return temp
951 if not getattr(inst, 'filename', None):
946 if not getattr(inst, 'filename', None):
952 inst.filename = name
947 inst.filename = name
953 raise
948 raise
954 ofp = posixfile(temp, "wb")
949 ofp = posixfile(temp, "wb")
955 for chunk in filechunkiter(ifp):
950 for chunk in filechunkiter(ifp):
956 ofp.write(chunk)
951 ofp.write(chunk)
957 ifp.close()
952 ifp.close()
958 ofp.close()
953 ofp.close()
959 except:
954 except:
960 try: os.unlink(temp)
955 try: os.unlink(temp)
961 except: pass
956 except: pass
962 raise
957 raise
963 return temp
958 return temp
964
959
965 class atomictempfile:
960 class atomictempfile:
966 """file-like object that atomically updates a file
961 """file-like object that atomically updates a file
967
962
968 All writes will be redirected to a temporary copy of the original
963 All writes will be redirected to a temporary copy of the original
969 file. When rename is called, the copy is renamed to the original
964 file. When rename is called, the copy is renamed to the original
970 name, making the changes visible.
965 name, making the changes visible.
971 """
966 """
972 def __init__(self, name, mode, createmode):
967 def __init__(self, name, mode, createmode):
973 self.__name = name
968 self.__name = name
974 self._fp = None
969 self._fp = None
975 self.temp = mktempcopy(name, emptyok=('w' in mode),
970 self.temp = mktempcopy(name, emptyok=('w' in mode),
976 createmode=createmode)
971 createmode=createmode)
977 self._fp = posixfile(self.temp, mode)
972 self._fp = posixfile(self.temp, mode)
978
973
979 def __getattr__(self, name):
974 def __getattr__(self, name):
980 return getattr(self._fp, name)
975 return getattr(self._fp, name)
981
976
982 def rename(self):
977 def rename(self):
983 if not self.closed:
978 if not self.closed:
984 self._fp.close()
979 self._fp.close()
985 rename(self.temp, localpath(self.__name))
980 rename(self.temp, localpath(self.__name))
986
981
987 def __del__(self):
982 def __del__(self):
988 if not self.closed:
983 if not self.closed:
989 try:
984 try:
990 os.unlink(self.temp)
985 os.unlink(self.temp)
991 except: pass
986 except: pass
992 if self._fp:
987 if self._fp:
993 self._fp.close()
988 self._fp.close()
994
989
995 def makedirs(name, mode=None):
990 def makedirs(name, mode=None):
996 """recursive directory creation with parent mode inheritance"""
991 """recursive directory creation with parent mode inheritance"""
997 try:
992 try:
998 os.mkdir(name)
993 os.mkdir(name)
999 if mode is not None:
994 if mode is not None:
1000 os.chmod(name, mode)
995 os.chmod(name, mode)
1001 return
996 return
1002 except OSError, err:
997 except OSError, err:
1003 if err.errno == errno.EEXIST:
998 if err.errno == errno.EEXIST:
1004 return
999 return
1005 if err.errno != errno.ENOENT:
1000 if err.errno != errno.ENOENT:
1006 raise
1001 raise
1007 parent = os.path.abspath(os.path.dirname(name))
1002 parent = os.path.abspath(os.path.dirname(name))
1008 makedirs(parent, mode)
1003 makedirs(parent, mode)
1009 makedirs(name, mode)
1004 makedirs(name, mode)
1010
1005
1011 class opener(object):
1006 class opener(object):
1012 """Open files relative to a base directory
1007 """Open files relative to a base directory
1013
1008
1014 This class is used to hide the details of COW semantics and
1009 This class is used to hide the details of COW semantics and
1015 remote file access from higher level code.
1010 remote file access from higher level code.
1016 """
1011 """
1017 def __init__(self, base, audit=True):
1012 def __init__(self, base, audit=True):
1018 self.base = base
1013 self.base = base
1019 if audit:
1014 if audit:
1020 self.audit_path = path_auditor(base)
1015 self.audit_path = path_auditor(base)
1021 else:
1016 else:
1022 self.audit_path = always
1017 self.audit_path = always
1023 self.createmode = None
1018 self.createmode = None
1024
1019
1025 def __getattr__(self, name):
1020 def __getattr__(self, name):
1026 if name == '_can_symlink':
1021 if name == '_can_symlink':
1027 self._can_symlink = checklink(self.base)
1022 self._can_symlink = checklink(self.base)
1028 return self._can_symlink
1023 return self._can_symlink
1029 raise AttributeError(name)
1024 raise AttributeError(name)
1030
1025
1031 def _fixfilemode(self, name):
1026 def _fixfilemode(self, name):
1032 if self.createmode is None:
1027 if self.createmode is None:
1033 return
1028 return
1034 os.chmod(name, self.createmode & 0666)
1029 os.chmod(name, self.createmode & 0666)
1035
1030
1036 def __call__(self, path, mode="r", text=False, atomictemp=False):
1031 def __call__(self, path, mode="r", text=False, atomictemp=False):
1037 self.audit_path(path)
1032 self.audit_path(path)
1038 f = os.path.join(self.base, path)
1033 f = os.path.join(self.base, path)
1039
1034
1040 if not text and "b" not in mode:
1035 if not text and "b" not in mode:
1041 mode += "b" # for that other OS
1036 mode += "b" # for that other OS
1042
1037
1043 nlink = -1
1038 nlink = -1
1044 if mode not in ("r", "rb"):
1039 if mode not in ("r", "rb"):
1045 try:
1040 try:
1046 nlink = nlinks(f)
1041 nlink = nlinks(f)
1047 except OSError:
1042 except OSError:
1048 nlink = 0
1043 nlink = 0
1049 d = os.path.dirname(f)
1044 d = os.path.dirname(f)
1050 if not os.path.isdir(d):
1045 if not os.path.isdir(d):
1051 makedirs(d, self.createmode)
1046 makedirs(d, self.createmode)
1052 if atomictemp:
1047 if atomictemp:
1053 return atomictempfile(f, mode, self.createmode)
1048 return atomictempfile(f, mode, self.createmode)
1054 if nlink > 1:
1049 if nlink > 1:
1055 rename(mktempcopy(f), f)
1050 rename(mktempcopy(f), f)
1056 fp = posixfile(f, mode)
1051 fp = posixfile(f, mode)
1057 if nlink == 0:
1052 if nlink == 0:
1058 self._fixfilemode(f)
1053 self._fixfilemode(f)
1059 return fp
1054 return fp
1060
1055
1061 def symlink(self, src, dst):
1056 def symlink(self, src, dst):
1062 self.audit_path(dst)
1057 self.audit_path(dst)
1063 linkname = os.path.join(self.base, dst)
1058 linkname = os.path.join(self.base, dst)
1064 try:
1059 try:
1065 os.unlink(linkname)
1060 os.unlink(linkname)
1066 except OSError:
1061 except OSError:
1067 pass
1062 pass
1068
1063
1069 dirname = os.path.dirname(linkname)
1064 dirname = os.path.dirname(linkname)
1070 if not os.path.exists(dirname):
1065 if not os.path.exists(dirname):
1071 makedirs(dirname, self.createmode)
1066 makedirs(dirname, self.createmode)
1072
1067
1073 if self._can_symlink:
1068 if self._can_symlink:
1074 try:
1069 try:
1075 os.symlink(src, linkname)
1070 os.symlink(src, linkname)
1076 except OSError, err:
1071 except OSError, err:
1077 raise OSError(err.errno, _('could not symlink to %r: %s') %
1072 raise OSError(err.errno, _('could not symlink to %r: %s') %
1078 (src, err.strerror), linkname)
1073 (src, err.strerror), linkname)
1079 else:
1074 else:
1080 f = self(dst, "w")
1075 f = self(dst, "w")
1081 f.write(src)
1076 f.write(src)
1082 f.close()
1077 f.close()
1083 self._fixfilemode(dst)
1078 self._fixfilemode(dst)
1084
1079
1085 class chunkbuffer(object):
1080 class chunkbuffer(object):
1086 """Allow arbitrary sized chunks of data to be efficiently read from an
1081 """Allow arbitrary sized chunks of data to be efficiently read from an
1087 iterator over chunks of arbitrary size."""
1082 iterator over chunks of arbitrary size."""
1088
1083
1089 def __init__(self, in_iter):
1084 def __init__(self, in_iter):
1090 """in_iter is the iterator that's iterating over the input chunks.
1085 """in_iter is the iterator that's iterating over the input chunks.
1091 targetsize is how big a buffer to try to maintain."""
1086 targetsize is how big a buffer to try to maintain."""
1092 self.iter = iter(in_iter)
1087 self.iter = iter(in_iter)
1093 self.buf = ''
1088 self.buf = ''
1094 self.targetsize = 2**16
1089 self.targetsize = 2**16
1095
1090
1096 def read(self, l):
1091 def read(self, l):
1097 """Read L bytes of data from the iterator of chunks of data.
1092 """Read L bytes of data from the iterator of chunks of data.
1098 Returns less than L bytes if the iterator runs dry."""
1093 Returns less than L bytes if the iterator runs dry."""
1099 if l > len(self.buf) and self.iter:
1094 if l > len(self.buf) and self.iter:
1100 # Clamp to a multiple of self.targetsize
1095 # Clamp to a multiple of self.targetsize
1101 targetsize = max(l, self.targetsize)
1096 targetsize = max(l, self.targetsize)
1102 collector = cStringIO.StringIO()
1097 collector = cStringIO.StringIO()
1103 collector.write(self.buf)
1098 collector.write(self.buf)
1104 collected = len(self.buf)
1099 collected = len(self.buf)
1105 for chunk in self.iter:
1100 for chunk in self.iter:
1106 collector.write(chunk)
1101 collector.write(chunk)
1107 collected += len(chunk)
1102 collected += len(chunk)
1108 if collected >= targetsize:
1103 if collected >= targetsize:
1109 break
1104 break
1110 if collected < targetsize:
1105 if collected < targetsize:
1111 self.iter = False
1106 self.iter = False
1112 self.buf = collector.getvalue()
1107 self.buf = collector.getvalue()
1113 if len(self.buf) == l:
1108 if len(self.buf) == l:
1114 s, self.buf = str(self.buf), ''
1109 s, self.buf = str(self.buf), ''
1115 else:
1110 else:
1116 s, self.buf = self.buf[:l], buffer(self.buf, l)
1111 s, self.buf = self.buf[:l], buffer(self.buf, l)
1117 return s
1112 return s
1118
1113
1119 def filechunkiter(f, size=65536, limit=None):
1114 def filechunkiter(f, size=65536, limit=None):
1120 """Create a generator that produces the data in the file size
1115 """Create a generator that produces the data in the file size
1121 (default 65536) bytes at a time, up to optional limit (default is
1116 (default 65536) bytes at a time, up to optional limit (default is
1122 to read all data). Chunks may be less than size bytes if the
1117 to read all data). Chunks may be less than size bytes if the
1123 chunk is the last chunk in the file, or the file is a socket or
1118 chunk is the last chunk in the file, or the file is a socket or
1124 some other type of file that sometimes reads less data than is
1119 some other type of file that sometimes reads less data than is
1125 requested."""
1120 requested."""
1126 assert size >= 0
1121 assert size >= 0
1127 assert limit is None or limit >= 0
1122 assert limit is None or limit >= 0
1128 while True:
1123 while True:
1129 if limit is None: nbytes = size
1124 if limit is None: nbytes = size
1130 else: nbytes = min(limit, size)
1125 else: nbytes = min(limit, size)
1131 s = nbytes and f.read(nbytes)
1126 s = nbytes and f.read(nbytes)
1132 if not s: break
1127 if not s: break
1133 if limit: limit -= len(s)
1128 if limit: limit -= len(s)
1134 yield s
1129 yield s
1135
1130
1136 def makedate():
1131 def makedate():
1137 lt = time.localtime()
1132 lt = time.localtime()
1138 if lt[8] == 1 and time.daylight:
1133 if lt[8] == 1 and time.daylight:
1139 tz = time.altzone
1134 tz = time.altzone
1140 else:
1135 else:
1141 tz = time.timezone
1136 tz = time.timezone
1142 return time.mktime(lt), tz
1137 return time.mktime(lt), tz
1143
1138
1144 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1139 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1145 """represent a (unixtime, offset) tuple as a localized time.
1140 """represent a (unixtime, offset) tuple as a localized time.
1146 unixtime is seconds since the epoch, and offset is the time zone's
1141 unixtime is seconds since the epoch, and offset is the time zone's
1147 number of seconds away from UTC. if timezone is false, do not
1142 number of seconds away from UTC. if timezone is false, do not
1148 append time zone to string."""
1143 append time zone to string."""
1149 t, tz = date or makedate()
1144 t, tz = date or makedate()
1150 if "%1" in format or "%2" in format:
1145 if "%1" in format or "%2" in format:
1151 sign = (tz > 0) and "-" or "+"
1146 sign = (tz > 0) and "-" or "+"
1152 minutes = abs(tz) / 60
1147 minutes = abs(tz) / 60
1153 format = format.replace("%1", "%c%02d" % (sign, minutes / 60))
1148 format = format.replace("%1", "%c%02d" % (sign, minutes / 60))
1154 format = format.replace("%2", "%02d" % (minutes % 60))
1149 format = format.replace("%2", "%02d" % (minutes % 60))
1155 s = time.strftime(format, time.gmtime(float(t) - tz))
1150 s = time.strftime(format, time.gmtime(float(t) - tz))
1156 return s
1151 return s
1157
1152
1158 def shortdate(date=None):
1153 def shortdate(date=None):
1159 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1154 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1160 return datestr(date, format='%Y-%m-%d')
1155 return datestr(date, format='%Y-%m-%d')
1161
1156
1162 def strdate(string, format, defaults=[]):
1157 def strdate(string, format, defaults=[]):
1163 """parse a localized time string and return a (unixtime, offset) tuple.
1158 """parse a localized time string and return a (unixtime, offset) tuple.
1164 if the string cannot be parsed, ValueError is raised."""
1159 if the string cannot be parsed, ValueError is raised."""
1165 def timezone(string):
1160 def timezone(string):
1166 tz = string.split()[-1]
1161 tz = string.split()[-1]
1167 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1162 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1168 sign = (tz[0] == "+") and 1 or -1
1163 sign = (tz[0] == "+") and 1 or -1
1169 hours = int(tz[1:3])
1164 hours = int(tz[1:3])
1170 minutes = int(tz[3:5])
1165 minutes = int(tz[3:5])
1171 return -sign * (hours * 60 + minutes) * 60
1166 return -sign * (hours * 60 + minutes) * 60
1172 if tz == "GMT" or tz == "UTC":
1167 if tz == "GMT" or tz == "UTC":
1173 return 0
1168 return 0
1174 return None
1169 return None
1175
1170
1176 # NOTE: unixtime = localunixtime + offset
1171 # NOTE: unixtime = localunixtime + offset
1177 offset, date = timezone(string), string
1172 offset, date = timezone(string), string
1178 if offset != None:
1173 if offset != None:
1179 date = " ".join(string.split()[:-1])
1174 date = " ".join(string.split()[:-1])
1180
1175
1181 # add missing elements from defaults
1176 # add missing elements from defaults
1182 for part in defaults:
1177 for part in defaults:
1183 found = [True for p in part if ("%"+p) in format]
1178 found = [True for p in part if ("%"+p) in format]
1184 if not found:
1179 if not found:
1185 date += "@" + defaults[part]
1180 date += "@" + defaults[part]
1186 format += "@%" + part[0]
1181 format += "@%" + part[0]
1187
1182
1188 timetuple = time.strptime(date, format)
1183 timetuple = time.strptime(date, format)
1189 localunixtime = int(calendar.timegm(timetuple))
1184 localunixtime = int(calendar.timegm(timetuple))
1190 if offset is None:
1185 if offset is None:
1191 # local timezone
1186 # local timezone
1192 unixtime = int(time.mktime(timetuple))
1187 unixtime = int(time.mktime(timetuple))
1193 offset = unixtime - localunixtime
1188 offset = unixtime - localunixtime
1194 else:
1189 else:
1195 unixtime = localunixtime + offset
1190 unixtime = localunixtime + offset
1196 return unixtime, offset
1191 return unixtime, offset
1197
1192
1198 def parsedate(date, formats=None, defaults=None):
1193 def parsedate(date, formats=None, defaults=None):
1199 """parse a localized date/time string and return a (unixtime, offset) tuple.
1194 """parse a localized date/time string and return a (unixtime, offset) tuple.
1200
1195
1201 The date may be a "unixtime offset" string or in one of the specified
1196 The date may be a "unixtime offset" string or in one of the specified
1202 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1197 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1203 """
1198 """
1204 if not date:
1199 if not date:
1205 return 0, 0
1200 return 0, 0
1206 if isinstance(date, tuple) and len(date) == 2:
1201 if isinstance(date, tuple) and len(date) == 2:
1207 return date
1202 return date
1208 if not formats:
1203 if not formats:
1209 formats = defaultdateformats
1204 formats = defaultdateformats
1210 date = date.strip()
1205 date = date.strip()
1211 try:
1206 try:
1212 when, offset = map(int, date.split(' '))
1207 when, offset = map(int, date.split(' '))
1213 except ValueError:
1208 except ValueError:
1214 # fill out defaults
1209 # fill out defaults
1215 if not defaults:
1210 if not defaults:
1216 defaults = {}
1211 defaults = {}
1217 now = makedate()
1212 now = makedate()
1218 for part in "d mb yY HI M S".split():
1213 for part in "d mb yY HI M S".split():
1219 if part not in defaults:
1214 if part not in defaults:
1220 if part[0] in "HMS":
1215 if part[0] in "HMS":
1221 defaults[part] = "00"
1216 defaults[part] = "00"
1222 else:
1217 else:
1223 defaults[part] = datestr(now, "%" + part[0])
1218 defaults[part] = datestr(now, "%" + part[0])
1224
1219
1225 for format in formats:
1220 for format in formats:
1226 try:
1221 try:
1227 when, offset = strdate(date, format, defaults)
1222 when, offset = strdate(date, format, defaults)
1228 except (ValueError, OverflowError):
1223 except (ValueError, OverflowError):
1229 pass
1224 pass
1230 else:
1225 else:
1231 break
1226 break
1232 else:
1227 else:
1233 raise Abort(_('invalid date: %r ') % date)
1228 raise Abort(_('invalid date: %r ') % date)
1234 # validate explicit (probably user-specified) date and
1229 # validate explicit (probably user-specified) date and
1235 # time zone offset. values must fit in signed 32 bits for
1230 # time zone offset. values must fit in signed 32 bits for
1236 # current 32-bit linux runtimes. timezones go from UTC-12
1231 # current 32-bit linux runtimes. timezones go from UTC-12
1237 # to UTC+14
1232 # to UTC+14
1238 if abs(when) > 0x7fffffff:
1233 if abs(when) > 0x7fffffff:
1239 raise Abort(_('date exceeds 32 bits: %d') % when)
1234 raise Abort(_('date exceeds 32 bits: %d') % when)
1240 if offset < -50400 or offset > 43200:
1235 if offset < -50400 or offset > 43200:
1241 raise Abort(_('impossible time zone offset: %d') % offset)
1236 raise Abort(_('impossible time zone offset: %d') % offset)
1242 return when, offset
1237 return when, offset
1243
1238
1244 def matchdate(date):
1239 def matchdate(date):
1245 """Return a function that matches a given date match specifier
1240 """Return a function that matches a given date match specifier
1246
1241
1247 Formats include:
1242 Formats include:
1248
1243
1249 '{date}' match a given date to the accuracy provided
1244 '{date}' match a given date to the accuracy provided
1250
1245
1251 '<{date}' on or before a given date
1246 '<{date}' on or before a given date
1252
1247
1253 '>{date}' on or after a given date
1248 '>{date}' on or after a given date
1254
1249
1255 """
1250 """
1256
1251
1257 def lower(date):
1252 def lower(date):
1258 d = dict(mb="1", d="1")
1253 d = dict(mb="1", d="1")
1259 return parsedate(date, extendeddateformats, d)[0]
1254 return parsedate(date, extendeddateformats, d)[0]
1260
1255
1261 def upper(date):
1256 def upper(date):
1262 d = dict(mb="12", HI="23", M="59", S="59")
1257 d = dict(mb="12", HI="23", M="59", S="59")
1263 for days in "31 30 29".split():
1258 for days in "31 30 29".split():
1264 try:
1259 try:
1265 d["d"] = days
1260 d["d"] = days
1266 return parsedate(date, extendeddateformats, d)[0]
1261 return parsedate(date, extendeddateformats, d)[0]
1267 except:
1262 except:
1268 pass
1263 pass
1269 d["d"] = "28"
1264 d["d"] = "28"
1270 return parsedate(date, extendeddateformats, d)[0]
1265 return parsedate(date, extendeddateformats, d)[0]
1271
1266
1272 date = date.strip()
1267 date = date.strip()
1273 if date[0] == "<":
1268 if date[0] == "<":
1274 when = upper(date[1:])
1269 when = upper(date[1:])
1275 return lambda x: x <= when
1270 return lambda x: x <= when
1276 elif date[0] == ">":
1271 elif date[0] == ">":
1277 when = lower(date[1:])
1272 when = lower(date[1:])
1278 return lambda x: x >= when
1273 return lambda x: x >= when
1279 elif date[0] == "-":
1274 elif date[0] == "-":
1280 try:
1275 try:
1281 days = int(date[1:])
1276 days = int(date[1:])
1282 except ValueError:
1277 except ValueError:
1283 raise Abort(_("invalid day spec: %s") % date[1:])
1278 raise Abort(_("invalid day spec: %s") % date[1:])
1284 when = makedate()[0] - days * 3600 * 24
1279 when = makedate()[0] - days * 3600 * 24
1285 return lambda x: x >= when
1280 return lambda x: x >= when
1286 elif " to " in date:
1281 elif " to " in date:
1287 a, b = date.split(" to ")
1282 a, b = date.split(" to ")
1288 start, stop = lower(a), upper(b)
1283 start, stop = lower(a), upper(b)
1289 return lambda x: x >= start and x <= stop
1284 return lambda x: x >= start and x <= stop
1290 else:
1285 else:
1291 start, stop = lower(date), upper(date)
1286 start, stop = lower(date), upper(date)
1292 return lambda x: x >= start and x <= stop
1287 return lambda x: x >= start and x <= stop
1293
1288
1294 def shortuser(user):
1289 def shortuser(user):
1295 """Return a short representation of a user name or email address."""
1290 """Return a short representation of a user name or email address."""
1296 f = user.find('@')
1291 f = user.find('@')
1297 if f >= 0:
1292 if f >= 0:
1298 user = user[:f]
1293 user = user[:f]
1299 f = user.find('<')
1294 f = user.find('<')
1300 if f >= 0:
1295 if f >= 0:
1301 user = user[f+1:]
1296 user = user[f+1:]
1302 f = user.find(' ')
1297 f = user.find(' ')
1303 if f >= 0:
1298 if f >= 0:
1304 user = user[:f]
1299 user = user[:f]
1305 f = user.find('.')
1300 f = user.find('.')
1306 if f >= 0:
1301 if f >= 0:
1307 user = user[:f]
1302 user = user[:f]
1308 return user
1303 return user
1309
1304
1310 def email(author):
1305 def email(author):
1311 '''get email of author.'''
1306 '''get email of author.'''
1312 r = author.find('>')
1307 r = author.find('>')
1313 if r == -1: r = None
1308 if r == -1: r = None
1314 return author[author.find('<')+1:r]
1309 return author[author.find('<')+1:r]
1315
1310
1316 def ellipsis(text, maxlength=400):
1311 def ellipsis(text, maxlength=400):
1317 """Trim string to at most maxlength (default: 400) characters."""
1312 """Trim string to at most maxlength (default: 400) characters."""
1318 if len(text) <= maxlength:
1313 if len(text) <= maxlength:
1319 return text
1314 return text
1320 else:
1315 else:
1321 return "%s..." % (text[:maxlength-3])
1316 return "%s..." % (text[:maxlength-3])
1322
1317
1323 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1318 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1324 '''yield every hg repository under path, recursively.'''
1319 '''yield every hg repository under path, recursively.'''
1325 def errhandler(err):
1320 def errhandler(err):
1326 if err.filename == path:
1321 if err.filename == path:
1327 raise err
1322 raise err
1328 if followsym and hasattr(os.path, 'samestat'):
1323 if followsym and hasattr(os.path, 'samestat'):
1329 def _add_dir_if_not_there(dirlst, dirname):
1324 def _add_dir_if_not_there(dirlst, dirname):
1330 match = False
1325 match = False
1331 samestat = os.path.samestat
1326 samestat = os.path.samestat
1332 dirstat = os.stat(dirname)
1327 dirstat = os.stat(dirname)
1333 for lstdirstat in dirlst:
1328 for lstdirstat in dirlst:
1334 if samestat(dirstat, lstdirstat):
1329 if samestat(dirstat, lstdirstat):
1335 match = True
1330 match = True
1336 break
1331 break
1337 if not match:
1332 if not match:
1338 dirlst.append(dirstat)
1333 dirlst.append(dirstat)
1339 return not match
1334 return not match
1340 else:
1335 else:
1341 followsym = False
1336 followsym = False
1342
1337
1343 if (seen_dirs is None) and followsym:
1338 if (seen_dirs is None) and followsym:
1344 seen_dirs = []
1339 seen_dirs = []
1345 _add_dir_if_not_there(seen_dirs, path)
1340 _add_dir_if_not_there(seen_dirs, path)
1346 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
1341 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
1347 if '.hg' in dirs:
1342 if '.hg' in dirs:
1348 yield root # found a repository
1343 yield root # found a repository
1349 qroot = os.path.join(root, '.hg', 'patches')
1344 qroot = os.path.join(root, '.hg', 'patches')
1350 if os.path.isdir(os.path.join(qroot, '.hg')):
1345 if os.path.isdir(os.path.join(qroot, '.hg')):
1351 yield qroot # we have a patch queue repo here
1346 yield qroot # we have a patch queue repo here
1352 if recurse:
1347 if recurse:
1353 # avoid recursing inside the .hg directory
1348 # avoid recursing inside the .hg directory
1354 dirs.remove('.hg')
1349 dirs.remove('.hg')
1355 else:
1350 else:
1356 dirs[:] = [] # don't descend further
1351 dirs[:] = [] # don't descend further
1357 elif followsym:
1352 elif followsym:
1358 newdirs = []
1353 newdirs = []
1359 for d in dirs:
1354 for d in dirs:
1360 fname = os.path.join(root, d)
1355 fname = os.path.join(root, d)
1361 if _add_dir_if_not_there(seen_dirs, fname):
1356 if _add_dir_if_not_there(seen_dirs, fname):
1362 if os.path.islink(fname):
1357 if os.path.islink(fname):
1363 for hgname in walkrepos(fname, True, seen_dirs):
1358 for hgname in walkrepos(fname, True, seen_dirs):
1364 yield hgname
1359 yield hgname
1365 else:
1360 else:
1366 newdirs.append(d)
1361 newdirs.append(d)
1367 dirs[:] = newdirs
1362 dirs[:] = newdirs
1368
1363
1369 _rcpath = None
1364 _rcpath = None
1370
1365
1371 def os_rcpath():
1366 def os_rcpath():
1372 '''return default os-specific hgrc search path'''
1367 '''return default os-specific hgrc search path'''
1373 path = system_rcpath()
1368 path = system_rcpath()
1374 path.extend(user_rcpath())
1369 path.extend(user_rcpath())
1375 path = [os.path.normpath(f) for f in path]
1370 path = [os.path.normpath(f) for f in path]
1376 return path
1371 return path
1377
1372
1378 def rcpath():
1373 def rcpath():
1379 '''return hgrc search path. if env var HGRCPATH is set, use it.
1374 '''return hgrc search path. if env var HGRCPATH is set, use it.
1380 for each item in path, if directory, use files ending in .rc,
1375 for each item in path, if directory, use files ending in .rc,
1381 else use item.
1376 else use item.
1382 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1377 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1383 if no HGRCPATH, use default os-specific path.'''
1378 if no HGRCPATH, use default os-specific path.'''
1384 global _rcpath
1379 global _rcpath
1385 if _rcpath is None:
1380 if _rcpath is None:
1386 if 'HGRCPATH' in os.environ:
1381 if 'HGRCPATH' in os.environ:
1387 _rcpath = []
1382 _rcpath = []
1388 for p in os.environ['HGRCPATH'].split(os.pathsep):
1383 for p in os.environ['HGRCPATH'].split(os.pathsep):
1389 if not p: continue
1384 if not p: continue
1390 if os.path.isdir(p):
1385 if os.path.isdir(p):
1391 for f, kind in osutil.listdir(p):
1386 for f, kind in osutil.listdir(p):
1392 if f.endswith('.rc'):
1387 if f.endswith('.rc'):
1393 _rcpath.append(os.path.join(p, f))
1388 _rcpath.append(os.path.join(p, f))
1394 else:
1389 else:
1395 _rcpath.append(p)
1390 _rcpath.append(p)
1396 else:
1391 else:
1397 _rcpath = os_rcpath()
1392 _rcpath = os_rcpath()
1398 return _rcpath
1393 return _rcpath
1399
1394
1400 def bytecount(nbytes):
1395 def bytecount(nbytes):
1401 '''return byte count formatted as readable string, with units'''
1396 '''return byte count formatted as readable string, with units'''
1402
1397
1403 units = (
1398 units = (
1404 (100, 1<<30, _('%.0f GB')),
1399 (100, 1<<30, _('%.0f GB')),
1405 (10, 1<<30, _('%.1f GB')),
1400 (10, 1<<30, _('%.1f GB')),
1406 (1, 1<<30, _('%.2f GB')),
1401 (1, 1<<30, _('%.2f GB')),
1407 (100, 1<<20, _('%.0f MB')),
1402 (100, 1<<20, _('%.0f MB')),
1408 (10, 1<<20, _('%.1f MB')),
1403 (10, 1<<20, _('%.1f MB')),
1409 (1, 1<<20, _('%.2f MB')),
1404 (1, 1<<20, _('%.2f MB')),
1410 (100, 1<<10, _('%.0f KB')),
1405 (100, 1<<10, _('%.0f KB')),
1411 (10, 1<<10, _('%.1f KB')),
1406 (10, 1<<10, _('%.1f KB')),
1412 (1, 1<<10, _('%.2f KB')),
1407 (1, 1<<10, _('%.2f KB')),
1413 (1, 1, _('%.0f bytes')),
1408 (1, 1, _('%.0f bytes')),
1414 )
1409 )
1415
1410
1416 for multiplier, divisor, format in units:
1411 for multiplier, divisor, format in units:
1417 if nbytes >= divisor * multiplier:
1412 if nbytes >= divisor * multiplier:
1418 return format % (nbytes / float(divisor))
1413 return format % (nbytes / float(divisor))
1419 return units[-1][2] % nbytes
1414 return units[-1][2] % nbytes
1420
1415
1421 def drop_scheme(scheme, path):
1416 def drop_scheme(scheme, path):
1422 sc = scheme + ':'
1417 sc = scheme + ':'
1423 if path.startswith(sc):
1418 if path.startswith(sc):
1424 path = path[len(sc):]
1419 path = path[len(sc):]
1425 if path.startswith('//'):
1420 if path.startswith('//'):
1426 path = path[2:]
1421 path = path[2:]
1427 return path
1422 return path
1428
1423
1429 def uirepr(s):
1424 def uirepr(s):
1430 # Avoid double backslash in Windows path repr()
1425 # Avoid double backslash in Windows path repr()
1431 return repr(s).replace('\\\\', '\\')
1426 return repr(s).replace('\\\\', '\\')
1432
1427
1433 def termwidth():
1428 def termwidth():
1434 if 'COLUMNS' in os.environ:
1429 if 'COLUMNS' in os.environ:
1435 try:
1430 try:
1436 return int(os.environ['COLUMNS'])
1431 return int(os.environ['COLUMNS'])
1437 except ValueError:
1432 except ValueError:
1438 pass
1433 pass
1439 try:
1434 try:
1440 import termios, array, fcntl
1435 import termios, array, fcntl
1441 for dev in (sys.stdout, sys.stdin):
1436 for dev in (sys.stdout, sys.stdin):
1442 try:
1437 try:
1443 try:
1438 try:
1444 fd = dev.fileno()
1439 fd = dev.fileno()
1445 except AttributeError:
1440 except AttributeError:
1446 continue
1441 continue
1447 if not os.isatty(fd):
1442 if not os.isatty(fd):
1448 continue
1443 continue
1449 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
1444 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
1450 return array.array('h', arri)[1]
1445 return array.array('h', arri)[1]
1451 except ValueError:
1446 except ValueError:
1452 pass
1447 pass
1453 except ImportError:
1448 except ImportError:
1454 pass
1449 pass
1455 return 80
1450 return 80
1456
1451
1457 def iterlines(iterator):
1452 def iterlines(iterator):
1458 for chunk in iterator:
1453 for chunk in iterator:
1459 for line in chunk.splitlines():
1454 for line in chunk.splitlines():
1460 yield line
1455 yield line
General Comments 0
You need to be logged in to leave comments. Login now