1 ;; stgit.el: An emacs mode for StGit
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
10 ;; To start: `M-x stgit'
15 "Manage stgit patches"
16 (interactive "DDirectory: \n")
17 (switch-to-stgit-buffer (git-get-top-dir dir))
20 (defun git-get-top-dir (dir)
21 "Retrieve the top-level directory of a git tree."
22 (let ((cdup (with-output-to-string
23 (with-current-buffer standard-output
25 (unless (eq 0 (call-process "git" nil t nil
26 "rev-parse" "--show-cdup"))
27 (error "cannot find top-level git tree for %s." dir))))))
28 (expand-file-name (concat (file-name-as-directory dir)
29 (car (split-string cdup "\n"))))))
31 (defun switch-to-stgit-buffer (dir)
32 "Switch to a (possibly new) buffer displaying StGit patches for DIR"
33 (setq dir (file-name-as-directory dir))
34 (let ((buffers (buffer-list)))
36 (not (with-current-buffer (car buffers)
37 (and (eq major-mode 'stgit-mode)
38 (string= default-directory dir)))))
39 (setq buffers (cdr buffers)))
40 (switch-to-buffer (if buffers
42 (create-stgit-buffer dir)))))
44 (defun create-stgit-buffer (dir)
45 "Create a buffer for showing StGit patches.
46 Argument DIR is the repository path."
47 (let ((buf (create-file-buffer (concat dir "*stgit*")))
48 (inhibit-read-only t))
49 (with-current-buffer buf
50 (setq default-directory dir)
52 (setq buffer-read-only t))
55 (defmacro stgit-capture-output (name &rest body)
56 "Capture StGit output and show it in a window at the end"
57 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
58 (stgit-dir default-directory)
59 (inhibit-read-only t))
60 (with-current-buffer output-buf
62 (setq default-directory stgit-dir)
63 (setq buffer-read-only t))
64 (let ((standard-output output-buf))
66 (with-current-buffer output-buf
67 (set-buffer-modified-p nil)
68 (setq buffer-read-only t)
69 (if (< (point-min) (point-max))
70 (display-buffer output-buf t)))))
71 (put 'stgit-capture-output 'lisp-indent-function 1)
73 (defun stgit-run-silent (&rest args)
74 (apply 'call-process "stg" nil standard-output nil args))
76 (defun stgit-run (&rest args)
77 (let ((msgcmd (mapconcat #'identity args " ")))
78 (message "Running stg %s..." msgcmd)
79 (apply 'call-process "stg" nil standard-output nil args)
80 (message "Running stg %s...done" msgcmd)))
82 (defun stgit-reload ()
83 "Update the contents of the stgit buffer"
85 (let ((inhibit-read-only t)
86 (curline (line-number-at-pos))
87 (curpatch (stgit-patch-at-point)))
90 (stgit-run-silent "branch")
91 (stgit-run-silent "series" "--description")
94 (stgit-goto-patch curpatch)
95 (goto-line curline))))
97 (defface stgit-description-face
98 '((((background dark)) (:foreground "tan"))
99 (((background light)) (:foreground "dark red")))
100 "The face used for StGit desriptions")
102 (defface stgit-top-patch-face
103 '((((background dark)) (:weight bold :foreground "yellow"))
104 (((background light)) (:weight bold :foreground "purple"))
106 "The face used for the top patch names")
108 (defface stgit-applied-patch-face
109 '((((background dark)) (:foreground "light yellow"))
110 (((background light)) (:foreground "purple"))
112 "The face used for applied patch names")
114 (defface stgit-unapplied-patch-face
115 '((((background dark)) (:foreground "gray80"))
116 (((background light)) (:foreground "orchid"))
118 "The face used for unapplied patch names")
120 (defun stgit-rescan ()
121 "Rescan the status buffer."
124 (goto-char (point-min))
126 (cond ((looking-at "Branch: \\(.*\\)")
127 (put-text-property (match-beginning 1) (match-end 1)
129 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
130 (let ((state (match-string 1))
131 (patchsym (intern (match-string 3))))
133 (match-beginning 3) (match-end 3) 'face
134 (cond ((string= state ">") 'stgit-top-patch-face)
135 ((string= state "+") 'stgit-applied-patch-face)
136 ((string= state "-") 'stgit-unapplied-patch-face)))
137 (put-text-property (match-beginning 4) (match-end 4)
138 'face 'stgit-description-face)
139 (when (memq patchsym stgit-marked-patches)
140 (replace-match "*" nil nil nil 2)
141 (setq marked (cons patchsym marked)))))
142 ((or (looking-at "stg series: Branch \".*\" not initialised")
143 (looking-at "stg series: .*: branch not initialized"))
145 (insert "Run M-x stgit-init to initialise")))
147 (setq stgit-marked-patches (nreverse marked)))))
150 "Hide the stgit buffer"
154 (defun stgit-git-status ()
155 "Show status using `git-status'"
157 (unless (fboundp 'git-status)
158 (error "stgit-git-status requires git-status"))
159 (let ((dir default-directory))
160 (save-selected-window
164 (defvar stgit-mode-hook nil
165 "Run after `stgit-mode' is setup.")
167 (defvar stgit-mode-map nil
168 "Keymap for StGit major mode.")
170 (unless stgit-mode-map
171 (setq stgit-mode-map (make-keymap))
172 (suppress-keymap stgit-mode-map)
173 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
176 ("\d" . stgit-unmark-up)
177 ("u" . stgit-unmark-down)
180 ("p" . previous-line)
182 ("s" . stgit-git-status)
184 ("r" . stgit-refresh)
185 ("\C-c\C-r" . stgit-rename)
187 ("c" . stgit-coalesce)
191 ("U" . stgit-uncommit)
192 (">" . stgit-push-next)
193 ("<" . stgit-pop-next)
194 ("P" . stgit-push-or-pop)
198 ([(control ?/)] . stgit-undo)
199 ("\C-_" . stgit-undo)
200 ("q" . stgit-quit))))
203 "Major mode for interacting with StGit.
206 (kill-all-local-variables)
207 (buffer-disable-undo)
208 (setq mode-name "StGit"
209 major-mode 'stgit-mode
211 (use-local-map stgit-mode-map)
212 (set (make-local-variable 'list-buffers-directory) default-directory)
213 (set (make-local-variable 'stgit-marked-patches) nil)
214 (set-variable 'truncate-lines 't)
215 (run-hooks 'stgit-mode-hook))
217 (defun stgit-add-mark (patch)
218 (let ((patchsym (intern patch)))
219 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
221 (defun stgit-remove-mark (patch)
222 (let ((patchsym (intern patch)))
223 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
225 (defun stgit-clear-marks ()
226 (setq stgit-marked-patches '()))
228 (defun stgit-marked-patches ()
229 "Return the names of the marked patches."
230 (mapcar 'symbol-name stgit-marked-patches))
232 (defun stgit-patch-at-point ()
233 "Return the patch name on the current line"
236 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
237 (match-string-no-properties 1)
240 (defun stgit-patches-marked-or-at-point ()
241 "Return the names of the marked patches, or the patch on the current line."
242 (if stgit-marked-patches
243 (stgit-marked-patches)
244 (let ((patch (stgit-patch-at-point)))
249 (defun stgit-goto-patch (patch)
250 "Move point to the line containing PATCH"
252 (goto-char (point-min))
253 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
254 (progn (move-to-column goal-column)
262 (stgit-capture-output nil
267 "Mark the patch under point"
269 (let ((patch (stgit-patch-at-point)))
270 (stgit-add-mark patch)
274 (defun stgit-unmark-up ()
275 "Remove mark from the patch on the previous line"
278 (stgit-remove-mark (stgit-patch-at-point))
281 (defun stgit-unmark-down ()
282 "Remove mark from the patch on the current line"
284 (stgit-remove-mark (stgit-patch-at-point))
288 (defun stgit-rename (name)
289 "Rename the patch under point"
290 (interactive (list (read-string "Patch name: " (stgit-patch-at-point))))
291 (let ((old-name (stgit-patch-at-point)))
293 (error "No patch on this line"))
294 (stgit-capture-output nil
295 (stgit-run "rename" old-name name))
297 (stgit-goto-patch name)))
299 (defun stgit-repair ()
302 (stgit-capture-output nil
303 (stgit-run "repair"))
306 (defun stgit-commit ()
309 (stgit-capture-output nil (stgit-run "commit"))
312 (defun stgit-uncommit (arg)
313 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
315 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
318 (defun stgit-push-next (npatches)
319 "Push the first unapplied patch.
320 With numeric prefix argument, push that many patches."
322 (stgit-capture-output nil (stgit-run "push" "-n"
323 (number-to-string npatches)))
326 (defun stgit-pop-next (npatches)
327 "Pop the topmost applied patch.
328 With numeric prefix argument, pop that many patches."
330 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
333 (defun stgit-applied-at-point ()
334 "Is the patch on the current line applied?"
337 (looking-at "[>+]")))
339 (defun stgit-push-or-pop ()
340 "Push or pop the patch on the current line"
342 (let ((patch (stgit-patch-at-point))
343 (applied (stgit-applied-at-point)))
344 (stgit-capture-output nil
345 (stgit-run (if applied "pop" "push") patch))
349 "Go to the patch on the current line"
351 (let ((patch (stgit-patch-at-point)))
352 (stgit-capture-output nil
353 (stgit-run "goto" patch))
357 "Show the patch on the current line"
359 (stgit-capture-output "*StGit patch*"
360 (stgit-run "show" (stgit-patch-at-point))
361 (with-current-buffer standard-output
362 (goto-char (point-min))
366 "Edit the patch on the current line"
368 (let ((patch (stgit-patch-at-point))
369 (edit-buf (get-buffer-create "*StGit edit*"))
370 (dir default-directory))
371 (log-edit 'stgit-confirm-edit t nil edit-buf)
372 (set (make-local-variable 'stgit-edit-patch) patch)
373 (setq default-directory dir)
374 (let ((standard-output edit-buf))
375 (stgit-run-silent "edit" "--save-template=-" patch))))
377 (defun stgit-confirm-edit ()
379 (let ((file (make-temp-file "stgit-edit-")))
380 (write-region (point-min) (point-max) file)
381 (stgit-capture-output nil
382 (stgit-run "edit" "-f" file stgit-edit-patch))
383 (with-current-buffer log-edit-parent-buffer
389 (let ((edit-buf (get-buffer-create "*StGit edit*")))
390 (log-edit 'stgit-confirm-new t nil edit-buf)))
392 (defun stgit-confirm-new ()
394 (let ((file (make-temp-file "stgit-edit-")))
395 (write-region (point-min) (point-max) file)
396 (stgit-capture-output nil
397 (stgit-run "new" "-f" file))
398 (with-current-buffer log-edit-parent-buffer
401 (defun stgit-create-patch-name (description)
402 "Create a patch name from a long description"
404 (while (> (length description) 0)
405 (cond ((string-match "\\`[a-zA-Z_-]+" description)
406 (setq patch (downcase (concat patch (match-string 0 description))))
407 (setq description (substring description (match-end 0))))
408 ((string-match "\\` +" description)
409 (setq patch (concat patch "-"))
410 (setq description (substring description (match-end 0))))
411 ((string-match "\\`[^a-zA-Z_-]+" description)
412 (setq description (substring description (match-end 0))))))
413 (cond ((= (length patch) 0)
415 ((> (length patch) 20)
416 (substring patch 0 20))
419 (defun stgit-delete (patch-names)
420 "Delete the named patches"
421 (interactive (list (stgit-patches-marked-or-at-point)))
422 (if (zerop (length patch-names))
423 (error "No patches to delete")
424 (when (yes-or-no-p (format "Really delete %d patches? "
425 (length patch-names)))
426 (stgit-capture-output nil
427 (apply 'stgit-run "delete" patch-names))
430 (defun stgit-coalesce (patch-names)
431 "Run stg coalesce on the named patches"
432 (interactive (list (stgit-marked-patches)))
433 (let ((edit-buf (get-buffer-create "*StGit edit*"))
434 (dir default-directory))
435 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
436 (set (make-local-variable 'stgit-patches) patch-names)
437 (setq default-directory dir)
438 (let ((standard-output edit-buf))
439 (apply 'stgit-run-silent "coalesce" "--save-template=-" patch-names))))
441 (defun stgit-confirm-coalesce ()
443 (let ((file (make-temp-file "stgit-edit-")))
444 (write-region (point-min) (point-max) file)
445 (stgit-capture-output nil
446 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
447 (with-current-buffer log-edit-parent-buffer
449 ;; Go to first marked patch and stay there
450 (goto-char (point-min))
451 (re-search-forward (concat "^[>+-]\\*") nil t)
452 (move-to-column goal-column)
458 "Display help for the StGit mode."
460 (describe-function 'stgit-mode))
462 (defun stgit-undo (&optional arg)
464 With prefix argument, run it with the --hard flag."
466 (stgit-capture-output nil
468 (stgit-run "undo" "--hard")
472 (defun stgit-refresh (&optional arg)
474 With prefix argument, refresh the patch under point."
476 (let ((patchargs (if arg
477 (let ((patches (stgit-patches-marked-or-at-point)))
478 (cond ((null patches)
479 (error "no patch to update"))
480 ((> (length patches) 1)
481 (error "too many patches selected"))
483 (cons "-p" patches))))
485 (stgit-capture-output nil
486 (apply 'stgit-run "refresh" patchargs)))