vim-patch.sh 24 KB

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