Show More
@@ -1,1243 +1,1250 | |||
|
1 | 1 | # cmdutil.py - help for command processing in mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from node import hex, nullid, nullrev, short |
|
9 | 9 | from i18n import _ |
|
10 | 10 | import os, sys, errno, re, glob, tempfile |
|
11 | 11 | import util, templater, patch, error, encoding, templatekw |
|
12 | 12 | import match as _match |
|
13 | 13 | import similar, revset |
|
14 | 14 | |
|
15 | 15 | revrangesep = ':' |
|
16 | 16 | |
|
17 | 17 | def parsealiases(cmd): |
|
18 | 18 | return cmd.lstrip("^").split("|") |
|
19 | 19 | |
|
20 | 20 | def findpossible(cmd, table, strict=False): |
|
21 | 21 | """ |
|
22 | 22 | Return cmd -> (aliases, command table entry) |
|
23 | 23 | for each matching command. |
|
24 | 24 | Return debug commands (or their aliases) only if no normal command matches. |
|
25 | 25 | """ |
|
26 | 26 | choice = {} |
|
27 | 27 | debugchoice = {} |
|
28 | 28 | for e in table.keys(): |
|
29 | 29 | aliases = parsealiases(e) |
|
30 | 30 | found = None |
|
31 | 31 | if cmd in aliases: |
|
32 | 32 | found = cmd |
|
33 | 33 | elif not strict: |
|
34 | 34 | for a in aliases: |
|
35 | 35 | if a.startswith(cmd): |
|
36 | 36 | found = a |
|
37 | 37 | break |
|
38 | 38 | if found is not None: |
|
39 | 39 | if aliases[0].startswith("debug") or found.startswith("debug"): |
|
40 | 40 | debugchoice[found] = (aliases, table[e]) |
|
41 | 41 | else: |
|
42 | 42 | choice[found] = (aliases, table[e]) |
|
43 | 43 | |
|
44 | 44 | if not choice and debugchoice: |
|
45 | 45 | choice = debugchoice |
|
46 | 46 | |
|
47 | 47 | return choice |
|
48 | 48 | |
|
49 | 49 | def findcmd(cmd, table, strict=True): |
|
50 | 50 | """Return (aliases, command table entry) for command string.""" |
|
51 | 51 | choice = findpossible(cmd, table, strict) |
|
52 | 52 | |
|
53 | 53 | if cmd in choice: |
|
54 | 54 | return choice[cmd] |
|
55 | 55 | |
|
56 | 56 | if len(choice) > 1: |
|
57 | 57 | clist = choice.keys() |
|
58 | 58 | clist.sort() |
|
59 | 59 | raise error.AmbiguousCommand(cmd, clist) |
|
60 | 60 | |
|
61 | 61 | if choice: |
|
62 | 62 | return choice.values()[0] |
|
63 | 63 | |
|
64 | 64 | raise error.UnknownCommand(cmd) |
|
65 | 65 | |
|
66 | 66 | def findrepo(p): |
|
67 | 67 | while not os.path.isdir(os.path.join(p, ".hg")): |
|
68 | 68 | oldp, p = p, os.path.dirname(p) |
|
69 | 69 | if p == oldp: |
|
70 | 70 | return None |
|
71 | 71 | |
|
72 | 72 | return p |
|
73 | 73 | |
|
74 | 74 | def bail_if_changed(repo): |
|
75 | 75 | if repo.dirstate.parents()[1] != nullid: |
|
76 | 76 | raise util.Abort(_('outstanding uncommitted merge')) |
|
77 | 77 | modified, added, removed, deleted = repo.status()[:4] |
|
78 | 78 | if modified or added or removed or deleted: |
|
79 | 79 | raise util.Abort(_("outstanding uncommitted changes")) |
|
80 | 80 | |
|
81 | 81 | def logmessage(opts): |
|
82 | 82 | """ get the log message according to -m and -l option """ |
|
83 | 83 | message = opts.get('message') |
|
84 | 84 | logfile = opts.get('logfile') |
|
85 | 85 | |
|
86 | 86 | if message and logfile: |
|
87 | 87 | raise util.Abort(_('options --message and --logfile are mutually ' |
|
88 | 88 | 'exclusive')) |
|
89 | 89 | if not message and logfile: |
|
90 | 90 | try: |
|
91 | 91 | if logfile == '-': |
|
92 | 92 | message = sys.stdin.read() |
|
93 | 93 | else: |
|
94 | 94 | message = open(logfile).read() |
|
95 | 95 | except IOError, inst: |
|
96 | 96 | raise util.Abort(_("can't read commit message '%s': %s") % |
|
97 | 97 | (logfile, inst.strerror)) |
|
98 | 98 | return message |
|
99 | 99 | |
|
100 | 100 | def loglimit(opts): |
|
101 | 101 | """get the log limit according to option -l/--limit""" |
|
102 | 102 | limit = opts.get('limit') |
|
103 | 103 | if limit: |
|
104 | 104 | try: |
|
105 | 105 | limit = int(limit) |
|
106 | 106 | except ValueError: |
|
107 | 107 | raise util.Abort(_('limit must be a positive integer')) |
|
108 | 108 | if limit <= 0: |
|
109 | 109 | raise util.Abort(_('limit must be positive')) |
|
110 | 110 | else: |
|
111 | 111 | limit = None |
|
112 | 112 | return limit |
|
113 | 113 | |
|
114 | 114 | def revpair(repo, revs): |
|
115 | 115 | '''return pair of nodes, given list of revisions. second item can |
|
116 | 116 | be None, meaning use working dir.''' |
|
117 | 117 | |
|
118 | 118 | def revfix(repo, val, defval): |
|
119 | 119 | if not val and val != 0 and defval is not None: |
|
120 | 120 | val = defval |
|
121 | 121 | return repo.lookup(val) |
|
122 | 122 | |
|
123 | 123 | if not revs: |
|
124 | 124 | return repo.dirstate.parents()[0], None |
|
125 | 125 | end = None |
|
126 | 126 | if len(revs) == 1: |
|
127 | 127 | if revrangesep in revs[0]: |
|
128 | 128 | start, end = revs[0].split(revrangesep, 1) |
|
129 | 129 | start = revfix(repo, start, 0) |
|
130 | 130 | end = revfix(repo, end, len(repo) - 1) |
|
131 | 131 | else: |
|
132 | 132 | start = revfix(repo, revs[0], None) |
|
133 | 133 | elif len(revs) == 2: |
|
134 | 134 | if revrangesep in revs[0] or revrangesep in revs[1]: |
|
135 | 135 | raise util.Abort(_('too many revisions specified')) |
|
136 | 136 | start = revfix(repo, revs[0], None) |
|
137 | 137 | end = revfix(repo, revs[1], None) |
|
138 | 138 | else: |
|
139 | 139 | raise util.Abort(_('too many revisions specified')) |
|
140 | 140 | return start, end |
|
141 | 141 | |
|
142 | 142 | def revrange(repo, revs): |
|
143 | 143 | """Yield revision as strings from a list of revision specifications.""" |
|
144 | 144 | |
|
145 | 145 | def revfix(repo, val, defval): |
|
146 | 146 | if not val and val != 0 and defval is not None: |
|
147 | 147 | return defval |
|
148 | 148 | return repo.changelog.rev(repo.lookup(val)) |
|
149 | 149 | |
|
150 | 150 | seen, l = set(), [] |
|
151 | 151 | for spec in revs: |
|
152 | 152 | # attempt to parse old-style ranges first to deal with |
|
153 | 153 | # things like old-tag which contain query metacharacters |
|
154 | 154 | try: |
|
155 | 155 | if revrangesep in spec: |
|
156 | 156 | start, end = spec.split(revrangesep, 1) |
|
157 | 157 | start = revfix(repo, start, 0) |
|
158 | 158 | end = revfix(repo, end, len(repo) - 1) |
|
159 | 159 | step = start > end and -1 or 1 |
|
160 | 160 | for rev in xrange(start, end + step, step): |
|
161 | 161 | if rev in seen: |
|
162 | 162 | continue |
|
163 | 163 | seen.add(rev) |
|
164 | 164 | l.append(rev) |
|
165 | 165 | continue |
|
166 | 166 | elif spec and spec in repo: # single unquoted rev |
|
167 | 167 | rev = revfix(repo, spec, None) |
|
168 | 168 | if rev in seen: |
|
169 | 169 | continue |
|
170 | 170 | seen.add(rev) |
|
171 | 171 | l.append(rev) |
|
172 | 172 | continue |
|
173 | 173 | except error.RepoLookupError: |
|
174 | 174 | pass |
|
175 | 175 | |
|
176 | 176 | # fall through to new-style queries if old-style fails |
|
177 | 177 | m = revset.match(spec) |
|
178 | 178 | for r in m(repo, range(len(repo))): |
|
179 | 179 | if r not in seen: |
|
180 | 180 | l.append(r) |
|
181 | 181 | seen.update(l) |
|
182 | 182 | |
|
183 | 183 | return l |
|
184 | 184 | |
|
185 | 185 | def make_filename(repo, pat, node, |
|
186 | 186 | total=None, seqno=None, revwidth=None, pathname=None): |
|
187 | 187 | node_expander = { |
|
188 | 188 | 'H': lambda: hex(node), |
|
189 | 189 | 'R': lambda: str(repo.changelog.rev(node)), |
|
190 | 190 | 'h': lambda: short(node), |
|
191 | 191 | } |
|
192 | 192 | expander = { |
|
193 | 193 | '%': lambda: '%', |
|
194 | 194 | 'b': lambda: os.path.basename(repo.root), |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | try: |
|
198 | 198 | if node: |
|
199 | 199 | expander.update(node_expander) |
|
200 | 200 | if node: |
|
201 | 201 | expander['r'] = (lambda: |
|
202 | 202 | str(repo.changelog.rev(node)).zfill(revwidth or 0)) |
|
203 | 203 | if total is not None: |
|
204 | 204 | expander['N'] = lambda: str(total) |
|
205 | 205 | if seqno is not None: |
|
206 | 206 | expander['n'] = lambda: str(seqno) |
|
207 | 207 | if total is not None and seqno is not None: |
|
208 | 208 | expander['n'] = lambda: str(seqno).zfill(len(str(total))) |
|
209 | 209 | if pathname is not None: |
|
210 | 210 | expander['s'] = lambda: os.path.basename(pathname) |
|
211 | 211 | expander['d'] = lambda: os.path.dirname(pathname) or '.' |
|
212 | 212 | expander['p'] = lambda: pathname |
|
213 | 213 | |
|
214 | 214 | newname = [] |
|
215 | 215 | patlen = len(pat) |
|
216 | 216 | i = 0 |
|
217 | 217 | while i < patlen: |
|
218 | 218 | c = pat[i] |
|
219 | 219 | if c == '%': |
|
220 | 220 | i += 1 |
|
221 | 221 | c = pat[i] |
|
222 | 222 | c = expander[c]() |
|
223 | 223 | newname.append(c) |
|
224 | 224 | i += 1 |
|
225 | 225 | return ''.join(newname) |
|
226 | 226 | except KeyError, inst: |
|
227 | 227 | raise util.Abort(_("invalid format spec '%%%s' in output filename") % |
|
228 | 228 | inst.args[0]) |
|
229 | 229 | |
|
230 | 230 | def make_file(repo, pat, node=None, |
|
231 | 231 | total=None, seqno=None, revwidth=None, mode='wb', pathname=None): |
|
232 | 232 | |
|
233 | 233 | writable = 'w' in mode or 'a' in mode |
|
234 | 234 | |
|
235 | 235 | if not pat or pat == '-': |
|
236 | 236 | return writable and sys.stdout or sys.stdin |
|
237 | 237 | if hasattr(pat, 'write') and writable: |
|
238 | 238 | return pat |
|
239 | 239 | if hasattr(pat, 'read') and 'r' in mode: |
|
240 | 240 | return pat |
|
241 | 241 | return open(make_filename(repo, pat, node, total, seqno, revwidth, |
|
242 | 242 | pathname), |
|
243 | 243 | mode) |
|
244 | 244 | |
|
245 | 245 | def expandpats(pats): |
|
246 | 246 | if not util.expandglobs: |
|
247 | 247 | return list(pats) |
|
248 | 248 | ret = [] |
|
249 | 249 | for p in pats: |
|
250 | 250 | kind, name = _match._patsplit(p, None) |
|
251 | 251 | if kind is None: |
|
252 | 252 | try: |
|
253 | 253 | globbed = glob.glob(name) |
|
254 | 254 | except re.error: |
|
255 | 255 | globbed = [name] |
|
256 | 256 | if globbed: |
|
257 | 257 | ret.extend(globbed) |
|
258 | 258 | continue |
|
259 | 259 | ret.append(p) |
|
260 | 260 | return ret |
|
261 | 261 | |
|
262 | 262 | def match(repo, pats=[], opts={}, globbed=False, default='relpath'): |
|
263 | 263 | if not globbed and default == 'relpath': |
|
264 | 264 | pats = expandpats(pats or []) |
|
265 | 265 | m = _match.match(repo.root, repo.getcwd(), pats, |
|
266 | 266 | opts.get('include'), opts.get('exclude'), default) |
|
267 | 267 | def badfn(f, msg): |
|
268 | 268 | repo.ui.warn("%s: %s\n" % (m.rel(f), msg)) |
|
269 | 269 | m.bad = badfn |
|
270 | 270 | return m |
|
271 | 271 | |
|
272 | 272 | def matchall(repo): |
|
273 | 273 | return _match.always(repo.root, repo.getcwd()) |
|
274 | 274 | |
|
275 | 275 | def matchfiles(repo, files): |
|
276 | 276 | return _match.exact(repo.root, repo.getcwd(), files) |
|
277 | 277 | |
|
278 | 278 | def addremove(repo, pats=[], opts={}, dry_run=None, similarity=None): |
|
279 | 279 | if dry_run is None: |
|
280 | 280 | dry_run = opts.get('dry_run') |
|
281 | 281 | if similarity is None: |
|
282 | 282 | similarity = float(opts.get('similarity') or 0) |
|
283 | 283 | # we'd use status here, except handling of symlinks and ignore is tricky |
|
284 | 284 | added, unknown, deleted, removed = [], [], [], [] |
|
285 | 285 | audit_path = util.path_auditor(repo.root) |
|
286 | 286 | m = match(repo, pats, opts) |
|
287 | 287 | for abs in repo.walk(m): |
|
288 | 288 | target = repo.wjoin(abs) |
|
289 | 289 | good = True |
|
290 | 290 | try: |
|
291 | 291 | audit_path(abs) |
|
292 | 292 | except: |
|
293 | 293 | good = False |
|
294 | 294 | rel = m.rel(abs) |
|
295 | 295 | exact = m.exact(abs) |
|
296 | 296 | if good and abs not in repo.dirstate: |
|
297 | 297 | unknown.append(abs) |
|
298 | 298 | if repo.ui.verbose or not exact: |
|
299 | 299 | repo.ui.status(_('adding %s\n') % ((pats and rel) or abs)) |
|
300 | 300 | elif repo.dirstate[abs] != 'r' and (not good or not util.lexists(target) |
|
301 | 301 | or (os.path.isdir(target) and not os.path.islink(target))): |
|
302 | 302 | deleted.append(abs) |
|
303 | 303 | if repo.ui.verbose or not exact: |
|
304 | 304 | repo.ui.status(_('removing %s\n') % ((pats and rel) or abs)) |
|
305 | 305 | # for finding renames |
|
306 | 306 | elif repo.dirstate[abs] == 'r': |
|
307 | 307 | removed.append(abs) |
|
308 | 308 | elif repo.dirstate[abs] == 'a': |
|
309 | 309 | added.append(abs) |
|
310 | 310 | copies = {} |
|
311 | 311 | if similarity > 0: |
|
312 | 312 | for old, new, score in similar.findrenames(repo, |
|
313 | 313 | added + unknown, removed + deleted, similarity): |
|
314 | 314 | if repo.ui.verbose or not m.exact(old) or not m.exact(new): |
|
315 | 315 | repo.ui.status(_('recording removal of %s as rename to %s ' |
|
316 | 316 | '(%d%% similar)\n') % |
|
317 | 317 | (m.rel(old), m.rel(new), score * 100)) |
|
318 | 318 | copies[new] = old |
|
319 | 319 | |
|
320 | 320 | if not dry_run: |
|
321 | 321 | wctx = repo[None] |
|
322 | 322 | wlock = repo.wlock() |
|
323 | 323 | try: |
|
324 | 324 | wctx.remove(deleted) |
|
325 | 325 | wctx.add(unknown) |
|
326 | 326 | for new, old in copies.iteritems(): |
|
327 | 327 | wctx.copy(old, new) |
|
328 | 328 | finally: |
|
329 | 329 | wlock.release() |
|
330 | 330 | |
|
331 | 331 | def copy(ui, repo, pats, opts, rename=False): |
|
332 | 332 | # called with the repo lock held |
|
333 | 333 | # |
|
334 | 334 | # hgsep => pathname that uses "/" to separate directories |
|
335 | 335 | # ossep => pathname that uses os.sep to separate directories |
|
336 | 336 | cwd = repo.getcwd() |
|
337 | 337 | targets = {} |
|
338 | 338 | after = opts.get("after") |
|
339 | 339 | dryrun = opts.get("dry_run") |
|
340 | 340 | wctx = repo[None] |
|
341 | 341 | |
|
342 | 342 | def walkpat(pat): |
|
343 | 343 | srcs = [] |
|
344 | 344 | badstates = after and '?' or '?r' |
|
345 | 345 | m = match(repo, [pat], opts, globbed=True) |
|
346 | 346 | for abs in repo.walk(m): |
|
347 | 347 | state = repo.dirstate[abs] |
|
348 | 348 | rel = m.rel(abs) |
|
349 | 349 | exact = m.exact(abs) |
|
350 | 350 | if state in badstates: |
|
351 | 351 | if exact and state == '?': |
|
352 | 352 | ui.warn(_('%s: not copying - file is not managed\n') % rel) |
|
353 | 353 | if exact and state == 'r': |
|
354 | 354 | ui.warn(_('%s: not copying - file has been marked for' |
|
355 | 355 | ' remove\n') % rel) |
|
356 | 356 | continue |
|
357 | 357 | # abs: hgsep |
|
358 | 358 | # rel: ossep |
|
359 | 359 | srcs.append((abs, rel, exact)) |
|
360 | 360 | return srcs |
|
361 | 361 | |
|
362 | 362 | # abssrc: hgsep |
|
363 | 363 | # relsrc: ossep |
|
364 | 364 | # otarget: ossep |
|
365 | 365 | def copyfile(abssrc, relsrc, otarget, exact): |
|
366 | 366 | abstarget = util.canonpath(repo.root, cwd, otarget) |
|
367 | 367 | reltarget = repo.pathto(abstarget, cwd) |
|
368 | 368 | target = repo.wjoin(abstarget) |
|
369 | 369 | src = repo.wjoin(abssrc) |
|
370 | 370 | state = repo.dirstate[abstarget] |
|
371 | 371 | |
|
372 | 372 | # check for collisions |
|
373 | 373 | prevsrc = targets.get(abstarget) |
|
374 | 374 | if prevsrc is not None: |
|
375 | 375 | ui.warn(_('%s: not overwriting - %s collides with %s\n') % |
|
376 | 376 | (reltarget, repo.pathto(abssrc, cwd), |
|
377 | 377 | repo.pathto(prevsrc, cwd))) |
|
378 | 378 | return |
|
379 | 379 | |
|
380 | 380 | # check for overwrites |
|
381 | 381 | exists = os.path.exists(target) |
|
382 | 382 | if not after and exists or after and state in 'mn': |
|
383 | 383 | if not opts['force']: |
|
384 | 384 | ui.warn(_('%s: not overwriting - file exists\n') % |
|
385 | 385 | reltarget) |
|
386 | 386 | return |
|
387 | 387 | |
|
388 | 388 | if after: |
|
389 | 389 | if not exists: |
|
390 | 390 | if rename: |
|
391 | 391 | ui.warn(_('%s: not recording move - %s does not exist\n') % |
|
392 | 392 | (relsrc, reltarget)) |
|
393 | 393 | else: |
|
394 | 394 | ui.warn(_('%s: not recording copy - %s does not exist\n') % |
|
395 | 395 | (relsrc, reltarget)) |
|
396 | 396 | return |
|
397 | 397 | elif not dryrun: |
|
398 | 398 | try: |
|
399 | 399 | if exists: |
|
400 | 400 | os.unlink(target) |
|
401 | 401 | targetdir = os.path.dirname(target) or '.' |
|
402 | 402 | if not os.path.isdir(targetdir): |
|
403 | 403 | os.makedirs(targetdir) |
|
404 | 404 | util.copyfile(src, target) |
|
405 | 405 | except IOError, inst: |
|
406 | 406 | if inst.errno == errno.ENOENT: |
|
407 | 407 | ui.warn(_('%s: deleted in working copy\n') % relsrc) |
|
408 | 408 | else: |
|
409 | 409 | ui.warn(_('%s: cannot copy - %s\n') % |
|
410 | 410 | (relsrc, inst.strerror)) |
|
411 | 411 | return True # report a failure |
|
412 | 412 | |
|
413 | 413 | if ui.verbose or not exact: |
|
414 | 414 | if rename: |
|
415 | 415 | ui.status(_('moving %s to %s\n') % (relsrc, reltarget)) |
|
416 | 416 | else: |
|
417 | 417 | ui.status(_('copying %s to %s\n') % (relsrc, reltarget)) |
|
418 | 418 | |
|
419 | 419 | targets[abstarget] = abssrc |
|
420 | 420 | |
|
421 | 421 | # fix up dirstate |
|
422 | 422 | origsrc = repo.dirstate.copied(abssrc) or abssrc |
|
423 | 423 | if abstarget == origsrc: # copying back a copy? |
|
424 | 424 | if state not in 'mn' and not dryrun: |
|
425 | 425 | repo.dirstate.normallookup(abstarget) |
|
426 | 426 | else: |
|
427 | 427 | if repo.dirstate[origsrc] == 'a' and origsrc == abssrc: |
|
428 | 428 | if not ui.quiet: |
|
429 | 429 | ui.warn(_("%s has not been committed yet, so no copy " |
|
430 | 430 | "data will be stored for %s.\n") |
|
431 | 431 | % (repo.pathto(origsrc, cwd), reltarget)) |
|
432 | 432 | if repo.dirstate[abstarget] in '?r' and not dryrun: |
|
433 | 433 | wctx.add([abstarget]) |
|
434 | 434 | elif not dryrun: |
|
435 | 435 | wctx.copy(origsrc, abstarget) |
|
436 | 436 | |
|
437 | 437 | if rename and not dryrun: |
|
438 | 438 | wctx.remove([abssrc], not after) |
|
439 | 439 | |
|
440 | 440 | # pat: ossep |
|
441 | 441 | # dest ossep |
|
442 | 442 | # srcs: list of (hgsep, hgsep, ossep, bool) |
|
443 | 443 | # return: function that takes hgsep and returns ossep |
|
444 | 444 | def targetpathfn(pat, dest, srcs): |
|
445 | 445 | if os.path.isdir(pat): |
|
446 | 446 | abspfx = util.canonpath(repo.root, cwd, pat) |
|
447 | 447 | abspfx = util.localpath(abspfx) |
|
448 | 448 | if destdirexists: |
|
449 | 449 | striplen = len(os.path.split(abspfx)[0]) |
|
450 | 450 | else: |
|
451 | 451 | striplen = len(abspfx) |
|
452 | 452 | if striplen: |
|
453 | 453 | striplen += len(os.sep) |
|
454 | 454 | res = lambda p: os.path.join(dest, util.localpath(p)[striplen:]) |
|
455 | 455 | elif destdirexists: |
|
456 | 456 | res = lambda p: os.path.join(dest, |
|
457 | 457 | os.path.basename(util.localpath(p))) |
|
458 | 458 | else: |
|
459 | 459 | res = lambda p: dest |
|
460 | 460 | return res |
|
461 | 461 | |
|
462 | 462 | # pat: ossep |
|
463 | 463 | # dest ossep |
|
464 | 464 | # srcs: list of (hgsep, hgsep, ossep, bool) |
|
465 | 465 | # return: function that takes hgsep and returns ossep |
|
466 | 466 | def targetpathafterfn(pat, dest, srcs): |
|
467 | 467 | if _match.patkind(pat): |
|
468 | 468 | # a mercurial pattern |
|
469 | 469 | res = lambda p: os.path.join(dest, |
|
470 | 470 | os.path.basename(util.localpath(p))) |
|
471 | 471 | else: |
|
472 | 472 | abspfx = util.canonpath(repo.root, cwd, pat) |
|
473 | 473 | if len(abspfx) < len(srcs[0][0]): |
|
474 | 474 | # A directory. Either the target path contains the last |
|
475 | 475 | # component of the source path or it does not. |
|
476 | 476 | def evalpath(striplen): |
|
477 | 477 | score = 0 |
|
478 | 478 | for s in srcs: |
|
479 | 479 | t = os.path.join(dest, util.localpath(s[0])[striplen:]) |
|
480 | 480 | if os.path.exists(t): |
|
481 | 481 | score += 1 |
|
482 | 482 | return score |
|
483 | 483 | |
|
484 | 484 | abspfx = util.localpath(abspfx) |
|
485 | 485 | striplen = len(abspfx) |
|
486 | 486 | if striplen: |
|
487 | 487 | striplen += len(os.sep) |
|
488 | 488 | if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])): |
|
489 | 489 | score = evalpath(striplen) |
|
490 | 490 | striplen1 = len(os.path.split(abspfx)[0]) |
|
491 | 491 | if striplen1: |
|
492 | 492 | striplen1 += len(os.sep) |
|
493 | 493 | if evalpath(striplen1) > score: |
|
494 | 494 | striplen = striplen1 |
|
495 | 495 | res = lambda p: os.path.join(dest, |
|
496 | 496 | util.localpath(p)[striplen:]) |
|
497 | 497 | else: |
|
498 | 498 | # a file |
|
499 | 499 | if destdirexists: |
|
500 | 500 | res = lambda p: os.path.join(dest, |
|
501 | 501 | os.path.basename(util.localpath(p))) |
|
502 | 502 | else: |
|
503 | 503 | res = lambda p: dest |
|
504 | 504 | return res |
|
505 | 505 | |
|
506 | 506 | |
|
507 | 507 | pats = expandpats(pats) |
|
508 | 508 | if not pats: |
|
509 | 509 | raise util.Abort(_('no source or destination specified')) |
|
510 | 510 | if len(pats) == 1: |
|
511 | 511 | raise util.Abort(_('no destination specified')) |
|
512 | 512 | dest = pats.pop() |
|
513 | 513 | destdirexists = os.path.isdir(dest) and not os.path.islink(dest) |
|
514 | 514 | if not destdirexists: |
|
515 | 515 | if len(pats) > 1 or _match.patkind(pats[0]): |
|
516 | 516 | raise util.Abort(_('with multiple sources, destination must be an ' |
|
517 | 517 | 'existing directory')) |
|
518 | 518 | if util.endswithsep(dest): |
|
519 | 519 | raise util.Abort(_('destination %s is not a directory') % dest) |
|
520 | 520 | |
|
521 | 521 | tfn = targetpathfn |
|
522 | 522 | if after: |
|
523 | 523 | tfn = targetpathafterfn |
|
524 | 524 | copylist = [] |
|
525 | 525 | for pat in pats: |
|
526 | 526 | srcs = walkpat(pat) |
|
527 | 527 | if not srcs: |
|
528 | 528 | continue |
|
529 | 529 | copylist.append((tfn(pat, dest, srcs), srcs)) |
|
530 | 530 | if not copylist: |
|
531 | 531 | raise util.Abort(_('no files to copy')) |
|
532 | 532 | |
|
533 | 533 | errors = 0 |
|
534 | 534 | for targetpath, srcs in copylist: |
|
535 | 535 | for abssrc, relsrc, exact in srcs: |
|
536 | 536 | if copyfile(abssrc, relsrc, targetpath(abssrc), exact): |
|
537 | 537 | errors += 1 |
|
538 | 538 | |
|
539 | 539 | if errors: |
|
540 | 540 | ui.warn(_('(consider using --after)\n')) |
|
541 | 541 | |
|
542 | 542 | return errors != 0 |
|
543 | 543 | |
|
544 | 544 | def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None, |
|
545 | 545 | runargs=None, appendpid=False): |
|
546 | 546 | '''Run a command as a service.''' |
|
547 | 547 | |
|
548 | 548 | if opts['daemon'] and not opts['daemon_pipefds']: |
|
549 | 549 | # Signal child process startup with file removal |
|
550 | 550 | lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-') |
|
551 | 551 | os.close(lockfd) |
|
552 | 552 | try: |
|
553 | 553 | if not runargs: |
|
554 | 554 | runargs = util.hgcmd() + sys.argv[1:] |
|
555 | 555 | runargs.append('--daemon-pipefds=%s' % lockpath) |
|
556 | 556 | # Don't pass --cwd to the child process, because we've already |
|
557 | 557 | # changed directory. |
|
558 | 558 | for i in xrange(1, len(runargs)): |
|
559 | 559 | if runargs[i].startswith('--cwd='): |
|
560 | 560 | del runargs[i] |
|
561 | 561 | break |
|
562 | 562 | elif runargs[i].startswith('--cwd'): |
|
563 | 563 | del runargs[i:i + 2] |
|
564 | 564 | break |
|
565 | 565 | def condfn(): |
|
566 | 566 | return not os.path.exists(lockpath) |
|
567 | 567 | pid = util.rundetached(runargs, condfn) |
|
568 | 568 | if pid < 0: |
|
569 | 569 | raise util.Abort(_('child process failed to start')) |
|
570 | 570 | finally: |
|
571 | 571 | try: |
|
572 | 572 | os.unlink(lockpath) |
|
573 | 573 | except OSError, e: |
|
574 | 574 | if e.errno != errno.ENOENT: |
|
575 | 575 | raise |
|
576 | 576 | if parentfn: |
|
577 | 577 | return parentfn(pid) |
|
578 | 578 | else: |
|
579 | 579 | return |
|
580 | 580 | |
|
581 | 581 | if initfn: |
|
582 | 582 | initfn() |
|
583 | 583 | |
|
584 | 584 | if opts['pid_file']: |
|
585 | 585 | mode = appendpid and 'a' or 'w' |
|
586 | 586 | fp = open(opts['pid_file'], mode) |
|
587 | 587 | fp.write(str(os.getpid()) + '\n') |
|
588 | 588 | fp.close() |
|
589 | 589 | |
|
590 | 590 | if opts['daemon_pipefds']: |
|
591 | 591 | lockpath = opts['daemon_pipefds'] |
|
592 | 592 | try: |
|
593 | 593 | os.setsid() |
|
594 | 594 | except AttributeError: |
|
595 | 595 | pass |
|
596 | 596 | os.unlink(lockpath) |
|
597 | 597 | util.hidewindow() |
|
598 | 598 | sys.stdout.flush() |
|
599 | 599 | sys.stderr.flush() |
|
600 | 600 | |
|
601 | 601 | nullfd = os.open(util.nulldev, os.O_RDWR) |
|
602 | 602 | logfilefd = nullfd |
|
603 | 603 | if logfile: |
|
604 | 604 | logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND) |
|
605 | 605 | os.dup2(nullfd, 0) |
|
606 | 606 | os.dup2(logfilefd, 1) |
|
607 | 607 | os.dup2(logfilefd, 2) |
|
608 | 608 | if nullfd not in (0, 1, 2): |
|
609 | 609 | os.close(nullfd) |
|
610 | 610 | if logfile and logfilefd not in (0, 1, 2): |
|
611 | 611 | os.close(logfilefd) |
|
612 | 612 | |
|
613 | 613 | if runfn: |
|
614 | 614 | return runfn() |
|
615 | 615 | |
|
616 | 616 | def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False, |
|
617 | 617 | opts=None): |
|
618 | 618 | '''export changesets as hg patches.''' |
|
619 | 619 | |
|
620 | 620 | total = len(revs) |
|
621 | 621 | revwidth = max([len(str(rev)) for rev in revs]) |
|
622 | 622 | |
|
623 | 623 | def single(rev, seqno, fp): |
|
624 | 624 | ctx = repo[rev] |
|
625 | 625 | node = ctx.node() |
|
626 | 626 | parents = [p.node() for p in ctx.parents() if p] |
|
627 | 627 | branch = ctx.branch() |
|
628 | 628 | if switch_parent: |
|
629 | 629 | parents.reverse() |
|
630 | 630 | prev = (parents and parents[0]) or nullid |
|
631 | 631 | |
|
632 | 632 | if not fp: |
|
633 | 633 | fp = make_file(repo, template, node, total=total, seqno=seqno, |
|
634 | 634 | revwidth=revwidth, mode='ab') |
|
635 | 635 | if fp != sys.stdout and hasattr(fp, 'name'): |
|
636 | 636 | repo.ui.note("%s\n" % fp.name) |
|
637 | 637 | |
|
638 | 638 | fp.write("# HG changeset patch\n") |
|
639 | 639 | fp.write("# User %s\n" % ctx.user()) |
|
640 | 640 | fp.write("# Date %d %d\n" % ctx.date()) |
|
641 | 641 | if branch and (branch != 'default'): |
|
642 | 642 | fp.write("# Branch %s\n" % branch) |
|
643 | 643 | fp.write("# Node ID %s\n" % hex(node)) |
|
644 | 644 | fp.write("# Parent %s\n" % hex(prev)) |
|
645 | 645 | if len(parents) > 1: |
|
646 | 646 | fp.write("# Parent %s\n" % hex(parents[1])) |
|
647 | 647 | fp.write(ctx.description().rstrip()) |
|
648 | 648 | fp.write("\n\n") |
|
649 | 649 | |
|
650 | 650 | for chunk in patch.diff(repo, prev, node, opts=opts): |
|
651 | 651 | fp.write(chunk) |
|
652 | 652 | |
|
653 | 653 | for seqno, rev in enumerate(revs): |
|
654 | 654 | single(rev, seqno + 1, fp) |
|
655 | 655 | |
|
656 | 656 | def diffordiffstat(ui, repo, diffopts, node1, node2, match, |
|
657 | 657 | changes=None, stat=False, fp=None): |
|
658 | 658 | '''show diff or diffstat.''' |
|
659 | 659 | if fp is None: |
|
660 | 660 | write = ui.write |
|
661 | 661 | else: |
|
662 | 662 | def write(s, **kw): |
|
663 | 663 | fp.write(s) |
|
664 | 664 | |
|
665 | 665 | if stat: |
|
666 |
diffopts |
|
|
666 | diffopts = diffopts.copy(context=0) | |
|
667 | 667 | width = 80 |
|
668 | 668 | if not ui.plain(): |
|
669 | 669 | width = util.termwidth() |
|
670 | 670 | chunks = patch.diff(repo, node1, node2, match, changes, diffopts) |
|
671 | 671 | for chunk, label in patch.diffstatui(util.iterlines(chunks), |
|
672 | 672 | width=width, |
|
673 | 673 | git=diffopts.git): |
|
674 | 674 | write(chunk, label=label) |
|
675 | 675 | else: |
|
676 | 676 | for chunk, label in patch.diffui(repo, node1, node2, match, |
|
677 | 677 | changes, diffopts): |
|
678 | 678 | write(chunk, label=label) |
|
679 | 679 | |
|
680 | 680 | class changeset_printer(object): |
|
681 | 681 | '''show changeset information when templating not requested.''' |
|
682 | 682 | |
|
683 | 683 | def __init__(self, ui, repo, patch, diffopts, buffered): |
|
684 | 684 | self.ui = ui |
|
685 | 685 | self.repo = repo |
|
686 | 686 | self.buffered = buffered |
|
687 | 687 | self.patch = patch |
|
688 | 688 | self.diffopts = diffopts |
|
689 | 689 | self.header = {} |
|
690 | 690 | self.hunk = {} |
|
691 | 691 | self.lastheader = None |
|
692 | 692 | self.footer = None |
|
693 | 693 | |
|
694 | 694 | def flush(self, rev): |
|
695 | 695 | if rev in self.header: |
|
696 | 696 | h = self.header[rev] |
|
697 | 697 | if h != self.lastheader: |
|
698 | 698 | self.lastheader = h |
|
699 | 699 | self.ui.write(h) |
|
700 | 700 | del self.header[rev] |
|
701 | 701 | if rev in self.hunk: |
|
702 | 702 | self.ui.write(self.hunk[rev]) |
|
703 | 703 | del self.hunk[rev] |
|
704 | 704 | return 1 |
|
705 | 705 | return 0 |
|
706 | 706 | |
|
707 | 707 | def close(self): |
|
708 | 708 | if self.footer: |
|
709 | 709 | self.ui.write(self.footer) |
|
710 | 710 | |
|
711 | 711 | def show(self, ctx, copies=None, matchfn=None, **props): |
|
712 | 712 | if self.buffered: |
|
713 | 713 | self.ui.pushbuffer() |
|
714 | 714 | self._show(ctx, copies, matchfn, props) |
|
715 | 715 | self.hunk[ctx.rev()] = self.ui.popbuffer(labeled=True) |
|
716 | 716 | else: |
|
717 | 717 | self._show(ctx, copies, matchfn, props) |
|
718 | 718 | |
|
719 | 719 | def _show(self, ctx, copies, matchfn, props): |
|
720 | 720 | '''show a single changeset or file revision''' |
|
721 | 721 | changenode = ctx.node() |
|
722 | 722 | rev = ctx.rev() |
|
723 | 723 | |
|
724 | 724 | if self.ui.quiet: |
|
725 | 725 | self.ui.write("%d:%s\n" % (rev, short(changenode)), |
|
726 | 726 | label='log.node') |
|
727 | 727 | return |
|
728 | 728 | |
|
729 | 729 | log = self.repo.changelog |
|
730 | 730 | date = util.datestr(ctx.date()) |
|
731 | 731 | |
|
732 | 732 | hexfunc = self.ui.debugflag and hex or short |
|
733 | 733 | |
|
734 | 734 | parents = [(p, hexfunc(log.node(p))) |
|
735 | 735 | for p in self._meaningful_parentrevs(log, rev)] |
|
736 | 736 | |
|
737 | 737 | self.ui.write(_("changeset: %d:%s\n") % (rev, hexfunc(changenode)), |
|
738 | 738 | label='log.changeset') |
|
739 | 739 | |
|
740 | 740 | branch = ctx.branch() |
|
741 | 741 | # don't show the default branch name |
|
742 | 742 | if branch != 'default': |
|
743 | 743 | branch = encoding.tolocal(branch) |
|
744 | 744 | self.ui.write(_("branch: %s\n") % branch, |
|
745 | 745 | label='log.branch') |
|
746 | 746 | for tag in self.repo.nodetags(changenode): |
|
747 | 747 | self.ui.write(_("tag: %s\n") % tag, |
|
748 | 748 | label='log.tag') |
|
749 | 749 | for parent in parents: |
|
750 | 750 | self.ui.write(_("parent: %d:%s\n") % parent, |
|
751 | 751 | label='log.parent') |
|
752 | 752 | |
|
753 | 753 | if self.ui.debugflag: |
|
754 | 754 | mnode = ctx.manifestnode() |
|
755 | 755 | self.ui.write(_("manifest: %d:%s\n") % |
|
756 | 756 | (self.repo.manifest.rev(mnode), hex(mnode)), |
|
757 | 757 | label='ui.debug log.manifest') |
|
758 | 758 | self.ui.write(_("user: %s\n") % ctx.user(), |
|
759 | 759 | label='log.user') |
|
760 | 760 | self.ui.write(_("date: %s\n") % date, |
|
761 | 761 | label='log.date') |
|
762 | 762 | |
|
763 | 763 | if self.ui.debugflag: |
|
764 | 764 | files = self.repo.status(log.parents(changenode)[0], changenode)[:3] |
|
765 | 765 | for key, value in zip([_("files:"), _("files+:"), _("files-:")], |
|
766 | 766 | files): |
|
767 | 767 | if value: |
|
768 | 768 | self.ui.write("%-12s %s\n" % (key, " ".join(value)), |
|
769 | 769 | label='ui.debug log.files') |
|
770 | 770 | elif ctx.files() and self.ui.verbose: |
|
771 | 771 | self.ui.write(_("files: %s\n") % " ".join(ctx.files()), |
|
772 | 772 | label='ui.note log.files') |
|
773 | 773 | if copies and self.ui.verbose: |
|
774 | 774 | copies = ['%s (%s)' % c for c in copies] |
|
775 | 775 | self.ui.write(_("copies: %s\n") % ' '.join(copies), |
|
776 | 776 | label='ui.note log.copies') |
|
777 | 777 | |
|
778 | 778 | extra = ctx.extra() |
|
779 | 779 | if extra and self.ui.debugflag: |
|
780 | 780 | for key, value in sorted(extra.items()): |
|
781 | 781 | self.ui.write(_("extra: %s=%s\n") |
|
782 | 782 | % (key, value.encode('string_escape')), |
|
783 | 783 | label='ui.debug log.extra') |
|
784 | 784 | |
|
785 | 785 | description = ctx.description().strip() |
|
786 | 786 | if description: |
|
787 | 787 | if self.ui.verbose: |
|
788 | 788 | self.ui.write(_("description:\n"), |
|
789 | 789 | label='ui.note log.description') |
|
790 | 790 | self.ui.write(description, |
|
791 | 791 | label='ui.note log.description') |
|
792 | 792 | self.ui.write("\n\n") |
|
793 | 793 | else: |
|
794 | 794 | self.ui.write(_("summary: %s\n") % |
|
795 | 795 | description.splitlines()[0], |
|
796 | 796 | label='log.summary') |
|
797 | 797 | self.ui.write("\n") |
|
798 | 798 | |
|
799 | 799 | self.showpatch(changenode, matchfn) |
|
800 | 800 | |
|
801 | 801 | def showpatch(self, node, matchfn): |
|
802 | 802 | if not matchfn: |
|
803 | 803 | matchfn = self.patch |
|
804 | 804 | if matchfn: |
|
805 | 805 | stat = self.diffopts.get('stat') |
|
806 | diff = self.diffopts.get('patch') | |
|
806 | 807 | diffopts = patch.diffopts(self.ui, self.diffopts) |
|
807 | 808 | prev = self.repo.changelog.parents(node)[0] |
|
808 | diffordiffstat(self.ui, self.repo, diffopts, prev, node, | |
|
809 | match=matchfn, stat=stat) | |
|
809 | if stat: | |
|
810 | diffordiffstat(self.ui, self.repo, diffopts, prev, node, | |
|
811 | match=matchfn, stat=True) | |
|
812 | if diff: | |
|
813 | if stat: | |
|
814 | self.ui.write("\n") | |
|
815 | diffordiffstat(self.ui, self.repo, diffopts, prev, node, | |
|
816 | match=matchfn, stat=False) | |
|
810 | 817 | self.ui.write("\n") |
|
811 | 818 | |
|
812 | 819 | def _meaningful_parentrevs(self, log, rev): |
|
813 | 820 | """Return list of meaningful (or all if debug) parentrevs for rev. |
|
814 | 821 | |
|
815 | 822 | For merges (two non-nullrev revisions) both parents are meaningful. |
|
816 | 823 | Otherwise the first parent revision is considered meaningful if it |
|
817 | 824 | is not the preceding revision. |
|
818 | 825 | """ |
|
819 | 826 | parents = log.parentrevs(rev) |
|
820 | 827 | if not self.ui.debugflag and parents[1] == nullrev: |
|
821 | 828 | if parents[0] >= rev - 1: |
|
822 | 829 | parents = [] |
|
823 | 830 | else: |
|
824 | 831 | parents = [parents[0]] |
|
825 | 832 | return parents |
|
826 | 833 | |
|
827 | 834 | |
|
828 | 835 | class changeset_templater(changeset_printer): |
|
829 | 836 | '''format changeset information.''' |
|
830 | 837 | |
|
831 | 838 | def __init__(self, ui, repo, patch, diffopts, mapfile, buffered): |
|
832 | 839 | changeset_printer.__init__(self, ui, repo, patch, diffopts, buffered) |
|
833 | 840 | formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12]) |
|
834 | 841 | defaulttempl = { |
|
835 | 842 | 'parent': '{rev}:{node|formatnode} ', |
|
836 | 843 | 'manifest': '{rev}:{node|formatnode}', |
|
837 | 844 | 'file_copy': '{name} ({source})', |
|
838 | 845 | 'extra': '{key}={value|stringescape}' |
|
839 | 846 | } |
|
840 | 847 | # filecopy is preserved for compatibility reasons |
|
841 | 848 | defaulttempl['filecopy'] = defaulttempl['file_copy'] |
|
842 | 849 | self.t = templater.templater(mapfile, {'formatnode': formatnode}, |
|
843 | 850 | cache=defaulttempl) |
|
844 | 851 | self.cache = {} |
|
845 | 852 | |
|
846 | 853 | def use_template(self, t): |
|
847 | 854 | '''set template string to use''' |
|
848 | 855 | self.t.cache['changeset'] = t |
|
849 | 856 | |
|
850 | 857 | def _meaningful_parentrevs(self, ctx): |
|
851 | 858 | """Return list of meaningful (or all if debug) parentrevs for rev. |
|
852 | 859 | """ |
|
853 | 860 | parents = ctx.parents() |
|
854 | 861 | if len(parents) > 1: |
|
855 | 862 | return parents |
|
856 | 863 | if self.ui.debugflag: |
|
857 | 864 | return [parents[0], self.repo['null']] |
|
858 | 865 | if parents[0].rev() >= ctx.rev() - 1: |
|
859 | 866 | return [] |
|
860 | 867 | return parents |
|
861 | 868 | |
|
862 | 869 | def _show(self, ctx, copies, matchfn, props): |
|
863 | 870 | '''show a single changeset or file revision''' |
|
864 | 871 | |
|
865 | 872 | showlist = templatekw.showlist |
|
866 | 873 | |
|
867 | 874 | # showparents() behaviour depends on ui trace level which |
|
868 | 875 | # causes unexpected behaviours at templating level and makes |
|
869 | 876 | # it harder to extract it in a standalone function. Its |
|
870 | 877 | # behaviour cannot be changed so leave it here for now. |
|
871 | 878 | def showparents(**args): |
|
872 | 879 | ctx = args['ctx'] |
|
873 | 880 | parents = [[('rev', p.rev()), ('node', p.hex())] |
|
874 | 881 | for p in self._meaningful_parentrevs(ctx)] |
|
875 | 882 | return showlist('parent', parents, **args) |
|
876 | 883 | |
|
877 | 884 | props = props.copy() |
|
878 | 885 | props.update(templatekw.keywords) |
|
879 | 886 | props['parents'] = showparents |
|
880 | 887 | props['templ'] = self.t |
|
881 | 888 | props['ctx'] = ctx |
|
882 | 889 | props['repo'] = self.repo |
|
883 | 890 | props['revcache'] = {'copies': copies} |
|
884 | 891 | props['cache'] = self.cache |
|
885 | 892 | |
|
886 | 893 | # find correct templates for current mode |
|
887 | 894 | |
|
888 | 895 | tmplmodes = [ |
|
889 | 896 | (True, None), |
|
890 | 897 | (self.ui.verbose, 'verbose'), |
|
891 | 898 | (self.ui.quiet, 'quiet'), |
|
892 | 899 | (self.ui.debugflag, 'debug'), |
|
893 | 900 | ] |
|
894 | 901 | |
|
895 | 902 | types = {'header': '', 'footer':'', 'changeset': 'changeset'} |
|
896 | 903 | for mode, postfix in tmplmodes: |
|
897 | 904 | for type in types: |
|
898 | 905 | cur = postfix and ('%s_%s' % (type, postfix)) or type |
|
899 | 906 | if mode and cur in self.t: |
|
900 | 907 | types[type] = cur |
|
901 | 908 | |
|
902 | 909 | try: |
|
903 | 910 | |
|
904 | 911 | # write header |
|
905 | 912 | if types['header']: |
|
906 | 913 | h = templater.stringify(self.t(types['header'], **props)) |
|
907 | 914 | if self.buffered: |
|
908 | 915 | self.header[ctx.rev()] = h |
|
909 | 916 | else: |
|
910 | 917 | if self.lastheader != h: |
|
911 | 918 | self.lastheader = h |
|
912 | 919 | self.ui.write(h) |
|
913 | 920 | |
|
914 | 921 | # write changeset metadata, then patch if requested |
|
915 | 922 | key = types['changeset'] |
|
916 | 923 | self.ui.write(templater.stringify(self.t(key, **props))) |
|
917 | 924 | self.showpatch(ctx.node(), matchfn) |
|
918 | 925 | |
|
919 | 926 | if types['footer']: |
|
920 | 927 | if not self.footer: |
|
921 | 928 | self.footer = templater.stringify(self.t(types['footer'], |
|
922 | 929 | **props)) |
|
923 | 930 | |
|
924 | 931 | except KeyError, inst: |
|
925 | 932 | msg = _("%s: no key named '%s'") |
|
926 | 933 | raise util.Abort(msg % (self.t.mapfile, inst.args[0])) |
|
927 | 934 | except SyntaxError, inst: |
|
928 | 935 | raise util.Abort('%s: %s' % (self.t.mapfile, inst.args[0])) |
|
929 | 936 | |
|
930 | 937 | def show_changeset(ui, repo, opts, buffered=False): |
|
931 | 938 | """show one changeset using template or regular display. |
|
932 | 939 | |
|
933 | 940 | Display format will be the first non-empty hit of: |
|
934 | 941 | 1. option 'template' |
|
935 | 942 | 2. option 'style' |
|
936 | 943 | 3. [ui] setting 'logtemplate' |
|
937 | 944 | 4. [ui] setting 'style' |
|
938 | 945 | If all of these values are either the unset or the empty string, |
|
939 | 946 | regular display via changeset_printer() is done. |
|
940 | 947 | """ |
|
941 | 948 | # options |
|
942 | 949 | patch = False |
|
943 | 950 | if opts.get('patch') or opts.get('stat'): |
|
944 | 951 | patch = matchall(repo) |
|
945 | 952 | |
|
946 | 953 | tmpl = opts.get('template') |
|
947 | 954 | style = None |
|
948 | 955 | if tmpl: |
|
949 | 956 | tmpl = templater.parsestring(tmpl, quoted=False) |
|
950 | 957 | else: |
|
951 | 958 | style = opts.get('style') |
|
952 | 959 | |
|
953 | 960 | # ui settings |
|
954 | 961 | if not (tmpl or style): |
|
955 | 962 | tmpl = ui.config('ui', 'logtemplate') |
|
956 | 963 | if tmpl: |
|
957 | 964 | tmpl = templater.parsestring(tmpl) |
|
958 | 965 | else: |
|
959 | 966 | style = util.expandpath(ui.config('ui', 'style', '')) |
|
960 | 967 | |
|
961 | 968 | if not (tmpl or style): |
|
962 | 969 | return changeset_printer(ui, repo, patch, opts, buffered) |
|
963 | 970 | |
|
964 | 971 | mapfile = None |
|
965 | 972 | if style and not tmpl: |
|
966 | 973 | mapfile = style |
|
967 | 974 | if not os.path.split(mapfile)[0]: |
|
968 | 975 | mapname = (templater.templatepath('map-cmdline.' + mapfile) |
|
969 | 976 | or templater.templatepath(mapfile)) |
|
970 | 977 | if mapname: |
|
971 | 978 | mapfile = mapname |
|
972 | 979 | |
|
973 | 980 | try: |
|
974 | 981 | t = changeset_templater(ui, repo, patch, opts, mapfile, buffered) |
|
975 | 982 | except SyntaxError, inst: |
|
976 | 983 | raise util.Abort(inst.args[0]) |
|
977 | 984 | if tmpl: |
|
978 | 985 | t.use_template(tmpl) |
|
979 | 986 | return t |
|
980 | 987 | |
|
981 | 988 | def finddate(ui, repo, date): |
|
982 | 989 | """Find the tipmost changeset that matches the given date spec""" |
|
983 | 990 | |
|
984 | 991 | df = util.matchdate(date) |
|
985 | 992 | m = matchall(repo) |
|
986 | 993 | results = {} |
|
987 | 994 | |
|
988 | 995 | def prep(ctx, fns): |
|
989 | 996 | d = ctx.date() |
|
990 | 997 | if df(d[0]): |
|
991 | 998 | results[ctx.rev()] = d |
|
992 | 999 | |
|
993 | 1000 | for ctx in walkchangerevs(repo, m, {'rev': None}, prep): |
|
994 | 1001 | rev = ctx.rev() |
|
995 | 1002 | if rev in results: |
|
996 | 1003 | ui.status(_("Found revision %s from %s\n") % |
|
997 | 1004 | (rev, util.datestr(results[rev]))) |
|
998 | 1005 | return str(rev) |
|
999 | 1006 | |
|
1000 | 1007 | raise util.Abort(_("revision matching date not found")) |
|
1001 | 1008 | |
|
1002 | 1009 | def walkchangerevs(repo, match, opts, prepare): |
|
1003 | 1010 | '''Iterate over files and the revs in which they changed. |
|
1004 | 1011 | |
|
1005 | 1012 | Callers most commonly need to iterate backwards over the history |
|
1006 | 1013 | in which they are interested. Doing so has awful (quadratic-looking) |
|
1007 | 1014 | performance, so we use iterators in a "windowed" way. |
|
1008 | 1015 | |
|
1009 | 1016 | We walk a window of revisions in the desired order. Within the |
|
1010 | 1017 | window, we first walk forwards to gather data, then in the desired |
|
1011 | 1018 | order (usually backwards) to display it. |
|
1012 | 1019 | |
|
1013 | 1020 | This function returns an iterator yielding contexts. Before |
|
1014 | 1021 | yielding each context, the iterator will first call the prepare |
|
1015 | 1022 | function on each context in the window in forward order.''' |
|
1016 | 1023 | |
|
1017 | 1024 | def increasing_windows(start, end, windowsize=8, sizelimit=512): |
|
1018 | 1025 | if start < end: |
|
1019 | 1026 | while start < end: |
|
1020 | 1027 | yield start, min(windowsize, end - start) |
|
1021 | 1028 | start += windowsize |
|
1022 | 1029 | if windowsize < sizelimit: |
|
1023 | 1030 | windowsize *= 2 |
|
1024 | 1031 | else: |
|
1025 | 1032 | while start > end: |
|
1026 | 1033 | yield start, min(windowsize, start - end - 1) |
|
1027 | 1034 | start -= windowsize |
|
1028 | 1035 | if windowsize < sizelimit: |
|
1029 | 1036 | windowsize *= 2 |
|
1030 | 1037 | |
|
1031 | 1038 | follow = opts.get('follow') or opts.get('follow_first') |
|
1032 | 1039 | |
|
1033 | 1040 | if not len(repo): |
|
1034 | 1041 | return [] |
|
1035 | 1042 | |
|
1036 | 1043 | if follow: |
|
1037 | 1044 | defrange = '%s:0' % repo['.'].rev() |
|
1038 | 1045 | else: |
|
1039 | 1046 | defrange = '-1:0' |
|
1040 | 1047 | revs = revrange(repo, opts['rev'] or [defrange]) |
|
1041 | 1048 | if not revs: |
|
1042 | 1049 | return [] |
|
1043 | 1050 | wanted = set() |
|
1044 | 1051 | slowpath = match.anypats() or (match.files() and opts.get('removed')) |
|
1045 | 1052 | fncache = {} |
|
1046 | 1053 | change = util.cachefunc(repo.changectx) |
|
1047 | 1054 | |
|
1048 | 1055 | if not slowpath and not match.files(): |
|
1049 | 1056 | # No files, no patterns. Display all revs. |
|
1050 | 1057 | wanted = set(revs) |
|
1051 | 1058 | copies = [] |
|
1052 | 1059 | |
|
1053 | 1060 | if not slowpath: |
|
1054 | 1061 | # Only files, no patterns. Check the history of each file. |
|
1055 | 1062 | def filerevgen(filelog, node): |
|
1056 | 1063 | cl_count = len(repo) |
|
1057 | 1064 | if node is None: |
|
1058 | 1065 | last = len(filelog) - 1 |
|
1059 | 1066 | else: |
|
1060 | 1067 | last = filelog.rev(node) |
|
1061 | 1068 | for i, window in increasing_windows(last, nullrev): |
|
1062 | 1069 | revs = [] |
|
1063 | 1070 | for j in xrange(i - window, i + 1): |
|
1064 | 1071 | n = filelog.node(j) |
|
1065 | 1072 | revs.append((filelog.linkrev(j), |
|
1066 | 1073 | follow and filelog.renamed(n))) |
|
1067 | 1074 | for rev in reversed(revs): |
|
1068 | 1075 | # only yield rev for which we have the changelog, it can |
|
1069 | 1076 | # happen while doing "hg log" during a pull or commit |
|
1070 | 1077 | if rev[0] < cl_count: |
|
1071 | 1078 | yield rev |
|
1072 | 1079 | def iterfiles(): |
|
1073 | 1080 | for filename in match.files(): |
|
1074 | 1081 | yield filename, None |
|
1075 | 1082 | for filename_node in copies: |
|
1076 | 1083 | yield filename_node |
|
1077 | 1084 | minrev, maxrev = min(revs), max(revs) |
|
1078 | 1085 | for file_, node in iterfiles(): |
|
1079 | 1086 | filelog = repo.file(file_) |
|
1080 | 1087 | if not len(filelog): |
|
1081 | 1088 | if node is None: |
|
1082 | 1089 | # A zero count may be a directory or deleted file, so |
|
1083 | 1090 | # try to find matching entries on the slow path. |
|
1084 | 1091 | if follow: |
|
1085 | 1092 | raise util.Abort( |
|
1086 | 1093 | _('cannot follow nonexistent file: "%s"') % file_) |
|
1087 | 1094 | slowpath = True |
|
1088 | 1095 | break |
|
1089 | 1096 | else: |
|
1090 | 1097 | continue |
|
1091 | 1098 | for rev, copied in filerevgen(filelog, node): |
|
1092 | 1099 | if rev <= maxrev: |
|
1093 | 1100 | if rev < minrev: |
|
1094 | 1101 | break |
|
1095 | 1102 | fncache.setdefault(rev, []) |
|
1096 | 1103 | fncache[rev].append(file_) |
|
1097 | 1104 | wanted.add(rev) |
|
1098 | 1105 | if copied: |
|
1099 | 1106 | copies.append(copied) |
|
1100 | 1107 | if slowpath: |
|
1101 | 1108 | if follow: |
|
1102 | 1109 | raise util.Abort(_('can only follow copies/renames for explicit ' |
|
1103 | 1110 | 'filenames')) |
|
1104 | 1111 | |
|
1105 | 1112 | # The slow path checks files modified in every changeset. |
|
1106 | 1113 | def changerevgen(): |
|
1107 | 1114 | for i, window in increasing_windows(len(repo) - 1, nullrev): |
|
1108 | 1115 | for j in xrange(i - window, i + 1): |
|
1109 | 1116 | yield change(j) |
|
1110 | 1117 | |
|
1111 | 1118 | for ctx in changerevgen(): |
|
1112 | 1119 | matches = filter(match, ctx.files()) |
|
1113 | 1120 | if matches: |
|
1114 | 1121 | fncache[ctx.rev()] = matches |
|
1115 | 1122 | wanted.add(ctx.rev()) |
|
1116 | 1123 | |
|
1117 | 1124 | class followfilter(object): |
|
1118 | 1125 | def __init__(self, onlyfirst=False): |
|
1119 | 1126 | self.startrev = nullrev |
|
1120 | 1127 | self.roots = set() |
|
1121 | 1128 | self.onlyfirst = onlyfirst |
|
1122 | 1129 | |
|
1123 | 1130 | def match(self, rev): |
|
1124 | 1131 | def realparents(rev): |
|
1125 | 1132 | if self.onlyfirst: |
|
1126 | 1133 | return repo.changelog.parentrevs(rev)[0:1] |
|
1127 | 1134 | else: |
|
1128 | 1135 | return filter(lambda x: x != nullrev, |
|
1129 | 1136 | repo.changelog.parentrevs(rev)) |
|
1130 | 1137 | |
|
1131 | 1138 | if self.startrev == nullrev: |
|
1132 | 1139 | self.startrev = rev |
|
1133 | 1140 | return True |
|
1134 | 1141 | |
|
1135 | 1142 | if rev > self.startrev: |
|
1136 | 1143 | # forward: all descendants |
|
1137 | 1144 | if not self.roots: |
|
1138 | 1145 | self.roots.add(self.startrev) |
|
1139 | 1146 | for parent in realparents(rev): |
|
1140 | 1147 | if parent in self.roots: |
|
1141 | 1148 | self.roots.add(rev) |
|
1142 | 1149 | return True |
|
1143 | 1150 | else: |
|
1144 | 1151 | # backwards: all parents |
|
1145 | 1152 | if not self.roots: |
|
1146 | 1153 | self.roots.update(realparents(self.startrev)) |
|
1147 | 1154 | if rev in self.roots: |
|
1148 | 1155 | self.roots.remove(rev) |
|
1149 | 1156 | self.roots.update(realparents(rev)) |
|
1150 | 1157 | return True |
|
1151 | 1158 | |
|
1152 | 1159 | return False |
|
1153 | 1160 | |
|
1154 | 1161 | # it might be worthwhile to do this in the iterator if the rev range |
|
1155 | 1162 | # is descending and the prune args are all within that range |
|
1156 | 1163 | for rev in opts.get('prune', ()): |
|
1157 | 1164 | rev = repo.changelog.rev(repo.lookup(rev)) |
|
1158 | 1165 | ff = followfilter() |
|
1159 | 1166 | stop = min(revs[0], revs[-1]) |
|
1160 | 1167 | for x in xrange(rev, stop - 1, -1): |
|
1161 | 1168 | if ff.match(x): |
|
1162 | 1169 | wanted.discard(x) |
|
1163 | 1170 | |
|
1164 | 1171 | def iterate(): |
|
1165 | 1172 | if follow and not match.files(): |
|
1166 | 1173 | ff = followfilter(onlyfirst=opts.get('follow_first')) |
|
1167 | 1174 | def want(rev): |
|
1168 | 1175 | return ff.match(rev) and rev in wanted |
|
1169 | 1176 | else: |
|
1170 | 1177 | def want(rev): |
|
1171 | 1178 | return rev in wanted |
|
1172 | 1179 | |
|
1173 | 1180 | for i, window in increasing_windows(0, len(revs)): |
|
1174 | 1181 | change = util.cachefunc(repo.changectx) |
|
1175 | 1182 | nrevs = [rev for rev in revs[i:i + window] if want(rev)] |
|
1176 | 1183 | for rev in sorted(nrevs): |
|
1177 | 1184 | fns = fncache.get(rev) |
|
1178 | 1185 | ctx = change(rev) |
|
1179 | 1186 | if not fns: |
|
1180 | 1187 | def fns_generator(): |
|
1181 | 1188 | for f in ctx.files(): |
|
1182 | 1189 | if match(f): |
|
1183 | 1190 | yield f |
|
1184 | 1191 | fns = fns_generator() |
|
1185 | 1192 | prepare(ctx, fns) |
|
1186 | 1193 | for rev in nrevs: |
|
1187 | 1194 | yield change(rev) |
|
1188 | 1195 | return iterate() |
|
1189 | 1196 | |
|
1190 | 1197 | def commit(ui, repo, commitfunc, pats, opts): |
|
1191 | 1198 | '''commit the specified files or all outstanding changes''' |
|
1192 | 1199 | date = opts.get('date') |
|
1193 | 1200 | if date: |
|
1194 | 1201 | opts['date'] = util.parsedate(date) |
|
1195 | 1202 | message = logmessage(opts) |
|
1196 | 1203 | |
|
1197 | 1204 | # extract addremove carefully -- this function can be called from a command |
|
1198 | 1205 | # that doesn't support addremove |
|
1199 | 1206 | if opts.get('addremove'): |
|
1200 | 1207 | addremove(repo, pats, opts) |
|
1201 | 1208 | |
|
1202 | 1209 | return commitfunc(ui, repo, message, match(repo, pats, opts), opts) |
|
1203 | 1210 | |
|
1204 | 1211 | def commiteditor(repo, ctx, subs): |
|
1205 | 1212 | if ctx.description(): |
|
1206 | 1213 | return ctx.description() |
|
1207 | 1214 | return commitforceeditor(repo, ctx, subs) |
|
1208 | 1215 | |
|
1209 | 1216 | def commitforceeditor(repo, ctx, subs): |
|
1210 | 1217 | edittext = [] |
|
1211 | 1218 | modified, added, removed = ctx.modified(), ctx.added(), ctx.removed() |
|
1212 | 1219 | if ctx.description(): |
|
1213 | 1220 | edittext.append(ctx.description()) |
|
1214 | 1221 | edittext.append("") |
|
1215 | 1222 | edittext.append("") # Empty line between message and comments. |
|
1216 | 1223 | edittext.append(_("HG: Enter commit message." |
|
1217 | 1224 | " Lines beginning with 'HG:' are removed.")) |
|
1218 | 1225 | edittext.append(_("HG: Leave message empty to abort commit.")) |
|
1219 | 1226 | edittext.append("HG: --") |
|
1220 | 1227 | edittext.append(_("HG: user: %s") % ctx.user()) |
|
1221 | 1228 | if ctx.p2(): |
|
1222 | 1229 | edittext.append(_("HG: branch merge")) |
|
1223 | 1230 | if ctx.branch(): |
|
1224 | 1231 | edittext.append(_("HG: branch '%s'") |
|
1225 | 1232 | % encoding.tolocal(ctx.branch())) |
|
1226 | 1233 | edittext.extend([_("HG: subrepo %s") % s for s in subs]) |
|
1227 | 1234 | edittext.extend([_("HG: added %s") % f for f in added]) |
|
1228 | 1235 | edittext.extend([_("HG: changed %s") % f for f in modified]) |
|
1229 | 1236 | edittext.extend([_("HG: removed %s") % f for f in removed]) |
|
1230 | 1237 | if not added and not modified and not removed: |
|
1231 | 1238 | edittext.append(_("HG: no files changed")) |
|
1232 | 1239 | edittext.append("") |
|
1233 | 1240 | # run editor in the repository root |
|
1234 | 1241 | olddir = os.getcwd() |
|
1235 | 1242 | os.chdir(repo.root) |
|
1236 | 1243 | text = repo.ui.edit("\n".join(edittext), ctx.user()) |
|
1237 | 1244 | text = re.sub("(?m)^HG:.*\n", "", text) |
|
1238 | 1245 | os.chdir(olddir) |
|
1239 | 1246 | |
|
1240 | 1247 | if not text.strip(): |
|
1241 | 1248 | raise util.Abort(_("empty commit message")) |
|
1242 | 1249 | |
|
1243 | 1250 | return text |
General Comments 0
You need to be logged in to leave comments.
Login now