[PATCH] Use setconfig in ui.__init__ to setup ui.username, ui.editor, and ui.merge

Stephen Darnell stephen at darnell.plus.com
Tue May 9 17:12:15 CDT 2006


In Re: [PATCH] Introduce ui functions that return the edit/merge tool to use
Matt wrote:
>>...
>> +
>> +    def merge(self):
>> +        return (os.environ.get("HGMERGE") or
>> +                self.config("ui", "merge") or
>> +                "hgmerge")
>
> I think it'd be better to do this with setconfig in ui.__init__.
> Similarly for several other legacy functions, like username.

Okay, attached is an updated patch where I've done what I think you're 
asking for.

Regards,
  Stephen

PS I don't think that username should default to including the fqdn appended 
to LOGNAME/USERNAME I think assuming that people will be running a mail 
server AND that they want that to be advertised is dubious. A large 
percentage of the time it will be a mistake, and either just using 
LOGNAME/USERNAME or requiring HGUSER/ui.username (or maybe EMAIL) to be set 
would seem a better solution.

Remember that the username is not displayed during commit, and users will 
mostly only spot a mistake afterwards and have to use the big red "Don't 
press me" button (hg undo/rollback) assuming they spot it before other 
commits.
For the benefit of newbies I think it would be better to abort unless a 
really good default is available, or at least display HGUSER in the commit 
text - so one can abort the commit if it isn't right.
-------------- next part --------------
# HG changeset patch
# User Stephen Darnell <stephen at darnell.plus.com>
# Node ID 093548e798adf07bff362583a03e779a0e15fdef
# Parent  8b106ff5b8a980b1d76a8ee66461a4b6a35243a0
Use setconfig in ui.__init__ to setup ui.username, ui.editor, and ui.merge.
Then remove the existing ui.username(), and change clients to use the
normal ui.config() lookups.

diff -r 8b106ff5b8a9 -r 093548e798ad hgext/notify.py
--- a/hgext/notify.py	Mon May 08 14:20:37 2006 -0700
+++ b/hgext/notify.py	Tue May 09 22:50:10 2006 +0100
@@ -196,7 +196,8 @@ class notifier(object):
 
             sender = msg['From']
             if not sender:
-                sender = self.ui.config('email', 'from') or self.ui.username()
+                sender = self.ui.config('email', 'from') or
+                                        self.ui.config('ui', 'username')
             if '@' not in sender or '@localhost' in sender:
                 sender = self.fixmail(sender)
             del msg['From']
diff -r 8b106ff5b8a9 -r 093548e798ad hgext/patchbomb.py
--- a/hgext/patchbomb.py	Mon May 08 14:20:37 2006 -0700
+++ b/hgext/patchbomb.py	Tue May 09 22:50:10 2006 +0100
@@ -175,7 +175,7 @@ def patchbomb(ui, repo, *revs, **opts):
 
     sender = (opts['from'] or ui.config('email', 'from') or
               ui.config('patchbomb', 'from') or
-              prompt('From', ui.username()))
+              prompt('From', ui.config('ui', 'username')))
 
     def getaddrs(opt, prpt, default = None):
         addrs = opts[opt] or (ui.config('email', opt) or
diff -r 8b106ff5b8a9 -r 093548e798ad mercurial/localrepo.py
--- a/mercurial/localrepo.py	Mon May 08 14:20:37 2006 -0700
+++ b/mercurial/localrepo.py	Tue May 09 22:50:10 2006 +0100
@@ -439,7 +439,7 @@ class localrepository(object):
                     pass
 
         mnode = self.manifest.add(mm, mfm, tr, linkrev, c1[0], c2[0])
-        user = user or self.ui.username()
+        user = user or self.ui.config('ui', 'username')
         n = self.changelog.add(mnode, changed, text, tr, p1, p2, user, date)
         tr.close()
         if update_dirstate:
@@ -534,7 +534,7 @@ class localrepository(object):
         new = new.keys()
         new.sort()
 
-        user = user or self.ui.username()
+        user = user or self.ui.config('ui', 'username')
         if not text:
             edittext = [""]
             if p2 != nullid:
@@ -1860,8 +1860,7 @@ class localrepository(object):
         self.ui.debug(_("file %s: my %s other %s ancestor %s\n") %
                               (fn, short(my), short(other), short(base)))
 
-        cmd = (os.environ.get("HGMERGE") or self.ui.config("ui", "merge")
-               or "hgmerge")
+        cmd = self.ui.config("ui", "merge")
         r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=self.root,
                         environ={'HG_FILE': fn,
                                  'HG_MY_NODE': p1,
diff -r 8b106ff5b8a9 -r 093548e798ad mercurial/ui.py
--- a/mercurial/ui.py	Mon May 08 14:20:37 2006 -0700
+++ b/mercurial/ui.py	Tue May 09 22:50:10 2006 +0100
@@ -41,6 +41,24 @@ class ui(object):
                 self.cdata.add_section(section)
                 for name, value in parent_cdata.items(section, raw=True):
                     self.cdata.set(section, name, value)
+
+        # Override some configuration options that can be derived from
+        # environment variables
+        self.setconfig( "ui", "editor", os.environ.get("HGEDITOR") or
+            self.config("ui", "editor") or os.environ.get("EDITOR", "vi"))
+
+        self.setconfig( "ui", "merge", os.environ.get("HGMERGE") or
+            self.config("ui", "merge", "hgmerge"))
+
+        user = (os.environ.get("HGUSER") or self.config("ui", "username") or
+            os.environ.get("EMAIL"))
+        if user is None:
+            user = os.environ.get("LOGNAME") or os.environ.get("USERNAME")
+            if user:
+                user = "%s@%s" % (user, socket.getfqdn())
+        if user is None:
+            raise util.Abort(_("Please specify a username."))
+        self.setconfig("ui", "username", user)
 
     def __getattr__(self, key):
         return getattr(self.parentui, key)
@@ -159,28 +177,6 @@ class ui(object):
                 ret[k] = value
         self.diffcache = ret
         return ret
-
-    def username(self):
-        """Return default username to be used in commits.
-
-        Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
-        and stop searching if one of these is set.
-        Abort if found username is an empty string to force specifying
-        the commit user elsewhere, e.g. with line option or repo hgrc.
-        If not found, use $LOGNAME or $USERNAME +"@full.hostname".
-        """
-        user = os.environ.get("HGUSER")
-        if user is None:
-            user = self.config("ui", "username")
-        if user is None:
-            user = os.environ.get("EMAIL")
-        if user is None:
-            user = os.environ.get("LOGNAME") or os.environ.get("USERNAME")
-            if user:
-                user = "%s@%s" % (user, socket.getfqdn())
-        if not user:
-            raise util.Abort(_("Please specify a username."))
-        return user
 
     def shortuser(self, user):
         """Return a short representation of a user name or email address."""
@@ -249,11 +245,7 @@ class ui(object):
             f.write(text)
             f.close()
 
-            editor = (os.environ.get("HGEDITOR") or
-                    self.config("ui", "editor") or
-                    os.environ.get("EDITOR", "vi"))
-
-            util.system("%s \"%s\"" % (editor, name),
+            util.system("%s \"%s\"" % (self.config('ui', 'editor'), name),
                         environ={'HGUSER': user},
                         onerr=util.Abort, errprefix=_("edit failed"))
 


More information about the Mercurial mailing list