-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgit_service.py
More file actions
141 lines (118 loc) · 4.86 KB
/
git_service.py
File metadata and controls
141 lines (118 loc) · 4.86 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
from ai_commit_msg.utils.utils import execute_cli_command
from enum import Enum
class GitConfigKeysEnum(Enum):
hookPath = "core.hooksPath"
class GitService:
def __init__(self):
return
@staticmethod
def get_staged_diff():
return execute_cli_command(
["git", "diff", "--staged"],
cwd=GitService.get_repo_root_directory(),
output=False,
)
@staticmethod
def get_repo_root_directory():
git_directory = execute_cli_command(
["git", "rev-parse", "--show-toplevel"]
).stdout.rstrip()
return git_directory
@staticmethod
def get_git_directory():
script_directory = execute_cli_command(
["git", "rev-parse", "--git-dir"]
).stdout.rstrip()
return script_directory
@staticmethod
def get_commit_editmsg_file_path():
git_directory = GitService.get_repo_root_directory()
commit_editmsg_file = git_directory + "/.git/COMMIT_EDITMSG"
return commit_editmsg_file
@staticmethod
def read_commit_editmsg_file():
existing_content = ""
try:
with open(GitService.get_commit_editmsg_file_path(), "r") as file:
existing_content = file.read()
except FileNotFoundError:
pass
return existing_content
@staticmethod
def update_commit_message(commit_message):
existing_content = GitService.read_commit_editmsg_file()
new_content = commit_message + existing_content
with open(GitService.get_commit_editmsg_file_path(), "w") as file:
file.write(new_content)
@staticmethod
def get_staged_files():
staged_files = execute_cli_command(
["git", "diff", "--name-only", "--cached"]
).stdout.splitlines()
return staged_files
@staticmethod
def get_current_branch():
return execute_cli_command(
["git", "rev-parse", "--abbrev-ref", "HEAD"]
).stdout.strip()
@staticmethod
def has_upstream_branch(branch_name):
try:
execute_cli_command(
["git", "rev-parse", "--abbrev-ref", f"{branch_name}@{{u}}"]
)
return True
except Exception:
return False
@staticmethod
def is_git_installed():
try:
execute_cli_command(["git", "--version"])
return True
except Exception:
return False
@staticmethod
def is_git_repository():
try:
execute_cli_command(["git", "rev-parse", "--is-inside-work-tree"])
return True
except Exception:
return False
@staticmethod
def get_success_banner():
return """
#############################################################
# #
# This commit message was generated by `ai-commit-msg` #
# ⚡️⚡️⚡️ #
# Learn more at https://github.com/the-cafe/git-ai-commit #
# #
#############################################################
"""
@staticmethod
def get_error_banner(error_message):
return f"""
#########################################################################################################################################
# #
# An error occurred while generating the commit message using AI. Please enter your commit message manually. #
# #
# {error_message} #
# #
#########################################################################################################################################
"""
@staticmethod
def get_git_config_value(key: GitConfigKeysEnum):
return execute_cli_command(["git", "config", "--local", key]).stdout.strip()
@staticmethod
def get_git_prepare_commit_msg_hook_path():
# Use git's built-in command to get the correct hooks directory
# This properly handles worktrees and other git configurations
hooks_dir = execute_cli_command(
["git", "rev-parse", "--git-path", "hooks"]
).stdout.strip()
return hooks_dir + "/prepare-commit-msg"
@staticmethod
def get_last_n_commit_msg(n):
return execute_cli_command(
["git", "log", f"-n {n}", "--pretty=format:%s"]
).stdout.splitlines()