gmsg 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gmsg/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .gmsg import main
gmsg/api_key.py ADDED
@@ -0,0 +1,20 @@
1
+ import sys
2
+ import os
3
+
4
+ from pathlib import Path
5
+
6
+
7
+ def get_or_set_api_key():
8
+ try:
9
+ rc = os.path.join(Path.home(), ".gmsgrc")
10
+ if not os.path.isfile(rc):
11
+ key_inp = input("Enter your Gemini API KEY: ").strip()
12
+ with open(rc, 'w') as f:
13
+ f.write(key_inp)
14
+
15
+ return key_inp
16
+
17
+ with open(rc, 'r') as f:
18
+ return f.read().strip()
19
+ except KeyboardInterrupt:
20
+ sys.exit(0)
gmsg/gmsg.py ADDED
@@ -0,0 +1,104 @@
1
+ import sys
2
+ import subprocess
3
+ from google import genai
4
+ from pathlib import Path
5
+ from .api_key import get_or_set_api_key
6
+
7
+
8
+ def is_git_repo() -> bool:
9
+ path = Path(Path.cwd())
10
+ for parent in [path] + list(path.parents):
11
+ if (parent / ".git").exists():
12
+ return True
13
+ return False
14
+
15
+
16
+ def git_diff() -> str | None:
17
+ try:
18
+ result = subprocess.run(["git", "diff", "--cached"],
19
+ stdout=subprocess.PIPE,
20
+ stderr=subprocess.PIPE,
21
+ check=True,
22
+ text=True)
23
+
24
+ if not result.stdout:
25
+ return None
26
+ return result.stdout
27
+ except subprocess.CalledProcessError:
28
+ sys.exit(1)
29
+
30
+
31
+ def make_query(diff) -> str:
32
+ prompt = f"Generate a one liner git commit message for these changes, Below are the changes:\n {diff}"
33
+ return prompt
34
+
35
+
36
+ def generate_commit_message(diff: str) -> str:
37
+ try:
38
+ query = make_query(diff)
39
+ while True:
40
+ client = genai.Client(api_key=USER_API_KEY)
41
+ msg = client.models.generate_content(model="gemini-2.0-flash",
42
+ contents=query)
43
+
44
+ print(msg.text)
45
+ # generated_msg = "feat: Add poetry.lock and pyproject.toml files."
46
+ # print(generated_msg)
47
+ if continue_with_this_prompt(msg.text):
48
+ break
49
+ except genai.errors.ClientError as error:
50
+ if error.code == 400:
51
+ print(dict(error="Gemini API key not valid, check ~/.gmsg"))
52
+ else:
53
+ print(error)
54
+ sys.exit(1)
55
+
56
+
57
+ def continue_with_this_prompt(commit_msg: str) -> bool:
58
+ action = input("Do you want to continue with this message [Y/n]: ")
59
+ if action in ("", "y", "Y"):
60
+ print(f"Running: `git commit -m {commit_msg}`")
61
+ print(
62
+ f"Message commited to git, to confirm you can run git commit --amend."
63
+ )
64
+ commit_message_to_git(commit_msg)
65
+ return True
66
+ elif action in ("n", "N"):
67
+ return False
68
+
69
+
70
+ def commit_message_to_git(generated_msg: str) -> str | None:
71
+ try:
72
+ result = subprocess.run(["git", "commit", "-m", generated_msg],
73
+ stdout=subprocess.PIPE,
74
+ stderr=subprocess.PIPE,
75
+ check=True,
76
+ text=True)
77
+
78
+ return result.stdout
79
+ except subprocess.CalledProcessError:
80
+ return None
81
+
82
+
83
+ def main():
84
+ global USER_API_KEY
85
+
86
+ USER_API_KEY = get_or_set_api_key()
87
+ try:
88
+ if not is_git_repo():
89
+ print("Current directory does not have git initialized",
90
+ file=sys.stdout)
91
+ sys.exit(1)
92
+ diff = git_diff()
93
+ if not diff:
94
+ print("There's nothing in git diff.")
95
+ sys.exit(1)
96
+
97
+ generate_commit_message(diff)
98
+
99
+ except KeyboardInterrupt:
100
+ sys.exit(0)
101
+
102
+
103
+ if __name__ == "__main__":
104
+ main()
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.3
2
+ Name: gmsg
3
+ Version: 0.1.0
4
+ Summary: Generates git commit messages for you using AI.
5
+ License: MIT
6
+ Author: shiyan99s@gmail.com
7
+ Requires-Python: >=3.12
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Requires-Dist: google-genai (>=1.9.0,<2.0.0)
13
+ Description-Content-Type: text/markdown
14
+
15
+
@@ -0,0 +1,7 @@
1
+ gmsg/__init__.py,sha256=9B4ZkbOBiBYYqv3Kh-mdlNn-yDPvkpXOqrm52Ng8CTk,23
2
+ gmsg/api_key.py,sha256=92X--Gmwki2QxBSpMwo5xTl6I1g6GMw8FbCwVx8ojME,454
3
+ gmsg/gmsg.py,sha256=NmQd55li6D5g8vR2hY1gxKXarm0XPSG9jyPO63RvS5g,3016
4
+ gmsg-0.1.0.dist-info/METADATA,sha256=GusjYVbsPUYN-LV7Mn7lg30VvidTTJjFpmUdjfZCS94,458
5
+ gmsg-0.1.0.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
6
+ gmsg-0.1.0.dist-info/entry_points.txt,sha256=LqLzIXT9IVenl_xEptCn4LqWHm34ypTaVMhoW5tFBSg,39
7
+ gmsg-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.0.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ gmsg=gmsg.gmsg:main
3
+