mirror of
https://gerrit.googlesource.com/git-repo
synced 2024-12-21 07:16:21 +00:00
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>
This commit is contained in:
parent
ce64e3d47b
commit
38867fb6d3
@ -161,6 +161,12 @@ class GitConfig(object):
|
|||||||
return False
|
return False
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def SetBoolean(self, name, value):
|
||||||
|
"""Set the truthy value for a key."""
|
||||||
|
if value is not None:
|
||||||
|
value = 'true' if value else 'false'
|
||||||
|
self.SetString(name, value)
|
||||||
|
|
||||||
def GetString(self, name, all_keys=False):
|
def GetString(self, name, all_keys=False):
|
||||||
"""Get the first value for a key, or None if it is not defined.
|
"""Get the first value for a key, or None if it is not defined.
|
||||||
|
|
||||||
|
@ -2472,10 +2472,7 @@ class Project(object):
|
|||||||
self.config.SetString(key, m.GetString(key))
|
self.config.SetString(key, m.GetString(key))
|
||||||
self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f')
|
self.config.SetString('filter.lfs.smudge', 'git-lfs smudge --skip -- %f')
|
||||||
self.config.SetString('filter.lfs.process', 'git-lfs filter-process --skip')
|
self.config.SetString('filter.lfs.process', 'git-lfs filter-process --skip')
|
||||||
if self.manifest.IsMirror:
|
self.config.SetBoolean('core.bare', True if self.manifest.IsMirror else None)
|
||||||
self.config.SetString('core.bare', 'true')
|
|
||||||
else:
|
|
||||||
self.config.SetString('core.bare', None)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
if init_obj_dir and os.path.exists(self.objdir):
|
if init_obj_dir and os.path.exists(self.objdir):
|
||||||
platform_utils.rmtree(self.objdir)
|
platform_utils.rmtree(self.objdir)
|
||||||
|
@ -250,7 +250,7 @@ to update the working directory files.
|
|||||||
m.config.SetString('repo.reference', opt.reference)
|
m.config.SetString('repo.reference', opt.reference)
|
||||||
|
|
||||||
if opt.dissociate:
|
if opt.dissociate:
|
||||||
m.config.SetString('repo.dissociate', 'true')
|
m.config.SetBoolean('repo.dissociate', opt.dissociate)
|
||||||
|
|
||||||
if opt.worktree:
|
if opt.worktree:
|
||||||
if opt.mirror:
|
if opt.mirror:
|
||||||
@ -261,14 +261,14 @@ to update the working directory files.
|
|||||||
print('fatal: --submodules and --worktree are incompatible',
|
print('fatal: --submodules and --worktree are incompatible',
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
m.config.SetString('repo.worktree', 'true')
|
m.config.SetBoolean('repo.worktree', opt.worktree)
|
||||||
if is_new:
|
if is_new:
|
||||||
m.use_git_worktrees = True
|
m.use_git_worktrees = True
|
||||||
print('warning: --worktree is experimental!', file=sys.stderr)
|
print('warning: --worktree is experimental!', file=sys.stderr)
|
||||||
|
|
||||||
if opt.archive:
|
if opt.archive:
|
||||||
if is_new:
|
if is_new:
|
||||||
m.config.SetString('repo.archive', 'true')
|
m.config.SetBoolean('repo.archive', opt.archive)
|
||||||
else:
|
else:
|
||||||
print('fatal: --archive is only supported when initializing a new '
|
print('fatal: --archive is only supported when initializing a new '
|
||||||
'workspace.', file=sys.stderr)
|
'workspace.', file=sys.stderr)
|
||||||
@ -278,7 +278,7 @@ to update the working directory files.
|
|||||||
|
|
||||||
if opt.mirror:
|
if opt.mirror:
|
||||||
if is_new:
|
if is_new:
|
||||||
m.config.SetString('repo.mirror', 'true')
|
m.config.SetBoolean('repo.mirror', opt.mirror)
|
||||||
else:
|
else:
|
||||||
print('fatal: --mirror is only supported when initializing a new '
|
print('fatal: --mirror is only supported when initializing a new '
|
||||||
'workspace.', file=sys.stderr)
|
'workspace.', file=sys.stderr)
|
||||||
@ -291,7 +291,7 @@ to update the working directory files.
|
|||||||
print('fatal: --mirror and --partial-clone are mutually exclusive',
|
print('fatal: --mirror and --partial-clone are mutually exclusive',
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
m.config.SetString('repo.partialclone', 'true')
|
m.config.SetBoolean('repo.partialclone', opt.partial_clone)
|
||||||
if opt.clone_filter:
|
if opt.clone_filter:
|
||||||
m.config.SetString('repo.clonefilter', opt.clone_filter)
|
m.config.SetString('repo.clonefilter', opt.clone_filter)
|
||||||
else:
|
else:
|
||||||
@ -300,10 +300,10 @@ to update the working directory files.
|
|||||||
if opt.clone_bundle is None:
|
if opt.clone_bundle is None:
|
||||||
opt.clone_bundle = False if opt.partial_clone else True
|
opt.clone_bundle = False if opt.partial_clone else True
|
||||||
else:
|
else:
|
||||||
m.config.SetString('repo.clonebundle', 'true' if opt.clone_bundle else 'false')
|
m.config.SetBoolean('repo.clonebundle', opt.clone_bundle)
|
||||||
|
|
||||||
if opt.submodules:
|
if opt.submodules:
|
||||||
m.config.SetString('repo.submodules', 'true')
|
m.config.SetBoolean('repo.submodules', opt.submodules)
|
||||||
|
|
||||||
if not m.Sync_NetworkHalf(is_new=is_new, quiet=opt.quiet, verbose=opt.verbose,
|
if not m.Sync_NetworkHalf(is_new=is_new, quiet=opt.quiet, verbose=opt.verbose,
|
||||||
clone_bundle=opt.clone_bundle,
|
clone_bundle=opt.clone_bundle,
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
"""Unittests for the git_config.py module."""
|
"""Unittests for the git_config.py module."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import git_config
|
import git_config
|
||||||
@ -26,9 +27,8 @@ def fixture(*paths):
|
|||||||
return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
|
return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
|
||||||
|
|
||||||
|
|
||||||
class GitConfigUnitTest(unittest.TestCase):
|
class GitConfigReadOnlyTests(unittest.TestCase):
|
||||||
"""Tests the GitConfig class.
|
"""Read-only tests of the GitConfig class."""
|
||||||
"""
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Create a GitConfig object using the test.gitconfig fixture.
|
"""Create a GitConfig object using the test.gitconfig fixture.
|
||||||
@ -105,5 +105,69 @@ class GitConfigUnitTest(unittest.TestCase):
|
|||||||
self.assertEqual(value, self.config.GetInt('section.%s' % (key,)))
|
self.assertEqual(value, self.config.GetInt('section.%s' % (key,)))
|
||||||
|
|
||||||
|
|
||||||
|
class GitConfigReadWriteTests(unittest.TestCase):
|
||||||
|
"""Read/write tests of the GitConfig class."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.tmpfile = tempfile.NamedTemporaryFile()
|
||||||
|
self.config = self.get_config()
|
||||||
|
|
||||||
|
def get_config(self):
|
||||||
|
"""Get a new GitConfig instance."""
|
||||||
|
return git_config.GitConfig(self.tmpfile.name)
|
||||||
|
|
||||||
|
def test_SetString(self):
|
||||||
|
"""Test SetString behavior."""
|
||||||
|
# Set a value.
|
||||||
|
self.assertIsNone(self.config.GetString('foo.bar'))
|
||||||
|
self.config.SetString('foo.bar', 'val')
|
||||||
|
self.assertEqual('val', self.config.GetString('foo.bar'))
|
||||||
|
|
||||||
|
# Make sure the value was actually written out.
|
||||||
|
config = self.get_config()
|
||||||
|
self.assertEqual('val', config.GetString('foo.bar'))
|
||||||
|
|
||||||
|
# Update the value.
|
||||||
|
self.config.SetString('foo.bar', 'valll')
|
||||||
|
self.assertEqual('valll', self.config.GetString('foo.bar'))
|
||||||
|
config = self.get_config()
|
||||||
|
self.assertEqual('valll', config.GetString('foo.bar'))
|
||||||
|
|
||||||
|
# Delete the value.
|
||||||
|
self.config.SetString('foo.bar', None)
|
||||||
|
self.assertIsNone(self.config.GetString('foo.bar'))
|
||||||
|
config = self.get_config()
|
||||||
|
self.assertIsNone(config.GetString('foo.bar'))
|
||||||
|
|
||||||
|
def test_SetBoolean(self):
|
||||||
|
"""Test SetBoolean behavior."""
|
||||||
|
# Set a true value.
|
||||||
|
self.assertIsNone(self.config.GetBoolean('foo.bar'))
|
||||||
|
for val in (True, 1):
|
||||||
|
self.config.SetBoolean('foo.bar', val)
|
||||||
|
self.assertTrue(self.config.GetBoolean('foo.bar'))
|
||||||
|
|
||||||
|
# Make sure the value was actually written out.
|
||||||
|
config = self.get_config()
|
||||||
|
self.assertTrue(config.GetBoolean('foo.bar'))
|
||||||
|
self.assertEqual('true', config.GetString('foo.bar'))
|
||||||
|
|
||||||
|
# Set a false value.
|
||||||
|
for val in (False, 0):
|
||||||
|
self.config.SetBoolean('foo.bar', val)
|
||||||
|
self.assertFalse(self.config.GetBoolean('foo.bar'))
|
||||||
|
|
||||||
|
# Make sure the value was actually written out.
|
||||||
|
config = self.get_config()
|
||||||
|
self.assertFalse(config.GetBoolean('foo.bar'))
|
||||||
|
self.assertEqual('false', config.GetString('foo.bar'))
|
||||||
|
|
||||||
|
# Delete the value.
|
||||||
|
self.config.SetBoolean('foo.bar', None)
|
||||||
|
self.assertIsNone(self.config.GetBoolean('foo.bar'))
|
||||||
|
config = self.get_config()
|
||||||
|
self.assertIsNone(config.GetBoolean('foo.bar'))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user