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"))
20 (require 'format-spec)
22 (defun stgit-set-default (symbol value)
23 "Set default value of SYMBOL to VALUE using `set-default' and
24 reload all StGit buffers."
25 (set-default symbol value)
26 (dolist (buf (buffer-list))
27 (with-current-buffer buf
28 (when (eq major-mode 'stgit-mode)
32 "A user interface for the StGit patch maintenance tool."
34 :link '(function-link stgit)
35 :link '(url-link "http://www.procode.org/stgit/"))
37 (defcustom stgit-abbreviate-copies-and-renames t
38 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
39 instead of \"dir/old/file -> dir/new/file\"."
42 :set 'stgit-set-default)
44 (defcustom stgit-default-show-worktree t
45 "Set to non-nil to by default show the working tree in a new stgit buffer.
47 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle this \
48 setting in an already-started StGit buffer."
51 :link '(variable-link stgit-show-worktree))
53 (defcustom stgit-default-show-unknown nil
54 "Set to non-nil to by default show unknown files a new stgit buffer.
56 Use \\<stgit-mode-map>\\[stgit-toggle-unknown] to toggle this \
57 setting in an already-started StGit buffer."
60 :link '(variable-link stgit-show-unknown))
62 (defcustom stgit-default-show-ignored nil
63 "Set to non-nil to by default show ignored files a new stgit buffer.
65 Use \\<stgit-mode-map>\\[stgit-toggle-ignored] to toggle this \
66 setting in an already-started StGit buffer."
69 :link '(variable-link stgit-show-ignored))
71 (defcustom stgit-default-show-svn t
72 "Set to non-nil to by default show subversion information in a
75 Use \\<stgit-mode-map>\\[stgit-toggle-svn] to toggle this \
76 setting in an already-started StGit buffer."
79 :link '(variable-link stgit-show-worktree))
81 (defcustom stgit-find-copies-harder nil
82 "Try harder to find copied files when listing patches.
84 When not nil, runs git diff-tree with the --find-copies-harder
85 flag, which reduces performance."
88 :set 'stgit-set-default)
90 (defcustom stgit-show-worktree-mode 'center
91 "This variable controls where the \"Index\" and \"Work tree\"
92 will be shown on in the buffer.
94 It can be set to 'top (above all patches), 'center (show between
95 applied and unapplied patches), and 'bottom (below all patches)."
96 :type '(radio (const :tag "above all patches (top)" top)
97 (const :tag "between applied and unapplied patches (center)"
99 (const :tag "below all patches (bottom)" bottom))
101 :link '(variable-link stgit-show-worktree)
102 :set 'stgit-set-default)
104 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
105 "The format string used to format patch lines.
106 The format string is passed to `format-spec' and the following
107 format characters are recognized:
109 %s - A '+', '-', '>' or space, depending on whether the patch is
110 applied, unapplied, top, or something else.
112 %m - An asterisk if the patch is marked, and a space otherwise.
116 %e - The string \"(empty) \" if the patch is empty.
118 %d - The short patch description.
120 %D - The short patch description, or the patch name.
122 When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
123 variable is used instead."
126 :set 'stgit-set-default)
128 (defcustom stgit-noname-patch-line-format "%s%m%e%D"
129 "The alternate format string used to format patch lines.
130 It has the same semantics as `stgit-patch-line-format', and the
131 display can be toggled between the two formats using \
132 \\<stgit-mode-map>\\[stgit-toggle-patch-names].
134 The alternate form is used when the patch name is hidden."
137 :set 'stgit-set-default)
139 (defcustom stgit-default-show-committed nil
140 "Set to nil to inhibit showing of historical git commits by default.
142 Use \\<stgit-mode-map>\\[stgit-toggle-committed] \
143 to toggle this setting and to control how many commits are
147 :link '(variable-link stgit-default-committed-count)
148 :link '(variable-link stgit-show-committed))
150 (defcustom stgit-default-committed-count 5
151 "The number of historical commits to show when `stgit-show-committed'
154 :link '(variable-link stgit-default-show-committed)
155 :link '(variable-link stgit-committed-count))
157 (defcustom stgit-default-show-patch-names t
158 "If non-nil, default to showing patch names in a new stgit buffer.
160 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] \
161 to toggle the this setting in an already-started StGit buffer."
164 :link '(variable-link stgit-show-patch-names))
166 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
167 "The format string used to format file lines.
168 The format string is passed to `format-spec' and the following
169 format characters are recognized:
171 %s - A string describing the status of the file.
173 %m - Mode change information
177 %c - A description of file changes."
180 :set 'stgit-set-default)
182 (defface stgit-branch-name-face
184 "The face used for the StGit branch name"
187 (defface stgit-top-patch-face
188 '((((background dark)) (:weight bold :foreground "yellow"))
189 (((background light)) (:weight bold :foreground "purple"))
191 "The face used for the top patch names"
194 (defface stgit-applied-patch-face
195 '((((background dark)) (:foreground "light yellow"))
196 (((background light)) (:foreground "purple"))
198 "The face used for applied patch names"
201 (defface stgit-unapplied-patch-face
202 '((((background dark)) (:foreground "gray80"))
203 (((background light)) (:foreground "orchid"))
205 "The face used for unapplied patch names"
208 (defface stgit-committed-patch-face
209 '((((background dark)) (:foreground "gray50"))
210 (((background light)) (:foreground "gray50"))
212 "The face used for already committed patch names"
215 (defface stgit-description-face
216 '((((background dark)) (:foreground "tan"))
217 (((background light)) (:foreground "dark red")))
218 "The face used for StGit descriptions"
221 (defface stgit-index-work-tree-title-face
222 '((((supports :slant italic)) :slant italic)
224 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
227 (defface stgit-unmerged-file-face
228 '((((class color) (background light)) (:foreground "red" :bold t))
229 (((class color) (background dark)) (:foreground "red" :bold t)))
230 "StGit mode face used for unmerged file status"
233 (defface stgit-unknown-file-face
234 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
235 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
236 "StGit mode face used for unknown file status"
239 (defface stgit-ignored-file-face
240 '((((class color) (background light)) (:foreground "grey60"))
241 (((class color) (background dark)) (:foreground "grey40")))
242 "StGit mode face used for ignored files")
244 (defface stgit-file-permission-face
245 '((((class color) (background light)) (:foreground "green" :bold t))
246 (((class color) (background dark)) (:foreground "green" :bold t)))
247 "StGit mode face used for permission changes."
250 (defface stgit-modified-file-face
251 '((((class color) (background light)) (:foreground "purple"))
252 (((class color) (background dark)) (:foreground "salmon")))
253 "StGit mode face used for modified file status"
257 "Manage StGit patches for the tree in DIR.
259 See `stgit-mode' for commands available."
260 (interactive "DDirectory: \n")
261 (switch-to-stgit-buffer (git-get-top-dir dir))
264 (defun stgit-assert-mode ()
265 "Signal an error if not in an StGit buffer."
266 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
268 (unless (fboundp 'git-get-top-dir)
269 (defun git-get-top-dir (dir)
270 "Retrieve the top-level directory of a git tree."
271 (let ((cdup (with-output-to-string
272 (with-current-buffer standard-output
274 (unless (eq 0 (call-process "git" nil t nil
275 "rev-parse" "--show-cdup"))
276 (error "Cannot find top-level git tree for %s" dir))))))
277 (expand-file-name (concat (file-name-as-directory dir)
278 (car (split-string cdup "\n")))))))
280 (defun stgit-refresh-git-status (&optional dir)
281 "If it exists, refresh the `git-status' buffer belonging to
282 directory DIR or `default-directory'"
283 (when (and (fboundp 'git-find-status-buffer)
284 (fboundp 'git-refresh-status))
285 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
286 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
287 (when git-status-buffer
288 (with-current-buffer git-status-buffer
289 (git-refresh-status))))))
291 (defun stgit-find-buffer (dir)
292 "Return the buffer displaying StGit patches for DIR, or nil if none."
293 (setq dir (file-name-as-directory dir))
294 (let ((buffers (buffer-list)))
296 (not (with-current-buffer (car buffers)
297 (and (eq major-mode 'stgit-mode)
298 (string= default-directory dir)))))
299 (setq buffers (cdr buffers)))
300 (and buffers (car buffers))))
302 (defun switch-to-stgit-buffer (dir)
303 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
304 (setq dir (file-name-as-directory dir))
305 (let ((buffer (stgit-find-buffer dir)))
306 (switch-to-buffer (or buffer
307 (create-stgit-buffer dir)))))
309 (defstruct (stgit-patch
310 (:conc-name stgit-patch->))
311 status name desc empty files-ewoc)
313 (defun stgit-patch-display-name (patch)
314 (let ((name (stgit-patch->name patch)))
318 (t (symbol-name name)))))
320 (defun stgit-insert-without-trailing-whitespace (text)
321 "Insert TEXT in buffer using `insert', without trailing whitespace.
322 A newline is appended."
323 (unless (string-match "\\(.*?\\) *$" text)
325 (insert (match-string 1 text) ?\n))
327 (defun stgit-line-format ()
328 "Return the current line format; one of
329 `stgit-patch-line-format' and `stgit-noname-patch-line-format'"
330 (if stgit-show-patch-names
331 stgit-patch-line-format
332 stgit-noname-patch-line-format))
334 (defun stgit-patch-pp (patch)
335 (let* ((status (stgit-patch->status patch))
337 (name (stgit-patch->name patch))
338 (face (cdr (assq status stgit-patch-status-face-alist)))
339 (fmt (stgit-line-format))
340 (spec (format-spec-make
346 ?m (if (memq name stgit-marked-patches)
348 ?n (propertize (stgit-patch-display-name patch)
350 'syntax-table (string-to-syntax "w"))
351 ?e (if (stgit-patch->empty patch) "(empty) " "")
352 ?d (propertize (or (stgit-patch->desc patch) "")
353 'face 'stgit-description-face)
354 ?D (propertize (let ((desc (stgit-patch->desc patch)))
355 (if (zerop (length desc))
356 (stgit-patch-display-name patch)
359 (text (format-spec fmt spec)))
361 (stgit-insert-without-trailing-whitespace text)
362 (put-text-property start (point) 'entry-type 'patch)
363 (when (memq name stgit-expanded-patches)
364 (stgit-insert-patch-files patch))
365 (put-text-property start (point) 'patch-data patch)))
367 (defun create-stgit-buffer (dir)
368 "Create a buffer for showing StGit patches.
369 Argument DIR is the repository path."
370 (let ((buf (create-file-buffer (concat dir "*stgit*")))
371 (inhibit-read-only t))
372 (with-current-buffer buf
373 (setq default-directory dir)
375 (set (make-local-variable 'stgit-ewoc)
376 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
377 (setq buffer-read-only t))
380 (def-edebug-spec stgit-capture-output
382 (defmacro stgit-capture-output (name &rest body)
383 "Capture StGit output and, if there was any output, show it in a window
385 Returns nil if there was no output."
386 (declare (debug ([&or stringp null] body))
388 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
389 (stgit-dir default-directory)
390 (inhibit-read-only t))
391 (with-current-buffer output-buf
392 (buffer-disable-undo)
394 (setq default-directory stgit-dir)
395 (setq buffer-read-only t))
396 (let ((standard-output output-buf))
398 (with-current-buffer output-buf
399 (set-buffer-modified-p nil)
400 (setq buffer-read-only t)
401 (if (< (point-min) (point-max))
402 (display-buffer output-buf t)))))
404 (defun stgit-make-run-args (args)
405 "Return a copy of ARGS with its elements converted to strings."
407 ;; don't use (format "%s" ...) to limit type errors
408 (cond ((stringp x) x)
409 ((integerp x) (number-to-string x))
410 ((symbolp x) (symbol-name x))
412 (error "Bad element in stgit-make-run-args args: %S" x))))
415 (defvar stgit-inhibit-messages nil
416 "Set to non-nil to inhibit messages when running `stg' commands.
417 See also `stgit-message'.")
418 (defun stgit-message (format-spec &rest args)
419 "Call `message' on the arguments unless `stgit-inhibit-messages' is non-nil."
420 (unless stgit-inhibit-messages
421 (apply 'message format-spec args)))
423 (defun stgit-run (&rest args)
424 (setq args (stgit-make-run-args args))
425 (let ((msgcmd (mapconcat #'identity args " ")))
426 (stgit-message "Running stg %s..." msgcmd)
428 (apply 'call-process "stg" nil standard-output nil args)
429 (stgit-message "Running stg %s...done" msgcmd))))
431 (defun stgit-run-silent (&rest args)
432 (let ((stgit-inhibit-messages t))
433 (apply 'stgit-run args)))
435 (defun stgit-run-git (&rest args)
436 (setq args (stgit-make-run-args args))
437 (let ((msgcmd (mapconcat #'identity args " ")))
438 (stgit-message "Running git %s..." msgcmd)
440 (apply 'call-process "git" nil standard-output nil args)
441 (stgit-message "Running git %s...done" msgcmd))))
443 (defun stgit-run-git-silent (&rest args)
444 (let ((stgit-inhibit-messages t))
445 (apply 'stgit-run-git args)))
447 (defun stgit-index-empty-p ()
448 "Returns non-nil if the index contains no changes from HEAD."
449 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
451 (defun stgit-work-tree-empty-p ()
452 "Returns non-nil if the work tree contains no changes from index."
453 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
455 (defvar stgit-did-advise nil
456 "Set to non-nil if appropriate (non-stgit) git functions have
457 been advised to update the stgit status when necessary.")
459 (defconst stgit-allowed-branch-name-re
460 ;; Disallow control characters, space, del, and "/:@^{}~" in
461 ;; "/"-separated parts; parts may not start with a period (.)
462 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
463 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
464 "Regular expression that (new) branch names must match.")
466 (defun stgit-refresh-index ()
467 (when stgit-index-node
468 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
470 (defun stgit-refresh-worktree ()
471 (when stgit-worktree-node
472 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
474 (defun stgit-run-series-insert-index (ewoc)
475 (setq index-node (cons ewoc (ewoc-enter-last ewoc
481 worktree-node (cons ewoc (ewoc-enter-last ewoc
488 (defun stgit-svn-find-rev (sha1 hash)
489 "Return the subversion revision corresponding to SHA1 as
492 Cached data is stored in HASH, which must have been created
493 using (make-hash-table :test 'equal)."
494 (let ((result (gethash sha1 hash t)))
496 (let ((svn-rev (with-output-to-string
497 (stgit-run-git-silent "svn" "find-rev"
499 (setq result (when (string-match "\\`[0-9]+" svn-rev)
500 (string-to-number (match-string 0 svn-rev))))
501 (puthash sha1 result hash)))
504 (defun stgit-run-series (ewoc)
505 (setq stgit-index-node nil
506 stgit-worktree-node nil)
508 (when stgit-show-committed
509 (let* ((show-svn stgit-show-svn)
510 (svn-hash stgit-svn-find-rev-hash)
511 (base (stgit-id "{base}"))
512 (range (format "%s~%d..%s" base stgit-committed-count base)))
514 (let* ((standard-output (current-buffer))
515 (fmt (stgit-line-format))
516 (commit-abbrev (when (string-match "%-\\([0-9]+\\)n" fmt)
517 (string-to-number (match-string 1 fmt))))
518 (exit-status (stgit-run-git-silent "--no-pager" "log"
522 (goto-char (point-min))
523 (if (not (zerop exit-status))
524 (message "Failed to run git log")
527 "\\([0-9a-f]+\\)\\(\\.\\.\\.\\)? \\(.*\\)")
528 (error "Syntax error in output from git log"))
529 (let* ((state 'committed)
530 (name (match-string 1))
531 (desc (match-string 3))
535 (let ((svn-rev (stgit-svn-find-rev name svn-hash)))
537 (setq desc (format "(r%s) %s" svn-rev desc)))))
540 (< commit-abbrev (length name))
541 (setq name (substring name 0 commit-abbrev)))
543 (setq name (intern name))
545 (setq all-patchsyms (cons name all-patchsyms))
546 (ewoc-enter-last ewoc
552 (forward-line 1)))))))
553 (let ((inserted-index (not stgit-show-worktree))
557 (let* ((standard-output (current-buffer))
558 (exit-status (stgit-run-silent "series"
559 "--description" "--empty")))
560 (goto-char (point-min))
561 (if (not (zerop exit-status))
562 (cond ((looking-at "stg series: \\(.*\\)")
563 (setq inserted-index t)
564 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
565 (substitute-command-keys
566 "-- not initialized; run \\[stgit-init]")))
568 (error "Error running stg: %s"
572 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
573 (error "Syntax error in output from stg series"))
574 (let* ((state-str (match-string 2))
575 (state (cond ((string= state-str ">") 'top)
576 ((string= state-str "+") 'applied)
577 ((string= state-str "-") 'unapplied)))
578 (name (intern (match-string 4)))
579 (desc (match-string 5))
580 (empty (string= (match-string 1) "0")))
581 (unless inserted-index
582 (when (or (eq stgit-show-worktree-mode 'top)
583 (and (eq stgit-show-worktree-mode 'center)
584 (eq state 'unapplied)))
585 (setq inserted-index t)
586 (stgit-run-series-insert-index ewoc)))
587 (setq all-patchsyms (cons name all-patchsyms))
588 (ewoc-enter-last ewoc
595 (unless inserted-index
596 (stgit-run-series-insert-index ewoc))
597 (setq stgit-index-node index-node
598 stgit-worktree-node worktree-node
599 stgit-marked-patches (intersection stgit-marked-patches
602 (defun stgit-current-branch ()
603 "Return the name of the current branch."
604 (substring (with-output-to-string
605 (stgit-run-silent "branch"))
608 (defun stgit-reload (&optional description)
609 "Update the contents of the StGit buffer.
611 If DESCRIPTION is non-nil, it is displayed as a status message
612 during the operation."
616 (message "%s..." description))
617 (let ((inhibit-read-only t)
618 (curline (line-number-at-pos))
619 (curpatch (stgit-patch-name-at-point))
620 (curfile (stgit-patched-file-at-point))
621 (stgit-inhibit-messages description))
622 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
623 (ewoc-set-hf stgit-ewoc
625 (propertize (stgit-current-branch)
626 'face 'stgit-branch-name-face)
628 (if stgit-show-worktree
631 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
632 shows the working tree\n")
633 'face 'stgit-description-face)))
634 (stgit-run-series stgit-ewoc)
635 (unless (and curpatch
636 (stgit-goto-patch curpatch
637 (and curfile (stgit-file->file curfile))))
638 (goto-char (point-min))
639 (forward-line (1- curline))
640 (move-to-column (stgit-goal-column)))
641 (stgit-refresh-git-status))
643 (message "%s...done" description)))
645 (defconst stgit-file-status-code-strings
646 (mapcar (lambda (arg)
648 (propertize (cadr arg) 'face (car (cddr arg)))))
649 '((add "Added" stgit-modified-file-face)
650 (copy "Copied" stgit-modified-file-face)
651 (delete "Deleted" stgit-modified-file-face)
652 (modify "Modified" stgit-modified-file-face)
653 (rename "Renamed" stgit-modified-file-face)
654 (mode-change "Mode change" stgit-modified-file-face)
655 (unmerged "Unmerged" stgit-unmerged-file-face)
656 (unknown "Unknown" stgit-unknown-file-face)
657 (ignore "Ignored" stgit-ignored-file-face)))
658 "Alist of code symbols to description strings")
660 (defconst stgit-patch-status-face-alist
661 '((applied . stgit-applied-patch-face)
662 (top . stgit-top-patch-face)
663 (unapplied . stgit-unapplied-patch-face)
664 (committed . stgit-committed-patch-face)
665 (index . stgit-index-work-tree-title-face)
666 (work . stgit-index-work-tree-title-face))
667 "Alist of face to use for a given patch status")
669 (defun stgit-file-status-code-as-string (file)
670 "Return stgit status code for FILE as a string"
671 (let* ((code (assq (stgit-file->status file)
672 stgit-file-status-code-strings))
673 (score (stgit-file->cr-score file)))
675 (if (and score (/= score 100))
676 (format "%s %s" (cdr code)
677 (propertize (format "%d%%" score)
678 'face 'stgit-description-face))
681 (defun stgit-file-status-code (str &optional score)
682 "Return stgit status code from git status string"
683 (let ((code (assoc str '(("A" . add)
692 (setq code (if code (cdr code) 'unknown))
693 (when (stringp score)
694 (if (> (length score) 0)
695 (setq score (string-to-number score))
697 (if score (cons code score) code)))
699 (defconst stgit-file-type-strings
702 (#o160 . "subproject"))
703 "Alist of names of file types")
705 (defun stgit-file-type-string (type)
706 "Return string describing file type TYPE (the high bits of file permission).
707 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
708 (let ((type-str (assoc type stgit-file-type-strings)))
709 (or (and type-str (cdr type-str))
710 (format "unknown type %o" type))))
712 (defun stgit-file-type-change-string (old-perm new-perm)
713 "Return string describing file type change from OLD-PERM to NEW-PERM.
714 Cf. `stgit-file-type-string'."
715 (let ((old-type (lsh old-perm -9))
716 (new-type (lsh new-perm -9)))
717 (cond ((= old-type new-type) "")
718 ((zerop new-type) "")
720 (if (= new-type #o100)
722 (format "(%s)" (stgit-file-type-string new-type))))
723 (t (format "(%s -> %s)"
724 (stgit-file-type-string old-type)
725 (stgit-file-type-string new-type))))))
727 (defun stgit-file-mode-change-string (old-perm new-perm)
728 "Return string describing file mode change from OLD-PERM to NEW-PERM.
729 Cf. `stgit-file-type-change-string'."
730 (setq old-perm (logand old-perm #o777)
731 new-perm (logand new-perm #o777))
732 (if (or (= old-perm new-perm)
736 (let* ((modified (logxor old-perm new-perm))
737 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
738 (cond ((zerop modified) "")
739 ((and (zerop not-x-modified)
740 (or (and (eq #o111 (logand old-perm #o111))
741 (propertize "-x" 'face 'stgit-file-permission-face))
742 (and (eq #o111 (logand new-perm #o111))
743 (propertize "+x" 'face
744 'stgit-file-permission-face)))))
745 (t (concat (propertize (format "%o" old-perm)
746 'face 'stgit-file-permission-face)
748 'face 'stgit-description-face)
749 (propertize (format "%o" new-perm)
750 'face 'stgit-file-permission-face)))))))
752 (defstruct (stgit-file
753 (:conc-name stgit-file->))
754 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
756 (defun stgit-describe-copy-or-rename (file)
757 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
758 from to common-head common-tail)
760 (when stgit-abbreviate-copies-and-renames
761 (setq from (split-string (stgit-file->cr-from file) "/")
762 to (split-string (stgit-file->cr-to file) "/"))
764 (while (and from to (cdr from) (cdr to)
765 (string-equal (car from) (car to)))
766 (setq common-head (cons (car from) common-head)
769 (setq common-head (nreverse common-head)
772 (while (and from to (cdr from) (cdr to)
773 (string-equal (car from) (car to)))
774 (setq common-tail (cons (car from) common-tail)
777 (setq from (nreverse from)
780 (if (or common-head common-tail)
781 (concat (if common-head
782 (mapconcat #'identity common-head "/")
784 (if common-head "/" "")
785 (propertize "{" 'face 'stgit-description-face)
786 (mapconcat #'identity from "/")
788 (mapconcat #'identity to "/")
789 (propertize "}" 'face 'stgit-description-face)
790 (if common-tail "/" "")
792 (mapconcat #'identity common-tail "/")
794 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
796 (defun stgit-file-pp (file)
797 (let ((start (point))
798 (spec (format-spec-make
799 ?s (stgit-file-status-code-as-string file)
800 ?m (stgit-file-mode-change-string
801 (stgit-file->old-perm file)
802 (stgit-file->new-perm file))
803 ?n (if (stgit-file->copy-or-rename file)
804 (stgit-describe-copy-or-rename file)
805 (stgit-file->file file))
806 ?c (propertize (stgit-file-type-change-string
807 (stgit-file->old-perm file)
808 (stgit-file->new-perm file))
809 'face 'stgit-description-face))))
810 (stgit-insert-without-trailing-whitespace
811 (format-spec stgit-file-line-format spec))
812 (add-text-properties start (point)
813 (list 'entry-type 'file
816 (defun stgit-find-copies-harder-diff-arg ()
817 "Return the flag to use with `git-diff' depending on the
818 `stgit-find-copies-harder' flag."
819 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
821 (defun stgit-insert-ls-files (args file-flag)
822 (let ((start (point)))
823 (apply 'stgit-run-git
824 (append '("ls-files" "--exclude-standard" "-z") args))
826 (while (looking-at "\\([^\0]*\\)\0")
827 (let ((name-len (- (match-end 0) (match-beginning 0))))
828 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
829 (forward-char name-len)))))
831 (defun stgit-process-files (callback)
832 (goto-char (point-min))
833 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
834 (goto-char (match-end 0)))
835 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
836 (let ((old-perm (string-to-number (match-string 1) 8))
837 (new-perm (string-to-number (match-string 2) 8)))
838 (goto-char (match-end 0))
841 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
842 (let* ((patch-status (stgit-patch->status patch))
843 (file-subexp (if (eq patch-status 'unapplied)
846 (file (match-string file-subexp)))
851 :cr-score (string-to-number (match-string 2))
852 :cr-from (match-string 3)
853 :cr-to (match-string 4)
854 :status (stgit-file-status-code
857 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
865 :status (stgit-file-status-code
867 :file (match-string 2))))))
868 (goto-char (match-end 0))
869 (funcall callback file)))))
872 (defun stgit-insert-patch-files (patch)
873 "Expand (show modification of) the patch PATCH after the line
875 (let* ((patchsym (stgit-patch->name patch))
877 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
878 (ewoc (ewoc-create #'stgit-file-pp nil nil t))
879 (show-ignored stgit-show-ignored)
880 (show-unknown stgit-show-unknown))
881 (set-marker-insertion-type end t)
882 (setf (stgit-patch->files-ewoc patch) ewoc)
884 (let ((standard-output (current-buffer)))
885 (apply 'stgit-run-git
886 (cond ((eq patchsym :work)
887 (let (standard-output)
888 (stgit-run-git "update-index" "--refresh"))
889 `("diff-files" "-0" ,@args))
890 ((eq patchsym :index)
891 `("diff-index" ,@args "--cached" "HEAD"))
893 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
895 (when (and (eq patchsym :work))
897 (stgit-insert-ls-files '("--ignored" "--others") "I"))
899 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
902 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
903 (point-min) (point-max)))
905 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
907 (unless (ewoc-nth ewoc 0)
910 (propertize "<no files>"
911 'face 'stgit-description-face)
915 (defun stgit-find-file (&optional other-window)
916 (let* ((file (or (stgit-patched-file-at-point)
917 (error "No file at point")))
918 (filename (expand-file-name (stgit-file->file file))))
919 (unless (file-exists-p filename)
920 (error "File does not exist"))
921 (funcall (if other-window 'find-file-other-window 'find-file)
923 (when (eq (stgit-file->status file) 'unmerged)
926 (defun stgit-expand (&optional patches collapse)
927 "Show the contents of marked patches, or the patch at point.
929 See also `stgit-collapse'.
931 Non-interactively, operate on PATCHES, and collapse instead of
932 expand if COLLAPSE is not nil."
933 (interactive (list (stgit-patches-marked-or-at-point t)))
935 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
936 patches stgit-expanded-patches)))
937 (setq stgit-expanded-patches
939 (set-difference stgit-expanded-patches patches-diff)
940 (append stgit-expanded-patches patches-diff)))
941 (ewoc-map #'(lambda (patch)
942 (memq (stgit-patch->name patch) patches-diff))
944 (move-to-column (stgit-goal-column)))
946 (defun stgit-collapse (&optional patches)
947 "Hide the contents of marked patches, or the patch at point.
949 See also `stgit-expand'."
950 (interactive (list (stgit-patches-marked-or-at-point t)))
952 (stgit-expand patches t))
954 (defun stgit-select-patch ()
955 (let ((patchname (stgit-patch-name-at-point)))
956 (stgit-expand (list patchname)
957 (memq patchname stgit-expanded-patches))))
959 (defun stgit-expand-directory (file)
960 (let* ((patch (stgit-patch-at-point))
961 (ewoc (stgit-patch->files-ewoc patch))
962 (node (ewoc-locate ewoc))
963 (filename (stgit-file->file file))
964 (start (make-marker))
969 (set-marker start (point))
970 (set-marker end (point))
971 (set-marker-insertion-type end t))
973 (assert (string-match "/$" filename))
974 ;; remove trailing "/"
975 (setf (stgit-file->file file) (substring filename 0 -1))
976 (ewoc-invalidate ewoc node)
979 (let ((standard-output (current-buffer)))
980 (stgit-insert-ls-files (list "--directory" "--others"
981 "--no-empty-directory" "--"
984 (stgit-process-files (lambda (f)
985 (setq node (ewoc-enter-after ewoc node f))))))
987 (move-to-column (stgit-goal-column))
989 (let ((inhibit-read-only t))
990 (put-text-property start end 'patch-data patch))))
992 (defun stgit-select-file ()
993 (let* ((file (or (stgit-patched-file-at-point)
994 (error "No file at point")))
995 (filename (stgit-file->file file)))
996 (if (string-match "/$" filename)
997 (stgit-expand-directory file)
1000 (defun stgit-select ()
1001 "With point on a patch, toggle showing files in the patch.
1003 With point on a file, open the associated file. Opens the target
1004 file for (applied) copies and renames."
1007 (case (get-text-property (point) 'entry-type)
1009 (stgit-select-patch))
1011 (stgit-select-file))
1013 (error "No patch or file on line"))))
1015 (defun stgit-find-file-other-window ()
1016 "Open file at point in other window"
1019 (stgit-find-file t))
1021 (defun stgit-find-file-merge ()
1022 "Open file at point and merge it using `smerge-ediff'."
1028 (defun stgit-quit ()
1029 "Hide the stgit buffer."
1034 (defun stgit-git-status ()
1035 "Show status using `git-status'."
1038 (unless (fboundp 'git-status)
1039 (error "The stgit-git-status command requires git-status"))
1040 (let ((dir default-directory))
1041 (save-selected-window
1045 (defun stgit-goal-column ()
1046 "Return goal column for the current line"
1047 (case (get-text-property (point) 'entry-type)
1052 (defun stgit-next-line (&optional arg)
1053 "Move cursor vertically down ARG lines"
1057 (move-to-column (stgit-goal-column)))
1059 (defun stgit-previous-line (&optional arg)
1060 "Move cursor vertically up ARG lines"
1064 (move-to-column (stgit-goal-column)))
1066 (defun stgit-next-patch (&optional arg)
1067 "Move cursor down ARG patches."
1070 (ewoc-goto-next stgit-ewoc (or arg 1))
1071 (move-to-column goal-column))
1073 (defun stgit-previous-patch (&optional arg)
1074 "Move cursor up ARG patches."
1077 (ewoc-goto-prev stgit-ewoc (or arg 1))
1078 (move-to-column goal-column))
1080 (defvar stgit-mode-hook nil
1081 "Run after `stgit-mode' is setup.")
1083 (defvar stgit-mode-map nil
1084 "Keymap for StGit major mode.")
1086 (unless stgit-mode-map
1087 (let ((diff-map (make-sparse-keymap))
1088 (toggle-map (make-sparse-keymap)))
1089 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
1090 '(("b" . stgit-diff-base)
1091 ("c" . stgit-diff-combined)
1092 ("m" . stgit-find-file-merge)
1093 ("o" . stgit-diff-ours)
1094 ("r" . stgit-diff-range)
1095 ("t" . stgit-diff-theirs)))
1096 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
1097 '(("n" . stgit-toggle-patch-names)
1098 ("t" . stgit-toggle-worktree)
1099 ("h" . stgit-toggle-committed)
1100 ("i" . stgit-toggle-ignored)
1101 ("u" . stgit-toggle-unknown)
1102 ("s" . stgit-toggle-svn)))
1103 (setq stgit-mode-map (make-keymap))
1104 (suppress-keymap stgit-mode-map)
1105 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
1106 `((" " . stgit-mark-down)
1107 ("m" . stgit-mark-down)
1108 ("\d" . stgit-unmark-up)
1109 ("u" . stgit-unmark-down)
1112 ("\C-p" . stgit-previous-line)
1113 ("\C-n" . stgit-next-line)
1114 ([up] . stgit-previous-line)
1115 ([down] . stgit-next-line)
1116 ("p" . stgit-previous-patch)
1117 ("n" . stgit-next-patch)
1118 ("\M-{" . stgit-previous-patch)
1119 ("\M-}" . stgit-next-patch)
1120 ("s" . stgit-git-status)
1121 ("g" . stgit-reload-or-repair)
1122 ("r" . stgit-refresh)
1123 ("\C-c\C-r" . stgit-rename)
1125 ("M" . stgit-move-patches)
1126 ("S" . stgit-squash)
1128 ("c" . stgit-new-and-refresh)
1129 ("\C-c\C-c" . stgit-commit)
1130 ("\C-c\C-u" . stgit-uncommit)
1131 ("U" . stgit-revert)
1132 ("R" . stgit-resolve-file)
1133 ("\r" . stgit-select)
1134 ("+" . stgit-expand)
1135 ("-" . stgit-collapse)
1136 ("o" . stgit-find-file-other-window)
1137 ("i" . stgit-toggle-index)
1138 (">" . stgit-push-next)
1139 ("<" . stgit-pop-next)
1140 ("P" . stgit-push-or-pop)
1143 ("D" . stgit-delete)
1144 ([?\C-/] . stgit-undo)
1145 ("\C-_" . stgit-undo)
1146 ([?\C-c ?\C-/] . stgit-redo)
1147 ("\C-c\C-_" . stgit-redo)
1148 ("B" . stgit-branch)
1149 ("\C-c\C-b" . stgit-rebase)
1153 ("!" . stgit-execute))))
1155 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1156 (and file (eq (stgit-file->status file)
1158 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1159 (easy-menu-define stgit-menu stgit-mode-map
1162 ["Reload" stgit-reload-or-repair
1163 :help "Reload StGit status from disk"]
1164 ["Repair" stgit-repair
1165 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1166 :help "Repair StGit metadata"]
1168 ["Undo" stgit-undo t]
1169 ["Redo" stgit-redo t]
1171 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1173 ["New patch" stgit-new-and-refresh
1174 :help "Create a new patch from changes in index or work tree"
1175 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1176 ["New empty patch" stgit-new
1177 :help "Create a new, empty patch"]
1178 ["(Un)mark patch" stgit-toggle-mark
1179 :label (if (memq (stgit-patch-name-at-point nil t)
1180 stgit-marked-patches)
1181 "Unmark patch" "Mark patch")
1182 :active (stgit-patch-name-at-point nil t)]
1183 ["Expand/collapse patch"
1184 (let ((patches (stgit-patches-marked-or-at-point)))
1185 (if (member-if ,patch-collapsed-p patches)
1186 (stgit-expand patches)
1187 (stgit-collapse patches)))
1188 :label (if (member-if ,patch-collapsed-p
1189 (stgit-patches-marked-or-at-point))
1192 :active (stgit-patches-marked-or-at-point)]
1193 ["Edit patch" stgit-edit
1194 :help "Edit patch comment"
1195 :active (stgit-patch-name-at-point nil t)]
1196 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1197 ["Push/pop patch" stgit-push-or-pop
1198 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1199 (stgit-applied-patchsyms t))
1200 "Pop patches" "Push patches")]
1201 ["Delete patches" stgit-delete
1202 :active (stgit-patches-marked-or-at-point nil t)]
1204 ["Move patches" stgit-move-patches
1205 :active stgit-marked-patches
1206 :help "Move marked patch(es) to point"]
1207 ["Squash patches" stgit-squash
1208 :active (> (length stgit-marked-patches) 1)
1209 :help "Merge marked patches into one"]
1211 ["Refresh top patch" stgit-refresh
1212 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1213 :help "Refresh the top patch with changes in index or work tree"]
1214 ["Refresh this patch" (stgit-refresh t)
1215 :keys "\\[universal-argument] \\[stgit-refresh]"
1216 :help "Refresh marked patch with changes in index or work tree"
1217 :active (and (not (and (stgit-index-empty-p)
1218 (stgit-work-tree-empty-p)))
1219 (stgit-patch-name-at-point nil t))]
1221 ["Find file" stgit-select
1222 :active (eq (get-text-property (point) 'entry-type) 'file)]
1223 ["Open file" stgit-find-file-other-window
1224 :active (eq (get-text-property (point) 'entry-type) 'file)]
1225 ["Toggle file index" stgit-toggle-index
1226 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1227 (memq (stgit-patch-name-at-point) '(:work :index)))
1228 :label (if (eq (stgit-patch-name-at-point) :work)
1229 "Move change to index"
1230 "Move change to work tree")]
1232 ["Show diff" stgit-diff
1233 :active (get-text-property (point) 'entry-type)]
1234 ["Show diff for range of applied patches" stgit-diff-range
1235 :active (= (length stgit-marked-patches) 1)]
1237 :active (stgit-git-index-unmerged-p)
1238 ["Combined diff" stgit-diff-combined
1239 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1240 ["Diff against base" stgit-diff-base
1241 :help "Show diff against the common base"
1242 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1243 ["Diff against ours" stgit-diff-ours
1244 :help "Show diff against our branch"
1245 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1246 ["Diff against theirs" stgit-diff-theirs
1247 :help "Show diff against their branch"
1248 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1250 ["Interactive merge" stgit-find-file-merge
1251 :help "Interactively merge the file"
1252 :active ,at-unmerged-file]
1253 ["Resolve file" stgit-resolve-file
1254 :help "Mark file conflict as resolved"
1255 :active ,at-unmerged-file]
1258 ["Show index & work tree" stgit-toggle-worktree :style toggle
1259 :selected stgit-show-worktree]
1260 ["Show unknown files" stgit-toggle-unknown :style toggle
1261 :selected stgit-show-unknown :active stgit-show-worktree]
1262 ["Show ignored files" stgit-toggle-ignored :style toggle
1263 :selected stgit-show-ignored :active stgit-show-worktree]
1264 ["Show patch names" stgit-toggle-patch-names :style toggle
1265 :selected stgit-show-patch-names]
1266 ["Show recent commits" stgit-toggle-committed :style toggle
1267 :selected stgit-show-committed]
1268 ["Show subversion info" stgit-toggle-svn :style toggle
1269 :selected stgit-show-svn]
1271 ["Switch branches" stgit-branch t
1272 :help "Switch to or create another branch"]
1273 ["Rebase branch" stgit-rebase t
1274 :help "Rebase the current branch"]
1277 ;; disable tool bar editing buttons
1278 (put 'stgit-mode 'mode-class 'special)
1280 (defun stgit-mode ()
1281 "Major mode for interacting with StGit.
1283 Start StGit using \\[stgit].
1287 \\[stgit-help] Show this help text
1288 \\[stgit-quit] Hide the StGit buffer
1289 \\[describe-bindings] Show all key bindings
1291 \\[stgit-reload-or-repair] Reload the StGit buffer
1292 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1294 \\[stgit-undo] Undo most recent StGit operation
1295 \\[stgit-redo] Undo recent undo
1297 \\[stgit-git-status] Run `git-status' (if available)
1299 \\[stgit-execute] Run an stg shell command
1302 \\[stgit-previous-line] Move to previous line
1303 \\[stgit-next-line] Move to next line
1304 \\[stgit-previous-patch] Move to previous patch
1305 \\[stgit-next-patch] Move to next patch
1307 \\[stgit-mark-down] Mark patch and move down
1308 \\[stgit-unmark-up] Unmark patch and move up
1309 \\[stgit-unmark-down] Unmark patch and move down
1311 Commands for patches:
1312 \\[stgit-select] Toggle showing changed files in patch
1313 \\[stgit-refresh] Refresh patch with changes in index or work tree
1314 \\[stgit-diff] Show the patch log and diff
1316 \\[stgit-expand] Show changes in marked patches
1317 \\[stgit-collapse] Hide changes in marked patches
1319 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1320 \\[stgit-new] Create a new, empty patch
1322 \\[stgit-rename] Rename patch
1323 \\[stgit-edit] Edit patch description
1324 \\[stgit-delete] Delete patch(es)
1326 \\[stgit-revert] Revert all changes in index or work tree
1327 \\[stgit-toggle-index] Toggle all changes between index and work tree
1329 \\[stgit-push-next] Push next patch onto stack
1330 \\[stgit-pop-next] Pop current patch from stack
1331 \\[stgit-push-or-pop] Push or pop marked patches
1332 \\[stgit-goto] Make patch at point current by popping or pushing
1334 \\[stgit-squash] Squash (meld together) patches
1335 \\[stgit-move-patches] Move marked patches to point
1337 \\[stgit-commit] Commit patch(es)
1338 \\[stgit-uncommit] Uncommit patch(es)
1341 \\[stgit-select] Open the file in this window
1342 \\[stgit-find-file-other-window] Open the file in another window
1343 \\[stgit-diff] Show the file's diff
1345 \\[stgit-toggle-index] Toggle change between index and work tree
1346 \\[stgit-revert] Revert changes to file
1349 \\[stgit-toggle-patch-names] Toggle showing patch names
1350 \\[stgit-toggle-worktree] Toggle showing index and work tree
1351 \\[stgit-toggle-unknown] Toggle showing unknown files
1352 \\[stgit-toggle-ignored] Toggle showing ignored files
1353 \\[stgit-toggle-committed] Toggle showing recent commits
1354 \\[stgit-toggle-svn] Toggle showing subversion information
1357 \\[stgit-diff] Show diff of patch or file
1358 \\[stgit-diff-range] Show diff for range of patches
1359 \\[stgit-diff-base] Show diff against the merge base
1360 \\[stgit-diff-ours] Show diff against our branch
1361 \\[stgit-diff-theirs] Show diff against their branch
1363 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1364 ignore space changes.
1365 With two prefix arguments (e.g., \\[universal-argument] \
1366 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1368 Commands for merge conflicts:
1369 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1370 \\[stgit-resolve-file] Mark unmerged file as resolved
1372 Commands for branches:
1373 \\[stgit-branch] Switch to or create another branch
1374 \\[stgit-rebase] Rebase the current branch
1376 Customization variables:
1377 `stgit-abbreviate-copies-and-renames'
1378 `stgit-default-show-ignored'
1379 `stgit-default-show-patch-names'
1380 `stgit-default-show-unknown'
1381 `stgit-default-show-worktree'
1382 `stgit-default-show-committed'
1383 `stgit-default-show-svn'
1384 `stgit-default-committed-count'
1385 `stgit-find-copies-harder'
1386 `stgit-show-worktree-mode'
1388 See also \\[customize-group] for the \"stgit\" group."
1389 (kill-all-local-variables)
1390 (buffer-disable-undo)
1391 (setq mode-name "StGit"
1392 major-mode 'stgit-mode
1394 (use-local-map stgit-mode-map)
1395 (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x)))
1396 `((list-buffers-directory . ,default-directory)
1397 (parse-sexp-lookup-properties . t)
1398 (stgit-expanded-patches . (:work :index))
1399 (stgit-index-node . nil)
1400 (stgit-worktree-node . nil)
1401 (stgit-marked-patches . nil)
1402 (stgit-svn-find-rev-hash . ,(make-hash-table :test 'equal))
1403 (stgit-committed-count . ,stgit-default-committed-count)
1404 (stgit-show-committed . ,stgit-default-show-committed)
1405 (stgit-show-ignored . ,stgit-default-show-ignored)
1406 (stgit-show-patch-names . ,stgit-default-show-patch-names)
1407 (stgit-show-svn . ,stgit-default-show-svn)
1408 (stgit-show-unknown . ,stgit-default-show-unknown)
1409 (stgit-show-worktree . ,stgit-default-show-worktree)))
1410 (set-variable 'truncate-lines 't)
1411 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1412 (unless stgit-did-advise
1414 (setq stgit-did-advise t))
1415 (run-hooks 'stgit-mode-hook))
1417 (defun stgit-advise-funlist (funlist)
1418 "Add advice to the functions in FUNLIST so we can refresh the
1419 stgit buffers as the git status of files change."
1422 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1423 (stgit-update-stgit-for-buffer t)))
1427 (defun stgit-advise ()
1428 "Add advice to appropriate (non-stgit) git functions so we can
1429 refresh the stgit buffers as the git status of files change."
1431 (let ((feature (car arg))
1432 (funlist (cdr arg)))
1433 (if (featurep feature)
1434 (stgit-advise-funlist funlist)
1435 (add-to-list 'after-load-alist
1436 `(,feature (stgit-advise-funlist
1437 (quote ,funlist)))))))
1438 ;; lists of (<feature> <function> <function> ...) to be advised
1439 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1440 (git git-add-file git-checkout git-revert-file git-remove-file)
1441 (dired dired-delete-file))))
1443 (defvar stgit-pending-refresh-buffers nil
1444 "Alist of (cons `buffer' `refresh-index') of buffers that need
1445 to be refreshed. `refresh-index' is non-nil if both work tree
1446 and index need to be refreshed.")
1448 (defun stgit-run-pending-refreshs ()
1449 "Run all pending stgit buffer updates as posted by `stgit-post-refresh'."
1450 (let ((buffers stgit-pending-refresh-buffers)
1451 (stgit-inhibit-messages t))
1452 (setq stgit-pending-refresh-buffers nil)
1454 (let* ((elem (car buffers))
1456 (refresh-index (cdr elem)))
1457 (when (buffer-name buffer)
1458 (with-current-buffer buffer
1459 (stgit-refresh-worktree)
1460 (when refresh-index (stgit-refresh-index)))))
1461 (setq buffers (cdr buffers)))))
1463 (defun stgit-post-refresh (buffer refresh-index)
1464 "Update worktree status in BUFFER when Emacs becomes idle. If
1465 REFRESH-INDEX is non-nil, also update the index."
1466 (unless stgit-pending-refresh-buffers
1467 (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs))
1468 (let ((elem (assq buffer stgit-pending-refresh-buffers)))
1470 ;; if buffer is already present, set its refresh-index flag if
1475 (setq stgit-pending-refresh-buffers
1476 (cons (cons buffer refresh-index)
1477 stgit-pending-refresh-buffers)))))
1479 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1480 "When Emacs becomes idle, refresh worktree status in any
1481 `stgit-mode' buffer that shows the status of the current buffer.
1483 If REFRESH-INDEX is non-nil, also update the index."
1484 (let* ((dir (cond ((derived-mode-p 'stgit-status-mode 'dired-mode)
1487 (file-name-directory
1488 (expand-file-name buffer-file-name)))))
1489 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1491 (buffer (and gitdir (stgit-find-buffer gitdir))))
1493 (stgit-post-refresh buffer refresh-index))))
1495 (defun stgit-add-mark (patchsym)
1496 "Mark the patch PATCHSYM."
1497 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1499 (defun stgit-remove-mark (patchsym)
1500 "Unmark the patch PATCHSYM."
1501 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1503 (defun stgit-clear-marks ()
1504 "Unmark all patches."
1505 (setq stgit-marked-patches '()))
1507 (defun stgit-patch-at-point (&optional cause-error)
1508 (get-text-property (point) 'patch-data))
1510 (defun stgit-patch-name-at-point (&optional cause-error types)
1511 "Return the patch name on the current line as a symbol.
1512 If CAUSE-ERROR is not nil, signal an error if none found.
1514 TYPES controls which types of commits and patches can be returned.
1515 If it is t, only allow stgit patches; if 'allow-committed, also
1516 allow historical commits; if nil, also allow work tree and index."
1517 (let ((patch (stgit-patch-at-point)))
1519 (memq (stgit-patch->status patch)
1522 ((allow-committed) '(work index))
1523 ((t) '(work index committed))
1524 (t (error "Bad value"))))
1527 (stgit-patch->name patch))
1529 (error "No patch on this line")))))
1531 (defun stgit-patched-file-at-point ()
1532 (get-text-property (point) 'file-data))
1534 (defun stgit-patches-marked-or-at-point (&optional cause-error types)
1535 "Return the symbols of the marked patches, or the patch on the current line.
1536 If CAUSE-ERRROR is not nil, signal an error if none found.
1538 TYPES controls which types of commits and patches can be returned.
1539 If it is t, only allow stgit patches; if 'allow-committed, also
1540 allow historical commits; if nil, also allow work tree and index."
1541 (if stgit-marked-patches
1542 stgit-marked-patches
1543 (let ((patch (stgit-patch-name-at-point nil types)))
1544 (cond (patch (list patch))
1545 (cause-error (error "No patches marked or at this line"))
1548 (defun stgit-goto-patch (patchsym &optional file)
1549 "Move point to the line containing patch PATCHSYM and return non-nil.
1550 If that patch cannot be found, do nothing and return nil.
1552 If the patch was found and FILE is not nil, instead move to that
1553 file's line. If FILE cannot be found, stay on the line of
1555 (let ((node (ewoc-nth stgit-ewoc 0)))
1556 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1558 (setq node (ewoc-next stgit-ewoc node)))
1559 (when (and node file)
1560 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1561 (file-node (ewoc-nth file-ewoc 0)))
1562 (while (and file-node
1563 (not (equal (stgit-file->file (ewoc-data file-node))
1565 (setq file-node (ewoc-next file-ewoc file-node)))
1567 (ewoc-goto-node file-ewoc file-node)
1568 (move-to-column (stgit-goal-column))
1571 (ewoc-goto-node stgit-ewoc node)
1572 (move-to-column goal-column))))
1574 (defun stgit-init ()
1578 (stgit-capture-output nil
1582 (defun stgit-toggle-mark ()
1583 "Toggle mark on the patch under point."
1586 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1590 (defun stgit-mark ()
1591 "Mark the patch under point."
1594 (let* ((node (ewoc-locate stgit-ewoc))
1595 (patch (ewoc-data node)))
1596 (case (stgit-patch->status patch)
1597 (work (error "Cannot mark the work tree"))
1598 (index (error "Cannot mark the index"))
1599 (committed (error "Cannot mark a committed patch")))
1600 (stgit-add-mark (stgit-patch->name patch))
1601 (let ((column (current-column)))
1602 (ewoc-invalidate stgit-ewoc node)
1603 (move-to-column column))))
1605 (defun stgit-mark-down ()
1606 "Mark the patch under point and move to the next patch."
1611 (defun stgit-unmark ()
1612 "Remove mark from the patch on the current line."
1615 (let* ((node (ewoc-locate stgit-ewoc))
1616 (patch (ewoc-data node)))
1617 (stgit-remove-mark (stgit-patch->name patch))
1618 (let ((column (current-column)))
1619 (ewoc-invalidate stgit-ewoc node)
1620 (move-to-column column))))
1622 (defun stgit-unmark-up ()
1623 "Remove mark from the patch on the previous line."
1626 (stgit-previous-patch)
1629 (defun stgit-unmark-down ()
1630 "Remove mark from the patch on the current line."
1636 (defun stgit-rename (name)
1637 "Rename the patch under point to NAME."
1639 (read-string "Patch name: "
1640 (symbol-name (stgit-patch-name-at-point t t)))))
1642 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1643 (stgit-capture-output nil
1644 (stgit-run "rename" "--" old-patchsym name))
1645 (let ((name-sym (intern name)))
1646 (when (memq old-patchsym stgit-expanded-patches)
1647 (setq stgit-expanded-patches
1648 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1649 (when (memq old-patchsym stgit-marked-patches)
1650 (setq stgit-marked-patches
1651 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1653 (stgit-goto-patch name-sym))))
1655 (defun stgit-reload-or-repair (repair)
1656 "Update the contents of the StGit buffer (`stgit-reload').
1658 With a prefix argument, repair the StGit metadata if the branch
1659 was modified with git commands (`stgit-repair')."
1666 (defun stgit-repair ()
1670 (stgit-capture-output nil
1671 (stgit-run "repair"))
1674 (defun stgit-available-branches (&optional all)
1675 "Returns a list of the names of the available stg branches as strings.
1677 If ALL is not nil, also return non-stgit branches."
1678 (let ((output (with-output-to-string
1679 (stgit-run "branch" "--list")))
1680 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1684 (while (string-match pattern output start)
1685 (setq result (cons (match-string 1 output) result))
1686 (setq start (match-end 0)))
1689 (defun stgit-branch (branch)
1690 "Switch to or create branch BRANCH."
1691 (interactive (list (completing-read "Switch to branch: "
1692 (stgit-available-branches))))
1694 (when (cond ((equal branch (stgit-current-branch))
1695 (error "Branch is already current"))
1696 ((member branch (stgit-available-branches t))
1697 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1699 ((not (string-match stgit-allowed-branch-name-re branch))
1700 (error "Invalid branch name"))
1701 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1702 (let ((branch-point (completing-read
1703 "Branch from (default current branch): "
1704 (stgit-available-branches))))
1705 (stgit-capture-output nil
1707 `("branch" "--create" "--"
1709 ,@(unless (zerop (length branch-point))
1710 (list branch-point)))))
1714 (defun stgit-available-refs (&optional omit-stgit)
1715 "Returns a list of the available git refs.
1716 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1717 (let* ((output (with-output-to-string
1718 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1719 "refs/tags" "refs/heads"
1721 (result (split-string output "\n" t)))
1723 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1724 (substring s (match-end 0))
1727 (delete-if (lambda (s)
1728 (string-match "^refs/heads/.*\\.stgit$" s))
1732 (defun stgit-parent-branch ()
1733 "Return the parent branch of the current stg branch as per
1734 git-config setting branch.<branch>.stgit.parentbranch."
1735 (let ((output (with-output-to-string
1736 (stgit-run-git-silent "config"
1737 (format "branch.%s.stgit.parentbranch"
1738 (stgit-current-branch))))))
1739 (when (string-match ".*" output)
1740 (match-string 0 output))))
1742 (defun stgit-rebase (new-base)
1743 "Rebase the current branch to NEW-BASE.
1745 Interactively, first ask which branch to rebase to. Defaults to
1746 what git-config branch.<branch>.stgit.parentbranch is set to."
1747 (interactive (list (completing-read "Rebase to: "
1748 (stgit-available-refs t)
1750 (stgit-parent-branch))))
1752 (stgit-capture-output nil (stgit-run "rebase" "--" new-base))
1755 (defun stgit-commit (count)
1756 "Run stg commit on COUNT commits.
1757 Interactively, the prefix argument is used as COUNT.
1758 A negative COUNT will uncommit instead."
1762 (stgit-uncommit (- count))
1763 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1766 (defun stgit-uncommit (count)
1767 "Run stg uncommit on COUNT commits.
1768 Interactively, the prefix argument is used as COUNT.
1769 A negative COUNT will commit instead."
1773 (stgit-commit (- count))
1774 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1777 (defun stgit-neighbour-file ()
1778 "Return the file name of the next file after point, or the
1779 previous file if point is at the last file within a patch."
1780 (let ((old-point (point))
1782 (and (zerop (forward-line 1))
1783 (let ((f (stgit-patched-file-at-point)))
1784 (and f (setq neighbour-file (stgit-file->file f)))))
1785 (goto-char old-point)
1786 (unless neighbour-file
1787 (and (zerop (forward-line -1))
1788 (let ((f (stgit-patched-file-at-point)))
1789 (and f (setq neighbour-file (stgit-file->file f)))))
1790 (goto-char old-point))
1793 (defun stgit-revert-file ()
1794 "Revert the file at point, which must be in the index or the
1798 (let* ((patched-file (or (stgit-patched-file-at-point)
1799 (error "No file on the current line")))
1800 (patch-name (stgit-patch-name-at-point))
1801 (file-status (stgit-file->status patched-file))
1802 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1803 (stgit-file->cr-to patched-file))
1804 ((eq file-status 'add)
1805 (stgit-file->file patched-file))))
1806 (co-file (cond ((eq file-status 'rename)
1807 (stgit-file->cr-from patched-file))
1808 ((not (memq file-status '(copy add)))
1809 (stgit-file->file patched-file))))
1810 (next-file (stgit-neighbour-file)))
1812 (unless (memq patch-name '(:work :index))
1813 (error "No index or working tree file on this line"))
1815 (when (eq file-status 'ignore)
1816 (error "Cannot revert ignored files"))
1818 (when (eq file-status 'unknown)
1819 (error "Cannot revert unknown files"))
1821 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1822 (when (yes-or-no-p (format "Revert %d file%s? "
1824 (if (= nfiles 1) "" "s")))
1825 (stgit-capture-output nil
1827 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1829 (stgit-run-git "checkout" "HEAD" co-file)))
1831 (stgit-goto-patch patch-name next-file)))))
1833 (defun stgit-revert ()
1834 "Revert the change at point, which must be the index, the work
1835 tree, or a single change in either."
1838 (let ((patched-file (stgit-patched-file-at-point)))
1841 (let* ((patch-name (or (stgit-patch-name-at-point)
1842 (error "No patch or file at point")))
1843 (patch-desc (case patch-name
1846 (t (error (substitute-command-keys
1847 "Use \\[stgit-delete] to delete a patch"))))))
1848 (when (if (eq patch-name :work)
1849 (stgit-work-tree-empty-p)
1850 (stgit-index-empty-p))
1851 (error (format "There are no changes in the %s to revert"
1853 (and (eq patch-name :index)
1854 (not (stgit-work-tree-empty-p))
1855 (error "Cannot revert index as work tree contains unstaged changes"))
1857 (when (yes-or-no-p (format "Revert all changes in the %s? "
1859 (if (eq patch-name :index)
1860 (stgit-run-git-silent "reset" "--hard" "-q")
1861 (stgit-run-git-silent "checkout" "--" "."))
1862 (stgit-refresh-index)
1863 (stgit-refresh-worktree)
1864 (stgit-goto-patch patch-name))))))
1866 (defun stgit-resolve-file ()
1867 "Resolve conflict in the file at point."
1870 (let* ((patched-file (stgit-patched-file-at-point))
1871 (patch (stgit-patch-at-point))
1872 (patch-name (and patch (stgit-patch->name patch)))
1873 (status (and patched-file (stgit-file->status patched-file))))
1875 (unless (memq patch-name '(:work :index))
1876 (error "No index or working tree file on this line"))
1878 (unless (eq status 'unmerged)
1879 (error "No conflict to resolve at the current line"))
1881 (stgit-capture-output nil
1882 (stgit-move-change-to-index (stgit-file->file patched-file)))
1886 (defun stgit-push-or-pop-patches (do-push npatches)
1887 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1888 NPATCHES patches, or all patches if NPATCHES is t."
1890 (stgit-capture-output nil
1892 (if do-push "push" "pop")
1895 (list "-n" npatches))))
1897 (stgit-refresh-git-status))
1899 (defun stgit-push-next (npatches)
1900 "Push the first unapplied patch.
1901 With numeric prefix argument, push that many patches."
1903 (stgit-push-or-pop-patches t npatches))
1905 (defun stgit-pop-next (npatches)
1906 "Pop the topmost applied patch.
1907 With numeric prefix argument, pop that many patches.
1909 If NPATCHES is t, pop all patches."
1911 (stgit-push-or-pop-patches nil npatches))
1913 (defun stgit-applied-patches (&optional only-patches)
1914 "Return a list of the applied patches.
1916 If ONLY-PATCHES is not nil, exclude index and work tree."
1917 (let ((states (if only-patches
1919 '(applied top index work)))
1921 (ewoc-map (lambda (patch)
1922 (when (memq (stgit-patch->status patch) states)
1923 (setq result (cons patch result)))
1928 (defun stgit-applied-patchsyms (&optional only-patches)
1929 "Return a list of the symbols of the applied patches.
1931 If ONLY-PATCHES is not nil, exclude index and work tree."
1932 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1934 (defun stgit-push-or-pop ()
1935 "Push or pop the marked patches."
1938 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1939 (applied-syms (stgit-applied-patchsyms t))
1940 (unapplied (set-difference patchsyms applied-syms)))
1941 (stgit-capture-output nil
1943 (if unapplied "push" "pop")
1945 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1948 (defun stgit-goto-target ()
1949 "Return the goto target at point: a patchsym, :top,
1951 (let ((patch (stgit-patch-at-point)))
1953 (case (stgit-patch->status patch)
1955 ((committed) :bottom)
1956 (t (stgit-patch->name patch))))
1957 ((not (next-single-property-change (point) 'patch-data))
1959 ((not (previous-single-property-change (point) 'patch-data))
1962 (defun stgit-goto ()
1963 "Go to the patch on the current line.
1965 Push or pop patches to make this patch topmost. Push or pop all
1966 patches if used on a line after or before all patches."
1969 (let ((patchsym (stgit-goto-target)))
1971 (error "No patch to go to on this line"))
1973 (:top (stgit-push-or-pop-patches t t))
1974 (:bottom (stgit-push-or-pop-patches nil t))
1975 (t (stgit-capture-output nil
1976 (stgit-run "goto" "--" patchsym))
1979 (defun stgit-id (patchsym)
1980 "Return the git commit id for PATCHSYM.
1981 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1982 (if (keywordp patchsym)
1984 (let ((result (with-output-to-string
1985 (stgit-run-silent "id" "--" patchsym))))
1986 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1987 (error "Cannot find commit id for %s" patchsym))
1988 (match-string 1 result))))
1990 (defun stgit-whitespace-diff-arg (arg)
1992 (cond ((> arg 4) "--ignore-all-space")
1993 ((> arg 1) "--ignore-space-change"))))
1995 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1996 "Show the patch on the current line.
1998 UNMERGED-STAGE is the argument to `git-diff' that that selects
1999 which stage to diff against in the case of unmerged files."
2000 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
2001 (patch-name (stgit-patch-name-at-point t)))
2002 (stgit-capture-output "*StGit patch*"
2003 (case (get-text-property (point) 'entry-type)
2005 (let* ((patched-file (stgit-patched-file-at-point))
2006 (patch-id (let ((id (stgit-id patch-name)))
2007 (if (and (eq id :index)
2008 (eq (stgit-file->status patched-file)
2012 (args (append (and space-arg (list space-arg))
2013 (and (stgit-file->cr-from patched-file)
2014 (list (stgit-find-copies-harder-diff-arg)))
2015 (cond ((eq patch-id :index)
2017 ((eq patch-id :work)
2018 (list unmerged-stage))
2020 (list (concat patch-id "^") patch-id)))
2021 (and (eq (stgit-file->status patched-file) 'copy)
2022 '("--diff-filter=C"))
2024 (if (stgit-file->copy-or-rename patched-file)
2025 (list (stgit-file->cr-from patched-file)
2026 (stgit-file->cr-to patched-file))
2027 (list (stgit-file->file patched-file))))))
2028 (apply 'stgit-run-git "diff" args)))
2030 (let* ((patch-id (stgit-id patch-name)))
2031 (if (or (eq patch-id :index) (eq patch-id :work))
2032 (apply 'stgit-run-git "diff"
2033 (stgit-find-copies-harder-diff-arg)
2034 (append (and space-arg (list space-arg))
2035 (if (eq patch-id :index)
2037 (list unmerged-stage))))
2038 (let ((args (append '("show" "-O" "--patch-with-stat")
2039 `("-O" ,(stgit-find-copies-harder-diff-arg))
2040 (and space-arg (list "-O" space-arg))
2042 (list (stgit-patch-name-at-point)))))
2043 (apply 'stgit-run args)))))
2045 (error "No patch or file at point")))
2046 (with-current-buffer standard-output
2047 (goto-char (point-min))
2050 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
2051 `(defun ,name (&optional ignore-whitespace)
2052 ,(format "Show the patch on the current line.
2054 %sWith a prefix argument, ignore whitespace. With a prefix argument
2055 greater than four (e.g., \\[universal-argument] \
2056 \\[universal-argument] \\[%s]), ignore all whitespace."
2058 (format "For unmerged files, %s.\n\n" unmerged-action)
2063 (stgit-show-patch ,diff-arg ignore-whitespace)))
2065 (stgit-define-diff stgit-diff
2067 (stgit-define-diff stgit-diff-ours
2069 "diff against our branch")
2070 (stgit-define-diff stgit-diff-theirs
2072 "diff against their branch")
2073 (stgit-define-diff stgit-diff-base
2075 "diff against the merge base")
2076 (stgit-define-diff stgit-diff-combined
2078 "show a combined diff")
2080 (defun stgit-diff-range (&optional ignore-whitespace)
2081 "Show diff for the range of patches between point and the marked patch.
2083 With a prefix argument, ignore whitespace. With a prefix argument
2084 greater than four (e.g., \\[universal-argument] \
2085 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
2088 (unless (= (length stgit-marked-patches) 1)
2089 (error "Need exactly one patch marked"))
2090 (let* ((patches (stgit-sort-patches
2091 (cons (stgit-patch-name-at-point t 'allow-committed)
2092 stgit-marked-patches)
2094 (first-patch (car patches))
2095 (second-patch (if (cdr patches) (cadr patches) first-patch))
2096 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
2097 (applied (stgit-applied-patchsyms t)))
2098 (unless (and (memq first-patch applied) (memq second-patch applied))
2099 (error "Can only show diff range for applied patches"))
2100 (stgit-capture-output (format "*StGit diff %s..%s*"
2101 first-patch second-patch)
2102 (apply 'stgit-run-git
2103 "diff" "--patch-with-stat"
2104 (stgit-find-copies-harder-diff-arg)
2105 (append (and whitespace-arg (list whitespace-arg))
2106 (list (format "%s^" (stgit-id first-patch))
2107 (stgit-id second-patch))))
2108 (with-current-buffer standard-output
2109 (goto-char (point-min))
2112 (defun stgit-move-change-to-index (file &optional force)
2113 "Copies the work tree state of FILE to index, using git add or git rm.
2115 If FORCE is not nil, use --force."
2116 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
2117 '("add") '("rm" "-q"))))
2118 (stgit-capture-output "*git output*"
2119 (apply 'stgit-run-git (append op (and force '("--force"))
2120 '("--") (list file))))))
2122 (defun stgit-remove-change-from-index (file)
2123 "Unstages the change in FILE from the index"
2124 (stgit-capture-output "*git output*"
2125 (stgit-run-git "reset" "-q" "--" file)))
2127 (defun stgit-git-index-unmerged-p ()
2129 (with-output-to-string
2130 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
2132 "--quiet" "HEAD")))))
2135 (defun stgit-file-toggle-index ()
2136 "Move modified file in or out of the index.
2138 Leaves the point where it is, but moves the mark to where the
2139 file ended up. You can then jump to the file with \
2140 \\[exchange-point-and-mark]."
2143 (let* ((patched-file (or (stgit-patched-file-at-point)
2144 (error "No file on the current line")))
2145 (patched-status (stgit-file->status patched-file)))
2146 (when (eq patched-status 'unmerged)
2147 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
2148 (let* ((patch (stgit-patch-at-point))
2149 (patch-name (stgit-patch->name patch))
2150 (mark-file (if (eq patched-status 'rename)
2151 (stgit-file->cr-to patched-file)
2152 (stgit-file->file patched-file)))
2153 (point-file (if (eq patched-status 'rename)
2154 (stgit-file->cr-from patched-file)
2155 (stgit-neighbour-file))))
2157 (cond ((eq patch-name :work)
2158 (stgit-move-change-to-index (stgit-file->file patched-file)
2159 (eq patched-status 'ignore)))
2160 ((eq patch-name :index)
2161 (stgit-remove-change-from-index (stgit-file->file patched-file)))
2163 (error "Can only move files between working tree and index")))
2164 (stgit-refresh-worktree)
2165 (stgit-refresh-index)
2166 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
2168 (stgit-goto-patch patch-name point-file))))
2170 (defun stgit-toggle-index ()
2171 "Move change in or out of the index.
2173 Works on index and work tree, as well as files in either.
2175 Leaves the point where it is, but moves the mark to where the
2176 file ended up. You can then jump to the file with \
2177 \\[exchange-point-and-mark]."
2180 (if (stgit-patched-file-at-point)
2181 (stgit-file-toggle-index)
2182 (let ((patch-name (stgit-patch-name-at-point)))
2183 (unless (memq patch-name '(:index :work))
2184 (error "Can only move changes between working tree and index"))
2185 (when (stgit-git-index-unmerged-p)
2186 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
2187 (if (if (eq patch-name :index)
2188 (stgit-index-empty-p)
2189 (stgit-work-tree-empty-p))
2190 (message "No changes to be moved")
2191 (stgit-capture-output nil
2192 (if (eq patch-name :work)
2193 (stgit-run-git "add" "--update")
2194 (stgit-run-git "reset" "--mixed" "-q")))
2195 (stgit-refresh-worktree)
2196 (stgit-refresh-index))
2197 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
2199 (defun stgit-edit ()
2200 "Edit the patch on the current line."
2203 (let ((patchsym (stgit-patch-name-at-point t t))
2204 (edit-buf (get-buffer-create "*StGit edit*"))
2205 (dir default-directory))
2206 (log-edit 'stgit-confirm-edit t nil edit-buf)
2207 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
2208 (setq default-directory dir)
2209 (let ((standard-output edit-buf))
2211 (stgit-run-silent "edit" "--save-template=-" "--" patchsym)))))
2213 (defun stgit-confirm-edit ()
2215 (let ((file (make-temp-file "stgit-edit-")))
2216 (write-region (point-min) (point-max) file)
2217 (stgit-capture-output nil
2218 (stgit-run "edit" "-f" file "--" stgit-edit-patchsym))
2219 (with-current-buffer log-edit-parent-buffer
2222 (defun stgit-new (add-sign &optional refresh)
2223 "Create a new patch.
2224 With a prefix argument, include a \"Signed-off-by:\" line at the
2228 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2229 (dir default-directory))
2230 (log-edit 'stgit-confirm-new t nil edit-buf)
2231 (setq default-directory dir)
2232 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2235 (let ((standard-output (current-buffer)))
2236 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2238 (defun stgit-confirm-new ()
2240 (let ((file (make-temp-file "stgit-edit-"))
2241 (refresh stgit-refresh-after-new))
2242 (write-region (point-min) (point-max) file)
2243 (stgit-capture-output nil
2244 (stgit-run "new" "-f" file))
2245 (with-current-buffer log-edit-parent-buffer
2250 (defun stgit-new-and-refresh (add-sign)
2251 "Create a new patch and refresh it with the current changes.
2253 With a prefix argument, include a \"Signed-off-by:\" line at the
2256 This works just like running `stgit-new' followed by `stgit-refresh'."
2259 (stgit-new add-sign t))
2261 (defun stgit-create-patch-name (description)
2262 "Create a patch name from a long description"
2264 (while (> (length description) 0)
2265 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2266 (setq patch (downcase (concat patch
2267 (match-string 0 description))))
2268 (setq description (substring description (match-end 0))))
2269 ((string-match "\\` +" description)
2270 (setq patch (concat patch "-"))
2271 (setq description (substring description (match-end 0))))
2272 ((string-match "\\`[^a-zA-Z_-]+" description)
2273 (setq description (substring description (match-end 0))))))
2274 (cond ((= (length patch) 0)
2276 ((> (length patch) 20)
2277 (substring patch 0 20))
2280 (defun stgit-delete (patchsyms &optional spill-p)
2281 "Delete the patches in PATCHSYMS.
2282 Interactively, delete the marked patches, or the patch at point.
2284 With a prefix argument, or SPILL-P, spill the patch contents to
2285 the work tree and index."
2286 (interactive (list (stgit-patches-marked-or-at-point t t)
2287 current-prefix-arg))
2290 (error "No patches to delete"))
2291 (when (memq :index patchsyms)
2292 (error "Cannot delete the index"))
2293 (when (memq :work patchsyms)
2294 (error "Cannot delete the work tree"))
2296 (let ((npatches (length patchsyms)))
2297 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2299 (if (= 1 npatches) "" "es")
2301 " (spilling contents to index)"
2303 (let ((args (append (when spill-p '("--spill"))
2306 (stgit-capture-output nil
2307 (apply 'stgit-run "delete" args))
2310 (defun stgit-move-patches-target ()
2311 "Return the patchsym indicating a target patch for
2312 `stgit-move-patches'.
2314 This is either the first unmarked patch at or after point, or one
2315 of :top and :bottom if the point is after or before the applied
2321 (let ((patchsym (stgit-patch-name-at-point)))
2322 (cond ((memq patchsym '(:work :index)) (setq result :top))
2323 (patchsym (if (memq patchsym stgit-marked-patches)
2325 (setq result patchsym)))
2326 ((re-search-backward "^>" nil t) (setq result :top))
2327 (t (setq result :bottom)))))
2330 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2331 "Returns the list of patches in PATCHSYMS sorted according to
2332 their position in the patch series, bottommost first.
2334 PATCHSYMS must not contain duplicate entries, unless
2335 ALLOW-DUPLICATES is not nil."
2336 (let (sorted-patchsyms)
2337 (ewoc-map #'(lambda (patch)
2338 (let ((name (stgit-patch->name patch)))
2339 (when (memq name patchsyms)
2340 (setq sorted-patchsyms (cons name sorted-patchsyms))))
2343 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2345 (unless allow-duplicates
2346 (unless (= (length patchsyms) (length sorted-patchsyms))
2347 (error "Internal error")))
2351 (defun stgit-move-patches (patchsyms target-patch)
2352 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2353 If TARGET-PATCH is :bottom or :top, move the patches to the
2354 bottom or top of the stack, respectively.
2356 Interactively, move the marked patches to where the point is."
2357 (interactive (list stgit-marked-patches
2358 (stgit-move-patches-target)))
2361 (error "Need at least one patch to move"))
2363 (unless target-patch
2364 (error "Point not at a patch"))
2366 ;; need to have patchsyms sorted by position in the stack
2367 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2368 (stgit-capture-output nil
2369 (if (eq target-patch :top)
2370 (apply 'stgit-run "float" "--" sorted-patchsyms)
2373 (append (unless (eq target-patch :bottom)
2374 (list "--to" target-patch))
2376 sorted-patchsyms)))))
2379 (defun stgit-squash (patchsyms)
2380 "Squash the patches in PATCHSYMS.
2381 Interactively, squash the marked patches.
2383 Unless there are any conflicts, the patches will be merged into
2384 one patch, which will occupy the same spot in the series as the
2385 deepest patch had before the squash."
2386 (interactive (list stgit-marked-patches))
2388 (when (< (length patchsyms) 2)
2389 (error "Need at least two patches to squash"))
2390 (let ((stgit-buffer (current-buffer))
2391 (edit-buf (get-buffer-create "*StGit edit*"))
2392 (dir default-directory)
2393 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2394 (log-edit 'stgit-confirm-squash t nil edit-buf)
2395 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2396 (setq default-directory dir)
2397 (let ((result (let ((standard-output edit-buf))
2399 (apply 'stgit-run-silent "squash"
2400 "--save-template=-" "--" sorted-patchsyms)))))
2402 ;; stg squash may have reordered the patches or caused conflicts
2403 (with-current-buffer stgit-buffer
2406 (unless (eq 0 result)
2408 (rename-buffer "*StGit error*")
2409 (resize-temp-buffer-window)
2410 (switch-to-buffer-other-window stgit-buffer)
2411 (error "stg squash failed")))))
2413 (defun stgit-confirm-squash ()
2415 (let ((file (make-temp-file "stgit-edit-")))
2416 (write-region (point-min) (point-max) file)
2417 (stgit-capture-output nil
2418 (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms))
2419 (with-current-buffer log-edit-parent-buffer
2421 ;; Go to first marked patch and stay there
2422 (goto-char (point-min))
2423 (re-search-forward (concat "^[>+-]\\*") nil t)
2424 (move-to-column goal-column)
2425 (let ((pos (point)))
2429 (defun stgit-help ()
2430 "Display help for the StGit mode."
2432 (describe-function 'stgit-mode))
2434 (defun stgit-execute-process-sentinel (process sentinel)
2435 (let (old-sentinel stgit-buf)
2436 (with-current-buffer (process-buffer process)
2437 (setq old-sentinel old-process-sentinel
2438 stgit-buf stgit-buffer))
2439 (and (memq (process-status process) '(exit signal))
2440 (buffer-live-p stgit-buf)
2441 (with-current-buffer stgit-buf
2443 (funcall old-sentinel process sentinel)))
2445 (defun stgit-execute-process-filter (process output)
2446 (with-current-buffer (process-buffer process)
2447 (let* ((old-point (point))
2448 (pmark (process-mark process))
2449 (insert-at (marker-position pmark))
2450 (at-pmark (= insert-at old-point)))
2451 (goto-char insert-at)
2452 (insert-before-markers output)
2453 (comint-carriage-motion insert-at (point))
2454 (set-marker pmark (point))
2456 (goto-char old-point)))))
2458 (defun stgit-execute (&optional git-mode)
2459 "Prompt for an stg command to execute in a shell.
2461 The names of any marked patches or the patch at point are
2462 inserted in the command to be executed.
2464 With a prefix argument, or if GIT-MODE is non-nil, insert SHA1
2465 sums of the marked patches instead, and prompt for a git command.
2467 If the command ends in an ampersand, run it asynchronously.
2469 When the command has finished, reload the stgit buffer."
2472 (let* ((patches (stgit-sort-patches
2473 (stgit-patches-marked-or-at-point nil 'allow-committed)))
2474 (patch-names (mapcar 'symbol-name patches))
2475 (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names))
2476 (defaultcmd (if patches
2477 (concat (if git-mode "git" "stg") " "
2479 (mapconcat (if git-mode 'stgit-id 'identity)
2482 (cmd (read-from-minibuffer "Shell command: " (cons defaultcmd 5)
2483 nil nil 'shell-command-history))
2484 (async (string-match "&[ \t]*\\'" cmd))
2485 (buffer (get-buffer-create
2487 "*Async Shell Command*"
2488 "*Shell Command Output*"))))
2489 ;; cannot use minibuffer as stgit-reload would overwrite it; if we
2490 ;; show the buffer, shell-command will not use the minibuffer
2491 (display-buffer buffer)
2494 (let ((old-buffer (current-buffer)))
2495 (with-current-buffer buffer
2496 (let ((process (get-buffer-process buffer)))
2497 (set (make-local-variable 'old-process-sentinel)
2498 (process-sentinel process))
2499 (set (make-local-variable 'stgit-buffer)
2501 (set-process-filter process 'stgit-execute-process-filter)
2502 (set-process-sentinel process 'stgit-execute-process-sentinel))))
2503 (with-current-buffer buffer
2504 (comint-carriage-motion (point-min) (point-max)))
2505 (shrink-window-if-larger-than-buffer (get-buffer-window buffer))
2508 (defun stgit-undo-or-redo (redo hard)
2509 "Run stg undo or, if REDO is non-nil, stg redo.
2511 If HARD is non-nil, use the --hard flag."
2513 (let ((cmd (if redo "redo" "undo")))
2514 (stgit-capture-output nil
2516 (when (or (and (stgit-index-empty-p)
2517 (stgit-work-tree-empty-p))
2518 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2520 (stgit-run cmd "--hard"))
2524 (defun stgit-undo (&optional arg)
2526 With prefix argument, run it with the --hard flag.
2528 See also `stgit-redo'."
2530 (stgit-undo-or-redo nil arg))
2532 (defun stgit-redo (&optional arg)
2534 With prefix argument, run it with the --hard flag.
2536 See also `stgit-undo'."
2538 (stgit-undo-or-redo t arg))
2540 (defun stgit-refresh (&optional arg)
2542 If the index contains any changes, only refresh from index.
2544 With prefix argument, refresh the marked patch or the patch under point."
2547 (let ((patchargs (if arg
2548 (let ((patches (stgit-patches-marked-or-at-point t t)))
2549 (when (> (length patches) 1)
2550 (error "Too many patches marked"))
2551 (cons "-p" patches))
2553 (unless (stgit-index-empty-p)
2554 (setq patchargs (cons "--index" patchargs)))
2555 (stgit-capture-output nil
2556 (apply 'stgit-run "refresh" patchargs))
2557 (stgit-refresh-git-status))
2560 (defvar stgit-show-worktree nil
2561 "If nil, inhibit showing work tree and index in the stgit buffer.
2563 See also `stgit-show-worktree-mode'.")
2565 (defvar stgit-show-ignored nil
2566 "If nil, inhibit showing files ignored by git.")
2568 (defvar stgit-show-unknown nil
2569 "If nil, inhibit showing files not registered with git.")
2571 (defvar stgit-show-patch-names t
2572 "If nil, inhibit showing patch names.")
2574 (defvar stgit-show-committed nil
2575 "If nil, inhibit showing recent commits.")
2577 (defvar stgit-show-svn nil
2578 "If nil, inhibit showing git svn information.")
2580 (defvar stgit-committed-count nil
2581 "The number of recent commits to show.")
2583 (defmacro stgit-define-toggle-view (sym desc help)
2584 (declare (indent 1))
2585 (let* ((name (symbol-name sym))
2586 (fun (intern (concat "stgit-toggle-" name)))
2587 (flag (intern (concat "stgit-show-" name))))
2588 ;; make help-follow find the correct function
2589 `(put (quote ,fun) 'definition-name 'stgit-define-toggle-view)
2590 `(defun ,fun (&optional arg)
2595 (> (prefix-numeric-value arg) 0)
2597 (stgit-reload (format "%s %s" (if ,flag "Showing" "Hiding") ,desc)))))
2599 (stgit-define-toggle-view worktree
2600 "work tree and index"
2601 "Toggle the visibility of the work tree.
2602 With ARG, show the work tree if ARG is positive.
2604 Its initial setting is controlled by `stgit-default-show-worktree'.
2606 `stgit-show-worktree-mode' controls where on screen the index and
2607 work tree will show up.")
2609 (stgit-define-toggle-view ignored
2611 "Toggle the visibility of files ignored by git in the work
2612 tree. With ARG, show these files if ARG is positive.
2614 Its initial setting is controlled by `stgit-default-show-ignored'.
2616 Use \\[stgit-toggle-worktree] to show the work tree.")
2618 (stgit-define-toggle-view unknown
2620 "Toggle the visibility of files not registered with git in the
2621 work tree. With ARG, show these files if ARG is positive.
2623 Its initial setting is controlled by `stgit-default-show-unknown'.
2625 Use \\[stgit-toggle-worktree] to show the work tree.")
2627 (stgit-define-toggle-view patch-names
2629 "Toggle the visibility of patch names. With ARG, show patch names
2632 The initial setting is controlled by `stgit-default-show-patch-names'.")
2634 (stgit-define-toggle-view svn
2635 "subversion revisions"
2636 "Toggle showing subversion information from git svn. With ARG,
2637 show svn information if ARG is positive.
2639 The initial setting is controlled by `stgit-default-show-svn'.")
2641 (defun stgit-toggle-committed (&optional arg)
2642 "Toggle the visibility of historical git commits.
2643 With ARG, set the number of commits to show to ARG, and disable
2644 them if ARG is zero.
2646 The initial setting is controlled by `stgit-default-show-committed'."
2650 (setq stgit-show-committed (not stgit-show-committed))
2651 (let ((n (prefix-numeric-value arg)))
2652 (setq stgit-show-committed (> n 0))
2653 (setq stgit-committed-count n)))
2654 (stgit-reload (format "%s historical commits"
2655 (if (and stgit-show-committed
2656 (> stgit-committed-count 0))