diff options
-rw-r--r-- | lib/git/config.py | 20 | ||||
-rw-r--r-- | test/git/test_config.py | 6 |
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/git/config.py b/lib/git/config.py index 1739e786..e8bdfc75 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -387,3 +387,23 @@ class GitConfigParser(cp.RawConfigParser, LockFile): raise TypeError( "Invalid value type: only int, long, float and str are allowed", valuestr ) return valuestr + + @_needs_values + @_set_dirty_and_flush_changes + def set_value(self, section, option, value): + """Sets the given option in section to the given value. + It will create the section if required, and will not throw as opposed to the default + ConfigParser 'set' method. + + ``section`` + Name of the section in which the option resides or should reside + + ``option`` + Name of the options whose value to set + + ``value`` + Value to set the option to. It must be a string or convertible to a string + """ + if not self.has_section(section): + self.add_section(section) + self.set(section, option, str(value)) diff --git a/test/git/test_config.py b/test/git/test_config.py index e3d723a0..6103ab8d 100644 --- a/test/git/test_config.py +++ b/test/git/test_config.py @@ -49,6 +49,12 @@ class TestBase(TestCase): assert w_config.has_option(sname,oname) assert w_config.get(sname, oname) == val + sname_new = "new_section" + oname_new = "new_key" + ival = 10 + w_config.set_value(sname_new, oname_new, ival) + assert w_config.get_value(sname_new, oname_new) == ival + file_obj.seek(0) r_config = GitConfigParser(file_obj, read_only=True) assert r_config.has_section(sname) |