configwizard: add support for setting optimal diff settings (bug 1277406); r?glob draft
authorGregory Szorc <gps@mozilla.com>
Wed, 01 Jun 2016 13:14:15 -0700
changeset 8494 12a4c9f1a7b69ce88e2595536f3fa846a6fd51c4
parent 8493 a745dd39c70a77643db360eb5019e32ab9c56457
child 8495 5f699400f28ab997810d583e4a0db23eda3c095c
push id918
push userbmo:gps@mozilla.com
push dateThu, 09 Jun 2016 19:23:31 +0000
reviewersglob
bugs1277406
configwizard: add support for setting optimal diff settings (bug 1277406); r?glob This is almost a straight port of the legacy wizard. We dropped setting diff.unified=8 because this is holdover from the days when we exported patches to Bugzilla after `hg export`. These days we have tools to submit to MozReview and attach to Bugzilla. These tools should provide appropriate context. But diff.git and diff.showfunc are generally useful, so keep them enabled. MozReview-Commit-ID: FMJhkEMaWNk
hgext/configwizard/__init__.py
hgext/configwizard/hgsetup/config.py
hgext/configwizard/hgsetup/wizard.py
hgext/configwizard/tests/test-diff.t
--- a/hgext/configwizard/__init__.py
+++ b/hgext/configwizard/__init__.py
@@ -58,25 +58,32 @@ to author commits, you'll need to define
 
 This data will be publicly available when you send commits/patches to others.
 If you aren't comfortable giving us your full name, pseudonames are
 acceptable.
 
 (Relevant config option: ui.username)
 '''.lstrip()
 
+BAD_DIFF_SETTINGS = '''
+Mercurial is not configured to produce diffs in a more readable format.
+
+Would you like to change this (Yn)? $$ &Yes $$ &No
+'''.strip()
+
 testedwith = '3.5 3.6 3.7 3.8'
 buglink = 'https://bugzilla.mozilla.org/enter_bug.cgi?product=Developer%20Services&component=General'
 
 cmdtable = {}
 command = cmdutil.command(cmdtable)
 
 wizardsteps = {
     'hgversion',
     'username',
+    'diff',
     'configchange',
 }
 
 @command('configwizard', [
     ('s', 'statedir', '', _('directory to store state')),
     ], _('hg configwizard'), optionalrepo=True)
 def configwizard(ui, repo, statedir=None, **opts):
     """Ensure your Mercurial configuration is up to date."""
@@ -98,16 +105,19 @@ def configwizard(ui, repo, statedir=None
 
     if 'hgversion' in runsteps:
         if _checkhgversion(ui, hgversion):
             return 1
 
     if 'username' in runsteps:
         _checkusername(ui, cw)
 
+    if 'diff' in runsteps:
+        _checkdiffsettings(ui, cw)
+
     if 'configchange' in runsteps:
         return _handleconfigchange(ui, cw)
 
     return 0
 
 
 def _checkhgversion(ui, hgversion):
     if hgversion >= OLDEST_NON_LEGACY_VERSION:
@@ -133,16 +143,22 @@ def uiprompt(ui, msg, default=None):
     This prevents entire prompt text from rendering as a special color which
     may be hard to read.
     """
     lines = msg.splitlines(True)
     ui.write(''.join(lines[0:-1]))
     return ui.prompt(lines[-1], default=default)
 
 
+def uipromptchoice(ui, msg):
+    lines = msg.splitlines(True)
+    ui.write(''.join(lines[0:-1]))
+    return ui.promptchoice(lines[-1])
+
+
 def _checkusername(ui, cw):
     if ui.config('ui', 'username'):
         return
 
     ui.write(MISSING_USERNAME)
 
     name, email = None, None
 
@@ -157,16 +173,31 @@ def _checkusername(ui, cw):
         cw.c['ui']['username'] = username.strip()
 
         ui.write('setting ui.username=%s\n\n' % username)
     else:
         ui.warn('Unable to set username; You will be unable to author '
                 'commits\n\n')
 
 
+def _checkdiffsettings(ui, cw):
+    git = ui.configbool('diff', 'git')
+    showfunc = ui.configbool('diff', 'showfunc')
+
+    if git and showfunc:
+        return
+
+    if not uipromptchoice(ui, BAD_DIFF_SETTINGS):
+        if 'diff' not in cw.c:
+            cw.c['diff'] = {}
+
+        cw.c['diff']['git'] = 'true'
+        cw.c['diff']['showfunc'] = 'true'
+
+
 def _handleconfigchange(ui, cw):
     # Obtain the old and new content so we can show a diff.
     newbuf = io.BytesIO()
     cw.write(newbuf)
     newbuf.seek(0)
     newlines = [l.rstrip() for l in newbuf.readlines()]
     oldlines = []
     if os.path.exists(cw.path):
--- a/hgext/configwizard/hgsetup/config.py
+++ b/hgext/configwizard/hgsetup/config.py
@@ -40,37 +40,16 @@ class MercurialConfig(object):
         if not path:
             path = ''
 
         if 'extensions' not in self._c:
             self._c['extensions'] = {}
 
         self._c['extensions'][name] = path
 
-    def have_recommended_diff_settings(self):
-        if 'diff' not in self._c:
-            return False
-
-        old = dict(self._c['diff'])
-        try:
-            self.ensure_recommended_diff_settings()
-        finally:
-            self._c['diff'].update(old)
-
-        return self._c['diff'] == old
-
-    def ensure_recommended_diff_settings(self):
-        if 'diff' not in self._c:
-            self._c['diff'] = {}
-
-        d = self._c['diff']
-        d['git'] = 1
-        d['showfunc'] = 1
-        d['unified'] = 8
-
     def get_bugzilla_credentials(self):
         if 'bugzilla' not in self._c:
             return None, None, None, None, None
 
         b = self._c['bugzilla']
         return (
             b.get('username', None),
             b.get('password', None),
--- a/hgext/configwizard/hgsetup/wizard.py
+++ b/hgext/configwizard/hgsetup/wizard.py
@@ -23,23 +23,16 @@ from mozversioncontrol import get_hg_pat
 from .update import MercurialUpdater
 from .config import (
     config_file,
     MercurialConfig,
     ParseException,
 )
 
 
-BAD_DIFF_SETTINGS = '''
-Mozilla developers produce patches in a standard format, but your Mercurial is
-not configured to produce patches in that format.
-
-(Relevant config options: diff.git, diff.showfunc, diff.unified)
-'''.strip()
-
 BZEXPORT_INFO = '''
 If you plan on uploading patches to Mozilla, there is an extension called
 bzexport that makes it easy to upload patches from the command line via the
 |hg bzexport| command. More info is available at
 https://hg.mozilla.org/hgcustom/version-control-tools/file/default/hgext/bzexport/README
 
 (Relevant config option: extensions.bzexport)
 
