Added a TODO list with tasks that need to be completed. These tasks include building for both mjs and cjs, configuring esbuild to make the bundle smaller, adding // TODOs in the code, batching small files in one request, adding tests, and making the hook work. * ✨ feat(api.ts): add OpenAI class with generateCommitMessage method This commit adds a new OpenAI class with a generateCommitMessage method that uses the OpenAI API to generate a commit message based on an array of messages. The method takes an array of ChatCompletionRequestMessage objects and returns a Promise that resolves to a ChatCompletionResponseMessage object. The OpenAI class is exported as a singleton instance named api. * ✨ feat(prepare-commit-msg-hook.ts): add support for generating commit messages with chat completion This commit adds support for generating commit messages with chat completion. The `prepare-commit-msg-hook.ts` file now imports the `generateCommitMessageWithChatCompletion` function from the `generateCommitMessageFromGitDiff` module. The function generates a commit message based on the staged git diff and appends it to the commit message file. If the `OPENAI_API_KEY` environment variable is not set, an error is thrown. If the commit source is specified, the function returns without generating a commit message. * 🆕 feat(generateCommitMessageFromGitDiff.ts): add functionality to generate commit messages from git diff This commit adds a new file, generateCommitMessageFromGitDiff.ts, which contains a function that generates commit messages from the output of the 'git diff --staged' command. The function uses the OpenAI API to prompt the user to create a commit message in the conventional commit convention. The user can choose to use Gitmoji convention to preface the commit and add a short description of what the commit is about. * 🐛 fix(server.ts): change port variable case from lowercase port to uppercase PORT * ✨ feat(server.ts): add support for process.env.PORT environment variable The port variable is now named PORT, which improves consistency with the naming conventions as PORT is a constant. Support for an environment variable allows the application to be more flexible as it can now run on any available port specified via the process.env.PORT environment variable. * 🚀 feat: add function to generate commit messages from diff This commit adds a new function that generates commit messages from a diff. The function takes a diff as input and splits it into files. It then generates commit messages for each file and returns them as a concatenated string. If the total length of the commit message exceeds the maximum allowed length, the function skips the file. If the commit message is empty, the function skips the file. If an error occurs during the process, the function returns an error. * ✨ feat(git.ts): add function to assert git repository existence * ✨ feat(git.ts): add function to get staged git diff The assertGitRepo function checks if the current directory is a git repository by running the 'git rev-parse' command. If the command fails, an error is thrown. The getStagedGitDiff function returns the staged diff of the git repository. It takes an optional boolean argument isStageAllFlag, which when true stages all changes before getting the diff. The function uses the 'git diff --staged' command to get the diff and excludes big files from the diff. The function returns an object with two properties: files, which is an array of the names of the files that have changes, and diff, which is the diff of the staged changes.
Examples
Look into the commits to see how OpenCommit works. Emoji and long commit description text is configurable.
Setup
The minimum supported version of Node.js is the latest v14. Check your Node.js version with
node --version.
-
Install opencommit globally to use in any repository:
npm install -g opencommit -
Get your API key from OpenAI
-
Set the key to opencommit config:
opencommit config set OPENAI_API_KEY=<your token>Your token isn't sent to anyone, it's saved in
~/.opencommitconfig file.
Usage
You can call opencommit directly to generate a commit message for your staged changes:
git add <files...>
opencommit
You can also use the oc shortcut:
git add <files...>
oc
Features
Preface commits with emoji 🤠
GitMoji convention is used.
To add emoji:
opencommit config set emoji=true
To remove emoji:
opencommit config set emoji=false
Postface commits with descriptions of changes 🤠
To add descriptions:
opencommit config set description=true
To remove description:
opencommit config set description=false
Git hook
You can set opencommit as Git prepare-commit-msg hook. Hook integrates with you IDE Source Control and allows you edit the message before commit.
To set the hook:
opencommit hook set
To unset the hook:
opencommit hook unset
To use the hook:
git add <files...>
git commit
Or follow the process of your IDE Source Control feature, when it calls git commit command — OpenCommit will integrate into the flow.
Payments
You pay for your own requests to OpenAI API. OpenCommit uses ChatGPT official model, that is ~10x times cheaper than GPT-3.