config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

magit-log.el (81499B)


      1 ;;; magit-log.el --- Inspect Git history  -*- lexical-binding:t; coding:utf-8 -*-
      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 logs, including
     26 ;; special logs like cherry-logs, as well as for selecting a commit
     27 ;; from a log.
     28 
     29 ;;; Code:
     30 
     31 (require 'magit-core)
     32 (require 'magit-diff)
     33 
     34 (declare-function magit-blob-visit "magit-files" (blob-or-file))
     35 (declare-function magit-cherry-apply "magit-sequence" (commit &optional args))
     36 (declare-function magit-insert-head-branch-header "magit-status"
     37                   (&optional branch))
     38 (declare-function magit-insert-upstream-branch-header "magit-status"
     39                   (&optional branch pull keyword))
     40 (declare-function magit-read-file-from-rev "magit-files"
     41                   (rev prompt &optional default))
     42 (declare-function magit-rebase--get-state-lines "magit-sequence"
     43                   (file))
     44 (declare-function magit-show-commit "magit-diff"
     45                   (arg1 &optional arg2 arg3 arg4))
     46 (declare-function magit-reflog-format-subject "magit-reflog" (subject))
     47 (defvar magit-refs-focus-column-width)
     48 (defvar magit-refs-margin)
     49 (defvar magit-refs-show-commit-count)
     50 (defvar magit-buffer-margin)
     51 (defvar magit-status-margin)
     52 (defvar magit-status-sections-hook)
     53 
     54 (require 'ansi-color)
     55 (require 'crm)
     56 (require 'which-func)
     57 
     58 ;;; Options
     59 ;;;; Log Mode
     60 
     61 (defgroup magit-log nil
     62   "Inspect and manipulate Git history."
     63   :link '(info-link "(magit)Logging")
     64   :group 'magit-commands
     65   :group 'magit-modes)
     66 
     67 (defcustom magit-log-mode-hook nil
     68   "Hook run after entering Magit-Log mode."
     69   :group 'magit-log
     70   :type 'hook)
     71 
     72 (defcustom magit-log-remove-graph-args '("--follow" "--grep" "-G" "-S" "-L")
     73   "The log arguments that cause the `--graph' argument to be dropped.
     74 
     75 The default value lists the arguments that are incompatible with
     76 `--graph' and therefore must be dropped when that is used.  You
     77 can add additional arguments that are available in `magit-log',
     78 but I recommend that you don't do that.  Nowadays I would define
     79 this as a constant, but I am preserving it as an option, in case
     80 someone actually customized it."
     81   :package-version '(magit . "2.3.0")
     82   :group 'magit-log
     83   :type '(repeat (string :tag "Argument"))
     84   :options '("--follow" "--grep" "-G" "-S" "-L"))
     85 
     86 (defcustom magit-log-revision-headers-format "\
     87 %+b%+N
     88 Author:    %aN <%aE>
     89 Committer: %cN <%cE>"
     90   "Additional format string used with the `++header' argument."
     91   :package-version '(magit . "3.2.0")
     92   :group 'magit-log
     93   :type 'string)
     94 
     95 (defcustom magit-log-auto-more nil
     96   "Insert more log entries automatically when moving past the last entry.
     97 Only considered when moving past the last entry with
     98 `magit-goto-*-section' commands."
     99   :group 'magit-log
    100   :type 'boolean)
    101 
    102 (defcustom magit-log-margin '(t age magit-log-margin-width t 18)
    103   "Format of the margin in `magit-log-mode' buffers.
    104 
    105 The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
    106 
    107 If INIT is non-nil, then the margin is shown initially.
    108 STYLE controls how to format the author or committer date.
    109   It can be one of `age' (to show the age of the commit),
    110   `age-abbreviated' (to abbreviate the time unit to a character),
    111   or a string (suitable for `format-time-string') to show the
    112   actual date.  Option `magit-log-margin-show-committer-date'
    113   controls which date is being displayed.
    114 WIDTH controls the width of the margin.  This exists for forward
    115   compatibility and currently the value should not be changed.
    116 AUTHOR controls whether the name of the author is also shown by
    117   default.
    118 AUTHOR-WIDTH has to be an integer.  When the name of the author
    119   is shown, then this specifies how much space is used to do so."
    120   :package-version '(magit . "2.9.0")
    121   :group 'magit-log
    122   :group 'magit-margin
    123   :type magit-log-margin--custom-type
    124   :initialize #'magit-custom-initialize-reset
    125   :set (apply-partially #'magit-margin-set-variable 'magit-log-mode))
    126 
    127 (defcustom magit-log-margin-show-committer-date nil
    128   "Whether to show the committer date in the margin.
    129 
    130 This option only controls whether the committer date is displayed
    131 instead of the author date.  Whether some date is displayed in
    132 the margin and whether the margin is displayed at all is
    133 controlled by other options."
    134   :package-version '(magit . "3.0.0")
    135   :group 'magit-log
    136   :group 'magit-margin
    137   :type 'boolean)
    138 
    139 (defcustom magit-log-show-refname-after-summary nil
    140   "Whether to show refnames after commit summaries.
    141 This is useful if you use really long branch names."
    142   :package-version '(magit . "2.2.0")
    143   :group 'magit-log
    144   :type 'boolean)
    145 
    146 (defcustom magit-log-highlight-keywords t
    147   "Whether to highlight bracketed keywords in commit summaries."
    148   :package-version '(magit . "2.12.0")
    149   :group 'magit-log
    150   :type 'boolean)
    151 
    152 (defcustom magit-log-header-line-function #'magit-log-header-line-sentence
    153   "Function used to generate text shown in header line of log buffers."
    154   :package-version '(magit . "2.12.0")
    155   :group 'magit-log
    156   :type '(choice (function-item magit-log-header-line-arguments)
    157                  (function-item magit-log-header-line-sentence)
    158                  function))
    159 
    160 (defcustom magit-log-trace-definition-function #'magit-which-function
    161   "Function used to determine the function at point.
    162 This is used by the command `magit-log-trace-definition'.
    163 You should prefer `magit-which-function' over `which-function'
    164 because the latter may make use of Imenu's outdated cache."
    165   :package-version '(magit . "3.0.0")
    166   :group 'magit-log
    167   :type '(choice (function-item magit-which-function)
    168                  (function-item which-function)
    169                  (function-item add-log-current-defun)
    170                  function))
    171 
    172 (defcustom magit-log-color-graph-limit 256
    173   "Number of commits over which log graphs are not colored.
    174 When showing more commits than specified, then the `--color'
    175 argument is silently dropped.  This is necessary because the
    176 `ansi-color' library, which is used to turn control sequences
    177 into faces, is just too slow."
    178   :package-version '(magit . "4.0.0")
    179   :group 'magit-log
    180   :type 'number)
    181 
    182 (defcustom magit-log-show-signatures-limit 256
    183   "Number of commits over which signatures are not verified.
    184 When showing more commits than specified by this option, then the
    185 `--show-signature' argument, if specified, is silently dropped.
    186 This is necessary because checking the signature of a large
    187 number of commits is just too slow."
    188   :package-version '(magit . "4.0.0")
    189   :group 'magit-log
    190   :type 'number)
    191 
    192 (defface magit-log-graph
    193   '((((class color) (background light)) :foreground "grey30")
    194     (((class color) (background  dark)) :foreground "grey80"))
    195   "Face for the graph part of the log output."
    196   :group 'magit-faces)
    197 
    198 (defface magit-log-author
    199   '((((class color) (background light))
    200      :foreground "firebrick"
    201      :slant normal
    202      :weight normal)
    203     (((class color) (background  dark))
    204      :foreground "tomato"
    205      :slant normal
    206      :weight normal))
    207   "Face for the author part of the log output."
    208   :group 'magit-faces)
    209 
    210 (defface magit-log-date
    211   '((((class color) (background light))
    212      :foreground "grey30"
    213      :slant normal
    214      :weight normal)
    215     (((class color) (background  dark))
    216      :foreground "grey80"
    217      :slant normal
    218      :weight normal))
    219   "Face for the date part of the log output."
    220   :group 'magit-faces)
    221 
    222 (defface magit-header-line-log-select
    223   '((t :inherit bold))
    224   "Face for the `header-line' in `magit-log-select-mode'."
    225   :group 'magit-faces)
    226 
    227 ;;;; File Log
    228 
    229 (defcustom magit-log-buffer-file-locked t
    230   "Whether `magit-log-buffer-file-quick' uses a dedicated buffer."
    231   :package-version '(magit . "2.7.0")
    232   :group 'magit-commands
    233   :group 'magit-log
    234   :type 'boolean)
    235 
    236 ;;;; Select Mode
    237 
    238 (defcustom magit-log-select-show-usage 'both
    239   "Whether to show usage information when selecting a commit from a log.
    240 The message can be shown in the `echo-area' or the `header-line', or in
    241 `both' places.  If the value isn't one of these symbols, then it should
    242 be nil, in which case no usage information is shown."
    243   :package-version '(magit . "2.1.0")
    244   :group 'magit-log
    245   :type '(choice (const :tag "in echo-area" echo-area)
    246                  (const :tag "in header-line" header-line)
    247                  (const :tag "in both places" both)
    248                  (const :tag "nowhere")))
    249 
    250 (defcustom magit-log-select-margin
    251   (list (nth 0 magit-log-margin)
    252         (nth 1 magit-log-margin)
    253         'magit-log-margin-width t
    254         (nth 4 magit-log-margin))
    255   "Format of the margin in `magit-log-select-mode' buffers.
    256 
    257 The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
    258 
    259 If INIT is non-nil, then the margin is shown initially.
    260 STYLE controls how to format the author or committer date.
    261   It can be one of `age' (to show the age of the commit),
    262   `age-abbreviated' (to abbreviate the time unit to a character),
    263   or a string (suitable for `format-time-string') to show the
    264   actual date.  Option `magit-log-margin-show-committer-date'
    265   controls which date is being displayed.
    266 WIDTH controls the width of the margin.  This exists for forward
    267   compatibility and currently the value should not be changed.
    268 AUTHOR controls whether the name of the author is also shown by
    269   default.
    270 AUTHOR-WIDTH has to be an integer.  When the name of the author
    271   is shown, then this specifies how much space is used to do so."
    272   :package-version '(magit . "2.9.0")
    273   :group 'magit-log
    274   :group 'magit-margin
    275   :type magit-log-margin--custom-type
    276   :initialize #'magit-custom-initialize-reset
    277   :set-after '(magit-log-margin)
    278   :set (apply-partially #'magit-margin-set-variable 'magit-log-select-mode))
    279 
    280 ;;;; Cherry Mode
    281 
    282 (defcustom magit-cherry-sections-hook
    283   '(magit-insert-cherry-headers
    284     magit-insert-cherry-commits)
    285   "Hook run to insert sections into the cherry buffer."
    286   :package-version '(magit . "2.1.0")
    287   :group 'magit-log
    288   :type 'hook)
    289 
    290 (defcustom magit-cherry-margin
    291   (list (nth 0 magit-log-margin)
    292         (nth 1 magit-log-margin)
    293         'magit-log-margin-width t
    294         (nth 4 magit-log-margin))
    295   "Format of the margin in `magit-cherry-mode' buffers.
    296 
    297 The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
    298 
    299 If INIT is non-nil, then the margin is shown initially.
    300 STYLE controls how to format the author or committer date.
    301   It can be one of `age' (to show the age of the commit),
    302   `age-abbreviated' (to abbreviate the time unit to a character),
    303   or a string (suitable for `format-time-string') to show the
    304   actual date.  Option `magit-log-margin-show-committer-date'
    305   controls which date is being displayed.
    306 WIDTH controls the width of the margin.  This exists for forward
    307   compatibility and currently the value should not be changed.
    308 AUTHOR controls whether the name of the author is also shown by
    309   default.
    310 AUTHOR-WIDTH has to be an integer.  When the name of the author
    311   is shown, then this specifies how much space is used to do so."
    312   :package-version '(magit . "2.9.0")
    313   :group 'magit-log
    314   :group 'magit-margin
    315   :type magit-log-margin--custom-type
    316   :initialize #'magit-custom-initialize-reset
    317   :set-after '(magit-log-margin)
    318   :set (apply-partially #'magit-margin-set-variable 'magit-cherry-mode))
    319 
    320 ;;;; Log Sections
    321 
    322 (defcustom magit-log-section-commit-count 10
    323   "How many recent commits to show in certain log sections.
    324 How many recent commits `magit-insert-recent-commits' and
    325 `magit-insert-unpulled-from-upstream-or-recent' (provided
    326 the upstream isn't ahead of the current branch) show."
    327   :package-version '(magit . "2.1.0")
    328   :group 'magit-status
    329   :type 'number)
    330 
    331 (defcustom magit-log-merged-commit-count 20
    332   "How many surrounding commits to show for `magit-log-merged'.
    333 `magit-log-merged' will shows approximately half of this number
    334 commits before and half after."
    335   :package-version '(magit . "3.3.0")
    336   :group 'magit-log
    337   :type 'integer)
    338 
    339 ;;; Arguments
    340 ;;;; Prefix Classes
    341 
    342 (defclass magit-log-prefix (transient-prefix)
    343   ((history-key :initform 'magit-log)
    344    (major-mode  :initform 'magit-log-mode)))
    345 
    346 (defclass magit-log-refresh-prefix (magit-log-prefix)
    347   ((history-key :initform 'magit-log)
    348    (major-mode  :initform nil)))
    349 
    350 ;;;; Prefix Methods
    351 
    352 (cl-defmethod transient-init-value ((obj magit-log-prefix))
    353   (pcase-let ((`(,args ,files)
    354                (magit-log--get-value 'magit-log-mode
    355                                      magit-prefix-use-buffer-arguments)))
    356     (unless (eq transient-current-command 'magit-dispatch)
    357       (when-let ((file (magit-file-relative-name)))
    358         (setq files (list file))))
    359     (oset obj value (if files `(("--" ,@files) ,args) args))))
    360 
    361 (cl-defmethod transient-init-value ((obj magit-log-refresh-prefix))
    362   (oset obj value (if magit-buffer-log-files
    363                       `(("--" ,@magit-buffer-log-files)
    364                         ,magit-buffer-log-args)
    365                     magit-buffer-log-args)))
    366 
    367 (cl-defmethod transient-set-value ((obj magit-log-prefix))
    368   (magit-log--set-value obj))
    369 
    370 (cl-defmethod transient-save-value ((obj magit-log-prefix))
    371   (magit-log--set-value obj 'save))
    372 
    373 ;;;; Argument Access
    374 
    375 (defun magit-log-arguments (&optional mode)
    376   "Return the current log arguments."
    377   (if (memq transient-current-command '(magit-log magit-log-refresh))
    378       (magit--transient-args-and-files)
    379     (magit-log--get-value (or mode 'magit-log-mode))))
    380 
    381 (defun magit-log--get-value (mode &optional use-buffer-args)
    382   (unless use-buffer-args
    383     (setq use-buffer-args magit-direct-use-buffer-arguments))
    384   (let (args files)
    385     (cond
    386      ((and (memq use-buffer-args '(always selected current))
    387            (eq major-mode mode))
    388       (setq args  magit-buffer-log-args)
    389       (setq files magit-buffer-log-files))
    390      ((and (memq use-buffer-args '(always selected))
    391            (when-let ((buffer (magit-get-mode-buffer
    392                                mode nil
    393                                (eq use-buffer-args 'selected))))
    394              (setq args  (buffer-local-value 'magit-buffer-log-args buffer))
    395              (setq files (buffer-local-value 'magit-buffer-log-files buffer))
    396              t)))
    397      ((plist-member (symbol-plist mode) 'magit-log-current-arguments)
    398       (setq args (get mode 'magit-log-current-arguments)))
    399      ((when-let ((elt (assq (intern (format "magit-log:%s" mode))
    400                             transient-values)))
    401         (setq args (cdr elt))
    402         t))
    403      (t
    404       (setq args (get mode 'magit-log-default-arguments))))
    405     (list args files)))
    406 
    407 (defun magit-log--set-value (obj &optional save)
    408   (pcase-let* ((obj  (oref obj prototype))
    409                (mode (or (oref obj major-mode) major-mode))
    410                (key  (intern (format "magit-log:%s" mode)))
    411                (`(,args ,files) (magit--transient-args-and-files)))
    412     (put mode 'magit-log-current-arguments args)
    413     (when save
    414       (setf (alist-get key transient-values) args)
    415       (transient-save-values))
    416     (transient--history-push obj)
    417     (setq magit-buffer-log-args args)
    418     (unless (derived-mode-p 'magit-log-select-mode)
    419       (setq magit-buffer-log-files files))
    420     (magit-refresh)))
    421 
    422 ;;; Commands
    423 ;;;; Prefix Commands
    424 
    425 ;;;###autoload (autoload 'magit-log "magit-log" nil t)
    426 (transient-define-prefix magit-log ()
    427   "Show a commit or reference log."
    428   :man-page "git-log"
    429   :class 'magit-log-prefix
    430   ;; The grouping in git-log(1) appears to be guided by implementation
    431   ;; details, so our logical grouping only follows it to an extend.
    432   ;; Arguments that are "misplaced" here:
    433   ;;   1. From "Commit Formatting".
    434   ;;   2. From "Common Diff Options".
    435   ;;   3. From unnamed first group.
    436   ;;   4. Implemented by Magit.
    437   ["Commit limiting"
    438    (magit-log:-n)
    439    (magit:--author)
    440    (7 magit-log:--since)
    441    (7 magit-log:--until)
    442    (magit-log:--grep)
    443    (7 "-i" "Search case-insensitive" ("-i" "--regexp-ignore-case"))
    444    (7 "-I" "Invert search pattern"   "--invert-grep")
    445    (magit-log:-G)     ;2
    446    (magit-log:-S)     ;2
    447    (magit-log:-L)     ;2
    448    (7 "=m" "Omit merges"            "--no-merges")
    449    (7 "=p" "First parent"           "--first-parent")]
    450   ["History simplification"
    451    (  "-D" "Simplify by decoration"                  "--simplify-by-decoration")
    452    (magit:--)
    453    (  "-f" "Follow renames when showing single-file log"     "--follow") ;3
    454    (6 "/s" "Only commits changing given paths"               "--sparse")
    455    (7 "/d" "Only selected commits plus meaningful history"   "--dense")
    456    (7 "/a" "Only commits existing directly on ancestry path" "--ancestry-path")
    457    (6 "/f" "Do not prune history"                            "--full-history")
    458    (7 "/m" "Prune some history"                              "--simplify-merges")]
    459   ["Commit ordering"
    460    (magit-log:--*-order)
    461    ("-r" "Reverse order" "--reverse")]
    462   ["Formatting"
    463    ("-g" "Show graph"          "--graph")          ;1
    464    ("-c" "Show graph in color" "--color")          ;2
    465    ("-d" "Show refnames"       "--decorate")       ;3
    466    ("=S" "Show signatures"     "--show-signature") ;1
    467    ("-h" "Show header"         "++header")         ;4
    468    ("-p" "Show diffs"          ("-p" "--patch"))   ;2
    469    ("-s" "Show diffstats"      "--stat")]          ;2
    470   [["Log"
    471     ("l" "current"             magit-log-current)
    472     ("h" "HEAD"                magit-log-head)
    473     ("u" "related"             magit-log-related)
    474     ("o" "other"               magit-log-other)]
    475    [""
    476     ("L" "local branches"      magit-log-branches)
    477     ("b" "all branches"        magit-log-all-branches)
    478     ("a" "all references"      magit-log-all)
    479     (7 "B" "matching branches" magit-log-matching-branches)
    480     (7 "T" "matching tags"     magit-log-matching-tags)
    481     (7 "m" "merged"            magit-log-merged)]
    482    ["Reflog"
    483     ("r" "current"             magit-reflog-current)
    484     ("H" "HEAD"                magit-reflog-head)
    485     ("O" "other"               magit-reflog-other)]
    486    [:if (lambda ()
    487           (and (fboundp 'magit--any-wip-mode-enabled-p)
    488                (magit--any-wip-mode-enabled-p)))
    489     :description "Wiplog"
    490     ("i" "index"          magit-wip-log-index)
    491     ("w" "worktree"       magit-wip-log-worktree)]
    492    ["Other"
    493     (5 "s" "shortlog"    magit-shortlog)]])
    494 
    495 ;;;###autoload (autoload 'magit-log-refresh "magit-log" nil t)
    496 (transient-define-prefix magit-log-refresh ()
    497   "Change the arguments used for the log(s) in the current buffer."
    498   :man-page "git-log"
    499   :class 'magit-log-refresh-prefix
    500   [:if-mode magit-log-mode
    501    :class transient-subgroups
    502    ["Commit limiting"
    503     (magit-log:-n)
    504     (magit:--author)
    505     (magit-log:--grep)
    506     (7 "-i" "Search case-insensitive" ("-i" "--regexp-ignore-case"))
    507     (7 "-I" "Invert search pattern"   "--invert-grep")
    508     (magit-log:-G)
    509     (magit-log:-S)
    510     (magit-log:-L)]
    511    ["History simplification"
    512     (  "-D" "Simplify by decoration"                  "--simplify-by-decoration")
    513     (magit:--)
    514     (  "-f" "Follow renames when showing single-file log"     "--follow") ;3
    515     (6 "/s" "Only commits changing given paths"               "--sparse")
    516     (7 "/d" "Only selected commits plus meaningful history"   "--dense")
    517     (7 "/a" "Only commits existing directly on ancestry path" "--ancestry-path")
    518     (6 "/f" "Do not prune history"                            "--full-history")
    519     (7 "/m" "Prune some history"                              "--simplify-merges")]
    520    ["Commit ordering"
    521     (magit-log:--*-order)
    522     ("-r" "Reverse order" "--reverse")]
    523    ["Formatting"
    524     ("-g" "Show graph"              "--graph")
    525     ("-c" "Show graph in color"     "--color")
    526     ("-d" "Show refnames"           "--decorate")
    527     ("=S" "Show signatures"         "--show-signature")
    528     ("-h" "Show header"             "++header")
    529     ("-p" "Show diffs"              ("-p" "--patch"))
    530     ("-s" "Show diffstats"          "--stat")]]
    531   [:if-not-mode magit-log-mode
    532    :description "Arguments"
    533    (magit-log:-n)
    534    (magit-log:--*-order)
    535    ("-g" "Show graph"               "--graph")
    536    ("-c" "Show graph in color"      "--color")
    537    ("-d" "Show refnames"            "--decorate")]
    538   [["Refresh"
    539     ("g" "buffer"                   magit-log-refresh)
    540     ("s" "buffer and set defaults"  transient-set-and-exit)
    541     ("w" "buffer and save defaults" transient-save-and-exit)]
    542    ["Margin"
    543     (magit-toggle-margin)
    544     (magit-cycle-margin-style)
    545     (magit-toggle-margin-details)
    546     (magit-toggle-log-margin-style)]
    547    [:if-mode magit-log-mode
    548     :description "Toggle"
    549     ("b" "buffer lock"              magit-toggle-buffer-lock)]]
    550   (interactive)
    551   (cond
    552    ((not (eq transient-current-command 'magit-log-refresh))
    553     (pcase major-mode
    554       ('magit-reflog-mode
    555        (user-error "Cannot change log arguments in reflog buffers"))
    556       ('magit-cherry-mode
    557        (user-error "Cannot change log arguments in cherry buffers")))
    558     (transient-setup 'magit-log-refresh))
    559    (t
    560     (pcase-let ((`(,args ,files) (magit-log-arguments)))
    561       (setq magit-buffer-log-args args)
    562       (unless (derived-mode-p 'magit-log-select-mode)
    563         (setq magit-buffer-log-files files)))
    564     (magit-refresh))))
    565 
    566 ;;;; Infix Commands
    567 
    568 (transient-define-argument magit-log:-n ()
    569   :description "Limit number of commits"
    570   :class 'transient-option
    571   ;; For historic reasons (and because it easy to guess what "-n"
    572   ;; stands for) this is the only argument where we do not use the
    573   ;; long argument ("--max-count").
    574   :shortarg "-n"
    575   :argument "-n"
    576   :reader #'transient-read-number-N+)
    577 
    578 (transient-define-argument magit:--author ()
    579   :description "Limit to author"
    580   :class 'transient-option
    581   :key "-A"
    582   :argument "--author="
    583   :reader #'magit-transient-read-person)
    584 
    585 (transient-define-argument magit-log:--since ()
    586   :description "Limit to commits since"
    587   :class 'transient-option
    588   :key "=s"
    589   :argument "--since="
    590   :reader #'transient-read-date)
    591 
    592 (transient-define-argument magit-log:--until ()
    593   :description "Limit to commits until"
    594   :class 'transient-option
    595   :key "=u"
    596   :argument "--until="
    597   :reader #'transient-read-date)
    598 
    599 (transient-define-argument magit-log:--*-order ()
    600   :description "Order commits by"
    601   :class 'transient-switches
    602   :key "-o"
    603   :argument-format "--%s-order"
    604   :argument-regexp "\\(--\\(topo\\|author-date\\|date\\)-order\\)"
    605   :choices '("topo" "author-date" "date"))
    606 
    607 (transient-define-argument magit-log:--grep ()
    608   :description "Search messages"
    609   :class 'transient-option
    610   :key "-F"
    611   :argument "--grep=")
    612 
    613 (transient-define-argument magit-log:-G ()
    614   :description "Search changes"
    615   :class 'transient-option
    616   :argument "-G")
    617 
    618 (transient-define-argument magit-log:-S ()
    619   :description "Search occurrences"
    620   :class 'transient-option
    621   :argument "-S")
    622 
    623 (transient-define-argument magit-log:-L ()
    624   :description "Trace line evolution"
    625   :class 'transient-option
    626   :argument "-L"
    627   :reader #'magit-read-file-trace)
    628 
    629 (defun magit-read-file-trace (&rest _ignored)
    630   (let ((file  (magit-read-file-from-rev "HEAD" "File"))
    631         (trace (magit-read-string "Trace")))
    632     (concat trace ":" file)))
    633 
    634 ;;;; Setup Commands
    635 
    636 (defvar-keymap magit-log-read-revs-map
    637   :parent crm-local-completion-map
    638   "SPC" #'self-insert-command)
    639 
    640 (defun magit-log-read-revs (&optional use-current)
    641   (or (and use-current (and-let* ((buf (magit-get-current-branch))) (list buf)))
    642       (let ((crm-separator "\\(\\.\\.\\.?\\|[, ]\\)")
    643             (crm-local-completion-map magit-log-read-revs-map))
    644         (split-string (magit-completing-read-multiple
    645                        "Log rev,s: "
    646                        (magit-list-refnames nil t)
    647                        nil nil nil 'magit-revision-history
    648                        (or (magit-branch-or-commit-at-point)
    649                            (and (not use-current)
    650                                 (magit-get-previous-branch)))
    651                        nil t)
    652                       "[, ]" t))))
    653 
    654 (defun magit-log-read-pattern (option)
    655   "Read a string from the user to pass as parameter to OPTION."
    656   (magit-read-string (format "Type a pattern to pass to %s" option)))
    657 
    658 ;;;###autoload
    659 (defun magit-log-current (revs &optional args files)
    660   "Show log for the current branch.
    661 When `HEAD' is detached or with a prefix argument show log for
    662 one or more revs read from the minibuffer."
    663   (interactive (cons (magit-log-read-revs t)
    664                      (magit-log-arguments)))
    665   (magit-log-setup-buffer revs args files))
    666 
    667 ;;;###autoload
    668 (defun magit-log-head (&optional args files)
    669   "Show log for `HEAD'."
    670   (interactive (magit-log-arguments))
    671   (magit-log-setup-buffer (list "HEAD") args files))
    672 
    673 ;;;###autoload
    674 (defun magit-log-related (revs &optional args files)
    675   "Show log for the current branch, its upstream and its push target.
    676 When the upstream is a local branch, then also show its own
    677 upstream.  When `HEAD' is detached, then show log for that, the
    678 previously checked out branch and its upstream and push-target."
    679   (interactive
    680    (cons (let ((current (magit-get-current-branch))
    681                head rebase target upstream upup)
    682            (unless current
    683              (setq rebase (magit-rebase--get-state-lines "head-name"))
    684              (cond (rebase
    685                     (setq rebase (magit-ref-abbrev rebase))
    686                     (setq current rebase)
    687                     (setq head "HEAD"))
    688                    ((setq current (magit-get-previous-branch)))))
    689            (cond (current
    690                   (setq current
    691                         (magit--propertize-face current 'magit-branch-local))
    692                   (setq target (magit-get-push-branch current t))
    693                   (setq upstream (magit-get-upstream-branch current))
    694                   (when upstream
    695                     (setq upup (and (magit-local-branch-p upstream)
    696                                     (magit-get-upstream-branch upstream)))))
    697                  ((setq head "HEAD")))
    698            (delq nil (list current head target upstream upup)))
    699          (magit-log-arguments)))
    700   (magit-log-setup-buffer revs args files))
    701 
    702 ;;;###autoload
    703 (defun magit-log-other (revs &optional args files)
    704   "Show log for one or more revs read from the minibuffer.
    705 The user can input any revision or revisions separated by a
    706 space, or even ranges, but only branches and tags, and a
    707 representation of the commit at point, are available as
    708 completion candidates."
    709   (interactive (cons (magit-log-read-revs)
    710                      (magit-log-arguments)))
    711   (magit-log-setup-buffer revs args files))
    712 
    713 ;;;###autoload
    714 (defun magit-log-branches (&optional args files)
    715   "Show log for all local branches and `HEAD'."
    716   (interactive (magit-log-arguments))
    717   (magit-log-setup-buffer (if (magit-get-current-branch)
    718                               (list "--branches")
    719                             (list "HEAD" "--branches"))
    720                           args files))
    721 
    722 ;;;###autoload
    723 (defun magit-log-matching-branches (pattern &optional args files)
    724   "Show log for all branches matching PATTERN and `HEAD'."
    725   (interactive (cons (magit-log-read-pattern "--branches") (magit-log-arguments)))
    726   (magit-log-setup-buffer
    727    (list "HEAD" (format "--branches=%s" pattern))
    728    args files))
    729 
    730 ;;;###autoload
    731 (defun magit-log-matching-tags (pattern &optional args files)
    732   "Show log for all tags matching PATTERN and `HEAD'."
    733   (interactive (cons (magit-log-read-pattern "--tags") (magit-log-arguments)))
    734   (magit-log-setup-buffer
    735    (list "HEAD" (format "--tags=%s" pattern))
    736    args files))
    737 
    738 ;;;###autoload
    739 (defun magit-log-all-branches (&optional args files)
    740   "Show log for all local and remote branches and `HEAD'."
    741   (interactive (magit-log-arguments))
    742   (magit-log-setup-buffer (if (magit-get-current-branch)
    743                               (list "--branches" "--remotes")
    744                             (list "HEAD" "--branches" "--remotes"))
    745                           args files))
    746 
    747 ;;;###autoload
    748 (defun magit-log-all (&optional args files)
    749   "Show log for all references and `HEAD'."
    750   (interactive (magit-log-arguments))
    751   (magit-log-setup-buffer (if (magit-get-current-branch)
    752                               (list "--all")
    753                             (list "HEAD" "--all"))
    754                           args files))
    755 
    756 ;;;###autoload
    757 (defun magit-log-buffer-file (&optional follow beg end)
    758   "Show log for the blob or file visited in the current buffer.
    759 With a prefix argument or when `--follow' is an active log
    760 argument, then follow renames.  When the region is active,
    761 restrict the log to the lines that the region touches."
    762   (interactive
    763    (cons current-prefix-arg
    764          (and (region-active-p)
    765               (magit-file-relative-name)
    766               (not (derived-mode-p 'dired-mode))
    767               (save-restriction
    768                 (widen)
    769                 (list (line-number-at-pos (region-beginning))
    770                       (line-number-at-pos
    771                        (let ((end (region-end)))
    772                          (if (char-after end)
    773                              end
    774                            ;; Ensure that we don't get the line number
    775                            ;; of a trailing newline.
    776                            (1- end)))))))))
    777   (require 'magit)
    778   (if-let ((file (magit-file-relative-name)))
    779       (magit-log-setup-buffer
    780        (list (or magit-buffer-refname
    781                  (magit-get-current-branch)
    782                  "HEAD"))
    783        (let ((args (car (magit-log-arguments))))
    784          (when (and follow (not (member "--follow" args)))
    785            (push "--follow" args))
    786          (when (and beg end)
    787            (setq args (cons (format "-L%s,%s:%s" beg end file)
    788                             (cl-delete "-L" args :test
    789                                        #'string-prefix-p)))
    790            (setq file nil))
    791          args)
    792        (and file (list file))
    793        magit-log-buffer-file-locked)
    794     (user-error "Buffer isn't visiting a file")))
    795 
    796 ;;;###autoload
    797 (defun magit-log-trace-definition (file fn rev)
    798   "Show log for the definition at point."
    799   (interactive (list (or (magit-file-relative-name)
    800                          (user-error "Buffer isn't visiting a file"))
    801                      (or (funcall magit-log-trace-definition-function)
    802                          (user-error "No function at point found"))
    803                      (or magit-buffer-refname
    804                          (magit-get-current-branch)
    805                          "HEAD")))
    806   (require 'magit)
    807   (magit-log-setup-buffer
    808    (list rev)
    809    (cons (format "-L:%s%s:%s"
    810                  (string-replace ":" "\\:" (regexp-quote fn))
    811                  (if (derived-mode-p 'lisp-mode 'emacs-lisp-mode)
    812                      ;; Git doesn't treat "-" the same way as
    813                      ;; "_", leading to false-positives such as
    814                      ;; "foo-suffix" being considered a match
    815                      ;; for "foo".  Wing it.
    816                      "\\( \\|$\\)"
    817                    ;; We could use "\\b" here, but since Git
    818                    ;; already does something equivalent, that
    819                    ;; isn't necessary.
    820                    "")
    821                  file)
    822          (cl-delete "-L" (car (magit-log-arguments))
    823                     :test #'string-prefix-p))
    824    nil magit-log-buffer-file-locked))
    825 
    826 (defun magit-diff-trace-definition ()
    827   "Show log for the definition at point in a diff."
    828   (interactive)
    829   (pcase-let ((`(,buf ,pos) (magit-diff-visit-file--noselect)))
    830     (magit--with-temp-position buf pos
    831       (call-interactively #'magit-log-trace-definition))))
    832 
    833 ;;;###autoload
    834 (defun magit-log-merged (commit branch &optional args files)
    835   "Show log for the merge of COMMIT into BRANCH.
    836 
    837 More precisely, find merge commit M that brought COMMIT into
    838 BRANCH, and show the log of the range \"M^1..M\". If COMMIT is
    839 directly on BRANCH, then show approximately
    840 `magit-log-merged-commit-count' surrounding commits instead.
    841 
    842 This command requires git-when-merged, which is available from
    843 https://github.com/mhagger/git-when-merged."
    844   (interactive
    845    (append (let ((commit (magit-read-branch-or-commit "Log merge of commit")))
    846              (list commit
    847                    (magit-read-other-branch "Merged into" commit)))
    848            (magit-log-arguments)))
    849   (unless (magit-git-executable-find "git-when-merged")
    850     (user-error "This command requires git-when-merged (%s)"
    851                 "https://github.com/mhagger/git-when-merged"))
    852   (let (exit m)
    853     (with-temp-buffer
    854       (save-excursion
    855         (setq exit (magit-process-git t "when-merged" "-c"
    856                                       (magit-abbrev-arg)
    857                                       commit branch)))
    858       (setq m (buffer-substring-no-properties (point) (line-end-position))))
    859     (if (zerop exit)
    860         (magit-log-setup-buffer (list (format "%s^1..%s" m m))
    861                                 args files nil commit)
    862       ;; Output: "<ref><lots of spaces><message>".
    863       ;; This is not the same as `string-trim'.
    864       (setq m (string-trim-left (substring m (string-match " " m))))
    865       (if (equal m "Commit is directly on this branch.")
    866           (let* ((from (format "%s~%d" commit
    867                                (/ magit-log-merged-commit-count 2)))
    868                  (to (- (car (magit-rev-diff-count branch commit t))
    869                         (/ magit-log-merged-commit-count 2)))
    870                  (to (if (<= to 0)
    871                          branch
    872                        (format "%s~%s" branch to))))
    873             (unless (magit-rev-verify-commit from)
    874               (setq from (magit-git-string "rev-list" "--max-parents=0"
    875                                            commit)))
    876             (magit-log-setup-buffer (list (concat from ".." to))
    877                                     (cons "--first-parent" args)
    878                                     files nil commit))
    879         (user-error "Could not find when %s was merged into %s: %s"
    880                     commit branch m)))))
    881 
    882 ;;;; Limit Commands
    883 
    884 (defun magit-log-toggle-commit-limit ()
    885   "Toggle the number of commits the current log buffer is limited to.
    886 If the number of commits is currently limited, then remove that
    887 limit.  Otherwise set it to 256."
    888   (interactive)
    889   (magit-log-set-commit-limit (lambda (&rest _) nil)))
    890 
    891 (defun magit-log-double-commit-limit ()
    892   "Double the number of commits the current log buffer is limited to."
    893   (interactive)
    894   (magit-log-set-commit-limit '*))
    895 
    896 (defun magit-log-half-commit-limit ()
    897   "Half the number of commits the current log buffer is limited to."
    898   (interactive)
    899   (magit-log-set-commit-limit '/))
    900 
    901 (defun magit-log-set-commit-limit (fn)
    902   (let* ((val magit-buffer-log-args)
    903          (arg (--first (string-match "^-n\\([0-9]+\\)?$" it) val))
    904          (num (and arg (string-to-number (match-string 1 arg))))
    905          (num (if num (funcall fn num 2) 256)))
    906     (setq val (remove arg val))
    907     (setq magit-buffer-log-args
    908           (if (and num (> num 0))
    909               (cons (format "-n%d" num) val)
    910             val)))
    911   (magit-refresh))
    912 
    913 (defun magit-log-get-commit-limit (&optional args)
    914   (and-let* ((str (--first (string-match "^-n\\([0-9]+\\)?$" it)
    915                            (or args magit-buffer-log-args))))
    916     (string-to-number (match-string 1 str))))
    917 
    918 ;;;; Mode Commands
    919 
    920 (defun magit-log-bury-buffer (&optional arg)
    921   "Bury the current buffer or the revision buffer in the same frame.
    922 Like `magit-mode-bury-buffer' (which see) but with a negative
    923 prefix argument instead bury the revision buffer, provided it
    924 is displayed in the current frame."
    925   (interactive "p")
    926   (if (< arg 0)
    927       (let* ((buf (magit-get-mode-buffer 'magit-revision-mode))
    928              (win (and buf (get-buffer-window buf (selected-frame)))))
    929         (if win
    930             (with-selected-window win
    931               (with-current-buffer buf
    932                 (magit-mode-bury-buffer (> (abs arg) 1))))
    933           (user-error "No revision buffer in this frame")))
    934     (magit-mode-bury-buffer (> arg 1))))
    935 
    936 ;;;###autoload
    937 (defun magit-log-move-to-parent (&optional n)
    938   "Move to the Nth parent of the current commit."
    939   (interactive "p")
    940   (when (derived-mode-p 'magit-log-mode)
    941     (when (magit-section-match 'commit)
    942       (let* ((section (magit-current-section))
    943              (parent-rev (format "%s^%s" (oref section value) (or n 1))))
    944         (if-let ((parent-hash (magit-rev-parse "--short" parent-rev)))
    945             (if-let ((parent (--first (equal (oref it value)
    946                                              parent-hash)
    947                                       (magit-section-siblings section 'next))))
    948                 (magit-section-goto parent)
    949               (user-error
    950                (substitute-command-keys
    951                 (concat "Parent " parent-hash " not found.  Try typing "
    952                         "\\[magit-log-double-commit-limit] first"))))
    953           (user-error "Parent %s does not exist" parent-rev))))))
    954 
    955 (defun magit-log-move-to-revision (rev)
    956   "Read a revision and move to it in current log buffer.
    957 
    958 If the chosen reference or revision isn't being displayed in
    959 the current log buffer, then inform the user about that and do
    960 nothing else.
    961 
    962 If invoked outside any log buffer, then display the log buffer
    963 of the current repository first; creating it if necessary."
    964   (interactive
    965    (list (or (magit-completing-read
    966               "In log, jump to"
    967               (magit-list-refnames nil t)
    968               nil nil nil 'magit-revision-history
    969               (or (and-let* ((rev (magit-commit-at-point)))
    970                     (magit-rev-fixup-target rev))
    971                   (magit-get-current-branch)))
    972              (user-error "Nothing selected"))))
    973   (with-current-buffer
    974       (cond ((derived-mode-p 'magit-log-mode)
    975              (current-buffer))
    976             ((and-let* ((buf (magit-get-mode-buffer 'magit-log-mode)))
    977                (pop-to-buffer-same-window buf)))
    978             (t
    979              (apply #'magit-log-all-branches (magit-log-arguments))))
    980     (unless (magit-log-goto-commit-section (magit-rev-abbrev rev))
    981       (user-error "%s isn't visible in the current log buffer" rev))))
    982 
    983 ;;;; Shortlog Commands
    984 
    985 ;;;###autoload (autoload 'magit-shortlog "magit-log" nil t)
    986 (transient-define-prefix magit-shortlog ()
    987   "Show a history summary."
    988   :man-page "git-shortlog"
    989   :value '("--numbered" "--summary")
    990   ["Arguments"
    991    ("-n" "Sort by number of commits"      ("-n" "--numbered"))
    992    ("-s" "Show commit count summary only" ("-s" "--summary"))
    993    ("-e" "Show email addresses"           ("-e" "--email"))
    994    ("-g" "Group commits by" "--group="
    995     :choices ("author" "committer" "trailer:"))
    996    (7 "-f" "Format string" "--format=")
    997    (7 "-w" "Linewrap" "-w" :class transient-option)]
    998   ["Shortlog"
    999    ("s" "since" magit-shortlog-since)
   1000    ("r" "range" magit-shortlog-range)])
   1001 
   1002 (defun magit-git-shortlog (rev args)
   1003   (let ((dir default-directory))
   1004     (with-current-buffer (get-buffer-create "*magit-shortlog*")
   1005       (setq default-directory dir)
   1006       (setq buffer-read-only t)
   1007       (let ((inhibit-read-only t))
   1008         (erase-buffer)
   1009         (save-excursion
   1010           (magit-git-insert "shortlog" args rev))
   1011         (switch-to-buffer-other-window (current-buffer))))))
   1012 
   1013 ;;;###autoload
   1014 (defun magit-shortlog-since (rev args)
   1015   "Show a history summary for commits since REV."
   1016   (interactive
   1017    (list (magit-read-branch-or-commit "Shortlog since" (magit-get-current-tag))
   1018          (transient-args 'magit-shortlog)))
   1019   (magit-git-shortlog (concat rev "..") args))
   1020 
   1021 ;;;###autoload
   1022 (defun magit-shortlog-range (rev-or-range args)
   1023   "Show a history summary for commit or range REV-OR-RANGE."
   1024   (interactive
   1025    (list (magit-read-range-or-commit "Shortlog for revision or range")
   1026          (transient-args 'magit-shortlog)))
   1027   (magit-git-shortlog rev-or-range args))
   1028 
   1029 ;;; Log Mode
   1030 
   1031 (defvar magit-log-disable-graph-hack-args
   1032   '("-G" "--grep" "--author")
   1033   "Arguments which disable the graph speedup hack.")
   1034 
   1035 (defvar-keymap magit-log-mode-map
   1036   :doc "Keymap for `magit-log-mode'."
   1037   :parent magit-mode-map
   1038   "C-c C-b" #'magit-go-backward
   1039   "C-c C-f" #'magit-go-forward
   1040   "C-c C-n" #'magit-log-move-to-parent
   1041   "j" #'magit-log-move-to-revision
   1042   "=" #'magit-log-toggle-commit-limit
   1043   "+" #'magit-log-double-commit-limit
   1044   "-" #'magit-log-half-commit-limit
   1045   "q" #'magit-log-bury-buffer)
   1046 
   1047 (define-derived-mode magit-log-mode magit-mode "Magit Log"
   1048   "Mode for looking at Git log.
   1049 
   1050 This mode is documented in info node `(magit)Log Buffer'.
   1051 
   1052 \\<magit-mode-map>\
   1053 Type \\[magit-refresh] to refresh the current buffer.
   1054 Type \\[magit-visit-thing] or \\[magit-diff-show-or-scroll-up] \
   1055 to visit the commit at point.
   1056 
   1057 Type \\[magit-branch] to see available branch commands.
   1058 Type \\[magit-merge] to merge the branch or commit at point.
   1059 Type \\[magit-cherry-pick] to apply the commit at point.
   1060 Type \\[magit-reset] to reset `HEAD' to the commit at point.
   1061 
   1062 \\{magit-log-mode-map}"
   1063   :group 'magit-log
   1064   (hack-dir-local-variables-non-file-buffer)
   1065   (setq magit--imenu-item-types 'commit))
   1066 
   1067 (put 'magit-log-mode 'magit-log-default-arguments
   1068      '("--graph" "-n256" "--decorate"))
   1069 
   1070 (defun magit-log-setup-buffer (revs args files &optional locked focus)
   1071   (require 'magit)
   1072   (with-current-buffer
   1073       (magit-setup-buffer #'magit-log-mode locked
   1074         (magit-buffer-revisions revs)
   1075         (magit-buffer-log-args args)
   1076         (magit-buffer-log-files files))
   1077     (when (if focus
   1078               (magit-log-goto-commit-section focus)
   1079             (magit-log-goto-same-commit))
   1080       (magit-section-update-highlight))
   1081     (current-buffer)))
   1082 
   1083 (defun magit-log-refresh-buffer ()
   1084   (let ((revs  magit-buffer-revisions)
   1085         (args  magit-buffer-log-args)
   1086         (files magit-buffer-log-files)
   1087         (limit (magit-log-get-commit-limit)))
   1088     (magit-set-header-line-format
   1089      (funcall magit-log-header-line-function revs args files))
   1090     (unless (length= files 1)
   1091       (setq args (remove "--follow" args)))
   1092     (when (and (car magit-log-remove-graph-args)
   1093                (--any-p (string-match-p
   1094                          (concat "^" (regexp-opt magit-log-remove-graph-args)) it)
   1095                         args))
   1096       (setq args (remove "--graph" args)))
   1097     (setq args (magit-log--maybe-drop-color-graph args limit))
   1098     (when-let* ((limit limit)
   1099                 (limit (* 2 limit)) ; increase odds for complete graph
   1100                 (count (and (length= revs 1)
   1101                             (> limit 1024) ; otherwise it's fast enough
   1102                             (setq revs (car revs))
   1103                             (not (string-search ".." revs))
   1104                             (not (member revs '("--all" "--branches")))
   1105                             (not (seq-some
   1106                                   (lambda (arg)
   1107                                     (--any-p (string-prefix-p it arg)
   1108                                              magit-log-disable-graph-hack-args))
   1109                                   args))
   1110                             (magit-git-string "rev-list" "--count"
   1111                                               "--first-parent" args revs))))
   1112       (setq revs (if (< (string-to-number count) limit)
   1113                      revs
   1114                    (format "%s~%s..%s" revs limit revs))))
   1115     (let ((delay (cl-find-if (lambda (arg)
   1116                                (member arg '("++header" "--patch" "--stat")))
   1117                              args)))
   1118       (setq magit-section-inhibit-markers (if delay 'delay t))
   1119       (setq magit-section-insert-in-reverse (not delay)))
   1120     (magit-insert-section (logbuf)
   1121       (magit--insert-log t revs args files))))
   1122 
   1123 (defvar-local magit-log--color-graph nil)
   1124 
   1125 (defun magit-log--maybe-drop-color-graph (args limit)
   1126   (if (member "--color" args)
   1127       (if (cond ((not (member "--graph" args)))
   1128                 ((not magit-log-color-graph-limit) nil)
   1129                 ((not limit)
   1130                  (message "Dropping --color because -n isn't set (see %s)"
   1131                           'magit-log-color-graph-limit))
   1132                 ((> limit magit-log-color-graph-limit)
   1133                  (message "Dropping --color because -n is larger than %s"
   1134                           'magit-log-color-graph-limit)))
   1135           (progn (setq args (remove "--color" args))
   1136                  (setq magit-log--color-graph nil))
   1137         (setq magit-log--color-graph t))
   1138     (setq magit-log--color-graph nil))
   1139   args)
   1140 
   1141 (cl-defmethod magit-buffer-value (&context (major-mode magit-log-mode))
   1142   (append magit-buffer-revisions
   1143           (if (and magit-buffer-revisions magit-buffer-log-files)
   1144               (cons "--" magit-buffer-log-files)
   1145             magit-buffer-log-files)))
   1146 
   1147 (defun magit-log-header-line-arguments (revs args files)
   1148   "Return string describing some of the used arguments."
   1149   (mapconcat (lambda (arg)
   1150                (if (string-search " " arg)
   1151                    (prin1 arg)
   1152                  arg))
   1153              `("git" "log" ,@args ,@revs "--" ,@files)
   1154              " "))
   1155 
   1156 (defun magit-log-header-line-sentence (revs args files)
   1157   "Return string containing all arguments."
   1158   (concat "Commits in "
   1159           (mapconcat #'identity revs " ")
   1160           (and (member "--reverse" args)
   1161                " in reverse")
   1162           (and files (concat " touching "
   1163                              (mapconcat #'identity files " ")))
   1164           (--some (and (string-prefix-p "-L" it)
   1165                        (concat " " it))
   1166                   args)))
   1167 
   1168 (defun magit-insert-log (revs &optional args files)
   1169   (declare (obsolete magit--insert-log "Magit 4.0.0"))
   1170   (magit--insert-log nil revs args files))
   1171 
   1172 (defun magit--insert-log (keep-error revs &optional args files)
   1173   "Insert a log section.
   1174 Do not add this to a hook variable."
   1175   (declare (indent defun))
   1176   (setq magit-section-preserve-visibility t) ; TODO do it here?
   1177   (let ((magit-git-global-arguments
   1178          (remove "--literal-pathspecs" magit-git-global-arguments)))
   1179     (magit--git-wash (apply-partially #'magit-log-wash-log 'log) keep-error
   1180       "log"
   1181       (format "--format=%s%%h%%x0c%s%%x0c%s%%x0c%%aN%%x0c%s%%x0c%%s%s"
   1182               (if (and (member "--left-right" args)
   1183                        (not (member "--graph" args)))
   1184                   "%m "
   1185                 "")
   1186               (if (member "--decorate" args) "%D" "")
   1187               (if (not (member "--show-signature" args))
   1188                   ""
   1189                 (setq args (remove "--show-signature" args))
   1190                 (let ((limit (magit-log-get-commit-limit args)))
   1191                   (cond
   1192                    ((not limit)
   1193                     (message
   1194                      "Dropping --show-signature because -n isn't set (see %s)"
   1195                      'magit-log-show-signatures-limit)
   1196                     "")
   1197                    ((> limit magit-log-show-signatures-limit)
   1198                     (message
   1199                      "Dropping --show-signature because -n is larger than %s"
   1200                      'magit-log-show-signatures-limit)
   1201                     "")
   1202                    ("%G?"))))
   1203               (if magit-log-margin-show-committer-date "%ct" "%at")
   1204               (if (member "++header" args)
   1205                   (if (member "--graph" (setq args (remove "++header" args)))
   1206                       (concat "\n" magit-log-revision-headers-format "\n")
   1207                     (concat "\n" magit-log-revision-headers-format "\n"))
   1208                 ""))
   1209       (progn
   1210         (when-let ((order (--first (string-match "^\\+\\+order=\\(.+\\)$" it)
   1211                                    args)))
   1212           (setq args (cons (format "--%s-order" (match-string 1 order))
   1213                            (remove order args))))
   1214         (when (member "--decorate" args)
   1215           (setq args (cons "--decorate=full" (remove "--decorate" args))))
   1216         (when (member "--reverse" args)
   1217           (setq args (remove "--graph" args)))
   1218         (setq args (magit-diff--maybe-add-stat-arguments args))
   1219         args)
   1220       "--use-mailmap" "--no-prefix" revs "--" files)))
   1221 
   1222 (cl-defmethod magit-menu-common-value ((_section magit-commit-section))
   1223   (or (magit-diff--region-range)
   1224       (oref (magit-current-section) value)))
   1225 
   1226 (defvar-keymap magit-commit-section-map
   1227   :doc "Keymap for `commit' sections."
   1228   "<remap> <magit-visit-thing>" #'magit-show-commit
   1229   "<3>" (magit-menu-item "Apply %x" #'magit-cherry-apply)
   1230   "<2>" (magit-menu-item "Show commit %x" #'magit-show-commit
   1231                          '(:visible (not (region-active-p))))
   1232   "<1>" (magit-menu-item "Diff %x" #'magit-diff-range
   1233                          '(:visible (region-active-p))))
   1234 
   1235 (defvar-keymap magit-module-commit-section-map
   1236   :doc "Keymap for `module-commit' sections."
   1237   :parent magit-commit-section-map)
   1238 
   1239 (defconst magit-log-heading-re
   1240   ;; Note: A form feed instead of a null byte is used as the delimiter
   1241   ;; because using the latter interferes with the graph prefix when
   1242   ;; ++header is used.
   1243   (concat "^"
   1244           "\\(?4:[-_/|\\*o<>. ]*\\)"               ; graph
   1245           "\\(?1:[0-9a-fA-F]+\\)?"               ; hash
   1246           "\\(?3:[^\n]+\\)?"                   ; refs
   1247           "\\(?7:[BGUXYREN]\\)?"                 ; gpg
   1248           "\\(?5:[^\n]*\\)"                    ; author
   1249           ;; Note: Date is optional because, prior to Git v2.19.0,
   1250           ;; `git rebase -i --root` corrupts the root's author date.
   1251           "\\(?6:[^\n]*\\)"                    ; date
   1252           "\\(?2:.*\\)$"))                         ; msg
   1253 
   1254 (defconst magit-log-cherry-re
   1255   (concat "^"
   1256           "\\(?8:[-+]\\) "                         ; cherry
   1257           "\\(?1:[0-9a-fA-F]+\\) "                 ; hash
   1258           "\\(?2:.*\\)$"))                         ; msg
   1259 
   1260 (defconst magit-log-module-re
   1261   (concat "^"
   1262           "\\(?:\\(?11:[<>]\\) \\)?"               ; side
   1263           "\\(?1:[0-9a-fA-F]+\\) "                 ; hash
   1264           "\\(?2:.*\\)$"))                         ; msg
   1265 
   1266 (defconst magit-log-bisect-vis-re
   1267   (concat "^"
   1268           "\\(?4:[-_/|\\*o<>. ]*\\)"               ; graph
   1269           "\\(?1:[0-9a-fA-F]+\\)?\0"               ; hash
   1270           "\\(?3:[^\0\n]+\\)?\0"                   ; refs
   1271           "\\(?2:.*\\)$"))                         ; msg
   1272 
   1273 (defconst magit-log-bisect-log-re
   1274   (concat "^# "
   1275           "\\(?3:[^: \n]+:\\) "                    ; "refs"
   1276           "\\[\\(?1:[^]\n]+\\)\\] "                ; hash
   1277           "\\(?2:.*\\)$"))                         ; msg
   1278 
   1279 (defconst magit-log-reflog-re
   1280   (concat "^"
   1281           "\\(?1:[^\0\n]+\\)\0"                    ; hash
   1282           "\\(?5:[^\0\n]*\\)\0"                    ; author
   1283           "\\(?:\\(?:[^@\n]+@{\\(?6:[^}\n]+\\)}\0" ; date
   1284                                                  ;;; refsub
   1285           "\\(?10:merge \\|autosave \\|restart \\|rewritten \\|[^:\n]+: \\)?"
   1286           "\\(?2:.*\\)\\)\\|\0\\)$"))              ; msg
   1287 
   1288 (defconst magit-reflog-subject-re
   1289   (concat "\\(?1:[^ ]+\\) ?"                       ; command
   1290           "\\(?2:\\(?: ?-[^ ]+\\)+\\)?"            ; option
   1291           "\\(?: ?(\\(?3:[^)]+\\))\\)?"))          ; type
   1292 
   1293 (defconst magit-log-stash-re
   1294   (concat "^"
   1295           "\\(?1:[^\0\n]+\\)\0"                    ; "hash"
   1296           "\\(?5:[^\0\n]*\\)\0"                    ; author
   1297           "\\(?6:[^\0\n]+\\)\0"                    ; date
   1298           "\\(?2:.*\\)$"))                         ; msg
   1299 
   1300 (defvar magit-log-count nil)
   1301 
   1302 (defvar magit-log-format-message-function #'magit-log-propertize-keywords)
   1303 
   1304 (defun magit-log-wash-log (style args)
   1305   (setq args (flatten-tree args))
   1306   (when (if (derived-mode-p 'magit-log-mode)
   1307             magit-log--color-graph
   1308           (and (member "--graph" args)
   1309                (member "--color" args)))
   1310     (let ((ansi-color-apply-face-function
   1311            (lambda (beg end face)
   1312              (put-text-property beg end 'font-lock-face
   1313                                 (or face 'magit-log-graph)))))
   1314       (ansi-color-apply-on-region (point-min) (point-max))))
   1315   (when (eq style 'cherry)
   1316     (reverse-region (point-min) (point-max)))
   1317   (let ((magit-log-count 0))
   1318     (when (looking-at "^\\.\\.\\.")
   1319       (magit-delete-line))
   1320     (magit-wash-sequence (apply-partially #'magit-log-wash-rev style
   1321                                           (magit-abbrev-length)))
   1322     (if (derived-mode-p 'magit-log-mode 'magit-reflog-mode)
   1323         (when (eq magit-log-count (magit-log-get-commit-limit))
   1324           (magit-insert-section (longer)
   1325             (insert-text-button
   1326              (substitute-command-keys
   1327               (format "Type \\<%s>\\[%s] to show more history"
   1328                       'magit-log-mode-map
   1329                       'magit-log-double-commit-limit))
   1330              'action (lambda (_button)
   1331                        (magit-log-double-commit-limit))
   1332              'follow-link t
   1333              'mouse-face 'magit-section-highlight)))
   1334       (insert ?\n))))
   1335 
   1336 (cl-defun magit-log-wash-rev (style abbrev)
   1337   (when (derived-mode-p 'magit-log-mode 'magit-reflog-mode)
   1338     (cl-incf magit-log-count))
   1339   (looking-at (pcase style
   1340                 ('log        magit-log-heading-re)
   1341                 ('cherry     magit-log-cherry-re)
   1342                 ('module     magit-log-module-re)
   1343                 ('reflog     magit-log-reflog-re)
   1344                 ('stash      magit-log-stash-re)
   1345                 ('bisect-vis magit-log-bisect-vis-re)
   1346                 ('bisect-log magit-log-bisect-log-re)))
   1347   (magit-bind-match-strings
   1348       (hash msg refs graph author date gpg cherry _ refsub side) nil
   1349     (setq msg (substring-no-properties msg))
   1350     (when refs
   1351       (setq refs (substring-no-properties refs)))
   1352     (let ((align (or (eq style 'cherry)
   1353                      (not (member "--stat" magit-buffer-log-args))))
   1354           (non-graph-re (if (eq style 'bisect-vis)
   1355                             magit-log-bisect-vis-re
   1356                           magit-log-heading-re)))
   1357       (magit-delete-line)
   1358       ;; If the reflog entries have been pruned, the output of `git
   1359       ;; reflog show' includes a partial line that refers to the hash
   1360       ;; of the youngest expired reflog entry.
   1361       (when (and (eq style 'reflog) (not date))
   1362         (cl-return-from magit-log-wash-rev t))
   1363       (magit-insert-section
   1364           ((eval (pcase style
   1365                    ('stash  'stash)
   1366                    ('module 'module-commit)
   1367                    (_       'commit)))
   1368            hash)
   1369         (setq hash (propertize (if (eq style 'bisect-log)
   1370                                    (magit-rev-parse "--short" hash)
   1371                                  hash)
   1372                                'font-lock-face
   1373                                (pcase (and gpg (aref gpg 0))
   1374                                  (?G 'magit-signature-good)
   1375                                  (?B 'magit-signature-bad)
   1376                                  (?U 'magit-signature-untrusted)
   1377                                  (?X 'magit-signature-expired)
   1378                                  (?Y 'magit-signature-expired-key)
   1379                                  (?R 'magit-signature-revoked)
   1380                                  (?E 'magit-signature-error)
   1381                                  (?N 'magit-hash)
   1382                                  (_  'magit-hash))))
   1383         (when cherry
   1384           (when (and (derived-mode-p 'magit-refs-mode)
   1385                      magit-refs-show-commit-count)
   1386             (insert (make-string (1- magit-refs-focus-column-width) ?\s)))
   1387           (insert (propertize cherry 'font-lock-face
   1388                               (if (string= cherry "-")
   1389                                   'magit-cherry-equivalent
   1390                                 'magit-cherry-unmatched)))
   1391           (insert ?\s))
   1392         (when side
   1393           (insert (propertize side 'font-lock-face
   1394                               (if (string= side "<")
   1395                                   'magit-cherry-equivalent
   1396                                 'magit-cherry-unmatched)))
   1397           (insert ?\s))
   1398         (when align
   1399           (insert hash ?\s))
   1400         (when graph
   1401           (insert graph))
   1402         (unless align
   1403           (insert hash ?\s))
   1404         (when (and refs (not magit-log-show-refname-after-summary))
   1405           (insert (magit-format-ref-labels refs) ?\s))
   1406         (when (eq style 'reflog)
   1407           (insert (format "%-2s " (1- magit-log-count)))
   1408           (when refsub
   1409             (insert (magit-reflog-format-subject
   1410                      (substring refsub 0
   1411                                 (if (string-search ":" refsub) -2 -1))))))
   1412         (insert (funcall magit-log-format-message-function hash msg))
   1413         (when (and refs magit-log-show-refname-after-summary)
   1414           (insert ?\s)
   1415           (insert (magit-format-ref-labels refs)))
   1416         (insert ?\n)
   1417         (when (memq style '(log reflog stash))
   1418           (goto-char (line-beginning-position))
   1419           (when (and refsub
   1420                      (string-match "\\`\\([^ ]\\) \\+\\(..\\)\\(..\\)" date))
   1421             (setq date (+ (string-to-number (match-string 1 date))
   1422                           (* (string-to-number (match-string 2 date)) 60 60)
   1423                           (* (string-to-number (match-string 3 date)) 60))))
   1424           (save-excursion
   1425             (backward-char)
   1426             (magit-log-format-margin hash author date)))
   1427         (when (and (eq style 'cherry)
   1428                    (magit-buffer-margin-p))
   1429           (save-excursion
   1430             (backward-char)
   1431             (apply #'magit-log-format-margin hash
   1432                    (split-string (magit-rev-format "%aN%x00%ct" hash) "\0"))))
   1433         (when (and graph
   1434                    (not (eobp))
   1435                    (not (looking-at non-graph-re)))
   1436           (when (looking-at "")
   1437             (magit-insert-heading)
   1438             (delete-char 1)
   1439             (magit-insert-section (commit-header)
   1440               (forward-line)
   1441               (magit-insert-heading)
   1442               (re-search-forward "")
   1443               (delete-char -1)
   1444               (forward-char)
   1445               (insert ?\n))
   1446             (delete-char 1))
   1447           (if (looking-at "^\\(---\\|\n\s\\|\ndiff\\)")
   1448               (let ((limit (save-excursion
   1449                              (and (re-search-forward non-graph-re nil t)
   1450                                   (match-beginning 0)))))
   1451                 (unless (oref magit-insert-section--current content)
   1452                   (magit-insert-heading))
   1453                 (delete-char (if (looking-at "\n") 1 4))
   1454                 (magit-diff-wash-diffs (list "--stat") limit))
   1455             (when align
   1456               (setq align (make-string (1+ abbrev) ? )))
   1457             (when (and (not (eobp)) (not (looking-at non-graph-re)))
   1458               (when align
   1459                 (setq align (make-string (1+ abbrev) ? )))
   1460               (while (and (not (eobp)) (not (looking-at non-graph-re)))
   1461                 (when align
   1462                   (save-excursion (insert align)))
   1463                 (magit-make-margin-overlay)
   1464                 (forward-line))
   1465               ;; When `--format' is used and its value isn't one of the
   1466               ;; predefined formats, then `git-log' does not insert a
   1467               ;; separator line.
   1468               (save-excursion
   1469                 (forward-line -1)
   1470                 (looking-at "[-_/|\\*o<>. ]*"))
   1471               (setq graph (match-string 0))
   1472               (unless (string-match-p "[/\\.]" graph)
   1473                 (insert graph ?\n))))))))
   1474   t)
   1475 
   1476 (defun magit-log-propertize-keywords (_rev msg)
   1477   (let ((boundary 0))
   1478     (when (string-match "^\\(?:squash\\|fixup\\)! " msg boundary)
   1479       (setq boundary (match-end 0))
   1480       (magit--put-face (match-beginning 0) (1- boundary)
   1481                        'magit-keyword-squash msg))
   1482     (when magit-log-highlight-keywords
   1483       (while (string-match "\\[[^][]*]" msg boundary)
   1484         (setq boundary (match-end 0))
   1485         (magit--put-face (match-beginning 0) boundary
   1486                          'magit-keyword msg))))
   1487   msg)
   1488 
   1489 (defun magit-log-maybe-show-more-commits (section)
   1490   "When point is at the end of a log buffer, insert more commits.
   1491 
   1492 Log buffers end with a button \"Type + to show more history\".
   1493 When the use of a section movement command puts point on that
   1494 button, then automatically show more commits, without the user
   1495 having to press \"+\".
   1496 
   1497 This function is called by `magit-section-movement-hook' and
   1498 exists mostly for backward compatibility reasons."
   1499   (when (and (eq (oref section type) 'longer)
   1500              magit-log-auto-more)
   1501     (magit-log-double-commit-limit)
   1502     (forward-line -1)
   1503     (magit-section-forward)))
   1504 
   1505 (add-hook 'magit-section-movement-hook #'magit-log-maybe-show-more-commits)
   1506 
   1507 (defvar magit--update-revision-buffer nil)
   1508 
   1509 (defun magit-log-maybe-update-revision-buffer (&optional _)
   1510   "When moving in a log or cherry buffer, update the revision buffer.
   1511 If there is no revision buffer in the same frame, then do nothing."
   1512   (when (derived-mode-p 'magit-log-mode 'magit-cherry-mode 'magit-reflog-mode)
   1513     (magit--maybe-update-revision-buffer)))
   1514 
   1515 (add-hook 'magit-section-movement-hook #'magit-log-maybe-update-revision-buffer)
   1516 
   1517 (defun magit--maybe-update-revision-buffer ()
   1518   (when-let* ((commit (magit-section-value-if 'commit))
   1519               (buffer (magit-get-mode-buffer 'magit-revision-mode nil t)))
   1520     (if magit--update-revision-buffer
   1521         (setq magit--update-revision-buffer (list commit buffer))
   1522       (setq magit--update-revision-buffer (list commit buffer))
   1523       (run-with-idle-timer
   1524        magit-update-other-window-delay nil
   1525        (let ((args (let ((magit-direct-use-buffer-arguments 'selected))
   1526                      (magit-show-commit--arguments))))
   1527          (lambda ()
   1528            (pcase-let ((`(,rev ,buf) magit--update-revision-buffer))
   1529              (setq magit--update-revision-buffer nil)
   1530              (when (buffer-live-p buf)
   1531                (let ((magit-display-buffer-noselect t))
   1532                  (apply #'magit-show-commit rev args))))
   1533            (setq magit--update-revision-buffer nil)))))))
   1534 
   1535 (defvar magit--update-blob-buffer nil)
   1536 
   1537 (defun magit-log-maybe-update-blob-buffer (&optional _)
   1538   "When moving in a log or cherry buffer, update the blob buffer.
   1539 If there is no blob buffer in the same frame, then do nothing."
   1540   (when (derived-mode-p 'magit-log-mode 'magit-cherry-mode 'magit-reflog-mode)
   1541     (magit--maybe-update-blob-buffer)))
   1542 
   1543 (defun magit--maybe-update-blob-buffer ()
   1544   (when-let* ((commit (magit-section-value-if 'commit))
   1545               (buffer (--first (with-current-buffer it
   1546                                  (eq revert-buffer-function
   1547                                      'magit-revert-rev-file-buffer))
   1548                                (mapcar #'window-buffer (window-list)))))
   1549     (if magit--update-blob-buffer
   1550         (setq magit--update-blob-buffer (list commit buffer))
   1551       (setq magit--update-blob-buffer (list commit buffer))
   1552       (run-with-idle-timer
   1553        magit-update-other-window-delay nil
   1554        (lambda ()
   1555          (pcase-let ((`(,rev ,buf) magit--update-blob-buffer))
   1556            (setq magit--update-blob-buffer nil)
   1557            (when (buffer-live-p buf)
   1558              (with-selected-window (get-buffer-window buf)
   1559                (with-current-buffer buf
   1560                  (save-excursion
   1561                    (magit-blob-visit (list (magit-rev-parse rev)
   1562                                            (magit-file-relative-name
   1563                                             magit-buffer-file-name)))))))))))))
   1564 
   1565 (defun magit-log-goto-commit-section (rev)
   1566   (let ((abbrev (magit-rev-format "%h" rev)))
   1567     (when-let ((section (--first (equal (oref it value) abbrev)
   1568                                  (oref magit-root-section children))))
   1569       (goto-char (oref section start)))))
   1570 
   1571 (defun magit-log-goto-same-commit ()
   1572   (when (and magit-previous-section
   1573              (magit-section-match '(commit branch)
   1574                                   magit-previous-section))
   1575     (magit-log-goto-commit-section (oref magit-previous-section value))))
   1576 
   1577 ;;; Log Margin
   1578 
   1579 (defvar-local magit-log-margin-show-shortstat nil)
   1580 
   1581 (transient-define-suffix magit-toggle-log-margin-style ()
   1582   "Toggle between the regular and the shortstat margin style.
   1583 The shortstat style is experimental and rather slow."
   1584   :description "Toggle shortstat"
   1585   :key "x"
   1586   :transient t
   1587   (interactive)
   1588   (setq magit-log-margin-show-shortstat
   1589         (not magit-log-margin-show-shortstat))
   1590   (magit-set-buffer-margin nil t))
   1591 
   1592 (defun magit-log-format-margin (rev author date)
   1593   (when (magit-margin-option)
   1594     (if magit-log-margin-show-shortstat
   1595         (magit-log-format-shortstat-margin rev)
   1596       (magit-log-format-author-margin author date))))
   1597 
   1598 (defun magit-log-format-author-margin (author date &optional previous-line)
   1599   (pcase-let ((`(,_ ,style ,width ,details ,details-width)
   1600                (or magit-buffer-margin
   1601                    (symbol-value (magit-margin-option))
   1602                    (error "No margin format specified for %s" major-mode))))
   1603     (magit-make-margin-overlay
   1604      (concat (and details
   1605                   (concat (magit--propertize-face
   1606                            (truncate-string-to-width
   1607                             (or author "")
   1608                             details-width
   1609                             nil ?\s
   1610                             (magit--ellipsis 'margin))
   1611                            'magit-log-author)
   1612                           " "))
   1613              (magit--propertize-face
   1614               (if (stringp style)
   1615                   (format-time-string
   1616                    style
   1617                    (seconds-to-time (string-to-number date)))
   1618                 (pcase-let* ((abbr (eq style 'age-abbreviated))
   1619                              (`(,cnt ,unit) (magit--age date abbr)))
   1620                   (format (format (if abbr "%%2d%%-%dc" "%%2d %%-%ds")
   1621                                   (- width (if details (1+ details-width) 0)))
   1622                           cnt unit)))
   1623               'magit-log-date))
   1624      previous-line)))
   1625 
   1626 (defun magit-log-format-shortstat-margin (rev)
   1627   (magit-make-margin-overlay
   1628    (if-let ((line (and rev (magit-git-string
   1629                             "show" "--format=" "--shortstat" rev))))
   1630        (if (string-match "\
   1631 \\([0-9]+\\) files? changed, \
   1632 \\(?:\\([0-9]+\\) insertions?(\\+)\\)?\
   1633 \\(?:\\(?:, \\)?\\([0-9]+\\) deletions?(-)\\)?\\'" line)
   1634            (magit-bind-match-strings (files add del) line
   1635              (format
   1636               "%5s %5s%4s"
   1637               (if add
   1638                   (magit--propertize-face (format "%s+" add)
   1639                                           'magit-diffstat-added)
   1640                 "")
   1641               (if del
   1642                   (magit--propertize-face (format "%s-" del)
   1643                                           'magit-diffstat-removed)
   1644                 "")
   1645               files))
   1646          "")
   1647      "")))
   1648 
   1649 (defun magit-log-margin-width (style details details-width)
   1650   (if magit-log-margin-show-shortstat
   1651       16
   1652     (+ (if details (1+ details-width) 0)
   1653        (if (stringp style)
   1654            (length (format-time-string style))
   1655          (+ 2 ; two digits
   1656             1 ; trailing space
   1657             (if (eq style 'age-abbreviated)
   1658                 1  ; single character
   1659               (+ 1 ; gap after digits
   1660                  (apply #'max (--map (max (length (nth 1 it))
   1661                                           (length (nth 2 it)))
   1662                                      magit--age-spec)))))))))
   1663 
   1664 ;;; Select Mode
   1665 
   1666 (defvar-keymap magit-log-select-mode-map
   1667   :doc "Keymap for `magit-log-select-mode'."
   1668   :parent magit-log-mode-map
   1669   "C-c C-b" #'undefined
   1670   "C-c C-f" #'undefined
   1671   "."       #'magit-log-select-pick
   1672   "e"       #'magit-log-select-pick
   1673   "C-c C-c" #'magit-log-select-pick
   1674   "q"       #'magit-log-select-quit
   1675   "C-c C-k" #'magit-log-select-quit)
   1676 (put 'magit-log-select-pick :advertised-binding [?\C-c ?\C-c])
   1677 (put 'magit-log-select-quit :advertised-binding [?\C-c ?\C-k])
   1678 
   1679 (define-derived-mode magit-log-select-mode magit-log-mode "Magit Select"
   1680   "Mode for selecting a commit from history.
   1681 
   1682 This mode is documented in info node `(magit)Select from Log'.
   1683 
   1684 \\<magit-mode-map>\
   1685 Type \\[magit-refresh] to refresh the current buffer.
   1686 Type \\[magit-visit-thing] or \\[magit-diff-show-or-scroll-up] \
   1687 to visit the commit at point.
   1688 
   1689 \\<magit-log-select-mode-map>\
   1690 Type \\[magit-log-select-pick] to select the commit at point.
   1691 Type \\[magit-log-select-quit] to abort without selecting a commit."
   1692   :group 'magit-log
   1693   (hack-dir-local-variables-non-file-buffer))
   1694 
   1695 (put 'magit-log-select-mode 'magit-log-default-arguments
   1696      '("--graph" "-n256" "--decorate"))
   1697 
   1698 (defun magit-log-select-setup-buffer (revs args)
   1699   (magit-setup-buffer #'magit-log-select-mode nil
   1700     (magit-buffer-revisions revs)
   1701     (magit-buffer-log-args args)))
   1702 
   1703 (defun magit-log-select-refresh-buffer ()
   1704   (setq magit-section-inhibit-markers t)
   1705   (setq magit-section-insert-in-reverse t)
   1706   (magit-insert-section (logbuf)
   1707     (magit--insert-log t magit-buffer-revisions
   1708       (magit-log--maybe-drop-color-graph
   1709        magit-buffer-log-args
   1710        (magit-log-get-commit-limit)))))
   1711 
   1712 (cl-defmethod magit-buffer-value (&context (major-mode magit-log-select-mode))
   1713   magit-buffer-revisions)
   1714 
   1715 (defvar-local magit-log-select-pick-function nil)
   1716 (defvar-local magit-log-select-quit-function nil)
   1717 
   1718 (defun magit-log-select (pick &optional msg quit branch args initial)
   1719   (declare (indent defun))
   1720   (unless initial
   1721     (setq initial (magit-commit-at-point)))
   1722   (magit-log-select-setup-buffer
   1723    (or branch (magit-get-current-branch) "HEAD")
   1724    (append args
   1725            (car (magit-log--get-value 'magit-log-select-mode
   1726                                       magit-direct-use-buffer-arguments))))
   1727   (when initial
   1728     (magit-log-goto-commit-section initial))
   1729   (setq magit-log-select-pick-function pick)
   1730   (setq magit-log-select-quit-function quit)
   1731   (when magit-log-select-show-usage
   1732     (let ((pick (propertize (substitute-command-keys
   1733                              "\\[magit-log-select-pick]")
   1734                             'font-lock-face
   1735                             'magit-header-line-key))
   1736           (quit (propertize (substitute-command-keys
   1737                              "\\[magit-log-select-quit]")
   1738                             'font-lock-face
   1739                             'magit-header-line-key)))
   1740       (setq msg (format-spec
   1741                  (if msg
   1742                      (if (string-suffix-p "," msg)
   1743                          (concat msg " or %q to abort")
   1744                        msg)
   1745                    "Type %p to select commit at point, or %q to abort")
   1746                  `((?p . ,pick)
   1747                    (?q . ,quit)))))
   1748     (magit--add-face-text-property
   1749      0 (length msg) 'magit-header-line-log-select t msg)
   1750     (when (memq magit-log-select-show-usage '(both header-line))
   1751       (magit-set-header-line-format msg))
   1752     (when (memq magit-log-select-show-usage '(both echo-area))
   1753       (message "%s" (substring-no-properties msg)))))
   1754 
   1755 (defun magit-log-select-pick ()
   1756   "Select the commit at point and act on it.
   1757 Call `magit-log-select-pick-function' with the selected
   1758 commit as argument."
   1759   (interactive)
   1760   (let ((fun magit-log-select-pick-function)
   1761         (rev (magit-commit-at-point)))
   1762     (magit-mode-bury-buffer 'kill)
   1763     (funcall fun rev)))
   1764 
   1765 (defun magit-log-select-quit ()
   1766   "Abort selecting a commit, don't act on any commit.
   1767 Call `magit-log-select-quit-function' if set."
   1768   (interactive)
   1769   (let ((fun magit-log-select-quit-function))
   1770     (magit-mode-bury-buffer 'kill)
   1771     (when fun (funcall fun))))
   1772 
   1773 ;;; Cherry Mode
   1774 
   1775 (defvar-keymap magit-cherry-mode-map
   1776   :doc "Keymap for `magit-cherry-mode'."
   1777   :parent magit-mode-map
   1778   "q" #'magit-log-bury-buffer
   1779   "L" #'magit-margin-settings)
   1780 
   1781 (define-derived-mode magit-cherry-mode magit-mode "Magit Cherry"
   1782   "Mode for looking at commits not merged upstream.
   1783 
   1784 \\<magit-mode-map>\
   1785 Type \\[magit-refresh] to refresh the current buffer.
   1786 Type \\[magit-visit-thing] or \\[magit-diff-show-or-scroll-up] \
   1787 to visit the commit at point.
   1788 
   1789 Type \\[magit-cherry-pick] to apply the commit at point.
   1790 
   1791 \\{magit-cherry-mode-map}"
   1792   :group 'magit-log
   1793   (hack-dir-local-variables-non-file-buffer)
   1794   (setq magit--imenu-group-types 'cherries))
   1795 
   1796 (defun magit-cherry-setup-buffer (head upstream)
   1797   (magit-setup-buffer #'magit-cherry-mode nil
   1798     (magit-buffer-refname head)
   1799     (magit-buffer-upstream upstream)
   1800     (magit-buffer-range (concat upstream ".." head))))
   1801 
   1802 (defun magit-cherry-refresh-buffer ()
   1803   (setq magit-section-insert-in-reverse t)
   1804   (magit-insert-section (cherry)
   1805     (magit-run-section-hook 'magit-cherry-sections-hook)))
   1806 
   1807 (cl-defmethod magit-buffer-value (&context (major-mode magit-cherry-mode))
   1808   magit-buffer-range)
   1809 
   1810 ;;;###autoload
   1811 (defun magit-cherry (head upstream)
   1812   "Show commits in a branch that are not merged in the upstream branch."
   1813   (interactive
   1814    (let  ((head (magit-read-branch "Cherry head")))
   1815      (list head (magit-read-other-branch "Cherry upstream" head
   1816                                          (magit-get-upstream-branch head)))))
   1817   (require 'magit)
   1818   (magit-cherry-setup-buffer head upstream))
   1819 
   1820 (defun magit-insert-cherry-headers ()
   1821   "Insert headers appropriate for `magit-cherry-mode' buffers."
   1822   (let ((branch (propertize magit-buffer-refname
   1823                             'font-lock-face 'magit-branch-local))
   1824         (upstream (propertize magit-buffer-upstream 'font-lock-face
   1825                               (if (magit-local-branch-p magit-buffer-upstream)
   1826                                   'magit-branch-local
   1827                                 'magit-branch-remote))))
   1828     (magit-insert-head-branch-header branch)
   1829     (magit-insert-upstream-branch-header branch upstream "Upstream: ")
   1830     (insert ?\n)))
   1831 
   1832 (defun magit-insert-cherry-commits ()
   1833   "Insert commit sections into a `magit-cherry-mode' buffer."
   1834   (magit-insert-section (cherries)
   1835     (magit-insert-heading "Cherry commits:")
   1836     (magit-git-wash (apply-partially #'magit-log-wash-log 'cherry)
   1837       "cherry" "-v" "--abbrev"
   1838       magit-buffer-upstream
   1839       magit-buffer-refname)))
   1840 
   1841 ;;; Log Sections
   1842 ;;;; Standard Log Sections
   1843 
   1844 (defvar-keymap magit-log-section-map
   1845   :doc "Keymap for log sections.
   1846 The classes `magit-{unpulled,unpushed,unmerged}-section' derive
   1847 from the abstract `magit-log-section' class.  Accordingly this
   1848 keymap is the parent of their keymaps."
   1849   "<remap> <magit-visit-thing>" #'magit-diff-dwim
   1850   "<1>" (magit-menu-item "Visit diff" #'magit-diff-dwim))
   1851 
   1852 (cl-defmethod magit-section-ident-value ((section magit-unpulled-section))
   1853   "\"..@{push}\" cannot be used as the value because that is
   1854 ambiguous if `push.default' does not allow a 1:1 mapping, and
   1855 many commands would fail because of that.  But here that does
   1856 not matter and we need an unique value so we use that string
   1857 in the pushremote case."
   1858   (let ((value (oref section value)))
   1859     (if (equal value "..@{upstream}") value "..@{push}")))
   1860 
   1861 (magit-define-section-jumper magit-jump-to-unpulled-from-upstream
   1862   "Unpulled from @{upstream}" unpulled "..@{upstream}")
   1863 
   1864 (defun magit-insert-unpulled-from-upstream ()
   1865   "Insert commits that haven't been pulled from the upstream yet."
   1866   (when-let ((upstream (magit-get-upstream-branch)))
   1867     (magit-insert-section (unpulled "..@{upstream}" t)
   1868       (magit-insert-heading
   1869         (format (propertize "Unpulled from %s."
   1870                             'font-lock-face 'magit-section-heading)
   1871                 upstream))
   1872       (magit--insert-log nil "..@{upstream}" magit-buffer-log-args)
   1873       (magit-log-insert-child-count))))
   1874 
   1875 (magit-define-section-jumper magit-jump-to-unpulled-from-pushremote
   1876   "Unpulled from <push-remote>" unpulled "..@{push}")
   1877 
   1878 (defun magit-insert-unpulled-from-pushremote ()
   1879   "Insert commits that haven't been pulled from the push-remote yet."
   1880   (when-let* ((target (magit-get-push-branch))
   1881               (range  (concat ".." target)))
   1882     (when (magit--insert-pushremote-log-p)
   1883       (magit-insert-section (unpulled range t)
   1884         (magit-insert-heading
   1885           (format (propertize "Unpulled from %s."
   1886                               'font-lock-face 'magit-section-heading)
   1887                   (propertize target 'font-lock-face 'magit-branch-remote)))
   1888         (magit--insert-log nil range magit-buffer-log-args)
   1889         (magit-log-insert-child-count)))))
   1890 
   1891 (cl-defmethod magit-section-ident-value ((section magit-unpushed-section))
   1892   "\"..@{push}\" cannot be used as the value because that is
   1893 ambiguous if `push.default' does not allow a 1:1 mapping, and
   1894 many commands would fail because of that.  But here that does
   1895 not matter and we need an unique value so we use that string
   1896 in the pushremote case."
   1897   (let ((value (oref section value)))
   1898     (if (equal value "@{upstream}..") value "@{push}..")))
   1899 
   1900 (magit-define-section-jumper magit-jump-to-unpushed-to-upstream
   1901   "Unpushed to @{upstream}" unpushed "@{upstream}..")
   1902 
   1903 (defun magit-insert-unpushed-to-upstream-or-recent ()
   1904   "Insert section showing unpushed or other recent commits.
   1905 If an upstream is configured for the current branch and it is
   1906 behind of the current branch, then show the commits that have
   1907 not yet been pushed into the upstream branch.  If no upstream is
   1908 configured or if the upstream is not behind of the current branch,
   1909 then show the last `magit-log-section-commit-count' commits."
   1910   (let ((upstream (magit-get-upstream-branch)))
   1911     (if (or (not upstream)
   1912             (magit-rev-ancestor-p "HEAD" upstream))
   1913         (magit-insert-recent-commits 'unpushed "@{upstream}..")
   1914       (magit-insert-unpushed-to-upstream))))
   1915 
   1916 (defun magit-insert-unpushed-to-upstream ()
   1917   "Insert commits that haven't been pushed to the upstream yet."
   1918   (when (magit-git-success "rev-parse" "@{upstream}")
   1919     (magit-insert-section (unpushed "@{upstream}..")
   1920       (magit-insert-heading
   1921         (format (propertize "Unmerged into %s."
   1922                             'font-lock-face 'magit-section-heading)
   1923                 (magit-get-upstream-branch)))
   1924       (magit--insert-log nil "@{upstream}.." magit-buffer-log-args)
   1925       (magit-log-insert-child-count))))
   1926 
   1927 (defun magit-insert-recent-commits (&optional type value)
   1928   "Insert section showing recent commits.
   1929 Show the last `magit-log-section-commit-count' commits."
   1930   (let* ((start (format "HEAD~%s" magit-log-section-commit-count))
   1931          (range (and (magit-rev-verify start)
   1932                      (concat start "..HEAD"))))
   1933     (magit-insert-section ((eval (or type 'recent))
   1934                            (or value range)
   1935                            t)
   1936       (magit-insert-heading "Recent commits")
   1937       (magit--insert-log nil range
   1938         (cons (format "-n%d" magit-log-section-commit-count)
   1939               (--remove (string-prefix-p "-n" it)
   1940                         magit-buffer-log-args))))))
   1941 
   1942 (magit-define-section-jumper magit-jump-to-unpushed-to-pushremote
   1943   "Unpushed to <push-remote>" unpushed "@{push}..")
   1944 
   1945 (defun magit-insert-unpushed-to-pushremote ()
   1946   "Insert commits that haven't been pushed to the push-remote yet."
   1947   (when-let* ((target (magit-get-push-branch))
   1948               (range  (concat target "..")))
   1949     (when (magit--insert-pushremote-log-p)
   1950       (magit-insert-section (unpushed range t)
   1951         (magit-insert-heading
   1952           (format (propertize "Unpushed to %s."
   1953                               'font-lock-face 'magit-section-heading)
   1954                   (propertize target 'font-lock-face 'magit-branch-remote)))
   1955         (magit--insert-log nil range magit-buffer-log-args)
   1956         (magit-log-insert-child-count)))))
   1957 
   1958 (defun magit--insert-pushremote-log-p ()
   1959   (magit--with-refresh-cache
   1960       (cons default-directory 'magit--insert-pushremote-log-p)
   1961     (not (and (equal (magit-get-push-branch)
   1962                      (magit-get-upstream-branch))
   1963               (or (memq 'magit-insert-unpulled-from-upstream
   1964                         magit-status-sections-hook)
   1965                   (memq 'magit-insert-unpulled-from-upstream-or-recent
   1966                         magit-status-sections-hook))))))
   1967 
   1968 (defun magit-log-insert-child-count ()
   1969   (when magit-section-show-child-count
   1970     (let ((count (length (oref magit-insert-section--current children))))
   1971       (when (> count 0)
   1972         (when (eq count (magit-log-get-commit-limit))
   1973           (setq count (format "%s+" count)))
   1974         (save-excursion
   1975           (goto-char (- (oref magit-insert-section--current content) 2))
   1976           (insert (format " (%s)" count))
   1977           (delete-char 1))))))
   1978 
   1979 ;;;; Auxiliary Log Sections
   1980 
   1981 (defun magit-insert-unpulled-cherries ()
   1982   "Insert section showing unpulled commits.
   1983 Like `magit-insert-unpulled-from-upstream' but prefix each commit
   1984 which has not been applied yet (i.e., a commit with a patch-id
   1985 not shared with any local commit) with \"+\", and all others with
   1986 \"-\"."
   1987   (when (magit-git-success "rev-parse" "@{upstream}")
   1988     (magit-insert-section (unpulled "..@{upstream}")
   1989       (magit-insert-heading "Unpulled commits:")
   1990       (magit-git-wash (apply-partially #'magit-log-wash-log 'cherry)
   1991         "cherry" "-v" (magit-abbrev-arg)
   1992         (magit-get-current-branch) "@{upstream}"))))
   1993 
   1994 (defun magit-insert-unpushed-cherries ()
   1995   "Insert section showing unpushed commits.
   1996 Like `magit-insert-unpushed-to-upstream' but prefix each commit
   1997 which has not been applied to upstream yet (i.e., a commit with
   1998 a patch-id not shared with any upstream commit) with \"+\", and
   1999 all others with \"-\"."
   2000   (when (magit-git-success "rev-parse" "@{upstream}")
   2001     (magit-insert-section (unpushed "@{upstream}..")
   2002       (magit-insert-heading "Unpushed commits:")
   2003       (magit-git-wash (apply-partially #'magit-log-wash-log 'cherry)
   2004         "cherry" "-v" (magit-abbrev-arg) "@{upstream}"))))
   2005 
   2006 ;;; _
   2007 (provide 'magit-log)
   2008 ;;; magit-log.el ends here