Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ authors = [
requires-python = ">=3.10"

dependencies = [
"gitpython>=3.1.52",
"pyyaml>=6.0.3",
"httpx[socks]>=0.28.1",
"packaging>=26.2",
Expand Down
6 changes: 3 additions & 3 deletions src/composekit/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ def main(args: argparse.Namespace) -> None:

if args.commit:
repo = open_repo(reset=False)
repo.git.add(".")
staged_count = len(repo.index.diff(repo.head.commit))
repo.add(".")
staged_count = repo.staged_count()
if staged_count > 0:
repo.index.commit(
repo.commit(
f"chore(composes): update {staged_count} compose file(s)"
)
9 changes: 4 additions & 5 deletions src/composekit/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

try:
import yaml
from git import Repo

from composekit.utils import iter_container_files, open_repo
from composekit.utils import Repository, iter_container_files, open_repo
except ImportError as err:
raise RuntimeError(
"ERROR: Missing required packages. See the README."
Expand All @@ -20,7 +19,7 @@

async def process_file(
path: Path,
repo: Repo | None,
repo: Repository | None,
git_lock: asyncio.Lock,
) -> None:
with open(path) as file:
Expand All @@ -41,8 +40,8 @@ async def process_file(
yaml.dump_all(sorted_containers, file, sort_keys=False)

if repo is not None:
repo.index.add(path)
repo.index.commit(f"chore({path.stem}): sort keys")
repo.add(path)
repo.commit(f"chore({path.stem}): sort keys")


def main(args: argparse.Namespace) -> None:
Expand Down
14 changes: 9 additions & 5 deletions src/composekit/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
try:
import httpx
import yaml
from git import Repo
from packaging.version import InvalidVersion, Version

from composekit.container import Container, load_containers
from composekit.utils import Config as _Config
from composekit.utils import iter_container_files, list_tags, open_repo
from composekit.utils import (
Repository,
iter_container_files,
list_tags,
open_repo,
)
except ImportError as err:
raise RuntimeError(
"ERROR: Missing required packages. See the README."
Expand Down Expand Up @@ -202,7 +206,7 @@ async def process_file(
path: Path,
client: httpx.AsyncClient,
config: Config,
repo: Repo | None,
repo: Repository | None,
git_lock: asyncio.Lock,
) -> None:
with open(path) as file:
Expand All @@ -224,8 +228,8 @@ async def process_file(
)

if repo is not None:
repo.index.add(path)
repo.index.commit(
repo.add(path)
repo.commit(
f"chore({path.stem}): update {image} to {newest_version}"
)

Expand Down
10 changes: 8 additions & 2 deletions src/composekit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from .config import Config
from .file import iter_container_files
from .git import open_repo
from .git import Repository, open_repo
from .oci_api import list_tags

__all__ = ("Config", "iter_container_files", "list_tags", "open_repo")
__all__ = (
"Config",
"Repository",
"iter_container_files",
"list_tags",
"open_repo",
)
51 changes: 41 additions & 10 deletions src/composekit/utils/git.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
try:
from git import Repo
except ImportError as err:
raise RuntimeError(
"ERROR: Missing required packages. See the README."
) from err
import shutil
import subprocess

Check notice on line 2 in src/composekit/utils/git.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/composekit/utils/git.py#L2

Consider possible security implications associated with the subprocess module.
from dataclasses import dataclass
from pathlib import Path

_git = shutil.which("git")
if _git is None:
raise RuntimeError("ERROR: Git is required but was not found in PATH.")

def open_repo(reset: bool = True) -> Repo:
repo = Repo(".", search_parent_directories=True)
GIT: str = _git


@dataclass(frozen=True)
class Repository:
root: Path

def _run(self, *args: str) -> str:
result = subprocess.check_output( # noqa: S603

Check failure on line 18 in src/composekit/utils/git.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/composekit/utils/git.py#L18

Detected subprocess function 'check_output' without a static string.

Check warning on line 18 in src/composekit/utils/git.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/composekit/utils/git.py#L18

subprocess call - check for execution of untrusted input.
[GIT, "-C", self.root, *args], text=True
)
return result

def add(self, path: str | Path) -> None:
self._run("add", "--", str(path))

def commit(self, message: str) -> None:
self._run("commit", "-m", message)

def reset(self) -> None:
self._run("reset", "--hard", "HEAD")

def staged_count(self) -> int:
output = self._run("diff", "--cached", "--name-only", "-z")
return len([path for path in output.split("\0") if path])


def open_repo(reset: bool = True) -> Repository:
result = subprocess.check_output( # noqa: S603
[GIT, "rev-parse", "--show-toplevel"],
text=True,
)
repo = Repository(Path(result.strip()))
if reset:
# Discard any changes
repo.index.reset(working_tree=True)
repo.reset()

return repo
35 changes: 0 additions & 35 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.