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