D6356: mdiff: prepare mdiff to be used for run-tests to replace unidiff

sangeet259 (Sangeet Kumar Mishra) phabricator at mercurial-scm.org
Fri May 10 13:28:07 EDT 2019


sangeet259 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Patch 1/2
  
  This patch adds some functions to mdiff.py that will be called from run-tests.py
  which will be added in the second part of this patch series.
  
  The future changes to run-tests.py will enable one to use mdiif
  to diff rather than unidiff whenever the mercurial is installed in the system.
  
  Why do I need to split the patches into two ?
  The reason to split what would have been a single patch into two is because
  for the next patch to be able to use the mdiff during tests,
  this revision has to be there in the system's mercurial installation.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D6356

AFFECTED FILES
  mercurial/mdiff.py

CHANGE DETAILS

diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -7,6 +7,8 @@
 
 from __future__ import absolute_import
 
+import datetime
+import itertools
 import re
 import struct
 import zlib
@@ -525,3 +527,37 @@
 
 def replacediffheader(oldlen, newlen):
     return struct.pack(">lll", 0, oldlen, newlen)
+
+def prepare_mdiff(expected, output):
+    """Prepare the inputs for the mdiff.unidiff function"""
+    date1 = datetime.datetime.now().strftime("%a %b %d %y %H:%M:%S %Y %z")
+    date2 = date1
+    # join all the different elements into a single string
+    # to regenerate that file
+    exp = "".join(expected)
+    out = "".join(output)
+    opts = diffopts(noprefix=True)
+    return exp, date1, out, date2, opts
+
+def process_mdiff(uheaders, hunks):
+    """Process the output of mdiff into a list of lines,
+    to be used by getdiff"""
+    # the hunklines are in the hunks generator
+    # get the hunklines from the (hunkrange,hunklines) tuple
+    hunklines = [x[1] for x in hunks]
+    # extract and insert the headers at the beginnng of the hunklines list
+    headers = [uheaders[0].split("\t")[0]+"\n", uheaders[1].split("\t")[0]+"\n"]
+    hunklines.insert(0, headers)
+    difflines = itertools.chain.from_iterable(hunklines)
+    return difflines
+
+def new_diff(expected, output, ref, err):
+    """
+    The API of new_diff is designed to be same as difflib.unified_diff.
+    This is done for backwards compatibility and resuing existing code.
+    """
+    exp, date1, out, date2, opts = prepare_mdiff(expected, output)
+    uheaders, hunks = unidiff(exp, date1, out, date2,
+                            ref, err, binary=False, opts=opts)
+    difflines = process_mdiff(uheaders, hunks)
+    return difflines



To: sangeet259, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list