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."
91 :set 'stgit-set-default)
93 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
94 "The format string used to format file lines.
95 The format string is passed to `format-spec' and the following
96 format characters are recognized:
98 %s - A string describing the status of the file.
100 %m - Mode change information
104 %c - A description of file changes."
107 :set 'stgit-set-default)
109 (defface stgit-branch-name-face
111 "The face used for the StGit branch name"
114 (defface stgit-top-patch-face
115 '((((background dark)) (:weight bold :foreground "yellow"))
116 (((background light)) (:weight bold :foreground "purple"))
118 "The face used for the top patch names"
121 (defface stgit-applied-patch-face
122 '((((background dark)) (:foreground "light yellow"))
123 (((background light)) (:foreground "purple"))
125 "The face used for applied patch names"
128 (defface stgit-unapplied-patch-face
129 '((((background dark)) (:foreground "gray80"))
130 (((background light)) (:foreground "orchid"))
132 "The face used for unapplied patch names"
135 (defface stgit-description-face
136 '((((background dark)) (:foreground "tan"))
137 (((background light)) (:foreground "dark red")))
138 "The face used for StGit descriptions"
141 (defface stgit-index-work-tree-title-face
142 '((((supports :slant italic)) :slant italic)
144 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
147 (defface stgit-unmerged-file-face
148 '((((class color) (background light)) (:foreground "red" :bold t))
149 (((class color) (background dark)) (:foreground "red" :bold t)))
150 "StGit mode face used for unmerged file status"
153 (defface stgit-unknown-file-face
154 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
155 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
156 "StGit mode face used for unknown file status"
159 (defface stgit-ignored-file-face
160 '((((class color) (background light)) (:foreground "grey60"))
161 (((class color) (background dark)) (:foreground "grey40")))
162 "StGit mode face used for ignored files")
164 (defface stgit-file-permission-face
165 '((((class color) (background light)) (:foreground "green" :bold t))
166 (((class color) (background dark)) (:foreground "green" :bold t)))
167 "StGit mode face used for permission changes."
170 (defface stgit-modified-file-face
171 '((((class color) (background light)) (:foreground "purple"))
172 (((class color) (background dark)) (:foreground "salmon")))
173 "StGit mode face used for modified file status"
177 "Manage StGit patches for the tree in DIR.
179 See `stgit-mode' for commands available."
180 (interactive "DDirectory: \n")
181 (switch-to-stgit-buffer (git-get-top-dir dir))
184 (defun stgit-assert-mode ()
185 "Signal an error if not in an StGit buffer."
186 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
188 (unless (fboundp 'git-get-top-dir)
189 (defun git-get-top-dir (dir)
190 "Retrieve the top-level directory of a git tree."
191 (let ((cdup (with-output-to-string
192 (with-current-buffer standard-output
194 (unless (eq 0 (call-process "git" nil t nil
195 "rev-parse" "--show-cdup"))
196 (error "Cannot find top-level git tree for %s" dir))))))
197 (expand-file-name (concat (file-name-as-directory dir)
198 (car (split-string cdup "\n")))))))
200 (defun stgit-refresh-git-status (&optional dir)
201 "If it exists, refresh the `git-status' buffer belonging to
202 directory DIR or `default-directory'"
203 (when (and (fboundp 'git-find-status-buffer)
204 (fboundp 'git-refresh-status))
205 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
206 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
207 (when git-status-buffer
208 (with-current-buffer git-status-buffer
209 (git-refresh-status))))))
211 (defun stgit-find-buffer (dir)
212 "Return the buffer displaying StGit patches for DIR, or nil if none."
213 (setq dir (file-name-as-directory dir))
214 (let ((buffers (buffer-list)))
216 (not (with-current-buffer (car buffers)
217 (and (eq major-mode 'stgit-mode)
218 (string= default-directory dir)))))
219 (setq buffers (cdr buffers)))
220 (and buffers (car buffers))))
222 (defun switch-to-stgit-buffer (dir)
223 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
224 (setq dir (file-name-as-directory dir))
225 (let ((buffer (stgit-find-buffer dir)))
226 (switch-to-buffer (or buffer
227 (create-stgit-buffer dir)))))
229 (defstruct (stgit-patch)
230 status name desc empty files-ewoc)
232 (defun stgit-patch-display-name (patch)
233 (let ((name (stgit-patch-name patch)))
237 (t (symbol-name name)))))
239 (defun stgit-patch-pp (patch)
240 (let* ((status (stgit-patch-status patch))
242 (name (stgit-patch-name patch))
243 (face (cdr (assq status stgit-patch-status-face-alist)))
244 (spec (format-spec-make
250 ?m (if (memq name stgit-marked-patches)
252 ?n (propertize (stgit-patch-display-name patch)
254 'syntax-table (string-to-syntax "w"))
255 ?e (if (stgit-patch-empty patch) "(empty) " "")
256 ?d (propertize (or (stgit-patch-desc patch) "")
257 'face 'stgit-description-face))))
259 (insert (format-spec stgit-patch-line-format spec) "\n")
260 (put-text-property start (point) 'entry-type 'patch)
261 (when (memq name stgit-expanded-patches)
262 (stgit-insert-patch-files patch))
263 (put-text-property start (point) 'patch-data patch)))
265 (defun create-stgit-buffer (dir)
266 "Create a buffer for showing StGit patches.
267 Argument DIR is the repository path."
268 (let ((buf (create-file-buffer (concat dir "*stgit*")))
269 (inhibit-read-only t))
270 (with-current-buffer buf
271 (setq default-directory dir)
273 (set (make-local-variable 'stgit-ewoc)
274 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
275 (setq buffer-read-only t))
278 (defmacro stgit-capture-output (name &rest body)
279 "Capture StGit output and, if there was any output, show it in a window
281 Returns nil if there was no output."
282 (declare (debug ([&or stringp null] body))
284 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
285 (stgit-dir default-directory)
286 (inhibit-read-only t))
287 (with-current-buffer output-buf
289 (setq default-directory stgit-dir)
290 (setq buffer-read-only t))
291 (let ((standard-output output-buf))
293 (with-current-buffer output-buf
294 (set-buffer-modified-p nil)
295 (setq buffer-read-only t)
296 (if (< (point-min) (point-max))
297 (display-buffer output-buf t)))))
299 (defun stgit-make-run-args (args)
300 "Return a copy of ARGS with its elements converted to strings."
302 ;; don't use (format "%s" ...) to limit type errors
303 (cond ((stringp x) x)
304 ((integerp x) (number-to-string x))
305 ((symbolp x) (symbol-name x))
307 (error "Bad element in stgit-make-run-args args: %S" x))))
310 (defun stgit-run-silent (&rest args)
311 (setq args (stgit-make-run-args args))
312 (apply 'call-process "stg" nil standard-output nil args))
314 (defun stgit-run (&rest args)
315 (setq args (stgit-make-run-args args))
316 (let ((msgcmd (mapconcat #'identity args " ")))
317 (message "Running stg %s..." msgcmd)
318 (apply 'call-process "stg" nil standard-output nil args)
319 (message "Running stg %s...done" msgcmd)))
321 (defun stgit-run-git (&rest args)
322 (setq args (stgit-make-run-args args))
323 (let ((msgcmd (mapconcat #'identity args " ")))
324 (message "Running git %s..." msgcmd)
325 (apply 'call-process "git" nil standard-output nil args)
326 (message "Running git %s...done" msgcmd)))
328 (defun stgit-run-git-silent (&rest args)
329 (setq args (stgit-make-run-args args))
330 (apply 'call-process "git" nil standard-output nil args))
332 (defun stgit-index-empty-p ()
333 "Returns non-nil if the index contains no changes from HEAD."
334 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
336 (defun stgit-work-tree-empty-p ()
337 "Returns non-nil if the work tree contains no changes from index."
338 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
340 (defvar stgit-index-node)
341 (defvar stgit-worktree-node)
343 (defun stgit-refresh-index ()
344 (when stgit-index-node
345 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
347 (defun stgit-refresh-worktree ()
348 (when stgit-worktree-node
349 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
351 (defun stgit-run-series-insert-index (ewoc)
352 (setq index-node (cons ewoc (ewoc-enter-last ewoc
358 worktree-node (cons ewoc (ewoc-enter-last ewoc
365 (defun stgit-run-series (ewoc)
366 (setq stgit-index-node nil
367 stgit-worktree-node nil)
368 (let ((inserted-index (not stgit-show-worktree))
373 (let* ((standard-output (current-buffer))
374 (exit-status (stgit-run-silent "series"
375 "--description" "--empty")))
376 (goto-char (point-min))
377 (if (not (zerop exit-status))
378 (cond ((looking-at "stg series: \\(.*\\)")
379 (setq inserted-index t)
380 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
381 (substitute-command-keys
382 "-- not initialized; run \\[stgit-init]")))
384 (error "Error running stg: %s"
388 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
389 (error "Syntax error in output from stg series"))
390 (let* ((state-str (match-string 2))
391 (state (cond ((string= state-str ">") 'top)
392 ((string= state-str "+") 'applied)
393 ((string= state-str "-") 'unapplied)))
394 (name (intern (match-string 4)))
395 (desc (match-string 5))
396 (empty (string= (match-string 1) "0")))
397 (unless inserted-index
398 (when (or (eq stgit-show-worktree-mode 'top)
399 (and (eq stgit-show-worktree-mode 'center)
400 (eq state 'unapplied)))
401 (setq inserted-index t)
402 (stgit-run-series-insert-index ewoc)))
403 (setq all-patchsyms (cons name all-patchsyms))
404 (ewoc-enter-last ewoc
411 (unless inserted-index
412 (stgit-run-series-insert-index ewoc)))
413 (setq stgit-index-node index-node
414 stgit-worktree-node worktree-node
415 stgit-marked-patches (intersection stgit-marked-patches
418 (defun stgit-reload ()
419 "Update the contents of the StGit buffer."
422 (let ((inhibit-read-only t)
423 (curline (line-number-at-pos))
424 (curpatch (stgit-patch-name-at-point))
425 (curfile (stgit-patched-file-at-point)))
426 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
427 (ewoc-set-hf stgit-ewoc
430 (substring (with-output-to-string
431 (stgit-run-silent "branch"))
433 'face 'stgit-branch-name-face)
435 (if stgit-show-worktree
438 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
439 shows the working tree\n")
440 'face 'stgit-description-face)))
441 (stgit-run-series stgit-ewoc)
443 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
444 (goto-line curline)))
445 (stgit-refresh-git-status))
447 (defconst stgit-file-status-code-strings
448 (mapcar (lambda (arg)
450 (propertize (cadr arg) 'face (car (cddr arg)))))
451 '((add "Added" stgit-modified-file-face)
452 (copy "Copied" stgit-modified-file-face)
453 (delete "Deleted" stgit-modified-file-face)
454 (modify "Modified" stgit-modified-file-face)
455 (rename "Renamed" stgit-modified-file-face)
456 (mode-change "Mode change" stgit-modified-file-face)
457 (unmerged "Unmerged" stgit-unmerged-file-face)
458 (unknown "Unknown" stgit-unknown-file-face)
459 (ignore "Ignored" stgit-ignored-file-face)))
460 "Alist of code symbols to description strings")
462 (defconst stgit-patch-status-face-alist
463 '((applied . stgit-applied-patch-face)
464 (top . stgit-top-patch-face)
465 (unapplied . stgit-unapplied-patch-face)
466 (index . stgit-index-work-tree-title-face)
467 (work . stgit-index-work-tree-title-face))
468 "Alist of face to use for a given patch status")
470 (defun stgit-file-status-code-as-string (file)
471 "Return stgit status code for FILE as a string"
472 (let* ((code (assq (stgit-file-status file)
473 stgit-file-status-code-strings))
474 (score (stgit-file-cr-score file)))
476 (if (and score (/= score 100))
477 (format "%s %s" (cdr code)
478 (propertize (format "%d%%" score)
479 'face 'stgit-description-face))
482 (defun stgit-file-status-code (str &optional score)
483 "Return stgit status code from git status string"
484 (let ((code (assoc str '(("A" . add)
493 (setq code (if code (cdr code) 'unknown))
494 (when (stringp score)
495 (if (> (length score) 0)
496 (setq score (string-to-number score))
498 (if score (cons code score) code)))
500 (defconst stgit-file-type-strings
503 (#o160 . "subproject"))
504 "Alist of names of file types")
506 (defun stgit-file-type-string (type)
507 "Return string describing file type TYPE (the high bits of file permission).
508 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
509 (let ((type-str (assoc type stgit-file-type-strings)))
510 (or (and type-str (cdr type-str))
511 (format "unknown type %o" type))))
513 (defun stgit-file-type-change-string (old-perm new-perm)
514 "Return string describing file type change from OLD-PERM to NEW-PERM.
515 Cf. `stgit-file-type-string'."
516 (let ((old-type (lsh old-perm -9))
517 (new-type (lsh new-perm -9)))
518 (cond ((= old-type new-type) "")
519 ((zerop new-type) "")
521 (if (= new-type #o100)
523 (format "(%s)" (stgit-file-type-string new-type))))
524 (t (format "(%s -> %s)"
525 (stgit-file-type-string old-type)
526 (stgit-file-type-string new-type))))))
528 (defun stgit-file-mode-change-string (old-perm new-perm)
529 "Return string describing file mode change from OLD-PERM to NEW-PERM.
530 Cf. `stgit-file-type-change-string'."
531 (setq old-perm (logand old-perm #o777)
532 new-perm (logand new-perm #o777))
533 (if (or (= old-perm new-perm)
537 (let* ((modified (logxor old-perm new-perm))
538 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
539 (cond ((zerop modified) "")
540 ((and (zerop not-x-modified)
541 (or (and (eq #o111 (logand old-perm #o111))
542 (propertize "-x" 'face 'stgit-file-permission-face))
543 (and (eq #o111 (logand new-perm #o111))
544 (propertize "+x" 'face
545 'stgit-file-permission-face)))))
546 (t (concat (propertize (format "%o" old-perm)
547 'face 'stgit-file-permission-face)
549 'face 'stgit-description-face)
550 (propertize (format "%o" new-perm)
551 'face 'stgit-file-permission-face)))))))
553 (defstruct (stgit-file)
554 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
556 (defun stgit-describe-copy-or-rename (file)
557 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
558 from to common-head common-tail)
560 (when stgit-abbreviate-copies-and-renames
561 (setq from (split-string (stgit-file-cr-from file) "/")
562 to (split-string (stgit-file-cr-to file) "/"))
564 (while (and from to (cdr from) (cdr to)
565 (string-equal (car from) (car to)))
566 (setq common-head (cons (car from) common-head)
569 (setq common-head (nreverse common-head)
572 (while (and from to (cdr from) (cdr to)
573 (string-equal (car from) (car to)))
574 (setq common-tail (cons (car from) common-tail)
577 (setq from (nreverse from)
580 (if (or common-head common-tail)
581 (concat (if common-head
582 (mapconcat #'identity common-head "/")
584 (if common-head "/" "")
585 (propertize "{" 'face 'stgit-description-face)
586 (mapconcat #'identity from "/")
588 (mapconcat #'identity to "/")
589 (propertize "}" 'face 'stgit-description-face)
590 (if common-tail "/" "")
592 (mapconcat #'identity common-tail "/")
594 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
596 (defun stgit-file-pp (file)
597 (let ((start (point))
598 (spec (format-spec-make
599 ?s (stgit-file-status-code-as-string file)
600 ?m (stgit-file-mode-change-string
601 (stgit-file-old-perm file)
602 (stgit-file-new-perm file))
603 ?n (if (stgit-file-copy-or-rename file)
604 (stgit-describe-copy-or-rename file)
605 (stgit-file-file file))
606 ?c (propertize (stgit-file-type-change-string
607 (stgit-file-old-perm file)
608 (stgit-file-new-perm file))
609 'face 'stgit-description-face))))
610 (insert (format-spec stgit-file-line-format spec) "\n")
611 (add-text-properties start (point)
612 (list 'entry-type 'file
615 (defun stgit-find-copies-harder-diff-arg ()
616 "Return the flag to use with `git-diff' depending on the
617 `stgit-find-copies-harder' flag."
618 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
620 (defun stgit-insert-ls-files (args file-flag)
621 (let ((start (point)))
622 (apply 'stgit-run-git
623 (append '("ls-files" "--exclude-standard" "-z") args))
625 (while (looking-at "\\([^\0]*\\)\0")
626 (let ((name-len (- (match-end 0) (match-beginning 0))))
627 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
628 (forward-char name-len)))))
630 (defun stgit-insert-patch-files (patch)
631 "Expand (show modification of) the patch PATCH after the line
633 (let* ((patchsym (stgit-patch-name patch))
635 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
636 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
637 (set-marker-insertion-type end t)
638 (setf (stgit-patch-files-ewoc patch) ewoc)
640 (let ((standard-output (current-buffer)))
641 (apply 'stgit-run-git
642 (cond ((eq patchsym :work)
643 `("diff-files" "-0" ,@args))
644 ((eq patchsym :index)
645 `("diff-index" ,@args "--cached" "HEAD"))
647 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
649 (when (and (eq patchsym :work))
650 (when stgit-show-ignored
651 (stgit-insert-ls-files '("--ignored" "--others") "I"))
652 (when stgit-show-unknown
653 (stgit-insert-ls-files '("--others") "X"))
654 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
655 (point-min) (point-max)))
657 (goto-char (point-min))
658 (unless (or (eobp) (memq patchsym '(:work :index)))
660 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
661 (let ((old-perm (string-to-number (match-string 1) 8))
662 (new-perm (string-to-number (match-string 2) 8)))
663 (goto-char (match-end 0))
666 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
667 (let* ((patch-status (stgit-patch-status patch))
668 (file-subexp (if (eq patch-status 'unapplied)
671 (file (match-string file-subexp)))
676 :cr-score (string-to-number (match-string 2))
677 :cr-from (match-string 3)
678 :cr-to (match-string 4)
679 :status (stgit-file-status-code
682 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
690 :status (stgit-file-status-code
692 :file (match-string 2))))))
693 (goto-char (match-end 0))
694 (ewoc-enter-last ewoc file))))
696 (unless (ewoc-nth ewoc 0)
699 (propertize "<no files>"
700 'face 'stgit-description-face)
704 (defun stgit-find-file (&optional other-window)
705 (let* ((file (or (stgit-patched-file-at-point)
706 (error "No file at point")))
707 (filename (expand-file-name (stgit-file-file file))))
708 (unless (file-exists-p filename)
709 (error "File does not exist"))
710 (funcall (if other-window 'find-file-other-window 'find-file)
712 (when (eq (stgit-file-status file) 'unmerged)
715 (defun stgit-expand (&optional patches collapse)
716 "Show the contents of marked patches, or the patch at point.
718 See also `stgit-collapse'.
720 Non-interactively, operate on PATCHES, and collapse instead of
721 expand if COLLAPSE is not nil."
722 (interactive (list (stgit-patches-marked-or-at-point t)))
724 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
725 patches stgit-expanded-patches)))
726 (setq stgit-expanded-patches
728 (set-difference stgit-expanded-patches patches-diff)
729 (append stgit-expanded-patches patches-diff)))
730 (ewoc-map #'(lambda (patch)
731 (memq (stgit-patch-name patch) patches-diff))
733 (move-to-column (stgit-goal-column)))
735 (defun stgit-collapse (&optional patches)
736 "Hide the contents of marked patches, or the patch at point.
738 See also `stgit-expand'."
739 (interactive (list (stgit-patches-marked-or-at-point t)))
741 (stgit-expand patches t))
743 (defun stgit-select-patch ()
744 (let ((patchname (stgit-patch-name-at-point)))
745 (stgit-expand (list patchname)
746 (memq patchname stgit-expanded-patches))))
748 (defun stgit-select ()
749 "With point on a patch, toggle showing files in the patch.
751 With point on a file, open the associated file. Opens the target
752 file for (applied) copies and renames."
755 (case (get-text-property (point) 'entry-type)
757 (stgit-select-patch))
761 (error "No patch or file on line"))))
763 (defun stgit-find-file-other-window ()
764 "Open file at point in other window"
769 (defun stgit-find-file-merge ()
770 "Open file at point and merge it using `smerge-ediff'."
777 "Hide the stgit buffer."
782 (defun stgit-git-status ()
783 "Show status using `git-status'."
786 (unless (fboundp 'git-status)
787 (error "The stgit-git-status command requires git-status"))
788 (let ((dir default-directory))
789 (save-selected-window
793 (defun stgit-goal-column ()
794 "Return goal column for the current line"
795 (case (get-text-property (point) 'entry-type)
800 (defun stgit-next-line (&optional arg)
801 "Move cursor vertically down ARG lines"
805 (move-to-column (stgit-goal-column)))
807 (defun stgit-previous-line (&optional arg)
808 "Move cursor vertically up ARG lines"
812 (move-to-column (stgit-goal-column)))
814 (defun stgit-next-patch (&optional arg)
815 "Move cursor down ARG patches."
818 (ewoc-goto-next stgit-ewoc (or arg 1))
819 (move-to-column goal-column))
821 (defun stgit-previous-patch (&optional arg)
822 "Move cursor up ARG patches."
825 (ewoc-goto-prev stgit-ewoc (or arg 1))
826 (move-to-column goal-column))
828 (defvar stgit-mode-hook nil
829 "Run after `stgit-mode' is setup.")
831 (defvar stgit-mode-map nil
832 "Keymap for StGit major mode.")
834 (unless stgit-mode-map
835 (let ((diff-map (make-sparse-keymap))
836 (toggle-map (make-sparse-keymap)))
837 (suppress-keymap diff-map)
838 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
839 '(("b" . stgit-diff-base)
840 ("c" . stgit-diff-combined)
841 ("m" . stgit-find-file-merge)
842 ("o" . stgit-diff-ours)
843 ("t" . stgit-diff-theirs)))
844 (suppress-keymap toggle-map)
845 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
846 '(("t" . stgit-toggle-worktree)
847 ("i" . stgit-toggle-ignored)
848 ("u" . stgit-toggle-unknown)))
849 (setq stgit-mode-map (make-keymap))
850 (suppress-keymap stgit-mode-map)
851 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
852 `((" " . stgit-mark-down)
853 ("m" . stgit-mark-down)
854 ("\d" . stgit-unmark-up)
855 ("u" . stgit-unmark-down)
858 ("\C-p" . stgit-previous-line)
859 ("\C-n" . stgit-next-line)
860 ([up] . stgit-previous-line)
861 ([down] . stgit-next-line)
862 ("p" . stgit-previous-patch)
863 ("n" . stgit-next-patch)
864 ("\M-{" . stgit-previous-patch)
865 ("\M-}" . stgit-next-patch)
866 ("s" . stgit-git-status)
867 ("g" . stgit-reload-or-repair)
868 ("r" . stgit-refresh)
869 ("\C-c\C-r" . stgit-rename)
871 ("M" . stgit-move-patches)
874 ("c" . stgit-new-and-refresh)
875 ("\C-c\C-c" . stgit-commit)
876 ("\C-c\C-u" . stgit-uncommit)
878 ("R" . stgit-resolve-file)
879 ("\r" . stgit-select)
881 ("-" . stgit-collapse)
882 ("o" . stgit-find-file-other-window)
883 ("i" . stgit-toggle-index)
884 (">" . stgit-push-next)
885 ("<" . stgit-pop-next)
886 ("P" . stgit-push-or-pop)
890 ([?\C-/] . stgit-undo)
891 ("\C-_" . stgit-undo)
892 ([?\C-c ?\C-/] . stgit-redo)
893 ("\C-c\C-_" . stgit-redo)
895 ("\C-c\C-b" . stgit-rebase)
898 ("q" . stgit-quit))))
900 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
901 (and file (eq (stgit-file-status file)
903 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
904 (easy-menu-define stgit-menu stgit-mode-map
907 ["Reload" stgit-reload-or-repair
908 :help "Reload StGit status from disk"]
909 ["Repair" stgit-repair
910 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
911 :help "Repair StGit metadata"]
913 ["Undo" stgit-undo t]
914 ["Redo" stgit-redo t]
916 ["Git status" stgit-git-status :active (fboundp 'git-status)]
918 ["New patch" stgit-new-and-refresh
919 :help "Create a new patch from changes in index or work tree"
920 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
921 ["New empty patch" stgit-new
922 :help "Create a new, empty patch"]
923 ["(Un)mark patch" stgit-toggle-mark
924 :label (if (memq (stgit-patch-name-at-point nil t)
925 stgit-marked-patches)
926 "Unmark patch" "Mark patch")
927 :active (stgit-patch-name-at-point nil t)]
928 ["Expand/collapse patch"
929 (let ((patches (stgit-patches-marked-or-at-point)))
930 (if (member-if ,patch-collapsed-p patches)
931 (stgit-expand patches)
932 (stgit-collapse patches)))
933 :label (if (member-if ,patch-collapsed-p
934 (stgit-patches-marked-or-at-point))
937 :active (stgit-patches-marked-or-at-point)]
938 ["Edit patch" stgit-edit
939 :help "Edit patch comment"
940 :active (stgit-patch-name-at-point nil t)]
941 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
942 ["Push/pop patch" stgit-push-or-pop
943 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
944 (stgit-applied-patchsyms t))
945 "Pop patches" "Push patches")]
946 ["Delete patches" stgit-delete
947 :active (stgit-patches-marked-or-at-point nil t)]
949 ["Move patches" stgit-move-patches
950 :active stgit-marked-patches
951 :help "Move marked patch(es) to point"]
952 ["Squash patches" stgit-squash
953 :active (> (length stgit-marked-patches) 1)
954 :help "Merge marked patches into one"]
956 ["Refresh top patch" stgit-refresh
957 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
958 :help "Refresh the top patch with changes in index or work tree"]
959 ["Refresh this patch" (stgit-refresh t)
960 :keys "\\[universal-argument] \\[stgit-refresh]"
961 :help "Refresh marked patch with changes in index or work tree"
962 :active (and (not (and (stgit-index-empty-p)
963 (stgit-work-tree-empty-p)))
964 (stgit-patch-name-at-point nil t))]
966 ["Find file" stgit-select
967 :active (eq (get-text-property (point) 'entry-type) 'file)]
968 ["Open file" stgit-find-file-other-window
969 :active (eq (get-text-property (point) 'entry-type) 'file)]
970 ["Toggle file index" stgit-toggle-index
971 :active (and (eq (get-text-property (point) 'entry-type) 'file)
972 (memq (stgit-patch-name-at-point) '(:work :index)))
973 :label (if (eq (stgit-patch-name-at-point) :work)
974 "Move change to index"
975 "Move change to work tree")]
977 ["Show diff" stgit-diff
978 :active (get-text-property (point) 'entry-type)]
980 :active (stgit-git-index-unmerged-p)
981 ["Combined diff" stgit-diff-combined
982 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
983 ["Diff against base" stgit-diff-base
984 :help "Show diff against the common base"
985 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
986 ["Diff against ours" stgit-diff-ours
987 :help "Show diff against our branch"
988 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
989 ["Diff against theirs" stgit-diff-theirs
990 :help "Show diff against their branch"
991 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
993 ["Interactive merge" stgit-find-file-merge
994 :help "Interactively merge the file"
995 :active ,at-unmerged-file]
996 ["Resolve file" stgit-resolve-file
997 :help "Mark file conflict as resolved"
998 :active ,at-unmerged-file]
1001 ["Show index & work tree" stgit-toggle-worktree :style toggle
1002 :selected stgit-show-worktree]
1003 ["Show unknown files" stgit-toggle-unknown :style toggle
1004 :selected stgit-show-unknown :active stgit-show-worktree]
1005 ["Show ignored files" stgit-toggle-ignored :style toggle
1006 :selected stgit-show-ignored :active stgit-show-worktree]
1008 ["Switch branches" stgit-branch t
1009 :help "Switch to another branch"]
1010 ["Rebase branch" stgit-rebase t
1011 :help "Rebase the current branch"]
1014 ;; disable tool bar editing buttons
1015 (put 'stgit-mode 'mode-class 'special)
1017 (defun stgit-mode ()
1018 "Major mode for interacting with StGit.
1020 Start StGit using \\[stgit].
1024 \\[stgit-help] Show this help text
1025 \\[stgit-quit] Hide the StGit buffer
1026 \\[describe-bindings] Show all key bindings
1028 \\[stgit-reload-or-repair] Reload the StGit buffer
1029 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1031 \\[stgit-undo] Undo most recent StGit operation
1032 \\[stgit-redo] Undo recent undo
1034 \\[stgit-git-status] Run `git-status' (if available)
1037 \\[stgit-previous-line] Move to previous line
1038 \\[stgit-next-line] Move to next line
1039 \\[stgit-previous-patch] Move to previous patch
1040 \\[stgit-next-patch] Move to next patch
1042 \\[stgit-mark-down] Mark patch and move down
1043 \\[stgit-unmark-up] Unmark patch and move up
1044 \\[stgit-unmark-down] Unmark patch and move down
1046 Commands for patches:
1047 \\[stgit-select] Toggle showing changed files in patch
1048 \\[stgit-refresh] Refresh patch with changes in index or work tree
1049 \\[stgit-diff] Show the patch log and diff
1051 \\[stgit-expand] Show changes in marked patches
1052 \\[stgit-collapse] Hide changes in marked patches
1054 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1055 \\[stgit-new] Create a new, empty patch
1057 \\[stgit-rename] Rename patch
1058 \\[stgit-edit] Edit patch description
1059 \\[stgit-delete] Delete patch(es)
1061 \\[stgit-revert] Revert all changes in index or work tree
1062 \\[stgit-toggle-index] Toggle all changes between index and work tree
1064 \\[stgit-push-next] Push next patch onto stack
1065 \\[stgit-pop-next] Pop current patch from stack
1066 \\[stgit-push-or-pop] Push or pop marked patches
1067 \\[stgit-goto] Make patch at point current by popping or pushing
1069 \\[stgit-squash] Squash (meld together) patches
1070 \\[stgit-move-patches] Move marked patches to point
1072 \\[stgit-commit] Commit patch(es)
1073 \\[stgit-uncommit] Uncommit patch(es)
1076 \\[stgit-select] Open the file in this window
1077 \\[stgit-find-file-other-window] Open the file in another window
1078 \\[stgit-diff] Show the file's diff
1080 \\[stgit-toggle-index] Toggle change between index and work tree
1081 \\[stgit-revert] Revert changes to file
1084 \\[stgit-toggle-worktree] Toggle showing index and work tree
1085 \\[stgit-toggle-unknown] Toggle showing unknown files
1086 \\[stgit-toggle-ignored] Toggle showing ignored files
1089 \\[stgit-diff] Show diff of patch or file
1090 \\[stgit-diff-base] Show diff against the merge base
1091 \\[stgit-diff-ours] Show diff against our branch
1092 \\[stgit-diff-theirs] Show diff against their branch
1094 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1095 ignore space changes.
1096 With two prefix arguments (e.g., \\[universal-argument] \
1097 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1099 Commands for merge conflicts:
1100 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1101 \\[stgit-resolve-file] Mark unmerged file as resolved
1103 Commands for branches:
1104 \\[stgit-branch] Switch to another branch
1105 \\[stgit-rebase] Rebase the current branch
1107 Customization variables:
1108 `stgit-abbreviate-copies-and-renames'
1109 `stgit-default-show-worktree'
1110 `stgit-find-copies-harder'
1111 `stgit-show-worktree-mode'
1113 See also \\[customize-group] for the \"stgit\" group."
1114 (kill-all-local-variables)
1115 (buffer-disable-undo)
1116 (setq mode-name "StGit"
1117 major-mode 'stgit-mode
1119 (use-local-map stgit-mode-map)
1120 (set (make-local-variable 'list-buffers-directory) default-directory)
1121 (set (make-local-variable 'stgit-marked-patches) nil)
1122 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1123 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1124 (set (make-local-variable 'stgit-index-node) nil)
1125 (set (make-local-variable 'stgit-worktree-node) nil)
1126 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1127 (set-variable 'truncate-lines 't)
1128 (add-hook 'after-save-hook 'stgit-update-saved-file)
1129 (run-hooks 'stgit-mode-hook))
1131 (defun stgit-update-saved-file ()
1132 (let* ((file (expand-file-name buffer-file-name))
1133 (dir (file-name-directory file))
1134 (gitdir (condition-case nil (git-get-top-dir dir)
1136 (buffer (and gitdir (stgit-find-buffer gitdir))))
1138 (with-current-buffer buffer
1139 (stgit-refresh-worktree)))))
1141 (defun stgit-add-mark (patchsym)
1142 "Mark the patch PATCHSYM."
1143 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1145 (defun stgit-remove-mark (patchsym)
1146 "Unmark the patch PATCHSYM."
1147 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1149 (defun stgit-clear-marks ()
1150 "Unmark all patches."
1151 (setq stgit-marked-patches '()))
1153 (defun stgit-patch-at-point (&optional cause-error)
1154 (get-text-property (point) 'patch-data))
1156 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1157 "Return the patch name on the current line as a symbol.
1158 If CAUSE-ERROR is not nil, signal an error if none found.
1159 If ONLY-PATCHES is not nil, only allow real patches, and not
1160 index or work tree."
1161 (let ((patch (stgit-patch-at-point)))
1164 (memq (stgit-patch-status patch) '(work index))
1167 (stgit-patch-name patch))
1169 (error "No patch on this line")))))
1171 (defun stgit-patched-file-at-point ()
1172 (get-text-property (point) 'file-data))
1174 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1175 "Return the symbols of the marked patches, or the patch on the current line.
1176 If CAUSE-ERRROR is not nil, signal an error if none found.
1177 If ONLY-PATCHES is not nil, do not include index or work tree."
1178 (if stgit-marked-patches
1179 stgit-marked-patches
1180 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1181 (cond (patch (list patch))
1182 (cause-error (error "No patches marked or at this line"))
1185 (defun stgit-goto-patch (patchsym &optional file)
1186 "Move point to the line containing patch PATCHSYM.
1187 If that patch cannot be found, do nothing.
1189 If the patch was found and FILE is not nil, instead move to that
1190 file's line. If FILE cannot be found, stay on the line of
1192 (let ((node (ewoc-nth stgit-ewoc 0)))
1193 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
1195 (setq node (ewoc-next stgit-ewoc node)))
1196 (when (and node file)
1197 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
1198 (file-node (ewoc-nth file-ewoc 0)))
1199 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
1200 (setq file-node (ewoc-next file-ewoc file-node)))
1202 (ewoc-goto-node file-ewoc file-node)
1203 (move-to-column (stgit-goal-column))
1206 (ewoc-goto-node stgit-ewoc node)
1207 (move-to-column goal-column))))
1209 (defun stgit-init ()
1213 (stgit-capture-output nil
1217 (defun stgit-toggle-mark ()
1218 "Toggle mark on the patch under point."
1221 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1225 (defun stgit-mark ()
1226 "Mark the patch under point."
1229 (let* ((node (ewoc-locate stgit-ewoc))
1230 (patch (ewoc-data node))
1231 (name (stgit-patch-name patch)))
1232 (when (eq name :work)
1233 (error "Cannot mark the work tree"))
1234 (when (eq name :index)
1235 (error "Cannot mark the index"))
1236 (stgit-add-mark (stgit-patch-name patch))
1237 (let ((column (current-column)))
1238 (ewoc-invalidate stgit-ewoc node)
1239 (move-to-column column))))
1241 (defun stgit-mark-down ()
1242 "Mark the patch under point and move to the next patch."
1247 (defun stgit-unmark ()
1248 "Remove mark from the patch on the current line."
1251 (let* ((node (ewoc-locate stgit-ewoc))
1252 (patch (ewoc-data node)))
1253 (stgit-remove-mark (stgit-patch-name patch))
1254 (let ((column (current-column)))
1255 (ewoc-invalidate stgit-ewoc node)
1256 (move-to-column column))))
1258 (defun stgit-unmark-up ()
1259 "Remove mark from the patch on the previous line."
1262 (stgit-previous-patch)
1265 (defun stgit-unmark-down ()
1266 "Remove mark from the patch on the current line."
1272 (defun stgit-rename (name)
1273 "Rename the patch under point to NAME."
1275 (read-string "Patch name: "
1276 (symbol-name (stgit-patch-name-at-point t t)))))
1278 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1279 (stgit-capture-output nil
1280 (stgit-run "rename" old-patchsym name))
1281 (let ((name-sym (intern name)))
1282 (when (memq old-patchsym stgit-expanded-patches)
1283 (setq stgit-expanded-patches
1284 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1285 (when (memq old-patchsym stgit-marked-patches)
1286 (setq stgit-marked-patches
1287 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1289 (stgit-goto-patch name-sym))))
1291 (defun stgit-reload-or-repair (repair)
1292 "Update the contents of the StGit buffer (`stgit-reload').
1294 With a prefix argument, repair the StGit metadata if the branch
1295 was modified with git commands (`stgit-repair')."
1302 (defun stgit-repair ()
1306 (stgit-capture-output nil
1307 (stgit-run "repair"))
1310 (defun stgit-available-branches ()
1311 "Returns a list of the available stg branches"
1312 (let ((output (with-output-to-string
1313 (stgit-run "branch" "--list")))
1316 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1317 (setq result (cons (match-string 1 output) result))
1318 (setq start (match-end 0)))
1321 (defun stgit-branch (branch)
1322 "Switch to branch BRANCH."
1323 (interactive (list (completing-read "Switch to branch: "
1324 (stgit-available-branches))))
1326 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1329 (defun stgit-available-refs (&optional omit-stgit)
1330 "Returns a list of the available git refs.
1331 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1332 (let* ((output (with-output-to-string
1333 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1334 "refs/tags" "refs/heads"
1336 (result (split-string output "\n" t)))
1338 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1339 (substring s (match-end 0))
1342 (delete-if (lambda (s)
1343 (string-match "^refs/heads/.*\\.stgit$" s))
1347 (defun stgit-rebase (new-base)
1348 "Rebase to NEW-BASE."
1349 (interactive (list (completing-read "Rebase to: "
1350 (stgit-available-refs t))))
1352 (stgit-capture-output nil (stgit-run "rebase" new-base))
1355 (defun stgit-commit (count)
1356 "Run stg commit on COUNT commits.
1357 Interactively, the prefix argument is used as COUNT.
1358 A negative COUNT will uncommit instead."
1362 (stgit-uncommit (- count))
1363 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1366 (defun stgit-uncommit (count)
1367 "Run stg uncommit on COUNT commits.
1368 Interactively, the prefix argument is used as COUNT.
1369 A negative COUNT will commit instead."
1373 (stgit-commit (- count))
1374 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1377 (defun stgit-neighbour-file ()
1378 "Return the file name of the next file after point, or the
1379 previous file if point is at the last file within a patch."
1380 (let ((old-point (point))
1382 (and (zerop (forward-line 1))
1383 (let ((f (stgit-patched-file-at-point)))
1384 (and f (setq neighbour-file (stgit-file-file f)))))
1385 (goto-char old-point)
1386 (unless neighbour-file
1387 (and (zerop (forward-line -1))
1388 (let ((f (stgit-patched-file-at-point)))
1389 (and f (setq neighbour-file (stgit-file-file f)))))
1390 (goto-char old-point))
1393 (defun stgit-revert-file ()
1394 "Revert the file at point, which must be in the index or the
1398 (let* ((patched-file (or (stgit-patched-file-at-point)
1399 (error "No file on the current line")))
1400 (patch-name (stgit-patch-name-at-point))
1401 (file-status (stgit-file-status patched-file))
1402 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1403 (stgit-file-cr-to patched-file))
1404 ((eq file-status 'add)
1405 (stgit-file-file patched-file))))
1406 (co-file (cond ((eq file-status 'rename)
1407 (stgit-file-cr-from patched-file))
1408 ((not (memq file-status '(copy add)))
1409 (stgit-file-file patched-file))))
1410 (next-file (stgit-neighbour-file)))
1412 (unless (memq patch-name '(:work :index))
1413 (error "No index or working tree file on this line"))
1415 (when (eq file-status 'ignore)
1416 (error "Cannot revert ignored files"))
1418 (when (eq file-status 'unknown)
1419 (error "Cannot revert unknown files"))
1421 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1422 (when (yes-or-no-p (format "Revert %d file%s? "
1424 (if (= nfiles 1) "" "s")))
1425 (stgit-capture-output nil
1427 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1429 (stgit-run-git "checkout" "HEAD" co-file)))
1431 (stgit-goto-patch patch-name next-file)))))
1433 (defun stgit-revert ()
1434 "Revert the change at point, which must be the index, the work
1435 tree, or a single change in either."
1438 (let ((patched-file (stgit-patched-file-at-point)))
1441 (let* ((patch-name (or (stgit-patch-name-at-point)
1442 (error "No patch or file at point")))
1443 (patch-desc (case patch-name
1446 (t (error (substitute-command-keys
1447 "Use \\[stgit-delete] to delete a patch"))))))
1448 (when (if (eq patch-name :work)
1449 (stgit-work-tree-empty-p)
1450 (stgit-index-empty-p))
1451 (error (format "There are no changes in the %s to revert"
1453 (and (eq patch-name :index)
1454 (not (stgit-work-tree-empty-p))
1455 (error "Cannot revert index as work tree contains unstaged changes"))
1457 (when (yes-or-no-p (format "Revert all changes in the %s? "
1459 (if (eq patch-name :index)
1460 (stgit-run-git-silent "reset" "--hard" "-q")
1461 (stgit-run-git-silent "checkout" "--" "."))
1462 (stgit-refresh-index)
1463 (stgit-refresh-worktree)
1464 (stgit-goto-patch patch-name))))))
1466 (defun stgit-resolve-file ()
1467 "Resolve conflict in the file at point."
1470 (let* ((patched-file (stgit-patched-file-at-point))
1471 (patch (stgit-patch-at-point))
1472 (patch-name (and patch (stgit-patch-name patch)))
1473 (status (and patched-file (stgit-file-status patched-file))))
1475 (unless (memq patch-name '(:work :index))
1476 (error "No index or working tree file on this line"))
1478 (unless (eq status 'unmerged)
1479 (error "No conflict to resolve at the current line"))
1481 (stgit-capture-output nil
1482 (stgit-move-change-to-index (stgit-file-file patched-file)))
1486 (defun stgit-push-next (npatches)
1487 "Push the first unapplied patch.
1488 With numeric prefix argument, push that many patches."
1491 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1493 (stgit-refresh-git-status))
1495 (defun stgit-pop-next (npatches)
1496 "Pop the topmost applied patch.
1497 With numeric prefix argument, pop that many patches."
1500 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1502 (stgit-refresh-git-status))
1504 (defun stgit-applied-patches (&optional only-patches)
1505 "Return a list of the applied patches.
1507 If ONLY-PATCHES is not nil, exclude index and work tree."
1508 (let ((states (if only-patches
1510 '(applied top index work)))
1512 (ewoc-map (lambda (patch) (when (memq (stgit-patch-status patch) states)
1513 (setq result (cons patch result))))
1517 (defun stgit-applied-patchsyms (&optional only-patches)
1518 "Return a list of the symbols of the applied patches.
1520 If ONLY-PATCHES is not nil, exclude index and work tree."
1521 (mapcar #'stgit-patch-name (stgit-applied-patches only-patches)))
1523 (defun stgit-push-or-pop ()
1524 "Push or pop the marked patches."
1527 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1528 (applied-syms (stgit-applied-patchsyms t))
1529 (unapplied (set-difference patchsyms applied-syms)))
1530 (stgit-capture-output nil
1532 (if unapplied "push" "pop")
1534 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1537 (defun stgit-goto ()
1538 "Go to the patch on the current line.
1540 Pops or pushes patches to make this patch topmost."
1543 (let ((patchsym (stgit-patch-name-at-point t)))
1544 (stgit-capture-output nil
1545 (stgit-run "goto" patchsym))
1548 (defun stgit-id (patchsym)
1549 "Return the git commit id for PATCHSYM.
1550 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1551 (if (keywordp patchsym)
1553 (let ((result (with-output-to-string
1554 (stgit-run-silent "id" patchsym))))
1555 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1556 (error "Cannot find commit id for %s" patchsym))
1557 (match-string 1 result))))
1559 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1560 "Show the patch on the current line.
1562 UNMERGED-STAGE is the argument to `git-diff' that that selects
1563 which stage to diff against in the case of unmerged files."
1564 (let ((space-arg (when (numberp ignore-whitespace)
1565 (cond ((> ignore-whitespace 4)
1566 "--ignore-all-space")
1567 ((> ignore-whitespace 1)
1568 "--ignore-space-change"))))
1569 (patch-name (stgit-patch-name-at-point t)))
1570 (stgit-capture-output "*StGit patch*"
1571 (case (get-text-property (point) 'entry-type)
1573 (let* ((patched-file (stgit-patched-file-at-point))
1574 (patch-id (let ((id (stgit-id patch-name)))
1575 (if (and (eq id :index)
1576 (eq (stgit-file-status patched-file)
1580 (args (append (and space-arg (list space-arg))
1581 (and (stgit-file-cr-from patched-file)
1582 (list (stgit-find-copies-harder-diff-arg)))
1583 (cond ((eq patch-id :index)
1585 ((eq patch-id :work)
1586 (list unmerged-stage))
1588 (list (concat patch-id "^") patch-id)))
1590 (if (stgit-file-copy-or-rename patched-file)
1591 (list (stgit-file-cr-from patched-file)
1592 (stgit-file-cr-to patched-file))
1593 (list (stgit-file-file patched-file))))))
1594 (apply 'stgit-run-git "diff" args)))
1596 (let* ((patch-id (stgit-id patch-name)))
1597 (if (or (eq patch-id :index) (eq patch-id :work))
1598 (apply 'stgit-run-git "diff"
1599 (stgit-find-copies-harder-diff-arg)
1600 (append (and space-arg (list space-arg))
1601 (if (eq patch-id :index)
1603 (list unmerged-stage))))
1604 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1605 (and space-arg (list "-O" space-arg))
1606 (list (stgit-patch-name-at-point)))))
1607 (apply 'stgit-run args)))))
1609 (error "No patch or file at point")))
1610 (with-current-buffer standard-output
1611 (goto-char (point-min))
1614 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1615 `(defun ,name (&optional ignore-whitespace)
1616 ,(format "Show the patch on the current line.
1618 %sWith a prefix argument, ignore whitespace. With a prefix argument
1619 greater than four (e.g., \\[universal-argument] \
1620 \\[universal-argument] \\[%s]), ignore all whitespace."
1622 (format "For unmerged files, %s.\n\n" unmerged-action)
1627 (stgit-show-patch ,diff-arg ignore-whitespace)))
1629 (stgit-define-diff stgit-diff
1631 (stgit-define-diff stgit-diff-ours
1633 "diff against our branch")
1634 (stgit-define-diff stgit-diff-theirs
1636 "diff against their branch")
1637 (stgit-define-diff stgit-diff-base
1639 "diff against the merge base")
1640 (stgit-define-diff stgit-diff-combined
1642 "show a combined diff")
1644 (defun stgit-move-change-to-index (file &optional force)
1645 "Copies the work tree state of FILE to index, using git add or git rm.
1647 If FORCE is not nil, use --force."
1648 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1649 '("add") '("rm" "-q"))))
1650 (stgit-capture-output "*git output*"
1651 (apply 'stgit-run-git (append op (and force '("--force"))
1652 '("--") (list file))))))
1654 (defun stgit-remove-change-from-index (file)
1655 "Unstages the change in FILE from the index"
1656 (stgit-capture-output "*git output*"
1657 (stgit-run-git "reset" "-q" "--" file)))
1659 (defun stgit-git-index-unmerged-p ()
1661 (with-output-to-string
1662 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1664 "--quiet" "HEAD")))))
1667 (defun stgit-file-toggle-index ()
1668 "Move modified file in or out of the index.
1670 Leaves the point where it is, but moves the mark to where the
1671 file ended up. You can then jump to the file with \
1672 \\[exchange-point-and-mark]."
1675 (let* ((patched-file (or (stgit-patched-file-at-point)
1676 (error "No file on the current line")))
1677 (patched-status (stgit-file-status patched-file)))
1678 (when (eq patched-status 'unmerged)
1679 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1680 (let* ((patch (stgit-patch-at-point))
1681 (patch-name (stgit-patch-name patch))
1682 (mark-file (if (eq patched-status 'rename)
1683 (stgit-file-cr-to patched-file)
1684 (stgit-file-file patched-file)))
1685 (point-file (if (eq patched-status 'rename)
1686 (stgit-file-cr-from patched-file)
1687 (stgit-neighbour-file))))
1689 (cond ((eq patch-name :work)
1690 (stgit-move-change-to-index (stgit-file-file patched-file)
1691 (eq patched-status 'ignore)))
1692 ((eq patch-name :index)
1693 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1695 (error "Can only move files between working tree and index")))
1696 (stgit-refresh-worktree)
1697 (stgit-refresh-index)
1698 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1700 (stgit-goto-patch patch-name point-file))))
1702 (defun stgit-toggle-index ()
1703 "Move change in or out of the index.
1705 Works on index and work tree, as well as files in either.
1707 Leaves the point where it is, but moves the mark to where the
1708 file ended up. You can then jump to the file with \
1709 \\[exchange-point-and-mark]."
1712 (if (stgit-patched-file-at-point)
1713 (stgit-file-toggle-index)
1714 (let ((patch-name (stgit-patch-name-at-point)))
1715 (unless (memq patch-name '(:index :work))
1716 (error "Can only move changes between working tree and index"))
1717 (when (stgit-git-index-unmerged-p)
1718 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1719 (if (if (eq patch-name :index)
1720 (stgit-index-empty-p)
1721 (stgit-work-tree-empty-p))
1722 (message "No changes to be moved")
1723 (stgit-capture-output nil
1724 (if (eq patch-name :work)
1725 (stgit-run-git "add" "--update")
1726 (stgit-run-git "reset" "--mixed" "-q")))
1727 (stgit-refresh-worktree)
1728 (stgit-refresh-index))
1729 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1731 (defun stgit-edit ()
1732 "Edit the patch on the current line."
1735 (let ((patchsym (stgit-patch-name-at-point t t))
1736 (edit-buf (get-buffer-create "*StGit edit*"))
1737 (dir default-directory))
1738 (log-edit 'stgit-confirm-edit t nil edit-buf)
1739 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1740 (setq default-directory dir)
1741 (let ((standard-output edit-buf))
1743 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1745 (defun stgit-confirm-edit ()
1747 (let ((file (make-temp-file "stgit-edit-")))
1748 (write-region (point-min) (point-max) file)
1749 (stgit-capture-output nil
1750 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1751 (with-current-buffer log-edit-parent-buffer
1754 (defun stgit-new (add-sign &optional refresh)
1755 "Create a new patch.
1756 With a prefix argument, include a \"Signed-off-by:\" line at the
1760 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1761 (dir default-directory))
1762 (log-edit 'stgit-confirm-new t nil edit-buf)
1763 (setq default-directory dir)
1764 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1767 (let ((standard-output (current-buffer)))
1768 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1770 (defun stgit-confirm-new ()
1772 (let ((file (make-temp-file "stgit-edit-"))
1773 (refresh stgit-refresh-after-new))
1774 (write-region (point-min) (point-max) file)
1775 (stgit-capture-output nil
1776 (stgit-run "new" "-f" file))
1777 (with-current-buffer log-edit-parent-buffer
1782 (defun stgit-new-and-refresh (add-sign)
1783 "Create a new patch and refresh it with the current changes.
1785 With a prefix argument, include a \"Signed-off-by:\" line at the
1788 This works just like running `stgit-new' followed by `stgit-refresh'."
1791 (stgit-new add-sign t))
1793 (defun stgit-create-patch-name (description)
1794 "Create a patch name from a long description"
1796 (while (> (length description) 0)
1797 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1798 (setq patch (downcase (concat patch
1799 (match-string 0 description))))
1800 (setq description (substring description (match-end 0))))
1801 ((string-match "\\` +" description)
1802 (setq patch (concat patch "-"))
1803 (setq description (substring description (match-end 0))))
1804 ((string-match "\\`[^a-zA-Z_-]+" description)
1805 (setq description (substring description (match-end 0))))))
1806 (cond ((= (length patch) 0)
1808 ((> (length patch) 20)
1809 (substring patch 0 20))
1812 (defun stgit-delete (patchsyms &optional spill-p)
1813 "Delete the patches in PATCHSYMS.
1814 Interactively, delete the marked patches, or the patch at point.
1816 With a prefix argument, or SPILL-P, spill the patch contents to
1817 the work tree and index."
1818 (interactive (list (stgit-patches-marked-or-at-point t t)
1819 current-prefix-arg))
1822 (error "No patches to delete"))
1823 (when (memq :index patchsyms)
1824 (error "Cannot delete the index"))
1825 (when (memq :work patchsyms)
1826 (error "Cannot delete the work tree"))
1828 (let ((npatches (length patchsyms)))
1829 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1831 (if (= 1 npatches) "" "es")
1833 " (spilling contents to index)"
1835 (let ((args (if spill-p
1836 (cons "--spill" patchsyms)
1838 (stgit-capture-output nil
1839 (apply 'stgit-run "delete" args))
1842 (defun stgit-move-patches-target ()
1843 "Return the patchsym indicating a target patch for
1844 `stgit-move-patches'.
1846 This is either the first unmarked patch at or after point, or one
1847 of :top and :bottom if the point is after or before the applied
1853 (let ((patchsym (stgit-patch-name-at-point)))
1854 (cond ((memq patchsym '(:work :index)) (setq result :top))
1855 (patchsym (if (memq patchsym stgit-marked-patches)
1857 (setq result patchsym)))
1858 ((re-search-backward "^>" nil t) (setq result :top))
1859 (t (setq result :bottom)))))
1862 (defun stgit-sort-patches (patchsyms)
1863 "Returns the list of patches in PATCHSYMS sorted according to
1864 their position in the patch series, bottommost first.
1866 PATCHSYMS must not contain duplicate entries."
1867 (let (sorted-patchsyms
1868 (series (with-output-to-string
1869 (with-current-buffer standard-output
1870 (stgit-run-silent "series" "--noprefix"))))
1872 (while (string-match "^\\(.+\\)" series start)
1873 (let ((patchsym (intern (match-string 1 series))))
1874 (when (memq patchsym patchsyms)
1875 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1876 (setq start (match-end 0)))
1877 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1879 (unless (= (length patchsyms) (length sorted-patchsyms))
1880 (error "Internal error"))
1884 (defun stgit-move-patches (patchsyms target-patch)
1885 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1886 If TARGET-PATCH is :bottom or :top, move the patches to the
1887 bottom or top of the stack, respectively.
1889 Interactively, move the marked patches to where the point is."
1890 (interactive (list stgit-marked-patches
1891 (stgit-move-patches-target)))
1894 (error "Need at least one patch to move"))
1896 (unless target-patch
1897 (error "Point not at a patch"))
1899 ;; need to have patchsyms sorted by position in the stack
1900 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1901 (stgit-capture-output nil
1902 (if (eq target-patch :top)
1903 (apply 'stgit-run "float" sorted-patchsyms)
1906 (append (unless (eq target-patch :bottom)
1907 (list "--to" target-patch))
1909 sorted-patchsyms)))))
1912 (defun stgit-squash (patchsyms)
1913 "Squash the patches in PATCHSYMS.
1914 Interactively, squash the marked patches.
1916 Unless there are any conflicts, the patches will be merged into
1917 one patch, which will occupy the same spot in the series as the
1918 deepest patch had before the squash."
1919 (interactive (list stgit-marked-patches))
1921 (when (< (length patchsyms) 2)
1922 (error "Need at least two patches to squash"))
1923 (let ((stgit-buffer (current-buffer))
1924 (edit-buf (get-buffer-create "*StGit edit*"))
1925 (dir default-directory)
1926 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1927 (log-edit 'stgit-confirm-squash t nil edit-buf)
1928 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1929 (setq default-directory dir)
1930 (let ((result (let ((standard-output edit-buf))
1932 (apply 'stgit-run-silent "squash"
1933 "--save-template=-" sorted-patchsyms)))))
1935 ;; stg squash may have reordered the patches or caused conflicts
1936 (with-current-buffer stgit-buffer
1939 (unless (eq 0 result)
1941 (rename-buffer "*StGit error*")
1942 (resize-temp-buffer-window)
1943 (switch-to-buffer-other-window stgit-buffer)
1944 (error "stg squash failed")))))
1946 (defun stgit-confirm-squash ()
1948 (let ((file (make-temp-file "stgit-edit-")))
1949 (write-region (point-min) (point-max) file)
1950 (stgit-capture-output nil
1951 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1952 (with-current-buffer log-edit-parent-buffer
1954 ;; Go to first marked patch and stay there
1955 (goto-char (point-min))
1956 (re-search-forward (concat "^[>+-]\\*") nil t)
1957 (move-to-column goal-column)
1958 (let ((pos (point)))
1962 (defun stgit-help ()
1963 "Display help for the StGit mode."
1965 (describe-function 'stgit-mode))
1967 (defun stgit-undo (&optional arg)
1969 With prefix argument, run it with the --hard flag.
1971 See also `stgit-redo'."
1974 (stgit-capture-output nil
1976 (stgit-run "undo" "--hard")
1977 (stgit-run "undo")))
1980 (defun stgit-redo (&optional arg)
1982 With prefix argument, run it with the --hard flag.
1984 See also `stgit-undo'."
1987 (stgit-capture-output nil
1989 (stgit-run "redo" "--hard")
1990 (stgit-run "redo")))
1993 (defun stgit-refresh (&optional arg)
1995 If the index contains any changes, only refresh from index.
1997 With prefix argument, refresh the marked patch or the patch under point."
2000 (let ((patchargs (if arg
2001 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2002 (when (> (length patches) 1)
2003 (error "Too many patches marked"))
2004 (cons "-p" patches))
2006 (unless (stgit-index-empty-p)
2007 (setq patchargs (cons "--index" patchargs)))
2008 (stgit-capture-output nil
2009 (apply 'stgit-run "refresh" patchargs))
2010 (stgit-refresh-git-status))
2013 (defvar stgit-show-worktree nil
2014 "If nil, inhibit showing work tree and index in the stgit buffer.
2016 See also `stgit-show-worktree-mode'.")
2018 (defvar stgit-show-ignored nil
2019 "If nil, inhibit showing files ignored by git.")
2021 (defvar stgit-show-unknown nil
2022 "If nil, inhibit showing files not registered with git.")
2024 (defun stgit-toggle-worktree (&optional arg)
2025 "Toggle the visibility of the work tree.
2026 With ARG, show the work tree if ARG is positive.
2028 Its initial setting is controlled by `stgit-default-show-worktree'.
2030 `stgit-show-worktree-mode' controls where on screen the index and
2031 work tree will show up."
2034 (setq stgit-show-worktree
2037 (not stgit-show-worktree)))
2040 (defun stgit-toggle-ignored (&optional arg)
2041 "Toggle the visibility of files ignored by git in the work
2042 tree. With ARG, show these files if ARG is positive.
2044 Use \\[stgit-toggle-worktree] to show the work tree."
2047 (setq stgit-show-ignored
2050 (not stgit-show-ignored)))
2053 (defun stgit-toggle-unknown (&optional arg)
2054 "Toggle the visibility of files not registered with git in the
2055 work tree. With ARG, show these files if ARG is positive.
2057 Use \\[stgit-toggle-worktree] to show the work tree."
2060 (setq stgit-show-unknown
2063 (not stgit-show-unknown)))