color: don't crash on invalid status codes (issue2036)
authorBrodie Rao <me+hg@dackz.net>
Sun, 14 Feb 2010 17:08:52 -0500
changeset 10475 2253715fde97
parent 10460 14184a2ac46a
child 10477 44b4a2a31623
child 10478 ed4de30e16c5
color: don't crash on invalid status codes (issue2036) If an unknown file with a newline appears in the status output, color shouldn't raise a KeyError trying to parse second line in the filename.
hgext/color.py
tests/test-eolfilename
tests/test-eolfilename.out
tests/test-issue352
tests/test-issue352.out
--- a/hgext/color.py	Sun Feb 14 13:28:34 2010 +0100
+++ b/hgext/color.py	Sun Feb 14 17:08:52 2010 -0500
@@ -117,10 +117,16 @@
 
     # apply color to output and display it
     for i in xrange(len(lines)):
-        status = abbreviations[lines_with_status[i][0]]
-        effects = effectdefs[status]
-        if effects:
-            lines[i] = render_effects(lines[i], effects)
+        try:
+            status = abbreviations[lines_with_status[i][0]]
+        except KeyError:
+            # Ignore lines with invalid codes, especially in the case of
+            # of unknown filenames containing newlines (issue2036).
+            pass
+        else:
+            effects = effectdefs[status]
+            if effects:
+                lines[i] = render_effects(lines[i], effects)
         ui.write(lines[i] + delimiter)
     return retval
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-eolfilename	Sun Feb 14 17:08:52 2010 -0500
@@ -0,0 +1,43 @@
+#!/bin/sh
+# http://mercurial.selenic.com/bts/issue352
+
+"$TESTDIR/hghave" eol-in-paths || exit 80
+
+echo % test issue352
+hg init foo
+cd foo
+
+A=`printf 'he\rllo'`
+
+echo foo > "$A"
+hg add
+hg ci -A -m m
+rm "$A"
+
+echo foo > "hell
+o"
+hg add
+hg ci -A -m m
+
+echo foo > "$A"
+hg debugwalk
+
+# http://mercurial.selenic.com/bts/issue2036
+cd ..
+echo % test issue2039
+
+hg init bar
+cd bar
+
+echo "[extensions]" >> $HGRCPATH
+echo "color=" >> $HGRCPATH
+
+A=`printf 'foo\nbar'`
+B=`printf 'foo\nbar.baz'`
+
+touch "$A"
+touch "$B"
+
+hg status --color=always
+
+exit 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-eolfilename.out	Sun Feb 14 17:08:52 2010 -0500
@@ -0,0 +1,20 @@
+% test issue352
+adding he
llo
+abort: '\n' and '\r' disallowed in filenames: 'he\rllo'
+adding he
llo
+abort: '\n' and '\r' disallowed in filenames: 'he\rllo'
+adding hell
+o
+abort: '\n' and '\r' disallowed in filenames: 'hell\no'
+adding hell
+o
+abort: '\n' and '\r' disallowed in filenames: 'hell\no'
+f  he
llo  he
llo
+f  hell
+o  hell
+o
+% test issue2039
+? foo
+bar
+? foo
+bar.baz
--- a/tests/test-issue352	Sun Feb 14 13:28:34 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-#!/bin/sh
-# http://mercurial.selenic.com/bts/issue352
-
-"$TESTDIR/hghave" eol-in-paths || exit 80
-
-hg init foo
-cd foo
-
-A=`printf 'he\rllo'`
-
-echo foo > "$A"
-hg add
-hg ci -A -m m
-rm "$A"
-
-echo foo > "hell
-o"
-hg add
-hg ci -A -m m
-
-echo foo > "$A"
-hg debugwalk
-
-exit 0
--- a/tests/test-issue352.out	Sun Feb 14 13:28:34 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-adding he
llo
-abort: '\n' and '\r' disallowed in filenames: 'he\rllo'
-adding he
llo
-abort: '\n' and '\r' disallowed in filenames: 'he\rllo'
-adding hell
-o
-abort: '\n' and '\r' disallowed in filenames: 'hell\no'
-adding hell
-o
-abort: '\n' and '\r' disallowed in filenames: 'hell\no'
-f  he
llo  he
llo
-f  hell
-o  hell
-o