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-insert-without-trailing-whitespace (text)
266 "Insert TEXT in buffer using `insert', without trailing whitespace.
267 A newline is appended."
268 (unless (string-match "\\(.*?\\) *$" text)
270 (insert (match-string 1 text) ?\n))
272 (defun stgit-patch-pp (patch)
273 (let* ((status (stgit-patch->status patch))
275 (name (stgit-patch->name patch))
276 (face (cdr (assq status stgit-patch-status-face-alist)))
277 (fmt (if stgit-show-patch-names
278 stgit-patch-line-format
279 stgit-noname-patch-line-format))
280 (spec (format-spec-make
286 ?m (if (memq name stgit-marked-patches)
288 ?n (propertize (stgit-patch-display-name patch)
290 'syntax-table (string-to-syntax "w"))
291 ?e (if (stgit-patch->empty patch) "(empty) " "")
292 ?d (propertize (or (stgit-patch->desc patch) "")
293 'face 'stgit-description-face)
294 ?D (propertize (or (stgit-patch->desc patch)
295 (stgit-patch-display-name patch))
297 (text (format-spec fmt spec)))
299 (stgit-insert-without-trailing-whitespace text)
300 (put-text-property start (point) 'entry-type 'patch)
301 (when (memq name stgit-expanded-patches)
302 (stgit-insert-patch-files patch))
303 (put-text-property start (point) 'patch-data patch)))
305 (defun create-stgit-buffer (dir)
306 "Create a buffer for showing StGit patches.
307 Argument DIR is the repository path."
308 (let ((buf (create-file-buffer (concat dir "*stgit*")))
309 (inhibit-read-only t))
310 (with-current-buffer buf
311 (setq default-directory dir)
313 (set (make-local-variable 'stgit-ewoc)
314 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
315 (setq buffer-read-only t))
318 (defmacro stgit-capture-output (name &rest body)
319 "Capture StGit output and, if there was any output, show it in a window
321 Returns nil if there was no output."
322 (declare (debug ([&or stringp null] body))
324 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
325 (stgit-dir default-directory)
326 (inhibit-read-only t))
327 (with-current-buffer output-buf
328 (buffer-disable-undo)
330 (setq default-directory stgit-dir)
331 (setq buffer-read-only t))
332 (let ((standard-output output-buf))
334 (with-current-buffer output-buf
335 (set-buffer-modified-p nil)
336 (setq buffer-read-only t)
337 (if (< (point-min) (point-max))
338 (display-buffer output-buf t)))))
340 (defun stgit-make-run-args (args)
341 "Return a copy of ARGS with its elements converted to strings."
343 ;; don't use (format "%s" ...) to limit type errors
344 (cond ((stringp x) x)
345 ((integerp x) (number-to-string x))
346 ((symbolp x) (symbol-name x))
348 (error "Bad element in stgit-make-run-args args: %S" x))))
351 (defun stgit-run-silent (&rest args)
352 (setq args (stgit-make-run-args args))
353 (apply 'call-process "stg" nil standard-output nil args))
355 (defun stgit-run (&rest args)
356 (setq args (stgit-make-run-args args))
357 (let ((msgcmd (mapconcat #'identity args " ")))
358 (message "Running stg %s..." msgcmd)
359 (apply 'call-process "stg" nil standard-output nil args)
360 (message "Running stg %s...done" msgcmd)))
362 (defun stgit-run-git (&rest args)
363 (setq args (stgit-make-run-args args))
364 (let ((msgcmd (mapconcat #'identity args " ")))
365 (message "Running git %s..." msgcmd)
366 (apply 'call-process "git" nil standard-output nil args)
367 (message "Running git %s...done" msgcmd)))
369 (defun stgit-run-git-silent (&rest args)
370 (setq args (stgit-make-run-args args))
371 (apply 'call-process "git" nil standard-output nil args))
373 (defun stgit-index-empty-p ()
374 "Returns non-nil if the index contains no changes from HEAD."
375 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
377 (defun stgit-work-tree-empty-p ()
378 "Returns non-nil if the work tree contains no changes from index."
379 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
381 (defvar stgit-index-node)
382 (defvar stgit-worktree-node)
384 (defconst stgit-allowed-branch-name-re
385 ;; Disallow control characters, space, del, and "/:@^{}~" in
386 ;; "/"-separated parts; parts may not start with a period (.)
387 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
388 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
389 "Regular expression that (new) branch names must match.")
391 (defun stgit-refresh-index ()
392 (when stgit-index-node
393 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
395 (defun stgit-refresh-worktree ()
396 (when stgit-worktree-node
397 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
399 (defun stgit-run-series-insert-index (ewoc)
400 (setq index-node (cons ewoc (ewoc-enter-last ewoc
406 worktree-node (cons ewoc (ewoc-enter-last ewoc
413 (defun stgit-run-series (ewoc)
414 (setq stgit-index-node nil
415 stgit-worktree-node nil)
416 (let ((inserted-index (not stgit-show-worktree))
421 (let* ((standard-output (current-buffer))
422 (exit-status (stgit-run-silent "series"
423 "--description" "--empty")))
424 (goto-char (point-min))
425 (if (not (zerop exit-status))
426 (cond ((looking-at "stg series: \\(.*\\)")
427 (setq inserted-index t)
428 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
429 (substitute-command-keys
430 "-- not initialized; run \\[stgit-init]")))
432 (error "Error running stg: %s"
436 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
437 (error "Syntax error in output from stg series"))
438 (let* ((state-str (match-string 2))
439 (state (cond ((string= state-str ">") 'top)
440 ((string= state-str "+") 'applied)
441 ((string= state-str "-") 'unapplied)))
442 (name (intern (match-string 4)))
443 (desc (match-string 5))
444 (empty (string= (match-string 1) "0")))
445 (unless inserted-index
446 (when (or (eq stgit-show-worktree-mode 'top)
447 (and (eq stgit-show-worktree-mode 'center)
448 (eq state 'unapplied)))
449 (setq inserted-index t)
450 (stgit-run-series-insert-index ewoc)))
451 (setq all-patchsyms (cons name all-patchsyms))
452 (ewoc-enter-last ewoc
459 (unless inserted-index
460 (stgit-run-series-insert-index ewoc)))
461 (setq stgit-index-node index-node
462 stgit-worktree-node worktree-node
463 stgit-marked-patches (intersection stgit-marked-patches
466 (defun stgit-current-branch ()
467 "Return the name of the current branch."
468 (substring (with-output-to-string
469 (stgit-run-silent "branch"))
472 (defun stgit-reload ()
473 "Update the contents of the StGit buffer."
476 (let ((inhibit-read-only t)
477 (curline (line-number-at-pos))
478 (curpatch (stgit-patch-name-at-point))
479 (curfile (stgit-patched-file-at-point)))
480 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
481 (ewoc-set-hf stgit-ewoc
483 (propertize (stgit-current-branch)
484 'face 'stgit-branch-name-face)
486 (if stgit-show-worktree
489 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
490 shows the working tree\n")
491 'face 'stgit-description-face)))
492 (stgit-run-series stgit-ewoc)
494 (stgit-goto-patch curpatch (and curfile (stgit-file->file curfile)))
495 (goto-line curline)))
496 (stgit-refresh-git-status))
498 (defconst stgit-file-status-code-strings
499 (mapcar (lambda (arg)
501 (propertize (cadr arg) 'face (car (cddr arg)))))
502 '((add "Added" stgit-modified-file-face)
503 (copy "Copied" stgit-modified-file-face)
504 (delete "Deleted" stgit-modified-file-face)
505 (modify "Modified" stgit-modified-file-face)
506 (rename "Renamed" stgit-modified-file-face)
507 (mode-change "Mode change" stgit-modified-file-face)
508 (unmerged "Unmerged" stgit-unmerged-file-face)
509 (unknown "Unknown" stgit-unknown-file-face)
510 (ignore "Ignored" stgit-ignored-file-face)))
511 "Alist of code symbols to description strings")
513 (defconst stgit-patch-status-face-alist
514 '((applied . stgit-applied-patch-face)
515 (top . stgit-top-patch-face)
516 (unapplied . stgit-unapplied-patch-face)
517 (index . stgit-index-work-tree-title-face)
518 (work . stgit-index-work-tree-title-face))
519 "Alist of face to use for a given patch status")
521 (defun stgit-file-status-code-as-string (file)
522 "Return stgit status code for FILE as a string"
523 (let* ((code (assq (stgit-file->status file)
524 stgit-file-status-code-strings))
525 (score (stgit-file->cr-score file)))
527 (if (and score (/= score 100))
528 (format "%s %s" (cdr code)
529 (propertize (format "%d%%" score)
530 'face 'stgit-description-face))
533 (defun stgit-file-status-code (str &optional score)
534 "Return stgit status code from git status string"
535 (let ((code (assoc str '(("A" . add)
544 (setq code (if code (cdr code) 'unknown))
545 (when (stringp score)
546 (if (> (length score) 0)
547 (setq score (string-to-number score))
549 (if score (cons code score) code)))
551 (defconst stgit-file-type-strings
554 (#o160 . "subproject"))
555 "Alist of names of file types")
557 (defun stgit-file-type-string (type)
558 "Return string describing file type TYPE (the high bits of file permission).
559 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
560 (let ((type-str (assoc type stgit-file-type-strings)))
561 (or (and type-str (cdr type-str))
562 (format "unknown type %o" type))))
564 (defun stgit-file-type-change-string (old-perm new-perm)
565 "Return string describing file type change from OLD-PERM to NEW-PERM.
566 Cf. `stgit-file-type-string'."
567 (let ((old-type (lsh old-perm -9))
568 (new-type (lsh new-perm -9)))
569 (cond ((= old-type new-type) "")
570 ((zerop new-type) "")
572 (if (= new-type #o100)
574 (format "(%s)" (stgit-file-type-string new-type))))
575 (t (format "(%s -> %s)"
576 (stgit-file-type-string old-type)
577 (stgit-file-type-string new-type))))))
579 (defun stgit-file-mode-change-string (old-perm new-perm)
580 "Return string describing file mode change from OLD-PERM to NEW-PERM.
581 Cf. `stgit-file-type-change-string'."
582 (setq old-perm (logand old-perm #o777)
583 new-perm (logand new-perm #o777))
584 (if (or (= old-perm new-perm)
588 (let* ((modified (logxor old-perm new-perm))
589 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
590 (cond ((zerop modified) "")
591 ((and (zerop not-x-modified)
592 (or (and (eq #o111 (logand old-perm #o111))
593 (propertize "-x" 'face 'stgit-file-permission-face))
594 (and (eq #o111 (logand new-perm #o111))
595 (propertize "+x" 'face
596 'stgit-file-permission-face)))))
597 (t (concat (propertize (format "%o" old-perm)
598 'face 'stgit-file-permission-face)
600 'face 'stgit-description-face)
601 (propertize (format "%o" new-perm)
602 'face 'stgit-file-permission-face)))))))
604 (defstruct (stgit-file
605 (:conc-name stgit-file->))
606 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
608 (defun stgit-describe-copy-or-rename (file)
609 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
610 from to common-head common-tail)
612 (when stgit-abbreviate-copies-and-renames
613 (setq from (split-string (stgit-file->cr-from file) "/")
614 to (split-string (stgit-file->cr-to file) "/"))
616 (while (and from to (cdr from) (cdr to)
617 (string-equal (car from) (car to)))
618 (setq common-head (cons (car from) common-head)
621 (setq common-head (nreverse common-head)
624 (while (and from to (cdr from) (cdr to)
625 (string-equal (car from) (car to)))
626 (setq common-tail (cons (car from) common-tail)
629 (setq from (nreverse from)
632 (if (or common-head common-tail)
633 (concat (if common-head
634 (mapconcat #'identity common-head "/")
636 (if common-head "/" "")
637 (propertize "{" 'face 'stgit-description-face)
638 (mapconcat #'identity from "/")
640 (mapconcat #'identity to "/")
641 (propertize "}" 'face 'stgit-description-face)
642 (if common-tail "/" "")
644 (mapconcat #'identity common-tail "/")
646 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
648 (defun stgit-file-pp (file)
649 (let ((start (point))
650 (spec (format-spec-make
651 ?s (stgit-file-status-code-as-string file)
652 ?m (stgit-file-mode-change-string
653 (stgit-file->old-perm file)
654 (stgit-file->new-perm file))
655 ?n (if (stgit-file->copy-or-rename file)
656 (stgit-describe-copy-or-rename file)
657 (stgit-file->file file))
658 ?c (propertize (stgit-file-type-change-string
659 (stgit-file->old-perm file)
660 (stgit-file->new-perm file))
661 'face 'stgit-description-face))))
662 (stgit-insert-without-trailing-whitespace
663 (format-spec stgit-file-line-format spec))
664 (add-text-properties start (point)
665 (list 'entry-type 'file
668 (defun stgit-find-copies-harder-diff-arg ()
669 "Return the flag to use with `git-diff' depending on the
670 `stgit-find-copies-harder' flag."
671 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
673 (defun stgit-insert-ls-files (args file-flag)
674 (let ((start (point)))
675 (apply 'stgit-run-git
676 (append '("ls-files" "--exclude-standard" "-z") args))
678 (while (looking-at "\\([^\0]*\\)\0")
679 (let ((name-len (- (match-end 0) (match-beginning 0))))
680 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
681 (forward-char name-len)))))
683 (defun stgit-process-files (callback)
684 (goto-char (point-min))
685 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
686 (goto-char (match-end 0)))
687 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
688 (let ((old-perm (string-to-number (match-string 1) 8))
689 (new-perm (string-to-number (match-string 2) 8)))
690 (goto-char (match-end 0))
693 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
694 (let* ((patch-status (stgit-patch->status patch))
695 (file-subexp (if (eq patch-status 'unapplied)
698 (file (match-string file-subexp)))
703 :cr-score (string-to-number (match-string 2))
704 :cr-from (match-string 3)
705 :cr-to (match-string 4)
706 :status (stgit-file-status-code
709 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
717 :status (stgit-file-status-code
719 :file (match-string 2))))))
720 (goto-char (match-end 0))
721 (funcall callback file)))))
724 (defun stgit-insert-patch-files (patch)
725 "Expand (show modification of) the patch PATCH after the line
727 (let* ((patchsym (stgit-patch->name patch))
729 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
730 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
731 (set-marker-insertion-type end t)
732 (setf (stgit-patch->files-ewoc patch) ewoc)
734 (let ((standard-output (current-buffer)))
735 (apply 'stgit-run-git
736 (cond ((eq patchsym :work)
737 `("diff-files" "-0" ,@args))
738 ((eq patchsym :index)
739 `("diff-index" ,@args "--cached" "HEAD"))
741 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
743 (when (and (eq patchsym :work))
744 (when stgit-show-ignored
745 (stgit-insert-ls-files '("--ignored" "--others") "I"))
746 (when stgit-show-unknown
747 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
750 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
751 (point-min) (point-max)))
753 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
755 (unless (ewoc-nth ewoc 0)
758 (propertize "<no files>"
759 'face 'stgit-description-face)
763 (defun stgit-find-file (&optional other-window)
764 (let* ((file (or (stgit-patched-file-at-point)
765 (error "No file at point")))
766 (filename (expand-file-name (stgit-file->file file))))
767 (unless (file-exists-p filename)
768 (error "File does not exist"))
769 (funcall (if other-window 'find-file-other-window 'find-file)
771 (when (eq (stgit-file->status file) 'unmerged)
774 (defun stgit-expand (&optional patches collapse)
775 "Show the contents of marked patches, or the patch at point.
777 See also `stgit-collapse'.
779 Non-interactively, operate on PATCHES, and collapse instead of
780 expand if COLLAPSE is not nil."
781 (interactive (list (stgit-patches-marked-or-at-point t)))
783 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
784 patches stgit-expanded-patches)))
785 (setq stgit-expanded-patches
787 (set-difference stgit-expanded-patches patches-diff)
788 (append stgit-expanded-patches patches-diff)))
789 (ewoc-map #'(lambda (patch)
790 (memq (stgit-patch->name patch) patches-diff))
792 (move-to-column (stgit-goal-column)))
794 (defun stgit-collapse (&optional patches)
795 "Hide the contents of marked patches, or the patch at point.
797 See also `stgit-expand'."
798 (interactive (list (stgit-patches-marked-or-at-point t)))
800 (stgit-expand patches t))
802 (defun stgit-select-patch ()
803 (let ((patchname (stgit-patch-name-at-point)))
804 (stgit-expand (list patchname)
805 (memq patchname stgit-expanded-patches))))
807 (defun stgit-expand-directory (file)
808 (let* ((patch (stgit-patch-at-point))
809 (ewoc (stgit-patch->files-ewoc patch))
810 (node (ewoc-locate ewoc))
811 (filename (stgit-file->file file))
812 (start (make-marker))
817 (set-marker start (point))
818 (set-marker end (point))
819 (set-marker-insertion-type end t))
821 (assert (string-match "/$" filename))
822 ;; remove trailing "/"
823 (setf (stgit-file->file file) (substring filename 0 -1))
824 (ewoc-invalidate ewoc node)
827 (let ((standard-output (current-buffer)))
828 (stgit-insert-ls-files (list "--directory" "--others"
829 "--no-empty-directory" "--"
832 (stgit-process-files (lambda (f)
833 (setq node (ewoc-enter-after ewoc node f))))))
835 (let ((inhibit-read-only t))
836 (put-text-property start end 'patch-data patch))))
838 (defun stgit-select-file ()
839 (let* ((file (or (stgit-patched-file-at-point)
840 (error "No file at point")))
841 (filename (stgit-file->file file)))
842 (if (string-match "/$" filename)
843 (stgit-expand-directory file)
846 (defun stgit-select ()
847 "With point on a patch, toggle showing files in the patch.
849 With point on a file, open the associated file. Opens the target
850 file for (applied) copies and renames."
853 (case (get-text-property (point) 'entry-type)
855 (stgit-select-patch))
859 (error "No patch or file on line"))))
861 (defun stgit-find-file-other-window ()
862 "Open file at point in other window"
867 (defun stgit-find-file-merge ()
868 "Open file at point and merge it using `smerge-ediff'."
875 "Hide the stgit buffer."
880 (defun stgit-git-status ()
881 "Show status using `git-status'."
884 (unless (fboundp 'git-status)
885 (error "The stgit-git-status command requires git-status"))
886 (let ((dir default-directory))
887 (save-selected-window
891 (defun stgit-goal-column ()
892 "Return goal column for the current line"
893 (case (get-text-property (point) 'entry-type)
898 (defun stgit-next-line (&optional arg)
899 "Move cursor vertically down ARG lines"
903 (move-to-column (stgit-goal-column)))
905 (defun stgit-previous-line (&optional arg)
906 "Move cursor vertically up ARG lines"
910 (move-to-column (stgit-goal-column)))
912 (defun stgit-next-patch (&optional arg)
913 "Move cursor down ARG patches."
916 (ewoc-goto-next stgit-ewoc (or arg 1))
917 (move-to-column goal-column))
919 (defun stgit-previous-patch (&optional arg)
920 "Move cursor up ARG patches."
923 (ewoc-goto-prev stgit-ewoc (or arg 1))
924 (move-to-column goal-column))
926 (defvar stgit-mode-hook nil
927 "Run after `stgit-mode' is setup.")
929 (defvar stgit-mode-map nil
930 "Keymap for StGit major mode.")
932 (unless stgit-mode-map
933 (let ((diff-map (make-sparse-keymap))
934 (toggle-map (make-sparse-keymap)))
935 (suppress-keymap diff-map)
936 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
937 '(("b" . stgit-diff-base)
938 ("c" . stgit-diff-combined)
939 ("m" . stgit-find-file-merge)
940 ("o" . stgit-diff-ours)
941 ("t" . stgit-diff-theirs)))
942 (suppress-keymap toggle-map)
943 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
944 '(("n" . stgit-toggle-patch-names)
945 ("t" . stgit-toggle-worktree)
946 ("i" . stgit-toggle-ignored)
947 ("u" . stgit-toggle-unknown)))
948 (setq stgit-mode-map (make-keymap))
949 (suppress-keymap stgit-mode-map)
950 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
951 `((" " . stgit-mark-down)
952 ("m" . stgit-mark-down)
953 ("\d" . stgit-unmark-up)
954 ("u" . stgit-unmark-down)
957 ("\C-p" . stgit-previous-line)
958 ("\C-n" . stgit-next-line)
959 ([up] . stgit-previous-line)
960 ([down] . stgit-next-line)
961 ("p" . stgit-previous-patch)
962 ("n" . stgit-next-patch)
963 ("\M-{" . stgit-previous-patch)
964 ("\M-}" . stgit-next-patch)
965 ("s" . stgit-git-status)
966 ("g" . stgit-reload-or-repair)
967 ("r" . stgit-refresh)
968 ("\C-c\C-r" . stgit-rename)
970 ("M" . stgit-move-patches)
973 ("c" . stgit-new-and-refresh)
974 ("\C-c\C-c" . stgit-commit)
975 ("\C-c\C-u" . stgit-uncommit)
977 ("R" . stgit-resolve-file)
978 ("\r" . stgit-select)
980 ("-" . stgit-collapse)
981 ("o" . stgit-find-file-other-window)
982 ("i" . stgit-toggle-index)
983 (">" . stgit-push-next)
984 ("<" . stgit-pop-next)
985 ("P" . stgit-push-or-pop)
989 ([?\C-/] . stgit-undo)
990 ("\C-_" . stgit-undo)
991 ([?\C-c ?\C-/] . stgit-redo)
992 ("\C-c\C-_" . stgit-redo)
994 ("\C-c\C-b" . stgit-rebase)
997 ("q" . stgit-quit))))
999 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1000 (and file (eq (stgit-file->status file)
1002 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1003 (easy-menu-define stgit-menu stgit-mode-map
1006 ["Reload" stgit-reload-or-repair
1007 :help "Reload StGit status from disk"]
1008 ["Repair" stgit-repair
1009 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1010 :help "Repair StGit metadata"]
1012 ["Undo" stgit-undo t]
1013 ["Redo" stgit-redo t]
1015 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1017 ["New patch" stgit-new-and-refresh
1018 :help "Create a new patch from changes in index or work tree"
1019 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1020 ["New empty patch" stgit-new
1021 :help "Create a new, empty patch"]
1022 ["(Un)mark patch" stgit-toggle-mark
1023 :label (if (memq (stgit-patch-name-at-point nil t)
1024 stgit-marked-patches)
1025 "Unmark patch" "Mark patch")
1026 :active (stgit-patch-name-at-point nil t)]
1027 ["Expand/collapse patch"
1028 (let ((patches (stgit-patches-marked-or-at-point)))
1029 (if (member-if ,patch-collapsed-p patches)
1030 (stgit-expand patches)
1031 (stgit-collapse patches)))
1032 :label (if (member-if ,patch-collapsed-p
1033 (stgit-patches-marked-or-at-point))
1036 :active (stgit-patches-marked-or-at-point)]
1037 ["Edit patch" stgit-edit
1038 :help "Edit patch comment"
1039 :active (stgit-patch-name-at-point nil t)]
1040 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1041 ["Push/pop patch" stgit-push-or-pop
1042 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1043 (stgit-applied-patchsyms t))
1044 "Pop patches" "Push patches")]
1045 ["Delete patches" stgit-delete
1046 :active (stgit-patches-marked-or-at-point nil t)]
1048 ["Move patches" stgit-move-patches
1049 :active stgit-marked-patches
1050 :help "Move marked patch(es) to point"]
1051 ["Squash patches" stgit-squash
1052 :active (> (length stgit-marked-patches) 1)
1053 :help "Merge marked patches into one"]
1055 ["Refresh top patch" stgit-refresh
1056 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1057 :help "Refresh the top patch with changes in index or work tree"]
1058 ["Refresh this patch" (stgit-refresh t)
1059 :keys "\\[universal-argument] \\[stgit-refresh]"
1060 :help "Refresh marked patch with changes in index or work tree"
1061 :active (and (not (and (stgit-index-empty-p)
1062 (stgit-work-tree-empty-p)))
1063 (stgit-patch-name-at-point nil t))]
1065 ["Find file" stgit-select
1066 :active (eq (get-text-property (point) 'entry-type) 'file)]
1067 ["Open file" stgit-find-file-other-window
1068 :active (eq (get-text-property (point) 'entry-type) 'file)]
1069 ["Toggle file index" stgit-toggle-index
1070 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1071 (memq (stgit-patch-name-at-point) '(:work :index)))
1072 :label (if (eq (stgit-patch-name-at-point) :work)
1073 "Move change to index"
1074 "Move change to work tree")]
1076 ["Show diff" stgit-diff
1077 :active (get-text-property (point) 'entry-type)]
1079 :active (stgit-git-index-unmerged-p)
1080 ["Combined diff" stgit-diff-combined
1081 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1082 ["Diff against base" stgit-diff-base
1083 :help "Show diff against the common base"
1084 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1085 ["Diff against ours" stgit-diff-ours
1086 :help "Show diff against our branch"
1087 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1088 ["Diff against theirs" stgit-diff-theirs
1089 :help "Show diff against their branch"
1090 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1092 ["Interactive merge" stgit-find-file-merge
1093 :help "Interactively merge the file"
1094 :active ,at-unmerged-file]
1095 ["Resolve file" stgit-resolve-file
1096 :help "Mark file conflict as resolved"
1097 :active ,at-unmerged-file]
1100 ["Show index & work tree" stgit-toggle-worktree :style toggle
1101 :selected stgit-show-worktree]
1102 ["Show unknown files" stgit-toggle-unknown :style toggle
1103 :selected stgit-show-unknown :active stgit-show-worktree]
1104 ["Show ignored files" stgit-toggle-ignored :style toggle
1105 :selected stgit-show-ignored :active stgit-show-worktree]
1106 ["Show patch names" stgit-toggle-patch-names :style toggle
1107 :selected stgit-show-patch-names]
1109 ["Switch branches" stgit-branch t
1110 :help "Switch to or create another branch"]
1111 ["Rebase branch" stgit-rebase t
1112 :help "Rebase the current branch"]
1115 ;; disable tool bar editing buttons
1116 (put 'stgit-mode 'mode-class 'special)
1118 (defun stgit-mode ()
1119 "Major mode for interacting with StGit.
1121 Start StGit using \\[stgit].
1125 \\[stgit-help] Show this help text
1126 \\[stgit-quit] Hide the StGit buffer
1127 \\[describe-bindings] Show all key bindings
1129 \\[stgit-reload-or-repair] Reload the StGit buffer
1130 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1132 \\[stgit-undo] Undo most recent StGit operation
1133 \\[stgit-redo] Undo recent undo
1135 \\[stgit-git-status] Run `git-status' (if available)
1138 \\[stgit-previous-line] Move to previous line
1139 \\[stgit-next-line] Move to next line
1140 \\[stgit-previous-patch] Move to previous patch
1141 \\[stgit-next-patch] Move to next patch
1143 \\[stgit-mark-down] Mark patch and move down
1144 \\[stgit-unmark-up] Unmark patch and move up
1145 \\[stgit-unmark-down] Unmark patch and move down
1147 Commands for patches:
1148 \\[stgit-select] Toggle showing changed files in patch
1149 \\[stgit-refresh] Refresh patch with changes in index or work tree
1150 \\[stgit-diff] Show the patch log and diff
1152 \\[stgit-expand] Show changes in marked patches
1153 \\[stgit-collapse] Hide changes in marked patches
1155 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1156 \\[stgit-new] Create a new, empty patch
1158 \\[stgit-rename] Rename patch
1159 \\[stgit-edit] Edit patch description
1160 \\[stgit-delete] Delete patch(es)
1162 \\[stgit-revert] Revert all changes in index or work tree
1163 \\[stgit-toggle-index] Toggle all changes between index and work tree
1165 \\[stgit-push-next] Push next patch onto stack
1166 \\[stgit-pop-next] Pop current patch from stack
1167 \\[stgit-push-or-pop] Push or pop marked patches
1168 \\[stgit-goto] Make patch at point current by popping or pushing
1170 \\[stgit-squash] Squash (meld together) patches
1171 \\[stgit-move-patches] Move marked patches to point
1173 \\[stgit-commit] Commit patch(es)
1174 \\[stgit-uncommit] Uncommit patch(es)
1177 \\[stgit-select] Open the file in this window
1178 \\[stgit-find-file-other-window] Open the file in another window
1179 \\[stgit-diff] Show the file's diff
1181 \\[stgit-toggle-index] Toggle change between index and work tree
1182 \\[stgit-revert] Revert changes to file
1185 \\[stgit-toggle-patch-names] Toggle showing patch names
1186 \\[stgit-toggle-worktree] Toggle showing index and work tree
1187 \\[stgit-toggle-unknown] Toggle showing unknown files
1188 \\[stgit-toggle-ignored] Toggle showing ignored files
1191 \\[stgit-diff] Show diff of patch or file
1192 \\[stgit-diff-base] Show diff against the merge base
1193 \\[stgit-diff-ours] Show diff against our branch
1194 \\[stgit-diff-theirs] Show diff against their branch
1196 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1197 ignore space changes.
1198 With two prefix arguments (e.g., \\[universal-argument] \
1199 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1201 Commands for merge conflicts:
1202 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1203 \\[stgit-resolve-file] Mark unmerged file as resolved
1205 Commands for branches:
1206 \\[stgit-branch] Switch to or create another branch
1207 \\[stgit-rebase] Rebase the current branch
1209 Customization variables:
1210 `stgit-abbreviate-copies-and-renames'
1211 `stgit-default-show-patch-names'
1212 `stgit-default-show-worktree'
1213 `stgit-find-copies-harder'
1214 `stgit-show-worktree-mode'
1216 See also \\[customize-group] for the \"stgit\" group."
1217 (kill-all-local-variables)
1218 (buffer-disable-undo)
1219 (setq mode-name "StGit"
1220 major-mode 'stgit-mode
1222 (use-local-map stgit-mode-map)
1223 (set (make-local-variable 'list-buffers-directory) default-directory)
1224 (set (make-local-variable 'stgit-marked-patches) nil)
1225 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1226 (set (make-local-variable 'stgit-show-patch-names)
1227 stgit-default-show-patch-names)
1228 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1229 (set (make-local-variable 'stgit-index-node) nil)
1230 (set (make-local-variable 'stgit-worktree-node) nil)
1231 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1232 (set-variable 'truncate-lines 't)
1233 (add-hook 'after-save-hook 'stgit-update-saved-file)
1234 (run-hooks 'stgit-mode-hook))
1236 (defun stgit-update-saved-file ()
1237 (let* ((file (expand-file-name buffer-file-name))
1238 (dir (file-name-directory file))
1239 (gitdir (condition-case nil (git-get-top-dir dir)
1241 (buffer (and gitdir (stgit-find-buffer gitdir))))
1243 (with-current-buffer buffer
1244 (stgit-refresh-worktree)))))
1246 (defun stgit-add-mark (patchsym)
1247 "Mark the patch PATCHSYM."
1248 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1250 (defun stgit-remove-mark (patchsym)
1251 "Unmark the patch PATCHSYM."
1252 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1254 (defun stgit-clear-marks ()
1255 "Unmark all patches."
1256 (setq stgit-marked-patches '()))
1258 (defun stgit-patch-at-point (&optional cause-error)
1259 (get-text-property (point) 'patch-data))
1261 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1262 "Return the patch name on the current line as a symbol.
1263 If CAUSE-ERROR is not nil, signal an error if none found.
1264 If ONLY-PATCHES is not nil, only allow real patches, and not
1265 index or work tree."
1266 (let ((patch (stgit-patch-at-point)))
1269 (memq (stgit-patch->status patch) '(work index))
1272 (stgit-patch->name patch))
1274 (error "No patch on this line")))))
1276 (defun stgit-patched-file-at-point ()
1277 (get-text-property (point) 'file-data))
1279 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1280 "Return the symbols of the marked patches, or the patch on the current line.
1281 If CAUSE-ERRROR is not nil, signal an error if none found.
1282 If ONLY-PATCHES is not nil, do not include index or work tree."
1283 (if stgit-marked-patches
1284 stgit-marked-patches
1285 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1286 (cond (patch (list patch))
1287 (cause-error (error "No patches marked or at this line"))
1290 (defun stgit-goto-patch (patchsym &optional file)
1291 "Move point to the line containing patch PATCHSYM.
1292 If that patch cannot be found, do nothing.
1294 If the patch was found and FILE is not nil, instead move to that
1295 file's line. If FILE cannot be found, stay on the line of
1297 (let ((node (ewoc-nth stgit-ewoc 0)))
1298 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1300 (setq node (ewoc-next stgit-ewoc node)))
1301 (when (and node file)
1302 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1303 (file-node (ewoc-nth file-ewoc 0)))
1304 (while (and file-node (not (equal (stgit-file->file (ewoc-data file-node)) file)))
1305 (setq file-node (ewoc-next file-ewoc file-node)))
1307 (ewoc-goto-node file-ewoc file-node)
1308 (move-to-column (stgit-goal-column))
1311 (ewoc-goto-node stgit-ewoc node)
1312 (move-to-column goal-column))))
1314 (defun stgit-init ()
1318 (stgit-capture-output nil
1322 (defun stgit-toggle-mark ()
1323 "Toggle mark on the patch under point."
1326 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1330 (defun stgit-mark ()
1331 "Mark the patch under point."
1334 (let* ((node (ewoc-locate stgit-ewoc))
1335 (patch (ewoc-data node))
1336 (name (stgit-patch->name patch)))
1337 (when (eq name :work)
1338 (error "Cannot mark the work tree"))
1339 (when (eq name :index)
1340 (error "Cannot mark the index"))
1341 (stgit-add-mark (stgit-patch->name patch))
1342 (let ((column (current-column)))
1343 (ewoc-invalidate stgit-ewoc node)
1344 (move-to-column column))))
1346 (defun stgit-mark-down ()
1347 "Mark the patch under point and move to the next patch."
1352 (defun stgit-unmark ()
1353 "Remove mark from the patch on the current line."
1356 (let* ((node (ewoc-locate stgit-ewoc))
1357 (patch (ewoc-data node)))
1358 (stgit-remove-mark (stgit-patch->name patch))
1359 (let ((column (current-column)))
1360 (ewoc-invalidate stgit-ewoc node)
1361 (move-to-column column))))
1363 (defun stgit-unmark-up ()
1364 "Remove mark from the patch on the previous line."
1367 (stgit-previous-patch)
1370 (defun stgit-unmark-down ()
1371 "Remove mark from the patch on the current line."
1377 (defun stgit-rename (name)
1378 "Rename the patch under point to NAME."
1380 (read-string "Patch name: "
1381 (symbol-name (stgit-patch-name-at-point t t)))))
1383 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1384 (stgit-capture-output nil
1385 (stgit-run "rename" old-patchsym name))
1386 (let ((name-sym (intern name)))
1387 (when (memq old-patchsym stgit-expanded-patches)
1388 (setq stgit-expanded-patches
1389 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1390 (when (memq old-patchsym stgit-marked-patches)
1391 (setq stgit-marked-patches
1392 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1394 (stgit-goto-patch name-sym))))
1396 (defun stgit-reload-or-repair (repair)
1397 "Update the contents of the StGit buffer (`stgit-reload').
1399 With a prefix argument, repair the StGit metadata if the branch
1400 was modified with git commands (`stgit-repair')."
1407 (defun stgit-repair ()
1411 (stgit-capture-output nil
1412 (stgit-run "repair"))
1415 (defun stgit-available-branches (&optional all)
1416 "Returns a list of the names of the available stg branches as strings.
1418 If ALL is not nil, also return non-stgit branches."
1419 (let ((output (with-output-to-string
1420 (stgit-run "branch" "--list")))
1421 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1425 (while (string-match pattern output start)
1426 (setq result (cons (match-string 1 output) result))
1427 (setq start (match-end 0)))
1430 (defun stgit-branch (branch)
1431 "Switch to or create branch BRANCH."
1432 (interactive (list (completing-read "Switch to branch: "
1433 (stgit-available-branches))))
1435 (when (cond ((equal branch (stgit-current-branch))
1436 (error "Branch is already current"))
1437 ((member branch (stgit-available-branches t))
1438 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1440 ((not (string-match stgit-allowed-branch-name-re branch))
1441 (error "Invalid branch name"))
1442 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1443 (stgit-capture-output nil (stgit-run "branch" "--create" "--"
1448 (defun stgit-available-refs (&optional omit-stgit)
1449 "Returns a list of the available git refs.
1450 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1451 (let* ((output (with-output-to-string
1452 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1453 "refs/tags" "refs/heads"
1455 (result (split-string output "\n" t)))
1457 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1458 (substring s (match-end 0))
1461 (delete-if (lambda (s)
1462 (string-match "^refs/heads/.*\\.stgit$" s))
1466 (defun stgit-parent-branch ()
1467 "Return the parent branch of the current stg branch as per
1468 git-config setting branch.<branch>.stgit.parentbranch."
1469 (let ((output (with-output-to-string
1470 (stgit-run-git-silent "config"
1471 (format "branch.%s.stgit.parentbranch"
1472 (stgit-current-branch))))))
1473 (when (string-match ".*" output)
1474 (match-string 0 output))))
1476 (defun stgit-rebase (new-base)
1477 "Rebase the current branch to NEW-BASE.
1479 Interactively, first ask which branch to rebase to. Defaults to
1480 what git-config branch.<branch>.stgit.parentbranch is set to."
1481 (interactive (list (completing-read "Rebase to: "
1482 (stgit-available-refs t)
1484 (stgit-parent-branch))))
1486 (stgit-capture-output nil (stgit-run "rebase" new-base))
1489 (defun stgit-commit (count)
1490 "Run stg commit on COUNT commits.
1491 Interactively, the prefix argument is used as COUNT.
1492 A negative COUNT will uncommit instead."
1496 (stgit-uncommit (- count))
1497 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1500 (defun stgit-uncommit (count)
1501 "Run stg uncommit on COUNT commits.
1502 Interactively, the prefix argument is used as COUNT.
1503 A negative COUNT will commit instead."
1507 (stgit-commit (- count))
1508 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1511 (defun stgit-neighbour-file ()
1512 "Return the file name of the next file after point, or the
1513 previous file if point is at the last file within a patch."
1514 (let ((old-point (point))
1516 (and (zerop (forward-line 1))
1517 (let ((f (stgit-patched-file-at-point)))
1518 (and f (setq neighbour-file (stgit-file->file f)))))
1519 (goto-char old-point)
1520 (unless neighbour-file
1521 (and (zerop (forward-line -1))
1522 (let ((f (stgit-patched-file-at-point)))
1523 (and f (setq neighbour-file (stgit-file->file f)))))
1524 (goto-char old-point))
1527 (defun stgit-revert-file ()
1528 "Revert the file at point, which must be in the index or the
1532 (let* ((patched-file (or (stgit-patched-file-at-point)
1533 (error "No file on the current line")))
1534 (patch-name (stgit-patch-name-at-point))
1535 (file-status (stgit-file->status patched-file))
1536 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1537 (stgit-file->cr-to patched-file))
1538 ((eq file-status 'add)
1539 (stgit-file->file patched-file))))
1540 (co-file (cond ((eq file-status 'rename)
1541 (stgit-file->cr-from patched-file))
1542 ((not (memq file-status '(copy add)))
1543 (stgit-file->file patched-file))))
1544 (next-file (stgit-neighbour-file)))
1546 (unless (memq patch-name '(:work :index))
1547 (error "No index or working tree file on this line"))
1549 (when (eq file-status 'ignore)
1550 (error "Cannot revert ignored files"))
1552 (when (eq file-status 'unknown)
1553 (error "Cannot revert unknown files"))
1555 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1556 (when (yes-or-no-p (format "Revert %d file%s? "
1558 (if (= nfiles 1) "" "s")))
1559 (stgit-capture-output nil
1561 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1563 (stgit-run-git "checkout" "HEAD" co-file)))
1565 (stgit-goto-patch patch-name next-file)))))
1567 (defun stgit-revert ()
1568 "Revert the change at point, which must be the index, the work
1569 tree, or a single change in either."
1572 (let ((patched-file (stgit-patched-file-at-point)))
1575 (let* ((patch-name (or (stgit-patch-name-at-point)
1576 (error "No patch or file at point")))
1577 (patch-desc (case patch-name
1580 (t (error (substitute-command-keys
1581 "Use \\[stgit-delete] to delete a patch"))))))
1582 (when (if (eq patch-name :work)
1583 (stgit-work-tree-empty-p)
1584 (stgit-index-empty-p))
1585 (error (format "There are no changes in the %s to revert"
1587 (and (eq patch-name :index)
1588 (not (stgit-work-tree-empty-p))
1589 (error "Cannot revert index as work tree contains unstaged changes"))
1591 (when (yes-or-no-p (format "Revert all changes in the %s? "
1593 (if (eq patch-name :index)
1594 (stgit-run-git-silent "reset" "--hard" "-q")
1595 (stgit-run-git-silent "checkout" "--" "."))
1596 (stgit-refresh-index)
1597 (stgit-refresh-worktree)
1598 (stgit-goto-patch patch-name))))))
1600 (defun stgit-resolve-file ()
1601 "Resolve conflict in the file at point."
1604 (let* ((patched-file (stgit-patched-file-at-point))
1605 (patch (stgit-patch-at-point))
1606 (patch-name (and patch (stgit-patch->name patch)))
1607 (status (and patched-file (stgit-file->status patched-file))))
1609 (unless (memq patch-name '(:work :index))
1610 (error "No index or working tree file on this line"))
1612 (unless (eq status 'unmerged)
1613 (error "No conflict to resolve at the current line"))
1615 (stgit-capture-output nil
1616 (stgit-move-change-to-index (stgit-file->file patched-file)))
1620 (defun stgit-push-next (npatches)
1621 "Push the first unapplied patch.
1622 With numeric prefix argument, push that many patches."
1625 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1627 (stgit-refresh-git-status))
1629 (defun stgit-pop-next (npatches)
1630 "Pop the topmost applied patch.
1631 With numeric prefix argument, pop that many patches."
1634 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1636 (stgit-refresh-git-status))
1638 (defun stgit-applied-patches (&optional only-patches)
1639 "Return a list of the applied patches.
1641 If ONLY-PATCHES is not nil, exclude index and work tree."
1642 (let ((states (if only-patches
1644 '(applied top index work)))
1646 (ewoc-map (lambda (patch) (when (memq (stgit-patch->status patch) states)
1647 (setq result (cons patch result))))
1651 (defun stgit-applied-patchsyms (&optional only-patches)
1652 "Return a list of the symbols of the applied patches.
1654 If ONLY-PATCHES is not nil, exclude index and work tree."
1655 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1657 (defun stgit-push-or-pop ()
1658 "Push or pop the marked patches."
1661 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1662 (applied-syms (stgit-applied-patchsyms t))
1663 (unapplied (set-difference patchsyms applied-syms)))
1664 (stgit-capture-output nil
1666 (if unapplied "push" "pop")
1668 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1671 (defun stgit-goto ()
1672 "Go to the patch on the current line.
1674 Pops or pushes patches to make this patch topmost."
1677 (let ((patchsym (stgit-patch-name-at-point t)))
1678 (stgit-capture-output nil
1679 (stgit-run "goto" patchsym))
1682 (defun stgit-id (patchsym)
1683 "Return the git commit id for PATCHSYM.
1684 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1685 (if (keywordp patchsym)
1687 (let ((result (with-output-to-string
1688 (stgit-run-silent "id" patchsym))))
1689 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1690 (error "Cannot find commit id for %s" patchsym))
1691 (match-string 1 result))))
1693 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1694 "Show the patch on the current line.
1696 UNMERGED-STAGE is the argument to `git-diff' that that selects
1697 which stage to diff against in the case of unmerged files."
1698 (let ((space-arg (when (numberp ignore-whitespace)
1699 (cond ((> ignore-whitespace 4)
1700 "--ignore-all-space")
1701 ((> ignore-whitespace 1)
1702 "--ignore-space-change"))))
1703 (patch-name (stgit-patch-name-at-point t)))
1704 (stgit-capture-output "*StGit patch*"
1705 (case (get-text-property (point) 'entry-type)
1707 (let* ((patched-file (stgit-patched-file-at-point))
1708 (patch-id (let ((id (stgit-id patch-name)))
1709 (if (and (eq id :index)
1710 (eq (stgit-file->status patched-file)
1714 (args (append (and space-arg (list space-arg))
1715 (and (stgit-file->cr-from patched-file)
1716 (list (stgit-find-copies-harder-diff-arg)))
1717 (cond ((eq patch-id :index)
1719 ((eq patch-id :work)
1720 (list unmerged-stage))
1722 (list (concat patch-id "^") patch-id)))
1724 (if (stgit-file->copy-or-rename patched-file)
1725 (list (stgit-file->cr-from patched-file)
1726 (stgit-file->cr-to patched-file))
1727 (list (stgit-file->file patched-file))))))
1728 (apply 'stgit-run-git "diff" args)))
1730 (let* ((patch-id (stgit-id patch-name)))
1731 (if (or (eq patch-id :index) (eq patch-id :work))
1732 (apply 'stgit-run-git "diff"
1733 (stgit-find-copies-harder-diff-arg)
1734 (append (and space-arg (list space-arg))
1735 (if (eq patch-id :index)
1737 (list unmerged-stage))))
1738 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1739 (and space-arg (list "-O" space-arg))
1740 (list (stgit-patch-name-at-point)))))
1741 (apply 'stgit-run args)))))
1743 (error "No patch or file at point")))
1744 (with-current-buffer standard-output
1745 (goto-char (point-min))
1748 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1749 `(defun ,name (&optional ignore-whitespace)
1750 ,(format "Show the patch on the current line.
1752 %sWith a prefix argument, ignore whitespace. With a prefix argument
1753 greater than four (e.g., \\[universal-argument] \
1754 \\[universal-argument] \\[%s]), ignore all whitespace."
1756 (format "For unmerged files, %s.\n\n" unmerged-action)
1761 (stgit-show-patch ,diff-arg ignore-whitespace)))
1763 (stgit-define-diff stgit-diff
1765 (stgit-define-diff stgit-diff-ours
1767 "diff against our branch")
1768 (stgit-define-diff stgit-diff-theirs
1770 "diff against their branch")
1771 (stgit-define-diff stgit-diff-base
1773 "diff against the merge base")
1774 (stgit-define-diff stgit-diff-combined
1776 "show a combined diff")
1778 (defun stgit-move-change-to-index (file &optional force)
1779 "Copies the work tree state of FILE to index, using git add or git rm.
1781 If FORCE is not nil, use --force."
1782 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1783 '("add") '("rm" "-q"))))
1784 (stgit-capture-output "*git output*"
1785 (apply 'stgit-run-git (append op (and force '("--force"))
1786 '("--") (list file))))))
1788 (defun stgit-remove-change-from-index (file)
1789 "Unstages the change in FILE from the index"
1790 (stgit-capture-output "*git output*"
1791 (stgit-run-git "reset" "-q" "--" file)))
1793 (defun stgit-git-index-unmerged-p ()
1795 (with-output-to-string
1796 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1798 "--quiet" "HEAD")))))
1801 (defun stgit-file-toggle-index ()
1802 "Move modified file in or out of the index.
1804 Leaves the point where it is, but moves the mark to where the
1805 file ended up. You can then jump to the file with \
1806 \\[exchange-point-and-mark]."
1809 (let* ((patched-file (or (stgit-patched-file-at-point)
1810 (error "No file on the current line")))
1811 (patched-status (stgit-file->status patched-file)))
1812 (when (eq patched-status 'unmerged)
1813 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1814 (let* ((patch (stgit-patch-at-point))
1815 (patch-name (stgit-patch->name patch))
1816 (mark-file (if (eq patched-status 'rename)
1817 (stgit-file->cr-to patched-file)
1818 (stgit-file->file patched-file)))
1819 (point-file (if (eq patched-status 'rename)
1820 (stgit-file->cr-from patched-file)
1821 (stgit-neighbour-file))))
1823 (cond ((eq patch-name :work)
1824 (stgit-move-change-to-index (stgit-file->file patched-file)
1825 (eq patched-status 'ignore)))
1826 ((eq patch-name :index)
1827 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1829 (error "Can only move files between working tree and index")))
1830 (stgit-refresh-worktree)
1831 (stgit-refresh-index)
1832 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1834 (stgit-goto-patch patch-name point-file))))
1836 (defun stgit-toggle-index ()
1837 "Move change in or out of the index.
1839 Works on index and work tree, as well as files in either.
1841 Leaves the point where it is, but moves the mark to where the
1842 file ended up. You can then jump to the file with \
1843 \\[exchange-point-and-mark]."
1846 (if (stgit-patched-file-at-point)
1847 (stgit-file-toggle-index)
1848 (let ((patch-name (stgit-patch-name-at-point)))
1849 (unless (memq patch-name '(:index :work))
1850 (error "Can only move changes between working tree and index"))
1851 (when (stgit-git-index-unmerged-p)
1852 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1853 (if (if (eq patch-name :index)
1854 (stgit-index-empty-p)
1855 (stgit-work-tree-empty-p))
1856 (message "No changes to be moved")
1857 (stgit-capture-output nil
1858 (if (eq patch-name :work)
1859 (stgit-run-git "add" "--update")
1860 (stgit-run-git "reset" "--mixed" "-q")))
1861 (stgit-refresh-worktree)
1862 (stgit-refresh-index))
1863 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1865 (defun stgit-edit ()
1866 "Edit the patch on the current line."
1869 (let ((patchsym (stgit-patch-name-at-point t t))
1870 (edit-buf (get-buffer-create "*StGit edit*"))
1871 (dir default-directory))
1872 (log-edit 'stgit-confirm-edit t nil edit-buf)
1873 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1874 (setq default-directory dir)
1875 (let ((standard-output edit-buf))
1877 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1879 (defun stgit-confirm-edit ()
1881 (let ((file (make-temp-file "stgit-edit-")))
1882 (write-region (point-min) (point-max) file)
1883 (stgit-capture-output nil
1884 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1885 (with-current-buffer log-edit-parent-buffer
1888 (defun stgit-new (add-sign &optional refresh)
1889 "Create a new patch.
1890 With a prefix argument, include a \"Signed-off-by:\" line at the
1894 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1895 (dir default-directory))
1896 (log-edit 'stgit-confirm-new t nil edit-buf)
1897 (setq default-directory dir)
1898 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1901 (let ((standard-output (current-buffer)))
1902 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1904 (defun stgit-confirm-new ()
1906 (let ((file (make-temp-file "stgit-edit-"))
1907 (refresh stgit-refresh-after-new))
1908 (write-region (point-min) (point-max) file)
1909 (stgit-capture-output nil
1910 (stgit-run "new" "-f" file))
1911 (with-current-buffer log-edit-parent-buffer
1916 (defun stgit-new-and-refresh (add-sign)
1917 "Create a new patch and refresh it with the current changes.
1919 With a prefix argument, include a \"Signed-off-by:\" line at the
1922 This works just like running `stgit-new' followed by `stgit-refresh'."
1925 (stgit-new add-sign t))
1927 (defun stgit-create-patch-name (description)
1928 "Create a patch name from a long description"
1930 (while (> (length description) 0)
1931 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1932 (setq patch (downcase (concat patch
1933 (match-string 0 description))))
1934 (setq description (substring description (match-end 0))))
1935 ((string-match "\\` +" description)
1936 (setq patch (concat patch "-"))
1937 (setq description (substring description (match-end 0))))
1938 ((string-match "\\`[^a-zA-Z_-]+" description)
1939 (setq description (substring description (match-end 0))))))
1940 (cond ((= (length patch) 0)
1942 ((> (length patch) 20)
1943 (substring patch 0 20))
1946 (defun stgit-delete (patchsyms &optional spill-p)
1947 "Delete the patches in PATCHSYMS.
1948 Interactively, delete the marked patches, or the patch at point.
1950 With a prefix argument, or SPILL-P, spill the patch contents to
1951 the work tree and index."
1952 (interactive (list (stgit-patches-marked-or-at-point t t)
1953 current-prefix-arg))
1956 (error "No patches to delete"))
1957 (when (memq :index patchsyms)
1958 (error "Cannot delete the index"))
1959 (when (memq :work patchsyms)
1960 (error "Cannot delete the work tree"))
1962 (let ((npatches (length patchsyms)))
1963 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1965 (if (= 1 npatches) "" "es")
1967 " (spilling contents to index)"
1969 (let ((args (if spill-p
1970 (cons "--spill" patchsyms)
1972 (stgit-capture-output nil
1973 (apply 'stgit-run "delete" args))
1976 (defun stgit-move-patches-target ()
1977 "Return the patchsym indicating a target patch for
1978 `stgit-move-patches'.
1980 This is either the first unmarked patch at or after point, or one
1981 of :top and :bottom if the point is after or before the applied
1987 (let ((patchsym (stgit-patch-name-at-point)))
1988 (cond ((memq patchsym '(:work :index)) (setq result :top))
1989 (patchsym (if (memq patchsym stgit-marked-patches)
1991 (setq result patchsym)))
1992 ((re-search-backward "^>" nil t) (setq result :top))
1993 (t (setq result :bottom)))))
1996 (defun stgit-sort-patches (patchsyms)
1997 "Returns the list of patches in PATCHSYMS sorted according to
1998 their position in the patch series, bottommost first.
2000 PATCHSYMS must not contain duplicate entries."
2001 (let (sorted-patchsyms
2002 (series (with-output-to-string
2003 (with-current-buffer standard-output
2004 (stgit-run-silent "series" "--noprefix"))))
2006 (while (string-match "^\\(.+\\)" series start)
2007 (let ((patchsym (intern (match-string 1 series))))
2008 (when (memq patchsym patchsyms)
2009 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2010 (setq start (match-end 0)))
2011 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2013 (unless (= (length patchsyms) (length sorted-patchsyms))
2014 (error "Internal error"))
2018 (defun stgit-move-patches (patchsyms target-patch)
2019 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2020 If TARGET-PATCH is :bottom or :top, move the patches to the
2021 bottom or top of the stack, respectively.
2023 Interactively, move the marked patches to where the point is."
2024 (interactive (list stgit-marked-patches
2025 (stgit-move-patches-target)))
2028 (error "Need at least one patch to move"))
2030 (unless target-patch
2031 (error "Point not at a patch"))
2033 ;; need to have patchsyms sorted by position in the stack
2034 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2035 (stgit-capture-output nil
2036 (if (eq target-patch :top)
2037 (apply 'stgit-run "float" sorted-patchsyms)
2040 (append (unless (eq target-patch :bottom)
2041 (list "--to" target-patch))
2043 sorted-patchsyms)))))
2046 (defun stgit-squash (patchsyms)
2047 "Squash the patches in PATCHSYMS.
2048 Interactively, squash the marked patches.
2050 Unless there are any conflicts, the patches will be merged into
2051 one patch, which will occupy the same spot in the series as the
2052 deepest patch had before the squash."
2053 (interactive (list stgit-marked-patches))
2055 (when (< (length patchsyms) 2)
2056 (error "Need at least two patches to squash"))
2057 (let ((stgit-buffer (current-buffer))
2058 (edit-buf (get-buffer-create "*StGit edit*"))
2059 (dir default-directory)
2060 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2061 (log-edit 'stgit-confirm-squash t nil edit-buf)
2062 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2063 (setq default-directory dir)
2064 (let ((result (let ((standard-output edit-buf))
2066 (apply 'stgit-run-silent "squash"
2067 "--save-template=-" sorted-patchsyms)))))
2069 ;; stg squash may have reordered the patches or caused conflicts
2070 (with-current-buffer stgit-buffer
2073 (unless (eq 0 result)
2075 (rename-buffer "*StGit error*")
2076 (resize-temp-buffer-window)
2077 (switch-to-buffer-other-window stgit-buffer)
2078 (error "stg squash failed")))))
2080 (defun stgit-confirm-squash ()
2082 (let ((file (make-temp-file "stgit-edit-")))
2083 (write-region (point-min) (point-max) file)
2084 (stgit-capture-output nil
2085 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2086 (with-current-buffer log-edit-parent-buffer
2088 ;; Go to first marked patch and stay there
2089 (goto-char (point-min))
2090 (re-search-forward (concat "^[>+-]\\*") nil t)
2091 (move-to-column goal-column)
2092 (let ((pos (point)))
2096 (defun stgit-help ()
2097 "Display help for the StGit mode."
2099 (describe-function 'stgit-mode))
2101 (defun stgit-undo-or-redo (redo hard)
2102 "Run stg undo or, if REDO is non-nil, stg redo.
2104 If HARD is non-nil, use the --hard flag."
2106 (let ((cmd (if redo "redo" "undo")))
2107 (stgit-capture-output nil
2109 (when (or (and (stgit-index-empty-p)
2110 (stgit-work-tree-empty-p))
2111 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2113 (stgit-run cmd "--hard"))
2117 (defun stgit-undo (&optional arg)
2119 With prefix argument, run it with the --hard flag.
2121 See also `stgit-redo'."
2123 (stgit-undo-or-redo nil arg))
2125 (defun stgit-redo (&optional arg)
2127 With prefix argument, run it with the --hard flag.
2129 See also `stgit-undo'."
2131 (stgit-undo-or-redo t arg))
2133 (defun stgit-refresh (&optional arg)
2135 If the index contains any changes, only refresh from index.
2137 With prefix argument, refresh the marked patch or the patch under point."
2140 (let ((patchargs (if arg
2141 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2142 (when (> (length patches) 1)
2143 (error "Too many patches marked"))
2144 (cons "-p" patches))
2146 (unless (stgit-index-empty-p)
2147 (setq patchargs (cons "--index" patchargs)))
2148 (stgit-capture-output nil
2149 (apply 'stgit-run "refresh" patchargs))
2150 (stgit-refresh-git-status))
2153 (defvar stgit-show-worktree nil
2154 "If nil, inhibit showing work tree and index in the stgit buffer.
2156 See also `stgit-show-worktree-mode'.")
2158 (defvar stgit-show-ignored nil
2159 "If nil, inhibit showing files ignored by git.")
2161 (defvar stgit-show-unknown nil
2162 "If nil, inhibit showing files not registered with git.")
2164 (defvar stgit-show-patch-names t
2165 "If nil, inhibit showing patch names.")
2167 (defun stgit-toggle-worktree (&optional arg)
2168 "Toggle the visibility of the work tree.
2169 With ARG, show the work tree if ARG is positive.
2171 Its initial setting is controlled by `stgit-default-show-worktree'.
2173 `stgit-show-worktree-mode' controls where on screen the index and
2174 work tree will show up."
2177 (setq stgit-show-worktree
2180 (not stgit-show-worktree)))
2183 (defun stgit-toggle-ignored (&optional arg)
2184 "Toggle the visibility of files ignored by git in the work
2185 tree. With ARG, show these files if ARG is positive.
2187 Use \\[stgit-toggle-worktree] to show the work tree."
2190 (setq stgit-show-ignored
2193 (not stgit-show-ignored)))
2196 (defun stgit-toggle-unknown (&optional arg)
2197 "Toggle the visibility of files not registered with git in the
2198 work tree. With ARG, show these files if ARG is positive.
2200 Use \\[stgit-toggle-worktree] to show the work tree."
2203 (setq stgit-show-unknown
2206 (not stgit-show-unknown)))
2209 (defun stgit-toggle-patch-names (&optional arg)
2210 "Toggle the visibility of patch names. With ARG, show patch names
2213 The initial setting is controlled by `stgit-default-show-patch-names'."
2216 (setq stgit-show-patch-names
2219 (not stgit-show-patch-names)))