##// END OF EJS Templates
move SignalInterrupt class into util module.
Vadim Gelfer -
r2153:635653cd default
parent child Browse files
Show More
@@ -1,3519 +1,3516
1 # commands.py - command processing for mercurial
1 # commands.py - command processing for mercurial
2 #
2 #
3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005 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 demandload import demandload
8 from demandload import demandload
9 from node import *
9 from node import *
10 from i18n import gettext as _
10 from i18n import gettext as _
11 demandload(globals(), "os re sys signal shutil imp urllib pdb")
11 demandload(globals(), "os re sys signal shutil imp urllib pdb")
12 demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo")
12 demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo")
13 demandload(globals(), "fnmatch hgweb mdiff random signal tempfile time")
13 demandload(globals(), "fnmatch hgweb mdiff random signal tempfile time")
14 demandload(globals(), "traceback errno socket version struct atexit sets bz2")
14 demandload(globals(), "traceback errno socket version struct atexit sets bz2")
15 demandload(globals(), "archival changegroup")
15 demandload(globals(), "archival changegroup")
16
16
17 class UnknownCommand(Exception):
17 class UnknownCommand(Exception):
18 """Exception raised if command is not in the command table."""
18 """Exception raised if command is not in the command table."""
19 class AmbiguousCommand(Exception):
19 class AmbiguousCommand(Exception):
20 """Exception raised if command shortcut matches more than one command."""
20 """Exception raised if command shortcut matches more than one command."""
21
21
22 def filterfiles(filters, files):
22 def filterfiles(filters, files):
23 l = [x for x in files if x in filters]
23 l = [x for x in files if x in filters]
24
24
25 for t in filters:
25 for t in filters:
26 if t and t[-1] != "/":
26 if t and t[-1] != "/":
27 t += "/"
27 t += "/"
28 l += [x for x in files if x.startswith(t)]
28 l += [x for x in files if x.startswith(t)]
29 return l
29 return l
30
30
31 def relpath(repo, args):
31 def relpath(repo, args):
32 cwd = repo.getcwd()
32 cwd = repo.getcwd()
33 if cwd:
33 if cwd:
34 return [util.normpath(os.path.join(cwd, x)) for x in args]
34 return [util.normpath(os.path.join(cwd, x)) for x in args]
35 return args
35 return args
36
36
37 def matchpats(repo, pats=[], opts={}, head=''):
37 def matchpats(repo, pats=[], opts={}, head=''):
38 cwd = repo.getcwd()
38 cwd = repo.getcwd()
39 if not pats and cwd:
39 if not pats and cwd:
40 opts['include'] = [os.path.join(cwd, i) for i in opts['include']]
40 opts['include'] = [os.path.join(cwd, i) for i in opts['include']]
41 opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']]
41 opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']]
42 cwd = ''
42 cwd = ''
43 return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'),
43 return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'),
44 opts.get('exclude'), head)
44 opts.get('exclude'), head)
45
45
46 def makewalk(repo, pats, opts, node=None, head='', badmatch=None):
46 def makewalk(repo, pats, opts, node=None, head='', badmatch=None):
47 files, matchfn, anypats = matchpats(repo, pats, opts, head)
47 files, matchfn, anypats = matchpats(repo, pats, opts, head)
48 exact = dict(zip(files, files))
48 exact = dict(zip(files, files))
49 def walk():
49 def walk():
50 for src, fn in repo.walk(node=node, files=files, match=matchfn,
50 for src, fn in repo.walk(node=node, files=files, match=matchfn,
51 badmatch=badmatch):
51 badmatch=badmatch):
52 yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact
52 yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact
53 return files, matchfn, walk()
53 return files, matchfn, walk()
54
54
55 def walk(repo, pats, opts, node=None, head='', badmatch=None):
55 def walk(repo, pats, opts, node=None, head='', badmatch=None):
56 files, matchfn, results = makewalk(repo, pats, opts, node, head, badmatch)
56 files, matchfn, results = makewalk(repo, pats, opts, node, head, badmatch)
57 for r in results:
57 for r in results:
58 yield r
58 yield r
59
59
60 def walkchangerevs(ui, repo, pats, opts):
60 def walkchangerevs(ui, repo, pats, opts):
61 '''Iterate over files and the revs they changed in.
61 '''Iterate over files and the revs they changed in.
62
62
63 Callers most commonly need to iterate backwards over the history
63 Callers most commonly need to iterate backwards over the history
64 it is interested in. Doing so has awful (quadratic-looking)
64 it is interested in. Doing so has awful (quadratic-looking)
65 performance, so we use iterators in a "windowed" way.
65 performance, so we use iterators in a "windowed" way.
66
66
67 We walk a window of revisions in the desired order. Within the
67 We walk a window of revisions in the desired order. Within the
68 window, we first walk forwards to gather data, then in the desired
68 window, we first walk forwards to gather data, then in the desired
69 order (usually backwards) to display it.
69 order (usually backwards) to display it.
70
70
71 This function returns an (iterator, getchange, matchfn) tuple. The
71 This function returns an (iterator, getchange, matchfn) tuple. The
72 getchange function returns the changelog entry for a numeric
72 getchange function returns the changelog entry for a numeric
73 revision. The iterator yields 3-tuples. They will be of one of
73 revision. The iterator yields 3-tuples. They will be of one of
74 the following forms:
74 the following forms:
75
75
76 "window", incrementing, lastrev: stepping through a window,
76 "window", incrementing, lastrev: stepping through a window,
77 positive if walking forwards through revs, last rev in the
77 positive if walking forwards through revs, last rev in the
78 sequence iterated over - use to reset state for the current window
78 sequence iterated over - use to reset state for the current window
79
79
80 "add", rev, fns: out-of-order traversal of the given file names
80 "add", rev, fns: out-of-order traversal of the given file names
81 fns, which changed during revision rev - use to gather data for
81 fns, which changed during revision rev - use to gather data for
82 possible display
82 possible display
83
83
84 "iter", rev, None: in-order traversal of the revs earlier iterated
84 "iter", rev, None: in-order traversal of the revs earlier iterated
85 over with "add" - use to display data'''
85 over with "add" - use to display data'''
86
86
87 def increasing_windows(start, end, windowsize=8, sizelimit=512):
87 def increasing_windows(start, end, windowsize=8, sizelimit=512):
88 if start < end:
88 if start < end:
89 while start < end:
89 while start < end:
90 yield start, min(windowsize, end-start)
90 yield start, min(windowsize, end-start)
91 start += windowsize
91 start += windowsize
92 if windowsize < sizelimit:
92 if windowsize < sizelimit:
93 windowsize *= 2
93 windowsize *= 2
94 else:
94 else:
95 while start > end:
95 while start > end:
96 yield start, min(windowsize, start-end-1)
96 yield start, min(windowsize, start-end-1)
97 start -= windowsize
97 start -= windowsize
98 if windowsize < sizelimit:
98 if windowsize < sizelimit:
99 windowsize *= 2
99 windowsize *= 2
100
100
101
101
102 files, matchfn, anypats = matchpats(repo, pats, opts)
102 files, matchfn, anypats = matchpats(repo, pats, opts)
103
103
104 if repo.changelog.count() == 0:
104 if repo.changelog.count() == 0:
105 return [], False, matchfn
105 return [], False, matchfn
106
106
107 revs = map(int, revrange(ui, repo, opts['rev'] or ['tip:0']))
107 revs = map(int, revrange(ui, repo, opts['rev'] or ['tip:0']))
108 wanted = {}
108 wanted = {}
109 slowpath = anypats
109 slowpath = anypats
110 fncache = {}
110 fncache = {}
111
111
112 chcache = {}
112 chcache = {}
113 def getchange(rev):
113 def getchange(rev):
114 ch = chcache.get(rev)
114 ch = chcache.get(rev)
115 if ch is None:
115 if ch is None:
116 chcache[rev] = ch = repo.changelog.read(repo.lookup(str(rev)))
116 chcache[rev] = ch = repo.changelog.read(repo.lookup(str(rev)))
117 return ch
117 return ch
118
118
119 if not slowpath and not files:
119 if not slowpath and not files:
120 # No files, no patterns. Display all revs.
120 # No files, no patterns. Display all revs.
121 wanted = dict(zip(revs, revs))
121 wanted = dict(zip(revs, revs))
122 if not slowpath:
122 if not slowpath:
123 # Only files, no patterns. Check the history of each file.
123 # Only files, no patterns. Check the history of each file.
124 def filerevgen(filelog):
124 def filerevgen(filelog):
125 for i, window in increasing_windows(filelog.count()-1, -1):
125 for i, window in increasing_windows(filelog.count()-1, -1):
126 revs = []
126 revs = []
127 for j in xrange(i - window, i + 1):
127 for j in xrange(i - window, i + 1):
128 revs.append(filelog.linkrev(filelog.node(j)))
128 revs.append(filelog.linkrev(filelog.node(j)))
129 revs.reverse()
129 revs.reverse()
130 for rev in revs:
130 for rev in revs:
131 yield rev
131 yield rev
132
132
133 minrev, maxrev = min(revs), max(revs)
133 minrev, maxrev = min(revs), max(revs)
134 for file_ in files:
134 for file_ in files:
135 filelog = repo.file(file_)
135 filelog = repo.file(file_)
136 # A zero count may be a directory or deleted file, so
136 # A zero count may be a directory or deleted file, so
137 # try to find matching entries on the slow path.
137 # try to find matching entries on the slow path.
138 if filelog.count() == 0:
138 if filelog.count() == 0:
139 slowpath = True
139 slowpath = True
140 break
140 break
141 for rev in filerevgen(filelog):
141 for rev in filerevgen(filelog):
142 if rev <= maxrev:
142 if rev <= maxrev:
143 if rev < minrev:
143 if rev < minrev:
144 break
144 break
145 fncache.setdefault(rev, [])
145 fncache.setdefault(rev, [])
146 fncache[rev].append(file_)
146 fncache[rev].append(file_)
147 wanted[rev] = 1
147 wanted[rev] = 1
148 if slowpath:
148 if slowpath:
149 # The slow path checks files modified in every changeset.
149 # The slow path checks files modified in every changeset.
150 def changerevgen():
150 def changerevgen():
151 for i, window in increasing_windows(repo.changelog.count()-1, -1):
151 for i, window in increasing_windows(repo.changelog.count()-1, -1):
152 for j in xrange(i - window, i + 1):
152 for j in xrange(i - window, i + 1):
153 yield j, getchange(j)[3]
153 yield j, getchange(j)[3]
154
154
155 for rev, changefiles in changerevgen():
155 for rev, changefiles in changerevgen():
156 matches = filter(matchfn, changefiles)
156 matches = filter(matchfn, changefiles)
157 if matches:
157 if matches:
158 fncache[rev] = matches
158 fncache[rev] = matches
159 wanted[rev] = 1
159 wanted[rev] = 1
160
160
161 def iterate():
161 def iterate():
162 for i, window in increasing_windows(0, len(revs)):
162 for i, window in increasing_windows(0, len(revs)):
163 yield 'window', revs[0] < revs[-1], revs[-1]
163 yield 'window', revs[0] < revs[-1], revs[-1]
164 nrevs = [rev for rev in revs[i:i+window]
164 nrevs = [rev for rev in revs[i:i+window]
165 if rev in wanted]
165 if rev in wanted]
166 srevs = list(nrevs)
166 srevs = list(nrevs)
167 srevs.sort()
167 srevs.sort()
168 for rev in srevs:
168 for rev in srevs:
169 fns = fncache.get(rev) or filter(matchfn, getchange(rev)[3])
169 fns = fncache.get(rev) or filter(matchfn, getchange(rev)[3])
170 yield 'add', rev, fns
170 yield 'add', rev, fns
171 for rev in nrevs:
171 for rev in nrevs:
172 yield 'iter', rev, None
172 yield 'iter', rev, None
173 return iterate(), getchange, matchfn
173 return iterate(), getchange, matchfn
174
174
175 revrangesep = ':'
175 revrangesep = ':'
176
176
177 def revrange(ui, repo, revs, revlog=None):
177 def revrange(ui, repo, revs, revlog=None):
178 """Yield revision as strings from a list of revision specifications."""
178 """Yield revision as strings from a list of revision specifications."""
179 if revlog is None:
179 if revlog is None:
180 revlog = repo.changelog
180 revlog = repo.changelog
181 revcount = revlog.count()
181 revcount = revlog.count()
182 def fix(val, defval):
182 def fix(val, defval):
183 if not val:
183 if not val:
184 return defval
184 return defval
185 try:
185 try:
186 num = int(val)
186 num = int(val)
187 if str(num) != val:
187 if str(num) != val:
188 raise ValueError
188 raise ValueError
189 if num < 0:
189 if num < 0:
190 num += revcount
190 num += revcount
191 if num < 0:
191 if num < 0:
192 num = 0
192 num = 0
193 elif num >= revcount:
193 elif num >= revcount:
194 raise ValueError
194 raise ValueError
195 except ValueError:
195 except ValueError:
196 try:
196 try:
197 num = repo.changelog.rev(repo.lookup(val))
197 num = repo.changelog.rev(repo.lookup(val))
198 except KeyError:
198 except KeyError:
199 try:
199 try:
200 num = revlog.rev(revlog.lookup(val))
200 num = revlog.rev(revlog.lookup(val))
201 except KeyError:
201 except KeyError:
202 raise util.Abort(_('invalid revision identifier %s'), val)
202 raise util.Abort(_('invalid revision identifier %s'), val)
203 return num
203 return num
204 seen = {}
204 seen = {}
205 for spec in revs:
205 for spec in revs:
206 if spec.find(revrangesep) >= 0:
206 if spec.find(revrangesep) >= 0:
207 start, end = spec.split(revrangesep, 1)
207 start, end = spec.split(revrangesep, 1)
208 start = fix(start, 0)
208 start = fix(start, 0)
209 end = fix(end, revcount - 1)
209 end = fix(end, revcount - 1)
210 step = start > end and -1 or 1
210 step = start > end and -1 or 1
211 for rev in xrange(start, end+step, step):
211 for rev in xrange(start, end+step, step):
212 if rev in seen:
212 if rev in seen:
213 continue
213 continue
214 seen[rev] = 1
214 seen[rev] = 1
215 yield str(rev)
215 yield str(rev)
216 else:
216 else:
217 rev = fix(spec, None)
217 rev = fix(spec, None)
218 if rev in seen:
218 if rev in seen:
219 continue
219 continue
220 seen[rev] = 1
220 seen[rev] = 1
221 yield str(rev)
221 yield str(rev)
222
222
223 def make_filename(repo, r, pat, node=None,
223 def make_filename(repo, r, pat, node=None,
224 total=None, seqno=None, revwidth=None, pathname=None):
224 total=None, seqno=None, revwidth=None, pathname=None):
225 node_expander = {
225 node_expander = {
226 'H': lambda: hex(node),
226 'H': lambda: hex(node),
227 'R': lambda: str(r.rev(node)),
227 'R': lambda: str(r.rev(node)),
228 'h': lambda: short(node),
228 'h': lambda: short(node),
229 }
229 }
230 expander = {
230 expander = {
231 '%': lambda: '%',
231 '%': lambda: '%',
232 'b': lambda: os.path.basename(repo.root),
232 'b': lambda: os.path.basename(repo.root),
233 }
233 }
234
234
235 try:
235 try:
236 if node:
236 if node:
237 expander.update(node_expander)
237 expander.update(node_expander)
238 if node and revwidth is not None:
238 if node and revwidth is not None:
239 expander['r'] = lambda: str(r.rev(node)).zfill(revwidth)
239 expander['r'] = lambda: str(r.rev(node)).zfill(revwidth)
240 if total is not None:
240 if total is not None:
241 expander['N'] = lambda: str(total)
241 expander['N'] = lambda: str(total)
242 if seqno is not None:
242 if seqno is not None:
243 expander['n'] = lambda: str(seqno)
243 expander['n'] = lambda: str(seqno)
244 if total is not None and seqno is not None:
244 if total is not None and seqno is not None:
245 expander['n'] = lambda:str(seqno).zfill(len(str(total)))
245 expander['n'] = lambda:str(seqno).zfill(len(str(total)))
246 if pathname is not None:
246 if pathname is not None:
247 expander['s'] = lambda: os.path.basename(pathname)
247 expander['s'] = lambda: os.path.basename(pathname)
248 expander['d'] = lambda: os.path.dirname(pathname) or '.'
248 expander['d'] = lambda: os.path.dirname(pathname) or '.'
249 expander['p'] = lambda: pathname
249 expander['p'] = lambda: pathname
250
250
251 newname = []
251 newname = []
252 patlen = len(pat)
252 patlen = len(pat)
253 i = 0
253 i = 0
254 while i < patlen:
254 while i < patlen:
255 c = pat[i]
255 c = pat[i]
256 if c == '%':
256 if c == '%':
257 i += 1
257 i += 1
258 c = pat[i]
258 c = pat[i]
259 c = expander[c]()
259 c = expander[c]()
260 newname.append(c)
260 newname.append(c)
261 i += 1
261 i += 1
262 return ''.join(newname)
262 return ''.join(newname)
263 except KeyError, inst:
263 except KeyError, inst:
264 raise util.Abort(_("invalid format spec '%%%s' in output file name"),
264 raise util.Abort(_("invalid format spec '%%%s' in output file name"),
265 inst.args[0])
265 inst.args[0])
266
266
267 def make_file(repo, r, pat, node=None,
267 def make_file(repo, r, pat, node=None,
268 total=None, seqno=None, revwidth=None, mode='wb', pathname=None):
268 total=None, seqno=None, revwidth=None, mode='wb', pathname=None):
269 if not pat or pat == '-':
269 if not pat or pat == '-':
270 return 'w' in mode and sys.stdout or sys.stdin
270 return 'w' in mode and sys.stdout or sys.stdin
271 if hasattr(pat, 'write') and 'w' in mode:
271 if hasattr(pat, 'write') and 'w' in mode:
272 return pat
272 return pat
273 if hasattr(pat, 'read') and 'r' in mode:
273 if hasattr(pat, 'read') and 'r' in mode:
274 return pat
274 return pat
275 return open(make_filename(repo, r, pat, node, total, seqno, revwidth,
275 return open(make_filename(repo, r, pat, node, total, seqno, revwidth,
276 pathname),
276 pathname),
277 mode)
277 mode)
278
278
279 def write_bundle(cg, filename=None, compress=True):
279 def write_bundle(cg, filename=None, compress=True):
280 """Write a bundle file and return its filename.
280 """Write a bundle file and return its filename.
281
281
282 Existing files will not be overwritten.
282 Existing files will not be overwritten.
283 If no filename is specified, a temporary file is created.
283 If no filename is specified, a temporary file is created.
284 bz2 compression can be turned off.
284 bz2 compression can be turned off.
285 The bundle file will be deleted in case of errors.
285 The bundle file will be deleted in case of errors.
286 """
286 """
287 class nocompress(object):
287 class nocompress(object):
288 def compress(self, x):
288 def compress(self, x):
289 return x
289 return x
290 def flush(self):
290 def flush(self):
291 return ""
291 return ""
292
292
293 fh = None
293 fh = None
294 cleanup = None
294 cleanup = None
295 try:
295 try:
296 if filename:
296 if filename:
297 if os.path.exists(filename):
297 if os.path.exists(filename):
298 raise util.Abort(_("file '%s' already exists"), filename)
298 raise util.Abort(_("file '%s' already exists"), filename)
299 fh = open(filename, "wb")
299 fh = open(filename, "wb")
300 else:
300 else:
301 fd, filename = tempfile.mkstemp(suffix=".hg", prefix="hg-bundle-")
301 fd, filename = tempfile.mkstemp(suffix=".hg", prefix="hg-bundle-")
302 fh = os.fdopen(fd, "wb")
302 fh = os.fdopen(fd, "wb")
303 cleanup = filename
303 cleanup = filename
304
304
305 if compress:
305 if compress:
306 fh.write("HG10")
306 fh.write("HG10")
307 z = bz2.BZ2Compressor(9)
307 z = bz2.BZ2Compressor(9)
308 else:
308 else:
309 fh.write("HG10UN")
309 fh.write("HG10UN")
310 z = nocompress()
310 z = nocompress()
311 # parse the changegroup data, otherwise we will block
311 # parse the changegroup data, otherwise we will block
312 # in case of sshrepo because we don't know the end of the stream
312 # in case of sshrepo because we don't know the end of the stream
313
313
314 # an empty chunkiter is the end of the changegroup
314 # an empty chunkiter is the end of the changegroup
315 empty = False
315 empty = False
316 while not empty:
316 while not empty:
317 empty = True
317 empty = True
318 for chunk in changegroup.chunkiter(cg):
318 for chunk in changegroup.chunkiter(cg):
319 empty = False
319 empty = False
320 fh.write(z.compress(changegroup.genchunk(chunk)))
320 fh.write(z.compress(changegroup.genchunk(chunk)))
321 fh.write(z.compress(changegroup.closechunk()))
321 fh.write(z.compress(changegroup.closechunk()))
322 fh.write(z.flush())
322 fh.write(z.flush())
323 cleanup = None
323 cleanup = None
324 return filename
324 return filename
325 finally:
325 finally:
326 if fh is not None:
326 if fh is not None:
327 fh.close()
327 fh.close()
328 if cleanup is not None:
328 if cleanup is not None:
329 os.unlink(cleanup)
329 os.unlink(cleanup)
330
330
331 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always,
331 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always,
332 changes=None, text=False, opts={}):
332 changes=None, text=False, opts={}):
333 if not node1:
333 if not node1:
334 node1 = repo.dirstate.parents()[0]
334 node1 = repo.dirstate.parents()[0]
335 # reading the data for node1 early allows it to play nicely
335 # reading the data for node1 early allows it to play nicely
336 # with repo.changes and the revlog cache.
336 # with repo.changes and the revlog cache.
337 change = repo.changelog.read(node1)
337 change = repo.changelog.read(node1)
338 mmap = repo.manifest.read(change[0])
338 mmap = repo.manifest.read(change[0])
339 date1 = util.datestr(change[2])
339 date1 = util.datestr(change[2])
340
340
341 if not changes:
341 if not changes:
342 changes = repo.changes(node1, node2, files, match=match)
342 changes = repo.changes(node1, node2, files, match=match)
343 modified, added, removed, deleted, unknown = changes
343 modified, added, removed, deleted, unknown = changes
344 if files:
344 if files:
345 modified, added, removed = map(lambda x: filterfiles(files, x),
345 modified, added, removed = map(lambda x: filterfiles(files, x),
346 (modified, added, removed))
346 (modified, added, removed))
347
347
348 if not modified and not added and not removed:
348 if not modified and not added and not removed:
349 return
349 return
350
350
351 if node2:
351 if node2:
352 change = repo.changelog.read(node2)
352 change = repo.changelog.read(node2)
353 mmap2 = repo.manifest.read(change[0])
353 mmap2 = repo.manifest.read(change[0])
354 date2 = util.datestr(change[2])
354 date2 = util.datestr(change[2])
355 def read(f):
355 def read(f):
356 return repo.file(f).read(mmap2[f])
356 return repo.file(f).read(mmap2[f])
357 else:
357 else:
358 date2 = util.datestr()
358 date2 = util.datestr()
359 def read(f):
359 def read(f):
360 return repo.wread(f)
360 return repo.wread(f)
361
361
362 if ui.quiet:
362 if ui.quiet:
363 r = None
363 r = None
364 else:
364 else:
365 hexfunc = ui.verbose and hex or short
365 hexfunc = ui.verbose and hex or short
366 r = [hexfunc(node) for node in [node1, node2] if node]
366 r = [hexfunc(node) for node in [node1, node2] if node]
367
367
368 diffopts = ui.diffopts()
368 diffopts = ui.diffopts()
369 showfunc = opts.get('show_function') or diffopts['showfunc']
369 showfunc = opts.get('show_function') or diffopts['showfunc']
370 ignorews = opts.get('ignore_all_space') or diffopts['ignorews']
370 ignorews = opts.get('ignore_all_space') or diffopts['ignorews']
371 for f in modified:
371 for f in modified:
372 to = None
372 to = None
373 if f in mmap:
373 if f in mmap:
374 to = repo.file(f).read(mmap[f])
374 to = repo.file(f).read(mmap[f])
375 tn = read(f)
375 tn = read(f)
376 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
376 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
377 showfunc=showfunc, ignorews=ignorews))
377 showfunc=showfunc, ignorews=ignorews))
378 for f in added:
378 for f in added:
379 to = None
379 to = None
380 tn = read(f)
380 tn = read(f)
381 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
381 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
382 showfunc=showfunc, ignorews=ignorews))
382 showfunc=showfunc, ignorews=ignorews))
383 for f in removed:
383 for f in removed:
384 to = repo.file(f).read(mmap[f])
384 to = repo.file(f).read(mmap[f])
385 tn = None
385 tn = None
386 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
386 fp.write(mdiff.unidiff(to, date1, tn, date2, f, r, text=text,
387 showfunc=showfunc, ignorews=ignorews))
387 showfunc=showfunc, ignorews=ignorews))
388
388
389 def trimuser(ui, name, rev, revcache):
389 def trimuser(ui, name, rev, revcache):
390 """trim the name of the user who committed a change"""
390 """trim the name of the user who committed a change"""
391 user = revcache.get(rev)
391 user = revcache.get(rev)
392 if user is None:
392 if user is None:
393 user = revcache[rev] = ui.shortuser(name)
393 user = revcache[rev] = ui.shortuser(name)
394 return user
394 return user
395
395
396 class changeset_templater(object):
396 class changeset_templater(object):
397 '''use templater module to format changeset information.'''
397 '''use templater module to format changeset information.'''
398
398
399 def __init__(self, ui, repo, mapfile):
399 def __init__(self, ui, repo, mapfile):
400 self.t = templater.templater(mapfile, templater.common_filters,
400 self.t = templater.templater(mapfile, templater.common_filters,
401 cache={'parent': '{rev}:{node|short} ',
401 cache={'parent': '{rev}:{node|short} ',
402 'manifest': '{rev}:{node|short}'})
402 'manifest': '{rev}:{node|short}'})
403 self.ui = ui
403 self.ui = ui
404 self.repo = repo
404 self.repo = repo
405
405
406 def use_template(self, t):
406 def use_template(self, t):
407 '''set template string to use'''
407 '''set template string to use'''
408 self.t.cache['changeset'] = t
408 self.t.cache['changeset'] = t
409
409
410 def write(self, thing, header=False):
410 def write(self, thing, header=False):
411 '''write expanded template.
411 '''write expanded template.
412 uses in-order recursive traverse of iterators.'''
412 uses in-order recursive traverse of iterators.'''
413 for t in thing:
413 for t in thing:
414 if hasattr(t, '__iter__'):
414 if hasattr(t, '__iter__'):
415 self.write(t, header=header)
415 self.write(t, header=header)
416 elif header:
416 elif header:
417 self.ui.write_header(t)
417 self.ui.write_header(t)
418 else:
418 else:
419 self.ui.write(t)
419 self.ui.write(t)
420
420
421 def write_header(self, thing):
421 def write_header(self, thing):
422 self.write(thing, header=True)
422 self.write(thing, header=True)
423
423
424 def show(self, rev=0, changenode=None, brinfo=None):
424 def show(self, rev=0, changenode=None, brinfo=None):
425 '''show a single changeset or file revision'''
425 '''show a single changeset or file revision'''
426 log = self.repo.changelog
426 log = self.repo.changelog
427 if changenode is None:
427 if changenode is None:
428 changenode = log.node(rev)
428 changenode = log.node(rev)
429 elif not rev:
429 elif not rev:
430 rev = log.rev(changenode)
430 rev = log.rev(changenode)
431
431
432 changes = log.read(changenode)
432 changes = log.read(changenode)
433
433
434 def showlist(name, values, plural=None, **args):
434 def showlist(name, values, plural=None, **args):
435 '''expand set of values.
435 '''expand set of values.
436 name is name of key in template map.
436 name is name of key in template map.
437 values is list of strings or dicts.
437 values is list of strings or dicts.
438 plural is plural of name, if not simply name + 's'.
438 plural is plural of name, if not simply name + 's'.
439
439
440 expansion works like this, given name 'foo'.
440 expansion works like this, given name 'foo'.
441
441
442 if values is empty, expand 'no_foos'.
442 if values is empty, expand 'no_foos'.
443
443
444 if 'foo' not in template map, return values as a string,
444 if 'foo' not in template map, return values as a string,
445 joined by space.
445 joined by space.
446
446
447 expand 'start_foos'.
447 expand 'start_foos'.
448
448
449 for each value, expand 'foo'. if 'last_foo' in template
449 for each value, expand 'foo'. if 'last_foo' in template
450 map, expand it instead of 'foo' for last key.
450 map, expand it instead of 'foo' for last key.
451
451
452 expand 'end_foos'.
452 expand 'end_foos'.
453 '''
453 '''
454 if plural: names = plural
454 if plural: names = plural
455 else: names = name + 's'
455 else: names = name + 's'
456 if not values:
456 if not values:
457 noname = 'no_' + names
457 noname = 'no_' + names
458 if noname in self.t:
458 if noname in self.t:
459 yield self.t(noname, **args)
459 yield self.t(noname, **args)
460 return
460 return
461 if name not in self.t:
461 if name not in self.t:
462 if isinstance(values[0], str):
462 if isinstance(values[0], str):
463 yield ' '.join(values)
463 yield ' '.join(values)
464 else:
464 else:
465 for v in values:
465 for v in values:
466 yield dict(v, **args)
466 yield dict(v, **args)
467 return
467 return
468 startname = 'start_' + names
468 startname = 'start_' + names
469 if startname in self.t:
469 if startname in self.t:
470 yield self.t(startname, **args)
470 yield self.t(startname, **args)
471 vargs = args.copy()
471 vargs = args.copy()
472 def one(v, tag=name):
472 def one(v, tag=name):
473 try:
473 try:
474 vargs.update(v)
474 vargs.update(v)
475 except (AttributeError, ValueError):
475 except (AttributeError, ValueError):
476 try:
476 try:
477 for a, b in v:
477 for a, b in v:
478 vargs[a] = b
478 vargs[a] = b
479 except ValueError:
479 except ValueError:
480 vargs[name] = v
480 vargs[name] = v
481 return self.t(tag, **vargs)
481 return self.t(tag, **vargs)
482 lastname = 'last_' + name
482 lastname = 'last_' + name
483 if lastname in self.t:
483 if lastname in self.t:
484 last = values.pop()
484 last = values.pop()
485 else:
485 else:
486 last = None
486 last = None
487 for v in values:
487 for v in values:
488 yield one(v)
488 yield one(v)
489 if last is not None:
489 if last is not None:
490 yield one(last, tag=lastname)
490 yield one(last, tag=lastname)
491 endname = 'end_' + names
491 endname = 'end_' + names
492 if endname in self.t:
492 if endname in self.t:
493 yield self.t(endname, **args)
493 yield self.t(endname, **args)
494
494
495 if brinfo:
495 if brinfo:
496 def showbranches(**args):
496 def showbranches(**args):
497 if changenode in brinfo:
497 if changenode in brinfo:
498 for x in showlist('branch', brinfo[changenode],
498 for x in showlist('branch', brinfo[changenode],
499 plural='branches', **args):
499 plural='branches', **args):
500 yield x
500 yield x
501 else:
501 else:
502 showbranches = ''
502 showbranches = ''
503
503
504 if self.ui.debugflag:
504 if self.ui.debugflag:
505 def showmanifest(**args):
505 def showmanifest(**args):
506 args = args.copy()
506 args = args.copy()
507 args.update(dict(rev=self.repo.manifest.rev(changes[0]),
507 args.update(dict(rev=self.repo.manifest.rev(changes[0]),
508 node=hex(changes[0])))
508 node=hex(changes[0])))
509 yield self.t('manifest', **args)
509 yield self.t('manifest', **args)
510 else:
510 else:
511 showmanifest = ''
511 showmanifest = ''
512
512
513 def showparents(**args):
513 def showparents(**args):
514 parents = [[('rev', log.rev(p)), ('node', hex(p))]
514 parents = [[('rev', log.rev(p)), ('node', hex(p))]
515 for p in log.parents(changenode)
515 for p in log.parents(changenode)
516 if self.ui.debugflag or p != nullid]
516 if self.ui.debugflag or p != nullid]
517 if (not self.ui.debugflag and len(parents) == 1 and
517 if (not self.ui.debugflag and len(parents) == 1 and
518 parents[0][0][1] == rev - 1):
518 parents[0][0][1] == rev - 1):
519 return
519 return
520 for x in showlist('parent', parents, **args):
520 for x in showlist('parent', parents, **args):
521 yield x
521 yield x
522
522
523 def showtags(**args):
523 def showtags(**args):
524 for x in showlist('tag', self.repo.nodetags(changenode), **args):
524 for x in showlist('tag', self.repo.nodetags(changenode), **args):
525 yield x
525 yield x
526
526
527 if self.ui.debugflag:
527 if self.ui.debugflag:
528 files = self.repo.changes(log.parents(changenode)[0], changenode)
528 files = self.repo.changes(log.parents(changenode)[0], changenode)
529 def showfiles(**args):
529 def showfiles(**args):
530 for x in showlist('file', files[0], **args): yield x
530 for x in showlist('file', files[0], **args): yield x
531 def showadds(**args):
531 def showadds(**args):
532 for x in showlist('file_add', files[1], **args): yield x
532 for x in showlist('file_add', files[1], **args): yield x
533 def showdels(**args):
533 def showdels(**args):
534 for x in showlist('file_del', files[2], **args): yield x
534 for x in showlist('file_del', files[2], **args): yield x
535 else:
535 else:
536 def showfiles(**args):
536 def showfiles(**args):
537 for x in showlist('file', changes[3], **args): yield x
537 for x in showlist('file', changes[3], **args): yield x
538 showadds = ''
538 showadds = ''
539 showdels = ''
539 showdels = ''
540
540
541 props = {
541 props = {
542 'author': changes[1],
542 'author': changes[1],
543 'branches': showbranches,
543 'branches': showbranches,
544 'date': changes[2],
544 'date': changes[2],
545 'desc': changes[4],
545 'desc': changes[4],
546 'file_adds': showadds,
546 'file_adds': showadds,
547 'file_dels': showdels,
547 'file_dels': showdels,
548 'files': showfiles,
548 'files': showfiles,
549 'manifest': showmanifest,
549 'manifest': showmanifest,
550 'node': hex(changenode),
550 'node': hex(changenode),
551 'parents': showparents,
551 'parents': showparents,
552 'rev': rev,
552 'rev': rev,
553 'tags': showtags,
553 'tags': showtags,
554 }
554 }
555
555
556 try:
556 try:
557 if self.ui.debugflag and 'header_debug' in self.t:
557 if self.ui.debugflag and 'header_debug' in self.t:
558 key = 'header_debug'
558 key = 'header_debug'
559 elif self.ui.quiet and 'header_quiet' in self.t:
559 elif self.ui.quiet and 'header_quiet' in self.t:
560 key = 'header_quiet'
560 key = 'header_quiet'
561 elif self.ui.verbose and 'header_verbose' in self.t:
561 elif self.ui.verbose and 'header_verbose' in self.t:
562 key = 'header_verbose'
562 key = 'header_verbose'
563 elif 'header' in self.t:
563 elif 'header' in self.t:
564 key = 'header'
564 key = 'header'
565 else:
565 else:
566 key = ''
566 key = ''
567 if key:
567 if key:
568 self.write_header(self.t(key, **props))
568 self.write_header(self.t(key, **props))
569 if self.ui.debugflag and 'changeset_debug' in self.t:
569 if self.ui.debugflag and 'changeset_debug' in self.t:
570 key = 'changeset_debug'
570 key = 'changeset_debug'
571 elif self.ui.quiet and 'changeset_quiet' in self.t:
571 elif self.ui.quiet and 'changeset_quiet' in self.t:
572 key = 'changeset_quiet'
572 key = 'changeset_quiet'
573 elif self.ui.verbose and 'changeset_verbose' in self.t:
573 elif self.ui.verbose and 'changeset_verbose' in self.t:
574 key = 'changeset_verbose'
574 key = 'changeset_verbose'
575 else:
575 else:
576 key = 'changeset'
576 key = 'changeset'
577 self.write(self.t(key, **props))
577 self.write(self.t(key, **props))
578 except KeyError, inst:
578 except KeyError, inst:
579 raise util.Abort(_("%s: no key named '%s'") % (self.t.mapfile,
579 raise util.Abort(_("%s: no key named '%s'") % (self.t.mapfile,
580 inst.args[0]))
580 inst.args[0]))
581 except SyntaxError, inst:
581 except SyntaxError, inst:
582 raise util.Abort(_('%s: %s') % (self.t.mapfile, inst.args[0]))
582 raise util.Abort(_('%s: %s') % (self.t.mapfile, inst.args[0]))
583
583
584 class changeset_printer(object):
584 class changeset_printer(object):
585 '''show changeset information when templating not requested.'''
585 '''show changeset information when templating not requested.'''
586
586
587 def __init__(self, ui, repo):
587 def __init__(self, ui, repo):
588 self.ui = ui
588 self.ui = ui
589 self.repo = repo
589 self.repo = repo
590
590
591 def show(self, rev=0, changenode=None, brinfo=None):
591 def show(self, rev=0, changenode=None, brinfo=None):
592 '''show a single changeset or file revision'''
592 '''show a single changeset or file revision'''
593 log = self.repo.changelog
593 log = self.repo.changelog
594 if changenode is None:
594 if changenode is None:
595 changenode = log.node(rev)
595 changenode = log.node(rev)
596 elif not rev:
596 elif not rev:
597 rev = log.rev(changenode)
597 rev = log.rev(changenode)
598
598
599 if self.ui.quiet:
599 if self.ui.quiet:
600 self.ui.write("%d:%s\n" % (rev, short(changenode)))
600 self.ui.write("%d:%s\n" % (rev, short(changenode)))
601 return
601 return
602
602
603 changes = log.read(changenode)
603 changes = log.read(changenode)
604 date = util.datestr(changes[2])
604 date = util.datestr(changes[2])
605
605
606 parents = [(log.rev(p), self.ui.verbose and hex(p) or short(p))
606 parents = [(log.rev(p), self.ui.verbose and hex(p) or short(p))
607 for p in log.parents(changenode)
607 for p in log.parents(changenode)
608 if self.ui.debugflag or p != nullid]
608 if self.ui.debugflag or p != nullid]
609 if (not self.ui.debugflag and len(parents) == 1 and
609 if (not self.ui.debugflag and len(parents) == 1 and
610 parents[0][0] == rev-1):
610 parents[0][0] == rev-1):
611 parents = []
611 parents = []
612
612
613 if self.ui.verbose:
613 if self.ui.verbose:
614 self.ui.write(_("changeset: %d:%s\n") % (rev, hex(changenode)))
614 self.ui.write(_("changeset: %d:%s\n") % (rev, hex(changenode)))
615 else:
615 else:
616 self.ui.write(_("changeset: %d:%s\n") % (rev, short(changenode)))
616 self.ui.write(_("changeset: %d:%s\n") % (rev, short(changenode)))
617
617
618 for tag in self.repo.nodetags(changenode):
618 for tag in self.repo.nodetags(changenode):
619 self.ui.status(_("tag: %s\n") % tag)
619 self.ui.status(_("tag: %s\n") % tag)
620 for parent in parents:
620 for parent in parents:
621 self.ui.write(_("parent: %d:%s\n") % parent)
621 self.ui.write(_("parent: %d:%s\n") % parent)
622
622
623 if brinfo and changenode in brinfo:
623 if brinfo and changenode in brinfo:
624 br = brinfo[changenode]
624 br = brinfo[changenode]
625 self.ui.write(_("branch: %s\n") % " ".join(br))
625 self.ui.write(_("branch: %s\n") % " ".join(br))
626
626
627 self.ui.debug(_("manifest: %d:%s\n") %
627 self.ui.debug(_("manifest: %d:%s\n") %
628 (self.repo.manifest.rev(changes[0]), hex(changes[0])))
628 (self.repo.manifest.rev(changes[0]), hex(changes[0])))
629 self.ui.status(_("user: %s\n") % changes[1])
629 self.ui.status(_("user: %s\n") % changes[1])
630 self.ui.status(_("date: %s\n") % date)
630 self.ui.status(_("date: %s\n") % date)
631
631
632 if self.ui.debugflag:
632 if self.ui.debugflag:
633 files = self.repo.changes(log.parents(changenode)[0], changenode)
633 files = self.repo.changes(log.parents(changenode)[0], changenode)
634 for key, value in zip([_("files:"), _("files+:"), _("files-:")],
634 for key, value in zip([_("files:"), _("files+:"), _("files-:")],
635 files):
635 files):
636 if value:
636 if value:
637 self.ui.note("%-12s %s\n" % (key, " ".join(value)))
637 self.ui.note("%-12s %s\n" % (key, " ".join(value)))
638 else:
638 else:
639 self.ui.note(_("files: %s\n") % " ".join(changes[3]))
639 self.ui.note(_("files: %s\n") % " ".join(changes[3]))
640
640
641 description = changes[4].strip()
641 description = changes[4].strip()
642 if description:
642 if description:
643 if self.ui.verbose:
643 if self.ui.verbose:
644 self.ui.status(_("description:\n"))
644 self.ui.status(_("description:\n"))
645 self.ui.status(description)
645 self.ui.status(description)
646 self.ui.status("\n\n")
646 self.ui.status("\n\n")
647 else:
647 else:
648 self.ui.status(_("summary: %s\n") %
648 self.ui.status(_("summary: %s\n") %
649 description.splitlines()[0])
649 description.splitlines()[0])
650 self.ui.status("\n")
650 self.ui.status("\n")
651
651
652 def show_changeset(ui, repo, opts):
652 def show_changeset(ui, repo, opts):
653 '''show one changeset. uses template or regular display. caller
653 '''show one changeset. uses template or regular display. caller
654 can pass in 'style' and 'template' options in opts.'''
654 can pass in 'style' and 'template' options in opts.'''
655
655
656 tmpl = opts.get('template')
656 tmpl = opts.get('template')
657 if tmpl:
657 if tmpl:
658 tmpl = templater.parsestring(tmpl, quoted=False)
658 tmpl = templater.parsestring(tmpl, quoted=False)
659 else:
659 else:
660 tmpl = ui.config('ui', 'logtemplate')
660 tmpl = ui.config('ui', 'logtemplate')
661 if tmpl: tmpl = templater.parsestring(tmpl)
661 if tmpl: tmpl = templater.parsestring(tmpl)
662 mapfile = opts.get('style') or ui.config('ui', 'style')
662 mapfile = opts.get('style') or ui.config('ui', 'style')
663 if tmpl or mapfile:
663 if tmpl or mapfile:
664 if mapfile:
664 if mapfile:
665 if not os.path.isfile(mapfile):
665 if not os.path.isfile(mapfile):
666 mapname = templater.templatepath('map-cmdline.' + mapfile)
666 mapname = templater.templatepath('map-cmdline.' + mapfile)
667 if not mapname: mapname = templater.templatepath(mapfile)
667 if not mapname: mapname = templater.templatepath(mapfile)
668 if mapname: mapfile = mapname
668 if mapname: mapfile = mapname
669 try:
669 try:
670 t = changeset_templater(ui, repo, mapfile)
670 t = changeset_templater(ui, repo, mapfile)
671 except SyntaxError, inst:
671 except SyntaxError, inst:
672 raise util.Abort(inst.args[0])
672 raise util.Abort(inst.args[0])
673 if tmpl: t.use_template(tmpl)
673 if tmpl: t.use_template(tmpl)
674 return t
674 return t
675 return changeset_printer(ui, repo)
675 return changeset_printer(ui, repo)
676
676
677 def show_version(ui):
677 def show_version(ui):
678 """output version and copyright information"""
678 """output version and copyright information"""
679 ui.write(_("Mercurial Distributed SCM (version %s)\n")
679 ui.write(_("Mercurial Distributed SCM (version %s)\n")
680 % version.get_version())
680 % version.get_version())
681 ui.status(_(
681 ui.status(_(
682 "\nCopyright (C) 2005 Matt Mackall <mpm@selenic.com>\n"
682 "\nCopyright (C) 2005 Matt Mackall <mpm@selenic.com>\n"
683 "This is free software; see the source for copying conditions. "
683 "This is free software; see the source for copying conditions. "
684 "There is NO\nwarranty; "
684 "There is NO\nwarranty; "
685 "not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
685 "not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
686 ))
686 ))
687
687
688 def help_(ui, cmd=None, with_version=False):
688 def help_(ui, cmd=None, with_version=False):
689 """show help for a given command or all commands"""
689 """show help for a given command or all commands"""
690 option_lists = []
690 option_lists = []
691 if cmd and cmd != 'shortlist':
691 if cmd and cmd != 'shortlist':
692 if with_version:
692 if with_version:
693 show_version(ui)
693 show_version(ui)
694 ui.write('\n')
694 ui.write('\n')
695 aliases, i = find(cmd)
695 aliases, i = find(cmd)
696 # synopsis
696 # synopsis
697 ui.write("%s\n\n" % i[2])
697 ui.write("%s\n\n" % i[2])
698
698
699 # description
699 # description
700 doc = i[0].__doc__
700 doc = i[0].__doc__
701 if not doc:
701 if not doc:
702 doc = _("(No help text available)")
702 doc = _("(No help text available)")
703 if ui.quiet:
703 if ui.quiet:
704 doc = doc.splitlines(0)[0]
704 doc = doc.splitlines(0)[0]
705 ui.write("%s\n" % doc.rstrip())
705 ui.write("%s\n" % doc.rstrip())
706
706
707 if not ui.quiet:
707 if not ui.quiet:
708 # aliases
708 # aliases
709 if len(aliases) > 1:
709 if len(aliases) > 1:
710 ui.write(_("\naliases: %s\n") % ', '.join(aliases[1:]))
710 ui.write(_("\naliases: %s\n") % ', '.join(aliases[1:]))
711
711
712 # options
712 # options
713 if i[1]:
713 if i[1]:
714 option_lists.append(("options", i[1]))
714 option_lists.append(("options", i[1]))
715
715
716 else:
716 else:
717 # program name
717 # program name
718 if ui.verbose or with_version:
718 if ui.verbose or with_version:
719 show_version(ui)
719 show_version(ui)
720 else:
720 else:
721 ui.status(_("Mercurial Distributed SCM\n"))
721 ui.status(_("Mercurial Distributed SCM\n"))
722 ui.status('\n')
722 ui.status('\n')
723
723
724 # list of commands
724 # list of commands
725 if cmd == "shortlist":
725 if cmd == "shortlist":
726 ui.status(_('basic commands (use "hg help" '
726 ui.status(_('basic commands (use "hg help" '
727 'for the full list or option "-v" for details):\n\n'))
727 'for the full list or option "-v" for details):\n\n'))
728 elif ui.verbose:
728 elif ui.verbose:
729 ui.status(_('list of commands:\n\n'))
729 ui.status(_('list of commands:\n\n'))
730 else:
730 else:
731 ui.status(_('list of commands (use "hg help -v" '
731 ui.status(_('list of commands (use "hg help -v" '
732 'to show aliases and global options):\n\n'))
732 'to show aliases and global options):\n\n'))
733
733
734 h = {}
734 h = {}
735 cmds = {}
735 cmds = {}
736 for c, e in table.items():
736 for c, e in table.items():
737 f = c.split("|")[0]
737 f = c.split("|")[0]
738 if cmd == "shortlist" and not f.startswith("^"):
738 if cmd == "shortlist" and not f.startswith("^"):
739 continue
739 continue
740 f = f.lstrip("^")
740 f = f.lstrip("^")
741 if not ui.debugflag and f.startswith("debug"):
741 if not ui.debugflag and f.startswith("debug"):
742 continue
742 continue
743 doc = e[0].__doc__
743 doc = e[0].__doc__
744 if not doc:
744 if not doc:
745 doc = _("(No help text available)")
745 doc = _("(No help text available)")
746 h[f] = doc.splitlines(0)[0].rstrip()
746 h[f] = doc.splitlines(0)[0].rstrip()
747 cmds[f] = c.lstrip("^")
747 cmds[f] = c.lstrip("^")
748
748
749 fns = h.keys()
749 fns = h.keys()
750 fns.sort()
750 fns.sort()
751 m = max(map(len, fns))
751 m = max(map(len, fns))
752 for f in fns:
752 for f in fns:
753 if ui.verbose:
753 if ui.verbose:
754 commands = cmds[f].replace("|",", ")
754 commands = cmds[f].replace("|",", ")
755 ui.write(" %s:\n %s\n"%(commands, h[f]))
755 ui.write(" %s:\n %s\n"%(commands, h[f]))
756 else:
756 else:
757 ui.write(' %-*s %s\n' % (m, f, h[f]))
757 ui.write(' %-*s %s\n' % (m, f, h[f]))
758
758
759 # global options
759 # global options
760 if ui.verbose:
760 if ui.verbose:
761 option_lists.append(("global options", globalopts))
761 option_lists.append(("global options", globalopts))
762
762
763 # list all option lists
763 # list all option lists
764 opt_output = []
764 opt_output = []
765 for title, options in option_lists:
765 for title, options in option_lists:
766 opt_output.append(("\n%s:\n" % title, None))
766 opt_output.append(("\n%s:\n" % title, None))
767 for shortopt, longopt, default, desc in options:
767 for shortopt, longopt, default, desc in options:
768 opt_output.append(("%2s%s" % (shortopt and "-%s" % shortopt,
768 opt_output.append(("%2s%s" % (shortopt and "-%s" % shortopt,
769 longopt and " --%s" % longopt),
769 longopt and " --%s" % longopt),
770 "%s%s" % (desc,
770 "%s%s" % (desc,
771 default
771 default
772 and _(" (default: %s)") % default
772 and _(" (default: %s)") % default
773 or "")))
773 or "")))
774
774
775 if opt_output:
775 if opt_output:
776 opts_len = max([len(line[0]) for line in opt_output if line[1]])
776 opts_len = max([len(line[0]) for line in opt_output if line[1]])
777 for first, second in opt_output:
777 for first, second in opt_output:
778 if second:
778 if second:
779 ui.write(" %-*s %s\n" % (opts_len, first, second))
779 ui.write(" %-*s %s\n" % (opts_len, first, second))
780 else:
780 else:
781 ui.write("%s\n" % first)
781 ui.write("%s\n" % first)
782
782
783 # Commands start here, listed alphabetically
783 # Commands start here, listed alphabetically
784
784
785 def add(ui, repo, *pats, **opts):
785 def add(ui, repo, *pats, **opts):
786 """add the specified files on the next commit
786 """add the specified files on the next commit
787
787
788 Schedule files to be version controlled and added to the repository.
788 Schedule files to be version controlled and added to the repository.
789
789
790 The files will be added to the repository at the next commit.
790 The files will be added to the repository at the next commit.
791
791
792 If no names are given, add all files in the repository.
792 If no names are given, add all files in the repository.
793 """
793 """
794
794
795 names = []
795 names = []
796 for src, abs, rel, exact in walk(repo, pats, opts):
796 for src, abs, rel, exact in walk(repo, pats, opts):
797 if exact:
797 if exact:
798 if ui.verbose:
798 if ui.verbose:
799 ui.status(_('adding %s\n') % rel)
799 ui.status(_('adding %s\n') % rel)
800 names.append(abs)
800 names.append(abs)
801 elif repo.dirstate.state(abs) == '?':
801 elif repo.dirstate.state(abs) == '?':
802 ui.status(_('adding %s\n') % rel)
802 ui.status(_('adding %s\n') % rel)
803 names.append(abs)
803 names.append(abs)
804 repo.add(names)
804 repo.add(names)
805
805
806 def addremove(ui, repo, *pats, **opts):
806 def addremove(ui, repo, *pats, **opts):
807 """add all new files, delete all missing files
807 """add all new files, delete all missing files
808
808
809 Add all new files and remove all missing files from the repository.
809 Add all new files and remove all missing files from the repository.
810
810
811 New files are ignored if they match any of the patterns in .hgignore. As
811 New files are ignored if they match any of the patterns in .hgignore. As
812 with add, these changes take effect at the next commit.
812 with add, these changes take effect at the next commit.
813 """
813 """
814 return addremove_lock(ui, repo, pats, opts)
814 return addremove_lock(ui, repo, pats, opts)
815
815
816 def addremove_lock(ui, repo, pats, opts, wlock=None):
816 def addremove_lock(ui, repo, pats, opts, wlock=None):
817 add, remove = [], []
817 add, remove = [], []
818 for src, abs, rel, exact in walk(repo, pats, opts):
818 for src, abs, rel, exact in walk(repo, pats, opts):
819 if src == 'f' and repo.dirstate.state(abs) == '?':
819 if src == 'f' and repo.dirstate.state(abs) == '?':
820 add.append(abs)
820 add.append(abs)
821 if ui.verbose or not exact:
821 if ui.verbose or not exact:
822 ui.status(_('adding %s\n') % ((pats and rel) or abs))
822 ui.status(_('adding %s\n') % ((pats and rel) or abs))
823 if repo.dirstate.state(abs) != 'r' and not os.path.exists(rel):
823 if repo.dirstate.state(abs) != 'r' and not os.path.exists(rel):
824 remove.append(abs)
824 remove.append(abs)
825 if ui.verbose or not exact:
825 if ui.verbose or not exact:
826 ui.status(_('removing %s\n') % ((pats and rel) or abs))
826 ui.status(_('removing %s\n') % ((pats and rel) or abs))
827 repo.add(add, wlock=wlock)
827 repo.add(add, wlock=wlock)
828 repo.remove(remove, wlock=wlock)
828 repo.remove(remove, wlock=wlock)
829
829
830 def annotate(ui, repo, *pats, **opts):
830 def annotate(ui, repo, *pats, **opts):
831 """show changeset information per file line
831 """show changeset information per file line
832
832
833 List changes in files, showing the revision id responsible for each line
833 List changes in files, showing the revision id responsible for each line
834
834
835 This command is useful to discover who did a change or when a change took
835 This command is useful to discover who did a change or when a change took
836 place.
836 place.
837
837
838 Without the -a option, annotate will avoid processing files it
838 Without the -a option, annotate will avoid processing files it
839 detects as binary. With -a, annotate will generate an annotation
839 detects as binary. With -a, annotate will generate an annotation
840 anyway, probably with undesirable results.
840 anyway, probably with undesirable results.
841 """
841 """
842 def getnode(rev):
842 def getnode(rev):
843 return short(repo.changelog.node(rev))
843 return short(repo.changelog.node(rev))
844
844
845 ucache = {}
845 ucache = {}
846 def getname(rev):
846 def getname(rev):
847 cl = repo.changelog.read(repo.changelog.node(rev))
847 cl = repo.changelog.read(repo.changelog.node(rev))
848 return trimuser(ui, cl[1], rev, ucache)
848 return trimuser(ui, cl[1], rev, ucache)
849
849
850 dcache = {}
850 dcache = {}
851 def getdate(rev):
851 def getdate(rev):
852 datestr = dcache.get(rev)
852 datestr = dcache.get(rev)
853 if datestr is None:
853 if datestr is None:
854 cl = repo.changelog.read(repo.changelog.node(rev))
854 cl = repo.changelog.read(repo.changelog.node(rev))
855 datestr = dcache[rev] = util.datestr(cl[2])
855 datestr = dcache[rev] = util.datestr(cl[2])
856 return datestr
856 return datestr
857
857
858 if not pats:
858 if not pats:
859 raise util.Abort(_('at least one file name or pattern required'))
859 raise util.Abort(_('at least one file name or pattern required'))
860
860
861 opmap = [['user', getname], ['number', str], ['changeset', getnode],
861 opmap = [['user', getname], ['number', str], ['changeset', getnode],
862 ['date', getdate]]
862 ['date', getdate]]
863 if not opts['user'] and not opts['changeset'] and not opts['date']:
863 if not opts['user'] and not opts['changeset'] and not opts['date']:
864 opts['number'] = 1
864 opts['number'] = 1
865
865
866 if opts['rev']:
866 if opts['rev']:
867 node = repo.changelog.lookup(opts['rev'])
867 node = repo.changelog.lookup(opts['rev'])
868 else:
868 else:
869 node = repo.dirstate.parents()[0]
869 node = repo.dirstate.parents()[0]
870 change = repo.changelog.read(node)
870 change = repo.changelog.read(node)
871 mmap = repo.manifest.read(change[0])
871 mmap = repo.manifest.read(change[0])
872
872
873 for src, abs, rel, exact in walk(repo, pats, opts, node=node):
873 for src, abs, rel, exact in walk(repo, pats, opts, node=node):
874 f = repo.file(abs)
874 f = repo.file(abs)
875 if not opts['text'] and util.binary(f.read(mmap[abs])):
875 if not opts['text'] and util.binary(f.read(mmap[abs])):
876 ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
876 ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
877 continue
877 continue
878
878
879 lines = f.annotate(mmap[abs])
879 lines = f.annotate(mmap[abs])
880 pieces = []
880 pieces = []
881
881
882 for o, f in opmap:
882 for o, f in opmap:
883 if opts[o]:
883 if opts[o]:
884 l = [f(n) for n, dummy in lines]
884 l = [f(n) for n, dummy in lines]
885 if l:
885 if l:
886 m = max(map(len, l))
886 m = max(map(len, l))
887 pieces.append(["%*s" % (m, x) for x in l])
887 pieces.append(["%*s" % (m, x) for x in l])
888
888
889 if pieces:
889 if pieces:
890 for p, l in zip(zip(*pieces), lines):
890 for p, l in zip(zip(*pieces), lines):
891 ui.write("%s: %s" % (" ".join(p), l[1]))
891 ui.write("%s: %s" % (" ".join(p), l[1]))
892
892
893 def archive(ui, repo, dest, **opts):
893 def archive(ui, repo, dest, **opts):
894 '''create unversioned archive of a repository revision
894 '''create unversioned archive of a repository revision
895
895
896 By default, the revision used is the parent of the working
896 By default, the revision used is the parent of the working
897 directory; use "-r" to specify a different revision.
897 directory; use "-r" to specify a different revision.
898
898
899 To specify the type of archive to create, use "-t". Valid
899 To specify the type of archive to create, use "-t". Valid
900 types are:
900 types are:
901
901
902 "files" (default): a directory full of files
902 "files" (default): a directory full of files
903 "tar": tar archive, uncompressed
903 "tar": tar archive, uncompressed
904 "tbz2": tar archive, compressed using bzip2
904 "tbz2": tar archive, compressed using bzip2
905 "tgz": tar archive, compressed using gzip
905 "tgz": tar archive, compressed using gzip
906 "uzip": zip archive, uncompressed
906 "uzip": zip archive, uncompressed
907 "zip": zip archive, compressed using deflate
907 "zip": zip archive, compressed using deflate
908
908
909 The exact name of the destination archive or directory is given
909 The exact name of the destination archive or directory is given
910 using a format string; see "hg help export" for details.
910 using a format string; see "hg help export" for details.
911
911
912 Each member added to an archive file has a directory prefix
912 Each member added to an archive file has a directory prefix
913 prepended. Use "-p" to specify a format string for the prefix.
913 prepended. Use "-p" to specify a format string for the prefix.
914 The default is the basename of the archive, with suffixes removed.
914 The default is the basename of the archive, with suffixes removed.
915 '''
915 '''
916
916
917 if opts['rev']:
917 if opts['rev']:
918 node = repo.lookup(opts['rev'])
918 node = repo.lookup(opts['rev'])
919 else:
919 else:
920 node, p2 = repo.dirstate.parents()
920 node, p2 = repo.dirstate.parents()
921 if p2 != nullid:
921 if p2 != nullid:
922 raise util.Abort(_('uncommitted merge - please provide a '
922 raise util.Abort(_('uncommitted merge - please provide a '
923 'specific revision'))
923 'specific revision'))
924
924
925 dest = make_filename(repo, repo.changelog, dest, node)
925 dest = make_filename(repo, repo.changelog, dest, node)
926 prefix = make_filename(repo, repo.changelog, opts['prefix'], node)
926 prefix = make_filename(repo, repo.changelog, opts['prefix'], node)
927 if os.path.realpath(dest) == repo.root:
927 if os.path.realpath(dest) == repo.root:
928 raise util.Abort(_('repository root cannot be destination'))
928 raise util.Abort(_('repository root cannot be destination'))
929 _, matchfn, _ = matchpats(repo, [], opts)
929 _, matchfn, _ = matchpats(repo, [], opts)
930 archival.archive(repo, dest, node, opts.get('type') or 'files',
930 archival.archive(repo, dest, node, opts.get('type') or 'files',
931 not opts['no_decode'], matchfn, prefix)
931 not opts['no_decode'], matchfn, prefix)
932
932
933 def bundle(ui, repo, fname, dest="default-push", **opts):
933 def bundle(ui, repo, fname, dest="default-push", **opts):
934 """create a changegroup file
934 """create a changegroup file
935
935
936 Generate a compressed changegroup file collecting all changesets
936 Generate a compressed changegroup file collecting all changesets
937 not found in the other repository.
937 not found in the other repository.
938
938
939 This file can then be transferred using conventional means and
939 This file can then be transferred using conventional means and
940 applied to another repository with the unbundle command. This is
940 applied to another repository with the unbundle command. This is
941 useful when native push and pull are not available or when
941 useful when native push and pull are not available or when
942 exporting an entire repository is undesirable. The standard file
942 exporting an entire repository is undesirable. The standard file
943 extension is ".hg".
943 extension is ".hg".
944
944
945 Unlike import/export, this exactly preserves all changeset
945 Unlike import/export, this exactly preserves all changeset
946 contents including permissions, rename data, and revision history.
946 contents including permissions, rename data, and revision history.
947 """
947 """
948 dest = ui.expandpath(dest)
948 dest = ui.expandpath(dest)
949 other = hg.repository(ui, dest)
949 other = hg.repository(ui, dest)
950 o = repo.findoutgoing(other, force=opts['force'])
950 o = repo.findoutgoing(other, force=opts['force'])
951 cg = repo.changegroup(o, 'bundle')
951 cg = repo.changegroup(o, 'bundle')
952 write_bundle(cg, fname)
952 write_bundle(cg, fname)
953
953
954 def cat(ui, repo, file1, *pats, **opts):
954 def cat(ui, repo, file1, *pats, **opts):
955 """output the latest or given revisions of files
955 """output the latest or given revisions of files
956
956
957 Print the specified files as they were at the given revision.
957 Print the specified files as they were at the given revision.
958 If no revision is given then the tip is used.
958 If no revision is given then the tip is used.
959
959
960 Output may be to a file, in which case the name of the file is
960 Output may be to a file, in which case the name of the file is
961 given using a format string. The formatting rules are the same as
961 given using a format string. The formatting rules are the same as
962 for the export command, with the following additions:
962 for the export command, with the following additions:
963
963
964 %s basename of file being printed
964 %s basename of file being printed
965 %d dirname of file being printed, or '.' if in repo root
965 %d dirname of file being printed, or '.' if in repo root
966 %p root-relative path name of file being printed
966 %p root-relative path name of file being printed
967 """
967 """
968 mf = {}
968 mf = {}
969 rev = opts['rev']
969 rev = opts['rev']
970 if rev:
970 if rev:
971 node = repo.lookup(rev)
971 node = repo.lookup(rev)
972 else:
972 else:
973 node = repo.changelog.tip()
973 node = repo.changelog.tip()
974 change = repo.changelog.read(node)
974 change = repo.changelog.read(node)
975 mf = repo.manifest.read(change[0])
975 mf = repo.manifest.read(change[0])
976 for src, abs, rel, exact in walk(repo, (file1,) + pats, opts, node):
976 for src, abs, rel, exact in walk(repo, (file1,) + pats, opts, node):
977 r = repo.file(abs)
977 r = repo.file(abs)
978 n = mf[abs]
978 n = mf[abs]
979 fp = make_file(repo, r, opts['output'], node=n, pathname=abs)
979 fp = make_file(repo, r, opts['output'], node=n, pathname=abs)
980 fp.write(r.read(n))
980 fp.write(r.read(n))
981
981
982 def clone(ui, source, dest=None, **opts):
982 def clone(ui, source, dest=None, **opts):
983 """make a copy of an existing repository
983 """make a copy of an existing repository
984
984
985 Create a copy of an existing repository in a new directory.
985 Create a copy of an existing repository in a new directory.
986
986
987 If no destination directory name is specified, it defaults to the
987 If no destination directory name is specified, it defaults to the
988 basename of the source.
988 basename of the source.
989
989
990 The location of the source is added to the new repository's
990 The location of the source is added to the new repository's
991 .hg/hgrc file, as the default to be used for future pulls.
991 .hg/hgrc file, as the default to be used for future pulls.
992
992
993 For efficiency, hardlinks are used for cloning whenever the source
993 For efficiency, hardlinks are used for cloning whenever the source
994 and destination are on the same filesystem. Some filesystems,
994 and destination are on the same filesystem. Some filesystems,
995 such as AFS, implement hardlinking incorrectly, but do not report
995 such as AFS, implement hardlinking incorrectly, but do not report
996 errors. In these cases, use the --pull option to avoid
996 errors. In these cases, use the --pull option to avoid
997 hardlinking.
997 hardlinking.
998
998
999 See pull for valid source format details.
999 See pull for valid source format details.
1000 """
1000 """
1001 if dest is None:
1001 if dest is None:
1002 dest = os.path.basename(os.path.normpath(source))
1002 dest = os.path.basename(os.path.normpath(source))
1003
1003
1004 if os.path.exists(dest):
1004 if os.path.exists(dest):
1005 raise util.Abort(_("destination '%s' already exists"), dest)
1005 raise util.Abort(_("destination '%s' already exists"), dest)
1006
1006
1007 dest = os.path.realpath(dest)
1007 dest = os.path.realpath(dest)
1008
1008
1009 class Dircleanup(object):
1009 class Dircleanup(object):
1010 def __init__(self, dir_):
1010 def __init__(self, dir_):
1011 self.rmtree = shutil.rmtree
1011 self.rmtree = shutil.rmtree
1012 self.dir_ = dir_
1012 self.dir_ = dir_
1013 os.mkdir(dir_)
1013 os.mkdir(dir_)
1014 def close(self):
1014 def close(self):
1015 self.dir_ = None
1015 self.dir_ = None
1016 def __del__(self):
1016 def __del__(self):
1017 if self.dir_:
1017 if self.dir_:
1018 self.rmtree(self.dir_, True)
1018 self.rmtree(self.dir_, True)
1019
1019
1020 if opts['ssh']:
1020 if opts['ssh']:
1021 ui.setconfig("ui", "ssh", opts['ssh'])
1021 ui.setconfig("ui", "ssh", opts['ssh'])
1022 if opts['remotecmd']:
1022 if opts['remotecmd']:
1023 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
1023 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
1024
1024
1025 source = ui.expandpath(source)
1025 source = ui.expandpath(source)
1026
1026
1027 d = Dircleanup(dest)
1027 d = Dircleanup(dest)
1028 abspath = source
1028 abspath = source
1029 other = hg.repository(ui, source)
1029 other = hg.repository(ui, source)
1030
1030
1031 copy = False
1031 copy = False
1032 if other.dev() != -1:
1032 if other.dev() != -1:
1033 abspath = os.path.abspath(source)
1033 abspath = os.path.abspath(source)
1034 if not opts['pull'] and not opts['rev']:
1034 if not opts['pull'] and not opts['rev']:
1035 copy = True
1035 copy = True
1036
1036
1037 if copy:
1037 if copy:
1038 try:
1038 try:
1039 # we use a lock here because if we race with commit, we
1039 # we use a lock here because if we race with commit, we
1040 # can end up with extra data in the cloned revlogs that's
1040 # can end up with extra data in the cloned revlogs that's
1041 # not pointed to by changesets, thus causing verify to
1041 # not pointed to by changesets, thus causing verify to
1042 # fail
1042 # fail
1043 l1 = other.lock()
1043 l1 = other.lock()
1044 except lock.LockException:
1044 except lock.LockException:
1045 copy = False
1045 copy = False
1046
1046
1047 if copy:
1047 if copy:
1048 # we lock here to avoid premature writing to the target
1048 # we lock here to avoid premature writing to the target
1049 os.mkdir(os.path.join(dest, ".hg"))
1049 os.mkdir(os.path.join(dest, ".hg"))
1050 l2 = lock.lock(os.path.join(dest, ".hg", "lock"))
1050 l2 = lock.lock(os.path.join(dest, ".hg", "lock"))
1051
1051
1052 files = "data 00manifest.d 00manifest.i 00changelog.d 00changelog.i"
1052 files = "data 00manifest.d 00manifest.i 00changelog.d 00changelog.i"
1053 for f in files.split():
1053 for f in files.split():
1054 src = os.path.join(source, ".hg", f)
1054 src = os.path.join(source, ".hg", f)
1055 dst = os.path.join(dest, ".hg", f)
1055 dst = os.path.join(dest, ".hg", f)
1056 try:
1056 try:
1057 util.copyfiles(src, dst)
1057 util.copyfiles(src, dst)
1058 except OSError, inst:
1058 except OSError, inst:
1059 if inst.errno != errno.ENOENT:
1059 if inst.errno != errno.ENOENT:
1060 raise
1060 raise
1061
1061
1062 repo = hg.repository(ui, dest)
1062 repo = hg.repository(ui, dest)
1063
1063
1064 else:
1064 else:
1065 revs = None
1065 revs = None
1066 if opts['rev']:
1066 if opts['rev']:
1067 if not other.local():
1067 if not other.local():
1068 error = _("clone -r not supported yet for remote repositories.")
1068 error = _("clone -r not supported yet for remote repositories.")
1069 raise util.Abort(error)
1069 raise util.Abort(error)
1070 else:
1070 else:
1071 revs = [other.lookup(rev) for rev in opts['rev']]
1071 revs = [other.lookup(rev) for rev in opts['rev']]
1072 repo = hg.repository(ui, dest, create=1)
1072 repo = hg.repository(ui, dest, create=1)
1073 repo.pull(other, heads = revs)
1073 repo.pull(other, heads = revs)
1074
1074
1075 f = repo.opener("hgrc", "w", text=True)
1075 f = repo.opener("hgrc", "w", text=True)
1076 f.write("[paths]\n")
1076 f.write("[paths]\n")
1077 f.write("default = %s\n" % abspath)
1077 f.write("default = %s\n" % abspath)
1078 f.close()
1078 f.close()
1079
1079
1080 if not opts['noupdate']:
1080 if not opts['noupdate']:
1081 update(repo.ui, repo)
1081 update(repo.ui, repo)
1082
1082
1083 d.close()
1083 d.close()
1084
1084
1085 def commit(ui, repo, *pats, **opts):
1085 def commit(ui, repo, *pats, **opts):
1086 """commit the specified files or all outstanding changes
1086 """commit the specified files or all outstanding changes
1087
1087
1088 Commit changes to the given files into the repository.
1088 Commit changes to the given files into the repository.
1089
1089
1090 If a list of files is omitted, all changes reported by "hg status"
1090 If a list of files is omitted, all changes reported by "hg status"
1091 will be committed.
1091 will be committed.
1092
1092
1093 If no commit message is specified, the editor configured in your hgrc
1093 If no commit message is specified, the editor configured in your hgrc
1094 or in the EDITOR environment variable is started to enter a message.
1094 or in the EDITOR environment variable is started to enter a message.
1095 """
1095 """
1096 message = opts['message']
1096 message = opts['message']
1097 logfile = opts['logfile']
1097 logfile = opts['logfile']
1098
1098
1099 if message and logfile:
1099 if message and logfile:
1100 raise util.Abort(_('options --message and --logfile are mutually '
1100 raise util.Abort(_('options --message and --logfile are mutually '
1101 'exclusive'))
1101 'exclusive'))
1102 if not message and logfile:
1102 if not message and logfile:
1103 try:
1103 try:
1104 if logfile == '-':
1104 if logfile == '-':
1105 message = sys.stdin.read()
1105 message = sys.stdin.read()
1106 else:
1106 else:
1107 message = open(logfile).read()
1107 message = open(logfile).read()
1108 except IOError, inst:
1108 except IOError, inst:
1109 raise util.Abort(_("can't read commit message '%s': %s") %
1109 raise util.Abort(_("can't read commit message '%s': %s") %
1110 (logfile, inst.strerror))
1110 (logfile, inst.strerror))
1111
1111
1112 if opts['addremove']:
1112 if opts['addremove']:
1113 addremove(ui, repo, *pats, **opts)
1113 addremove(ui, repo, *pats, **opts)
1114 fns, match, anypats = matchpats(repo, pats, opts)
1114 fns, match, anypats = matchpats(repo, pats, opts)
1115 if pats:
1115 if pats:
1116 modified, added, removed, deleted, unknown = (
1116 modified, added, removed, deleted, unknown = (
1117 repo.changes(files=fns, match=match))
1117 repo.changes(files=fns, match=match))
1118 files = modified + added + removed
1118 files = modified + added + removed
1119 else:
1119 else:
1120 files = []
1120 files = []
1121 try:
1121 try:
1122 repo.commit(files, message, opts['user'], opts['date'], match)
1122 repo.commit(files, message, opts['user'], opts['date'], match)
1123 except ValueError, inst:
1123 except ValueError, inst:
1124 raise util.Abort(str(inst))
1124 raise util.Abort(str(inst))
1125
1125
1126 def docopy(ui, repo, pats, opts, wlock):
1126 def docopy(ui, repo, pats, opts, wlock):
1127 # called with the repo lock held
1127 # called with the repo lock held
1128 cwd = repo.getcwd()
1128 cwd = repo.getcwd()
1129 errors = 0
1129 errors = 0
1130 copied = []
1130 copied = []
1131 targets = {}
1131 targets = {}
1132
1132
1133 def okaytocopy(abs, rel, exact):
1133 def okaytocopy(abs, rel, exact):
1134 reasons = {'?': _('is not managed'),
1134 reasons = {'?': _('is not managed'),
1135 'a': _('has been marked for add'),
1135 'a': _('has been marked for add'),
1136 'r': _('has been marked for remove')}
1136 'r': _('has been marked for remove')}
1137 state = repo.dirstate.state(abs)
1137 state = repo.dirstate.state(abs)
1138 reason = reasons.get(state)
1138 reason = reasons.get(state)
1139 if reason:
1139 if reason:
1140 if state == 'a':
1140 if state == 'a':
1141 origsrc = repo.dirstate.copied(abs)
1141 origsrc = repo.dirstate.copied(abs)
1142 if origsrc is not None:
1142 if origsrc is not None:
1143 return origsrc
1143 return origsrc
1144 if exact:
1144 if exact:
1145 ui.warn(_('%s: not copying - file %s\n') % (rel, reason))
1145 ui.warn(_('%s: not copying - file %s\n') % (rel, reason))
1146 else:
1146 else:
1147 return abs
1147 return abs
1148
1148
1149 def copy(origsrc, abssrc, relsrc, target, exact):
1149 def copy(origsrc, abssrc, relsrc, target, exact):
1150 abstarget = util.canonpath(repo.root, cwd, target)
1150 abstarget = util.canonpath(repo.root, cwd, target)
1151 reltarget = util.pathto(cwd, abstarget)
1151 reltarget = util.pathto(cwd, abstarget)
1152 prevsrc = targets.get(abstarget)
1152 prevsrc = targets.get(abstarget)
1153 if prevsrc is not None:
1153 if prevsrc is not None:
1154 ui.warn(_('%s: not overwriting - %s collides with %s\n') %
1154 ui.warn(_('%s: not overwriting - %s collides with %s\n') %
1155 (reltarget, abssrc, prevsrc))
1155 (reltarget, abssrc, prevsrc))
1156 return
1156 return
1157 if (not opts['after'] and os.path.exists(reltarget) or
1157 if (not opts['after'] and os.path.exists(reltarget) or
1158 opts['after'] and repo.dirstate.state(abstarget) not in '?r'):
1158 opts['after'] and repo.dirstate.state(abstarget) not in '?r'):
1159 if not opts['force']:
1159 if not opts['force']:
1160 ui.warn(_('%s: not overwriting - file exists\n') %
1160 ui.warn(_('%s: not overwriting - file exists\n') %
1161 reltarget)
1161 reltarget)
1162 return
1162 return
1163 if not opts['after']:
1163 if not opts['after']:
1164 os.unlink(reltarget)
1164 os.unlink(reltarget)
1165 if opts['after']:
1165 if opts['after']:
1166 if not os.path.exists(reltarget):
1166 if not os.path.exists(reltarget):
1167 return
1167 return
1168 else:
1168 else:
1169 targetdir = os.path.dirname(reltarget) or '.'
1169 targetdir = os.path.dirname(reltarget) or '.'
1170 if not os.path.isdir(targetdir):
1170 if not os.path.isdir(targetdir):
1171 os.makedirs(targetdir)
1171 os.makedirs(targetdir)
1172 try:
1172 try:
1173 restore = repo.dirstate.state(abstarget) == 'r'
1173 restore = repo.dirstate.state(abstarget) == 'r'
1174 if restore:
1174 if restore:
1175 repo.undelete([abstarget], wlock)
1175 repo.undelete([abstarget], wlock)
1176 try:
1176 try:
1177 shutil.copyfile(relsrc, reltarget)
1177 shutil.copyfile(relsrc, reltarget)
1178 shutil.copymode(relsrc, reltarget)
1178 shutil.copymode(relsrc, reltarget)
1179 restore = False
1179 restore = False
1180 finally:
1180 finally:
1181 if restore:
1181 if restore:
1182 repo.remove([abstarget], wlock)
1182 repo.remove([abstarget], wlock)
1183 except shutil.Error, inst:
1183 except shutil.Error, inst:
1184 raise util.Abort(str(inst))
1184 raise util.Abort(str(inst))
1185 except IOError, inst:
1185 except IOError, inst:
1186 if inst.errno == errno.ENOENT:
1186 if inst.errno == errno.ENOENT:
1187 ui.warn(_('%s: deleted in working copy\n') % relsrc)
1187 ui.warn(_('%s: deleted in working copy\n') % relsrc)
1188 else:
1188 else:
1189 ui.warn(_('%s: cannot copy - %s\n') %
1189 ui.warn(_('%s: cannot copy - %s\n') %
1190 (relsrc, inst.strerror))
1190 (relsrc, inst.strerror))
1191 errors += 1
1191 errors += 1
1192 return
1192 return
1193 if ui.verbose or not exact:
1193 if ui.verbose or not exact:
1194 ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
1194 ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
1195 targets[abstarget] = abssrc
1195 targets[abstarget] = abssrc
1196 if abstarget != origsrc:
1196 if abstarget != origsrc:
1197 repo.copy(origsrc, abstarget, wlock)
1197 repo.copy(origsrc, abstarget, wlock)
1198 copied.append((abssrc, relsrc, exact))
1198 copied.append((abssrc, relsrc, exact))
1199
1199
1200 def targetpathfn(pat, dest, srcs):
1200 def targetpathfn(pat, dest, srcs):
1201 if os.path.isdir(pat):
1201 if os.path.isdir(pat):
1202 abspfx = util.canonpath(repo.root, cwd, pat)
1202 abspfx = util.canonpath(repo.root, cwd, pat)
1203 if destdirexists:
1203 if destdirexists:
1204 striplen = len(os.path.split(abspfx)[0])
1204 striplen = len(os.path.split(abspfx)[0])
1205 else:
1205 else:
1206 striplen = len(abspfx)
1206 striplen = len(abspfx)
1207 if striplen:
1207 if striplen:
1208 striplen += len(os.sep)
1208 striplen += len(os.sep)
1209 res = lambda p: os.path.join(dest, p[striplen:])
1209 res = lambda p: os.path.join(dest, p[striplen:])
1210 elif destdirexists:
1210 elif destdirexists:
1211 res = lambda p: os.path.join(dest, os.path.basename(p))
1211 res = lambda p: os.path.join(dest, os.path.basename(p))
1212 else:
1212 else:
1213 res = lambda p: dest
1213 res = lambda p: dest
1214 return res
1214 return res
1215
1215
1216 def targetpathafterfn(pat, dest, srcs):
1216 def targetpathafterfn(pat, dest, srcs):
1217 if util.patkind(pat, None)[0]:
1217 if util.patkind(pat, None)[0]:
1218 # a mercurial pattern
1218 # a mercurial pattern
1219 res = lambda p: os.path.join(dest, os.path.basename(p))
1219 res = lambda p: os.path.join(dest, os.path.basename(p))
1220 else:
1220 else:
1221 abspfx = util.canonpath(repo.root, cwd, pat)
1221 abspfx = util.canonpath(repo.root, cwd, pat)
1222 if len(abspfx) < len(srcs[0][0]):
1222 if len(abspfx) < len(srcs[0][0]):
1223 # A directory. Either the target path contains the last
1223 # A directory. Either the target path contains the last
1224 # component of the source path or it does not.
1224 # component of the source path or it does not.
1225 def evalpath(striplen):
1225 def evalpath(striplen):
1226 score = 0
1226 score = 0
1227 for s in srcs:
1227 for s in srcs:
1228 t = os.path.join(dest, s[0][striplen:])
1228 t = os.path.join(dest, s[0][striplen:])
1229 if os.path.exists(t):
1229 if os.path.exists(t):
1230 score += 1
1230 score += 1
1231 return score
1231 return score
1232
1232
1233 striplen = len(abspfx)
1233 striplen = len(abspfx)
1234 if striplen:
1234 if striplen:
1235 striplen += len(os.sep)
1235 striplen += len(os.sep)
1236 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
1236 if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
1237 score = evalpath(striplen)
1237 score = evalpath(striplen)
1238 striplen1 = len(os.path.split(abspfx)[0])
1238 striplen1 = len(os.path.split(abspfx)[0])
1239 if striplen1:
1239 if striplen1:
1240 striplen1 += len(os.sep)
1240 striplen1 += len(os.sep)
1241 if evalpath(striplen1) > score:
1241 if evalpath(striplen1) > score:
1242 striplen = striplen1
1242 striplen = striplen1
1243 res = lambda p: os.path.join(dest, p[striplen:])
1243 res = lambda p: os.path.join(dest, p[striplen:])
1244 else:
1244 else:
1245 # a file
1245 # a file
1246 if destdirexists:
1246 if destdirexists:
1247 res = lambda p: os.path.join(dest, os.path.basename(p))
1247 res = lambda p: os.path.join(dest, os.path.basename(p))
1248 else:
1248 else:
1249 res = lambda p: dest
1249 res = lambda p: dest
1250 return res
1250 return res
1251
1251
1252
1252
1253 pats = list(pats)
1253 pats = list(pats)
1254 if not pats:
1254 if not pats:
1255 raise util.Abort(_('no source or destination specified'))
1255 raise util.Abort(_('no source or destination specified'))
1256 if len(pats) == 1:
1256 if len(pats) == 1:
1257 raise util.Abort(_('no destination specified'))
1257 raise util.Abort(_('no destination specified'))
1258 dest = pats.pop()
1258 dest = pats.pop()
1259 destdirexists = os.path.isdir(dest)
1259 destdirexists = os.path.isdir(dest)
1260 if (len(pats) > 1 or util.patkind(pats[0], None)[0]) and not destdirexists:
1260 if (len(pats) > 1 or util.patkind(pats[0], None)[0]) and not destdirexists:
1261 raise util.Abort(_('with multiple sources, destination must be an '
1261 raise util.Abort(_('with multiple sources, destination must be an '
1262 'existing directory'))
1262 'existing directory'))
1263 if opts['after']:
1263 if opts['after']:
1264 tfn = targetpathafterfn
1264 tfn = targetpathafterfn
1265 else:
1265 else:
1266 tfn = targetpathfn
1266 tfn = targetpathfn
1267 copylist = []
1267 copylist = []
1268 for pat in pats:
1268 for pat in pats:
1269 srcs = []
1269 srcs = []
1270 for tag, abssrc, relsrc, exact in walk(repo, [pat], opts):
1270 for tag, abssrc, relsrc, exact in walk(repo, [pat], opts):
1271 origsrc = okaytocopy(abssrc, relsrc, exact)
1271 origsrc = okaytocopy(abssrc, relsrc, exact)
1272 if origsrc:
1272 if origsrc:
1273 srcs.append((origsrc, abssrc, relsrc, exact))
1273 srcs.append((origsrc, abssrc, relsrc, exact))
1274 if not srcs:
1274 if not srcs:
1275 continue
1275 continue
1276 copylist.append((tfn(pat, dest, srcs), srcs))
1276 copylist.append((tfn(pat, dest, srcs), srcs))
1277 if not copylist:
1277 if not copylist:
1278 raise util.Abort(_('no files to copy'))
1278 raise util.Abort(_('no files to copy'))
1279
1279
1280 for targetpath, srcs in copylist:
1280 for targetpath, srcs in copylist:
1281 for origsrc, abssrc, relsrc, exact in srcs:
1281 for origsrc, abssrc, relsrc, exact in srcs:
1282 copy(origsrc, abssrc, relsrc, targetpath(abssrc), exact)
1282 copy(origsrc, abssrc, relsrc, targetpath(abssrc), exact)
1283
1283
1284 if errors:
1284 if errors:
1285 ui.warn(_('(consider using --after)\n'))
1285 ui.warn(_('(consider using --after)\n'))
1286 return errors, copied
1286 return errors, copied
1287
1287
1288 def copy(ui, repo, *pats, **opts):
1288 def copy(ui, repo, *pats, **opts):
1289 """mark files as copied for the next commit
1289 """mark files as copied for the next commit
1290
1290
1291 Mark dest as having copies of source files. If dest is a
1291 Mark dest as having copies of source files. If dest is a
1292 directory, copies are put in that directory. If dest is a file,
1292 directory, copies are put in that directory. If dest is a file,
1293 there can only be one source.
1293 there can only be one source.
1294
1294
1295 By default, this command copies the contents of files as they
1295 By default, this command copies the contents of files as they
1296 stand in the working directory. If invoked with --after, the
1296 stand in the working directory. If invoked with --after, the
1297 operation is recorded, but no copying is performed.
1297 operation is recorded, but no copying is performed.
1298
1298
1299 This command takes effect in the next commit.
1299 This command takes effect in the next commit.
1300
1300
1301 NOTE: This command should be treated as experimental. While it
1301 NOTE: This command should be treated as experimental. While it
1302 should properly record copied files, this information is not yet
1302 should properly record copied files, this information is not yet
1303 fully used by merge, nor fully reported by log.
1303 fully used by merge, nor fully reported by log.
1304 """
1304 """
1305 wlock = repo.wlock(0)
1305 wlock = repo.wlock(0)
1306 errs, copied = docopy(ui, repo, pats, opts, wlock)
1306 errs, copied = docopy(ui, repo, pats, opts, wlock)
1307 return errs
1307 return errs
1308
1308
1309 def debugancestor(ui, index, rev1, rev2):
1309 def debugancestor(ui, index, rev1, rev2):
1310 """find the ancestor revision of two revisions in a given index"""
1310 """find the ancestor revision of two revisions in a given index"""
1311 r = revlog.revlog(util.opener(os.getcwd(), audit=False), index, "", 0)
1311 r = revlog.revlog(util.opener(os.getcwd(), audit=False), index, "", 0)
1312 a = r.ancestor(r.lookup(rev1), r.lookup(rev2))
1312 a = r.ancestor(r.lookup(rev1), r.lookup(rev2))
1313 ui.write("%d:%s\n" % (r.rev(a), hex(a)))
1313 ui.write("%d:%s\n" % (r.rev(a), hex(a)))
1314
1314
1315 def debugcomplete(ui, cmd='', **opts):
1315 def debugcomplete(ui, cmd='', **opts):
1316 """returns the completion list associated with the given command"""
1316 """returns the completion list associated with the given command"""
1317
1317
1318 if opts['options']:
1318 if opts['options']:
1319 options = []
1319 options = []
1320 otables = [globalopts]
1320 otables = [globalopts]
1321 if cmd:
1321 if cmd:
1322 aliases, entry = find(cmd)
1322 aliases, entry = find(cmd)
1323 otables.append(entry[1])
1323 otables.append(entry[1])
1324 for t in otables:
1324 for t in otables:
1325 for o in t:
1325 for o in t:
1326 if o[0]:
1326 if o[0]:
1327 options.append('-%s' % o[0])
1327 options.append('-%s' % o[0])
1328 options.append('--%s' % o[1])
1328 options.append('--%s' % o[1])
1329 ui.write("%s\n" % "\n".join(options))
1329 ui.write("%s\n" % "\n".join(options))
1330 return
1330 return
1331
1331
1332 clist = findpossible(cmd).keys()
1332 clist = findpossible(cmd).keys()
1333 clist.sort()
1333 clist.sort()
1334 ui.write("%s\n" % "\n".join(clist))
1334 ui.write("%s\n" % "\n".join(clist))
1335
1335
1336 def debugrebuildstate(ui, repo, rev=None):
1336 def debugrebuildstate(ui, repo, rev=None):
1337 """rebuild the dirstate as it would look like for the given revision"""
1337 """rebuild the dirstate as it would look like for the given revision"""
1338 if not rev:
1338 if not rev:
1339 rev = repo.changelog.tip()
1339 rev = repo.changelog.tip()
1340 else:
1340 else:
1341 rev = repo.lookup(rev)
1341 rev = repo.lookup(rev)
1342 change = repo.changelog.read(rev)
1342 change = repo.changelog.read(rev)
1343 n = change[0]
1343 n = change[0]
1344 files = repo.manifest.readflags(n)
1344 files = repo.manifest.readflags(n)
1345 wlock = repo.wlock()
1345 wlock = repo.wlock()
1346 repo.dirstate.rebuild(rev, files.iteritems())
1346 repo.dirstate.rebuild(rev, files.iteritems())
1347
1347
1348 def debugcheckstate(ui, repo):
1348 def debugcheckstate(ui, repo):
1349 """validate the correctness of the current dirstate"""
1349 """validate the correctness of the current dirstate"""
1350 parent1, parent2 = repo.dirstate.parents()
1350 parent1, parent2 = repo.dirstate.parents()
1351 repo.dirstate.read()
1351 repo.dirstate.read()
1352 dc = repo.dirstate.map
1352 dc = repo.dirstate.map
1353 keys = dc.keys()
1353 keys = dc.keys()
1354 keys.sort()
1354 keys.sort()
1355 m1n = repo.changelog.read(parent1)[0]
1355 m1n = repo.changelog.read(parent1)[0]
1356 m2n = repo.changelog.read(parent2)[0]
1356 m2n = repo.changelog.read(parent2)[0]
1357 m1 = repo.manifest.read(m1n)
1357 m1 = repo.manifest.read(m1n)
1358 m2 = repo.manifest.read(m2n)
1358 m2 = repo.manifest.read(m2n)
1359 errors = 0
1359 errors = 0
1360 for f in dc:
1360 for f in dc:
1361 state = repo.dirstate.state(f)
1361 state = repo.dirstate.state(f)
1362 if state in "nr" and f not in m1:
1362 if state in "nr" and f not in m1:
1363 ui.warn(_("%s in state %s, but not in manifest1\n") % (f, state))
1363 ui.warn(_("%s in state %s, but not in manifest1\n") % (f, state))
1364 errors += 1
1364 errors += 1
1365 if state in "a" and f in m1:
1365 if state in "a" and f in m1:
1366 ui.warn(_("%s in state %s, but also in manifest1\n") % (f, state))
1366 ui.warn(_("%s in state %s, but also in manifest1\n") % (f, state))
1367 errors += 1
1367 errors += 1
1368 if state in "m" and f not in m1 and f not in m2:
1368 if state in "m" and f not in m1 and f not in m2:
1369 ui.warn(_("%s in state %s, but not in either manifest\n") %
1369 ui.warn(_("%s in state %s, but not in either manifest\n") %
1370 (f, state))
1370 (f, state))
1371 errors += 1
1371 errors += 1
1372 for f in m1:
1372 for f in m1:
1373 state = repo.dirstate.state(f)
1373 state = repo.dirstate.state(f)
1374 if state not in "nrm":
1374 if state not in "nrm":
1375 ui.warn(_("%s in manifest1, but listed as state %s") % (f, state))
1375 ui.warn(_("%s in manifest1, but listed as state %s") % (f, state))
1376 errors += 1
1376 errors += 1
1377 if errors:
1377 if errors:
1378 error = _(".hg/dirstate inconsistent with current parent's manifest")
1378 error = _(".hg/dirstate inconsistent with current parent's manifest")
1379 raise util.Abort(error)
1379 raise util.Abort(error)
1380
1380
1381 def debugconfig(ui, repo):
1381 def debugconfig(ui, repo):
1382 """show combined config settings from all hgrc files"""
1382 """show combined config settings from all hgrc files"""
1383 for section, name, value in ui.walkconfig():
1383 for section, name, value in ui.walkconfig():
1384 ui.write('%s.%s=%s\n' % (section, name, value))
1384 ui.write('%s.%s=%s\n' % (section, name, value))
1385
1385
1386 def debugsetparents(ui, repo, rev1, rev2=None):
1386 def debugsetparents(ui, repo, rev1, rev2=None):
1387 """manually set the parents of the current working directory
1387 """manually set the parents of the current working directory
1388
1388
1389 This is useful for writing repository conversion tools, but should
1389 This is useful for writing repository conversion tools, but should
1390 be used with care.
1390 be used with care.
1391 """
1391 """
1392
1392
1393 if not rev2:
1393 if not rev2:
1394 rev2 = hex(nullid)
1394 rev2 = hex(nullid)
1395
1395
1396 repo.dirstate.setparents(repo.lookup(rev1), repo.lookup(rev2))
1396 repo.dirstate.setparents(repo.lookup(rev1), repo.lookup(rev2))
1397
1397
1398 def debugstate(ui, repo):
1398 def debugstate(ui, repo):
1399 """show the contents of the current dirstate"""
1399 """show the contents of the current dirstate"""
1400 repo.dirstate.read()
1400 repo.dirstate.read()
1401 dc = repo.dirstate.map
1401 dc = repo.dirstate.map
1402 keys = dc.keys()
1402 keys = dc.keys()
1403 keys.sort()
1403 keys.sort()
1404 for file_ in keys:
1404 for file_ in keys:
1405 ui.write("%c %3o %10d %s %s\n"
1405 ui.write("%c %3o %10d %s %s\n"
1406 % (dc[file_][0], dc[file_][1] & 0777, dc[file_][2],
1406 % (dc[file_][0], dc[file_][1] & 0777, dc[file_][2],
1407 time.strftime("%x %X",
1407 time.strftime("%x %X",
1408 time.localtime(dc[file_][3])), file_))
1408 time.localtime(dc[file_][3])), file_))
1409 for f in repo.dirstate.copies:
1409 for f in repo.dirstate.copies:
1410 ui.write(_("copy: %s -> %s\n") % (repo.dirstate.copies[f], f))
1410 ui.write(_("copy: %s -> %s\n") % (repo.dirstate.copies[f], f))
1411
1411
1412 def debugdata(ui, file_, rev):
1412 def debugdata(ui, file_, rev):
1413 """dump the contents of an data file revision"""
1413 """dump the contents of an data file revision"""
1414 r = revlog.revlog(util.opener(os.getcwd(), audit=False),
1414 r = revlog.revlog(util.opener(os.getcwd(), audit=False),
1415 file_[:-2] + ".i", file_, 0)
1415 file_[:-2] + ".i", file_, 0)
1416 try:
1416 try:
1417 ui.write(r.revision(r.lookup(rev)))
1417 ui.write(r.revision(r.lookup(rev)))
1418 except KeyError:
1418 except KeyError:
1419 raise util.Abort(_('invalid revision identifier %s'), rev)
1419 raise util.Abort(_('invalid revision identifier %s'), rev)
1420
1420
1421 def debugindex(ui, file_):
1421 def debugindex(ui, file_):
1422 """dump the contents of an index file"""
1422 """dump the contents of an index file"""
1423 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_, "", 0)
1423 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_, "", 0)
1424 ui.write(" rev offset length base linkrev" +
1424 ui.write(" rev offset length base linkrev" +
1425 " nodeid p1 p2\n")
1425 " nodeid p1 p2\n")
1426 for i in range(r.count()):
1426 for i in range(r.count()):
1427 node = r.node(i)
1427 node = r.node(i)
1428 pp = r.parents(node)
1428 pp = r.parents(node)
1429 ui.write("% 6d % 9d % 7d % 6d % 7d %s %s %s\n" % (
1429 ui.write("% 6d % 9d % 7d % 6d % 7d %s %s %s\n" % (
1430 i, r.start(i), r.length(i), r.base(i), r.linkrev(node),
1430 i, r.start(i), r.length(i), r.base(i), r.linkrev(node),
1431 short(node), short(pp[0]), short(pp[1])))
1431 short(node), short(pp[0]), short(pp[1])))
1432
1432
1433 def debugindexdot(ui, file_):
1433 def debugindexdot(ui, file_):
1434 """dump an index DAG as a .dot file"""
1434 """dump an index DAG as a .dot file"""
1435 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_, "", 0)
1435 r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_, "", 0)
1436 ui.write("digraph G {\n")
1436 ui.write("digraph G {\n")
1437 for i in range(r.count()):
1437 for i in range(r.count()):
1438 e = r.index[i]
1438 e = r.index[i]
1439 ui.write("\t%d -> %d\n" % (r.rev(e[4]), i))
1439 ui.write("\t%d -> %d\n" % (r.rev(e[4]), i))
1440 if e[5] != nullid:
1440 if e[5] != nullid:
1441 ui.write("\t%d -> %d\n" % (r.rev(e[5]), i))
1441 ui.write("\t%d -> %d\n" % (r.rev(e[5]), i))
1442 ui.write("}\n")
1442 ui.write("}\n")
1443
1443
1444 def debugrename(ui, repo, file, rev=None):
1444 def debugrename(ui, repo, file, rev=None):
1445 """dump rename information"""
1445 """dump rename information"""
1446 r = repo.file(relpath(repo, [file])[0])
1446 r = repo.file(relpath(repo, [file])[0])
1447 if rev:
1447 if rev:
1448 try:
1448 try:
1449 # assume all revision numbers are for changesets
1449 # assume all revision numbers are for changesets
1450 n = repo.lookup(rev)
1450 n = repo.lookup(rev)
1451 change = repo.changelog.read(n)
1451 change = repo.changelog.read(n)
1452 m = repo.manifest.read(change[0])
1452 m = repo.manifest.read(change[0])
1453 n = m[relpath(repo, [file])[0]]
1453 n = m[relpath(repo, [file])[0]]
1454 except (hg.RepoError, KeyError):
1454 except (hg.RepoError, KeyError):
1455 n = r.lookup(rev)
1455 n = r.lookup(rev)
1456 else:
1456 else:
1457 n = r.tip()
1457 n = r.tip()
1458 m = r.renamed(n)
1458 m = r.renamed(n)
1459 if m:
1459 if m:
1460 ui.write(_("renamed from %s:%s\n") % (m[0], hex(m[1])))
1460 ui.write(_("renamed from %s:%s\n") % (m[0], hex(m[1])))
1461 else:
1461 else:
1462 ui.write(_("not renamed\n"))
1462 ui.write(_("not renamed\n"))
1463
1463
1464 def debugwalk(ui, repo, *pats, **opts):
1464 def debugwalk(ui, repo, *pats, **opts):
1465 """show how files match on given patterns"""
1465 """show how files match on given patterns"""
1466 items = list(walk(repo, pats, opts))
1466 items = list(walk(repo, pats, opts))
1467 if not items:
1467 if not items:
1468 return
1468 return
1469 fmt = '%%s %%-%ds %%-%ds %%s' % (
1469 fmt = '%%s %%-%ds %%-%ds %%s' % (
1470 max([len(abs) for (src, abs, rel, exact) in items]),
1470 max([len(abs) for (src, abs, rel, exact) in items]),
1471 max([len(rel) for (src, abs, rel, exact) in items]))
1471 max([len(rel) for (src, abs, rel, exact) in items]))
1472 for src, abs, rel, exact in items:
1472 for src, abs, rel, exact in items:
1473 line = fmt % (src, abs, rel, exact and 'exact' or '')
1473 line = fmt % (src, abs, rel, exact and 'exact' or '')
1474 ui.write("%s\n" % line.rstrip())
1474 ui.write("%s\n" % line.rstrip())
1475
1475
1476 def diff(ui, repo, *pats, **opts):
1476 def diff(ui, repo, *pats, **opts):
1477 """diff repository (or selected files)
1477 """diff repository (or selected files)
1478
1478
1479 Show differences between revisions for the specified files.
1479 Show differences between revisions for the specified files.
1480
1480
1481 Differences between files are shown using the unified diff format.
1481 Differences between files are shown using the unified diff format.
1482
1482
1483 When two revision arguments are given, then changes are shown
1483 When two revision arguments are given, then changes are shown
1484 between those revisions. If only one revision is specified then
1484 between those revisions. If only one revision is specified then
1485 that revision is compared to the working directory, and, when no
1485 that revision is compared to the working directory, and, when no
1486 revisions are specified, the working directory files are compared
1486 revisions are specified, the working directory files are compared
1487 to its parent.
1487 to its parent.
1488
1488
1489 Without the -a option, diff will avoid generating diffs of files
1489 Without the -a option, diff will avoid generating diffs of files
1490 it detects as binary. With -a, diff will generate a diff anyway,
1490 it detects as binary. With -a, diff will generate a diff anyway,
1491 probably with undesirable results.
1491 probably with undesirable results.
1492 """
1492 """
1493 node1, node2 = None, None
1493 node1, node2 = None, None
1494 revs = [repo.lookup(x) for x in opts['rev']]
1494 revs = [repo.lookup(x) for x in opts['rev']]
1495
1495
1496 if len(revs) > 0:
1496 if len(revs) > 0:
1497 node1 = revs[0]
1497 node1 = revs[0]
1498 if len(revs) > 1:
1498 if len(revs) > 1:
1499 node2 = revs[1]
1499 node2 = revs[1]
1500 if len(revs) > 2:
1500 if len(revs) > 2:
1501 raise util.Abort(_("too many revisions to diff"))
1501 raise util.Abort(_("too many revisions to diff"))
1502
1502
1503 fns, matchfn, anypats = matchpats(repo, pats, opts)
1503 fns, matchfn, anypats = matchpats(repo, pats, opts)
1504
1504
1505 dodiff(sys.stdout, ui, repo, node1, node2, fns, match=matchfn,
1505 dodiff(sys.stdout, ui, repo, node1, node2, fns, match=matchfn,
1506 text=opts['text'], opts=opts)
1506 text=opts['text'], opts=opts)
1507
1507
1508 def doexport(ui, repo, changeset, seqno, total, revwidth, opts):
1508 def doexport(ui, repo, changeset, seqno, total, revwidth, opts):
1509 node = repo.lookup(changeset)
1509 node = repo.lookup(changeset)
1510 parents = [p for p in repo.changelog.parents(node) if p != nullid]
1510 parents = [p for p in repo.changelog.parents(node) if p != nullid]
1511 if opts['switch_parent']:
1511 if opts['switch_parent']:
1512 parents.reverse()
1512 parents.reverse()
1513 prev = (parents and parents[0]) or nullid
1513 prev = (parents and parents[0]) or nullid
1514 change = repo.changelog.read(node)
1514 change = repo.changelog.read(node)
1515
1515
1516 fp = make_file(repo, repo.changelog, opts['output'],
1516 fp = make_file(repo, repo.changelog, opts['output'],
1517 node=node, total=total, seqno=seqno,
1517 node=node, total=total, seqno=seqno,
1518 revwidth=revwidth)
1518 revwidth=revwidth)
1519 if fp != sys.stdout:
1519 if fp != sys.stdout:
1520 ui.note("%s\n" % fp.name)
1520 ui.note("%s\n" % fp.name)
1521
1521
1522 fp.write("# HG changeset patch\n")
1522 fp.write("# HG changeset patch\n")
1523 fp.write("# User %s\n" % change[1])
1523 fp.write("# User %s\n" % change[1])
1524 fp.write("# Node ID %s\n" % hex(node))
1524 fp.write("# Node ID %s\n" % hex(node))
1525 fp.write("# Parent %s\n" % hex(prev))
1525 fp.write("# Parent %s\n" % hex(prev))
1526 if len(parents) > 1:
1526 if len(parents) > 1:
1527 fp.write("# Parent %s\n" % hex(parents[1]))
1527 fp.write("# Parent %s\n" % hex(parents[1]))
1528 fp.write(change[4].rstrip())
1528 fp.write(change[4].rstrip())
1529 fp.write("\n\n")
1529 fp.write("\n\n")
1530
1530
1531 dodiff(fp, ui, repo, prev, node, text=opts['text'])
1531 dodiff(fp, ui, repo, prev, node, text=opts['text'])
1532 if fp != sys.stdout:
1532 if fp != sys.stdout:
1533 fp.close()
1533 fp.close()
1534
1534
1535 def export(ui, repo, *changesets, **opts):
1535 def export(ui, repo, *changesets, **opts):
1536 """dump the header and diffs for one or more changesets
1536 """dump the header and diffs for one or more changesets
1537
1537
1538 Print the changeset header and diffs for one or more revisions.
1538 Print the changeset header and diffs for one or more revisions.
1539
1539
1540 The information shown in the changeset header is: author,
1540 The information shown in the changeset header is: author,
1541 changeset hash, parent and commit comment.
1541 changeset hash, parent and commit comment.
1542
1542
1543 Output may be to a file, in which case the name of the file is
1543 Output may be to a file, in which case the name of the file is
1544 given using a format string. The formatting rules are as follows:
1544 given using a format string. The formatting rules are as follows:
1545
1545
1546 %% literal "%" character
1546 %% literal "%" character
1547 %H changeset hash (40 bytes of hexadecimal)
1547 %H changeset hash (40 bytes of hexadecimal)
1548 %N number of patches being generated
1548 %N number of patches being generated
1549 %R changeset revision number
1549 %R changeset revision number
1550 %b basename of the exporting repository
1550 %b basename of the exporting repository
1551 %h short-form changeset hash (12 bytes of hexadecimal)
1551 %h short-form changeset hash (12 bytes of hexadecimal)
1552 %n zero-padded sequence number, starting at 1
1552 %n zero-padded sequence number, starting at 1
1553 %r zero-padded changeset revision number
1553 %r zero-padded changeset revision number
1554
1554
1555 Without the -a option, export will avoid generating diffs of files
1555 Without the -a option, export will avoid generating diffs of files
1556 it detects as binary. With -a, export will generate a diff anyway,
1556 it detects as binary. With -a, export will generate a diff anyway,
1557 probably with undesirable results.
1557 probably with undesirable results.
1558
1558
1559 With the --switch-parent option, the diff will be against the second
1559 With the --switch-parent option, the diff will be against the second
1560 parent. It can be useful to review a merge.
1560 parent. It can be useful to review a merge.
1561 """
1561 """
1562 if not changesets:
1562 if not changesets:
1563 raise util.Abort(_("export requires at least one changeset"))
1563 raise util.Abort(_("export requires at least one changeset"))
1564 seqno = 0
1564 seqno = 0
1565 revs = list(revrange(ui, repo, changesets))
1565 revs = list(revrange(ui, repo, changesets))
1566 total = len(revs)
1566 total = len(revs)
1567 revwidth = max(map(len, revs))
1567 revwidth = max(map(len, revs))
1568 msg = len(revs) > 1 and _("Exporting patches:\n") or _("Exporting patch:\n")
1568 msg = len(revs) > 1 and _("Exporting patches:\n") or _("Exporting patch:\n")
1569 ui.note(msg)
1569 ui.note(msg)
1570 for cset in revs:
1570 for cset in revs:
1571 seqno += 1
1571 seqno += 1
1572 doexport(ui, repo, cset, seqno, total, revwidth, opts)
1572 doexport(ui, repo, cset, seqno, total, revwidth, opts)
1573
1573
1574 def forget(ui, repo, *pats, **opts):
1574 def forget(ui, repo, *pats, **opts):
1575 """don't add the specified files on the next commit (DEPRECATED)
1575 """don't add the specified files on the next commit (DEPRECATED)
1576
1576
1577 (DEPRECATED)
1577 (DEPRECATED)
1578 Undo an 'hg add' scheduled for the next commit.
1578 Undo an 'hg add' scheduled for the next commit.
1579
1579
1580 This command is now deprecated and will be removed in a future
1580 This command is now deprecated and will be removed in a future
1581 release. Please use revert instead.
1581 release. Please use revert instead.
1582 """
1582 """
1583 ui.warn(_("(the forget command is deprecated; use revert instead)\n"))
1583 ui.warn(_("(the forget command is deprecated; use revert instead)\n"))
1584 forget = []
1584 forget = []
1585 for src, abs, rel, exact in walk(repo, pats, opts):
1585 for src, abs, rel, exact in walk(repo, pats, opts):
1586 if repo.dirstate.state(abs) == 'a':
1586 if repo.dirstate.state(abs) == 'a':
1587 forget.append(abs)
1587 forget.append(abs)
1588 if ui.verbose or not exact:
1588 if ui.verbose or not exact:
1589 ui.status(_('forgetting %s\n') % ((pats and rel) or abs))
1589 ui.status(_('forgetting %s\n') % ((pats and rel) or abs))
1590 repo.forget(forget)
1590 repo.forget(forget)
1591
1591
1592 def grep(ui, repo, pattern, *pats, **opts):
1592 def grep(ui, repo, pattern, *pats, **opts):
1593 """search for a pattern in specified files and revisions
1593 """search for a pattern in specified files and revisions
1594
1594
1595 Search revisions of files for a regular expression.
1595 Search revisions of files for a regular expression.
1596
1596
1597 This command behaves differently than Unix grep. It only accepts
1597 This command behaves differently than Unix grep. It only accepts
1598 Python/Perl regexps. It searches repository history, not the
1598 Python/Perl regexps. It searches repository history, not the
1599 working directory. It always prints the revision number in which
1599 working directory. It always prints the revision number in which
1600 a match appears.
1600 a match appears.
1601
1601
1602 By default, grep only prints output for the first revision of a
1602 By default, grep only prints output for the first revision of a
1603 file in which it finds a match. To get it to print every revision
1603 file in which it finds a match. To get it to print every revision
1604 that contains a change in match status ("-" for a match that
1604 that contains a change in match status ("-" for a match that
1605 becomes a non-match, or "+" for a non-match that becomes a match),
1605 becomes a non-match, or "+" for a non-match that becomes a match),
1606 use the --all flag.
1606 use the --all flag.
1607 """
1607 """
1608 reflags = 0
1608 reflags = 0
1609 if opts['ignore_case']:
1609 if opts['ignore_case']:
1610 reflags |= re.I
1610 reflags |= re.I
1611 regexp = re.compile(pattern, reflags)
1611 regexp = re.compile(pattern, reflags)
1612 sep, eol = ':', '\n'
1612 sep, eol = ':', '\n'
1613 if opts['print0']:
1613 if opts['print0']:
1614 sep = eol = '\0'
1614 sep = eol = '\0'
1615
1615
1616 fcache = {}
1616 fcache = {}
1617 def getfile(fn):
1617 def getfile(fn):
1618 if fn not in fcache:
1618 if fn not in fcache:
1619 fcache[fn] = repo.file(fn)
1619 fcache[fn] = repo.file(fn)
1620 return fcache[fn]
1620 return fcache[fn]
1621
1621
1622 def matchlines(body):
1622 def matchlines(body):
1623 begin = 0
1623 begin = 0
1624 linenum = 0
1624 linenum = 0
1625 while True:
1625 while True:
1626 match = regexp.search(body, begin)
1626 match = regexp.search(body, begin)
1627 if not match:
1627 if not match:
1628 break
1628 break
1629 mstart, mend = match.span()
1629 mstart, mend = match.span()
1630 linenum += body.count('\n', begin, mstart) + 1
1630 linenum += body.count('\n', begin, mstart) + 1
1631 lstart = body.rfind('\n', begin, mstart) + 1 or begin
1631 lstart = body.rfind('\n', begin, mstart) + 1 or begin
1632 lend = body.find('\n', mend)
1632 lend = body.find('\n', mend)
1633 yield linenum, mstart - lstart, mend - lstart, body[lstart:lend]
1633 yield linenum, mstart - lstart, mend - lstart, body[lstart:lend]
1634 begin = lend + 1
1634 begin = lend + 1
1635
1635
1636 class linestate(object):
1636 class linestate(object):
1637 def __init__(self, line, linenum, colstart, colend):
1637 def __init__(self, line, linenum, colstart, colend):
1638 self.line = line
1638 self.line = line
1639 self.linenum = linenum
1639 self.linenum = linenum
1640 self.colstart = colstart
1640 self.colstart = colstart
1641 self.colend = colend
1641 self.colend = colend
1642 def __eq__(self, other):
1642 def __eq__(self, other):
1643 return self.line == other.line
1643 return self.line == other.line
1644 def __hash__(self):
1644 def __hash__(self):
1645 return hash(self.line)
1645 return hash(self.line)
1646
1646
1647 matches = {}
1647 matches = {}
1648 def grepbody(fn, rev, body):
1648 def grepbody(fn, rev, body):
1649 matches[rev].setdefault(fn, {})
1649 matches[rev].setdefault(fn, {})
1650 m = matches[rev][fn]
1650 m = matches[rev][fn]
1651 for lnum, cstart, cend, line in matchlines(body):
1651 for lnum, cstart, cend, line in matchlines(body):
1652 s = linestate(line, lnum, cstart, cend)
1652 s = linestate(line, lnum, cstart, cend)
1653 m[s] = s
1653 m[s] = s
1654
1654
1655 # FIXME: prev isn't used, why ?
1655 # FIXME: prev isn't used, why ?
1656 prev = {}
1656 prev = {}
1657 ucache = {}
1657 ucache = {}
1658 def display(fn, rev, states, prevstates):
1658 def display(fn, rev, states, prevstates):
1659 diff = list(sets.Set(states).symmetric_difference(sets.Set(prevstates)))
1659 diff = list(sets.Set(states).symmetric_difference(sets.Set(prevstates)))
1660 diff.sort(lambda x, y: cmp(x.linenum, y.linenum))
1660 diff.sort(lambda x, y: cmp(x.linenum, y.linenum))
1661 counts = {'-': 0, '+': 0}
1661 counts = {'-': 0, '+': 0}
1662 filerevmatches = {}
1662 filerevmatches = {}
1663 for l in diff:
1663 for l in diff:
1664 if incrementing or not opts['all']:
1664 if incrementing or not opts['all']:
1665 change = ((l in prevstates) and '-') or '+'
1665 change = ((l in prevstates) and '-') or '+'
1666 r = rev
1666 r = rev
1667 else:
1667 else:
1668 change = ((l in states) and '-') or '+'
1668 change = ((l in states) and '-') or '+'
1669 r = prev[fn]
1669 r = prev[fn]
1670 cols = [fn, str(rev)]
1670 cols = [fn, str(rev)]
1671 if opts['line_number']:
1671 if opts['line_number']:
1672 cols.append(str(l.linenum))
1672 cols.append(str(l.linenum))
1673 if opts['all']:
1673 if opts['all']:
1674 cols.append(change)
1674 cols.append(change)
1675 if opts['user']:
1675 if opts['user']:
1676 cols.append(trimuser(ui, getchange(rev)[1], rev,
1676 cols.append(trimuser(ui, getchange(rev)[1], rev,
1677 ucache))
1677 ucache))
1678 if opts['files_with_matches']:
1678 if opts['files_with_matches']:
1679 c = (fn, rev)
1679 c = (fn, rev)
1680 if c in filerevmatches:
1680 if c in filerevmatches:
1681 continue
1681 continue
1682 filerevmatches[c] = 1
1682 filerevmatches[c] = 1
1683 else:
1683 else:
1684 cols.append(l.line)
1684 cols.append(l.line)
1685 ui.write(sep.join(cols), eol)
1685 ui.write(sep.join(cols), eol)
1686 counts[change] += 1
1686 counts[change] += 1
1687 return counts['+'], counts['-']
1687 return counts['+'], counts['-']
1688
1688
1689 fstate = {}
1689 fstate = {}
1690 skip = {}
1690 skip = {}
1691 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1691 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1692 count = 0
1692 count = 0
1693 incrementing = False
1693 incrementing = False
1694 for st, rev, fns in changeiter:
1694 for st, rev, fns in changeiter:
1695 if st == 'window':
1695 if st == 'window':
1696 incrementing = rev
1696 incrementing = rev
1697 matches.clear()
1697 matches.clear()
1698 elif st == 'add':
1698 elif st == 'add':
1699 change = repo.changelog.read(repo.lookup(str(rev)))
1699 change = repo.changelog.read(repo.lookup(str(rev)))
1700 mf = repo.manifest.read(change[0])
1700 mf = repo.manifest.read(change[0])
1701 matches[rev] = {}
1701 matches[rev] = {}
1702 for fn in fns:
1702 for fn in fns:
1703 if fn in skip:
1703 if fn in skip:
1704 continue
1704 continue
1705 fstate.setdefault(fn, {})
1705 fstate.setdefault(fn, {})
1706 try:
1706 try:
1707 grepbody(fn, rev, getfile(fn).read(mf[fn]))
1707 grepbody(fn, rev, getfile(fn).read(mf[fn]))
1708 except KeyError:
1708 except KeyError:
1709 pass
1709 pass
1710 elif st == 'iter':
1710 elif st == 'iter':
1711 states = matches[rev].items()
1711 states = matches[rev].items()
1712 states.sort()
1712 states.sort()
1713 for fn, m in states:
1713 for fn, m in states:
1714 if fn in skip:
1714 if fn in skip:
1715 continue
1715 continue
1716 if incrementing or not opts['all'] or fstate[fn]:
1716 if incrementing or not opts['all'] or fstate[fn]:
1717 pos, neg = display(fn, rev, m, fstate[fn])
1717 pos, neg = display(fn, rev, m, fstate[fn])
1718 count += pos + neg
1718 count += pos + neg
1719 if pos and not opts['all']:
1719 if pos and not opts['all']:
1720 skip[fn] = True
1720 skip[fn] = True
1721 fstate[fn] = m
1721 fstate[fn] = m
1722 prev[fn] = rev
1722 prev[fn] = rev
1723
1723
1724 if not incrementing:
1724 if not incrementing:
1725 fstate = fstate.items()
1725 fstate = fstate.items()
1726 fstate.sort()
1726 fstate.sort()
1727 for fn, state in fstate:
1727 for fn, state in fstate:
1728 if fn in skip:
1728 if fn in skip:
1729 continue
1729 continue
1730 display(fn, rev, {}, state)
1730 display(fn, rev, {}, state)
1731 return (count == 0 and 1) or 0
1731 return (count == 0 and 1) or 0
1732
1732
1733 def heads(ui, repo, **opts):
1733 def heads(ui, repo, **opts):
1734 """show current repository heads
1734 """show current repository heads
1735
1735
1736 Show all repository head changesets.
1736 Show all repository head changesets.
1737
1737
1738 Repository "heads" are changesets that don't have children
1738 Repository "heads" are changesets that don't have children
1739 changesets. They are where development generally takes place and
1739 changesets. They are where development generally takes place and
1740 are the usual targets for update and merge operations.
1740 are the usual targets for update and merge operations.
1741 """
1741 """
1742 if opts['rev']:
1742 if opts['rev']:
1743 heads = repo.heads(repo.lookup(opts['rev']))
1743 heads = repo.heads(repo.lookup(opts['rev']))
1744 else:
1744 else:
1745 heads = repo.heads()
1745 heads = repo.heads()
1746 br = None
1746 br = None
1747 if opts['branches']:
1747 if opts['branches']:
1748 br = repo.branchlookup(heads)
1748 br = repo.branchlookup(heads)
1749 displayer = show_changeset(ui, repo, opts)
1749 displayer = show_changeset(ui, repo, opts)
1750 for n in heads:
1750 for n in heads:
1751 displayer.show(changenode=n, brinfo=br)
1751 displayer.show(changenode=n, brinfo=br)
1752
1752
1753 def identify(ui, repo):
1753 def identify(ui, repo):
1754 """print information about the working copy
1754 """print information about the working copy
1755
1755
1756 Print a short summary of the current state of the repo.
1756 Print a short summary of the current state of the repo.
1757
1757
1758 This summary identifies the repository state using one or two parent
1758 This summary identifies the repository state using one or two parent
1759 hash identifiers, followed by a "+" if there are uncommitted changes
1759 hash identifiers, followed by a "+" if there are uncommitted changes
1760 in the working directory, followed by a list of tags for this revision.
1760 in the working directory, followed by a list of tags for this revision.
1761 """
1761 """
1762 parents = [p for p in repo.dirstate.parents() if p != nullid]
1762 parents = [p for p in repo.dirstate.parents() if p != nullid]
1763 if not parents:
1763 if not parents:
1764 ui.write(_("unknown\n"))
1764 ui.write(_("unknown\n"))
1765 return
1765 return
1766
1766
1767 hexfunc = ui.verbose and hex or short
1767 hexfunc = ui.verbose and hex or short
1768 modified, added, removed, deleted, unknown = repo.changes()
1768 modified, added, removed, deleted, unknown = repo.changes()
1769 output = ["%s%s" %
1769 output = ["%s%s" %
1770 ('+'.join([hexfunc(parent) for parent in parents]),
1770 ('+'.join([hexfunc(parent) for parent in parents]),
1771 (modified or added or removed or deleted) and "+" or "")]
1771 (modified or added or removed or deleted) and "+" or "")]
1772
1772
1773 if not ui.quiet:
1773 if not ui.quiet:
1774 # multiple tags for a single parent separated by '/'
1774 # multiple tags for a single parent separated by '/'
1775 parenttags = ['/'.join(tags)
1775 parenttags = ['/'.join(tags)
1776 for tags in map(repo.nodetags, parents) if tags]
1776 for tags in map(repo.nodetags, parents) if tags]
1777 # tags for multiple parents separated by ' + '
1777 # tags for multiple parents separated by ' + '
1778 if parenttags:
1778 if parenttags:
1779 output.append(' + '.join(parenttags))
1779 output.append(' + '.join(parenttags))
1780
1780
1781 ui.write("%s\n" % ' '.join(output))
1781 ui.write("%s\n" % ' '.join(output))
1782
1782
1783 def import_(ui, repo, patch1, *patches, **opts):
1783 def import_(ui, repo, patch1, *patches, **opts):
1784 """import an ordered set of patches
1784 """import an ordered set of patches
1785
1785
1786 Import a list of patches and commit them individually.
1786 Import a list of patches and commit them individually.
1787
1787
1788 If there are outstanding changes in the working directory, import
1788 If there are outstanding changes in the working directory, import
1789 will abort unless given the -f flag.
1789 will abort unless given the -f flag.
1790
1790
1791 If a patch looks like a mail message (its first line starts with
1791 If a patch looks like a mail message (its first line starts with
1792 "From " or looks like an RFC822 header), it will not be applied
1792 "From " or looks like an RFC822 header), it will not be applied
1793 unless the -f option is used. The importer neither parses nor
1793 unless the -f option is used. The importer neither parses nor
1794 discards mail headers, so use -f only to override the "mailness"
1794 discards mail headers, so use -f only to override the "mailness"
1795 safety check, not to import a real mail message.
1795 safety check, not to import a real mail message.
1796 """
1796 """
1797 patches = (patch1,) + patches
1797 patches = (patch1,) + patches
1798
1798
1799 if not opts['force']:
1799 if not opts['force']:
1800 modified, added, removed, deleted, unknown = repo.changes()
1800 modified, added, removed, deleted, unknown = repo.changes()
1801 if modified or added or removed or deleted:
1801 if modified or added or removed or deleted:
1802 raise util.Abort(_("outstanding uncommitted changes"))
1802 raise util.Abort(_("outstanding uncommitted changes"))
1803
1803
1804 d = opts["base"]
1804 d = opts["base"]
1805 strip = opts["strip"]
1805 strip = opts["strip"]
1806
1806
1807 mailre = re.compile(r'(?:From |[\w-]+:)')
1807 mailre = re.compile(r'(?:From |[\w-]+:)')
1808
1808
1809 # attempt to detect the start of a patch
1809 # attempt to detect the start of a patch
1810 # (this heuristic is borrowed from quilt)
1810 # (this heuristic is borrowed from quilt)
1811 diffre = re.compile(r'(?:Index:[ \t]|diff[ \t]|RCS file: |' +
1811 diffre = re.compile(r'(?:Index:[ \t]|diff[ \t]|RCS file: |' +
1812 'retrieving revision [0-9]+(\.[0-9]+)*$|' +
1812 'retrieving revision [0-9]+(\.[0-9]+)*$|' +
1813 '(---|\*\*\*)[ \t])')
1813 '(---|\*\*\*)[ \t])')
1814
1814
1815 for patch in patches:
1815 for patch in patches:
1816 ui.status(_("applying %s\n") % patch)
1816 ui.status(_("applying %s\n") % patch)
1817 pf = os.path.join(d, patch)
1817 pf = os.path.join(d, patch)
1818
1818
1819 message = []
1819 message = []
1820 user = None
1820 user = None
1821 hgpatch = False
1821 hgpatch = False
1822 for line in file(pf):
1822 for line in file(pf):
1823 line = line.rstrip()
1823 line = line.rstrip()
1824 if (not message and not hgpatch and
1824 if (not message and not hgpatch and
1825 mailre.match(line) and not opts['force']):
1825 mailre.match(line) and not opts['force']):
1826 if len(line) > 35:
1826 if len(line) > 35:
1827 line = line[:32] + '...'
1827 line = line[:32] + '...'
1828 raise util.Abort(_('first line looks like a '
1828 raise util.Abort(_('first line looks like a '
1829 'mail header: ') + line)
1829 'mail header: ') + line)
1830 if diffre.match(line):
1830 if diffre.match(line):
1831 break
1831 break
1832 elif hgpatch:
1832 elif hgpatch:
1833 # parse values when importing the result of an hg export
1833 # parse values when importing the result of an hg export
1834 if line.startswith("# User "):
1834 if line.startswith("# User "):
1835 user = line[7:]
1835 user = line[7:]
1836 ui.debug(_('User: %s\n') % user)
1836 ui.debug(_('User: %s\n') % user)
1837 elif not line.startswith("# ") and line:
1837 elif not line.startswith("# ") and line:
1838 message.append(line)
1838 message.append(line)
1839 hgpatch = False
1839 hgpatch = False
1840 elif line == '# HG changeset patch':
1840 elif line == '# HG changeset patch':
1841 hgpatch = True
1841 hgpatch = True
1842 message = [] # We may have collected garbage
1842 message = [] # We may have collected garbage
1843 else:
1843 else:
1844 message.append(line)
1844 message.append(line)
1845
1845
1846 # make sure message isn't empty
1846 # make sure message isn't empty
1847 if not message:
1847 if not message:
1848 message = _("imported patch %s\n") % patch
1848 message = _("imported patch %s\n") % patch
1849 else:
1849 else:
1850 message = "%s\n" % '\n'.join(message)
1850 message = "%s\n" % '\n'.join(message)
1851 ui.debug(_('message:\n%s\n') % message)
1851 ui.debug(_('message:\n%s\n') % message)
1852
1852
1853 files = util.patch(strip, pf, ui)
1853 files = util.patch(strip, pf, ui)
1854
1854
1855 if len(files) > 0:
1855 if len(files) > 0:
1856 addremove(ui, repo, *files)
1856 addremove(ui, repo, *files)
1857 repo.commit(files, message, user)
1857 repo.commit(files, message, user)
1858
1858
1859 def incoming(ui, repo, source="default", **opts):
1859 def incoming(ui, repo, source="default", **opts):
1860 """show new changesets found in source
1860 """show new changesets found in source
1861
1861
1862 Show new changesets found in the specified path/URL or the default
1862 Show new changesets found in the specified path/URL or the default
1863 pull location. These are the changesets that would be pulled if a pull
1863 pull location. These are the changesets that would be pulled if a pull
1864 was requested.
1864 was requested.
1865
1865
1866 For remote repository, using --bundle avoids downloading the changesets
1866 For remote repository, using --bundle avoids downloading the changesets
1867 twice if the incoming is followed by a pull.
1867 twice if the incoming is followed by a pull.
1868
1868
1869 See pull for valid source format details.
1869 See pull for valid source format details.
1870 """
1870 """
1871 source = ui.expandpath(source)
1871 source = ui.expandpath(source)
1872 if opts['ssh']:
1872 if opts['ssh']:
1873 ui.setconfig("ui", "ssh", opts['ssh'])
1873 ui.setconfig("ui", "ssh", opts['ssh'])
1874 if opts['remotecmd']:
1874 if opts['remotecmd']:
1875 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
1875 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
1876
1876
1877 other = hg.repository(ui, source)
1877 other = hg.repository(ui, source)
1878 incoming = repo.findincoming(other, force=opts["force"])
1878 incoming = repo.findincoming(other, force=opts["force"])
1879 if not incoming:
1879 if not incoming:
1880 ui.status(_("no changes found\n"))
1880 ui.status(_("no changes found\n"))
1881 return
1881 return
1882
1882
1883 cleanup = None
1883 cleanup = None
1884 try:
1884 try:
1885 fname = opts["bundle"]
1885 fname = opts["bundle"]
1886 if fname or not other.local():
1886 if fname or not other.local():
1887 # create a bundle (uncompressed if other repo is not local)
1887 # create a bundle (uncompressed if other repo is not local)
1888 cg = other.changegroup(incoming, "incoming")
1888 cg = other.changegroup(incoming, "incoming")
1889 fname = cleanup = write_bundle(cg, fname, compress=other.local())
1889 fname = cleanup = write_bundle(cg, fname, compress=other.local())
1890 # keep written bundle?
1890 # keep written bundle?
1891 if opts["bundle"]:
1891 if opts["bundle"]:
1892 cleanup = None
1892 cleanup = None
1893 if not other.local():
1893 if not other.local():
1894 # use the created uncompressed bundlerepo
1894 # use the created uncompressed bundlerepo
1895 other = bundlerepo.bundlerepository(ui, repo.root, fname)
1895 other = bundlerepo.bundlerepository(ui, repo.root, fname)
1896
1896
1897 o = other.changelog.nodesbetween(incoming)[0]
1897 o = other.changelog.nodesbetween(incoming)[0]
1898 if opts['newest_first']:
1898 if opts['newest_first']:
1899 o.reverse()
1899 o.reverse()
1900 displayer = show_changeset(ui, other, opts)
1900 displayer = show_changeset(ui, other, opts)
1901 for n in o:
1901 for n in o:
1902 parents = [p for p in other.changelog.parents(n) if p != nullid]
1902 parents = [p for p in other.changelog.parents(n) if p != nullid]
1903 if opts['no_merges'] and len(parents) == 2:
1903 if opts['no_merges'] and len(parents) == 2:
1904 continue
1904 continue
1905 displayer.show(changenode=n)
1905 displayer.show(changenode=n)
1906 if opts['patch']:
1906 if opts['patch']:
1907 prev = (parents and parents[0]) or nullid
1907 prev = (parents and parents[0]) or nullid
1908 dodiff(ui, ui, other, prev, n)
1908 dodiff(ui, ui, other, prev, n)
1909 ui.write("\n")
1909 ui.write("\n")
1910 finally:
1910 finally:
1911 if hasattr(other, 'close'):
1911 if hasattr(other, 'close'):
1912 other.close()
1912 other.close()
1913 if cleanup:
1913 if cleanup:
1914 os.unlink(cleanup)
1914 os.unlink(cleanup)
1915
1915
1916 def init(ui, dest="."):
1916 def init(ui, dest="."):
1917 """create a new repository in the given directory
1917 """create a new repository in the given directory
1918
1918
1919 Initialize a new repository in the given directory. If the given
1919 Initialize a new repository in the given directory. If the given
1920 directory does not exist, it is created.
1920 directory does not exist, it is created.
1921
1921
1922 If no directory is given, the current directory is used.
1922 If no directory is given, the current directory is used.
1923 """
1923 """
1924 if not os.path.exists(dest):
1924 if not os.path.exists(dest):
1925 os.mkdir(dest)
1925 os.mkdir(dest)
1926 hg.repository(ui, dest, create=1)
1926 hg.repository(ui, dest, create=1)
1927
1927
1928 def locate(ui, repo, *pats, **opts):
1928 def locate(ui, repo, *pats, **opts):
1929 """locate files matching specific patterns
1929 """locate files matching specific patterns
1930
1930
1931 Print all files under Mercurial control whose names match the
1931 Print all files under Mercurial control whose names match the
1932 given patterns.
1932 given patterns.
1933
1933
1934 This command searches the current directory and its
1934 This command searches the current directory and its
1935 subdirectories. To search an entire repository, move to the root
1935 subdirectories. To search an entire repository, move to the root
1936 of the repository.
1936 of the repository.
1937
1937
1938 If no patterns are given to match, this command prints all file
1938 If no patterns are given to match, this command prints all file
1939 names.
1939 names.
1940
1940
1941 If you want to feed the output of this command into the "xargs"
1941 If you want to feed the output of this command into the "xargs"
1942 command, use the "-0" option to both this command and "xargs".
1942 command, use the "-0" option to both this command and "xargs".
1943 This will avoid the problem of "xargs" treating single filenames
1943 This will avoid the problem of "xargs" treating single filenames
1944 that contain white space as multiple filenames.
1944 that contain white space as multiple filenames.
1945 """
1945 """
1946 end = opts['print0'] and '\0' or '\n'
1946 end = opts['print0'] and '\0' or '\n'
1947 rev = opts['rev']
1947 rev = opts['rev']
1948 if rev:
1948 if rev:
1949 node = repo.lookup(rev)
1949 node = repo.lookup(rev)
1950 else:
1950 else:
1951 node = None
1951 node = None
1952
1952
1953 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
1953 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
1954 head='(?:.*/|)'):
1954 head='(?:.*/|)'):
1955 if not node and repo.dirstate.state(abs) == '?':
1955 if not node and repo.dirstate.state(abs) == '?':
1956 continue
1956 continue
1957 if opts['fullpath']:
1957 if opts['fullpath']:
1958 ui.write(os.path.join(repo.root, abs), end)
1958 ui.write(os.path.join(repo.root, abs), end)
1959 else:
1959 else:
1960 ui.write(((pats and rel) or abs), end)
1960 ui.write(((pats and rel) or abs), end)
1961
1961
1962 def log(ui, repo, *pats, **opts):
1962 def log(ui, repo, *pats, **opts):
1963 """show revision history of entire repository or files
1963 """show revision history of entire repository or files
1964
1964
1965 Print the revision history of the specified files or the entire project.
1965 Print the revision history of the specified files or the entire project.
1966
1966
1967 By default this command outputs: changeset id and hash, tags,
1967 By default this command outputs: changeset id and hash, tags,
1968 non-trivial parents, user, date and time, and a summary for each
1968 non-trivial parents, user, date and time, and a summary for each
1969 commit. When the -v/--verbose switch is used, the list of changed
1969 commit. When the -v/--verbose switch is used, the list of changed
1970 files and full commit message is shown.
1970 files and full commit message is shown.
1971 """
1971 """
1972 class dui(object):
1972 class dui(object):
1973 # Implement and delegate some ui protocol. Save hunks of
1973 # Implement and delegate some ui protocol. Save hunks of
1974 # output for later display in the desired order.
1974 # output for later display in the desired order.
1975 def __init__(self, ui):
1975 def __init__(self, ui):
1976 self.ui = ui
1976 self.ui = ui
1977 self.hunk = {}
1977 self.hunk = {}
1978 self.header = {}
1978 self.header = {}
1979 def bump(self, rev):
1979 def bump(self, rev):
1980 self.rev = rev
1980 self.rev = rev
1981 self.hunk[rev] = []
1981 self.hunk[rev] = []
1982 self.header[rev] = []
1982 self.header[rev] = []
1983 def note(self, *args):
1983 def note(self, *args):
1984 if self.verbose:
1984 if self.verbose:
1985 self.write(*args)
1985 self.write(*args)
1986 def status(self, *args):
1986 def status(self, *args):
1987 if not self.quiet:
1987 if not self.quiet:
1988 self.write(*args)
1988 self.write(*args)
1989 def write(self, *args):
1989 def write(self, *args):
1990 self.hunk[self.rev].append(args)
1990 self.hunk[self.rev].append(args)
1991 def write_header(self, *args):
1991 def write_header(self, *args):
1992 self.header[self.rev].append(args)
1992 self.header[self.rev].append(args)
1993 def debug(self, *args):
1993 def debug(self, *args):
1994 if self.debugflag:
1994 if self.debugflag:
1995 self.write(*args)
1995 self.write(*args)
1996 def __getattr__(self, key):
1996 def __getattr__(self, key):
1997 return getattr(self.ui, key)
1997 return getattr(self.ui, key)
1998
1998
1999 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1999 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
2000
2000
2001 if opts['limit']:
2001 if opts['limit']:
2002 try:
2002 try:
2003 limit = int(opts['limit'])
2003 limit = int(opts['limit'])
2004 except ValueError:
2004 except ValueError:
2005 raise util.Abort(_('limit must be a positive integer'))
2005 raise util.Abort(_('limit must be a positive integer'))
2006 if limit <= 0: raise util.Abort(_('limit must be positive'))
2006 if limit <= 0: raise util.Abort(_('limit must be positive'))
2007 else:
2007 else:
2008 limit = sys.maxint
2008 limit = sys.maxint
2009 count = 0
2009 count = 0
2010
2010
2011 displayer = show_changeset(ui, repo, opts)
2011 displayer = show_changeset(ui, repo, opts)
2012 for st, rev, fns in changeiter:
2012 for st, rev, fns in changeiter:
2013 if st == 'window':
2013 if st == 'window':
2014 du = dui(ui)
2014 du = dui(ui)
2015 displayer.ui = du
2015 displayer.ui = du
2016 elif st == 'add':
2016 elif st == 'add':
2017 du.bump(rev)
2017 du.bump(rev)
2018 changenode = repo.changelog.node(rev)
2018 changenode = repo.changelog.node(rev)
2019 parents = [p for p in repo.changelog.parents(changenode)
2019 parents = [p for p in repo.changelog.parents(changenode)
2020 if p != nullid]
2020 if p != nullid]
2021 if opts['no_merges'] and len(parents) == 2:
2021 if opts['no_merges'] and len(parents) == 2:
2022 continue
2022 continue
2023 if opts['only_merges'] and len(parents) != 2:
2023 if opts['only_merges'] and len(parents) != 2:
2024 continue
2024 continue
2025
2025
2026 if opts['keyword']:
2026 if opts['keyword']:
2027 changes = getchange(rev)
2027 changes = getchange(rev)
2028 miss = 0
2028 miss = 0
2029 for k in [kw.lower() for kw in opts['keyword']]:
2029 for k in [kw.lower() for kw in opts['keyword']]:
2030 if not (k in changes[1].lower() or
2030 if not (k in changes[1].lower() or
2031 k in changes[4].lower() or
2031 k in changes[4].lower() or
2032 k in " ".join(changes[3][:20]).lower()):
2032 k in " ".join(changes[3][:20]).lower()):
2033 miss = 1
2033 miss = 1
2034 break
2034 break
2035 if miss:
2035 if miss:
2036 continue
2036 continue
2037
2037
2038 br = None
2038 br = None
2039 if opts['branches']:
2039 if opts['branches']:
2040 br = repo.branchlookup([repo.changelog.node(rev)])
2040 br = repo.branchlookup([repo.changelog.node(rev)])
2041
2041
2042 displayer.show(rev, brinfo=br)
2042 displayer.show(rev, brinfo=br)
2043 if opts['patch']:
2043 if opts['patch']:
2044 prev = (parents and parents[0]) or nullid
2044 prev = (parents and parents[0]) or nullid
2045 dodiff(du, du, repo, prev, changenode, match=matchfn)
2045 dodiff(du, du, repo, prev, changenode, match=matchfn)
2046 du.write("\n\n")
2046 du.write("\n\n")
2047 elif st == 'iter':
2047 elif st == 'iter':
2048 if count == limit: break
2048 if count == limit: break
2049 if du.header[rev]:
2049 if du.header[rev]:
2050 for args in du.header[rev]:
2050 for args in du.header[rev]:
2051 ui.write_header(*args)
2051 ui.write_header(*args)
2052 if du.hunk[rev]:
2052 if du.hunk[rev]:
2053 count += 1
2053 count += 1
2054 for args in du.hunk[rev]:
2054 for args in du.hunk[rev]:
2055 ui.write(*args)
2055 ui.write(*args)
2056
2056
2057 def manifest(ui, repo, rev=None):
2057 def manifest(ui, repo, rev=None):
2058 """output the latest or given revision of the project manifest
2058 """output the latest or given revision of the project manifest
2059
2059
2060 Print a list of version controlled files for the given revision.
2060 Print a list of version controlled files for the given revision.
2061
2061
2062 The manifest is the list of files being version controlled. If no revision
2062 The manifest is the list of files being version controlled. If no revision
2063 is given then the tip is used.
2063 is given then the tip is used.
2064 """
2064 """
2065 if rev:
2065 if rev:
2066 try:
2066 try:
2067 # assume all revision numbers are for changesets
2067 # assume all revision numbers are for changesets
2068 n = repo.lookup(rev)
2068 n = repo.lookup(rev)
2069 change = repo.changelog.read(n)
2069 change = repo.changelog.read(n)
2070 n = change[0]
2070 n = change[0]
2071 except hg.RepoError:
2071 except hg.RepoError:
2072 n = repo.manifest.lookup(rev)
2072 n = repo.manifest.lookup(rev)
2073 else:
2073 else:
2074 n = repo.manifest.tip()
2074 n = repo.manifest.tip()
2075 m = repo.manifest.read(n)
2075 m = repo.manifest.read(n)
2076 mf = repo.manifest.readflags(n)
2076 mf = repo.manifest.readflags(n)
2077 files = m.keys()
2077 files = m.keys()
2078 files.sort()
2078 files.sort()
2079
2079
2080 for f in files:
2080 for f in files:
2081 ui.write("%40s %3s %s\n" % (hex(m[f]), mf[f] and "755" or "644", f))
2081 ui.write("%40s %3s %s\n" % (hex(m[f]), mf[f] and "755" or "644", f))
2082
2082
2083 def merge(ui, repo, node=None, **opts):
2083 def merge(ui, repo, node=None, **opts):
2084 """Merge working directory with another revision
2084 """Merge working directory with another revision
2085
2085
2086 Merge the contents of the current working directory and the
2086 Merge the contents of the current working directory and the
2087 requested revision. Files that changed between either parent are
2087 requested revision. Files that changed between either parent are
2088 marked as changed for the next commit and a commit must be
2088 marked as changed for the next commit and a commit must be
2089 performed before any further updates are allowed.
2089 performed before any further updates are allowed.
2090 """
2090 """
2091 return update(ui, repo, node=node, merge=True, **opts)
2091 return update(ui, repo, node=node, merge=True, **opts)
2092
2092
2093 def outgoing(ui, repo, dest="default-push", **opts):
2093 def outgoing(ui, repo, dest="default-push", **opts):
2094 """show changesets not found in destination
2094 """show changesets not found in destination
2095
2095
2096 Show changesets not found in the specified destination repository or
2096 Show changesets not found in the specified destination repository or
2097 the default push location. These are the changesets that would be pushed
2097 the default push location. These are the changesets that would be pushed
2098 if a push was requested.
2098 if a push was requested.
2099
2099
2100 See pull for valid destination format details.
2100 See pull for valid destination format details.
2101 """
2101 """
2102 dest = ui.expandpath(dest)
2102 dest = ui.expandpath(dest)
2103 if opts['ssh']:
2103 if opts['ssh']:
2104 ui.setconfig("ui", "ssh", opts['ssh'])
2104 ui.setconfig("ui", "ssh", opts['ssh'])
2105 if opts['remotecmd']:
2105 if opts['remotecmd']:
2106 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2106 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2107
2107
2108 other = hg.repository(ui, dest)
2108 other = hg.repository(ui, dest)
2109 o = repo.findoutgoing(other, force=opts['force'])
2109 o = repo.findoutgoing(other, force=opts['force'])
2110 if not o:
2110 if not o:
2111 ui.status(_("no changes found\n"))
2111 ui.status(_("no changes found\n"))
2112 return
2112 return
2113 o = repo.changelog.nodesbetween(o)[0]
2113 o = repo.changelog.nodesbetween(o)[0]
2114 if opts['newest_first']:
2114 if opts['newest_first']:
2115 o.reverse()
2115 o.reverse()
2116 displayer = show_changeset(ui, repo, opts)
2116 displayer = show_changeset(ui, repo, opts)
2117 for n in o:
2117 for n in o:
2118 parents = [p for p in repo.changelog.parents(n) if p != nullid]
2118 parents = [p for p in repo.changelog.parents(n) if p != nullid]
2119 if opts['no_merges'] and len(parents) == 2:
2119 if opts['no_merges'] and len(parents) == 2:
2120 continue
2120 continue
2121 displayer.show(changenode=n)
2121 displayer.show(changenode=n)
2122 if opts['patch']:
2122 if opts['patch']:
2123 prev = (parents and parents[0]) or nullid
2123 prev = (parents and parents[0]) or nullid
2124 dodiff(ui, ui, repo, prev, n)
2124 dodiff(ui, ui, repo, prev, n)
2125 ui.write("\n")
2125 ui.write("\n")
2126
2126
2127 def parents(ui, repo, rev=None, branches=None, **opts):
2127 def parents(ui, repo, rev=None, branches=None, **opts):
2128 """show the parents of the working dir or revision
2128 """show the parents of the working dir or revision
2129
2129
2130 Print the working directory's parent revisions.
2130 Print the working directory's parent revisions.
2131 """
2131 """
2132 if rev:
2132 if rev:
2133 p = repo.changelog.parents(repo.lookup(rev))
2133 p = repo.changelog.parents(repo.lookup(rev))
2134 else:
2134 else:
2135 p = repo.dirstate.parents()
2135 p = repo.dirstate.parents()
2136
2136
2137 br = None
2137 br = None
2138 if branches is not None:
2138 if branches is not None:
2139 br = repo.branchlookup(p)
2139 br = repo.branchlookup(p)
2140 displayer = show_changeset(ui, repo, opts)
2140 displayer = show_changeset(ui, repo, opts)
2141 for n in p:
2141 for n in p:
2142 if n != nullid:
2142 if n != nullid:
2143 displayer.show(changenode=n, brinfo=br)
2143 displayer.show(changenode=n, brinfo=br)
2144
2144
2145 def paths(ui, repo, search=None):
2145 def paths(ui, repo, search=None):
2146 """show definition of symbolic path names
2146 """show definition of symbolic path names
2147
2147
2148 Show definition of symbolic path name NAME. If no name is given, show
2148 Show definition of symbolic path name NAME. If no name is given, show
2149 definition of available names.
2149 definition of available names.
2150
2150
2151 Path names are defined in the [paths] section of /etc/mercurial/hgrc
2151 Path names are defined in the [paths] section of /etc/mercurial/hgrc
2152 and $HOME/.hgrc. If run inside a repository, .hg/hgrc is used, too.
2152 and $HOME/.hgrc. If run inside a repository, .hg/hgrc is used, too.
2153 """
2153 """
2154 if search:
2154 if search:
2155 for name, path in ui.configitems("paths"):
2155 for name, path in ui.configitems("paths"):
2156 if name == search:
2156 if name == search:
2157 ui.write("%s\n" % path)
2157 ui.write("%s\n" % path)
2158 return
2158 return
2159 ui.warn(_("not found!\n"))
2159 ui.warn(_("not found!\n"))
2160 return 1
2160 return 1
2161 else:
2161 else:
2162 for name, path in ui.configitems("paths"):
2162 for name, path in ui.configitems("paths"):
2163 ui.write("%s = %s\n" % (name, path))
2163 ui.write("%s = %s\n" % (name, path))
2164
2164
2165 def postincoming(ui, repo, modheads, optupdate):
2165 def postincoming(ui, repo, modheads, optupdate):
2166 if modheads == 0:
2166 if modheads == 0:
2167 return
2167 return
2168 if optupdate:
2168 if optupdate:
2169 if modheads == 1:
2169 if modheads == 1:
2170 return update(ui, repo)
2170 return update(ui, repo)
2171 else:
2171 else:
2172 ui.status(_("not updating, since new heads added\n"))
2172 ui.status(_("not updating, since new heads added\n"))
2173 if modheads > 1:
2173 if modheads > 1:
2174 ui.status(_("(run 'hg heads' to see heads, 'hg merge' to merge)\n"))
2174 ui.status(_("(run 'hg heads' to see heads, 'hg merge' to merge)\n"))
2175 else:
2175 else:
2176 ui.status(_("(run 'hg update' to get a working copy)\n"))
2176 ui.status(_("(run 'hg update' to get a working copy)\n"))
2177
2177
2178 def pull(ui, repo, source="default", **opts):
2178 def pull(ui, repo, source="default", **opts):
2179 """pull changes from the specified source
2179 """pull changes from the specified source
2180
2180
2181 Pull changes from a remote repository to a local one.
2181 Pull changes from a remote repository to a local one.
2182
2182
2183 This finds all changes from the repository at the specified path
2183 This finds all changes from the repository at the specified path
2184 or URL and adds them to the local repository. By default, this
2184 or URL and adds them to the local repository. By default, this
2185 does not update the copy of the project in the working directory.
2185 does not update the copy of the project in the working directory.
2186
2186
2187 Valid URLs are of the form:
2187 Valid URLs are of the form:
2188
2188
2189 local/filesystem/path
2189 local/filesystem/path
2190 http://[user@]host[:port][/path]
2190 http://[user@]host[:port][/path]
2191 https://[user@]host[:port][/path]
2191 https://[user@]host[:port][/path]
2192 ssh://[user@]host[:port][/path]
2192 ssh://[user@]host[:port][/path]
2193
2193
2194 Some notes about using SSH with Mercurial:
2194 Some notes about using SSH with Mercurial:
2195 - SSH requires an accessible shell account on the destination machine
2195 - SSH requires an accessible shell account on the destination machine
2196 and a copy of hg in the remote path or specified with as remotecmd.
2196 and a copy of hg in the remote path or specified with as remotecmd.
2197 - /path is relative to the remote user's home directory by default.
2197 - /path is relative to the remote user's home directory by default.
2198 Use two slashes at the start of a path to specify an absolute path.
2198 Use two slashes at the start of a path to specify an absolute path.
2199 - Mercurial doesn't use its own compression via SSH; the right thing
2199 - Mercurial doesn't use its own compression via SSH; the right thing
2200 to do is to configure it in your ~/.ssh/ssh_config, e.g.:
2200 to do is to configure it in your ~/.ssh/ssh_config, e.g.:
2201 Host *.mylocalnetwork.example.com
2201 Host *.mylocalnetwork.example.com
2202 Compression off
2202 Compression off
2203 Host *
2203 Host *
2204 Compression on
2204 Compression on
2205 Alternatively specify "ssh -C" as your ssh command in your hgrc or
2205 Alternatively specify "ssh -C" as your ssh command in your hgrc or
2206 with the --ssh command line option.
2206 with the --ssh command line option.
2207 """
2207 """
2208 source = ui.expandpath(source)
2208 source = ui.expandpath(source)
2209 ui.status(_('pulling from %s\n') % (source))
2209 ui.status(_('pulling from %s\n') % (source))
2210
2210
2211 if opts['ssh']:
2211 if opts['ssh']:
2212 ui.setconfig("ui", "ssh", opts['ssh'])
2212 ui.setconfig("ui", "ssh", opts['ssh'])
2213 if opts['remotecmd']:
2213 if opts['remotecmd']:
2214 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2214 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2215
2215
2216 other = hg.repository(ui, source)
2216 other = hg.repository(ui, source)
2217 revs = None
2217 revs = None
2218 if opts['rev'] and not other.local():
2218 if opts['rev'] and not other.local():
2219 raise util.Abort(_("pull -r doesn't work for remote repositories yet"))
2219 raise util.Abort(_("pull -r doesn't work for remote repositories yet"))
2220 elif opts['rev']:
2220 elif opts['rev']:
2221 revs = [other.lookup(rev) for rev in opts['rev']]
2221 revs = [other.lookup(rev) for rev in opts['rev']]
2222 modheads = repo.pull(other, heads=revs, force=opts['force'])
2222 modheads = repo.pull(other, heads=revs, force=opts['force'])
2223 return postincoming(ui, repo, modheads, opts['update'])
2223 return postincoming(ui, repo, modheads, opts['update'])
2224
2224
2225 def push(ui, repo, dest="default-push", **opts):
2225 def push(ui, repo, dest="default-push", **opts):
2226 """push changes to the specified destination
2226 """push changes to the specified destination
2227
2227
2228 Push changes from the local repository to the given destination.
2228 Push changes from the local repository to the given destination.
2229
2229
2230 This is the symmetrical operation for pull. It helps to move
2230 This is the symmetrical operation for pull. It helps to move
2231 changes from the current repository to a different one. If the
2231 changes from the current repository to a different one. If the
2232 destination is local this is identical to a pull in that directory
2232 destination is local this is identical to a pull in that directory
2233 from the current one.
2233 from the current one.
2234
2234
2235 By default, push will refuse to run if it detects the result would
2235 By default, push will refuse to run if it detects the result would
2236 increase the number of remote heads. This generally indicates the
2236 increase the number of remote heads. This generally indicates the
2237 the client has forgotten to sync and merge before pushing.
2237 the client has forgotten to sync and merge before pushing.
2238
2238
2239 Valid URLs are of the form:
2239 Valid URLs are of the form:
2240
2240
2241 local/filesystem/path
2241 local/filesystem/path
2242 ssh://[user@]host[:port][/path]
2242 ssh://[user@]host[:port][/path]
2243
2243
2244 Look at the help text for the pull command for important details
2244 Look at the help text for the pull command for important details
2245 about ssh:// URLs.
2245 about ssh:// URLs.
2246 """
2246 """
2247 dest = ui.expandpath(dest)
2247 dest = ui.expandpath(dest)
2248 ui.status('pushing to %s\n' % (dest))
2248 ui.status('pushing to %s\n' % (dest))
2249
2249
2250 if opts['ssh']:
2250 if opts['ssh']:
2251 ui.setconfig("ui", "ssh", opts['ssh'])
2251 ui.setconfig("ui", "ssh", opts['ssh'])
2252 if opts['remotecmd']:
2252 if opts['remotecmd']:
2253 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2253 ui.setconfig("ui", "remotecmd", opts['remotecmd'])
2254
2254
2255 other = hg.repository(ui, dest)
2255 other = hg.repository(ui, dest)
2256 revs = None
2256 revs = None
2257 if opts['rev']:
2257 if opts['rev']:
2258 revs = [repo.lookup(rev) for rev in opts['rev']]
2258 revs = [repo.lookup(rev) for rev in opts['rev']]
2259 r = repo.push(other, opts['force'], revs=revs)
2259 r = repo.push(other, opts['force'], revs=revs)
2260 return r == 0
2260 return r == 0
2261
2261
2262 def rawcommit(ui, repo, *flist, **rc):
2262 def rawcommit(ui, repo, *flist, **rc):
2263 """raw commit interface (DEPRECATED)
2263 """raw commit interface (DEPRECATED)
2264
2264
2265 (DEPRECATED)
2265 (DEPRECATED)
2266 Lowlevel commit, for use in helper scripts.
2266 Lowlevel commit, for use in helper scripts.
2267
2267
2268 This command is not intended to be used by normal users, as it is
2268 This command is not intended to be used by normal users, as it is
2269 primarily useful for importing from other SCMs.
2269 primarily useful for importing from other SCMs.
2270
2270
2271 This command is now deprecated and will be removed in a future
2271 This command is now deprecated and will be removed in a future
2272 release, please use debugsetparents and commit instead.
2272 release, please use debugsetparents and commit instead.
2273 """
2273 """
2274
2274
2275 ui.warn(_("(the rawcommit command is deprecated)\n"))
2275 ui.warn(_("(the rawcommit command is deprecated)\n"))
2276
2276
2277 message = rc['message']
2277 message = rc['message']
2278 if not message and rc['logfile']:
2278 if not message and rc['logfile']:
2279 try:
2279 try:
2280 message = open(rc['logfile']).read()
2280 message = open(rc['logfile']).read()
2281 except IOError:
2281 except IOError:
2282 pass
2282 pass
2283 if not message and not rc['logfile']:
2283 if not message and not rc['logfile']:
2284 raise util.Abort(_("missing commit message"))
2284 raise util.Abort(_("missing commit message"))
2285
2285
2286 files = relpath(repo, list(flist))
2286 files = relpath(repo, list(flist))
2287 if rc['files']:
2287 if rc['files']:
2288 files += open(rc['files']).read().splitlines()
2288 files += open(rc['files']).read().splitlines()
2289
2289
2290 rc['parent'] = map(repo.lookup, rc['parent'])
2290 rc['parent'] = map(repo.lookup, rc['parent'])
2291
2291
2292 try:
2292 try:
2293 repo.rawcommit(files, message, rc['user'], rc['date'], *rc['parent'])
2293 repo.rawcommit(files, message, rc['user'], rc['date'], *rc['parent'])
2294 except ValueError, inst:
2294 except ValueError, inst:
2295 raise util.Abort(str(inst))
2295 raise util.Abort(str(inst))
2296
2296
2297 def recover(ui, repo):
2297 def recover(ui, repo):
2298 """roll back an interrupted transaction
2298 """roll back an interrupted transaction
2299
2299
2300 Recover from an interrupted commit or pull.
2300 Recover from an interrupted commit or pull.
2301
2301
2302 This command tries to fix the repository status after an interrupted
2302 This command tries to fix the repository status after an interrupted
2303 operation. It should only be necessary when Mercurial suggests it.
2303 operation. It should only be necessary when Mercurial suggests it.
2304 """
2304 """
2305 if repo.recover():
2305 if repo.recover():
2306 return repo.verify()
2306 return repo.verify()
2307 return 1
2307 return 1
2308
2308
2309 def remove(ui, repo, pat, *pats, **opts):
2309 def remove(ui, repo, pat, *pats, **opts):
2310 """remove the specified files on the next commit
2310 """remove the specified files on the next commit
2311
2311
2312 Schedule the indicated files for removal from the repository.
2312 Schedule the indicated files for removal from the repository.
2313
2313
2314 This command schedules the files to be removed at the next commit.
2314 This command schedules the files to be removed at the next commit.
2315 This only removes files from the current branch, not from the
2315 This only removes files from the current branch, not from the
2316 entire project history. If the files still exist in the working
2316 entire project history. If the files still exist in the working
2317 directory, they will be deleted from it.
2317 directory, they will be deleted from it.
2318 """
2318 """
2319 names = []
2319 names = []
2320 def okaytoremove(abs, rel, exact):
2320 def okaytoremove(abs, rel, exact):
2321 modified, added, removed, deleted, unknown = repo.changes(files=[abs])
2321 modified, added, removed, deleted, unknown = repo.changes(files=[abs])
2322 reason = None
2322 reason = None
2323 if modified and not opts['force']:
2323 if modified and not opts['force']:
2324 reason = _('is modified')
2324 reason = _('is modified')
2325 elif added:
2325 elif added:
2326 reason = _('has been marked for add')
2326 reason = _('has been marked for add')
2327 elif unknown:
2327 elif unknown:
2328 reason = _('is not managed')
2328 reason = _('is not managed')
2329 if reason:
2329 if reason:
2330 if exact:
2330 if exact:
2331 ui.warn(_('not removing %s: file %s\n') % (rel, reason))
2331 ui.warn(_('not removing %s: file %s\n') % (rel, reason))
2332 else:
2332 else:
2333 return True
2333 return True
2334 for src, abs, rel, exact in walk(repo, (pat,) + pats, opts):
2334 for src, abs, rel, exact in walk(repo, (pat,) + pats, opts):
2335 if okaytoremove(abs, rel, exact):
2335 if okaytoremove(abs, rel, exact):
2336 if ui.verbose or not exact:
2336 if ui.verbose or not exact:
2337 ui.status(_('removing %s\n') % rel)
2337 ui.status(_('removing %s\n') % rel)
2338 names.append(abs)
2338 names.append(abs)
2339 repo.remove(names, unlink=True)
2339 repo.remove(names, unlink=True)
2340
2340
2341 def rename(ui, repo, *pats, **opts):
2341 def rename(ui, repo, *pats, **opts):
2342 """rename files; equivalent of copy + remove
2342 """rename files; equivalent of copy + remove
2343
2343
2344 Mark dest as copies of sources; mark sources for deletion. If
2344 Mark dest as copies of sources; mark sources for deletion. If
2345 dest is a directory, copies are put in that directory. If dest is
2345 dest is a directory, copies are put in that directory. If dest is
2346 a file, there can only be one source.
2346 a file, there can only be one source.
2347
2347
2348 By default, this command copies the contents of files as they
2348 By default, this command copies the contents of files as they
2349 stand in the working directory. If invoked with --after, the
2349 stand in the working directory. If invoked with --after, the
2350 operation is recorded, but no copying is performed.
2350 operation is recorded, but no copying is performed.
2351
2351
2352 This command takes effect in the next commit.
2352 This command takes effect in the next commit.
2353
2353
2354 NOTE: This command should be treated as experimental. While it
2354 NOTE: This command should be treated as experimental. While it
2355 should properly record rename files, this information is not yet
2355 should properly record rename files, this information is not yet
2356 fully used by merge, nor fully reported by log.
2356 fully used by merge, nor fully reported by log.
2357 """
2357 """
2358 wlock = repo.wlock(0)
2358 wlock = repo.wlock(0)
2359 errs, copied = docopy(ui, repo, pats, opts, wlock)
2359 errs, copied = docopy(ui, repo, pats, opts, wlock)
2360 names = []
2360 names = []
2361 for abs, rel, exact in copied:
2361 for abs, rel, exact in copied:
2362 if ui.verbose or not exact:
2362 if ui.verbose or not exact:
2363 ui.status(_('removing %s\n') % rel)
2363 ui.status(_('removing %s\n') % rel)
2364 names.append(abs)
2364 names.append(abs)
2365 repo.remove(names, True, wlock)
2365 repo.remove(names, True, wlock)
2366 return errs
2366 return errs
2367
2367
2368 def revert(ui, repo, *pats, **opts):
2368 def revert(ui, repo, *pats, **opts):
2369 """revert modified files or dirs back to their unmodified states
2369 """revert modified files or dirs back to their unmodified states
2370
2370
2371 In its default mode, it reverts any uncommitted modifications made
2371 In its default mode, it reverts any uncommitted modifications made
2372 to the named files or directories. This restores the contents of
2372 to the named files or directories. This restores the contents of
2373 the affected files to an unmodified state.
2373 the affected files to an unmodified state.
2374
2374
2375 Modified files are saved with a .orig suffix before reverting.
2375 Modified files are saved with a .orig suffix before reverting.
2376 To disable these backups, use --no-backup.
2376 To disable these backups, use --no-backup.
2377
2377
2378 Using the -r option, it reverts the given files or directories to
2378 Using the -r option, it reverts the given files or directories to
2379 their state as of an earlier revision. This can be helpful to "roll
2379 their state as of an earlier revision. This can be helpful to "roll
2380 back" some or all of a change that should not have been committed.
2380 back" some or all of a change that should not have been committed.
2381
2381
2382 Revert modifies the working directory. It does not commit any
2382 Revert modifies the working directory. It does not commit any
2383 changes, or change the parent of the current working directory.
2383 changes, or change the parent of the current working directory.
2384
2384
2385 If a file has been deleted, it is recreated. If the executable
2385 If a file has been deleted, it is recreated. If the executable
2386 mode of a file was changed, it is reset.
2386 mode of a file was changed, it is reset.
2387
2387
2388 If names are given, all files matching the names are reverted.
2388 If names are given, all files matching the names are reverted.
2389
2389
2390 If no arguments are given, all files in the repository are reverted.
2390 If no arguments are given, all files in the repository are reverted.
2391 """
2391 """
2392 parent = repo.dirstate.parents()[0]
2392 parent = repo.dirstate.parents()[0]
2393 node = opts['rev'] and repo.lookup(opts['rev']) or parent
2393 node = opts['rev'] and repo.lookup(opts['rev']) or parent
2394 mf = repo.manifest.read(repo.changelog.read(node)[0])
2394 mf = repo.manifest.read(repo.changelog.read(node)[0])
2395
2395
2396 wlock = repo.wlock()
2396 wlock = repo.wlock()
2397
2397
2398 # need all matching names in dirstate and manifest of target rev,
2398 # need all matching names in dirstate and manifest of target rev,
2399 # so have to walk both. do not print errors if files exist in one
2399 # so have to walk both. do not print errors if files exist in one
2400 # but not other.
2400 # but not other.
2401
2401
2402 names = {}
2402 names = {}
2403 target_only = {}
2403 target_only = {}
2404
2404
2405 # walk dirstate.
2405 # walk dirstate.
2406
2406
2407 for src, abs, rel, exact in walk(repo, pats, opts, badmatch=mf.has_key):
2407 for src, abs, rel, exact in walk(repo, pats, opts, badmatch=mf.has_key):
2408 names[abs] = (rel, exact)
2408 names[abs] = (rel, exact)
2409 if src == 'b':
2409 if src == 'b':
2410 target_only[abs] = True
2410 target_only[abs] = True
2411
2411
2412 # walk target manifest.
2412 # walk target manifest.
2413
2413
2414 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
2414 for src, abs, rel, exact in walk(repo, pats, opts, node=node,
2415 badmatch=names.has_key):
2415 badmatch=names.has_key):
2416 if abs in names: continue
2416 if abs in names: continue
2417 names[abs] = (rel, exact)
2417 names[abs] = (rel, exact)
2418 target_only[abs] = True
2418 target_only[abs] = True
2419
2419
2420 changes = repo.changes(match=names.has_key, wlock=wlock)
2420 changes = repo.changes(match=names.has_key, wlock=wlock)
2421 modified, added, removed, deleted, unknown = map(dict.fromkeys, changes)
2421 modified, added, removed, deleted, unknown = map(dict.fromkeys, changes)
2422
2422
2423 revert = ([], _('reverting %s\n'))
2423 revert = ([], _('reverting %s\n'))
2424 add = ([], _('adding %s\n'))
2424 add = ([], _('adding %s\n'))
2425 remove = ([], _('removing %s\n'))
2425 remove = ([], _('removing %s\n'))
2426 forget = ([], _('forgetting %s\n'))
2426 forget = ([], _('forgetting %s\n'))
2427 undelete = ([], _('undeleting %s\n'))
2427 undelete = ([], _('undeleting %s\n'))
2428 update = {}
2428 update = {}
2429
2429
2430 disptable = (
2430 disptable = (
2431 # dispatch table:
2431 # dispatch table:
2432 # file state
2432 # file state
2433 # action if in target manifest
2433 # action if in target manifest
2434 # action if not in target manifest
2434 # action if not in target manifest
2435 # make backup if in target manifest
2435 # make backup if in target manifest
2436 # make backup if not in target manifest
2436 # make backup if not in target manifest
2437 (modified, revert, remove, True, True),
2437 (modified, revert, remove, True, True),
2438 (added, revert, forget, True, False),
2438 (added, revert, forget, True, False),
2439 (removed, undelete, None, False, False),
2439 (removed, undelete, None, False, False),
2440 (deleted, revert, remove, False, False),
2440 (deleted, revert, remove, False, False),
2441 (unknown, add, None, True, False),
2441 (unknown, add, None, True, False),
2442 (target_only, add, None, False, False),
2442 (target_only, add, None, False, False),
2443 )
2443 )
2444
2444
2445 entries = names.items()
2445 entries = names.items()
2446 entries.sort()
2446 entries.sort()
2447
2447
2448 for abs, (rel, exact) in entries:
2448 for abs, (rel, exact) in entries:
2449 in_mf = abs in mf
2449 in_mf = abs in mf
2450 def handle(xlist, dobackup):
2450 def handle(xlist, dobackup):
2451 xlist[0].append(abs)
2451 xlist[0].append(abs)
2452 if dobackup and not opts['no_backup'] and os.path.exists(rel):
2452 if dobackup and not opts['no_backup'] and os.path.exists(rel):
2453 bakname = "%s.orig" % rel
2453 bakname = "%s.orig" % rel
2454 ui.note(_('saving current version of %s as %s\n') %
2454 ui.note(_('saving current version of %s as %s\n') %
2455 (rel, bakname))
2455 (rel, bakname))
2456 shutil.copyfile(rel, bakname)
2456 shutil.copyfile(rel, bakname)
2457 shutil.copymode(rel, bakname)
2457 shutil.copymode(rel, bakname)
2458 if ui.verbose or not exact:
2458 if ui.verbose or not exact:
2459 ui.status(xlist[1] % rel)
2459 ui.status(xlist[1] % rel)
2460 for table, hitlist, misslist, backuphit, backupmiss in disptable:
2460 for table, hitlist, misslist, backuphit, backupmiss in disptable:
2461 if abs not in table: continue
2461 if abs not in table: continue
2462 # file has changed in dirstate
2462 # file has changed in dirstate
2463 if in_mf:
2463 if in_mf:
2464 handle(hitlist, backuphit)
2464 handle(hitlist, backuphit)
2465 elif misslist is not None:
2465 elif misslist is not None:
2466 handle(misslist, backupmiss)
2466 handle(misslist, backupmiss)
2467 else:
2467 else:
2468 if exact: ui.warn(_('file not managed: %s\n' % rel))
2468 if exact: ui.warn(_('file not managed: %s\n' % rel))
2469 break
2469 break
2470 else:
2470 else:
2471 # file has not changed in dirstate
2471 # file has not changed in dirstate
2472 if node == parent:
2472 if node == parent:
2473 if exact: ui.warn(_('no changes needed to %s\n' % rel))
2473 if exact: ui.warn(_('no changes needed to %s\n' % rel))
2474 continue
2474 continue
2475 if not in_mf:
2475 if not in_mf:
2476 handle(remove, False)
2476 handle(remove, False)
2477 update[abs] = True
2477 update[abs] = True
2478
2478
2479 repo.dirstate.forget(forget[0])
2479 repo.dirstate.forget(forget[0])
2480 r = repo.update(node, False, True, update.has_key, False, wlock=wlock)
2480 r = repo.update(node, False, True, update.has_key, False, wlock=wlock)
2481 repo.dirstate.update(add[0], 'a')
2481 repo.dirstate.update(add[0], 'a')
2482 repo.dirstate.update(undelete[0], 'n')
2482 repo.dirstate.update(undelete[0], 'n')
2483 repo.dirstate.update(remove[0], 'r')
2483 repo.dirstate.update(remove[0], 'r')
2484 return r
2484 return r
2485
2485
2486 def root(ui, repo):
2486 def root(ui, repo):
2487 """print the root (top) of the current working dir
2487 """print the root (top) of the current working dir
2488
2488
2489 Print the root directory of the current repository.
2489 Print the root directory of the current repository.
2490 """
2490 """
2491 ui.write(repo.root + "\n")
2491 ui.write(repo.root + "\n")
2492
2492
2493 def serve(ui, repo, **opts):
2493 def serve(ui, repo, **opts):
2494 """export the repository via HTTP
2494 """export the repository via HTTP
2495
2495
2496 Start a local HTTP repository browser and pull server.
2496 Start a local HTTP repository browser and pull server.
2497
2497
2498 By default, the server logs accesses to stdout and errors to
2498 By default, the server logs accesses to stdout and errors to
2499 stderr. Use the "-A" and "-E" options to log to files.
2499 stderr. Use the "-A" and "-E" options to log to files.
2500 """
2500 """
2501
2501
2502 if opts["stdio"]:
2502 if opts["stdio"]:
2503 if repo is None:
2503 if repo is None:
2504 raise hg.RepoError(_('no repo found'))
2504 raise hg.RepoError(_('no repo found'))
2505 fin, fout = sys.stdin, sys.stdout
2505 fin, fout = sys.stdin, sys.stdout
2506 sys.stdout = sys.stderr
2506 sys.stdout = sys.stderr
2507
2507
2508 # Prevent insertion/deletion of CRs
2508 # Prevent insertion/deletion of CRs
2509 util.set_binary(fin)
2509 util.set_binary(fin)
2510 util.set_binary(fout)
2510 util.set_binary(fout)
2511
2511
2512 def getarg():
2512 def getarg():
2513 argline = fin.readline()[:-1]
2513 argline = fin.readline()[:-1]
2514 arg, l = argline.split()
2514 arg, l = argline.split()
2515 val = fin.read(int(l))
2515 val = fin.read(int(l))
2516 return arg, val
2516 return arg, val
2517 def respond(v):
2517 def respond(v):
2518 fout.write("%d\n" % len(v))
2518 fout.write("%d\n" % len(v))
2519 fout.write(v)
2519 fout.write(v)
2520 fout.flush()
2520 fout.flush()
2521
2521
2522 lock = None
2522 lock = None
2523
2523
2524 while 1:
2524 while 1:
2525 cmd = fin.readline()[:-1]
2525 cmd = fin.readline()[:-1]
2526 if cmd == '':
2526 if cmd == '':
2527 return
2527 return
2528 if cmd == "heads":
2528 if cmd == "heads":
2529 h = repo.heads()
2529 h = repo.heads()
2530 respond(" ".join(map(hex, h)) + "\n")
2530 respond(" ".join(map(hex, h)) + "\n")
2531 if cmd == "lock":
2531 if cmd == "lock":
2532 lock = repo.lock()
2532 lock = repo.lock()
2533 respond("")
2533 respond("")
2534 if cmd == "unlock":
2534 if cmd == "unlock":
2535 if lock:
2535 if lock:
2536 lock.release()
2536 lock.release()
2537 lock = None
2537 lock = None
2538 respond("")
2538 respond("")
2539 elif cmd == "branches":
2539 elif cmd == "branches":
2540 arg, nodes = getarg()
2540 arg, nodes = getarg()
2541 nodes = map(bin, nodes.split(" "))
2541 nodes = map(bin, nodes.split(" "))
2542 r = []
2542 r = []
2543 for b in repo.branches(nodes):
2543 for b in repo.branches(nodes):
2544 r.append(" ".join(map(hex, b)) + "\n")
2544 r.append(" ".join(map(hex, b)) + "\n")
2545 respond("".join(r))
2545 respond("".join(r))
2546 elif cmd == "between":
2546 elif cmd == "between":
2547 arg, pairs = getarg()
2547 arg, pairs = getarg()
2548 pairs = [map(bin, p.split("-")) for p in pairs.split(" ")]
2548 pairs = [map(bin, p.split("-")) for p in pairs.split(" ")]
2549 r = []
2549 r = []
2550 for b in repo.between(pairs):
2550 for b in repo.between(pairs):
2551 r.append(" ".join(map(hex, b)) + "\n")
2551 r.append(" ".join(map(hex, b)) + "\n")
2552 respond("".join(r))
2552 respond("".join(r))
2553 elif cmd == "changegroup":
2553 elif cmd == "changegroup":
2554 nodes = []
2554 nodes = []
2555 arg, roots = getarg()
2555 arg, roots = getarg()
2556 nodes = map(bin, roots.split(" "))
2556 nodes = map(bin, roots.split(" "))
2557
2557
2558 cg = repo.changegroup(nodes, 'serve')
2558 cg = repo.changegroup(nodes, 'serve')
2559 while 1:
2559 while 1:
2560 d = cg.read(4096)
2560 d = cg.read(4096)
2561 if not d:
2561 if not d:
2562 break
2562 break
2563 fout.write(d)
2563 fout.write(d)
2564
2564
2565 fout.flush()
2565 fout.flush()
2566
2566
2567 elif cmd == "addchangegroup":
2567 elif cmd == "addchangegroup":
2568 if not lock:
2568 if not lock:
2569 respond("not locked")
2569 respond("not locked")
2570 continue
2570 continue
2571 respond("")
2571 respond("")
2572
2572
2573 r = repo.addchangegroup(fin)
2573 r = repo.addchangegroup(fin)
2574 respond(str(r))
2574 respond(str(r))
2575
2575
2576 optlist = ("name templates style address port ipv6"
2576 optlist = ("name templates style address port ipv6"
2577 " accesslog errorlog webdir_conf")
2577 " accesslog errorlog webdir_conf")
2578 for o in optlist.split():
2578 for o in optlist.split():
2579 if opts[o]:
2579 if opts[o]:
2580 ui.setconfig("web", o, opts[o])
2580 ui.setconfig("web", o, opts[o])
2581
2581
2582 if repo is None and not ui.config("web", "webdir_conf"):
2582 if repo is None and not ui.config("web", "webdir_conf"):
2583 raise hg.RepoError(_('no repo found'))
2583 raise hg.RepoError(_('no repo found'))
2584
2584
2585 if opts['daemon'] and not opts['daemon_pipefds']:
2585 if opts['daemon'] and not opts['daemon_pipefds']:
2586 rfd, wfd = os.pipe()
2586 rfd, wfd = os.pipe()
2587 args = sys.argv[:]
2587 args = sys.argv[:]
2588 args.append('--daemon-pipefds=%d,%d' % (rfd, wfd))
2588 args.append('--daemon-pipefds=%d,%d' % (rfd, wfd))
2589 pid = os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
2589 pid = os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
2590 args[0], args)
2590 args[0], args)
2591 os.close(wfd)
2591 os.close(wfd)
2592 os.read(rfd, 1)
2592 os.read(rfd, 1)
2593 os._exit(0)
2593 os._exit(0)
2594
2594
2595 try:
2595 try:
2596 httpd = hgweb.create_server(ui, repo)
2596 httpd = hgweb.create_server(ui, repo)
2597 except socket.error, inst:
2597 except socket.error, inst:
2598 raise util.Abort(_('cannot start server: ') + inst.args[1])
2598 raise util.Abort(_('cannot start server: ') + inst.args[1])
2599
2599
2600 if ui.verbose:
2600 if ui.verbose:
2601 addr, port = httpd.socket.getsockname()
2601 addr, port = httpd.socket.getsockname()
2602 if addr == '0.0.0.0':
2602 if addr == '0.0.0.0':
2603 addr = socket.gethostname()
2603 addr = socket.gethostname()
2604 else:
2604 else:
2605 try:
2605 try:
2606 addr = socket.gethostbyaddr(addr)[0]
2606 addr = socket.gethostbyaddr(addr)[0]
2607 except socket.error:
2607 except socket.error:
2608 pass
2608 pass
2609 if port != 80:
2609 if port != 80:
2610 ui.status(_('listening at http://%s:%d/\n') % (addr, port))
2610 ui.status(_('listening at http://%s:%d/\n') % (addr, port))
2611 else:
2611 else:
2612 ui.status(_('listening at http://%s/\n') % addr)
2612 ui.status(_('listening at http://%s/\n') % addr)
2613
2613
2614 if opts['pid_file']:
2614 if opts['pid_file']:
2615 fp = open(opts['pid_file'], 'w')
2615 fp = open(opts['pid_file'], 'w')
2616 fp.write(str(os.getpid()))
2616 fp.write(str(os.getpid()))
2617 fp.close()
2617 fp.close()
2618
2618
2619 if opts['daemon_pipefds']:
2619 if opts['daemon_pipefds']:
2620 rfd, wfd = [int(x) for x in opts['daemon_pipefds'].split(',')]
2620 rfd, wfd = [int(x) for x in opts['daemon_pipefds'].split(',')]
2621 os.close(rfd)
2621 os.close(rfd)
2622 os.write(wfd, 'y')
2622 os.write(wfd, 'y')
2623 os.close(wfd)
2623 os.close(wfd)
2624 sys.stdout.flush()
2624 sys.stdout.flush()
2625 sys.stderr.flush()
2625 sys.stderr.flush()
2626 fd = os.open(util.nulldev, os.O_RDWR)
2626 fd = os.open(util.nulldev, os.O_RDWR)
2627 if fd != 0: os.dup2(fd, 0)
2627 if fd != 0: os.dup2(fd, 0)
2628 if fd != 1: os.dup2(fd, 1)
2628 if fd != 1: os.dup2(fd, 1)
2629 if fd != 2: os.dup2(fd, 2)
2629 if fd != 2: os.dup2(fd, 2)
2630 if fd not in (0, 1, 2): os.close(fd)
2630 if fd not in (0, 1, 2): os.close(fd)
2631
2631
2632 httpd.serve_forever()
2632 httpd.serve_forever()
2633
2633
2634 def status(ui, repo, *pats, **opts):
2634 def status(ui, repo, *pats, **opts):
2635 """show changed files in the working directory
2635 """show changed files in the working directory
2636
2636
2637 Show changed files in the repository. If names are
2637 Show changed files in the repository. If names are
2638 given, only files that match are shown.
2638 given, only files that match are shown.
2639
2639
2640 The codes used to show the status of files are:
2640 The codes used to show the status of files are:
2641 M = modified
2641 M = modified
2642 A = added
2642 A = added
2643 R = removed
2643 R = removed
2644 ! = deleted, but still tracked
2644 ! = deleted, but still tracked
2645 ? = not tracked
2645 ? = not tracked
2646 I = ignored (not shown by default)
2646 I = ignored (not shown by default)
2647 """
2647 """
2648
2648
2649 show_ignored = opts['ignored'] and True or False
2649 show_ignored = opts['ignored'] and True or False
2650 files, matchfn, anypats = matchpats(repo, pats, opts)
2650 files, matchfn, anypats = matchpats(repo, pats, opts)
2651 cwd = (pats and repo.getcwd()) or ''
2651 cwd = (pats and repo.getcwd()) or ''
2652 modified, added, removed, deleted, unknown, ignored = [
2652 modified, added, removed, deleted, unknown, ignored = [
2653 [util.pathto(cwd, x) for x in n]
2653 [util.pathto(cwd, x) for x in n]
2654 for n in repo.changes(files=files, match=matchfn,
2654 for n in repo.changes(files=files, match=matchfn,
2655 show_ignored=show_ignored)]
2655 show_ignored=show_ignored)]
2656
2656
2657 changetypes = [('modified', 'M', modified),
2657 changetypes = [('modified', 'M', modified),
2658 ('added', 'A', added),
2658 ('added', 'A', added),
2659 ('removed', 'R', removed),
2659 ('removed', 'R', removed),
2660 ('deleted', '!', deleted),
2660 ('deleted', '!', deleted),
2661 ('unknown', '?', unknown),
2661 ('unknown', '?', unknown),
2662 ('ignored', 'I', ignored)]
2662 ('ignored', 'I', ignored)]
2663
2663
2664 end = opts['print0'] and '\0' or '\n'
2664 end = opts['print0'] and '\0' or '\n'
2665
2665
2666 for opt, char, changes in ([ct for ct in changetypes if opts[ct[0]]]
2666 for opt, char, changes in ([ct for ct in changetypes if opts[ct[0]]]
2667 or changetypes):
2667 or changetypes):
2668 if opts['no_status']:
2668 if opts['no_status']:
2669 format = "%%s%s" % end
2669 format = "%%s%s" % end
2670 else:
2670 else:
2671 format = "%s %%s%s" % (char, end)
2671 format = "%s %%s%s" % (char, end)
2672
2672
2673 for f in changes:
2673 for f in changes:
2674 ui.write(format % f)
2674 ui.write(format % f)
2675
2675
2676 def tag(ui, repo, name, rev_=None, **opts):
2676 def tag(ui, repo, name, rev_=None, **opts):
2677 """add a tag for the current tip or a given revision
2677 """add a tag for the current tip or a given revision
2678
2678
2679 Name a particular revision using <name>.
2679 Name a particular revision using <name>.
2680
2680
2681 Tags are used to name particular revisions of the repository and are
2681 Tags are used to name particular revisions of the repository and are
2682 very useful to compare different revision, to go back to significant
2682 very useful to compare different revision, to go back to significant
2683 earlier versions or to mark branch points as releases, etc.
2683 earlier versions or to mark branch points as releases, etc.
2684
2684
2685 If no revision is given, the tip is used.
2685 If no revision is given, the tip is used.
2686
2686
2687 To facilitate version control, distribution, and merging of tags,
2687 To facilitate version control, distribution, and merging of tags,
2688 they are stored as a file named ".hgtags" which is managed
2688 they are stored as a file named ".hgtags" which is managed
2689 similarly to other project files and can be hand-edited if
2689 similarly to other project files and can be hand-edited if
2690 necessary. The file '.hg/localtags' is used for local tags (not
2690 necessary. The file '.hg/localtags' is used for local tags (not
2691 shared among repositories).
2691 shared among repositories).
2692 """
2692 """
2693 if name == "tip":
2693 if name == "tip":
2694 raise util.Abort(_("the name 'tip' is reserved"))
2694 raise util.Abort(_("the name 'tip' is reserved"))
2695 if rev_ is not None:
2695 if rev_ is not None:
2696 ui.warn(_("use of 'hg tag NAME [REV]' is deprecated, "
2696 ui.warn(_("use of 'hg tag NAME [REV]' is deprecated, "
2697 "please use 'hg tag [-r REV] NAME' instead\n"))
2697 "please use 'hg tag [-r REV] NAME' instead\n"))
2698 if opts['rev']:
2698 if opts['rev']:
2699 raise util.Abort(_("use only one form to specify the revision"))
2699 raise util.Abort(_("use only one form to specify the revision"))
2700 if opts['rev']:
2700 if opts['rev']:
2701 rev_ = opts['rev']
2701 rev_ = opts['rev']
2702 if rev_:
2702 if rev_:
2703 r = hex(repo.lookup(rev_))
2703 r = hex(repo.lookup(rev_))
2704 else:
2704 else:
2705 r = hex(repo.changelog.tip())
2705 r = hex(repo.changelog.tip())
2706
2706
2707 disallowed = (revrangesep, '\r', '\n')
2707 disallowed = (revrangesep, '\r', '\n')
2708 for c in disallowed:
2708 for c in disallowed:
2709 if name.find(c) >= 0:
2709 if name.find(c) >= 0:
2710 raise util.Abort(_("%s cannot be used in a tag name") % repr(c))
2710 raise util.Abort(_("%s cannot be used in a tag name") % repr(c))
2711
2711
2712 repo.hook('pretag', throw=True, node=r, tag=name,
2712 repo.hook('pretag', throw=True, node=r, tag=name,
2713 local=int(not not opts['local']))
2713 local=int(not not opts['local']))
2714
2714
2715 if opts['local']:
2715 if opts['local']:
2716 repo.opener("localtags", "a").write("%s %s\n" % (r, name))
2716 repo.opener("localtags", "a").write("%s %s\n" % (r, name))
2717 repo.hook('tag', node=r, tag=name, local=1)
2717 repo.hook('tag', node=r, tag=name, local=1)
2718 return
2718 return
2719
2719
2720 for x in repo.changes():
2720 for x in repo.changes():
2721 if ".hgtags" in x:
2721 if ".hgtags" in x:
2722 raise util.Abort(_("working copy of .hgtags is changed "
2722 raise util.Abort(_("working copy of .hgtags is changed "
2723 "(please commit .hgtags manually)"))
2723 "(please commit .hgtags manually)"))
2724
2724
2725 repo.wfile(".hgtags", "ab").write("%s %s\n" % (r, name))
2725 repo.wfile(".hgtags", "ab").write("%s %s\n" % (r, name))
2726 if repo.dirstate.state(".hgtags") == '?':
2726 if repo.dirstate.state(".hgtags") == '?':
2727 repo.add([".hgtags"])
2727 repo.add([".hgtags"])
2728
2728
2729 message = (opts['message'] or
2729 message = (opts['message'] or
2730 _("Added tag %s for changeset %s") % (name, r))
2730 _("Added tag %s for changeset %s") % (name, r))
2731 try:
2731 try:
2732 repo.commit([".hgtags"], message, opts['user'], opts['date'])
2732 repo.commit([".hgtags"], message, opts['user'], opts['date'])
2733 repo.hook('tag', node=r, tag=name, local=0)
2733 repo.hook('tag', node=r, tag=name, local=0)
2734 except ValueError, inst:
2734 except ValueError, inst:
2735 raise util.Abort(str(inst))
2735 raise util.Abort(str(inst))
2736
2736
2737 def tags(ui, repo):
2737 def tags(ui, repo):
2738 """list repository tags
2738 """list repository tags
2739
2739
2740 List the repository tags.
2740 List the repository tags.
2741
2741
2742 This lists both regular and local tags.
2742 This lists both regular and local tags.
2743 """
2743 """
2744
2744
2745 l = repo.tagslist()
2745 l = repo.tagslist()
2746 l.reverse()
2746 l.reverse()
2747 for t, n in l:
2747 for t, n in l:
2748 try:
2748 try:
2749 r = "%5d:%s" % (repo.changelog.rev(n), hex(n))
2749 r = "%5d:%s" % (repo.changelog.rev(n), hex(n))
2750 except KeyError:
2750 except KeyError:
2751 r = " ?:?"
2751 r = " ?:?"
2752 if ui.quiet:
2752 if ui.quiet:
2753 ui.write("%s\n" % t)
2753 ui.write("%s\n" % t)
2754 else:
2754 else:
2755 ui.write("%-30s %s\n" % (t, r))
2755 ui.write("%-30s %s\n" % (t, r))
2756
2756
2757 def tip(ui, repo, **opts):
2757 def tip(ui, repo, **opts):
2758 """show the tip revision
2758 """show the tip revision
2759
2759
2760 Show the tip revision.
2760 Show the tip revision.
2761 """
2761 """
2762 n = repo.changelog.tip()
2762 n = repo.changelog.tip()
2763 br = None
2763 br = None
2764 if opts['branches']:
2764 if opts['branches']:
2765 br = repo.branchlookup([n])
2765 br = repo.branchlookup([n])
2766 show_changeset(ui, repo, opts).show(changenode=n, brinfo=br)
2766 show_changeset(ui, repo, opts).show(changenode=n, brinfo=br)
2767 if opts['patch']:
2767 if opts['patch']:
2768 dodiff(ui, ui, repo, repo.changelog.parents(n)[0], n)
2768 dodiff(ui, ui, repo, repo.changelog.parents(n)[0], n)
2769
2769
2770 def unbundle(ui, repo, fname, **opts):
2770 def unbundle(ui, repo, fname, **opts):
2771 """apply a changegroup file
2771 """apply a changegroup file
2772
2772
2773 Apply a compressed changegroup file generated by the bundle
2773 Apply a compressed changegroup file generated by the bundle
2774 command.
2774 command.
2775 """
2775 """
2776 f = urllib.urlopen(fname)
2776 f = urllib.urlopen(fname)
2777
2777
2778 header = f.read(6)
2778 header = f.read(6)
2779 if not header.startswith("HG"):
2779 if not header.startswith("HG"):
2780 raise util.Abort(_("%s: not a Mercurial bundle file") % fname)
2780 raise util.Abort(_("%s: not a Mercurial bundle file") % fname)
2781 elif not header.startswith("HG10"):
2781 elif not header.startswith("HG10"):
2782 raise util.Abort(_("%s: unknown bundle version") % fname)
2782 raise util.Abort(_("%s: unknown bundle version") % fname)
2783 elif header == "HG10BZ":
2783 elif header == "HG10BZ":
2784 def generator(f):
2784 def generator(f):
2785 zd = bz2.BZ2Decompressor()
2785 zd = bz2.BZ2Decompressor()
2786 zd.decompress("BZ")
2786 zd.decompress("BZ")
2787 for chunk in f:
2787 for chunk in f:
2788 yield zd.decompress(chunk)
2788 yield zd.decompress(chunk)
2789 elif header == "HG10UN":
2789 elif header == "HG10UN":
2790 def generator(f):
2790 def generator(f):
2791 for chunk in f:
2791 for chunk in f:
2792 yield chunk
2792 yield chunk
2793 else:
2793 else:
2794 raise util.Abort(_("%s: unknown bundle compression type")
2794 raise util.Abort(_("%s: unknown bundle compression type")
2795 % fname)
2795 % fname)
2796 gen = generator(util.filechunkiter(f, 4096))
2796 gen = generator(util.filechunkiter(f, 4096))
2797 modheads = repo.addchangegroup(util.chunkbuffer(gen))
2797 modheads = repo.addchangegroup(util.chunkbuffer(gen))
2798 return postincoming(ui, repo, modheads, opts['update'])
2798 return postincoming(ui, repo, modheads, opts['update'])
2799
2799
2800 def undo(ui, repo):
2800 def undo(ui, repo):
2801 """undo the last commit or pull
2801 """undo the last commit or pull
2802
2802
2803 Roll back the last pull or commit transaction on the
2803 Roll back the last pull or commit transaction on the
2804 repository, restoring the project to its earlier state.
2804 repository, restoring the project to its earlier state.
2805
2805
2806 This command should be used with care. There is only one level of
2806 This command should be used with care. There is only one level of
2807 undo and there is no redo.
2807 undo and there is no redo.
2808
2808
2809 This command is not intended for use on public repositories. Once
2809 This command is not intended for use on public repositories. Once
2810 a change is visible for pull by other users, undoing it locally is
2810 a change is visible for pull by other users, undoing it locally is
2811 ineffective. Furthemore a race is possible with readers of the
2811 ineffective. Furthemore a race is possible with readers of the
2812 repository, for example an ongoing pull from the repository will
2812 repository, for example an ongoing pull from the repository will
2813 fail and rollback.
2813 fail and rollback.
2814 """
2814 """
2815 repo.undo()
2815 repo.undo()
2816
2816
2817 def update(ui, repo, node=None, merge=False, clean=False, force=None,
2817 def update(ui, repo, node=None, merge=False, clean=False, force=None,
2818 branch=None, **opts):
2818 branch=None, **opts):
2819 """update or merge working directory
2819 """update or merge working directory
2820
2820
2821 Update the working directory to the specified revision.
2821 Update the working directory to the specified revision.
2822
2822
2823 If there are no outstanding changes in the working directory and
2823 If there are no outstanding changes in the working directory and
2824 there is a linear relationship between the current version and the
2824 there is a linear relationship between the current version and the
2825 requested version, the result is the requested version.
2825 requested version, the result is the requested version.
2826
2826
2827 Otherwise the result is a merge between the contents of the
2827 Otherwise the result is a merge between the contents of the
2828 current working directory and the requested version. Files that
2828 current working directory and the requested version. Files that
2829 changed between either parent are marked as changed for the next
2829 changed between either parent are marked as changed for the next
2830 commit and a commit must be performed before any further updates
2830 commit and a commit must be performed before any further updates
2831 are allowed.
2831 are allowed.
2832
2832
2833 By default, update will refuse to run if doing so would require
2833 By default, update will refuse to run if doing so would require
2834 merging or discarding local changes.
2834 merging or discarding local changes.
2835 """
2835 """
2836 if branch:
2836 if branch:
2837 br = repo.branchlookup(branch=branch)
2837 br = repo.branchlookup(branch=branch)
2838 found = []
2838 found = []
2839 for x in br:
2839 for x in br:
2840 if branch in br[x]:
2840 if branch in br[x]:
2841 found.append(x)
2841 found.append(x)
2842 if len(found) > 1:
2842 if len(found) > 1:
2843 ui.warn(_("Found multiple heads for %s\n") % branch)
2843 ui.warn(_("Found multiple heads for %s\n") % branch)
2844 for x in found:
2844 for x in found:
2845 show_changeset(ui, repo, opts).show(changenode=x, brinfo=br)
2845 show_changeset(ui, repo, opts).show(changenode=x, brinfo=br)
2846 return 1
2846 return 1
2847 if len(found) == 1:
2847 if len(found) == 1:
2848 node = found[0]
2848 node = found[0]
2849 ui.warn(_("Using head %s for branch %s\n") % (short(node), branch))
2849 ui.warn(_("Using head %s for branch %s\n") % (short(node), branch))
2850 else:
2850 else:
2851 ui.warn(_("branch %s not found\n") % (branch))
2851 ui.warn(_("branch %s not found\n") % (branch))
2852 return 1
2852 return 1
2853 else:
2853 else:
2854 node = node and repo.lookup(node) or repo.changelog.tip()
2854 node = node and repo.lookup(node) or repo.changelog.tip()
2855 return repo.update(node, allow=merge, force=clean, forcemerge=force)
2855 return repo.update(node, allow=merge, force=clean, forcemerge=force)
2856
2856
2857 def verify(ui, repo):
2857 def verify(ui, repo):
2858 """verify the integrity of the repository
2858 """verify the integrity of the repository
2859
2859
2860 Verify the integrity of the current repository.
2860 Verify the integrity of the current repository.
2861
2861
2862 This will perform an extensive check of the repository's
2862 This will perform an extensive check of the repository's
2863 integrity, validating the hashes and checksums of each entry in
2863 integrity, validating the hashes and checksums of each entry in
2864 the changelog, manifest, and tracked files, as well as the
2864 the changelog, manifest, and tracked files, as well as the
2865 integrity of their crosslinks and indices.
2865 integrity of their crosslinks and indices.
2866 """
2866 """
2867 return repo.verify()
2867 return repo.verify()
2868
2868
2869 # Command options and aliases are listed here, alphabetically
2869 # Command options and aliases are listed here, alphabetically
2870
2870
2871 table = {
2871 table = {
2872 "^add":
2872 "^add":
2873 (add,
2873 (add,
2874 [('I', 'include', [], _('include names matching the given patterns')),
2874 [('I', 'include', [], _('include names matching the given patterns')),
2875 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2875 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2876 _('hg add [OPTION]... [FILE]...')),
2876 _('hg add [OPTION]... [FILE]...')),
2877 "addremove":
2877 "addremove":
2878 (addremove,
2878 (addremove,
2879 [('I', 'include', [], _('include names matching the given patterns')),
2879 [('I', 'include', [], _('include names matching the given patterns')),
2880 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2880 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2881 _('hg addremove [OPTION]... [FILE]...')),
2881 _('hg addremove [OPTION]... [FILE]...')),
2882 "^annotate":
2882 "^annotate":
2883 (annotate,
2883 (annotate,
2884 [('r', 'rev', '', _('annotate the specified revision')),
2884 [('r', 'rev', '', _('annotate the specified revision')),
2885 ('a', 'text', None, _('treat all files as text')),
2885 ('a', 'text', None, _('treat all files as text')),
2886 ('u', 'user', None, _('list the author')),
2886 ('u', 'user', None, _('list the author')),
2887 ('d', 'date', None, _('list the date')),
2887 ('d', 'date', None, _('list the date')),
2888 ('n', 'number', None, _('list the revision number (default)')),
2888 ('n', 'number', None, _('list the revision number (default)')),
2889 ('c', 'changeset', None, _('list the changeset')),
2889 ('c', 'changeset', None, _('list the changeset')),
2890 ('I', 'include', [], _('include names matching the given patterns')),
2890 ('I', 'include', [], _('include names matching the given patterns')),
2891 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2891 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2892 _('hg annotate [-r REV] [-a] [-u] [-d] [-n] [-c] FILE...')),
2892 _('hg annotate [-r REV] [-a] [-u] [-d] [-n] [-c] FILE...')),
2893 'archive':
2893 'archive':
2894 (archive,
2894 (archive,
2895 [('', 'no-decode', None, _('do not pass files through decoders')),
2895 [('', 'no-decode', None, _('do not pass files through decoders')),
2896 ('p', 'prefix', '', _('directory prefix for files in archive')),
2896 ('p', 'prefix', '', _('directory prefix for files in archive')),
2897 ('r', 'rev', '', _('revision to distribute')),
2897 ('r', 'rev', '', _('revision to distribute')),
2898 ('t', 'type', '', _('type of distribution to create')),
2898 ('t', 'type', '', _('type of distribution to create')),
2899 ('I', 'include', [], _('include names matching the given patterns')),
2899 ('I', 'include', [], _('include names matching the given patterns')),
2900 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2900 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2901 _('hg archive [OPTION]... DEST')),
2901 _('hg archive [OPTION]... DEST')),
2902 "bundle":
2902 "bundle":
2903 (bundle,
2903 (bundle,
2904 [('f', 'force', None,
2904 [('f', 'force', None,
2905 _('run even when remote repository is unrelated'))],
2905 _('run even when remote repository is unrelated'))],
2906 _('hg bundle FILE DEST')),
2906 _('hg bundle FILE DEST')),
2907 "cat":
2907 "cat":
2908 (cat,
2908 (cat,
2909 [('o', 'output', '', _('print output to file with formatted name')),
2909 [('o', 'output', '', _('print output to file with formatted name')),
2910 ('r', 'rev', '', _('print the given revision')),
2910 ('r', 'rev', '', _('print the given revision')),
2911 ('I', 'include', [], _('include names matching the given patterns')),
2911 ('I', 'include', [], _('include names matching the given patterns')),
2912 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2912 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2913 _('hg cat [OPTION]... FILE...')),
2913 _('hg cat [OPTION]... FILE...')),
2914 "^clone":
2914 "^clone":
2915 (clone,
2915 (clone,
2916 [('U', 'noupdate', None, _('do not update the new working directory')),
2916 [('U', 'noupdate', None, _('do not update the new working directory')),
2917 ('r', 'rev', [],
2917 ('r', 'rev', [],
2918 _('a changeset you would like to have after cloning')),
2918 _('a changeset you would like to have after cloning')),
2919 ('', 'pull', None, _('use pull protocol to copy metadata')),
2919 ('', 'pull', None, _('use pull protocol to copy metadata')),
2920 ('e', 'ssh', '', _('specify ssh command to use')),
2920 ('e', 'ssh', '', _('specify ssh command to use')),
2921 ('', 'remotecmd', '',
2921 ('', 'remotecmd', '',
2922 _('specify hg command to run on the remote side'))],
2922 _('specify hg command to run on the remote side'))],
2923 _('hg clone [OPTION]... SOURCE [DEST]')),
2923 _('hg clone [OPTION]... SOURCE [DEST]')),
2924 "^commit|ci":
2924 "^commit|ci":
2925 (commit,
2925 (commit,
2926 [('A', 'addremove', None, _('run addremove during commit')),
2926 [('A', 'addremove', None, _('run addremove during commit')),
2927 ('m', 'message', '', _('use <text> as commit message')),
2927 ('m', 'message', '', _('use <text> as commit message')),
2928 ('l', 'logfile', '', _('read the commit message from <file>')),
2928 ('l', 'logfile', '', _('read the commit message from <file>')),
2929 ('d', 'date', '', _('record datecode as commit date')),
2929 ('d', 'date', '', _('record datecode as commit date')),
2930 ('u', 'user', '', _('record user as commiter')),
2930 ('u', 'user', '', _('record user as commiter')),
2931 ('I', 'include', [], _('include names matching the given patterns')),
2931 ('I', 'include', [], _('include names matching the given patterns')),
2932 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2932 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2933 _('hg commit [OPTION]... [FILE]...')),
2933 _('hg commit [OPTION]... [FILE]...')),
2934 "copy|cp":
2934 "copy|cp":
2935 (copy,
2935 (copy,
2936 [('A', 'after', None, _('record a copy that has already occurred')),
2936 [('A', 'after', None, _('record a copy that has already occurred')),
2937 ('f', 'force', None,
2937 ('f', 'force', None,
2938 _('forcibly copy over an existing managed file')),
2938 _('forcibly copy over an existing managed file')),
2939 ('I', 'include', [], _('include names matching the given patterns')),
2939 ('I', 'include', [], _('include names matching the given patterns')),
2940 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2940 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2941 _('hg copy [OPTION]... [SOURCE]... DEST')),
2941 _('hg copy [OPTION]... [SOURCE]... DEST')),
2942 "debugancestor": (debugancestor, [], _('debugancestor INDEX REV1 REV2')),
2942 "debugancestor": (debugancestor, [], _('debugancestor INDEX REV1 REV2')),
2943 "debugcomplete":
2943 "debugcomplete":
2944 (debugcomplete,
2944 (debugcomplete,
2945 [('o', 'options', None, _('show the command options'))],
2945 [('o', 'options', None, _('show the command options'))],
2946 _('debugcomplete [-o] CMD')),
2946 _('debugcomplete [-o] CMD')),
2947 "debugrebuildstate":
2947 "debugrebuildstate":
2948 (debugrebuildstate,
2948 (debugrebuildstate,
2949 [('r', 'rev', '', _('revision to rebuild to'))],
2949 [('r', 'rev', '', _('revision to rebuild to'))],
2950 _('debugrebuildstate [-r REV] [REV]')),
2950 _('debugrebuildstate [-r REV] [REV]')),
2951 "debugcheckstate": (debugcheckstate, [], _('debugcheckstate')),
2951 "debugcheckstate": (debugcheckstate, [], _('debugcheckstate')),
2952 "debugconfig": (debugconfig, [], _('debugconfig')),
2952 "debugconfig": (debugconfig, [], _('debugconfig')),
2953 "debugsetparents": (debugsetparents, [], _('debugsetparents REV1 [REV2]')),
2953 "debugsetparents": (debugsetparents, [], _('debugsetparents REV1 [REV2]')),
2954 "debugstate": (debugstate, [], _('debugstate')),
2954 "debugstate": (debugstate, [], _('debugstate')),
2955 "debugdata": (debugdata, [], _('debugdata FILE REV')),
2955 "debugdata": (debugdata, [], _('debugdata FILE REV')),
2956 "debugindex": (debugindex, [], _('debugindex FILE')),
2956 "debugindex": (debugindex, [], _('debugindex FILE')),
2957 "debugindexdot": (debugindexdot, [], _('debugindexdot FILE')),
2957 "debugindexdot": (debugindexdot, [], _('debugindexdot FILE')),
2958 "debugrename": (debugrename, [], _('debugrename FILE [REV]')),
2958 "debugrename": (debugrename, [], _('debugrename FILE [REV]')),
2959 "debugwalk":
2959 "debugwalk":
2960 (debugwalk,
2960 (debugwalk,
2961 [('I', 'include', [], _('include names matching the given patterns')),
2961 [('I', 'include', [], _('include names matching the given patterns')),
2962 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2962 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2963 _('debugwalk [OPTION]... [FILE]...')),
2963 _('debugwalk [OPTION]... [FILE]...')),
2964 "^diff":
2964 "^diff":
2965 (diff,
2965 (diff,
2966 [('r', 'rev', [], _('revision')),
2966 [('r', 'rev', [], _('revision')),
2967 ('a', 'text', None, _('treat all files as text')),
2967 ('a', 'text', None, _('treat all files as text')),
2968 ('p', 'show-function', None,
2968 ('p', 'show-function', None,
2969 _('show which function each change is in')),
2969 _('show which function each change is in')),
2970 ('w', 'ignore-all-space', None,
2970 ('w', 'ignore-all-space', None,
2971 _('ignore white space when comparing lines')),
2971 _('ignore white space when comparing lines')),
2972 ('I', 'include', [], _('include names matching the given patterns')),
2972 ('I', 'include', [], _('include names matching the given patterns')),
2973 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2973 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2974 _('hg diff [-a] [-I] [-X] [-r REV1 [-r REV2]] [FILE]...')),
2974 _('hg diff [-a] [-I] [-X] [-r REV1 [-r REV2]] [FILE]...')),
2975 "^export":
2975 "^export":
2976 (export,
2976 (export,
2977 [('o', 'output', '', _('print output to file with formatted name')),
2977 [('o', 'output', '', _('print output to file with formatted name')),
2978 ('a', 'text', None, _('treat all files as text')),
2978 ('a', 'text', None, _('treat all files as text')),
2979 ('', 'switch-parent', None, _('diff against the second parent'))],
2979 ('', 'switch-parent', None, _('diff against the second parent'))],
2980 _('hg export [-a] [-o OUTFILESPEC] REV...')),
2980 _('hg export [-a] [-o OUTFILESPEC] REV...')),
2981 "debugforget|forget":
2981 "debugforget|forget":
2982 (forget,
2982 (forget,
2983 [('I', 'include', [], _('include names matching the given patterns')),
2983 [('I', 'include', [], _('include names matching the given patterns')),
2984 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2984 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2985 _('hg forget [OPTION]... FILE...')),
2985 _('hg forget [OPTION]... FILE...')),
2986 "grep":
2986 "grep":
2987 (grep,
2987 (grep,
2988 [('0', 'print0', None, _('end fields with NUL')),
2988 [('0', 'print0', None, _('end fields with NUL')),
2989 ('', 'all', None, _('print all revisions that match')),
2989 ('', 'all', None, _('print all revisions that match')),
2990 ('i', 'ignore-case', None, _('ignore case when matching')),
2990 ('i', 'ignore-case', None, _('ignore case when matching')),
2991 ('l', 'files-with-matches', None,
2991 ('l', 'files-with-matches', None,
2992 _('print only filenames and revs that match')),
2992 _('print only filenames and revs that match')),
2993 ('n', 'line-number', None, _('print matching line numbers')),
2993 ('n', 'line-number', None, _('print matching line numbers')),
2994 ('r', 'rev', [], _('search in given revision range')),
2994 ('r', 'rev', [], _('search in given revision range')),
2995 ('u', 'user', None, _('print user who committed change')),
2995 ('u', 'user', None, _('print user who committed change')),
2996 ('I', 'include', [], _('include names matching the given patterns')),
2996 ('I', 'include', [], _('include names matching the given patterns')),
2997 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2997 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
2998 _('hg grep [OPTION]... PATTERN [FILE]...')),
2998 _('hg grep [OPTION]... PATTERN [FILE]...')),
2999 "heads":
2999 "heads":
3000 (heads,
3000 (heads,
3001 [('b', 'branches', None, _('show branches')),
3001 [('b', 'branches', None, _('show branches')),
3002 ('', 'style', '', _('display using template map file')),
3002 ('', 'style', '', _('display using template map file')),
3003 ('r', 'rev', '', _('show only heads which are descendants of rev')),
3003 ('r', 'rev', '', _('show only heads which are descendants of rev')),
3004 ('', 'template', '', _('display with template'))],
3004 ('', 'template', '', _('display with template'))],
3005 _('hg heads [-b] [-r <rev>]')),
3005 _('hg heads [-b] [-r <rev>]')),
3006 "help": (help_, [], _('hg help [COMMAND]')),
3006 "help": (help_, [], _('hg help [COMMAND]')),
3007 "identify|id": (identify, [], _('hg identify')),
3007 "identify|id": (identify, [], _('hg identify')),
3008 "import|patch":
3008 "import|patch":
3009 (import_,
3009 (import_,
3010 [('p', 'strip', 1,
3010 [('p', 'strip', 1,
3011 _('directory strip option for patch. This has the same\n'
3011 _('directory strip option for patch. This has the same\n'
3012 'meaning as the corresponding patch option')),
3012 'meaning as the corresponding patch option')),
3013 ('b', 'base', '', _('base path')),
3013 ('b', 'base', '', _('base path')),
3014 ('f', 'force', None,
3014 ('f', 'force', None,
3015 _('skip check for outstanding uncommitted changes'))],
3015 _('skip check for outstanding uncommitted changes'))],
3016 _('hg import [-p NUM] [-b BASE] [-f] PATCH...')),
3016 _('hg import [-p NUM] [-b BASE] [-f] PATCH...')),
3017 "incoming|in": (incoming,
3017 "incoming|in": (incoming,
3018 [('M', 'no-merges', None, _('do not show merges')),
3018 [('M', 'no-merges', None, _('do not show merges')),
3019 ('f', 'force', None,
3019 ('f', 'force', None,
3020 _('run even when remote repository is unrelated')),
3020 _('run even when remote repository is unrelated')),
3021 ('', 'style', '', _('display using template map file')),
3021 ('', 'style', '', _('display using template map file')),
3022 ('n', 'newest-first', None, _('show newest record first')),
3022 ('n', 'newest-first', None, _('show newest record first')),
3023 ('', 'bundle', '', _('file to store the bundles into')),
3023 ('', 'bundle', '', _('file to store the bundles into')),
3024 ('p', 'patch', None, _('show patch')),
3024 ('p', 'patch', None, _('show patch')),
3025 ('', 'template', '', _('display with template')),
3025 ('', 'template', '', _('display with template')),
3026 ('e', 'ssh', '', _('specify ssh command to use')),
3026 ('e', 'ssh', '', _('specify ssh command to use')),
3027 ('', 'remotecmd', '',
3027 ('', 'remotecmd', '',
3028 _('specify hg command to run on the remote side'))],
3028 _('specify hg command to run on the remote side'))],
3029 _('hg incoming [-p] [-n] [-M] [--bundle FILENAME] [SOURCE]')),
3029 _('hg incoming [-p] [-n] [-M] [--bundle FILENAME] [SOURCE]')),
3030 "^init": (init, [], _('hg init [DEST]')),
3030 "^init": (init, [], _('hg init [DEST]')),
3031 "locate":
3031 "locate":
3032 (locate,
3032 (locate,
3033 [('r', 'rev', '', _('search the repository as it stood at rev')),
3033 [('r', 'rev', '', _('search the repository as it stood at rev')),
3034 ('0', 'print0', None,
3034 ('0', 'print0', None,
3035 _('end filenames with NUL, for use with xargs')),
3035 _('end filenames with NUL, for use with xargs')),
3036 ('f', 'fullpath', None,
3036 ('f', 'fullpath', None,
3037 _('print complete paths from the filesystem root')),
3037 _('print complete paths from the filesystem root')),
3038 ('I', 'include', [], _('include names matching the given patterns')),
3038 ('I', 'include', [], _('include names matching the given patterns')),
3039 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3039 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3040 _('hg locate [OPTION]... [PATTERN]...')),
3040 _('hg locate [OPTION]... [PATTERN]...')),
3041 "^log|history":
3041 "^log|history":
3042 (log,
3042 (log,
3043 [('b', 'branches', None, _('show branches')),
3043 [('b', 'branches', None, _('show branches')),
3044 ('k', 'keyword', [], _('search for a keyword')),
3044 ('k', 'keyword', [], _('search for a keyword')),
3045 ('l', 'limit', '', _('limit number of changes displayed')),
3045 ('l', 'limit', '', _('limit number of changes displayed')),
3046 ('r', 'rev', [], _('show the specified revision or range')),
3046 ('r', 'rev', [], _('show the specified revision or range')),
3047 ('M', 'no-merges', None, _('do not show merges')),
3047 ('M', 'no-merges', None, _('do not show merges')),
3048 ('', 'style', '', _('display using template map file')),
3048 ('', 'style', '', _('display using template map file')),
3049 ('m', 'only-merges', None, _('show only merges')),
3049 ('m', 'only-merges', None, _('show only merges')),
3050 ('p', 'patch', None, _('show patch')),
3050 ('p', 'patch', None, _('show patch')),
3051 ('', 'template', '', _('display with template')),
3051 ('', 'template', '', _('display with template')),
3052 ('I', 'include', [], _('include names matching the given patterns')),
3052 ('I', 'include', [], _('include names matching the given patterns')),
3053 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3053 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3054 _('hg log [OPTION]... [FILE]')),
3054 _('hg log [OPTION]... [FILE]')),
3055 "manifest": (manifest, [], _('hg manifest [REV]')),
3055 "manifest": (manifest, [], _('hg manifest [REV]')),
3056 "merge":
3056 "merge":
3057 (merge,
3057 (merge,
3058 [('b', 'branch', '', _('merge with head of a specific branch')),
3058 [('b', 'branch', '', _('merge with head of a specific branch')),
3059 ('f', 'force', None, _('force a merge with outstanding changes'))],
3059 ('f', 'force', None, _('force a merge with outstanding changes'))],
3060 _('hg merge [-b TAG] [-f] [REV]')),
3060 _('hg merge [-b TAG] [-f] [REV]')),
3061 "outgoing|out": (outgoing,
3061 "outgoing|out": (outgoing,
3062 [('M', 'no-merges', None, _('do not show merges')),
3062 [('M', 'no-merges', None, _('do not show merges')),
3063 ('f', 'force', None,
3063 ('f', 'force', None,
3064 _('run even when remote repository is unrelated')),
3064 _('run even when remote repository is unrelated')),
3065 ('p', 'patch', None, _('show patch')),
3065 ('p', 'patch', None, _('show patch')),
3066 ('', 'style', '', _('display using template map file')),
3066 ('', 'style', '', _('display using template map file')),
3067 ('n', 'newest-first', None, _('show newest record first')),
3067 ('n', 'newest-first', None, _('show newest record first')),
3068 ('', 'template', '', _('display with template')),
3068 ('', 'template', '', _('display with template')),
3069 ('e', 'ssh', '', _('specify ssh command to use')),
3069 ('e', 'ssh', '', _('specify ssh command to use')),
3070 ('', 'remotecmd', '',
3070 ('', 'remotecmd', '',
3071 _('specify hg command to run on the remote side'))],
3071 _('specify hg command to run on the remote side'))],
3072 _('hg outgoing [-M] [-p] [-n] [DEST]')),
3072 _('hg outgoing [-M] [-p] [-n] [DEST]')),
3073 "^parents":
3073 "^parents":
3074 (parents,
3074 (parents,
3075 [('b', 'branches', None, _('show branches')),
3075 [('b', 'branches', None, _('show branches')),
3076 ('', 'style', '', _('display using template map file')),
3076 ('', 'style', '', _('display using template map file')),
3077 ('', 'template', '', _('display with template'))],
3077 ('', 'template', '', _('display with template'))],
3078 _('hg parents [-b] [REV]')),
3078 _('hg parents [-b] [REV]')),
3079 "paths": (paths, [], _('hg paths [NAME]')),
3079 "paths": (paths, [], _('hg paths [NAME]')),
3080 "^pull":
3080 "^pull":
3081 (pull,
3081 (pull,
3082 [('u', 'update', None,
3082 [('u', 'update', None,
3083 _('update the working directory to tip after pull')),
3083 _('update the working directory to tip after pull')),
3084 ('e', 'ssh', '', _('specify ssh command to use')),
3084 ('e', 'ssh', '', _('specify ssh command to use')),
3085 ('f', 'force', None,
3085 ('f', 'force', None,
3086 _('run even when remote repository is unrelated')),
3086 _('run even when remote repository is unrelated')),
3087 ('r', 'rev', [], _('a specific revision you would like to pull')),
3087 ('r', 'rev', [], _('a specific revision you would like to pull')),
3088 ('', 'remotecmd', '',
3088 ('', 'remotecmd', '',
3089 _('specify hg command to run on the remote side'))],
3089 _('specify hg command to run on the remote side'))],
3090 _('hg pull [-u] [-e FILE] [-r REV]... [--remotecmd FILE] [SOURCE]')),
3090 _('hg pull [-u] [-e FILE] [-r REV]... [--remotecmd FILE] [SOURCE]')),
3091 "^push":
3091 "^push":
3092 (push,
3092 (push,
3093 [('f', 'force', None, _('force push')),
3093 [('f', 'force', None, _('force push')),
3094 ('e', 'ssh', '', _('specify ssh command to use')),
3094 ('e', 'ssh', '', _('specify ssh command to use')),
3095 ('r', 'rev', [], _('a specific revision you would like to push')),
3095 ('r', 'rev', [], _('a specific revision you would like to push')),
3096 ('', 'remotecmd', '',
3096 ('', 'remotecmd', '',
3097 _('specify hg command to run on the remote side'))],
3097 _('specify hg command to run on the remote side'))],
3098 _('hg push [-f] [-e FILE] [-r REV]... [--remotecmd FILE] [DEST]')),
3098 _('hg push [-f] [-e FILE] [-r REV]... [--remotecmd FILE] [DEST]')),
3099 "debugrawcommit|rawcommit":
3099 "debugrawcommit|rawcommit":
3100 (rawcommit,
3100 (rawcommit,
3101 [('p', 'parent', [], _('parent')),
3101 [('p', 'parent', [], _('parent')),
3102 ('d', 'date', '', _('date code')),
3102 ('d', 'date', '', _('date code')),
3103 ('u', 'user', '', _('user')),
3103 ('u', 'user', '', _('user')),
3104 ('F', 'files', '', _('file list')),
3104 ('F', 'files', '', _('file list')),
3105 ('m', 'message', '', _('commit message')),
3105 ('m', 'message', '', _('commit message')),
3106 ('l', 'logfile', '', _('commit message file'))],
3106 ('l', 'logfile', '', _('commit message file'))],
3107 _('hg debugrawcommit [OPTION]... [FILE]...')),
3107 _('hg debugrawcommit [OPTION]... [FILE]...')),
3108 "recover": (recover, [], _('hg recover')),
3108 "recover": (recover, [], _('hg recover')),
3109 "^remove|rm":
3109 "^remove|rm":
3110 (remove,
3110 (remove,
3111 [('f', 'force', None, _('remove file even if modified')),
3111 [('f', 'force', None, _('remove file even if modified')),
3112 ('I', 'include', [], _('include names matching the given patterns')),
3112 ('I', 'include', [], _('include names matching the given patterns')),
3113 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3113 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3114 _('hg remove [OPTION]... FILE...')),
3114 _('hg remove [OPTION]... FILE...')),
3115 "rename|mv":
3115 "rename|mv":
3116 (rename,
3116 (rename,
3117 [('A', 'after', None, _('record a rename that has already occurred')),
3117 [('A', 'after', None, _('record a rename that has already occurred')),
3118 ('f', 'force', None,
3118 ('f', 'force', None,
3119 _('forcibly copy over an existing managed file')),
3119 _('forcibly copy over an existing managed file')),
3120 ('I', 'include', [], _('include names matching the given patterns')),
3120 ('I', 'include', [], _('include names matching the given patterns')),
3121 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3121 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3122 _('hg rename [OPTION]... SOURCE... DEST')),
3122 _('hg rename [OPTION]... SOURCE... DEST')),
3123 "^revert":
3123 "^revert":
3124 (revert,
3124 (revert,
3125 [('r', 'rev', '', _('revision to revert to')),
3125 [('r', 'rev', '', _('revision to revert to')),
3126 ('', 'no-backup', None, _('do not save backup copies of files')),
3126 ('', 'no-backup', None, _('do not save backup copies of files')),
3127 ('I', 'include', [], _('include names matching given patterns')),
3127 ('I', 'include', [], _('include names matching given patterns')),
3128 ('X', 'exclude', [], _('exclude names matching given patterns'))],
3128 ('X', 'exclude', [], _('exclude names matching given patterns'))],
3129 _('hg revert [-r REV] [NAME]...')),
3129 _('hg revert [-r REV] [NAME]...')),
3130 "root": (root, [], _('hg root')),
3130 "root": (root, [], _('hg root')),
3131 "^serve":
3131 "^serve":
3132 (serve,
3132 (serve,
3133 [('A', 'accesslog', '', _('name of access log file to write to')),
3133 [('A', 'accesslog', '', _('name of access log file to write to')),
3134 ('d', 'daemon', None, _('run server in background')),
3134 ('d', 'daemon', None, _('run server in background')),
3135 ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
3135 ('', 'daemon-pipefds', '', _('used internally by daemon mode')),
3136 ('E', 'errorlog', '', _('name of error log file to write to')),
3136 ('E', 'errorlog', '', _('name of error log file to write to')),
3137 ('p', 'port', 0, _('port to use (default: 8000)')),
3137 ('p', 'port', 0, _('port to use (default: 8000)')),
3138 ('a', 'address', '', _('address to use')),
3138 ('a', 'address', '', _('address to use')),
3139 ('n', 'name', '',
3139 ('n', 'name', '',
3140 _('name to show in web pages (default: working dir)')),
3140 _('name to show in web pages (default: working dir)')),
3141 ('', 'webdir-conf', '', _('name of the webdir config file'
3141 ('', 'webdir-conf', '', _('name of the webdir config file'
3142 ' (serve more than one repo)')),
3142 ' (serve more than one repo)')),
3143 ('', 'pid-file', '', _('name of file to write process ID to')),
3143 ('', 'pid-file', '', _('name of file to write process ID to')),
3144 ('', 'stdio', None, _('for remote clients')),
3144 ('', 'stdio', None, _('for remote clients')),
3145 ('t', 'templates', '', _('web templates to use')),
3145 ('t', 'templates', '', _('web templates to use')),
3146 ('', 'style', '', _('template style to use')),
3146 ('', 'style', '', _('template style to use')),
3147 ('6', 'ipv6', None, _('use IPv6 in addition to IPv4'))],
3147 ('6', 'ipv6', None, _('use IPv6 in addition to IPv4'))],
3148 _('hg serve [OPTION]...')),
3148 _('hg serve [OPTION]...')),
3149 "^status|st":
3149 "^status|st":
3150 (status,
3150 (status,
3151 [('m', 'modified', None, _('show only modified files')),
3151 [('m', 'modified', None, _('show only modified files')),
3152 ('a', 'added', None, _('show only added files')),
3152 ('a', 'added', None, _('show only added files')),
3153 ('r', 'removed', None, _('show only removed files')),
3153 ('r', 'removed', None, _('show only removed files')),
3154 ('d', 'deleted', None, _('show only deleted (but tracked) files')),
3154 ('d', 'deleted', None, _('show only deleted (but tracked) files')),
3155 ('u', 'unknown', None, _('show only unknown (not tracked) files')),
3155 ('u', 'unknown', None, _('show only unknown (not tracked) files')),
3156 ('i', 'ignored', None, _('show ignored files')),
3156 ('i', 'ignored', None, _('show ignored files')),
3157 ('n', 'no-status', None, _('hide status prefix')),
3157 ('n', 'no-status', None, _('hide status prefix')),
3158 ('0', 'print0', None,
3158 ('0', 'print0', None,
3159 _('end filenames with NUL, for use with xargs')),
3159 _('end filenames with NUL, for use with xargs')),
3160 ('I', 'include', [], _('include names matching the given patterns')),
3160 ('I', 'include', [], _('include names matching the given patterns')),
3161 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3161 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
3162 _('hg status [OPTION]... [FILE]...')),
3162 _('hg status [OPTION]... [FILE]...')),
3163 "tag":
3163 "tag":
3164 (tag,
3164 (tag,
3165 [('l', 'local', None, _('make the tag local')),
3165 [('l', 'local', None, _('make the tag local')),
3166 ('m', 'message', '', _('message for tag commit log entry')),
3166 ('m', 'message', '', _('message for tag commit log entry')),
3167 ('d', 'date', '', _('record datecode as commit date')),
3167 ('d', 'date', '', _('record datecode as commit date')),
3168 ('u', 'user', '', _('record user as commiter')),
3168 ('u', 'user', '', _('record user as commiter')),
3169 ('r', 'rev', '', _('revision to tag'))],
3169 ('r', 'rev', '', _('revision to tag'))],
3170 _('hg tag [-l] [-m TEXT] [-d DATE] [-u USER] [-r REV] NAME')),
3170 _('hg tag [-l] [-m TEXT] [-d DATE] [-u USER] [-r REV] NAME')),
3171 "tags": (tags, [], _('hg tags')),
3171 "tags": (tags, [], _('hg tags')),
3172 "tip":
3172 "tip":
3173 (tip,
3173 (tip,
3174 [('b', 'branches', None, _('show branches')),
3174 [('b', 'branches', None, _('show branches')),
3175 ('', 'style', '', _('display using template map file')),
3175 ('', 'style', '', _('display using template map file')),
3176 ('p', 'patch', None, _('show patch')),
3176 ('p', 'patch', None, _('show patch')),
3177 ('', 'template', '', _('display with template'))],
3177 ('', 'template', '', _('display with template'))],
3178 _('hg tip [-b] [-p]')),
3178 _('hg tip [-b] [-p]')),
3179 "unbundle":
3179 "unbundle":
3180 (unbundle,
3180 (unbundle,
3181 [('u', 'update', None,
3181 [('u', 'update', None,
3182 _('update the working directory to tip after unbundle'))],
3182 _('update the working directory to tip after unbundle'))],
3183 _('hg unbundle [-u] FILE')),
3183 _('hg unbundle [-u] FILE')),
3184 "undo": (undo, [], _('hg undo')),
3184 "undo": (undo, [], _('hg undo')),
3185 "^update|up|checkout|co":
3185 "^update|up|checkout|co":
3186 (update,
3186 (update,
3187 [('b', 'branch', '', _('checkout the head of a specific branch')),
3187 [('b', 'branch', '', _('checkout the head of a specific branch')),
3188 ('m', 'merge', None, _('allow merging of branches')),
3188 ('m', 'merge', None, _('allow merging of branches')),
3189 ('C', 'clean', None, _('overwrite locally modified files')),
3189 ('C', 'clean', None, _('overwrite locally modified files')),
3190 ('f', 'force', None, _('force a merge with outstanding changes'))],
3190 ('f', 'force', None, _('force a merge with outstanding changes'))],
3191 _('hg update [-b TAG] [-m] [-C] [-f] [REV]')),
3191 _('hg update [-b TAG] [-m] [-C] [-f] [REV]')),
3192 "verify": (verify, [], _('hg verify')),
3192 "verify": (verify, [], _('hg verify')),
3193 "version": (show_version, [], _('hg version')),
3193 "version": (show_version, [], _('hg version')),
3194 }
3194 }
3195
3195
3196 globalopts = [
3196 globalopts = [
3197 ('R', 'repository', '',
3197 ('R', 'repository', '',
3198 _('repository root directory or symbolic path name')),
3198 _('repository root directory or symbolic path name')),
3199 ('', 'cwd', '', _('change working directory')),
3199 ('', 'cwd', '', _('change working directory')),
3200 ('y', 'noninteractive', None,
3200 ('y', 'noninteractive', None,
3201 _('do not prompt, assume \'yes\' for any required answers')),
3201 _('do not prompt, assume \'yes\' for any required answers')),
3202 ('q', 'quiet', None, _('suppress output')),
3202 ('q', 'quiet', None, _('suppress output')),
3203 ('v', 'verbose', None, _('enable additional output')),
3203 ('v', 'verbose', None, _('enable additional output')),
3204 ('', 'debug', None, _('enable debugging output')),
3204 ('', 'debug', None, _('enable debugging output')),
3205 ('', 'debugger', None, _('start debugger')),
3205 ('', 'debugger', None, _('start debugger')),
3206 ('', 'traceback', None, _('print traceback on exception')),
3206 ('', 'traceback', None, _('print traceback on exception')),
3207 ('', 'time', None, _('time how long the command takes')),
3207 ('', 'time', None, _('time how long the command takes')),
3208 ('', 'profile', None, _('print command execution profile')),
3208 ('', 'profile', None, _('print command execution profile')),
3209 ('', 'version', None, _('output version information and exit')),
3209 ('', 'version', None, _('output version information and exit')),
3210 ('h', 'help', None, _('display help and exit')),
3210 ('h', 'help', None, _('display help and exit')),
3211 ]
3211 ]
3212
3212
3213 norepo = ("clone init version help debugancestor debugcomplete debugdata"
3213 norepo = ("clone init version help debugancestor debugcomplete debugdata"
3214 " debugindex debugindexdot")
3214 " debugindex debugindexdot")
3215 optionalrepo = ("paths serve debugconfig")
3215 optionalrepo = ("paths serve debugconfig")
3216
3216
3217 def findpossible(cmd):
3217 def findpossible(cmd):
3218 """
3218 """
3219 Return cmd -> (aliases, command table entry)
3219 Return cmd -> (aliases, command table entry)
3220 for each matching command.
3220 for each matching command.
3221 Return debug commands (or their aliases) only if no normal command matches.
3221 Return debug commands (or their aliases) only if no normal command matches.
3222 """
3222 """
3223 choice = {}
3223 choice = {}
3224 debugchoice = {}
3224 debugchoice = {}
3225 for e in table.keys():
3225 for e in table.keys():
3226 aliases = e.lstrip("^").split("|")
3226 aliases = e.lstrip("^").split("|")
3227 found = None
3227 found = None
3228 if cmd in aliases:
3228 if cmd in aliases:
3229 found = cmd
3229 found = cmd
3230 else:
3230 else:
3231 for a in aliases:
3231 for a in aliases:
3232 if a.startswith(cmd):
3232 if a.startswith(cmd):
3233 found = a
3233 found = a
3234 break
3234 break
3235 if found is not None:
3235 if found is not None:
3236 if aliases[0].startswith("debug"):
3236 if aliases[0].startswith("debug"):
3237 debugchoice[found] = (aliases, table[e])
3237 debugchoice[found] = (aliases, table[e])
3238 else:
3238 else:
3239 choice[found] = (aliases, table[e])
3239 choice[found] = (aliases, table[e])
3240
3240
3241 if not choice and debugchoice:
3241 if not choice and debugchoice:
3242 choice = debugchoice
3242 choice = debugchoice
3243
3243
3244 return choice
3244 return choice
3245
3245
3246 def find(cmd):
3246 def find(cmd):
3247 """Return (aliases, command table entry) for command string."""
3247 """Return (aliases, command table entry) for command string."""
3248 choice = findpossible(cmd)
3248 choice = findpossible(cmd)
3249
3249
3250 if choice.has_key(cmd):
3250 if choice.has_key(cmd):
3251 return choice[cmd]
3251 return choice[cmd]
3252
3252
3253 if len(choice) > 1:
3253 if len(choice) > 1:
3254 clist = choice.keys()
3254 clist = choice.keys()
3255 clist.sort()
3255 clist.sort()
3256 raise AmbiguousCommand(cmd, clist)
3256 raise AmbiguousCommand(cmd, clist)
3257
3257
3258 if choice:
3258 if choice:
3259 return choice.values()[0]
3259 return choice.values()[0]
3260
3260
3261 raise UnknownCommand(cmd)
3261 raise UnknownCommand(cmd)
3262
3262
3263 class SignalInterrupt(Exception):
3264 """Exception raised on SIGTERM and SIGHUP."""
3265
3266 def catchterm(*args):
3263 def catchterm(*args):
3267 raise SignalInterrupt
3264 raise util.SignalInterrupt
3268
3265
3269 def run():
3266 def run():
3270 sys.exit(dispatch(sys.argv[1:]))
3267 sys.exit(dispatch(sys.argv[1:]))
3271
3268
3272 class ParseError(Exception):
3269 class ParseError(Exception):
3273 """Exception raised on errors in parsing the command line."""
3270 """Exception raised on errors in parsing the command line."""
3274
3271
3275 def parse(ui, args):
3272 def parse(ui, args):
3276 options = {}
3273 options = {}
3277 cmdoptions = {}
3274 cmdoptions = {}
3278
3275
3279 try:
3276 try:
3280 args = fancyopts.fancyopts(args, globalopts, options)
3277 args = fancyopts.fancyopts(args, globalopts, options)
3281 except fancyopts.getopt.GetoptError, inst:
3278 except fancyopts.getopt.GetoptError, inst:
3282 raise ParseError(None, inst)
3279 raise ParseError(None, inst)
3283
3280
3284 if args:
3281 if args:
3285 cmd, args = args[0], args[1:]
3282 cmd, args = args[0], args[1:]
3286 aliases, i = find(cmd)
3283 aliases, i = find(cmd)
3287 cmd = aliases[0]
3284 cmd = aliases[0]
3288 defaults = ui.config("defaults", cmd)
3285 defaults = ui.config("defaults", cmd)
3289 if defaults:
3286 if defaults:
3290 args = defaults.split() + args
3287 args = defaults.split() + args
3291 c = list(i[1])
3288 c = list(i[1])
3292 else:
3289 else:
3293 cmd = None
3290 cmd = None
3294 c = []
3291 c = []
3295
3292
3296 # combine global options into local
3293 # combine global options into local
3297 for o in globalopts:
3294 for o in globalopts:
3298 c.append((o[0], o[1], options[o[1]], o[3]))
3295 c.append((o[0], o[1], options[o[1]], o[3]))
3299
3296
3300 try:
3297 try:
3301 args = fancyopts.fancyopts(args, c, cmdoptions)
3298 args = fancyopts.fancyopts(args, c, cmdoptions)
3302 except fancyopts.getopt.GetoptError, inst:
3299 except fancyopts.getopt.GetoptError, inst:
3303 raise ParseError(cmd, inst)
3300 raise ParseError(cmd, inst)
3304
3301
3305 # separate global options back out
3302 # separate global options back out
3306 for o in globalopts:
3303 for o in globalopts:
3307 n = o[1]
3304 n = o[1]
3308 options[n] = cmdoptions[n]
3305 options[n] = cmdoptions[n]
3309 del cmdoptions[n]
3306 del cmdoptions[n]
3310
3307
3311 return (cmd, cmd and i[0] or None, args, options, cmdoptions)
3308 return (cmd, cmd and i[0] or None, args, options, cmdoptions)
3312
3309
3313 def dispatch(args):
3310 def dispatch(args):
3314 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM':
3311 for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM':
3315 num = getattr(signal, name, None)
3312 num = getattr(signal, name, None)
3316 if num: signal.signal(num, catchterm)
3313 if num: signal.signal(num, catchterm)
3317
3314
3318 try:
3315 try:
3319 u = ui.ui()
3316 u = ui.ui()
3320 except util.Abort, inst:
3317 except util.Abort, inst:
3321 sys.stderr.write(_("abort: %s\n") % inst)
3318 sys.stderr.write(_("abort: %s\n") % inst)
3322 return -1
3319 return -1
3323
3320
3324 external = []
3321 external = []
3325 for x in u.extensions():
3322 for x in u.extensions():
3326 try:
3323 try:
3327 if x[1]:
3324 if x[1]:
3328 mod = imp.load_source(x[0], x[1])
3325 mod = imp.load_source(x[0], x[1])
3329 else:
3326 else:
3330 def importh(name):
3327 def importh(name):
3331 mod = __import__(name)
3328 mod = __import__(name)
3332 components = name.split('.')
3329 components = name.split('.')
3333 for comp in components[1:]:
3330 for comp in components[1:]:
3334 mod = getattr(mod, comp)
3331 mod = getattr(mod, comp)
3335 return mod
3332 return mod
3336 try:
3333 try:
3337 mod = importh("hgext." + x[0])
3334 mod = importh("hgext." + x[0])
3338 except ImportError:
3335 except ImportError:
3339 mod = importh(x[0])
3336 mod = importh(x[0])
3340 external.append(mod)
3337 external.append(mod)
3341 except Exception, inst:
3338 except Exception, inst:
3342 u.warn(_("*** failed to import extension %s: %s\n") % (x[0], inst))
3339 u.warn(_("*** failed to import extension %s: %s\n") % (x[0], inst))
3343 if "--traceback" in sys.argv[1:]:
3340 if "--traceback" in sys.argv[1:]:
3344 traceback.print_exc()
3341 traceback.print_exc()
3345 return 1
3342 return 1
3346 continue
3343 continue
3347
3344
3348 for x in external:
3345 for x in external:
3349 cmdtable = getattr(x, 'cmdtable', {})
3346 cmdtable = getattr(x, 'cmdtable', {})
3350 for t in cmdtable:
3347 for t in cmdtable:
3351 if t in table:
3348 if t in table:
3352 u.warn(_("module %s overrides %s\n") % (x.__name__, t))
3349 u.warn(_("module %s overrides %s\n") % (x.__name__, t))
3353 table.update(cmdtable)
3350 table.update(cmdtable)
3354
3351
3355 try:
3352 try:
3356 cmd, func, args, options, cmdoptions = parse(u, args)
3353 cmd, func, args, options, cmdoptions = parse(u, args)
3357 if options["time"]:
3354 if options["time"]:
3358 def get_times():
3355 def get_times():
3359 t = os.times()
3356 t = os.times()
3360 if t[4] == 0.0: # Windows leaves this as zero, so use time.clock()
3357 if t[4] == 0.0: # Windows leaves this as zero, so use time.clock()
3361 t = (t[0], t[1], t[2], t[3], time.clock())
3358 t = (t[0], t[1], t[2], t[3], time.clock())
3362 return t
3359 return t
3363 s = get_times()
3360 s = get_times()
3364 def print_time():
3361 def print_time():
3365 t = get_times()
3362 t = get_times()
3366 u.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") %
3363 u.warn(_("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n") %
3367 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3]))
3364 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3]))
3368 atexit.register(print_time)
3365 atexit.register(print_time)
3369
3366
3370 u.updateopts(options["verbose"], options["debug"], options["quiet"],
3367 u.updateopts(options["verbose"], options["debug"], options["quiet"],
3371 not options["noninteractive"])
3368 not options["noninteractive"])
3372
3369
3373 # enter the debugger before command execution
3370 # enter the debugger before command execution
3374 if options['debugger']:
3371 if options['debugger']:
3375 pdb.set_trace()
3372 pdb.set_trace()
3376
3373
3377 try:
3374 try:
3378 if options['cwd']:
3375 if options['cwd']:
3379 try:
3376 try:
3380 os.chdir(options['cwd'])
3377 os.chdir(options['cwd'])
3381 except OSError, inst:
3378 except OSError, inst:
3382 raise util.Abort('%s: %s' %
3379 raise util.Abort('%s: %s' %
3383 (options['cwd'], inst.strerror))
3380 (options['cwd'], inst.strerror))
3384
3381
3385 path = u.expandpath(options["repository"]) or ""
3382 path = u.expandpath(options["repository"]) or ""
3386 repo = path and hg.repository(u, path=path) or None
3383 repo = path and hg.repository(u, path=path) or None
3387
3384
3388 if options['help']:
3385 if options['help']:
3389 return help_(u, cmd, options['version'])
3386 return help_(u, cmd, options['version'])
3390 elif options['version']:
3387 elif options['version']:
3391 return show_version(u)
3388 return show_version(u)
3392 elif not cmd:
3389 elif not cmd:
3393 return help_(u, 'shortlist')
3390 return help_(u, 'shortlist')
3394
3391
3395 if cmd not in norepo.split():
3392 if cmd not in norepo.split():
3396 try:
3393 try:
3397 if not repo:
3394 if not repo:
3398 repo = hg.repository(u, path=path)
3395 repo = hg.repository(u, path=path)
3399 u = repo.ui
3396 u = repo.ui
3400 for x in external:
3397 for x in external:
3401 if hasattr(x, 'reposetup'):
3398 if hasattr(x, 'reposetup'):
3402 x.reposetup(u, repo)
3399 x.reposetup(u, repo)
3403 except hg.RepoError:
3400 except hg.RepoError:
3404 if cmd not in optionalrepo.split():
3401 if cmd not in optionalrepo.split():
3405 raise
3402 raise
3406 d = lambda: func(u, repo, *args, **cmdoptions)
3403 d = lambda: func(u, repo, *args, **cmdoptions)
3407 else:
3404 else:
3408 d = lambda: func(u, *args, **cmdoptions)
3405 d = lambda: func(u, *args, **cmdoptions)
3409
3406
3410 try:
3407 try:
3411 if options['profile']:
3408 if options['profile']:
3412 import hotshot, hotshot.stats
3409 import hotshot, hotshot.stats
3413 prof = hotshot.Profile("hg.prof")
3410 prof = hotshot.Profile("hg.prof")
3414 try:
3411 try:
3415 try:
3412 try:
3416 return prof.runcall(d)
3413 return prof.runcall(d)
3417 except:
3414 except:
3418 try:
3415 try:
3419 u.warn(_('exception raised - generating '
3416 u.warn(_('exception raised - generating '
3420 'profile anyway\n'))
3417 'profile anyway\n'))
3421 except:
3418 except:
3422 pass
3419 pass
3423 raise
3420 raise
3424 finally:
3421 finally:
3425 prof.close()
3422 prof.close()
3426 stats = hotshot.stats.load("hg.prof")
3423 stats = hotshot.stats.load("hg.prof")
3427 stats.strip_dirs()
3424 stats.strip_dirs()
3428 stats.sort_stats('time', 'calls')
3425 stats.sort_stats('time', 'calls')
3429 stats.print_stats(40)
3426 stats.print_stats(40)
3430 else:
3427 else:
3431 return d()
3428 return d()
3432 finally:
3429 finally:
3433 u.flush()
3430 u.flush()
3434 except:
3431 except:
3435 # enter the debugger when we hit an exception
3432 # enter the debugger when we hit an exception
3436 if options['debugger']:
3433 if options['debugger']:
3437 pdb.post_mortem(sys.exc_info()[2])
3434 pdb.post_mortem(sys.exc_info()[2])
3438 if options['traceback']:
3435 if options['traceback']:
3439 traceback.print_exc()
3436 traceback.print_exc()
3440 raise
3437 raise
3441 except ParseError, inst:
3438 except ParseError, inst:
3442 if inst.args[0]:
3439 if inst.args[0]:
3443 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
3440 u.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
3444 help_(u, inst.args[0])
3441 help_(u, inst.args[0])
3445 else:
3442 else:
3446 u.warn(_("hg: %s\n") % inst.args[1])
3443 u.warn(_("hg: %s\n") % inst.args[1])
3447 help_(u, 'shortlist')
3444 help_(u, 'shortlist')
3448 except AmbiguousCommand, inst:
3445 except AmbiguousCommand, inst:
3449 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
3446 u.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
3450 (inst.args[0], " ".join(inst.args[1])))
3447 (inst.args[0], " ".join(inst.args[1])))
3451 except UnknownCommand, inst:
3448 except UnknownCommand, inst:
3452 u.warn(_("hg: unknown command '%s'\n") % inst.args[0])
3449 u.warn(_("hg: unknown command '%s'\n") % inst.args[0])
3453 help_(u, 'shortlist')
3450 help_(u, 'shortlist')
3454 except hg.RepoError, inst:
3451 except hg.RepoError, inst:
3455 u.warn(_("abort: %s!\n") % inst)
3452 u.warn(_("abort: %s!\n") % inst)
3456 except lock.LockHeld, inst:
3453 except lock.LockHeld, inst:
3457 if inst.errno == errno.ETIMEDOUT:
3454 if inst.errno == errno.ETIMEDOUT:
3458 reason = _('timed out waiting for lock held by %s') % inst.locker
3455 reason = _('timed out waiting for lock held by %s') % inst.locker
3459 else:
3456 else:
3460 reason = _('lock held by %s') % inst.locker
3457 reason = _('lock held by %s') % inst.locker
3461 u.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
3458 u.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
3462 except lock.LockUnavailable, inst:
3459 except lock.LockUnavailable, inst:
3463 u.warn(_("abort: could not lock %s: %s\n") %
3460 u.warn(_("abort: could not lock %s: %s\n") %
3464 (inst.desc or inst.filename, inst.strerror))
3461 (inst.desc or inst.filename, inst.strerror))
3465 except revlog.RevlogError, inst:
3462 except revlog.RevlogError, inst:
3466 u.warn(_("abort: "), inst, "!\n")
3463 u.warn(_("abort: "), inst, "!\n")
3467 except SignalInterrupt:
3464 except util.SignalInterrupt:
3468 u.warn(_("killed!\n"))
3465 u.warn(_("killed!\n"))
3469 except KeyboardInterrupt:
3466 except KeyboardInterrupt:
3470 try:
3467 try:
3471 u.warn(_("interrupted!\n"))
3468 u.warn(_("interrupted!\n"))
3472 except IOError, inst:
3469 except IOError, inst:
3473 if inst.errno == errno.EPIPE:
3470 if inst.errno == errno.EPIPE:
3474 if u.debugflag:
3471 if u.debugflag:
3475 u.warn(_("\nbroken pipe\n"))
3472 u.warn(_("\nbroken pipe\n"))
3476 else:
3473 else:
3477 raise
3474 raise
3478 except IOError, inst:
3475 except IOError, inst:
3479 if hasattr(inst, "code"):
3476 if hasattr(inst, "code"):
3480 u.warn(_("abort: %s\n") % inst)
3477 u.warn(_("abort: %s\n") % inst)
3481 elif hasattr(inst, "reason"):
3478 elif hasattr(inst, "reason"):
3482 u.warn(_("abort: error: %s\n") % inst.reason[1])
3479 u.warn(_("abort: error: %s\n") % inst.reason[1])
3483 elif hasattr(inst, "args") and inst[0] == errno.EPIPE:
3480 elif hasattr(inst, "args") and inst[0] == errno.EPIPE:
3484 if u.debugflag:
3481 if u.debugflag:
3485 u.warn(_("broken pipe\n"))
3482 u.warn(_("broken pipe\n"))
3486 elif getattr(inst, "strerror", None):
3483 elif getattr(inst, "strerror", None):
3487 if getattr(inst, "filename", None):
3484 if getattr(inst, "filename", None):
3488 u.warn(_("abort: %s - %s\n") % (inst.strerror, inst.filename))
3485 u.warn(_("abort: %s - %s\n") % (inst.strerror, inst.filename))
3489 else:
3486 else:
3490 u.warn(_("abort: %s\n") % inst.strerror)
3487 u.warn(_("abort: %s\n") % inst.strerror)
3491 else:
3488 else:
3492 raise
3489 raise
3493 except OSError, inst:
3490 except OSError, inst:
3494 if hasattr(inst, "filename"):
3491 if hasattr(inst, "filename"):
3495 u.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
3492 u.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
3496 else:
3493 else:
3497 u.warn(_("abort: %s\n") % inst.strerror)
3494 u.warn(_("abort: %s\n") % inst.strerror)
3498 except util.Abort, inst:
3495 except util.Abort, inst:
3499 u.warn(_('abort: '), inst.args[0] % inst.args[1:], '\n')
3496 u.warn(_('abort: '), inst.args[0] % inst.args[1:], '\n')
3500 except TypeError, inst:
3497 except TypeError, inst:
3501 # was this an argument error?
3498 # was this an argument error?
3502 tb = traceback.extract_tb(sys.exc_info()[2])
3499 tb = traceback.extract_tb(sys.exc_info()[2])
3503 if len(tb) > 2: # no
3500 if len(tb) > 2: # no
3504 raise
3501 raise
3505 u.debug(inst, "\n")
3502 u.debug(inst, "\n")
3506 u.warn(_("%s: invalid arguments\n") % cmd)
3503 u.warn(_("%s: invalid arguments\n") % cmd)
3507 help_(u, cmd)
3504 help_(u, cmd)
3508 except SystemExit, inst:
3505 except SystemExit, inst:
3509 # Commands shouldn't sys.exit directly, but give a return code.
3506 # Commands shouldn't sys.exit directly, but give a return code.
3510 # Just in case catch this and and pass exit code to caller.
3507 # Just in case catch this and and pass exit code to caller.
3511 return inst.code
3508 return inst.code
3512 except:
3509 except:
3513 u.warn(_("** unknown exception encountered, details follow\n"))
3510 u.warn(_("** unknown exception encountered, details follow\n"))
3514 u.warn(_("** report bug details to mercurial@selenic.com\n"))
3511 u.warn(_("** report bug details to mercurial@selenic.com\n"))
3515 u.warn(_("** Mercurial Distributed SCM (version %s)\n")
3512 u.warn(_("** Mercurial Distributed SCM (version %s)\n")
3516 % version.get_version())
3513 % version.get_version())
3517 raise
3514 raise
3518
3515
3519 return -1
3516 return -1
@@ -1,845 +1,848
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
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 This contains helper routines that are independent of the SCM core and hide
9 This contains helper routines that are independent of the SCM core and hide
10 platform-specific details from the core.
10 platform-specific details from the core.
11 """
11 """
12
12
13 import os, errno
13 import os, errno
14 from i18n import gettext as _
14 from i18n import gettext as _
15 from demandload import *
15 from demandload import *
16 demandload(globals(), "cStringIO errno popen2 re shutil sys tempfile")
16 demandload(globals(), "cStringIO errno popen2 re shutil sys tempfile")
17 demandload(globals(), "threading time")
17 demandload(globals(), "threading time")
18
18
19 class SignalInterrupt(Exception):
20 """Exception raised on SIGTERM and SIGHUP."""
21
19 def pipefilter(s, cmd):
22 def pipefilter(s, cmd):
20 '''filter string S through command CMD, returning its output'''
23 '''filter string S through command CMD, returning its output'''
21 (pout, pin) = popen2.popen2(cmd, -1, 'b')
24 (pout, pin) = popen2.popen2(cmd, -1, 'b')
22 def writer():
25 def writer():
23 try:
26 try:
24 pin.write(s)
27 pin.write(s)
25 pin.close()
28 pin.close()
26 except IOError, inst:
29 except IOError, inst:
27 if inst.errno != errno.EPIPE:
30 if inst.errno != errno.EPIPE:
28 raise
31 raise
29
32
30 # we should use select instead on UNIX, but this will work on most
33 # we should use select instead on UNIX, but this will work on most
31 # systems, including Windows
34 # systems, including Windows
32 w = threading.Thread(target=writer)
35 w = threading.Thread(target=writer)
33 w.start()
36 w.start()
34 f = pout.read()
37 f = pout.read()
35 pout.close()
38 pout.close()
36 w.join()
39 w.join()
37 return f
40 return f
38
41
39 def tempfilter(s, cmd):
42 def tempfilter(s, cmd):
40 '''filter string S through a pair of temporary files with CMD.
43 '''filter string S through a pair of temporary files with CMD.
41 CMD is used as a template to create the real command to be run,
44 CMD is used as a template to create the real command to be run,
42 with the strings INFILE and OUTFILE replaced by the real names of
45 with the strings INFILE and OUTFILE replaced by the real names of
43 the temporary files generated.'''
46 the temporary files generated.'''
44 inname, outname = None, None
47 inname, outname = None, None
45 try:
48 try:
46 infd, inname = tempfile.mkstemp(prefix='hgfin')
49 infd, inname = tempfile.mkstemp(prefix='hgfin')
47 fp = os.fdopen(infd, 'wb')
50 fp = os.fdopen(infd, 'wb')
48 fp.write(s)
51 fp.write(s)
49 fp.close()
52 fp.close()
50 outfd, outname = tempfile.mkstemp(prefix='hgfout')
53 outfd, outname = tempfile.mkstemp(prefix='hgfout')
51 os.close(outfd)
54 os.close(outfd)
52 cmd = cmd.replace('INFILE', inname)
55 cmd = cmd.replace('INFILE', inname)
53 cmd = cmd.replace('OUTFILE', outname)
56 cmd = cmd.replace('OUTFILE', outname)
54 code = os.system(cmd)
57 code = os.system(cmd)
55 if code: raise Abort(_("command '%s' failed: %s") %
58 if code: raise Abort(_("command '%s' failed: %s") %
56 (cmd, explain_exit(code)))
59 (cmd, explain_exit(code)))
57 return open(outname, 'rb').read()
60 return open(outname, 'rb').read()
58 finally:
61 finally:
59 try:
62 try:
60 if inname: os.unlink(inname)
63 if inname: os.unlink(inname)
61 except: pass
64 except: pass
62 try:
65 try:
63 if outname: os.unlink(outname)
66 if outname: os.unlink(outname)
64 except: pass
67 except: pass
65
68
66 filtertable = {
69 filtertable = {
67 'tempfile:': tempfilter,
70 'tempfile:': tempfilter,
68 'pipe:': pipefilter,
71 'pipe:': pipefilter,
69 }
72 }
70
73
71 def filter(s, cmd):
74 def filter(s, cmd):
72 "filter a string through a command that transforms its input to its output"
75 "filter a string through a command that transforms its input to its output"
73 for name, fn in filtertable.iteritems():
76 for name, fn in filtertable.iteritems():
74 if cmd.startswith(name):
77 if cmd.startswith(name):
75 return fn(s, cmd[len(name):].lstrip())
78 return fn(s, cmd[len(name):].lstrip())
76 return pipefilter(s, cmd)
79 return pipefilter(s, cmd)
77
80
78 def find_in_path(name, path, default=None):
81 def find_in_path(name, path, default=None):
79 '''find name in search path. path can be string (will be split
82 '''find name in search path. path can be string (will be split
80 with os.pathsep), or iterable thing that returns strings. if name
83 with os.pathsep), or iterable thing that returns strings. if name
81 found, return path to name. else return default.'''
84 found, return path to name. else return default.'''
82 if isinstance(path, str):
85 if isinstance(path, str):
83 path = path.split(os.pathsep)
86 path = path.split(os.pathsep)
84 for p in path:
87 for p in path:
85 p_name = os.path.join(p, name)
88 p_name = os.path.join(p, name)
86 if os.path.exists(p_name):
89 if os.path.exists(p_name):
87 return p_name
90 return p_name
88 return default
91 return default
89
92
90 def patch(strip, patchname, ui):
93 def patch(strip, patchname, ui):
91 """apply the patch <patchname> to the working directory.
94 """apply the patch <patchname> to the working directory.
92 a list of patched files is returned"""
95 a list of patched files is returned"""
93 patcher = find_in_path('gpatch', os.environ.get('PATH', ''), 'patch')
96 patcher = find_in_path('gpatch', os.environ.get('PATH', ''), 'patch')
94 fp = os.popen('"%s" -p%d < "%s"' % (patcher, strip, patchname))
97 fp = os.popen('"%s" -p%d < "%s"' % (patcher, strip, patchname))
95 files = {}
98 files = {}
96 for line in fp:
99 for line in fp:
97 line = line.rstrip()
100 line = line.rstrip()
98 ui.status("%s\n" % line)
101 ui.status("%s\n" % line)
99 if line.startswith('patching file '):
102 if line.startswith('patching file '):
100 pf = parse_patch_output(line)
103 pf = parse_patch_output(line)
101 files.setdefault(pf, 1)
104 files.setdefault(pf, 1)
102 code = fp.close()
105 code = fp.close()
103 if code:
106 if code:
104 raise Abort(_("patch command failed: %s") % explain_exit(code)[0])
107 raise Abort(_("patch command failed: %s") % explain_exit(code)[0])
105 return files.keys()
108 return files.keys()
106
109
107 def binary(s):
110 def binary(s):
108 """return true if a string is binary data using diff's heuristic"""
111 """return true if a string is binary data using diff's heuristic"""
109 if s and '\0' in s[:4096]:
112 if s and '\0' in s[:4096]:
110 return True
113 return True
111 return False
114 return False
112
115
113 def unique(g):
116 def unique(g):
114 """return the uniq elements of iterable g"""
117 """return the uniq elements of iterable g"""
115 seen = {}
118 seen = {}
116 for f in g:
119 for f in g:
117 if f not in seen:
120 if f not in seen:
118 seen[f] = 1
121 seen[f] = 1
119 yield f
122 yield f
120
123
121 class Abort(Exception):
124 class Abort(Exception):
122 """Raised if a command needs to print an error and exit."""
125 """Raised if a command needs to print an error and exit."""
123
126
124 def always(fn): return True
127 def always(fn): return True
125 def never(fn): return False
128 def never(fn): return False
126
129
127 def patkind(name, dflt_pat='glob'):
130 def patkind(name, dflt_pat='glob'):
128 """Split a string into an optional pattern kind prefix and the
131 """Split a string into an optional pattern kind prefix and the
129 actual pattern."""
132 actual pattern."""
130 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
133 for prefix in 're', 'glob', 'path', 'relglob', 'relpath', 'relre':
131 if name.startswith(prefix + ':'): return name.split(':', 1)
134 if name.startswith(prefix + ':'): return name.split(':', 1)
132 return dflt_pat, name
135 return dflt_pat, name
133
136
134 def globre(pat, head='^', tail='$'):
137 def globre(pat, head='^', tail='$'):
135 "convert a glob pattern into a regexp"
138 "convert a glob pattern into a regexp"
136 i, n = 0, len(pat)
139 i, n = 0, len(pat)
137 res = ''
140 res = ''
138 group = False
141 group = False
139 def peek(): return i < n and pat[i]
142 def peek(): return i < n and pat[i]
140 while i < n:
143 while i < n:
141 c = pat[i]
144 c = pat[i]
142 i = i+1
145 i = i+1
143 if c == '*':
146 if c == '*':
144 if peek() == '*':
147 if peek() == '*':
145 i += 1
148 i += 1
146 res += '.*'
149 res += '.*'
147 else:
150 else:
148 res += '[^/]*'
151 res += '[^/]*'
149 elif c == '?':
152 elif c == '?':
150 res += '.'
153 res += '.'
151 elif c == '[':
154 elif c == '[':
152 j = i
155 j = i
153 if j < n and pat[j] in '!]':
156 if j < n and pat[j] in '!]':
154 j += 1
157 j += 1
155 while j < n and pat[j] != ']':
158 while j < n and pat[j] != ']':
156 j += 1
159 j += 1
157 if j >= n:
160 if j >= n:
158 res += '\\['
161 res += '\\['
159 else:
162 else:
160 stuff = pat[i:j].replace('\\','\\\\')
163 stuff = pat[i:j].replace('\\','\\\\')
161 i = j + 1
164 i = j + 1
162 if stuff[0] == '!':
165 if stuff[0] == '!':
163 stuff = '^' + stuff[1:]
166 stuff = '^' + stuff[1:]
164 elif stuff[0] == '^':
167 elif stuff[0] == '^':
165 stuff = '\\' + stuff
168 stuff = '\\' + stuff
166 res = '%s[%s]' % (res, stuff)
169 res = '%s[%s]' % (res, stuff)
167 elif c == '{':
170 elif c == '{':
168 group = True
171 group = True
169 res += '(?:'
172 res += '(?:'
170 elif c == '}' and group:
173 elif c == '}' and group:
171 res += ')'
174 res += ')'
172 group = False
175 group = False
173 elif c == ',' and group:
176 elif c == ',' and group:
174 res += '|'
177 res += '|'
175 elif c == '\\':
178 elif c == '\\':
176 p = peek()
179 p = peek()
177 if p:
180 if p:
178 i += 1
181 i += 1
179 res += re.escape(p)
182 res += re.escape(p)
180 else:
183 else:
181 res += re.escape(c)
184 res += re.escape(c)
182 else:
185 else:
183 res += re.escape(c)
186 res += re.escape(c)
184 return head + res + tail
187 return head + res + tail
185
188
186 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
189 _globchars = {'[': 1, '{': 1, '*': 1, '?': 1}
187
190
188 def pathto(n1, n2):
191 def pathto(n1, n2):
189 '''return the relative path from one place to another.
192 '''return the relative path from one place to another.
190 this returns a path in the form used by the local filesystem, not hg.'''
193 this returns a path in the form used by the local filesystem, not hg.'''
191 if not n1: return localpath(n2)
194 if not n1: return localpath(n2)
192 a, b = n1.split('/'), n2.split('/')
195 a, b = n1.split('/'), n2.split('/')
193 a.reverse()
196 a.reverse()
194 b.reverse()
197 b.reverse()
195 while a and b and a[-1] == b[-1]:
198 while a and b and a[-1] == b[-1]:
196 a.pop()
199 a.pop()
197 b.pop()
200 b.pop()
198 b.reverse()
201 b.reverse()
199 return os.sep.join((['..'] * len(a)) + b)
202 return os.sep.join((['..'] * len(a)) + b)
200
203
201 def canonpath(root, cwd, myname):
204 def canonpath(root, cwd, myname):
202 """return the canonical path of myname, given cwd and root"""
205 """return the canonical path of myname, given cwd and root"""
203 if root == os.sep:
206 if root == os.sep:
204 rootsep = os.sep
207 rootsep = os.sep
205 else:
208 else:
206 rootsep = root + os.sep
209 rootsep = root + os.sep
207 name = myname
210 name = myname
208 if not os.path.isabs(name):
211 if not os.path.isabs(name):
209 name = os.path.join(root, cwd, name)
212 name = os.path.join(root, cwd, name)
210 name = os.path.normpath(name)
213 name = os.path.normpath(name)
211 if name.startswith(rootsep):
214 if name.startswith(rootsep):
212 name = name[len(rootsep):]
215 name = name[len(rootsep):]
213 audit_path(name)
216 audit_path(name)
214 return pconvert(name)
217 return pconvert(name)
215 elif name == root:
218 elif name == root:
216 return ''
219 return ''
217 else:
220 else:
218 # Determine whether `name' is in the hierarchy at or beneath `root',
221 # Determine whether `name' is in the hierarchy at or beneath `root',
219 # by iterating name=dirname(name) until that causes no change (can't
222 # by iterating name=dirname(name) until that causes no change (can't
220 # check name == '/', because that doesn't work on windows). For each
223 # check name == '/', because that doesn't work on windows). For each
221 # `name', compare dev/inode numbers. If they match, the list `rel'
224 # `name', compare dev/inode numbers. If they match, the list `rel'
222 # holds the reversed list of components making up the relative file
225 # holds the reversed list of components making up the relative file
223 # name we want.
226 # name we want.
224 root_st = os.stat(root)
227 root_st = os.stat(root)
225 rel = []
228 rel = []
226 while True:
229 while True:
227 try:
230 try:
228 name_st = os.stat(name)
231 name_st = os.stat(name)
229 except OSError:
232 except OSError:
230 break
233 break
231 if os.path.samestat(name_st, root_st):
234 if os.path.samestat(name_st, root_st):
232 rel.reverse()
235 rel.reverse()
233 name = os.path.join(*rel)
236 name = os.path.join(*rel)
234 audit_path(name)
237 audit_path(name)
235 return pconvert(name)
238 return pconvert(name)
236 dirname, basename = os.path.split(name)
239 dirname, basename = os.path.split(name)
237 rel.append(basename)
240 rel.append(basename)
238 if dirname == name:
241 if dirname == name:
239 break
242 break
240 name = dirname
243 name = dirname
241
244
242 raise Abort('%s not under root' % myname)
245 raise Abort('%s not under root' % myname)
243
246
244 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
247 def matcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
245 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
248 return _matcher(canonroot, cwd, names, inc, exc, head, 'glob', src)
246
249
247 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
250 def cmdmatcher(canonroot, cwd='', names=['.'], inc=[], exc=[], head='', src=None):
248 if os.name == 'nt':
251 if os.name == 'nt':
249 dflt_pat = 'glob'
252 dflt_pat = 'glob'
250 else:
253 else:
251 dflt_pat = 'relpath'
254 dflt_pat = 'relpath'
252 return _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src)
255 return _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src)
253
256
254 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
257 def _matcher(canonroot, cwd, names, inc, exc, head, dflt_pat, src):
255 """build a function to match a set of file patterns
258 """build a function to match a set of file patterns
256
259
257 arguments:
260 arguments:
258 canonroot - the canonical root of the tree you're matching against
261 canonroot - the canonical root of the tree you're matching against
259 cwd - the current working directory, if relevant
262 cwd - the current working directory, if relevant
260 names - patterns to find
263 names - patterns to find
261 inc - patterns to include
264 inc - patterns to include
262 exc - patterns to exclude
265 exc - patterns to exclude
263 head - a regex to prepend to patterns to control whether a match is rooted
266 head - a regex to prepend to patterns to control whether a match is rooted
264
267
265 a pattern is one of:
268 a pattern is one of:
266 'glob:<rooted glob>'
269 'glob:<rooted glob>'
267 're:<rooted regexp>'
270 're:<rooted regexp>'
268 'path:<rooted path>'
271 'path:<rooted path>'
269 'relglob:<relative glob>'
272 'relglob:<relative glob>'
270 'relpath:<relative path>'
273 'relpath:<relative path>'
271 'relre:<relative regexp>'
274 'relre:<relative regexp>'
272 '<rooted path or regexp>'
275 '<rooted path or regexp>'
273
276
274 returns:
277 returns:
275 a 3-tuple containing
278 a 3-tuple containing
276 - list of explicit non-pattern names passed in
279 - list of explicit non-pattern names passed in
277 - a bool match(filename) function
280 - a bool match(filename) function
278 - a bool indicating if any patterns were passed in
281 - a bool indicating if any patterns were passed in
279
282
280 todo:
283 todo:
281 make head regex a rooted bool
284 make head regex a rooted bool
282 """
285 """
283
286
284 def contains_glob(name):
287 def contains_glob(name):
285 for c in name:
288 for c in name:
286 if c in _globchars: return True
289 if c in _globchars: return True
287 return False
290 return False
288
291
289 def regex(kind, name, tail):
292 def regex(kind, name, tail):
290 '''convert a pattern into a regular expression'''
293 '''convert a pattern into a regular expression'''
291 if kind == 're':
294 if kind == 're':
292 return name
295 return name
293 elif kind == 'path':
296 elif kind == 'path':
294 return '^' + re.escape(name) + '(?:/|$)'
297 return '^' + re.escape(name) + '(?:/|$)'
295 elif kind == 'relglob':
298 elif kind == 'relglob':
296 return head + globre(name, '(?:|.*/)', tail)
299 return head + globre(name, '(?:|.*/)', tail)
297 elif kind == 'relpath':
300 elif kind == 'relpath':
298 return head + re.escape(name) + tail
301 return head + re.escape(name) + tail
299 elif kind == 'relre':
302 elif kind == 'relre':
300 if name.startswith('^'):
303 if name.startswith('^'):
301 return name
304 return name
302 return '.*' + name
305 return '.*' + name
303 return head + globre(name, '', tail)
306 return head + globre(name, '', tail)
304
307
305 def matchfn(pats, tail):
308 def matchfn(pats, tail):
306 """build a matching function from a set of patterns"""
309 """build a matching function from a set of patterns"""
307 if not pats:
310 if not pats:
308 return
311 return
309 matches = []
312 matches = []
310 for k, p in pats:
313 for k, p in pats:
311 try:
314 try:
312 pat = '(?:%s)' % regex(k, p, tail)
315 pat = '(?:%s)' % regex(k, p, tail)
313 matches.append(re.compile(pat).match)
316 matches.append(re.compile(pat).match)
314 except re.error:
317 except re.error:
315 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
318 if src: raise Abort("%s: invalid pattern (%s): %s" % (src, k, p))
316 else: raise Abort("invalid pattern (%s): %s" % (k, p))
319 else: raise Abort("invalid pattern (%s): %s" % (k, p))
317
320
318 def buildfn(text):
321 def buildfn(text):
319 for m in matches:
322 for m in matches:
320 r = m(text)
323 r = m(text)
321 if r:
324 if r:
322 return r
325 return r
323
326
324 return buildfn
327 return buildfn
325
328
326 def globprefix(pat):
329 def globprefix(pat):
327 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
330 '''return the non-glob prefix of a path, e.g. foo/* -> foo'''
328 root = []
331 root = []
329 for p in pat.split(os.sep):
332 for p in pat.split(os.sep):
330 if contains_glob(p): break
333 if contains_glob(p): break
331 root.append(p)
334 root.append(p)
332 return '/'.join(root)
335 return '/'.join(root)
333
336
334 pats = []
337 pats = []
335 files = []
338 files = []
336 roots = []
339 roots = []
337 for kind, name in [patkind(p, dflt_pat) for p in names]:
340 for kind, name in [patkind(p, dflt_pat) for p in names]:
338 if kind in ('glob', 'relpath'):
341 if kind in ('glob', 'relpath'):
339 name = canonpath(canonroot, cwd, name)
342 name = canonpath(canonroot, cwd, name)
340 if name == '':
343 if name == '':
341 kind, name = 'glob', '**'
344 kind, name = 'glob', '**'
342 if kind in ('glob', 'path', 're'):
345 if kind in ('glob', 'path', 're'):
343 pats.append((kind, name))
346 pats.append((kind, name))
344 if kind == 'glob':
347 if kind == 'glob':
345 root = globprefix(name)
348 root = globprefix(name)
346 if root: roots.append(root)
349 if root: roots.append(root)
347 elif kind == 'relpath':
350 elif kind == 'relpath':
348 files.append((kind, name))
351 files.append((kind, name))
349 roots.append(name)
352 roots.append(name)
350
353
351 patmatch = matchfn(pats, '$') or always
354 patmatch = matchfn(pats, '$') or always
352 filematch = matchfn(files, '(?:/|$)') or always
355 filematch = matchfn(files, '(?:/|$)') or always
353 incmatch = always
356 incmatch = always
354 if inc:
357 if inc:
355 incmatch = matchfn(map(patkind, inc), '(?:/|$)')
358 incmatch = matchfn(map(patkind, inc), '(?:/|$)')
356 excmatch = lambda fn: False
359 excmatch = lambda fn: False
357 if exc:
360 if exc:
358 excmatch = matchfn(map(patkind, exc), '(?:/|$)')
361 excmatch = matchfn(map(patkind, exc), '(?:/|$)')
359
362
360 return (roots,
363 return (roots,
361 lambda fn: (incmatch(fn) and not excmatch(fn) and
364 lambda fn: (incmatch(fn) and not excmatch(fn) and
362 (fn.endswith('/') or
365 (fn.endswith('/') or
363 (not pats and not files) or
366 (not pats and not files) or
364 (pats and patmatch(fn)) or
367 (pats and patmatch(fn)) or
365 (files and filematch(fn)))),
368 (files and filematch(fn)))),
366 (inc or exc or (pats and pats != [('glob', '**')])) and True)
369 (inc or exc or (pats and pats != [('glob', '**')])) and True)
367
370
368 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
371 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
369 '''enhanced shell command execution.
372 '''enhanced shell command execution.
370 run with environment maybe modified, maybe in different dir.
373 run with environment maybe modified, maybe in different dir.
371
374
372 if command fails and onerr is None, return status. if ui object,
375 if command fails and onerr is None, return status. if ui object,
373 print error message and return status, else raise onerr object as
376 print error message and return status, else raise onerr object as
374 exception.'''
377 exception.'''
375 oldenv = {}
378 oldenv = {}
376 for k in environ:
379 for k in environ:
377 oldenv[k] = os.environ.get(k)
380 oldenv[k] = os.environ.get(k)
378 if cwd is not None:
381 if cwd is not None:
379 oldcwd = os.getcwd()
382 oldcwd = os.getcwd()
380 try:
383 try:
381 for k, v in environ.iteritems():
384 for k, v in environ.iteritems():
382 os.environ[k] = str(v)
385 os.environ[k] = str(v)
383 if cwd is not None and oldcwd != cwd:
386 if cwd is not None and oldcwd != cwd:
384 os.chdir(cwd)
387 os.chdir(cwd)
385 rc = os.system(cmd)
388 rc = os.system(cmd)
386 if rc and onerr:
389 if rc and onerr:
387 errmsg = '%s %s' % (os.path.basename(cmd.split(None, 1)[0]),
390 errmsg = '%s %s' % (os.path.basename(cmd.split(None, 1)[0]),
388 explain_exit(rc)[0])
391 explain_exit(rc)[0])
389 if errprefix:
392 if errprefix:
390 errmsg = '%s: %s' % (errprefix, errmsg)
393 errmsg = '%s: %s' % (errprefix, errmsg)
391 try:
394 try:
392 onerr.warn(errmsg + '\n')
395 onerr.warn(errmsg + '\n')
393 except AttributeError:
396 except AttributeError:
394 raise onerr(errmsg)
397 raise onerr(errmsg)
395 return rc
398 return rc
396 finally:
399 finally:
397 for k, v in oldenv.iteritems():
400 for k, v in oldenv.iteritems():
398 if v is None:
401 if v is None:
399 del os.environ[k]
402 del os.environ[k]
400 else:
403 else:
401 os.environ[k] = v
404 os.environ[k] = v
402 if cwd is not None and oldcwd != cwd:
405 if cwd is not None and oldcwd != cwd:
403 os.chdir(oldcwd)
406 os.chdir(oldcwd)
404
407
405 def rename(src, dst):
408 def rename(src, dst):
406 """forcibly rename a file"""
409 """forcibly rename a file"""
407 try:
410 try:
408 os.rename(src, dst)
411 os.rename(src, dst)
409 except:
412 except:
410 os.unlink(dst)
413 os.unlink(dst)
411 os.rename(src, dst)
414 os.rename(src, dst)
412
415
413 def unlink(f):
416 def unlink(f):
414 """unlink and remove the directory if it is empty"""
417 """unlink and remove the directory if it is empty"""
415 os.unlink(f)
418 os.unlink(f)
416 # try removing directories that might now be empty
419 # try removing directories that might now be empty
417 try:
420 try:
418 os.removedirs(os.path.dirname(f))
421 os.removedirs(os.path.dirname(f))
419 except OSError:
422 except OSError:
420 pass
423 pass
421
424
422 def copyfiles(src, dst, hardlink=None):
425 def copyfiles(src, dst, hardlink=None):
423 """Copy a directory tree using hardlinks if possible"""
426 """Copy a directory tree using hardlinks if possible"""
424
427
425 if hardlink is None:
428 if hardlink is None:
426 hardlink = (os.stat(src).st_dev ==
429 hardlink = (os.stat(src).st_dev ==
427 os.stat(os.path.dirname(dst)).st_dev)
430 os.stat(os.path.dirname(dst)).st_dev)
428
431
429 if os.path.isdir(src):
432 if os.path.isdir(src):
430 os.mkdir(dst)
433 os.mkdir(dst)
431 for name in os.listdir(src):
434 for name in os.listdir(src):
432 srcname = os.path.join(src, name)
435 srcname = os.path.join(src, name)
433 dstname = os.path.join(dst, name)
436 dstname = os.path.join(dst, name)
434 copyfiles(srcname, dstname, hardlink)
437 copyfiles(srcname, dstname, hardlink)
435 else:
438 else:
436 if hardlink:
439 if hardlink:
437 try:
440 try:
438 os_link(src, dst)
441 os_link(src, dst)
439 except (IOError, OSError):
442 except (IOError, OSError):
440 hardlink = False
443 hardlink = False
441 shutil.copy(src, dst)
444 shutil.copy(src, dst)
442 else:
445 else:
443 shutil.copy(src, dst)
446 shutil.copy(src, dst)
444
447
445 def audit_path(path):
448 def audit_path(path):
446 """Abort if path contains dangerous components"""
449 """Abort if path contains dangerous components"""
447 parts = os.path.normcase(path).split(os.sep)
450 parts = os.path.normcase(path).split(os.sep)
448 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
451 if (os.path.splitdrive(path)[0] or parts[0] in ('.hg', '')
449 or os.pardir in parts):
452 or os.pardir in parts):
450 raise Abort(_("path contains illegal component: %s\n") % path)
453 raise Abort(_("path contains illegal component: %s\n") % path)
451
454
452 def opener(base, audit=True):
455 def opener(base, audit=True):
453 """
456 """
454 return a function that opens files relative to base
457 return a function that opens files relative to base
455
458
456 this function is used to hide the details of COW semantics and
459 this function is used to hide the details of COW semantics and
457 remote file access from higher level code.
460 remote file access from higher level code.
458 """
461 """
459 p = base
462 p = base
460 audit_p = audit
463 audit_p = audit
461
464
462 def mktempcopy(name):
465 def mktempcopy(name):
463 d, fn = os.path.split(name)
466 d, fn = os.path.split(name)
464 fd, temp = tempfile.mkstemp(prefix=fn, dir=d)
467 fd, temp = tempfile.mkstemp(prefix=fn, dir=d)
465 fp = os.fdopen(fd, "wb")
468 fp = os.fdopen(fd, "wb")
466 try:
469 try:
467 fp.write(file(name, "rb").read())
470 fp.write(file(name, "rb").read())
468 except:
471 except:
469 try: os.unlink(temp)
472 try: os.unlink(temp)
470 except: pass
473 except: pass
471 raise
474 raise
472 fp.close()
475 fp.close()
473 st = os.lstat(name)
476 st = os.lstat(name)
474 os.chmod(temp, st.st_mode)
477 os.chmod(temp, st.st_mode)
475 return temp
478 return temp
476
479
477 class atomictempfile(file):
480 class atomictempfile(file):
478 """the file will only be copied when rename is called"""
481 """the file will only be copied when rename is called"""
479 def __init__(self, name, mode):
482 def __init__(self, name, mode):
480 self.__name = name
483 self.__name = name
481 self.temp = mktempcopy(name)
484 self.temp = mktempcopy(name)
482 file.__init__(self, self.temp, mode)
485 file.__init__(self, self.temp, mode)
483 def rename(self):
486 def rename(self):
484 if not self.closed:
487 if not self.closed:
485 file.close(self)
488 file.close(self)
486 rename(self.temp, self.__name)
489 rename(self.temp, self.__name)
487 def __del__(self):
490 def __del__(self):
488 if not self.closed:
491 if not self.closed:
489 try:
492 try:
490 os.unlink(self.temp)
493 os.unlink(self.temp)
491 except: pass
494 except: pass
492 file.close(self)
495 file.close(self)
493
496
494 class atomicfile(atomictempfile):
497 class atomicfile(atomictempfile):
495 """the file will only be copied on close"""
498 """the file will only be copied on close"""
496 def __init__(self, name, mode):
499 def __init__(self, name, mode):
497 atomictempfile.__init__(self, name, mode)
500 atomictempfile.__init__(self, name, mode)
498 def close(self):
501 def close(self):
499 self.rename()
502 self.rename()
500 def __del__(self):
503 def __del__(self):
501 self.rename()
504 self.rename()
502
505
503 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
506 def o(path, mode="r", text=False, atomic=False, atomictemp=False):
504 if audit_p:
507 if audit_p:
505 audit_path(path)
508 audit_path(path)
506 f = os.path.join(p, path)
509 f = os.path.join(p, path)
507
510
508 if not text:
511 if not text:
509 mode += "b" # for that other OS
512 mode += "b" # for that other OS
510
513
511 if mode[0] != "r":
514 if mode[0] != "r":
512 try:
515 try:
513 nlink = nlinks(f)
516 nlink = nlinks(f)
514 except OSError:
517 except OSError:
515 d = os.path.dirname(f)
518 d = os.path.dirname(f)
516 if not os.path.isdir(d):
519 if not os.path.isdir(d):
517 os.makedirs(d)
520 os.makedirs(d)
518 else:
521 else:
519 if atomic:
522 if atomic:
520 return atomicfile(f, mode)
523 return atomicfile(f, mode)
521 elif atomictemp:
524 elif atomictemp:
522 return atomictempfile(f, mode)
525 return atomictempfile(f, mode)
523 if nlink > 1:
526 if nlink > 1:
524 rename(mktempcopy(f), f)
527 rename(mktempcopy(f), f)
525 return file(f, mode)
528 return file(f, mode)
526
529
527 return o
530 return o
528
531
529 def _makelock_file(info, pathname):
532 def _makelock_file(info, pathname):
530 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
533 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
531 os.write(ld, info)
534 os.write(ld, info)
532 os.close(ld)
535 os.close(ld)
533
536
534 def _readlock_file(pathname):
537 def _readlock_file(pathname):
535 return file(pathname).read()
538 return file(pathname).read()
536
539
537 def nlinks(pathname):
540 def nlinks(pathname):
538 """Return number of hardlinks for the given file."""
541 """Return number of hardlinks for the given file."""
539 return os.stat(pathname).st_nlink
542 return os.stat(pathname).st_nlink
540
543
541 if hasattr(os, 'link'):
544 if hasattr(os, 'link'):
542 os_link = os.link
545 os_link = os.link
543 else:
546 else:
544 def os_link(src, dst):
547 def os_link(src, dst):
545 raise OSError(0, _("Hardlinks not supported"))
548 raise OSError(0, _("Hardlinks not supported"))
546
549
547 # Platform specific variants
550 # Platform specific variants
548 if os.name == 'nt':
551 if os.name == 'nt':
549 demandload(globals(), "msvcrt")
552 demandload(globals(), "msvcrt")
550 nulldev = 'NUL:'
553 nulldev = 'NUL:'
551
554
552 class winstdout:
555 class winstdout:
553 '''stdout on windows misbehaves if sent through a pipe'''
556 '''stdout on windows misbehaves if sent through a pipe'''
554
557
555 def __init__(self, fp):
558 def __init__(self, fp):
556 self.fp = fp
559 self.fp = fp
557
560
558 def __getattr__(self, key):
561 def __getattr__(self, key):
559 return getattr(self.fp, key)
562 return getattr(self.fp, key)
560
563
561 def close(self):
564 def close(self):
562 try:
565 try:
563 self.fp.close()
566 self.fp.close()
564 except: pass
567 except: pass
565
568
566 def write(self, s):
569 def write(self, s):
567 try:
570 try:
568 return self.fp.write(s)
571 return self.fp.write(s)
569 except IOError, inst:
572 except IOError, inst:
570 if inst.errno != 0: raise
573 if inst.errno != 0: raise
571 self.close()
574 self.close()
572 raise IOError(errno.EPIPE, 'Broken pipe')
575 raise IOError(errno.EPIPE, 'Broken pipe')
573
576
574 sys.stdout = winstdout(sys.stdout)
577 sys.stdout = winstdout(sys.stdout)
575
578
576 def system_rcpath():
579 def system_rcpath():
577 try:
580 try:
578 return system_rcpath_win32()
581 return system_rcpath_win32()
579 except:
582 except:
580 return [r'c:\mercurial\mercurial.ini']
583 return [r'c:\mercurial\mercurial.ini']
581
584
582 def os_rcpath():
585 def os_rcpath():
583 '''return default os-specific hgrc search path'''
586 '''return default os-specific hgrc search path'''
584 return system_rcpath() + [os.path.join(os.path.expanduser('~'),
587 return system_rcpath() + [os.path.join(os.path.expanduser('~'),
585 'mercurial.ini')]
588 'mercurial.ini')]
586
589
587 def parse_patch_output(output_line):
590 def parse_patch_output(output_line):
588 """parses the output produced by patch and returns the file name"""
591 """parses the output produced by patch and returns the file name"""
589 pf = output_line[14:]
592 pf = output_line[14:]
590 if pf[0] == '`':
593 if pf[0] == '`':
591 pf = pf[1:-1] # Remove the quotes
594 pf = pf[1:-1] # Remove the quotes
592 return pf
595 return pf
593
596
594 def testpid(pid):
597 def testpid(pid):
595 '''return False if pid dead, True if running or not known'''
598 '''return False if pid dead, True if running or not known'''
596 return True
599 return True
597
600
598 def is_exec(f, last):
601 def is_exec(f, last):
599 return last
602 return last
600
603
601 def set_exec(f, mode):
604 def set_exec(f, mode):
602 pass
605 pass
603
606
604 def set_binary(fd):
607 def set_binary(fd):
605 msvcrt.setmode(fd.fileno(), os.O_BINARY)
608 msvcrt.setmode(fd.fileno(), os.O_BINARY)
606
609
607 def pconvert(path):
610 def pconvert(path):
608 return path.replace("\\", "/")
611 return path.replace("\\", "/")
609
612
610 def localpath(path):
613 def localpath(path):
611 return path.replace('/', '\\')
614 return path.replace('/', '\\')
612
615
613 def normpath(path):
616 def normpath(path):
614 return pconvert(os.path.normpath(path))
617 return pconvert(os.path.normpath(path))
615
618
616 makelock = _makelock_file
619 makelock = _makelock_file
617 readlock = _readlock_file
620 readlock = _readlock_file
618
621
619 def explain_exit(code):
622 def explain_exit(code):
620 return _("exited with status %d") % code, code
623 return _("exited with status %d") % code, code
621
624
622 try:
625 try:
623 # override functions with win32 versions if possible
626 # override functions with win32 versions if possible
624 from util_win32 import *
627 from util_win32 import *
625 except ImportError:
628 except ImportError:
626 pass
629 pass
627
630
628 else:
631 else:
629 nulldev = '/dev/null'
632 nulldev = '/dev/null'
630
633
631 def rcfiles(path):
634 def rcfiles(path):
632 rcs = [os.path.join(path, 'hgrc')]
635 rcs = [os.path.join(path, 'hgrc')]
633 rcdir = os.path.join(path, 'hgrc.d')
636 rcdir = os.path.join(path, 'hgrc.d')
634 try:
637 try:
635 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
638 rcs.extend([os.path.join(rcdir, f) for f in os.listdir(rcdir)
636 if f.endswith(".rc")])
639 if f.endswith(".rc")])
637 except OSError, inst: pass
640 except OSError, inst: pass
638 return rcs
641 return rcs
639
642
640 def os_rcpath():
643 def os_rcpath():
641 '''return default os-specific hgrc search path'''
644 '''return default os-specific hgrc search path'''
642 path = []
645 path = []
643 if len(sys.argv) > 0:
646 if len(sys.argv) > 0:
644 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
647 path.extend(rcfiles(os.path.dirname(sys.argv[0]) +
645 '/../etc/mercurial'))
648 '/../etc/mercurial'))
646 path.extend(rcfiles('/etc/mercurial'))
649 path.extend(rcfiles('/etc/mercurial'))
647 path.append(os.path.expanduser('~/.hgrc'))
650 path.append(os.path.expanduser('~/.hgrc'))
648 path = [os.path.normpath(f) for f in path]
651 path = [os.path.normpath(f) for f in path]
649 return path
652 return path
650
653
651 def parse_patch_output(output_line):
654 def parse_patch_output(output_line):
652 """parses the output produced by patch and returns the file name"""
655 """parses the output produced by patch and returns the file name"""
653 pf = output_line[14:]
656 pf = output_line[14:]
654 if pf.startswith("'") and pf.endswith("'") and pf.find(" ") >= 0:
657 if pf.startswith("'") and pf.endswith("'") and pf.find(" ") >= 0:
655 pf = pf[1:-1] # Remove the quotes
658 pf = pf[1:-1] # Remove the quotes
656 return pf
659 return pf
657
660
658 def is_exec(f, last):
661 def is_exec(f, last):
659 """check whether a file is executable"""
662 """check whether a file is executable"""
660 return (os.stat(f).st_mode & 0100 != 0)
663 return (os.stat(f).st_mode & 0100 != 0)
661
664
662 def set_exec(f, mode):
665 def set_exec(f, mode):
663 s = os.stat(f).st_mode
666 s = os.stat(f).st_mode
664 if (s & 0100 != 0) == mode:
667 if (s & 0100 != 0) == mode:
665 return
668 return
666 if mode:
669 if mode:
667 # Turn on +x for every +r bit when making a file executable
670 # Turn on +x for every +r bit when making a file executable
668 # and obey umask.
671 # and obey umask.
669 umask = os.umask(0)
672 umask = os.umask(0)
670 os.umask(umask)
673 os.umask(umask)
671 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
674 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
672 else:
675 else:
673 os.chmod(f, s & 0666)
676 os.chmod(f, s & 0666)
674
677
675 def set_binary(fd):
678 def set_binary(fd):
676 pass
679 pass
677
680
678 def pconvert(path):
681 def pconvert(path):
679 return path
682 return path
680
683
681 def localpath(path):
684 def localpath(path):
682 return path
685 return path
683
686
684 normpath = os.path.normpath
687 normpath = os.path.normpath
685
688
686 def makelock(info, pathname):
689 def makelock(info, pathname):
687 try:
690 try:
688 os.symlink(info, pathname)
691 os.symlink(info, pathname)
689 except OSError, why:
692 except OSError, why:
690 if why.errno == errno.EEXIST:
693 if why.errno == errno.EEXIST:
691 raise
694 raise
692 else:
695 else:
693 _makelock_file(info, pathname)
696 _makelock_file(info, pathname)
694
697
695 def readlock(pathname):
698 def readlock(pathname):
696 try:
699 try:
697 return os.readlink(pathname)
700 return os.readlink(pathname)
698 except OSError, why:
701 except OSError, why:
699 if why.errno == errno.EINVAL:
702 if why.errno == errno.EINVAL:
700 return _readlock_file(pathname)
703 return _readlock_file(pathname)
701 else:
704 else:
702 raise
705 raise
703
706
704 def testpid(pid):
707 def testpid(pid):
705 '''return False if pid dead, True if running or not sure'''
708 '''return False if pid dead, True if running or not sure'''
706 try:
709 try:
707 os.kill(pid, 0)
710 os.kill(pid, 0)
708 return True
711 return True
709 except OSError, inst:
712 except OSError, inst:
710 return inst.errno != errno.ESRCH
713 return inst.errno != errno.ESRCH
711
714
712 def explain_exit(code):
715 def explain_exit(code):
713 """return a 2-tuple (desc, code) describing a process's status"""
716 """return a 2-tuple (desc, code) describing a process's status"""
714 if os.WIFEXITED(code):
717 if os.WIFEXITED(code):
715 val = os.WEXITSTATUS(code)
718 val = os.WEXITSTATUS(code)
716 return _("exited with status %d") % val, val
719 return _("exited with status %d") % val, val
717 elif os.WIFSIGNALED(code):
720 elif os.WIFSIGNALED(code):
718 val = os.WTERMSIG(code)
721 val = os.WTERMSIG(code)
719 return _("killed by signal %d") % val, val
722 return _("killed by signal %d") % val, val
720 elif os.WIFSTOPPED(code):
723 elif os.WIFSTOPPED(code):
721 val = os.WSTOPSIG(code)
724 val = os.WSTOPSIG(code)
722 return _("stopped by signal %d") % val, val
725 return _("stopped by signal %d") % val, val
723 raise ValueError(_("invalid exit code"))
726 raise ValueError(_("invalid exit code"))
724
727
725 class chunkbuffer(object):
728 class chunkbuffer(object):
726 """Allow arbitrary sized chunks of data to be efficiently read from an
729 """Allow arbitrary sized chunks of data to be efficiently read from an
727 iterator over chunks of arbitrary size."""
730 iterator over chunks of arbitrary size."""
728
731
729 def __init__(self, in_iter, targetsize = 2**16):
732 def __init__(self, in_iter, targetsize = 2**16):
730 """in_iter is the iterator that's iterating over the input chunks.
733 """in_iter is the iterator that's iterating over the input chunks.
731 targetsize is how big a buffer to try to maintain."""
734 targetsize is how big a buffer to try to maintain."""
732 self.in_iter = iter(in_iter)
735 self.in_iter = iter(in_iter)
733 self.buf = ''
736 self.buf = ''
734 self.targetsize = int(targetsize)
737 self.targetsize = int(targetsize)
735 if self.targetsize <= 0:
738 if self.targetsize <= 0:
736 raise ValueError(_("targetsize must be greater than 0, was %d") %
739 raise ValueError(_("targetsize must be greater than 0, was %d") %
737 targetsize)
740 targetsize)
738 self.iterempty = False
741 self.iterempty = False
739
742
740 def fillbuf(self):
743 def fillbuf(self):
741 """Ignore target size; read every chunk from iterator until empty."""
744 """Ignore target size; read every chunk from iterator until empty."""
742 if not self.iterempty:
745 if not self.iterempty:
743 collector = cStringIO.StringIO()
746 collector = cStringIO.StringIO()
744 collector.write(self.buf)
747 collector.write(self.buf)
745 for ch in self.in_iter:
748 for ch in self.in_iter:
746 collector.write(ch)
749 collector.write(ch)
747 self.buf = collector.getvalue()
750 self.buf = collector.getvalue()
748 self.iterempty = True
751 self.iterempty = True
749
752
750 def read(self, l):
753 def read(self, l):
751 """Read L bytes of data from the iterator of chunks of data.
754 """Read L bytes of data from the iterator of chunks of data.
752 Returns less than L bytes if the iterator runs dry."""
755 Returns less than L bytes if the iterator runs dry."""
753 if l > len(self.buf) and not self.iterempty:
756 if l > len(self.buf) and not self.iterempty:
754 # Clamp to a multiple of self.targetsize
757 # Clamp to a multiple of self.targetsize
755 targetsize = self.targetsize * ((l // self.targetsize) + 1)
758 targetsize = self.targetsize * ((l // self.targetsize) + 1)
756 collector = cStringIO.StringIO()
759 collector = cStringIO.StringIO()
757 collector.write(self.buf)
760 collector.write(self.buf)
758 collected = len(self.buf)
761 collected = len(self.buf)
759 for chunk in self.in_iter:
762 for chunk in self.in_iter:
760 collector.write(chunk)
763 collector.write(chunk)
761 collected += len(chunk)
764 collected += len(chunk)
762 if collected >= targetsize:
765 if collected >= targetsize:
763 break
766 break
764 if collected < targetsize:
767 if collected < targetsize:
765 self.iterempty = True
768 self.iterempty = True
766 self.buf = collector.getvalue()
769 self.buf = collector.getvalue()
767 s, self.buf = self.buf[:l], buffer(self.buf, l)
770 s, self.buf = self.buf[:l], buffer(self.buf, l)
768 return s
771 return s
769
772
770 def filechunkiter(f, size = 65536):
773 def filechunkiter(f, size = 65536):
771 """Create a generator that produces all the data in the file size
774 """Create a generator that produces all the data in the file size
772 (default 65536) bytes at a time. Chunks may be less than size
775 (default 65536) bytes at a time. Chunks may be less than size
773 bytes if the chunk is the last chunk in the file, or the file is a
776 bytes if the chunk is the last chunk in the file, or the file is a
774 socket or some other type of file that sometimes reads less data
777 socket or some other type of file that sometimes reads less data
775 than is requested."""
778 than is requested."""
776 s = f.read(size)
779 s = f.read(size)
777 while len(s) > 0:
780 while len(s) > 0:
778 yield s
781 yield s
779 s = f.read(size)
782 s = f.read(size)
780
783
781 def makedate():
784 def makedate():
782 lt = time.localtime()
785 lt = time.localtime()
783 if lt[8] == 1 and time.daylight:
786 if lt[8] == 1 and time.daylight:
784 tz = time.altzone
787 tz = time.altzone
785 else:
788 else:
786 tz = time.timezone
789 tz = time.timezone
787 return time.mktime(lt), tz
790 return time.mktime(lt), tz
788
791
789 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
792 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True):
790 """represent a (unixtime, offset) tuple as a localized time.
793 """represent a (unixtime, offset) tuple as a localized time.
791 unixtime is seconds since the epoch, and offset is the time zone's
794 unixtime is seconds since the epoch, and offset is the time zone's
792 number of seconds away from UTC. if timezone is false, do not
795 number of seconds away from UTC. if timezone is false, do not
793 append time zone to string."""
796 append time zone to string."""
794 t, tz = date or makedate()
797 t, tz = date or makedate()
795 s = time.strftime(format, time.gmtime(float(t) - tz))
798 s = time.strftime(format, time.gmtime(float(t) - tz))
796 if timezone:
799 if timezone:
797 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
800 s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
798 return s
801 return s
799
802
800 def shortuser(user):
803 def shortuser(user):
801 """Return a short representation of a user name or email address."""
804 """Return a short representation of a user name or email address."""
802 f = user.find('@')
805 f = user.find('@')
803 if f >= 0:
806 if f >= 0:
804 user = user[:f]
807 user = user[:f]
805 f = user.find('<')
808 f = user.find('<')
806 if f >= 0:
809 if f >= 0:
807 user = user[f+1:]
810 user = user[f+1:]
808 return user
811 return user
809
812
810 def walkrepos(path):
813 def walkrepos(path):
811 '''yield every hg repository under path, recursively.'''
814 '''yield every hg repository under path, recursively.'''
812 def errhandler(err):
815 def errhandler(err):
813 if err.filename == path:
816 if err.filename == path:
814 raise err
817 raise err
815
818
816 for root, dirs, files in os.walk(path, onerror=errhandler):
819 for root, dirs, files in os.walk(path, onerror=errhandler):
817 for d in dirs:
820 for d in dirs:
818 if d == '.hg':
821 if d == '.hg':
819 yield root
822 yield root
820 dirs[:] = []
823 dirs[:] = []
821 break
824 break
822
825
823 _rcpath = None
826 _rcpath = None
824
827
825 def rcpath():
828 def rcpath():
826 '''return hgrc search path. if env var HGRCPATH is set, use it.
829 '''return hgrc search path. if env var HGRCPATH is set, use it.
827 for each item in path, if directory, use files ending in .rc,
830 for each item in path, if directory, use files ending in .rc,
828 else use item.
831 else use item.
829 make HGRCPATH empty to only look in .hg/hgrc of current repo.
832 make HGRCPATH empty to only look in .hg/hgrc of current repo.
830 if no HGRCPATH, use default os-specific path.'''
833 if no HGRCPATH, use default os-specific path.'''
831 global _rcpath
834 global _rcpath
832 if _rcpath is None:
835 if _rcpath is None:
833 if 'HGRCPATH' in os.environ:
836 if 'HGRCPATH' in os.environ:
834 _rcpath = []
837 _rcpath = []
835 for p in os.environ['HGRCPATH'].split(os.pathsep):
838 for p in os.environ['HGRCPATH'].split(os.pathsep):
836 if not p: continue
839 if not p: continue
837 if os.path.isdir(p):
840 if os.path.isdir(p):
838 for f in os.listdir(p):
841 for f in os.listdir(p):
839 if f.endswith('.rc'):
842 if f.endswith('.rc'):
840 _rcpath.append(os.path.join(p, f))
843 _rcpath.append(os.path.join(p, f))
841 else:
844 else:
842 _rcpath.append(p)
845 _rcpath.append(p)
843 else:
846 else:
844 _rcpath = os_rcpath()
847 _rcpath = os_rcpath()
845 return _rcpath
848 return _rcpath
General Comments 0
You need to be logged in to leave comments. Login now