Patchwork [STABLE] ui: fix crash by non-interactive prompt echo for user name

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 6, 2016, 12:04 p.m.
Message ID <a586b649d1fcac9302f2.1454760283@mimosa>
Download mbox | patch
Permalink /patch/13027/
State Accepted
Delegated to: Martin von Zweigbergk
Headers show

Comments

Yuya Nishihara - Feb. 6, 2016, 12:04 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1454759000 -32400
#      Sat Feb 06 20:43:20 2016 +0900
# Branch stable
# Node ID a586b649d1fcac9302f24c690fb9cb2aa4d75a30
# Parent  7b433529446141f3160223b07da781373b740e32
ui: fix crash by non-interactive prompt echo for user name

Since we've dropped a str cast at write() by f04bd381e8c0, ui.prompt() should
convert default to '' if it is None. Otherwise, write() would fail with
"TypeError: object of type 'NoneType' has no len()".

This patch includes the tests for both interactive and non-interactive cases
because "ui.askusername" was never tested.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -798,7 +798,7 @@  class ui(object):
         If ui is not interactive, the default is returned.
         """
         if not self.interactive():
-            self.write(msg, ' ', default, "\n")
+            self.write(msg, ' ', default or '', "\n")
             return default
         try:
             r = self._readline(self.label(msg, 'ui.prompt'))
diff --git a/tests/test-committer.t b/tests/test-committer.t
--- a/tests/test-committer.t
+++ b/tests/test-committer.t
@@ -66,6 +66,31 @@ 
   date:        Thu Jan 01 00:00:00 1970 +0000
   summary:     commit-1
   
+# test prompt username
+
+  $ cat > .hg/hgrc <<EOF
+  > [ui]
+  > askusername = True
+  > EOF
+
+  $ echo 12345 > asdf
+  $ hg commit --config ui.interactive=False -m ask
+  enter a commit username: 
+  no username found, using '[^']*' instead (re)
+  $ hg rollback -q
+
+  $ hg commit --config ui.interactive=True -m ask <<EOF
+  > Asked User <ask@example.com>
+  > EOF
+  enter a commit username: Asked User <ask@example.com>
+  $ hg tip
+  changeset:   5:84c91d963b70
+  tag:         tip
+  user:        Asked User <ask@example.com>
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     ask
+  
+
 # test no .hg/hgrc (uses generated non-interactive username)
 
   $ echo space > asdf