vim-patch.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. #!/usr/bin/env bash
  2. set -e
  3. set -u
  4. # Use privileged mode, which e.g. skips using CDPATH.
  5. set -p
  6. # https://www.shellcheck.net/wiki/SC2031
  7. shopt -s lastpipe
  8. # Ensure that the user has a bash that supports -A
  9. if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
  10. >&2 echo "error: script requires bash 4+ (you have ${BASH_VERSION})."
  11. exit 1
  12. fi
  13. readonly NVIM_SOURCE_DIR="${NVIM_SOURCE_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
  14. readonly VIM_SOURCE_DIR_DEFAULT="${NVIM_SOURCE_DIR}/.vim-src"
  15. readonly VIM_SOURCE_DIR="${VIM_SOURCE_DIR:-${VIM_SOURCE_DIR_DEFAULT}}"
  16. BASENAME="$(basename "${0}")"
  17. readonly BASENAME
  18. readonly BRANCH_PREFIX="vim-"
  19. CREATED_FILES=()
  20. usage() {
  21. echo "Port Vim patches to Neovim"
  22. echo "https://neovim.io/doc/user/dev_vimpatch.html"
  23. echo
  24. echo "Usage: ${BASENAME} [-h | -l | -p vim-revision | -r pr-number]"
  25. echo
  26. echo "Options:"
  27. echo " -h Show this message and exit."
  28. echo " -l [git-log opts] List missing Vim patches."
  29. echo " -L [git-log opts] List missing Vim patches (for scripts)."
  30. echo " -m {vim-revision} List previous (older) missing Vim patches."
  31. echo " -M List all merged patch-numbers (at current v:version)."
  32. echo " -p {vim-revision} Download and generate a Vim patch. vim-revision"
  33. echo " can be a Vim version (8.1.xxx) or a Git hash."
  34. echo " -P {vim-revision} Download, generate and apply a Vim patch."
  35. echo " -g {vim-revision} Download a Vim patch."
  36. echo " -s [pr args] Create a vim-patch pull request."
  37. echo " -r {pr-number} Review a vim-patch pull request."
  38. echo " -V Clone the Vim source code to \$VIM_SOURCE_DIR."
  39. echo
  40. echo " \$VIM_SOURCE_DIR controls where Vim sources are found"
  41. echo " (default: '${VIM_SOURCE_DIR_DEFAULT}')"
  42. echo
  43. echo "Examples:"
  44. echo
  45. echo " - List missing patches for a given file (in the Vim source):"
  46. echo " $0 -l -- src/edit.c"
  47. }
  48. msg_ok() {
  49. printf '\e[32m✔\e[0m %s\n' "$@"
  50. }
  51. msg_err() {
  52. printf '\e[31m✘\e[0m %s\n' "$@" >&2
  53. }
  54. # Checks if a program is in the user's PATH, and is executable.
  55. check_executable() {
  56. test -x "$(command -v "${1}")"
  57. }
  58. require_executable() {
  59. if ! check_executable "${1}"; then
  60. >&2 echo "${BASENAME}: '${1}' not found in PATH or not executable."
  61. exit 1
  62. fi
  63. }
  64. clean_files() {
  65. if [[ ${#CREATED_FILES[@]} -eq 0 ]]; then
  66. return
  67. fi
  68. echo
  69. echo "Created files:"
  70. local file
  71. for file in "${CREATED_FILES[@]}"; do
  72. echo " • ${file}"
  73. done
  74. read -p "Delete these files (Y/n)? " -n 1 -r reply
  75. echo
  76. if [[ "${reply}" == n ]]; then
  77. echo "You can use 'git clean' to remove these files when you're done."
  78. else
  79. rm -- "${CREATED_FILES[@]}"
  80. fi
  81. }
  82. get_vim_sources() {
  83. require_executable git
  84. if [[ ! -d ${VIM_SOURCE_DIR} ]]; then
  85. echo "Cloning Vim into: ${VIM_SOURCE_DIR}"
  86. git clone https://github.com/vim/vim.git "${VIM_SOURCE_DIR}"
  87. cd "${VIM_SOURCE_DIR}"
  88. elif [[ "${1-}" == update ]]; then
  89. cd "${VIM_SOURCE_DIR}"
  90. if ! [ -d ".git" ] \
  91. && ! [ "$(git rev-parse --show-toplevel)" = "${VIM_SOURCE_DIR}" ]; then
  92. msg_err "${VIM_SOURCE_DIR} does not appear to be a git repository."
  93. echo " Please remove it and try again."
  94. exit 1
  95. fi
  96. echo "Updating Vim sources: ${VIM_SOURCE_DIR}"
  97. if git pull --ff; then
  98. msg_ok "Updated Vim sources."
  99. else
  100. msg_err "Could not update Vim sources; ignoring error."
  101. fi
  102. else
  103. cd "${VIM_SOURCE_DIR}"
  104. fi
  105. }
  106. commit_message() {
  107. if [[ "${vim_message}" == "vim-patch:${vim_version}:"* ]]; then
  108. printf '%s\n\n%s\n\n%s' "${vim_message}" "${vim_commit_url}" "${vim_coauthors}"
  109. else
  110. printf 'vim-patch:%s: %s\n\n%s\n\n%s' "${vim_version:0:7}" "${vim_message}" "${vim_commit_url}" "${vim_coauthors}"
  111. fi
  112. }
  113. find_git_remote() {
  114. local git_remote
  115. if [[ "${1-}" == fork ]]; then
  116. git_remote=$(git remote -v | awk '$2 !~ /github.com[:\/]neovim\/neovim/ && $3 == "(fetch)" {print $1; exit}')
  117. else
  118. git_remote=$(git remote -v | awk '$2 ~ /github.com[:\/]neovim\/neovim/ && $3 == "(fetch)" {print $1; exit}')
  119. fi
  120. if [[ -z "$git_remote" ]]; then
  121. git_remote="origin"
  122. fi
  123. echo "$git_remote"
  124. }
  125. # Assign variables for a given Vim tag, patch version, or commit.
  126. # Might exit in case it cannot be found, after updating Vim sources.
  127. assign_commit_details() {
  128. local vim_commit_ref
  129. if [[ ${1} =~ v?[0-9]\.[0-9]\.[0-9]{3,4} ]]; then
  130. # Interpret parameter as version number (tag).
  131. if [[ "${1:0:1}" == v ]]; then
  132. vim_version="${1:1}"
  133. vim_tag="${1}"
  134. else
  135. vim_version="${1}"
  136. vim_tag="v${1}"
  137. fi
  138. vim_commit_ref="$vim_tag"
  139. local munge_commit_line=true
  140. else
  141. # Interpret parameter as commit hash.
  142. vim_version="${1:0:7}"
  143. vim_tag=
  144. vim_commit_ref="$vim_version"
  145. local munge_commit_line=false
  146. fi
  147. local get_vim_commit_cmd="git -C ${VIM_SOURCE_DIR} log -1 --format=%H ${vim_commit_ref} --"
  148. vim_commit=$($get_vim_commit_cmd 2>&1) || {
  149. # Update Vim sources.
  150. get_vim_sources update
  151. vim_commit=$($get_vim_commit_cmd 2>&1) || {
  152. >&2 msg_err "Couldn't find Vim revision '${vim_commit_ref}': git error: ${vim_commit}."
  153. exit 3
  154. }
  155. }
  156. vim_commit_url="https://github.com/vim/vim/commit/${vim_commit}"
  157. vim_message="$(git -C "${VIM_SOURCE_DIR}" log -1 --pretty='format:%B' "${vim_commit}" \
  158. | sed -Ee 's/([^A-Za-z0-9])(#[0-9]{1,})/\1vim\/vim\2/g')"
  159. local vim_coauthor0
  160. vim_coauthor0="$(git -C "${VIM_SOURCE_DIR}" log -1 --pretty='format:Co-authored-by: %an <%ae>' "${vim_commit}")"
  161. # Extract co-authors from the commit message.
  162. vim_coauthors="$(echo "${vim_message}" | (grep -E '^Co-authored-by: ' || true) | (grep -Fxv "${vim_coauthor0}" || true))"
  163. vim_coauthors="$(echo "${vim_coauthor0}"; echo "${vim_coauthors}")"
  164. # Remove Co-authored-by and Signed-off-by lines from the commit message.
  165. vim_message="$(echo "${vim_message}" | grep -Ev '^(Co-authored|Signed-off)-by: ')"
  166. if [[ ${munge_commit_line} == "true" ]]; then
  167. # Remove first line of commit message.
  168. vim_message="$(echo "${vim_message}" | sed -Ee '1s/^patch /vim-patch:/')"
  169. fi
  170. patch_file="vim-${vim_version}.patch"
  171. }
  172. # Patch surgery
  173. preprocess_patch() {
  174. local file="$1"
  175. local nvim="nvim -u NONE -n -i NONE --headless"
  176. # Remove Filelist, README
  177. local na_files='Filelist\|README.*'
  178. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/\<\%('"${na_files}"'\)\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  179. # Remove *.proto, Make*, INSTALL*, gui_*, beval.*, some if_*, gvim, libvterm, tee, VisVim, xpm, xxd
  180. local na_src='auto\|configure.*\|GvimExt\|hardcopy.*\|libvterm\|proto\|tee\|VisVim\|xpm\|xxd\|Make.*\|INSTALL.*\|beval.*\|gui.*\|if_cscop\|if_lua\|if_mzsch\|if_olepp\|if_ole\|if_perl\|if_py\|if_ruby\|if_tcl\|if_xcmdsrv'
  181. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/\S*\<\%(testdir/\)\@<!\%('"${na_src}"'\)\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  182. # Remove runtime/print/
  183. local na_rt='print\/.*'
  184. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/\<\%('"${na_rt}"'\)\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  185. # Remove unwanted Vim doc files.
  186. local na_doc='channel\.txt\|if_cscop\.txt\|netbeans\.txt\|os_\w\+\.txt\|print\.txt\|term\.txt\|todo\.txt\|vim9\.txt\|tags'
  187. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/doc/\<\%('"${na_doc}"'\)\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  188. # Remove "Last change ..." changes in doc files.
  189. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'%s/^@@.*\n.*For Vim version.*Last change.*\n.*For Vim version.*Last change.*//' +w +q "$file"
  190. # Remove gui, setup, screen dumps, testdir/Make_*.mak files
  191. local na_src_testdir='gui_.*\|Make_amiga\.mak\|Make_dos\.mak\|Make_ming\.mak\|Make_vms\.mms\|dumps/.*\.dump\|setup_gui\.vim'
  192. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/testdir/\<\%('"${na_src_testdir}"'\)\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  193. # Remove testdir/test_*.vim files
  194. local na_src_testdir='balloon.*\|behave\.vim\|channel.*\|crypt\.vim\|cscope\.vim\|gui.*\|hardcopy\.vim\|job_fails\.vim\|json\.vim\|listener\.vim\|mzscheme\.vim\|netbeans.*\|paste\.vim\|popupwin.*\|python2\.vim\|pyx2\.vim\|restricted\.vim\|shortpathname\.vim\|sound\.vim\|tcl\.vim\|terminal.*\|xxd\.vim'
  195. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/testdir/\<test_\%('"${na_src_testdir}"'\)\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  196. # Remove N/A src/*.[ch] files: sound.c, version.c
  197. local na_src_c='sound\|version'
  198. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/\<\%('"${na_src_c}"'\)\.[ch]\>@exe "norm! d/\\v(^diff)|%$\r"' +w +q "$file"
  199. # Remove some *.po files. #5622
  200. local na_po='sjiscorr\.c\|ja\.sjis\.po\|ko\.po\|pl\.cp1250\.po\|pl\.po\|ru\.cp1251\.po\|uk\.cp1251\.po\|zh_CN\.cp936\.po\|zh_CN\.po\|zh_TW\.po'
  201. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/po/\<\%('${na_po}'\)\>@exe "norm! d/\\v(^diff)|%$\r"+' +w +q "$file"
  202. # Remove vimrc_example.vim
  203. local na_vimrcexample='vimrc_example\.vim'
  204. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/\<\%('${na_vimrcexample}'\)\>@exe "norm! d/\\v(^diff)|%$\r"+' +w +q "$file"
  205. # Rename src/testdir/ paths to test/old/testdir/
  206. LC_ALL=C sed -Ee 's/( [ab])\/src\/testdir/\1\/test\/old\/testdir/g' \
  207. "$file" > "$file".tmp && mv "$file".tmp "$file"
  208. # Rename src/ paths to src/nvim/
  209. LC_ALL=C sed -Ee 's/( [ab]\/src)/\1\/nvim/g' \
  210. "$file" > "$file".tmp && mv "$file".tmp "$file"
  211. # Rename evalfunc.c to eval/funcs.c
  212. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/evalfunc\.c/\1\/eval\/funcs.c/g' \
  213. "$file" > "$file".tmp && mv "$file".tmp "$file"
  214. # Rename evalvars.c to eval/vars.c
  215. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/evalvars\.c/\1\/eval\/vars.c/g' \
  216. "$file" > "$file".tmp && mv "$file".tmp "$file"
  217. # Rename userfunc.c to eval/userfunc.c
  218. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/userfunc\.c/\1\/eval\/userfunc.c/g' \
  219. "$file" > "$file".tmp && mv "$file".tmp "$file"
  220. # Rename evalbuffer.c to eval/buffer.c
  221. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/evalbuffer\.c/\1\/eval\/buffer.c/g' \
  222. "$file" > "$file".tmp && mv "$file".tmp "$file"
  223. # Rename evalwindow.c to eval/window.c
  224. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/evalwindow\.c/\1\/eval\/window.c/g' \
  225. "$file" > "$file".tmp && mv "$file".tmp "$file"
  226. # Rename map.c to mapping.c
  227. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/map\.c/\1\/mapping.c/g' \
  228. "$file" > "$file".tmp && mv "$file".tmp "$file"
  229. # Rename profiler.c to profile.c
  230. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/profiler\.c/\1\/profile.c/g' \
  231. "$file" > "$file".tmp && mv "$file".tmp "$file"
  232. # Rename scriptfile.c to runtime.c
  233. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/scriptfile\.c/\1\/runtime.c/g' \
  234. "$file" > "$file".tmp && mv "$file".tmp "$file"
  235. # Rename session.c to ex_session.c
  236. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/session\.c/\1\/ex_session.c/g' \
  237. "$file" > "$file".tmp && mv "$file".tmp "$file"
  238. # Rename highlight.c to highlight_group.c
  239. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/highlight\.c/\1\/highlight_group.c/g' \
  240. "$file" > "$file".tmp && mv "$file".tmp "$file"
  241. # Rename locale.c to os/lang.c
  242. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/locale\.c/\1\/os\/lang.c/g' \
  243. "$file" > "$file".tmp && mv "$file".tmp "$file"
  244. # Rename keymap.h to keycodes.h
  245. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/keymap\.h/\1\/keycodes.h/g' \
  246. "$file" > "$file".tmp && mv "$file".tmp "$file"
  247. # Rename option.h to option_vars.h
  248. LC_ALL=C sed -Ee 's/( [ab]\/src\/nvim)\/option\.h/\1\/option_vars.h/g' \
  249. "$file" > "$file".tmp && mv "$file".tmp "$file"
  250. # Rename runtime/doc/eval.txt to runtime/doc/vimeval.txt
  251. LC_ALL=C sed -Ee 's/( [ab]\/runtime\/doc)\/eval\.txt/\1\/vimeval.txt/g' \
  252. "$file" > "$file".tmp && mv "$file".tmp "$file"
  253. # Rename version*.txt to news.txt
  254. LC_ALL=C sed -Ee 's/( [ab]\/runtime\/doc)\/version[0-9]+\.txt/\1\/news.txt/g' \
  255. "$file" > "$file".tmp && mv "$file".tmp "$file"
  256. # Rename sponsor.txt to intro.txt
  257. LC_ALL=C sed -Ee 's/( [ab]\/runtime\/doc)\/sponsor\.txt/\1\/intro.txt/g' \
  258. "$file" > "$file".tmp && mv "$file".tmp "$file"
  259. # Rename test_urls.vim to check_urls.vim
  260. LC_ALL=C sed -Ee 's/( [ab])\/runtime\/doc\/test(_urls\.vim)/\1\/scripts\/check\2/g' \
  261. "$file" > "$file".tmp && mv "$file".tmp "$file"
  262. # Rename path to check_colors.vim
  263. LC_ALL=C sed -Ee 's/( [ab]\/runtime)\/colors\/(tools\/check_colors\.vim)/\1\/\2/g' \
  264. "$file" > "$file".tmp && mv "$file".tmp "$file"
  265. }
  266. uncrustify_patch() {
  267. git diff --quiet || {
  268. >&2 echo 'Vim source working tree dirty, aborting.'
  269. exit 1
  270. }
  271. local patch_path="$NVIM_SOURCE_DIR"/build/vim_patch
  272. rm -rf "$patch_path"
  273. mkdir -p "$patch_path"/{a,b}
  274. local commit="$1"
  275. for file in $(git diff-tree --name-only --no-commit-id -r --diff-filter=a "$commit"); do
  276. git --work-tree="$patch_path"/a checkout --quiet "$commit"~ -- "$file"
  277. done
  278. for file in $(git diff-tree --name-only --no-commit-id -r --diff-filter=d "$commit"); do
  279. git --work-tree="$patch_path"/b checkout --quiet "$commit" -- "$file"
  280. done
  281. git reset --quiet --hard HEAD
  282. # If the difference are drastic enough uncrustify may need to be used more
  283. # than once. This is obviously a bug that needs to be fixed on uncrustify's
  284. # end, but in the meantime this workaround is sufficient.
  285. for _ in {1..2}; do
  286. "$NVIM_SOURCE_DIR"/build/usr/bin/uncrustify -c "$NVIM_SOURCE_DIR"/src/uncrustify.cfg -q --replace --no-backup "$patch_path"/{a,b}/src/*.[ch]
  287. done
  288. (cd "$patch_path" && (git --no-pager diff --no-index --no-prefix --patch --unified=5 --color=never a/ b/ || true))
  289. }
  290. get_vimpatch() {
  291. get_vim_sources
  292. assign_commit_details "${1}"
  293. msg_ok "Found Vim revision '${vim_commit}'."
  294. local patch_content
  295. if check_executable "$NVIM_SOURCE_DIR"/build/usr/bin/uncrustify; then
  296. patch_content="$(uncrustify_patch "${vim_commit}")"
  297. else
  298. patch_content="$(git --no-pager show --unified=5 --color=never -1 --pretty=medium "${vim_commit}")"
  299. fi
  300. cd "${NVIM_SOURCE_DIR}"
  301. printf "Creating patch...\n"
  302. echo "$patch_content" > "${NVIM_SOURCE_DIR}/${patch_file}"
  303. printf "Pre-processing patch...\n"
  304. preprocess_patch "${NVIM_SOURCE_DIR}/${patch_file}"
  305. msg_ok "Saved patch to '${NVIM_SOURCE_DIR}/${patch_file}'."
  306. }
  307. stage_patch() {
  308. get_vimpatch "$1"
  309. local try_apply="${2:-}"
  310. local nvim_remote
  311. nvim_remote="$(find_git_remote)"
  312. local checked_out_branch
  313. checked_out_branch="$(git rev-parse --abbrev-ref HEAD)"
  314. if [[ "${checked_out_branch}" == ${BRANCH_PREFIX}* ]]; then
  315. msg_ok "Current branch '${checked_out_branch}' seems to be a vim-patch"
  316. echo " branch; not creating a new branch."
  317. else
  318. printf '\nFetching "%s/master".\n' "${nvim_remote}"
  319. if output="$(git fetch "$nvim_remote" master 2>&1)"; then
  320. msg_ok "$output"
  321. else
  322. msg_err "$output"
  323. exit 1
  324. fi
  325. local nvim_branch="${BRANCH_PREFIX}${vim_version}"
  326. echo
  327. echo "Creating new branch '${nvim_branch}' based on '${nvim_remote}/master'."
  328. cd "${NVIM_SOURCE_DIR}"
  329. if output="$(git checkout -b "$nvim_branch" "$nvim_remote/master" 2>&1)"; then
  330. msg_ok "$output"
  331. else
  332. msg_err "$output"
  333. exit 1
  334. fi
  335. fi
  336. printf "\nCreating empty commit with correct commit message.\n"
  337. if output="$(commit_message | git commit --allow-empty --file 2>&1 -)"; then
  338. msg_ok "$output"
  339. else
  340. msg_err "$output"
  341. exit 1
  342. fi
  343. local ret=0
  344. if test -n "$try_apply" ; then
  345. if ! check_executable patch; then
  346. printf "\n"
  347. msg_err "'patch' command not found\n"
  348. else
  349. printf "\nApplying patch...\n"
  350. patch -p1 --fuzz=1 --no-backup-if-mismatch < "${patch_file}" || ret=$?
  351. fi
  352. printf "\nInstructions:\n Proceed to port the patch.\n"
  353. else
  354. printf '\nInstructions:\n Proceed to port the patch.\n Try the "patch" command (or use "%s -P ..." next time):\n patch -p1 < %s\n' "${BASENAME}" "${patch_file}"
  355. fi
  356. printf '
  357. Stage your changes ("git add ..."), then use "git commit --amend" to commit.
  358. To port more patches (if any) related to %s,
  359. run "%s" again.
  360. * Do this only for _related_ patches (otherwise it increases the
  361. size of the pull request, making it harder to review)
  362. When you are done, try "%s -s" to create the pull request,
  363. or "%s -s --draft" to create a draft pull request.
  364. See the wiki for more information:
  365. * https://neovim.io/doc/user/dev_vimpatch.html
  366. ' "${vim_version}" "${BASENAME}" "${BASENAME}" "${BASENAME}"
  367. return $ret
  368. }
  369. gh_pr() {
  370. local pr_title
  371. local pr_body
  372. pr_title="$1"
  373. pr_body="$2"
  374. shift 2
  375. gh pr create --title "${pr_title}" --body "${pr_body}" "$@"
  376. }
  377. git_hub_pr() {
  378. local pr_message
  379. pr_message="$(printf '%s\n\n%s\n' "$1" "$2")"
  380. shift 2
  381. git hub pull new -m "${pr_message}" "$@"
  382. }
  383. submit_pr() {
  384. require_executable git
  385. local push_first
  386. push_first=1
  387. local submit_fn
  388. if check_executable gh; then
  389. submit_fn="gh_pr"
  390. elif check_executable git-hub; then
  391. push_first=0
  392. submit_fn="git_hub_pr"
  393. else
  394. >&2 echo "${BASENAME}: 'gh' or 'git-hub' not found in PATH or not executable."
  395. >&2 echo " Get it here: https://cli.github.com/"
  396. exit 1
  397. fi
  398. cd "${NVIM_SOURCE_DIR}"
  399. local checked_out_branch
  400. checked_out_branch="$(git rev-parse --abbrev-ref HEAD)"
  401. if [[ "${checked_out_branch}" != ${BRANCH_PREFIX}* ]]; then
  402. msg_err "Current branch '${checked_out_branch}' doesn't seem to be a vim-patch branch."
  403. exit 1
  404. fi
  405. local nvim_remote
  406. nvim_remote="$(find_git_remote)"
  407. local pr_body
  408. pr_body="$(git log --grep=vim-patch --reverse --format='#### %s%n%n%b%n' "${nvim_remote}"/master..HEAD)"
  409. local patches
  410. # Extract just the "vim-patch:X.Y.ZZZZ" or "vim-patch:sha" portion of each log
  411. patches=("$(git log --grep=vim-patch --reverse --format='%s' "${nvim_remote}"/master..HEAD | sed 's/: .*//')")
  412. # shellcheck disable=SC2206
  413. patches=(${patches[@]//vim-patch:}) # Remove 'vim-patch:' prefix for each item in array.
  414. local pr_title="${patches[*]}" # Create space-separated string from array.
  415. pr_title="${pr_title// /,}" # Replace spaces with commas.
  416. pr_title="$(printf 'vim-patch:%s' "${pr_title#,}")"
  417. if [[ $push_first -ne 0 ]]; then
  418. local push_remote
  419. push_remote="$(git config --get branch."${checked_out_branch}".pushRemote || true)"
  420. if [[ -z "$push_remote" ]]; then
  421. push_remote="$(git config --get remote.pushDefault || true)"
  422. if [[ -z "$push_remote" ]]; then
  423. push_remote="$(git config --get branch."${checked_out_branch}".remote || true)"
  424. if [[ -z "$push_remote" ]] || [[ "$push_remote" == "$nvim_remote" ]]; then
  425. push_remote="$(find_git_remote fork)"
  426. fi
  427. fi
  428. fi
  429. echo "Pushing to '${push_remote}/${checked_out_branch}'."
  430. if output="$(git push "$push_remote" "$checked_out_branch" 2>&1)"; then
  431. msg_ok "$output"
  432. else
  433. msg_err "$output"
  434. exit 1
  435. fi
  436. echo
  437. fi
  438. echo "Creating pull request."
  439. if output="$($submit_fn "$pr_title" "$pr_body" "$@" 2>&1)"; then
  440. msg_ok "$output"
  441. else
  442. msg_err "$output"
  443. exit 1
  444. fi
  445. echo
  446. echo "Cleaning up files."
  447. local patch_file
  448. for patch_file in "${patches[@]}"; do
  449. patch_file="vim-${patch_file}.patch"
  450. if [[ ! -f "${NVIM_SOURCE_DIR}/${patch_file}" ]]; then
  451. continue
  452. fi
  453. rm -- "${NVIM_SOURCE_DIR}/${patch_file}"
  454. msg_ok "Removed '${NVIM_SOURCE_DIR}/${patch_file}'."
  455. done
  456. }
  457. # Gets all Vim commits since the "start" commit.
  458. list_vim_commits() { (
  459. cd "${VIM_SOURCE_DIR}" && git log --reverse v8.1.0000..HEAD "$@"
  460. ) }
  461. # Prints all (sorted) "vim-patch:xxx" tokens found in the Nvim git log.
  462. list_vimpatch_tokens() {
  463. # Use sed…{7,7} to normalize (internal) Git hashes (for tokens caches).
  464. git -C "${NVIM_SOURCE_DIR}" log -E --grep='vim-patch:[^ ,{]{7,}' \
  465. | grep -oE 'vim-patch:[^ ,{:]{7,}' \
  466. | sort \
  467. | uniq \
  468. | sed -nEe 's/^(vim-patch:([0-9]+\.[^ ]+|[0-9a-z]{7,7})).*/\1/p'
  469. }
  470. # Prints all patch-numbers (for the current v:version) for which there is
  471. # a "vim-patch:xxx" token in the Nvim git log.
  472. list_vimpatch_numbers() {
  473. # Transform "vim-patch:X.Y.ZZZZ" to "ZZZZ".
  474. list_vimpatch_tokens | while read -r vimpatch_token; do
  475. echo "$vimpatch_token" | grep -F '8.1.' | sed -Ee 's/.*vim-patch:8\.1\.([0-9a-z]+).*/\1/'
  476. done
  477. }
  478. declare -A tokens
  479. declare -A vim_commit_tags
  480. _set_tokens_and_tags() {
  481. set +u # Avoid "unbound variable" with bash < 4.4 below.
  482. if [[ -n "${tokens[*]}" ]]; then
  483. return
  484. fi
  485. set -u
  486. # Find all "vim-patch:xxx" tokens in the Nvim git log.
  487. for token in $(list_vimpatch_tokens); do
  488. tokens[$token]=1
  489. done
  490. # Create an associative array mapping Vim commits to tags.
  491. eval "vim_commit_tags=(
  492. $(git -C "${VIM_SOURCE_DIR}" show-ref --tags --dereference \
  493. | sed -nEe 's/^([0-9a-f]+) refs\/tags\/(v[0-9.]+)(\^\{\})?$/["\1"]="\2"/p')
  494. )"
  495. # Exit in case of errors from the above eval (empty vim_commit_tags).
  496. if ! (( "${#vim_commit_tags[@]}" )); then
  497. msg_err "Could not get Vim commits/tags."
  498. exit 1
  499. fi
  500. }
  501. # Prints a newline-delimited list of Vim commits, for use by scripts.
  502. # "$1": use extended format? (with subject)
  503. # "$@" is passed to list_vim_commits, as extra arguments to git-log.
  504. list_missing_vimpatches() {
  505. local -a missing_vim_patches=()
  506. _set_missing_vimpatches "$@"
  507. set +u # Avoid "unbound variable" with bash < 4.4 below.
  508. for line in "${missing_vim_patches[@]}"; do
  509. printf '%s\n' "$line"
  510. done
  511. set -u
  512. }
  513. # Sets / appends to missing_vim_patches (useful to avoid a subshell when
  514. # used multiple times to cache tokens/vim_commit_tags).
  515. # "$1": use extended format? (with subject)
  516. # "$@": extra arguments to git-log.
  517. _set_missing_vimpatches() {
  518. local token vim_commit vim_tag patch_number
  519. declare -a git_log_args
  520. local extended_format=$1; shift
  521. if [[ "$extended_format" == 1 ]]; then
  522. git_log_args=("--format=%H %s")
  523. else
  524. git_log_args=("--format=%H")
  525. fi
  526. # Massage arguments for git-log.
  527. declare -A git_log_replacements=(
  528. [^\(.*/\)?src/nvim/\(.*\)]="\${BASH_REMATCH[1]}src/\${BASH_REMATCH[2]}"
  529. [^\(.*/\)?test/old/\(.*\)]="\${BASH_REMATCH[1]}src/\${BASH_REMATCH[2]}"
  530. [^\(.*/\)?\.vim-src/\(.*\)]="\${BASH_REMATCH[2]}"
  531. )
  532. local i j
  533. for i in "$@"; do
  534. for j in "${!git_log_replacements[@]}"; do
  535. if [[ "$i" =~ $j ]]; then
  536. eval "git_log_args+=(${git_log_replacements[$j]})"
  537. continue 2
  538. fi
  539. done
  540. git_log_args+=("$i")
  541. done
  542. _set_tokens_and_tags
  543. # Get missing Vim commits
  544. set +u # Avoid "unbound variable" with bash < 4.4 below.
  545. local vim_commit info
  546. while IFS=' ' read -r line; do
  547. # Check for vim-patch:<commit_hash> (usually runtime updates).
  548. token="vim-patch:${line:0:7}"
  549. if [[ "${tokens[$token]-}" ]]; then
  550. continue
  551. fi
  552. # Get commit hash, and optional info from line. This is used in
  553. # extended mode, and when using e.g. '--format' manually.
  554. vim_commit=${line%% *}
  555. if [[ "$vim_commit" == "$line" ]]; then
  556. info=
  557. else
  558. info=${line#* }
  559. if [[ -n $info ]]; then
  560. # Remove any "patch 8.1.0902: " prefixes, and prefix with ": ".
  561. info=": ${info#patch*: }"
  562. fi
  563. fi
  564. vim_tag="${vim_commit_tags[$vim_commit]-}"
  565. if [[ -n "$vim_tag" ]]; then
  566. # Check for vim-patch:<tag> (not commit hash).
  567. patch_number="vim-patch:${vim_tag:1}" # "v7.4.0001" => "7.4.0001"
  568. if [[ "${tokens[$patch_number]-}" ]]; then
  569. continue
  570. fi
  571. missing_vim_patches+=("$vim_tag$info")
  572. else
  573. missing_vim_patches+=("$vim_commit$info")
  574. fi
  575. done < <(list_vim_commits "${git_log_args[@]}")
  576. set -u
  577. }
  578. # Prints a human-formatted list of Vim commits, with instructional messages.
  579. # Passes "$@" onto list_missing_vimpatches (args for git-log).
  580. show_vimpatches() {
  581. get_vim_sources update
  582. printf "Vim patches missing from Neovim:\n"
  583. local -A runtime_commits
  584. for commit in $(git -C "${VIM_SOURCE_DIR}" log --format="%H %D" -- runtime | sed -Ee 's/,\? tag: / /g'); do
  585. runtime_commits[$commit]=1
  586. done
  587. list_missing_vimpatches 1 "$@" | while read -r vim_commit; do
  588. if [[ "${runtime_commits[$vim_commit]-}" ]]; then
  589. printf ' • %s (+runtime)\n' "${vim_commit}"
  590. else
  591. printf ' • %s\n' "${vim_commit}"
  592. fi
  593. done
  594. cat << EOF
  595. Instructions:
  596. To port one of the above patches to Neovim, execute this script with the patch revision as argument and follow the instructions, e.g.
  597. '${BASENAME} -p v8.1.1234', or '${BASENAME} -P v8.1.1234'
  598. NOTE: Please port the _oldest_ patch if you possibly can.
  599. You can use '${BASENAME} -l path/to/file' to see what patches are missing for a file.
  600. EOF
  601. }
  602. list_missing_previous_vimpatches_for_patch() {
  603. local for_vim_patch="${1}"
  604. local vim_commit vim_tag
  605. assign_commit_details "${for_vim_patch}"
  606. local file
  607. local -a missing_list
  608. local -a fnames
  609. while IFS= read -r line ; do
  610. fnames+=("$line")
  611. done < <(git -C "${VIM_SOURCE_DIR}" diff-tree --no-commit-id --name-only -r "${vim_commit}" -- . ':!src/version.c')
  612. local i=0
  613. local n=${#fnames[@]}
  614. printf '=== getting missing patches for %d files ===\n' "$n"
  615. if [[ -z "${vim_tag}" ]]; then
  616. printf 'NOTE: "%s" is not a Vim tag - listing all oldest missing patches\n' "${for_vim_patch}" >&2
  617. fi
  618. for fname in "${fnames[@]}"; do
  619. i=$(( i+1 ))
  620. printf '[%.*d/%d] %s: ' "${#n}" "$i" "$n" "$fname"
  621. local -a missing_vim_patches=()
  622. _set_missing_vimpatches 1 -- "${fname}"
  623. set +u # Avoid "unbound variable" with bash < 4.4 below.
  624. for missing_vim_commit_info in "${missing_vim_patches[@]}"; do
  625. if [[ -z "${missing_vim_commit_info}" ]]; then
  626. printf -- "-\r"
  627. else
  628. printf -- "-\r"
  629. local missing_vim_commit="${missing_vim_commit_info%%:*}"
  630. if [[ -z "${vim_tag}" ]] || [[ "${missing_vim_commit}" < "${vim_tag}" ]]; then
  631. printf -- "%s\n" "$missing_vim_commit_info"
  632. missing_list+=("$missing_vim_commit_info")
  633. else
  634. printf -- "-\r"
  635. fi
  636. fi
  637. done
  638. set -u
  639. done
  640. set +u # Avoid "unbound variable" with bash < 4.4 below.
  641. if [[ -z "${missing_list[*]}" ]]; then
  642. msg_ok 'no missing previous Vim patches'
  643. set -u
  644. return 0
  645. fi
  646. set -u
  647. local -a missing_unique
  648. local stat
  649. while IFS= read -r line; do
  650. local commit="${line%%:*}"
  651. stat="$(git -C "${VIM_SOURCE_DIR}" show --format= --shortstat "${commit}")"
  652. missing_unique+=("$(printf '%s\n %s' "$line" "$stat")")
  653. done < <(printf '%s\n' "${missing_list[@]}" | sort -u)
  654. msg_err "$(printf '%d missing previous Vim patches:' ${#missing_unique[@]})"
  655. printf ' - %s\n' "${missing_unique[@]}"
  656. return 1
  657. }
  658. review_commit() {
  659. local nvim_commit_url="${1}"
  660. local nvim_patch_url="${nvim_commit_url}.patch"
  661. local git_patch_prefix='Subject: \[PATCH\] '
  662. local nvim_patch
  663. nvim_patch="$(curl -Ssf "${nvim_patch_url}")"
  664. local vim_version
  665. vim_version="$(head -n 4 <<< "${nvim_patch}" | sed -nEe 's/'"${git_patch_prefix}"'vim-patch:([a-z0-9.]*)(:.*){0,1}$/\1/p')"
  666. echo
  667. if [[ -n "${vim_version}" ]]; then
  668. msg_ok "Detected Vim patch '${vim_version}'."
  669. else
  670. msg_err "Could not detect the Vim patch number."
  671. echo " This script assumes that the PR contains only commits"
  672. echo " with 'vim-patch:XXX' in their title."
  673. echo
  674. printf -- '%s\n\n' "$(head -n 4 <<< "${nvim_patch}")"
  675. local reply
  676. read -p "Continue reviewing (y/N)? " -n 1 -r reply
  677. if [[ "${reply}" == y ]]; then
  678. echo
  679. return
  680. fi
  681. exit 1
  682. fi
  683. assign_commit_details "${vim_version}"
  684. echo
  685. echo "Creating files."
  686. echo "${nvim_patch}" > "${NVIM_SOURCE_DIR}/n${patch_file}"
  687. msg_ok "Saved pull request diff to '${NVIM_SOURCE_DIR}/n${patch_file}'."
  688. CREATED_FILES+=("${NVIM_SOURCE_DIR}/n${patch_file}")
  689. local nvim="nvim -u NONE -n -i NONE --headless"
  690. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'1,/^$/g/^ /-1join' +w +q "${NVIM_SOURCE_DIR}/n${patch_file}"
  691. local expected_commit_message
  692. expected_commit_message="$(commit_message)"
  693. local message_length
  694. message_length="$(wc -l <<< "${expected_commit_message}")"
  695. local commit_message
  696. commit_message="$(tail -n +4 "${NVIM_SOURCE_DIR}/n${patch_file}" | head -n "${message_length}")"
  697. if [[ "${commit_message#"$git_patch_prefix"}" == "${expected_commit_message}" ]]; then
  698. msg_ok "Found expected commit message."
  699. else
  700. msg_err "Wrong commit message."
  701. echo " Expected:"
  702. echo "${expected_commit_message}"
  703. echo " Actual:"
  704. echo "${commit_message#"$git_patch_prefix"}"
  705. fi
  706. get_vimpatch "${vim_version}"
  707. CREATED_FILES+=("${NVIM_SOURCE_DIR}/${patch_file}")
  708. echo
  709. echo "Launching nvim."
  710. nvim -c "cd ${NVIM_SOURCE_DIR}" \
  711. -O "${NVIM_SOURCE_DIR}/${patch_file}" "${NVIM_SOURCE_DIR}/n${patch_file}"
  712. }
  713. review_pr() {
  714. require_executable curl
  715. require_executable nvim
  716. require_executable jq
  717. get_vim_sources
  718. local pr="${1}"
  719. echo
  720. echo "Downloading data for pull request #${pr}."
  721. local -a pr_commit_urls
  722. while IFS= read -r pr_commit_url; do
  723. pr_commit_urls+=("$pr_commit_url")
  724. done < <(curl -Ssf "https://api.github.com/repos/neovim/neovim/pulls/${pr}/commits" \
  725. | jq -r '.[].html_url')
  726. echo "Found ${#pr_commit_urls[@]} commit(s)."
  727. local pr_commit_url
  728. local reply
  729. for pr_commit_url in "${pr_commit_urls[@]}"; do
  730. review_commit "${pr_commit_url}"
  731. if [[ "${pr_commit_url}" != "${pr_commit_urls[-1]}" ]]; then
  732. read -p "Continue with next commit (Y/n)? " -n 1 -r reply
  733. echo
  734. if [[ "${reply}" == n ]]; then
  735. break
  736. fi
  737. fi
  738. done
  739. clean_files
  740. }
  741. while getopts "hlLmMVp:P:g:r:s" opt; do
  742. case ${opt} in
  743. h)
  744. usage
  745. exit 0
  746. ;;
  747. l)
  748. shift # remove opt
  749. show_vimpatches "$@"
  750. exit 0
  751. ;;
  752. L)
  753. shift # remove opt
  754. list_missing_vimpatches 0 "$@"
  755. exit 0
  756. ;;
  757. M)
  758. list_vimpatch_numbers
  759. exit 0
  760. ;;
  761. m)
  762. shift # remove opt
  763. list_missing_previous_vimpatches_for_patch "$@"
  764. exit 0
  765. ;;
  766. p)
  767. stage_patch "${OPTARG}"
  768. exit
  769. ;;
  770. P)
  771. stage_patch "${OPTARG}" TRY_APPLY
  772. exit 0
  773. ;;
  774. g)
  775. get_vimpatch "${OPTARG}"
  776. exit 0
  777. ;;
  778. r)
  779. review_pr "${OPTARG}"
  780. exit 0
  781. ;;
  782. s)
  783. shift # remove opt
  784. submit_pr "$@"
  785. exit 0
  786. ;;
  787. V)
  788. get_vim_sources update
  789. exit 0
  790. ;;
  791. *)
  792. exit 1
  793. ;;
  794. esac
  795. done
  796. usage
  797. # vim: et sw=2