-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_manager.py
More file actions
71 lines (60 loc) · 2.31 KB
/
Copy pathpatch_manager.py
File metadata and controls
71 lines (60 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import json
import shutil
import subprocess
from datetime import datetime
from tabulate import tabulate
PATCHES_DIRNAME = ".gitpatch/patches"
METADATA_FILENAME = ".gitpatch/metadata.json"
def ensure_dirs(storage_path):
os.makedirs(os.path.join(storage_path, PATCHES_DIRNAME), exist_ok=True)
def load_metadata(storage_path):
metadata_path = os.path.join(storage_path, METADATA_FILENAME)
if not os.path.exists(metadata_path):
return []
with open(metadata_path, "r") as f:
return json.load(f)
def save_metadata(storage_path, metadata):
metadata_path = os.path.join(storage_path, METADATA_FILENAME)
with open(metadata_path, "w") as f:
json.dump(metadata, f, indent=4)
def save_patch(patch_file, name, tag, storage_path):
ensure_dirs(storage_path)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
patch_filename = f"{timestamp}_{name}_{tag}.patch"
dest_path = os.path.join(storage_path, PATCHES_DIRNAME, patch_filename)
shutil.copy(patch_file, dest_path)
metadata = load_metadata(storage_path)
metadata.append({
"filename": patch_filename,
"name": name,
"tag": tag,
"timestamp": timestamp
})
save_metadata(storage_path, metadata)
print(f"✅ Patch saved as {patch_filename}")
def list_patches(storage_path):
metadata = load_metadata(storage_path)
if not metadata:
print("No patches found.")
return
table = [
[m["filename"], m["name"], m["tag"], m["timestamp"]]
for m in metadata
]
print(tabulate(table, headers=["Filename", "Name", "Tag", "Timestamp"], tablefmt="grid"))
def apply_patch(name, tag, storage_path):
metadata = load_metadata(storage_path)
matching = [m for m in metadata if m["name"] == name and m["tag"] == tag]
if not matching:
print("❌ No matching patches found.")
return
# Sort by timestamp, newest first
latest = sorted(matching, key=lambda m: m["timestamp"], reverse=True)[0]
patch_path = os.path.join(storage_path, PATCHES_DIRNAME, latest["filename"])
try:
subprocess.run(["git", "apply", patch_path], check=True)
print(f"✅ Applied patch: {latest['filename']}")
except subprocess.CalledProcessError as e:
print(f"❌ Failed to apply patch: {latest['filename']}")
print(e)