From 235db00d4e95dc792f9881675159ddbfb94573e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Caletka?= Date: Thu, 12 Jul 2018 16:38:25 +0200 Subject: [PATCH] Add get_config function --- dzonegit.py | 22 ++++++++++++++++++++++ test_dzonegit.py | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/dzonegit.py b/dzonegit.py index 341bfe0..b1b6e3c 100644 --- a/dzonegit.py +++ b/dzonegit.py @@ -221,6 +221,28 @@ def check_updated_zones(against, revision=None): pass # Old version of zone did not exist +def get_config(name, type_=None): + cmd = ["git", "config", ] + if type_ == bool: + cmd.append("--bool") + elif type_ == int: + cmd.append("--int") + elif type_: + raise ValueError("Invalid type supplied") + cmd.append(name) + r = subprocess.run( + cmd, + stdout=subprocess.PIPE, + check=True, + ) + if type_ == bool: + return r.stdout == b"true\n" + elif type_ == int: + return int(r.stdout) + else: + return r.stdout.decode("utf-8").rstrip("\n") + + def pre_commit(): against = get_head() try: diff --git a/test_dzonegit.py b/test_dzonegit.py index 3dc7e27..061ab50 100644 --- a/test_dzonegit.py +++ b/test_dzonegit.py @@ -193,3 +193,13 @@ def test_get_increased_serial(): todayser = datetime.date.today().strftime("%Y%m%d00") assert todayser == dzonegit.get_increased_serial("2018010100") assert str(int(todayser) + 1) == dzonegit.get_increased_serial(todayser) + + +def test_get_config(): + subprocess.call(["git", "config", "test.bool", "TRUE"]) + subprocess.call(["git", "config", "test.bool2", "fAlSe"]) + subprocess.call(["git", "config", "test.int", "42"]) + assert "TRUE" == dzonegit.get_config("test.bool") + assert dzonegit.get_config("test.bool", bool) + assert not dzonegit.get_config("test.bool2", bool) + assert 42 == dzonegit.get_config("test.int", int)