copilot.vim icon indicating copy to clipboard operation
copilot.vim copied to clipboard

Enable Copilot to generate Git commit messages based on staged changes

Open khatchad opened this issue 4 months ago • 7 comments

I've configured copilot.vim to work on Git commit message as follows:

let g:copilot_filetypes = {
	    \ 'gitcommit': v:true,
	    \}

However, the suggestions don't seem to take into account the staged changes.

Related to https://github.com/github/copilot.vim/issues/66.

khatchad avatar Oct 24 '25 15:10 khatchad

If you do git commit --verbose then the full diff of the commit should be able in the buffer for Copilot to use as context

devm33 avatar Oct 24 '25 15:10 devm33

That works better, thanks. But, I guess we still don't have the full repository context, which presumably would make the predictions better?

khatchad avatar Oct 27 '25 20:10 khatchad

the full repository context, which presumably would make the predictions better?

Does the g:copilot_workspace_folders look relevant? 🤔

yermulnik avatar Oct 27 '25 20:10 yermulnik

the full repository context, which presumably would make the predictions better?

Does the g:copilot_workspace_folders look relevant? 🤔

Yes, I saw that, bur the value would change dynamically.

khatchad avatar Oct 27 '25 21:10 khatchad

the full repository context, which presumably would make the predictions better?

Does the g:copilot_workspace_folders look relevant? 🤔

Yes, I saw that, bur the value would change dynamically.

Sort of this should depict what I mean:

let g:copilot_workspace_folders = g:copilot_workspace_folders + [expand('%:p:h')]

(the expand('%:p:h') expands to directory of the file which is being opened by vim)

You can check the resulting value then using :echo keys(copilot#Client().workspaceFolders) (ref)

Maybe also expand file's dir to repo root for a more proper context and return file's dir otherwise:

function! GetFileRepoRoot()
	let l:file_path = expand('%:p')
	let l:file_dir = expand('%:p:h')

	if empty(l:file_path) || !filereadable(l:file_path)
		return
	endif

	let l:git_root = system('git -C ' . shellescape(l:file_dir) . ' rev-parse --show-toplevel')
	if v:shell_error == 0
		return substitute(l:git_root, '\n\+$', '', '')
	endif

	return l:file_dir
endfunction
let g:copilot_workspace_folders = ["~/projects/", "/some/other/global/path/", GetFileRepoRoot()]

Your mileage may vary though.

yermulnik avatar Oct 27 '25 22:10 yermulnik

function! GetFileRepoRoot() let l:file_path = expand('%:p') let l:file_dir = expand('%:p:h')

if empty(l:file_path) || !filereadable(l:file_path)
	return
endif

let l:git_root = system('git -C ' . shellescape(l:file_dir) . ' rev-parse --show-toplevel')
if v:shell_error == 0
	return substitute(l:git_root, '\n\+$', '', '')
endif

return l:file_dir

endfunction let g:copilot_workspace_folders = ["~/projects/", "/some/other/global/path/", GetFileRepoRoot()]

erikpoliveira-byte avatar Nov 13 '25 15:11 erikpoliveira-byte

let g:copilot_filetypes = { \ 'gitcommit': v:true, }

erikpoliveira-byte avatar Nov 13 '25 15:11 erikpoliveira-byte