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 for the tree in DIR."
16 (interactive "DDirectory: \n")
17 (switch-to-stgit-buffer (git-get-top-dir dir))
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
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")))))))
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))))))
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)))
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
54 (create-stgit-buffer dir)))))
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)
64 (setq buffer-read-only t))
67 (defmacro stgit-capture-output (name &rest body)
68 "Capture StGit output and show it in a window at the end."
69 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
70 (stgit-dir default-directory)
71 (inhibit-read-only t))
72 (with-current-buffer output-buf
74 (setq default-directory stgit-dir)
75 (setq buffer-read-only t))
76 (let ((standard-output output-buf))
78 (with-current-buffer output-buf
79 (set-buffer-modified-p nil)
80 (setq buffer-read-only t)
81 (if (< (point-min) (point-max))
82 (display-buffer output-buf t)))))
83 (put 'stgit-capture-output 'lisp-indent-function 1)
85 (defun stgit-make-run-args (args)
86 "Return a copy of ARGS with its elements converted to strings."
88 ;; don't use (format "%s" ...) to limit type errors
90 ((integerp x) (number-to-string x))
91 ((symbolp x) (symbol-name x))
93 (error "Bad element in stgit-make-run-args args: %S" x))))
96 (defun stgit-run-silent (&rest args)
97 (setq args (stgit-make-run-args args))
98 (apply 'call-process "stg" nil standard-output nil args))
100 (defun stgit-run (&rest args)
101 (setq args (stgit-make-run-args args))
102 (let ((msgcmd (mapconcat #'identity args " ")))
103 (message "Running stg %s..." msgcmd)
104 (apply 'call-process "stg" nil standard-output nil args)
105 (message "Running stg %s...done" msgcmd)))
107 (defun stgit-run-git (&rest args)
108 (setq args (stgit-make-run-args args))
109 (let ((msgcmd (mapconcat #'identity args " ")))
110 (message "Running git %s..." msgcmd)
111 (apply 'call-process "git" nil standard-output nil args)
112 (message "Running git %s...done" msgcmd)))
114 (defun stgit-run-git-silent (&rest args)
115 (setq args (stgit-make-run-args args))
116 (apply 'call-process "git" nil standard-output nil args))
118 (defun stgit-reload ()
119 "Update the contents of the StGit buffer."
121 (let ((inhibit-read-only t)
122 (curline (line-number-at-pos))
123 (curpatch (stgit-patch-at-point)))
126 (stgit-run-silent "branch")
127 (stgit-run-silent "series" "--description" "--empty")
130 (stgit-goto-patch curpatch)
131 (goto-line curline)))
132 (stgit-refresh-git-status))
135 "A user interface for the StGit patch maintenance tool."
138 (defface stgit-description-face
139 '((((background dark)) (:foreground "tan"))
140 (((background light)) (:foreground "dark red")))
141 "The face used for StGit descriptions"
144 (defface stgit-top-patch-face
145 '((((background dark)) (:weight bold :foreground "yellow"))
146 (((background light)) (:weight bold :foreground "purple"))
148 "The face used for the top patch names"
151 (defface stgit-applied-patch-face
152 '((((background dark)) (:foreground "light yellow"))
153 (((background light)) (:foreground "purple"))
155 "The face used for applied patch names"
158 (defface stgit-unapplied-patch-face
159 '((((background dark)) (:foreground "gray80"))
160 (((background light)) (:foreground "orchid"))
162 "The face used for unapplied patch names"
165 (defface stgit-modified-file-face
166 '((((class color) (background light)) (:foreground "purple"))
167 (((class color) (background dark)) (:foreground "salmon")))
168 "StGit mode face used for modified file status"
171 (defface stgit-unmerged-file-face
172 '((((class color) (background light)) (:foreground "red" :bold t))
173 (((class color) (background dark)) (:foreground "red" :bold t)))
174 "StGit mode face used for unmerged file status"
177 (defface stgit-unknown-file-face
178 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
179 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
180 "StGit mode face used for unknown file status"
183 (defface stgit-file-permission-face
184 '((((class color) (background light)) (:foreground "green" :bold t))
185 (((class color) (background dark)) (:foreground "green" :bold t)))
186 "StGit mode face used for permission changes."
189 (defcustom stgit-expand-find-copies-harder
191 "Try harder to find copied files when listing patches.
193 When not nil, runs git diff-tree with the --find-copies-harder
194 flag, which reduces performance."
198 (defconst stgit-file-status-code-strings
199 (mapcar (lambda (arg)
201 (propertize (cadr arg) 'face (car (cddr arg)))))
202 '((add "Added" stgit-modified-file-face)
203 (copy "Copied" stgit-modified-file-face)
204 (delete "Deleted" stgit-modified-file-face)
205 (modify "Modified" stgit-modified-file-face)
206 (rename "Renamed" stgit-modified-file-face)
207 (mode-change "Mode change" stgit-modified-file-face)
208 (unmerged "Unmerged" stgit-unmerged-file-face)
209 (unknown "Unknown" stgit-unknown-file-face)))
210 "Alist of code symbols to description strings")
212 (defun stgit-file-status-code-as-string (code)
213 "Return stgit status code as string"
214 (let ((str (assq (if (consp code) (car code) code)
215 stgit-file-status-code-strings)))
218 (if (and str (consp code) (/= (cdr code) 100))
219 (format "%s %s" (cdr str)
220 (propertize (format "%d%%" (cdr code))
221 'face 'stgit-description-face))
224 (defun stgit-file-status-code (str &optional score)
225 "Return stgit status code from git status string"
226 (let ((code (assoc str '(("A" . add)
234 (setq code (if code (cdr code) 'unknown))
235 (when (stringp score)
236 (if (> (length score) 0)
237 (setq score (string-to-number score))
239 (if score (cons code score) code)))
241 (defconst stgit-file-type-strings
244 (#o160 . "subproject"))
245 "Alist of names of file types")
247 (defun stgit-file-type-string (type)
248 "Return string describing file type TYPE (the high bits of file permission).
249 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
250 (let ((type-str (assoc type stgit-file-type-strings)))
251 (or (and type-str (cdr type-str))
252 (format "unknown type %o" type))))
254 (defun stgit-file-type-change-string (old-perm new-perm)
255 "Return string describing file type change from OLD-PERM to NEW-PERM.
256 Cf. `stgit-file-type-string'."
257 (let ((old-type (lsh old-perm -9))
258 (new-type (lsh new-perm -9)))
259 (cond ((= old-type new-type) "")
260 ((zerop new-type) "")
262 (if (= new-type #o100)
264 (format " (%s)" (stgit-file-type-string new-type))))
265 (t (format " (%s -> %s)"
266 (stgit-file-type-string old-type)
267 (stgit-file-type-string new-type))))))
269 (defun stgit-file-mode-change-string (old-perm new-perm)
270 "Return string describing file mode change from OLD-PERM to NEW-PERM.
271 Cf. `stgit-file-type-change-string'."
272 (setq old-perm (logand old-perm #o777)
273 new-perm (logand new-perm #o777))
274 (if (or (= old-perm new-perm)
278 (let* ((modified (logxor old-perm new-perm))
279 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
280 (cond ((zerop modified) "")
281 ((and (zerop not-x-modified)
282 (or (and (eq #o111 (logand old-perm #o111))
283 (propertize "-x" 'face 'stgit-file-permission-face))
284 (and (eq #o111 (logand new-perm #o111))
285 (propertize "+x" 'face
286 'stgit-file-permission-face)))))
287 (t (concat (propertize (format "%o" old-perm)
288 'face 'stgit-file-permission-face)
290 'face 'stgit-description-face)
291 (propertize (format "%o" new-perm)
292 'face 'stgit-file-permission-face)))))))
294 (defun stgit-expand-patch (patchsym)
295 "Expand (show modification of) the patch with name PATCHSYM (a
297 `stgit-expand-find-copies-harder' controls how hard to try to
301 (let* ((start (point))
302 (result (with-output-to-string
303 (stgit-run-git "diff-tree" "-r" "-z"
304 (if stgit-expand-find-copies-harder
305 "--find-copies-harder"
307 (stgit-id patchsym)))))
309 (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]*\\)\\)"
311 (let ((copy-or-rename (match-string 4 result))
312 (old-perm (read (format "#o%s" (match-string 1 result))))
313 (new-perm (read (format "#o%s" (match-string 2 result))))
320 (let ((cr-score (match-string 5 result))
321 (cr-from-file (match-string 6 result))
322 (cr-to-file (match-string 7 result)))
323 (setq status (stgit-file-status-code copy-or-rename
325 properties (list 'stgit-old-file cr-from-file
326 'stgit-new-file cr-to-file)
330 'face 'stgit-description-face)
332 (setq status (stgit-file-status-code (match-string 8 result))
333 properties (list 'stgit-file (match-string 9 result))
334 change (match-string 9 result)))
336 (let ((mode-change (stgit-file-mode-change-string old-perm
338 (insert (format "%-12s" (stgit-file-status-code-as-string
341 (if (> (length mode-change) 0) " " "")
343 (propertize (stgit-file-type-change-string old-perm
345 'face 'stgit-description-face)
347 (add-text-properties line-start (point) properties))
348 (setq mstart (match-end 0))))
349 (when (= start (point))
350 (insert " <no files>\n"))
351 (put-text-property start (point) 'stgit-file-patchsym patchsym))))
353 (defun stgit-rescan ()
354 "Rescan the status buffer."
358 (goto-char (point-min))
360 (cond ((looking-at "Branch: \\(.*\\)")
361 (put-text-property (match-beginning 1) (match-end 1)
363 ((looking-at "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
365 (let ((empty (match-string 1))
366 (state (match-string 2))
367 (patchsym (intern (match-string 4))))
369 (match-beginning 4) (match-end 4) 'face
370 (cond ((string= state ">") 'stgit-top-patch-face)
371 ((string= state "+") 'stgit-applied-patch-face)
372 ((string= state "-") 'stgit-unapplied-patch-face)))
373 (put-text-property (match-beginning 5) (match-end 5)
374 'face 'stgit-description-face)
375 (when (memq patchsym stgit-marked-patches)
377 (replace-match "*" nil nil nil 3))
378 (setq marked (cons patchsym marked)))
379 (put-text-property (match-beginning 0) (match-end 0)
380 'stgit-patchsym patchsym)
381 (when (memq patchsym stgit-expanded-patches)
382 (stgit-expand-patch patchsym))
383 (when (equal "0" empty)
385 (goto-char (match-beginning 5))
386 (insert "(empty) ")))
389 ((or (looking-at "stg series: Branch \".*\" not initialised")
390 (looking-at "stg series: .*: branch not initialized"))
393 (insert "Run M-x stgit-init to initialise")))
395 (setq stgit-marked-patches (nreverse marked))
398 (propertize "no patches in series"
399 'face 'stgit-description-face))))))
401 (defun stgit-select ()
402 "Expand or collapse the current entry"
404 (let ((curpatch (stgit-patch-at-point)))
406 (let ((patched-file (stgit-patched-file-at-point)))
408 (error "No patch or file on the current line"))
409 (let ((filename (expand-file-name (cdr patched-file))))
410 (unless (file-exists-p filename)
411 (error "File does not exist"))
412 (find-file filename)))
413 (setq stgit-expanded-patches
414 (if (memq curpatch stgit-expanded-patches)
415 (delq curpatch stgit-expanded-patches)
416 (cons curpatch stgit-expanded-patches)))
419 (defun stgit-find-file-other-window ()
420 "Open file at point in other window"
422 (let ((patched-file (stgit-patched-file-at-point)))
424 (error "No file on the current line"))
425 (let ((filename (expand-file-name (cdr patched-file))))
426 (unless (file-exists-p filename)
427 (error "File does not exist"))
428 (find-file-other-window filename))))
431 "Hide the stgit buffer."
435 (defun stgit-git-status ()
436 "Show status using `git-status'."
438 (unless (fboundp 'git-status)
439 (error "The stgit-git-status command requires git-status"))
440 (let ((dir default-directory))
441 (save-selected-window
445 (defun stgit-next-line (&optional arg try-vscroll)
446 "Move cursor vertically down ARG lines"
448 (next-line arg try-vscroll)
449 (when (looking-at " \\S-")
452 (defun stgit-previous-line (&optional arg try-vscroll)
453 "Move cursor vertically up ARG lines"
455 (previous-line arg try-vscroll)
456 (when (looking-at " \\S-")
459 (defun stgit-next-patch (&optional arg)
460 "Move cursor down ARG patches"
465 (stgit-previous-patch (- arg))
466 (while (not (zerop arg))
468 (while (progn (stgit-next-line)
469 (not (stgit-patch-at-point)))))))
471 (defun stgit-previous-patch (&optional arg)
472 "Move cursor up ARG patches"
477 (stgit-next-patch (- arg))
478 (while (not (zerop arg))
480 (while (progn (stgit-previous-line)
481 (not (stgit-patch-at-point)))))))
483 (defvar stgit-mode-hook nil
484 "Run after `stgit-mode' is setup.")
486 (defvar stgit-mode-map nil
487 "Keymap for StGit major mode.")
489 (unless stgit-mode-map
490 (setq stgit-mode-map (make-keymap))
491 (suppress-keymap stgit-mode-map)
492 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
495 ("\d" . stgit-unmark-up)
496 ("u" . stgit-unmark-down)
499 ("p" . stgit-previous-line)
500 ("n" . stgit-next-line)
501 ("\C-p" . stgit-previous-patch)
502 ("\C-n" . stgit-next-patch)
503 ("\M-{" . stgit-previous-patch)
504 ("\M-}" . stgit-next-patch)
505 ("s" . stgit-git-status)
507 ("r" . stgit-refresh)
508 ("\C-c\C-r" . stgit-rename)
510 ("c" . stgit-coalesce)
514 ("U" . stgit-uncommit)
515 ("\r" . stgit-select)
516 ("o" . stgit-find-file-other-window)
517 (">" . stgit-push-next)
518 ("<" . stgit-pop-next)
519 ("P" . stgit-push-or-pop)
523 ([(control ?/)] . stgit-undo)
524 ("\C-_" . stgit-undo)
525 ("q" . stgit-quit))))
528 "Major mode for interacting with StGit.
531 (kill-all-local-variables)
532 (buffer-disable-undo)
533 (setq mode-name "StGit"
534 major-mode 'stgit-mode
536 (use-local-map stgit-mode-map)
537 (set (make-local-variable 'list-buffers-directory) default-directory)
538 (set (make-local-variable 'stgit-marked-patches) nil)
539 (set (make-local-variable 'stgit-expanded-patches) nil)
540 (set-variable 'truncate-lines 't)
541 (run-hooks 'stgit-mode-hook))
543 (defun stgit-add-mark (patchsym)
544 "Mark the patch PATCHSYM."
545 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
547 (defun stgit-remove-mark (patchsym)
548 "Unmark the patch PATCHSYM."
549 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
551 (defun stgit-clear-marks ()
552 "Unmark all patches."
553 (setq stgit-marked-patches '()))
555 (defun stgit-patch-at-point (&optional cause-error allow-file)
556 "Return the patch name on the current line as a symbol.
557 If CAUSE-ERROR is not nil, signal an error if none found.
558 If ALLOW-FILE is not nil, also handle when point is on a file of
560 (or (get-text-property (point) 'stgit-patchsym)
562 (get-text-property (point) 'stgit-file-patchsym))
564 (error "No patch on this line"))))
566 (defun stgit-patched-file-at-point (&optional both-files)
567 "Returns a cons of the patchsym and file name at point. For
568 copies and renames, return the new file if the patch is either
569 applied. If BOTH-FILES is non-nil, return a cons of the old and
570 the new file names instead of just one name."
571 (let ((patchsym (get-text-property (point) 'stgit-file-patchsym))
572 (file (get-text-property (point) 'stgit-file)))
573 (cond ((not patchsym) nil)
574 (file (cons patchsym file))
576 (cons patchsym (cons (get-text-property (point) 'stgit-old-file)
577 (get-text-property (point) 'stgit-new-file))))
579 (let ((file-sym (save-excursion
580 (stgit-previous-patch)
581 (unless (eq (stgit-patch-at-point)
583 (error "Cannot find the %s patch" patchsym))
585 (if (= (char-after) ?-)
588 (cons patchsym (get-text-property (point) file-sym)))))))
590 (defun stgit-patches-marked-or-at-point ()
591 "Return the symbols of the marked patches, or the patch on the current line."
592 (if stgit-marked-patches
594 (let ((patch (stgit-patch-at-point)))
599 (defun stgit-goto-patch (patchsym)
600 "Move point to the line containing patch PATCHSYM.
601 If that patch cannot be found, return nil."
602 (let ((p (text-property-any (point-min) (point-max)
603 'stgit-patchsym patchsym)))
606 (move-to-column goal-column))))
611 (stgit-capture-output nil
616 "Mark the patch under point."
618 (let ((patch (stgit-patch-at-point t)))
619 (stgit-add-mark patch)
623 (defun stgit-unmark-up ()
624 "Remove mark from the patch on the previous line."
626 (stgit-previous-patch)
627 (stgit-remove-mark (stgit-patch-at-point t))
630 (defun stgit-unmark-down ()
631 "Remove mark from the patch on the current line."
633 (stgit-remove-mark (stgit-patch-at-point t))
637 (defun stgit-rename (name)
638 "Rename the patch under point to NAME."
639 (interactive (list (read-string "Patch name: "
640 (symbol-name (stgit-patch-at-point t)))))
641 (let ((old-patchsym (stgit-patch-at-point t)))
642 (stgit-capture-output nil
643 (stgit-run "rename" old-patchsym name))
644 (let ((name-sym (intern name)))
645 (when (memq old-patchsym stgit-expanded-patches)
646 (setq stgit-expanded-patches
647 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
648 (when (memq old-patchsym stgit-marked-patches)
649 (setq stgit-marked-patches
650 (cons name-sym (delq old-patchsym stgit-marked-patches))))
652 (stgit-goto-patch name-sym))))
654 (defun stgit-repair ()
657 (stgit-capture-output nil
658 (stgit-run "repair"))
661 (defun stgit-commit ()
664 (stgit-capture-output nil (stgit-run "commit"))
667 (defun stgit-uncommit (arg)
668 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
670 (stgit-capture-output nil (stgit-run "uncommit" "-n" arg))
673 (defun stgit-push-next (npatches)
674 "Push the first unapplied patch.
675 With numeric prefix argument, push that many patches."
677 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
679 (stgit-refresh-git-status))
681 (defun stgit-pop-next (npatches)
682 "Pop the topmost applied patch.
683 With numeric prefix argument, pop that many patches."
685 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
687 (stgit-refresh-git-status))
689 (defun stgit-applied-at-point ()
690 "Is the patch on the current line applied?"
693 (looking-at "[>+]")))
695 (defun stgit-push-or-pop ()
696 "Push or pop the patch on the current line."
698 (let ((patchsym (stgit-patch-at-point t))
699 (applied (stgit-applied-at-point)))
700 (stgit-capture-output nil
701 (stgit-run (if applied "pop" "push") patchsym))
705 "Go to the patch on the current line."
707 (let ((patchsym (stgit-patch-at-point t)))
708 (stgit-capture-output nil
709 (stgit-run "goto" patchsym))
712 (defun stgit-id (patchsym)
713 "Return the git commit id for PATCHSYM."
714 (let ((result (with-output-to-string
715 (stgit-run-silent "id" patchsym))))
716 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
717 (error "Cannot find commit id for %s" patchsym))
718 (match-string 1 result)))
721 "Show the patch on the current line."
723 (stgit-capture-output "*StGit patch*"
724 (let ((patchsym (stgit-patch-at-point)))
726 (let ((patched-file (stgit-patched-file-at-point t)))
728 (error "No patch or file at point"))
729 (let ((id (stgit-id (car patched-file))))
730 (if (consp (cdr patched-file))
731 ;; two files (copy or rename)
732 (stgit-run-git "diff" "-C" "-C" (concat id "^") id "--"
733 (cadr patched-file) (cddr patched-file))
735 (stgit-run-git "diff" (concat id "^") id "--"
736 (cdr patched-file)))))
737 (stgit-run "show" "-O" "--patch-with-stat" patchsym))
738 (with-current-buffer standard-output
739 (goto-char (point-min))
743 "Edit the patch on the current line."
745 (let ((patchsym (stgit-patch-at-point t))
746 (edit-buf (get-buffer-create "*StGit edit*"))
747 (dir default-directory))
748 (log-edit 'stgit-confirm-edit t nil edit-buf)
749 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
750 (setq default-directory dir)
751 (let ((standard-output edit-buf))
752 (stgit-run-silent "edit" "--save-template=-" patchsym))))
754 (defun stgit-confirm-edit ()
756 (let ((file (make-temp-file "stgit-edit-")))
757 (write-region (point-min) (point-max) file)
758 (stgit-capture-output nil
759 (stgit-run "edit" "-f" file stgit-edit-patchsym))
760 (with-current-buffer log-edit-parent-buffer
763 (defun stgit-new (add-sign)
765 With a prefix argument, include a \"Signed-off-by:\" line at the
768 (let ((edit-buf (get-buffer-create "*StGit edit*"))
769 (dir default-directory))
770 (log-edit 'stgit-confirm-new t nil edit-buf)
771 (setq default-directory dir)
774 (let ((standard-output (current-buffer)))
775 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
777 (defun stgit-confirm-new ()
779 (let ((file (make-temp-file "stgit-edit-")))
780 (write-region (point-min) (point-max) file)
781 (stgit-capture-output nil
782 (stgit-run "new" "-f" file))
783 (with-current-buffer log-edit-parent-buffer
786 (defun stgit-create-patch-name (description)
787 "Create a patch name from a long description"
789 (while (> (length description) 0)
790 (cond ((string-match "\\`[a-zA-Z_-]+" description)
791 (setq patch (downcase (concat patch
792 (match-string 0 description))))
793 (setq description (substring description (match-end 0))))
794 ((string-match "\\` +" description)
795 (setq patch (concat patch "-"))
796 (setq description (substring description (match-end 0))))
797 ((string-match "\\`[^a-zA-Z_-]+" description)
798 (setq description (substring description (match-end 0))))))
799 (cond ((= (length patch) 0)
801 ((> (length patch) 20)
802 (substring patch 0 20))
805 (defun stgit-delete (patchsyms)
806 "Delete the patches in PATCHSYMS.
807 Interactively, delete the marked patches, or the patch at point."
808 (interactive (list (stgit-patches-marked-or-at-point)))
809 (let ((npatches (length patchsyms)))
811 (error "No patches to delete")
812 (when (yes-or-no-p (format "Really delete %d patch%s? "
814 (if (= 1 npatches) "" "es")))
815 (stgit-capture-output nil
816 (apply 'stgit-run "delete" patchsyms))
819 (defun stgit-coalesce (patchsyms)
820 "Coalesce the patches in PATCHSYMS.
821 Interactively, coalesce the marked patches."
822 (interactive (list stgit-marked-patches))
823 (when (< (length patchsyms) 2)
824 (error "Need at least two patches to coalesce"))
825 (let ((edit-buf (get-buffer-create "*StGit edit*"))
826 (dir default-directory))
827 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
828 (set (make-local-variable 'stgit-patchsyms) patchsyms)
829 (setq default-directory dir)
830 (let ((standard-output edit-buf))
831 (apply 'stgit-run-silent "coalesce" "--save-template=-" patchsyms))))
833 (defun stgit-confirm-coalesce ()
835 (let ((file (make-temp-file "stgit-edit-")))
836 (write-region (point-min) (point-max) file)
837 (stgit-capture-output nil
838 (apply 'stgit-run "coalesce" "-f" file stgit-patchsyms))
839 (with-current-buffer log-edit-parent-buffer
841 ;; Go to first marked patch and stay there
842 (goto-char (point-min))
843 (re-search-forward (concat "^[>+-]\\*") nil t)
844 (move-to-column goal-column)
850 "Display help for the StGit mode."
852 (describe-function 'stgit-mode))
854 (defun stgit-undo (&optional arg)
856 With prefix argument, run it with the --hard flag."
858 (stgit-capture-output nil
860 (stgit-run "undo" "--hard")
864 (defun stgit-refresh (&optional arg)
866 With prefix argument, refresh the marked patch or the patch under point."
868 (let ((patchargs (if arg
869 (let ((patches (stgit-patches-marked-or-at-point)))
870 (cond ((null patches)
871 (error "No patch to update"))
872 ((> (length patches) 1)
873 (error "Too many patches selected"))
875 (cons "-p" patches))))
877 (stgit-capture-output nil
878 (apply 'stgit-run "refresh" patchargs))
879 (stgit-refresh-git-status))