Skip to content

Commit 41c6871

Browse files
committed
refactor: Turn GitStatus into a NamedTuple
1 parent 93314e2 commit 41c6871

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

libvcs/git.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import logging
1919
import os
2020
import re
21-
from typing import NamedTuple
21+
from typing import NamedTuple, Optional
2222

2323
from . import exc
2424
from ._compat import urlparse
@@ -38,7 +38,16 @@ class GitRemote(NamedTuple):
3838
push_url: str
3939

4040

41-
def extract_status(value):
41+
class GitStatus(NamedTuple):
42+
branch_oid: Optional[str]
43+
branch_head: Optional[str]
44+
branch_upstream: Optional[str]
45+
branch_ab: Optional[str]
46+
branch_ahead: Optional[str]
47+
branch_behind: Optional[str]
48+
49+
50+
def extract_status(value) -> GitStatus:
4251
"""Returns ``git status -sb --porcelain=2`` extracted to a dict
4352
4453
Returns
@@ -90,7 +99,7 @@ def extract_status(value):
9099
re.VERBOSE | re.MULTILINE,
91100
)
92101
matches = pattern.search(value)
93-
return matches.groupdict()
102+
return GitStatus(**matches.groupdict())
94103

95104

96105
class GitRepo(BaseRepo):
@@ -474,6 +483,6 @@ def get_current_remote_name(self):
474483
"""
475484
match = self.status()
476485

477-
if match['branch_upstream'] is None: # no upstream set
478-
return match['branch_head']
479-
return match['branch_upstream'].replace('/' + match['branch_head'], '')
486+
if match.branch_upstream is None: # no upstream set
487+
return match.branch_head
488+
return match.branch_upstream.replace('/' + match.branch_head, '')

tests/test_git.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_extract_status():
264264
assert {
265265
"branch_oid": 'd4ccd4d6af04b53949f89fbf0cdae13719dc5a08',
266266
"branch_head": 'fix-current-remote-name',
267-
}.items() <= extract_status(FIXTURE_A).items()
267+
}.items() <= extract_status(FIXTURE_A)._asdict().items()
268268

269269

270270
@pytest.mark.parametrize(
@@ -313,11 +313,13 @@ def test_extract_status():
313313
def test_extract_status_b(fixture, expected_result):
314314
if PY2:
315315
assert (
316-
extract_status(textwrap.dedent(fixture)).items() <= expected_result.items()
316+
extract_status(textwrap.dedent(fixture))._asdict().items()
317+
<= expected_result.items()
317318
)
318319
else:
319320
assert (
320-
extract_status(textwrap.dedent(fixture)).items() >= expected_result.items()
321+
extract_status(textwrap.dedent(fixture))._asdict().items()
322+
>= expected_result.items()
321323
)
322324

323325

@@ -365,4 +367,7 @@ def test_extract_status_b(fixture, expected_result):
365367
],
366368
)
367369
def test_extract_status_c(fixture, expected_result):
368-
assert expected_result.items() <= extract_status(textwrap.dedent(fixture)).items()
370+
assert (
371+
expected_result.items()
372+
<= extract_status(textwrap.dedent(fixture))._asdict().items()
373+
)

0 commit comments

Comments
 (0)