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 (:conc-name stgit-patch->))
256 status name desc empty files-ewoc)
258 (defun stgit-patch-display-name (patch)
259 (let ((name (stgit-patch->name patch)))
263 (t (symbol-name name)))))
265 (defun stgit-patch-pp (patch)
266 (let* ((status (stgit-patch->status patch))
268 (name (stgit-patch->name patch))
269 (face (cdr (assq status stgit-patch-status-face-alist)))
270 (fmt (if stgit-show-patch-names
271 stgit-patch-line-format
272 stgit-noname-patch-line-format))
273 (spec (format-spec-make
279 ?m (if (memq name stgit-marked-patches)
281 ?n (propertize (stgit-patch-display-name patch)
283 'syntax-table (string-to-syntax "w"))
284 ?e (if (stgit-patch->empty patch) "(empty) " "")
285 ?d (propertize (or (stgit-patch->desc patch) "")
286 'face 'stgit-description-face)
287 ?D (propertize (or (stgit-patch->desc patch)
288 (stgit-patch-display-name patch))
291 (insert (format-spec fmt spec) "\n")
292 (put-text-property start (point) 'entry-type 'patch)
293 (when (memq name stgit-expanded-patches)
294 (stgit-insert-patch-files patch))
295 (put-text-property start (point) 'patch-data patch)))
297 (defun create-stgit-buffer (dir)
298 "Create a buffer for showing StGit patches.
299 Argument DIR is the repository path."
300 (let ((buf (create-file-buffer (concat dir "*stgit*")))
301 (inhibit-read-only t))
302 (with-current-buffer buf
303 (setq default-directory dir)
305 (set (make-local-variable 'stgit-ewoc)
306 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
307 (setq buffer-read-only t))
310 (defmacro stgit-capture-output (name &rest body)
311 "Capture StGit output and, if there was any output, show it in a window
313 Returns nil if there was no output."
314 (declare (debug ([&or stringp null] body))
316 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
317 (stgit-dir default-directory)
318 (inhibit-read-only t))
319 (with-current-buffer output-buf
320 (buffer-disable-undo)
322 (setq default-directory stgit-dir)
323 (setq buffer-read-only t))
324 (let ((standard-output output-buf))
326 (with-current-buffer output-buf
327 (set-buffer-modified-p nil)
328 (setq buffer-read-only t)
329 (if (< (point-min) (point-max))
330 (display-buffer output-buf t)))))
332 (defun stgit-make-run-args (args)
333 "Return a copy of ARGS with its elements converted to strings."
335 ;; don't use (format "%s" ...) to limit type errors
336 (cond ((stringp x) x)
337 ((integerp x) (number-to-string x))
338 ((symbolp x) (symbol-name x))
340 (error "Bad element in stgit-make-run-args args: %S" x))))
343 (defun stgit-run-silent (&rest args)
344 (setq args (stgit-make-run-args args))
345 (apply 'call-process "stg" nil standard-output nil args))
347 (defun stgit-run (&rest args)
348 (setq args (stgit-make-run-args args))
349 (let ((msgcmd (mapconcat #'identity args " ")))
350 (message "Running stg %s..." msgcmd)
351 (apply 'call-process "stg" nil standard-output nil args)
352 (message "Running stg %s...done" msgcmd)))
354 (defun stgit-run-git (&rest args)
355 (setq args (stgit-make-run-args args))
356 (let ((msgcmd (mapconcat #'identity args " ")))
357 (message "Running git %s..." msgcmd)
358 (apply 'call-process "git" nil standard-output nil args)
359 (message "Running git %s...done" msgcmd)))
361 (defun stgit-run-git-silent (&rest args)
362 (setq args (stgit-make-run-args args))
363 (apply 'call-process "git" nil standard-output nil args))
365 (defun stgit-index-empty-p ()
366 "Returns non-nil if the index contains no changes from HEAD."
367 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
369 (defun stgit-work-tree-empty-p ()
370 "Returns non-nil if the work tree contains no changes from index."
371 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
373 (defvar stgit-index-node)
374 (defvar stgit-worktree-node)
376 (defun stgit-refresh-index ()
377 (when stgit-index-node
378 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
380 (defun stgit-refresh-worktree ()
381 (when stgit-worktree-node
382 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
384 (defun stgit-run-series-insert-index (ewoc)
385 (setq index-node (cons ewoc (ewoc-enter-last ewoc
391 worktree-node (cons ewoc (ewoc-enter-last ewoc
398 (defun stgit-run-series (ewoc)
399 (setq stgit-index-node nil
400 stgit-worktree-node nil)
401 (let ((inserted-index (not stgit-show-worktree))
406 (let* ((standard-output (current-buffer))
407 (exit-status (stgit-run-silent "series"
408 "--description" "--empty")))
409 (goto-char (point-min))
410 (if (not (zerop exit-status))
411 (cond ((looking-at "stg series: \\(.*\\)")
412 (setq inserted-index t)
413 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
414 (substitute-command-keys
415 "-- not initialized; run \\[stgit-init]")))
417 (error "Error running stg: %s"
421 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
422 (error "Syntax error in output from stg series"))
423 (let* ((state-str (match-string 2))
424 (state (cond ((string= state-str ">") 'top)
425 ((string= state-str "+") 'applied)
426 ((string= state-str "-") 'unapplied)))
427 (name (intern (match-string 4)))
428 (desc (match-string 5))
429 (empty (string= (match-string 1) "0")))
430 (unless inserted-index
431 (when (or (eq stgit-show-worktree-mode 'top)
432 (and (eq stgit-show-worktree-mode 'center)
433 (eq state 'unapplied)))
434 (setq inserted-index t)
435 (stgit-run-series-insert-index ewoc)))
436 (setq all-patchsyms (cons name all-patchsyms))
437 (ewoc-enter-last ewoc
444 (unless inserted-index
445 (stgit-run-series-insert-index ewoc)))
446 (setq stgit-index-node index-node
447 stgit-worktree-node worktree-node
448 stgit-marked-patches (intersection stgit-marked-patches
451 (defun stgit-reload ()
452 "Update the contents of the StGit buffer."
455 (let ((inhibit-read-only t)
456 (curline (line-number-at-pos))
457 (curpatch (stgit-patch-name-at-point))
458 (curfile (stgit-patched-file-at-point)))
459 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
460 (ewoc-set-hf stgit-ewoc
463 (substring (with-output-to-string
464 (stgit-run-silent "branch"))
466 'face 'stgit-branch-name-face)
468 (if stgit-show-worktree
471 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
472 shows the working tree\n")
473 'face 'stgit-description-face)))
474 (stgit-run-series stgit-ewoc)
476 (stgit-goto-patch curpatch (and curfile (stgit-file->file curfile)))
477 (goto-line curline)))
478 (stgit-refresh-git-status))
480 (defconst stgit-file-status-code-strings
481 (mapcar (lambda (arg)
483 (propertize (cadr arg) 'face (car (cddr arg)))))
484 '((add "Added" stgit-modified-file-face)
485 (copy "Copied" stgit-modified-file-face)
486 (delete "Deleted" stgit-modified-file-face)
487 (modify "Modified" stgit-modified-file-face)
488 (rename "Renamed" stgit-modified-file-face)
489 (mode-change "Mode change" stgit-modified-file-face)
490 (unmerged "Unmerged" stgit-unmerged-file-face)
491 (unknown "Unknown" stgit-unknown-file-face)
492 (ignore "Ignored" stgit-ignored-file-face)))
493 "Alist of code symbols to description strings")
495 (defconst stgit-patch-status-face-alist
496 '((applied . stgit-applied-patch-face)
497 (top . stgit-top-patch-face)
498 (unapplied . stgit-unapplied-patch-face)
499 (index . stgit-index-work-tree-title-face)
500 (work . stgit-index-work-tree-title-face))
501 "Alist of face to use for a given patch status")
503 (defun stgit-file-status-code-as-string (file)
504 "Return stgit status code for FILE as a string"
505 (let* ((code (assq (stgit-file->status file)
506 stgit-file-status-code-strings))
507 (score (stgit-file->cr-score file)))
509 (if (and score (/= score 100))
510 (format "%s %s" (cdr code)
511 (propertize (format "%d%%" score)
512 'face 'stgit-description-face))
515 (defun stgit-file-status-code (str &optional score)
516 "Return stgit status code from git status string"
517 (let ((code (assoc str '(("A" . add)
526 (setq code (if code (cdr code) 'unknown))
527 (when (stringp score)
528 (if (> (length score) 0)
529 (setq score (string-to-number score))
531 (if score (cons code score) code)))
533 (defconst stgit-file-type-strings
536 (#o160 . "subproject"))
537 "Alist of names of file types")
539 (defun stgit-file-type-string (type)
540 "Return string describing file type TYPE (the high bits of file permission).
541 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
542 (let ((type-str (assoc type stgit-file-type-strings)))
543 (or (and type-str (cdr type-str))
544 (format "unknown type %o" type))))
546 (defun stgit-file-type-change-string (old-perm new-perm)
547 "Return string describing file type change from OLD-PERM to NEW-PERM.
548 Cf. `stgit-file-type-string'."
549 (let ((old-type (lsh old-perm -9))
550 (new-type (lsh new-perm -9)))
551 (cond ((= old-type new-type) "")
552 ((zerop new-type) "")
554 (if (= new-type #o100)
556 (format "(%s)" (stgit-file-type-string new-type))))
557 (t (format "(%s -> %s)"
558 (stgit-file-type-string old-type)
559 (stgit-file-type-string new-type))))))
561 (defun stgit-file-mode-change-string (old-perm new-perm)
562 "Return string describing file mode change from OLD-PERM to NEW-PERM.
563 Cf. `stgit-file-type-change-string'."
564 (setq old-perm (logand old-perm #o777)
565 new-perm (logand new-perm #o777))
566 (if (or (= old-perm new-perm)
570 (let* ((modified (logxor old-perm new-perm))
571 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
572 (cond ((zerop modified) "")
573 ((and (zerop not-x-modified)
574 (or (and (eq #o111 (logand old-perm #o111))
575 (propertize "-x" 'face 'stgit-file-permission-face))
576 (and (eq #o111 (logand new-perm #o111))
577 (propertize "+x" 'face
578 'stgit-file-permission-face)))))
579 (t (concat (propertize (format "%o" old-perm)
580 'face 'stgit-file-permission-face)
582 'face 'stgit-description-face)
583 (propertize (format "%o" new-perm)
584 'face 'stgit-file-permission-face)))))))
586 (defstruct (stgit-file
587 (:conc-name stgit-file->))
588 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
590 (defun stgit-describe-copy-or-rename (file)
591 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
592 from to common-head common-tail)
594 (when stgit-abbreviate-copies-and-renames
595 (setq from (split-string (stgit-file->cr-from file) "/")
596 to (split-string (stgit-file->cr-to file) "/"))
598 (while (and from to (cdr from) (cdr to)
599 (string-equal (car from) (car to)))
600 (setq common-head (cons (car from) common-head)
603 (setq common-head (nreverse common-head)
606 (while (and from to (cdr from) (cdr to)
607 (string-equal (car from) (car to)))
608 (setq common-tail (cons (car from) common-tail)
611 (setq from (nreverse from)
614 (if (or common-head common-tail)
615 (concat (if common-head
616 (mapconcat #'identity common-head "/")
618 (if common-head "/" "")
619 (propertize "{" 'face 'stgit-description-face)
620 (mapconcat #'identity from "/")
622 (mapconcat #'identity to "/")
623 (propertize "}" 'face 'stgit-description-face)
624 (if common-tail "/" "")
626 (mapconcat #'identity common-tail "/")
628 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
630 (defun stgit-file-pp (file)
631 (let ((start (point))
632 (spec (format-spec-make
633 ?s (stgit-file-status-code-as-string file)
634 ?m (stgit-file-mode-change-string
635 (stgit-file->old-perm file)
636 (stgit-file->new-perm file))
637 ?n (if (stgit-file->copy-or-rename file)
638 (stgit-describe-copy-or-rename file)
639 (stgit-file->file file))
640 ?c (propertize (stgit-file-type-change-string
641 (stgit-file->old-perm file)
642 (stgit-file->new-perm file))
643 'face 'stgit-description-face))))
644 (insert (format-spec stgit-file-line-format spec) "\n")
645 (add-text-properties start (point)
646 (list 'entry-type 'file
649 (defun stgit-find-copies-harder-diff-arg ()
650 "Return the flag to use with `git-diff' depending on the
651 `stgit-find-copies-harder' flag."
652 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
654 (defun stgit-insert-ls-files (args file-flag)
655 (let ((start (point)))
656 (apply 'stgit-run-git
657 (append '("ls-files" "--exclude-standard" "-z") args))
659 (while (looking-at "\\([^\0]*\\)\0")
660 (let ((name-len (- (match-end 0) (match-beginning 0))))
661 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
662 (forward-char name-len)))))
664 (defun stgit-process-files (callback)
665 (goto-char (point-min))
666 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
667 (goto-char (match-end 0)))
668 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
669 (let ((old-perm (string-to-number (match-string 1) 8))
670 (new-perm (string-to-number (match-string 2) 8)))
671 (goto-char (match-end 0))
674 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
675 (let* ((patch-status (stgit-patch->status patch))
676 (file-subexp (if (eq patch-status 'unapplied)
679 (file (match-string file-subexp)))
684 :cr-score (string-to-number (match-string 2))
685 :cr-from (match-string 3)
686 :cr-to (match-string 4)
687 :status (stgit-file-status-code
690 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
698 :status (stgit-file-status-code
700 :file (match-string 2))))))
701 (goto-char (match-end 0))
702 (funcall callback file)))))
705 (defun stgit-insert-patch-files (patch)
706 "Expand (show modification of) the patch PATCH after the line
708 (let* ((patchsym (stgit-patch->name patch))
710 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
711 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
712 (set-marker-insertion-type end t)
713 (setf (stgit-patch->files-ewoc patch) ewoc)
715 (let ((standard-output (current-buffer)))
716 (apply 'stgit-run-git
717 (cond ((eq patchsym :work)
718 `("diff-files" "-0" ,@args))
719 ((eq patchsym :index)
720 `("diff-index" ,@args "--cached" "HEAD"))
722 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
724 (when (and (eq patchsym :work))
725 (when stgit-show-ignored
726 (stgit-insert-ls-files '("--ignored" "--others") "I"))
727 (when stgit-show-unknown
728 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
731 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
732 (point-min) (point-max)))
734 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
736 (unless (ewoc-nth ewoc 0)
739 (propertize "<no files>"
740 'face 'stgit-description-face)
744 (defun stgit-find-file (&optional other-window)
745 (let* ((file (or (stgit-patched-file-at-point)
746 (error "No file at point")))
747 (filename (expand-file-name (stgit-file->file file))))
748 (unless (file-exists-p filename)
749 (error "File does not exist"))
750 (funcall (if other-window 'find-file-other-window 'find-file)
752 (when (eq (stgit-file->status file) 'unmerged)
755 (defun stgit-expand (&optional patches collapse)
756 "Show the contents of marked patches, or the patch at point.
758 See also `stgit-collapse'.
760 Non-interactively, operate on PATCHES, and collapse instead of
761 expand if COLLAPSE is not nil."
762 (interactive (list (stgit-patches-marked-or-at-point t)))
764 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
765 patches stgit-expanded-patches)))
766 (setq stgit-expanded-patches
768 (set-difference stgit-expanded-patches patches-diff)
769 (append stgit-expanded-patches patches-diff)))
770 (ewoc-map #'(lambda (patch)
771 (memq (stgit-patch->name patch) patches-diff))
773 (move-to-column (stgit-goal-column)))
775 (defun stgit-collapse (&optional patches)
776 "Hide the contents of marked patches, or the patch at point.
778 See also `stgit-expand'."
779 (interactive (list (stgit-patches-marked-or-at-point t)))
781 (stgit-expand patches t))
783 (defun stgit-select-patch ()
784 (let ((patchname (stgit-patch-name-at-point)))
785 (stgit-expand (list patchname)
786 (memq patchname stgit-expanded-patches))))
788 (defun stgit-expand-directory (file)
789 (let* ((patch (stgit-patch-at-point))
790 (ewoc (stgit-patch->files-ewoc patch))
791 (node (ewoc-locate ewoc))
792 (filename (stgit-file->file file))
793 (start (make-marker))
798 (set-marker start (point))
799 (set-marker end (point))
800 (set-marker-insertion-type end t))
802 (assert (string-match "/$" filename))
803 ;; remove trailing "/"
804 (setf (stgit-file->file file) (substring filename 0 -1))
805 (ewoc-invalidate ewoc node)
808 (let ((standard-output (current-buffer)))
809 (stgit-insert-ls-files (list "--directory" "--others"
810 "--no-empty-directory" "--"
813 (stgit-process-files (lambda (f)
814 (setq node (ewoc-enter-after ewoc node f))))))
816 (let ((inhibit-read-only t))
817 (put-text-property start end 'patch-data patch))))
819 (defun stgit-select-file ()
820 (let* ((file (or (stgit-patched-file-at-point)
821 (error "No file at point")))
822 (filename (stgit-file->file file)))
823 (if (string-match "/$" filename)
824 (stgit-expand-directory file)
827 (defun stgit-select ()
828 "With point on a patch, toggle showing files in the patch.
830 With point on a file, open the associated file. Opens the target
831 file for (applied) copies and renames."
834 (case (get-text-property (point) 'entry-type)
836 (stgit-select-patch))
840 (error "No patch or file on line"))))
842 (defun stgit-find-file-other-window ()
843 "Open file at point in other window"
848 (defun stgit-find-file-merge ()
849 "Open file at point and merge it using `smerge-ediff'."
856 "Hide the stgit buffer."
861 (defun stgit-git-status ()
862 "Show status using `git-status'."
865 (unless (fboundp 'git-status)
866 (error "The stgit-git-status command requires git-status"))
867 (let ((dir default-directory))
868 (save-selected-window
872 (defun stgit-goal-column ()
873 "Return goal column for the current line"
874 (case (get-text-property (point) 'entry-type)
879 (defun stgit-next-line (&optional arg)
880 "Move cursor vertically down ARG lines"
884 (move-to-column (stgit-goal-column)))
886 (defun stgit-previous-line (&optional arg)
887 "Move cursor vertically up ARG lines"
891 (move-to-column (stgit-goal-column)))
893 (defun stgit-next-patch (&optional arg)
894 "Move cursor down ARG patches."
897 (ewoc-goto-next stgit-ewoc (or arg 1))
898 (move-to-column goal-column))
900 (defun stgit-previous-patch (&optional arg)
901 "Move cursor up ARG patches."
904 (ewoc-goto-prev stgit-ewoc (or arg 1))
905 (move-to-column goal-column))
907 (defvar stgit-mode-hook nil
908 "Run after `stgit-mode' is setup.")
910 (defvar stgit-mode-map nil
911 "Keymap for StGit major mode.")
913 (unless stgit-mode-map
914 (let ((diff-map (make-sparse-keymap))
915 (toggle-map (make-sparse-keymap)))
916 (suppress-keymap diff-map)
917 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
918 '(("b" . stgit-diff-base)
919 ("c" . stgit-diff-combined)
920 ("m" . stgit-find-file-merge)
921 ("o" . stgit-diff-ours)
922 ("t" . stgit-diff-theirs)))
923 (suppress-keymap toggle-map)
924 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
925 '(("n" . stgit-toggle-patch-names)
926 ("t" . stgit-toggle-worktree)
927 ("i" . stgit-toggle-ignored)
928 ("u" . stgit-toggle-unknown)))
929 (setq stgit-mode-map (make-keymap))
930 (suppress-keymap stgit-mode-map)
931 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
932 `((" " . stgit-mark-down)
933 ("m" . stgit-mark-down)
934 ("\d" . stgit-unmark-up)
935 ("u" . stgit-unmark-down)
938 ("\C-p" . stgit-previous-line)
939 ("\C-n" . stgit-next-line)
940 ([up] . stgit-previous-line)
941 ([down] . stgit-next-line)
942 ("p" . stgit-previous-patch)
943 ("n" . stgit-next-patch)
944 ("\M-{" . stgit-previous-patch)
945 ("\M-}" . stgit-next-patch)
946 ("s" . stgit-git-status)
947 ("g" . stgit-reload-or-repair)
948 ("r" . stgit-refresh)
949 ("\C-c\C-r" . stgit-rename)
951 ("M" . stgit-move-patches)
954 ("c" . stgit-new-and-refresh)
955 ("\C-c\C-c" . stgit-commit)
956 ("\C-c\C-u" . stgit-uncommit)
958 ("R" . stgit-resolve-file)
959 ("\r" . stgit-select)
961 ("-" . stgit-collapse)
962 ("o" . stgit-find-file-other-window)
963 ("i" . stgit-toggle-index)
964 (">" . stgit-push-next)
965 ("<" . stgit-pop-next)
966 ("P" . stgit-push-or-pop)
970 ([?\C-/] . stgit-undo)
971 ("\C-_" . stgit-undo)
972 ([?\C-c ?\C-/] . stgit-redo)
973 ("\C-c\C-_" . stgit-redo)
975 ("\C-c\C-b" . stgit-rebase)
978 ("q" . stgit-quit))))
980 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
981 (and file (eq (stgit-file->status file)
983 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
984 (easy-menu-define stgit-menu stgit-mode-map
987 ["Reload" stgit-reload-or-repair
988 :help "Reload StGit status from disk"]
989 ["Repair" stgit-repair
990 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
991 :help "Repair StGit metadata"]
993 ["Undo" stgit-undo t]
994 ["Redo" stgit-redo t]
996 ["Git status" stgit-git-status :active (fboundp 'git-status)]
998 ["New patch" stgit-new-and-refresh
999 :help "Create a new patch from changes in index or work tree"
1000 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1001 ["New empty patch" stgit-new
1002 :help "Create a new, empty patch"]
1003 ["(Un)mark patch" stgit-toggle-mark
1004 :label (if (memq (stgit-patch-name-at-point nil t)
1005 stgit-marked-patches)
1006 "Unmark patch" "Mark patch")
1007 :active (stgit-patch-name-at-point nil t)]
1008 ["Expand/collapse patch"
1009 (let ((patches (stgit-patches-marked-or-at-point)))
1010 (if (member-if ,patch-collapsed-p patches)
1011 (stgit-expand patches)
1012 (stgit-collapse patches)))
1013 :label (if (member-if ,patch-collapsed-p
1014 (stgit-patches-marked-or-at-point))
1017 :active (stgit-patches-marked-or-at-point)]
1018 ["Edit patch" stgit-edit
1019 :help "Edit patch comment"
1020 :active (stgit-patch-name-at-point nil t)]
1021 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1022 ["Push/pop patch" stgit-push-or-pop
1023 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1024 (stgit-applied-patchsyms t))
1025 "Pop patches" "Push patches")]
1026 ["Delete patches" stgit-delete
1027 :active (stgit-patches-marked-or-at-point nil t)]
1029 ["Move patches" stgit-move-patches
1030 :active stgit-marked-patches
1031 :help "Move marked patch(es) to point"]
1032 ["Squash patches" stgit-squash
1033 :active (> (length stgit-marked-patches) 1)
1034 :help "Merge marked patches into one"]
1036 ["Refresh top patch" stgit-refresh
1037 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1038 :help "Refresh the top patch with changes in index or work tree"]
1039 ["Refresh this patch" (stgit-refresh t)
1040 :keys "\\[universal-argument] \\[stgit-refresh]"
1041 :help "Refresh marked patch with changes in index or work tree"
1042 :active (and (not (and (stgit-index-empty-p)
1043 (stgit-work-tree-empty-p)))
1044 (stgit-patch-name-at-point nil t))]
1046 ["Find file" stgit-select
1047 :active (eq (get-text-property (point) 'entry-type) 'file)]
1048 ["Open file" stgit-find-file-other-window
1049 :active (eq (get-text-property (point) 'entry-type) 'file)]
1050 ["Toggle file index" stgit-toggle-index
1051 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1052 (memq (stgit-patch-name-at-point) '(:work :index)))
1053 :label (if (eq (stgit-patch-name-at-point) :work)
1054 "Move change to index"
1055 "Move change to work tree")]
1057 ["Show diff" stgit-diff
1058 :active (get-text-property (point) 'entry-type)]
1060 :active (stgit-git-index-unmerged-p)
1061 ["Combined diff" stgit-diff-combined
1062 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1063 ["Diff against base" stgit-diff-base
1064 :help "Show diff against the common base"
1065 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1066 ["Diff against ours" stgit-diff-ours
1067 :help "Show diff against our branch"
1068 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1069 ["Diff against theirs" stgit-diff-theirs
1070 :help "Show diff against their branch"
1071 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1073 ["Interactive merge" stgit-find-file-merge
1074 :help "Interactively merge the file"
1075 :active ,at-unmerged-file]
1076 ["Resolve file" stgit-resolve-file
1077 :help "Mark file conflict as resolved"
1078 :active ,at-unmerged-file]
1081 ["Show index & work tree" stgit-toggle-worktree :style toggle
1082 :selected stgit-show-worktree]
1083 ["Show unknown files" stgit-toggle-unknown :style toggle
1084 :selected stgit-show-unknown :active stgit-show-worktree]
1085 ["Show ignored files" stgit-toggle-ignored :style toggle
1086 :selected stgit-show-ignored :active stgit-show-worktree]
1087 ["Show patch names" stgit-toggle-patch-names :style toggle
1088 :selected stgit-show-patch-names]
1090 ["Switch branches" stgit-branch t
1091 :help "Switch to another branch"]
1092 ["Rebase branch" stgit-rebase t
1093 :help "Rebase the current branch"]
1096 ;; disable tool bar editing buttons
1097 (put 'stgit-mode 'mode-class 'special)
1099 (defun stgit-mode ()
1100 "Major mode for interacting with StGit.
1102 Start StGit using \\[stgit].
1106 \\[stgit-help] Show this help text
1107 \\[stgit-quit] Hide the StGit buffer
1108 \\[describe-bindings] Show all key bindings
1110 \\[stgit-reload-or-repair] Reload the StGit buffer
1111 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1113 \\[stgit-undo] Undo most recent StGit operation
1114 \\[stgit-redo] Undo recent undo
1116 \\[stgit-git-status] Run `git-status' (if available)
1119 \\[stgit-previous-line] Move to previous line
1120 \\[stgit-next-line] Move to next line
1121 \\[stgit-previous-patch] Move to previous patch
1122 \\[stgit-next-patch] Move to next patch
1124 \\[stgit-mark-down] Mark patch and move down
1125 \\[stgit-unmark-up] Unmark patch and move up
1126 \\[stgit-unmark-down] Unmark patch and move down
1128 Commands for patches:
1129 \\[stgit-select] Toggle showing changed files in patch
1130 \\[stgit-refresh] Refresh patch with changes in index or work tree
1131 \\[stgit-diff] Show the patch log and diff
1133 \\[stgit-expand] Show changes in marked patches
1134 \\[stgit-collapse] Hide changes in marked patches
1136 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1137 \\[stgit-new] Create a new, empty patch
1139 \\[stgit-rename] Rename patch
1140 \\[stgit-edit] Edit patch description
1141 \\[stgit-delete] Delete patch(es)
1143 \\[stgit-revert] Revert all changes in index or work tree
1144 \\[stgit-toggle-index] Toggle all changes between index and work tree
1146 \\[stgit-push-next] Push next patch onto stack
1147 \\[stgit-pop-next] Pop current patch from stack
1148 \\[stgit-push-or-pop] Push or pop marked patches
1149 \\[stgit-goto] Make patch at point current by popping or pushing
1151 \\[stgit-squash] Squash (meld together) patches
1152 \\[stgit-move-patches] Move marked patches to point
1154 \\[stgit-commit] Commit patch(es)
1155 \\[stgit-uncommit] Uncommit patch(es)
1158 \\[stgit-select] Open the file in this window
1159 \\[stgit-find-file-other-window] Open the file in another window
1160 \\[stgit-diff] Show the file's diff
1162 \\[stgit-toggle-index] Toggle change between index and work tree
1163 \\[stgit-revert] Revert changes to file
1166 \\[stgit-toggle-patch-names] Toggle showing patch names
1167 \\[stgit-toggle-worktree] Toggle showing index and work tree
1168 \\[stgit-toggle-unknown] Toggle showing unknown files
1169 \\[stgit-toggle-ignored] Toggle showing ignored files
1172 \\[stgit-diff] Show diff of patch or file
1173 \\[stgit-diff-base] Show diff against the merge base
1174 \\[stgit-diff-ours] Show diff against our branch
1175 \\[stgit-diff-theirs] Show diff against their branch
1177 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1178 ignore space changes.
1179 With two prefix arguments (e.g., \\[universal-argument] \
1180 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1182 Commands for merge conflicts:
1183 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1184 \\[stgit-resolve-file] Mark unmerged file as resolved
1186 Commands for branches:
1187 \\[stgit-branch] Switch to another branch
1188 \\[stgit-rebase] Rebase the current branch
1190 Customization variables:
1191 `stgit-abbreviate-copies-and-renames'
1192 `stgit-default-show-patch-names'
1193 `stgit-default-show-worktree'
1194 `stgit-find-copies-harder'
1195 `stgit-show-worktree-mode'
1197 See also \\[customize-group] for the \"stgit\" group."
1198 (kill-all-local-variables)
1199 (buffer-disable-undo)
1200 (setq mode-name "StGit"
1201 major-mode 'stgit-mode
1203 (use-local-map stgit-mode-map)
1204 (set (make-local-variable 'list-buffers-directory) default-directory)
1205 (set (make-local-variable 'stgit-marked-patches) nil)
1206 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1207 (set (make-local-variable 'stgit-show-patch-names)
1208 stgit-default-show-patch-names)
1209 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1210 (set (make-local-variable 'stgit-index-node) nil)
1211 (set (make-local-variable 'stgit-worktree-node) nil)
1212 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1213 (set-variable 'truncate-lines 't)
1214 (add-hook 'after-save-hook 'stgit-update-saved-file)
1215 (run-hooks 'stgit-mode-hook))
1217 (defun stgit-update-saved-file ()
1218 (let* ((file (expand-file-name buffer-file-name))
1219 (dir (file-name-directory file))
1220 (gitdir (condition-case nil (git-get-top-dir dir)
1222 (buffer (and gitdir (stgit-find-buffer gitdir))))
1224 (with-current-buffer buffer
1225 (stgit-refresh-worktree)))))
1227 (defun stgit-add-mark (patchsym)
1228 "Mark the patch PATCHSYM."
1229 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1231 (defun stgit-remove-mark (patchsym)
1232 "Unmark the patch PATCHSYM."
1233 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1235 (defun stgit-clear-marks ()
1236 "Unmark all patches."
1237 (setq stgit-marked-patches '()))
1239 (defun stgit-patch-at-point (&optional cause-error)
1240 (get-text-property (point) 'patch-data))
1242 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1243 "Return the patch name on the current line as a symbol.
1244 If CAUSE-ERROR is not nil, signal an error if none found.
1245 If ONLY-PATCHES is not nil, only allow real patches, and not
1246 index or work tree."
1247 (let ((patch (stgit-patch-at-point)))
1250 (memq (stgit-patch->status patch) '(work index))
1253 (stgit-patch->name patch))
1255 (error "No patch on this line")))))
1257 (defun stgit-patched-file-at-point ()
1258 (get-text-property (point) 'file-data))
1260 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1261 "Return the symbols of the marked patches, or the patch on the current line.
1262 If CAUSE-ERRROR is not nil, signal an error if none found.
1263 If ONLY-PATCHES is not nil, do not include index or work tree."
1264 (if stgit-marked-patches
1265 stgit-marked-patches
1266 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1267 (cond (patch (list patch))
1268 (cause-error (error "No patches marked or at this line"))
1271 (defun stgit-goto-patch (patchsym &optional file)
1272 "Move point to the line containing patch PATCHSYM.
1273 If that patch cannot be found, do nothing.
1275 If the patch was found and FILE is not nil, instead move to that
1276 file's line. If FILE cannot be found, stay on the line of
1278 (let ((node (ewoc-nth stgit-ewoc 0)))
1279 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1281 (setq node (ewoc-next stgit-ewoc node)))
1282 (when (and node file)
1283 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1284 (file-node (ewoc-nth file-ewoc 0)))
1285 (while (and file-node (not (equal (stgit-file->file (ewoc-data file-node)) file)))
1286 (setq file-node (ewoc-next file-ewoc file-node)))
1288 (ewoc-goto-node file-ewoc file-node)
1289 (move-to-column (stgit-goal-column))
1292 (ewoc-goto-node stgit-ewoc node)
1293 (move-to-column goal-column))))
1295 (defun stgit-init ()
1299 (stgit-capture-output nil
1303 (defun stgit-toggle-mark ()
1304 "Toggle mark on the patch under point."
1307 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1311 (defun stgit-mark ()
1312 "Mark the patch under point."
1315 (let* ((node (ewoc-locate stgit-ewoc))
1316 (patch (ewoc-data node))
1317 (name (stgit-patch->name patch)))
1318 (when (eq name :work)
1319 (error "Cannot mark the work tree"))
1320 (when (eq name :index)
1321 (error "Cannot mark the index"))
1322 (stgit-add-mark (stgit-patch->name patch))
1323 (let ((column (current-column)))
1324 (ewoc-invalidate stgit-ewoc node)
1325 (move-to-column column))))
1327 (defun stgit-mark-down ()
1328 "Mark the patch under point and move to the next patch."
1333 (defun stgit-unmark ()
1334 "Remove mark from the patch on the current line."
1337 (let* ((node (ewoc-locate stgit-ewoc))
1338 (patch (ewoc-data node)))
1339 (stgit-remove-mark (stgit-patch->name patch))
1340 (let ((column (current-column)))
1341 (ewoc-invalidate stgit-ewoc node)
1342 (move-to-column column))))
1344 (defun stgit-unmark-up ()
1345 "Remove mark from the patch on the previous line."
1348 (stgit-previous-patch)
1351 (defun stgit-unmark-down ()
1352 "Remove mark from the patch on the current line."
1358 (defun stgit-rename (name)
1359 "Rename the patch under point to NAME."
1361 (read-string "Patch name: "
1362 (symbol-name (stgit-patch-name-at-point t t)))))
1364 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1365 (stgit-capture-output nil
1366 (stgit-run "rename" old-patchsym name))
1367 (let ((name-sym (intern name)))
1368 (when (memq old-patchsym stgit-expanded-patches)
1369 (setq stgit-expanded-patches
1370 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1371 (when (memq old-patchsym stgit-marked-patches)
1372 (setq stgit-marked-patches
1373 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1375 (stgit-goto-patch name-sym))))
1377 (defun stgit-reload-or-repair (repair)
1378 "Update the contents of the StGit buffer (`stgit-reload').
1380 With a prefix argument, repair the StGit metadata if the branch
1381 was modified with git commands (`stgit-repair')."
1388 (defun stgit-repair ()
1392 (stgit-capture-output nil
1393 (stgit-run "repair"))
1396 (defun stgit-available-branches ()
1397 "Returns a list of the available stg branches"
1398 (let ((output (with-output-to-string
1399 (stgit-run "branch" "--list")))
1402 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1403 (setq result (cons (match-string 1 output) result))
1404 (setq start (match-end 0)))
1407 (defun stgit-branch (branch)
1408 "Switch to branch BRANCH."
1409 (interactive (list (completing-read "Switch to branch: "
1410 (stgit-available-branches))))
1412 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1415 (defun stgit-available-refs (&optional omit-stgit)
1416 "Returns a list of the available git refs.
1417 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1418 (let* ((output (with-output-to-string
1419 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1420 "refs/tags" "refs/heads"
1422 (result (split-string output "\n" t)))
1424 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1425 (substring s (match-end 0))
1428 (delete-if (lambda (s)
1429 (string-match "^refs/heads/.*\\.stgit$" s))
1433 (defun stgit-rebase (new-base)
1434 "Rebase to NEW-BASE."
1435 (interactive (list (completing-read "Rebase to: "
1436 (stgit-available-refs t))))
1438 (stgit-capture-output nil (stgit-run "rebase" new-base))
1441 (defun stgit-commit (count)
1442 "Run stg commit on COUNT commits.
1443 Interactively, the prefix argument is used as COUNT.
1444 A negative COUNT will uncommit instead."
1448 (stgit-uncommit (- count))
1449 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1452 (defun stgit-uncommit (count)
1453 "Run stg uncommit on COUNT commits.
1454 Interactively, the prefix argument is used as COUNT.
1455 A negative COUNT will commit instead."
1459 (stgit-commit (- count))
1460 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1463 (defun stgit-neighbour-file ()
1464 "Return the file name of the next file after point, or the
1465 previous file if point is at the last file within a patch."
1466 (let ((old-point (point))
1468 (and (zerop (forward-line 1))
1469 (let ((f (stgit-patched-file-at-point)))
1470 (and f (setq neighbour-file (stgit-file->file f)))))
1471 (goto-char old-point)
1472 (unless neighbour-file
1473 (and (zerop (forward-line -1))
1474 (let ((f (stgit-patched-file-at-point)))
1475 (and f (setq neighbour-file (stgit-file->file f)))))
1476 (goto-char old-point))
1479 (defun stgit-revert-file ()
1480 "Revert the file at point, which must be in the index or the
1484 (let* ((patched-file (or (stgit-patched-file-at-point)
1485 (error "No file on the current line")))
1486 (patch-name (stgit-patch-name-at-point))
1487 (file-status (stgit-file->status patched-file))
1488 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1489 (stgit-file->cr-to patched-file))
1490 ((eq file-status 'add)
1491 (stgit-file->file patched-file))))
1492 (co-file (cond ((eq file-status 'rename)
1493 (stgit-file->cr-from patched-file))
1494 ((not (memq file-status '(copy add)))
1495 (stgit-file->file patched-file))))
1496 (next-file (stgit-neighbour-file)))
1498 (unless (memq patch-name '(:work :index))
1499 (error "No index or working tree file on this line"))
1501 (when (eq file-status 'ignore)
1502 (error "Cannot revert ignored files"))
1504 (when (eq file-status 'unknown)
1505 (error "Cannot revert unknown files"))
1507 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1508 (when (yes-or-no-p (format "Revert %d file%s? "
1510 (if (= nfiles 1) "" "s")))
1511 (stgit-capture-output nil
1513 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1515 (stgit-run-git "checkout" "HEAD" co-file)))
1517 (stgit-goto-patch patch-name next-file)))))
1519 (defun stgit-revert ()
1520 "Revert the change at point, which must be the index, the work
1521 tree, or a single change in either."
1524 (let ((patched-file (stgit-patched-file-at-point)))
1527 (let* ((patch-name (or (stgit-patch-name-at-point)
1528 (error "No patch or file at point")))
1529 (patch-desc (case patch-name
1532 (t (error (substitute-command-keys
1533 "Use \\[stgit-delete] to delete a patch"))))))
1534 (when (if (eq patch-name :work)
1535 (stgit-work-tree-empty-p)
1536 (stgit-index-empty-p))
1537 (error (format "There are no changes in the %s to revert"
1539 (and (eq patch-name :index)
1540 (not (stgit-work-tree-empty-p))
1541 (error "Cannot revert index as work tree contains unstaged changes"))
1543 (when (yes-or-no-p (format "Revert all changes in the %s? "
1545 (if (eq patch-name :index)
1546 (stgit-run-git-silent "reset" "--hard" "-q")
1547 (stgit-run-git-silent "checkout" "--" "."))
1548 (stgit-refresh-index)
1549 (stgit-refresh-worktree)
1550 (stgit-goto-patch patch-name))))))
1552 (defun stgit-resolve-file ()
1553 "Resolve conflict in the file at point."
1556 (let* ((patched-file (stgit-patched-file-at-point))
1557 (patch (stgit-patch-at-point))
1558 (patch-name (and patch (stgit-patch->name patch)))
1559 (status (and patched-file (stgit-file->status patched-file))))
1561 (unless (memq patch-name '(:work :index))
1562 (error "No index or working tree file on this line"))
1564 (unless (eq status 'unmerged)
1565 (error "No conflict to resolve at the current line"))
1567 (stgit-capture-output nil
1568 (stgit-move-change-to-index (stgit-file->file patched-file)))
1572 (defun stgit-push-next (npatches)
1573 "Push the first unapplied patch.
1574 With numeric prefix argument, push that many patches."
1577 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1579 (stgit-refresh-git-status))
1581 (defun stgit-pop-next (npatches)
1582 "Pop the topmost applied patch.
1583 With numeric prefix argument, pop that many patches."
1586 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1588 (stgit-refresh-git-status))
1590 (defun stgit-applied-patches (&optional only-patches)
1591 "Return a list of the applied patches.
1593 If ONLY-PATCHES is not nil, exclude index and work tree."
1594 (let ((states (if only-patches
1596 '(applied top index work)))
1598 (ewoc-map (lambda (patch) (when (memq (stgit-patch->status patch) states)
1599 (setq result (cons patch result))))
1603 (defun stgit-applied-patchsyms (&optional only-patches)
1604 "Return a list of the symbols of the applied patches.
1606 If ONLY-PATCHES is not nil, exclude index and work tree."
1607 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1609 (defun stgit-push-or-pop ()
1610 "Push or pop the marked patches."
1613 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1614 (applied-syms (stgit-applied-patchsyms t))
1615 (unapplied (set-difference patchsyms applied-syms)))
1616 (stgit-capture-output nil
1618 (if unapplied "push" "pop")
1620 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1623 (defun stgit-goto ()
1624 "Go to the patch on the current line.
1626 Pops or pushes patches to make this patch topmost."
1629 (let ((patchsym (stgit-patch-name-at-point t)))
1630 (stgit-capture-output nil
1631 (stgit-run "goto" patchsym))
1634 (defun stgit-id (patchsym)
1635 "Return the git commit id for PATCHSYM.
1636 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1637 (if (keywordp patchsym)
1639 (let ((result (with-output-to-string
1640 (stgit-run-silent "id" patchsym))))
1641 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1642 (error "Cannot find commit id for %s" patchsym))
1643 (match-string 1 result))))
1645 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1646 "Show the patch on the current line.
1648 UNMERGED-STAGE is the argument to `git-diff' that that selects
1649 which stage to diff against in the case of unmerged files."
1650 (let ((space-arg (when (numberp ignore-whitespace)
1651 (cond ((> ignore-whitespace 4)
1652 "--ignore-all-space")
1653 ((> ignore-whitespace 1)
1654 "--ignore-space-change"))))
1655 (patch-name (stgit-patch-name-at-point t)))
1656 (stgit-capture-output "*StGit patch*"
1657 (case (get-text-property (point) 'entry-type)
1659 (let* ((patched-file (stgit-patched-file-at-point))
1660 (patch-id (let ((id (stgit-id patch-name)))
1661 (if (and (eq id :index)
1662 (eq (stgit-file->status patched-file)
1666 (args (append (and space-arg (list space-arg))
1667 (and (stgit-file->cr-from patched-file)
1668 (list (stgit-find-copies-harder-diff-arg)))
1669 (cond ((eq patch-id :index)
1671 ((eq patch-id :work)
1672 (list unmerged-stage))
1674 (list (concat patch-id "^") patch-id)))
1676 (if (stgit-file->copy-or-rename patched-file)
1677 (list (stgit-file->cr-from patched-file)
1678 (stgit-file->cr-to patched-file))
1679 (list (stgit-file->file patched-file))))))
1680 (apply 'stgit-run-git "diff" args)))
1682 (let* ((patch-id (stgit-id patch-name)))
1683 (if (or (eq patch-id :index) (eq patch-id :work))
1684 (apply 'stgit-run-git "diff"
1685 (stgit-find-copies-harder-diff-arg)
1686 (append (and space-arg (list space-arg))
1687 (if (eq patch-id :index)
1689 (list unmerged-stage))))
1690 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1691 (and space-arg (list "-O" space-arg))
1692 (list (stgit-patch-name-at-point)))))
1693 (apply 'stgit-run args)))))
1695 (error "No patch or file at point")))
1696 (with-current-buffer standard-output
1697 (goto-char (point-min))
1700 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1701 `(defun ,name (&optional ignore-whitespace)
1702 ,(format "Show the patch on the current line.
1704 %sWith a prefix argument, ignore whitespace. With a prefix argument
1705 greater than four (e.g., \\[universal-argument] \
1706 \\[universal-argument] \\[%s]), ignore all whitespace."
1708 (format "For unmerged files, %s.\n\n" unmerged-action)
1713 (stgit-show-patch ,diff-arg ignore-whitespace)))
1715 (stgit-define-diff stgit-diff
1717 (stgit-define-diff stgit-diff-ours
1719 "diff against our branch")
1720 (stgit-define-diff stgit-diff-theirs
1722 "diff against their branch")
1723 (stgit-define-diff stgit-diff-base
1725 "diff against the merge base")
1726 (stgit-define-diff stgit-diff-combined
1728 "show a combined diff")
1730 (defun stgit-move-change-to-index (file &optional force)
1731 "Copies the work tree state of FILE to index, using git add or git rm.
1733 If FORCE is not nil, use --force."
1734 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1735 '("add") '("rm" "-q"))))
1736 (stgit-capture-output "*git output*"
1737 (apply 'stgit-run-git (append op (and force '("--force"))
1738 '("--") (list file))))))
1740 (defun stgit-remove-change-from-index (file)
1741 "Unstages the change in FILE from the index"
1742 (stgit-capture-output "*git output*"
1743 (stgit-run-git "reset" "-q" "--" file)))
1745 (defun stgit-git-index-unmerged-p ()
1747 (with-output-to-string
1748 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1750 "--quiet" "HEAD")))))
1753 (defun stgit-file-toggle-index ()
1754 "Move modified file in or out of the index.
1756 Leaves the point where it is, but moves the mark to where the
1757 file ended up. You can then jump to the file with \
1758 \\[exchange-point-and-mark]."
1761 (let* ((patched-file (or (stgit-patched-file-at-point)
1762 (error "No file on the current line")))
1763 (patched-status (stgit-file->status patched-file)))
1764 (when (eq patched-status 'unmerged)
1765 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1766 (let* ((patch (stgit-patch-at-point))
1767 (patch-name (stgit-patch->name patch))
1768 (mark-file (if (eq patched-status 'rename)
1769 (stgit-file->cr-to patched-file)
1770 (stgit-file->file patched-file)))
1771 (point-file (if (eq patched-status 'rename)
1772 (stgit-file->cr-from patched-file)
1773 (stgit-neighbour-file))))
1775 (cond ((eq patch-name :work)
1776 (stgit-move-change-to-index (stgit-file->file patched-file)
1777 (eq patched-status 'ignore)))
1778 ((eq patch-name :index)
1779 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1781 (error "Can only move files between working tree and index")))
1782 (stgit-refresh-worktree)
1783 (stgit-refresh-index)
1784 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1786 (stgit-goto-patch patch-name point-file))))
1788 (defun stgit-toggle-index ()
1789 "Move change in or out of the index.
1791 Works on index and work tree, as well as files in either.
1793 Leaves the point where it is, but moves the mark to where the
1794 file ended up. You can then jump to the file with \
1795 \\[exchange-point-and-mark]."
1798 (if (stgit-patched-file-at-point)
1799 (stgit-file-toggle-index)
1800 (let ((patch-name (stgit-patch-name-at-point)))
1801 (unless (memq patch-name '(:index :work))
1802 (error "Can only move changes between working tree and index"))
1803 (when (stgit-git-index-unmerged-p)
1804 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1805 (if (if (eq patch-name :index)
1806 (stgit-index-empty-p)
1807 (stgit-work-tree-empty-p))
1808 (message "No changes to be moved")
1809 (stgit-capture-output nil
1810 (if (eq patch-name :work)
1811 (stgit-run-git "add" "--update")
1812 (stgit-run-git "reset" "--mixed" "-q")))
1813 (stgit-refresh-worktree)
1814 (stgit-refresh-index))
1815 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1817 (defun stgit-edit ()
1818 "Edit the patch on the current line."
1821 (let ((patchsym (stgit-patch-name-at-point t t))
1822 (edit-buf (get-buffer-create "*StGit edit*"))
1823 (dir default-directory))
1824 (log-edit 'stgit-confirm-edit t nil edit-buf)
1825 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1826 (setq default-directory dir)
1827 (let ((standard-output edit-buf))
1829 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1831 (defun stgit-confirm-edit ()
1833 (let ((file (make-temp-file "stgit-edit-")))
1834 (write-region (point-min) (point-max) file)
1835 (stgit-capture-output nil
1836 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1837 (with-current-buffer log-edit-parent-buffer
1840 (defun stgit-new (add-sign &optional refresh)
1841 "Create a new patch.
1842 With a prefix argument, include a \"Signed-off-by:\" line at the
1846 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1847 (dir default-directory))
1848 (log-edit 'stgit-confirm-new t nil edit-buf)
1849 (setq default-directory dir)
1850 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1853 (let ((standard-output (current-buffer)))
1854 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1856 (defun stgit-confirm-new ()
1858 (let ((file (make-temp-file "stgit-edit-"))
1859 (refresh stgit-refresh-after-new))
1860 (write-region (point-min) (point-max) file)
1861 (stgit-capture-output nil
1862 (stgit-run "new" "-f" file))
1863 (with-current-buffer log-edit-parent-buffer
1868 (defun stgit-new-and-refresh (add-sign)
1869 "Create a new patch and refresh it with the current changes.
1871 With a prefix argument, include a \"Signed-off-by:\" line at the
1874 This works just like running `stgit-new' followed by `stgit-refresh'."
1877 (stgit-new add-sign t))
1879 (defun stgit-create-patch-name (description)
1880 "Create a patch name from a long description"
1882 (while (> (length description) 0)
1883 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1884 (setq patch (downcase (concat patch
1885 (match-string 0 description))))
1886 (setq description (substring description (match-end 0))))
1887 ((string-match "\\` +" description)
1888 (setq patch (concat patch "-"))
1889 (setq description (substring description (match-end 0))))
1890 ((string-match "\\`[^a-zA-Z_-]+" description)
1891 (setq description (substring description (match-end 0))))))
1892 (cond ((= (length patch) 0)
1894 ((> (length patch) 20)
1895 (substring patch 0 20))
1898 (defun stgit-delete (patchsyms &optional spill-p)
1899 "Delete the patches in PATCHSYMS.
1900 Interactively, delete the marked patches, or the patch at point.
1902 With a prefix argument, or SPILL-P, spill the patch contents to
1903 the work tree and index."
1904 (interactive (list (stgit-patches-marked-or-at-point t t)
1905 current-prefix-arg))
1908 (error "No patches to delete"))
1909 (when (memq :index patchsyms)
1910 (error "Cannot delete the index"))
1911 (when (memq :work patchsyms)
1912 (error "Cannot delete the work tree"))
1914 (let ((npatches (length patchsyms)))
1915 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1917 (if (= 1 npatches) "" "es")
1919 " (spilling contents to index)"
1921 (let ((args (if spill-p
1922 (cons "--spill" patchsyms)
1924 (stgit-capture-output nil
1925 (apply 'stgit-run "delete" args))
1928 (defun stgit-move-patches-target ()
1929 "Return the patchsym indicating a target patch for
1930 `stgit-move-patches'.
1932 This is either the first unmarked patch at or after point, or one
1933 of :top and :bottom if the point is after or before the applied
1939 (let ((patchsym (stgit-patch-name-at-point)))
1940 (cond ((memq patchsym '(:work :index)) (setq result :top))
1941 (patchsym (if (memq patchsym stgit-marked-patches)
1943 (setq result patchsym)))
1944 ((re-search-backward "^>" nil t) (setq result :top))
1945 (t (setq result :bottom)))))
1948 (defun stgit-sort-patches (patchsyms)
1949 "Returns the list of patches in PATCHSYMS sorted according to
1950 their position in the patch series, bottommost first.
1952 PATCHSYMS must not contain duplicate entries."
1953 (let (sorted-patchsyms
1954 (series (with-output-to-string
1955 (with-current-buffer standard-output
1956 (stgit-run-silent "series" "--noprefix"))))
1958 (while (string-match "^\\(.+\\)" series start)
1959 (let ((patchsym (intern (match-string 1 series))))
1960 (when (memq patchsym patchsyms)
1961 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1962 (setq start (match-end 0)))
1963 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1965 (unless (= (length patchsyms) (length sorted-patchsyms))
1966 (error "Internal error"))
1970 (defun stgit-move-patches (patchsyms target-patch)
1971 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1972 If TARGET-PATCH is :bottom or :top, move the patches to the
1973 bottom or top of the stack, respectively.
1975 Interactively, move the marked patches to where the point is."
1976 (interactive (list stgit-marked-patches
1977 (stgit-move-patches-target)))
1980 (error "Need at least one patch to move"))
1982 (unless target-patch
1983 (error "Point not at a patch"))
1985 ;; need to have patchsyms sorted by position in the stack
1986 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1987 (stgit-capture-output nil
1988 (if (eq target-patch :top)
1989 (apply 'stgit-run "float" sorted-patchsyms)
1992 (append (unless (eq target-patch :bottom)
1993 (list "--to" target-patch))
1995 sorted-patchsyms)))))
1998 (defun stgit-squash (patchsyms)
1999 "Squash the patches in PATCHSYMS.
2000 Interactively, squash the marked patches.
2002 Unless there are any conflicts, the patches will be merged into
2003 one patch, which will occupy the same spot in the series as the
2004 deepest patch had before the squash."
2005 (interactive (list stgit-marked-patches))
2007 (when (< (length patchsyms) 2)
2008 (error "Need at least two patches to squash"))
2009 (let ((stgit-buffer (current-buffer))
2010 (edit-buf (get-buffer-create "*StGit edit*"))
2011 (dir default-directory)
2012 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2013 (log-edit 'stgit-confirm-squash t nil edit-buf)
2014 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2015 (setq default-directory dir)
2016 (let ((result (let ((standard-output edit-buf))
2018 (apply 'stgit-run-silent "squash"
2019 "--save-template=-" sorted-patchsyms)))))
2021 ;; stg squash may have reordered the patches or caused conflicts
2022 (with-current-buffer stgit-buffer
2025 (unless (eq 0 result)
2027 (rename-buffer "*StGit error*")
2028 (resize-temp-buffer-window)
2029 (switch-to-buffer-other-window stgit-buffer)
2030 (error "stg squash failed")))))
2032 (defun stgit-confirm-squash ()
2034 (let ((file (make-temp-file "stgit-edit-")))
2035 (write-region (point-min) (point-max) file)
2036 (stgit-capture-output nil
2037 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2038 (with-current-buffer log-edit-parent-buffer
2040 ;; Go to first marked patch and stay there
2041 (goto-char (point-min))
2042 (re-search-forward (concat "^[>+-]\\*") nil t)
2043 (move-to-column goal-column)
2044 (let ((pos (point)))
2048 (defun stgit-help ()
2049 "Display help for the StGit mode."
2051 (describe-function 'stgit-mode))
2053 (defun stgit-undo-or-redo (redo hard)
2054 "Run stg undo or, if REDO is non-nil, stg redo.
2056 If HARD is non-nil, use the --hard flag."
2058 (let ((cmd (if redo "redo" "undo")))
2059 (stgit-capture-output nil
2061 (when (or (and (stgit-index-empty-p)
2062 (stgit-work-tree-empty-p))
2063 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2065 (stgit-run cmd "--hard"))
2069 (defun stgit-undo (&optional arg)
2071 With prefix argument, run it with the --hard flag.
2073 See also `stgit-redo'."
2075 (stgit-undo-or-redo nil arg))
2077 (defun stgit-redo (&optional arg)
2079 With prefix argument, run it with the --hard flag.
2081 See also `stgit-undo'."
2083 (stgit-undo-or-redo t arg))
2085 (defun stgit-refresh (&optional arg)
2087 If the index contains any changes, only refresh from index.
2089 With prefix argument, refresh the marked patch or the patch under point."
2092 (let ((patchargs (if arg
2093 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2094 (when (> (length patches) 1)
2095 (error "Too many patches marked"))
2096 (cons "-p" patches))
2098 (unless (stgit-index-empty-p)
2099 (setq patchargs (cons "--index" patchargs)))
2100 (stgit-capture-output nil
2101 (apply 'stgit-run "refresh" patchargs))
2102 (stgit-refresh-git-status))
2105 (defvar stgit-show-worktree nil
2106 "If nil, inhibit showing work tree and index in the stgit buffer.
2108 See also `stgit-show-worktree-mode'.")
2110 (defvar stgit-show-ignored nil
2111 "If nil, inhibit showing files ignored by git.")
2113 (defvar stgit-show-unknown nil
2114 "If nil, inhibit showing files not registered with git.")
2116 (defvar stgit-show-patch-names t
2117 "If nil, inhibit showing patch names.")
2119 (defun stgit-toggle-worktree (&optional arg)
2120 "Toggle the visibility of the work tree.
2121 With ARG, show the work tree if ARG is positive.
2123 Its initial setting is controlled by `stgit-default-show-worktree'.
2125 `stgit-show-worktree-mode' controls where on screen the index and
2126 work tree will show up."
2129 (setq stgit-show-worktree
2132 (not stgit-show-worktree)))
2135 (defun stgit-toggle-ignored (&optional arg)
2136 "Toggle the visibility of files ignored by git in the work
2137 tree. With ARG, show these files if ARG is positive.
2139 Use \\[stgit-toggle-worktree] to show the work tree."
2142 (setq stgit-show-ignored
2145 (not stgit-show-ignored)))
2148 (defun stgit-toggle-unknown (&optional arg)
2149 "Toggle the visibility of files not registered with git in the
2150 work tree. With ARG, show these files if ARG is positive.
2152 Use \\[stgit-toggle-worktree] to show the work tree."
2155 (setq stgit-show-unknown
2158 (not stgit-show-unknown)))
2161 (defun stgit-toggle-patch-names (&optional arg)
2162 "Toggle the visibility of patch names. With ARG, show patch names
2165 The initial setting is controlled by `stgit-default-show-patch-names'."
2168 (setq stgit-show-patch-names
2171 (not stgit-show-patch-names)))