Mercurial > hg-stable
comparison mercurial/cmdutil.py @ 21576:33395a7e5527
revert: group action into a single dictionary
We had 4 different variables to hold the list of the 4 possibles
actions. I'm grouping them in a single dictionary for a few reasons.
First, it makes it clearer they are all related and meant to be the
final actions performed by revert. Second this simplifies the parameter
of the _performrevert function. Finally the two elements in each entry
(list and message) have a different consumers in different functions,
this change will make it easier to split them in a later commit.
author | Pierre-Yves David <pierre-yves.david@fb.com> |
---|---|
date | Tue, 13 May 2014 16:42:31 -0700 |
parents | 8262c2a39ab8 |
children | c62c5ce750ee |
comparison
equal
deleted
inserted
replaced
21575:8262c2a39ab8 | 21576:33395a7e5527 |
---|---|
2316 return _('forgetting %s\n') | 2316 return _('forgetting %s\n') |
2317 return _('removing %s\n') | 2317 return _('removing %s\n') |
2318 | 2318 |
2319 # action to be actually performed by revert | 2319 # action to be actually performed by revert |
2320 # (<list of file>, message>) tuple | 2320 # (<list of file>, message>) tuple |
2321 revert = ([], _('reverting %s\n')) | 2321 actions = {'revert': ([], _('reverting %s\n')), |
2322 add = ([], _('adding %s\n')) | 2322 'add': ([], _('adding %s\n')), |
2323 remove = ([], removeforget) | 2323 'remove': ([], removeforget), |
2324 undelete = ([], _('undeleting %s\n')) | 2324 'undelete': ([], _('undeleting %s\n'))} |
2325 | 2325 |
2326 disptable = ( | 2326 disptable = ( |
2327 # dispatch table: | 2327 # dispatch table: |
2328 # file state | 2328 # file state |
2329 # action if in target manifest | 2329 # action if in target manifest |
2330 # action if not in target manifest | 2330 # action if not in target manifest |
2331 # make backup if in target manifest | 2331 # make backup if in target manifest |
2332 # make backup if not in target manifest | 2332 # make backup if not in target manifest |
2333 (modified, revert, remove, True, True), | 2333 (modified, actions['revert'], actions['remove'], True, True), |
2334 (added, revert, remove, True, False), | 2334 (added, actions['revert'], actions['remove'], True, False), |
2335 (removed, undelete, None, True, False), | 2335 (removed, actions['undelete'], None, True, False), |
2336 (deleted, revert, remove, False, False), | 2336 (deleted, actions['revert'], actions['remove'], False, False), |
2337 ) | 2337 ) |
2338 | 2338 |
2339 for abs, (rel, exact) in sorted(names.items()): | 2339 for abs, (rel, exact) in sorted(names.items()): |
2340 # hash on file in target manifest (or None if missing from target) | 2340 # hash on file in target manifest (or None if missing from target) |
2341 mfentry = mf.get(abs) | 2341 mfentry = mf.get(abs) |
2372 # Not touched in current dirstate. | 2372 # Not touched in current dirstate. |
2373 | 2373 |
2374 # file is unknown in parent, restore older version or ignore. | 2374 # file is unknown in parent, restore older version or ignore. |
2375 if abs not in repo.dirstate: | 2375 if abs not in repo.dirstate: |
2376 if mfentry: | 2376 if mfentry: |
2377 handle(add, True) | 2377 handle(actions['add'], True) |
2378 elif exact: | 2378 elif exact: |
2379 ui.warn(_('file not managed: %s\n') % rel) | 2379 ui.warn(_('file not managed: %s\n') % rel) |
2380 continue | 2380 continue |
2381 | 2381 |
2382 # parent is target, no changes mean no changes | 2382 # parent is target, no changes mean no changes |
2392 if abs in pmf and mfentry: | 2392 if abs in pmf and mfentry: |
2393 # if version of file is same in parent and target | 2393 # if version of file is same in parent and target |
2394 # manifests, do nothing | 2394 # manifests, do nothing |
2395 if (pmf[abs] != mfentry or | 2395 if (pmf[abs] != mfentry or |
2396 pmf.flags(abs) != mf.flags(abs)): | 2396 pmf.flags(abs) != mf.flags(abs)): |
2397 handle(revert, False) | 2397 handle(actions['revert'], False) |
2398 else: | 2398 else: |
2399 handle(remove, False) | 2399 handle(actions['remove'], False) |
2400 | |
2400 if not opts.get('dry_run'): | 2401 if not opts.get('dry_run'): |
2401 _performrevert(repo, parents, ctx, revert, add, remove, undelete) | 2402 _performrevert(repo, parents, ctx, actions) |
2402 | 2403 |
2403 if targetsubs: | 2404 if targetsubs: |
2404 # Revert the subrepos on the revert list | 2405 # Revert the subrepos on the revert list |
2405 for sub in targetsubs: | 2406 for sub in targetsubs: |
2406 ctx.sub(sub).revert(ui, ctx.substate[sub], *pats, **opts) | 2407 ctx.sub(sub).revert(ui, ctx.substate[sub], *pats, **opts) |
2407 finally: | 2408 finally: |
2408 wlock.release() | 2409 wlock.release() |
2409 | 2410 |
2410 def _performrevert(repo, parents, ctx, revert, add, remove, undelete): | 2411 def _performrevert(repo, parents, ctx, actions): |
2411 """function that actually perform all the action computed for revert | 2412 """function that actually perform all the actions computed for revert |
2412 | 2413 |
2413 This is an independent function to let extension to plug in and react to | 2414 This is an independent function to let extension to plug in and react to |
2414 the imminent revert. | 2415 the imminent revert. |
2415 | 2416 |
2416 Make sure you have the working directory locked when calling this function. | 2417 Make sure you have the working directory locked when calling this function. |
2420 def checkout(f): | 2421 def checkout(f): |
2421 fc = ctx[f] | 2422 fc = ctx[f] |
2422 repo.wwrite(f, fc.data(), fc.flags()) | 2423 repo.wwrite(f, fc.data(), fc.flags()) |
2423 | 2424 |
2424 audit_path = pathutil.pathauditor(repo.root) | 2425 audit_path = pathutil.pathauditor(repo.root) |
2425 for f in remove[0]: | 2426 for f in actions['remove'][0]: |
2426 if repo.dirstate[f] == 'a': | 2427 if repo.dirstate[f] == 'a': |
2427 repo.dirstate.drop(f) | 2428 repo.dirstate.drop(f) |
2428 continue | 2429 continue |
2429 audit_path(f) | 2430 audit_path(f) |
2430 try: | 2431 try: |
2440 # merges to avoid losing information about merged/dirty files. | 2441 # merges to avoid losing information about merged/dirty files. |
2441 if p2 != nullid: | 2442 if p2 != nullid: |
2442 normal = repo.dirstate.normallookup | 2443 normal = repo.dirstate.normallookup |
2443 else: | 2444 else: |
2444 normal = repo.dirstate.normal | 2445 normal = repo.dirstate.normal |
2445 for f in revert[0]: | 2446 for f in actions['revert'][0]: |
2446 checkout(f) | 2447 checkout(f) |
2447 if normal: | 2448 if normal: |
2448 normal(f) | 2449 normal(f) |
2449 | 2450 |
2450 for f in add[0]: | 2451 for f in actions['add'][0]: |
2451 checkout(f) | 2452 checkout(f) |
2452 repo.dirstate.add(f) | 2453 repo.dirstate.add(f) |
2453 | 2454 |
2454 normal = repo.dirstate.normallookup | 2455 normal = repo.dirstate.normallookup |
2455 if node == parent and p2 == nullid: | 2456 if node == parent and p2 == nullid: |
2456 normal = repo.dirstate.normal | 2457 normal = repo.dirstate.normal |
2457 for f in undelete[0]: | 2458 for f in actions['undelete'][0]: |
2458 checkout(f) | 2459 checkout(f) |
2459 normal(f) | 2460 normal(f) |
2460 | 2461 |
2461 copied = copies.pathcopies(repo[parent], ctx) | 2462 copied = copies.pathcopies(repo[parent], ctx) |
2462 | 2463 |
2463 for f in add[0] + undelete[0] + revert[0]: | 2464 for f in actions['add'][0] + actions['undelete'][0] + actions['revert'][0]: |
2464 if f in copied: | 2465 if f in copied: |
2465 repo.dirstate.copy(copied[f], f) | 2466 repo.dirstate.copy(copied[f], f) |
2466 | 2467 |
2467 def command(table): | 2468 def command(table): |
2468 '''returns a function object bound to table which can be used as | 2469 '''returns a function object bound to table which can be used as |