* test: ✅ add the first E2E test and configuration to CI (#316) * add tests * Add push config (#220) * feat: add instructions and support for configuring gpt-4-turbo (#320) * 3.0.12 * build * feat: add 'gpt-4-turbo' to supported models in README and config validation --------- Co-authored-by: di-sukharev <dim.sukharev@gmail.com> * ✅ fix the broken E2E tests due to the addition of OCO_GITPUSH (#321) * test(oneFile.test.ts): update test expectations to match new push prompt text * build * Feat: Add Claude 3 support (#318) * 3.0.12 * build * feat: anthropic claude 3 support * fix: add system prompt * fix: type check * fix: package version * fix: update anthropic for dependency bug fix * feat: update build files * feat: update version number --------- Co-authored-by: di-sukharev <dim.sukharev@gmail.com> * 🐛bug fix: enable to use the new format of OpenAI's project API Key (#328) * fix(config.ts): remove validation for OCO_OPENAI_API_KEY length to accommodate variable key lengths * build * ♻️ refactor(config.ts): Addition of UnitTest environment and unittest for commands/config.ts#getConfig (#330) * feat(jest.config.ts): update jest preset for TS ESM support and ignore patterns feat(package.json): add test:unit script with NODE_OPTIONS for ESM refactor(src/commands/config.ts): improve dotenv usage with dynamic paths feat(src/commands/config.ts): allow custom config and env paths in getConfig refactor(src/commands/config.ts): streamline environment variable access feat(test/unit): add unit tests for config handling and utility functions - Implement unit tests for `getConfig` function to ensure correct behavior in various scenarios including default values, global config, and local env file precedence. - Add utility function `prepareFile` for creating temporary files during tests, facilitating testing of file-based configurations. * feat(e2e.yml): add unit-test job to GitHub Actions for running unit tests on pull requests * ci(test.yml): add GitHub Actions workflow for unit and e2e tests on pull requests * refactor(config.ts): streamline environment variable access using process.env directly test(config.test.ts): add setup and teardown for environment variables in tests to ensure test isolation * feat(package.json): add `test:all` script to run all tests in Docker refactor(package.json): consolidate Docker build steps into `test:docker-build` script for DRY principle fix(package.json): ensure `test:unit:docker` and `test:e2e:docker` scripts use the same Docker image and remove container after run chore(test/Dockerfile): remove default CMD to allow dynamic test script execution in Docker * refactor(config.test.ts): anonymize API keys in tests for better security practices * feat(config.test.ts): add tests for OCO_ANTHROPIC_API_KEY configuration * refactor(config.ts): streamline path imports and remove unused DotenvParseOutput - Simplify path module imports by removing default import and using named imports for `pathJoin` and `pathResolve`. - Remove unused `DotenvParseOutput` import to clean up the code. * refactor(config.test.ts): simplify API key mock values for clarity in tests * test(config.test.ts): remove tests for default config values and redundant cases - Removed tests that checked for default config values when no config or env files are present, as these scenarios are now handled differently. - Eliminated tests for empty global config and local env files to streamline testing focus on actual config loading logic. - Removed test for prioritizing local env over global config due to changes in config loading strategy, simplifying the configuration management. * new version --------- Co-authored-by: Takanori Matsumoto <matscube@gmail.com> Co-authored-by: Moret84 <aurelienrivet@hotmail.fr> Co-authored-by: yowatari <4982161+YOwatari@users.noreply.github.com> Co-authored-by: metavind <94786679+metavind@users.noreply.github.com>
Auto-generate meaningful commits in a second
Killing lame commits with AI 🤯🔫
🪩 Winner of GitHub 2023 hackathon 🪩
All the commits in this repo are authored by OpenCommit — look at the commits to see how OpenCommit works. Emojis and long commit descriptions are configurable.
Setup OpenCommit as a CLI tool
You can use OpenCommit by simply running it via the CLI like this oco. 2 seconds and your staged changes are committed with a meaningful message.
-
Install OpenCommit globally to use in any repository:
npm install -g opencommitAlternatively run it via
npx opencommitorbunx opencommitMacOS may ask to run the command with
sudowhen installing a package globally. -
Get your API key from OpenAI. Make sure that you add your payment details, so the API works.
-
Set the key to OpenCommit config:
oco config set OCO_OPENAI_API_KEY=<your_api_key>Your API key is stored locally in the
~/.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 oco shortcut:
git add <files...>
oco
Link to the GitMoji specification: https://gitmoji.dev/
You can also run it with local model through ollama:
- install and start ollama
- run
ollama run mistral(do this only once, to pull model) - run (in your project directory):
git add <files...>
OCO_AI_PROVIDER='ollama' opencommit
Flags
There are multiple optional flags that can be used with the oco command:
Use Full GitMoji Specification
This flag can only be used if the OCO_EMOJI configuration item is set to true. This flag allows users to use all emojis in the GitMoji specification, By default, the GitMoji full specification is set to false, which only includes 10 emojis (🐛✨📝🚀✅♻️⬆️🔧🌐💡).
This is due to limit the number of tokens sent in each request. However, if you would like to use the full GitMoji specification, you can use the --fgm flag.
oco --fgm
Configuration
Local per repo configuration
Create a .env file and add OpenCommit config variables there like this:
OCO_OPENAI_API_KEY=<your OpenAI API token>
OCO_TOKENS_MAX_INPUT=<max model token limit (default: 4096)>
OCO_TOKENS_MAX_OUTPUT=<max response tokens (default: 500)>
OCO_OPENAI_BASE_PATH=<may be used to set proxy path to OpenAI api>
OCO_DESCRIPTION=<postface a message with ~3 sentences description of the changes>
OCO_EMOJI=<boolean, add GitMoji>
OCO_MODEL=<either 'gpt-4', 'gpt-4-turbo', 'gpt-3.5-turbo' (default), 'gpt-3.5-turbo-0125', 'gpt-4-1106-preview', 'gpt-4-turbo-preview' or 'gpt-4-0125-preview'>
OCO_LANGUAGE=<locale, scroll to the bottom to see options>
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, default: '$msg'>
OCO_PROMPT_MODULE=<either conventional-commit or @commitlint, default: conventional-commit>
OCO_ONE_LINE_COMMIT=<one line commit message, default: false>
Global config for all repos
Local config still has more priority than Global config, but you may set OCO_MODEL and OCO_LOCALE globally and set local configs for OCO_EMOJI and OCO_DESCRIPTION per repo which is more convenient.
Simply set any of the variables above like this:
oco config set OCO_MODEL=gpt-4
Configure GitMoji to preface a message.
oco config set OCO_EMOJI=true
To remove preface emojis:
oco config set OCO_EMOJI=false
Switch to GPT-4 or other models
By default, OpenCommit uses gpt-3.5-turbo model.
You may switch to GPT-4 which performs better, but costs ~x15 times more 🤠
oco config set OCO_MODEL=gpt-4
or for as a cheaper option:
oco config set OCO_MODEL=gpt-3.5-turbo
or for GPT-4 Turbo (Preview) which is more capable, has knowledge of world events up to April 2023, a 128k context window and 2-3x cheaper vs GPT-4:
oco config set OCO_MODEL=gpt-4-0125-preview
Make sure that you spell it gpt-4 (lowercase) and that you have API access to the 4th model. Even if you have ChatGPT+, that doesn't necessarily mean that you have API access to GPT-4.
Locale configuration
To globally specify the language used to generate commit messages:
# de, German ,Deutsch
oco config set OCO_LANGUAGE=de
oco config set OCO_LANGUAGE=German
oco config set OCO_LANGUAGE=Deutsch
# fr, French, française
oco config set OCO_LANGUAGE=fr
oco config set OCO_LANGUAGE=French
oco config set OCO_LANGUAGE=française
The default language setting is English All available languages are currently listed in the i18n folder
Push to git
Pushing to git is on by default but if you would like to turn it off just use:
oc config set OCO_GITPUSH=false
Switch to @commitlint
OpenCommit allows you to choose the prompt module used to generate commit messages. By default, OpenCommit uses its conventional-commit message generator. However, you can switch to using the @commitlint prompt module if you prefer. This option lets you generate commit messages in respect with the local config.
You can set this option by running the following command:
oco config set OCO_PROMPT_MODULE=<module>
Replace <module> with either conventional-commit or @commitlint.
Example:
To switch to using th '@commitlint prompt module, run:
oco config set OCO_PROMPT_MODULE=@commitlint
To switch back to the default conventional-commit message generator, run:
oco config set OCO_PROMPT_MODULE=conventional-commit
Integrating with @commitlint
The integration between @commitlint and OpenCommit is done automatically the first time OpenCommit is run with OCO_PROMPT_MODULE set to @commitlint. However, if you need to force set or reset the configuration for @commitlint, you can run the following command:
oco commitlint force
To view the generated configuration for @commitlint, you can use this command:
oco commitlint get
This allows you to ensure that the configuration is set up as desired.
Additionally, the integration creates a file named .opencommit-commitlint which contains the prompts used for the local @commitlint configuration. You can modify this file to fine-tune the example commit message generated by OpenAI. This gives you the flexibility to make adjustments based on your preferences or project guidelines.
OpenCommit generates a file named .opencommit-commitlint in your project directory which contains the prompts used for the local @commitlint configuration. You can modify this file to fine-tune the example commit message generated by OpenAI. If the local @commitlint configuration changes, this file will be updated the next time OpenCommit is run.
This offers you greater control over the generated commit messages, allowing for customization that aligns with your project's conventions.
Git flags
The opencommit or oco commands can be used in place of the git commit -m "${generatedMessage}" command. This means that any regular flags that are used with the git commit command will also be applied when using opencommit or oco.
oco --no-verify
is translated to :
git commit -m "${generatedMessage}" --no-verify
To include a message in the generated message, you can utilize the template function, for instance:
oco '#205: $msg’
opencommit examines placeholders in the parameters, allowing you to append additional information before and after the placeholders, such as the relevant Issue or Pull Request. Similarly, you have the option to customize the OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item, for example, simplifying it to $m!"
Message Template Placeholder Config
Overview
The OCO_MESSAGE_TEMPLATE_PLACEHOLDER feature in the opencommit tool allows users to embed a custom message within the generated commit message using a template function. This configuration is designed to enhance the flexibility and customizability of commit messages, making it easier for users to include relevant information directly within their commits.
Implementation Details
In our codebase, the implementation of this feature can be found in the following segment:
commitMessage = messageTemplate.replace(
config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
commitMessage
);
This line is responsible for replacing the placeholder in the messageTemplate with the actual commitMessage.
Usage
For instance, using the command oco '$msg #205’, users can leverage this feature. The provided code represents the backend mechanics of such commands, ensuring that the placeholder is replaced with the appropriate commit message.
Committing with the Message
Once users have generated their desired commit message, they can proceed to commit using the generated message. By understanding the feature's full potential and its implementation details, users can confidently use the generated messages for their commits.
Ignore files
You can remove files from being sent to OpenAI by creating a .opencommitignore file. For example:
path/to/large-asset.zip
**/*.jpg
This helps prevent opencommit from uploading artifacts and large files.
By default, opencommit ignores files matching: *-lock.* and *.lock
Git hook (KILLER FEATURE)
You can set OpenCommit as Git prepare-commit-msg hook. Hook integrates with your IDE Source Control and allows you to edit the message before committing.
To set the hook:
oco hook set
To unset the hook:
oco 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.
Setup OpenCommit as a GitHub Action (BETA) 🔥
OpenCommit is now available as a GitHub Action which automatically improves all new commits messages when you push to remote!
This is great if you want to make sure all of the commits in all of your repository branches are meaningful and not lame like fix1 or done2.
Create a file .github/workflows/opencommit.yml with the contents below:
name: 'OpenCommit Action'
on:
push:
# this list of branches is often enough,
# but you may still ignore other public branches
branches-ignore: [main master dev development release]
jobs:
opencommit:
timeout-minutes: 10
name: OpenCommit
runs-on: ubuntu-latest
permissions: write-all
steps:
- name: Setup Node.js Environment
uses: actions/setup-node@v2
with:
node-version: '16'
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: di-sukharev/opencommit@github-action-v1.0.4
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
env:
# set openAI api key in repo actions secrets,
# for openAI keys go to: https://platform.openai.com/account/api-keys
# for repo secret go to: <your_repo_url>/settings/secrets/actions
OCO_OPENAI_API_KEY: ${{ secrets.OCO_OPENAI_API_KEY }}
# customization
OCO_TOKENS_MAX_INPUT: 4096
OCO_TOKENS_MAX_OUTPUT: 500
OCO_OPENAI_BASE_PATH: ''
OCO_DESCRIPTION: false
OCO_EMOJI: false
OCO_MODEL: gpt-3.5-turbo
OCO_LANGUAGE: en
OCO_PROMPT_MODULE: conventional-commit
That is it. Now when you push to any branch in your repo — all NEW commits are being improved by your never-tired AI.
Make sure you exclude public collaboration branches (main, dev, etc) in branches-ignore, so OpenCommit does not rebase commits there while improving the messages.
Interactive rebase (rebase -i) changes commits' SHA, so the commit history in remote becomes different from your local branch history. This is okay if you work on the branch alone, but may be inconvenient for other collaborators.
Payments
You pay for your requests to OpenAI API on your own.
OpenCommit stores your key locally.
OpenCommit by default uses 3.5-turbo model, it should not exceed $0.10 per casual working day.
You may switch to gpt-4, it's better, but more expensive.
