A CLI written in Go language that writes git commit messages or do a code review brief for you using ChatGPT AI (gpt-3.5-turbo, gpt-4 model) and automatically installs a git prepare-commit-msg hook.
- Support Azure OpenAI Service or OpenAI API.
- Support conventional commits specification.
- Support Git prepare-commit-msg Hook, see the Git Hooks documentation.
- Support customize generate diffs with n lines of context, the default is three.
- Support for excluding files from the git diff command.
- Support commit message translation into another language (support
en
,zh-tw
orzh-cn
). - Support socks proxy or custom network HTTP proxy.
- Support model lists like
gpt-4
,gpt-3.5-turbo
...etc. - Support do a brief code review.
Currently, the only supported method of installation on MacOS is Homebrew. To install codegpt
via brew:
brew tap appleboy/tap
brew install codegpt
The pre-compiled binaries can be downloaded from release page.Change the binary permissions to 755
and copy the binary to the system bin directory. Use the codegpt
command as shown below.
$ codegpt version
version: v0.4.3 commit: xxxxxxx
Please first create your OpenAI API Key. The OpenAI Platform allows you to generate a new API Key.
An environment variable is a variable that is set on your operating system, rather than within your application. It consists of a name and value.We recommend that you set the name of the variable to OPENAI_API_KEY
.
See the Best Practices for API Key Safety.
export OPENAI_API_KEY=sk-xxxxxxx
or store your API key in custom config file.
codegpt config set openai.api_key sk-xxxxxxx
This will create a .codegpt.yaml
file in your home directory ($HOME/.config/codegpt/.codegpt.yaml). The following options are available.
- openai.base_url: replace the default base URL (
https://api.openai.com/v1
). - openai.api_key: generate API key from openai platform page.
- openai.org_id: Identifier for this organization sometimes used in API requests. see organization settings. only for
openai
service. - openai.model: default model is
gpt-3.5-turbo
, you can change togpt-4
or other available model list. - openai.proxy: http/https client proxy.
- openai.socks: socks client proxy.
- openai.timeout: default http timeout is
10s
(ten seconds). - openai.max_tokens: default max tokens is
300
. see reference max_tokens. - openai.temperature: default temperature is
0.7
. see reference temperature. - git.diff_unified: generate diffs with
<n>
lines of context, default is3
. - git.exclude_list: exclude file from
git diff
command. - openai.provider: default service provider is
openai
, you can change toazure
. - openai.model_name: model deployment name (for azure).
- output.lang: default language is
en
and available languageszh-tw
,zh-cn
,ja
.
Please get the API key
, Endpoint
and Model deployments
list from Azure Resource Management Portal on left menu.
Update your config file.
codegpt config set openai.provider azure
codegpt config set openai.base_url https://xxxxxxxxx.openai.azure.com/
codegpt config set openai.api_key xxxxxxxxxxxxxxxx
codegpt config set openai.model_name xxxxx-gpt-35-turbo
There are two methods for generating a commit message using the codegpt
command. The first is CLI mode, and the second is Git Hook.
You can call codegpt
directly to generate a commit message for your staged changes:
git add <files...>
codegpt commit --preview
The commit message is shown below.
Summarize the commit message use gpt-3.5-turbo model
We are trying to summarize a git diff
We are trying to summarize a title for pull request
================Commit Summary====================
feat: Add preview flag and remove disableCommit flag in commit command and template file.
- Add a `preview` flag to the `commit` command
- Remove the `disbaleCommit` flag from the `prepare-commit-msg` template file
==================================================
Write the commit message to .git/COMMIT_EDITMSG file
or translate all git commit messages into a different language (Traditional Chinese
, Simplified Chinese
or Japanese
)
codegpt commit --lang zh-tw --preview
Consider the following outcome:
Summarize the commit message use gpt-3.5-turbo model
We are trying to summarize a git diff
We are trying to summarize a title for pull request
We are trying to translate a git commit message to Traditional Chinese language
================Commit Summary====================
功能:重構 codegpt commit 命令標記
- 將「codegpt commit」命令新增「預覽」標記
- 從「codegpt commit」命令中移除「--disableCommit」標記
==================================================
Write the commit message to .git/COMMIT_EDITMSG file
You can replace the tip of the current branch by creating a new commit. just use --amend
flag
codegpt commit --amend
Default commit message template as following:
{{ .summarize_prefix }}: {{ .summarize_title }}
{{ .summarize_message }}
change format with template string using --template_string
parameter:
codegpt commit --preview --template_string \
"[{{ .summarize_prefix }}]: {{ .summarize_title }}"
change format with template file using --template_file
parameter:
codegpt commit --preview --template_file your_file_path
Add custom variable to git commit message template:
{{ .summarize_prefix }}: {{ .summarize_title }}
{{ .summarize_message }}
{{ if .JIRA_URL }}{{ .JIRA_URL }}{{ end }}
Add custom variable to git commit message template using --template_vars
parameter:
codegpt commit --preview --template_file your_file_path --template_vars JIRA_URL=https://jira.example.com/ABC-123
Load custom variable from file using --template_vars_file
parameter:
codegpt commit --preview --template_file your_file_path --template_vars_file your_file_path
See the template_vars_file
format as following:
JIRA_URL=https://jira.example.com/ABC-123
You can also use the prepare-commit-msg hook to integrate codegpt
with Git. This allows you to use Git normally and edit the commit message before committing.
You want to install the hook in the Git repository:
codegpt hook install
You want to remove the hook from the Git repository:
codegpt hook uninstall
Stage your files and commit after installation:
git add <files...>
git commit
codegpt
will generate the commit message for you and pass it back to Git. Git will open it with the configured editor for you to review/edit it. Then, to commit, save and close the editor!
$ git commit
Summarize the commit message use gpt-3.5-turbo model
We are trying to summarize a git diff
We are trying to summarize a title for pull request
================Commit Summary====================
Improve user experience and documentation for OpenAI tools
- Add download links for pre-compiled binaries
- Include instructions for setting up OpenAI API key
- Add a CLI mode for generating commit messages
- Provide references for OpenAI Chat completions and ChatGPT/Whisper APIs
==================================================
Write the commit message to .git/COMMIT_EDITMSG file
[main 6a9e879] Improve user experience and documentation for OpenAI tools
1 file changed, 56 insertions(+)
You can use codegpt
to generate a code review message for your staged changes:
codegpt review
or translate all code review messages into a different language (Traditional Chinese
, Simplified Chinese
or Japanese
)
codegpt review --lang zh-tw
See the following result:
Code review your changes using gpt-3.5-turbo model
We are trying to review code changes
PromptTokens: 1021, CompletionTokens: 200, TotalTokens: 1221
We are trying to translate core review to Traditional Chinese language
PromptTokens: 287, CompletionTokens: 199, TotalTokens: 486
================Review Summary====================
總體而言,此程式碼修補似乎在增加 Review 指令的功能,允許指定輸出語言並在必要時進行翻譯。以下是需要考慮的潛在問題:
- 輸出語言沒有進行輸入驗證。如果指定了無效的語言代碼,程式可能會崩潰或產生意外結果。
- 此使用的翻譯 API 未指定,因此不清楚是否存在任何安全漏洞。
- 無法處理翻譯 API 調用的錯誤。如果翻譯服
==================================================
another php example code:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
$html .= "<pre>{$cmd}</pre>";
}
?>
code review result:
================Review Summary====================
Code review:
1. Security: The code is vulnerable to command injection attacks as the user input is directly used in the shell_exec() function. An attacker can potentially execute malicious commands on the server by injecting them into the 'ip' parameter.
2. Error handling: There is no error handling in the code. If the ping command fails, the error message is not displayed to the user.
3. Input validation: There is no input validation for the 'ip' parameter. It should be validated to ensure that it is a valid IP address or domain name.
4. Cross-platform issues: The code assumes that the server is either running Windows or *nix operating systems. It may not work correctly on other platforms.
Suggestions for improvement:
1. Use escapeshellarg() function to sanitize the user input before passing it to shell_exec() function to prevent command injection.
2. Implement error handling to display error messages to the user if the ping command fails.
3. Use a regular expression to validate the 'ip' parameter to ensure that it is a valid IP address or domain name.
4. Use a more robust method to determine the operating system, such as the PHP_OS constant, which can detect a wider range of operating systems.
==================================================