Skip to content

Commit 0c92af5

Browse files
author
skywind3000
committed
Generate proper git commit message using ChatGPT in Vim
This commit adds functionality to generate a proper git commit message using ChatGPT in Vim. The changes include: - Adding an animated GIF to demonstrate the usage of the feature. - Updating the README.md file with instructions on how to use the feature. - Adding a script, `gptcommit.py`, in the `bin` directory for command line usage. - Adding a new function, `gptcommit#gpt#generate()`, in the `gpt.vim` file to generate the commit message. - Modifying the existing function, `gptcommit#gpt#generate()`, to ensure proper handling of fake messages if the `g:gpt_commit_fake` flag is set. - Updating the `gptcommit.py` script to include an example commit message in the `EXAMPLE_MESSAGE` variable. - Adding logic to display the example commit message if the `--concise` option is not provided. These changes aim to improve the commit message generation process and make it more user-friendly.
1 parent c2bcf64 commit 0c92af5

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Preface
22

3-
Generate proper git commit message using ChatGPT in Vim
3+
Generate proper git commit message using ChatGPT in Vim:
4+
5+
![](https://skywind3000.github.io/images/p/misc/2024/gptcommit1.gif)
6+
47

58
## Quick start
69

@@ -55,5 +58,24 @@ If the bang (`!`) is included, `:GptCommand` will not append any text to the cur
5558
| g:gpt_commit_staged | - | `1` | set to 1 to use staged diff |
5659
| g:gpt_commit_max_line | - | `160` | max diff lines to reference |
5760

61+
## Script
62+
63+
For command line usage, you can execute `gptcommit.py` in the `bin` directory:
64+
65+
```
66+
usage: python3 gptcommit.py <options> repo_path
67+
available options:
68+
--key=xxx required, your openai apikey
69+
--staged optional, use staged diff if present
70+
--proxy=xxx optional, proxy support
71+
--maxline=num optional, max diff lines to feed ChatGPT, default ot 160
72+
--model=xxx optional, can be gpt-3.5-turbo (default) or something
73+
--lang=xxx optional, output language
74+
--concise optional, generate concise message if present
75+
--utf8 optional, output utf-8 encoded text if present
76+
```
77+
78+
79+
5880
## Credit
5981

autoload/gptcommit/gpt.vim

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ function! gptcommit#gpt#generate(path) abort
6666
endif
6767
let args += [path]
6868
" echo args
69-
return gptcommit#utils#request(args)
69+
let hr = gptcommit#utils#request(args)
70+
if get(g:, 'gpt_commit_fake', 0)
71+
if exists('g:gpt_commit_fake_msg')
72+
if type(g:gpt_commit_fake_msg) == type([])
73+
let hr = join(g:gpt_commit_fake_msg, "\n")
74+
else
75+
let hr = g:gpt_commit_fake_msg
76+
endif
77+
endif
78+
endif
79+
return hr
7080
endfunc
7181

7282

bin/gptcommit.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,21 @@ def ExtractInfo(obj):
271271
'total_tokens': 1067}}
272272

273273

274+
EXAMPLE_MESSAGE = '''
275+
Add new function to handle command interface in gpt.vim and utils.vim files
276+
- Added a new function `gptcommit#gpt#cmd(bang, path)` to handle the command interface for generating the commit message.
277+
- Added logic to detect the current path if no path is provided.
278+
- Added logic to check if the provided path is a valid directory.
279+
- Added logic to check if the provided path is within a git repository.
280+
- Added logic to handle cases where the buffer is not writable.
281+
- Added logic to generate the commit message and display it.
282+
- Added logic to append the generated commit message to the current buffer or copy it to the unnamed register.
283+
- Modified the `gptcommit#utils#current_path()` function to handle cases specific to the `fugitive` plugin and the `gitcommit` filetype.
284+
- Added a new function `gptcommit#utils#buffer_writable()` to check if a buffer is writable.
285+
- Added a new function `gptcommit#utils#repo_root(path)` to find the root directory of a git repository.
286+
'''.strip('\r\n\t ')
287+
288+
274289
#----------------------------------------------------------------------
275290
# help
276291
#----------------------------------------------------------------------
@@ -365,6 +380,8 @@ def main(argv = None):
365380
obj = chatgpt_request(msgs, OPTIONS['key'], opts)
366381
else:
367382
obj = EXAMPLE_RETURN
383+
if not OPTIONS['concise']:
384+
obj['choices'][0]['message']['content'] = EXAMPLE_MESSAGE
368385
msg = ExtractInfo(obj)
369386
if not isinstance(msg, str):
370387
sys.exit(msg)

0 commit comments

Comments
 (0)