commitmessagegenerator 1.6.0__tar.gz → 2.5.0__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.
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/PKG-INFO +35 -5
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/README.md +34 -4
- commitmessagegenerator-2.5.0/commitmessagegenerator/cli.py +192 -0
- commitmessagegenerator-2.5.0/commitmessagegenerator/configure.py +196 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator/generator.py +8 -5
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator.egg-info/PKG-INFO +35 -5
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/pyproject.toml +1 -1
- commitmessagegenerator-1.6.0/commitmessagegenerator/cli.py +0 -78
- commitmessagegenerator-1.6.0/commitmessagegenerator/configure.py +0 -47
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/LICENSE +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator/__init__.py +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator.egg-info/SOURCES.txt +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator.egg-info/dependency_links.txt +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator.egg-info/entry_points.txt +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator.egg-info/requires.txt +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator.egg-info/top_level.txt +0 -0
- {commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: commitmessagegenerator
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.5.0
|
|
4
4
|
Summary: Generate commit messages with AI (Google Gemini) automatically using `git diff`.
|
|
5
5
|
Author-email: Gabriel Terceiro <gcarolinoterceiro@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -35,10 +35,21 @@ pip install commitmessagegenerator
|
|
|
35
35
|
commitgen -cf
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
You can explicitly choose where configuration is written:
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
```bash
|
|
41
|
+
commitgen -cf --config-scope auto # default behavior
|
|
42
|
+
commitgen -cf --config-scope local # always write .env in current directory
|
|
43
|
+
commitgen -cf --config-scope global # always write ~/.commitgen/.env
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This opens an interactive configuration menu where you can:
|
|
47
|
+
|
|
48
|
+
1. Set or update your Gemini API key
|
|
49
|
+
2. Change the AI model
|
|
50
|
+
3. Configure file staging behavior
|
|
51
|
+
|
|
52
|
+
Each option can be configured independently, and you can exit at any time without saving changes.
|
|
42
53
|
|
|
43
54
|
## Run this and type you API key to the terminal so the package creates the .env file and automatically adds it to the .gitignore
|
|
44
55
|
|
|
@@ -51,8 +62,17 @@ Create a `.env` file in the directory where you will run commitgen (usually the
|
|
|
51
62
|
```
|
|
52
63
|
GEMINI_API_KEY=your-gemini-api-key
|
|
53
64
|
AI_MODEL=gemini-2.0-flash
|
|
65
|
+
AUTO_ADD_ALL=true
|
|
54
66
|
```
|
|
55
67
|
|
|
68
|
+
Config discovery order:
|
|
69
|
+
|
|
70
|
+
1. `.env` in current directory
|
|
71
|
+
2. `.env` in parent directories (useful when running from subfolders)
|
|
72
|
+
3. Global config in `~/.commitgen/.env` (created automatically by `commitgen -cf --config-scope auto` when no local `.env` exists)
|
|
73
|
+
|
|
74
|
+
Environment variables already set in your shell (e.g. `GEMINI_API_KEY`) are also respected.
|
|
75
|
+
|
|
56
76
|
## 🚀 Usage
|
|
57
77
|
|
|
58
78
|
With the terminal, inside any Git repository with pending changes, run:
|
|
@@ -72,7 +92,8 @@ The command will:
|
|
|
72
92
|
- `commitgen` - Generate commit message only
|
|
73
93
|
- `commitgen -c` - Generate and commit with the message
|
|
74
94
|
- `commitgen -cp` - Generate, commit, and push
|
|
75
|
-
- `commitgen -cf` - Configure API key and
|
|
95
|
+
- `commitgen -cf` - Configure API key, model, and file staging behavior
|
|
96
|
+
- `commitgen -cf --config-scope [auto|local|global]` - Choose where `.env` is created/updated
|
|
76
97
|
- `commitgen -s` - Show current configuration status
|
|
77
98
|
|
|
78
99
|
### Available Models
|
|
@@ -86,6 +107,15 @@ When configuring with `-cf`, you can choose from:
|
|
|
86
107
|
5. **gemini-2.5-flash** - Latest version, fast and efficient
|
|
87
108
|
6. **gemini-2.5-pro** - Latest version, highest quality
|
|
88
109
|
|
|
110
|
+
### File Staging Behavior
|
|
111
|
+
|
|
112
|
+
When configuring with `-cf`, you can choose how files are staged:
|
|
113
|
+
|
|
114
|
+
1. **Auto-add all files** (default) - Automatically runs `git add --all` before generating the commit message
|
|
115
|
+
2. **Staged only** - Only reads the diff from files you've already staged with `git add`
|
|
116
|
+
|
|
117
|
+
The "staged only" option gives you more control over which changes are included in the commit message.
|
|
118
|
+
|
|
89
119
|
## 🧩 Requisites
|
|
90
120
|
|
|
91
121
|
- Python 3.8 or higher
|
|
@@ -22,10 +22,21 @@ pip install commitmessagegenerator
|
|
|
22
22
|
commitgen -cf
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
You can explicitly choose where configuration is written:
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
```bash
|
|
28
|
+
commitgen -cf --config-scope auto # default behavior
|
|
29
|
+
commitgen -cf --config-scope local # always write .env in current directory
|
|
30
|
+
commitgen -cf --config-scope global # always write ~/.commitgen/.env
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
This opens an interactive configuration menu where you can:
|
|
34
|
+
|
|
35
|
+
1. Set or update your Gemini API key
|
|
36
|
+
2. Change the AI model
|
|
37
|
+
3. Configure file staging behavior
|
|
38
|
+
|
|
39
|
+
Each option can be configured independently, and you can exit at any time without saving changes.
|
|
29
40
|
|
|
30
41
|
## Run this and type you API key to the terminal so the package creates the .env file and automatically adds it to the .gitignore
|
|
31
42
|
|
|
@@ -38,8 +49,17 @@ Create a `.env` file in the directory where you will run commitgen (usually the
|
|
|
38
49
|
```
|
|
39
50
|
GEMINI_API_KEY=your-gemini-api-key
|
|
40
51
|
AI_MODEL=gemini-2.0-flash
|
|
52
|
+
AUTO_ADD_ALL=true
|
|
41
53
|
```
|
|
42
54
|
|
|
55
|
+
Config discovery order:
|
|
56
|
+
|
|
57
|
+
1. `.env` in current directory
|
|
58
|
+
2. `.env` in parent directories (useful when running from subfolders)
|
|
59
|
+
3. Global config in `~/.commitgen/.env` (created automatically by `commitgen -cf --config-scope auto` when no local `.env` exists)
|
|
60
|
+
|
|
61
|
+
Environment variables already set in your shell (e.g. `GEMINI_API_KEY`) are also respected.
|
|
62
|
+
|
|
43
63
|
## 🚀 Usage
|
|
44
64
|
|
|
45
65
|
With the terminal, inside any Git repository with pending changes, run:
|
|
@@ -59,7 +79,8 @@ The command will:
|
|
|
59
79
|
- `commitgen` - Generate commit message only
|
|
60
80
|
- `commitgen -c` - Generate and commit with the message
|
|
61
81
|
- `commitgen -cp` - Generate, commit, and push
|
|
62
|
-
- `commitgen -cf` - Configure API key and
|
|
82
|
+
- `commitgen -cf` - Configure API key, model, and file staging behavior
|
|
83
|
+
- `commitgen -cf --config-scope [auto|local|global]` - Choose where `.env` is created/updated
|
|
63
84
|
- `commitgen -s` - Show current configuration status
|
|
64
85
|
|
|
65
86
|
### Available Models
|
|
@@ -73,6 +94,15 @@ When configuring with `-cf`, you can choose from:
|
|
|
73
94
|
5. **gemini-2.5-flash** - Latest version, fast and efficient
|
|
74
95
|
6. **gemini-2.5-pro** - Latest version, highest quality
|
|
75
96
|
|
|
97
|
+
### File Staging Behavior
|
|
98
|
+
|
|
99
|
+
When configuring with `-cf`, you can choose how files are staged:
|
|
100
|
+
|
|
101
|
+
1. **Auto-add all files** (default) - Automatically runs `git add --all` before generating the commit message
|
|
102
|
+
2. **Staged only** - Only reads the diff from files you've already staged with `git add`
|
|
103
|
+
|
|
104
|
+
The "staged only" option gives you more control over which changes are included in the commit message.
|
|
105
|
+
|
|
76
106
|
## 🧩 Requisites
|
|
77
107
|
|
|
78
108
|
- Python 3.8 or higher
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import subprocess
|
|
3
|
+
from .generator import gerar_mensagem_commit
|
|
4
|
+
from .configure import (
|
|
5
|
+
api_key,
|
|
6
|
+
get_configured_model,
|
|
7
|
+
get_auto_add_setting,
|
|
8
|
+
update_setting,
|
|
9
|
+
get_api_key_status,
|
|
10
|
+
load_config_env,
|
|
11
|
+
resolve_env_path,
|
|
12
|
+
)
|
|
13
|
+
import sys
|
|
14
|
+
import getpass
|
|
15
|
+
|
|
16
|
+
MODEL_MAP = {
|
|
17
|
+
"1": "gemini-2.0-flash",
|
|
18
|
+
"2": "gemini-1.5-flash",
|
|
19
|
+
"3": "gemini-1.5-pro",
|
|
20
|
+
"4": "gemini-2.0-flash-exp",
|
|
21
|
+
"5": "gemini-2.5-flash",
|
|
22
|
+
"6": "gemini-2.5-pro"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
def configure_menu():
|
|
26
|
+
"""Interactive configuration menu"""
|
|
27
|
+
configure_menu_with_scope("auto")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def configure_menu_with_scope(config_scope):
|
|
31
|
+
"""Interactive configuration menu with target scope."""
|
|
32
|
+
while True:
|
|
33
|
+
# Get current settings
|
|
34
|
+
api_set = get_api_key_status()
|
|
35
|
+
current_model = get_configured_model()
|
|
36
|
+
auto_add = get_auto_add_setting()
|
|
37
|
+
|
|
38
|
+
print("\n" + "="*40)
|
|
39
|
+
print(" CONFIGURATION MENU")
|
|
40
|
+
print("="*40)
|
|
41
|
+
print(f"\n1. API Key [{('✓ Set' if api_set else '✗ Not set')}]")
|
|
42
|
+
print(f"2. Model [{current_model}]")
|
|
43
|
+
print(f"3. File staging [{('Auto-add all' if auto_add else 'Staged only')}]")
|
|
44
|
+
print("\n0. Exit")
|
|
45
|
+
print("="*40)
|
|
46
|
+
|
|
47
|
+
choice = input("\nSelect option (0-3): ").strip()
|
|
48
|
+
|
|
49
|
+
if choice == "0" or choice == "":
|
|
50
|
+
print("\nExiting configuration.\n")
|
|
51
|
+
break
|
|
52
|
+
elif choice == "1":
|
|
53
|
+
configure_api_key(config_scope)
|
|
54
|
+
elif choice == "2":
|
|
55
|
+
configure_model(config_scope)
|
|
56
|
+
elif choice == "3":
|
|
57
|
+
configure_staging(config_scope)
|
|
58
|
+
else:
|
|
59
|
+
print("\nInvalid option. Please try again.")
|
|
60
|
+
|
|
61
|
+
def configure_api_key(config_scope):
|
|
62
|
+
"""Configure the API key"""
|
|
63
|
+
print("\n" + "-"*40)
|
|
64
|
+
print("API KEY CONFIGURATION")
|
|
65
|
+
print("-"*40)
|
|
66
|
+
print("Enter your Gemini API key below.")
|
|
67
|
+
print("Press Enter without typing to cancel.\n")
|
|
68
|
+
|
|
69
|
+
key = getpass.getpass("API Key: ")
|
|
70
|
+
|
|
71
|
+
if not key.strip():
|
|
72
|
+
print("\nNo changes made.")
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
update_setting("GEMINI_API_KEY", key, scope=config_scope)
|
|
76
|
+
print("\n✓ API Key saved successfully!")
|
|
77
|
+
|
|
78
|
+
def configure_model(config_scope):
|
|
79
|
+
"""Configure the AI model"""
|
|
80
|
+
current_model = get_configured_model()
|
|
81
|
+
|
|
82
|
+
print("\n" + "-"*40)
|
|
83
|
+
print("MODEL CONFIGURATION")
|
|
84
|
+
print("-"*40)
|
|
85
|
+
print(f"Current model: {current_model}\n")
|
|
86
|
+
print("Available models:")
|
|
87
|
+
print("1. gemini-2.0-flash (fast and efficient)")
|
|
88
|
+
print("2. gemini-1.5-flash (good balance)")
|
|
89
|
+
print("3. gemini-1.5-pro (highest quality, slower)")
|
|
90
|
+
print("4. gemini-2.0-flash-exp (experimental)")
|
|
91
|
+
print("5. gemini-2.5-flash (latest, fast)")
|
|
92
|
+
print("6. gemini-2.5-pro (latest, highest quality)")
|
|
93
|
+
print("\n0. Cancel")
|
|
94
|
+
|
|
95
|
+
choice = input("\nSelect model (0-6): ").strip()
|
|
96
|
+
|
|
97
|
+
if choice == "0" or choice == "":
|
|
98
|
+
print("\nNo changes made.")
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
if choice in MODEL_MAP:
|
|
102
|
+
selected_model = MODEL_MAP[choice]
|
|
103
|
+
update_setting("AI_MODEL", selected_model, scope=config_scope)
|
|
104
|
+
print(f"\n✓ Model changed to: {selected_model}")
|
|
105
|
+
else:
|
|
106
|
+
print("\nInvalid option. No changes made.")
|
|
107
|
+
|
|
108
|
+
def configure_staging(config_scope):
|
|
109
|
+
"""Configure file staging behavior"""
|
|
110
|
+
auto_add = get_auto_add_setting()
|
|
111
|
+
|
|
112
|
+
print("\n" + "-"*40)
|
|
113
|
+
print("FILE STAGING CONFIGURATION")
|
|
114
|
+
print("-"*40)
|
|
115
|
+
print(f"Current setting: {'Auto-add all files' if auto_add else 'Staged only'}\n")
|
|
116
|
+
print("1. Auto-add all files - Automatically stages all changes")
|
|
117
|
+
print("2. Staged only - Only use already staged files")
|
|
118
|
+
print("\n0. Cancel")
|
|
119
|
+
|
|
120
|
+
choice = input("\nSelect behavior (0-2): ").strip()
|
|
121
|
+
|
|
122
|
+
if choice == "0" or choice == "":
|
|
123
|
+
print("\nNo changes made.")
|
|
124
|
+
return
|
|
125
|
+
|
|
126
|
+
if choice == "1":
|
|
127
|
+
update_setting("AUTO_ADD_ALL", "true", scope=config_scope)
|
|
128
|
+
print("\n✓ Set to: Auto-add all files")
|
|
129
|
+
elif choice == "2":
|
|
130
|
+
update_setting("AUTO_ADD_ALL", "false", scope=config_scope)
|
|
131
|
+
print("\n✓ Set to: Staged only")
|
|
132
|
+
else:
|
|
133
|
+
print("\nInvalid option. No changes made.")
|
|
134
|
+
|
|
135
|
+
def main():
|
|
136
|
+
parser = argparse.ArgumentParser(description="Gerador de mensagens de commit com IA")
|
|
137
|
+
parser.add_argument("-c", "--commit", action="store_true", help="Commits with the generated message")
|
|
138
|
+
parser.add_argument("-cp", "--commitpush", action="store_true", help="Commits and pushes with the generated message")
|
|
139
|
+
parser.add_argument("-cf", "--configure", action="store_true", help="Configures the GEMINI_API_KEY environment variable")
|
|
140
|
+
parser.add_argument("-s", "--status", action="store_true", help="Shows current configuration status")
|
|
141
|
+
parser.add_argument(
|
|
142
|
+
"--config-scope",
|
|
143
|
+
choices=["auto", "local", "global"],
|
|
144
|
+
default="auto",
|
|
145
|
+
help="Where -cf writes configuration (.env): auto, local, or global",
|
|
146
|
+
)
|
|
147
|
+
args = parser.parse_args()
|
|
148
|
+
|
|
149
|
+
if args.status:
|
|
150
|
+
import os
|
|
151
|
+
|
|
152
|
+
env_path = load_config_env()
|
|
153
|
+
key = os.getenv("GEMINI_API_KEY")
|
|
154
|
+
model = get_configured_model()
|
|
155
|
+
auto_add = get_auto_add_setting()
|
|
156
|
+
|
|
157
|
+
print("\nCurrent Configuration:")
|
|
158
|
+
print(f"API Key: {'✓ Set' if key else '✗ Not set'}")
|
|
159
|
+
print(f"Model: {model}")
|
|
160
|
+
print(f"Auto-add all files: {'✓ Yes' if auto_add else '✗ No (staged only)'}")
|
|
161
|
+
if env_path is not None:
|
|
162
|
+
print(f"Config file: {env_path}")
|
|
163
|
+
else:
|
|
164
|
+
print("Config file: not found (.env not discovered)")
|
|
165
|
+
return
|
|
166
|
+
|
|
167
|
+
if not args.configure:
|
|
168
|
+
mensagem = gerar_mensagem_commit()
|
|
169
|
+
|
|
170
|
+
if "No changes detected" in mensagem:
|
|
171
|
+
print(mensagem)
|
|
172
|
+
return
|
|
173
|
+
|
|
174
|
+
print("\nGenerated commit message:\n" + mensagem)
|
|
175
|
+
|
|
176
|
+
if args.commit or args.commitpush:
|
|
177
|
+
print("\nCommitting changes...")
|
|
178
|
+
subprocess.run(["git", "commit", "-m", mensagem])
|
|
179
|
+
|
|
180
|
+
if args.commitpush:
|
|
181
|
+
print("\nPushing changes...")
|
|
182
|
+
subprocess.run(["git", "push"])
|
|
183
|
+
|
|
184
|
+
if args.configure:
|
|
185
|
+
configure_menu_with_scope(args.config_scope)
|
|
186
|
+
configured_path = resolve_env_path()
|
|
187
|
+
if configured_path is not None:
|
|
188
|
+
print(f"\nConfiguration file in use: {configured_path}")
|
|
189
|
+
|
|
190
|
+
if len(sys.argv) == 1:
|
|
191
|
+
print("\nRemoving staged changes (git reset)...")
|
|
192
|
+
subprocess.run(["git", "reset"])
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from dotenv import load_dotenv
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
GLOBAL_ENV_PATH = Path.home() / ".commitgen" / ".env"
|
|
7
|
+
CONFIG_SCOPES = {"auto", "local", "global"}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _iter_local_env_candidates():
|
|
11
|
+
"""Yield .env paths from cwd up to filesystem root."""
|
|
12
|
+
current = Path.cwd().resolve()
|
|
13
|
+
for directory in (current, *current.parents):
|
|
14
|
+
yield directory / ".env"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def resolve_local_env_path():
|
|
18
|
+
"""Resolve an existing local .env from cwd up to filesystem root."""
|
|
19
|
+
for candidate in _iter_local_env_candidates():
|
|
20
|
+
if candidate.exists():
|
|
21
|
+
return candidate
|
|
22
|
+
return None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def resolve_env_path():
|
|
26
|
+
"""Resolve the best available config file path."""
|
|
27
|
+
local_path = resolve_local_env_path()
|
|
28
|
+
if local_path is not None:
|
|
29
|
+
return local_path
|
|
30
|
+
if GLOBAL_ENV_PATH.exists():
|
|
31
|
+
return GLOBAL_ENV_PATH
|
|
32
|
+
return None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def resolve_writable_env_path(scope="auto"):
|
|
36
|
+
"""
|
|
37
|
+
Resolve where settings should be written.
|
|
38
|
+
|
|
39
|
+
scope:
|
|
40
|
+
- auto: existing local .env, then existing global .env, else new global .env
|
|
41
|
+
- local: .env in current working directory
|
|
42
|
+
- global: ~/.commitgen/.env
|
|
43
|
+
"""
|
|
44
|
+
if scope not in CONFIG_SCOPES:
|
|
45
|
+
raise ValueError(f"Invalid config scope: {scope}")
|
|
46
|
+
|
|
47
|
+
if scope == "local":
|
|
48
|
+
return Path.cwd().resolve() / ".env"
|
|
49
|
+
|
|
50
|
+
if scope == "global":
|
|
51
|
+
return GLOBAL_ENV_PATH
|
|
52
|
+
|
|
53
|
+
existing_path = resolve_env_path()
|
|
54
|
+
if existing_path is not None:
|
|
55
|
+
return existing_path
|
|
56
|
+
return GLOBAL_ENV_PATH
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def load_config_env():
|
|
60
|
+
"""
|
|
61
|
+
Load configuration from the resolved .env path (if found).
|
|
62
|
+
Returns the loaded path or None.
|
|
63
|
+
"""
|
|
64
|
+
env_path = resolve_env_path()
|
|
65
|
+
if env_path is not None:
|
|
66
|
+
load_dotenv(dotenv_path=env_path, override=False)
|
|
67
|
+
return env_path
|
|
68
|
+
load_dotenv(override=False)
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
def update_setting(setting_name, value, scope="auto"):
|
|
72
|
+
"""Update a single setting in the .env file"""
|
|
73
|
+
env_path = resolve_writable_env_path(scope=scope)
|
|
74
|
+
env_path.parent.mkdir(parents=True, exist_ok=True)
|
|
75
|
+
|
|
76
|
+
if env_path.exists():
|
|
77
|
+
with open(env_path, "r", encoding="utf-8") as f:
|
|
78
|
+
lines = f.readlines()
|
|
79
|
+
|
|
80
|
+
found = False
|
|
81
|
+
new_lines = []
|
|
82
|
+
for line in lines:
|
|
83
|
+
key_name = line.split("=")[0] if "=" in line else None
|
|
84
|
+
if key_name == setting_name:
|
|
85
|
+
new_lines.append(f"{setting_name}={value}\n")
|
|
86
|
+
found = True
|
|
87
|
+
else:
|
|
88
|
+
new_lines.append(line)
|
|
89
|
+
|
|
90
|
+
if not found:
|
|
91
|
+
new_lines.append(f"{setting_name}={value}\n")
|
|
92
|
+
|
|
93
|
+
with open(env_path, "w", encoding="utf-8") as f:
|
|
94
|
+
f.writelines(new_lines)
|
|
95
|
+
else:
|
|
96
|
+
with open(env_path, "w", encoding="utf-8") as f:
|
|
97
|
+
f.write(f"{setting_name}={value}\n")
|
|
98
|
+
|
|
99
|
+
_ensure_gitignore(env_path)
|
|
100
|
+
|
|
101
|
+
def _ensure_gitignore(env_path):
|
|
102
|
+
"""Ensure .env is in .gitignore for local (non-global) config files."""
|
|
103
|
+
if env_path == GLOBAL_ENV_PATH:
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
gitignore_path = env_path.parent / ".gitignore"
|
|
107
|
+
if gitignore_path.exists():
|
|
108
|
+
with open(gitignore_path, "r+", encoding="utf-8") as outfile:
|
|
109
|
+
lines = outfile.readlines()
|
|
110
|
+
env_in_gitignore = next((line for line in lines if line.strip() == ".env"), None)
|
|
111
|
+
if not env_in_gitignore:
|
|
112
|
+
outfile.write("\n.env")
|
|
113
|
+
|
|
114
|
+
def api_key(key, model="gemini-2.0-flash", auto_add_all=True, scope="auto"):
|
|
115
|
+
"""Set all configuration at once (legacy function)"""
|
|
116
|
+
config = {
|
|
117
|
+
"GEMINI_API_KEY": key,
|
|
118
|
+
"AI_MODEL": model,
|
|
119
|
+
"AUTO_ADD_ALL": str(auto_add_all).lower()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
env_path = resolve_writable_env_path(scope=scope)
|
|
123
|
+
env_path.parent.mkdir(parents=True, exist_ok=True)
|
|
124
|
+
|
|
125
|
+
if env_path.exists():
|
|
126
|
+
with open(env_path, "r", encoding="utf-8") as f:
|
|
127
|
+
lines = f.readlines()
|
|
128
|
+
|
|
129
|
+
existing_keys = set()
|
|
130
|
+
new_lines = []
|
|
131
|
+
for line in lines:
|
|
132
|
+
key_name = line.split("=")[0] if "=" in line else None
|
|
133
|
+
if key_name in config:
|
|
134
|
+
new_lines.append(f"{key_name}={config[key_name]}\n")
|
|
135
|
+
existing_keys.add(key_name)
|
|
136
|
+
else:
|
|
137
|
+
new_lines.append(line)
|
|
138
|
+
|
|
139
|
+
for key_name, value in config.items():
|
|
140
|
+
if key_name not in existing_keys:
|
|
141
|
+
new_lines.append(f"{key_name}={value}\n")
|
|
142
|
+
|
|
143
|
+
with open(env_path, "w", encoding="utf-8") as f:
|
|
144
|
+
f.writelines(new_lines)
|
|
145
|
+
else:
|
|
146
|
+
with open(env_path, "w", encoding="utf-8") as f:
|
|
147
|
+
for key_name, value in config.items():
|
|
148
|
+
f.write(f"{key_name}={value}\n")
|
|
149
|
+
|
|
150
|
+
_ensure_gitignore(env_path)
|
|
151
|
+
|
|
152
|
+
def get_api_key_status():
|
|
153
|
+
"""Check if API key is set"""
|
|
154
|
+
env_key = os.getenv("GEMINI_API_KEY")
|
|
155
|
+
if env_key:
|
|
156
|
+
return True
|
|
157
|
+
|
|
158
|
+
env_path = resolve_env_path()
|
|
159
|
+
if env_path is not None:
|
|
160
|
+
with open(env_path, "r", encoding="utf-8") as f:
|
|
161
|
+
lines = f.readlines()
|
|
162
|
+
api_line = next((line for line in lines if line.startswith("GEMINI_API_KEY=")), None)
|
|
163
|
+
if api_line:
|
|
164
|
+
value = api_line.split("=", 1)[1].strip()
|
|
165
|
+
return bool(value)
|
|
166
|
+
return False
|
|
167
|
+
|
|
168
|
+
def get_configured_model():
|
|
169
|
+
"""Get the currently configured AI model from .env file"""
|
|
170
|
+
env_model = os.getenv("AI_MODEL")
|
|
171
|
+
if env_model:
|
|
172
|
+
return env_model
|
|
173
|
+
|
|
174
|
+
env_path = resolve_env_path()
|
|
175
|
+
if env_path is not None:
|
|
176
|
+
with open(env_path, "r", encoding="utf-8") as outfile:
|
|
177
|
+
lines = outfile.readlines()
|
|
178
|
+
model_line = next((line for line in lines if line.startswith("AI_MODEL=")), None)
|
|
179
|
+
if model_line:
|
|
180
|
+
return model_line.split("=", 1)[1].strip()
|
|
181
|
+
return "gemini-2.0-flash" # Default fallback
|
|
182
|
+
|
|
183
|
+
def get_auto_add_setting():
|
|
184
|
+
"""Get the auto-add all files setting from .env file"""
|
|
185
|
+
env_auto_add = os.getenv("AUTO_ADD_ALL")
|
|
186
|
+
if env_auto_add is not None:
|
|
187
|
+
return env_auto_add.strip().lower() == "true"
|
|
188
|
+
|
|
189
|
+
env_path = resolve_env_path()
|
|
190
|
+
if env_path is not None:
|
|
191
|
+
with open(env_path, "r", encoding="utf-8") as outfile:
|
|
192
|
+
lines = outfile.readlines()
|
|
193
|
+
auto_add_line = next((line for line in lines if line.startswith("AUTO_ADD_ALL=")), None)
|
|
194
|
+
if auto_add_line:
|
|
195
|
+
return auto_add_line.split("=", 1)[1].strip().lower() == "true"
|
|
196
|
+
return True # Default: auto-add all files (current behavior)
|
{commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator/generator.py
RENAMED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import os
|
|
2
|
-
from dotenv import load_dotenv
|
|
3
2
|
from google import genai
|
|
4
3
|
from git import Repo
|
|
5
|
-
from .configure import get_configured_model
|
|
4
|
+
from .configure import get_configured_model, get_auto_add_setting, load_config_env
|
|
6
5
|
|
|
7
6
|
def gerar_mensagem_commit():
|
|
8
|
-
|
|
7
|
+
load_config_env()
|
|
9
8
|
key = os.getenv("GEMINI_API_KEY")
|
|
10
9
|
if not key:
|
|
11
10
|
raise RuntimeError("The GEMINI_API_KEY environment variable is not set.")
|
|
@@ -18,8 +17,12 @@ def gerar_mensagem_commit():
|
|
|
18
17
|
|
|
19
18
|
repo = Repo(os.getcwd())
|
|
20
19
|
|
|
21
|
-
#
|
|
22
|
-
|
|
20
|
+
# Check if auto-add is enabled
|
|
21
|
+
auto_add = get_auto_add_setting()
|
|
22
|
+
if auto_add:
|
|
23
|
+
# Automatically stage all changes
|
|
24
|
+
repo.git.add(all=True)
|
|
25
|
+
|
|
23
26
|
diff = repo.git.diff("--cached")
|
|
24
27
|
|
|
25
28
|
if not diff.strip():
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: commitmessagegenerator
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.5.0
|
|
4
4
|
Summary: Generate commit messages with AI (Google Gemini) automatically using `git diff`.
|
|
5
5
|
Author-email: Gabriel Terceiro <gcarolinoterceiro@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -35,10 +35,21 @@ pip install commitmessagegenerator
|
|
|
35
35
|
commitgen -cf
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
You can explicitly choose where configuration is written:
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
```bash
|
|
41
|
+
commitgen -cf --config-scope auto # default behavior
|
|
42
|
+
commitgen -cf --config-scope local # always write .env in current directory
|
|
43
|
+
commitgen -cf --config-scope global # always write ~/.commitgen/.env
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This opens an interactive configuration menu where you can:
|
|
47
|
+
|
|
48
|
+
1. Set or update your Gemini API key
|
|
49
|
+
2. Change the AI model
|
|
50
|
+
3. Configure file staging behavior
|
|
51
|
+
|
|
52
|
+
Each option can be configured independently, and you can exit at any time without saving changes.
|
|
42
53
|
|
|
43
54
|
## Run this and type you API key to the terminal so the package creates the .env file and automatically adds it to the .gitignore
|
|
44
55
|
|
|
@@ -51,8 +62,17 @@ Create a `.env` file in the directory where you will run commitgen (usually the
|
|
|
51
62
|
```
|
|
52
63
|
GEMINI_API_KEY=your-gemini-api-key
|
|
53
64
|
AI_MODEL=gemini-2.0-flash
|
|
65
|
+
AUTO_ADD_ALL=true
|
|
54
66
|
```
|
|
55
67
|
|
|
68
|
+
Config discovery order:
|
|
69
|
+
|
|
70
|
+
1. `.env` in current directory
|
|
71
|
+
2. `.env` in parent directories (useful when running from subfolders)
|
|
72
|
+
3. Global config in `~/.commitgen/.env` (created automatically by `commitgen -cf --config-scope auto` when no local `.env` exists)
|
|
73
|
+
|
|
74
|
+
Environment variables already set in your shell (e.g. `GEMINI_API_KEY`) are also respected.
|
|
75
|
+
|
|
56
76
|
## 🚀 Usage
|
|
57
77
|
|
|
58
78
|
With the terminal, inside any Git repository with pending changes, run:
|
|
@@ -72,7 +92,8 @@ The command will:
|
|
|
72
92
|
- `commitgen` - Generate commit message only
|
|
73
93
|
- `commitgen -c` - Generate and commit with the message
|
|
74
94
|
- `commitgen -cp` - Generate, commit, and push
|
|
75
|
-
- `commitgen -cf` - Configure API key and
|
|
95
|
+
- `commitgen -cf` - Configure API key, model, and file staging behavior
|
|
96
|
+
- `commitgen -cf --config-scope [auto|local|global]` - Choose where `.env` is created/updated
|
|
76
97
|
- `commitgen -s` - Show current configuration status
|
|
77
98
|
|
|
78
99
|
### Available Models
|
|
@@ -86,6 +107,15 @@ When configuring with `-cf`, you can choose from:
|
|
|
86
107
|
5. **gemini-2.5-flash** - Latest version, fast and efficient
|
|
87
108
|
6. **gemini-2.5-pro** - Latest version, highest quality
|
|
88
109
|
|
|
110
|
+
### File Staging Behavior
|
|
111
|
+
|
|
112
|
+
When configuring with `-cf`, you can choose how files are staged:
|
|
113
|
+
|
|
114
|
+
1. **Auto-add all files** (default) - Automatically runs `git add --all` before generating the commit message
|
|
115
|
+
2. **Staged only** - Only reads the diff from files you've already staged with `git add`
|
|
116
|
+
|
|
117
|
+
The "staged only" option gives you more control over which changes are included in the commit message.
|
|
118
|
+
|
|
89
119
|
## 🧩 Requisites
|
|
90
120
|
|
|
91
121
|
- Python 3.8 or higher
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "commitmessagegenerator"
|
|
7
|
-
version = "
|
|
7
|
+
version = "2.5.0"
|
|
8
8
|
description = "Generate commit messages with AI (Google Gemini) automatically using `git diff`."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = {text = "MIT"}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import argparse
|
|
2
|
-
import subprocess
|
|
3
|
-
from .generator import gerar_mensagem_commit
|
|
4
|
-
from .configure import api_key, get_configured_model
|
|
5
|
-
import sys
|
|
6
|
-
import getpass
|
|
7
|
-
|
|
8
|
-
def main():
|
|
9
|
-
parser = argparse.ArgumentParser(description="Gerador de mensagens de commit com IA")
|
|
10
|
-
parser.add_argument("-c", "--commit", action="store_true", help="Commits with the generated message")
|
|
11
|
-
parser.add_argument("-cp", "--commitpush", action="store_true", help="Commits and pushes with the generated message")
|
|
12
|
-
parser.add_argument("-cf", "--configure", action="store_true", help="Configures the GEMINI_API_KEY environment variable")
|
|
13
|
-
parser.add_argument("-s", "--status", action="store_true", help="Shows current configuration status")
|
|
14
|
-
args = parser.parse_args()
|
|
15
|
-
|
|
16
|
-
if args.status:
|
|
17
|
-
from .configure import get_configured_model
|
|
18
|
-
import os
|
|
19
|
-
from dotenv import load_dotenv
|
|
20
|
-
|
|
21
|
-
load_dotenv()
|
|
22
|
-
key = os.getenv("GEMINI_API_KEY")
|
|
23
|
-
model = get_configured_model()
|
|
24
|
-
|
|
25
|
-
print("\nCurrent Configuration:")
|
|
26
|
-
print(f"API Key: {'✓ Set' if key else '✗ Not set'}")
|
|
27
|
-
print(f"Model: {model}")
|
|
28
|
-
return
|
|
29
|
-
|
|
30
|
-
if not args.configure:
|
|
31
|
-
mensagem = gerar_mensagem_commit()
|
|
32
|
-
|
|
33
|
-
if "No changes detected" in mensagem:
|
|
34
|
-
print(mensagem)
|
|
35
|
-
return
|
|
36
|
-
|
|
37
|
-
print("\nGenerated commit message:\n" + mensagem)
|
|
38
|
-
|
|
39
|
-
if args.commit or args.commitpush:
|
|
40
|
-
print("\nCommitting changes...")
|
|
41
|
-
subprocess.run(["git", "commit", "-m", mensagem])
|
|
42
|
-
|
|
43
|
-
if args.commitpush:
|
|
44
|
-
print("\nPushing changes...")
|
|
45
|
-
subprocess.run(["git", "push"])
|
|
46
|
-
|
|
47
|
-
if args.configure:
|
|
48
|
-
print("\nPlease input your API KEY\nThis is directly set in the .env file")
|
|
49
|
-
key = getpass.getpass()
|
|
50
|
-
|
|
51
|
-
# Model selection
|
|
52
|
-
print("\nAvailable models:")
|
|
53
|
-
print("1. gemini-2.0-flash (default, fast and efficient)")
|
|
54
|
-
print("2. gemini-1.5-flash (good balance of speed and quality)")
|
|
55
|
-
print("3. gemini-1.5-pro (highest quality, slower)")
|
|
56
|
-
print("4. gemini-2.0-flash-exp (experimental)")
|
|
57
|
-
print("5. gemini-2.5-flash (latest, fast and efficient)")
|
|
58
|
-
print("6. gemini-2.5-pro (latest, highest quality)")
|
|
59
|
-
|
|
60
|
-
model_choice = input("\nSelect model (1-6) or press Enter for default (1): ").strip()
|
|
61
|
-
|
|
62
|
-
model_map = {
|
|
63
|
-
"1": "gemini-2.0-flash",
|
|
64
|
-
"2": "gemini-1.5-flash",
|
|
65
|
-
"3": "gemini-1.5-pro",
|
|
66
|
-
"4": "gemini-2.0-flash-exp",
|
|
67
|
-
"5": "gemini-2.5-flash",
|
|
68
|
-
"6": "gemini-2.5-pro"
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
selected_model = model_map.get(model_choice, "gemini-2.0-flash")
|
|
72
|
-
|
|
73
|
-
api_key(key, selected_model)
|
|
74
|
-
print(f"\nAPI KEY and model ({selected_model}) saved in .env file\n")
|
|
75
|
-
|
|
76
|
-
if len(sys.argv) == 1:
|
|
77
|
-
print("\nRemoving staged changes (git reset)...")
|
|
78
|
-
subprocess.run(["git", "reset"])
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
|
|
3
|
-
def api_key(key, model="gemini-2.0-flash"):
|
|
4
|
-
if os.path.exists(".env"):
|
|
5
|
-
with open(".env", "r+") as outfile:
|
|
6
|
-
lines = outfile.readlines()
|
|
7
|
-
gemini_api_key = next((line for line in lines if line.startswith("GEMINI_API_KEY=")), None)
|
|
8
|
-
model_line = next((line for line in lines if line.startswith("AI_MODEL=")), None)
|
|
9
|
-
|
|
10
|
-
outfile.seek(0)
|
|
11
|
-
outfile.truncate()
|
|
12
|
-
|
|
13
|
-
# Update or add GEMINI_API_KEY
|
|
14
|
-
if gemini_api_key:
|
|
15
|
-
outfile.writelines([line if not line.startswith("GEMINI_API_KEY=") else f"GEMINI_API_KEY={key}\n" for line in lines])
|
|
16
|
-
else:
|
|
17
|
-
outfile.writelines(lines)
|
|
18
|
-
outfile.write(f"GEMINI_API_KEY={key}\n")
|
|
19
|
-
|
|
20
|
-
# Update or add AI_MODEL
|
|
21
|
-
if model_line:
|
|
22
|
-
outfile.seek(0)
|
|
23
|
-
outfile.truncate()
|
|
24
|
-
outfile.writelines([line if not line.startswith("AI_MODEL=") else f"AI_MODEL={model}\n" for line in outfile.readlines()])
|
|
25
|
-
else:
|
|
26
|
-
outfile.write(f"AI_MODEL={model}\n")
|
|
27
|
-
else:
|
|
28
|
-
with open(".env", "w") as outfile:
|
|
29
|
-
outfile.write(f"GEMINI_API_KEY={key}\n")
|
|
30
|
-
outfile.write(f"AI_MODEL={model}\n")
|
|
31
|
-
|
|
32
|
-
if os.path.exists(".gitignore"):
|
|
33
|
-
with open(".gitignore", "r+") as outfile:
|
|
34
|
-
lines = outfile.readlines()
|
|
35
|
-
env_in_gitignore = next((line for line in lines if line.strip() == ".env"), None)
|
|
36
|
-
if not env_in_gitignore:
|
|
37
|
-
outfile.write("\n.env")
|
|
38
|
-
|
|
39
|
-
def get_configured_model():
|
|
40
|
-
"""Get the currently configured AI model from .env file"""
|
|
41
|
-
if os.path.exists(".env"):
|
|
42
|
-
with open(".env", "r") as outfile:
|
|
43
|
-
lines = outfile.readlines()
|
|
44
|
-
model_line = next((line for line in lines if line.startswith("AI_MODEL=")), None)
|
|
45
|
-
if model_line:
|
|
46
|
-
return model_line.split("=", 1)[1].strip()
|
|
47
|
-
return "gemini-2.0-flash" # Default fallback
|
|
File without changes
|
{commitmessagegenerator-1.6.0 → commitmessagegenerator-2.5.0}/commitmessagegenerator/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|