@@ -246,24 +239,16 @@ class MercurialSetupWizard(object):
 
         hg = get_hg_path()
         config_path = config_file(config_paths)
 
         self.updater.update_all()
 
         hg_version = get_hg_version(hg)
 
-        if not c.have_recommended_diff_settings():
-            print(BAD_DIFF_SETTINGS)
-            print('')
-            if self._prompt_yn('Would you like me to fix this for you'):
-                c.ensure_recommended_diff_settings()
-                print('Fixed patch settings.')
-                print('')
-
         # Progress is built into core and enabled by default in Mercurial 3.5.
         if hg_version < LooseVersion('3.5'):
             self.prompt_native_extension(c, 'progress',
                 'Would you like to see progress bars during Mercurial operations')
 
         self.prompt_native_extension(c, 'color',
             'Would you like Mercurial to colorize output to your terminal')
 
new file mode 100644
--- /dev/null
+++ b/hgext/configwizard/tests/test-diff.t
@@ -0,0 +1,59 @@
+  $ . $TESTDIR/hgext/configwizard/tests/helpers.sh
+
+Rejecting diff setting works
+
+  $ hg --config ui.interactive=true --config configwizard.steps=diff,configchange configwizard << EOF
+  > 
+  > n
+  > EOF
+  This wizard will guide you through configuring Mercurial for an optimal
+  experience contributing to Mozilla projects.
+  
+  The wizard makes no changes without your permission.
+  
+  To begin, press the enter/return key.
+   <RETURN>
+  Mercurial is not configured to produce diffs in a more readable format.
+  
+  Would you like to change this (Yn)?  n
+
+
+diff.git and diff.showfunc should be enabled
+
+  $ hg --config configwizard.steps=diff,configchange configwizard
+  This wizard will guide you through configuring Mercurial for an optimal
+  experience contributing to Mozilla projects.
+  
+  The wizard makes no changes without your permission.
+  
+  To begin, press the enter/return key.
+   <RETURN>
+  Mercurial is not configured to produce diffs in a more readable format.
+  
+  Would you like to change this (Yn)?  y
+  Your config file needs updating.
+  Would you like to see a diff of the changes first (Yn)?  y
+  --- hgrc.old
+  +++ hgrc.new
+  @@ -0,0 +1,3 @@
+  +[diff]
+  +git = true
+  +showfunc = true
+  
+  Write changes to hgrc file (Yn)?  y
+
+  $ cat .hgrc
+  [diff]
+  git = true
+  showfunc = true
+
+Should no-op when diff settings already optimal
+
+  $ hg --config diff.git=1 --config diff.showfunc=1 --config configwizard.steps=diff,configchange configwizard
+  This wizard will guide you through configuring Mercurial for an optimal
+  experience contributing to Mozilla projects.
+  
+  The wizard makes no changes without your permission.
+  
+  To begin, press the enter/return key.
+   <RETURN>