Show More
@@ -1,1282 +1,1282 b'' | |||
|
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, tempfile |
|
11 | 11 | import util, scmutil, templater, patch, error, templatekw, revlog, copies |
|
12 | 12 | import match as matchmod |
|
13 | 13 | import subrepo |
|
14 | 14 | |
|
15 | 15 | def parsealiases(cmd): |
|
16 | 16 | return cmd.lstrip("^").split("|") |
|
17 | 17 | |
|
18 | 18 | def findpossible(cmd, table, strict=False): |
|
19 | 19 | """ |
|
20 | 20 | Return cmd -> (aliases, command table entry) |
|
21 | 21 | for each matching command. |
|
22 | 22 | Return debug commands (or their aliases) only if no normal command matches. |
|
23 | 23 | """ |
|
24 | 24 | choice = {} |
|
25 | 25 | debugchoice = {} |
|
26 | 26 | |
|
27 | 27 | if cmd in table: |
|
28 | 28 | # short-circuit exact matches, "log" alias beats "^log|history" |
|
29 | 29 | keys = [cmd] |
|
30 | 30 | else: |
|
31 | 31 | keys = table.keys() |
|
32 | 32 | |
|
33 | 33 | for e in keys: |
|
34 | 34 | aliases = parsealiases(e) |
|
35 | 35 | found = None |
|
36 | 36 | if cmd in aliases: |
|
37 | 37 | found = cmd |
|
38 | 38 | elif not strict: |
|
39 | 39 | for a in aliases: |
|
40 | 40 | if a.startswith(cmd): |
|
41 | 41 | found = a |
|
42 | 42 | break |
|
43 | 43 | if found is not None: |
|
44 | 44 | if aliases[0].startswith("debug") or found.startswith("debug"): |
|
45 | 45 | debugchoice[found] = (aliases, table[e]) |
|
46 | 46 | else: |
|
47 | 47 | choice[found] = (aliases, table[e]) |
|
48 | 48 | |
|
49 | 49 | if not choice and debugchoice: |
|
50 | 50 | choice = debugchoice |
|
51 | 51 | |
|
52 | 52 | return choice |
|
53 | 53 | |
|
54 | 54 | def findcmd(cmd, table, strict=True): |
|
55 | 55 | """Return (aliases, command table entry) for command string.""" |
|
56 | 56 | choice = findpossible(cmd, table, strict) |
|
57 | 57 | |
|
58 | 58 | if cmd in choice: |
|
59 | 59 | return choice[cmd] |
|
60 | 60 | |
|
61 | 61 | if len(choice) > 1: |
|
62 | 62 | clist = choice.keys() |
|
63 | 63 | clist.sort() |
|
64 | 64 | raise error.AmbiguousCommand(cmd, clist) |
|
65 | 65 | |
|
66 | 66 | if choice: |
|
67 | 67 | return choice.values()[0] |
|
68 | 68 | |
|
69 | 69 | raise error.UnknownCommand(cmd) |
|
70 | 70 | |
|
71 | 71 | def findrepo(p): |
|
72 | 72 | while not os.path.isdir(os.path.join(p, ".hg")): |
|
73 | 73 | oldp, p = p, os.path.dirname(p) |
|
74 | 74 | if p == oldp: |
|
75 | 75 | return None |
|
76 | 76 | |
|
77 | 77 | return p |
|
78 | 78 | |
|
79 | 79 | def bailifchanged(repo): |
|
80 | 80 | if repo.dirstate.p2() != nullid: |
|
81 | 81 | raise util.Abort(_('outstanding uncommitted merge')) |
|
82 | 82 | modified, added, removed, deleted = repo.status()[:4] |
|
83 | 83 | if modified or added or removed or deleted: |
|
84 | 84 | raise util.Abort(_("outstanding uncommitted changes")) |
|
85 | 85 | ctx = repo[None] |
|
86 | 86 | for s in ctx.substate: |
|
87 | 87 | if ctx.sub(s).dirty(): |
|
88 | 88 | raise util.Abort(_("uncommitted changes in subrepo %s") % s) |
|
89 | 89 | |
|
90 | 90 | def logmessage(ui, opts): |
|
91 | 91 | """ get the log message according to -m and -l option """ |
|
92 | 92 | message = opts.get('message') |
|
93 | 93 | logfile = opts.get('logfile') |
|
94 | 94 | |
|
95 | 95 | if message and logfile: |
|
96 | 96 | raise util.Abort(_('options --message and --logfile are mutually ' |
|
97 | 97 | 'exclusive')) |
|
98 | 98 | if not message and logfile: |
|
99 | 99 | try: |
|
100 | 100 | if logfile == '-': |
|
101 | 101 | message = ui.fin.read() |
|
102 | 102 | else: |
|
103 | 103 | message = '\n'.join(util.readfile(logfile).splitlines()) |
|
104 | 104 | except IOError, inst: |
|
105 | 105 | raise util.Abort(_("can't read commit message '%s': %s") % |
|
106 | 106 | (logfile, inst.strerror)) |
|
107 | 107 | return message |
|
108 | 108 | |
|
109 | 109 | def loglimit(opts): |
|
110 | 110 | """get the log limit according to option -l/--limit""" |
|
111 | 111 | limit = opts.get('limit') |
|
112 | 112 | if limit: |
|
113 | 113 | try: |
|
114 | 114 | limit = int(limit) |
|
115 | 115 | except ValueError: |
|
116 | 116 | raise util.Abort(_('limit must be a positive integer')) |
|
117 | 117 | if limit <= 0: |
|
118 | 118 | raise util.Abort(_('limit must be positive')) |
|
119 | 119 | else: |
|
120 | 120 | limit = None |
|
121 | 121 | return limit |
|
122 | 122 | |
|
123 | 123 | def makefilename(repo, pat, node, desc=None, |
|
124 | 124 | total=None, seqno=None, revwidth=None, pathname=None): |
|
125 | 125 | node_expander = { |
|
126 | 126 | 'H': lambda: hex(node), |
|
127 | 127 | 'R': lambda: str(repo.changelog.rev(node)), |
|
128 | 128 | 'h': lambda: short(node), |
|
129 | 129 | 'm': lambda: re.sub('[^\w]', '_', str(desc)) |
|
130 | 130 | } |
|
131 | 131 | expander = { |
|
132 | 132 | '%': lambda: '%', |
|
133 | 133 | 'b': lambda: os.path.basename(repo.root), |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | try: |
|
137 | 137 | if node: |
|
138 | 138 | expander.update(node_expander) |
|
139 | 139 | if node: |
|
140 | 140 | expander['r'] = (lambda: |
|
141 | 141 | str(repo.changelog.rev(node)).zfill(revwidth or 0)) |
|
142 | 142 | if total is not None: |
|
143 | 143 | expander['N'] = lambda: str(total) |
|
144 | 144 | if seqno is not None: |
|
145 | 145 | expander['n'] = lambda: str(seqno) |
|
146 | 146 | if total is not None and seqno is not None: |
|
147 | 147 | expander['n'] = lambda: str(seqno).zfill(len(str(total))) |
|
148 | 148 | if pathname is not None: |
|
149 | 149 | expander['s'] = lambda: os.path.basename(pathname) |
|
150 | 150 | expander['d'] = lambda: os.path.dirname(pathname) or '.' |
|
151 | 151 | expander['p'] = lambda: pathname |
|
152 | 152 | |
|
153 | 153 | newname = [] |
|
154 | 154 | patlen = len(pat) |
|
155 | 155 | i = 0 |
|
156 | 156 | while i < patlen: |
|
157 | 157 | c = pat[i] |
|
158 | 158 | if c == '%': |
|
159 | 159 | i += 1 |
|
160 | 160 | c = pat[i] |
|
161 | 161 | c = expander[c]() |
|
162 | 162 | newname.append(c) |
|
163 | 163 | i += 1 |
|
164 | 164 | return ''.join(newname) |
|
165 | 165 | except KeyError, inst: |
|
166 | 166 | raise util.Abort(_("invalid format spec '%%%s' in output filename") % |
|
167 | 167 | inst.args[0]) |
|
168 | 168 | |
|
169 | 169 | def makefileobj(repo, pat, node=None, desc=None, total=None, |
|
170 | 170 | seqno=None, revwidth=None, mode='wb', pathname=None): |
|
171 | 171 | |
|
172 | 172 | writable = mode not in ('r', 'rb') |
|
173 | 173 | |
|
174 | 174 | if not pat or pat == '-': |
|
175 | 175 | fp = writable and repo.ui.fout or repo.ui.fin |
|
176 | 176 | if util.safehasattr(fp, 'fileno'): |
|
177 | 177 | return os.fdopen(os.dup(fp.fileno()), mode) |
|
178 | 178 | else: |
|
179 | 179 | # if this fp can't be duped properly, return |
|
180 | 180 | # a dummy object that can be closed |
|
181 | 181 | class wrappedfileobj(object): |
|
182 | 182 | noop = lambda x: None |
|
183 | 183 | def __init__(self, f): |
|
184 | 184 | self.f = f |
|
185 | 185 | def __getattr__(self, attr): |
|
186 | 186 | if attr == 'close': |
|
187 | 187 | return self.noop |
|
188 | 188 | else: |
|
189 | 189 | return getattr(self.f, attr) |
|
190 | 190 | |
|
191 | 191 | return wrappedfileobj(fp) |
|
192 | 192 | if util.safehasattr(pat, 'write') and writable: |
|
193 | 193 | return pat |
|
194 | 194 | if util.safehasattr(pat, 'read') and 'r' in mode: |
|
195 | 195 | return pat |
|
196 | 196 | return open(makefilename(repo, pat, node, desc, total, seqno, revwidth, |
|
197 | 197 | pathname), |
|
198 | 198 | mode) |
|
199 | 199 | |
|
200 | 200 | def openrevlog(repo, cmd, file_, opts): |
|
201 | 201 | """opens the changelog, manifest, a filelog or a given revlog""" |
|
202 | 202 | cl = opts['changelog'] |
|
203 | 203 | mf = opts['manifest'] |
|
204 | 204 | msg = None |
|
205 | 205 | if cl and mf: |
|
206 | 206 | msg = _('cannot specify --changelog and --manifest at the same time') |
|
207 | 207 | elif cl or mf: |
|
208 | 208 | if file_: |
|
209 | 209 | msg = _('cannot specify filename with --changelog or --manifest') |
|
210 | 210 | elif not repo: |
|
211 | 211 | msg = _('cannot specify --changelog or --manifest ' |
|
212 | 212 | 'without a repository') |
|
213 | 213 | if msg: |
|
214 | 214 | raise util.Abort(msg) |
|
215 | 215 | |
|
216 | 216 | r = None |
|
217 | 217 | if repo: |
|
218 | 218 | if cl: |
|
219 | 219 | r = repo.changelog |
|
220 | 220 | elif mf: |
|
221 | 221 | r = repo.manifest |
|
222 | 222 | elif file_: |
|
223 | 223 | filelog = repo.file(file_) |
|
224 | 224 | if len(filelog): |
|
225 | 225 | r = filelog |
|
226 | 226 | if not r: |
|
227 | 227 | if not file_: |
|
228 | 228 | raise error.CommandError(cmd, _('invalid arguments')) |
|
229 | 229 | if not os.path.isfile(file_): |
|
230 | 230 | raise util.Abort(_("revlog '%s' not found") % file_) |
|
231 | 231 | r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), |
|
232 | 232 | file_[:-2] + ".i") |
|
233 | 233 | return r |
|
234 | 234 | |
|
235 | 235 | def copy(ui, repo, pats, opts, rename=False): |
|
236 | 236 | # called with the repo lock held |
|
237 | 237 | # |
|
238 | 238 | # hgsep => pathname that uses "/" to separate directories |
|
239 | 239 | # ossep => pathname that uses os.sep to separate directories |
|
240 | 240 | cwd = repo.getcwd() |
|
241 | 241 | targets = {} |
|
242 | 242 | after = opts.get("after") |
|
243 | 243 | dryrun = opts.get("dry_run") |
|
244 | 244 | wctx = repo[None] |
|
245 | 245 | |
|
246 | 246 | def walkpat(pat): |
|
247 | 247 | srcs = [] |
|
248 | 248 | badstates = after and '?' or '?r' |
|
249 | 249 | m = scmutil.match(repo[None], [pat], opts, globbed=True) |
|
250 | 250 | for abs in repo.walk(m): |
|
251 | 251 | state = repo.dirstate[abs] |
|
252 | 252 | rel = m.rel(abs) |
|
253 | 253 | exact = m.exact(abs) |
|
254 | 254 | if state in badstates: |
|
255 | 255 | if exact and state == '?': |
|
256 | 256 | ui.warn(_('%s: not copying - file is not managed\n') % rel) |
|
257 | 257 | if exact and state == 'r': |
|
258 | 258 | ui.warn(_('%s: not copying - file has been marked for' |
|
259 | 259 | ' remove\n') % rel) |
|
260 | 260 | continue |
|
261 | 261 | # abs: hgsep |
|
262 | 262 | # rel: ossep |
|
263 | 263 | srcs.append((abs, rel, exact)) |
|
264 | 264 | return srcs |
|
265 | 265 | |
|
266 | 266 | # abssrc: hgsep |
|
267 | 267 | # relsrc: ossep |
|
268 | 268 | # otarget: ossep |
|
269 | 269 | def copyfile(abssrc, relsrc, otarget, exact): |
|
270 | 270 | abstarget = scmutil.canonpath(repo.root, cwd, otarget) |
|
271 | 271 | reltarget = repo.pathto(abstarget, cwd) |
|
272 | 272 | target = repo.wjoin(abstarget) |
|
273 | 273 | src = repo.wjoin(abssrc) |
|
274 | 274 | state = repo.dirstate[abstarget] |
|
275 | 275 | |
|
276 | 276 | scmutil.checkportable(ui, abstarget) |
|
277 | 277 | |
|
278 | 278 | # check for collisions |
|
279 | 279 | prevsrc = targets.get(abstarget) |
|
280 | 280 | if prevsrc is not None: |
|
281 | 281 | ui.warn(_('%s: not overwriting - %s collides with %s\n') % |
|
282 | 282 | (reltarget, repo.pathto(abssrc, cwd), |
|
283 | 283 | repo.pathto(prevsrc, cwd))) |
|
284 | 284 | return |
|
285 | 285 | |
|
286 | 286 | # check for overwrites |
|
287 | 287 | exists = os.path.lexists(target) |
|
288 | 288 | if not after and exists or after and state in 'mn': |
|
289 | 289 | if not opts['force']: |
|
290 | 290 | ui.warn(_('%s: not overwriting - file exists\n') % |
|
291 | 291 | reltarget) |
|
292 | 292 | return |
|
293 | 293 | |
|
294 | 294 | if after: |
|
295 | 295 | if not exists: |
|
296 | 296 | if rename: |
|
297 | 297 | ui.warn(_('%s: not recording move - %s does not exist\n') % |
|
298 | 298 | (relsrc, reltarget)) |
|
299 | 299 | else: |
|
300 | 300 | ui.warn(_('%s: not recording copy - %s does not exist\n') % |
|
301 | 301 | (relsrc, reltarget)) |
|
302 | 302 | return |
|
303 | 303 | elif not dryrun: |
|
304 | 304 | try: |
|
305 | 305 | if exists: |
|
306 | 306 | os.unlink(target) |
|
307 | 307 | targetdir = os.path.dirname(target) or '.' |
|
308 | 308 | if not os.path.isdir(targetdir): |
|
309 | 309 | os.makedirs(targetdir) |
|
310 | 310 | util.copyfile(src, target) |
|
311 | 311 | srcexists = True |
|
312 | 312 | except IOError, inst: |
|
313 | 313 | if inst.errno == errno.ENOENT: |
|
314 | 314 | ui.warn(_('%s: deleted in working copy\n') % relsrc) |
|
315 | 315 | srcexists = False |
|
316 | 316 | else: |
|
317 | 317 | ui.warn(_('%s: cannot copy - %s\n') % |
|
318 | 318 | (relsrc, inst.strerror)) |
|
319 | 319 | return True # report a failure |
|
320 | 320 | |
|
321 | 321 | if ui.verbose or not exact: |
|
322 | 322 | if rename: |
|
323 | 323 | ui.status(_('moving %s to %s\n') % (relsrc, reltarget)) |
|
324 | 324 | else: |
|
325 | 325 | ui.status(_('copying %s to %s\n') % (relsrc, reltarget)) |
|
326 | 326 | |
|
327 | 327 | targets[abstarget] = abssrc |
|
328 | 328 | |
|
329 | 329 | # fix up dirstate |
|
330 | 330 | scmutil.dirstatecopy(ui, repo, wctx, abssrc, abstarget, |
|
331 | 331 | dryrun=dryrun, cwd=cwd) |
|
332 | 332 | if rename and not dryrun: |
|
333 | 333 | if not after and srcexists: |
|
334 | 334 | util.unlinkpath(repo.wjoin(abssrc)) |
|
335 | 335 | wctx.forget([abssrc]) |
|
336 | 336 | |
|
337 | 337 | # pat: ossep |
|
338 | 338 | # dest ossep |
|
339 | 339 | # srcs: list of (hgsep, hgsep, ossep, bool) |
|
340 | 340 | # return: function that takes hgsep and returns ossep |
|
341 | 341 | def targetpathfn(pat, dest, srcs): |
|
342 | 342 | if os.path.isdir(pat): |
|
343 | 343 | abspfx = scmutil.canonpath(repo.root, cwd, pat) |
|
344 | 344 | abspfx = util.localpath(abspfx) |
|
345 | 345 | if destdirexists: |
|
346 | 346 | striplen = len(os.path.split(abspfx)[0]) |
|
347 | 347 | else: |
|
348 | 348 | striplen = len(abspfx) |
|
349 | 349 | if striplen: |
|
350 | 350 | striplen += len(os.sep) |
|
351 | 351 | res = lambda p: os.path.join(dest, util.localpath(p)[striplen:]) |
|
352 | 352 | elif destdirexists: |
|
353 | 353 | res = lambda p: os.path.join(dest, |
|
354 | 354 | os.path.basename(util.localpath(p))) |
|
355 | 355 | else: |
|
356 | 356 | res = lambda p: dest |
|
357 | 357 | return res |
|
358 | 358 | |
|
359 | 359 | # pat: ossep |
|
360 | 360 | # dest ossep |
|
361 | 361 | # srcs: list of (hgsep, hgsep, ossep, bool) |
|
362 | 362 | # return: function that takes hgsep and returns ossep |
|
363 | 363 | def targetpathafterfn(pat, dest, srcs): |
|
364 | 364 | if matchmod.patkind(pat): |
|
365 | 365 | # a mercurial pattern |
|
366 | 366 | res = lambda p: os.path.join(dest, |
|
367 | 367 | os.path.basename(util.localpath(p))) |
|
368 | 368 | else: |
|
369 | 369 | abspfx = scmutil.canonpath(repo.root, cwd, pat) |
|
370 | 370 | if len(abspfx) < len(srcs[0][0]): |
|
371 | 371 | # A directory. Either the target path contains the last |
|
372 | 372 | # component of the source path or it does not. |
|
373 | 373 | def evalpath(striplen): |
|
374 | 374 | score = 0 |
|
375 | 375 | for s in srcs: |
|
376 | 376 | t = os.path.join(dest, util.localpath(s[0])[striplen:]) |
|
377 | 377 | if os.path.lexists(t): |
|
378 | 378 | score += 1 |
|
379 | 379 | return score |
|
380 | 380 | |
|
381 | 381 | abspfx = util.localpath(abspfx) |
|
382 | 382 | striplen = len(abspfx) |
|
383 | 383 | if striplen: |
|
384 | 384 | striplen += len(os.sep) |
|
385 | 385 | if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])): |
|
386 | 386 | score = evalpath(striplen) |
|
387 | 387 | striplen1 = len(os.path.split(abspfx)[0]) |
|
388 | 388 | if striplen1: |
|
389 | 389 | striplen1 += len(os.sep) |
|
390 | 390 | if evalpath(striplen1) > score: |
|
391 | 391 | striplen = striplen1 |
|
392 | 392 | res = lambda p: os.path.join(dest, |
|
393 | 393 | util.localpath(p)[striplen:]) |
|
394 | 394 | else: |
|
395 | 395 | # a file |
|
396 | 396 | if destdirexists: |
|
397 | 397 | res = lambda p: os.path.join(dest, |
|
398 | 398 | os.path.basename(util.localpath(p))) |
|
399 | 399 | else: |
|
400 | 400 | res = lambda p: dest |
|
401 | 401 | return res |
|
402 | 402 | |
|
403 | 403 | |
|
404 | 404 | pats = scmutil.expandpats(pats) |
|
405 | 405 | if not pats: |
|
406 | 406 | raise util.Abort(_('no source or destination specified')) |
|
407 | 407 | if len(pats) == 1: |
|
408 | 408 | raise util.Abort(_('no destination specified')) |
|
409 | 409 | dest = pats.pop() |
|
410 | 410 | destdirexists = os.path.isdir(dest) and not os.path.islink(dest) |
|
411 | 411 | if not destdirexists: |
|
412 | 412 | if len(pats) > 1 or matchmod.patkind(pats[0]): |
|
413 | 413 | raise util.Abort(_('with multiple sources, destination must be an ' |
|
414 | 414 | 'existing directory')) |
|
415 | 415 | if util.endswithsep(dest): |
|
416 | 416 | raise util.Abort(_('destination %s is not a directory') % dest) |
|
417 | 417 | |
|
418 | 418 | tfn = targetpathfn |
|
419 | 419 | if after: |
|
420 | 420 | tfn = targetpathafterfn |
|
421 | 421 | copylist = [] |
|
422 | 422 | for pat in pats: |
|
423 | 423 | srcs = walkpat(pat) |
|
424 | 424 | if not srcs: |
|
425 | 425 | continue |
|
426 | 426 | copylist.append((tfn(pat, dest, srcs), srcs)) |
|
427 | 427 | if not copylist: |
|
428 | 428 | raise util.Abort(_('no files to copy')) |
|
429 | 429 | |
|
430 | 430 | errors = 0 |
|
431 | 431 | for targetpath, srcs in copylist: |
|
432 | 432 | for abssrc, relsrc, exact in srcs: |
|
433 | 433 | if copyfile(abssrc, relsrc, targetpath(abssrc), exact): |
|
434 | 434 | errors += 1 |
|
435 | 435 | |
|
436 | 436 | if errors: |
|
437 | 437 | ui.warn(_('(consider using --after)\n')) |
|
438 | 438 | |
|
439 | 439 | return errors != 0 |
|
440 | 440 | |
|
441 | 441 | def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None, |
|
442 | 442 | runargs=None, appendpid=False): |
|
443 | 443 | '''Run a command as a service.''' |
|
444 | 444 | |
|
445 | 445 | if opts['daemon'] and not opts['daemon_pipefds']: |
|
446 | 446 | # Signal child process startup with file removal |
|
447 | 447 | lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-') |
|
448 | 448 | os.close(lockfd) |
|
449 | 449 | try: |
|
450 | 450 | if not runargs: |
|
451 | 451 | runargs = util.hgcmd() + sys.argv[1:] |
|
452 | 452 | runargs.append('--daemon-pipefds=%s' % lockpath) |
|
453 | 453 | # Don't pass --cwd to the child process, because we've already |
|
454 | 454 | # changed directory. |
|
455 | 455 | for i in xrange(1, len(runargs)): |
|
456 | 456 | if runargs[i].startswith('--cwd='): |
|
457 | 457 | del runargs[i] |
|
458 | 458 | break |
|
459 | 459 | elif runargs[i].startswith('--cwd'): |
|
460 | 460 | del runargs[i:i + 2] |
|
461 | 461 | break |
|
462 | 462 | def condfn(): |
|
463 | 463 | return not os.path.exists(lockpath) |
|
464 | 464 | pid = util.rundetached(runargs, condfn) |
|
465 | 465 | if pid < 0: |
|
466 | 466 | raise util.Abort(_('child process failed to start')) |
|
467 | 467 | finally: |
|
468 | 468 | try: |
|
469 | 469 | os.unlink(lockpath) |
|
470 | 470 | except OSError, e: |
|
471 | 471 | if e.errno != errno.ENOENT: |
|
472 | 472 | raise |
|
473 | 473 | if parentfn: |
|
474 | 474 | return parentfn(pid) |
|
475 | 475 | else: |
|
476 | 476 | return |
|
477 | 477 | |
|
478 | 478 | if initfn: |
|
479 | 479 | initfn() |
|
480 | 480 | |
|
481 | 481 | if opts['pid_file']: |
|
482 | 482 | mode = appendpid and 'a' or 'w' |
|
483 | 483 | fp = open(opts['pid_file'], mode) |
|
484 | 484 | fp.write(str(os.getpid()) + '\n') |
|
485 | 485 | fp.close() |
|
486 | 486 | |
|
487 | 487 | if opts['daemon_pipefds']: |
|
488 | 488 | lockpath = opts['daemon_pipefds'] |
|
489 | 489 | try: |
|
490 | 490 | os.setsid() |
|
491 | 491 | except AttributeError: |
|
492 | 492 | pass |
|
493 | 493 | os.unlink(lockpath) |
|
494 | 494 | util.hidewindow() |
|
495 | 495 | sys.stdout.flush() |
|
496 | 496 | sys.stderr.flush() |
|
497 | 497 | |
|
498 | 498 | nullfd = os.open(util.nulldev, os.O_RDWR) |
|
499 | 499 | logfilefd = nullfd |
|
500 | 500 | if logfile: |
|
501 | 501 | logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND) |
|
502 | 502 | os.dup2(nullfd, 0) |
|
503 | 503 | os.dup2(logfilefd, 1) |
|
504 | 504 | os.dup2(logfilefd, 2) |
|
505 | 505 | if nullfd not in (0, 1, 2): |
|
506 | 506 | os.close(nullfd) |
|
507 | 507 | if logfile and logfilefd not in (0, 1, 2): |
|
508 | 508 | os.close(logfilefd) |
|
509 | 509 | |
|
510 | 510 | if runfn: |
|
511 | 511 | return runfn() |
|
512 | 512 | |
|
513 | 513 | def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False, |
|
514 | 514 | opts=None): |
|
515 | 515 | '''export changesets as hg patches.''' |
|
516 | 516 | |
|
517 | 517 | total = len(revs) |
|
518 | 518 | revwidth = max([len(str(rev)) for rev in revs]) |
|
519 | 519 | |
|
520 | 520 | def single(rev, seqno, fp): |
|
521 | 521 | ctx = repo[rev] |
|
522 | 522 | node = ctx.node() |
|
523 | 523 | parents = [p.node() for p in ctx.parents() if p] |
|
524 | 524 | branch = ctx.branch() |
|
525 | 525 | if switch_parent: |
|
526 | 526 | parents.reverse() |
|
527 | 527 | prev = (parents and parents[0]) or nullid |
|
528 | 528 | |
|
529 | 529 | shouldclose = False |
|
530 | 530 | if not fp: |
|
531 | 531 | desc_lines = ctx.description().rstrip().split('\n') |
|
532 | 532 | desc = desc_lines[0] #Commit always has a first line. |
|
533 | 533 | fp = makefileobj(repo, template, node, desc=desc, total=total, |
|
534 | 534 | seqno=seqno, revwidth=revwidth, mode='ab') |
|
535 | 535 | if fp != template: |
|
536 | 536 | shouldclose = True |
|
537 | 537 | if fp != sys.stdout and util.safehasattr(fp, 'name'): |
|
538 | 538 | repo.ui.note("%s\n" % fp.name) |
|
539 | 539 | |
|
540 | 540 | fp.write("# HG changeset patch\n") |
|
541 | 541 | fp.write("# User %s\n" % ctx.user()) |
|
542 | 542 | fp.write("# Date %d %d\n" % ctx.date()) |
|
543 | 543 | if branch and branch != 'default': |
|
544 | 544 | fp.write("# Branch %s\n" % branch) |
|
545 | 545 | fp.write("# Node ID %s\n" % hex(node)) |
|
546 | 546 | fp.write("# Parent %s\n" % hex(prev)) |
|
547 | 547 | if len(parents) > 1: |
|
548 | 548 | fp.write("# Parent %s\n" % hex(parents[1])) |
|
549 | 549 | fp.write(ctx.description().rstrip()) |
|
550 | 550 | fp.write("\n\n") |
|
551 | 551 | |
|
552 | 552 | for chunk in patch.diff(repo, prev, node, opts=opts): |
|
553 | 553 | fp.write(chunk) |
|
554 | 554 | |
|
555 | 555 | if shouldclose: |
|
556 | 556 | fp.close() |
|
557 | 557 | |
|
558 | 558 | for seqno, rev in enumerate(revs): |
|
559 | 559 | single(rev, seqno + 1, fp) |
|
560 | 560 | |
|
561 | 561 | def diffordiffstat(ui, repo, diffopts, node1, node2, match, |
|
562 | 562 | changes=None, stat=False, fp=None, prefix='', |
|
563 | 563 | listsubrepos=False): |
|
564 | 564 | '''show diff or diffstat.''' |
|
565 | 565 | if fp is None: |
|
566 | 566 | write = ui.write |
|
567 | 567 | else: |
|
568 | 568 | def write(s, **kw): |
|
569 | 569 | fp.write(s) |
|
570 | 570 | |
|
571 | 571 | if stat: |
|
572 | 572 | diffopts = diffopts.copy(context=0) |
|
573 | 573 | width = 80 |
|
574 | 574 | if not ui.plain(): |
|
575 | 575 | width = ui.termwidth() |
|
576 | 576 | chunks = patch.diff(repo, node1, node2, match, changes, diffopts, |
|
577 | 577 | prefix=prefix) |
|
578 | 578 | for chunk, label in patch.diffstatui(util.iterlines(chunks), |
|
579 | 579 | width=width, |
|
580 | 580 | git=diffopts.git): |
|
581 | 581 | write(chunk, label=label) |
|
582 | 582 | else: |
|
583 | 583 | for chunk, label in patch.diffui(repo, node1, node2, match, |
|
584 | 584 | changes, diffopts, prefix=prefix): |
|
585 | 585 | write(chunk, label=label) |
|
586 | 586 | |
|
587 | 587 | if listsubrepos: |
|
588 | 588 | ctx1 = repo[node1] |
|
589 | 589 | ctx2 = repo[node2] |
|
590 | 590 | for subpath, sub in subrepo.itersubrepos(ctx1, ctx2): |
|
591 | 591 | tempnode2 = node2 |
|
592 | 592 | try: |
|
593 | 593 | if node2 is not None: |
|
594 | 594 | tempnode2 = ctx2.substate[subpath][1] |
|
595 | 595 | except KeyError: |
|
596 | 596 | # A subrepo that existed in node1 was deleted between node1 and |
|
597 | 597 | # node2 (inclusive). Thus, ctx2's substate won't contain that |
|
598 | 598 | # subpath. The best we can do is to ignore it. |
|
599 | 599 | tempnode2 = None |
|
600 | 600 | submatch = matchmod.narrowmatcher(subpath, match) |
|
601 | 601 | sub.diff(diffopts, tempnode2, submatch, changes=changes, |
|
602 | 602 | stat=stat, fp=fp, prefix=prefix) |
|
603 | 603 | |
|
604 | 604 | class changeset_printer(object): |
|
605 | 605 | '''show changeset information when templating not requested.''' |
|
606 | 606 | |
|
607 | 607 | def __init__(self, ui, repo, patch, diffopts, buffered): |
|
608 | 608 | self.ui = ui |
|
609 | 609 | self.repo = repo |
|
610 | 610 | self.buffered = buffered |
|
611 | 611 | self.patch = patch |
|
612 | 612 | self.diffopts = diffopts |
|
613 | 613 | self.header = {} |
|
614 | 614 | self.hunk = {} |
|
615 | 615 | self.lastheader = None |
|
616 | 616 | self.footer = None |
|
617 | 617 | |
|
618 | 618 | def flush(self, rev): |
|
619 | 619 | if rev in self.header: |
|
620 | 620 | h = self.header[rev] |
|
621 | 621 | if h != self.lastheader: |
|
622 | 622 | self.lastheader = h |
|
623 | 623 | self.ui.write(h) |
|
624 | 624 | del self.header[rev] |
|
625 | 625 | if rev in self.hunk: |
|
626 | 626 | self.ui.write(self.hunk[rev]) |
|
627 | 627 | del self.hunk[rev] |
|
628 | 628 | return 1 |
|
629 | 629 | return 0 |
|
630 | 630 | |
|
631 | 631 | def close(self): |
|
632 | 632 | if self.footer: |
|
633 | 633 | self.ui.write(self.footer) |
|
634 | 634 | |
|
635 | 635 | def show(self, ctx, copies=None, matchfn=None, **props): |
|
636 | 636 | if self.buffered: |
|
637 | 637 | self.ui.pushbuffer() |
|
638 | 638 | self._show(ctx, copies, matchfn, props) |
|
639 | 639 | self.hunk[ctx.rev()] = self.ui.popbuffer(labeled=True) |
|
640 | 640 | else: |
|
641 | 641 | self._show(ctx, copies, matchfn, props) |
|
642 | 642 | |
|
643 | 643 | def _show(self, ctx, copies, matchfn, props): |
|
644 | 644 | '''show a single changeset or file revision''' |
|
645 | 645 | changenode = ctx.node() |
|
646 | 646 | rev = ctx.rev() |
|
647 | 647 | |
|
648 | 648 | if self.ui.quiet: |
|
649 | 649 | self.ui.write("%d:%s\n" % (rev, short(changenode)), |
|
650 | 650 | label='log.node') |
|
651 | 651 | return |
|
652 | 652 | |
|
653 | 653 | log = self.repo.changelog |
|
654 | 654 | date = util.datestr(ctx.date()) |
|
655 | 655 | |
|
656 | 656 | hexfunc = self.ui.debugflag and hex or short |
|
657 | 657 | |
|
658 | 658 | parents = [(p, hexfunc(log.node(p))) |
|
659 | 659 | for p in self._meaningful_parentrevs(log, rev)] |
|
660 | 660 | |
|
661 | 661 | self.ui.write(_("changeset: %d:%s\n") % (rev, hexfunc(changenode)), |
|
662 | 662 | label='log.changeset') |
|
663 | 663 | |
|
664 | 664 | branch = ctx.branch() |
|
665 | 665 | # don't show the default branch name |
|
666 | 666 | if branch != 'default': |
|
667 | 667 | self.ui.write(_("branch: %s\n") % branch, |
|
668 | 668 | label='log.branch') |
|
669 | 669 | for bookmark in self.repo.nodebookmarks(changenode): |
|
670 | 670 | self.ui.write(_("bookmark: %s\n") % bookmark, |
|
671 | 671 | label='log.bookmark') |
|
672 | 672 | for tag in self.repo.nodetags(changenode): |
|
673 | 673 | self.ui.write(_("tag: %s\n") % tag, |
|
674 | 674 | label='log.tag') |
|
675 | 675 | for parent in parents: |
|
676 | 676 | self.ui.write(_("parent: %d:%s\n") % parent, |
|
677 | 677 | label='log.parent') |
|
678 | 678 | |
|
679 | 679 | if self.ui.debugflag: |
|
680 | 680 | mnode = ctx.manifestnode() |
|
681 | 681 | self.ui.write(_("manifest: %d:%s\n") % |
|
682 | 682 | (self.repo.manifest.rev(mnode), hex(mnode)), |
|
683 | 683 | label='ui.debug log.manifest') |
|
684 | 684 | self.ui.write(_("user: %s\n") % ctx.user(), |
|
685 | 685 | label='log.user') |
|
686 | 686 | self.ui.write(_("date: %s\n") % date, |
|
687 | 687 | label='log.date') |
|
688 | 688 | |
|
689 | 689 | if self.ui.debugflag: |
|
690 | 690 | files = self.repo.status(log.parents(changenode)[0], changenode)[:3] |
|
691 | 691 | for key, value in zip([_("files:"), _("files+:"), _("files-:")], |
|
692 | 692 | files): |
|
693 | 693 | if value: |
|
694 | 694 | self.ui.write("%-12s %s\n" % (key, " ".join(value)), |
|
695 | 695 | label='ui.debug log.files') |
|
696 | 696 | elif ctx.files() and self.ui.verbose: |
|
697 | 697 | self.ui.write(_("files: %s\n") % " ".join(ctx.files()), |
|
698 | 698 | label='ui.note log.files') |
|
699 | 699 | if copies and self.ui.verbose: |
|
700 | 700 | copies = ['%s (%s)' % c for c in copies] |
|
701 | 701 | self.ui.write(_("copies: %s\n") % ' '.join(copies), |
|
702 | 702 | label='ui.note log.copies') |
|
703 | 703 | |
|
704 | 704 | extra = ctx.extra() |
|
705 | 705 | if extra and self.ui.debugflag: |
|
706 | 706 | for key, value in sorted(extra.items()): |
|
707 | 707 | self.ui.write(_("extra: %s=%s\n") |
|
708 | 708 | % (key, value.encode('string_escape')), |
|
709 | 709 | label='ui.debug log.extra') |
|
710 | 710 | |
|
711 | 711 | description = ctx.description().strip() |
|
712 | 712 | if description: |
|
713 | 713 | if self.ui.verbose: |
|
714 | 714 | self.ui.write(_("description:\n"), |
|
715 | 715 | label='ui.note log.description') |
|
716 | 716 | self.ui.write(description, |
|
717 | 717 | label='ui.note log.description') |
|
718 | 718 | self.ui.write("\n\n") |
|
719 | 719 | else: |
|
720 | 720 | self.ui.write(_("summary: %s\n") % |
|
721 | 721 | description.splitlines()[0], |
|
722 | 722 | label='log.summary') |
|
723 | 723 | self.ui.write("\n") |
|
724 | 724 | |
|
725 | 725 | self.showpatch(changenode, matchfn) |
|
726 | 726 | |
|
727 | 727 | def showpatch(self, node, matchfn): |
|
728 | 728 | if not matchfn: |
|
729 | 729 | matchfn = self.patch |
|
730 | 730 | if matchfn: |
|
731 | 731 | stat = self.diffopts.get('stat') |
|
732 | 732 | diff = self.diffopts.get('patch') |
|
733 | 733 | diffopts = patch.diffopts(self.ui, self.diffopts) |
|
734 | 734 | prev = self.repo.changelog.parents(node)[0] |
|
735 | 735 | if stat: |
|
736 | 736 | diffordiffstat(self.ui, self.repo, diffopts, prev, node, |
|
737 | 737 | match=matchfn, stat=True) |
|
738 | 738 | if diff: |
|
739 | 739 | if stat: |
|
740 | 740 | self.ui.write("\n") |
|
741 | 741 | diffordiffstat(self.ui, self.repo, diffopts, prev, node, |
|
742 | 742 | match=matchfn, stat=False) |
|
743 | 743 | self.ui.write("\n") |
|
744 | 744 | |
|
745 | 745 | def _meaningful_parentrevs(self, log, rev): |
|
746 | 746 | """Return list of meaningful (or all if debug) parentrevs for rev. |
|
747 | 747 | |
|
748 | 748 | For merges (two non-nullrev revisions) both parents are meaningful. |
|
749 | 749 | Otherwise the first parent revision is considered meaningful if it |
|
750 | 750 | is not the preceding revision. |
|
751 | 751 | """ |
|
752 | 752 | parents = log.parentrevs(rev) |
|
753 | 753 | if not self.ui.debugflag and parents[1] == nullrev: |
|
754 | 754 | if parents[0] >= rev - 1: |
|
755 | 755 | parents = [] |
|
756 | 756 | else: |
|
757 | 757 | parents = [parents[0]] |
|
758 | 758 | return parents |
|
759 | 759 | |
|
760 | 760 | |
|
761 | 761 | class changeset_templater(changeset_printer): |
|
762 | 762 | '''format changeset information.''' |
|
763 | 763 | |
|
764 | 764 | def __init__(self, ui, repo, patch, diffopts, mapfile, buffered): |
|
765 | 765 | changeset_printer.__init__(self, ui, repo, patch, diffopts, buffered) |
|
766 | 766 | formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12]) |
|
767 | 767 | defaulttempl = { |
|
768 | 768 | 'parent': '{rev}:{node|formatnode} ', |
|
769 | 769 | 'manifest': '{rev}:{node|formatnode}', |
|
770 | 770 | 'file_copy': '{name} ({source})', |
|
771 | 771 | 'extra': '{key}={value|stringescape}' |
|
772 | 772 | } |
|
773 | 773 | # filecopy is preserved for compatibility reasons |
|
774 | 774 | defaulttempl['filecopy'] = defaulttempl['file_copy'] |
|
775 | 775 | self.t = templater.templater(mapfile, {'formatnode': formatnode}, |
|
776 | 776 | cache=defaulttempl) |
|
777 | 777 | self.cache = {} |
|
778 | 778 | |
|
779 | 779 | def use_template(self, t): |
|
780 | 780 | '''set template string to use''' |
|
781 | 781 | self.t.cache['changeset'] = t |
|
782 | 782 | |
|
783 | 783 | def _meaningful_parentrevs(self, ctx): |
|
784 | 784 | """Return list of meaningful (or all if debug) parentrevs for rev. |
|
785 | 785 | """ |
|
786 | 786 | parents = ctx.parents() |
|
787 | 787 | if len(parents) > 1: |
|
788 | 788 | return parents |
|
789 | 789 | if self.ui.debugflag: |
|
790 | 790 | return [parents[0], self.repo['null']] |
|
791 | 791 | if parents[0].rev() >= ctx.rev() - 1: |
|
792 | 792 | return [] |
|
793 | 793 | return parents |
|
794 | 794 | |
|
795 | 795 | def _show(self, ctx, copies, matchfn, props): |
|
796 | 796 | '''show a single changeset or file revision''' |
|
797 | 797 | |
|
798 | 798 | showlist = templatekw.showlist |
|
799 | 799 | |
|
800 | 800 | # showparents() behaviour depends on ui trace level which |
|
801 | 801 | # causes unexpected behaviours at templating level and makes |
|
802 | 802 | # it harder to extract it in a standalone function. Its |
|
803 | 803 | # behaviour cannot be changed so leave it here for now. |
|
804 | 804 | def showparents(**args): |
|
805 | 805 | ctx = args['ctx'] |
|
806 | 806 | parents = [[('rev', p.rev()), ('node', p.hex())] |
|
807 | 807 | for p in self._meaningful_parentrevs(ctx)] |
|
808 | 808 | return showlist('parent', parents, **args) |
|
809 | 809 | |
|
810 | 810 | props = props.copy() |
|
811 | 811 | props.update(templatekw.keywords) |
|
812 | 812 | props['parents'] = showparents |
|
813 | 813 | props['templ'] = self.t |
|
814 | 814 | props['ctx'] = ctx |
|
815 | 815 | props['repo'] = self.repo |
|
816 | 816 | props['revcache'] = {'copies': copies} |
|
817 | 817 | props['cache'] = self.cache |
|
818 | 818 | |
|
819 | 819 | # find correct templates for current mode |
|
820 | 820 | |
|
821 | 821 | tmplmodes = [ |
|
822 | 822 | (True, None), |
|
823 | 823 | (self.ui.verbose, 'verbose'), |
|
824 | 824 | (self.ui.quiet, 'quiet'), |
|
825 | 825 | (self.ui.debugflag, 'debug'), |
|
826 | 826 | ] |
|
827 | 827 | |
|
828 | 828 | types = {'header': '', 'footer':'', 'changeset': 'changeset'} |
|
829 | 829 | for mode, postfix in tmplmodes: |
|
830 | 830 | for type in types: |
|
831 | 831 | cur = postfix and ('%s_%s' % (type, postfix)) or type |
|
832 | 832 | if mode and cur in self.t: |
|
833 | 833 | types[type] = cur |
|
834 | 834 | |
|
835 | 835 | try: |
|
836 | 836 | |
|
837 | 837 | # write header |
|
838 | 838 | if types['header']: |
|
839 | 839 | h = templater.stringify(self.t(types['header'], **props)) |
|
840 | 840 | if self.buffered: |
|
841 | 841 | self.header[ctx.rev()] = h |
|
842 | 842 | else: |
|
843 | 843 | if self.lastheader != h: |
|
844 | 844 | self.lastheader = h |
|
845 | 845 | self.ui.write(h) |
|
846 | 846 | |
|
847 | 847 | # write changeset metadata, then patch if requested |
|
848 | 848 | key = types['changeset'] |
|
849 | 849 | self.ui.write(templater.stringify(self.t(key, **props))) |
|
850 | 850 | self.showpatch(ctx.node(), matchfn) |
|
851 | 851 | |
|
852 | 852 | if types['footer']: |
|
853 | 853 | if not self.footer: |
|
854 | 854 | self.footer = templater.stringify(self.t(types['footer'], |
|
855 | 855 | **props)) |
|
856 | 856 | |
|
857 | 857 | except KeyError, inst: |
|
858 | 858 | msg = _("%s: no key named '%s'") |
|
859 | 859 | raise util.Abort(msg % (self.t.mapfile, inst.args[0])) |
|
860 | 860 | except SyntaxError, inst: |
|
861 | 861 | raise util.Abort('%s: %s' % (self.t.mapfile, inst.args[0])) |
|
862 | 862 | |
|
863 | 863 | def show_changeset(ui, repo, opts, buffered=False): |
|
864 | 864 | """show one changeset using template or regular display. |
|
865 | 865 | |
|
866 | 866 | Display format will be the first non-empty hit of: |
|
867 | 867 | 1. option 'template' |
|
868 | 868 | 2. option 'style' |
|
869 | 869 | 3. [ui] setting 'logtemplate' |
|
870 | 870 | 4. [ui] setting 'style' |
|
871 | 871 | If all of these values are either the unset or the empty string, |
|
872 | 872 | regular display via changeset_printer() is done. |
|
873 | 873 | """ |
|
874 | 874 | # options |
|
875 | 875 | patch = False |
|
876 | 876 | if opts.get('patch') or opts.get('stat'): |
|
877 | 877 | patch = scmutil.matchall(repo) |
|
878 | 878 | |
|
879 | 879 | tmpl = opts.get('template') |
|
880 | 880 | style = None |
|
881 | 881 | if tmpl: |
|
882 | 882 | tmpl = templater.parsestring(tmpl, quoted=False) |
|
883 | 883 | else: |
|
884 | 884 | style = opts.get('style') |
|
885 | 885 | |
|
886 | 886 | # ui settings |
|
887 | 887 | if not (tmpl or style): |
|
888 | 888 | tmpl = ui.config('ui', 'logtemplate') |
|
889 | 889 | if tmpl: |
|
890 | 890 | tmpl = templater.parsestring(tmpl) |
|
891 | 891 | else: |
|
892 | 892 | style = util.expandpath(ui.config('ui', 'style', '')) |
|
893 | 893 | |
|
894 | 894 | if not (tmpl or style): |
|
895 | 895 | return changeset_printer(ui, repo, patch, opts, buffered) |
|
896 | 896 | |
|
897 | 897 | mapfile = None |
|
898 | 898 | if style and not tmpl: |
|
899 | 899 | mapfile = style |
|
900 | 900 | if not os.path.split(mapfile)[0]: |
|
901 | 901 | mapname = (templater.templatepath('map-cmdline.' + mapfile) |
|
902 | 902 | or templater.templatepath(mapfile)) |
|
903 | 903 | if mapname: |
|
904 | 904 | mapfile = mapname |
|
905 | 905 | |
|
906 | 906 | try: |
|
907 | 907 | t = changeset_templater(ui, repo, patch, opts, mapfile, buffered) |
|
908 | 908 | except SyntaxError, inst: |
|
909 | 909 | raise util.Abort(inst.args[0]) |
|
910 | 910 | if tmpl: |
|
911 | 911 | t.use_template(tmpl) |
|
912 | 912 | return t |
|
913 | 913 | |
|
914 | 914 | def finddate(ui, repo, date): |
|
915 | 915 | """Find the tipmost changeset that matches the given date spec""" |
|
916 | 916 | |
|
917 | 917 | df = util.matchdate(date) |
|
918 | 918 | m = scmutil.matchall(repo) |
|
919 | 919 | results = {} |
|
920 | 920 | |
|
921 | 921 | def prep(ctx, fns): |
|
922 | 922 | d = ctx.date() |
|
923 | 923 | if df(d[0]): |
|
924 | 924 | results[ctx.rev()] = d |
|
925 | 925 | |
|
926 | 926 | for ctx in walkchangerevs(repo, m, {'rev': None}, prep): |
|
927 | 927 | rev = ctx.rev() |
|
928 | 928 | if rev in results: |
|
929 | 929 | ui.status(_("Found revision %s from %s\n") % |
|
930 | 930 | (rev, util.datestr(results[rev]))) |
|
931 | 931 | return str(rev) |
|
932 | 932 | |
|
933 | 933 | raise util.Abort(_("revision matching date not found")) |
|
934 | 934 | |
|
935 | 935 | def walkchangerevs(repo, match, opts, prepare): |
|
936 | 936 | '''Iterate over files and the revs in which they changed. |
|
937 | 937 | |
|
938 | 938 | Callers most commonly need to iterate backwards over the history |
|
939 | 939 | in which they are interested. Doing so has awful (quadratic-looking) |
|
940 | 940 | performance, so we use iterators in a "windowed" way. |
|
941 | 941 | |
|
942 | 942 | We walk a window of revisions in the desired order. Within the |
|
943 | 943 | window, we first walk forwards to gather data, then in the desired |
|
944 | 944 | order (usually backwards) to display it. |
|
945 | 945 | |
|
946 | 946 | This function returns an iterator yielding contexts. Before |
|
947 | 947 | yielding each context, the iterator will first call the prepare |
|
948 | 948 | function on each context in the window in forward order.''' |
|
949 | 949 | |
|
950 | 950 | def increasing_windows(start, end, windowsize=8, sizelimit=512): |
|
951 | 951 | if start < end: |
|
952 | 952 | while start < end: |
|
953 | 953 | yield start, min(windowsize, end - start) |
|
954 | 954 | start += windowsize |
|
955 | 955 | if windowsize < sizelimit: |
|
956 | 956 | windowsize *= 2 |
|
957 | 957 | else: |
|
958 | 958 | while start > end: |
|
959 | 959 | yield start, min(windowsize, start - end - 1) |
|
960 | 960 | start -= windowsize |
|
961 | 961 | if windowsize < sizelimit: |
|
962 | 962 | windowsize *= 2 |
|
963 | 963 | |
|
964 | 964 | follow = opts.get('follow') or opts.get('follow_first') |
|
965 | 965 | |
|
966 | 966 | if not len(repo): |
|
967 | 967 | return [] |
|
968 | 968 | |
|
969 | 969 | if follow: |
|
970 | 970 | defrange = '%s:0' % repo['.'].rev() |
|
971 | 971 | else: |
|
972 | 972 | defrange = '-1:0' |
|
973 | 973 | revs = scmutil.revrange(repo, opts['rev'] or [defrange]) |
|
974 | 974 | if not revs: |
|
975 | 975 | return [] |
|
976 | 976 | wanted = set() |
|
977 | 977 | slowpath = match.anypats() or (match.files() and opts.get('removed')) |
|
978 | 978 | fncache = {} |
|
979 | 979 | change = util.cachefunc(repo.changectx) |
|
980 | 980 | |
|
981 | 981 | # First step is to fill wanted, the set of revisions that we want to yield. |
|
982 | 982 | # When it does not induce extra cost, we also fill fncache for revisions in |
|
983 | 983 | # wanted: a cache of filenames that were changed (ctx.files()) and that |
|
984 | 984 | # match the file filtering conditions. |
|
985 | 985 | |
|
986 | 986 | if not slowpath and not match.files(): |
|
987 | 987 | # No files, no patterns. Display all revs. |
|
988 | 988 | wanted = set(revs) |
|
989 | 989 | copies = [] |
|
990 | 990 | |
|
991 | 991 | if not slowpath: |
|
992 | 992 | # We only have to read through the filelog to find wanted revisions |
|
993 | 993 | |
|
994 | 994 | minrev, maxrev = min(revs), max(revs) |
|
995 | 995 | def filerevgen(filelog, last): |
|
996 | 996 | """ |
|
997 | 997 | Only files, no patterns. Check the history of each file. |
|
998 | 998 | |
|
999 | 999 | Examines filelog entries within minrev, maxrev linkrev range |
|
1000 | 1000 | Returns an iterator yielding (linkrev, parentlinkrevs, copied) |
|
1001 | 1001 | tuples in backwards order |
|
1002 | 1002 | """ |
|
1003 | 1003 | cl_count = len(repo) |
|
1004 | 1004 | revs = [] |
|
1005 | 1005 | for j in xrange(0, last + 1): |
|
1006 | 1006 | linkrev = filelog.linkrev(j) |
|
1007 | 1007 | if linkrev < minrev: |
|
1008 | 1008 | continue |
|
1009 | 1009 | # only yield rev for which we have the changelog, it can |
|
1010 | 1010 | # happen while doing "hg log" during a pull or commit |
|
1011 | 1011 | if linkrev >= cl_count: |
|
1012 | 1012 | break |
|
1013 | 1013 | |
|
1014 | 1014 | parentlinkrevs = [] |
|
1015 | 1015 | for p in filelog.parentrevs(j): |
|
1016 | 1016 | if p != nullrev: |
|
1017 | 1017 | parentlinkrevs.append(filelog.linkrev(p)) |
|
1018 | 1018 | n = filelog.node(j) |
|
1019 | 1019 | revs.append((linkrev, parentlinkrevs, |
|
1020 | 1020 | follow and filelog.renamed(n))) |
|
1021 | 1021 | |
|
1022 | 1022 | return reversed(revs) |
|
1023 | 1023 | def iterfiles(): |
|
1024 | 1024 | for filename in match.files(): |
|
1025 | 1025 | yield filename, None |
|
1026 | 1026 | for filename_node in copies: |
|
1027 | 1027 | yield filename_node |
|
1028 | 1028 | for file_, node in iterfiles(): |
|
1029 | 1029 | filelog = repo.file(file_) |
|
1030 | 1030 | if not len(filelog): |
|
1031 | 1031 | if node is None: |
|
1032 | 1032 | # A zero count may be a directory or deleted file, so |
|
1033 | 1033 | # try to find matching entries on the slow path. |
|
1034 | 1034 | if follow: |
|
1035 | 1035 | raise util.Abort( |
|
1036 | 1036 | _('cannot follow nonexistent file: "%s"') % file_) |
|
1037 | 1037 | slowpath = True |
|
1038 | 1038 | break |
|
1039 | 1039 | else: |
|
1040 | 1040 | continue |
|
1041 | 1041 | |
|
1042 | 1042 | if node is None: |
|
1043 | 1043 | last = len(filelog) - 1 |
|
1044 | 1044 | else: |
|
1045 | 1045 | last = filelog.rev(node) |
|
1046 | 1046 | |
|
1047 | 1047 | |
|
1048 | 1048 | # keep track of all ancestors of the file |
|
1049 | 1049 | ancestors = set([filelog.linkrev(last)]) |
|
1050 | 1050 | |
|
1051 | 1051 | # iterate from latest to oldest revision |
|
1052 | 1052 | for rev, flparentlinkrevs, copied in filerevgen(filelog, last): |
|
1053 | 1053 | if not follow: |
|
1054 | 1054 | if rev > maxrev: |
|
1055 | 1055 | continue |
|
1056 | 1056 | else: |
|
1057 | 1057 | # Note that last might not be the first interesting |
|
1058 | 1058 | # rev to us: |
|
1059 | 1059 | # if the file has been changed after maxrev, we'll |
|
1060 | 1060 | # have linkrev(last) > maxrev, and we still need |
|
1061 | 1061 | # to explore the file graph |
|
1062 | 1062 | if rev not in ancestors: |
|
1063 | 1063 | continue |
|
1064 | 1064 | # XXX insert 1327 fix here |
|
1065 | 1065 | if flparentlinkrevs: |
|
1066 | 1066 | ancestors.update(flparentlinkrevs) |
|
1067 | 1067 | |
|
1068 | 1068 | fncache.setdefault(rev, []).append(file_) |
|
1069 | 1069 | wanted.add(rev) |
|
1070 | 1070 | if copied: |
|
1071 | 1071 | copies.append(copied) |
|
1072 | 1072 | if slowpath: |
|
1073 | 1073 | # We have to read the changelog to match filenames against |
|
1074 | 1074 | # changed files |
|
1075 | 1075 | |
|
1076 | 1076 | if follow: |
|
1077 | 1077 | raise util.Abort(_('can only follow copies/renames for explicit ' |
|
1078 | 1078 | 'filenames')) |
|
1079 | 1079 | |
|
1080 | 1080 | # The slow path checks files modified in every changeset. |
|
1081 | 1081 | for i in sorted(revs): |
|
1082 | 1082 | ctx = change(i) |
|
1083 | 1083 | matches = filter(match, ctx.files()) |
|
1084 | 1084 | if matches: |
|
1085 | 1085 | fncache[i] = matches |
|
1086 | 1086 | wanted.add(i) |
|
1087 | 1087 | |
|
1088 | 1088 | class followfilter(object): |
|
1089 | 1089 | def __init__(self, onlyfirst=False): |
|
1090 | 1090 | self.startrev = nullrev |
|
1091 | 1091 | self.roots = set() |
|
1092 | 1092 | self.onlyfirst = onlyfirst |
|
1093 | 1093 | |
|
1094 | 1094 | def match(self, rev): |
|
1095 | 1095 | def realparents(rev): |
|
1096 | 1096 | if self.onlyfirst: |
|
1097 | 1097 | return repo.changelog.parentrevs(rev)[0:1] |
|
1098 | 1098 | else: |
|
1099 | 1099 | return filter(lambda x: x != nullrev, |
|
1100 | 1100 | repo.changelog.parentrevs(rev)) |
|
1101 | 1101 | |
|
1102 | 1102 | if self.startrev == nullrev: |
|
1103 | 1103 | self.startrev = rev |
|
1104 | 1104 | return True |
|
1105 | 1105 | |
|
1106 | 1106 | if rev > self.startrev: |
|
1107 | 1107 | # forward: all descendants |
|
1108 | 1108 | if not self.roots: |
|
1109 | 1109 | self.roots.add(self.startrev) |
|
1110 | 1110 | for parent in realparents(rev): |
|
1111 | 1111 | if parent in self.roots: |
|
1112 | 1112 | self.roots.add(rev) |
|
1113 | 1113 | return True |
|
1114 | 1114 | else: |
|
1115 | 1115 | # backwards: all parents |
|
1116 | 1116 | if not self.roots: |
|
1117 | 1117 | self.roots.update(realparents(self.startrev)) |
|
1118 | 1118 | if rev in self.roots: |
|
1119 | 1119 | self.roots.remove(rev) |
|
1120 | 1120 | self.roots.update(realparents(rev)) |
|
1121 | 1121 | return True |
|
1122 | 1122 | |
|
1123 | 1123 | return False |
|
1124 | 1124 | |
|
1125 | 1125 | # it might be worthwhile to do this in the iterator if the rev range |
|
1126 | 1126 | # is descending and the prune args are all within that range |
|
1127 | 1127 | for rev in opts.get('prune', ()): |
|
1128 | 1128 | rev = repo.changelog.rev(repo.lookup(rev)) |
|
1129 | 1129 | ff = followfilter() |
|
1130 | 1130 | stop = min(revs[0], revs[-1]) |
|
1131 | 1131 | for x in xrange(rev, stop - 1, -1): |
|
1132 | 1132 | if ff.match(x): |
|
1133 | 1133 | wanted.discard(x) |
|
1134 | 1134 | |
|
1135 | 1135 | # Now that wanted is correctly initialized, we can iterate over the |
|
1136 | 1136 | # revision range, yielding only revisions in wanted. |
|
1137 | 1137 | def iterate(): |
|
1138 | 1138 | if follow and not match.files(): |
|
1139 | 1139 | ff = followfilter(onlyfirst=opts.get('follow_first')) |
|
1140 | 1140 | def want(rev): |
|
1141 | 1141 | return ff.match(rev) and rev in wanted |
|
1142 | 1142 | else: |
|
1143 | 1143 | def want(rev): |
|
1144 | 1144 | return rev in wanted |
|
1145 | 1145 | |
|
1146 | 1146 | for i, window in increasing_windows(0, len(revs)): |
|
1147 | 1147 | nrevs = [rev for rev in revs[i:i + window] if want(rev)] |
|
1148 | 1148 | for rev in sorted(nrevs): |
|
1149 | 1149 | fns = fncache.get(rev) |
|
1150 | 1150 | ctx = change(rev) |
|
1151 | 1151 | if not fns: |
|
1152 | 1152 | def fns_generator(): |
|
1153 | 1153 | for f in ctx.files(): |
|
1154 | 1154 | if match(f): |
|
1155 | 1155 | yield f |
|
1156 | 1156 | fns = fns_generator() |
|
1157 | 1157 | prepare(ctx, fns) |
|
1158 | 1158 | for rev in nrevs: |
|
1159 | 1159 | yield change(rev) |
|
1160 | 1160 | return iterate() |
|
1161 | 1161 | |
|
1162 | 1162 | def add(ui, repo, match, dryrun, listsubrepos, prefix): |
|
1163 | 1163 | join = lambda f: os.path.join(prefix, f) |
|
1164 | 1164 | bad = [] |
|
1165 | 1165 | oldbad = match.bad |
|
1166 | 1166 | match.bad = lambda x, y: bad.append(x) or oldbad(x, y) |
|
1167 | 1167 | names = [] |
|
1168 | 1168 | wctx = repo[None] |
|
1169 | 1169 | cca = None |
|
1170 | 1170 | abort, warn = scmutil.checkportabilityalert(ui) |
|
1171 | 1171 | if abort or warn: |
|
1172 | 1172 | cca = scmutil.casecollisionauditor(ui, abort, wctx) |
|
1173 | 1173 | for f in repo.walk(match): |
|
1174 | 1174 | exact = match.exact(f) |
|
1175 | 1175 | if exact or f not in repo.dirstate: |
|
1176 | 1176 | if cca: |
|
1177 | 1177 | cca(f) |
|
1178 | 1178 | names.append(f) |
|
1179 | 1179 | if ui.verbose or not exact: |
|
1180 | 1180 | ui.status(_('adding %s\n') % match.rel(join(f))) |
|
1181 | 1181 | |
|
1182 | 1182 | for subpath in wctx.substate: |
|
1183 | 1183 | sub = wctx.sub(subpath) |
|
1184 | 1184 | try: |
|
1185 | 1185 | submatch = matchmod.narrowmatcher(subpath, match) |
|
1186 | 1186 | if listsubrepos: |
|
1187 | 1187 | bad.extend(sub.add(ui, submatch, dryrun, prefix)) |
|
1188 | 1188 | else: |
|
1189 | 1189 | for f in sub.walk(submatch): |
|
1190 | 1190 | if submatch.exact(f): |
|
1191 | 1191 | bad.extend(sub.add(ui, submatch, dryrun, prefix)) |
|
1192 | 1192 | except error.LookupError: |
|
1193 | 1193 | ui.status(_("skipping missing subrepository: %s\n") |
|
1194 | 1194 | % join(subpath)) |
|
1195 | 1195 | |
|
1196 | 1196 | if not dryrun: |
|
1197 | 1197 | rejected = wctx.add(names, prefix) |
|
1198 | 1198 | bad.extend(f for f in rejected if f in match.files()) |
|
1199 | 1199 | return bad |
|
1200 | 1200 | |
|
1201 | 1201 | def duplicatecopies(repo, rev, p1, p2): |
|
1202 | 1202 | "Reproduce copies found in the source revision in the dirstate for grafts" |
|
1203 | 1203 | # Here we simulate the copies and renames in the source changeset |
|
1204 |
cop, diver = copies.copies(repo, repo[rev], repo[p1], repo[p2] |
|
|
1204 | cop, diver = copies.mergecopies(repo, repo[rev], repo[p1], repo[p2]) | |
|
1205 | 1205 | m1 = repo[rev].manifest() |
|
1206 | 1206 | m2 = repo[p1].manifest() |
|
1207 | 1207 | for k, v in cop.iteritems(): |
|
1208 | 1208 | if k in m1: |
|
1209 | 1209 | if v in m1 or v in m2: |
|
1210 | 1210 | repo.dirstate.copy(v, k) |
|
1211 | 1211 | if v in m2 and v not in m1 and k in m2: |
|
1212 | 1212 | repo.dirstate.remove(v) |
|
1213 | 1213 | |
|
1214 | 1214 | def commit(ui, repo, commitfunc, pats, opts): |
|
1215 | 1215 | '''commit the specified files or all outstanding changes''' |
|
1216 | 1216 | date = opts.get('date') |
|
1217 | 1217 | if date: |
|
1218 | 1218 | opts['date'] = util.parsedate(date) |
|
1219 | 1219 | message = logmessage(ui, opts) |
|
1220 | 1220 | |
|
1221 | 1221 | # extract addremove carefully -- this function can be called from a command |
|
1222 | 1222 | # that doesn't support addremove |
|
1223 | 1223 | if opts.get('addremove'): |
|
1224 | 1224 | scmutil.addremove(repo, pats, opts) |
|
1225 | 1225 | |
|
1226 | 1226 | return commitfunc(ui, repo, message, |
|
1227 | 1227 | scmutil.match(repo[None], pats, opts), opts) |
|
1228 | 1228 | |
|
1229 | 1229 | def commiteditor(repo, ctx, subs): |
|
1230 | 1230 | if ctx.description(): |
|
1231 | 1231 | return ctx.description() |
|
1232 | 1232 | return commitforceeditor(repo, ctx, subs) |
|
1233 | 1233 | |
|
1234 | 1234 | def commitforceeditor(repo, ctx, subs): |
|
1235 | 1235 | edittext = [] |
|
1236 | 1236 | modified, added, removed = ctx.modified(), ctx.added(), ctx.removed() |
|
1237 | 1237 | if ctx.description(): |
|
1238 | 1238 | edittext.append(ctx.description()) |
|
1239 | 1239 | edittext.append("") |
|
1240 | 1240 | edittext.append("") # Empty line between message and comments. |
|
1241 | 1241 | edittext.append(_("HG: Enter commit message." |
|
1242 | 1242 | " Lines beginning with 'HG:' are removed.")) |
|
1243 | 1243 | edittext.append(_("HG: Leave message empty to abort commit.")) |
|
1244 | 1244 | edittext.append("HG: --") |
|
1245 | 1245 | edittext.append(_("HG: user: %s") % ctx.user()) |
|
1246 | 1246 | if ctx.p2(): |
|
1247 | 1247 | edittext.append(_("HG: branch merge")) |
|
1248 | 1248 | if ctx.branch(): |
|
1249 | 1249 | edittext.append(_("HG: branch '%s'") % ctx.branch()) |
|
1250 | 1250 | edittext.extend([_("HG: subrepo %s") % s for s in subs]) |
|
1251 | 1251 | edittext.extend([_("HG: added %s") % f for f in added]) |
|
1252 | 1252 | edittext.extend([_("HG: changed %s") % f for f in modified]) |
|
1253 | 1253 | edittext.extend([_("HG: removed %s") % f for f in removed]) |
|
1254 | 1254 | if not added and not modified and not removed: |
|
1255 | 1255 | edittext.append(_("HG: no files changed")) |
|
1256 | 1256 | edittext.append("") |
|
1257 | 1257 | # run editor in the repository root |
|
1258 | 1258 | olddir = os.getcwd() |
|
1259 | 1259 | os.chdir(repo.root) |
|
1260 | 1260 | text = repo.ui.edit("\n".join(edittext), ctx.user()) |
|
1261 | 1261 | text = re.sub("(?m)^HG:.*(\n|$)", "", text) |
|
1262 | 1262 | os.chdir(olddir) |
|
1263 | 1263 | |
|
1264 | 1264 | if not text.strip(): |
|
1265 | 1265 | raise util.Abort(_("empty commit message")) |
|
1266 | 1266 | |
|
1267 | 1267 | return text |
|
1268 | 1268 | |
|
1269 | 1269 | def command(table): |
|
1270 | 1270 | '''returns a function object bound to table which can be used as |
|
1271 | 1271 | a decorator for populating table as a command table''' |
|
1272 | 1272 | |
|
1273 | 1273 | def cmd(name, options, synopsis=None): |
|
1274 | 1274 | def decorator(func): |
|
1275 | 1275 | if synopsis: |
|
1276 | 1276 | table[name] = func, options[:], synopsis |
|
1277 | 1277 | else: |
|
1278 | 1278 | table[name] = func, options[:] |
|
1279 | 1279 | return func |
|
1280 | 1280 | return decorator |
|
1281 | 1281 | |
|
1282 | 1282 | return cmd |
@@ -1,5711 +1,5710 b'' | |||
|
1 | 1 | # commands.py - command processing for 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, bin, nullid, nullrev, short |
|
9 | 9 | from lock import release |
|
10 | 10 | from i18n import _, gettext |
|
11 | 11 | import os, re, difflib, time, tempfile, errno |
|
12 | 12 | import hg, scmutil, util, revlog, extensions, copies, error, bookmarks |
|
13 | 13 | import patch, help, url, encoding, templatekw, discovery |
|
14 | 14 | import archival, changegroup, cmdutil, hbisect |
|
15 | 15 | import sshserver, hgweb, hgweb.server, commandserver |
|
16 | 16 | import match as matchmod |
|
17 | 17 | import merge as mergemod |
|
18 | 18 | import minirst, revset, fileset |
|
19 | 19 | import dagparser, context, simplemerge |
|
20 | 20 | import random, setdiscovery, treediscovery, dagutil |
|
21 | 21 | |
|
22 | 22 | table = {} |
|
23 | 23 | |
|
24 | 24 | command = cmdutil.command(table) |
|
25 | 25 | |
|
26 | 26 | # common command options |
|
27 | 27 | |
|
28 | 28 | globalopts = [ |
|
29 | 29 | ('R', 'repository', '', |
|
30 | 30 | _('repository root directory or name of overlay bundle file'), |
|
31 | 31 | _('REPO')), |
|
32 | 32 | ('', 'cwd', '', |
|
33 | 33 | _('change working directory'), _('DIR')), |
|
34 | 34 | ('y', 'noninteractive', None, |
|
35 | 35 | _('do not prompt, automatically pick the first choice for all prompts')), |
|
36 | 36 | ('q', 'quiet', None, _('suppress output')), |
|
37 | 37 | ('v', 'verbose', None, _('enable additional output')), |
|
38 | 38 | ('', 'config', [], |
|
39 | 39 | _('set/override config option (use \'section.name=value\')'), |
|
40 | 40 | _('CONFIG')), |
|
41 | 41 | ('', 'debug', None, _('enable debugging output')), |
|
42 | 42 | ('', 'debugger', None, _('start debugger')), |
|
43 | 43 | ('', 'encoding', encoding.encoding, _('set the charset encoding'), |
|
44 | 44 | _('ENCODE')), |
|
45 | 45 | ('', 'encodingmode', encoding.encodingmode, |
|
46 | 46 | _('set the charset encoding mode'), _('MODE')), |
|
47 | 47 | ('', 'traceback', None, _('always print a traceback on exception')), |
|
48 | 48 | ('', 'time', None, _('time how long the command takes')), |
|
49 | 49 | ('', 'profile', None, _('print command execution profile')), |
|
50 | 50 | ('', 'version', None, _('output version information and exit')), |
|
51 | 51 | ('h', 'help', None, _('display help and exit')), |
|
52 | 52 | ] |
|
53 | 53 | |
|
54 | 54 | dryrunopts = [('n', 'dry-run', None, |
|
55 | 55 | _('do not perform actions, just print output'))] |
|
56 | 56 | |
|
57 | 57 | remoteopts = [ |
|
58 | 58 | ('e', 'ssh', '', |
|
59 | 59 | _('specify ssh command to use'), _('CMD')), |
|
60 | 60 | ('', 'remotecmd', '', |
|
61 | 61 | _('specify hg command to run on the remote side'), _('CMD')), |
|
62 | 62 | ('', 'insecure', None, |
|
63 | 63 | _('do not verify server certificate (ignoring web.cacerts config)')), |
|
64 | 64 | ] |
|
65 | 65 | |
|
66 | 66 | walkopts = [ |
|
67 | 67 | ('I', 'include', [], |
|
68 | 68 | _('include names matching the given patterns'), _('PATTERN')), |
|
69 | 69 | ('X', 'exclude', [], |
|
70 | 70 | _('exclude names matching the given patterns'), _('PATTERN')), |
|
71 | 71 | ] |
|
72 | 72 | |
|
73 | 73 | commitopts = [ |
|
74 | 74 | ('m', 'message', '', |
|
75 | 75 | _('use text as commit message'), _('TEXT')), |
|
76 | 76 | ('l', 'logfile', '', |
|
77 | 77 | _('read commit message from file'), _('FILE')), |
|
78 | 78 | ] |
|
79 | 79 | |
|
80 | 80 | commitopts2 = [ |
|
81 | 81 | ('d', 'date', '', |
|
82 | 82 | _('record the specified date as commit date'), _('DATE')), |
|
83 | 83 | ('u', 'user', '', |
|
84 | 84 | _('record the specified user as committer'), _('USER')), |
|
85 | 85 | ] |
|
86 | 86 | |
|
87 | 87 | templateopts = [ |
|
88 | 88 | ('', 'style', '', |
|
89 | 89 | _('display using template map file'), _('STYLE')), |
|
90 | 90 | ('', 'template', '', |
|
91 | 91 | _('display with template'), _('TEMPLATE')), |
|
92 | 92 | ] |
|
93 | 93 | |
|
94 | 94 | logopts = [ |
|
95 | 95 | ('p', 'patch', None, _('show patch')), |
|
96 | 96 | ('g', 'git', None, _('use git extended diff format')), |
|
97 | 97 | ('l', 'limit', '', |
|
98 | 98 | _('limit number of changes displayed'), _('NUM')), |
|
99 | 99 | ('M', 'no-merges', None, _('do not show merges')), |
|
100 | 100 | ('', 'stat', None, _('output diffstat-style summary of changes')), |
|
101 | 101 | ] + templateopts |
|
102 | 102 | |
|
103 | 103 | diffopts = [ |
|
104 | 104 | ('a', 'text', None, _('treat all files as text')), |
|
105 | 105 | ('g', 'git', None, _('use git extended diff format')), |
|
106 | 106 | ('', 'nodates', None, _('omit dates from diff headers')) |
|
107 | 107 | ] |
|
108 | 108 | |
|
109 | 109 | diffwsopts = [ |
|
110 | 110 | ('w', 'ignore-all-space', None, |
|
111 | 111 | _('ignore white space when comparing lines')), |
|
112 | 112 | ('b', 'ignore-space-change', None, |
|
113 | 113 | _('ignore changes in the amount of white space')), |
|
114 | 114 | ('B', 'ignore-blank-lines', None, |
|
115 | 115 | _('ignore changes whose lines are all blank')), |
|
116 | 116 | ] |
|
117 | 117 | |
|
118 | 118 | diffopts2 = [ |
|
119 | 119 | ('p', 'show-function', None, _('show which function each change is in')), |
|
120 | 120 | ('', 'reverse', None, _('produce a diff that undoes the changes')), |
|
121 | 121 | ] + diffwsopts + [ |
|
122 | 122 | ('U', 'unified', '', |
|
123 | 123 | _('number of lines of context to show'), _('NUM')), |
|
124 | 124 | ('', 'stat', None, _('output diffstat-style summary of changes')), |
|
125 | 125 | ] |
|
126 | 126 | |
|
127 | 127 | mergetoolopts = [ |
|
128 | 128 | ('t', 'tool', '', _('specify merge tool')), |
|
129 | 129 | ] |
|
130 | 130 | |
|
131 | 131 | similarityopts = [ |
|
132 | 132 | ('s', 'similarity', '', |
|
133 | 133 | _('guess renamed files by similarity (0<=s<=100)'), _('SIMILARITY')) |
|
134 | 134 | ] |
|
135 | 135 | |
|
136 | 136 | subrepoopts = [ |
|
137 | 137 | ('S', 'subrepos', None, |
|
138 | 138 | _('recurse into subrepositories')) |
|
139 | 139 | ] |
|
140 | 140 | |
|
141 | 141 | # Commands start here, listed alphabetically |
|
142 | 142 | |
|
143 | 143 | @command('^add', |
|
144 | 144 | walkopts + subrepoopts + dryrunopts, |
|
145 | 145 | _('[OPTION]... [FILE]...')) |
|
146 | 146 | def add(ui, repo, *pats, **opts): |
|
147 | 147 | """add the specified files on the next commit |
|
148 | 148 | |
|
149 | 149 | Schedule files to be version controlled and added to the |
|
150 | 150 | repository. |
|
151 | 151 | |
|
152 | 152 | The files will be added to the repository at the next commit. To |
|
153 | 153 | undo an add before that, see :hg:`forget`. |
|
154 | 154 | |
|
155 | 155 | If no names are given, add all files to the repository. |
|
156 | 156 | |
|
157 | 157 | .. container:: verbose |
|
158 | 158 | |
|
159 | 159 | An example showing how new (unknown) files are added |
|
160 | 160 | automatically by :hg:`add`:: |
|
161 | 161 | |
|
162 | 162 | $ ls |
|
163 | 163 | foo.c |
|
164 | 164 | $ hg status |
|
165 | 165 | ? foo.c |
|
166 | 166 | $ hg add |
|
167 | 167 | adding foo.c |
|
168 | 168 | $ hg status |
|
169 | 169 | A foo.c |
|
170 | 170 | |
|
171 | 171 | Returns 0 if all files are successfully added. |
|
172 | 172 | """ |
|
173 | 173 | |
|
174 | 174 | m = scmutil.match(repo[None], pats, opts) |
|
175 | 175 | rejected = cmdutil.add(ui, repo, m, opts.get('dry_run'), |
|
176 | 176 | opts.get('subrepos'), prefix="") |
|
177 | 177 | return rejected and 1 or 0 |
|
178 | 178 | |
|
179 | 179 | @command('addremove', |
|
180 | 180 | similarityopts + walkopts + dryrunopts, |
|
181 | 181 | _('[OPTION]... [FILE]...')) |
|
182 | 182 | def addremove(ui, repo, *pats, **opts): |
|
183 | 183 | """add all new files, delete all missing files |
|
184 | 184 | |
|
185 | 185 | Add all new files and remove all missing files from the |
|
186 | 186 | repository. |
|
187 | 187 | |
|
188 | 188 | New files are ignored if they match any of the patterns in |
|
189 | 189 | ``.hgignore``. As with add, these changes take effect at the next |
|
190 | 190 | commit. |
|
191 | 191 | |
|
192 | 192 | Use the -s/--similarity option to detect renamed files. With a |
|
193 | 193 | parameter greater than 0, this compares every removed file with |
|
194 | 194 | every added file and records those similar enough as renames. This |
|
195 | 195 | option takes a percentage between 0 (disabled) and 100 (files must |
|
196 | 196 | be identical) as its parameter. Detecting renamed files this way |
|
197 | 197 | can be expensive. After using this option, :hg:`status -C` can be |
|
198 | 198 | used to check which files were identified as moved or renamed. |
|
199 | 199 | |
|
200 | 200 | Returns 0 if all files are successfully added. |
|
201 | 201 | """ |
|
202 | 202 | try: |
|
203 | 203 | sim = float(opts.get('similarity') or 100) |
|
204 | 204 | except ValueError: |
|
205 | 205 | raise util.Abort(_('similarity must be a number')) |
|
206 | 206 | if sim < 0 or sim > 100: |
|
207 | 207 | raise util.Abort(_('similarity must be between 0 and 100')) |
|
208 | 208 | return scmutil.addremove(repo, pats, opts, similarity=sim / 100.0) |
|
209 | 209 | |
|
210 | 210 | @command('^annotate|blame', |
|
211 | 211 | [('r', 'rev', '', _('annotate the specified revision'), _('REV')), |
|
212 | 212 | ('', 'follow', None, |
|
213 | 213 | _('follow copies/renames and list the filename (DEPRECATED)')), |
|
214 | 214 | ('', 'no-follow', None, _("don't follow copies and renames")), |
|
215 | 215 | ('a', 'text', None, _('treat all files as text')), |
|
216 | 216 | ('u', 'user', None, _('list the author (long with -v)')), |
|
217 | 217 | ('f', 'file', None, _('list the filename')), |
|
218 | 218 | ('d', 'date', None, _('list the date (short with -q)')), |
|
219 | 219 | ('n', 'number', None, _('list the revision number (default)')), |
|
220 | 220 | ('c', 'changeset', None, _('list the changeset')), |
|
221 | 221 | ('l', 'line-number', None, _('show line number at the first appearance')) |
|
222 | 222 | ] + diffwsopts + walkopts, |
|
223 | 223 | _('[-r REV] [-f] [-a] [-u] [-d] [-n] [-c] [-l] FILE...')) |
|
224 | 224 | def annotate(ui, repo, *pats, **opts): |
|
225 | 225 | """show changeset information by line for each file |
|
226 | 226 | |
|
227 | 227 | List changes in files, showing the revision id responsible for |
|
228 | 228 | each line |
|
229 | 229 | |
|
230 | 230 | This command is useful for discovering when a change was made and |
|
231 | 231 | by whom. |
|
232 | 232 | |
|
233 | 233 | Without the -a/--text option, annotate will avoid processing files |
|
234 | 234 | it detects as binary. With -a, annotate will annotate the file |
|
235 | 235 | anyway, although the results will probably be neither useful |
|
236 | 236 | nor desirable. |
|
237 | 237 | |
|
238 | 238 | Returns 0 on success. |
|
239 | 239 | """ |
|
240 | 240 | if opts.get('follow'): |
|
241 | 241 | # --follow is deprecated and now just an alias for -f/--file |
|
242 | 242 | # to mimic the behavior of Mercurial before version 1.5 |
|
243 | 243 | opts['file'] = True |
|
244 | 244 | |
|
245 | 245 | datefunc = ui.quiet and util.shortdate or util.datestr |
|
246 | 246 | getdate = util.cachefunc(lambda x: datefunc(x[0].date())) |
|
247 | 247 | |
|
248 | 248 | if not pats: |
|
249 | 249 | raise util.Abort(_('at least one filename or pattern is required')) |
|
250 | 250 | |
|
251 | 251 | hexfn = ui.debugflag and hex or short |
|
252 | 252 | |
|
253 | 253 | opmap = [('user', ' ', lambda x: ui.shortuser(x[0].user())), |
|
254 | 254 | ('number', ' ', lambda x: str(x[0].rev())), |
|
255 | 255 | ('changeset', ' ', lambda x: hexfn(x[0].node())), |
|
256 | 256 | ('date', ' ', getdate), |
|
257 | 257 | ('file', ' ', lambda x: x[0].path()), |
|
258 | 258 | ('line_number', ':', lambda x: str(x[1])), |
|
259 | 259 | ] |
|
260 | 260 | |
|
261 | 261 | if (not opts.get('user') and not opts.get('changeset') |
|
262 | 262 | and not opts.get('date') and not opts.get('file')): |
|
263 | 263 | opts['number'] = True |
|
264 | 264 | |
|
265 | 265 | linenumber = opts.get('line_number') is not None |
|
266 | 266 | if linenumber and (not opts.get('changeset')) and (not opts.get('number')): |
|
267 | 267 | raise util.Abort(_('at least one of -n/-c is required for -l')) |
|
268 | 268 | |
|
269 | 269 | funcmap = [(func, sep) for op, sep, func in opmap if opts.get(op)] |
|
270 | 270 | funcmap[0] = (funcmap[0][0], '') # no separator in front of first column |
|
271 | 271 | |
|
272 | 272 | def bad(x, y): |
|
273 | 273 | raise util.Abort("%s: %s" % (x, y)) |
|
274 | 274 | |
|
275 | 275 | ctx = scmutil.revsingle(repo, opts.get('rev')) |
|
276 | 276 | m = scmutil.match(ctx, pats, opts) |
|
277 | 277 | m.bad = bad |
|
278 | 278 | follow = not opts.get('no_follow') |
|
279 | 279 | diffopts = patch.diffopts(ui, opts, section='annotate') |
|
280 | 280 | for abs in ctx.walk(m): |
|
281 | 281 | fctx = ctx[abs] |
|
282 | 282 | if not opts.get('text') and util.binary(fctx.data()): |
|
283 | 283 | ui.write(_("%s: binary file\n") % ((pats and m.rel(abs)) or abs)) |
|
284 | 284 | continue |
|
285 | 285 | |
|
286 | 286 | lines = fctx.annotate(follow=follow, linenumber=linenumber, |
|
287 | 287 | diffopts=diffopts) |
|
288 | 288 | pieces = [] |
|
289 | 289 | |
|
290 | 290 | for f, sep in funcmap: |
|
291 | 291 | l = [f(n) for n, dummy in lines] |
|
292 | 292 | if l: |
|
293 | 293 | sized = [(x, encoding.colwidth(x)) for x in l] |
|
294 | 294 | ml = max([w for x, w in sized]) |
|
295 | 295 | pieces.append(["%s%s%s" % (sep, ' ' * (ml - w), x) |
|
296 | 296 | for x, w in sized]) |
|
297 | 297 | |
|
298 | 298 | if pieces: |
|
299 | 299 | for p, l in zip(zip(*pieces), lines): |
|
300 | 300 | ui.write("%s: %s" % ("".join(p), l[1])) |
|
301 | 301 | |
|
302 | 302 | @command('archive', |
|
303 | 303 | [('', 'no-decode', None, _('do not pass files through decoders')), |
|
304 | 304 | ('p', 'prefix', '', _('directory prefix for files in archive'), |
|
305 | 305 | _('PREFIX')), |
|
306 | 306 | ('r', 'rev', '', _('revision to distribute'), _('REV')), |
|
307 | 307 | ('t', 'type', '', _('type of distribution to create'), _('TYPE')), |
|
308 | 308 | ] + subrepoopts + walkopts, |
|
309 | 309 | _('[OPTION]... DEST')) |
|
310 | 310 | def archive(ui, repo, dest, **opts): |
|
311 | 311 | '''create an unversioned archive of a repository revision |
|
312 | 312 | |
|
313 | 313 | By default, the revision used is the parent of the working |
|
314 | 314 | directory; use -r/--rev to specify a different revision. |
|
315 | 315 | |
|
316 | 316 | The archive type is automatically detected based on file |
|
317 | 317 | extension (or override using -t/--type). |
|
318 | 318 | |
|
319 | 319 | .. container:: verbose |
|
320 | 320 | |
|
321 | 321 | Examples: |
|
322 | 322 | |
|
323 | 323 | - create a zip file containing the 1.0 release:: |
|
324 | 324 | |
|
325 | 325 | hg archive -r 1.0 project-1.0.zip |
|
326 | 326 | |
|
327 | 327 | - create a tarball excluding .hg files:: |
|
328 | 328 | |
|
329 | 329 | hg archive project.tar.gz -X ".hg*" |
|
330 | 330 | |
|
331 | 331 | Valid types are: |
|
332 | 332 | |
|
333 | 333 | :``files``: a directory full of files (default) |
|
334 | 334 | :``tar``: tar archive, uncompressed |
|
335 | 335 | :``tbz2``: tar archive, compressed using bzip2 |
|
336 | 336 | :``tgz``: tar archive, compressed using gzip |
|
337 | 337 | :``uzip``: zip archive, uncompressed |
|
338 | 338 | :``zip``: zip archive, compressed using deflate |
|
339 | 339 | |
|
340 | 340 | The exact name of the destination archive or directory is given |
|
341 | 341 | using a format string; see :hg:`help export` for details. |
|
342 | 342 | |
|
343 | 343 | Each member added to an archive file has a directory prefix |
|
344 | 344 | prepended. Use -p/--prefix to specify a format string for the |
|
345 | 345 | prefix. The default is the basename of the archive, with suffixes |
|
346 | 346 | removed. |
|
347 | 347 | |
|
348 | 348 | Returns 0 on success. |
|
349 | 349 | ''' |
|
350 | 350 | |
|
351 | 351 | ctx = scmutil.revsingle(repo, opts.get('rev')) |
|
352 | 352 | if not ctx: |
|
353 | 353 | raise util.Abort(_('no working directory: please specify a revision')) |
|
354 | 354 | node = ctx.node() |
|
355 | 355 | dest = cmdutil.makefilename(repo, dest, node) |
|
356 | 356 | if os.path.realpath(dest) == repo.root: |
|
357 | 357 | raise util.Abort(_('repository root cannot be destination')) |
|
358 | 358 | |
|
359 | 359 | kind = opts.get('type') or archival.guesskind(dest) or 'files' |
|
360 | 360 | prefix = opts.get('prefix') |
|
361 | 361 | |
|
362 | 362 | if dest == '-': |
|
363 | 363 | if kind == 'files': |
|
364 | 364 | raise util.Abort(_('cannot archive plain files to stdout')) |
|
365 | 365 | dest = cmdutil.makefileobj(repo, dest) |
|
366 | 366 | if not prefix: |
|
367 | 367 | prefix = os.path.basename(repo.root) + '-%h' |
|
368 | 368 | |
|
369 | 369 | prefix = cmdutil.makefilename(repo, prefix, node) |
|
370 | 370 | matchfn = scmutil.match(ctx, [], opts) |
|
371 | 371 | archival.archive(repo, dest, node, kind, not opts.get('no_decode'), |
|
372 | 372 | matchfn, prefix, subrepos=opts.get('subrepos')) |
|
373 | 373 | |
|
374 | 374 | @command('backout', |
|
375 | 375 | [('', 'merge', None, _('merge with old dirstate parent after backout')), |
|
376 | 376 | ('', 'parent', '', |
|
377 | 377 | _('parent to choose when backing out merge (DEPRECATED)'), _('REV')), |
|
378 | 378 | ('r', 'rev', '', _('revision to backout'), _('REV')), |
|
379 | 379 | ] + mergetoolopts + walkopts + commitopts + commitopts2, |
|
380 | 380 | _('[OPTION]... [-r] REV')) |
|
381 | 381 | def backout(ui, repo, node=None, rev=None, **opts): |
|
382 | 382 | '''reverse effect of earlier changeset |
|
383 | 383 | |
|
384 | 384 | Prepare a new changeset with the effect of REV undone in the |
|
385 | 385 | current working directory. |
|
386 | 386 | |
|
387 | 387 | If REV is the parent of the working directory, then this new changeset |
|
388 | 388 | is committed automatically. Otherwise, hg needs to merge the |
|
389 | 389 | changes and the merged result is left uncommitted. |
|
390 | 390 | |
|
391 | 391 | .. note:: |
|
392 | 392 | backout cannot be used to fix either an unwanted or |
|
393 | 393 | incorrect merge. |
|
394 | 394 | |
|
395 | 395 | .. container:: verbose |
|
396 | 396 | |
|
397 | 397 | By default, the pending changeset will have one parent, |
|
398 | 398 | maintaining a linear history. With --merge, the pending |
|
399 | 399 | changeset will instead have two parents: the old parent of the |
|
400 | 400 | working directory and a new child of REV that simply undoes REV. |
|
401 | 401 | |
|
402 | 402 | Before version 1.7, the behavior without --merge was equivalent |
|
403 | 403 | to specifying --merge followed by :hg:`update --clean .` to |
|
404 | 404 | cancel the merge and leave the child of REV as a head to be |
|
405 | 405 | merged separately. |
|
406 | 406 | |
|
407 | 407 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
408 | 408 | |
|
409 | 409 | Returns 0 on success. |
|
410 | 410 | ''' |
|
411 | 411 | if rev and node: |
|
412 | 412 | raise util.Abort(_("please specify just one revision")) |
|
413 | 413 | |
|
414 | 414 | if not rev: |
|
415 | 415 | rev = node |
|
416 | 416 | |
|
417 | 417 | if not rev: |
|
418 | 418 | raise util.Abort(_("please specify a revision to backout")) |
|
419 | 419 | |
|
420 | 420 | date = opts.get('date') |
|
421 | 421 | if date: |
|
422 | 422 | opts['date'] = util.parsedate(date) |
|
423 | 423 | |
|
424 | 424 | cmdutil.bailifchanged(repo) |
|
425 | 425 | node = scmutil.revsingle(repo, rev).node() |
|
426 | 426 | |
|
427 | 427 | op1, op2 = repo.dirstate.parents() |
|
428 | 428 | a = repo.changelog.ancestor(op1, node) |
|
429 | 429 | if a != node: |
|
430 | 430 | raise util.Abort(_('cannot backout change on a different branch')) |
|
431 | 431 | |
|
432 | 432 | p1, p2 = repo.changelog.parents(node) |
|
433 | 433 | if p1 == nullid: |
|
434 | 434 | raise util.Abort(_('cannot backout a change with no parents')) |
|
435 | 435 | if p2 != nullid: |
|
436 | 436 | if not opts.get('parent'): |
|
437 | 437 | raise util.Abort(_('cannot backout a merge changeset')) |
|
438 | 438 | p = repo.lookup(opts['parent']) |
|
439 | 439 | if p not in (p1, p2): |
|
440 | 440 | raise util.Abort(_('%s is not a parent of %s') % |
|
441 | 441 | (short(p), short(node))) |
|
442 | 442 | parent = p |
|
443 | 443 | else: |
|
444 | 444 | if opts.get('parent'): |
|
445 | 445 | raise util.Abort(_('cannot use --parent on non-merge changeset')) |
|
446 | 446 | parent = p1 |
|
447 | 447 | |
|
448 | 448 | # the backout should appear on the same branch |
|
449 | 449 | branch = repo.dirstate.branch() |
|
450 | 450 | hg.clean(repo, node, show_stats=False) |
|
451 | 451 | repo.dirstate.setbranch(branch) |
|
452 | 452 | revert_opts = opts.copy() |
|
453 | 453 | revert_opts['date'] = None |
|
454 | 454 | revert_opts['all'] = True |
|
455 | 455 | revert_opts['rev'] = hex(parent) |
|
456 | 456 | revert_opts['no_backup'] = None |
|
457 | 457 | revert(ui, repo, **revert_opts) |
|
458 | 458 | if not opts.get('merge') and op1 != node: |
|
459 | 459 | try: |
|
460 | 460 | ui.setconfig('ui', 'forcemerge', opts.get('tool', '')) |
|
461 | 461 | return hg.update(repo, op1) |
|
462 | 462 | finally: |
|
463 | 463 | ui.setconfig('ui', 'forcemerge', '') |
|
464 | 464 | |
|
465 | 465 | commit_opts = opts.copy() |
|
466 | 466 | commit_opts['addremove'] = False |
|
467 | 467 | if not commit_opts['message'] and not commit_opts['logfile']: |
|
468 | 468 | # we don't translate commit messages |
|
469 | 469 | commit_opts['message'] = "Backed out changeset %s" % short(node) |
|
470 | 470 | commit_opts['force_editor'] = True |
|
471 | 471 | commit(ui, repo, **commit_opts) |
|
472 | 472 | def nice(node): |
|
473 | 473 | return '%d:%s' % (repo.changelog.rev(node), short(node)) |
|
474 | 474 | ui.status(_('changeset %s backs out changeset %s\n') % |
|
475 | 475 | (nice(repo.changelog.tip()), nice(node))) |
|
476 | 476 | if opts.get('merge') and op1 != node: |
|
477 | 477 | hg.clean(repo, op1, show_stats=False) |
|
478 | 478 | ui.status(_('merging with changeset %s\n') |
|
479 | 479 | % nice(repo.changelog.tip())) |
|
480 | 480 | try: |
|
481 | 481 | ui.setconfig('ui', 'forcemerge', opts.get('tool', '')) |
|
482 | 482 | return hg.merge(repo, hex(repo.changelog.tip())) |
|
483 | 483 | finally: |
|
484 | 484 | ui.setconfig('ui', 'forcemerge', '') |
|
485 | 485 | return 0 |
|
486 | 486 | |
|
487 | 487 | @command('bisect', |
|
488 | 488 | [('r', 'reset', False, _('reset bisect state')), |
|
489 | 489 | ('g', 'good', False, _('mark changeset good')), |
|
490 | 490 | ('b', 'bad', False, _('mark changeset bad')), |
|
491 | 491 | ('s', 'skip', False, _('skip testing changeset')), |
|
492 | 492 | ('e', 'extend', False, _('extend the bisect range')), |
|
493 | 493 | ('c', 'command', '', _('use command to check changeset state'), _('CMD')), |
|
494 | 494 | ('U', 'noupdate', False, _('do not update to target'))], |
|
495 | 495 | _("[-gbsr] [-U] [-c CMD] [REV]")) |
|
496 | 496 | def bisect(ui, repo, rev=None, extra=None, command=None, |
|
497 | 497 | reset=None, good=None, bad=None, skip=None, extend=None, |
|
498 | 498 | noupdate=None): |
|
499 | 499 | """subdivision search of changesets |
|
500 | 500 | |
|
501 | 501 | This command helps to find changesets which introduce problems. To |
|
502 | 502 | use, mark the earliest changeset you know exhibits the problem as |
|
503 | 503 | bad, then mark the latest changeset which is free from the problem |
|
504 | 504 | as good. Bisect will update your working directory to a revision |
|
505 | 505 | for testing (unless the -U/--noupdate option is specified). Once |
|
506 | 506 | you have performed tests, mark the working directory as good or |
|
507 | 507 | bad, and bisect will either update to another candidate changeset |
|
508 | 508 | or announce that it has found the bad revision. |
|
509 | 509 | |
|
510 | 510 | As a shortcut, you can also use the revision argument to mark a |
|
511 | 511 | revision as good or bad without checking it out first. |
|
512 | 512 | |
|
513 | 513 | If you supply a command, it will be used for automatic bisection. |
|
514 | 514 | Its exit status will be used to mark revisions as good or bad: |
|
515 | 515 | status 0 means good, 125 means to skip the revision, 127 |
|
516 | 516 | (command not found) will abort the bisection, and any other |
|
517 | 517 | non-zero exit status means the revision is bad. |
|
518 | 518 | |
|
519 | 519 | .. container:: verbose |
|
520 | 520 | |
|
521 | 521 | Some examples: |
|
522 | 522 | |
|
523 | 523 | - start a bisection with known bad revision 12, and good revision 34:: |
|
524 | 524 | |
|
525 | 525 | hg bisect --bad 34 |
|
526 | 526 | hg bisect --good 12 |
|
527 | 527 | |
|
528 | 528 | - advance the current bisection by marking current revision as good or |
|
529 | 529 | bad:: |
|
530 | 530 | |
|
531 | 531 | hg bisect --good |
|
532 | 532 | hg bisect --bad |
|
533 | 533 | |
|
534 | 534 | - mark the current revision, or a known revision, to be skipped (eg. if |
|
535 | 535 | that revision is not usable because of another issue):: |
|
536 | 536 | |
|
537 | 537 | hg bisect --skip |
|
538 | 538 | hg bisect --skip 23 |
|
539 | 539 | |
|
540 | 540 | - forget the current bisection:: |
|
541 | 541 | |
|
542 | 542 | hg bisect --reset |
|
543 | 543 | |
|
544 | 544 | - use 'make && make tests' to automatically find the first broken |
|
545 | 545 | revision:: |
|
546 | 546 | |
|
547 | 547 | hg bisect --reset |
|
548 | 548 | hg bisect --bad 34 |
|
549 | 549 | hg bisect --good 12 |
|
550 | 550 | hg bisect --command 'make && make tests' |
|
551 | 551 | |
|
552 | 552 | - see all changesets whose states are already known in the current |
|
553 | 553 | bisection:: |
|
554 | 554 | |
|
555 | 555 | hg log -r "bisect(pruned)" |
|
556 | 556 | |
|
557 | 557 | - see all changesets that took part in the current bisection:: |
|
558 | 558 | |
|
559 | 559 | hg log -r "bisect(range)" |
|
560 | 560 | |
|
561 | 561 | - with the graphlog extension, you can even get a nice graph:: |
|
562 | 562 | |
|
563 | 563 | hg log --graph -r "bisect(range)" |
|
564 | 564 | |
|
565 | 565 | See :hg:`help revsets` for more about the `bisect()` keyword. |
|
566 | 566 | |
|
567 | 567 | Returns 0 on success. |
|
568 | 568 | """ |
|
569 | 569 | def extendbisectrange(nodes, good): |
|
570 | 570 | # bisect is incomplete when it ends on a merge node and |
|
571 | 571 | # one of the parent was not checked. |
|
572 | 572 | parents = repo[nodes[0]].parents() |
|
573 | 573 | if len(parents) > 1: |
|
574 | 574 | side = good and state['bad'] or state['good'] |
|
575 | 575 | num = len(set(i.node() for i in parents) & set(side)) |
|
576 | 576 | if num == 1: |
|
577 | 577 | return parents[0].ancestor(parents[1]) |
|
578 | 578 | return None |
|
579 | 579 | |
|
580 | 580 | def print_result(nodes, good): |
|
581 | 581 | displayer = cmdutil.show_changeset(ui, repo, {}) |
|
582 | 582 | if len(nodes) == 1: |
|
583 | 583 | # narrowed it down to a single revision |
|
584 | 584 | if good: |
|
585 | 585 | ui.write(_("The first good revision is:\n")) |
|
586 | 586 | else: |
|
587 | 587 | ui.write(_("The first bad revision is:\n")) |
|
588 | 588 | displayer.show(repo[nodes[0]]) |
|
589 | 589 | extendnode = extendbisectrange(nodes, good) |
|
590 | 590 | if extendnode is not None: |
|
591 | 591 | ui.write(_('Not all ancestors of this changeset have been' |
|
592 | 592 | ' checked.\nUse bisect --extend to continue the ' |
|
593 | 593 | 'bisection from\nthe common ancestor, %s.\n') |
|
594 | 594 | % extendnode) |
|
595 | 595 | else: |
|
596 | 596 | # multiple possible revisions |
|
597 | 597 | if good: |
|
598 | 598 | ui.write(_("Due to skipped revisions, the first " |
|
599 | 599 | "good revision could be any of:\n")) |
|
600 | 600 | else: |
|
601 | 601 | ui.write(_("Due to skipped revisions, the first " |
|
602 | 602 | "bad revision could be any of:\n")) |
|
603 | 603 | for n in nodes: |
|
604 | 604 | displayer.show(repo[n]) |
|
605 | 605 | displayer.close() |
|
606 | 606 | |
|
607 | 607 | def check_state(state, interactive=True): |
|
608 | 608 | if not state['good'] or not state['bad']: |
|
609 | 609 | if (good or bad or skip or reset) and interactive: |
|
610 | 610 | return |
|
611 | 611 | if not state['good']: |
|
612 | 612 | raise util.Abort(_('cannot bisect (no known good revisions)')) |
|
613 | 613 | else: |
|
614 | 614 | raise util.Abort(_('cannot bisect (no known bad revisions)')) |
|
615 | 615 | return True |
|
616 | 616 | |
|
617 | 617 | # backward compatibility |
|
618 | 618 | if rev in "good bad reset init".split(): |
|
619 | 619 | ui.warn(_("(use of 'hg bisect <cmd>' is deprecated)\n")) |
|
620 | 620 | cmd, rev, extra = rev, extra, None |
|
621 | 621 | if cmd == "good": |
|
622 | 622 | good = True |
|
623 | 623 | elif cmd == "bad": |
|
624 | 624 | bad = True |
|
625 | 625 | else: |
|
626 | 626 | reset = True |
|
627 | 627 | elif extra or good + bad + skip + reset + extend + bool(command) > 1: |
|
628 | 628 | raise util.Abort(_('incompatible arguments')) |
|
629 | 629 | |
|
630 | 630 | if reset: |
|
631 | 631 | p = repo.join("bisect.state") |
|
632 | 632 | if os.path.exists(p): |
|
633 | 633 | os.unlink(p) |
|
634 | 634 | return |
|
635 | 635 | |
|
636 | 636 | state = hbisect.load_state(repo) |
|
637 | 637 | |
|
638 | 638 | if command: |
|
639 | 639 | changesets = 1 |
|
640 | 640 | try: |
|
641 | 641 | while changesets: |
|
642 | 642 | # update state |
|
643 | 643 | status = util.system(command, out=ui.fout) |
|
644 | 644 | if status == 125: |
|
645 | 645 | transition = "skip" |
|
646 | 646 | elif status == 0: |
|
647 | 647 | transition = "good" |
|
648 | 648 | # status < 0 means process was killed |
|
649 | 649 | elif status == 127: |
|
650 | 650 | raise util.Abort(_("failed to execute %s") % command) |
|
651 | 651 | elif status < 0: |
|
652 | 652 | raise util.Abort(_("%s killed") % command) |
|
653 | 653 | else: |
|
654 | 654 | transition = "bad" |
|
655 | 655 | ctx = scmutil.revsingle(repo, rev) |
|
656 | 656 | rev = None # clear for future iterations |
|
657 | 657 | state[transition].append(ctx.node()) |
|
658 | 658 | ui.status(_('Changeset %d:%s: %s\n') % (ctx, ctx, transition)) |
|
659 | 659 | check_state(state, interactive=False) |
|
660 | 660 | # bisect |
|
661 | 661 | nodes, changesets, good = hbisect.bisect(repo.changelog, state) |
|
662 | 662 | # update to next check |
|
663 | 663 | cmdutil.bailifchanged(repo) |
|
664 | 664 | hg.clean(repo, nodes[0], show_stats=False) |
|
665 | 665 | finally: |
|
666 | 666 | hbisect.save_state(repo, state) |
|
667 | 667 | print_result(nodes, good) |
|
668 | 668 | return |
|
669 | 669 | |
|
670 | 670 | # update state |
|
671 | 671 | |
|
672 | 672 | if rev: |
|
673 | 673 | nodes = [repo.lookup(i) for i in scmutil.revrange(repo, [rev])] |
|
674 | 674 | else: |
|
675 | 675 | nodes = [repo.lookup('.')] |
|
676 | 676 | |
|
677 | 677 | if good or bad or skip: |
|
678 | 678 | if good: |
|
679 | 679 | state['good'] += nodes |
|
680 | 680 | elif bad: |
|
681 | 681 | state['bad'] += nodes |
|
682 | 682 | elif skip: |
|
683 | 683 | state['skip'] += nodes |
|
684 | 684 | hbisect.save_state(repo, state) |
|
685 | 685 | |
|
686 | 686 | if not check_state(state): |
|
687 | 687 | return |
|
688 | 688 | |
|
689 | 689 | # actually bisect |
|
690 | 690 | nodes, changesets, good = hbisect.bisect(repo.changelog, state) |
|
691 | 691 | if extend: |
|
692 | 692 | if not changesets: |
|
693 | 693 | extendnode = extendbisectrange(nodes, good) |
|
694 | 694 | if extendnode is not None: |
|
695 | 695 | ui.write(_("Extending search to changeset %d:%s\n" |
|
696 | 696 | % (extendnode.rev(), extendnode))) |
|
697 | 697 | if noupdate: |
|
698 | 698 | return |
|
699 | 699 | cmdutil.bailifchanged(repo) |
|
700 | 700 | return hg.clean(repo, extendnode.node()) |
|
701 | 701 | raise util.Abort(_("nothing to extend")) |
|
702 | 702 | |
|
703 | 703 | if changesets == 0: |
|
704 | 704 | print_result(nodes, good) |
|
705 | 705 | else: |
|
706 | 706 | assert len(nodes) == 1 # only a single node can be tested next |
|
707 | 707 | node = nodes[0] |
|
708 | 708 | # compute the approximate number of remaining tests |
|
709 | 709 | tests, size = 0, 2 |
|
710 | 710 | while size <= changesets: |
|
711 | 711 | tests, size = tests + 1, size * 2 |
|
712 | 712 | rev = repo.changelog.rev(node) |
|
713 | 713 | ui.write(_("Testing changeset %d:%s " |
|
714 | 714 | "(%d changesets remaining, ~%d tests)\n") |
|
715 | 715 | % (rev, short(node), changesets, tests)) |
|
716 | 716 | if not noupdate: |
|
717 | 717 | cmdutil.bailifchanged(repo) |
|
718 | 718 | return hg.clean(repo, node) |
|
719 | 719 | |
|
720 | 720 | @command('bookmarks', |
|
721 | 721 | [('f', 'force', False, _('force')), |
|
722 | 722 | ('r', 'rev', '', _('revision'), _('REV')), |
|
723 | 723 | ('d', 'delete', False, _('delete a given bookmark')), |
|
724 | 724 | ('m', 'rename', '', _('rename a given bookmark'), _('NAME')), |
|
725 | 725 | ('i', 'inactive', False, _('do not mark a new bookmark active'))], |
|
726 | 726 | _('hg bookmarks [-f] [-d] [-i] [-m NAME] [-r REV] [NAME]')) |
|
727 | 727 | def bookmark(ui, repo, mark=None, rev=None, force=False, delete=False, |
|
728 | 728 | rename=None, inactive=False): |
|
729 | 729 | '''track a line of development with movable markers |
|
730 | 730 | |
|
731 | 731 | Bookmarks are pointers to certain commits that move when committing. |
|
732 | 732 | Bookmarks are local. They can be renamed, copied and deleted. It is |
|
733 | 733 | possible to use :hg:`merge NAME` to merge from a given bookmark, and |
|
734 | 734 | :hg:`update NAME` to update to a given bookmark. |
|
735 | 735 | |
|
736 | 736 | You can use :hg:`bookmark NAME` to set a bookmark on the working |
|
737 | 737 | directory's parent revision with the given name. If you specify |
|
738 | 738 | a revision using -r REV (where REV may be an existing bookmark), |
|
739 | 739 | the bookmark is assigned to that revision. |
|
740 | 740 | |
|
741 | 741 | Bookmarks can be pushed and pulled between repositories (see :hg:`help |
|
742 | 742 | push` and :hg:`help pull`). This requires both the local and remote |
|
743 | 743 | repositories to support bookmarks. For versions prior to 1.8, this means |
|
744 | 744 | the bookmarks extension must be enabled. |
|
745 | 745 | ''' |
|
746 | 746 | hexfn = ui.debugflag and hex or short |
|
747 | 747 | marks = repo._bookmarks |
|
748 | 748 | cur = repo.changectx('.').node() |
|
749 | 749 | |
|
750 | 750 | if delete: |
|
751 | 751 | if mark is None: |
|
752 | 752 | raise util.Abort(_("bookmark name required")) |
|
753 | 753 | if mark not in marks: |
|
754 | 754 | raise util.Abort(_("bookmark '%s' does not exist") % mark) |
|
755 | 755 | if mark == repo._bookmarkcurrent: |
|
756 | 756 | bookmarks.setcurrent(repo, None) |
|
757 | 757 | del marks[mark] |
|
758 | 758 | bookmarks.write(repo) |
|
759 | 759 | return |
|
760 | 760 | |
|
761 | 761 | if rename: |
|
762 | 762 | if rename not in marks: |
|
763 | 763 | raise util.Abort(_("bookmark '%s' does not exist") % rename) |
|
764 | 764 | if mark in marks and not force: |
|
765 | 765 | raise util.Abort(_("bookmark '%s' already exists " |
|
766 | 766 | "(use -f to force)") % mark) |
|
767 | 767 | if mark is None: |
|
768 | 768 | raise util.Abort(_("new bookmark name required")) |
|
769 | 769 | marks[mark] = marks[rename] |
|
770 | 770 | if repo._bookmarkcurrent == rename and not inactive: |
|
771 | 771 | bookmarks.setcurrent(repo, mark) |
|
772 | 772 | del marks[rename] |
|
773 | 773 | bookmarks.write(repo) |
|
774 | 774 | return |
|
775 | 775 | |
|
776 | 776 | if mark is not None: |
|
777 | 777 | if "\n" in mark: |
|
778 | 778 | raise util.Abort(_("bookmark name cannot contain newlines")) |
|
779 | 779 | mark = mark.strip() |
|
780 | 780 | if not mark: |
|
781 | 781 | raise util.Abort(_("bookmark names cannot consist entirely of " |
|
782 | 782 | "whitespace")) |
|
783 | 783 | if inactive and mark == repo._bookmarkcurrent: |
|
784 | 784 | bookmarks.setcurrent(repo, None) |
|
785 | 785 | return |
|
786 | 786 | if mark in marks and not force: |
|
787 | 787 | raise util.Abort(_("bookmark '%s' already exists " |
|
788 | 788 | "(use -f to force)") % mark) |
|
789 | 789 | if ((mark in repo.branchtags() or mark == repo.dirstate.branch()) |
|
790 | 790 | and not force): |
|
791 | 791 | raise util.Abort( |
|
792 | 792 | _("a bookmark cannot have the name of an existing branch")) |
|
793 | 793 | if rev: |
|
794 | 794 | marks[mark] = repo.lookup(rev) |
|
795 | 795 | else: |
|
796 | 796 | marks[mark] = cur |
|
797 | 797 | if not inactive and cur == marks[mark]: |
|
798 | 798 | bookmarks.setcurrent(repo, mark) |
|
799 | 799 | bookmarks.write(repo) |
|
800 | 800 | return |
|
801 | 801 | |
|
802 | 802 | if mark is None: |
|
803 | 803 | if rev: |
|
804 | 804 | raise util.Abort(_("bookmark name required")) |
|
805 | 805 | if len(marks) == 0: |
|
806 | 806 | ui.status(_("no bookmarks set\n")) |
|
807 | 807 | else: |
|
808 | 808 | for bmark, n in sorted(marks.iteritems()): |
|
809 | 809 | current = repo._bookmarkcurrent |
|
810 | 810 | if bmark == current and n == cur: |
|
811 | 811 | prefix, label = '*', 'bookmarks.current' |
|
812 | 812 | else: |
|
813 | 813 | prefix, label = ' ', '' |
|
814 | 814 | |
|
815 | 815 | if ui.quiet: |
|
816 | 816 | ui.write("%s\n" % bmark, label=label) |
|
817 | 817 | else: |
|
818 | 818 | ui.write(" %s %-25s %d:%s\n" % ( |
|
819 | 819 | prefix, bmark, repo.changelog.rev(n), hexfn(n)), |
|
820 | 820 | label=label) |
|
821 | 821 | return |
|
822 | 822 | |
|
823 | 823 | @command('branch', |
|
824 | 824 | [('f', 'force', None, |
|
825 | 825 | _('set branch name even if it shadows an existing branch')), |
|
826 | 826 | ('C', 'clean', None, _('reset branch name to parent branch name'))], |
|
827 | 827 | _('[-fC] [NAME]')) |
|
828 | 828 | def branch(ui, repo, label=None, **opts): |
|
829 | 829 | """set or show the current branch name |
|
830 | 830 | |
|
831 | 831 | .. note:: |
|
832 | 832 | Branch names are permanent and global. Use :hg:`bookmark` to create a |
|
833 | 833 | light-weight bookmark instead. See :hg:`help glossary` for more |
|
834 | 834 | information about named branches and bookmarks. |
|
835 | 835 | |
|
836 | 836 | With no argument, show the current branch name. With one argument, |
|
837 | 837 | set the working directory branch name (the branch will not exist |
|
838 | 838 | in the repository until the next commit). Standard practice |
|
839 | 839 | recommends that primary development take place on the 'default' |
|
840 | 840 | branch. |
|
841 | 841 | |
|
842 | 842 | Unless -f/--force is specified, branch will not let you set a |
|
843 | 843 | branch name that already exists, even if it's inactive. |
|
844 | 844 | |
|
845 | 845 | Use -C/--clean to reset the working directory branch to that of |
|
846 | 846 | the parent of the working directory, negating a previous branch |
|
847 | 847 | change. |
|
848 | 848 | |
|
849 | 849 | Use the command :hg:`update` to switch to an existing branch. Use |
|
850 | 850 | :hg:`commit --close-branch` to mark this branch as closed. |
|
851 | 851 | |
|
852 | 852 | Returns 0 on success. |
|
853 | 853 | """ |
|
854 | 854 | |
|
855 | 855 | if opts.get('clean'): |
|
856 | 856 | label = repo[None].p1().branch() |
|
857 | 857 | repo.dirstate.setbranch(label) |
|
858 | 858 | ui.status(_('reset working directory to branch %s\n') % label) |
|
859 | 859 | elif label: |
|
860 | 860 | if not opts.get('force') and label in repo.branchtags(): |
|
861 | 861 | if label not in [p.branch() for p in repo.parents()]: |
|
862 | 862 | raise util.Abort(_('a branch of the same name already exists'), |
|
863 | 863 | # i18n: "it" refers to an existing branch |
|
864 | 864 | hint=_("use 'hg update' to switch to it")) |
|
865 | 865 | repo.dirstate.setbranch(label) |
|
866 | 866 | ui.status(_('marked working directory as branch %s\n') % label) |
|
867 | 867 | ui.status(_('(branches are permanent and global, ' |
|
868 | 868 | 'did you want a bookmark?)\n')) |
|
869 | 869 | else: |
|
870 | 870 | ui.write("%s\n" % repo.dirstate.branch()) |
|
871 | 871 | |
|
872 | 872 | @command('branches', |
|
873 | 873 | [('a', 'active', False, _('show only branches that have unmerged heads')), |
|
874 | 874 | ('c', 'closed', False, _('show normal and closed branches'))], |
|
875 | 875 | _('[-ac]')) |
|
876 | 876 | def branches(ui, repo, active=False, closed=False): |
|
877 | 877 | """list repository named branches |
|
878 | 878 | |
|
879 | 879 | List the repository's named branches, indicating which ones are |
|
880 | 880 | inactive. If -c/--closed is specified, also list branches which have |
|
881 | 881 | been marked closed (see :hg:`commit --close-branch`). |
|
882 | 882 | |
|
883 | 883 | If -a/--active is specified, only show active branches. A branch |
|
884 | 884 | is considered active if it contains repository heads. |
|
885 | 885 | |
|
886 | 886 | Use the command :hg:`update` to switch to an existing branch. |
|
887 | 887 | |
|
888 | 888 | Returns 0. |
|
889 | 889 | """ |
|
890 | 890 | |
|
891 | 891 | hexfunc = ui.debugflag and hex or short |
|
892 | 892 | activebranches = [repo[n].branch() for n in repo.heads()] |
|
893 | 893 | def testactive(tag, node): |
|
894 | 894 | realhead = tag in activebranches |
|
895 | 895 | open = node in repo.branchheads(tag, closed=False) |
|
896 | 896 | return realhead and open |
|
897 | 897 | branches = sorted([(testactive(tag, node), repo.changelog.rev(node), tag) |
|
898 | 898 | for tag, node in repo.branchtags().items()], |
|
899 | 899 | reverse=True) |
|
900 | 900 | |
|
901 | 901 | for isactive, node, tag in branches: |
|
902 | 902 | if (not active) or isactive: |
|
903 | 903 | if ui.quiet: |
|
904 | 904 | ui.write("%s\n" % tag) |
|
905 | 905 | else: |
|
906 | 906 | hn = repo.lookup(node) |
|
907 | 907 | if isactive: |
|
908 | 908 | label = 'branches.active' |
|
909 | 909 | notice = '' |
|
910 | 910 | elif hn not in repo.branchheads(tag, closed=False): |
|
911 | 911 | if not closed: |
|
912 | 912 | continue |
|
913 | 913 | label = 'branches.closed' |
|
914 | 914 | notice = _(' (closed)') |
|
915 | 915 | else: |
|
916 | 916 | label = 'branches.inactive' |
|
917 | 917 | notice = _(' (inactive)') |
|
918 | 918 | if tag == repo.dirstate.branch(): |
|
919 | 919 | label = 'branches.current' |
|
920 | 920 | rev = str(node).rjust(31 - encoding.colwidth(tag)) |
|
921 | 921 | rev = ui.label('%s:%s' % (rev, hexfunc(hn)), 'log.changeset') |
|
922 | 922 | tag = ui.label(tag, label) |
|
923 | 923 | ui.write("%s %s%s\n" % (tag, rev, notice)) |
|
924 | 924 | |
|
925 | 925 | @command('bundle', |
|
926 | 926 | [('f', 'force', None, _('run even when the destination is unrelated')), |
|
927 | 927 | ('r', 'rev', [], _('a changeset intended to be added to the destination'), |
|
928 | 928 | _('REV')), |
|
929 | 929 | ('b', 'branch', [], _('a specific branch you would like to bundle'), |
|
930 | 930 | _('BRANCH')), |
|
931 | 931 | ('', 'base', [], |
|
932 | 932 | _('a base changeset assumed to be available at the destination'), |
|
933 | 933 | _('REV')), |
|
934 | 934 | ('a', 'all', None, _('bundle all changesets in the repository')), |
|
935 | 935 | ('t', 'type', 'bzip2', _('bundle compression type to use'), _('TYPE')), |
|
936 | 936 | ] + remoteopts, |
|
937 | 937 | _('[-f] [-t TYPE] [-a] [-r REV]... [--base REV]... FILE [DEST]')) |
|
938 | 938 | def bundle(ui, repo, fname, dest=None, **opts): |
|
939 | 939 | """create a changegroup file |
|
940 | 940 | |
|
941 | 941 | Generate a compressed changegroup file collecting changesets not |
|
942 | 942 | known to be in another repository. |
|
943 | 943 | |
|
944 | 944 | If you omit the destination repository, then hg assumes the |
|
945 | 945 | destination will have all the nodes you specify with --base |
|
946 | 946 | parameters. To create a bundle containing all changesets, use |
|
947 | 947 | -a/--all (or --base null). |
|
948 | 948 | |
|
949 | 949 | You can change compression method with the -t/--type option. |
|
950 | 950 | The available compression methods are: none, bzip2, and |
|
951 | 951 | gzip (by default, bundles are compressed using bzip2). |
|
952 | 952 | |
|
953 | 953 | The bundle file can then be transferred using conventional means |
|
954 | 954 | and applied to another repository with the unbundle or pull |
|
955 | 955 | command. This is useful when direct push and pull are not |
|
956 | 956 | available or when exporting an entire repository is undesirable. |
|
957 | 957 | |
|
958 | 958 | Applying bundles preserves all changeset contents including |
|
959 | 959 | permissions, copy/rename information, and revision history. |
|
960 | 960 | |
|
961 | 961 | Returns 0 on success, 1 if no changes found. |
|
962 | 962 | """ |
|
963 | 963 | revs = None |
|
964 | 964 | if 'rev' in opts: |
|
965 | 965 | revs = scmutil.revrange(repo, opts['rev']) |
|
966 | 966 | |
|
967 | 967 | if opts.get('all'): |
|
968 | 968 | base = ['null'] |
|
969 | 969 | else: |
|
970 | 970 | base = scmutil.revrange(repo, opts.get('base')) |
|
971 | 971 | if base: |
|
972 | 972 | if dest: |
|
973 | 973 | raise util.Abort(_("--base is incompatible with specifying " |
|
974 | 974 | "a destination")) |
|
975 | 975 | common = [repo.lookup(rev) for rev in base] |
|
976 | 976 | heads = revs and map(repo.lookup, revs) or revs |
|
977 | 977 | else: |
|
978 | 978 | dest = ui.expandpath(dest or 'default-push', dest or 'default') |
|
979 | 979 | dest, branches = hg.parseurl(dest, opts.get('branch')) |
|
980 | 980 | other = hg.peer(repo, opts, dest) |
|
981 | 981 | revs, checkout = hg.addbranchrevs(repo, other, branches, revs) |
|
982 | 982 | heads = revs and map(repo.lookup, revs) or revs |
|
983 | 983 | common, outheads = discovery.findcommonoutgoing(repo, other, |
|
984 | 984 | onlyheads=heads, |
|
985 | 985 | force=opts.get('force')) |
|
986 | 986 | |
|
987 | 987 | cg = repo.getbundle('bundle', common=common, heads=heads) |
|
988 | 988 | if not cg: |
|
989 | 989 | ui.status(_("no changes found\n")) |
|
990 | 990 | return 1 |
|
991 | 991 | |
|
992 | 992 | bundletype = opts.get('type', 'bzip2').lower() |
|
993 | 993 | btypes = {'none': 'HG10UN', 'bzip2': 'HG10BZ', 'gzip': 'HG10GZ'} |
|
994 | 994 | bundletype = btypes.get(bundletype) |
|
995 | 995 | if bundletype not in changegroup.bundletypes: |
|
996 | 996 | raise util.Abort(_('unknown bundle type specified with --type')) |
|
997 | 997 | |
|
998 | 998 | changegroup.writebundle(cg, fname, bundletype) |
|
999 | 999 | |
|
1000 | 1000 | @command('cat', |
|
1001 | 1001 | [('o', 'output', '', |
|
1002 | 1002 | _('print output to file with formatted name'), _('FORMAT')), |
|
1003 | 1003 | ('r', 'rev', '', _('print the given revision'), _('REV')), |
|
1004 | 1004 | ('', 'decode', None, _('apply any matching decode filter')), |
|
1005 | 1005 | ] + walkopts, |
|
1006 | 1006 | _('[OPTION]... FILE...')) |
|
1007 | 1007 | def cat(ui, repo, file1, *pats, **opts): |
|
1008 | 1008 | """output the current or given revision of files |
|
1009 | 1009 | |
|
1010 | 1010 | Print the specified files as they were at the given revision. If |
|
1011 | 1011 | no revision is given, the parent of the working directory is used, |
|
1012 | 1012 | or tip if no revision is checked out. |
|
1013 | 1013 | |
|
1014 | 1014 | Output may be to a file, in which case the name of the file is |
|
1015 | 1015 | given using a format string. The formatting rules are the same as |
|
1016 | 1016 | for the export command, with the following additions: |
|
1017 | 1017 | |
|
1018 | 1018 | :``%s``: basename of file being printed |
|
1019 | 1019 | :``%d``: dirname of file being printed, or '.' if in repository root |
|
1020 | 1020 | :``%p``: root-relative path name of file being printed |
|
1021 | 1021 | |
|
1022 | 1022 | Returns 0 on success. |
|
1023 | 1023 | """ |
|
1024 | 1024 | ctx = scmutil.revsingle(repo, opts.get('rev')) |
|
1025 | 1025 | err = 1 |
|
1026 | 1026 | m = scmutil.match(ctx, (file1,) + pats, opts) |
|
1027 | 1027 | for abs in ctx.walk(m): |
|
1028 | 1028 | fp = cmdutil.makefileobj(repo, opts.get('output'), ctx.node(), |
|
1029 | 1029 | pathname=abs) |
|
1030 | 1030 | data = ctx[abs].data() |
|
1031 | 1031 | if opts.get('decode'): |
|
1032 | 1032 | data = repo.wwritedata(abs, data) |
|
1033 | 1033 | fp.write(data) |
|
1034 | 1034 | fp.close() |
|
1035 | 1035 | err = 0 |
|
1036 | 1036 | return err |
|
1037 | 1037 | |
|
1038 | 1038 | @command('^clone', |
|
1039 | 1039 | [('U', 'noupdate', None, |
|
1040 | 1040 | _('the clone will include an empty working copy (only a repository)')), |
|
1041 | 1041 | ('u', 'updaterev', '', _('revision, tag or branch to check out'), _('REV')), |
|
1042 | 1042 | ('r', 'rev', [], _('include the specified changeset'), _('REV')), |
|
1043 | 1043 | ('b', 'branch', [], _('clone only the specified branch'), _('BRANCH')), |
|
1044 | 1044 | ('', 'pull', None, _('use pull protocol to copy metadata')), |
|
1045 | 1045 | ('', 'uncompressed', None, _('use uncompressed transfer (fast over LAN)')), |
|
1046 | 1046 | ] + remoteopts, |
|
1047 | 1047 | _('[OPTION]... SOURCE [DEST]')) |
|
1048 | 1048 | def clone(ui, source, dest=None, **opts): |
|
1049 | 1049 | """make a copy of an existing repository |
|
1050 | 1050 | |
|
1051 | 1051 | Create a copy of an existing repository in a new directory. |
|
1052 | 1052 | |
|
1053 | 1053 | If no destination directory name is specified, it defaults to the |
|
1054 | 1054 | basename of the source. |
|
1055 | 1055 | |
|
1056 | 1056 | The location of the source is added to the new repository's |
|
1057 | 1057 | ``.hg/hgrc`` file, as the default to be used for future pulls. |
|
1058 | 1058 | |
|
1059 | 1059 | Only local paths and ``ssh://`` URLs are supported as |
|
1060 | 1060 | destinations. For ``ssh://`` destinations, no working directory or |
|
1061 | 1061 | ``.hg/hgrc`` will be created on the remote side. |
|
1062 | 1062 | |
|
1063 | 1063 | To pull only a subset of changesets, specify one or more revisions |
|
1064 | 1064 | identifiers with -r/--rev or branches with -b/--branch. The |
|
1065 | 1065 | resulting clone will contain only the specified changesets and |
|
1066 | 1066 | their ancestors. These options (or 'clone src#rev dest') imply |
|
1067 | 1067 | --pull, even for local source repositories. Note that specifying a |
|
1068 | 1068 | tag will include the tagged changeset but not the changeset |
|
1069 | 1069 | containing the tag. |
|
1070 | 1070 | |
|
1071 | 1071 | To check out a particular version, use -u/--update, or |
|
1072 | 1072 | -U/--noupdate to create a clone with no working directory. |
|
1073 | 1073 | |
|
1074 | 1074 | .. container:: verbose |
|
1075 | 1075 | |
|
1076 | 1076 | For efficiency, hardlinks are used for cloning whenever the |
|
1077 | 1077 | source and destination are on the same filesystem (note this |
|
1078 | 1078 | applies only to the repository data, not to the working |
|
1079 | 1079 | directory). Some filesystems, such as AFS, implement hardlinking |
|
1080 | 1080 | incorrectly, but do not report errors. In these cases, use the |
|
1081 | 1081 | --pull option to avoid hardlinking. |
|
1082 | 1082 | |
|
1083 | 1083 | In some cases, you can clone repositories and the working |
|
1084 | 1084 | directory using full hardlinks with :: |
|
1085 | 1085 | |
|
1086 | 1086 | $ cp -al REPO REPOCLONE |
|
1087 | 1087 | |
|
1088 | 1088 | This is the fastest way to clone, but it is not always safe. The |
|
1089 | 1089 | operation is not atomic (making sure REPO is not modified during |
|
1090 | 1090 | the operation is up to you) and you have to make sure your |
|
1091 | 1091 | editor breaks hardlinks (Emacs and most Linux Kernel tools do |
|
1092 | 1092 | so). Also, this is not compatible with certain extensions that |
|
1093 | 1093 | place their metadata under the .hg directory, such as mq. |
|
1094 | 1094 | |
|
1095 | 1095 | Mercurial will update the working directory to the first applicable |
|
1096 | 1096 | revision from this list: |
|
1097 | 1097 | |
|
1098 | 1098 | a) null if -U or the source repository has no changesets |
|
1099 | 1099 | b) if -u . and the source repository is local, the first parent of |
|
1100 | 1100 | the source repository's working directory |
|
1101 | 1101 | c) the changeset specified with -u (if a branch name, this means the |
|
1102 | 1102 | latest head of that branch) |
|
1103 | 1103 | d) the changeset specified with -r |
|
1104 | 1104 | e) the tipmost head specified with -b |
|
1105 | 1105 | f) the tipmost head specified with the url#branch source syntax |
|
1106 | 1106 | g) the tipmost head of the default branch |
|
1107 | 1107 | h) tip |
|
1108 | 1108 | |
|
1109 | 1109 | Examples: |
|
1110 | 1110 | |
|
1111 | 1111 | - clone a remote repository to a new directory named hg/:: |
|
1112 | 1112 | |
|
1113 | 1113 | hg clone http://selenic.com/hg |
|
1114 | 1114 | |
|
1115 | 1115 | - create a lightweight local clone:: |
|
1116 | 1116 | |
|
1117 | 1117 | hg clone project/ project-feature/ |
|
1118 | 1118 | |
|
1119 | 1119 | - clone from an absolute path on an ssh server (note double-slash):: |
|
1120 | 1120 | |
|
1121 | 1121 | hg clone ssh://user@server//home/projects/alpha/ |
|
1122 | 1122 | |
|
1123 | 1123 | - do a high-speed clone over a LAN while checking out a |
|
1124 | 1124 | specified version:: |
|
1125 | 1125 | |
|
1126 | 1126 | hg clone --uncompressed http://server/repo -u 1.5 |
|
1127 | 1127 | |
|
1128 | 1128 | - create a repository without changesets after a particular revision:: |
|
1129 | 1129 | |
|
1130 | 1130 | hg clone -r 04e544 experimental/ good/ |
|
1131 | 1131 | |
|
1132 | 1132 | - clone (and track) a particular named branch:: |
|
1133 | 1133 | |
|
1134 | 1134 | hg clone http://selenic.com/hg#stable |
|
1135 | 1135 | |
|
1136 | 1136 | See :hg:`help urls` for details on specifying URLs. |
|
1137 | 1137 | |
|
1138 | 1138 | Returns 0 on success. |
|
1139 | 1139 | """ |
|
1140 | 1140 | if opts.get('noupdate') and opts.get('updaterev'): |
|
1141 | 1141 | raise util.Abort(_("cannot specify both --noupdate and --updaterev")) |
|
1142 | 1142 | |
|
1143 | 1143 | r = hg.clone(ui, opts, source, dest, |
|
1144 | 1144 | pull=opts.get('pull'), |
|
1145 | 1145 | stream=opts.get('uncompressed'), |
|
1146 | 1146 | rev=opts.get('rev'), |
|
1147 | 1147 | update=opts.get('updaterev') or not opts.get('noupdate'), |
|
1148 | 1148 | branch=opts.get('branch')) |
|
1149 | 1149 | |
|
1150 | 1150 | return r is None |
|
1151 | 1151 | |
|
1152 | 1152 | @command('^commit|ci', |
|
1153 | 1153 | [('A', 'addremove', None, |
|
1154 | 1154 | _('mark new/missing files as added/removed before committing')), |
|
1155 | 1155 | ('', 'close-branch', None, |
|
1156 | 1156 | _('mark a branch as closed, hiding it from the branch list')), |
|
1157 | 1157 | ] + walkopts + commitopts + commitopts2 + subrepoopts, |
|
1158 | 1158 | _('[OPTION]... [FILE]...')) |
|
1159 | 1159 | def commit(ui, repo, *pats, **opts): |
|
1160 | 1160 | """commit the specified files or all outstanding changes |
|
1161 | 1161 | |
|
1162 | 1162 | Commit changes to the given files into the repository. Unlike a |
|
1163 | 1163 | centralized SCM, this operation is a local operation. See |
|
1164 | 1164 | :hg:`push` for a way to actively distribute your changes. |
|
1165 | 1165 | |
|
1166 | 1166 | If a list of files is omitted, all changes reported by :hg:`status` |
|
1167 | 1167 | will be committed. |
|
1168 | 1168 | |
|
1169 | 1169 | If you are committing the result of a merge, do not provide any |
|
1170 | 1170 | filenames or -I/-X filters. |
|
1171 | 1171 | |
|
1172 | 1172 | If no commit message is specified, Mercurial starts your |
|
1173 | 1173 | configured editor where you can enter a message. In case your |
|
1174 | 1174 | commit fails, you will find a backup of your message in |
|
1175 | 1175 | ``.hg/last-message.txt``. |
|
1176 | 1176 | |
|
1177 | 1177 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
1178 | 1178 | |
|
1179 | 1179 | Returns 0 on success, 1 if nothing changed. |
|
1180 | 1180 | """ |
|
1181 | 1181 | if opts.get('subrepos'): |
|
1182 | 1182 | # Let --subrepos on the command line overide config setting. |
|
1183 | 1183 | ui.setconfig('ui', 'commitsubrepos', True) |
|
1184 | 1184 | |
|
1185 | 1185 | extra = {} |
|
1186 | 1186 | if opts.get('close_branch'): |
|
1187 | 1187 | if repo['.'].node() not in repo.branchheads(): |
|
1188 | 1188 | # The topo heads set is included in the branch heads set of the |
|
1189 | 1189 | # current branch, so it's sufficient to test branchheads |
|
1190 | 1190 | raise util.Abort(_('can only close branch heads')) |
|
1191 | 1191 | extra['close'] = 1 |
|
1192 | 1192 | e = cmdutil.commiteditor |
|
1193 | 1193 | if opts.get('force_editor'): |
|
1194 | 1194 | e = cmdutil.commitforceeditor |
|
1195 | 1195 | |
|
1196 | 1196 | def commitfunc(ui, repo, message, match, opts): |
|
1197 | 1197 | return repo.commit(message, opts.get('user'), opts.get('date'), match, |
|
1198 | 1198 | editor=e, extra=extra) |
|
1199 | 1199 | |
|
1200 | 1200 | branch = repo[None].branch() |
|
1201 | 1201 | bheads = repo.branchheads(branch) |
|
1202 | 1202 | |
|
1203 | 1203 | node = cmdutil.commit(ui, repo, commitfunc, pats, opts) |
|
1204 | 1204 | if not node: |
|
1205 | 1205 | stat = repo.status(match=scmutil.match(repo[None], pats, opts)) |
|
1206 | 1206 | if stat[3]: |
|
1207 | 1207 | ui.status(_("nothing changed (%d missing files, see 'hg status')\n") |
|
1208 | 1208 | % len(stat[3])) |
|
1209 | 1209 | else: |
|
1210 | 1210 | ui.status(_("nothing changed\n")) |
|
1211 | 1211 | return 1 |
|
1212 | 1212 | |
|
1213 | 1213 | ctx = repo[node] |
|
1214 | 1214 | parents = ctx.parents() |
|
1215 | 1215 | |
|
1216 | 1216 | if (bheads and node not in bheads and not |
|
1217 | 1217 | [x for x in parents if x.node() in bheads and x.branch() == branch]): |
|
1218 | 1218 | ui.status(_('created new head\n')) |
|
1219 | 1219 | # The message is not printed for initial roots. For the other |
|
1220 | 1220 | # changesets, it is printed in the following situations: |
|
1221 | 1221 | # |
|
1222 | 1222 | # Par column: for the 2 parents with ... |
|
1223 | 1223 | # N: null or no parent |
|
1224 | 1224 | # B: parent is on another named branch |
|
1225 | 1225 | # C: parent is a regular non head changeset |
|
1226 | 1226 | # H: parent was a branch head of the current branch |
|
1227 | 1227 | # Msg column: whether we print "created new head" message |
|
1228 | 1228 | # In the following, it is assumed that there already exists some |
|
1229 | 1229 | # initial branch heads of the current branch, otherwise nothing is |
|
1230 | 1230 | # printed anyway. |
|
1231 | 1231 | # |
|
1232 | 1232 | # Par Msg Comment |
|
1233 | 1233 | # NN y additional topo root |
|
1234 | 1234 | # |
|
1235 | 1235 | # BN y additional branch root |
|
1236 | 1236 | # CN y additional topo head |
|
1237 | 1237 | # HN n usual case |
|
1238 | 1238 | # |
|
1239 | 1239 | # BB y weird additional branch root |
|
1240 | 1240 | # CB y branch merge |
|
1241 | 1241 | # HB n merge with named branch |
|
1242 | 1242 | # |
|
1243 | 1243 | # CC y additional head from merge |
|
1244 | 1244 | # CH n merge with a head |
|
1245 | 1245 | # |
|
1246 | 1246 | # HH n head merge: head count decreases |
|
1247 | 1247 | |
|
1248 | 1248 | if not opts.get('close_branch'): |
|
1249 | 1249 | for r in parents: |
|
1250 | 1250 | if r.extra().get('close') and r.branch() == branch: |
|
1251 | 1251 | ui.status(_('reopening closed branch head %d\n') % r) |
|
1252 | 1252 | |
|
1253 | 1253 | if ui.debugflag: |
|
1254 | 1254 | ui.write(_('committed changeset %d:%s\n') % (int(ctx), ctx.hex())) |
|
1255 | 1255 | elif ui.verbose: |
|
1256 | 1256 | ui.write(_('committed changeset %d:%s\n') % (int(ctx), ctx)) |
|
1257 | 1257 | |
|
1258 | 1258 | @command('copy|cp', |
|
1259 | 1259 | [('A', 'after', None, _('record a copy that has already occurred')), |
|
1260 | 1260 | ('f', 'force', None, _('forcibly copy over an existing managed file')), |
|
1261 | 1261 | ] + walkopts + dryrunopts, |
|
1262 | 1262 | _('[OPTION]... [SOURCE]... DEST')) |
|
1263 | 1263 | def copy(ui, repo, *pats, **opts): |
|
1264 | 1264 | """mark files as copied for the next commit |
|
1265 | 1265 | |
|
1266 | 1266 | Mark dest as having copies of source files. If dest is a |
|
1267 | 1267 | directory, copies are put in that directory. If dest is a file, |
|
1268 | 1268 | the source must be a single file. |
|
1269 | 1269 | |
|
1270 | 1270 | By default, this command copies the contents of files as they |
|
1271 | 1271 | exist in the working directory. If invoked with -A/--after, the |
|
1272 | 1272 | operation is recorded, but no copying is performed. |
|
1273 | 1273 | |
|
1274 | 1274 | This command takes effect with the next commit. To undo a copy |
|
1275 | 1275 | before that, see :hg:`revert`. |
|
1276 | 1276 | |
|
1277 | 1277 | Returns 0 on success, 1 if errors are encountered. |
|
1278 | 1278 | """ |
|
1279 | 1279 | wlock = repo.wlock(False) |
|
1280 | 1280 | try: |
|
1281 | 1281 | return cmdutil.copy(ui, repo, pats, opts) |
|
1282 | 1282 | finally: |
|
1283 | 1283 | wlock.release() |
|
1284 | 1284 | |
|
1285 | 1285 | @command('debugancestor', [], _('[INDEX] REV1 REV2')) |
|
1286 | 1286 | def debugancestor(ui, repo, *args): |
|
1287 | 1287 | """find the ancestor revision of two revisions in a given index""" |
|
1288 | 1288 | if len(args) == 3: |
|
1289 | 1289 | index, rev1, rev2 = args |
|
1290 | 1290 | r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), index) |
|
1291 | 1291 | lookup = r.lookup |
|
1292 | 1292 | elif len(args) == 2: |
|
1293 | 1293 | if not repo: |
|
1294 | 1294 | raise util.Abort(_("there is no Mercurial repository here " |
|
1295 | 1295 | "(.hg not found)")) |
|
1296 | 1296 | rev1, rev2 = args |
|
1297 | 1297 | r = repo.changelog |
|
1298 | 1298 | lookup = repo.lookup |
|
1299 | 1299 | else: |
|
1300 | 1300 | raise util.Abort(_('either two or three arguments required')) |
|
1301 | 1301 | a = r.ancestor(lookup(rev1), lookup(rev2)) |
|
1302 | 1302 | ui.write("%d:%s\n" % (r.rev(a), hex(a))) |
|
1303 | 1303 | |
|
1304 | 1304 | @command('debugbuilddag', |
|
1305 | 1305 | [('m', 'mergeable-file', None, _('add single file mergeable changes')), |
|
1306 | 1306 | ('o', 'overwritten-file', None, _('add single file all revs overwrite')), |
|
1307 | 1307 | ('n', 'new-file', None, _('add new file at each rev'))], |
|
1308 | 1308 | _('[OPTION]... [TEXT]')) |
|
1309 | 1309 | def debugbuilddag(ui, repo, text=None, |
|
1310 | 1310 | mergeable_file=False, |
|
1311 | 1311 | overwritten_file=False, |
|
1312 | 1312 | new_file=False): |
|
1313 | 1313 | """builds a repo with a given DAG from scratch in the current empty repo |
|
1314 | 1314 | |
|
1315 | 1315 | The description of the DAG is read from stdin if not given on the |
|
1316 | 1316 | command line. |
|
1317 | 1317 | |
|
1318 | 1318 | Elements: |
|
1319 | 1319 | |
|
1320 | 1320 | - "+n" is a linear run of n nodes based on the current default parent |
|
1321 | 1321 | - "." is a single node based on the current default parent |
|
1322 | 1322 | - "$" resets the default parent to null (implied at the start); |
|
1323 | 1323 | otherwise the default parent is always the last node created |
|
1324 | 1324 | - "<p" sets the default parent to the backref p |
|
1325 | 1325 | - "*p" is a fork at parent p, which is a backref |
|
1326 | 1326 | - "*p1/p2" is a merge of parents p1 and p2, which are backrefs |
|
1327 | 1327 | - "/p2" is a merge of the preceding node and p2 |
|
1328 | 1328 | - ":tag" defines a local tag for the preceding node |
|
1329 | 1329 | - "@branch" sets the named branch for subsequent nodes |
|
1330 | 1330 | - "#...\\n" is a comment up to the end of the line |
|
1331 | 1331 | |
|
1332 | 1332 | Whitespace between the above elements is ignored. |
|
1333 | 1333 | |
|
1334 | 1334 | A backref is either |
|
1335 | 1335 | |
|
1336 | 1336 | - a number n, which references the node curr-n, where curr is the current |
|
1337 | 1337 | node, or |
|
1338 | 1338 | - the name of a local tag you placed earlier using ":tag", or |
|
1339 | 1339 | - empty to denote the default parent. |
|
1340 | 1340 | |
|
1341 | 1341 | All string valued-elements are either strictly alphanumeric, or must |
|
1342 | 1342 | be enclosed in double quotes ("..."), with "\\" as escape character. |
|
1343 | 1343 | """ |
|
1344 | 1344 | |
|
1345 | 1345 | if text is None: |
|
1346 | 1346 | ui.status(_("reading DAG from stdin\n")) |
|
1347 | 1347 | text = ui.fin.read() |
|
1348 | 1348 | |
|
1349 | 1349 | cl = repo.changelog |
|
1350 | 1350 | if len(cl) > 0: |
|
1351 | 1351 | raise util.Abort(_('repository is not empty')) |
|
1352 | 1352 | |
|
1353 | 1353 | # determine number of revs in DAG |
|
1354 | 1354 | total = 0 |
|
1355 | 1355 | for type, data in dagparser.parsedag(text): |
|
1356 | 1356 | if type == 'n': |
|
1357 | 1357 | total += 1 |
|
1358 | 1358 | |
|
1359 | 1359 | if mergeable_file: |
|
1360 | 1360 | linesperrev = 2 |
|
1361 | 1361 | # make a file with k lines per rev |
|
1362 | 1362 | initialmergedlines = [str(i) for i in xrange(0, total * linesperrev)] |
|
1363 | 1363 | initialmergedlines.append("") |
|
1364 | 1364 | |
|
1365 | 1365 | tags = [] |
|
1366 | 1366 | |
|
1367 | 1367 | tr = repo.transaction("builddag") |
|
1368 | 1368 | try: |
|
1369 | 1369 | |
|
1370 | 1370 | at = -1 |
|
1371 | 1371 | atbranch = 'default' |
|
1372 | 1372 | nodeids = [] |
|
1373 | 1373 | ui.progress(_('building'), 0, unit=_('revisions'), total=total) |
|
1374 | 1374 | for type, data in dagparser.parsedag(text): |
|
1375 | 1375 | if type == 'n': |
|
1376 | 1376 | ui.note('node %s\n' % str(data)) |
|
1377 | 1377 | id, ps = data |
|
1378 | 1378 | |
|
1379 | 1379 | files = [] |
|
1380 | 1380 | fctxs = {} |
|
1381 | 1381 | |
|
1382 | 1382 | p2 = None |
|
1383 | 1383 | if mergeable_file: |
|
1384 | 1384 | fn = "mf" |
|
1385 | 1385 | p1 = repo[ps[0]] |
|
1386 | 1386 | if len(ps) > 1: |
|
1387 | 1387 | p2 = repo[ps[1]] |
|
1388 | 1388 | pa = p1.ancestor(p2) |
|
1389 | 1389 | base, local, other = [x[fn].data() for x in pa, p1, p2] |
|
1390 | 1390 | m3 = simplemerge.Merge3Text(base, local, other) |
|
1391 | 1391 | ml = [l.strip() for l in m3.merge_lines()] |
|
1392 | 1392 | ml.append("") |
|
1393 | 1393 | elif at > 0: |
|
1394 | 1394 | ml = p1[fn].data().split("\n") |
|
1395 | 1395 | else: |
|
1396 | 1396 | ml = initialmergedlines |
|
1397 | 1397 | ml[id * linesperrev] += " r%i" % id |
|
1398 | 1398 | mergedtext = "\n".join(ml) |
|
1399 | 1399 | files.append(fn) |
|
1400 | 1400 | fctxs[fn] = context.memfilectx(fn, mergedtext) |
|
1401 | 1401 | |
|
1402 | 1402 | if overwritten_file: |
|
1403 | 1403 | fn = "of" |
|
1404 | 1404 | files.append(fn) |
|
1405 | 1405 | fctxs[fn] = context.memfilectx(fn, "r%i\n" % id) |
|
1406 | 1406 | |
|
1407 | 1407 | if new_file: |
|
1408 | 1408 | fn = "nf%i" % id |
|
1409 | 1409 | files.append(fn) |
|
1410 | 1410 | fctxs[fn] = context.memfilectx(fn, "r%i\n" % id) |
|
1411 | 1411 | if len(ps) > 1: |
|
1412 | 1412 | if not p2: |
|
1413 | 1413 | p2 = repo[ps[1]] |
|
1414 | 1414 | for fn in p2: |
|
1415 | 1415 | if fn.startswith("nf"): |
|
1416 | 1416 | files.append(fn) |
|
1417 | 1417 | fctxs[fn] = p2[fn] |
|
1418 | 1418 | |
|
1419 | 1419 | def fctxfn(repo, cx, path): |
|
1420 | 1420 | return fctxs.get(path) |
|
1421 | 1421 | |
|
1422 | 1422 | if len(ps) == 0 or ps[0] < 0: |
|
1423 | 1423 | pars = [None, None] |
|
1424 | 1424 | elif len(ps) == 1: |
|
1425 | 1425 | pars = [nodeids[ps[0]], None] |
|
1426 | 1426 | else: |
|
1427 | 1427 | pars = [nodeids[p] for p in ps] |
|
1428 | 1428 | cx = context.memctx(repo, pars, "r%i" % id, files, fctxfn, |
|
1429 | 1429 | date=(id, 0), |
|
1430 | 1430 | user="debugbuilddag", |
|
1431 | 1431 | extra={'branch': atbranch}) |
|
1432 | 1432 | nodeid = repo.commitctx(cx) |
|
1433 | 1433 | nodeids.append(nodeid) |
|
1434 | 1434 | at = id |
|
1435 | 1435 | elif type == 'l': |
|
1436 | 1436 | id, name = data |
|
1437 | 1437 | ui.note('tag %s\n' % name) |
|
1438 | 1438 | tags.append("%s %s\n" % (hex(repo.changelog.node(id)), name)) |
|
1439 | 1439 | elif type == 'a': |
|
1440 | 1440 | ui.note('branch %s\n' % data) |
|
1441 | 1441 | atbranch = data |
|
1442 | 1442 | ui.progress(_('building'), id, unit=_('revisions'), total=total) |
|
1443 | 1443 | tr.close() |
|
1444 | 1444 | finally: |
|
1445 | 1445 | ui.progress(_('building'), None) |
|
1446 | 1446 | tr.release() |
|
1447 | 1447 | |
|
1448 | 1448 | if tags: |
|
1449 | 1449 | repo.opener.write("localtags", "".join(tags)) |
|
1450 | 1450 | |
|
1451 | 1451 | @command('debugbundle', [('a', 'all', None, _('show all details'))], _('FILE')) |
|
1452 | 1452 | def debugbundle(ui, bundlepath, all=None, **opts): |
|
1453 | 1453 | """lists the contents of a bundle""" |
|
1454 | 1454 | f = url.open(ui, bundlepath) |
|
1455 | 1455 | try: |
|
1456 | 1456 | gen = changegroup.readbundle(f, bundlepath) |
|
1457 | 1457 | if all: |
|
1458 | 1458 | ui.write("format: id, p1, p2, cset, delta base, len(delta)\n") |
|
1459 | 1459 | |
|
1460 | 1460 | def showchunks(named): |
|
1461 | 1461 | ui.write("\n%s\n" % named) |
|
1462 | 1462 | chain = None |
|
1463 | 1463 | while True: |
|
1464 | 1464 | chunkdata = gen.deltachunk(chain) |
|
1465 | 1465 | if not chunkdata: |
|
1466 | 1466 | break |
|
1467 | 1467 | node = chunkdata['node'] |
|
1468 | 1468 | p1 = chunkdata['p1'] |
|
1469 | 1469 | p2 = chunkdata['p2'] |
|
1470 | 1470 | cs = chunkdata['cs'] |
|
1471 | 1471 | deltabase = chunkdata['deltabase'] |
|
1472 | 1472 | delta = chunkdata['delta'] |
|
1473 | 1473 | ui.write("%s %s %s %s %s %s\n" % |
|
1474 | 1474 | (hex(node), hex(p1), hex(p2), |
|
1475 | 1475 | hex(cs), hex(deltabase), len(delta))) |
|
1476 | 1476 | chain = node |
|
1477 | 1477 | |
|
1478 | 1478 | chunkdata = gen.changelogheader() |
|
1479 | 1479 | showchunks("changelog") |
|
1480 | 1480 | chunkdata = gen.manifestheader() |
|
1481 | 1481 | showchunks("manifest") |
|
1482 | 1482 | while True: |
|
1483 | 1483 | chunkdata = gen.filelogheader() |
|
1484 | 1484 | if not chunkdata: |
|
1485 | 1485 | break |
|
1486 | 1486 | fname = chunkdata['filename'] |
|
1487 | 1487 | showchunks(fname) |
|
1488 | 1488 | else: |
|
1489 | 1489 | chunkdata = gen.changelogheader() |
|
1490 | 1490 | chain = None |
|
1491 | 1491 | while True: |
|
1492 | 1492 | chunkdata = gen.deltachunk(chain) |
|
1493 | 1493 | if not chunkdata: |
|
1494 | 1494 | break |
|
1495 | 1495 | node = chunkdata['node'] |
|
1496 | 1496 | ui.write("%s\n" % hex(node)) |
|
1497 | 1497 | chain = node |
|
1498 | 1498 | finally: |
|
1499 | 1499 | f.close() |
|
1500 | 1500 | |
|
1501 | 1501 | @command('debugcheckstate', [], '') |
|
1502 | 1502 | def debugcheckstate(ui, repo): |
|
1503 | 1503 | """validate the correctness of the current dirstate""" |
|
1504 | 1504 | parent1, parent2 = repo.dirstate.parents() |
|
1505 | 1505 | m1 = repo[parent1].manifest() |
|
1506 | 1506 | m2 = repo[parent2].manifest() |
|
1507 | 1507 | errors = 0 |
|
1508 | 1508 | for f in repo.dirstate: |
|
1509 | 1509 | state = repo.dirstate[f] |
|
1510 | 1510 | if state in "nr" and f not in m1: |
|
1511 | 1511 | ui.warn(_("%s in state %s, but not in manifest1\n") % (f, state)) |
|
1512 | 1512 | errors += 1 |
|
1513 | 1513 | if state in "a" and f in m1: |
|
1514 | 1514 | ui.warn(_("%s in state %s, but also in manifest1\n") % (f, state)) |
|
1515 | 1515 | errors += 1 |
|
1516 | 1516 | if state in "m" and f not in m1 and f not in m2: |
|
1517 | 1517 | ui.warn(_("%s in state %s, but not in either manifest\n") % |
|
1518 | 1518 | (f, state)) |
|
1519 | 1519 | errors += 1 |
|
1520 | 1520 | for f in m1: |
|
1521 | 1521 | state = repo.dirstate[f] |
|
1522 | 1522 | if state not in "nrm": |
|
1523 | 1523 | ui.warn(_("%s in manifest1, but listed as state %s") % (f, state)) |
|
1524 | 1524 | errors += 1 |
|
1525 | 1525 | if errors: |
|
1526 | 1526 | error = _(".hg/dirstate inconsistent with current parent's manifest") |
|
1527 | 1527 | raise util.Abort(error) |
|
1528 | 1528 | |
|
1529 | 1529 | @command('debugcommands', [], _('[COMMAND]')) |
|
1530 | 1530 | def debugcommands(ui, cmd='', *args): |
|
1531 | 1531 | """list all available commands and options""" |
|
1532 | 1532 | for cmd, vals in sorted(table.iteritems()): |
|
1533 | 1533 | cmd = cmd.split('|')[0].strip('^') |
|
1534 | 1534 | opts = ', '.join([i[1] for i in vals[1]]) |
|
1535 | 1535 | ui.write('%s: %s\n' % (cmd, opts)) |
|
1536 | 1536 | |
|
1537 | 1537 | @command('debugcomplete', |
|
1538 | 1538 | [('o', 'options', None, _('show the command options'))], |
|
1539 | 1539 | _('[-o] CMD')) |
|
1540 | 1540 | def debugcomplete(ui, cmd='', **opts): |
|
1541 | 1541 | """returns the completion list associated with the given command""" |
|
1542 | 1542 | |
|
1543 | 1543 | if opts.get('options'): |
|
1544 | 1544 | options = [] |
|
1545 | 1545 | otables = [globalopts] |
|
1546 | 1546 | if cmd: |
|
1547 | 1547 | aliases, entry = cmdutil.findcmd(cmd, table, False) |
|
1548 | 1548 | otables.append(entry[1]) |
|
1549 | 1549 | for t in otables: |
|
1550 | 1550 | for o in t: |
|
1551 | 1551 | if "(DEPRECATED)" in o[3]: |
|
1552 | 1552 | continue |
|
1553 | 1553 | if o[0]: |
|
1554 | 1554 | options.append('-%s' % o[0]) |
|
1555 | 1555 | options.append('--%s' % o[1]) |
|
1556 | 1556 | ui.write("%s\n" % "\n".join(options)) |
|
1557 | 1557 | return |
|
1558 | 1558 | |
|
1559 | 1559 | cmdlist = cmdutil.findpossible(cmd, table) |
|
1560 | 1560 | if ui.verbose: |
|
1561 | 1561 | cmdlist = [' '.join(c[0]) for c in cmdlist.values()] |
|
1562 | 1562 | ui.write("%s\n" % "\n".join(sorted(cmdlist))) |
|
1563 | 1563 | |
|
1564 | 1564 | @command('debugdag', |
|
1565 | 1565 | [('t', 'tags', None, _('use tags as labels')), |
|
1566 | 1566 | ('b', 'branches', None, _('annotate with branch names')), |
|
1567 | 1567 | ('', 'dots', None, _('use dots for runs')), |
|
1568 | 1568 | ('s', 'spaces', None, _('separate elements by spaces'))], |
|
1569 | 1569 | _('[OPTION]... [FILE [REV]...]')) |
|
1570 | 1570 | def debugdag(ui, repo, file_=None, *revs, **opts): |
|
1571 | 1571 | """format the changelog or an index DAG as a concise textual description |
|
1572 | 1572 | |
|
1573 | 1573 | If you pass a revlog index, the revlog's DAG is emitted. If you list |
|
1574 | 1574 | revision numbers, they get labelled in the output as rN. |
|
1575 | 1575 | |
|
1576 | 1576 | Otherwise, the changelog DAG of the current repo is emitted. |
|
1577 | 1577 | """ |
|
1578 | 1578 | spaces = opts.get('spaces') |
|
1579 | 1579 | dots = opts.get('dots') |
|
1580 | 1580 | if file_: |
|
1581 | 1581 | rlog = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), file_) |
|
1582 | 1582 | revs = set((int(r) for r in revs)) |
|
1583 | 1583 | def events(): |
|
1584 | 1584 | for r in rlog: |
|
1585 | 1585 | yield 'n', (r, list(set(p for p in rlog.parentrevs(r) if p != -1))) |
|
1586 | 1586 | if r in revs: |
|
1587 | 1587 | yield 'l', (r, "r%i" % r) |
|
1588 | 1588 | elif repo: |
|
1589 | 1589 | cl = repo.changelog |
|
1590 | 1590 | tags = opts.get('tags') |
|
1591 | 1591 | branches = opts.get('branches') |
|
1592 | 1592 | if tags: |
|
1593 | 1593 | labels = {} |
|
1594 | 1594 | for l, n in repo.tags().items(): |
|
1595 | 1595 | labels.setdefault(cl.rev(n), []).append(l) |
|
1596 | 1596 | def events(): |
|
1597 | 1597 | b = "default" |
|
1598 | 1598 | for r in cl: |
|
1599 | 1599 | if branches: |
|
1600 | 1600 | newb = cl.read(cl.node(r))[5]['branch'] |
|
1601 | 1601 | if newb != b: |
|
1602 | 1602 | yield 'a', newb |
|
1603 | 1603 | b = newb |
|
1604 | 1604 | yield 'n', (r, list(set(p for p in cl.parentrevs(r) if p != -1))) |
|
1605 | 1605 | if tags: |
|
1606 | 1606 | ls = labels.get(r) |
|
1607 | 1607 | if ls: |
|
1608 | 1608 | for l in ls: |
|
1609 | 1609 | yield 'l', (r, l) |
|
1610 | 1610 | else: |
|
1611 | 1611 | raise util.Abort(_('need repo for changelog dag')) |
|
1612 | 1612 | |
|
1613 | 1613 | for line in dagparser.dagtextlines(events(), |
|
1614 | 1614 | addspaces=spaces, |
|
1615 | 1615 | wraplabels=True, |
|
1616 | 1616 | wrapannotations=True, |
|
1617 | 1617 | wrapnonlinear=dots, |
|
1618 | 1618 | usedots=dots, |
|
1619 | 1619 | maxlinewidth=70): |
|
1620 | 1620 | ui.write(line) |
|
1621 | 1621 | ui.write("\n") |
|
1622 | 1622 | |
|
1623 | 1623 | @command('debugdata', |
|
1624 | 1624 | [('c', 'changelog', False, _('open changelog')), |
|
1625 | 1625 | ('m', 'manifest', False, _('open manifest'))], |
|
1626 | 1626 | _('-c|-m|FILE REV')) |
|
1627 | 1627 | def debugdata(ui, repo, file_, rev = None, **opts): |
|
1628 | 1628 | """dump the contents of a data file revision""" |
|
1629 | 1629 | if opts.get('changelog') or opts.get('manifest'): |
|
1630 | 1630 | file_, rev = None, file_ |
|
1631 | 1631 | elif rev is None: |
|
1632 | 1632 | raise error.CommandError('debugdata', _('invalid arguments')) |
|
1633 | 1633 | r = cmdutil.openrevlog(repo, 'debugdata', file_, opts) |
|
1634 | 1634 | try: |
|
1635 | 1635 | ui.write(r.revision(r.lookup(rev))) |
|
1636 | 1636 | except KeyError: |
|
1637 | 1637 | raise util.Abort(_('invalid revision identifier %s') % rev) |
|
1638 | 1638 | |
|
1639 | 1639 | @command('debugdate', |
|
1640 | 1640 | [('e', 'extended', None, _('try extended date formats'))], |
|
1641 | 1641 | _('[-e] DATE [RANGE]')) |
|
1642 | 1642 | def debugdate(ui, date, range=None, **opts): |
|
1643 | 1643 | """parse and display a date""" |
|
1644 | 1644 | if opts["extended"]: |
|
1645 | 1645 | d = util.parsedate(date, util.extendeddateformats) |
|
1646 | 1646 | else: |
|
1647 | 1647 | d = util.parsedate(date) |
|
1648 | 1648 | ui.write("internal: %s %s\n" % d) |
|
1649 | 1649 | ui.write("standard: %s\n" % util.datestr(d)) |
|
1650 | 1650 | if range: |
|
1651 | 1651 | m = util.matchdate(range) |
|
1652 | 1652 | ui.write("match: %s\n" % m(d[0])) |
|
1653 | 1653 | |
|
1654 | 1654 | @command('debugdiscovery', |
|
1655 | 1655 | [('', 'old', None, _('use old-style discovery')), |
|
1656 | 1656 | ('', 'nonheads', None, |
|
1657 | 1657 | _('use old-style discovery with non-heads included')), |
|
1658 | 1658 | ] + remoteopts, |
|
1659 | 1659 | _('[-l REV] [-r REV] [-b BRANCH]... [OTHER]')) |
|
1660 | 1660 | def debugdiscovery(ui, repo, remoteurl="default", **opts): |
|
1661 | 1661 | """runs the changeset discovery protocol in isolation""" |
|
1662 | 1662 | remoteurl, branches = hg.parseurl(ui.expandpath(remoteurl), opts.get('branch')) |
|
1663 | 1663 | remote = hg.peer(repo, opts, remoteurl) |
|
1664 | 1664 | ui.status(_('comparing with %s\n') % util.hidepassword(remoteurl)) |
|
1665 | 1665 | |
|
1666 | 1666 | # make sure tests are repeatable |
|
1667 | 1667 | random.seed(12323) |
|
1668 | 1668 | |
|
1669 | 1669 | def doit(localheads, remoteheads): |
|
1670 | 1670 | if opts.get('old'): |
|
1671 | 1671 | if localheads: |
|
1672 | 1672 | raise util.Abort('cannot use localheads with old style discovery') |
|
1673 | 1673 | common, _in, hds = treediscovery.findcommonincoming(repo, remote, |
|
1674 | 1674 | force=True) |
|
1675 | 1675 | common = set(common) |
|
1676 | 1676 | if not opts.get('nonheads'): |
|
1677 | 1677 | ui.write("unpruned common: %s\n" % " ".join([short(n) |
|
1678 | 1678 | for n in common])) |
|
1679 | 1679 | dag = dagutil.revlogdag(repo.changelog) |
|
1680 | 1680 | all = dag.ancestorset(dag.internalizeall(common)) |
|
1681 | 1681 | common = dag.externalizeall(dag.headsetofconnecteds(all)) |
|
1682 | 1682 | else: |
|
1683 | 1683 | common, any, hds = setdiscovery.findcommonheads(ui, repo, remote) |
|
1684 | 1684 | common = set(common) |
|
1685 | 1685 | rheads = set(hds) |
|
1686 | 1686 | lheads = set(repo.heads()) |
|
1687 | 1687 | ui.write("common heads: %s\n" % " ".join([short(n) for n in common])) |
|
1688 | 1688 | if lheads <= common: |
|
1689 | 1689 | ui.write("local is subset\n") |
|
1690 | 1690 | elif rheads <= common: |
|
1691 | 1691 | ui.write("remote is subset\n") |
|
1692 | 1692 | |
|
1693 | 1693 | serverlogs = opts.get('serverlog') |
|
1694 | 1694 | if serverlogs: |
|
1695 | 1695 | for filename in serverlogs: |
|
1696 | 1696 | logfile = open(filename, 'r') |
|
1697 | 1697 | try: |
|
1698 | 1698 | line = logfile.readline() |
|
1699 | 1699 | while line: |
|
1700 | 1700 | parts = line.strip().split(';') |
|
1701 | 1701 | op = parts[1] |
|
1702 | 1702 | if op == 'cg': |
|
1703 | 1703 | pass |
|
1704 | 1704 | elif op == 'cgss': |
|
1705 | 1705 | doit(parts[2].split(' '), parts[3].split(' ')) |
|
1706 | 1706 | elif op == 'unb': |
|
1707 | 1707 | doit(parts[3].split(' '), parts[2].split(' ')) |
|
1708 | 1708 | line = logfile.readline() |
|
1709 | 1709 | finally: |
|
1710 | 1710 | logfile.close() |
|
1711 | 1711 | |
|
1712 | 1712 | else: |
|
1713 | 1713 | remoterevs, _checkout = hg.addbranchrevs(repo, remote, branches, |
|
1714 | 1714 | opts.get('remote_head')) |
|
1715 | 1715 | localrevs = opts.get('local_head') |
|
1716 | 1716 | doit(localrevs, remoterevs) |
|
1717 | 1717 | |
|
1718 | 1718 | @command('debugfileset', [], ('REVSPEC')) |
|
1719 | 1719 | def debugfileset(ui, repo, expr): |
|
1720 | 1720 | '''parse and apply a fileset specification''' |
|
1721 | 1721 | if ui.verbose: |
|
1722 | 1722 | tree = fileset.parse(expr)[0] |
|
1723 | 1723 | ui.note(tree, "\n") |
|
1724 | 1724 | |
|
1725 | 1725 | for f in fileset.getfileset(repo[None], expr): |
|
1726 | 1726 | ui.write("%s\n" % f) |
|
1727 | 1727 | |
|
1728 | 1728 | @command('debugfsinfo', [], _('[PATH]')) |
|
1729 | 1729 | def debugfsinfo(ui, path = "."): |
|
1730 | 1730 | """show information detected about current filesystem""" |
|
1731 | 1731 | util.writefile('.debugfsinfo', '') |
|
1732 | 1732 | ui.write('exec: %s\n' % (util.checkexec(path) and 'yes' or 'no')) |
|
1733 | 1733 | ui.write('symlink: %s\n' % (util.checklink(path) and 'yes' or 'no')) |
|
1734 | 1734 | ui.write('case-sensitive: %s\n' % (util.checkcase('.debugfsinfo') |
|
1735 | 1735 | and 'yes' or 'no')) |
|
1736 | 1736 | os.unlink('.debugfsinfo') |
|
1737 | 1737 | |
|
1738 | 1738 | @command('debuggetbundle', |
|
1739 | 1739 | [('H', 'head', [], _('id of head node'), _('ID')), |
|
1740 | 1740 | ('C', 'common', [], _('id of common node'), _('ID')), |
|
1741 | 1741 | ('t', 'type', 'bzip2', _('bundle compression type to use'), _('TYPE'))], |
|
1742 | 1742 | _('REPO FILE [-H|-C ID]...')) |
|
1743 | 1743 | def debuggetbundle(ui, repopath, bundlepath, head=None, common=None, **opts): |
|
1744 | 1744 | """retrieves a bundle from a repo |
|
1745 | 1745 | |
|
1746 | 1746 | Every ID must be a full-length hex node id string. Saves the bundle to the |
|
1747 | 1747 | given file. |
|
1748 | 1748 | """ |
|
1749 | 1749 | repo = hg.peer(ui, opts, repopath) |
|
1750 | 1750 | if not repo.capable('getbundle'): |
|
1751 | 1751 | raise util.Abort("getbundle() not supported by target repository") |
|
1752 | 1752 | args = {} |
|
1753 | 1753 | if common: |
|
1754 | 1754 | args['common'] = [bin(s) for s in common] |
|
1755 | 1755 | if head: |
|
1756 | 1756 | args['heads'] = [bin(s) for s in head] |
|
1757 | 1757 | bundle = repo.getbundle('debug', **args) |
|
1758 | 1758 | |
|
1759 | 1759 | bundletype = opts.get('type', 'bzip2').lower() |
|
1760 | 1760 | btypes = {'none': 'HG10UN', 'bzip2': 'HG10BZ', 'gzip': 'HG10GZ'} |
|
1761 | 1761 | bundletype = btypes.get(bundletype) |
|
1762 | 1762 | if bundletype not in changegroup.bundletypes: |
|
1763 | 1763 | raise util.Abort(_('unknown bundle type specified with --type')) |
|
1764 | 1764 | changegroup.writebundle(bundle, bundlepath, bundletype) |
|
1765 | 1765 | |
|
1766 | 1766 | @command('debugignore', [], '') |
|
1767 | 1767 | def debugignore(ui, repo, *values, **opts): |
|
1768 | 1768 | """display the combined ignore pattern""" |
|
1769 | 1769 | ignore = repo.dirstate._ignore |
|
1770 | 1770 | includepat = getattr(ignore, 'includepat', None) |
|
1771 | 1771 | if includepat is not None: |
|
1772 | 1772 | ui.write("%s\n" % includepat) |
|
1773 | 1773 | else: |
|
1774 | 1774 | raise util.Abort(_("no ignore patterns found")) |
|
1775 | 1775 | |
|
1776 | 1776 | @command('debugindex', |
|
1777 | 1777 | [('c', 'changelog', False, _('open changelog')), |
|
1778 | 1778 | ('m', 'manifest', False, _('open manifest')), |
|
1779 | 1779 | ('f', 'format', 0, _('revlog format'), _('FORMAT'))], |
|
1780 | 1780 | _('[-f FORMAT] -c|-m|FILE')) |
|
1781 | 1781 | def debugindex(ui, repo, file_ = None, **opts): |
|
1782 | 1782 | """dump the contents of an index file""" |
|
1783 | 1783 | r = cmdutil.openrevlog(repo, 'debugindex', file_, opts) |
|
1784 | 1784 | format = opts.get('format', 0) |
|
1785 | 1785 | if format not in (0, 1): |
|
1786 | 1786 | raise util.Abort(_("unknown format %d") % format) |
|
1787 | 1787 | |
|
1788 | 1788 | generaldelta = r.version & revlog.REVLOGGENERALDELTA |
|
1789 | 1789 | if generaldelta: |
|
1790 | 1790 | basehdr = ' delta' |
|
1791 | 1791 | else: |
|
1792 | 1792 | basehdr = ' base' |
|
1793 | 1793 | |
|
1794 | 1794 | if format == 0: |
|
1795 | 1795 | ui.write(" rev offset length " + basehdr + " linkrev" |
|
1796 | 1796 | " nodeid p1 p2\n") |
|
1797 | 1797 | elif format == 1: |
|
1798 | 1798 | ui.write(" rev flag offset length" |
|
1799 | 1799 | " size " + basehdr + " link p1 p2 nodeid\n") |
|
1800 | 1800 | |
|
1801 | 1801 | for i in r: |
|
1802 | 1802 | node = r.node(i) |
|
1803 | 1803 | if generaldelta: |
|
1804 | 1804 | base = r.deltaparent(i) |
|
1805 | 1805 | else: |
|
1806 | 1806 | base = r.chainbase(i) |
|
1807 | 1807 | if format == 0: |
|
1808 | 1808 | try: |
|
1809 | 1809 | pp = r.parents(node) |
|
1810 | 1810 | except: |
|
1811 | 1811 | pp = [nullid, nullid] |
|
1812 | 1812 | ui.write("% 6d % 9d % 7d % 6d % 7d %s %s %s\n" % ( |
|
1813 | 1813 | i, r.start(i), r.length(i), base, r.linkrev(i), |
|
1814 | 1814 | short(node), short(pp[0]), short(pp[1]))) |
|
1815 | 1815 | elif format == 1: |
|
1816 | 1816 | pr = r.parentrevs(i) |
|
1817 | 1817 | ui.write("% 6d %04x % 8d % 8d % 8d % 6d % 6d % 6d % 6d %s\n" % ( |
|
1818 | 1818 | i, r.flags(i), r.start(i), r.length(i), r.rawsize(i), |
|
1819 | 1819 | base, r.linkrev(i), pr[0], pr[1], short(node))) |
|
1820 | 1820 | |
|
1821 | 1821 | @command('debugindexdot', [], _('FILE')) |
|
1822 | 1822 | def debugindexdot(ui, repo, file_): |
|
1823 | 1823 | """dump an index DAG as a graphviz dot file""" |
|
1824 | 1824 | r = None |
|
1825 | 1825 | if repo: |
|
1826 | 1826 | filelog = repo.file(file_) |
|
1827 | 1827 | if len(filelog): |
|
1828 | 1828 | r = filelog |
|
1829 | 1829 | if not r: |
|
1830 | 1830 | r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), file_) |
|
1831 | 1831 | ui.write("digraph G {\n") |
|
1832 | 1832 | for i in r: |
|
1833 | 1833 | node = r.node(i) |
|
1834 | 1834 | pp = r.parents(node) |
|
1835 | 1835 | ui.write("\t%d -> %d\n" % (r.rev(pp[0]), i)) |
|
1836 | 1836 | if pp[1] != nullid: |
|
1837 | 1837 | ui.write("\t%d -> %d\n" % (r.rev(pp[1]), i)) |
|
1838 | 1838 | ui.write("}\n") |
|
1839 | 1839 | |
|
1840 | 1840 | @command('debuginstall', [], '') |
|
1841 | 1841 | def debuginstall(ui): |
|
1842 | 1842 | '''test Mercurial installation |
|
1843 | 1843 | |
|
1844 | 1844 | Returns 0 on success. |
|
1845 | 1845 | ''' |
|
1846 | 1846 | |
|
1847 | 1847 | def writetemp(contents): |
|
1848 | 1848 | (fd, name) = tempfile.mkstemp(prefix="hg-debuginstall-") |
|
1849 | 1849 | f = os.fdopen(fd, "wb") |
|
1850 | 1850 | f.write(contents) |
|
1851 | 1851 | f.close() |
|
1852 | 1852 | return name |
|
1853 | 1853 | |
|
1854 | 1854 | problems = 0 |
|
1855 | 1855 | |
|
1856 | 1856 | # encoding |
|
1857 | 1857 | ui.status(_("Checking encoding (%s)...\n") % encoding.encoding) |
|
1858 | 1858 | try: |
|
1859 | 1859 | encoding.fromlocal("test") |
|
1860 | 1860 | except util.Abort, inst: |
|
1861 | 1861 | ui.write(" %s\n" % inst) |
|
1862 | 1862 | ui.write(_(" (check that your locale is properly set)\n")) |
|
1863 | 1863 | problems += 1 |
|
1864 | 1864 | |
|
1865 | 1865 | # compiled modules |
|
1866 | 1866 | ui.status(_("Checking installed modules (%s)...\n") |
|
1867 | 1867 | % os.path.dirname(__file__)) |
|
1868 | 1868 | try: |
|
1869 | 1869 | import bdiff, mpatch, base85, osutil |
|
1870 | 1870 | dir(bdiff), dir(mpatch), dir(base85), dir(osutil) # quiet pyflakes |
|
1871 | 1871 | except Exception, inst: |
|
1872 | 1872 | ui.write(" %s\n" % inst) |
|
1873 | 1873 | ui.write(_(" One or more extensions could not be found")) |
|
1874 | 1874 | ui.write(_(" (check that you compiled the extensions)\n")) |
|
1875 | 1875 | problems += 1 |
|
1876 | 1876 | |
|
1877 | 1877 | # templates |
|
1878 | 1878 | import templater |
|
1879 | 1879 | p = templater.templatepath() |
|
1880 | 1880 | ui.status(_("Checking templates (%s)...\n") % ' '.join(p)) |
|
1881 | 1881 | try: |
|
1882 | 1882 | templater.templater(templater.templatepath("map-cmdline.default")) |
|
1883 | 1883 | except Exception, inst: |
|
1884 | 1884 | ui.write(" %s\n" % inst) |
|
1885 | 1885 | ui.write(_(" (templates seem to have been installed incorrectly)\n")) |
|
1886 | 1886 | problems += 1 |
|
1887 | 1887 | |
|
1888 | 1888 | # editor |
|
1889 | 1889 | ui.status(_("Checking commit editor...\n")) |
|
1890 | 1890 | editor = ui.geteditor() |
|
1891 | 1891 | cmdpath = util.findexe(editor) or util.findexe(editor.split()[0]) |
|
1892 | 1892 | if not cmdpath: |
|
1893 | 1893 | if editor == 'vi': |
|
1894 | 1894 | ui.write(_(" No commit editor set and can't find vi in PATH\n")) |
|
1895 | 1895 | ui.write(_(" (specify a commit editor in your configuration" |
|
1896 | 1896 | " file)\n")) |
|
1897 | 1897 | else: |
|
1898 | 1898 | ui.write(_(" Can't find editor '%s' in PATH\n") % editor) |
|
1899 | 1899 | ui.write(_(" (specify a commit editor in your configuration" |
|
1900 | 1900 | " file)\n")) |
|
1901 | 1901 | problems += 1 |
|
1902 | 1902 | |
|
1903 | 1903 | # check username |
|
1904 | 1904 | ui.status(_("Checking username...\n")) |
|
1905 | 1905 | try: |
|
1906 | 1906 | ui.username() |
|
1907 | 1907 | except util.Abort, e: |
|
1908 | 1908 | ui.write(" %s\n" % e) |
|
1909 | 1909 | ui.write(_(" (specify a username in your configuration file)\n")) |
|
1910 | 1910 | problems += 1 |
|
1911 | 1911 | |
|
1912 | 1912 | if not problems: |
|
1913 | 1913 | ui.status(_("No problems detected\n")) |
|
1914 | 1914 | else: |
|
1915 | 1915 | ui.write(_("%s problems detected," |
|
1916 | 1916 | " please check your install!\n") % problems) |
|
1917 | 1917 | |
|
1918 | 1918 | return problems |
|
1919 | 1919 | |
|
1920 | 1920 | @command('debugknown', [], _('REPO ID...')) |
|
1921 | 1921 | def debugknown(ui, repopath, *ids, **opts): |
|
1922 | 1922 | """test whether node ids are known to a repo |
|
1923 | 1923 | |
|
1924 | 1924 | Every ID must be a full-length hex node id string. Returns a list of 0s and 1s |
|
1925 | 1925 | indicating unknown/known. |
|
1926 | 1926 | """ |
|
1927 | 1927 | repo = hg.peer(ui, opts, repopath) |
|
1928 | 1928 | if not repo.capable('known'): |
|
1929 | 1929 | raise util.Abort("known() not supported by target repository") |
|
1930 | 1930 | flags = repo.known([bin(s) for s in ids]) |
|
1931 | 1931 | ui.write("%s\n" % ("".join([f and "1" or "0" for f in flags]))) |
|
1932 | 1932 | |
|
1933 | 1933 | @command('debugpushkey', [], _('REPO NAMESPACE [KEY OLD NEW]')) |
|
1934 | 1934 | def debugpushkey(ui, repopath, namespace, *keyinfo, **opts): |
|
1935 | 1935 | '''access the pushkey key/value protocol |
|
1936 | 1936 | |
|
1937 | 1937 | With two args, list the keys in the given namespace. |
|
1938 | 1938 | |
|
1939 | 1939 | With five args, set a key to new if it currently is set to old. |
|
1940 | 1940 | Reports success or failure. |
|
1941 | 1941 | ''' |
|
1942 | 1942 | |
|
1943 | 1943 | target = hg.peer(ui, {}, repopath) |
|
1944 | 1944 | if keyinfo: |
|
1945 | 1945 | key, old, new = keyinfo |
|
1946 | 1946 | r = target.pushkey(namespace, key, old, new) |
|
1947 | 1947 | ui.status(str(r) + '\n') |
|
1948 | 1948 | return not r |
|
1949 | 1949 | else: |
|
1950 | 1950 | for k, v in target.listkeys(namespace).iteritems(): |
|
1951 | 1951 | ui.write("%s\t%s\n" % (k.encode('string-escape'), |
|
1952 | 1952 | v.encode('string-escape'))) |
|
1953 | 1953 | |
|
1954 | 1954 | @command('debugrebuildstate', |
|
1955 | 1955 | [('r', 'rev', '', _('revision to rebuild to'), _('REV'))], |
|
1956 | 1956 | _('[-r REV] [REV]')) |
|
1957 | 1957 | def debugrebuildstate(ui, repo, rev="tip"): |
|
1958 | 1958 | """rebuild the dirstate as it would look like for the given revision""" |
|
1959 | 1959 | ctx = scmutil.revsingle(repo, rev) |
|
1960 | 1960 | wlock = repo.wlock() |
|
1961 | 1961 | try: |
|
1962 | 1962 | repo.dirstate.rebuild(ctx.node(), ctx.manifest()) |
|
1963 | 1963 | finally: |
|
1964 | 1964 | wlock.release() |
|
1965 | 1965 | |
|
1966 | 1966 | @command('debugrename', |
|
1967 | 1967 | [('r', 'rev', '', _('revision to debug'), _('REV'))], |
|
1968 | 1968 | _('[-r REV] FILE')) |
|
1969 | 1969 | def debugrename(ui, repo, file1, *pats, **opts): |
|
1970 | 1970 | """dump rename information""" |
|
1971 | 1971 | |
|
1972 | 1972 | ctx = scmutil.revsingle(repo, opts.get('rev')) |
|
1973 | 1973 | m = scmutil.match(ctx, (file1,) + pats, opts) |
|
1974 | 1974 | for abs in ctx.walk(m): |
|
1975 | 1975 | fctx = ctx[abs] |
|
1976 | 1976 | o = fctx.filelog().renamed(fctx.filenode()) |
|
1977 | 1977 | rel = m.rel(abs) |
|
1978 | 1978 | if o: |
|
1979 | 1979 | ui.write(_("%s renamed from %s:%s\n") % (rel, o[0], hex(o[1]))) |
|
1980 | 1980 | else: |
|
1981 | 1981 | ui.write(_("%s not renamed\n") % rel) |
|
1982 | 1982 | |
|
1983 | 1983 | @command('debugrevlog', |
|
1984 | 1984 | [('c', 'changelog', False, _('open changelog')), |
|
1985 | 1985 | ('m', 'manifest', False, _('open manifest')), |
|
1986 | 1986 | ('d', 'dump', False, _('dump index data'))], |
|
1987 | 1987 | _('-c|-m|FILE')) |
|
1988 | 1988 | def debugrevlog(ui, repo, file_ = None, **opts): |
|
1989 | 1989 | """show data and statistics about a revlog""" |
|
1990 | 1990 | r = cmdutil.openrevlog(repo, 'debugrevlog', file_, opts) |
|
1991 | 1991 | |
|
1992 | 1992 | if opts.get("dump"): |
|
1993 | 1993 | numrevs = len(r) |
|
1994 | 1994 | ui.write("# rev p1rev p2rev start end deltastart base p1 p2" |
|
1995 | 1995 | " rawsize totalsize compression heads\n") |
|
1996 | 1996 | ts = 0 |
|
1997 | 1997 | heads = set() |
|
1998 | 1998 | for rev in xrange(numrevs): |
|
1999 | 1999 | dbase = r.deltaparent(rev) |
|
2000 | 2000 | if dbase == -1: |
|
2001 | 2001 | dbase = rev |
|
2002 | 2002 | cbase = r.chainbase(rev) |
|
2003 | 2003 | p1, p2 = r.parentrevs(rev) |
|
2004 | 2004 | rs = r.rawsize(rev) |
|
2005 | 2005 | ts = ts + rs |
|
2006 | 2006 | heads -= set(r.parentrevs(rev)) |
|
2007 | 2007 | heads.add(rev) |
|
2008 | 2008 | ui.write("%d %d %d %d %d %d %d %d %d %d %d %d %d\n" % |
|
2009 | 2009 | (rev, p1, p2, r.start(rev), r.end(rev), |
|
2010 | 2010 | r.start(dbase), r.start(cbase), |
|
2011 | 2011 | r.start(p1), r.start(p2), |
|
2012 | 2012 | rs, ts, ts / r.end(rev), len(heads))) |
|
2013 | 2013 | return 0 |
|
2014 | 2014 | |
|
2015 | 2015 | v = r.version |
|
2016 | 2016 | format = v & 0xFFFF |
|
2017 | 2017 | flags = [] |
|
2018 | 2018 | gdelta = False |
|
2019 | 2019 | if v & revlog.REVLOGNGINLINEDATA: |
|
2020 | 2020 | flags.append('inline') |
|
2021 | 2021 | if v & revlog.REVLOGGENERALDELTA: |
|
2022 | 2022 | gdelta = True |
|
2023 | 2023 | flags.append('generaldelta') |
|
2024 | 2024 | if not flags: |
|
2025 | 2025 | flags = ['(none)'] |
|
2026 | 2026 | |
|
2027 | 2027 | nummerges = 0 |
|
2028 | 2028 | numfull = 0 |
|
2029 | 2029 | numprev = 0 |
|
2030 | 2030 | nump1 = 0 |
|
2031 | 2031 | nump2 = 0 |
|
2032 | 2032 | numother = 0 |
|
2033 | 2033 | nump1prev = 0 |
|
2034 | 2034 | nump2prev = 0 |
|
2035 | 2035 | chainlengths = [] |
|
2036 | 2036 | |
|
2037 | 2037 | datasize = [None, 0, 0L] |
|
2038 | 2038 | fullsize = [None, 0, 0L] |
|
2039 | 2039 | deltasize = [None, 0, 0L] |
|
2040 | 2040 | |
|
2041 | 2041 | def addsize(size, l): |
|
2042 | 2042 | if l[0] is None or size < l[0]: |
|
2043 | 2043 | l[0] = size |
|
2044 | 2044 | if size > l[1]: |
|
2045 | 2045 | l[1] = size |
|
2046 | 2046 | l[2] += size |
|
2047 | 2047 | |
|
2048 | 2048 | numrevs = len(r) |
|
2049 | 2049 | for rev in xrange(numrevs): |
|
2050 | 2050 | p1, p2 = r.parentrevs(rev) |
|
2051 | 2051 | delta = r.deltaparent(rev) |
|
2052 | 2052 | if format > 0: |
|
2053 | 2053 | addsize(r.rawsize(rev), datasize) |
|
2054 | 2054 | if p2 != nullrev: |
|
2055 | 2055 | nummerges += 1 |
|
2056 | 2056 | size = r.length(rev) |
|
2057 | 2057 | if delta == nullrev: |
|
2058 | 2058 | chainlengths.append(0) |
|
2059 | 2059 | numfull += 1 |
|
2060 | 2060 | addsize(size, fullsize) |
|
2061 | 2061 | else: |
|
2062 | 2062 | chainlengths.append(chainlengths[delta] + 1) |
|
2063 | 2063 | addsize(size, deltasize) |
|
2064 | 2064 | if delta == rev - 1: |
|
2065 | 2065 | numprev += 1 |
|
2066 | 2066 | if delta == p1: |
|
2067 | 2067 | nump1prev += 1 |
|
2068 | 2068 | elif delta == p2: |
|
2069 | 2069 | nump2prev += 1 |
|
2070 | 2070 | elif delta == p1: |
|
2071 | 2071 | nump1 += 1 |
|
2072 | 2072 | elif delta == p2: |
|
2073 | 2073 | nump2 += 1 |
|
2074 | 2074 | elif delta != nullrev: |
|
2075 | 2075 | numother += 1 |
|
2076 | 2076 | |
|
2077 | 2077 | numdeltas = numrevs - numfull |
|
2078 | 2078 | numoprev = numprev - nump1prev - nump2prev |
|
2079 | 2079 | totalrawsize = datasize[2] |
|
2080 | 2080 | datasize[2] /= numrevs |
|
2081 | 2081 | fulltotal = fullsize[2] |
|
2082 | 2082 | fullsize[2] /= numfull |
|
2083 | 2083 | deltatotal = deltasize[2] |
|
2084 | 2084 | deltasize[2] /= numrevs - numfull |
|
2085 | 2085 | totalsize = fulltotal + deltatotal |
|
2086 | 2086 | avgchainlen = sum(chainlengths) / numrevs |
|
2087 | 2087 | compratio = totalrawsize / totalsize |
|
2088 | 2088 | |
|
2089 | 2089 | basedfmtstr = '%%%dd\n' |
|
2090 | 2090 | basepcfmtstr = '%%%dd %s(%%5.2f%%%%)\n' |
|
2091 | 2091 | |
|
2092 | 2092 | def dfmtstr(max): |
|
2093 | 2093 | return basedfmtstr % len(str(max)) |
|
2094 | 2094 | def pcfmtstr(max, padding=0): |
|
2095 | 2095 | return basepcfmtstr % (len(str(max)), ' ' * padding) |
|
2096 | 2096 | |
|
2097 | 2097 | def pcfmt(value, total): |
|
2098 | 2098 | return (value, 100 * float(value) / total) |
|
2099 | 2099 | |
|
2100 | 2100 | ui.write('format : %d\n' % format) |
|
2101 | 2101 | ui.write('flags : %s\n' % ', '.join(flags)) |
|
2102 | 2102 | |
|
2103 | 2103 | ui.write('\n') |
|
2104 | 2104 | fmt = pcfmtstr(totalsize) |
|
2105 | 2105 | fmt2 = dfmtstr(totalsize) |
|
2106 | 2106 | ui.write('revisions : ' + fmt2 % numrevs) |
|
2107 | 2107 | ui.write(' merges : ' + fmt % pcfmt(nummerges, numrevs)) |
|
2108 | 2108 | ui.write(' normal : ' + fmt % pcfmt(numrevs - nummerges, numrevs)) |
|
2109 | 2109 | ui.write('revisions : ' + fmt2 % numrevs) |
|
2110 | 2110 | ui.write(' full : ' + fmt % pcfmt(numfull, numrevs)) |
|
2111 | 2111 | ui.write(' deltas : ' + fmt % pcfmt(numdeltas, numrevs)) |
|
2112 | 2112 | ui.write('revision size : ' + fmt2 % totalsize) |
|
2113 | 2113 | ui.write(' full : ' + fmt % pcfmt(fulltotal, totalsize)) |
|
2114 | 2114 | ui.write(' deltas : ' + fmt % pcfmt(deltatotal, totalsize)) |
|
2115 | 2115 | |
|
2116 | 2116 | ui.write('\n') |
|
2117 | 2117 | fmt = dfmtstr(max(avgchainlen, compratio)) |
|
2118 | 2118 | ui.write('avg chain length : ' + fmt % avgchainlen) |
|
2119 | 2119 | ui.write('compression ratio : ' + fmt % compratio) |
|
2120 | 2120 | |
|
2121 | 2121 | if format > 0: |
|
2122 | 2122 | ui.write('\n') |
|
2123 | 2123 | ui.write('uncompressed data size (min/max/avg) : %d / %d / %d\n' |
|
2124 | 2124 | % tuple(datasize)) |
|
2125 | 2125 | ui.write('full revision size (min/max/avg) : %d / %d / %d\n' |
|
2126 | 2126 | % tuple(fullsize)) |
|
2127 | 2127 | ui.write('delta size (min/max/avg) : %d / %d / %d\n' |
|
2128 | 2128 | % tuple(deltasize)) |
|
2129 | 2129 | |
|
2130 | 2130 | if numdeltas > 0: |
|
2131 | 2131 | ui.write('\n') |
|
2132 | 2132 | fmt = pcfmtstr(numdeltas) |
|
2133 | 2133 | fmt2 = pcfmtstr(numdeltas, 4) |
|
2134 | 2134 | ui.write('deltas against prev : ' + fmt % pcfmt(numprev, numdeltas)) |
|
2135 | 2135 | if numprev > 0: |
|
2136 | 2136 | ui.write(' where prev = p1 : ' + fmt2 % pcfmt(nump1prev, numprev)) |
|
2137 | 2137 | ui.write(' where prev = p2 : ' + fmt2 % pcfmt(nump2prev, numprev)) |
|
2138 | 2138 | ui.write(' other : ' + fmt2 % pcfmt(numoprev, numprev)) |
|
2139 | 2139 | if gdelta: |
|
2140 | 2140 | ui.write('deltas against p1 : ' + fmt % pcfmt(nump1, numdeltas)) |
|
2141 | 2141 | ui.write('deltas against p2 : ' + fmt % pcfmt(nump2, numdeltas)) |
|
2142 | 2142 | ui.write('deltas against other : ' + fmt % pcfmt(numother, numdeltas)) |
|
2143 | 2143 | |
|
2144 | 2144 | @command('debugrevspec', [], ('REVSPEC')) |
|
2145 | 2145 | def debugrevspec(ui, repo, expr): |
|
2146 | 2146 | '''parse and apply a revision specification''' |
|
2147 | 2147 | if ui.verbose: |
|
2148 | 2148 | tree = revset.parse(expr)[0] |
|
2149 | 2149 | ui.note(tree, "\n") |
|
2150 | 2150 | newtree = revset.findaliases(ui, tree) |
|
2151 | 2151 | if newtree != tree: |
|
2152 | 2152 | ui.note(newtree, "\n") |
|
2153 | 2153 | func = revset.match(ui, expr) |
|
2154 | 2154 | for c in func(repo, range(len(repo))): |
|
2155 | 2155 | ui.write("%s\n" % c) |
|
2156 | 2156 | |
|
2157 | 2157 | @command('debugsetparents', [], _('REV1 [REV2]')) |
|
2158 | 2158 | def debugsetparents(ui, repo, rev1, rev2=None): |
|
2159 | 2159 | """manually set the parents of the current working directory |
|
2160 | 2160 | |
|
2161 | 2161 | This is useful for writing repository conversion tools, but should |
|
2162 | 2162 | be used with care. |
|
2163 | 2163 | |
|
2164 | 2164 | Returns 0 on success. |
|
2165 | 2165 | """ |
|
2166 | 2166 | |
|
2167 | 2167 | r1 = scmutil.revsingle(repo, rev1).node() |
|
2168 | 2168 | r2 = scmutil.revsingle(repo, rev2, 'null').node() |
|
2169 | 2169 | |
|
2170 | 2170 | wlock = repo.wlock() |
|
2171 | 2171 | try: |
|
2172 | 2172 | repo.dirstate.setparents(r1, r2) |
|
2173 | 2173 | finally: |
|
2174 | 2174 | wlock.release() |
|
2175 | 2175 | |
|
2176 | 2176 | @command('debugstate', |
|
2177 | 2177 | [('', 'nodates', None, _('do not display the saved mtime')), |
|
2178 | 2178 | ('', 'datesort', None, _('sort by saved mtime'))], |
|
2179 | 2179 | _('[OPTION]...')) |
|
2180 | 2180 | def debugstate(ui, repo, nodates=None, datesort=None): |
|
2181 | 2181 | """show the contents of the current dirstate""" |
|
2182 | 2182 | timestr = "" |
|
2183 | 2183 | showdate = not nodates |
|
2184 | 2184 | if datesort: |
|
2185 | 2185 | keyfunc = lambda x: (x[1][3], x[0]) # sort by mtime, then by filename |
|
2186 | 2186 | else: |
|
2187 | 2187 | keyfunc = None # sort by filename |
|
2188 | 2188 | for file_, ent in sorted(repo.dirstate._map.iteritems(), key=keyfunc): |
|
2189 | 2189 | if showdate: |
|
2190 | 2190 | if ent[3] == -1: |
|
2191 | 2191 | # Pad or slice to locale representation |
|
2192 | 2192 | locale_len = len(time.strftime("%Y-%m-%d %H:%M:%S ", |
|
2193 | 2193 | time.localtime(0))) |
|
2194 | 2194 | timestr = 'unset' |
|
2195 | 2195 | timestr = (timestr[:locale_len] + |
|
2196 | 2196 | ' ' * (locale_len - len(timestr))) |
|
2197 | 2197 | else: |
|
2198 | 2198 | timestr = time.strftime("%Y-%m-%d %H:%M:%S ", |
|
2199 | 2199 | time.localtime(ent[3])) |
|
2200 | 2200 | if ent[1] & 020000: |
|
2201 | 2201 | mode = 'lnk' |
|
2202 | 2202 | else: |
|
2203 | 2203 | mode = '%3o' % (ent[1] & 0777 & ~util.umask) |
|
2204 | 2204 | ui.write("%c %s %10d %s%s\n" % (ent[0], mode, ent[2], timestr, file_)) |
|
2205 | 2205 | for f in repo.dirstate.copies(): |
|
2206 | 2206 | ui.write(_("copy: %s -> %s\n") % (repo.dirstate.copied(f), f)) |
|
2207 | 2207 | |
|
2208 | 2208 | @command('debugsub', |
|
2209 | 2209 | [('r', 'rev', '', |
|
2210 | 2210 | _('revision to check'), _('REV'))], |
|
2211 | 2211 | _('[-r REV] [REV]')) |
|
2212 | 2212 | def debugsub(ui, repo, rev=None): |
|
2213 | 2213 | ctx = scmutil.revsingle(repo, rev, None) |
|
2214 | 2214 | for k, v in sorted(ctx.substate.items()): |
|
2215 | 2215 | ui.write('path %s\n' % k) |
|
2216 | 2216 | ui.write(' source %s\n' % v[0]) |
|
2217 | 2217 | ui.write(' revision %s\n' % v[1]) |
|
2218 | 2218 | |
|
2219 | 2219 | @command('debugwalk', walkopts, _('[OPTION]... [FILE]...')) |
|
2220 | 2220 | def debugwalk(ui, repo, *pats, **opts): |
|
2221 | 2221 | """show how files match on given patterns""" |
|
2222 | 2222 | m = scmutil.match(repo[None], pats, opts) |
|
2223 | 2223 | items = list(repo.walk(m)) |
|
2224 | 2224 | if not items: |
|
2225 | 2225 | return |
|
2226 | 2226 | fmt = 'f %%-%ds %%-%ds %%s' % ( |
|
2227 | 2227 | max([len(abs) for abs in items]), |
|
2228 | 2228 | max([len(m.rel(abs)) for abs in items])) |
|
2229 | 2229 | for abs in items: |
|
2230 | 2230 | line = fmt % (abs, m.rel(abs), m.exact(abs) and 'exact' or '') |
|
2231 | 2231 | ui.write("%s\n" % line.rstrip()) |
|
2232 | 2232 | |
|
2233 | 2233 | @command('debugwireargs', |
|
2234 | 2234 | [('', 'three', '', 'three'), |
|
2235 | 2235 | ('', 'four', '', 'four'), |
|
2236 | 2236 | ('', 'five', '', 'five'), |
|
2237 | 2237 | ] + remoteopts, |
|
2238 | 2238 | _('REPO [OPTIONS]... [ONE [TWO]]')) |
|
2239 | 2239 | def debugwireargs(ui, repopath, *vals, **opts): |
|
2240 | 2240 | repo = hg.peer(ui, opts, repopath) |
|
2241 | 2241 | for opt in remoteopts: |
|
2242 | 2242 | del opts[opt[1]] |
|
2243 | 2243 | args = {} |
|
2244 | 2244 | for k, v in opts.iteritems(): |
|
2245 | 2245 | if v: |
|
2246 | 2246 | args[k] = v |
|
2247 | 2247 | # run twice to check that we don't mess up the stream for the next command |
|
2248 | 2248 | res1 = repo.debugwireargs(*vals, **args) |
|
2249 | 2249 | res2 = repo.debugwireargs(*vals, **args) |
|
2250 | 2250 | ui.write("%s\n" % res1) |
|
2251 | 2251 | if res1 != res2: |
|
2252 | 2252 | ui.warn("%s\n" % res2) |
|
2253 | 2253 | |
|
2254 | 2254 | @command('^diff', |
|
2255 | 2255 | [('r', 'rev', [], _('revision'), _('REV')), |
|
2256 | 2256 | ('c', 'change', '', _('change made by revision'), _('REV')) |
|
2257 | 2257 | ] + diffopts + diffopts2 + walkopts + subrepoopts, |
|
2258 | 2258 | _('[OPTION]... ([-c REV] | [-r REV1 [-r REV2]]) [FILE]...')) |
|
2259 | 2259 | def diff(ui, repo, *pats, **opts): |
|
2260 | 2260 | """diff repository (or selected files) |
|
2261 | 2261 | |
|
2262 | 2262 | Show differences between revisions for the specified files. |
|
2263 | 2263 | |
|
2264 | 2264 | Differences between files are shown using the unified diff format. |
|
2265 | 2265 | |
|
2266 | 2266 | .. note:: |
|
2267 | 2267 | diff may generate unexpected results for merges, as it will |
|
2268 | 2268 | default to comparing against the working directory's first |
|
2269 | 2269 | parent changeset if no revisions are specified. |
|
2270 | 2270 | |
|
2271 | 2271 | When two revision arguments are given, then changes are shown |
|
2272 | 2272 | between those revisions. If only one revision is specified then |
|
2273 | 2273 | that revision is compared to the working directory, and, when no |
|
2274 | 2274 | revisions are specified, the working directory files are compared |
|
2275 | 2275 | to its parent. |
|
2276 | 2276 | |
|
2277 | 2277 | Alternatively you can specify -c/--change with a revision to see |
|
2278 | 2278 | the changes in that changeset relative to its first parent. |
|
2279 | 2279 | |
|
2280 | 2280 | Without the -a/--text option, diff will avoid generating diffs of |
|
2281 | 2281 | files it detects as binary. With -a, diff will generate a diff |
|
2282 | 2282 | anyway, probably with undesirable results. |
|
2283 | 2283 | |
|
2284 | 2284 | Use the -g/--git option to generate diffs in the git extended diff |
|
2285 | 2285 | format. For more information, read :hg:`help diffs`. |
|
2286 | 2286 | |
|
2287 | 2287 | .. container:: verbose |
|
2288 | 2288 | |
|
2289 | 2289 | Examples: |
|
2290 | 2290 | |
|
2291 | 2291 | - compare a file in the current working directory to its parent:: |
|
2292 | 2292 | |
|
2293 | 2293 | hg diff foo.c |
|
2294 | 2294 | |
|
2295 | 2295 | - compare two historical versions of a directory, with rename info:: |
|
2296 | 2296 | |
|
2297 | 2297 | hg diff --git -r 1.0:1.2 lib/ |
|
2298 | 2298 | |
|
2299 | 2299 | - get change stats relative to the last change on some date:: |
|
2300 | 2300 | |
|
2301 | 2301 | hg diff --stat -r "date('may 2')" |
|
2302 | 2302 | |
|
2303 | 2303 | - diff all newly-added files that contain a keyword:: |
|
2304 | 2304 | |
|
2305 | 2305 | hg diff "set:added() and grep(GNU)" |
|
2306 | 2306 | |
|
2307 | 2307 | - compare a revision and its parents:: |
|
2308 | 2308 | |
|
2309 | 2309 | hg diff -c 9353 # compare against first parent |
|
2310 | 2310 | hg diff -r 9353^:9353 # same using revset syntax |
|
2311 | 2311 | hg diff -r 9353^2:9353 # compare against the second parent |
|
2312 | 2312 | |
|
2313 | 2313 | Returns 0 on success. |
|
2314 | 2314 | """ |
|
2315 | 2315 | |
|
2316 | 2316 | revs = opts.get('rev') |
|
2317 | 2317 | change = opts.get('change') |
|
2318 | 2318 | stat = opts.get('stat') |
|
2319 | 2319 | reverse = opts.get('reverse') |
|
2320 | 2320 | |
|
2321 | 2321 | if revs and change: |
|
2322 | 2322 | msg = _('cannot specify --rev and --change at the same time') |
|
2323 | 2323 | raise util.Abort(msg) |
|
2324 | 2324 | elif change: |
|
2325 | 2325 | node2 = scmutil.revsingle(repo, change, None).node() |
|
2326 | 2326 | node1 = repo[node2].p1().node() |
|
2327 | 2327 | else: |
|
2328 | 2328 | node1, node2 = scmutil.revpair(repo, revs) |
|
2329 | 2329 | |
|
2330 | 2330 | if reverse: |
|
2331 | 2331 | node1, node2 = node2, node1 |
|
2332 | 2332 | |
|
2333 | 2333 | diffopts = patch.diffopts(ui, opts) |
|
2334 | 2334 | m = scmutil.match(repo[node2], pats, opts) |
|
2335 | 2335 | cmdutil.diffordiffstat(ui, repo, diffopts, node1, node2, m, stat=stat, |
|
2336 | 2336 | listsubrepos=opts.get('subrepos')) |
|
2337 | 2337 | |
|
2338 | 2338 | @command('^export', |
|
2339 | 2339 | [('o', 'output', '', |
|
2340 | 2340 | _('print output to file with formatted name'), _('FORMAT')), |
|
2341 | 2341 | ('', 'switch-parent', None, _('diff against the second parent')), |
|
2342 | 2342 | ('r', 'rev', [], _('revisions to export'), _('REV')), |
|
2343 | 2343 | ] + diffopts, |
|
2344 | 2344 | _('[OPTION]... [-o OUTFILESPEC] REV...')) |
|
2345 | 2345 | def export(ui, repo, *changesets, **opts): |
|
2346 | 2346 | """dump the header and diffs for one or more changesets |
|
2347 | 2347 | |
|
2348 | 2348 | Print the changeset header and diffs for one or more revisions. |
|
2349 | 2349 | |
|
2350 | 2350 | The information shown in the changeset header is: author, date, |
|
2351 | 2351 | branch name (if non-default), changeset hash, parent(s) and commit |
|
2352 | 2352 | comment. |
|
2353 | 2353 | |
|
2354 | 2354 | .. note:: |
|
2355 | 2355 | export may generate unexpected diff output for merge |
|
2356 | 2356 | changesets, as it will compare the merge changeset against its |
|
2357 | 2357 | first parent only. |
|
2358 | 2358 | |
|
2359 | 2359 | Output may be to a file, in which case the name of the file is |
|
2360 | 2360 | given using a format string. The formatting rules are as follows: |
|
2361 | 2361 | |
|
2362 | 2362 | :``%%``: literal "%" character |
|
2363 | 2363 | :``%H``: changeset hash (40 hexadecimal digits) |
|
2364 | 2364 | :``%N``: number of patches being generated |
|
2365 | 2365 | :``%R``: changeset revision number |
|
2366 | 2366 | :``%b``: basename of the exporting repository |
|
2367 | 2367 | :``%h``: short-form changeset hash (12 hexadecimal digits) |
|
2368 | 2368 | :``%m``: first line of the commit message (only alphanumeric characters) |
|
2369 | 2369 | :``%n``: zero-padded sequence number, starting at 1 |
|
2370 | 2370 | :``%r``: zero-padded changeset revision number |
|
2371 | 2371 | |
|
2372 | 2372 | Without the -a/--text option, export will avoid generating diffs |
|
2373 | 2373 | of files it detects as binary. With -a, export will generate a |
|
2374 | 2374 | diff anyway, probably with undesirable results. |
|
2375 | 2375 | |
|
2376 | 2376 | Use the -g/--git option to generate diffs in the git extended diff |
|
2377 | 2377 | format. See :hg:`help diffs` for more information. |
|
2378 | 2378 | |
|
2379 | 2379 | With the --switch-parent option, the diff will be against the |
|
2380 | 2380 | second parent. It can be useful to review a merge. |
|
2381 | 2381 | |
|
2382 | 2382 | .. container:: verbose |
|
2383 | 2383 | |
|
2384 | 2384 | Examples: |
|
2385 | 2385 | |
|
2386 | 2386 | - use export and import to transplant a bugfix to the current |
|
2387 | 2387 | branch:: |
|
2388 | 2388 | |
|
2389 | 2389 | hg export -r 9353 | hg import - |
|
2390 | 2390 | |
|
2391 | 2391 | - export all the changesets between two revisions to a file with |
|
2392 | 2392 | rename information:: |
|
2393 | 2393 | |
|
2394 | 2394 | hg export --git -r 123:150 > changes.txt |
|
2395 | 2395 | |
|
2396 | 2396 | - split outgoing changes into a series of patches with |
|
2397 | 2397 | descriptive names:: |
|
2398 | 2398 | |
|
2399 | 2399 | hg export -r "outgoing()" -o "%n-%m.patch" |
|
2400 | 2400 | |
|
2401 | 2401 | Returns 0 on success. |
|
2402 | 2402 | """ |
|
2403 | 2403 | changesets += tuple(opts.get('rev', [])) |
|
2404 | 2404 | if not changesets: |
|
2405 | 2405 | raise util.Abort(_("export requires at least one changeset")) |
|
2406 | 2406 | revs = scmutil.revrange(repo, changesets) |
|
2407 | 2407 | if len(revs) > 1: |
|
2408 | 2408 | ui.note(_('exporting patches:\n')) |
|
2409 | 2409 | else: |
|
2410 | 2410 | ui.note(_('exporting patch:\n')) |
|
2411 | 2411 | cmdutil.export(repo, revs, template=opts.get('output'), |
|
2412 | 2412 | switch_parent=opts.get('switch_parent'), |
|
2413 | 2413 | opts=patch.diffopts(ui, opts)) |
|
2414 | 2414 | |
|
2415 | 2415 | @command('^forget', walkopts, _('[OPTION]... FILE...')) |
|
2416 | 2416 | def forget(ui, repo, *pats, **opts): |
|
2417 | 2417 | """forget the specified files on the next commit |
|
2418 | 2418 | |
|
2419 | 2419 | Mark the specified files so they will no longer be tracked |
|
2420 | 2420 | after the next commit. |
|
2421 | 2421 | |
|
2422 | 2422 | This only removes files from the current branch, not from the |
|
2423 | 2423 | entire project history, and it does not delete them from the |
|
2424 | 2424 | working directory. |
|
2425 | 2425 | |
|
2426 | 2426 | To undo a forget before the next commit, see :hg:`add`. |
|
2427 | 2427 | |
|
2428 | 2428 | .. container:: verbose |
|
2429 | 2429 | |
|
2430 | 2430 | Examples: |
|
2431 | 2431 | |
|
2432 | 2432 | - forget newly-added binary files:: |
|
2433 | 2433 | |
|
2434 | 2434 | hg forget "set:added() and binary()" |
|
2435 | 2435 | |
|
2436 | 2436 | - forget files that would be excluded by .hgignore:: |
|
2437 | 2437 | |
|
2438 | 2438 | hg forget "set:hgignore()" |
|
2439 | 2439 | |
|
2440 | 2440 | Returns 0 on success. |
|
2441 | 2441 | """ |
|
2442 | 2442 | |
|
2443 | 2443 | if not pats: |
|
2444 | 2444 | raise util.Abort(_('no files specified')) |
|
2445 | 2445 | |
|
2446 | 2446 | wctx = repo[None] |
|
2447 | 2447 | m = scmutil.match(wctx, pats, opts) |
|
2448 | 2448 | s = repo.status(match=m, clean=True) |
|
2449 | 2449 | forget = sorted(s[0] + s[1] + s[3] + s[6]) |
|
2450 | 2450 | subforget = {} |
|
2451 | 2451 | errs = 0 |
|
2452 | 2452 | |
|
2453 | 2453 | for subpath in wctx.substate: |
|
2454 | 2454 | sub = wctx.sub(subpath) |
|
2455 | 2455 | try: |
|
2456 | 2456 | submatch = matchmod.narrowmatcher(subpath, m) |
|
2457 | 2457 | for fsub in sub.walk(submatch): |
|
2458 | 2458 | if submatch.exact(fsub): |
|
2459 | 2459 | subforget[subpath + '/' + fsub] = (fsub, sub) |
|
2460 | 2460 | except error.LookupError: |
|
2461 | 2461 | ui.status(_("skipping missing subrepository: %s\n") % subpath) |
|
2462 | 2462 | |
|
2463 | 2463 | for f in m.files(): |
|
2464 | 2464 | if f not in repo.dirstate and not os.path.isdir(m.rel(f)): |
|
2465 | 2465 | if f not in subforget: |
|
2466 | 2466 | if os.path.exists(m.rel(f)): |
|
2467 | 2467 | ui.warn(_('not removing %s: file is already untracked\n') |
|
2468 | 2468 | % m.rel(f)) |
|
2469 | 2469 | errs = 1 |
|
2470 | 2470 | |
|
2471 | 2471 | for f in forget: |
|
2472 | 2472 | if ui.verbose or not m.exact(f): |
|
2473 | 2473 | ui.status(_('removing %s\n') % m.rel(f)) |
|
2474 | 2474 | |
|
2475 | 2475 | if ui.verbose: |
|
2476 | 2476 | for f in sorted(subforget.keys()): |
|
2477 | 2477 | ui.status(_('removing %s\n') % m.rel(f)) |
|
2478 | 2478 | |
|
2479 | 2479 | wctx.forget(forget) |
|
2480 | 2480 | |
|
2481 | 2481 | for f in sorted(subforget.keys()): |
|
2482 | 2482 | fsub, sub = subforget[f] |
|
2483 | 2483 | sub.forget([fsub]) |
|
2484 | 2484 | |
|
2485 | 2485 | return errs |
|
2486 | 2486 | |
|
2487 | 2487 | @command( |
|
2488 | 2488 | 'graft', |
|
2489 | 2489 | [('c', 'continue', False, _('resume interrupted graft')), |
|
2490 | 2490 | ('e', 'edit', False, _('invoke editor on commit messages')), |
|
2491 | 2491 | ('D', 'currentdate', False, |
|
2492 | 2492 | _('record the current date as commit date')), |
|
2493 | 2493 | ('U', 'currentuser', False, |
|
2494 | 2494 | _('record the current user as committer'), _('DATE'))] |
|
2495 | 2495 | + commitopts2 + mergetoolopts, |
|
2496 | 2496 | _('[OPTION]... REVISION...')) |
|
2497 | 2497 | def graft(ui, repo, *revs, **opts): |
|
2498 | 2498 | '''copy changes from other branches onto the current branch |
|
2499 | 2499 | |
|
2500 | 2500 | This command uses Mercurial's merge logic to copy individual |
|
2501 | 2501 | changes from other branches without merging branches in the |
|
2502 | 2502 | history graph. This is sometimes known as 'backporting' or |
|
2503 | 2503 | 'cherry-picking'. By default, graft will copy user, date, and |
|
2504 | 2504 | description from the source changesets. |
|
2505 | 2505 | |
|
2506 | 2506 | Changesets that are ancestors of the current revision, that have |
|
2507 | 2507 | already been grafted, or that are merges will be skipped. |
|
2508 | 2508 | |
|
2509 | 2509 | If a graft merge results in conflicts, the graft process is |
|
2510 | 2510 | interrupted so that the current merge can be manually resolved. |
|
2511 | 2511 | Once all conflicts are addressed, the graft process can be |
|
2512 | 2512 | continued with the -c/--continue option. |
|
2513 | 2513 | |
|
2514 | 2514 | .. note:: |
|
2515 | 2515 | The -c/--continue option does not reapply earlier options. |
|
2516 | 2516 | |
|
2517 | 2517 | .. container:: verbose |
|
2518 | 2518 | |
|
2519 | 2519 | Examples: |
|
2520 | 2520 | |
|
2521 | 2521 | - copy a single change to the stable branch and edit its description:: |
|
2522 | 2522 | |
|
2523 | 2523 | hg update stable |
|
2524 | 2524 | hg graft --edit 9393 |
|
2525 | 2525 | |
|
2526 | 2526 | - graft a range of changesets with one exception, updating dates:: |
|
2527 | 2527 | |
|
2528 | 2528 | hg graft -D "2085::2093 and not 2091" |
|
2529 | 2529 | |
|
2530 | 2530 | - continue a graft after resolving conflicts:: |
|
2531 | 2531 | |
|
2532 | 2532 | hg graft -c |
|
2533 | 2533 | |
|
2534 | 2534 | - show the source of a grafted changeset:: |
|
2535 | 2535 | |
|
2536 | 2536 | hg log --debug -r tip |
|
2537 | 2537 | |
|
2538 | 2538 | Returns 0 on successful completion. |
|
2539 | 2539 | ''' |
|
2540 | 2540 | |
|
2541 | 2541 | if not opts.get('user') and opts.get('currentuser'): |
|
2542 | 2542 | opts['user'] = ui.username() |
|
2543 | 2543 | if not opts.get('date') and opts.get('currentdate'): |
|
2544 | 2544 | opts['date'] = "%d %d" % util.makedate() |
|
2545 | 2545 | |
|
2546 | 2546 | editor = None |
|
2547 | 2547 | if opts.get('edit'): |
|
2548 | 2548 | editor = cmdutil.commitforceeditor |
|
2549 | 2549 | |
|
2550 | 2550 | cont = False |
|
2551 | 2551 | if opts['continue']: |
|
2552 | 2552 | cont = True |
|
2553 | 2553 | if revs: |
|
2554 | 2554 | raise util.Abort(_("can't specify --continue and revisions")) |
|
2555 | 2555 | # read in unfinished revisions |
|
2556 | 2556 | try: |
|
2557 | 2557 | nodes = repo.opener.read('graftstate').splitlines() |
|
2558 | 2558 | revs = [repo[node].rev() for node in nodes] |
|
2559 | 2559 | except IOError, inst: |
|
2560 | 2560 | if inst.errno != errno.ENOENT: |
|
2561 | 2561 | raise |
|
2562 | 2562 | raise util.Abort(_("no graft state found, can't continue")) |
|
2563 | 2563 | else: |
|
2564 | 2564 | cmdutil.bailifchanged(repo) |
|
2565 | 2565 | if not revs: |
|
2566 | 2566 | raise util.Abort(_('no revisions specified')) |
|
2567 | 2567 | revs = scmutil.revrange(repo, revs) |
|
2568 | 2568 | |
|
2569 | 2569 | # check for merges |
|
2570 | 2570 | for rev in repo.revs('%ld and merge()', revs): |
|
2571 | 2571 | ui.warn(_('skipping ungraftable merge revision %s\n') % rev) |
|
2572 | 2572 | revs.remove(rev) |
|
2573 | 2573 | if not revs: |
|
2574 | 2574 | return -1 |
|
2575 | 2575 | |
|
2576 | 2576 | # check for ancestors of dest branch |
|
2577 | 2577 | for rev in repo.revs('::. and %ld', revs): |
|
2578 | 2578 | ui.warn(_('skipping ancestor revision %s\n') % rev) |
|
2579 | 2579 | revs.remove(rev) |
|
2580 | 2580 | if not revs: |
|
2581 | 2581 | return -1 |
|
2582 | 2582 | |
|
2583 | 2583 | # analyze revs for earlier grafts |
|
2584 | 2584 | ids = {} |
|
2585 | 2585 | for ctx in repo.set("%ld", revs): |
|
2586 | 2586 | ids[ctx.hex()] = ctx.rev() |
|
2587 | 2587 | n = ctx.extra().get('source') |
|
2588 | 2588 | if n: |
|
2589 | 2589 | ids[n] = ctx.rev() |
|
2590 | 2590 | |
|
2591 | 2591 | # check ancestors for earlier grafts |
|
2592 | 2592 | ui.debug('scanning for duplicate grafts\n') |
|
2593 | 2593 | for ctx in repo.set("::. - ::%ld", revs): |
|
2594 | 2594 | n = ctx.extra().get('source') |
|
2595 | 2595 | if n in ids: |
|
2596 | 2596 | r = repo[n].rev() |
|
2597 | 2597 | if r in revs: |
|
2598 | 2598 | ui.warn(_('skipping already grafted revision %s\n') % r) |
|
2599 | 2599 | revs.remove(r) |
|
2600 | 2600 | elif ids[n] in revs: |
|
2601 | 2601 | ui.warn(_('skipping already grafted revision %s ' |
|
2602 | 2602 | '(same origin %d)\n') % (ids[n], r)) |
|
2603 | 2603 | revs.remove(ids[n]) |
|
2604 | 2604 | elif ctx.hex() in ids: |
|
2605 | 2605 | r = ids[ctx.hex()] |
|
2606 | 2606 | ui.warn(_('skipping already grafted revision %s ' |
|
2607 | 2607 | '(was grafted from %d)\n') % (r, ctx.rev())) |
|
2608 | 2608 | revs.remove(r) |
|
2609 | 2609 | if not revs: |
|
2610 | 2610 | return -1 |
|
2611 | 2611 | |
|
2612 | 2612 | for pos, ctx in enumerate(repo.set("%ld", revs)): |
|
2613 | 2613 | current = repo['.'] |
|
2614 | 2614 | ui.status(_('grafting revision %s\n') % ctx.rev()) |
|
2615 | 2615 | |
|
2616 | 2616 | # we don't merge the first commit when continuing |
|
2617 | 2617 | if not cont: |
|
2618 | 2618 | # perform the graft merge with p1(rev) as 'ancestor' |
|
2619 | 2619 | try: |
|
2620 | 2620 | # ui.forcemerge is an internal variable, do not document |
|
2621 | 2621 | repo.ui.setconfig('ui', 'forcemerge', opts.get('tool', '')) |
|
2622 | 2622 | stats = mergemod.update(repo, ctx.node(), True, True, False, |
|
2623 | 2623 | ctx.p1().node()) |
|
2624 | 2624 | finally: |
|
2625 | 2625 | ui.setconfig('ui', 'forcemerge', '') |
|
2626 | 2626 | # drop the second merge parent |
|
2627 | 2627 | repo.dirstate.setparents(current.node(), nullid) |
|
2628 | 2628 | repo.dirstate.write() |
|
2629 | 2629 | # fix up dirstate for copies and renames |
|
2630 | 2630 | cmdutil.duplicatecopies(repo, ctx.rev(), current.node(), nullid) |
|
2631 | 2631 | # report any conflicts |
|
2632 | 2632 | if stats and stats[3] > 0: |
|
2633 | 2633 | # write out state for --continue |
|
2634 | 2634 | nodelines = [repo[rev].hex() + "\n" for rev in revs[pos:]] |
|
2635 | 2635 | repo.opener.write('graftstate', ''.join(nodelines)) |
|
2636 | 2636 | raise util.Abort( |
|
2637 | 2637 | _("unresolved conflicts, can't continue"), |
|
2638 | 2638 | hint=_('use hg resolve and hg graft --continue')) |
|
2639 | 2639 | else: |
|
2640 | 2640 | cont = False |
|
2641 | 2641 | |
|
2642 | 2642 | # commit |
|
2643 | 2643 | source = ctx.extra().get('source') |
|
2644 | 2644 | if not source: |
|
2645 | 2645 | source = ctx.hex() |
|
2646 | 2646 | extra = {'source': source} |
|
2647 | 2647 | user = ctx.user() |
|
2648 | 2648 | if opts.get('user'): |
|
2649 | 2649 | user = opts['user'] |
|
2650 | 2650 | date = ctx.date() |
|
2651 | 2651 | if opts.get('date'): |
|
2652 | 2652 | date = opts['date'] |
|
2653 | 2653 | repo.commit(text=ctx.description(), user=user, |
|
2654 | 2654 | date=date, extra=extra, editor=editor) |
|
2655 | 2655 | |
|
2656 | 2656 | # remove state when we complete successfully |
|
2657 | 2657 | if os.path.exists(repo.join('graftstate')): |
|
2658 | 2658 | util.unlinkpath(repo.join('graftstate')) |
|
2659 | 2659 | |
|
2660 | 2660 | return 0 |
|
2661 | 2661 | |
|
2662 | 2662 | @command('grep', |
|
2663 | 2663 | [('0', 'print0', None, _('end fields with NUL')), |
|
2664 | 2664 | ('', 'all', None, _('print all revisions that match')), |
|
2665 | 2665 | ('a', 'text', None, _('treat all files as text')), |
|
2666 | 2666 | ('f', 'follow', None, |
|
2667 | 2667 | _('follow changeset history,' |
|
2668 | 2668 | ' or file history across copies and renames')), |
|
2669 | 2669 | ('i', 'ignore-case', None, _('ignore case when matching')), |
|
2670 | 2670 | ('l', 'files-with-matches', None, |
|
2671 | 2671 | _('print only filenames and revisions that match')), |
|
2672 | 2672 | ('n', 'line-number', None, _('print matching line numbers')), |
|
2673 | 2673 | ('r', 'rev', [], |
|
2674 | 2674 | _('only search files changed within revision range'), _('REV')), |
|
2675 | 2675 | ('u', 'user', None, _('list the author (long with -v)')), |
|
2676 | 2676 | ('d', 'date', None, _('list the date (short with -q)')), |
|
2677 | 2677 | ] + walkopts, |
|
2678 | 2678 | _('[OPTION]... PATTERN [FILE]...')) |
|
2679 | 2679 | def grep(ui, repo, pattern, *pats, **opts): |
|
2680 | 2680 | """search for a pattern in specified files and revisions |
|
2681 | 2681 | |
|
2682 | 2682 | Search revisions of files for a regular expression. |
|
2683 | 2683 | |
|
2684 | 2684 | This command behaves differently than Unix grep. It only accepts |
|
2685 | 2685 | Python/Perl regexps. It searches repository history, not the |
|
2686 | 2686 | working directory. It always prints the revision number in which a |
|
2687 | 2687 | match appears. |
|
2688 | 2688 | |
|
2689 | 2689 | By default, grep only prints output for the first revision of a |
|
2690 | 2690 | file in which it finds a match. To get it to print every revision |
|
2691 | 2691 | that contains a change in match status ("-" for a match that |
|
2692 | 2692 | becomes a non-match, or "+" for a non-match that becomes a match), |
|
2693 | 2693 | use the --all flag. |
|
2694 | 2694 | |
|
2695 | 2695 | Returns 0 if a match is found, 1 otherwise. |
|
2696 | 2696 | """ |
|
2697 | 2697 | reflags = re.M |
|
2698 | 2698 | if opts.get('ignore_case'): |
|
2699 | 2699 | reflags |= re.I |
|
2700 | 2700 | try: |
|
2701 | 2701 | regexp = re.compile(pattern, reflags) |
|
2702 | 2702 | except re.error, inst: |
|
2703 | 2703 | ui.warn(_("grep: invalid match pattern: %s\n") % inst) |
|
2704 | 2704 | return 1 |
|
2705 | 2705 | sep, eol = ':', '\n' |
|
2706 | 2706 | if opts.get('print0'): |
|
2707 | 2707 | sep = eol = '\0' |
|
2708 | 2708 | |
|
2709 | 2709 | getfile = util.lrucachefunc(repo.file) |
|
2710 | 2710 | |
|
2711 | 2711 | def matchlines(body): |
|
2712 | 2712 | begin = 0 |
|
2713 | 2713 | linenum = 0 |
|
2714 | 2714 | while True: |
|
2715 | 2715 | match = regexp.search(body, begin) |
|
2716 | 2716 | if not match: |
|
2717 | 2717 | break |
|
2718 | 2718 | mstart, mend = match.span() |
|
2719 | 2719 | linenum += body.count('\n', begin, mstart) + 1 |
|
2720 | 2720 | lstart = body.rfind('\n', begin, mstart) + 1 or begin |
|
2721 | 2721 | begin = body.find('\n', mend) + 1 or len(body) + 1 |
|
2722 | 2722 | lend = begin - 1 |
|
2723 | 2723 | yield linenum, mstart - lstart, mend - lstart, body[lstart:lend] |
|
2724 | 2724 | |
|
2725 | 2725 | class linestate(object): |
|
2726 | 2726 | def __init__(self, line, linenum, colstart, colend): |
|
2727 | 2727 | self.line = line |
|
2728 | 2728 | self.linenum = linenum |
|
2729 | 2729 | self.colstart = colstart |
|
2730 | 2730 | self.colend = colend |
|
2731 | 2731 | |
|
2732 | 2732 | def __hash__(self): |
|
2733 | 2733 | return hash((self.linenum, self.line)) |
|
2734 | 2734 | |
|
2735 | 2735 | def __eq__(self, other): |
|
2736 | 2736 | return self.line == other.line |
|
2737 | 2737 | |
|
2738 | 2738 | matches = {} |
|
2739 | 2739 | copies = {} |
|
2740 | 2740 | def grepbody(fn, rev, body): |
|
2741 | 2741 | matches[rev].setdefault(fn, []) |
|
2742 | 2742 | m = matches[rev][fn] |
|
2743 | 2743 | for lnum, cstart, cend, line in matchlines(body): |
|
2744 | 2744 | s = linestate(line, lnum, cstart, cend) |
|
2745 | 2745 | m.append(s) |
|
2746 | 2746 | |
|
2747 | 2747 | def difflinestates(a, b): |
|
2748 | 2748 | sm = difflib.SequenceMatcher(None, a, b) |
|
2749 | 2749 | for tag, alo, ahi, blo, bhi in sm.get_opcodes(): |
|
2750 | 2750 | if tag == 'insert': |
|
2751 | 2751 | for i in xrange(blo, bhi): |
|
2752 | 2752 | yield ('+', b[i]) |
|
2753 | 2753 | elif tag == 'delete': |
|
2754 | 2754 | for i in xrange(alo, ahi): |
|
2755 | 2755 | yield ('-', a[i]) |
|
2756 | 2756 | elif tag == 'replace': |
|
2757 | 2757 | for i in xrange(alo, ahi): |
|
2758 | 2758 | yield ('-', a[i]) |
|
2759 | 2759 | for i in xrange(blo, bhi): |
|
2760 | 2760 | yield ('+', b[i]) |
|
2761 | 2761 | |
|
2762 | 2762 | def display(fn, ctx, pstates, states): |
|
2763 | 2763 | rev = ctx.rev() |
|
2764 | 2764 | datefunc = ui.quiet and util.shortdate or util.datestr |
|
2765 | 2765 | found = False |
|
2766 | 2766 | filerevmatches = {} |
|
2767 | 2767 | def binary(): |
|
2768 | 2768 | flog = getfile(fn) |
|
2769 | 2769 | return util.binary(flog.read(ctx.filenode(fn))) |
|
2770 | 2770 | |
|
2771 | 2771 | if opts.get('all'): |
|
2772 | 2772 | iter = difflinestates(pstates, states) |
|
2773 | 2773 | else: |
|
2774 | 2774 | iter = [('', l) for l in states] |
|
2775 | 2775 | for change, l in iter: |
|
2776 | 2776 | cols = [fn, str(rev)] |
|
2777 | 2777 | before, match, after = None, None, None |
|
2778 | 2778 | if opts.get('line_number'): |
|
2779 | 2779 | cols.append(str(l.linenum)) |
|
2780 | 2780 | if opts.get('all'): |
|
2781 | 2781 | cols.append(change) |
|
2782 | 2782 | if opts.get('user'): |
|
2783 | 2783 | cols.append(ui.shortuser(ctx.user())) |
|
2784 | 2784 | if opts.get('date'): |
|
2785 | 2785 | cols.append(datefunc(ctx.date())) |
|
2786 | 2786 | if opts.get('files_with_matches'): |
|
2787 | 2787 | c = (fn, rev) |
|
2788 | 2788 | if c in filerevmatches: |
|
2789 | 2789 | continue |
|
2790 | 2790 | filerevmatches[c] = 1 |
|
2791 | 2791 | else: |
|
2792 | 2792 | before = l.line[:l.colstart] |
|
2793 | 2793 | match = l.line[l.colstart:l.colend] |
|
2794 | 2794 | after = l.line[l.colend:] |
|
2795 | 2795 | ui.write(sep.join(cols)) |
|
2796 | 2796 | if before is not None: |
|
2797 | 2797 | if not opts.get('text') and binary(): |
|
2798 | 2798 | ui.write(sep + " Binary file matches") |
|
2799 | 2799 | else: |
|
2800 | 2800 | ui.write(sep + before) |
|
2801 | 2801 | ui.write(match, label='grep.match') |
|
2802 | 2802 | ui.write(after) |
|
2803 | 2803 | ui.write(eol) |
|
2804 | 2804 | found = True |
|
2805 | 2805 | return found |
|
2806 | 2806 | |
|
2807 | 2807 | skip = {} |
|
2808 | 2808 | revfiles = {} |
|
2809 | 2809 | matchfn = scmutil.match(repo[None], pats, opts) |
|
2810 | 2810 | found = False |
|
2811 | 2811 | follow = opts.get('follow') |
|
2812 | 2812 | |
|
2813 | 2813 | def prep(ctx, fns): |
|
2814 | 2814 | rev = ctx.rev() |
|
2815 | 2815 | pctx = ctx.p1() |
|
2816 | 2816 | parent = pctx.rev() |
|
2817 | 2817 | matches.setdefault(rev, {}) |
|
2818 | 2818 | matches.setdefault(parent, {}) |
|
2819 | 2819 | files = revfiles.setdefault(rev, []) |
|
2820 | 2820 | for fn in fns: |
|
2821 | 2821 | flog = getfile(fn) |
|
2822 | 2822 | try: |
|
2823 | 2823 | fnode = ctx.filenode(fn) |
|
2824 | 2824 | except error.LookupError: |
|
2825 | 2825 | continue |
|
2826 | 2826 | |
|
2827 | 2827 | copied = flog.renamed(fnode) |
|
2828 | 2828 | copy = follow and copied and copied[0] |
|
2829 | 2829 | if copy: |
|
2830 | 2830 | copies.setdefault(rev, {})[fn] = copy |
|
2831 | 2831 | if fn in skip: |
|
2832 | 2832 | if copy: |
|
2833 | 2833 | skip[copy] = True |
|
2834 | 2834 | continue |
|
2835 | 2835 | files.append(fn) |
|
2836 | 2836 | |
|
2837 | 2837 | if fn not in matches[rev]: |
|
2838 | 2838 | grepbody(fn, rev, flog.read(fnode)) |
|
2839 | 2839 | |
|
2840 | 2840 | pfn = copy or fn |
|
2841 | 2841 | if pfn not in matches[parent]: |
|
2842 | 2842 | try: |
|
2843 | 2843 | fnode = pctx.filenode(pfn) |
|
2844 | 2844 | grepbody(pfn, parent, flog.read(fnode)) |
|
2845 | 2845 | except error.LookupError: |
|
2846 | 2846 | pass |
|
2847 | 2847 | |
|
2848 | 2848 | for ctx in cmdutil.walkchangerevs(repo, matchfn, opts, prep): |
|
2849 | 2849 | rev = ctx.rev() |
|
2850 | 2850 | parent = ctx.p1().rev() |
|
2851 | 2851 | for fn in sorted(revfiles.get(rev, [])): |
|
2852 | 2852 | states = matches[rev][fn] |
|
2853 | 2853 | copy = copies.get(rev, {}).get(fn) |
|
2854 | 2854 | if fn in skip: |
|
2855 | 2855 | if copy: |
|
2856 | 2856 | skip[copy] = True |
|
2857 | 2857 | continue |
|
2858 | 2858 | pstates = matches.get(parent, {}).get(copy or fn, []) |
|
2859 | 2859 | if pstates or states: |
|
2860 | 2860 | r = display(fn, ctx, pstates, states) |
|
2861 | 2861 | found = found or r |
|
2862 | 2862 | if r and not opts.get('all'): |
|
2863 | 2863 | skip[fn] = True |
|
2864 | 2864 | if copy: |
|
2865 | 2865 | skip[copy] = True |
|
2866 | 2866 | del matches[rev] |
|
2867 | 2867 | del revfiles[rev] |
|
2868 | 2868 | |
|
2869 | 2869 | return not found |
|
2870 | 2870 | |
|
2871 | 2871 | @command('heads', |
|
2872 | 2872 | [('r', 'rev', '', |
|
2873 | 2873 | _('show only heads which are descendants of STARTREV'), _('STARTREV')), |
|
2874 | 2874 | ('t', 'topo', False, _('show topological heads only')), |
|
2875 | 2875 | ('a', 'active', False, _('show active branchheads only (DEPRECATED)')), |
|
2876 | 2876 | ('c', 'closed', False, _('show normal and closed branch heads')), |
|
2877 | 2877 | ] + templateopts, |
|
2878 | 2878 | _('[-ac] [-r STARTREV] [REV]...')) |
|
2879 | 2879 | def heads(ui, repo, *branchrevs, **opts): |
|
2880 | 2880 | """show current repository heads or show branch heads |
|
2881 | 2881 | |
|
2882 | 2882 | With no arguments, show all repository branch heads. |
|
2883 | 2883 | |
|
2884 | 2884 | Repository "heads" are changesets with no child changesets. They are |
|
2885 | 2885 | where development generally takes place and are the usual targets |
|
2886 | 2886 | for update and merge operations. Branch heads are changesets that have |
|
2887 | 2887 | no child changeset on the same branch. |
|
2888 | 2888 | |
|
2889 | 2889 | If one or more REVs are given, only branch heads on the branches |
|
2890 | 2890 | associated with the specified changesets are shown. This means |
|
2891 | 2891 | that you can use :hg:`heads foo` to see the heads on a branch |
|
2892 | 2892 | named ``foo``. |
|
2893 | 2893 | |
|
2894 | 2894 | If -c/--closed is specified, also show branch heads marked closed |
|
2895 | 2895 | (see :hg:`commit --close-branch`). |
|
2896 | 2896 | |
|
2897 | 2897 | If STARTREV is specified, only those heads that are descendants of |
|
2898 | 2898 | STARTREV will be displayed. |
|
2899 | 2899 | |
|
2900 | 2900 | If -t/--topo is specified, named branch mechanics will be ignored and only |
|
2901 | 2901 | changesets without children will be shown. |
|
2902 | 2902 | |
|
2903 | 2903 | Returns 0 if matching heads are found, 1 if not. |
|
2904 | 2904 | """ |
|
2905 | 2905 | |
|
2906 | 2906 | start = None |
|
2907 | 2907 | if 'rev' in opts: |
|
2908 | 2908 | start = scmutil.revsingle(repo, opts['rev'], None).node() |
|
2909 | 2909 | |
|
2910 | 2910 | if opts.get('topo'): |
|
2911 | 2911 | heads = [repo[h] for h in repo.heads(start)] |
|
2912 | 2912 | else: |
|
2913 | 2913 | heads = [] |
|
2914 | 2914 | for branch in repo.branchmap(): |
|
2915 | 2915 | heads += repo.branchheads(branch, start, opts.get('closed')) |
|
2916 | 2916 | heads = [repo[h] for h in heads] |
|
2917 | 2917 | |
|
2918 | 2918 | if branchrevs: |
|
2919 | 2919 | branches = set(repo[br].branch() for br in branchrevs) |
|
2920 | 2920 | heads = [h for h in heads if h.branch() in branches] |
|
2921 | 2921 | |
|
2922 | 2922 | if opts.get('active') and branchrevs: |
|
2923 | 2923 | dagheads = repo.heads(start) |
|
2924 | 2924 | heads = [h for h in heads if h.node() in dagheads] |
|
2925 | 2925 | |
|
2926 | 2926 | if branchrevs: |
|
2927 | 2927 | haveheads = set(h.branch() for h in heads) |
|
2928 | 2928 | if branches - haveheads: |
|
2929 | 2929 | headless = ', '.join(b for b in branches - haveheads) |
|
2930 | 2930 | msg = _('no open branch heads found on branches %s') |
|
2931 | 2931 | if opts.get('rev'): |
|
2932 | 2932 | msg += _(' (started at %s)' % opts['rev']) |
|
2933 | 2933 | ui.warn((msg + '\n') % headless) |
|
2934 | 2934 | |
|
2935 | 2935 | if not heads: |
|
2936 | 2936 | return 1 |
|
2937 | 2937 | |
|
2938 | 2938 | heads = sorted(heads, key=lambda x: -x.rev()) |
|
2939 | 2939 | displayer = cmdutil.show_changeset(ui, repo, opts) |
|
2940 | 2940 | for ctx in heads: |
|
2941 | 2941 | displayer.show(ctx) |
|
2942 | 2942 | displayer.close() |
|
2943 | 2943 | |
|
2944 | 2944 | @command('help', |
|
2945 | 2945 | [('e', 'extension', None, _('show only help for extensions')), |
|
2946 | 2946 | ('c', 'command', None, _('show only help for commands'))], |
|
2947 | 2947 | _('[-ec] [TOPIC]')) |
|
2948 | 2948 | def help_(ui, name=None, unknowncmd=False, full=True, **opts): |
|
2949 | 2949 | """show help for a given topic or a help overview |
|
2950 | 2950 | |
|
2951 | 2951 | With no arguments, print a list of commands with short help messages. |
|
2952 | 2952 | |
|
2953 | 2953 | Given a topic, extension, or command name, print help for that |
|
2954 | 2954 | topic. |
|
2955 | 2955 | |
|
2956 | 2956 | Returns 0 if successful. |
|
2957 | 2957 | """ |
|
2958 | 2958 | |
|
2959 | 2959 | textwidth = min(ui.termwidth(), 80) - 2 |
|
2960 | 2960 | |
|
2961 | 2961 | def optrst(options): |
|
2962 | 2962 | data = [] |
|
2963 | 2963 | multioccur = False |
|
2964 | 2964 | for option in options: |
|
2965 | 2965 | if len(option) == 5: |
|
2966 | 2966 | shortopt, longopt, default, desc, optlabel = option |
|
2967 | 2967 | else: |
|
2968 | 2968 | shortopt, longopt, default, desc = option |
|
2969 | 2969 | optlabel = _("VALUE") # default label |
|
2970 | 2970 | |
|
2971 | 2971 | if _("DEPRECATED") in desc and not ui.verbose: |
|
2972 | 2972 | continue |
|
2973 | 2973 | |
|
2974 | 2974 | so = '' |
|
2975 | 2975 | if shortopt: |
|
2976 | 2976 | so = '-' + shortopt |
|
2977 | 2977 | lo = '--' + longopt |
|
2978 | 2978 | if default: |
|
2979 | 2979 | desc += _(" (default: %s)") % default |
|
2980 | 2980 | |
|
2981 | 2981 | if isinstance(default, list): |
|
2982 | 2982 | lo += " %s [+]" % optlabel |
|
2983 | 2983 | multioccur = True |
|
2984 | 2984 | elif (default is not None) and not isinstance(default, bool): |
|
2985 | 2985 | lo += " %s" % optlabel |
|
2986 | 2986 | |
|
2987 | 2987 | data.append((so, lo, desc)) |
|
2988 | 2988 | |
|
2989 | 2989 | rst = minirst.maketable(data, 1) |
|
2990 | 2990 | |
|
2991 | 2991 | if multioccur: |
|
2992 | 2992 | rst += _("\n[+] marked option can be specified multiple times\n") |
|
2993 | 2993 | |
|
2994 | 2994 | return rst |
|
2995 | 2995 | |
|
2996 | 2996 | # list all option lists |
|
2997 | 2997 | def opttext(optlist, width): |
|
2998 | 2998 | rst = '' |
|
2999 | 2999 | if not optlist: |
|
3000 | 3000 | return '' |
|
3001 | 3001 | |
|
3002 | 3002 | for title, options in optlist: |
|
3003 | 3003 | rst += '\n%s\n' % title |
|
3004 | 3004 | if options: |
|
3005 | 3005 | rst += "\n" |
|
3006 | 3006 | rst += optrst(options) |
|
3007 | 3007 | rst += '\n' |
|
3008 | 3008 | |
|
3009 | 3009 | return '\n' + minirst.format(rst, width) |
|
3010 | 3010 | |
|
3011 | 3011 | def addglobalopts(optlist, aliases): |
|
3012 | 3012 | if ui.quiet: |
|
3013 | 3013 | return [] |
|
3014 | 3014 | |
|
3015 | 3015 | if ui.verbose: |
|
3016 | 3016 | optlist.append((_("global options:"), globalopts)) |
|
3017 | 3017 | if name == 'shortlist': |
|
3018 | 3018 | optlist.append((_('use "hg help" for the full list ' |
|
3019 | 3019 | 'of commands'), ())) |
|
3020 | 3020 | else: |
|
3021 | 3021 | if name == 'shortlist': |
|
3022 | 3022 | msg = _('use "hg help" for the full list of commands ' |
|
3023 | 3023 | 'or "hg -v" for details') |
|
3024 | 3024 | elif name and not full: |
|
3025 | 3025 | msg = _('use "hg help %s" to show the full help text' % name) |
|
3026 | 3026 | elif aliases: |
|
3027 | 3027 | msg = _('use "hg -v help%s" to show builtin aliases and ' |
|
3028 | 3028 | 'global options') % (name and " " + name or "") |
|
3029 | 3029 | else: |
|
3030 | 3030 | msg = _('use "hg -v help %s" to show more info') % name |
|
3031 | 3031 | optlist.append((msg, ())) |
|
3032 | 3032 | |
|
3033 | 3033 | def helpcmd(name): |
|
3034 | 3034 | try: |
|
3035 | 3035 | aliases, entry = cmdutil.findcmd(name, table, strict=unknowncmd) |
|
3036 | 3036 | except error.AmbiguousCommand, inst: |
|
3037 | 3037 | # py3k fix: except vars can't be used outside the scope of the |
|
3038 | 3038 | # except block, nor can be used inside a lambda. python issue4617 |
|
3039 | 3039 | prefix = inst.args[0] |
|
3040 | 3040 | select = lambda c: c.lstrip('^').startswith(prefix) |
|
3041 | 3041 | helplist(select) |
|
3042 | 3042 | return |
|
3043 | 3043 | |
|
3044 | 3044 | # check if it's an invalid alias and display its error if it is |
|
3045 | 3045 | if getattr(entry[0], 'badalias', False): |
|
3046 | 3046 | if not unknowncmd: |
|
3047 | 3047 | entry[0](ui) |
|
3048 | 3048 | return |
|
3049 | 3049 | |
|
3050 | 3050 | rst = "" |
|
3051 | 3051 | |
|
3052 | 3052 | # synopsis |
|
3053 | 3053 | if len(entry) > 2: |
|
3054 | 3054 | if entry[2].startswith('hg'): |
|
3055 | 3055 | rst += "%s\n" % entry[2] |
|
3056 | 3056 | else: |
|
3057 | 3057 | rst += 'hg %s %s\n' % (aliases[0], entry[2]) |
|
3058 | 3058 | else: |
|
3059 | 3059 | rst += 'hg %s\n' % aliases[0] |
|
3060 | 3060 | |
|
3061 | 3061 | # aliases |
|
3062 | 3062 | if full and not ui.quiet and len(aliases) > 1: |
|
3063 | 3063 | rst += _("\naliases: %s\n") % ', '.join(aliases[1:]) |
|
3064 | 3064 | |
|
3065 | 3065 | # description |
|
3066 | 3066 | doc = gettext(entry[0].__doc__) |
|
3067 | 3067 | if not doc: |
|
3068 | 3068 | doc = _("(no help text available)") |
|
3069 | 3069 | if util.safehasattr(entry[0], 'definition'): # aliased command |
|
3070 | 3070 | if entry[0].definition.startswith('!'): # shell alias |
|
3071 | 3071 | doc = _('shell alias for::\n\n %s') % entry[0].definition[1:] |
|
3072 | 3072 | else: |
|
3073 | 3073 | doc = _('alias for: hg %s\n\n%s') % (entry[0].definition, doc) |
|
3074 | 3074 | if ui.quiet or not full: |
|
3075 | 3075 | doc = doc.splitlines()[0] |
|
3076 | 3076 | rst += "\n" + doc + "\n" |
|
3077 | 3077 | |
|
3078 | 3078 | # check if this command shadows a non-trivial (multi-line) |
|
3079 | 3079 | # extension help text |
|
3080 | 3080 | try: |
|
3081 | 3081 | mod = extensions.find(name) |
|
3082 | 3082 | doc = gettext(mod.__doc__) or '' |
|
3083 | 3083 | if '\n' in doc.strip(): |
|
3084 | 3084 | msg = _('use "hg help -e %s" to show help for ' |
|
3085 | 3085 | 'the %s extension') % (name, name) |
|
3086 | 3086 | rst += '\n%s\n' % msg |
|
3087 | 3087 | except KeyError: |
|
3088 | 3088 | pass |
|
3089 | 3089 | |
|
3090 | 3090 | # options |
|
3091 | 3091 | if not ui.quiet and entry[1]: |
|
3092 | 3092 | rst += '\noptions:\n\n' |
|
3093 | 3093 | rst += optrst(entry[1]) |
|
3094 | 3094 | |
|
3095 | 3095 | if ui.verbose: |
|
3096 | 3096 | rst += '\nglobal options:\n\n' |
|
3097 | 3097 | rst += optrst(globalopts) |
|
3098 | 3098 | |
|
3099 | 3099 | keep = ui.verbose and ['verbose'] or [] |
|
3100 | 3100 | formatted, pruned = minirst.format(rst, textwidth, keep=keep) |
|
3101 | 3101 | ui.write(formatted) |
|
3102 | 3102 | |
|
3103 | 3103 | if not ui.verbose: |
|
3104 | 3104 | if not full: |
|
3105 | 3105 | ui.write(_('\nuse "hg help %s" to show the full help text\n') |
|
3106 | 3106 | % name) |
|
3107 | 3107 | elif not ui.quiet: |
|
3108 | 3108 | ui.write(_('\nuse "hg -v help %s" to show more info\n') % name) |
|
3109 | 3109 | |
|
3110 | 3110 | |
|
3111 | 3111 | def helplist(select=None): |
|
3112 | 3112 | # list of commands |
|
3113 | 3113 | if name == "shortlist": |
|
3114 | 3114 | header = _('basic commands:\n\n') |
|
3115 | 3115 | else: |
|
3116 | 3116 | header = _('list of commands:\n\n') |
|
3117 | 3117 | |
|
3118 | 3118 | h = {} |
|
3119 | 3119 | cmds = {} |
|
3120 | 3120 | for c, e in table.iteritems(): |
|
3121 | 3121 | f = c.split("|", 1)[0] |
|
3122 | 3122 | if select and not select(f): |
|
3123 | 3123 | continue |
|
3124 | 3124 | if (not select and name != 'shortlist' and |
|
3125 | 3125 | e[0].__module__ != __name__): |
|
3126 | 3126 | continue |
|
3127 | 3127 | if name == "shortlist" and not f.startswith("^"): |
|
3128 | 3128 | continue |
|
3129 | 3129 | f = f.lstrip("^") |
|
3130 | 3130 | if not ui.debugflag and f.startswith("debug"): |
|
3131 | 3131 | continue |
|
3132 | 3132 | doc = e[0].__doc__ |
|
3133 | 3133 | if doc and 'DEPRECATED' in doc and not ui.verbose: |
|
3134 | 3134 | continue |
|
3135 | 3135 | doc = gettext(doc) |
|
3136 | 3136 | if not doc: |
|
3137 | 3137 | doc = _("(no help text available)") |
|
3138 | 3138 | h[f] = doc.splitlines()[0].rstrip() |
|
3139 | 3139 | cmds[f] = c.lstrip("^") |
|
3140 | 3140 | |
|
3141 | 3141 | if not h: |
|
3142 | 3142 | ui.status(_('no commands defined\n')) |
|
3143 | 3143 | return |
|
3144 | 3144 | |
|
3145 | 3145 | ui.status(header) |
|
3146 | 3146 | fns = sorted(h) |
|
3147 | 3147 | m = max(map(len, fns)) |
|
3148 | 3148 | for f in fns: |
|
3149 | 3149 | if ui.verbose: |
|
3150 | 3150 | commands = cmds[f].replace("|",", ") |
|
3151 | 3151 | ui.write(" %s:\n %s\n"%(commands, h[f])) |
|
3152 | 3152 | else: |
|
3153 | 3153 | ui.write('%s\n' % (util.wrap(h[f], textwidth, |
|
3154 | 3154 | initindent=' %-*s ' % (m, f), |
|
3155 | 3155 | hangindent=' ' * (m + 4)))) |
|
3156 | 3156 | |
|
3157 | 3157 | if not name: |
|
3158 | 3158 | text = help.listexts(_('enabled extensions:'), extensions.enabled()) |
|
3159 | 3159 | if text: |
|
3160 | 3160 | ui.write("\n%s" % minirst.format(text, textwidth)) |
|
3161 | 3161 | |
|
3162 | 3162 | ui.write(_("\nadditional help topics:\n\n")) |
|
3163 | 3163 | topics = [] |
|
3164 | 3164 | for names, header, doc in help.helptable: |
|
3165 | 3165 | topics.append((sorted(names, key=len, reverse=True)[0], header)) |
|
3166 | 3166 | topics_len = max([len(s[0]) for s in topics]) |
|
3167 | 3167 | for t, desc in topics: |
|
3168 | 3168 | ui.write(" %-*s %s\n" % (topics_len, t, desc)) |
|
3169 | 3169 | |
|
3170 | 3170 | optlist = [] |
|
3171 | 3171 | addglobalopts(optlist, True) |
|
3172 | 3172 | ui.write(opttext(optlist, textwidth)) |
|
3173 | 3173 | |
|
3174 | 3174 | def helptopic(name): |
|
3175 | 3175 | for names, header, doc in help.helptable: |
|
3176 | 3176 | if name in names: |
|
3177 | 3177 | break |
|
3178 | 3178 | else: |
|
3179 | 3179 | raise error.UnknownCommand(name) |
|
3180 | 3180 | |
|
3181 | 3181 | # description |
|
3182 | 3182 | if not doc: |
|
3183 | 3183 | doc = _("(no help text available)") |
|
3184 | 3184 | if util.safehasattr(doc, '__call__'): |
|
3185 | 3185 | doc = doc() |
|
3186 | 3186 | |
|
3187 | 3187 | ui.write("%s\n\n" % header) |
|
3188 | 3188 | ui.write("%s" % minirst.format(doc, textwidth, indent=4)) |
|
3189 | 3189 | try: |
|
3190 | 3190 | cmdutil.findcmd(name, table) |
|
3191 | 3191 | ui.write(_('\nuse "hg help -c %s" to see help for ' |
|
3192 | 3192 | 'the %s command\n') % (name, name)) |
|
3193 | 3193 | except error.UnknownCommand: |
|
3194 | 3194 | pass |
|
3195 | 3195 | |
|
3196 | 3196 | def helpext(name): |
|
3197 | 3197 | try: |
|
3198 | 3198 | mod = extensions.find(name) |
|
3199 | 3199 | doc = gettext(mod.__doc__) or _('no help text available') |
|
3200 | 3200 | except KeyError: |
|
3201 | 3201 | mod = None |
|
3202 | 3202 | doc = extensions.disabledext(name) |
|
3203 | 3203 | if not doc: |
|
3204 | 3204 | raise error.UnknownCommand(name) |
|
3205 | 3205 | |
|
3206 | 3206 | if '\n' not in doc: |
|
3207 | 3207 | head, tail = doc, "" |
|
3208 | 3208 | else: |
|
3209 | 3209 | head, tail = doc.split('\n', 1) |
|
3210 | 3210 | ui.write(_('%s extension - %s\n\n') % (name.split('.')[-1], head)) |
|
3211 | 3211 | if tail: |
|
3212 | 3212 | ui.write(minirst.format(tail, textwidth)) |
|
3213 | 3213 | ui.status('\n') |
|
3214 | 3214 | |
|
3215 | 3215 | if mod: |
|
3216 | 3216 | try: |
|
3217 | 3217 | ct = mod.cmdtable |
|
3218 | 3218 | except AttributeError: |
|
3219 | 3219 | ct = {} |
|
3220 | 3220 | modcmds = set([c.split('|', 1)[0] for c in ct]) |
|
3221 | 3221 | helplist(modcmds.__contains__) |
|
3222 | 3222 | else: |
|
3223 | 3223 | ui.write(_('use "hg help extensions" for information on enabling ' |
|
3224 | 3224 | 'extensions\n')) |
|
3225 | 3225 | |
|
3226 | 3226 | def helpextcmd(name): |
|
3227 | 3227 | cmd, ext, mod = extensions.disabledcmd(ui, name, ui.config('ui', 'strict')) |
|
3228 | 3228 | doc = gettext(mod.__doc__).splitlines()[0] |
|
3229 | 3229 | |
|
3230 | 3230 | msg = help.listexts(_("'%s' is provided by the following " |
|
3231 | 3231 | "extension:") % cmd, {ext: doc}, indent=4) |
|
3232 | 3232 | ui.write(minirst.format(msg, textwidth)) |
|
3233 | 3233 | ui.write('\n') |
|
3234 | 3234 | ui.write(_('use "hg help extensions" for information on enabling ' |
|
3235 | 3235 | 'extensions\n')) |
|
3236 | 3236 | |
|
3237 | 3237 | if name and name != 'shortlist': |
|
3238 | 3238 | i = None |
|
3239 | 3239 | if unknowncmd: |
|
3240 | 3240 | queries = (helpextcmd,) |
|
3241 | 3241 | elif opts.get('extension'): |
|
3242 | 3242 | queries = (helpext,) |
|
3243 | 3243 | elif opts.get('command'): |
|
3244 | 3244 | queries = (helpcmd,) |
|
3245 | 3245 | else: |
|
3246 | 3246 | queries = (helptopic, helpcmd, helpext, helpextcmd) |
|
3247 | 3247 | for f in queries: |
|
3248 | 3248 | try: |
|
3249 | 3249 | f(name) |
|
3250 | 3250 | i = None |
|
3251 | 3251 | break |
|
3252 | 3252 | except error.UnknownCommand, inst: |
|
3253 | 3253 | i = inst |
|
3254 | 3254 | if i: |
|
3255 | 3255 | raise i |
|
3256 | 3256 | else: |
|
3257 | 3257 | # program name |
|
3258 | 3258 | ui.status(_("Mercurial Distributed SCM\n")) |
|
3259 | 3259 | ui.status('\n') |
|
3260 | 3260 | helplist() |
|
3261 | 3261 | |
|
3262 | 3262 | |
|
3263 | 3263 | @command('identify|id', |
|
3264 | 3264 | [('r', 'rev', '', |
|
3265 | 3265 | _('identify the specified revision'), _('REV')), |
|
3266 | 3266 | ('n', 'num', None, _('show local revision number')), |
|
3267 | 3267 | ('i', 'id', None, _('show global revision id')), |
|
3268 | 3268 | ('b', 'branch', None, _('show branch')), |
|
3269 | 3269 | ('t', 'tags', None, _('show tags')), |
|
3270 | 3270 | ('B', 'bookmarks', None, _('show bookmarks')), |
|
3271 | 3271 | ] + remoteopts, |
|
3272 | 3272 | _('[-nibtB] [-r REV] [SOURCE]')) |
|
3273 | 3273 | def identify(ui, repo, source=None, rev=None, |
|
3274 | 3274 | num=None, id=None, branch=None, tags=None, bookmarks=None, **opts): |
|
3275 | 3275 | """identify the working copy or specified revision |
|
3276 | 3276 | |
|
3277 | 3277 | Print a summary identifying the repository state at REV using one or |
|
3278 | 3278 | two parent hash identifiers, followed by a "+" if the working |
|
3279 | 3279 | directory has uncommitted changes, the branch name (if not default), |
|
3280 | 3280 | a list of tags, and a list of bookmarks. |
|
3281 | 3281 | |
|
3282 | 3282 | When REV is not given, print a summary of the current state of the |
|
3283 | 3283 | repository. |
|
3284 | 3284 | |
|
3285 | 3285 | Specifying a path to a repository root or Mercurial bundle will |
|
3286 | 3286 | cause lookup to operate on that repository/bundle. |
|
3287 | 3287 | |
|
3288 | 3288 | .. container:: verbose |
|
3289 | 3289 | |
|
3290 | 3290 | Examples: |
|
3291 | 3291 | |
|
3292 | 3292 | - generate a build identifier for the working directory:: |
|
3293 | 3293 | |
|
3294 | 3294 | hg id --id > build-id.dat |
|
3295 | 3295 | |
|
3296 | 3296 | - find the revision corresponding to a tag:: |
|
3297 | 3297 | |
|
3298 | 3298 | hg id -n -r 1.3 |
|
3299 | 3299 | |
|
3300 | 3300 | - check the most recent revision of a remote repository:: |
|
3301 | 3301 | |
|
3302 | 3302 | hg id -r tip http://selenic.com/hg/ |
|
3303 | 3303 | |
|
3304 | 3304 | Returns 0 if successful. |
|
3305 | 3305 | """ |
|
3306 | 3306 | |
|
3307 | 3307 | if not repo and not source: |
|
3308 | 3308 | raise util.Abort(_("there is no Mercurial repository here " |
|
3309 | 3309 | "(.hg not found)")) |
|
3310 | 3310 | |
|
3311 | 3311 | hexfunc = ui.debugflag and hex or short |
|
3312 | 3312 | default = not (num or id or branch or tags or bookmarks) |
|
3313 | 3313 | output = [] |
|
3314 | 3314 | revs = [] |
|
3315 | 3315 | |
|
3316 | 3316 | if source: |
|
3317 | 3317 | source, branches = hg.parseurl(ui.expandpath(source)) |
|
3318 | 3318 | repo = hg.peer(ui, opts, source) |
|
3319 | 3319 | revs, checkout = hg.addbranchrevs(repo, repo, branches, None) |
|
3320 | 3320 | |
|
3321 | 3321 | if not repo.local(): |
|
3322 | 3322 | if num or branch or tags: |
|
3323 | 3323 | raise util.Abort( |
|
3324 | 3324 | _("can't query remote revision number, branch, or tags")) |
|
3325 | 3325 | if not rev and revs: |
|
3326 | 3326 | rev = revs[0] |
|
3327 | 3327 | if not rev: |
|
3328 | 3328 | rev = "tip" |
|
3329 | 3329 | |
|
3330 | 3330 | remoterev = repo.lookup(rev) |
|
3331 | 3331 | if default or id: |
|
3332 | 3332 | output = [hexfunc(remoterev)] |
|
3333 | 3333 | |
|
3334 | 3334 | def getbms(): |
|
3335 | 3335 | bms = [] |
|
3336 | 3336 | |
|
3337 | 3337 | if 'bookmarks' in repo.listkeys('namespaces'): |
|
3338 | 3338 | hexremoterev = hex(remoterev) |
|
3339 | 3339 | bms = [bm for bm, bmr in repo.listkeys('bookmarks').iteritems() |
|
3340 | 3340 | if bmr == hexremoterev] |
|
3341 | 3341 | |
|
3342 | 3342 | return bms |
|
3343 | 3343 | |
|
3344 | 3344 | if bookmarks: |
|
3345 | 3345 | output.extend(getbms()) |
|
3346 | 3346 | elif default and not ui.quiet: |
|
3347 | 3347 | # multiple bookmarks for a single parent separated by '/' |
|
3348 | 3348 | bm = '/'.join(getbms()) |
|
3349 | 3349 | if bm: |
|
3350 | 3350 | output.append(bm) |
|
3351 | 3351 | else: |
|
3352 | 3352 | if not rev: |
|
3353 | 3353 | ctx = repo[None] |
|
3354 | 3354 | parents = ctx.parents() |
|
3355 | 3355 | changed = "" |
|
3356 | 3356 | if default or id or num: |
|
3357 | 3357 | changed = util.any(repo.status()) and "+" or "" |
|
3358 | 3358 | if default or id: |
|
3359 | 3359 | output = ["%s%s" % |
|
3360 | 3360 | ('+'.join([hexfunc(p.node()) for p in parents]), changed)] |
|
3361 | 3361 | if num: |
|
3362 | 3362 | output.append("%s%s" % |
|
3363 | 3363 | ('+'.join([str(p.rev()) for p in parents]), changed)) |
|
3364 | 3364 | else: |
|
3365 | 3365 | ctx = scmutil.revsingle(repo, rev) |
|
3366 | 3366 | if default or id: |
|
3367 | 3367 | output = [hexfunc(ctx.node())] |
|
3368 | 3368 | if num: |
|
3369 | 3369 | output.append(str(ctx.rev())) |
|
3370 | 3370 | |
|
3371 | 3371 | if default and not ui.quiet: |
|
3372 | 3372 | b = ctx.branch() |
|
3373 | 3373 | if b != 'default': |
|
3374 | 3374 | output.append("(%s)" % b) |
|
3375 | 3375 | |
|
3376 | 3376 | # multiple tags for a single parent separated by '/' |
|
3377 | 3377 | t = '/'.join(ctx.tags()) |
|
3378 | 3378 | if t: |
|
3379 | 3379 | output.append(t) |
|
3380 | 3380 | |
|
3381 | 3381 | # multiple bookmarks for a single parent separated by '/' |
|
3382 | 3382 | bm = '/'.join(ctx.bookmarks()) |
|
3383 | 3383 | if bm: |
|
3384 | 3384 | output.append(bm) |
|
3385 | 3385 | else: |
|
3386 | 3386 | if branch: |
|
3387 | 3387 | output.append(ctx.branch()) |
|
3388 | 3388 | |
|
3389 | 3389 | if tags: |
|
3390 | 3390 | output.extend(ctx.tags()) |
|
3391 | 3391 | |
|
3392 | 3392 | if bookmarks: |
|
3393 | 3393 | output.extend(ctx.bookmarks()) |
|
3394 | 3394 | |
|
3395 | 3395 | ui.write("%s\n" % ' '.join(output)) |
|
3396 | 3396 | |
|
3397 | 3397 | @command('import|patch', |
|
3398 | 3398 | [('p', 'strip', 1, |
|
3399 | 3399 | _('directory strip option for patch. This has the same ' |
|
3400 | 3400 | 'meaning as the corresponding patch option'), _('NUM')), |
|
3401 | 3401 | ('b', 'base', '', _('base path (DEPRECATED)'), _('PATH')), |
|
3402 | 3402 | ('e', 'edit', False, _('invoke editor on commit messages')), |
|
3403 | 3403 | ('f', 'force', None, _('skip check for outstanding uncommitted changes')), |
|
3404 | 3404 | ('', 'no-commit', None, |
|
3405 | 3405 | _("don't commit, just update the working directory")), |
|
3406 | 3406 | ('', 'bypass', None, |
|
3407 | 3407 | _("apply patch without touching the working directory")), |
|
3408 | 3408 | ('', 'exact', None, |
|
3409 | 3409 | _('apply patch to the nodes from which it was generated')), |
|
3410 | 3410 | ('', 'import-branch', None, |
|
3411 | 3411 | _('use any branch information in patch (implied by --exact)'))] + |
|
3412 | 3412 | commitopts + commitopts2 + similarityopts, |
|
3413 | 3413 | _('[OPTION]... PATCH...')) |
|
3414 | 3414 | def import_(ui, repo, patch1=None, *patches, **opts): |
|
3415 | 3415 | """import an ordered set of patches |
|
3416 | 3416 | |
|
3417 | 3417 | Import a list of patches and commit them individually (unless |
|
3418 | 3418 | --no-commit is specified). |
|
3419 | 3419 | |
|
3420 | 3420 | If there are outstanding changes in the working directory, import |
|
3421 | 3421 | will abort unless given the -f/--force flag. |
|
3422 | 3422 | |
|
3423 | 3423 | You can import a patch straight from a mail message. Even patches |
|
3424 | 3424 | as attachments work (to use the body part, it must have type |
|
3425 | 3425 | text/plain or text/x-patch). From and Subject headers of email |
|
3426 | 3426 | message are used as default committer and commit message. All |
|
3427 | 3427 | text/plain body parts before first diff are added to commit |
|
3428 | 3428 | message. |
|
3429 | 3429 | |
|
3430 | 3430 | If the imported patch was generated by :hg:`export`, user and |
|
3431 | 3431 | description from patch override values from message headers and |
|
3432 | 3432 | body. Values given on command line with -m/--message and -u/--user |
|
3433 | 3433 | override these. |
|
3434 | 3434 | |
|
3435 | 3435 | If --exact is specified, import will set the working directory to |
|
3436 | 3436 | the parent of each patch before applying it, and will abort if the |
|
3437 | 3437 | resulting changeset has a different ID than the one recorded in |
|
3438 | 3438 | the patch. This may happen due to character set problems or other |
|
3439 | 3439 | deficiencies in the text patch format. |
|
3440 | 3440 | |
|
3441 | 3441 | Use --bypass to apply and commit patches directly to the |
|
3442 | 3442 | repository, not touching the working directory. Without --exact, |
|
3443 | 3443 | patches will be applied on top of the working directory parent |
|
3444 | 3444 | revision. |
|
3445 | 3445 | |
|
3446 | 3446 | With -s/--similarity, hg will attempt to discover renames and |
|
3447 | 3447 | copies in the patch in the same way as 'addremove'. |
|
3448 | 3448 | |
|
3449 | 3449 | To read a patch from standard input, use "-" as the patch name. If |
|
3450 | 3450 | a URL is specified, the patch will be downloaded from it. |
|
3451 | 3451 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
3452 | 3452 | |
|
3453 | 3453 | .. container:: verbose |
|
3454 | 3454 | |
|
3455 | 3455 | Examples: |
|
3456 | 3456 | |
|
3457 | 3457 | - import a traditional patch from a website and detect renames:: |
|
3458 | 3458 | |
|
3459 | 3459 | hg import -s 80 http://example.com/bugfix.patch |
|
3460 | 3460 | |
|
3461 | 3461 | - import a changeset from an hgweb server:: |
|
3462 | 3462 | |
|
3463 | 3463 | hg import http://www.selenic.com/hg/rev/5ca8c111e9aa |
|
3464 | 3464 | |
|
3465 | 3465 | - import all the patches in an Unix-style mbox:: |
|
3466 | 3466 | |
|
3467 | 3467 | hg import incoming-patches.mbox |
|
3468 | 3468 | |
|
3469 | 3469 | - attempt to exactly restore an exported changeset (not always |
|
3470 | 3470 | possible):: |
|
3471 | 3471 | |
|
3472 | 3472 | hg import --exact proposed-fix.patch |
|
3473 | 3473 | |
|
3474 | 3474 | Returns 0 on success. |
|
3475 | 3475 | """ |
|
3476 | 3476 | |
|
3477 | 3477 | if not patch1: |
|
3478 | 3478 | raise util.Abort(_('need at least one patch to import')) |
|
3479 | 3479 | |
|
3480 | 3480 | patches = (patch1,) + patches |
|
3481 | 3481 | |
|
3482 | 3482 | date = opts.get('date') |
|
3483 | 3483 | if date: |
|
3484 | 3484 | opts['date'] = util.parsedate(date) |
|
3485 | 3485 | |
|
3486 | 3486 | editor = cmdutil.commiteditor |
|
3487 | 3487 | if opts.get('edit'): |
|
3488 | 3488 | editor = cmdutil.commitforceeditor |
|
3489 | 3489 | |
|
3490 | 3490 | update = not opts.get('bypass') |
|
3491 | 3491 | if not update and opts.get('no_commit'): |
|
3492 | 3492 | raise util.Abort(_('cannot use --no-commit with --bypass')) |
|
3493 | 3493 | try: |
|
3494 | 3494 | sim = float(opts.get('similarity') or 0) |
|
3495 | 3495 | except ValueError: |
|
3496 | 3496 | raise util.Abort(_('similarity must be a number')) |
|
3497 | 3497 | if sim < 0 or sim > 100: |
|
3498 | 3498 | raise util.Abort(_('similarity must be between 0 and 100')) |
|
3499 | 3499 | if sim and not update: |
|
3500 | 3500 | raise util.Abort(_('cannot use --similarity with --bypass')) |
|
3501 | 3501 | |
|
3502 | 3502 | if (opts.get('exact') or not opts.get('force')) and update: |
|
3503 | 3503 | cmdutil.bailifchanged(repo) |
|
3504 | 3504 | |
|
3505 | 3505 | base = opts["base"] |
|
3506 | 3506 | strip = opts["strip"] |
|
3507 | 3507 | wlock = lock = tr = None |
|
3508 | 3508 | msgs = [] |
|
3509 | 3509 | |
|
3510 | 3510 | def checkexact(repo, n, nodeid): |
|
3511 | 3511 | if opts.get('exact') and hex(n) != nodeid: |
|
3512 | 3512 | repo.rollback() |
|
3513 | 3513 | raise util.Abort(_('patch is damaged or loses information')) |
|
3514 | 3514 | |
|
3515 | 3515 | def tryone(ui, hunk, parents): |
|
3516 | 3516 | tmpname, message, user, date, branch, nodeid, p1, p2 = \ |
|
3517 | 3517 | patch.extract(ui, hunk) |
|
3518 | 3518 | |
|
3519 | 3519 | if not tmpname: |
|
3520 | 3520 | return (None, None) |
|
3521 | 3521 | msg = _('applied to working directory') |
|
3522 | 3522 | |
|
3523 | 3523 | try: |
|
3524 | 3524 | cmdline_message = cmdutil.logmessage(ui, opts) |
|
3525 | 3525 | if cmdline_message: |
|
3526 | 3526 | # pickup the cmdline msg |
|
3527 | 3527 | message = cmdline_message |
|
3528 | 3528 | elif message: |
|
3529 | 3529 | # pickup the patch msg |
|
3530 | 3530 | message = message.strip() |
|
3531 | 3531 | else: |
|
3532 | 3532 | # launch the editor |
|
3533 | 3533 | message = None |
|
3534 | 3534 | ui.debug('message:\n%s\n' % message) |
|
3535 | 3535 | |
|
3536 | 3536 | if len(parents) == 1: |
|
3537 | 3537 | parents.append(repo[nullid]) |
|
3538 | 3538 | if opts.get('exact'): |
|
3539 | 3539 | if not nodeid or not p1: |
|
3540 | 3540 | raise util.Abort(_('not a Mercurial patch')) |
|
3541 | 3541 | p1 = repo[p1] |
|
3542 | 3542 | p2 = repo[p2 or nullid] |
|
3543 | 3543 | elif p2: |
|
3544 | 3544 | try: |
|
3545 | 3545 | p1 = repo[p1] |
|
3546 | 3546 | p2 = repo[p2] |
|
3547 | 3547 | # Without any options, consider p2 only if the |
|
3548 | 3548 | # patch is being applied on top of the recorded |
|
3549 | 3549 | # first parent. |
|
3550 | 3550 | if p1 != parents[0]: |
|
3551 | 3551 | p1 = parents[0] |
|
3552 | 3552 | p2 = repo[nullid] |
|
3553 | 3553 | except error.RepoError: |
|
3554 | 3554 | p1, p2 = parents |
|
3555 | 3555 | else: |
|
3556 | 3556 | p1, p2 = parents |
|
3557 | 3557 | |
|
3558 | 3558 | n = None |
|
3559 | 3559 | if update: |
|
3560 | 3560 | if p1 != parents[0]: |
|
3561 | 3561 | hg.clean(repo, p1.node()) |
|
3562 | 3562 | if p2 != parents[1]: |
|
3563 | 3563 | repo.dirstate.setparents(p1.node(), p2.node()) |
|
3564 | 3564 | |
|
3565 | 3565 | if opts.get('exact') or opts.get('import_branch'): |
|
3566 | 3566 | repo.dirstate.setbranch(branch or 'default') |
|
3567 | 3567 | |
|
3568 | 3568 | files = set() |
|
3569 | 3569 | patch.patch(ui, repo, tmpname, strip=strip, files=files, |
|
3570 | 3570 | eolmode=None, similarity=sim / 100.0) |
|
3571 | 3571 | files = list(files) |
|
3572 | 3572 | if opts.get('no_commit'): |
|
3573 | 3573 | if message: |
|
3574 | 3574 | msgs.append(message) |
|
3575 | 3575 | else: |
|
3576 | 3576 | if opts.get('exact') or p2: |
|
3577 | 3577 | # If you got here, you either use --force and know what |
|
3578 | 3578 | # you are doing or used --exact or a merge patch while |
|
3579 | 3579 | # being updated to its first parent. |
|
3580 | 3580 | m = None |
|
3581 | 3581 | else: |
|
3582 | 3582 | m = scmutil.matchfiles(repo, files or []) |
|
3583 | 3583 | n = repo.commit(message, opts.get('user') or user, |
|
3584 | 3584 | opts.get('date') or date, match=m, |
|
3585 | 3585 | editor=editor) |
|
3586 | 3586 | checkexact(repo, n, nodeid) |
|
3587 | 3587 | else: |
|
3588 | 3588 | if opts.get('exact') or opts.get('import_branch'): |
|
3589 | 3589 | branch = branch or 'default' |
|
3590 | 3590 | else: |
|
3591 | 3591 | branch = p1.branch() |
|
3592 | 3592 | store = patch.filestore() |
|
3593 | 3593 | try: |
|
3594 | 3594 | files = set() |
|
3595 | 3595 | try: |
|
3596 | 3596 | patch.patchrepo(ui, repo, p1, store, tmpname, strip, |
|
3597 | 3597 | files, eolmode=None) |
|
3598 | 3598 | except patch.PatchError, e: |
|
3599 | 3599 | raise util.Abort(str(e)) |
|
3600 | 3600 | memctx = patch.makememctx(repo, (p1.node(), p2.node()), |
|
3601 | 3601 | message, |
|
3602 | 3602 | opts.get('user') or user, |
|
3603 | 3603 | opts.get('date') or date, |
|
3604 | 3604 | branch, files, store, |
|
3605 | 3605 | editor=cmdutil.commiteditor) |
|
3606 | 3606 | repo.savecommitmessage(memctx.description()) |
|
3607 | 3607 | n = memctx.commit() |
|
3608 | 3608 | checkexact(repo, n, nodeid) |
|
3609 | 3609 | finally: |
|
3610 | 3610 | store.close() |
|
3611 | 3611 | if n: |
|
3612 | 3612 | # i18n: refers to a short changeset id |
|
3613 | 3613 | msg = _('created %s') % short(n) |
|
3614 | 3614 | return (msg, n) |
|
3615 | 3615 | finally: |
|
3616 | 3616 | os.unlink(tmpname) |
|
3617 | 3617 | |
|
3618 | 3618 | try: |
|
3619 | 3619 | try: |
|
3620 | 3620 | wlock = repo.wlock() |
|
3621 | 3621 | lock = repo.lock() |
|
3622 | 3622 | tr = repo.transaction('import') |
|
3623 | 3623 | parents = repo.parents() |
|
3624 | 3624 | for patchurl in patches: |
|
3625 | 3625 | if patchurl == '-': |
|
3626 | 3626 | ui.status(_('applying patch from stdin\n')) |
|
3627 | 3627 | patchfile = ui.fin |
|
3628 | 3628 | patchurl = 'stdin' # for error message |
|
3629 | 3629 | else: |
|
3630 | 3630 | patchurl = os.path.join(base, patchurl) |
|
3631 | 3631 | ui.status(_('applying %s\n') % patchurl) |
|
3632 | 3632 | patchfile = url.open(ui, patchurl) |
|
3633 | 3633 | |
|
3634 | 3634 | haspatch = False |
|
3635 | 3635 | for hunk in patch.split(patchfile): |
|
3636 | 3636 | (msg, node) = tryone(ui, hunk, parents) |
|
3637 | 3637 | if msg: |
|
3638 | 3638 | haspatch = True |
|
3639 | 3639 | ui.note(msg + '\n') |
|
3640 | 3640 | if update or opts.get('exact'): |
|
3641 | 3641 | parents = repo.parents() |
|
3642 | 3642 | else: |
|
3643 | 3643 | parents = [repo[node]] |
|
3644 | 3644 | |
|
3645 | 3645 | if not haspatch: |
|
3646 | 3646 | raise util.Abort(_('%s: no diffs found') % patchurl) |
|
3647 | 3647 | |
|
3648 | 3648 | tr.close() |
|
3649 | 3649 | if msgs: |
|
3650 | 3650 | repo.savecommitmessage('\n* * *\n'.join(msgs)) |
|
3651 | 3651 | except: |
|
3652 | 3652 | # wlock.release() indirectly calls dirstate.write(): since |
|
3653 | 3653 | # we're crashing, we do not want to change the working dir |
|
3654 | 3654 | # parent after all, so make sure it writes nothing |
|
3655 | 3655 | repo.dirstate.invalidate() |
|
3656 | 3656 | raise |
|
3657 | 3657 | finally: |
|
3658 | 3658 | if tr: |
|
3659 | 3659 | tr.release() |
|
3660 | 3660 | release(lock, wlock) |
|
3661 | 3661 | |
|
3662 | 3662 | @command('incoming|in', |
|
3663 | 3663 | [('f', 'force', None, |
|
3664 | 3664 | _('run even if remote repository is unrelated')), |
|
3665 | 3665 | ('n', 'newest-first', None, _('show newest record first')), |
|
3666 | 3666 | ('', 'bundle', '', |
|
3667 | 3667 | _('file to store the bundles into'), _('FILE')), |
|
3668 | 3668 | ('r', 'rev', [], _('a remote changeset intended to be added'), _('REV')), |
|
3669 | 3669 | ('B', 'bookmarks', False, _("compare bookmarks")), |
|
3670 | 3670 | ('b', 'branch', [], |
|
3671 | 3671 | _('a specific branch you would like to pull'), _('BRANCH')), |
|
3672 | 3672 | ] + logopts + remoteopts + subrepoopts, |
|
3673 | 3673 | _('[-p] [-n] [-M] [-f] [-r REV]... [--bundle FILENAME] [SOURCE]')) |
|
3674 | 3674 | def incoming(ui, repo, source="default", **opts): |
|
3675 | 3675 | """show new changesets found in source |
|
3676 | 3676 | |
|
3677 | 3677 | Show new changesets found in the specified path/URL or the default |
|
3678 | 3678 | pull location. These are the changesets that would have been pulled |
|
3679 | 3679 | if a pull at the time you issued this command. |
|
3680 | 3680 | |
|
3681 | 3681 | For remote repository, using --bundle avoids downloading the |
|
3682 | 3682 | changesets twice if the incoming is followed by a pull. |
|
3683 | 3683 | |
|
3684 | 3684 | See pull for valid source format details. |
|
3685 | 3685 | |
|
3686 | 3686 | Returns 0 if there are incoming changes, 1 otherwise. |
|
3687 | 3687 | """ |
|
3688 | 3688 | if opts.get('bundle') and opts.get('subrepos'): |
|
3689 | 3689 | raise util.Abort(_('cannot combine --bundle and --subrepos')) |
|
3690 | 3690 | |
|
3691 | 3691 | if opts.get('bookmarks'): |
|
3692 | 3692 | source, branches = hg.parseurl(ui.expandpath(source), |
|
3693 | 3693 | opts.get('branch')) |
|
3694 | 3694 | other = hg.peer(repo, opts, source) |
|
3695 | 3695 | if 'bookmarks' not in other.listkeys('namespaces'): |
|
3696 | 3696 | ui.warn(_("remote doesn't support bookmarks\n")) |
|
3697 | 3697 | return 0 |
|
3698 | 3698 | ui.status(_('comparing with %s\n') % util.hidepassword(source)) |
|
3699 | 3699 | return bookmarks.diff(ui, repo, other) |
|
3700 | 3700 | |
|
3701 | 3701 | repo._subtoppath = ui.expandpath(source) |
|
3702 | 3702 | try: |
|
3703 | 3703 | return hg.incoming(ui, repo, source, opts) |
|
3704 | 3704 | finally: |
|
3705 | 3705 | del repo._subtoppath |
|
3706 | 3706 | |
|
3707 | 3707 | |
|
3708 | 3708 | @command('^init', remoteopts, _('[-e CMD] [--remotecmd CMD] [DEST]')) |
|
3709 | 3709 | def init(ui, dest=".", **opts): |
|
3710 | 3710 | """create a new repository in the given directory |
|
3711 | 3711 | |
|
3712 | 3712 | Initialize a new repository in the given directory. If the given |
|
3713 | 3713 | directory does not exist, it will be created. |
|
3714 | 3714 | |
|
3715 | 3715 | If no directory is given, the current directory is used. |
|
3716 | 3716 | |
|
3717 | 3717 | It is possible to specify an ``ssh://`` URL as the destination. |
|
3718 | 3718 | See :hg:`help urls` for more information. |
|
3719 | 3719 | |
|
3720 | 3720 | Returns 0 on success. |
|
3721 | 3721 | """ |
|
3722 | 3722 | hg.peer(ui, opts, ui.expandpath(dest), create=True) |
|
3723 | 3723 | |
|
3724 | 3724 | @command('locate', |
|
3725 | 3725 | [('r', 'rev', '', _('search the repository as it is in REV'), _('REV')), |
|
3726 | 3726 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs')), |
|
3727 | 3727 | ('f', 'fullpath', None, _('print complete paths from the filesystem root')), |
|
3728 | 3728 | ] + walkopts, |
|
3729 | 3729 | _('[OPTION]... [PATTERN]...')) |
|
3730 | 3730 | def locate(ui, repo, *pats, **opts): |
|
3731 | 3731 | """locate files matching specific patterns |
|
3732 | 3732 | |
|
3733 | 3733 | Print files under Mercurial control in the working directory whose |
|
3734 | 3734 | names match the given patterns. |
|
3735 | 3735 | |
|
3736 | 3736 | By default, this command searches all directories in the working |
|
3737 | 3737 | directory. To search just the current directory and its |
|
3738 | 3738 | subdirectories, use "--include .". |
|
3739 | 3739 | |
|
3740 | 3740 | If no patterns are given to match, this command prints the names |
|
3741 | 3741 | of all files under Mercurial control in the working directory. |
|
3742 | 3742 | |
|
3743 | 3743 | If you want to feed the output of this command into the "xargs" |
|
3744 | 3744 | command, use the -0 option to both this command and "xargs". This |
|
3745 | 3745 | will avoid the problem of "xargs" treating single filenames that |
|
3746 | 3746 | contain whitespace as multiple filenames. |
|
3747 | 3747 | |
|
3748 | 3748 | Returns 0 if a match is found, 1 otherwise. |
|
3749 | 3749 | """ |
|
3750 | 3750 | end = opts.get('print0') and '\0' or '\n' |
|
3751 | 3751 | rev = scmutil.revsingle(repo, opts.get('rev'), None).node() |
|
3752 | 3752 | |
|
3753 | 3753 | ret = 1 |
|
3754 | 3754 | m = scmutil.match(repo[rev], pats, opts, default='relglob') |
|
3755 | 3755 | m.bad = lambda x, y: False |
|
3756 | 3756 | for abs in repo[rev].walk(m): |
|
3757 | 3757 | if not rev and abs not in repo.dirstate: |
|
3758 | 3758 | continue |
|
3759 | 3759 | if opts.get('fullpath'): |
|
3760 | 3760 | ui.write(repo.wjoin(abs), end) |
|
3761 | 3761 | else: |
|
3762 | 3762 | ui.write(((pats and m.rel(abs)) or abs), end) |
|
3763 | 3763 | ret = 0 |
|
3764 | 3764 | |
|
3765 | 3765 | return ret |
|
3766 | 3766 | |
|
3767 | 3767 | @command('^log|history', |
|
3768 | 3768 | [('f', 'follow', None, |
|
3769 | 3769 | _('follow changeset history, or file history across copies and renames')), |
|
3770 | 3770 | ('', 'follow-first', None, |
|
3771 | 3771 | _('only follow the first parent of merge changesets (DEPRECATED)')), |
|
3772 | 3772 | ('d', 'date', '', _('show revisions matching date spec'), _('DATE')), |
|
3773 | 3773 | ('C', 'copies', None, _('show copied files')), |
|
3774 | 3774 | ('k', 'keyword', [], |
|
3775 | 3775 | _('do case-insensitive search for a given text'), _('TEXT')), |
|
3776 | 3776 | ('r', 'rev', [], _('show the specified revision or range'), _('REV')), |
|
3777 | 3777 | ('', 'removed', None, _('include revisions where files were removed')), |
|
3778 | 3778 | ('m', 'only-merges', None, _('show only merges (DEPRECATED)')), |
|
3779 | 3779 | ('u', 'user', [], _('revisions committed by user'), _('USER')), |
|
3780 | 3780 | ('', 'only-branch', [], |
|
3781 | 3781 | _('show only changesets within the given named branch (DEPRECATED)'), |
|
3782 | 3782 | _('BRANCH')), |
|
3783 | 3783 | ('b', 'branch', [], |
|
3784 | 3784 | _('show changesets within the given named branch'), _('BRANCH')), |
|
3785 | 3785 | ('P', 'prune', [], |
|
3786 | 3786 | _('do not display revision or any of its ancestors'), _('REV')), |
|
3787 | 3787 | ('', 'hidden', False, _('show hidden changesets (DEPRECATED)')), |
|
3788 | 3788 | ] + logopts + walkopts, |
|
3789 | 3789 | _('[OPTION]... [FILE]')) |
|
3790 | 3790 | def log(ui, repo, *pats, **opts): |
|
3791 | 3791 | """show revision history of entire repository or files |
|
3792 | 3792 | |
|
3793 | 3793 | Print the revision history of the specified files or the entire |
|
3794 | 3794 | project. |
|
3795 | 3795 | |
|
3796 | 3796 | If no revision range is specified, the default is ``tip:0`` unless |
|
3797 | 3797 | --follow is set, in which case the working directory parent is |
|
3798 | 3798 | used as the starting revision. |
|
3799 | 3799 | |
|
3800 | 3800 | File history is shown without following rename or copy history of |
|
3801 | 3801 | files. Use -f/--follow with a filename to follow history across |
|
3802 | 3802 | renames and copies. --follow without a filename will only show |
|
3803 | 3803 | ancestors or descendants of the starting revision. |
|
3804 | 3804 | |
|
3805 | 3805 | By default this command prints revision number and changeset id, |
|
3806 | 3806 | tags, non-trivial parents, user, date and time, and a summary for |
|
3807 | 3807 | each commit. When the -v/--verbose switch is used, the list of |
|
3808 | 3808 | changed files and full commit message are shown. |
|
3809 | 3809 | |
|
3810 | 3810 | .. note:: |
|
3811 | 3811 | log -p/--patch may generate unexpected diff output for merge |
|
3812 | 3812 | changesets, as it will only compare the merge changeset against |
|
3813 | 3813 | its first parent. Also, only files different from BOTH parents |
|
3814 | 3814 | will appear in files:. |
|
3815 | 3815 | |
|
3816 | 3816 | .. note:: |
|
3817 | 3817 | for performance reasons, log FILE may omit duplicate changes |
|
3818 | 3818 | made on branches and will not show deletions. To see all |
|
3819 | 3819 | changes including duplicates and deletions, use the --removed |
|
3820 | 3820 | switch. |
|
3821 | 3821 | |
|
3822 | 3822 | .. container:: verbose |
|
3823 | 3823 | |
|
3824 | 3824 | Some examples: |
|
3825 | 3825 | |
|
3826 | 3826 | - changesets with full descriptions and file lists:: |
|
3827 | 3827 | |
|
3828 | 3828 | hg log -v |
|
3829 | 3829 | |
|
3830 | 3830 | - changesets ancestral to the working directory:: |
|
3831 | 3831 | |
|
3832 | 3832 | hg log -f |
|
3833 | 3833 | |
|
3834 | 3834 | - last 10 commits on the current branch:: |
|
3835 | 3835 | |
|
3836 | 3836 | hg log -l 10 -b . |
|
3837 | 3837 | |
|
3838 | 3838 | - changesets showing all modifications of a file, including removals:: |
|
3839 | 3839 | |
|
3840 | 3840 | hg log --removed file.c |
|
3841 | 3841 | |
|
3842 | 3842 | - all changesets that touch a directory, with diffs, excluding merges:: |
|
3843 | 3843 | |
|
3844 | 3844 | hg log -Mp lib/ |
|
3845 | 3845 | |
|
3846 | 3846 | - all revision numbers that match a keyword:: |
|
3847 | 3847 | |
|
3848 | 3848 | hg log -k bug --template "{rev}\\n" |
|
3849 | 3849 | |
|
3850 | 3850 | - check if a given changeset is included is a tagged release:: |
|
3851 | 3851 | |
|
3852 | 3852 | hg log -r "a21ccf and ancestor(1.9)" |
|
3853 | 3853 | |
|
3854 | 3854 | - find all changesets by some user in a date range:: |
|
3855 | 3855 | |
|
3856 | 3856 | hg log -k alice -d "may 2008 to jul 2008" |
|
3857 | 3857 | |
|
3858 | 3858 | - summary of all changesets after the last tag:: |
|
3859 | 3859 | |
|
3860 | 3860 | hg log -r "last(tagged())::" --template "{desc|firstline}\\n" |
|
3861 | 3861 | |
|
3862 | 3862 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
3863 | 3863 | |
|
3864 | 3864 | See :hg:`help revisions` and :hg:`help revsets` for more about |
|
3865 | 3865 | specifying revisions. |
|
3866 | 3866 | |
|
3867 | 3867 | Returns 0 on success. |
|
3868 | 3868 | """ |
|
3869 | 3869 | |
|
3870 | 3870 | matchfn = scmutil.match(repo[None], pats, opts) |
|
3871 | 3871 | limit = cmdutil.loglimit(opts) |
|
3872 | 3872 | count = 0 |
|
3873 | 3873 | |
|
3874 | 3874 | endrev = None |
|
3875 | 3875 | if opts.get('copies') and opts.get('rev'): |
|
3876 | 3876 | endrev = max(scmutil.revrange(repo, opts.get('rev'))) + 1 |
|
3877 | 3877 | |
|
3878 | 3878 | df = False |
|
3879 | 3879 | if opts["date"]: |
|
3880 | 3880 | df = util.matchdate(opts["date"]) |
|
3881 | 3881 | |
|
3882 | 3882 | branches = opts.get('branch', []) + opts.get('only_branch', []) |
|
3883 | 3883 | opts['branch'] = [repo.lookupbranch(b) for b in branches] |
|
3884 | 3884 | |
|
3885 | 3885 | displayer = cmdutil.show_changeset(ui, repo, opts, True) |
|
3886 | 3886 | def prep(ctx, fns): |
|
3887 | 3887 | rev = ctx.rev() |
|
3888 | 3888 | parents = [p for p in repo.changelog.parentrevs(rev) |
|
3889 | 3889 | if p != nullrev] |
|
3890 | 3890 | if opts.get('no_merges') and len(parents) == 2: |
|
3891 | 3891 | return |
|
3892 | 3892 | if opts.get('only_merges') and len(parents) != 2: |
|
3893 | 3893 | return |
|
3894 | 3894 | if opts.get('branch') and ctx.branch() not in opts['branch']: |
|
3895 | 3895 | return |
|
3896 | 3896 | if not opts.get('hidden') and ctx.hidden(): |
|
3897 | 3897 | return |
|
3898 | 3898 | if df and not df(ctx.date()[0]): |
|
3899 | 3899 | return |
|
3900 | 3900 | |
|
3901 | 3901 | lower = encoding.lower |
|
3902 | 3902 | if opts.get('user'): |
|
3903 | 3903 | luser = lower(ctx.user()) |
|
3904 | 3904 | for k in [lower(x) for x in opts['user']]: |
|
3905 | 3905 | if (k in luser): |
|
3906 | 3906 | break |
|
3907 | 3907 | else: |
|
3908 | 3908 | return |
|
3909 | 3909 | if opts.get('keyword'): |
|
3910 | 3910 | luser = lower(ctx.user()) |
|
3911 | 3911 | ldesc = lower(ctx.description()) |
|
3912 | 3912 | lfiles = lower(" ".join(ctx.files())) |
|
3913 | 3913 | for k in [lower(x) for x in opts['keyword']]: |
|
3914 | 3914 | if (k in luser or k in ldesc or k in lfiles): |
|
3915 | 3915 | break |
|
3916 | 3916 | else: |
|
3917 | 3917 | return |
|
3918 | 3918 | |
|
3919 | 3919 | copies = None |
|
3920 | 3920 | if opts.get('copies') and rev: |
|
3921 | 3921 | copies = [] |
|
3922 | 3922 | getrenamed = templatekw.getrenamedfn(repo, endrev=endrev) |
|
3923 | 3923 | for fn in ctx.files(): |
|
3924 | 3924 | rename = getrenamed(fn, rev) |
|
3925 | 3925 | if rename: |
|
3926 | 3926 | copies.append((fn, rename[0])) |
|
3927 | 3927 | |
|
3928 | 3928 | revmatchfn = None |
|
3929 | 3929 | if opts.get('patch') or opts.get('stat'): |
|
3930 | 3930 | if opts.get('follow') or opts.get('follow_first'): |
|
3931 | 3931 | # note: this might be wrong when following through merges |
|
3932 | 3932 | revmatchfn = scmutil.match(repo[None], fns, default='path') |
|
3933 | 3933 | else: |
|
3934 | 3934 | revmatchfn = matchfn |
|
3935 | 3935 | |
|
3936 | 3936 | displayer.show(ctx, copies=copies, matchfn=revmatchfn) |
|
3937 | 3937 | |
|
3938 | 3938 | for ctx in cmdutil.walkchangerevs(repo, matchfn, opts, prep): |
|
3939 | 3939 | if count == limit: |
|
3940 | 3940 | break |
|
3941 | 3941 | if displayer.flush(ctx.rev()): |
|
3942 | 3942 | count += 1 |
|
3943 | 3943 | displayer.close() |
|
3944 | 3944 | |
|
3945 | 3945 | @command('manifest', |
|
3946 | 3946 | [('r', 'rev', '', _('revision to display'), _('REV')), |
|
3947 | 3947 | ('', 'all', False, _("list files from all revisions"))], |
|
3948 | 3948 | _('[-r REV]')) |
|
3949 | 3949 | def manifest(ui, repo, node=None, rev=None, **opts): |
|
3950 | 3950 | """output the current or given revision of the project manifest |
|
3951 | 3951 | |
|
3952 | 3952 | Print a list of version controlled files for the given revision. |
|
3953 | 3953 | If no revision is given, the first parent of the working directory |
|
3954 | 3954 | is used, or the null revision if no revision is checked out. |
|
3955 | 3955 | |
|
3956 | 3956 | With -v, print file permissions, symlink and executable bits. |
|
3957 | 3957 | With --debug, print file revision hashes. |
|
3958 | 3958 | |
|
3959 | 3959 | If option --all is specified, the list of all files from all revisions |
|
3960 | 3960 | is printed. This includes deleted and renamed files. |
|
3961 | 3961 | |
|
3962 | 3962 | Returns 0 on success. |
|
3963 | 3963 | """ |
|
3964 | 3964 | if opts.get('all'): |
|
3965 | 3965 | if rev or node: |
|
3966 | 3966 | raise util.Abort(_("can't specify a revision with --all")) |
|
3967 | 3967 | |
|
3968 | 3968 | res = [] |
|
3969 | 3969 | prefix = "data/" |
|
3970 | 3970 | suffix = ".i" |
|
3971 | 3971 | plen = len(prefix) |
|
3972 | 3972 | slen = len(suffix) |
|
3973 | 3973 | lock = repo.lock() |
|
3974 | 3974 | try: |
|
3975 | 3975 | for fn, b, size in repo.store.datafiles(): |
|
3976 | 3976 | if size != 0 and fn[-slen:] == suffix and fn[:plen] == prefix: |
|
3977 | 3977 | res.append(fn[plen:-slen]) |
|
3978 | 3978 | finally: |
|
3979 | 3979 | lock.release() |
|
3980 | 3980 | for f in sorted(res): |
|
3981 | 3981 | ui.write("%s\n" % f) |
|
3982 | 3982 | return |
|
3983 | 3983 | |
|
3984 | 3984 | if rev and node: |
|
3985 | 3985 | raise util.Abort(_("please specify just one revision")) |
|
3986 | 3986 | |
|
3987 | 3987 | if not node: |
|
3988 | 3988 | node = rev |
|
3989 | 3989 | |
|
3990 | 3990 | decor = {'l':'644 @ ', 'x':'755 * ', '':'644 '} |
|
3991 | 3991 | ctx = scmutil.revsingle(repo, node) |
|
3992 | 3992 | for f in ctx: |
|
3993 | 3993 | if ui.debugflag: |
|
3994 | 3994 | ui.write("%40s " % hex(ctx.manifest()[f])) |
|
3995 | 3995 | if ui.verbose: |
|
3996 | 3996 | ui.write(decor[ctx.flags(f)]) |
|
3997 | 3997 | ui.write("%s\n" % f) |
|
3998 | 3998 | |
|
3999 | 3999 | @command('^merge', |
|
4000 | 4000 | [('f', 'force', None, _('force a merge with outstanding changes')), |
|
4001 | 4001 | ('r', 'rev', '', _('revision to merge'), _('REV')), |
|
4002 | 4002 | ('P', 'preview', None, |
|
4003 | 4003 | _('review revisions to merge (no merge is performed)')) |
|
4004 | 4004 | ] + mergetoolopts, |
|
4005 | 4005 | _('[-P] [-f] [[-r] REV]')) |
|
4006 | 4006 | def merge(ui, repo, node=None, **opts): |
|
4007 | 4007 | """merge working directory with another revision |
|
4008 | 4008 | |
|
4009 | 4009 | The current working directory is updated with all changes made in |
|
4010 | 4010 | the requested revision since the last common predecessor revision. |
|
4011 | 4011 | |
|
4012 | 4012 | Files that changed between either parent are marked as changed for |
|
4013 | 4013 | the next commit and a commit must be performed before any further |
|
4014 | 4014 | updates to the repository are allowed. The next commit will have |
|
4015 | 4015 | two parents. |
|
4016 | 4016 | |
|
4017 | 4017 | ``--tool`` can be used to specify the merge tool used for file |
|
4018 | 4018 | merges. It overrides the HGMERGE environment variable and your |
|
4019 | 4019 | configuration files. See :hg:`help merge-tools` for options. |
|
4020 | 4020 | |
|
4021 | 4021 | If no revision is specified, the working directory's parent is a |
|
4022 | 4022 | head revision, and the current branch contains exactly one other |
|
4023 | 4023 | head, the other head is merged with by default. Otherwise, an |
|
4024 | 4024 | explicit revision with which to merge with must be provided. |
|
4025 | 4025 | |
|
4026 | 4026 | :hg:`resolve` must be used to resolve unresolved files. |
|
4027 | 4027 | |
|
4028 | 4028 | To undo an uncommitted merge, use :hg:`update --clean .` which |
|
4029 | 4029 | will check out a clean copy of the original merge parent, losing |
|
4030 | 4030 | all changes. |
|
4031 | 4031 | |
|
4032 | 4032 | Returns 0 on success, 1 if there are unresolved files. |
|
4033 | 4033 | """ |
|
4034 | 4034 | |
|
4035 | 4035 | if opts.get('rev') and node: |
|
4036 | 4036 | raise util.Abort(_("please specify just one revision")) |
|
4037 | 4037 | if not node: |
|
4038 | 4038 | node = opts.get('rev') |
|
4039 | 4039 | |
|
4040 | 4040 | if not node: |
|
4041 | 4041 | branch = repo[None].branch() |
|
4042 | 4042 | bheads = repo.branchheads(branch) |
|
4043 | 4043 | if len(bheads) > 2: |
|
4044 | 4044 | raise util.Abort(_("branch '%s' has %d heads - " |
|
4045 | 4045 | "please merge with an explicit rev") |
|
4046 | 4046 | % (branch, len(bheads)), |
|
4047 | 4047 | hint=_("run 'hg heads .' to see heads")) |
|
4048 | 4048 | |
|
4049 | 4049 | parent = repo.dirstate.p1() |
|
4050 | 4050 | if len(bheads) == 1: |
|
4051 | 4051 | if len(repo.heads()) > 1: |
|
4052 | 4052 | raise util.Abort(_("branch '%s' has one head - " |
|
4053 | 4053 | "please merge with an explicit rev") |
|
4054 | 4054 | % branch, |
|
4055 | 4055 | hint=_("run 'hg heads' to see all heads")) |
|
4056 | 4056 | msg, hint = _('nothing to merge'), None |
|
4057 | 4057 | if parent != repo.lookup(branch): |
|
4058 | 4058 | hint = _("use 'hg update' instead") |
|
4059 | 4059 | raise util.Abort(msg, hint=hint) |
|
4060 | 4060 | |
|
4061 | 4061 | if parent not in bheads: |
|
4062 | 4062 | raise util.Abort(_('working directory not at a head revision'), |
|
4063 | 4063 | hint=_("use 'hg update' or merge with an " |
|
4064 | 4064 | "explicit revision")) |
|
4065 | 4065 | node = parent == bheads[0] and bheads[-1] or bheads[0] |
|
4066 | 4066 | else: |
|
4067 | 4067 | node = scmutil.revsingle(repo, node).node() |
|
4068 | 4068 | |
|
4069 | 4069 | if opts.get('preview'): |
|
4070 | 4070 | # find nodes that are ancestors of p2 but not of p1 |
|
4071 | 4071 | p1 = repo.lookup('.') |
|
4072 | 4072 | p2 = repo.lookup(node) |
|
4073 | 4073 | nodes = repo.changelog.findmissing(common=[p1], heads=[p2]) |
|
4074 | 4074 | |
|
4075 | 4075 | displayer = cmdutil.show_changeset(ui, repo, opts) |
|
4076 | 4076 | for node in nodes: |
|
4077 | 4077 | displayer.show(repo[node]) |
|
4078 | 4078 | displayer.close() |
|
4079 | 4079 | return 0 |
|
4080 | 4080 | |
|
4081 | 4081 | try: |
|
4082 | 4082 | # ui.forcemerge is an internal variable, do not document |
|
4083 | 4083 | repo.ui.setconfig('ui', 'forcemerge', opts.get('tool', '')) |
|
4084 | 4084 | return hg.merge(repo, node, force=opts.get('force')) |
|
4085 | 4085 | finally: |
|
4086 | 4086 | ui.setconfig('ui', 'forcemerge', '') |
|
4087 | 4087 | |
|
4088 | 4088 | @command('outgoing|out', |
|
4089 | 4089 | [('f', 'force', None, _('run even when the destination is unrelated')), |
|
4090 | 4090 | ('r', 'rev', [], |
|
4091 | 4091 | _('a changeset intended to be included in the destination'), _('REV')), |
|
4092 | 4092 | ('n', 'newest-first', None, _('show newest record first')), |
|
4093 | 4093 | ('B', 'bookmarks', False, _('compare bookmarks')), |
|
4094 | 4094 | ('b', 'branch', [], _('a specific branch you would like to push'), |
|
4095 | 4095 | _('BRANCH')), |
|
4096 | 4096 | ] + logopts + remoteopts + subrepoopts, |
|
4097 | 4097 | _('[-M] [-p] [-n] [-f] [-r REV]... [DEST]')) |
|
4098 | 4098 | def outgoing(ui, repo, dest=None, **opts): |
|
4099 | 4099 | """show changesets not found in the destination |
|
4100 | 4100 | |
|
4101 | 4101 | Show changesets not found in the specified destination repository |
|
4102 | 4102 | or the default push location. These are the changesets that would |
|
4103 | 4103 | be pushed if a push was requested. |
|
4104 | 4104 | |
|
4105 | 4105 | See pull for details of valid destination formats. |
|
4106 | 4106 | |
|
4107 | 4107 | Returns 0 if there are outgoing changes, 1 otherwise. |
|
4108 | 4108 | """ |
|
4109 | 4109 | |
|
4110 | 4110 | if opts.get('bookmarks'): |
|
4111 | 4111 | dest = ui.expandpath(dest or 'default-push', dest or 'default') |
|
4112 | 4112 | dest, branches = hg.parseurl(dest, opts.get('branch')) |
|
4113 | 4113 | other = hg.peer(repo, opts, dest) |
|
4114 | 4114 | if 'bookmarks' not in other.listkeys('namespaces'): |
|
4115 | 4115 | ui.warn(_("remote doesn't support bookmarks\n")) |
|
4116 | 4116 | return 0 |
|
4117 | 4117 | ui.status(_('comparing with %s\n') % util.hidepassword(dest)) |
|
4118 | 4118 | return bookmarks.diff(ui, other, repo) |
|
4119 | 4119 | |
|
4120 | 4120 | repo._subtoppath = ui.expandpath(dest or 'default-push', dest or 'default') |
|
4121 | 4121 | try: |
|
4122 | 4122 | return hg.outgoing(ui, repo, dest, opts) |
|
4123 | 4123 | finally: |
|
4124 | 4124 | del repo._subtoppath |
|
4125 | 4125 | |
|
4126 | 4126 | @command('parents', |
|
4127 | 4127 | [('r', 'rev', '', _('show parents of the specified revision'), _('REV')), |
|
4128 | 4128 | ] + templateopts, |
|
4129 | 4129 | _('[-r REV] [FILE]')) |
|
4130 | 4130 | def parents(ui, repo, file_=None, **opts): |
|
4131 | 4131 | """show the parents of the working directory or revision |
|
4132 | 4132 | |
|
4133 | 4133 | Print the working directory's parent revisions. If a revision is |
|
4134 | 4134 | given via -r/--rev, the parent of that revision will be printed. |
|
4135 | 4135 | If a file argument is given, the revision in which the file was |
|
4136 | 4136 | last changed (before the working directory revision or the |
|
4137 | 4137 | argument to --rev if given) is printed. |
|
4138 | 4138 | |
|
4139 | 4139 | Returns 0 on success. |
|
4140 | 4140 | """ |
|
4141 | 4141 | |
|
4142 | 4142 | ctx = scmutil.revsingle(repo, opts.get('rev'), None) |
|
4143 | 4143 | |
|
4144 | 4144 | if file_: |
|
4145 | 4145 | m = scmutil.match(ctx, (file_,), opts) |
|
4146 | 4146 | if m.anypats() or len(m.files()) != 1: |
|
4147 | 4147 | raise util.Abort(_('can only specify an explicit filename')) |
|
4148 | 4148 | file_ = m.files()[0] |
|
4149 | 4149 | filenodes = [] |
|
4150 | 4150 | for cp in ctx.parents(): |
|
4151 | 4151 | if not cp: |
|
4152 | 4152 | continue |
|
4153 | 4153 | try: |
|
4154 | 4154 | filenodes.append(cp.filenode(file_)) |
|
4155 | 4155 | except error.LookupError: |
|
4156 | 4156 | pass |
|
4157 | 4157 | if not filenodes: |
|
4158 | 4158 | raise util.Abort(_("'%s' not found in manifest!") % file_) |
|
4159 | 4159 | fl = repo.file(file_) |
|
4160 | 4160 | p = [repo.lookup(fl.linkrev(fl.rev(fn))) for fn in filenodes] |
|
4161 | 4161 | else: |
|
4162 | 4162 | p = [cp.node() for cp in ctx.parents()] |
|
4163 | 4163 | |
|
4164 | 4164 | displayer = cmdutil.show_changeset(ui, repo, opts) |
|
4165 | 4165 | for n in p: |
|
4166 | 4166 | if n != nullid: |
|
4167 | 4167 | displayer.show(repo[n]) |
|
4168 | 4168 | displayer.close() |
|
4169 | 4169 | |
|
4170 | 4170 | @command('paths', [], _('[NAME]')) |
|
4171 | 4171 | def paths(ui, repo, search=None): |
|
4172 | 4172 | """show aliases for remote repositories |
|
4173 | 4173 | |
|
4174 | 4174 | Show definition of symbolic path name NAME. If no name is given, |
|
4175 | 4175 | show definition of all available names. |
|
4176 | 4176 | |
|
4177 | 4177 | Option -q/--quiet suppresses all output when searching for NAME |
|
4178 | 4178 | and shows only the path names when listing all definitions. |
|
4179 | 4179 | |
|
4180 | 4180 | Path names are defined in the [paths] section of your |
|
4181 | 4181 | configuration file and in ``/etc/mercurial/hgrc``. If run inside a |
|
4182 | 4182 | repository, ``.hg/hgrc`` is used, too. |
|
4183 | 4183 | |
|
4184 | 4184 | The path names ``default`` and ``default-push`` have a special |
|
4185 | 4185 | meaning. When performing a push or pull operation, they are used |
|
4186 | 4186 | as fallbacks if no location is specified on the command-line. |
|
4187 | 4187 | When ``default-push`` is set, it will be used for push and |
|
4188 | 4188 | ``default`` will be used for pull; otherwise ``default`` is used |
|
4189 | 4189 | as the fallback for both. When cloning a repository, the clone |
|
4190 | 4190 | source is written as ``default`` in ``.hg/hgrc``. Note that |
|
4191 | 4191 | ``default`` and ``default-push`` apply to all inbound (e.g. |
|
4192 | 4192 | :hg:`incoming`) and outbound (e.g. :hg:`outgoing`, :hg:`email` and |
|
4193 | 4193 | :hg:`bundle`) operations. |
|
4194 | 4194 | |
|
4195 | 4195 | See :hg:`help urls` for more information. |
|
4196 | 4196 | |
|
4197 | 4197 | Returns 0 on success. |
|
4198 | 4198 | """ |
|
4199 | 4199 | if search: |
|
4200 | 4200 | for name, path in ui.configitems("paths"): |
|
4201 | 4201 | if name == search: |
|
4202 | 4202 | ui.status("%s\n" % util.hidepassword(path)) |
|
4203 | 4203 | return |
|
4204 | 4204 | if not ui.quiet: |
|
4205 | 4205 | ui.warn(_("not found!\n")) |
|
4206 | 4206 | return 1 |
|
4207 | 4207 | else: |
|
4208 | 4208 | for name, path in ui.configitems("paths"): |
|
4209 | 4209 | if ui.quiet: |
|
4210 | 4210 | ui.write("%s\n" % name) |
|
4211 | 4211 | else: |
|
4212 | 4212 | ui.write("%s = %s\n" % (name, util.hidepassword(path))) |
|
4213 | 4213 | |
|
4214 | 4214 | def postincoming(ui, repo, modheads, optupdate, checkout): |
|
4215 | 4215 | if modheads == 0: |
|
4216 | 4216 | return |
|
4217 | 4217 | if optupdate: |
|
4218 | 4218 | try: |
|
4219 | 4219 | return hg.update(repo, checkout) |
|
4220 | 4220 | except util.Abort, inst: |
|
4221 | 4221 | ui.warn(_("not updating: %s\n" % str(inst))) |
|
4222 | 4222 | return 0 |
|
4223 | 4223 | if modheads > 1: |
|
4224 | 4224 | currentbranchheads = len(repo.branchheads()) |
|
4225 | 4225 | if currentbranchheads == modheads: |
|
4226 | 4226 | ui.status(_("(run 'hg heads' to see heads, 'hg merge' to merge)\n")) |
|
4227 | 4227 | elif currentbranchheads > 1: |
|
4228 | 4228 | ui.status(_("(run 'hg heads .' to see heads, 'hg merge' to merge)\n")) |
|
4229 | 4229 | else: |
|
4230 | 4230 | ui.status(_("(run 'hg heads' to see heads)\n")) |
|
4231 | 4231 | else: |
|
4232 | 4232 | ui.status(_("(run 'hg update' to get a working copy)\n")) |
|
4233 | 4233 | |
|
4234 | 4234 | @command('^pull', |
|
4235 | 4235 | [('u', 'update', None, |
|
4236 | 4236 | _('update to new branch head if changesets were pulled')), |
|
4237 | 4237 | ('f', 'force', None, _('run even when remote repository is unrelated')), |
|
4238 | 4238 | ('r', 'rev', [], _('a remote changeset intended to be added'), _('REV')), |
|
4239 | 4239 | ('B', 'bookmark', [], _("bookmark to pull"), _('BOOKMARK')), |
|
4240 | 4240 | ('b', 'branch', [], _('a specific branch you would like to pull'), |
|
4241 | 4241 | _('BRANCH')), |
|
4242 | 4242 | ] + remoteopts, |
|
4243 | 4243 | _('[-u] [-f] [-r REV]... [-e CMD] [--remotecmd CMD] [SOURCE]')) |
|
4244 | 4244 | def pull(ui, repo, source="default", **opts): |
|
4245 | 4245 | """pull changes from the specified source |
|
4246 | 4246 | |
|
4247 | 4247 | Pull changes from a remote repository to a local one. |
|
4248 | 4248 | |
|
4249 | 4249 | This finds all changes from the repository at the specified path |
|
4250 | 4250 | or URL and adds them to a local repository (the current one unless |
|
4251 | 4251 | -R is specified). By default, this does not update the copy of the |
|
4252 | 4252 | project in the working directory. |
|
4253 | 4253 | |
|
4254 | 4254 | Use :hg:`incoming` if you want to see what would have been added |
|
4255 | 4255 | by a pull at the time you issued this command. If you then decide |
|
4256 | 4256 | to add those changes to the repository, you should use :hg:`pull |
|
4257 | 4257 | -r X` where ``X`` is the last changeset listed by :hg:`incoming`. |
|
4258 | 4258 | |
|
4259 | 4259 | If SOURCE is omitted, the 'default' path will be used. |
|
4260 | 4260 | See :hg:`help urls` for more information. |
|
4261 | 4261 | |
|
4262 | 4262 | Returns 0 on success, 1 if an update had unresolved files. |
|
4263 | 4263 | """ |
|
4264 | 4264 | source, branches = hg.parseurl(ui.expandpath(source), opts.get('branch')) |
|
4265 | 4265 | other = hg.peer(repo, opts, source) |
|
4266 | 4266 | ui.status(_('pulling from %s\n') % util.hidepassword(source)) |
|
4267 | 4267 | revs, checkout = hg.addbranchrevs(repo, other, branches, opts.get('rev')) |
|
4268 | 4268 | |
|
4269 | 4269 | if opts.get('bookmark'): |
|
4270 | 4270 | if not revs: |
|
4271 | 4271 | revs = [] |
|
4272 | 4272 | rb = other.listkeys('bookmarks') |
|
4273 | 4273 | for b in opts['bookmark']: |
|
4274 | 4274 | if b not in rb: |
|
4275 | 4275 | raise util.Abort(_('remote bookmark %s not found!') % b) |
|
4276 | 4276 | revs.append(rb[b]) |
|
4277 | 4277 | |
|
4278 | 4278 | if revs: |
|
4279 | 4279 | try: |
|
4280 | 4280 | revs = [other.lookup(rev) for rev in revs] |
|
4281 | 4281 | except error.CapabilityError: |
|
4282 | 4282 | err = _("other repository doesn't support revision lookup, " |
|
4283 | 4283 | "so a rev cannot be specified.") |
|
4284 | 4284 | raise util.Abort(err) |
|
4285 | 4285 | |
|
4286 | 4286 | modheads = repo.pull(other, heads=revs, force=opts.get('force')) |
|
4287 | 4287 | bookmarks.updatefromremote(ui, repo, other, source) |
|
4288 | 4288 | if checkout: |
|
4289 | 4289 | checkout = str(repo.changelog.rev(other.lookup(checkout))) |
|
4290 | 4290 | repo._subtoppath = source |
|
4291 | 4291 | try: |
|
4292 | 4292 | ret = postincoming(ui, repo, modheads, opts.get('update'), checkout) |
|
4293 | 4293 | |
|
4294 | 4294 | finally: |
|
4295 | 4295 | del repo._subtoppath |
|
4296 | 4296 | |
|
4297 | 4297 | # update specified bookmarks |
|
4298 | 4298 | if opts.get('bookmark'): |
|
4299 | 4299 | for b in opts['bookmark']: |
|
4300 | 4300 | # explicit pull overrides local bookmark if any |
|
4301 | 4301 | ui.status(_("importing bookmark %s\n") % b) |
|
4302 | 4302 | repo._bookmarks[b] = repo[rb[b]].node() |
|
4303 | 4303 | bookmarks.write(repo) |
|
4304 | 4304 | |
|
4305 | 4305 | return ret |
|
4306 | 4306 | |
|
4307 | 4307 | @command('^push', |
|
4308 | 4308 | [('f', 'force', None, _('force push')), |
|
4309 | 4309 | ('r', 'rev', [], |
|
4310 | 4310 | _('a changeset intended to be included in the destination'), |
|
4311 | 4311 | _('REV')), |
|
4312 | 4312 | ('B', 'bookmark', [], _("bookmark to push"), _('BOOKMARK')), |
|
4313 | 4313 | ('b', 'branch', [], |
|
4314 | 4314 | _('a specific branch you would like to push'), _('BRANCH')), |
|
4315 | 4315 | ('', 'new-branch', False, _('allow pushing a new branch')), |
|
4316 | 4316 | ] + remoteopts, |
|
4317 | 4317 | _('[-f] [-r REV]... [-e CMD] [--remotecmd CMD] [DEST]')) |
|
4318 | 4318 | def push(ui, repo, dest=None, **opts): |
|
4319 | 4319 | """push changes to the specified destination |
|
4320 | 4320 | |
|
4321 | 4321 | Push changesets from the local repository to the specified |
|
4322 | 4322 | destination. |
|
4323 | 4323 | |
|
4324 | 4324 | This operation is symmetrical to pull: it is identical to a pull |
|
4325 | 4325 | in the destination repository from the current one. |
|
4326 | 4326 | |
|
4327 | 4327 | By default, push will not allow creation of new heads at the |
|
4328 | 4328 | destination, since multiple heads would make it unclear which head |
|
4329 | 4329 | to use. In this situation, it is recommended to pull and merge |
|
4330 | 4330 | before pushing. |
|
4331 | 4331 | |
|
4332 | 4332 | Use --new-branch if you want to allow push to create a new named |
|
4333 | 4333 | branch that is not present at the destination. This allows you to |
|
4334 | 4334 | only create a new branch without forcing other changes. |
|
4335 | 4335 | |
|
4336 | 4336 | Use -f/--force to override the default behavior and push all |
|
4337 | 4337 | changesets on all branches. |
|
4338 | 4338 | |
|
4339 | 4339 | If -r/--rev is used, the specified revision and all its ancestors |
|
4340 | 4340 | will be pushed to the remote repository. |
|
4341 | 4341 | |
|
4342 | 4342 | Please see :hg:`help urls` for important details about ``ssh://`` |
|
4343 | 4343 | URLs. If DESTINATION is omitted, a default path will be used. |
|
4344 | 4344 | |
|
4345 | 4345 | Returns 0 if push was successful, 1 if nothing to push. |
|
4346 | 4346 | """ |
|
4347 | 4347 | |
|
4348 | 4348 | if opts.get('bookmark'): |
|
4349 | 4349 | for b in opts['bookmark']: |
|
4350 | 4350 | # translate -B options to -r so changesets get pushed |
|
4351 | 4351 | if b in repo._bookmarks: |
|
4352 | 4352 | opts.setdefault('rev', []).append(b) |
|
4353 | 4353 | else: |
|
4354 | 4354 | # if we try to push a deleted bookmark, translate it to null |
|
4355 | 4355 | # this lets simultaneous -r, -b options continue working |
|
4356 | 4356 | opts.setdefault('rev', []).append("null") |
|
4357 | 4357 | |
|
4358 | 4358 | dest = ui.expandpath(dest or 'default-push', dest or 'default') |
|
4359 | 4359 | dest, branches = hg.parseurl(dest, opts.get('branch')) |
|
4360 | 4360 | ui.status(_('pushing to %s\n') % util.hidepassword(dest)) |
|
4361 | 4361 | revs, checkout = hg.addbranchrevs(repo, repo, branches, opts.get('rev')) |
|
4362 | 4362 | other = hg.peer(repo, opts, dest) |
|
4363 | 4363 | if revs: |
|
4364 | 4364 | revs = [repo.lookup(rev) for rev in revs] |
|
4365 | 4365 | |
|
4366 | 4366 | repo._subtoppath = dest |
|
4367 | 4367 | try: |
|
4368 | 4368 | # push subrepos depth-first for coherent ordering |
|
4369 | 4369 | c = repo[''] |
|
4370 | 4370 | subs = c.substate # only repos that are committed |
|
4371 | 4371 | for s in sorted(subs): |
|
4372 | 4372 | if not c.sub(s).push(opts): |
|
4373 | 4373 | return False |
|
4374 | 4374 | finally: |
|
4375 | 4375 | del repo._subtoppath |
|
4376 | 4376 | result = repo.push(other, opts.get('force'), revs=revs, |
|
4377 | 4377 | newbranch=opts.get('new_branch')) |
|
4378 | 4378 | |
|
4379 | 4379 | result = (result == 0) |
|
4380 | 4380 | |
|
4381 | 4381 | if opts.get('bookmark'): |
|
4382 | 4382 | rb = other.listkeys('bookmarks') |
|
4383 | 4383 | for b in opts['bookmark']: |
|
4384 | 4384 | # explicit push overrides remote bookmark if any |
|
4385 | 4385 | if b in repo._bookmarks: |
|
4386 | 4386 | ui.status(_("exporting bookmark %s\n") % b) |
|
4387 | 4387 | new = repo[b].hex() |
|
4388 | 4388 | elif b in rb: |
|
4389 | 4389 | ui.status(_("deleting remote bookmark %s\n") % b) |
|
4390 | 4390 | new = '' # delete |
|
4391 | 4391 | else: |
|
4392 | 4392 | ui.warn(_('bookmark %s does not exist on the local ' |
|
4393 | 4393 | 'or remote repository!\n') % b) |
|
4394 | 4394 | return 2 |
|
4395 | 4395 | old = rb.get(b, '') |
|
4396 | 4396 | r = other.pushkey('bookmarks', b, old, new) |
|
4397 | 4397 | if not r: |
|
4398 | 4398 | ui.warn(_('updating bookmark %s failed!\n') % b) |
|
4399 | 4399 | if not result: |
|
4400 | 4400 | result = 2 |
|
4401 | 4401 | |
|
4402 | 4402 | return result |
|
4403 | 4403 | |
|
4404 | 4404 | @command('recover', []) |
|
4405 | 4405 | def recover(ui, repo): |
|
4406 | 4406 | """roll back an interrupted transaction |
|
4407 | 4407 | |
|
4408 | 4408 | Recover from an interrupted commit or pull. |
|
4409 | 4409 | |
|
4410 | 4410 | This command tries to fix the repository status after an |
|
4411 | 4411 | interrupted operation. It should only be necessary when Mercurial |
|
4412 | 4412 | suggests it. |
|
4413 | 4413 | |
|
4414 | 4414 | Returns 0 if successful, 1 if nothing to recover or verify fails. |
|
4415 | 4415 | """ |
|
4416 | 4416 | if repo.recover(): |
|
4417 | 4417 | return hg.verify(repo) |
|
4418 | 4418 | return 1 |
|
4419 | 4419 | |
|
4420 | 4420 | @command('^remove|rm', |
|
4421 | 4421 | [('A', 'after', None, _('record delete for missing files')), |
|
4422 | 4422 | ('f', 'force', None, |
|
4423 | 4423 | _('remove (and delete) file even if added or modified')), |
|
4424 | 4424 | ] + walkopts, |
|
4425 | 4425 | _('[OPTION]... FILE...')) |
|
4426 | 4426 | def remove(ui, repo, *pats, **opts): |
|
4427 | 4427 | """remove the specified files on the next commit |
|
4428 | 4428 | |
|
4429 | 4429 | Schedule the indicated files for removal from the current branch. |
|
4430 | 4430 | |
|
4431 | 4431 | This command schedules the files to be removed at the next commit. |
|
4432 | 4432 | To undo a remove before that, see :hg:`revert`. To undo added |
|
4433 | 4433 | files, see :hg:`forget`. |
|
4434 | 4434 | |
|
4435 | 4435 | .. container:: verbose |
|
4436 | 4436 | |
|
4437 | 4437 | -A/--after can be used to remove only files that have already |
|
4438 | 4438 | been deleted, -f/--force can be used to force deletion, and -Af |
|
4439 | 4439 | can be used to remove files from the next revision without |
|
4440 | 4440 | deleting them from the working directory. |
|
4441 | 4441 | |
|
4442 | 4442 | The following table details the behavior of remove for different |
|
4443 | 4443 | file states (columns) and option combinations (rows). The file |
|
4444 | 4444 | states are Added [A], Clean [C], Modified [M] and Missing [!] |
|
4445 | 4445 | (as reported by :hg:`status`). The actions are Warn, Remove |
|
4446 | 4446 | (from branch) and Delete (from disk): |
|
4447 | 4447 | |
|
4448 | 4448 | ======= == == == == |
|
4449 | 4449 | A C M ! |
|
4450 | 4450 | ======= == == == == |
|
4451 | 4451 | none W RD W R |
|
4452 | 4452 | -f R RD RD R |
|
4453 | 4453 | -A W W W R |
|
4454 | 4454 | -Af R R R R |
|
4455 | 4455 | ======= == == == == |
|
4456 | 4456 | |
|
4457 | 4457 | Note that remove never deletes files in Added [A] state from the |
|
4458 | 4458 | working directory, not even if option --force is specified. |
|
4459 | 4459 | |
|
4460 | 4460 | Returns 0 on success, 1 if any warnings encountered. |
|
4461 | 4461 | """ |
|
4462 | 4462 | |
|
4463 | 4463 | ret = 0 |
|
4464 | 4464 | after, force = opts.get('after'), opts.get('force') |
|
4465 | 4465 | if not pats and not after: |
|
4466 | 4466 | raise util.Abort(_('no files specified')) |
|
4467 | 4467 | |
|
4468 | 4468 | m = scmutil.match(repo[None], pats, opts) |
|
4469 | 4469 | s = repo.status(match=m, clean=True) |
|
4470 | 4470 | modified, added, deleted, clean = s[0], s[1], s[3], s[6] |
|
4471 | 4471 | |
|
4472 | 4472 | for f in m.files(): |
|
4473 | 4473 | if f not in repo.dirstate and not os.path.isdir(m.rel(f)): |
|
4474 | 4474 | if os.path.exists(m.rel(f)): |
|
4475 | 4475 | ui.warn(_('not removing %s: file is untracked\n') % m.rel(f)) |
|
4476 | 4476 | ret = 1 |
|
4477 | 4477 | |
|
4478 | 4478 | if force: |
|
4479 | 4479 | list = modified + deleted + clean + added |
|
4480 | 4480 | elif after: |
|
4481 | 4481 | list = deleted |
|
4482 | 4482 | for f in modified + added + clean: |
|
4483 | 4483 | ui.warn(_('not removing %s: file still exists (use -f' |
|
4484 | 4484 | ' to force removal)\n') % m.rel(f)) |
|
4485 | 4485 | ret = 1 |
|
4486 | 4486 | else: |
|
4487 | 4487 | list = deleted + clean |
|
4488 | 4488 | for f in modified: |
|
4489 | 4489 | ui.warn(_('not removing %s: file is modified (use -f' |
|
4490 | 4490 | ' to force removal)\n') % m.rel(f)) |
|
4491 | 4491 | ret = 1 |
|
4492 | 4492 | for f in added: |
|
4493 | 4493 | ui.warn(_('not removing %s: file has been marked for add' |
|
4494 | 4494 | ' (use forget to undo)\n') % m.rel(f)) |
|
4495 | 4495 | ret = 1 |
|
4496 | 4496 | |
|
4497 | 4497 | for f in sorted(list): |
|
4498 | 4498 | if ui.verbose or not m.exact(f): |
|
4499 | 4499 | ui.status(_('removing %s\n') % m.rel(f)) |
|
4500 | 4500 | |
|
4501 | 4501 | wlock = repo.wlock() |
|
4502 | 4502 | try: |
|
4503 | 4503 | if not after: |
|
4504 | 4504 | for f in list: |
|
4505 | 4505 | if f in added: |
|
4506 | 4506 | continue # we never unlink added files on remove |
|
4507 | 4507 | try: |
|
4508 | 4508 | util.unlinkpath(repo.wjoin(f)) |
|
4509 | 4509 | except OSError, inst: |
|
4510 | 4510 | if inst.errno != errno.ENOENT: |
|
4511 | 4511 | raise |
|
4512 | 4512 | repo[None].forget(list) |
|
4513 | 4513 | finally: |
|
4514 | 4514 | wlock.release() |
|
4515 | 4515 | |
|
4516 | 4516 | return ret |
|
4517 | 4517 | |
|
4518 | 4518 | @command('rename|move|mv', |
|
4519 | 4519 | [('A', 'after', None, _('record a rename that has already occurred')), |
|
4520 | 4520 | ('f', 'force', None, _('forcibly copy over an existing managed file')), |
|
4521 | 4521 | ] + walkopts + dryrunopts, |
|
4522 | 4522 | _('[OPTION]... SOURCE... DEST')) |
|
4523 | 4523 | def rename(ui, repo, *pats, **opts): |
|
4524 | 4524 | """rename files; equivalent of copy + remove |
|
4525 | 4525 | |
|
4526 | 4526 | Mark dest as copies of sources; mark sources for deletion. If dest |
|
4527 | 4527 | is a directory, copies are put in that directory. If dest is a |
|
4528 | 4528 | file, there can only be one source. |
|
4529 | 4529 | |
|
4530 | 4530 | By default, this command copies the contents of files as they |
|
4531 | 4531 | exist in the working directory. If invoked with -A/--after, the |
|
4532 | 4532 | operation is recorded, but no copying is performed. |
|
4533 | 4533 | |
|
4534 | 4534 | This command takes effect at the next commit. To undo a rename |
|
4535 | 4535 | before that, see :hg:`revert`. |
|
4536 | 4536 | |
|
4537 | 4537 | Returns 0 on success, 1 if errors are encountered. |
|
4538 | 4538 | """ |
|
4539 | 4539 | wlock = repo.wlock(False) |
|
4540 | 4540 | try: |
|
4541 | 4541 | return cmdutil.copy(ui, repo, pats, opts, rename=True) |
|
4542 | 4542 | finally: |
|
4543 | 4543 | wlock.release() |
|
4544 | 4544 | |
|
4545 | 4545 | @command('resolve', |
|
4546 | 4546 | [('a', 'all', None, _('select all unresolved files')), |
|
4547 | 4547 | ('l', 'list', None, _('list state of files needing merge')), |
|
4548 | 4548 | ('m', 'mark', None, _('mark files as resolved')), |
|
4549 | 4549 | ('u', 'unmark', None, _('mark files as unresolved')), |
|
4550 | 4550 | ('n', 'no-status', None, _('hide status prefix'))] |
|
4551 | 4551 | + mergetoolopts + walkopts, |
|
4552 | 4552 | _('[OPTION]... [FILE]...')) |
|
4553 | 4553 | def resolve(ui, repo, *pats, **opts): |
|
4554 | 4554 | """redo merges or set/view the merge status of files |
|
4555 | 4555 | |
|
4556 | 4556 | Merges with unresolved conflicts are often the result of |
|
4557 | 4557 | non-interactive merging using the ``internal:merge`` configuration |
|
4558 | 4558 | setting, or a command-line merge tool like ``diff3``. The resolve |
|
4559 | 4559 | command is used to manage the files involved in a merge, after |
|
4560 | 4560 | :hg:`merge` has been run, and before :hg:`commit` is run (i.e. the |
|
4561 | 4561 | working directory must have two parents). |
|
4562 | 4562 | |
|
4563 | 4563 | The resolve command can be used in the following ways: |
|
4564 | 4564 | |
|
4565 | 4565 | - :hg:`resolve [--tool TOOL] FILE...`: attempt to re-merge the specified |
|
4566 | 4566 | files, discarding any previous merge attempts. Re-merging is not |
|
4567 | 4567 | performed for files already marked as resolved. Use ``--all/-a`` |
|
4568 | 4568 | to select all unresolved files. ``--tool`` can be used to specify |
|
4569 | 4569 | the merge tool used for the given files. It overrides the HGMERGE |
|
4570 | 4570 | environment variable and your configuration files. Previous file |
|
4571 | 4571 | contents are saved with a ``.orig`` suffix. |
|
4572 | 4572 | |
|
4573 | 4573 | - :hg:`resolve -m [FILE]`: mark a file as having been resolved |
|
4574 | 4574 | (e.g. after having manually fixed-up the files). The default is |
|
4575 | 4575 | to mark all unresolved files. |
|
4576 | 4576 | |
|
4577 | 4577 | - :hg:`resolve -u [FILE]...`: mark a file as unresolved. The |
|
4578 | 4578 | default is to mark all resolved files. |
|
4579 | 4579 | |
|
4580 | 4580 | - :hg:`resolve -l`: list files which had or still have conflicts. |
|
4581 | 4581 | In the printed list, ``U`` = unresolved and ``R`` = resolved. |
|
4582 | 4582 | |
|
4583 | 4583 | Note that Mercurial will not let you commit files with unresolved |
|
4584 | 4584 | merge conflicts. You must use :hg:`resolve -m ...` before you can |
|
4585 | 4585 | commit after a conflicting merge. |
|
4586 | 4586 | |
|
4587 | 4587 | Returns 0 on success, 1 if any files fail a resolve attempt. |
|
4588 | 4588 | """ |
|
4589 | 4589 | |
|
4590 | 4590 | all, mark, unmark, show, nostatus = \ |
|
4591 | 4591 | [opts.get(o) for o in 'all mark unmark list no_status'.split()] |
|
4592 | 4592 | |
|
4593 | 4593 | if (show and (mark or unmark)) or (mark and unmark): |
|
4594 | 4594 | raise util.Abort(_("too many options specified")) |
|
4595 | 4595 | if pats and all: |
|
4596 | 4596 | raise util.Abort(_("can't specify --all and patterns")) |
|
4597 | 4597 | if not (all or pats or show or mark or unmark): |
|
4598 | 4598 | raise util.Abort(_('no files or directories specified; ' |
|
4599 | 4599 | 'use --all to remerge all files')) |
|
4600 | 4600 | |
|
4601 | 4601 | ms = mergemod.mergestate(repo) |
|
4602 | 4602 | m = scmutil.match(repo[None], pats, opts) |
|
4603 | 4603 | ret = 0 |
|
4604 | 4604 | |
|
4605 | 4605 | for f in ms: |
|
4606 | 4606 | if m(f): |
|
4607 | 4607 | if show: |
|
4608 | 4608 | if nostatus: |
|
4609 | 4609 | ui.write("%s\n" % f) |
|
4610 | 4610 | else: |
|
4611 | 4611 | ui.write("%s %s\n" % (ms[f].upper(), f), |
|
4612 | 4612 | label='resolve.' + |
|
4613 | 4613 | {'u': 'unresolved', 'r': 'resolved'}[ms[f]]) |
|
4614 | 4614 | elif mark: |
|
4615 | 4615 | ms.mark(f, "r") |
|
4616 | 4616 | elif unmark: |
|
4617 | 4617 | ms.mark(f, "u") |
|
4618 | 4618 | else: |
|
4619 | 4619 | wctx = repo[None] |
|
4620 | 4620 | mctx = wctx.parents()[-1] |
|
4621 | 4621 | |
|
4622 | 4622 | # backup pre-resolve (merge uses .orig for its own purposes) |
|
4623 | 4623 | a = repo.wjoin(f) |
|
4624 | 4624 | util.copyfile(a, a + ".resolve") |
|
4625 | 4625 | |
|
4626 | 4626 | try: |
|
4627 | 4627 | # resolve file |
|
4628 | 4628 | ui.setconfig('ui', 'forcemerge', opts.get('tool', '')) |
|
4629 | 4629 | if ms.resolve(f, wctx, mctx): |
|
4630 | 4630 | ret = 1 |
|
4631 | 4631 | finally: |
|
4632 | 4632 | ui.setconfig('ui', 'forcemerge', '') |
|
4633 | 4633 | |
|
4634 | 4634 | # replace filemerge's .orig file with our resolve file |
|
4635 | 4635 | util.rename(a + ".resolve", a + ".orig") |
|
4636 | 4636 | |
|
4637 | 4637 | ms.commit() |
|
4638 | 4638 | return ret |
|
4639 | 4639 | |
|
4640 | 4640 | @command('revert', |
|
4641 | 4641 | [('a', 'all', None, _('revert all changes when no arguments given')), |
|
4642 | 4642 | ('d', 'date', '', _('tipmost revision matching date'), _('DATE')), |
|
4643 | 4643 | ('r', 'rev', '', _('revert to the specified revision'), _('REV')), |
|
4644 | 4644 | ('C', 'no-backup', None, _('do not save backup copies of files')), |
|
4645 | 4645 | ] + walkopts + dryrunopts, |
|
4646 | 4646 | _('[OPTION]... [-r REV] [NAME]...')) |
|
4647 | 4647 | def revert(ui, repo, *pats, **opts): |
|
4648 | 4648 | """restore files to their checkout state |
|
4649 | 4649 | |
|
4650 | 4650 | .. note:: |
|
4651 | 4651 | To check out earlier revisions, you should use :hg:`update REV`. |
|
4652 | 4652 | To cancel a merge (and lose your changes), use :hg:`update --clean .`. |
|
4653 | 4653 | |
|
4654 | 4654 | With no revision specified, revert the specified files or directories |
|
4655 | 4655 | to the contents they had in the parent of the working directory. |
|
4656 | 4656 | This restores the contents of files to an unmodified |
|
4657 | 4657 | state and unschedules adds, removes, copies, and renames. If the |
|
4658 | 4658 | working directory has two parents, you must explicitly specify a |
|
4659 | 4659 | revision. |
|
4660 | 4660 | |
|
4661 | 4661 | Using the -r/--rev or -d/--date options, revert the given files or |
|
4662 | 4662 | directories to their states as of a specific revision. Because |
|
4663 | 4663 | revert does not change the working directory parents, this will |
|
4664 | 4664 | cause these files to appear modified. This can be helpful to "back |
|
4665 | 4665 | out" some or all of an earlier change. See :hg:`backout` for a |
|
4666 | 4666 | related method. |
|
4667 | 4667 | |
|
4668 | 4668 | Modified files are saved with a .orig suffix before reverting. |
|
4669 | 4669 | To disable these backups, use --no-backup. |
|
4670 | 4670 | |
|
4671 | 4671 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
4672 | 4672 | |
|
4673 | 4673 | Returns 0 on success. |
|
4674 | 4674 | """ |
|
4675 | 4675 | |
|
4676 | 4676 | if opts.get("date"): |
|
4677 | 4677 | if opts.get("rev"): |
|
4678 | 4678 | raise util.Abort(_("you can't specify a revision and a date")) |
|
4679 | 4679 | opts["rev"] = cmdutil.finddate(ui, repo, opts["date"]) |
|
4680 | 4680 | |
|
4681 | 4681 | parent, p2 = repo.dirstate.parents() |
|
4682 | 4682 | if not opts.get('rev') and p2 != nullid: |
|
4683 | 4683 | # revert after merge is a trap for new users (issue2915) |
|
4684 | 4684 | raise util.Abort(_('uncommitted merge with no revision specified'), |
|
4685 | 4685 | hint=_('use "hg update" or see "hg help revert"')) |
|
4686 | 4686 | |
|
4687 | 4687 | ctx = scmutil.revsingle(repo, opts.get('rev')) |
|
4688 | 4688 | node = ctx.node() |
|
4689 | 4689 | |
|
4690 | 4690 | if not pats and not opts.get('all'): |
|
4691 | 4691 | msg = _("no files or directories specified") |
|
4692 | 4692 | if p2 != nullid: |
|
4693 | 4693 | hint = _("uncommitted merge, use --all to discard all changes," |
|
4694 | 4694 | " or 'hg update -C .' to abort the merge") |
|
4695 | 4695 | raise util.Abort(msg, hint=hint) |
|
4696 | 4696 | dirty = util.any(repo.status()) |
|
4697 | 4697 | if node != parent: |
|
4698 | 4698 | if dirty: |
|
4699 | 4699 | hint = _("uncommitted changes, use --all to discard all" |
|
4700 | 4700 | " changes, or 'hg update %s' to update") % ctx.rev() |
|
4701 | 4701 | else: |
|
4702 | 4702 | hint = _("use --all to revert all files," |
|
4703 | 4703 | " or 'hg update %s' to update") % ctx.rev() |
|
4704 | 4704 | elif dirty: |
|
4705 | 4705 | hint = _("uncommitted changes, use --all to discard all changes") |
|
4706 | 4706 | else: |
|
4707 | 4707 | hint = _("use --all to revert all files") |
|
4708 | 4708 | raise util.Abort(msg, hint=hint) |
|
4709 | 4709 | |
|
4710 | 4710 | mf = ctx.manifest() |
|
4711 | 4711 | if node == parent: |
|
4712 | 4712 | pmf = mf |
|
4713 | 4713 | else: |
|
4714 | 4714 | pmf = None |
|
4715 | 4715 | |
|
4716 | 4716 | # need all matching names in dirstate and manifest of target rev, |
|
4717 | 4717 | # so have to walk both. do not print errors if files exist in one |
|
4718 | 4718 | # but not other. |
|
4719 | 4719 | |
|
4720 | 4720 | names = {} |
|
4721 | 4721 | |
|
4722 | 4722 | wlock = repo.wlock() |
|
4723 | 4723 | try: |
|
4724 | 4724 | # walk dirstate. |
|
4725 | 4725 | |
|
4726 | 4726 | m = scmutil.match(repo[None], pats, opts) |
|
4727 | 4727 | m.bad = lambda x, y: False |
|
4728 | 4728 | for abs in repo.walk(m): |
|
4729 | 4729 | names[abs] = m.rel(abs), m.exact(abs) |
|
4730 | 4730 | |
|
4731 | 4731 | # walk target manifest. |
|
4732 | 4732 | |
|
4733 | 4733 | def badfn(path, msg): |
|
4734 | 4734 | if path in names: |
|
4735 | 4735 | return |
|
4736 | 4736 | if path in repo[node].substate: |
|
4737 | 4737 | ui.warn("%s: %s\n" % (m.rel(path), |
|
4738 | 4738 | 'reverting subrepos is unsupported')) |
|
4739 | 4739 | return |
|
4740 | 4740 | path_ = path + '/' |
|
4741 | 4741 | for f in names: |
|
4742 | 4742 | if f.startswith(path_): |
|
4743 | 4743 | return |
|
4744 | 4744 | ui.warn("%s: %s\n" % (m.rel(path), msg)) |
|
4745 | 4745 | |
|
4746 | 4746 | m = scmutil.match(repo[node], pats, opts) |
|
4747 | 4747 | m.bad = badfn |
|
4748 | 4748 | for abs in repo[node].walk(m): |
|
4749 | 4749 | if abs not in names: |
|
4750 | 4750 | names[abs] = m.rel(abs), m.exact(abs) |
|
4751 | 4751 | |
|
4752 | 4752 | m = scmutil.matchfiles(repo, names) |
|
4753 | 4753 | changes = repo.status(match=m)[:4] |
|
4754 | 4754 | modified, added, removed, deleted = map(set, changes) |
|
4755 | 4755 | |
|
4756 | 4756 | # if f is a rename, also revert the source |
|
4757 | 4757 | cwd = repo.getcwd() |
|
4758 | 4758 | for f in added: |
|
4759 | 4759 | src = repo.dirstate.copied(f) |
|
4760 | 4760 | if src and src not in names and repo.dirstate[src] == 'r': |
|
4761 | 4761 | removed.add(src) |
|
4762 | 4762 | names[src] = (repo.pathto(src, cwd), True) |
|
4763 | 4763 | |
|
4764 | 4764 | def removeforget(abs): |
|
4765 | 4765 | if repo.dirstate[abs] == 'a': |
|
4766 | 4766 | return _('forgetting %s\n') |
|
4767 | 4767 | return _('removing %s\n') |
|
4768 | 4768 | |
|
4769 | 4769 | revert = ([], _('reverting %s\n')) |
|
4770 | 4770 | add = ([], _('adding %s\n')) |
|
4771 | 4771 | remove = ([], removeforget) |
|
4772 | 4772 | undelete = ([], _('undeleting %s\n')) |
|
4773 | 4773 | |
|
4774 | 4774 | disptable = ( |
|
4775 | 4775 | # dispatch table: |
|
4776 | 4776 | # file state |
|
4777 | 4777 | # action if in target manifest |
|
4778 | 4778 | # action if not in target manifest |
|
4779 | 4779 | # make backup if in target manifest |
|
4780 | 4780 | # make backup if not in target manifest |
|
4781 | 4781 | (modified, revert, remove, True, True), |
|
4782 | 4782 | (added, revert, remove, True, False), |
|
4783 | 4783 | (removed, undelete, None, False, False), |
|
4784 | 4784 | (deleted, revert, remove, False, False), |
|
4785 | 4785 | ) |
|
4786 | 4786 | |
|
4787 | 4787 | for abs, (rel, exact) in sorted(names.items()): |
|
4788 | 4788 | mfentry = mf.get(abs) |
|
4789 | 4789 | target = repo.wjoin(abs) |
|
4790 | 4790 | def handle(xlist, dobackup): |
|
4791 | 4791 | xlist[0].append(abs) |
|
4792 | 4792 | if (dobackup and not opts.get('no_backup') and |
|
4793 | 4793 | os.path.lexists(target)): |
|
4794 | 4794 | bakname = "%s.orig" % rel |
|
4795 | 4795 | ui.note(_('saving current version of %s as %s\n') % |
|
4796 | 4796 | (rel, bakname)) |
|
4797 | 4797 | if not opts.get('dry_run'): |
|
4798 | 4798 | util.rename(target, bakname) |
|
4799 | 4799 | if ui.verbose or not exact: |
|
4800 | 4800 | msg = xlist[1] |
|
4801 | 4801 | if not isinstance(msg, basestring): |
|
4802 | 4802 | msg = msg(abs) |
|
4803 | 4803 | ui.status(msg % rel) |
|
4804 | 4804 | for table, hitlist, misslist, backuphit, backupmiss in disptable: |
|
4805 | 4805 | if abs not in table: |
|
4806 | 4806 | continue |
|
4807 | 4807 | # file has changed in dirstate |
|
4808 | 4808 | if mfentry: |
|
4809 | 4809 | handle(hitlist, backuphit) |
|
4810 | 4810 | elif misslist is not None: |
|
4811 | 4811 | handle(misslist, backupmiss) |
|
4812 | 4812 | break |
|
4813 | 4813 | else: |
|
4814 | 4814 | if abs not in repo.dirstate: |
|
4815 | 4815 | if mfentry: |
|
4816 | 4816 | handle(add, True) |
|
4817 | 4817 | elif exact: |
|
4818 | 4818 | ui.warn(_('file not managed: %s\n') % rel) |
|
4819 | 4819 | continue |
|
4820 | 4820 | # file has not changed in dirstate |
|
4821 | 4821 | if node == parent: |
|
4822 | 4822 | if exact: |
|
4823 | 4823 | ui.warn(_('no changes needed to %s\n') % rel) |
|
4824 | 4824 | continue |
|
4825 | 4825 | if pmf is None: |
|
4826 | 4826 | # only need parent manifest in this unlikely case, |
|
4827 | 4827 | # so do not read by default |
|
4828 | 4828 | pmf = repo[parent].manifest() |
|
4829 | 4829 | if abs in pmf and mfentry: |
|
4830 | 4830 | # if version of file is same in parent and target |
|
4831 | 4831 | # manifests, do nothing |
|
4832 | 4832 | if (pmf[abs] != mfentry or |
|
4833 | 4833 | pmf.flags(abs) != mf.flags(abs)): |
|
4834 | 4834 | handle(revert, False) |
|
4835 | 4835 | else: |
|
4836 | 4836 | handle(remove, False) |
|
4837 | 4837 | |
|
4838 | 4838 | if not opts.get('dry_run'): |
|
4839 | 4839 | def checkout(f): |
|
4840 | 4840 | fc = ctx[f] |
|
4841 | 4841 | repo.wwrite(f, fc.data(), fc.flags()) |
|
4842 | 4842 | |
|
4843 | 4843 | audit_path = scmutil.pathauditor(repo.root) |
|
4844 | 4844 | for f in remove[0]: |
|
4845 | 4845 | if repo.dirstate[f] == 'a': |
|
4846 | 4846 | repo.dirstate.drop(f) |
|
4847 | 4847 | continue |
|
4848 | 4848 | audit_path(f) |
|
4849 | 4849 | try: |
|
4850 | 4850 | util.unlinkpath(repo.wjoin(f)) |
|
4851 | 4851 | except OSError: |
|
4852 | 4852 | pass |
|
4853 | 4853 | repo.dirstate.remove(f) |
|
4854 | 4854 | |
|
4855 | 4855 | normal = None |
|
4856 | 4856 | if node == parent: |
|
4857 | 4857 | # We're reverting to our parent. If possible, we'd like status |
|
4858 | 4858 | # to report the file as clean. We have to use normallookup for |
|
4859 | 4859 | # merges to avoid losing information about merged/dirty files. |
|
4860 | 4860 | if p2 != nullid: |
|
4861 | 4861 | normal = repo.dirstate.normallookup |
|
4862 | 4862 | else: |
|
4863 | 4863 | normal = repo.dirstate.normal |
|
4864 | 4864 | for f in revert[0]: |
|
4865 | 4865 | checkout(f) |
|
4866 | 4866 | if normal: |
|
4867 | 4867 | normal(f) |
|
4868 | 4868 | |
|
4869 | 4869 | for f in add[0]: |
|
4870 | 4870 | checkout(f) |
|
4871 | 4871 | repo.dirstate.add(f) |
|
4872 | 4872 | |
|
4873 | 4873 | normal = repo.dirstate.normallookup |
|
4874 | 4874 | if node == parent and p2 == nullid: |
|
4875 | 4875 | normal = repo.dirstate.normal |
|
4876 | 4876 | for f in undelete[0]: |
|
4877 | 4877 | checkout(f) |
|
4878 | 4878 | normal(f) |
|
4879 | 4879 | |
|
4880 | 4880 | finally: |
|
4881 | 4881 | wlock.release() |
|
4882 | 4882 | |
|
4883 | 4883 | @command('rollback', dryrunopts + |
|
4884 | 4884 | [('f', 'force', False, _('ignore safety measures'))]) |
|
4885 | 4885 | def rollback(ui, repo, **opts): |
|
4886 | 4886 | """roll back the last transaction (dangerous) |
|
4887 | 4887 | |
|
4888 | 4888 | This command should be used with care. There is only one level of |
|
4889 | 4889 | rollback, and there is no way to undo a rollback. It will also |
|
4890 | 4890 | restore the dirstate at the time of the last transaction, losing |
|
4891 | 4891 | any dirstate changes since that time. This command does not alter |
|
4892 | 4892 | the working directory. |
|
4893 | 4893 | |
|
4894 | 4894 | Transactions are used to encapsulate the effects of all commands |
|
4895 | 4895 | that create new changesets or propagate existing changesets into a |
|
4896 | 4896 | repository. For example, the following commands are transactional, |
|
4897 | 4897 | and their effects can be rolled back: |
|
4898 | 4898 | |
|
4899 | 4899 | - commit |
|
4900 | 4900 | - import |
|
4901 | 4901 | - pull |
|
4902 | 4902 | - push (with this repository as the destination) |
|
4903 | 4903 | - unbundle |
|
4904 | 4904 | |
|
4905 | 4905 | To avoid permanent data loss, rollback will refuse to rollback a |
|
4906 | 4906 | commit transaction if it isn't checked out. Use --force to |
|
4907 | 4907 | override this protection. |
|
4908 | 4908 | |
|
4909 | 4909 | This command is not intended for use on public repositories. Once |
|
4910 | 4910 | changes are visible for pull by other users, rolling a transaction |
|
4911 | 4911 | back locally is ineffective (someone else may already have pulled |
|
4912 | 4912 | the changes). Furthermore, a race is possible with readers of the |
|
4913 | 4913 | repository; for example an in-progress pull from the repository |
|
4914 | 4914 | may fail if a rollback is performed. |
|
4915 | 4915 | |
|
4916 | 4916 | Returns 0 on success, 1 if no rollback data is available. |
|
4917 | 4917 | """ |
|
4918 | 4918 | return repo.rollback(dryrun=opts.get('dry_run'), |
|
4919 | 4919 | force=opts.get('force')) |
|
4920 | 4920 | |
|
4921 | 4921 | @command('root', []) |
|
4922 | 4922 | def root(ui, repo): |
|
4923 | 4923 | """print the root (top) of the current working directory |
|
4924 | 4924 | |
|
4925 | 4925 | Print the root directory of the current repository. |
|
4926 | 4926 | |
|
4927 | 4927 | Returns 0 on success. |
|
4928 | 4928 | """ |
|
4929 | 4929 | ui.write(repo.root + "\n") |
|
4930 | 4930 | |
|
4931 | 4931 | @command('^serve', |
|
4932 | 4932 | [('A', 'accesslog', '', _('name of access log file to write to'), |
|
4933 | 4933 | _('FILE')), |
|
4934 | 4934 | ('d', 'daemon', None, _('run server in background')), |
|
4935 | 4935 | ('', 'daemon-pipefds', '', _('used internally by daemon mode'), _('NUM')), |
|
4936 | 4936 | ('E', 'errorlog', '', _('name of error log file to write to'), _('FILE')), |
|
4937 | 4937 | # use string type, then we can check if something was passed |
|
4938 | 4938 | ('p', 'port', '', _('port to listen on (default: 8000)'), _('PORT')), |
|
4939 | 4939 | ('a', 'address', '', _('address to listen on (default: all interfaces)'), |
|
4940 | 4940 | _('ADDR')), |
|
4941 | 4941 | ('', 'prefix', '', _('prefix path to serve from (default: server root)'), |
|
4942 | 4942 | _('PREFIX')), |
|
4943 | 4943 | ('n', 'name', '', |
|
4944 | 4944 | _('name to show in web pages (default: working directory)'), _('NAME')), |
|
4945 | 4945 | ('', 'web-conf', '', |
|
4946 | 4946 | _('name of the hgweb config file (see "hg help hgweb")'), _('FILE')), |
|
4947 | 4947 | ('', 'webdir-conf', '', _('name of the hgweb config file (DEPRECATED)'), |
|
4948 | 4948 | _('FILE')), |
|
4949 | 4949 | ('', 'pid-file', '', _('name of file to write process ID to'), _('FILE')), |
|
4950 | 4950 | ('', 'stdio', None, _('for remote clients')), |
|
4951 | 4951 | ('', 'cmdserver', '', _('for remote clients'), _('MODE')), |
|
4952 | 4952 | ('t', 'templates', '', _('web templates to use'), _('TEMPLATE')), |
|
4953 | 4953 | ('', 'style', '', _('template style to use'), _('STYLE')), |
|
4954 | 4954 | ('6', 'ipv6', None, _('use IPv6 in addition to IPv4')), |
|
4955 | 4955 | ('', 'certificate', '', _('SSL certificate file'), _('FILE'))], |
|
4956 | 4956 | _('[OPTION]...')) |
|
4957 | 4957 | def serve(ui, repo, **opts): |
|
4958 | 4958 | """start stand-alone webserver |
|
4959 | 4959 | |
|
4960 | 4960 | Start a local HTTP repository browser and pull server. You can use |
|
4961 | 4961 | this for ad-hoc sharing and browsing of repositories. It is |
|
4962 | 4962 | recommended to use a real web server to serve a repository for |
|
4963 | 4963 | longer periods of time. |
|
4964 | 4964 | |
|
4965 | 4965 | Please note that the server does not implement access control. |
|
4966 | 4966 | This means that, by default, anybody can read from the server and |
|
4967 | 4967 | nobody can write to it by default. Set the ``web.allow_push`` |
|
4968 | 4968 | option to ``*`` to allow everybody to push to the server. You |
|
4969 | 4969 | should use a real web server if you need to authenticate users. |
|
4970 | 4970 | |
|
4971 | 4971 | By default, the server logs accesses to stdout and errors to |
|
4972 | 4972 | stderr. Use the -A/--accesslog and -E/--errorlog options to log to |
|
4973 | 4973 | files. |
|
4974 | 4974 | |
|
4975 | 4975 | To have the server choose a free port number to listen on, specify |
|
4976 | 4976 | a port number of 0; in this case, the server will print the port |
|
4977 | 4977 | number it uses. |
|
4978 | 4978 | |
|
4979 | 4979 | Returns 0 on success. |
|
4980 | 4980 | """ |
|
4981 | 4981 | |
|
4982 | 4982 | if opts["stdio"] and opts["cmdserver"]: |
|
4983 | 4983 | raise util.Abort(_("cannot use --stdio with --cmdserver")) |
|
4984 | 4984 | |
|
4985 | 4985 | def checkrepo(): |
|
4986 | 4986 | if repo is None: |
|
4987 | 4987 | raise error.RepoError(_("There is no Mercurial repository here" |
|
4988 | 4988 | " (.hg not found)")) |
|
4989 | 4989 | |
|
4990 | 4990 | if opts["stdio"]: |
|
4991 | 4991 | checkrepo() |
|
4992 | 4992 | s = sshserver.sshserver(ui, repo) |
|
4993 | 4993 | s.serve_forever() |
|
4994 | 4994 | |
|
4995 | 4995 | if opts["cmdserver"]: |
|
4996 | 4996 | checkrepo() |
|
4997 | 4997 | s = commandserver.server(ui, repo, opts["cmdserver"]) |
|
4998 | 4998 | return s.serve() |
|
4999 | 4999 | |
|
5000 | 5000 | # this way we can check if something was given in the command-line |
|
5001 | 5001 | if opts.get('port'): |
|
5002 | 5002 | opts['port'] = util.getport(opts.get('port')) |
|
5003 | 5003 | |
|
5004 | 5004 | baseui = repo and repo.baseui or ui |
|
5005 | 5005 | optlist = ("name templates style address port prefix ipv6" |
|
5006 | 5006 | " accesslog errorlog certificate encoding") |
|
5007 | 5007 | for o in optlist.split(): |
|
5008 | 5008 | val = opts.get(o, '') |
|
5009 | 5009 | if val in (None, ''): # should check against default options instead |
|
5010 | 5010 | continue |
|
5011 | 5011 | baseui.setconfig("web", o, val) |
|
5012 | 5012 | if repo and repo.ui != baseui: |
|
5013 | 5013 | repo.ui.setconfig("web", o, val) |
|
5014 | 5014 | |
|
5015 | 5015 | o = opts.get('web_conf') or opts.get('webdir_conf') |
|
5016 | 5016 | if not o: |
|
5017 | 5017 | if not repo: |
|
5018 | 5018 | raise error.RepoError(_("There is no Mercurial repository" |
|
5019 | 5019 | " here (.hg not found)")) |
|
5020 | 5020 | o = repo.root |
|
5021 | 5021 | |
|
5022 | 5022 | app = hgweb.hgweb(o, baseui=ui) |
|
5023 | 5023 | |
|
5024 | 5024 | class service(object): |
|
5025 | 5025 | def init(self): |
|
5026 | 5026 | util.setsignalhandler() |
|
5027 | 5027 | self.httpd = hgweb.server.create_server(ui, app) |
|
5028 | 5028 | |
|
5029 | 5029 | if opts['port'] and not ui.verbose: |
|
5030 | 5030 | return |
|
5031 | 5031 | |
|
5032 | 5032 | if self.httpd.prefix: |
|
5033 | 5033 | prefix = self.httpd.prefix.strip('/') + '/' |
|
5034 | 5034 | else: |
|
5035 | 5035 | prefix = '' |
|
5036 | 5036 | |
|
5037 | 5037 | port = ':%d' % self.httpd.port |
|
5038 | 5038 | if port == ':80': |
|
5039 | 5039 | port = '' |
|
5040 | 5040 | |
|
5041 | 5041 | bindaddr = self.httpd.addr |
|
5042 | 5042 | if bindaddr == '0.0.0.0': |
|
5043 | 5043 | bindaddr = '*' |
|
5044 | 5044 | elif ':' in bindaddr: # IPv6 |
|
5045 | 5045 | bindaddr = '[%s]' % bindaddr |
|
5046 | 5046 | |
|
5047 | 5047 | fqaddr = self.httpd.fqaddr |
|
5048 | 5048 | if ':' in fqaddr: |
|
5049 | 5049 | fqaddr = '[%s]' % fqaddr |
|
5050 | 5050 | if opts['port']: |
|
5051 | 5051 | write = ui.status |
|
5052 | 5052 | else: |
|
5053 | 5053 | write = ui.write |
|
5054 | 5054 | write(_('listening at http://%s%s/%s (bound to %s:%d)\n') % |
|
5055 | 5055 | (fqaddr, port, prefix, bindaddr, self.httpd.port)) |
|
5056 | 5056 | |
|
5057 | 5057 | def run(self): |
|
5058 | 5058 | self.httpd.serve_forever() |
|
5059 | 5059 | |
|
5060 | 5060 | service = service() |
|
5061 | 5061 | |
|
5062 | 5062 | cmdutil.service(opts, initfn=service.init, runfn=service.run) |
|
5063 | 5063 | |
|
5064 | 5064 | @command('showconfig|debugconfig', |
|
5065 | 5065 | [('u', 'untrusted', None, _('show untrusted configuration options'))], |
|
5066 | 5066 | _('[-u] [NAME]...')) |
|
5067 | 5067 | def showconfig(ui, repo, *values, **opts): |
|
5068 | 5068 | """show combined config settings from all hgrc files |
|
5069 | 5069 | |
|
5070 | 5070 | With no arguments, print names and values of all config items. |
|
5071 | 5071 | |
|
5072 | 5072 | With one argument of the form section.name, print just the value |
|
5073 | 5073 | of that config item. |
|
5074 | 5074 | |
|
5075 | 5075 | With multiple arguments, print names and values of all config |
|
5076 | 5076 | items with matching section names. |
|
5077 | 5077 | |
|
5078 | 5078 | With --debug, the source (filename and line number) is printed |
|
5079 | 5079 | for each config item. |
|
5080 | 5080 | |
|
5081 | 5081 | Returns 0 on success. |
|
5082 | 5082 | """ |
|
5083 | 5083 | |
|
5084 | 5084 | for f in scmutil.rcpath(): |
|
5085 | 5085 | ui.debug('read config from: %s\n' % f) |
|
5086 | 5086 | untrusted = bool(opts.get('untrusted')) |
|
5087 | 5087 | if values: |
|
5088 | 5088 | sections = [v for v in values if '.' not in v] |
|
5089 | 5089 | items = [v for v in values if '.' in v] |
|
5090 | 5090 | if len(items) > 1 or items and sections: |
|
5091 | 5091 | raise util.Abort(_('only one config item permitted')) |
|
5092 | 5092 | for section, name, value in ui.walkconfig(untrusted=untrusted): |
|
5093 | 5093 | value = str(value).replace('\n', '\\n') |
|
5094 | 5094 | sectname = section + '.' + name |
|
5095 | 5095 | if values: |
|
5096 | 5096 | for v in values: |
|
5097 | 5097 | if v == section: |
|
5098 | 5098 | ui.debug('%s: ' % |
|
5099 | 5099 | ui.configsource(section, name, untrusted)) |
|
5100 | 5100 | ui.write('%s=%s\n' % (sectname, value)) |
|
5101 | 5101 | elif v == sectname: |
|
5102 | 5102 | ui.debug('%s: ' % |
|
5103 | 5103 | ui.configsource(section, name, untrusted)) |
|
5104 | 5104 | ui.write(value, '\n') |
|
5105 | 5105 | else: |
|
5106 | 5106 | ui.debug('%s: ' % |
|
5107 | 5107 | ui.configsource(section, name, untrusted)) |
|
5108 | 5108 | ui.write('%s=%s\n' % (sectname, value)) |
|
5109 | 5109 | |
|
5110 | 5110 | @command('^status|st', |
|
5111 | 5111 | [('A', 'all', None, _('show status of all files')), |
|
5112 | 5112 | ('m', 'modified', None, _('show only modified files')), |
|
5113 | 5113 | ('a', 'added', None, _('show only added files')), |
|
5114 | 5114 | ('r', 'removed', None, _('show only removed files')), |
|
5115 | 5115 | ('d', 'deleted', None, _('show only deleted (but tracked) files')), |
|
5116 | 5116 | ('c', 'clean', None, _('show only files without changes')), |
|
5117 | 5117 | ('u', 'unknown', None, _('show only unknown (not tracked) files')), |
|
5118 | 5118 | ('i', 'ignored', None, _('show only ignored files')), |
|
5119 | 5119 | ('n', 'no-status', None, _('hide status prefix')), |
|
5120 | 5120 | ('C', 'copies', None, _('show source of copied files')), |
|
5121 | 5121 | ('0', 'print0', None, _('end filenames with NUL, for use with xargs')), |
|
5122 | 5122 | ('', 'rev', [], _('show difference from revision'), _('REV')), |
|
5123 | 5123 | ('', 'change', '', _('list the changed files of a revision'), _('REV')), |
|
5124 | 5124 | ] + walkopts + subrepoopts, |
|
5125 | 5125 | _('[OPTION]... [FILE]...')) |
|
5126 | 5126 | def status(ui, repo, *pats, **opts): |
|
5127 | 5127 | """show changed files in the working directory |
|
5128 | 5128 | |
|
5129 | 5129 | Show status of files in the repository. If names are given, only |
|
5130 | 5130 | files that match are shown. Files that are clean or ignored or |
|
5131 | 5131 | the source of a copy/move operation, are not listed unless |
|
5132 | 5132 | -c/--clean, -i/--ignored, -C/--copies or -A/--all are given. |
|
5133 | 5133 | Unless options described with "show only ..." are given, the |
|
5134 | 5134 | options -mardu are used. |
|
5135 | 5135 | |
|
5136 | 5136 | Option -q/--quiet hides untracked (unknown and ignored) files |
|
5137 | 5137 | unless explicitly requested with -u/--unknown or -i/--ignored. |
|
5138 | 5138 | |
|
5139 | 5139 | .. note:: |
|
5140 | 5140 | status may appear to disagree with diff if permissions have |
|
5141 | 5141 | changed or a merge has occurred. The standard diff format does |
|
5142 | 5142 | not report permission changes and diff only reports changes |
|
5143 | 5143 | relative to one merge parent. |
|
5144 | 5144 | |
|
5145 | 5145 | If one revision is given, it is used as the base revision. |
|
5146 | 5146 | If two revisions are given, the differences between them are |
|
5147 | 5147 | shown. The --change option can also be used as a shortcut to list |
|
5148 | 5148 | the changed files of a revision from its first parent. |
|
5149 | 5149 | |
|
5150 | 5150 | The codes used to show the status of files are:: |
|
5151 | 5151 | |
|
5152 | 5152 | M = modified |
|
5153 | 5153 | A = added |
|
5154 | 5154 | R = removed |
|
5155 | 5155 | C = clean |
|
5156 | 5156 | ! = missing (deleted by non-hg command, but still tracked) |
|
5157 | 5157 | ? = not tracked |
|
5158 | 5158 | I = ignored |
|
5159 | 5159 | = origin of the previous file listed as A (added) |
|
5160 | 5160 | |
|
5161 | 5161 | .. container:: verbose |
|
5162 | 5162 | |
|
5163 | 5163 | Examples: |
|
5164 | 5164 | |
|
5165 | 5165 | - show changes in the working directory relative to a |
|
5166 | 5166 | changeset:: |
|
5167 | 5167 | |
|
5168 | 5168 | hg status --rev 9353 |
|
5169 | 5169 | |
|
5170 | 5170 | - show all changes including copies in an existing changeset:: |
|
5171 | 5171 | |
|
5172 | 5172 | hg status --copies --change 9353 |
|
5173 | 5173 | |
|
5174 | 5174 | - get a NUL separated list of added files, suitable for xargs:: |
|
5175 | 5175 | |
|
5176 | 5176 | hg status -an0 |
|
5177 | 5177 | |
|
5178 | 5178 | Returns 0 on success. |
|
5179 | 5179 | """ |
|
5180 | 5180 | |
|
5181 | 5181 | revs = opts.get('rev') |
|
5182 | 5182 | change = opts.get('change') |
|
5183 | 5183 | |
|
5184 | 5184 | if revs and change: |
|
5185 | 5185 | msg = _('cannot specify --rev and --change at the same time') |
|
5186 | 5186 | raise util.Abort(msg) |
|
5187 | 5187 | elif change: |
|
5188 | 5188 | node2 = scmutil.revsingle(repo, change, None).node() |
|
5189 | 5189 | node1 = repo[node2].p1().node() |
|
5190 | 5190 | else: |
|
5191 | 5191 | node1, node2 = scmutil.revpair(repo, revs) |
|
5192 | 5192 | |
|
5193 | 5193 | cwd = (pats and repo.getcwd()) or '' |
|
5194 | 5194 | end = opts.get('print0') and '\0' or '\n' |
|
5195 | 5195 | copy = {} |
|
5196 | 5196 | states = 'modified added removed deleted unknown ignored clean'.split() |
|
5197 | 5197 | show = [k for k in states if opts.get(k)] |
|
5198 | 5198 | if opts.get('all'): |
|
5199 | 5199 | show += ui.quiet and (states[:4] + ['clean']) or states |
|
5200 | 5200 | if not show: |
|
5201 | 5201 | show = ui.quiet and states[:4] or states[:5] |
|
5202 | 5202 | |
|
5203 | 5203 | stat = repo.status(node1, node2, scmutil.match(repo[node2], pats, opts), |
|
5204 | 5204 | 'ignored' in show, 'clean' in show, 'unknown' in show, |
|
5205 | 5205 | opts.get('subrepos')) |
|
5206 | 5206 | changestates = zip(states, 'MAR!?IC', stat) |
|
5207 | 5207 | |
|
5208 | 5208 | if (opts.get('all') or opts.get('copies')) and not opts.get('no_status'): |
|
5209 | ctxn = repo[nullid] | |
|
5210 | 5209 | ctx1 = repo[node1] |
|
5211 | 5210 | ctx2 = repo[node2] |
|
5212 | 5211 | added = stat[1] |
|
5213 | 5212 | if node2 is None: |
|
5214 | 5213 | added = stat[0] + stat[1] # merged? |
|
5215 | 5214 | |
|
5216 |
for k, v in copies.copies( |
|
|
5215 | for k, v in copies.pathcopies(ctx1, ctx2).iteritems(): | |
|
5217 | 5216 | if k in added: |
|
5218 | 5217 | copy[k] = v |
|
5219 | 5218 | elif v in added: |
|
5220 | 5219 | copy[v] = k |
|
5221 | 5220 | |
|
5222 | 5221 | for state, char, files in changestates: |
|
5223 | 5222 | if state in show: |
|
5224 | 5223 | format = "%s %%s%s" % (char, end) |
|
5225 | 5224 | if opts.get('no_status'): |
|
5226 | 5225 | format = "%%s%s" % end |
|
5227 | 5226 | |
|
5228 | 5227 | for f in files: |
|
5229 | 5228 | ui.write(format % repo.pathto(f, cwd), |
|
5230 | 5229 | label='status.' + state) |
|
5231 | 5230 | if f in copy: |
|
5232 | 5231 | ui.write(' %s%s' % (repo.pathto(copy[f], cwd), end), |
|
5233 | 5232 | label='status.copied') |
|
5234 | 5233 | |
|
5235 | 5234 | @command('^summary|sum', |
|
5236 | 5235 | [('', 'remote', None, _('check for push and pull'))], '[--remote]') |
|
5237 | 5236 | def summary(ui, repo, **opts): |
|
5238 | 5237 | """summarize working directory state |
|
5239 | 5238 | |
|
5240 | 5239 | This generates a brief summary of the working directory state, |
|
5241 | 5240 | including parents, branch, commit status, and available updates. |
|
5242 | 5241 | |
|
5243 | 5242 | With the --remote option, this will check the default paths for |
|
5244 | 5243 | incoming and outgoing changes. This can be time-consuming. |
|
5245 | 5244 | |
|
5246 | 5245 | Returns 0 on success. |
|
5247 | 5246 | """ |
|
5248 | 5247 | |
|
5249 | 5248 | ctx = repo[None] |
|
5250 | 5249 | parents = ctx.parents() |
|
5251 | 5250 | pnode = parents[0].node() |
|
5252 | 5251 | marks = [] |
|
5253 | 5252 | |
|
5254 | 5253 | for p in parents: |
|
5255 | 5254 | # label with log.changeset (instead of log.parent) since this |
|
5256 | 5255 | # shows a working directory parent *changeset*: |
|
5257 | 5256 | ui.write(_('parent: %d:%s ') % (p.rev(), str(p)), |
|
5258 | 5257 | label='log.changeset') |
|
5259 | 5258 | ui.write(' '.join(p.tags()), label='log.tag') |
|
5260 | 5259 | if p.bookmarks(): |
|
5261 | 5260 | marks.extend(p.bookmarks()) |
|
5262 | 5261 | if p.rev() == -1: |
|
5263 | 5262 | if not len(repo): |
|
5264 | 5263 | ui.write(_(' (empty repository)')) |
|
5265 | 5264 | else: |
|
5266 | 5265 | ui.write(_(' (no revision checked out)')) |
|
5267 | 5266 | ui.write('\n') |
|
5268 | 5267 | if p.description(): |
|
5269 | 5268 | ui.status(' ' + p.description().splitlines()[0].strip() + '\n', |
|
5270 | 5269 | label='log.summary') |
|
5271 | 5270 | |
|
5272 | 5271 | branch = ctx.branch() |
|
5273 | 5272 | bheads = repo.branchheads(branch) |
|
5274 | 5273 | m = _('branch: %s\n') % branch |
|
5275 | 5274 | if branch != 'default': |
|
5276 | 5275 | ui.write(m, label='log.branch') |
|
5277 | 5276 | else: |
|
5278 | 5277 | ui.status(m, label='log.branch') |
|
5279 | 5278 | |
|
5280 | 5279 | if marks: |
|
5281 | 5280 | current = repo._bookmarkcurrent |
|
5282 | 5281 | ui.write(_('bookmarks:'), label='log.bookmark') |
|
5283 | 5282 | if current is not None: |
|
5284 | 5283 | try: |
|
5285 | 5284 | marks.remove(current) |
|
5286 | 5285 | ui.write(' *' + current, label='bookmarks.current') |
|
5287 | 5286 | except ValueError: |
|
5288 | 5287 | # current bookmark not in parent ctx marks |
|
5289 | 5288 | pass |
|
5290 | 5289 | for m in marks: |
|
5291 | 5290 | ui.write(' ' + m, label='log.bookmark') |
|
5292 | 5291 | ui.write('\n', label='log.bookmark') |
|
5293 | 5292 | |
|
5294 | 5293 | st = list(repo.status(unknown=True))[:6] |
|
5295 | 5294 | |
|
5296 | 5295 | c = repo.dirstate.copies() |
|
5297 | 5296 | copied, renamed = [], [] |
|
5298 | 5297 | for d, s in c.iteritems(): |
|
5299 | 5298 | if s in st[2]: |
|
5300 | 5299 | st[2].remove(s) |
|
5301 | 5300 | renamed.append(d) |
|
5302 | 5301 | else: |
|
5303 | 5302 | copied.append(d) |
|
5304 | 5303 | if d in st[1]: |
|
5305 | 5304 | st[1].remove(d) |
|
5306 | 5305 | st.insert(3, renamed) |
|
5307 | 5306 | st.insert(4, copied) |
|
5308 | 5307 | |
|
5309 | 5308 | ms = mergemod.mergestate(repo) |
|
5310 | 5309 | st.append([f for f in ms if ms[f] == 'u']) |
|
5311 | 5310 | |
|
5312 | 5311 | subs = [s for s in ctx.substate if ctx.sub(s).dirty()] |
|
5313 | 5312 | st.append(subs) |
|
5314 | 5313 | |
|
5315 | 5314 | labels = [ui.label(_('%d modified'), 'status.modified'), |
|
5316 | 5315 | ui.label(_('%d added'), 'status.added'), |
|
5317 | 5316 | ui.label(_('%d removed'), 'status.removed'), |
|
5318 | 5317 | ui.label(_('%d renamed'), 'status.copied'), |
|
5319 | 5318 | ui.label(_('%d copied'), 'status.copied'), |
|
5320 | 5319 | ui.label(_('%d deleted'), 'status.deleted'), |
|
5321 | 5320 | ui.label(_('%d unknown'), 'status.unknown'), |
|
5322 | 5321 | ui.label(_('%d ignored'), 'status.ignored'), |
|
5323 | 5322 | ui.label(_('%d unresolved'), 'resolve.unresolved'), |
|
5324 | 5323 | ui.label(_('%d subrepos'), 'status.modified')] |
|
5325 | 5324 | t = [] |
|
5326 | 5325 | for s, l in zip(st, labels): |
|
5327 | 5326 | if s: |
|
5328 | 5327 | t.append(l % len(s)) |
|
5329 | 5328 | |
|
5330 | 5329 | t = ', '.join(t) |
|
5331 | 5330 | cleanworkdir = False |
|
5332 | 5331 | |
|
5333 | 5332 | if len(parents) > 1: |
|
5334 | 5333 | t += _(' (merge)') |
|
5335 | 5334 | elif branch != parents[0].branch(): |
|
5336 | 5335 | t += _(' (new branch)') |
|
5337 | 5336 | elif (parents[0].extra().get('close') and |
|
5338 | 5337 | pnode in repo.branchheads(branch, closed=True)): |
|
5339 | 5338 | t += _(' (head closed)') |
|
5340 | 5339 | elif not (st[0] or st[1] or st[2] or st[3] or st[4] or st[9]): |
|
5341 | 5340 | t += _(' (clean)') |
|
5342 | 5341 | cleanworkdir = True |
|
5343 | 5342 | elif pnode not in bheads: |
|
5344 | 5343 | t += _(' (new branch head)') |
|
5345 | 5344 | |
|
5346 | 5345 | if cleanworkdir: |
|
5347 | 5346 | ui.status(_('commit: %s\n') % t.strip()) |
|
5348 | 5347 | else: |
|
5349 | 5348 | ui.write(_('commit: %s\n') % t.strip()) |
|
5350 | 5349 | |
|
5351 | 5350 | # all ancestors of branch heads - all ancestors of parent = new csets |
|
5352 | 5351 | new = [0] * len(repo) |
|
5353 | 5352 | cl = repo.changelog |
|
5354 | 5353 | for a in [cl.rev(n) for n in bheads]: |
|
5355 | 5354 | new[a] = 1 |
|
5356 | 5355 | for a in cl.ancestors(*[cl.rev(n) for n in bheads]): |
|
5357 | 5356 | new[a] = 1 |
|
5358 | 5357 | for a in [p.rev() for p in parents]: |
|
5359 | 5358 | if a >= 0: |
|
5360 | 5359 | new[a] = 0 |
|
5361 | 5360 | for a in cl.ancestors(*[p.rev() for p in parents]): |
|
5362 | 5361 | new[a] = 0 |
|
5363 | 5362 | new = sum(new) |
|
5364 | 5363 | |
|
5365 | 5364 | if new == 0: |
|
5366 | 5365 | ui.status(_('update: (current)\n')) |
|
5367 | 5366 | elif pnode not in bheads: |
|
5368 | 5367 | ui.write(_('update: %d new changesets (update)\n') % new) |
|
5369 | 5368 | else: |
|
5370 | 5369 | ui.write(_('update: %d new changesets, %d branch heads (merge)\n') % |
|
5371 | 5370 | (new, len(bheads))) |
|
5372 | 5371 | |
|
5373 | 5372 | if opts.get('remote'): |
|
5374 | 5373 | t = [] |
|
5375 | 5374 | source, branches = hg.parseurl(ui.expandpath('default')) |
|
5376 | 5375 | other = hg.peer(repo, {}, source) |
|
5377 | 5376 | revs, checkout = hg.addbranchrevs(repo, other, branches, opts.get('rev')) |
|
5378 | 5377 | ui.debug('comparing with %s\n' % util.hidepassword(source)) |
|
5379 | 5378 | repo.ui.pushbuffer() |
|
5380 | 5379 | commoninc = discovery.findcommonincoming(repo, other) |
|
5381 | 5380 | _common, incoming, _rheads = commoninc |
|
5382 | 5381 | repo.ui.popbuffer() |
|
5383 | 5382 | if incoming: |
|
5384 | 5383 | t.append(_('1 or more incoming')) |
|
5385 | 5384 | |
|
5386 | 5385 | dest, branches = hg.parseurl(ui.expandpath('default-push', 'default')) |
|
5387 | 5386 | revs, checkout = hg.addbranchrevs(repo, repo, branches, None) |
|
5388 | 5387 | if source != dest: |
|
5389 | 5388 | other = hg.peer(repo, {}, dest) |
|
5390 | 5389 | commoninc = None |
|
5391 | 5390 | ui.debug('comparing with %s\n' % util.hidepassword(dest)) |
|
5392 | 5391 | repo.ui.pushbuffer() |
|
5393 | 5392 | common, outheads = discovery.findcommonoutgoing(repo, other, |
|
5394 | 5393 | commoninc=commoninc) |
|
5395 | 5394 | repo.ui.popbuffer() |
|
5396 | 5395 | o = repo.changelog.findmissing(common=common, heads=outheads) |
|
5397 | 5396 | if o: |
|
5398 | 5397 | t.append(_('%d outgoing') % len(o)) |
|
5399 | 5398 | if 'bookmarks' in other.listkeys('namespaces'): |
|
5400 | 5399 | lmarks = repo.listkeys('bookmarks') |
|
5401 | 5400 | rmarks = other.listkeys('bookmarks') |
|
5402 | 5401 | diff = set(rmarks) - set(lmarks) |
|
5403 | 5402 | if len(diff) > 0: |
|
5404 | 5403 | t.append(_('%d incoming bookmarks') % len(diff)) |
|
5405 | 5404 | diff = set(lmarks) - set(rmarks) |
|
5406 | 5405 | if len(diff) > 0: |
|
5407 | 5406 | t.append(_('%d outgoing bookmarks') % len(diff)) |
|
5408 | 5407 | |
|
5409 | 5408 | if t: |
|
5410 | 5409 | ui.write(_('remote: %s\n') % (', '.join(t))) |
|
5411 | 5410 | else: |
|
5412 | 5411 | ui.status(_('remote: (synced)\n')) |
|
5413 | 5412 | |
|
5414 | 5413 | @command('tag', |
|
5415 | 5414 | [('f', 'force', None, _('force tag')), |
|
5416 | 5415 | ('l', 'local', None, _('make the tag local')), |
|
5417 | 5416 | ('r', 'rev', '', _('revision to tag'), _('REV')), |
|
5418 | 5417 | ('', 'remove', None, _('remove a tag')), |
|
5419 | 5418 | # -l/--local is already there, commitopts cannot be used |
|
5420 | 5419 | ('e', 'edit', None, _('edit commit message')), |
|
5421 | 5420 | ('m', 'message', '', _('use <text> as commit message'), _('TEXT')), |
|
5422 | 5421 | ] + commitopts2, |
|
5423 | 5422 | _('[-f] [-l] [-m TEXT] [-d DATE] [-u USER] [-r REV] NAME...')) |
|
5424 | 5423 | def tag(ui, repo, name1, *names, **opts): |
|
5425 | 5424 | """add one or more tags for the current or given revision |
|
5426 | 5425 | |
|
5427 | 5426 | Name a particular revision using <name>. |
|
5428 | 5427 | |
|
5429 | 5428 | Tags are used to name particular revisions of the repository and are |
|
5430 | 5429 | very useful to compare different revisions, to go back to significant |
|
5431 | 5430 | earlier versions or to mark branch points as releases, etc. Changing |
|
5432 | 5431 | an existing tag is normally disallowed; use -f/--force to override. |
|
5433 | 5432 | |
|
5434 | 5433 | If no revision is given, the parent of the working directory is |
|
5435 | 5434 | used, or tip if no revision is checked out. |
|
5436 | 5435 | |
|
5437 | 5436 | To facilitate version control, distribution, and merging of tags, |
|
5438 | 5437 | they are stored as a file named ".hgtags" which is managed similarly |
|
5439 | 5438 | to other project files and can be hand-edited if necessary. This |
|
5440 | 5439 | also means that tagging creates a new commit. The file |
|
5441 | 5440 | ".hg/localtags" is used for local tags (not shared among |
|
5442 | 5441 | repositories). |
|
5443 | 5442 | |
|
5444 | 5443 | Tag commits are usually made at the head of a branch. If the parent |
|
5445 | 5444 | of the working directory is not a branch head, :hg:`tag` aborts; use |
|
5446 | 5445 | -f/--force to force the tag commit to be based on a non-head |
|
5447 | 5446 | changeset. |
|
5448 | 5447 | |
|
5449 | 5448 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
5450 | 5449 | |
|
5451 | 5450 | Since tag names have priority over branch names during revision |
|
5452 | 5451 | lookup, using an existing branch name as a tag name is discouraged. |
|
5453 | 5452 | |
|
5454 | 5453 | Returns 0 on success. |
|
5455 | 5454 | """ |
|
5456 | 5455 | |
|
5457 | 5456 | rev_ = "." |
|
5458 | 5457 | names = [t.strip() for t in (name1,) + names] |
|
5459 | 5458 | if len(names) != len(set(names)): |
|
5460 | 5459 | raise util.Abort(_('tag names must be unique')) |
|
5461 | 5460 | for n in names: |
|
5462 | 5461 | if n in ['tip', '.', 'null']: |
|
5463 | 5462 | raise util.Abort(_("the name '%s' is reserved") % n) |
|
5464 | 5463 | if not n: |
|
5465 | 5464 | raise util.Abort(_('tag names cannot consist entirely of whitespace')) |
|
5466 | 5465 | if opts.get('rev') and opts.get('remove'): |
|
5467 | 5466 | raise util.Abort(_("--rev and --remove are incompatible")) |
|
5468 | 5467 | if opts.get('rev'): |
|
5469 | 5468 | rev_ = opts['rev'] |
|
5470 | 5469 | message = opts.get('message') |
|
5471 | 5470 | if opts.get('remove'): |
|
5472 | 5471 | expectedtype = opts.get('local') and 'local' or 'global' |
|
5473 | 5472 | for n in names: |
|
5474 | 5473 | if not repo.tagtype(n): |
|
5475 | 5474 | raise util.Abort(_("tag '%s' does not exist") % n) |
|
5476 | 5475 | if repo.tagtype(n) != expectedtype: |
|
5477 | 5476 | if expectedtype == 'global': |
|
5478 | 5477 | raise util.Abort(_("tag '%s' is not a global tag") % n) |
|
5479 | 5478 | else: |
|
5480 | 5479 | raise util.Abort(_("tag '%s' is not a local tag") % n) |
|
5481 | 5480 | rev_ = nullid |
|
5482 | 5481 | if not message: |
|
5483 | 5482 | # we don't translate commit messages |
|
5484 | 5483 | message = 'Removed tag %s' % ', '.join(names) |
|
5485 | 5484 | elif not opts.get('force'): |
|
5486 | 5485 | for n in names: |
|
5487 | 5486 | if n in repo.tags(): |
|
5488 | 5487 | raise util.Abort(_("tag '%s' already exists " |
|
5489 | 5488 | "(use -f to force)") % n) |
|
5490 | 5489 | if not opts.get('local'): |
|
5491 | 5490 | p1, p2 = repo.dirstate.parents() |
|
5492 | 5491 | if p2 != nullid: |
|
5493 | 5492 | raise util.Abort(_('uncommitted merge')) |
|
5494 | 5493 | bheads = repo.branchheads() |
|
5495 | 5494 | if not opts.get('force') and bheads and p1 not in bheads: |
|
5496 | 5495 | raise util.Abort(_('not at a branch head (use -f to force)')) |
|
5497 | 5496 | r = scmutil.revsingle(repo, rev_).node() |
|
5498 | 5497 | |
|
5499 | 5498 | if not message: |
|
5500 | 5499 | # we don't translate commit messages |
|
5501 | 5500 | message = ('Added tag %s for changeset %s' % |
|
5502 | 5501 | (', '.join(names), short(r))) |
|
5503 | 5502 | |
|
5504 | 5503 | date = opts.get('date') |
|
5505 | 5504 | if date: |
|
5506 | 5505 | date = util.parsedate(date) |
|
5507 | 5506 | |
|
5508 | 5507 | if opts.get('edit'): |
|
5509 | 5508 | message = ui.edit(message, ui.username()) |
|
5510 | 5509 | |
|
5511 | 5510 | repo.tag(names, r, message, opts.get('local'), opts.get('user'), date) |
|
5512 | 5511 | |
|
5513 | 5512 | @command('tags', [], '') |
|
5514 | 5513 | def tags(ui, repo): |
|
5515 | 5514 | """list repository tags |
|
5516 | 5515 | |
|
5517 | 5516 | This lists both regular and local tags. When the -v/--verbose |
|
5518 | 5517 | switch is used, a third column "local" is printed for local tags. |
|
5519 | 5518 | |
|
5520 | 5519 | Returns 0 on success. |
|
5521 | 5520 | """ |
|
5522 | 5521 | |
|
5523 | 5522 | hexfunc = ui.debugflag and hex or short |
|
5524 | 5523 | tagtype = "" |
|
5525 | 5524 | |
|
5526 | 5525 | for t, n in reversed(repo.tagslist()): |
|
5527 | 5526 | if ui.quiet: |
|
5528 | 5527 | ui.write("%s\n" % t, label='tags.normal') |
|
5529 | 5528 | continue |
|
5530 | 5529 | |
|
5531 | 5530 | hn = hexfunc(n) |
|
5532 | 5531 | r = "%5d:%s" % (repo.changelog.rev(n), hn) |
|
5533 | 5532 | rev = ui.label(r, 'log.changeset') |
|
5534 | 5533 | spaces = " " * (30 - encoding.colwidth(t)) |
|
5535 | 5534 | |
|
5536 | 5535 | tag = ui.label(t, 'tags.normal') |
|
5537 | 5536 | if ui.verbose: |
|
5538 | 5537 | if repo.tagtype(t) == 'local': |
|
5539 | 5538 | tagtype = " local" |
|
5540 | 5539 | tag = ui.label(t, 'tags.local') |
|
5541 | 5540 | else: |
|
5542 | 5541 | tagtype = "" |
|
5543 | 5542 | ui.write("%s%s %s%s\n" % (tag, spaces, rev, tagtype)) |
|
5544 | 5543 | |
|
5545 | 5544 | @command('tip', |
|
5546 | 5545 | [('p', 'patch', None, _('show patch')), |
|
5547 | 5546 | ('g', 'git', None, _('use git extended diff format')), |
|
5548 | 5547 | ] + templateopts, |
|
5549 | 5548 | _('[-p] [-g]')) |
|
5550 | 5549 | def tip(ui, repo, **opts): |
|
5551 | 5550 | """show the tip revision |
|
5552 | 5551 | |
|
5553 | 5552 | The tip revision (usually just called the tip) is the changeset |
|
5554 | 5553 | most recently added to the repository (and therefore the most |
|
5555 | 5554 | recently changed head). |
|
5556 | 5555 | |
|
5557 | 5556 | If you have just made a commit, that commit will be the tip. If |
|
5558 | 5557 | you have just pulled changes from another repository, the tip of |
|
5559 | 5558 | that repository becomes the current tip. The "tip" tag is special |
|
5560 | 5559 | and cannot be renamed or assigned to a different changeset. |
|
5561 | 5560 | |
|
5562 | 5561 | Returns 0 on success. |
|
5563 | 5562 | """ |
|
5564 | 5563 | displayer = cmdutil.show_changeset(ui, repo, opts) |
|
5565 | 5564 | displayer.show(repo[len(repo) - 1]) |
|
5566 | 5565 | displayer.close() |
|
5567 | 5566 | |
|
5568 | 5567 | @command('unbundle', |
|
5569 | 5568 | [('u', 'update', None, |
|
5570 | 5569 | _('update to new branch head if changesets were unbundled'))], |
|
5571 | 5570 | _('[-u] FILE...')) |
|
5572 | 5571 | def unbundle(ui, repo, fname1, *fnames, **opts): |
|
5573 | 5572 | """apply one or more changegroup files |
|
5574 | 5573 | |
|
5575 | 5574 | Apply one or more compressed changegroup files generated by the |
|
5576 | 5575 | bundle command. |
|
5577 | 5576 | |
|
5578 | 5577 | Returns 0 on success, 1 if an update has unresolved files. |
|
5579 | 5578 | """ |
|
5580 | 5579 | fnames = (fname1,) + fnames |
|
5581 | 5580 | |
|
5582 | 5581 | lock = repo.lock() |
|
5583 | 5582 | wc = repo['.'] |
|
5584 | 5583 | try: |
|
5585 | 5584 | for fname in fnames: |
|
5586 | 5585 | f = url.open(ui, fname) |
|
5587 | 5586 | gen = changegroup.readbundle(f, fname) |
|
5588 | 5587 | modheads = repo.addchangegroup(gen, 'unbundle', 'bundle:' + fname) |
|
5589 | 5588 | bookmarks.updatecurrentbookmark(repo, wc.node(), wc.branch()) |
|
5590 | 5589 | finally: |
|
5591 | 5590 | lock.release() |
|
5592 | 5591 | return postincoming(ui, repo, modheads, opts.get('update'), None) |
|
5593 | 5592 | |
|
5594 | 5593 | @command('^update|up|checkout|co', |
|
5595 | 5594 | [('C', 'clean', None, _('discard uncommitted changes (no backup)')), |
|
5596 | 5595 | ('c', 'check', None, |
|
5597 | 5596 | _('update across branches if no uncommitted changes')), |
|
5598 | 5597 | ('d', 'date', '', _('tipmost revision matching date'), _('DATE')), |
|
5599 | 5598 | ('r', 'rev', '', _('revision'), _('REV'))], |
|
5600 | 5599 | _('[-c] [-C] [-d DATE] [[-r] REV]')) |
|
5601 | 5600 | def update(ui, repo, node=None, rev=None, clean=False, date=None, check=False): |
|
5602 | 5601 | """update working directory (or switch revisions) |
|
5603 | 5602 | |
|
5604 | 5603 | Update the repository's working directory to the specified |
|
5605 | 5604 | changeset. If no changeset is specified, update to the tip of the |
|
5606 | 5605 | current named branch. |
|
5607 | 5606 | |
|
5608 | 5607 | If the changeset is not a descendant of the working directory's |
|
5609 | 5608 | parent, the update is aborted. With the -c/--check option, the |
|
5610 | 5609 | working directory is checked for uncommitted changes; if none are |
|
5611 | 5610 | found, the working directory is updated to the specified |
|
5612 | 5611 | changeset. |
|
5613 | 5612 | |
|
5614 | 5613 | Update sets the working directory's parent revison to the specified |
|
5615 | 5614 | changeset (see :hg:`help parents`). |
|
5616 | 5615 | |
|
5617 | 5616 | The following rules apply when the working directory contains |
|
5618 | 5617 | uncommitted changes: |
|
5619 | 5618 | |
|
5620 | 5619 | 1. If neither -c/--check nor -C/--clean is specified, and if |
|
5621 | 5620 | the requested changeset is an ancestor or descendant of |
|
5622 | 5621 | the working directory's parent, the uncommitted changes |
|
5623 | 5622 | are merged into the requested changeset and the merged |
|
5624 | 5623 | result is left uncommitted. If the requested changeset is |
|
5625 | 5624 | not an ancestor or descendant (that is, it is on another |
|
5626 | 5625 | branch), the update is aborted and the uncommitted changes |
|
5627 | 5626 | are preserved. |
|
5628 | 5627 | |
|
5629 | 5628 | 2. With the -c/--check option, the update is aborted and the |
|
5630 | 5629 | uncommitted changes are preserved. |
|
5631 | 5630 | |
|
5632 | 5631 | 3. With the -C/--clean option, uncommitted changes are discarded and |
|
5633 | 5632 | the working directory is updated to the requested changeset. |
|
5634 | 5633 | |
|
5635 | 5634 | Use null as the changeset to remove the working directory (like |
|
5636 | 5635 | :hg:`clone -U`). |
|
5637 | 5636 | |
|
5638 | 5637 | If you want to revert just one file to an older revision, use |
|
5639 | 5638 | :hg:`revert [-r REV] NAME`. |
|
5640 | 5639 | |
|
5641 | 5640 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
5642 | 5641 | |
|
5643 | 5642 | Returns 0 on success, 1 if there are unresolved files. |
|
5644 | 5643 | """ |
|
5645 | 5644 | if rev and node: |
|
5646 | 5645 | raise util.Abort(_("please specify just one revision")) |
|
5647 | 5646 | |
|
5648 | 5647 | if rev is None or rev == '': |
|
5649 | 5648 | rev = node |
|
5650 | 5649 | |
|
5651 | 5650 | # if we defined a bookmark, we have to remember the original bookmark name |
|
5652 | 5651 | brev = rev |
|
5653 | 5652 | rev = scmutil.revsingle(repo, rev, rev).rev() |
|
5654 | 5653 | |
|
5655 | 5654 | if check and clean: |
|
5656 | 5655 | raise util.Abort(_("cannot specify both -c/--check and -C/--clean")) |
|
5657 | 5656 | |
|
5658 | 5657 | if check: |
|
5659 | 5658 | # we could use dirty() but we can ignore merge and branch trivia |
|
5660 | 5659 | c = repo[None] |
|
5661 | 5660 | if c.modified() or c.added() or c.removed(): |
|
5662 | 5661 | raise util.Abort(_("uncommitted local changes")) |
|
5663 | 5662 | |
|
5664 | 5663 | if date: |
|
5665 | 5664 | if rev is not None: |
|
5666 | 5665 | raise util.Abort(_("you can't specify a revision and a date")) |
|
5667 | 5666 | rev = cmdutil.finddate(ui, repo, date) |
|
5668 | 5667 | |
|
5669 | 5668 | if clean or check: |
|
5670 | 5669 | ret = hg.clean(repo, rev) |
|
5671 | 5670 | else: |
|
5672 | 5671 | ret = hg.update(repo, rev) |
|
5673 | 5672 | |
|
5674 | 5673 | if brev in repo._bookmarks: |
|
5675 | 5674 | bookmarks.setcurrent(repo, brev) |
|
5676 | 5675 | |
|
5677 | 5676 | return ret |
|
5678 | 5677 | |
|
5679 | 5678 | @command('verify', []) |
|
5680 | 5679 | def verify(ui, repo): |
|
5681 | 5680 | """verify the integrity of the repository |
|
5682 | 5681 | |
|
5683 | 5682 | Verify the integrity of the current repository. |
|
5684 | 5683 | |
|
5685 | 5684 | This will perform an extensive check of the repository's |
|
5686 | 5685 | integrity, validating the hashes and checksums of each entry in |
|
5687 | 5686 | the changelog, manifest, and tracked files, as well as the |
|
5688 | 5687 | integrity of their crosslinks and indices. |
|
5689 | 5688 | |
|
5690 | 5689 | Returns 0 on success, 1 if errors are encountered. |
|
5691 | 5690 | """ |
|
5692 | 5691 | return hg.verify(repo) |
|
5693 | 5692 | |
|
5694 | 5693 | @command('version', []) |
|
5695 | 5694 | def version_(ui): |
|
5696 | 5695 | """output version and copyright information""" |
|
5697 | 5696 | ui.write(_("Mercurial Distributed SCM (version %s)\n") |
|
5698 | 5697 | % util.version()) |
|
5699 | 5698 | ui.status(_( |
|
5700 | 5699 | "(see http://mercurial.selenic.com for more information)\n" |
|
5701 | 5700 | "\nCopyright (C) 2005-2011 Matt Mackall and others\n" |
|
5702 | 5701 | "This is free software; see the source for copying conditions. " |
|
5703 | 5702 | "There is NO\nwarranty; " |
|
5704 | 5703 | "not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" |
|
5705 | 5704 | )) |
|
5706 | 5705 | |
|
5707 | 5706 | norepo = ("clone init version help debugcommands debugcomplete" |
|
5708 | 5707 | " debugdate debuginstall debugfsinfo debugpushkey debugwireargs" |
|
5709 | 5708 | " debugknown debuggetbundle debugbundle") |
|
5710 | 5709 | optionalrepo = ("identify paths serve showconfig debugancestor debugdag" |
|
5711 | 5710 | " debugdata debugindex debugindexdot debugrevlog") |
@@ -1,267 +1,270 b'' | |||
|
1 | 1 | # copies.py - copy detection for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2008 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 | import util |
|
9 | 9 | import heapq |
|
10 | 10 | |
|
11 | 11 | def _nonoverlap(d1, d2, d3): |
|
12 | 12 | "Return list of elements in d1 not in d2 or d3" |
|
13 | 13 | return sorted([d for d in d1 if d not in d3 and d not in d2]) |
|
14 | 14 | |
|
15 | 15 | def _dirname(f): |
|
16 | 16 | s = f.rfind("/") |
|
17 | 17 | if s == -1: |
|
18 | 18 | return "" |
|
19 | 19 | return f[:s] |
|
20 | 20 | |
|
21 | 21 | def _dirs(files): |
|
22 | 22 | d = set() |
|
23 | 23 | for f in files: |
|
24 | 24 | f = _dirname(f) |
|
25 | 25 | while f not in d: |
|
26 | 26 | d.add(f) |
|
27 | 27 | f = _dirname(f) |
|
28 | 28 | return d |
|
29 | 29 | |
|
30 | 30 | def _findlimit(repo, a, b): |
|
31 | 31 | """Find the earliest revision that's an ancestor of a or b but not both, |
|
32 | 32 | None if no such revision exists. |
|
33 | 33 | """ |
|
34 | 34 | # basic idea: |
|
35 | 35 | # - mark a and b with different sides |
|
36 | 36 | # - if a parent's children are all on the same side, the parent is |
|
37 | 37 | # on that side, otherwise it is on no side |
|
38 | 38 | # - walk the graph in topological order with the help of a heap; |
|
39 | 39 | # - add unseen parents to side map |
|
40 | 40 | # - clear side of any parent that has children on different sides |
|
41 | 41 | # - track number of interesting revs that might still be on a side |
|
42 | 42 | # - track the lowest interesting rev seen |
|
43 | 43 | # - quit when interesting revs is zero |
|
44 | 44 | |
|
45 | 45 | cl = repo.changelog |
|
46 | 46 | working = len(cl) # pseudo rev for the working directory |
|
47 | 47 | if a is None: |
|
48 | 48 | a = working |
|
49 | 49 | if b is None: |
|
50 | 50 | b = working |
|
51 | 51 | |
|
52 | 52 | side = {a: -1, b: 1} |
|
53 | 53 | visit = [-a, -b] |
|
54 | 54 | heapq.heapify(visit) |
|
55 | 55 | interesting = len(visit) |
|
56 | 56 | hascommonancestor = False |
|
57 | 57 | limit = working |
|
58 | 58 | |
|
59 | 59 | while interesting: |
|
60 | 60 | r = -heapq.heappop(visit) |
|
61 | 61 | if r == working: |
|
62 | 62 | parents = [cl.rev(p) for p in repo.dirstate.parents()] |
|
63 | 63 | else: |
|
64 | 64 | parents = cl.parentrevs(r) |
|
65 | 65 | for p in parents: |
|
66 | 66 | if p < 0: |
|
67 | 67 | continue |
|
68 | 68 | if p not in side: |
|
69 | 69 | # first time we see p; add it to visit |
|
70 | 70 | side[p] = side[r] |
|
71 | 71 | if side[p]: |
|
72 | 72 | interesting += 1 |
|
73 | 73 | heapq.heappush(visit, -p) |
|
74 | 74 | elif side[p] and side[p] != side[r]: |
|
75 | 75 | # p was interesting but now we know better |
|
76 | 76 | side[p] = 0 |
|
77 | 77 | interesting -= 1 |
|
78 | 78 | hascommonancestor = True |
|
79 | 79 | if side[r]: |
|
80 | 80 | limit = r # lowest rev visited |
|
81 | 81 | interesting -= 1 |
|
82 | 82 | |
|
83 | 83 | if not hascommonancestor: |
|
84 | 84 | return None |
|
85 | 85 | return limit |
|
86 | 86 | |
|
87 | def copies(repo, c1, c2, ca, checkdirs=False): | |
|
87 | def pathcopies(c1, c2): | |
|
88 | return mergecopies(c1._repo, c1, c2, c1._repo["null"], False)[0] | |
|
89 | ||
|
90 | def mergecopies(repo, c1, c2, ca, checkdirs=True): | |
|
88 | 91 | """ |
|
89 | 92 | Find moves and copies between context c1 and c2 |
|
90 | 93 | """ |
|
91 | 94 | # avoid silly behavior for update from empty dir |
|
92 | 95 | if not c1 or not c2 or c1 == c2: |
|
93 | 96 | return {}, {} |
|
94 | 97 | |
|
95 | 98 | # avoid silly behavior for parent -> working dir |
|
96 | 99 | if c2.node() is None and c1.node() == repo.dirstate.p1(): |
|
97 | 100 | return repo.dirstate.copies(), {} |
|
98 | 101 | |
|
99 | 102 | limit = _findlimit(repo, c1.rev(), c2.rev()) |
|
100 | 103 | if limit is None: |
|
101 | 104 | # no common ancestor, no copies |
|
102 | 105 | return {}, {} |
|
103 | 106 | m1 = c1.manifest() |
|
104 | 107 | m2 = c2.manifest() |
|
105 | 108 | ma = ca.manifest() |
|
106 | 109 | |
|
107 | 110 | def makectx(f, n): |
|
108 | 111 | if len(n) != 20: # in a working context? |
|
109 | 112 | if c1.rev() is None: |
|
110 | 113 | return c1.filectx(f) |
|
111 | 114 | return c2.filectx(f) |
|
112 | 115 | return repo.filectx(f, fileid=n) |
|
113 | 116 | |
|
114 | 117 | ctx = util.lrucachefunc(makectx) |
|
115 | 118 | copy = {} |
|
116 | 119 | fullcopy = {} |
|
117 | 120 | diverge = {} |
|
118 | 121 | |
|
119 | 122 | def related(f1, f2, limit): |
|
120 | 123 | # Walk back to common ancestor to see if the two files originate |
|
121 | 124 | # from the same file. Since workingfilectx's rev() is None it messes |
|
122 | 125 | # up the integer comparison logic, hence the pre-step check for |
|
123 | 126 | # None (f1 and f2 can only be workingfilectx's initially). |
|
124 | 127 | |
|
125 | 128 | if f1 == f2: |
|
126 | 129 | return f1 # a match |
|
127 | 130 | |
|
128 | 131 | g1, g2 = f1.ancestors(), f2.ancestors() |
|
129 | 132 | try: |
|
130 | 133 | f1r, f2r = f1.rev(), f2.rev() |
|
131 | 134 | |
|
132 | 135 | if f1r is None: |
|
133 | 136 | f1 = g1.next() |
|
134 | 137 | if f2r is None: |
|
135 | 138 | f2 = g2.next() |
|
136 | 139 | |
|
137 | 140 | while True: |
|
138 | 141 | f1r, f2r = f1.rev(), f2.rev() |
|
139 | 142 | if f1r > f2r: |
|
140 | 143 | f1 = g1.next() |
|
141 | 144 | elif f2r > f1r: |
|
142 | 145 | f2 = g2.next() |
|
143 | 146 | elif f1 == f2: |
|
144 | 147 | return f1 # a match |
|
145 | 148 | elif f1r == f2r or f1r < limit or f2r < limit: |
|
146 | 149 | return False # copy no longer relevant |
|
147 | 150 | except StopIteration: |
|
148 | 151 | return False |
|
149 | 152 | |
|
150 | 153 | def checkcopies(f, m1, m2): |
|
151 | 154 | '''check possible copies of f from m1 to m2''' |
|
152 | 155 | of = None |
|
153 | 156 | seen = set([f]) |
|
154 | 157 | for oc in ctx(f, m1[f]).ancestors(): |
|
155 | 158 | ocr = oc.rev() |
|
156 | 159 | of = oc.path() |
|
157 | 160 | if of in seen: |
|
158 | 161 | # check limit late - grab last rename before |
|
159 | 162 | if ocr < limit: |
|
160 | 163 | break |
|
161 | 164 | continue |
|
162 | 165 | seen.add(of) |
|
163 | 166 | |
|
164 | 167 | fullcopy[f] = of # remember for dir rename detection |
|
165 | 168 | if of not in m2: |
|
166 | 169 | continue # no match, keep looking |
|
167 | 170 | if m2[of] == ma.get(of): |
|
168 | 171 | break # no merge needed, quit early |
|
169 | 172 | c2 = ctx(of, m2[of]) |
|
170 | 173 | cr = related(oc, c2, ca.rev()) |
|
171 | 174 | if cr and (of == f or of == c2.path()): # non-divergent |
|
172 | 175 | copy[f] = of |
|
173 | 176 | of = None |
|
174 | 177 | break |
|
175 | 178 | |
|
176 | 179 | if of in ma: |
|
177 | 180 | diverge.setdefault(of, []).append(f) |
|
178 | 181 | |
|
179 | 182 | repo.ui.debug(" searching for copies back to rev %d\n" % limit) |
|
180 | 183 | |
|
181 | 184 | u1 = _nonoverlap(m1, m2, ma) |
|
182 | 185 | u2 = _nonoverlap(m2, m1, ma) |
|
183 | 186 | |
|
184 | 187 | if u1: |
|
185 | 188 | repo.ui.debug(" unmatched files in local:\n %s\n" |
|
186 | 189 | % "\n ".join(u1)) |
|
187 | 190 | if u2: |
|
188 | 191 | repo.ui.debug(" unmatched files in other:\n %s\n" |
|
189 | 192 | % "\n ".join(u2)) |
|
190 | 193 | |
|
191 | 194 | for f in u1: |
|
192 | 195 | checkcopies(f, m1, m2) |
|
193 | 196 | for f in u2: |
|
194 | 197 | checkcopies(f, m2, m1) |
|
195 | 198 | |
|
196 | 199 | diverge2 = set() |
|
197 | 200 | for of, fl in diverge.items(): |
|
198 | 201 | if len(fl) == 1 or of in c2: |
|
199 | 202 | del diverge[of] # not actually divergent, or not a rename |
|
200 | 203 | else: |
|
201 | 204 | diverge2.update(fl) # reverse map for below |
|
202 | 205 | |
|
203 | 206 | if fullcopy: |
|
204 | 207 | repo.ui.debug(" all copies found (* = to merge, ! = divergent):\n") |
|
205 | 208 | for f in fullcopy: |
|
206 | 209 | note = "" |
|
207 | 210 | if f in copy: |
|
208 | 211 | note += "*" |
|
209 | 212 | if f in diverge2: |
|
210 | 213 | note += "!" |
|
211 | 214 | repo.ui.debug(" %s -> %s %s\n" % (f, fullcopy[f], note)) |
|
212 | 215 | del diverge2 |
|
213 | 216 | |
|
214 | 217 | if not fullcopy or not checkdirs: |
|
215 | 218 | return copy, diverge |
|
216 | 219 | |
|
217 | 220 | repo.ui.debug(" checking for directory renames\n") |
|
218 | 221 | |
|
219 | 222 | # generate a directory move map |
|
220 | 223 | d1, d2 = _dirs(m1), _dirs(m2) |
|
221 | 224 | invalid = set() |
|
222 | 225 | dirmove = {} |
|
223 | 226 | |
|
224 | 227 | # examine each file copy for a potential directory move, which is |
|
225 | 228 | # when all the files in a directory are moved to a new directory |
|
226 | 229 | for dst, src in fullcopy.iteritems(): |
|
227 | 230 | dsrc, ddst = _dirname(src), _dirname(dst) |
|
228 | 231 | if dsrc in invalid: |
|
229 | 232 | # already seen to be uninteresting |
|
230 | 233 | continue |
|
231 | 234 | elif dsrc in d1 and ddst in d1: |
|
232 | 235 | # directory wasn't entirely moved locally |
|
233 | 236 | invalid.add(dsrc) |
|
234 | 237 | elif dsrc in d2 and ddst in d2: |
|
235 | 238 | # directory wasn't entirely moved remotely |
|
236 | 239 | invalid.add(dsrc) |
|
237 | 240 | elif dsrc in dirmove and dirmove[dsrc] != ddst: |
|
238 | 241 | # files from the same directory moved to two different places |
|
239 | 242 | invalid.add(dsrc) |
|
240 | 243 | else: |
|
241 | 244 | # looks good so far |
|
242 | 245 | dirmove[dsrc + "/"] = ddst + "/" |
|
243 | 246 | |
|
244 | 247 | for i in invalid: |
|
245 | 248 | if i in dirmove: |
|
246 | 249 | del dirmove[i] |
|
247 | 250 | del d1, d2, invalid |
|
248 | 251 | |
|
249 | 252 | if not dirmove: |
|
250 | 253 | return copy, diverge |
|
251 | 254 | |
|
252 | 255 | for d in dirmove: |
|
253 | 256 | repo.ui.debug(" dir %s -> %s\n" % (d, dirmove[d])) |
|
254 | 257 | |
|
255 | 258 | # check unaccounted nonoverlapping files against directory moves |
|
256 | 259 | for f in u1 + u2: |
|
257 | 260 | if f not in fullcopy: |
|
258 | 261 | for d in dirmove: |
|
259 | 262 | if f.startswith(d): |
|
260 | 263 | # new file added in a directory that was moved, move it |
|
261 | 264 | df = dirmove[d] + f[len(d):] |
|
262 | 265 | if df not in copy: |
|
263 | 266 | copy[f] = df |
|
264 | 267 | repo.ui.debug(" file %s -> %s\n" % (f, copy[f])) |
|
265 | 268 | break |
|
266 | 269 | |
|
267 | 270 | return copy, diverge |
@@ -1,584 +1,584 b'' | |||
|
1 | 1 | # merge.py - directory-level update/merge handling for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006, 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 nullid, nullrev, hex, bin |
|
9 | 9 | from i18n import _ |
|
10 | 10 | import scmutil, util, filemerge, copies, subrepo |
|
11 | 11 | import errno, os, shutil |
|
12 | 12 | |
|
13 | 13 | class mergestate(object): |
|
14 | 14 | '''track 3-way merge state of individual files''' |
|
15 | 15 | def __init__(self, repo): |
|
16 | 16 | self._repo = repo |
|
17 | 17 | self._dirty = False |
|
18 | 18 | self._read() |
|
19 | 19 | def reset(self, node=None): |
|
20 | 20 | self._state = {} |
|
21 | 21 | if node: |
|
22 | 22 | self._local = node |
|
23 | 23 | shutil.rmtree(self._repo.join("merge"), True) |
|
24 | 24 | self._dirty = False |
|
25 | 25 | def _read(self): |
|
26 | 26 | self._state = {} |
|
27 | 27 | try: |
|
28 | 28 | f = self._repo.opener("merge/state") |
|
29 | 29 | for i, l in enumerate(f): |
|
30 | 30 | if i == 0: |
|
31 | 31 | self._local = bin(l[:-1]) |
|
32 | 32 | else: |
|
33 | 33 | bits = l[:-1].split("\0") |
|
34 | 34 | self._state[bits[0]] = bits[1:] |
|
35 | 35 | f.close() |
|
36 | 36 | except IOError, err: |
|
37 | 37 | if err.errno != errno.ENOENT: |
|
38 | 38 | raise |
|
39 | 39 | self._dirty = False |
|
40 | 40 | def commit(self): |
|
41 | 41 | if self._dirty: |
|
42 | 42 | f = self._repo.opener("merge/state", "w") |
|
43 | 43 | f.write(hex(self._local) + "\n") |
|
44 | 44 | for d, v in self._state.iteritems(): |
|
45 | 45 | f.write("\0".join([d] + v) + "\n") |
|
46 | 46 | f.close() |
|
47 | 47 | self._dirty = False |
|
48 | 48 | def add(self, fcl, fco, fca, fd, flags): |
|
49 | 49 | hash = util.sha1(fcl.path()).hexdigest() |
|
50 | 50 | self._repo.opener.write("merge/" + hash, fcl.data()) |
|
51 | 51 | self._state[fd] = ['u', hash, fcl.path(), fca.path(), |
|
52 | 52 | hex(fca.filenode()), fco.path(), flags] |
|
53 | 53 | self._dirty = True |
|
54 | 54 | def __contains__(self, dfile): |
|
55 | 55 | return dfile in self._state |
|
56 | 56 | def __getitem__(self, dfile): |
|
57 | 57 | return self._state[dfile][0] |
|
58 | 58 | def __iter__(self): |
|
59 | 59 | l = self._state.keys() |
|
60 | 60 | l.sort() |
|
61 | 61 | for f in l: |
|
62 | 62 | yield f |
|
63 | 63 | def mark(self, dfile, state): |
|
64 | 64 | self._state[dfile][0] = state |
|
65 | 65 | self._dirty = True |
|
66 | 66 | def resolve(self, dfile, wctx, octx): |
|
67 | 67 | if self[dfile] == 'r': |
|
68 | 68 | return 0 |
|
69 | 69 | state, hash, lfile, afile, anode, ofile, flags = self._state[dfile] |
|
70 | 70 | f = self._repo.opener("merge/" + hash) |
|
71 | 71 | self._repo.wwrite(dfile, f.read(), flags) |
|
72 | 72 | f.close() |
|
73 | 73 | fcd = wctx[dfile] |
|
74 | 74 | fco = octx[ofile] |
|
75 | 75 | fca = self._repo.filectx(afile, fileid=anode) |
|
76 | 76 | r = filemerge.filemerge(self._repo, self._local, lfile, fcd, fco, fca) |
|
77 | 77 | if r is None: |
|
78 | 78 | # no real conflict |
|
79 | 79 | del self._state[dfile] |
|
80 | 80 | elif not r: |
|
81 | 81 | self.mark(dfile, 'r') |
|
82 | 82 | return r |
|
83 | 83 | |
|
84 | 84 | def _checkunknown(wctx, mctx, folding): |
|
85 | 85 | "check for collisions between unknown files and files in mctx" |
|
86 | 86 | if folding: |
|
87 | 87 | foldf = util.normcase |
|
88 | 88 | else: |
|
89 | 89 | foldf = lambda fn: fn |
|
90 | 90 | folded = {} |
|
91 | 91 | for fn in mctx: |
|
92 | 92 | folded[foldf(fn)] = fn |
|
93 | 93 | for fn in wctx.unknown(): |
|
94 | 94 | f = foldf(fn) |
|
95 | 95 | if f in folded and mctx[folded[f]].cmp(wctx[f]): |
|
96 | 96 | raise util.Abort(_("untracked file in working directory differs" |
|
97 | 97 | " from file in requested revision: '%s'") % fn) |
|
98 | 98 | |
|
99 | 99 | def _checkcollision(mctx, wctx): |
|
100 | 100 | "check for case folding collisions in the destination context" |
|
101 | 101 | folded = {} |
|
102 | 102 | for fn in mctx: |
|
103 | 103 | fold = util.normcase(fn) |
|
104 | 104 | if fold in folded: |
|
105 | 105 | raise util.Abort(_("case-folding collision between %s and %s") |
|
106 | 106 | % (fn, folded[fold])) |
|
107 | 107 | folded[fold] = fn |
|
108 | 108 | |
|
109 | 109 | if wctx: |
|
110 | 110 | for fn in wctx: |
|
111 | 111 | fold = util.normcase(fn) |
|
112 | 112 | mfn = folded.get(fold, None) |
|
113 | 113 | if mfn and (mfn != fn): |
|
114 | 114 | raise util.Abort(_("case-folding collision between %s and %s") |
|
115 | 115 | % (mfn, fn)) |
|
116 | 116 | |
|
117 | 117 | def _forgetremoved(wctx, mctx, branchmerge): |
|
118 | 118 | """ |
|
119 | 119 | Forget removed files |
|
120 | 120 | |
|
121 | 121 | If we're jumping between revisions (as opposed to merging), and if |
|
122 | 122 | neither the working directory nor the target rev has the file, |
|
123 | 123 | then we need to remove it from the dirstate, to prevent the |
|
124 | 124 | dirstate from listing the file when it is no longer in the |
|
125 | 125 | manifest. |
|
126 | 126 | |
|
127 | 127 | If we're merging, and the other revision has removed a file |
|
128 | 128 | that is not present in the working directory, we need to mark it |
|
129 | 129 | as removed. |
|
130 | 130 | """ |
|
131 | 131 | |
|
132 | 132 | action = [] |
|
133 | 133 | state = branchmerge and 'r' or 'f' |
|
134 | 134 | for f in wctx.deleted(): |
|
135 | 135 | if f not in mctx: |
|
136 | 136 | action.append((f, state)) |
|
137 | 137 | |
|
138 | 138 | if not branchmerge: |
|
139 | 139 | for f in wctx.removed(): |
|
140 | 140 | if f not in mctx: |
|
141 | 141 | action.append((f, "f")) |
|
142 | 142 | |
|
143 | 143 | return action |
|
144 | 144 | |
|
145 | 145 | def manifestmerge(repo, p1, p2, pa, overwrite, partial): |
|
146 | 146 | """ |
|
147 | 147 | Merge p1 and p2 with ancestor pa and generate merge action list |
|
148 | 148 | |
|
149 | 149 | overwrite = whether we clobber working files |
|
150 | 150 | partial = function to filter file lists |
|
151 | 151 | """ |
|
152 | 152 | |
|
153 | 153 | def fmerge(f, f2, fa): |
|
154 | 154 | """merge flags""" |
|
155 | 155 | a, m, n = ma.flags(fa), m1.flags(f), m2.flags(f2) |
|
156 | 156 | if m == n: # flags agree |
|
157 | 157 | return m # unchanged |
|
158 | 158 | if m and n and not a: # flags set, don't agree, differ from parent |
|
159 | 159 | r = repo.ui.promptchoice( |
|
160 | 160 | _(" conflicting flags for %s\n" |
|
161 | 161 | "(n)one, e(x)ec or sym(l)ink?") % f, |
|
162 | 162 | (_("&None"), _("E&xec"), _("Sym&link")), 0) |
|
163 | 163 | if r == 1: |
|
164 | 164 | return "x" # Exec |
|
165 | 165 | if r == 2: |
|
166 | 166 | return "l" # Symlink |
|
167 | 167 | return "" |
|
168 | 168 | if m and m != a: # changed from a to m |
|
169 | 169 | return m |
|
170 | 170 | if n and n != a: # changed from a to n |
|
171 | 171 | return n |
|
172 | 172 | return '' # flag was cleared |
|
173 | 173 | |
|
174 | 174 | def act(msg, m, f, *args): |
|
175 | 175 | repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m)) |
|
176 | 176 | action.append((f, m) + args) |
|
177 | 177 | |
|
178 | 178 | action, copy = [], {} |
|
179 | 179 | |
|
180 | 180 | if overwrite: |
|
181 | 181 | pa = p1 |
|
182 | 182 | elif pa == p2: # backwards |
|
183 | 183 | pa = p1.p1() |
|
184 | 184 | elif pa and repo.ui.configbool("merge", "followcopies", True): |
|
185 | 185 | dirs = repo.ui.configbool("merge", "followdirs", True) |
|
186 | copy, diverge = copies.copies(repo, p1, p2, pa, dirs) | |
|
186 | copy, diverge = copies.mergecopies(repo, p1, p2, pa, dirs) | |
|
187 | 187 | for of, fl in diverge.iteritems(): |
|
188 | 188 | act("divergent renames", "dr", of, fl) |
|
189 | 189 | |
|
190 | 190 | repo.ui.note(_("resolving manifests\n")) |
|
191 | 191 | repo.ui.debug(" overwrite: %s, partial: %s\n" |
|
192 | 192 | % (bool(overwrite), bool(partial))) |
|
193 | 193 | repo.ui.debug(" ancestor: %s, local: %s, remote: %s\n" % (pa, p1, p2)) |
|
194 | 194 | |
|
195 | 195 | m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest() |
|
196 | 196 | copied = set(copy.values()) |
|
197 | 197 | |
|
198 | 198 | if '.hgsubstate' in m1: |
|
199 | 199 | # check whether sub state is modified |
|
200 | 200 | for s in p1.substate: |
|
201 | 201 | if p1.sub(s).dirty(): |
|
202 | 202 | m1['.hgsubstate'] += "+" |
|
203 | 203 | break |
|
204 | 204 | |
|
205 | 205 | # Compare manifests |
|
206 | 206 | for f, n in m1.iteritems(): |
|
207 | 207 | if partial and not partial(f): |
|
208 | 208 | continue |
|
209 | 209 | if f in m2: |
|
210 | 210 | rflags = fmerge(f, f, f) |
|
211 | 211 | a = ma.get(f, nullid) |
|
212 | 212 | if n == m2[f] or m2[f] == a: # same or local newer |
|
213 | 213 | # is file locally modified or flags need changing? |
|
214 | 214 | # dirstate flags may need to be made current |
|
215 | 215 | if m1.flags(f) != rflags or n[20:]: |
|
216 | 216 | act("update permissions", "e", f, rflags) |
|
217 | 217 | elif n == a: # remote newer |
|
218 | 218 | act("remote is newer", "g", f, rflags) |
|
219 | 219 | else: # both changed |
|
220 | 220 | act("versions differ", "m", f, f, f, rflags, False) |
|
221 | 221 | elif f in copied: # files we'll deal with on m2 side |
|
222 | 222 | pass |
|
223 | 223 | elif f in copy: |
|
224 | 224 | f2 = copy[f] |
|
225 | 225 | if f2 not in m2: # directory rename |
|
226 | 226 | act("remote renamed directory to " + f2, "d", |
|
227 | 227 | f, None, f2, m1.flags(f)) |
|
228 | 228 | else: # case 2 A,B/B/B or case 4,21 A/B/B |
|
229 | 229 | act("local copied/moved to " + f2, "m", |
|
230 | 230 | f, f2, f, fmerge(f, f2, f2), False) |
|
231 | 231 | elif f in ma: # clean, a different, no remote |
|
232 | 232 | if n != ma[f]: |
|
233 | 233 | if repo.ui.promptchoice( |
|
234 | 234 | _(" local changed %s which remote deleted\n" |
|
235 | 235 | "use (c)hanged version or (d)elete?") % f, |
|
236 | 236 | (_("&Changed"), _("&Delete")), 0): |
|
237 | 237 | act("prompt delete", "r", f) |
|
238 | 238 | else: |
|
239 | 239 | act("prompt keep", "a", f) |
|
240 | 240 | elif n[20:] == "a": # added, no remote |
|
241 | 241 | act("remote deleted", "f", f) |
|
242 | 242 | elif n[20:] != "u": |
|
243 | 243 | act("other deleted", "r", f) |
|
244 | 244 | |
|
245 | 245 | for f, n in m2.iteritems(): |
|
246 | 246 | if partial and not partial(f): |
|
247 | 247 | continue |
|
248 | 248 | if f in m1 or f in copied: # files already visited |
|
249 | 249 | continue |
|
250 | 250 | if f in copy: |
|
251 | 251 | f2 = copy[f] |
|
252 | 252 | if f2 not in m1: # directory rename |
|
253 | 253 | act("local renamed directory to " + f2, "d", |
|
254 | 254 | None, f, f2, m2.flags(f)) |
|
255 | 255 | elif f2 in m2: # rename case 1, A/A,B/A |
|
256 | 256 | act("remote copied to " + f, "m", |
|
257 | 257 | f2, f, f, fmerge(f2, f, f2), False) |
|
258 | 258 | else: # case 3,20 A/B/A |
|
259 | 259 | act("remote moved to " + f, "m", |
|
260 | 260 | f2, f, f, fmerge(f2, f, f2), True) |
|
261 | 261 | elif f not in ma: |
|
262 | 262 | act("remote created", "g", f, m2.flags(f)) |
|
263 | 263 | elif n != ma[f]: |
|
264 | 264 | if repo.ui.promptchoice( |
|
265 | 265 | _("remote changed %s which local deleted\n" |
|
266 | 266 | "use (c)hanged version or leave (d)eleted?") % f, |
|
267 | 267 | (_("&Changed"), _("&Deleted")), 0) == 0: |
|
268 | 268 | act("prompt recreating", "g", f, m2.flags(f)) |
|
269 | 269 | |
|
270 | 270 | return action |
|
271 | 271 | |
|
272 | 272 | def actionkey(a): |
|
273 | 273 | return a[1] == 'r' and -1 or 0, a |
|
274 | 274 | |
|
275 | 275 | def applyupdates(repo, action, wctx, mctx, actx, overwrite): |
|
276 | 276 | """apply the merge action list to the working directory |
|
277 | 277 | |
|
278 | 278 | wctx is the working copy context |
|
279 | 279 | mctx is the context to be merged into the working copy |
|
280 | 280 | actx is the context of the common ancestor |
|
281 | 281 | |
|
282 | 282 | Return a tuple of counts (updated, merged, removed, unresolved) that |
|
283 | 283 | describes how many files were affected by the update. |
|
284 | 284 | """ |
|
285 | 285 | |
|
286 | 286 | updated, merged, removed, unresolved = 0, 0, 0, 0 |
|
287 | 287 | ms = mergestate(repo) |
|
288 | 288 | ms.reset(wctx.p1().node()) |
|
289 | 289 | moves = [] |
|
290 | 290 | action.sort(key=actionkey) |
|
291 | 291 | |
|
292 | 292 | # prescan for merges |
|
293 | 293 | for a in action: |
|
294 | 294 | f, m = a[:2] |
|
295 | 295 | if m == 'm': # merge |
|
296 | 296 | f2, fd, flags, move = a[2:] |
|
297 | 297 | if f == '.hgsubstate': # merged internally |
|
298 | 298 | continue |
|
299 | 299 | repo.ui.debug("preserving %s for resolve of %s\n" % (f, fd)) |
|
300 | 300 | fcl = wctx[f] |
|
301 | 301 | fco = mctx[f2] |
|
302 | 302 | if mctx == actx: # backwards, use working dir parent as ancestor |
|
303 | 303 | if fcl.parents(): |
|
304 | 304 | fca = fcl.p1() |
|
305 | 305 | else: |
|
306 | 306 | fca = repo.filectx(f, fileid=nullrev) |
|
307 | 307 | else: |
|
308 | 308 | fca = fcl.ancestor(fco, actx) |
|
309 | 309 | if not fca: |
|
310 | 310 | fca = repo.filectx(f, fileid=nullrev) |
|
311 | 311 | ms.add(fcl, fco, fca, fd, flags) |
|
312 | 312 | if f != fd and move: |
|
313 | 313 | moves.append(f) |
|
314 | 314 | |
|
315 | 315 | audit = scmutil.pathauditor(repo.root) |
|
316 | 316 | |
|
317 | 317 | # remove renamed files after safely stored |
|
318 | 318 | for f in moves: |
|
319 | 319 | if os.path.lexists(repo.wjoin(f)): |
|
320 | 320 | repo.ui.debug("removing %s\n" % f) |
|
321 | 321 | audit(f) |
|
322 | 322 | os.unlink(repo.wjoin(f)) |
|
323 | 323 | |
|
324 | 324 | numupdates = len(action) |
|
325 | 325 | for i, a in enumerate(action): |
|
326 | 326 | f, m = a[:2] |
|
327 | 327 | repo.ui.progress(_('updating'), i + 1, item=f, total=numupdates, |
|
328 | 328 | unit=_('files')) |
|
329 | 329 | if f and f[0] == "/": |
|
330 | 330 | continue |
|
331 | 331 | if m == "r": # remove |
|
332 | 332 | repo.ui.note(_("removing %s\n") % f) |
|
333 | 333 | audit(f) |
|
334 | 334 | if f == '.hgsubstate': # subrepo states need updating |
|
335 | 335 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) |
|
336 | 336 | try: |
|
337 | 337 | util.unlinkpath(repo.wjoin(f)) |
|
338 | 338 | except OSError, inst: |
|
339 | 339 | if inst.errno != errno.ENOENT: |
|
340 | 340 | repo.ui.warn(_("update failed to remove %s: %s!\n") % |
|
341 | 341 | (f, inst.strerror)) |
|
342 | 342 | removed += 1 |
|
343 | 343 | elif m == "m": # merge |
|
344 | 344 | if f == '.hgsubstate': # subrepo states need updating |
|
345 | 345 | subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx), overwrite) |
|
346 | 346 | continue |
|
347 | 347 | f2, fd, flags, move = a[2:] |
|
348 | 348 | repo.wopener.audit(fd) |
|
349 | 349 | r = ms.resolve(fd, wctx, mctx) |
|
350 | 350 | if r is not None and r > 0: |
|
351 | 351 | unresolved += 1 |
|
352 | 352 | else: |
|
353 | 353 | if r is None: |
|
354 | 354 | updated += 1 |
|
355 | 355 | else: |
|
356 | 356 | merged += 1 |
|
357 | 357 | util.setflags(repo.wjoin(fd), 'l' in flags, 'x' in flags) |
|
358 | 358 | if (move and repo.dirstate.normalize(fd) != f |
|
359 | 359 | and os.path.lexists(repo.wjoin(f))): |
|
360 | 360 | repo.ui.debug("removing %s\n" % f) |
|
361 | 361 | audit(f) |
|
362 | 362 | os.unlink(repo.wjoin(f)) |
|
363 | 363 | elif m == "g": # get |
|
364 | 364 | flags = a[2] |
|
365 | 365 | repo.ui.note(_("getting %s\n") % f) |
|
366 | 366 | t = mctx.filectx(f).data() |
|
367 | 367 | repo.wwrite(f, t, flags) |
|
368 | 368 | t = None |
|
369 | 369 | updated += 1 |
|
370 | 370 | if f == '.hgsubstate': # subrepo states need updating |
|
371 | 371 | subrepo.submerge(repo, wctx, mctx, wctx, overwrite) |
|
372 | 372 | elif m == "d": # directory rename |
|
373 | 373 | f2, fd, flags = a[2:] |
|
374 | 374 | if f: |
|
375 | 375 | repo.ui.note(_("moving %s to %s\n") % (f, fd)) |
|
376 | 376 | audit(f) |
|
377 | 377 | t = wctx.filectx(f).data() |
|
378 | 378 | repo.wwrite(fd, t, flags) |
|
379 | 379 | util.unlinkpath(repo.wjoin(f)) |
|
380 | 380 | if f2: |
|
381 | 381 | repo.ui.note(_("getting %s to %s\n") % (f2, fd)) |
|
382 | 382 | t = mctx.filectx(f2).data() |
|
383 | 383 | repo.wwrite(fd, t, flags) |
|
384 | 384 | updated += 1 |
|
385 | 385 | elif m == "dr": # divergent renames |
|
386 | 386 | fl = a[2] |
|
387 | 387 | repo.ui.warn(_("note: possible conflict - %s was renamed " |
|
388 | 388 | "multiple times to:\n") % f) |
|
389 | 389 | for nf in fl: |
|
390 | 390 | repo.ui.warn(" %s\n" % nf) |
|
391 | 391 | elif m == "e": # exec |
|
392 | 392 | flags = a[2] |
|
393 | 393 | repo.wopener.audit(f) |
|
394 | 394 | util.setflags(repo.wjoin(f), 'l' in flags, 'x' in flags) |
|
395 | 395 | ms.commit() |
|
396 | 396 | repo.ui.progress(_('updating'), None, total=numupdates, unit=_('files')) |
|
397 | 397 | |
|
398 | 398 | return updated, merged, removed, unresolved |
|
399 | 399 | |
|
400 | 400 | def recordupdates(repo, action, branchmerge): |
|
401 | 401 | "record merge actions to the dirstate" |
|
402 | 402 | |
|
403 | 403 | for a in action: |
|
404 | 404 | f, m = a[:2] |
|
405 | 405 | if m == "r": # remove |
|
406 | 406 | if branchmerge: |
|
407 | 407 | repo.dirstate.remove(f) |
|
408 | 408 | else: |
|
409 | 409 | repo.dirstate.drop(f) |
|
410 | 410 | elif m == "a": # re-add |
|
411 | 411 | if not branchmerge: |
|
412 | 412 | repo.dirstate.add(f) |
|
413 | 413 | elif m == "f": # forget |
|
414 | 414 | repo.dirstate.drop(f) |
|
415 | 415 | elif m == "e": # exec change |
|
416 | 416 | repo.dirstate.normallookup(f) |
|
417 | 417 | elif m == "g": # get |
|
418 | 418 | if branchmerge: |
|
419 | 419 | repo.dirstate.otherparent(f) |
|
420 | 420 | else: |
|
421 | 421 | repo.dirstate.normal(f) |
|
422 | 422 | elif m == "m": # merge |
|
423 | 423 | f2, fd, flag, move = a[2:] |
|
424 | 424 | if branchmerge: |
|
425 | 425 | # We've done a branch merge, mark this file as merged |
|
426 | 426 | # so that we properly record the merger later |
|
427 | 427 | repo.dirstate.merge(fd) |
|
428 | 428 | if f != f2: # copy/rename |
|
429 | 429 | if move: |
|
430 | 430 | repo.dirstate.remove(f) |
|
431 | 431 | if f != fd: |
|
432 | 432 | repo.dirstate.copy(f, fd) |
|
433 | 433 | else: |
|
434 | 434 | repo.dirstate.copy(f2, fd) |
|
435 | 435 | else: |
|
436 | 436 | # We've update-merged a locally modified file, so |
|
437 | 437 | # we set the dirstate to emulate a normal checkout |
|
438 | 438 | # of that file some time in the past. Thus our |
|
439 | 439 | # merge will appear as a normal local file |
|
440 | 440 | # modification. |
|
441 | 441 | if f2 == fd: # file not locally copied/moved |
|
442 | 442 | repo.dirstate.normallookup(fd) |
|
443 | 443 | if move: |
|
444 | 444 | repo.dirstate.drop(f) |
|
445 | 445 | elif m == "d": # directory rename |
|
446 | 446 | f2, fd, flag = a[2:] |
|
447 | 447 | if not f2 and f not in repo.dirstate: |
|
448 | 448 | # untracked file moved |
|
449 | 449 | continue |
|
450 | 450 | if branchmerge: |
|
451 | 451 | repo.dirstate.add(fd) |
|
452 | 452 | if f: |
|
453 | 453 | repo.dirstate.remove(f) |
|
454 | 454 | repo.dirstate.copy(f, fd) |
|
455 | 455 | if f2: |
|
456 | 456 | repo.dirstate.copy(f2, fd) |
|
457 | 457 | else: |
|
458 | 458 | repo.dirstate.normal(fd) |
|
459 | 459 | if f: |
|
460 | 460 | repo.dirstate.drop(f) |
|
461 | 461 | |
|
462 | 462 | def update(repo, node, branchmerge, force, partial, ancestor=None): |
|
463 | 463 | """ |
|
464 | 464 | Perform a merge between the working directory and the given node |
|
465 | 465 | |
|
466 | 466 | node = the node to update to, or None if unspecified |
|
467 | 467 | branchmerge = whether to merge between branches |
|
468 | 468 | force = whether to force branch merging or file overwriting |
|
469 | 469 | partial = a function to filter file lists (dirstate not updated) |
|
470 | 470 | |
|
471 | 471 | The table below shows all the behaviors of the update command |
|
472 | 472 | given the -c and -C or no options, whether the working directory |
|
473 | 473 | is dirty, whether a revision is specified, and the relationship of |
|
474 | 474 | the parent rev to the target rev (linear, on the same named |
|
475 | 475 | branch, or on another named branch). |
|
476 | 476 | |
|
477 | 477 | This logic is tested by test-update-branches.t. |
|
478 | 478 | |
|
479 | 479 | -c -C dirty rev | linear same cross |
|
480 | 480 | n n n n | ok (1) x |
|
481 | 481 | n n n y | ok ok ok |
|
482 | 482 | n n y * | merge (2) (2) |
|
483 | 483 | n y * * | --- discard --- |
|
484 | 484 | y n y * | --- (3) --- |
|
485 | 485 | y n n * | --- ok --- |
|
486 | 486 | y y * * | --- (4) --- |
|
487 | 487 | |
|
488 | 488 | x = can't happen |
|
489 | 489 | * = don't-care |
|
490 | 490 | 1 = abort: crosses branches (use 'hg merge' or 'hg update -c') |
|
491 | 491 | 2 = abort: crosses branches (use 'hg merge' to merge or |
|
492 | 492 | use 'hg update -C' to discard changes) |
|
493 | 493 | 3 = abort: uncommitted local changes |
|
494 | 494 | 4 = incompatible options (checked in commands.py) |
|
495 | 495 | |
|
496 | 496 | Return the same tuple as applyupdates(). |
|
497 | 497 | """ |
|
498 | 498 | |
|
499 | 499 | onode = node |
|
500 | 500 | wlock = repo.wlock() |
|
501 | 501 | try: |
|
502 | 502 | wc = repo[None] |
|
503 | 503 | if node is None: |
|
504 | 504 | # tip of current branch |
|
505 | 505 | try: |
|
506 | 506 | node = repo.branchtags()[wc.branch()] |
|
507 | 507 | except KeyError: |
|
508 | 508 | if wc.branch() == "default": # no default branch! |
|
509 | 509 | node = repo.lookup("tip") # update to tip |
|
510 | 510 | else: |
|
511 | 511 | raise util.Abort(_("branch %s not found") % wc.branch()) |
|
512 | 512 | overwrite = force and not branchmerge |
|
513 | 513 | pl = wc.parents() |
|
514 | 514 | p1, p2 = pl[0], repo[node] |
|
515 | 515 | if ancestor: |
|
516 | 516 | pa = repo[ancestor] |
|
517 | 517 | else: |
|
518 | 518 | pa = p1.ancestor(p2) |
|
519 | 519 | |
|
520 | 520 | fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2) |
|
521 | 521 | |
|
522 | 522 | ### check phase |
|
523 | 523 | if not overwrite and len(pl) > 1: |
|
524 | 524 | raise util.Abort(_("outstanding uncommitted merges")) |
|
525 | 525 | if branchmerge: |
|
526 | 526 | if pa == p2: |
|
527 | 527 | raise util.Abort(_("merging with a working directory ancestor" |
|
528 | 528 | " has no effect")) |
|
529 | 529 | elif pa == p1: |
|
530 | 530 | if p1.branch() == p2.branch(): |
|
531 | 531 | raise util.Abort(_("nothing to merge"), |
|
532 | 532 | hint=_("use 'hg update' " |
|
533 | 533 | "or check 'hg heads'")) |
|
534 | 534 | if not force and (wc.files() or wc.deleted()): |
|
535 | 535 | raise util.Abort(_("outstanding uncommitted changes"), |
|
536 | 536 | hint=_("use 'hg status' to list changes")) |
|
537 | 537 | for s in wc.substate: |
|
538 | 538 | if wc.sub(s).dirty(): |
|
539 | 539 | raise util.Abort(_("outstanding uncommitted changes in " |
|
540 | 540 | "subrepository '%s'") % s) |
|
541 | 541 | |
|
542 | 542 | elif not overwrite: |
|
543 | 543 | if pa == p1 or pa == p2: # linear |
|
544 | 544 | pass # all good |
|
545 | 545 | elif wc.dirty(missing=True): |
|
546 | 546 | raise util.Abort(_("crosses branches (merge branches or use" |
|
547 | 547 | " --clean to discard changes)")) |
|
548 | 548 | elif onode is None: |
|
549 | 549 | raise util.Abort(_("crosses branches (merge branches or update" |
|
550 | 550 | " --check to force update)")) |
|
551 | 551 | else: |
|
552 | 552 | # Allow jumping branches if clean and specific rev given |
|
553 | 553 | overwrite = True |
|
554 | 554 | |
|
555 | 555 | ### calculate phase |
|
556 | 556 | action = [] |
|
557 | 557 | wc.status(unknown=True) # prime cache |
|
558 | 558 | folding = not util.checkcase(repo.path) |
|
559 | 559 | if not force: |
|
560 | 560 | _checkunknown(wc, p2, folding) |
|
561 | 561 | if folding: |
|
562 | 562 | _checkcollision(p2, branchmerge and p1) |
|
563 | 563 | action += _forgetremoved(wc, p2, branchmerge) |
|
564 | 564 | action += manifestmerge(repo, wc, p2, pa, overwrite, partial) |
|
565 | 565 | |
|
566 | 566 | ### apply phase |
|
567 | 567 | if not branchmerge: # just jump to the new rev |
|
568 | 568 | fp1, fp2, xp1, xp2 = fp2, nullid, xp2, '' |
|
569 | 569 | if not partial: |
|
570 | 570 | repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2) |
|
571 | 571 | |
|
572 | 572 | stats = applyupdates(repo, action, wc, p2, pa, overwrite) |
|
573 | 573 | |
|
574 | 574 | if not partial: |
|
575 | 575 | repo.dirstate.setparents(fp1, fp2) |
|
576 | 576 | recordupdates(repo, action, branchmerge) |
|
577 | 577 | if not branchmerge: |
|
578 | 578 | repo.dirstate.setbranch(p2.branch()) |
|
579 | 579 | finally: |
|
580 | 580 | wlock.release() |
|
581 | 581 | |
|
582 | 582 | if not partial: |
|
583 | 583 | repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3]) |
|
584 | 584 | return stats |
@@ -1,1869 +1,1869 b'' | |||
|
1 | 1 | # patch.py - patch file parsing routines |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 | 9 | import cStringIO, email.Parser, os, errno, re |
|
10 | 10 | import tempfile, zlib, shutil |
|
11 | 11 | |
|
12 | 12 | from i18n import _ |
|
13 | 13 | from node import hex, nullid, short |
|
14 | 14 | import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error |
|
15 | 15 | import context |
|
16 | 16 | |
|
17 | 17 | gitre = re.compile('diff --git a/(.*) b/(.*)') |
|
18 | 18 | |
|
19 | 19 | class PatchError(Exception): |
|
20 | 20 | pass |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | # public functions |
|
24 | 24 | |
|
25 | 25 | def split(stream): |
|
26 | 26 | '''return an iterator of individual patches from a stream''' |
|
27 | 27 | def isheader(line, inheader): |
|
28 | 28 | if inheader and line[0] in (' ', '\t'): |
|
29 | 29 | # continuation |
|
30 | 30 | return True |
|
31 | 31 | if line[0] in (' ', '-', '+'): |
|
32 | 32 | # diff line - don't check for header pattern in there |
|
33 | 33 | return False |
|
34 | 34 | l = line.split(': ', 1) |
|
35 | 35 | return len(l) == 2 and ' ' not in l[0] |
|
36 | 36 | |
|
37 | 37 | def chunk(lines): |
|
38 | 38 | return cStringIO.StringIO(''.join(lines)) |
|
39 | 39 | |
|
40 | 40 | def hgsplit(stream, cur): |
|
41 | 41 | inheader = True |
|
42 | 42 | |
|
43 | 43 | for line in stream: |
|
44 | 44 | if not line.strip(): |
|
45 | 45 | inheader = False |
|
46 | 46 | if not inheader and line.startswith('# HG changeset patch'): |
|
47 | 47 | yield chunk(cur) |
|
48 | 48 | cur = [] |
|
49 | 49 | inheader = True |
|
50 | 50 | |
|
51 | 51 | cur.append(line) |
|
52 | 52 | |
|
53 | 53 | if cur: |
|
54 | 54 | yield chunk(cur) |
|
55 | 55 | |
|
56 | 56 | def mboxsplit(stream, cur): |
|
57 | 57 | for line in stream: |
|
58 | 58 | if line.startswith('From '): |
|
59 | 59 | for c in split(chunk(cur[1:])): |
|
60 | 60 | yield c |
|
61 | 61 | cur = [] |
|
62 | 62 | |
|
63 | 63 | cur.append(line) |
|
64 | 64 | |
|
65 | 65 | if cur: |
|
66 | 66 | for c in split(chunk(cur[1:])): |
|
67 | 67 | yield c |
|
68 | 68 | |
|
69 | 69 | def mimesplit(stream, cur): |
|
70 | 70 | def msgfp(m): |
|
71 | 71 | fp = cStringIO.StringIO() |
|
72 | 72 | g = email.Generator.Generator(fp, mangle_from_=False) |
|
73 | 73 | g.flatten(m) |
|
74 | 74 | fp.seek(0) |
|
75 | 75 | return fp |
|
76 | 76 | |
|
77 | 77 | for line in stream: |
|
78 | 78 | cur.append(line) |
|
79 | 79 | c = chunk(cur) |
|
80 | 80 | |
|
81 | 81 | m = email.Parser.Parser().parse(c) |
|
82 | 82 | if not m.is_multipart(): |
|
83 | 83 | yield msgfp(m) |
|
84 | 84 | else: |
|
85 | 85 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
86 | 86 | for part in m.walk(): |
|
87 | 87 | ct = part.get_content_type() |
|
88 | 88 | if ct not in ok_types: |
|
89 | 89 | continue |
|
90 | 90 | yield msgfp(part) |
|
91 | 91 | |
|
92 | 92 | def headersplit(stream, cur): |
|
93 | 93 | inheader = False |
|
94 | 94 | |
|
95 | 95 | for line in stream: |
|
96 | 96 | if not inheader and isheader(line, inheader): |
|
97 | 97 | yield chunk(cur) |
|
98 | 98 | cur = [] |
|
99 | 99 | inheader = True |
|
100 | 100 | if inheader and not isheader(line, inheader): |
|
101 | 101 | inheader = False |
|
102 | 102 | |
|
103 | 103 | cur.append(line) |
|
104 | 104 | |
|
105 | 105 | if cur: |
|
106 | 106 | yield chunk(cur) |
|
107 | 107 | |
|
108 | 108 | def remainder(cur): |
|
109 | 109 | yield chunk(cur) |
|
110 | 110 | |
|
111 | 111 | class fiter(object): |
|
112 | 112 | def __init__(self, fp): |
|
113 | 113 | self.fp = fp |
|
114 | 114 | |
|
115 | 115 | def __iter__(self): |
|
116 | 116 | return self |
|
117 | 117 | |
|
118 | 118 | def next(self): |
|
119 | 119 | l = self.fp.readline() |
|
120 | 120 | if not l: |
|
121 | 121 | raise StopIteration |
|
122 | 122 | return l |
|
123 | 123 | |
|
124 | 124 | inheader = False |
|
125 | 125 | cur = [] |
|
126 | 126 | |
|
127 | 127 | mimeheaders = ['content-type'] |
|
128 | 128 | |
|
129 | 129 | if not util.safehasattr(stream, 'next'): |
|
130 | 130 | # http responses, for example, have readline but not next |
|
131 | 131 | stream = fiter(stream) |
|
132 | 132 | |
|
133 | 133 | for line in stream: |
|
134 | 134 | cur.append(line) |
|
135 | 135 | if line.startswith('# HG changeset patch'): |
|
136 | 136 | return hgsplit(stream, cur) |
|
137 | 137 | elif line.startswith('From '): |
|
138 | 138 | return mboxsplit(stream, cur) |
|
139 | 139 | elif isheader(line, inheader): |
|
140 | 140 | inheader = True |
|
141 | 141 | if line.split(':', 1)[0].lower() in mimeheaders: |
|
142 | 142 | # let email parser handle this |
|
143 | 143 | return mimesplit(stream, cur) |
|
144 | 144 | elif line.startswith('--- ') and inheader: |
|
145 | 145 | # No evil headers seen by diff start, split by hand |
|
146 | 146 | return headersplit(stream, cur) |
|
147 | 147 | # Not enough info, keep reading |
|
148 | 148 | |
|
149 | 149 | # if we are here, we have a very plain patch |
|
150 | 150 | return remainder(cur) |
|
151 | 151 | |
|
152 | 152 | def extract(ui, fileobj): |
|
153 | 153 | '''extract patch from data read from fileobj. |
|
154 | 154 | |
|
155 | 155 | patch can be a normal patch or contained in an email message. |
|
156 | 156 | |
|
157 | 157 | return tuple (filename, message, user, date, branch, node, p1, p2). |
|
158 | 158 | Any item in the returned tuple can be None. If filename is None, |
|
159 | 159 | fileobj did not contain a patch. Caller must unlink filename when done.''' |
|
160 | 160 | |
|
161 | 161 | # attempt to detect the start of a patch |
|
162 | 162 | # (this heuristic is borrowed from quilt) |
|
163 | 163 | diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' |
|
164 | 164 | r'retrieving revision [0-9]+(\.[0-9]+)*$|' |
|
165 | 165 | r'---[ \t].*?^\+\+\+[ \t]|' |
|
166 | 166 | r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL) |
|
167 | 167 | |
|
168 | 168 | fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') |
|
169 | 169 | tmpfp = os.fdopen(fd, 'w') |
|
170 | 170 | try: |
|
171 | 171 | msg = email.Parser.Parser().parse(fileobj) |
|
172 | 172 | |
|
173 | 173 | subject = msg['Subject'] |
|
174 | 174 | user = msg['From'] |
|
175 | 175 | if not subject and not user: |
|
176 | 176 | # Not an email, restore parsed headers if any |
|
177 | 177 | subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n' |
|
178 | 178 | |
|
179 | 179 | gitsendmail = 'git-send-email' in msg.get('X-Mailer', '') |
|
180 | 180 | # should try to parse msg['Date'] |
|
181 | 181 | date = None |
|
182 | 182 | nodeid = None |
|
183 | 183 | branch = None |
|
184 | 184 | parents = [] |
|
185 | 185 | |
|
186 | 186 | if subject: |
|
187 | 187 | if subject.startswith('[PATCH'): |
|
188 | 188 | pend = subject.find(']') |
|
189 | 189 | if pend >= 0: |
|
190 | 190 | subject = subject[pend + 1:].lstrip() |
|
191 | 191 | subject = re.sub(r'\n[ \t]+', ' ', subject) |
|
192 | 192 | ui.debug('Subject: %s\n' % subject) |
|
193 | 193 | if user: |
|
194 | 194 | ui.debug('From: %s\n' % user) |
|
195 | 195 | diffs_seen = 0 |
|
196 | 196 | ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') |
|
197 | 197 | message = '' |
|
198 | 198 | for part in msg.walk(): |
|
199 | 199 | content_type = part.get_content_type() |
|
200 | 200 | ui.debug('Content-Type: %s\n' % content_type) |
|
201 | 201 | if content_type not in ok_types: |
|
202 | 202 | continue |
|
203 | 203 | payload = part.get_payload(decode=True) |
|
204 | 204 | m = diffre.search(payload) |
|
205 | 205 | if m: |
|
206 | 206 | hgpatch = False |
|
207 | 207 | hgpatchheader = False |
|
208 | 208 | ignoretext = False |
|
209 | 209 | |
|
210 | 210 | ui.debug('found patch at byte %d\n' % m.start(0)) |
|
211 | 211 | diffs_seen += 1 |
|
212 | 212 | cfp = cStringIO.StringIO() |
|
213 | 213 | for line in payload[:m.start(0)].splitlines(): |
|
214 | 214 | if line.startswith('# HG changeset patch') and not hgpatch: |
|
215 | 215 | ui.debug('patch generated by hg export\n') |
|
216 | 216 | hgpatch = True |
|
217 | 217 | hgpatchheader = True |
|
218 | 218 | # drop earlier commit message content |
|
219 | 219 | cfp.seek(0) |
|
220 | 220 | cfp.truncate() |
|
221 | 221 | subject = None |
|
222 | 222 | elif hgpatchheader: |
|
223 | 223 | if line.startswith('# User '): |
|
224 | 224 | user = line[7:] |
|
225 | 225 | ui.debug('From: %s\n' % user) |
|
226 | 226 | elif line.startswith("# Date "): |
|
227 | 227 | date = line[7:] |
|
228 | 228 | elif line.startswith("# Branch "): |
|
229 | 229 | branch = line[9:] |
|
230 | 230 | elif line.startswith("# Node ID "): |
|
231 | 231 | nodeid = line[10:] |
|
232 | 232 | elif line.startswith("# Parent "): |
|
233 | 233 | parents.append(line[10:]) |
|
234 | 234 | elif not line.startswith("# "): |
|
235 | 235 | hgpatchheader = False |
|
236 | 236 | elif line == '---' and gitsendmail: |
|
237 | 237 | ignoretext = True |
|
238 | 238 | if not hgpatchheader and not ignoretext: |
|
239 | 239 | cfp.write(line) |
|
240 | 240 | cfp.write('\n') |
|
241 | 241 | message = cfp.getvalue() |
|
242 | 242 | if tmpfp: |
|
243 | 243 | tmpfp.write(payload) |
|
244 | 244 | if not payload.endswith('\n'): |
|
245 | 245 | tmpfp.write('\n') |
|
246 | 246 | elif not diffs_seen and message and content_type == 'text/plain': |
|
247 | 247 | message += '\n' + payload |
|
248 | 248 | except: |
|
249 | 249 | tmpfp.close() |
|
250 | 250 | os.unlink(tmpname) |
|
251 | 251 | raise |
|
252 | 252 | |
|
253 | 253 | if subject and not message.startswith(subject): |
|
254 | 254 | message = '%s\n%s' % (subject, message) |
|
255 | 255 | tmpfp.close() |
|
256 | 256 | if not diffs_seen: |
|
257 | 257 | os.unlink(tmpname) |
|
258 | 258 | return None, message, user, date, branch, None, None, None |
|
259 | 259 | p1 = parents and parents.pop(0) or None |
|
260 | 260 | p2 = parents and parents.pop(0) or None |
|
261 | 261 | return tmpname, message, user, date, branch, nodeid, p1, p2 |
|
262 | 262 | |
|
263 | 263 | class patchmeta(object): |
|
264 | 264 | """Patched file metadata |
|
265 | 265 | |
|
266 | 266 | 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY |
|
267 | 267 | or COPY. 'path' is patched file path. 'oldpath' is set to the |
|
268 | 268 | origin file when 'op' is either COPY or RENAME, None otherwise. If |
|
269 | 269 | file mode is changed, 'mode' is a tuple (islink, isexec) where |
|
270 | 270 | 'islink' is True if the file is a symlink and 'isexec' is True if |
|
271 | 271 | the file is executable. Otherwise, 'mode' is None. |
|
272 | 272 | """ |
|
273 | 273 | def __init__(self, path): |
|
274 | 274 | self.path = path |
|
275 | 275 | self.oldpath = None |
|
276 | 276 | self.mode = None |
|
277 | 277 | self.op = 'MODIFY' |
|
278 | 278 | self.binary = False |
|
279 | 279 | |
|
280 | 280 | def setmode(self, mode): |
|
281 | 281 | islink = mode & 020000 |
|
282 | 282 | isexec = mode & 0100 |
|
283 | 283 | self.mode = (islink, isexec) |
|
284 | 284 | |
|
285 | 285 | def copy(self): |
|
286 | 286 | other = patchmeta(self.path) |
|
287 | 287 | other.oldpath = self.oldpath |
|
288 | 288 | other.mode = self.mode |
|
289 | 289 | other.op = self.op |
|
290 | 290 | other.binary = self.binary |
|
291 | 291 | return other |
|
292 | 292 | |
|
293 | 293 | def __repr__(self): |
|
294 | 294 | return "<patchmeta %s %r>" % (self.op, self.path) |
|
295 | 295 | |
|
296 | 296 | def readgitpatch(lr): |
|
297 | 297 | """extract git-style metadata about patches from <patchname>""" |
|
298 | 298 | |
|
299 | 299 | # Filter patch for git information |
|
300 | 300 | gp = None |
|
301 | 301 | gitpatches = [] |
|
302 | 302 | for line in lr: |
|
303 | 303 | line = line.rstrip(' \r\n') |
|
304 | 304 | if line.startswith('diff --git'): |
|
305 | 305 | m = gitre.match(line) |
|
306 | 306 | if m: |
|
307 | 307 | if gp: |
|
308 | 308 | gitpatches.append(gp) |
|
309 | 309 | dst = m.group(2) |
|
310 | 310 | gp = patchmeta(dst) |
|
311 | 311 | elif gp: |
|
312 | 312 | if line.startswith('--- '): |
|
313 | 313 | gitpatches.append(gp) |
|
314 | 314 | gp = None |
|
315 | 315 | continue |
|
316 | 316 | if line.startswith('rename from '): |
|
317 | 317 | gp.op = 'RENAME' |
|
318 | 318 | gp.oldpath = line[12:] |
|
319 | 319 | elif line.startswith('rename to '): |
|
320 | 320 | gp.path = line[10:] |
|
321 | 321 | elif line.startswith('copy from '): |
|
322 | 322 | gp.op = 'COPY' |
|
323 | 323 | gp.oldpath = line[10:] |
|
324 | 324 | elif line.startswith('copy to '): |
|
325 | 325 | gp.path = line[8:] |
|
326 | 326 | elif line.startswith('deleted file'): |
|
327 | 327 | gp.op = 'DELETE' |
|
328 | 328 | elif line.startswith('new file mode '): |
|
329 | 329 | gp.op = 'ADD' |
|
330 | 330 | gp.setmode(int(line[-6:], 8)) |
|
331 | 331 | elif line.startswith('new mode '): |
|
332 | 332 | gp.setmode(int(line[-6:], 8)) |
|
333 | 333 | elif line.startswith('GIT binary patch'): |
|
334 | 334 | gp.binary = True |
|
335 | 335 | if gp: |
|
336 | 336 | gitpatches.append(gp) |
|
337 | 337 | |
|
338 | 338 | return gitpatches |
|
339 | 339 | |
|
340 | 340 | class linereader(object): |
|
341 | 341 | # simple class to allow pushing lines back into the input stream |
|
342 | 342 | def __init__(self, fp): |
|
343 | 343 | self.fp = fp |
|
344 | 344 | self.buf = [] |
|
345 | 345 | |
|
346 | 346 | def push(self, line): |
|
347 | 347 | if line is not None: |
|
348 | 348 | self.buf.append(line) |
|
349 | 349 | |
|
350 | 350 | def readline(self): |
|
351 | 351 | if self.buf: |
|
352 | 352 | l = self.buf[0] |
|
353 | 353 | del self.buf[0] |
|
354 | 354 | return l |
|
355 | 355 | return self.fp.readline() |
|
356 | 356 | |
|
357 | 357 | def __iter__(self): |
|
358 | 358 | while True: |
|
359 | 359 | l = self.readline() |
|
360 | 360 | if not l: |
|
361 | 361 | break |
|
362 | 362 | yield l |
|
363 | 363 | |
|
364 | 364 | class abstractbackend(object): |
|
365 | 365 | def __init__(self, ui): |
|
366 | 366 | self.ui = ui |
|
367 | 367 | |
|
368 | 368 | def getfile(self, fname): |
|
369 | 369 | """Return target file data and flags as a (data, (islink, |
|
370 | 370 | isexec)) tuple. |
|
371 | 371 | """ |
|
372 | 372 | raise NotImplementedError |
|
373 | 373 | |
|
374 | 374 | def setfile(self, fname, data, mode, copysource): |
|
375 | 375 | """Write data to target file fname and set its mode. mode is a |
|
376 | 376 | (islink, isexec) tuple. If data is None, the file content should |
|
377 | 377 | be left unchanged. If the file is modified after being copied, |
|
378 | 378 | copysource is set to the original file name. |
|
379 | 379 | """ |
|
380 | 380 | raise NotImplementedError |
|
381 | 381 | |
|
382 | 382 | def unlink(self, fname): |
|
383 | 383 | """Unlink target file.""" |
|
384 | 384 | raise NotImplementedError |
|
385 | 385 | |
|
386 | 386 | def writerej(self, fname, failed, total, lines): |
|
387 | 387 | """Write rejected lines for fname. total is the number of hunks |
|
388 | 388 | which failed to apply and total the total number of hunks for this |
|
389 | 389 | files. |
|
390 | 390 | """ |
|
391 | 391 | pass |
|
392 | 392 | |
|
393 | 393 | def exists(self, fname): |
|
394 | 394 | raise NotImplementedError |
|
395 | 395 | |
|
396 | 396 | class fsbackend(abstractbackend): |
|
397 | 397 | def __init__(self, ui, basedir): |
|
398 | 398 | super(fsbackend, self).__init__(ui) |
|
399 | 399 | self.opener = scmutil.opener(basedir) |
|
400 | 400 | |
|
401 | 401 | def _join(self, f): |
|
402 | 402 | return os.path.join(self.opener.base, f) |
|
403 | 403 | |
|
404 | 404 | def getfile(self, fname): |
|
405 | 405 | path = self._join(fname) |
|
406 | 406 | if os.path.islink(path): |
|
407 | 407 | return (os.readlink(path), (True, False)) |
|
408 | 408 | isexec = False |
|
409 | 409 | try: |
|
410 | 410 | isexec = os.lstat(path).st_mode & 0100 != 0 |
|
411 | 411 | except OSError, e: |
|
412 | 412 | if e.errno != errno.ENOENT: |
|
413 | 413 | raise |
|
414 | 414 | return (self.opener.read(fname), (False, isexec)) |
|
415 | 415 | |
|
416 | 416 | def setfile(self, fname, data, mode, copysource): |
|
417 | 417 | islink, isexec = mode |
|
418 | 418 | if data is None: |
|
419 | 419 | util.setflags(self._join(fname), islink, isexec) |
|
420 | 420 | return |
|
421 | 421 | if islink: |
|
422 | 422 | self.opener.symlink(data, fname) |
|
423 | 423 | else: |
|
424 | 424 | self.opener.write(fname, data) |
|
425 | 425 | if isexec: |
|
426 | 426 | util.setflags(self._join(fname), False, True) |
|
427 | 427 | |
|
428 | 428 | def unlink(self, fname): |
|
429 | 429 | try: |
|
430 | 430 | util.unlinkpath(self._join(fname)) |
|
431 | 431 | except OSError, inst: |
|
432 | 432 | if inst.errno != errno.ENOENT: |
|
433 | 433 | raise |
|
434 | 434 | |
|
435 | 435 | def writerej(self, fname, failed, total, lines): |
|
436 | 436 | fname = fname + ".rej" |
|
437 | 437 | self.ui.warn( |
|
438 | 438 | _("%d out of %d hunks FAILED -- saving rejects to file %s\n") % |
|
439 | 439 | (failed, total, fname)) |
|
440 | 440 | fp = self.opener(fname, 'w') |
|
441 | 441 | fp.writelines(lines) |
|
442 | 442 | fp.close() |
|
443 | 443 | |
|
444 | 444 | def exists(self, fname): |
|
445 | 445 | return os.path.lexists(self._join(fname)) |
|
446 | 446 | |
|
447 | 447 | class workingbackend(fsbackend): |
|
448 | 448 | def __init__(self, ui, repo, similarity): |
|
449 | 449 | super(workingbackend, self).__init__(ui, repo.root) |
|
450 | 450 | self.repo = repo |
|
451 | 451 | self.similarity = similarity |
|
452 | 452 | self.removed = set() |
|
453 | 453 | self.changed = set() |
|
454 | 454 | self.copied = [] |
|
455 | 455 | |
|
456 | 456 | def _checkknown(self, fname): |
|
457 | 457 | if self.repo.dirstate[fname] == '?' and self.exists(fname): |
|
458 | 458 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) |
|
459 | 459 | |
|
460 | 460 | def setfile(self, fname, data, mode, copysource): |
|
461 | 461 | self._checkknown(fname) |
|
462 | 462 | super(workingbackend, self).setfile(fname, data, mode, copysource) |
|
463 | 463 | if copysource is not None: |
|
464 | 464 | self.copied.append((copysource, fname)) |
|
465 | 465 | self.changed.add(fname) |
|
466 | 466 | |
|
467 | 467 | def unlink(self, fname): |
|
468 | 468 | self._checkknown(fname) |
|
469 | 469 | super(workingbackend, self).unlink(fname) |
|
470 | 470 | self.removed.add(fname) |
|
471 | 471 | self.changed.add(fname) |
|
472 | 472 | |
|
473 | 473 | def close(self): |
|
474 | 474 | wctx = self.repo[None] |
|
475 | 475 | addremoved = set(self.changed) |
|
476 | 476 | for src, dst in self.copied: |
|
477 | 477 | scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) |
|
478 | 478 | addremoved.discard(src) |
|
479 | 479 | if (not self.similarity) and self.removed: |
|
480 | 480 | wctx.forget(sorted(self.removed)) |
|
481 | 481 | if addremoved: |
|
482 | 482 | cwd = self.repo.getcwd() |
|
483 | 483 | if cwd: |
|
484 | 484 | addremoved = [util.pathto(self.repo.root, cwd, f) |
|
485 | 485 | for f in addremoved] |
|
486 | 486 | scmutil.addremove(self.repo, addremoved, similarity=self.similarity) |
|
487 | 487 | return sorted(self.changed) |
|
488 | 488 | |
|
489 | 489 | class filestore(object): |
|
490 | 490 | def __init__(self, maxsize=None): |
|
491 | 491 | self.opener = None |
|
492 | 492 | self.files = {} |
|
493 | 493 | self.created = 0 |
|
494 | 494 | self.maxsize = maxsize |
|
495 | 495 | if self.maxsize is None: |
|
496 | 496 | self.maxsize = 4*(2**20) |
|
497 | 497 | self.size = 0 |
|
498 | 498 | self.data = {} |
|
499 | 499 | |
|
500 | 500 | def setfile(self, fname, data, mode, copied=None): |
|
501 | 501 | if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize: |
|
502 | 502 | self.data[fname] = (data, mode, copied) |
|
503 | 503 | self.size += len(data) |
|
504 | 504 | else: |
|
505 | 505 | if self.opener is None: |
|
506 | 506 | root = tempfile.mkdtemp(prefix='hg-patch-') |
|
507 | 507 | self.opener = scmutil.opener(root) |
|
508 | 508 | # Avoid filename issues with these simple names |
|
509 | 509 | fn = str(self.created) |
|
510 | 510 | self.opener.write(fn, data) |
|
511 | 511 | self.created += 1 |
|
512 | 512 | self.files[fname] = (fn, mode, copied) |
|
513 | 513 | |
|
514 | 514 | def getfile(self, fname): |
|
515 | 515 | if fname in self.data: |
|
516 | 516 | return self.data[fname] |
|
517 | 517 | if not self.opener or fname not in self.files: |
|
518 | 518 | raise IOError() |
|
519 | 519 | fn, mode, copied = self.files[fname] |
|
520 | 520 | return self.opener.read(fn), mode, copied |
|
521 | 521 | |
|
522 | 522 | def close(self): |
|
523 | 523 | if self.opener: |
|
524 | 524 | shutil.rmtree(self.opener.base) |
|
525 | 525 | |
|
526 | 526 | class repobackend(abstractbackend): |
|
527 | 527 | def __init__(self, ui, repo, ctx, store): |
|
528 | 528 | super(repobackend, self).__init__(ui) |
|
529 | 529 | self.repo = repo |
|
530 | 530 | self.ctx = ctx |
|
531 | 531 | self.store = store |
|
532 | 532 | self.changed = set() |
|
533 | 533 | self.removed = set() |
|
534 | 534 | self.copied = {} |
|
535 | 535 | |
|
536 | 536 | def _checkknown(self, fname): |
|
537 | 537 | if fname not in self.ctx: |
|
538 | 538 | raise PatchError(_('cannot patch %s: file is not tracked') % fname) |
|
539 | 539 | |
|
540 | 540 | def getfile(self, fname): |
|
541 | 541 | try: |
|
542 | 542 | fctx = self.ctx[fname] |
|
543 | 543 | except error.LookupError: |
|
544 | 544 | raise IOError() |
|
545 | 545 | flags = fctx.flags() |
|
546 | 546 | return fctx.data(), ('l' in flags, 'x' in flags) |
|
547 | 547 | |
|
548 | 548 | def setfile(self, fname, data, mode, copysource): |
|
549 | 549 | if copysource: |
|
550 | 550 | self._checkknown(copysource) |
|
551 | 551 | if data is None: |
|
552 | 552 | data = self.ctx[fname].data() |
|
553 | 553 | self.store.setfile(fname, data, mode, copysource) |
|
554 | 554 | self.changed.add(fname) |
|
555 | 555 | if copysource: |
|
556 | 556 | self.copied[fname] = copysource |
|
557 | 557 | |
|
558 | 558 | def unlink(self, fname): |
|
559 | 559 | self._checkknown(fname) |
|
560 | 560 | self.removed.add(fname) |
|
561 | 561 | |
|
562 | 562 | def exists(self, fname): |
|
563 | 563 | return fname in self.ctx |
|
564 | 564 | |
|
565 | 565 | def close(self): |
|
566 | 566 | return self.changed | self.removed |
|
567 | 567 | |
|
568 | 568 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
|
569 | 569 | unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') |
|
570 | 570 | contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') |
|
571 | 571 | eolmodes = ['strict', 'crlf', 'lf', 'auto'] |
|
572 | 572 | |
|
573 | 573 | class patchfile(object): |
|
574 | 574 | def __init__(self, ui, gp, backend, store, eolmode='strict'): |
|
575 | 575 | self.fname = gp.path |
|
576 | 576 | self.eolmode = eolmode |
|
577 | 577 | self.eol = None |
|
578 | 578 | self.backend = backend |
|
579 | 579 | self.ui = ui |
|
580 | 580 | self.lines = [] |
|
581 | 581 | self.exists = False |
|
582 | 582 | self.missing = True |
|
583 | 583 | self.mode = gp.mode |
|
584 | 584 | self.copysource = gp.oldpath |
|
585 | 585 | self.create = gp.op in ('ADD', 'COPY', 'RENAME') |
|
586 | 586 | self.remove = gp.op == 'DELETE' |
|
587 | 587 | try: |
|
588 | 588 | if self.copysource is None: |
|
589 | 589 | data, mode = backend.getfile(self.fname) |
|
590 | 590 | self.exists = True |
|
591 | 591 | else: |
|
592 | 592 | data, mode = store.getfile(self.copysource)[:2] |
|
593 | 593 | self.exists = backend.exists(self.fname) |
|
594 | 594 | self.missing = False |
|
595 | 595 | if data: |
|
596 | 596 | self.lines = mdiff.splitnewlines(data) |
|
597 | 597 | if self.mode is None: |
|
598 | 598 | self.mode = mode |
|
599 | 599 | if self.lines: |
|
600 | 600 | # Normalize line endings |
|
601 | 601 | if self.lines[0].endswith('\r\n'): |
|
602 | 602 | self.eol = '\r\n' |
|
603 | 603 | elif self.lines[0].endswith('\n'): |
|
604 | 604 | self.eol = '\n' |
|
605 | 605 | if eolmode != 'strict': |
|
606 | 606 | nlines = [] |
|
607 | 607 | for l in self.lines: |
|
608 | 608 | if l.endswith('\r\n'): |
|
609 | 609 | l = l[:-2] + '\n' |
|
610 | 610 | nlines.append(l) |
|
611 | 611 | self.lines = nlines |
|
612 | 612 | except IOError: |
|
613 | 613 | if self.create: |
|
614 | 614 | self.missing = False |
|
615 | 615 | if self.mode is None: |
|
616 | 616 | self.mode = (False, False) |
|
617 | 617 | if self.missing: |
|
618 | 618 | self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) |
|
619 | 619 | |
|
620 | 620 | self.hash = {} |
|
621 | 621 | self.dirty = 0 |
|
622 | 622 | self.offset = 0 |
|
623 | 623 | self.skew = 0 |
|
624 | 624 | self.rej = [] |
|
625 | 625 | self.fileprinted = False |
|
626 | 626 | self.printfile(False) |
|
627 | 627 | self.hunks = 0 |
|
628 | 628 | |
|
629 | 629 | def writelines(self, fname, lines, mode): |
|
630 | 630 | if self.eolmode == 'auto': |
|
631 | 631 | eol = self.eol |
|
632 | 632 | elif self.eolmode == 'crlf': |
|
633 | 633 | eol = '\r\n' |
|
634 | 634 | else: |
|
635 | 635 | eol = '\n' |
|
636 | 636 | |
|
637 | 637 | if self.eolmode != 'strict' and eol and eol != '\n': |
|
638 | 638 | rawlines = [] |
|
639 | 639 | for l in lines: |
|
640 | 640 | if l and l[-1] == '\n': |
|
641 | 641 | l = l[:-1] + eol |
|
642 | 642 | rawlines.append(l) |
|
643 | 643 | lines = rawlines |
|
644 | 644 | |
|
645 | 645 | self.backend.setfile(fname, ''.join(lines), mode, self.copysource) |
|
646 | 646 | |
|
647 | 647 | def printfile(self, warn): |
|
648 | 648 | if self.fileprinted: |
|
649 | 649 | return |
|
650 | 650 | if warn or self.ui.verbose: |
|
651 | 651 | self.fileprinted = True |
|
652 | 652 | s = _("patching file %s\n") % self.fname |
|
653 | 653 | if warn: |
|
654 | 654 | self.ui.warn(s) |
|
655 | 655 | else: |
|
656 | 656 | self.ui.note(s) |
|
657 | 657 | |
|
658 | 658 | |
|
659 | 659 | def findlines(self, l, linenum): |
|
660 | 660 | # looks through the hash and finds candidate lines. The |
|
661 | 661 | # result is a list of line numbers sorted based on distance |
|
662 | 662 | # from linenum |
|
663 | 663 | |
|
664 | 664 | cand = self.hash.get(l, []) |
|
665 | 665 | if len(cand) > 1: |
|
666 | 666 | # resort our list of potentials forward then back. |
|
667 | 667 | cand.sort(key=lambda x: abs(x - linenum)) |
|
668 | 668 | return cand |
|
669 | 669 | |
|
670 | 670 | def write_rej(self): |
|
671 | 671 | # our rejects are a little different from patch(1). This always |
|
672 | 672 | # creates rejects in the same form as the original patch. A file |
|
673 | 673 | # header is inserted so that you can run the reject through patch again |
|
674 | 674 | # without having to type the filename. |
|
675 | 675 | if not self.rej: |
|
676 | 676 | return |
|
677 | 677 | base = os.path.basename(self.fname) |
|
678 | 678 | lines = ["--- %s\n+++ %s\n" % (base, base)] |
|
679 | 679 | for x in self.rej: |
|
680 | 680 | for l in x.hunk: |
|
681 | 681 | lines.append(l) |
|
682 | 682 | if l[-1] != '\n': |
|
683 | 683 | lines.append("\n\ No newline at end of file\n") |
|
684 | 684 | self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) |
|
685 | 685 | |
|
686 | 686 | def apply(self, h): |
|
687 | 687 | if not h.complete(): |
|
688 | 688 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % |
|
689 | 689 | (h.number, h.desc, len(h.a), h.lena, len(h.b), |
|
690 | 690 | h.lenb)) |
|
691 | 691 | |
|
692 | 692 | self.hunks += 1 |
|
693 | 693 | |
|
694 | 694 | if self.missing: |
|
695 | 695 | self.rej.append(h) |
|
696 | 696 | return -1 |
|
697 | 697 | |
|
698 | 698 | if self.exists and self.create: |
|
699 | 699 | if self.copysource: |
|
700 | 700 | self.ui.warn(_("cannot create %s: destination already " |
|
701 | 701 | "exists\n" % self.fname)) |
|
702 | 702 | else: |
|
703 | 703 | self.ui.warn(_("file %s already exists\n") % self.fname) |
|
704 | 704 | self.rej.append(h) |
|
705 | 705 | return -1 |
|
706 | 706 | |
|
707 | 707 | if isinstance(h, binhunk): |
|
708 | 708 | if self.remove: |
|
709 | 709 | self.backend.unlink(self.fname) |
|
710 | 710 | else: |
|
711 | 711 | self.lines[:] = h.new() |
|
712 | 712 | self.offset += len(h.new()) |
|
713 | 713 | self.dirty = True |
|
714 | 714 | return 0 |
|
715 | 715 | |
|
716 | 716 | horig = h |
|
717 | 717 | if (self.eolmode in ('crlf', 'lf') |
|
718 | 718 | or self.eolmode == 'auto' and self.eol): |
|
719 | 719 | # If new eols are going to be normalized, then normalize |
|
720 | 720 | # hunk data before patching. Otherwise, preserve input |
|
721 | 721 | # line-endings. |
|
722 | 722 | h = h.getnormalized() |
|
723 | 723 | |
|
724 | 724 | # fast case first, no offsets, no fuzz |
|
725 | 725 | old = h.old() |
|
726 | 726 | start = h.starta + self.offset |
|
727 | 727 | # zero length hunk ranges already have their start decremented |
|
728 | 728 | if h.lena: |
|
729 | 729 | start -= 1 |
|
730 | 730 | orig_start = start |
|
731 | 731 | # if there's skew we want to emit the "(offset %d lines)" even |
|
732 | 732 | # when the hunk cleanly applies at start + skew, so skip the |
|
733 | 733 | # fast case code |
|
734 | 734 | if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0: |
|
735 | 735 | if self.remove: |
|
736 | 736 | self.backend.unlink(self.fname) |
|
737 | 737 | else: |
|
738 | 738 | self.lines[start : start + h.lena] = h.new() |
|
739 | 739 | self.offset += h.lenb - h.lena |
|
740 | 740 | self.dirty = True |
|
741 | 741 | return 0 |
|
742 | 742 | |
|
743 | 743 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it |
|
744 | 744 | self.hash = {} |
|
745 | 745 | for x, s in enumerate(self.lines): |
|
746 | 746 | self.hash.setdefault(s, []).append(x) |
|
747 | 747 | if h.hunk[-1][0] != ' ': |
|
748 | 748 | # if the hunk tried to put something at the bottom of the file |
|
749 | 749 | # override the start line and use eof here |
|
750 | 750 | search_start = len(self.lines) |
|
751 | 751 | else: |
|
752 | 752 | search_start = orig_start + self.skew |
|
753 | 753 | |
|
754 | 754 | for fuzzlen in xrange(3): |
|
755 | 755 | for toponly in [True, False]: |
|
756 | 756 | old = h.old(fuzzlen, toponly) |
|
757 | 757 | |
|
758 | 758 | cand = self.findlines(old[0][1:], search_start) |
|
759 | 759 | for l in cand: |
|
760 | 760 | if diffhelpers.testhunk(old, self.lines, l) == 0: |
|
761 | 761 | newlines = h.new(fuzzlen, toponly) |
|
762 | 762 | self.lines[l : l + len(old)] = newlines |
|
763 | 763 | self.offset += len(newlines) - len(old) |
|
764 | 764 | self.skew = l - orig_start |
|
765 | 765 | self.dirty = True |
|
766 | 766 | offset = l - orig_start - fuzzlen |
|
767 | 767 | if fuzzlen: |
|
768 | 768 | msg = _("Hunk #%d succeeded at %d " |
|
769 | 769 | "with fuzz %d " |
|
770 | 770 | "(offset %d lines).\n") |
|
771 | 771 | self.printfile(True) |
|
772 | 772 | self.ui.warn(msg % |
|
773 | 773 | (h.number, l + 1, fuzzlen, offset)) |
|
774 | 774 | else: |
|
775 | 775 | msg = _("Hunk #%d succeeded at %d " |
|
776 | 776 | "(offset %d lines).\n") |
|
777 | 777 | self.ui.note(msg % (h.number, l + 1, offset)) |
|
778 | 778 | return fuzzlen |
|
779 | 779 | self.printfile(True) |
|
780 | 780 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) |
|
781 | 781 | self.rej.append(horig) |
|
782 | 782 | return -1 |
|
783 | 783 | |
|
784 | 784 | def close(self): |
|
785 | 785 | if self.dirty: |
|
786 | 786 | self.writelines(self.fname, self.lines, self.mode) |
|
787 | 787 | self.write_rej() |
|
788 | 788 | return len(self.rej) |
|
789 | 789 | |
|
790 | 790 | class hunk(object): |
|
791 | 791 | def __init__(self, desc, num, lr, context): |
|
792 | 792 | self.number = num |
|
793 | 793 | self.desc = desc |
|
794 | 794 | self.hunk = [desc] |
|
795 | 795 | self.a = [] |
|
796 | 796 | self.b = [] |
|
797 | 797 | self.starta = self.lena = None |
|
798 | 798 | self.startb = self.lenb = None |
|
799 | 799 | if lr is not None: |
|
800 | 800 | if context: |
|
801 | 801 | self.read_context_hunk(lr) |
|
802 | 802 | else: |
|
803 | 803 | self.read_unified_hunk(lr) |
|
804 | 804 | |
|
805 | 805 | def getnormalized(self): |
|
806 | 806 | """Return a copy with line endings normalized to LF.""" |
|
807 | 807 | |
|
808 | 808 | def normalize(lines): |
|
809 | 809 | nlines = [] |
|
810 | 810 | for line in lines: |
|
811 | 811 | if line.endswith('\r\n'): |
|
812 | 812 | line = line[:-2] + '\n' |
|
813 | 813 | nlines.append(line) |
|
814 | 814 | return nlines |
|
815 | 815 | |
|
816 | 816 | # Dummy object, it is rebuilt manually |
|
817 | 817 | nh = hunk(self.desc, self.number, None, None) |
|
818 | 818 | nh.number = self.number |
|
819 | 819 | nh.desc = self.desc |
|
820 | 820 | nh.hunk = self.hunk |
|
821 | 821 | nh.a = normalize(self.a) |
|
822 | 822 | nh.b = normalize(self.b) |
|
823 | 823 | nh.starta = self.starta |
|
824 | 824 | nh.startb = self.startb |
|
825 | 825 | nh.lena = self.lena |
|
826 | 826 | nh.lenb = self.lenb |
|
827 | 827 | return nh |
|
828 | 828 | |
|
829 | 829 | def read_unified_hunk(self, lr): |
|
830 | 830 | m = unidesc.match(self.desc) |
|
831 | 831 | if not m: |
|
832 | 832 | raise PatchError(_("bad hunk #%d") % self.number) |
|
833 | 833 | self.starta, self.lena, self.startb, self.lenb = m.groups() |
|
834 | 834 | if self.lena is None: |
|
835 | 835 | self.lena = 1 |
|
836 | 836 | else: |
|
837 | 837 | self.lena = int(self.lena) |
|
838 | 838 | if self.lenb is None: |
|
839 | 839 | self.lenb = 1 |
|
840 | 840 | else: |
|
841 | 841 | self.lenb = int(self.lenb) |
|
842 | 842 | self.starta = int(self.starta) |
|
843 | 843 | self.startb = int(self.startb) |
|
844 | 844 | diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b) |
|
845 | 845 | # if we hit eof before finishing out the hunk, the last line will |
|
846 | 846 | # be zero length. Lets try to fix it up. |
|
847 | 847 | while len(self.hunk[-1]) == 0: |
|
848 | 848 | del self.hunk[-1] |
|
849 | 849 | del self.a[-1] |
|
850 | 850 | del self.b[-1] |
|
851 | 851 | self.lena -= 1 |
|
852 | 852 | self.lenb -= 1 |
|
853 | 853 | self._fixnewline(lr) |
|
854 | 854 | |
|
855 | 855 | def read_context_hunk(self, lr): |
|
856 | 856 | self.desc = lr.readline() |
|
857 | 857 | m = contextdesc.match(self.desc) |
|
858 | 858 | if not m: |
|
859 | 859 | raise PatchError(_("bad hunk #%d") % self.number) |
|
860 | 860 | self.starta, aend = m.groups() |
|
861 | 861 | self.starta = int(self.starta) |
|
862 | 862 | if aend is None: |
|
863 | 863 | aend = self.starta |
|
864 | 864 | self.lena = int(aend) - self.starta |
|
865 | 865 | if self.starta: |
|
866 | 866 | self.lena += 1 |
|
867 | 867 | for x in xrange(self.lena): |
|
868 | 868 | l = lr.readline() |
|
869 | 869 | if l.startswith('---'): |
|
870 | 870 | # lines addition, old block is empty |
|
871 | 871 | lr.push(l) |
|
872 | 872 | break |
|
873 | 873 | s = l[2:] |
|
874 | 874 | if l.startswith('- ') or l.startswith('! '): |
|
875 | 875 | u = '-' + s |
|
876 | 876 | elif l.startswith(' '): |
|
877 | 877 | u = ' ' + s |
|
878 | 878 | else: |
|
879 | 879 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
880 | 880 | (self.number, x)) |
|
881 | 881 | self.a.append(u) |
|
882 | 882 | self.hunk.append(u) |
|
883 | 883 | |
|
884 | 884 | l = lr.readline() |
|
885 | 885 | if l.startswith('\ '): |
|
886 | 886 | s = self.a[-1][:-1] |
|
887 | 887 | self.a[-1] = s |
|
888 | 888 | self.hunk[-1] = s |
|
889 | 889 | l = lr.readline() |
|
890 | 890 | m = contextdesc.match(l) |
|
891 | 891 | if not m: |
|
892 | 892 | raise PatchError(_("bad hunk #%d") % self.number) |
|
893 | 893 | self.startb, bend = m.groups() |
|
894 | 894 | self.startb = int(self.startb) |
|
895 | 895 | if bend is None: |
|
896 | 896 | bend = self.startb |
|
897 | 897 | self.lenb = int(bend) - self.startb |
|
898 | 898 | if self.startb: |
|
899 | 899 | self.lenb += 1 |
|
900 | 900 | hunki = 1 |
|
901 | 901 | for x in xrange(self.lenb): |
|
902 | 902 | l = lr.readline() |
|
903 | 903 | if l.startswith('\ '): |
|
904 | 904 | # XXX: the only way to hit this is with an invalid line range. |
|
905 | 905 | # The no-eol marker is not counted in the line range, but I |
|
906 | 906 | # guess there are diff(1) out there which behave differently. |
|
907 | 907 | s = self.b[-1][:-1] |
|
908 | 908 | self.b[-1] = s |
|
909 | 909 | self.hunk[hunki - 1] = s |
|
910 | 910 | continue |
|
911 | 911 | if not l: |
|
912 | 912 | # line deletions, new block is empty and we hit EOF |
|
913 | 913 | lr.push(l) |
|
914 | 914 | break |
|
915 | 915 | s = l[2:] |
|
916 | 916 | if l.startswith('+ ') or l.startswith('! '): |
|
917 | 917 | u = '+' + s |
|
918 | 918 | elif l.startswith(' '): |
|
919 | 919 | u = ' ' + s |
|
920 | 920 | elif len(self.b) == 0: |
|
921 | 921 | # line deletions, new block is empty |
|
922 | 922 | lr.push(l) |
|
923 | 923 | break |
|
924 | 924 | else: |
|
925 | 925 | raise PatchError(_("bad hunk #%d old text line %d") % |
|
926 | 926 | (self.number, x)) |
|
927 | 927 | self.b.append(s) |
|
928 | 928 | while True: |
|
929 | 929 | if hunki >= len(self.hunk): |
|
930 | 930 | h = "" |
|
931 | 931 | else: |
|
932 | 932 | h = self.hunk[hunki] |
|
933 | 933 | hunki += 1 |
|
934 | 934 | if h == u: |
|
935 | 935 | break |
|
936 | 936 | elif h.startswith('-'): |
|
937 | 937 | continue |
|
938 | 938 | else: |
|
939 | 939 | self.hunk.insert(hunki - 1, u) |
|
940 | 940 | break |
|
941 | 941 | |
|
942 | 942 | if not self.a: |
|
943 | 943 | # this happens when lines were only added to the hunk |
|
944 | 944 | for x in self.hunk: |
|
945 | 945 | if x.startswith('-') or x.startswith(' '): |
|
946 | 946 | self.a.append(x) |
|
947 | 947 | if not self.b: |
|
948 | 948 | # this happens when lines were only deleted from the hunk |
|
949 | 949 | for x in self.hunk: |
|
950 | 950 | if x.startswith('+') or x.startswith(' '): |
|
951 | 951 | self.b.append(x[1:]) |
|
952 | 952 | # @@ -start,len +start,len @@ |
|
953 | 953 | self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, |
|
954 | 954 | self.startb, self.lenb) |
|
955 | 955 | self.hunk[0] = self.desc |
|
956 | 956 | self._fixnewline(lr) |
|
957 | 957 | |
|
958 | 958 | def _fixnewline(self, lr): |
|
959 | 959 | l = lr.readline() |
|
960 | 960 | if l.startswith('\ '): |
|
961 | 961 | diffhelpers.fix_newline(self.hunk, self.a, self.b) |
|
962 | 962 | else: |
|
963 | 963 | lr.push(l) |
|
964 | 964 | |
|
965 | 965 | def complete(self): |
|
966 | 966 | return len(self.a) == self.lena and len(self.b) == self.lenb |
|
967 | 967 | |
|
968 | 968 | def fuzzit(self, l, fuzz, toponly): |
|
969 | 969 | # this removes context lines from the top and bottom of list 'l'. It |
|
970 | 970 | # checks the hunk to make sure only context lines are removed, and then |
|
971 | 971 | # returns a new shortened list of lines. |
|
972 | 972 | fuzz = min(fuzz, len(l)-1) |
|
973 | 973 | if fuzz: |
|
974 | 974 | top = 0 |
|
975 | 975 | bot = 0 |
|
976 | 976 | hlen = len(self.hunk) |
|
977 | 977 | for x in xrange(hlen - 1): |
|
978 | 978 | # the hunk starts with the @@ line, so use x+1 |
|
979 | 979 | if self.hunk[x + 1][0] == ' ': |
|
980 | 980 | top += 1 |
|
981 | 981 | else: |
|
982 | 982 | break |
|
983 | 983 | if not toponly: |
|
984 | 984 | for x in xrange(hlen - 1): |
|
985 | 985 | if self.hunk[hlen - bot - 1][0] == ' ': |
|
986 | 986 | bot += 1 |
|
987 | 987 | else: |
|
988 | 988 | break |
|
989 | 989 | |
|
990 | 990 | # top and bot now count context in the hunk |
|
991 | 991 | # adjust them if either one is short |
|
992 | 992 | context = max(top, bot, 3) |
|
993 | 993 | if bot < context: |
|
994 | 994 | bot = max(0, fuzz - (context - bot)) |
|
995 | 995 | else: |
|
996 | 996 | bot = min(fuzz, bot) |
|
997 | 997 | if top < context: |
|
998 | 998 | top = max(0, fuzz - (context - top)) |
|
999 | 999 | else: |
|
1000 | 1000 | top = min(fuzz, top) |
|
1001 | 1001 | |
|
1002 | 1002 | return l[top:len(l)-bot] |
|
1003 | 1003 | return l |
|
1004 | 1004 | |
|
1005 | 1005 | def old(self, fuzz=0, toponly=False): |
|
1006 | 1006 | return self.fuzzit(self.a, fuzz, toponly) |
|
1007 | 1007 | |
|
1008 | 1008 | def new(self, fuzz=0, toponly=False): |
|
1009 | 1009 | return self.fuzzit(self.b, fuzz, toponly) |
|
1010 | 1010 | |
|
1011 | 1011 | class binhunk(object): |
|
1012 | 1012 | 'A binary patch file. Only understands literals so far.' |
|
1013 | 1013 | def __init__(self, lr): |
|
1014 | 1014 | self.text = None |
|
1015 | 1015 | self.hunk = ['GIT binary patch\n'] |
|
1016 | 1016 | self._read(lr) |
|
1017 | 1017 | |
|
1018 | 1018 | def complete(self): |
|
1019 | 1019 | return self.text is not None |
|
1020 | 1020 | |
|
1021 | 1021 | def new(self): |
|
1022 | 1022 | return [self.text] |
|
1023 | 1023 | |
|
1024 | 1024 | def _read(self, lr): |
|
1025 | 1025 | line = lr.readline() |
|
1026 | 1026 | self.hunk.append(line) |
|
1027 | 1027 | while line and not line.startswith('literal '): |
|
1028 | 1028 | line = lr.readline() |
|
1029 | 1029 | self.hunk.append(line) |
|
1030 | 1030 | if not line: |
|
1031 | 1031 | raise PatchError(_('could not extract binary patch')) |
|
1032 | 1032 | size = int(line[8:].rstrip()) |
|
1033 | 1033 | dec = [] |
|
1034 | 1034 | line = lr.readline() |
|
1035 | 1035 | self.hunk.append(line) |
|
1036 | 1036 | while len(line) > 1: |
|
1037 | 1037 | l = line[0] |
|
1038 | 1038 | if l <= 'Z' and l >= 'A': |
|
1039 | 1039 | l = ord(l) - ord('A') + 1 |
|
1040 | 1040 | else: |
|
1041 | 1041 | l = ord(l) - ord('a') + 27 |
|
1042 | 1042 | dec.append(base85.b85decode(line[1:-1])[:l]) |
|
1043 | 1043 | line = lr.readline() |
|
1044 | 1044 | self.hunk.append(line) |
|
1045 | 1045 | text = zlib.decompress(''.join(dec)) |
|
1046 | 1046 | if len(text) != size: |
|
1047 | 1047 | raise PatchError(_('binary patch is %d bytes, not %d') % |
|
1048 | 1048 | len(text), size) |
|
1049 | 1049 | self.text = text |
|
1050 | 1050 | |
|
1051 | 1051 | def parsefilename(str): |
|
1052 | 1052 | # --- filename \t|space stuff |
|
1053 | 1053 | s = str[4:].rstrip('\r\n') |
|
1054 | 1054 | i = s.find('\t') |
|
1055 | 1055 | if i < 0: |
|
1056 | 1056 | i = s.find(' ') |
|
1057 | 1057 | if i < 0: |
|
1058 | 1058 | return s |
|
1059 | 1059 | return s[:i] |
|
1060 | 1060 | |
|
1061 | 1061 | def pathstrip(path, strip): |
|
1062 | 1062 | pathlen = len(path) |
|
1063 | 1063 | i = 0 |
|
1064 | 1064 | if strip == 0: |
|
1065 | 1065 | return '', path.rstrip() |
|
1066 | 1066 | count = strip |
|
1067 | 1067 | while count > 0: |
|
1068 | 1068 | i = path.find('/', i) |
|
1069 | 1069 | if i == -1: |
|
1070 | 1070 | raise PatchError(_("unable to strip away %d of %d dirs from %s") % |
|
1071 | 1071 | (count, strip, path)) |
|
1072 | 1072 | i += 1 |
|
1073 | 1073 | # consume '//' in the path |
|
1074 | 1074 | while i < pathlen - 1 and path[i] == '/': |
|
1075 | 1075 | i += 1 |
|
1076 | 1076 | count -= 1 |
|
1077 | 1077 | return path[:i].lstrip(), path[i:].rstrip() |
|
1078 | 1078 | |
|
1079 | 1079 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip): |
|
1080 | 1080 | nulla = afile_orig == "/dev/null" |
|
1081 | 1081 | nullb = bfile_orig == "/dev/null" |
|
1082 | 1082 | create = nulla and hunk.starta == 0 and hunk.lena == 0 |
|
1083 | 1083 | remove = nullb and hunk.startb == 0 and hunk.lenb == 0 |
|
1084 | 1084 | abase, afile = pathstrip(afile_orig, strip) |
|
1085 | 1085 | gooda = not nulla and backend.exists(afile) |
|
1086 | 1086 | bbase, bfile = pathstrip(bfile_orig, strip) |
|
1087 | 1087 | if afile == bfile: |
|
1088 | 1088 | goodb = gooda |
|
1089 | 1089 | else: |
|
1090 | 1090 | goodb = not nullb and backend.exists(bfile) |
|
1091 | 1091 | missing = not goodb and not gooda and not create |
|
1092 | 1092 | |
|
1093 | 1093 | # some diff programs apparently produce patches where the afile is |
|
1094 | 1094 | # not /dev/null, but afile starts with bfile |
|
1095 | 1095 | abasedir = afile[:afile.rfind('/') + 1] |
|
1096 | 1096 | bbasedir = bfile[:bfile.rfind('/') + 1] |
|
1097 | 1097 | if (missing and abasedir == bbasedir and afile.startswith(bfile) |
|
1098 | 1098 | and hunk.starta == 0 and hunk.lena == 0): |
|
1099 | 1099 | create = True |
|
1100 | 1100 | missing = False |
|
1101 | 1101 | |
|
1102 | 1102 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the |
|
1103 | 1103 | # diff is between a file and its backup. In this case, the original |
|
1104 | 1104 | # file should be patched (see original mpatch code). |
|
1105 | 1105 | isbackup = (abase == bbase and bfile.startswith(afile)) |
|
1106 | 1106 | fname = None |
|
1107 | 1107 | if not missing: |
|
1108 | 1108 | if gooda and goodb: |
|
1109 | 1109 | fname = isbackup and afile or bfile |
|
1110 | 1110 | elif gooda: |
|
1111 | 1111 | fname = afile |
|
1112 | 1112 | |
|
1113 | 1113 | if not fname: |
|
1114 | 1114 | if not nullb: |
|
1115 | 1115 | fname = isbackup and afile or bfile |
|
1116 | 1116 | elif not nulla: |
|
1117 | 1117 | fname = afile |
|
1118 | 1118 | else: |
|
1119 | 1119 | raise PatchError(_("undefined source and destination files")) |
|
1120 | 1120 | |
|
1121 | 1121 | gp = patchmeta(fname) |
|
1122 | 1122 | if create: |
|
1123 | 1123 | gp.op = 'ADD' |
|
1124 | 1124 | elif remove: |
|
1125 | 1125 | gp.op = 'DELETE' |
|
1126 | 1126 | return gp |
|
1127 | 1127 | |
|
1128 | 1128 | def scangitpatch(lr, firstline): |
|
1129 | 1129 | """ |
|
1130 | 1130 | Git patches can emit: |
|
1131 | 1131 | - rename a to b |
|
1132 | 1132 | - change b |
|
1133 | 1133 | - copy a to c |
|
1134 | 1134 | - change c |
|
1135 | 1135 | |
|
1136 | 1136 | We cannot apply this sequence as-is, the renamed 'a' could not be |
|
1137 | 1137 | found for it would have been renamed already. And we cannot copy |
|
1138 | 1138 | from 'b' instead because 'b' would have been changed already. So |
|
1139 | 1139 | we scan the git patch for copy and rename commands so we can |
|
1140 | 1140 | perform the copies ahead of time. |
|
1141 | 1141 | """ |
|
1142 | 1142 | pos = 0 |
|
1143 | 1143 | try: |
|
1144 | 1144 | pos = lr.fp.tell() |
|
1145 | 1145 | fp = lr.fp |
|
1146 | 1146 | except IOError: |
|
1147 | 1147 | fp = cStringIO.StringIO(lr.fp.read()) |
|
1148 | 1148 | gitlr = linereader(fp) |
|
1149 | 1149 | gitlr.push(firstline) |
|
1150 | 1150 | gitpatches = readgitpatch(gitlr) |
|
1151 | 1151 | fp.seek(pos) |
|
1152 | 1152 | return gitpatches |
|
1153 | 1153 | |
|
1154 | 1154 | def iterhunks(fp): |
|
1155 | 1155 | """Read a patch and yield the following events: |
|
1156 | 1156 | - ("file", afile, bfile, firsthunk): select a new target file. |
|
1157 | 1157 | - ("hunk", hunk): a new hunk is ready to be applied, follows a |
|
1158 | 1158 | "file" event. |
|
1159 | 1159 | - ("git", gitchanges): current diff is in git format, gitchanges |
|
1160 | 1160 | maps filenames to gitpatch records. Unique event. |
|
1161 | 1161 | """ |
|
1162 | 1162 | afile = "" |
|
1163 | 1163 | bfile = "" |
|
1164 | 1164 | state = None |
|
1165 | 1165 | hunknum = 0 |
|
1166 | 1166 | emitfile = newfile = False |
|
1167 | 1167 | gitpatches = None |
|
1168 | 1168 | |
|
1169 | 1169 | # our states |
|
1170 | 1170 | BFILE = 1 |
|
1171 | 1171 | context = None |
|
1172 | 1172 | lr = linereader(fp) |
|
1173 | 1173 | |
|
1174 | 1174 | while True: |
|
1175 | 1175 | x = lr.readline() |
|
1176 | 1176 | if not x: |
|
1177 | 1177 | break |
|
1178 | 1178 | if state == BFILE and ( |
|
1179 | 1179 | (not context and x[0] == '@') |
|
1180 | 1180 | or (context is not False and x.startswith('***************')) |
|
1181 | 1181 | or x.startswith('GIT binary patch')): |
|
1182 | 1182 | gp = None |
|
1183 | 1183 | if (gitpatches and |
|
1184 | 1184 | (gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)): |
|
1185 | 1185 | gp = gitpatches.pop()[2] |
|
1186 | 1186 | if x.startswith('GIT binary patch'): |
|
1187 | 1187 | h = binhunk(lr) |
|
1188 | 1188 | else: |
|
1189 | 1189 | if context is None and x.startswith('***************'): |
|
1190 | 1190 | context = True |
|
1191 | 1191 | h = hunk(x, hunknum + 1, lr, context) |
|
1192 | 1192 | hunknum += 1 |
|
1193 | 1193 | if emitfile: |
|
1194 | 1194 | emitfile = False |
|
1195 | 1195 | yield 'file', (afile, bfile, h, gp and gp.copy() or None) |
|
1196 | 1196 | yield 'hunk', h |
|
1197 | 1197 | elif x.startswith('diff --git'): |
|
1198 | 1198 | m = gitre.match(x) |
|
1199 | 1199 | if not m: |
|
1200 | 1200 | continue |
|
1201 | 1201 | if not gitpatches: |
|
1202 | 1202 | # scan whole input for git metadata |
|
1203 | 1203 | gitpatches = [('a/' + gp.path, 'b/' + gp.path, gp) for gp |
|
1204 | 1204 | in scangitpatch(lr, x)] |
|
1205 | 1205 | yield 'git', [g[2].copy() for g in gitpatches |
|
1206 | 1206 | if g[2].op in ('COPY', 'RENAME')] |
|
1207 | 1207 | gitpatches.reverse() |
|
1208 | 1208 | afile = 'a/' + m.group(1) |
|
1209 | 1209 | bfile = 'b/' + m.group(2) |
|
1210 | 1210 | while afile != gitpatches[-1][0] and bfile != gitpatches[-1][1]: |
|
1211 | 1211 | gp = gitpatches.pop()[2] |
|
1212 | 1212 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) |
|
1213 | 1213 | gp = gitpatches[-1][2] |
|
1214 | 1214 | # copy/rename + modify should modify target, not source |
|
1215 | 1215 | if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode: |
|
1216 | 1216 | afile = bfile |
|
1217 | 1217 | newfile = True |
|
1218 | 1218 | elif x.startswith('---'): |
|
1219 | 1219 | # check for a unified diff |
|
1220 | 1220 | l2 = lr.readline() |
|
1221 | 1221 | if not l2.startswith('+++'): |
|
1222 | 1222 | lr.push(l2) |
|
1223 | 1223 | continue |
|
1224 | 1224 | newfile = True |
|
1225 | 1225 | context = False |
|
1226 | 1226 | afile = parsefilename(x) |
|
1227 | 1227 | bfile = parsefilename(l2) |
|
1228 | 1228 | elif x.startswith('***'): |
|
1229 | 1229 | # check for a context diff |
|
1230 | 1230 | l2 = lr.readline() |
|
1231 | 1231 | if not l2.startswith('---'): |
|
1232 | 1232 | lr.push(l2) |
|
1233 | 1233 | continue |
|
1234 | 1234 | l3 = lr.readline() |
|
1235 | 1235 | lr.push(l3) |
|
1236 | 1236 | if not l3.startswith("***************"): |
|
1237 | 1237 | lr.push(l2) |
|
1238 | 1238 | continue |
|
1239 | 1239 | newfile = True |
|
1240 | 1240 | context = True |
|
1241 | 1241 | afile = parsefilename(x) |
|
1242 | 1242 | bfile = parsefilename(l2) |
|
1243 | 1243 | |
|
1244 | 1244 | if newfile: |
|
1245 | 1245 | newfile = False |
|
1246 | 1246 | emitfile = True |
|
1247 | 1247 | state = BFILE |
|
1248 | 1248 | hunknum = 0 |
|
1249 | 1249 | |
|
1250 | 1250 | while gitpatches: |
|
1251 | 1251 | gp = gitpatches.pop()[2] |
|
1252 | 1252 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) |
|
1253 | 1253 | |
|
1254 | 1254 | def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'): |
|
1255 | 1255 | """Reads a patch from fp and tries to apply it. |
|
1256 | 1256 | |
|
1257 | 1257 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if |
|
1258 | 1258 | there was any fuzz. |
|
1259 | 1259 | |
|
1260 | 1260 | If 'eolmode' is 'strict', the patch content and patched file are |
|
1261 | 1261 | read in binary mode. Otherwise, line endings are ignored when |
|
1262 | 1262 | patching then normalized according to 'eolmode'. |
|
1263 | 1263 | """ |
|
1264 | 1264 | return _applydiff(ui, fp, patchfile, backend, store, strip=strip, |
|
1265 | 1265 | eolmode=eolmode) |
|
1266 | 1266 | |
|
1267 | 1267 | def _applydiff(ui, fp, patcher, backend, store, strip=1, |
|
1268 | 1268 | eolmode='strict'): |
|
1269 | 1269 | |
|
1270 | 1270 | def pstrip(p): |
|
1271 | 1271 | return pathstrip(p, strip - 1)[1] |
|
1272 | 1272 | |
|
1273 | 1273 | rejects = 0 |
|
1274 | 1274 | err = 0 |
|
1275 | 1275 | current_file = None |
|
1276 | 1276 | |
|
1277 | 1277 | for state, values in iterhunks(fp): |
|
1278 | 1278 | if state == 'hunk': |
|
1279 | 1279 | if not current_file: |
|
1280 | 1280 | continue |
|
1281 | 1281 | ret = current_file.apply(values) |
|
1282 | 1282 | if ret > 0: |
|
1283 | 1283 | err = 1 |
|
1284 | 1284 | elif state == 'file': |
|
1285 | 1285 | if current_file: |
|
1286 | 1286 | rejects += current_file.close() |
|
1287 | 1287 | current_file = None |
|
1288 | 1288 | afile, bfile, first_hunk, gp = values |
|
1289 | 1289 | if gp: |
|
1290 | 1290 | path = pstrip(gp.path) |
|
1291 | 1291 | gp.path = pstrip(gp.path) |
|
1292 | 1292 | if gp.oldpath: |
|
1293 | 1293 | gp.oldpath = pstrip(gp.oldpath) |
|
1294 | 1294 | else: |
|
1295 | 1295 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip) |
|
1296 | 1296 | if gp.op == 'RENAME': |
|
1297 | 1297 | backend.unlink(gp.oldpath) |
|
1298 | 1298 | if not first_hunk: |
|
1299 | 1299 | if gp.op == 'DELETE': |
|
1300 | 1300 | backend.unlink(gp.path) |
|
1301 | 1301 | continue |
|
1302 | 1302 | data, mode = None, None |
|
1303 | 1303 | if gp.op in ('RENAME', 'COPY'): |
|
1304 | 1304 | data, mode = store.getfile(gp.oldpath)[:2] |
|
1305 | 1305 | if gp.mode: |
|
1306 | 1306 | mode = gp.mode |
|
1307 | 1307 | if gp.op == 'ADD': |
|
1308 | 1308 | # Added files without content have no hunk and |
|
1309 | 1309 | # must be created |
|
1310 | 1310 | data = '' |
|
1311 | 1311 | if data or mode: |
|
1312 | 1312 | if (gp.op in ('ADD', 'RENAME', 'COPY') |
|
1313 | 1313 | and backend.exists(gp.path)): |
|
1314 | 1314 | raise PatchError(_("cannot create %s: destination " |
|
1315 | 1315 | "already exists") % gp.path) |
|
1316 | 1316 | backend.setfile(gp.path, data, mode, gp.oldpath) |
|
1317 | 1317 | continue |
|
1318 | 1318 | try: |
|
1319 | 1319 | current_file = patcher(ui, gp, backend, store, |
|
1320 | 1320 | eolmode=eolmode) |
|
1321 | 1321 | except PatchError, inst: |
|
1322 | 1322 | ui.warn(str(inst) + '\n') |
|
1323 | 1323 | current_file = None |
|
1324 | 1324 | rejects += 1 |
|
1325 | 1325 | continue |
|
1326 | 1326 | elif state == 'git': |
|
1327 | 1327 | for gp in values: |
|
1328 | 1328 | path = pstrip(gp.oldpath) |
|
1329 | 1329 | data, mode = backend.getfile(path) |
|
1330 | 1330 | store.setfile(path, data, mode) |
|
1331 | 1331 | else: |
|
1332 | 1332 | raise util.Abort(_('unsupported parser state: %s') % state) |
|
1333 | 1333 | |
|
1334 | 1334 | if current_file: |
|
1335 | 1335 | rejects += current_file.close() |
|
1336 | 1336 | |
|
1337 | 1337 | if rejects: |
|
1338 | 1338 | return -1 |
|
1339 | 1339 | return err |
|
1340 | 1340 | |
|
1341 | 1341 | def _externalpatch(ui, repo, patcher, patchname, strip, files, |
|
1342 | 1342 | similarity): |
|
1343 | 1343 | """use <patcher> to apply <patchname> to the working directory. |
|
1344 | 1344 | returns whether patch was applied with fuzz factor.""" |
|
1345 | 1345 | |
|
1346 | 1346 | fuzz = False |
|
1347 | 1347 | args = [] |
|
1348 | 1348 | cwd = repo.root |
|
1349 | 1349 | if cwd: |
|
1350 | 1350 | args.append('-d %s' % util.shellquote(cwd)) |
|
1351 | 1351 | fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, |
|
1352 | 1352 | util.shellquote(patchname))) |
|
1353 | 1353 | try: |
|
1354 | 1354 | for line in fp: |
|
1355 | 1355 | line = line.rstrip() |
|
1356 | 1356 | ui.note(line + '\n') |
|
1357 | 1357 | if line.startswith('patching file '): |
|
1358 | 1358 | pf = util.parsepatchoutput(line) |
|
1359 | 1359 | printed_file = False |
|
1360 | 1360 | files.add(pf) |
|
1361 | 1361 | elif line.find('with fuzz') >= 0: |
|
1362 | 1362 | fuzz = True |
|
1363 | 1363 | if not printed_file: |
|
1364 | 1364 | ui.warn(pf + '\n') |
|
1365 | 1365 | printed_file = True |
|
1366 | 1366 | ui.warn(line + '\n') |
|
1367 | 1367 | elif line.find('saving rejects to file') >= 0: |
|
1368 | 1368 | ui.warn(line + '\n') |
|
1369 | 1369 | elif line.find('FAILED') >= 0: |
|
1370 | 1370 | if not printed_file: |
|
1371 | 1371 | ui.warn(pf + '\n') |
|
1372 | 1372 | printed_file = True |
|
1373 | 1373 | ui.warn(line + '\n') |
|
1374 | 1374 | finally: |
|
1375 | 1375 | if files: |
|
1376 | 1376 | cfiles = list(files) |
|
1377 | 1377 | cwd = repo.getcwd() |
|
1378 | 1378 | if cwd: |
|
1379 | 1379 | cfiles = [util.pathto(repo.root, cwd, f) |
|
1380 | 1380 | for f in cfiles] |
|
1381 | 1381 | scmutil.addremove(repo, cfiles, similarity=similarity) |
|
1382 | 1382 | code = fp.close() |
|
1383 | 1383 | if code: |
|
1384 | 1384 | raise PatchError(_("patch command failed: %s") % |
|
1385 | 1385 | util.explainexit(code)[0]) |
|
1386 | 1386 | return fuzz |
|
1387 | 1387 | |
|
1388 | 1388 | def patchbackend(ui, backend, patchobj, strip, files=None, eolmode='strict'): |
|
1389 | 1389 | if files is None: |
|
1390 | 1390 | files = set() |
|
1391 | 1391 | if eolmode is None: |
|
1392 | 1392 | eolmode = ui.config('patch', 'eol', 'strict') |
|
1393 | 1393 | if eolmode.lower() not in eolmodes: |
|
1394 | 1394 | raise util.Abort(_('unsupported line endings type: %s') % eolmode) |
|
1395 | 1395 | eolmode = eolmode.lower() |
|
1396 | 1396 | |
|
1397 | 1397 | store = filestore() |
|
1398 | 1398 | try: |
|
1399 | 1399 | fp = open(patchobj, 'rb') |
|
1400 | 1400 | except TypeError: |
|
1401 | 1401 | fp = patchobj |
|
1402 | 1402 | try: |
|
1403 | 1403 | ret = applydiff(ui, fp, backend, store, strip=strip, |
|
1404 | 1404 | eolmode=eolmode) |
|
1405 | 1405 | finally: |
|
1406 | 1406 | if fp != patchobj: |
|
1407 | 1407 | fp.close() |
|
1408 | 1408 | files.update(backend.close()) |
|
1409 | 1409 | store.close() |
|
1410 | 1410 | if ret < 0: |
|
1411 | 1411 | raise PatchError(_('patch failed to apply')) |
|
1412 | 1412 | return ret > 0 |
|
1413 | 1413 | |
|
1414 | 1414 | def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict', |
|
1415 | 1415 | similarity=0): |
|
1416 | 1416 | """use builtin patch to apply <patchobj> to the working directory. |
|
1417 | 1417 | returns whether patch was applied with fuzz factor.""" |
|
1418 | 1418 | backend = workingbackend(ui, repo, similarity) |
|
1419 | 1419 | return patchbackend(ui, backend, patchobj, strip, files, eolmode) |
|
1420 | 1420 | |
|
1421 | 1421 | def patchrepo(ui, repo, ctx, store, patchobj, strip, files=None, |
|
1422 | 1422 | eolmode='strict'): |
|
1423 | 1423 | backend = repobackend(ui, repo, ctx, store) |
|
1424 | 1424 | return patchbackend(ui, backend, patchobj, strip, files, eolmode) |
|
1425 | 1425 | |
|
1426 | 1426 | def makememctx(repo, parents, text, user, date, branch, files, store, |
|
1427 | 1427 | editor=None): |
|
1428 | 1428 | def getfilectx(repo, memctx, path): |
|
1429 | 1429 | data, (islink, isexec), copied = store.getfile(path) |
|
1430 | 1430 | return context.memfilectx(path, data, islink=islink, isexec=isexec, |
|
1431 | 1431 | copied=copied) |
|
1432 | 1432 | extra = {} |
|
1433 | 1433 | if branch: |
|
1434 | 1434 | extra['branch'] = encoding.fromlocal(branch) |
|
1435 | 1435 | ctx = context.memctx(repo, parents, text, files, getfilectx, user, |
|
1436 | 1436 | date, extra) |
|
1437 | 1437 | if editor: |
|
1438 | 1438 | ctx._text = editor(repo, ctx, []) |
|
1439 | 1439 | return ctx |
|
1440 | 1440 | |
|
1441 | 1441 | def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict', |
|
1442 | 1442 | similarity=0): |
|
1443 | 1443 | """Apply <patchname> to the working directory. |
|
1444 | 1444 | |
|
1445 | 1445 | 'eolmode' specifies how end of lines should be handled. It can be: |
|
1446 | 1446 | - 'strict': inputs are read in binary mode, EOLs are preserved |
|
1447 | 1447 | - 'crlf': EOLs are ignored when patching and reset to CRLF |
|
1448 | 1448 | - 'lf': EOLs are ignored when patching and reset to LF |
|
1449 | 1449 | - None: get it from user settings, default to 'strict' |
|
1450 | 1450 | 'eolmode' is ignored when using an external patcher program. |
|
1451 | 1451 | |
|
1452 | 1452 | Returns whether patch was applied with fuzz factor. |
|
1453 | 1453 | """ |
|
1454 | 1454 | patcher = ui.config('ui', 'patch') |
|
1455 | 1455 | if files is None: |
|
1456 | 1456 | files = set() |
|
1457 | 1457 | try: |
|
1458 | 1458 | if patcher: |
|
1459 | 1459 | return _externalpatch(ui, repo, patcher, patchname, strip, |
|
1460 | 1460 | files, similarity) |
|
1461 | 1461 | return internalpatch(ui, repo, patchname, strip, files, eolmode, |
|
1462 | 1462 | similarity) |
|
1463 | 1463 | except PatchError, err: |
|
1464 | 1464 | raise util.Abort(str(err)) |
|
1465 | 1465 | |
|
1466 | 1466 | def changedfiles(ui, repo, patchpath, strip=1): |
|
1467 | 1467 | backend = fsbackend(ui, repo.root) |
|
1468 | 1468 | fp = open(patchpath, 'rb') |
|
1469 | 1469 | try: |
|
1470 | 1470 | changed = set() |
|
1471 | 1471 | for state, values in iterhunks(fp): |
|
1472 | 1472 | if state == 'file': |
|
1473 | 1473 | afile, bfile, first_hunk, gp = values |
|
1474 | 1474 | if gp: |
|
1475 | 1475 | gp.path = pathstrip(gp.path, strip - 1)[1] |
|
1476 | 1476 | if gp.oldpath: |
|
1477 | 1477 | gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1] |
|
1478 | 1478 | else: |
|
1479 | 1479 | gp = makepatchmeta(backend, afile, bfile, first_hunk, strip) |
|
1480 | 1480 | changed.add(gp.path) |
|
1481 | 1481 | if gp.op == 'RENAME': |
|
1482 | 1482 | changed.add(gp.oldpath) |
|
1483 | 1483 | elif state not in ('hunk', 'git'): |
|
1484 | 1484 | raise util.Abort(_('unsupported parser state: %s') % state) |
|
1485 | 1485 | return changed |
|
1486 | 1486 | finally: |
|
1487 | 1487 | fp.close() |
|
1488 | 1488 | |
|
1489 | 1489 | def b85diff(to, tn): |
|
1490 | 1490 | '''print base85-encoded binary diff''' |
|
1491 | 1491 | def gitindex(text): |
|
1492 | 1492 | if not text: |
|
1493 | 1493 | return hex(nullid) |
|
1494 | 1494 | l = len(text) |
|
1495 | 1495 | s = util.sha1('blob %d\0' % l) |
|
1496 | 1496 | s.update(text) |
|
1497 | 1497 | return s.hexdigest() |
|
1498 | 1498 | |
|
1499 | 1499 | def fmtline(line): |
|
1500 | 1500 | l = len(line) |
|
1501 | 1501 | if l <= 26: |
|
1502 | 1502 | l = chr(ord('A') + l - 1) |
|
1503 | 1503 | else: |
|
1504 | 1504 | l = chr(l - 26 + ord('a') - 1) |
|
1505 | 1505 | return '%c%s\n' % (l, base85.b85encode(line, True)) |
|
1506 | 1506 | |
|
1507 | 1507 | def chunk(text, csize=52): |
|
1508 | 1508 | l = len(text) |
|
1509 | 1509 | i = 0 |
|
1510 | 1510 | while i < l: |
|
1511 | 1511 | yield text[i:i + csize] |
|
1512 | 1512 | i += csize |
|
1513 | 1513 | |
|
1514 | 1514 | tohash = gitindex(to) |
|
1515 | 1515 | tnhash = gitindex(tn) |
|
1516 | 1516 | if tohash == tnhash: |
|
1517 | 1517 | return "" |
|
1518 | 1518 | |
|
1519 | 1519 | # TODO: deltas |
|
1520 | 1520 | ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' % |
|
1521 | 1521 | (tohash, tnhash, len(tn))] |
|
1522 | 1522 | for l in chunk(zlib.compress(tn)): |
|
1523 | 1523 | ret.append(fmtline(l)) |
|
1524 | 1524 | ret.append('\n') |
|
1525 | 1525 | return ''.join(ret) |
|
1526 | 1526 | |
|
1527 | 1527 | class GitDiffRequired(Exception): |
|
1528 | 1528 | pass |
|
1529 | 1529 | |
|
1530 | 1530 | def diffopts(ui, opts=None, untrusted=False, section='diff'): |
|
1531 | 1531 | def get(key, name=None, getter=ui.configbool): |
|
1532 | 1532 | return ((opts and opts.get(key)) or |
|
1533 | 1533 | getter(section, name or key, None, untrusted=untrusted)) |
|
1534 | 1534 | return mdiff.diffopts( |
|
1535 | 1535 | text=opts and opts.get('text'), |
|
1536 | 1536 | git=get('git'), |
|
1537 | 1537 | nodates=get('nodates'), |
|
1538 | 1538 | showfunc=get('show_function', 'showfunc'), |
|
1539 | 1539 | ignorews=get('ignore_all_space', 'ignorews'), |
|
1540 | 1540 | ignorewsamount=get('ignore_space_change', 'ignorewsamount'), |
|
1541 | 1541 | ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'), |
|
1542 | 1542 | context=get('unified', getter=ui.config)) |
|
1543 | 1543 | |
|
1544 | 1544 | def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None, |
|
1545 | 1545 | losedatafn=None, prefix=''): |
|
1546 | 1546 | '''yields diff of changes to files between two nodes, or node and |
|
1547 | 1547 | working directory. |
|
1548 | 1548 | |
|
1549 | 1549 | if node1 is None, use first dirstate parent instead. |
|
1550 | 1550 | if node2 is None, compare node1 with working directory. |
|
1551 | 1551 | |
|
1552 | 1552 | losedatafn(**kwarg) is a callable run when opts.upgrade=True and |
|
1553 | 1553 | every time some change cannot be represented with the current |
|
1554 | 1554 | patch format. Return False to upgrade to git patch format, True to |
|
1555 | 1555 | accept the loss or raise an exception to abort the diff. It is |
|
1556 | 1556 | called with the name of current file being diffed as 'fn'. If set |
|
1557 | 1557 | to None, patches will always be upgraded to git format when |
|
1558 | 1558 | necessary. |
|
1559 | 1559 | |
|
1560 | 1560 | prefix is a filename prefix that is prepended to all filenames on |
|
1561 | 1561 | display (used for subrepos). |
|
1562 | 1562 | ''' |
|
1563 | 1563 | |
|
1564 | 1564 | if opts is None: |
|
1565 | 1565 | opts = mdiff.defaultopts |
|
1566 | 1566 | |
|
1567 | 1567 | if not node1 and not node2: |
|
1568 | 1568 | node1 = repo.dirstate.p1() |
|
1569 | 1569 | |
|
1570 | 1570 | def lrugetfilectx(): |
|
1571 | 1571 | cache = {} |
|
1572 | 1572 | order = [] |
|
1573 | 1573 | def getfilectx(f, ctx): |
|
1574 | 1574 | fctx = ctx.filectx(f, filelog=cache.get(f)) |
|
1575 | 1575 | if f not in cache: |
|
1576 | 1576 | if len(cache) > 20: |
|
1577 | 1577 | del cache[order.pop(0)] |
|
1578 | 1578 | cache[f] = fctx.filelog() |
|
1579 | 1579 | else: |
|
1580 | 1580 | order.remove(f) |
|
1581 | 1581 | order.append(f) |
|
1582 | 1582 | return fctx |
|
1583 | 1583 | return getfilectx |
|
1584 | 1584 | getfilectx = lrugetfilectx() |
|
1585 | 1585 | |
|
1586 | 1586 | ctx1 = repo[node1] |
|
1587 | 1587 | ctx2 = repo[node2] |
|
1588 | 1588 | |
|
1589 | 1589 | if not changes: |
|
1590 | 1590 | changes = repo.status(ctx1, ctx2, match=match) |
|
1591 | 1591 | modified, added, removed = changes[:3] |
|
1592 | 1592 | |
|
1593 | 1593 | if not modified and not added and not removed: |
|
1594 | 1594 | return [] |
|
1595 | 1595 | |
|
1596 | 1596 | revs = None |
|
1597 | 1597 | if not repo.ui.quiet: |
|
1598 | 1598 | hexfunc = repo.ui.debugflag and hex or short |
|
1599 | 1599 | revs = [hexfunc(node) for node in [node1, node2] if node] |
|
1600 | 1600 | |
|
1601 | 1601 | copy = {} |
|
1602 | 1602 | if opts.git or opts.upgrade: |
|
1603 |
copy = copies.copies( |
|
|
1603 | copy = copies.pathcopies(ctx1, ctx2) | |
|
1604 | 1604 | |
|
1605 | 1605 | difffn = lambda opts, losedata: trydiff(repo, revs, ctx1, ctx2, |
|
1606 | 1606 | modified, added, removed, copy, getfilectx, opts, losedata, prefix) |
|
1607 | 1607 | if opts.upgrade and not opts.git: |
|
1608 | 1608 | try: |
|
1609 | 1609 | def losedata(fn): |
|
1610 | 1610 | if not losedatafn or not losedatafn(fn=fn): |
|
1611 | 1611 | raise GitDiffRequired() |
|
1612 | 1612 | # Buffer the whole output until we are sure it can be generated |
|
1613 | 1613 | return list(difffn(opts.copy(git=False), losedata)) |
|
1614 | 1614 | except GitDiffRequired: |
|
1615 | 1615 | return difffn(opts.copy(git=True), None) |
|
1616 | 1616 | else: |
|
1617 | 1617 | return difffn(opts, None) |
|
1618 | 1618 | |
|
1619 | 1619 | def difflabel(func, *args, **kw): |
|
1620 | 1620 | '''yields 2-tuples of (output, label) based on the output of func()''' |
|
1621 | 1621 | headprefixes = [('diff', 'diff.diffline'), |
|
1622 | 1622 | ('copy', 'diff.extended'), |
|
1623 | 1623 | ('rename', 'diff.extended'), |
|
1624 | 1624 | ('old', 'diff.extended'), |
|
1625 | 1625 | ('new', 'diff.extended'), |
|
1626 | 1626 | ('deleted', 'diff.extended'), |
|
1627 | 1627 | ('---', 'diff.file_a'), |
|
1628 | 1628 | ('+++', 'diff.file_b')] |
|
1629 | 1629 | textprefixes = [('@', 'diff.hunk'), |
|
1630 | 1630 | ('-', 'diff.deleted'), |
|
1631 | 1631 | ('+', 'diff.inserted')] |
|
1632 | 1632 | head = False |
|
1633 | 1633 | for chunk in func(*args, **kw): |
|
1634 | 1634 | lines = chunk.split('\n') |
|
1635 | 1635 | for i, line in enumerate(lines): |
|
1636 | 1636 | if i != 0: |
|
1637 | 1637 | yield ('\n', '') |
|
1638 | 1638 | if head: |
|
1639 | 1639 | if line.startswith('@'): |
|
1640 | 1640 | head = False |
|
1641 | 1641 | else: |
|
1642 | 1642 | if line and not line[0] in ' +-@\\': |
|
1643 | 1643 | head = True |
|
1644 | 1644 | stripline = line |
|
1645 | 1645 | if not head and line and line[0] in '+-': |
|
1646 | 1646 | # highlight trailing whitespace, but only in changed lines |
|
1647 | 1647 | stripline = line.rstrip() |
|
1648 | 1648 | prefixes = textprefixes |
|
1649 | 1649 | if head: |
|
1650 | 1650 | prefixes = headprefixes |
|
1651 | 1651 | for prefix, label in prefixes: |
|
1652 | 1652 | if stripline.startswith(prefix): |
|
1653 | 1653 | yield (stripline, label) |
|
1654 | 1654 | break |
|
1655 | 1655 | else: |
|
1656 | 1656 | yield (line, '') |
|
1657 | 1657 | if line != stripline: |
|
1658 | 1658 | yield (line[len(stripline):], 'diff.trailingwhitespace') |
|
1659 | 1659 | |
|
1660 | 1660 | def diffui(*args, **kw): |
|
1661 | 1661 | '''like diff(), but yields 2-tuples of (output, label) for ui.write()''' |
|
1662 | 1662 | return difflabel(diff, *args, **kw) |
|
1663 | 1663 | |
|
1664 | 1664 | |
|
1665 | 1665 | def _addmodehdr(header, omode, nmode): |
|
1666 | 1666 | if omode != nmode: |
|
1667 | 1667 | header.append('old mode %s\n' % omode) |
|
1668 | 1668 | header.append('new mode %s\n' % nmode) |
|
1669 | 1669 | |
|
1670 | 1670 | def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, |
|
1671 | 1671 | copy, getfilectx, opts, losedatafn, prefix): |
|
1672 | 1672 | |
|
1673 | 1673 | def join(f): |
|
1674 | 1674 | return os.path.join(prefix, f) |
|
1675 | 1675 | |
|
1676 | 1676 | date1 = util.datestr(ctx1.date()) |
|
1677 | 1677 | man1 = ctx1.manifest() |
|
1678 | 1678 | |
|
1679 | 1679 | gone = set() |
|
1680 | 1680 | gitmode = {'l': '120000', 'x': '100755', '': '100644'} |
|
1681 | 1681 | |
|
1682 | 1682 | copyto = dict([(v, k) for k, v in copy.items()]) |
|
1683 | 1683 | |
|
1684 | 1684 | if opts.git: |
|
1685 | 1685 | revs = None |
|
1686 | 1686 | |
|
1687 | 1687 | for f in sorted(modified + added + removed): |
|
1688 | 1688 | to = None |
|
1689 | 1689 | tn = None |
|
1690 | 1690 | dodiff = True |
|
1691 | 1691 | header = [] |
|
1692 | 1692 | if f in man1: |
|
1693 | 1693 | to = getfilectx(f, ctx1).data() |
|
1694 | 1694 | if f not in removed: |
|
1695 | 1695 | tn = getfilectx(f, ctx2).data() |
|
1696 | 1696 | a, b = f, f |
|
1697 | 1697 | if opts.git or losedatafn: |
|
1698 | 1698 | if f in added: |
|
1699 | 1699 | mode = gitmode[ctx2.flags(f)] |
|
1700 | 1700 | if f in copy or f in copyto: |
|
1701 | 1701 | if opts.git: |
|
1702 | 1702 | if f in copy: |
|
1703 | 1703 | a = copy[f] |
|
1704 | 1704 | else: |
|
1705 | 1705 | a = copyto[f] |
|
1706 | 1706 | omode = gitmode[man1.flags(a)] |
|
1707 | 1707 | _addmodehdr(header, omode, mode) |
|
1708 | 1708 | if a in removed and a not in gone: |
|
1709 | 1709 | op = 'rename' |
|
1710 | 1710 | gone.add(a) |
|
1711 | 1711 | else: |
|
1712 | 1712 | op = 'copy' |
|
1713 | 1713 | header.append('%s from %s\n' % (op, join(a))) |
|
1714 | 1714 | header.append('%s to %s\n' % (op, join(f))) |
|
1715 | 1715 | to = getfilectx(a, ctx1).data() |
|
1716 | 1716 | else: |
|
1717 | 1717 | losedatafn(f) |
|
1718 | 1718 | else: |
|
1719 | 1719 | if opts.git: |
|
1720 | 1720 | header.append('new file mode %s\n' % mode) |
|
1721 | 1721 | elif ctx2.flags(f): |
|
1722 | 1722 | losedatafn(f) |
|
1723 | 1723 | # In theory, if tn was copied or renamed we should check |
|
1724 | 1724 | # if the source is binary too but the copy record already |
|
1725 | 1725 | # forces git mode. |
|
1726 | 1726 | if util.binary(tn): |
|
1727 | 1727 | if opts.git: |
|
1728 | 1728 | dodiff = 'binary' |
|
1729 | 1729 | else: |
|
1730 | 1730 | losedatafn(f) |
|
1731 | 1731 | if not opts.git and not tn: |
|
1732 | 1732 | # regular diffs cannot represent new empty file |
|
1733 | 1733 | losedatafn(f) |
|
1734 | 1734 | elif f in removed: |
|
1735 | 1735 | if opts.git: |
|
1736 | 1736 | # have we already reported a copy above? |
|
1737 | 1737 | if ((f in copy and copy[f] in added |
|
1738 | 1738 | and copyto[copy[f]] == f) or |
|
1739 | 1739 | (f in copyto and copyto[f] in added |
|
1740 | 1740 | and copy[copyto[f]] == f)): |
|
1741 | 1741 | dodiff = False |
|
1742 | 1742 | else: |
|
1743 | 1743 | header.append('deleted file mode %s\n' % |
|
1744 | 1744 | gitmode[man1.flags(f)]) |
|
1745 | 1745 | elif not to or util.binary(to): |
|
1746 | 1746 | # regular diffs cannot represent empty file deletion |
|
1747 | 1747 | losedatafn(f) |
|
1748 | 1748 | else: |
|
1749 | 1749 | oflag = man1.flags(f) |
|
1750 | 1750 | nflag = ctx2.flags(f) |
|
1751 | 1751 | binary = util.binary(to) or util.binary(tn) |
|
1752 | 1752 | if opts.git: |
|
1753 | 1753 | _addmodehdr(header, gitmode[oflag], gitmode[nflag]) |
|
1754 | 1754 | if binary: |
|
1755 | 1755 | dodiff = 'binary' |
|
1756 | 1756 | elif binary or nflag != oflag: |
|
1757 | 1757 | losedatafn(f) |
|
1758 | 1758 | if opts.git: |
|
1759 | 1759 | header.insert(0, mdiff.diffline(revs, join(a), join(b), opts)) |
|
1760 | 1760 | |
|
1761 | 1761 | if dodiff: |
|
1762 | 1762 | if dodiff == 'binary': |
|
1763 | 1763 | text = b85diff(to, tn) |
|
1764 | 1764 | else: |
|
1765 | 1765 | text = mdiff.unidiff(to, date1, |
|
1766 | 1766 | # ctx2 date may be dynamic |
|
1767 | 1767 | tn, util.datestr(ctx2.date()), |
|
1768 | 1768 | join(a), join(b), revs, opts=opts) |
|
1769 | 1769 | if header and (text or len(header) > 1): |
|
1770 | 1770 | yield ''.join(header) |
|
1771 | 1771 | if text: |
|
1772 | 1772 | yield text |
|
1773 | 1773 | |
|
1774 | 1774 | def diffstatsum(stats): |
|
1775 | 1775 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False |
|
1776 | 1776 | for f, a, r, b in stats: |
|
1777 | 1777 | maxfile = max(maxfile, encoding.colwidth(f)) |
|
1778 | 1778 | maxtotal = max(maxtotal, a + r) |
|
1779 | 1779 | addtotal += a |
|
1780 | 1780 | removetotal += r |
|
1781 | 1781 | binary = binary or b |
|
1782 | 1782 | |
|
1783 | 1783 | return maxfile, maxtotal, addtotal, removetotal, binary |
|
1784 | 1784 | |
|
1785 | 1785 | def diffstatdata(lines): |
|
1786 | 1786 | diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$') |
|
1787 | 1787 | |
|
1788 | 1788 | results = [] |
|
1789 | 1789 | filename, adds, removes, isbinary = None, 0, 0, False |
|
1790 | 1790 | |
|
1791 | 1791 | def addresult(): |
|
1792 | 1792 | if filename: |
|
1793 | 1793 | results.append((filename, adds, removes, isbinary)) |
|
1794 | 1794 | |
|
1795 | 1795 | for line in lines: |
|
1796 | 1796 | if line.startswith('diff'): |
|
1797 | 1797 | addresult() |
|
1798 | 1798 | # set numbers to 0 anyway when starting new file |
|
1799 | 1799 | adds, removes, isbinary = 0, 0, False |
|
1800 | 1800 | if line.startswith('diff --git'): |
|
1801 | 1801 | filename = gitre.search(line).group(1) |
|
1802 | 1802 | elif line.startswith('diff -r'): |
|
1803 | 1803 | # format: "diff -r ... -r ... filename" |
|
1804 | 1804 | filename = diffre.search(line).group(1) |
|
1805 | 1805 | elif line.startswith('+') and not line.startswith('+++'): |
|
1806 | 1806 | adds += 1 |
|
1807 | 1807 | elif line.startswith('-') and not line.startswith('---'): |
|
1808 | 1808 | removes += 1 |
|
1809 | 1809 | elif (line.startswith('GIT binary patch') or |
|
1810 | 1810 | line.startswith('Binary file')): |
|
1811 | 1811 | isbinary = True |
|
1812 | 1812 | addresult() |
|
1813 | 1813 | return results |
|
1814 | 1814 | |
|
1815 | 1815 | def diffstat(lines, width=80, git=False): |
|
1816 | 1816 | output = [] |
|
1817 | 1817 | stats = diffstatdata(lines) |
|
1818 | 1818 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) |
|
1819 | 1819 | |
|
1820 | 1820 | countwidth = len(str(maxtotal)) |
|
1821 | 1821 | if hasbinary and countwidth < 3: |
|
1822 | 1822 | countwidth = 3 |
|
1823 | 1823 | graphwidth = width - countwidth - maxname - 6 |
|
1824 | 1824 | if graphwidth < 10: |
|
1825 | 1825 | graphwidth = 10 |
|
1826 | 1826 | |
|
1827 | 1827 | def scale(i): |
|
1828 | 1828 | if maxtotal <= graphwidth: |
|
1829 | 1829 | return i |
|
1830 | 1830 | # If diffstat runs out of room it doesn't print anything, |
|
1831 | 1831 | # which isn't very useful, so always print at least one + or - |
|
1832 | 1832 | # if there were at least some changes. |
|
1833 | 1833 | return max(i * graphwidth // maxtotal, int(bool(i))) |
|
1834 | 1834 | |
|
1835 | 1835 | for filename, adds, removes, isbinary in stats: |
|
1836 | 1836 | if isbinary: |
|
1837 | 1837 | count = 'Bin' |
|
1838 | 1838 | else: |
|
1839 | 1839 | count = adds + removes |
|
1840 | 1840 | pluses = '+' * scale(adds) |
|
1841 | 1841 | minuses = '-' * scale(removes) |
|
1842 | 1842 | output.append(' %s%s | %*s %s%s\n' % |
|
1843 | 1843 | (filename, ' ' * (maxname - encoding.colwidth(filename)), |
|
1844 | 1844 | countwidth, count, pluses, minuses)) |
|
1845 | 1845 | |
|
1846 | 1846 | if stats: |
|
1847 | 1847 | output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n') |
|
1848 | 1848 | % (len(stats), totaladds, totalremoves)) |
|
1849 | 1849 | |
|
1850 | 1850 | return ''.join(output) |
|
1851 | 1851 | |
|
1852 | 1852 | def diffstatui(*args, **kw): |
|
1853 | 1853 | '''like diffstat(), but yields 2-tuples of (output, label) for |
|
1854 | 1854 | ui.write() |
|
1855 | 1855 | ''' |
|
1856 | 1856 | |
|
1857 | 1857 | for line in diffstat(*args, **kw).splitlines(): |
|
1858 | 1858 | if line and line[-1] in '+-': |
|
1859 | 1859 | name, graph = line.rsplit(' ', 1) |
|
1860 | 1860 | yield (name + ' ', '') |
|
1861 | 1861 | m = re.search(r'\++', graph) |
|
1862 | 1862 | if m: |
|
1863 | 1863 | yield (m.group(0), 'diffstat.inserted') |
|
1864 | 1864 | m = re.search(r'-+', graph) |
|
1865 | 1865 | if m: |
|
1866 | 1866 | yield (m.group(0), 'diffstat.deleted') |
|
1867 | 1867 | else: |
|
1868 | 1868 | yield (line, '') |
|
1869 | 1869 | yield ('\n', '') |
General Comments 0
You need to be logged in to leave comments.
Login now