D6723: fix: allow tools to use :linerange, but also run if a file is unchanged

hooper (Danny Hooper) phabricator at mercurial-scm.org
Mon Aug 12 23:40:26 UTC 2019


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

REVISION SUMMARY
  The definition of "unchanged" here is subtle, because pure deletion diff hunks
  are ignored. That means this is different from using the --whole flag. This
  change allows you to configure, for example, a code formatter that:
  
  1. Formats specific line ranges if specified via flags
  2. Does not format the entire file when there are no line ranges provided
  3. Performs some other kind of formatting regardless of provided line ranges
  
  This sounds a little far fetched, but it is meant to address a specific corner
  case encountered in Google's use of the fix extension. The default behavior is
  kept because it exists to prevent mistakes that could erase uncommitted
  changes.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  hgext/fix.py
  tests/test-fix.t

CHANGE DETAILS

diff --git a/tests/test-fix.t b/tests/test-fix.t
--- a/tests/test-fix.t
+++ b/tests/test-fix.t
@@ -147,6 +147,15 @@
     {first}   The 1-based line number of the first line in the modified range
     {last}    The 1-based line number of the last line in the modified range
   
+  Deleted sections of a file will be ignored by :linerange, because there is no
+  corresponding line range in the version being fixed.
+  
+  By default, tools that set :linerange will only be executed if there is at
+  least one changed line range. This is meant to prevent accidents like running
+  a code formatter in such a way that it unexpectedly reformats the whole file.
+  If such a tool needs to operate on unchanged files, it should set the
+  :skipclean suboption to false.
+  
   The :pattern suboption determines which files will be passed through each
   configured tool. See 'hg help patterns' for possible values. If there are file
   arguments to 'hg fix', the intersection of these patterns is used.
diff --git a/hgext/fix.py b/hgext/fix.py
--- a/hgext/fix.py
+++ b/hgext/fix.py
@@ -36,6 +36,15 @@
   {first}   The 1-based line number of the first line in the modified range
   {last}    The 1-based line number of the last line in the modified range
 
+Deleted sections of a file will be ignored by :linerange, because there is no
+corresponding line range in the version being fixed.
+
+By default, tools that set :linerange will only be executed if there is at least
+one changed line range. This is meant to prevent accidents like running a code
+formatter in such a way that it unexpectedly reformats the whole file. If such a
+tool needs to operate on unchanged files, it should set the :skipclean suboption
+to false.
+
 The :pattern suboption determines which files will be passed through each
 configured tool. See :hg:`help patterns` for possible values. If there are file
 arguments to :hg:`fix`, the intersection of these patterns is used.
@@ -162,6 +171,7 @@
     'pattern': None,
     'priority': 0,
     'metadata': False,
+    'skipclean': True,
 }
 
 for key, default in FIXER_ATTRS.items():
@@ -756,7 +766,7 @@
                         {'rootpath': path, 'basename': os.path.basename(path)})]
         if self._linerange:
             ranges = rangesfn()
-            if not ranges:
+            if self._skipclean and not ranges:
                 # No line ranges to fix, so don't run the fixer.
                 return None
             for first, last in ranges:



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


More information about the Mercurial-devel mailing list