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, if there was any output, show it in a window
70 Returns nil if there was no output."
71 (declare (debug ([&or stringp null] body))
73 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
74 (stgit-dir default-directory)
75 (inhibit-read-only t))
76 (with-current-buffer output-buf
78 (setq default-directory stgit-dir)
79 (setq buffer-read-only t))
80 (let ((standard-output output-buf))
82 (with-current-buffer output-buf
83 (set-buffer-modified-p nil)
84 (setq buffer-read-only t)
85 (if (< (point-min) (point-max))
86 (display-buffer output-buf t)))))
88 (defun stgit-make-run-args (args)
89 "Return a copy of ARGS with its elements converted to strings."
91 ;; don't use (format "%s" ...) to limit type errors
93 ((integerp x) (number-to-string x))
94 ((symbolp x) (symbol-name x))
96 (error "Bad element in stgit-make-run-args args: %S" x))))
99 (defun stgit-run-silent (&rest args)
100 (setq args (stgit-make-run-args args))
101 (apply 'call-process "stg" nil standard-output nil args))
103 (defun stgit-run (&rest args)
104 (setq args (stgit-make-run-args args))
105 (let ((msgcmd (mapconcat #'identity args " ")))
106 (message "Running stg %s..." msgcmd)
107 (apply 'call-process "stg" nil standard-output nil args)
108 (message "Running stg %s...done" msgcmd)))
110 (defun stgit-run-git (&rest args)
111 (setq args (stgit-make-run-args args))
112 (let ((msgcmd (mapconcat #'identity args " ")))
113 (message "Running git %s..." msgcmd)
114 (apply 'call-process "git" nil standard-output nil args)
115 (message "Running git %s...done" msgcmd)))
117 (defun stgit-run-git-silent (&rest args)
118 (setq args (stgit-make-run-args args))
119 (apply 'call-process "git" nil standard-output nil args))
121 (defun stgit-reload ()
122 "Update the contents of the StGit buffer."
124 (let ((inhibit-read-only t)
125 (curline (line-number-at-pos))
126 (curpatch (stgit-patch-at-point)))
129 (stgit-run-silent "branch")
130 (stgit-run-silent "series" "--description" "--empty")
133 (stgit-goto-patch curpatch)
134 (goto-line curline)))
135 (stgit-refresh-git-status))
138 "A user interface for the StGit patch maintenance tool."
141 (defface stgit-description-face
142 '((((background dark)) (:foreground "tan"))
143 (((background light)) (:foreground "dark red")))
144 "The face used for StGit descriptions"
147 (defface stgit-top-patch-face
148 '((((background dark)) (:weight bold :foreground "yellow"))
149 (((background light)) (:weight bold :foreground "purple"))
151 "The face used for the top patch names"
154 (defface stgit-applied-patch-face
155 '((((background dark)) (:foreground "light yellow"))
156 (((background light)) (:foreground "purple"))
158 "The face used for applied patch names"
161 (defface stgit-unapplied-patch-face
162 '((((background dark)) (:foreground "gray80"))
163 (((background light)) (:foreground "orchid"))
165 "The face used for unapplied patch names"
168 (defface stgit-modified-file-face
169 '((((class color) (background light)) (:foreground "purple"))
170 (((class color) (background dark)) (:foreground "salmon")))
171 "StGit mode face used for modified file status"
174 (defface stgit-unmerged-file-face
175 '((((class color) (background light)) (:foreground "red" :bold t))
176 (((class color) (background dark)) (:foreground "red" :bold t)))
177 "StGit mode face used for unmerged file status"
180 (defface stgit-unknown-file-face
181 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
182 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
183 "StGit mode face used for unknown file status"
186 (defface stgit-file-permission-face
187 '((((class color) (background light)) (:foreground "green" :bold t))
188 (((class color) (background dark)) (:foreground "green" :bold t)))
189 "StGit mode face used for permission changes."
192 (defcustom stgit-expand-find-copies-harder
194 "Try harder to find copied files when listing patches.
196 When not nil, runs git diff-tree with the --find-copies-harder
197 flag, which reduces performance."
201 (defconst stgit-file-status-code-strings
202 (mapcar (lambda (arg)
204 (propertize (cadr arg) 'face (car (cddr arg)))))
205 '((add "Added" stgit-modified-file-face)
206 (copy "Copied" stgit-modified-file-face)
207 (delete "Deleted" stgit-modified-file-face)
208 (modify "Modified" stgit-modified-file-face)
209 (rename "Renamed" stgit-modified-file-face)
210 (mode-change "Mode change" stgit-modified-file-face)
211 (unmerged "Unmerged" stgit-unmerged-file-face)
212 (unknown "Unknown" stgit-unknown-file-face)))
213 "Alist of code symbols to description strings")
215 (defun stgit-file-status-code-as-string (code)
216 "Return stgit status code as string"
217 (let ((str (assq (if (consp code) (car code) code)
218 stgit-file-status-code-strings)))
221 (if (and str (consp code) (/= (cdr code) 100))
222 (format "%s %s" (cdr str)
223 (propertize (format "%d%%" (cdr code))
224 'face 'stgit-description-face))
227 (defun stgit-file-status-code (str &optional score)
228 "Return stgit status code from git status string"
229 (let ((code (assoc str '(("A" . add)
237 (setq code (if code (cdr code) 'unknown))
238 (when (stringp score)
239 (if (> (length score) 0)
240 (setq score (string-to-number score))
242 (if score (cons code score) code)))
244 (defconst stgit-file-type-strings
247 (#o160 . "subproject"))
248 "Alist of names of file types")
250 (defun stgit-file-type-string (type)
251 "Return string describing file type TYPE (the high bits of file permission).
252 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
253 (let ((type-str (assoc type stgit-file-type-strings)))
254 (or (and type-str (cdr type-str))
255 (format "unknown type %o" type))))
257 (defun stgit-file-type-change-string (old-perm new-perm)
258 "Return string describing file type change from OLD-PERM to NEW-PERM.
259 Cf. `stgit-file-type-string'."
260 (let ((old-type (lsh old-perm -9))
261 (new-type (lsh new-perm -9)))
262 (cond ((= old-type new-type) "")
263 ((zerop new-type) "")
265 (if (= new-type #o100)
267 (format " (%s)" (stgit-file-type-string new-type))))
268 (t (format " (%s -> %s)"
269 (stgit-file-type-string old-type)
270 (stgit-file-type-string new-type))))))
272 (defun stgit-file-mode-change-string (old-perm new-perm)
273 "Return string describing file mode change from OLD-PERM to NEW-PERM.
274 Cf. `stgit-file-type-change-string'."
275 (setq old-perm (logand old-perm #o777)
276 new-perm (logand new-perm #o777))
277 (if (or (= old-perm new-perm)
281 (let* ((modified (logxor old-perm new-perm))
282 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
283 (cond ((zerop modified) "")
284 ((and (zerop not-x-modified)
285 (or (and (eq #o111 (logand old-perm #o111))
286 (propertize "-x" 'face 'stgit-file-permission-face))
287 (and (eq #o111 (logand new-perm #o111))
288 (propertize "+x" 'face
289 'stgit-file-permission-face)))))
290 (t (concat (propertize (format "%o" old-perm)
291 'face 'stgit-file-permission-face)
293 'face 'stgit-description-face)
294 (propertize (format "%o" new-perm)
295 'face 'stgit-file-permission-face)))))))
297 (defun stgit-expand-patch (patchsym)
298 "Expand (show modification of) the patch with name PATCHSYM (a
300 `stgit-expand-find-copies-harder' controls how hard to try to
304 (let* ((start (point))
305 (result (with-output-to-string
306 (stgit-run-git "diff-tree" "-r" "-z"
307 (if stgit-expand-find-copies-harder
308 "--find-copies-harder"
310 (stgit-id patchsym)))))
312 (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]*\\)\\)"
314 (let ((copy-or-rename (match-string 4 result))
315 (old-perm (read (format "#o%s" (match-string 1 result))))
316 (new-perm (read (format "#o%s" (match-string 2 result))))
323 (let ((cr-score (match-string 5 result))
324 (cr-from-file (match-string 6 result))
325 (cr-to-file (match-string 7 result)))
326 (setq status (stgit-file-status-code copy-or-rename
328 properties (list 'stgit-old-file cr-from-file
329 'stgit-new-file cr-to-file)
333 'face 'stgit-description-face)
335 (setq status (stgit-file-status-code (match-string 8 result))
336 properties (list 'stgit-file (match-string 9 result))
337 change (match-string 9 result)))
339 (let ((mode-change (stgit-file-mode-change-string old-perm
341 (insert (format "%-12s" (stgit-file-status-code-as-string
344 (if (> (length mode-change) 0) " " "")
346 (propertize (stgit-file-type-change-string old-perm
348 'face 'stgit-description-face)
350 (add-text-properties line-start (point) properties))
351 (setq mstart (match-end 0))))
352 (when (= start (point))
353 (insert " <no files>\n"))
354 (put-text-property start (point) 'stgit-file-patchsym patchsym))))
356 (defun stgit-collapse-patch (patchsym)
357 "Collapse the patch with name PATCHSYM after the line at point."
360 (let ((start (point)))
361 (while (eq (get-text-property (point) 'stgit-file-patchsym) patchsym)
363 (delete-region start (point)))))
365 (defun stgit-rescan ()
366 "Rescan the status buffer."
370 (goto-char (point-min))
372 (cond ((looking-at "Branch: \\(.*\\)")
373 (put-text-property (match-beginning 1) (match-end 1)
375 ((looking-at "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
377 (let ((empty (match-string 1))
378 (state (match-string 2))
379 (patchsym (intern (match-string 4))))
381 (match-beginning 4) (match-end 4) 'face
382 (cond ((string= state ">") 'stgit-top-patch-face)
383 ((string= state "+") 'stgit-applied-patch-face)
384 ((string= state "-") 'stgit-unapplied-patch-face)))
385 (put-text-property (match-beginning 5) (match-end 5)
386 'face 'stgit-description-face)
387 (when (memq patchsym stgit-marked-patches)
389 (replace-match "*" nil nil nil 3))
390 (setq marked (cons patchsym marked)))
391 (put-text-property (match-beginning 0) (match-end 0)
392 'stgit-patchsym patchsym)
393 (when (memq patchsym stgit-expanded-patches)
394 (stgit-expand-patch patchsym))
395 (when (equal "0" empty)
397 (goto-char (match-beginning 5))
398 (insert "(empty) ")))
401 ((or (looking-at "stg series: Branch \".*\" not initialised")
402 (looking-at "stg series: .*: branch not initialized"))
405 (insert "Run M-x stgit-init to initialise")))
407 (setq stgit-marked-patches (nreverse marked))
410 (propertize "no patches in series"
411 'face 'stgit-description-face))))))
413 (defun stgit-select-file ()
414 (let ((patched-file (stgit-patched-file-at-point)))
416 (error "No patch or file on the current line"))
417 (let ((filename (expand-file-name (cdr patched-file))))
418 (unless (file-exists-p filename)
419 (error "File does not exist"))
420 (find-file filename))))
422 (defun stgit-toggle-patch-file-list (curpath)
423 (let ((inhibit-read-only t))
424 (if (memq curpatch stgit-expanded-patches)
426 (setq stgit-expanded-patches (delq curpatch stgit-expanded-patches))
427 (stgit-collapse-patch curpatch))
429 (setq stgit-expanded-patches (cons curpatch stgit-expanded-patches))
430 (stgit-expand-patch curpatch)))))
432 (defun stgit-select ()
433 "Expand or collapse the current entry"
435 (let ((curpatch (stgit-patch-at-point)))
437 (stgit-toggle-patch-file-list curpatch)
438 (stgit-select-file))))
441 (defun stgit-find-file-other-window ()
442 "Open file at point in other window"
444 (let ((patched-file (stgit-patched-file-at-point)))
446 (error "No file on the current line"))
447 (let ((filename (expand-file-name (cdr patched-file))))
448 (unless (file-exists-p filename)
449 (error "File does not exist"))
450 (find-file-other-window filename))))
453 "Hide the stgit buffer."
457 (defun stgit-git-status ()
458 "Show status using `git-status'."
460 (unless (fboundp 'git-status)
461 (error "The stgit-git-status command requires git-status"))
462 (let ((dir default-directory))
463 (save-selected-window
467 (defun stgit-goal-column ()
468 "Return goal column for the current line"
469 (cond ((get-text-property (point) 'stgit-file-patchsym) 4)
470 ((get-text-property (point) 'stgit-patchsym) 2)
473 (defun stgit-next-line (&optional arg)
474 "Move cursor vertically down ARG lines"
477 (move-to-column (stgit-goal-column)))
479 (defun stgit-previous-line (&optional arg)
480 "Move cursor vertically up ARG lines"
483 (move-to-column (stgit-goal-column)))
485 (defun stgit-next-patch (&optional arg)
486 "Move cursor down ARG patches"
491 (stgit-previous-patch (- arg))
492 (while (not (zerop arg))
494 (while (progn (stgit-next-line)
495 (not (stgit-patch-at-point)))))))
497 (defun stgit-previous-patch (&optional arg)
498 "Move cursor up ARG patches"
503 (stgit-next-patch (- arg))
504 (while (not (zerop arg))
506 (while (progn (stgit-previous-line)
507 (not (stgit-patch-at-point)))))))
509 (defvar stgit-mode-hook nil
510 "Run after `stgit-mode' is setup.")
512 (defvar stgit-mode-map nil
513 "Keymap for StGit major mode.")
515 (unless stgit-mode-map
516 (setq stgit-mode-map (make-keymap))
517 (suppress-keymap stgit-mode-map)
518 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
521 ("\d" . stgit-unmark-up)
522 ("u" . stgit-unmark-down)
525 ("\C-p" . stgit-previous-line)
526 ("\C-n" . stgit-next-line)
527 ([up] . stgit-previous-line)
528 ([down] . stgit-next-line)
529 ("p" . stgit-previous-patch)
530 ("n" . stgit-next-patch)
531 ("\M-{" . stgit-previous-patch)
532 ("\M-}" . stgit-next-patch)
533 ("s" . stgit-git-status)
535 ("r" . stgit-refresh)
536 ("\C-c\C-r" . stgit-rename)
538 ("M" . stgit-move-patches)
543 ("U" . stgit-uncommit)
544 ("\r" . stgit-select)
545 ("o" . stgit-find-file-other-window)
546 (">" . stgit-push-next)
547 ("<" . stgit-pop-next)
548 ("P" . stgit-push-or-pop)
552 ([(control ?/)] . stgit-undo)
553 ("\C-_" . stgit-undo)
555 ("q" . stgit-quit))))
558 "Major mode for interacting with StGit.
561 (kill-all-local-variables)
562 (buffer-disable-undo)
563 (setq mode-name "StGit"
564 major-mode 'stgit-mode
566 (use-local-map stgit-mode-map)
567 (set (make-local-variable 'list-buffers-directory) default-directory)
568 (set (make-local-variable 'stgit-marked-patches) nil)
569 (set (make-local-variable 'stgit-expanded-patches) nil)
570 (set-variable 'truncate-lines 't)
571 (run-hooks 'stgit-mode-hook))
573 (defun stgit-add-mark (patchsym)
574 "Mark the patch PATCHSYM."
575 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))
577 (when (stgit-goto-patch patchsym)
579 (let ((inhibit-read-only t))
580 (insert-and-inherit ?*)
583 (defun stgit-remove-mark (patchsym)
584 "Unmark the patch PATCHSYM."
585 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))
587 (when (stgit-goto-patch patchsym)
589 (let ((inhibit-read-only t))
590 (insert-and-inherit ? )
593 (defun stgit-clear-marks ()
594 "Unmark all patches."
595 (setq stgit-marked-patches '()))
597 (defun stgit-patch-at-point (&optional cause-error)
598 "Return the patch name on the current line as a symbol.
599 If CAUSE-ERROR is not nil, signal an error if none found."
600 (or (get-text-property (point) 'stgit-patchsym)
602 (error "No patch on this line"))))
604 (defun stgit-patched-file-at-point (&optional both-files)
605 "Returns a cons of the patchsym and file name at point. For
606 copies and renames, return the new file if the patch is either
607 applied. If BOTH-FILES is non-nil, return a cons of the old and
608 the new file names instead of just one name."
609 (let ((patchsym (get-text-property (point) 'stgit-file-patchsym))
610 (file (get-text-property (point) 'stgit-file)))
611 (cond ((not patchsym) nil)
612 (file (cons patchsym file))
614 (cons patchsym (cons (get-text-property (point) 'stgit-old-file)
615 (get-text-property (point) 'stgit-new-file))))
617 (let ((file-sym (save-excursion
618 (stgit-previous-patch)
619 (unless (eq (stgit-patch-at-point)
621 (error "Cannot find the %s patch" patchsym))
623 (if (= (char-after) ?-)
626 (cons patchsym (get-text-property (point) file-sym)))))))
628 (defun stgit-patches-marked-or-at-point ()
629 "Return the symbols of the marked patches, or the patch on the current line."
630 (if stgit-marked-patches
632 (let ((patch (stgit-patch-at-point)))
637 (defun stgit-goto-patch (patchsym)
638 "Move point to the line containing patch PATCHSYM.
639 If that patch cannot be found, return nil."
640 (let ((p (text-property-any (point-min) (point-max)
641 'stgit-patchsym patchsym)))
644 (move-to-column goal-column))))
649 (stgit-capture-output nil
654 "Mark the patch under point."
656 (let ((patch (stgit-patch-at-point t)))
657 (stgit-add-mark patch))
660 (defun stgit-unmark-up ()
661 "Remove mark from the patch on the previous line."
663 (stgit-previous-patch)
664 (stgit-remove-mark (stgit-patch-at-point t)))
666 (defun stgit-unmark-down ()
667 "Remove mark from the patch on the current line."
669 (stgit-remove-mark (stgit-patch-at-point t))
672 (defun stgit-rename (name)
673 "Rename the patch under point to NAME."
674 (interactive (list (read-string "Patch name: "
675 (symbol-name (stgit-patch-at-point t)))))
676 (let ((old-patchsym (stgit-patch-at-point t)))
677 (stgit-capture-output nil
678 (stgit-run "rename" old-patchsym name))
679 (let ((name-sym (intern name)))
680 (when (memq old-patchsym stgit-expanded-patches)
681 (setq stgit-expanded-patches
682 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
683 (when (memq old-patchsym stgit-marked-patches)
684 (setq stgit-marked-patches
685 (cons name-sym (delq old-patchsym stgit-marked-patches))))
687 (stgit-goto-patch name-sym))))
689 (defun stgit-repair ()
692 (stgit-capture-output nil
693 (stgit-run "repair"))
696 (defun stgit-available-branches ()
697 "Returns a list of the available stg branches"
698 (let ((output (with-output-to-string
699 (stgit-run "branch" "--list")))
702 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
703 (setq result (cons (match-string 1 output) result))
704 (setq start (match-end 0)))
707 (defun stgit-branch (branch)
708 "Switch to branch BRANCH."
709 (interactive (list (completing-read "Switch to branch: "
710 (stgit-available-branches))))
711 (stgit-capture-output nil (stgit-run "branch" "--" branch))
714 (defun stgit-commit (count)
715 "Run stg commit on COUNT commits.
716 Interactively, the prefix argument is used as COUNT."
718 (stgit-capture-output nil (stgit-run "commit" "-n" count))
721 (defun stgit-uncommit (count)
722 "Run stg uncommit on COUNT commits.
723 Interactively, the prefix argument is used as COUNT."
725 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
728 (defun stgit-push-next (npatches)
729 "Push the first unapplied patch.
730 With numeric prefix argument, push that many patches."
732 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
734 (stgit-refresh-git-status))
736 (defun stgit-pop-next (npatches)
737 "Pop the topmost applied patch.
738 With numeric prefix argument, pop that many patches."
740 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
742 (stgit-refresh-git-status))
744 (defun stgit-applied-at-point ()
745 "Is the patch on the current line applied?"
748 (looking-at "[>+]")))
750 (defun stgit-push-or-pop ()
751 "Push or pop the patch on the current line."
753 (let ((patchsym (stgit-patch-at-point t))
754 (applied (stgit-applied-at-point)))
755 (stgit-capture-output nil
756 (stgit-run (if applied "pop" "push") patchsym))
760 "Go to the patch on the current line."
762 (let ((patchsym (stgit-patch-at-point t)))
763 (stgit-capture-output nil
764 (stgit-run "goto" patchsym))
767 (defun stgit-id (patchsym)
768 "Return the git commit id for PATCHSYM."
769 (let ((result (with-output-to-string
770 (stgit-run-silent "id" patchsym))))
771 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
772 (error "Cannot find commit id for %s" patchsym))
773 (match-string 1 result)))
776 "Show the patch on the current line."
778 (stgit-capture-output "*StGit patch*"
779 (let ((patchsym (stgit-patch-at-point)))
781 (let ((patched-file (stgit-patched-file-at-point t)))
783 (error "No patch or file at point"))
784 (let ((id (stgit-id (car patched-file))))
785 (if (consp (cdr patched-file))
786 ;; two files (copy or rename)
787 (stgit-run-git "diff" "-C" "-C" (concat id "^") id "--"
788 (cadr patched-file) (cddr patched-file))
790 (stgit-run-git "diff" (concat id "^") id "--"
791 (cdr patched-file)))))
792 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M" patchsym))
793 (with-current-buffer standard-output
794 (goto-char (point-min))
798 "Edit the patch on the current line."
800 (let ((patchsym (stgit-patch-at-point t))
801 (edit-buf (get-buffer-create "*StGit edit*"))
802 (dir default-directory))
803 (log-edit 'stgit-confirm-edit t nil edit-buf)
804 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
805 (setq default-directory dir)
806 (let ((standard-output edit-buf))
807 (stgit-run-silent "edit" "--save-template=-" patchsym))))
809 (defun stgit-confirm-edit ()
811 (let ((file (make-temp-file "stgit-edit-")))
812 (write-region (point-min) (point-max) file)
813 (stgit-capture-output nil
814 (stgit-run "edit" "-f" file stgit-edit-patchsym))
815 (with-current-buffer log-edit-parent-buffer
818 (defun stgit-new (add-sign)
820 With a prefix argument, include a \"Signed-off-by:\" line at the
823 (let ((edit-buf (get-buffer-create "*StGit edit*"))
824 (dir default-directory))
825 (log-edit 'stgit-confirm-new t nil edit-buf)
826 (setq default-directory dir)
829 (let ((standard-output (current-buffer)))
830 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
832 (defun stgit-confirm-new ()
834 (let ((file (make-temp-file "stgit-edit-")))
835 (write-region (point-min) (point-max) file)
836 (stgit-capture-output nil
837 (stgit-run "new" "-f" file))
838 (with-current-buffer log-edit-parent-buffer
841 (defun stgit-create-patch-name (description)
842 "Create a patch name from a long description"
844 (while (> (length description) 0)
845 (cond ((string-match "\\`[a-zA-Z_-]+" description)
846 (setq patch (downcase (concat patch
847 (match-string 0 description))))
848 (setq description (substring description (match-end 0))))
849 ((string-match "\\` +" description)
850 (setq patch (concat patch "-"))
851 (setq description (substring description (match-end 0))))
852 ((string-match "\\`[^a-zA-Z_-]+" description)
853 (setq description (substring description (match-end 0))))))
854 (cond ((= (length patch) 0)
856 ((> (length patch) 20)
857 (substring patch 0 20))
860 (defun stgit-delete (patchsyms &optional spill-p)
861 "Delete the patches in PATCHSYMS.
862 Interactively, delete the marked patches, or the patch at point.
864 With a prefix argument, or SPILL-P, spill the patch contents to
865 the work tree and index."
866 (interactive (list (stgit-patches-marked-or-at-point)
869 (error "No patches to delete"))
870 (let ((npatches (length patchsyms)))
871 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
873 (if (= 1 npatches) "" "es")
875 " (spilling contents to index)"
877 (let ((args (if spill-p
878 (cons "--spill" patchsyms)
880 (stgit-capture-output nil
881 (apply 'stgit-run "delete" args))
884 (defun stgit-move-patches-target ()
885 "Return the patchsym indicating a target patch for
886 `stgit-move-patches'.
888 This is either the patch at point, or one of :top and :bottom, if
889 the point is after or before the applied patches."
891 (let ((patchsym (stgit-patch-at-point)))
892 (cond (patchsym patchsym)
893 ((save-excursion (re-search-backward "^>" nil t)) :top)
896 (defun stgit-sort-patches (patchsyms)
897 "Returns the list of patches in PATCHSYMS sorted according to
898 their position in the patch series, bottommost first.
900 PATCHSYMS may not contain duplicate entries."
901 (let (sorted-patchsyms
902 (series (with-output-to-string
903 (with-current-buffer standard-output
904 (stgit-run-silent "series" "--noprefix"))))
906 (while (string-match "^\\(.+\\)" series start)
907 (let ((patchsym (intern (match-string 1 series))))
908 (when (memq patchsym patchsyms)
909 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
910 (setq start (match-end 0)))
911 (setq sorted-patchsyms (nreverse sorted-patchsyms))
913 (unless (= (length patchsyms) (length sorted-patchsyms))
914 (error "Internal error"))
918 (defun stgit-move-patches (patchsyms target-patch)
919 "Move the patches in PATCHSYMS to below TARGET-PATCH.
920 If TARGET-PATCH is :bottom or :top, move the patches to the
921 bottom or top of the stack, respectively.
923 Interactively, move the marked patches to where the point is."
924 (interactive (list stgit-marked-patches
925 (stgit-move-patches-target)))
927 (error "Need at least one patch to move"))
930 (error "Point not at a patch"))
932 (if (eq target-patch :top)
933 (stgit-capture-output nil
934 (apply 'stgit-run "float" patchsyms))
936 ;; need to have patchsyms sorted by position in the stack
937 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
938 (while sorted-patchsyms
939 (setq sorted-patchsyms
940 (and (stgit-capture-output nil
941 (if (eq target-patch :bottom)
942 (stgit-run "sink" "--" (car sorted-patchsyms))
943 (stgit-run "sink" "--to" target-patch "--"
944 (car sorted-patchsyms))))
945 (cdr sorted-patchsyms))))))
948 (defun stgit-squash (patchsyms)
949 "Squash the patches in PATCHSYMS.
950 Interactively, squash the marked patches.
952 Unless there are any conflicts, the patches will be merged into
953 one patch, which will occupy the same spot in the series as the
954 deepest patch had before the squash."
955 (interactive (list stgit-marked-patches))
956 (when (< (length patchsyms) 2)
957 (error "Need at least two patches to squash"))
958 (let ((stgit-buffer (current-buffer))
959 (edit-buf (get-buffer-create "*StGit edit*"))
960 (dir default-directory)
961 (sorted-patchsyms (stgit-sort-patches patchsyms)))
962 (log-edit 'stgit-confirm-squash t nil edit-buf)
963 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
964 (setq default-directory dir)
965 (let ((result (let ((standard-output edit-buf))
966 (apply 'stgit-run-silent "squash"
967 "--save-template=-" sorted-patchsyms))))
969 ;; stg squash may have reordered the patches or caused conflicts
970 (with-current-buffer stgit-buffer
973 (unless (eq 0 result)
975 (rename-buffer "*StGit error*")
976 (resize-temp-buffer-window)
977 (switch-to-buffer-other-window stgit-buffer)
978 (error "stg squash failed")))))
980 (defun stgit-confirm-squash ()
982 (let ((file (make-temp-file "stgit-edit-")))
983 (write-region (point-min) (point-max) file)
984 (stgit-capture-output nil
985 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
986 (with-current-buffer log-edit-parent-buffer
988 ;; Go to first marked patch and stay there
989 (goto-char (point-min))
990 (re-search-forward (concat "^[>+-]\\*") nil t)
991 (move-to-column goal-column)
997 "Display help for the StGit mode."
999 (describe-function 'stgit-mode))
1001 (defun stgit-undo (&optional arg)
1003 With prefix argument, run it with the --hard flag."
1005 (stgit-capture-output nil
1007 (stgit-run "undo" "--hard")
1008 (stgit-run "undo")))
1011 (defun stgit-refresh (&optional arg)
1013 With prefix argument, refresh the marked patch or the patch under point."
1015 (let ((patchargs (if arg
1016 (let ((patches (stgit-patches-marked-or-at-point)))
1017 (cond ((null patches)
1018 (error "No patch to update"))
1019 ((> (length patches) 1)
1020 (error "Too many patches selected"))
1022 (cons "-p" patches))))
1024 (stgit-capture-output nil
1025 (apply 'stgit-run "refresh" patchargs))
1026 (stgit-refresh-git-status))