-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit_generation.py
More file actions
146 lines (133 loc) · 4.8 KB
/
commit_generation.py
File metadata and controls
146 lines (133 loc) · 4.8 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import subprocess
import sys
from typing import List
import shutil
# --- EXAMPLE OF OUTPUT:
# 🗂️ Commit category:
# 1. DELETED FILE/DIR
# 2. CHANGED FILE/DIR
# 3. BUGFIX IN FILE/DIR
# 4. HOTFIX
# 5. NEW FILE/DIR
# > 2
#
# ⚡ Changed files:
# M commit_generation.py
# A python/cargo_install.py
#
# ❓ Files/dirs changed (comma separated):
# python/cargo_install.py, .deepsource.toml
#
# 📜 Short description:
# Optimized script speed, added some files to ignores
# --- AUTO-GENERATION OF COMMITS
class CommitGen:
def __init__(self) -> None:
self.msg = ""
self.git_path = str(shutil.which("git"))
if not self.git_path:
print("❌ Git not found in PATH!")
sys.exit(1)
self.categories = {
1: "DELETED FILE/DIR",
2: "CHANGED FILE/DIR",
3: "BUGFIX IN FILE/DIR",
4: "HOTFIX",
5: "NEW FILE/DIR",
}
def get_category(self) -> list[str]:
"""Get multiple commit categories from user input."""
print(
"\n🗂️ Commit categories (enter numbers separated by commas or spaces, e.g., 2,5): "
)
for num, name in self.categories.items():
print(f"{num}. {name}")
while True:
try:
choices_input = input("> ").strip().replace(",", " ")
if not choices_input:
print("❌ At least one category must be selected.")
continue
choices = [int(x) for x in choices_input.split() if x]
if all(choice in self.categories for choice in choices):
return [self.categories[choice] for choice in choices]
print("❌ Invalid choice(s), select valid numbers.")
except ValueError:
print("❌ Enter numbers, not text.")
def show_git_changes(self) -> None:
"""Show changed files."""
try:
result = subprocess.run(
[str(self.git_path), "status", "--short"],
text=True,
capture_output=True,
check=True,
)
lines = result.stdout.strip().split("\n")
if not lines or lines == [""]:
print("✅ No unstaged changes")
else:
print("⚡ Changed files:")
for line in lines:
print(" " + line)
except subprocess.CalledProcessError as e:
print(f"❌ Git error:\n{e.stdout}\n{e.stderr}")
except FileNotFoundError:
print("❌ Git command not found!")
except PermissionError:
print("❌ Permission denied for git operations!")
@staticmethod
def get_changed_files() -> List[str]:
"""Get list of changed files/dirs from user input."""
while True:
files_input = input(
"""\n❓ Files/dirs changed
(comma separated):\n"""
).strip()
if not files_input:
print("❌ No files/dirs provided, try again.")
continue
files = [f.strip() for f in files_input.split(",") if f.strip()]
if files:
return files
print("❌ Invalid input, provide at least one file/dir.")
@staticmethod
def get_description() -> str:
"""Get short description from user input."""
while True:
desc = input("\n📜 Description:\n").strip()
if desc:
return desc
print("❌ Description cannot be empty, try again.")
def run(self) -> None:
"""Generate and execute commit."""
categories = self.get_category()
self.show_git_changes()
changed_files = ", ".join(self.get_changed_files())
description = self.get_description()
category_str = ", ".join(categories)
self.msg = f"[{category_str}: {changed_files}] {description}"
print(f"\n✅ Commit message:\n{self.msg}")
try:
subprocess.run([self.git_path, "add", "."], check=True)
subprocess.run([self.git_path, "commit", "-m", self.msg], check=True)
except subprocess.CalledProcessError as e:
print(f"❌ Git error:\n{e.stdout}\n{e.stderr}")
return
push_question = (
input(
"""\nYou want to push it on
current branch? (Yes/No): """
)
.lower()
.strip()
)
if push_question in ("y", "yes"):
try:
subprocess.run([self.git_path, "push"], check=True)
print("✅ Push successful!")
except subprocess.CalledProcessError as e:
print(f"❌ Push failed: {e.stderr}")
if __name__ == "__main__":
CommitGen().run()