From 0c6e5ed62ba0ee9bfd4e0c571193f944d07c48c6 Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 14 Jan 2025 14:11:04 -0800 Subject: [PATCH] add repo.commit shortcut --- CHANGES.md | 1 + src/modify_repos/repo/git.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 37d17c3..d17fc38 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ Unreleased - If a PR already exists from a previous failed run, force push to update the existing branch and PR. +- Add shortcut `repo.commit(message)` method. ## Version 0.1.1 diff --git a/src/modify_repos/repo/git.py b/src/modify_repos/repo/git.py index f3b3d81..5cca072 100644 --- a/src/modify_repos/repo/git.py +++ b/src/modify_repos/repo/git.py @@ -5,6 +5,7 @@ from shutil import which from subprocess import CompletedProcess from ..utils import run_cmd +from ..utils import wrap_text from .base import Repo @@ -64,6 +65,20 @@ class GitRepo(Repo): self.git_cmd("merge", "--ff-only", self.script.branch) self.git_cmd("push") + def commit(self, message: str, add: bool = False) -> None: + """Create a commit with the given message. + + :param message: The commit message. + :param add: Update tracked files while committing. Disabled by default. + Alternatively, call {meth}`add_files` first. + """ + args = ["commit", "--message", wrap_text(message, width=72)] + + if add: + args.insert(1, "-a") + + self.git_cmd(*args) + def add_files( self, *items: str | Path, update: bool = False, all: bool = False ) -> None: