Commit | Line | Data |
---|---|---|
3a59f3db KH |
1 | ;; stgit.el: An emacs mode for StGit |
2 | ;; | |
3 | ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se> | |
4 | ;; | |
5 | ;; To install: put this file on the load-path and place the following | |
6 | ;; in your .emacs file: | |
7 | ;; | |
8 | ;; (require 'stgit) | |
9 | ;; | |
10 | ;; To start: `M-x stgit' | |
11 | ||
0f076fe6 | 12 | (require 'git nil t) |
50d88c67 | 13 | (require 'cl) |
98230edd | 14 | (require 'ewoc) |
0f076fe6 | 15 | |
56d81fe5 | 16 | (defun stgit (dir) |
a53347d9 | 17 | "Manage StGit patches for the tree in DIR." |
56d81fe5 | 18 | (interactive "DDirectory: \n") |
52144ce5 | 19 | (switch-to-stgit-buffer (git-get-top-dir dir)) |
1f0bf00f | 20 | (stgit-reload)) |
56d81fe5 | 21 | |
074a4fb0 GH |
22 | (unless (fboundp 'git-get-top-dir) |
23 | (defun git-get-top-dir (dir) | |
24 | "Retrieve the top-level directory of a git tree." | |
25 | (let ((cdup (with-output-to-string | |
26 | (with-current-buffer standard-output | |
27 | (cd dir) | |
28 | (unless (eq 0 (call-process "git" nil t nil | |
29 | "rev-parse" "--show-cdup")) | |
df283a8b | 30 | (error "Cannot find top-level git tree for %s" dir)))))) |
074a4fb0 GH |
31 | (expand-file-name (concat (file-name-as-directory dir) |
32 | (car (split-string cdup "\n"))))))) | |
33 | ||
34 | (defun stgit-refresh-git-status (&optional dir) | |
35 | "If it exists, refresh the `git-status' buffer belonging to | |
36 | directory DIR or `default-directory'" | |
37 | (when (and (fboundp 'git-find-status-buffer) | |
38 | (fboundp 'git-refresh-status)) | |
39 | (let* ((top-dir (git-get-top-dir (or dir default-directory))) | |
40 | (git-status-buffer (and top-dir (git-find-status-buffer top-dir)))) | |
41 | (when git-status-buffer | |
42 | (with-current-buffer git-status-buffer | |
43 | (git-refresh-status)))))) | |
52144ce5 | 44 | |
b894e680 DK |
45 | (defun stgit-find-buffer (dir) |
46 | "Return the buffer displaying StGit patches for DIR, or nil if none." | |
56d81fe5 DK |
47 | (setq dir (file-name-as-directory dir)) |
48 | (let ((buffers (buffer-list))) | |
49 | (while (and buffers | |
50 | (not (with-current-buffer (car buffers) | |
51 | (and (eq major-mode 'stgit-mode) | |
52 | (string= default-directory dir))))) | |
53 | (setq buffers (cdr buffers))) | |
b894e680 DK |
54 | (and buffers (car buffers)))) |
55 | ||
56 | (defun switch-to-stgit-buffer (dir) | |
57 | "Switch to a (possibly new) buffer displaying StGit patches for DIR." | |
58 | (setq dir (file-name-as-directory dir)) | |
59 | (let ((buffer (stgit-find-buffer dir))) | |
60 | (switch-to-buffer (or buffer | |
61 | (create-stgit-buffer dir))))) | |
62 | ||
2c862b07 | 63 | (defstruct (stgit-patch) |
3164eec6 | 64 | status name desc empty files-ewoc) |
56d81fe5 | 65 | |
98230edd DK |
66 | (defun stgit-patch-pp (patch) |
67 | (let ((status (stgit-patch-status patch)) | |
68 | (start (point)) | |
69 | (name (stgit-patch-name patch))) | |
b894e680 DK |
70 | (case name |
71 | (:index (insert (propertize " Index" 'face 'italic))) | |
72 | (:work (insert (propertize " Work tree" 'face 'italic))) | |
73 | (t (insert (case status | |
74 | ('applied "+") | |
75 | ('top ">") | |
76 | ('unapplied "-")) | |
77 | (if (memq name stgit-marked-patches) | |
78 | "*" " ") | |
79 | (propertize (format "%-30s" | |
80 | (symbol-name name)) | |
81 | 'face (case status | |
82 | ('applied 'stgit-applied-patch-face) | |
83 | ('top 'stgit-top-patch-face) | |
84 | ('unapplied 'stgit-unapplied-patch-face) | |
85 | ('index nil) | |
86 | ('work nil))) | |
87 | " " | |
88 | (if (stgit-patch-empty patch) "(empty) " "") | |
89 | (propertize (or (stgit-patch-desc patch) "") | |
90 | 'face 'stgit-description-face)))) | |
f9b82d36 | 91 | (put-text-property start (point) 'entry-type 'patch) |
98230edd | 92 | (when (memq name stgit-expanded-patches) |
0de6881a | 93 | (stgit-insert-patch-files patch)) |
98230edd DK |
94 | (put-text-property start (point) 'patch-data patch))) |
95 | ||
56d81fe5 DK |
96 | (defun create-stgit-buffer (dir) |
97 | "Create a buffer for showing StGit patches. | |
98 | Argument DIR is the repository path." | |
99 | (let ((buf (create-file-buffer (concat dir "*stgit*"))) | |
100 | (inhibit-read-only t)) | |
101 | (with-current-buffer buf | |
102 | (setq default-directory dir) | |
103 | (stgit-mode) | |
98230edd DK |
104 | (set (make-local-variable 'stgit-ewoc) |
105 | (ewoc-create #'stgit-patch-pp "Branch:\n" "--")) | |
56d81fe5 DK |
106 | (setq buffer-read-only t)) |
107 | buf)) | |
108 | ||
109 | (defmacro stgit-capture-output (name &rest body) | |
e558a4ab GH |
110 | "Capture StGit output and, if there was any output, show it in a window |
111 | at the end. | |
112 | Returns nil if there was no output." | |
94baef5a DK |
113 | (declare (debug ([&or stringp null] body)) |
114 | (indent 1)) | |
34afb86c DK |
115 | `(let ((output-buf (get-buffer-create ,(or name "*StGit output*"))) |
116 | (stgit-dir default-directory) | |
117 | (inhibit-read-only t)) | |
56d81fe5 | 118 | (with-current-buffer output-buf |
34afb86c DK |
119 | (erase-buffer) |
120 | (setq default-directory stgit-dir) | |
121 | (setq buffer-read-only t)) | |
56d81fe5 DK |
122 | (let ((standard-output output-buf)) |
123 | ,@body) | |
34afb86c DK |
124 | (with-current-buffer output-buf |
125 | (set-buffer-modified-p nil) | |
126 | (setq buffer-read-only t) | |
127 | (if (< (point-min) (point-max)) | |
128 | (display-buffer output-buf t))))) | |
56d81fe5 | 129 | |
d51722b7 GH |
130 | (defun stgit-make-run-args (args) |
131 | "Return a copy of ARGS with its elements converted to strings." | |
132 | (mapcar (lambda (x) | |
133 | ;; don't use (format "%s" ...) to limit type errors | |
134 | (cond ((stringp x) x) | |
135 | ((integerp x) (number-to-string x)) | |
136 | ((symbolp x) (symbol-name x)) | |
137 | (t | |
138 | (error "Bad element in stgit-make-run-args args: %S" x)))) | |
139 | args)) | |
140 | ||
9aecd505 | 141 | (defun stgit-run-silent (&rest args) |
d51722b7 | 142 | (setq args (stgit-make-run-args args)) |
56d81fe5 DK |
143 | (apply 'call-process "stg" nil standard-output nil args)) |
144 | ||
9aecd505 | 145 | (defun stgit-run (&rest args) |
d51722b7 | 146 | (setq args (stgit-make-run-args args)) |
9aecd505 DK |
147 | (let ((msgcmd (mapconcat #'identity args " "))) |
148 | (message "Running stg %s..." msgcmd) | |
149 | (apply 'call-process "stg" nil standard-output nil args) | |
150 | (message "Running stg %s...done" msgcmd))) | |
151 | ||
378a003d | 152 | (defun stgit-run-git (&rest args) |
d51722b7 | 153 | (setq args (stgit-make-run-args args)) |
378a003d GH |
154 | (let ((msgcmd (mapconcat #'identity args " "))) |
155 | (message "Running git %s..." msgcmd) | |
156 | (apply 'call-process "git" nil standard-output nil args) | |
157 | (message "Running git %s...done" msgcmd))) | |
158 | ||
1f60181a | 159 | (defun stgit-run-git-silent (&rest args) |
d51722b7 | 160 | (setq args (stgit-make-run-args args)) |
1f60181a GH |
161 | (apply 'call-process "git" nil standard-output nil args)) |
162 | ||
b894e680 DK |
163 | (defun stgit-index-empty-p () |
164 | "Returns non-nil if the index contains no changes from HEAD." | |
165 | (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD"))) | |
166 | ||
98230edd DK |
167 | (defun stgit-run-series (ewoc) |
168 | (let ((first-line t)) | |
169 | (with-temp-buffer | |
170 | (let ((exit-status (stgit-run-silent "series" "--description" "--empty"))) | |
171 | (goto-char (point-min)) | |
172 | (if (not (zerop exit-status)) | |
173 | (cond ((looking-at "stg series: \\(.*\\)") | |
174 | (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc)) | |
175 | "-- not initialized (run M-x stgit-init)")) | |
176 | ((looking-at ".*") | |
177 | (error "Error running stg: %s" | |
178 | (match-string 0)))) | |
179 | (while (not (eobp)) | |
180 | (unless (looking-at | |
181 | "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)") | |
182 | (error "Syntax error in output from stg series")) | |
183 | (let* ((state-str (match-string 2)) | |
184 | (state (cond ((string= state-str ">") 'top) | |
185 | ((string= state-str "+") 'applied) | |
186 | ((string= state-str "-") 'unapplied)))) | |
187 | (ewoc-enter-last ewoc | |
188 | (make-stgit-patch | |
189 | :status state | |
190 | :name (intern (match-string 4)) | |
191 | :desc (match-string 5) | |
192 | :empty (string= (match-string 1) "0")))) | |
193 | (setq first-line nil) | |
b894e680 DK |
194 | (forward-line 1)) |
195 | (ewoc-enter-last ewoc | |
196 | (make-stgit-patch | |
197 | :status 'index | |
198 | :name :index | |
199 | :desc nil | |
200 | :empty nil)) | |
201 | (ewoc-enter-last ewoc | |
202 | (make-stgit-patch | |
203 | :status 'work | |
204 | :name :work | |
205 | :desc nil | |
206 | :empty nil))))))) | |
98230edd DK |
207 | |
208 | ||
1f0bf00f | 209 | (defun stgit-reload () |
a53347d9 | 210 | "Update the contents of the StGit buffer." |
56d81fe5 DK |
211 | (interactive) |
212 | (let ((inhibit-read-only t) | |
213 | (curline (line-number-at-pos)) | |
2c862b07 | 214 | (curpatch (stgit-patch-name-at-point))) |
98230edd DK |
215 | (ewoc-filter stgit-ewoc #'(lambda (x) nil)) |
216 | (ewoc-set-hf stgit-ewoc | |
217 | (concat "Branch: " | |
218 | (propertize | |
219 | (with-temp-buffer | |
220 | (stgit-run-silent "branch") | |
221 | (buffer-substring (point-min) (1- (point-max)))) | |
222 | 'face 'bold) | |
223 | "\n") | |
224 | "--") | |
225 | (stgit-run-series stgit-ewoc) | |
56d81fe5 DK |
226 | (if curpatch |
227 | (stgit-goto-patch curpatch) | |
074a4fb0 GH |
228 | (goto-line curline))) |
229 | (stgit-refresh-git-status)) | |
56d81fe5 | 230 | |
8f40753a GH |
231 | (defgroup stgit nil |
232 | "A user interface for the StGit patch maintenance tool." | |
233 | :group 'tools) | |
234 | ||
07f464e0 DK |
235 | (defface stgit-description-face |
236 | '((((background dark)) (:foreground "tan")) | |
237 | (((background light)) (:foreground "dark red"))) | |
8f40753a GH |
238 | "The face used for StGit descriptions" |
239 | :group 'stgit) | |
07f464e0 DK |
240 | |
241 | (defface stgit-top-patch-face | |
242 | '((((background dark)) (:weight bold :foreground "yellow")) | |
243 | (((background light)) (:weight bold :foreground "purple")) | |
244 | (t (:weight bold))) | |
8f40753a GH |
245 | "The face used for the top patch names" |
246 | :group 'stgit) | |
07f464e0 DK |
247 | |
248 | (defface stgit-applied-patch-face | |
249 | '((((background dark)) (:foreground "light yellow")) | |
250 | (((background light)) (:foreground "purple")) | |
251 | (t ())) | |
8f40753a GH |
252 | "The face used for applied patch names" |
253 | :group 'stgit) | |
07f464e0 DK |
254 | |
255 | (defface stgit-unapplied-patch-face | |
256 | '((((background dark)) (:foreground "gray80")) | |
257 | (((background light)) (:foreground "orchid")) | |
258 | (t ())) | |
8f40753a GH |
259 | "The face used for unapplied patch names" |
260 | :group 'stgit) | |
07f464e0 | 261 | |
1f60181a GH |
262 | (defface stgit-modified-file-face |
263 | '((((class color) (background light)) (:foreground "purple")) | |
264 | (((class color) (background dark)) (:foreground "salmon"))) | |
265 | "StGit mode face used for modified file status" | |
266 | :group 'stgit) | |
267 | ||
268 | (defface stgit-unmerged-file-face | |
269 | '((((class color) (background light)) (:foreground "red" :bold t)) | |
270 | (((class color) (background dark)) (:foreground "red" :bold t))) | |
271 | "StGit mode face used for unmerged file status" | |
272 | :group 'stgit) | |
273 | ||
274 | (defface stgit-unknown-file-face | |
275 | '((((class color) (background light)) (:foreground "goldenrod" :bold t)) | |
276 | (((class color) (background dark)) (:foreground "goldenrod" :bold t))) | |
277 | "StGit mode face used for unknown file status" | |
278 | :group 'stgit) | |
279 | ||
a6d9a852 GH |
280 | (defface stgit-file-permission-face |
281 | '((((class color) (background light)) (:foreground "green" :bold t)) | |
282 | (((class color) (background dark)) (:foreground "green" :bold t))) | |
283 | "StGit mode face used for permission changes." | |
284 | :group 'stgit) | |
285 | ||
1f60181a GH |
286 | (defcustom stgit-expand-find-copies-harder |
287 | nil | |
288 | "Try harder to find copied files when listing patches. | |
289 | ||
290 | When not nil, runs git diff-tree with the --find-copies-harder | |
291 | flag, which reduces performance." | |
292 | :type 'boolean | |
293 | :group 'stgit) | |
294 | ||
295 | (defconst stgit-file-status-code-strings | |
296 | (mapcar (lambda (arg) | |
297 | (cons (car arg) | |
a6d9a852 GH |
298 | (propertize (cadr arg) 'face (car (cddr arg))))) |
299 | '((add "Added" stgit-modified-file-face) | |
300 | (copy "Copied" stgit-modified-file-face) | |
301 | (delete "Deleted" stgit-modified-file-face) | |
302 | (modify "Modified" stgit-modified-file-face) | |
303 | (rename "Renamed" stgit-modified-file-face) | |
304 | (mode-change "Mode change" stgit-modified-file-face) | |
305 | (unmerged "Unmerged" stgit-unmerged-file-face) | |
306 | (unknown "Unknown" stgit-unknown-file-face))) | |
1f60181a GH |
307 | "Alist of code symbols to description strings") |
308 | ||
3164eec6 DK |
309 | (defun stgit-file-status-code-as-string (file) |
310 | "Return stgit status code for FILE as a string" | |
311 | (let* ((code (assq (stgit-file-status file) | |
312 | stgit-file-status-code-strings)) | |
313 | (score (stgit-file-cr-score file))) | |
314 | (when code | |
a6d9a852 | 315 | (format "%-11s " |
3164eec6 DK |
316 | (if (and score (/= score 100)) |
317 | (format "%s %s" (cdr code) | |
318 | (propertize (format "%d%%" score) | |
a6d9a852 | 319 | 'face 'stgit-description-face)) |
3164eec6 | 320 | (cdr code)))))) |
1f60181a | 321 | |
a6d9a852 | 322 | (defun stgit-file-status-code (str &optional score) |
1f60181a GH |
323 | "Return stgit status code from git status string" |
324 | (let ((code (assoc str '(("A" . add) | |
325 | ("C" . copy) | |
326 | ("D" . delete) | |
327 | ("M" . modify) | |
328 | ("R" . rename) | |
329 | ("T" . mode-change) | |
330 | ("U" . unmerged) | |
331 | ("X" . unknown))))) | |
a6d9a852 GH |
332 | (setq code (if code (cdr code) 'unknown)) |
333 | (when (stringp score) | |
334 | (if (> (length score) 0) | |
335 | (setq score (string-to-number score)) | |
336 | (setq score nil))) | |
337 | (if score (cons code score) code))) | |
338 | ||
339 | (defconst stgit-file-type-strings | |
340 | '((#o100 . "file") | |
341 | (#o120 . "symlink") | |
342 | (#o160 . "subproject")) | |
343 | "Alist of names of file types") | |
344 | ||
345 | (defun stgit-file-type-string (type) | |
47271f41 GH |
346 | "Return string describing file type TYPE (the high bits of file permission). |
347 | Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'." | |
a6d9a852 GH |
348 | (let ((type-str (assoc type stgit-file-type-strings))) |
349 | (or (and type-str (cdr type-str)) | |
350 | (format "unknown type %o" type)))) | |
351 | ||
352 | (defun stgit-file-type-change-string (old-perm new-perm) | |
47271f41 GH |
353 | "Return string describing file type change from OLD-PERM to NEW-PERM. |
354 | Cf. `stgit-file-type-string'." | |
a6d9a852 GH |
355 | (let ((old-type (lsh old-perm -9)) |
356 | (new-type (lsh new-perm -9))) | |
357 | (cond ((= old-type new-type) "") | |
358 | ((zerop new-type) "") | |
359 | ((zerop old-type) | |
360 | (if (= new-type #o100) | |
361 | "" | |
362 | (format " (%s)" (stgit-file-type-string new-type)))) | |
363 | (t (format " (%s -> %s)" | |
364 | (stgit-file-type-string old-type) | |
365 | (stgit-file-type-string new-type)))))) | |
366 | ||
367 | (defun stgit-file-mode-change-string (old-perm new-perm) | |
47271f41 GH |
368 | "Return string describing file mode change from OLD-PERM to NEW-PERM. |
369 | Cf. `stgit-file-type-change-string'." | |
a6d9a852 GH |
370 | (setq old-perm (logand old-perm #o777) |
371 | new-perm (logand new-perm #o777)) | |
372 | (if (or (= old-perm new-perm) | |
373 | (zerop old-perm) | |
374 | (zerop new-perm)) | |
375 | "" | |
376 | (let* ((modified (logxor old-perm new-perm)) | |
377 | (not-x-modified (logand (logxor old-perm new-perm) #o666))) | |
378 | (cond ((zerop modified) "") | |
379 | ((and (zerop not-x-modified) | |
380 | (or (and (eq #o111 (logand old-perm #o111)) | |
381 | (propertize "-x" 'face 'stgit-file-permission-face)) | |
382 | (and (eq #o111 (logand new-perm #o111)) | |
383 | (propertize "+x" 'face | |
384 | 'stgit-file-permission-face))))) | |
385 | (t (concat (propertize (format "%o" old-perm) | |
386 | 'face 'stgit-file-permission-face) | |
387 | (propertize " -> " | |
388 | 'face 'stgit-description-face) | |
389 | (propertize (format "%o" new-perm) | |
390 | 'face 'stgit-file-permission-face))))))) | |
1f60181a | 391 | |
0de6881a DK |
392 | (defstruct (stgit-file) |
393 | old-perm new-perm copy-or-rename cr-score cr-from cr-to status file) | |
394 | ||
3164eec6 | 395 | (defun stgit-file-pp (file) |
0de6881a DK |
396 | (let ((status (stgit-file-status file)) |
397 | (name (if (stgit-file-copy-or-rename file) | |
398 | (concat (stgit-file-cr-from file) | |
399 | (propertize " -> " | |
400 | 'face 'stgit-description-face) | |
401 | (stgit-file-cr-to file)) | |
402 | (stgit-file-file file))) | |
403 | (mode-change (stgit-file-mode-change-string | |
404 | (stgit-file-old-perm file) | |
405 | (stgit-file-new-perm file))) | |
406 | (start (point))) | |
3164eec6 DK |
407 | (insert (format " %-12s%1s%s%s\n" |
408 | (stgit-file-status-code-as-string file) | |
98230edd | 409 | mode-change |
0de6881a DK |
410 | name |
411 | (propertize (stgit-file-type-change-string | |
412 | (stgit-file-old-perm file) | |
413 | (stgit-file-new-perm file)) | |
98230edd | 414 | 'face 'stgit-description-face))) |
0de6881a | 415 | (add-text-properties start (point) |
3164eec6 DK |
416 | (list 'entry-type 'file |
417 | 'file-data file)))) | |
0de6881a DK |
418 | |
419 | (defun stgit-insert-patch-files (patch) | |
420 | "Expand (show modification of) the patch with name PATCHSYM (a | |
421 | symbol) after the line at point. | |
422 | `stgit-expand-find-copies-harder' controls how hard to try to | |
423 | find copied files." | |
3164eec6 DK |
424 | (insert "\n") |
425 | (let* ((patchsym (stgit-patch-name patch)) | |
426 | (end (progn (insert "#") (prog1 (point-marker) (forward-char -1)))) | |
b894e680 DK |
427 | (args (list "-z" (if stgit-expand-find-copies-harder |
428 | "--find-copies-harder" | |
429 | "-C"))) | |
3164eec6 DK |
430 | (ewoc (ewoc-create #'stgit-file-pp nil nil t))) |
431 | (setf (stgit-patch-files-ewoc patch) ewoc) | |
0de6881a | 432 | (with-temp-buffer |
b894e680 DK |
433 | (apply 'stgit-run-git |
434 | (cond ((eq patchsym :work) | |
435 | `("diff-files" ,@args)) | |
436 | ((eq patchsym :index) | |
437 | `("diff-index" ,@args "--cached" "HEAD")) | |
438 | (t | |
439 | `("diff-tree" ,@args "-r" ,(stgit-id patchsym))))) | |
0de6881a | 440 | (goto-char (point-min)) |
b894e680 DK |
441 | (unless (or (eobp) (memq patchsym '(:work :index))) |
442 | (forward-char 41)) | |
0de6881a DK |
443 | (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ") |
444 | (let ((old-perm (string-to-number (match-string 1) 8)) | |
445 | (new-perm (string-to-number (match-string 2) 8))) | |
446 | (goto-char (match-end 0)) | |
447 | (let ((file | |
448 | (cond ((looking-at | |
449 | "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0") | |
450 | (make-stgit-file | |
451 | :old-perm old-perm | |
452 | :new-perm new-perm | |
453 | :copy-or-rename t | |
454 | :cr-score (string-to-number (match-string 2)) | |
455 | :cr-from (match-string 3) | |
456 | :cr-to (match-string 4) | |
457 | :status (stgit-file-status-code (match-string 1)) | |
458 | :file (match-string 3))) | |
459 | ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0") | |
460 | (make-stgit-file | |
461 | :old-perm old-perm | |
462 | :new-perm new-perm | |
463 | :copy-or-rename nil | |
464 | :cr-score nil | |
465 | :cr-from nil | |
466 | :cr-to nil | |
467 | :status (stgit-file-status-code (match-string 1)) | |
468 | :file (match-string 2)))))) | |
3164eec6 DK |
469 | (ewoc-enter-last ewoc file)) |
470 | (goto-char (match-end 0)))) | |
471 | (unless (ewoc-nth ewoc 0) | |
472 | (ewoc-set-hf ewoc "" (propertize " <no files>\n" | |
473 | 'face 'stgit-description-face)))) | |
474 | (goto-char end) | |
475 | (delete-char -2))) | |
07f464e0 | 476 | |
acc5652f | 477 | (defun stgit-select-file () |
3164eec6 DK |
478 | (let ((filename (expand-file-name |
479 | (stgit-file-file (stgit-patched-file-at-point))))) | |
0de6881a DK |
480 | (unless (file-exists-p filename) |
481 | (error "File does not exist")) | |
482 | (find-file filename))) | |
acc5652f | 483 | |
50d88c67 | 484 | (defun stgit-select-patch () |
98230edd DK |
485 | (let ((patchname (stgit-patch-name-at-point))) |
486 | (if (memq patchname stgit-expanded-patches) | |
487 | (setq stgit-expanded-patches (delq patchname stgit-expanded-patches)) | |
488 | (setq stgit-expanded-patches (cons patchname stgit-expanded-patches))) | |
489 | (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc))) | |
490 | (move-to-column (stgit-goal-column))) | |
acc5652f | 491 | |
378a003d GH |
492 | (defun stgit-select () |
493 | "Expand or collapse the current entry" | |
494 | (interactive) | |
50d88c67 DK |
495 | (case (get-text-property (point) 'entry-type) |
496 | ('patch | |
497 | (stgit-select-patch)) | |
498 | ('file | |
499 | (stgit-select-file)) | |
500 | (t | |
501 | (error "No patch or file on line")))) | |
378a003d GH |
502 | |
503 | (defun stgit-find-file-other-window () | |
504 | "Open file at point in other window" | |
505 | (interactive) | |
506 | (let ((patched-file (stgit-patched-file-at-point))) | |
507 | (unless patched-file | |
508 | (error "No file on the current line")) | |
3164eec6 | 509 | (let ((filename (expand-file-name (stgit-file-file patched-file)))) |
378a003d GH |
510 | (unless (file-exists-p filename) |
511 | (error "File does not exist")) | |
512 | (find-file-other-window filename)))) | |
513 | ||
83327d53 | 514 | (defun stgit-quit () |
a53347d9 | 515 | "Hide the stgit buffer." |
83327d53 GH |
516 | (interactive) |
517 | (bury-buffer)) | |
518 | ||
0f076fe6 | 519 | (defun stgit-git-status () |
a53347d9 | 520 | "Show status using `git-status'." |
0f076fe6 GH |
521 | (interactive) |
522 | (unless (fboundp 'git-status) | |
df283a8b | 523 | (error "The stgit-git-status command requires git-status")) |
0f076fe6 GH |
524 | (let ((dir default-directory)) |
525 | (save-selected-window | |
526 | (pop-to-buffer nil) | |
527 | (git-status dir)))) | |
528 | ||
58f72f16 GH |
529 | (defun stgit-goal-column () |
530 | "Return goal column for the current line" | |
50d88c67 DK |
531 | (case (get-text-property (point) 'entry-type) |
532 | ('patch 2) | |
533 | ('file 4) | |
534 | (t 0))) | |
58f72f16 GH |
535 | |
536 | (defun stgit-next-line (&optional arg) | |
378a003d | 537 | "Move cursor vertically down ARG lines" |
58f72f16 GH |
538 | (interactive "p") |
539 | (next-line arg) | |
540 | (move-to-column (stgit-goal-column))) | |
378a003d | 541 | |
58f72f16 | 542 | (defun stgit-previous-line (&optional arg) |
378a003d | 543 | "Move cursor vertically up ARG lines" |
58f72f16 GH |
544 | (interactive "p") |
545 | (previous-line arg) | |
546 | (move-to-column (stgit-goal-column))) | |
378a003d GH |
547 | |
548 | (defun stgit-next-patch (&optional arg) | |
98230edd | 549 | "Move cursor down ARG patches." |
378a003d | 550 | (interactive "p") |
98230edd DK |
551 | (ewoc-goto-next stgit-ewoc (or arg 1)) |
552 | (move-to-column goal-column)) | |
378a003d GH |
553 | |
554 | (defun stgit-previous-patch (&optional arg) | |
98230edd | 555 | "Move cursor up ARG patches." |
378a003d | 556 | (interactive "p") |
98230edd DK |
557 | (ewoc-goto-prev stgit-ewoc (or arg 1)) |
558 | (move-to-column goal-column)) | |
378a003d | 559 | |
56d81fe5 DK |
560 | (defvar stgit-mode-hook nil |
561 | "Run after `stgit-mode' is setup.") | |
562 | ||
563 | (defvar stgit-mode-map nil | |
564 | "Keymap for StGit major mode.") | |
565 | ||
566 | (unless stgit-mode-map | |
567 | (setq stgit-mode-map (make-keymap)) | |
568 | (suppress-keymap stgit-mode-map) | |
022a3664 GH |
569 | (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg))) |
570 | '((" " . stgit-mark) | |
3dccdc9b | 571 | ("m" . stgit-mark) |
9b151b27 GH |
572 | ("\d" . stgit-unmark-up) |
573 | ("u" . stgit-unmark-down) | |
022a3664 GH |
574 | ("?" . stgit-help) |
575 | ("h" . stgit-help) | |
58f72f16 GH |
576 | ("\C-p" . stgit-previous-line) |
577 | ("\C-n" . stgit-next-line) | |
578 | ([up] . stgit-previous-line) | |
579 | ([down] . stgit-next-line) | |
580 | ("p" . stgit-previous-patch) | |
581 | ("n" . stgit-next-patch) | |
378a003d GH |
582 | ("\M-{" . stgit-previous-patch) |
583 | ("\M-}" . stgit-next-patch) | |
0f076fe6 | 584 | ("s" . stgit-git-status) |
022a3664 GH |
585 | ("g" . stgit-reload) |
586 | ("r" . stgit-refresh) | |
587 | ("\C-c\C-r" . stgit-rename) | |
588 | ("e" . stgit-edit) | |
7cc45294 | 589 | ("M" . stgit-move-patches) |
594aa463 | 590 | ("S" . stgit-squash) |
022a3664 GH |
591 | ("N" . stgit-new) |
592 | ("R" . stgit-repair) | |
593 | ("C" . stgit-commit) | |
594 | ("U" . stgit-uncommit) | |
378a003d GH |
595 | ("\r" . stgit-select) |
596 | ("o" . stgit-find-file-other-window) | |
37cb5766 | 597 | ("i" . stgit-file-toggle-index) |
022a3664 GH |
598 | (">" . stgit-push-next) |
599 | ("<" . stgit-pop-next) | |
600 | ("P" . stgit-push-or-pop) | |
601 | ("G" . stgit-goto) | |
602 | ("=" . stgit-show) | |
603 | ("D" . stgit-delete) | |
604 | ([(control ?/)] . stgit-undo) | |
83327d53 | 605 | ("\C-_" . stgit-undo) |
adeef6bc GH |
606 | ("B" . stgit-branch) |
607 | ("q" . stgit-quit)))) | |
56d81fe5 DK |
608 | |
609 | (defun stgit-mode () | |
610 | "Major mode for interacting with StGit. | |
611 | Commands: | |
612 | \\{stgit-mode-map}" | |
613 | (kill-all-local-variables) | |
614 | (buffer-disable-undo) | |
615 | (setq mode-name "StGit" | |
616 | major-mode 'stgit-mode | |
617 | goal-column 2) | |
618 | (use-local-map stgit-mode-map) | |
619 | (set (make-local-variable 'list-buffers-directory) default-directory) | |
6df83d42 | 620 | (set (make-local-variable 'stgit-marked-patches) nil) |
378a003d | 621 | (set (make-local-variable 'stgit-expanded-patches) nil) |
2870f8b8 | 622 | (set-variable 'truncate-lines 't) |
b894e680 | 623 | (add-hook 'after-save-hook 'stgit-update-saved-file) |
56d81fe5 DK |
624 | (run-hooks 'stgit-mode-hook)) |
625 | ||
b894e680 DK |
626 | (defun stgit-update-saved-file () |
627 | (let* ((file (expand-file-name buffer-file-name)) | |
628 | (dir (file-name-directory file)) | |
629 | (gitdir (condition-case nil (git-get-top-dir dir) | |
630 | (error nil))) | |
631 | (buffer (and gitdir (stgit-find-buffer gitdir)))) | |
632 | (when buffer | |
633 | (with-current-buffer buffer | |
634 | ;; FIXME: just invalidate ewoc node | |
635 | (stgit-reload))))) | |
636 | ||
d51722b7 GH |
637 | (defun stgit-add-mark (patchsym) |
638 | "Mark the patch PATCHSYM." | |
8036afdd | 639 | (setq stgit-marked-patches (cons patchsym stgit-marked-patches))) |
6df83d42 | 640 | |
d51722b7 GH |
641 | (defun stgit-remove-mark (patchsym) |
642 | "Unmark the patch PATCHSYM." | |
8036afdd | 643 | (setq stgit-marked-patches (delq patchsym stgit-marked-patches))) |
6df83d42 | 644 | |
e6b1fdae | 645 | (defun stgit-clear-marks () |
47271f41 | 646 | "Unmark all patches." |
e6b1fdae DK |
647 | (setq stgit-marked-patches '())) |
648 | ||
735cb7ec | 649 | (defun stgit-patch-at-point (&optional cause-error) |
2c862b07 DK |
650 | (get-text-property (point) 'patch-data)) |
651 | ||
652 | (defun stgit-patch-name-at-point (&optional cause-error) | |
d51722b7 | 653 | "Return the patch name on the current line as a symbol. |
735cb7ec | 654 | If CAUSE-ERROR is not nil, signal an error if none found." |
2c862b07 DK |
655 | (let ((patch (stgit-patch-at-point))) |
656 | (cond (patch | |
657 | (stgit-patch-name patch)) | |
658 | (cause-error | |
659 | (error "No patch on this line"))))) | |
378a003d | 660 | |
3164eec6 DK |
661 | (defun stgit-patched-file-at-point () |
662 | (get-text-property (point) 'file-data)) | |
56d81fe5 | 663 | |
7755d7f1 | 664 | (defun stgit-patches-marked-or-at-point () |
d51722b7 | 665 | "Return the symbols of the marked patches, or the patch on the current line." |
7755d7f1 | 666 | (if stgit-marked-patches |
d51722b7 | 667 | stgit-marked-patches |
2c862b07 | 668 | (let ((patch (stgit-patch-name-at-point))) |
7755d7f1 KH |
669 | (if patch |
670 | (list patch) | |
671 | '())))) | |
672 | ||
d51722b7 GH |
673 | (defun stgit-goto-patch (patchsym) |
674 | "Move point to the line containing patch PATCHSYM. | |
f9b82d36 DK |
675 | If that patch cannot be found, do nothing." |
676 | (let ((node (ewoc-nth stgit-ewoc 0))) | |
677 | (while (and node (not (eq (stgit-patch-name (ewoc-data node)) | |
678 | patchsym))) | |
679 | (setq node (ewoc-next stgit-ewoc node))) | |
680 | (when node | |
681 | (ewoc-goto-node stgit-ewoc node) | |
d51722b7 | 682 | (move-to-column goal-column)))) |
56d81fe5 | 683 | |
1c2426dc | 684 | (defun stgit-init () |
a53347d9 | 685 | "Run stg init." |
1c2426dc DK |
686 | (interactive) |
687 | (stgit-capture-output nil | |
b0424080 | 688 | (stgit-run "init")) |
1f0bf00f | 689 | (stgit-reload)) |
1c2426dc | 690 | |
6df83d42 | 691 | (defun stgit-mark () |
a53347d9 | 692 | "Mark the patch under point." |
6df83d42 | 693 | (interactive) |
8036afdd DK |
694 | (let* ((node (ewoc-locate stgit-ewoc)) |
695 | (patch (ewoc-data node))) | |
696 | (stgit-add-mark (stgit-patch-name patch)) | |
697 | (ewoc-invalidate stgit-ewoc node)) | |
378a003d | 698 | (stgit-next-patch)) |
6df83d42 | 699 | |
9b151b27 | 700 | (defun stgit-unmark-up () |
a53347d9 | 701 | "Remove mark from the patch on the previous line." |
6df83d42 | 702 | (interactive) |
378a003d | 703 | (stgit-previous-patch) |
8036afdd DK |
704 | (let* ((node (ewoc-locate stgit-ewoc)) |
705 | (patch (ewoc-data node))) | |
706 | (stgit-remove-mark (stgit-patch-name patch)) | |
707 | (ewoc-invalidate stgit-ewoc node)) | |
708 | (move-to-column (stgit-goal-column))) | |
9b151b27 GH |
709 | |
710 | (defun stgit-unmark-down () | |
a53347d9 | 711 | "Remove mark from the patch on the current line." |
9b151b27 | 712 | (interactive) |
8036afdd DK |
713 | (let* ((node (ewoc-locate stgit-ewoc)) |
714 | (patch (ewoc-data node))) | |
715 | (stgit-remove-mark (stgit-patch-name patch)) | |
716 | (ewoc-invalidate stgit-ewoc node)) | |
1288eda2 | 717 | (stgit-next-patch)) |
6df83d42 | 718 | |
56d81fe5 | 719 | (defun stgit-rename (name) |
018fa1ac | 720 | "Rename the patch under point to NAME." |
d51722b7 | 721 | (interactive (list (read-string "Patch name: " |
2c862b07 DK |
722 | (symbol-name (stgit-patch-name-at-point t))))) |
723 | (let ((old-patchsym (stgit-patch-name-at-point t))) | |
56d81fe5 | 724 | (stgit-capture-output nil |
d51722b7 GH |
725 | (stgit-run "rename" old-patchsym name)) |
726 | (let ((name-sym (intern name))) | |
727 | (when (memq old-patchsym stgit-expanded-patches) | |
378a003d | 728 | (setq stgit-expanded-patches |
d51722b7 GH |
729 | (cons name-sym (delq old-patchsym stgit-expanded-patches)))) |
730 | (when (memq old-patchsym stgit-marked-patches) | |
378a003d | 731 | (setq stgit-marked-patches |
d51722b7 GH |
732 | (cons name-sym (delq old-patchsym stgit-marked-patches)))) |
733 | (stgit-reload) | |
734 | (stgit-goto-patch name-sym)))) | |
56d81fe5 | 735 | |
26201d96 | 736 | (defun stgit-repair () |
a53347d9 | 737 | "Run stg repair." |
26201d96 DK |
738 | (interactive) |
739 | (stgit-capture-output nil | |
b0424080 | 740 | (stgit-run "repair")) |
1f0bf00f | 741 | (stgit-reload)) |
26201d96 | 742 | |
adeef6bc GH |
743 | (defun stgit-available-branches () |
744 | "Returns a list of the available stg branches" | |
745 | (let ((output (with-output-to-string | |
746 | (stgit-run "branch" "--list"))) | |
747 | (start 0) | |
748 | result) | |
749 | (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start) | |
750 | (setq result (cons (match-string 1 output) result)) | |
751 | (setq start (match-end 0))) | |
752 | result)) | |
753 | ||
754 | (defun stgit-branch (branch) | |
755 | "Switch to branch BRANCH." | |
756 | (interactive (list (completing-read "Switch to branch: " | |
757 | (stgit-available-branches)))) | |
758 | (stgit-capture-output nil (stgit-run "branch" "--" branch)) | |
759 | (stgit-reload)) | |
760 | ||
41c1c59c GH |
761 | (defun stgit-commit (count) |
762 | "Run stg commit on COUNT commits. | |
763 | Interactively, the prefix argument is used as COUNT." | |
764 | (interactive "p") | |
765 | (stgit-capture-output nil (stgit-run "commit" "-n" count)) | |
1f0bf00f | 766 | (stgit-reload)) |
c4aad9a7 | 767 | |
41c1c59c GH |
768 | (defun stgit-uncommit (count) |
769 | "Run stg uncommit on COUNT commits. | |
770 | Interactively, the prefix argument is used as COUNT." | |
c4aad9a7 | 771 | (interactive "p") |
41c1c59c | 772 | (stgit-capture-output nil (stgit-run "uncommit" "-n" count)) |
1f0bf00f | 773 | (stgit-reload)) |
c4aad9a7 | 774 | |
0b661144 DK |
775 | (defun stgit-push-next (npatches) |
776 | "Push the first unapplied patch. | |
777 | With numeric prefix argument, push that many patches." | |
778 | (interactive "p") | |
d51722b7 | 779 | (stgit-capture-output nil (stgit-run "push" "-n" npatches)) |
074a4fb0 GH |
780 | (stgit-reload) |
781 | (stgit-refresh-git-status)) | |
56d81fe5 | 782 | |
0b661144 DK |
783 | (defun stgit-pop-next (npatches) |
784 | "Pop the topmost applied patch. | |
785 | With numeric prefix argument, pop that many patches." | |
786 | (interactive "p") | |
d51722b7 | 787 | (stgit-capture-output nil (stgit-run "pop" "-n" npatches)) |
074a4fb0 GH |
788 | (stgit-reload) |
789 | (stgit-refresh-git-status)) | |
56d81fe5 | 790 | |
f9182fca KH |
791 | (defun stgit-applied-at-point () |
792 | "Is the patch on the current line applied?" | |
793 | (save-excursion | |
794 | (beginning-of-line) | |
795 | (looking-at "[>+]"))) | |
796 | ||
797 | (defun stgit-push-or-pop () | |
a53347d9 | 798 | "Push or pop the patch on the current line." |
f9182fca | 799 | (interactive) |
2c862b07 | 800 | (let ((patchsym (stgit-patch-name-at-point t)) |
f9182fca KH |
801 | (applied (stgit-applied-at-point))) |
802 | (stgit-capture-output nil | |
d51722b7 | 803 | (stgit-run (if applied "pop" "push") patchsym)) |
1f0bf00f | 804 | (stgit-reload))) |
f9182fca | 805 | |
c7adf5ef | 806 | (defun stgit-goto () |
a53347d9 | 807 | "Go to the patch on the current line." |
c7adf5ef | 808 | (interactive) |
2c862b07 | 809 | (let ((patchsym (stgit-patch-name-at-point t))) |
c7adf5ef | 810 | (stgit-capture-output nil |
d51722b7 | 811 | (stgit-run "goto" patchsym)) |
1f0bf00f | 812 | (stgit-reload))) |
c7adf5ef | 813 | |
d51722b7 | 814 | (defun stgit-id (patchsym) |
50d88c67 DK |
815 | "Return the git commit id for PATCHSYM. |
816 | If PATCHSYM is a keyword, returns PATCHSYM unmodified." | |
817 | (if (keywordp patchsym) | |
818 | patchsym | |
819 | (let ((result (with-output-to-string | |
820 | (stgit-run-silent "id" patchsym)))) | |
821 | (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result) | |
822 | (error "Cannot find commit id for %s" patchsym)) | |
823 | (match-string 1 result)))) | |
378a003d | 824 | |
56d81fe5 | 825 | (defun stgit-show () |
a53347d9 | 826 | "Show the patch on the current line." |
56d81fe5 DK |
827 | (interactive) |
828 | (stgit-capture-output "*StGit patch*" | |
50d88c67 DK |
829 | (case (get-text-property (point) 'entry-type) |
830 | ('file | |
3164eec6 DK |
831 | (let* ((patched-file (stgit-patched-file-at-point)) |
832 | (patch-name (stgit-patch-name-at-point)) | |
833 | (patch-id (stgit-id patch-name)) | |
834 | (args (append (and (stgit-file-cr-from patched-file) | |
835 | (if stgit-expand-find-copies-harder | |
836 | '("--find-copies-harder") | |
837 | '("-C"))) | |
b894e680 DK |
838 | (cond ((eq patch-id :index) |
839 | '("--cached")) | |
840 | ((eq patch-id :work) | |
841 | nil) | |
842 | (t | |
843 | (list (concat patch-id "^") patch-id))) | |
3164eec6 DK |
844 | '("--") |
845 | (if (stgit-file-copy-or-rename patched-file) | |
846 | (list (stgit-file-cr-from patched-file) | |
847 | (stgit-file-cr-to patched-file)) | |
848 | (list (stgit-file-file patched-file)))))) | |
849 | (apply 'stgit-run-git "diff" args))) | |
50d88c67 | 850 | ('patch |
2c862b07 DK |
851 | (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M" |
852 | (stgit-patch-name-at-point))) | |
50d88c67 DK |
853 | (t |
854 | (error "No patch or file at point"))) | |
855 | (with-current-buffer standard-output | |
856 | (goto-char (point-min)) | |
857 | (diff-mode)))) | |
0663524d | 858 | |
37cb5766 DK |
859 | (defun stgit-move-change-to-index (file status) |
860 | "Copies the workspace state of FILE to index, using git add or git rm" | |
861 | (let ((op (if (file-exists-p file) "add" "rm"))) | |
862 | (stgit-capture-output "*git output*" | |
863 | (stgit-run-git op "--" file)))) | |
864 | ||
865 | (defun stgit-remove-change-from-index (file status) | |
866 | "Unstages the change in FILE from the index" | |
867 | (stgit-capture-output "*git output*" | |
868 | (stgit-run-git "reset" "-q" "--" file))) | |
869 | ||
870 | (defun stgit-file-toggle-index () | |
871 | "Move modified file in or out of the index." | |
872 | (interactive) | |
873 | (let ((patched-file (stgit-patched-file-at-point))) | |
874 | (unless patched-file | |
875 | (error "No file on the current line")) | |
876 | (let ((patch-name (stgit-patch-name-at-point))) | |
877 | (cond ((eq patch-name :work) | |
878 | (stgit-move-change-to-index (stgit-file-file patched-file) | |
879 | (stgit-file-status patched-file))) | |
880 | ((eq patch-name :index) | |
881 | (stgit-remove-change-from-index (stgit-file-file patched-file) | |
882 | (stgit-file-status patched-file))) | |
883 | (t | |
884 | (error "Can only move files in the working tree to index"))))) | |
885 | ;; FIXME: invalidate ewoc | |
886 | (stgit-reload)) | |
887 | ||
0bca35c8 | 888 | (defun stgit-edit () |
a53347d9 | 889 | "Edit the patch on the current line." |
0bca35c8 | 890 | (interactive) |
2c862b07 | 891 | (let ((patchsym (stgit-patch-name-at-point t)) |
0780be79 | 892 | (edit-buf (get-buffer-create "*StGit edit*")) |
0bca35c8 DK |
893 | (dir default-directory)) |
894 | (log-edit 'stgit-confirm-edit t nil edit-buf) | |
d51722b7 | 895 | (set (make-local-variable 'stgit-edit-patchsym) patchsym) |
0bca35c8 DK |
896 | (setq default-directory dir) |
897 | (let ((standard-output edit-buf)) | |
d51722b7 | 898 | (stgit-run-silent "edit" "--save-template=-" patchsym)))) |
0bca35c8 DK |
899 | |
900 | (defun stgit-confirm-edit () | |
901 | (interactive) | |
902 | (let ((file (make-temp-file "stgit-edit-"))) | |
903 | (write-region (point-min) (point-max) file) | |
904 | (stgit-capture-output nil | |
d51722b7 | 905 | (stgit-run "edit" "-f" file stgit-edit-patchsym)) |
0bca35c8 | 906 | (with-current-buffer log-edit-parent-buffer |
1f0bf00f | 907 | (stgit-reload)))) |
0bca35c8 | 908 | |
aa04f831 GH |
909 | (defun stgit-new (add-sign) |
910 | "Create a new patch. | |
911 | With a prefix argument, include a \"Signed-off-by:\" line at the | |
912 | end of the patch." | |
913 | (interactive "P") | |
c5d45b92 GH |
914 | (let ((edit-buf (get-buffer-create "*StGit edit*")) |
915 | (dir default-directory)) | |
916 | (log-edit 'stgit-confirm-new t nil edit-buf) | |
aa04f831 GH |
917 | (setq default-directory dir) |
918 | (when add-sign | |
919 | (save-excursion | |
920 | (let ((standard-output (current-buffer))) | |
921 | (stgit-run-silent "new" "--sign" "--save-template=-")))))) | |
64c097a0 DK |
922 | |
923 | (defun stgit-confirm-new () | |
924 | (interactive) | |
27b0f9e4 | 925 | (let ((file (make-temp-file "stgit-edit-"))) |
64c097a0 DK |
926 | (write-region (point-min) (point-max) file) |
927 | (stgit-capture-output nil | |
27b0f9e4 | 928 | (stgit-run "new" "-f" file)) |
64c097a0 | 929 | (with-current-buffer log-edit-parent-buffer |
1f0bf00f | 930 | (stgit-reload)))) |
64c097a0 DK |
931 | |
932 | (defun stgit-create-patch-name (description) | |
933 | "Create a patch name from a long description" | |
934 | (let ((patch "")) | |
935 | (while (> (length description) 0) | |
936 | (cond ((string-match "\\`[a-zA-Z_-]+" description) | |
8439f657 GH |
937 | (setq patch (downcase (concat patch |
938 | (match-string 0 description)))) | |
64c097a0 DK |
939 | (setq description (substring description (match-end 0)))) |
940 | ((string-match "\\` +" description) | |
941 | (setq patch (concat patch "-")) | |
942 | (setq description (substring description (match-end 0)))) | |
943 | ((string-match "\\`[^a-zA-Z_-]+" description) | |
944 | (setq description (substring description (match-end 0)))))) | |
945 | (cond ((= (length patch) 0) | |
946 | "patch") | |
947 | ((> (length patch) 20) | |
948 | (substring patch 0 20)) | |
949 | (t patch)))) | |
0bca35c8 | 950 | |
9008e45b | 951 | (defun stgit-delete (patchsyms &optional spill-p) |
d51722b7 | 952 | "Delete the patches in PATCHSYMS. |
9008e45b GH |
953 | Interactively, delete the marked patches, or the patch at point. |
954 | ||
955 | With a prefix argument, or SPILL-P, spill the patch contents to | |
956 | the work tree and index." | |
957 | (interactive (list (stgit-patches-marked-or-at-point) | |
958 | current-prefix-arg)) | |
e7231e4f GH |
959 | (unless patchsyms |
960 | (error "No patches to delete")) | |
d51722b7 | 961 | (let ((npatches (length patchsyms))) |
9008e45b | 962 | (when (yes-or-no-p (format "Really delete %d patch%s%s? " |
e7231e4f | 963 | npatches |
9008e45b GH |
964 | (if (= 1 npatches) "" "es") |
965 | (if spill-p | |
966 | " (spilling contents to index)" | |
967 | ""))) | |
968 | (let ((args (if spill-p | |
969 | (cons "--spill" patchsyms) | |
970 | patchsyms))) | |
971 | (stgit-capture-output nil | |
972 | (apply 'stgit-run "delete" args)) | |
973 | (stgit-reload))))) | |
d51722b7 | 974 | |
7cc45294 GH |
975 | (defun stgit-move-patches-target () |
976 | "Return the patchsym indicating a target patch for | |
977 | `stgit-move-patches'. | |
978 | ||
979 | This is either the patch at point, or one of :top and :bottom, if | |
980 | the point is after or before the applied patches." | |
981 | ||
2c862b07 | 982 | (let ((patchsym (stgit-patch-name-at-point))) |
7cc45294 GH |
983 | (cond (patchsym patchsym) |
984 | ((save-excursion (re-search-backward "^>" nil t)) :top) | |
985 | (t :bottom)))) | |
986 | ||
95369f6c GH |
987 | (defun stgit-sort-patches (patchsyms) |
988 | "Returns the list of patches in PATCHSYMS sorted according to | |
989 | their position in the patch series, bottommost first. | |
990 | ||
991 | PATCHSYMS may not contain duplicate entries." | |
992 | (let (sorted-patchsyms | |
993 | (series (with-output-to-string | |
994 | (with-current-buffer standard-output | |
995 | (stgit-run-silent "series" "--noprefix")))) | |
996 | start) | |
997 | (while (string-match "^\\(.+\\)" series start) | |
998 | (let ((patchsym (intern (match-string 1 series)))) | |
999 | (when (memq patchsym patchsyms) | |
1000 | (setq sorted-patchsyms (cons patchsym sorted-patchsyms)))) | |
1001 | (setq start (match-end 0))) | |
1002 | (setq sorted-patchsyms (nreverse sorted-patchsyms)) | |
1003 | ||
1004 | (unless (= (length patchsyms) (length sorted-patchsyms)) | |
1005 | (error "Internal error")) | |
1006 | ||
1007 | sorted-patchsyms)) | |
1008 | ||
7cc45294 GH |
1009 | (defun stgit-move-patches (patchsyms target-patch) |
1010 | "Move the patches in PATCHSYMS to below TARGET-PATCH. | |
1011 | If TARGET-PATCH is :bottom or :top, move the patches to the | |
1012 | bottom or top of the stack, respectively. | |
1013 | ||
1014 | Interactively, move the marked patches to where the point is." | |
1015 | (interactive (list stgit-marked-patches | |
1016 | (stgit-move-patches-target))) | |
1017 | (unless patchsyms | |
1018 | (error "Need at least one patch to move")) | |
1019 | ||
1020 | (unless target-patch | |
1021 | (error "Point not at a patch")) | |
1022 | ||
1023 | (if (eq target-patch :top) | |
1024 | (stgit-capture-output nil | |
1025 | (apply 'stgit-run "float" patchsyms)) | |
1026 | ||
1027 | ;; need to have patchsyms sorted by position in the stack | |
95369f6c | 1028 | (let ((sorted-patchsyms (stgit-sort-patches patchsyms))) |
7cc45294 GH |
1029 | (while sorted-patchsyms |
1030 | (setq sorted-patchsyms | |
1031 | (and (stgit-capture-output nil | |
1032 | (if (eq target-patch :bottom) | |
1033 | (stgit-run "sink" "--" (car sorted-patchsyms)) | |
1034 | (stgit-run "sink" "--to" target-patch "--" | |
1035 | (car sorted-patchsyms)))) | |
1036 | (cdr sorted-patchsyms)))))) | |
1037 | (stgit-reload)) | |
1038 | ||
594aa463 KH |
1039 | (defun stgit-squash (patchsyms) |
1040 | "Squash the patches in PATCHSYMS. | |
693d179b GH |
1041 | Interactively, squash the marked patches. |
1042 | ||
1043 | Unless there are any conflicts, the patches will be merged into | |
1044 | one patch, which will occupy the same spot in the series as the | |
1045 | deepest patch had before the squash." | |
d51722b7 GH |
1046 | (interactive (list stgit-marked-patches)) |
1047 | (when (< (length patchsyms) 2) | |
594aa463 | 1048 | (error "Need at least two patches to squash")) |
32d7545d GH |
1049 | (let ((stgit-buffer (current-buffer)) |
1050 | (edit-buf (get-buffer-create "*StGit edit*")) | |
693d179b GH |
1051 | (dir default-directory) |
1052 | (sorted-patchsyms (stgit-sort-patches patchsyms))) | |
594aa463 | 1053 | (log-edit 'stgit-confirm-squash t nil edit-buf) |
693d179b | 1054 | (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms) |
ea0def18 | 1055 | (setq default-directory dir) |
32d7545d GH |
1056 | (let ((result (let ((standard-output edit-buf)) |
1057 | (apply 'stgit-run-silent "squash" | |
1058 | "--save-template=-" sorted-patchsyms)))) | |
1059 | ||
1060 | ;; stg squash may have reordered the patches or caused conflicts | |
1061 | (with-current-buffer stgit-buffer | |
1062 | (stgit-reload)) | |
1063 | ||
1064 | (unless (eq 0 result) | |
1065 | (fundamental-mode) | |
1066 | (rename-buffer "*StGit error*") | |
1067 | (resize-temp-buffer-window) | |
1068 | (switch-to-buffer-other-window stgit-buffer) | |
1069 | (error "stg squash failed"))))) | |
ea0def18 | 1070 | |
594aa463 | 1071 | (defun stgit-confirm-squash () |
ea0def18 DK |
1072 | (interactive) |
1073 | (let ((file (make-temp-file "stgit-edit-"))) | |
1074 | (write-region (point-min) (point-max) file) | |
1075 | (stgit-capture-output nil | |
594aa463 | 1076 | (apply 'stgit-run "squash" "-f" file stgit-patchsyms)) |
ea0def18 | 1077 | (with-current-buffer log-edit-parent-buffer |
e6b1fdae DK |
1078 | (stgit-clear-marks) |
1079 | ;; Go to first marked patch and stay there | |
1080 | (goto-char (point-min)) | |
1081 | (re-search-forward (concat "^[>+-]\\*") nil t) | |
1082 | (move-to-column goal-column) | |
1083 | (let ((pos (point))) | |
1f0bf00f | 1084 | (stgit-reload) |
e6b1fdae | 1085 | (goto-char pos))))) |
ea0def18 | 1086 | |
0663524d KH |
1087 | (defun stgit-help () |
1088 | "Display help for the StGit mode." | |
1089 | (interactive) | |
1090 | (describe-function 'stgit-mode)) | |
3a59f3db | 1091 | |
83e51dbf DK |
1092 | (defun stgit-undo (&optional arg) |
1093 | "Run stg undo. | |
1094 | With prefix argument, run it with the --hard flag." | |
1095 | (interactive "P") | |
1096 | (stgit-capture-output nil | |
1097 | (if arg | |
1098 | (stgit-run "undo" "--hard") | |
1099 | (stgit-run "undo"))) | |
1f0bf00f | 1100 | (stgit-reload)) |
83e51dbf | 1101 | |
4d73c4d8 DK |
1102 | (defun stgit-refresh (&optional arg) |
1103 | "Run stg refresh. | |
a53347d9 | 1104 | With prefix argument, refresh the marked patch or the patch under point." |
4d73c4d8 DK |
1105 | (interactive "P") |
1106 | (let ((patchargs (if arg | |
b0424080 GH |
1107 | (let ((patches (stgit-patches-marked-or-at-point))) |
1108 | (cond ((null patches) | |
df283a8b | 1109 | (error "No patch to update")) |
b0424080 | 1110 | ((> (length patches) 1) |
df283a8b | 1111 | (error "Too many patches selected")) |
b0424080 GH |
1112 | (t |
1113 | (cons "-p" patches)))) | |
1114 | nil))) | |
4d73c4d8 | 1115 | (stgit-capture-output nil |
074a4fb0 GH |
1116 | (apply 'stgit-run "refresh" patchargs)) |
1117 | (stgit-refresh-git-status)) | |
4d73c4d8 DK |
1118 | (stgit-reload)) |
1119 | ||
3a59f3db | 1120 | (provide 'stgit) |