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 stgit-find-buffer (dir)
46 "Return the buffer displaying StGit patches for DIR, or nil if none."
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 (and buffers (car buffers))))
56 (defun switch-to-stgit-buffer (dir)
57 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
58 (setq dir (file-name-as-directory dir))
59 (let ((buffer (stgit-find-buffer dir)))
60 (switch-to-buffer (or buffer
61 (create-stgit-buffer dir)))))
63 (defstruct (stgit-patch)
64 status name desc empty files-ewoc)
66 (defun stgit-patch-pp (patch)
67 (let ((status (stgit-patch-status patch))
69 (name (stgit-patch-name patch)))
73 'face 'stgit-index-work-tree-title-face)))
75 (propertize "Work tree"
76 'face 'stgit-index-work-tree-title-face)))
77 (t (insert (case status
81 (if (memq name stgit-marked-patches)
83 (propertize (format "%-30s"
86 ('applied 'stgit-applied-patch-face)
87 ('top 'stgit-top-patch-face)
88 ('unapplied 'stgit-unapplied-patch-face)
92 (if (stgit-patch-empty patch) "(empty) " "")
93 (propertize (or (stgit-patch-desc patch) "")
94 'face 'stgit-description-face))))
95 (put-text-property start (point) 'entry-type 'patch)
96 (when (memq name stgit-expanded-patches)
97 (stgit-insert-patch-files patch))
98 (put-text-property start (point) 'patch-data patch)))
100 (defun create-stgit-buffer (dir)
101 "Create a buffer for showing StGit patches.
102 Argument DIR is the repository path."
103 (let ((buf (create-file-buffer (concat dir "*stgit*")))
104 (inhibit-read-only t))
105 (with-current-buffer buf
106 (setq default-directory dir)
108 (set (make-local-variable 'stgit-ewoc)
109 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
110 (setq buffer-read-only t))
113 (defmacro stgit-capture-output (name &rest body)
114 "Capture StGit output and, if there was any output, show it in a window
116 Returns nil if there was no output."
117 (declare (debug ([&or stringp null] body))
119 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
120 (stgit-dir default-directory)
121 (inhibit-read-only t))
122 (with-current-buffer output-buf
124 (setq default-directory stgit-dir)
125 (setq buffer-read-only t))
126 (let ((standard-output output-buf))
128 (with-current-buffer output-buf
129 (set-buffer-modified-p nil)
130 (setq buffer-read-only t)
131 (if (< (point-min) (point-max))
132 (display-buffer output-buf t)))))
134 (defun stgit-make-run-args (args)
135 "Return a copy of ARGS with its elements converted to strings."
137 ;; don't use (format "%s" ...) to limit type errors
138 (cond ((stringp x) x)
139 ((integerp x) (number-to-string x))
140 ((symbolp x) (symbol-name x))
142 (error "Bad element in stgit-make-run-args args: %S" x))))
145 (defun stgit-run-silent (&rest args)
146 (setq args (stgit-make-run-args args))
147 (apply 'call-process "stg" nil standard-output nil args))
149 (defun stgit-run (&rest args)
150 (setq args (stgit-make-run-args args))
151 (let ((msgcmd (mapconcat #'identity args " ")))
152 (message "Running stg %s..." msgcmd)
153 (apply 'call-process "stg" nil standard-output nil args)
154 (message "Running stg %s...done" msgcmd)))
156 (defun stgit-run-git (&rest args)
157 (setq args (stgit-make-run-args args))
158 (let ((msgcmd (mapconcat #'identity args " ")))
159 (message "Running git %s..." msgcmd)
160 (apply 'call-process "git" nil standard-output nil args)
161 (message "Running git %s...done" msgcmd)))
163 (defun stgit-run-git-silent (&rest args)
164 (setq args (stgit-make-run-args args))
165 (apply 'call-process "git" nil standard-output nil args))
167 (defun stgit-index-empty-p ()
168 "Returns non-nil if the index contains no changes from HEAD."
169 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
171 (defvar stgit-index-node)
172 (defvar stgit-worktree-node)
174 (defun stgit-refresh-index ()
175 (when stgit-index-node
176 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
178 (defun stgit-refresh-worktree ()
179 (when stgit-worktree-node
180 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
182 (defun stgit-run-series-insert-index (ewoc)
183 (setq index-node (cons ewoc (ewoc-enter-last ewoc
189 worktree-node (cons ewoc (ewoc-enter-last ewoc
196 (defun stgit-run-series (ewoc)
197 (setq stgit-index-node nil
198 stgit-worktree-node nil)
199 (let ((inserted-index (not stgit-show-worktree))
204 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
205 (goto-char (point-min))
206 (if (not (zerop exit-status))
207 (cond ((looking-at "stg series: \\(.*\\)")
208 (setq inserted-index t)
209 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
210 (substitute-command-keys
211 "-- not initialized; run \\[stgit-init]")))
213 (error "Error running stg: %s"
217 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
218 (error "Syntax error in output from stg series"))
219 (let* ((state-str (match-string 2))
220 (state (cond ((string= state-str ">") 'top)
221 ((string= state-str "+") 'applied)
222 ((string= state-str "-") 'unapplied)))
223 (name (intern (match-string 4)))
224 (desc (match-string 5))
225 (empty (string= (match-string 1) "0")))
226 (unless inserted-index
227 (when (or (eq stgit-show-worktree-mode 'top)
228 (and (eq stgit-show-worktree-mode 'center)
229 (eq state 'unapplied)))
230 (setq inserted-index t)
231 (stgit-run-series-insert-index ewoc)))
232 (setq all-patchsyms (cons name all-patchsyms))
233 (ewoc-enter-last ewoc
240 (unless inserted-index
241 (stgit-run-series-insert-index ewoc)))
242 (setq stgit-index-node index-node
243 stgit-worktree-node worktree-node
244 stgit-marked-patches (intersection stgit-marked-patches
247 (defun stgit-reload ()
248 "Update the contents of the StGit buffer."
250 (let ((inhibit-read-only t)
251 (curline (line-number-at-pos))
252 (curpatch (stgit-patch-name-at-point)))
253 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
254 (ewoc-set-hf stgit-ewoc
258 (stgit-run-silent "branch")
259 (buffer-substring (point-min) (1- (point-max))))
260 'face 'stgit-branch-name-face)
262 (if stgit-show-worktree
265 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
266 shows the working tree\n")
267 'face 'stgit-description-face)))
268 (stgit-run-series stgit-ewoc)
270 (stgit-goto-patch curpatch)
271 (goto-line curline)))
272 (stgit-refresh-git-status))
275 "A user interface for the StGit patch maintenance tool."
278 (defface stgit-description-face
279 '((((background dark)) (:foreground "tan"))
280 (((background light)) (:foreground "dark red")))
281 "The face used for StGit descriptions"
284 (defface stgit-branch-name-face
286 "The face used for the StGit branch name"
289 (defface stgit-top-patch-face
290 '((((background dark)) (:weight bold :foreground "yellow"))
291 (((background light)) (:weight bold :foreground "purple"))
293 "The face used for the top patch names"
296 (defface stgit-applied-patch-face
297 '((((background dark)) (:foreground "light yellow"))
298 (((background light)) (:foreground "purple"))
300 "The face used for applied patch names"
303 (defface stgit-unapplied-patch-face
304 '((((background dark)) (:foreground "gray80"))
305 (((background light)) (:foreground "orchid"))
307 "The face used for unapplied patch names"
310 (defface stgit-modified-file-face
311 '((((class color) (background light)) (:foreground "purple"))
312 (((class color) (background dark)) (:foreground "salmon")))
313 "StGit mode face used for modified file status"
316 (defface stgit-unmerged-file-face
317 '((((class color) (background light)) (:foreground "red" :bold t))
318 (((class color) (background dark)) (:foreground "red" :bold t)))
319 "StGit mode face used for unmerged file status"
322 (defface stgit-unknown-file-face
323 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
324 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
325 "StGit mode face used for unknown file status"
328 (defface stgit-file-permission-face
329 '((((class color) (background light)) (:foreground "green" :bold t))
330 (((class color) (background dark)) (:foreground "green" :bold t)))
331 "StGit mode face used for permission changes."
334 (defface stgit-index-work-tree-title-face
335 '((((supports :slant italic)) :slant italic)
337 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
341 (defcustom stgit-expand-find-copies-harder
343 "Try harder to find copied files when listing patches.
345 When not nil, runs git diff-tree with the --find-copies-harder
346 flag, which reduces performance."
350 (defconst stgit-file-status-code-strings
351 (mapcar (lambda (arg)
353 (propertize (cadr arg) 'face (car (cddr arg)))))
354 '((add "Added" stgit-modified-file-face)
355 (copy "Copied" stgit-modified-file-face)
356 (delete "Deleted" stgit-modified-file-face)
357 (modify "Modified" stgit-modified-file-face)
358 (rename "Renamed" stgit-modified-file-face)
359 (mode-change "Mode change" stgit-modified-file-face)
360 (unmerged "Unmerged" stgit-unmerged-file-face)
361 (unknown "Unknown" stgit-unknown-file-face)))
362 "Alist of code symbols to description strings")
364 (defun stgit-file-status-code-as-string (file)
365 "Return stgit status code for FILE as a string"
366 (let* ((code (assq (stgit-file-status file)
367 stgit-file-status-code-strings))
368 (score (stgit-file-cr-score file)))
371 (if (and score (/= score 100))
372 (format "%s %s" (cdr code)
373 (propertize (format "%d%%" score)
374 'face 'stgit-description-face))
377 (defun stgit-file-status-code (str &optional score)
378 "Return stgit status code from git status string"
379 (let ((code (assoc str '(("A" . add)
387 (setq code (if code (cdr code) 'unknown))
388 (when (stringp score)
389 (if (> (length score) 0)
390 (setq score (string-to-number score))
392 (if score (cons code score) code)))
394 (defconst stgit-file-type-strings
397 (#o160 . "subproject"))
398 "Alist of names of file types")
400 (defun stgit-file-type-string (type)
401 "Return string describing file type TYPE (the high bits of file permission).
402 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
403 (let ((type-str (assoc type stgit-file-type-strings)))
404 (or (and type-str (cdr type-str))
405 (format "unknown type %o" type))))
407 (defun stgit-file-type-change-string (old-perm new-perm)
408 "Return string describing file type change from OLD-PERM to NEW-PERM.
409 Cf. `stgit-file-type-string'."
410 (let ((old-type (lsh old-perm -9))
411 (new-type (lsh new-perm -9)))
412 (cond ((= old-type new-type) "")
413 ((zerop new-type) "")
415 (if (= new-type #o100)
417 (format " (%s)" (stgit-file-type-string new-type))))
418 (t (format " (%s -> %s)"
419 (stgit-file-type-string old-type)
420 (stgit-file-type-string new-type))))))
422 (defun stgit-file-mode-change-string (old-perm new-perm)
423 "Return string describing file mode change from OLD-PERM to NEW-PERM.
424 Cf. `stgit-file-type-change-string'."
425 (setq old-perm (logand old-perm #o777)
426 new-perm (logand new-perm #o777))
427 (if (or (= old-perm new-perm)
431 (let* ((modified (logxor old-perm new-perm))
432 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
433 (cond ((zerop modified) "")
434 ((and (zerop not-x-modified)
435 (or (and (eq #o111 (logand old-perm #o111))
436 (propertize "-x" 'face 'stgit-file-permission-face))
437 (and (eq #o111 (logand new-perm #o111))
438 (propertize "+x" 'face
439 'stgit-file-permission-face)))))
440 (t (concat (propertize (format "%o" old-perm)
441 'face 'stgit-file-permission-face)
443 'face 'stgit-description-face)
444 (propertize (format "%o" new-perm)
445 'face 'stgit-file-permission-face)))))))
447 (defstruct (stgit-file)
448 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
450 (defun stgit-file-pp (file)
451 (let ((status (stgit-file-status file))
452 (name (if (stgit-file-copy-or-rename file)
453 (concat (stgit-file-cr-from file)
455 'face 'stgit-description-face)
456 (stgit-file-cr-to file))
457 (stgit-file-file file)))
458 (mode-change (stgit-file-mode-change-string
459 (stgit-file-old-perm file)
460 (stgit-file-new-perm file)))
462 (insert (format " %-12s%1s%s%s\n"
463 (stgit-file-status-code-as-string file)
466 (propertize (stgit-file-type-change-string
467 (stgit-file-old-perm file)
468 (stgit-file-new-perm file))
469 'face 'stgit-description-face)))
470 (add-text-properties start (point)
471 (list 'entry-type 'file
474 (defun stgit-find-copies-harder-diff-arg ()
475 "Return the flag to use with `git-diff' depending on the
476 `stgit-expand-find-copies-harder' flag."
477 (if stgit-expand-find-copies-harder
478 "--find-copies-harder"
481 (defun stgit-insert-patch-files (patch)
482 "Expand (show modification of) the patch PATCH after the line
484 (let* ((patchsym (stgit-patch-name patch))
485 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
486 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
487 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
488 (setf (stgit-patch-files-ewoc patch) ewoc)
490 (apply 'stgit-run-git
491 (cond ((eq patchsym :work)
492 `("diff-files" ,@args))
493 ((eq patchsym :index)
494 `("diff-index" ,@args "--cached" "HEAD"))
496 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
497 (goto-char (point-min))
498 (unless (or (eobp) (memq patchsym '(:work :index)))
500 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
501 (let ((old-perm (string-to-number (match-string 1) 8))
502 (new-perm (string-to-number (match-string 2) 8)))
503 (goto-char (match-end 0))
506 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
507 (let* ((patch-status (stgit-patch-status patch))
508 (file-subexp (if (eq patch-status 'unapplied)
511 (file (match-string file-subexp)))
516 :cr-score (string-to-number (match-string 2))
517 :cr-from (match-string 3)
518 :cr-to (match-string 4)
519 :status (stgit-file-status-code
522 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
530 :status (stgit-file-status-code
532 :file (match-string 2))))))
533 (ewoc-enter-last ewoc file))
534 (goto-char (match-end 0))))
535 (unless (ewoc-nth ewoc 0)
536 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
537 'face 'stgit-description-face))))
541 (defun stgit-select-file ()
542 (let ((filename (expand-file-name
543 (stgit-file-file (stgit-patched-file-at-point)))))
544 (unless (file-exists-p filename)
545 (error "File does not exist"))
546 (find-file filename)))
548 (defun stgit-select-patch ()
549 (let ((patchname (stgit-patch-name-at-point)))
550 (if (memq patchname stgit-expanded-patches)
551 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
552 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
553 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
554 (move-to-column (stgit-goal-column)))
556 (defun stgit-select ()
557 "Expand or collapse the current entry"
559 (case (get-text-property (point) 'entry-type)
561 (stgit-select-patch))
565 (error "No patch or file on line"))))
567 (defun stgit-find-file-other-window ()
568 "Open file at point in other window"
570 (let ((patched-file (stgit-patched-file-at-point)))
572 (error "No file on the current line"))
573 (let ((filename (expand-file-name (stgit-file-file patched-file))))
574 (unless (file-exists-p filename)
575 (error "File does not exist"))
576 (find-file-other-window filename))))
579 "Hide the stgit buffer."
583 (defun stgit-git-status ()
584 "Show status using `git-status'."
586 (unless (fboundp 'git-status)
587 (error "The stgit-git-status command requires git-status"))
588 (let ((dir default-directory))
589 (save-selected-window
593 (defun stgit-goal-column ()
594 "Return goal column for the current line"
595 (case (get-text-property (point) 'entry-type)
600 (defun stgit-next-line (&optional arg)
601 "Move cursor vertically down ARG lines"
604 (move-to-column (stgit-goal-column)))
606 (defun stgit-previous-line (&optional arg)
607 "Move cursor vertically up ARG lines"
610 (move-to-column (stgit-goal-column)))
612 (defun stgit-next-patch (&optional arg)
613 "Move cursor down ARG patches."
615 (ewoc-goto-next stgit-ewoc (or arg 1))
616 (move-to-column goal-column))
618 (defun stgit-previous-patch (&optional arg)
619 "Move cursor up ARG patches."
621 (ewoc-goto-prev stgit-ewoc (or arg 1))
622 (move-to-column goal-column))
624 (defvar stgit-mode-hook nil
625 "Run after `stgit-mode' is setup.")
627 (defvar stgit-mode-map nil
628 "Keymap for StGit major mode.")
630 (unless stgit-mode-map
631 (let ((toggle-map (make-keymap)))
632 (suppress-keymap toggle-map)
633 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
634 '(("t" . stgit-toggle-worktree)))
635 (setq stgit-mode-map (make-keymap))
636 (suppress-keymap stgit-mode-map)
637 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
640 ("\d" . stgit-unmark-up)
641 ("u" . stgit-unmark-down)
644 ("\C-p" . stgit-previous-line)
645 ("\C-n" . stgit-next-line)
646 ([up] . stgit-previous-line)
647 ([down] . stgit-next-line)
648 ("p" . stgit-previous-patch)
649 ("n" . stgit-next-patch)
650 ("\M-{" . stgit-previous-patch)
651 ("\M-}" . stgit-next-patch)
652 ("s" . stgit-git-status)
653 ("g" . stgit-reload-or-repair)
654 ("r" . stgit-refresh)
655 ("\C-c\C-r" . stgit-rename)
657 ("M" . stgit-move-patches)
660 ("\C-c\C-c" . stgit-commit)
661 ("\C-c\C-u" . stgit-uncommit)
662 ("U" . stgit-revert-file)
663 ("R" . stgit-resolve-file)
664 ("\r" . stgit-select)
665 ("o" . stgit-find-file-other-window)
666 ("i" . stgit-file-toggle-index)
667 (">" . stgit-push-next)
668 ("<" . stgit-pop-next)
669 ("P" . stgit-push-or-pop)
673 ([(control ?/)] . stgit-undo)
674 ("\C-_" . stgit-undo)
677 ("q" . stgit-quit)))))
680 "Major mode for interacting with StGit.
683 (kill-all-local-variables)
684 (buffer-disable-undo)
685 (setq mode-name "StGit"
686 major-mode 'stgit-mode
688 (use-local-map stgit-mode-map)
689 (set (make-local-variable 'list-buffers-directory) default-directory)
690 (set (make-local-variable 'stgit-marked-patches) nil)
691 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
692 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
693 (set (make-local-variable 'stgit-index-node) nil)
694 (set (make-local-variable 'stgit-worktree-node) nil)
695 (set-variable 'truncate-lines 't)
696 (add-hook 'after-save-hook 'stgit-update-saved-file)
697 (run-hooks 'stgit-mode-hook))
699 (defun stgit-update-saved-file ()
700 (let* ((file (expand-file-name buffer-file-name))
701 (dir (file-name-directory file))
702 (gitdir (condition-case nil (git-get-top-dir dir)
704 (buffer (and gitdir (stgit-find-buffer gitdir))))
706 (with-current-buffer buffer
707 (stgit-refresh-worktree)))))
709 (defun stgit-add-mark (patchsym)
710 "Mark the patch PATCHSYM."
711 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
713 (defun stgit-remove-mark (patchsym)
714 "Unmark the patch PATCHSYM."
715 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
717 (defun stgit-clear-marks ()
718 "Unmark all patches."
719 (setq stgit-marked-patches '()))
721 (defun stgit-patch-at-point (&optional cause-error)
722 (get-text-property (point) 'patch-data))
724 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
725 "Return the patch name on the current line as a symbol.
726 If CAUSE-ERROR is not nil, signal an error if none found.
727 If ONLY-PATCHES is not nil, only allow real patches, and not
729 (let ((patch (stgit-patch-at-point)))
732 (memq (stgit-patch-status patch) '(work index))
735 (stgit-patch-name patch))
737 (error "No patch on this line")))))
739 (defun stgit-patched-file-at-point ()
740 (get-text-property (point) 'file-data))
742 (defun stgit-patches-marked-or-at-point ()
743 "Return the symbols of the marked patches, or the patch on the current line."
744 (if stgit-marked-patches
746 (let ((patch (stgit-patch-name-at-point)))
751 (defun stgit-goto-patch (patchsym)
752 "Move point to the line containing patch PATCHSYM.
753 If that patch cannot be found, do nothing."
754 (let ((node (ewoc-nth stgit-ewoc 0)))
755 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
757 (setq node (ewoc-next stgit-ewoc node)))
759 (ewoc-goto-node stgit-ewoc node)
760 (move-to-column goal-column))))
765 (stgit-capture-output nil
770 "Mark the patch under point."
772 (let* ((node (ewoc-locate stgit-ewoc))
773 (patch (ewoc-data node))
774 (name (stgit-patch-name patch)))
775 (when (eq name :work)
776 (error "Cannot mark the work tree"))
777 (when (eq name :index)
778 (error "Cannot mark the index"))
779 (stgit-add-mark (stgit-patch-name patch))
780 (ewoc-invalidate stgit-ewoc node))
783 (defun stgit-unmark-up ()
784 "Remove mark from the patch on the previous line."
786 (stgit-previous-patch)
787 (let* ((node (ewoc-locate stgit-ewoc))
788 (patch (ewoc-data node)))
789 (stgit-remove-mark (stgit-patch-name patch))
790 (ewoc-invalidate stgit-ewoc node))
791 (move-to-column (stgit-goal-column)))
793 (defun stgit-unmark-down ()
794 "Remove mark from the patch on the current line."
796 (let* ((node (ewoc-locate stgit-ewoc))
797 (patch (ewoc-data node)))
798 (stgit-remove-mark (stgit-patch-name patch))
799 (ewoc-invalidate stgit-ewoc node))
802 (defun stgit-rename (name)
803 "Rename the patch under point to NAME."
805 (read-string "Patch name: "
806 (symbol-name (stgit-patch-name-at-point t t)))))
807 (let ((old-patchsym (stgit-patch-name-at-point t t)))
808 (stgit-capture-output nil
809 (stgit-run "rename" old-patchsym name))
810 (let ((name-sym (intern name)))
811 (when (memq old-patchsym stgit-expanded-patches)
812 (setq stgit-expanded-patches
813 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
814 (when (memq old-patchsym stgit-marked-patches)
815 (setq stgit-marked-patches
816 (cons name-sym (delq old-patchsym stgit-marked-patches))))
818 (stgit-goto-patch name-sym))))
820 (defun stgit-reload-or-repair (repair)
821 "Update the contents of the StGit buffer (`stgit-reload').
823 With a prefix argument, repair the StGit metadata if the branch
824 was modified with git commands (`stgit-repair')."
830 (defun stgit-repair ()
833 (stgit-capture-output nil
834 (stgit-run "repair"))
837 (defun stgit-available-branches ()
838 "Returns a list of the available stg branches"
839 (let ((output (with-output-to-string
840 (stgit-run "branch" "--list")))
843 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
844 (setq result (cons (match-string 1 output) result))
845 (setq start (match-end 0)))
848 (defun stgit-branch (branch)
849 "Switch to branch BRANCH."
850 (interactive (list (completing-read "Switch to branch: "
851 (stgit-available-branches))))
852 (stgit-capture-output nil (stgit-run "branch" "--" branch))
855 (defun stgit-commit (count)
856 "Run stg commit on COUNT commits.
857 Interactively, the prefix argument is used as COUNT."
859 (stgit-capture-output nil (stgit-run "commit" "-n" count))
862 (defun stgit-revert-file ()
863 "Revert the file at point, which must be in the index or the
866 (let* ((patched-file (or (stgit-patched-file-at-point)
867 (error "No file on the current line")))
868 (patch-name (stgit-patch-name-at-point))
869 (file-status (stgit-file-status patched-file))
870 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
871 (stgit-file-cr-to patched-file))
872 ((eq file-status 'add)
873 (stgit-file-file patched-file))))
874 (co-file (cond ((eq file-status 'rename)
875 (stgit-file-cr-from patched-file))
876 ((not (memq file-status '(copy add)))
877 (stgit-file-file patched-file)))))
879 (unless (memq patch-name '(:work :index))
880 (error "No index or working tree file on this line"))
882 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
883 (when (yes-or-no-p (format "Revert %d file%s? "
885 (if (= nfiles 1) "" "s")))
886 (stgit-capture-output nil
888 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
890 (stgit-run-git "checkout" "HEAD" co-file)))
893 (defun stgit-resolve-file ()
894 "Resolve conflict in the file at point."
896 (let* ((patched-file (stgit-patched-file-at-point))
897 (patch (stgit-patch-at-point))
898 (patch-name (and patch (stgit-patch-name patch)))
899 (status (and patched-file (stgit-file-status patched-file))))
901 (unless (memq patch-name '(:work :index))
902 (error "No index or working tree file on this line"))
904 (unless (eq status 'unmerged)
905 (error "No conflict to resolve at the current line"))
907 (stgit-capture-output nil
908 (stgit-move-change-to-index (stgit-file-file patched-file)))
912 (defun stgit-uncommit (count)
913 "Run stg uncommit on COUNT commits.
914 Interactively, the prefix argument is used as COUNT."
916 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
919 (defun stgit-push-next (npatches)
920 "Push the first unapplied patch.
921 With numeric prefix argument, push that many patches."
923 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
925 (stgit-refresh-git-status))
927 (defun stgit-pop-next (npatches)
928 "Pop the topmost applied patch.
929 With numeric prefix argument, pop that many patches."
931 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
933 (stgit-refresh-git-status))
935 (defun stgit-applied-at-point ()
936 "Is the patch on the current line applied?"
939 (looking-at "[>+]")))
941 (defun stgit-push-or-pop ()
942 "Push or pop the patch on the current line."
944 (let ((patchsym (stgit-patch-name-at-point t))
945 (applied (stgit-applied-at-point)))
946 (stgit-capture-output nil
947 (stgit-run (if applied "pop" "push") patchsym))
951 "Go to the patch on the current line."
953 (let ((patchsym (stgit-patch-name-at-point t)))
954 (stgit-capture-output nil
955 (stgit-run "goto" patchsym))
958 (defun stgit-id (patchsym)
959 "Return the git commit id for PATCHSYM.
960 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
961 (if (keywordp patchsym)
963 (let ((result (with-output-to-string
964 (stgit-run-silent "id" patchsym))))
965 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
966 (error "Cannot find commit id for %s" patchsym))
967 (match-string 1 result))))
970 "Show the patch on the current line."
972 (stgit-capture-output "*StGit patch*"
973 (case (get-text-property (point) 'entry-type)
975 (let* ((patched-file (stgit-patched-file-at-point))
976 (patch-name (stgit-patch-name-at-point))
977 (patch-id (stgit-id patch-name))
978 (args (append (and (stgit-file-cr-from patched-file)
979 (list (stgit-find-copies-harder-diff-arg)))
980 (cond ((eq patch-id :index)
985 (list (concat patch-id "^") patch-id)))
987 (if (stgit-file-copy-or-rename patched-file)
988 (list (stgit-file-cr-from patched-file)
989 (stgit-file-cr-to patched-file))
990 (list (stgit-file-file patched-file))))))
991 (apply 'stgit-run-git "diff" args)))
993 (let* ((patch-name (stgit-patch-name-at-point))
994 (patch-id (stgit-id patch-name)))
995 (if (or (eq patch-id :index) (eq patch-id :work))
996 (apply 'stgit-run-git "diff"
997 (stgit-find-copies-harder-diff-arg)
998 (and (eq patch-id :index)
1000 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
1001 (stgit-patch-name-at-point)))))
1003 (error "No patch or file at point")))
1004 (with-current-buffer standard-output
1005 (goto-char (point-min))
1008 (defun stgit-move-change-to-index (file)
1009 "Copies the workspace state of FILE to index, using git add or git rm"
1010 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1011 '("add") '("rm" "-q"))))
1012 (stgit-capture-output "*git output*"
1013 (apply 'stgit-run-git (append op '("--") (list file))))))
1015 (defun stgit-remove-change-from-index (file)
1016 "Unstages the change in FILE from the index"
1017 (stgit-capture-output "*git output*"
1018 (stgit-run-git "reset" "-q" "--" file)))
1020 (defun stgit-file-toggle-index ()
1021 "Move modified file in or out of the index."
1023 (let ((patched-file (stgit-patched-file-at-point)))
1024 (unless patched-file
1025 (error "No file on the current line"))
1026 (when (eq (stgit-file-status patched-file) 'unmerged)
1027 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1028 (let ((patch-name (stgit-patch-name-at-point)))
1029 (cond ((eq patch-name :work)
1030 (stgit-move-change-to-index (stgit-file-file patched-file)))
1031 ((eq patch-name :index)
1032 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1034 (error "Can only move files in the working tree to index")))))
1035 (stgit-refresh-worktree)
1036 (stgit-refresh-index))
1038 (defun stgit-edit ()
1039 "Edit the patch on the current line."
1041 (let ((patchsym (stgit-patch-name-at-point t t))
1042 (edit-buf (get-buffer-create "*StGit edit*"))
1043 (dir default-directory))
1044 (log-edit 'stgit-confirm-edit t nil edit-buf)
1045 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1046 (setq default-directory dir)
1047 (let ((standard-output edit-buf))
1048 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1050 (defun stgit-confirm-edit ()
1052 (let ((file (make-temp-file "stgit-edit-")))
1053 (write-region (point-min) (point-max) file)
1054 (stgit-capture-output nil
1055 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1056 (with-current-buffer log-edit-parent-buffer
1059 (defun stgit-new (add-sign)
1060 "Create a new patch.
1061 With a prefix argument, include a \"Signed-off-by:\" line at the
1064 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1065 (dir default-directory))
1066 (log-edit 'stgit-confirm-new t nil edit-buf)
1067 (setq default-directory dir)
1070 (let ((standard-output (current-buffer)))
1071 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1073 (defun stgit-confirm-new ()
1075 (let ((file (make-temp-file "stgit-edit-")))
1076 (write-region (point-min) (point-max) file)
1077 (stgit-capture-output nil
1078 (stgit-run "new" "-f" file))
1079 (with-current-buffer log-edit-parent-buffer
1082 (defun stgit-create-patch-name (description)
1083 "Create a patch name from a long description"
1085 (while (> (length description) 0)
1086 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1087 (setq patch (downcase (concat patch
1088 (match-string 0 description))))
1089 (setq description (substring description (match-end 0))))
1090 ((string-match "\\` +" description)
1091 (setq patch (concat patch "-"))
1092 (setq description (substring description (match-end 0))))
1093 ((string-match "\\`[^a-zA-Z_-]+" description)
1094 (setq description (substring description (match-end 0))))))
1095 (cond ((= (length patch) 0)
1097 ((> (length patch) 20)
1098 (substring patch 0 20))
1101 (defun stgit-delete (patchsyms &optional spill-p)
1102 "Delete the patches in PATCHSYMS.
1103 Interactively, delete the marked patches, or the patch at point.
1105 With a prefix argument, or SPILL-P, spill the patch contents to
1106 the work tree and index."
1107 (interactive (list (stgit-patches-marked-or-at-point)
1108 current-prefix-arg))
1110 (error "No patches to delete"))
1111 (when (memq :index patchsyms)
1112 (error "Cannot delete the index"))
1113 (when (memq :work patchsyms)
1114 (error "Cannot delete the work tree"))
1116 (let ((npatches (length patchsyms)))
1117 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1119 (if (= 1 npatches) "" "es")
1121 " (spilling contents to index)"
1123 (let ((args (if spill-p
1124 (cons "--spill" patchsyms)
1126 (stgit-capture-output nil
1127 (apply 'stgit-run "delete" args))
1130 (defun stgit-move-patches-target ()
1131 "Return the patchsym indicating a target patch for
1132 `stgit-move-patches'.
1134 This is either the patch at point, or one of :top and :bottom, if
1135 the point is after or before the applied patches."
1137 (let ((patchsym (stgit-patch-name-at-point)))
1138 (cond (patchsym patchsym)
1139 ((save-excursion (re-search-backward "^>" nil t)) :top)
1142 (defun stgit-sort-patches (patchsyms)
1143 "Returns the list of patches in PATCHSYMS sorted according to
1144 their position in the patch series, bottommost first.
1146 PATCHSYMS may not contain duplicate entries."
1147 (let (sorted-patchsyms
1148 (series (with-output-to-string
1149 (with-current-buffer standard-output
1150 (stgit-run-silent "series" "--noprefix"))))
1152 (while (string-match "^\\(.+\\)" series start)
1153 (let ((patchsym (intern (match-string 1 series))))
1154 (when (memq patchsym patchsyms)
1155 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1156 (setq start (match-end 0)))
1157 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1159 (unless (= (length patchsyms) (length sorted-patchsyms))
1160 (error "Internal error"))
1164 (defun stgit-move-patches (patchsyms target-patch)
1165 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1166 If TARGET-PATCH is :bottom or :top, move the patches to the
1167 bottom or top of the stack, respectively.
1169 Interactively, move the marked patches to where the point is."
1170 (interactive (list stgit-marked-patches
1171 (stgit-move-patches-target)))
1173 (error "Need at least one patch to move"))
1175 (unless target-patch
1176 (error "Point not at a patch"))
1178 (if (eq target-patch :top)
1179 (stgit-capture-output nil
1180 (apply 'stgit-run "float" patchsyms))
1182 ;; need to have patchsyms sorted by position in the stack
1183 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1184 (while sorted-patchsyms
1185 (setq sorted-patchsyms
1186 (and (stgit-capture-output nil
1187 (if (eq target-patch :bottom)
1188 (stgit-run "sink" "--" (car sorted-patchsyms))
1189 (stgit-run "sink" "--to" target-patch "--"
1190 (car sorted-patchsyms))))
1191 (cdr sorted-patchsyms))))))
1194 (defun stgit-squash (patchsyms)
1195 "Squash the patches in PATCHSYMS.
1196 Interactively, squash the marked patches.
1198 Unless there are any conflicts, the patches will be merged into
1199 one patch, which will occupy the same spot in the series as the
1200 deepest patch had before the squash."
1201 (interactive (list stgit-marked-patches))
1202 (when (< (length patchsyms) 2)
1203 (error "Need at least two patches to squash"))
1204 (let ((stgit-buffer (current-buffer))
1205 (edit-buf (get-buffer-create "*StGit edit*"))
1206 (dir default-directory)
1207 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1208 (log-edit 'stgit-confirm-squash t nil edit-buf)
1209 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1210 (setq default-directory dir)
1211 (let ((result (let ((standard-output edit-buf))
1212 (apply 'stgit-run-silent "squash"
1213 "--save-template=-" sorted-patchsyms))))
1215 ;; stg squash may have reordered the patches or caused conflicts
1216 (with-current-buffer stgit-buffer
1219 (unless (eq 0 result)
1221 (rename-buffer "*StGit error*")
1222 (resize-temp-buffer-window)
1223 (switch-to-buffer-other-window stgit-buffer)
1224 (error "stg squash failed")))))
1226 (defun stgit-confirm-squash ()
1228 (let ((file (make-temp-file "stgit-edit-")))
1229 (write-region (point-min) (point-max) file)
1230 (stgit-capture-output nil
1231 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1232 (with-current-buffer log-edit-parent-buffer
1234 ;; Go to first marked patch and stay there
1235 (goto-char (point-min))
1236 (re-search-forward (concat "^[>+-]\\*") nil t)
1237 (move-to-column goal-column)
1238 (let ((pos (point)))
1242 (defun stgit-help ()
1243 "Display help for the StGit mode."
1245 (describe-function 'stgit-mode))
1247 (defun stgit-undo (&optional arg)
1249 With prefix argument, run it with the --hard flag."
1251 (stgit-capture-output nil
1253 (stgit-run "undo" "--hard")
1254 (stgit-run "undo")))
1257 (defun stgit-refresh (&optional arg)
1259 If the index contains any changes, only refresh from index.
1261 With prefix argument, refresh the marked patch or the patch under point."
1263 (let ((patchargs (if arg
1264 (let ((patches (stgit-patches-marked-or-at-point)))
1265 (cond ((null patches)
1266 (error "No patch to update"))
1267 ((> (length patches) 1)
1268 (error "Too many patches selected"))
1270 (cons "-p" patches))))
1272 (unless (stgit-index-empty-p)
1273 (setq patchargs (cons "--index" patchargs)))
1274 (stgit-capture-output nil
1275 (apply 'stgit-run "refresh" patchargs))
1276 (stgit-refresh-git-status))
1279 (defcustom stgit-show-worktree-mode 'center
1280 "This variable controls where the \"Index\" and \"Work tree\"
1281 will be shown on in the buffer.
1283 It can be set to 'top (above all patches), 'center (show between
1284 applied and unapplied patches), and 'bottom (below all patches).
1286 See also `stgit-show-worktree'."
1287 :type '(radio (const :tag "above all patches (top)" top)
1288 (const :tag "between applied and unapplied patches (center)"
1290 (const :tag "below all patches (bottom)" bottom))
1293 (defcustom stgit-default-show-worktree
1295 "Set to non-nil to by default show the working tree in a new stgit buffer.
1297 This value is used as the default value for `stgit-show-worktree'."
1301 (defvar stgit-show-worktree nil
1302 "If nil, inhibit showing work tree and index in the stgit buffer.
1304 See also `stgit-show-worktree-mode'.")
1306 (defun stgit-toggle-worktree (&optional arg)
1307 "Toggle the visibility of the work tree.
1308 With arg, show the work tree if arg is positive.
1310 Its initial setting is controlled by `stgit-default-show-worktree'.
1312 `stgit-show-worktree-mode' controls where on screen the index and
1313 work tree will show up."
1315 (setq stgit-show-worktree
1318 (not stgit-show-worktree)))