From bffb8d1c8af25001a1f254390825b7da493f5249 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 5 Feb 2026 20:56:04 +0000 Subject: [PATCH] Initial upload: Local LLM Prompt Manager CLI tool --- src/git.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/git.py diff --git a/src/git.py b/src/git.py new file mode 100644 index 0000000..0c0078c --- /dev/null +++ b/src/git.py @@ -0,0 +1,82 @@ +"""Git utilities for commit message generation.""" + +import subprocess +from pathlib import Path +from typing import Optional + + +class GitUtils: + """Utilities for git operations.""" + + @staticmethod + def get_staged_diff() -> str: + """Get the diff of staged changes.""" + try: + result = subprocess.run( + ["git", "diff", "--cached"], + capture_output=True, + text=True, + check=True + ) + return result.stdout + except subprocess.CalledProcessError: + return "" + + @staticmethod + def get_staged_files() -> list[str]: + """Get list of staged files.""" + try: + result = subprocess.run( + ["git", "diff", "--cached", "--name-only"], + capture_output=True, + text=True, + check=True + ) + return result.stdout.strip().split("\n") if result.stdout.strip() else [] + except subprocess.CalledProcessError: + return [] + + @staticmethod + def get_unstaged_diff() -> str: + """Get the diff of unstaged changes.""" + try: + result = subprocess.run( + ["git", "diff"], + capture_output=True, + text=True, + check=True + ) + return result.stdout + except subprocess.CalledProcessError: + return "" + + @staticmethod + def get_repo_root() -> Optional[Path]: + """Get the root directory of the git repository.""" + try: + result = subprocess.run( + ["git", "rev-parse", "--show-toplevel"], + capture_output=True, + text=True, + check=True + ) + return Path(result.stdout.strip()) + except subprocess.CalledProcessError: + return None + + @staticmethod + def is_git_repo() -> bool: + """Check if the current directory is a git repository.""" + return GitUtils.get_repo_root() is not None + + @staticmethod + def get_diff_summary(diff: str) -> str: + """Get a brief summary of changes from a diff.""" + lines = diff.split("\n") + summary_lines = [] + for line in lines[:50]: + if line.startswith("+++") or line.startswith("---"): + continue + if line.startswith("+") or line.startswith("-"): + summary_lines.append(line[:80]) + return "\n".join(summary_lines)[:1000]