[PATCH RFC-V2] commands: add 'setup' command to allow automatic hgrc configuration

mathias.demare at gmail.com mathias.demare at gmail.com
Thu Mar 12 18:02:15 UTC 2015


# HG changeset patch
# User Mathias De Maré <mathias.demare at gmail.com>
# Date 1426162870 -3600
#      Thu Mar 12 13:21:10 2015 +0100
# Node ID 62ced0ed5466bff586585c6ba0bd35f7e4948466
# Parent  7cf9a9e0cf893e7ae82dc576a03c843fd6640438
commands: add 'setup' command to allow automatic hgrc configuration

This is a proposal meant to easily set up new users with
a good hgrc file. It's very basic (a combination of
'release early' and 'I don't have a lot of time').
Comments are very much welcome.

I wasn't too sure about using the first path
listed in the userhgrc, not sure if that's the best approach.

Some other ideas to throw out there:
- Running 'setup' by default if a user does not have a .hgrc
      and is using the terminal (possibly falls under moot?)
- Perhaps having a global 'do you want to have a lot of sensible defaults set'
  question could be useful as well.
- Popping up a merge window in case a user already has a .hgrc.
  This could allow the user to do the merge,
  and avoids us having to mess with editing configs
  (this was a very good idea by smf).

diff -r 7cf9a9e0cf89 -r 62ced0ed5466 mercurial/commands.py
--- a/mercurial/commands.py	Wed Mar 11 15:22:34 2015 -0700
+++ b/mercurial/commands.py	Thu Mar 12 13:21:10 2015 +0100
@@ -19,10 +19,12 @@
 import merge as mergemod
 import minirst, revset, fileset
 import dagparser, context, simplemerge, graphmod, copies
+import query
 import random
 import setdiscovery, treediscovery, dagutil, pvec, localrepo
 import phases, obsolete, exchange, bundle2
 import ui as uimod
+import config as configmod
 
 table = {}
 
@@ -5615,6 +5617,50 @@
         self.httpd.serve_forever()
 
 
