Patchwork [08,of,15] commitctx: extract sidedata encoding inside its own function

login
register
mail settings
Submitter Pierre-Yves David
Date July 29, 2020, 4:57 p.m.
Message ID <72d5e455f4d09cf58d10.1596041858@nodosa.octobus.net>
Download mbox | patch
Permalink /patch/46928/
State Accepted
Headers show

Comments

Pierre-Yves David - July 29, 2020, 4:57 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1595685746 -7200
#      Sat Jul 25 16:02:26 2020 +0200
# Node ID 72d5e455f4d09cf58d1094120a6528a8591e614b
# Parent  a728c320f0d7c71c733524969e728e533e2d34d6
# EXP-Topic files-change
# Available At https://foss.heptapod.net/octobus/mercurial-devel/
#              hg pull https://foss.heptapod.net/octobus/mercurial-devel/ -r 72d5e455f4d0
commitctx: extract sidedata encoding inside its own function

This part of the code is quite independent from the rest. Thank to the new
ChangingFiles object, moving with the rest of the sidedata code (in metadata.py)
is simple.

The changelog.add method is simply passing the `files` object to the new
function. It will be easy to increase/change the data we gather and encode
without impacting the changelog method.

Patch

diff --git a/mercurial/changelog.py b/mercurial/changelog.py
--- a/mercurial/changelog.py
+++ b/mercurial/changelog.py
@@ -558,27 +558,7 @@  class changelog(revlog.revlog):
         sortedfiles = sorted(files.touched)
         sidedata = None
         if self._copiesstorage == b'changeset-sidedata':
-            sidedata = {}
-            p1copies = files.copied_from_p1
-            if p1copies:
-                p1copies = metadata.encodecopies(sortedfiles, p1copies)
-                sidedata[sidedatamod.SD_P1COPIES] = p1copies
-            p2copies = files.copied_from_p2
-            if p2copies:
-                p2copies = metadata.encodecopies(sortedfiles, p2copies)
-                sidedata[sidedatamod.SD_P2COPIES] = p2copies
-            filesadded = files.added
-            if filesadded:
-                filesadded = metadata.encodefileindices(sortedfiles, filesadded)
-                sidedata[sidedatamod.SD_FILESADDED] = filesadded
-            filesremoved = files.removed
-            if filesremoved:
-                filesremoved = metadata.encodefileindices(
-                    sortedfiles, filesremoved
-                )
-                sidedata[sidedatamod.SD_FILESREMOVED] = filesremoved
-            if not sidedata:
-                sidedata = None
+            sidedata = metadata.encode_copies_sidedata(files)
 
         if extra:
             extra = encodeextra(extra)
diff --git a/mercurial/metadata.py b/mercurial/metadata.py
--- a/mercurial/metadata.py
+++ b/mercurial/metadata.py
@@ -254,6 +254,30 @@  def decodefileindices(files, data):
         return None
 
 
+def encode_copies_sidedata(files):
+    sortedfiles = sorted(files.touched)
+    sidedata = {}
+    p1copies = files.copied_from_p1
+    if p1copies:
+        p1copies = encodecopies(sortedfiles, p1copies)
+        sidedata[sidedatamod.SD_P1COPIES] = p1copies
+    p2copies = files.copied_from_p2
+    if p2copies:
+        p2copies = encodecopies(sortedfiles, p2copies)
+        sidedata[sidedatamod.SD_P2COPIES] = p2copies
+    filesadded = files.added
+    if filesadded:
+        filesadded = encodefileindices(sortedfiles, filesadded)
+        sidedata[sidedatamod.SD_FILESADDED] = filesadded
+    filesremoved = files.removed
+    if filesremoved:
+        filesremoved = encodefileindices(sortedfiles, filesremoved)
+        sidedata[sidedatamod.SD_FILESREMOVED] = filesremoved
+    if not sidedata:
+        sidedata = None
+    return sidedata
+
+
 def _getsidedata(srcrepo, rev):
     ctx = srcrepo[rev]
     filescopies = computechangesetcopies(ctx)