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"))
21 "Manage StGit patches for the tree in DIR.
23 See `stgit-mode' for commands available."
24 (interactive "DDirectory: \n")
25 (switch-to-stgit-buffer (git-get-top-dir dir))
28 (defun stgit-assert-mode ()
29 "Signal an error if not in an StGit buffer."
30 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
32 (unless (fboundp 'git-get-top-dir)
33 (defun git-get-top-dir (dir)
34 "Retrieve the top-level directory of a git tree."
35 (let ((cdup (with-output-to-string
36 (with-current-buffer standard-output
38 (unless (eq 0 (call-process "git" nil t nil
39 "rev-parse" "--show-cdup"))
40 (error "Cannot find top-level git tree for %s" dir))))))
41 (expand-file-name (concat (file-name-as-directory dir)
42 (car (split-string cdup "\n")))))))
44 (defun stgit-refresh-git-status (&optional dir)
45 "If it exists, refresh the `git-status' buffer belonging to
46 directory DIR or `default-directory'"
47 (when (and (fboundp 'git-find-status-buffer)
48 (fboundp 'git-refresh-status))
49 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
50 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
51 (when git-status-buffer
52 (with-current-buffer git-status-buffer
53 (git-refresh-status))))))
55 (defun stgit-find-buffer (dir)
56 "Return the buffer displaying StGit patches for DIR, or nil if none."
57 (setq dir (file-name-as-directory dir))
58 (let ((buffers (buffer-list)))
60 (not (with-current-buffer (car buffers)
61 (and (eq major-mode 'stgit-mode)
62 (string= default-directory dir)))))
63 (setq buffers (cdr buffers)))
64 (and buffers (car buffers))))
66 (defun switch-to-stgit-buffer (dir)
67 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
68 (setq dir (file-name-as-directory dir))
69 (let ((buffer (stgit-find-buffer dir)))
70 (switch-to-buffer (or buffer
71 (create-stgit-buffer dir)))))
73 (defstruct (stgit-patch)
74 status name desc empty files-ewoc)
76 (defun stgit-patch-pp (patch)
77 (let* ((status (stgit-patch-status patch))
79 (name (stgit-patch-name patch))
80 (face (cdr (assq status stgit-patch-status-face-alist))))
86 (if (memq name stgit-marked-patches)
88 (if (memq status '(index work))
89 (insert (propertize (if (eq status 'index) "Index" "Work tree")
91 (insert (format "%-30s"
92 (propertize (symbol-name name)
94 'syntax-table (string-to-syntax "w")))
96 (if (stgit-patch-empty patch) "(empty) " "")
97 (propertize (or (stgit-patch-desc patch) "")
98 'face 'stgit-description-face)))
100 (put-text-property start (point) 'entry-type 'patch)
101 (when (memq name stgit-expanded-patches)
102 (stgit-insert-patch-files patch))
103 (put-text-property start (point) 'patch-data patch)))
105 (defun create-stgit-buffer (dir)
106 "Create a buffer for showing StGit patches.
107 Argument DIR is the repository path."
108 (let ((buf (create-file-buffer (concat dir "*stgit*")))
109 (inhibit-read-only t))
110 (with-current-buffer buf
111 (setq default-directory dir)
113 (set (make-local-variable 'stgit-ewoc)
114 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
115 (setq buffer-read-only t))
118 (defmacro stgit-capture-output (name &rest body)
119 "Capture StGit output and, if there was any output, show it in a window
121 Returns nil if there was no output."
122 (declare (debug ([&or stringp null] body))
124 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
125 (stgit-dir default-directory)
126 (inhibit-read-only t))
127 (with-current-buffer output-buf
129 (setq default-directory stgit-dir)
130 (setq buffer-read-only t))
131 (let ((standard-output output-buf))
133 (with-current-buffer output-buf
134 (set-buffer-modified-p nil)
135 (setq buffer-read-only t)
136 (if (< (point-min) (point-max))
137 (display-buffer output-buf t)))))
139 (defun stgit-make-run-args (args)
140 "Return a copy of ARGS with its elements converted to strings."
142 ;; don't use (format "%s" ...) to limit type errors
143 (cond ((stringp x) x)
144 ((integerp x) (number-to-string x))
145 ((symbolp x) (symbol-name x))
147 (error "Bad element in stgit-make-run-args args: %S" x))))
150 (defun stgit-run-silent (&rest args)
151 (setq args (stgit-make-run-args args))
152 (apply 'call-process "stg" nil standard-output nil args))
154 (defun stgit-run (&rest args)
155 (setq args (stgit-make-run-args args))
156 (let ((msgcmd (mapconcat #'identity args " ")))
157 (message "Running stg %s..." msgcmd)
158 (apply 'call-process "stg" nil standard-output nil args)
159 (message "Running stg %s...done" msgcmd)))
161 (defun stgit-run-git (&rest args)
162 (setq args (stgit-make-run-args args))
163 (let ((msgcmd (mapconcat #'identity args " ")))
164 (message "Running git %s..." msgcmd)
165 (apply 'call-process "git" nil standard-output nil args)
166 (message "Running git %s...done" msgcmd)))
168 (defun stgit-run-git-silent (&rest args)
169 (setq args (stgit-make-run-args args))
170 (apply 'call-process "git" nil standard-output nil args))
172 (defun stgit-index-empty-p ()
173 "Returns non-nil if the index contains no changes from HEAD."
174 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
176 (defun stgit-work-tree-empty-p ()
177 "Returns non-nil if the work tree contains no changes from index."
178 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
180 (defvar stgit-index-node)
181 (defvar stgit-worktree-node)
183 (defun stgit-refresh-index ()
184 (when stgit-index-node
185 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
187 (defun stgit-refresh-worktree ()
188 (when stgit-worktree-node
189 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
191 (defun stgit-run-series-insert-index (ewoc)
192 (setq index-node (cons ewoc (ewoc-enter-last ewoc
198 worktree-node (cons ewoc (ewoc-enter-last ewoc
205 (defun stgit-run-series (ewoc)
206 (setq stgit-index-node nil
207 stgit-worktree-node nil)
208 (let ((inserted-index (not stgit-show-worktree))
213 (let* ((standard-output (current-buffer))
214 (exit-status (stgit-run-silent "series"
215 "--description" "--empty")))
216 (goto-char (point-min))
217 (if (not (zerop exit-status))
218 (cond ((looking-at "stg series: \\(.*\\)")
219 (setq inserted-index t)
220 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
221 (substitute-command-keys
222 "-- not initialized; run \\[stgit-init]")))
224 (error "Error running stg: %s"
228 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
229 (error "Syntax error in output from stg series"))
230 (let* ((state-str (match-string 2))
231 (state (cond ((string= state-str ">") 'top)
232 ((string= state-str "+") 'applied)
233 ((string= state-str "-") 'unapplied)))
234 (name (intern (match-string 4)))
235 (desc (match-string 5))
236 (empty (string= (match-string 1) "0")))
237 (unless inserted-index
238 (when (or (eq stgit-show-worktree-mode 'top)
239 (and (eq stgit-show-worktree-mode 'center)
240 (eq state 'unapplied)))
241 (setq inserted-index t)
242 (stgit-run-series-insert-index ewoc)))
243 (setq all-patchsyms (cons name all-patchsyms))
244 (ewoc-enter-last ewoc
251 (unless inserted-index
252 (stgit-run-series-insert-index ewoc)))
253 (setq stgit-index-node index-node
254 stgit-worktree-node worktree-node
255 stgit-marked-patches (intersection stgit-marked-patches
258 (defun stgit-reload ()
259 "Update the contents of the StGit buffer."
262 (let ((inhibit-read-only t)
263 (curline (line-number-at-pos))
264 (curpatch (stgit-patch-name-at-point))
265 (curfile (stgit-patched-file-at-point)))
266 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
267 (ewoc-set-hf stgit-ewoc
270 (substring (with-output-to-string
271 (stgit-run-silent "branch"))
273 'face 'stgit-branch-name-face)
275 (if stgit-show-worktree
278 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
279 shows the working tree\n")
280 'face 'stgit-description-face)))
281 (stgit-run-series stgit-ewoc)
283 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
284 (goto-line curline)))
285 (stgit-refresh-git-status))
287 (defun stgit-set-default (symbol value)
288 "Set default value of SYMBOL to VALUE using `set-default' and
289 reload all StGit buffers."
290 (set-default symbol value)
291 (dolist (buf (buffer-list))
292 (with-current-buffer buf
293 (when (eq major-mode 'stgit-mode)
297 "A user interface for the StGit patch maintenance tool."
299 :link '(function-link stgit)
300 :link '(url-link "http://www.procode.org/stgit/"))
302 (defcustom stgit-abbreviate-copies-and-renames t
303 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
304 instead of \"dir/old/file -> dir/new/file\"."
307 :set 'stgit-set-default)
309 (defcustom stgit-default-show-worktree t
310 "Set to non-nil to by default show the working tree in a new stgit buffer.
312 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
315 :link '(variable-link stgit-show-worktree))
317 (defcustom stgit-find-copies-harder nil
318 "Try harder to find copied files when listing patches.
320 When not nil, runs git diff-tree with the --find-copies-harder
321 flag, which reduces performance."
324 :set 'stgit-set-default)
326 (defcustom stgit-show-worktree-mode 'center
327 "This variable controls where the \"Index\" and \"Work tree\"
328 will be shown on in the buffer.
330 It can be set to 'top (above all patches), 'center (show between
331 applied and unapplied patches), and 'bottom (below all patches)."
332 :type '(radio (const :tag "above all patches (top)" top)
333 (const :tag "between applied and unapplied patches (center)"
335 (const :tag "below all patches (bottom)" bottom))
337 :link '(variable-link stgit-show-worktree)
338 :set 'stgit-set-default)
340 (defface stgit-branch-name-face
342 "The face used for the StGit branch name"
345 (defface stgit-top-patch-face
346 '((((background dark)) (:weight bold :foreground "yellow"))
347 (((background light)) (:weight bold :foreground "purple"))
349 "The face used for the top patch names"
352 (defface stgit-applied-patch-face
353 '((((background dark)) (:foreground "light yellow"))
354 (((background light)) (:foreground "purple"))
356 "The face used for applied patch names"
359 (defface stgit-unapplied-patch-face
360 '((((background dark)) (:foreground "gray80"))
361 (((background light)) (:foreground "orchid"))
363 "The face used for unapplied patch names"
366 (defface stgit-description-face
367 '((((background dark)) (:foreground "tan"))
368 (((background light)) (:foreground "dark red")))
369 "The face used for StGit descriptions"
372 (defface stgit-index-work-tree-title-face
373 '((((supports :slant italic)) :slant italic)
375 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
378 (defface stgit-unmerged-file-face
379 '((((class color) (background light)) (:foreground "red" :bold t))
380 (((class color) (background dark)) (:foreground "red" :bold t)))
381 "StGit mode face used for unmerged file status"
384 (defface stgit-unknown-file-face
385 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
386 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
387 "StGit mode face used for unknown file status"
390 (defface stgit-ignored-file-face
391 '((((class color) (background light)) (:foreground "grey60"))
392 (((class color) (background dark)) (:foreground "grey40")))
393 "StGit mode face used for ignored files")
395 (defface stgit-file-permission-face
396 '((((class color) (background light)) (:foreground "green" :bold t))
397 (((class color) (background dark)) (:foreground "green" :bold t)))
398 "StGit mode face used for permission changes."
401 (defface stgit-modified-file-face
402 '((((class color) (background light)) (:foreground "purple"))
403 (((class color) (background dark)) (:foreground "salmon")))
404 "StGit mode face used for modified file status"
407 (defconst stgit-file-status-code-strings
408 (mapcar (lambda (arg)
410 (propertize (cadr arg) 'face (car (cddr arg)))))
411 '((add "Added" stgit-modified-file-face)
412 (copy "Copied" stgit-modified-file-face)
413 (delete "Deleted" stgit-modified-file-face)
414 (modify "Modified" stgit-modified-file-face)
415 (rename "Renamed" stgit-modified-file-face)
416 (mode-change "Mode change" stgit-modified-file-face)
417 (unmerged "Unmerged" stgit-unmerged-file-face)
418 (unknown "Unknown" stgit-unknown-file-face)
419 (ignore "Ignored" stgit-ignored-file-face)))
420 "Alist of code symbols to description strings")
422 (defconst stgit-patch-status-face-alist
423 '((applied . stgit-applied-patch-face)
424 (top . stgit-top-patch-face)
425 (unapplied . stgit-unapplied-patch-face)
426 (index . stgit-index-work-tree-title-face)
427 (work . stgit-index-work-tree-title-face))
428 "Alist of face to use for a given patch status")
430 (defun stgit-file-status-code-as-string (file)
431 "Return stgit status code for FILE as a string"
432 (let* ((code (assq (stgit-file-status file)
433 stgit-file-status-code-strings))
434 (score (stgit-file-cr-score file)))
437 (if (and score (/= score 100))
438 (format "%s %s" (cdr code)
439 (propertize (format "%d%%" score)
440 'face 'stgit-description-face))
443 (defun stgit-file-status-code (str &optional score)
444 "Return stgit status code from git status string"
445 (let ((code (assoc str '(("A" . add)
454 (setq code (if code (cdr code) 'unknown))
455 (when (stringp score)
456 (if (> (length score) 0)
457 (setq score (string-to-number score))
459 (if score (cons code score) code)))
461 (defconst stgit-file-type-strings
464 (#o160 . "subproject"))
465 "Alist of names of file types")
467 (defun stgit-file-type-string (type)
468 "Return string describing file type TYPE (the high bits of file permission).
469 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
470 (let ((type-str (assoc type stgit-file-type-strings)))
471 (or (and type-str (cdr type-str))
472 (format "unknown type %o" type))))
474 (defun stgit-file-type-change-string (old-perm new-perm)
475 "Return string describing file type change from OLD-PERM to NEW-PERM.
476 Cf. `stgit-file-type-string'."
477 (let ((old-type (lsh old-perm -9))
478 (new-type (lsh new-perm -9)))
479 (cond ((= old-type new-type) "")
480 ((zerop new-type) "")
482 (if (= new-type #o100)
484 (format " (%s)" (stgit-file-type-string new-type))))
485 (t (format " (%s -> %s)"
486 (stgit-file-type-string old-type)
487 (stgit-file-type-string new-type))))))
489 (defun stgit-file-mode-change-string (old-perm new-perm)
490 "Return string describing file mode change from OLD-PERM to NEW-PERM.
491 Cf. `stgit-file-type-change-string'."
492 (setq old-perm (logand old-perm #o777)
493 new-perm (logand new-perm #o777))
494 (if (or (= old-perm new-perm)
498 (let* ((modified (logxor old-perm new-perm))
499 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
500 (cond ((zerop modified) "")
501 ((and (zerop not-x-modified)
502 (or (and (eq #o111 (logand old-perm #o111))
503 (propertize "-x" 'face 'stgit-file-permission-face))
504 (and (eq #o111 (logand new-perm #o111))
505 (propertize "+x" 'face
506 'stgit-file-permission-face)))))
507 (t (concat (propertize (format "%o" old-perm)
508 'face 'stgit-file-permission-face)
510 'face 'stgit-description-face)
511 (propertize (format "%o" new-perm)
512 'face 'stgit-file-permission-face)))))))
514 (defstruct (stgit-file)
515 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
517 (defun stgit-describe-copy-or-rename (file)
518 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
519 from to common-head common-tail)
521 (when stgit-abbreviate-copies-and-renames
522 (setq from (split-string (stgit-file-cr-from file) "/")
523 to (split-string (stgit-file-cr-to file) "/"))
525 (while (and from to (cdr from) (cdr to)
526 (string-equal (car from) (car to)))
527 (setq common-head (cons (car from) common-head)
530 (setq common-head (nreverse common-head)
533 (while (and from to (cdr from) (cdr to)
534 (string-equal (car from) (car to)))
535 (setq common-tail (cons (car from) common-tail)
538 (setq from (nreverse from)
541 (if (or common-head common-tail)
542 (concat (if common-head
543 (mapconcat #'identity common-head "/")
545 (if common-head "/" "")
546 (propertize "{" 'face 'stgit-description-face)
547 (mapconcat #'identity from "/")
549 (mapconcat #'identity to "/")
550 (propertize "}" 'face 'stgit-description-face)
551 (if common-tail "/" "")
553 (mapconcat #'identity common-tail "/")
555 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
557 (defun stgit-file-pp (file)
558 (let ((status (stgit-file-status file))
559 (name (if (stgit-file-copy-or-rename file)
560 (stgit-describe-copy-or-rename file)
561 (stgit-file-file file)))
562 (mode-change (stgit-file-mode-change-string
563 (stgit-file-old-perm file)
564 (stgit-file-new-perm file)))
566 (insert (format " %-12s%s%s%s%s\n"
567 (stgit-file-status-code-as-string file)
569 (if (zerop (length mode-change)) "" " ")
571 (propertize (stgit-file-type-change-string
572 (stgit-file-old-perm file)
573 (stgit-file-new-perm file))
574 'face 'stgit-description-face)))
575 (add-text-properties start (point)
576 (list 'entry-type 'file
579 (defun stgit-find-copies-harder-diff-arg ()
580 "Return the flag to use with `git-diff' depending on the
581 `stgit-find-copies-harder' flag."
582 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
584 (defun stgit-insert-ls-files (args file-flag)
585 (let ((start (point)))
586 (apply 'stgit-run-git
587 (append '("ls-files" "--exclude-standard" "-z") args))
589 (while (looking-at "\\([^\0]*\\)\0")
590 (let ((name-len (- (match-end 0) (match-beginning 0))))
591 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
592 (forward-char name-len)))))
594 (defun stgit-insert-patch-files (patch)
595 "Expand (show modification of) the patch PATCH after the line
597 (let* ((patchsym (stgit-patch-name patch))
599 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
600 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
601 (set-marker-insertion-type end t)
602 (setf (stgit-patch-files-ewoc patch) ewoc)
604 (let ((standard-output (current-buffer)))
605 (apply 'stgit-run-git
606 (cond ((eq patchsym :work)
607 `("diff-files" "-0" ,@args))
608 ((eq patchsym :index)
609 `("diff-index" ,@args "--cached" "HEAD"))
611 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
613 (when (and (eq patchsym :work))
614 (when stgit-show-ignored
615 (stgit-insert-ls-files '("--ignored" "--others") "I"))
616 (when stgit-show-unknown
617 (stgit-insert-ls-files '("--others") "X"))
618 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
619 (point-min) (point-max)))
621 (goto-char (point-min))
622 (unless (or (eobp) (memq patchsym '(:work :index)))
624 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
625 (let ((old-perm (string-to-number (match-string 1) 8))
626 (new-perm (string-to-number (match-string 2) 8)))
627 (goto-char (match-end 0))
630 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
631 (let* ((patch-status (stgit-patch-status patch))
632 (file-subexp (if (eq patch-status 'unapplied)
635 (file (match-string file-subexp)))
640 :cr-score (string-to-number (match-string 2))
641 :cr-from (match-string 3)
642 :cr-to (match-string 4)
643 :status (stgit-file-status-code
646 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
654 :status (stgit-file-status-code
656 :file (match-string 2))))))
657 (goto-char (match-end 0))
658 (ewoc-enter-last ewoc file))))
660 (unless (ewoc-nth ewoc 0)
663 (propertize "<no files>"
664 'face 'stgit-description-face)
668 (defun stgit-find-file (&optional other-window)
669 (let* ((file (or (stgit-patched-file-at-point)
670 (error "No file at point")))
671 (filename (expand-file-name (stgit-file-file file))))
672 (unless (file-exists-p filename)
673 (error "File does not exist"))
674 (funcall (if other-window 'find-file-other-window 'find-file)
676 (when (eq (stgit-file-status file) 'unmerged)
679 (defun stgit-expand (&optional patches collapse)
680 "Show the contents selected patches, or the patch at point.
682 See also `stgit-collapse'.
684 Non-interactively, operate on PATCHES, and collapse instead of
685 expand if COLLAPSE is not nil."
686 (interactive (list (stgit-patches-marked-or-at-point)))
688 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
689 patches stgit-expanded-patches)))
690 (setq stgit-expanded-patches
692 (set-difference stgit-expanded-patches patches-diff)
693 (append stgit-expanded-patches patches-diff)))
694 (ewoc-map #'(lambda (patch)
695 (memq (stgit-patch-name patch) patches-diff))
697 (move-to-column (stgit-goal-column)))
699 (defun stgit-collapse (&optional patches)
700 "Hide the contents selected patches, or the patch at point.
702 See also `stgit-expand'."
703 (interactive (list (stgit-patches-marked-or-at-point)))
705 (stgit-expand patches t))
707 (defun stgit-select-patch ()
708 (let ((patchname (stgit-patch-name-at-point)))
709 (stgit-expand (list patchname)
710 (memq patchname stgit-expanded-patches))))
712 (defun stgit-select ()
713 "With point on a patch, toggle showing files in the patch.
715 With point on a file, open the associated file. Opens the target
716 file for (applied) copies and renames."
719 (case (get-text-property (point) 'entry-type)
721 (stgit-select-patch))
725 (error "No patch or file on line"))))
727 (defun stgit-find-file-other-window ()
728 "Open file at point in other window"
733 (defun stgit-find-file-merge ()
734 "Open file at point and merge it using `smerge-ediff'."
741 "Hide the stgit buffer."
746 (defun stgit-git-status ()
747 "Show status using `git-status'."
750 (unless (fboundp 'git-status)
751 (error "The stgit-git-status command requires git-status"))
752 (let ((dir default-directory))
753 (save-selected-window
757 (defun stgit-goal-column ()
758 "Return goal column for the current line"
759 (case (get-text-property (point) 'entry-type)
764 (defun stgit-next-line (&optional arg)
765 "Move cursor vertically down ARG lines"
769 (move-to-column (stgit-goal-column)))
771 (defun stgit-previous-line (&optional arg)
772 "Move cursor vertically up ARG lines"
776 (move-to-column (stgit-goal-column)))
778 (defun stgit-next-patch (&optional arg)
779 "Move cursor down ARG patches."
782 (ewoc-goto-next stgit-ewoc (or arg 1))
783 (move-to-column goal-column))
785 (defun stgit-previous-patch (&optional arg)
786 "Move cursor up ARG patches."
789 (ewoc-goto-prev stgit-ewoc (or arg 1))
790 (move-to-column goal-column))
792 (defvar stgit-mode-hook nil
793 "Run after `stgit-mode' is setup.")
795 (defvar stgit-mode-map nil
796 "Keymap for StGit major mode.")
798 (unless stgit-mode-map
799 (let ((diff-map (make-sparse-keymap))
800 (toggle-map (make-sparse-keymap)))
801 (suppress-keymap diff-map)
802 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
803 '(("b" . stgit-diff-base)
804 ("c" . stgit-diff-combined)
805 ("m" . stgit-find-file-merge)
806 ("o" . stgit-diff-ours)
807 ("t" . stgit-diff-theirs)))
808 (suppress-keymap toggle-map)
809 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
810 '(("t" . stgit-toggle-worktree)
811 ("i" . stgit-toggle-ignored)
812 ("u" . stgit-toggle-unknown)))
813 (setq stgit-mode-map (make-keymap))
814 (suppress-keymap stgit-mode-map)
815 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
816 `((" " . stgit-mark-down)
817 ("m" . stgit-mark-down)
818 ("\d" . stgit-unmark-up)
819 ("u" . stgit-unmark-down)
822 ("\C-p" . stgit-previous-line)
823 ("\C-n" . stgit-next-line)
824 ([up] . stgit-previous-line)
825 ([down] . stgit-next-line)
826 ("p" . stgit-previous-patch)
827 ("n" . stgit-next-patch)
828 ("\M-{" . stgit-previous-patch)
829 ("\M-}" . stgit-next-patch)
830 ("s" . stgit-git-status)
831 ("g" . stgit-reload-or-repair)
832 ("r" . stgit-refresh)
833 ("\C-c\C-r" . stgit-rename)
835 ("M" . stgit-move-patches)
838 ("c" . stgit-new-and-refresh)
839 ("\C-c\C-c" . stgit-commit)
840 ("\C-c\C-u" . stgit-uncommit)
842 ("R" . stgit-resolve-file)
843 ("\r" . stgit-select)
845 ("-" . stgit-collapse)
846 ("o" . stgit-find-file-other-window)
847 ("i" . stgit-toggle-index)
848 (">" . stgit-push-next)
849 ("<" . stgit-pop-next)
850 ("P" . stgit-push-or-pop)
854 ([?\C-/] . stgit-undo)
855 ("\C-_" . stgit-undo)
856 ([?\C-c ?\C-/] . stgit-redo)
857 ("\C-c\C-_" . stgit-redo)
859 ("\C-c\C-b" . stgit-rebase)
862 ("q" . stgit-quit))))
864 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
865 (and file (eq (stgit-file-status file)
867 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
868 (easy-menu-define stgit-menu stgit-mode-map
871 ["Reload" stgit-reload-or-repair
872 :help "Reload StGit status from disk"]
873 ["Repair" stgit-repair
874 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
875 :help "Repair StGit metadata"]
877 ["Undo" stgit-undo t]
878 ["Redo" stgit-redo t]
880 ["Git status" stgit-git-status :active (fboundp 'git-status)]
882 ["New patch" stgit-new-and-refresh
883 :help "Create a new patch from changes in index or work tree"
884 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
885 ["New empty patch" stgit-new
886 :help "Create a new, empty patch"]
887 ["(Un)mark patch" stgit-toggle-mark
888 :label (if (memq (stgit-patch-name-at-point nil t)
889 stgit-marked-patches)
890 "Unmark patch" "Mark patch")
891 :active (stgit-patch-name-at-point nil t)]
892 ["Expand/collapse patch"
893 (let ((patches (stgit-patches-marked-or-at-point)))
894 (if (member-if ,patch-collapsed-p patches)
895 (stgit-expand patches)
896 (stgit-collapse patches)))
897 :label (if (member-if ,patch-collapsed-p
898 (stgit-patches-marked-or-at-point))
901 :active (stgit-patches-marked-or-at-point)]
902 ["Edit patch" stgit-edit
903 :help "Edit patch comment"
904 :active (stgit-patch-name-at-point nil t)]
905 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
906 ["Push/pop patch" stgit-push-or-pop
907 :label (if (stgit-applied-at-point-p) "Pop patch" "Push patch")
908 :active (stgit-patch-name-at-point nil t)]
909 ["Delete patch" stgit-delete :active (stgit-patch-name-at-point nil t)]
911 ["Move patches" stgit-move-patches
912 :active stgit-marked-patches
913 :help "Move selected patch(es) to point"]
914 ["Squash patches" stgit-squash
915 :active (> (length stgit-marked-patches) 1)
916 :help "Merge selected patches into one"]
918 ["Refresh top patch" stgit-refresh
919 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
920 :help "Refresh the top patch with changes in index or work tree"]
921 ["Refresh this patch" (stgit-refresh t)
922 :keys "\\[universal-argument] \\[stgit-refresh]"
923 :help "Refresh the patch at point with changes in index or work tree"
924 :active (and (not (and (stgit-index-empty-p)
925 (stgit-work-tree-empty-p)))
926 (stgit-patch-name-at-point nil t))]
928 ["Find file" stgit-select
929 :active (eq (get-text-property (point) 'entry-type) 'file)]
930 ["Open file" stgit-find-file-other-window
931 :active (eq (get-text-property (point) 'entry-type) 'file)]
932 ["Toggle file index" stgit-toggle-index
933 :active (and (eq (get-text-property (point) 'entry-type) 'file)
934 (memq (stgit-patch-name-at-point) '(:work :index)))
935 :label (if (eq (stgit-patch-name-at-point) :work)
936 "Move change to index"
937 "Move change to work tree")]
939 ["Show diff" stgit-diff
940 :active (get-text-property (point) 'entry-type)]
942 :active (stgit-git-index-unmerged-p)
943 ["Combined diff" stgit-diff-combined
944 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
945 ["Diff against base" stgit-diff-base
946 :help "Show diff against the common base"
947 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
948 ["Diff against ours" stgit-diff-ours
949 :help "Show diff against our branch"
950 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
951 ["Diff against theirs" stgit-diff-theirs
952 :help "Show diff against their branch"
953 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
955 ["Interactive merge" stgit-find-file-merge
956 :help "Interactively merge the file"
957 :active ,at-unmerged-file]
958 ["Resolve file" stgit-resolve-file
959 :help "Mark file conflict as resolved"
960 :active ,at-unmerged-file]
963 ["Show index & work tree" stgit-toggle-worktree :style toggle
964 :selected stgit-show-worktree]
965 ["Show unknown files" stgit-toggle-unknown :style toggle
966 :selected stgit-show-unknown :active stgit-show-worktree]
967 ["Show ignored files" stgit-toggle-ignored :style toggle
968 :selected stgit-show-ignored :active stgit-show-worktree]
970 ["Switch branches" stgit-branch t
971 :help "Switch to another branch"]
972 ["Rebase branch" stgit-rebase t
973 :help "Rebase the current branch"]
976 ;; disable tool bar editing buttons
977 (put 'stgit-mode 'mode-class 'special)
980 "Major mode for interacting with StGit.
982 Start StGit using \\[stgit].
986 \\[stgit-help] Show this help text
987 \\[stgit-quit] Hide the StGit buffer
988 \\[describe-bindings] Show all key bindings
990 \\[stgit-reload-or-repair] Reload the StGit buffer
991 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
993 \\[stgit-undo] Undo most recent StGit operation
994 \\[stgit-redo] Undo recent undo
996 \\[stgit-git-status] Run `git-status' (if available)
999 \\[stgit-previous-line] Move to previous line
1000 \\[stgit-next-line] Move to next line
1001 \\[stgit-previous-patch] Move to previous patch
1002 \\[stgit-next-patch] Move to next patch
1004 \\[stgit-mark-down] Mark patch and move down
1005 \\[stgit-unmark-up] Unmark patch and move up
1006 \\[stgit-unmark-down] Unmark patch and move down
1008 Commands for patches:
1009 \\[stgit-select] Toggle showing changed files in patch
1010 \\[stgit-refresh] Refresh patch with changes in index or work tree
1011 \\[stgit-diff] Show the patch log and diff
1013 \\[stgit-expand] Show changes in selected patches
1014 \\[stgit-collapse] Hide changes in selected patches
1016 \\[stgit-new] Create a new, empty patch
1017 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1018 \\[stgit-rename] Rename patch
1019 \\[stgit-edit] Edit patch description
1020 \\[stgit-delete] Delete patch(es)
1022 \\[stgit-revert] Revert all changes in index or work tree
1023 \\[stgit-toggle-index] Toggle all changes between index and work tree
1025 \\[stgit-push-next] Push next patch onto stack
1026 \\[stgit-pop-next] Pop current patch from stack
1027 \\[stgit-push-or-pop] Push or pop patch at point
1028 \\[stgit-goto] Make current patch current by popping or pushing
1030 \\[stgit-squash] Squash (meld together) patches
1031 \\[stgit-move-patches] Move patch(es) to point
1033 \\[stgit-commit] Commit patch(es)
1034 \\[stgit-uncommit] Uncommit patch(es)
1037 \\[stgit-select] Open the file in this window
1038 \\[stgit-find-file-other-window] Open the file in another window
1039 \\[stgit-diff] Show the file's diff
1041 \\[stgit-toggle-index] Toggle change between index and work tree
1042 \\[stgit-revert] Revert changes to file
1045 \\[stgit-toggle-worktree] Toggle showing index and work tree
1046 \\[stgit-toggle-unknown] Toggle showing unknown files
1047 \\[stgit-toggle-ignored] Toggle showing ignored files
1050 \\[stgit-diff] Show diff of patch or file
1051 \\[stgit-diff-base] Show diff against the merge base
1052 \\[stgit-diff-ours] Show diff against our branch
1053 \\[stgit-diff-theirs] Show diff against their branch
1055 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1056 ignore space changes.
1057 With two prefix arguments (e.g., \\[universal-argument] \
1058 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1060 Commands for merge conflicts:
1061 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1062 \\[stgit-resolve-file] Mark unmerged file as resolved
1064 Commands for branches:
1065 \\[stgit-branch] Switch to another branch
1066 \\[stgit-rebase] Rebase the current branch
1068 Customization variables:
1069 `stgit-abbreviate-copies-and-renames'
1070 `stgit-default-show-worktree'
1071 `stgit-find-copies-harder'
1072 `stgit-show-worktree-mode'
1074 See also \\[customize-group] for the \"stgit\" group."
1075 (kill-all-local-variables)
1076 (buffer-disable-undo)
1077 (setq mode-name "StGit"
1078 major-mode 'stgit-mode
1080 (use-local-map stgit-mode-map)
1081 (set (make-local-variable 'list-buffers-directory) default-directory)
1082 (set (make-local-variable 'stgit-marked-patches) nil)
1083 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1084 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1085 (set (make-local-variable 'stgit-index-node) nil)
1086 (set (make-local-variable 'stgit-worktree-node) nil)
1087 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1088 (set-variable 'truncate-lines 't)
1089 (add-hook 'after-save-hook 'stgit-update-saved-file)
1090 (run-hooks 'stgit-mode-hook))
1092 (defun stgit-update-saved-file ()
1093 (let* ((file (expand-file-name buffer-file-name))
1094 (dir (file-name-directory file))
1095 (gitdir (condition-case nil (git-get-top-dir dir)
1097 (buffer (and gitdir (stgit-find-buffer gitdir))))
1099 (with-current-buffer buffer
1100 (stgit-refresh-worktree)))))
1102 (defun stgit-add-mark (patchsym)
1103 "Mark the patch PATCHSYM."
1104 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1106 (defun stgit-remove-mark (patchsym)
1107 "Unmark the patch PATCHSYM."
1108 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1110 (defun stgit-clear-marks ()
1111 "Unmark all patches."
1112 (setq stgit-marked-patches '()))
1114 (defun stgit-patch-at-point (&optional cause-error)
1115 (get-text-property (point) 'patch-data))
1117 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1118 "Return the patch name on the current line as a symbol.
1119 If CAUSE-ERROR is not nil, signal an error if none found.
1120 If ONLY-PATCHES is not nil, only allow real patches, and not
1121 index or work tree."
1122 (let ((patch (stgit-patch-at-point)))
1125 (memq (stgit-patch-status patch) '(work index))
1128 (stgit-patch-name patch))
1130 (error "No patch on this line")))))
1132 (defun stgit-patched-file-at-point ()
1133 (get-text-property (point) 'file-data))
1135 (defun stgit-patches-marked-or-at-point ()
1136 "Return the symbols of the marked patches, or the patch on the current line."
1137 (if stgit-marked-patches
1138 stgit-marked-patches
1139 (let ((patch (stgit-patch-name-at-point)))
1144 (defun stgit-goto-patch (patchsym &optional file)
1145 "Move point to the line containing patch PATCHSYM.
1146 If that patch cannot be found, do nothing.
1148 If the patch was found and FILE is not nil, instead move to that
1149 file's line. If FILE cannot be found, stay on the line of
1151 (let ((node (ewoc-nth stgit-ewoc 0)))
1152 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
1154 (setq node (ewoc-next stgit-ewoc node)))
1155 (when (and node file)
1156 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
1157 (file-node (ewoc-nth file-ewoc 0)))
1158 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
1159 (setq file-node (ewoc-next file-ewoc file-node)))
1161 (ewoc-goto-node file-ewoc file-node)
1162 (move-to-column (stgit-goal-column))
1165 (ewoc-goto-node stgit-ewoc node)
1166 (move-to-column goal-column))))
1168 (defun stgit-init ()
1172 (stgit-capture-output nil
1176 (defun stgit-toggle-mark ()
1177 "Toggle mark on the patch under point."
1180 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1184 (defun stgit-mark ()
1185 "Mark the patch under point."
1188 (let* ((node (ewoc-locate stgit-ewoc))
1189 (patch (ewoc-data node))
1190 (name (stgit-patch-name patch)))
1191 (when (eq name :work)
1192 (error "Cannot mark the work tree"))
1193 (when (eq name :index)
1194 (error "Cannot mark the index"))
1195 (stgit-add-mark (stgit-patch-name patch))
1196 (let ((column (current-column)))
1197 (ewoc-invalidate stgit-ewoc node)
1198 (move-to-column column))))
1200 (defun stgit-mark-down ()
1201 "Mark the patch under point and move to the next patch."
1206 (defun stgit-unmark ()
1207 "Remove mark from the patch on the current line."
1210 (let* ((node (ewoc-locate stgit-ewoc))
1211 (patch (ewoc-data node)))
1212 (stgit-remove-mark (stgit-patch-name patch))
1213 (let ((column (current-column)))
1214 (ewoc-invalidate stgit-ewoc node)
1215 (move-to-column column))))
1217 (defun stgit-unmark-up ()
1218 "Remove mark from the patch on the previous line."
1221 (stgit-previous-patch)
1224 (defun stgit-unmark-down ()
1225 "Remove mark from the patch on the current line."
1231 (defun stgit-rename (name)
1232 "Rename the patch under point to NAME."
1234 (read-string "Patch name: "
1235 (symbol-name (stgit-patch-name-at-point t t)))))
1237 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1238 (stgit-capture-output nil
1239 (stgit-run "rename" old-patchsym name))
1240 (let ((name-sym (intern name)))
1241 (when (memq old-patchsym stgit-expanded-patches)
1242 (setq stgit-expanded-patches
1243 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1244 (when (memq old-patchsym stgit-marked-patches)
1245 (setq stgit-marked-patches
1246 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1248 (stgit-goto-patch name-sym))))
1250 (defun stgit-reload-or-repair (repair)
1251 "Update the contents of the StGit buffer (`stgit-reload').
1253 With a prefix argument, repair the StGit metadata if the branch
1254 was modified with git commands (`stgit-repair')."
1261 (defun stgit-repair ()
1265 (stgit-capture-output nil
1266 (stgit-run "repair"))
1269 (defun stgit-available-branches ()
1270 "Returns a list of the available stg branches"
1271 (let ((output (with-output-to-string
1272 (stgit-run "branch" "--list")))
1275 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1276 (setq result (cons (match-string 1 output) result))
1277 (setq start (match-end 0)))
1280 (defun stgit-branch (branch)
1281 "Switch to branch BRANCH."
1282 (interactive (list (completing-read "Switch to branch: "
1283 (stgit-available-branches))))
1285 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1288 (defun stgit-available-refs (&optional omit-stgit)
1289 "Returns a list of the available git refs.
1290 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1291 (let* ((output (with-output-to-string
1292 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1293 "refs/tags" "refs/heads"
1295 (result (split-string output "\n" t)))
1297 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1298 (substring s (match-end 0))
1301 (delete-if (lambda (s)
1302 (string-match "^refs/heads/.*\\.stgit$" s))
1306 (defun stgit-rebase (new-base)
1307 "Rebase to NEW-BASE."
1308 (interactive (list (completing-read "Rebase to: "
1309 (stgit-available-refs t))))
1311 (stgit-capture-output nil (stgit-run "rebase" new-base))
1314 (defun stgit-commit (count)
1315 "Run stg commit on COUNT commits.
1316 Interactively, the prefix argument is used as COUNT.
1317 A negative COUNT will uncommit instead."
1321 (stgit-uncommit (- count))
1322 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1325 (defun stgit-uncommit (count)
1326 "Run stg uncommit on COUNT commits.
1327 Interactively, the prefix argument is used as COUNT.
1328 A negative COUNT will commit instead."
1332 (stgit-commit (- count))
1333 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1336 (defun stgit-neighbour-file ()
1337 "Return the file name of the next file after point, or the
1338 previous file if point is at the last file within a patch."
1339 (let ((old-point (point))
1341 (and (zerop (forward-line 1))
1342 (let ((f (stgit-patched-file-at-point)))
1343 (and f (setq neighbour-file (stgit-file-file f)))))
1344 (goto-char old-point)
1345 (unless neighbour-file
1346 (and (zerop (forward-line -1))
1347 (let ((f (stgit-patched-file-at-point)))
1348 (and f (setq neighbour-file (stgit-file-file f)))))
1349 (goto-char old-point))
1352 (defun stgit-revert-file ()
1353 "Revert the file at point, which must be in the index or the
1357 (let* ((patched-file (or (stgit-patched-file-at-point)
1358 (error "No file on the current line")))
1359 (patch-name (stgit-patch-name-at-point))
1360 (file-status (stgit-file-status patched-file))
1361 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1362 (stgit-file-cr-to patched-file))
1363 ((eq file-status 'add)
1364 (stgit-file-file patched-file))))
1365 (co-file (cond ((eq file-status 'rename)
1366 (stgit-file-cr-from patched-file))
1367 ((not (memq file-status '(copy add)))
1368 (stgit-file-file patched-file))))
1369 (next-file (stgit-neighbour-file)))
1371 (unless (memq patch-name '(:work :index))
1372 (error "No index or working tree file on this line"))
1374 (when (eq file-status 'ignore)
1375 (error "Cannot revert ignored files"))
1377 (when (eq file-status 'unknown)
1378 (error "Cannot revert unknown files"))
1380 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1381 (when (yes-or-no-p (format "Revert %d file%s? "
1383 (if (= nfiles 1) "" "s")))
1384 (stgit-capture-output nil
1386 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1388 (stgit-run-git "checkout" "HEAD" co-file)))
1390 (stgit-goto-patch patch-name next-file)))))
1392 (defun stgit-revert ()
1393 "Revert the change at point, which must be the index, the work
1394 tree, or a single change in either."
1397 (let ((patched-file (stgit-patched-file-at-point)))
1400 (let* ((patch-name (or (stgit-patch-name-at-point)
1401 (error "No patch or file at point")))
1402 (patch-desc (case patch-name
1405 (t (error (substitute-command-keys
1406 "Use \\[stgit-delete] to delete a patch"))))))
1407 (when (if (eq patch-name :work)
1408 (stgit-work-tree-empty-p)
1409 (stgit-index-empty-p))
1410 (error (format "There are no changes in the %s to revert"
1412 (and (eq patch-name :index)
1413 (not (stgit-work-tree-empty-p))
1414 (error "Cannot revert index as work tree contains unstaged changes"))
1416 (when (yes-or-no-p (format "Revert all changes in the %s? "
1418 (if (eq patch-name :index)
1419 (stgit-run-git-silent "reset" "--hard" "-q")
1420 (stgit-run-git-silent "checkout" "--" "."))
1421 (stgit-refresh-index)
1422 (stgit-refresh-worktree)
1423 (stgit-goto-patch patch-name))))))
1425 (defun stgit-resolve-file ()
1426 "Resolve conflict in the file at point."
1429 (let* ((patched-file (stgit-patched-file-at-point))
1430 (patch (stgit-patch-at-point))
1431 (patch-name (and patch (stgit-patch-name patch)))
1432 (status (and patched-file (stgit-file-status patched-file))))
1434 (unless (memq patch-name '(:work :index))
1435 (error "No index or working tree file on this line"))
1437 (unless (eq status 'unmerged)
1438 (error "No conflict to resolve at the current line"))
1440 (stgit-capture-output nil
1441 (stgit-move-change-to-index (stgit-file-file patched-file)))
1445 (defun stgit-push-next (npatches)
1446 "Push the first unapplied patch.
1447 With numeric prefix argument, push that many patches."
1450 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1452 (stgit-refresh-git-status))
1454 (defun stgit-pop-next (npatches)
1455 "Pop the topmost applied patch.
1456 With numeric prefix argument, pop that many patches."
1459 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1461 (stgit-refresh-git-status))
1463 (defun stgit-applied-at-point-p ()
1464 "Return non-nil if the patch at point is applied."
1465 (let ((patch (stgit-patch-at-point t)))
1466 (not (eq (stgit-patch-status patch) 'unapplied))))
1468 (defun stgit-push-or-pop ()
1469 "Push or pop the patch on the current line."
1472 (let ((patchsym (stgit-patch-name-at-point t t))
1473 (applied (stgit-applied-at-point-p)))
1474 (stgit-capture-output nil
1475 (stgit-run (if applied "pop" "push") patchsym))
1478 (defun stgit-goto ()
1479 "Go to the patch on the current line."
1482 (let ((patchsym (stgit-patch-name-at-point t)))
1483 (stgit-capture-output nil
1484 (stgit-run "goto" patchsym))
1487 (defun stgit-id (patchsym)
1488 "Return the git commit id for PATCHSYM.
1489 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1490 (if (keywordp patchsym)
1492 (let ((result (with-output-to-string
1493 (stgit-run-silent "id" patchsym))))
1494 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1495 (error "Cannot find commit id for %s" patchsym))
1496 (match-string 1 result))))
1498 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1499 "Show the patch on the current line.
1501 UNMERGED-STAGE is the argument to `git-diff' that that selects
1502 which stage to diff against in the case of unmerged files."
1503 (let ((space-arg (when (numberp ignore-whitespace)
1504 (cond ((> ignore-whitespace 4)
1505 "--ignore-all-space")
1506 ((> ignore-whitespace 1)
1507 "--ignore-space-change"))))
1508 (patch-name (stgit-patch-name-at-point t)))
1509 (stgit-capture-output "*StGit patch*"
1510 (case (get-text-property (point) 'entry-type)
1512 (let* ((patched-file (stgit-patched-file-at-point))
1513 (patch-id (let ((id (stgit-id patch-name)))
1514 (if (and (eq id :index)
1515 (eq (stgit-file-status patched-file)
1519 (args (append (and space-arg (list space-arg))
1520 (and (stgit-file-cr-from patched-file)
1521 (list (stgit-find-copies-harder-diff-arg)))
1522 (cond ((eq patch-id :index)
1524 ((eq patch-id :work)
1525 (list unmerged-stage))
1527 (list (concat patch-id "^") patch-id)))
1529 (if (stgit-file-copy-or-rename patched-file)
1530 (list (stgit-file-cr-from patched-file)
1531 (stgit-file-cr-to patched-file))
1532 (list (stgit-file-file patched-file))))))
1533 (apply 'stgit-run-git "diff" args)))
1535 (let* ((patch-id (stgit-id patch-name)))
1536 (if (or (eq patch-id :index) (eq patch-id :work))
1537 (apply 'stgit-run-git "diff"
1538 (stgit-find-copies-harder-diff-arg)
1539 (append (and space-arg (list space-arg))
1540 (if (eq patch-id :index)
1542 (list unmerged-stage))))
1543 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1544 (and space-arg (list "-O" space-arg))
1545 (list (stgit-patch-name-at-point)))))
1546 (apply 'stgit-run args)))))
1548 (error "No patch or file at point")))
1549 (with-current-buffer standard-output
1550 (goto-char (point-min))
1553 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1554 `(defun ,name (&optional ignore-whitespace)
1555 ,(format "Show the patch on the current line.
1557 %sWith a prefix argument, ignore whitespace. With a prefix argument
1558 greater than four (e.g., \\[universal-argument] \
1559 \\[universal-argument] \\[%s]), ignore all whitespace."
1561 (format "For unmerged files, %s.\n\n" unmerged-action)
1566 (stgit-show-patch ,diff-arg ignore-whitespace)))
1568 (stgit-define-diff stgit-diff
1570 (stgit-define-diff stgit-diff-ours
1572 "diff against our branch")
1573 (stgit-define-diff stgit-diff-theirs
1575 "diff against their branch")
1576 (stgit-define-diff stgit-diff-base
1578 "diff against the merge base")
1579 (stgit-define-diff stgit-diff-combined
1581 "show a combined diff")
1583 (defun stgit-move-change-to-index (file &optional force)
1584 "Copies the work tree state of FILE to index, using git add or git rm.
1586 If FORCE is not nil, use --force."
1587 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1588 '("add") '("rm" "-q"))))
1589 (stgit-capture-output "*git output*"
1590 (apply 'stgit-run-git (append op (and force '("--force"))
1591 '("--") (list file))))))
1593 (defun stgit-remove-change-from-index (file)
1594 "Unstages the change in FILE from the index"
1595 (stgit-capture-output "*git output*"
1596 (stgit-run-git "reset" "-q" "--" file)))
1598 (defun stgit-git-index-unmerged-p ()
1600 (with-output-to-string
1601 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1603 "--quiet" "HEAD")))))
1606 (defun stgit-file-toggle-index ()
1607 "Move modified file in or out of the index.
1609 Leaves the point where it is, but moves the mark to where the
1610 file ended up. You can then jump to the file with \
1611 \\[exchange-point-and-mark]."
1614 (let* ((patched-file (or (stgit-patched-file-at-point)
1615 (error "No file on the current line")))
1616 (patched-status (stgit-file-status patched-file)))
1617 (when (eq patched-status 'unmerged)
1618 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1619 (let* ((patch (stgit-patch-at-point))
1620 (patch-name (stgit-patch-name patch))
1621 (mark-file (if (eq patched-status 'rename)
1622 (stgit-file-cr-to patched-file)
1623 (stgit-file-file patched-file)))
1624 (point-file (if (eq patched-status 'rename)
1625 (stgit-file-cr-from patched-file)
1626 (stgit-neighbour-file))))
1628 (cond ((eq patch-name :work)
1629 (stgit-move-change-to-index (stgit-file-file patched-file)
1630 (eq patched-status 'ignore)))
1631 ((eq patch-name :index)
1632 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1634 (error "Can only move files between working tree and index")))
1635 (stgit-refresh-worktree)
1636 (stgit-refresh-index)
1637 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1639 (stgit-goto-patch patch-name point-file))))
1641 (defun stgit-toggle-index ()
1642 "Move change in or out of the index.
1644 Works on index and work tree, as well as files in either.
1646 Leaves the point where it is, but moves the mark to where the
1647 file ended up. You can then jump to the file with \
1648 \\[exchange-point-and-mark]."
1651 (if (stgit-patched-file-at-point)
1652 (stgit-file-toggle-index)
1653 (let ((patch-name (stgit-patch-name-at-point)))
1654 (unless (memq patch-name '(:index :work))
1655 (error "Can only move changes between working tree and index"))
1656 (when (stgit-git-index-unmerged-p)
1657 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1658 (if (if (eq patch-name :index)
1659 (stgit-index-empty-p)
1660 (stgit-work-tree-empty-p))
1661 (message "No changes to be moved")
1662 (stgit-capture-output nil
1663 (if (eq patch-name :work)
1664 (stgit-run-git "add" "--update")
1665 (stgit-run-git "reset" "--mixed" "-q")))
1666 (stgit-refresh-worktree)
1667 (stgit-refresh-index))
1668 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1670 (defun stgit-edit ()
1671 "Edit the patch on the current line."
1674 (let ((patchsym (stgit-patch-name-at-point t t))
1675 (edit-buf (get-buffer-create "*StGit edit*"))
1676 (dir default-directory))
1677 (log-edit 'stgit-confirm-edit t nil edit-buf)
1678 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1679 (setq default-directory dir)
1680 (let ((standard-output edit-buf))
1681 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1683 (defun stgit-confirm-edit ()
1685 (let ((file (make-temp-file "stgit-edit-")))
1686 (write-region (point-min) (point-max) file)
1687 (stgit-capture-output nil
1688 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1689 (with-current-buffer log-edit-parent-buffer
1692 (defun stgit-new (add-sign &optional refresh)
1693 "Create a new patch.
1694 With a prefix argument, include a \"Signed-off-by:\" line at the
1698 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1699 (dir default-directory))
1700 (log-edit 'stgit-confirm-new t nil edit-buf)
1701 (setq default-directory dir)
1702 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1705 (let ((standard-output (current-buffer)))
1706 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1708 (defun stgit-confirm-new ()
1710 (let ((file (make-temp-file "stgit-edit-"))
1711 (refresh stgit-refresh-after-new))
1712 (write-region (point-min) (point-max) file)
1713 (stgit-capture-output nil
1714 (stgit-run "new" "-f" file))
1715 (with-current-buffer log-edit-parent-buffer
1720 (defun stgit-new-and-refresh (add-sign)
1721 "Create a new patch and refresh it with the current changes.
1723 With a prefix argument, include a \"Signed-off-by:\" line at the
1726 This works just like running `stgit-new' followed by `stgit-refresh'."
1729 (stgit-new add-sign t))
1731 (defun stgit-create-patch-name (description)
1732 "Create a patch name from a long description"
1734 (while (> (length description) 0)
1735 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1736 (setq patch (downcase (concat patch
1737 (match-string 0 description))))
1738 (setq description (substring description (match-end 0))))
1739 ((string-match "\\` +" description)
1740 (setq patch (concat patch "-"))
1741 (setq description (substring description (match-end 0))))
1742 ((string-match "\\`[^a-zA-Z_-]+" description)
1743 (setq description (substring description (match-end 0))))))
1744 (cond ((= (length patch) 0)
1746 ((> (length patch) 20)
1747 (substring patch 0 20))
1750 (defun stgit-delete (patchsyms &optional spill-p)
1751 "Delete the patches in PATCHSYMS.
1752 Interactively, delete the marked patches, or the patch at point.
1754 With a prefix argument, or SPILL-P, spill the patch contents to
1755 the work tree and index."
1756 (interactive (list (stgit-patches-marked-or-at-point)
1757 current-prefix-arg))
1760 (error "No patches to delete"))
1761 (when (memq :index patchsyms)
1762 (error "Cannot delete the index"))
1763 (when (memq :work patchsyms)
1764 (error "Cannot delete the work tree"))
1766 (let ((npatches (length patchsyms)))
1767 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1769 (if (= 1 npatches) "" "es")
1771 " (spilling contents to index)"
1773 (let ((args (if spill-p
1774 (cons "--spill" patchsyms)
1776 (stgit-capture-output nil
1777 (apply 'stgit-run "delete" args))
1780 (defun stgit-move-patches-target ()
1781 "Return the patchsym indicating a target patch for
1782 `stgit-move-patches'.
1784 This is either the patch at point, or one of :top and :bottom, if
1785 the point is after or before the applied patches."
1787 (let ((patchsym (stgit-patch-name-at-point nil t)))
1788 (cond (patchsym patchsym)
1789 ((save-excursion (re-search-backward "^>" nil t)) :top)
1792 (defun stgit-sort-patches (patchsyms)
1793 "Returns the list of patches in PATCHSYMS sorted according to
1794 their position in the patch series, bottommost first.
1796 PATCHSYMS must not contain duplicate entries."
1797 (let (sorted-patchsyms
1798 (series (with-output-to-string
1799 (with-current-buffer standard-output
1800 (stgit-run-silent "series" "--noprefix"))))
1802 (while (string-match "^\\(.+\\)" series start)
1803 (let ((patchsym (intern (match-string 1 series))))
1804 (when (memq patchsym patchsyms)
1805 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1806 (setq start (match-end 0)))
1807 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1809 (unless (= (length patchsyms) (length sorted-patchsyms))
1810 (error "Internal error"))
1814 (defun stgit-move-patches (patchsyms target-patch)
1815 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1816 If TARGET-PATCH is :bottom or :top, move the patches to the
1817 bottom or top of the stack, respectively.
1819 Interactively, move the marked patches to where the point is."
1820 (interactive (list stgit-marked-patches
1821 (stgit-move-patches-target)))
1824 (error "Need at least one patch to move"))
1826 (unless target-patch
1827 (error "Point not at a patch"))
1829 (if (eq target-patch :top)
1830 (stgit-capture-output nil
1831 (apply 'stgit-run "float" patchsyms))
1833 ;; need to have patchsyms sorted by position in the stack
1834 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1835 (while sorted-patchsyms
1836 (setq sorted-patchsyms
1837 (and (stgit-capture-output nil
1838 (if (eq target-patch :bottom)
1839 (stgit-run "sink" "--" (car sorted-patchsyms))
1840 (stgit-run "sink" "--to" target-patch "--"
1841 (car sorted-patchsyms))))
1842 (cdr sorted-patchsyms))))))
1845 (defun stgit-squash (patchsyms)
1846 "Squash the patches in PATCHSYMS.
1847 Interactively, squash the marked patches.
1849 Unless there are any conflicts, the patches will be merged into
1850 one patch, which will occupy the same spot in the series as the
1851 deepest patch had before the squash."
1852 (interactive (list stgit-marked-patches))
1854 (when (< (length patchsyms) 2)
1855 (error "Need at least two patches to squash"))
1856 (let ((stgit-buffer (current-buffer))
1857 (edit-buf (get-buffer-create "*StGit edit*"))
1858 (dir default-directory)
1859 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1860 (log-edit 'stgit-confirm-squash t nil edit-buf)
1861 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1862 (setq default-directory dir)
1863 (let ((result (let ((standard-output edit-buf))
1864 (apply 'stgit-run-silent "squash"
1865 "--save-template=-" sorted-patchsyms))))
1867 ;; stg squash may have reordered the patches or caused conflicts
1868 (with-current-buffer stgit-buffer
1871 (unless (eq 0 result)
1873 (rename-buffer "*StGit error*")
1874 (resize-temp-buffer-window)
1875 (switch-to-buffer-other-window stgit-buffer)
1876 (error "stg squash failed")))))
1878 (defun stgit-confirm-squash ()
1880 (let ((file (make-temp-file "stgit-edit-")))
1881 (write-region (point-min) (point-max) file)
1882 (stgit-capture-output nil
1883 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1884 (with-current-buffer log-edit-parent-buffer
1886 ;; Go to first marked patch and stay there
1887 (goto-char (point-min))
1888 (re-search-forward (concat "^[>+-]\\*") nil t)
1889 (move-to-column goal-column)
1890 (let ((pos (point)))
1894 (defun stgit-help ()
1895 "Display help for the StGit mode."
1897 (describe-function 'stgit-mode))
1899 (defun stgit-undo (&optional arg)
1901 With prefix argument, run it with the --hard flag.
1903 See also `stgit-redo'."
1906 (stgit-capture-output nil
1908 (stgit-run "undo" "--hard")
1909 (stgit-run "undo")))
1912 (defun stgit-redo (&optional arg)
1914 With prefix argument, run it with the --hard flag.
1916 See also `stgit-undo'."
1919 (stgit-capture-output nil
1921 (stgit-run "redo" "--hard")
1922 (stgit-run "redo")))
1925 (defun stgit-refresh (&optional arg)
1927 If the index contains any changes, only refresh from index.
1929 With prefix argument, refresh the marked patch or the patch under point."
1932 (let ((patchargs (if arg
1933 (let ((patches (stgit-patches-marked-or-at-point)))
1934 (cond ((null patches)
1935 (error "No patch to update"))
1936 ((> (length patches) 1)
1937 (error "Too many patches selected"))
1939 (cons "-p" patches))))
1941 (unless (stgit-index-empty-p)
1942 (setq patchargs (cons "--index" patchargs)))
1943 (stgit-capture-output nil
1944 (apply 'stgit-run "refresh" patchargs))
1945 (stgit-refresh-git-status))
1948 (defvar stgit-show-worktree nil
1949 "If nil, inhibit showing work tree and index in the stgit buffer.
1951 See also `stgit-show-worktree-mode'.")
1953 (defvar stgit-show-ignored nil
1954 "If nil, inhibit showing files ignored by git.")
1956 (defvar stgit-show-unknown nil
1957 "If nil, inhibit showing files not registered with git.")
1959 (defun stgit-toggle-worktree (&optional arg)
1960 "Toggle the visibility of the work tree.
1961 With ARG, show the work tree if ARG is positive.
1963 Its initial setting is controlled by `stgit-default-show-worktree'.
1965 `stgit-show-worktree-mode' controls where on screen the index and
1966 work tree will show up."
1969 (setq stgit-show-worktree
1972 (not stgit-show-worktree)))
1975 (defun stgit-toggle-ignored (&optional arg)
1976 "Toggle the visibility of files ignored by git in the work
1977 tree. With ARG, show these files if ARG is positive.
1979 Use \\[stgit-toggle-worktree] to show the work tree."
1982 (setq stgit-show-ignored
1985 (not stgit-show-ignored)))
1988 (defun stgit-toggle-unknown (&optional arg)
1989 "Toggle the visibility of files not registered with git in the
1990 work tree. With ARG, show these files if ARG is positive.
1992 Use \\[stgit-toggle-worktree] to show the work tree."
1995 (setq stgit-show-unknown
1998 (not stgit-show-unknown)))