View all files | ||||
Commit for me! 🤖 Generate commit messages using aichat.
Uses the conventional-commits standard by default, but is easily customizable to your own needs.
Just run cfme after staging your changes, and it will generate a commit message using AI, prompt you to review and edit it, then commit it for you.
There is also a demo available showing how to use cfme from within Lazygit. Jump to the Lazygit Integration section below.
Install dependencies:
e.g. using Homebrew/Linuxbrew brew install aichat fzf yq
Install cfme:
Setup aichat credentials (just run aichat and follow the prompts)
ℹ️ Note: You can get your GitHub 'API Key' by creating a Personal Access Token (classic) with Copilot scope.
Stage some changes: git add .
Run cfme to generate commit message: cfme
Table of Contents
| --instructions | -i | INSTRUCTIONS | An optional brief set of instructions to pass the AI, to help it generate commit message candidates. Replaces template string <__INSTRUCTIONS__> in the prompt file. | |
| --prompt-file | -p | FILE_PATH | Overrides the default prompt by reading from a specified file. | |
| --variables-file | -v | FILE_PATH | Overrides path for the prompt variables file ($CFME_PROMPT_VARIABLES_FILE) by reading from a specified file. | |
| --response | -r | (none) | Prints the direct response from the AI, instead of prompting to review response and then committing. Used for piping the response into other tools. | Conflicts with --message |
| --message | -m | (none) | Prints the reviewed commit message instead of committing. Used for piping the reviewed message into other tools. | Conflicts with --response |
| --silent | -s | (none) | Suppresses all non-error output. Also possible through the $CFME_SILENT_MODE environment variable. | |
| --print-parsed-prompt | (none) | (none) | Prints the parsed prompt after replacing template strings with variable values, then exits. Useful for debugging. |
| cfme | Generate, review, and commit using the defaults. |
| cfme -i "Fix issue with user login" | Overrides the <__INSTRUCTIONS__> template string — useful for helping the AI figure out what message to generate. |
| cfme -p "./my-custom-prompt.md" | Overrides the default prompt file. |
| cfme -v "./my-custom-variables.yaml" | Overrides the default variables file. |
| cfme -m | Generates and reviews a commit message, but echoes it instead of committing. |
| cfme -rs | Generates a response to the prompt, then echoes the raw response instead of reviewing and committing, suppressing all non-error output. |
| cfme -si "<Instructions>" -p "./my-prompt.md" -v "./my-variables.yaml" | Generates a raw response to the prompt, replacing the <__INSTRUCTIONS__> template string, using a custom prompt file and variables file, while suppressing all non-error output. |
The Quick Start section above provides a quick way to setup cfme. What follows is a more detailed explanation of the installation process.
Install dependencies:
cfme depends on the following command-line tools. Please refer to their respective installation instructions for your platform.
Brew is a convenient way to install all of the dependencies on macOS and Linux, but feel free to use any other method you prefer.
Install cfme: Download the latest release binary from the Releases page, make it executable, and move it to a directory in your $PATH.
On Unix-like systems, you can do this with the following commands:
On Windows, it is important to know that cfme is a shell script written in bash, so you will need to have a bash environment set up, e.g. through WSL or, Git Bash (which comes with Git), Cygwin, mingw-64 etc.
Setup aichat credentials: Run aichat and follow the prompts to set up your AI chat model and API Key.
For GitHub Copilot, you can get your 'API Key' by creating a Personal Access Token (classic). Be sure to give this token the Copilot scope. Everyone who has a GitHub account has access to the GitHub Copilot Free plan. You may hit limits with this plan, however.
Educators, students, and open source contributors may be eligible for free access to GitHub Copilot Pro. See this page for more information.
aichat supports a bunch of different AI providers, such as OpenAI GPT, Claude, and more.
Choose a model wisely: For example, GPT-4 mini generates faster commit message faster, whereas GPT-4 probably writes more accurate messages.
Stage some changes: cfme generates commit messages based on the staged changes in your git repository. Note that changed files aren't yet staged, so you need to stage them first using git add <file>. (You can optionally alter your prompt to include unstaged changes if you want to, see the 'Customization' section below for more information.)
Run cfme to generate commit message: Simply run cfme in your terminal while in the git repository with staged changes. Follow the prompts to review and edit the generated commit message, then confirm to commit it.
Be sure to check out the 'Customization' section below to learn how to customize the prompt and variables to your own needs, and check out the 'Usage' section to learn about the available options, such as passing instructions to the AI on what the commit is about, piping the output to other tools, and more.
Not happy with the default prompt or variables?
cfme is designed to be customizable to your needs.
Simply create your own prompt at $CFME_CONFIG_DIR/$CFME_PROMPT_DIR/$CFME_PROMPT_TYPE/<your-prompt>.md and/or your own variables file at $CFME_CONFIG_DIR/$CFME_PROMPT_DIR/$CFME_PROMPT_TYPE/<your-variables>.yaml. then set $CFME_DEFAULT_PROMPT_FILE and/or $CFME_PROMPT_VARIABLES_FILE to point to your custom files, or use the -p and -v flags to specify them at runtime.
$CFME_PROMPT_TYPE will determine the subdirectory in the $CFME_PROMPT_DIR directory by default, allowing you to quickly switch between different prompt types (e.g. conventional-commits, or any other standard your team uses).
Example:
Say you want to create a custom prompt for your own commit message standard, you could do the following:
Or just use the flags:
Say you want to use the conventional-commits standard, but with your own custom prompt and variables, you could do the following:
Prompt file: ~/.config/cfme/prompts/conventional-commits/my-custom-prompt.md
Variables file: ~/.config/cfme/prompts/conventional-commits/my-custom-vars.yaml
Environment variables:
Or just use the flags:
ℹ️ If the specified $CFME_DEFAULT_PROMPT_FILE or $CFME_PROMPT_VARIABLES file does not yet exist, cfme will attempt to fetch these files from the /defaults folder in this repository. This requires $CFME_PROMPT_TYPE to be set to the default value of conventional-commits. See the 'Environment Variables' section below for more information.
cfme uses a prompt file in markdown format, in which you can write your prompt that will be sent to the AI.
cfme will parse your prompt file, replacing any template strings in the format of <__MY_TEMPLATE_STRING__> with the corresponding values from your variables file.
There are three special template strings that cfme recognizes by default:
ℹ️cfme will warn you if you have template strings in your prompt file that are not defined in your variables file, and vice versa, it will warn you if you have variables defined that are not used in your prompt file.
For example, the simplest prompt file could look like this:
A more complex prompt file could look like this:
See below for how to write the corresponding variables file.
The variables file is a simple YAML file that defines key-value pairs for use in your prompt file. You can either define literal strings (single or multi-line), or command (sequences) that will be evaluated, and their output used as the value.
A valid variables file must have a top-level key vars, which contains an array of variable definitions.
A variable definition
For example, the variables file corresponding to the above complex prompt could look like this:
ℹ️ If you want to do so, you can use define default <__INSTRUCTIONS__> variable in your variables file, which you can overwrite by passing the [-i|--instructions] flag at runtime. You can also just leave out the <__INSTRUCTIONS__> template string from the variables file, and cfme will replace it with an empty string if no instructions are provided at runtime.
cfme supports the following environment variables for configuration:
Recommendations:
For advanced users, you can also set where cfme fetches the default files from, should they not exist on your system. This requires $CFME_PROMPT_TYPE to be set something other than conventional-commits. This is useful if you maintain your own fork with custom default prompts you want to use.
Lazygit is awesome. But cfme allows you to be even lazier! You can use cfme from within Lazygit's TUI to generate commit messages:
Just add the following custom command to your Lazygit config file (e.g. ~/.config/lazygit/config.yml):
And now, use lazygit to stage your changes, then just press Ctrl+a to run cfme and generate a commit message for the staged changes!
As cfme is just a bash script, you can easily integrate it into other tools and workflows. Have a look at the Lazygit section above for an example of how to do this.
The -r and -m flags are especially interesting for integration into other tools, as they allow you to pipe the AI-generated response or the reviewed commit message into other tools, instead of committing it directly.
The -p and -v flags allow you to specify custom prompt and variables files, which is useful if you want to have different prompts for different workflows.
I'm sure you can come up with many more use cases! If you ever get stuck integrating cfme into your workflow, or if you need an additional feature to make the integration easier, feel free to ask for help. Just open up an issue!
If you would like to contribute to this project, you're more than welcome to do so! Feel free to open up issues or submit pull requests.
Example contributions include:
See CONTRIBUTING.md for more information on how to contribute.
This project is licensed under the MIT License. See the LICENSE file for details.