stgit.el: Show running commands
[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 (defun stgit (dir)
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
15 (switch-to-stgit-buffer (git-get-top-dir dir))
16 (stgit-reload))
17
18 (defun git-get-top-dir (dir)
19 "Retrieve the top-level directory of a git tree."
20 (let ((cdup (with-output-to-string
21 (with-current-buffer standard-output
22 (cd dir)
23 (unless (eq 0 (call-process "git" nil t nil
24 "rev-parse" "--show-cdup"))
25 (error "cannot find top-level git tree for %s." dir))))))
26 (expand-file-name (concat (file-name-as-directory dir)
27 (car (split-string cdup "\n"))))))
28
29 (defun switch-to-stgit-buffer (dir)
30 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
31 (setq dir (file-name-as-directory dir))
32 (let ((buffers (buffer-list)))
33 (while (and buffers
34 (not (with-current-buffer (car buffers)
35 (and (eq major-mode 'stgit-mode)
36 (string= default-directory dir)))))
37 (setq buffers (cdr buffers)))
38 (switch-to-buffer (if buffers
39 (car buffers)
40 (create-stgit-buffer dir)))))
41
42 (defun create-stgit-buffer (dir)
43 "Create a buffer for showing StGit patches.
44 Argument DIR is the repository path."
45 (let ((buf (create-file-buffer (concat dir "*stgit*")))
46 (inhibit-read-only t))
47 (with-current-buffer buf
48 (setq default-directory dir)
49 (stgit-mode)
50 (setq buffer-read-only t))
51 buf))
52
53 (defmacro stgit-capture-output (name &rest body)
54 "Capture StGit output and show it in a window at the end"
55 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
56 (stgit-dir default-directory)
57 (inhibit-read-only t))
58 (with-current-buffer output-buf
59 (erase-buffer)
60 (setq default-directory stgit-dir)
61 (setq buffer-read-only t))
62 (let ((standard-output output-buf))
63 ,@body)
64 (with-current-buffer output-buf
65 (set-buffer-modified-p nil)
66 (setq buffer-read-only t)
67 (if (< (point-min) (point-max))
68 (display-buffer output-buf t)))))
69 (put 'stgit-capture-output 'lisp-indent-function 1)
70
71 (defun stgit-run-silent (&rest args)
72 (apply 'call-process "stg" nil standard-output nil args))
73
74 (defun stgit-run (&rest args)
75 (let ((msgcmd (mapconcat #'identity args " ")))
76 (message "Running stg %s..." msgcmd)
77 (apply 'call-process "stg" nil standard-output nil args)
78 (message "Running stg %s...done" msgcmd)))
79
80 (defun stgit-reload ()
81 "Update the contents of the stgit buffer"
82 (interactive)
83 (let ((inhibit-read-only t)
84 (curline (line-number-at-pos))
85 (curpatch (stgit-patch-at-point)))
86 (erase-buffer)
87 (insert "Branch: ")
88 (stgit-run-silent "branch")
89 (stgit-run-silent "series" "--description")
90 (stgit-rescan)
91 (if curpatch
92 (stgit-goto-patch curpatch)
93 (goto-line curline))))
94
95 (defface stgit-description-face
96 '((((background dark)) (:foreground "tan"))
97 (((background light)) (:foreground "dark red")))
98 "The face used for StGit desriptions")
99
100 (defface stgit-top-patch-face
101 '((((background dark)) (:weight bold :foreground "yellow"))
102 (((background light)) (:weight bold :foreground "purple"))
103 (t (:weight bold)))
104 "The face used for the top patch names")
105
106 (defface stgit-applied-patch-face
107 '((((background dark)) (:foreground "light yellow"))
108 (((background light)) (:foreground "purple"))
109 (t ()))
110 "The face used for applied patch names")
111
112 (defface stgit-unapplied-patch-face
113 '((((background dark)) (:foreground "gray80"))
114 (((background light)) (:foreground "orchid"))
115 (t ()))
116 "The face used for unapplied patch names")
117
118 (defun stgit-rescan ()
119 "Rescan the status buffer."
120 (save-excursion
121 (let ((marked ()))
122 (goto-char (point-min))
123 (while (not (eobp))
124 (cond ((looking-at "Branch: \\(.*\\)")
125 (put-text-property (match-beginning 1) (match-end 1)
126 'face 'bold))
127 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
128 (let ((state (match-string 1))
129 (patchsym (intern (match-string 3))))
130 (put-text-property
131 (match-beginning 3) (match-end 3) 'face
132 (cond ((string= state ">") 'stgit-top-patch-face)
133 ((string= state "+") 'stgit-applied-patch-face)
134 ((string= state "-") 'stgit-unapplied-patch-face)))
135 (put-text-property (match-beginning 4) (match-end 4)
136 'face 'stgit-description-face)
137 (when (memq patchsym stgit-marked-patches)
138 (replace-match "*" nil nil nil 2)
139 (setq marked (cons patchsym marked)))))
140 ((or (looking-at "stg series: Branch \".*\" not initialised")
141 (looking-at "stg series: .*: branch not initialized"))
142 (forward-line 1)
143 (insert "Run M-x stgit-init to initialise")))
144 (forward-line 1))
145 (setq stgit-marked-patches (nreverse marked)))))
146
147 (defvar stgit-mode-hook nil
148 "Run after `stgit-mode' is setup.")
149
150 (defvar stgit-mode-map nil
151 "Keymap for StGit major mode.")
152
153 (unless stgit-mode-map
154 (setq stgit-mode-map (make-keymap))
155 (suppress-keymap stgit-mode-map)
156 (define-key stgit-mode-map " " 'stgit-mark)
157 (define-key stgit-mode-map "\d" 'stgit-unmark)
158 (define-key stgit-mode-map "?" 'stgit-help)
159 (define-key stgit-mode-map "h" 'stgit-help)
160 (define-key stgit-mode-map "p" 'previous-line)
161 (define-key stgit-mode-map "n" 'next-line)
162 (define-key stgit-mode-map "g" 'stgit-reload)
163 (define-key stgit-mode-map "r" 'stgit-refresh)
164 (define-key stgit-mode-map "\C-c\C-r" 'stgit-rename)
165 (define-key stgit-mode-map "e" 'stgit-edit)
166 (define-key stgit-mode-map "c" 'stgit-coalesce)
167 (define-key stgit-mode-map "N" 'stgit-new)
168 (define-key stgit-mode-map "R" 'stgit-repair)
169 (define-key stgit-mode-map "C" 'stgit-commit)
170 (define-key stgit-mode-map "U" 'stgit-uncommit)
171 (define-key stgit-mode-map ">" 'stgit-push-next)
172 (define-key stgit-mode-map "<" 'stgit-pop-next)
173 (define-key stgit-mode-map "P" 'stgit-push-or-pop)
174 (define-key stgit-mode-map "G" 'stgit-goto)
175 (define-key stgit-mode-map "=" 'stgit-show)
176 (define-key stgit-mode-map "D" 'stgit-delete)
177 (define-key stgit-mode-map [(control ?/)] 'stgit-undo)
178 (define-key stgit-mode-map "\C-_" 'stgit-undo))
179
180 (defun stgit-mode ()
181 "Major mode for interacting with StGit.
182 Commands:
183 \\{stgit-mode-map}"
184 (kill-all-local-variables)
185 (buffer-disable-undo)
186 (setq mode-name "StGit"
187 major-mode 'stgit-mode
188 goal-column 2)
189 (use-local-map stgit-mode-map)
190 (set (make-local-variable 'list-buffers-directory) default-directory)
191 (set (make-local-variable 'stgit-marked-patches) nil)
192 (set-variable 'truncate-lines 't)
193 (run-hooks 'stgit-mode-hook))
194
195 (defun stgit-add-mark (patch)
196 (let ((patchsym (intern patch)))
197 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
198
199 (defun stgit-remove-mark (patch)
200 (let ((patchsym (intern patch)))
201 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
202
203 (defun stgit-clear-marks ()
204 (setq stgit-marked-patches '()))
205
206 (defun stgit-marked-patches ()
207 "Return the names of the marked patches."
208 (mapcar 'symbol-name stgit-marked-patches))
209
210 (defun stgit-patch-at-point ()
211 "Return the patch name on the current line"
212 (save-excursion
213 (beginning-of-line)
214 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
215 (match-string-no-properties 1)
216 nil)))
217
218 (defun stgit-patches-marked-or-at-point ()
219 "Return the names of the marked patches, or the patch on the current line."
220 (if stgit-marked-patches
221 (stgit-marked-patches)
222 (let ((patch (stgit-patch-at-point)))
223 (if patch
224 (list patch)
225 '()))))
226
227 (defun stgit-goto-patch (patch)
228 "Move point to the line containing PATCH"
229 (let ((p (point)))
230 (goto-char (point-min))
231 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
232 (progn (move-to-column goal-column)
233 t)
234 (goto-char p)
235 nil)))
236
237 (defun stgit-init ()
238 "Run stg init"
239 (interactive)
240 (stgit-capture-output nil
241 (stgit-run "init"))
242 (stgit-reload))
243
244 (defun stgit-mark ()
245 "Mark the patch under point"
246 (interactive)
247 (let ((patch (stgit-patch-at-point)))
248 (stgit-add-mark patch)
249 (stgit-reload))
250 (next-line))
251
252 (defun stgit-unmark ()
253 "Mark the patch on the previous line"
254 (interactive)
255 (forward-line -1)
256 (let ((patch (stgit-patch-at-point)))
257 (stgit-remove-mark patch)
258 (stgit-reload)))
259
260 (defun stgit-rename (name)
261 "Rename the patch under point"
262 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
263 (let ((old-name (stgit-patch-at-point)))
264 (unless old-name
265 (error "No patch on this line"))
266 (stgit-capture-output nil
267 (stgit-run "rename" old-name name))
268 (stgit-reload)
269 (stgit-goto-patch name)))
270
271 (defun stgit-repair ()
272 "Run stg repair"
273 (interactive)
274 (stgit-capture-output nil
275 (stgit-run "repair"))
276 (stgit-reload))
277
278 (defun stgit-commit ()
279 "Run stg commit."
280 (interactive)
281 (stgit-capture-output nil (stgit-run "commit"))
282 (stgit-reload))
283
284 (defun stgit-uncommit (arg)
285 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
286 (interactive "p")
287 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
288 (stgit-reload))
289
290 (defun stgit-push-next (npatches)
291 "Push the first unapplied patch.
292 With numeric prefix argument, push that many patches."
293 (interactive "p")
294 (stgit-capture-output nil (stgit-run "push" "-n"
295 (number-to-string npatches)))
296 (stgit-reload))
297
298 (defun stgit-pop-next (npatches)
299 "Pop the topmost applied patch.
300 With numeric prefix argument, pop that many patches."
301 (interactive "p")
302 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
303 (stgit-reload))
304
305 (defun stgit-applied-at-point ()
306 "Is the patch on the current line applied?"
307 (save-excursion
308 (beginning-of-line)
309 (looking-at "[>+]")))
310
311 (defun stgit-push-or-pop ()
312 "Push or pop the patch on the current line"
313 (interactive)
314 (let ((patch (stgit-patch-at-point))
315 (applied (stgit-applied-at-point)))
316 (stgit-capture-output nil
317 (stgit-run (if applied "pop" "push") patch))
318 (stgit-reload)))
319
320 (defun stgit-goto ()
321 "Go to the patch on the current line"
322 (interactive)
323 (let ((patch (stgit-patch-at-point)))
324 (stgit-capture-output nil
325 (stgit-run "goto" patch))
326 (stgit-reload)))
327
328 (defun stgit-show ()
329 "Show the patch on the current line"
330 (interactive)
331 (stgit-capture-output "*StGit patch*"
332 (stgit-run "show" (stgit-patch-at-point))
333 (with-current-buffer standard-output
334 (goto-char (point-min))
335 (diff-mode))))
336
337 (defun stgit-edit ()
338 "Edit the patch on the current line"
339 (interactive)
340 (let ((patch (stgit-patch-at-point))
341 (edit-buf (get-buffer-create "*StGit edit*"))
342 (dir default-directory))
343 (log-edit 'stgit-confirm-edit t nil edit-buf)
344 (set (make-local-variable 'stgit-edit-patch) patch)
345 (setq default-directory dir)
346 (let ((standard-output edit-buf))
347 (stgit-run-silent "edit" "--save-template=-" patch))))
348
349 (defun stgit-confirm-edit ()
350 (interactive)
351 (let ((file (make-temp-file "stgit-edit-")))
352 (write-region (point-min) (point-max) file)
353 (stgit-capture-output nil
354 (stgit-run "edit" "-f" file stgit-edit-patch))
355 (with-current-buffer log-edit-parent-buffer
356 (stgit-reload))))
357
358 (defun stgit-new ()
359 "Create a new patch"
360 (interactive)
361 (let ((edit-buf (get-buffer-create "*StGit edit*")))
362 (log-edit 'stgit-confirm-new t nil edit-buf)))
363
364 (defun stgit-confirm-new ()
365 (interactive)
366 (let ((file (make-temp-file "stgit-edit-")))
367 (write-region (point-min) (point-max) file)
368 (stgit-capture-output nil
369 (stgit-run "new" "-f" file))
370 (with-current-buffer log-edit-parent-buffer
371 (stgit-reload))))
372
373 (defun stgit-create-patch-name (description)
374 "Create a patch name from a long description"
375 (let ((patch ""))
376 (while (> (length description) 0)
377 (cond ((string-match "\\`[a-zA-Z_-]+" description)
378 (setq patch (downcase (concat patch (match-string 0 description))))
379 (setq description (substring description (match-end 0))))
380 ((string-match "\\` +" description)
381 (setq patch (concat patch "-"))
382 (setq description (substring description (match-end 0))))
383 ((string-match "\\`[^a-zA-Z_-]+" description)
384 (setq description (substring description (match-end 0))))))
385 (cond ((= (length patch) 0)
386 "patch")
387 ((> (length patch) 20)
388 (substring patch 0 20))
389 (t patch))))
390
391 (defun stgit-delete (patch-names)
392 "Delete the named patches"
393 (interactive (list (stgit-patches-marked-or-at-point)))
394 (if (zerop (length patch-names))
395 (error "No patches to delete")
396 (when (yes-or-no-p (format "Really delete %d patches? "
397 (length patch-names)))
398 (stgit-capture-output nil
399 (apply 'stgit-run "delete" patch-names))
400 (stgit-reload))))
401
402 (defun stgit-coalesce (patch-names)
403 "Run stg coalesce on the named patches"
404 (interactive (list (stgit-marked-patches)))
405 (let ((edit-buf (get-buffer-create "*StGit edit*"))
406 (dir default-directory))
407 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
408 (set (make-local-variable 'stgit-patches) patch-names)
409 (setq default-directory dir)
410 (let ((standard-output edit-buf))
411 (apply 'stgit-run-silent "coalesce" "--save-template=-" patch-names))))
412
413 (defun stgit-confirm-coalesce ()
414 (interactive)
415 (let ((file (make-temp-file "stgit-edit-")))
416 (write-region (point-min) (point-max) file)
417 (stgit-capture-output nil
418 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
419 (with-current-buffer log-edit-parent-buffer
420 (stgit-clear-marks)
421 ;; Go to first marked patch and stay there
422 (goto-char (point-min))
423 (re-search-forward (concat "^[>+-]\\*") nil t)
424 (move-to-column goal-column)
425 (let ((pos (point)))
426 (stgit-reload)
427 (goto-char pos)))))
428
429 (defun stgit-help ()
430 "Display help for the StGit mode."
431 (interactive)
432 (describe-function 'stgit-mode))
433
434 (defun stgit-undo (&optional arg)
435 "Run stg undo.
436 With prefix argument, run it with the --hard flag."
437 (interactive "P")
438 (stgit-capture-output nil
439 (if arg
440 (stgit-run "undo" "--hard")
441 (stgit-run "undo")))
442 (stgit-reload))
443
444 (defun stgit-refresh (&optional arg)
445 "Run stg refresh.
446 With prefix argument, refresh the patch under point."
447 (interactive "P")
448 (let ((patchargs (if arg
449 (let ((patches (stgit-patches-marked-or-at-point)))
450 (cond ((null patches)
451 (error "no patch to update"))
452 ((> (length patches) 1)
453 (error "too many patches selected"))
454 (t
455 (cons "-p" patches))))
456 nil)))
457 (stgit-capture-output nil
458 (apply 'stgit-run "refresh" patchargs)))
459 (stgit-reload))
460
461 (provide 'stgit)