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