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'
13 "Manage stgit patches"
14 (interactive "DDirectory: \n")
15 (switch-to-stgit-buffer (git-get-top-dir dir))
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
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"))))))
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)))
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
40 (create-stgit-buffer dir)))))
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)
50 (setq buffer-read-only t))
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
60 (setq default-directory stgit-dir)
61 (setq buffer-read-only t))
62 (let ((standard-output output-buf))
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)
71 (defun stgit-run-silent (&rest args)
72 (apply 'call-process "stg" nil standard-output nil args))
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)))
80 (defun stgit-reload ()
81 "Update the contents of the stgit buffer"
83 (let ((inhibit-read-only t)
84 (curline (line-number-at-pos))
85 (curpatch (stgit-patch-at-point)))
88 (stgit-run-silent "branch")
89 (stgit-run-silent "series" "--description")
92 (stgit-goto-patch curpatch)
93 (goto-line curline))))
95 (defface stgit-description-face
96 '((((background dark)) (:foreground "tan"))
97 (((background light)) (:foreground "dark red")))
98 "The face used for StGit desriptions")
100 (defface stgit-top-patch-face
101 '((((background dark)) (:weight bold :foreground "yellow"))
102 (((background light)) (:weight bold :foreground "purple"))
104 "The face used for the top patch names")
106 (defface stgit-applied-patch-face
107 '((((background dark)) (:foreground "light yellow"))
108 (((background light)) (:foreground "purple"))
110 "The face used for applied patch names")
112 (defface stgit-unapplied-patch-face
113 '((((background dark)) (:foreground "gray80"))
114 (((background light)) (:foreground "orchid"))
116 "The face used for unapplied patch names")
118 (defun stgit-rescan ()
119 "Rescan the status buffer."
122 (goto-char (point-min))
124 (cond ((looking-at "Branch: \\(.*\\)")
125 (put-text-property (match-beginning 1) (match-end 1)
127 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
128 (let ((state (match-string 1))
129 (patchsym (intern (match-string 3))))
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"))
143 (insert "Run M-x stgit-init to initialise")))
145 (setq stgit-marked-patches (nreverse marked)))))
147 (defvar stgit-mode-hook nil
148 "Run after `stgit-mode' is setup.")
150 (defvar stgit-mode-map nil
151 "Keymap for StGit major mode.")
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))
181 "Major mode for interacting with StGit.
184 (kill-all-local-variables)
185 (buffer-disable-undo)
186 (setq mode-name "StGit"
187 major-mode 'stgit-mode
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))
195 (defun stgit-add-mark (patch)
196 (let ((patchsym (intern patch)))
197 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
199 (defun stgit-remove-mark (patch)
200 (let ((patchsym (intern patch)))
201 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
203 (defun stgit-clear-marks ()
204 (setq stgit-marked-patches '()))
206 (defun stgit-marked-patches ()
207 "Return the names of the marked patches."
208 (mapcar 'symbol-name stgit-marked-patches))
210 (defun stgit-patch-at-point ()
211 "Return the patch name on the current line"
214 (if (looking-at "[>+-][ *]\\([^ ]*\\)")
215 (match-string-no-properties 1)
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)))
227 (defun stgit-goto-patch (patch)
228 "Move point to the line containing PATCH"
230 (goto-char (point-min))
231 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
232 (progn (move-to-column goal-column)
240 (stgit-capture-output nil
245 "Mark the patch under point"
247 (let ((patch (stgit-patch-at-point)))
248 (stgit-add-mark patch)
252 (defun stgit-unmark ()
253 "Mark the patch on the previous line"
256 (let ((patch (stgit-patch-at-point)))
257 (stgit-remove-mark patch)
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)))
265 (error "No patch on this line"))
266 (stgit-capture-output nil
267 (stgit-run "rename" old-name name))
269 (stgit-goto-patch name)))
271 (defun stgit-repair ()
274 (stgit-capture-output nil
275 (stgit-run "repair"))
278 (defun stgit-commit ()
281 (stgit-capture-output nil (stgit-run "commit"))
284 (defun stgit-uncommit (arg)
285 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
287 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
290 (defun stgit-push-next (npatches)
291 "Push the first unapplied patch.
292 With numeric prefix argument, push that many patches."
294 (stgit-capture-output nil (stgit-run "push" "-n"
295 (number-to-string npatches)))
298 (defun stgit-pop-next (npatches)
299 "Pop the topmost applied patch.
300 With numeric prefix argument, pop that many patches."
302 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
305 (defun stgit-applied-at-point ()
306 "Is the patch on the current line applied?"
309 (looking-at "[>+]")))
311 (defun stgit-push-or-pop ()
312 "Push or pop the patch on the current line"
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))
321 "Go to the patch on the current line"
323 (let ((patch (stgit-patch-at-point)))
324 (stgit-capture-output nil
325 (stgit-run "goto" patch))
329 "Show the patch on the current line"
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))
338 "Edit the patch on the current line"
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))))
349 (defun stgit-confirm-edit ()
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
361 (let ((edit-buf (get-buffer-create "*StGit edit*")))
362 (log-edit 'stgit-confirm-new t nil edit-buf)))
364 (defun stgit-confirm-new ()
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
373 (defun stgit-create-patch-name (description)
374 "Create a patch name from a long description"
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)
387 ((> (length patch) 20)
388 (substring patch 0 20))
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))
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))))
413 (defun stgit-confirm-coalesce ()
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
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)
430 "Display help for the StGit mode."
432 (describe-function 'stgit-mode))
434 (defun stgit-undo (&optional arg)
436 With prefix argument, run it with the --hard flag."
438 (stgit-capture-output nil
440 (stgit-run "undo" "--hard")
444 (defun stgit-refresh (&optional arg)
446 With prefix argument, refresh the patch under point."
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"))
455 (cons "-p" patches))))
457 (stgit-capture-output nil
458 (apply 'stgit-run "refresh" patchargs)))