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'
17 "Manage StGit patches for the tree in DIR."
18 (interactive "DDirectory: \n")
19 (switch-to-stgit-buffer (git-get-top-dir dir))
22 (unless (fboundp 'git-get-top-dir)
23 (defun git-get-top-dir (dir)
24 "Retrieve the top-level directory of a git tree."
25 (let ((cdup (with-output-to-string
26 (with-current-buffer standard-output
28 (unless (eq 0 (call-process "git" nil t nil
29 "rev-parse" "--show-cdup"))
30 (error "Cannot find top-level git tree for %s" dir))))))
31 (expand-file-name (concat (file-name-as-directory dir)
32 (car (split-string cdup "\n")))))))
34 (defun stgit-refresh-git-status (&optional dir)
35 "If it exists, refresh the `git-status' buffer belonging to
36 directory DIR or `default-directory'"
37 (when (and (fboundp 'git-find-status-buffer)
38 (fboundp 'git-refresh-status))
39 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
40 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
41 (when git-status-buffer
42 (with-current-buffer git-status-buffer
43 (git-refresh-status))))))
45 (defun switch-to-stgit-buffer (dir)
46 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
47 (setq dir (file-name-as-directory dir))
48 (let ((buffers (buffer-list)))
50 (not (with-current-buffer (car buffers)
51 (and (eq major-mode 'stgit-mode)
52 (string= default-directory dir)))))
53 (setq buffers (cdr buffers)))
54 (switch-to-buffer (if buffers
56 (create-stgit-buffer dir)))))
57 (defstruct (stgit-patch)
58 status name desc empty)
60 (defun stgit-patch-pp (patch)
61 (let ((status (stgit-patch-status patch))
63 (name (stgit-patch-name patch)))
69 (if (memq name stgit-marked-patches)
71 (propertize (format "%-30s" (symbol-name name))
73 ('applied 'stgit-applied-patch-face)
74 ('top 'stgit-top-patch-face)
75 ('unapplied 'stgit-unapplied-patch-face)))
77 (if (stgit-patch-empty patch) "(empty) " "")
78 (propertize (or (stgit-patch-desc patch) "")
79 'face 'stgit-description-face))
80 (put-text-property start (point) 'entry-type 'patch)
81 (when (memq name stgit-expanded-patches)
82 (stgit-insert-patch-files name))
83 (put-text-property start (point) 'patch-data patch)))
85 (defun create-stgit-buffer (dir)
86 "Create a buffer for showing StGit patches.
87 Argument DIR is the repository path."
88 (let ((buf (create-file-buffer (concat dir "*stgit*")))
89 (inhibit-read-only t))
90 (with-current-buffer buf
91 (setq default-directory dir)
93 (set (make-local-variable 'stgit-ewoc)
94 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
95 (setq buffer-read-only t))
98 (defmacro stgit-capture-output (name &rest body)
99 "Capture StGit output and, if there was any output, show it in a window
101 Returns nil if there was no output."
102 (declare (debug ([&or stringp null] body))
104 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
105 (stgit-dir default-directory)
106 (inhibit-read-only t))
107 (with-current-buffer output-buf
109 (setq default-directory stgit-dir)
110 (setq buffer-read-only t))
111 (let ((standard-output output-buf))
113 (with-current-buffer output-buf
114 (set-buffer-modified-p nil)
115 (setq buffer-read-only t)
116 (if (< (point-min) (point-max))
117 (display-buffer output-buf t)))))
119 (defun stgit-make-run-args (args)
120 "Return a copy of ARGS with its elements converted to strings."
122 ;; don't use (format "%s" ...) to limit type errors
123 (cond ((stringp x) x)
124 ((integerp x) (number-to-string x))
125 ((symbolp x) (symbol-name x))
127 (error "Bad element in stgit-make-run-args args: %S" x))))
130 (defun stgit-run-silent (&rest args)
131 (setq args (stgit-make-run-args args))
132 (apply 'call-process "stg" nil standard-output nil args))
134 (defun stgit-run (&rest args)
135 (setq args (stgit-make-run-args args))
136 (let ((msgcmd (mapconcat #'identity args " ")))
137 (message "Running stg %s..." msgcmd)
138 (apply 'call-process "stg" nil standard-output nil args)
139 (message "Running stg %s...done" msgcmd)))
141 (defun stgit-run-git (&rest args)
142 (setq args (stgit-make-run-args args))
143 (let ((msgcmd (mapconcat #'identity args " ")))
144 (message "Running git %s..." msgcmd)
145 (apply 'call-process "git" nil standard-output nil args)
146 (message "Running git %s...done" msgcmd)))
148 (defun stgit-run-git-silent (&rest args)
149 (setq args (stgit-make-run-args args))
150 (apply 'call-process "git" nil standard-output nil args))
152 (defun stgit-run-series (ewoc)
153 (let ((first-line t))
155 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
156 (goto-char (point-min))
157 (if (not (zerop exit-status))
158 (cond ((looking-at "stg series: \\(.*\\)")
159 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
160 "-- not initialized (run M-x stgit-init)"))
162 (error "Error running stg: %s"
166 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
167 (error "Syntax error in output from stg series"))
168 (let* ((state-str (match-string 2))
169 (state (cond ((string= state-str ">") 'top)
170 ((string= state-str "+") 'applied)
171 ((string= state-str "-") 'unapplied))))
172 (ewoc-enter-last ewoc
175 :name (intern (match-string 4))
176 :desc (match-string 5)
177 :empty (string= (match-string 1) "0"))))
178 (setq first-line nil)
179 (forward-line 1)))))))
182 (defun stgit-reload ()
183 "Update the contents of the StGit buffer."
185 (let ((inhibit-read-only t)
186 (curline (line-number-at-pos))
187 (curpatch (stgit-patch-name-at-point)))
188 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
189 (ewoc-set-hf stgit-ewoc
193 (stgit-run-silent "branch")
194 (buffer-substring (point-min) (1- (point-max))))
198 (stgit-run-series stgit-ewoc)
200 (stgit-goto-patch curpatch)
201 (goto-line curline)))
202 (stgit-refresh-git-status))
205 "A user interface for the StGit patch maintenance tool."
208 (defface stgit-description-face
209 '((((background dark)) (:foreground "tan"))
210 (((background light)) (:foreground "dark red")))
211 "The face used for StGit descriptions"
214 (defface stgit-top-patch-face
215 '((((background dark)) (:weight bold :foreground "yellow"))
216 (((background light)) (:weight bold :foreground "purple"))
218 "The face used for the top patch names"
221 (defface stgit-applied-patch-face
222 '((((background dark)) (:foreground "light yellow"))
223 (((background light)) (:foreground "purple"))
225 "The face used for applied patch names"
228 (defface stgit-unapplied-patch-face
229 '((((background dark)) (:foreground "gray80"))
230 (((background light)) (:foreground "orchid"))
232 "The face used for unapplied patch names"
235 (defface stgit-modified-file-face
236 '((((class color) (background light)) (:foreground "purple"))
237 (((class color) (background dark)) (:foreground "salmon")))
238 "StGit mode face used for modified file status"
241 (defface stgit-unmerged-file-face
242 '((((class color) (background light)) (:foreground "red" :bold t))
243 (((class color) (background dark)) (:foreground "red" :bold t)))
244 "StGit mode face used for unmerged file status"
247 (defface stgit-unknown-file-face
248 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
249 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
250 "StGit mode face used for unknown file status"
253 (defface stgit-file-permission-face
254 '((((class color) (background light)) (:foreground "green" :bold t))
255 (((class color) (background dark)) (:foreground "green" :bold t)))
256 "StGit mode face used for permission changes."
259 (defcustom stgit-expand-find-copies-harder
261 "Try harder to find copied files when listing patches.
263 When not nil, runs git diff-tree with the --find-copies-harder
264 flag, which reduces performance."
268 (defconst stgit-file-status-code-strings
269 (mapcar (lambda (arg)
271 (propertize (cadr arg) 'face (car (cddr arg)))))
272 '((add "Added" stgit-modified-file-face)
273 (copy "Copied" stgit-modified-file-face)
274 (delete "Deleted" stgit-modified-file-face)
275 (modify "Modified" stgit-modified-file-face)
276 (rename "Renamed" stgit-modified-file-face)
277 (mode-change "Mode change" stgit-modified-file-face)
278 (unmerged "Unmerged" stgit-unmerged-file-face)
279 (unknown "Unknown" stgit-unknown-file-face)))
280 "Alist of code symbols to description strings")
282 (defun stgit-file-status-code-as-string (code)
283 "Return stgit status code as string"
284 (let ((str (assq (if (consp code) (car code) code)
285 stgit-file-status-code-strings)))
288 (if (and str (consp code) (/= (cdr code) 100))
289 (format "%s %s" (cdr str)
290 (propertize (format "%d%%" (cdr code))
291 'face 'stgit-description-face))
294 (defun stgit-file-status-code (str &optional score)
295 "Return stgit status code from git status string"
296 (let ((code (assoc str '(("A" . add)
304 (setq code (if code (cdr code) 'unknown))
305 (when (stringp score)
306 (if (> (length score) 0)
307 (setq score (string-to-number score))
309 (if score (cons code score) code)))
311 (defconst stgit-file-type-strings
314 (#o160 . "subproject"))
315 "Alist of names of file types")
317 (defun stgit-file-type-string (type)
318 "Return string describing file type TYPE (the high bits of file permission).
319 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
320 (let ((type-str (assoc type stgit-file-type-strings)))
321 (or (and type-str (cdr type-str))
322 (format "unknown type %o" type))))
324 (defun stgit-file-type-change-string (old-perm new-perm)
325 "Return string describing file type change from OLD-PERM to NEW-PERM.
326 Cf. `stgit-file-type-string'."
327 (let ((old-type (lsh old-perm -9))
328 (new-type (lsh new-perm -9)))
329 (cond ((= old-type new-type) "")
330 ((zerop new-type) "")
332 (if (= new-type #o100)
334 (format " (%s)" (stgit-file-type-string new-type))))
335 (t (format " (%s -> %s)"
336 (stgit-file-type-string old-type)
337 (stgit-file-type-string new-type))))))
339 (defun stgit-file-mode-change-string (old-perm new-perm)
340 "Return string describing file mode change from OLD-PERM to NEW-PERM.
341 Cf. `stgit-file-type-change-string'."
342 (setq old-perm (logand old-perm #o777)
343 new-perm (logand new-perm #o777))
344 (if (or (= old-perm new-perm)
348 (let* ((modified (logxor old-perm new-perm))
349 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
350 (cond ((zerop modified) "")
351 ((and (zerop not-x-modified)
352 (or (and (eq #o111 (logand old-perm #o111))
353 (propertize "-x" 'face 'stgit-file-permission-face))
354 (and (eq #o111 (logand new-perm #o111))
355 (propertize "+x" 'face
356 'stgit-file-permission-face)))))
357 (t (concat (propertize (format "%o" old-perm)
358 'face 'stgit-file-permission-face)
360 'face 'stgit-description-face)
361 (propertize (format "%o" new-perm)
362 'face 'stgit-file-permission-face)))))))
364 (defun stgit-insert-patch-files (patchsym)
365 (let* ((start (point))
366 (result (with-output-to-string
367 (stgit-run-git "diff-tree" "-r" "-z"
368 (if stgit-expand-find-copies-harder
369 "--find-copies-harder"
371 (stgit-id patchsym)))))
373 (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]*\\)\\)"
375 (let ((copy-or-rename (match-string 4 result))
376 (old-perm (read (format "#o%s" (match-string 1 result))))
377 (new-perm (read (format "#o%s" (match-string 2 result))))
381 (properties '(entry-type file)))
383 (let ((cr-score (match-string 5 result))
384 (cr-from-file (match-string 6 result))
385 (cr-to-file (match-string 7 result)))
386 (setq status (stgit-file-status-code copy-or-rename
388 properties (list* 'stgit-old-file cr-from-file
389 'stgit-new-file cr-to-file
394 'face 'stgit-description-face)
396 (setq status (stgit-file-status-code (match-string 8 result))
397 properties (list* 'stgit-file (match-string 9 result)
399 change (match-string 9 result)))
401 (let ((mode-change (stgit-file-mode-change-string old-perm
404 (format "%-12s" (stgit-file-status-code-as-string
407 (if (> (length mode-change) 0) " " "")
409 (propertize (stgit-file-type-change-string old-perm
411 'face 'stgit-description-face)))
412 (add-text-properties line-start (point) properties))
413 (setq mstart (match-end 0))))
414 (when (= start (point))
415 (insert " <no files>\n"))
416 (put-text-property start (point) 'stgit-file-patchsym patchsym)))
418 (defun stgit-select-file ()
419 (let ((patched-file (stgit-patched-file-at-point)))
421 (error "No patch or file on the current line"))
422 (let ((filename (expand-file-name (cdr patched-file))))
423 (unless (file-exists-p filename)
424 (error "File does not exist"))
425 (find-file filename))))
427 (defun stgit-select-patch ()
428 (let ((patchname (stgit-patch-name-at-point)))
429 (if (memq patchname stgit-expanded-patches)
430 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
431 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
432 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
433 (move-to-column (stgit-goal-column)))
435 (defun stgit-select ()
436 "Expand or collapse the current entry"
438 (case (get-text-property (point) 'entry-type)
440 (stgit-select-patch))
444 (error "No patch or file on line"))))
446 (defun stgit-find-file-other-window ()
447 "Open file at point in other window"
449 (let ((patched-file (stgit-patched-file-at-point)))
451 (error "No file on the current line"))
452 (let ((filename (expand-file-name (cdr patched-file))))
453 (unless (file-exists-p filename)
454 (error "File does not exist"))
455 (find-file-other-window filename))))
458 "Hide the stgit buffer."
462 (defun stgit-git-status ()
463 "Show status using `git-status'."
465 (unless (fboundp 'git-status)
466 (error "The stgit-git-status command requires git-status"))
467 (let ((dir default-directory))
468 (save-selected-window
472 (defun stgit-goal-column ()
473 "Return goal column for the current line"
474 (case (get-text-property (point) 'entry-type)
479 (defun stgit-next-line (&optional arg)
480 "Move cursor vertically down ARG lines"
483 (move-to-column (stgit-goal-column)))
485 (defun stgit-previous-line (&optional arg)
486 "Move cursor vertically up ARG lines"
489 (move-to-column (stgit-goal-column)))
491 (defun stgit-next-patch (&optional arg)
492 "Move cursor down ARG patches."
494 (ewoc-goto-next stgit-ewoc (or arg 1))
495 (move-to-column goal-column))
497 (defun stgit-previous-patch (&optional arg)
498 "Move cursor up ARG patches."
500 (ewoc-goto-prev stgit-ewoc (or arg 1))
501 (move-to-column goal-column))
503 (defvar stgit-mode-hook nil
504 "Run after `stgit-mode' is setup.")
506 (defvar stgit-mode-map nil
507 "Keymap for StGit major mode.")
509 (unless stgit-mode-map
510 (setq stgit-mode-map (make-keymap))
511 (suppress-keymap stgit-mode-map)
512 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
515 ("\d" . stgit-unmark-up)
516 ("u" . stgit-unmark-down)
519 ("\C-p" . stgit-previous-line)
520 ("\C-n" . stgit-next-line)
521 ([up] . stgit-previous-line)
522 ([down] . stgit-next-line)
523 ("p" . stgit-previous-patch)
524 ("n" . stgit-next-patch)
525 ("\M-{" . stgit-previous-patch)
526 ("\M-}" . stgit-next-patch)
527 ("s" . stgit-git-status)
529 ("r" . stgit-refresh)
530 ("\C-c\C-r" . stgit-rename)
532 ("M" . stgit-move-patches)
537 ("U" . stgit-uncommit)
538 ("\r" . stgit-select)
539 ("o" . stgit-find-file-other-window)
540 (">" . stgit-push-next)
541 ("<" . stgit-pop-next)
542 ("P" . stgit-push-or-pop)
546 ([(control ?/)] . stgit-undo)
547 ("\C-_" . stgit-undo)
549 ("q" . stgit-quit))))
552 "Major mode for interacting with StGit.
555 (kill-all-local-variables)
556 (buffer-disable-undo)
557 (setq mode-name "StGit"
558 major-mode 'stgit-mode
560 (use-local-map stgit-mode-map)
561 (set (make-local-variable 'list-buffers-directory) default-directory)
562 (set (make-local-variable 'stgit-marked-patches) nil)
563 (set (make-local-variable 'stgit-expanded-patches) nil)
564 (set-variable 'truncate-lines 't)
565 (run-hooks 'stgit-mode-hook))
567 (defun stgit-add-mark (patchsym)
568 "Mark the patch PATCHSYM."
569 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
571 (defun stgit-remove-mark (patchsym)
572 "Unmark the patch PATCHSYM."
573 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
575 (defun stgit-clear-marks ()
576 "Unmark all patches."
577 (setq stgit-marked-patches '()))
579 (defun stgit-patch-at-point (&optional cause-error)
580 (get-text-property (point) 'patch-data))
582 (defun stgit-patch-name-at-point (&optional cause-error)
583 "Return the patch name on the current line as a symbol.
584 If CAUSE-ERROR is not nil, signal an error if none found."
585 (let ((patch (stgit-patch-at-point)))
587 (stgit-patch-name patch))
589 (error "No patch on this line")))))
591 (defun stgit-patched-file-at-point (&optional both-files)
592 "Returns a cons of the patchsym and file name at point. For
593 copies and renames, return the new file if the patch is either
594 applied. If BOTH-FILES is non-nil, return a cons of the old and
595 the new file names instead of just one name."
596 (let ((patchsym (get-text-property (point) 'stgit-file-patchsym))
597 (file (get-text-property (point) 'stgit-file)))
598 (cond ((not patchsym) nil)
599 (file (cons patchsym file))
601 (cons patchsym (cons (get-text-property (point) 'stgit-old-file)
602 (get-text-property (point) 'stgit-new-file))))
604 (let ((file-sym (save-excursion
605 (stgit-previous-patch)
606 (unless (eq (stgit-patch-name-at-point)
608 (error "Cannot find the %s patch" patchsym))
610 (if (= (char-after) ?-)
613 (cons patchsym (get-text-property (point) file-sym)))))))
615 (defun stgit-patches-marked-or-at-point ()
616 "Return the symbols of the marked patches, or the patch on the current line."
617 (if stgit-marked-patches
619 (let ((patch (stgit-patch-name-at-point)))
624 (defun stgit-goto-patch (patchsym)
625 "Move point to the line containing patch PATCHSYM.
626 If that patch cannot be found, do nothing."
627 (let ((node (ewoc-nth stgit-ewoc 0)))
628 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
630 (setq node (ewoc-next stgit-ewoc node)))
632 (ewoc-goto-node stgit-ewoc node)
633 (move-to-column goal-column))))
638 (stgit-capture-output nil
643 "Mark the patch under point."
645 (let* ((node (ewoc-locate stgit-ewoc))
646 (patch (ewoc-data node)))
647 (stgit-add-mark (stgit-patch-name patch))
648 (ewoc-invalidate stgit-ewoc node))
651 (defun stgit-unmark-up ()
652 "Remove mark from the patch on the previous line."
654 (stgit-previous-patch)
655 (let* ((node (ewoc-locate stgit-ewoc))
656 (patch (ewoc-data node)))
657 (stgit-remove-mark (stgit-patch-name patch))
658 (ewoc-invalidate stgit-ewoc node))
659 (move-to-column (stgit-goal-column)))
661 (defun stgit-unmark-down ()
662 "Remove mark from the patch on the current line."
664 (let* ((node (ewoc-locate stgit-ewoc))
665 (patch (ewoc-data node)))
666 (stgit-remove-mark (stgit-patch-name patch))
667 (ewoc-invalidate stgit-ewoc node))
670 (defun stgit-rename (name)
671 "Rename the patch under point to NAME."
672 (interactive (list (read-string "Patch name: "
673 (symbol-name (stgit-patch-name-at-point t)))))
674 (let ((old-patchsym (stgit-patch-name-at-point t)))
675 (stgit-capture-output nil
676 (stgit-run "rename" old-patchsym name))
677 (let ((name-sym (intern name)))
678 (when (memq old-patchsym stgit-expanded-patches)
679 (setq stgit-expanded-patches
680 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
681 (when (memq old-patchsym stgit-marked-patches)
682 (setq stgit-marked-patches
683 (cons name-sym (delq old-patchsym stgit-marked-patches))))
685 (stgit-goto-patch name-sym))))
687 (defun stgit-repair ()
690 (stgit-capture-output nil
691 (stgit-run "repair"))
694 (defun stgit-available-branches ()
695 "Returns a list of the available stg branches"
696 (let ((output (with-output-to-string
697 (stgit-run "branch" "--list")))
700 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
701 (setq result (cons (match-string 1 output) result))
702 (setq start (match-end 0)))
705 (defun stgit-branch (branch)
706 "Switch to branch BRANCH."
707 (interactive (list (completing-read "Switch to branch: "
708 (stgit-available-branches))))
709 (stgit-capture-output nil (stgit-run "branch" "--" branch))
712 (defun stgit-commit (count)
713 "Run stg commit on COUNT commits.
714 Interactively, the prefix argument is used as COUNT."
716 (stgit-capture-output nil (stgit-run "commit" "-n" count))
719 (defun stgit-uncommit (count)
720 "Run stg uncommit on COUNT commits.
721 Interactively, the prefix argument is used as COUNT."
723 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
726 (defun stgit-push-next (npatches)
727 "Push the first unapplied patch.
728 With numeric prefix argument, push that many patches."
730 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
732 (stgit-refresh-git-status))
734 (defun stgit-pop-next (npatches)
735 "Pop the topmost applied patch.
736 With numeric prefix argument, pop that many patches."
738 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
740 (stgit-refresh-git-status))
742 (defun stgit-applied-at-point ()
743 "Is the patch on the current line applied?"
746 (looking-at "[>+]")))
748 (defun stgit-push-or-pop ()
749 "Push or pop the patch on the current line."
751 (let ((patchsym (stgit-patch-name-at-point t))
752 (applied (stgit-applied-at-point)))
753 (stgit-capture-output nil
754 (stgit-run (if applied "pop" "push") patchsym))
758 "Go to the patch on the current line."
760 (let ((patchsym (stgit-patch-name-at-point t)))
761 (stgit-capture-output nil
762 (stgit-run "goto" patchsym))
765 (defun stgit-id (patchsym)
766 "Return the git commit id for PATCHSYM.
767 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
768 (if (keywordp patchsym)
770 (let ((result (with-output-to-string
771 (stgit-run-silent "id" patchsym))))
772 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
773 (error "Cannot find commit id for %s" patchsym))
774 (match-string 1 result))))
777 "Show the patch on the current line."
779 (stgit-capture-output "*StGit patch*"
780 (case (get-text-property (point) 'entry-type)
782 (let ((patchsym (stgit-patch-name-at-point))
783 (patched-file (stgit-patched-file-at-point t)))
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))))))
793 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
794 (stgit-patch-name-at-point)))
796 (error "No patch or file at point")))
797 (with-current-buffer standard-output
798 (goto-char (point-min))
802 "Edit the patch on the current line."
804 (let ((patchsym (stgit-patch-name-at-point t))
805 (edit-buf (get-buffer-create "*StGit edit*"))
806 (dir default-directory))
807 (log-edit 'stgit-confirm-edit t nil edit-buf)
808 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
809 (setq default-directory dir)
810 (let ((standard-output edit-buf))
811 (stgit-run-silent "edit" "--save-template=-" patchsym))))
813 (defun stgit-confirm-edit ()
815 (let ((file (make-temp-file "stgit-edit-")))
816 (write-region (point-min) (point-max) file)
817 (stgit-capture-output nil
818 (stgit-run "edit" "-f" file stgit-edit-patchsym))
819 (with-current-buffer log-edit-parent-buffer
822 (defun stgit-new (add-sign)
824 With a prefix argument, include a \"Signed-off-by:\" line at the
827 (let ((edit-buf (get-buffer-create "*StGit edit*"))
828 (dir default-directory))
829 (log-edit 'stgit-confirm-new t nil edit-buf)
830 (setq default-directory dir)
833 (let ((standard-output (current-buffer)))
834 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
836 (defun stgit-confirm-new ()
838 (let ((file (make-temp-file "stgit-edit-")))
839 (write-region (point-min) (point-max) file)
840 (stgit-capture-output nil
841 (stgit-run "new" "-f" file))
842 (with-current-buffer log-edit-parent-buffer
845 (defun stgit-create-patch-name (description)
846 "Create a patch name from a long description"
848 (while (> (length description) 0)
849 (cond ((string-match "\\`[a-zA-Z_-]+" description)
850 (setq patch (downcase (concat patch
851 (match-string 0 description))))
852 (setq description (substring description (match-end 0))))
853 ((string-match "\\` +" description)
854 (setq patch (concat patch "-"))
855 (setq description (substring description (match-end 0))))
856 ((string-match "\\`[^a-zA-Z_-]+" description)
857 (setq description (substring description (match-end 0))))))
858 (cond ((= (length patch) 0)
860 ((> (length patch) 20)
861 (substring patch 0 20))
864 (defun stgit-delete (patchsyms &optional spill-p)
865 "Delete the patches in PATCHSYMS.
866 Interactively, delete the marked patches, or the patch at point.
868 With a prefix argument, or SPILL-P, spill the patch contents to
869 the work tree and index."
870 (interactive (list (stgit-patches-marked-or-at-point)
873 (error "No patches to delete"))
874 (let ((npatches (length patchsyms)))
875 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
877 (if (= 1 npatches) "" "es")
879 " (spilling contents to index)"
881 (let ((args (if spill-p
882 (cons "--spill" patchsyms)
884 (stgit-capture-output nil
885 (apply 'stgit-run "delete" args))
888 (defun stgit-move-patches-target ()
889 "Return the patchsym indicating a target patch for
890 `stgit-move-patches'.
892 This is either the patch at point, or one of :top and :bottom, if
893 the point is after or before the applied patches."
895 (let ((patchsym (stgit-patch-name-at-point)))
896 (cond (patchsym patchsym)
897 ((save-excursion (re-search-backward "^>" nil t)) :top)
900 (defun stgit-sort-patches (patchsyms)
901 "Returns the list of patches in PATCHSYMS sorted according to
902 their position in the patch series, bottommost first.
904 PATCHSYMS may not contain duplicate entries."
905 (let (sorted-patchsyms
906 (series (with-output-to-string
907 (with-current-buffer standard-output
908 (stgit-run-silent "series" "--noprefix"))))
910 (while (string-match "^\\(.+\\)" series start)
911 (let ((patchsym (intern (match-string 1 series))))
912 (when (memq patchsym patchsyms)
913 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
914 (setq start (match-end 0)))
915 (setq sorted-patchsyms (nreverse sorted-patchsyms))
917 (unless (= (length patchsyms) (length sorted-patchsyms))
918 (error "Internal error"))
922 (defun stgit-move-patches (patchsyms target-patch)
923 "Move the patches in PATCHSYMS to below TARGET-PATCH.
924 If TARGET-PATCH is :bottom or :top, move the patches to the
925 bottom or top of the stack, respectively.
927 Interactively, move the marked patches to where the point is."
928 (interactive (list stgit-marked-patches
929 (stgit-move-patches-target)))
931 (error "Need at least one patch to move"))
934 (error "Point not at a patch"))
936 (if (eq target-patch :top)
937 (stgit-capture-output nil
938 (apply 'stgit-run "float" patchsyms))
940 ;; need to have patchsyms sorted by position in the stack
941 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
942 (while sorted-patchsyms
943 (setq sorted-patchsyms
944 (and (stgit-capture-output nil
945 (if (eq target-patch :bottom)
946 (stgit-run "sink" "--" (car sorted-patchsyms))
947 (stgit-run "sink" "--to" target-patch "--"
948 (car sorted-patchsyms))))
949 (cdr sorted-patchsyms))))))
952 (defun stgit-squash (patchsyms)
953 "Squash the patches in PATCHSYMS.
954 Interactively, squash the marked patches.
956 Unless there are any conflicts, the patches will be merged into
957 one patch, which will occupy the same spot in the series as the
958 deepest patch had before the squash."
959 (interactive (list stgit-marked-patches))
960 (when (< (length patchsyms) 2)
961 (error "Need at least two patches to squash"))
962 (let ((stgit-buffer (current-buffer))
963 (edit-buf (get-buffer-create "*StGit edit*"))
964 (dir default-directory)
965 (sorted-patchsyms (stgit-sort-patches patchsyms)))
966 (log-edit 'stgit-confirm-squash t nil edit-buf)
967 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
968 (setq default-directory dir)
969 (let ((result (let ((standard-output edit-buf))
970 (apply 'stgit-run-silent "squash"
971 "--save-template=-" sorted-patchsyms))))
973 ;; stg squash may have reordered the patches or caused conflicts
974 (with-current-buffer stgit-buffer
977 (unless (eq 0 result)
979 (rename-buffer "*StGit error*")
980 (resize-temp-buffer-window)
981 (switch-to-buffer-other-window stgit-buffer)
982 (error "stg squash failed")))))
984 (defun stgit-confirm-squash ()
986 (let ((file (make-temp-file "stgit-edit-")))
987 (write-region (point-min) (point-max) file)
988 (stgit-capture-output nil
989 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
990 (with-current-buffer log-edit-parent-buffer
992 ;; Go to first marked patch and stay there
993 (goto-char (point-min))
994 (re-search-forward (concat "^[>+-]\\*") nil t)
995 (move-to-column goal-column)
1000 (defun stgit-help ()
1001 "Display help for the StGit mode."
1003 (describe-function 'stgit-mode))
1005 (defun stgit-undo (&optional arg)
1007 With prefix argument, run it with the --hard flag."
1009 (stgit-capture-output nil
1011 (stgit-run "undo" "--hard")
1012 (stgit-run "undo")))
1015 (defun stgit-refresh (&optional arg)
1017 With prefix argument, refresh the marked patch or the patch under point."
1019 (let ((patchargs (if arg
1020 (let ((patches (stgit-patches-marked-or-at-point)))
1021 (cond ((null patches)
1022 (error "No patch to update"))
1023 ((> (length patches) 1)
1024 (error "Too many patches selected"))
1026 (cons "-p" patches))))
1028 (stgit-capture-output nil
1029 (apply 'stgit-run "refresh" patchargs))
1030 (stgit-refresh-git-status))