[PATCH STABLE] color: don't crash on invalid status codes (issue2036)

Brodie Rao dackze at gmail.com
Sun Feb 14 16:09:58 CST 2010


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1266185332 18000
# Branch stable
# Node ID 3b4f879df5f9cf818ae9c8a981ef7eafc7f5e8b5
# Parent  634b0e7561eca044e37ac9643578f1c044eec64d
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.

diff --git a/hgext/color.py b/hgext/color.py
--- a/hgext/color.py
+++ b/hgext/color.py
@@ -117,10 +117,16 @@ def _colorstatuslike(abbreviations, effe
 
     # 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
 
diff --git a/tests/test-issue2036 b/tests/test-issue2036
new file mode 100755
--- /dev/null
+++ b/tests/test-issue2036
@@ -0,0 +1,17 @@
+#!/bin/sh
+# http://mercurial.selenic.com/bts/issue2036
+
+"$TESTDIR/hghave" eol-in-paths || exit 80
+
+echo "[extensions]" >> $HGRCPATH
+echo "color=" >> $HGRCPATH
+
+hg init foo
+cd foo
+
+touch "foo
+bar"
+touch "foo
+bar.baz"
+
+hg status --color=always
diff --git a/tests/test-issue2036.out b/tests/test-issue2036.out
new file mode 100644
--- /dev/null
+++ b/tests/test-issue2036.out
@@ -0,0 +1,4 @@
+? foo
+bar
+? foo
+bar.baz


More information about the Mercurial-devel mailing list