Klimi's new dotfiles with stow.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

432 行
16 KiB

  1. ;;; magit-wip.el --- commit snapshots to work-in-progress refs -*- 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 defines tree global modes which automatically commit
  22. ;; snapshots to branch-specific work-in-progress refs before and after
  23. ;; making changes, and two commands which can be used to do so on
  24. ;; demand.
  25. ;;; Code:
  26. (eval-when-compile
  27. (require 'subr-x))
  28. (require 'magit-core)
  29. (require 'magit-log)
  30. ;;; Options
  31. (defgroup magit-wip nil
  32. "Automatically commit to work-in-progress refs."
  33. :link '(info-link "(magit)Wip Modes")
  34. :group 'magit-modes
  35. :group 'magit-essentials)
  36. (defgroup magit-wip-legacy nil
  37. "It is better to not use these modes individually."
  38. :link '(info-link "(magit)Legacy Wip Modes")
  39. :group 'magit-wip)
  40. (defcustom magit-wip-mode-lighter " Wip"
  41. "Lighter for Magit-Wip mode."
  42. :package-version '(magit . "2.90.0")
  43. :group 'magit-wip
  44. :type 'string)
  45. (defcustom magit-wip-after-save-local-mode-lighter ""
  46. "Lighter for Magit-Wip-After-Save-Local mode."
  47. :package-version '(magit . "2.1.0")
  48. :group 'magit-wip-legacy
  49. :type 'string)
  50. (defcustom magit-wip-after-apply-mode-lighter ""
  51. "Lighter for Magit-Wip-After-Apply mode."
  52. :package-version '(magit . "2.1.0")
  53. :group 'magit-wip-legacy
  54. :type 'string)
  55. (defcustom magit-wip-before-change-mode-lighter ""
  56. "Lighter for Magit-Wip-Before-Change mode."
  57. :package-version '(magit . "2.1.0")
  58. :group 'magit-wip-legacy
  59. :type 'string)
  60. (defcustom magit-wip-initial-backup-mode-lighter ""
  61. "Lighter for Magit-Wip-Initial Backup mode."
  62. :package-version '(magit . "2.1.0")
  63. :group 'magit-wip-legacy
  64. :type 'string)
  65. (defcustom magit-wip-merge-branch nil
  66. "Whether to merge the current branch into its wip ref.
  67. If non-nil and the current branch has new commits, then it is
  68. merged into the wip ref before creating a new wip commit. This
  69. makes it easier to inspect wip history and the wip commits are
  70. never garbage collected.
  71. If nil and the current branch has new commits, then the wip ref
  72. is reset to the tip of the branch before creating a new wip
  73. commit. With this setting wip commits are eventually garbage
  74. collected. This is currently the default."
  75. :package-version '(magit . "2.90.0")
  76. :group 'magit-wip
  77. :type 'boolean)
  78. (defcustom magit-wip-namespace "refs/wip/"
  79. "Namespace used for work-in-progress refs.
  80. The wip refs are named \"<namespace/>index/<branchref>\"
  81. and \"<namespace/>wtree/<branchref>\". When snapshots
  82. are created while the `HEAD' is detached then \"HEAD\"
  83. is used as `branch-ref'."
  84. :package-version '(magit . "2.1.0")
  85. :group 'magit-wip
  86. :type 'string)
  87. ;;; Modes
  88. (define-minor-mode magit-wip-mode
  89. "Save uncommitted changes to work-in-progress refs.
  90. Whenever appropriate (i.e. when dataloss would be a possibility
  91. otherwise) this mode causes uncommitted changes to be committed
  92. to dedicated work-in-progress refs.
  93. For historic reasons this mode is implemented on top of four
  94. other `magit-wip-*' modes, which can also be used individually,
  95. if you want finer control over when the wip refs are updated;
  96. but that is discouraged."
  97. :package-version '(magit . "2.90.0")
  98. :lighter magit-wip-mode-lighter
  99. :global t
  100. (let ((arg (if magit-wip-mode 1 -1)))
  101. (magit-wip-after-save-mode arg)
  102. (magit-wip-after-apply-mode arg)
  103. (magit-wip-before-change-mode arg)
  104. (magit-wip-initial-backup-mode arg)))
  105. (define-minor-mode magit-wip-after-save-local-mode
  106. "After saving, also commit to a worktree work-in-progress ref.
  107. After saving the current file-visiting buffer this mode also
  108. commits the changes to the worktree work-in-progress ref for
  109. the current branch.
  110. This mode should be enabled globally by turning on the globalized
  111. variant `magit-wip-after-save-mode'."
  112. :package-version '(magit . "2.1.0")
  113. :lighter magit-wip-after-save-local-mode-lighter
  114. (if magit-wip-after-save-local-mode
  115. (if (and buffer-file-name (magit-inside-worktree-p t))
  116. (add-hook 'after-save-hook 'magit-wip-commit-buffer-file t t)
  117. (setq magit-wip-after-save-local-mode nil)
  118. (user-error "Need a worktree and a file"))
  119. (remove-hook 'after-save-hook 'magit-wip-commit-buffer-file t)))
  120. (defun magit-wip-after-save-local-mode-turn-on ()
  121. (and buffer-file-name
  122. (magit-inside-worktree-p t)
  123. (magit-file-tracked-p buffer-file-name)
  124. (magit-wip-after-save-local-mode)))
  125. ;;;###autoload
  126. (define-globalized-minor-mode magit-wip-after-save-mode
  127. magit-wip-after-save-local-mode magit-wip-after-save-local-mode-turn-on
  128. :package-version '(magit . "2.1.0")
  129. :group 'magit-wip)
  130. (defun magit-wip-commit-buffer-file (&optional msg)
  131. "Commit visited file to a worktree work-in-progress ref.
  132. Also see `magit-wip-after-save-mode' which calls this function
  133. automatically whenever a buffer visiting a tracked file is saved."
  134. (interactive)
  135. (--when-let (magit-wip-get-ref)
  136. (magit-with-toplevel
  137. (let ((file (file-relative-name buffer-file-name)))
  138. (magit-wip-commit-worktree
  139. it (list file)
  140. (format (cond (msg)
  141. ((called-interactively-p 'any)
  142. "wip-save %s after save")
  143. (t
  144. "autosave %s after save"))
  145. file))))))
  146. ;;;###autoload
  147. (define-minor-mode magit-wip-after-apply-mode
  148. "Commit to work-in-progress refs.
  149. After applying a change using any \"apply variant\"
  150. command (apply, stage, unstage, discard, and reverse) commit the
  151. affected files to the current wip refs. For each branch there
  152. may be two wip refs; one contains snapshots of the files as found
  153. in the worktree and the other contains snapshots of the entries
  154. in the index."
  155. :package-version '(magit . "2.1.0")
  156. :group 'magit-wip
  157. :lighter magit-wip-after-apply-mode-lighter
  158. :global t)
  159. (defun magit-wip-commit-after-apply (&optional files msg)
  160. (when magit-wip-after-apply-mode
  161. (magit-wip-commit files msg)))
  162. ;;;###autoload
  163. (define-minor-mode magit-wip-before-change-mode
  164. "Commit to work-in-progress refs before certain destructive changes.
  165. Before invoking a revert command or an \"apply variant\"
  166. command (apply, stage, unstage, discard, and reverse) commit the
  167. affected tracked files to the current wip refs. For each branch
  168. there may be two wip refs; one contains snapshots of the files
  169. as found in the worktree and the other contains snapshots of the
  170. entries in the index.
  171. Only changes to files which could potentially be affected by the
  172. command which is about to be called are committed."
  173. :package-version '(magit . "2.1.0")
  174. :group 'magit-wip
  175. :lighter magit-wip-before-change-mode-lighter
  176. :global t)
  177. (defun magit-wip-commit-before-change (&optional files msg)
  178. (when magit-wip-before-change-mode
  179. (magit-with-toplevel
  180. (magit-wip-commit files msg))))
  181. (define-minor-mode magit-wip-initial-backup-mode
  182. "Before saving a buffer for the first time, commit to a wip ref."
  183. :package-version '(magit . "2.90.0")
  184. :group 'magit-wip
  185. :lighter magit-wip-initial-backup-mode-lighter
  186. :global t
  187. (if magit-wip-initial-backup-mode
  188. (add-hook 'before-save-hook 'magit-wip-commit-initial-backup)
  189. (remove-hook 'before-save-hook 'magit-wip-commit-initial-backup)))
  190. (defvar-local magit-wip-buffer-backed-up nil)
  191. (put 'magit-wip-buffer-backed-up 'permanent-local t)
  192. ;;;###autoload
  193. (defun magit-wip-commit-initial-backup ()
  194. "Before saving, commit current file to a worktree wip ref.
  195. The user has to add this function to `before-save-hook'.
  196. Commit the current state of the visited file before saving the
  197. current buffer to that file. This backs up the same version of
  198. the file as `backup-buffer' would, but stores the backup in the
  199. worktree wip ref, which is also used by the various Magit Wip
  200. modes, instead of in a backup file as `backup-buffer' would.
  201. This function ignores the variables that affect `backup-buffer'
  202. and can be used along-side that function, which is recommended
  203. because this function only backs up files that are tracked in
  204. a Git repository."
  205. (when (and (not magit-wip-buffer-backed-up)
  206. buffer-file-name
  207. (magit-inside-worktree-p t)
  208. (magit-file-tracked-p buffer-file-name))
  209. (let ((magit-save-repository-buffers nil))
  210. (magit-wip-commit-buffer-file "autosave %s before save"))
  211. (setq magit-wip-buffer-backed-up t)))
  212. ;;; Core
  213. (defun magit-wip-commit (&optional files msg)
  214. "Commit all tracked files to the work-in-progress refs.
  215. Interactively, commit all changes to all tracked files using
  216. a generic commit message. With a prefix-argument the commit
  217. message is read in the minibuffer.
  218. Non-interactively, only commit changes to FILES using MSG as
  219. commit message."
  220. (interactive (list nil (if current-prefix-arg
  221. (magit-read-string "Wip commit message")
  222. "wip-save tracked files")))
  223. (--when-let (magit-wip-get-ref)
  224. (magit-wip-commit-index it files msg)
  225. (magit-wip-commit-worktree it files msg)))
  226. (defun magit-wip-commit-index (ref files msg)
  227. (let* ((wipref (magit--wip-index-ref ref))
  228. (parent (magit-wip-get-parent ref wipref))
  229. (tree (magit-git-string "write-tree")))
  230. (magit-wip-update-wipref ref wipref tree parent files msg "index")))
  231. (defun magit-wip-commit-worktree (ref files msg)
  232. (let* ((wipref (magit--wip-wtree-ref ref))
  233. (parent (magit-wip-get-parent ref wipref))
  234. (tree (magit-with-temp-index parent "--reset"
  235. (if files
  236. (magit-call-git "add" "--" files)
  237. (magit-with-toplevel
  238. (magit-call-git "add" "-u" ".")))
  239. (magit-git-string "write-tree"))))
  240. (magit-wip-update-wipref ref wipref tree parent files msg "worktree")))
  241. (defun magit-wip-update-wipref (ref wipref tree parent files msg start-msg)
  242. (cond
  243. ((and (not (equal parent wipref))
  244. (or (not magit-wip-merge-branch)
  245. (not (magit-rev-verify wipref))))
  246. (setq start-msg (concat "start autosaving " start-msg))
  247. (magit-update-ref wipref start-msg
  248. (magit-git-string "commit-tree" "--no-gpg-sign"
  249. "-p" parent "-m" start-msg
  250. (concat parent "^{tree}")))
  251. (setq parent wipref))
  252. ((and magit-wip-merge-branch
  253. (or (not (magit-rev-ancestor-p ref wipref))
  254. (not (magit-rev-ancestor-p
  255. (concat (magit-git-string "log" "--format=%H"
  256. "-1" "--merges" wipref)
  257. "^2")
  258. ref))))
  259. (setq start-msg (format "merge %s into %s" ref start-msg))
  260. (magit-update-ref wipref start-msg
  261. (magit-git-string "commit-tree" "--no-gpg-sign"
  262. "-p" wipref "-p" ref
  263. "-m" start-msg
  264. (concat ref "^{tree}")))
  265. (setq parent wipref)))
  266. (when (magit-git-failure "diff-tree" "--quiet" parent tree "--" files)
  267. (unless (and msg (not (= (aref msg 0) ?\s)))
  268. (let ((len (length files)))
  269. (setq msg (concat
  270. (cond ((= len 0) "autosave tracked files")
  271. ((> len 1) (format "autosave %s files" len))
  272. (t (concat "autosave "
  273. (file-relative-name (car files)
  274. (magit-toplevel)))))
  275. msg))))
  276. (magit-update-ref wipref msg
  277. (magit-git-string "commit-tree" "--no-gpg-sign"
  278. "-p" parent "-m" msg tree))))
  279. (defun magit-wip-get-ref ()
  280. (let ((ref (or (magit-git-string "symbolic-ref" "HEAD") "HEAD")))
  281. (and (magit-rev-verify ref)
  282. ref)))
  283. (defun magit-wip-get-parent (ref wipref)
  284. (if (and (magit-rev-verify wipref)
  285. (equal (magit-git-string "merge-base" wipref ref)
  286. (magit-rev-verify ref)))
  287. wipref
  288. ref))
  289. (defun magit--wip-index-ref (&optional ref)
  290. (magit--wip-ref "index/" ref))
  291. (defun magit--wip-wtree-ref (&optional ref)
  292. (magit--wip-ref "wtree/" ref))
  293. (defun magit--wip-ref (namespace &optional ref)
  294. (concat magit-wip-namespace namespace
  295. (or (and ref (string-prefix-p "refs/" ref) ref)
  296. (when-let ((branch (or ref (magit-get-current-branch))))
  297. (concat "refs/heads/" branch))
  298. "HEAD")))
  299. (defun magit-wip-maybe-add-commit-hook ()
  300. (when (and magit-wip-merge-branch
  301. (magit-wip-any-enabled-p))
  302. (add-hook 'git-commit-post-finish-hook 'magit-wip-commit nil t)))
  303. (defun magit-wip-any-enabled-p ()
  304. (or magit-wip-mode
  305. magit-wip-after-save-local-mode
  306. magit-wip-after-save-mode
  307. magit-wip-after-apply-mode
  308. magit-wip-before-change-mode
  309. magit-wip-initial-backup-mode))
  310. ;;; Log
  311. (defun magit-wip-log-index (args files)
  312. "Show log for the index wip ref of the current branch."
  313. (interactive (magit-log-arguments))
  314. (magit-log-setup-buffer (list (magit--wip-index-ref)) args files))
  315. (defun magit-wip-log-worktree (args files)
  316. "Show log for the worktree wip ref of the current branch."
  317. (interactive (magit-log-arguments))
  318. (magit-log-setup-buffer (list (magit--wip-wtree-ref)) args files))
  319. (defun magit-wip-log-current (branch args files count)
  320. "Show log for the current branch and its wip refs.
  321. With a negative prefix argument only show the worktree wip ref.
  322. The absolute numeric value of the prefix argument controls how
  323. many \"branches\" of each wip ref are shown."
  324. (interactive
  325. (nconc (list (or (magit-get-current-branch) "HEAD"))
  326. (magit-log-arguments)
  327. (list (prefix-numeric-value current-prefix-arg))))
  328. (magit-wip-log branch args files count))
  329. (defun magit-wip-log (branch args files count)
  330. "Show log for a branch and its wip refs.
  331. With a negative prefix argument only show the worktree wip ref.
  332. The absolute numeric value of the prefix argument controls how
  333. many \"branches\" of each wip ref are shown."
  334. (interactive
  335. (nconc (list (magit-completing-read
  336. "Log branch and its wip refs"
  337. (-snoc (magit-list-local-branch-names) "HEAD")
  338. nil t nil 'magit-revision-history
  339. (or (magit-branch-at-point)
  340. (magit-get-current-branch)
  341. "HEAD")))
  342. (magit-log-arguments)
  343. (list (prefix-numeric-value current-prefix-arg))))
  344. (magit-log-setup-buffer (nconc (list branch)
  345. (magit-wip-log-get-tips
  346. (magit--wip-wtree-ref branch)
  347. (abs count))
  348. (and (>= count 0)
  349. (magit-wip-log-get-tips
  350. (magit--wip-index-ref branch)
  351. (abs count))))
  352. args files))
  353. (defun magit-wip-log-get-tips (wipref count)
  354. (when-let ((reflog (magit-git-lines "reflog" wipref)))
  355. (let (tips)
  356. (while (and reflog (> count 1))
  357. (setq reflog (cl-member "^[^ ]+ [^:]+: restart autosaving"
  358. reflog :test #'string-match-p))
  359. (when (and (cadr reflog)
  360. (string-match "^[^ ]+ \\([^:]+\\)" (cadr reflog)))
  361. (push (match-string 1 (cadr reflog)) tips))
  362. (setq reflog (cddr reflog))
  363. (cl-decf count))
  364. (cons wipref (nreverse tips)))))
  365. ;;; _
  366. (provide 'magit-wip)
  367. ;;; magit-wip.el ends here