Initial upload: Local LLM Prompt Manager CLI tool
This commit is contained in:
82
src/git.py
Normal file
82
src/git.py
Normal file
@@ -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]
|
||||
Reference in New Issue
Block a user