Patchwork [11,of,11] commitctx: extract all the file preparation logic in a new function

login
register
mail settings
Submitter Pierre-Yves David
Date July 24, 2020, 2:38 p.m.
Message ID <68263067cc8120b2f643.1595601516@nodosa.octobus.net>
Download mbox | patch
Permalink /patch/46872/
State Accepted
Headers show

Comments

Pierre-Yves David - July 24, 2020, 2:38 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1595541151 -7200
#      Thu Jul 23 23:52:31 2020 +0200
# Node ID 68263067cc8120b2f6434ed282147966b67e7214
# Parent  8c926a9ea89ea18ba068fd59314b73d7f6f7db27
# EXP-Topic commitctx-cleanup-2
# Available At https://foss.heptapod.net/octobus/mercurial-devel/
#              hg pull https://foss.heptapod.net/octobus/mercurial-devel/ -r 68263067cc81
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.

Patch

diff --git a/mercurial/commit.py b/mercurial/commit.py
--- a/mercurial/commit.py
+++ b/mercurial/commit.py
@@ -64,43 +64,8 @@  def commitctx(repo, ctx, error=False, or
     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 @@  def commitctx(repo, ctx, error=False, or
         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()