vim-patch.sh 25 KB

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