##// END OF EJS Templates
error: move UnexpectedOutput (now ResponseError)
Matt Mackall -
r7641:d2f75383 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 util.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 cmdutil.AmbiguousCommand, inst:
58 except cmdutil.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 cmdutil.UnknownCommand, inst:
61 except cmdutil.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 util.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 util.UnexpectedOutput, 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,48 +1,52
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
50 class ResponseError(Exception):
51 """Raised to print an error with part of output and exit."""
52
@@ -1,237 +1,237
1 # httprepo.py - HTTP repository proxy classes for mercurial
1 # httprepo.py - HTTP repository proxy classes for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 #
5 #
6 # This software may be used and distributed according to the terms
6 # This software may be used and distributed according to the terms
7 # of the GNU General Public License, incorporated herein by reference.
7 # of the GNU General Public License, incorporated herein by reference.
8
8
9 from node import bin, hex, nullid
9 from node import bin, hex, nullid
10 from i18n import _
10 from i18n import _
11 import repo, os, urllib, urllib2, urlparse, zlib, util, httplib
11 import repo, os, urllib, urllib2, urlparse, zlib, util, httplib
12 import errno, socket, changegroup, statichttprepo, error, url
12 import errno, socket, changegroup, statichttprepo, error, url
13
13
14 def zgenerator(f):
14 def zgenerator(f):
15 zd = zlib.decompressobj()
15 zd = zlib.decompressobj()
16 try:
16 try:
17 for chunk in util.filechunkiter(f):
17 for chunk in util.filechunkiter(f):
18 yield zd.decompress(chunk)
18 yield zd.decompress(chunk)
19 except httplib.HTTPException:
19 except httplib.HTTPException:
20 raise IOError(None, _('connection ended unexpectedly'))
20 raise IOError(None, _('connection ended unexpectedly'))
21 yield zd.flush()
21 yield zd.flush()
22
22
23 class httprepository(repo.repository):
23 class httprepository(repo.repository):
24 def __init__(self, ui, path):
24 def __init__(self, ui, path):
25 self.path = path
25 self.path = path
26 self.caps = None
26 self.caps = None
27 self.handler = None
27 self.handler = None
28 scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
28 scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
29 if query or frag:
29 if query or frag:
30 raise util.Abort(_('unsupported URL component: "%s"') %
30 raise util.Abort(_('unsupported URL component: "%s"') %
31 (query or frag))
31 (query or frag))
32
32
33 # urllib cannot handle URLs with embedded user or passwd
33 # urllib cannot handle URLs with embedded user or passwd
34 self._url, authinfo = url.getauthinfo(path)
34 self._url, authinfo = url.getauthinfo(path)
35
35
36 self.ui = ui
36 self.ui = ui
37 self.ui.debug(_('using %s\n') % self._url)
37 self.ui.debug(_('using %s\n') % self._url)
38
38
39 self.urlopener = url.opener(ui, authinfo)
39 self.urlopener = url.opener(ui, authinfo)
40
40
41 def url(self):
41 def url(self):
42 return self.path
42 return self.path
43
43
44 # look up capabilities only when needed
44 # look up capabilities only when needed
45
45
46 def get_caps(self):
46 def get_caps(self):
47 if self.caps is None:
47 if self.caps is None:
48 try:
48 try:
49 self.caps = util.set(self.do_read('capabilities').split())
49 self.caps = util.set(self.do_read('capabilities').split())
50 except error.RepoError:
50 except error.RepoError:
51 self.caps = util.set()
51 self.caps = util.set()
52 self.ui.debug(_('capabilities: %s\n') %
52 self.ui.debug(_('capabilities: %s\n') %
53 (' '.join(self.caps or ['none'])))
53 (' '.join(self.caps or ['none'])))
54 return self.caps
54 return self.caps
55
55
56 capabilities = property(get_caps)
56 capabilities = property(get_caps)
57
57
58 def lock(self):
58 def lock(self):
59 raise util.Abort(_('operation not supported over http'))
59 raise util.Abort(_('operation not supported over http'))
60
60
61 def do_cmd(self, cmd, **args):
61 def do_cmd(self, cmd, **args):
62 data = args.pop('data', None)
62 data = args.pop('data', None)
63 headers = args.pop('headers', {})
63 headers = args.pop('headers', {})
64 self.ui.debug(_("sending %s command\n") % cmd)
64 self.ui.debug(_("sending %s command\n") % cmd)
65 q = {"cmd": cmd}
65 q = {"cmd": cmd}
66 q.update(args)
66 q.update(args)
67 qs = '?%s' % urllib.urlencode(q)
67 qs = '?%s' % urllib.urlencode(q)
68 cu = "%s%s" % (self._url, qs)
68 cu = "%s%s" % (self._url, qs)
69 try:
69 try:
70 if data:
70 if data:
71 self.ui.debug(_("sending %s bytes\n") % len(data))
71 self.ui.debug(_("sending %s bytes\n") % len(data))
72 resp = self.urlopener.open(urllib2.Request(cu, data, headers))
72 resp = self.urlopener.open(urllib2.Request(cu, data, headers))
73 except urllib2.HTTPError, inst:
73 except urllib2.HTTPError, inst:
74 if inst.code == 401:
74 if inst.code == 401:
75 raise util.Abort(_('authorization failed'))
75 raise util.Abort(_('authorization failed'))
76 raise
76 raise
77 except httplib.HTTPException, inst:
77 except httplib.HTTPException, inst:
78 self.ui.debug(_('http error while sending %s command\n') % cmd)
78 self.ui.debug(_('http error while sending %s command\n') % cmd)
79 self.ui.print_exc()
79 self.ui.print_exc()
80 raise IOError(None, inst)
80 raise IOError(None, inst)
81 except IndexError:
81 except IndexError:
82 # this only happens with Python 2.3, later versions raise URLError
82 # this only happens with Python 2.3, later versions raise URLError
83 raise util.Abort(_('http error, possibly caused by proxy setting'))
83 raise util.Abort(_('http error, possibly caused by proxy setting'))
84 # record the url we got redirected to
84 # record the url we got redirected to
85 resp_url = resp.geturl()
85 resp_url = resp.geturl()
86 if resp_url.endswith(qs):
86 if resp_url.endswith(qs):
87 resp_url = resp_url[:-len(qs)]
87 resp_url = resp_url[:-len(qs)]
88 if self._url != resp_url:
88 if self._url != resp_url:
89 self.ui.status(_('real URL is %s\n') % resp_url)
89 self.ui.status(_('real URL is %s\n') % resp_url)
90 self._url = resp_url
90 self._url = resp_url
91 try:
91 try:
92 proto = resp.getheader('content-type')
92 proto = resp.getheader('content-type')
93 except AttributeError:
93 except AttributeError:
94 proto = resp.headers['content-type']
94 proto = resp.headers['content-type']
95
95
96 # accept old "text/plain" and "application/hg-changegroup" for now
96 # accept old "text/plain" and "application/hg-changegroup" for now
97 if not (proto.startswith('application/mercurial-') or
97 if not (proto.startswith('application/mercurial-') or
98 proto.startswith('text/plain') or
98 proto.startswith('text/plain') or
99 proto.startswith('application/hg-changegroup')):
99 proto.startswith('application/hg-changegroup')):
100 self.ui.debug(_("Requested URL: '%s'\n") % cu)
100 self.ui.debug(_("Requested URL: '%s'\n") % cu)
101 raise error.RepoError(_("'%s' does not appear to be an hg repository")
101 raise error.RepoError(_("'%s' does not appear to be an hg repository")
102 % self._url)
102 % self._url)
103
103
104 if proto.startswith('application/mercurial-'):
104 if proto.startswith('application/mercurial-'):
105 try:
105 try:
106 version = proto.split('-', 1)[1]
106 version = proto.split('-', 1)[1]
107 version_info = tuple([int(n) for n in version.split('.')])
107 version_info = tuple([int(n) for n in version.split('.')])
108 except ValueError:
108 except ValueError:
109 raise error.RepoError(_("'%s' sent a broken Content-Type "
109 raise error.RepoError(_("'%s' sent a broken Content-Type "
110 "header (%s)") % (self._url, proto))
110 "header (%s)") % (self._url, proto))
111 if version_info > (0, 1):
111 if version_info > (0, 1):
112 raise error.RepoError(_("'%s' uses newer protocol %s") %
112 raise error.RepoError(_("'%s' uses newer protocol %s") %
113 (self._url, version))
113 (self._url, version))
114
114
115 return resp
115 return resp
116
116
117 def do_read(self, cmd, **args):
117 def do_read(self, cmd, **args):
118 fp = self.do_cmd(cmd, **args)
118 fp = self.do_cmd(cmd, **args)
119 try:
119 try:
120 return fp.read()
120 return fp.read()
121 finally:
121 finally:
122 # if using keepalive, allow connection to be reused
122 # if using keepalive, allow connection to be reused
123 fp.close()
123 fp.close()
124
124
125 def lookup(self, key):
125 def lookup(self, key):
126 self.requirecap('lookup', _('look up remote revision'))
126 self.requirecap('lookup', _('look up remote revision'))
127 d = self.do_cmd("lookup", key = key).read()
127 d = self.do_cmd("lookup", key = key).read()
128 success, data = d[:-1].split(' ', 1)
128 success, data = d[:-1].split(' ', 1)
129 if int(success):
129 if int(success):
130 return bin(data)
130 return bin(data)
131 raise error.RepoError(data)
131 raise error.RepoError(data)
132
132
133 def heads(self):
133 def heads(self):
134 d = self.do_read("heads")
134 d = self.do_read("heads")
135 try:
135 try:
136 return map(bin, d[:-1].split(" "))
136 return map(bin, d[:-1].split(" "))
137 except:
137 except:
138 raise util.UnexpectedOutput(_("unexpected response:"), d)
138 raise error.ResponseError(_("unexpected response:"), d)
139
139
140 def branches(self, nodes):
140 def branches(self, nodes):
141 n = " ".join(map(hex, nodes))
141 n = " ".join(map(hex, nodes))
142 d = self.do_read("branches", nodes=n)
142 d = self.do_read("branches", nodes=n)
143 try:
143 try:
144 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
144 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
145 return br
145 return br
146 except:
146 except:
147 raise util.UnexpectedOutput(_("unexpected response:"), d)
147 raise error.ResponseError(_("unexpected response:"), d)
148
148
149 def between(self, pairs):
149 def between(self, pairs):
150 batch = 8 # avoid giant requests
150 batch = 8 # avoid giant requests
151 r = []
151 r = []
152 for i in xrange(0, len(pairs), batch):
152 for i in xrange(0, len(pairs), batch):
153 n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]])
153 n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]])
154 d = self.do_read("between", pairs=n)
154 d = self.do_read("between", pairs=n)
155 try:
155 try:
156 r += [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
156 r += [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
157 except:
157 except:
158 raise util.UnexpectedOutput(_("unexpected response:"), d)
158 raise error.ResponseError(_("unexpected response:"), d)
159 return r
159 return r
160
160
161 def changegroup(self, nodes, kind):
161 def changegroup(self, nodes, kind):
162 n = " ".join(map(hex, nodes))
162 n = " ".join(map(hex, nodes))
163 f = self.do_cmd("changegroup", roots=n)
163 f = self.do_cmd("changegroup", roots=n)
164 return util.chunkbuffer(zgenerator(f))
164 return util.chunkbuffer(zgenerator(f))
165
165
166 def changegroupsubset(self, bases, heads, source):
166 def changegroupsubset(self, bases, heads, source):
167 self.requirecap('changegroupsubset', _('look up remote changes'))
167 self.requirecap('changegroupsubset', _('look up remote changes'))
168 baselst = " ".join([hex(n) for n in bases])
168 baselst = " ".join([hex(n) for n in bases])
169 headlst = " ".join([hex(n) for n in heads])
169 headlst = " ".join([hex(n) for n in heads])
170 f = self.do_cmd("changegroupsubset", bases=baselst, heads=headlst)
170 f = self.do_cmd("changegroupsubset", bases=baselst, heads=headlst)
171 return util.chunkbuffer(zgenerator(f))
171 return util.chunkbuffer(zgenerator(f))
172
172
173 def unbundle(self, cg, heads, source):
173 def unbundle(self, cg, heads, source):
174 # have to stream bundle to a temp file because we do not have
174 # have to stream bundle to a temp file because we do not have
175 # http 1.1 chunked transfer.
175 # http 1.1 chunked transfer.
176
176
177 type = ""
177 type = ""
178 types = self.capable('unbundle')
178 types = self.capable('unbundle')
179 # servers older than d1b16a746db6 will send 'unbundle' as a
179 # servers older than d1b16a746db6 will send 'unbundle' as a
180 # boolean capability
180 # boolean capability
181 try:
181 try:
182 types = types.split(',')
182 types = types.split(',')
183 except AttributeError:
183 except AttributeError:
184 types = [""]
184 types = [""]
185 if types:
185 if types:
186 for x in types:
186 for x in types:
187 if x in changegroup.bundletypes:
187 if x in changegroup.bundletypes:
188 type = x
188 type = x
189 break
189 break
190
190
191 tempname = changegroup.writebundle(cg, None, type)
191 tempname = changegroup.writebundle(cg, None, type)
192 fp = url.httpsendfile(tempname, "rb")
192 fp = url.httpsendfile(tempname, "rb")
193 try:
193 try:
194 try:
194 try:
195 resp = self.do_read(
195 resp = self.do_read(
196 'unbundle', data=fp,
196 'unbundle', data=fp,
197 headers={'Content-Type': 'application/octet-stream'},
197 headers={'Content-Type': 'application/octet-stream'},
198 heads=' '.join(map(hex, heads)))
198 heads=' '.join(map(hex, heads)))
199 resp_code, output = resp.split('\n', 1)
199 resp_code, output = resp.split('\n', 1)
200 try:
200 try:
201 ret = int(resp_code)
201 ret = int(resp_code)
202 except ValueError, err:
202 except ValueError, err:
203 raise util.UnexpectedOutput(
203 raise error.ResponseError(
204 _('push failed (unexpected response):'), resp)
204 _('push failed (unexpected response):'), resp)
205 self.ui.write(output)
205 self.ui.write(output)
206 return ret
206 return ret
207 except socket.error, err:
207 except socket.error, err:
208 if err[0] in (errno.ECONNRESET, errno.EPIPE):
208 if err[0] in (errno.ECONNRESET, errno.EPIPE):
209 raise util.Abort(_('push failed: %s') % err[1])
209 raise util.Abort(_('push failed: %s') % err[1])
210 raise util.Abort(err[1])
210 raise util.Abort(err[1])
211 finally:
211 finally:
212 fp.close()
212 fp.close()
213 os.unlink(tempname)
213 os.unlink(tempname)
214
214
215 def stream_out(self):
215 def stream_out(self):
216 return self.do_cmd('stream_out')
216 return self.do_cmd('stream_out')
217
217
218 class httpsrepository(httprepository):
218 class httpsrepository(httprepository):
219 def __init__(self, ui, path):
219 def __init__(self, ui, path):
220 if not url.has_https:
220 if not url.has_https:
221 raise util.Abort(_('Python support for SSL and HTTPS '
221 raise util.Abort(_('Python support for SSL and HTTPS '
222 'is not installed'))
222 'is not installed'))
223 httprepository.__init__(self, ui, path)
223 httprepository.__init__(self, ui, path)
224
224
225 def instance(ui, path, create):
225 def instance(ui, path, create):
226 if create:
226 if create:
227 raise util.Abort(_('cannot create new http repository'))
227 raise util.Abort(_('cannot create new http repository'))
228 try:
228 try:
229 if path.startswith('https:'):
229 if path.startswith('https:'):
230 inst = httpsrepository(ui, path)
230 inst = httpsrepository(ui, path)
231 else:
231 else:
232 inst = httprepository(ui, path)
232 inst = httprepository(ui, path)
233 inst.between([(nullid, nullid)])
233 inst.between([(nullid, nullid)])
234 return inst
234 return inst
235 except error.RepoError:
235 except error.RepoError:
236 ui.note('(falling back to static-http)\n')
236 ui.note('(falling back to static-http)\n')
237 return statichttprepo.instance(ui, "static-" + path, create)
237 return statichttprepo.instance(ui, "static-" + path, create)
@@ -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, util.SignalInterrupt):
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 util.UnexpectedOutput(
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 util.UnexpectedOutput(
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 util.UnexpectedOutput(
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,247 +1,247
1 # sshrepo.py - ssh repository proxy class for mercurial
1 # sshrepo.py - ssh repository proxy class for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 from node import bin, hex
8 from node import bin, hex
9 from i18n import _
9 from i18n import _
10 import repo, os, re, util, error
10 import repo, os, re, util, error
11
11
12 class remotelock(object):
12 class remotelock(object):
13 def __init__(self, repo):
13 def __init__(self, repo):
14 self.repo = repo
14 self.repo = repo
15 def release(self):
15 def release(self):
16 self.repo.unlock()
16 self.repo.unlock()
17 self.repo = None
17 self.repo = None
18 def __del__(self):
18 def __del__(self):
19 if self.repo:
19 if self.repo:
20 self.release()
20 self.release()
21
21
22 class sshrepository(repo.repository):
22 class sshrepository(repo.repository):
23 def __init__(self, ui, path, create=0):
23 def __init__(self, ui, path, create=0):
24 self._url = path
24 self._url = path
25 self.ui = ui
25 self.ui = ui
26
26
27 m = re.match(r'^ssh://(([^@]+)@)?([^:/]+)(:(\d+))?(/(.*))?$', path)
27 m = re.match(r'^ssh://(([^@]+)@)?([^:/]+)(:(\d+))?(/(.*))?$', path)
28 if not m:
28 if not m:
29 self.raise_(error.RepoError(_("couldn't parse location %s") % path))
29 self.raise_(error.RepoError(_("couldn't parse location %s") % path))
30
30
31 self.user = m.group(2)
31 self.user = m.group(2)
32 self.host = m.group(3)
32 self.host = m.group(3)
33 self.port = m.group(5)
33 self.port = m.group(5)
34 self.path = m.group(7) or "."
34 self.path = m.group(7) or "."
35
35
36 sshcmd = self.ui.config("ui", "ssh", "ssh")
36 sshcmd = self.ui.config("ui", "ssh", "ssh")
37 remotecmd = self.ui.config("ui", "remotecmd", "hg")
37 remotecmd = self.ui.config("ui", "remotecmd", "hg")
38
38
39 args = util.sshargs(sshcmd, self.host, self.user, self.port)
39 args = util.sshargs(sshcmd, self.host, self.user, self.port)
40
40
41 if create:
41 if create:
42 cmd = '%s %s "%s init %s"'
42 cmd = '%s %s "%s init %s"'
43 cmd = cmd % (sshcmd, args, remotecmd, self.path)
43 cmd = cmd % (sshcmd, args, remotecmd, self.path)
44
44
45 ui.note(_('running %s\n') % cmd)
45 ui.note(_('running %s\n') % cmd)
46 res = util.system(cmd)
46 res = util.system(cmd)
47 if res != 0:
47 if res != 0:
48 self.raise_(error.RepoError(_("could not create remote repo")))
48 self.raise_(error.RepoError(_("could not create remote repo")))
49
49
50 self.validate_repo(ui, sshcmd, args, remotecmd)
50 self.validate_repo(ui, sshcmd, args, remotecmd)
51
51
52 def url(self):
52 def url(self):
53 return self._url
53 return self._url
54
54
55 def validate_repo(self, ui, sshcmd, args, remotecmd):
55 def validate_repo(self, ui, sshcmd, args, remotecmd):
56 # cleanup up previous run
56 # cleanup up previous run
57 self.cleanup()
57 self.cleanup()
58
58
59 cmd = '%s %s "%s -R %s serve --stdio"'
59 cmd = '%s %s "%s -R %s serve --stdio"'
60 cmd = cmd % (sshcmd, args, remotecmd, self.path)
60 cmd = cmd % (sshcmd, args, remotecmd, self.path)
61
61
62 cmd = util.quotecommand(cmd)
62 cmd = util.quotecommand(cmd)
63 ui.note(_('running %s\n') % cmd)
63 ui.note(_('running %s\n') % cmd)
64 self.pipeo, self.pipei, self.pipee = util.popen3(cmd, 'b')
64 self.pipeo, self.pipei, self.pipee = util.popen3(cmd, 'b')
65
65
66 # skip any noise generated by remote shell
66 # skip any noise generated by remote shell
67 self.do_cmd("hello")
67 self.do_cmd("hello")
68 r = self.do_cmd("between", pairs=("%s-%s" % ("0"*40, "0"*40)))
68 r = self.do_cmd("between", pairs=("%s-%s" % ("0"*40, "0"*40)))
69 lines = ["", "dummy"]
69 lines = ["", "dummy"]
70 max_noise = 500
70 max_noise = 500
71 while lines[-1] and max_noise:
71 while lines[-1] and max_noise:
72 l = r.readline()
72 l = r.readline()
73 self.readerr()
73 self.readerr()
74 if lines[-1] == "1\n" and l == "\n":
74 if lines[-1] == "1\n" and l == "\n":
75 break
75 break
76 if l:
76 if l:
77 ui.debug(_("remote: "), l)
77 ui.debug(_("remote: "), l)
78 lines.append(l)
78 lines.append(l)
79 max_noise -= 1
79 max_noise -= 1
80 else:
80 else:
81 self.raise_(error.RepoError(_("no suitable response from remote hg")))
81 self.raise_(error.RepoError(_("no suitable response from remote hg")))
82
82
83 self.capabilities = util.set()
83 self.capabilities = util.set()
84 lines.reverse()
84 lines.reverse()
85 for l in lines:
85 for l in lines:
86 if l.startswith("capabilities:"):
86 if l.startswith("capabilities:"):
87 self.capabilities.update(l[:-1].split(":")[1].split())
87 self.capabilities.update(l[:-1].split(":")[1].split())
88 break
88 break
89
89
90 def readerr(self):
90 def readerr(self):
91 while 1:
91 while 1:
92 size = util.fstat(self.pipee).st_size
92 size = util.fstat(self.pipee).st_size
93 if size == 0: break
93 if size == 0: break
94 l = self.pipee.readline()
94 l = self.pipee.readline()
95 if not l: break
95 if not l: break
96 self.ui.status(_("remote: "), l)
96 self.ui.status(_("remote: "), l)
97
97
98 def raise_(self, exception):
98 def raise_(self, exception):
99 self.cleanup()
99 self.cleanup()
100 raise exception
100 raise exception
101
101
102 def cleanup(self):
102 def cleanup(self):
103 try:
103 try:
104 self.pipeo.close()
104 self.pipeo.close()
105 self.pipei.close()
105 self.pipei.close()
106 # read the error descriptor until EOF
106 # read the error descriptor until EOF
107 for l in self.pipee:
107 for l in self.pipee:
108 self.ui.status(_("remote: "), l)
108 self.ui.status(_("remote: "), l)
109 self.pipee.close()
109 self.pipee.close()
110 except:
110 except:
111 pass
111 pass
112
112
113 __del__ = cleanup
113 __del__ = cleanup
114
114
115 def do_cmd(self, cmd, **args):
115 def do_cmd(self, cmd, **args):
116 self.ui.debug(_("sending %s command\n") % cmd)
116 self.ui.debug(_("sending %s command\n") % cmd)
117 self.pipeo.write("%s\n" % cmd)
117 self.pipeo.write("%s\n" % cmd)
118 for k, v in args.iteritems():
118 for k, v in args.iteritems():
119 self.pipeo.write("%s %d\n" % (k, len(v)))
119 self.pipeo.write("%s %d\n" % (k, len(v)))
120 self.pipeo.write(v)
120 self.pipeo.write(v)
121 self.pipeo.flush()
121 self.pipeo.flush()
122
122
123 return self.pipei
123 return self.pipei
124
124
125 def call(self, cmd, **args):
125 def call(self, cmd, **args):
126 self.do_cmd(cmd, **args)
126 self.do_cmd(cmd, **args)
127 return self._recv()
127 return self._recv()
128
128
129 def _recv(self):
129 def _recv(self):
130 l = self.pipei.readline()
130 l = self.pipei.readline()
131 self.readerr()
131 self.readerr()
132 try:
132 try:
133 l = int(l)
133 l = int(l)
134 except:
134 except:
135 self.raise_(util.UnexpectedOutput(_("unexpected response:"), l))
135 self.raise_(error.ResponseError(_("unexpected response:"), l))
136 return self.pipei.read(l)
136 return self.pipei.read(l)
137
137
138 def _send(self, data, flush=False):
138 def _send(self, data, flush=False):
139 self.pipeo.write("%d\n" % len(data))
139 self.pipeo.write("%d\n" % len(data))
140 if data:
140 if data:
141 self.pipeo.write(data)
141 self.pipeo.write(data)
142 if flush:
142 if flush:
143 self.pipeo.flush()
143 self.pipeo.flush()
144 self.readerr()
144 self.readerr()
145
145
146 def lock(self):
146 def lock(self):
147 self.call("lock")
147 self.call("lock")
148 return remotelock(self)
148 return remotelock(self)
149
149
150 def unlock(self):
150 def unlock(self):
151 self.call("unlock")
151 self.call("unlock")
152
152
153 def lookup(self, key):
153 def lookup(self, key):
154 self.requirecap('lookup', _('look up remote revision'))
154 self.requirecap('lookup', _('look up remote revision'))
155 d = self.call("lookup", key=key)
155 d = self.call("lookup", key=key)
156 success, data = d[:-1].split(" ", 1)
156 success, data = d[:-1].split(" ", 1)
157 if int(success):
157 if int(success):
158 return bin(data)
158 return bin(data)
159 else:
159 else:
160 self.raise_(error.RepoError(data))
160 self.raise_(error.RepoError(data))
161
161
162 def heads(self):
162 def heads(self):
163 d = self.call("heads")
163 d = self.call("heads")
164 try:
164 try:
165 return map(bin, d[:-1].split(" "))
165 return map(bin, d[:-1].split(" "))
166 except:
166 except:
167 self.raise_(util.UnexpectedOutput(_("unexpected response:"), d))
167 self.raise_(error.ResponseError(_("unexpected response:"), d))
168
168
169 def branches(self, nodes):
169 def branches(self, nodes):
170 n = " ".join(map(hex, nodes))
170 n = " ".join(map(hex, nodes))
171 d = self.call("branches", nodes=n)
171 d = self.call("branches", nodes=n)
172 try:
172 try:
173 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
173 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
174 return br
174 return br
175 except:
175 except:
176 self.raise_(util.UnexpectedOutput(_("unexpected response:"), d))
176 self.raise_(error.ResponseError(_("unexpected response:"), d))
177
177
178 def between(self, pairs):
178 def between(self, pairs):
179 n = " ".join(["-".join(map(hex, p)) for p in pairs])
179 n = " ".join(["-".join(map(hex, p)) for p in pairs])
180 d = self.call("between", pairs=n)
180 d = self.call("between", pairs=n)
181 try:
181 try:
182 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
182 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
183 return p
183 return p
184 except:
184 except:
185 self.raise_(util.UnexpectedOutput(_("unexpected response:"), d))
185 self.raise_(error.ResponseError(_("unexpected response:"), d))
186
186
187 def changegroup(self, nodes, kind):
187 def changegroup(self, nodes, kind):
188 n = " ".join(map(hex, nodes))
188 n = " ".join(map(hex, nodes))
189 return self.do_cmd("changegroup", roots=n)
189 return self.do_cmd("changegroup", roots=n)
190
190
191 def changegroupsubset(self, bases, heads, kind):
191 def changegroupsubset(self, bases, heads, kind):
192 self.requirecap('changegroupsubset', _('look up remote changes'))
192 self.requirecap('changegroupsubset', _('look up remote changes'))
193 bases = " ".join(map(hex, bases))
193 bases = " ".join(map(hex, bases))
194 heads = " ".join(map(hex, heads))
194 heads = " ".join(map(hex, heads))
195 return self.do_cmd("changegroupsubset", bases=bases, heads=heads)
195 return self.do_cmd("changegroupsubset", bases=bases, heads=heads)
196
196
197 def unbundle(self, cg, heads, source):
197 def unbundle(self, cg, heads, source):
198 d = self.call("unbundle", heads=' '.join(map(hex, heads)))
198 d = self.call("unbundle", heads=' '.join(map(hex, heads)))
199 if d:
199 if d:
200 # remote may send "unsynced changes"
200 # remote may send "unsynced changes"
201 self.raise_(error.RepoError(_("push refused: %s") % d))
201 self.raise_(error.RepoError(_("push refused: %s") % d))
202
202
203 while 1:
203 while 1:
204 d = cg.read(4096)
204 d = cg.read(4096)
205 if not d:
205 if not d:
206 break
206 break
207 self._send(d)
207 self._send(d)
208
208
209 self._send("", flush=True)
209 self._send("", flush=True)
210
210
211 r = self._recv()
211 r = self._recv()
212 if r:
212 if r:
213 # remote may send "unsynced changes"
213 # remote may send "unsynced changes"
214 self.raise_(error.RepoError(_("push failed: %s") % r))
214 self.raise_(error.RepoError(_("push failed: %s") % r))
215
215
216 r = self._recv()
216 r = self._recv()
217 try:
217 try:
218 return int(r)
218 return int(r)
219 except:
219 except:
220 self.raise_(util.UnexpectedOutput(_("unexpected response:"), r))
220 self.raise_(error.ResponseError(_("unexpected response:"), r))
221
221
222 def addchangegroup(self, cg, source, url):
222 def addchangegroup(self, cg, source, url):
223 d = self.call("addchangegroup")
223 d = self.call("addchangegroup")
224 if d:
224 if d:
225 self.raise_(error.RepoError(_("push refused: %s") % d))
225 self.raise_(error.RepoError(_("push refused: %s") % d))
226 while 1:
226 while 1:
227 d = cg.read(4096)
227 d = cg.read(4096)
228 if not d:
228 if not d:
229 break
229 break
230 self.pipeo.write(d)
230 self.pipeo.write(d)
231 self.readerr()
231 self.readerr()
232
232
233 self.pipeo.flush()
233 self.pipeo.flush()
234
234
235 self.readerr()
235 self.readerr()
236 r = self._recv()
236 r = self._recv()
237 if not r:
237 if not r:
238 return 1
238 return 1
239 try:
239 try:
240 return int(r)
240 return int(r)
241 except:
241 except:
242 self.raise_(util.UnexpectedOutput(_("unexpected response:"), r))
242 self.raise_(error.ResponseError(_("unexpected response:"), r))
243
243
244 def stream_out(self):
244 def stream_out(self):
245 return self.do_cmd('stream_out')
245 return self.do_cmd('stream_out')
246
246
247 instance = sshrepository
247 instance = sshrepository
@@ -1,2022 +1,2019
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):
188 class SignalInterrupt(Exception):
189 """Exception raised on SIGTERM and SIGHUP."""
189 """Exception raised on SIGTERM and SIGHUP."""
190
190
191 # differences from SafeConfigParser:
191 # differences from SafeConfigParser:
192 # - case-sensitive keys
192 # - case-sensitive keys
193 # - allows values that are not strings (this means that you may not
193 # - allows values that are not strings (this means that you may not
194 # be able to save the configuration to a file)
194 # be able to save the configuration to a file)
195 class configparser(ConfigParser.SafeConfigParser):
195 class configparser(ConfigParser.SafeConfigParser):
196 def optionxform(self, optionstr):
196 def optionxform(self, optionstr):
197 return optionstr
197 return optionstr
198
198
199 def set(self, section, option, value):
199 def set(self, section, option, value):
200 return ConfigParser.ConfigParser.set(self, section, option, value)
200 return ConfigParser.ConfigParser.set(self, section, option, value)
201
201
202 def _interpolate(self, section, option, rawval, vars):
202 def _interpolate(self, section, option, rawval, vars):
203 if not isinstance(rawval, basestring):
203 if not isinstance(rawval, basestring):
204 return rawval
204 return rawval
205 return ConfigParser.SafeConfigParser._interpolate(self, section,
205 return ConfigParser.SafeConfigParser._interpolate(self, section,
206 option, rawval, vars)
206 option, rawval, vars)
207
207
208 def cachefunc(func):
208 def cachefunc(func):
209 '''cache the result of function calls'''
209 '''cache the result of function calls'''
210 # XXX doesn't handle keywords args
210 # XXX doesn't handle keywords args
211 cache = {}
211 cache = {}
212 if func.func_code.co_argcount == 1:
212 if func.func_code.co_argcount == 1:
213 # we gain a small amount of time because
213 # we gain a small amount of time because
214 # we don't need to pack/unpack the list
214 # we don't need to pack/unpack the list
215 def f(arg):
215 def f(arg):
216 if arg not in cache:
216 if arg not in cache:
217 cache[arg] = func(arg)
217 cache[arg] = func(arg)
218 return cache[arg]
218 return cache[arg]
219 else:
219 else:
220 def f(*args):
220 def f(*args):
221 if args not in cache:
221 if args not in cache:
222 cache[args] = func(*args)
222 cache[args] = func(*args)
223 return cache[args]
223 return cache[args]
224
224
225 return f
225 return f
226
226
227 def pipefilter(s, cmd):
227 def pipefilter(s, cmd):
228 '''filter string S through command CMD, returning its output'''
228 '''filter string S through command CMD, returning its output'''
229 (pin, pout) = popen2(cmd, 'b')
229 (pin, pout) = popen2(cmd, 'b')
230 def writer():
230 def writer():
231 try:
231 try:
232 pin.write(s)
232 pin.write(s)
233 pin.close()
233 pin.close()
234 except IOError, inst:
234 except IOError, inst:
235 if inst.errno != errno.EPIPE:
235 if inst.errno != errno.EPIPE:
236 raise
236 raise
237
237
238 # we should use select instead on UNIX, but this will work on most
238 # we should use select instead on UNIX, but this will work on most
239 # systems, including Windows
239 # systems, including Windows
240 w = threading.Thread(target=writer)
240 w = threading.Thread(target=writer)
241 w.start()
241 w.start()
242 f = pout.read()
242 f = pout.read()
243 pout.close()
243 pout.close()
244 w.join()
244 w.join()
245 return f
245 return f
246
246
247 def tempfilter(s, cmd):
247 def tempfilter(s, cmd):
248 '''filter string S through a pair of temporary files with CMD.
248 '''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,
249 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
250 with the strings INFILE and OUTFILE replaced by the real names of
251 the temporary files generated.'''
251 the temporary files generated.'''
252 inname, outname = None, None
252 inname, outname = None, None
253 try:
253 try:
254 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
254 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
255 fp = os.fdopen(infd, 'wb')
255 fp = os.fdopen(infd, 'wb')
256 fp.write(s)
256 fp.write(s)
257 fp.close()
257 fp.close()
258 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
258 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
259 os.close(outfd)
259 os.close(outfd)
260 cmd = cmd.replace('INFILE', inname)
260 cmd = cmd.replace('INFILE', inname)
261 cmd = cmd.replace('OUTFILE', outname)
261 cmd = cmd.replace('OUTFILE', outname)
262 code = os.system(cmd)
262 code = os.system(cmd)
263 if sys.platform == 'OpenVMS' and code & 1:
263 if sys.platform == 'OpenVMS' and code & 1:
264 code = 0
264 code = 0
265 if code: raise Abort(_("command '%s' failed: %s") %
265 if code: raise Abort(_("command '%s' failed: %s") %
266 (cmd, explain_exit(code)))
266 (cmd, explain_exit(code)))
267 return open(outname, 'rb').read()
267 return open(outname, 'rb').read()
268 finally:
268 finally:
269 try:
269 try:
270 if inname: os.unlink(inname)
270 if inname: os.unlink(inname)
271 except: pass
271 except: pass
272 try:
272 try:
273 if outname: os.unlink(outname)
273 if outname: os.unlink(outname)
274 except: pass
274 except: pass
275
275
276 filtertable = {
276 filtertable = {
277 'tempfile:': tempfilter,
277 'tempfile:': tempfilter,
278 'pipe:': pipefilter,
278 'pipe:': pipefilter,
279 }
279 }
280
280
281 def filter(s, cmd):
281 def filter(s, cmd):
282 "filter a string through a command that transforms its input to its output"
282 "filter a string through a command that transforms its input to its output"
283 for name, fn in filtertable.iteritems():
283 for name, fn in filtertable.iteritems():
284 if cmd.startswith(name):
284 if cmd.startswith(name):
285 return fn(s, cmd[len(name):].lstrip())
285 return fn(s, cmd[len(name):].lstrip())
286 return pipefilter(s, cmd)
286 return pipefilter(s, cmd)
287
287
288 def binary(s):
288 def binary(s):
289 """return true if a string is binary data"""
289 """return true if a string is binary data"""
290 if s and '\0' in s:
290 if s and '\0' in s:
291 return True
291 return True
292 return False
292 return False
293
293
294 def unique(g):
294 def unique(g):
295 """return the uniq elements of iterable g"""
295 """return the uniq elements of iterable g"""
296 return dict.fromkeys(g).keys()
296 return dict.fromkeys(g).keys()
297
297
298 def sort(l):
298 def sort(l):
299 if not isinstance(l, list):
299 if not isinstance(l, list):
300 l = list(l)
300 l = list(l)
301 l.sort()
301 l.sort()
302 return l
302 return l
303
303
304 def increasingchunks(source, min=1024, max=65536):
304 def increasingchunks(source, min=1024, max=65536):
305 '''return no less than min bytes per chunk while data remains,
305 '''return no less than min bytes per chunk while data remains,
306 doubling min after each chunk until it reaches max'''
306 doubling min after each chunk until it reaches max'''
307 def log2(x):
307 def log2(x):
308 if not x:
308 if not x:
309 return 0
309 return 0
310 i = 0
310 i = 0
311 while x:
311 while x:
312 x >>= 1
312 x >>= 1
313 i += 1
313 i += 1
314 return i - 1
314 return i - 1
315
315
316 buf = []
316 buf = []
317 blen = 0
317 blen = 0
318 for chunk in source:
318 for chunk in source:
319 buf.append(chunk)
319 buf.append(chunk)
320 blen += len(chunk)
320 blen += len(chunk)
321 if blen >= min:
321 if blen >= min:
322 if min < max:
322 if min < max:
323 min = min << 1
323 min = min << 1
324 nmin = 1 << log2(blen)
324 nmin = 1 << log2(blen)
325 if nmin > min:
325 if nmin > min:
326 min = nmin
326 min = nmin
327 if min > max:
327 if min > max:
328 min = max
328 min = max
329 yield ''.join(buf)
329 yield ''.join(buf)
330 blen = 0
330 blen = 0
331 buf = []
331 buf = []
332 if buf:
332 if buf:
333 yield ''.join(buf)
333 yield ''.join(buf)
334
334
335 class Abort(Exception):
335 class Abort(Exception):
336 """Raised if a command needs to print an error and exit."""
336 """Raised if a command needs to print an error and exit."""
337
337
338 class UnexpectedOutput(Abort):
339 """Raised to print an error with part of output and exit."""
340
341 def always(fn): return True
338 def always(fn): return True
342 def never(fn): return False
339 def never(fn): return False
343
340
344 def expand_glob(pats):
341 def expand_glob(pats):
345 '''On Windows, expand the implicit globs in a list of patterns'''
342 '''On Windows, expand the implicit globs in a list of patterns'''
346 if os.name != 'nt':
343 if os.name != 'nt':
347 return list(pats)
344 return list(pats)
348 ret = []
345 ret = []
349 for p in pats:
346 for p in pats:
350 kind, name = patkind(p, None)
347 kind, name = patkind(p, None)
351 if kind is None:
348 if kind is None:
352 globbed = glob.glob(name)
349 globbed = glob.glob(name)
353 if globbed:
350 if globbed:
354 ret.extend(globbed)
351 ret.extend(globbed)
355 continue
352 continue
356 # if we couldn't expand the glob, just keep it around
353 # if we couldn't expand the glob, just keep it around
357 ret.append(p)
354 ret.append(p)
358 return ret
355 return ret
359
356
360 def patkind(name, default):
357 def patkind(name, default):
361 """Split a string into an optional pattern kind prefix and the
358 """Split a string into an optional pattern kind prefix and the
362 actual pattern."""
359 actual pattern."""
363 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
360 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
364 if name.startswith(prefix + ':'): return name.split(':', 1)
361 if name.startswith(prefix + ':'): return name.split(':', 1)
365 return default, name
362 return default, name
366
363
367 def globre(pat, head='^', tail='$'):
364 def globre(pat, head='^', tail='$'):
368 "convert a glob pattern into a regexp"
365 "convert a glob pattern into a regexp"
369 i, n = 0, len(pat)
366 i, n = 0, len(pat)
370 res = ''
367 res = ''
371 group = 0
368 group = 0
372 def peek(): return i < n and pat[i]
369 def peek(): return i < n and pat[i]
373 while i < n:
370 while i < n:
374 c = pat[i]
371 c = pat[i]
375 i = i+1
372 i = i+1
376 if c == '*':
373 if c == '*':
377 if peek() == '*':
374 if peek() == '*':
378 i += 1
375 i += 1
379 res += '.*'
376 res += '.*'
380 else:
377 else:
381 res += '[^/]*'
378 res += '[^/]*'
382 elif c == '?':
379 elif c == '?':
383 res += '.'
380 res += '.'
384 elif c == '[':
381 elif c == '[':
385 j = i
382 j = i
386 if j < n and pat[j] in '!]':
383 if j < n and pat[j] in '!]':
387 j += 1
384 j += 1
388 while j < n and pat[j] != ']':
385 while j < n and pat[j] != ']':
389 j += 1
386 j += 1
390 if j >= n:
387 if j >= n:
391 res += '\\['
388 res += '\\['
392 else:
389 else:
393 stuff = pat[i:j].replace('\\','\\\\')
390 stuff = pat[i:j].replace('\\','\\\\')
394 i = j + 1
391 i = j + 1
395 if stuff[0] == '!':
392 if stuff[0] == '!':
396 stuff = '^' + stuff[1:]
393 stuff = '^' + stuff[1:]
397 elif stuff[0] == '^':
394 elif stuff[0] == '^':
398 stuff = '\\' + stuff
395 stuff = '\\' + stuff
399 res = '%s[%s]' % (res, stuff)
396 res = '%s[%s]' % (res, stuff)
400 elif c == '{':
397 elif c == '{':
401 group += 1
398 group += 1
402 res += '(?:'
399 res += '(?:'
403 elif c == '}' and group:
400 elif c == '}' and group:
404 res += ')'
401 res += ')'
405 group -= 1
402 group -= 1
406 elif c == ',' and group:
403 elif c == ',' and group:
407 res += '|'
404 res += '|'
408 elif c == '\\':
405 elif c == '\\':
409 p = peek()
406 p = peek()
410 if p:
407 if p:
411 i += 1
408 i += 1
412 res += re.escape(p)
409 res += re.escape(p)
413 else:
410 else:
414 res += re.escape(c)
411 res += re.escape(c)
415 else:
412 else:
416 res += re.escape(c)
413 res += re.escape(c)
417 return head + res + tail
414 return head + res + tail
418
415
419 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
416 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
420
417
421 def pathto(root, n1, n2):
418 def pathto(root, n1, n2):
422 '''return the relative path from one place to another.
419 '''return the relative path from one place to another.
423 root should use os.sep to separate directories
420 root should use os.sep to separate directories
424 n1 should use os.sep to separate directories
421 n1 should use os.sep to separate directories
425 n2 should use "/" to separate directories
422 n2 should use "/" to separate directories
426 returns an os.sep-separated path.
423 returns an os.sep-separated path.
427
424
428 If n1 is a relative path, it's assumed it's
425 If n1 is a relative path, it's assumed it's
429 relative to root.
426 relative to root.
430 n2 should always be relative to root.
427 n2 should always be relative to root.
431 '''
428 '''
432 if not n1: return localpath(n2)
429 if not n1: return localpath(n2)
433 if os.path.isabs(n1):
430 if os.path.isabs(n1):
434 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
431 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
435 return os.path.join(root, localpath(n2))
432 return os.path.join(root, localpath(n2))
436 n2 = '/'.join((pconvert(root), n2))
433 n2 = '/'.join((pconvert(root), n2))
437 a, b = splitpath(n1), n2.split('/')
434 a, b = splitpath(n1), n2.split('/')
438 a.reverse()
435 a.reverse()
439 b.reverse()
436 b.reverse()
440 while a and b and a[-1] == b[-1]:
437 while a and b and a[-1] == b[-1]:
441 a.pop()
438 a.pop()
442 b.pop()
439 b.pop()
443 b.reverse()
440 b.reverse()
444 return os.sep.join((['..'] * len(a)) + b) or '.'
441 return os.sep.join((['..'] * len(a)) + b) or '.'
445
442
446 def canonpath(root, cwd, myname):
443 def canonpath(root, cwd, myname):
447 """return the canonical path of myname, given cwd and root"""
444 """return the canonical path of myname, given cwd and root"""
448 if root == os.sep:
445 if root == os.sep:
449 rootsep = os.sep
446 rootsep = os.sep
450 elif endswithsep(root):
447 elif endswithsep(root):
451 rootsep = root
448 rootsep = root
452 else:
449 else:
453 rootsep = root + os.sep
450 rootsep = root + os.sep
454 name = myname
451 name = myname
455 if not os.path.isabs(name):
452 if not os.path.isabs(name):
456 name = os.path.join(root, cwd, name)
453 name = os.path.join(root, cwd, name)
457 name = os.path.normpath(name)
454 name = os.path.normpath(name)
458 audit_path = path_auditor(root)
455 audit_path = path_auditor(root)
459 if name != rootsep and name.startswith(rootsep):
456 if name != rootsep and name.startswith(rootsep):
460 name = name[len(rootsep):]
457 name = name[len(rootsep):]
461 audit_path(name)
458 audit_path(name)
462 return pconvert(name)
459 return pconvert(name)
463 elif name == root:
460 elif name == root:
464 return ''
461 return ''
465 else:
462 else:
466 # Determine whether `name' is in the hierarchy at or beneath `root',
463 # Determine whether `name' is in the hierarchy at or beneath `root',
467 # by iterating name=dirname(name) until that causes no change (can't
464 # by iterating name=dirname(name) until that causes no change (can't
468 # check name == '/', because that doesn't work on windows). For each
465 # check name == '/', because that doesn't work on windows). For each
469 # `name', compare dev/inode numbers. If they match, the list `rel'
466 # `name', compare dev/inode numbers. If they match, the list `rel'
470 # holds the reversed list of components making up the relative file
467 # holds the reversed list of components making up the relative file
471 # name we want.
468 # name we want.
472 root_st = os.stat(root)
469 root_st = os.stat(root)
473 rel = []
470 rel = []
474 while True:
471 while True:
475 try:
472 try:
476 name_st = os.stat(name)
473 name_st = os.stat(name)
477 except OSError:
474 except OSError:
478 break
475 break
479 if samestat(name_st, root_st):
476 if samestat(name_st, root_st):
480 if not rel:
477 if not rel:
481 # name was actually the same as root (maybe a symlink)
478 # name was actually the same as root (maybe a symlink)
482 return ''
479 return ''
483 rel.reverse()
480 rel.reverse()
484 name = os.path.join(*rel)
481 name = os.path.join(*rel)
485 audit_path(name)
482 audit_path(name)
486 return pconvert(name)
483 return pconvert(name)
487 dirname, basename = os.path.split(name)
484 dirname, basename = os.path.split(name)
488 rel.append(basename)
485 rel.append(basename)
489 if dirname == name:
486 if dirname == name:
490 break
487 break
491 name = dirname
488 name = dirname
492
489
493 raise Abort('%s not under root' % myname)
490 raise Abort('%s not under root' % myname)
494
491
495 def matcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None, dflt_pat='glob'):
492 def matcher(canonroot, cwd='', names=[], inc=[], exc=[], src=None, dflt_pat='glob'):
496 """build a function to match a set of file patterns
493 """build a function to match a set of file patterns
497
494
498 arguments:
495 arguments:
499 canonroot - the canonical root of the tree you're matching against
496 canonroot - the canonical root of the tree you're matching against
500 cwd - the current working directory, if relevant
497 cwd - the current working directory, if relevant
501 names - patterns to find
498 names - patterns to find
502 inc - patterns to include
499 inc - patterns to include
503 exc - patterns to exclude
500 exc - patterns to exclude
504 dflt_pat - if a pattern in names has no explicit type, assume this one
501 dflt_pat - if a pattern in names has no explicit type, assume this one
505 src - where these patterns came from (e.g. .hgignore)
502 src - where these patterns came from (e.g. .hgignore)
506
503
507 a pattern is one of:
504 a pattern is one of:
508 'glob:<glob>' - a glob relative to cwd
505 'glob:<glob>' - a glob relative to cwd
509 're:<regexp>' - a regular expression
506 're:<regexp>' - a regular expression
510 'path:<path>' - a path relative to canonroot
507 'path:<path>' - a path relative to canonroot
511 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
508 'relglob:<glob>' - an unrooted glob (*.c matches C files in all dirs)
512 'relpath:<path>' - a path relative to cwd
509 'relpath:<path>' - a path relative to cwd
513 'relre:<regexp>' - a regexp that doesn't have to match the start of a name
510 'relre:<regexp>' - a regexp that doesn't have to match the start of a name
514 '<something>' - one of the cases above, selected by the dflt_pat argument
511 '<something>' - one of the cases above, selected by the dflt_pat argument
515
512
516 returns:
513 returns:
517 a 3-tuple containing
514 a 3-tuple containing
518 - list of roots (places where one should start a recursive walk of the fs);
515 - list of roots (places where one should start a recursive walk of the fs);
519 this often matches the explicit non-pattern names passed in, but also
516 this often matches the explicit non-pattern names passed in, but also
520 includes the initial part of glob: patterns that has no glob characters
517 includes the initial part of glob: patterns that has no glob characters
521 - a bool match(filename) function
518 - a bool match(filename) function
522 - a bool indicating if any patterns were passed in
519 - a bool indicating if any patterns were passed in
523 """
520 """
524
521
525 # a common case: no patterns at all
522 # a common case: no patterns at all
526 if not names and not inc and not exc:
523 if not names and not inc and not exc:
527 return [], always, False
524 return [], always, False
528
525
529 def contains_glob(name):
526 def contains_glob(name):
530 for c in name:
527 for c in name:
531 if c in _globchars: return True
528 if c in _globchars: return True
532 return False
529 return False
533
530
534 def regex(kind, name, tail):
531 def regex(kind, name, tail):
535 '''convert a pattern into a regular expression'''
532 '''convert a pattern into a regular expression'''
536 if not name:
533 if not name:
537 return ''
534 return ''
538 if kind == 're':
535 if kind == 're':
539 return name
536 return name
540 elif kind == 'path':
537 elif kind == 'path':
541 return '^' + re.escape(name) + '(?:/|$)'
538 return '^' + re.escape(name) + '(?:/|$)'
542 elif kind == 'relglob':
539 elif kind == 'relglob':
543 return globre(name, '(?:|.*/)', tail)
540 return globre(name, '(?:|.*/)', tail)
544 elif kind == 'relpath':
541 elif kind == 'relpath':
545 return re.escape(name) + '(?:/|$)'
542 return re.escape(name) + '(?:/|$)'
546 elif kind == 'relre':
543 elif kind == 'relre':
547 if name.startswith('^'):
544 if name.startswith('^'):
548 return name
545 return name
549 return '.*' + name
546 return '.*' + name
550 return globre(name, '', tail)
547 return globre(name, '', tail)
551
548
552 def matchfn(pats, tail):
549 def matchfn(pats, tail):
553 """build a matching function from a set of patterns"""
550 """build a matching function from a set of patterns"""
554 if not pats:
551 if not pats:
555 return
552 return
556 try:
553 try:
557 pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats])
554 pat = '(?:%s)' % '|'.join([regex(k, p, tail) for (k, p) in pats])
558 if len(pat) > 20000:
555 if len(pat) > 20000:
559 raise OverflowError()
556 raise OverflowError()
560 return re.compile(pat).match
557 return re.compile(pat).match
561 except OverflowError:
558 except OverflowError:
562 # We're using a Python with a tiny regex engine and we
559 # We're using a Python with a tiny regex engine and we
563 # made it explode, so we'll divide the pattern list in two
560 # made it explode, so we'll divide the pattern list in two
564 # until it works
561 # until it works
565 l = len(pats)
562 l = len(pats)
566 if l < 2:
563 if l < 2:
567 raise
564 raise
568 a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail)
565 a, b = matchfn(pats[:l//2], tail), matchfn(pats[l//2:], tail)
569 return lambda s: a(s) or b(s)
566 return lambda s: a(s) or b(s)
570 except re.error:
567 except re.error:
571 for k, p in pats:
568 for k, p in pats:
572 try:
569 try:
573 re.compile('(?:%s)' % regex(k, p, tail))
570 re.compile('(?:%s)' % regex(k, p, tail))
574 except re.error:
571 except re.error:
575 if src:
572 if src:
576 raise Abort("%s: invalid pattern (%s): %s" %
573 raise Abort("%s: invalid pattern (%s): %s" %
577 (src, k, p))
574 (src, k, p))
578 else:
575 else:
579 raise Abort("invalid pattern (%s): %s" % (k, p))
576 raise Abort("invalid pattern (%s): %s" % (k, p))
580 raise Abort("invalid pattern")
577 raise Abort("invalid pattern")
581
578
582 def globprefix(pat):
579 def globprefix(pat):
583 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
580 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
584 root = []
581 root = []
585 for p in pat.split('/'):
582 for p in pat.split('/'):
586 if contains_glob(p): break
583 if contains_glob(p): break
587 root.append(p)
584 root.append(p)
588 return '/'.join(root) or '.'
585 return '/'.join(root) or '.'
589
586
590 def normalizepats(names, default):
587 def normalizepats(names, default):
591 pats = []
588 pats = []
592 roots = []
589 roots = []
593 anypats = False
590 anypats = False
594 for kind, name in [patkind(p, default) for p in names]:
591 for kind, name in [patkind(p, default) for p in names]:
595 if kind in ('glob', 'relpath'):
592 if kind in ('glob', 'relpath'):
596 name = canonpath(canonroot, cwd, name)
593 name = canonpath(canonroot, cwd, name)
597 elif kind in ('relglob', 'path'):
594 elif kind in ('relglob', 'path'):
598 name = normpath(name)
595 name = normpath(name)
599
596
600 pats.append((kind, name))
597 pats.append((kind, name))
601
598
602 if kind in ('glob', 're', 'relglob', 'relre'):
599 if kind in ('glob', 're', 'relglob', 'relre'):
603 anypats = True
600 anypats = True
604
601
605 if kind == 'glob':
602 if kind == 'glob':
606 root = globprefix(name)
603 root = globprefix(name)
607 roots.append(root)
604 roots.append(root)
608 elif kind in ('relpath', 'path'):
605 elif kind in ('relpath', 'path'):
609 roots.append(name or '.')
606 roots.append(name or '.')
610 elif kind == 'relglob':
607 elif kind == 'relglob':
611 roots.append('.')
608 roots.append('.')
612 return roots, pats, anypats
609 return roots, pats, anypats
613
610
614 roots, pats, anypats = normalizepats(names, dflt_pat)
611 roots, pats, anypats = normalizepats(names, dflt_pat)
615
612
616 patmatch = matchfn(pats, '$') or always
613 patmatch = matchfn(pats, '$') or always
617 incmatch = always
614 incmatch = always
618 if inc:
615 if inc:
619 dummy, inckinds, dummy = normalizepats(inc, 'glob')
616 dummy, inckinds, dummy = normalizepats(inc, 'glob')
620 incmatch = matchfn(inckinds, '(?:/|$)')
617 incmatch = matchfn(inckinds, '(?:/|$)')
621 excmatch = never
618 excmatch = never
622 if exc:
619 if exc:
623 dummy, exckinds, dummy = normalizepats(exc, 'glob')
620 dummy, exckinds, dummy = normalizepats(exc, 'glob')
624 excmatch = matchfn(exckinds, '(?:/|$)')
621 excmatch = matchfn(exckinds, '(?:/|$)')
625
622
626 if not names and inc and not exc:
623 if not names and inc and not exc:
627 # common case: hgignore patterns
624 # common case: hgignore patterns
628 match = incmatch
625 match = incmatch
629 else:
626 else:
630 match = lambda fn: incmatch(fn) and not excmatch(fn) and patmatch(fn)
627 match = lambda fn: incmatch(fn) and not excmatch(fn) and patmatch(fn)
631
628
632 return (roots, match, (inc or exc or anypats) and True)
629 return (roots, match, (inc or exc or anypats) and True)
633
630
634 _hgexecutable = None
631 _hgexecutable = None
635
632
636 def main_is_frozen():
633 def main_is_frozen():
637 """return True if we are a frozen executable.
634 """return True if we are a frozen executable.
638
635
639 The code supports py2exe (most common, Windows only) and tools/freeze
636 The code supports py2exe (most common, Windows only) and tools/freeze
640 (portable, not much used).
637 (portable, not much used).
641 """
638 """
642 return (hasattr(sys, "frozen") or # new py2exe
639 return (hasattr(sys, "frozen") or # new py2exe
643 hasattr(sys, "importers") or # old py2exe
640 hasattr(sys, "importers") or # old py2exe
644 imp.is_frozen("__main__")) # tools/freeze
641 imp.is_frozen("__main__")) # tools/freeze
645
642
646 def hgexecutable():
643 def hgexecutable():
647 """return location of the 'hg' executable.
644 """return location of the 'hg' executable.
648
645
649 Defaults to $HG or 'hg' in the search path.
646 Defaults to $HG or 'hg' in the search path.
650 """
647 """
651 if _hgexecutable is None:
648 if _hgexecutable is None:
652 hg = os.environ.get('HG')
649 hg = os.environ.get('HG')
653 if hg:
650 if hg:
654 set_hgexecutable(hg)
651 set_hgexecutable(hg)
655 elif main_is_frozen():
652 elif main_is_frozen():
656 set_hgexecutable(sys.executable)
653 set_hgexecutable(sys.executable)
657 else:
654 else:
658 set_hgexecutable(find_exe('hg', 'hg'))
655 set_hgexecutable(find_exe('hg', 'hg'))
659 return _hgexecutable
656 return _hgexecutable
660
657
661 def set_hgexecutable(path):
658 def set_hgexecutable(path):
662 """set location of the 'hg' executable"""
659 """set location of the 'hg' executable"""
663 global _hgexecutable
660 global _hgexecutable
664 _hgexecutable = path
661 _hgexecutable = path
665
662
666 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
663 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
667 '''enhanced shell command execution.
664 '''enhanced shell command execution.
668 run with environment maybe modified, maybe in different dir.
665 run with environment maybe modified, maybe in different dir.
669
666
670 if command fails and onerr is None, return status. if ui object,
667 if command fails and onerr is None, return status. if ui object,
671 print error message and return status, else raise onerr object as
668 print error message and return status, else raise onerr object as
672 exception.'''
669 exception.'''
673 def py2shell(val):
670 def py2shell(val):
674 'convert python object into string that is useful to shell'
671 'convert python object into string that is useful to shell'
675 if val in (None, False):
672 if val in (None, False):
676 return '0'
673 return '0'
677 if val == True:
674 if val == True:
678 return '1'
675 return '1'
679 return str(val)
676 return str(val)
680 oldenv = {}
677 oldenv = {}
681 for k in environ:
678 for k in environ:
682 oldenv[k] = os.environ.get(k)
679 oldenv[k] = os.environ.get(k)
683 if cwd is not None:
680 if cwd is not None:
684 oldcwd = os.getcwd()
681 oldcwd = os.getcwd()
685 origcmd = cmd
682 origcmd = cmd
686 if os.name == 'nt':
683 if os.name == 'nt':
687 cmd = '"%s"' % cmd
684 cmd = '"%s"' % cmd
688 try:
685 try:
689 for k, v in environ.iteritems():
686 for k, v in environ.iteritems():
690 os.environ[k] = py2shell(v)
687 os.environ[k] = py2shell(v)
691 os.environ['HG'] = hgexecutable()
688 os.environ['HG'] = hgexecutable()
692 if cwd is not None and oldcwd != cwd:
689 if cwd is not None and oldcwd != cwd:
693 os.chdir(cwd)
690 os.chdir(cwd)
694 rc = os.system(cmd)
691 rc = os.system(cmd)
695 if sys.platform == 'OpenVMS' and rc & 1:
692 if sys.platform == 'OpenVMS' and rc & 1:
696 rc = 0
693 rc = 0
697 if rc and onerr:
694 if rc and onerr:
698 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
695 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
699 explain_exit(rc)[0])
696 explain_exit(rc)[0])
700 if errprefix:
697 if errprefix:
701 errmsg = '%s: %s' % (errprefix, errmsg)
698 errmsg = '%s: %s' % (errprefix, errmsg)
702 try:
699 try:
703 onerr.warn(errmsg + '\n')
700 onerr.warn(errmsg + '\n')
704 except AttributeError:
701 except AttributeError:
705 raise onerr(errmsg)
702 raise onerr(errmsg)
706 return rc
703 return rc
707 finally:
704 finally:
708 for k, v in oldenv.iteritems():
705 for k, v in oldenv.iteritems():
709 if v is None:
706 if v is None:
710 del os.environ[k]
707 del os.environ[k]
711 else:
708 else:
712 os.environ[k] = v
709 os.environ[k] = v
713 if cwd is not None and oldcwd != cwd:
710 if cwd is not None and oldcwd != cwd:
714 os.chdir(oldcwd)
711 os.chdir(oldcwd)
715
712
716 class SignatureError(Exception):
713 class SignatureError(Exception):
717 pass
714 pass
718
715
719 def checksignature(func):
716 def checksignature(func):
720 '''wrap a function with code to check for calling errors'''
717 '''wrap a function with code to check for calling errors'''
721 def check(*args, **kwargs):
718 def check(*args, **kwargs):
722 try:
719 try:
723 return func(*args, **kwargs)
720 return func(*args, **kwargs)
724 except TypeError:
721 except TypeError:
725 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
722 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
726 raise SignatureError
723 raise SignatureError
727 raise
724 raise
728
725
729 return check
726 return check
730
727
731 # os.path.lexists is not available on python2.3
728 # os.path.lexists is not available on python2.3
732 def lexists(filename):
729 def lexists(filename):
733 "test whether a file with this name exists. does not follow symlinks"
730 "test whether a file with this name exists. does not follow symlinks"
734 try:
731 try:
735 os.lstat(filename)
732 os.lstat(filename)
736 except:
733 except:
737 return False
734 return False
738 return True
735 return True
739
736
740 def rename(src, dst):
737 def rename(src, dst):
741 """forcibly rename a file"""
738 """forcibly rename a file"""
742 try:
739 try:
743 os.rename(src, dst)
740 os.rename(src, dst)
744 except OSError, err: # FIXME: check err (EEXIST ?)
741 except OSError, err: # FIXME: check err (EEXIST ?)
745 # on windows, rename to existing file is not allowed, so we
742 # on windows, rename to existing file is not allowed, so we
746 # must delete destination first. but if file is open, unlink
743 # must delete destination first. but if file is open, unlink
747 # schedules it for delete but does not delete it. rename
744 # schedules it for delete but does not delete it. rename
748 # happens immediately even for open files, so we create
745 # happens immediately even for open files, so we create
749 # temporary file, delete it, rename destination to that name,
746 # temporary file, delete it, rename destination to that name,
750 # then delete that. then rename is safe to do.
747 # then delete that. then rename is safe to do.
751 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
748 fd, temp = tempfile.mkstemp(dir=os.path.dirname(dst) or '.')
752 os.close(fd)
749 os.close(fd)
753 os.unlink(temp)
750 os.unlink(temp)
754 os.rename(dst, temp)
751 os.rename(dst, temp)
755 os.unlink(temp)
752 os.unlink(temp)
756 os.rename(src, dst)
753 os.rename(src, dst)
757
754
758 def unlink(f):
755 def unlink(f):
759 """unlink and remove the directory if it is empty"""
756 """unlink and remove the directory if it is empty"""
760 os.unlink(f)
757 os.unlink(f)
761 # try removing directories that might now be empty
758 # try removing directories that might now be empty
762 try:
759 try:
763 os.removedirs(os.path.dirname(f))
760 os.removedirs(os.path.dirname(f))
764 except OSError:
761 except OSError:
765 pass
762 pass
766
763
767 def copyfile(src, dest):
764 def copyfile(src, dest):
768 "copy a file, preserving mode"
765 "copy a file, preserving mode"
769 if os.path.islink(src):
766 if os.path.islink(src):
770 try:
767 try:
771 os.unlink(dest)
768 os.unlink(dest)
772 except:
769 except:
773 pass
770 pass
774 os.symlink(os.readlink(src), dest)
771 os.symlink(os.readlink(src), dest)
775 else:
772 else:
776 try:
773 try:
777 shutil.copyfile(src, dest)
774 shutil.copyfile(src, dest)
778 shutil.copymode(src, dest)
775 shutil.copymode(src, dest)
779 except shutil.Error, inst:
776 except shutil.Error, inst:
780 raise Abort(str(inst))
777 raise Abort(str(inst))
781
778
782 def copyfiles(src, dst, hardlink=None):
779 def copyfiles(src, dst, hardlink=None):
783 """Copy a directory tree using hardlinks if possible"""
780 """Copy a directory tree using hardlinks if possible"""
784
781
785 if hardlink is None:
782 if hardlink is None:
786 hardlink = (os.stat(src).st_dev ==
783 hardlink = (os.stat(src).st_dev ==
787 os.stat(os.path.dirname(dst)).st_dev)
784 os.stat(os.path.dirname(dst)).st_dev)
788
785
789 if os.path.isdir(src):
786 if os.path.isdir(src):
790 os.mkdir(dst)
787 os.mkdir(dst)
791 for name, kind in osutil.listdir(src):
788 for name, kind in osutil.listdir(src):
792 srcname = os.path.join(src, name)
789 srcname = os.path.join(src, name)
793 dstname = os.path.join(dst, name)
790 dstname = os.path.join(dst, name)
794 copyfiles(srcname, dstname, hardlink)
791 copyfiles(srcname, dstname, hardlink)
795 else:
792 else:
796 if hardlink:
793 if hardlink:
797 try:
794 try:
798 os_link(src, dst)
795 os_link(src, dst)
799 except (IOError, OSError):
796 except (IOError, OSError):
800 hardlink = False
797 hardlink = False
801 shutil.copy(src, dst)
798 shutil.copy(src, dst)
802 else:
799 else:
803 shutil.copy(src, dst)
800 shutil.copy(src, dst)
804
801
805 class path_auditor(object):
802 class path_auditor(object):
806 '''ensure that a filesystem path contains no banned components.
803 '''ensure that a filesystem path contains no banned components.
807 the following properties of a path are checked:
804 the following properties of a path are checked:
808
805
809 - under top-level .hg
806 - under top-level .hg
810 - starts at the root of a windows drive
807 - starts at the root of a windows drive
811 - contains ".."
808 - contains ".."
812 - traverses a symlink (e.g. a/symlink_here/b)
809 - traverses a symlink (e.g. a/symlink_here/b)
813 - inside a nested repository'''
810 - inside a nested repository'''
814
811
815 def __init__(self, root):
812 def __init__(self, root):
816 self.audited = set()
813 self.audited = set()
817 self.auditeddir = set()
814 self.auditeddir = set()
818 self.root = root
815 self.root = root
819
816
820 def __call__(self, path):
817 def __call__(self, path):
821 if path in self.audited:
818 if path in self.audited:
822 return
819 return
823 normpath = os.path.normcase(path)
820 normpath = os.path.normcase(path)
824 parts = splitpath(normpath)
821 parts = splitpath(normpath)
825 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '.hg.', '')
822 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '.hg.', '')
826 or os.pardir in parts):
823 or os.pardir in parts):
827 raise Abort(_("path contains illegal component: %s") % path)
824 raise Abort(_("path contains illegal component: %s") % path)
828 if '.hg' in path:
825 if '.hg' in path:
829 for p in '.hg', '.hg.':
826 for p in '.hg', '.hg.':
830 if p in parts[1:-1]:
827 if p in parts[1:-1]:
831 pos = parts.index(p)
828 pos = parts.index(p)
832 base = os.path.join(*parts[:pos])
829 base = os.path.join(*parts[:pos])
833 raise Abort(_('path %r is inside repo %r') % (path, base))
830 raise Abort(_('path %r is inside repo %r') % (path, base))
834 def check(prefix):
831 def check(prefix):
835 curpath = os.path.join(self.root, prefix)
832 curpath = os.path.join(self.root, prefix)
836 try:
833 try:
837 st = os.lstat(curpath)
834 st = os.lstat(curpath)
838 except OSError, err:
835 except OSError, err:
839 # EINVAL can be raised as invalid path syntax under win32.
836 # EINVAL can be raised as invalid path syntax under win32.
840 # They must be ignored for patterns can be checked too.
837 # They must be ignored for patterns can be checked too.
841 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
838 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
842 raise
839 raise
843 else:
840 else:
844 if stat.S_ISLNK(st.st_mode):
841 if stat.S_ISLNK(st.st_mode):
845 raise Abort(_('path %r traverses symbolic link %r') %
842 raise Abort(_('path %r traverses symbolic link %r') %
846 (path, prefix))
843 (path, prefix))
847 elif (stat.S_ISDIR(st.st_mode) and
844 elif (stat.S_ISDIR(st.st_mode) and
848 os.path.isdir(os.path.join(curpath, '.hg'))):
845 os.path.isdir(os.path.join(curpath, '.hg'))):
849 raise Abort(_('path %r is inside repo %r') %
846 raise Abort(_('path %r is inside repo %r') %
850 (path, prefix))
847 (path, prefix))
851 parts.pop()
848 parts.pop()
852 prefixes = []
849 prefixes = []
853 for n in range(len(parts)):
850 for n in range(len(parts)):
854 prefix = os.sep.join(parts)
851 prefix = os.sep.join(parts)
855 if prefix in self.auditeddir:
852 if prefix in self.auditeddir:
856 break
853 break
857 check(prefix)
854 check(prefix)
858 prefixes.append(prefix)
855 prefixes.append(prefix)
859 parts.pop()
856 parts.pop()
860
857
861 self.audited.add(path)
858 self.audited.add(path)
862 # only add prefixes to the cache after checking everything: we don't
859 # only add prefixes to the cache after checking everything: we don't
863 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
860 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
864 self.auditeddir.update(prefixes)
861 self.auditeddir.update(prefixes)
865
862
866 def _makelock_file(info, pathname):
863 def _makelock_file(info, pathname):
867 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
864 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
868 os.write(ld, info)
865 os.write(ld, info)
869 os.close(ld)
866 os.close(ld)
870
867
871 def _readlock_file(pathname):
868 def _readlock_file(pathname):
872 return posixfile(pathname).read()
869 return posixfile(pathname).read()
873
870
874 def nlinks(pathname):
871 def nlinks(pathname):
875 """Return number of hardlinks for the given file."""
872 """Return number of hardlinks for the given file."""
876 return os.lstat(pathname).st_nlink
873 return os.lstat(pathname).st_nlink
877
874
878 if hasattr(os, 'link'):
875 if hasattr(os, 'link'):
879 os_link = os.link
876 os_link = os.link
880 else:
877 else:
881 def os_link(src, dst):
878 def os_link(src, dst):
882 raise OSError(0, _("Hardlinks not supported"))
879 raise OSError(0, _("Hardlinks not supported"))
883
880
884 def fstat(fp):
881 def fstat(fp):
885 '''stat file object that may not have fileno method.'''
882 '''stat file object that may not have fileno method.'''
886 try:
883 try:
887 return os.fstat(fp.fileno())
884 return os.fstat(fp.fileno())
888 except AttributeError:
885 except AttributeError:
889 return os.stat(fp.name)
886 return os.stat(fp.name)
890
887
891 posixfile = file
888 posixfile = file
892
889
893 def openhardlinks():
890 def openhardlinks():
894 '''return true if it is safe to hold open file handles to hardlinks'''
891 '''return true if it is safe to hold open file handles to hardlinks'''
895 return True
892 return True
896
893
897 def _statfiles(files):
894 def _statfiles(files):
898 'Stat each file in files and yield stat or None if file does not exist.'
895 'Stat each file in files and yield stat or None if file does not exist.'
899 lstat = os.lstat
896 lstat = os.lstat
900 for nf in files:
897 for nf in files:
901 try:
898 try:
902 st = lstat(nf)
899 st = lstat(nf)
903 except OSError, err:
900 except OSError, err:
904 if err.errno not in (errno.ENOENT, errno.ENOTDIR):
901 if err.errno not in (errno.ENOENT, errno.ENOTDIR):
905 raise
902 raise
906 st = None
903 st = None
907 yield st
904 yield st
908
905
909 def _statfiles_clustered(files):
906 def _statfiles_clustered(files):
910 '''Stat each file in files and yield stat or None if file does not exist.
907 '''Stat each file in files and yield stat or None if file does not exist.
911 Cluster and cache stat per directory to minimize number of OS stat calls.'''
908 Cluster and cache stat per directory to minimize number of OS stat calls.'''
912 lstat = os.lstat
909 lstat = os.lstat
913 ncase = os.path.normcase
910 ncase = os.path.normcase
914 sep = os.sep
911 sep = os.sep
915 dircache = {} # dirname -> filename -> status | None if file does not exist
912 dircache = {} # dirname -> filename -> status | None if file does not exist
916 for nf in files:
913 for nf in files:
917 nf = ncase(nf)
914 nf = ncase(nf)
918 pos = nf.rfind(sep)
915 pos = nf.rfind(sep)
919 if pos == -1:
916 if pos == -1:
920 dir, base = '.', nf
917 dir, base = '.', nf
921 else:
918 else:
922 dir, base = nf[:pos+1], nf[pos+1:]
919 dir, base = nf[:pos+1], nf[pos+1:]
923 cache = dircache.get(dir, None)
920 cache = dircache.get(dir, None)
924 if cache is None:
921 if cache is None:
925 try:
922 try:
926 dmap = dict([(ncase(n), s)
923 dmap = dict([(ncase(n), s)
927 for n, k, s in osutil.listdir(dir, True)])
924 for n, k, s in osutil.listdir(dir, True)])
928 except OSError, err:
925 except OSError, err:
929 # handle directory not found in Python version prior to 2.5
926 # handle directory not found in Python version prior to 2.5
930 # Python <= 2.4 returns native Windows code 3 in errno
927 # Python <= 2.4 returns native Windows code 3 in errno
931 # Python >= 2.5 returns ENOENT and adds winerror field
928 # Python >= 2.5 returns ENOENT and adds winerror field
932 # EINVAL is raised if dir is not a directory.
929 # EINVAL is raised if dir is not a directory.
933 if err.errno not in (3, errno.ENOENT, errno.EINVAL,
930 if err.errno not in (3, errno.ENOENT, errno.EINVAL,
934 errno.ENOTDIR):
931 errno.ENOTDIR):
935 raise
932 raise
936 dmap = {}
933 dmap = {}
937 cache = dircache.setdefault(dir, dmap)
934 cache = dircache.setdefault(dir, dmap)
938 yield cache.get(base, None)
935 yield cache.get(base, None)
939
936
940 if sys.platform == 'win32':
937 if sys.platform == 'win32':
941 statfiles = _statfiles_clustered
938 statfiles = _statfiles_clustered
942 else:
939 else:
943 statfiles = _statfiles
940 statfiles = _statfiles
944
941
945 getuser_fallback = None
942 getuser_fallback = None
946
943
947 def getuser():
944 def getuser():
948 '''return name of current user'''
945 '''return name of current user'''
949 try:
946 try:
950 return getpass.getuser()
947 return getpass.getuser()
951 except ImportError:
948 except ImportError:
952 # import of pwd will fail on windows - try fallback
949 # import of pwd will fail on windows - try fallback
953 if getuser_fallback:
950 if getuser_fallback:
954 return getuser_fallback()
951 return getuser_fallback()
955 # raised if win32api not available
952 # raised if win32api not available
956 raise Abort(_('user name not available - set USERNAME '
953 raise Abort(_('user name not available - set USERNAME '
957 'environment variable'))
954 'environment variable'))
958
955
959 def username(uid=None):
956 def username(uid=None):
960 """Return the name of the user with the given uid.
957 """Return the name of the user with the given uid.
961
958
962 If uid is None, return the name of the current user."""
959 If uid is None, return the name of the current user."""
963 try:
960 try:
964 import pwd
961 import pwd
965 if uid is None:
962 if uid is None:
966 uid = os.getuid()
963 uid = os.getuid()
967 try:
964 try:
968 return pwd.getpwuid(uid)[0]
965 return pwd.getpwuid(uid)[0]
969 except KeyError:
966 except KeyError:
970 return str(uid)
967 return str(uid)
971 except ImportError:
968 except ImportError:
972 return None
969 return None
973
970
974 def groupname(gid=None):
971 def groupname(gid=None):
975 """Return the name of the group with the given gid.
972 """Return the name of the group with the given gid.
976
973
977 If gid is None, return the name of the current group."""
974 If gid is None, return the name of the current group."""
978 try:
975 try:
979 import grp
976 import grp
980 if gid is None:
977 if gid is None:
981 gid = os.getgid()
978 gid = os.getgid()
982 try:
979 try:
983 return grp.getgrgid(gid)[0]
980 return grp.getgrgid(gid)[0]
984 except KeyError:
981 except KeyError:
985 return str(gid)
982 return str(gid)
986 except ImportError:
983 except ImportError:
987 return None
984 return None
988
985
989 # File system features
986 # File system features
990
987
991 def checkcase(path):
988 def checkcase(path):
992 """
989 """
993 Check whether the given path is on a case-sensitive filesystem
990 Check whether the given path is on a case-sensitive filesystem
994
991
995 Requires a path (like /foo/.hg) ending with a foldable final
992 Requires a path (like /foo/.hg) ending with a foldable final
996 directory component.
993 directory component.
997 """
994 """
998 s1 = os.stat(path)
995 s1 = os.stat(path)
999 d, b = os.path.split(path)
996 d, b = os.path.split(path)
1000 p2 = os.path.join(d, b.upper())
997 p2 = os.path.join(d, b.upper())
1001 if path == p2:
998 if path == p2:
1002 p2 = os.path.join(d, b.lower())
999 p2 = os.path.join(d, b.lower())
1003 try:
1000 try:
1004 s2 = os.stat(p2)
1001 s2 = os.stat(p2)
1005 if s2 == s1:
1002 if s2 == s1:
1006 return False
1003 return False
1007 return True
1004 return True
1008 except:
1005 except:
1009 return True
1006 return True
1010
1007
1011 _fspathcache = {}
1008 _fspathcache = {}
1012 def fspath(name, root):
1009 def fspath(name, root):
1013 '''Get name in the case stored in the filesystem
1010 '''Get name in the case stored in the filesystem
1014
1011
1015 The name is either relative to root, or it is an absolute path starting
1012 The name is either relative to root, or it is an absolute path starting
1016 with root. Note that this function is unnecessary, and should not be
1013 with root. Note that this function is unnecessary, and should not be
1017 called, for case-sensitive filesystems (simply because it's expensive).
1014 called, for case-sensitive filesystems (simply because it's expensive).
1018 '''
1015 '''
1019 # If name is absolute, make it relative
1016 # If name is absolute, make it relative
1020 if name.lower().startswith(root.lower()):
1017 if name.lower().startswith(root.lower()):
1021 l = len(root)
1018 l = len(root)
1022 if name[l] == os.sep or name[l] == os.altsep:
1019 if name[l] == os.sep or name[l] == os.altsep:
1023 l = l + 1
1020 l = l + 1
1024 name = name[l:]
1021 name = name[l:]
1025
1022
1026 if not os.path.exists(os.path.join(root, name)):
1023 if not os.path.exists(os.path.join(root, name)):
1027 return None
1024 return None
1028
1025
1029 seps = os.sep
1026 seps = os.sep
1030 if os.altsep:
1027 if os.altsep:
1031 seps = seps + os.altsep
1028 seps = seps + os.altsep
1032 # Protect backslashes. This gets silly very quickly.
1029 # Protect backslashes. This gets silly very quickly.
1033 seps.replace('\\','\\\\')
1030 seps.replace('\\','\\\\')
1034 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1031 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1035 dir = os.path.normcase(os.path.normpath(root))
1032 dir = os.path.normcase(os.path.normpath(root))
1036 result = []
1033 result = []
1037 for part, sep in pattern.findall(name):
1034 for part, sep in pattern.findall(name):
1038 if sep:
1035 if sep:
1039 result.append(sep)
1036 result.append(sep)
1040 continue
1037 continue
1041
1038
1042 if dir not in _fspathcache:
1039 if dir not in _fspathcache:
1043 _fspathcache[dir] = os.listdir(dir)
1040 _fspathcache[dir] = os.listdir(dir)
1044 contents = _fspathcache[dir]
1041 contents = _fspathcache[dir]
1045
1042
1046 lpart = part.lower()
1043 lpart = part.lower()
1047 for n in contents:
1044 for n in contents:
1048 if n.lower() == lpart:
1045 if n.lower() == lpart:
1049 result.append(n)
1046 result.append(n)
1050 break
1047 break
1051 else:
1048 else:
1052 # Cannot happen, as the file exists!
1049 # Cannot happen, as the file exists!
1053 result.append(part)
1050 result.append(part)
1054 dir = os.path.join(dir, lpart)
1051 dir = os.path.join(dir, lpart)
1055
1052
1056 return ''.join(result)
1053 return ''.join(result)
1057
1054
1058 def checkexec(path):
1055 def checkexec(path):
1059 """
1056 """
1060 Check whether the given path is on a filesystem with UNIX-like exec flags
1057 Check whether the given path is on a filesystem with UNIX-like exec flags
1061
1058
1062 Requires a directory (like /foo/.hg)
1059 Requires a directory (like /foo/.hg)
1063 """
1060 """
1064
1061
1065 # VFAT on some Linux versions can flip mode but it doesn't persist
1062 # VFAT on some Linux versions can flip mode but it doesn't persist
1066 # a FS remount. Frequently we can detect it if files are created
1063 # a FS remount. Frequently we can detect it if files are created
1067 # with exec bit on.
1064 # with exec bit on.
1068
1065
1069 try:
1066 try:
1070 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
1067 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
1071 fh, fn = tempfile.mkstemp("", "", path)
1068 fh, fn = tempfile.mkstemp("", "", path)
1072 try:
1069 try:
1073 os.close(fh)
1070 os.close(fh)
1074 m = os.stat(fn).st_mode & 0777
1071 m = os.stat(fn).st_mode & 0777
1075 new_file_has_exec = m & EXECFLAGS
1072 new_file_has_exec = m & EXECFLAGS
1076 os.chmod(fn, m ^ EXECFLAGS)
1073 os.chmod(fn, m ^ EXECFLAGS)
1077 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
1074 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
1078 finally:
1075 finally:
1079 os.unlink(fn)
1076 os.unlink(fn)
1080 except (IOError, OSError):
1077 except (IOError, OSError):
1081 # we don't care, the user probably won't be able to commit anyway
1078 # we don't care, the user probably won't be able to commit anyway
1082 return False
1079 return False
1083 return not (new_file_has_exec or exec_flags_cannot_flip)
1080 return not (new_file_has_exec or exec_flags_cannot_flip)
1084
1081
1085 def checklink(path):
1082 def checklink(path):
1086 """check whether the given path is on a symlink-capable filesystem"""
1083 """check whether the given path is on a symlink-capable filesystem"""
1087 # mktemp is not racy because symlink creation will fail if the
1084 # mktemp is not racy because symlink creation will fail if the
1088 # file already exists
1085 # file already exists
1089 name = tempfile.mktemp(dir=path)
1086 name = tempfile.mktemp(dir=path)
1090 try:
1087 try:
1091 os.symlink(".", name)
1088 os.symlink(".", name)
1092 os.unlink(name)
1089 os.unlink(name)
1093 return True
1090 return True
1094 except (OSError, AttributeError):
1091 except (OSError, AttributeError):
1095 return False
1092 return False
1096
1093
1097 _umask = os.umask(0)
1094 _umask = os.umask(0)
1098 os.umask(_umask)
1095 os.umask(_umask)
1099
1096
1100 def needbinarypatch():
1097 def needbinarypatch():
1101 """return True if patches should be applied in binary mode by default."""
1098 """return True if patches should be applied in binary mode by default."""
1102 return os.name == 'nt'
1099 return os.name == 'nt'
1103
1100
1104 def endswithsep(path):
1101 def endswithsep(path):
1105 '''Check path ends with os.sep or os.altsep.'''
1102 '''Check path ends with os.sep or os.altsep.'''
1106 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
1103 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
1107
1104
1108 def splitpath(path):
1105 def splitpath(path):
1109 '''Split path by os.sep.
1106 '''Split path by os.sep.
1110 Note that this function does not use os.altsep because this is
1107 Note that this function does not use os.altsep because this is
1111 an alternative of simple "xxx.split(os.sep)".
1108 an alternative of simple "xxx.split(os.sep)".
1112 It is recommended to use os.path.normpath() before using this
1109 It is recommended to use os.path.normpath() before using this
1113 function if need.'''
1110 function if need.'''
1114 return path.split(os.sep)
1111 return path.split(os.sep)
1115
1112
1116 def gui():
1113 def gui():
1117 '''Are we running in a GUI?'''
1114 '''Are we running in a GUI?'''
1118 return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY")
1115 return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY")
1119
1116
1120 def lookup_reg(key, name=None, scope=None):
1117 def lookup_reg(key, name=None, scope=None):
1121 return None
1118 return None
1122
1119
1123 # Platform specific variants
1120 # Platform specific variants
1124 if os.name == 'nt':
1121 if os.name == 'nt':
1125 import msvcrt
1122 import msvcrt
1126 nulldev = 'NUL:'
1123 nulldev = 'NUL:'
1127
1124
1128 class winstdout:
1125 class winstdout:
1129 '''stdout on windows misbehaves if sent through a pipe'''
1126 '''stdout on windows misbehaves if sent through a pipe'''
1130
1127
1131 def __init__(self, fp):
1128 def __init__(self, fp):
1132 self.fp = fp
1129 self.fp = fp
1133
1130
1134 def __getattr__(self, key):
1131 def __getattr__(self, key):
1135 return getattr(self.fp, key)
1132 return getattr(self.fp, key)
1136
1133
1137 def close(self):
1134 def close(self):
1138 try:
1135 try:
1139 self.fp.close()
1136 self.fp.close()
1140 except: pass
1137 except: pass
1141
1138
1142 def write(self, s):
1139 def write(self, s):
1143 try:
1140 try:
1144 # This is workaround for "Not enough space" error on
1141 # This is workaround for "Not enough space" error on
1145 # writing large size of data to console.
1142 # writing large size of data to console.
1146 limit = 16000
1143 limit = 16000
1147 l = len(s)
1144 l = len(s)
1148 start = 0
1145 start = 0
1149 while start < l:
1146 while start < l:
1150 end = start + limit
1147 end = start + limit
1151 self.fp.write(s[start:end])
1148 self.fp.write(s[start:end])
1152 start = end
1149 start = end
1153 except IOError, inst:
1150 except IOError, inst:
1154 if inst.errno != 0: raise
1151 if inst.errno != 0: raise
1155 self.close()
1152 self.close()
1156 raise IOError(errno.EPIPE, 'Broken pipe')
1153 raise IOError(errno.EPIPE, 'Broken pipe')
1157
1154
1158 def flush(self):
1155 def flush(self):
1159 try:
1156 try:
1160 return self.fp.flush()
1157 return self.fp.flush()
1161 except IOError, inst:
1158 except IOError, inst:
1162 if inst.errno != errno.EINVAL: raise
1159 if inst.errno != errno.EINVAL: raise
1163 self.close()
1160 self.close()
1164 raise IOError(errno.EPIPE, 'Broken pipe')
1161 raise IOError(errno.EPIPE, 'Broken pipe')
1165
1162
1166 sys.stdout = winstdout(sys.stdout)
1163 sys.stdout = winstdout(sys.stdout)
1167
1164
1168 def _is_win_9x():
1165 def _is_win_9x():
1169 '''return true if run on windows 95, 98 or me.'''
1166 '''return true if run on windows 95, 98 or me.'''
1170 try:
1167 try:
1171 return sys.getwindowsversion()[3] == 1
1168 return sys.getwindowsversion()[3] == 1
1172 except AttributeError:
1169 except AttributeError:
1173 return 'command' in os.environ.get('comspec', '')
1170 return 'command' in os.environ.get('comspec', '')
1174
1171
1175 def openhardlinks():
1172 def openhardlinks():
1176 return not _is_win_9x and "win32api" in locals()
1173 return not _is_win_9x and "win32api" in locals()
1177
1174
1178 def system_rcpath():
1175 def system_rcpath():
1179 try:
1176 try:
1180 return system_rcpath_win32()
1177 return system_rcpath_win32()
1181 except:
1178 except:
1182 return [r'c:\mercurial\mercurial.ini']
1179 return [r'c:\mercurial\mercurial.ini']
1183
1180
1184 def user_rcpath():
1181 def user_rcpath():
1185 '''return os-specific hgrc search path to the user dir'''
1182 '''return os-specific hgrc search path to the user dir'''
1186 try:
1183 try:
1187 path = user_rcpath_win32()
1184 path = user_rcpath_win32()
1188 except:
1185 except:
1189 home = os.path.expanduser('~')
1186 home = os.path.expanduser('~')
1190 path = [os.path.join(home, 'mercurial.ini'),
1187 path = [os.path.join(home, 'mercurial.ini'),
1191 os.path.join(home, '.hgrc')]
1188 os.path.join(home, '.hgrc')]
1192 userprofile = os.environ.get('USERPROFILE')
1189 userprofile = os.environ.get('USERPROFILE')
1193 if userprofile:
1190 if userprofile:
1194 path.append(os.path.join(userprofile, 'mercurial.ini'))
1191 path.append(os.path.join(userprofile, 'mercurial.ini'))
1195 path.append(os.path.join(userprofile, '.hgrc'))
1192 path.append(os.path.join(userprofile, '.hgrc'))
1196 return path
1193 return path
1197
1194
1198 def parse_patch_output(output_line):
1195 def parse_patch_output(output_line):
1199 """parses the output produced by patch and returns the file name"""
1196 """parses the output produced by patch and returns the file name"""
1200 pf = output_line[14:]
1197 pf = output_line[14:]
1201 if pf[0] == '`':
1198 if pf[0] == '`':
1202 pf = pf[1:-1] # Remove the quotes
1199 pf = pf[1:-1] # Remove the quotes
1203 return pf
1200 return pf
1204
1201
1205 def sshargs(sshcmd, host, user, port):
1202 def sshargs(sshcmd, host, user, port):
1206 '''Build argument list for ssh or Plink'''
1203 '''Build argument list for ssh or Plink'''
1207 pflag = 'plink' in sshcmd.lower() and '-P' or '-p'
1204 pflag = 'plink' in sshcmd.lower() and '-P' or '-p'
1208 args = user and ("%s@%s" % (user, host)) or host
1205 args = user and ("%s@%s" % (user, host)) or host
1209 return port and ("%s %s %s" % (args, pflag, port)) or args
1206 return port and ("%s %s %s" % (args, pflag, port)) or args
1210
1207
1211 def testpid(pid):
1208 def testpid(pid):
1212 '''return False if pid dead, True if running or not known'''
1209 '''return False if pid dead, True if running or not known'''
1213 return True
1210 return True
1214
1211
1215 def set_flags(f, l, x):
1212 def set_flags(f, l, x):
1216 pass
1213 pass
1217
1214
1218 def set_binary(fd):
1215 def set_binary(fd):
1219 # When run without console, pipes may expose invalid
1216 # When run without console, pipes may expose invalid
1220 # fileno(), usually set to -1.
1217 # fileno(), usually set to -1.
1221 if hasattr(fd, 'fileno') and fd.fileno() >= 0:
1218 if hasattr(fd, 'fileno') and fd.fileno() >= 0:
1222 msvcrt.setmode(fd.fileno(), os.O_BINARY)
1219 msvcrt.setmode(fd.fileno(), os.O_BINARY)
1223
1220
1224 def pconvert(path):
1221 def pconvert(path):
1225 return '/'.join(splitpath(path))
1222 return '/'.join(splitpath(path))
1226
1223
1227 def localpath(path):
1224 def localpath(path):
1228 return path.replace('/', '\\')
1225 return path.replace('/', '\\')
1229
1226
1230 def normpath(path):
1227 def normpath(path):
1231 return pconvert(os.path.normpath(path))
1228 return pconvert(os.path.normpath(path))
1232
1229
1233 makelock = _makelock_file
1230 makelock = _makelock_file
1234 readlock = _readlock_file
1231 readlock = _readlock_file
1235
1232
1236 def samestat(s1, s2):
1233 def samestat(s1, s2):
1237 return False
1234 return False
1238
1235
1239 # A sequence of backslashes is special iff it precedes a double quote:
1236 # A sequence of backslashes is special iff it precedes a double quote:
1240 # - if there's an even number of backslashes, the double quote is not
1237 # - if there's an even number of backslashes, the double quote is not
1241 # quoted (i.e. it ends the quoted region)
1238 # quoted (i.e. it ends the quoted region)
1242 # - if there's an odd number of backslashes, the double quote is quoted
1239 # - if there's an odd number of backslashes, the double quote is quoted
1243 # - in both cases, every pair of backslashes is unquoted into a single
1240 # - in both cases, every pair of backslashes is unquoted into a single
1244 # backslash
1241 # backslash
1245 # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx )
1242 # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx )
1246 # So, to quote a string, we must surround it in double quotes, double
1243 # So, to quote a string, we must surround it in double quotes, double
1247 # the number of backslashes that preceed double quotes and add another
1244 # the number of backslashes that preceed double quotes and add another
1248 # backslash before every double quote (being careful with the double
1245 # backslash before every double quote (being careful with the double
1249 # quote we've appended to the end)
1246 # quote we've appended to the end)
1250 _quotere = None
1247 _quotere = None
1251 def shellquote(s):
1248 def shellquote(s):
1252 global _quotere
1249 global _quotere
1253 if _quotere is None:
1250 if _quotere is None:
1254 _quotere = re.compile(r'(\\*)("|\\$)')
1251 _quotere = re.compile(r'(\\*)("|\\$)')
1255 return '"%s"' % _quotere.sub(r'\1\1\\\2', s)
1252 return '"%s"' % _quotere.sub(r'\1\1\\\2', s)
1256
1253
1257 def quotecommand(cmd):
1254 def quotecommand(cmd):
1258 """Build a command string suitable for os.popen* calls."""
1255 """Build a command string suitable for os.popen* calls."""
1259 # The extra quotes are needed because popen* runs the command
1256 # The extra quotes are needed because popen* runs the command
1260 # through the current COMSPEC. cmd.exe suppress enclosing quotes.
1257 # through the current COMSPEC. cmd.exe suppress enclosing quotes.
1261 return '"' + cmd + '"'
1258 return '"' + cmd + '"'
1262
1259
1263 def popen(command, mode='r'):
1260 def popen(command, mode='r'):
1264 # Work around "popen spawned process may not write to stdout
1261 # Work around "popen spawned process may not write to stdout
1265 # under windows"
1262 # under windows"
1266 # http://bugs.python.org/issue1366
1263 # http://bugs.python.org/issue1366
1267 command += " 2> %s" % nulldev
1264 command += " 2> %s" % nulldev
1268 return os.popen(quotecommand(command), mode)
1265 return os.popen(quotecommand(command), mode)
1269
1266
1270 def explain_exit(code):
1267 def explain_exit(code):
1271 return _("exited with status %d") % code, code
1268 return _("exited with status %d") % code, code
1272
1269
1273 # if you change this stub into a real check, please try to implement the
1270 # if you change this stub into a real check, please try to implement the
1274 # username and groupname functions above, too.
1271 # username and groupname functions above, too.
1275 def isowner(fp, st=None):
1272 def isowner(fp, st=None):
1276 return True
1273 return True
1277
1274
1278 def find_in_path(name, path, default=None):
1275 def find_in_path(name, path, default=None):
1279 '''find name in search path. path can be string (will be split
1276 '''find name in search path. path can be string (will be split
1280 with os.pathsep), or iterable thing that returns strings. if name
1277 with os.pathsep), or iterable thing that returns strings. if name
1281 found, return path to name. else return default. name is looked up
1278 found, return path to name. else return default. name is looked up
1282 using cmd.exe rules, using PATHEXT.'''
1279 using cmd.exe rules, using PATHEXT.'''
1283 if isinstance(path, str):
1280 if isinstance(path, str):
1284 path = path.split(os.pathsep)
1281 path = path.split(os.pathsep)
1285
1282
1286 pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD')
1283 pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD')
1287 pathext = pathext.lower().split(os.pathsep)
1284 pathext = pathext.lower().split(os.pathsep)
1288 isexec = os.path.splitext(name)[1].lower() in pathext
1285 isexec = os.path.splitext(name)[1].lower() in pathext
1289
1286
1290 for p in path:
1287 for p in path:
1291 p_name = os.path.join(p, name)
1288 p_name = os.path.join(p, name)
1292
1289
1293 if isexec and os.path.exists(p_name):
1290 if isexec and os.path.exists(p_name):
1294 return p_name
1291 return p_name
1295
1292
1296 for ext in pathext:
1293 for ext in pathext:
1297 p_name_ext = p_name + ext
1294 p_name_ext = p_name + ext
1298 if os.path.exists(p_name_ext):
1295 if os.path.exists(p_name_ext):
1299 return p_name_ext
1296 return p_name_ext
1300 return default
1297 return default
1301
1298
1302 def set_signal_handler():
1299 def set_signal_handler():
1303 try:
1300 try:
1304 set_signal_handler_win32()
1301 set_signal_handler_win32()
1305 except NameError:
1302 except NameError:
1306 pass
1303 pass
1307
1304
1308 try:
1305 try:
1309 # override functions with win32 versions if possible
1306 # override functions with win32 versions if possible
1310 from util_win32 import *
1307 from util_win32 import *
1311 if not _is_win_9x():
1308 if not _is_win_9x():
1312 posixfile = posixfile_nt
1309 posixfile = posixfile_nt
1313 except ImportError:
1310 except ImportError:
1314 pass
1311 pass
1315
1312
1316 else:
1313 else:
1317 nulldev = '/dev/null'
1314 nulldev = '/dev/null'
1318
1315
1319 def rcfiles(path):
1316 def rcfiles(path):
1320 rcs = [os.path.join(path, 'hgrc')]
1317 rcs = [os.path.join(path, 'hgrc')]
1321 rcdir = os.path.join(path, 'hgrc.d')
1318 rcdir = os.path.join(path, 'hgrc.d')
1322 try:
1319 try:
1323 rcs.extend([os.path.join(rcdir, f)
1320 rcs.extend([os.path.join(rcdir, f)
1324 for f, kind in osutil.listdir(rcdir)
1321 for f, kind in osutil.listdir(rcdir)
1325 if f.endswith(".rc")])
1322 if f.endswith(".rc")])
1326 except OSError:
1323 except OSError:
1327 pass
1324 pass
1328 return rcs
1325 return rcs
1329
1326
1330 def system_rcpath():
1327 def system_rcpath():
1331 path = []
1328 path = []
1332 # old mod_python does not set sys.argv
1329 # old mod_python does not set sys.argv
1333 if len(getattr(sys, 'argv', [])) > 0:
1330 if len(getattr(sys, 'argv', [])) > 0:
1334 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
1331 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
1335 '/../etc/mercurial'))
1332 '/../etc/mercurial'))
1336 path.extend(rcfiles('/etc/mercurial'))
1333 path.extend(rcfiles('/etc/mercurial'))
1337 return path
1334 return path
1338
1335
1339 def user_rcpath():
1336 def user_rcpath():
1340 return [os.path.expanduser('~/.hgrc')]
1337 return [os.path.expanduser('~/.hgrc')]
1341
1338
1342 def parse_patch_output(output_line):
1339 def parse_patch_output(output_line):
1343 """parses the output produced by patch and returns the file name"""
1340 """parses the output produced by patch and returns the file name"""
1344 pf = output_line[14:]
1341 pf = output_line[14:]
1345 if os.sys.platform == 'OpenVMS':
1342 if os.sys.platform == 'OpenVMS':
1346 if pf[0] == '`':
1343 if pf[0] == '`':
1347 pf = pf[1:-1] # Remove the quotes
1344 pf = pf[1:-1] # Remove the quotes
1348 else:
1345 else:
1349 if pf.startswith("'") and pf.endswith("'") and " " in pf:
1346 if pf.startswith("'") and pf.endswith("'") and " " in pf:
1350 pf = pf[1:-1] # Remove the quotes
1347 pf = pf[1:-1] # Remove the quotes
1351 return pf
1348 return pf
1352
1349
1353 def sshargs(sshcmd, host, user, port):
1350 def sshargs(sshcmd, host, user, port):
1354 '''Build argument list for ssh'''
1351 '''Build argument list for ssh'''
1355 args = user and ("%s@%s" % (user, host)) or host
1352 args = user and ("%s@%s" % (user, host)) or host
1356 return port and ("%s -p %s" % (args, port)) or args
1353 return port and ("%s -p %s" % (args, port)) or args
1357
1354
1358 def is_exec(f):
1355 def is_exec(f):
1359 """check whether a file is executable"""
1356 """check whether a file is executable"""
1360 return (os.lstat(f).st_mode & 0100 != 0)
1357 return (os.lstat(f).st_mode & 0100 != 0)
1361
1358
1362 def set_flags(f, l, x):
1359 def set_flags(f, l, x):
1363 s = os.lstat(f).st_mode
1360 s = os.lstat(f).st_mode
1364 if l:
1361 if l:
1365 if not stat.S_ISLNK(s):
1362 if not stat.S_ISLNK(s):
1366 # switch file to link
1363 # switch file to link
1367 data = file(f).read()
1364 data = file(f).read()
1368 os.unlink(f)
1365 os.unlink(f)
1369 try:
1366 try:
1370 os.symlink(data, f)
1367 os.symlink(data, f)
1371 except:
1368 except:
1372 # failed to make a link, rewrite file
1369 # failed to make a link, rewrite file
1373 file(f, "w").write(data)
1370 file(f, "w").write(data)
1374 # no chmod needed at this point
1371 # no chmod needed at this point
1375 return
1372 return
1376 if stat.S_ISLNK(s):
1373 if stat.S_ISLNK(s):
1377 # switch link to file
1374 # switch link to file
1378 data = os.readlink(f)
1375 data = os.readlink(f)
1379 os.unlink(f)
1376 os.unlink(f)
1380 file(f, "w").write(data)
1377 file(f, "w").write(data)
1381 s = 0666 & ~_umask # avoid restatting for chmod
1378 s = 0666 & ~_umask # avoid restatting for chmod
1382
1379
1383 sx = s & 0100
1380 sx = s & 0100
1384 if x and not sx:
1381 if x and not sx:
1385 # Turn on +x for every +r bit when making a file executable
1382 # Turn on +x for every +r bit when making a file executable
1386 # and obey umask.
1383 # and obey umask.
1387 os.chmod(f, s | (s & 0444) >> 2 & ~_umask)
1384 os.chmod(f, s | (s & 0444) >> 2 & ~_umask)
1388 elif not x and sx:
1385 elif not x and sx:
1389 # Turn off all +x bits
1386 # Turn off all +x bits
1390 os.chmod(f, s & 0666)
1387 os.chmod(f, s & 0666)
1391
1388
1392 def set_binary(fd):
1389 def set_binary(fd):
1393 pass
1390 pass
1394
1391
1395 def pconvert(path):
1392 def pconvert(path):
1396 return path
1393 return path
1397
1394
1398 def localpath(path):
1395 def localpath(path):
1399 return path
1396 return path
1400
1397
1401 normpath = os.path.normpath
1398 normpath = os.path.normpath
1402 samestat = os.path.samestat
1399 samestat = os.path.samestat
1403
1400
1404 def makelock(info, pathname):
1401 def makelock(info, pathname):
1405 try:
1402 try:
1406 os.symlink(info, pathname)
1403 os.symlink(info, pathname)
1407 except OSError, why:
1404 except OSError, why:
1408 if why.errno == errno.EEXIST:
1405 if why.errno == errno.EEXIST:
1409 raise
1406 raise
1410 else:
1407 else:
1411 _makelock_file(info, pathname)
1408 _makelock_file(info, pathname)
1412
1409
1413 def readlock(pathname):
1410 def readlock(pathname):
1414 try:
1411 try:
1415 return os.readlink(pathname)
1412 return os.readlink(pathname)
1416 except OSError, why:
1413 except OSError, why:
1417 if why.errno in (errno.EINVAL, errno.ENOSYS):
1414 if why.errno in (errno.EINVAL, errno.ENOSYS):
1418 return _readlock_file(pathname)
1415 return _readlock_file(pathname)
1419 else:
1416 else:
1420 raise
1417 raise
1421
1418
1422 def shellquote(s):
1419 def shellquote(s):
1423 if os.sys.platform == 'OpenVMS':
1420 if os.sys.platform == 'OpenVMS':
1424 return '"%s"' % s
1421 return '"%s"' % s
1425 else:
1422 else:
1426 return "'%s'" % s.replace("'", "'\\''")
1423 return "'%s'" % s.replace("'", "'\\''")
1427
1424
1428 def quotecommand(cmd):
1425 def quotecommand(cmd):
1429 return cmd
1426 return cmd
1430
1427
1431 def popen(command, mode='r'):
1428 def popen(command, mode='r'):
1432 return os.popen(command, mode)
1429 return os.popen(command, mode)
1433
1430
1434 def testpid(pid):
1431 def testpid(pid):
1435 '''return False if pid dead, True if running or not sure'''
1432 '''return False if pid dead, True if running or not sure'''
1436 if os.sys.platform == 'OpenVMS':
1433 if os.sys.platform == 'OpenVMS':
1437 return True
1434 return True
1438 try:
1435 try:
1439 os.kill(pid, 0)
1436 os.kill(pid, 0)
1440 return True
1437 return True
1441 except OSError, inst:
1438 except OSError, inst:
1442 return inst.errno != errno.ESRCH
1439 return inst.errno != errno.ESRCH
1443
1440
1444 def explain_exit(code):
1441 def explain_exit(code):
1445 """return a 2-tuple (desc, code) describing a process's status"""
1442 """return a 2-tuple (desc, code) describing a process's status"""
1446 if os.WIFEXITED(code):
1443 if os.WIFEXITED(code):
1447 val = os.WEXITSTATUS(code)
1444 val = os.WEXITSTATUS(code)
1448 return _("exited with status %d") % val, val
1445 return _("exited with status %d") % val, val
1449 elif os.WIFSIGNALED(code):
1446 elif os.WIFSIGNALED(code):
1450 val = os.WTERMSIG(code)
1447 val = os.WTERMSIG(code)
1451 return _("killed by signal %d") % val, val
1448 return _("killed by signal %d") % val, val
1452 elif os.WIFSTOPPED(code):
1449 elif os.WIFSTOPPED(code):
1453 val = os.WSTOPSIG(code)
1450 val = os.WSTOPSIG(code)
1454 return _("stopped by signal %d") % val, val
1451 return _("stopped by signal %d") % val, val
1455 raise ValueError(_("invalid exit code"))
1452 raise ValueError(_("invalid exit code"))
1456
1453
1457 def isowner(fp, st=None):
1454 def isowner(fp, st=None):
1458 """Return True if the file object f belongs to the current user.
1455 """Return True if the file object f belongs to the current user.
1459
1456
1460 The return value of a util.fstat(f) may be passed as the st argument.
1457 The return value of a util.fstat(f) may be passed as the st argument.
1461 """
1458 """
1462 if st is None:
1459 if st is None:
1463 st = fstat(fp)
1460 st = fstat(fp)
1464 return st.st_uid == os.getuid()
1461 return st.st_uid == os.getuid()
1465
1462
1466 def find_in_path(name, path, default=None):
1463 def find_in_path(name, path, default=None):
1467 '''find name in search path. path can be string (will be split
1464 '''find name in search path. path can be string (will be split
1468 with os.pathsep), or iterable thing that returns strings. if name
1465 with os.pathsep), or iterable thing that returns strings. if name
1469 found, return path to name. else return default.'''
1466 found, return path to name. else return default.'''
1470 if isinstance(path, str):
1467 if isinstance(path, str):
1471 path = path.split(os.pathsep)
1468 path = path.split(os.pathsep)
1472 for p in path:
1469 for p in path:
1473 p_name = os.path.join(p, name)
1470 p_name = os.path.join(p, name)
1474 if os.path.exists(p_name):
1471 if os.path.exists(p_name):
1475 return p_name
1472 return p_name
1476 return default
1473 return default
1477
1474
1478 def set_signal_handler():
1475 def set_signal_handler():
1479 pass
1476 pass
1480
1477
1481 def find_exe(name, default=None):
1478 def find_exe(name, default=None):
1482 '''find path of an executable.
1479 '''find path of an executable.
1483 if name contains a path component, return it as is. otherwise,
1480 if name contains a path component, return it as is. otherwise,
1484 use normal executable search path.'''
1481 use normal executable search path.'''
1485
1482
1486 if os.sep in name or sys.platform == 'OpenVMS':
1483 if os.sep in name or sys.platform == 'OpenVMS':
1487 # don't check the executable bit. if the file isn't
1484 # don't check the executable bit. if the file isn't
1488 # executable, whoever tries to actually run it will give a
1485 # executable, whoever tries to actually run it will give a
1489 # much more useful error message.
1486 # much more useful error message.
1490 return name
1487 return name
1491 return find_in_path(name, os.environ.get('PATH', ''), default=default)
1488 return find_in_path(name, os.environ.get('PATH', ''), default=default)
1492
1489
1493 def mktempcopy(name, emptyok=False, createmode=None):
1490 def mktempcopy(name, emptyok=False, createmode=None):
1494 """Create a temporary file with the same contents from name
1491 """Create a temporary file with the same contents from name
1495
1492
1496 The permission bits are copied from the original file.
1493 The permission bits are copied from the original file.
1497
1494
1498 If the temporary file is going to be truncated immediately, you
1495 If the temporary file is going to be truncated immediately, you
1499 can use emptyok=True as an optimization.
1496 can use emptyok=True as an optimization.
1500
1497
1501 Returns the name of the temporary file.
1498 Returns the name of the temporary file.
1502 """
1499 """
1503 d, fn = os.path.split(name)
1500 d, fn = os.path.split(name)
1504 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1501 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1505 os.close(fd)
1502 os.close(fd)
1506 # Temporary files are created with mode 0600, which is usually not
1503 # Temporary files are created with mode 0600, which is usually not
1507 # what we want. If the original file already exists, just copy
1504 # what we want. If the original file already exists, just copy
1508 # its mode. Otherwise, manually obey umask.
1505 # its mode. Otherwise, manually obey umask.
1509 try:
1506 try:
1510 st_mode = os.lstat(name).st_mode & 0777
1507 st_mode = os.lstat(name).st_mode & 0777
1511 except OSError, inst:
1508 except OSError, inst:
1512 if inst.errno != errno.ENOENT:
1509 if inst.errno != errno.ENOENT:
1513 raise
1510 raise
1514 st_mode = createmode
1511 st_mode = createmode
1515 if st_mode is None:
1512 if st_mode is None:
1516 st_mode = ~_umask
1513 st_mode = ~_umask
1517 st_mode &= 0666
1514 st_mode &= 0666
1518 os.chmod(temp, st_mode)
1515 os.chmod(temp, st_mode)
1519 if emptyok:
1516 if emptyok:
1520 return temp
1517 return temp
1521 try:
1518 try:
1522 try:
1519 try:
1523 ifp = posixfile(name, "rb")
1520 ifp = posixfile(name, "rb")
1524 except IOError, inst:
1521 except IOError, inst:
1525 if inst.errno == errno.ENOENT:
1522 if inst.errno == errno.ENOENT:
1526 return temp
1523 return temp
1527 if not getattr(inst, 'filename', None):
1524 if not getattr(inst, 'filename', None):
1528 inst.filename = name
1525 inst.filename = name
1529 raise
1526 raise
1530 ofp = posixfile(temp, "wb")
1527 ofp = posixfile(temp, "wb")
1531 for chunk in filechunkiter(ifp):
1528 for chunk in filechunkiter(ifp):
1532 ofp.write(chunk)
1529 ofp.write(chunk)
1533 ifp.close()
1530 ifp.close()
1534 ofp.close()
1531 ofp.close()
1535 except:
1532 except:
1536 try: os.unlink(temp)
1533 try: os.unlink(temp)
1537 except: pass
1534 except: pass
1538 raise
1535 raise
1539 return temp
1536 return temp
1540
1537
1541 class atomictempfile(posixfile):
1538 class atomictempfile(posixfile):
1542 """file-like object that atomically updates a file
1539 """file-like object that atomically updates a file
1543
1540
1544 All writes will be redirected to a temporary copy of the original
1541 All writes will be redirected to a temporary copy of the original
1545 file. When rename is called, the copy is renamed to the original
1542 file. When rename is called, the copy is renamed to the original
1546 name, making the changes visible.
1543 name, making the changes visible.
1547 """
1544 """
1548 def __init__(self, name, mode, createmode):
1545 def __init__(self, name, mode, createmode):
1549 self.__name = name
1546 self.__name = name
1550 self.temp = mktempcopy(name, emptyok=('w' in mode),
1547 self.temp = mktempcopy(name, emptyok=('w' in mode),
1551 createmode=createmode)
1548 createmode=createmode)
1552 posixfile.__init__(self, self.temp, mode)
1549 posixfile.__init__(self, self.temp, mode)
1553
1550
1554 def rename(self):
1551 def rename(self):
1555 if not self.closed:
1552 if not self.closed:
1556 posixfile.close(self)
1553 posixfile.close(self)
1557 rename(self.temp, localpath(self.__name))
1554 rename(self.temp, localpath(self.__name))
1558
1555
1559 def __del__(self):
1556 def __del__(self):
1560 if not self.closed:
1557 if not self.closed:
1561 try:
1558 try:
1562 os.unlink(self.temp)
1559 os.unlink(self.temp)
1563 except: pass
1560 except: pass
1564 posixfile.close(self)
1561 posixfile.close(self)
1565
1562
1566 def makedirs(name, mode=None):
1563 def makedirs(name, mode=None):
1567 """recursive directory creation with parent mode inheritance"""
1564 """recursive directory creation with parent mode inheritance"""
1568 try:
1565 try:
1569 os.mkdir(name)
1566 os.mkdir(name)
1570 if mode is not None:
1567 if mode is not None:
1571 os.chmod(name, mode)
1568 os.chmod(name, mode)
1572 return
1569 return
1573 except OSError, err:
1570 except OSError, err:
1574 if err.errno == errno.EEXIST:
1571 if err.errno == errno.EEXIST:
1575 return
1572 return
1576 if err.errno != errno.ENOENT:
1573 if err.errno != errno.ENOENT:
1577 raise
1574 raise
1578 parent = os.path.abspath(os.path.dirname(name))
1575 parent = os.path.abspath(os.path.dirname(name))
1579 makedirs(parent, mode)
1576 makedirs(parent, mode)
1580 makedirs(name, mode)
1577 makedirs(name, mode)
1581
1578
1582 class opener(object):
1579 class opener(object):
1583 """Open files relative to a base directory
1580 """Open files relative to a base directory
1584
1581
1585 This class is used to hide the details of COW semantics and
1582 This class is used to hide the details of COW semantics and
1586 remote file access from higher level code.
1583 remote file access from higher level code.
1587 """
1584 """
1588 def __init__(self, base, audit=True):
1585 def __init__(self, base, audit=True):
1589 self.base = base
1586 self.base = base
1590 if audit:
1587 if audit:
1591 self.audit_path = path_auditor(base)
1588 self.audit_path = path_auditor(base)
1592 else:
1589 else:
1593 self.audit_path = always
1590 self.audit_path = always
1594 self.createmode = None
1591 self.createmode = None
1595
1592
1596 def __getattr__(self, name):
1593 def __getattr__(self, name):
1597 if name == '_can_symlink':
1594 if name == '_can_symlink':
1598 self._can_symlink = checklink(self.base)
1595 self._can_symlink = checklink(self.base)
1599 return self._can_symlink
1596 return self._can_symlink
1600 raise AttributeError(name)
1597 raise AttributeError(name)
1601
1598
1602 def _fixfilemode(self, name):
1599 def _fixfilemode(self, name):
1603 if self.createmode is None:
1600 if self.createmode is None:
1604 return
1601 return
1605 os.chmod(name, self.createmode & 0666)
1602 os.chmod(name, self.createmode & 0666)
1606
1603
1607 def __call__(self, path, mode="r", text=False, atomictemp=False):
1604 def __call__(self, path, mode="r", text=False, atomictemp=False):
1608 self.audit_path(path)
1605 self.audit_path(path)
1609 f = os.path.join(self.base, path)
1606 f = os.path.join(self.base, path)
1610
1607
1611 if not text and "b" not in mode:
1608 if not text and "b" not in mode:
1612 mode += "b" # for that other OS
1609 mode += "b" # for that other OS
1613
1610
1614 nlink = -1
1611 nlink = -1
1615 if mode not in ("r", "rb"):
1612 if mode not in ("r", "rb"):
1616 try:
1613 try:
1617 nlink = nlinks(f)
1614 nlink = nlinks(f)
1618 except OSError:
1615 except OSError:
1619 nlink = 0
1616 nlink = 0
1620 d = os.path.dirname(f)
1617 d = os.path.dirname(f)
1621 if not os.path.isdir(d):
1618 if not os.path.isdir(d):
1622 makedirs(d, self.createmode)
1619 makedirs(d, self.createmode)
1623 if atomictemp:
1620 if atomictemp:
1624 return atomictempfile(f, mode, self.createmode)
1621 return atomictempfile(f, mode, self.createmode)
1625 if nlink > 1:
1622 if nlink > 1:
1626 rename(mktempcopy(f), f)
1623 rename(mktempcopy(f), f)
1627 fp = posixfile(f, mode)
1624 fp = posixfile(f, mode)
1628 if nlink == 0:
1625 if nlink == 0:
1629 self._fixfilemode(f)
1626 self._fixfilemode(f)
1630 return fp
1627 return fp
1631
1628
1632 def symlink(self, src, dst):
1629 def symlink(self, src, dst):
1633 self.audit_path(dst)
1630 self.audit_path(dst)
1634 linkname = os.path.join(self.base, dst)
1631 linkname = os.path.join(self.base, dst)
1635 try:
1632 try:
1636 os.unlink(linkname)
1633 os.unlink(linkname)
1637 except OSError:
1634 except OSError:
1638 pass
1635 pass
1639
1636
1640 dirname = os.path.dirname(linkname)
1637 dirname = os.path.dirname(linkname)
1641 if not os.path.exists(dirname):
1638 if not os.path.exists(dirname):
1642 makedirs(dirname, self.createmode)
1639 makedirs(dirname, self.createmode)
1643
1640
1644 if self._can_symlink:
1641 if self._can_symlink:
1645 try:
1642 try:
1646 os.symlink(src, linkname)
1643 os.symlink(src, linkname)
1647 except OSError, err:
1644 except OSError, err:
1648 raise OSError(err.errno, _('could not symlink to %r: %s') %
1645 raise OSError(err.errno, _('could not symlink to %r: %s') %
1649 (src, err.strerror), linkname)
1646 (src, err.strerror), linkname)
1650 else:
1647 else:
1651 f = self(dst, "w")
1648 f = self(dst, "w")
1652 f.write(src)
1649 f.write(src)
1653 f.close()
1650 f.close()
1654 self._fixfilemode(dst)
1651 self._fixfilemode(dst)
1655
1652
1656 class chunkbuffer(object):
1653 class chunkbuffer(object):
1657 """Allow arbitrary sized chunks of data to be efficiently read from an
1654 """Allow arbitrary sized chunks of data to be efficiently read from an
1658 iterator over chunks of arbitrary size."""
1655 iterator over chunks of arbitrary size."""
1659
1656
1660 def __init__(self, in_iter):
1657 def __init__(self, in_iter):
1661 """in_iter is the iterator that's iterating over the input chunks.
1658 """in_iter is the iterator that's iterating over the input chunks.
1662 targetsize is how big a buffer to try to maintain."""
1659 targetsize is how big a buffer to try to maintain."""
1663 self.iter = iter(in_iter)
1660 self.iter = iter(in_iter)
1664 self.buf = ''
1661 self.buf = ''
1665 self.targetsize = 2**16
1662 self.targetsize = 2**16
1666
1663
1667 def read(self, l):
1664 def read(self, l):
1668 """Read L bytes of data from the iterator of chunks of data.
1665 """Read L bytes of data from the iterator of chunks of data.
1669 Returns less than L bytes if the iterator runs dry."""
1666 Returns less than L bytes if the iterator runs dry."""
1670 if l > len(self.buf) and self.iter:
1667 if l > len(self.buf) and self.iter:
1671 # Clamp to a multiple of self.targetsize
1668 # Clamp to a multiple of self.targetsize
1672 targetsize = max(l, self.targetsize)
1669 targetsize = max(l, self.targetsize)
1673 collector = cStringIO.StringIO()
1670 collector = cStringIO.StringIO()
1674 collector.write(self.buf)
1671 collector.write(self.buf)
1675 collected = len(self.buf)
1672 collected = len(self.buf)
1676 for chunk in self.iter:
1673 for chunk in self.iter:
1677 collector.write(chunk)
1674 collector.write(chunk)
1678 collected += len(chunk)
1675 collected += len(chunk)
1679 if collected >= targetsize:
1676 if collected >= targetsize:
1680 break
1677 break
1681 if collected < targetsize:
1678 if collected < targetsize:
1682 self.iter = False
1679 self.iter = False
1683 self.buf = collector.getvalue()
1680 self.buf = collector.getvalue()
1684 if len(self.buf) == l:
1681 if len(self.buf) == l:
1685 s, self.buf = str(self.buf), ''
1682 s, self.buf = str(self.buf), ''
1686 else:
1683 else:
1687 s, self.buf = self.buf[:l], buffer(self.buf, l)
1684 s, self.buf = self.buf[:l], buffer(self.buf, l)
1688 return s
1685 return s
1689
1686
1690 def filechunkiter(f, size=65536, limit=None):
1687 def filechunkiter(f, size=65536, limit=None):
1691 """Create a generator that produces the data in the file size
1688 """Create a generator that produces the data in the file size
1692 (default 65536) bytes at a time, up to optional limit (default is
1689 (default 65536) bytes at a time, up to optional limit (default is
1693 to read all data). Chunks may be less than size bytes if the
1690 to read all data). Chunks may be less than size bytes if the
1694 chunk is the last chunk in the file, or the file is a socket or
1691 chunk is the last chunk in the file, or the file is a socket or
1695 some other type of file that sometimes reads less data than is
1692 some other type of file that sometimes reads less data than is
1696 requested."""
1693 requested."""
1697 assert size >= 0
1694 assert size >= 0
1698 assert limit is None or limit >= 0
1695 assert limit is None or limit >= 0
1699 while True:
1696 while True:
1700 if limit is None: nbytes = size
1697 if limit is None: nbytes = size
1701 else: nbytes = min(limit, size)
1698 else: nbytes = min(limit, size)
1702 s = nbytes and f.read(nbytes)
1699 s = nbytes and f.read(nbytes)
1703 if not s: break
1700 if not s: break
1704 if limit: limit -= len(s)
1701 if limit: limit -= len(s)
1705 yield s
1702 yield s
1706
1703
1707 def makedate():
1704 def makedate():
1708 lt = time.localtime()
1705 lt = time.localtime()
1709 if lt[8] == 1 and time.daylight:
1706 if lt[8] == 1 and time.daylight:
1710 tz = time.altzone
1707 tz = time.altzone
1711 else:
1708 else:
1712 tz = time.timezone
1709 tz = time.timezone
1713 return time.mktime(lt), tz
1710 return time.mktime(lt), tz
1714
1711
1715 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1712 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1716 """represent a (unixtime, offset) tuple as a localized time.
1713 """represent a (unixtime, offset) tuple as a localized time.
1717 unixtime is seconds since the epoch, and offset is the time zone's
1714 unixtime is seconds since the epoch, and offset is the time zone's
1718 number of seconds away from UTC. if timezone is false, do not
1715 number of seconds away from UTC. if timezone is false, do not
1719 append time zone to string."""
1716 append time zone to string."""
1720 t, tz = date or makedate()
1717 t, tz = date or makedate()
1721 if "%1" in format or "%2" in format:
1718 if "%1" in format or "%2" in format:
1722 sign = (tz > 0) and "-" or "+"
1719 sign = (tz > 0) and "-" or "+"
1723 minutes = abs(tz) / 60
1720 minutes = abs(tz) / 60
1724 format = format.replace("%1", "%c%02d" % (sign, minutes / 60))
1721 format = format.replace("%1", "%c%02d" % (sign, minutes / 60))
1725 format = format.replace("%2", "%02d" % (minutes % 60))
1722 format = format.replace("%2", "%02d" % (minutes % 60))
1726 s = time.strftime(format, time.gmtime(float(t) - tz))
1723 s = time.strftime(format, time.gmtime(float(t) - tz))
1727 return s
1724 return s
1728
1725
1729 def shortdate(date=None):
1726 def shortdate(date=None):
1730 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1727 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1731 return datestr(date, format='%Y-%m-%d')
1728 return datestr(date, format='%Y-%m-%d')
1732
1729
1733 def strdate(string, format, defaults=[]):
1730 def strdate(string, format, defaults=[]):
1734 """parse a localized time string and return a (unixtime, offset) tuple.
1731 """parse a localized time string and return a (unixtime, offset) tuple.
1735 if the string cannot be parsed, ValueError is raised."""
1732 if the string cannot be parsed, ValueError is raised."""
1736 def timezone(string):
1733 def timezone(string):
1737 tz = string.split()[-1]
1734 tz = string.split()[-1]
1738 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1735 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1739 sign = (tz[0] == "+") and 1 or -1
1736 sign = (tz[0] == "+") and 1 or -1
1740 hours = int(tz[1:3])
1737 hours = int(tz[1:3])
1741 minutes = int(tz[3:5])
1738 minutes = int(tz[3:5])
1742 return -sign * (hours * 60 + minutes) * 60
1739 return -sign * (hours * 60 + minutes) * 60
1743 if tz == "GMT" or tz == "UTC":
1740 if tz == "GMT" or tz == "UTC":
1744 return 0
1741 return 0
1745 return None
1742 return None
1746
1743
1747 # NOTE: unixtime = localunixtime + offset
1744 # NOTE: unixtime = localunixtime + offset
1748 offset, date = timezone(string), string
1745 offset, date = timezone(string), string
1749 if offset != None:
1746 if offset != None:
1750 date = " ".join(string.split()[:-1])
1747 date = " ".join(string.split()[:-1])
1751
1748
1752 # add missing elements from defaults
1749 # add missing elements from defaults
1753 for part in defaults:
1750 for part in defaults:
1754 found = [True for p in part if ("%"+p) in format]
1751 found = [True for p in part if ("%"+p) in format]
1755 if not found:
1752 if not found:
1756 date += "@" + defaults[part]
1753 date += "@" + defaults[part]
1757 format += "@%" + part[0]
1754 format += "@%" + part[0]
1758
1755
1759 timetuple = time.strptime(date, format)
1756 timetuple = time.strptime(date, format)
1760 localunixtime = int(calendar.timegm(timetuple))
1757 localunixtime = int(calendar.timegm(timetuple))
1761 if offset is None:
1758 if offset is None:
1762 # local timezone
1759 # local timezone
1763 unixtime = int(time.mktime(timetuple))
1760 unixtime = int(time.mktime(timetuple))
1764 offset = unixtime - localunixtime
1761 offset = unixtime - localunixtime
1765 else:
1762 else:
1766 unixtime = localunixtime + offset
1763 unixtime = localunixtime + offset
1767 return unixtime, offset
1764 return unixtime, offset
1768
1765
1769 def parsedate(date, formats=None, defaults=None):
1766 def parsedate(date, formats=None, defaults=None):
1770 """parse a localized date/time string and return a (unixtime, offset) tuple.
1767 """parse a localized date/time string and return a (unixtime, offset) tuple.
1771
1768
1772 The date may be a "unixtime offset" string or in one of the specified
1769 The date may be a "unixtime offset" string or in one of the specified
1773 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1770 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1774 """
1771 """
1775 if not date:
1772 if not date:
1776 return 0, 0
1773 return 0, 0
1777 if isinstance(date, tuple) and len(date) == 2:
1774 if isinstance(date, tuple) and len(date) == 2:
1778 return date
1775 return date
1779 if not formats:
1776 if not formats:
1780 formats = defaultdateformats
1777 formats = defaultdateformats
1781 date = date.strip()
1778 date = date.strip()
1782 try:
1779 try:
1783 when, offset = map(int, date.split(' '))
1780 when, offset = map(int, date.split(' '))
1784 except ValueError:
1781 except ValueError:
1785 # fill out defaults
1782 # fill out defaults
1786 if not defaults:
1783 if not defaults:
1787 defaults = {}
1784 defaults = {}
1788 now = makedate()
1785 now = makedate()
1789 for part in "d mb yY HI M S".split():
1786 for part in "d mb yY HI M S".split():
1790 if part not in defaults:
1787 if part not in defaults:
1791 if part[0] in "HMS":
1788 if part[0] in "HMS":
1792 defaults[part] = "00"
1789 defaults[part] = "00"
1793 else:
1790 else:
1794 defaults[part] = datestr(now, "%" + part[0])
1791 defaults[part] = datestr(now, "%" + part[0])
1795
1792
1796 for format in formats:
1793 for format in formats:
1797 try:
1794 try:
1798 when, offset = strdate(date, format, defaults)
1795 when, offset = strdate(date, format, defaults)
1799 except (ValueError, OverflowError):
1796 except (ValueError, OverflowError):
1800 pass
1797 pass
1801 else:
1798 else:
1802 break
1799 break
1803 else:
1800 else:
1804 raise Abort(_('invalid date: %r ') % date)
1801 raise Abort(_('invalid date: %r ') % date)
1805 # validate explicit (probably user-specified) date and
1802 # validate explicit (probably user-specified) date and
1806 # time zone offset. values must fit in signed 32 bits for
1803 # time zone offset. values must fit in signed 32 bits for
1807 # current 32-bit linux runtimes. timezones go from UTC-12
1804 # current 32-bit linux runtimes. timezones go from UTC-12
1808 # to UTC+14
1805 # to UTC+14
1809 if abs(when) > 0x7fffffff:
1806 if abs(when) > 0x7fffffff:
1810 raise Abort(_('date exceeds 32 bits: %d') % when)
1807 raise Abort(_('date exceeds 32 bits: %d') % when)
1811 if offset < -50400 or offset > 43200:
1808 if offset < -50400 or offset > 43200:
1812 raise Abort(_('impossible time zone offset: %d') % offset)
1809 raise Abort(_('impossible time zone offset: %d') % offset)
1813 return when, offset
1810 return when, offset
1814
1811
1815 def matchdate(date):
1812 def matchdate(date):
1816 """Return a function that matches a given date match specifier
1813 """Return a function that matches a given date match specifier
1817
1814
1818 Formats include:
1815 Formats include:
1819
1816
1820 '{date}' match a given date to the accuracy provided
1817 '{date}' match a given date to the accuracy provided
1821
1818
1822 '<{date}' on or before a given date
1819 '<{date}' on or before a given date
1823
1820
1824 '>{date}' on or after a given date
1821 '>{date}' on or after a given date
1825
1822
1826 """
1823 """
1827
1824
1828 def lower(date):
1825 def lower(date):
1829 d = dict(mb="1", d="1")
1826 d = dict(mb="1", d="1")
1830 return parsedate(date, extendeddateformats, d)[0]
1827 return parsedate(date, extendeddateformats, d)[0]
1831
1828
1832 def upper(date):
1829 def upper(date):
1833 d = dict(mb="12", HI="23", M="59", S="59")
1830 d = dict(mb="12", HI="23", M="59", S="59")
1834 for days in "31 30 29".split():
1831 for days in "31 30 29".split():
1835 try:
1832 try:
1836 d["d"] = days
1833 d["d"] = days
1837 return parsedate(date, extendeddateformats, d)[0]
1834 return parsedate(date, extendeddateformats, d)[0]
1838 except:
1835 except:
1839 pass
1836 pass
1840 d["d"] = "28"
1837 d["d"] = "28"
1841 return parsedate(date, extendeddateformats, d)[0]
1838 return parsedate(date, extendeddateformats, d)[0]
1842
1839
1843 if date[0] == "<":
1840 if date[0] == "<":
1844 when = upper(date[1:])
1841 when = upper(date[1:])
1845 return lambda x: x <= when
1842 return lambda x: x <= when
1846 elif date[0] == ">":
1843 elif date[0] == ">":
1847 when = lower(date[1:])
1844 when = lower(date[1:])
1848 return lambda x: x >= when
1845 return lambda x: x >= when
1849 elif date[0] == "-":
1846 elif date[0] == "-":
1850 try:
1847 try:
1851 days = int(date[1:])
1848 days = int(date[1:])
1852 except ValueError:
1849 except ValueError:
1853 raise Abort(_("invalid day spec: %s") % date[1:])
1850 raise Abort(_("invalid day spec: %s") % date[1:])
1854 when = makedate()[0] - days * 3600 * 24
1851 when = makedate()[0] - days * 3600 * 24
1855 return lambda x: x >= when
1852 return lambda x: x >= when
1856 elif " to " in date:
1853 elif " to " in date:
1857 a, b = date.split(" to ")
1854 a, b = date.split(" to ")
1858 start, stop = lower(a), upper(b)
1855 start, stop = lower(a), upper(b)
1859 return lambda x: x >= start and x <= stop
1856 return lambda x: x >= start and x <= stop
1860 else:
1857 else:
1861 start, stop = lower(date), upper(date)
1858 start, stop = lower(date), upper(date)
1862 return lambda x: x >= start and x <= stop
1859 return lambda x: x >= start and x <= stop
1863
1860
1864 def shortuser(user):
1861 def shortuser(user):
1865 """Return a short representation of a user name or email address."""
1862 """Return a short representation of a user name or email address."""
1866 f = user.find('@')
1863 f = user.find('@')
1867 if f >= 0:
1864 if f >= 0:
1868 user = user[:f]
1865 user = user[:f]
1869 f = user.find('<')
1866 f = user.find('<')
1870 if f >= 0:
1867 if f >= 0:
1871 user = user[f+1:]
1868 user = user[f+1:]
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 f = user.find('.')
1872 f = user.find('.')
1876 if f >= 0:
1873 if f >= 0:
1877 user = user[:f]
1874 user = user[:f]
1878 return user
1875 return user
1879
1876
1880 def email(author):
1877 def email(author):
1881 '''get email of author.'''
1878 '''get email of author.'''
1882 r = author.find('>')
1879 r = author.find('>')
1883 if r == -1: r = None
1880 if r == -1: r = None
1884 return author[author.find('<')+1:r]
1881 return author[author.find('<')+1:r]
1885
1882
1886 def ellipsis(text, maxlength=400):
1883 def ellipsis(text, maxlength=400):
1887 """Trim string to at most maxlength (default: 400) characters."""
1884 """Trim string to at most maxlength (default: 400) characters."""
1888 if len(text) <= maxlength:
1885 if len(text) <= maxlength:
1889 return text
1886 return text
1890 else:
1887 else:
1891 return "%s..." % (text[:maxlength-3])
1888 return "%s..." % (text[:maxlength-3])
1892
1889
1893 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1890 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1894 '''yield every hg repository under path, recursively.'''
1891 '''yield every hg repository under path, recursively.'''
1895 def errhandler(err):
1892 def errhandler(err):
1896 if err.filename == path:
1893 if err.filename == path:
1897 raise err
1894 raise err
1898 if followsym and hasattr(os.path, 'samestat'):
1895 if followsym and hasattr(os.path, 'samestat'):
1899 def _add_dir_if_not_there(dirlst, dirname):
1896 def _add_dir_if_not_there(dirlst, dirname):
1900 match = False
1897 match = False
1901 samestat = os.path.samestat
1898 samestat = os.path.samestat
1902 dirstat = os.stat(dirname)
1899 dirstat = os.stat(dirname)
1903 for lstdirstat in dirlst:
1900 for lstdirstat in dirlst:
1904 if samestat(dirstat, lstdirstat):
1901 if samestat(dirstat, lstdirstat):
1905 match = True
1902 match = True
1906 break
1903 break
1907 if not match:
1904 if not match:
1908 dirlst.append(dirstat)
1905 dirlst.append(dirstat)
1909 return not match
1906 return not match
1910 else:
1907 else:
1911 followsym = False
1908 followsym = False
1912
1909
1913 if (seen_dirs is None) and followsym:
1910 if (seen_dirs is None) and followsym:
1914 seen_dirs = []
1911 seen_dirs = []
1915 _add_dir_if_not_there(seen_dirs, path)
1912 _add_dir_if_not_there(seen_dirs, path)
1916 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
1913 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
1917 if '.hg' in dirs:
1914 if '.hg' in dirs:
1918 yield root # found a repository
1915 yield root # found a repository
1919 qroot = os.path.join(root, '.hg', 'patches')
1916 qroot = os.path.join(root, '.hg', 'patches')
1920 if os.path.isdir(os.path.join(qroot, '.hg')):
1917 if os.path.isdir(os.path.join(qroot, '.hg')):
1921 yield qroot # we have a patch queue repo here
1918 yield qroot # we have a patch queue repo here
1922 if recurse:
1919 if recurse:
1923 # avoid recursing inside the .hg directory
1920 # avoid recursing inside the .hg directory
1924 dirs.remove('.hg')
1921 dirs.remove('.hg')
1925 else:
1922 else:
1926 dirs[:] = [] # don't descend further
1923 dirs[:] = [] # don't descend further
1927 elif followsym:
1924 elif followsym:
1928 newdirs = []
1925 newdirs = []
1929 for d in dirs:
1926 for d in dirs:
1930 fname = os.path.join(root, d)
1927 fname = os.path.join(root, d)
1931 if _add_dir_if_not_there(seen_dirs, fname):
1928 if _add_dir_if_not_there(seen_dirs, fname):
1932 if os.path.islink(fname):
1929 if os.path.islink(fname):
1933 for hgname in walkrepos(fname, True, seen_dirs):
1930 for hgname in walkrepos(fname, True, seen_dirs):
1934 yield hgname
1931 yield hgname
1935 else:
1932 else:
1936 newdirs.append(d)
1933 newdirs.append(d)
1937 dirs[:] = newdirs
1934 dirs[:] = newdirs
1938
1935
1939 _rcpath = None
1936 _rcpath = None
1940
1937
1941 def os_rcpath():
1938 def os_rcpath():
1942 '''return default os-specific hgrc search path'''
1939 '''return default os-specific hgrc search path'''
1943 path = system_rcpath()
1940 path = system_rcpath()
1944 path.extend(user_rcpath())
1941 path.extend(user_rcpath())
1945 path = [os.path.normpath(f) for f in path]
1942 path = [os.path.normpath(f) for f in path]
1946 return path
1943 return path
1947
1944
1948 def rcpath():
1945 def rcpath():
1949 '''return hgrc search path. if env var HGRCPATH is set, use it.
1946 '''return hgrc search path. if env var HGRCPATH is set, use it.
1950 for each item in path, if directory, use files ending in .rc,
1947 for each item in path, if directory, use files ending in .rc,
1951 else use item.
1948 else use item.
1952 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1949 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1953 if no HGRCPATH, use default os-specific path.'''
1950 if no HGRCPATH, use default os-specific path.'''
1954 global _rcpath
1951 global _rcpath
1955 if _rcpath is None:
1952 if _rcpath is None:
1956 if 'HGRCPATH' in os.environ:
1953 if 'HGRCPATH' in os.environ:
1957 _rcpath = []
1954 _rcpath = []
1958 for p in os.environ['HGRCPATH'].split(os.pathsep):
1955 for p in os.environ['HGRCPATH'].split(os.pathsep):
1959 if not p: continue
1956 if not p: continue
1960 if os.path.isdir(p):
1957 if os.path.isdir(p):
1961 for f, kind in osutil.listdir(p):
1958 for f, kind in osutil.listdir(p):
1962 if f.endswith('.rc'):
1959 if f.endswith('.rc'):
1963 _rcpath.append(os.path.join(p, f))
1960 _rcpath.append(os.path.join(p, f))
1964 else:
1961 else:
1965 _rcpath.append(p)
1962 _rcpath.append(p)
1966 else:
1963 else:
1967 _rcpath = os_rcpath()
1964 _rcpath = os_rcpath()
1968 return _rcpath
1965 return _rcpath
1969
1966
1970 def bytecount(nbytes):
1967 def bytecount(nbytes):
1971 '''return byte count formatted as readable string, with units'''
1968 '''return byte count formatted as readable string, with units'''
1972
1969
1973 units = (
1970 units = (
1974 (100, 1<<30, _('%.0f GB')),
1971 (100, 1<<30, _('%.0f GB')),
1975 (10, 1<<30, _('%.1f GB')),
1972 (10, 1<<30, _('%.1f GB')),
1976 (1, 1<<30, _('%.2f GB')),
1973 (1, 1<<30, _('%.2f GB')),
1977 (100, 1<<20, _('%.0f MB')),
1974 (100, 1<<20, _('%.0f MB')),
1978 (10, 1<<20, _('%.1f MB')),
1975 (10, 1<<20, _('%.1f MB')),
1979 (1, 1<<20, _('%.2f MB')),
1976 (1, 1<<20, _('%.2f MB')),
1980 (100, 1<<10, _('%.0f KB')),
1977 (100, 1<<10, _('%.0f KB')),
1981 (10, 1<<10, _('%.1f KB')),
1978 (10, 1<<10, _('%.1f KB')),
1982 (1, 1<<10, _('%.2f KB')),
1979 (1, 1<<10, _('%.2f KB')),
1983 (1, 1, _('%.0f bytes')),
1980 (1, 1, _('%.0f bytes')),
1984 )
1981 )
1985
1982
1986 for multiplier, divisor, format in units:
1983 for multiplier, divisor, format in units:
1987 if nbytes >= divisor * multiplier:
1984 if nbytes >= divisor * multiplier:
1988 return format % (nbytes / float(divisor))
1985 return format % (nbytes / float(divisor))
1989 return units[-1][2] % nbytes
1986 return units[-1][2] % nbytes
1990
1987
1991 def drop_scheme(scheme, path):
1988 def drop_scheme(scheme, path):
1992 sc = scheme + ':'
1989 sc = scheme + ':'
1993 if path.startswith(sc):
1990 if path.startswith(sc):
1994 path = path[len(sc):]
1991 path = path[len(sc):]
1995 if path.startswith('//'):
1992 if path.startswith('//'):
1996 path = path[2:]
1993 path = path[2:]
1997 return path
1994 return path
1998
1995
1999 def uirepr(s):
1996 def uirepr(s):
2000 # Avoid double backslash in Windows path repr()
1997 # Avoid double backslash in Windows path repr()
2001 return repr(s).replace('\\\\', '\\')
1998 return repr(s).replace('\\\\', '\\')
2002
1999
2003 def termwidth():
2000 def termwidth():
2004 if 'COLUMNS' in os.environ:
2001 if 'COLUMNS' in os.environ:
2005 try:
2002 try:
2006 return int(os.environ['COLUMNS'])
2003 return int(os.environ['COLUMNS'])
2007 except ValueError:
2004 except ValueError:
2008 pass
2005 pass
2009 try:
2006 try:
2010 import termios, array, fcntl
2007 import termios, array, fcntl
2011 for dev in (sys.stdout, sys.stdin):
2008 for dev in (sys.stdout, sys.stdin):
2012 try:
2009 try:
2013 fd = dev.fileno()
2010 fd = dev.fileno()
2014 if not os.isatty(fd):
2011 if not os.isatty(fd):
2015 continue
2012 continue
2016 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
2013 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
2017 return array.array('h', arri)[1]
2014 return array.array('h', arri)[1]
2018 except ValueError:
2015 except ValueError:
2019 pass
2016 pass
2020 except ImportError:
2017 except ImportError:
2021 pass
2018 pass
2022 return 80
2019 return 80
General Comments 0
You need to be logged in to leave comments. Login now