Patchwork D10001: patch: make diff --git to differentiate b/w file is empty or doesn't exists

login
register
mail settings
Submitter phabricator
Date Feb. 16, 2021, 10:27 a.m.
Message ID <differential-rev-PHID-DREV-jrhw2ssofko6kxfwllka-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48316/
State Superseded
Headers show

Comments

phabricator - Feb. 16, 2021, 10:27 a.m.
khanchi97 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Before this patch, as we didn't differentiate the two cases of a file in a context:
  
  1. File doesn't exists
  2. File is empty
  
  which causes the blob id to be same for both the cases.
  
  Now we use `nullhex` for a file which doesn't exists in a context (aligning it with
  the git diff format)
  
  Changes in test file reflect the fixed behavior.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/patch.py
  tests/test-diff-unified.t

CHANGE DETAILS




To: khanchi97, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-diff-unified.t b/tests/test-diff-unified.t
--- a/tests/test-diff-unified.t
+++ b/tests/test-diff-unified.t
@@ -467,7 +467,7 @@ 
   $ hg ci -Am "empty a"
   adding a
   $ hg diff -c 0 --git --config experimental.extendedheader.index=full | grep index
-  index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644
+  index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644
 
 Make sure `hg diff --git` differentiate "file was empty" and "file is removed"
 for git blob oids
@@ -476,6 +476,6 @@ 
   $ hg ci -Am "removed a"
   removing a
   $ hg diff -c 1 --git --config experimental.extendedheader.index=full | grep index
-  index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644
+  index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 100644
 
   $ cd ..
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -20,6 +20,7 @@ 
 from .i18n import _
 from .node import (
     hex,
+    nullhex,
     short,
 )
 from .pycompat import open
@@ -3099,11 +3100,13 @@ 
 
     ctx1, fctx1, path1, flag1, content1, date1 = data1
     ctx2, fctx2, path2, flag2, content2, date2 = data2
+    index1 = _gitindex(content1) if path1 in ctx1 else nullhex
+    index2 = _gitindex(content2) if path2 in ctx2 else nullhex
     if binary and opts.git and not opts.nobinary:
         text = mdiff.b85diff(content1, content2)
         if text:
             header.append(
-                b'index %s..%s' % (_gitindex(content1), _gitindex(content2))
+                b'index %s..%s' % (index1, index2)
             )
         hunks = ((None, [text]),)
     else:
@@ -3114,8 +3117,8 @@ 
             header.append(
                 b'index %s..%s %s'
                 % (
-                    _gitindex(content1)[0 : opts.index],
-                    _gitindex(content2)[0 : opts.index],
+                    index1[0 : opts.index],
+                    index2[0 : opts.index],
                     _gitmode[flag],
                 )
             )