fetch: linearize code by eliminating nested functions
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 28 Aug 2008 11:19:17 +0200
changeset 6941 b2bc2d984bac
parent 6940 05ec27530d04
child 6942 5423224c7913
fetch: linearize code by eliminating nested functions
hgext/fetch.py
--- a/hgext/fetch.py	Tue Aug 26 00:04:18 2008 +0200
+++ b/hgext/fetch.py	Thu Aug 28 11:19:17 2008 +0200
@@ -28,17 +28,58 @@
     See 'hg help dates' for a list of formats valid for -d/--date.
     '''
 
-    def postincoming(other, modheads):
+    date = opts.get('date')
+    if date:
+        opts['date'] = util.parsedate(date)
+
+    parent, p2 = repo.dirstate.parents()
+    if parent != repo.changelog.tip():
+        raise util.Abort(_('working dir not at tip '
+                           '(use "hg update" to check out tip)'))
+
+    if p2 != nullid:
+        raise util.Abort(_('outstanding uncommitted merge'))
+
+    wlock = lock = None
+    try:
+        wlock = repo.wlock()
+        lock = repo.lock()
+        mod, add, rem, del_ = repo.status()[:4]
+
+        if mod or add or rem:
+            raise util.Abort(_('outstanding uncommitted changes'))
+        if del_:
+            raise util.Abort(_('working directory is missing some files'))
+        if len(repo.heads()) > 1:
+            raise util.Abort(_('multiple heads in this repository '
+                               '(use "hg heads" and "hg merge" to merge)'))
+
+        cmdutil.setremoteconfig(ui, opts)
+
+        other = hg.repository(ui, ui.expandpath(source))
+        ui.status(_('pulling from %s\n') %
+                  util.hidepassword(ui.expandpath(source)))
+        revs = None
+        if opts['rev']:
+            if not other.local():
+                raise util.Abort(_("fetch -r doesn't work for remote "
+                                   "repositories yet"))
+            else:
+                revs = [other.lookup(rev) for rev in opts['rev']]
+
+        modheads = repo.pull(other, heads=revs)
         if modheads == 0:
             return 0
         if modheads == 1:
             return hg.clean(repo, repo.changelog.tip())
+
         newheads = repo.heads(parent)
         newchildren = [n for n in repo.heads(parent) if n != parent]
         newparent = parent
         if newchildren:
             newparent = newchildren[0]
             hg.clean(repo, newparent)
+
         newheads = [n for n in repo.heads() if n != newparent]
         if len(newheads) > 1:
             ui.status(_('not merging with %d other new heads '
@@ -46,6 +87,7 @@
                       (len(newheads) - 1))
             return
         err = False
+
         if newheads:
             # By default, we consider the repository we're pulling
             # *from* as authoritative, so we merge our changes into
@@ -61,6 +103,7 @@
             ui.status(_('merging with %d:%s\n') %
                       (repo.changelog.rev(secondparent), short(secondparent)))
             err = hg.merge(repo, secondparent, remind=False)
+
         if not err:
             mod, add, rem = repo.status()[:3]
             message = (cmdutil.logmessage(opts) or
@@ -74,45 +117,6 @@
                         'with local\n') % (repo.changelog.rev(n),
                                            short(n)))
 
-    def pull():
-        cmdutil.setremoteconfig(ui, opts)
-
-        other = hg.repository(ui, ui.expandpath(source))
-        ui.status(_('pulling from %s\n') %
-                  util.hidepassword(ui.expandpath(source)))
-        revs = None
-        if opts['rev']:
-            if not other.local():
-                raise util.Abort(_("fetch -r doesn't work for remote "
-                                   "repositories yet"))
-            else:
-                revs = [other.lookup(rev) for rev in opts['rev']]
-        modheads = repo.pull(other, heads=revs)
-        return postincoming(other, modheads)
-
-    date = opts.get('date')
-    if date:
-        opts['date'] = util.parsedate(date)
-
-    parent, p2 = repo.dirstate.parents()
-    if parent != repo.changelog.tip():
-        raise util.Abort(_('working dir not at tip '
-                           '(use "hg update" to check out tip)'))
-    if p2 != nullid:
-        raise util.Abort(_('outstanding uncommitted merge'))
-    wlock = lock = None
-    try:
-        wlock = repo.wlock()
-        lock = repo.lock()
-        mod, add, rem, del_ = repo.status()[:4]
-        if mod or add or rem:
-            raise util.Abort(_('outstanding uncommitted changes'))
-        if del_:
-            raise util.Abort(_('working directory is missing some files'))
-        if len(repo.heads()) > 1:
-            raise util.Abort(_('multiple heads in this repository '
-                               '(use "hg heads" and "hg merge" to merge)'))
-        return pull()
     finally:
         del lock, wlock