Skip to content

Commit 741086c

Browse files
authored
Merge pull request #385 from legendu-net/dev
Merge dev into main
2 parents edd419b + 4d97d32 commit 741086c

File tree

5 files changed

+791
-599
lines changed

5 files changed

+791
-599
lines changed

aiutil/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"""
33
from . import poetry
44

5-
__version__ = "0.80.6"
5+
__version__ = "0.81.0"

aiutil/git.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""A module providing wrap over the git command.
2+
"""
3+
from pathlib import Path
4+
import subprocess as sp
5+
6+
7+
class Repo:
8+
"""A local Git repository."""
9+
10+
def __init__(self, root: str | Path):
11+
self._root = str(root)
12+
self._remote = self.remote()
13+
14+
def remote(self) -> str:
15+
"""Run the "git remote" command."""
16+
proc = sp.run(
17+
["git", "-C", self._root, "remote"], check=True, capture_output=True
18+
)
19+
return proc.stdout.strip().decode()
20+
21+
def pull(self, branch: str) -> None:
22+
"""Run the "git pull" command."""
23+
sp.run(["git", "-C", self._root, "pull", self._remote, branch], check=True)
24+
25+
def push(self, branch: str = "") -> None:
26+
"""Run the "git push" command."""
27+
if not branch:
28+
branch = self.active_branch()
29+
sp.run(["git", "-C", self._root, "push", self._remote, branch], check=True)
30+
31+
def _branch(self) -> list[str]:
32+
proc = sp.run(
33+
["git", "-C", self._root, "branch"], check=True, capture_output=True
34+
)
35+
return proc.stdout.strip().decode().split("\n")
36+
37+
def active_branch(self) -> str:
38+
"""Get the active branch of the repository."""
39+
lines = self._branch()
40+
return next(line[2:] for line in lines if line.startswith("* "))
41+
42+
def branch(self):
43+
"""Run the "git branch" command to get all branches of the repository."""
44+
return [b[2:] for b in self._branch()]
45+
46+
def add(self, pattern: str = "."):
47+
"""Run the "git add" command."""
48+
sp.run(["git", "-C", self._root, "add", pattern], check=True)
49+
50+
def commit(self, message: str):
51+
"""Run the "git commit" command."""
52+
sp.run(["git", "-C", self._root, "commit", "-m", message], check=True)
53+
54+
def status(self):
55+
"""Run the "git status" command."""
56+
sp.run(["git", "-C", self._root, "status"], check=True)
57+
58+
def tag(self, tag="") -> sp.CompletedProcess | list[str]:
59+
"""Run the "git tag" command."""
60+
if tag:
61+
return sp.run(["git", "tag", tag], check=True)
62+
proc = sp.run(["git", "tag"], check=True, capture_output=True)
63+
return proc.stdout.strip().decode().split("\n")
64+
65+
def checkout(self, branch: str = "", new: str = ""):
66+
"""Run the "git checkout" command."""
67+
if new:
68+
return sp.run(["git", "checkout", "-b", new], check=True)
69+
if not branch:
70+
raise ValueError("Either new or branch is required!")
71+
return sp.run(["git", "checkout", branch], check=True)

aiutil/poetry.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
import toml
1010
from loguru import logger
1111
import pathspec
12-
import dulwich
13-
from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor
1412
from .filesystem import replace_patterns
13+
from . import git
1514

1615
DIST = "dist"
1716
README = "README.md"
1817
TOML = "pyproject.toml"
19-
dulwich.client.get_ssh_vendor = ParamikoSSHVendor
2018

2119

2220
def _project_dir() -> Path:
@@ -30,9 +28,7 @@ def _project_dir() -> Path:
3028
if (path / TOML).is_file():
3129
return path
3230
path = path.parent
33-
raise RuntimeError(
34-
f"The current work directory {Path.cwd()} is not a (subdirectory of a) Python Poetry project."
35-
)
31+
return Path()
3632

3733

3834
def _project_name(proj_dir: Path) -> str:
@@ -118,10 +114,10 @@ def version(
118114
if ver:
119115
_update_version(ver=ver, proj_dir=proj_dir)
120116
if commit:
121-
repo = dulwich.repo.Repo(proj_dir)
122-
dulwich.porcelain.add(repo=repo)
123-
dulwich.porcelain.commit(repo=repo, message="bump up version")
124-
dulwich.porcelain.push(repo=repo)
117+
repo = git.Repo(root=proj_dir)
118+
repo.add()
119+
repo.commit(message="bump up version")
120+
repo.push()
125121
else:
126122
print(_project_version(proj_dir))
127123

@@ -137,25 +133,23 @@ def add_tag_release(
137133
:param branch_release: The branch for releasing.
138134
:raises ValueError: If the tag to create already exists.
139135
"""
136+
if proj_dir is None:
137+
proj_dir = _project_dir()
140138
if not tag:
141-
if proj_dir is None:
142-
proj_dir = _project_dir()
143139
tag = "v" + _project_version(proj_dir)
144-
repo = dulwich.repo.Repo(proj_dir)
145-
if tag.encode() in dulwich.porcelain.tag_list(repo):
140+
repo = git.Repo(proj_dir)
141+
if tag.encode() in repo.tag():
146142
raise ValueError(
147143
f"The tag {tag} already exists! Please merge new changes to the {branch_release} branch first."
148144
)
149-
branch_old = dulwich.porcelain.active_branch(repo=repo).decode()
145+
branch_old = repo.active_branch()
150146
# add tag to the release branch
151-
dulwich.porcelain.checkout_branch(repo=repo, target=branch_release)
152-
dulwich.porcelain.pull(repo=repo, refspecs=branch_release)
153-
dulwich.porcelain.tag_create(repo=repo, tag=tag, annotated=True)
154-
remote = dulwich.porcelain.get_branch_remote(repo=repo).decode()
155-
sp.run(f"git push {remote} {tag}", shell=True, check=True)
156-
# dulwich.porcelain.push(repo=repo, refspecs=[f"refs/tags/{tag}"])
147+
repo.checkout(branch=branch_release)
148+
repo.pull(branch=branch_release)
149+
repo.tag(tag=tag)
150+
repo.push(branch=tag)
157151
# switch back to the old branch
158-
dulwich.porcelain.checkout_branch(repo=repo, target=branch_old)
152+
repo.checkout(branch=branch_old)
159153

160154

161155
def format_code(
@@ -184,11 +178,11 @@ def format_code(
184178
logger.info("Formatting code using black ...")
185179
sp.run(cmd, shell=True, check=False, stdout=sp.PIPE)
186180
if commit:
187-
repo = dulwich.repo.Repo(proj_dir)
188-
dulwich.porcelain.add(repo=repo)
189-
dulwich.porcelain.commit(repo=repo, message="format code")
190-
dulwich.porcelain.push(repo=repo)
191-
print(dulwich.porcelain.status())
181+
repo = git.Repo(proj_dir)
182+
repo.add()
183+
repo.commit(message="format code")
184+
repo.push()
185+
repo.status()
192186

193187

194188
def _lint_code(proj_dir: Union[Path, None], linter: Union[str, list[str]]):

0 commit comments

Comments
 (0)