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