After tirelessly working on a project feature, adjusting functions here and there, you might find yourself with numerous modified files, facing a final task: Commiting your changes in a way that maintains clarity for future reference. Diligently scrolling through the diff, attempting to pinpoint and acticulate every modification can be a daunting task.
This is where gc-smart
steps in.
The script generates an AI enhanced commit message based on the diffs of your staged changes, reducing the manual effort involved in crafting meaningful commit descriptions. By default, after running the script within a repository, a preview of the AI-generated commit message is displayed. You are then presented with the options:
- To continue with the current commit message.
- To regenerate a new commit message.
- To regenerate a new commit message by adding further instruction.
- To view the staged changes.
- To abort the commit process altogether.
If you choose to continue, the generated message is used as a template for the
git commit
command, allowing you to review and further customize the message
if needed before finalizing it.
For quick commits, you can use the -q
or --quick
options to skip the
preview and commit directly with the AI-generated message. To see all the
possible options, run gc-smart --help
.
- Python 3.7 or higher
- An OpenAI API key, see: Where do I find my Secret API Key?
- Git installed on your system
- Staged changes in the current repo to be commited
tmp_commit_msg.txt
has to be configured as commit template (see below)
-
Clone this repository to your local machine using git:
git clone https://github.com/5n00py/SmartCommit.git
-
Setting up the Python Environment:
The necessary Python libraries have to be installed and isolated for this project in a virtual environment called
venv
within thepython
folder. Thegc-smart
script will automatically activate and deactivate this virtual environment, so manual activation is not required.Navigate to the
python
directory in theSmartCommit
project:cd SmartCommit/python
Create a virtual environment named
venv
:python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Install the required Python libraries:
pip3 install -r requirements.txt
Deactivate the virtual environment:
deactivate
Now the python environment is set up with all the openai dependences, and the
gp-smart
script will automatically use this environment when executed. -
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY=<your_api_key>
To avoid having to export the OpenAI API key every time you open a new terminal session, you can set it permanently by adding it to your shell's configuration file by adding the above line for example to the .bashrc or .zshrc depending on the shell.
-
Add the location of the
gc-smart
script to your shell's PATH environment variable. This allows you to run it from any directory. To do this, use a command like the following:export PATH=$PATH:/path/to/SmartCommit
(Don't forget to replace
/path/to/SmartCommit
with the actual path to the directory containinggc-smart
.)To make this change permanent, you can add this command to your shell's config file (like
.bashrc
or.zshrc
).Alternatively, you can create an alias for
gc-smart
providing the full path. -
Make the
gc-smart
andgpt-commit-prompter
script executable:chmod +x /path/to/SmartCommit/gc-smart chmod +x /path/to/SmartCommit/python/gpt-commit-prompter.py
(Don't forget to replace
/path/to/gc-smart
with the actual path to the script.
The gpt-commit-prompter
used in gc-smart
allows configuration through the
config.json
file. This flexibility ensures that the tool can be easily
adjusted to specific needs for generating commit messages.
The default configuration is located in the config.json file within the SmartCommit directory. For personalized settings, you can create a custom configuration file at
~/.config/SmartCommit/config.json
When present, this file will be prioritized over the default configuration in the SmartCommit folder.
To do this you can create the ~/.config/SmartCommit
directory (if it doesn't
exist yet) and copy the default config.json
file to it:
mkdir -p ~/.config/SmartCommit/
cp /path/to/SmartCommit/config.json ~/.config/SmartCommit/
By default, the script uses the imperative
style for commit messages. You can
choose a different style by using the -s
or --style
option when running the
script. The available styles, as defined in the config.json
file, are:
imperative
: Generates a message in the imperative mood with a conventional title and bullet points.simple
: Produces a concise, one-line commit message.detailed
: Creates a verbose commit message, elaborating on the changes.conventional
: Follows the Conventional Commits specification for the commit message format.
You can view and customize the styles by editing their corresponding entries in
the config.json
file.
In addition to style customization, the config.json
file allows you to
configure the GPT model used by the script. This feature enables you to select
the most appropriate model version for your needs, ensuring optimal performance
and relevance of the generated commit messages. To change the model, simply
update the model
section in the config.json
file.
For instance, if you have access to newer models like GPT-4, you can change the
model configuration to use gpt-4
. This might improve the quality of the
generated commit messages.
To change the model, simply update the model
section in the config.json
file.
For example:
{
"model": {
"name": "gpt-4"
}
...
Keep in mind that using different models may require different levels of access or subscription plans with OpenAI.
Starting from version 0.4.0 of gc-smart
, there is no longer a need to
manually set tmp_commit_msg.txt
as the global commit template in your Git
configuration. The script has been updated to automatically handle the commit
template for you.
When you run gc-smart
, it will now:
- Temporarily back up any existing Git commit template you have configured.
- Set
tmp_commit_msg.txt
as the commit template for the duration of its execution. - Once the script completes or exits, it will restore your original commit template.
This improvement ensures to maintain your existing Git configurations while gc-smart is in operation.
Let's assume you've made some code changes in your Git repository and have staged them ready to commit.
For illustration, suppose the staged changes are as follows:
--- old_version.py 2021-08-01 12:00:00.000000000 +0100
+++ new_version.py 2021-08-02 12:00:00.000000000 +0100
@@ -1,9 +1,11 @@
def add(a, b):
- return a + b
+ return abs(a + b)
-def subtract(a, b):
- return a - b
+def subtract_absolute(a, b):
+ return abs(a - b)
def multiply(a, b):
- return a * b
+ if a > 0 and b > 0:
+ return a * b
+ else:
+ raise ValueError("Both inputs must be positive.")
-def divide(a, b):
- if b != 0:
- return a / b
- else:
- print("Error: Division by zero.")
- return None
+
+def exponentiate(a, b):
+ return a**b
When you run the gc-smart
script from within the repository, you'll be
presented with an interactive preview of the auto-generated commit message,
which might look something like this:
Refactor arithmetic functions
- Change `add` function to return the absolute value of the sum
- Rename `subtract` function to `subtract_absolute`
- Modify `multiply` function to raise a ValueError if either input is not positive
- Add new `exponentiate` function to calculate the exponential power of two numbers
After reviewing the proposed commit message, if you decide to proceed, this message will appear as a template in your default Git editor. You can then finalize the message or make any necessary modifications before committing the changes to your repository.
This project is licensed under the MIT License - see the LICENSE.md file for details.