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))
70 (face (cdr (assq status stgit-patch-status-face-alist))))
76 (if (memq name stgit-marked-patches)
78 (if (memq status '(index work))
79 (insert (propertize (if (eq status 'index) "Index" "Work tree")
81 (insert (format "%-30s"
82 (propertize (symbol-name name) 'face face))
84 (if (stgit-patch-empty patch) "(empty) " "")
85 (propertize (or (stgit-patch-desc patch) "")
86 'face 'stgit-description-face)))
88 (put-text-property start (point) 'entry-type 'patch)
89 (when (memq name stgit-expanded-patches)
90 (stgit-insert-patch-files patch))
91 (put-text-property start (point) 'patch-data patch)))
93 (defun create-stgit-buffer (dir)
94 "Create a buffer for showing StGit patches.
95 Argument DIR is the repository path."
96 (let ((buf (create-file-buffer (concat dir "*stgit*")))
97 (inhibit-read-only t))
98 (with-current-buffer buf
99 (setq default-directory dir)
101 (set (make-local-variable 'stgit-ewoc)
102 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
103 (setq buffer-read-only t))
106 (defmacro stgit-capture-output (name &rest body)
107 "Capture StGit output and, if there was any output, show it in a window
109 Returns nil if there was no output."
110 (declare (debug ([&or stringp null] body))
112 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
113 (stgit-dir default-directory)
114 (inhibit-read-only t))
115 (with-current-buffer output-buf
117 (setq default-directory stgit-dir)
118 (setq buffer-read-only t))
119 (let ((standard-output output-buf))
121 (with-current-buffer output-buf
122 (set-buffer-modified-p nil)
123 (setq buffer-read-only t)
124 (if (< (point-min) (point-max))
125 (display-buffer output-buf t)))))
127 (defun stgit-make-run-args (args)
128 "Return a copy of ARGS with its elements converted to strings."
130 ;; don't use (format "%s" ...) to limit type errors
131 (cond ((stringp x) x)
132 ((integerp x) (number-to-string x))
133 ((symbolp x) (symbol-name x))
135 (error "Bad element in stgit-make-run-args args: %S" x))))
138 (defun stgit-run-silent (&rest args)
139 (setq args (stgit-make-run-args args))
140 (apply 'call-process "stg" nil standard-output nil args))
142 (defun stgit-run (&rest args)
143 (setq args (stgit-make-run-args args))
144 (let ((msgcmd (mapconcat #'identity args " ")))
145 (message "Running stg %s..." msgcmd)
146 (apply 'call-process "stg" nil standard-output nil args)
147 (message "Running stg %s...done" msgcmd)))
149 (defun stgit-run-git (&rest args)
150 (setq args (stgit-make-run-args args))
151 (let ((msgcmd (mapconcat #'identity args " ")))
152 (message "Running git %s..." msgcmd)
153 (apply 'call-process "git" nil standard-output nil args)
154 (message "Running git %s...done" msgcmd)))
156 (defun stgit-run-git-silent (&rest args)
157 (setq args (stgit-make-run-args args))
158 (apply 'call-process "git" nil standard-output nil args))
160 (defun stgit-index-empty-p ()
161 "Returns non-nil if the index contains no changes from HEAD."
162 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
164 (defvar stgit-index-node)
165 (defvar stgit-worktree-node)
167 (defun stgit-refresh-index ()
168 (when stgit-index-node
169 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
171 (defun stgit-refresh-worktree ()
172 (when stgit-worktree-node
173 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
175 (defun stgit-run-series-insert-index (ewoc)
176 (setq index-node (cons ewoc (ewoc-enter-last ewoc
182 worktree-node (cons ewoc (ewoc-enter-last ewoc
189 (defun stgit-run-series (ewoc)
190 (setq stgit-index-node nil
191 stgit-worktree-node nil)
192 (let ((inserted-index (not stgit-show-worktree))
197 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
198 (goto-char (point-min))
199 (if (not (zerop exit-status))
200 (cond ((looking-at "stg series: \\(.*\\)")
201 (setq inserted-index t)
202 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
203 (substitute-command-keys
204 "-- not initialized; run \\[stgit-init]")))
206 (error "Error running stg: %s"
210 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
211 (error "Syntax error in output from stg series"))
212 (let* ((state-str (match-string 2))
213 (state (cond ((string= state-str ">") 'top)
214 ((string= state-str "+") 'applied)
215 ((string= state-str "-") 'unapplied)))
216 (name (intern (match-string 4)))
217 (desc (match-string 5))
218 (empty (string= (match-string 1) "0")))
219 (unless inserted-index
220 (when (or (eq stgit-show-worktree-mode 'top)
221 (and (eq stgit-show-worktree-mode 'center)
222 (eq state 'unapplied)))
223 (setq inserted-index t)
224 (stgit-run-series-insert-index ewoc)))
225 (setq all-patchsyms (cons name all-patchsyms))
226 (ewoc-enter-last ewoc
233 (unless inserted-index
234 (stgit-run-series-insert-index ewoc)))
235 (setq stgit-index-node index-node
236 stgit-worktree-node worktree-node
237 stgit-marked-patches (intersection stgit-marked-patches
240 (defun stgit-reload ()
241 "Update the contents of the StGit buffer."
243 (let ((inhibit-read-only t)
244 (curline (line-number-at-pos))
245 (curpatch (stgit-patch-name-at-point)))
246 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
247 (ewoc-set-hf stgit-ewoc
251 (stgit-run-silent "branch")
252 (buffer-substring (point-min) (1- (point-max))))
253 'face 'stgit-branch-name-face)
255 (if stgit-show-worktree
258 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
259 shows the working tree\n")
260 'face 'stgit-description-face)))
261 (stgit-run-series stgit-ewoc)
263 (stgit-goto-patch curpatch)
264 (goto-line curline)))
265 (stgit-refresh-git-status))
268 "A user interface for the StGit patch maintenance tool."
271 (defface stgit-description-face
272 '((((background dark)) (:foreground "tan"))
273 (((background light)) (:foreground "dark red")))
274 "The face used for StGit descriptions"
277 (defface stgit-branch-name-face
279 "The face used for the StGit branch name"
282 (defface stgit-top-patch-face
283 '((((background dark)) (:weight bold :foreground "yellow"))
284 (((background light)) (:weight bold :foreground "purple"))
286 "The face used for the top patch names"
289 (defface stgit-applied-patch-face
290 '((((background dark)) (:foreground "light yellow"))
291 (((background light)) (:foreground "purple"))
293 "The face used for applied patch names"
296 (defface stgit-unapplied-patch-face
297 '((((background dark)) (:foreground "gray80"))
298 (((background light)) (:foreground "orchid"))
300 "The face used for unapplied patch names"
303 (defface stgit-modified-file-face
304 '((((class color) (background light)) (:foreground "purple"))
305 (((class color) (background dark)) (:foreground "salmon")))
306 "StGit mode face used for modified file status"
309 (defface stgit-unmerged-file-face
310 '((((class color) (background light)) (:foreground "red" :bold t))
311 (((class color) (background dark)) (:foreground "red" :bold t)))
312 "StGit mode face used for unmerged file status"
315 (defface stgit-unknown-file-face
316 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
317 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
318 "StGit mode face used for unknown file status"
321 (defface stgit-file-permission-face
322 '((((class color) (background light)) (:foreground "green" :bold t))
323 (((class color) (background dark)) (:foreground "green" :bold t)))
324 "StGit mode face used for permission changes."
327 (defface stgit-index-work-tree-title-face
328 '((((supports :slant italic)) :slant italic)
330 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
334 (defcustom stgit-expand-find-copies-harder
336 "Try harder to find copied files when listing patches.
338 When not nil, runs git diff-tree with the --find-copies-harder
339 flag, which reduces performance."
343 (defconst stgit-file-status-code-strings
344 (mapcar (lambda (arg)
346 (propertize (cadr arg) 'face (car (cddr arg)))))
347 '((add "Added" stgit-modified-file-face)
348 (copy "Copied" stgit-modified-file-face)
349 (delete "Deleted" stgit-modified-file-face)
350 (modify "Modified" stgit-modified-file-face)
351 (rename "Renamed" stgit-modified-file-face)
352 (mode-change "Mode change" stgit-modified-file-face)
353 (unmerged "Unmerged" stgit-unmerged-file-face)
354 (unknown "Unknown" stgit-unknown-file-face)))
355 "Alist of code symbols to description strings")
357 (defconst stgit-patch-status-face-alist
358 '((applied . stgit-applied-patch-face)
359 (top . stgit-top-patch-face)
360 (unapplied . stgit-unapplied-patch-face)
361 (index . stgit-index-work-tree-title-face)
362 (work . stgit-index-work-tree-title-face))
363 "Alist of face to use for a given patch status")
365 (defun stgit-file-status-code-as-string (file)
366 "Return stgit status code for FILE as a string"
367 (let* ((code (assq (stgit-file-status file)
368 stgit-file-status-code-strings))
369 (score (stgit-file-cr-score file)))
372 (if (and score (/= score 100))
373 (format "%s %s" (cdr code)
374 (propertize (format "%d%%" score)
375 'face 'stgit-description-face))
378 (defun stgit-file-status-code (str &optional score)
379 "Return stgit status code from git status string"
380 (let ((code (assoc str '(("A" . add)
388 (setq code (if code (cdr code) 'unknown))
389 (when (stringp score)
390 (if (> (length score) 0)
391 (setq score (string-to-number score))
393 (if score (cons code score) code)))
395 (defconst stgit-file-type-strings
398 (#o160 . "subproject"))
399 "Alist of names of file types")
401 (defun stgit-file-type-string (type)
402 "Return string describing file type TYPE (the high bits of file permission).
403 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
404 (let ((type-str (assoc type stgit-file-type-strings)))
405 (or (and type-str (cdr type-str))
406 (format "unknown type %o" type))))
408 (defun stgit-file-type-change-string (old-perm new-perm)
409 "Return string describing file type change from OLD-PERM to NEW-PERM.
410 Cf. `stgit-file-type-string'."
411 (let ((old-type (lsh old-perm -9))
412 (new-type (lsh new-perm -9)))
413 (cond ((= old-type new-type) "")
414 ((zerop new-type) "")
416 (if (= new-type #o100)
418 (format " (%s)" (stgit-file-type-string new-type))))
419 (t (format " (%s -> %s)"
420 (stgit-file-type-string old-type)
421 (stgit-file-type-string new-type))))))
423 (defun stgit-file-mode-change-string (old-perm new-perm)
424 "Return string describing file mode change from OLD-PERM to NEW-PERM.
425 Cf. `stgit-file-type-change-string'."
426 (setq old-perm (logand old-perm #o777)
427 new-perm (logand new-perm #o777))
428 (if (or (= old-perm new-perm)
432 (let* ((modified (logxor old-perm new-perm))
433 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
434 (cond ((zerop modified) "")
435 ((and (zerop not-x-modified)
436 (or (and (eq #o111 (logand old-perm #o111))
437 (propertize "-x" 'face 'stgit-file-permission-face))
438 (and (eq #o111 (logand new-perm #o111))
439 (propertize "+x" 'face
440 'stgit-file-permission-face)))))
441 (t (concat (propertize (format "%o" old-perm)
442 'face 'stgit-file-permission-face)
444 'face 'stgit-description-face)
445 (propertize (format "%o" new-perm)
446 'face 'stgit-file-permission-face)))))))
448 (defstruct (stgit-file)
449 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
451 (defun stgit-file-pp (file)
452 (let ((status (stgit-file-status file))
453 (name (if (stgit-file-copy-or-rename file)
454 (concat (stgit-file-cr-from file)
456 'face 'stgit-description-face)
457 (stgit-file-cr-to file))
458 (stgit-file-file file)))
459 (mode-change (stgit-file-mode-change-string
460 (stgit-file-old-perm file)
461 (stgit-file-new-perm file)))
463 (insert (format " %-12s%1s%s%s\n"
464 (stgit-file-status-code-as-string file)
467 (propertize (stgit-file-type-change-string
468 (stgit-file-old-perm file)
469 (stgit-file-new-perm file))
470 'face 'stgit-description-face)))
471 (add-text-properties start (point)
472 (list 'entry-type 'file
475 (defun stgit-find-copies-harder-diff-arg ()
476 "Return the flag to use with `git-diff' depending on the
477 `stgit-expand-find-copies-harder' flag."
478 (if stgit-expand-find-copies-harder
479 "--find-copies-harder"
482 (defun stgit-insert-patch-files (patch)
483 "Expand (show modification of) the patch PATCH after the line
485 (let* ((patchsym (stgit-patch-name patch))
487 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
488 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
489 (set-marker-insertion-type end t)
490 (setf (stgit-patch-files-ewoc patch) ewoc)
492 (apply 'stgit-run-git
493 (cond ((eq patchsym :work)
494 `("diff-files" ,@args))
495 ((eq patchsym :index)
496 `("diff-index" ,@args "--cached" "HEAD"))
498 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
499 (goto-char (point-min))
500 (unless (or (eobp) (memq patchsym '(:work :index)))
502 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
503 (let ((old-perm (string-to-number (match-string 1) 8))
504 (new-perm (string-to-number (match-string 2) 8)))
505 (goto-char (match-end 0))
508 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
509 (let* ((patch-status (stgit-patch-status patch))
510 (file-subexp (if (eq patch-status 'unapplied)
513 (file (match-string file-subexp)))
518 :cr-score (string-to-number (match-string 2))
519 :cr-from (match-string 3)
520 :cr-to (match-string 4)
521 :status (stgit-file-status-code
524 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
532 :status (stgit-file-status-code
534 :file (match-string 2))))))
535 (ewoc-enter-last ewoc file))
536 (goto-char (match-end 0))))
537 (unless (ewoc-nth ewoc 0)
540 (propertize "<no files>"
541 'face 'stgit-description-face)
545 (defun stgit-select-file ()
546 (let ((filename (expand-file-name
547 (stgit-file-file (stgit-patched-file-at-point)))))
548 (unless (file-exists-p filename)
549 (error "File does not exist"))
550 (find-file filename)))
552 (defun stgit-select-patch ()
553 (let ((patchname (stgit-patch-name-at-point)))
554 (if (memq patchname stgit-expanded-patches)
555 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
556 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
557 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
558 (move-to-column (stgit-goal-column)))
560 (defun stgit-select ()
561 "With point on a patch, toggle showing files in the patch.
563 With point on a file, open the associated file. Opens the target
564 file for (applied) copies and renames."
566 (case (get-text-property (point) 'entry-type)
568 (stgit-select-patch))
572 (error "No patch or file on line"))))
574 (defun stgit-find-file-other-window ()
575 "Open file at point in other window"
577 (let ((patched-file (stgit-patched-file-at-point)))
579 (error "No file on the current line"))
580 (let ((filename (expand-file-name (stgit-file-file patched-file))))
581 (unless (file-exists-p filename)
582 (error "File does not exist"))
583 (find-file-other-window filename))))
586 "Hide the stgit buffer."
590 (defun stgit-git-status ()
591 "Show status using `git-status'."
593 (unless (fboundp 'git-status)
594 (error "The stgit-git-status command requires git-status"))
595 (let ((dir default-directory))
596 (save-selected-window
600 (defun stgit-goal-column ()
601 "Return goal column for the current line"
602 (case (get-text-property (point) 'entry-type)
607 (defun stgit-next-line (&optional arg)
608 "Move cursor vertically down ARG lines"
611 (move-to-column (stgit-goal-column)))
613 (defun stgit-previous-line (&optional arg)
614 "Move cursor vertically up ARG lines"
617 (move-to-column (stgit-goal-column)))
619 (defun stgit-next-patch (&optional arg)
620 "Move cursor down ARG patches."
622 (ewoc-goto-next stgit-ewoc (or arg 1))
623 (move-to-column goal-column))
625 (defun stgit-previous-patch (&optional arg)
626 "Move cursor up ARG patches."
628 (ewoc-goto-prev stgit-ewoc (or arg 1))
629 (move-to-column goal-column))
631 (defvar stgit-mode-hook nil
632 "Run after `stgit-mode' is setup.")
634 (defvar stgit-mode-map nil
635 "Keymap for StGit major mode.")
637 (unless stgit-mode-map
638 (let ((toggle-map (make-keymap)))
639 (suppress-keymap toggle-map)
640 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
641 '(("t" . stgit-toggle-worktree)))
642 (setq stgit-mode-map (make-keymap))
643 (suppress-keymap stgit-mode-map)
644 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
647 ("\d" . stgit-unmark-up)
648 ("u" . stgit-unmark-down)
651 ("\C-p" . stgit-previous-line)
652 ("\C-n" . stgit-next-line)
653 ([up] . stgit-previous-line)
654 ([down] . stgit-next-line)
655 ("p" . stgit-previous-patch)
656 ("n" . stgit-next-patch)
657 ("\M-{" . stgit-previous-patch)
658 ("\M-}" . stgit-next-patch)
659 ("s" . stgit-git-status)
660 ("g" . stgit-reload-or-repair)
661 ("r" . stgit-refresh)
662 ("\C-c\C-r" . stgit-rename)
664 ("M" . stgit-move-patches)
667 ("\C-c\C-c" . stgit-commit)
668 ("\C-c\C-u" . stgit-uncommit)
669 ("U" . stgit-revert-file)
670 ("R" . stgit-resolve-file)
671 ("\r" . stgit-select)
672 ("o" . stgit-find-file-other-window)
673 ("i" . stgit-file-toggle-index)
674 (">" . stgit-push-next)
675 ("<" . stgit-pop-next)
676 ("P" . stgit-push-or-pop)
680 ([(control ?/)] . stgit-undo)
681 ("\C-_" . stgit-undo)
684 ("q" . stgit-quit)))))
687 "Major mode for interacting with StGit.
690 (kill-all-local-variables)
691 (buffer-disable-undo)
692 (setq mode-name "StGit"
693 major-mode 'stgit-mode
695 (use-local-map stgit-mode-map)
696 (set (make-local-variable 'list-buffers-directory) default-directory)
697 (set (make-local-variable 'stgit-marked-patches) nil)
698 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
699 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
700 (set (make-local-variable 'stgit-index-node) nil)
701 (set (make-local-variable 'stgit-worktree-node) nil)
702 (set-variable 'truncate-lines 't)
703 (add-hook 'after-save-hook 'stgit-update-saved-file)
704 (run-hooks 'stgit-mode-hook))
706 (defun stgit-update-saved-file ()
707 (let* ((file (expand-file-name buffer-file-name))
708 (dir (file-name-directory file))
709 (gitdir (condition-case nil (git-get-top-dir dir)
711 (buffer (and gitdir (stgit-find-buffer gitdir))))
713 (with-current-buffer buffer
714 (stgit-refresh-worktree)))))
716 (defun stgit-add-mark (patchsym)
717 "Mark the patch PATCHSYM."
718 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
720 (defun stgit-remove-mark (patchsym)
721 "Unmark the patch PATCHSYM."
722 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
724 (defun stgit-clear-marks ()
725 "Unmark all patches."
726 (setq stgit-marked-patches '()))
728 (defun stgit-patch-at-point (&optional cause-error)
729 (get-text-property (point) 'patch-data))
731 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
732 "Return the patch name on the current line as a symbol.
733 If CAUSE-ERROR is not nil, signal an error if none found.
734 If ONLY-PATCHES is not nil, only allow real patches, and not
736 (let ((patch (stgit-patch-at-point)))
739 (memq (stgit-patch-status patch) '(work index))
742 (stgit-patch-name patch))
744 (error "No patch on this line")))))
746 (defun stgit-patched-file-at-point ()
747 (get-text-property (point) 'file-data))
749 (defun stgit-patches-marked-or-at-point ()
750 "Return the symbols of the marked patches, or the patch on the current line."
751 (if stgit-marked-patches
753 (let ((patch (stgit-patch-name-at-point)))
758 (defun stgit-goto-patch (patchsym)
759 "Move point to the line containing patch PATCHSYM.
760 If that patch cannot be found, do nothing."
761 (let ((node (ewoc-nth stgit-ewoc 0)))
762 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
764 (setq node (ewoc-next stgit-ewoc node)))
766 (ewoc-goto-node stgit-ewoc node)
767 (move-to-column goal-column))))
772 (stgit-capture-output nil
777 "Mark the patch under point."
779 (let* ((node (ewoc-locate stgit-ewoc))
780 (patch (ewoc-data node))
781 (name (stgit-patch-name patch)))
782 (when (eq name :work)
783 (error "Cannot mark the work tree"))
784 (when (eq name :index)
785 (error "Cannot mark the index"))
786 (stgit-add-mark (stgit-patch-name patch))
787 (ewoc-invalidate stgit-ewoc node))
790 (defun stgit-unmark-up ()
791 "Remove mark from the patch on the previous line."
793 (stgit-previous-patch)
794 (let* ((node (ewoc-locate stgit-ewoc))
795 (patch (ewoc-data node)))
796 (stgit-remove-mark (stgit-patch-name patch))
797 (ewoc-invalidate stgit-ewoc node))
798 (move-to-column (stgit-goal-column)))
800 (defun stgit-unmark-down ()
801 "Remove mark from the patch on the current line."
803 (let* ((node (ewoc-locate stgit-ewoc))
804 (patch (ewoc-data node)))
805 (stgit-remove-mark (stgit-patch-name patch))
806 (ewoc-invalidate stgit-ewoc node))
809 (defun stgit-rename (name)
810 "Rename the patch under point to NAME."
812 (read-string "Patch name: "
813 (symbol-name (stgit-patch-name-at-point t t)))))
814 (let ((old-patchsym (stgit-patch-name-at-point t t)))
815 (stgit-capture-output nil
816 (stgit-run "rename" old-patchsym name))
817 (let ((name-sym (intern name)))
818 (when (memq old-patchsym stgit-expanded-patches)
819 (setq stgit-expanded-patches
820 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
821 (when (memq old-patchsym stgit-marked-patches)
822 (setq stgit-marked-patches
823 (cons name-sym (delq old-patchsym stgit-marked-patches))))
825 (stgit-goto-patch name-sym))))
827 (defun stgit-reload-or-repair (repair)
828 "Update the contents of the StGit buffer (`stgit-reload').
830 With a prefix argument, repair the StGit metadata if the branch
831 was modified with git commands (`stgit-repair')."
837 (defun stgit-repair ()
840 (stgit-capture-output nil
841 (stgit-run "repair"))
844 (defun stgit-available-branches ()
845 "Returns a list of the available stg branches"
846 (let ((output (with-output-to-string
847 (stgit-run "branch" "--list")))
850 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
851 (setq result (cons (match-string 1 output) result))
852 (setq start (match-end 0)))
855 (defun stgit-branch (branch)
856 "Switch to branch BRANCH."
857 (interactive (list (completing-read "Switch to branch: "
858 (stgit-available-branches))))
859 (stgit-capture-output nil (stgit-run "branch" "--" branch))
862 (defun stgit-commit (count)
863 "Run stg commit on COUNT commits.
864 Interactively, the prefix argument is used as COUNT."
866 (stgit-capture-output nil (stgit-run "commit" "-n" count))
869 (defun stgit-revert-file ()
870 "Revert the file at point, which must be in the index or the
873 (let* ((patched-file (or (stgit-patched-file-at-point)
874 (error "No file on the current line")))
875 (patch-name (stgit-patch-name-at-point))
876 (file-status (stgit-file-status patched-file))
877 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
878 (stgit-file-cr-to patched-file))
879 ((eq file-status 'add)
880 (stgit-file-file patched-file))))
881 (co-file (cond ((eq file-status 'rename)
882 (stgit-file-cr-from patched-file))
883 ((not (memq file-status '(copy add)))
884 (stgit-file-file patched-file)))))
886 (unless (memq patch-name '(:work :index))
887 (error "No index or working tree file on this line"))
889 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
890 (when (yes-or-no-p (format "Revert %d file%s? "
892 (if (= nfiles 1) "" "s")))
893 (stgit-capture-output nil
895 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
897 (stgit-run-git "checkout" "HEAD" co-file)))
900 (defun stgit-resolve-file ()
901 "Resolve conflict in the file at point."
903 (let* ((patched-file (stgit-patched-file-at-point))
904 (patch (stgit-patch-at-point))
905 (patch-name (and patch (stgit-patch-name patch)))
906 (status (and patched-file (stgit-file-status patched-file))))
908 (unless (memq patch-name '(:work :index))
909 (error "No index or working tree file on this line"))
911 (unless (eq status 'unmerged)
912 (error "No conflict to resolve at the current line"))
914 (stgit-capture-output nil
915 (stgit-move-change-to-index (stgit-file-file patched-file)))
919 (defun stgit-uncommit (count)
920 "Run stg uncommit on COUNT commits.
921 Interactively, the prefix argument is used as COUNT."
923 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
926 (defun stgit-push-next (npatches)
927 "Push the first unapplied patch.
928 With numeric prefix argument, push that many patches."
930 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
932 (stgit-refresh-git-status))
934 (defun stgit-pop-next (npatches)
935 "Pop the topmost applied patch.
936 With numeric prefix argument, pop that many patches."
938 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
940 (stgit-refresh-git-status))
942 (defun stgit-applied-at-point ()
943 "Is the patch on the current line applied?"
946 (looking-at "[>+]")))
948 (defun stgit-push-or-pop ()
949 "Push or pop the patch on the current line."
951 (let ((patchsym (stgit-patch-name-at-point t))
952 (applied (stgit-applied-at-point)))
953 (stgit-capture-output nil
954 (stgit-run (if applied "pop" "push") patchsym))
958 "Go to the patch on the current line."
960 (let ((patchsym (stgit-patch-name-at-point t)))
961 (stgit-capture-output nil
962 (stgit-run "goto" patchsym))
965 (defun stgit-id (patchsym)
966 "Return the git commit id for PATCHSYM.
967 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
968 (if (keywordp patchsym)
970 (let ((result (with-output-to-string
971 (stgit-run-silent "id" patchsym))))
972 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
973 (error "Cannot find commit id for %s" patchsym))
974 (match-string 1 result))))
977 "Show the patch on the current line."
979 (stgit-capture-output "*StGit patch*"
980 (case (get-text-property (point) 'entry-type)
982 (let* ((patched-file (stgit-patched-file-at-point))
983 (patch-name (stgit-patch-name-at-point))
984 (patch-id (stgit-id patch-name))
985 (args (append (and (stgit-file-cr-from patched-file)
986 (list (stgit-find-copies-harder-diff-arg)))
987 (cond ((eq patch-id :index)
992 (list (concat patch-id "^") patch-id)))
994 (if (stgit-file-copy-or-rename patched-file)
995 (list (stgit-file-cr-from patched-file)
996 (stgit-file-cr-to patched-file))
997 (list (stgit-file-file patched-file))))))
998 (apply 'stgit-run-git "diff" args)))
1000 (let* ((patch-name (stgit-patch-name-at-point))
1001 (patch-id (stgit-id patch-name)))
1002 (if (or (eq patch-id :index) (eq patch-id :work))
1003 (apply 'stgit-run-git "diff"
1004 (stgit-find-copies-harder-diff-arg)
1005 (and (eq patch-id :index)
1007 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
1008 (stgit-patch-name-at-point)))))
1010 (error "No patch or file at point")))
1011 (with-current-buffer standard-output
1012 (goto-char (point-min))
1015 (defun stgit-move-change-to-index (file)
1016 "Copies the workspace state of FILE to index, using git add or git rm"
1017 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1018 '("add") '("rm" "-q"))))
1019 (stgit-capture-output "*git output*"
1020 (apply 'stgit-run-git (append op '("--") (list file))))))
1022 (defun stgit-remove-change-from-index (file)
1023 "Unstages the change in FILE from the index"
1024 (stgit-capture-output "*git output*"
1025 (stgit-run-git "reset" "-q" "--" file)))
1027 (defun stgit-file-toggle-index ()
1028 "Move modified file in or out of the index."
1030 (let ((patched-file (stgit-patched-file-at-point)))
1031 (unless patched-file
1032 (error "No file on the current line"))
1033 (when (eq (stgit-file-status patched-file) 'unmerged)
1034 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1035 (let ((patch-name (stgit-patch-name-at-point)))
1036 (cond ((eq patch-name :work)
1037 (stgit-move-change-to-index (stgit-file-file patched-file)))
1038 ((eq patch-name :index)
1039 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1041 (error "Can only move files in the working tree to index")))))
1042 (stgit-refresh-worktree)
1043 (stgit-refresh-index))
1045 (defun stgit-edit ()
1046 "Edit the patch on the current line."
1048 (let ((patchsym (stgit-patch-name-at-point t t))
1049 (edit-buf (get-buffer-create "*StGit edit*"))
1050 (dir default-directory))
1051 (log-edit 'stgit-confirm-edit t nil edit-buf)
1052 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1053 (setq default-directory dir)
1054 (let ((standard-output edit-buf))
1055 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1057 (defun stgit-confirm-edit ()
1059 (let ((file (make-temp-file "stgit-edit-")))
1060 (write-region (point-min) (point-max) file)
1061 (stgit-capture-output nil
1062 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1063 (with-current-buffer log-edit-parent-buffer
1066 (defun stgit-new (add-sign)
1067 "Create a new patch.
1068 With a prefix argument, include a \"Signed-off-by:\" line at the
1071 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1072 (dir default-directory))
1073 (log-edit 'stgit-confirm-new t nil edit-buf)
1074 (setq default-directory dir)
1077 (let ((standard-output (current-buffer)))
1078 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1080 (defun stgit-confirm-new ()
1082 (let ((file (make-temp-file "stgit-edit-")))
1083 (write-region (point-min) (point-max) file)
1084 (stgit-capture-output nil
1085 (stgit-run "new" "-f" file))
1086 (with-current-buffer log-edit-parent-buffer
1089 (defun stgit-create-patch-name (description)
1090 "Create a patch name from a long description"
1092 (while (> (length description) 0)
1093 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1094 (setq patch (downcase (concat patch
1095 (match-string 0 description))))
1096 (setq description (substring description (match-end 0))))
1097 ((string-match "\\` +" description)
1098 (setq patch (concat patch "-"))
1099 (setq description (substring description (match-end 0))))
1100 ((string-match "\\`[^a-zA-Z_-]+" description)
1101 (setq description (substring description (match-end 0))))))
1102 (cond ((= (length patch) 0)
1104 ((> (length patch) 20)
1105 (substring patch 0 20))
1108 (defun stgit-delete (patchsyms &optional spill-p)
1109 "Delete the patches in PATCHSYMS.
1110 Interactively, delete the marked patches, or the patch at point.
1112 With a prefix argument, or SPILL-P, spill the patch contents to
1113 the work tree and index."
1114 (interactive (list (stgit-patches-marked-or-at-point)
1115 current-prefix-arg))
1117 (error "No patches to delete"))
1118 (when (memq :index patchsyms)
1119 (error "Cannot delete the index"))
1120 (when (memq :work patchsyms)
1121 (error "Cannot delete the work tree"))
1123 (let ((npatches (length patchsyms)))
1124 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1126 (if (= 1 npatches) "" "es")
1128 " (spilling contents to index)"
1130 (let ((args (if spill-p
1131 (cons "--spill" patchsyms)
1133 (stgit-capture-output nil
1134 (apply 'stgit-run "delete" args))
1137 (defun stgit-move-patches-target ()
1138 "Return the patchsym indicating a target patch for
1139 `stgit-move-patches'.
1141 This is either the patch at point, or one of :top and :bottom, if
1142 the point is after or before the applied patches."
1144 (let ((patchsym (stgit-patch-name-at-point)))
1145 (cond (patchsym patchsym)
1146 ((save-excursion (re-search-backward "^>" nil t)) :top)
1149 (defun stgit-sort-patches (patchsyms)
1150 "Returns the list of patches in PATCHSYMS sorted according to
1151 their position in the patch series, bottommost first.
1153 PATCHSYMS may not contain duplicate entries."
1154 (let (sorted-patchsyms
1155 (series (with-output-to-string
1156 (with-current-buffer standard-output
1157 (stgit-run-silent "series" "--noprefix"))))
1159 (while (string-match "^\\(.+\\)" series start)
1160 (let ((patchsym (intern (match-string 1 series))))
1161 (when (memq patchsym patchsyms)
1162 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1163 (setq start (match-end 0)))
1164 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1166 (unless (= (length patchsyms) (length sorted-patchsyms))
1167 (error "Internal error"))
1171 (defun stgit-move-patches (patchsyms target-patch)
1172 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1173 If TARGET-PATCH is :bottom or :top, move the patches to the
1174 bottom or top of the stack, respectively.
1176 Interactively, move the marked patches to where the point is."
1177 (interactive (list stgit-marked-patches
1178 (stgit-move-patches-target)))
1180 (error "Need at least one patch to move"))
1182 (unless target-patch
1183 (error "Point not at a patch"))
1185 (if (eq target-patch :top)
1186 (stgit-capture-output nil
1187 (apply 'stgit-run "float" patchsyms))
1189 ;; need to have patchsyms sorted by position in the stack
1190 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1191 (while sorted-patchsyms
1192 (setq sorted-patchsyms
1193 (and (stgit-capture-output nil
1194 (if (eq target-patch :bottom)
1195 (stgit-run "sink" "--" (car sorted-patchsyms))
1196 (stgit-run "sink" "--to" target-patch "--"
1197 (car sorted-patchsyms))))
1198 (cdr sorted-patchsyms))))))
1201 (defun stgit-squash (patchsyms)
1202 "Squash the patches in PATCHSYMS.
1203 Interactively, squash the marked patches.
1205 Unless there are any conflicts, the patches will be merged into
1206 one patch, which will occupy the same spot in the series as the
1207 deepest patch had before the squash."
1208 (interactive (list stgit-marked-patches))
1209 (when (< (length patchsyms) 2)
1210 (error "Need at least two patches to squash"))
1211 (let ((stgit-buffer (current-buffer))
1212 (edit-buf (get-buffer-create "*StGit edit*"))
1213 (dir default-directory)
1214 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1215 (log-edit 'stgit-confirm-squash t nil edit-buf)
1216 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1217 (setq default-directory dir)
1218 (let ((result (let ((standard-output edit-buf))
1219 (apply 'stgit-run-silent "squash"
1220 "--save-template=-" sorted-patchsyms))))
1222 ;; stg squash may have reordered the patches or caused conflicts
1223 (with-current-buffer stgit-buffer
1226 (unless (eq 0 result)
1228 (rename-buffer "*StGit error*")
1229 (resize-temp-buffer-window)
1230 (switch-to-buffer-other-window stgit-buffer)
1231 (error "stg squash failed")))))
1233 (defun stgit-confirm-squash ()
1235 (let ((file (make-temp-file "stgit-edit-")))
1236 (write-region (point-min) (point-max) file)
1237 (stgit-capture-output nil
1238 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1239 (with-current-buffer log-edit-parent-buffer
1241 ;; Go to first marked patch and stay there
1242 (goto-char (point-min))
1243 (re-search-forward (concat "^[>+-]\\*") nil t)
1244 (move-to-column goal-column)
1245 (let ((pos (point)))
1249 (defun stgit-help ()
1250 "Display help for the StGit mode."
1252 (describe-function 'stgit-mode))
1254 (defun stgit-undo (&optional arg)
1256 With prefix argument, run it with the --hard flag."
1258 (stgit-capture-output nil
1260 (stgit-run "undo" "--hard")
1261 (stgit-run "undo")))
1264 (defun stgit-refresh (&optional arg)
1266 If the index contains any changes, only refresh from index.
1268 With prefix argument, refresh the marked patch or the patch under point."
1270 (let ((patchargs (if arg
1271 (let ((patches (stgit-patches-marked-or-at-point)))
1272 (cond ((null patches)
1273 (error "No patch to update"))
1274 ((> (length patches) 1)
1275 (error "Too many patches selected"))
1277 (cons "-p" patches))))
1279 (unless (stgit-index-empty-p)
1280 (setq patchargs (cons "--index" patchargs)))
1281 (stgit-capture-output nil
1282 (apply 'stgit-run "refresh" patchargs))
1283 (stgit-refresh-git-status))
1286 (defcustom stgit-show-worktree-mode 'center
1287 "This variable controls where the \"Index\" and \"Work tree\"
1288 will be shown on in the buffer.
1290 It can be set to 'top (above all patches), 'center (show between
1291 applied and unapplied patches), and 'bottom (below all patches).
1293 See also `stgit-show-worktree'."
1294 :type '(radio (const :tag "above all patches (top)" top)
1295 (const :tag "between applied and unapplied patches (center)"
1297 (const :tag "below all patches (bottom)" bottom))
1300 (defcustom stgit-default-show-worktree
1302 "Set to non-nil to by default show the working tree in a new stgit buffer.
1304 This value is used as the default value for `stgit-show-worktree'."
1308 (defvar stgit-show-worktree nil
1309 "If nil, inhibit showing work tree and index in the stgit buffer.
1311 See also `stgit-show-worktree-mode'.")
1313 (defun stgit-toggle-worktree (&optional arg)
1314 "Toggle the visibility of the work tree.
1315 With arg, show the work tree if arg is positive.
1317 Its initial setting is controlled by `stgit-default-show-worktree'.
1319 `stgit-show-worktree-mode' controls where on screen the index and
1320 work tree will show up."
1322 (setq stgit-show-worktree
1325 (not stgit-show-worktree)))