vimpatch.lua: automate version.c

Invoke it like this:

    VIM_SOURCE_DIR=~/neovim/.vim-src/ nvim -i NONE -u NONE --headless +'luafile ./scripts/vimpatch.lua' +q
This commit is contained in:
Justin M. Keyes 2017-12-26 03:38:42 +01:00
parent 903ed09a61
commit 7773bbd098
2 changed files with 93 additions and 15 deletions

View File

@ -14,25 +14,23 @@ readonly BRANCH_PREFIX="vim-"
CREATED_FILES=()
usage() {
echo "Helper script for porting Vim patches. For more information, see"
echo "Port Vim patches to Neovim"
echo "https://github.com/neovim/neovim/wiki/Merging-patches-from-upstream-vim"
echo
echo "Usage: ${BASENAME} [-h | -l | -p vim-revision | -r pr-number]"
echo
echo "Options:"
echo " -h Show this message and exit."
echo " -l Show list of missing Vim patches."
echo " -L Print missing Vim patches in machine-readable form."
echo " -p {vim-revision} Download and generate the specified Vim patch."
echo " vim-revision can be a version number '8.0.xxx'"
echo " or a valid Git ref (hash, tag, etc.)."
echo " -P {vim-revision} Download, generate and apply the Vim patch."
echo " -g {vim-revision} Download the Vim patch vim-revision."
echo " vim-revision can be a version number of the "
echo " format '7.4.xxx' or a Git commit hash."
echo " -s Submit a vim-patch pull request to Neovim."
echo " -r {pr-number} Review a vim-patch pull request to Neovim."
echo ' -V Clones the Vim source code to $VIM_SOURCE_DIR.'
echo " -l List missing Vim patches."
echo " -L List missing Vim patches (for scripts)."
echo " -M List all merged patch-numbers (at current v:version)."
echo " -p {vim-revision} Download and generate a Vim patch. vim-revision"
echo " can be a Vim version (8.0.xxx) or a Git hash."
echo " -P {vim-revision} Download, generate and apply a Vim patch."
echo " -g {vim-revision} Download a Vim patch."
echo " -s Create a vim-patch pull request."
echo " -r {pr-number} Review a vim-patch pull request."
echo ' -V Clone the Vim source code to $VIM_SOURCE_DIR.'
echo
echo ' $VIM_SOURCE_DIR controls where Vim sources are found'
echo " (default: '${VIM_SOURCE_DIR_DEFAULT}')"
@ -334,7 +332,7 @@ list_vim_commits() { (
cd "${VIM_SOURCE_DIR}" && git log --reverse --format='%H' v8.0.0000..HEAD
) }
# Prints all "vim-patch:xxx" tokens found in the Nvim git log.
# Prints all (sorted) "vim-patch:xxx" tokens found in the Nvim git log.
list_vimpatch_tokens() {
local tokens
# Find all "vim-patch:xxx" tokens in the Nvim git log.
@ -345,6 +343,15 @@ list_vimpatch_tokens() {
| uniq
}
# Prints all patch-numbers (for the current v:version) for which there is
# a "vim-patch:xxx" token in the Nvim git log.
list_vimpatch_numbers() {
# Transform "vim-patch:X.Y.ZZZZ" to "ZZZZ".
list_vimpatch_tokens | while read vimpatch_token; do
echo "$vimpatch_token" | grep '8\.0\.' | sed 's/.*vim-patch:8\.0\.\([0-9a-z]\+\).*/\1/'
done
}
# Prints a newline-delimited list of Vim commits, for use by scripts.
list_missing_vimpatches() {
local tokens vim_commit vim_commits is_missing vim_tag patch_number
@ -494,7 +501,7 @@ review_pr() {
clean_files
}
while getopts "hlLVp:P:g:r:s" opt; do
while getopts "hlLMVp:P:g:r:s" opt; do
case ${opt} in
h)
usage
@ -508,6 +515,10 @@ while getopts "hlLVp:P:g:r:s" opt; do
list_missing_vimpatches
exit 0
;;
M)
list_vimpatch_numbers
exit 0
;;
p)
stage_patch "${OPTARG}"
exit 0

67
scripts/vimpatch.lua Executable file
View File

@ -0,0 +1,67 @@
-- Updates version.c list of applied Vim patches.
--
-- Usage:
-- VIM_SOURCE_DIR=~/neovim/.vim-src/ nvim -i NONE -u NONE --headless +'luafile ./scripts/vimpatch.lua' +q
local nvim = vim.api
local function pprint(o)
print(nvim.nvim_call_function('string', { o }))
end
local function systemlist(...)
local rv = nvim.nvim_call_function('systemlist', ...)
local err = nvim.nvim_get_vvar('shell_error')
local args_str = nvim.nvim_call_function('string', ...)
if 0 ~= err then
error('command failed: '..args_str)
end
return rv
end
local function vimpatch_sh_list_numbers()
return systemlist( { { 'bash', '-c', 'scripts/vim-patch.sh -M', } } )
end
-- Generates the lines to be inserted into the src/version.c
-- `included_patches[]` definition.
local function gen_version_c_lines()
-- Set of merged Vim 8.0.zzzz patch numbers.
local merged_patch_numbers = {}
local highest = 0
for _, n in ipairs(vimpatch_sh_list_numbers()) do
if n then
merged_patch_numbers[tonumber(n)] = true
highest = math.max(highest, n)
end
end
local lines = {}
for i = highest, 0, -1 do
local is_merged = (nil ~= merged_patch_numbers[i])
if is_merged then
table.insert(lines, string.format(' %s,', i))
else
table.insert(lines, string.format(' // %s,', i))
end
end
return lines
end
local function patch_version_c()
local lines = gen_version_c_lines()
nvim.nvim_command('silent noswapfile noautocmd edit src/nvim/version.c')
nvim.nvim_command('/static const int included_patches')
-- Delete the existing lines.
nvim.nvim_command('silent normal! j0d/};\rk')
-- Insert the lines.
nvim.nvim_call_function('append', {
nvim.nvim_eval('line(".")'),
lines,
})
nvim.nvim_command('silent write')
end
patch_version_c()