Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb9a2ea8

Browse files
committed
fix(config): selective cfg write;fix cfg parser
* config parser now handles quoted values correctly. This doesn't hamper multi-line support.* added regression test to travis to assure we will be warned if we rewrite and break the user's .gitconfig file* only rewrite configuration files if we actually called a mutating method on the writer. Previously it would always rewrite it.Fixesgitpython-developers#285
1 parente39c8b0 commitb9a2ea8

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

‎.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ install:
2525
# as commits are performed with the default user, it needs to be set for travis too
2626
-git config --global user.email "travis@ci.com"
2727
-git config --global user.name "Travis Runner"
28+
# If we rewrite the user's config by accident, we will mess it up
29+
# and cause subsequent tests to fail
30+
-cp git/test/fixtures/.gitconfig ~/
2831
script:
2932
# Make sure we limit open handles to see if we are leaking them
3033
-ulimit -n 96

‎git/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def set_dirty_and_flush_changes(non_const_func):
8181

8282
defflush_changes(self,*args,**kwargs):
8383
rval=non_const_func(self,*args,**kwargs)
84+
self._dirty=True
8485
self.write()
8586
returnrval
8687
# END wrapper method
@@ -190,6 +191,7 @@ def __init__(self, file_or_files, read_only=True, merge_includes=True):
190191

191192
self._file_or_files=file_or_files
192193
self._read_only=read_only
194+
self._dirty=False
193195
self._is_initialized=False
194196
self._merge_includes=merge_includes
195197
self._lock=None
@@ -304,7 +306,7 @@ def string_decode(v):
304306
ifmo:
305307
# We might just have handled the last line, which could contain a quotation we want to remove
306308
optname,vi,optval=mo.group('option','vi','value')
307-
ifviin ('=',':')and';'inoptval:
309+
ifviin ('=',':')and';'inoptvalandnotoptval.strip().startswith('"'):
308310
pos=optval.find(';')
309311
ifpos!=-1andoptval[pos-1].isspace():
310312
optval=optval[:pos]
@@ -433,6 +435,8 @@ def write(self):
433435
:raise IOError: if this is a read-only writer instance or if we could not obtain
434436
a file lock"""
435437
self._assure_writable("write")
438+
ifnotself._dirty:
439+
return
436440

437441
ifisinstance(self._file_or_files, (list,tuple)):
438442
raiseAssertionError("Cannot write back if there is not exactly a single file to write to, have %i files"

‎git/test/fixtures/.gitconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[alias]
2+
rbi="!g() { git rebase -i origin/${1:-master}; }; g"
3+
expush="!f() { git branch -f tmp; { git rbi$1&& git push; }; git reset --hard tmp; git rebase origin/${1:-master}; }; f"

‎git/test/test_config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
)
1919
importio
2020
importos
21-
fromcopyimportcopy
2221
fromgit.configimportcp
2322

2423

@@ -30,21 +29,18 @@ def _to_memcache(self, file_path):
3029
sio.name=file_path
3130
returnsio
3231

33-
def_parsers_equal_or_raise(self,lhs,rhs):
34-
pass
35-
3632
deftest_read_write(self):
3733
# writer must create the exact same file as the one read before
3834
forfilenamein ("git_config","git_config_global"):
3935
file_obj=self._to_memcache(fixture_path(filename))
40-
file_obj_orig=copy(file_obj)
4136
w_config=GitConfigParser(file_obj,read_only=False)
4237
w_config.read()# enforce reading
4338
assertw_config._sections
4439
w_config.write()# enforce writing
4540

4641
# we stripped lines when reading, so the results differ
47-
assertfile_obj.getvalue()andfile_obj.getvalue()!=file_obj_orig.getvalue()
42+
assertfile_obj.getvalue()
43+
self.assertEqual(file_obj.getvalue(),self._to_memcache(fixture_path(filename)).getvalue())
4844

4945
# creating an additional config writer must fail due to exclusive access
5046
self.failUnlessRaises(IOError,GitConfigParser,file_obj,read_only=False)
@@ -207,3 +203,10 @@ def test_rename(self):
207203
assertnotcw.has_section('core')
208204
assertlen(cw.items(nn))==4
209205
cw.release()
206+
207+
deftest_complex_aliases(self):
208+
file_obj=self._to_memcache(fixture_path('.gitconfig'))
209+
w_config=GitConfigParser(file_obj,read_only=False)
210+
self.assertEqual(w_config.get('alias','rbi'),'"!g() { git rebase -i origin/${1:-master} ; } ; g"')
211+
w_config.release()
212+
self.assertEqual(file_obj.getvalue(),self._to_memcache(fixture_path('.gitconfig')).getvalue())

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp