Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3097 lines
124 KiB

4 years ago
  1. ;;; magit-diff.el --- inspect Git diffs -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-2019 The Magit Project Contributors
  3. ;;
  4. ;; You should have received a copy of the AUTHORS.md file which
  5. ;; lists all contributors. If not, see http://magit.vc/authors.
  6. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  7. ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
  8. ;; Magit is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; Magit is distributed in the hope that it will be useful, but WITHOUT
  14. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. ;; License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with Magit. If not, see http://www.gnu.org/licenses.
  20. ;;; Commentary:
  21. ;; This library implements support for looking at Git diffs and
  22. ;; commits.
  23. ;;; Code:
  24. (eval-when-compile
  25. (require 'ansi-color)
  26. (require 'subr-x))
  27. (require 'git-commit)
  28. (require 'magit-core)
  29. ;; For `magit-diff-popup'
  30. (declare-function magit-stash-show "magit-stash" (stash &optional args files))
  31. ;; For `magit-diff-visit-file'
  32. (declare-function dired-jump "dired-x" (&optional other-window file-name))
  33. (declare-function magit-find-file-noselect "magit-files" (rev file))
  34. (declare-function magit-status-setup-buffer "magit-status" (directory))
  35. ;; For `magit-diff-while-committing'
  36. (declare-function magit-commit-message-buffer "magit-commit" ())
  37. ;; For `magit-insert-revision-gravatar'
  38. (defvar gravatar-size)
  39. ;; For `magit-show-commit' and `magit-diff-show-or-scroll'
  40. (declare-function magit-current-blame-chunk "magit-blame" ())
  41. (declare-function magit-blame-mode "magit-blame" (&optional arg))
  42. (defvar magit-blame-mode)
  43. ;; For `magit-diff-show-or-scroll'
  44. (declare-function git-rebase-current-line "git-rebase" ())
  45. ;; For `magit-diff-unmerged'
  46. (declare-function magit-merge-in-progress-p "magit-merge" ())
  47. (declare-function magit--merge-range "magit-merge" (&optional head))
  48. ;; For `magit-diff--dwim'
  49. (declare-function forge--pullreq-ref "forge-pullreq" (pullreq))
  50. ;; For `magit-diff-wash-diff'
  51. (declare-function ansi-color-apply-on-region "ansi-color" (begin end))
  52. (eval-when-compile
  53. (cl-pushnew 'base-ref eieio--known-slot-names)
  54. (cl-pushnew 'orig-rev eieio--known-slot-names)
  55. (cl-pushnew 'action-type eieio--known-slot-names)
  56. (cl-pushnew 'target eieio--known-slot-names))
  57. (require 'diff-mode)
  58. (require 'smerge-mode)
  59. ;;; Options
  60. ;;;; Diff Mode
  61. (defgroup magit-diff nil
  62. "Inspect and manipulate Git diffs."
  63. :link '(info-link "(magit)Diffing")
  64. :group 'magit-modes)
  65. (defcustom magit-diff-mode-hook nil
  66. "Hook run after entering Magit-Diff mode."
  67. :group 'magit-diff
  68. :type 'hook)
  69. (defcustom magit-diff-sections-hook
  70. '(magit-insert-diff
  71. magit-insert-xref-buttons)
  72. "Hook run to insert sections into a `magit-diff-mode' buffer."
  73. :package-version '(magit . "2.3.0")
  74. :group 'magit-diff
  75. :type 'hook)
  76. (defcustom magit-diff-expansion-threshold 60
  77. "After how many seconds not to expand anymore diffs.
  78. Except in status buffers, diffs are usually start out fully
  79. expanded. Because that can take a long time, all diffs that
  80. haven't been fontified during a refresh before the threshold
  81. defined here are instead displayed with their bodies collapsed.
  82. Note that this can cause sections that were previously expanded
  83. to be collapsed. So you should not pick a very low value here.
  84. The hook function `magit-diff-expansion-threshold' has to be a
  85. member of `magit-section-set-visibility-hook' for this option
  86. to have any effect."
  87. :package-version '(magit . "2.9.0")
  88. :group 'magit-diff
  89. :type 'float)
  90. (defcustom magit-diff-highlight-hunk-body t
  91. "Whether to highlight bodies of selected hunk sections.
  92. This only has an effect if `magit-diff-highlight' is a
  93. member of `magit-section-highlight-hook', which see."
  94. :package-version '(magit . "2.1.0")
  95. :group 'magit-diff
  96. :type 'boolean)
  97. (defcustom magit-diff-highlight-hunk-region-functions
  98. '(magit-diff-highlight-hunk-region-dim-outside
  99. magit-diff-highlight-hunk-region-using-overlays)
  100. "The functions used to highlight the hunk-internal region.
  101. `magit-diff-highlight-hunk-region-dim-outside' overlays the outside
  102. of the hunk internal selection with a face that causes the added and
  103. removed lines to have the same background color as context lines.
  104. This function should not be removed from the value of this option.
  105. `magit-diff-highlight-hunk-region-using-overlays' and
  106. `magit-diff-highlight-hunk-region-using-underline' emphasize the
  107. region by placing delimiting horizonal lines before and after it.
  108. The underline variant was implemented because Eli said that is
  109. how we should do it. However the overlay variant actually works
  110. better. Also see https://github.com/magit/magit/issues/2758.
  111. Instead of, or in addition to, using delimiting horizontal lines,
  112. to emphasize the boundaries, you may which to emphasize the text
  113. itself, using `magit-diff-highlight-hunk-region-using-face'.
  114. In terminal frames it's not possible to draw lines as the overlay
  115. and underline variants normally do, so there they fall back to
  116. calling the face function instead."
  117. :package-version '(magit . "2.9.0")
  118. :set-after '(magit-diff-show-lines-boundaries)
  119. :group 'magit-diff
  120. :type 'hook
  121. :options '(magit-diff-highlight-hunk-region-dim-outside
  122. magit-diff-highlight-hunk-region-using-underline
  123. magit-diff-highlight-hunk-region-using-overlays
  124. magit-diff-highlight-hunk-region-using-face))
  125. (defcustom magit-diff-unmarked-lines-keep-foreground t
  126. "Whether `magit-diff-highlight-hunk-region-dim-outside' preserves foreground.
  127. When this is set to nil, then that function only adjusts the
  128. foreground color but added and removed lines outside the region
  129. keep their distinct foreground colors."
  130. :package-version '(magit . "2.9.0")
  131. :group 'magit-diff
  132. :type 'boolean)
  133. (defcustom magit-diff-refine-hunk nil
  134. "Whether to show word-granularity differences within diff hunks.
  135. nil Never show fine differences.
  136. t Show fine differences for the current diff hunk only.
  137. `all' Show fine differences for all displayed diff hunks."
  138. :group 'magit-diff
  139. :safe (lambda (val) (memq val '(nil t all)))
  140. :type '(choice (const :tag "Never" nil)
  141. (const :tag "Current" t)
  142. (const :tag "All" all)))
  143. (defcustom magit-diff-refine-ignore-whitespace smerge-refine-ignore-whitespace
  144. "Whether to ignore whitespace changes in word-granularity differences."
  145. :package-version '(magit . "2.91.0")
  146. :set-after '(smerge-refine-ignore-whitespace)
  147. :group 'magit-diff
  148. :safe 'booleanp
  149. :type 'boolean)
  150. (put 'magit-diff-refine-hunk 'permanent-local t)
  151. (defcustom magit-diff-adjust-tab-width nil
  152. "Whether to adjust the width of tabs in diffs.
  153. Determining the correct width can be expensive if it requires
  154. opening large and/or many files, so the widths are cached in
  155. the variable `magit-diff--tab-width-cache'. Set that to nil
  156. to invalidate the cache.
  157. nil Never adjust tab width. Use `tab-width's value from
  158. the Magit buffer itself instead.
  159. t If the corresponding file-visiting buffer exits, then
  160. use `tab-width's value from that buffer. Doing this is
  161. cheap, so this value is used even if a corresponding
  162. cache entry exists.
  163. `always' If there is no such buffer, then temporarily visit the
  164. file to determine the value.
  165. NUMBER Like `always', but don't visit files larger than NUMBER
  166. bytes."
  167. :package-version '(magit . "2.12.0")
  168. :group 'magit-diff
  169. :type '(choice (const :tag "Never" nil)
  170. (const :tag "If file-visiting buffer exists" t)
  171. (integer :tag "If file isn't larger than N bytes")
  172. (const :tag "Always" always)))
  173. (defcustom magit-diff-paint-whitespace t
  174. "Specify where to highlight whitespace errors.
  175. nil Never highlight whitespace errors.
  176. t Highlight whitespace errors everywhere.
  177. `uncommitted' Only highlight whitespace errors in diffs
  178. showing uncommitted changes.
  179. For backward compatibility `status' is treated as a synonym
  180. for `uncommitted'.
  181. The option `magit-diff-paint-whitespace-lines' controls for
  182. what lines (added/remove/context) errors are highlighted.
  183. The options `magit-diff-highlight-trailing' and
  184. `magit-diff-highlight-indentation' control what kind of
  185. whitespace errors are highlighted."
  186. :group 'magit-diff
  187. :safe (lambda (val) (memq val '(t nil uncommitted status)))
  188. :type '(choice (const :tag "In all diffs" t)
  189. (const :tag "Only in uncommitted changes" uncommitted)
  190. (const :tag "Never" nil)))
  191. (defcustom magit-diff-paint-whitespace-lines t
  192. "Specify in what kind of lines to highlight whitespace errors.
  193. t Highlight only in added lines.
  194. `both' Highlight in added and removed lines.
  195. `all' Highlight in added, removed and context lines."
  196. :package-version '(magit . "2.91.0")
  197. :group 'magit-diff
  198. :safe (lambda (val) (memq val '(t both all)))
  199. :type '(choice (const :tag "in added lines" t)
  200. (const :tag "in added and removed lines" both)
  201. (const :tag "in added, removed and context lines" all)))
  202. (defcustom magit-diff-highlight-trailing t
  203. "Whether to highlight whitespace at the end of a line in diffs.
  204. Used only when `magit-diff-paint-whitespace' is non-nil."
  205. :group 'magit-diff
  206. :safe 'booleanp
  207. :type 'boolean)
  208. (defcustom magit-diff-highlight-indentation nil
  209. "Highlight the \"wrong\" indentation style.
  210. Used only when `magit-diff-paint-whitespace' is non-nil.
  211. The value is an alist of the form ((REGEXP . INDENT)...). The
  212. path to the current repository is matched against each element
  213. in reverse order. Therefore if a REGEXP matches, then earlier
  214. elements are not tried.
  215. If the used INDENT is `tabs', highlight indentation with tabs.
  216. If INDENT is an integer, highlight indentation with at least
  217. that many spaces. Otherwise, highlight neither."
  218. :group 'magit-diff
  219. :type `(repeat (cons (string :tag "Directory regexp")
  220. (choice (const :tag "Tabs" tabs)
  221. (integer :tag "Spaces" :value ,tab-width)
  222. (const :tag "Neither" nil)))))
  223. (defcustom magit-diff-hide-trailing-cr-characters
  224. (and (memq system-type '(ms-dos windows-nt)) t)
  225. "Whether to hide ^M characters at the end of a line in diffs."
  226. :package-version '(magit . "2.6.0")
  227. :group 'magit-diff
  228. :type 'boolean)
  229. (defcustom magit-diff-highlight-keywords t
  230. "Whether to highlight bracketed keywords in commit messages."
  231. :package-version '(magit . "2.12.0")
  232. :group 'magit-diff
  233. :type 'boolean)
  234. ;;;; File Diff
  235. (defcustom magit-diff-buffer-file-locked t
  236. "Whether `magit-diff-buffer-file' uses a dedicated buffer."
  237. :package-version '(magit . "2.7.0")
  238. :group 'magit-commands
  239. :group 'magit-diff
  240. :type 'boolean)
  241. ;;;; Revision Mode
  242. (defgroup magit-revision nil
  243. "Inspect and manipulate Git commits."
  244. :link '(info-link "(magit)Revision Buffer")
  245. :group 'magit-modes)
  246. (defcustom magit-revision-mode-hook '(bug-reference-mode)
  247. "Hook run after entering Magit-Revision mode."
  248. :group 'magit-revision
  249. :type 'hook
  250. :options '(bug-reference-mode))
  251. (defcustom magit-revision-sections-hook
  252. '(magit-insert-revision-tag
  253. magit-insert-revision-headers
  254. magit-insert-revision-message
  255. magit-insert-revision-notes
  256. magit-insert-revision-diff
  257. magit-insert-xref-buttons)
  258. "Hook run to insert sections into a `magit-revision-mode' buffer."
  259. :package-version '(magit . "2.3.0")
  260. :group 'magit-revision
  261. :type 'hook)
  262. (defcustom magit-revision-headers-format "\
  263. Author: %aN <%aE>
  264. AuthorDate: %ad
  265. Commit: %cN <%cE>
  266. CommitDate: %cd
  267. "
  268. "Format string used to insert headers in revision buffers.
  269. All headers in revision buffers are inserted by the section
  270. inserter `magit-insert-revision-headers'. Some of the headers
  271. are created by calling `git show --format=FORMAT' where FORMAT
  272. is the format specified here. Other headers are hard coded or
  273. subject to option `magit-revision-insert-related-refs'."
  274. :package-version '(magit . "2.3.0")
  275. :group 'magit-revision
  276. :type 'string)
  277. (defcustom magit-revision-insert-related-refs t
  278. "Whether to show related branches in revision buffers
  279. `nil' Don't show any related branches.
  280. `t' Show related local branches.
  281. `all' Show related local and remote branches.
  282. `mixed' Show all containing branches and local merged branches."
  283. :package-version '(magit . "2.1.0")
  284. :group 'magit-revision
  285. :type '(choice (const :tag "don't" nil)
  286. (const :tag "local only" t)
  287. (const :tag "all related" all)
  288. (const :tag "all containing, local merged" mixed)))
  289. (defcustom magit-revision-use-hash-sections 'quicker
  290. "Whether to turn hashes inside the commit message into sections.
  291. If non-nil, then hashes inside the commit message are turned into
  292. `commit' sections. There is a trade off to be made between
  293. performance and reliability:
  294. - `slow' calls git for every word to be absolutely sure.
  295. - `quick' skips words less than seven characters long.
  296. - `quicker' additionally skips words that don't contain a number.
  297. - `quickest' uses all words that are at least seven characters
  298. long and which contain at least one number as well as at least
  299. one letter.
  300. If nil, then no hashes are turned into sections, but you can
  301. still visit the commit at point using \"RET\"."
  302. :package-version '(magit . "2.12.0")
  303. :group 'magit-revision
  304. :type '(choice (const :tag "Use sections, quickest" quickest)
  305. (const :tag "Use sections, quicker" quicker)
  306. (const :tag "Use sections, quick" quick)
  307. (const :tag "Use sections, slow" slow)
  308. (const :tag "Don't use sections" nil)))
  309. (defcustom magit-revision-show-gravatars nil
  310. "Whether to show gravatar images in revision buffers.
  311. If nil, then don't insert any gravatar images. If t, then insert
  312. both images. If `author' or `committer', then insert only the
  313. respective image.
  314. If you have customized the option `magit-revision-header-format'
  315. and want to insert the images then you might also have to specify
  316. where to do so. In that case the value has to be a cons-cell of
  317. two regular expressions. The car specifies where to insert the
  318. author's image. The top half of the image is inserted right
  319. after the matched text, the bottom half on the next line in the
  320. same column. The cdr specifies where to insert the committer's
  321. image, accordingly. Either the car or the cdr may be nil."
  322. :package-version '(magit . "2.3.0")
  323. :group 'magit-revision
  324. :type '(choice (const :tag "Don't show gravatars" nil)
  325. (const :tag "Show gravatars" t)
  326. (const :tag "Show author gravatar" author)
  327. (const :tag "Show committer gravatar" committer)
  328. (cons :tag "Show gravatars using custom pattern."
  329. (regexp :tag "Author regexp" "^Author: ")
  330. (regexp :tag "Committer regexp" "^Commit: "))))
  331. (defcustom magit-revision-use-gravatar-kludge nil
  332. "Whether to work around a bug which affects display of gravatars.
  333. Gravatar images are spliced into two halves which are then
  334. displayed on separate lines. On OS X the splicing has a bug in
  335. some Emacs builds, which causes the top and bottom halves to be
  336. interchanged. Enabling this option works around this issue by
  337. interchanging the halves once more, which cancels out the effect
  338. of the bug.
  339. See https://github.com/magit/magit/issues/2265
  340. and https://debbugs.gnu.org/cgi/bugreport.cgi?bug=7847.
  341. Starting with Emacs 26.1 this kludge should not be required for
  342. any build."
  343. :package-version '(magit . "2.3.0")
  344. :group 'magit-revision
  345. :type 'boolean)
  346. (defcustom magit-revision-fill-summary-line nil
  347. "Whether to fill excessively long summary lines.
  348. If this is an integer, then the summary line is filled if it is
  349. longer than either the limit specified here or `window-width'.
  350. You may want to only set this locally in \".dir-locals-2.el\" for
  351. repositories known to contain bad commit messages.
  352. The body of the message is left alone because (a) most people who
  353. write excessively long summary lines usually don't add a body and
  354. (b) even people who have the decency to wrap their lines may have
  355. a good reason to include a long line in the body sometimes."
  356. :package-version '(magit . "2.90.0")
  357. :group 'magit-revision
  358. :type '(choice (const :tag "Don't fill" nil)
  359. (integer :tag "Fill if longer than")))
  360. (defcustom magit-revision-filter-files-on-follow nil
  361. "Whether to honor file filter if log arguments include --follow.
  362. When a commit is displayed from a log buffer, the resulting
  363. revision buffer usually shares the log's file arguments,
  364. restricting the diff to those files. However, there's a
  365. complication when the log arguments include --follow: if the log
  366. follows a file across a rename event, keeping the file
  367. restriction would mean showing an empty diff in revision buffers
  368. for commits before the rename event.
  369. When this option is nil, the revision buffer ignores the log's
  370. filter if the log arguments include --follow. If non-nil, the
  371. log's file filter is always honored."
  372. :package-version '(magit . "2.91.0")
  373. :group 'magit-revision
  374. :type 'boolean)
  375. ;;;; Visit Commands
  376. (defcustom magit-diff-visit-previous-blob t
  377. "Whether `magit-diff-visit-file' may visit the previous blob.
  378. When this is t and point is on a removed line in a diff for a
  379. committed change, then `magit-diff-visit-file' visits the blob
  380. from the last revision which still had that line.
  381. Currently this is only supported for committed changes, for
  382. staged and unstaged changes `magit-diff-visit-file' always
  383. visits the file in the working tree."
  384. :package-version '(magit . "2.9.0")
  385. :group 'magit-diff
  386. :type 'boolean)
  387. (defcustom magit-diff-visit-avoid-head-blob nil
  388. "Whether `magit-diff-visit-file' avoids visiting a blob from `HEAD'.
  389. By default `magit-diff-visit-file' always visits the blob that
  390. added the current line, while `magit-diff-visit-file-worktree'
  391. visits the respective file in the working tree. For the `HEAD'
  392. commit, the former command used to visit the worktree file too,
  393. but that made it impossible to visit a blob from `HEAD'.
  394. If you prefer the old behavior, then set this to t."
  395. :package-version '(magit . "2.91.0")
  396. :group 'magit-diff
  397. :type 'boolean)
  398. ;;; Faces
  399. (defface magit-diff-file-heading
  400. '((t :weight bold))
  401. "Face for diff file headings."
  402. :group 'magit-faces)
  403. (defface magit-diff-file-heading-highlight
  404. '((t :inherit (magit-section-highlight)))
  405. "Face for current diff file headings."
  406. :group 'magit-faces)
  407. (defface magit-diff-file-heading-selection
  408. '((((class color) (background light))
  409. :inherit magit-diff-file-heading-highlight
  410. :foreground "salmon4")
  411. (((class color) (background dark))
  412. :inherit magit-diff-file-heading-highlight
  413. :foreground "LightSalmon3"))
  414. "Face for selected diff file headings."
  415. :group 'magit-faces)
  416. (defface magit-diff-hunk-heading
  417. '((((class color) (background light))
  418. :background "grey80"
  419. :foreground "grey30")
  420. (((class color) (background dark))
  421. :background "grey25"
  422. :foreground "grey70"))
  423. "Face for diff hunk headings."
  424. :group 'magit-faces)
  425. (defface magit-diff-hunk-heading-highlight
  426. '((((class color) (background light))
  427. :background "grey75"
  428. :foreground "grey30")
  429. (((class color) (background dark))
  430. :background "grey35"
  431. :foreground "grey70"))
  432. "Face for current diff hunk headings."
  433. :group 'magit-faces)
  434. (defface magit-diff-hunk-heading-selection
  435. '((((class color) (background light))
  436. :inherit magit-diff-hunk-heading-highlight
  437. :foreground "salmon4")
  438. (((class color) (background dark))
  439. :inherit magit-diff-hunk-heading-highlight
  440. :foreground "LightSalmon3"))
  441. "Face for selected diff hunk headings."
  442. :group 'magit-faces)
  443. (defface magit-diff-hunk-region
  444. '((t :inherit bold))
  445. "Face used by `magit-diff-highlight-hunk-region-using-face'.
  446. This face is overlayed over text that uses other hunk faces,
  447. and those normally set the foreground and background colors.
  448. The `:foreground' and especially the `:background' properties
  449. should be avoided here. Setting the latter would cause the
  450. lose of information. Good properties to set here are `:weight'
  451. and `:slant'."
  452. :group 'magit-faces)
  453. (defface magit-diff-lines-heading
  454. '((((class color) (background light))
  455. :inherit magit-diff-hunk-heading-highlight
  456. :background "LightSalmon3")
  457. (((class color) (background dark))
  458. :inherit magit-diff-hunk-heading-highlight
  459. :foreground "grey80"
  460. :background "salmon4"))
  461. "Face for diff hunk heading when lines are marked."
  462. :group 'magit-faces)
  463. (defface magit-diff-lines-boundary
  464. '((t :inherit magit-diff-lines-heading))
  465. "Face for boundary of marked lines in diff hunk."
  466. :group 'magit-faces)
  467. (defface magit-diff-conflict-heading
  468. '((t :inherit magit-diff-hunk-heading))
  469. "Face for conflict markers."
  470. :group 'magit-faces)
  471. (defface magit-diff-added
  472. '((((class color) (background light))
  473. :background "#ddffdd"
  474. :foreground "#22aa22")
  475. (((class color) (background dark))
  476. :background "#335533"
  477. :foreground "#ddffdd"))
  478. "Face for lines in a diff that have been added."
  479. :group 'magit-faces)
  480. (defface magit-diff-removed
  481. '((((class color) (background light))
  482. :background "#ffdddd"
  483. :foreground "#aa2222")
  484. (((class color) (background dark))
  485. :background "#553333"
  486. :foreground "#ffdddd"))
  487. "Face for lines in a diff that have been removed."
  488. :group 'magit-faces)
  489. (defface magit-diff-our
  490. '((t :inherit magit-diff-removed))
  491. "Face for lines in a diff for our side in a conflict."
  492. :group 'magit-faces)
  493. (defface magit-diff-base
  494. '((((class color) (background light))
  495. :background "#ffffcc"
  496. :foreground "#aaaa11")
  497. (((class color) (background dark))
  498. :background "#555522"
  499. :foreground "#ffffcc"))
  500. "Face for lines in a diff for the base side in a conflict."
  501. :group 'magit-faces)
  502. (defface magit-diff-their
  503. '((t :inherit magit-diff-added))
  504. "Face for lines in a diff for their side in a conflict."
  505. :group 'magit-faces)
  506. (defface magit-diff-context
  507. '((((class color) (background light)) :foreground "grey50")
  508. (((class color) (background dark)) :foreground "grey70"))
  509. "Face for lines in a diff that are unchanged."
  510. :group 'magit-faces)
  511. (defface magit-diff-added-highlight
  512. '((((class color) (background light))
  513. :background "#cceecc"
  514. :foreground "#22aa22")
  515. (((class color) (background dark))
  516. :background "#336633"
  517. :foreground "#cceecc"))
  518. "Face for lines in a diff that have been added."
  519. :group 'magit-faces)
  520. (defface magit-diff-removed-highlight
  521. '((((class color) (background light))
  522. :background "#eecccc"
  523. :foreground "#aa2222")
  524. (((class color) (background dark))
  525. :background "#663333"
  526. :foreground "#eecccc"))
  527. "Face for lines in a diff that have been removed."
  528. :group 'magit-faces)
  529. (defface magit-diff-our-highlight
  530. '((t :inherit magit-diff-removed-highlight))
  531. "Face for lines in a diff for our side in a conflict."
  532. :group 'magit-faces)
  533. (defface magit-diff-base-highlight
  534. '((((class color) (background light))
  535. :background "#eeeebb"
  536. :foreground "#aaaa11")
  537. (((class color) (background dark))
  538. :background "#666622"
  539. :foreground "#eeeebb"))
  540. "Face for lines in a diff for the base side in a conflict."
  541. :group 'magit-faces)
  542. (defface magit-diff-their-highlight
  543. '((t :inherit magit-diff-added-highlight))
  544. "Face for lines in a diff for their side in a conflict."
  545. :group 'magit-faces)
  546. (defface magit-diff-context-highlight
  547. '((((class color) (background light))
  548. :background "grey95"
  549. :foreground "grey50")
  550. (((class color) (background dark))
  551. :background "grey20"
  552. :foreground "grey70"))
  553. "Face for lines in the current context in a diff."
  554. :group 'magit-faces)
  555. (defface magit-diff-whitespace-warning
  556. '((t :inherit trailing-whitespace))
  557. "Face for highlighting whitespace errors added lines."
  558. :group 'magit-faces)
  559. (defface magit-diffstat-added
  560. '((((class color) (background light)) :foreground "#22aa22")
  561. (((class color) (background dark)) :foreground "#448844"))
  562. "Face for plus sign in diffstat."
  563. :group 'magit-faces)
  564. (defface magit-diffstat-removed
  565. '((((class color) (background light)) :foreground "#aa2222")
  566. (((class color) (background dark)) :foreground "#aa4444"))
  567. "Face for minus sign in diffstat."
  568. :group 'magit-faces)
  569. ;;; Arguments
  570. ;;;; Prefix Classes
  571. (defclass magit-diff-prefix (transient-prefix)
  572. ((history-key :initform 'magit-diff)
  573. (major-mode :initform 'magit-diff-mode)))
  574. (defclass magit-diff-refresh-prefix (magit-diff-prefix)
  575. ((history-key :initform 'magit-diff)
  576. (major-mode :initform nil)))
  577. ;;;; Prefix Methods
  578. (cl-defmethod transient-init-value ((obj magit-diff-prefix))
  579. (pcase-let ((`(,args ,files)
  580. (magit-diff--get-value 'magit-diff-mode
  581. magit-prefix-use-buffer-arguments)))
  582. (unless (eq current-transient-command 'magit-dispatch)
  583. (when-let ((file (magit-file-relative-name)))
  584. (setq files (list file))))
  585. (oset obj value (if files `(("--" ,@files) ,args) args))))
  586. (cl-defmethod transient-init-value ((obj magit-diff-refresh-prefix))
  587. (oset obj value (if magit-buffer-diff-files
  588. `(("--" ,@magit-buffer-diff-files)
  589. ,magit-buffer-diff-args)
  590. magit-buffer-diff-args)))
  591. (cl-defmethod transient-set-value ((obj magit-diff-prefix))
  592. (magit-diff--set-value obj))
  593. (cl-defmethod transient-save-value ((obj magit-diff-prefix))
  594. (magit-diff--set-value obj 'save))
  595. ;;;; Argument Access
  596. (defun magit-diff-arguments (&optional mode)
  597. "Return the current diff arguments."
  598. (if (memq current-transient-command '(magit-diff magit-diff-refresh))
  599. (pcase-let ((`(,args ,alist)
  600. (transient-args nil t)))
  601. (list args (cdr (assoc "--" alist))))
  602. (magit-diff--get-value (or mode 'magit-diff-mode))))
  603. (defun magit-diff--get-value (mode &optional use-buffer-args)
  604. (unless use-buffer-args
  605. (setq use-buffer-args magit-direct-use-buffer-arguments))
  606. (let (args files)
  607. (cond
  608. ((and (memq use-buffer-args '(always selected current))
  609. (eq major-mode mode))
  610. (setq args magit-buffer-diff-args)
  611. (setq files magit-buffer-diff-files))
  612. ((and (memq use-buffer-args '(always selected))
  613. (when-let ((buffer (magit-get-mode-buffer
  614. mode nil
  615. (or (eq use-buffer-args 'selected) 'all))))
  616. (setq args (buffer-local-value 'magit-buffer-diff-args buffer))
  617. (setq files (buffer-local-value 'magit-buffer-diff-files buffer))
  618. t)))
  619. ((plist-member (symbol-plist mode) 'magit-diff-current-arguments)
  620. (setq args (get mode 'magit-diff-current-arguments)))
  621. ((when-let ((elt (assq (intern (format "magit-diff:%s" mode))
  622. transient-values)))
  623. (setq args (cdr elt))
  624. t))
  625. (t
  626. (setq args (get mode 'magit-diff-default-arguments))))
  627. (list args files)))
  628. (defun magit-diff--set-value (obj &optional save)
  629. (pcase-let* ((obj (oref obj prototype))
  630. (mode (or (oref obj major-mode) major-mode))
  631. (key (intern (format "magit-diff:%s" mode)))
  632. (`(,args ,alist)
  633. (-separate #'atom (transient-args)))
  634. (files (cdr (assoc "--" alist))))
  635. (put mode 'magit-diff-current-arguments args)
  636. (when save
  637. (setf (alist-get key transient-values) args)
  638. (transient-save-values))
  639. (transient--history-push obj)
  640. (setq magit-buffer-diff-args args)
  641. (setq magit-buffer-diff-files files)
  642. (magit-refresh)))
  643. ;;; Commands
  644. ;;;; Prefix Commands
  645. ;;;###autoload (autoload 'magit-diff "magit-diff" nil t)
  646. (define-transient-command magit-diff ()
  647. "Show changes between different versions."
  648. :man-page "git-diff"
  649. :class 'magit-diff-prefix
  650. ["Limit arguments"
  651. (magit:--)
  652. (magit-diff:--ignore-submodules)
  653. ("-b" "Ignore whitespace changes" ("-b" "--ignore-space-change"))
  654. ("-w" "Ignore all whitespace" ("-w" "--ignore-all-space"))]
  655. ["Context arguments"
  656. (magit-diff:-U)
  657. ("-W" "Show surrounding functions" ("-W" "--function-context"))]
  658. ["Tune arguments"
  659. (magit-diff:--diff-algorithm)
  660. (magit-diff:-M)
  661. (magit-diff:-C)
  662. ("-x" "Disallow external diff drivers" "--no-ext-diff")
  663. ("-s" "Show stats" "--stat")
  664. (5 magit-diff:--color-moved)
  665. (5 magit-diff:--color-moved-ws)]
  666. ["Actions"
  667. [("d" "Dwim" magit-diff-dwim)
  668. ("r" "Diff range" magit-diff-range)
  669. ("p" "Diff paths" magit-diff-paths)]
  670. [("u" "Diff unstaged" magit-diff-unstaged)
  671. ("s" "Diff staged" magit-diff-staged)
  672. ("w" "Diff worktree" magit-diff-working-tree)]
  673. [("c" "Show commit" magit-show-commit)
  674. ("t" "Show stash" magit-stash-show)]])
  675. ;;;###autoload (autoload 'magit-diff-refresh "magit-diff" nil t)
  676. (define-transient-command magit-diff-refresh ()
  677. "Change the arguments used for the diff(s) in the current buffer."
  678. :man-page "git-diff"
  679. :class 'magit-diff-refresh-prefix
  680. ["Limit arguments"
  681. (magit:--)
  682. (magit-diff:--ignore-submodules)
  683. ("-b" "Ignore whitespace changes" ("-b" "--ignore-space-change"))
  684. ("-w" "Ignore all whitespace" ("-w" "--ignore-all-space"))]
  685. ["Context arguments"
  686. (magit-diff:-U)
  687. ("-W" "Show surrounding functions" ("-W" "--function-context"))]
  688. ["Tune arguments"
  689. (magit-diff:--diff-algorithm)
  690. (magit-diff:-M)
  691. (magit-diff:-C)
  692. ("-x" "Disallow external diff drivers" "--no-ext-diff")
  693. ("-s" "Show stats" "--stat"
  694. :if-derived magit-diff-mode)
  695. (5 magit-diff:--color-moved)
  696. (5 magit-diff:--color-moved-ws)]
  697. [["Refresh"
  698. ("g" "buffer" magit-diff-refresh)
  699. ("s" "buffer and set defaults" transient-set :transient nil)
  700. ("w" "buffer and save defaults" transient-save :transient nil)]
  701. ["Toggle"
  702. ("t" "hunk refinement" magit-diff-toggle-refine-hunk)
  703. ("F" "file filter" magit-diff-toggle-file-filter)
  704. ("b" "buffer lock" magit-toggle-buffer-lock
  705. :if-mode (magit-diff-mode magit-revision-mode magit-stash-mode))]
  706. [:if-mode magit-diff-mode
  707. :description "Do"
  708. ("r" "switch range type" magit-diff-switch-range-type)
  709. ("f" "flip revisions" magit-diff-flip-revs)]]
  710. (interactive)
  711. (if (not (eq current-transient-command 'magit-diff-refresh))
  712. (transient-setup 'magit-diff-refresh)
  713. (pcase-let ((`(,args ,files) (magit-diff-arguments)))
  714. (setq magit-buffer-diff-args args)
  715. (setq magit-buffer-diff-files files))
  716. (magit-refresh)))
  717. ;;;; Infix Commands
  718. (define-infix-argument magit:-- ()
  719. :description "Limit to files"
  720. :class 'transient-files
  721. :key "--"
  722. :argument "--"
  723. :prompt "Limit to file(s): "
  724. :reader 'magit-read-files
  725. :multi-value t)
  726. (defun magit-read-files (prompt initial-input history)
  727. (magit-completing-read-multiple* prompt
  728. (magit-list-files)
  729. nil nil initial-input history))
  730. (define-infix-argument magit-diff:-U ()
  731. :description "Context lines"
  732. :class 'transient-option
  733. :argument "-U"
  734. :reader 'transient-read-number-N+)
  735. (define-infix-argument magit-diff:-M ()
  736. :description "Detect renames"
  737. :class 'transient-option
  738. :argument "-M"
  739. :reader 'transient-read-number-N+)
  740. (define-infix-argument magit-diff:-C ()
  741. :description "Detect copies"
  742. :class 'transient-option
  743. :argument "-C"
  744. :reader 'transient-read-number-N+)
  745. (define-infix-argument magit-diff:--diff-algorithm ()
  746. :description "Diff algorithm"
  747. :class 'transient-option
  748. :key "-A"
  749. :argument "--diff-algorithm="
  750. :reader 'magit-diff-select-algorithm)
  751. (defun magit-diff-select-algorithm (&rest _ignore)
  752. (magit-read-char-case nil t
  753. (?d "[d]efault" "default")
  754. (?m "[m]inimal" "minimal")
  755. (?p "[p]atience" "patience")
  756. (?h "[h]istogram" "histogram")))
  757. (define-infix-argument magit-diff:--ignore-submodules ()
  758. :description "Ignore submodules"
  759. :class 'transient-option
  760. :key "-i"
  761. :argument "--ignore-submodules="
  762. :reader 'magit-diff-select-ignore-submodules)
  763. (defun magit-diff-select-ignore-submodules (&rest _ignored)
  764. (magit-read-char-case "Ignore submodules " t
  765. (?u "[u]ntracked" "untracked")
  766. (?d "[d]irty" "dirty")
  767. (?a "[a]ll" "all")))
  768. (define-infix-argument magit-diff:--color-moved ()
  769. :description "Color moved lines"
  770. :class 'transient-option
  771. :key "-m"
  772. :argument "--color-moved="
  773. :reader 'magit-diff-select-color-moved-mode)
  774. (defun magit-diff-select-color-moved-mode (&rest _ignore)
  775. (magit-read-char-case "Color moved " t
  776. (?d "[d]efault" "default")
  777. (?p "[p]lain" "plain")
  778. (?b "[b]locks" "blocks")
  779. (?z "[z]ebra" "zebra")
  780. (?Z "[Z] dimmed-zebra" "dimmed-zebra")))
  781. (define-infix-argument magit-diff:--color-moved-ws ()
  782. :description "Whitespace treatment for --color-moved"
  783. :class 'transient-option
  784. :key "=w"
  785. :argument "--color-moved-ws="
  786. :reader 'magit-diff-select-color-moved-ws-mode)
  787. (defun magit-diff-select-color-moved-ws-mode (&rest _ignore)
  788. (magit-read-char-case "Ignore whitespace " t
  789. (?i "[i]ndentation" "allow-indentation-change")
  790. (?e "[e]nd of line" "ignore-space-at-eol")
  791. (?s "[s]pace change" "ignore-space-change")
  792. (?a "[a]ll space" "ignore-all-space")
  793. (?n "[n]o" "no")))
  794. ;;;; Setup Commands
  795. ;;;###autoload
  796. (defun magit-diff-dwim (&optional args files)
  797. "Show changes for the thing at point."
  798. (interactive (magit-diff-arguments))
  799. (pcase (magit-diff--dwim)
  800. (`unmerged (magit-diff-unmerged args files))
  801. (`unstaged (magit-diff-unstaged args files))
  802. (`staged
  803. (let ((file (magit-file-at-point)))
  804. (if (and file (equal (cddr (car (magit-file-status file))) '(?D ?U)))
  805. ;; File was deleted by us and modified by them. Show the latter.
  806. (magit-diff-unmerged args (list file))
  807. (magit-diff-staged nil args files))))
  808. (`(commit . ,value)
  809. (magit-diff-range (format "%s^..%s" value value) args files))
  810. (`(stash . ,value) (magit-stash-show value args))
  811. ((and range (pred stringp))
  812. (magit-diff-range range args files))
  813. (_
  814. (call-interactively #'magit-diff-range))))
  815. (defun magit-diff--dwim ()
  816. "Return information for performing DWIM diff.
  817. The information can be in three forms:
  818. 1. TYPE
  819. A symbol describing a type of diff where no additional information
  820. is needed to generate the diff. Currently, this includes `staged',
  821. `unstaged' and `unmerged'.
  822. 2. (TYPE . VALUE)
  823. Like #1 but the diff requires additional information, which is
  824. given by VALUE. Currently, this includes `commit' and `stash',
  825. where VALUE is the given commit or stash, respectively.
  826. 3. RANGE
  827. A string indicating a diff range.
  828. If no DWIM context is found, nil is returned."
  829. (cond
  830. ((--when-let (magit-region-values '(commit branch) t)
  831. (deactivate-mark)
  832. (concat (car (last it)) ".." (car it))))
  833. (magit-buffer-refname
  834. (cons 'commit magit-buffer-refname))
  835. ((derived-mode-p 'magit-stash-mode)
  836. (cons 'commit
  837. (magit-section-case
  838. (commit (oref it value))
  839. (file (-> it
  840. (oref parent)
  841. (oref value)))
  842. (hunk (-> it
  843. (oref parent)
  844. (oref parent)
  845. (oref value))))))
  846. ((derived-mode-p 'magit-revision-mode)
  847. (cons 'commit magit-buffer-revision))
  848. ((derived-mode-p 'magit-diff-mode)
  849. magit-buffer-range)
  850. (t
  851. (magit-section-case
  852. ([* unstaged] 'unstaged)
  853. ([* staged] 'staged)
  854. (unmerged 'unmerged)
  855. (unpushed (oref it value))
  856. (unpulled (oref it value))
  857. (branch (let ((current (magit-get-current-branch))
  858. (atpoint (oref it value)))
  859. (if (equal atpoint current)
  860. (--if-let (magit-get-upstream-branch)
  861. (format "%s...%s" it current)
  862. (if (magit-anything-modified-p)
  863. current
  864. (cons 'commit current)))
  865. (format "%s...%s"
  866. (or current "HEAD")
  867. atpoint))))
  868. (commit (cons 'commit (oref it value)))
  869. (stash (cons 'stash (oref it value)))
  870. (pullreq (let ((pullreq (oref it value)))
  871. (format "%s...%s"
  872. (oref pullreq base-ref)
  873. (forge--pullreq-ref pullreq))))))))
  874. (defun magit-diff-read-range-or-commit (prompt &optional secondary-default mbase)
  875. "Read range or revision with special diff range treatment.
  876. If MBASE is non-nil, prompt for which rev to place at the end of
  877. a \"revA...revB\" range. Otherwise, always construct
  878. \"revA..revB\" range."
  879. (--if-let (magit-region-values '(commit branch) t)
  880. (let ((revA (car (last it)))
  881. (revB (car it)))
  882. (deactivate-mark)
  883. (if mbase
  884. (let ((base (magit-git-string "merge-base" revA revB)))
  885. (cond
  886. ((string= (magit-rev-parse revA) base)
  887. (format "%s..%s" revA revB))
  888. ((string= (magit-rev-parse revB) base)
  889. (format "%s..%s" revB revA))
  890. (t
  891. (let ((main (magit-completing-read "View changes along"
  892. (list revA revB)
  893. nil t nil nil revB)))
  894. (format "%s...%s"
  895. (if (string= main revB) revA revB) main)))))
  896. (format "%s..%s" revA revB)))
  897. (magit-read-range prompt
  898. (or (pcase (magit-diff--dwim)
  899. (`(commit . ,value)
  900. (format "%s^..%s" value value))
  901. ((and range (pred stringp))
  902. range))
  903. secondary-default
  904. (magit-get-current-branch)))))
  905. ;;;###autoload
  906. (defun magit-diff-range (rev-or-range &optional args files)
  907. "Show differences between two commits.
  908. REV-OR-RANGE should be a range or a single revision. If it is a
  909. revision, then show changes in the working tree relative to that
  910. revision. If it is a range, but one side is omitted, then show
  911. changes relative to `HEAD'.
  912. If the region is active, use the revisions on the first and last
  913. line of the region as the two sides of the range. With a prefix
  914. argument, instead of diffing the revisions, choose a revision to
  915. view changes along, starting at the common ancestor of both
  916. revisions (i.e., use a \"...\" range)."
  917. (interactive (cons (magit-diff-read-range-or-commit "Diff for range"
  918. nil current-prefix-arg)
  919. (magit-diff-arguments)))
  920. (magit-diff-setup-buffer rev-or-range nil args files))
  921. ;;;###autoload
  922. (defun magit-diff-working-tree (&optional rev args files)
  923. "Show changes between the current working tree and the `HEAD' commit.
  924. With a prefix argument show changes between the working tree and
  925. a commit read from the minibuffer."
  926. (interactive
  927. (cons (and current-prefix-arg
  928. (magit-read-branch-or-commit "Diff working tree and commit"))
  929. (magit-diff-arguments)))
  930. (magit-diff-setup-buffer (or rev "HEAD") nil args files))
  931. ;;;###autoload
  932. (defun magit-diff-staged (&optional rev args files)
  933. "Show changes between the index and the `HEAD' commit.
  934. With a prefix argument show changes between the index and
  935. a commit read from the minibuffer."
  936. (interactive
  937. (cons (and current-prefix-arg
  938. (magit-read-branch-or-commit "Diff index and commit"))
  939. (magit-diff-arguments)))
  940. (magit-diff-setup-buffer rev "--cached" args files))
  941. ;;;###autoload
  942. (defun magit-diff-unstaged (&optional args files)
  943. "Show changes between the working tree and the index."
  944. (interactive (magit-diff-arguments))
  945. (magit-diff-setup-buffer nil nil args files))
  946. ;;;###autoload
  947. (defun magit-diff-unmerged (&optional args files)
  948. "Show changes that are being merged."
  949. (interactive (magit-diff-arguments))
  950. (unless (magit-merge-in-progress-p)
  951. (user-error "No merge is in progress"))
  952. (magit-diff-setup-buffer (magit--merge-range) nil args files))
  953. ;;;###autoload
  954. (defun magit-diff-while-committing (&optional args)
  955. "While committing, show the changes that are about to be committed.
  956. While amending, invoking the command again toggles between
  957. showing just the new changes or all the changes that will
  958. be committed."
  959. (interactive (list (car (magit-diff-arguments))))
  960. (unless (magit-commit-message-buffer)
  961. (user-error "No commit in progress"))
  962. (let ((magit-display-buffer-noselect t))
  963. (if-let ((diff-buf (magit-get-mode-buffer 'magit-diff-mode 'selected)))
  964. (with-current-buffer diff-buf
  965. (cond ((and (equal magit-buffer-range "HEAD^")
  966. (equal magit-buffer-typearg "--cached"))
  967. (magit-diff-staged nil args))
  968. ((and (equal magit-buffer-range nil)
  969. (equal magit-buffer-typearg "--cached"))
  970. (magit-diff-while-amending args))
  971. ((magit-anything-staged-p)
  972. (magit-diff-staged nil args))
  973. (t
  974. (magit-diff-while-amending args))))
  975. (if (magit-anything-staged-p)
  976. (magit-diff-staged nil args)
  977. (magit-diff-while-amending args)))))
  978. (define-key git-commit-mode-map
  979. (kbd "C-c C-d") 'magit-diff-while-committing)
  980. (defun magit-diff-while-amending (&optional args)
  981. (magit-diff-setup-buffer "HEAD^" "--cached" args nil))
  982. ;;;###autoload
  983. (defun magit-diff-buffer-file ()
  984. "Show diff for the blob or file visited in the current buffer.
  985. When the buffer visits a blob, then show the respective commit.
  986. When the buffer visits a file, then show the differenced between
  987. `HEAD' and the working tree. In both cases limit the diff to
  988. the file or blob."
  989. (interactive)
  990. (require 'magit)
  991. (if-let ((file (magit-file-relative-name)))
  992. (if magit-buffer-refname
  993. (magit-show-commit magit-buffer-refname
  994. (car (magit-show-commit--arguments))
  995. (list file))
  996. (save-buffer)
  997. (let ((line (line-number-at-pos))
  998. (col (current-column)))
  999. (with-current-buffer
  1000. (magit-diff-setup-buffer (or (magit-get-current-branch) "HEAD")
  1001. nil
  1002. (car (magit-diff-arguments))
  1003. (list file)
  1004. magit-diff-buffer-file-locked)
  1005. (magit-diff--goto-position file line col))))
  1006. (user-error "Buffer isn't visiting a file")))
  1007. ;;;###autoload
  1008. (defun magit-diff-paths (a b)
  1009. "Show changes between any two files on disk."
  1010. (interactive (list (read-file-name "First file: " nil nil t)
  1011. (read-file-name "Second file: " nil nil t)))
  1012. (magit-diff-setup-buffer nil "--no-index"
  1013. nil (list (magit-convert-filename-for-git
  1014. (expand-file-name a))
  1015. (magit-convert-filename-for-git
  1016. (expand-file-name b)))))
  1017. (defun magit-show-commit--arguments ()
  1018. (pcase-let ((`(,args ,diff-files)
  1019. (magit-diff-arguments 'magit-revision-mode)))
  1020. (list args (if (derived-mode-p 'magit-log-mode)
  1021. (and (or magit-revision-filter-files-on-follow
  1022. (not (member "--follow" magit-buffer-log-args)))
  1023. magit-buffer-log-files)
  1024. diff-files))))
  1025. ;;;###autoload
  1026. (defun magit-show-commit (rev &optional args files module)
  1027. "Visit the revision at point in another buffer.
  1028. If there is no revision at point or with a prefix argument prompt
  1029. for a revision."
  1030. (interactive
  1031. (pcase-let* ((mcommit (magit-section-value-if 'module-commit))
  1032. (atpoint (or (and (bound-and-true-p magit-blame-mode)
  1033. (oref (magit-current-blame-chunk) orig-rev))
  1034. mcommit
  1035. (magit-branch-or-commit-at-point)))
  1036. (`(,args ,files) (magit-show-commit--arguments)))
  1037. (list (or (and (not current-prefix-arg) atpoint)
  1038. (magit-read-branch-or-commit "Show commit" atpoint))
  1039. args
  1040. files
  1041. (and mcommit
  1042. (magit-section-parent-value (magit-current-section))))))
  1043. (require 'magit)
  1044. (let ((file (magit-file-relative-name)))
  1045. (magit-with-toplevel
  1046. (when module
  1047. (setq default-directory
  1048. (expand-file-name (file-name-as-directory module))))
  1049. (unless (magit-commit-p rev)
  1050. (user-error "%s is not a commit" rev))
  1051. (let ((buf (magit-revision-setup-buffer rev args files)))
  1052. (when file
  1053. (save-buffer)
  1054. (let ((line (magit-diff-visit--offset file (list "-R" rev)
  1055. (line-number-at-pos)))
  1056. (col (current-column)))
  1057. (with-current-buffer buf
  1058. (magit-diff--goto-position file line col))))))))
  1059. (defun magit-diff--locate-hunk (file line &optional parent)
  1060. (when-let ((diff (cl-find-if (lambda (section)
  1061. (and (cl-typep section 'magit-file-section)
  1062. (equal (oref section value) file)))
  1063. (oref (or parent magit-root-section) children))))
  1064. (let (hunk (hunks (oref diff children)))
  1065. (cl-block nil
  1066. (while (setq hunk (pop hunks))
  1067. (pcase-let* ((`(,beg ,len) (oref hunk to-range))
  1068. (end (+ beg len)))
  1069. (cond ((> beg line) (cl-return (list diff nil)))
  1070. ((<= beg line end) (cl-return (list hunk t)))
  1071. ((null hunks) (cl-return (list hunk nil))))))))))
  1072. (defun magit-diff--goto-position (file line column &optional parent)
  1073. (when-let ((pos (magit-diff--locate-hunk file line parent)))
  1074. (pcase-let ((`(,section ,exact) pos))
  1075. (cond ((cl-typep section 'magit-file-section)
  1076. (goto-char (oref section start)))
  1077. (exact
  1078. (goto-char (oref section content))
  1079. (let ((pos (car (oref section to-range))))
  1080. (while (or (< pos line)
  1081. (= (char-after) ?-))
  1082. (unless (= (char-after) ?-)
  1083. (cl-incf pos))
  1084. (forward-line)))
  1085. (forward-char (1+ column)))
  1086. (t
  1087. (goto-char (oref section start))
  1088. (setq section (oref section parent))))
  1089. (while section
  1090. (when (oref section hidden)
  1091. (magit-section-show section))
  1092. (setq section (oref section parent))))
  1093. (magit-section-update-highlight)
  1094. t))
  1095. (cl-defmethod magit-buffer-value (&context (major-mode magit-revision-mode))
  1096. (cons magit-buffer-range magit-buffer-diff-files))
  1097. ;;;; Setting Commands
  1098. (defun magit-diff-switch-range-type ()
  1099. "Convert diff range type.
  1100. Change \"revA..revB\" to \"revA...revB\", or vice versa."
  1101. (interactive)
  1102. (if (and magit-buffer-range
  1103. (derived-mode-p 'magit-diff-mode)
  1104. (string-match magit-range-re magit-buffer-range))
  1105. (setq magit-buffer-range
  1106. (replace-match (if (string= (match-string 2 magit-buffer-range) "..")
  1107. "..."
  1108. "..")
  1109. t t magit-buffer-range 2))
  1110. (user-error "No range to change"))
  1111. (magit-refresh))
  1112. (defun magit-diff-flip-revs ()
  1113. "Swap revisions in diff range.
  1114. Change \"revA..revB\" to \"revB..revA\"."
  1115. (interactive)
  1116. (if (and magit-buffer-range
  1117. (derived-mode-p 'magit-diff-mode)
  1118. (string-match magit-range-re magit-buffer-range))
  1119. (progn
  1120. (setq magit-buffer-range
  1121. (concat (match-string 3 magit-buffer-range)
  1122. (match-string 2 magit-buffer-range)
  1123. (match-string 1 magit-buffer-range)))
  1124. (magit-refresh))
  1125. (user-error "No range to swap")))
  1126. (defun magit-diff-toggle-file-filter ()
  1127. "Toggle the file restriction of the current buffer's diffs.
  1128. If the current buffer's mode is derived from `magit-log-mode',
  1129. toggle the file restriction in the repository's revision buffer
  1130. instead."
  1131. (interactive)
  1132. (cl-flet ((toggle ()
  1133. (if (or magit-buffer-diff-files
  1134. magit-buffer-diff-files-suspended)
  1135. (cl-rotatef magit-buffer-diff-files
  1136. magit-buffer-diff-files-suspended)
  1137. (setq magit-buffer-diff-files
  1138. (magit-read-files "Limit to file(s): "
  1139. (magit-file-at-point)
  1140. nil)))
  1141. (magit-refresh)))
  1142. (cond
  1143. ((derived-mode-p 'magit-log-mode
  1144. 'magit-cherry-mode
  1145. 'magit-reflog-mode)
  1146. (if-let ((buffer (magit-get-mode-buffer 'magit-revision-mode)))
  1147. (with-current-buffer buffer (toggle))
  1148. (message "No revision buffer")))
  1149. ((local-variable-p 'magit-buffer-diff-files)
  1150. (toggle))
  1151. (t
  1152. (user-error "Cannot toggle file filter in this buffer")))))
  1153. (defun magit-diff-less-context (&optional count)
  1154. "Decrease the context for diff hunks by COUNT lines."
  1155. (interactive "p")
  1156. (magit-diff-set-context `(lambda (cur) (max 0 (- (or cur 0) ,count)))))
  1157. (defun magit-diff-more-context (&optional count)
  1158. "Increase the context for diff hunks by COUNT lines."
  1159. (interactive "p")
  1160. (magit-diff-set-context `(lambda (cur) (+ (or cur 0) ,count))))
  1161. (defun magit-diff-default-context ()
  1162. "Reset context for diff hunks to the default height."
  1163. (interactive)
  1164. (magit-diff-set-context #'ignore))
  1165. (defun magit-diff-set-context (fn)
  1166. (let* ((def (--if-let (magit-get "diff.context") (string-to-number it) 3))
  1167. (val magit-buffer-diff-args)
  1168. (arg (--first (string-match "^-U\\([0-9]+\\)?$" it) val))
  1169. (num (--if-let (and arg (match-string 1 arg)) (string-to-number it) def))
  1170. (val (delete arg val))
  1171. (num (funcall fn num))
  1172. (arg (and num (not (= num def)) (format "-U%i" num)))
  1173. (val (if arg (cons arg val) val)))
  1174. (setq magit-buffer-diff-args val))
  1175. (magit-refresh))
  1176. (defun magit-diff-context-p ()
  1177. (if-let ((arg (--first (string-match "^-U\\([0-9]+\\)$" it)
  1178. magit-buffer-diff-args)))
  1179. (not (equal arg "-U0"))
  1180. t))
  1181. (defun magit-diff-ignore-any-space-p ()
  1182. (--any-p (member it magit-buffer-diff-args)
  1183. '("--ignore-cr-at-eol"
  1184. "--ignore-space-at-eol"
  1185. "--ignore-space-change" "-b"
  1186. "--ignore-all-space" "-w"
  1187. "--ignore-blank-space")))
  1188. (defun magit-diff-toggle-refine-hunk (&optional style)
  1189. "Turn diff-hunk refining on or off.
  1190. If hunk refining is currently on, then hunk refining is turned off.
  1191. If hunk refining is off, then hunk refining is turned on, in
  1192. `selected' mode (only the currently selected hunk is refined).
  1193. With a prefix argument, the \"third choice\" is used instead:
  1194. If hunk refining is currently on, then refining is kept on, but
  1195. the refining mode (`selected' or `all') is switched.
  1196. If hunk refining is off, then hunk refining is turned on, in
  1197. `all' mode (all hunks refined).
  1198. Customize variable `magit-diff-refine-hunk' to change the default mode."
  1199. (interactive "P")
  1200. (setq-local magit-diff-refine-hunk
  1201. (if style
  1202. (if (eq magit-diff-refine-hunk 'all) t 'all)
  1203. (not magit-diff-refine-hunk)))
  1204. (magit-diff-update-hunk-refinement))
  1205. ;;;; Visit Commands
  1206. ;;;;; Dwim Variants
  1207. (defun magit-diff-visit-file (file &optional other-window)
  1208. "From a diff visit the appropriate version of FILE.
  1209. Display the buffer in the selected window. With a prefix
  1210. argument OTHER-WINDOW display the buffer in another window
  1211. instead.
  1212. Visit the worktree version of the appropriate file. The location
  1213. of point inside the diff determines which file is being visited.
  1214. The visited version depends on what changes the diff is about.
  1215. 1. If the diff shows uncommitted changes (i.e. stage or unstaged
  1216. changes), then visit the file in the working tree (i.e. the
  1217. same \"real\" file that `find-file' would visit. In all other
  1218. cases visit a \"blob\" (i.e. the version of a file as stored
  1219. in some commit).
  1220. 2. If point is on a removed line, then visit the blob for the
  1221. first parent of the commit that removed that line, i.e. the
  1222. last commit where that line still exists.
  1223. 3. If point is on an added or context line, then visit the blob
  1224. that adds that line, or if the diff shows from more than a
  1225. single commit, then visit the blob from the last of these
  1226. commits.
  1227. In the file-visiting buffer also go to the line that corresponds
  1228. to the line that point is on in the diff.
  1229. Note that this command only works if point is inside a diff. In
  1230. other cases `magit-find-file' (which see) had to be used."
  1231. (interactive (list (magit-file-at-point t t) current-prefix-arg))
  1232. (magit-diff-visit-file--internal file nil
  1233. (if other-window
  1234. #'switch-to-buffer-other-window
  1235. #'pop-to-buffer-same-window)))
  1236. (defun magit-diff-visit-file-other-window (file)
  1237. "From a diff visit the appropriate version of FILE in another window.
  1238. Like `magit-diff-visit-file' but use
  1239. `switch-to-buffer-other-window'."
  1240. (interactive (list (magit-file-at-point t t)))
  1241. (magit-diff-visit-file--internal file nil #'switch-to-buffer-other-window))
  1242. (defun magit-diff-visit-file-other-frame (file)
  1243. "From a diff visit the appropriate version of FILE in another frame.
  1244. Like `magit-diff-visit-file' but use
  1245. `switch-to-buffer-other-frame'."
  1246. (interactive (list (magit-file-at-point t t)))
  1247. (magit-diff-visit-file--internal file nil #'switch-to-buffer-other-frame))
  1248. ;;;;; Worktree Variants
  1249. (defun magit-diff-visit-worktree-file (file &optional other-window)
  1250. "From a diff visit the worktree version of FILE.
  1251. Display the buffer in the selected window. With a prefix
  1252. argument OTHER-WINDOW display the buffer in another window
  1253. instead.
  1254. Visit the worktree version of the appropriate file. The location
  1255. of point inside the diff determines which file is being visited.
  1256. Unlike `magit-diff-visit-file' always visits the \"real\" file in
  1257. the working tree, i.e the \"current version\" of the file.
  1258. In the file-visiting buffer also go to the line that corresponds
  1259. to the line that point is on in the diff. Lines that were added
  1260. or removed in the working tree, the index and other commits in
  1261. between are automatically accounted for."
  1262. (interactive (list (magit-file-at-point t t) current-prefix-arg))
  1263. (magit-diff-visit-file--internal file t
  1264. (if other-window
  1265. #'switch-to-buffer-other-window
  1266. #'pop-to-buffer-same-window)))
  1267. (defun magit-diff-visit-worktree-file-other-window (file)
  1268. "From a diff visit the worktree version of FILE in another window.
  1269. Like `magit-diff-visit-worktree-file' but use
  1270. `switch-to-buffer-other-window'."
  1271. (interactive (list (magit-file-at-point t t)))
  1272. (magit-diff-visit-file--internal file t #'switch-to-buffer-other-window))
  1273. (defun magit-diff-visit-worktree-file-other-frame (file)
  1274. "From a diff visit the worktree version of FILE in another frame.
  1275. Like `magit-diff-visit-worktree-file' but use
  1276. `switch-to-buffer-other-frame'."
  1277. (interactive (list (magit-file-at-point t t)))
  1278. (magit-diff-visit-file--internal file t #'switch-to-buffer-other-frame))
  1279. ;;;;; Internal
  1280. (defun magit-diff-visit-file--internal (file force-worktree fn)
  1281. "From a diff visit the appropriate version of FILE.
  1282. If FORCE-WORKTREE is non-nil, the visit the worktree version of
  1283. the file, even if the diff is about a committed change. USE FN
  1284. to display the buffer in some window."
  1285. (if (magit-file-accessible-directory-p file)
  1286. (magit-diff-visit-directory file force-worktree)
  1287. (pcase-let ((`(,buf ,pos)
  1288. (magit-diff-visit-file--noselect file force-worktree)))
  1289. (funcall fn buf)
  1290. (magit-diff-visit-file--setup buf pos)
  1291. buf)))
  1292. (defun magit-diff-visit-directory (directory &optional other-window)
  1293. "Visit DIRECTORY in some window.
  1294. Display the buffer in the selected window unless OTHER-WINDOW is
  1295. non-nil. If DIRECTORY is the top-level directory of the current
  1296. repository, then visit the containing directory using Dired and
  1297. in the Dired buffer put point on DIRECTORY. Otherwise display
  1298. the Magit-Status buffer for DIRECTORY."
  1299. (if (equal (magit-toplevel directory)
  1300. (magit-toplevel))
  1301. (dired-jump other-window (concat directory "/."))
  1302. (let ((display-buffer-overriding-action
  1303. (if other-window
  1304. '(nil (inhibit-same-window t))
  1305. '(display-buffer-same-window))))
  1306. (magit-status-setup-buffer directory))))
  1307. (defun magit-diff-visit-file--setup (buf pos)
  1308. (if-let ((win (get-buffer-window buf 'visible)))
  1309. (with-selected-window win
  1310. (when pos
  1311. (unless (<= (point-min) pos (point-max))
  1312. (widen))
  1313. (goto-char pos))
  1314. (when (and buffer-file-name
  1315. (magit-anything-unmerged-p buffer-file-name))
  1316. (smerge-start-session))
  1317. (run-hooks 'magit-diff-visit-file-hook))
  1318. (error "File buffer is not visible")))
  1319. (defun magit-diff-visit-file--noselect (&optional file goto-worktree)
  1320. (unless file
  1321. (setq file (magit-file-at-point t t)))
  1322. (let* ((hunk (magit-diff-visit--hunk))
  1323. (goto-from (and hunk
  1324. (magit-diff-visit--goto-from-p hunk goto-worktree)))
  1325. (line (and hunk (magit-diff-hunk-line hunk goto-from)))
  1326. (col (and hunk (magit-diff-hunk-column hunk goto-from)))
  1327. (spec (magit-diff--dwim))
  1328. (rev (if goto-from
  1329. (magit-diff-visit--range-from spec)
  1330. (magit-diff-visit--range-to spec)))
  1331. (buf (if (and (not goto-worktree)
  1332. (stringp rev))
  1333. (magit-find-file-noselect rev file)
  1334. (or (get-file-buffer file)
  1335. (find-file-noselect file)))))
  1336. (if line
  1337. (with-current-buffer buf
  1338. (cond ((eq rev 'staged)
  1339. (setq line (magit-diff-visit--offset file nil line)))
  1340. ((and goto-worktree
  1341. (stringp rev))
  1342. (setq line (magit-diff-visit--offset file rev line))))
  1343. (list buf (save-restriction
  1344. (widen)
  1345. (goto-char (point-min))
  1346. (forward-line (1- line))
  1347. (move-to-column col)
  1348. (point))))
  1349. (list buf nil))))
  1350. (defun magit-diff-visit--hunk ()
  1351. (when-let ((scope (magit-diff-scope)))
  1352. (let ((section (magit-current-section)))
  1353. (cl-case scope
  1354. ((file files)
  1355. (setq section (car (oref section children))))
  1356. (list
  1357. (setq section (car (oref section children)))
  1358. (when section
  1359. (setq section (car (oref section children))))))
  1360. (and
  1361. ;; Unmerged files appear in the list of staged changes
  1362. ;; but unlike in the list of unstaged changes no diffs
  1363. ;; are shown here. In that case `section' is nil.
  1364. section
  1365. ;; Currently the `hunk' type is also abused for file
  1366. ;; mode changes, which we are not interested in here.
  1367. ;; Such sections have no value.
  1368. (oref section value)
  1369. section))))
  1370. (defun magit-diff-visit--goto-from-p (section in-worktree)
  1371. (and magit-diff-visit-previous-blob
  1372. (not in-worktree)
  1373. (not (oref section combined))
  1374. (not (< (point) (oref section content)))
  1375. (= (char-after (line-beginning-position)) ?-)))
  1376. (defun magit-diff-hunk-line (section goto-from)
  1377. (save-excursion
  1378. (goto-char (line-beginning-position))
  1379. (with-slots (content combined from-ranges from-range to-range) section
  1380. (when (< (point) content)
  1381. (goto-char content)
  1382. (re-search-forward "^[-+]"))
  1383. (+ (car (if goto-from from-range to-range))
  1384. (let ((prefix (if combined (length from-ranges) 1))
  1385. (target (point))
  1386. (offset 0))
  1387. (goto-char content)
  1388. (while (< (point) target)
  1389. (unless (string-match-p
  1390. (if goto-from "\\+" "-")
  1391. (buffer-substring (point) (+ (point) prefix)))
  1392. (cl-incf offset))
  1393. (forward-line))
  1394. offset)))))
  1395. (defun magit-diff-hunk-column (section goto-from)
  1396. (if (or (< (point)
  1397. (oref section content))
  1398. (and (not goto-from)
  1399. (= (char-after (line-beginning-position)) ?-)))
  1400. 0
  1401. (max 0 (- (+ (current-column) 2)
  1402. (length (oref section value))))))
  1403. (defun magit-diff-visit--range-from (spec)
  1404. (cond ((consp spec)
  1405. (concat (cdr spec) "^"))
  1406. ((stringp spec)
  1407. (car (magit-split-range spec)))
  1408. (t
  1409. spec)))
  1410. (defun magit-diff-visit--range-to (spec)
  1411. (if (symbolp spec)
  1412. spec
  1413. (let ((rev (if (consp spec)
  1414. (cdr spec)
  1415. (cdr (magit-split-range spec)))))
  1416. (if (and magit-diff-visit-avoid-head-blob
  1417. (magit-rev-head-p spec))
  1418. 'unstaged
  1419. rev))))
  1420. (defun magit-diff-visit--offset (file rev line)
  1421. (let ((offset 0))
  1422. (with-temp-buffer
  1423. (save-excursion
  1424. (magit-with-toplevel
  1425. (magit-git-insert "diff" rev "--" file)))
  1426. (catch 'found
  1427. (while (re-search-forward
  1428. "^@@ -\\([0-9]+\\),\\([0-9]+\\) \\+\\([0-9]+\\),\\([0-9]+\\) @@.*\n"
  1429. nil t)
  1430. (let ((from-beg (string-to-number (match-string 1)))
  1431. (from-len (string-to-number (match-string 2)))
  1432. ( to-len (string-to-number (match-string 4))))
  1433. (if (<= from-beg line)
  1434. (if (< (+ from-beg from-len) line)
  1435. (cl-incf offset (- to-len from-len))
  1436. (let ((rest (- line from-beg)))
  1437. (while (> rest 0)
  1438. (pcase (char-after)
  1439. (?\s (cl-decf rest))
  1440. (?- (cl-decf offset) (cl-decf rest))
  1441. (?+ (cl-incf offset)))
  1442. (forward-line))))
  1443. (throw 'found nil))))))
  1444. (+ line offset)))
  1445. ;;;; Scroll Commands
  1446. (defun magit-diff-show-or-scroll-up ()
  1447. "Update the commit or diff buffer for the thing at point.
  1448. Either show the commit or stash at point in the appropriate
  1449. buffer, or if that buffer is already being displayed in the
  1450. current frame and contains information about that commit or
  1451. stash, then instead scroll the buffer up. If there is no
  1452. commit or stash at point, then prompt for a commit."
  1453. (interactive)
  1454. (magit-diff-show-or-scroll 'scroll-up))
  1455. (defun magit-diff-show-or-scroll-down ()
  1456. "Update the commit or diff buffer for the thing at point.
  1457. Either show the commit or stash at point in the appropriate
  1458. buffer, or if that buffer is already being displayed in the
  1459. current frame and contains information about that commit or
  1460. stash, then instead scroll the buffer down. If there is no
  1461. commit or stash at point, then prompt for a commit."
  1462. (interactive)
  1463. (magit-diff-show-or-scroll 'scroll-down))
  1464. (defun magit-diff-show-or-scroll (fn)
  1465. (let (rev cmd buf win)
  1466. (cond
  1467. (magit-blame-mode
  1468. (setq rev (oref (magit-current-blame-chunk) orig-rev))
  1469. (setq cmd 'magit-show-commit)
  1470. (setq buf (magit-get-mode-buffer 'magit-revision-mode)))
  1471. ((derived-mode-p 'git-rebase-mode)
  1472. (with-slots (action-type target)
  1473. (git-rebase-current-line)
  1474. (if (not (eq action-type 'commit))
  1475. (user-error "No commit on this line")
  1476. (setq rev target)
  1477. (setq cmd 'magit-show-commit)
  1478. (setq buf (magit-get-mode-buffer 'magit-revision-mode)))))
  1479. (t
  1480. (magit-section-case
  1481. (branch
  1482. (setq rev (magit-ref-maybe-qualify (oref it value)))
  1483. (setq cmd 'magit-show-commit)
  1484. (setq buf (magit-get-mode-buffer 'magit-revision-mode)))
  1485. (commit
  1486. (setq rev (oref it value))
  1487. (setq cmd 'magit-show-commit)
  1488. (setq buf (magit-get-mode-buffer 'magit-revision-mode)))
  1489. (stash
  1490. (setq rev (oref it value))
  1491. (setq cmd 'magit-stash-show)
  1492. (setq buf (magit-get-mode-buffer 'magit-stash-mode))))))
  1493. (if rev
  1494. (if (and buf
  1495. (setq win (get-buffer-window buf))
  1496. (with-current-buffer buf
  1497. (and (equal rev magit-buffer-revision)
  1498. (equal (magit-rev-parse rev)
  1499. magit-buffer-revision-hash))))
  1500. (with-selected-window win
  1501. (condition-case nil
  1502. (funcall fn)
  1503. (error
  1504. (goto-char (pcase fn
  1505. (`scroll-up (point-min))
  1506. (`scroll-down (point-max)))))))
  1507. (let ((magit-display-buffer-noselect t))
  1508. (if (eq cmd 'magit-show-commit)
  1509. (apply #'magit-show-commit rev (magit-show-commit--arguments))
  1510. (funcall cmd rev))))
  1511. (call-interactively #'magit-show-commit))))
  1512. ;;; Diff Mode
  1513. (defvar magit-diff-mode-map
  1514. (let ((map (make-sparse-keymap)))
  1515. (set-keymap-parent map magit-mode-map)
  1516. (define-key map (kbd "C-c C-d") 'magit-diff-while-committing)
  1517. (define-key map (kbd "C-c C-b") 'magit-go-backward)
  1518. (define-key map (kbd "C-c C-f") 'magit-go-forward)
  1519. (define-key map (kbd "SPC") 'scroll-up)
  1520. (define-key map (kbd "DEL") 'scroll-down)
  1521. (define-key map (kbd "j") 'magit-jump-to-diffstat-or-diff)
  1522. (define-key map [remap write-file] 'magit-patch-save)
  1523. map)
  1524. "Keymap for `magit-diff-mode'.")
  1525. (define-derived-mode magit-diff-mode magit-mode "Magit Diff"
  1526. "Mode for looking at a Git diff.
  1527. This mode is documented in info node `(magit)Diff Buffer'.
  1528. \\<magit-mode-map>\
  1529. Type \\[magit-refresh] to refresh the current buffer.
  1530. Type \\[magit-section-toggle] to expand or hide the section at point.
  1531. Type \\[magit-visit-thing] to visit the hunk or file at point.
  1532. Staging and applying changes is documented in info node
  1533. `(magit)Staging and Unstaging' and info node `(magit)Applying'.
  1534. \\<magit-hunk-section-map>Type \
  1535. \\[magit-apply] to apply the change at point, \
  1536. \\[magit-stage] to stage,
  1537. \\[magit-unstage] to unstage, \
  1538. \\[magit-discard] to discard, or \
  1539. \\[magit-reverse] to reverse it.
  1540. \\{magit-diff-mode-map}"
  1541. :group 'magit-diff
  1542. (hack-dir-local-variables-non-file-buffer)
  1543. (setq imenu-prev-index-position-function
  1544. 'magit-imenu--diff-prev-index-position-function)
  1545. (setq imenu-extract-index-name-function
  1546. 'magit-imenu--diff-extract-index-name-function))
  1547. (put 'magit-diff-mode 'magit-diff-default-arguments
  1548. '("--stat" "--no-ext-diff"))
  1549. (defun magit-diff-setup-buffer (range typearg args files &optional locked)
  1550. (require 'magit)
  1551. (magit-setup-buffer #'magit-diff-mode locked
  1552. (magit-buffer-range range)
  1553. (magit-buffer-typearg typearg)
  1554. (magit-buffer-diff-args args)
  1555. (magit-buffer-diff-files files)))
  1556. (defun magit-diff-refresh-buffer ()
  1557. "Refresh the current `magit-diff-mode' buffer."
  1558. (magit-set-header-line-format
  1559. (if (equal magit-buffer-typearg "--no-index")
  1560. (apply #'format "Differences between %s and %s" magit-buffer-diff-files)
  1561. (concat (if magit-buffer-range
  1562. (if (string-match-p "\\(\\.\\.\\|\\^-\\)"
  1563. magit-buffer-range)
  1564. (format "Changes in %s" magit-buffer-range)
  1565. (format "Changes from %s to working tree" magit-buffer-range))
  1566. (if (equal magit-buffer-typearg "--cached")
  1567. "Staged changes"
  1568. "Unstaged changes"))
  1569. (pcase (length magit-buffer-diff-files)
  1570. (0)
  1571. (1 (concat " in file " (car magit-buffer-diff-files)))
  1572. (_ (concat " in files "
  1573. (mapconcat #'identity magit-buffer-diff-files ", ")))))))
  1574. (setq magit-buffer-range-hashed
  1575. (and magit-buffer-range (magit-hash-range magit-buffer-range)))
  1576. (magit-insert-section (diffbuf)
  1577. (magit-run-section-hook 'magit-diff-sections-hook)))
  1578. (cl-defmethod magit-buffer-value (&context (major-mode magit-diff-mode))
  1579. (nconc (cond (magit-buffer-range
  1580. (delq nil (list magit-buffer-range magit-buffer-typearg)))
  1581. ((equal magit-buffer-typearg "--cached")
  1582. (list 'staged))
  1583. (t
  1584. (list 'unstaged magit-buffer-typearg)))
  1585. (and magit-buffer-diff-files (cons "--" magit-buffer-diff-files))))
  1586. (defvar magit-file-section-map
  1587. (let ((map (make-sparse-keymap)))
  1588. (unless (featurep 'jkl)
  1589. (define-key map (kbd "C-j") 'magit-diff-visit-file-worktree))
  1590. (define-key map [C-return] 'magit-diff-visit-file-worktree)
  1591. (define-key map [remap magit-visit-thing] 'magit-diff-visit-file)
  1592. (define-key map [remap magit-delete-thing] 'magit-discard)
  1593. (define-key map [remap magit-revert-no-commit] 'magit-reverse)
  1594. (define-key map "a" 'magit-apply)
  1595. (define-key map "C" 'magit-commit-add-log)
  1596. (define-key map "s" 'magit-stage)
  1597. (define-key map "u" 'magit-unstage)
  1598. (define-key map "&" 'magit-do-async-shell-command)
  1599. (define-key map "\C-c\C-t" 'magit-diff-trace-definition)
  1600. (define-key map "\C-c\C-e" 'magit-diff-edit-hunk-commit)
  1601. map)
  1602. "Keymap for `file' sections.")
  1603. (defvar magit-hunk-section-map
  1604. (let ((map (make-sparse-keymap)))
  1605. (unless (featurep 'jkl)
  1606. (define-key map (kbd "C-j") 'magit-diff-visit-file-worktree))
  1607. (define-key map [C-return] 'magit-diff-visit-file-worktree)
  1608. (define-key map [remap magit-visit-thing] 'magit-diff-visit-file)
  1609. (define-key map [remap magit-delete-thing] 'magit-discard)
  1610. (define-key map [remap magit-revert-no-commit] 'magit-reverse)
  1611. (define-key map "a" 'magit-apply)
  1612. (define-key map "C" 'magit-commit-add-log)
  1613. (define-key map "s" 'magit-stage)
  1614. (define-key map "u" 'magit-unstage)
  1615. (define-key map "&" 'magit-do-async-shell-command)
  1616. (define-key map "\C-c\C-t" 'magit-diff-trace-definition)
  1617. (define-key map "\C-c\C-e" 'magit-diff-edit-hunk-commit)
  1618. map)
  1619. "Keymap for `hunk' sections.")
  1620. (defconst magit-diff-headline-re
  1621. (concat "^\\(@@@?\\|diff\\|Submodule\\|"
  1622. "\\* Unmerged path\\|merged\\|changed in both\\|"
  1623. "added in remote\\|removed in remote\\)"))
  1624. (defconst magit-diff-statline-re
  1625. (concat "^ ?"
  1626. "\\(.*\\)" ; file
  1627. "\\( +| +\\)" ; separator
  1628. "\\([0-9]+\\|Bin\\(?: +[0-9]+ -> [0-9]+ bytes\\)?$\\) ?"
  1629. "\\(\\+*\\)" ; add
  1630. "\\(-*\\)$")) ; del
  1631. (defvar magit-diff--reset-non-color-moved
  1632. (list
  1633. "-c" "color.diff.context=normal"
  1634. "-c" "color.diff.plain=normal" ; historical synonym for context
  1635. "-c" "color.diff.meta=normal"
  1636. "-c" "color.diff.frag=normal"
  1637. "-c" "color.diff.func=normal"
  1638. "-c" "color.diff.old=normal"
  1639. "-c" "color.diff.new=normal"
  1640. "-c" "color.diff.commit=normal"
  1641. "-c" "color.diff.whitespace=normal"
  1642. ;; "git-range-diff" does not support "--color-moved", so we don't
  1643. ;; need to reset contextDimmed, oldDimmed, newDimmed, contextBold,
  1644. ;; oldBold, and newBold.
  1645. ))
  1646. (defun magit-insert-diff ()
  1647. "Insert the diff into this `magit-diff-mode' buffer."
  1648. (magit--insert-diff
  1649. "diff" magit-buffer-range "-p" "--no-prefix"
  1650. (and (member "--stat" magit-buffer-diff-args) "--numstat")
  1651. magit-buffer-typearg
  1652. magit-buffer-diff-args "--"
  1653. magit-buffer-diff-files))
  1654. (defun magit--insert-diff (&rest args)
  1655. (declare (indent 0))
  1656. (let ((magit-git-global-arguments
  1657. (remove "--literal-pathspecs" magit-git-global-arguments)))
  1658. (setq args (-flatten args))
  1659. (when (cl-member-if (lambda (arg) (string-prefix-p "--color-moved" arg)) args)
  1660. (push "--color=always" (cdr args))
  1661. (setq magit-git-global-arguments
  1662. (append magit-diff--reset-non-color-moved
  1663. magit-git-global-arguments)))
  1664. (magit-git-wash #'magit-diff-wash-diffs args)))
  1665. (defun magit-diff-wash-diffs (args &optional limit)
  1666. (when (member "--stat" args)
  1667. (magit-diff-wash-diffstat))
  1668. (when (re-search-forward magit-diff-headline-re limit t)
  1669. (goto-char (line-beginning-position))
  1670. (magit-wash-sequence (apply-partially 'magit-diff-wash-diff args))
  1671. (insert ?\n)))
  1672. (defun magit-jump-to-diffstat-or-diff ()
  1673. "Jump to the diffstat or diff.
  1674. When point is on a file inside the diffstat section, then jump
  1675. to the respective diff section, otherwise jump to the diffstat
  1676. section or a child thereof."
  1677. (interactive)
  1678. (--if-let (magit-get-section
  1679. (append (magit-section-case
  1680. ([file diffstat] `((file . ,(oref it value))))
  1681. (file `((file . ,(oref it value)) (diffstat)))
  1682. (t '((diffstat))))
  1683. (magit-section-ident magit-root-section)))
  1684. (magit-section-goto it)
  1685. (user-error "No diffstat in this buffer")))
  1686. (defun magit-diff-wash-diffstat ()
  1687. (let (heading (beg (point)))
  1688. (when (re-search-forward "^ ?\\([0-9]+ +files? change[^\n]*\n\\)" nil t)
  1689. (setq heading (match-string 1))
  1690. (magit-delete-match)
  1691. (goto-char beg)
  1692. (magit-insert-section (diffstat)
  1693. (insert (propertize heading 'font-lock-face 'magit-diff-file-heading))
  1694. (magit-insert-heading)
  1695. (let (files)
  1696. (while (looking-at "^[-0-9]+\t[-0-9]+\t\\(.+\\)$")
  1697. (push (magit-decode-git-path
  1698. (let ((f (match-string 1)))
  1699. (cond
  1700. ((string-match "\\`\\([^{]+\\){\\(.+\\) => \\(.+\\)}\\'" f)
  1701. (concat (match-string 1 f)
  1702. (match-string 3 f)))
  1703. ((string-match " => " f)
  1704. (substring f (match-end 0)))
  1705. (t f))))
  1706. files)
  1707. (magit-delete-line))
  1708. (setq files (nreverse files))
  1709. (while (looking-at magit-diff-statline-re)
  1710. (magit-bind-match-strings (file sep cnt add del) nil
  1711. (magit-delete-line)
  1712. (when (string-match " +$" file)
  1713. (setq sep (concat (match-string 0 file) sep))
  1714. (setq file (substring file 0 (match-beginning 0))))
  1715. (let ((le (length file)) ld)
  1716. (setq file (magit-decode-git-path file))
  1717. (setq ld (length file))
  1718. (when (> le ld)
  1719. (setq sep (concat (make-string (- le ld) ?\s) sep))))
  1720. (magit-insert-section (file (pop files))
  1721. (insert (propertize file 'font-lock-face 'magit-filename)
  1722. sep cnt " ")
  1723. (when add
  1724. (insert (propertize add 'font-lock-face
  1725. 'magit-diffstat-added)))
  1726. (when del
  1727. (insert (propertize del 'font-lock-face
  1728. 'magit-diffstat-removed)))
  1729. (insert "\n")))))
  1730. (if (looking-at "^$") (forward-line) (insert "\n"))))))
  1731. (defun magit-diff-wash-diff (args)
  1732. (when (cl-member-if (lambda (arg) (string-prefix-p "--color-moved" arg)) args)
  1733. (require 'ansi-color)
  1734. (ansi-color-apply-on-region (point-min) (point-max)))
  1735. (cond
  1736. ((looking-at "^Submodule")
  1737. (magit-diff-wash-submodule))
  1738. ((looking-at "^\\* Unmerged path \\(.*\\)")
  1739. (let ((file (magit-decode-git-path (match-string 1))))
  1740. (magit-delete-line)
  1741. (unless (and (derived-mode-p 'magit-status-mode)
  1742. (not (member "--cached" args)))
  1743. (magit-insert-section (file file)
  1744. (insert (propertize
  1745. (format "unmerged %s%s" file
  1746. (pcase (cddr (car (magit-file-status file)))
  1747. (`(?D ?D) " (both deleted)")
  1748. (`(?D ?U) " (deleted by us)")
  1749. (`(?U ?D) " (deleted by them)")
  1750. (`(?A ?A) " (both added)")
  1751. (`(?A ?U) " (added by us)")
  1752. (`(?U ?A) " (added by them)")
  1753. (`(?U ?U) "")))
  1754. 'font-lock-face 'magit-diff-file-heading))
  1755. (insert ?\n))))
  1756. t)
  1757. ((looking-at (concat "^\\(merged\\|changed in both\\|"
  1758. "added in remote\\|removed in remote\\)"))
  1759. (let ((status (pcase (match-string 1)
  1760. ("merged" "merged")
  1761. ("changed in both" "conflict")
  1762. ("added in remote" "new file")
  1763. ("removed in remote" "deleted")))
  1764. file orig base modes)
  1765. (magit-delete-line)
  1766. (while (looking-at
  1767. "^ \\([^ ]+\\) +[0-9]\\{6\\} \\([a-z0-9]\\{40\\}\\) \\(.+\\)$")
  1768. (magit-bind-match-strings (side _blob name) nil
  1769. (pcase side
  1770. ("result" (setq file name))
  1771. ("our" (setq orig name))
  1772. ("their" (setq file name))
  1773. ("base" (setq base name))))
  1774. (magit-delete-line))
  1775. (when orig (setq orig (magit-decode-git-path orig)))
  1776. (when file (setq file (magit-decode-git-path file)))
  1777. (magit-diff-insert-file-section (or file base) orig status modes nil)))
  1778. ((looking-at
  1779. "^diff --\\(?:\\(git\\) \\(?:\\(.+?\\) \\2\\)?\\|\\(cc\\|combined\\) \\(.+\\)\\)")
  1780. (let ((status (cond ((equal (match-string 1) "git") "modified")
  1781. ((derived-mode-p 'magit-revision-mode) "resolved")
  1782. (t "unmerged")))
  1783. (file (or (match-string 2) (match-string 4)))
  1784. (beg (point))
  1785. orig header modes)
  1786. (save-excursion
  1787. (forward-line 1)
  1788. (setq header (buffer-substring
  1789. beg (if (re-search-forward magit-diff-headline-re nil t)
  1790. (match-beginning 0)
  1791. (point-max)))))
  1792. (magit-delete-line)
  1793. (while (not (or (eobp) (looking-at magit-diff-headline-re)))
  1794. (if (looking-at "^old mode \\([^\n]+\\)\nnew mode \\([^\n]+\\)\n")
  1795. (progn (setq modes (match-string 0))
  1796. (magit-delete-match))
  1797. (cond
  1798. ((looking-at "^--- \\([^/].*?\\)\t?$") ; i.e. not /dev/null
  1799. (setq orig (match-string 1)))
  1800. ((looking-at "^\\+\\+\\+ \\([^/].*?\\)\t?$")
  1801. (setq file (match-string 1)))
  1802. ((looking-at "^\\(copy\\|rename\\) from \\(.+\\)$")
  1803. (setq orig (match-string 2)))
  1804. ((looking-at "^\\(copy\\|rename\\) to \\(.+\\)$")
  1805. (setq file (match-string 2))
  1806. (setq status (if (equal (match-string 1) "copy") "new file" "renamed")))
  1807. ((looking-at "^\\(new file\\|deleted\\)")
  1808. (setq status (match-string 1))))
  1809. (magit-delete-line)))
  1810. (when orig
  1811. (setq orig (magit-decode-git-path orig)))
  1812. (setq file (magit-decode-git-path file))
  1813. ;; KLUDGE `git-log' ignores `--no-prefix' when `-L' is used.
  1814. (when (and (derived-mode-p 'magit-log-mode)
  1815. (--first (string-match-p "\\`-L" it) magit-buffer-log-args))
  1816. (setq file (substring file 2))
  1817. (when orig
  1818. (setq orig (substring orig 2))))
  1819. (magit-diff-insert-file-section file orig status modes header)))))
  1820. (defun magit-diff-insert-file-section (file orig status modes header)
  1821. (magit-insert-section section
  1822. (file file (or (equal status "deleted")
  1823. (derived-mode-p 'magit-status-mode)))
  1824. (insert (propertize (format "%-10s %s\n" status
  1825. (if (or (not orig) (equal orig file))
  1826. file
  1827. (format "%s -> %s" orig file)))
  1828. 'font-lock-face 'magit-diff-file-heading))
  1829. (magit-insert-heading)
  1830. (unless (equal orig file)
  1831. (oset section source orig))
  1832. (oset section header header)
  1833. (when modes
  1834. (magit-insert-section (hunk)
  1835. (insert modes)
  1836. (magit-insert-heading)))
  1837. (magit-wash-sequence #'magit-diff-wash-hunk)))
  1838. (defun magit-diff-wash-submodule ()
  1839. ;; See `show_submodule_summary' in submodule.c and "this" commit.
  1840. (when (looking-at "^Submodule \\([^ ]+\\)")
  1841. (let ((module (match-string 1))
  1842. untracked modified)
  1843. (when (looking-at "^Submodule [^ ]+ contains untracked content$")
  1844. (magit-delete-line)
  1845. (setq untracked t))
  1846. (when (looking-at "^Submodule [^ ]+ contains modified content$")
  1847. (magit-delete-line)
  1848. (setq modified t))
  1849. (cond
  1850. ((and (looking-at "^Submodule \\([^ ]+\\) \\([^ :]+\\)\\( (rewind)\\)?:$")
  1851. (equal (match-string 1) module))
  1852. (magit-bind-match-strings (_module range rewind) nil
  1853. (magit-delete-line)
  1854. (while (looking-at "^ \\([<>]\\) \\(.+\\)$")
  1855. (magit-delete-line))
  1856. (when rewind
  1857. (setq range (replace-regexp-in-string "[^.]\\(\\.\\.\\)[^.]"
  1858. "..." range t t 1)))
  1859. (magit-insert-section (magit-module-section module t)
  1860. (magit-insert-heading
  1861. (propertize (concat "modified " module)
  1862. 'font-lock-face 'magit-diff-file-heading)
  1863. " ("
  1864. (cond (rewind "rewind")
  1865. ((string-match-p "\\.\\.\\." range) "non-ff")
  1866. (t "new commits"))
  1867. (and (or modified untracked)
  1868. (concat ", "
  1869. (and modified "modified")
  1870. (and modified untracked " and ")
  1871. (and untracked "untracked")
  1872. " content"))
  1873. ")")
  1874. (let ((default-directory
  1875. (file-name-as-directory
  1876. (expand-file-name module (magit-toplevel)))))
  1877. (magit-git-wash (apply-partially 'magit-log-wash-log 'module)
  1878. "log" "--oneline" "--left-right" range)
  1879. (delete-char -1)))))
  1880. ((and (looking-at "^Submodule \\([^ ]+\\) \\([^ ]+\\) (\\([^)]+\\))$")
  1881. (equal (match-string 1) module))
  1882. (magit-bind-match-strings (_module _range msg) nil
  1883. (magit-delete-line)
  1884. (magit-insert-section (magit-module-section module)
  1885. (magit-insert-heading
  1886. (propertize (concat "submodule " module)
  1887. 'font-lock-face 'magit-diff-file-heading)
  1888. " (" msg ")"))))
  1889. (t
  1890. (magit-insert-section (magit-module-section module)
  1891. (magit-insert-heading
  1892. (propertize (concat "modified " module)
  1893. 'font-lock-face 'magit-diff-file-heading)
  1894. " ("
  1895. (and modified "modified")
  1896. (and modified untracked " and ")
  1897. (and untracked "untracked")
  1898. " content)")))))))
  1899. (defun magit-diff-wash-hunk ()
  1900. (when (looking-at "^@\\{2,\\} \\(.+?\\) @\\{2,\\}\\(?: \\(.*\\)\\)?")
  1901. (let* ((heading (match-string 0))
  1902. (ranges (mapcar (lambda (str)
  1903. (mapcar (lambda (n) (string-to-number n))
  1904. (split-string (substring str 1) ",")))
  1905. (split-string (match-string 1))))
  1906. (about (match-string 2))
  1907. (combined (= (length ranges) 3))
  1908. (value (cons about ranges)))
  1909. (magit-delete-line)
  1910. (magit-insert-section section (hunk value)
  1911. (insert (propertize (concat heading "\n")
  1912. 'font-lock-face 'magit-diff-hunk-heading))
  1913. (magit-insert-heading)
  1914. (while (not (or (eobp) (looking-at "^[^-+\s\\]")))
  1915. (forward-line))
  1916. (oset section end (point))
  1917. (oset section washer 'magit-diff-paint-hunk)
  1918. (oset section combined combined)
  1919. (if combined
  1920. (oset section from-ranges (butlast ranges))
  1921. (oset section from-range (car ranges)))
  1922. (oset section to-range (car (last ranges)))
  1923. (oset section about about)))
  1924. t))
  1925. (defun magit-diff-expansion-threshold (section)
  1926. "Keep new diff sections collapsed if washing takes too long."
  1927. (and (magit-file-section-p section)
  1928. (> (float-time (time-subtract (current-time) magit-refresh-start-time))
  1929. magit-diff-expansion-threshold)
  1930. 'hide))
  1931. ;;; Revision Mode
  1932. (define-derived-mode magit-revision-mode magit-diff-mode "Magit Rev"
  1933. "Mode for looking at a Git commit.
  1934. This mode is documented in info node `(magit)Revision Buffer'.
  1935. \\<magit-mode-map>\
  1936. Type \\[magit-refresh] to refresh the current buffer.
  1937. Type \\[magit-section-toggle] to expand or hide the section at point.
  1938. Type \\[magit-visit-thing] to visit the hunk or file at point.
  1939. Staging and applying changes is documented in info node
  1940. `(magit)Staging and Unstaging' and info node `(magit)Applying'.
  1941. \\<magit-hunk-section-map>Type \
  1942. \\[magit-apply] to apply the change at point, \
  1943. \\[magit-stage] to stage,
  1944. \\[magit-unstage] to unstage, \
  1945. \\[magit-discard] to discard, or \
  1946. \\[magit-reverse] to reverse it.
  1947. \\{magit-revision-mode-map}"
  1948. :group 'magit-revision
  1949. (hack-dir-local-variables-non-file-buffer))
  1950. (put 'magit-revision-mode 'magit-diff-default-arguments
  1951. '("--stat" "--no-ext-diff"))
  1952. (defun magit-revision-setup-buffer (rev args files)
  1953. (magit-setup-buffer #'magit-revision-mode nil
  1954. (magit-buffer-revision rev)
  1955. (magit-buffer-range (format "%s^..%s" rev rev))
  1956. (magit-buffer-diff-args args)
  1957. (magit-buffer-diff-files files)))
  1958. (defun magit-revision-refresh-buffer ()
  1959. (magit-set-header-line-format
  1960. (concat (capitalize (magit-object-type magit-buffer-revision))
  1961. " " magit-buffer-revision
  1962. (pcase (length magit-buffer-diff-files)
  1963. (0)
  1964. (1 (concat " limited to file " (car magit-buffer-diff-files)))
  1965. (_ (concat " limited to files "
  1966. (mapconcat #'identity magit-buffer-diff-files ", "))))))
  1967. (setq magit-buffer-revision-hash (magit-rev-parse magit-buffer-revision))
  1968. (magit-insert-section (commitbuf)
  1969. (magit-run-section-hook 'magit-revision-sections-hook)))
  1970. (cl-defmethod magit-buffer-value (&context (major-mode magit-revision-mode))
  1971. (cons magit-buffer-revision magit-buffer-diff-files))
  1972. (defun magit-insert-revision-diff ()
  1973. "Insert the diff into this `magit-revision-mode' buffer."
  1974. (magit--insert-diff
  1975. "show" "-p" "--cc" "--format=" "--no-prefix"
  1976. (and (member "--stat" magit-buffer-diff-args) "--numstat")
  1977. magit-buffer-diff-args
  1978. (concat magit-buffer-revision "^{commit}")
  1979. "--" magit-buffer-diff-files))
  1980. (defun magit-insert-revision-tag ()
  1981. "Insert tag message and headers into a revision buffer.
  1982. This function only inserts anything when `magit-show-commit' is
  1983. called with a tag as argument, when that is called with a commit
  1984. or a ref which is not a branch, then it inserts nothing."
  1985. (when (equal (magit-object-type magit-buffer-revision) "tag")
  1986. (magit-insert-section (taginfo)
  1987. (let ((beg (point)))
  1988. ;; "git verify-tag -v" would output what we need, but the gpg
  1989. ;; output is send to stderr and we have no control over the
  1990. ;; order in which stdout and stderr are inserted, which would
  1991. ;; make parsing hard. We are forced to use "git cat-file tag"
  1992. ;; instead, which inserts the signature instead of verifying
  1993. ;; it. We remove that later and then insert the verification
  1994. ;; output using "git verify-tag" (without the "-v").
  1995. (magit-git-insert "cat-file" "tag" magit-buffer-revision)
  1996. (goto-char beg)
  1997. (forward-line 3)
  1998. (delete-region beg (point)))
  1999. (looking-at "^tagger \\([^<]+\\) <\\([^>]+\\)")
  2000. (let ((heading (format "Tagger: %s <%s>"
  2001. (match-string 1)
  2002. (match-string 2))))
  2003. (magit-delete-line)
  2004. (insert (propertize heading 'font-lock-face
  2005. 'magit-section-secondary-heading)))
  2006. (magit-insert-heading)
  2007. (if (re-search-forward "-----BEGIN PGP SIGNATURE-----" nil t)
  2008. (progn
  2009. (let ((beg (match-beginning 0)))
  2010. (re-search-forward "-----END PGP SIGNATURE-----")
  2011. (delete-region beg (point)))
  2012. (insert ?\n)
  2013. (process-file magit-git-executable nil t nil
  2014. "verify-tag" magit-buffer-revision))
  2015. (goto-char (point-max)))
  2016. (insert ?\n))))
  2017. (defvar magit-commit-message-section-map
  2018. (let ((map (make-sparse-keymap)))
  2019. (define-key map [remap magit-visit-thing] 'magit-show-commit)
  2020. map)
  2021. "Keymap for `commit-message' sections.")
  2022. (defun magit-insert-revision-message ()
  2023. "Insert the commit message into a revision buffer."
  2024. (magit-insert-section section (commit-message)
  2025. (oset section heading-highlight-face 'magit-diff-hunk-heading-highlight)
  2026. (let ((beg (point))
  2027. (rev magit-buffer-revision))
  2028. (insert (with-temp-buffer
  2029. (magit-rev-insert-format "%B" rev)
  2030. (magit-revision--wash-message)))
  2031. (if (= (point) (+ beg 2))
  2032. (progn (backward-delete-char 2)
  2033. (insert "(no message)\n"))
  2034. (goto-char beg)
  2035. (save-excursion
  2036. (while (search-forward "\r\n" nil t) ; Remove trailing CRs.
  2037. (delete-region (match-beginning 0) (1+ (match-beginning 0)))))
  2038. (when magit-revision-fill-summary-line
  2039. (let ((fill-column (min magit-revision-fill-summary-line
  2040. (window-width))))
  2041. (fill-region (point) (line-end-position))))
  2042. (when magit-revision-use-hash-sections
  2043. (save-excursion
  2044. (while (not (eobp))
  2045. (re-search-forward "\\_<" nil 'move)
  2046. (let ((beg (point)))
  2047. (re-search-forward "\\_>" nil t)
  2048. (when (> (point) beg)
  2049. (let ((text (buffer-substring-no-properties beg (point))))
  2050. (when (pcase magit-revision-use-hash-sections
  2051. (`quickest ; false negatives and positives
  2052. (and (>= (length text) 7)
  2053. (string-match-p "[0-9]" text)
  2054. (string-match-p "[a-z]" text)))
  2055. (`quicker ; false negatives (number-less hashes)
  2056. (and (>= (length text) 7)
  2057. (string-match-p "[0-9]" text)
  2058. (magit-commit-p text)))
  2059. (`quick ; false negatives (short hashes)
  2060. (and (>= (length text) 7)
  2061. (magit-commit-p text)))
  2062. (`slow
  2063. (magit-commit-p text)))
  2064. (put-text-property beg (point)
  2065. 'font-lock-face 'magit-hash)
  2066. (let ((end (point)))
  2067. (goto-char beg)
  2068. (magit-insert-section (commit text)
  2069. (goto-char end))))))))))
  2070. (save-excursion
  2071. (forward-line)
  2072. (add-face-text-property beg (point) 'magit-diff-hunk-heading)
  2073. (magit-insert-heading))
  2074. (when magit-diff-highlight-keywords
  2075. (save-excursion
  2076. (while (re-search-forward "\\[[^[]*\\]" nil t)
  2077. (let ((beg (match-beginning 0))
  2078. (end (match-end 0)))
  2079. (put-text-property
  2080. beg end 'font-lock-face
  2081. (if-let ((face (get-text-property beg 'font-lock-face)))
  2082. (list face 'magit-keyword)
  2083. 'magit-keyword))))))
  2084. (goto-char (point-max))))))
  2085. (defun magit-insert-revision-notes ()
  2086. "Insert commit notes into a revision buffer."
  2087. (let* ((var "core.notesRef")
  2088. (def (or (magit-get var) "refs/notes/commits")))
  2089. (dolist (ref (or (magit-list-active-notes-refs)))
  2090. (magit-insert-section section (notes ref (not (equal ref def)))
  2091. (oset section heading-highlight-face 'magit-diff-hunk-heading-highlight)
  2092. (let ((beg (point))
  2093. (rev magit-buffer-revision))
  2094. (insert (with-temp-buffer
  2095. (magit-git-insert "-c" (concat "core.notesRef=" ref)
  2096. "notes" "show" rev)
  2097. (magit-revision--wash-message)))
  2098. (if (= (point) beg)
  2099. (magit-cancel-section)
  2100. (goto-char beg)
  2101. (end-of-line)
  2102. (insert (format " (%s)"
  2103. (propertize (if (string-prefix-p "refs/notes/" ref)
  2104. (substring ref 11)
  2105. ref)
  2106. 'font-lock-face 'magit-refname)))
  2107. (forward-char)
  2108. (add-face-text-property beg (point) 'magit-diff-hunk-heading)
  2109. (magit-insert-heading)
  2110. (goto-char (point-max))
  2111. (insert ?\n)))))))
  2112. (defun magit-revision--wash-message ()
  2113. (let ((major-mode 'git-commit-mode))
  2114. (hack-dir-local-variables)
  2115. (hack-local-variables-apply))
  2116. (unless (memq git-commit-major-mode '(nil text-mode))
  2117. (funcall git-commit-major-mode)
  2118. (font-lock-ensure))
  2119. (buffer-string))
  2120. (defun magit-insert-revision-headers ()
  2121. "Insert headers about the commit into a revision buffer."
  2122. (magit-insert-section (headers)
  2123. (--when-let (magit-rev-format "%D" magit-buffer-revision "--decorate=full")
  2124. (insert (magit-format-ref-labels it) ?\s))
  2125. (insert (propertize
  2126. (magit-rev-parse (concat magit-buffer-revision "^{commit}"))
  2127. 'font-lock-face 'magit-hash))
  2128. (magit-insert-heading)
  2129. (let ((beg (point)))
  2130. (magit-rev-insert-format magit-revision-headers-format
  2131. magit-buffer-revision)
  2132. (magit-insert-revision-gravatars magit-buffer-revision beg))
  2133. (when magit-revision-insert-related-refs
  2134. (dolist (parent (magit-commit-parents magit-buffer-revision))
  2135. (magit-insert-section (commit parent)
  2136. (let ((line (magit-rev-format "%h %s" parent)))
  2137. (string-match "^\\([^ ]+\\) \\(.*\\)" line)
  2138. (magit-bind-match-strings (hash msg) line
  2139. (insert "Parent: ")
  2140. (insert (propertize hash 'font-lock-face 'magit-hash))
  2141. (insert " " msg "\n")))))
  2142. (magit--insert-related-refs
  2143. magit-buffer-revision "--merged" "Merged"
  2144. (eq magit-revision-insert-related-refs 'all))
  2145. (magit--insert-related-refs
  2146. magit-buffer-revision "--contains" "Contained"
  2147. (eq magit-revision-insert-related-refs '(all mixed)))
  2148. (when-let ((follows (magit-get-current-tag magit-buffer-revision t)))
  2149. (let ((tag (car follows))
  2150. (cnt (cadr follows)))
  2151. (magit-insert-section (tag tag)
  2152. (insert
  2153. (format "Follows: %s (%s)\n"
  2154. (propertize tag 'font-lock-face 'magit-tag)
  2155. (propertize (number-to-string cnt)
  2156. 'font-lock-face 'magit-branch-local))))))
  2157. (when-let ((precedes (magit-get-next-tag magit-buffer-revision t)))
  2158. (let ((tag (car precedes))
  2159. (cnt (cadr precedes)))
  2160. (magit-insert-section (tag tag)
  2161. (insert (format "Precedes: %s (%s)\n"
  2162. (propertize tag 'font-lock-face 'magit-tag)
  2163. (propertize (number-to-string cnt)
  2164. 'font-lock-face 'magit-tag))))))
  2165. (insert ?\n))))
  2166. (defun magit--insert-related-refs (rev arg title remote)
  2167. (when-let ((refs (magit-list-related-branches arg rev (and remote "-a"))))
  2168. (insert title ":" (make-string (- 10 (length title)) ?\s))
  2169. (dolist (branch refs)
  2170. (if (<= (+ (current-column) 1 (length branch))
  2171. (window-width))
  2172. (insert ?\s)
  2173. (insert ?\n (make-string 12 ?\s)))
  2174. (magit-insert-section (branch branch)
  2175. (insert (propertize branch 'font-lock-face
  2176. (if (string-prefix-p "remotes/" branch)
  2177. 'magit-branch-remote
  2178. 'magit-branch-local)))))
  2179. (insert ?\n)))
  2180. (defun magit-insert-revision-gravatars (rev beg)
  2181. (when (and magit-revision-show-gravatars
  2182. (window-system))
  2183. (require 'gravatar)
  2184. (pcase-let ((`(,author . ,committer)
  2185. (pcase magit-revision-show-gravatars
  2186. (`t '("^Author: " . "^Commit: "))
  2187. (`author '("^Author: " . nil))
  2188. (`committer '(nil . "^Commit: "))
  2189. (_ magit-revision-show-gravatars))))
  2190. (--when-let (and author (magit-rev-format "%aE" rev))
  2191. (magit-insert-revision-gravatar beg rev it author))
  2192. (--when-let (and committer (magit-rev-format "%cE" rev))
  2193. (magit-insert-revision-gravatar beg rev it committer)))))
  2194. (defun magit-insert-revision-gravatar (beg rev email regexp)
  2195. (save-excursion
  2196. (goto-char beg)
  2197. (when (re-search-forward regexp nil t)
  2198. (when-let ((window (get-buffer-window)))
  2199. (let* ((column (length (match-string 0)))
  2200. (font-obj (query-font (font-at (point) window)))
  2201. (size (* 2 (+ (aref font-obj 4)
  2202. (aref font-obj 5))))
  2203. (align-to (+ column
  2204. (ceiling (/ size (aref font-obj 7) 1.0))
  2205. 1))
  2206. (gravatar-size (- size 2)))
  2207. (ignore-errors ; service may be unreachable
  2208. (gravatar-retrieve email 'magit-insert-revision-gravatar-cb
  2209. (list rev (point-marker) align-to column))))))))
  2210. (defun magit-insert-revision-gravatar-cb (image rev marker align-to column)
  2211. (unless (eq image 'error)
  2212. (when-let ((buffer (marker-buffer marker)))
  2213. (with-current-buffer buffer
  2214. (save-excursion
  2215. (goto-char marker)
  2216. ;; The buffer might display another revision by now or
  2217. ;; it might have been refreshed, in which case another
  2218. ;; process might already have inserted the image.
  2219. (when (and (equal rev magit-buffer-revision)
  2220. (not (eq (car-safe
  2221. (car-safe
  2222. (get-text-property (point) 'display)))
  2223. 'image)))
  2224. (let ((top `((,@image :ascent center :relief 1)
  2225. (slice 0.0 0.0 1.0 0.5)))
  2226. (bot `((,@image :ascent center :relief 1)
  2227. (slice 0.0 0.5 1.0 1.0)))
  2228. (align `((space :align-to ,align-to))))
  2229. (when magit-revision-use-gravatar-kludge
  2230. (cl-rotatef top bot))
  2231. (let ((inhibit-read-only t))
  2232. (insert (propertize " " 'display top))
  2233. (insert (propertize " " 'display align))
  2234. (forward-line)
  2235. (forward-char column)
  2236. (insert (propertize " " 'display bot))
  2237. (insert (propertize " " 'display align))))))))))
  2238. ;;; Merge-Preview Mode
  2239. (define-derived-mode magit-merge-preview-mode magit-diff-mode "Magit Merge"
  2240. "Mode for previewing a merge."
  2241. :group 'magit-diff
  2242. (hack-dir-local-variables-non-file-buffer))
  2243. (put 'magit-merge-preview-mode 'magit-diff-default-arguments
  2244. '("--no-ext-diff"))
  2245. (defun magit-merge-preview-setup-buffer (rev)
  2246. (magit-setup-buffer #'magit-merge-preview-mode nil
  2247. (magit-buffer-revision rev)
  2248. (magit-buffer-range (format "%s^..%s" rev rev))))
  2249. (defun magit-merge-preview-refresh-buffer ()
  2250. (let* ((branch (magit-get-current-branch))
  2251. (head (or branch (magit-rev-verify "HEAD"))))
  2252. (magit-set-header-line-format (format "Preview merge of %s into %s"
  2253. magit-buffer-revision
  2254. (or branch "HEAD")))
  2255. (magit-insert-section (diffbuf)
  2256. (magit--insert-diff
  2257. "merge-tree" (magit-git-string "merge-base" head magit-buffer-revision)
  2258. head magit-buffer-revision))))
  2259. (cl-defmethod magit-buffer-value (&context (major-mode magit-merge-preview-mode))
  2260. magit-buffer-revision)
  2261. ;;; Diff Sections
  2262. (defvar magit-unstaged-section-map
  2263. (let ((map (make-sparse-keymap)))
  2264. (define-key map [remap magit-visit-thing] 'magit-diff-unstaged)
  2265. (define-key map [remap magit-delete-thing] 'magit-discard)
  2266. (define-key map "s" 'magit-stage)
  2267. (define-key map "u" 'magit-unstage)
  2268. map)
  2269. "Keymap for the `unstaged' section.")
  2270. (magit-define-section-jumper magit-jump-to-unstaged "Unstaged changes" unstaged)
  2271. (defun magit-insert-unstaged-changes ()
  2272. "Insert section showing unstaged changes."
  2273. (magit-insert-section (unstaged)
  2274. (magit-insert-heading "Unstaged changes:")
  2275. (magit--insert-diff
  2276. "diff" magit-buffer-diff-args "--no-prefix"
  2277. "--" magit-buffer-diff-files)))
  2278. (defvar magit-staged-section-map
  2279. (let ((map (make-sparse-keymap)))
  2280. (define-key map [remap magit-visit-thing] 'magit-diff-staged)
  2281. (define-key map [remap magit-delete-thing] 'magit-discard)
  2282. (define-key map [remap magit-revert-no-commit] 'magit-reverse)
  2283. (define-key map "s" 'magit-stage)
  2284. (define-key map "u" 'magit-unstage)
  2285. map)
  2286. "Keymap for the `staged' section.")
  2287. (magit-define-section-jumper magit-jump-to-staged "Staged changes" staged)
  2288. (defun magit-insert-staged-changes ()
  2289. "Insert section showing staged changes."
  2290. ;; Avoid listing all files as deleted when visiting a bare repo.
  2291. (unless (magit-bare-repo-p)
  2292. (magit-insert-section (staged)
  2293. (magit-insert-heading "Staged changes:")
  2294. (magit--insert-diff
  2295. "diff" "--cached" magit-buffer-diff-args "--no-prefix"
  2296. "--" magit-buffer-diff-files))))
  2297. ;;; Diff Type
  2298. (defun magit-diff-type (&optional section)
  2299. "Return the diff type of SECTION.
  2300. The returned type is one of the symbols `staged', `unstaged',
  2301. `committed', or `undefined'. This type serves a similar purpose
  2302. as the general type common to all sections (which is stored in
  2303. the `type' slot of the corresponding `magit-section' struct) but
  2304. takes additional information into account. When the SECTION
  2305. isn't related to diffs and the buffer containing it also isn't
  2306. a diff-only buffer, then return nil.
  2307. Currently the type can also be one of `tracked' and `untracked'
  2308. but these values are not handled explicitly everywhere they
  2309. should be and a possible fix could be to just return nil here.
  2310. The section has to be a `diff' or `hunk' section, or a section
  2311. whose children are of type `diff'. If optional SECTION is nil,
  2312. return the diff type for the current section. In buffers whose
  2313. major mode is `magit-diff-mode' SECTION is ignored and the type
  2314. is determined using other means. In `magit-revision-mode'
  2315. buffers the type is always `committed'.
  2316. Do not confuse this with `magit-diff-scope' (which see)."
  2317. (--when-let (or section (magit-current-section))
  2318. (cond ((derived-mode-p 'magit-revision-mode 'magit-stash-mode) 'committed)
  2319. ((derived-mode-p 'magit-diff-mode)
  2320. (let ((range magit-buffer-range)
  2321. (const magit-buffer-typearg))
  2322. (cond ((equal const "--no-index") 'undefined)
  2323. ((or (not range)
  2324. (magit-rev-eq range "HEAD"))
  2325. (if (equal const "--cached")
  2326. 'staged
  2327. 'unstaged))
  2328. ((equal const "--cached")
  2329. (if (magit-rev-head-p range)
  2330. 'staged
  2331. 'undefined)) ; i.e. committed and staged
  2332. (t 'committed))))
  2333. ((derived-mode-p 'magit-status-mode)
  2334. (let ((stype (oref it type)))
  2335. (if (memq stype '(staged unstaged tracked untracked))
  2336. stype
  2337. (pcase stype
  2338. ((or `file `module)
  2339. (let* ((parent (oref it parent))
  2340. (type (oref parent type)))
  2341. (if (memq type '(file module))
  2342. (magit-diff-type parent)
  2343. type)))
  2344. (`hunk (-> it
  2345. (oref parent)
  2346. (oref parent)
  2347. (oref type)))))))
  2348. ((derived-mode-p 'magit-log-mode)
  2349. (if (or (and (magit-section-match 'commit section)
  2350. (oref section children))
  2351. (magit-section-match [* file commit] section))
  2352. 'committed
  2353. 'undefined))
  2354. (t 'undefined))))
  2355. (cl-defun magit-diff-scope (&optional (section nil ssection) strict)
  2356. "Return the diff scope of SECTION or the selected section(s).
  2357. A diff's \"scope\" describes what part of a diff is selected, it is
  2358. a symbol, one of `region', `hunk', `hunks', `file', `files', or
  2359. `list'. Do not confuse this with the diff \"type\", as returned by
  2360. `magit-diff-type'.
  2361. If optional SECTION is non-nil, then return the scope of that,
  2362. ignoring the sections selected by the region. Otherwise return
  2363. the scope of the current section, or if the region is active and
  2364. selects a valid group of diff related sections, the type of these
  2365. sections, i.e. `hunks' or `files'. If SECTION, or if that is nil
  2366. the current section, is a `hunk' section; and the region region
  2367. starts and ends inside the body of a that section, then the type
  2368. is `region'. If the region is empty after a mouse click, then
  2369. `hunk' is returned instead of `region'.
  2370. If optional STRICT is non-nil, then return nil if the diff type of
  2371. the section at point is `untracked' or the section at point is not
  2372. actually a `diff' but a `diffstat' section."
  2373. (let ((siblings (and (not ssection) (magit-region-sections nil t))))
  2374. (setq section (or section (car siblings) (magit-current-section)))
  2375. (when (and section
  2376. (or (not strict)
  2377. (and (not (eq (magit-diff-type section) 'untracked))
  2378. (not (eq (--when-let (oref section parent)
  2379. (oref it type))
  2380. 'diffstat)))))
  2381. (pcase (list (oref section type)
  2382. (and siblings t)
  2383. (magit-diff-use-hunk-region-p)
  2384. ssection)
  2385. (`(hunk nil t ,_)
  2386. (if (magit-section-internal-region-p section) 'region 'hunk))
  2387. (`(hunk t t nil) 'hunks)
  2388. (`(hunk ,_ ,_ ,_) 'hunk)
  2389. (`(file t t nil) 'files)
  2390. (`(file ,_ ,_ ,_) 'file)
  2391. (`(module t t nil) 'files)
  2392. (`(module ,_ ,_ ,_) 'file)
  2393. (`(,(or `staged `unstaged `untracked)
  2394. nil ,_ ,_) 'list)))))
  2395. (defun magit-diff-use-hunk-region-p ()
  2396. (and (region-active-p)
  2397. ;; TODO implement this from first principals
  2398. ;; currently it's trial-and-error
  2399. (not (and (or (eq this-command 'mouse-drag-region)
  2400. (eq last-command 'mouse-drag-region)
  2401. ;; When another window was previously
  2402. ;; selected then the last-command is
  2403. ;; some byte-code function.
  2404. (byte-code-function-p last-command))
  2405. (eq (region-end) (region-beginning))))))
  2406. ;;; Diff Highlight
  2407. (defun magit-diff-unhighlight (section selection)
  2408. "Remove the highlighting of the diff-related SECTION."
  2409. (when (magit-hunk-section-p section)
  2410. (magit-diff-paint-hunk section selection nil)
  2411. t))
  2412. (defun magit-diff-highlight (section selection)
  2413. "Highlight the diff-related SECTION.
  2414. If SECTION is not a diff-related section, then do nothing and
  2415. return nil. If SELECTION is non-nil, then it is a list of sections
  2416. selected by the region, including SECTION. All of these sections
  2417. are highlighted."
  2418. (if (and (magit-section-match 'commit section)
  2419. (oref section children))
  2420. (progn (if selection
  2421. (dolist (section selection)
  2422. (magit-diff-highlight-list section selection))
  2423. (magit-diff-highlight-list section))
  2424. t)
  2425. (when-let ((scope (magit-diff-scope section t)))
  2426. (cond ((eq scope 'region)
  2427. (magit-diff-paint-hunk section selection t))
  2428. (selection
  2429. (dolist (section selection)
  2430. (magit-diff-highlight-recursive section selection)))
  2431. (t
  2432. (magit-diff-highlight-recursive section)))
  2433. t)))
  2434. (defun magit-diff-highlight-recursive (section &optional selection)
  2435. (pcase (magit-diff-scope section)
  2436. (`list (magit-diff-highlight-list section selection))
  2437. (`file (magit-diff-highlight-file section selection))
  2438. (`hunk (magit-diff-highlight-heading section selection)
  2439. (magit-diff-paint-hunk section selection t))
  2440. (_ (magit-section-highlight section nil))))
  2441. (defun magit-diff-highlight-list (section &optional selection)
  2442. (let ((beg (oref section start))
  2443. (cnt (oref section content))
  2444. (end (oref section end)))
  2445. (when (or (eq this-command 'mouse-drag-region)
  2446. (not selection))
  2447. (unless (and (region-active-p)
  2448. (<= (region-beginning) beg))
  2449. (magit-section-make-overlay beg cnt 'magit-section-highlight))
  2450. (unless (oref section hidden)
  2451. (dolist (child (oref section children))
  2452. (when (or (eq this-command 'mouse-drag-region)
  2453. (not (and (region-active-p)
  2454. (<= (region-beginning)
  2455. (oref child start)))))
  2456. (magit-diff-highlight-recursive child selection)))))
  2457. (when magit-diff-highlight-hunk-body
  2458. (magit-section-make-overlay (1- end) end 'magit-section-highlight))))
  2459. (defun magit-diff-highlight-file (section &optional selection)
  2460. (magit-diff-highlight-heading section selection)
  2461. (unless (oref section hidden)
  2462. (dolist (child (oref section children))
  2463. (magit-diff-highlight-recursive child selection))))
  2464. (defun magit-diff-highlight-heading (section &optional selection)
  2465. (magit-section-make-overlay
  2466. (oref section start)
  2467. (or (oref section content)
  2468. (oref section end))
  2469. (pcase (list (oref section type)
  2470. (and (member section selection)
  2471. (not (eq this-command 'mouse-drag-region))))
  2472. (`(file t) 'magit-diff-file-heading-selection)
  2473. (`(file nil) 'magit-diff-file-heading-highlight)
  2474. (`(module t) 'magit-diff-file-heading-selection)
  2475. (`(module nil) 'magit-diff-file-heading-highlight)
  2476. (`(hunk t) 'magit-diff-hunk-heading-selection)
  2477. (`(hunk nil) 'magit-diff-hunk-heading-highlight))))
  2478. ;;; Hunk Paint
  2479. (cl-defun magit-diff-paint-hunk
  2480. (section &optional selection
  2481. (highlight (magit-section-selected-p section selection)))
  2482. (let (paint)
  2483. (unless magit-diff-highlight-hunk-body
  2484. (setq highlight nil))
  2485. (cond (highlight
  2486. (unless (oref section hidden)
  2487. (add-to-list 'magit-section-highlighted-sections section)
  2488. (cond ((memq section magit-section-unhighlight-sections)
  2489. (setq magit-section-unhighlight-sections
  2490. (delq section magit-section-unhighlight-sections)))
  2491. (magit-diff-highlight-hunk-body
  2492. (setq paint t)))))
  2493. (t
  2494. (cond ((and (oref section hidden)
  2495. (memq section magit-section-unhighlight-sections))
  2496. (add-to-list 'magit-section-highlighted-sections section)
  2497. (setq magit-section-unhighlight-sections
  2498. (delq section magit-section-unhighlight-sections)))
  2499. (t
  2500. (setq paint t)))))
  2501. (when paint
  2502. (save-excursion
  2503. (goto-char (oref section start))
  2504. (let ((end (oref section end))
  2505. (merging (looking-at "@@@"))
  2506. (diff-type (magit-diff-type))
  2507. (stage nil)
  2508. (tab-width (magit-diff-tab-width
  2509. (magit-section-parent-value section))))
  2510. (forward-line)
  2511. (while (< (point) end)
  2512. (when (and magit-diff-hide-trailing-cr-characters
  2513. (char-equal ?\r (char-before (line-end-position))))
  2514. (put-text-property (1- (line-end-position)) (line-end-position)
  2515. 'invisible t))
  2516. (put-text-property
  2517. (point) (1+ (line-end-position)) 'font-lock-face
  2518. (cond
  2519. ((looking-at "^\\+\\+?\\([<=|>]\\)\\{7\\}")
  2520. (setq stage (pcase (list (match-string 1) highlight)
  2521. (`("<" nil) 'magit-diff-our)
  2522. (`("<" t) 'magit-diff-our-highlight)
  2523. (`("|" nil) 'magit-diff-base)
  2524. (`("|" t) 'magit-diff-base-highlight)
  2525. (`("=" nil) 'magit-diff-their)
  2526. (`("=" t) 'magit-diff-their-highlight)
  2527. (`(">" nil) nil)))
  2528. 'magit-diff-conflict-heading)
  2529. ((looking-at (if merging "^\\(\\+\\| \\+\\)" "^\\+"))
  2530. (magit-diff-paint-tab merging tab-width)
  2531. (magit-diff-paint-whitespace merging 'added diff-type)
  2532. (or stage
  2533. (if highlight 'magit-diff-added-highlight 'magit-diff-added)))
  2534. ((looking-at (if merging "^\\(-\\| -\\)" "^-"))
  2535. (magit-diff-paint-tab merging tab-width)
  2536. (magit-diff-paint-whitespace merging 'removed diff-type)
  2537. (if highlight 'magit-diff-removed-highlight 'magit-diff-removed))
  2538. (t
  2539. (magit-diff-paint-tab merging tab-width)
  2540. (magit-diff-paint-whitespace merging 'context diff-type)
  2541. (if highlight 'magit-diff-context-highlight 'magit-diff-context))))
  2542. (forward-line))))))
  2543. (magit-diff-update-hunk-refinement section))
  2544. (defvar magit-diff--tab-width-cache nil)
  2545. (defun magit-diff-tab-width (file)
  2546. (setq file (expand-file-name file))
  2547. (cl-flet ((cache (value)
  2548. (let ((elt (assoc file magit-diff--tab-width-cache)))
  2549. (if elt
  2550. (setcdr elt value)
  2551. (setq magit-diff--tab-width-cache
  2552. (cons (cons file value)
  2553. magit-diff--tab-width-cache))))
  2554. value))
  2555. (cond
  2556. ((not magit-diff-adjust-tab-width)
  2557. tab-width)
  2558. ((--when-let (find-buffer-visiting file)
  2559. (cache (buffer-local-value 'tab-width it))))
  2560. ((--when-let (assoc file magit-diff--tab-width-cache)
  2561. (or (cdr it)
  2562. tab-width)))
  2563. ((or (eq magit-diff-adjust-tab-width 'always)
  2564. (and (numberp magit-diff-adjust-tab-width)
  2565. (>= magit-diff-adjust-tab-width
  2566. (nth 7 (file-attributes file)))))
  2567. (cache (buffer-local-value 'tab-width (find-file-noselect file))))
  2568. (t
  2569. (cache nil)
  2570. tab-width))))
  2571. (defun magit-diff-paint-tab (merging width)
  2572. (save-excursion
  2573. (forward-char (if merging 2 1))
  2574. (while (= (char-after) ?\t)
  2575. (put-text-property (point) (1+ (point))
  2576. 'display (list (list 'space :width width)))
  2577. (forward-char))))
  2578. (defun magit-diff-paint-whitespace (merging line-type diff-type)
  2579. (when (and magit-diff-paint-whitespace
  2580. (or (not (memq magit-diff-paint-whitespace '(uncommitted status)))
  2581. (memq diff-type '(staged unstaged)))
  2582. (cl-case line-type
  2583. (added t)
  2584. (removed (memq magit-diff-paint-whitespace-lines '(all both)))
  2585. (context (memq magit-diff-paint-whitespace-lines '(all)))))
  2586. (let ((prefix (if merging "^[-\\+\s]\\{2\\}" "^[-\\+\s]"))
  2587. (indent
  2588. (if (local-variable-p 'magit-diff-highlight-indentation)
  2589. magit-diff-highlight-indentation
  2590. (setq-local
  2591. magit-diff-highlight-indentation
  2592. (cdr (--first (string-match-p (car it) default-directory)
  2593. (nreverse
  2594. (default-value
  2595. 'magit-diff-highlight-indentation))))))))
  2596. (when (and magit-diff-highlight-trailing
  2597. (looking-at (concat prefix ".*?\\([ \t]+\\)$")))
  2598. (let ((ov (make-overlay (match-beginning 1) (match-end 1) nil t)))
  2599. (overlay-put ov 'font-lock-face 'magit-diff-whitespace-warning)
  2600. (overlay-put ov 'priority 2)
  2601. (overlay-put ov 'evaporate t)))
  2602. (when (or (and (eq indent 'tabs)
  2603. (looking-at (concat prefix "\\( *\t[ \t]*\\)")))
  2604. (and (integerp indent)
  2605. (looking-at (format "%s\\([ \t]* \\{%s,\\}[ \t]*\\)"
  2606. prefix indent))))
  2607. (let ((ov (make-overlay (match-beginning 1) (match-end 1) nil t)))
  2608. (overlay-put ov 'font-lock-face 'magit-diff-whitespace-warning)
  2609. (overlay-put ov 'priority 2)
  2610. (overlay-put ov 'evaporate t))))))
  2611. (defun magit-diff-update-hunk-refinement (&optional section)
  2612. (if section
  2613. (unless (oref section hidden)
  2614. (pcase (list magit-diff-refine-hunk
  2615. (oref section refined)
  2616. (eq section (magit-current-section)))
  2617. ((or `(all nil ,_) `(t nil t))
  2618. (oset section refined t)
  2619. (save-excursion
  2620. (goto-char (oref section start))
  2621. ;; `diff-refine-hunk' does not handle combined diffs.
  2622. (unless (looking-at "@@@")
  2623. (let ((smerge-refine-ignore-whitespace
  2624. magit-diff-refine-ignore-whitespace)
  2625. ;; Avoid fsyncing many small temp files
  2626. (write-region-inhibit-fsync t))
  2627. (diff-refine-hunk)))))
  2628. ((or `(nil t ,_) `(t t nil))
  2629. (oset section refined nil)
  2630. (remove-overlays (oref section start)
  2631. (oref section end)
  2632. 'diff-mode 'fine))))
  2633. (cl-labels ((recurse (section)
  2634. (if (magit-section-match 'hunk section)
  2635. (magit-diff-update-hunk-refinement section)
  2636. (dolist (child (oref section children))
  2637. (recurse child)))))
  2638. (recurse magit-root-section))))
  2639. ;;; Hunk Region
  2640. (defun magit-diff-hunk-region-beginning ()
  2641. (save-excursion (goto-char (region-beginning))
  2642. (line-beginning-position)))
  2643. (defun magit-diff-hunk-region-end ()
  2644. (save-excursion (goto-char (region-end))
  2645. (line-end-position)))
  2646. (defun magit-diff-update-hunk-region (section)
  2647. "Highlight the hunk-internal region if any."
  2648. (when (eq (magit-diff-scope section t) 'region)
  2649. (magit-diff--make-hunk-overlay
  2650. (oref section start)
  2651. (1- (oref section content))
  2652. 'font-lock-face 'magit-diff-lines-heading
  2653. 'display (magit-diff-hunk-region-header section)
  2654. 'after-string (magit-diff--hunk-after-string 'magit-diff-lines-heading))
  2655. (run-hook-with-args 'magit-diff-highlight-hunk-region-functions section)
  2656. t))
  2657. (defun magit-diff-highlight-hunk-region-dim-outside (section)
  2658. "Dim the parts of the hunk that are outside the hunk-internal region.
  2659. This is done by using the same foreground and background color
  2660. for added and removed lines as for context lines."
  2661. (let ((face (if magit-diff-highlight-hunk-body
  2662. 'magit-diff-context-highlight
  2663. 'magit-diff-context)))
  2664. (when magit-diff-unmarked-lines-keep-foreground
  2665. (setq face (list :background (face-attribute face :background))))
  2666. (magit-diff--make-hunk-overlay (oref section content)
  2667. (magit-diff-hunk-region-beginning)
  2668. 'font-lock-face face
  2669. 'priority 2)
  2670. (magit-diff--make-hunk-overlay (1+ (magit-diff-hunk-region-end))
  2671. (oref section end)
  2672. 'font-lock-face face
  2673. 'priority 2)))
  2674. (defun magit-diff-highlight-hunk-region-using-face (_section)
  2675. "Highlight the hunk-internal region by making it bold.
  2676. Or rather highlight using the face `magit-diff-hunk-region', though
  2677. changing only the `:weight' and/or `:slant' is recommended for that
  2678. face."
  2679. (magit-diff--make-hunk-overlay (magit-diff-hunk-region-beginning)
  2680. (1+ (magit-diff-hunk-region-end))
  2681. 'font-lock-face 'magit-diff-hunk-region))
  2682. (defun magit-diff-highlight-hunk-region-using-overlays (section)
  2683. "Emphasize the hunk-internal region using delimiting horizontal lines.
  2684. This is implemented as single-pixel newlines places inside overlays."
  2685. (if (window-system)
  2686. (let ((beg (magit-diff-hunk-region-beginning))
  2687. (end (magit-diff-hunk-region-end))
  2688. (str (propertize
  2689. (concat (propertize "\s" 'display '(space :height (1)))
  2690. (propertize "\n" 'line-height t))
  2691. 'font-lock-face 'magit-diff-lines-boundary)))
  2692. (magit-diff--make-hunk-overlay beg (1+ beg) 'before-string str)
  2693. (magit-diff--make-hunk-overlay end (1+ end) 'after-string str))
  2694. (magit-diff-highlight-hunk-region-using-face section)))
  2695. (defun magit-diff-highlight-hunk-region-using-underline (section)
  2696. "Emphasize the hunk-internal region using delimiting horizontal lines.
  2697. This is implemented by overlining and underlining the first and
  2698. last (visual) lines of the region."
  2699. (if (window-system)
  2700. (let* ((beg (magit-diff-hunk-region-beginning))
  2701. (end (magit-diff-hunk-region-end))
  2702. (beg-eol (save-excursion (goto-char beg)
  2703. (end-of-visual-line)
  2704. (point)))
  2705. (end-bol (save-excursion (goto-char end)
  2706. (beginning-of-visual-line)
  2707. (point)))
  2708. (color (face-background 'magit-diff-lines-boundary nil t)))
  2709. (cl-flet ((ln (b e &rest face)
  2710. (magit-diff--make-hunk-overlay
  2711. b e 'font-lock-face face 'after-string
  2712. (magit-diff--hunk-after-string face))))
  2713. (if (= beg end-bol)
  2714. (ln beg beg-eol :overline color :underline color)
  2715. (ln beg beg-eol :overline color)
  2716. (ln end-bol end :underline color))))
  2717. (magit-diff-highlight-hunk-region-using-face section)))
  2718. (defun magit-diff--make-hunk-overlay (start end &rest args)
  2719. (let ((ov (make-overlay start end nil t)))
  2720. (overlay-put ov 'evaporate t)
  2721. (while args (overlay-put ov (pop args) (pop args)))
  2722. (push ov magit-region-overlays)
  2723. ov))
  2724. (defun magit-diff--hunk-after-string (face)
  2725. (propertize "\s"
  2726. 'font-lock-face face
  2727. 'display (list 'space :align-to
  2728. `(+ (0 . right)
  2729. ,(min (window-hscroll)
  2730. (- (line-end-position)
  2731. (line-beginning-position)))))
  2732. ;; This prevents the cursor from being rendered at the
  2733. ;; edge of the window.
  2734. 'cursor t))
  2735. ;;; Hunk Utilities
  2736. (defun magit-diff-inside-hunk-body-p ()
  2737. "Return non-nil if point is inside the body of a hunk."
  2738. (and (magit-section-match 'hunk)
  2739. (when-let ((content (oref (magit-current-section) content)))
  2740. (> (point) content))))
  2741. ;;; Diff Extract
  2742. (defun magit-diff-file-header (section)
  2743. (when (magit-hunk-section-p section)
  2744. (setq section (oref section parent)))
  2745. (when (magit-file-section-p section)
  2746. (oref section header)))
  2747. (defun magit-diff-hunk-region-header (section)
  2748. (let ((patch (magit-diff-hunk-region-patch section)))
  2749. (string-match "\n" patch)
  2750. (substring patch 0 (1- (match-end 0)))))
  2751. (defun magit-diff-hunk-region-patch (section &optional args)
  2752. (let ((op (if (member "--reverse" args) "+" "-"))
  2753. (sbeg (oref section start))
  2754. (rbeg (magit-diff-hunk-region-beginning))
  2755. (rend (region-end))
  2756. (send (oref section end))
  2757. (patch nil))
  2758. (save-excursion
  2759. (goto-char sbeg)
  2760. (while (< (point) send)
  2761. (looking-at "\\(.\\)\\([^\n]*\n\\)")
  2762. (cond ((or (string-match-p "[@ ]" (match-string-no-properties 1))
  2763. (and (>= (point) rbeg)
  2764. (<= (point) rend)))
  2765. (push (match-string-no-properties 0) patch))
  2766. ((equal op (match-string-no-properties 1))
  2767. (push (concat " " (match-string-no-properties 2)) patch)))
  2768. (forward-line)))
  2769. (let ((buffer-list-update-hook nil)) ; #3759
  2770. (with-temp-buffer
  2771. (insert (mapconcat #'identity (reverse patch) ""))
  2772. (diff-fixup-modifs (point-min) (point-max))
  2773. (setq patch (buffer-string))))
  2774. patch))
  2775. ;;; _
  2776. (provide 'magit-diff)
  2777. ;;; magit-diff.el ends here