diff options
author | Mike Frysinger <vapier@google.com> | 2021-02-09 23:14:41 -0500 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2021-02-11 01:48:12 +0000 |
commit | 38867fb6d38d45a11271d6ef492e4d40066b078f (patch) | |
tree | 99778a43a5007322414ecd8b7c290842a4bc3fc6 | |
parent | ce64e3d47b97e54cc7fac3d7577cd53f1857de26 (diff) | |
download | git-repo-38867fb6d38d45a11271d6ef492e4d40066b078f.tar.gz |
git_config: add SetBoolean helper
A little sugar simplifies the code a bit.
Change-Id: Ie2b8a965faa9f9ca05c7be479d03e8e073cd816d
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/296522
Reviewed-by: Raman Tenneti <rtenneti@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
-rw-r--r-- | git_config.py | 6 | ||||
-rw-r--r-- | project.py | 5 | ||||
-rw-r--r-- | subcmds/init.py | 14 | ||||
-rw-r--r-- | tests/test_git_config.py | 70 |
4 files changed, 81 insertions, 14 deletions
diff --git a/git_config.py b/git_config.py index 2fa43a1e..282c0802 100644 --- a/git_config.py +++ b/git_config.py | |||
@@ -161,6 +161,12 @@ class GitConfig(object): | |||
161 | return False | 161 | return False |
162 | return None | 162 | return None |
163 | 163 | ||
164 | def SetBoolean(self, name, value): | ||
165 | """Set the truthy value for a key.""" | ||
166 | if value is not None: | ||
167 | value = 'true' if value else 'false' | ||
168 | self.SetString(name, value) | ||
169 | |||
164 | def GetString(self, name, all_keys=False): | 170 | def GetString(self, name, all_keys=False): |
165 | """Get the first value for a key, or None if it is not defined. | 171 | """Get the first value for a key, or None if it is not defined. |
166 | 172 | ||
@@ -2472,10 +2472,7 @@ class Project(object): | |||
2472 | self.config.SetString(key, m.GetString(key)) | 2472 | self.config.SetString(key, m.GetString(key)) |
2473 | self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f') | 2473 | self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f') |
2474 | self.config.SetString('filter.lfs.process', 'git-lfs filter-process --skip') | 2474 | self.config.SetString('filter.lfs.process', 'git-lfs filter-process --skip') |
2475 | if self.manifest.IsMirror: | 2475 | self.config.SetBoolean('core.bare', True if self.manifest.IsMirror else None) |
2476 | self.config.SetString('core.bare', 'true') | ||
2477 | else: | ||
2478 | self.config.SetString('core.bare', None) | ||
2479 | except Exception: | 2476 | except Exception: |
2480 | if init_obj_dir and os.path.exists(self.objdir): | 2477 | if init_obj_dir and os.path.exists(self.objdir): |
2481 | platform_utils.rmtree(self.objdir) | 2478 | platform_utils.rmtree(self.objdir) |
diff --git a/subcmds/init.py b/subcmds/init.py index 1bcf5463..fe3ebd2c 100644 --- a/subcmds/init.py +++ b/subcmds/init.py | |||
@@ -250,7 +250,7 @@ to update the working directory files. | |||
250 | m.config.SetString('repo.reference', opt.reference) | 250 | m.config.SetString('repo.reference', opt.reference) |
251 | 251 | ||
252 | if opt.dissociate: | 252 | if opt.dissociate: |
253 | m.config.SetString('repo.dissociate', 'true') | 253 | m.config.SetBoolean('repo.dissociate', opt.dissociate) |
254 | 254 | ||
255 | if opt.worktree: | 255 | if opt.worktree: |
256 | if opt.mirror: | 256 | if opt.mirror: |
@@ -261,14 +261,14 @@ to update the working directory files. | |||
261 | print('fatal: --submodules and --worktree are incompatible', | 261 | print('fatal: --submodules and --worktree are incompatible', |
262 | file=sys.stderr) | 262 | file=sys.stderr) |
263 | sys.exit(1) | 263 | sys.exit(1) |
264 | m.config.SetString('repo.worktree', 'true') | 264 | m.config.SetBoolean('repo.worktree', opt.worktree) |
265 | if is_new: | 265 | if is_new: |
266 | m.use_git_worktrees = True | 266 | m.use_git_worktrees = True |
267 | print('warning: --worktree is experimental!', file=sys.stderr) | 267 | print('warning: --worktree is experimental!', file=sys.stderr) |
268 | 268 | ||
269 | if opt.archive: | 269 | if opt.archive: |
270 | if is_new: | 270 | if is_new: |
271 | m.config.SetString('repo.archive', 'true') | 271 | m.config.SetBoolean('repo.archive', opt.archive) |
272 | else: | 272 | else: |
273 | print('fatal: --archive is only supported when initializing a new ' | 273 | print('fatal: --archive is only supported when initializing a new ' |
274 | 'workspace.', file=sys.stderr) | 274 | 'workspace.', file=sys.stderr) |
@@ -278,7 +278,7 @@ to update the working directory files. | |||
278 | 278 | ||
279 | if opt.mirror: | 279 | if opt.mirror: |
280 | if is_new: | 280 | if is_new: |
281 | m.config.SetString('repo.mirror', 'true') | 281 | m.config.SetBoolean('repo.mirror', opt.mirror) |
282 | else: | 282 | else: |
283 | print('fatal: --mirror is only supported when initializing a new ' | 283 | print('fatal: --mirror is only supported when initializing a new ' |
284 | 'workspace.', file=sys.stderr) | 284 | 'workspace.', file=sys.stderr) |
@@ -291,7 +291,7 @@ to update the working directory files. | |||
291 | print('fatal: --mirror and --partial-clone are mutually exclusive', | 291 | print('fatal: --mirror and --partial-clone are mutually exclusive', |
292 | file=sys.stderr) | 292 | file=sys.stderr) |
293 | sys.exit(1) | 293 | sys.exit(1) |
294 | m.config.SetString('repo.partialclone', 'true') | 294 | m.config.SetBoolean('repo.partialclone', opt.partial_clone) |
295 | if opt.clone_filter: | 295 | if opt.clone_filter: |
296 | m.config.SetString('repo.clonefilter', opt.clone_filter) | 296 | m.config.SetString('repo.clonefilter', opt.clone_filter) |
297 | else: | 297 | else: |
@@ -300,10 +300,10 @@ to update the working directory files. | |||
300 | if opt.clone_bundle is None: | 300 | if opt.clone_bundle is None: |
301 | opt.clone_bundle = False if opt.partial_clone else True | 301 | opt.clone_bundle = False if opt.partial_clone else True |
302 | else: | 302 | else: |
303 | m.config.SetString('repo.clonebundle', 'true' if opt.clone_bundle else 'false') | 303 | m.config.SetBoolean('repo.clonebundle', opt.clone_bundle) |
304 | 304 | ||
305 | if opt.submodules: | 305 | if opt.submodules: |
306 | m.config.SetString('repo.submodules', 'true') | 306 | m.config.SetBoolean('repo.submodules', opt.submodules) |
307 | 307 | ||
308 | if not m.Sync_NetworkHalf(is_new=is_new, quiet=opt.quiet, verbose=opt.verbose, | 308 | if not m.Sync_NetworkHalf(is_new=is_new, quiet=opt.quiet, verbose=opt.verbose, |
309 | clone_bundle=opt.clone_bundle, | 309 | clone_bundle=opt.clone_bundle, |
diff --git a/tests/test_git_config.py b/tests/test_git_config.py index 964bc324..3300c12f 100644 --- a/tests/test_git_config.py +++ b/tests/test_git_config.py | |||
@@ -15,6 +15,7 @@ | |||
15 | """Unittests for the git_config.py module.""" | 15 | """Unittests for the git_config.py module.""" |
16 | 16 | ||
17 | import os | 17 | import os |
18 | import tempfile | ||
18 | import unittest | 19 | import unittest |
19 | 20 | ||
20 | import git_config | 21 | import git_config |
@@ -26,9 +27,8 @@ def fixture(*paths): | |||
26 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) | 27 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) |
27 | 28 | ||
28 | 29 | ||
29 | class GitConfigUnitTest(unittest.TestCase): | 30 | class GitConfigReadOnlyTests(unittest.TestCase): |
30 | """Tests the GitConfig class. | 31 | """Read-only tests of the GitConfig class.""" |
31 | """ | ||
32 | 32 | ||
33 | def setUp(self): | 33 | def setUp(self): |
34 | """Create a GitConfig object using the test.gitconfig fixture. | 34 | """Create a GitConfig object using the test.gitconfig fixture. |
@@ -105,5 +105,69 @@ class GitConfigUnitTest(unittest.TestCase): | |||
105 | self.assertEqual(value, self.config.GetInt('section.%s' % (key,))) | 105 | self.assertEqual(value, self.config.GetInt('section.%s' % (key,))) |
106 | 106 | ||
107 | 107 | ||
108 | class GitConfigReadWriteTests(unittest.TestCase): | ||
109 | """Read/write tests of the GitConfig class.""" | ||
110 | |||
111 | def setUp(self): | ||
112 | self.tmpfile = tempfile.NamedTemporaryFile() | ||
113 | self.config = self.get_config() | ||
114 | |||
115 | def get_config(self): | ||
116 | """Get a new GitConfig instance.""" | ||
117 | return git_config.GitConfig(self.tmpfile.name) | ||
118 | |||
119 | def test_SetString(self): | ||
120 | """Test SetString behavior.""" | ||
121 | # Set a value. | ||
122 | self.assertIsNone(self.config.GetString('foo.bar')) | ||
123 | self.config.SetString('foo.bar', 'val') | ||
124 | self.assertEqual('val', self.config.GetString('foo.bar')) | ||
125 | |||
126 | # Make sure the value was actually written out. | ||
127 | config = self.get_config() | ||
128 | self.assertEqual('val', config.GetString('foo.bar')) | ||
129 | |||
130 | # Update the value. | ||
131 | self.config.SetString('foo.bar', 'valll') | ||
132 | self.assertEqual('valll', self.config.GetString('foo.bar')) | ||
133 | config = self.get_config() | ||
134 | self.assertEqual('valll', config.GetString('foo.bar')) | ||
135 | |||
136 | # Delete the value. | ||
137 | self.config.SetString('foo.bar', None) | ||
138 | self.assertIsNone(self.config.GetString('foo.bar')) | ||
139 | config = self.get_config() | ||
140 | self.assertIsNone(config.GetString('foo.bar')) | ||
141 | |||
142 | def test_SetBoolean(self): | ||
143 | """Test SetBoolean behavior.""" | ||
144 | # Set a true value. | ||
145 | self.assertIsNone(self.config.GetBoolean('foo.bar')) | ||
146 | for val in (True, 1): | ||
147 | self.config.SetBoolean('foo.bar', val) | ||
148 | self.assertTrue(self.config.GetBoolean('foo.bar')) | ||
149 | |||
150 | # Make sure the value was actually written out. | ||
151 | config = self.get_config() | ||
152 | self.assertTrue(config.GetBoolean('foo.bar')) | ||
153 | self.assertEqual('true', config.GetString('foo.bar')) | ||
154 | |||
155 | # Set a false value. | ||
156 | for val in (False, 0): | ||
157 | self.config.SetBoolean('foo.bar', val) | ||
158 | self.assertFalse(self.config.GetBoolean('foo.bar')) | ||
159 | |||
160 | # Make sure the value was actually written out. | ||
161 | config = self.get_config() | ||
162 | self.assertFalse(config.GetBoolean('foo.bar')) | ||
163 | self.assertEqual('false', config.GetString('foo.bar')) | ||
164 | |||
165 | # Delete the value. | ||
166 | self.config.SetBoolean('foo.bar', None) | ||
167 | self.assertIsNone(self.config.GetBoolean('foo.bar')) | ||
168 | config = self.get_config() | ||
169 | self.assertIsNone(config.GetBoolean('foo.bar')) | ||
170 | |||
171 | |||
108 | if __name__ == '__main__': | 172 | if __name__ == '__main__': |
109 | unittest.main() | 173 | unittest.main() |