1 ;; stgit.el: An emacs mode for StGit
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
10 ;; To start: `M-x stgit'
12 (when (< emacs-major-version 22)
13 (error "Emacs older than 22 is not supported by stgit.el"))
19 (require 'format-spec)
21 (defun stgit-set-default (symbol value)
22 "Set default value of SYMBOL to VALUE using `set-default' and
23 reload all StGit buffers."
24 (set-default symbol value)
25 (dolist (buf (buffer-list))
26 (with-current-buffer buf
27 (when (eq major-mode 'stgit-mode)
31 "A user interface for the StGit patch maintenance tool."
33 :link '(function-link stgit)
34 :link '(url-link "http://www.procode.org/stgit/"))
36 (defcustom stgit-abbreviate-copies-and-renames t
37 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
38 instead of \"dir/old/file -> dir/new/file\"."
41 :set 'stgit-set-default)
43 (defcustom stgit-default-show-worktree t
44 "Set to non-nil to by default show the working tree in a new stgit buffer.
46 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
49 :link '(variable-link stgit-show-worktree))
51 (defcustom stgit-find-copies-harder nil
52 "Try harder to find copied files when listing patches.
54 When not nil, runs git diff-tree with the --find-copies-harder
55 flag, which reduces performance."
58 :set 'stgit-set-default)
60 (defcustom stgit-show-worktree-mode 'center
61 "This variable controls where the \"Index\" and \"Work tree\"
62 will be shown on in the buffer.
64 It can be set to 'top (above all patches), 'center (show between
65 applied and unapplied patches), and 'bottom (below all patches)."
66 :type '(radio (const :tag "above all patches (top)" top)
67 (const :tag "between applied and unapplied patches (center)"
69 (const :tag "below all patches (bottom)" bottom))
71 :link '(variable-link stgit-show-worktree)
72 :set 'stgit-set-default)
74 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
75 "The format string used to format patch lines.
76 The format string is passed to `format-spec' and the following
77 format characters are recognized:
79 %s - A '+', '-', '>' or space, depending on whether the patch is
80 applied, unapplied, top, or something else.
82 %m - An asterisk if the patch is marked, and a space otherwise.
86 %e - The string \"(empty) \" if the patch is empty.
88 %d - The short patch description.
90 %D - The short patch description, or the patch name.
92 When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
93 variable is used instead."
96 :set 'stgit-set-default)
98 (defcustom stgit-noname-patch-line-format "%s%m%e%D"
99 "The alternate format string used to format patch lines.
100 It has the same semantics as `stgit-patch-line-format', and the
101 display can be toggled between the two formats using
102 \\<stgit-mode-map>>\\[stgit-toggle-patch-names].
104 The alternate form is used when the patch name is hidden."
107 :set 'stgit-set-default)
109 (defcustom stgit-default-show-patch-names t
110 "If non-nil, default to showing patch names in a new stgit buffer.
112 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] to toggle the
113 this setting in an already-started StGit buffer."
116 :link '(variable-link stgit-show-patch-names))
118 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
119 "The format string used to format file lines.
120 The format string is passed to `format-spec' and the following
121 format characters are recognized:
123 %s - A string describing the status of the file.
125 %m - Mode change information
129 %c - A description of file changes."
132 :set 'stgit-set-default)
134 (defface stgit-branch-name-face
136 "The face used for the StGit branch name"
139 (defface stgit-top-patch-face
140 '((((background dark)) (:weight bold :foreground "yellow"))
141 (((background light)) (:weight bold :foreground "purple"))
143 "The face used for the top patch names"
146 (defface stgit-applied-patch-face
147 '((((background dark)) (:foreground "light yellow"))
148 (((background light)) (:foreground "purple"))
150 "The face used for applied patch names"
153 (defface stgit-unapplied-patch-face
154 '((((background dark)) (:foreground "gray80"))
155 (((background light)) (:foreground "orchid"))
157 "The face used for unapplied patch names"
160 (defface stgit-description-face
161 '((((background dark)) (:foreground "tan"))
162 (((background light)) (:foreground "dark red")))
163 "The face used for StGit descriptions"
166 (defface stgit-index-work-tree-title-face
167 '((((supports :slant italic)) :slant italic)
169 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
172 (defface stgit-unmerged-file-face
173 '((((class color) (background light)) (:foreground "red" :bold t))
174 (((class color) (background dark)) (:foreground "red" :bold t)))
175 "StGit mode face used for unmerged file status"
178 (defface stgit-unknown-file-face
179 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
180 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
181 "StGit mode face used for unknown file status"
184 (defface stgit-ignored-file-face
185 '((((class color) (background light)) (:foreground "grey60"))
186 (((class color) (background dark)) (:foreground "grey40")))
187 "StGit mode face used for ignored files")
189 (defface stgit-file-permission-face
190 '((((class color) (background light)) (:foreground "green" :bold t))
191 (((class color) (background dark)) (:foreground "green" :bold t)))
192 "StGit mode face used for permission changes."
195 (defface stgit-modified-file-face
196 '((((class color) (background light)) (:foreground "purple"))
197 (((class color) (background dark)) (:foreground "salmon")))
198 "StGit mode face used for modified file status"
202 "Manage StGit patches for the tree in DIR.
204 See `stgit-mode' for commands available."
205 (interactive "DDirectory: \n")
206 (switch-to-stgit-buffer (git-get-top-dir dir))
209 (defun stgit-assert-mode ()
210 "Signal an error if not in an StGit buffer."
211 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
213 (unless (fboundp 'git-get-top-dir)
214 (defun git-get-top-dir (dir)
215 "Retrieve the top-level directory of a git tree."
216 (let ((cdup (with-output-to-string
217 (with-current-buffer standard-output
219 (unless (eq 0 (call-process "git" nil t nil
220 "rev-parse" "--show-cdup"))
221 (error "Cannot find top-level git tree for %s" dir))))))
222 (expand-file-name (concat (file-name-as-directory dir)
223 (car (split-string cdup "\n")))))))
225 (defun stgit-refresh-git-status (&optional dir)
226 "If it exists, refresh the `git-status' buffer belonging to
227 directory DIR or `default-directory'"
228 (when (and (fboundp 'git-find-status-buffer)
229 (fboundp 'git-refresh-status))
230 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
231 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
232 (when git-status-buffer
233 (with-current-buffer git-status-buffer
234 (git-refresh-status))))))
236 (defun stgit-find-buffer (dir)
237 "Return the buffer displaying StGit patches for DIR, or nil if none."
238 (setq dir (file-name-as-directory dir))
239 (let ((buffers (buffer-list)))
241 (not (with-current-buffer (car buffers)
242 (and (eq major-mode 'stgit-mode)
243 (string= default-directory dir)))))
244 (setq buffers (cdr buffers)))
245 (and buffers (car buffers))))
247 (defun switch-to-stgit-buffer (dir)
248 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
249 (setq dir (file-name-as-directory dir))
250 (let ((buffer (stgit-find-buffer dir)))
251 (switch-to-buffer (or buffer
252 (create-stgit-buffer dir)))))
254 (defstruct (stgit-patch
255 (:conc-name stgit-patch->))
256 status name desc empty files-ewoc)
258 (defun stgit-patch-display-name (patch)
259 (let ((name (stgit-patch->name patch)))
263 (t (symbol-name name)))))
265 (defun stgit-insert-without-trailing-whitespace (text)
266 "Insert TEXT in buffer using `insert', without trailing whitespace.
267 A newline is appended."
268 (unless (string-match "\\(.*?\\) *$" text)
270 (insert (match-string 1 text) ?\n))
272 (defun stgit-patch-pp (patch)
273 (let* ((status (stgit-patch->status patch))
275 (name (stgit-patch->name patch))
276 (face (cdr (assq status stgit-patch-status-face-alist)))
277 (fmt (if stgit-show-patch-names
278 stgit-patch-line-format
279 stgit-noname-patch-line-format))
280 (spec (format-spec-make
286 ?m (if (memq name stgit-marked-patches)
288 ?n (propertize (stgit-patch-display-name patch)
290 'syntax-table (string-to-syntax "w"))
291 ?e (if (stgit-patch->empty patch) "(empty) " "")
292 ?d (propertize (or (stgit-patch->desc patch) "")
293 'face 'stgit-description-face)
294 ?D (propertize (let ((desc (stgit-patch->desc patch)))
295 (if (zerop (length desc))
296 (stgit-patch-display-name patch)
299 (text (format-spec fmt spec)))
301 (stgit-insert-without-trailing-whitespace text)
302 (put-text-property start (point) 'entry-type 'patch)
303 (when (memq name stgit-expanded-patches)
304 (stgit-insert-patch-files patch))
305 (put-text-property start (point) 'patch-data patch)))
307 (defun create-stgit-buffer (dir)
308 "Create a buffer for showing StGit patches.
309 Argument DIR is the repository path."
310 (let ((buf (create-file-buffer (concat dir "*stgit*")))
311 (inhibit-read-only t))
312 (with-current-buffer buf
313 (setq default-directory dir)
315 (set (make-local-variable 'stgit-ewoc)
316 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
317 (setq buffer-read-only t))
320 (def-edebug-spec stgit-capture-output
322 (defmacro stgit-capture-output (name &rest body)
323 "Capture StGit output and, if there was any output, show it in a window
325 Returns nil if there was no output."
326 (declare (debug ([&or stringp null] body))
328 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
329 (stgit-dir default-directory)
330 (inhibit-read-only t))
331 (with-current-buffer output-buf
332 (buffer-disable-undo)
334 (setq default-directory stgit-dir)
335 (setq buffer-read-only t))
336 (let ((standard-output output-buf))
338 (with-current-buffer output-buf
339 (set-buffer-modified-p nil)
340 (setq buffer-read-only t)
341 (if (< (point-min) (point-max))
342 (display-buffer output-buf t)))))
344 (defun stgit-make-run-args (args)
345 "Return a copy of ARGS with its elements converted to strings."
347 ;; don't use (format "%s" ...) to limit type errors
348 (cond ((stringp x) x)
349 ((integerp x) (number-to-string x))
350 ((symbolp x) (symbol-name x))
352 (error "Bad element in stgit-make-run-args args: %S" x))))
355 (defun stgit-run-silent (&rest args)
356 (setq args (stgit-make-run-args args))
357 (apply 'call-process "stg" nil standard-output nil args))
359 (defun stgit-run (&rest args)
360 (setq args (stgit-make-run-args args))
361 (let ((msgcmd (mapconcat #'identity args " ")))
362 (message "Running stg %s..." msgcmd)
363 (apply 'call-process "stg" nil standard-output nil args)
364 (message "Running stg %s...done" msgcmd)))
366 (defun stgit-run-git (&rest args)
367 (setq args (stgit-make-run-args args))
368 (let ((msgcmd (mapconcat #'identity args " ")))
369 (message "Running git %s..." msgcmd)
370 (apply 'call-process "git" nil standard-output nil args)
371 (message "Running git %s...done" msgcmd)))
373 (defun stgit-run-git-silent (&rest args)
374 (setq args (stgit-make-run-args args))
375 (apply 'call-process "git" nil standard-output nil args))
377 (defun stgit-index-empty-p ()
378 "Returns non-nil if the index contains no changes from HEAD."
379 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
381 (defun stgit-work-tree-empty-p ()
382 "Returns non-nil if the work tree contains no changes from index."
383 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
385 (defvar stgit-index-node)
386 (defvar stgit-worktree-node)
388 (defvar stgit-did-advise nil
389 "Set to non-nil if appropriate (non-stgit) git functions have
390 been advised to update the stgit status when necessary.")
392 (defconst stgit-allowed-branch-name-re
393 ;; Disallow control characters, space, del, and "/:@^{}~" in
394 ;; "/"-separated parts; parts may not start with a period (.)
395 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
396 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
397 "Regular expression that (new) branch names must match.")
399 (defun stgit-refresh-index ()
400 (when stgit-index-node
401 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
403 (defun stgit-refresh-worktree ()
404 (when stgit-worktree-node
405 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
407 (defun stgit-run-series-insert-index (ewoc)
408 (setq index-node (cons ewoc (ewoc-enter-last ewoc
414 worktree-node (cons ewoc (ewoc-enter-last ewoc
421 (defun stgit-run-series (ewoc)
422 (setq stgit-index-node nil
423 stgit-worktree-node nil)
424 (let ((inserted-index (not stgit-show-worktree))
429 (let* ((standard-output (current-buffer))
430 (exit-status (stgit-run-silent "series"
431 "--description" "--empty")))
432 (goto-char (point-min))
433 (if (not (zerop exit-status))
434 (cond ((looking-at "stg series: \\(.*\\)")
435 (setq inserted-index t)
436 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
437 (substitute-command-keys
438 "-- not initialized; run \\[stgit-init]")))
440 (error "Error running stg: %s"
444 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
445 (error "Syntax error in output from stg series"))
446 (let* ((state-str (match-string 2))
447 (state (cond ((string= state-str ">") 'top)
448 ((string= state-str "+") 'applied)
449 ((string= state-str "-") 'unapplied)))
450 (name (intern (match-string 4)))
451 (desc (match-string 5))
452 (empty (string= (match-string 1) "0")))
453 (unless inserted-index
454 (when (or (eq stgit-show-worktree-mode 'top)
455 (and (eq stgit-show-worktree-mode 'center)
456 (eq state 'unapplied)))
457 (setq inserted-index t)
458 (stgit-run-series-insert-index ewoc)))
459 (setq all-patchsyms (cons name all-patchsyms))
460 (ewoc-enter-last ewoc
467 (unless inserted-index
468 (stgit-run-series-insert-index ewoc)))
469 (setq stgit-index-node index-node
470 stgit-worktree-node worktree-node
471 stgit-marked-patches (intersection stgit-marked-patches
474 (defun stgit-current-branch ()
475 "Return the name of the current branch."
476 (substring (with-output-to-string
477 (stgit-run-silent "branch"))
480 (defun stgit-reload ()
481 "Update the contents of the StGit buffer."
484 (let ((inhibit-read-only t)
485 (curline (line-number-at-pos))
486 (curpatch (stgit-patch-name-at-point))
487 (curfile (stgit-patched-file-at-point)))
488 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
489 (ewoc-set-hf stgit-ewoc
491 (propertize (stgit-current-branch)
492 'face 'stgit-branch-name-face)
494 (if stgit-show-worktree
497 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
498 shows the working tree\n")
499 'face 'stgit-description-face)))
500 (stgit-run-series stgit-ewoc)
502 (stgit-goto-patch curpatch (and curfile (stgit-file->file curfile)))
503 (goto-line curline)))
504 (stgit-refresh-git-status))
506 (defconst stgit-file-status-code-strings
507 (mapcar (lambda (arg)
509 (propertize (cadr arg) 'face (car (cddr arg)))))
510 '((add "Added" stgit-modified-file-face)
511 (copy "Copied" stgit-modified-file-face)
512 (delete "Deleted" stgit-modified-file-face)
513 (modify "Modified" stgit-modified-file-face)
514 (rename "Renamed" stgit-modified-file-face)
515 (mode-change "Mode change" stgit-modified-file-face)
516 (unmerged "Unmerged" stgit-unmerged-file-face)
517 (unknown "Unknown" stgit-unknown-file-face)
518 (ignore "Ignored" stgit-ignored-file-face)))
519 "Alist of code symbols to description strings")
521 (defconst stgit-patch-status-face-alist
522 '((applied . stgit-applied-patch-face)
523 (top . stgit-top-patch-face)
524 (unapplied . stgit-unapplied-patch-face)
525 (index . stgit-index-work-tree-title-face)
526 (work . stgit-index-work-tree-title-face))
527 "Alist of face to use for a given patch status")
529 (defun stgit-file-status-code-as-string (file)
530 "Return stgit status code for FILE as a string"
531 (let* ((code (assq (stgit-file->status file)
532 stgit-file-status-code-strings))
533 (score (stgit-file->cr-score file)))
535 (if (and score (/= score 100))
536 (format "%s %s" (cdr code)
537 (propertize (format "%d%%" score)
538 'face 'stgit-description-face))
541 (defun stgit-file-status-code (str &optional score)
542 "Return stgit status code from git status string"
543 (let ((code (assoc str '(("A" . add)
552 (setq code (if code (cdr code) 'unknown))
553 (when (stringp score)
554 (if (> (length score) 0)
555 (setq score (string-to-number score))
557 (if score (cons code score) code)))
559 (defconst stgit-file-type-strings
562 (#o160 . "subproject"))
563 "Alist of names of file types")
565 (defun stgit-file-type-string (type)
566 "Return string describing file type TYPE (the high bits of file permission).
567 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
568 (let ((type-str (assoc type stgit-file-type-strings)))
569 (or (and type-str (cdr type-str))
570 (format "unknown type %o" type))))
572 (defun stgit-file-type-change-string (old-perm new-perm)
573 "Return string describing file type change from OLD-PERM to NEW-PERM.
574 Cf. `stgit-file-type-string'."
575 (let ((old-type (lsh old-perm -9))
576 (new-type (lsh new-perm -9)))
577 (cond ((= old-type new-type) "")
578 ((zerop new-type) "")
580 (if (= new-type #o100)
582 (format "(%s)" (stgit-file-type-string new-type))))
583 (t (format "(%s -> %s)"
584 (stgit-file-type-string old-type)
585 (stgit-file-type-string new-type))))))
587 (defun stgit-file-mode-change-string (old-perm new-perm)
588 "Return string describing file mode change from OLD-PERM to NEW-PERM.
589 Cf. `stgit-file-type-change-string'."
590 (setq old-perm (logand old-perm #o777)
591 new-perm (logand new-perm #o777))
592 (if (or (= old-perm new-perm)
596 (let* ((modified (logxor old-perm new-perm))
597 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
598 (cond ((zerop modified) "")
599 ((and (zerop not-x-modified)
600 (or (and (eq #o111 (logand old-perm #o111))
601 (propertize "-x" 'face 'stgit-file-permission-face))
602 (and (eq #o111 (logand new-perm #o111))
603 (propertize "+x" 'face
604 'stgit-file-permission-face)))))
605 (t (concat (propertize (format "%o" old-perm)
606 'face 'stgit-file-permission-face)
608 'face 'stgit-description-face)
609 (propertize (format "%o" new-perm)
610 'face 'stgit-file-permission-face)))))))
612 (defstruct (stgit-file
613 (:conc-name stgit-file->))
614 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
616 (defun stgit-describe-copy-or-rename (file)
617 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
618 from to common-head common-tail)
620 (when stgit-abbreviate-copies-and-renames
621 (setq from (split-string (stgit-file->cr-from file) "/")
622 to (split-string (stgit-file->cr-to file) "/"))
624 (while (and from to (cdr from) (cdr to)
625 (string-equal (car from) (car to)))
626 (setq common-head (cons (car from) common-head)
629 (setq common-head (nreverse common-head)
632 (while (and from to (cdr from) (cdr to)
633 (string-equal (car from) (car to)))
634 (setq common-tail (cons (car from) common-tail)
637 (setq from (nreverse from)
640 (if (or common-head common-tail)
641 (concat (if common-head
642 (mapconcat #'identity common-head "/")
644 (if common-head "/" "")
645 (propertize "{" 'face 'stgit-description-face)
646 (mapconcat #'identity from "/")
648 (mapconcat #'identity to "/")
649 (propertize "}" 'face 'stgit-description-face)
650 (if common-tail "/" "")
652 (mapconcat #'identity common-tail "/")
654 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
656 (defun stgit-file-pp (file)
657 (let ((start (point))
658 (spec (format-spec-make
659 ?s (stgit-file-status-code-as-string file)
660 ?m (stgit-file-mode-change-string
661 (stgit-file->old-perm file)
662 (stgit-file->new-perm file))
663 ?n (if (stgit-file->copy-or-rename file)
664 (stgit-describe-copy-or-rename file)
665 (stgit-file->file file))
666 ?c (propertize (stgit-file-type-change-string
667 (stgit-file->old-perm file)
668 (stgit-file->new-perm file))
669 'face 'stgit-description-face))))
670 (stgit-insert-without-trailing-whitespace
671 (format-spec stgit-file-line-format spec))
672 (add-text-properties start (point)
673 (list 'entry-type 'file
676 (defun stgit-find-copies-harder-diff-arg ()
677 "Return the flag to use with `git-diff' depending on the
678 `stgit-find-copies-harder' flag."
679 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
681 (defun stgit-insert-ls-files (args file-flag)
682 (let ((start (point)))
683 (apply 'stgit-run-git
684 (append '("ls-files" "--exclude-standard" "-z") args))
686 (while (looking-at "\\([^\0]*\\)\0")
687 (let ((name-len (- (match-end 0) (match-beginning 0))))
688 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
689 (forward-char name-len)))))
691 (defun stgit-process-files (callback)
692 (goto-char (point-min))
693 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
694 (goto-char (match-end 0)))
695 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
696 (let ((old-perm (string-to-number (match-string 1) 8))
697 (new-perm (string-to-number (match-string 2) 8)))
698 (goto-char (match-end 0))
701 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
702 (let* ((patch-status (stgit-patch->status patch))
703 (file-subexp (if (eq patch-status 'unapplied)
706 (file (match-string file-subexp)))
711 :cr-score (string-to-number (match-string 2))
712 :cr-from (match-string 3)
713 :cr-to (match-string 4)
714 :status (stgit-file-status-code
717 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
725 :status (stgit-file-status-code
727 :file (match-string 2))))))
728 (goto-char (match-end 0))
729 (funcall callback file)))))
732 (defun stgit-insert-patch-files (patch)
733 "Expand (show modification of) the patch PATCH after the line
735 (let* ((patchsym (stgit-patch->name patch))
737 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
738 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
739 (set-marker-insertion-type end t)
740 (setf (stgit-patch->files-ewoc patch) ewoc)
742 (let ((standard-output (current-buffer)))
743 (apply 'stgit-run-git
744 (cond ((eq patchsym :work)
745 `("diff-files" "-0" ,@args))
746 ((eq patchsym :index)
747 `("diff-index" ,@args "--cached" "HEAD"))
749 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
751 (when (and (eq patchsym :work))
752 (when stgit-show-ignored
753 (stgit-insert-ls-files '("--ignored" "--others") "I"))
754 (when stgit-show-unknown
755 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
758 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
759 (point-min) (point-max)))
761 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
763 (unless (ewoc-nth ewoc 0)
766 (propertize "<no files>"
767 'face 'stgit-description-face)
771 (defun stgit-find-file (&optional other-window)
772 (let* ((file (or (stgit-patched-file-at-point)
773 (error "No file at point")))
774 (filename (expand-file-name (stgit-file->file file))))
775 (unless (file-exists-p filename)
776 (error "File does not exist"))
777 (funcall (if other-window 'find-file-other-window 'find-file)
779 (when (eq (stgit-file->status file) 'unmerged)
782 (defun stgit-expand (&optional patches collapse)
783 "Show the contents of marked patches, or the patch at point.
785 See also `stgit-collapse'.
787 Non-interactively, operate on PATCHES, and collapse instead of
788 expand if COLLAPSE is not nil."
789 (interactive (list (stgit-patches-marked-or-at-point t)))
791 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
792 patches stgit-expanded-patches)))
793 (setq stgit-expanded-patches
795 (set-difference stgit-expanded-patches patches-diff)
796 (append stgit-expanded-patches patches-diff)))
797 (ewoc-map #'(lambda (patch)
798 (memq (stgit-patch->name patch) patches-diff))
800 (move-to-column (stgit-goal-column)))
802 (defun stgit-collapse (&optional patches)
803 "Hide the contents of marked patches, or the patch at point.
805 See also `stgit-expand'."
806 (interactive (list (stgit-patches-marked-or-at-point t)))
808 (stgit-expand patches t))
810 (defun stgit-select-patch ()
811 (let ((patchname (stgit-patch-name-at-point)))
812 (stgit-expand (list patchname)
813 (memq patchname stgit-expanded-patches))))
815 (defun stgit-expand-directory (file)
816 (let* ((patch (stgit-patch-at-point))
817 (ewoc (stgit-patch->files-ewoc patch))
818 (node (ewoc-locate ewoc))
819 (filename (stgit-file->file file))
820 (start (make-marker))
825 (set-marker start (point))
826 (set-marker end (point))
827 (set-marker-insertion-type end t))
829 (assert (string-match "/$" filename))
830 ;; remove trailing "/"
831 (setf (stgit-file->file file) (substring filename 0 -1))
832 (ewoc-invalidate ewoc node)
835 (let ((standard-output (current-buffer)))
836 (stgit-insert-ls-files (list "--directory" "--others"
837 "--no-empty-directory" "--"
840 (stgit-process-files (lambda (f)
841 (setq node (ewoc-enter-after ewoc node f))))))
843 (let ((inhibit-read-only t))
844 (put-text-property start end 'patch-data patch))))
846 (defun stgit-select-file ()
847 (let* ((file (or (stgit-patched-file-at-point)
848 (error "No file at point")))
849 (filename (stgit-file->file file)))
850 (if (string-match "/$" filename)
851 (stgit-expand-directory file)
854 (defun stgit-select ()
855 "With point on a patch, toggle showing files in the patch.
857 With point on a file, open the associated file. Opens the target
858 file for (applied) copies and renames."
861 (case (get-text-property (point) 'entry-type)
863 (stgit-select-patch))
867 (error "No patch or file on line"))))
869 (defun stgit-find-file-other-window ()
870 "Open file at point in other window"
875 (defun stgit-find-file-merge ()
876 "Open file at point and merge it using `smerge-ediff'."
883 "Hide the stgit buffer."
888 (defun stgit-git-status ()
889 "Show status using `git-status'."
892 (unless (fboundp 'git-status)
893 (error "The stgit-git-status command requires git-status"))
894 (let ((dir default-directory))
895 (save-selected-window
899 (defun stgit-goal-column ()
900 "Return goal column for the current line"
901 (case (get-text-property (point) 'entry-type)
906 (defun stgit-next-line (&optional arg)
907 "Move cursor vertically down ARG lines"
911 (move-to-column (stgit-goal-column)))
913 (defun stgit-previous-line (&optional arg)
914 "Move cursor vertically up ARG lines"
918 (move-to-column (stgit-goal-column)))
920 (defun stgit-next-patch (&optional arg)
921 "Move cursor down ARG patches."
924 (ewoc-goto-next stgit-ewoc (or arg 1))
925 (move-to-column goal-column))
927 (defun stgit-previous-patch (&optional arg)
928 "Move cursor up ARG patches."
931 (ewoc-goto-prev stgit-ewoc (or arg 1))
932 (move-to-column goal-column))
934 (defvar stgit-mode-hook nil
935 "Run after `stgit-mode' is setup.")
937 (defvar stgit-mode-map nil
938 "Keymap for StGit major mode.")
940 (unless stgit-mode-map
941 (let ((diff-map (make-sparse-keymap))
942 (toggle-map (make-sparse-keymap)))
943 (suppress-keymap diff-map)
944 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
945 '(("b" . stgit-diff-base)
946 ("c" . stgit-diff-combined)
947 ("m" . stgit-find-file-merge)
948 ("o" . stgit-diff-ours)
949 ("r" . stgit-diff-range)
950 ("t" . stgit-diff-theirs)))
951 (suppress-keymap toggle-map)
952 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
953 '(("n" . stgit-toggle-patch-names)
954 ("t" . stgit-toggle-worktree)
955 ("i" . stgit-toggle-ignored)
956 ("u" . stgit-toggle-unknown)))
957 (setq stgit-mode-map (make-keymap))
958 (suppress-keymap stgit-mode-map)
959 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
960 `((" " . stgit-mark-down)
961 ("m" . stgit-mark-down)
962 ("\d" . stgit-unmark-up)
963 ("u" . stgit-unmark-down)
966 ("\C-p" . stgit-previous-line)
967 ("\C-n" . stgit-next-line)
968 ([up] . stgit-previous-line)
969 ([down] . stgit-next-line)
970 ("p" . stgit-previous-patch)
971 ("n" . stgit-next-patch)
972 ("\M-{" . stgit-previous-patch)
973 ("\M-}" . stgit-next-patch)
974 ("s" . stgit-git-status)
975 ("g" . stgit-reload-or-repair)
976 ("r" . stgit-refresh)
977 ("\C-c\C-r" . stgit-rename)
979 ("M" . stgit-move-patches)
982 ("c" . stgit-new-and-refresh)
983 ("\C-c\C-c" . stgit-commit)
984 ("\C-c\C-u" . stgit-uncommit)
986 ("R" . stgit-resolve-file)
987 ("\r" . stgit-select)
989 ("-" . stgit-collapse)
990 ("o" . stgit-find-file-other-window)
991 ("i" . stgit-toggle-index)
992 (">" . stgit-push-next)
993 ("<" . stgit-pop-next)
994 ("P" . stgit-push-or-pop)
998 ([?\C-/] . stgit-undo)
999 ("\C-_" . stgit-undo)
1000 ([?\C-c ?\C-/] . stgit-redo)
1001 ("\C-c\C-_" . stgit-redo)
1002 ("B" . stgit-branch)
1003 ("\C-c\C-b" . stgit-rebase)
1006 ("q" . stgit-quit))))
1008 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1009 (and file (eq (stgit-file->status file)
1011 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1012 (easy-menu-define stgit-menu stgit-mode-map
1015 ["Reload" stgit-reload-or-repair
1016 :help "Reload StGit status from disk"]
1017 ["Repair" stgit-repair
1018 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1019 :help "Repair StGit metadata"]
1021 ["Undo" stgit-undo t]
1022 ["Redo" stgit-redo t]
1024 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1026 ["New patch" stgit-new-and-refresh
1027 :help "Create a new patch from changes in index or work tree"
1028 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1029 ["New empty patch" stgit-new
1030 :help "Create a new, empty patch"]
1031 ["(Un)mark patch" stgit-toggle-mark
1032 :label (if (memq (stgit-patch-name-at-point nil t)
1033 stgit-marked-patches)
1034 "Unmark patch" "Mark patch")
1035 :active (stgit-patch-name-at-point nil t)]
1036 ["Expand/collapse patch"
1037 (let ((patches (stgit-patches-marked-or-at-point)))
1038 (if (member-if ,patch-collapsed-p patches)
1039 (stgit-expand patches)
1040 (stgit-collapse patches)))
1041 :label (if (member-if ,patch-collapsed-p
1042 (stgit-patches-marked-or-at-point))
1045 :active (stgit-patches-marked-or-at-point)]
1046 ["Edit patch" stgit-edit
1047 :help "Edit patch comment"
1048 :active (stgit-patch-name-at-point nil t)]
1049 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1050 ["Push/pop patch" stgit-push-or-pop
1051 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1052 (stgit-applied-patchsyms t))
1053 "Pop patches" "Push patches")]
1054 ["Delete patches" stgit-delete
1055 :active (stgit-patches-marked-or-at-point nil t)]
1057 ["Move patches" stgit-move-patches
1058 :active stgit-marked-patches
1059 :help "Move marked patch(es) to point"]
1060 ["Squash patches" stgit-squash
1061 :active (> (length stgit-marked-patches) 1)
1062 :help "Merge marked patches into one"]
1064 ["Refresh top patch" stgit-refresh
1065 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1066 :help "Refresh the top patch with changes in index or work tree"]
1067 ["Refresh this patch" (stgit-refresh t)
1068 :keys "\\[universal-argument] \\[stgit-refresh]"
1069 :help "Refresh marked patch with changes in index or work tree"
1070 :active (and (not (and (stgit-index-empty-p)
1071 (stgit-work-tree-empty-p)))
1072 (stgit-patch-name-at-point nil t))]
1074 ["Find file" stgit-select
1075 :active (eq (get-text-property (point) 'entry-type) 'file)]
1076 ["Open file" stgit-find-file-other-window
1077 :active (eq (get-text-property (point) 'entry-type) 'file)]
1078 ["Toggle file index" stgit-toggle-index
1079 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1080 (memq (stgit-patch-name-at-point) '(:work :index)))
1081 :label (if (eq (stgit-patch-name-at-point) :work)
1082 "Move change to index"
1083 "Move change to work tree")]
1085 ["Show diff" stgit-diff
1086 :active (get-text-property (point) 'entry-type)]
1087 ["Show diff for range of applied patches" stgit-diff-range
1088 :active (= (length stgit-marked-patches) 1)]
1090 :active (stgit-git-index-unmerged-p)
1091 ["Combined diff" stgit-diff-combined
1092 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1093 ["Diff against base" stgit-diff-base
1094 :help "Show diff against the common base"
1095 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1096 ["Diff against ours" stgit-diff-ours
1097 :help "Show diff against our branch"
1098 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1099 ["Diff against theirs" stgit-diff-theirs
1100 :help "Show diff against their branch"
1101 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1103 ["Interactive merge" stgit-find-file-merge
1104 :help "Interactively merge the file"
1105 :active ,at-unmerged-file]
1106 ["Resolve file" stgit-resolve-file
1107 :help "Mark file conflict as resolved"
1108 :active ,at-unmerged-file]
1111 ["Show index & work tree" stgit-toggle-worktree :style toggle
1112 :selected stgit-show-worktree]
1113 ["Show unknown files" stgit-toggle-unknown :style toggle
1114 :selected stgit-show-unknown :active stgit-show-worktree]
1115 ["Show ignored files" stgit-toggle-ignored :style toggle
1116 :selected stgit-show-ignored :active stgit-show-worktree]
1117 ["Show patch names" stgit-toggle-patch-names :style toggle
1118 :selected stgit-show-patch-names]
1120 ["Switch branches" stgit-branch t
1121 :help "Switch to or create another branch"]
1122 ["Rebase branch" stgit-rebase t
1123 :help "Rebase the current branch"]
1126 ;; disable tool bar editing buttons
1127 (put 'stgit-mode 'mode-class 'special)
1129 (defun stgit-mode ()
1130 "Major mode for interacting with StGit.
1132 Start StGit using \\[stgit].
1136 \\[stgit-help] Show this help text
1137 \\[stgit-quit] Hide the StGit buffer
1138 \\[describe-bindings] Show all key bindings
1140 \\[stgit-reload-or-repair] Reload the StGit buffer
1141 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1143 \\[stgit-undo] Undo most recent StGit operation
1144 \\[stgit-redo] Undo recent undo
1146 \\[stgit-git-status] Run `git-status' (if available)
1149 \\[stgit-previous-line] Move to previous line
1150 \\[stgit-next-line] Move to next line
1151 \\[stgit-previous-patch] Move to previous patch
1152 \\[stgit-next-patch] Move to next patch
1154 \\[stgit-mark-down] Mark patch and move down
1155 \\[stgit-unmark-up] Unmark patch and move up
1156 \\[stgit-unmark-down] Unmark patch and move down
1158 Commands for patches:
1159 \\[stgit-select] Toggle showing changed files in patch
1160 \\[stgit-refresh] Refresh patch with changes in index or work tree
1161 \\[stgit-diff] Show the patch log and diff
1163 \\[stgit-expand] Show changes in marked patches
1164 \\[stgit-collapse] Hide changes in marked patches
1166 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1167 \\[stgit-new] Create a new, empty patch
1169 \\[stgit-rename] Rename patch
1170 \\[stgit-edit] Edit patch description
1171 \\[stgit-delete] Delete patch(es)
1173 \\[stgit-revert] Revert all changes in index or work tree
1174 \\[stgit-toggle-index] Toggle all changes between index and work tree
1176 \\[stgit-push-next] Push next patch onto stack
1177 \\[stgit-pop-next] Pop current patch from stack
1178 \\[stgit-push-or-pop] Push or pop marked patches
1179 \\[stgit-goto] Make patch at point current by popping or pushing
1181 \\[stgit-squash] Squash (meld together) patches
1182 \\[stgit-move-patches] Move marked patches to point
1184 \\[stgit-commit] Commit patch(es)
1185 \\[stgit-uncommit] Uncommit patch(es)
1188 \\[stgit-select] Open the file in this window
1189 \\[stgit-find-file-other-window] Open the file in another window
1190 \\[stgit-diff] Show the file's diff
1192 \\[stgit-toggle-index] Toggle change between index and work tree
1193 \\[stgit-revert] Revert changes to file
1196 \\[stgit-toggle-patch-names] Toggle showing patch names
1197 \\[stgit-toggle-worktree] Toggle showing index and work tree
1198 \\[stgit-toggle-unknown] Toggle showing unknown files
1199 \\[stgit-toggle-ignored] Toggle showing ignored files
1202 \\[stgit-diff] Show diff of patch or file
1203 \\[stgit-diff-range] Show diff for range of patches
1204 \\[stgit-diff-base] Show diff against the merge base
1205 \\[stgit-diff-ours] Show diff against our branch
1206 \\[stgit-diff-theirs] Show diff against their branch
1208 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1209 ignore space changes.
1210 With two prefix arguments (e.g., \\[universal-argument] \
1211 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1213 Commands for merge conflicts:
1214 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1215 \\[stgit-resolve-file] Mark unmerged file as resolved
1217 Commands for branches:
1218 \\[stgit-branch] Switch to or create another branch
1219 \\[stgit-rebase] Rebase the current branch
1221 Customization variables:
1222 `stgit-abbreviate-copies-and-renames'
1223 `stgit-default-show-patch-names'
1224 `stgit-default-show-worktree'
1225 `stgit-find-copies-harder'
1226 `stgit-show-worktree-mode'
1228 See also \\[customize-group] for the \"stgit\" group."
1229 (kill-all-local-variables)
1230 (buffer-disable-undo)
1231 (setq mode-name "StGit"
1232 major-mode 'stgit-mode
1234 (use-local-map stgit-mode-map)
1235 (set (make-local-variable 'list-buffers-directory) default-directory)
1236 (set (make-local-variable 'stgit-marked-patches) nil)
1237 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1238 (set (make-local-variable 'stgit-show-patch-names)
1239 stgit-default-show-patch-names)
1240 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1241 (set (make-local-variable 'stgit-index-node) nil)
1242 (set (make-local-variable 'stgit-worktree-node) nil)
1243 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1244 (set-variable 'truncate-lines 't)
1245 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1246 (unless stgit-did-advise
1248 (setq stgit-did-advise t))
1249 (run-hooks 'stgit-mode-hook))
1251 (defun stgit-advise-funlist (funlist)
1252 "Add advice to the functions in FUNLIST so we can refresh the
1253 stgit buffers as the git status of files change."
1256 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1257 (stgit-update-stgit-for-buffer t)))
1261 (defun stgit-advise ()
1262 "Add advice to appropriate (non-stgit) git functions so we can
1263 refresh the stgit buffers as the git status of files change."
1265 (let ((feature (car arg))
1266 (funlist (cdr arg)))
1267 (if (featurep feature)
1268 (stgit-advise-funlist funlist)
1269 (add-to-list 'after-load-alist
1270 `(,feature (stgit-advise-funlist
1271 (quote ,funlist)))))))
1272 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1273 (git git-add-file git-checkout git-revert-file git-remove-file))))
1275 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1276 "Refresh worktree status in any `stgit-mode' buffer that shows
1277 the status of the current buffer.
1279 If REFRESH-INDEX is not-nil, also update the index."
1280 (let* ((dir (cond ((eq major-mode 'git-status-mode)
1283 (file-name-directory
1284 (expand-file-name buffer-file-name)))))
1285 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1287 (buffer (and gitdir (stgit-find-buffer gitdir))))
1289 (with-current-buffer buffer
1290 (stgit-refresh-worktree)
1291 (when refresh-index (stgit-refresh-index))))))
1293 (defun stgit-add-mark (patchsym)
1294 "Mark the patch PATCHSYM."
1295 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1297 (defun stgit-remove-mark (patchsym)
1298 "Unmark the patch PATCHSYM."
1299 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1301 (defun stgit-clear-marks ()
1302 "Unmark all patches."
1303 (setq stgit-marked-patches '()))
1305 (defun stgit-patch-at-point (&optional cause-error)
1306 (get-text-property (point) 'patch-data))
1308 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1309 "Return the patch name on the current line as a symbol.
1310 If CAUSE-ERROR is not nil, signal an error if none found.
1311 If ONLY-PATCHES is not nil, only allow real patches, and not
1312 index or work tree."
1313 (let ((patch (stgit-patch-at-point)))
1316 (memq (stgit-patch->status patch) '(work index))
1319 (stgit-patch->name patch))
1321 (error "No patch on this line")))))
1323 (defun stgit-patched-file-at-point ()
1324 (get-text-property (point) 'file-data))
1326 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1327 "Return the symbols of the marked patches, or the patch on the current line.
1328 If CAUSE-ERRROR is not nil, signal an error if none found.
1329 If ONLY-PATCHES is not nil, do not include index or work tree."
1330 (if stgit-marked-patches
1331 stgit-marked-patches
1332 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1333 (cond (patch (list patch))
1334 (cause-error (error "No patches marked or at this line"))
1337 (defun stgit-goto-patch (patchsym &optional file)
1338 "Move point to the line containing patch PATCHSYM.
1339 If that patch cannot be found, do nothing.
1341 If the patch was found and FILE is not nil, instead move to that
1342 file's line. If FILE cannot be found, stay on the line of
1344 (let ((node (ewoc-nth stgit-ewoc 0)))
1345 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1347 (setq node (ewoc-next stgit-ewoc node)))
1348 (when (and node file)
1349 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1350 (file-node (ewoc-nth file-ewoc 0)))
1351 (while (and file-node (not (equal (stgit-file->file (ewoc-data file-node)) file)))
1352 (setq file-node (ewoc-next file-ewoc file-node)))
1354 (ewoc-goto-node file-ewoc file-node)
1355 (move-to-column (stgit-goal-column))
1358 (ewoc-goto-node stgit-ewoc node)
1359 (move-to-column goal-column))))
1361 (defun stgit-init ()
1365 (stgit-capture-output nil
1369 (defun stgit-toggle-mark ()
1370 "Toggle mark on the patch under point."
1373 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1377 (defun stgit-mark ()
1378 "Mark the patch under point."
1381 (let* ((node (ewoc-locate stgit-ewoc))
1382 (patch (ewoc-data node))
1383 (name (stgit-patch->name patch)))
1384 (when (eq name :work)
1385 (error "Cannot mark the work tree"))
1386 (when (eq name :index)
1387 (error "Cannot mark the index"))
1388 (stgit-add-mark (stgit-patch->name patch))
1389 (let ((column (current-column)))
1390 (ewoc-invalidate stgit-ewoc node)
1391 (move-to-column column))))
1393 (defun stgit-mark-down ()
1394 "Mark the patch under point and move to the next patch."
1399 (defun stgit-unmark ()
1400 "Remove mark from the patch on the current line."
1403 (let* ((node (ewoc-locate stgit-ewoc))
1404 (patch (ewoc-data node)))
1405 (stgit-remove-mark (stgit-patch->name patch))
1406 (let ((column (current-column)))
1407 (ewoc-invalidate stgit-ewoc node)
1408 (move-to-column column))))
1410 (defun stgit-unmark-up ()
1411 "Remove mark from the patch on the previous line."
1414 (stgit-previous-patch)
1417 (defun stgit-unmark-down ()
1418 "Remove mark from the patch on the current line."
1424 (defun stgit-rename (name)
1425 "Rename the patch under point to NAME."
1427 (read-string "Patch name: "
1428 (symbol-name (stgit-patch-name-at-point t t)))))
1430 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1431 (stgit-capture-output nil
1432 (stgit-run "rename" old-patchsym name))
1433 (let ((name-sym (intern name)))
1434 (when (memq old-patchsym stgit-expanded-patches)
1435 (setq stgit-expanded-patches
1436 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1437 (when (memq old-patchsym stgit-marked-patches)
1438 (setq stgit-marked-patches
1439 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1441 (stgit-goto-patch name-sym))))
1443 (defun stgit-reload-or-repair (repair)
1444 "Update the contents of the StGit buffer (`stgit-reload').
1446 With a prefix argument, repair the StGit metadata if the branch
1447 was modified with git commands (`stgit-repair')."
1454 (defun stgit-repair ()
1458 (stgit-capture-output nil
1459 (stgit-run "repair"))
1462 (defun stgit-available-branches (&optional all)
1463 "Returns a list of the names of the available stg branches as strings.
1465 If ALL is not nil, also return non-stgit branches."
1466 (let ((output (with-output-to-string
1467 (stgit-run "branch" "--list")))
1468 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1472 (while (string-match pattern output start)
1473 (setq result (cons (match-string 1 output) result))
1474 (setq start (match-end 0)))
1477 (defun stgit-branch (branch)
1478 "Switch to or create branch BRANCH."
1479 (interactive (list (completing-read "Switch to branch: "
1480 (stgit-available-branches))))
1482 (when (cond ((equal branch (stgit-current-branch))
1483 (error "Branch is already current"))
1484 ((member branch (stgit-available-branches t))
1485 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1487 ((not (string-match stgit-allowed-branch-name-re branch))
1488 (error "Invalid branch name"))
1489 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1490 (stgit-capture-output nil (stgit-run "branch" "--create" "--"
1495 (defun stgit-available-refs (&optional omit-stgit)
1496 "Returns a list of the available git refs.
1497 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1498 (let* ((output (with-output-to-string
1499 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1500 "refs/tags" "refs/heads"
1502 (result (split-string output "\n" t)))
1504 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1505 (substring s (match-end 0))
1508 (delete-if (lambda (s)
1509 (string-match "^refs/heads/.*\\.stgit$" s))
1513 (defun stgit-parent-branch ()
1514 "Return the parent branch of the current stg branch as per
1515 git-config setting branch.<branch>.stgit.parentbranch."
1516 (let ((output (with-output-to-string
1517 (stgit-run-git-silent "config"
1518 (format "branch.%s.stgit.parentbranch"
1519 (stgit-current-branch))))))
1520 (when (string-match ".*" output)
1521 (match-string 0 output))))
1523 (defun stgit-rebase (new-base)
1524 "Rebase the current branch to NEW-BASE.
1526 Interactively, first ask which branch to rebase to. Defaults to
1527 what git-config branch.<branch>.stgit.parentbranch is set to."
1528 (interactive (list (completing-read "Rebase to: "
1529 (stgit-available-refs t)
1531 (stgit-parent-branch))))
1533 (stgit-capture-output nil (stgit-run "rebase" new-base))
1536 (defun stgit-commit (count)
1537 "Run stg commit on COUNT commits.
1538 Interactively, the prefix argument is used as COUNT.
1539 A negative COUNT will uncommit instead."
1543 (stgit-uncommit (- count))
1544 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1547 (defun stgit-uncommit (count)
1548 "Run stg uncommit on COUNT commits.
1549 Interactively, the prefix argument is used as COUNT.
1550 A negative COUNT will commit instead."
1554 (stgit-commit (- count))
1555 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1558 (defun stgit-neighbour-file ()
1559 "Return the file name of the next file after point, or the
1560 previous file if point is at the last file within a patch."
1561 (let ((old-point (point))
1563 (and (zerop (forward-line 1))
1564 (let ((f (stgit-patched-file-at-point)))
1565 (and f (setq neighbour-file (stgit-file->file f)))))
1566 (goto-char old-point)
1567 (unless neighbour-file
1568 (and (zerop (forward-line -1))
1569 (let ((f (stgit-patched-file-at-point)))
1570 (and f (setq neighbour-file (stgit-file->file f)))))
1571 (goto-char old-point))
1574 (defun stgit-revert-file ()
1575 "Revert the file at point, which must be in the index or the
1579 (let* ((patched-file (or (stgit-patched-file-at-point)
1580 (error "No file on the current line")))
1581 (patch-name (stgit-patch-name-at-point))
1582 (file-status (stgit-file->status patched-file))
1583 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1584 (stgit-file->cr-to patched-file))
1585 ((eq file-status 'add)
1586 (stgit-file->file patched-file))))
1587 (co-file (cond ((eq file-status 'rename)
1588 (stgit-file->cr-from patched-file))
1589 ((not (memq file-status '(copy add)))
1590 (stgit-file->file patched-file))))
1591 (next-file (stgit-neighbour-file)))
1593 (unless (memq patch-name '(:work :index))
1594 (error "No index or working tree file on this line"))
1596 (when (eq file-status 'ignore)
1597 (error "Cannot revert ignored files"))
1599 (when (eq file-status 'unknown)
1600 (error "Cannot revert unknown files"))
1602 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1603 (when (yes-or-no-p (format "Revert %d file%s? "
1605 (if (= nfiles 1) "" "s")))
1606 (stgit-capture-output nil
1608 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1610 (stgit-run-git "checkout" "HEAD" co-file)))
1612 (stgit-goto-patch patch-name next-file)))))
1614 (defun stgit-revert ()
1615 "Revert the change at point, which must be the index, the work
1616 tree, or a single change in either."
1619 (let ((patched-file (stgit-patched-file-at-point)))
1622 (let* ((patch-name (or (stgit-patch-name-at-point)
1623 (error "No patch or file at point")))
1624 (patch-desc (case patch-name
1627 (t (error (substitute-command-keys
1628 "Use \\[stgit-delete] to delete a patch"))))))
1629 (when (if (eq patch-name :work)
1630 (stgit-work-tree-empty-p)
1631 (stgit-index-empty-p))
1632 (error (format "There are no changes in the %s to revert"
1634 (and (eq patch-name :index)
1635 (not (stgit-work-tree-empty-p))
1636 (error "Cannot revert index as work tree contains unstaged changes"))
1638 (when (yes-or-no-p (format "Revert all changes in the %s? "
1640 (if (eq patch-name :index)
1641 (stgit-run-git-silent "reset" "--hard" "-q")
1642 (stgit-run-git-silent "checkout" "--" "."))
1643 (stgit-refresh-index)
1644 (stgit-refresh-worktree)
1645 (stgit-goto-patch patch-name))))))
1647 (defun stgit-resolve-file ()
1648 "Resolve conflict in the file at point."
1651 (let* ((patched-file (stgit-patched-file-at-point))
1652 (patch (stgit-patch-at-point))
1653 (patch-name (and patch (stgit-patch->name patch)))
1654 (status (and patched-file (stgit-file->status patched-file))))
1656 (unless (memq patch-name '(:work :index))
1657 (error "No index or working tree file on this line"))
1659 (unless (eq status 'unmerged)
1660 (error "No conflict to resolve at the current line"))
1662 (stgit-capture-output nil
1663 (stgit-move-change-to-index (stgit-file->file patched-file)))
1667 (defun stgit-push-or-pop-patches (do-push npatches)
1668 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1669 NPATCHES patches, or all patches if NPATCHES is t."
1671 (stgit-capture-output nil
1673 (if do-push "push" "pop")
1676 (list "-n" npatches))))
1678 (stgit-refresh-git-status))
1680 (defun stgit-push-next (npatches)
1681 "Push the first unapplied patch.
1682 With numeric prefix argument, push that many patches."
1684 (stgit-push-or-pop-patches t npatches))
1686 (defun stgit-pop-next (npatches)
1687 "Pop the topmost applied patch.
1688 With numeric prefix argument, pop that many patches.
1690 If NPATCHES is t, pop all patches."
1692 (stgit-push-or-pop-patches nil npatches))
1694 (defun stgit-applied-patches (&optional only-patches)
1695 "Return a list of the applied patches.
1697 If ONLY-PATCHES is not nil, exclude index and work tree."
1698 (let ((states (if only-patches
1700 '(applied top index work)))
1702 (ewoc-map (lambda (patch)
1703 (when (memq (stgit-patch->status patch) states)
1704 (setq result (cons patch result)))
1709 (defun stgit-applied-patchsyms (&optional only-patches)
1710 "Return a list of the symbols of the applied patches.
1712 If ONLY-PATCHES is not nil, exclude index and work tree."
1713 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1715 (defun stgit-push-or-pop ()
1716 "Push or pop the marked patches."
1719 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1720 (applied-syms (stgit-applied-patchsyms t))
1721 (unapplied (set-difference patchsyms applied-syms)))
1722 (stgit-capture-output nil
1724 (if unapplied "push" "pop")
1726 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1729 (defun stgit-goto-target ()
1730 "Return the goto target a point; either a patchsym, :top,
1732 (let ((patchsym (stgit-patch-name-at-point)))
1733 (cond ((memq patchsym '(:work :index)) nil)
1735 ((not (next-single-property-change (point) 'patch-data))
1737 ((not (previous-single-property-change (point) 'patch-data))
1740 (defun stgit-goto ()
1741 "Go to the patch on the current line.
1743 Push or pop patches to make this patch topmost. Push or pop all
1744 patches if used on a line after or before all patches."
1747 (let ((patchsym (stgit-goto-target)))
1749 (error "No patch to go to on this line"))
1751 (:top (stgit-push-or-pop-patches t t))
1752 (:bottom (stgit-push-or-pop-patches nil t))
1753 (t (stgit-capture-output nil
1754 (stgit-run "goto" patchsym))
1757 (defun stgit-id (patchsym)
1758 "Return the git commit id for PATCHSYM.
1759 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1760 (if (keywordp patchsym)
1762 (let ((result (with-output-to-string
1763 (stgit-run-silent "id" patchsym))))
1764 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1765 (error "Cannot find commit id for %s" patchsym))
1766 (match-string 1 result))))
1768 (defun stgit-whitespace-diff-arg (arg)
1770 (cond ((> arg 4) "--ignore-all-space")
1771 ((> arg 1) "--ignore-space-change"))))
1773 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1774 "Show the patch on the current line.
1776 UNMERGED-STAGE is the argument to `git-diff' that that selects
1777 which stage to diff against in the case of unmerged files."
1778 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
1779 (patch-name (stgit-patch-name-at-point t)))
1780 (stgit-capture-output "*StGit patch*"
1781 (case (get-text-property (point) 'entry-type)
1783 (let* ((patched-file (stgit-patched-file-at-point))
1784 (patch-id (let ((id (stgit-id patch-name)))
1785 (if (and (eq id :index)
1786 (eq (stgit-file->status patched-file)
1790 (args (append (and space-arg (list space-arg))
1791 (and (stgit-file->cr-from patched-file)
1792 (list (stgit-find-copies-harder-diff-arg)))
1793 (cond ((eq patch-id :index)
1795 ((eq patch-id :work)
1796 (list unmerged-stage))
1798 (list (concat patch-id "^") patch-id)))
1800 (if (stgit-file->copy-or-rename patched-file)
1801 (list (stgit-file->cr-from patched-file)
1802 (stgit-file->cr-to patched-file))
1803 (list (stgit-file->file patched-file))))))
1804 (apply 'stgit-run-git "diff" args)))
1806 (let* ((patch-id (stgit-id patch-name)))
1807 (if (or (eq patch-id :index) (eq patch-id :work))
1808 (apply 'stgit-run-git "diff"
1809 (stgit-find-copies-harder-diff-arg)
1810 (append (and space-arg (list space-arg))
1811 (if (eq patch-id :index)
1813 (list unmerged-stage))))
1814 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1815 (and space-arg (list "-O" space-arg))
1816 (list (stgit-patch-name-at-point)))))
1817 (apply 'stgit-run args)))))
1819 (error "No patch or file at point")))
1820 (with-current-buffer standard-output
1821 (goto-char (point-min))
1824 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1825 `(defun ,name (&optional ignore-whitespace)
1826 ,(format "Show the patch on the current line.
1828 %sWith a prefix argument, ignore whitespace. With a prefix argument
1829 greater than four (e.g., \\[universal-argument] \
1830 \\[universal-argument] \\[%s]), ignore all whitespace."
1832 (format "For unmerged files, %s.\n\n" unmerged-action)
1837 (stgit-show-patch ,diff-arg ignore-whitespace)))
1839 (stgit-define-diff stgit-diff
1841 (stgit-define-diff stgit-diff-ours
1843 "diff against our branch")
1844 (stgit-define-diff stgit-diff-theirs
1846 "diff against their branch")
1847 (stgit-define-diff stgit-diff-base
1849 "diff against the merge base")
1850 (stgit-define-diff stgit-diff-combined
1852 "show a combined diff")
1854 (defun stgit-diff-range (&optional ignore-whitespace)
1855 "Show diff for the range of patches between point and the marked patch.
1857 With a prefix argument, ignore whitespace. With a prefix argument
1858 greater than four (e.g., \\[universal-argument] \
1859 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
1862 (unless (= (length stgit-marked-patches) 1)
1863 (error "Need exactly one patch marked"))
1864 (let* ((patches (stgit-sort-patches (cons (stgit-patch-name-at-point t t)
1865 stgit-marked-patches)
1867 (first-patch (car patches))
1868 (second-patch (if (cdr patches) (cadr patches) first-patch))
1869 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
1870 (applied (stgit-applied-patchsyms t)))
1871 (unless (and (memq first-patch applied) (memq second-patch applied))
1872 (error "Can only show diff range for applied patches"))
1873 (stgit-capture-output (format "*StGit diff %s..%s*"
1874 first-patch second-patch)
1875 (apply 'stgit-run-git (append '("diff" "--patch-with-stat")
1876 (and whitespace-arg (list whitespace-arg))
1877 (list (format "%s^" (stgit-id first-patch))
1878 (stgit-id second-patch))))
1879 (with-current-buffer standard-output
1880 (goto-char (point-min))
1883 (defun stgit-move-change-to-index (file &optional force)
1884 "Copies the work tree state of FILE to index, using git add or git rm.
1886 If FORCE is not nil, use --force."
1887 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1888 '("add") '("rm" "-q"))))
1889 (stgit-capture-output "*git output*"
1890 (apply 'stgit-run-git (append op (and force '("--force"))
1891 '("--") (list file))))))
1893 (defun stgit-remove-change-from-index (file)
1894 "Unstages the change in FILE from the index"
1895 (stgit-capture-output "*git output*"
1896 (stgit-run-git "reset" "-q" "--" file)))
1898 (defun stgit-git-index-unmerged-p ()
1900 (with-output-to-string
1901 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1903 "--quiet" "HEAD")))))
1906 (defun stgit-file-toggle-index ()
1907 "Move modified file in or out of the index.
1909 Leaves the point where it is, but moves the mark to where the
1910 file ended up. You can then jump to the file with \
1911 \\[exchange-point-and-mark]."
1914 (let* ((patched-file (or (stgit-patched-file-at-point)
1915 (error "No file on the current line")))
1916 (patched-status (stgit-file->status patched-file)))
1917 (when (eq patched-status 'unmerged)
1918 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1919 (let* ((patch (stgit-patch-at-point))
1920 (patch-name (stgit-patch->name patch))
1921 (mark-file (if (eq patched-status 'rename)
1922 (stgit-file->cr-to patched-file)
1923 (stgit-file->file patched-file)))
1924 (point-file (if (eq patched-status 'rename)
1925 (stgit-file->cr-from patched-file)
1926 (stgit-neighbour-file))))
1928 (cond ((eq patch-name :work)
1929 (stgit-move-change-to-index (stgit-file->file patched-file)
1930 (eq patched-status 'ignore)))
1931 ((eq patch-name :index)
1932 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1934 (error "Can only move files between working tree and index")))
1935 (stgit-refresh-worktree)
1936 (stgit-refresh-index)
1937 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1939 (stgit-goto-patch patch-name point-file))))
1941 (defun stgit-toggle-index ()
1942 "Move change in or out of the index.
1944 Works on index and work tree, as well as files in either.
1946 Leaves the point where it is, but moves the mark to where the
1947 file ended up. You can then jump to the file with \
1948 \\[exchange-point-and-mark]."
1951 (if (stgit-patched-file-at-point)
1952 (stgit-file-toggle-index)
1953 (let ((patch-name (stgit-patch-name-at-point)))
1954 (unless (memq patch-name '(:index :work))
1955 (error "Can only move changes between working tree and index"))
1956 (when (stgit-git-index-unmerged-p)
1957 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1958 (if (if (eq patch-name :index)
1959 (stgit-index-empty-p)
1960 (stgit-work-tree-empty-p))
1961 (message "No changes to be moved")
1962 (stgit-capture-output nil
1963 (if (eq patch-name :work)
1964 (stgit-run-git "add" "--update")
1965 (stgit-run-git "reset" "--mixed" "-q")))
1966 (stgit-refresh-worktree)
1967 (stgit-refresh-index))
1968 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1970 (defun stgit-edit ()
1971 "Edit the patch on the current line."
1974 (let ((patchsym (stgit-patch-name-at-point t t))
1975 (edit-buf (get-buffer-create "*StGit edit*"))
1976 (dir default-directory))
1977 (log-edit 'stgit-confirm-edit t nil edit-buf)
1978 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1979 (setq default-directory dir)
1980 (let ((standard-output edit-buf))
1982 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1984 (defun stgit-confirm-edit ()
1986 (let ((file (make-temp-file "stgit-edit-")))
1987 (write-region (point-min) (point-max) file)
1988 (stgit-capture-output nil
1989 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1990 (with-current-buffer log-edit-parent-buffer
1993 (defun stgit-new (add-sign &optional refresh)
1994 "Create a new patch.
1995 With a prefix argument, include a \"Signed-off-by:\" line at the
1999 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2000 (dir default-directory))
2001 (log-edit 'stgit-confirm-new t nil edit-buf)
2002 (setq default-directory dir)
2003 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2006 (let ((standard-output (current-buffer)))
2007 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2009 (defun stgit-confirm-new ()
2011 (let ((file (make-temp-file "stgit-edit-"))
2012 (refresh stgit-refresh-after-new))
2013 (write-region (point-min) (point-max) file)
2014 (stgit-capture-output nil
2015 (stgit-run "new" "-f" file))
2016 (with-current-buffer log-edit-parent-buffer
2021 (defun stgit-new-and-refresh (add-sign)
2022 "Create a new patch and refresh it with the current changes.
2024 With a prefix argument, include a \"Signed-off-by:\" line at the
2027 This works just like running `stgit-new' followed by `stgit-refresh'."
2030 (stgit-new add-sign t))
2032 (defun stgit-create-patch-name (description)
2033 "Create a patch name from a long description"
2035 (while (> (length description) 0)
2036 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2037 (setq patch (downcase (concat patch
2038 (match-string 0 description))))
2039 (setq description (substring description (match-end 0))))
2040 ((string-match "\\` +" description)
2041 (setq patch (concat patch "-"))
2042 (setq description (substring description (match-end 0))))
2043 ((string-match "\\`[^a-zA-Z_-]+" description)
2044 (setq description (substring description (match-end 0))))))
2045 (cond ((= (length patch) 0)
2047 ((> (length patch) 20)
2048 (substring patch 0 20))
2051 (defun stgit-delete (patchsyms &optional spill-p)
2052 "Delete the patches in PATCHSYMS.
2053 Interactively, delete the marked patches, or the patch at point.
2055 With a prefix argument, or SPILL-P, spill the patch contents to
2056 the work tree and index."
2057 (interactive (list (stgit-patches-marked-or-at-point t t)
2058 current-prefix-arg))
2061 (error "No patches to delete"))
2062 (when (memq :index patchsyms)
2063 (error "Cannot delete the index"))
2064 (when (memq :work patchsyms)
2065 (error "Cannot delete the work tree"))
2067 (let ((npatches (length patchsyms)))
2068 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2070 (if (= 1 npatches) "" "es")
2072 " (spilling contents to index)"
2074 (let ((args (if spill-p
2075 (cons "--spill" patchsyms)
2077 (stgit-capture-output nil
2078 (apply 'stgit-run "delete" args))
2081 (defun stgit-move-patches-target ()
2082 "Return the patchsym indicating a target patch for
2083 `stgit-move-patches'.
2085 This is either the first unmarked patch at or after point, or one
2086 of :top and :bottom if the point is after or before the applied
2092 (let ((patchsym (stgit-patch-name-at-point)))
2093 (cond ((memq patchsym '(:work :index)) (setq result :top))
2094 (patchsym (if (memq patchsym stgit-marked-patches)
2096 (setq result patchsym)))
2097 ((re-search-backward "^>" nil t) (setq result :top))
2098 (t (setq result :bottom)))))
2101 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2102 "Returns the list of patches in PATCHSYMS sorted according to
2103 their position in the patch series, bottommost first.
2105 PATCHSYMS must not contain duplicate entries, unless
2106 ALLOW-DUPLICATES is not nil."
2107 (let (sorted-patchsyms
2108 (series (with-output-to-string
2109 (with-current-buffer standard-output
2110 (stgit-run-silent "series" "--noprefix"))))
2112 (while (string-match "^\\(.+\\)" series start)
2113 (let ((patchsym (intern (match-string 1 series))))
2114 (when (memq patchsym patchsyms)
2115 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2116 (setq start (match-end 0)))
2117 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2119 (unless allow-duplicates
2120 (unless (= (length patchsyms) (length sorted-patchsyms))
2121 (error "Internal error")))
2125 (defun stgit-move-patches (patchsyms target-patch)
2126 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2127 If TARGET-PATCH is :bottom or :top, move the patches to the
2128 bottom or top of the stack, respectively.
2130 Interactively, move the marked patches to where the point is."
2131 (interactive (list stgit-marked-patches
2132 (stgit-move-patches-target)))
2135 (error "Need at least one patch to move"))
2137 (unless target-patch
2138 (error "Point not at a patch"))
2140 ;; need to have patchsyms sorted by position in the stack
2141 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2142 (stgit-capture-output nil
2143 (if (eq target-patch :top)
2144 (apply 'stgit-run "float" sorted-patchsyms)
2147 (append (unless (eq target-patch :bottom)
2148 (list "--to" target-patch))
2150 sorted-patchsyms)))))
2153 (defun stgit-squash (patchsyms)
2154 "Squash the patches in PATCHSYMS.
2155 Interactively, squash the marked patches.
2157 Unless there are any conflicts, the patches will be merged into
2158 one patch, which will occupy the same spot in the series as the
2159 deepest patch had before the squash."
2160 (interactive (list stgit-marked-patches))
2162 (when (< (length patchsyms) 2)
2163 (error "Need at least two patches to squash"))
2164 (let ((stgit-buffer (current-buffer))
2165 (edit-buf (get-buffer-create "*StGit edit*"))
2166 (dir default-directory)
2167 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2168 (log-edit 'stgit-confirm-squash t nil edit-buf)
2169 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2170 (setq default-directory dir)
2171 (let ((result (let ((standard-output edit-buf))
2173 (apply 'stgit-run-silent "squash"
2174 "--save-template=-" sorted-patchsyms)))))
2176 ;; stg squash may have reordered the patches or caused conflicts
2177 (with-current-buffer stgit-buffer
2180 (unless (eq 0 result)
2182 (rename-buffer "*StGit error*")
2183 (resize-temp-buffer-window)
2184 (switch-to-buffer-other-window stgit-buffer)
2185 (error "stg squash failed")))))
2187 (defun stgit-confirm-squash ()
2189 (let ((file (make-temp-file "stgit-edit-")))
2190 (write-region (point-min) (point-max) file)
2191 (stgit-capture-output nil
2192 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2193 (with-current-buffer log-edit-parent-buffer
2195 ;; Go to first marked patch and stay there
2196 (goto-char (point-min))
2197 (re-search-forward (concat "^[>+-]\\*") nil t)
2198 (move-to-column goal-column)
2199 (let ((pos (point)))
2203 (defun stgit-help ()
2204 "Display help for the StGit mode."
2206 (describe-function 'stgit-mode))
2208 (defun stgit-undo-or-redo (redo hard)
2209 "Run stg undo or, if REDO is non-nil, stg redo.
2211 If HARD is non-nil, use the --hard flag."
2213 (let ((cmd (if redo "redo" "undo")))
2214 (stgit-capture-output nil
2216 (when (or (and (stgit-index-empty-p)
2217 (stgit-work-tree-empty-p))
2218 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2220 (stgit-run cmd "--hard"))
2224 (defun stgit-undo (&optional arg)
2226 With prefix argument, run it with the --hard flag.
2228 See also `stgit-redo'."
2230 (stgit-undo-or-redo nil arg))
2232 (defun stgit-redo (&optional arg)
2234 With prefix argument, run it with the --hard flag.
2236 See also `stgit-undo'."
2238 (stgit-undo-or-redo t arg))
2240 (defun stgit-refresh (&optional arg)
2242 If the index contains any changes, only refresh from index.
2244 With prefix argument, refresh the marked patch or the patch under point."
2247 (let ((patchargs (if arg
2248 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2249 (when (> (length patches) 1)
2250 (error "Too many patches marked"))
2251 (cons "-p" patches))
2253 (unless (stgit-index-empty-p)
2254 (setq patchargs (cons "--index" patchargs)))
2255 (stgit-capture-output nil
2256 (apply 'stgit-run "refresh" patchargs))
2257 (stgit-refresh-git-status))
2260 (defvar stgit-show-worktree nil
2261 "If nil, inhibit showing work tree and index in the stgit buffer.
2263 See also `stgit-show-worktree-mode'.")
2265 (defvar stgit-show-ignored nil
2266 "If nil, inhibit showing files ignored by git.")
2268 (defvar stgit-show-unknown nil
2269 "If nil, inhibit showing files not registered with git.")
2271 (defvar stgit-show-patch-names t
2272 "If nil, inhibit showing patch names.")
2274 (defun stgit-toggle-worktree (&optional arg)
2275 "Toggle the visibility of the work tree.
2276 With ARG, show the work tree if ARG is positive.
2278 Its initial setting is controlled by `stgit-default-show-worktree'.
2280 `stgit-show-worktree-mode' controls where on screen the index and
2281 work tree will show up."
2284 (setq stgit-show-worktree
2287 (not stgit-show-worktree)))
2290 (defun stgit-toggle-ignored (&optional arg)
2291 "Toggle the visibility of files ignored by git in the work
2292 tree. With ARG, show these files if ARG is positive.
2294 Use \\[stgit-toggle-worktree] to show the work tree."
2297 (setq stgit-show-ignored
2300 (not stgit-show-ignored)))
2303 (defun stgit-toggle-unknown (&optional arg)
2304 "Toggle the visibility of files not registered with git in the
2305 work tree. With ARG, show these files if ARG is positive.
2307 Use \\[stgit-toggle-worktree] to show the work tree."
2310 (setq stgit-show-unknown
2313 (not stgit-show-unknown)))
2316 (defun stgit-toggle-patch-names (&optional arg)
2317 "Toggle the visibility of patch names. With ARG, show patch names
2320 The initial setting is controlled by `stgit-default-show-patch-names'."
2323 (setq stgit-show-patch-names
2326 (not stgit-show-patch-names)))