-
Notifications
You must be signed in to change notification settings - Fork 1
/
automate_git_commit.py
117 lines (94 loc) · 3.79 KB
/
automate_git_commit.py
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
# automate_git_commit.py
import subprocess
def get_git_status():
"""Run `git status` command and return its output."""
result = subprocess.run(
["git", "status", "--porcelain"], stdout=subprocess.PIPE, text=True)
return result.stdout.strip().split('\n')
def parse_git_status(status_lines):
"""Parse the output of `git status --porcelain` and organize file changes."""
changes = {'A': [], 'M': [], 'D': []
} # A for added, M for modified, D for deleted
for line in status_lines:
if line: # Ignore empty lines
status = line[0]
filename = line[3:]
if status in changes:
changes[status].append(filename)
elif status == 'R': # Handle renamed files
_, old_file, new_file = line.split(" ")
changes['D'].append(old_file)
changes['A'].append(new_file)
return changes
def generate_commit_message(changes):
"""Generate a commit message based on the file changes."""
messages = []
if changes['A']:
messages.append("Added: " + ", ".join(changes['A']))
if changes['M']:
messages.append("Modified: " + ", ".join(changes['M']))
if changes['D']:
messages.append("Deleted: " + ", ".join(changes['D']))
return "; ".join(messages)
def commit_changes(message):
"""Commit changes with the given commit message."""
subprocess.run(["git", "add", "."]) # Stage all changes
subprocess.run(["git", "commit", "-m", message])
# Main workflow
status_lines = get_git_status()
if status_lines:
changes = parse_git_status(status_lines)
commit_message = generate_commit_message(changes)
if commit_message: # Proceed to commit if there are changes
commit_changes(commit_message)
print(f"Committed with message: '{commit_message}'")
else:
print("No changes to commit.")
else:
print("No changes detected.")
def get_git_status():
"""Run `git status` command and return its output."""
result = subprocess.run(
["git", "status", "--porcelain"], stdout=subprocess.PIPE, text=True)
return result.stdout.strip().split('\n')
def parse_git_status(status_lines):
"""Parse the output of `git status --porcelain` and organize file changes."""
changes = {'A': [], 'M': [], 'D': []
} # A for added, M for modified, D for deleted
for line in status_lines:
if line: # Ignore empty lines
status = line[0]
filename = line[3:]
if status in changes:
changes[status].append(filename)
elif status == 'R': # Handle renamed files
_, old_file, new_file = line.split(" ")
changes['D'].append(old_file)
changes['A'].append(new_file)
return changes
def generate_commit_message(changes):
"""Generate a commit message based on the file changes."""
messages = []
if changes['A']:
messages.append("Added: " + ", ".join(changes['A']))
if changes['M']:
messages.append("Modified: " + ", ".join(changes['M']))
if changes['D']:
messages.append("Deleted: " + ", ".join(changes['D']))
return "; ".join(messages)
def commit_changes(message):
"""Commit changes with the given commit message."""
subprocess.run(["git", "add", "."]) # Stage all changes
subprocess.run(["git", "commit", "-m", message])
# Main workflow
status_lines = get_git_status()
if status_lines:
changes = parse_git_status(status_lines)
commit_message = generate_commit_message(changes)
if commit_message: # Proceed to commit if there are changes
commit_changes(commit_message)
print(f"Committed with message: '{commit_message}'")
else:
print("No changes to commit.")
else:
print("No changes detected.")