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'
16 "Manage StGit patches for the tree in DIR."
17 (interactive "DDirectory: \n")
18 (switch-to-stgit-buffer (git-get-top-dir dir))
21 (unless (fboundp 'git-get-top-dir)
22 (defun git-get-top-dir (dir)
23 "Retrieve the top-level directory of a git tree."
24 (let ((cdup (with-output-to-string
25 (with-current-buffer standard-output
27 (unless (eq 0 (call-process "git" nil t nil
28 "rev-parse" "--show-cdup"))
29 (error "Cannot find top-level git tree for %s" dir))))))
30 (expand-file-name (concat (file-name-as-directory dir)
31 (car (split-string cdup "\n")))))))
33 (defun stgit-refresh-git-status (&optional dir)
34 "If it exists, refresh the `git-status' buffer belonging to
35 directory DIR or `default-directory'"
36 (when (and (fboundp 'git-find-status-buffer)
37 (fboundp 'git-refresh-status))
38 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
39 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
40 (when git-status-buffer
41 (with-current-buffer git-status-buffer
42 (git-refresh-status))))))
44 (defun switch-to-stgit-buffer (dir)
45 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
46 (setq dir (file-name-as-directory dir))
47 (let ((buffers (buffer-list)))
49 (not (with-current-buffer (car buffers)
50 (and (eq major-mode 'stgit-mode)
51 (string= default-directory dir)))))
52 (setq buffers (cdr buffers)))
53 (switch-to-buffer (if buffers
55 (create-stgit-buffer dir)))))
56 (defstruct (stgit-patch)
57 status name desc empty)
59 (defun create-stgit-buffer (dir)
60 "Create a buffer for showing StGit patches.
61 Argument DIR is the repository path."
62 (let ((buf (create-file-buffer (concat dir "*stgit*")))
63 (inhibit-read-only t))
64 (with-current-buffer buf
65 (setq default-directory dir)
67 (setq buffer-read-only t))
70 (defmacro stgit-capture-output (name &rest body)
71 "Capture StGit output and, if there was any output, show it in a window
73 Returns nil if there was no output."
74 (declare (debug ([&or stringp null] body))
76 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
77 (stgit-dir default-directory)
78 (inhibit-read-only t))
79 (with-current-buffer output-buf
81 (setq default-directory stgit-dir)
82 (setq buffer-read-only t))
83 (let ((standard-output output-buf))
85 (with-current-buffer output-buf
86 (set-buffer-modified-p nil)
87 (setq buffer-read-only t)
88 (if (< (point-min) (point-max))
89 (display-buffer output-buf t)))))
91 (defun stgit-make-run-args (args)
92 "Return a copy of ARGS with its elements converted to strings."
94 ;; don't use (format "%s" ...) to limit type errors
96 ((integerp x) (number-to-string x))
97 ((symbolp x) (symbol-name x))
99 (error "Bad element in stgit-make-run-args args: %S" x))))
102 (defun stgit-run-silent (&rest args)
103 (setq args (stgit-make-run-args args))
104 (apply 'call-process "stg" nil standard-output nil args))
106 (defun stgit-run (&rest args)
107 (setq args (stgit-make-run-args args))
108 (let ((msgcmd (mapconcat #'identity args " ")))
109 (message "Running stg %s..." msgcmd)
110 (apply 'call-process "stg" nil standard-output nil args)
111 (message "Running stg %s...done" msgcmd)))
113 (defun stgit-run-git (&rest args)
114 (setq args (stgit-make-run-args args))
115 (let ((msgcmd (mapconcat #'identity args " ")))
116 (message "Running git %s..." msgcmd)
117 (apply 'call-process "git" nil standard-output nil args)
118 (message "Running git %s...done" msgcmd)))
120 (defun stgit-run-git-silent (&rest args)
121 (setq args (stgit-make-run-args args))
122 (apply 'call-process "git" nil standard-output nil args))
124 (defun stgit-reload ()
125 "Update the contents of the StGit buffer."
127 (let ((inhibit-read-only t)
128 (curline (line-number-at-pos))
129 (curpatch (stgit-patch-name-at-point)))
132 (stgit-run-silent "branch")
133 (stgit-run-silent "series" "--description" "--empty")
136 (stgit-goto-patch curpatch)
137 (goto-line curline)))
138 (stgit-refresh-git-status))
141 "A user interface for the StGit patch maintenance tool."
144 (defface stgit-description-face
145 '((((background dark)) (:foreground "tan"))
146 (((background light)) (:foreground "dark red")))
147 "The face used for StGit descriptions"
150 (defface stgit-top-patch-face
151 '((((background dark)) (:weight bold :foreground "yellow"))
152 (((background light)) (:weight bold :foreground "purple"))
154 "The face used for the top patch names"
157 (defface stgit-applied-patch-face
158 '((((background dark)) (:foreground "light yellow"))
159 (((background light)) (:foreground "purple"))
161 "The face used for applied patch names"
164 (defface stgit-unapplied-patch-face
165 '((((background dark)) (:foreground "gray80"))
166 (((background light)) (:foreground "orchid"))
168 "The face used for unapplied patch names"
171 (defface stgit-modified-file-face
172 '((((class color) (background light)) (:foreground "purple"))
173 (((class color) (background dark)) (:foreground "salmon")))
174 "StGit mode face used for modified file status"
177 (defface stgit-unmerged-file-face
178 '((((class color) (background light)) (:foreground "red" :bold t))
179 (((class color) (background dark)) (:foreground "red" :bold t)))
180 "StGit mode face used for unmerged file status"
183 (defface stgit-unknown-file-face
184 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
185 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
186 "StGit mode face used for unknown file status"
189 (defface stgit-file-permission-face
190 '((((class color) (background light)) (:foreground "green" :bold t))
191 (((class color) (background dark)) (:foreground "green" :bold t)))
192 "StGit mode face used for permission changes."
195 (defcustom stgit-expand-find-copies-harder
197 "Try harder to find copied files when listing patches.
199 When not nil, runs git diff-tree with the --find-copies-harder
200 flag, which reduces performance."
204 (defconst stgit-file-status-code-strings
205 (mapcar (lambda (arg)
207 (propertize (cadr arg) 'face (car (cddr arg)))))
208 '((add "Added" stgit-modified-file-face)
209 (copy "Copied" stgit-modified-file-face)
210 (delete "Deleted" stgit-modified-file-face)
211 (modify "Modified" stgit-modified-file-face)
212 (rename "Renamed" stgit-modified-file-face)
213 (mode-change "Mode change" stgit-modified-file-face)
214 (unmerged "Unmerged" stgit-unmerged-file-face)
215 (unknown "Unknown" stgit-unknown-file-face)))
216 "Alist of code symbols to description strings")
218 (defun stgit-file-status-code-as-string (code)
219 "Return stgit status code as string"
220 (let ((str (assq (if (consp code) (car code) code)
221 stgit-file-status-code-strings)))
224 (if (and str (consp code) (/= (cdr code) 100))
225 (format "%s %s" (cdr str)
226 (propertize (format "%d%%" (cdr code))
227 'face 'stgit-description-face))
230 (defun stgit-file-status-code (str &optional score)
231 "Return stgit status code from git status string"
232 (let ((code (assoc str '(("A" . add)
240 (setq code (if code (cdr code) 'unknown))
241 (when (stringp score)
242 (if (> (length score) 0)
243 (setq score (string-to-number score))
245 (if score (cons code score) code)))
247 (defconst stgit-file-type-strings
250 (#o160 . "subproject"))
251 "Alist of names of file types")
253 (defun stgit-file-type-string (type)
254 "Return string describing file type TYPE (the high bits of file permission).
255 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
256 (let ((type-str (assoc type stgit-file-type-strings)))
257 (or (and type-str (cdr type-str))
258 (format "unknown type %o" type))))
260 (defun stgit-file-type-change-string (old-perm new-perm)
261 "Return string describing file type change from OLD-PERM to NEW-PERM.
262 Cf. `stgit-file-type-string'."
263 (let ((old-type (lsh old-perm -9))
264 (new-type (lsh new-perm -9)))
265 (cond ((= old-type new-type) "")
266 ((zerop new-type) "")
268 (if (= new-type #o100)
270 (format " (%s)" (stgit-file-type-string new-type))))
271 (t (format " (%s -> %s)"
272 (stgit-file-type-string old-type)
273 (stgit-file-type-string new-type))))))
275 (defun stgit-file-mode-change-string (old-perm new-perm)
276 "Return string describing file mode change from OLD-PERM to NEW-PERM.
277 Cf. `stgit-file-type-change-string'."
278 (setq old-perm (logand old-perm #o777)
279 new-perm (logand new-perm #o777))
280 (if (or (= old-perm new-perm)
284 (let* ((modified (logxor old-perm new-perm))
285 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
286 (cond ((zerop modified) "")
287 ((and (zerop not-x-modified)
288 (or (and (eq #o111 (logand old-perm #o111))
289 (propertize "-x" 'face 'stgit-file-permission-face))
290 (and (eq #o111 (logand new-perm #o111))
291 (propertize "+x" 'face
292 'stgit-file-permission-face)))))
293 (t (concat (propertize (format "%o" old-perm)
294 'face 'stgit-file-permission-face)
296 'face 'stgit-description-face)
297 (propertize (format "%o" new-perm)
298 'face 'stgit-file-permission-face)))))))
300 (defun stgit-expand-patch (patchsym)
301 "Expand (show modification of) the patch with name PATCHSYM (a
303 `stgit-expand-find-copies-harder' controls how hard to try to
307 (let* ((start (point))
308 (result (with-output-to-string
309 (stgit-run-git "diff-tree" "-r" "-z"
310 (if stgit-expand-find-copies-harder
311 "--find-copies-harder"
313 (stgit-id patchsym)))))
315 (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]*\\)\\)"
317 (let ((copy-or-rename (match-string 4 result))
318 (old-perm (read (format "#o%s" (match-string 1 result))))
319 (new-perm (read (format "#o%s" (match-string 2 result))))
323 (properties '(entry-type file)))
326 (let ((cr-score (match-string 5 result))
327 (cr-from-file (match-string 6 result))
328 (cr-to-file (match-string 7 result)))
329 (setq status (stgit-file-status-code copy-or-rename
331 properties (list* 'stgit-old-file cr-from-file
332 'stgit-new-file cr-to-file
337 'face 'stgit-description-face)
339 (setq status (stgit-file-status-code (match-string 8 result))
340 properties (list* 'stgit-file (match-string 9 result)
342 change (match-string 9 result)))
344 (let ((mode-change (stgit-file-mode-change-string old-perm
346 (insert (format "%-12s" (stgit-file-status-code-as-string
349 (if (> (length mode-change) 0) " " "")
351 (propertize (stgit-file-type-change-string old-perm
353 'face 'stgit-description-face)
355 (add-text-properties line-start (point) properties))
356 (setq mstart (match-end 0))))
357 (when (= start (point))
358 (insert " <no files>\n"))
359 (put-text-property start (point) 'stgit-file-patchsym patchsym))))
361 (defun stgit-collapse-patch (patchsym)
362 "Collapse the patch with name PATCHSYM after the line at point."
365 (let ((start (point)))
366 (while (eq (get-text-property (point) 'stgit-file-patchsym) patchsym)
368 (delete-region start (point)))))
370 (defun stgit-rescan ()
371 "Rescan the status buffer."
375 (goto-char (point-min))
377 (cond ((looking-at "Branch: \\(.*\\)")
378 (put-text-property (match-beginning 1) (match-end 1)
380 ((looking-at "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
382 (let ((empty (string= (match-string 1) "0"))
383 (state (match-string 2))
384 (patchsym (intern (match-string 4))))
386 (match-beginning 4) (match-end 4) 'face
387 (cond ((string= state ">") 'stgit-top-patch-face)
388 ((string= state "+") 'stgit-applied-patch-face)
389 ((string= state "-") 'stgit-unapplied-patch-face)))
390 (put-text-property (match-beginning 5) (match-end 5)
391 'face 'stgit-description-face)
392 (when (memq patchsym stgit-marked-patches)
394 (replace-match "*" nil nil nil 3))
395 (setq marked (cons patchsym marked)))
396 (add-text-properties (match-beginning 0) (match-end 0)
397 (list 'stgit-patchsym patchsym
399 'patch-data (make-stgit-patch
402 :desc (match-string 5)
404 (when (memq patchsym stgit-expanded-patches)
405 (stgit-expand-patch patchsym))
408 (goto-char (match-beginning 5))
409 (insert "(empty) ")))
412 ((or (looking-at "stg series: Branch \".*\" not initialised")
413 (looking-at "stg series: .*: branch not initialized"))
416 (insert "Run M-x stgit-init to initialise")))
418 (setq stgit-marked-patches (nreverse marked))
421 (propertize "no patches in series"
422 'face 'stgit-description-face))))))
424 (defun stgit-select-file ()
425 (let ((patched-file (stgit-patched-file-at-point)))
427 (error "No patch or file on the current line"))
428 (let ((filename (expand-file-name (cdr patched-file))))
429 (unless (file-exists-p filename)
430 (error "File does not exist"))
431 (find-file filename))))
433 (defun stgit-select-patch ()
434 (let ((inhibit-read-only t)
435 (curpatch (stgit-patch-name-at-point)))
436 (if (memq curpatch stgit-expanded-patches)
438 (setq stgit-expanded-patches (delq curpatch stgit-expanded-patches))
439 (stgit-collapse-patch curpatch))
441 (setq stgit-expanded-patches (cons curpatch stgit-expanded-patches))
442 (stgit-expand-patch curpatch)))))
444 (defun stgit-select ()
445 "Expand or collapse the current entry"
447 (case (get-text-property (point) 'entry-type)
449 (stgit-select-patch))
453 (error "No patch or file on line"))))
455 (defun stgit-find-file-other-window ()
456 "Open file at point in other window"
458 (let ((patched-file (stgit-patched-file-at-point)))
460 (error "No file on the current line"))
461 (let ((filename (expand-file-name (cdr patched-file))))
462 (unless (file-exists-p filename)
463 (error "File does not exist"))
464 (find-file-other-window filename))))
467 "Hide the stgit buffer."
471 (defun stgit-git-status ()
472 "Show status using `git-status'."
474 (unless (fboundp 'git-status)
475 (error "The stgit-git-status command requires git-status"))
476 (let ((dir default-directory))
477 (save-selected-window
481 (defun stgit-goal-column ()
482 "Return goal column for the current line"
483 (case (get-text-property (point) 'entry-type)
488 (defun stgit-next-line (&optional arg)
489 "Move cursor vertically down ARG lines"
492 (move-to-column (stgit-goal-column)))
494 (defun stgit-previous-line (&optional arg)
495 "Move cursor vertically up ARG lines"
498 (move-to-column (stgit-goal-column)))
500 (defun stgit-next-patch (&optional arg)
501 "Move cursor down ARG patches"
506 (stgit-previous-patch (- arg))
507 (while (not (zerop arg))
509 (while (progn (stgit-next-line)
510 (not (stgit-patch-name-at-point)))))))
512 (defun stgit-previous-patch (&optional arg)
513 "Move cursor up ARG patches"
518 (stgit-next-patch (- arg))
519 (while (not (zerop arg))
521 (while (progn (stgit-previous-line)
522 (not (stgit-patch-name-at-point)))))))
524 (defvar stgit-mode-hook nil
525 "Run after `stgit-mode' is setup.")
527 (defvar stgit-mode-map nil
528 "Keymap for StGit major mode.")
530 (unless stgit-mode-map
531 (setq stgit-mode-map (make-keymap))
532 (suppress-keymap stgit-mode-map)
533 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
536 ("\d" . stgit-unmark-up)
537 ("u" . stgit-unmark-down)
540 ("\C-p" . stgit-previous-line)
541 ("\C-n" . stgit-next-line)
542 ([up] . stgit-previous-line)
543 ([down] . stgit-next-line)
544 ("p" . stgit-previous-patch)
545 ("n" . stgit-next-patch)
546 ("\M-{" . stgit-previous-patch)
547 ("\M-}" . stgit-next-patch)
548 ("s" . stgit-git-status)
550 ("r" . stgit-refresh)
551 ("\C-c\C-r" . stgit-rename)
553 ("M" . stgit-move-patches)
558 ("U" . stgit-uncommit)
559 ("\r" . stgit-select)
560 ("o" . stgit-find-file-other-window)
561 (">" . stgit-push-next)
562 ("<" . stgit-pop-next)
563 ("P" . stgit-push-or-pop)
567 ([(control ?/)] . stgit-undo)
568 ("\C-_" . stgit-undo)
570 ("q" . stgit-quit))))
573 "Major mode for interacting with StGit.
576 (kill-all-local-variables)
577 (buffer-disable-undo)
578 (setq mode-name "StGit"
579 major-mode 'stgit-mode
581 (use-local-map stgit-mode-map)
582 (set (make-local-variable 'list-buffers-directory) default-directory)
583 (set (make-local-variable 'stgit-marked-patches) nil)
584 (set (make-local-variable 'stgit-expanded-patches) nil)
585 (set-variable 'truncate-lines 't)
586 (run-hooks 'stgit-mode-hook))
588 (defun stgit-add-mark (patchsym)
589 "Mark the patch PATCHSYM."
590 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))
592 (when (stgit-goto-patch patchsym)
594 (let ((inhibit-read-only t))
595 (insert-and-inherit ?*)
598 (defun stgit-remove-mark (patchsym)
599 "Unmark the patch PATCHSYM."
600 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))
602 (when (stgit-goto-patch patchsym)
604 (let ((inhibit-read-only t))
605 (insert-and-inherit ? )
608 (defun stgit-clear-marks ()
609 "Unmark all patches."
610 (setq stgit-marked-patches '()))
612 (defun stgit-patch-at-point (&optional cause-error)
613 (get-text-property (point) 'patch-data))
615 (defun stgit-patch-name-at-point (&optional cause-error)
616 "Return the patch name on the current line as a symbol.
617 If CAUSE-ERROR is not nil, signal an error if none found."
618 (let ((patch (stgit-patch-at-point)))
620 (stgit-patch-name patch))
622 (error "No patch on this line")))))
624 (defun stgit-patched-file-at-point (&optional both-files)
625 "Returns a cons of the patchsym and file name at point. For
626 copies and renames, return the new file if the patch is either
627 applied. If BOTH-FILES is non-nil, return a cons of the old and
628 the new file names instead of just one name."
629 (let ((patchsym (get-text-property (point) 'stgit-file-patchsym))
630 (file (get-text-property (point) 'stgit-file)))
631 (cond ((not patchsym) nil)
632 (file (cons patchsym file))
634 (cons patchsym (cons (get-text-property (point) 'stgit-old-file)
635 (get-text-property (point) 'stgit-new-file))))
637 (let ((file-sym (save-excursion
638 (stgit-previous-patch)
639 (unless (eq (stgit-patch-name-at-point)
641 (error "Cannot find the %s patch" patchsym))
643 (if (= (char-after) ?-)
646 (cons patchsym (get-text-property (point) file-sym)))))))
648 (defun stgit-patches-marked-or-at-point ()
649 "Return the symbols of the marked patches, or the patch on the current line."
650 (if stgit-marked-patches
652 (let ((patch (stgit-patch-name-at-point)))
657 (defun stgit-goto-patch (patchsym)
658 "Move point to the line containing patch PATCHSYM.
659 If that patch cannot be found, return nil."
660 (let ((p (text-property-any (point-min) (point-max)
661 'stgit-patchsym patchsym)))
664 (move-to-column goal-column))))
669 (stgit-capture-output nil
674 "Mark the patch under point."
676 (let ((patch (stgit-patch-name-at-point t)))
677 (stgit-add-mark patch))
680 (defun stgit-unmark-up ()
681 "Remove mark from the patch on the previous line."
683 (stgit-previous-patch)
684 (stgit-remove-mark (stgit-patch-name-at-point t)))
686 (defun stgit-unmark-down ()
687 "Remove mark from the patch on the current line."
689 (stgit-remove-mark (stgit-patch-name-at-point t))
692 (defun stgit-rename (name)
693 "Rename the patch under point to NAME."
694 (interactive (list (read-string "Patch name: "
695 (symbol-name (stgit-patch-name-at-point t)))))
696 (let ((old-patchsym (stgit-patch-name-at-point t)))
697 (stgit-capture-output nil
698 (stgit-run "rename" old-patchsym name))
699 (let ((name-sym (intern name)))
700 (when (memq old-patchsym stgit-expanded-patches)
701 (setq stgit-expanded-patches
702 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
703 (when (memq old-patchsym stgit-marked-patches)
704 (setq stgit-marked-patches
705 (cons name-sym (delq old-patchsym stgit-marked-patches))))
707 (stgit-goto-patch name-sym))))
709 (defun stgit-repair ()
712 (stgit-capture-output nil
713 (stgit-run "repair"))
716 (defun stgit-available-branches ()
717 "Returns a list of the available stg branches"
718 (let ((output (with-output-to-string
719 (stgit-run "branch" "--list")))
722 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
723 (setq result (cons (match-string 1 output) result))
724 (setq start (match-end 0)))
727 (defun stgit-branch (branch)
728 "Switch to branch BRANCH."
729 (interactive (list (completing-read "Switch to branch: "
730 (stgit-available-branches))))
731 (stgit-capture-output nil (stgit-run "branch" "--" branch))
734 (defun stgit-commit (count)
735 "Run stg commit on COUNT commits.
736 Interactively, the prefix argument is used as COUNT."
738 (stgit-capture-output nil (stgit-run "commit" "-n" count))
741 (defun stgit-uncommit (count)
742 "Run stg uncommit on COUNT commits.
743 Interactively, the prefix argument is used as COUNT."
745 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
748 (defun stgit-push-next (npatches)
749 "Push the first unapplied patch.
750 With numeric prefix argument, push that many patches."
752 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
754 (stgit-refresh-git-status))
756 (defun stgit-pop-next (npatches)
757 "Pop the topmost applied patch.
758 With numeric prefix argument, pop that many patches."
760 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
762 (stgit-refresh-git-status))
764 (defun stgit-applied-at-point ()
765 "Is the patch on the current line applied?"
768 (looking-at "[>+]")))
770 (defun stgit-push-or-pop ()
771 "Push or pop the patch on the current line."
773 (let ((patchsym (stgit-patch-name-at-point t))
774 (applied (stgit-applied-at-point)))
775 (stgit-capture-output nil
776 (stgit-run (if applied "pop" "push") patchsym))
780 "Go to the patch on the current line."
782 (let ((patchsym (stgit-patch-name-at-point t)))
783 (stgit-capture-output nil
784 (stgit-run "goto" patchsym))
787 (defun stgit-id (patchsym)
788 "Return the git commit id for PATCHSYM.
789 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
790 (if (keywordp patchsym)
792 (let ((result (with-output-to-string
793 (stgit-run-silent "id" patchsym))))
794 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
795 (error "Cannot find commit id for %s" patchsym))
796 (match-string 1 result))))
799 "Show the patch on the current line."
801 (stgit-capture-output "*StGit patch*"
802 (case (get-text-property (point) 'entry-type)
804 (let ((patchsym (stgit-patch-name-at-point))
805 (patched-file (stgit-patched-file-at-point t)))
806 (let ((id (stgit-id (car patched-file))))
807 (if (consp (cdr patched-file))
808 ;; two files (copy or rename)
809 (stgit-run-git "diff" "-C" "-C" (concat id "^") id "--"
810 (cadr patched-file) (cddr patched-file))
812 (stgit-run-git "diff" (concat id "^") id "--"
813 (cdr patched-file))))))
815 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
816 (stgit-patch-name-at-point)))
818 (error "No patch or file at point")))
819 (with-current-buffer standard-output
820 (goto-char (point-min))
824 "Edit the patch on the current line."
826 (let ((patchsym (stgit-patch-name-at-point t))
827 (edit-buf (get-buffer-create "*StGit edit*"))
828 (dir default-directory))
829 (log-edit 'stgit-confirm-edit t nil edit-buf)
830 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
831 (setq default-directory dir)
832 (let ((standard-output edit-buf))
833 (stgit-run-silent "edit" "--save-template=-" patchsym))))
835 (defun stgit-confirm-edit ()
837 (let ((file (make-temp-file "stgit-edit-")))
838 (write-region (point-min) (point-max) file)
839 (stgit-capture-output nil
840 (stgit-run "edit" "-f" file stgit-edit-patchsym))
841 (with-current-buffer log-edit-parent-buffer
844 (defun stgit-new (add-sign)
846 With a prefix argument, include a \"Signed-off-by:\" line at the
849 (let ((edit-buf (get-buffer-create "*StGit edit*"))
850 (dir default-directory))
851 (log-edit 'stgit-confirm-new t nil edit-buf)
852 (setq default-directory dir)
855 (let ((standard-output (current-buffer)))
856 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
858 (defun stgit-confirm-new ()
860 (let ((file (make-temp-file "stgit-edit-")))
861 (write-region (point-min) (point-max) file)
862 (stgit-capture-output nil
863 (stgit-run "new" "-f" file))
864 (with-current-buffer log-edit-parent-buffer
867 (defun stgit-create-patch-name (description)
868 "Create a patch name from a long description"
870 (while (> (length description) 0)
871 (cond ((string-match "\\`[a-zA-Z_-]+" description)
872 (setq patch (downcase (concat patch
873 (match-string 0 description))))
874 (setq description (substring description (match-end 0))))
875 ((string-match "\\` +" description)
876 (setq patch (concat patch "-"))
877 (setq description (substring description (match-end 0))))
878 ((string-match "\\`[^a-zA-Z_-]+" description)
879 (setq description (substring description (match-end 0))))))
880 (cond ((= (length patch) 0)
882 ((> (length patch) 20)
883 (substring patch 0 20))
886 (defun stgit-delete (patchsyms &optional spill-p)
887 "Delete the patches in PATCHSYMS.
888 Interactively, delete the marked patches, or the patch at point.
890 With a prefix argument, or SPILL-P, spill the patch contents to
891 the work tree and index."
892 (interactive (list (stgit-patches-marked-or-at-point)
895 (error "No patches to delete"))
896 (let ((npatches (length patchsyms)))
897 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
899 (if (= 1 npatches) "" "es")
901 " (spilling contents to index)"
903 (let ((args (if spill-p
904 (cons "--spill" patchsyms)
906 (stgit-capture-output nil
907 (apply 'stgit-run "delete" args))
910 (defun stgit-move-patches-target ()
911 "Return the patchsym indicating a target patch for
912 `stgit-move-patches'.
914 This is either the patch at point, or one of :top and :bottom, if
915 the point is after or before the applied patches."
917 (let ((patchsym (stgit-patch-name-at-point)))
918 (cond (patchsym patchsym)
919 ((save-excursion (re-search-backward "^>" nil t)) :top)
922 (defun stgit-sort-patches (patchsyms)
923 "Returns the list of patches in PATCHSYMS sorted according to
924 their position in the patch series, bottommost first.
926 PATCHSYMS may not contain duplicate entries."
927 (let (sorted-patchsyms
928 (series (with-output-to-string
929 (with-current-buffer standard-output
930 (stgit-run-silent "series" "--noprefix"))))
932 (while (string-match "^\\(.+\\)" series start)
933 (let ((patchsym (intern (match-string 1 series))))
934 (when (memq patchsym patchsyms)
935 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
936 (setq start (match-end 0)))
937 (setq sorted-patchsyms (nreverse sorted-patchsyms))
939 (unless (= (length patchsyms) (length sorted-patchsyms))
940 (error "Internal error"))
944 (defun stgit-move-patches (patchsyms target-patch)
945 "Move the patches in PATCHSYMS to below TARGET-PATCH.
946 If TARGET-PATCH is :bottom or :top, move the patches to the
947 bottom or top of the stack, respectively.
949 Interactively, move the marked patches to where the point is."
950 (interactive (list stgit-marked-patches
951 (stgit-move-patches-target)))
953 (error "Need at least one patch to move"))
956 (error "Point not at a patch"))
958 (if (eq target-patch :top)
959 (stgit-capture-output nil
960 (apply 'stgit-run "float" patchsyms))
962 ;; need to have patchsyms sorted by position in the stack
963 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
964 (while sorted-patchsyms
965 (setq sorted-patchsyms
966 (and (stgit-capture-output nil
967 (if (eq target-patch :bottom)
968 (stgit-run "sink" "--" (car sorted-patchsyms))
969 (stgit-run "sink" "--to" target-patch "--"
970 (car sorted-patchsyms))))
971 (cdr sorted-patchsyms))))))
974 (defun stgit-squash (patchsyms)
975 "Squash the patches in PATCHSYMS.
976 Interactively, squash the marked patches.
978 Unless there are any conflicts, the patches will be merged into
979 one patch, which will occupy the same spot in the series as the
980 deepest patch had before the squash."
981 (interactive (list stgit-marked-patches))
982 (when (< (length patchsyms) 2)
983 (error "Need at least two patches to squash"))
984 (let ((stgit-buffer (current-buffer))
985 (edit-buf (get-buffer-create "*StGit edit*"))
986 (dir default-directory)
987 (sorted-patchsyms (stgit-sort-patches patchsyms)))
988 (log-edit 'stgit-confirm-squash t nil edit-buf)
989 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
990 (setq default-directory dir)
991 (let ((result (let ((standard-output edit-buf))
992 (apply 'stgit-run-silent "squash"
993 "--save-template=-" sorted-patchsyms))))
995 ;; stg squash may have reordered the patches or caused conflicts
996 (with-current-buffer stgit-buffer
999 (unless (eq 0 result)
1001 (rename-buffer "*StGit error*")
1002 (resize-temp-buffer-window)
1003 (switch-to-buffer-other-window stgit-buffer)
1004 (error "stg squash failed")))))
1006 (defun stgit-confirm-squash ()
1008 (let ((file (make-temp-file "stgit-edit-")))
1009 (write-region (point-min) (point-max) file)
1010 (stgit-capture-output nil
1011 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1012 (with-current-buffer log-edit-parent-buffer
1014 ;; Go to first marked patch and stay there
1015 (goto-char (point-min))
1016 (re-search-forward (concat "^[>+-]\\*") nil t)
1017 (move-to-column goal-column)
1018 (let ((pos (point)))
1022 (defun stgit-help ()
1023 "Display help for the StGit mode."
1025 (describe-function 'stgit-mode))
1027 (defun stgit-undo (&optional arg)
1029 With prefix argument, run it with the --hard flag."
1031 (stgit-capture-output nil
1033 (stgit-run "undo" "--hard")
1034 (stgit-run "undo")))
1037 (defun stgit-refresh (&optional arg)
1039 With prefix argument, refresh the marked patch or the patch under point."
1041 (let ((patchargs (if arg
1042 (let ((patches (stgit-patches-marked-or-at-point)))
1043 (cond ((null patches)
1044 (error "No patch to update"))
1045 ((> (length patches) 1)
1046 (error "Too many patches selected"))
1048 (cons "-p" patches))))
1050 (stgit-capture-output nil
1051 (apply 'stgit-run "refresh" patchargs))
1052 (stgit-refresh-git-status))