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-find-copies-harder nil
72 "Try harder to find copied files when listing patches.
74 When not nil, runs git diff-tree with the --find-copies-harder
75 flag, which reduces performance."
78 :set 'stgit-set-default)
80 (defcustom stgit-show-worktree-mode 'center
81 "This variable controls where the \"Index\" and \"Work tree\"
82 will be shown on in the buffer.
84 It can be set to 'top (above all patches), 'center (show between
85 applied and unapplied patches), and 'bottom (below all patches)."
86 :type '(radio (const :tag "above all patches (top)" top)
87 (const :tag "between applied and unapplied patches (center)"
89 (const :tag "below all patches (bottom)" bottom))
91 :link '(variable-link stgit-show-worktree)
92 :set 'stgit-set-default)
94 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
95 "The format string used to format patch lines.
96 The format string is passed to `format-spec' and the following
97 format characters are recognized:
99 %s - A '+', '-', '>' or space, depending on whether the patch is
100 applied, unapplied, top, or something else.
102 %m - An asterisk if the patch is marked, and a space otherwise.
106 %e - The string \"(empty) \" if the patch is empty.
108 %d - The short patch description.
110 %D - The short patch description, or the patch name.
112 When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
113 variable is used instead."
116 :set 'stgit-set-default)
118 (defcustom stgit-noname-patch-line-format "%s%m%e%D"
119 "The alternate format string used to format patch lines.
120 It has the same semantics as `stgit-patch-line-format', and the
121 display can be toggled between the two formats using \
122 \\<stgit-mode-map>\\[stgit-toggle-patch-names].
124 The alternate form is used when the patch name is hidden."
127 :set 'stgit-set-default)
129 (defcustom stgit-default-show-patch-names t
130 "If non-nil, default to showing patch names in a new stgit buffer.
132 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] \
133 to toggle the this setting in an already-started StGit buffer."
136 :link '(variable-link stgit-show-patch-names))
138 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
139 "The format string used to format file lines.
140 The format string is passed to `format-spec' and the following
141 format characters are recognized:
143 %s - A string describing the status of the file.
145 %m - Mode change information
149 %c - A description of file changes."
152 :set 'stgit-set-default)
154 (defface stgit-branch-name-face
156 "The face used for the StGit branch name"
159 (defface stgit-top-patch-face
160 '((((background dark)) (:weight bold :foreground "yellow"))
161 (((background light)) (:weight bold :foreground "purple"))
163 "The face used for the top patch names"
166 (defface stgit-applied-patch-face
167 '((((background dark)) (:foreground "light yellow"))
168 (((background light)) (:foreground "purple"))
170 "The face used for applied patch names"
173 (defface stgit-unapplied-patch-face
174 '((((background dark)) (:foreground "gray80"))
175 (((background light)) (:foreground "orchid"))
177 "The face used for unapplied patch names"
180 (defface stgit-description-face
181 '((((background dark)) (:foreground "tan"))
182 (((background light)) (:foreground "dark red")))
183 "The face used for StGit descriptions"
186 (defface stgit-index-work-tree-title-face
187 '((((supports :slant italic)) :slant italic)
189 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
192 (defface stgit-unmerged-file-face
193 '((((class color) (background light)) (:foreground "red" :bold t))
194 (((class color) (background dark)) (:foreground "red" :bold t)))
195 "StGit mode face used for unmerged file status"
198 (defface stgit-unknown-file-face
199 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
200 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
201 "StGit mode face used for unknown file status"
204 (defface stgit-ignored-file-face
205 '((((class color) (background light)) (:foreground "grey60"))
206 (((class color) (background dark)) (:foreground "grey40")))
207 "StGit mode face used for ignored files")
209 (defface stgit-file-permission-face
210 '((((class color) (background light)) (:foreground "green" :bold t))
211 (((class color) (background dark)) (:foreground "green" :bold t)))
212 "StGit mode face used for permission changes."
215 (defface stgit-modified-file-face
216 '((((class color) (background light)) (:foreground "purple"))
217 (((class color) (background dark)) (:foreground "salmon")))
218 "StGit mode face used for modified file status"
222 "Manage StGit patches for the tree in DIR.
224 See `stgit-mode' for commands available."
225 (interactive "DDirectory: \n")
226 (switch-to-stgit-buffer (git-get-top-dir dir))
229 (defun stgit-assert-mode ()
230 "Signal an error if not in an StGit buffer."
231 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
233 (unless (fboundp 'git-get-top-dir)
234 (defun git-get-top-dir (dir)
235 "Retrieve the top-level directory of a git tree."
236 (let ((cdup (with-output-to-string
237 (with-current-buffer standard-output
239 (unless (eq 0 (call-process "git" nil t nil
240 "rev-parse" "--show-cdup"))
241 (error "Cannot find top-level git tree for %s" dir))))))
242 (expand-file-name (concat (file-name-as-directory dir)
243 (car (split-string cdup "\n")))))))
245 (defun stgit-refresh-git-status (&optional dir)
246 "If it exists, refresh the `git-status' buffer belonging to
247 directory DIR or `default-directory'"
248 (when (and (fboundp 'git-find-status-buffer)
249 (fboundp 'git-refresh-status))
250 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
251 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
252 (when git-status-buffer
253 (with-current-buffer git-status-buffer
254 (git-refresh-status))))))
256 (defun stgit-find-buffer (dir)
257 "Return the buffer displaying StGit patches for DIR, or nil if none."
258 (setq dir (file-name-as-directory dir))
259 (let ((buffers (buffer-list)))
261 (not (with-current-buffer (car buffers)
262 (and (eq major-mode 'stgit-mode)
263 (string= default-directory dir)))))
264 (setq buffers (cdr buffers)))
265 (and buffers (car buffers))))
267 (defun switch-to-stgit-buffer (dir)
268 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
269 (setq dir (file-name-as-directory dir))
270 (let ((buffer (stgit-find-buffer dir)))
271 (switch-to-buffer (or buffer
272 (create-stgit-buffer dir)))))
274 (defstruct (stgit-patch
275 (:conc-name stgit-patch->))
276 status name desc empty files-ewoc)
278 (defun stgit-patch-display-name (patch)
279 (let ((name (stgit-patch->name patch)))
283 (t (symbol-name name)))))
285 (defun stgit-insert-without-trailing-whitespace (text)
286 "Insert TEXT in buffer using `insert', without trailing whitespace.
287 A newline is appended."
288 (unless (string-match "\\(.*?\\) *$" text)
290 (insert (match-string 1 text) ?\n))
292 (defun stgit-line-format ()
293 "Return the current line format; one of
294 `stgit-patch-line-format' and `stgit-noname-patch-line-format'"
295 (if stgit-show-patch-names
296 stgit-patch-line-format
297 stgit-noname-patch-line-format))
299 (defun stgit-patch-pp (patch)
300 (let* ((status (stgit-patch->status patch))
302 (name (stgit-patch->name patch))
303 (face (cdr (assq status stgit-patch-status-face-alist)))
304 (fmt (stgit-line-format))
305 (spec (format-spec-make
311 ?m (if (memq name stgit-marked-patches)
313 ?n (propertize (stgit-patch-display-name patch)
315 'syntax-table (string-to-syntax "w"))
316 ?e (if (stgit-patch->empty patch) "(empty) " "")
317 ?d (propertize (or (stgit-patch->desc patch) "")
318 'face 'stgit-description-face)
319 ?D (propertize (let ((desc (stgit-patch->desc patch)))
320 (if (zerop (length desc))
321 (stgit-patch-display-name patch)
324 (text (format-spec fmt spec)))
326 (stgit-insert-without-trailing-whitespace text)
327 (put-text-property start (point) 'entry-type 'patch)
328 (when (memq name stgit-expanded-patches)
329 (stgit-insert-patch-files patch))
330 (put-text-property start (point) 'patch-data patch)))
332 (defun create-stgit-buffer (dir)
333 "Create a buffer for showing StGit patches.
334 Argument DIR is the repository path."
335 (let ((buf (create-file-buffer (concat dir "*stgit*")))
336 (inhibit-read-only t))
337 (with-current-buffer buf
338 (setq default-directory dir)
340 (set (make-local-variable 'stgit-ewoc)
341 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
342 (setq buffer-read-only t))
345 (def-edebug-spec stgit-capture-output
347 (defmacro stgit-capture-output (name &rest body)
348 "Capture StGit output and, if there was any output, show it in a window
350 Returns nil if there was no output."
351 (declare (debug ([&or stringp null] body))
353 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
354 (stgit-dir default-directory)
355 (inhibit-read-only t))
356 (with-current-buffer output-buf
357 (buffer-disable-undo)
359 (setq default-directory stgit-dir)
360 (setq buffer-read-only t))
361 (let ((standard-output output-buf))
363 (with-current-buffer output-buf
364 (set-buffer-modified-p nil)
365 (setq buffer-read-only t)
366 (if (< (point-min) (point-max))
367 (display-buffer output-buf t)))))
369 (defun stgit-make-run-args (args)
370 "Return a copy of ARGS with its elements converted to strings."
372 ;; don't use (format "%s" ...) to limit type errors
373 (cond ((stringp x) x)
374 ((integerp x) (number-to-string x))
375 ((symbolp x) (symbol-name x))
377 (error "Bad element in stgit-make-run-args args: %S" x))))
380 (defun stgit-run-silent (&rest args)
381 (setq args (stgit-make-run-args args))
382 (apply 'call-process "stg" nil standard-output nil args))
384 (defun stgit-run (&rest args)
385 (setq args (stgit-make-run-args args))
386 (let ((msgcmd (mapconcat #'identity args " ")))
387 (message "Running stg %s..." msgcmd)
388 (apply 'call-process "stg" nil standard-output nil args)
389 (message "Running stg %s...done" msgcmd)))
391 (defun stgit-run-git (&rest args)
392 (setq args (stgit-make-run-args args))
393 (let ((msgcmd (mapconcat #'identity args " ")))
394 (message "Running git %s..." msgcmd)
395 (apply 'call-process "git" nil standard-output nil args)
396 (message "Running git %s...done" msgcmd)))
398 (defun stgit-run-git-silent (&rest args)
399 (setq args (stgit-make-run-args args))
400 (apply 'call-process "git" nil standard-output nil args))
402 (defun stgit-index-empty-p ()
403 "Returns non-nil if the index contains no changes from HEAD."
404 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
406 (defun stgit-work-tree-empty-p ()
407 "Returns non-nil if the work tree contains no changes from index."
408 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
410 (defvar stgit-index-node)
411 (defvar stgit-worktree-node)
413 (defvar stgit-did-advise nil
414 "Set to non-nil if appropriate (non-stgit) git functions have
415 been advised to update the stgit status when necessary.")
417 (defconst stgit-allowed-branch-name-re
418 ;; Disallow control characters, space, del, and "/:@^{}~" in
419 ;; "/"-separated parts; parts may not start with a period (.)
420 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
421 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
422 "Regular expression that (new) branch names must match.")
424 (defun stgit-refresh-index ()
425 (when stgit-index-node
426 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
428 (defun stgit-refresh-worktree ()
429 (when stgit-worktree-node
430 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
432 (defun stgit-run-series-insert-index (ewoc)
433 (setq index-node (cons ewoc (ewoc-enter-last ewoc
439 worktree-node (cons ewoc (ewoc-enter-last ewoc
446 (defun stgit-run-series (ewoc)
447 (setq stgit-index-node nil
448 stgit-worktree-node nil)
449 (let ((inserted-index (not stgit-show-worktree))
454 (let* ((standard-output (current-buffer))
455 (exit-status (stgit-run-silent "series"
456 "--description" "--empty")))
457 (goto-char (point-min))
458 (if (not (zerop exit-status))
459 (cond ((looking-at "stg series: \\(.*\\)")
460 (setq inserted-index t)
461 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
462 (substitute-command-keys
463 "-- not initialized; run \\[stgit-init]")))
465 (error "Error running stg: %s"
469 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
470 (error "Syntax error in output from stg series"))
471 (let* ((state-str (match-string 2))
472 (state (cond ((string= state-str ">") 'top)
473 ((string= state-str "+") 'applied)
474 ((string= state-str "-") 'unapplied)))
475 (name (intern (match-string 4)))
476 (desc (match-string 5))
477 (empty (string= (match-string 1) "0")))
478 (unless inserted-index
479 (when (or (eq stgit-show-worktree-mode 'top)
480 (and (eq stgit-show-worktree-mode 'center)
481 (eq state 'unapplied)))
482 (setq inserted-index t)
483 (stgit-run-series-insert-index ewoc)))
484 (setq all-patchsyms (cons name all-patchsyms))
485 (ewoc-enter-last ewoc
492 (unless inserted-index
493 (stgit-run-series-insert-index ewoc)))
494 (setq stgit-index-node index-node
495 stgit-worktree-node worktree-node
496 stgit-marked-patches (intersection stgit-marked-patches
499 (defun stgit-current-branch ()
500 "Return the name of the current branch."
501 (substring (with-output-to-string
502 (stgit-run-silent "branch"))
505 (defun stgit-reload ()
506 "Update the contents of the StGit buffer."
509 (let ((inhibit-read-only t)
510 (curline (line-number-at-pos))
511 (curpatch (stgit-patch-name-at-point))
512 (curfile (stgit-patched-file-at-point)))
513 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
514 (ewoc-set-hf stgit-ewoc
516 (propertize (stgit-current-branch)
517 'face 'stgit-branch-name-face)
519 (if stgit-show-worktree
522 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
523 shows the working tree\n")
524 'face 'stgit-description-face)))
525 (stgit-run-series stgit-ewoc)
527 (stgit-goto-patch curpatch (and curfile (stgit-file->file curfile)))
528 (goto-line curline)))
529 (stgit-refresh-git-status))
531 (defconst stgit-file-status-code-strings
532 (mapcar (lambda (arg)
534 (propertize (cadr arg) 'face (car (cddr arg)))))
535 '((add "Added" stgit-modified-file-face)
536 (copy "Copied" stgit-modified-file-face)
537 (delete "Deleted" stgit-modified-file-face)
538 (modify "Modified" stgit-modified-file-face)
539 (rename "Renamed" stgit-modified-file-face)
540 (mode-change "Mode change" stgit-modified-file-face)
541 (unmerged "Unmerged" stgit-unmerged-file-face)
542 (unknown "Unknown" stgit-unknown-file-face)
543 (ignore "Ignored" stgit-ignored-file-face)))
544 "Alist of code symbols to description strings")
546 (defconst stgit-patch-status-face-alist
547 '((applied . stgit-applied-patch-face)
548 (top . stgit-top-patch-face)
549 (unapplied . stgit-unapplied-patch-face)
550 (index . stgit-index-work-tree-title-face)
551 (work . stgit-index-work-tree-title-face))
552 "Alist of face to use for a given patch status")
554 (defun stgit-file-status-code-as-string (file)
555 "Return stgit status code for FILE as a string"
556 (let* ((code (assq (stgit-file->status file)
557 stgit-file-status-code-strings))
558 (score (stgit-file->cr-score file)))
560 (if (and score (/= score 100))
561 (format "%s %s" (cdr code)
562 (propertize (format "%d%%" score)
563 'face 'stgit-description-face))
566 (defun stgit-file-status-code (str &optional score)
567 "Return stgit status code from git status string"
568 (let ((code (assoc str '(("A" . add)
577 (setq code (if code (cdr code) 'unknown))
578 (when (stringp score)
579 (if (> (length score) 0)
580 (setq score (string-to-number score))
582 (if score (cons code score) code)))
584 (defconst stgit-file-type-strings
587 (#o160 . "subproject"))
588 "Alist of names of file types")
590 (defun stgit-file-type-string (type)
591 "Return string describing file type TYPE (the high bits of file permission).
592 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
593 (let ((type-str (assoc type stgit-file-type-strings)))
594 (or (and type-str (cdr type-str))
595 (format "unknown type %o" type))))
597 (defun stgit-file-type-change-string (old-perm new-perm)
598 "Return string describing file type change from OLD-PERM to NEW-PERM.
599 Cf. `stgit-file-type-string'."
600 (let ((old-type (lsh old-perm -9))
601 (new-type (lsh new-perm -9)))
602 (cond ((= old-type new-type) "")
603 ((zerop new-type) "")
605 (if (= new-type #o100)
607 (format "(%s)" (stgit-file-type-string new-type))))
608 (t (format "(%s -> %s)"
609 (stgit-file-type-string old-type)
610 (stgit-file-type-string new-type))))))
612 (defun stgit-file-mode-change-string (old-perm new-perm)
613 "Return string describing file mode change from OLD-PERM to NEW-PERM.
614 Cf. `stgit-file-type-change-string'."
615 (setq old-perm (logand old-perm #o777)
616 new-perm (logand new-perm #o777))
617 (if (or (= old-perm new-perm)
621 (let* ((modified (logxor old-perm new-perm))
622 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
623 (cond ((zerop modified) "")
624 ((and (zerop not-x-modified)
625 (or (and (eq #o111 (logand old-perm #o111))
626 (propertize "-x" 'face 'stgit-file-permission-face))
627 (and (eq #o111 (logand new-perm #o111))
628 (propertize "+x" 'face
629 'stgit-file-permission-face)))))
630 (t (concat (propertize (format "%o" old-perm)
631 'face 'stgit-file-permission-face)
633 'face 'stgit-description-face)
634 (propertize (format "%o" new-perm)
635 'face 'stgit-file-permission-face)))))))
637 (defstruct (stgit-file
638 (:conc-name stgit-file->))
639 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
641 (defun stgit-describe-copy-or-rename (file)
642 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
643 from to common-head common-tail)
645 (when stgit-abbreviate-copies-and-renames
646 (setq from (split-string (stgit-file->cr-from file) "/")
647 to (split-string (stgit-file->cr-to file) "/"))
649 (while (and from to (cdr from) (cdr to)
650 (string-equal (car from) (car to)))
651 (setq common-head (cons (car from) common-head)
654 (setq common-head (nreverse common-head)
657 (while (and from to (cdr from) (cdr to)
658 (string-equal (car from) (car to)))
659 (setq common-tail (cons (car from) common-tail)
662 (setq from (nreverse from)
665 (if (or common-head common-tail)
666 (concat (if common-head
667 (mapconcat #'identity common-head "/")
669 (if common-head "/" "")
670 (propertize "{" 'face 'stgit-description-face)
671 (mapconcat #'identity from "/")
673 (mapconcat #'identity to "/")
674 (propertize "}" 'face 'stgit-description-face)
675 (if common-tail "/" "")
677 (mapconcat #'identity common-tail "/")
679 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
681 (defun stgit-file-pp (file)
682 (let ((start (point))
683 (spec (format-spec-make
684 ?s (stgit-file-status-code-as-string file)
685 ?m (stgit-file-mode-change-string
686 (stgit-file->old-perm file)
687 (stgit-file->new-perm file))
688 ?n (if (stgit-file->copy-or-rename file)
689 (stgit-describe-copy-or-rename file)
690 (stgit-file->file file))
691 ?c (propertize (stgit-file-type-change-string
692 (stgit-file->old-perm file)
693 (stgit-file->new-perm file))
694 'face 'stgit-description-face))))
695 (stgit-insert-without-trailing-whitespace
696 (format-spec stgit-file-line-format spec))
697 (add-text-properties start (point)
698 (list 'entry-type 'file
701 (defun stgit-find-copies-harder-diff-arg ()
702 "Return the flag to use with `git-diff' depending on the
703 `stgit-find-copies-harder' flag."
704 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
706 (defun stgit-insert-ls-files (args file-flag)
707 (let ((start (point)))
708 (apply 'stgit-run-git
709 (append '("ls-files" "--exclude-standard" "-z") args))
711 (while (looking-at "\\([^\0]*\\)\0")
712 (let ((name-len (- (match-end 0) (match-beginning 0))))
713 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
714 (forward-char name-len)))))
716 (defun stgit-process-files (callback)
717 (goto-char (point-min))
718 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
719 (goto-char (match-end 0)))
720 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
721 (let ((old-perm (string-to-number (match-string 1) 8))
722 (new-perm (string-to-number (match-string 2) 8)))
723 (goto-char (match-end 0))
726 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
727 (let* ((patch-status (stgit-patch->status patch))
728 (file-subexp (if (eq patch-status 'unapplied)
731 (file (match-string file-subexp)))
736 :cr-score (string-to-number (match-string 2))
737 :cr-from (match-string 3)
738 :cr-to (match-string 4)
739 :status (stgit-file-status-code
742 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
750 :status (stgit-file-status-code
752 :file (match-string 2))))))
753 (goto-char (match-end 0))
754 (funcall callback file)))))
757 (defun stgit-insert-patch-files (patch)
758 "Expand (show modification of) the patch PATCH after the line
760 (let* ((patchsym (stgit-patch->name patch))
762 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
763 (ewoc (ewoc-create #'stgit-file-pp nil nil t))
764 (show-ignored stgit-show-ignored)
765 (show-unknown stgit-show-unknown))
766 (set-marker-insertion-type end t)
767 (setf (stgit-patch->files-ewoc patch) ewoc)
769 (let ((standard-output (current-buffer)))
770 (apply 'stgit-run-git
771 (cond ((eq patchsym :work)
772 (let (standard-output)
773 (stgit-run-git "update-index" "--refresh"))
774 `("diff-files" "-0" ,@args))
775 ((eq patchsym :index)
776 `("diff-index" ,@args "--cached" "HEAD"))
778 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
780 (when (and (eq patchsym :work))
782 (stgit-insert-ls-files '("--ignored" "--others") "I"))
784 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
787 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
788 (point-min) (point-max)))
790 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
792 (unless (ewoc-nth ewoc 0)
795 (propertize "<no files>"
796 'face 'stgit-description-face)
800 (defun stgit-find-file (&optional other-window)
801 (let* ((file (or (stgit-patched-file-at-point)
802 (error "No file at point")))
803 (filename (expand-file-name (stgit-file->file file))))
804 (unless (file-exists-p filename)
805 (error "File does not exist"))
806 (funcall (if other-window 'find-file-other-window 'find-file)
808 (when (eq (stgit-file->status file) 'unmerged)
811 (defun stgit-expand (&optional patches collapse)
812 "Show the contents of marked patches, or the patch at point.
814 See also `stgit-collapse'.
816 Non-interactively, operate on PATCHES, and collapse instead of
817 expand if COLLAPSE is not nil."
818 (interactive (list (stgit-patches-marked-or-at-point t)))
820 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
821 patches stgit-expanded-patches)))
822 (setq stgit-expanded-patches
824 (set-difference stgit-expanded-patches patches-diff)
825 (append stgit-expanded-patches patches-diff)))
826 (ewoc-map #'(lambda (patch)
827 (memq (stgit-patch->name patch) patches-diff))
829 (move-to-column (stgit-goal-column)))
831 (defun stgit-collapse (&optional patches)
832 "Hide the contents of marked patches, or the patch at point.
834 See also `stgit-expand'."
835 (interactive (list (stgit-patches-marked-or-at-point t)))
837 (stgit-expand patches t))
839 (defun stgit-select-patch ()
840 (let ((patchname (stgit-patch-name-at-point)))
841 (stgit-expand (list patchname)
842 (memq patchname stgit-expanded-patches))))
844 (defun stgit-expand-directory (file)
845 (let* ((patch (stgit-patch-at-point))
846 (ewoc (stgit-patch->files-ewoc patch))
847 (node (ewoc-locate ewoc))
848 (filename (stgit-file->file file))
849 (start (make-marker))
854 (set-marker start (point))
855 (set-marker end (point))
856 (set-marker-insertion-type end t))
858 (assert (string-match "/$" filename))
859 ;; remove trailing "/"
860 (setf (stgit-file->file file) (substring filename 0 -1))
861 (ewoc-invalidate ewoc node)
864 (let ((standard-output (current-buffer)))
865 (stgit-insert-ls-files (list "--directory" "--others"
866 "--no-empty-directory" "--"
869 (stgit-process-files (lambda (f)
870 (setq node (ewoc-enter-after ewoc node f))))))
872 (move-to-column (stgit-goal-column))
874 (let ((inhibit-read-only t))
875 (put-text-property start end 'patch-data patch))))
877 (defun stgit-select-file ()
878 (let* ((file (or (stgit-patched-file-at-point)
879 (error "No file at point")))
880 (filename (stgit-file->file file)))
881 (if (string-match "/$" filename)
882 (stgit-expand-directory file)
885 (defun stgit-select ()
886 "With point on a patch, toggle showing files in the patch.
888 With point on a file, open the associated file. Opens the target
889 file for (applied) copies and renames."
892 (case (get-text-property (point) 'entry-type)
894 (stgit-select-patch))
898 (error "No patch or file on line"))))
900 (defun stgit-find-file-other-window ()
901 "Open file at point in other window"
906 (defun stgit-find-file-merge ()
907 "Open file at point and merge it using `smerge-ediff'."
914 "Hide the stgit buffer."
919 (defun stgit-git-status ()
920 "Show status using `git-status'."
923 (unless (fboundp 'git-status)
924 (error "The stgit-git-status command requires git-status"))
925 (let ((dir default-directory))
926 (save-selected-window
930 (defun stgit-goal-column ()
931 "Return goal column for the current line"
932 (case (get-text-property (point) 'entry-type)
937 (defun stgit-next-line (&optional arg)
938 "Move cursor vertically down ARG lines"
942 (move-to-column (stgit-goal-column)))
944 (defun stgit-previous-line (&optional arg)
945 "Move cursor vertically up ARG lines"
949 (move-to-column (stgit-goal-column)))
951 (defun stgit-next-patch (&optional arg)
952 "Move cursor down ARG patches."
955 (ewoc-goto-next stgit-ewoc (or arg 1))
956 (move-to-column goal-column))
958 (defun stgit-previous-patch (&optional arg)
959 "Move cursor up ARG patches."
962 (ewoc-goto-prev stgit-ewoc (or arg 1))
963 (move-to-column goal-column))
965 (defvar stgit-mode-hook nil
966 "Run after `stgit-mode' is setup.")
968 (defvar stgit-mode-map nil
969 "Keymap for StGit major mode.")
971 (unless stgit-mode-map
972 (let ((diff-map (make-sparse-keymap))
973 (toggle-map (make-sparse-keymap)))
974 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
975 '(("b" . stgit-diff-base)
976 ("c" . stgit-diff-combined)
977 ("m" . stgit-find-file-merge)
978 ("o" . stgit-diff-ours)
979 ("r" . stgit-diff-range)
980 ("t" . stgit-diff-theirs)))
981 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
982 '(("n" . stgit-toggle-patch-names)
983 ("t" . stgit-toggle-worktree)
984 ("i" . stgit-toggle-ignored)
985 ("u" . stgit-toggle-unknown)))
986 (setq stgit-mode-map (make-keymap))
987 (suppress-keymap stgit-mode-map)
988 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
989 `((" " . stgit-mark-down)
990 ("m" . stgit-mark-down)
991 ("\d" . stgit-unmark-up)
992 ("u" . stgit-unmark-down)
995 ("\C-p" . stgit-previous-line)
996 ("\C-n" . stgit-next-line)
997 ([up] . stgit-previous-line)
998 ([down] . stgit-next-line)
999 ("p" . stgit-previous-patch)
1000 ("n" . stgit-next-patch)
1001 ("\M-{" . stgit-previous-patch)
1002 ("\M-}" . stgit-next-patch)
1003 ("s" . stgit-git-status)
1004 ("g" . stgit-reload-or-repair)
1005 ("r" . stgit-refresh)
1006 ("\C-c\C-r" . stgit-rename)
1008 ("M" . stgit-move-patches)
1009 ("S" . stgit-squash)
1011 ("c" . stgit-new-and-refresh)
1012 ("\C-c\C-c" . stgit-commit)
1013 ("\C-c\C-u" . stgit-uncommit)
1014 ("U" . stgit-revert)
1015 ("R" . stgit-resolve-file)
1016 ("\r" . stgit-select)
1017 ("+" . stgit-expand)
1018 ("-" . stgit-collapse)
1019 ("o" . stgit-find-file-other-window)
1020 ("i" . stgit-toggle-index)
1021 (">" . stgit-push-next)
1022 ("<" . stgit-pop-next)
1023 ("P" . stgit-push-or-pop)
1026 ("D" . stgit-delete)
1027 ([?\C-/] . stgit-undo)
1028 ("\C-_" . stgit-undo)
1029 ([?\C-c ?\C-/] . stgit-redo)
1030 ("\C-c\C-_" . stgit-redo)
1031 ("B" . stgit-branch)
1032 ("\C-c\C-b" . stgit-rebase)
1036 ("!" . stgit-execute))))
1038 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1039 (and file (eq (stgit-file->status file)
1041 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1042 (easy-menu-define stgit-menu stgit-mode-map
1045 ["Reload" stgit-reload-or-repair
1046 :help "Reload StGit status from disk"]
1047 ["Repair" stgit-repair
1048 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1049 :help "Repair StGit metadata"]
1051 ["Undo" stgit-undo t]
1052 ["Redo" stgit-redo t]
1054 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1056 ["New patch" stgit-new-and-refresh
1057 :help "Create a new patch from changes in index or work tree"
1058 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1059 ["New empty patch" stgit-new
1060 :help "Create a new, empty patch"]
1061 ["(Un)mark patch" stgit-toggle-mark
1062 :label (if (memq (stgit-patch-name-at-point nil t)
1063 stgit-marked-patches)
1064 "Unmark patch" "Mark patch")
1065 :active (stgit-patch-name-at-point nil t)]
1066 ["Expand/collapse patch"
1067 (let ((patches (stgit-patches-marked-or-at-point)))
1068 (if (member-if ,patch-collapsed-p patches)
1069 (stgit-expand patches)
1070 (stgit-collapse patches)))
1071 :label (if (member-if ,patch-collapsed-p
1072 (stgit-patches-marked-or-at-point))
1075 :active (stgit-patches-marked-or-at-point)]
1076 ["Edit patch" stgit-edit
1077 :help "Edit patch comment"
1078 :active (stgit-patch-name-at-point nil t)]
1079 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1080 ["Push/pop patch" stgit-push-or-pop
1081 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1082 (stgit-applied-patchsyms t))
1083 "Pop patches" "Push patches")]
1084 ["Delete patches" stgit-delete
1085 :active (stgit-patches-marked-or-at-point nil t)]
1087 ["Move patches" stgit-move-patches
1088 :active stgit-marked-patches
1089 :help "Move marked patch(es) to point"]
1090 ["Squash patches" stgit-squash
1091 :active (> (length stgit-marked-patches) 1)
1092 :help "Merge marked patches into one"]
1094 ["Refresh top patch" stgit-refresh
1095 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1096 :help "Refresh the top patch with changes in index or work tree"]
1097 ["Refresh this patch" (stgit-refresh t)
1098 :keys "\\[universal-argument] \\[stgit-refresh]"
1099 :help "Refresh marked patch with changes in index or work tree"
1100 :active (and (not (and (stgit-index-empty-p)
1101 (stgit-work-tree-empty-p)))
1102 (stgit-patch-name-at-point nil t))]
1104 ["Find file" stgit-select
1105 :active (eq (get-text-property (point) 'entry-type) 'file)]
1106 ["Open file" stgit-find-file-other-window
1107 :active (eq (get-text-property (point) 'entry-type) 'file)]
1108 ["Toggle file index" stgit-toggle-index
1109 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1110 (memq (stgit-patch-name-at-point) '(:work :index)))
1111 :label (if (eq (stgit-patch-name-at-point) :work)
1112 "Move change to index"
1113 "Move change to work tree")]
1115 ["Show diff" stgit-diff
1116 :active (get-text-property (point) 'entry-type)]
1117 ["Show diff for range of applied patches" stgit-diff-range
1118 :active (= (length stgit-marked-patches) 1)]
1120 :active (stgit-git-index-unmerged-p)
1121 ["Combined diff" stgit-diff-combined
1122 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1123 ["Diff against base" stgit-diff-base
1124 :help "Show diff against the common base"
1125 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1126 ["Diff against ours" stgit-diff-ours
1127 :help "Show diff against our branch"
1128 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1129 ["Diff against theirs" stgit-diff-theirs
1130 :help "Show diff against their branch"
1131 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1133 ["Interactive merge" stgit-find-file-merge
1134 :help "Interactively merge the file"
1135 :active ,at-unmerged-file]
1136 ["Resolve file" stgit-resolve-file
1137 :help "Mark file conflict as resolved"
1138 :active ,at-unmerged-file]
1141 ["Show index & work tree" stgit-toggle-worktree :style toggle
1142 :selected stgit-show-worktree]
1143 ["Show unknown files" stgit-toggle-unknown :style toggle
1144 :selected stgit-show-unknown :active stgit-show-worktree]
1145 ["Show ignored files" stgit-toggle-ignored :style toggle
1146 :selected stgit-show-ignored :active stgit-show-worktree]
1147 ["Show patch names" stgit-toggle-patch-names :style toggle
1148 :selected stgit-show-patch-names]
1150 ["Switch branches" stgit-branch t
1151 :help "Switch to or create another branch"]
1152 ["Rebase branch" stgit-rebase t
1153 :help "Rebase the current branch"]
1156 ;; disable tool bar editing buttons
1157 (put 'stgit-mode 'mode-class 'special)
1159 (defun stgit-mode ()
1160 "Major mode for interacting with StGit.
1162 Start StGit using \\[stgit].
1166 \\[stgit-help] Show this help text
1167 \\[stgit-quit] Hide the StGit buffer
1168 \\[describe-bindings] Show all key bindings
1170 \\[stgit-reload-or-repair] Reload the StGit buffer
1171 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1173 \\[stgit-undo] Undo most recent StGit operation
1174 \\[stgit-redo] Undo recent undo
1176 \\[stgit-git-status] Run `git-status' (if available)
1178 \\[stgit-execute] Run an stg shell command
1181 \\[stgit-previous-line] Move to previous line
1182 \\[stgit-next-line] Move to next line
1183 \\[stgit-previous-patch] Move to previous patch
1184 \\[stgit-next-patch] Move to next patch
1186 \\[stgit-mark-down] Mark patch and move down
1187 \\[stgit-unmark-up] Unmark patch and move up
1188 \\[stgit-unmark-down] Unmark patch and move down
1190 Commands for patches:
1191 \\[stgit-select] Toggle showing changed files in patch
1192 \\[stgit-refresh] Refresh patch with changes in index or work tree
1193 \\[stgit-diff] Show the patch log and diff
1195 \\[stgit-expand] Show changes in marked patches
1196 \\[stgit-collapse] Hide changes in marked patches
1198 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1199 \\[stgit-new] Create a new, empty patch
1201 \\[stgit-rename] Rename patch
1202 \\[stgit-edit] Edit patch description
1203 \\[stgit-delete] Delete patch(es)
1205 \\[stgit-revert] Revert all changes in index or work tree
1206 \\[stgit-toggle-index] Toggle all changes between index and work tree
1208 \\[stgit-push-next] Push next patch onto stack
1209 \\[stgit-pop-next] Pop current patch from stack
1210 \\[stgit-push-or-pop] Push or pop marked patches
1211 \\[stgit-goto] Make patch at point current by popping or pushing
1213 \\[stgit-squash] Squash (meld together) patches
1214 \\[stgit-move-patches] Move marked patches to point
1216 \\[stgit-commit] Commit patch(es)
1217 \\[stgit-uncommit] Uncommit patch(es)
1220 \\[stgit-select] Open the file in this window
1221 \\[stgit-find-file-other-window] Open the file in another window
1222 \\[stgit-diff] Show the file's diff
1224 \\[stgit-toggle-index] Toggle change between index and work tree
1225 \\[stgit-revert] Revert changes to file
1228 \\[stgit-toggle-patch-names] Toggle showing patch names
1229 \\[stgit-toggle-worktree] Toggle showing index and work tree
1230 \\[stgit-toggle-unknown] Toggle showing unknown files
1231 \\[stgit-toggle-ignored] Toggle showing ignored files
1234 \\[stgit-diff] Show diff of patch or file
1235 \\[stgit-diff-range] Show diff for range of patches
1236 \\[stgit-diff-base] Show diff against the merge base
1237 \\[stgit-diff-ours] Show diff against our branch
1238 \\[stgit-diff-theirs] Show diff against their branch
1240 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1241 ignore space changes.
1242 With two prefix arguments (e.g., \\[universal-argument] \
1243 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1245 Commands for merge conflicts:
1246 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1247 \\[stgit-resolve-file] Mark unmerged file as resolved
1249 Commands for branches:
1250 \\[stgit-branch] Switch to or create another branch
1251 \\[stgit-rebase] Rebase the current branch
1253 Customization variables:
1254 `stgit-abbreviate-copies-and-renames'
1255 `stgit-default-show-ignored'
1256 `stgit-default-show-patch-names'
1257 `stgit-default-show-unknown'
1258 `stgit-default-show-worktree'
1259 `stgit-find-copies-harder'
1260 `stgit-show-worktree-mode'
1262 See also \\[customize-group] for the \"stgit\" group."
1263 (kill-all-local-variables)
1264 (buffer-disable-undo)
1265 (setq mode-name "StGit"
1266 major-mode 'stgit-mode
1268 (use-local-map stgit-mode-map)
1269 (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x)))
1270 `((list-buffers-directory . ,default-directory)
1271 (parse-sexp-lookup-properties . t)
1272 (stgit-expanded-patches . (:work :index))
1273 (stgit-index-node . nil)
1274 (stgit-worktree-node . nil)
1275 (stgit-marked-patches . nil)
1276 (stgit-show-ignored . ,stgit-default-show-ignored)
1277 (stgit-show-patch-names . ,stgit-default-show-patch-names)
1278 (stgit-show-unknown . ,stgit-default-show-unknown)
1279 (stgit-show-worktree . ,stgit-default-show-worktree)))
1280 (set-variable 'truncate-lines 't)
1281 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1282 (unless stgit-did-advise
1284 (setq stgit-did-advise t))
1285 (run-hooks 'stgit-mode-hook))
1287 (defun stgit-advise-funlist (funlist)
1288 "Add advice to the functions in FUNLIST so we can refresh the
1289 stgit buffers as the git status of files change."
1292 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1293 (stgit-update-stgit-for-buffer t)))
1297 (defun stgit-advise ()
1298 "Add advice to appropriate (non-stgit) git functions so we can
1299 refresh the stgit buffers as the git status of files change."
1301 (let ((feature (car arg))
1302 (funlist (cdr arg)))
1303 (if (featurep feature)
1304 (stgit-advise-funlist funlist)
1305 (add-to-list 'after-load-alist
1306 `(,feature (stgit-advise-funlist
1307 (quote ,funlist)))))))
1308 ;; lists of (<feature> <function> <function> ...) to be advised
1309 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1310 (git git-add-file git-checkout git-revert-file git-remove-file)
1311 (dired dired-delete-file))))
1313 (defvar stgit-pending-refresh-buffers nil
1314 "Alist of (cons `buffer' `refresh-index') of buffers that need
1315 to be refreshed. `refresh-index' is non-nil if both work tree
1316 and index need to be refreshed.")
1318 (defun stgit-run-pending-refreshs ()
1319 "Run all pending stgit buffer updates as posted by `stgit-post-refresh'."
1320 (let ((buffers stgit-pending-refresh-buffers)
1321 (stgit-inhibit-messages t))
1322 (setq stgit-pending-refresh-buffers nil)
1324 (let* ((elem (car buffers))
1326 (refresh-index (cdr elem)))
1327 (when (buffer-name buffer)
1328 (with-current-buffer buffer
1329 (stgit-refresh-worktree)
1330 (when refresh-index (stgit-refresh-index)))))
1331 (setq buffers (cdr buffers)))))
1333 (defun stgit-post-refresh (buffer refresh-index)
1334 "Update worktree status in BUFFER when Emacs becomes idle. If
1335 REFRESH-INDEX is non-nil, also update the index."
1336 (unless stgit-pending-refresh-buffers
1337 (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs))
1338 (let ((elem (assq buffer stgit-pending-refresh-buffers)))
1340 ;; if buffer is already present, set its refresh-index flag if
1345 (setq stgit-pending-refresh-buffers
1346 (cons (cons buffer refresh-index)
1347 stgit-pending-refresh-buffers)))))
1349 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1350 "When Emacs becomes idle, refresh worktree status in any
1351 `stgit-mode' buffer that shows the status of the current buffer.
1353 If REFRESH-INDEX is non-nil, also update the index."
1354 (let* ((dir (cond ((derived-mode-p 'stgit-status-mode 'dired-mode)
1357 (file-name-directory
1358 (expand-file-name buffer-file-name)))))
1359 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1361 (buffer (and gitdir (stgit-find-buffer gitdir))))
1363 (stgit-post-refresh buffer refresh-index))))
1365 (defun stgit-add-mark (patchsym)
1366 "Mark the patch PATCHSYM."
1367 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1369 (defun stgit-remove-mark (patchsym)
1370 "Unmark the patch PATCHSYM."
1371 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1373 (defun stgit-clear-marks ()
1374 "Unmark all patches."
1375 (setq stgit-marked-patches '()))
1377 (defun stgit-patch-at-point (&optional cause-error)
1378 (get-text-property (point) 'patch-data))
1380 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1381 "Return the patch name on the current line as a symbol.
1382 If CAUSE-ERROR is not nil, signal an error if none found.
1383 If ONLY-PATCHES is not nil, only allow real patches, and not
1384 index or work tree."
1385 (let ((patch (stgit-patch-at-point)))
1388 (memq (stgit-patch->status patch) '(work index))
1391 (stgit-patch->name patch))
1393 (error "No patch on this line")))))
1395 (defun stgit-patched-file-at-point ()
1396 (get-text-property (point) 'file-data))
1398 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1399 "Return the symbols of the marked patches, or the patch on the current line.
1400 If CAUSE-ERRROR is not nil, signal an error if none found.
1401 If ONLY-PATCHES is not nil, do not include index or work tree."
1402 (if stgit-marked-patches
1403 stgit-marked-patches
1404 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1405 (cond (patch (list patch))
1406 (cause-error (error "No patches marked or at this line"))
1409 (defun stgit-goto-patch (patchsym &optional file)
1410 "Move point to the line containing patch PATCHSYM.
1411 If that patch cannot be found, do nothing.
1413 If the patch was found and FILE is not nil, instead move to that
1414 file's line. If FILE cannot be found, stay on the line of
1416 (let ((node (ewoc-nth stgit-ewoc 0)))
1417 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1419 (setq node (ewoc-next stgit-ewoc node)))
1420 (when (and node file)
1421 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1422 (file-node (ewoc-nth file-ewoc 0)))
1423 (while (and file-node
1424 (not (equal (stgit-file->file (ewoc-data file-node))
1426 (setq file-node (ewoc-next file-ewoc file-node)))
1428 (ewoc-goto-node file-ewoc file-node)
1429 (move-to-column (stgit-goal-column))
1432 (ewoc-goto-node stgit-ewoc node)
1433 (move-to-column goal-column))))
1435 (defun stgit-init ()
1439 (stgit-capture-output nil
1443 (defun stgit-toggle-mark ()
1444 "Toggle mark on the patch under point."
1447 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1451 (defun stgit-mark ()
1452 "Mark the patch under point."
1455 (let* ((node (ewoc-locate stgit-ewoc))
1456 (patch (ewoc-data node))
1457 (name (stgit-patch->name patch)))
1458 (when (eq name :work)
1459 (error "Cannot mark the work tree"))
1460 (when (eq name :index)
1461 (error "Cannot mark the index"))
1462 (stgit-add-mark (stgit-patch->name patch))
1463 (let ((column (current-column)))
1464 (ewoc-invalidate stgit-ewoc node)
1465 (move-to-column column))))
1467 (defun stgit-mark-down ()
1468 "Mark the patch under point and move to the next patch."
1473 (defun stgit-unmark ()
1474 "Remove mark from the patch on the current line."
1477 (let* ((node (ewoc-locate stgit-ewoc))
1478 (patch (ewoc-data node)))
1479 (stgit-remove-mark (stgit-patch->name patch))
1480 (let ((column (current-column)))
1481 (ewoc-invalidate stgit-ewoc node)
1482 (move-to-column column))))
1484 (defun stgit-unmark-up ()
1485 "Remove mark from the patch on the previous line."
1488 (stgit-previous-patch)
1491 (defun stgit-unmark-down ()
1492 "Remove mark from the patch on the current line."
1498 (defun stgit-rename (name)
1499 "Rename the patch under point to NAME."
1501 (read-string "Patch name: "
1502 (symbol-name (stgit-patch-name-at-point t t)))))
1504 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1505 (stgit-capture-output nil
1506 (stgit-run "rename" "--" old-patchsym name))
1507 (let ((name-sym (intern name)))
1508 (when (memq old-patchsym stgit-expanded-patches)
1509 (setq stgit-expanded-patches
1510 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1511 (when (memq old-patchsym stgit-marked-patches)
1512 (setq stgit-marked-patches
1513 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1515 (stgit-goto-patch name-sym))))
1517 (defun stgit-reload-or-repair (repair)
1518 "Update the contents of the StGit buffer (`stgit-reload').
1520 With a prefix argument, repair the StGit metadata if the branch
1521 was modified with git commands (`stgit-repair')."
1528 (defun stgit-repair ()
1532 (stgit-capture-output nil
1533 (stgit-run "repair"))
1536 (defun stgit-available-branches (&optional all)
1537 "Returns a list of the names of the available stg branches as strings.
1539 If ALL is not nil, also return non-stgit branches."
1540 (let ((output (with-output-to-string
1541 (stgit-run "branch" "--list")))
1542 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1546 (while (string-match pattern output start)
1547 (setq result (cons (match-string 1 output) result))
1548 (setq start (match-end 0)))
1551 (defun stgit-branch (branch)
1552 "Switch to or create branch BRANCH."
1553 (interactive (list (completing-read "Switch to branch: "
1554 (stgit-available-branches))))
1556 (when (cond ((equal branch (stgit-current-branch))
1557 (error "Branch is already current"))
1558 ((member branch (stgit-available-branches t))
1559 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1561 ((not (string-match stgit-allowed-branch-name-re branch))
1562 (error "Invalid branch name"))
1563 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1564 (let ((branch-point (completing-read
1565 "Branch from (default current branch): "
1566 (stgit-available-branches))))
1567 (stgit-capture-output nil
1569 `("branch" "--create" "--"
1571 ,@(unless (zerop (length branch-point))
1572 (list branch-point)))))
1576 (defun stgit-available-refs (&optional omit-stgit)
1577 "Returns a list of the available git refs.
1578 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1579 (let* ((output (with-output-to-string
1580 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1581 "refs/tags" "refs/heads"
1583 (result (split-string output "\n" t)))
1585 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1586 (substring s (match-end 0))
1589 (delete-if (lambda (s)
1590 (string-match "^refs/heads/.*\\.stgit$" s))
1594 (defun stgit-parent-branch ()
1595 "Return the parent branch of the current stg branch as per
1596 git-config setting branch.<branch>.stgit.parentbranch."
1597 (let ((output (with-output-to-string
1598 (stgit-run-git-silent "config"
1599 (format "branch.%s.stgit.parentbranch"
1600 (stgit-current-branch))))))
1601 (when (string-match ".*" output)
1602 (match-string 0 output))))
1604 (defun stgit-rebase (new-base)
1605 "Rebase the current branch to NEW-BASE.
1607 Interactively, first ask which branch to rebase to. Defaults to
1608 what git-config branch.<branch>.stgit.parentbranch is set to."
1609 (interactive (list (completing-read "Rebase to: "
1610 (stgit-available-refs t)
1612 (stgit-parent-branch))))
1614 (stgit-capture-output nil (stgit-run "rebase" "--" new-base))
1617 (defun stgit-commit (count)
1618 "Run stg commit on COUNT commits.
1619 Interactively, the prefix argument is used as COUNT.
1620 A negative COUNT will uncommit instead."
1624 (stgit-uncommit (- count))
1625 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1628 (defun stgit-uncommit (count)
1629 "Run stg uncommit on COUNT commits.
1630 Interactively, the prefix argument is used as COUNT.
1631 A negative COUNT will commit instead."
1635 (stgit-commit (- count))
1636 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1639 (defun stgit-neighbour-file ()
1640 "Return the file name of the next file after point, or the
1641 previous file if point is at the last file within a patch."
1642 (let ((old-point (point))
1644 (and (zerop (forward-line 1))
1645 (let ((f (stgit-patched-file-at-point)))
1646 (and f (setq neighbour-file (stgit-file->file f)))))
1647 (goto-char old-point)
1648 (unless neighbour-file
1649 (and (zerop (forward-line -1))
1650 (let ((f (stgit-patched-file-at-point)))
1651 (and f (setq neighbour-file (stgit-file->file f)))))
1652 (goto-char old-point))
1655 (defun stgit-revert-file ()
1656 "Revert the file at point, which must be in the index or the
1660 (let* ((patched-file (or (stgit-patched-file-at-point)
1661 (error "No file on the current line")))
1662 (patch-name (stgit-patch-name-at-point))
1663 (file-status (stgit-file->status patched-file))
1664 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1665 (stgit-file->cr-to patched-file))
1666 ((eq file-status 'add)
1667 (stgit-file->file patched-file))))
1668 (co-file (cond ((eq file-status 'rename)
1669 (stgit-file->cr-from patched-file))
1670 ((not (memq file-status '(copy add)))
1671 (stgit-file->file patched-file))))
1672 (next-file (stgit-neighbour-file)))
1674 (unless (memq patch-name '(:work :index))
1675 (error "No index or working tree file on this line"))
1677 (when (eq file-status 'ignore)
1678 (error "Cannot revert ignored files"))
1680 (when (eq file-status 'unknown)
1681 (error "Cannot revert unknown files"))
1683 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1684 (when (yes-or-no-p (format "Revert %d file%s? "
1686 (if (= nfiles 1) "" "s")))
1687 (stgit-capture-output nil
1689 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1691 (stgit-run-git "checkout" "HEAD" co-file)))
1693 (stgit-goto-patch patch-name next-file)))))
1695 (defun stgit-revert ()
1696 "Revert the change at point, which must be the index, the work
1697 tree, or a single change in either."
1700 (let ((patched-file (stgit-patched-file-at-point)))
1703 (let* ((patch-name (or (stgit-patch-name-at-point)
1704 (error "No patch or file at point")))
1705 (patch-desc (case patch-name
1708 (t (error (substitute-command-keys
1709 "Use \\[stgit-delete] to delete a patch"))))))
1710 (when (if (eq patch-name :work)
1711 (stgit-work-tree-empty-p)
1712 (stgit-index-empty-p))
1713 (error (format "There are no changes in the %s to revert"
1715 (and (eq patch-name :index)
1716 (not (stgit-work-tree-empty-p))
1717 (error "Cannot revert index as work tree contains unstaged changes"))
1719 (when (yes-or-no-p (format "Revert all changes in the %s? "
1721 (if (eq patch-name :index)
1722 (stgit-run-git-silent "reset" "--hard" "-q")
1723 (stgit-run-git-silent "checkout" "--" "."))
1724 (stgit-refresh-index)
1725 (stgit-refresh-worktree)
1726 (stgit-goto-patch patch-name))))))
1728 (defun stgit-resolve-file ()
1729 "Resolve conflict in the file at point."
1732 (let* ((patched-file (stgit-patched-file-at-point))
1733 (patch (stgit-patch-at-point))
1734 (patch-name (and patch (stgit-patch->name patch)))
1735 (status (and patched-file (stgit-file->status patched-file))))
1737 (unless (memq patch-name '(:work :index))
1738 (error "No index or working tree file on this line"))
1740 (unless (eq status 'unmerged)
1741 (error "No conflict to resolve at the current line"))
1743 (stgit-capture-output nil
1744 (stgit-move-change-to-index (stgit-file->file patched-file)))
1748 (defun stgit-push-or-pop-patches (do-push npatches)
1749 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1750 NPATCHES patches, or all patches if NPATCHES is t."
1752 (stgit-capture-output nil
1754 (if do-push "push" "pop")
1757 (list "-n" npatches))))
1759 (stgit-refresh-git-status))
1761 (defun stgit-push-next (npatches)
1762 "Push the first unapplied patch.
1763 With numeric prefix argument, push that many patches."
1765 (stgit-push-or-pop-patches t npatches))
1767 (defun stgit-pop-next (npatches)
1768 "Pop the topmost applied patch.
1769 With numeric prefix argument, pop that many patches.
1771 If NPATCHES is t, pop all patches."
1773 (stgit-push-or-pop-patches nil npatches))
1775 (defun stgit-applied-patches (&optional only-patches)
1776 "Return a list of the applied patches.
1778 If ONLY-PATCHES is not nil, exclude index and work tree."
1779 (let ((states (if only-patches
1781 '(applied top index work)))
1783 (ewoc-map (lambda (patch)
1784 (when (memq (stgit-patch->status patch) states)
1785 (setq result (cons patch result)))
1790 (defun stgit-applied-patchsyms (&optional only-patches)
1791 "Return a list of the symbols of the applied patches.
1793 If ONLY-PATCHES is not nil, exclude index and work tree."
1794 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1796 (defun stgit-push-or-pop ()
1797 "Push or pop the marked patches."
1800 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1801 (applied-syms (stgit-applied-patchsyms t))
1802 (unapplied (set-difference patchsyms applied-syms)))
1803 (stgit-capture-output nil
1805 (if unapplied "push" "pop")
1807 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1810 (defun stgit-goto-target ()
1811 "Return the goto target a point; either a patchsym, :top,
1813 (let ((patchsym (stgit-patch-name-at-point)))
1814 (cond ((memq patchsym '(:work :index)) nil)
1816 ((not (next-single-property-change (point) 'patch-data))
1818 ((not (previous-single-property-change (point) 'patch-data))
1821 (defun stgit-goto ()
1822 "Go to the patch on the current line.
1824 Push or pop patches to make this patch topmost. Push or pop all
1825 patches if used on a line after or before all patches."
1828 (let ((patchsym (stgit-goto-target)))
1830 (error "No patch to go to on this line"))
1832 (:top (stgit-push-or-pop-patches t t))
1833 (:bottom (stgit-push-or-pop-patches nil t))
1834 (t (stgit-capture-output nil
1835 (stgit-run "goto" "--" patchsym))
1838 (defun stgit-id (patchsym)
1839 "Return the git commit id for PATCHSYM.
1840 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1841 (if (keywordp patchsym)
1843 (let ((result (with-output-to-string
1844 (stgit-run-silent "id" "--" patchsym))))
1845 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1846 (error "Cannot find commit id for %s" patchsym))
1847 (match-string 1 result))))
1849 (defun stgit-whitespace-diff-arg (arg)
1851 (cond ((> arg 4) "--ignore-all-space")
1852 ((> arg 1) "--ignore-space-change"))))
1854 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1855 "Show the patch on the current line.
1857 UNMERGED-STAGE is the argument to `git-diff' that that selects
1858 which stage to diff against in the case of unmerged files."
1859 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
1860 (patch-name (stgit-patch-name-at-point t)))
1861 (stgit-capture-output "*StGit patch*"
1862 (case (get-text-property (point) 'entry-type)
1864 (let* ((patched-file (stgit-patched-file-at-point))
1865 (patch-id (let ((id (stgit-id patch-name)))
1866 (if (and (eq id :index)
1867 (eq (stgit-file->status patched-file)
1871 (args (append (and space-arg (list space-arg))
1872 (and (stgit-file->cr-from patched-file)
1873 (list (stgit-find-copies-harder-diff-arg)))
1874 (cond ((eq patch-id :index)
1876 ((eq patch-id :work)
1877 (list unmerged-stage))
1879 (list (concat patch-id "^") patch-id)))
1881 (if (stgit-file->copy-or-rename patched-file)
1882 (list (stgit-file->cr-from patched-file)
1883 (stgit-file->cr-to patched-file))
1884 (list (stgit-file->file patched-file))))))
1885 (apply 'stgit-run-git "diff" args)))
1887 (let* ((patch-id (stgit-id patch-name)))
1888 (if (or (eq patch-id :index) (eq patch-id :work))
1889 (apply 'stgit-run-git "diff"
1890 (stgit-find-copies-harder-diff-arg)
1891 (append (and space-arg (list space-arg))
1892 (if (eq patch-id :index)
1894 (list unmerged-stage))))
1895 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1896 (and space-arg (list "-O" space-arg))
1898 (list (stgit-patch-name-at-point)))))
1899 (apply 'stgit-run args)))))
1901 (error "No patch or file at point")))
1902 (with-current-buffer standard-output
1903 (goto-char (point-min))
1906 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1907 `(defun ,name (&optional ignore-whitespace)
1908 ,(format "Show the patch on the current line.
1910 %sWith a prefix argument, ignore whitespace. With a prefix argument
1911 greater than four (e.g., \\[universal-argument] \
1912 \\[universal-argument] \\[%s]), ignore all whitespace."
1914 (format "For unmerged files, %s.\n\n" unmerged-action)
1919 (stgit-show-patch ,diff-arg ignore-whitespace)))
1921 (stgit-define-diff stgit-diff
1923 (stgit-define-diff stgit-diff-ours
1925 "diff against our branch")
1926 (stgit-define-diff stgit-diff-theirs
1928 "diff against their branch")
1929 (stgit-define-diff stgit-diff-base
1931 "diff against the merge base")
1932 (stgit-define-diff stgit-diff-combined
1934 "show a combined diff")
1936 (defun stgit-diff-range (&optional ignore-whitespace)
1937 "Show diff for the range of patches between point and the marked patch.
1939 With a prefix argument, ignore whitespace. With a prefix argument
1940 greater than four (e.g., \\[universal-argument] \
1941 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
1944 (unless (= (length stgit-marked-patches) 1)
1945 (error "Need exactly one patch marked"))
1946 (let* ((patches (stgit-sort-patches (cons (stgit-patch-name-at-point t t)
1947 stgit-marked-patches)
1949 (first-patch (car patches))
1950 (second-patch (if (cdr patches) (cadr patches) first-patch))
1951 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
1952 (applied (stgit-applied-patchsyms t)))
1953 (unless (and (memq first-patch applied) (memq second-patch applied))
1954 (error "Can only show diff range for applied patches"))
1955 (stgit-capture-output (format "*StGit diff %s..%s*"
1956 first-patch second-patch)
1957 (apply 'stgit-run-git (append '("diff" "--patch-with-stat")
1958 (and whitespace-arg (list whitespace-arg))
1959 (list (format "%s^" (stgit-id first-patch))
1960 (stgit-id second-patch))))
1961 (with-current-buffer standard-output
1962 (goto-char (point-min))
1965 (defun stgit-move-change-to-index (file &optional force)
1966 "Copies the work tree state of FILE to index, using git add or git rm.
1968 If FORCE is not nil, use --force."
1969 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1970 '("add") '("rm" "-q"))))
1971 (stgit-capture-output "*git output*"
1972 (apply 'stgit-run-git (append op (and force '("--force"))
1973 '("--") (list file))))))
1975 (defun stgit-remove-change-from-index (file)
1976 "Unstages the change in FILE from the index"
1977 (stgit-capture-output "*git output*"
1978 (stgit-run-git "reset" "-q" "--" file)))
1980 (defun stgit-git-index-unmerged-p ()
1982 (with-output-to-string
1983 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1985 "--quiet" "HEAD")))))
1988 (defun stgit-file-toggle-index ()
1989 "Move modified file in or out of the index.
1991 Leaves the point where it is, but moves the mark to where the
1992 file ended up. You can then jump to the file with \
1993 \\[exchange-point-and-mark]."
1996 (let* ((patched-file (or (stgit-patched-file-at-point)
1997 (error "No file on the current line")))
1998 (patched-status (stgit-file->status patched-file)))
1999 (when (eq patched-status 'unmerged)
2000 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
2001 (let* ((patch (stgit-patch-at-point))
2002 (patch-name (stgit-patch->name patch))
2003 (mark-file (if (eq patched-status 'rename)
2004 (stgit-file->cr-to patched-file)
2005 (stgit-file->file patched-file)))
2006 (point-file (if (eq patched-status 'rename)
2007 (stgit-file->cr-from patched-file)
2008 (stgit-neighbour-file))))
2010 (cond ((eq patch-name :work)
2011 (stgit-move-change-to-index (stgit-file->file patched-file)
2012 (eq patched-status 'ignore)))
2013 ((eq patch-name :index)
2014 (stgit-remove-change-from-index (stgit-file->file patched-file)))
2016 (error "Can only move files between working tree and index")))
2017 (stgit-refresh-worktree)
2018 (stgit-refresh-index)
2019 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
2021 (stgit-goto-patch patch-name point-file))))
2023 (defun stgit-toggle-index ()
2024 "Move change in or out of the index.
2026 Works on index and work tree, as well as files in either.
2028 Leaves the point where it is, but moves the mark to where the
2029 file ended up. You can then jump to the file with \
2030 \\[exchange-point-and-mark]."
2033 (if (stgit-patched-file-at-point)
2034 (stgit-file-toggle-index)
2035 (let ((patch-name (stgit-patch-name-at-point)))
2036 (unless (memq patch-name '(:index :work))
2037 (error "Can only move changes between working tree and index"))
2038 (when (stgit-git-index-unmerged-p)
2039 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
2040 (if (if (eq patch-name :index)
2041 (stgit-index-empty-p)
2042 (stgit-work-tree-empty-p))
2043 (message "No changes to be moved")
2044 (stgit-capture-output nil
2045 (if (eq patch-name :work)
2046 (stgit-run-git "add" "--update")
2047 (stgit-run-git "reset" "--mixed" "-q")))
2048 (stgit-refresh-worktree)
2049 (stgit-refresh-index))
2050 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
2052 (defun stgit-edit ()
2053 "Edit the patch on the current line."
2056 (let ((patchsym (stgit-patch-name-at-point t t))
2057 (edit-buf (get-buffer-create "*StGit edit*"))
2058 (dir default-directory))
2059 (log-edit 'stgit-confirm-edit t nil edit-buf)
2060 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
2061 (setq default-directory dir)
2062 (let ((standard-output edit-buf))
2064 (stgit-run-silent "edit" "--save-template=-" "--" patchsym)))))
2066 (defun stgit-confirm-edit ()
2068 (let ((file (make-temp-file "stgit-edit-")))
2069 (write-region (point-min) (point-max) file)
2070 (stgit-capture-output nil
2071 (stgit-run "edit" "-f" file "--" stgit-edit-patchsym))
2072 (with-current-buffer log-edit-parent-buffer
2075 (defun stgit-new (add-sign &optional refresh)
2076 "Create a new patch.
2077 With a prefix argument, include a \"Signed-off-by:\" line at the
2081 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2082 (dir default-directory))
2083 (log-edit 'stgit-confirm-new t nil edit-buf)
2084 (setq default-directory dir)
2085 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2088 (let ((standard-output (current-buffer)))
2089 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2091 (defun stgit-confirm-new ()
2093 (let ((file (make-temp-file "stgit-edit-"))
2094 (refresh stgit-refresh-after-new))
2095 (write-region (point-min) (point-max) file)
2096 (stgit-capture-output nil
2097 (stgit-run "new" "-f" file))
2098 (with-current-buffer log-edit-parent-buffer
2103 (defun stgit-new-and-refresh (add-sign)
2104 "Create a new patch and refresh it with the current changes.
2106 With a prefix argument, include a \"Signed-off-by:\" line at the
2109 This works just like running `stgit-new' followed by `stgit-refresh'."
2112 (stgit-new add-sign t))
2114 (defun stgit-create-patch-name (description)
2115 "Create a patch name from a long description"
2117 (while (> (length description) 0)
2118 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2119 (setq patch (downcase (concat patch
2120 (match-string 0 description))))
2121 (setq description (substring description (match-end 0))))
2122 ((string-match "\\` +" description)
2123 (setq patch (concat patch "-"))
2124 (setq description (substring description (match-end 0))))
2125 ((string-match "\\`[^a-zA-Z_-]+" description)
2126 (setq description (substring description (match-end 0))))))
2127 (cond ((= (length patch) 0)
2129 ((> (length patch) 20)
2130 (substring patch 0 20))
2133 (defun stgit-delete (patchsyms &optional spill-p)
2134 "Delete the patches in PATCHSYMS.
2135 Interactively, delete the marked patches, or the patch at point.
2137 With a prefix argument, or SPILL-P, spill the patch contents to
2138 the work tree and index."
2139 (interactive (list (stgit-patches-marked-or-at-point t t)
2140 current-prefix-arg))
2143 (error "No patches to delete"))
2144 (when (memq :index patchsyms)
2145 (error "Cannot delete the index"))
2146 (when (memq :work patchsyms)
2147 (error "Cannot delete the work tree"))
2149 (let ((npatches (length patchsyms)))
2150 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2152 (if (= 1 npatches) "" "es")
2154 " (spilling contents to index)"
2156 (let ((args (append (when spill-p '("--spill"))
2159 (stgit-capture-output nil
2160 (apply 'stgit-run "delete" args))
2163 (defun stgit-move-patches-target ()
2164 "Return the patchsym indicating a target patch for
2165 `stgit-move-patches'.
2167 This is either the first unmarked patch at or after point, or one
2168 of :top and :bottom if the point is after or before the applied
2174 (let ((patchsym (stgit-patch-name-at-point)))
2175 (cond ((memq patchsym '(:work :index)) (setq result :top))
2176 (patchsym (if (memq patchsym stgit-marked-patches)
2178 (setq result patchsym)))
2179 ((re-search-backward "^>" nil t) (setq result :top))
2180 (t (setq result :bottom)))))
2183 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2184 "Returns the list of patches in PATCHSYMS sorted according to
2185 their position in the patch series, bottommost first.
2187 PATCHSYMS must not contain duplicate entries, unless
2188 ALLOW-DUPLICATES is not nil."
2189 (let (sorted-patchsyms
2190 (series (with-output-to-string
2191 (with-current-buffer standard-output
2192 (stgit-run-silent "series" "--noprefix"))))
2194 (while (string-match "^\\(.+\\)" series start)
2195 (let ((patchsym (intern (match-string 1 series))))
2196 (when (memq patchsym patchsyms)
2197 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2198 (setq start (match-end 0)))
2199 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2201 (unless allow-duplicates
2202 (unless (= (length patchsyms) (length sorted-patchsyms))
2203 (error "Internal error")))
2207 (defun stgit-move-patches (patchsyms target-patch)
2208 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2209 If TARGET-PATCH is :bottom or :top, move the patches to the
2210 bottom or top of the stack, respectively.
2212 Interactively, move the marked patches to where the point is."
2213 (interactive (list stgit-marked-patches
2214 (stgit-move-patches-target)))
2217 (error "Need at least one patch to move"))
2219 (unless target-patch
2220 (error "Point not at a patch"))
2222 ;; need to have patchsyms sorted by position in the stack
2223 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2224 (stgit-capture-output nil
2225 (if (eq target-patch :top)
2226 (apply 'stgit-run "float" "--" sorted-patchsyms)
2229 (append (unless (eq target-patch :bottom)
2230 (list "--to" target-patch))
2232 sorted-patchsyms)))))
2235 (defun stgit-squash (patchsyms)
2236 "Squash the patches in PATCHSYMS.
2237 Interactively, squash the marked patches.
2239 Unless there are any conflicts, the patches will be merged into
2240 one patch, which will occupy the same spot in the series as the
2241 deepest patch had before the squash."
2242 (interactive (list stgit-marked-patches))
2244 (when (< (length patchsyms) 2)
2245 (error "Need at least two patches to squash"))
2246 (let ((stgit-buffer (current-buffer))
2247 (edit-buf (get-buffer-create "*StGit edit*"))
2248 (dir default-directory)
2249 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2250 (log-edit 'stgit-confirm-squash t nil edit-buf)
2251 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2252 (setq default-directory dir)
2253 (let ((result (let ((standard-output edit-buf))
2255 (apply 'stgit-run-silent "squash"
2256 "--save-template=-" "--" sorted-patchsyms)))))
2258 ;; stg squash may have reordered the patches or caused conflicts
2259 (with-current-buffer stgit-buffer
2262 (unless (eq 0 result)
2264 (rename-buffer "*StGit error*")
2265 (resize-temp-buffer-window)
2266 (switch-to-buffer-other-window stgit-buffer)
2267 (error "stg squash failed")))))
2269 (defun stgit-confirm-squash ()
2271 (let ((file (make-temp-file "stgit-edit-")))
2272 (write-region (point-min) (point-max) file)
2273 (stgit-capture-output nil
2274 (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms))
2275 (with-current-buffer log-edit-parent-buffer
2277 ;; Go to first marked patch and stay there
2278 (goto-char (point-min))
2279 (re-search-forward (concat "^[>+-]\\*") nil t)
2280 (move-to-column goal-column)
2281 (let ((pos (point)))
2285 (defun stgit-help ()
2286 "Display help for the StGit mode."
2288 (describe-function 'stgit-mode))
2290 (defun stgit-execute-process-sentinel (process sentinel)
2291 (let (old-sentinel stgit-buf)
2292 (with-current-buffer (process-buffer process)
2293 (setq old-sentinel old-process-sentinel
2294 stgit-buf stgit-buffer))
2295 (and (memq (process-status process) '(exit signal))
2296 (buffer-live-p stgit-buf)
2297 (with-current-buffer stgit-buf
2299 (funcall old-sentinel process sentinel)))
2301 (defun stgit-execute-process-filter (process output)
2302 (with-current-buffer (process-buffer process)
2303 (let* ((old-point (point))
2304 (pmark (process-mark process))
2305 (insert-at (marker-position pmark))
2306 (at-pmark (= insert-at old-point)))
2307 (goto-char insert-at)
2308 (insert-before-markers output)
2309 (comint-carriage-motion insert-at (point))
2310 (set-marker pmark (point))
2312 (goto-char old-point)))))
2314 (defun stgit-execute ()
2315 "Prompt for an stg command to execute in a shell.
2317 The names of any marked patches or the patch at point are
2318 inserted in the command to be executed.
2320 If the command ends in an ampersand, run it asynchronously.
2322 When the command has finished, reload the stgit buffer."
2325 (let* ((patches (stgit-patches-marked-or-at-point nil t))
2326 (patch-names (mapcar 'symbol-name patches))
2327 (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names))
2328 (defaultcmd (if patches
2331 (mapconcat 'identity patch-names " "))
2333 (cmd (read-from-minibuffer "Shell command: " (cons defaultcmd 5)
2334 nil nil 'shell-command-history))
2335 (async (string-match "&[ \t]*\\'" cmd))
2336 (buffer (get-buffer-create
2338 "*Async Shell Command*"
2339 "*Shell Command Output*"))))
2340 ;; cannot use minibuffer as stgit-reload would overwrite it; if we
2341 ;; show the buffer, shell-command will not use the minibuffer
2342 (display-buffer buffer)
2345 (let ((old-buffer (current-buffer)))
2346 (with-current-buffer buffer
2347 (let ((process (get-buffer-process buffer)))
2348 (set (make-local-variable 'old-process-sentinel)
2349 (process-sentinel process))
2350 (set (make-local-variable 'stgit-buffer)
2352 (set-process-filter process 'stgit-execute-process-filter)
2353 (set-process-sentinel process 'stgit-execute-process-sentinel))))
2354 (with-current-buffer buffer
2355 (comint-carriage-motion (point-min) (point-max)))
2356 (shrink-window-if-larger-than-buffer (get-buffer-window buffer))
2359 (defun stgit-undo-or-redo (redo hard)
2360 "Run stg undo or, if REDO is non-nil, stg redo.
2362 If HARD is non-nil, use the --hard flag."
2364 (let ((cmd (if redo "redo" "undo")))
2365 (stgit-capture-output nil
2367 (when (or (and (stgit-index-empty-p)
2368 (stgit-work-tree-empty-p))
2369 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2371 (stgit-run cmd "--hard"))
2375 (defun stgit-undo (&optional arg)
2377 With prefix argument, run it with the --hard flag.
2379 See also `stgit-redo'."
2381 (stgit-undo-or-redo nil arg))
2383 (defun stgit-redo (&optional arg)
2385 With prefix argument, run it with the --hard flag.
2387 See also `stgit-undo'."
2389 (stgit-undo-or-redo t arg))
2391 (defun stgit-refresh (&optional arg)
2393 If the index contains any changes, only refresh from index.
2395 With prefix argument, refresh the marked patch or the patch under point."
2398 (let ((patchargs (if arg
2399 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2400 (when (> (length patches) 1)
2401 (error "Too many patches marked"))
2402 (cons "-p" patches))
2404 (unless (stgit-index-empty-p)
2405 (setq patchargs (cons "--index" patchargs)))
2406 (stgit-capture-output nil
2407 (apply 'stgit-run "refresh" patchargs))
2408 (stgit-refresh-git-status))
2411 (defvar stgit-show-worktree nil
2412 "If nil, inhibit showing work tree and index in the stgit buffer.
2414 See also `stgit-show-worktree-mode'.")
2416 (defvar stgit-show-ignored nil
2417 "If nil, inhibit showing files ignored by git.")
2419 (defvar stgit-show-unknown nil
2420 "If nil, inhibit showing files not registered with git.")
2422 (defvar stgit-show-patch-names t
2423 "If nil, inhibit showing patch names.")
2425 (defun stgit-toggle-worktree (&optional arg)
2426 "Toggle the visibility of the work tree.
2427 With ARG, show the work tree if ARG is positive.
2429 Its initial setting is controlled by `stgit-default-show-worktree'.
2431 `stgit-show-worktree-mode' controls where on screen the index and
2432 work tree will show up."
2435 (setq stgit-show-worktree
2438 (not stgit-show-worktree)))
2441 (defun stgit-toggle-ignored (&optional arg)
2442 "Toggle the visibility of files ignored by git in the work
2443 tree. With ARG, show these files if ARG is positive.
2445 Its initial setting is controlled by `stgit-default-show-ignored'.
2447 Use \\[stgit-toggle-worktree] to show the work tree."
2450 (setq stgit-show-ignored
2453 (not stgit-show-ignored)))
2456 (defun stgit-toggle-unknown (&optional arg)
2457 "Toggle the visibility of files not registered with git in the
2458 work tree. With ARG, show these files if ARG is positive.
2460 Its initial setting is controlled by `stgit-default-show-unknown'.
2462 Use \\[stgit-toggle-worktree] to show the work tree."
2465 (setq stgit-show-unknown
2468 (not stgit-show-unknown)))
2471 (defun stgit-toggle-patch-names (&optional arg)
2472 "Toggle the visibility of patch names. With ARG, show patch names
2475 The initial setting is controlled by `stgit-default-show-patch-names'."
2478 (setq stgit-show-patch-names
2481 (not stgit-show-patch-names)))