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

755 regels
31 KiB

5 jaren geleden
  1. ;;; magit-refs.el --- listing references -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-2019 The Magit Project Contributors
  3. ;;
  4. ;; You should have received a copy of the AUTHORS.md file which
  5. ;; lists all contributors. If not, see http://magit.vc/authors.
  6. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  7. ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
  8. ;; Magit is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; Magit is distributed in the hope that it will be useful, but WITHOUT
  14. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. ;; License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with Magit. If not, see http://www.gnu.org/licenses.
  20. ;;; Commentary:
  21. ;; This library implements support for listing references in a buffer.
  22. ;;; Code:
  23. (eval-when-compile
  24. (require 'subr-x))
  25. (require 'magit)
  26. ;;; Options
  27. (defgroup magit-refs nil
  28. "Inspect and manipulate Git branches and tags."
  29. :link '(info-link "(magit)References Buffer")
  30. :group 'magit-modes)
  31. (defcustom magit-refs-mode-hook nil
  32. "Hook run after entering Magit-Refs mode."
  33. :package-version '(magit . "2.1.0")
  34. :group 'magit-refs
  35. :type 'hook)
  36. (defcustom magit-refs-sections-hook
  37. '(magit-insert-error-header
  38. magit-insert-branch-description
  39. magit-insert-local-branches
  40. magit-insert-remote-branches
  41. magit-insert-tags)
  42. "Hook run to insert sections into a references buffer."
  43. :package-version '(magit . "2.1.0")
  44. :group 'magit-refs
  45. :type 'hook)
  46. (defcustom magit-refs-show-commit-count nil
  47. "Whether to show commit counts in Magit-Refs mode buffers.
  48. all Show counts for branches and tags.
  49. branch Show counts for branches only.
  50. nil Never show counts.
  51. To change the value in an existing buffer use the command
  52. `magit-refs-show-commit-count'"
  53. :package-version '(magit . "2.1.0")
  54. :group 'magit-refs
  55. :safe (lambda (val) (memq val '(all branch nil)))
  56. :type '(choice (const all :tag "For branches and tags")
  57. (const branch :tag "For branches only")
  58. (const nil :tag "Never")))
  59. (put 'magit-refs-show-commit-count 'safe-local-variable 'symbolp)
  60. (put 'magit-refs-show-commit-count 'permanent-local t)
  61. (defcustom magit-refs-pad-commit-counts nil
  62. "Whether to pad all counts on all sides in `magit-refs-mode' buffers.
  63. If this is nil, then some commit counts are displayed right next
  64. to one of the branches that appear next to the count, without any
  65. space in between. This might look bad if the branch name faces
  66. look too similar to `magit-dimmed'.
  67. If this is non-nil, then spaces are placed on both sides of all
  68. commit counts."
  69. :package-version '(magit . "2.12.0")
  70. :group 'magit-refs
  71. :type 'boolean)
  72. (defvar magit-refs-show-push-remote nil
  73. "Whether to show the push-remotes of local branches.
  74. Also show the commits that the local branch is ahead and behind
  75. the push-target. Unfortunately there is a bug in Git that makes
  76. this useless (the commits ahead and behind the upstream are
  77. shown), so this isn't enabled yet.")
  78. (defcustom magit-refs-show-remote-prefix nil
  79. "Whether to show the remote prefix in lists of remote branches.
  80. This is redundant because the name of the remote is already shown
  81. in the heading preceeding the list of its branches."
  82. :package-version '(magit . "2.12.0")
  83. :group 'magit-refs
  84. :type 'boolean)
  85. (defcustom magit-refs-margin
  86. (list nil
  87. (nth 1 magit-log-margin)
  88. 'magit-log-margin-width nil
  89. (nth 4 magit-log-margin))
  90. "Format of the margin in `magit-refs-mode' buffers.
  91. The value has the form (INIT STYLE WIDTH AUTHOR AUTHOR-WIDTH).
  92. If INIT is non-nil, then the margin is shown initially.
  93. STYLE controls how to format the committer date. It can be one
  94. of `age' (to show the age of the commit), `age-abbreviated' (to
  95. abbreviate the time unit to a character), or a string (suitable
  96. for `format-time-string') to show the actual date.
  97. WIDTH controls the width of the margin. This exists for forward
  98. compatibility and currently the value should not be changed.
  99. AUTHOR controls whether the name of the author is also shown by
  100. default.
  101. AUTHOR-WIDTH has to be an integer. When the name of the author
  102. is shown, then this specifies how much space is used to do so."
  103. :package-version '(magit . "2.9.0")
  104. :group 'magit-refs
  105. :group 'magit-margin
  106. :safe (lambda (val) (memq val '(all branch nil)))
  107. :type magit-log-margin--custom-type
  108. :initialize 'magit-custom-initialize-reset
  109. :set-after '(magit-log-margin)
  110. :set (apply-partially #'magit-margin-set-variable 'magit-refs-mode))
  111. (defcustom magit-refs-margin-for-tags nil
  112. "Whether to show information about tags in the margin.
  113. This is disabled by default because it is slow if there are many
  114. tags."
  115. :package-version '(magit . "2.9.0")
  116. :group 'magit-refs
  117. :group 'magit-margin
  118. :type 'boolean)
  119. (defcustom magit-refs-primary-column-width (cons 16 32)
  120. "Width of the focus column in `magit-refs-mode' buffers.
  121. The primary column is the column that contains the name of the
  122. branch that the current row is about.
  123. If this is an integer, then the column is that many columns wide.
  124. Otherwise it has to be a cons-cell of two integers. The first
  125. specifies the minimal width, the second the maximal width. In that
  126. case the actual width is determined using the length of the names
  127. of the shown local branches. (Remote branches and tags are not
  128. taken into account when calculating to optimal width.)"
  129. :package-version '(magit . "2.12.0")
  130. :group 'magit-refs
  131. :type '(choice (integer :tag "Constant wide")
  132. (cons :tag "Wide constrains"
  133. (integer :tag "Minimum")
  134. (integer :tag "Maximum"))))
  135. (defcustom magit-refs-focus-column-width 5
  136. "Width of the focus column in `magit-refs-mode' buffers.
  137. The focus column is the first column, which marks one
  138. branch (usually the current branch) as the focused branch using
  139. \"*\" or \"@\". For each other reference, this column optionally
  140. shows how many commits it is ahead of the focused branch and \"<\", or
  141. if it isn't ahead then the commits it is behind and \">\", or if it
  142. isn't behind either, then a \"=\".
  143. This column may also display only \"*\" or \"@\" for the focused
  144. branch, in which case this option is ignored. Use \"L v\" to
  145. change the verbosity of this column."
  146. :package-version '(magit . "2.12.0")
  147. :group 'magit-refs
  148. :type 'integer)
  149. (defcustom magit-refs-filter-alist nil
  150. "Alist controlling which refs are omitted from `magit-refs-mode' buffers.
  151. The purpose of this option is to forgo displaying certain refs
  152. based on their name. If you want to not display any refs of a
  153. certain type, then you should remove the appropriate function
  154. from `magit-refs-sections-hook' instead.
  155. All keys are tried in order until one matches. Then its value
  156. is used and subsequent elements are ignored. If the value is
  157. non-nil, then the reference is displayed, otherwise it is not.
  158. If no element matches, then the reference is displayed.
  159. A key can either be a regular expression that the refname has to
  160. match, or a function that takes the refname as only argument and
  161. returns a boolean. A remote branch such as \"origin/master\" is
  162. displayed as just \"master\", however for this comparison the
  163. former is used."
  164. :package-version '(magit . "2.12.0")
  165. :group 'magit-refs
  166. :type '(alist :key-type (choice :tag "Key" regexp function)
  167. :value-type (boolean :tag "Value"
  168. :on "show (non-nil)"
  169. :off "omit (nil)")))
  170. (defcustom magit-visit-ref-behavior nil
  171. "Control how `magit-visit-ref' behaves in `magit-refs-mode' buffers.
  172. By default `magit-visit-ref' behaves like `magit-show-commit',
  173. in all buffers, including `magit-refs-mode' buffers. When the
  174. type of the section at point is `commit' then \"RET\" is bound to
  175. `magit-show-commit', and when the type is either `branch' or
  176. `tag' then it is bound to `magit-visit-ref'.
  177. \"RET\" is one of Magit's most essential keys and at least by
  178. default it should behave consistently across all of Magit,
  179. especially because users quickly learn that it does something
  180. very harmless; it shows more information about the thing at point
  181. in another buffer.
  182. However \"RET\" used to behave differently in `magit-refs-mode'
  183. buffers, doing surprising things, some of which cannot really be
  184. described as \"visit this thing\". If you have grown accustomed
  185. to such inconsistent, but to you useful, behavior, then you can
  186. restore that by adding one or more of the below symbols to the
  187. value of this option. But keep in mind that by doing so you
  188. don't only introduce inconsistencies, you also lose some
  189. functionality and might have to resort to `M-x magit-show-commit'
  190. to get it back.
  191. `magit-visit-ref' looks for these symbols in the order in which
  192. they are described here. If the presence of a symbol applies to
  193. the current situation, then the symbols that follow do not affect
  194. the outcome.
  195. `focus-on-ref'
  196. With a prefix argument update the buffer to show commit counts
  197. and lists of cherry commits relative to the reference at point
  198. instead of relative to the current buffer or `HEAD'.
  199. Instead of adding this symbol, consider pressing \"C-u y o RET\".
  200. `create-branch'
  201. If point is on a remote branch, then create a new local branch
  202. with the same name, use the remote branch as its upstream, and
  203. then check out the local branch.
  204. Instead of adding this symbol, consider pressing \"b c RET RET\",
  205. like you would do in other buffers.
  206. `checkout-any'
  207. Check out the reference at point. If that reference is a tag
  208. or a remote branch, then this results in a detached `HEAD'.
  209. Instead of adding this symbol, consider pressing \"b b RET\",
  210. like you would do in other buffers.
  211. `checkout-branch'
  212. Check out the local branch at point.
  213. Instead of adding this symbol, consider pressing \"b b RET\",
  214. like you would do in other buffers."
  215. :package-version '(magit . "2.9.0")
  216. :group 'magit-refs
  217. :group 'magit-commands
  218. :options '(focus-on-ref create-branch checkout-any checkout-branch)
  219. :type '(list :convert-widget custom-hook-convert-widget))
  220. ;;; Mode
  221. (defvar magit-refs-mode-map
  222. (let ((map (make-sparse-keymap)))
  223. (set-keymap-parent map magit-mode-map)
  224. (define-key map "\C-y" 'magit-refs-set-show-commit-count)
  225. (define-key map "L" 'magit-margin-settings)
  226. map)
  227. "Keymap for `magit-refs-mode'.")
  228. (define-derived-mode magit-refs-mode magit-mode "Magit Refs"
  229. "Mode which lists and compares references.
  230. This mode is documented in info node `(magit)References Buffer'.
  231. \\<magit-mode-map>\
  232. Type \\[magit-refresh] to refresh the current buffer.
  233. Type \\[magit-section-toggle] to expand or hide the section at point.
  234. Type \\[magit-visit-thing] or \\[magit-diff-show-or-scroll-up] \
  235. to visit the commit or branch at point.
  236. Type \\[magit-branch] to see available branch commands.
  237. Type \\[magit-merge] to merge the branch or commit at point.
  238. Type \\[magit-cherry-pick] to apply the commit at point.
  239. Type \\[magit-reset] to reset `HEAD' to the commit at point.
  240. \\{magit-refs-mode-map}"
  241. :group 'magit-refs
  242. (hack-dir-local-variables-non-file-buffer)
  243. (setq imenu-create-index-function
  244. #'magit-imenu--refs-create-index-function))
  245. (defun magit-refs-setup-buffer (ref args)
  246. (magit-setup-buffer #'magit-refs-mode nil
  247. (magit-buffer-upstream ref)
  248. (magit-buffer-arguments args)))
  249. (defun magit-refs-refresh-buffer ()
  250. (setq magit-set-buffer-margin-refresh (not (magit-buffer-margin-p)))
  251. (unless (magit-rev-verify magit-buffer-upstream)
  252. (setq magit-refs-show-commit-count nil))
  253. (magit-set-header-line-format
  254. (format "%s %s" magit-buffer-upstream
  255. (mapconcat #'identity magit-buffer-arguments " ")))
  256. (magit-insert-section (branchbuf)
  257. (magit-run-section-hook 'magit-refs-sections-hook))
  258. (add-hook 'kill-buffer-hook 'magit-preserve-section-visibility-cache))
  259. (cl-defmethod magit-buffer-value (&context (major-mode magit-refs-mode))
  260. (cons magit-buffer-upstream magit-buffer-arguments))
  261. ;;; Commands
  262. ;;;###autoload (autoload 'magit-show-refs "magit-refs" nil t)
  263. (define-transient-command magit-show-refs (&optional transient)
  264. "List and compare references in a dedicated buffer."
  265. :man-page "git-branch"
  266. :value 'magit-show-refs-arguments
  267. ["Arguments"
  268. (magit-for-each-ref:--contains)
  269. ("=m" "Merged" "--merged=" magit-transient-read-revision)
  270. ("-m" "Merged to HEAD" "--merged")
  271. ("-M" "Merged to master" "--merged=master")
  272. ("=n" "Not merged" "--no-merged=" magit-transient-read-revision)
  273. ("-n" "Not merged to HEAD" "--no-merged")
  274. ("-N" "Not merged to master" "--no-merged=master")
  275. (magit-for-each-ref:--sort)]
  276. ["Actions"
  277. ("y" "Show refs, comparing them with HEAD" magit-show-refs-head)
  278. ("c" "Show refs, comparing them with current branch" magit-show-refs-current)
  279. ("o" "Show refs, comparing them with other branch" magit-show-refs-other)]
  280. (interactive (list (or (derived-mode-p 'magit-refs-mode)
  281. current-prefix-arg)))
  282. (if transient
  283. (transient-setup 'magit-show-refs)
  284. (magit-refs-setup-buffer "HEAD" (magit-show-refs-arguments))))
  285. (defun magit-show-refs-arguments ()
  286. (let (args)
  287. (cond
  288. ((eq current-transient-command 'magit-show-refs)
  289. (setq args (transient-args 'magit-show-refs)))
  290. ((eq major-mode 'magit-show-refs-mode)
  291. (setq args magit-buffer-arguments))
  292. ((and (memq magit-prefix-use-buffer-arguments '(always selected))
  293. (when-let ((buffer (magit-get-mode-buffer
  294. 'magit-refs-mode nil
  295. (or (eq magit-prefix-use-buffer-arguments
  296. 'selected)
  297. 'all))))
  298. (setq args (buffer-local-value 'magit-buffer-arguments buffer))
  299. t)))
  300. (t
  301. (setq args (alist-get 'magit-show-refs transient-values))))
  302. args))
  303. (define-infix-argument magit-for-each-ref:--contains ()
  304. :description "Contains"
  305. :class 'transient-option
  306. :key "-c"
  307. :argument "--contains="
  308. :reader 'magit-transient-read-revision)
  309. (define-infix-argument magit-for-each-ref:--sort ()
  310. :description "Sort"
  311. :class 'transient-option
  312. :key "-s"
  313. :argument "--sort="
  314. :reader 'magit-read-ref-sort)
  315. (defun magit-read-ref-sort (prompt initial-input _history)
  316. (magit-completing-read prompt
  317. '("-committerdate" "-authordate"
  318. "committerdate" "authordate")
  319. nil nil initial-input))
  320. ;;;###autoload
  321. (defun magit-show-refs-head (&optional args)
  322. "List and compare references in a dedicated buffer.
  323. Compared with `HEAD'."
  324. (interactive (list (magit-show-refs-arguments)))
  325. (magit-refs-setup-buffer "HEAD" args))
  326. ;;;###autoload
  327. (defun magit-show-refs-current (&optional args)
  328. "List and compare references in a dedicated buffer.
  329. Compare with the current branch or `HEAD' if it is detached."
  330. (interactive (list (magit-show-refs-arguments)))
  331. (magit-refs-setup-buffer (magit-get-current-branch) args))
  332. ;;;###autoload
  333. (defun magit-show-refs-other (&optional ref args)
  334. "List and compare references in a dedicated buffer.
  335. Compared with a branch read from the user."
  336. (interactive (list (magit-read-other-branch "Compare with")
  337. (magit-show-refs-arguments)))
  338. (magit-refs-setup-buffer ref args))
  339. (defun magit-refs-set-show-commit-count ()
  340. "Change for which refs the commit count is shown."
  341. (interactive)
  342. (setq-local magit-refs-show-commit-count
  343. (magit-read-char-case "Show commit counts for " nil
  344. (?a "[a]ll refs" 'all)
  345. (?b "[b]ranches only" t)
  346. (?n "[n]othing" nil)))
  347. (magit-refresh))
  348. (defun magit-visit-ref ()
  349. "Visit the reference or revision at point in another buffer.
  350. If there is no revision at point or with a prefix argument prompt
  351. for a revision.
  352. This command behaves just like `magit-show-commit', except if
  353. point is on a reference in a `magit-refs-mode' buffer (a buffer
  354. listing branches and tags), in which case the behavior may be
  355. different, but only if you have customized the option
  356. `magit-visit-ref-behavior' (which see)."
  357. (interactive)
  358. (if (and (derived-mode-p 'magit-refs-mode)
  359. (magit-section-match '(branch tag)))
  360. (let ((ref (oref (magit-current-section) value)))
  361. (cond (current-prefix-arg
  362. (cond ((memq 'focus-on-ref magit-visit-ref-behavior)
  363. (magit-refs-setup-buffer ref (magit-show-refs-arguments)))
  364. (magit-visit-ref-behavior
  365. ;; Don't prompt for commit to visit.
  366. (let ((current-prefix-arg nil))
  367. (call-interactively #'magit-show-commit)))))
  368. ((and (memq 'create-branch magit-visit-ref-behavior)
  369. (magit-section-match [branch remote]))
  370. (let ((branch (cdr (magit-split-branch-name ref))))
  371. (if (magit-branch-p branch)
  372. (if (magit-rev-eq branch ref)
  373. (magit-call-git "checkout" branch)
  374. (setq branch (propertize branch 'face 'magit-branch-local))
  375. (setq ref (propertize ref 'face 'magit-branch-remote))
  376. (pcase (prog1 (read-char-choice (format (propertize "\
  377. Branch %s already exists.
  378. [c]heckout %s as-is
  379. [r]reset %s to %s and checkout %s
  380. [a]bort " 'face 'minibuffer-prompt) branch branch branch ref branch)
  381. '(?c ?r ?a))
  382. (message "")) ; otherwise prompt sticks
  383. (?c (magit-call-git "checkout" branch))
  384. (?r (magit-call-git "checkout" "-B" branch ref))
  385. (?a (user-error "Abort"))))
  386. (magit-call-git "checkout" "-b" branch ref))
  387. (setq magit-buffer-upstream branch)
  388. (magit-refresh)))
  389. ((or (memq 'checkout-any magit-visit-ref-behavior)
  390. (and (memq 'checkout-branch magit-visit-ref-behavior)
  391. (magit-section-match [branch local])))
  392. (magit-call-git "checkout" ref)
  393. (setq magit-buffer-upstream ref)
  394. (magit-refresh))
  395. (t
  396. (call-interactively #'magit-show-commit))))
  397. (call-interactively #'magit-show-commit)))
  398. ;;; Sections
  399. (defvar magit-remote-section-map
  400. (let ((map (make-sparse-keymap)))
  401. (define-key map [remap magit-delete-thing] 'magit-remote-remove)
  402. (define-key map "R" 'magit-remote-rename)
  403. map)
  404. "Keymap for `remote' sections.")
  405. (defvar magit-branch-section-map
  406. (let ((map (make-sparse-keymap)))
  407. (define-key map [remap magit-visit-thing] 'magit-visit-ref)
  408. (define-key map [remap magit-delete-thing] 'magit-branch-delete)
  409. (define-key map "R" 'magit-branch-rename)
  410. map)
  411. "Keymap for `branch' sections.")
  412. (defvar magit-tag-section-map
  413. (let ((map (make-sparse-keymap)))
  414. (define-key map [remap magit-visit-thing] 'magit-visit-ref)
  415. (define-key map [remap magit-delete-thing] 'magit-tag-delete)
  416. map)
  417. "Keymap for `tag' sections.")
  418. (defun magit-insert-branch-description ()
  419. "Insert header containing the description of the current branch.
  420. Insert a header line with the name and description of the
  421. current branch. The description is taken from the Git variable
  422. `branch.<NAME>.description'; if that is undefined then no header
  423. line is inserted at all."
  424. (when-let ((branch (magit-get-current-branch))
  425. (desc (magit-get "branch" branch "description"))
  426. (desc (split-string desc "\n")))
  427. (when (equal (car (last desc)) "")
  428. (setq desc (butlast desc)))
  429. (magit-insert-section (branchdesc branch t)
  430. (magit-insert-heading branch ": " (car desc))
  431. (when (cdr desc)
  432. (insert (mapconcat 'identity (cdr desc) "\n"))
  433. (insert "\n\n")))))
  434. (defun magit-insert-tags ()
  435. "Insert sections showing all tags."
  436. (when-let ((tags (magit-git-lines "tag" "--list" "-n" magit-buffer-arguments)))
  437. (let ((_head (magit-rev-parse "HEAD")))
  438. (magit-insert-section (tags)
  439. (magit-insert-heading "Tags:")
  440. (dolist (tag tags)
  441. (string-match "^\\([^ \t]+\\)[ \t]+\\([^ \t\n].*\\)?" tag)
  442. (let ((tag (match-string 1 tag))
  443. (msg (match-string 2 tag)))
  444. (when (magit-refs--insert-refname-p tag)
  445. (magit-insert-section (tag tag t)
  446. (magit-insert-heading
  447. (magit-refs--format-focus-column tag 'tag)
  448. (propertize tag 'font-lock-face 'magit-tag)
  449. (make-string (max 1 (- magit-refs-primary-column-width
  450. (length tag)))
  451. ?\s)
  452. (and msg (magit-log-propertize-keywords nil msg)))
  453. (when (and magit-refs-margin-for-tags (magit-buffer-margin-p))
  454. (magit-refs--format-margin tag))
  455. (magit-refs--insert-cherry-commits tag)))))
  456. (insert ?\n)
  457. (magit-make-margin-overlay nil t)))))
  458. (defun magit-insert-remote-branches ()
  459. "Insert sections showing all remote-tracking branches."
  460. (dolist (remote (magit-list-remotes))
  461. (magit-insert-section (remote remote)
  462. (magit-insert-heading
  463. (let ((pull (magit-get "remote" remote "url"))
  464. (push (magit-get "remote" remote "pushurl")))
  465. (format (propertize "Remote %s (%s):"
  466. 'font-lock-face 'magit-section-heading)
  467. (propertize remote 'font-lock-face 'magit-branch-remote)
  468. (concat pull (and pull push ", ") push))))
  469. (let (head)
  470. (dolist (line (magit-git-lines "for-each-ref" "--format=\
  471. %(symref:short)%00%(refname:short)%00%(refname)%00%(subject)"
  472. (concat "refs/remotes/" remote)
  473. magit-buffer-arguments))
  474. (pcase-let ((`(,head-branch ,branch ,ref ,msg)
  475. (-replace "" nil (split-string line "\0"))))
  476. (if head-branch
  477. (progn (cl-assert (equal branch (concat remote "/HEAD")))
  478. (setq head head-branch))
  479. (when (magit-refs--insert-refname-p branch)
  480. (magit-insert-section (branch branch t)
  481. (let ((headp (equal branch head))
  482. (abbrev (if magit-refs-show-remote-prefix
  483. branch
  484. (substring branch (1+ (length remote))))))
  485. (magit-insert-heading
  486. (magit-refs--format-focus-column branch)
  487. (magit-refs--propertize-branch
  488. abbrev ref (and headp 'magit-branch-remote-head))
  489. (make-string (max 1 (- magit-refs-primary-column-width
  490. (length abbrev)))
  491. ?\s)
  492. (and msg (magit-log-propertize-keywords nil msg))))
  493. (when (magit-buffer-margin-p)
  494. (magit-refs--format-margin branch))
  495. (magit-refs--insert-cherry-commits branch)))))))
  496. (insert ?\n)
  497. (magit-make-margin-overlay nil t))))
  498. (defun magit-insert-local-branches ()
  499. "Insert sections showing all local branches."
  500. (magit-insert-section (local nil)
  501. (magit-insert-heading "Branches:")
  502. (dolist (line (magit-refs--format-local-branches))
  503. (pcase-let ((`(,branch . ,strings) line))
  504. (magit-insert-section
  505. ((eval (if branch 'branch 'commit))
  506. (or branch (magit-rev-parse "HEAD"))
  507. t)
  508. (apply #'magit-insert-heading strings)
  509. (when (magit-buffer-margin-p)
  510. (magit-refs--format-margin branch))
  511. (magit-refs--insert-cherry-commits branch))))
  512. (insert ?\n)
  513. (magit-make-margin-overlay nil t)))
  514. (defun magit-refs--format-local-branches ()
  515. (let ((lines (-keep 'magit-refs--format-local-branch
  516. (magit-git-lines
  517. "for-each-ref"
  518. (concat "--format=\
  519. %(HEAD)%00%(refname:short)%00%(refname)%00\
  520. %(upstream:short)%00%(upstream)%00%(upstream:track)%00"
  521. (if magit-refs-show-push-remote "\
  522. %(push:remotename)%00%(push)%00%(push:track)%00%(subject)"
  523. "%00%00%00%(subject)"))
  524. "refs/heads"
  525. magit-buffer-arguments))))
  526. (unless (magit-get-current-branch)
  527. (push (magit-refs--format-local-branch
  528. (concat "*\0\0\0\0\0\0\0\0" (magit-rev-format "%s")))
  529. lines))
  530. (setq-local magit-refs-primary-column-width
  531. (let ((def (default-value 'magit-refs-primary-column-width)))
  532. (if (atom def)
  533. def
  534. (pcase-let ((`(,min . ,max) def))
  535. (min max (apply #'max min (mapcar #'car lines)))))))
  536. (mapcar (pcase-lambda (`(,_ ,branch ,focus ,branch-desc ,u:ahead ,p:ahead
  537. ,u:behind ,upstream ,p:behind ,push ,msg))
  538. (list branch focus branch-desc u:ahead p:ahead
  539. (make-string (max 1 (- magit-refs-primary-column-width
  540. (length (concat branch-desc
  541. u:ahead
  542. p:ahead
  543. u:behind))))
  544. ?\s)
  545. u:behind upstream p:behind push
  546. msg))
  547. lines)))
  548. (defun magit-refs--format-local-branch (line)
  549. (pcase-let ((`(,head ,branch ,ref ,upstream ,u:ref ,u:track
  550. ,push ,p:ref ,p:track ,msg)
  551. (-replace "" nil (split-string line "\0"))))
  552. (when (or (not branch)
  553. (magit-refs--insert-refname-p branch))
  554. (let* ((headp (equal head "*"))
  555. (pushp (and push
  556. magit-refs-show-push-remote
  557. (magit-rev-verify p:ref)
  558. (not (equal p:ref u:ref))))
  559. (branch-desc
  560. (if branch
  561. (magit-refs--propertize-branch
  562. branch ref (and headp 'magit-branch-current))
  563. (magit--propertize-face "(detached)"
  564. 'font-lock-warning-face)))
  565. (u:ahead (and u:track
  566. (string-match "ahead \\([0-9]+\\)" u:track)
  567. (magit--propertize-face
  568. (concat (and magit-refs-pad-commit-counts " ")
  569. (match-string 1 u:track)
  570. ">")
  571. 'magit-dimmed)))
  572. (u:behind (and u:track
  573. (string-match "behind \\([0-9]+\\)" u:track)
  574. (magit--propertize-face
  575. (concat "<"
  576. (match-string 1 u:track)
  577. (and magit-refs-pad-commit-counts " "))
  578. 'magit-dimmed)))
  579. (p:ahead (and pushp p:track
  580. (string-match "ahead \\([0-9]+\\)" p:track)
  581. (magit--propertize-face
  582. (concat (match-string 1 p:track)
  583. ">"
  584. (and magit-refs-pad-commit-counts " "))
  585. 'magit-branch-remote)))
  586. (p:behind (and pushp p:track
  587. (string-match "behind \\([0-9]+\\)" p:track)
  588. (magit--propertize-face
  589. (concat "<"
  590. (match-string 1 p:track)
  591. (and magit-refs-pad-commit-counts " "))
  592. 'magit-dimmed))))
  593. (list (1+ (length (concat branch-desc u:ahead p:ahead u:behind)))
  594. branch
  595. (magit-refs--format-focus-column branch headp)
  596. branch-desc u:ahead p:ahead u:behind
  597. (and upstream
  598. (concat (if (equal u:track "[gone]")
  599. (magit--propertize-face upstream 'error)
  600. (magit-refs--propertize-branch upstream u:ref))
  601. " "))
  602. (and pushp
  603. (concat p:behind
  604. (magit--propertize-face
  605. push 'magit-branch-remote)
  606. " "))
  607. (and msg (magit-log-propertize-keywords nil msg)))))))
  608. (defun magit-refs--format-focus-column (ref &optional type)
  609. (let ((focus magit-buffer-upstream)
  610. (width (if magit-refs-show-commit-count
  611. magit-refs-focus-column-width
  612. 1)))
  613. (format
  614. (format "%%%ss " width)
  615. (cond ((or (equal ref focus)
  616. (and (eq type t)
  617. (equal focus "HEAD")))
  618. (magit--propertize-face (concat (if (equal focus "HEAD") "@" "*")
  619. (make-string (1- width) ?\s))
  620. 'magit-section-heading))
  621. ((if (eq type 'tag)
  622. (eq magit-refs-show-commit-count 'all)
  623. magit-refs-show-commit-count)
  624. (pcase-let ((`(,behind ,ahead)
  625. (magit-rev-diff-count magit-buffer-upstream ref)))
  626. (magit--propertize-face
  627. (cond ((> ahead 0) (concat "<" (number-to-string ahead)))
  628. ((> behind 0) (concat (number-to-string behind) ">"))
  629. (t "="))
  630. 'magit-dimmed)))
  631. (t "")))))
  632. (defun magit-refs--propertize-branch (branch ref &optional head-face)
  633. (let ((face (cdr (cl-find-if (pcase-lambda (`(,re . ,_))
  634. (string-match-p re ref))
  635. magit-ref-namespaces))))
  636. (magit--propertize-face
  637. branch (if head-face (list face head-face) face))))
  638. (defun magit-refs--insert-refname-p (refname)
  639. (--if-let (-first (pcase-lambda (`(,key . ,_))
  640. (if (functionp key)
  641. (funcall key refname)
  642. (string-match-p key refname)))
  643. magit-refs-filter-alist)
  644. (cdr it)
  645. t))
  646. (defun magit-refs--insert-cherry-commits (ref)
  647. (magit-insert-section-body
  648. (let ((start (point))
  649. (magit-insert-section--current nil))
  650. (magit-git-wash (apply-partially 'magit-log-wash-log 'cherry)
  651. "cherry" "-v" (magit-abbrev-arg) magit-buffer-upstream ref)
  652. (if (= (point) start)
  653. (message "No cherries for %s" ref)
  654. (magit-make-margin-overlay nil t)))))
  655. (defun magit-refs--format-margin (commit)
  656. (save-excursion
  657. (goto-char (line-beginning-position 0))
  658. (let ((line (magit-rev-format "%ct%cN" commit)))
  659. (magit-log-format-margin commit
  660. (substring line 10)
  661. (substring line 0 10)))))
  662. ;;; _
  663. (provide 'magit-refs)
  664. ;;; magit-refs.el ends here