mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 c655eea759be1db69c5e6b45c228139d8390122a
commit | author | age
5cb5f7 1 ;;; magit-notes.el --- notes support  -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2010-2018  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 ;; Magit is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15 ;;
16 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19 ;; License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with Magit.  If not, see http://www.gnu.org/licenses.
23
24 ;;; Commentary:
25
26 ;; This library implements support for `git-notes'.
27
28 ;;; Code:
29
30 (require 'magit)
31
32 ;;; Popup
33
34 ;;;###autoload (autoload 'magit-notes-popup "magit" nil t)
35 (magit-define-popup magit-notes-popup
36   "Popup console for notes commands."
37   :man-page "git-notes"
38   :variables '("Configure local settings"
39                (?c "core.notesRef"
40                    magit-set-core.notesRef
41                    magit-format-core.notesRef)
42                (?d "notes.displayRef"
43                    magit-set-notes.displayRef
44                    magit-format-notes.displayRef)
45                "Configure global settings"
46                (?C "core.notesRef"
47                    magit-set-global-core.notesRef
48                    magit-format-global-core.notesRef)
49                (?D "notes.displayRef"
50                    magit-set-global-notes.displayRef
51                    magit-format-global-notes.displayRef))
52   :switches '("Switch for prune"
53               (?n "Dry run"          "--dry-run"))
54   :options  '("Option for edit and remove"
55               (?r "Manipulate ref"   "--ref=" magit-notes-popup-read-ref)
56               "Option for merge"
57               (?s "Merge strategy"   "--strategy="))
58   :actions  '((?T "Edit"             magit-notes-edit)
59               (?r "Remove"           magit-notes-remove)
60               (?m "Merge"            magit-notes-merge)
61               (?p "Prune"            magit-notes-prune))
62   :sequence-actions '((?c "Commit merge" magit-notes-merge-commit)
63                       (?a "Abort merge"  magit-notes-merge-abort))
64   :sequence-predicate 'magit-notes-merging-p
65   :default-action 'magit-notes-edit)
66
67 (defun magit-notes-merging-p ()
68   (let ((dir (magit-git-dir "NOTES_MERGE_WORKTREE")))
69     (and (file-directory-p dir)
70          (directory-files dir nil "^[^.]"))))
71
72 (defun magit-format-core.notesRef ()
73   (magit--format-popup-variable:value "core.notesRef" 22))
74
75 (defun magit-format-notes.displayRef ()
76   (magit--format-popup-variable:values "notes.displayRef" 22))
77
78 (defun magit-format-global-core.notesRef ()
79   (magit--format-popup-variable:value "core.notesRef" 22 t))
80
81 (defun magit-format-global-notes.displayRef ()
82   (magit--format-popup-variable:values "notes.displayRef" 22 t))
83
84 ;;; Commands
85
86 (defun magit-notes-edit (commit &optional ref)
87   "Edit the note attached to COMMIT.
88 REF is the notes ref used to store the notes.
89
90 Interactively or when optional REF is nil use the value of Git
91 variable `core.notesRef' or \"refs/notes/commits\" if that is
92 undefined."
93   (interactive (magit-notes-read-args "Edit notes"))
94   (magit-run-git-with-editor "notes" (and ref (concat "--ref=" ref))
95                              "edit" commit))
96
97 (defun magit-notes-remove (commit &optional ref)
98   "Remove the note attached to COMMIT.
99 REF is the notes ref from which the note is removed.
100
101 Interactively or when optional REF is nil use the value of Git
102 variable `core.notesRef' or \"refs/notes/commits\" if that is
103 undefined."
104   (interactive (magit-notes-read-args "Remove notes"))
105   (magit-run-git-with-editor "notes" (and ref (concat "--ref=" ref))
106                              "remove" commit))
107
108 (defun magit-notes-merge (ref)
109   "Merge the notes ref REF into the current notes ref.
110
111 The current notes ref is the value of Git variable
112 `core.notesRef' or \"refs/notes/commits\" if that is undefined.
113
114 When there are conflicts, then they have to be resolved in the
115 temporary worktree \".git/NOTES_MERGE_WORKTREE\".  When
116 done use `magit-notes-merge-commit' to finish.  To abort
117 use `magit-notes-merge-abort'."
118   (interactive (list (magit-read-string-ns "Merge reference")))
119   (magit-run-git-with-editor "notes" "merge" ref))
120
121 (defun magit-notes-merge-commit ()
122   "Commit the current notes ref merge.
123 Also see `magit-notes-merge'."
124   (interactive)
125   (magit-run-git-with-editor "notes" "merge" "--commit"))
126
127 (defun magit-notes-merge-abort ()
128   "Abort the current notes ref merge.
129 Also see `magit-notes-merge'."
130   (interactive)
131   (magit-run-git-with-editor "notes" "merge" "--abort"))
132
133 (defun magit-notes-prune (&optional dry-run)
134   "Remove notes about unreachable commits."
135   (interactive (list (and (member "--dry-run" (magit-notes-arguments)) t)))
136   (when dry-run
137     (magit-process-buffer))
138   (magit-run-git-with-editor "notes" "prune" (and dry-run "--dry-run")))
139
140 (defun magit-set-core.notesRef (ref)
141   "Set the local value of `core.notesRef' to REF."
142   (interactive (list (magit-notes-read-ref "Set local core.notesRef")))
143   (magit-set ref "core.notesRef")
144   (magit-with-pre-popup-buffer
145     (magit-refresh)))
146
147 (defun magit-set-global-core.notesRef (ref)
148   "Set the global value of `core.notesRef' to REF."
149   (interactive (list (magit-notes-read-ref "Set global core.notesRef")))
150   (magit-set ref "--global" "core.notesRef")
151   (magit-with-pre-popup-buffer
152     (magit-refresh)))
153
154 (defun magit-set-notes.displayRef (refs)
155   "Set the local values of `notes.displayRef' to REFS."
156   (interactive (list (magit-notes-read-refs "Set local notes.displayRef")))
157   (magit-set-all refs "notes.displayRef")
158   (magit-with-pre-popup-buffer
159     (magit-refresh)))
160
161 (defun magit-set-global-notes.displayRef (refs)
162   "Set the global values of `notes.displayRef' to REFS."
163   (interactive (list (magit-notes-read-refs "Set global notes.displayRef")))
164   (magit-set-all refs "--global" "notes.displayRef")
165   (magit-with-pre-popup-buffer
166     (magit-refresh)))
167
168 (defun magit-notes-read-ref (prompt)
169   (--when-let (magit-completing-read
170                prompt (magit-list-notes-refnames) nil nil
171                (--when-let (magit-get "core.notesRef")
172                  (if (string-prefix-p "refs/notes/" it)
173                      (substring it 11)
174                    it)))
175     (if (string-prefix-p "refs/" it)
176         it
177       (concat "refs/notes/" it))))
178
179 (defun magit-notes-read-refs (prompt)
180   (mapcar (lambda (ref)
181             (if (string-prefix-p "refs/" ref)
182                 ref
183               (concat "refs/notes/" ref)))
184           (completing-read-multiple
185            (concat prompt ": ")
186            (magit-list-notes-refnames) nil nil
187            (mapconcat (lambda (ref)
188                         (if (string-prefix-p "refs/notes/" ref)
189                             (substring ref 11)
190                           ref))
191                       (magit-get-all "notes.displayRef")
192                       ","))))
193
194 (defun magit-notes-read-args (prompt)
195  (list (magit-read-branch-or-commit prompt (magit-stash-at-point))
196        (--when-let (--first (string-match "^--ref=\\(.+\\)" it)
197                             (magit-notes-arguments))
198          (match-string 1 it))))
199
200 ;;; _
201 (provide 'magit-notes)
202 ;;; magit-notes.el ends here