Files
jrnl/jrnl/commands/regenerate.py

124 lines
3.9 KiB
Python

"""jrnl regenerate command - Regenerate commit compressions."""
import sqlite3
from ..database.operations import get_all_logs, get_connection
from ..config import Config
from ..llm_providers import get_provider
from ..utils.git_utils import extract_commit_info
from ..utils.formatting import format_error
def handle(args):
"""Handle the 'regenerate' command."""
try:
# Get all git-hook logs
all_logs = get_all_logs(limit=10000) # Get a large number
git_logs = [log for log in all_logs if log.type == "git-hook"]
if not git_logs:
print("No git commit logs found to regenerate.")
return 0
print(f"Found {len(git_logs)} git commit log(s).")
# Check if repo path is provided
if not args.repo_path:
print(
format_error(
"Repository path is required. Use: jrnl regenerate --repo-path /path/to/repo"
)
)
print("\nNote: The label field in logs contains commit hashes.")
print(" Provide the repo path where these commits exist.")
return 1
# Confirm with user
if not args.yes:
response = input(
f"\nRegenerate compressions for {len(git_logs)} log(s) from {args.repo_path}? (y/N): "
)
if response.lower() not in ["y", "yes"]:
print("Cancelled.")
return 0
# Load config and get LLM provider
config = Config.load()
provider = get_provider(config)
if args.debug and hasattr(provider, "set_debug"):
provider.set_debug(True)
# Process each log
success_count = 0
skip_count = 0
error_count = 0
for i, log in enumerate(git_logs, 1):
commit_hash = log.label
print(
f"\n[{i}/{len(git_logs)}] Processing commit {commit_hash}...", end=" "
)
# Extract commit info
commit_info = extract_commit_info(args.repo_path, commit_hash)
if not commit_info:
print("⊘ Skipped (commit not found in repo)")
skip_count += 1
continue
# Compress with LLM
try:
new_message = provider.compress_commit(
commit_message=commit_info["message"],
commit_diff=commit_info["diff"],
)
# Update the log
if update_log_message(log.id, new_message):
print("✓ Updated")
success_count += 1
else:
print("❌ Failed to update database")
error_count += 1
except Exception as e:
print(f"❌ Error: {e}")
error_count += 1
# Summary
print("\n" + "=" * 60)
print("Regeneration complete:")
print(f" ✓ Successfully regenerated: {success_count}")
print(f" ⊘ Skipped: {skip_count}")
print(f" ❌ Errors: {error_count}")
print("=" * 60)
return 0 if error_count == 0 else 1
except sqlite3.Error as e:
print(format_error(f"Database error: {e}"))
return 1
except RuntimeError as e: # From LLM providers or config
print(format_error(f"Error: {e}"))
return 1
except Exception as e:
print(format_error(f"Unexpected error: {e}"))
import traceback
traceback.print_exc()
return 1
def update_log_message(log_id: int, new_message: str) -> bool:
"""Update a log entry's message."""
try:
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"""UPDATE logs SET log_message = ? WHERE id = ?""",
(new_message, log_id),
)
return cursor.rowcount > 0
except Exception:
return False