vim-patch.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. readonly NVIM_SOURCE_DIR="${NVIM_SOURCE_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
  7. readonly VIM_SOURCE_DIR_DEFAULT="${NVIM_SOURCE_DIR}/.vim-src"
  8. readonly VIM_SOURCE_DIR="${VIM_SOURCE_DIR:-${VIM_SOURCE_DIR_DEFAULT}}"
  9. readonly BASENAME="$(basename "${0}")"
  10. readonly BRANCH_PREFIX="vim-"
  11. CREATED_FILES=()
  12. usage() {
  13. echo "Port Vim patches to Neovim"
  14. echo "https://github.com/neovim/neovim/wiki/Merging-patches-from-upstream-vim"
  15. echo
  16. echo "Usage: ${BASENAME} [-h | -l | -p vim-revision | -r pr-number]"
  17. echo
  18. echo "Options:"
  19. echo " -h Show this message and exit."
  20. echo " -l [git-log opts] List missing Vim patches."
  21. echo " -L [git-log opts] List missing Vim patches (for scripts)."
  22. echo " -M List all merged patch-numbers (at current v:version)."
  23. echo " -p {vim-revision} Download and generate a Vim patch. vim-revision"
  24. echo " can be a Vim version (8.0.xxx) or a Git hash."
  25. echo " -P {vim-revision} Download, generate and apply a Vim patch."
  26. echo " -g {vim-revision} Download a Vim patch."
  27. echo " -s Create a vim-patch pull request."
  28. echo " -r {pr-number} Review a vim-patch pull request."
  29. echo " -V Clone the Vim source code to \$VIM_SOURCE_DIR."
  30. echo
  31. echo " \$VIM_SOURCE_DIR controls where Vim sources are found"
  32. echo " (default: '${VIM_SOURCE_DIR_DEFAULT}')"
  33. echo
  34. echo "Examples:"
  35. echo
  36. echo " - List missing patches for a given file (in the Vim source):"
  37. echo " $0 -l -- src/edit.c"
  38. }
  39. msg_ok() {
  40. printf '\e[32m✔\e[0m %s\n' "$@"
  41. }
  42. msg_err() {
  43. printf '\e[31m✘\e[0m %s\n' "$@" >&2
  44. }
  45. # Checks if a program is in the user's PATH, and is executable.
  46. check_executable() {
  47. test -x "$(command -v "${1}")"
  48. }
  49. require_executable() {
  50. if ! check_executable "${1}"; then
  51. >&2 echo "${BASENAME}: '${1}' not found in PATH or not executable."
  52. exit 1
  53. fi
  54. }
  55. clean_files() {
  56. if [[ ${#CREATED_FILES[@]} -eq 0 ]]; then
  57. return
  58. fi
  59. echo
  60. echo "Created files:"
  61. local file
  62. for file in "${CREATED_FILES[@]}"; do
  63. echo " • ${file}"
  64. done
  65. read -p "Delete these files (Y/n)? " -n 1 -r reply
  66. echo
  67. if [[ "${reply}" == n ]]; then
  68. echo "You can use 'git clean' to remove these files when you're done."
  69. else
  70. rm -- "${CREATED_FILES[@]}"
  71. fi
  72. }
  73. get_vim_sources() {
  74. require_executable git
  75. if [[ ! -d ${VIM_SOURCE_DIR} ]]; then
  76. echo "Cloning Vim into: ${VIM_SOURCE_DIR}"
  77. git clone https://github.com/vim/vim.git "${VIM_SOURCE_DIR}"
  78. cd "${VIM_SOURCE_DIR}"
  79. else
  80. cd "${VIM_SOURCE_DIR}"
  81. if ! [ -d ".git" ] \
  82. && ! [ "$(git rev-parse --show-toplevel)" = "${VIM_SOURCE_DIR}" ]; then
  83. msg_err "${VIM_SOURCE_DIR} does not appear to be a git repository."
  84. echo " Please remove it and try again."
  85. exit 1
  86. fi
  87. echo "Updating Vim sources: ${VIM_SOURCE_DIR}"
  88. if git pull --ff; then
  89. msg_ok "Updated Vim sources."
  90. else
  91. msg_err "Could not update Vim sources; ignoring error."
  92. fi
  93. fi
  94. }
  95. commit_message() {
  96. if [[ -n "$vim_tag" ]]; then
  97. printf '%s\n%s' "${vim_message}" "${vim_commit_url}"
  98. else
  99. printf 'vim-patch:%s\n\n%s\n%s' "$vim_version" "$vim_message" "$vim_commit_url"
  100. fi
  101. }
  102. find_git_remote() {
  103. git_remote=$(git remote -v \
  104. | awk '$2 ~ /github.com[:\/]neovim\/neovim/ && $3 == "(fetch)" {print $1; exit}')
  105. if [[ -z "$git_remote" ]]; then
  106. git_remote="origin"
  107. fi
  108. echo "$git_remote"
  109. }
  110. # Assign variables for a given Vim tag, patch version, or commit.
  111. # Might exit in case it cannot be found.
  112. assign_commit_details() {
  113. local vim_commit_ref
  114. if [[ ${1} =~ v?[0-9]\.[0-9]\.[0-9]{3,4} ]]; then
  115. # Interpret parameter as version number (tag).
  116. if [[ "${1:0:1}" == v ]]; then
  117. vim_version="${1:1}"
  118. vim_tag="${1}"
  119. else
  120. vim_version="${1}"
  121. vim_tag="v${1}"
  122. fi
  123. vim_commit_ref="$vim_tag"
  124. local munge_commit_line=true
  125. else
  126. # Interpret parameter as commit hash.
  127. vim_version="${1:0:12}"
  128. vim_tag=
  129. vim_commit_ref="$vim_version"
  130. local munge_commit_line=false
  131. fi
  132. vim_commit=$(git -C "${VIM_SOURCE_DIR}" log -1 --format="%H" "${vim_commit_ref}" --) || {
  133. >&2 msg_err "Couldn't find Vim revision '${vim_commit_ref}'."
  134. exit 3
  135. }
  136. vim_commit_url="https://github.com/vim/vim/commit/${vim_commit}"
  137. vim_message="$(git -C "${VIM_SOURCE_DIR}" log -1 --pretty='format:%B' "${vim_commit}" \
  138. | sed -e 's/\(#[0-9]\{1,\}\)/vim\/vim\1/g')"
  139. if [[ ${munge_commit_line} == "true" ]]; then
  140. # Remove first line of commit message.
  141. vim_message="$(echo "${vim_message}" | sed -e '1s/^patch /vim-patch:/')"
  142. fi
  143. patch_file="vim-${vim_version}.patch"
  144. }
  145. # Patch surgery
  146. preprocess_patch() {
  147. local file="$1"
  148. local nvim="nvim -u NORC -i NONE --headless"
  149. # Remove *.proto, Make*, gui_*, some if_*
  150. local na_src='proto\|Make*\|gui_*\|if_lua\|if_mzsch\|if_olepp\|if_ole\|if_perl\|if_py\|if_ruby\|if_tcl\|if_xcmdsrv'
  151. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/\S*\<\%(testdir/\)\@<!\%('"${na_src}"'\)@norm! d/\v(^diff)|%$ ' +w +q "$file"
  152. # Remove unwanted Vim doc files.
  153. local na_doc='channel\.txt\|netbeans\.txt\|os_\w\+\.txt\|term\.txt\|todo\.txt\|version\d\.txt\|sponsor\.txt\|intro\.txt\|tags'
  154. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/doc/\<\%('"${na_doc}"'\)\>@norm! d/\v(^diff)|%$ ' +w +q "$file"
  155. # Remove "Last change ..." changes in doc files.
  156. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'%s/^@@.*\n.*For Vim version.*Last change.*\n.*For Vim version.*Last change.*//' +w +q "$file"
  157. # Remove screen dumps, testdir/Make_*.mak files
  158. local na_src_testdir='Make_amiga.mak\|Make_dos.mak\|Make_ming.mak\|Make_vms.mms\|dumps/.*.dump'
  159. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/testdir/\<\%('"${na_src_testdir}"'\)\>@norm! d/\v(^diff)|%$ ' +w +q "$file"
  160. # Remove version.c #7555
  161. local na_po='version.c'
  162. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/\<\%('${na_po}'\)\>@norm! d/\v(^diff)|%$ ' +w +q "$file"
  163. # Remove some *.po files. #5622
  164. 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'
  165. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/po/\<\%('${na_po}'\)\>@norm! d/\v(^diff)|%$ ' +w +q "$file"
  166. # Remove vimrc_example.vim
  167. local na_vimrcexample='vimrc_example\.vim'
  168. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/\<\%('${na_vimrcexample}'\)\>@norm! d/\v(^diff)|%$ ' +w +q "$file"
  169. # Rename src/ paths to src/nvim/
  170. LC_ALL=C sed -e 's/\( [ab]\/src\)/\1\/nvim/g' \
  171. "$file" > "$file".tmp && mv "$file".tmp "$file"
  172. # Rename test_urls.vim to check_urls.vim
  173. LC_ALL=C sed -e 's@\( [ab]\)/runtime/doc/test\(_urls.vim\)@\1/scripts/check\2@g' \
  174. "$file" > "$file".tmp && mv "$file".tmp "$file"
  175. # Rename path to check_colors.vim
  176. LC_ALL=C sed -e 's@\( [ab]/runtime\)/colors/\(tools/check_colors.vim\)@\1/\2@g' \
  177. "$file" > "$file".tmp && mv "$file".tmp "$file"
  178. }
  179. get_vimpatch() {
  180. get_vim_sources
  181. assign_commit_details "${1}"
  182. msg_ok "Found Vim revision '${vim_commit}'."
  183. local patch_content
  184. patch_content="$(git --no-pager show --unified=5 --color=never -1 --pretty=medium "${vim_commit}")"
  185. cd "${NVIM_SOURCE_DIR}"
  186. printf "Creating patch...\n"
  187. echo "$patch_content" > "${NVIM_SOURCE_DIR}/${patch_file}"
  188. printf "Pre-processing patch...\n"
  189. preprocess_patch "${NVIM_SOURCE_DIR}/${patch_file}"
  190. msg_ok "Saved patch to '${NVIM_SOURCE_DIR}/${patch_file}'."
  191. }
  192. # shellcheck disable=SC2015 # "Note that A && B || C is not if-then-else."
  193. stage_patch() {
  194. get_vimpatch "$1"
  195. local try_apply="${2:-}"
  196. local git_remote
  197. git_remote="$(find_git_remote)"
  198. local checked_out_branch
  199. checked_out_branch="$(git rev-parse --abbrev-ref HEAD)"
  200. if [[ "${checked_out_branch}" == ${BRANCH_PREFIX}* ]]; then
  201. msg_ok "Current branch '${checked_out_branch}' seems to be a vim-patch"
  202. echo " branch; not creating a new branch."
  203. else
  204. printf '\nFetching "%s/master".\n' "${git_remote}"
  205. output="$(git fetch "${git_remote}" master 2>&1)" &&
  206. msg_ok "${output}" ||
  207. (msg_err "${output}"; false)
  208. local nvim_branch="${BRANCH_PREFIX}${vim_version}"
  209. echo
  210. echo "Creating new branch '${nvim_branch}' based on '${git_remote}/master'."
  211. cd "${NVIM_SOURCE_DIR}"
  212. output="$(git checkout -b "${nvim_branch}" "${git_remote}/master" 2>&1)" &&
  213. msg_ok "${output}" ||
  214. (msg_err "${output}"; false)
  215. fi
  216. printf "\nCreating empty commit with correct commit message.\n"
  217. output="$(commit_message | git commit --allow-empty --file 2>&1 -)" &&
  218. msg_ok "${output}" ||
  219. (msg_err "${output}"; false)
  220. local ret=0
  221. if test -n "$try_apply" ; then
  222. if ! check_executable patch; then
  223. printf "\n"
  224. msg_err "'patch' command not found\n"
  225. else
  226. printf "\nApplying patch...\n"
  227. patch -p1 --fuzz=1 --no-backup-if-mismatch < "${patch_file}" || ret=$?
  228. fi
  229. printf "\nInstructions:\n Proceed to port the patch.\n"
  230. else
  231. 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}"
  232. fi
  233. printf '
  234. Stage your changes ("git add ..."), then use "git commit --amend" to commit.
  235. To port more patches (if any) related to %s,
  236. run "%s" again.
  237. * Do this only for _related_ patches (otherwise it increases the
  238. size of the pull request, making it harder to review)
  239. When you are done, try "%s -s" to create the pull request.
  240. See the wiki for more information:
  241. * https://github.com/neovim/neovim/wiki/Merging-patches-from-upstream-vim
  242. ' "${vim_version}" "${BASENAME}" "${BASENAME}"
  243. return $ret
  244. }
  245. hub_pr() {
  246. hub pull-request -m "$1"
  247. }
  248. git_hub_pr() {
  249. git hub pull new -m "$1"
  250. }
  251. # shellcheck disable=SC2015 # "Note that A && B || C is not if-then-else."
  252. submit_pr() {
  253. require_executable git
  254. local push_first
  255. push_first=1
  256. local submit_fn
  257. if check_executable hub; then
  258. submit_fn="hub_pr"
  259. elif check_executable git-hub; then
  260. push_first=0
  261. submit_fn="git_hub_pr"
  262. else
  263. >&2 echo "${BASENAME}: 'hub' or 'git-hub' not found in PATH or not executable."
  264. >&2 echo " Get it here: https://hub.github.com/"
  265. exit 1
  266. fi
  267. cd "${NVIM_SOURCE_DIR}"
  268. local checked_out_branch
  269. checked_out_branch="$(git rev-parse --abbrev-ref HEAD)"
  270. if [[ "${checked_out_branch}" != ${BRANCH_PREFIX}* ]]; then
  271. msg_err "Current branch '${checked_out_branch}' doesn't seem to be a vim-patch branch."
  272. exit 1
  273. fi
  274. local git_remote
  275. git_remote="$(find_git_remote)"
  276. local pr_body
  277. pr_body="$(git log --grep=vim-patch --reverse --format='#### %s%n%n%b%n' "${git_remote}"/master..HEAD)"
  278. local patches
  279. # Extract just the "vim-patch:X.Y.ZZZZ" or "vim-patch:sha" portion of each log
  280. patches=("$(git log --grep=vim-patch --reverse --format='%s' "${git_remote}"/master..HEAD | sed 's/: .*//')")
  281. patches=("${patches[@]//vim-patch:}") # Remove 'vim-patch:' prefix for each item in array.
  282. local pr_title="${patches[*]}" # Create space-separated string from array.
  283. pr_title="${pr_title// /,}" # Replace spaces with commas.
  284. local pr_message
  285. pr_message="$(printf '[RFC] vim-patch:%s\n\n%s\n' "${pr_title#,}" "${pr_body}")"
  286. if [[ $push_first -ne 0 ]]; then
  287. echo "Pushing to 'origin/${checked_out_branch}'."
  288. output="$(git push origin "${checked_out_branch}" 2>&1)" &&
  289. msg_ok "${output}" ||
  290. (msg_err "${output}"; false)
  291. echo
  292. fi
  293. echo "Creating pull request."
  294. output="$(${submit_fn} "${pr_message}" 2>&1)" &&
  295. msg_ok "${output}" ||
  296. (msg_err "${output}"; false)
  297. echo
  298. echo "Cleaning up files."
  299. local patch_file
  300. for patch_file in "${patches[@]}"; do
  301. patch_file="vim-${patch_file}.patch"
  302. if [[ ! -f "${NVIM_SOURCE_DIR}/${patch_file}" ]]; then
  303. continue
  304. fi
  305. rm -- "${NVIM_SOURCE_DIR}/${patch_file}"
  306. msg_ok "Removed '${NVIM_SOURCE_DIR}/${patch_file}'."
  307. done
  308. }
  309. # Gets all Vim commits since the "start" commit.
  310. list_vim_commits() { (
  311. cd "${VIM_SOURCE_DIR}" && git log --reverse --format='%H' v8.0.0000..HEAD "$@"
  312. ) }
  313. # Prints all (sorted) "vim-patch:xxx" tokens found in the Nvim git log.
  314. list_vimpatch_tokens() {
  315. # Use sed…{7,7} to normalize (internal) Git hashes (for tokens caches).
  316. git -C "${NVIM_SOURCE_DIR}" log -E --grep='vim-patch:[^ ,{]{7,}' \
  317. | grep -oE 'vim-patch:[^ ,{:]{7,}' \
  318. | sort \
  319. | uniq \
  320. | sed -nE 's/^(vim-patch:([0-9]+\.[^ ]+|[0-9a-z]{7,7})).*/\1/p'
  321. }
  322. # Prints all patch-numbers (for the current v:version) for which there is
  323. # a "vim-patch:xxx" token in the Nvim git log.
  324. list_vimpatch_numbers() {
  325. # Transform "vim-patch:X.Y.ZZZZ" to "ZZZZ".
  326. list_vimpatch_tokens | while read -r vimpatch_token; do
  327. echo "$vimpatch_token" | grep '8\.0\.' | sed 's/.*vim-patch:8\.0\.\([0-9a-z]\+\).*/\1/'
  328. done
  329. }
  330. # Prints a newline-delimited list of Vim commits, for use by scripts.
  331. # "$@" is passed to list_vim_commits, as extra arguments to git-log.
  332. list_missing_vimpatches() {
  333. local token vim_commit vim_tag patch_number
  334. declare -A tokens
  335. declare -A vim_commit_tags
  336. declare -a git_log_args
  337. # Massage arguments for git-log.
  338. declare -A git_log_replacements=(
  339. [^\(.*/\)?src/nvim/\(.*\)]="\${BASH_REMATCH[1]}src/\${BASH_REMATCH[2]}"
  340. [^\(.*/\)?\.vim-src/\(.*\)]="\${BASH_REMATCH[2]}"
  341. )
  342. for i in "$@"; do
  343. for j in "${!git_log_replacements[@]}"; do
  344. if [[ "$i" =~ $j ]]; then
  345. eval "git_log_args+=(${git_log_replacements[$j]})"
  346. continue 2
  347. fi
  348. done
  349. git_log_args+=("$i")
  350. done
  351. # Find all "vim-patch:xxx" tokens in the Nvim git log.
  352. for token in $(list_vimpatch_tokens); do
  353. tokens[$token]=1
  354. done
  355. # Create an associative array mapping Vim commits to tags.
  356. eval "declare -A vim_commit_tags=(
  357. $(git -C "${VIM_SOURCE_DIR}" for-each-ref refs/tags \
  358. --format '[%(objectname)]=%(refname:strip=2)' \
  359. --sort='-*authordate' \
  360. --shell)
  361. )"
  362. # Exit in case of errors from the above eval (empty vim_commit_tags).
  363. if ! (( "${#vim_commit_tags[@]}" )); then
  364. msg_err "Could not get Vim commits/tags."
  365. exit 1
  366. fi
  367. # Get missing Vim commits
  368. set +u # Avoid "unbound variable" with bash < 4.4 below.
  369. for vim_commit in $(list_vim_commits "${git_log_args[@]}"); do
  370. # Check for vim-patch:<commit_hash> (usually runtime updates).
  371. token="vim-patch:${vim_commit:0:7}"
  372. if [[ "${tokens[$token]-}" ]]; then
  373. continue
  374. fi
  375. vim_tag="${vim_commit_tags[$vim_commit]-}"
  376. if [[ -n "$vim_tag" ]]; then
  377. # Check for vim-patch:<tag> (not commit hash).
  378. patch_number="vim-patch:${vim_tag:1}" # "v7.4.0001" => "7.4.0001"
  379. if [[ "${tokens[$patch_number]-}" ]]; then
  380. continue
  381. fi
  382. echo "$vim_tag"
  383. else
  384. echo "$vim_commit"
  385. fi
  386. done
  387. set -u
  388. }
  389. # Prints a human-formatted list of Vim commits, with instructional messages.
  390. # Passes "$@" onto list_missing_vimpatches (args for git-log).
  391. show_vimpatches() {
  392. get_vim_sources
  393. printf "\nVim patches missing from Neovim:\n"
  394. local -A runtime_commits
  395. for commit in $(git -C "${VIM_SOURCE_DIR}" log --format="%H %D" -- runtime | sed 's/,\? tag: / /g'); do
  396. runtime_commits[$commit]=1
  397. done
  398. list_missing_vimpatches "$@" | while read -r vim_commit; do
  399. if [[ "${runtime_commits[$vim_commit]-}" ]]; then
  400. printf ' • %s (+runtime)\n' "${vim_commit}"
  401. else
  402. printf ' • %s\n' "${vim_commit}"
  403. fi
  404. done
  405. printf "Instructions:
  406. To port one of the above patches to Neovim, execute
  407. this script with the patch revision as argument and
  408. follow the instructions.
  409. Examples: '%s -p 7.4.487'
  410. '%s -p 1e8ebf870720e7b671f98f22d653009826304c4f'
  411. NOTE: Please port the _oldest_ patch if you possibly can.
  412. Out-of-order patches increase the possibility of bugs.
  413. " "${BASENAME}" "${BASENAME}"
  414. }
  415. review_commit() {
  416. local nvim_commit_url="${1}"
  417. local nvim_patch_url="${nvim_commit_url}.patch"
  418. local git_patch_prefix='Subject: \[PATCH\] '
  419. local nvim_patch
  420. nvim_patch="$(curl -Ssf "${nvim_patch_url}")"
  421. local vim_version
  422. vim_version="$(head -n 4 <<< "${nvim_patch}" | sed -n 's/'"${git_patch_prefix}"'vim-patch:\([a-z0-9.]*\)\(:.*\)\{0,1\}$/\1/p')"
  423. echo
  424. if [[ -n "${vim_version}" ]]; then
  425. msg_ok "Detected Vim patch '${vim_version}'."
  426. else
  427. msg_err "Could not detect the Vim patch number."
  428. echo " This script assumes that the PR contains only commits"
  429. echo " with 'vim-patch:XXX' in their title."
  430. echo
  431. printf -- '%s\n\n' "$(head -n 4 <<< "${nvim_patch}")"
  432. local reply
  433. read -p "Continue reviewing (y/N)? " -n 1 -r reply
  434. if [[ "${reply}" == y ]]; then
  435. echo
  436. return
  437. fi
  438. exit 1
  439. fi
  440. assign_commit_details "${vim_version}"
  441. echo
  442. echo "Creating files."
  443. echo "${nvim_patch}" > "${NVIM_SOURCE_DIR}/n${patch_file}"
  444. msg_ok "Saved pull request diff to '${NVIM_SOURCE_DIR}/n${patch_file}'."
  445. CREATED_FILES+=("${NVIM_SOURCE_DIR}/n${patch_file}")
  446. local nvim="nvim -u NORC -n -i NONE --headless"
  447. 2>/dev/null $nvim --cmd 'set dir=/tmp' +'1,/^$/g/^ /-1join' +w +q "${NVIM_SOURCE_DIR}/n${patch_file}"
  448. local expected_commit_message
  449. expected_commit_message="$(commit_message)"
  450. local message_length
  451. message_length="$(wc -l <<< "${expected_commit_message}")"
  452. local commit_message
  453. commit_message="$(tail -n +4 "${NVIM_SOURCE_DIR}/n${patch_file}" | head -n "${message_length}")"
  454. if [[ "${commit_message#${git_patch_prefix}}" == "${expected_commit_message}" ]]; then
  455. msg_ok "Found expected commit message."
  456. else
  457. msg_err "Wrong commit message."
  458. echo " Expected:"
  459. echo "${expected_commit_message}"
  460. echo " Actual:"
  461. echo "${commit_message#${git_patch_prefix}}"
  462. fi
  463. get_vimpatch "${vim_version}"
  464. CREATED_FILES+=("${NVIM_SOURCE_DIR}/${patch_file}")
  465. echo
  466. echo "Launching nvim."
  467. nvim -c "cd ${NVIM_SOURCE_DIR}" \
  468. -O "${NVIM_SOURCE_DIR}/${patch_file}" "${NVIM_SOURCE_DIR}/n${patch_file}"
  469. }
  470. review_pr() {
  471. require_executable curl
  472. require_executable nvim
  473. require_executable jq
  474. get_vim_sources
  475. local pr="${1}"
  476. echo
  477. echo "Downloading data for pull request #${pr}."
  478. local pr_commit_urls=(
  479. "$(curl -Ssf "https://api.github.com/repos/neovim/neovim/pulls/${pr}/commits" \
  480. | jq -r '.[].html_url')")
  481. echo "Found ${#pr_commit_urls[@]} commit(s)."
  482. local pr_commit_url
  483. local reply
  484. for pr_commit_url in "${pr_commit_urls[@]}"; do
  485. review_commit "${pr_commit_url}"
  486. if [[ "${pr_commit_url}" != "${pr_commit_urls[-1]}" ]]; then
  487. read -p "Continue with next commit (Y/n)? " -n 1 -r reply
  488. echo
  489. if [[ "${reply}" == n ]]; then
  490. break
  491. fi
  492. fi
  493. done
  494. clean_files
  495. }
  496. while getopts "hlLMVp:P:g:r:s" opt; do
  497. case ${opt} in
  498. h)
  499. usage
  500. exit 0
  501. ;;
  502. l)
  503. shift # remove opt
  504. show_vimpatches "$@"
  505. exit 0
  506. ;;
  507. L)
  508. shift # remove opt
  509. list_missing_vimpatches "$@"
  510. exit 0
  511. ;;
  512. M)
  513. list_vimpatch_numbers
  514. exit 0
  515. ;;
  516. p)
  517. stage_patch "${OPTARG}"
  518. exit
  519. ;;
  520. P)
  521. stage_patch "${OPTARG}" TRY_APPLY
  522. exit 0
  523. ;;
  524. g)
  525. get_vimpatch "${OPTARG}"
  526. exit 0
  527. ;;
  528. r)
  529. review_pr "${OPTARG}"
  530. exit 0
  531. ;;
  532. s)
  533. submit_pr
  534. exit 0
  535. ;;
  536. V)
  537. get_vim_sources
  538. exit 0
  539. ;;
  540. *)
  541. exit 1
  542. ;;
  543. esac
  544. done
  545. usage
  546. # vim: et sw=2