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.

323 lines
12 KiB

5 years ago
  1. ;;; magit-repos.el --- listing repositories -*- 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 repositories. This
  22. ;; includes getting a Lisp list of known repositories as well as a
  23. ;; mode for listing repositories in a buffer.
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'subr-x))
  27. (require 'magit-core)
  28. (declare-function magit-status-setup-buffer "magit-status" (directory))
  29. (defvar x-stretch-cursor)
  30. ;;; Options
  31. (defcustom magit-repository-directories nil
  32. "List of directories that are or contain Git repositories.
  33. Each element has the form (DIRECTORY . DEPTH). DIRECTORY has
  34. to be a directory or a directory file-name, a string. DEPTH,
  35. an integer, specifies the maximum depth to look for Git
  36. repositories. If it is 0, then only add DIRECTORY itself.
  37. This option controls which repositories are being listed by
  38. `magit-list-repositories'. It also affects `magit-status'
  39. \(which see) in potentially surprising ways."
  40. :package-version '(magit . "2.91.0")
  41. :group 'magit-essentials
  42. :type '(repeat (cons directory (integer :tag "Depth"))))
  43. (defgroup magit-repolist nil
  44. "List repositories in a buffer."
  45. :link '(info-link "(magit)Repository List")
  46. :group 'magit-modes)
  47. (defcustom magit-repolist-mode-hook '(hl-line-mode)
  48. "Hook run after entering Magit-Repolist mode."
  49. :package-version '(magit . "2.9.0")
  50. :group 'magit-repolist
  51. :type 'hook
  52. :get 'magit-hook-custom-get
  53. :options '(hl-line-mode))
  54. (defcustom magit-repolist-columns
  55. '(("Name" 25 magit-repolist-column-ident nil)
  56. ("Version" 25 magit-repolist-column-version nil)
  57. ("B<U" 3 magit-repolist-column-unpulled-from-upstream
  58. ((:right-align t)
  59. (:help-echo "Upstream changes not in branch")))
  60. ("B>U" 3 magit-repolist-column-unpushed-to-upstream
  61. ((:right-align t)
  62. (:help-echo "Local changes not in upstream")))
  63. ("Path" 99 magit-repolist-column-path nil))
  64. "List of columns displayed by `magit-list-repositories'.
  65. Each element has the form (HEADER WIDTH FORMAT PROPS).
  66. HEADER is the string displayed in the header. WIDTH is the width
  67. of the column. FORMAT is a function that is called with one
  68. argument, the repository identification (usually its basename),
  69. and with `default-directory' bound to the toplevel of its working
  70. tree. It has to return a string to be inserted or nil. PROPS is
  71. an alist that supports the keys `:right-align' and `:pad-right'.
  72. Some entries also use `:help-echo', but `tabulated-list' does not
  73. actually support that yet."
  74. :package-version '(magit . "2.12.0")
  75. :group 'magit-repolist
  76. :type `(repeat (list :tag "Column"
  77. (string :tag "Header Label")
  78. (integer :tag "Column Width")
  79. (function :tag "Inserter Function")
  80. (repeat :tag "Properties"
  81. (list (choice :tag "Property"
  82. (const :right-align)
  83. (const :pad-right)
  84. (symbol))
  85. (sexp :tag "Value"))))))
  86. ;;; List Repositories
  87. ;;;; Command
  88. ;;;###autoload
  89. (defun magit-list-repositories ()
  90. "Display a list of repositories.
  91. Use the options `magit-repository-directories' to control which
  92. repositories are displayed."
  93. (interactive)
  94. (if magit-repository-directories
  95. (with-current-buffer (get-buffer-create "*Magit Repositories*")
  96. (magit-repolist-mode)
  97. (magit-repolist-refresh)
  98. (tabulated-list-print)
  99. (switch-to-buffer (current-buffer)))
  100. (message "You need to customize `magit-repository-directories' %s"
  101. "before you can list repositories")))
  102. ;;;; Mode
  103. (defvar magit-repolist-mode-map
  104. (let ((map (make-sparse-keymap)))
  105. (set-keymap-parent map tabulated-list-mode-map)
  106. (define-key map (if (featurep 'jkl) [return] (kbd "C-m"))
  107. 'magit-repolist-status)
  108. map)
  109. "Local keymap for Magit-Repolist mode buffers.")
  110. (defun magit-repolist-status (&optional _button)
  111. "Show the status for the repository at point."
  112. (interactive)
  113. (--if-let (tabulated-list-get-id)
  114. (magit-status-setup-buffer (expand-file-name it))
  115. (user-error "There is no repository at point")))
  116. (define-derived-mode magit-repolist-mode tabulated-list-mode "Repos"
  117. "Major mode for browsing a list of Git repositories."
  118. (setq-local x-stretch-cursor nil)
  119. (setq tabulated-list-padding 0)
  120. (setq tabulated-list-sort-key (cons "Path" nil))
  121. (setq tabulated-list-format
  122. (vconcat (mapcar (pcase-lambda (`(,title ,width ,_fn ,props))
  123. (nconc (list title width t)
  124. (-flatten props)))
  125. magit-repolist-columns)))
  126. (tabulated-list-init-header)
  127. (add-hook 'tabulated-list-revert-hook 'magit-repolist-refresh nil t)
  128. (setq imenu-prev-index-position-function
  129. 'magit-imenu--repolist-prev-index-position-function)
  130. (setq imenu-extract-index-name-function
  131. 'magit-imenu--repolist-extract-index-name-function))
  132. (defun magit-repolist-refresh ()
  133. (setq tabulated-list-entries
  134. (mapcar (pcase-lambda (`(,id . ,path))
  135. (let ((default-directory path))
  136. (list path
  137. (vconcat (--map (or (funcall (nth 2 it) id) "")
  138. magit-repolist-columns)))))
  139. (magit-list-repos-uniquify
  140. (--map (cons (file-name-nondirectory (directory-file-name it))
  141. it)
  142. (magit-list-repos))))))
  143. ;;;; Columns
  144. (defun magit-repolist-column-ident (id)
  145. "Insert the identification of the repository.
  146. Usually this is just its basename."
  147. id)
  148. (defun magit-repolist-column-path (_id)
  149. "Insert the absolute path of the repository."
  150. (abbreviate-file-name default-directory))
  151. (defun magit-repolist-column-version (_id)
  152. "Insert a description of the repository's `HEAD' revision."
  153. (when-let ((v (or (magit-git-string "describe" "--tags" "--dirty")
  154. ;; If there are no tags, use the date in MELPA format.
  155. (magit-git-string "show" "--no-patch" "--format=%cd-g%h"
  156. "--date=format:%Y%m%d.%H%M"))))
  157. (save-match-data
  158. (when (string-match "-dirty\\'" v)
  159. (magit--put-face (1+ (match-beginning 0)) (length v) 'error v))
  160. (if (and v (string-match "\\`[0-9]" v))
  161. (concat " " v)
  162. v))))
  163. (defun magit-repolist-column-branch (_id)
  164. "Insert the current branch."
  165. (magit-get-current-branch))
  166. (defun magit-repolist-column-upstream (_id)
  167. "Insert the upstream branch of the current branch."
  168. (magit-get-upstream-branch))
  169. (defun magit-repolist-column-dirty (_id)
  170. "Insert a letter if there are uncommitted changes.
  171. Show N if there is at least one untracked file.
  172. Show U if there is at least one unstaged file.
  173. Show S if there is at least one staged file.
  174. Only one letter is shown, the first that applies."
  175. (cond ((magit-untracked-files) "N")
  176. ((magit-unstaged-files) "U")
  177. ((magit-staged-files) "S")))
  178. (defun magit-repolist-column-unpulled-from-upstream (_id)
  179. "Insert number of upstream commits not in the current branch."
  180. (--when-let (magit-get-upstream-branch)
  181. (let ((n (cadr (magit-rev-diff-count "HEAD" it))))
  182. (magit--propertize-face
  183. (number-to-string n) (if (> n 0) 'bold 'shadow)))))
  184. (defun magit-repolist-column-unpulled-from-pushremote (_id)
  185. "Insert number of commits in the push branch but not the current branch."
  186. (--when-let (magit-get-push-branch nil t)
  187. (let ((n (cadr (magit-rev-diff-count "HEAD" it))))
  188. (magit--propertize-face
  189. (number-to-string n) (if (> n 0) 'bold 'shadow)))))
  190. (defun magit-repolist-column-unpushed-to-upstream (_id)
  191. "Insert number of commits in the current branch but not its upstream."
  192. (--when-let (magit-get-upstream-branch)
  193. (let ((n (car (magit-rev-diff-count "HEAD" it))))
  194. (magit--propertize-face
  195. (number-to-string n) (if (> n 0) 'bold 'shadow)))))
  196. (defun magit-repolist-column-unpushed-to-pushremote (_id)
  197. "Insert number of commits in the current branch but not its push branch."
  198. (--when-let (magit-get-push-branch nil t)
  199. (let ((n (car (magit-rev-diff-count "HEAD" it))))
  200. (magit--propertize-face
  201. (number-to-string n) (if (> n 0) 'bold 'shadow)))))
  202. (defun magit-repolist-column-branches (_id)
  203. "Insert number of branches."
  204. (let ((n (length (magit-list-local-branches))))
  205. (magit--propertize-face (number-to-string n) (if (> n 1) 'bold 'shadow))))
  206. (defun magit-repolist-column-stashes (_id)
  207. "Insert number of stashes."
  208. (let ((n (length (magit-list-stashes))))
  209. (magit--propertize-face (number-to-string n) (if (> n 0) 'bold 'shadow))))
  210. ;;; Read Repository
  211. (defun magit-read-repository (&optional read-directory-name)
  212. "Read a Git repository in the minibuffer, with completion.
  213. The completion choices are the basenames of top-levels of
  214. repositories found in the directories specified by option
  215. `magit-repository-directories'. In case of name conflicts
  216. the basenames are prefixed with the name of the respective
  217. parent directories. The returned value is the actual path
  218. to the selected repository.
  219. If READ-DIRECTORY-NAME is non-nil or no repositories can be
  220. found based on the value of `magit-repository-directories',
  221. then read an arbitrary directory using `read-directory-name'
  222. instead."
  223. (if-let ((repos (and (not read-directory-name)
  224. magit-repository-directories
  225. (magit-repos-alist))))
  226. (let ((reply (magit-completing-read "Git repository" repos)))
  227. (file-name-as-directory
  228. (or (cdr (assoc reply repos))
  229. (if (file-directory-p reply)
  230. (expand-file-name reply)
  231. (user-error "Not a repository or a directory: %s" reply)))))
  232. (file-name-as-directory
  233. (read-directory-name "Git repository: "
  234. (or (magit-toplevel) default-directory)))))
  235. (defun magit-list-repos ()
  236. (cl-mapcan (pcase-lambda (`(,dir . ,depth))
  237. (magit-list-repos-1 dir depth))
  238. magit-repository-directories))
  239. (defun magit-list-repos-1 (directory depth)
  240. (cond ((file-readable-p (expand-file-name ".git" directory))
  241. (list (file-name-as-directory directory)))
  242. ((and (> depth 0) (magit-file-accessible-directory-p directory))
  243. (--mapcat (and (file-directory-p it)
  244. (magit-list-repos-1 it (1- depth)))
  245. (directory-files directory t
  246. directory-files-no-dot-files-regexp t)))))
  247. (defun magit-list-repos-uniquify (alist)
  248. (let (result (dict (make-hash-table :test 'equal)))
  249. (dolist (a (delete-dups alist))
  250. (puthash (car a) (cons (cdr a) (gethash (car a) dict)) dict))
  251. (maphash
  252. (lambda (key value)
  253. (if (= (length value) 1)
  254. (push (cons key (car value)) result)
  255. (setq result
  256. (append result
  257. (magit-list-repos-uniquify
  258. (--map (cons (concat
  259. key "\\"
  260. (file-name-nondirectory
  261. (directory-file-name
  262. (substring it 0 (- (1+ (length key)))))))
  263. it)
  264. value))))))
  265. dict)
  266. result))
  267. (defun magit-repos-alist ()
  268. (magit-list-repos-uniquify
  269. (--map (cons (file-name-nondirectory (directory-file-name it)) it)
  270. (magit-list-repos))))
  271. ;;; _
  272. (provide 'magit-repos)
  273. ;;; magit-repos.el ends here