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))
203 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
204 (goto-char (point-min))
205 (if (not (zerop exit-status))
206 (cond ((looking-at "stg series: \\(.*\\)")
207 (setq inserted-index t)
208 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
209 (substitute-command-keys
210 "-- not initialized; run \\[stgit-init]")))
212 (error "Error running stg: %s"
216 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
217 (error "Syntax error in output from stg series"))
218 (let* ((state-str (match-string 2))
219 (state (cond ((string= state-str ">") 'top)
220 ((string= state-str "+") 'applied)
221 ((string= state-str "-") 'unapplied)))
222 (name (intern (match-string 4)))
223 (desc (match-string 5))
224 (empty (string= (match-string 1) "0")))
225 (unless inserted-index
226 (when (or (eq stgit-show-worktree-mode 'top)
227 (and (eq stgit-show-worktree-mode 'center)
228 (eq state 'unapplied)))
229 (setq inserted-index t)
230 (stgit-run-series-insert-index ewoc)))
231 (ewoc-enter-last ewoc
238 (unless inserted-index
239 (stgit-run-series-insert-index ewoc)))
240 (setq stgit-index-node index-node
241 stgit-worktree-node worktree-node)))
243 (defun stgit-reload ()
244 "Update the contents of the StGit buffer."
246 (let ((inhibit-read-only t)
247 (curline (line-number-at-pos))
248 (curpatch (stgit-patch-name-at-point)))
249 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
250 (ewoc-set-hf stgit-ewoc
254 (stgit-run-silent "branch")
255 (buffer-substring (point-min) (1- (point-max))))
256 'face 'stgit-branch-name-face)
258 (if stgit-show-worktree
261 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
262 shows the working tree\n")
263 'face 'stgit-description-face)))
264 (stgit-run-series stgit-ewoc)
266 (stgit-goto-patch curpatch)
267 (goto-line curline)))
268 (stgit-refresh-git-status))
271 "A user interface for the StGit patch maintenance tool."
274 (defface stgit-description-face
275 '((((background dark)) (:foreground "tan"))
276 (((background light)) (:foreground "dark red")))
277 "The face used for StGit descriptions"
280 (defface stgit-branch-name-face
282 "The face used for the StGit branch name"
285 (defface stgit-top-patch-face
286 '((((background dark)) (:weight bold :foreground "yellow"))
287 (((background light)) (:weight bold :foreground "purple"))
289 "The face used for the top patch names"
292 (defface stgit-applied-patch-face
293 '((((background dark)) (:foreground "light yellow"))
294 (((background light)) (:foreground "purple"))
296 "The face used for applied patch names"
299 (defface stgit-unapplied-patch-face
300 '((((background dark)) (:foreground "gray80"))
301 (((background light)) (:foreground "orchid"))
303 "The face used for unapplied patch names"
306 (defface stgit-modified-file-face
307 '((((class color) (background light)) (:foreground "purple"))
308 (((class color) (background dark)) (:foreground "salmon")))
309 "StGit mode face used for modified file status"
312 (defface stgit-unmerged-file-face
313 '((((class color) (background light)) (:foreground "red" :bold t))
314 (((class color) (background dark)) (:foreground "red" :bold t)))
315 "StGit mode face used for unmerged file status"
318 (defface stgit-unknown-file-face
319 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
320 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
321 "StGit mode face used for unknown file status"
324 (defface stgit-file-permission-face
325 '((((class color) (background light)) (:foreground "green" :bold t))
326 (((class color) (background dark)) (:foreground "green" :bold t)))
327 "StGit mode face used for permission changes."
330 (defface stgit-index-work-tree-title-face
331 '((((supports :slant italic)) :slant italic)
333 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
337 (defcustom stgit-expand-find-copies-harder
339 "Try harder to find copied files when listing patches.
341 When not nil, runs git diff-tree with the --find-copies-harder
342 flag, which reduces performance."
346 (defconst stgit-file-status-code-strings
347 (mapcar (lambda (arg)
349 (propertize (cadr arg) 'face (car (cddr arg)))))
350 '((add "Added" stgit-modified-file-face)
351 (copy "Copied" stgit-modified-file-face)
352 (delete "Deleted" stgit-modified-file-face)
353 (modify "Modified" stgit-modified-file-face)
354 (rename "Renamed" stgit-modified-file-face)
355 (mode-change "Mode change" stgit-modified-file-face)
356 (unmerged "Unmerged" stgit-unmerged-file-face)
357 (unknown "Unknown" stgit-unknown-file-face)))
358 "Alist of code symbols to description strings")
360 (defun stgit-file-status-code-as-string (file)
361 "Return stgit status code for FILE as a string"
362 (let* ((code (assq (stgit-file-status file)
363 stgit-file-status-code-strings))
364 (score (stgit-file-cr-score file)))
367 (if (and score (/= score 100))
368 (format "%s %s" (cdr code)
369 (propertize (format "%d%%" score)
370 'face 'stgit-description-face))
373 (defun stgit-file-status-code (str &optional score)
374 "Return stgit status code from git status string"
375 (let ((code (assoc str '(("A" . add)
383 (setq code (if code (cdr code) 'unknown))
384 (when (stringp score)
385 (if (> (length score) 0)
386 (setq score (string-to-number score))
388 (if score (cons code score) code)))
390 (defconst stgit-file-type-strings
393 (#o160 . "subproject"))
394 "Alist of names of file types")
396 (defun stgit-file-type-string (type)
397 "Return string describing file type TYPE (the high bits of file permission).
398 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
399 (let ((type-str (assoc type stgit-file-type-strings)))
400 (or (and type-str (cdr type-str))
401 (format "unknown type %o" type))))
403 (defun stgit-file-type-change-string (old-perm new-perm)
404 "Return string describing file type change from OLD-PERM to NEW-PERM.
405 Cf. `stgit-file-type-string'."
406 (let ((old-type (lsh old-perm -9))
407 (new-type (lsh new-perm -9)))
408 (cond ((= old-type new-type) "")
409 ((zerop new-type) "")
411 (if (= new-type #o100)
413 (format " (%s)" (stgit-file-type-string new-type))))
414 (t (format " (%s -> %s)"
415 (stgit-file-type-string old-type)
416 (stgit-file-type-string new-type))))))
418 (defun stgit-file-mode-change-string (old-perm new-perm)
419 "Return string describing file mode change from OLD-PERM to NEW-PERM.
420 Cf. `stgit-file-type-change-string'."
421 (setq old-perm (logand old-perm #o777)
422 new-perm (logand new-perm #o777))
423 (if (or (= old-perm new-perm)
427 (let* ((modified (logxor old-perm new-perm))
428 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
429 (cond ((zerop modified) "")
430 ((and (zerop not-x-modified)
431 (or (and (eq #o111 (logand old-perm #o111))
432 (propertize "-x" 'face 'stgit-file-permission-face))
433 (and (eq #o111 (logand new-perm #o111))
434 (propertize "+x" 'face
435 'stgit-file-permission-face)))))
436 (t (concat (propertize (format "%o" old-perm)
437 'face 'stgit-file-permission-face)
439 'face 'stgit-description-face)
440 (propertize (format "%o" new-perm)
441 'face 'stgit-file-permission-face)))))))
443 (defstruct (stgit-file)
444 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
446 (defun stgit-file-pp (file)
447 (let ((status (stgit-file-status file))
448 (name (if (stgit-file-copy-or-rename file)
449 (concat (stgit-file-cr-from file)
451 'face 'stgit-description-face)
452 (stgit-file-cr-to file))
453 (stgit-file-file file)))
454 (mode-change (stgit-file-mode-change-string
455 (stgit-file-old-perm file)
456 (stgit-file-new-perm file)))
458 (insert (format " %-12s%1s%s%s\n"
459 (stgit-file-status-code-as-string file)
462 (propertize (stgit-file-type-change-string
463 (stgit-file-old-perm file)
464 (stgit-file-new-perm file))
465 'face 'stgit-description-face)))
466 (add-text-properties start (point)
467 (list 'entry-type 'file
470 (defun stgit-find-copies-harder-diff-arg ()
471 "Return the flag to use with `git-diff' depending on the
472 `stgit-expand-find-copies-harder' flag."
473 (if stgit-expand-find-copies-harder
474 "--find-copies-harder"
477 (defun stgit-insert-patch-files (patch)
478 "Expand (show modification of) the patch PATCH after the line
480 (let* ((patchsym (stgit-patch-name patch))
481 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
482 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
483 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
484 (setf (stgit-patch-files-ewoc patch) ewoc)
486 (apply 'stgit-run-git
487 (cond ((eq patchsym :work)
488 `("diff-files" ,@args))
489 ((eq patchsym :index)
490 `("diff-index" ,@args "--cached" "HEAD"))
492 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
493 (goto-char (point-min))
494 (unless (or (eobp) (memq patchsym '(:work :index)))
496 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
497 (let ((old-perm (string-to-number (match-string 1) 8))
498 (new-perm (string-to-number (match-string 2) 8)))
499 (goto-char (match-end 0))
502 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
503 (let* ((patch-status (stgit-patch-status patch))
504 (file-subexp (if (eq patch-status 'unapplied)
507 (file (match-string file-subexp)))
512 :cr-score (string-to-number (match-string 2))
513 :cr-from (match-string 3)
514 :cr-to (match-string 4)
515 :status (stgit-file-status-code
518 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
526 :status (stgit-file-status-code
528 :file (match-string 2))))))
529 (ewoc-enter-last ewoc file))
530 (goto-char (match-end 0))))
531 (unless (ewoc-nth ewoc 0)
532 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
533 'face 'stgit-description-face))))
537 (defun stgit-select-file ()
538 (let ((filename (expand-file-name
539 (stgit-file-file (stgit-patched-file-at-point)))))
540 (unless (file-exists-p filename)
541 (error "File does not exist"))
542 (find-file filename)))
544 (defun stgit-select-patch ()
545 (let ((patchname (stgit-patch-name-at-point)))
546 (if (memq patchname stgit-expanded-patches)
547 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
548 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
549 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
550 (move-to-column (stgit-goal-column)))
552 (defun stgit-select ()
553 "Expand or collapse the current entry"
555 (case (get-text-property (point) 'entry-type)
557 (stgit-select-patch))
561 (error "No patch or file on line"))))
563 (defun stgit-find-file-other-window ()
564 "Open file at point in other window"
566 (let ((patched-file (stgit-patched-file-at-point)))
568 (error "No file on the current line"))
569 (let ((filename (expand-file-name (stgit-file-file patched-file))))
570 (unless (file-exists-p filename)
571 (error "File does not exist"))
572 (find-file-other-window filename))))
575 "Hide the stgit buffer."
579 (defun stgit-git-status ()
580 "Show status using `git-status'."
582 (unless (fboundp 'git-status)
583 (error "The stgit-git-status command requires git-status"))
584 (let ((dir default-directory))
585 (save-selected-window
589 (defun stgit-goal-column ()
590 "Return goal column for the current line"
591 (case (get-text-property (point) 'entry-type)
596 (defun stgit-next-line (&optional arg)
597 "Move cursor vertically down ARG lines"
600 (move-to-column (stgit-goal-column)))
602 (defun stgit-previous-line (&optional arg)
603 "Move cursor vertically up ARG lines"
606 (move-to-column (stgit-goal-column)))
608 (defun stgit-next-patch (&optional arg)
609 "Move cursor down ARG patches."
611 (ewoc-goto-next stgit-ewoc (or arg 1))
612 (move-to-column goal-column))
614 (defun stgit-previous-patch (&optional arg)
615 "Move cursor up ARG patches."
617 (ewoc-goto-prev stgit-ewoc (or arg 1))
618 (move-to-column goal-column))
620 (defvar stgit-mode-hook nil
621 "Run after `stgit-mode' is setup.")
623 (defvar stgit-mode-map nil
624 "Keymap for StGit major mode.")
626 (unless stgit-mode-map
627 (let ((toggle-map (make-keymap)))
628 (suppress-keymap toggle-map)
629 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
630 '(("t" . stgit-toggle-worktree)))
631 (setq stgit-mode-map (make-keymap))
632 (suppress-keymap stgit-mode-map)
633 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
636 ("\d" . stgit-unmark-up)
637 ("u" . stgit-unmark-down)
640 ("\C-p" . stgit-previous-line)
641 ("\C-n" . stgit-next-line)
642 ([up] . stgit-previous-line)
643 ([down] . stgit-next-line)
644 ("p" . stgit-previous-patch)
645 ("n" . stgit-next-patch)
646 ("\M-{" . stgit-previous-patch)
647 ("\M-}" . stgit-next-patch)
648 ("s" . stgit-git-status)
649 ("g" . stgit-reload-or-repair)
650 ("r" . stgit-refresh)
651 ("\C-c\C-r" . stgit-rename)
653 ("M" . stgit-move-patches)
656 ("\C-c\C-c" . stgit-commit)
657 ("\C-c\C-u" . stgit-uncommit)
658 ("U" . stgit-revert-file)
659 ("R" . stgit-resolve-file)
660 ("\r" . stgit-select)
661 ("o" . stgit-find-file-other-window)
662 ("i" . stgit-file-toggle-index)
663 (">" . stgit-push-next)
664 ("<" . stgit-pop-next)
665 ("P" . stgit-push-or-pop)
669 ([(control ?/)] . stgit-undo)
670 ("\C-_" . stgit-undo)
673 ("q" . stgit-quit)))))
676 "Major mode for interacting with StGit.
679 (kill-all-local-variables)
680 (buffer-disable-undo)
681 (setq mode-name "StGit"
682 major-mode 'stgit-mode
684 (use-local-map stgit-mode-map)
685 (set (make-local-variable 'list-buffers-directory) default-directory)
686 (set (make-local-variable 'stgit-marked-patches) nil)
687 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
688 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
689 (set (make-local-variable 'stgit-index-node) nil)
690 (set (make-local-variable 'stgit-worktree-node) nil)
691 (set-variable 'truncate-lines 't)
692 (add-hook 'after-save-hook 'stgit-update-saved-file)
693 (run-hooks 'stgit-mode-hook))
695 (defun stgit-update-saved-file ()
696 (let* ((file (expand-file-name buffer-file-name))
697 (dir (file-name-directory file))
698 (gitdir (condition-case nil (git-get-top-dir dir)
700 (buffer (and gitdir (stgit-find-buffer gitdir))))
702 (with-current-buffer buffer
703 (stgit-refresh-worktree)))))
705 (defun stgit-add-mark (patchsym)
706 "Mark the patch PATCHSYM."
707 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
709 (defun stgit-remove-mark (patchsym)
710 "Unmark the patch PATCHSYM."
711 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
713 (defun stgit-clear-marks ()
714 "Unmark all patches."
715 (setq stgit-marked-patches '()))
717 (defun stgit-patch-at-point (&optional cause-error)
718 (get-text-property (point) 'patch-data))
720 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
721 "Return the patch name on the current line as a symbol.
722 If CAUSE-ERROR is not nil, signal an error if none found.
723 If ONLY-PATCHES is not nil, only allow real patches, and not
725 (let ((patch (stgit-patch-at-point)))
728 (memq (stgit-patch-status patch) '(work index))
731 (stgit-patch-name patch))
733 (error "No patch on this line")))))
735 (defun stgit-patched-file-at-point ()
736 (get-text-property (point) 'file-data))
738 (defun stgit-patches-marked-or-at-point ()
739 "Return the symbols of the marked patches, or the patch on the current line."
740 (if stgit-marked-patches
742 (let ((patch (stgit-patch-name-at-point)))
747 (defun stgit-goto-patch (patchsym)
748 "Move point to the line containing patch PATCHSYM.
749 If that patch cannot be found, do nothing."
750 (let ((node (ewoc-nth stgit-ewoc 0)))
751 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
753 (setq node (ewoc-next stgit-ewoc node)))
755 (ewoc-goto-node stgit-ewoc node)
756 (move-to-column goal-column))))
761 (stgit-capture-output nil
766 "Mark the patch under point."
768 (let* ((node (ewoc-locate stgit-ewoc))
769 (patch (ewoc-data node))
770 (name (stgit-patch-name patch)))
771 (when (eq name :work)
772 (error "Cannot mark the work tree"))
773 (when (eq name :index)
774 (error "Cannot mark the index"))
775 (stgit-add-mark (stgit-patch-name patch))
776 (ewoc-invalidate stgit-ewoc node))
779 (defun stgit-unmark-up ()
780 "Remove mark from the patch on the previous line."
782 (stgit-previous-patch)
783 (let* ((node (ewoc-locate stgit-ewoc))
784 (patch (ewoc-data node)))
785 (stgit-remove-mark (stgit-patch-name patch))
786 (ewoc-invalidate stgit-ewoc node))
787 (move-to-column (stgit-goal-column)))
789 (defun stgit-unmark-down ()
790 "Remove mark from the patch on the current line."
792 (let* ((node (ewoc-locate stgit-ewoc))
793 (patch (ewoc-data node)))
794 (stgit-remove-mark (stgit-patch-name patch))
795 (ewoc-invalidate stgit-ewoc node))
798 (defun stgit-rename (name)
799 "Rename the patch under point to NAME."
801 (read-string "Patch name: "
802 (symbol-name (stgit-patch-name-at-point t t)))))
803 (let ((old-patchsym (stgit-patch-name-at-point t t)))
804 (stgit-capture-output nil
805 (stgit-run "rename" old-patchsym name))
806 (let ((name-sym (intern name)))
807 (when (memq old-patchsym stgit-expanded-patches)
808 (setq stgit-expanded-patches
809 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
810 (when (memq old-patchsym stgit-marked-patches)
811 (setq stgit-marked-patches
812 (cons name-sym (delq old-patchsym stgit-marked-patches))))
814 (stgit-goto-patch name-sym))))
816 (defun stgit-reload-or-repair (repair)
817 "Update the contents of the StGit buffer (`stgit-reload').
819 With a prefix argument, repair the StGit metadata if the branch
820 was modified with git commands (`stgit-repair')."
826 (defun stgit-repair ()
829 (stgit-capture-output nil
830 (stgit-run "repair"))
833 (defun stgit-available-branches ()
834 "Returns a list of the available stg branches"
835 (let ((output (with-output-to-string
836 (stgit-run "branch" "--list")))
839 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
840 (setq result (cons (match-string 1 output) result))
841 (setq start (match-end 0)))
844 (defun stgit-branch (branch)
845 "Switch to branch BRANCH."
846 (interactive (list (completing-read "Switch to branch: "
847 (stgit-available-branches))))
848 (stgit-capture-output nil (stgit-run "branch" "--" branch))
851 (defun stgit-commit (count)
852 "Run stg commit on COUNT commits.
853 Interactively, the prefix argument is used as COUNT."
855 (stgit-capture-output nil (stgit-run "commit" "-n" count))
858 (defun stgit-revert-file ()
859 "Revert the file at point, which must be in the index or the
862 (let* ((patched-file (or (stgit-patched-file-at-point)
863 (error "No file on the current line")))
864 (patch-name (stgit-patch-name-at-point))
865 (file-status (stgit-file-status patched-file))
866 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
867 (stgit-file-cr-to patched-file))
868 ((eq file-status 'add)
869 (stgit-file-file patched-file))))
870 (co-file (cond ((eq file-status 'rename)
871 (stgit-file-cr-from patched-file))
872 ((not (memq file-status '(copy add)))
873 (stgit-file-file patched-file)))))
875 (unless (memq patch-name '(:work :index))
876 (error "No index or working tree file on this line"))
878 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
879 (when (yes-or-no-p (format "Revert %d file%s? "
881 (if (= nfiles 1) "" "s")))
882 (stgit-capture-output nil
884 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
886 (stgit-run-git "checkout" "HEAD" co-file)))
889 (defun stgit-resolve-file ()
890 "Resolve conflict in the file at point."
892 (let* ((patched-file (stgit-patched-file-at-point))
893 (patch (stgit-patch-at-point))
894 (patch-name (and patch (stgit-patch-name patch)))
895 (status (and patched-file (stgit-file-status patched-file))))
897 (unless (memq patch-name '(:work :index))
898 (error "No index or working tree file on this line"))
900 (unless (eq status 'unmerged)
901 (error "No conflict to resolve at the current line"))
903 (stgit-capture-output nil
904 (stgit-move-change-to-index (stgit-file-file patched-file)))
908 (defun stgit-uncommit (count)
909 "Run stg uncommit on COUNT commits.
910 Interactively, the prefix argument is used as COUNT."
912 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
915 (defun stgit-push-next (npatches)
916 "Push the first unapplied patch.
917 With numeric prefix argument, push that many patches."
919 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
921 (stgit-refresh-git-status))
923 (defun stgit-pop-next (npatches)
924 "Pop the topmost applied patch.
925 With numeric prefix argument, pop that many patches."
927 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
929 (stgit-refresh-git-status))
931 (defun stgit-applied-at-point ()
932 "Is the patch on the current line applied?"
935 (looking-at "[>+]")))
937 (defun stgit-push-or-pop ()
938 "Push or pop the patch on the current line."
940 (let ((patchsym (stgit-patch-name-at-point t))
941 (applied (stgit-applied-at-point)))
942 (stgit-capture-output nil
943 (stgit-run (if applied "pop" "push") patchsym))
947 "Go to the patch on the current line."
949 (let ((patchsym (stgit-patch-name-at-point t)))
950 (stgit-capture-output nil
951 (stgit-run "goto" patchsym))
954 (defun stgit-id (patchsym)
955 "Return the git commit id for PATCHSYM.
956 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
957 (if (keywordp patchsym)
959 (let ((result (with-output-to-string
960 (stgit-run-silent "id" patchsym))))
961 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
962 (error "Cannot find commit id for %s" patchsym))
963 (match-string 1 result))))
966 "Show the patch on the current line."
968 (stgit-capture-output "*StGit patch*"
969 (case (get-text-property (point) 'entry-type)
971 (let* ((patched-file (stgit-patched-file-at-point))
972 (patch-name (stgit-patch-name-at-point))
973 (patch-id (stgit-id patch-name))
974 (args (append (and (stgit-file-cr-from patched-file)
975 (list (stgit-find-copies-harder-diff-arg)))
976 (cond ((eq patch-id :index)
981 (list (concat patch-id "^") patch-id)))
983 (if (stgit-file-copy-or-rename patched-file)
984 (list (stgit-file-cr-from patched-file)
985 (stgit-file-cr-to patched-file))
986 (list (stgit-file-file patched-file))))))
987 (apply 'stgit-run-git "diff" args)))
989 (let* ((patch-name (stgit-patch-name-at-point))
990 (patch-id (stgit-id patch-name)))
991 (if (or (eq patch-id :index) (eq patch-id :work))
992 (apply 'stgit-run-git "diff"
993 (stgit-find-copies-harder-diff-arg)
994 (and (eq patch-id :index)
996 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
997 (stgit-patch-name-at-point)))))
999 (error "No patch or file at point")))
1000 (with-current-buffer standard-output
1001 (goto-char (point-min))
1004 (defun stgit-move-change-to-index (file)
1005 "Copies the workspace state of FILE to index, using git add or git rm"
1006 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1007 '("add") '("rm" "-q"))))
1008 (stgit-capture-output "*git output*"
1009 (apply 'stgit-run-git (append op '("--") (list file))))))
1011 (defun stgit-remove-change-from-index (file)
1012 "Unstages the change in FILE from the index"
1013 (stgit-capture-output "*git output*"
1014 (stgit-run-git "reset" "-q" "--" file)))
1016 (defun stgit-file-toggle-index ()
1017 "Move modified file in or out of the index."
1019 (let ((patched-file (stgit-patched-file-at-point)))
1020 (unless patched-file
1021 (error "No file on the current line"))
1022 (when (eq (stgit-file-status patched-file) 'unmerged)
1023 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1024 (let ((patch-name (stgit-patch-name-at-point)))
1025 (cond ((eq patch-name :work)
1026 (stgit-move-change-to-index (stgit-file-file patched-file)))
1027 ((eq patch-name :index)
1028 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1030 (error "Can only move files in the working tree to index")))))
1031 (stgit-refresh-worktree)
1032 (stgit-refresh-index))
1034 (defun stgit-edit ()
1035 "Edit the patch on the current line."
1037 (let ((patchsym (stgit-patch-name-at-point t t))
1038 (edit-buf (get-buffer-create "*StGit edit*"))
1039 (dir default-directory))
1040 (log-edit 'stgit-confirm-edit t nil edit-buf)
1041 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1042 (setq default-directory dir)
1043 (let ((standard-output edit-buf))
1044 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1046 (defun stgit-confirm-edit ()
1048 (let ((file (make-temp-file "stgit-edit-")))
1049 (write-region (point-min) (point-max) file)
1050 (stgit-capture-output nil
1051 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1052 (with-current-buffer log-edit-parent-buffer
1055 (defun stgit-new (add-sign)
1056 "Create a new patch.
1057 With a prefix argument, include a \"Signed-off-by:\" line at the
1060 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1061 (dir default-directory))
1062 (log-edit 'stgit-confirm-new t nil edit-buf)
1063 (setq default-directory dir)
1066 (let ((standard-output (current-buffer)))
1067 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1069 (defun stgit-confirm-new ()
1071 (let ((file (make-temp-file "stgit-edit-")))
1072 (write-region (point-min) (point-max) file)
1073 (stgit-capture-output nil
1074 (stgit-run "new" "-f" file))
1075 (with-current-buffer log-edit-parent-buffer
1078 (defun stgit-create-patch-name (description)
1079 "Create a patch name from a long description"
1081 (while (> (length description) 0)
1082 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1083 (setq patch (downcase (concat patch
1084 (match-string 0 description))))
1085 (setq description (substring description (match-end 0))))
1086 ((string-match "\\` +" description)
1087 (setq patch (concat patch "-"))
1088 (setq description (substring description (match-end 0))))
1089 ((string-match "\\`[^a-zA-Z_-]+" description)
1090 (setq description (substring description (match-end 0))))))
1091 (cond ((= (length patch) 0)
1093 ((> (length patch) 20)
1094 (substring patch 0 20))
1097 (defun stgit-delete (patchsyms &optional spill-p)
1098 "Delete the patches in PATCHSYMS.
1099 Interactively, delete the marked patches, or the patch at point.
1101 With a prefix argument, or SPILL-P, spill the patch contents to
1102 the work tree and index."
1103 (interactive (list (stgit-patches-marked-or-at-point)
1104 current-prefix-arg))
1106 (error "No patches to delete"))
1107 (when (memq :index patchsyms)
1108 (error "Cannot delete the index"))
1109 (when (memq :work patchsyms)
1110 (error "Cannot delete the work tree"))
1112 (let ((npatches (length patchsyms)))
1113 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1115 (if (= 1 npatches) "" "es")
1117 " (spilling contents to index)"
1119 (let ((args (if spill-p
1120 (cons "--spill" patchsyms)
1122 (stgit-capture-output nil
1123 (apply 'stgit-run "delete" args))
1126 (defun stgit-move-patches-target ()
1127 "Return the patchsym indicating a target patch for
1128 `stgit-move-patches'.
1130 This is either the patch at point, or one of :top and :bottom, if
1131 the point is after or before the applied patches."
1133 (let ((patchsym (stgit-patch-name-at-point)))
1134 (cond (patchsym patchsym)
1135 ((save-excursion (re-search-backward "^>" nil t)) :top)
1138 (defun stgit-sort-patches (patchsyms)
1139 "Returns the list of patches in PATCHSYMS sorted according to
1140 their position in the patch series, bottommost first.
1142 PATCHSYMS may not contain duplicate entries."
1143 (let (sorted-patchsyms
1144 (series (with-output-to-string
1145 (with-current-buffer standard-output
1146 (stgit-run-silent "series" "--noprefix"))))
1148 (while (string-match "^\\(.+\\)" series start)
1149 (let ((patchsym (intern (match-string 1 series))))
1150 (when (memq patchsym patchsyms)
1151 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1152 (setq start (match-end 0)))
1153 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1155 (unless (= (length patchsyms) (length sorted-patchsyms))
1156 (error "Internal error"))
1160 (defun stgit-move-patches (patchsyms target-patch)
1161 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1162 If TARGET-PATCH is :bottom or :top, move the patches to the
1163 bottom or top of the stack, respectively.
1165 Interactively, move the marked patches to where the point is."
1166 (interactive (list stgit-marked-patches
1167 (stgit-move-patches-target)))
1169 (error "Need at least one patch to move"))
1171 (unless target-patch
1172 (error "Point not at a patch"))
1174 (if (eq target-patch :top)
1175 (stgit-capture-output nil
1176 (apply 'stgit-run "float" patchsyms))
1178 ;; need to have patchsyms sorted by position in the stack
1179 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1180 (while sorted-patchsyms
1181 (setq sorted-patchsyms
1182 (and (stgit-capture-output nil
1183 (if (eq target-patch :bottom)
1184 (stgit-run "sink" "--" (car sorted-patchsyms))
1185 (stgit-run "sink" "--to" target-patch "--"
1186 (car sorted-patchsyms))))
1187 (cdr sorted-patchsyms))))))
1190 (defun stgit-squash (patchsyms)
1191 "Squash the patches in PATCHSYMS.
1192 Interactively, squash the marked patches.
1194 Unless there are any conflicts, the patches will be merged into
1195 one patch, which will occupy the same spot in the series as the
1196 deepest patch had before the squash."
1197 (interactive (list stgit-marked-patches))
1198 (when (< (length patchsyms) 2)
1199 (error "Need at least two patches to squash"))
1200 (let ((stgit-buffer (current-buffer))
1201 (edit-buf (get-buffer-create "*StGit edit*"))
1202 (dir default-directory)
1203 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1204 (log-edit 'stgit-confirm-squash t nil edit-buf)
1205 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1206 (setq default-directory dir)
1207 (let ((result (let ((standard-output edit-buf))
1208 (apply 'stgit-run-silent "squash"
1209 "--save-template=-" sorted-patchsyms))))
1211 ;; stg squash may have reordered the patches or caused conflicts
1212 (with-current-buffer stgit-buffer
1215 (unless (eq 0 result)
1217 (rename-buffer "*StGit error*")
1218 (resize-temp-buffer-window)
1219 (switch-to-buffer-other-window stgit-buffer)
1220 (error "stg squash failed")))))
1222 (defun stgit-confirm-squash ()
1224 (let ((file (make-temp-file "stgit-edit-")))
1225 (write-region (point-min) (point-max) file)
1226 (stgit-capture-output nil
1227 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1228 (with-current-buffer log-edit-parent-buffer
1230 ;; Go to first marked patch and stay there
1231 (goto-char (point-min))
1232 (re-search-forward (concat "^[>+-]\\*") nil t)
1233 (move-to-column goal-column)
1234 (let ((pos (point)))
1238 (defun stgit-help ()
1239 "Display help for the StGit mode."
1241 (describe-function 'stgit-mode))
1243 (defun stgit-undo (&optional arg)
1245 With prefix argument, run it with the --hard flag."
1247 (stgit-capture-output nil
1249 (stgit-run "undo" "--hard")
1250 (stgit-run "undo")))
1253 (defun stgit-refresh (&optional arg)
1255 If the index contains any changes, only refresh from index.
1257 With prefix argument, refresh the marked patch or the patch under point."
1259 (let ((patchargs (if arg
1260 (let ((patches (stgit-patches-marked-or-at-point)))
1261 (cond ((null patches)
1262 (error "No patch to update"))
1263 ((> (length patches) 1)
1264 (error "Too many patches selected"))
1266 (cons "-p" patches))))
1268 (unless (stgit-index-empty-p)
1269 (setq patchargs (cons "--index" patchargs)))
1270 (stgit-capture-output nil
1271 (apply 'stgit-run "refresh" patchargs))
1272 (stgit-refresh-git-status))
1275 (defcustom stgit-show-worktree-mode 'center
1276 "This variable controls where the \"Index\" and \"Work tree\"
1277 will be shown on in the buffer.
1279 It can be set to 'top (above all patches), 'center (show between
1280 applied and unapplied patches), and 'bottom (below all patches).
1282 See also `stgit-show-worktree'."
1283 :type '(radio (const :tag "above all patches (top)" top)
1284 (const :tag "between applied and unapplied patches (center)"
1286 (const :tag "below all patches (bottom)" bottom))
1289 (defcustom stgit-default-show-worktree
1291 "Set to non-nil to by default show the working tree in a new stgit buffer.
1293 This value is used as the default value for `stgit-show-worktree'."
1297 (defvar stgit-show-worktree nil
1298 "If nil, inhibit showing work tree and index in the stgit buffer.
1300 See also `stgit-show-worktree-mode'.")
1302 (defun stgit-toggle-worktree (&optional arg)
1303 "Toggle the visibility of the work tree.
1304 With arg, show the work tree if arg is positive.
1306 Its initial setting is controlled by `stgit-default-show-worktree'.
1308 `stgit-show-worktree-mode' controls where on screen the index and
1309 work tree will show up."
1311 (setq stgit-show-worktree
1314 (not stgit-show-worktree)))