+ at command('setup', [])
+def setup(ui, repo):
+    """run a setup to generate a Mercurial configuration
+
+    Answer a few questions to generate a basic Mercurial configuration.
+
+    Returns 0 on success.
+    """
+
+    paths = scmutil.userrcpath()
+    if os.path.isfile(paths[0]):
+        if not query.binaryquery(repo,
+                _('WARNING: you already have a ' \
+                'Mercurial configuration file!\n' \
+                'Are you sure you want to overwrite it ' \
+                'with a new configuration?'),
+            False):
+            return
+
+    userconfig = configmod.config()
+
+    if query.binaryquery(repo,
+            _('Would you like to configure your username? '), True):
+        name = query.textquery(repo, _('Please enter your name: '))
+        email = query.textquery(repo, _('Please enter your email adress: '))
+        userconfig.set('ui', 'username', '%s <%s>' % (name, email))
+    if query.binaryquery(repo, _('Would you like to enable ' \
+            'some useful extensions by default, ' \
+            'enhancing your experience?\n' \
+            'The extensions to be enabled are: ' \
+            'color, pager, progress'), True):
+        userconfig.set('extensions', 'color', '')
+        userconfig.set('extensions', 'pager', '')
+        userconfig.set('pager', 'pager', 'less -FRX')
+        userconfig.set('extensions', 'progress', '')
+    if query.binaryquery(repo,
+            'Would you like to use an extended diff format? ' \
+            'If you are viewing changes, ' \
+            'this allows you to easily see added/removed files.',
+            True):
+        userconfig.set('diff', 'git', True)
+    userconfig.write(paths[0])
+
+
 @command('^status|st',
     [('A', 'all', None, _('show status of all files')),
     ('m', 'modified', None, _('show only modified files')),
diff -r 7cf9a9e0cf89 -r 62ced0ed5466 mercurial/config.py
--- a/mercurial/config.py	Wed Mar 11 15:22:34 2015 -0700
+++ b/mercurial/config.py	Thu Mar 12 13:21:10 2015 +0100
@@ -157,3 +157,13 @@
         if not fp:
             fp = util.posixfile(path)
         self.parse(path, fp.read(), sections, remap, self.read)
+
+    def write(self, path, fp=None):
+        if not fp:
+            fp = util.posixfile(path, 'w')
+        for section in self:
+            d = self[section]
+            fp.write('[%s]\n' % section)
+            for key in d:
+                fp.write('%s = %s\n' % (key, d[key]))
+            fp.write('\n')
diff -r 7cf9a9e0cf89 -r 62ced0ed5466 mercurial/query.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/query.py	Thu Mar 12 13:21:10 2015 +0100
@@ -0,0 +1,43 @@
+# query.py - user querying for mercurial
+#
+# Copyright 2015 Mathias De Maré <mathias.demare at gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+def _generateresponsestr(responses, default):
+    defaultidx = responses.index(default)
+    if defaultidx >= 0 and len(responses[defaultidx]) > 0:
+        responses[defaultidx] = responses[defaultidx].title()
+    return '(%s)' % '/'.join(responses)
+
+def textquery(repo, question, emptyallowed=False):
+    """Query the user using an open question"""
+    repo.ui.write('%s\n' % question)
+    while True:
+        res = raw_input()
+        if emptyallowed or len(res):
+            return res
+
+def optionquery(repo, question, responses, default):
+    """Query the user using question and a given set of parameters.
+    Once a valid response is given, the query returns
+    the match from the list of responses."""
+    repo.ui.write('%s %s\n' % (question,
+                               _generateresponsestr(responses, default)))
+    while True:
+        res = raw_input()
+        if not len(res) and default:
+            return default
+        for response in responses:
+            if response.startswith(res):
+                return response
+
+def binaryquery(repo, question, default=True):
+    """Query the user using question and 'yes/no'"""
+    if default:
+        default = 'yes'
+    else:
+        default = 'no'
+    res = optionquery(repo, question, ['yes', 'no'], default)
+    return res == 'yes'
diff -r 7cf9a9e0cf89 -r 62ced0ed5466 tests/test-completion.t
--- a/tests/test-completion.t	Wed Mar 11 15:22:34 2015 -0700
+++ b/tests/test-completion.t	Thu Mar 12 13:21:10 2015 +0100
@@ -45,6 +45,7 @@
   rollback
   root
   serve
+  setup
   status
   summary
   tag
@@ -194,7 +195,7 @@
 Show an error if we use --options with an ambiguous abbreviation
   $ hg debugcomplete --options s
   hg: command 's' is ambiguous:
-      serve showconfig status summary
+      serve setup showconfig status summary
   [255]
 
 Show all commands + options
@@ -282,6 +283,7 @@
   revert: all, date, rev, no-backup, include, exclude, dry-run
   rollback: dry-run, force
   root: 
+  setup: 
   tag: force, local, rev, remove, edit, message, date, user
   tags: template
   tip: patch, git, style, template
diff -r 7cf9a9e0cf89 -r 62ced0ed5466 tests/test-globalopts.t
--- a/tests/test-globalopts.t	Wed Mar 11 15:22:34 2015 -0700
+++ b/tests/test-globalopts.t	Thu Mar 12 13:21:10 2015 +0100
@@ -328,6 +328,7 @@
    revert        restore files to their checkout state
    root          print the root (top) of the current working directory
    serve         start stand-alone webserver
+   setup         run a setup to generate a Mercurial configuration
    status        show changed files in the working directory
    summary       summarize working directory state
    tag           add one or more tags for the current or given revision
@@ -409,6 +410,7 @@
    revert        restore files to their checkout state
    root          print the root (top) of the current working directory
    serve         start stand-alone webserver
+   setup         run a setup to generate a Mercurial configuration
    status        show changed files in the working directory
    summary       summarize working directory state
    tag           add one or more tags for the current or given revision
diff -r 7cf9a9e0cf89 -r 62ced0ed5466 tests/test-help.t
--- a/tests/test-help.t	Wed Mar 11 15:22:34 2015 -0700
+++ b/tests/test-help.t	Thu Mar 12 13:21:10 2015 +0100
@@ -91,6 +91,7 @@
    revert        restore files to their checkout state
    root          print the root (top) of the current working directory
    serve         start stand-alone webserver
+   setup         run a setup to generate a Mercurial configuration
    status        show changed files in the working directory
    summary       summarize working directory state
    tag           add one or more tags for the current or given revision
@@ -166,6 +167,7 @@
    revert        restore files to their checkout state
    root          print the root (top) of the current working directory
    serve         start stand-alone webserver
+   setup         run a setup to generate a Mercurial configuration
    status        show changed files in the working directory
    summary       summarize working directory state
    tag           add one or more tags for the current or given revision
@@ -708,6 +710,7 @@
    revert        restore files to their checkout state
    root          print the root (top) of the current working directory
    serve         start stand-alone webserver
+   setup         run a setup to generate a Mercurial configuration
    status        show changed files in the working directory
    summary       summarize working directory state
    tag           add one or more tags for the current or given revision
@@ -1742,6 +1745,13 @@
   print the root (top) of the current working directory
   </td></tr>
   <tr><td>
+  <a href="/help/setup">
+  setup
+  </a>
+  </td><td>
+  run a setup to generate a Mercurial configuration
+  </td></tr>
+  <tr><td>
   <a href="/help/tag">
   tag
   </a>


More information about the Mercurial-devel mailing list