diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2012-05-30 05:30:51 -0700 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2012-05-30 05:30:51 -0700 |
commit | cd72d78e79271f0f9025984fb2d9a6c5b4dba4de (patch) | |
tree | 81d9629c6b6a2b3574f1d105b9b06e2c06b6d28f /git/test | |
parent | 91c04a12e0d66dacaee81acc8c03e47afe4aafd9 (diff) | |
parent | 3cadd6f793f6c59fa3ad0d6e1a209d6b6b06b6da (diff) | |
download | gitpython-cd72d78e79271f0f9025984fb2d9a6c5b4dba4de.tar.gz |
Merge pull request #46 from ereOn/master
Incorrect handling of backslashes and quotes in GitConfigParser
Steps to reproduce the issue:
import git
config = git.Repo().config_writer()
config.add_section('test')
config.set_value('test', 'test', r'some\data')
Now if you try to read this value using a regular (non Python) git config, Git complains that the configuration file is invalid:
fatal: bad config file line 11 in .git/config
Indeed, if you open .git/config you can see that the value is written as:
[test]
test = some\data
While the git-config configuration states that:
String values may be entirely or partially enclosed in double quotes. You need to enclose variable values in double quotes if you want to preserve leading or trailing whitespace, or if the variable value contains comment characters (i.e. it contains # or ;). Double quote " and backslash \ characters in variable values must be escaped: use \" for " and \ for .
That is, the backslashes are not escaped in the configuration file.
This also causes issues while reading, because values are not un-escaped.
This pull request fixes both those issues and also fixes unescaped quotes pairs.
A test-case has been provided along with the fixes.
Diffstat (limited to 'git/test')
-rw-r--r-- | git/test/fixtures/git_config_values | 4 | ||||
-rw-r--r-- | git/test/test_config.py | 26 |
2 files changed, 29 insertions, 1 deletions
diff --git a/git/test/fixtures/git_config_values b/git/test/fixtures/git_config_values new file mode 100644 index 00000000..5ba039ef --- /dev/null +++ b/git/test/fixtures/git_config_values @@ -0,0 +1,4 @@ +[values] + backslash = some\\data + quote = this is a \"quoted value\" + quoted = "all" "your \"quotes\" a"re bel"ong to """"us" diff --git a/git/test/test_config.py b/git/test/test_config.py index d2e199e3..b37db290 100644 --- a/git/test/test_config.py +++ b/git/test/test_config.py @@ -101,4 +101,28 @@ class TestConfig(TestBase): # it raises if there is no default though self.failUnlessRaises(NoSectionError, r_config.get_value, "doesnt", "exist") - + def test_values(self): + file_obj = self._to_memcache(fixture_path("git_config_values")) + w_config = GitConfigParser(file_obj, read_only = False) + w_config.write() # enforce writing + orig_value = file_obj.getvalue() + + # Reading must unescape backslashes + backslash = w_config.get('values', 'backslash') + assert backslash == r'some\data' + + # Reading must unescape quotes + quote = w_config.get('values', 'quote') + assert quote == 'this is a "quoted value"' + + # Reading must remove surrounding quotes + quoted = w_config.get('values', 'quoted') + assert quoted == 'all your "quotes" are belong to us' + + # Writing must escape backslashes and quotes + w_config.set('values', 'backslash', backslash) + w_config.set('values', 'quote', quote) + w_config.write() # enforce writing + + # Contents shouldn't differ + assert file_obj.getvalue() == orig_value |