gmsg 0.1.1__tar.gz → 0.1.3__tar.gz

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-0.1.3/LICENSE ADDED
@@ -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.3
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)
@@ -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)
@@ -0,0 +1,138 @@
1
+ import os
2
+ import sys
3
+ import tempfile
4
+ import subprocess
5
+ from google import genai
6
+ from pathlib import Path
7
+ from .api_key import get_or_set_api_key
8
+
9
+ GREEN = "\033[92m"
10
+ RED = "\033[91m"
11
+ RESET = "\033[0m"
12
+
13
+
14
+ def is_git_repo() -> bool:
15
+ path = Path(Path.cwd())
16
+ for parent in [path] + list(path.parents):
17
+ if (parent / ".git").exists():
18
+ return True
19
+ return False
20
+
21
+
22
+ def git_diff() -> str | None:
23
+ try:
24
+ result = subprocess.run(["git", "diff", "--cached"],
25
+ stdout=subprocess.PIPE,
26
+ stderr=subprocess.PIPE,
27
+ check=True,
28
+ text=True)
29
+
30
+ if not result.stdout:
31
+ return None
32
+ return result.stdout
33
+ except subprocess.CalledProcessError:
34
+ sys.exit(1)
35
+
36
+
37
+ def make_query(diff: str) -> str:
38
+ prompt = f"Generate a one liner git commit message for these changes, Below are the changes:\n {diff}"
39
+ return prompt
40
+
41
+
42
+ def trigger_query(query: str, api_key: str) -> str:
43
+ try:
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
48
+ except genai.errors.ClientError as error:
49
+ if error.code == 400:
50
+ printt(dict(error="Gemini API key not valid, check ~/.gmsg"),
51
+ is_success=False)
52
+ else:
53
+ printt(error, is_success=False)
54
+ sys.exit(1)
55
+
56
+
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)
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'.")
80
+
81
+
82
+ def commit_message_to_git(generated_msg: str) -> str | None:
83
+ try:
84
+ result = subprocess.run(["git", "commit", "-m", generated_msg],
85
+ stdout=subprocess.PIPE,
86
+ stderr=subprocess.PIPE,
87
+ check=True,
88
+ text=True)
89
+
90
+ return result.stdout
91
+ except subprocess.CalledProcessError:
92
+ return None
93
+
94
+
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])
104
+
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():
120
+ try:
121
+ if not is_git_repo():
122
+ printt("Not a git repository.", is_success=False)
123
+ sys.exit(1)
124
+
125
+ diff = git_diff()
126
+ if not diff:
127
+ printt("No staged changes found.", is_success=False)
128
+ sys.exit(1)
129
+
130
+ user_api_key = get_or_set_api_key()
131
+ cycle_through_messages(diff, user_api_key)
132
+
133
+ except KeyboardInterrupt:
134
+ sys.exit(0)
135
+
136
+
137
+ if __name__ == "__main__":
138
+ main()
@@ -1,13 +1,13 @@
1
1
  [project]
2
2
  name = "gmsg"
3
- version = "0.1.1"
3
+ version = "0.1.3"
4
4
  description = "Generates git commit messages for you using AI."
5
5
  authors = [
6
6
  {name = "shiyan99s@gmail.com"}
7
7
  ]
8
8
  license = {text = "MIT"}
9
9
  readme = "README.md"
10
- requires-python = ">=3.12"
10
+ requires-python = ">=3.9"
11
11
  dependencies = [
12
12
  "google-genai (>=1.9.0,<2.0.0)"
13
13
  ]
gmsg-0.1.1/gmsg/gmsg.py DELETED
@@ -1,111 +0,0 @@
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
- GREEN = "\033[92m"
8
- RED = "\033[91m"
9
- RESET = "\033[0m"
10
-
11
-
12
- def is_git_repo() -> bool:
13
- path = Path(Path.cwd())
14
- for parent in [path] + list(path.parents):
15
- if (parent / ".git").exists():
16
- return True
17
- return False
18
-
19
-
20
- def git_diff() -> str | None:
21
- try:
22
- result = subprocess.run(["git", "diff", "--cached"],
23
- stdout=subprocess.PIPE,
24
- stderr=subprocess.PIPE,
25
- check=True,
26
- text=True)
27
-
28
- if not result.stdout:
29
- return None
30
- return result.stdout
31
- except subprocess.CalledProcessError:
32
- sys.exit(1)
33
-
34
-
35
- def make_query(diff) -> str:
36
- prompt = f"Generate a one liner git commit message for these changes, Below are the changes:\n {diff}"
37
- return prompt
38
-
39
-
40
- def generate_commit_message(diff: str) -> str:
41
- 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
53
- except genai.errors.ClientError as error:
54
- if error.code == 400:
55
- print(
56
- dict(
57
- error=f"{RED}Gemini API key not valid, check ~/.gmsg{RESET}"
58
- ))
59
- else:
60
- print(error)
61
- sys.exit(1)
62
-
63
-
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
75
-
76
-
77
- def commit_message_to_git(generated_msg: str) -> str | None:
78
- try:
79
- result = subprocess.run(["git", "commit", "-m", generated_msg],
80
- stdout=subprocess.PIPE,
81
- stderr=subprocess.PIPE,
82
- check=True,
83
- text=True)
84
-
85
- return result.stdout
86
- except subprocess.CalledProcessError:
87
- return None
88
-
89
-
90
- def main():
91
- global USER_API_KEY
92
-
93
- USER_API_KEY = get_or_set_api_key()
94
- try:
95
- if not is_git_repo():
96
- print("Current directory does not have git initialized",
97
- file=sys.stdout)
98
- sys.exit(1)
99
- diff = git_diff()
100
- if not diff:
101
- print("There's nothing in git diff.")
102
- sys.exit(1)
103
-
104
- generate_commit_message(diff)
105
-
106
- except KeyboardInterrupt:
107
- sys.exit(0)
108
-
109
-
110
- if __name__ == "__main__":
111
- main()
File without changes
File without changes