gmsg 0.1.1__py3-none-any.whl → 0.1.4__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/api_key.py CHANGED
@@ -8,6 +8,9 @@ def get_or_set_api_key():
8
8
  try:
9
9
  rc = os.path.join(Path.home(), ".gmsgrc")
10
10
  if not os.path.isfile(rc):
11
+ print(
12
+ "Get your key from https://ai.google.dev/gemini-api/docs/api-key"
13
+ )
11
14
  key_inp = input("Enter your Gemini API KEY: ").strip()
12
15
  with open(rc, 'w') as f:
13
16
  f.write(key_inp)
gmsg/gmsg.py CHANGED
@@ -1,4 +1,6 @@
1
+ import os
1
2
  import sys
3
+ import tempfile
2
4
  import subprocess
3
5
  from google import genai
4
6
  from pathlib import Path
@@ -32,46 +34,49 @@ def git_diff() -> str | None:
32
34
  sys.exit(1)
33
35
 
34
36
 
35
- def make_query(diff) -> str:
37
+ def make_query(diff: str) -> str:
36
38
  prompt = f"Generate a one liner git commit message for these changes, Below are the changes:\n {diff}"
37
39
  return prompt
38
40
 
39
41
 
40
- def generate_commit_message(diff: str) -> str:
42
+ def trigger_query(query: str, api_key: str) -> str:
41
43
  try:
42
- query = make_query(diff)
43
- while True:
44
- client = genai.Client(api_key=USER_API_KEY)
45
- msg = client.models.generate_content(model="gemini-2.0-flash",
46
- contents=query)
47
-
48
- print(f"{GREEN}{msg.text}{RESET}")
49
- # generated_msg = "feat: Add poetry.lock and pyproject.toml files."
50
- # print(generated_msg)
51
- if continue_with_this_prompt(msg.text):
52
- break
44
+ client = genai.Client(api_key=api_key)
45
+ msg = client.models.generate_content(model="gemini-2.0-flash",
46
+ contents=query)
47
+ return msg.text
53
48
  except genai.errors.ClientError as error:
54
49
  if error.code == 400:
55
- print(
56
- dict(
57
- error=f"{RED}Gemini API key not valid, check ~/.gmsg{RESET}"
58
- ))
50
+ printt(dict(error="Gemini API key not valid, check ~/.gmsg"),
51
+ is_success=False)
59
52
  else:
60
- print(error)
53
+ printt(error, is_success=False)
61
54
  sys.exit(1)
62
55
 
63
56
 
64
- def continue_with_this_prompt(commit_msg: str) -> bool:
65
- action = input("Do you want to continue with this message [Y/n]: ")
66
- if action in ("", "y", "Y"):
67
- print(f"Running: `git commit -m {commit_msg}`")
68
- print(
69
- f"{GREEN}Message commited to git, to confirm you can run git commit --amend.{RESET}"
70
- )
71
- commit_message_to_git(commit_msg)
72
- return True
73
- elif action in ("n", "N"):
74
- return False
57
+ def cycle_through_messages(diff: str, api_key: str) -> bool:
58
+ query = make_query(diff)
59
+ msg = trigger_query(query, api_key)
60
+
61
+ while True:
62
+ printt(msg)
63
+ action = input(
64
+ "Do you want to continue with this message? [Y = yes / e = edit / n = no]: "
65
+ ).strip().lower()
66
+ if action in ("", "y", "Y"):
67
+ print(
68
+ f"Running: `git commit -m {msg}`\nMessage committed to git. You can run `git commit --amend` to modify it."
69
+ )
70
+
71
+ commit_message_to_git(msg)
72
+ break
73
+ elif action in ("n", "N"):
74
+ msg = trigger_query(query, api_key)
75
+ elif action in ("e", "E"):
76
+ msg = edit_message_in_editor(msg)
77
+
78
+ else:
79
+ print("Invalid input. Please enter 'Y', 'e', or 'n'.")
75
80
 
76
81
 
77
82
  def commit_message_to_git(generated_msg: str) -> str | None:
@@ -87,21 +92,43 @@ def commit_message_to_git(generated_msg: str) -> str | None:
87
92
  return None
88
93
 
89
94
 
