vim-patch.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 version*.txt to news.txt
  251. LC_ALL=C sed -Ee 's/( [ab]\/runtime\/doc)\/version[0-9]+\.txt/\1\/news.txt/g' \
  252. "$file" > "$file".tmp && mv "$file".tmp "$file"
  253. # Rename sponsor.txt to intro.txt
  254. LC_ALL=C sed -Ee 's/( [ab]\/runtime\/doc)\/sponsor\.txt/\1\/intro.txt/g' \
  255. "$file" > "$file".tmp && mv "$file".tmp "$file"
  256. # Rename test_urls.vim to check_urls.vim
  257. LC_ALL=C sed -Ee 's/( [ab])\/runtime\/doc\/test(_urls\.vim)/\1\/scripts\/check\2/g' \
  258. "$file" > "$file".tmp && mv "$file".tmp "$file"
  259. # Rename path to check_colors.vim
  260. LC_ALL=C sed -Ee 's/( [ab]\/runtime)\/colors\/(tools\/check_colors\.vim)/\1\/\2/g' \
  261. "$file" > "$file".tmp && mv "$file".tmp "$file"
  262. }
  263. uncrustify_patch() {
  264. git diff --quiet || {
  265. >&2 echo 'Vim source working tree dirty, aborting.'
  266. exit 1
  267. }
  268. local patch_path="$NVIM_SOURCE_DIR"/build/vim_patch
  269. rm -rf "$patch_path"
  270. mkdir -p "$patch_path"/{a,b}
  271. local commit="$1"
  272. for file in $(git diff-tree --name-only --no-commit-id -r --diff-filter=a "$commit"); do
  273. git --work-tree="$patch_path"/a checkout --quiet "$commit"~ -- "$file"
  274. done
  275. for file in $(git diff-tree --name-only --no-commit-id -r --diff-filter=d "$commit"); do
  276. git --work-tree="$patch_path"/b checkout --quiet "$commit" -- "$file"
  277. done
  278. git reset --quiet --hard HEAD
  279. # If the difference are drastic enough uncrustify may need to be used more
  280. # than once. This is obviously a bug that needs to be fixed on uncrustify's
  281. # end, but in the meantime this workaround is sufficient.
  282. for _ in {1..2}; do
  283. "$NVIM_SOURCE_DIR"/build/usr/bin/uncrustify -c "$NVIM_SOURCE_DIR"/src/uncrustify.cfg -q --replace --no-backup "$patch_path"/{a,b}/src/*.[ch]
  284. done
  285. (cd "$patch_path" && (git --no-pager diff --no-index --no-prefix --patch --unified=5 --color=never a/ b/ || true))
  286. }
  287. get_vimpatch() {
  288. get_vim_sources
  289. assign_commit_details "${1}"
  290. msg_ok "Found Vim revision '${vim_commit}'."
  291. local patch_content
  292. if check_executable "$NVIM_SOURCE_DIR"/build/usr/bin/uncrustify; then
  293. patch_content="$(uncrustify_patch "${vim_commit}")"
  294. else
  295. patch_content="$(git --no-pager show --unified=5 --color=never -1 --pretty=medium "${vim_commit}")"
  296. fi
  297. cd "${NVIM_SOURCE_DIR}"
  298. printf "Creating patch...\n"
  299. echo "$patch_content" > "${NVIM_SOURCE_DIR}/${patch_file}"
  300. printf "Pre-processing patch...\n"
  301. preprocess_patch "${NVIM_SOURCE_DIR}/${patch_file}"
  302. msg_ok "Saved patch to '${NVIM_SOURCE_DIR}/${patch_file}'."
  303. }
  304. stage_patch() {
  305. get_vimpatch "$1"
  306. local try_apply="${2:-}"
  307. local nvim_remote
  308. nvim_remote="$(find_git_remote)"
  309. local checked_out_branch
  310. checked_out_branch="$(git rev-parse --abbrev-ref HEAD)"
  311. if [[ "${checked_out_branch}" == ${BRANCH_PREFIX}* ]]; then
  312. msg_ok "Current branch '${checked_out_branch}' seems to be a vim-patch"
  313. echo " branch; not creating a new branch."
  314. else
  315. printf '\nFetching "%s/master".\n' "${nvim_remote}"
  316. if output="$(git fetch "$nvim_remote" master 2>&1)"; then
  317. msg_ok "$output"
  318. else
  319. msg_err "$output"
  320. exit 1
  321. fi
  322. local nvim_branch="${BRANCH_PREFIX}${vim_version}"
  323. echo
  324. echo "Creating new branch '${nvim_branch}' based on '${nvim_remote}/master'."
  325. cd "${NVIM_SOURCE_DIR}"
  326. if output="$(git checkout -b "$nvim_branch" "$nvim_remote/master" 2>&1)"; then
  327. msg_ok "$output"
  328. else
  329. msg_err "$output"
  330. exit 1
  331. fi
  332. fi
  333. printf "\nCreating empty commit with correct commit message.\n"
  334. if output="$(commit_message | git commit --allow-empty --file 2>&1 -)"; then
  335. msg_ok "$output"
  336. else
  337. msg_err "$output"
  338. exit 1
  339. fi
  340. local ret=0
  341. if test -n "$try_apply" ; then
  342. if ! check_executable patch; then
  343. printf "\n"
  344. msg_err "'patch' command not found\n"
  345. else
  346. printf "\nApplying patch...\n"
  347. patch -p1 --fuzz=1 --no-backup-if-mismatch < "${patch_file}" || ret=$?
  348. fi
  349. printf "\nInstructions:\n Proceed to port the patch.\n"
  350. else
  351. 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}"
  352. fi
  353. printf '
  354. Stage your changes ("git add ..."), then use "git commit --amend" to commit.
  355. To port more patches (if any) related to %s,
  356. run "%s" again.
  357. * Do this only for _related_ patches (otherwise it increases the
  358. size of the pull request, making it harder to review)
  359. When you are done, try "%s -s" to create the pull request,
  360. or "%s -s --draft" to create a draft pull request.
  361. See the wiki for more information:
  362. * https://neovim.io/doc/user/dev_vimpatch.html
  363. ' "${vim_version}" "${BASENAME}" "${BASENAME}" "${BASENAME}"
  364. return $ret
  365. }
  366. gh_pr() {
  367. local pr_title
  368. local pr_body
  369. pr_title="$1"
  370. pr_body="$2"
  371. shift 2
  372. gh pr create --title "${pr_title}" --body "${pr_body}" "$@"
  373. }
  374. git_hub_pr() {
  375. local pr_message
  376. pr_message="$(printf '%s\n\n%s\n' "$1" "$2")"
  377. shift 2
  378. git hub pull new -m "${pr_message}" "$@"
  379. }
  380. submit_pr() {
  381. require_executable git
  382. local push_first
  383. push_first=1
  384. local submit_fn
  385. if check_executable gh; then
  386. submit_fn="gh_pr"
  387. elif check_executable git-hub; then
  388. push_first=0
  389. submit_fn="git_hub_pr"
  390. else
  391. >&2 echo "${BASENAME}: 'gh' or 'git-hub' not found in PATH or not executable."
  392. >&2 echo " Get it here: https://cli.github.com/"
  393. exit 1
  394. fi
  395. cd "${NVIM_SOURCE_DIR}"
  396. local checked_out_branch
  397. checked_out_branch="$(git rev-parse --abbrev-ref HEAD)"
  398. if [[ "${checked_out_branch}" != ${BRANCH_PREFIX}* ]]; then
  399. msg_err "Current branch '${checked_out_branch}' doesn't seem to be a vim-patch branch."
  400. exit 1
  401. fi
  402. local nvim_remote
  403. nvim_remote="$(find_git_remote)"
  404. local pr_body
  405. pr_body="$(git log --grep=vim-patch --reverse --format='#### %s%n%n%b%n' "${nvim_remote}"/master..HEAD)"
  406. local patches
  407. # Extract just the "vim-patch:X.Y.ZZZZ" or "vim-patch:sha" portion of each log
  408. patches=("$(git log --grep=vim-patch --reverse --format='%s' "${nvim_remote}"/master..HEAD | sed 's/: .*//')")
  409. # shellcheck disable=SC2206
  410. patches=(${patches[@]//vim-patch:}) # Remove 'vim-patch:' prefix for each item in array.
  411. local pr_title="${patches[*]}" # Create space-separated string from array.
  412. pr_title="${pr_title// /,}" # Replace spaces with commas.
  413. pr_title="$(printf 'vim-patch:%s' "${pr_title#,}")"
  414. if [[ $push_first -ne 0 ]]; then
  415. local push_remote
  416. push_remote="$(git config --get branch."${checked_out_branch}".pushRemote || true)"
  417. if [[ -z "$push_remote" ]]; then
  418. push_remote="$(git config --get remote.pushDefault || true)"
  419. if [[ -z "$push_remote" ]]; then
  420. push_remote="$(git config --get branch."${checked_out_branch}".remote || true)"
  421. if [[ -z "$push_remote" ]] || [[ "$push_remote" == "$nvim_remote" ]]; then
  422. push_remote="$(find_git_remote fork)"
  423. fi
  424. fi
  425. fi
  426. echo "Pushing to '${push_remote}/${checked_out_branch}'."
  427. if output="$(git push "$push_remote" "$checked_out_branch" 2>&1)"; then
  428. msg_ok "$output"
  429. else
  430. msg_err "$output"
  431. exit 1
  432. fi
  433. echo
  434. fi
  435. echo "Creating pull request."
  436. if output="$($submit_fn "$pr_title" "$pr_body" "$@" 2>&1)"; then
  437. msg_ok "$output"
  438. else
  439. msg_err "$output"
  440. exit 1
  441. fi
  442. echo
  443. echo "Cleaning up files."
  444. local patch_file
  445. for patch_file in "${patches[@]}"; do
  446. patch_file="vim-${patch_file}.patch"
  447. if [[ ! -f "${NVIM_SOURCE_DIR}/${patch_file}" ]]; then
  448. continue
  449. fi
  450. rm -- "${NVIM_SOURCE_DIR}/${patch_file}"
  451. msg_ok "Removed '${NVIM_SOURCE_DIR}/${patch_file}'."
  452. done
  453. }
  454. # Gets all Vim commits since the "start" commit.
  455. list_vim_commits() { (
  456. cd "${VIM_SOURCE_DIR}" && git log --reverse v8.1.0000..HEAD "$@"
  457. ) }
  458. # Prints all (sorted) "vim-patch:xxx" tokens found in the Nvim git log.
  459. list_vimpatch_tokens() {
  460. # Use sed…{7,7} to normalize (internal) Git hashes (for tokens caches).
  461. git -C "${NVIM_SOURCE_DIR}" log -E --grep='vim-patch:[^ ,{]{7,}' \
  462. | grep -oE 'vim-patch:[^ ,{:]{7,}' \
  463. | sort \
  464. | uniq \
  465. | sed -nEe 's/^(vim-patch:([0-9]+\.[^ ]+|[0-9a-z]{7,7})).*/\1/p'
  466. }
  467. # Prints all patch-numbers (for the current v:version) for which there is
  468. # a "vim-patch:xxx" token in the Nvim git log.
  469. list_vimpatch_numbers() {
  470. # Transform "vim-patch:X.Y.ZZZZ" to "ZZZZ".
  471. list_vimpatch_tokens | while read -r vimpatch_token; do
  472. echo "$vimpatch_token" | grep -F '8.1.' | sed -Ee 's/.*vim-patch:8\.1\.([0-9a-z]+).*/\1/'
  473. done
  474. }
  475. declare -A tokens
  476. declare -A vim_commit_tags
  477. _set_tokens_and_tags() {
  478. set +u # Avoid "unbound variable" with bash < 4.4 below.
  479. if [[ -n "${tokens[*]}" ]]; then
  480. return
  481. fi
  482. set -u
  483. # Find all "vim-patch:xxx" tokens in the Nvim git log.
  484. for token in $(list_vimpatch_tokens); do
  485. tokens[$token]=1
  486. done
  487. # Create an associative array mapping Vim commits to tags.
  488. eval "vim_commit_tags=(
  489. $(git -C "${VIM_SOURCE_DIR}" show-ref --tags --dereference \
  490. | sed -nEe 's/^([0-9a-f]+) refs\/tags\/(v[0-9.]+)(\^\{\})?$/["\1"]="\2"/p')
  491. )"
  492. # Exit in case of errors from the above eval (empty vim_commit_tags).
  493. if ! (( "${#vim_commit_tags[@]}" )); then
  494. msg_err "Could not get Vim commits/tags."
  495. exit 1
  496. fi
  497. }
  498. # Prints a newline-delimited list of Vim commits, for use by scripts.
  499. # "$1": use extended format? (with subject)
  500. # "$@" is passed to list_vim_commits, as extra arguments to git-log.
  501. list_missing_vimpatches() {
  502. local -a missing_vim_patches=()
  503. _set_missing_vimpatches "$@"
  504. set +u # Avoid "unbound variable" with bash < 4.4 below.
  505. for line in "${missing_vim_patches[@]}"; do
  506. printf '%s\n' "$line"
  507. done
  508. set -u
  509. }
  510. # Sets / appends to missing_vim_patches (useful to avoid a subshell when
  511. # used multiple times to cache tokens/vim_commit_tags).
  512. # "$1": use extended format? (with subject)
  513. # "$@": extra arguments to git-log.
  514. _set_missing_vimpatches() {
  515. local token vim_commit vim_tag patch_number
  516. declare -a git_log_args
  517. local extended_format=$1; shift
  518. if [[ "$extended_format" == 1 ]]; then
  519. git_log_args=("--format=%H %s")
  520. else
  521. git_log_args=("--format=%H")
  522. fi
  523. # Massage arguments for git-log.
  524. declare -A git_log_replacements=(
  525. [^\(.*/\)?src/nvim/\(.*\)]="\${BASH_REMATCH[1]}src/\${BASH_REMATCH[2]}"
  526. [^\(.*/\)?test/old/\(.*\)]="\${BASH_REMATCH[1]}src/\${BASH_REMATCH[2]}"
  527. [^\(.*/\)?\.vim-src/\(.*\)]="\${BASH_REMATCH[2]}"
  528. )
  529. local i j
  530. for i in "$@"; do
  531. for j in "${!git_log_replacements[@]}"; do
  532. if [[ "$i" =~ $j ]]; then
  533. eval "git_log_args+=(${git_log_replacements[$j]})"
  534. continue 2
  535. fi
  536. done
  537. git_log_args+=("$i")
  538. done
  539. _set_tokens_and_tags
  540. # Get missing Vim commits
  541. set +u # Avoid "unbound variable" with bash < 4.4 below.
  542. local vim_commit info
  543. while IFS=' ' read -r line; do
  544. # Check for vim-patch:<commit_hash> (usually runtime updates).
  545. token="vim-patch:${line:0:7}"
  546. if [[ "${tokens[$token]-}" ]]; then
  547. continue
  548. fi
  549. # Get commit hash, and optional info from line. This is used in
  550. # extended mode, and when using e.g. '--format' manually.
  551. vim_commit=${line%% *}
  552. if [[ "$vim_commit" == "$line" ]]; then
  553. info=
  554. else
  555. info=${line#* }
  556. if [[ -n $info ]]; then
  557. # Remove any "patch 8.1.0902: " prefixes, and prefix with ": ".
  558. info=": ${info#patch*: }"
  559. fi
  560. fi
  561. vim_tag="${vim_commit_tags[$vim_commit]-}"
  562. if [[ -n "$vim_tag" ]]; then
  563. # Check for vim-patch:<tag> (not commit hash).
  564. patch_number="vim-patch:${vim_tag:1}" # "v7.4.0001" => "7.4.0001"
  565. if [[ "${tokens[$patch_number]-}" ]]; then
  566. continue
  567. fi
  568. missing_vim_patches+=("$vim_tag$info")
  569. else
  570. missing_vim_patches+=("$vim_commit$info")
  571. fi
  572. done < <(list_vim_commits "${git_log_args[@]}")
  573. set -u
  574. }
  575. # Prints a human-formatted list of Vim commits, with instructional messages.
  576. # Passes "$@" onto list_missing_vimpatches (args for git-log).
  577. show_vimpatches() {
  578. get_vim_sources update
  579. printf "Vim patches missing from Neovim:\n"
  580. local -A runtime_commits
  581. for commit in $(git -C "${VIM_SOURCE_DIR}" log --format="%H %D" -- runtime | sed -Ee 's/,\? tag: / /g'); do
  582. runtime_commits[$commit]=1
  583. done
  584. list_missing_vimpatches 1 "$@" | while read -r vim_commit; do
  585. if [[ "${runtime_commits[$vim_commit]-}" ]]; then
  586. printf ' • %s (+runtime)\n' "${vim_commit}"
  587. else
  588. printf ' • %s\n' "${vim_commit}"
  589. fi
  590. done
  591. cat << EOF
  592. Instructions:
  593. To port one of the above patches to Neovim, execute this script with the patch revision as argument and follow the instructions, e.g.
  594. '${BASENAME} -p v8.1.1234', or '${BASENAME} -P v8.1.1234'
  595. NOTE: Please port the _oldest_ patch if you possibly can.
  596. You can use '${BASENAME} -l path/to/file' to see what patches are missing for a file.
  597. EOF
  598. }
  599. list_missing_previous_vimpatches_for_patch() {
  600. local for_vim_patch="${1}"
  601. local vim_commit vim_tag
  602. assign_commit_details "${for_vim_patch}"
  603. local file
  604. local -a missing_list
  605. local -a fnames
  606. while IFS= read -r line ; do
  607. fnames+=("$line")
  608. done < <(git -C "${VIM_SOURCE_DIR}" diff-tree --no-commit-id --name-only -r "${vim_commit}" -- . ':!src/version.c')
  609. local i=0
  610. local n=${#fnames[@]}
  611. printf '=== getting missing patches for %d files ===\n' "$n"
  612. if [[ -z "${vim_tag}" ]]; then
  613. printf 'NOTE: "%s" is not a Vim tag - listing all oldest missing patches\n' "${for_vim_patch}" >&2
  614. fi
  615. for fname in "${fnames[@]}"; do
  616. i=$(( i+1 ))
  617. printf '[%.*d/%d] %s: ' "${#n}" "$i" "$n" "$fname"
  618. local -a missing_vim_patches=()
  619. _set_missing_vimpatches 1 -- "${fname}"
  620. set +u # Avoid "unbound variable" with bash < 4.4 below.
  621. for missing_vim_commit_info in "${missing_vim_patches[@]}"; do
  622. if [[ -z "${missing_vim_commit_info}" ]]; then
  623. printf -- "-\r"
  624. else
  625. printf -- "-\r"
  626. local missing_vim_commit="${missing_vim_commit_info%%:*}"
  627. if [[ -z "${vim_tag}" ]] || [[ "${missing_vim_commit}" < "${vim_tag}" ]]; then
  628. printf -- "%s\n" "$missing_vim_commit_info"
  629. missing_list+=("$missing_vim_commit_info")
  630. else
  631. printf -- "-\r"
  632. fi
  633. fi
  634. done
  635. set -u
  636. done
  637. set +u # Avoid "unbound variable" with bash < 4.4 below.
  638. if [[ -z "${missing_list[*]}" ]]; then
  639. msg_ok 'no missing previous Vim patches'
  640. set -u
  641. return 0
  642. fi
  643. set -u
  644. local -a missing_unique
  645. local stat
  646. while IFS= read -r line; do
  647. local commit="${line%%:*}"
  648. stat="$(git -C "${VIM_SOURCE_DIR}" show --format= --shortstat "${commit}")"
  649. missing_unique+=("$(printf '%s\n %s' "$line" "$stat")")
  650. done < <(printf '%s\n' "${missing_list[@]}" | sort -u)
  651. msg_err "$(printf '%d missing previous Vim patches:' ${#missing_unique[@]})"
  652. printf ' - %s\n' "${missing_unique[@]}"
  653. return 1
  654. }
  655. review_commit() {
  656. local nvim_commit_url="${1}"
  657. local nvim_patch_url="${nvim_commit_url}.patch"
  658. local git_patch_prefix='Subject: \[PATCH\] '
  659. local nvim_patch
  660. nvim_patch="$(curl -Ssf "${nvim_patch_url}")"
  661. local vim_version
  662. vim_version="$(head -n 4 <<< "${nvim_patch}" | sed -nEe 's/'"${git_patch_prefix}"'vim-patch:([a-z0-9.]*)(:.*){0,1}$/\1/p')"
  663. echo
  664. if [[ -n "${vim_version}" ]]; then
  665. msg_ok "Detected Vim patch '${vim_version}'."
  666. else
  667. msg_err "Could not detect the Vim patch number."
  668. echo " This script assumes that the PR contains only commits"
  669. echo " with 'vim-patch:XXX' in their title."
  670. echo
  671. printf -- '%s\n\n' "$(head -n 4 <<< "${nvim_patch}")"
  672. local reply
  673. read -p "Continue reviewing (y/N)? " -n 1 -r reply
  674. if [[ "${reply}" == y ]]; then
  675. echo
  676. return
  677. fi
  678. exit 1
  679. fi
  680. assign_commit_details "${vim_version}"
  681. echo
  682. echo "Creating files."
  683. echo "${nvim_patch}" > "${NVIM_SOURCE_DIR}/n${patch_file}"
  684. msg_ok "Saved pull request diff to '${NVIM_SOURCE_DIR}/n${patch_file}'."
  685. CREATED_FILES+=("${NVIM_SOURCE_DIR}/n${patch_file}")
  686. local nvim="nvim -u NONE -n -i NONE --headless"
  687. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'1,/^$/g/^ /-1join' +w +q "${NVIM_SOURCE_DIR}/n${patch_file}"
  688. local expected_commit_message
  689. expected_commit_message="$(commit_message)"
  690. local message_length
  691. message_length="$(wc -l <<< "${expected_commit_message}")"
  692. local commit_message
  693. commit_message="$(tail -n +4 "${NVIM_SOURCE_DIR}/n${patch_file}" | head -n "${message_length}")"
  694. if [[ "${commit_message#"$git_patch_prefix"}" == "${expected_commit_message}" ]]; then
  695. msg_ok "Found expected commit message."
  696. else
  697. msg_err "Wrong commit message."
  698. echo " Expected:"
  699. echo "${expected_commit_message}"
  700. echo " Actual:"
  701. echo "${commit_message#"$git_patch_prefix"}"
  702. fi
  703. get_vimpatch "${vim_version}"
  704. CREATED_FILES+=("${NVIM_SOURCE_DIR}/${patch_file}")
  705. echo
  706. echo "Launching nvim."
  707. nvim -c "cd ${NVIM_SOURCE_DIR}" \
  708. -O "${NVIM_SOURCE_DIR}/${patch_file}" "${NVIM_SOURCE_DIR}/n${patch_file}"
  709. }
  710. review_pr() {
  711. require_executable curl
  712. require_executable nvim
  713. require_executable jq
  714. get_vim_sources
  715. local pr="${1}"
  716. echo
  717. echo "Downloading data for pull request #${pr}."
  718. local -a pr_commit_urls
  719. while IFS= read -r pr_commit_url; do
  720. pr_commit_urls+=("$pr_commit_url")
  721. done < <(curl -Ssf "https://api.github.com/repos/neovim/neovim/pulls/${pr}/commits" \
  722. | jq -r '.[].html_url')
  723. echo "Found ${#pr_commit_urls[@]} commit(s)."
  724. local pr_commit_url
  725. local reply
  726. for pr_commit_url in "${pr_commit_urls[@]}"; do
  727. review_commit "${pr_commit_url}"
  728. if [[ "${pr_commit_url}" != "${pr_commit_urls[-1]}" ]]; then
  729. read -p "Continue with next commit (Y/n)? " -n 1 -r reply
  730. echo
  731. if [[ "${reply}" == n ]]; then
  732. break
  733. fi
  734. fi
  735. done
  736. clean_files
  737. }
  738. while getopts "hlLmMVp:P:g:r:s" opt; do
  739. case ${opt} in
  740. h)
  741. usage
  742. exit 0
  743. ;;
  744. l)
  745. shift # remove opt
  746. show_vimpatches "$@"
  747. exit 0
  748. ;;
  749. L)
  750. shift # remove opt
  751. list_missing_vimpatches 0 "$@"
  752. exit 0
  753. ;;
  754. M)
  755. list_vimpatch_numbers
  756. exit 0
  757. ;;
  758. m)
  759. shift # remove opt
  760. list_missing_previous_vimpatches_for_patch "$@"
  761. exit 0
  762. ;;
  763. p)
  764. stage_patch "${OPTARG}"
  765. exit
  766. ;;
  767. P)
  768. stage_patch "${OPTARG}" TRY_APPLY
  769. exit 0
  770. ;;
  771. g)
  772. get_vimpatch "${OPTARG}"
  773. exit 0
  774. ;;
  775. r)
  776. review_pr "${OPTARG}"
  777. exit 0
  778. ;;
  779. s)
  780. shift # remove opt
  781. submit_pr "$@"
  782. exit 0
  783. ;;
  784. V)
  785. get_vim_sources update
  786. exit 0
  787. ;;
  788. *)
  789. exit 1
  790. ;;
  791. esac
  792. done
  793. usage
  794. # vim: et sw=2