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'
12 (when (< emacs-major-version 22)
13 (error "Emacs older than 22 is not supported by stgit.el"))
19 (require 'format-spec)
21 (defun stgit-set-default (symbol value)
22 "Set default value of SYMBOL to VALUE using `set-default' and
23 reload all StGit buffers."
24 (set-default symbol value)
25 (dolist (buf (buffer-list))
26 (with-current-buffer buf
27 (when (eq major-mode 'stgit-mode)
31 "A user interface for the StGit patch maintenance tool."
33 :link '(function-link stgit)
34 :link '(url-link "http://www.procode.org/stgit/"))
36 (defcustom stgit-abbreviate-copies-and-renames t
37 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
38 instead of \"dir/old/file -> dir/new/file\"."
41 :set 'stgit-set-default)
43 (defcustom stgit-default-show-worktree t
44 "Set to non-nil to by default show the working tree in a new stgit buffer.
46 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
49 :link '(variable-link stgit-show-worktree))
51 (defcustom stgit-find-copies-harder nil
52 "Try harder to find copied files when listing patches.
54 When not nil, runs git diff-tree with the --find-copies-harder
55 flag, which reduces performance."
58 :set 'stgit-set-default)
60 (defcustom stgit-show-worktree-mode 'center
61 "This variable controls where the \"Index\" and \"Work tree\"
62 will be shown on in the buffer.
64 It can be set to 'top (above all patches), 'center (show between
65 applied and unapplied patches), and 'bottom (below all patches)."
66 :type '(radio (const :tag "above all patches (top)" top)
67 (const :tag "between applied and unapplied patches (center)"
69 (const :tag "below all patches (bottom)" bottom))
71 :link '(variable-link stgit-show-worktree)
72 :set 'stgit-set-default)
74 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
75 "The format string used to format patch lines.
76 The format string is passed to `format-spec' and the following
77 format characters are recognized:
79 %s - A '+', '-', '>' or space, depending on whether the patch is
80 applied, unapplied, top, or something else.
82 %m - An asterisk if the patch is marked, and a space otherwise.
86 %e - The string \"(empty) \" if the patch is empty.
88 %d - The short patch description.
90 %D - The short patch description, or the patch name.
92 When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
93 variable is used instead."
96 :set 'stgit-set-default)
98 (defcustom stgit-noname-patch-line-format "%s%m%e%D"
99 "The alternate format string used to format patch lines.
100 It has the same semantics as `stgit-patch-line-format', and the
101 display can be toggled between the two formats using
102 \\<stgit-mode-map>>\\[stgit-toggle-patch-names].
104 The alternate form is used when the patch name is hidden."
107 :set 'stgit-set-default)
109 (defcustom stgit-default-show-patch-names t
110 "If non-nil, default to showing patch names in a new stgit buffer.
112 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] to toggle the
113 this setting in an already-started StGit buffer."
116 :link '(variable-link stgit-show-patch-names))
118 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
119 "The format string used to format file lines.
120 The format string is passed to `format-spec' and the following
121 format characters are recognized:
123 %s - A string describing the status of the file.
125 %m - Mode change information
129 %c - A description of file changes."
132 :set 'stgit-set-default)
134 (defface stgit-branch-name-face
136 "The face used for the StGit branch name"
139 (defface stgit-top-patch-face
140 '((((background dark)) (:weight bold :foreground "yellow"))
141 (((background light)) (:weight bold :foreground "purple"))
143 "The face used for the top patch names"
146 (defface stgit-applied-patch-face
147 '((((background dark)) (:foreground "light yellow"))
148 (((background light)) (:foreground "purple"))
150 "The face used for applied patch names"
153 (defface stgit-unapplied-patch-face
154 '((((background dark)) (:foreground "gray80"))
155 (((background light)) (:foreground "orchid"))
157 "The face used for unapplied patch names"
160 (defface stgit-description-face
161 '((((background dark)) (:foreground "tan"))
162 (((background light)) (:foreground "dark red")))
163 "The face used for StGit descriptions"
166 (defface stgit-index-work-tree-title-face
167 '((((supports :slant italic)) :slant italic)
169 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
172 (defface stgit-unmerged-file-face
173 '((((class color) (background light)) (:foreground "red" :bold t))
174 (((class color) (background dark)) (:foreground "red" :bold t)))
175 "StGit mode face used for unmerged file status"
178 (defface stgit-unknown-file-face
179 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
180 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
181 "StGit mode face used for unknown file status"
184 (defface stgit-ignored-file-face
185 '((((class color) (background light)) (:foreground "grey60"))
186 (((class color) (background dark)) (:foreground "grey40")))
187 "StGit mode face used for ignored files")
189 (defface stgit-file-permission-face
190 '((((class color) (background light)) (:foreground "green" :bold t))
191 (((class color) (background dark)) (:foreground "green" :bold t)))
192 "StGit mode face used for permission changes."
195 (defface stgit-modified-file-face
196 '((((class color) (background light)) (:foreground "purple"))
197 (((class color) (background dark)) (:foreground "salmon")))
198 "StGit mode face used for modified file status"
202 "Manage StGit patches for the tree in DIR.
204 See `stgit-mode' for commands available."
205 (interactive "DDirectory: \n")
206 (switch-to-stgit-buffer (git-get-top-dir dir))
209 (defun stgit-assert-mode ()
210 "Signal an error if not in an StGit buffer."
211 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
213 (unless (fboundp 'git-get-top-dir)
214 (defun git-get-top-dir (dir)
215 "Retrieve the top-level directory of a git tree."
216 (let ((cdup (with-output-to-string
217 (with-current-buffer standard-output
219 (unless (eq 0 (call-process "git" nil t nil
220 "rev-parse" "--show-cdup"))
221 (error "Cannot find top-level git tree for %s" dir))))))
222 (expand-file-name (concat (file-name-as-directory dir)
223 (car (split-string cdup "\n")))))))
225 (defun stgit-refresh-git-status (&optional dir)
226 "If it exists, refresh the `git-status' buffer belonging to
227 directory DIR or `default-directory'"
228 (when (and (fboundp 'git-find-status-buffer)
229 (fboundp 'git-refresh-status))
230 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
231 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
232 (when git-status-buffer
233 (with-current-buffer git-status-buffer
234 (git-refresh-status))))))
236 (defun stgit-find-buffer (dir)
237 "Return the buffer displaying StGit patches for DIR, or nil if none."
238 (setq dir (file-name-as-directory dir))
239 (let ((buffers (buffer-list)))
241 (not (with-current-buffer (car buffers)
242 (and (eq major-mode 'stgit-mode)
243 (string= default-directory dir)))))
244 (setq buffers (cdr buffers)))
245 (and buffers (car buffers))))
247 (defun switch-to-stgit-buffer (dir)
248 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
249 (setq dir (file-name-as-directory dir))
250 (let ((buffer (stgit-find-buffer dir)))
251 (switch-to-buffer (or buffer
252 (create-stgit-buffer dir)))))
254 (defstruct (stgit-patch)
255 status name desc empty files-ewoc)
257 (defun stgit-patch-display-name (patch)
258 (let ((name (stgit-patch-name patch)))
262 (t (symbol-name name)))))
264 (defun stgit-patch-pp (patch)
265 (let* ((status (stgit-patch-status patch))
267 (name (stgit-patch-name patch))
268 (face (cdr (assq status stgit-patch-status-face-alist)))
269 (fmt (if stgit-show-patch-names
270 stgit-patch-line-format
271 stgit-noname-patch-line-format))
272 (spec (format-spec-make
278 ?m (if (memq name stgit-marked-patches)
280 ?n (propertize (stgit-patch-display-name patch)
282 'syntax-table (string-to-syntax "w"))
283 ?e (if (stgit-patch-empty patch) "(empty) " "")
284 ?d (propertize (or (stgit-patch-desc patch) "")
285 'face 'stgit-description-face)
286 ?D (propertize (or (stgit-patch-desc patch)
287 (stgit-patch-display-name patch))
290 (insert (format-spec fmt spec) "\n")
291 (put-text-property start (point) 'entry-type 'patch)
292 (when (memq name stgit-expanded-patches)
293 (stgit-insert-patch-files patch))
294 (put-text-property start (point) 'patch-data patch)))
296 (defun create-stgit-buffer (dir)
297 "Create a buffer for showing StGit patches.
298 Argument DIR is the repository path."
299 (let ((buf (create-file-buffer (concat dir "*stgit*")))
300 (inhibit-read-only t))
301 (with-current-buffer buf
302 (setq default-directory dir)
304 (set (make-local-variable 'stgit-ewoc)
305 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
306 (setq buffer-read-only t))
309 (defmacro stgit-capture-output (name &rest body)
310 "Capture StGit output and, if there was any output, show it in a window
312 Returns nil if there was no output."
313 (declare (debug ([&or stringp null] body))
315 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
316 (stgit-dir default-directory)
317 (inhibit-read-only t))
318 (with-current-buffer output-buf
319 (buffer-disable-undo)
321 (setq default-directory stgit-dir)
322 (setq buffer-read-only t))
323 (let ((standard-output output-buf))
325 (with-current-buffer output-buf
326 (set-buffer-modified-p nil)
327 (setq buffer-read-only t)
328 (if (< (point-min) (point-max))
329 (display-buffer output-buf t)))))
331 (defun stgit-make-run-args (args)
332 "Return a copy of ARGS with its elements converted to strings."
334 ;; don't use (format "%s" ...) to limit type errors
335 (cond ((stringp x) x)
336 ((integerp x) (number-to-string x))
337 ((symbolp x) (symbol-name x))
339 (error "Bad element in stgit-make-run-args args: %S" x))))
342 (defun stgit-run-silent (&rest args)
343 (setq args (stgit-make-run-args args))
344 (apply 'call-process "stg" nil standard-output nil args))
346 (defun stgit-run (&rest args)
347 (setq args (stgit-make-run-args args))
348 (let ((msgcmd (mapconcat #'identity args " ")))
349 (message "Running stg %s..." msgcmd)
350 (apply 'call-process "stg" nil standard-output nil args)
351 (message "Running stg %s...done" msgcmd)))
353 (defun stgit-run-git (&rest args)
354 (setq args (stgit-make-run-args args))
355 (let ((msgcmd (mapconcat #'identity args " ")))
356 (message "Running git %s..." msgcmd)
357 (apply 'call-process "git" nil standard-output nil args)
358 (message "Running git %s...done" msgcmd)))
360 (defun stgit-run-git-silent (&rest args)
361 (setq args (stgit-make-run-args args))
362 (apply 'call-process "git" nil standard-output nil args))
364 (defun stgit-index-empty-p ()
365 "Returns non-nil if the index contains no changes from HEAD."
366 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
368 (defun stgit-work-tree-empty-p ()
369 "Returns non-nil if the work tree contains no changes from index."
370 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
372 (defvar stgit-index-node)
373 (defvar stgit-worktree-node)
375 (defun stgit-refresh-index ()
376 (when stgit-index-node
377 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
379 (defun stgit-refresh-worktree ()
380 (when stgit-worktree-node
381 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
383 (defun stgit-run-series-insert-index (ewoc)
384 (setq index-node (cons ewoc (ewoc-enter-last ewoc
390 worktree-node (cons ewoc (ewoc-enter-last ewoc
397 (defun stgit-run-series (ewoc)
398 (setq stgit-index-node nil
399 stgit-worktree-node nil)
400 (let ((inserted-index (not stgit-show-worktree))
405 (let* ((standard-output (current-buffer))
406 (exit-status (stgit-run-silent "series"
407 "--description" "--empty")))
408 (goto-char (point-min))
409 (if (not (zerop exit-status))
410 (cond ((looking-at "stg series: \\(.*\\)")
411 (setq inserted-index t)
412 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
413 (substitute-command-keys
414 "-- not initialized; run \\[stgit-init]")))
416 (error "Error running stg: %s"
420 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
421 (error "Syntax error in output from stg series"))
422 (let* ((state-str (match-string 2))
423 (state (cond ((string= state-str ">") 'top)
424 ((string= state-str "+") 'applied)
425 ((string= state-str "-") 'unapplied)))
426 (name (intern (match-string 4)))
427 (desc (match-string 5))
428 (empty (string= (match-string 1) "0")))
429 (unless inserted-index
430 (when (or (eq stgit-show-worktree-mode 'top)
431 (and (eq stgit-show-worktree-mode 'center)
432 (eq state 'unapplied)))
433 (setq inserted-index t)
434 (stgit-run-series-insert-index ewoc)))
435 (setq all-patchsyms (cons name all-patchsyms))
436 (ewoc-enter-last ewoc
443 (unless inserted-index
444 (stgit-run-series-insert-index ewoc)))
445 (setq stgit-index-node index-node
446 stgit-worktree-node worktree-node
447 stgit-marked-patches (intersection stgit-marked-patches
450 (defun stgit-reload ()
451 "Update the contents of the StGit buffer."
454 (let ((inhibit-read-only t)
455 (curline (line-number-at-pos))
456 (curpatch (stgit-patch-name-at-point))
457 (curfile (stgit-patched-file-at-point)))
458 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
459 (ewoc-set-hf stgit-ewoc
462 (substring (with-output-to-string
463 (stgit-run-silent "branch"))
465 'face 'stgit-branch-name-face)
467 (if stgit-show-worktree
470 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
471 shows the working tree\n")
472 'face 'stgit-description-face)))
473 (stgit-run-series stgit-ewoc)
475 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
476 (goto-line curline)))
477 (stgit-refresh-git-status))
479 (defconst stgit-file-status-code-strings
480 (mapcar (lambda (arg)
482 (propertize (cadr arg) 'face (car (cddr arg)))))
483 '((add "Added" stgit-modified-file-face)
484 (copy "Copied" stgit-modified-file-face)
485 (delete "Deleted" stgit-modified-file-face)
486 (modify "Modified" stgit-modified-file-face)
487 (rename "Renamed" stgit-modified-file-face)
488 (mode-change "Mode change" stgit-modified-file-face)
489 (unmerged "Unmerged" stgit-unmerged-file-face)
490 (unknown "Unknown" stgit-unknown-file-face)
491 (ignore "Ignored" stgit-ignored-file-face)))
492 "Alist of code symbols to description strings")
494 (defconst stgit-patch-status-face-alist
495 '((applied . stgit-applied-patch-face)
496 (top . stgit-top-patch-face)
497 (unapplied . stgit-unapplied-patch-face)
498 (index . stgit-index-work-tree-title-face)
499 (work . stgit-index-work-tree-title-face))
500 "Alist of face to use for a given patch status")
502 (defun stgit-file-status-code-as-string (file)
503 "Return stgit status code for FILE as a string"
504 (let* ((code (assq (stgit-file-status file)
505 stgit-file-status-code-strings))
506 (score (stgit-file-cr-score file)))
508 (if (and score (/= score 100))
509 (format "%s %s" (cdr code)
510 (propertize (format "%d%%" score)
511 'face 'stgit-description-face))
514 (defun stgit-file-status-code (str &optional score)
515 "Return stgit status code from git status string"
516 (let ((code (assoc str '(("A" . add)
525 (setq code (if code (cdr code) 'unknown))
526 (when (stringp score)
527 (if (> (length score) 0)
528 (setq score (string-to-number score))
530 (if score (cons code score) code)))
532 (defconst stgit-file-type-strings
535 (#o160 . "subproject"))
536 "Alist of names of file types")
538 (defun stgit-file-type-string (type)
539 "Return string describing file type TYPE (the high bits of file permission).
540 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
541 (let ((type-str (assoc type stgit-file-type-strings)))
542 (or (and type-str (cdr type-str))
543 (format "unknown type %o" type))))
545 (defun stgit-file-type-change-string (old-perm new-perm)
546 "Return string describing file type change from OLD-PERM to NEW-PERM.
547 Cf. `stgit-file-type-string'."
548 (let ((old-type (lsh old-perm -9))
549 (new-type (lsh new-perm -9)))
550 (cond ((= old-type new-type) "")
551 ((zerop new-type) "")
553 (if (= new-type #o100)
555 (format "(%s)" (stgit-file-type-string new-type))))
556 (t (format "(%s -> %s)"
557 (stgit-file-type-string old-type)
558 (stgit-file-type-string new-type))))))
560 (defun stgit-file-mode-change-string (old-perm new-perm)
561 "Return string describing file mode change from OLD-PERM to NEW-PERM.
562 Cf. `stgit-file-type-change-string'."
563 (setq old-perm (logand old-perm #o777)
564 new-perm (logand new-perm #o777))
565 (if (or (= old-perm new-perm)
569 (let* ((modified (logxor old-perm new-perm))
570 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
571 (cond ((zerop modified) "")
572 ((and (zerop not-x-modified)
573 (or (and (eq #o111 (logand old-perm #o111))
574 (propertize "-x" 'face 'stgit-file-permission-face))
575 (and (eq #o111 (logand new-perm #o111))
576 (propertize "+x" 'face
577 'stgit-file-permission-face)))))
578 (t (concat (propertize (format "%o" old-perm)
579 'face 'stgit-file-permission-face)
581 'face 'stgit-description-face)
582 (propertize (format "%o" new-perm)
583 'face 'stgit-file-permission-face)))))))
585 (defstruct (stgit-file)
586 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
588 (defun stgit-describe-copy-or-rename (file)
589 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
590 from to common-head common-tail)
592 (when stgit-abbreviate-copies-and-renames
593 (setq from (split-string (stgit-file-cr-from file) "/")
594 to (split-string (stgit-file-cr-to file) "/"))
596 (while (and from to (cdr from) (cdr to)
597 (string-equal (car from) (car to)))
598 (setq common-head (cons (car from) common-head)
601 (setq common-head (nreverse common-head)
604 (while (and from to (cdr from) (cdr to)
605 (string-equal (car from) (car to)))
606 (setq common-tail (cons (car from) common-tail)
609 (setq from (nreverse from)
612 (if (or common-head common-tail)
613 (concat (if common-head
614 (mapconcat #'identity common-head "/")
616 (if common-head "/" "")
617 (propertize "{" 'face 'stgit-description-face)
618 (mapconcat #'identity from "/")
620 (mapconcat #'identity to "/")
621 (propertize "}" 'face 'stgit-description-face)
622 (if common-tail "/" "")
624 (mapconcat #'identity common-tail "/")
626 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
628 (defun stgit-file-pp (file)
629 (let ((start (point))
630 (spec (format-spec-make
631 ?s (stgit-file-status-code-as-string file)
632 ?m (stgit-file-mode-change-string
633 (stgit-file-old-perm file)
634 (stgit-file-new-perm file))
635 ?n (if (stgit-file-copy-or-rename file)
636 (stgit-describe-copy-or-rename file)
637 (stgit-file-file file))
638 ?c (propertize (stgit-file-type-change-string
639 (stgit-file-old-perm file)
640 (stgit-file-new-perm file))
641 'face 'stgit-description-face))))
642 (insert (format-spec stgit-file-line-format spec) "\n")
643 (add-text-properties start (point)
644 (list 'entry-type 'file
647 (defun stgit-find-copies-harder-diff-arg ()
648 "Return the flag to use with `git-diff' depending on the
649 `stgit-find-copies-harder' flag."
650 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
652 (defun stgit-insert-ls-files (args file-flag)
653 (let ((start (point)))
654 (apply 'stgit-run-git
655 (append '("ls-files" "--exclude-standard" "-z") args))
657 (while (looking-at "\\([^\0]*\\)\0")
658 (let ((name-len (- (match-end 0) (match-beginning 0))))
659 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
660 (forward-char name-len)))))
662 (defun stgit-insert-patch-files (patch)
663 "Expand (show modification of) the patch PATCH after the line
665 (let* ((patchsym (stgit-patch-name patch))
667 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
668 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
669 (set-marker-insertion-type end t)
670 (setf (stgit-patch-files-ewoc patch) ewoc)
672 (let ((standard-output (current-buffer)))
673 (apply 'stgit-run-git
674 (cond ((eq patchsym :work)
675 `("diff-files" "-0" ,@args))
676 ((eq patchsym :index)
677 `("diff-index" ,@args "--cached" "HEAD"))
679 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
681 (when (and (eq patchsym :work))
682 (when stgit-show-ignored
683 (stgit-insert-ls-files '("--ignored" "--others") "I"))
684 (when stgit-show-unknown
685 (stgit-insert-ls-files '("--others") "X"))
686 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
687 (point-min) (point-max)))
689 (goto-char (point-min))
690 (unless (or (eobp) (memq patchsym '(:work :index)))
692 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
693 (let ((old-perm (string-to-number (match-string 1) 8))
694 (new-perm (string-to-number (match-string 2) 8)))
695 (goto-char (match-end 0))
698 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
699 (let* ((patch-status (stgit-patch-status patch))
700 (file-subexp (if (eq patch-status 'unapplied)
703 (file (match-string file-subexp)))
708 :cr-score (string-to-number (match-string 2))
709 :cr-from (match-string 3)
710 :cr-to (match-string 4)
711 :status (stgit-file-status-code
714 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
722 :status (stgit-file-status-code
724 :file (match-string 2))))))
725 (goto-char (match-end 0))
726 (ewoc-enter-last ewoc file))))
728 (unless (ewoc-nth ewoc 0)
731 (propertize "<no files>"
732 'face 'stgit-description-face)
736 (defun stgit-find-file (&optional other-window)
737 (let* ((file (or (stgit-patched-file-at-point)
738 (error "No file at point")))
739 (filename (expand-file-name (stgit-file-file file))))
740 (unless (file-exists-p filename)
741 (error "File does not exist"))
742 (funcall (if other-window 'find-file-other-window 'find-file)
744 (when (eq (stgit-file-status file) 'unmerged)
747 (defun stgit-expand (&optional patches collapse)
748 "Show the contents of marked patches, or the patch at point.
750 See also `stgit-collapse'.
752 Non-interactively, operate on PATCHES, and collapse instead of
753 expand if COLLAPSE is not nil."
754 (interactive (list (stgit-patches-marked-or-at-point t)))
756 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
757 patches stgit-expanded-patches)))
758 (setq stgit-expanded-patches
760 (set-difference stgit-expanded-patches patches-diff)
761 (append stgit-expanded-patches patches-diff)))
762 (ewoc-map #'(lambda (patch)
763 (memq (stgit-patch-name patch) patches-diff))
765 (move-to-column (stgit-goal-column)))
767 (defun stgit-collapse (&optional patches)
768 "Hide the contents of marked patches, or the patch at point.
770 See also `stgit-expand'."
771 (interactive (list (stgit-patches-marked-or-at-point t)))
773 (stgit-expand patches t))
775 (defun stgit-select-patch ()
776 (let ((patchname (stgit-patch-name-at-point)))
777 (stgit-expand (list patchname)
778 (memq patchname stgit-expanded-patches))))
780 (defun stgit-select ()
781 "With point on a patch, toggle showing files in the patch.
783 With point on a file, open the associated file. Opens the target
784 file for (applied) copies and renames."
787 (case (get-text-property (point) 'entry-type)
789 (stgit-select-patch))
793 (error "No patch or file on line"))))
795 (defun stgit-find-file-other-window ()
796 "Open file at point in other window"
801 (defun stgit-find-file-merge ()
802 "Open file at point and merge it using `smerge-ediff'."
809 "Hide the stgit buffer."
814 (defun stgit-git-status ()
815 "Show status using `git-status'."
818 (unless (fboundp 'git-status)
819 (error "The stgit-git-status command requires git-status"))
820 (let ((dir default-directory))
821 (save-selected-window
825 (defun stgit-goal-column ()
826 "Return goal column for the current line"
827 (case (get-text-property (point) 'entry-type)
832 (defun stgit-next-line (&optional arg)
833 "Move cursor vertically down ARG lines"
837 (move-to-column (stgit-goal-column)))
839 (defun stgit-previous-line (&optional arg)
840 "Move cursor vertically up ARG lines"
844 (move-to-column (stgit-goal-column)))
846 (defun stgit-next-patch (&optional arg)
847 "Move cursor down ARG patches."
850 (ewoc-goto-next stgit-ewoc (or arg 1))
851 (move-to-column goal-column))
853 (defun stgit-previous-patch (&optional arg)
854 "Move cursor up ARG patches."
857 (ewoc-goto-prev stgit-ewoc (or arg 1))
858 (move-to-column goal-column))
860 (defvar stgit-mode-hook nil
861 "Run after `stgit-mode' is setup.")
863 (defvar stgit-mode-map nil
864 "Keymap for StGit major mode.")
866 (unless stgit-mode-map
867 (let ((diff-map (make-sparse-keymap))
868 (toggle-map (make-sparse-keymap)))
869 (suppress-keymap diff-map)
870 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
871 '(("b" . stgit-diff-base)
872 ("c" . stgit-diff-combined)
873 ("m" . stgit-find-file-merge)
874 ("o" . stgit-diff-ours)
875 ("t" . stgit-diff-theirs)))
876 (suppress-keymap toggle-map)
877 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
878 '(("n" . stgit-toggle-patch-names)
879 ("t" . stgit-toggle-worktree)
880 ("i" . stgit-toggle-ignored)
881 ("u" . stgit-toggle-unknown)))
882 (setq stgit-mode-map (make-keymap))
883 (suppress-keymap stgit-mode-map)
884 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
885 `((" " . stgit-mark-down)
886 ("m" . stgit-mark-down)
887 ("\d" . stgit-unmark-up)
888 ("u" . stgit-unmark-down)
891 ("\C-p" . stgit-previous-line)
892 ("\C-n" . stgit-next-line)
893 ([up] . stgit-previous-line)
894 ([down] . stgit-next-line)
895 ("p" . stgit-previous-patch)
896 ("n" . stgit-next-patch)
897 ("\M-{" . stgit-previous-patch)
898 ("\M-}" . stgit-next-patch)
899 ("s" . stgit-git-status)
900 ("g" . stgit-reload-or-repair)
901 ("r" . stgit-refresh)
902 ("\C-c\C-r" . stgit-rename)
904 ("M" . stgit-move-patches)
907 ("c" . stgit-new-and-refresh)
908 ("\C-c\C-c" . stgit-commit)
909 ("\C-c\C-u" . stgit-uncommit)
911 ("R" . stgit-resolve-file)
912 ("\r" . stgit-select)
914 ("-" . stgit-collapse)
915 ("o" . stgit-find-file-other-window)
916 ("i" . stgit-toggle-index)
917 (">" . stgit-push-next)
918 ("<" . stgit-pop-next)
919 ("P" . stgit-push-or-pop)
923 ([?\C-/] . stgit-undo)
924 ("\C-_" . stgit-undo)
925 ([?\C-c ?\C-/] . stgit-redo)
926 ("\C-c\C-_" . stgit-redo)
928 ("\C-c\C-b" . stgit-rebase)
931 ("q" . stgit-quit))))
933 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
934 (and file (eq (stgit-file-status file)
936 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
937 (easy-menu-define stgit-menu stgit-mode-map
940 ["Reload" stgit-reload-or-repair
941 :help "Reload StGit status from disk"]
942 ["Repair" stgit-repair
943 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
944 :help "Repair StGit metadata"]
946 ["Undo" stgit-undo t]
947 ["Redo" stgit-redo t]
949 ["Git status" stgit-git-status :active (fboundp 'git-status)]
951 ["New patch" stgit-new-and-refresh
952 :help "Create a new patch from changes in index or work tree"
953 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
954 ["New empty patch" stgit-new
955 :help "Create a new, empty patch"]
956 ["(Un)mark patch" stgit-toggle-mark
957 :label (if (memq (stgit-patch-name-at-point nil t)
958 stgit-marked-patches)
959 "Unmark patch" "Mark patch")
960 :active (stgit-patch-name-at-point nil t)]
961 ["Expand/collapse patch"
962 (let ((patches (stgit-patches-marked-or-at-point)))
963 (if (member-if ,patch-collapsed-p patches)
964 (stgit-expand patches)
965 (stgit-collapse patches)))
966 :label (if (member-if ,patch-collapsed-p
967 (stgit-patches-marked-or-at-point))
970 :active (stgit-patches-marked-or-at-point)]
971 ["Edit patch" stgit-edit
972 :help "Edit patch comment"
973 :active (stgit-patch-name-at-point nil t)]
974 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
975 ["Push/pop patch" stgit-push-or-pop
976 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
977 (stgit-applied-patchsyms t))
978 "Pop patches" "Push patches")]
979 ["Delete patches" stgit-delete
980 :active (stgit-patches-marked-or-at-point nil t)]
982 ["Move patches" stgit-move-patches
983 :active stgit-marked-patches
984 :help "Move marked patch(es) to point"]
985 ["Squash patches" stgit-squash
986 :active (> (length stgit-marked-patches) 1)
987 :help "Merge marked patches into one"]
989 ["Refresh top patch" stgit-refresh
990 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
991 :help "Refresh the top patch with changes in index or work tree"]
992 ["Refresh this patch" (stgit-refresh t)
993 :keys "\\[universal-argument] \\[stgit-refresh]"
994 :help "Refresh marked patch with changes in index or work tree"
995 :active (and (not (and (stgit-index-empty-p)
996 (stgit-work-tree-empty-p)))
997 (stgit-patch-name-at-point nil t))]
999 ["Find file" stgit-select
1000 :active (eq (get-text-property (point) 'entry-type) 'file)]
1001 ["Open file" stgit-find-file-other-window
1002 :active (eq (get-text-property (point) 'entry-type) 'file)]
1003 ["Toggle file index" stgit-toggle-index
1004 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1005 (memq (stgit-patch-name-at-point) '(:work :index)))
1006 :label (if (eq (stgit-patch-name-at-point) :work)
1007 "Move change to index"
1008 "Move change to work tree")]
1010 ["Show diff" stgit-diff
1011 :active (get-text-property (point) 'entry-type)]
1013 :active (stgit-git-index-unmerged-p)
1014 ["Combined diff" stgit-diff-combined
1015 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1016 ["Diff against base" stgit-diff-base
1017 :help "Show diff against the common base"
1018 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1019 ["Diff against ours" stgit-diff-ours
1020 :help "Show diff against our branch"
1021 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1022 ["Diff against theirs" stgit-diff-theirs
1023 :help "Show diff against their branch"
1024 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1026 ["Interactive merge" stgit-find-file-merge
1027 :help "Interactively merge the file"
1028 :active ,at-unmerged-file]
1029 ["Resolve file" stgit-resolve-file
1030 :help "Mark file conflict as resolved"
1031 :active ,at-unmerged-file]
1034 ["Show index & work tree" stgit-toggle-worktree :style toggle
1035 :selected stgit-show-worktree]
1036 ["Show unknown files" stgit-toggle-unknown :style toggle
1037 :selected stgit-show-unknown :active stgit-show-worktree]
1038 ["Show ignored files" stgit-toggle-ignored :style toggle
1039 :selected stgit-show-ignored :active stgit-show-worktree]
1040 ["Show patch names" stgit-toggle-patch-names :style toggle
1041 :selected stgit-show-patch-names]
1043 ["Switch branches" stgit-branch t
1044 :help "Switch to another branch"]
1045 ["Rebase branch" stgit-rebase t
1046 :help "Rebase the current branch"]
1049 ;; disable tool bar editing buttons
1050 (put 'stgit-mode 'mode-class 'special)
1052 (defun stgit-mode ()
1053 "Major mode for interacting with StGit.
1055 Start StGit using \\[stgit].
1059 \\[stgit-help] Show this help text
1060 \\[stgit-quit] Hide the StGit buffer
1061 \\[describe-bindings] Show all key bindings
1063 \\[stgit-reload-or-repair] Reload the StGit buffer
1064 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1066 \\[stgit-undo] Undo most recent StGit operation
1067 \\[stgit-redo] Undo recent undo
1069 \\[stgit-git-status] Run `git-status' (if available)
1072 \\[stgit-previous-line] Move to previous line
1073 \\[stgit-next-line] Move to next line
1074 \\[stgit-previous-patch] Move to previous patch
1075 \\[stgit-next-patch] Move to next patch
1077 \\[stgit-mark-down] Mark patch and move down
1078 \\[stgit-unmark-up] Unmark patch and move up
1079 \\[stgit-unmark-down] Unmark patch and move down
1081 Commands for patches:
1082 \\[stgit-select] Toggle showing changed files in patch
1083 \\[stgit-refresh] Refresh patch with changes in index or work tree
1084 \\[stgit-diff] Show the patch log and diff
1086 \\[stgit-expand] Show changes in marked patches
1087 \\[stgit-collapse] Hide changes in marked patches
1089 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1090 \\[stgit-new] Create a new, empty patch
1092 \\[stgit-rename] Rename patch
1093 \\[stgit-edit] Edit patch description
1094 \\[stgit-delete] Delete patch(es)
1096 \\[stgit-revert] Revert all changes in index or work tree
1097 \\[stgit-toggle-index] Toggle all changes between index and work tree
1099 \\[stgit-push-next] Push next patch onto stack
1100 \\[stgit-pop-next] Pop current patch from stack
1101 \\[stgit-push-or-pop] Push or pop marked patches
1102 \\[stgit-goto] Make patch at point current by popping or pushing
1104 \\[stgit-squash] Squash (meld together) patches
1105 \\[stgit-move-patches] Move marked patches to point
1107 \\[stgit-commit] Commit patch(es)
1108 \\[stgit-uncommit] Uncommit patch(es)
1111 \\[stgit-select] Open the file in this window
1112 \\[stgit-find-file-other-window] Open the file in another window
1113 \\[stgit-diff] Show the file's diff
1115 \\[stgit-toggle-index] Toggle change between index and work tree
1116 \\[stgit-revert] Revert changes to file
1119 \\[stgit-toggle-patch-names] Toggle showing patch names
1120 \\[stgit-toggle-worktree] Toggle showing index and work tree
1121 \\[stgit-toggle-unknown] Toggle showing unknown files
1122 \\[stgit-toggle-ignored] Toggle showing ignored files
1125 \\[stgit-diff] Show diff of patch or file
1126 \\[stgit-diff-base] Show diff against the merge base
1127 \\[stgit-diff-ours] Show diff against our branch
1128 \\[stgit-diff-theirs] Show diff against their branch
1130 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1131 ignore space changes.
1132 With two prefix arguments (e.g., \\[universal-argument] \
1133 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1135 Commands for merge conflicts:
1136 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1137 \\[stgit-resolve-file] Mark unmerged file as resolved
1139 Commands for branches:
1140 \\[stgit-branch] Switch to another branch
1141 \\[stgit-rebase] Rebase the current branch
1143 Customization variables:
1144 `stgit-abbreviate-copies-and-renames'
1145 `stgit-default-show-patch-names'
1146 `stgit-default-show-worktree'
1147 `stgit-find-copies-harder'
1148 `stgit-show-worktree-mode'
1150 See also \\[customize-group] for the \"stgit\" group."
1151 (kill-all-local-variables)
1152 (buffer-disable-undo)
1153 (setq mode-name "StGit"
1154 major-mode 'stgit-mode
1156 (use-local-map stgit-mode-map)
1157 (set (make-local-variable 'list-buffers-directory) default-directory)
1158 (set (make-local-variable 'stgit-marked-patches) nil)
1159 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1160 (set (make-local-variable 'stgit-show-patch-names)
1161 stgit-default-show-patch-names)
1162 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1163 (set (make-local-variable 'stgit-index-node) nil)
1164 (set (make-local-variable 'stgit-worktree-node) nil)
1165 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1166 (set-variable 'truncate-lines 't)
1167 (add-hook 'after-save-hook 'stgit-update-saved-file)
1168 (run-hooks 'stgit-mode-hook))
1170 (defun stgit-update-saved-file ()
1171 (let* ((file (expand-file-name buffer-file-name))
1172 (dir (file-name-directory file))
1173 (gitdir (condition-case nil (git-get-top-dir dir)
1175 (buffer (and gitdir (stgit-find-buffer gitdir))))
1177 (with-current-buffer buffer
1178 (stgit-refresh-worktree)))))
1180 (defun stgit-add-mark (patchsym)
1181 "Mark the patch PATCHSYM."
1182 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1184 (defun stgit-remove-mark (patchsym)
1185 "Unmark the patch PATCHSYM."
1186 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1188 (defun stgit-clear-marks ()
1189 "Unmark all patches."
1190 (setq stgit-marked-patches '()))
1192 (defun stgit-patch-at-point (&optional cause-error)
1193 (get-text-property (point) 'patch-data))
1195 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1196 "Return the patch name on the current line as a symbol.
1197 If CAUSE-ERROR is not nil, signal an error if none found.
1198 If ONLY-PATCHES is not nil, only allow real patches, and not
1199 index or work tree."
1200 (let ((patch (stgit-patch-at-point)))
1203 (memq (stgit-patch-status patch) '(work index))
1206 (stgit-patch-name patch))
1208 (error "No patch on this line")))))
1210 (defun stgit-patched-file-at-point ()
1211 (get-text-property (point) 'file-data))
1213 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1214 "Return the symbols of the marked patches, or the patch on the current line.
1215 If CAUSE-ERRROR is not nil, signal an error if none found.
1216 If ONLY-PATCHES is not nil, do not include index or work tree."
1217 (if stgit-marked-patches
1218 stgit-marked-patches
1219 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1220 (cond (patch (list patch))
1221 (cause-error (error "No patches marked or at this line"))
1224 (defun stgit-goto-patch (patchsym &optional file)
1225 "Move point to the line containing patch PATCHSYM.
1226 If that patch cannot be found, do nothing.
1228 If the patch was found and FILE is not nil, instead move to that
1229 file's line. If FILE cannot be found, stay on the line of
1231 (let ((node (ewoc-nth stgit-ewoc 0)))
1232 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
1234 (setq node (ewoc-next stgit-ewoc node)))
1235 (when (and node file)
1236 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
1237 (file-node (ewoc-nth file-ewoc 0)))
1238 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
1239 (setq file-node (ewoc-next file-ewoc file-node)))
1241 (ewoc-goto-node file-ewoc file-node)
1242 (move-to-column (stgit-goal-column))
1245 (ewoc-goto-node stgit-ewoc node)
1246 (move-to-column goal-column))))
1248 (defun stgit-init ()
1252 (stgit-capture-output nil
1256 (defun stgit-toggle-mark ()
1257 "Toggle mark on the patch under point."
1260 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1264 (defun stgit-mark ()
1265 "Mark the patch under point."
1268 (let* ((node (ewoc-locate stgit-ewoc))
1269 (patch (ewoc-data node))
1270 (name (stgit-patch-name patch)))
1271 (when (eq name :work)
1272 (error "Cannot mark the work tree"))
1273 (when (eq name :index)
1274 (error "Cannot mark the index"))
1275 (stgit-add-mark (stgit-patch-name patch))
1276 (let ((column (current-column)))
1277 (ewoc-invalidate stgit-ewoc node)
1278 (move-to-column column))))
1280 (defun stgit-mark-down ()
1281 "Mark the patch under point and move to the next patch."
1286 (defun stgit-unmark ()
1287 "Remove mark from the patch on the current line."
1290 (let* ((node (ewoc-locate stgit-ewoc))
1291 (patch (ewoc-data node)))
1292 (stgit-remove-mark (stgit-patch-name patch))
1293 (let ((column (current-column)))
1294 (ewoc-invalidate stgit-ewoc node)
1295 (move-to-column column))))
1297 (defun stgit-unmark-up ()
1298 "Remove mark from the patch on the previous line."
1301 (stgit-previous-patch)
1304 (defun stgit-unmark-down ()
1305 "Remove mark from the patch on the current line."
1311 (defun stgit-rename (name)
1312 "Rename the patch under point to NAME."
1314 (read-string "Patch name: "
1315 (symbol-name (stgit-patch-name-at-point t t)))))
1317 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1318 (stgit-capture-output nil
1319 (stgit-run "rename" old-patchsym name))
1320 (let ((name-sym (intern name)))
1321 (when (memq old-patchsym stgit-expanded-patches)
1322 (setq stgit-expanded-patches
1323 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1324 (when (memq old-patchsym stgit-marked-patches)
1325 (setq stgit-marked-patches
1326 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1328 (stgit-goto-patch name-sym))))
1330 (defun stgit-reload-or-repair (repair)
1331 "Update the contents of the StGit buffer (`stgit-reload').
1333 With a prefix argument, repair the StGit metadata if the branch
1334 was modified with git commands (`stgit-repair')."
1341 (defun stgit-repair ()
1345 (stgit-capture-output nil
1346 (stgit-run "repair"))
1349 (defun stgit-available-branches ()
1350 "Returns a list of the available stg branches"
1351 (let ((output (with-output-to-string
1352 (stgit-run "branch" "--list")))
1355 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1356 (setq result (cons (match-string 1 output) result))
1357 (setq start (match-end 0)))
1360 (defun stgit-branch (branch)
1361 "Switch to branch BRANCH."
1362 (interactive (list (completing-read "Switch to branch: "
1363 (stgit-available-branches))))
1365 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1368 (defun stgit-available-refs (&optional omit-stgit)
1369 "Returns a list of the available git refs.
1370 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1371 (let* ((output (with-output-to-string
1372 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1373 "refs/tags" "refs/heads"
1375 (result (split-string output "\n" t)))
1377 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1378 (substring s (match-end 0))
1381 (delete-if (lambda (s)
1382 (string-match "^refs/heads/.*\\.stgit$" s))
1386 (defun stgit-rebase (new-base)
1387 "Rebase to NEW-BASE."
1388 (interactive (list (completing-read "Rebase to: "
1389 (stgit-available-refs t))))
1391 (stgit-capture-output nil (stgit-run "rebase" new-base))
1394 (defun stgit-commit (count)
1395 "Run stg commit on COUNT commits.
1396 Interactively, the prefix argument is used as COUNT.
1397 A negative COUNT will uncommit instead."
1401 (stgit-uncommit (- count))
1402 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1405 (defun stgit-uncommit (count)
1406 "Run stg uncommit on COUNT commits.
1407 Interactively, the prefix argument is used as COUNT.
1408 A negative COUNT will commit instead."
1412 (stgit-commit (- count))
1413 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1416 (defun stgit-neighbour-file ()
1417 "Return the file name of the next file after point, or the
1418 previous file if point is at the last file within a patch."
1419 (let ((old-point (point))
1421 (and (zerop (forward-line 1))
1422 (let ((f (stgit-patched-file-at-point)))
1423 (and f (setq neighbour-file (stgit-file-file f)))))
1424 (goto-char old-point)
1425 (unless neighbour-file
1426 (and (zerop (forward-line -1))
1427 (let ((f (stgit-patched-file-at-point)))
1428 (and f (setq neighbour-file (stgit-file-file f)))))
1429 (goto-char old-point))
1432 (defun stgit-revert-file ()
1433 "Revert the file at point, which must be in the index or the
1437 (let* ((patched-file (or (stgit-patched-file-at-point)
1438 (error "No file on the current line")))
1439 (patch-name (stgit-patch-name-at-point))
1440 (file-status (stgit-file-status patched-file))
1441 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1442 (stgit-file-cr-to patched-file))
1443 ((eq file-status 'add)
1444 (stgit-file-file patched-file))))
1445 (co-file (cond ((eq file-status 'rename)
1446 (stgit-file-cr-from patched-file))
1447 ((not (memq file-status '(copy add)))
1448 (stgit-file-file patched-file))))
1449 (next-file (stgit-neighbour-file)))
1451 (unless (memq patch-name '(:work :index))
1452 (error "No index or working tree file on this line"))
1454 (when (eq file-status 'ignore)
1455 (error "Cannot revert ignored files"))
1457 (when (eq file-status 'unknown)
1458 (error "Cannot revert unknown files"))
1460 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1461 (when (yes-or-no-p (format "Revert %d file%s? "
1463 (if (= nfiles 1) "" "s")))
1464 (stgit-capture-output nil
1466 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1468 (stgit-run-git "checkout" "HEAD" co-file)))
1470 (stgit-goto-patch patch-name next-file)))))
1472 (defun stgit-revert ()
1473 "Revert the change at point, which must be the index, the work
1474 tree, or a single change in either."
1477 (let ((patched-file (stgit-patched-file-at-point)))
1480 (let* ((patch-name (or (stgit-patch-name-at-point)
1481 (error "No patch or file at point")))
1482 (patch-desc (case patch-name
1485 (t (error (substitute-command-keys
1486 "Use \\[stgit-delete] to delete a patch"))))))
1487 (when (if (eq patch-name :work)
1488 (stgit-work-tree-empty-p)
1489 (stgit-index-empty-p))
1490 (error (format "There are no changes in the %s to revert"
1492 (and (eq patch-name :index)
1493 (not (stgit-work-tree-empty-p))
1494 (error "Cannot revert index as work tree contains unstaged changes"))
1496 (when (yes-or-no-p (format "Revert all changes in the %s? "
1498 (if (eq patch-name :index)
1499 (stgit-run-git-silent "reset" "--hard" "-q")
1500 (stgit-run-git-silent "checkout" "--" "."))
1501 (stgit-refresh-index)
1502 (stgit-refresh-worktree)
1503 (stgit-goto-patch patch-name))))))
1505 (defun stgit-resolve-file ()
1506 "Resolve conflict in the file at point."
1509 (let* ((patched-file (stgit-patched-file-at-point))
1510 (patch (stgit-patch-at-point))
1511 (patch-name (and patch (stgit-patch-name patch)))
1512 (status (and patched-file (stgit-file-status patched-file))))
1514 (unless (memq patch-name '(:work :index))
1515 (error "No index or working tree file on this line"))
1517 (unless (eq status 'unmerged)
1518 (error "No conflict to resolve at the current line"))
1520 (stgit-capture-output nil
1521 (stgit-move-change-to-index (stgit-file-file patched-file)))
1525 (defun stgit-push-next (npatches)
1526 "Push the first unapplied patch.
1527 With numeric prefix argument, push that many patches."
1530 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1532 (stgit-refresh-git-status))
1534 (defun stgit-pop-next (npatches)
1535 "Pop the topmost applied patch.
1536 With numeric prefix argument, pop that many patches."
1539 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1541 (stgit-refresh-git-status))
1543 (defun stgit-applied-patches (&optional only-patches)
1544 "Return a list of the applied patches.
1546 If ONLY-PATCHES is not nil, exclude index and work tree."
1547 (let ((states (if only-patches
1549 '(applied top index work)))
1551 (ewoc-map (lambda (patch) (when (memq (stgit-patch-status patch) states)
1552 (setq result (cons patch result))))
1556 (defun stgit-applied-patchsyms (&optional only-patches)
1557 "Return a list of the symbols of the applied patches.
1559 If ONLY-PATCHES is not nil, exclude index and work tree."
1560 (mapcar #'stgit-patch-name (stgit-applied-patches only-patches)))
1562 (defun stgit-push-or-pop ()
1563 "Push or pop the marked patches."
1566 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1567 (applied-syms (stgit-applied-patchsyms t))
1568 (unapplied (set-difference patchsyms applied-syms)))
1569 (stgit-capture-output nil
1571 (if unapplied "push" "pop")
1573 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1576 (defun stgit-goto ()
1577 "Go to the patch on the current line.
1579 Pops or pushes patches to make this patch topmost."
1582 (let ((patchsym (stgit-patch-name-at-point t)))
1583 (stgit-capture-output nil
1584 (stgit-run "goto" patchsym))
1587 (defun stgit-id (patchsym)
1588 "Return the git commit id for PATCHSYM.
1589 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1590 (if (keywordp patchsym)
1592 (let ((result (with-output-to-string
1593 (stgit-run-silent "id" patchsym))))
1594 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1595 (error "Cannot find commit id for %s" patchsym))
1596 (match-string 1 result))))
1598 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1599 "Show the patch on the current line.
1601 UNMERGED-STAGE is the argument to `git-diff' that that selects
1602 which stage to diff against in the case of unmerged files."
1603 (let ((space-arg (when (numberp ignore-whitespace)
1604 (cond ((> ignore-whitespace 4)
1605 "--ignore-all-space")
1606 ((> ignore-whitespace 1)
1607 "--ignore-space-change"))))
1608 (patch-name (stgit-patch-name-at-point t)))
1609 (stgit-capture-output "*StGit patch*"
1610 (case (get-text-property (point) 'entry-type)
1612 (let* ((patched-file (stgit-patched-file-at-point))
1613 (patch-id (let ((id (stgit-id patch-name)))
1614 (if (and (eq id :index)
1615 (eq (stgit-file-status patched-file)
1619 (args (append (and space-arg (list space-arg))
1620 (and (stgit-file-cr-from patched-file)
1621 (list (stgit-find-copies-harder-diff-arg)))
1622 (cond ((eq patch-id :index)
1624 ((eq patch-id :work)
1625 (list unmerged-stage))
1627 (list (concat patch-id "^") patch-id)))
1629 (if (stgit-file-copy-or-rename patched-file)
1630 (list (stgit-file-cr-from patched-file)
1631 (stgit-file-cr-to patched-file))
1632 (list (stgit-file-file patched-file))))))
1633 (apply 'stgit-run-git "diff" args)))
1635 (let* ((patch-id (stgit-id patch-name)))
1636 (if (or (eq patch-id :index) (eq patch-id :work))
1637 (apply 'stgit-run-git "diff"
1638 (stgit-find-copies-harder-diff-arg)
1639 (append (and space-arg (list space-arg))
1640 (if (eq patch-id :index)
1642 (list unmerged-stage))))
1643 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1644 (and space-arg (list "-O" space-arg))
1645 (list (stgit-patch-name-at-point)))))
1646 (apply 'stgit-run args)))))
1648 (error "No patch or file at point")))
1649 (with-current-buffer standard-output
1650 (goto-char (point-min))
1653 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1654 `(defun ,name (&optional ignore-whitespace)
1655 ,(format "Show the patch on the current line.
1657 %sWith a prefix argument, ignore whitespace. With a prefix argument
1658 greater than four (e.g., \\[universal-argument] \
1659 \\[universal-argument] \\[%s]), ignore all whitespace."
1661 (format "For unmerged files, %s.\n\n" unmerged-action)
1666 (stgit-show-patch ,diff-arg ignore-whitespace)))
1668 (stgit-define-diff stgit-diff
1670 (stgit-define-diff stgit-diff-ours
1672 "diff against our branch")
1673 (stgit-define-diff stgit-diff-theirs
1675 "diff against their branch")
1676 (stgit-define-diff stgit-diff-base
1678 "diff against the merge base")
1679 (stgit-define-diff stgit-diff-combined
1681 "show a combined diff")
1683 (defun stgit-move-change-to-index (file &optional force)
1684 "Copies the work tree state of FILE to index, using git add or git rm.
1686 If FORCE is not nil, use --force."
1687 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1688 '("add") '("rm" "-q"))))
1689 (stgit-capture-output "*git output*"
1690 (apply 'stgit-run-git (append op (and force '("--force"))
1691 '("--") (list file))))))
1693 (defun stgit-remove-change-from-index (file)
1694 "Unstages the change in FILE from the index"
1695 (stgit-capture-output "*git output*"
1696 (stgit-run-git "reset" "-q" "--" file)))
1698 (defun stgit-git-index-unmerged-p ()
1700 (with-output-to-string
1701 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1703 "--quiet" "HEAD")))))
1706 (defun stgit-file-toggle-index ()
1707 "Move modified file in or out of the index.
1709 Leaves the point where it is, but moves the mark to where the
1710 file ended up. You can then jump to the file with \
1711 \\[exchange-point-and-mark]."
1714 (let* ((patched-file (or (stgit-patched-file-at-point)
1715 (error "No file on the current line")))
1716 (patched-status (stgit-file-status patched-file)))
1717 (when (eq patched-status 'unmerged)
1718 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1719 (let* ((patch (stgit-patch-at-point))
1720 (patch-name (stgit-patch-name patch))
1721 (mark-file (if (eq patched-status 'rename)
1722 (stgit-file-cr-to patched-file)
1723 (stgit-file-file patched-file)))
1724 (point-file (if (eq patched-status 'rename)
1725 (stgit-file-cr-from patched-file)
1726 (stgit-neighbour-file))))
1728 (cond ((eq patch-name :work)
1729 (stgit-move-change-to-index (stgit-file-file patched-file)
1730 (eq patched-status 'ignore)))
1731 ((eq patch-name :index)
1732 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1734 (error "Can only move files between working tree and index")))
1735 (stgit-refresh-worktree)
1736 (stgit-refresh-index)
1737 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1739 (stgit-goto-patch patch-name point-file))))
1741 (defun stgit-toggle-index ()
1742 "Move change in or out of the index.
1744 Works on index and work tree, as well as files in either.
1746 Leaves the point where it is, but moves the mark to where the
1747 file ended up. You can then jump to the file with \
1748 \\[exchange-point-and-mark]."
1751 (if (stgit-patched-file-at-point)
1752 (stgit-file-toggle-index)
1753 (let ((patch-name (stgit-patch-name-at-point)))
1754 (unless (memq patch-name '(:index :work))
1755 (error "Can only move changes between working tree and index"))
1756 (when (stgit-git-index-unmerged-p)
1757 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1758 (if (if (eq patch-name :index)
1759 (stgit-index-empty-p)
1760 (stgit-work-tree-empty-p))
1761 (message "No changes to be moved")
1762 (stgit-capture-output nil
1763 (if (eq patch-name :work)
1764 (stgit-run-git "add" "--update")
1765 (stgit-run-git "reset" "--mixed" "-q")))
1766 (stgit-refresh-worktree)
1767 (stgit-refresh-index))
1768 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1770 (defun stgit-edit ()
1771 "Edit the patch on the current line."
1774 (let ((patchsym (stgit-patch-name-at-point t t))
1775 (edit-buf (get-buffer-create "*StGit edit*"))
1776 (dir default-directory))
1777 (log-edit 'stgit-confirm-edit t nil edit-buf)
1778 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1779 (setq default-directory dir)
1780 (let ((standard-output edit-buf))
1782 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1784 (defun stgit-confirm-edit ()
1786 (let ((file (make-temp-file "stgit-edit-")))
1787 (write-region (point-min) (point-max) file)
1788 (stgit-capture-output nil
1789 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1790 (with-current-buffer log-edit-parent-buffer
1793 (defun stgit-new (add-sign &optional refresh)
1794 "Create a new patch.
1795 With a prefix argument, include a \"Signed-off-by:\" line at the
1799 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1800 (dir default-directory))
1801 (log-edit 'stgit-confirm-new t nil edit-buf)
1802 (setq default-directory dir)
1803 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1806 (let ((standard-output (current-buffer)))
1807 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1809 (defun stgit-confirm-new ()
1811 (let ((file (make-temp-file "stgit-edit-"))
1812 (refresh stgit-refresh-after-new))
1813 (write-region (point-min) (point-max) file)
1814 (stgit-capture-output nil
1815 (stgit-run "new" "-f" file))
1816 (with-current-buffer log-edit-parent-buffer
1821 (defun stgit-new-and-refresh (add-sign)
1822 "Create a new patch and refresh it with the current changes.
1824 With a prefix argument, include a \"Signed-off-by:\" line at the
1827 This works just like running `stgit-new' followed by `stgit-refresh'."
1830 (stgit-new add-sign t))
1832 (defun stgit-create-patch-name (description)
1833 "Create a patch name from a long description"
1835 (while (> (length description) 0)
1836 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1837 (setq patch (downcase (concat patch
1838 (match-string 0 description))))
1839 (setq description (substring description (match-end 0))))
1840 ((string-match "\\` +" description)
1841 (setq patch (concat patch "-"))
1842 (setq description (substring description (match-end 0))))
1843 ((string-match "\\`[^a-zA-Z_-]+" description)
1844 (setq description (substring description (match-end 0))))))
1845 (cond ((= (length patch) 0)
1847 ((> (length patch) 20)
1848 (substring patch 0 20))
1851 (defun stgit-delete (patchsyms &optional spill-p)
1852 "Delete the patches in PATCHSYMS.
1853 Interactively, delete the marked patches, or the patch at point.
1855 With a prefix argument, or SPILL-P, spill the patch contents to
1856 the work tree and index."
1857 (interactive (list (stgit-patches-marked-or-at-point t t)
1858 current-prefix-arg))
1861 (error "No patches to delete"))
1862 (when (memq :index patchsyms)
1863 (error "Cannot delete the index"))
1864 (when (memq :work patchsyms)
1865 (error "Cannot delete the work tree"))
1867 (let ((npatches (length patchsyms)))
1868 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1870 (if (= 1 npatches) "" "es")
1872 " (spilling contents to index)"
1874 (let ((args (if spill-p
1875 (cons "--spill" patchsyms)
1877 (stgit-capture-output nil
1878 (apply 'stgit-run "delete" args))
1881 (defun stgit-move-patches-target ()
1882 "Return the patchsym indicating a target patch for
1883 `stgit-move-patches'.
1885 This is either the first unmarked patch at or after point, or one
1886 of :top and :bottom if the point is after or before the applied
1892 (let ((patchsym (stgit-patch-name-at-point)))
1893 (cond ((memq patchsym '(:work :index)) (setq result :top))
1894 (patchsym (if (memq patchsym stgit-marked-patches)
1896 (setq result patchsym)))
1897 ((re-search-backward "^>" nil t) (setq result :top))
1898 (t (setq result :bottom)))))
1901 (defun stgit-sort-patches (patchsyms)
1902 "Returns the list of patches in PATCHSYMS sorted according to
1903 their position in the patch series, bottommost first.
1905 PATCHSYMS must not contain duplicate entries."
1906 (let (sorted-patchsyms
1907 (series (with-output-to-string
1908 (with-current-buffer standard-output
1909 (stgit-run-silent "series" "--noprefix"))))
1911 (while (string-match "^\\(.+\\)" series start)
1912 (let ((patchsym (intern (match-string 1 series))))
1913 (when (memq patchsym patchsyms)
1914 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1915 (setq start (match-end 0)))
1916 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1918 (unless (= (length patchsyms) (length sorted-patchsyms))
1919 (error "Internal error"))
1923 (defun stgit-move-patches (patchsyms target-patch)
1924 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1925 If TARGET-PATCH is :bottom or :top, move the patches to the
1926 bottom or top of the stack, respectively.
1928 Interactively, move the marked patches to where the point is."
1929 (interactive (list stgit-marked-patches
1930 (stgit-move-patches-target)))
1933 (error "Need at least one patch to move"))
1935 (unless target-patch
1936 (error "Point not at a patch"))
1938 ;; need to have patchsyms sorted by position in the stack
1939 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1940 (stgit-capture-output nil
1941 (if (eq target-patch :top)
1942 (apply 'stgit-run "float" sorted-patchsyms)
1945 (append (unless (eq target-patch :bottom)
1946 (list "--to" target-patch))
1948 sorted-patchsyms)))))
1951 (defun stgit-squash (patchsyms)
1952 "Squash the patches in PATCHSYMS.
1953 Interactively, squash the marked patches.
1955 Unless there are any conflicts, the patches will be merged into
1956 one patch, which will occupy the same spot in the series as the
1957 deepest patch had before the squash."
1958 (interactive (list stgit-marked-patches))
1960 (when (< (length patchsyms) 2)
1961 (error "Need at least two patches to squash"))
1962 (let ((stgit-buffer (current-buffer))
1963 (edit-buf (get-buffer-create "*StGit edit*"))
1964 (dir default-directory)
1965 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1966 (log-edit 'stgit-confirm-squash t nil edit-buf)
1967 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1968 (setq default-directory dir)
1969 (let ((result (let ((standard-output edit-buf))
1971 (apply 'stgit-run-silent "squash"
1972 "--save-template=-" sorted-patchsyms)))))
1974 ;; stg squash may have reordered the patches or caused conflicts
1975 (with-current-buffer stgit-buffer
1978 (unless (eq 0 result)
1980 (rename-buffer "*StGit error*")
1981 (resize-temp-buffer-window)
1982 (switch-to-buffer-other-window stgit-buffer)
1983 (error "stg squash failed")))))
1985 (defun stgit-confirm-squash ()
1987 (let ((file (make-temp-file "stgit-edit-")))
1988 (write-region (point-min) (point-max) file)
1989 (stgit-capture-output nil
1990 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1991 (with-current-buffer log-edit-parent-buffer
1993 ;; Go to first marked patch and stay there
1994 (goto-char (point-min))
1995 (re-search-forward (concat "^[>+-]\\*") nil t)
1996 (move-to-column goal-column)
1997 (let ((pos (point)))
2001 (defun stgit-help ()
2002 "Display help for the StGit mode."
2004 (describe-function 'stgit-mode))
2006 (defun stgit-undo (&optional arg)
2008 With prefix argument, run it with the --hard flag.
2010 See also `stgit-redo'."
2013 (stgit-capture-output nil
2015 (stgit-run "undo" "--hard")
2016 (stgit-run "undo")))
2019 (defun stgit-redo (&optional arg)
2021 With prefix argument, run it with the --hard flag.
2023 See also `stgit-undo'."
2026 (stgit-capture-output nil
2028 (stgit-run "redo" "--hard")
2029 (stgit-run "redo")))
2032 (defun stgit-refresh (&optional arg)
2034 If the index contains any changes, only refresh from index.
2036 With prefix argument, refresh the marked patch or the patch under point."
2039 (let ((patchargs (if arg
2040 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2041 (when (> (length patches) 1)
2042 (error "Too many patches marked"))
2043 (cons "-p" patches))
2045 (unless (stgit-index-empty-p)
2046 (setq patchargs (cons "--index" patchargs)))
2047 (stgit-capture-output nil
2048 (apply 'stgit-run "refresh" patchargs))
2049 (stgit-refresh-git-status))
2052 (defvar stgit-show-worktree nil
2053 "If nil, inhibit showing work tree and index in the stgit buffer.
2055 See also `stgit-show-worktree-mode'.")
2057 (defvar stgit-show-ignored nil
2058 "If nil, inhibit showing files ignored by git.")
2060 (defvar stgit-show-unknown nil
2061 "If nil, inhibit showing files not registered with git.")
2063 (defvar stgit-show-patch-names t
2064 "If nil, inhibit showing patch names.")
2066 (defun stgit-toggle-worktree (&optional arg)
2067 "Toggle the visibility of the work tree.
2068 With ARG, show the work tree if ARG is positive.
2070 Its initial setting is controlled by `stgit-default-show-worktree'.
2072 `stgit-show-worktree-mode' controls where on screen the index and
2073 work tree will show up."
2076 (setq stgit-show-worktree
2079 (not stgit-show-worktree)))
2082 (defun stgit-toggle-ignored (&optional arg)
2083 "Toggle the visibility of files ignored by git in the work
2084 tree. With ARG, show these files if ARG is positive.
2086 Use \\[stgit-toggle-worktree] to show the work tree."
2089 (setq stgit-show-ignored
2092 (not stgit-show-ignored)))
2095 (defun stgit-toggle-unknown (&optional arg)
2096 "Toggle the visibility of files not registered with git in the
2097 work tree. With ARG, show these files if ARG is positive.
2099 Use \\[stgit-toggle-worktree] to show the work tree."
2102 (setq stgit-show-unknown
2105 (not stgit-show-unknown)))
2108 (defun stgit-toggle-patch-names (&optional arg)
2109 "Toggle the visibility of patch names. With ARG, show patch names
2112 The initial setting is controlled by `stgit-default-show-patch-names'."
2115 (setq stgit-show-patch-names
2118 (not stgit-show-patch-names)))