[PATCH 3 of 5] histedit: break _histedit function into smaller pieces (add _getgoal and _validateargs)

Kostia Balytskyi ikostia at fb.com
Mon Feb 1 17:54:02 UTC 2016


# HG changeset patch
# User Kostia Balytskyi <ikostia at fb.com>
# Date 1454345734 0
#      Mon Feb 01 16:55:34 2016 +0000
# Branch stable
# Node ID 4118b5fdc4a1fc9acc4a8f6580edb7ea6dd2adcf
# Parent  2728f0ff3f167be2489fd8857fac536a4d937883
histedit: break _histedit function into smaller pieces (add _getgoal and _validateargs)

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -973,6 +973,70 @@
     finally:
         release(state.lock, state.wlock)
 
+class Goal(object):
+    CONTINUE = 'continue'
+    ABORT = 'abort'
+    EDITPLAN = 'edit-plan'
+    NEW = 'new'
+
+def _getgoal(opts):
+    if opts.get('continue'):
+        return Goal.CONTINUE
+    elif opts.get('abort'):
+        return Goal.ABORT
+    elif opts.get('edit_plan'):
+        return Goal.EDITPLAN
+    return Goal.NEW
+
+def _validateargs(ui, repo, state, goal, freeargs, opts):
+    # TODO only abort if we try to histedit mq patches, not just
+    # blanket if mq patches are applied somewhere
+    mq = getattr(repo, 'mq', None)
+    if mq and mq.applied:
+        raise error.Abort(_('source has mq patches applied'))
+
+    outg = opts.get('outgoing')
+    editplan = opts.get('edit_plan')
+    abort = opts.get('abort')
+    force = opts.get('force')
+    rules = opts.get('commands', '')
+    revs = opts.get('rev', [])
+    if force and not outg:
+        raise error.Abort(_('--force only allowed with --outgoing'))
+
+    if Goal.CONTINUE == goal:
+        if any((outg, abort, revs, freeargs, rules, editplan)):
+            raise error.Abort(_('no arguments allowed with --continue'))
+    elif Goal.ABORT == goal:
+        if any((outg, revs, freeargs, rules, editplan)):
+            raise error.Abort(_('no arguments allowed with --abort'))
+    elif Goal.EDITPLAN == goal:
+        if any((outg, revs, freeargs)):
+            raise error.Abort(_('only --commands argument allowed with '
+                               '--edit-plan'))
+    elif Goal.NEW == goal:
+        if os.path.exists(os.path.join(repo.path, 'histedit-state')):
+            raise error.Abort(_('history edit already in progress, try '
+                               '--continue or --abort'))
+        if outg and revs:
+            raise error.Abort(_('no revisions allowed with --outgoing'))
+        if outg and len(freeargs) > 1:
+            raise error.Abort(
+                _('only one repo argument allowed with --outgoing'))
+        if not outg:
+            # extending passed revs and validating total rev number
+            revs.extend(freeargs)
+            if len(revs) == 0:
+                defaultrev = destutil.desthistedit(ui, repo)
+                if defaultrev is not None:
+                    revs.append(defaultrev)
+
+            if len(revs) != 1:
+                raise error.Abort(
+                    _('histedit requires exactly one ancestor revision'))
+
+    return revs
+
 def _histedit(ui, repo, state, *freeargs, **opts):
     # TODO only abort if we try to histedit mq patches, not just
     # blanket if mq patches are applied somewhere
@@ -988,43 +1052,10 @@
     force = opts.get('force')
     rules = opts.get('commands', '')
     revs = opts.get('rev', [])
-    goal = 'new' # This invocation goal, in new, continue, abort
-    if force and not outg:
-        raise error.Abort(_('--force only allowed with --outgoing'))
-    if cont:
-        if any((outg, abort, revs, freeargs, rules, editplan)):
-            raise error.Abort(_('no arguments allowed with --continue'))
-        goal = 'continue'
-    elif abort:
-        if any((outg, revs, freeargs, rules, editplan)):
-            raise error.Abort(_('no arguments allowed with --abort'))
-        goal = 'abort'
-    elif editplan:
-        if any((outg, revs, freeargs)):
-            raise error.Abort(_('only --commands argument allowed with '
-                               '--edit-plan'))
-        goal = 'edit-plan'
-    else:
-        if os.path.exists(os.path.join(repo.path, 'histedit-state')):
-            raise error.Abort(_('history edit already in progress, try '
-                               '--continue or --abort'))
-        if outg:
-            if revs:
-                raise error.Abort(_('no revisions allowed with --outgoing'))
-            if len(freeargs) > 1:
-                raise error.Abort(
-                    _('only one repo argument allowed with --outgoing'))
-        else:
-            revs.extend(freeargs)
-            if len(revs) == 0:
-                defaultrev = destutil.desthistedit(ui, repo)
-                if defaultrev is not None:
-                    revs.append(defaultrev)
 
-            if len(revs) != 1:
-                raise error.Abort(
-                    _('histedit requires exactly one ancestor revision'))
-
+    # This invocation goal, in new, continue, abort, edit-plan
+    goal = _getgoal(opts)
+    revs = _validateargs(ui, repo, state, goal, freeargs, opts)
 
     replacements = []
     state.keep = opts.get('keep', False)


More information about the Mercurial-devel mailing list