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-show-committed))
149 (defcustom stgit-default-committed-count 5
150 "The number of historical commits to show when `stgit-show-committed'
153 :link '(variable-link stgit-committed-count))
155 (defcustom stgit-default-show-patch-names t
156 "If non-nil, default to showing patch names in a new stgit buffer.
158 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] \
159 to toggle the this setting in an already-started StGit buffer."
162 :link '(variable-link stgit-show-patch-names))
164 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
165 "The format string used to format file lines.
166 The format string is passed to `format-spec' and the following
167 format characters are recognized:
169 %s - A string describing the status of the file.
171 %m - Mode change information
175 %c - A description of file changes."
178 :set 'stgit-set-default)
180 (defface stgit-branch-name-face
182 "The face used for the StGit branch name"
185 (defface stgit-top-patch-face
186 '((((background dark)) (:weight bold :foreground "yellow"))
187 (((background light)) (:weight bold :foreground "purple"))
189 "The face used for the top patch names"
192 (defface stgit-applied-patch-face
193 '((((background dark)) (:foreground "light yellow"))
194 (((background light)) (:foreground "purple"))
196 "The face used for applied patch names"
199 (defface stgit-unapplied-patch-face
200 '((((background dark)) (:foreground "gray80"))
201 (((background light)) (:foreground "orchid"))
203 "The face used for unapplied patch names"
206 (defface stgit-committed-patch-face
207 '((((background dark)) (:foreground "gray50"))
208 (((background light)) (:foreground "gray50"))
210 "The face used for already committed patch names"
213 (defface stgit-description-face
214 '((((background dark)) (:foreground "tan"))
215 (((background light)) (:foreground "dark red")))
216 "The face used for StGit descriptions"
219 (defface stgit-index-work-tree-title-face
220 '((((supports :slant italic)) :slant italic)
222 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
225 (defface stgit-unmerged-file-face
226 '((((class color) (background light)) (:foreground "red" :bold t))
227 (((class color) (background dark)) (:foreground "red" :bold t)))
228 "StGit mode face used for unmerged file status"
231 (defface stgit-unknown-file-face
232 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
233 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
234 "StGit mode face used for unknown file status"
237 (defface stgit-ignored-file-face
238 '((((class color) (background light)) (:foreground "grey60"))
239 (((class color) (background dark)) (:foreground "grey40")))
240 "StGit mode face used for ignored files")
242 (defface stgit-file-permission-face
243 '((((class color) (background light)) (:foreground "green" :bold t))
244 (((class color) (background dark)) (:foreground "green" :bold t)))
245 "StGit mode face used for permission changes."
248 (defface stgit-modified-file-face
249 '((((class color) (background light)) (:foreground "purple"))
250 (((class color) (background dark)) (:foreground "salmon")))
251 "StGit mode face used for modified file status"
255 "Manage StGit patches for the tree in DIR.
257 See `stgit-mode' for commands available."
258 (interactive "DDirectory: \n")
259 (switch-to-stgit-buffer (git-get-top-dir dir))
262 (defun stgit-assert-mode ()
263 "Signal an error if not in an StGit buffer."
264 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
266 (unless (fboundp 'git-get-top-dir)
267 (defun git-get-top-dir (dir)
268 "Retrieve the top-level directory of a git tree."
269 (let ((cdup (with-output-to-string
270 (with-current-buffer standard-output
272 (unless (eq 0 (call-process "git" nil t nil
273 "rev-parse" "--show-cdup"))
274 (error "Cannot find top-level git tree for %s" dir))))))
275 (expand-file-name (concat (file-name-as-directory dir)
276 (car (split-string cdup "\n")))))))
278 (defun stgit-refresh-git-status (&optional dir)
279 "If it exists, refresh the `git-status' buffer belonging to
280 directory DIR or `default-directory'"
281 (when (and (fboundp 'git-find-status-buffer)
282 (fboundp 'git-refresh-status))
283 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
284 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
285 (when git-status-buffer
286 (with-current-buffer git-status-buffer
287 (git-refresh-status))))))
289 (defun stgit-find-buffer (dir)
290 "Return the buffer displaying StGit patches for DIR, or nil if none."
291 (setq dir (file-name-as-directory dir))
292 (let ((buffers (buffer-list)))
294 (not (with-current-buffer (car buffers)
295 (and (eq major-mode 'stgit-mode)
296 (string= default-directory dir)))))
297 (setq buffers (cdr buffers)))
298 (and buffers (car buffers))))
300 (defun switch-to-stgit-buffer (dir)
301 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
302 (setq dir (file-name-as-directory dir))
303 (let ((buffer (stgit-find-buffer dir)))
304 (switch-to-buffer (or buffer
305 (create-stgit-buffer dir)))))
307 (defstruct (stgit-patch
308 (:conc-name stgit-patch->))
309 status name desc empty files-ewoc)
311 (defun stgit-patch-display-name (patch)
312 (let ((name (stgit-patch->name patch)))
316 (t (symbol-name name)))))
318 (defun stgit-insert-without-trailing-whitespace (text)
319 "Insert TEXT in buffer using `insert', without trailing whitespace.
320 A newline is appended."
321 (unless (string-match "\\(.*?\\) *$" text)
323 (insert (match-string 1 text) ?\n))
325 (defun stgit-line-format ()
326 "Return the current line format; one of
327 `stgit-patch-line-format' and `stgit-noname-patch-line-format'"
328 (if stgit-show-patch-names
329 stgit-patch-line-format
330 stgit-noname-patch-line-format))
332 (defun stgit-patch-pp (patch)
333 (let* ((status (stgit-patch->status patch))
335 (name (stgit-patch->name patch))
336 (face (cdr (assq status stgit-patch-status-face-alist)))
337 (fmt (stgit-line-format))
338 (spec (format-spec-make
344 ?m (if (memq name stgit-marked-patches)
346 ?n (propertize (stgit-patch-display-name patch)
348 'syntax-table (string-to-syntax "w"))
349 ?e (if (stgit-patch->empty patch) "(empty) " "")
350 ?d (propertize (or (stgit-patch->desc patch) "")
351 'face 'stgit-description-face)
352 ?D (propertize (let ((desc (stgit-patch->desc patch)))
353 (if (zerop (length desc))
354 (stgit-patch-display-name patch)
357 (text (format-spec fmt spec)))
359 (stgit-insert-without-trailing-whitespace text)
360 (put-text-property start (point) 'entry-type 'patch)
361 (when (memq name stgit-expanded-patches)
362 (stgit-insert-patch-files patch))
363 (put-text-property start (point) 'patch-data patch)))
365 (defun create-stgit-buffer (dir)
366 "Create a buffer for showing StGit patches.
367 Argument DIR is the repository path."
368 (let ((buf (create-file-buffer (concat dir "*stgit*")))
369 (inhibit-read-only t))
370 (with-current-buffer buf
371 (setq default-directory dir)
373 (set (make-local-variable 'stgit-ewoc)
374 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
375 (setq buffer-read-only t))
378 (def-edebug-spec stgit-capture-output
380 (defmacro stgit-capture-output (name &rest body)
381 "Capture StGit output and, if there was any output, show it in a window
383 Returns nil if there was no output."
384 (declare (debug ([&or stringp null] body))
386 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
387 (stgit-dir default-directory)
388 (inhibit-read-only t))
389 (with-current-buffer output-buf
390 (buffer-disable-undo)
392 (setq default-directory stgit-dir)
393 (setq buffer-read-only t))
394 (let ((standard-output output-buf))
396 (with-current-buffer output-buf
397 (set-buffer-modified-p nil)
398 (setq buffer-read-only t)
399 (if (< (point-min) (point-max))
400 (display-buffer output-buf t)))))
402 (defun stgit-make-run-args (args)
403 "Return a copy of ARGS with its elements converted to strings."
405 ;; don't use (format "%s" ...) to limit type errors
406 (cond ((stringp x) x)
407 ((integerp x) (number-to-string x))
408 ((symbolp x) (symbol-name x))
410 (error "Bad element in stgit-make-run-args args: %S" x))))
413 (defun stgit-run-silent (&rest args)
414 (setq args (stgit-make-run-args args))
415 (apply 'call-process "stg" nil standard-output nil args))
417 (defun stgit-run (&rest args)
418 (setq args (stgit-make-run-args args))
419 (let ((msgcmd (mapconcat #'identity args " ")))
420 (message "Running stg %s..." msgcmd)
421 (apply 'call-process "stg" nil standard-output nil args)
422 (message "Running stg %s...done" msgcmd)))
424 (defun stgit-run-git (&rest args)
425 (setq args (stgit-make-run-args args))
426 (let ((msgcmd (mapconcat #'identity args " ")))
427 (message "Running git %s..." msgcmd)
428 (apply 'call-process "git" nil standard-output nil args)
429 (message "Running git %s...done" msgcmd)))
431 (defun stgit-run-git-silent (&rest args)
432 (setq args (stgit-make-run-args args))
433 (apply 'call-process "git" nil standard-output nil args))
435 (defun stgit-index-empty-p ()
436 "Returns non-nil if the index contains no changes from HEAD."
437 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
439 (defun stgit-work-tree-empty-p ()
440 "Returns non-nil if the work tree contains no changes from index."
441 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
443 (defvar stgit-did-advise nil
444 "Set to non-nil if appropriate (non-stgit) git functions have
445 been advised to update the stgit status when necessary.")
447 (defconst stgit-allowed-branch-name-re
448 ;; Disallow control characters, space, del, and "/:@^{}~" in
449 ;; "/"-separated parts; parts may not start with a period (.)
450 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
451 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
452 "Regular expression that (new) branch names must match.")
454 (defun stgit-refresh-index ()
455 (when stgit-index-node
456 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
458 (defun stgit-refresh-worktree ()
459 (when stgit-worktree-node
460 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
462 (defun stgit-run-series-insert-index (ewoc)
463 (setq index-node (cons ewoc (ewoc-enter-last ewoc
469 worktree-node (cons ewoc (ewoc-enter-last ewoc
476 (defun stgit-svn-find-rev (sha1 hash)
477 "Return the subversion revision corresponding to SHA1 as
480 Cached data is stored in HASH, which must have been created
481 using (make-hash-table :test 'equal)."
482 (let ((result (gethash sha1 hash t)))
484 (let ((svn-rev (with-output-to-string
485 (stgit-run-git-silent "svn" "find-rev"
487 (setq result (when (string-match "\\`[0-9]+" svn-rev)
488 (string-to-number (match-string 0 svn-rev))))
489 (puthash sha1 result hash)))
492 (defun stgit-run-series (ewoc)
493 (setq stgit-index-node nil
494 stgit-worktree-node nil)
496 (when stgit-show-committed
497 (let* ((show-svn stgit-show-svn)
498 (svn-hash stgit-svn-find-rev-hash)
499 (base (stgit-id "{base}"))
500 (range (format "%s~%d..%s" base stgit-committed-count base)))
502 (let* ((standard-output (current-buffer))
503 (fmt (stgit-line-format))
504 (commit-abbrev (when (string-match "%-\\([0-9]+\\)n" fmt)
505 (string-to-number (match-string 1 fmt))))
506 (exit-status (stgit-run-git-silent "--no-pager" "log"
510 (goto-char (point-min))
511 (if (not (zerop exit-status))
512 (message "Failed to run git log")
515 "\\([0-9a-f]+\\)\\(\\.\\.\\.\\)? \\(.*\\)")
516 (error "Syntax error in output from git log"))
517 (let* ((state 'committed)
518 (name (match-string 1))
519 (desc (match-string 3))
523 (let ((svn-rev (stgit-svn-find-rev name svn-hash)))
525 (setq desc (format "(r%s) %s" svn-rev desc)))))
528 (< commit-abbrev (length name))
529 (setq name (substring name 0 commit-abbrev)))
531 (setq name (intern name))
533 (setq all-patchsyms (cons name all-patchsyms))
534 (ewoc-enter-last ewoc
540 (forward-line 1)))))))
541 (let ((inserted-index (not stgit-show-worktree))
545 (let* ((standard-output (current-buffer))
546 (exit-status (stgit-run-silent "series"
547 "--description" "--empty")))
548 (goto-char (point-min))
549 (if (not (zerop exit-status))
550 (cond ((looking-at "stg series: \\(.*\\)")
551 (setq inserted-index t)
552 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
553 (substitute-command-keys
554 "-- not initialized; run \\[stgit-init]")))
556 (error "Error running stg: %s"
560 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
561 (error "Syntax error in output from stg series"))
562 (let* ((state-str (match-string 2))
563 (state (cond ((string= state-str ">") 'top)
564 ((string= state-str "+") 'applied)
565 ((string= state-str "-") 'unapplied)))
566 (name (intern (match-string 4)))
567 (desc (match-string 5))
568 (empty (string= (match-string 1) "0")))
569 (unless inserted-index
570 (when (or (eq stgit-show-worktree-mode 'top)
571 (and (eq stgit-show-worktree-mode 'center)
572 (eq state 'unapplied)))
573 (setq inserted-index t)
574 (stgit-run-series-insert-index ewoc)))
575 (setq all-patchsyms (cons name all-patchsyms))
576 (ewoc-enter-last ewoc
583 (unless inserted-index
584 (stgit-run-series-insert-index ewoc))
585 (setq stgit-index-node index-node
586 stgit-worktree-node worktree-node
587 stgit-marked-patches (intersection stgit-marked-patches
590 (defun stgit-current-branch ()
591 "Return the name of the current branch."
592 (substring (with-output-to-string
593 (stgit-run-silent "branch"))
596 (defun stgit-reload ()
597 "Update the contents of the StGit buffer."
600 (let ((inhibit-read-only t)
601 (curline (line-number-at-pos))
602 (curpatch (stgit-patch-name-at-point))
603 (curfile (stgit-patched-file-at-point)))
604 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
605 (ewoc-set-hf stgit-ewoc
607 (propertize (stgit-current-branch)
608 'face 'stgit-branch-name-face)
610 (if stgit-show-worktree
613 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
614 shows the working tree\n")
615 'face 'stgit-description-face)))
616 (stgit-run-series stgit-ewoc)
617 (unless (and curpatch
618 (stgit-goto-patch curpatch
619 (and curfile (stgit-file->file curfile))))
620 (goto-char (point-min))
621 (forward-line (1- curline))
622 (move-to-column (stgit-goal-column))))
623 (stgit-refresh-git-status))
625 (defconst stgit-file-status-code-strings
626 (mapcar (lambda (arg)
628 (propertize (cadr arg) 'face (car (cddr arg)))))
629 '((add "Added" stgit-modified-file-face)
630 (copy "Copied" stgit-modified-file-face)
631 (delete "Deleted" stgit-modified-file-face)
632 (modify "Modified" stgit-modified-file-face)
633 (rename "Renamed" stgit-modified-file-face)
634 (mode-change "Mode change" stgit-modified-file-face)
635 (unmerged "Unmerged" stgit-unmerged-file-face)
636 (unknown "Unknown" stgit-unknown-file-face)
637 (ignore "Ignored" stgit-ignored-file-face)))
638 "Alist of code symbols to description strings")
640 (defconst stgit-patch-status-face-alist
641 '((applied . stgit-applied-patch-face)
642 (top . stgit-top-patch-face)
643 (unapplied . stgit-unapplied-patch-face)
644 (committed . stgit-committed-patch-face)
645 (index . stgit-index-work-tree-title-face)
646 (work . stgit-index-work-tree-title-face))
647 "Alist of face to use for a given patch status")
649 (defun stgit-file-status-code-as-string (file)
650 "Return stgit status code for FILE as a string"
651 (let* ((code (assq (stgit-file->status file)
652 stgit-file-status-code-strings))
653 (score (stgit-file->cr-score file)))
655 (if (and score (/= score 100))
656 (format "%s %s" (cdr code)
657 (propertize (format "%d%%" score)
658 'face 'stgit-description-face))
661 (defun stgit-file-status-code (str &optional score)
662 "Return stgit status code from git status string"
663 (let ((code (assoc str '(("A" . add)
672 (setq code (if code (cdr code) 'unknown))
673 (when (stringp score)
674 (if (> (length score) 0)
675 (setq score (string-to-number score))
677 (if score (cons code score) code)))
679 (defconst stgit-file-type-strings
682 (#o160 . "subproject"))
683 "Alist of names of file types")
685 (defun stgit-file-type-string (type)
686 "Return string describing file type TYPE (the high bits of file permission).
687 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
688 (let ((type-str (assoc type stgit-file-type-strings)))
689 (or (and type-str (cdr type-str))
690 (format "unknown type %o" type))))
692 (defun stgit-file-type-change-string (old-perm new-perm)
693 "Return string describing file type change from OLD-PERM to NEW-PERM.
694 Cf. `stgit-file-type-string'."
695 (let ((old-type (lsh old-perm -9))
696 (new-type (lsh new-perm -9)))
697 (cond ((= old-type new-type) "")
698 ((zerop new-type) "")
700 (if (= new-type #o100)
702 (format "(%s)" (stgit-file-type-string new-type))))
703 (t (format "(%s -> %s)"
704 (stgit-file-type-string old-type)
705 (stgit-file-type-string new-type))))))
707 (defun stgit-file-mode-change-string (old-perm new-perm)
708 "Return string describing file mode change from OLD-PERM to NEW-PERM.
709 Cf. `stgit-file-type-change-string'."
710 (setq old-perm (logand old-perm #o777)
711 new-perm (logand new-perm #o777))
712 (if (or (= old-perm new-perm)
716 (let* ((modified (logxor old-perm new-perm))
717 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
718 (cond ((zerop modified) "")
719 ((and (zerop not-x-modified)
720 (or (and (eq #o111 (logand old-perm #o111))
721 (propertize "-x" 'face 'stgit-file-permission-face))
722 (and (eq #o111 (logand new-perm #o111))
723 (propertize "+x" 'face
724 'stgit-file-permission-face)))))
725 (t (concat (propertize (format "%o" old-perm)
726 'face 'stgit-file-permission-face)
728 'face 'stgit-description-face)
729 (propertize (format "%o" new-perm)
730 'face 'stgit-file-permission-face)))))))
732 (defstruct (stgit-file
733 (:conc-name stgit-file->))
734 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
736 (defun stgit-describe-copy-or-rename (file)
737 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
738 from to common-head common-tail)
740 (when stgit-abbreviate-copies-and-renames
741 (setq from (split-string (stgit-file->cr-from file) "/")
742 to (split-string (stgit-file->cr-to file) "/"))
744 (while (and from to (cdr from) (cdr to)
745 (string-equal (car from) (car to)))
746 (setq common-head (cons (car from) common-head)
749 (setq common-head (nreverse common-head)
752 (while (and from to (cdr from) (cdr to)
753 (string-equal (car from) (car to)))
754 (setq common-tail (cons (car from) common-tail)
757 (setq from (nreverse from)
760 (if (or common-head common-tail)
761 (concat (if common-head
762 (mapconcat #'identity common-head "/")
764 (if common-head "/" "")
765 (propertize "{" 'face 'stgit-description-face)
766 (mapconcat #'identity from "/")
768 (mapconcat #'identity to "/")
769 (propertize "}" 'face 'stgit-description-face)
770 (if common-tail "/" "")
772 (mapconcat #'identity common-tail "/")
774 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
776 (defun stgit-file-pp (file)
777 (let ((start (point))
778 (spec (format-spec-make
779 ?s (stgit-file-status-code-as-string file)
780 ?m (stgit-file-mode-change-string
781 (stgit-file->old-perm file)
782 (stgit-file->new-perm file))
783 ?n (if (stgit-file->copy-or-rename file)
784 (stgit-describe-copy-or-rename file)
785 (stgit-file->file file))
786 ?c (propertize (stgit-file-type-change-string
787 (stgit-file->old-perm file)
788 (stgit-file->new-perm file))
789 'face 'stgit-description-face))))
790 (stgit-insert-without-trailing-whitespace
791 (format-spec stgit-file-line-format spec))
792 (add-text-properties start (point)
793 (list 'entry-type 'file
796 (defun stgit-find-copies-harder-diff-arg ()
797 "Return the flag to use with `git-diff' depending on the
798 `stgit-find-copies-harder' flag."
799 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
801 (defun stgit-insert-ls-files (args file-flag)
802 (let ((start (point)))
803 (apply 'stgit-run-git
804 (append '("ls-files" "--exclude-standard" "-z") args))
806 (while (looking-at "\\([^\0]*\\)\0")
807 (let ((name-len (- (match-end 0) (match-beginning 0))))
808 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
809 (forward-char name-len)))))
811 (defun stgit-process-files (callback)
812 (goto-char (point-min))
813 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
814 (goto-char (match-end 0)))
815 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
816 (let ((old-perm (string-to-number (match-string 1) 8))
817 (new-perm (string-to-number (match-string 2) 8)))
818 (goto-char (match-end 0))
821 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
822 (let* ((patch-status (stgit-patch->status patch))
823 (file-subexp (if (eq patch-status 'unapplied)
826 (file (match-string file-subexp)))
831 :cr-score (string-to-number (match-string 2))
832 :cr-from (match-string 3)
833 :cr-to (match-string 4)
834 :status (stgit-file-status-code
837 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
845 :status (stgit-file-status-code
847 :file (match-string 2))))))
848 (goto-char (match-end 0))
849 (funcall callback file)))))
852 (defun stgit-insert-patch-files (patch)
853 "Expand (show modification of) the patch PATCH after the line
855 (let* ((patchsym (stgit-patch->name patch))
857 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
858 (ewoc (ewoc-create #'stgit-file-pp nil nil t))
859 (show-ignored stgit-show-ignored)
860 (show-unknown stgit-show-unknown))
861 (set-marker-insertion-type end t)
862 (setf (stgit-patch->files-ewoc patch) ewoc)
864 (let ((standard-output (current-buffer)))
865 (apply 'stgit-run-git
866 (cond ((eq patchsym :work)
867 (let (standard-output)
868 (stgit-run-git "update-index" "--refresh"))
869 `("diff-files" "-0" ,@args))
870 ((eq patchsym :index)
871 `("diff-index" ,@args "--cached" "HEAD"))
873 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
875 (when (and (eq patchsym :work))
877 (stgit-insert-ls-files '("--ignored" "--others") "I"))
879 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
882 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
883 (point-min) (point-max)))
885 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
887 (unless (ewoc-nth ewoc 0)
890 (propertize "<no files>"
891 'face 'stgit-description-face)
895 (defun stgit-find-file (&optional other-window)
896 (let* ((file (or (stgit-patched-file-at-point)
897 (error "No file at point")))
898 (filename (expand-file-name (stgit-file->file file))))
899 (unless (file-exists-p filename)
900 (error "File does not exist"))
901 (funcall (if other-window 'find-file-other-window 'find-file)
903 (when (eq (stgit-file->status file) 'unmerged)
906 (defun stgit-expand (&optional patches collapse)
907 "Show the contents of marked patches, or the patch at point.
909 See also `stgit-collapse'.
911 Non-interactively, operate on PATCHES, and collapse instead of
912 expand if COLLAPSE is not nil."
913 (interactive (list (stgit-patches-marked-or-at-point t)))
915 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
916 patches stgit-expanded-patches)))
917 (setq stgit-expanded-patches
919 (set-difference stgit-expanded-patches patches-diff)
920 (append stgit-expanded-patches patches-diff)))
921 (ewoc-map #'(lambda (patch)
922 (memq (stgit-patch->name patch) patches-diff))
924 (move-to-column (stgit-goal-column)))
926 (defun stgit-collapse (&optional patches)
927 "Hide the contents of marked patches, or the patch at point.
929 See also `stgit-expand'."
930 (interactive (list (stgit-patches-marked-or-at-point t)))
932 (stgit-expand patches t))
934 (defun stgit-select-patch ()
935 (let ((patchname (stgit-patch-name-at-point)))
936 (stgit-expand (list patchname)
937 (memq patchname stgit-expanded-patches))))
939 (defun stgit-expand-directory (file)
940 (let* ((patch (stgit-patch-at-point))
941 (ewoc (stgit-patch->files-ewoc patch))
942 (node (ewoc-locate ewoc))
943 (filename (stgit-file->file file))
944 (start (make-marker))
949 (set-marker start (point))
950 (set-marker end (point))
951 (set-marker-insertion-type end t))
953 (assert (string-match "/$" filename))
954 ;; remove trailing "/"
955 (setf (stgit-file->file file) (substring filename 0 -1))
956 (ewoc-invalidate ewoc node)
959 (let ((standard-output (current-buffer)))
960 (stgit-insert-ls-files (list "--directory" "--others"
961 "--no-empty-directory" "--"
964 (stgit-process-files (lambda (f)
965 (setq node (ewoc-enter-after ewoc node f))))))
967 (move-to-column (stgit-goal-column))
969 (let ((inhibit-read-only t))
970 (put-text-property start end 'patch-data patch))))
972 (defun stgit-select-file ()
973 (let* ((file (or (stgit-patched-file-at-point)
974 (error "No file at point")))
975 (filename (stgit-file->file file)))
976 (if (string-match "/$" filename)
977 (stgit-expand-directory file)
980 (defun stgit-select ()
981 "With point on a patch, toggle showing files in the patch.
983 With point on a file, open the associated file. Opens the target
984 file for (applied) copies and renames."
987 (case (get-text-property (point) 'entry-type)
989 (stgit-select-patch))
993 (error "No patch or file on line"))))
995 (defun stgit-find-file-other-window ()
996 "Open file at point in other window"
1001 (defun stgit-find-file-merge ()
1002 "Open file at point and merge it using `smerge-ediff'."
1008 (defun stgit-quit ()
1009 "Hide the stgit buffer."
1014 (defun stgit-git-status ()
1015 "Show status using `git-status'."
1018 (unless (fboundp 'git-status)
1019 (error "The stgit-git-status command requires git-status"))
1020 (let ((dir default-directory))
1021 (save-selected-window
1025 (defun stgit-goal-column ()
1026 "Return goal column for the current line"
1027 (case (get-text-property (point) 'entry-type)
1032 (defun stgit-next-line (&optional arg)
1033 "Move cursor vertically down ARG lines"
1037 (move-to-column (stgit-goal-column)))
1039 (defun stgit-previous-line (&optional arg)
1040 "Move cursor vertically up ARG lines"
1044 (move-to-column (stgit-goal-column)))
1046 (defun stgit-next-patch (&optional arg)
1047 "Move cursor down ARG patches."
1050 (ewoc-goto-next stgit-ewoc (or arg 1))
1051 (move-to-column goal-column))
1053 (defun stgit-previous-patch (&optional arg)
1054 "Move cursor up ARG patches."
1057 (ewoc-goto-prev stgit-ewoc (or arg 1))
1058 (move-to-column goal-column))
1060 (defvar stgit-mode-hook nil
1061 "Run after `stgit-mode' is setup.")
1063 (defvar stgit-mode-map nil
1064 "Keymap for StGit major mode.")
1066 (unless stgit-mode-map
1067 (let ((diff-map (make-sparse-keymap))
1068 (toggle-map (make-sparse-keymap)))
1069 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
1070 '(("b" . stgit-diff-base)
1071 ("c" . stgit-diff-combined)
1072 ("m" . stgit-find-file-merge)
1073 ("o" . stgit-diff-ours)
1074 ("r" . stgit-diff-range)
1075 ("t" . stgit-diff-theirs)))
1076 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
1077 '(("n" . stgit-toggle-patch-names)
1078 ("t" . stgit-toggle-worktree)
1079 ("h" . stgit-toggle-committed)
1080 ("i" . stgit-toggle-ignored)
1081 ("u" . stgit-toggle-unknown)
1082 ("s" . stgit-toggle-svn)))
1083 (setq stgit-mode-map (make-keymap))
1084 (suppress-keymap stgit-mode-map)
1085 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
1086 `((" " . stgit-mark-down)
1087 ("m" . stgit-mark-down)
1088 ("\d" . stgit-unmark-up)
1089 ("u" . stgit-unmark-down)
1092 ("\C-p" . stgit-previous-line)
1093 ("\C-n" . stgit-next-line)
1094 ([up] . stgit-previous-line)
1095 ([down] . stgit-next-line)
1096 ("p" . stgit-previous-patch)
1097 ("n" . stgit-next-patch)
1098 ("\M-{" . stgit-previous-patch)
1099 ("\M-}" . stgit-next-patch)
1100 ("s" . stgit-git-status)
1101 ("g" . stgit-reload-or-repair)
1102 ("r" . stgit-refresh)
1103 ("\C-c\C-r" . stgit-rename)
1105 ("M" . stgit-move-patches)
1106 ("S" . stgit-squash)
1108 ("c" . stgit-new-and-refresh)
1109 ("\C-c\C-c" . stgit-commit)
1110 ("\C-c\C-u" . stgit-uncommit)
1111 ("U" . stgit-revert)
1112 ("R" . stgit-resolve-file)
1113 ("\r" . stgit-select)
1114 ("+" . stgit-expand)
1115 ("-" . stgit-collapse)
1116 ("o" . stgit-find-file-other-window)
1117 ("i" . stgit-toggle-index)
1118 (">" . stgit-push-next)
1119 ("<" . stgit-pop-next)
1120 ("P" . stgit-push-or-pop)
1123 ("D" . stgit-delete)
1124 ([?\C-/] . stgit-undo)
1125 ("\C-_" . stgit-undo)
1126 ([?\C-c ?\C-/] . stgit-redo)
1127 ("\C-c\C-_" . stgit-redo)
1128 ("B" . stgit-branch)
1129 ("\C-c\C-b" . stgit-rebase)
1133 ("!" . stgit-execute))))
1135 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1136 (and file (eq (stgit-file->status file)
1138 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1139 (easy-menu-define stgit-menu stgit-mode-map
1142 ["Reload" stgit-reload-or-repair
1143 :help "Reload StGit status from disk"]
1144 ["Repair" stgit-repair
1145 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1146 :help "Repair StGit metadata"]
1148 ["Undo" stgit-undo t]
1149 ["Redo" stgit-redo t]
1151 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1153 ["New patch" stgit-new-and-refresh
1154 :help "Create a new patch from changes in index or work tree"
1155 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1156 ["New empty patch" stgit-new
1157 :help "Create a new, empty patch"]
1158 ["(Un)mark patch" stgit-toggle-mark
1159 :label (if (memq (stgit-patch-name-at-point nil t)
1160 stgit-marked-patches)
1161 "Unmark patch" "Mark patch")
1162 :active (stgit-patch-name-at-point nil t)]
1163 ["Expand/collapse patch"
1164 (let ((patches (stgit-patches-marked-or-at-point)))
1165 (if (member-if ,patch-collapsed-p patches)
1166 (stgit-expand patches)
1167 (stgit-collapse patches)))
1168 :label (if (member-if ,patch-collapsed-p
1169 (stgit-patches-marked-or-at-point))
1172 :active (stgit-patches-marked-or-at-point)]
1173 ["Edit patch" stgit-edit
1174 :help "Edit patch comment"
1175 :active (stgit-patch-name-at-point nil t)]
1176 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1177 ["Push/pop patch" stgit-push-or-pop
1178 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1179 (stgit-applied-patchsyms t))
1180 "Pop patches" "Push patches")]
1181 ["Delete patches" stgit-delete
1182 :active (stgit-patches-marked-or-at-point nil t)]
1184 ["Move patches" stgit-move-patches
1185 :active stgit-marked-patches
1186 :help "Move marked patch(es) to point"]
1187 ["Squash patches" stgit-squash
1188 :active (> (length stgit-marked-patches) 1)
1189 :help "Merge marked patches into one"]
1191 ["Refresh top patch" stgit-refresh
1192 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1193 :help "Refresh the top patch with changes in index or work tree"]
1194 ["Refresh this patch" (stgit-refresh t)
1195 :keys "\\[universal-argument] \\[stgit-refresh]"
1196 :help "Refresh marked patch with changes in index or work tree"
1197 :active (and (not (and (stgit-index-empty-p)
1198 (stgit-work-tree-empty-p)))
1199 (stgit-patch-name-at-point nil t))]
1201 ["Find file" stgit-select
1202 :active (eq (get-text-property (point) 'entry-type) 'file)]
1203 ["Open file" stgit-find-file-other-window
1204 :active (eq (get-text-property (point) 'entry-type) 'file)]
1205 ["Toggle file index" stgit-toggle-index
1206 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1207 (memq (stgit-patch-name-at-point) '(:work :index)))
1208 :label (if (eq (stgit-patch-name-at-point) :work)
1209 "Move change to index"
1210 "Move change to work tree")]
1212 ["Show diff" stgit-diff
1213 :active (get-text-property (point) 'entry-type)]
1214 ["Show diff for range of applied patches" stgit-diff-range
1215 :active (= (length stgit-marked-patches) 1)]
1217 :active (stgit-git-index-unmerged-p)
1218 ["Combined diff" stgit-diff-combined
1219 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1220 ["Diff against base" stgit-diff-base
1221 :help "Show diff against the common base"
1222 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1223 ["Diff against ours" stgit-diff-ours
1224 :help "Show diff against our branch"
1225 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1226 ["Diff against theirs" stgit-diff-theirs
1227 :help "Show diff against their branch"
1228 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1230 ["Interactive merge" stgit-find-file-merge
1231 :help "Interactively merge the file"
1232 :active ,at-unmerged-file]
1233 ["Resolve file" stgit-resolve-file
1234 :help "Mark file conflict as resolved"
1235 :active ,at-unmerged-file]
1238 ["Show index & work tree" stgit-toggle-worktree :style toggle
1239 :selected stgit-show-worktree]
1240 ["Show unknown files" stgit-toggle-unknown :style toggle
1241 :selected stgit-show-unknown :active stgit-show-worktree]
1242 ["Show ignored files" stgit-toggle-ignored :style toggle
1243 :selected stgit-show-ignored :active stgit-show-worktree]
1244 ["Show patch names" stgit-toggle-patch-names :style toggle
1245 :selected stgit-show-patch-names]
1246 ["Show recent commits" stgit-toggle-committed :style toggle
1247 :selected stgit-show-committed]
1248 ["Show subversion info" stgit-toggle-svn :style toggle
1249 :selected stgit-show-svn]
1251 ["Switch branches" stgit-branch t
1252 :help "Switch to or create another branch"]
1253 ["Rebase branch" stgit-rebase t
1254 :help "Rebase the current branch"]
1257 ;; disable tool bar editing buttons
1258 (put 'stgit-mode 'mode-class 'special)
1260 (defun stgit-mode ()
1261 "Major mode for interacting with StGit.
1263 Start StGit using \\[stgit].
1267 \\[stgit-help] Show this help text
1268 \\[stgit-quit] Hide the StGit buffer
1269 \\[describe-bindings] Show all key bindings
1271 \\[stgit-reload-or-repair] Reload the StGit buffer
1272 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1274 \\[stgit-undo] Undo most recent StGit operation
1275 \\[stgit-redo] Undo recent undo
1277 \\[stgit-git-status] Run `git-status' (if available)
1279 \\[stgit-execute] Run an stg shell command
1282 \\[stgit-previous-line] Move to previous line
1283 \\[stgit-next-line] Move to next line
1284 \\[stgit-previous-patch] Move to previous patch
1285 \\[stgit-next-patch] Move to next patch
1287 \\[stgit-mark-down] Mark patch and move down
1288 \\[stgit-unmark-up] Unmark patch and move up
1289 \\[stgit-unmark-down] Unmark patch and move down
1291 Commands for patches:
1292 \\[stgit-select] Toggle showing changed files in patch
1293 \\[stgit-refresh] Refresh patch with changes in index or work tree
1294 \\[stgit-diff] Show the patch log and diff
1296 \\[stgit-expand] Show changes in marked patches
1297 \\[stgit-collapse] Hide changes in marked patches
1299 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1300 \\[stgit-new] Create a new, empty patch
1302 \\[stgit-rename] Rename patch
1303 \\[stgit-edit] Edit patch description
1304 \\[stgit-delete] Delete patch(es)
1306 \\[stgit-revert] Revert all changes in index or work tree
1307 \\[stgit-toggle-index] Toggle all changes between index and work tree
1309 \\[stgit-push-next] Push next patch onto stack
1310 \\[stgit-pop-next] Pop current patch from stack
1311 \\[stgit-push-or-pop] Push or pop marked patches
1312 \\[stgit-goto] Make patch at point current by popping or pushing
1314 \\[stgit-squash] Squash (meld together) patches
1315 \\[stgit-move-patches] Move marked patches to point
1317 \\[stgit-commit] Commit patch(es)
1318 \\[stgit-uncommit] Uncommit patch(es)
1321 \\[stgit-select] Open the file in this window
1322 \\[stgit-find-file-other-window] Open the file in another window
1323 \\[stgit-diff] Show the file's diff
1325 \\[stgit-toggle-index] Toggle change between index and work tree
1326 \\[stgit-revert] Revert changes to file
1329 \\[stgit-toggle-patch-names] Toggle showing patch names
1330 \\[stgit-toggle-worktree] Toggle showing index and work tree
1331 \\[stgit-toggle-unknown] Toggle showing unknown files
1332 \\[stgit-toggle-ignored] Toggle showing ignored files
1333 \\[stgit-toggle-committed] Toggle showing recent commits
1334 \\[stgit-toggle-svn] Toggle showing subversion information
1337 \\[stgit-diff] Show diff of patch or file
1338 \\[stgit-diff-range] Show diff for range of patches
1339 \\[stgit-diff-base] Show diff against the merge base
1340 \\[stgit-diff-ours] Show diff against our branch
1341 \\[stgit-diff-theirs] Show diff against their branch
1343 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1344 ignore space changes.
1345 With two prefix arguments (e.g., \\[universal-argument] \
1346 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1348 Commands for merge conflicts:
1349 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1350 \\[stgit-resolve-file] Mark unmerged file as resolved
1352 Commands for branches:
1353 \\[stgit-branch] Switch to or create another branch
1354 \\[stgit-rebase] Rebase the current branch
1356 Customization variables:
1357 `stgit-abbreviate-copies-and-renames'
1358 `stgit-default-show-ignored'
1359 `stgit-default-show-patch-names'
1360 `stgit-default-show-unknown'
1361 `stgit-default-show-worktree'
1362 `stgit-default-show-committed'
1363 `stgit-default-show-svn'
1364 `stgit-default-committed-count'
1365 `stgit-find-copies-harder'
1366 `stgit-show-worktree-mode'
1368 See also \\[customize-group] for the \"stgit\" group."
1369 (kill-all-local-variables)
1370 (buffer-disable-undo)
1371 (setq mode-name "StGit"
1372 major-mode 'stgit-mode
1374 (use-local-map stgit-mode-map)
1375 (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x)))
1376 `((list-buffers-directory . ,default-directory)
1377 (parse-sexp-lookup-properties . t)
1378 (stgit-expanded-patches . (:work :index))
1379 (stgit-index-node . nil)
1380 (stgit-worktree-node . nil)
1381 (stgit-marked-patches . nil)
1382 (stgit-svn-find-rev-hash . ,(make-hash-table :test 'equal))
1383 (stgit-committed-count . ,stgit-default-committed-count)
1384 (stgit-show-committed . ,stgit-default-show-committed)
1385 (stgit-show-ignored . ,stgit-default-show-ignored)
1386 (stgit-show-patch-names . ,stgit-default-show-patch-names)
1387 (stgit-show-svn . ,stgit-default-show-svn)
1388 (stgit-show-unknown . ,stgit-default-show-unknown)
1389 (stgit-show-worktree . ,stgit-default-show-worktree)))
1390 (set-variable 'truncate-lines 't)
1391 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1392 (unless stgit-did-advise
1394 (setq stgit-did-advise t))
1395 (run-hooks 'stgit-mode-hook))
1397 (defun stgit-advise-funlist (funlist)
1398 "Add advice to the functions in FUNLIST so we can refresh the
1399 stgit buffers as the git status of files change."
1402 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1403 (stgit-update-stgit-for-buffer t)))
1407 (defun stgit-advise ()
1408 "Add advice to appropriate (non-stgit) git functions so we can
1409 refresh the stgit buffers as the git status of files change."
1411 (let ((feature (car arg))
1412 (funlist (cdr arg)))
1413 (if (featurep feature)
1414 (stgit-advise-funlist funlist)
1415 (add-to-list 'after-load-alist
1416 `(,feature (stgit-advise-funlist
1417 (quote ,funlist)))))))
1418 ;; lists of (<feature> <function> <function> ...) to be advised
1419 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1420 (git git-add-file git-checkout git-revert-file git-remove-file)
1421 (dired dired-delete-file))))
1423 (defvar stgit-pending-refresh-buffers nil
1424 "Alist of (cons `buffer' `refresh-index') of buffers that need
1425 to be refreshed. `refresh-index' is non-nil if both work tree
1426 and index need to be refreshed.")
1428 (defun stgit-run-pending-refreshs ()
1429 "Run all pending stgit buffer updates as posted by `stgit-post-refresh'."
1430 (let ((buffers stgit-pending-refresh-buffers)
1431 (stgit-inhibit-messages t))
1432 (setq stgit-pending-refresh-buffers nil)
1434 (let* ((elem (car buffers))
1436 (refresh-index (cdr elem)))
1437 (when (buffer-name buffer)
1438 (with-current-buffer buffer
1439 (stgit-refresh-worktree)
1440 (when refresh-index (stgit-refresh-index)))))
1441 (setq buffers (cdr buffers)))))
1443 (defun stgit-post-refresh (buffer refresh-index)
1444 "Update worktree status in BUFFER when Emacs becomes idle. If
1445 REFRESH-INDEX is non-nil, also update the index."
1446 (unless stgit-pending-refresh-buffers
1447 (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs))
1448 (let ((elem (assq buffer stgit-pending-refresh-buffers)))
1450 ;; if buffer is already present, set its refresh-index flag if
1455 (setq stgit-pending-refresh-buffers
1456 (cons (cons buffer refresh-index)
1457 stgit-pending-refresh-buffers)))))
1459 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1460 "When Emacs becomes idle, refresh worktree status in any
1461 `stgit-mode' buffer that shows the status of the current buffer.
1463 If REFRESH-INDEX is non-nil, also update the index."
1464 (let* ((dir (cond ((derived-mode-p 'stgit-status-mode 'dired-mode)
1467 (file-name-directory
1468 (expand-file-name buffer-file-name)))))
1469 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1471 (buffer (and gitdir (stgit-find-buffer gitdir))))
1473 (stgit-post-refresh buffer refresh-index))))
1475 (defun stgit-add-mark (patchsym)
1476 "Mark the patch PATCHSYM."
1477 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1479 (defun stgit-remove-mark (patchsym)
1480 "Unmark the patch PATCHSYM."
1481 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1483 (defun stgit-clear-marks ()
1484 "Unmark all patches."
1485 (setq stgit-marked-patches '()))
1487 (defun stgit-patch-at-point (&optional cause-error)
1488 (get-text-property (point) 'patch-data))
1490 (defun stgit-patch-name-at-point (&optional cause-error types)
1491 "Return the patch name on the current line as a symbol.
1492 If CAUSE-ERROR is not nil, signal an error if none found.
1494 TYPES controls which types of commits and patches can be returned.
1495 If it is t, only allow stgit patches; if 'allow-committed, also
1496 allow historical commits; if nil, also allow work tree and index."
1497 (let ((patch (stgit-patch-at-point)))
1499 (memq (stgit-patch->status patch)
1502 ((allow-committed) '(work index))
1503 ((t) '(work index committed))
1504 (t (error "Bad value"))))
1507 (stgit-patch->name patch))
1509 (error "No patch on this line")))))
1511 (defun stgit-patched-file-at-point ()
1512 (get-text-property (point) 'file-data))
1514 (defun stgit-patches-marked-or-at-point (&optional cause-error types)
1515 "Return the symbols of the marked patches, or the patch on the current line.
1516 If CAUSE-ERRROR is not nil, signal an error if none found.
1518 TYPES controls which types of commits and patches can be returned.
1519 If it is t, only allow stgit patches; if 'allow-committed, also
1520 allow historical commits; if nil, also allow work tree and index."
1521 (if stgit-marked-patches
1522 stgit-marked-patches
1523 (let ((patch (stgit-patch-name-at-point nil types)))
1524 (cond (patch (list patch))
1525 (cause-error (error "No patches marked or at this line"))
1528 (defun stgit-goto-patch (patchsym &optional file)
1529 "Move point to the line containing patch PATCHSYM and return non-nil.
1530 If that patch cannot be found, do nothing and return nil.
1532 If the patch was found and FILE is not nil, instead move to that
1533 file's line. If FILE cannot be found, stay on the line of
1535 (let ((node (ewoc-nth stgit-ewoc 0)))
1536 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1538 (setq node (ewoc-next stgit-ewoc node)))
1539 (when (and node file)
1540 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1541 (file-node (ewoc-nth file-ewoc 0)))
1542 (while (and file-node
1543 (not (equal (stgit-file->file (ewoc-data file-node))
1545 (setq file-node (ewoc-next file-ewoc file-node)))
1547 (ewoc-goto-node file-ewoc file-node)
1548 (move-to-column (stgit-goal-column))
1551 (ewoc-goto-node stgit-ewoc node)
1552 (move-to-column goal-column))))
1554 (defun stgit-init ()
1558 (stgit-capture-output nil
1562 (defun stgit-toggle-mark ()
1563 "Toggle mark on the patch under point."
1566 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1570 (defun stgit-mark ()
1571 "Mark the patch under point."
1574 (let* ((node (ewoc-locate stgit-ewoc))
1575 (patch (ewoc-data node)))
1576 (case (stgit-patch->status patch)
1577 (work (error "Cannot mark the work tree"))
1578 (index (error "Cannot mark the index"))
1579 (committed (error "Cannot mark a committed patch")))
1580 (stgit-add-mark (stgit-patch->name patch))
1581 (let ((column (current-column)))
1582 (ewoc-invalidate stgit-ewoc node)
1583 (move-to-column column))))
1585 (defun stgit-mark-down ()
1586 "Mark the patch under point and move to the next patch."
1591 (defun stgit-unmark ()
1592 "Remove mark from the patch on the current line."
1595 (let* ((node (ewoc-locate stgit-ewoc))
1596 (patch (ewoc-data node)))
1597 (stgit-remove-mark (stgit-patch->name patch))
1598 (let ((column (current-column)))
1599 (ewoc-invalidate stgit-ewoc node)
1600 (move-to-column column))))
1602 (defun stgit-unmark-up ()
1603 "Remove mark from the patch on the previous line."
1606 (stgit-previous-patch)
1609 (defun stgit-unmark-down ()
1610 "Remove mark from the patch on the current line."
1616 (defun stgit-rename (name)
1617 "Rename the patch under point to NAME."
1619 (read-string "Patch name: "
1620 (symbol-name (stgit-patch-name-at-point t t)))))
1622 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1623 (stgit-capture-output nil
1624 (stgit-run "rename" "--" old-patchsym name))
1625 (let ((name-sym (intern name)))
1626 (when (memq old-patchsym stgit-expanded-patches)
1627 (setq stgit-expanded-patches
1628 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1629 (when (memq old-patchsym stgit-marked-patches)
1630 (setq stgit-marked-patches
1631 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1633 (stgit-goto-patch name-sym))))
1635 (defun stgit-reload-or-repair (repair)
1636 "Update the contents of the StGit buffer (`stgit-reload').
1638 With a prefix argument, repair the StGit metadata if the branch
1639 was modified with git commands (`stgit-repair')."
1646 (defun stgit-repair ()
1650 (stgit-capture-output nil
1651 (stgit-run "repair"))
1654 (defun stgit-available-branches (&optional all)
1655 "Returns a list of the names of the available stg branches as strings.
1657 If ALL is not nil, also return non-stgit branches."
1658 (let ((output (with-output-to-string
1659 (stgit-run "branch" "--list")))
1660 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1664 (while (string-match pattern output start)
1665 (setq result (cons (match-string 1 output) result))
1666 (setq start (match-end 0)))
1669 (defun stgit-branch (branch)
1670 "Switch to or create branch BRANCH."
1671 (interactive (list (completing-read "Switch to branch: "
1672 (stgit-available-branches))))
1674 (when (cond ((equal branch (stgit-current-branch))
1675 (error "Branch is already current"))
1676 ((member branch (stgit-available-branches t))
1677 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1679 ((not (string-match stgit-allowed-branch-name-re branch))
1680 (error "Invalid branch name"))
1681 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1682 (let ((branch-point (completing-read
1683 "Branch from (default current branch): "
1684 (stgit-available-branches))))
1685 (stgit-capture-output nil
1687 `("branch" "--create" "--"
1689 ,@(unless (zerop (length branch-point))
1690 (list branch-point)))))
1694 (defun stgit-available-refs (&optional omit-stgit)
1695 "Returns a list of the available git refs.
1696 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1697 (let* ((output (with-output-to-string
1698 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1699 "refs/tags" "refs/heads"
1701 (result (split-string output "\n" t)))
1703 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1704 (substring s (match-end 0))
1707 (delete-if (lambda (s)
1708 (string-match "^refs/heads/.*\\.stgit$" s))
1712 (defun stgit-parent-branch ()
1713 "Return the parent branch of the current stg branch as per
1714 git-config setting branch.<branch>.stgit.parentbranch."
1715 (let ((output (with-output-to-string
1716 (stgit-run-git-silent "config"
1717 (format "branch.%s.stgit.parentbranch"
1718 (stgit-current-branch))))))
1719 (when (string-match ".*" output)
1720 (match-string 0 output))))
1722 (defun stgit-rebase (new-base)
1723 "Rebase the current branch to NEW-BASE.
1725 Interactively, first ask which branch to rebase to. Defaults to
1726 what git-config branch.<branch>.stgit.parentbranch is set to."
1727 (interactive (list (completing-read "Rebase to: "
1728 (stgit-available-refs t)
1730 (stgit-parent-branch))))
1732 (stgit-capture-output nil (stgit-run "rebase" "--" new-base))
1735 (defun stgit-commit (count)
1736 "Run stg commit on COUNT commits.
1737 Interactively, the prefix argument is used as COUNT.
1738 A negative COUNT will uncommit instead."
1742 (stgit-uncommit (- count))
1743 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1746 (defun stgit-uncommit (count)
1747 "Run stg uncommit on COUNT commits.
1748 Interactively, the prefix argument is used as COUNT.
1749 A negative COUNT will commit instead."
1753 (stgit-commit (- count))
1754 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1757 (defun stgit-neighbour-file ()
1758 "Return the file name of the next file after point, or the
1759 previous file if point is at the last file within a patch."
1760 (let ((old-point (point))
1762 (and (zerop (forward-line 1))
1763 (let ((f (stgit-patched-file-at-point)))
1764 (and f (setq neighbour-file (stgit-file->file f)))))
1765 (goto-char old-point)
1766 (unless neighbour-file
1767 (and (zerop (forward-line -1))
1768 (let ((f (stgit-patched-file-at-point)))
1769 (and f (setq neighbour-file (stgit-file->file f)))))
1770 (goto-char old-point))
1773 (defun stgit-revert-file ()
1774 "Revert the file at point, which must be in the index or the
1778 (let* ((patched-file (or (stgit-patched-file-at-point)
1779 (error "No file on the current line")))
1780 (patch-name (stgit-patch-name-at-point))
1781 (file-status (stgit-file->status patched-file))
1782 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1783 (stgit-file->cr-to patched-file))
1784 ((eq file-status 'add)
1785 (stgit-file->file patched-file))))
1786 (co-file (cond ((eq file-status 'rename)
1787 (stgit-file->cr-from patched-file))
1788 ((not (memq file-status '(copy add)))
1789 (stgit-file->file patched-file))))
1790 (next-file (stgit-neighbour-file)))
1792 (unless (memq patch-name '(:work :index))
1793 (error "No index or working tree file on this line"))
1795 (when (eq file-status 'ignore)
1796 (error "Cannot revert ignored files"))
1798 (when (eq file-status 'unknown)
1799 (error "Cannot revert unknown files"))
1801 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1802 (when (yes-or-no-p (format "Revert %d file%s? "
1804 (if (= nfiles 1) "" "s")))
1805 (stgit-capture-output nil
1807 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1809 (stgit-run-git "checkout" "HEAD" co-file)))
1811 (stgit-goto-patch patch-name next-file)))))
1813 (defun stgit-revert ()
1814 "Revert the change at point, which must be the index, the work
1815 tree, or a single change in either."
1818 (let ((patched-file (stgit-patched-file-at-point)))
1821 (let* ((patch-name (or (stgit-patch-name-at-point)
1822 (error "No patch or file at point")))
1823 (patch-desc (case patch-name
1826 (t (error (substitute-command-keys
1827 "Use \\[stgit-delete] to delete a patch"))))))
1828 (when (if (eq patch-name :work)
1829 (stgit-work-tree-empty-p)
1830 (stgit-index-empty-p))
1831 (error (format "There are no changes in the %s to revert"
1833 (and (eq patch-name :index)
1834 (not (stgit-work-tree-empty-p))
1835 (error "Cannot revert index as work tree contains unstaged changes"))
1837 (when (yes-or-no-p (format "Revert all changes in the %s? "
1839 (if (eq patch-name :index)
1840 (stgit-run-git-silent "reset" "--hard" "-q")
1841 (stgit-run-git-silent "checkout" "--" "."))
1842 (stgit-refresh-index)
1843 (stgit-refresh-worktree)
1844 (stgit-goto-patch patch-name))))))
1846 (defun stgit-resolve-file ()
1847 "Resolve conflict in the file at point."
1850 (let* ((patched-file (stgit-patched-file-at-point))
1851 (patch (stgit-patch-at-point))
1852 (patch-name (and patch (stgit-patch->name patch)))
1853 (status (and patched-file (stgit-file->status patched-file))))
1855 (unless (memq patch-name '(:work :index))
1856 (error "No index or working tree file on this line"))
1858 (unless (eq status 'unmerged)
1859 (error "No conflict to resolve at the current line"))
1861 (stgit-capture-output nil
1862 (stgit-move-change-to-index (stgit-file->file patched-file)))
1866 (defun stgit-push-or-pop-patches (do-push npatches)
1867 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1868 NPATCHES patches, or all patches if NPATCHES is t."
1870 (stgit-capture-output nil
1872 (if do-push "push" "pop")
1875 (list "-n" npatches))))
1877 (stgit-refresh-git-status))
1879 (defun stgit-push-next (npatches)
1880 "Push the first unapplied patch.
1881 With numeric prefix argument, push that many patches."
1883 (stgit-push-or-pop-patches t npatches))
1885 (defun stgit-pop-next (npatches)
1886 "Pop the topmost applied patch.
1887 With numeric prefix argument, pop that many patches.
1889 If NPATCHES is t, pop all patches."
1891 (stgit-push-or-pop-patches nil npatches))
1893 (defun stgit-applied-patches (&optional only-patches)
1894 "Return a list of the applied patches.
1896 If ONLY-PATCHES is not nil, exclude index and work tree."
1897 (let ((states (if only-patches
1899 '(applied top index work)))
1901 (ewoc-map (lambda (patch)
1902 (when (memq (stgit-patch->status patch) states)
1903 (setq result (cons patch result)))
1908 (defun stgit-applied-patchsyms (&optional only-patches)
1909 "Return a list of the symbols of the applied patches.
1911 If ONLY-PATCHES is not nil, exclude index and work tree."
1912 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1914 (defun stgit-push-or-pop ()
1915 "Push or pop the marked patches."
1918 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1919 (applied-syms (stgit-applied-patchsyms t))
1920 (unapplied (set-difference patchsyms applied-syms)))
1921 (stgit-capture-output nil
1923 (if unapplied "push" "pop")
1925 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1928 (defun stgit-goto-target ()
1929 "Return the goto target at point: a patchsym, :top,
1931 (let ((patch (stgit-patch-at-point)))
1933 (case (stgit-patch->status patch)
1935 ((committed) :bottom)
1936 (t (stgit-patch->name patch))))
1937 ((not (next-single-property-change (point) 'patch-data))
1939 ((not (previous-single-property-change (point) 'patch-data))
1942 (defun stgit-goto ()
1943 "Go to the patch on the current line.
1945 Push or pop patches to make this patch topmost. Push or pop all
1946 patches if used on a line after or before all patches."
1949 (let ((patchsym (stgit-goto-target)))
1951 (error "No patch to go to on this line"))
1953 (:top (stgit-push-or-pop-patches t t))
1954 (:bottom (stgit-push-or-pop-patches nil t))
1955 (t (stgit-capture-output nil
1956 (stgit-run "goto" "--" patchsym))
1959 (defun stgit-id (patchsym)
1960 "Return the git commit id for PATCHSYM.
1961 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1962 (if (keywordp patchsym)
1964 (let ((result (with-output-to-string
1965 (stgit-run-silent "id" "--" patchsym))))
1966 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1967 (error "Cannot find commit id for %s" patchsym))
1968 (match-string 1 result))))
1970 (defun stgit-whitespace-diff-arg (arg)
1972 (cond ((> arg 4) "--ignore-all-space")
1973 ((> arg 1) "--ignore-space-change"))))
1975 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1976 "Show the patch on the current line.
1978 UNMERGED-STAGE is the argument to `git-diff' that that selects
1979 which stage to diff against in the case of unmerged files."
1980 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
1981 (patch-name (stgit-patch-name-at-point t)))
1982 (stgit-capture-output "*StGit patch*"
1983 (case (get-text-property (point) 'entry-type)
1985 (let* ((patched-file (stgit-patched-file-at-point))
1986 (patch-id (let ((id (stgit-id patch-name)))
1987 (if (and (eq id :index)
1988 (eq (stgit-file->status patched-file)
1992 (args (append (and space-arg (list space-arg))
1993 (and (stgit-file->cr-from patched-file)
1994 (list (stgit-find-copies-harder-diff-arg)))
1995 (cond ((eq patch-id :index)
1997 ((eq patch-id :work)
1998 (list unmerged-stage))
2000 (list (concat patch-id "^") patch-id)))
2002 (if (stgit-file->copy-or-rename patched-file)
2003 (list (stgit-file->cr-from patched-file)
2004 (stgit-file->cr-to patched-file))
2005 (list (stgit-file->file patched-file))))))
2006 (apply 'stgit-run-git "diff" args)))
2008 (let* ((patch-id (stgit-id patch-name)))
2009 (if (or (eq patch-id :index) (eq patch-id :work))
2010 (apply 'stgit-run-git "diff"
2011 (stgit-find-copies-harder-diff-arg)
2012 (append (and space-arg (list space-arg))
2013 (if (eq patch-id :index)
2015 (list unmerged-stage))))
2016 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
2017 (and space-arg (list "-O" space-arg))
2019 (list (stgit-patch-name-at-point)))))
2020 (apply 'stgit-run args)))))
2022 (error "No patch or file at point")))
2023 (with-current-buffer standard-output
2024 (goto-char (point-min))
2027 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
2028 `(defun ,name (&optional ignore-whitespace)
2029 ,(format "Show the patch on the current line.
2031 %sWith a prefix argument, ignore whitespace. With a prefix argument
2032 greater than four (e.g., \\[universal-argument] \
2033 \\[universal-argument] \\[%s]), ignore all whitespace."
2035 (format "For unmerged files, %s.\n\n" unmerged-action)
2040 (stgit-show-patch ,diff-arg ignore-whitespace)))
2042 (stgit-define-diff stgit-diff
2044 (stgit-define-diff stgit-diff-ours
2046 "diff against our branch")
2047 (stgit-define-diff stgit-diff-theirs
2049 "diff against their branch")
2050 (stgit-define-diff stgit-diff-base
2052 "diff against the merge base")
2053 (stgit-define-diff stgit-diff-combined
2055 "show a combined diff")
2057 (defun stgit-diff-range (&optional ignore-whitespace)
2058 "Show diff for the range of patches between point and the marked patch.
2060 With a prefix argument, ignore whitespace. With a prefix argument
2061 greater than four (e.g., \\[universal-argument] \
2062 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
2065 (unless (= (length stgit-marked-patches) 1)
2066 (error "Need exactly one patch marked"))
2067 (let* ((patches (stgit-sort-patches
2068 (cons (stgit-patch-name-at-point t 'allow-committed)
2069 stgit-marked-patches)
2071 (first-patch (car patches))
2072 (second-patch (if (cdr patches) (cadr patches) first-patch))
2073 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
2074 (applied (stgit-applied-patchsyms t)))
2075 (unless (and (memq first-patch applied) (memq second-patch applied))
2076 (error "Can only show diff range for applied patches"))
2077 (stgit-capture-output (format "*StGit diff %s..%s*"
2078 first-patch second-patch)
2079 (apply 'stgit-run-git (append '("diff" "--patch-with-stat")
2080 (and whitespace-arg (list whitespace-arg))
2081 (list (format "%s^" (stgit-id first-patch))
2082 (stgit-id second-patch))))
2083 (with-current-buffer standard-output
2084 (goto-char (point-min))
2087 (defun stgit-move-change-to-index (file &optional force)
2088 "Copies the work tree state of FILE to index, using git add or git rm.
2090 If FORCE is not nil, use --force."
2091 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
2092 '("add") '("rm" "-q"))))
2093 (stgit-capture-output "*git output*"
2094 (apply 'stgit-run-git (append op (and force '("--force"))
2095 '("--") (list file))))))
2097 (defun stgit-remove-change-from-index (file)
2098 "Unstages the change in FILE from the index"
2099 (stgit-capture-output "*git output*"
2100 (stgit-run-git "reset" "-q" "--" file)))
2102 (defun stgit-git-index-unmerged-p ()
2104 (with-output-to-string
2105 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
2107 "--quiet" "HEAD")))))
2110 (defun stgit-file-toggle-index ()
2111 "Move modified file in or out of the index.
2113 Leaves the point where it is, but moves the mark to where the
2114 file ended up. You can then jump to the file with \
2115 \\[exchange-point-and-mark]."
2118 (let* ((patched-file (or (stgit-patched-file-at-point)
2119 (error "No file on the current line")))
2120 (patched-status (stgit-file->status patched-file)))
2121 (when (eq patched-status 'unmerged)
2122 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
2123 (let* ((patch (stgit-patch-at-point))
2124 (patch-name (stgit-patch->name patch))
2125 (mark-file (if (eq patched-status 'rename)
2126 (stgit-file->cr-to patched-file)
2127 (stgit-file->file patched-file)))
2128 (point-file (if (eq patched-status 'rename)
2129 (stgit-file->cr-from patched-file)
2130 (stgit-neighbour-file))))
2132 (cond ((eq patch-name :work)
2133 (stgit-move-change-to-index (stgit-file->file patched-file)
2134 (eq patched-status 'ignore)))
2135 ((eq patch-name :index)
2136 (stgit-remove-change-from-index (stgit-file->file patched-file)))
2138 (error "Can only move files between working tree and index")))
2139 (stgit-refresh-worktree)
2140 (stgit-refresh-index)
2141 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
2143 (stgit-goto-patch patch-name point-file))))
2145 (defun stgit-toggle-index ()
2146 "Move change in or out of the index.
2148 Works on index and work tree, as well as files in either.
2150 Leaves the point where it is, but moves the mark to where the
2151 file ended up. You can then jump to the file with \
2152 \\[exchange-point-and-mark]."
2155 (if (stgit-patched-file-at-point)
2156 (stgit-file-toggle-index)
2157 (let ((patch-name (stgit-patch-name-at-point)))
2158 (unless (memq patch-name '(:index :work))
2159 (error "Can only move changes between working tree and index"))
2160 (when (stgit-git-index-unmerged-p)
2161 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
2162 (if (if (eq patch-name :index)
2163 (stgit-index-empty-p)
2164 (stgit-work-tree-empty-p))
2165 (message "No changes to be moved")
2166 (stgit-capture-output nil
2167 (if (eq patch-name :work)
2168 (stgit-run-git "add" "--update")
2169 (stgit-run-git "reset" "--mixed" "-q")))
2170 (stgit-refresh-worktree)
2171 (stgit-refresh-index))
2172 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
2174 (defun stgit-edit ()
2175 "Edit the patch on the current line."
2178 (let ((patchsym (stgit-patch-name-at-point t t))
2179 (edit-buf (get-buffer-create "*StGit edit*"))
2180 (dir default-directory))
2181 (log-edit 'stgit-confirm-edit t nil edit-buf)
2182 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
2183 (setq default-directory dir)
2184 (let ((standard-output edit-buf))
2186 (stgit-run-silent "edit" "--save-template=-" "--" patchsym)))))
2188 (defun stgit-confirm-edit ()
2190 (let ((file (make-temp-file "stgit-edit-")))
2191 (write-region (point-min) (point-max) file)
2192 (stgit-capture-output nil
2193 (stgit-run "edit" "-f" file "--" stgit-edit-patchsym))
2194 (with-current-buffer log-edit-parent-buffer
2197 (defun stgit-new (add-sign &optional refresh)
2198 "Create a new patch.
2199 With a prefix argument, include a \"Signed-off-by:\" line at the
2203 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2204 (dir default-directory))
2205 (log-edit 'stgit-confirm-new t nil edit-buf)
2206 (setq default-directory dir)
2207 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2210 (let ((standard-output (current-buffer)))
2211 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2213 (defun stgit-confirm-new ()
2215 (let ((file (make-temp-file "stgit-edit-"))
2216 (refresh stgit-refresh-after-new))
2217 (write-region (point-min) (point-max) file)
2218 (stgit-capture-output nil
2219 (stgit-run "new" "-f" file))
2220 (with-current-buffer log-edit-parent-buffer
2225 (defun stgit-new-and-refresh (add-sign)
2226 "Create a new patch and refresh it with the current changes.
2228 With a prefix argument, include a \"Signed-off-by:\" line at the
2231 This works just like running `stgit-new' followed by `stgit-refresh'."
2234 (stgit-new add-sign t))
2236 (defun stgit-create-patch-name (description)
2237 "Create a patch name from a long description"
2239 (while (> (length description) 0)
2240 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2241 (setq patch (downcase (concat patch
2242 (match-string 0 description))))
2243 (setq description (substring description (match-end 0))))
2244 ((string-match "\\` +" description)
2245 (setq patch (concat patch "-"))
2246 (setq description (substring description (match-end 0))))
2247 ((string-match "\\`[^a-zA-Z_-]+" description)
2248 (setq description (substring description (match-end 0))))))
2249 (cond ((= (length patch) 0)
2251 ((> (length patch) 20)
2252 (substring patch 0 20))
2255 (defun stgit-delete (patchsyms &optional spill-p)
2256 "Delete the patches in PATCHSYMS.
2257 Interactively, delete the marked patches, or the patch at point.
2259 With a prefix argument, or SPILL-P, spill the patch contents to
2260 the work tree and index."
2261 (interactive (list (stgit-patches-marked-or-at-point t t)
2262 current-prefix-arg))
2265 (error "No patches to delete"))
2266 (when (memq :index patchsyms)
2267 (error "Cannot delete the index"))
2268 (when (memq :work patchsyms)
2269 (error "Cannot delete the work tree"))
2271 (let ((npatches (length patchsyms)))
2272 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2274 (if (= 1 npatches) "" "es")
2276 " (spilling contents to index)"
2278 (let ((args (append (when spill-p '("--spill"))
2281 (stgit-capture-output nil
2282 (apply 'stgit-run "delete" args))
2285 (defun stgit-move-patches-target ()
2286 "Return the patchsym indicating a target patch for
2287 `stgit-move-patches'.
2289 This is either the first unmarked patch at or after point, or one
2290 of :top and :bottom if the point is after or before the applied
2296 (let ((patchsym (stgit-patch-name-at-point)))
2297 (cond ((memq patchsym '(:work :index)) (setq result :top))
2298 (patchsym (if (memq patchsym stgit-marked-patches)
2300 (setq result patchsym)))
2301 ((re-search-backward "^>" nil t) (setq result :top))
2302 (t (setq result :bottom)))))
2305 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2306 "Returns the list of patches in PATCHSYMS sorted according to
2307 their position in the patch series, bottommost first.
2309 PATCHSYMS must not contain duplicate entries, unless
2310 ALLOW-DUPLICATES is not nil."
2311 (let (sorted-patchsyms
2312 (series (with-output-to-string
2313 (with-current-buffer standard-output
2314 (stgit-run-silent "series" "--noprefix"))))
2316 (while (string-match "^\\(.+\\)" series start)
2317 (let ((patchsym (intern (match-string 1 series))))
2318 (when (memq patchsym patchsyms)
2319 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2320 (setq start (match-end 0)))
2321 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2323 (unless allow-duplicates
2324 (unless (= (length patchsyms) (length sorted-patchsyms))
2325 (error "Internal error")))
2329 (defun stgit-move-patches (patchsyms target-patch)
2330 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2331 If TARGET-PATCH is :bottom or :top, move the patches to the
2332 bottom or top of the stack, respectively.
2334 Interactively, move the marked patches to where the point is."
2335 (interactive (list stgit-marked-patches
2336 (stgit-move-patches-target)))
2339 (error "Need at least one patch to move"))
2341 (unless target-patch
2342 (error "Point not at a patch"))
2344 ;; need to have patchsyms sorted by position in the stack
2345 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2346 (stgit-capture-output nil
2347 (if (eq target-patch :top)
2348 (apply 'stgit-run "float" "--" sorted-patchsyms)
2351 (append (unless (eq target-patch :bottom)
2352 (list "--to" target-patch))
2354 sorted-patchsyms)))))
2357 (defun stgit-squash (patchsyms)
2358 "Squash the patches in PATCHSYMS.
2359 Interactively, squash the marked patches.
2361 Unless there are any conflicts, the patches will be merged into
2362 one patch, which will occupy the same spot in the series as the
2363 deepest patch had before the squash."
2364 (interactive (list stgit-marked-patches))
2366 (when (< (length patchsyms) 2)
2367 (error "Need at least two patches to squash"))
2368 (let ((stgit-buffer (current-buffer))
2369 (edit-buf (get-buffer-create "*StGit edit*"))
2370 (dir default-directory)
2371 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2372 (log-edit 'stgit-confirm-squash t nil edit-buf)
2373 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2374 (setq default-directory dir)
2375 (let ((result (let ((standard-output edit-buf))
2377 (apply 'stgit-run-silent "squash"
2378 "--save-template=-" "--" sorted-patchsyms)))))
2380 ;; stg squash may have reordered the patches or caused conflicts
2381 (with-current-buffer stgit-buffer
2384 (unless (eq 0 result)
2386 (rename-buffer "*StGit error*")
2387 (resize-temp-buffer-window)
2388 (switch-to-buffer-other-window stgit-buffer)
2389 (error "stg squash failed")))))
2391 (defun stgit-confirm-squash ()
2393 (let ((file (make-temp-file "stgit-edit-")))
2394 (write-region (point-min) (point-max) file)
2395 (stgit-capture-output nil
2396 (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms))
2397 (with-current-buffer log-edit-parent-buffer
2399 ;; Go to first marked patch and stay there
2400 (goto-char (point-min))
2401 (re-search-forward (concat "^[>+-]\\*") nil t)
2402 (move-to-column goal-column)
2403 (let ((pos (point)))
2407 (defun stgit-help ()
2408 "Display help for the StGit mode."
2410 (describe-function 'stgit-mode))
2412 (defun stgit-execute-process-sentinel (process sentinel)
2413 (let (old-sentinel stgit-buf)
2414 (with-current-buffer (process-buffer process)
2415 (setq old-sentinel old-process-sentinel
2416 stgit-buf stgit-buffer))
2417 (and (memq (process-status process) '(exit signal))
2418 (buffer-live-p stgit-buf)
2419 (with-current-buffer stgit-buf
2421 (funcall old-sentinel process sentinel)))
2423 (defun stgit-execute-process-filter (process output)
2424 (with-current-buffer (process-buffer process)
2425 (let* ((old-point (point))
2426 (pmark (process-mark process))
2427 (insert-at (marker-position pmark))
2428 (at-pmark (= insert-at old-point)))
2429 (goto-char insert-at)
2430 (insert-before-markers output)
2431 (comint-carriage-motion insert-at (point))
2432 (set-marker pmark (point))
2434 (goto-char old-point)))))
2436 (defun stgit-execute ()
2437 "Prompt for an stg command to execute in a shell.
2439 The names of any marked patches or the patch at point are
2440 inserted in the command to be executed.
2442 If the command ends in an ampersand, run it asynchronously.
2444 When the command has finished, reload the stgit buffer."
2447 (let* ((patches (stgit-patches-marked-or-at-point nil 'allow-committed))
2448 (patch-names (mapcar 'symbol-name patches))
2449 (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names))
2450 (defaultcmd (if patches
2453 (mapconcat 'identity patch-names " "))
2455 (cmd (read-from-minibuffer "Shell command: " (cons defaultcmd 5)
2456 nil nil 'shell-command-history))
2457 (async (string-match "&[ \t]*\\'" cmd))
2458 (buffer (get-buffer-create
2460 "*Async Shell Command*"
2461 "*Shell Command Output*"))))
2462 ;; cannot use minibuffer as stgit-reload would overwrite it; if we
2463 ;; show the buffer, shell-command will not use the minibuffer
2464 (display-buffer buffer)
2467 (let ((old-buffer (current-buffer)))
2468 (with-current-buffer buffer
2469 (let ((process (get-buffer-process buffer)))
2470 (set (make-local-variable 'old-process-sentinel)
2471 (process-sentinel process))
2472 (set (make-local-variable 'stgit-buffer)
2474 (set-process-filter process 'stgit-execute-process-filter)
2475 (set-process-sentinel process 'stgit-execute-process-sentinel))))
2476 (with-current-buffer buffer
2477 (comint-carriage-motion (point-min) (point-max)))
2478 (shrink-window-if-larger-than-buffer (get-buffer-window buffer))
2481 (defun stgit-undo-or-redo (redo hard)
2482 "Run stg undo or, if REDO is non-nil, stg redo.
2484 If HARD is non-nil, use the --hard flag."
2486 (let ((cmd (if redo "redo" "undo")))
2487 (stgit-capture-output nil
2489 (when (or (and (stgit-index-empty-p)
2490 (stgit-work-tree-empty-p))
2491 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2493 (stgit-run cmd "--hard"))
2497 (defun stgit-undo (&optional arg)
2499 With prefix argument, run it with the --hard flag.
2501 See also `stgit-redo'."
2503 (stgit-undo-or-redo nil arg))
2505 (defun stgit-redo (&optional arg)
2507 With prefix argument, run it with the --hard flag.
2509 See also `stgit-undo'."
2511 (stgit-undo-or-redo t arg))
2513 (defun stgit-refresh (&optional arg)
2515 If the index contains any changes, only refresh from index.
2517 With prefix argument, refresh the marked patch or the patch under point."
2520 (let ((patchargs (if arg
2521 (let ((patches (stgit-patches-marked-or-at-point t t)))
2522 (when (> (length patches) 1)
2523 (error "Too many patches marked"))
2524 (cons "-p" patches))
2526 (unless (stgit-index-empty-p)
2527 (setq patchargs (cons "--index" patchargs)))
2528 (stgit-capture-output nil
2529 (apply 'stgit-run "refresh" patchargs))
2530 (stgit-refresh-git-status))
2533 (defvar stgit-show-worktree nil
2534 "If nil, inhibit showing work tree and index in the stgit buffer.
2536 See also `stgit-show-worktree-mode'.")
2538 (defvar stgit-show-ignored nil
2539 "If nil, inhibit showing files ignored by git.")
2541 (defvar stgit-show-unknown nil
2542 "If nil, inhibit showing files not registered with git.")
2544 (defvar stgit-show-patch-names t
2545 "If nil, inhibit showing patch names.")
2547 (defvar stgit-show-committed nil
2548 "If nil, inhibit showing recent commits.")
2550 (defvar stgit-show-svn nil
2551 "If nil, inhibit showing git svn information.")
2553 (defvar stgit-committed-count nil
2554 "The number of recent commits to show.")
2556 (defmacro stgit-define-toggle-view (sym help)
2557 (declare (indent 1))
2558 (let* ((name (symbol-name sym))
2559 (fun (intern (concat "stgit-toggle-" name)))
2560 (flag (intern (concat "stgit-show-" name))))
2561 ;; make help-follow find the correct function
2562 `(put (quote ,fun) 'definition-name 'stgit-define-toggle-view)
2563 `(defun ,fun (&optional arg)
2568 (> (prefix-numeric-value arg) 0)
2572 (stgit-define-toggle-view worktree
2573 "Toggle the visibility of the work tree.
2574 With ARG, show the work tree if ARG is positive.
2576 Its initial setting is controlled by `stgit-default-show-worktree'.
2578 `stgit-show-worktree-mode' controls where on screen the index and
2579 work tree will show up.")
2581 (stgit-define-toggle-view ignored
2582 "Toggle the visibility of files ignored by git in the work
2583 tree. With ARG, show these files if ARG is positive.
2585 Its initial setting is controlled by `stgit-default-show-ignored'.
2587 Use \\[stgit-toggle-worktree] to show the work tree.")
2589 (stgit-define-toggle-view unknown
2590 "Toggle the visibility of files not registered with git in the
2591 work tree. With ARG, show these files if ARG is positive.
2593 Its initial setting is controlled by `stgit-default-show-unknown'.
2595 Use \\[stgit-toggle-worktree] to show the work tree.")
2597 (stgit-define-toggle-view patch-names
2598 "Toggle the visibility of patch names. With ARG, show patch names
2601 The initial setting is controlled by `stgit-default-show-patch-names'.")
2603 (stgit-define-toggle-view svn
2604 "Toggle showing subversion information from git svn. With ARG,
2605 show svn information if ARG is positive.
2607 The initial setting is controlled by `stgit-default-show-svn'.")
2609 (defun stgit-toggle-committed (&optional arg)
2610 "Toggle the visibility of historical git commits.
2611 With ARG, set the number of commits to show to ARG, and disable
2612 them if ARG is zero.
2614 The initial setting is controlled by `stgit-default-show-committed'."
2618 (setq stgit-show-committed (not stgit-show-committed))
2619 (let ((n (prefix-numeric-value arg)))
2620 (setq stgit-show-committed (> n 0))
2621 (setq stgit-committed-count n)))