Skip to content

Commit 82b81f3

Browse files
committed
refactor(git): extract_status -> GitStatus.from_stdout
1 parent cdf2041 commit 82b81f3

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

libvcs/projects/git.py

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,59 +69,59 @@ def to_dict(self):
6969
def to_tuple(self):
7070
return dataclasses.astuple(self)
7171

72+
@classmethod
73+
def from_stdout(cls, value: str):
74+
"""Returns ``git status -sb --porcelain=2`` extracted to a dict
7275
73-
def extract_status(value) -> GitStatus:
74-
"""Returns ``git status -sb --porcelain=2`` extracted to a dict
75-
76-
Returns
77-
-------
78-
Dictionary of git repo's status
79-
"""
80-
pattern = re.compile(
81-
r"""[\n\r]?
82-
(
83-
#
84-
\W+
85-
branch.oid\W+
86-
(?P<branch_oid>
87-
[a-f0-9]{40}
88-
)
89-
)?
90-
(
91-
#
92-
\W+
93-
branch.head
94-
[\W]+
95-
(?P<branch_head>
96-
.*
97-
)
76+
Returns
77+
-------
78+
Dictionary of git repo's status
79+
"""
80+
pattern = re.compile(
81+
r"""[\n\r]?
82+
(
83+
#
84+
\W+
85+
branch.oid\W+
86+
(?P<branch_oid>
87+
[a-f0-9]{40}
88+
)
89+
)?
90+
(
91+
#
92+
\W+
93+
branch.head
94+
[\W]+
95+
(?P<branch_head>
96+
.*
97+
)
9898
99-
)?
100-
(
101-
#
102-
\W+
103-
branch.upstream
104-
[\W]+
105-
(?P<branch_upstream>
106-
.*
107-
)
108-
)?
109-
(
110-
#
111-
\W+
112-
branch.ab
113-
[\W]+
114-
(?P<branch_ab>
115-
\+(?P<branch_ahead>\d+)
116-
\W{1}
117-
\-(?P<branch_behind>\d+)
118-
)
119-
)?
120-
""",
121-
re.VERBOSE | re.MULTILINE,
122-
)
123-
matches = pattern.search(value)
124-
return GitStatus(**matches.groupdict())
99+
)?
100+
(
101+
#
102+
\W+
103+
branch.upstream
104+
[\W]+
105+
(?P<branch_upstream>
106+
.*
107+
)
108+
)?
109+
(
110+
#
111+
\W+
112+
branch.ab
113+
[\W]+
114+
(?P<branch_ab>
115+
\+(?P<branch_ahead>\d+)
116+
\W{1}
117+
\-(?P<branch_behind>\d+)
118+
)
119+
)?
120+
""",
121+
re.VERBOSE | re.MULTILINE,
122+
)
123+
matches = pattern.search(value)
124+
return cls(**matches.groupdict())
125125

126126

127127
def convert_pip_url(pip_url: str) -> VCSLocation:
@@ -618,7 +618,7 @@ def status(self) -> dict:
618618
branch_behind='0'\
619619
)
620620
"""
621-
return extract_status(self.run(["status", "-sb", "--porcelain=2"]))
621+
return GitStatus.from_stdout(self.run(["status", "-sb", "--porcelain=2"]))
622622

623623
def get_current_remote_name(self) -> str:
624624
"""Retrieve name of the remote / upstream of currently checked out branch.

tests/projects/test_git.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
GitFullRemoteDict,
1717
GitProject,
1818
GitRemote,
19+
GitStatus,
1920
convert_pip_url as git_convert_pip_url,
20-
extract_status,
2121
)
2222
from libvcs.shortcuts import create_project_from_pip_url
2323

@@ -617,7 +617,7 @@ def test_get_current_remote_name(git_repo: GitProject):
617617
), "Should reflect new upstream branch (different branch)"
618618

619619

620-
def test_extract_status():
620+
def test_GitRemote_from_stdout():
621621
FIXTURE_A = textwrap.dedent(
622622
"""
623623
# branch.oid d4ccd4d6af04b53949f89fbf0cdae13719dc5a08
@@ -628,7 +628,7 @@ def test_extract_status():
628628
assert {
629629
"branch_oid": "d4ccd4d6af04b53949f89fbf0cdae13719dc5a08",
630630
"branch_head": "fix-current-remote-name",
631-
}.items() <= extract_status(FIXTURE_A).to_dict().items()
631+
}.items() <= GitStatus.from_stdout(FIXTURE_A).to_dict().items()
632632

633633

634634
@pytest.mark.parametrize(
@@ -674,9 +674,9 @@ def test_extract_status():
674674
],
675675
],
676676
)
677-
def test_extract_status_b(fixture: str, expected_result: dict):
677+
def test_GitRemote__from_stdout_b(fixture: str, expected_result: dict):
678678
assert (
679-
extract_status(textwrap.dedent(fixture)).to_dict().items()
679+
GitStatus.from_stdout(textwrap.dedent(fixture)).to_dict().items()
680680
>= expected_result.items()
681681
)
682682

@@ -724,10 +724,10 @@ def test_extract_status_b(fixture: str, expected_result: dict):
724724
],
725725
],
726726
)
727-
def test_extract_status_c(fixture: str, expected_result: dict):
727+
def test_GitRemote__from_stdout_c(fixture: str, expected_result: dict):
728728
assert (
729729
expected_result.items()
730-
<= extract_status(textwrap.dedent(fixture)).to_dict().items()
730+
<= GitStatus.from_stdout(textwrap.dedent(fixture)).to_dict().items()
731731
)
732732

733733

0 commit comments

Comments
 (0)