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 (let (standard-output)
746 (stgit-run-git "update-index" "--refresh"))
747 `("diff-files" "-0" ,@args))
748 ((eq patchsym :index)
749 `("diff-index" ,@args "--cached" "HEAD"))
751 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
753 (when (and (eq patchsym :work))
754 (when stgit-show-ignored
755 (stgit-insert-ls-files '("--ignored" "--others") "I"))
756 (when stgit-show-unknown
757 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
760 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
761 (point-min) (point-max)))
763 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
765 (unless (ewoc-nth ewoc 0)
768 (propertize "<no files>"
769 'face 'stgit-description-face)
773 (defun stgit-find-file (&optional other-window)
774 (let* ((file (or (stgit-patched-file-at-point)
775 (error "No file at point")))
776 (filename (expand-file-name (stgit-file->file file))))
777 (unless (file-exists-p filename)
778 (error "File does not exist"))
779 (funcall (if other-window 'find-file-other-window 'find-file)
781 (when (eq (stgit-file->status file) 'unmerged)
784 (defun stgit-expand (&optional patches collapse)
785 "Show the contents of marked patches, or the patch at point.
787 See also `stgit-collapse'.
789 Non-interactively, operate on PATCHES, and collapse instead of
790 expand if COLLAPSE is not nil."
791 (interactive (list (stgit-patches-marked-or-at-point t)))
793 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
794 patches stgit-expanded-patches)))
795 (setq stgit-expanded-patches
797 (set-difference stgit-expanded-patches patches-diff)
798 (append stgit-expanded-patches patches-diff)))
799 (ewoc-map #'(lambda (patch)
800 (memq (stgit-patch->name patch) patches-diff))
802 (move-to-column (stgit-goal-column)))
804 (defun stgit-collapse (&optional patches)
805 "Hide the contents of marked patches, or the patch at point.
807 See also `stgit-expand'."
808 (interactive (list (stgit-patches-marked-or-at-point t)))
810 (stgit-expand patches t))
812 (defun stgit-select-patch ()
813 (let ((patchname (stgit-patch-name-at-point)))
814 (stgit-expand (list patchname)
815 (memq patchname stgit-expanded-patches))))
817 (defun stgit-expand-directory (file)
818 (let* ((patch (stgit-patch-at-point))
819 (ewoc (stgit-patch->files-ewoc patch))
820 (node (ewoc-locate ewoc))
821 (filename (stgit-file->file file))
822 (start (make-marker))
827 (set-marker start (point))
828 (set-marker end (point))
829 (set-marker-insertion-type end t))
831 (assert (string-match "/$" filename))
832 ;; remove trailing "/"
833 (setf (stgit-file->file file) (substring filename 0 -1))
834 (ewoc-invalidate ewoc node)
837 (let ((standard-output (current-buffer)))
838 (stgit-insert-ls-files (list "--directory" "--others"
839 "--no-empty-directory" "--"
842 (stgit-process-files (lambda (f)
843 (setq node (ewoc-enter-after ewoc node f))))))
845 (let ((inhibit-read-only t))
846 (put-text-property start end 'patch-data patch))))
848 (defun stgit-select-file ()
849 (let* ((file (or (stgit-patched-file-at-point)
850 (error "No file at point")))
851 (filename (stgit-file->file file)))
852 (if (string-match "/$" filename)
853 (stgit-expand-directory file)
856 (defun stgit-select ()
857 "With point on a patch, toggle showing files in the patch.
859 With point on a file, open the associated file. Opens the target
860 file for (applied) copies and renames."
863 (case (get-text-property (point) 'entry-type)
865 (stgit-select-patch))
869 (error "No patch or file on line"))))
871 (defun stgit-find-file-other-window ()
872 "Open file at point in other window"
877 (defun stgit-find-file-merge ()
878 "Open file at point and merge it using `smerge-ediff'."
885 "Hide the stgit buffer."
890 (defun stgit-git-status ()
891 "Show status using `git-status'."
894 (unless (fboundp 'git-status)
895 (error "The stgit-git-status command requires git-status"))
896 (let ((dir default-directory))
897 (save-selected-window
901 (defun stgit-goal-column ()
902 "Return goal column for the current line"
903 (case (get-text-property (point) 'entry-type)
908 (defun stgit-next-line (&optional arg)
909 "Move cursor vertically down ARG lines"
913 (move-to-column (stgit-goal-column)))
915 (defun stgit-previous-line (&optional arg)
916 "Move cursor vertically up ARG lines"
920 (move-to-column (stgit-goal-column)))
922 (defun stgit-next-patch (&optional arg)
923 "Move cursor down ARG patches."
926 (ewoc-goto-next stgit-ewoc (or arg 1))
927 (move-to-column goal-column))
929 (defun stgit-previous-patch (&optional arg)
930 "Move cursor up ARG patches."
933 (ewoc-goto-prev stgit-ewoc (or arg 1))
934 (move-to-column goal-column))
936 (defvar stgit-mode-hook nil
937 "Run after `stgit-mode' is setup.")
939 (defvar stgit-mode-map nil
940 "Keymap for StGit major mode.")
942 (unless stgit-mode-map
943 (let ((diff-map (make-sparse-keymap))
944 (toggle-map (make-sparse-keymap)))
945 (suppress-keymap diff-map)
946 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
947 '(("b" . stgit-diff-base)
948 ("c" . stgit-diff-combined)
949 ("m" . stgit-find-file-merge)
950 ("o" . stgit-diff-ours)
951 ("r" . stgit-diff-range)
952 ("t" . stgit-diff-theirs)))
953 (suppress-keymap toggle-map)
954 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
955 '(("n" . stgit-toggle-patch-names)
956 ("t" . stgit-toggle-worktree)
957 ("i" . stgit-toggle-ignored)
958 ("u" . stgit-toggle-unknown)))
959 (setq stgit-mode-map (make-keymap))
960 (suppress-keymap stgit-mode-map)
961 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
962 `((" " . stgit-mark-down)
963 ("m" . stgit-mark-down)
964 ("\d" . stgit-unmark-up)
965 ("u" . stgit-unmark-down)
968 ("\C-p" . stgit-previous-line)
969 ("\C-n" . stgit-next-line)
970 ([up] . stgit-previous-line)
971 ([down] . stgit-next-line)
972 ("p" . stgit-previous-patch)
973 ("n" . stgit-next-patch)
974 ("\M-{" . stgit-previous-patch)
975 ("\M-}" . stgit-next-patch)
976 ("s" . stgit-git-status)
977 ("g" . stgit-reload-or-repair)
978 ("r" . stgit-refresh)
979 ("\C-c\C-r" . stgit-rename)
981 ("M" . stgit-move-patches)
984 ("c" . stgit-new-and-refresh)
985 ("\C-c\C-c" . stgit-commit)
986 ("\C-c\C-u" . stgit-uncommit)
988 ("R" . stgit-resolve-file)
989 ("\r" . stgit-select)
991 ("-" . stgit-collapse)
992 ("o" . stgit-find-file-other-window)
993 ("i" . stgit-toggle-index)
994 (">" . stgit-push-next)
995 ("<" . stgit-pop-next)
996 ("P" . stgit-push-or-pop)
1000 ([?\C-/] . stgit-undo)
1001 ("\C-_" . stgit-undo)
1002 ([?\C-c ?\C-/] . stgit-redo)
1003 ("\C-c\C-_" . stgit-redo)
1004 ("B" . stgit-branch)
1005 ("\C-c\C-b" . stgit-rebase)
1008 ("q" . stgit-quit))))
1010 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1011 (and file (eq (stgit-file->status file)
1013 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1014 (easy-menu-define stgit-menu stgit-mode-map
1017 ["Reload" stgit-reload-or-repair
1018 :help "Reload StGit status from disk"]
1019 ["Repair" stgit-repair
1020 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1021 :help "Repair StGit metadata"]
1023 ["Undo" stgit-undo t]
1024 ["Redo" stgit-redo t]
1026 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1028 ["New patch" stgit-new-and-refresh
1029 :help "Create a new patch from changes in index or work tree"
1030 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1031 ["New empty patch" stgit-new
1032 :help "Create a new, empty patch"]
1033 ["(Un)mark patch" stgit-toggle-mark
1034 :label (if (memq (stgit-patch-name-at-point nil t)
1035 stgit-marked-patches)
1036 "Unmark patch" "Mark patch")
1037 :active (stgit-patch-name-at-point nil t)]
1038 ["Expand/collapse patch"
1039 (let ((patches (stgit-patches-marked-or-at-point)))
1040 (if (member-if ,patch-collapsed-p patches)
1041 (stgit-expand patches)
1042 (stgit-collapse patches)))
1043 :label (if (member-if ,patch-collapsed-p
1044 (stgit-patches-marked-or-at-point))
1047 :active (stgit-patches-marked-or-at-point)]
1048 ["Edit patch" stgit-edit
1049 :help "Edit patch comment"
1050 :active (stgit-patch-name-at-point nil t)]
1051 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1052 ["Push/pop patch" stgit-push-or-pop
1053 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1054 (stgit-applied-patchsyms t))
1055 "Pop patches" "Push patches")]
1056 ["Delete patches" stgit-delete
1057 :active (stgit-patches-marked-or-at-point nil t)]
1059 ["Move patches" stgit-move-patches
1060 :active stgit-marked-patches
1061 :help "Move marked patch(es) to point"]
1062 ["Squash patches" stgit-squash
1063 :active (> (length stgit-marked-patches) 1)
1064 :help "Merge marked patches into one"]
1066 ["Refresh top patch" stgit-refresh
1067 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1068 :help "Refresh the top patch with changes in index or work tree"]
1069 ["Refresh this patch" (stgit-refresh t)
1070 :keys "\\[universal-argument] \\[stgit-refresh]"
1071 :help "Refresh marked patch with changes in index or work tree"
1072 :active (and (not (and (stgit-index-empty-p)
1073 (stgit-work-tree-empty-p)))
1074 (stgit-patch-name-at-point nil t))]
1076 ["Find file" stgit-select
1077 :active (eq (get-text-property (point) 'entry-type) 'file)]
1078 ["Open file" stgit-find-file-other-window
1079 :active (eq (get-text-property (point) 'entry-type) 'file)]
1080 ["Toggle file index" stgit-toggle-index
1081 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1082 (memq (stgit-patch-name-at-point) '(:work :index)))
1083 :label (if (eq (stgit-patch-name-at-point) :work)
1084 "Move change to index"
1085 "Move change to work tree")]
1087 ["Show diff" stgit-diff
1088 :active (get-text-property (point) 'entry-type)]
1089 ["Show diff for range of applied patches" stgit-diff-range
1090 :active (= (length stgit-marked-patches) 1)]
1092 :active (stgit-git-index-unmerged-p)
1093 ["Combined diff" stgit-diff-combined
1094 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1095 ["Diff against base" stgit-diff-base
1096 :help "Show diff against the common base"
1097 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1098 ["Diff against ours" stgit-diff-ours
1099 :help "Show diff against our branch"
1100 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1101 ["Diff against theirs" stgit-diff-theirs
1102 :help "Show diff against their branch"
1103 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1105 ["Interactive merge" stgit-find-file-merge
1106 :help "Interactively merge the file"
1107 :active ,at-unmerged-file]
1108 ["Resolve file" stgit-resolve-file
1109 :help "Mark file conflict as resolved"
1110 :active ,at-unmerged-file]
1113 ["Show index & work tree" stgit-toggle-worktree :style toggle
1114 :selected stgit-show-worktree]
1115 ["Show unknown files" stgit-toggle-unknown :style toggle
1116 :selected stgit-show-unknown :active stgit-show-worktree]
1117 ["Show ignored files" stgit-toggle-ignored :style toggle
1118 :selected stgit-show-ignored :active stgit-show-worktree]
1119 ["Show patch names" stgit-toggle-patch-names :style toggle
1120 :selected stgit-show-patch-names]
1122 ["Switch branches" stgit-branch t
1123 :help "Switch to or create another branch"]
1124 ["Rebase branch" stgit-rebase t
1125 :help "Rebase the current branch"]
1128 ;; disable tool bar editing buttons
1129 (put 'stgit-mode 'mode-class 'special)
1131 (defun stgit-mode ()
1132 "Major mode for interacting with StGit.
1134 Start StGit using \\[stgit].
1138 \\[stgit-help] Show this help text
1139 \\[stgit-quit] Hide the StGit buffer
1140 \\[describe-bindings] Show all key bindings
1142 \\[stgit-reload-or-repair] Reload the StGit buffer
1143 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1145 \\[stgit-undo] Undo most recent StGit operation
1146 \\[stgit-redo] Undo recent undo
1148 \\[stgit-git-status] Run `git-status' (if available)
1151 \\[stgit-previous-line] Move to previous line
1152 \\[stgit-next-line] Move to next line
1153 \\[stgit-previous-patch] Move to previous patch
1154 \\[stgit-next-patch] Move to next patch
1156 \\[stgit-mark-down] Mark patch and move down
1157 \\[stgit-unmark-up] Unmark patch and move up
1158 \\[stgit-unmark-down] Unmark patch and move down
1160 Commands for patches:
1161 \\[stgit-select] Toggle showing changed files in patch
1162 \\[stgit-refresh] Refresh patch with changes in index or work tree
1163 \\[stgit-diff] Show the patch log and diff
1165 \\[stgit-expand] Show changes in marked patches
1166 \\[stgit-collapse] Hide changes in marked patches
1168 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1169 \\[stgit-new] Create a new, empty patch
1171 \\[stgit-rename] Rename patch
1172 \\[stgit-edit] Edit patch description
1173 \\[stgit-delete] Delete patch(es)
1175 \\[stgit-revert] Revert all changes in index or work tree
1176 \\[stgit-toggle-index] Toggle all changes between index and work tree
1178 \\[stgit-push-next] Push next patch onto stack
1179 \\[stgit-pop-next] Pop current patch from stack
1180 \\[stgit-push-or-pop] Push or pop marked patches
1181 \\[stgit-goto] Make patch at point current by popping or pushing
1183 \\[stgit-squash] Squash (meld together) patches
1184 \\[stgit-move-patches] Move marked patches to point
1186 \\[stgit-commit] Commit patch(es)
1187 \\[stgit-uncommit] Uncommit patch(es)
1190 \\[stgit-select] Open the file in this window
1191 \\[stgit-find-file-other-window] Open the file in another window
1192 \\[stgit-diff] Show the file's diff
1194 \\[stgit-toggle-index] Toggle change between index and work tree
1195 \\[stgit-revert] Revert changes to file
1198 \\[stgit-toggle-patch-names] Toggle showing patch names
1199 \\[stgit-toggle-worktree] Toggle showing index and work tree
1200 \\[stgit-toggle-unknown] Toggle showing unknown files
1201 \\[stgit-toggle-ignored] Toggle showing ignored files
1204 \\[stgit-diff] Show diff of patch or file
1205 \\[stgit-diff-range] Show diff for range of patches
1206 \\[stgit-diff-base] Show diff against the merge base
1207 \\[stgit-diff-ours] Show diff against our branch
1208 \\[stgit-diff-theirs] Show diff against their branch
1210 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1211 ignore space changes.
1212 With two prefix arguments (e.g., \\[universal-argument] \
1213 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1215 Commands for merge conflicts:
1216 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1217 \\[stgit-resolve-file] Mark unmerged file as resolved
1219 Commands for branches:
1220 \\[stgit-branch] Switch to or create another branch
1221 \\[stgit-rebase] Rebase the current branch
1223 Customization variables:
1224 `stgit-abbreviate-copies-and-renames'
1225 `stgit-default-show-patch-names'
1226 `stgit-default-show-worktree'
1227 `stgit-find-copies-harder'
1228 `stgit-show-worktree-mode'
1230 See also \\[customize-group] for the \"stgit\" group."
1231 (kill-all-local-variables)
1232 (buffer-disable-undo)
1233 (setq mode-name "StGit"
1234 major-mode 'stgit-mode
1236 (use-local-map stgit-mode-map)
1237 (set (make-local-variable 'list-buffers-directory) default-directory)
1238 (set (make-local-variable 'stgit-marked-patches) nil)
1239 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1240 (set (make-local-variable 'stgit-show-patch-names)
1241 stgit-default-show-patch-names)
1242 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1243 (set (make-local-variable 'stgit-index-node) nil)
1244 (set (make-local-variable 'stgit-worktree-node) nil)
1245 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1246 (set-variable 'truncate-lines 't)
1247 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1248 (unless stgit-did-advise
1250 (setq stgit-did-advise t))
1251 (run-hooks 'stgit-mode-hook))
1253 (defun stgit-advise-funlist (funlist)
1254 "Add advice to the functions in FUNLIST so we can refresh the
1255 stgit buffers as the git status of files change."
1258 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1259 (stgit-update-stgit-for-buffer t)))
1263 (defun stgit-advise ()
1264 "Add advice to appropriate (non-stgit) git functions so we can
1265 refresh the stgit buffers as the git status of files change."
1267 (let ((feature (car arg))
1268 (funlist (cdr arg)))
1269 (if (featurep feature)
1270 (stgit-advise-funlist funlist)
1271 (add-to-list 'after-load-alist
1272 `(,feature (stgit-advise-funlist
1273 (quote ,funlist)))))))
1274 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1275 (git git-add-file git-checkout git-revert-file git-remove-file))))
1277 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1278 "Refresh worktree status in any `stgit-mode' buffer that shows
1279 the status of the current buffer.
1281 If REFRESH-INDEX is not-nil, also update the index."
1282 (let* ((dir (cond ((eq major-mode 'git-status-mode)
1285 (file-name-directory
1286 (expand-file-name buffer-file-name)))))
1287 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1289 (buffer (and gitdir (stgit-find-buffer gitdir))))
1291 (with-current-buffer buffer
1292 (stgit-refresh-worktree)
1293 (when refresh-index (stgit-refresh-index))))))
1295 (defun stgit-add-mark (patchsym)
1296 "Mark the patch PATCHSYM."
1297 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1299 (defun stgit-remove-mark (patchsym)
1300 "Unmark the patch PATCHSYM."
1301 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1303 (defun stgit-clear-marks ()
1304 "Unmark all patches."
1305 (setq stgit-marked-patches '()))
1307 (defun stgit-patch-at-point (&optional cause-error)
1308 (get-text-property (point) 'patch-data))
1310 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1311 "Return the patch name on the current line as a symbol.
1312 If CAUSE-ERROR is not nil, signal an error if none found.
1313 If ONLY-PATCHES is not nil, only allow real patches, and not
1314 index or work tree."
1315 (let ((patch (stgit-patch-at-point)))
1318 (memq (stgit-patch->status patch) '(work index))
1321 (stgit-patch->name patch))
1323 (error "No patch on this line")))))
1325 (defun stgit-patched-file-at-point ()
1326 (get-text-property (point) 'file-data))
1328 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1329 "Return the symbols of the marked patches, or the patch on the current line.
1330 If CAUSE-ERRROR is not nil, signal an error if none found.
1331 If ONLY-PATCHES is not nil, do not include index or work tree."
1332 (if stgit-marked-patches
1333 stgit-marked-patches
1334 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1335 (cond (patch (list patch))
1336 (cause-error (error "No patches marked or at this line"))
1339 (defun stgit-goto-patch (patchsym &optional file)
1340 "Move point to the line containing patch PATCHSYM.
1341 If that patch cannot be found, do nothing.
1343 If the patch was found and FILE is not nil, instead move to that
1344 file's line. If FILE cannot be found, stay on the line of
1346 (let ((node (ewoc-nth stgit-ewoc 0)))
1347 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1349 (setq node (ewoc-next stgit-ewoc node)))
1350 (when (and node file)
1351 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1352 (file-node (ewoc-nth file-ewoc 0)))
1353 (while (and file-node (not (equal (stgit-file->file (ewoc-data file-node)) file)))
1354 (setq file-node (ewoc-next file-ewoc file-node)))
1356 (ewoc-goto-node file-ewoc file-node)
1357 (move-to-column (stgit-goal-column))
1360 (ewoc-goto-node stgit-ewoc node)
1361 (move-to-column goal-column))))
1363 (defun stgit-init ()
1367 (stgit-capture-output nil
1371 (defun stgit-toggle-mark ()
1372 "Toggle mark on the patch under point."
1375 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1379 (defun stgit-mark ()
1380 "Mark the patch under point."
1383 (let* ((node (ewoc-locate stgit-ewoc))
1384 (patch (ewoc-data node))
1385 (name (stgit-patch->name patch)))
1386 (when (eq name :work)
1387 (error "Cannot mark the work tree"))
1388 (when (eq name :index)
1389 (error "Cannot mark the index"))
1390 (stgit-add-mark (stgit-patch->name patch))
1391 (let ((column (current-column)))
1392 (ewoc-invalidate stgit-ewoc node)
1393 (move-to-column column))))
1395 (defun stgit-mark-down ()
1396 "Mark the patch under point and move to the next patch."
1401 (defun stgit-unmark ()
1402 "Remove mark from the patch on the current line."
1405 (let* ((node (ewoc-locate stgit-ewoc))
1406 (patch (ewoc-data node)))
1407 (stgit-remove-mark (stgit-patch->name patch))
1408 (let ((column (current-column)))
1409 (ewoc-invalidate stgit-ewoc node)
1410 (move-to-column column))))
1412 (defun stgit-unmark-up ()
1413 "Remove mark from the patch on the previous line."
1416 (stgit-previous-patch)
1419 (defun stgit-unmark-down ()
1420 "Remove mark from the patch on the current line."
1426 (defun stgit-rename (name)
1427 "Rename the patch under point to NAME."
1429 (read-string "Patch name: "
1430 (symbol-name (stgit-patch-name-at-point t t)))))
1432 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1433 (stgit-capture-output nil
1434 (stgit-run "rename" old-patchsym name))
1435 (let ((name-sym (intern name)))
1436 (when (memq old-patchsym stgit-expanded-patches)
1437 (setq stgit-expanded-patches
1438 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1439 (when (memq old-patchsym stgit-marked-patches)
1440 (setq stgit-marked-patches
1441 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1443 (stgit-goto-patch name-sym))))
1445 (defun stgit-reload-or-repair (repair)
1446 "Update the contents of the StGit buffer (`stgit-reload').
1448 With a prefix argument, repair the StGit metadata if the branch
1449 was modified with git commands (`stgit-repair')."
1456 (defun stgit-repair ()
1460 (stgit-capture-output nil
1461 (stgit-run "repair"))
1464 (defun stgit-available-branches (&optional all)
1465 "Returns a list of the names of the available stg branches as strings.
1467 If ALL is not nil, also return non-stgit branches."
1468 (let ((output (with-output-to-string
1469 (stgit-run "branch" "--list")))
1470 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1474 (while (string-match pattern output start)
1475 (setq result (cons (match-string 1 output) result))
1476 (setq start (match-end 0)))
1479 (defun stgit-branch (branch)
1480 "Switch to or create branch BRANCH."
1481 (interactive (list (completing-read "Switch to branch: "
1482 (stgit-available-branches))))
1484 (when (cond ((equal branch (stgit-current-branch))
1485 (error "Branch is already current"))
1486 ((member branch (stgit-available-branches t))
1487 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1489 ((not (string-match stgit-allowed-branch-name-re branch))
1490 (error "Invalid branch name"))
1491 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1492 (stgit-capture-output nil (stgit-run "branch" "--create" "--"
1497 (defun stgit-available-refs (&optional omit-stgit)
1498 "Returns a list of the available git refs.
1499 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1500 (let* ((output (with-output-to-string
1501 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1502 "refs/tags" "refs/heads"
1504 (result (split-string output "\n" t)))
1506 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1507 (substring s (match-end 0))
1510 (delete-if (lambda (s)
1511 (string-match "^refs/heads/.*\\.stgit$" s))
1515 (defun stgit-parent-branch ()
1516 "Return the parent branch of the current stg branch as per
1517 git-config setting branch.<branch>.stgit.parentbranch."
1518 (let ((output (with-output-to-string
1519 (stgit-run-git-silent "config"
1520 (format "branch.%s.stgit.parentbranch"
1521 (stgit-current-branch))))))
1522 (when (string-match ".*" output)
1523 (match-string 0 output))))
1525 (defun stgit-rebase (new-base)
1526 "Rebase the current branch to NEW-BASE.
1528 Interactively, first ask which branch to rebase to. Defaults to
1529 what git-config branch.<branch>.stgit.parentbranch is set to."
1530 (interactive (list (completing-read "Rebase to: "
1531 (stgit-available-refs t)
1533 (stgit-parent-branch))))
1535 (stgit-capture-output nil (stgit-run "rebase" new-base))
1538 (defun stgit-commit (count)
1539 "Run stg commit on COUNT commits.
1540 Interactively, the prefix argument is used as COUNT.
1541 A negative COUNT will uncommit instead."
1545 (stgit-uncommit (- count))
1546 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1549 (defun stgit-uncommit (count)
1550 "Run stg uncommit on COUNT commits.
1551 Interactively, the prefix argument is used as COUNT.
1552 A negative COUNT will commit instead."
1556 (stgit-commit (- count))
1557 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1560 (defun stgit-neighbour-file ()
1561 "Return the file name of the next file after point, or the
1562 previous file if point is at the last file within a patch."
1563 (let ((old-point (point))
1565 (and (zerop (forward-line 1))
1566 (let ((f (stgit-patched-file-at-point)))
1567 (and f (setq neighbour-file (stgit-file->file f)))))
1568 (goto-char old-point)
1569 (unless neighbour-file
1570 (and (zerop (forward-line -1))
1571 (let ((f (stgit-patched-file-at-point)))
1572 (and f (setq neighbour-file (stgit-file->file f)))))
1573 (goto-char old-point))
1576 (defun stgit-revert-file ()
1577 "Revert the file at point, which must be in the index or the
1581 (let* ((patched-file (or (stgit-patched-file-at-point)
1582 (error "No file on the current line")))
1583 (patch-name (stgit-patch-name-at-point))
1584 (file-status (stgit-file->status patched-file))
1585 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1586 (stgit-file->cr-to patched-file))
1587 ((eq file-status 'add)
1588 (stgit-file->file patched-file))))
1589 (co-file (cond ((eq file-status 'rename)
1590 (stgit-file->cr-from patched-file))
1591 ((not (memq file-status '(copy add)))
1592 (stgit-file->file patched-file))))
1593 (next-file (stgit-neighbour-file)))
1595 (unless (memq patch-name '(:work :index))
1596 (error "No index or working tree file on this line"))
1598 (when (eq file-status 'ignore)
1599 (error "Cannot revert ignored files"))
1601 (when (eq file-status 'unknown)
1602 (error "Cannot revert unknown files"))
1604 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1605 (when (yes-or-no-p (format "Revert %d file%s? "
1607 (if (= nfiles 1) "" "s")))
1608 (stgit-capture-output nil
1610 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1612 (stgit-run-git "checkout" "HEAD" co-file)))
1614 (stgit-goto-patch patch-name next-file)))))
1616 (defun stgit-revert ()
1617 "Revert the change at point, which must be the index, the work
1618 tree, or a single change in either."
1621 (let ((patched-file (stgit-patched-file-at-point)))
1624 (let* ((patch-name (or (stgit-patch-name-at-point)
1625 (error "No patch or file at point")))
1626 (patch-desc (case patch-name
1629 (t (error (substitute-command-keys
1630 "Use \\[stgit-delete] to delete a patch"))))))
1631 (when (if (eq patch-name :work)
1632 (stgit-work-tree-empty-p)
1633 (stgit-index-empty-p))
1634 (error (format "There are no changes in the %s to revert"
1636 (and (eq patch-name :index)
1637 (not (stgit-work-tree-empty-p))
1638 (error "Cannot revert index as work tree contains unstaged changes"))
1640 (when (yes-or-no-p (format "Revert all changes in the %s? "
1642 (if (eq patch-name :index)
1643 (stgit-run-git-silent "reset" "--hard" "-q")
1644 (stgit-run-git-silent "checkout" "--" "."))
1645 (stgit-refresh-index)
1646 (stgit-refresh-worktree)
1647 (stgit-goto-patch patch-name))))))
1649 (defun stgit-resolve-file ()
1650 "Resolve conflict in the file at point."
1653 (let* ((patched-file (stgit-patched-file-at-point))
1654 (patch (stgit-patch-at-point))
1655 (patch-name (and patch (stgit-patch->name patch)))
1656 (status (and patched-file (stgit-file->status patched-file))))
1658 (unless (memq patch-name '(:work :index))
1659 (error "No index or working tree file on this line"))
1661 (unless (eq status 'unmerged)
1662 (error "No conflict to resolve at the current line"))
1664 (stgit-capture-output nil
1665 (stgit-move-change-to-index (stgit-file->file patched-file)))
1669 (defun stgit-push-or-pop-patches (do-push npatches)
1670 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1671 NPATCHES patches, or all patches if NPATCHES is t."
1673 (stgit-capture-output nil
1675 (if do-push "push" "pop")
1678 (list "-n" npatches))))
1680 (stgit-refresh-git-status))
1682 (defun stgit-push-next (npatches)
1683 "Push the first unapplied patch.
1684 With numeric prefix argument, push that many patches."
1686 (stgit-push-or-pop-patches t npatches))
1688 (defun stgit-pop-next (npatches)
1689 "Pop the topmost applied patch.
1690 With numeric prefix argument, pop that many patches.
1692 If NPATCHES is t, pop all patches."
1694 (stgit-push-or-pop-patches nil npatches))
1696 (defun stgit-applied-patches (&optional only-patches)
1697 "Return a list of the applied patches.
1699 If ONLY-PATCHES is not nil, exclude index and work tree."
1700 (let ((states (if only-patches
1702 '(applied top index work)))
1704 (ewoc-map (lambda (patch)
1705 (when (memq (stgit-patch->status patch) states)
1706 (setq result (cons patch result)))
1711 (defun stgit-applied-patchsyms (&optional only-patches)
1712 "Return a list of the symbols of the applied patches.
1714 If ONLY-PATCHES is not nil, exclude index and work tree."
1715 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1717 (defun stgit-push-or-pop ()
1718 "Push or pop the marked patches."
1721 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1722 (applied-syms (stgit-applied-patchsyms t))
1723 (unapplied (set-difference patchsyms applied-syms)))
1724 (stgit-capture-output nil
1726 (if unapplied "push" "pop")
1728 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1731 (defun stgit-goto-target ()
1732 "Return the goto target a point; either a patchsym, :top,
1734 (let ((patchsym (stgit-patch-name-at-point)))
1735 (cond ((memq patchsym '(:work :index)) nil)
1737 ((not (next-single-property-change (point) 'patch-data))
1739 ((not (previous-single-property-change (point) 'patch-data))
1742 (defun stgit-goto ()
1743 "Go to the patch on the current line.
1745 Push or pop patches to make this patch topmost. Push or pop all
1746 patches if used on a line after or before all patches."
1749 (let ((patchsym (stgit-goto-target)))
1751 (error "No patch to go to on this line"))
1753 (:top (stgit-push-or-pop-patches t t))
1754 (:bottom (stgit-push-or-pop-patches nil t))
1755 (t (stgit-capture-output nil
1756 (stgit-run "goto" patchsym))
1759 (defun stgit-id (patchsym)
1760 "Return the git commit id for PATCHSYM.
1761 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1762 (if (keywordp patchsym)
1764 (let ((result (with-output-to-string
1765 (stgit-run-silent "id" patchsym))))
1766 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1767 (error "Cannot find commit id for %s" patchsym))
1768 (match-string 1 result))))
1770 (defun stgit-whitespace-diff-arg (arg)
1772 (cond ((> arg 4) "--ignore-all-space")
1773 ((> arg 1) "--ignore-space-change"))))
1775 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1776 "Show the patch on the current line.
1778 UNMERGED-STAGE is the argument to `git-diff' that that selects
1779 which stage to diff against in the case of unmerged files."
1780 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
1781 (patch-name (stgit-patch-name-at-point t)))
1782 (stgit-capture-output "*StGit patch*"
1783 (case (get-text-property (point) 'entry-type)
1785 (let* ((patched-file (stgit-patched-file-at-point))
1786 (patch-id (let ((id (stgit-id patch-name)))
1787 (if (and (eq id :index)
1788 (eq (stgit-file->status patched-file)
1792 (args (append (and space-arg (list space-arg))
1793 (and (stgit-file->cr-from patched-file)
1794 (list (stgit-find-copies-harder-diff-arg)))
1795 (cond ((eq patch-id :index)
1797 ((eq patch-id :work)
1798 (list unmerged-stage))
1800 (list (concat patch-id "^") patch-id)))
1802 (if (stgit-file->copy-or-rename patched-file)
1803 (list (stgit-file->cr-from patched-file)
1804 (stgit-file->cr-to patched-file))
1805 (list (stgit-file->file patched-file))))))
1806 (apply 'stgit-run-git "diff" args)))
1808 (let* ((patch-id (stgit-id patch-name)))
1809 (if (or (eq patch-id :index) (eq patch-id :work))
1810 (apply 'stgit-run-git "diff"
1811 (stgit-find-copies-harder-diff-arg)
1812 (append (and space-arg (list space-arg))
1813 (if (eq patch-id :index)
1815 (list unmerged-stage))))
1816 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1817 (and space-arg (list "-O" space-arg))
1818 (list (stgit-patch-name-at-point)))))
1819 (apply 'stgit-run args)))))
1821 (error "No patch or file at point")))
1822 (with-current-buffer standard-output
1823 (goto-char (point-min))
1826 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1827 `(defun ,name (&optional ignore-whitespace)
1828 ,(format "Show the patch on the current line.
1830 %sWith a prefix argument, ignore whitespace. With a prefix argument
1831 greater than four (e.g., \\[universal-argument] \
1832 \\[universal-argument] \\[%s]), ignore all whitespace."
1834 (format "For unmerged files, %s.\n\n" unmerged-action)
1839 (stgit-show-patch ,diff-arg ignore-whitespace)))
1841 (stgit-define-diff stgit-diff
1843 (stgit-define-diff stgit-diff-ours
1845 "diff against our branch")
1846 (stgit-define-diff stgit-diff-theirs
1848 "diff against their branch")
1849 (stgit-define-diff stgit-diff-base
1851 "diff against the merge base")
1852 (stgit-define-diff stgit-diff-combined
1854 "show a combined diff")
1856 (defun stgit-diff-range (&optional ignore-whitespace)
1857 "Show diff for the range of patches between point and the marked patch.
1859 With a prefix argument, ignore whitespace. With a prefix argument
1860 greater than four (e.g., \\[universal-argument] \
1861 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
1864 (unless (= (length stgit-marked-patches) 1)
1865 (error "Need exactly one patch marked"))
1866 (let* ((patches (stgit-sort-patches (cons (stgit-patch-name-at-point t t)
1867 stgit-marked-patches)
1869 (first-patch (car patches))
1870 (second-patch (if (cdr patches) (cadr patches) first-patch))
1871 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
1872 (applied (stgit-applied-patchsyms t)))
1873 (unless (and (memq first-patch applied) (memq second-patch applied))
1874 (error "Can only show diff range for applied patches"))
1875 (stgit-capture-output (format "*StGit diff %s..%s*"
1876 first-patch second-patch)
1877 (apply 'stgit-run-git (append '("diff" "--patch-with-stat")
1878 (and whitespace-arg (list whitespace-arg))
1879 (list (format "%s^" (stgit-id first-patch))
1880 (stgit-id second-patch))))
1881 (with-current-buffer standard-output
1882 (goto-char (point-min))
1885 (defun stgit-move-change-to-index (file &optional force)
1886 "Copies the work tree state of FILE to index, using git add or git rm.
1888 If FORCE is not nil, use --force."
1889 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1890 '("add") '("rm" "-q"))))
1891 (stgit-capture-output "*git output*"
1892 (apply 'stgit-run-git (append op (and force '("--force"))
1893 '("--") (list file))))))
1895 (defun stgit-remove-change-from-index (file)
1896 "Unstages the change in FILE from the index"
1897 (stgit-capture-output "*git output*"
1898 (stgit-run-git "reset" "-q" "--" file)))
1900 (defun stgit-git-index-unmerged-p ()
1902 (with-output-to-string
1903 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1905 "--quiet" "HEAD")))))
1908 (defun stgit-file-toggle-index ()
1909 "Move modified file in or out of the index.
1911 Leaves the point where it is, but moves the mark to where the
1912 file ended up. You can then jump to the file with \
1913 \\[exchange-point-and-mark]."
1916 (let* ((patched-file (or (stgit-patched-file-at-point)
1917 (error "No file on the current line")))
1918 (patched-status (stgit-file->status patched-file)))
1919 (when (eq patched-status 'unmerged)
1920 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1921 (let* ((patch (stgit-patch-at-point))
1922 (patch-name (stgit-patch->name patch))
1923 (mark-file (if (eq patched-status 'rename)
1924 (stgit-file->cr-to patched-file)
1925 (stgit-file->file patched-file)))
1926 (point-file (if (eq patched-status 'rename)
1927 (stgit-file->cr-from patched-file)
1928 (stgit-neighbour-file))))
1930 (cond ((eq patch-name :work)
1931 (stgit-move-change-to-index (stgit-file->file patched-file)
1932 (eq patched-status 'ignore)))
1933 ((eq patch-name :index)
1934 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1936 (error "Can only move files between working tree and index")))
1937 (stgit-refresh-worktree)
1938 (stgit-refresh-index)
1939 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1941 (stgit-goto-patch patch-name point-file))))
1943 (defun stgit-toggle-index ()
1944 "Move change in or out of the index.
1946 Works on index and work tree, as well as files in either.
1948 Leaves the point where it is, but moves the mark to where the
1949 file ended up. You can then jump to the file with \
1950 \\[exchange-point-and-mark]."
1953 (if (stgit-patched-file-at-point)
1954 (stgit-file-toggle-index)
1955 (let ((patch-name (stgit-patch-name-at-point)))
1956 (unless (memq patch-name '(:index :work))
1957 (error "Can only move changes between working tree and index"))
1958 (when (stgit-git-index-unmerged-p)
1959 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1960 (if (if (eq patch-name :index)
1961 (stgit-index-empty-p)
1962 (stgit-work-tree-empty-p))
1963 (message "No changes to be moved")
1964 (stgit-capture-output nil
1965 (if (eq patch-name :work)
1966 (stgit-run-git "add" "--update")
1967 (stgit-run-git "reset" "--mixed" "-q")))
1968 (stgit-refresh-worktree)
1969 (stgit-refresh-index))
1970 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1972 (defun stgit-edit ()
1973 "Edit the patch on the current line."
1976 (let ((patchsym (stgit-patch-name-at-point t t))
1977 (edit-buf (get-buffer-create "*StGit edit*"))
1978 (dir default-directory))
1979 (log-edit 'stgit-confirm-edit t nil edit-buf)
1980 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1981 (setq default-directory dir)
1982 (let ((standard-output edit-buf))
1984 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1986 (defun stgit-confirm-edit ()
1988 (let ((file (make-temp-file "stgit-edit-")))
1989 (write-region (point-min) (point-max) file)
1990 (stgit-capture-output nil
1991 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1992 (with-current-buffer log-edit-parent-buffer
1995 (defun stgit-new (add-sign &optional refresh)
1996 "Create a new patch.
1997 With a prefix argument, include a \"Signed-off-by:\" line at the
2001 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2002 (dir default-directory))
2003 (log-edit 'stgit-confirm-new t nil edit-buf)
2004 (setq default-directory dir)
2005 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2008 (let ((standard-output (current-buffer)))
2009 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2011 (defun stgit-confirm-new ()
2013 (let ((file (make-temp-file "stgit-edit-"))
2014 (refresh stgit-refresh-after-new))
2015 (write-region (point-min) (point-max) file)
2016 (stgit-capture-output nil
2017 (stgit-run "new" "-f" file))
2018 (with-current-buffer log-edit-parent-buffer
2023 (defun stgit-new-and-refresh (add-sign)
2024 "Create a new patch and refresh it with the current changes.
2026 With a prefix argument, include a \"Signed-off-by:\" line at the
2029 This works just like running `stgit-new' followed by `stgit-refresh'."
2032 (stgit-new add-sign t))
2034 (defun stgit-create-patch-name (description)
2035 "Create a patch name from a long description"
2037 (while (> (length description) 0)
2038 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2039 (setq patch (downcase (concat patch
2040 (match-string 0 description))))
2041 (setq description (substring description (match-end 0))))
2042 ((string-match "\\` +" description)
2043 (setq patch (concat patch "-"))
2044 (setq description (substring description (match-end 0))))
2045 ((string-match "\\`[^a-zA-Z_-]+" description)
2046 (setq description (substring description (match-end 0))))))
2047 (cond ((= (length patch) 0)
2049 ((> (length patch) 20)
2050 (substring patch 0 20))
2053 (defun stgit-delete (patchsyms &optional spill-p)
2054 "Delete the patches in PATCHSYMS.
2055 Interactively, delete the marked patches, or the patch at point.
2057 With a prefix argument, or SPILL-P, spill the patch contents to
2058 the work tree and index."
2059 (interactive (list (stgit-patches-marked-or-at-point t t)
2060 current-prefix-arg))
2063 (error "No patches to delete"))
2064 (when (memq :index patchsyms)
2065 (error "Cannot delete the index"))
2066 (when (memq :work patchsyms)
2067 (error "Cannot delete the work tree"))
2069 (let ((npatches (length patchsyms)))
2070 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2072 (if (= 1 npatches) "" "es")
2074 " (spilling contents to index)"
2076 (let ((args (if spill-p
2077 (cons "--spill" patchsyms)
2079 (stgit-capture-output nil
2080 (apply 'stgit-run "delete" args))
2083 (defun stgit-move-patches-target ()
2084 "Return the patchsym indicating a target patch for
2085 `stgit-move-patches'.
2087 This is either the first unmarked patch at or after point, or one
2088 of :top and :bottom if the point is after or before the applied
2094 (let ((patchsym (stgit-patch-name-at-point)))
2095 (cond ((memq patchsym '(:work :index)) (setq result :top))
2096 (patchsym (if (memq patchsym stgit-marked-patches)
2098 (setq result patchsym)))
2099 ((re-search-backward "^>" nil t) (setq result :top))
2100 (t (setq result :bottom)))))
2103 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2104 "Returns the list of patches in PATCHSYMS sorted according to
2105 their position in the patch series, bottommost first.
2107 PATCHSYMS must not contain duplicate entries, unless
2108 ALLOW-DUPLICATES is not nil."
2109 (let (sorted-patchsyms
2110 (series (with-output-to-string
2111 (with-current-buffer standard-output
2112 (stgit-run-silent "series" "--noprefix"))))
2114 (while (string-match "^\\(.+\\)" series start)
2115 (let ((patchsym (intern (match-string 1 series))))
2116 (when (memq patchsym patchsyms)
2117 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2118 (setq start (match-end 0)))
2119 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2121 (unless allow-duplicates
2122 (unless (= (length patchsyms) (length sorted-patchsyms))
2123 (error "Internal error")))
2127 (defun stgit-move-patches (patchsyms target-patch)
2128 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2129 If TARGET-PATCH is :bottom or :top, move the patches to the
2130 bottom or top of the stack, respectively.
2132 Interactively, move the marked patches to where the point is."
2133 (interactive (list stgit-marked-patches
2134 (stgit-move-patches-target)))
2137 (error "Need at least one patch to move"))
2139 (unless target-patch
2140 (error "Point not at a patch"))
2142 ;; need to have patchsyms sorted by position in the stack
2143 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2144 (stgit-capture-output nil
2145 (if (eq target-patch :top)
2146 (apply 'stgit-run "float" sorted-patchsyms)
2149 (append (unless (eq target-patch :bottom)
2150 (list "--to" target-patch))
2152 sorted-patchsyms)))))
2155 (defun stgit-squash (patchsyms)
2156 "Squash the patches in PATCHSYMS.
2157 Interactively, squash the marked patches.
2159 Unless there are any conflicts, the patches will be merged into
2160 one patch, which will occupy the same spot in the series as the
2161 deepest patch had before the squash."
2162 (interactive (list stgit-marked-patches))
2164 (when (< (length patchsyms) 2)
2165 (error "Need at least two patches to squash"))
2166 (let ((stgit-buffer (current-buffer))
2167 (edit-buf (get-buffer-create "*StGit edit*"))
2168 (dir default-directory)
2169 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2170 (log-edit 'stgit-confirm-squash t nil edit-buf)
2171 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2172 (setq default-directory dir)
2173 (let ((result (let ((standard-output edit-buf))
2175 (apply 'stgit-run-silent "squash"
2176 "--save-template=-" sorted-patchsyms)))))
2178 ;; stg squash may have reordered the patches or caused conflicts
2179 (with-current-buffer stgit-buffer
2182 (unless (eq 0 result)
2184 (rename-buffer "*StGit error*")
2185 (resize-temp-buffer-window)
2186 (switch-to-buffer-other-window stgit-buffer)
2187 (error "stg squash failed")))))
2189 (defun stgit-confirm-squash ()
2191 (let ((file (make-temp-file "stgit-edit-")))
2192 (write-region (point-min) (point-max) file)
2193 (stgit-capture-output nil
2194 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2195 (with-current-buffer log-edit-parent-buffer
2197 ;; Go to first marked patch and stay there
2198 (goto-char (point-min))
2199 (re-search-forward (concat "^[>+-]\\*") nil t)
2200 (move-to-column goal-column)
2201 (let ((pos (point)))
2205 (defun stgit-help ()
2206 "Display help for the StGit mode."
2208 (describe-function 'stgit-mode))
2210 (defun stgit-undo-or-redo (redo hard)
2211 "Run stg undo or, if REDO is non-nil, stg redo.
2213 If HARD is non-nil, use the --hard flag."
2215 (let ((cmd (if redo "redo" "undo")))
2216 (stgit-capture-output nil
2218 (when (or (and (stgit-index-empty-p)
2219 (stgit-work-tree-empty-p))
2220 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2222 (stgit-run cmd "--hard"))
2226 (defun stgit-undo (&optional arg)
2228 With prefix argument, run it with the --hard flag.
2230 See also `stgit-redo'."
2232 (stgit-undo-or-redo nil arg))
2234 (defun stgit-redo (&optional arg)
2236 With prefix argument, run it with the --hard flag.
2238 See also `stgit-undo'."
2240 (stgit-undo-or-redo t arg))
2242 (defun stgit-refresh (&optional arg)
2244 If the index contains any changes, only refresh from index.
2246 With prefix argument, refresh the marked patch or the patch under point."
2249 (let ((patchargs (if arg
2250 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2251 (when (> (length patches) 1)
2252 (error "Too many patches marked"))
2253 (cons "-p" patches))
2255 (unless (stgit-index-empty-p)
2256 (setq patchargs (cons "--index" patchargs)))
2257 (stgit-capture-output nil
2258 (apply 'stgit-run "refresh" patchargs))
2259 (stgit-refresh-git-status))
2262 (defvar stgit-show-worktree nil
2263 "If nil, inhibit showing work tree and index in the stgit buffer.
2265 See also `stgit-show-worktree-mode'.")
2267 (defvar stgit-show-ignored nil
2268 "If nil, inhibit showing files ignored by git.")
2270 (defvar stgit-show-unknown nil
2271 "If nil, inhibit showing files not registered with git.")
2273 (defvar stgit-show-patch-names t
2274 "If nil, inhibit showing patch names.")
2276 (defun stgit-toggle-worktree (&optional arg)
2277 "Toggle the visibility of the work tree.
2278 With ARG, show the work tree if ARG is positive.
2280 Its initial setting is controlled by `stgit-default-show-worktree'.
2282 `stgit-show-worktree-mode' controls where on screen the index and
2283 work tree will show up."
2286 (setq stgit-show-worktree
2289 (not stgit-show-worktree)))
2292 (defun stgit-toggle-ignored (&optional arg)
2293 "Toggle the visibility of files ignored by git in the work
2294 tree. With ARG, show these files if ARG is positive.
2296 Use \\[stgit-toggle-worktree] to show the work tree."
2299 (setq stgit-show-ignored
2302 (not stgit-show-ignored)))
2305 (defun stgit-toggle-unknown (&optional arg)
2306 "Toggle the visibility of files not registered with git in the
2307 work tree. With ARG, show these files if ARG is positive.
2309 Use \\[stgit-toggle-worktree] to show the work tree."
2312 (setq stgit-show-unknown
2315 (not stgit-show-unknown)))
2318 (defun stgit-toggle-patch-names (&optional arg)
2319 "Toggle the visibility of patch names. With ARG, show patch names
2322 The initial setting is controlled by `stgit-default-show-patch-names'."
2325 (setq stgit-show-patch-names
2328 (not stgit-show-patch-names)))