90
- def main():
91
- global USER_API_KEY
95
+ def edit_message_in_editor(initial_msg: str) -> str:
96
+ editor = os.environ.get("EDITOR", "vim")
97
+ with tempfile.NamedTemporaryFile(suffix=".tmp", mode='w+',
98
+ delete=False) as tf:
99
+ tf.write(initial_msg)
100
+ tf.flush()
101
+ temp_path = tf.name
102
+
103
+ subprocess.run([editor, temp_path])
92
104
 
93
- USER_API_KEY = get_or_set_api_key()
105
+ with open(temp_path, 'r') as tf:
106
+ edited_msg = tf.read().strip()
107
+
108
+ os.unlink(temp_path)
109
+ return edited_msg
110
+
111
+
112
+ def printt(text: str, is_success: bool = True):
113
+ if not is_success:
114
+ print(RED + text + RESET, file=sys.stderr)
115
+ return None
116
+ print(GREEN + text + RESET)
117
+
118
+
119
+ def main():
94
120
  try:
95
121
  if not is_git_repo():
96
- print("Current directory does not have git initialized",
97
- file=sys.stdout)
122
+ printt("Not a git repository.", is_success=False)
98
123
  sys.exit(1)
124
+
99
125
  diff = git_diff()
100
126
  if not diff:
101
- print("There's nothing in git diff.")
127
+ printt("No staged changes found.", is_success=False)
102
128
  sys.exit(1)
103
129
 
104
- generate_commit_message(diff)
130
+ user_api_key = get_or_set_api_key()
131
+ cycle_through_messages(diff, user_api_key)
105
132
 
106
133
  except KeyboardInterrupt:
107
134
  sys.exit(0)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shiyan Shirani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,12 +1,15 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: gmsg
3
- Version: 0.1.1
3
+ Version: 0.1.4
4
4
  Summary: Generates git commit messages for you using AI.
5
5
  License: MIT
6
6
  Author: shiyan99s@gmail.com
7
- Requires-Python: >=3.12
7
+ Requires-Python: >=3.9
8
8
  Classifier: License :: OSI Approved :: MIT License
9
9
  Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
10
13
  Classifier: Programming Language :: Python :: 3.12
11
14
  Classifier: Programming Language :: Python :: 3.13
12
15
  Requires-Dist: google-genai (>=1.9.0,<2.0.0)
@@ -0,0 +1,8 @@
1
+ gmsg/__init__.py,sha256=9B4ZkbOBiBYYqv3Kh-mdlNn-yDPvkpXOqrm52Ng8CTk,23
2
+ gmsg/api_key.py,sha256=73RAEjrcN6f1tc3cqaJ5888COTm4gnjQ4G7VRB3-CnQ,569
3
+ gmsg/gmsg.py,sha256=bPHA3Unsoe6XqpsY8rc5qnZ-rQGNcEBiJSrtgBJvXGk,3871
4
+ gmsg-0.1.4.dist-info/LICENSE,sha256=4gnFnu1ft89RQUn-ohdbqKLhpQ6-wHtq2eAOs1p6X6M,1071
5
+ gmsg-0.1.4.dist-info/METADATA,sha256=5hAkqmK7DnukSCFt5M7y7m8b9F49QL5gjHbG1s09WRw,609
6
+ gmsg-0.1.4.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
7
+ gmsg-0.1.4.dist-info/entry_points.txt,sha256=LqLzIXT9IVenl_xEptCn4LqWHm34ypTaVMhoW5tFBSg,39
8
+ gmsg-0.1.4.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- gmsg/__init__.py,sha256=9B4ZkbOBiBYYqv3Kh-mdlNn-yDPvkpXOqrm52Ng8CTk,23
2
- gmsg/api_key.py,sha256=92X--Gmwki2QxBSpMwo5xTl6I1g6GMw8FbCwVx8ojME,454
3
- gmsg/gmsg.py,sha256=hflPSahpCDgaCsFE33udZSkZ4hgB43UkCZZ9tcigTd4,3172
4
- gmsg-0.1.1.dist-info/METADATA,sha256=PWwm7v5MgB32x9ZCOXXw2ko39yK7Xk2uhYQyI4PK65w,458
5
- gmsg-0.1.1.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
6
- gmsg-0.1.1.dist-info/entry_points.txt,sha256=LqLzIXT9IVenl_xEptCn4LqWHm34ypTaVMhoW5tFBSg,39
7
- gmsg-0.1.1.dist-info/RECORD,,
File without changes