backup-2025-12-19-20: Automated backup - 10 files changed
📋 Files modified: 10 ⏰ Timestamp: 2025-12-19 20:27:31 UTC 🔒 Security: PASSED (no secrets detected) 💾 Automated by Git Agent
This commit is contained in:
138
tools/commands/git_restore.py
Normal file
138
tools/commands/git_restore.py
Normal file
@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Git Restore Slash Command
|
||||
Usage: /git-restore [timestamp]
|
||||
Restores from a backup branch
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
def get_backup_branches():
|
||||
"""Get list of backup branches"""
|
||||
project_root = "K:\\Projects\\uniswap_auto_clp"
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", "branch", "-a"],
|
||||
cwd=project_root,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
branches = []
|
||||
for line in result.stdout.strip().split('\n'):
|
||||
branch = line.strip().replace('* ', '').replace('remotes/origin/', '')
|
||||
if branch.startswith('backup-'):
|
||||
branches.append(branch)
|
||||
|
||||
branches.sort(key=lambda x: x.replace('backup-', ''), reverse=True)
|
||||
return branches
|
||||
|
||||
return []
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def format_backup_time(time_input):
|
||||
"""Convert user time input to backup branch name format"""
|
||||
try:
|
||||
if len(time_input) == 10: # YYYY-MM-DD
|
||||
return f"backup-{time_input}"
|
||||
elif len(time_input) == 13: # YYYY-MM-DD-HH
|
||||
return f"backup-{time_input}"
|
||||
elif len(time_input) == 8: # MM-DD-HH
|
||||
current_year = datetime.now().year
|
||||
return f"backup-{current_year}-{time_input}"
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def format_timestamp_display(timestamp):
|
||||
"""Format backup timestamp for display"""
|
||||
try:
|
||||
if len(timestamp) >= 10:
|
||||
date_part = timestamp[:10]
|
||||
if len(timestamp) >= 13:
|
||||
time_part = timestamp[11:13] + ":00"
|
||||
return f"{date_part} {time_part} UTC"
|
||||
return f"{date_part}"
|
||||
return timestamp
|
||||
except Exception:
|
||||
return timestamp
|
||||
|
||||
def main():
|
||||
"""Execute git restore command"""
|
||||
time_input = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
project_root = "K:\\Projects\\uniswap_auto_clp"
|
||||
|
||||
if not time_input:
|
||||
# Show available backups
|
||||
branches = get_backup_branches()
|
||||
if not branches:
|
||||
print("[INFO] No backup branches found")
|
||||
print("Use `/git-backup` to create a backup first.")
|
||||
return
|
||||
|
||||
response = "[INFO] Available Backups\n\nChoose a backup to restore:\n"
|
||||
for i, branch in enumerate(branches[:10]): # Show last 10
|
||||
timestamp = branch.replace('backup-', '')
|
||||
formatted_time = format_timestamp_display(timestamp)
|
||||
response += f"- {timestamp} - {formatted_time}\n"
|
||||
|
||||
if len(branches) > 10:
|
||||
response += f"\n... and {len(branches) - 10} more backups"
|
||||
|
||||
response += "\n\nUsage: `/git-restore <timestamp>` (e.g., 2025-12-19-14)"
|
||||
print(response)
|
||||
return
|
||||
|
||||
# Try to restore specific backup
|
||||
branch_name = format_backup_time(time_input)
|
||||
if not branch_name:
|
||||
print("[ERROR] Invalid time format")
|
||||
print("Expected format: YYYY-MM-DD-HH (e.g., 2025-12-19-14)")
|
||||
return
|
||||
|
||||
# Check if branch exists
|
||||
branches = get_backup_branches()
|
||||
matching_branches = [b for b in branches if branch_name in b]
|
||||
|
||||
if not matching_branches:
|
||||
print("[ERROR] Backup not found")
|
||||
print(f"No backup found for: {time_input}")
|
||||
print("Use `/git-restore` to see available backups.")
|
||||
return
|
||||
|
||||
# Use the most recent matching branch
|
||||
target_branch = matching_branches[0]
|
||||
|
||||
try:
|
||||
# Checkout the backup branch
|
||||
result = subprocess.run(
|
||||
["git", "checkout", target_branch],
|
||||
cwd=project_root,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
timestamp = target_branch.replace('backup-', '')
|
||||
formatted_time = format_timestamp_display(timestamp)
|
||||
print("[SUCCESS] Restored to backup")
|
||||
print(f"Branch: {target_branch}")
|
||||
print(f"Time: {formatted_time}")
|
||||
print("Note: You're now on a backup branch. Use `git checkout main` to return to the main branch when done.")
|
||||
else:
|
||||
print("[ERROR] Restore failed")
|
||||
print(f"Error: {result.stderr.strip()}")
|
||||
|
||||
except Exception as e:
|
||||
print("[ERROR] Restore failed")
|
||||
print(f"Exception: {str(e)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user