Patchwork D8100: help: add a mechanism to change flags' help depending on config

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

Comments

phabricator - Feb. 10, 2020, 12:12 a.m.
valentin.gatienbaron created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  It seems reasonable to have a similar mechanism for the rest of the
  help, but no such thing is implemented.
  
  The goal is to make the help of commands clearer in the presence of
  significant default changes, like tweakdefaults or with company-wide
  hgrcs. In these cases, a user looking at the help of a command doesn't
  exactly know what his hgrc is doing.
  
  Apply to this to the --git option of commands that display diffs, as
  this option in particular causes confusion for some reason.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/cmdutil.py
  mercurial/help.py
  tests/test-help.t

CHANGE DETAILS




To: valentin.gatienbaron, #hg-reviewers
Cc: mercurial-devel
phabricator - Feb. 10, 2020, 9:43 p.m.
This revision is now accepted and ready to land.
durin42 added a comment.
durin42 accepted this revision.


  I'm a huge fan.

REPOSITORY
  rHG Mercurial

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D8100/new/

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

To: valentin.gatienbaron, #hg-reviewers, durin42
Cc: durin42, mercurial-devel

Patch

diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -788,6 +788,12 @@ 
   (use 'hg help extensions' for information on enabling extensions)
   [255]
 
+Checking that help adapts based on the config:
+
+  $ hg help diff --config ui.tweakdefaults=true | egrep -e '^ *(-g|config)'
+   -g --[no-]git            use git extended diff format (default: on from
+                            config)
+
 Make sure that we don't run afoul of the help system thinking that
 this is a section and erroring out weirdly.
 
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -152,8 +152,17 @@ 
     doc = b''.join(rst)
     return doc
 
+def parsedefaultmarker(text):
+    """given a text 'abc (DEFAULT: def.ghi)',
+    returns (b'abc', (b'def', b'ghi')). Otherwise return None"""
+    if text[-1:] == b')':
+        marker = b' (DEFAULT: '
+        pos = text.find(marker)
+        if pos >= 0:
+            item = text[pos + len(marker):-1]
+            return text[:pos], item.split(b'.', 2)
 
-def optrst(header, options, verbose):
+def optrst(header, options, verbose, ui):
     data = []
     multioccur = False
     for option in options:
@@ -165,7 +174,14 @@ 
 
         if not verbose and any(w in desc for w in _exclkeywords):
             continue
-
+        defaultstrsuffix = b''
+        if default is None:
+            parseresult = parsedefaultmarker(desc)
+            if parseresult is not None:
+                (desc, (section, name)) = parseresult
+                if ui.configbool(section, name):
+                    default = True
+                    defaultstrsuffix = _(b' from config')
         so = b''
         if shortopt:
             so = b'-' + shortopt
@@ -183,7 +199,7 @@ 
             defaultstr = pycompat.bytestr(default)
             if default is True:
                 defaultstr = _(b"on")
-            desc += _(b" (default: %s)") % defaultstr
+            desc += _(b" (default: %s)") % (defaultstr + defaultstrsuffix)
 
         if isinstance(default, list):
             lo += b" %s [+]" % optlabel
@@ -714,11 +730,11 @@ 
 
         # options
         if not ui.quiet and entry[1]:
-            rst.append(optrst(_(b"options"), entry[1], ui.verbose))
+            rst.append(optrst(_(b"options"), entry[1], ui.verbose, ui))
 
         if ui.verbose:
             rst.append(
-                optrst(_(b"global options"), commands.globalopts, ui.verbose)
+                optrst(_(b"global options"), commands.globalopts, ui.verbose, ui)
             )
 
         if not ui.verbose:
@@ -858,7 +874,7 @@ 
         elif ui.verbose:
             rst.append(
                 b'\n%s\n'
-                % optrst(_(b"global options"), commands.globalopts, ui.verbose)
+                % optrst(_(b"global options"), commands.globalopts, ui.verbose, ui)
             )
             if name == b'shortlist':
                 rst.append(
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -170,7 +170,7 @@ 
 
 diffopts = [
     (b'a', b'text', None, _(b'treat all files as text')),
-    (b'g', b'git', None, _(b'use git extended diff format')),
+    (b'g', b'git', None, _(b'use git extended diff format (DEFAULT: diff.git)')),
     (b'', b'binary', None, _(b'generate binary diffs in git mode (default)')),
     (b'', b'nodates', None, _(b'omit dates from diff headers')),
 ]