dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

magit-reflog.el (7319B)


      1 ;;; magit-reflog.el --- inspect ref history  -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2010-2021  The Magit Project Contributors
      4 ;;
      5 ;; You should have received a copy of the AUTHORS.md file which
      6 ;; lists all contributors.  If not, see http://magit.vc/authors.
      7 
      8 ;; Author: Jonas Bernoulli <jonas@bernoul.li>
      9 ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
     10 
     11 ;; SPDX-License-Identifier: GPL-3.0-or-later
     12 
     13 ;; Magit is free software; you can redistribute it and/or modify it
     14 ;; under the terms of the GNU General Public License as published by
     15 ;; the Free Software Foundation; either version 3, or (at your option)
     16 ;; any later version.
     17 ;;
     18 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
     19 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     20 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     21 ;; License for more details.
     22 ;;
     23 ;; You should have received a copy of the GNU General Public License
     24 ;; along with Magit.  If not, see http://www.gnu.org/licenses.
     25 
     26 ;;; Commentary:
     27 
     28 ;; This library implements support for looking at Git reflogs.
     29 
     30 ;;; Code:
     31 
     32 (require 'magit-core)
     33 (require 'magit-log)
     34 
     35 ;;; Options
     36 
     37 (defcustom magit-reflog-limit 256
     38   "Maximal number of entries initially shown in reflog buffers.
     39 The limit in the current buffer can be changed using \"+\"
     40 and \"-\"."
     41   :package-version '(magit . "3.0.0")
     42   :group 'magit-commands
     43   :type 'number)
     44 
     45 (defcustom magit-reflog-margin
     46   (list (nth 0 magit-log-margin)
     47         (nth 1 magit-log-margin)
     48         'magit-log-margin-width nil
     49         (nth 4 magit-log-margin))
     50   "Format of the margin in `magit-reflog-mode' buffers.
     51 
     52 The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
     53 
     54 If INIT is non-nil, then the margin is shown initially.
     55 STYLE controls how to format the author or committer date.
     56   It can be one of `age' (to show the age of the commit),
     57   `age-abbreviated' (to abbreviate the time unit to a character),
     58   or a string (suitable for `format-time-string') to show the
     59   actual date.  Option `magit-log-margin-show-committer-date'
     60   controls which date is being displayed.
     61 WIDTH controls the width of the margin.  This exists for forward
     62   compatibility and currently the value should not be changed.
     63 AUTHOR controls whether the name of the author is also shown by
     64   default.
     65 AUTHOR-WIDTH has to be an integer.  When the name of the author
     66   is shown, then this specifies how much space is used to do so."
     67   :package-version '(magit . "2.9.0")
     68   :group 'magit-log
     69   :group 'magit-margin
     70   :type magit-log-margin--custom-type
     71   :initialize 'magit-custom-initialize-reset
     72   :set-after '(magit-log-margin)
     73   :set (apply-partially #'magit-margin-set-variable 'magit-reflog-mode))
     74 
     75 ;;; Faces
     76 
     77 (defface magit-reflog-commit '((t :foreground "green"))
     78   "Face for commit commands in reflogs."
     79   :group 'magit-faces)
     80 
     81 (defface magit-reflog-amend '((t :foreground "magenta"))
     82   "Face for amend commands in reflogs."
     83   :group 'magit-faces)
     84 
     85 (defface magit-reflog-merge '((t :foreground "green"))
     86   "Face for merge, checkout and branch commands in reflogs."
     87   :group 'magit-faces)
     88 
     89 (defface magit-reflog-checkout '((t :foreground "blue"))
     90   "Face for checkout commands in reflogs."
     91   :group 'magit-faces)
     92 
     93 (defface magit-reflog-reset '((t :foreground "red"))
     94   "Face for reset commands in reflogs."
     95   :group 'magit-faces)
     96 
     97 (defface magit-reflog-rebase '((t :foreground "magenta"))
     98   "Face for rebase commands in reflogs."
     99   :group 'magit-faces)
    100 
    101 (defface magit-reflog-cherry-pick '((t :foreground "green"))
    102   "Face for cherry-pick commands in reflogs."
    103   :group 'magit-faces)
    104 
    105 (defface magit-reflog-remote '((t :foreground "cyan"))
    106   "Face for pull and clone commands in reflogs."
    107   :group 'magit-faces)
    108 
    109 (defface magit-reflog-other '((t :foreground "cyan"))
    110   "Face for other commands in reflogs."
    111   :group 'magit-faces)
    112 
    113 ;;; Commands
    114 
    115 ;;;###autoload
    116 (defun magit-reflog-current ()
    117   "Display the reflog of the current branch.
    118 If `HEAD' is detached, then show the reflog for that instead."
    119   (interactive)
    120   (magit-reflog-setup-buffer (or (magit-get-current-branch) "HEAD")))
    121 
    122 ;;;###autoload
    123 (defun magit-reflog-other (ref)
    124   "Display the reflog of a branch or another ref."
    125   (interactive (list (magit-read-local-branch-or-ref "Show reflog for")))
    126   (magit-reflog-setup-buffer ref))
    127 
    128 ;;;###autoload
    129 (defun magit-reflog-head ()
    130   "Display the `HEAD' reflog."
    131   (interactive)
    132   (magit-reflog-setup-buffer "HEAD"))
    133 
    134 ;;; Mode
    135 
    136 (defvar magit-reflog-mode-map
    137   (let ((map (make-sparse-keymap)))
    138     (set-keymap-parent map magit-log-mode-map)
    139     (define-key map (kbd "C-c C-n") 'undefined)
    140     (define-key map (kbd "L")       'magit-margin-settings)
    141     map)
    142   "Keymap for `magit-reflog-mode'.")
    143 
    144 (define-derived-mode magit-reflog-mode magit-mode "Magit Reflog"
    145   "Mode for looking at Git reflog.
    146 
    147 This mode is documented in info node `(magit)Reflog'.
    148 
    149 \\<magit-mode-map>\
    150 Type \\[magit-refresh] to refresh the current buffer.
    151 Type \\[magit-visit-thing] or \\[magit-diff-show-or-scroll-up] \
    152 to visit the commit at point.
    153 
    154 Type \\[magit-cherry-pick] to apply the commit at point.
    155 Type \\[magit-reset] to reset `HEAD' to the commit at point.
    156 
    157 \\{magit-reflog-mode-map}"
    158   :group 'magit-log
    159   (hack-dir-local-variables-non-file-buffer))
    160 
    161 (defun magit-reflog-setup-buffer (ref)
    162   (require 'magit)
    163   (magit-setup-buffer #'magit-reflog-mode nil
    164     (magit-buffer-refname ref)
    165     (magit-buffer-log-args (list (format "-n%s" magit-reflog-limit)))))
    166 
    167 (defun magit-reflog-refresh-buffer ()
    168   (magit-set-header-line-format (concat "Reflog for " magit-buffer-refname))
    169   (magit-insert-section (reflogbuf)
    170     (magit-git-wash (apply-partially 'magit-log-wash-log 'reflog)
    171       "reflog" "show" "--format=%h%x00%aN%x00%gd%x00%gs" "--date=raw"
    172       magit-buffer-log-args magit-buffer-refname "--")))
    173 
    174 (cl-defmethod magit-buffer-value (&context (major-mode magit-reflog-mode))
    175   magit-buffer-refname)
    176 
    177 (defvar magit-reflog-labels
    178   '(("commit"      . magit-reflog-commit)
    179     ("amend"       . magit-reflog-amend)
    180     ("merge"       . magit-reflog-merge)
    181     ("checkout"    . magit-reflog-checkout)
    182     ("branch"      . magit-reflog-checkout)
    183     ("reset"       . magit-reflog-reset)
    184     ("rebase"      . magit-reflog-rebase)
    185     ("cherry-pick" . magit-reflog-cherry-pick)
    186     ("initial"     . magit-reflog-commit)
    187     ("pull"        . magit-reflog-remote)
    188     ("clone"       . magit-reflog-remote)
    189     ("autosave"    . magit-reflog-commit)
    190     ("restart"     . magit-reflog-reset)))
    191 
    192 (defun magit-reflog-format-subject (subject)
    193   (let* ((match (string-match magit-reflog-subject-re subject))
    194          (command (and match (match-string 1 subject)))
    195          (option  (and match (match-string 2 subject)))
    196          (type    (and match (match-string 3 subject)))
    197          (label (if (string= command "commit")
    198                     (or type command)
    199                   command))
    200          (text (if (string= command "commit")
    201                    label
    202                  (mapconcat #'identity
    203                             (delq nil (list command option type))
    204                             " "))))
    205     (format "%-16s "
    206             (magit--propertize-face
    207              text (or (cdr (assoc label magit-reflog-labels))
    208                       'magit-reflog-other)))))
    209 
    210 ;;; _
    211 (provide 'magit-reflog)
    212 ;;; magit-reflog.el ends here