commitctx: extract all the file preparation logic in a new function
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 23 Jul 2020 23:52:31 +0200
changeset 45239 13814622b3b1
parent 45238 bd7515273fd6
child 45241 e0bfde04f957
commitctx: extract all the file preparation logic in a new function Before we actually start to create a new commit we have a large block of logic that do the necessary file and manifest commit and that determine which files are been affected by the commit (and how). This is a complex process on its own. It return a "simple" output that can be fed to the next step. The output itself is not that simple as we return a lot of individual items (files, added, removed, ...). My next step (and actual goal for this cleanup) will be to simplify the return by returning a richer object that will be more suited for the variation of data we want to store. After this changeset the `commitctx` is a collection of smaller function with limited scope. The largest one is still `_filecommit` without about 100 lines of code.
mercurial/commit.py
--- a/mercurial/commit.py	Thu Jul 23 23:58:23 2020 +0200
+++ b/mercurial/commit.py	Thu Jul 23 23:52:31 2020 +0200
@@ -64,43 +64,8 @@
     user = ctx.user()
 
     with repo.lock(), repo.transaction(b"commit") as tr:
-        writechangesetcopy, writefilecopymeta = _write_copy_meta(repo)
-
-        p1copies, p2copies = None, None
-        if writechangesetcopy:
-            p1copies = ctx.p1copies()
-            p2copies = ctx.p2copies()
-        filesadded, filesremoved = None, None
-        if ctx.manifestnode():
-            # reuse an existing manifest revision
-            repo.ui.debug(b'reusing known manifest\n')
-            mn = ctx.manifestnode()
-            files = ctx.files()
-            if writechangesetcopy:
-                filesadded = ctx.filesadded()
-                filesremoved = ctx.filesremoved()
-        elif not ctx.files():
-            repo.ui.debug(b'reusing manifest from p1 (no file change)\n')
-            mn = p1.manifestnode()
-            files = []
-        else:
-            mn, files, added, removed = _process_files(tr, ctx, error=error)
-            if writechangesetcopy:
-                filesremoved = removed
-                filesadded = added
-
-        if origctx and origctx.manifestnode() == mn:
-            files = origctx.files()
-
-        if not writefilecopymeta:
-            # If writing only to changeset extras, use None to indicate that
-            # no entry should be written. If writing to both, write an empty
-            # entry to prevent the reader from falling back to reading
-            # filelogs.
-            p1copies = p1copies or None
-            p2copies = p2copies or None
-            filesadded = filesadded or None
-            filesremoved = filesremoved or None
+        r = _prepare_files(tr, ctx, error=error, origctx=origctx)
+        mn, files, p1copies, p2copies, filesadded, filesremoved = r
 
         # update changelog
         repo.ui.note(_(b"committing changelog\n"))
@@ -136,6 +101,51 @@
         return n
 
 
+def _prepare_files(tr, ctx, error=False, origctx=None):
+    repo = ctx.repo()
+    p1 = ctx.p1()
+
+    writechangesetcopy, writefilecopymeta = _write_copy_meta(repo)
+
+    p1copies, p2copies = None, None
+    if writechangesetcopy:
+        p1copies = ctx.p1copies()
+        p2copies = ctx.p2copies()
+    filesadded, filesremoved = None, None
+    if ctx.manifestnode():
+        # reuse an existing manifest revision
+        repo.ui.debug(b'reusing known manifest\n')
+        mn = ctx.manifestnode()
+        files = ctx.files()
+        if writechangesetcopy:
+            filesadded = ctx.filesadded()
+            filesremoved = ctx.filesremoved()
+    elif not ctx.files():
+        repo.ui.debug(b'reusing manifest from p1 (no file change)\n')
+        mn = p1.manifestnode()
+        files = []
+    else:
+        mn, files, added, removed = _process_files(tr, ctx, error=error)
+        if writechangesetcopy:
+            filesremoved = removed
+            filesadded = added
+
+    if origctx and origctx.manifestnode() == mn:
+        files = origctx.files()
+
+    if not writefilecopymeta:
+        # If writing only to changeset extras, use None to indicate that
+        # no entry should be written. If writing to both, write an empty
+        # entry to prevent the reader from falling back to reading
+        # filelogs.
+        p1copies = p1copies or None
+        p2copies = p2copies or None
+        filesadded = filesadded or None
+        filesremoved = filesremoved or None
+
+    return mn, files, p1copies, p2copies, filesadded, filesremoved
+
+
 def _process_files(tr, ctx, error=False):
     repo = ctx.repo()
     p1 = ctx.p1()