ediff-ptch.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. ;;; ediff-ptch.el --- Ediff's patch support
  2. ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Package: ediff
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (provide 'ediff-ptch)
  19. (defgroup ediff-ptch nil
  20. "Ediff patch support."
  21. :tag "Patch"
  22. :prefix "ediff-"
  23. :group 'ediff)
  24. ;; compiler pacifier
  25. (eval-when-compile
  26. (require 'ediff))
  27. ;; end pacifier
  28. (require 'ediff-init)
  29. (defcustom ediff-patch-program "patch"
  30. "Name of the program that applies patches.
  31. It is recommended to use GNU-compatible versions."
  32. :type 'string
  33. :group 'ediff-ptch)
  34. (defcustom ediff-patch-options "-f"
  35. "Options to pass to ediff-patch-program.
  36. Note: the `-b' option should be specified in `ediff-backup-specs'.
  37. It is recommended to pass the `-f' option to the patch program, so it won't ask
  38. questions. However, some implementations don't accept this option, in which
  39. case the default value for this variable should be changed."
  40. :type 'string
  41. :group 'ediff-ptch)
  42. (defvar ediff-last-dir-patch nil
  43. "Last directory used by an Ediff command for file to patch.")
  44. ;; the default backup extension
  45. (defconst ediff-default-backup-extension
  46. (if (eq system-type 'ms-dos)
  47. "_orig" ".orig"))
  48. (defcustom ediff-backup-extension ediff-default-backup-extension
  49. "Backup extension used by the patch program.
  50. See also `ediff-backup-specs'."
  51. :type 'string
  52. :group 'ediff-ptch)
  53. (defun ediff-test-patch-utility ()
  54. (condition-case nil
  55. (cond ((eq 0 (call-process ediff-patch-program nil nil nil "-z." "-b"))
  56. ;; GNU `patch' v. >= 2.2
  57. 'gnu)
  58. ((eq 0 (call-process ediff-patch-program nil nil nil "-b"))
  59. 'posix)
  60. (t 'traditional))
  61. (file-error nil)))
  62. (defcustom ediff-backup-specs
  63. (let ((type (ediff-test-patch-utility)))
  64. (cond ((eq type 'gnu)
  65. ;; GNU `patch' v. >= 2.2
  66. (format "-z%s -b" ediff-backup-extension))
  67. ((eq type 'posix)
  68. ;; POSIX `patch' -- ediff-backup-extension must be ".orig"
  69. (setq ediff-backup-extension ediff-default-backup-extension)
  70. "-b")
  71. (t
  72. ;; traditional `patch'
  73. (format "-b %s" ediff-backup-extension))))
  74. "Backup directives to pass to the patch program.
  75. Ediff requires that the old version of the file \(before applying the patch\)
  76. be saved in a file named `the-patch-file.extension'. Usually `extension' is
  77. `.orig', but this can be changed by the user and may depend on the system.
  78. Therefore, Ediff needs to know the backup extension used by the patch program.
  79. Some versions of the patch program let you specify `-b backup-extension'.
  80. Other versions only permit `-b', which assumes the extension `.orig'
  81. \(in which case ediff-backup-extension MUST be also `.orig'\). The latest
  82. versions of GNU patch require `-b -z backup-extension'.
  83. Note that both `ediff-backup-extension' and `ediff-backup-specs'
  84. must be set properly. If your patch program takes the option `-b',
  85. but not `-b extension', the variable `ediff-backup-extension' must
  86. still be set so Ediff will know which extension to use.
  87. Ediff tries to guess the appropriate value for this variables. It is believed
  88. to be working for `traditional' patch, all versions of GNU patch, and for POSIX
  89. patch. So, don't change these variables, unless the default doesn't work."
  90. :type 'string
  91. :group 'ediff-ptch)
  92. (defcustom ediff-patch-default-directory nil
  93. "Default directory to look for patches."
  94. :type '(choice (const nil) string)
  95. :group 'ediff-ptch)
  96. ;; This context diff does not recognize spaces inside files, but removing ' '
  97. ;; from [^ \t] breaks normal patches for some reason
  98. (defcustom ediff-context-diff-label-regexp
  99. (concat "\\(" ; context diff 2-liner
  100. "^\\*\\*\\* +\\([^ \t]+\\)[^*]+[\t ]*\n--- +\\([^ \t]+\\)"
  101. "\\|" ; unified format diff 2-liner
  102. "^--- +\\([^ \t]+\\).*\n\\+\\+\\+ +\\([^ \t]+\\)"
  103. "\\)")
  104. "Regexp matching filename 2-liners at the start of each context diff.
  105. You probably don't want to change that, unless you are using an obscure patch
  106. program."
  107. :type 'regexp
  108. :group 'ediff-ptch)
  109. ;; The buffer of the patch file. Local to control buffer.
  110. (ediff-defvar-local ediff-patchbufer nil "")
  111. ;; The buffer where patch displays its diagnostics.
  112. (ediff-defvar-local ediff-patch-diagnostics nil "")
  113. ;; Map of patch buffer. Has the form:
  114. ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
  115. ;; where filenames are files to which patch would have applied the patch;
  116. ;; marker1 delimits the beginning of the corresponding patch and marker2 does
  117. ;; it for the end.
  118. (ediff-defvar-local ediff-patch-map nil "")
  119. ;; strip prefix from filename
  120. ;; returns /dev/null, if can't strip prefix
  121. (defsubst ediff-file-name-sans-prefix (filename prefix)
  122. (if prefix
  123. (save-match-data
  124. (if (string-match (concat "^" (if (stringp prefix)
  125. (regexp-quote prefix)
  126. ""))
  127. filename)
  128. (substring filename (match-end 0))
  129. (concat "/null/" filename)))
  130. filename)
  131. )
  132. ;; no longer used
  133. ;; return the number of matches of regexp in buf starting from the beginning
  134. (defun ediff-count-matches (regexp buf)
  135. (ediff-with-current-buffer buf
  136. (let ((count 0) opoint)
  137. (save-excursion
  138. (goto-char (point-min))
  139. (while (and (not (eobp))
  140. (progn (setq opoint (point))
  141. (re-search-forward regexp nil t)))
  142. (if (= opoint (point))
  143. (forward-char 1)
  144. (setq count (1+ count)))))
  145. count)))
  146. ;; Scan BUF (which is supposed to contain a patch) and make a list of the form
  147. ;; ((nil nil filename-spec1 marker1 marker2)
  148. ;; (nil nil filename-spec2 marker1 marker2) ...)
  149. ;; where filename-spec[12] are files to which the `patch' program would
  150. ;; have applied the patch.
  151. ;; nin, nil are placeholders. See ediff-make-new-meta-list-element in
  152. ;; ediff-meta.el for the explanations.
  153. ;; In the beginning we don't know exactly which files need to be patched.
  154. ;; We usually come up with two candidates and ediff-file-name-sans-prefix
  155. ;; resolves this later.
  156. ;;
  157. ;; The marker `marker1' delimits the beginning of the corresponding patch and
  158. ;; `marker2' does it for the end.
  159. ;; The result of ediff-map-patch-buffer is a list, which is then assigned
  160. ;; to ediff-patch-map.
  161. ;; The function returns the number of elements in the list ediff-patch-map
  162. (defun ediff-map-patch-buffer (buf)
  163. (ediff-with-current-buffer buf
  164. (let ((count 0)
  165. (mark1 (move-marker (make-marker) (point-min)))
  166. (mark1-end (point-min))
  167. (possible-file-names '("/dev/null" . "/dev/null"))
  168. mark2-end mark2 filenames
  169. beg1 beg2 end1 end2
  170. patch-map opoint)
  171. (save-excursion
  172. (goto-char (point-min))
  173. (setq opoint (point))
  174. (while (and (not (eobp))
  175. (re-search-forward ediff-context-diff-label-regexp nil t))
  176. (if (= opoint (point))
  177. (forward-char 1) ; ensure progress towards the end
  178. (setq mark2 (move-marker (make-marker) (match-beginning 0))
  179. mark2-end (match-end 0)
  180. beg1 (or (match-beginning 2) (match-beginning 4))
  181. end1 (or (match-end 2) (match-end 4))
  182. beg2 (or (match-beginning 3) (match-beginning 5))
  183. end2 (or (match-end 3) (match-end 5)))
  184. ;; possible-file-names is holding the new file names until we
  185. ;; insert the old file name in the patch map
  186. ;; It is a pair
  187. ;; (filename-from-1st-header-line . filename-from-2nd-line)
  188. (setq possible-file-names
  189. (cons (if (and beg1 end1)
  190. (buffer-substring beg1 end1)
  191. "/dev/null")
  192. (if (and beg2 end2)
  193. (buffer-substring beg2 end2)
  194. "/dev/null")))
  195. ;; check for any `Index:' or `Prereq:' lines, but don't use them
  196. (if (re-search-backward "^Index:" mark1-end 'noerror)
  197. (move-marker mark2 (match-beginning 0)))
  198. (if (re-search-backward "^Prereq:" mark1-end 'noerror)
  199. (move-marker mark2 (match-beginning 0)))
  200. (goto-char mark2-end)
  201. (if filenames
  202. (setq patch-map
  203. (cons (ediff-make-new-meta-list-element
  204. filenames mark1 mark2)
  205. patch-map)))
  206. (setq mark1 mark2
  207. mark1-end mark2-end
  208. filenames possible-file-names))
  209. (setq opoint (point)
  210. count (1+ count))))
  211. (setq mark2 (point-max-marker)
  212. patch-map (cons (ediff-make-new-meta-list-element
  213. possible-file-names mark1 mark2)
  214. patch-map))
  215. (setq ediff-patch-map (nreverse patch-map))
  216. count)))
  217. ;; Fix up the file names in the list using the argument FILENAME
  218. ;; Algorithm: find the files' directories in the patch and, if a directory is
  219. ;; absolute, cut it out from the corresponding file name in the patch.
  220. ;; Relative directories are not cut out.
  221. ;; Prepend the directory of FILENAME to each resulting file (which came
  222. ;; originally from the patch).
  223. ;; In addition, the first file in the patch document is replaced by FILENAME.
  224. ;; Each file is actually a pair of files found in the context diff header
  225. ;; In the end, for each pair, we ask the user which file to patch.
  226. ;; Note: Ediff doesn't recognize multi-file patches that are separated
  227. ;; with the `Index:' line. It treats them as a single-file patch.
  228. ;;
  229. ;; Executes inside the patch buffer
  230. (defun ediff-fixup-patch-map (filename)
  231. (setq filename (expand-file-name filename))
  232. (let ((actual-dir (if (file-directory-p filename)
  233. ;; directory part of filename
  234. (file-name-as-directory filename)
  235. (file-name-directory filename)))
  236. ;; In case 2 files are possible patch targets, the user will be offered
  237. ;; to choose file1 or file2. In a multifile patch, if the user chooses
  238. ;; 1 or 2, this choice is preserved to decide future alternatives.
  239. chosen-alternative
  240. )
  241. ;; chop off base-dirs
  242. (mapc (lambda (session-info)
  243. (let* ((proposed-file-names
  244. ;; Filename-spec is objA; it is represented as
  245. ;; (file1 . file2). Get it using ediff-get-session-objA.
  246. (ediff-get-session-objA-name session-info))
  247. ;; base-dir1 is the dir part of the 1st file in the patch
  248. (base-dir1
  249. (or (file-name-directory (car proposed-file-names))
  250. ""))
  251. ;; directory part of the 2nd file in the patch
  252. (base-dir2
  253. (or (file-name-directory (cdr proposed-file-names))
  254. ""))
  255. )
  256. ;; If both base-dir1 and base-dir2 are relative and exist,
  257. ;; assume that
  258. ;; these dirs lead to the actual files starting at the present
  259. ;; directory. So, we don't strip these relative dirs from the
  260. ;; file names. This is a heuristic intended to improve guessing
  261. (let ((default-directory (file-name-directory filename)))
  262. (unless (or (file-name-absolute-p base-dir1)
  263. (file-name-absolute-p base-dir2)
  264. (not (file-exists-p base-dir1))
  265. (not (file-exists-p base-dir2)))
  266. (setq base-dir1 ""
  267. base-dir2 "")))
  268. (or (string= (car proposed-file-names) "/dev/null")
  269. (setcar proposed-file-names
  270. (ediff-file-name-sans-prefix
  271. (car proposed-file-names) base-dir1)))
  272. (or (string=
  273. (cdr proposed-file-names) "/dev/null")
  274. (setcdr proposed-file-names
  275. (ediff-file-name-sans-prefix
  276. (cdr proposed-file-names) base-dir2)))
  277. ))
  278. ediff-patch-map)
  279. ;; take the given file name into account
  280. (or (file-directory-p filename)
  281. (string= "/dev/null" filename)
  282. (setcar (ediff-get-session-objA (car ediff-patch-map))
  283. (cons (file-name-nondirectory filename)
  284. (file-name-nondirectory filename))))
  285. ;; prepend actual-dir
  286. (mapc (lambda (session-info)
  287. (let ((proposed-file-names
  288. (ediff-get-session-objA-name session-info)))
  289. (if (and (string-match "^/null/" (car proposed-file-names))
  290. (string-match "^/null/" (cdr proposed-file-names)))
  291. ;; couldn't intuit the file name to patch, so
  292. ;; something is amiss
  293. (progn
  294. (with-output-to-temp-buffer ediff-msg-buffer
  295. (ediff-with-current-buffer standard-output
  296. (fundamental-mode))
  297. (princ
  298. (format "
  299. The patch file contains a context diff for
  300. %s
  301. %s
  302. However, Ediff cannot infer the name of the actual file
  303. to be patched on your system. If you know the correct file name,
  304. please enter it now.
  305. If you don't know and still would like to apply patches to
  306. other files, enter /dev/null
  307. "
  308. (substring (car proposed-file-names) 6)
  309. (substring (cdr proposed-file-names) 6))))
  310. (let ((directory t)
  311. user-file)
  312. (while directory
  313. (setq user-file
  314. (read-file-name
  315. "Please enter file name: "
  316. actual-dir actual-dir t))
  317. (if (not (file-directory-p user-file))
  318. (setq directory nil)
  319. (setq directory t)
  320. (beep)
  321. (message "%s is a directory" user-file)
  322. (sit-for 2)))
  323. (setcar (ediff-get-session-objA session-info)
  324. (cons user-file user-file))))
  325. (setcar proposed-file-names
  326. (expand-file-name
  327. (concat actual-dir (car proposed-file-names))))
  328. (setcdr proposed-file-names
  329. (expand-file-name
  330. (concat actual-dir (cdr proposed-file-names)))))
  331. ))
  332. ediff-patch-map)
  333. ;; Check for the existing files in each pair and discard the nonexistent
  334. ;; ones. If both exist, ask the user.
  335. (mapcar (lambda (session-info)
  336. (let* ((file1 (car (ediff-get-session-objA-name session-info)))
  337. (file2 (cdr (ediff-get-session-objA-name session-info)))
  338. (session-file-object
  339. (ediff-get-session-objA session-info))
  340. (f1-exists (file-exists-p file1))
  341. (f2-exists (file-exists-p file2)))
  342. (cond
  343. ((and
  344. ;; The patch program prefers the shortest file as the patch
  345. ;; target. However, this is a questionable heuristic. In an
  346. ;; interactive program, like ediff, we can offer the user a
  347. ;; choice.
  348. ;; (< (length file2) (length file1))
  349. (not f1-exists)
  350. f2-exists)
  351. ;; replace file-pair with the winning file2
  352. (setcar session-file-object file2))
  353. ((and
  354. ;; (< (length file1) (length file2))
  355. (not f2-exists)
  356. f1-exists)
  357. ;; replace file-pair with the winning file1
  358. (setcar session-file-object file1))
  359. ((and f1-exists f2-exists
  360. (string= file1 file2))
  361. (setcar session-file-object file1))
  362. ((and f1-exists f2-exists (eq chosen-alternative 1))
  363. (setcar session-file-object file1))
  364. ((and f1-exists f2-exists (eq chosen-alternative 2))
  365. (setcar session-file-object file2))
  366. ((and f1-exists f2-exists)
  367. (with-output-to-temp-buffer ediff-msg-buffer
  368. (ediff-with-current-buffer standard-output
  369. (fundamental-mode))
  370. (princ (format "
  371. Ediff has inferred that
  372. %s
  373. %s
  374. are two possible targets for applying the patch.
  375. Both files seem to be plausible alternatives.
  376. Please advise:
  377. Type `y' to use %s as the target;
  378. Type `n' to use %s as the target.
  379. "
  380. file1 file2 file1 file2)))
  381. (setcar session-file-object
  382. (if (y-or-n-p (format "Use %s ? " file1))
  383. (progn
  384. (setq chosen-alternative 1)
  385. file1)
  386. (setq chosen-alternative 2)
  387. file2))
  388. )
  389. (f2-exists (setcar session-file-object file2))
  390. (f1-exists (setcar session-file-object file1))
  391. (t
  392. (with-output-to-temp-buffer ediff-msg-buffer
  393. (ediff-with-current-buffer standard-output
  394. (fundamental-mode))
  395. (princ "\nEdiff has inferred that")
  396. (if (string= file1 file2)
  397. (princ (format "
  398. %s
  399. is assumed to be the target for this patch. However, this file does not exist."
  400. file1))
  401. (princ (format "
  402. %s
  403. %s
  404. are two possible targets for this patch. However, these files do not exist."
  405. file1 file2)))
  406. (princ "
  407. \nPlease enter an alternative patch target ...\n"))
  408. (let ((directory t)
  409. target)
  410. (while directory
  411. (setq target (read-file-name
  412. "Please enter a patch target: "
  413. actual-dir actual-dir t))
  414. (if (not (file-directory-p target))
  415. (setq directory nil)
  416. (beep)
  417. (message "%s is a directory" target)
  418. (sit-for 2)))
  419. (setcar session-file-object target))))))
  420. ediff-patch-map)
  421. ))
  422. (defun ediff-show-patch-diagnostics ()
  423. (interactive)
  424. (cond ((window-live-p ediff-window-A)
  425. (set-window-buffer ediff-window-A ediff-patch-diagnostics))
  426. ((window-live-p ediff-window-B)
  427. (set-window-buffer ediff-window-B ediff-patch-diagnostics))
  428. (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
  429. ;; prompt for file, get the buffer
  430. (defun ediff-prompt-for-patch-file ()
  431. (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch)
  432. (ediff-patch-default-directory) ; try patch default dir
  433. (t default-directory)))
  434. (coding-system-for-read ediff-coding-system-for-read)
  435. patch-file-name)
  436. (setq patch-file-name
  437. (read-file-name
  438. (format "Patch is in file%s: "
  439. (cond ((and buffer-file-name
  440. (equal (expand-file-name dir)
  441. (file-name-directory buffer-file-name)))
  442. (concat
  443. " (default "
  444. (file-name-nondirectory buffer-file-name)
  445. ")"))
  446. (t "")))
  447. dir buffer-file-name 'must-match))
  448. (if (file-directory-p patch-file-name)
  449. (error "Patch file cannot be a directory: %s" patch-file-name)
  450. (find-file-noselect patch-file-name))
  451. ))
  452. ;; Try current buffer, then the other window's buffer. Else, give up.
  453. (defun ediff-prompt-for-patch-buffer ()
  454. (get-buffer
  455. (read-buffer
  456. "Buffer that holds the patch: "
  457. (cond ((save-excursion
  458. (goto-char (point-min))
  459. (re-search-forward ediff-context-diff-label-regexp nil t))
  460. (current-buffer))
  461. ((save-window-excursion
  462. (other-window 1)
  463. (save-excursion
  464. (goto-char (point-min))
  465. (and (re-search-forward ediff-context-diff-label-regexp nil t)
  466. (current-buffer)))))
  467. ((save-window-excursion
  468. (other-window -1)
  469. (save-excursion
  470. (goto-char (point-min))
  471. (and (re-search-forward ediff-context-diff-label-regexp nil t)
  472. (current-buffer)))))
  473. (t (ediff-other-buffer (current-buffer))))
  474. 'must-match)))
  475. (defun ediff-get-patch-buffer (&optional arg patch-buf)
  476. "Obtain patch buffer. If patch is already in a buffer---use it.
  477. Else, read patch file into a new buffer. If patch buffer is passed as an
  478. optional argument, then use it."
  479. (let ((last-nonmenu-event t) ; Emacs: don't use dialog box
  480. last-command-event) ; XEmacs: don't use dialog box
  481. (cond ((ediff-buffer-live-p patch-buf))
  482. ;; even prefix arg: patch in buffer
  483. ((and (integerp arg) (eq 0 (mod arg 2)))
  484. (setq patch-buf (ediff-prompt-for-patch-buffer)))
  485. ;; odd prefix arg: get patch from a file
  486. ((and (integerp arg) (eq 1 (mod arg 2)))
  487. (setq patch-buf (ediff-prompt-for-patch-file)))
  488. (t (setq patch-buf
  489. (if (y-or-n-p "Is the patch already in a buffer? ")
  490. (ediff-prompt-for-patch-buffer)
  491. (ediff-prompt-for-patch-file)))))
  492. (ediff-with-current-buffer patch-buf
  493. (goto-char (point-min))
  494. (or (ediff-get-visible-buffer-window patch-buf)
  495. (progn
  496. (pop-to-buffer patch-buf 'other-window)
  497. (select-window (previous-window)))))
  498. (ediff-map-patch-buffer patch-buf)
  499. patch-buf))
  500. ;; Dispatch the right patch file function: regular or meta-level,
  501. ;; depending on how many patches are in the patch file.
  502. ;; At present, there is no support for meta-level patches.
  503. ;; Should return either the ctl buffer or the meta-buffer
  504. (defun ediff-dispatch-file-patching-job (patch-buf filename
  505. &optional startup-hooks)
  506. (ediff-with-current-buffer patch-buf
  507. ;; relativize names in the patch with respect to source-file
  508. (ediff-fixup-patch-map filename)
  509. (if (< (length ediff-patch-map) 2)
  510. (ediff-patch-file-internal
  511. patch-buf
  512. (if (and ediff-patch-map
  513. (not (string-match
  514. "^/dev/null"
  515. ;; this is the file to patch
  516. (ediff-get-session-objA-name (car ediff-patch-map))))
  517. (> (length
  518. (ediff-get-session-objA-name (car ediff-patch-map)))
  519. 1))
  520. (ediff-get-session-objA-name (car ediff-patch-map))
  521. filename)
  522. startup-hooks)
  523. (ediff-multi-patch-internal patch-buf startup-hooks))
  524. ))
  525. ;; When patching a buffer, never change the orig file. Instead, create a new
  526. ;; buffer, ***_patched, even if the buff visits a file.
  527. ;; Users who want to actually patch the buffer should use
  528. ;; ediff-patch-file, not ediff-patch-buffer.
  529. (defun ediff-patch-buffer-internal (patch-buf
  530. buf-to-patch-name
  531. &optional startup-hooks)
  532. (let* ((buf-to-patch (get-buffer buf-to-patch-name))
  533. (visited-file (if buf-to-patch (buffer-file-name buf-to-patch)))
  534. (buf-mod-status (buffer-modified-p buf-to-patch))
  535. (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
  536. ediff-patch-map)) 1))
  537. default-dir file-name ctl-buf)
  538. (if multifile-patch-p
  539. (error
  540. "To apply multi-file patches, please use `ediff-patch-file'"))
  541. ;; create a temp file to patch
  542. (ediff-with-current-buffer buf-to-patch
  543. (setq default-dir default-directory)
  544. (setq file-name (ediff-make-temp-file buf-to-patch))
  545. ;; temporarily switch visited file name, if any
  546. (set-visited-file-name file-name)
  547. ;; don't create auto-save file, if buff was visiting a file
  548. (or visited-file
  549. (setq buffer-auto-save-file-name nil))
  550. ;; don't confuse the user with a new bufname
  551. (rename-buffer buf-to-patch-name)
  552. (set-buffer-modified-p nil)
  553. (set-visited-file-modtime) ; sync buffer and temp file
  554. (setq default-directory default-dir)
  555. )
  556. ;; dispatch a patch function
  557. (setq ctl-buf (ediff-dispatch-file-patching-job
  558. patch-buf file-name startup-hooks))
  559. (ediff-with-current-buffer ctl-buf
  560. (delete-file (buffer-file-name ediff-buffer-A))
  561. (delete-file (buffer-file-name ediff-buffer-B))
  562. (ediff-with-current-buffer ediff-buffer-A
  563. (if default-dir (setq default-directory default-dir))
  564. (set-visited-file-name visited-file) ; visited-file might be nil
  565. (rename-buffer buf-to-patch-name)
  566. (set-buffer-modified-p buf-mod-status))
  567. (ediff-with-current-buffer ediff-buffer-B
  568. (setq buffer-auto-save-file-name nil) ; don't create auto-save file
  569. (if default-dir (setq default-directory default-dir))
  570. (set-visited-file-name nil)
  571. (rename-buffer (ediff-unique-buffer-name
  572. (concat buf-to-patch-name "_patched") ""))
  573. (set-buffer-modified-p t)))
  574. ))
  575. ;; Traditional patch has weird return codes.
  576. ;; GNU and Posix return 1 if some hanks failed and 2 in case of trouble.
  577. ;; 0 is a good code in all cases.
  578. ;; We'll do the conservative thing.
  579. (defun ediff-patch-return-code-ok (code)
  580. (eq code 0))
  581. ;;; (if (eq (ediff-test-patch-utility) 'traditional)
  582. ;;; (eq code 0)
  583. ;;; (not (eq code 2))))
  584. (defun ediff-patch-file-internal (patch-buf source-filename
  585. &optional startup-hooks)
  586. (setq source-filename (expand-file-name source-filename))
  587. (let* ((shell-file-name ediff-shell)
  588. (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
  589. ;; ediff-find-file may use a temp file to do the patch
  590. ;; so, we save source-filename and true-source-filename as a var
  591. ;; that initially is source-filename but may be changed to a temp
  592. ;; file for the purpose of patching.
  593. (true-source-filename source-filename)
  594. (target-filename source-filename)
  595. ;; this ensures that the patch process gets patch buffer in the
  596. ;; encoding that Emacs thinks is right for that type of text
  597. (coding-system-for-write
  598. (if (boundp 'buffer-file-coding-system) buffer-file-coding-system))
  599. target-buf buf-to-patch file-name-magic-p
  600. patch-return-code ctl-buf backup-style aux-wind)
  601. (if (string-match "V" ediff-patch-options)
  602. (error
  603. "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
  604. ;; Make a temp file, if source-filename has a magic file handler (or if
  605. ;; it is handled via auto-mode-alist and similar magic).
  606. ;; Check if there is a buffer visiting source-filename and if they are in
  607. ;; sync; arrange for the deletion of temp file.
  608. (ediff-find-file 'true-source-filename 'buf-to-patch
  609. 'ediff-last-dir-patch 'startup-hooks)
  610. ;; Check if source file name has triggered black magic, such as file name
  611. ;; handlers or auto mode alist, and make a note of it.
  612. ;; true-source-filename should be either the original name or a
  613. ;; temporary file where we put the after-product of the file handler.
  614. (setq file-name-magic-p (not (equal (file-truename true-source-filename)
  615. (file-truename source-filename))))
  616. ;; Checkout orig file, if necessary, so that the patched file
  617. ;; could be checked back in.
  618. (ediff-maybe-checkout buf-to-patch)
  619. (ediff-with-current-buffer patch-diagnostics
  620. (insert-buffer-substring patch-buf)
  621. (message "Applying patch ... ")
  622. ;; fix environment for gnu patch, so it won't make numbered extensions
  623. (setq backup-style (getenv "VERSION_CONTROL"))
  624. (setenv "VERSION_CONTROL" nil)
  625. (setq patch-return-code
  626. (call-process-region
  627. (point-min) (point-max)
  628. shell-file-name
  629. t ; delete region (which contains the patch
  630. t ; insert output (patch diagnostics) in current buffer
  631. nil ; don't redisplay
  632. shell-command-switch ; usually -c
  633. (format "%s %s %s %s"
  634. ediff-patch-program
  635. ediff-patch-options
  636. ediff-backup-specs
  637. (expand-file-name true-source-filename))
  638. ))
  639. ;; restore environment for gnu patch
  640. (setenv "VERSION_CONTROL" backup-style))
  641. (message "Applying patch ... done")
  642. (message "")
  643. (switch-to-buffer patch-diagnostics)
  644. (sit-for 0) ; synchronize - let the user see diagnostics
  645. (or (and (ediff-patch-return-code-ok patch-return-code)
  646. (file-exists-p
  647. (concat true-source-filename ediff-backup-extension)))
  648. (progn
  649. (with-output-to-temp-buffer ediff-msg-buffer
  650. (ediff-with-current-buffer standard-output
  651. (fundamental-mode))
  652. (princ (format
  653. "Patch program has failed due to a bad patch file,
  654. it couldn't apply all hunks, OR
  655. it couldn't create the backup for the file being patched.
  656. The former could be caused by a corrupt patch file or because the %S
  657. program doesn't understand the format of the patch file in use.
  658. The second problem might be due to an incompatibility among these settings:
  659. ediff-patch-program = %S ediff-patch-options = %S
  660. ediff-backup-extension = %S ediff-backup-specs = %S
  661. See Ediff on-line manual for more details on these variables.
  662. In particular, check the documentation for `ediff-backup-specs'.
  663. In any of the above cases, Ediff doesn't compare files automatically.
  664. However, if the patch was applied partially and the backup file was created,
  665. you can still examine the changes via M-x ediff-files"
  666. ediff-patch-program
  667. ediff-patch-program
  668. ediff-patch-options
  669. ediff-backup-extension
  670. ediff-backup-specs
  671. )))
  672. (beep 1)
  673. (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
  674. (progn
  675. (select-window aux-wind)
  676. (goto-char (point-max))))
  677. (switch-to-buffer-other-window patch-diagnostics)
  678. (error "Patch appears to have failed")))
  679. ;; If black magic is involved, apply patch to a temp copy of the
  680. ;; file. Otherwise, apply patch to the orig copy. If patch is applied
  681. ;; to temp copy, we name the result old-name_patched for local files
  682. ;; and temp-copy_patched for remote files. The orig file name isn't
  683. ;; changed, and the temp copy of the original is later deleted.
  684. ;; Without magic, the original file is renamed (usually into
  685. ;; old-name_orig) and the result of patching will have the same name as
  686. ;; the original.
  687. (if (not file-name-magic-p)
  688. (ediff-with-current-buffer buf-to-patch
  689. (set-visited-file-name
  690. (concat source-filename ediff-backup-extension))
  691. (set-buffer-modified-p nil))
  692. ;; Black magic in effect.
  693. ;; If orig file was remote, put the patched file in the temp directory.
  694. ;; If orig file is local, put the patched file in the directory of
  695. ;; the orig file.
  696. (setq target-filename
  697. (concat
  698. (if (ediff-file-remote-p (file-truename source-filename))
  699. true-source-filename
  700. source-filename)
  701. "_patched"))
  702. (rename-file true-source-filename target-filename t)
  703. ;; arrange that the temp copy of orig will be deleted
  704. (rename-file (concat true-source-filename ediff-backup-extension)
  705. true-source-filename t))
  706. ;; make orig buffer read-only
  707. (setq startup-hooks
  708. (cons 'ediff-set-read-only-in-buf-A startup-hooks))
  709. ;; set up a buf for the patched file
  710. (setq target-buf (find-file-noselect target-filename))
  711. (setq ctl-buf
  712. (ediff-buffers-internal
  713. buf-to-patch target-buf nil
  714. startup-hooks 'epatch))
  715. (ediff-with-current-buffer ctl-buf
  716. (setq ediff-patchbufer patch-buf
  717. ediff-patch-diagnostics patch-diagnostics))
  718. (bury-buffer patch-diagnostics)
  719. (message "Type `P', if you need to see patch diagnostics")
  720. ctl-buf))
  721. (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
  722. (let (meta-buf)
  723. (setq startup-hooks
  724. ;; this sets various vars in the meta buffer inside
  725. ;; ediff-prepare-meta-buffer
  726. (cons `(lambda ()
  727. ;; tell what to do if the user clicks on a session record
  728. (setq ediff-session-action-function
  729. 'ediff-patch-file-form-meta
  730. ediff-meta-patchbufer patch-buf) )
  731. startup-hooks))
  732. (setq meta-buf (ediff-prepare-meta-buffer
  733. 'ediff-filegroup-action
  734. (ediff-with-current-buffer patch-buf
  735. (cons (ediff-make-new-meta-list-header
  736. nil ; regexp
  737. (format "%S" patch-buf) ; obj A
  738. nil nil ; objects B,C
  739. nil ; merge-auto-store-dir
  740. nil ; comparison-func
  741. )
  742. ediff-patch-map))
  743. "*Ediff Session Group Panel"
  744. 'ediff-redraw-directory-group-buffer
  745. 'ediff-multifile-patch
  746. startup-hooks))
  747. (ediff-show-meta-buffer meta-buf)
  748. ))
  749. ;; Local Variables:
  750. ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
  751. ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
  752. ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
  753. ;; End:
  754. ;;; ediff-ptch.el ends here