1 ;; stgit.el: An emacs mode for StGit
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
10 ;; To start: `M-x stgit'
12 (when (< emacs-major-version 22)
13 (error "Emacs older than 22 is not supported by stgit.el"))
20 "Manage StGit patches for the tree in DIR.
22 See `stgit-mode' for commands available."
23 (interactive "DDirectory: \n")
24 (switch-to-stgit-buffer (git-get-top-dir dir))
27 (defun stgit-assert-mode ()
28 "Signal an error if not in an StGit buffer."
29 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
31 (unless (fboundp 'git-get-top-dir)
32 (defun git-get-top-dir (dir)
33 "Retrieve the top-level directory of a git tree."
34 (let ((cdup (with-output-to-string
35 (with-current-buffer standard-output
37 (unless (eq 0 (call-process "git" nil t nil
38 "rev-parse" "--show-cdup"))
39 (error "Cannot find top-level git tree for %s" dir))))))
40 (expand-file-name (concat (file-name-as-directory dir)
41 (car (split-string cdup "\n")))))))
43 (defun stgit-refresh-git-status (&optional dir)
44 "If it exists, refresh the `git-status' buffer belonging to
45 directory DIR or `default-directory'"
46 (when (and (fboundp 'git-find-status-buffer)
47 (fboundp 'git-refresh-status))
48 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
49 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
50 (when git-status-buffer
51 (with-current-buffer git-status-buffer
52 (git-refresh-status))))))
54 (defun stgit-find-buffer (dir)
55 "Return the buffer displaying StGit patches for DIR, or nil if none."
56 (setq dir (file-name-as-directory dir))
57 (let ((buffers (buffer-list)))
59 (not (with-current-buffer (car buffers)
60 (and (eq major-mode 'stgit-mode)
61 (string= default-directory dir)))))
62 (setq buffers (cdr buffers)))
63 (and buffers (car buffers))))
65 (defun switch-to-stgit-buffer (dir)
66 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
67 (setq dir (file-name-as-directory dir))
68 (let ((buffer (stgit-find-buffer dir)))
69 (switch-to-buffer (or buffer
70 (create-stgit-buffer dir)))))
72 (defstruct (stgit-patch)
73 status name desc empty files-ewoc)
75 (defun stgit-patch-pp (patch)
76 (let* ((status (stgit-patch-status patch))
78 (name (stgit-patch-name patch))
79 (face (cdr (assq status stgit-patch-status-face-alist))))
85 (if (memq name stgit-marked-patches)
87 (if (memq status '(index work))
88 (insert (propertize (if (eq status 'index) "Index" "Work tree")
90 (insert (format "%-30s"
91 (propertize (symbol-name name)
93 'syntax-table (string-to-syntax "w")))
95 (if (stgit-patch-empty patch) "(empty) " "")
96 (propertize (or (stgit-patch-desc patch) "")
97 'face 'stgit-description-face)))
99 (put-text-property start (point) 'entry-type 'patch)
100 (when (memq name stgit-expanded-patches)
101 (stgit-insert-patch-files patch))
102 (put-text-property start (point) 'patch-data patch)))
104 (defun create-stgit-buffer (dir)
105 "Create a buffer for showing StGit patches.
106 Argument DIR is the repository path."
107 (let ((buf (create-file-buffer (concat dir "*stgit*")))
108 (inhibit-read-only t))
109 (with-current-buffer buf
110 (setq default-directory dir)
112 (set (make-local-variable 'stgit-ewoc)
113 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
114 (setq buffer-read-only t))
117 (defmacro stgit-capture-output (name &rest body)
118 "Capture StGit output and, if there was any output, show it in a window
120 Returns nil if there was no output."
121 (declare (debug ([&or stringp null] body))
123 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
124 (stgit-dir default-directory)
125 (inhibit-read-only t))
126 (with-current-buffer output-buf
128 (setq default-directory stgit-dir)
129 (setq buffer-read-only t))
130 (let ((standard-output output-buf))
132 (with-current-buffer output-buf
133 (set-buffer-modified-p nil)
134 (setq buffer-read-only t)
135 (if (< (point-min) (point-max))
136 (display-buffer output-buf t)))))
138 (defun stgit-make-run-args (args)
139 "Return a copy of ARGS with its elements converted to strings."
141 ;; don't use (format "%s" ...) to limit type errors
142 (cond ((stringp x) x)
143 ((integerp x) (number-to-string x))
144 ((symbolp x) (symbol-name x))
146 (error "Bad element in stgit-make-run-args args: %S" x))))
149 (defun stgit-run-silent (&rest args)
150 (setq args (stgit-make-run-args args))
151 (apply 'call-process "stg" nil standard-output nil args))
153 (defun stgit-run (&rest args)
154 (setq args (stgit-make-run-args args))
155 (let ((msgcmd (mapconcat #'identity args " ")))
156 (message "Running stg %s..." msgcmd)
157 (apply 'call-process "stg" nil standard-output nil args)
158 (message "Running stg %s...done" msgcmd)))
160 (defun stgit-run-git (&rest args)
161 (setq args (stgit-make-run-args args))
162 (let ((msgcmd (mapconcat #'identity args " ")))
163 (message "Running git %s..." msgcmd)
164 (apply 'call-process "git" nil standard-output nil args)
165 (message "Running git %s...done" msgcmd)))
167 (defun stgit-run-git-silent (&rest args)
168 (setq args (stgit-make-run-args args))
169 (apply 'call-process "git" nil standard-output nil args))
171 (defun stgit-index-empty-p ()
172 "Returns non-nil if the index contains no changes from HEAD."
173 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
175 (defun stgit-work-tree-empty-p ()
176 "Returns non-nil if the work tree contains no changes from index."
177 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
179 (defvar stgit-index-node)
180 (defvar stgit-worktree-node)
182 (defun stgit-refresh-index ()
183 (when stgit-index-node
184 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
186 (defun stgit-refresh-worktree ()
187 (when stgit-worktree-node
188 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
190 (defun stgit-run-series-insert-index (ewoc)
191 (setq index-node (cons ewoc (ewoc-enter-last ewoc
197 worktree-node (cons ewoc (ewoc-enter-last ewoc
204 (defun stgit-run-series (ewoc)
205 (setq stgit-index-node nil
206 stgit-worktree-node nil)
207 (let ((inserted-index (not stgit-show-worktree))
212 (let* ((standard-output (current-buffer))
213 (exit-status (stgit-run-silent "series"
214 "--description" "--empty")))
215 (goto-char (point-min))
216 (if (not (zerop exit-status))
217 (cond ((looking-at "stg series: \\(.*\\)")
218 (setq inserted-index t)
219 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
220 (substitute-command-keys
221 "-- not initialized; run \\[stgit-init]")))
223 (error "Error running stg: %s"
227 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
228 (error "Syntax error in output from stg series"))
229 (let* ((state-str (match-string 2))
230 (state (cond ((string= state-str ">") 'top)
231 ((string= state-str "+") 'applied)
232 ((string= state-str "-") 'unapplied)))
233 (name (intern (match-string 4)))
234 (desc (match-string 5))
235 (empty (string= (match-string 1) "0")))
236 (unless inserted-index
237 (when (or (eq stgit-show-worktree-mode 'top)
238 (and (eq stgit-show-worktree-mode 'center)
239 (eq state 'unapplied)))
240 (setq inserted-index t)
241 (stgit-run-series-insert-index ewoc)))
242 (setq all-patchsyms (cons name all-patchsyms))
243 (ewoc-enter-last ewoc
250 (unless inserted-index
251 (stgit-run-series-insert-index ewoc)))
252 (setq stgit-index-node index-node
253 stgit-worktree-node worktree-node
254 stgit-marked-patches (intersection stgit-marked-patches
257 (defun stgit-reload ()
258 "Update the contents of the StGit buffer."
261 (let ((inhibit-read-only t)
262 (curline (line-number-at-pos))
263 (curpatch (stgit-patch-name-at-point))
264 (curfile (stgit-patched-file-at-point)))
265 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
266 (ewoc-set-hf stgit-ewoc
269 (substring (with-output-to-string
270 (stgit-run-silent "branch"))
272 'face 'stgit-branch-name-face)
274 (if stgit-show-worktree
277 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
278 shows the working tree\n")
279 'face 'stgit-description-face)))
280 (stgit-run-series stgit-ewoc)
282 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
283 (goto-line curline)))
284 (stgit-refresh-git-status))
286 (defun stgit-set-default (symbol value)
287 "Set default value of SYMBOL to VALUE using `set-default' and
288 reload all StGit buffers."
289 (set-default symbol value)
290 (dolist (buf (buffer-list))
291 (with-current-buffer buf
292 (when (eq major-mode 'stgit-mode)
296 "A user interface for the StGit patch maintenance tool."
298 :link '(function-link stgit)
299 :link '(url-link "http://www.procode.org/stgit/"))
301 (defcustom stgit-abbreviate-copies-and-renames t
302 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
303 instead of \"dir/old/file -> dir/new/file\"."
306 :set 'stgit-set-default)
308 (defcustom stgit-default-show-worktree t
309 "Set to non-nil to by default show the working tree in a new stgit buffer.
311 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
314 :link '(variable-link stgit-show-worktree))
316 (defcustom stgit-find-copies-harder nil
317 "Try harder to find copied files when listing patches.
319 When not nil, runs git diff-tree with the --find-copies-harder
320 flag, which reduces performance."
323 :set 'stgit-set-default)
325 (defcustom stgit-show-worktree-mode 'center
326 "This variable controls where the \"Index\" and \"Work tree\"
327 will be shown on in the buffer.
329 It can be set to 'top (above all patches), 'center (show between
330 applied and unapplied patches), and 'bottom (below all patches)."
331 :type '(radio (const :tag "above all patches (top)" top)
332 (const :tag "between applied and unapplied patches (center)"
334 (const :tag "below all patches (bottom)" bottom))
336 :link '(variable-link stgit-show-worktree)
337 :set 'stgit-set-default)
339 (defface stgit-branch-name-face
341 "The face used for the StGit branch name"
344 (defface stgit-top-patch-face
345 '((((background dark)) (:weight bold :foreground "yellow"))
346 (((background light)) (:weight bold :foreground "purple"))
348 "The face used for the top patch names"
351 (defface stgit-applied-patch-face
352 '((((background dark)) (:foreground "light yellow"))
353 (((background light)) (:foreground "purple"))
355 "The face used for applied patch names"
358 (defface stgit-unapplied-patch-face
359 '((((background dark)) (:foreground "gray80"))
360 (((background light)) (:foreground "orchid"))
362 "The face used for unapplied patch names"
365 (defface stgit-description-face
366 '((((background dark)) (:foreground "tan"))
367 (((background light)) (:foreground "dark red")))
368 "The face used for StGit descriptions"
371 (defface stgit-index-work-tree-title-face
372 '((((supports :slant italic)) :slant italic)
374 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
377 (defface stgit-unmerged-file-face
378 '((((class color) (background light)) (:foreground "red" :bold t))
379 (((class color) (background dark)) (:foreground "red" :bold t)))
380 "StGit mode face used for unmerged file status"
383 (defface stgit-unknown-file-face
384 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
385 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
386 "StGit mode face used for unknown file status"
389 (defface stgit-ignored-file-face
390 '((((class color) (background light)) (:foreground "grey60"))
391 (((class color) (background dark)) (:foreground "grey40")))
392 "StGit mode face used for ignored files")
394 (defface stgit-file-permission-face
395 '((((class color) (background light)) (:foreground "green" :bold t))
396 (((class color) (background dark)) (:foreground "green" :bold t)))
397 "StGit mode face used for permission changes."
400 (defface stgit-modified-file-face
401 '((((class color) (background light)) (:foreground "purple"))
402 (((class color) (background dark)) (:foreground "salmon")))
403 "StGit mode face used for modified file status"
406 (defconst stgit-file-status-code-strings
407 (mapcar (lambda (arg)
409 (propertize (cadr arg) 'face (car (cddr arg)))))
410 '((add "Added" stgit-modified-file-face)
411 (copy "Copied" stgit-modified-file-face)
412 (delete "Deleted" stgit-modified-file-face)
413 (modify "Modified" stgit-modified-file-face)
414 (rename "Renamed" stgit-modified-file-face)
415 (mode-change "Mode change" stgit-modified-file-face)
416 (unmerged "Unmerged" stgit-unmerged-file-face)
417 (unknown "Unknown" stgit-unknown-file-face)
418 (ignore "Ignored" stgit-ignored-file-face)))
419 "Alist of code symbols to description strings")
421 (defconst stgit-patch-status-face-alist
422 '((applied . stgit-applied-patch-face)
423 (top . stgit-top-patch-face)
424 (unapplied . stgit-unapplied-patch-face)
425 (index . stgit-index-work-tree-title-face)
426 (work . stgit-index-work-tree-title-face))
427 "Alist of face to use for a given patch status")
429 (defun stgit-file-status-code-as-string (file)
430 "Return stgit status code for FILE as a string"
431 (let* ((code (assq (stgit-file-status file)
432 stgit-file-status-code-strings))
433 (score (stgit-file-cr-score file)))
436 (if (and score (/= score 100))
437 (format "%s %s" (cdr code)
438 (propertize (format "%d%%" score)
439 'face 'stgit-description-face))
442 (defun stgit-file-status-code (str &optional score)
443 "Return stgit status code from git status string"
444 (let ((code (assoc str '(("A" . add)
453 (setq code (if code (cdr code) 'unknown))
454 (when (stringp score)
455 (if (> (length score) 0)
456 (setq score (string-to-number score))
458 (if score (cons code score) code)))
460 (defconst stgit-file-type-strings
463 (#o160 . "subproject"))
464 "Alist of names of file types")
466 (defun stgit-file-type-string (type)
467 "Return string describing file type TYPE (the high bits of file permission).
468 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
469 (let ((type-str (assoc type stgit-file-type-strings)))
470 (or (and type-str (cdr type-str))
471 (format "unknown type %o" type))))
473 (defun stgit-file-type-change-string (old-perm new-perm)
474 "Return string describing file type change from OLD-PERM to NEW-PERM.
475 Cf. `stgit-file-type-string'."
476 (let ((old-type (lsh old-perm -9))
477 (new-type (lsh new-perm -9)))
478 (cond ((= old-type new-type) "")
479 ((zerop new-type) "")
481 (if (= new-type #o100)
483 (format " (%s)" (stgit-file-type-string new-type))))
484 (t (format " (%s -> %s)"
485 (stgit-file-type-string old-type)
486 (stgit-file-type-string new-type))))))
488 (defun stgit-file-mode-change-string (old-perm new-perm)
489 "Return string describing file mode change from OLD-PERM to NEW-PERM.
490 Cf. `stgit-file-type-change-string'."
491 (setq old-perm (logand old-perm #o777)
492 new-perm (logand new-perm #o777))
493 (if (or (= old-perm new-perm)
497 (let* ((modified (logxor old-perm new-perm))
498 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
499 (cond ((zerop modified) "")
500 ((and (zerop not-x-modified)
501 (or (and (eq #o111 (logand old-perm #o111))
502 (propertize "-x" 'face 'stgit-file-permission-face))
503 (and (eq #o111 (logand new-perm #o111))
504 (propertize "+x" 'face
505 'stgit-file-permission-face)))))
506 (t (concat (propertize (format "%o" old-perm)
507 'face 'stgit-file-permission-face)
509 'face 'stgit-description-face)
510 (propertize (format "%o" new-perm)
511 'face 'stgit-file-permission-face)))))))
513 (defstruct (stgit-file)
514 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
516 (defun stgit-describe-copy-or-rename (file)
517 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
518 from to common-head common-tail)
520 (when stgit-abbreviate-copies-and-renames
521 (setq from (split-string (stgit-file-cr-from file) "/")
522 to (split-string (stgit-file-cr-to file) "/"))
524 (while (and from to (cdr from) (cdr to)
525 (string-equal (car from) (car to)))
526 (setq common-head (cons (car from) common-head)
529 (setq common-head (nreverse common-head)
532 (while (and from to (cdr from) (cdr to)
533 (string-equal (car from) (car to)))
534 (setq common-tail (cons (car from) common-tail)
537 (setq from (nreverse from)
540 (if (or common-head common-tail)
541 (concat (if common-head
542 (mapconcat #'identity common-head "/")
544 (if common-head "/" "")
545 (propertize "{" 'face 'stgit-description-face)
546 (mapconcat #'identity from "/")
548 (mapconcat #'identity to "/")
549 (propertize "}" 'face 'stgit-description-face)
550 (if common-tail "/" "")
552 (mapconcat #'identity common-tail "/")
554 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
556 (defun stgit-file-pp (file)
557 (let ((status (stgit-file-status file))
558 (name (if (stgit-file-copy-or-rename file)
559 (stgit-describe-copy-or-rename file)
560 (stgit-file-file file)))
561 (mode-change (stgit-file-mode-change-string
562 (stgit-file-old-perm file)
563 (stgit-file-new-perm file)))
565 (insert (format " %-12s%s%s%s%s\n"
566 (stgit-file-status-code-as-string file)
568 (if (zerop (length mode-change)) "" " ")
570 (propertize (stgit-file-type-change-string
571 (stgit-file-old-perm file)
572 (stgit-file-new-perm file))
573 'face 'stgit-description-face)))
574 (add-text-properties start (point)
575 (list 'entry-type 'file
578 (defun stgit-find-copies-harder-diff-arg ()
579 "Return the flag to use with `git-diff' depending on the
580 `stgit-find-copies-harder' flag."
581 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
583 (defun stgit-insert-ls-files (args file-flag)
584 (let ((start (point)))
585 (apply 'stgit-run-git
586 (append '("ls-files" "--exclude-standard" "-z") args))
588 (while (looking-at "\\([^\0]*\\)\0")
589 (let ((name-len (- (match-end 0) (match-beginning 0))))
590 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
591 (forward-char name-len)))))
593 (defun stgit-insert-patch-files (patch)
594 "Expand (show modification of) the patch PATCH after the line
596 (let* ((patchsym (stgit-patch-name patch))
598 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
599 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
600 (set-marker-insertion-type end t)
601 (setf (stgit-patch-files-ewoc patch) ewoc)
603 (let ((standard-output (current-buffer)))
604 (apply 'stgit-run-git
605 (cond ((eq patchsym :work)
606 `("diff-files" "-0" ,@args))
607 ((eq patchsym :index)
608 `("diff-index" ,@args "--cached" "HEAD"))
610 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
612 (when (and (eq patchsym :work))
613 (when stgit-show-ignored
614 (stgit-insert-ls-files '("--ignored" "--others") "I"))
615 (when stgit-show-unknown
616 (stgit-insert-ls-files '("--others") "X"))
617 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
618 (point-min) (point-max)))
620 (goto-char (point-min))
621 (unless (or (eobp) (memq patchsym '(:work :index)))
623 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
624 (let ((old-perm (string-to-number (match-string 1) 8))
625 (new-perm (string-to-number (match-string 2) 8)))
626 (goto-char (match-end 0))
629 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
630 (let* ((patch-status (stgit-patch-status patch))
631 (file-subexp (if (eq patch-status 'unapplied)
634 (file (match-string file-subexp)))
639 :cr-score (string-to-number (match-string 2))
640 :cr-from (match-string 3)
641 :cr-to (match-string 4)
642 :status (stgit-file-status-code
645 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
653 :status (stgit-file-status-code
655 :file (match-string 2))))))
656 (goto-char (match-end 0))
657 (ewoc-enter-last ewoc file))))
659 (unless (ewoc-nth ewoc 0)
662 (propertize "<no files>"
663 'face 'stgit-description-face)
667 (defun stgit-find-file (&optional other-window)
668 (let* ((file (or (stgit-patched-file-at-point)
669 (error "No file at point")))
670 (filename (expand-file-name (stgit-file-file file))))
671 (unless (file-exists-p filename)
672 (error "File does not exist"))
673 (funcall (if other-window 'find-file-other-window 'find-file)
675 (when (eq (stgit-file-status file) 'unmerged)
678 (defun stgit-expand (&optional patches collapse)
679 "Show the contents selected patches, or the patch at point.
681 See also `stgit-collapse'.
683 Non-interactively, operate on PATCHES, and collapse instead of
684 expand if COLLAPSE is not nil."
685 (interactive (list (stgit-patches-marked-or-at-point)))
687 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
688 patches stgit-expanded-patches)))
689 (setq stgit-expanded-patches
691 (set-difference stgit-expanded-patches patches-diff)
692 (append stgit-expanded-patches patches-diff)))
693 (ewoc-map #'(lambda (patch)
694 (memq (stgit-patch-name patch) patches-diff))
696 (move-to-column (stgit-goal-column)))
698 (defun stgit-collapse (&optional patches)
699 "Hide the contents selected patches, or the patch at point.
701 See also `stgit-expand'."
702 (interactive (list (stgit-patches-marked-or-at-point)))
704 (stgit-expand patches t))
706 (defun stgit-select-patch ()
707 (let ((patchname (stgit-patch-name-at-point)))
708 (stgit-expand (list patchname)
709 (memq patchname stgit-expanded-patches))))
711 (defun stgit-select ()
712 "With point on a patch, toggle showing files in the patch.
714 With point on a file, open the associated file. Opens the target
715 file for (applied) copies and renames."
718 (case (get-text-property (point) 'entry-type)
720 (stgit-select-patch))
724 (error "No patch or file on line"))))
726 (defun stgit-find-file-other-window ()
727 "Open file at point in other window"
732 (defun stgit-find-file-merge ()
733 "Open file at point and merge it using `smerge-ediff'."
740 "Hide the stgit buffer."
745 (defun stgit-git-status ()
746 "Show status using `git-status'."
749 (unless (fboundp 'git-status)
750 (error "The stgit-git-status command requires git-status"))
751 (let ((dir default-directory))
752 (save-selected-window
756 (defun stgit-goal-column ()
757 "Return goal column for the current line"
758 (case (get-text-property (point) 'entry-type)
763 (defun stgit-next-line (&optional arg)
764 "Move cursor vertically down ARG lines"
768 (move-to-column (stgit-goal-column)))
770 (defun stgit-previous-line (&optional arg)
771 "Move cursor vertically up ARG lines"
775 (move-to-column (stgit-goal-column)))
777 (defun stgit-next-patch (&optional arg)
778 "Move cursor down ARG patches."
781 (ewoc-goto-next stgit-ewoc (or arg 1))
782 (move-to-column goal-column))
784 (defun stgit-previous-patch (&optional arg)
785 "Move cursor up ARG patches."
788 (ewoc-goto-prev stgit-ewoc (or arg 1))
789 (move-to-column goal-column))
791 (defvar stgit-mode-hook nil
792 "Run after `stgit-mode' is setup.")
794 (defvar stgit-mode-map nil
795 "Keymap for StGit major mode.")
797 (unless stgit-mode-map
798 (let ((diff-map (make-keymap))
799 (toggle-map (make-keymap)))
800 (suppress-keymap diff-map)
801 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
802 '(("b" . stgit-diff-base)
803 ("c" . stgit-diff-combined)
804 ("m" . stgit-find-file-merge)
805 ("o" . stgit-diff-ours)
806 ("t" . stgit-diff-theirs)))
807 (suppress-keymap toggle-map)
808 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
809 '(("t" . stgit-toggle-worktree)
810 ("i" . stgit-toggle-ignored)
811 ("u" . stgit-toggle-unknown)))
812 (setq stgit-mode-map (make-keymap))
813 (suppress-keymap stgit-mode-map)
814 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
817 ("\d" . stgit-unmark-up)
818 ("u" . stgit-unmark-down)
821 ("\C-p" . stgit-previous-line)
822 ("\C-n" . stgit-next-line)
823 ([up] . stgit-previous-line)
824 ([down] . stgit-next-line)
825 ("p" . stgit-previous-patch)
826 ("n" . stgit-next-patch)
827 ("\M-{" . stgit-previous-patch)
828 ("\M-}" . stgit-next-patch)
829 ("s" . stgit-git-status)
830 ("g" . stgit-reload-or-repair)
831 ("r" . stgit-refresh)
832 ("\C-c\C-r" . stgit-rename)
834 ("M" . stgit-move-patches)
837 ("c" . stgit-new-and-refresh)
838 ("\C-c\C-c" . stgit-commit)
839 ("\C-c\C-u" . stgit-uncommit)
841 ("R" . stgit-resolve-file)
842 ("\r" . stgit-select)
844 ("-" . stgit-collapse)
845 ("o" . stgit-find-file-other-window)
846 ("i" . stgit-toggle-index)
847 (">" . stgit-push-next)
848 ("<" . stgit-pop-next)
849 ("P" . stgit-push-or-pop)
853 ([?\C-/] . stgit-undo)
854 ("\C-_" . stgit-undo)
855 ([?\C-c ?\C-/] . stgit-redo)
856 ("\C-c\C-_" . stgit-redo)
858 ("\C-c\C-b" . stgit-rebase)
861 ("q" . stgit-quit)))))
864 "Major mode for interacting with StGit.
866 Start StGit using \\[stgit].
870 \\[stgit-help] Show this help text
871 \\[stgit-quit] Hide the StGit buffer
872 \\[describe-bindings] Show all key bindings
874 \\[stgit-reload-or-repair] Reload the StGit buffer
875 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
877 \\[stgit-undo] Undo most recent StGit operation
878 \\[stgit-redo] Undo recent undo
880 \\[stgit-git-status] Run `git-status' (if available)
883 \\[stgit-previous-line] Move to previous line
884 \\[stgit-next-line] Move to next line
885 \\[stgit-previous-patch] Move to previous patch
886 \\[stgit-next-patch] Move to next patch
888 \\[stgit-mark] Mark patch
889 \\[stgit-unmark-up] Unmark patch and move up
890 \\[stgit-unmark-down] Unmark patch and move down
892 Commands for patches:
893 \\[stgit-select] Toggle showing changed files in patch
894 \\[stgit-refresh] Refresh patch with changes in index or work tree
895 \\[stgit-diff] Show the patch log and diff
897 \\[stgit-expand] Show changes in selected patches
898 \\[stgit-collapse] Hide changes in selected patches
900 \\[stgit-new] Create a new, empty patch
901 \\[stgit-new-and-refresh] Create a new patch from index or work tree
902 \\[stgit-rename] Rename patch
903 \\[stgit-edit] Edit patch description
904 \\[stgit-delete] Delete patch(es)
906 \\[stgit-revert] Revert all changes in index or work tree
907 \\[stgit-toggle-index] Toggle all changes between index and work tree
909 \\[stgit-push-next] Push next patch onto stack
910 \\[stgit-pop-next] Pop current patch from stack
911 \\[stgit-push-or-pop] Push or pop patch at point
912 \\[stgit-goto] Make current patch current by popping or pushing
914 \\[stgit-squash] Squash (meld together) patches
915 \\[stgit-move-patches] Move patch(es) to point
917 \\[stgit-commit] Commit patch(es)
918 \\[stgit-uncommit] Uncommit patch(es)
921 \\[stgit-select] Open the file in this window
922 \\[stgit-find-file-other-window] Open the file in another window
923 \\[stgit-diff] Show the file's diff
925 \\[stgit-toggle-index] Toggle change between index and work tree
926 \\[stgit-revert] Revert changes to file
929 \\[stgit-toggle-worktree] Toggle showing index and work tree
930 \\[stgit-toggle-unknown] Toggle showing unknown files
931 \\[stgit-toggle-ignored] Toggle showing ignored files
934 \\[stgit-diff] Show diff of patch or file
935 \\[stgit-diff-base] Show diff against the merge base
936 \\[stgit-diff-ours] Show diff against our branch
937 \\[stgit-diff-theirs] Show diff against their branch
939 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
940 ignore space changes.
941 With two prefix arguments (e.g., \\[universal-argument] \
942 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
944 Commands for merge conflicts:
945 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
946 \\[stgit-resolve-file] Mark unmerged file as resolved
948 Commands for branches:
949 \\[stgit-branch] Switch to another branch
950 \\[stgit-rebase] Rebase the current branch
952 Customization variables:
953 `stgit-abbreviate-copies-and-renames'
954 `stgit-default-show-worktree'
955 `stgit-find-copies-harder'
956 `stgit-show-worktree-mode'
958 See also \\[customize-group] for the \"stgit\" group."
959 (kill-all-local-variables)
960 (buffer-disable-undo)
961 (setq mode-name "StGit"
962 major-mode 'stgit-mode
964 (use-local-map stgit-mode-map)
965 (set (make-local-variable 'list-buffers-directory) default-directory)
966 (set (make-local-variable 'stgit-marked-patches) nil)
967 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
968 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
969 (set (make-local-variable 'stgit-index-node) nil)
970 (set (make-local-variable 'stgit-worktree-node) nil)
971 (set (make-local-variable 'parse-sexp-lookup-properties) t)
972 (set-variable 'truncate-lines 't)
973 (add-hook 'after-save-hook 'stgit-update-saved-file)
974 (run-hooks 'stgit-mode-hook))
976 (defun stgit-update-saved-file ()
977 (let* ((file (expand-file-name buffer-file-name))
978 (dir (file-name-directory file))
979 (gitdir (condition-case nil (git-get-top-dir dir)
981 (buffer (and gitdir (stgit-find-buffer gitdir))))
983 (with-current-buffer buffer
984 (stgit-refresh-worktree)))))
986 (defun stgit-add-mark (patchsym)
987 "Mark the patch PATCHSYM."
988 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
990 (defun stgit-remove-mark (patchsym)
991 "Unmark the patch PATCHSYM."
992 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
994 (defun stgit-clear-marks ()
995 "Unmark all patches."
996 (setq stgit-marked-patches '()))
998 (defun stgit-patch-at-point (&optional cause-error)
999 (get-text-property (point) 'patch-data))
1001 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1002 "Return the patch name on the current line as a symbol.
1003 If CAUSE-ERROR is not nil, signal an error if none found.
1004 If ONLY-PATCHES is not nil, only allow real patches, and not
1005 index or work tree."
1006 (let ((patch (stgit-patch-at-point)))
1009 (memq (stgit-patch-status patch) '(work index))
1012 (stgit-patch-name patch))
1014 (error "No patch on this line")))))
1016 (defun stgit-patched-file-at-point ()
1017 (get-text-property (point) 'file-data))
1019 (defun stgit-patches-marked-or-at-point ()
1020 "Return the symbols of the marked patches, or the patch on the current line."
1021 (if stgit-marked-patches
1022 stgit-marked-patches
1023 (let ((patch (stgit-patch-name-at-point)))
1028 (defun stgit-goto-patch (patchsym &optional file)
1029 "Move point to the line containing patch PATCHSYM.
1030 If that patch cannot be found, do nothing.
1032 If the patch was found and FILE is not nil, instead move to that
1033 file's line. If FILE cannot be found, stay on the line of
1035 (let ((node (ewoc-nth stgit-ewoc 0)))
1036 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
1038 (setq node (ewoc-next stgit-ewoc node)))
1039 (when (and node file)
1040 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
1041 (file-node (ewoc-nth file-ewoc 0)))
1042 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
1043 (setq file-node (ewoc-next file-ewoc file-node)))
1045 (ewoc-goto-node file-ewoc file-node)
1046 (move-to-column (stgit-goal-column))
1049 (ewoc-goto-node stgit-ewoc node)
1050 (move-to-column goal-column))))
1052 (defun stgit-init ()
1056 (stgit-capture-output nil
1060 (defun stgit-mark ()
1061 "Mark the patch under point."
1064 (let* ((node (ewoc-locate stgit-ewoc))
1065 (patch (ewoc-data node))
1066 (name (stgit-patch-name patch)))
1067 (when (eq name :work)
1068 (error "Cannot mark the work tree"))
1069 (when (eq name :index)
1070 (error "Cannot mark the index"))
1071 (stgit-add-mark (stgit-patch-name patch))
1072 (ewoc-invalidate stgit-ewoc node))
1075 (defun stgit-unmark-up ()
1076 "Remove mark from the patch on the previous line."
1079 (stgit-previous-patch)
1080 (let* ((node (ewoc-locate stgit-ewoc))
1081 (patch (ewoc-data node)))
1082 (stgit-remove-mark (stgit-patch-name patch))
1083 (ewoc-invalidate stgit-ewoc node))
1084 (move-to-column (stgit-goal-column)))
1086 (defun stgit-unmark-down ()
1087 "Remove mark from the patch on the current line."
1090 (let* ((node (ewoc-locate stgit-ewoc))
1091 (patch (ewoc-data node)))
1092 (stgit-remove-mark (stgit-patch-name patch))
1093 (ewoc-invalidate stgit-ewoc node))
1096 (defun stgit-rename (name)
1097 "Rename the patch under point to NAME."
1099 (read-string "Patch name: "
1100 (symbol-name (stgit-patch-name-at-point t t)))))
1102 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1103 (stgit-capture-output nil
1104 (stgit-run "rename" old-patchsym name))
1105 (let ((name-sym (intern name)))
1106 (when (memq old-patchsym stgit-expanded-patches)
1107 (setq stgit-expanded-patches
1108 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1109 (when (memq old-patchsym stgit-marked-patches)
1110 (setq stgit-marked-patches
1111 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1113 (stgit-goto-patch name-sym))))
1115 (defun stgit-reload-or-repair (repair)
1116 "Update the contents of the StGit buffer (`stgit-reload').
1118 With a prefix argument, repair the StGit metadata if the branch
1119 was modified with git commands (`stgit-repair')."
1126 (defun stgit-repair ()
1130 (stgit-capture-output nil
1131 (stgit-run "repair"))
1134 (defun stgit-available-branches ()
1135 "Returns a list of the available stg branches"
1136 (let ((output (with-output-to-string
1137 (stgit-run "branch" "--list")))
1140 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1141 (setq result (cons (match-string 1 output) result))
1142 (setq start (match-end 0)))
1145 (defun stgit-branch (branch)
1146 "Switch to branch BRANCH."
1147 (interactive (list (completing-read "Switch to branch: "
1148 (stgit-available-branches))))
1150 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1153 (defun stgit-available-refs (&optional omit-stgit)
1154 "Returns a list of the available git refs.
1155 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1156 (let* ((output (with-output-to-string
1157 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1158 "refs/tags" "refs/heads"
1160 (result (split-string output "\n" t)))
1162 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1163 (substring s (match-end 0))
1166 (delete-if (lambda (s)
1167 (string-match "^refs/heads/.*\\.stgit$" s))
1171 (defun stgit-rebase (new-base)
1172 "Rebase to NEW-BASE."
1173 (interactive (list (completing-read "Rebase to: "
1174 (stgit-available-refs t))))
1176 (stgit-capture-output nil (stgit-run "rebase" new-base))
1179 (defun stgit-commit (count)
1180 "Run stg commit on COUNT commits.
1181 Interactively, the prefix argument is used as COUNT.
1182 A negative COUNT will uncommit instead."
1186 (stgit-uncommit (- count))
1187 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1190 (defun stgit-uncommit (count)
1191 "Run stg uncommit on COUNT commits.
1192 Interactively, the prefix argument is used as COUNT.
1193 A negative COUNT will commit instead."
1197 (stgit-commit (- count))
1198 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1201 (defun stgit-neighbour-file ()
1202 "Return the file name of the next file after point, or the
1203 previous file if point is at the last file within a patch."
1204 (let ((old-point (point))
1206 (and (zerop (forward-line 1))
1207 (let ((f (stgit-patched-file-at-point)))
1208 (and f (setq neighbour-file (stgit-file-file f)))))
1209 (goto-char old-point)
1210 (unless neighbour-file
1211 (and (zerop (forward-line -1))
1212 (let ((f (stgit-patched-file-at-point)))
1213 (and f (setq neighbour-file (stgit-file-file f)))))
1214 (goto-char old-point))
1217 (defun stgit-revert-file ()
1218 "Revert the file at point, which must be in the index or the
1222 (let* ((patched-file (or (stgit-patched-file-at-point)
1223 (error "No file on the current line")))
1224 (patch-name (stgit-patch-name-at-point))
1225 (file-status (stgit-file-status patched-file))
1226 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1227 (stgit-file-cr-to patched-file))
1228 ((eq file-status 'add)
1229 (stgit-file-file patched-file))))
1230 (co-file (cond ((eq file-status 'rename)
1231 (stgit-file-cr-from patched-file))
1232 ((not (memq file-status '(copy add)))
1233 (stgit-file-file patched-file))))
1234 (next-file (stgit-neighbour-file)))
1236 (unless (memq patch-name '(:work :index))
1237 (error "No index or working tree file on this line"))
1239 (when (eq file-status 'ignore)
1240 (error "Cannot revert ignored files"))
1242 (when (eq file-status 'unknown)
1243 (error "Cannot revert unknown files"))
1245 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1246 (when (yes-or-no-p (format "Revert %d file%s? "
1248 (if (= nfiles 1) "" "s")))
1249 (stgit-capture-output nil
1251 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1253 (stgit-run-git "checkout" "HEAD" co-file)))
1255 (stgit-goto-patch patch-name next-file)))))
1257 (defun stgit-revert ()
1258 "Revert the change at point, which must be the index, the work
1259 tree, or a single change in either."
1262 (let ((patched-file (stgit-patched-file-at-point)))
1265 (let* ((patch-name (or (stgit-patch-name-at-point)
1266 (error "No patch or file at point")))
1267 (patch-desc (case patch-name
1270 (t (error (substitute-command-keys
1271 "Use \\[stgit-delete] to delete a patch"))))))
1272 (when (if (eq patch-name :work)
1273 (stgit-work-tree-empty-p)
1274 (stgit-index-empty-p))
1275 (error (format "There are no changes in the %s to revert"
1277 (and (eq patch-name :index)
1278 (not (stgit-work-tree-empty-p))
1279 (error "Cannot revert index as work tree contains unstaged changes"))
1281 (when (yes-or-no-p (format "Revert all changes in the %s? "
1283 (if (eq patch-name :index)
1284 (stgit-run-git-silent "reset" "--hard" "-q")
1285 (stgit-run-git-silent "checkout" "--" "."))
1286 (stgit-refresh-index)
1287 (stgit-refresh-worktree)
1288 (stgit-goto-patch patch-name))))))
1290 (defun stgit-resolve-file ()
1291 "Resolve conflict in the file at point."
1294 (let* ((patched-file (stgit-patched-file-at-point))
1295 (patch (stgit-patch-at-point))
1296 (patch-name (and patch (stgit-patch-name patch)))
1297 (status (and patched-file (stgit-file-status patched-file))))
1299 (unless (memq patch-name '(:work :index))
1300 (error "No index or working tree file on this line"))
1302 (unless (eq status 'unmerged)
1303 (error "No conflict to resolve at the current line"))
1305 (stgit-capture-output nil
1306 (stgit-move-change-to-index (stgit-file-file patched-file)))
1310 (defun stgit-push-next (npatches)
1311 "Push the first unapplied patch.
1312 With numeric prefix argument, push that many patches."
1315 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1317 (stgit-refresh-git-status))
1319 (defun stgit-pop-next (npatches)
1320 "Pop the topmost applied patch.
1321 With numeric prefix argument, pop that many patches."
1324 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1326 (stgit-refresh-git-status))
1328 (defun stgit-applied-at-point-p ()
1329 "Return non-nil if the patch at point is applied."
1330 (let ((patch (stgit-patch-at-point t)))
1331 (not (eq (stgit-patch-status patch) 'unapplied))))
1333 (defun stgit-push-or-pop ()
1334 "Push or pop the patch on the current line."
1337 (let ((patchsym (stgit-patch-name-at-point t t))
1338 (applied (stgit-applied-at-point-p)))
1339 (stgit-capture-output nil
1340 (stgit-run (if applied "pop" "push") patchsym))
1343 (defun stgit-goto ()
1344 "Go to the patch on the current line."
1347 (let ((patchsym (stgit-patch-name-at-point t)))
1348 (stgit-capture-output nil
1349 (stgit-run "goto" patchsym))
1352 (defun stgit-id (patchsym)
1353 "Return the git commit id for PATCHSYM.
1354 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1355 (if (keywordp patchsym)
1357 (let ((result (with-output-to-string
1358 (stgit-run-silent "id" patchsym))))
1359 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1360 (error "Cannot find commit id for %s" patchsym))
1361 (match-string 1 result))))
1363 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1364 "Show the patch on the current line.
1366 UNMERGED-STAGE is the argument to `git-diff' that that selects
1367 which stage to diff against in the case of unmerged files."
1368 (let ((space-arg (when (numberp ignore-whitespace)
1369 (cond ((> ignore-whitespace 4)
1370 "--ignore-all-space")
1371 ((> ignore-whitespace 1)
1372 "--ignore-space-change"))))
1373 (patch-name (stgit-patch-name-at-point t)))
1374 (stgit-capture-output "*StGit patch*"
1375 (case (get-text-property (point) 'entry-type)
1377 (let* ((patched-file (stgit-patched-file-at-point))
1378 (patch-id (let ((id (stgit-id patch-name)))
1379 (if (and (eq id :index)
1380 (eq (stgit-file-status patched-file)
1384 (args (append (and space-arg (list space-arg))
1385 (and (stgit-file-cr-from patched-file)
1386 (list (stgit-find-copies-harder-diff-arg)))
1387 (cond ((eq patch-id :index)
1389 ((eq patch-id :work)
1390 (list unmerged-stage))
1392 (list (concat patch-id "^") patch-id)))
1394 (if (stgit-file-copy-or-rename patched-file)
1395 (list (stgit-file-cr-from patched-file)
1396 (stgit-file-cr-to patched-file))
1397 (list (stgit-file-file patched-file))))))
1398 (apply 'stgit-run-git "diff" args)))
1400 (let* ((patch-id (stgit-id patch-name)))
1401 (if (or (eq patch-id :index) (eq patch-id :work))
1402 (apply 'stgit-run-git "diff"
1403 (stgit-find-copies-harder-diff-arg)
1404 (append (and space-arg (list space-arg))
1405 (if (eq patch-id :index)
1407 (list unmerged-stage))))
1408 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1409 (and space-arg (list "-O" space-arg))
1410 (list (stgit-patch-name-at-point)))))
1411 (apply 'stgit-run args)))))
1413 (error "No patch or file at point")))
1414 (with-current-buffer standard-output
1415 (goto-char (point-min))
1418 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1419 `(defun ,name (&optional ignore-whitespace)
1420 ,(format "Show the patch on the current line.
1422 %sWith a prefix argument, ignore whitespace. With a prefix argument
1423 greater than four (e.g., \\[universal-argument] \
1424 \\[universal-argument] \\[%s]), ignore all whitespace."
1426 (format "For unmerged files, %s.\n\n" unmerged-action)
1431 (stgit-show-patch ,diff-arg ignore-whitespace)))
1433 (stgit-define-diff stgit-diff
1435 (stgit-define-diff stgit-diff-ours
1437 "diff against our branch")
1438 (stgit-define-diff stgit-diff-theirs
1440 "diff against their branch")
1441 (stgit-define-diff stgit-diff-base
1443 "diff against the merge base")
1444 (stgit-define-diff stgit-diff-combined
1446 "show a combined diff")
1448 (defun stgit-move-change-to-index (file &optional force)
1449 "Copies the work tree state of FILE to index, using git add or git rm.
1451 If FORCE is not nil, use --force."
1452 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1453 '("add") '("rm" "-q"))))
1454 (stgit-capture-output "*git output*"
1455 (apply 'stgit-run-git (append op (and force '("--force"))
1456 '("--") (list file))))))
1458 (defun stgit-remove-change-from-index (file)
1459 "Unstages the change in FILE from the index"
1460 (stgit-capture-output "*git output*"
1461 (stgit-run-git "reset" "-q" "--" file)))
1463 (defun stgit-git-index-unmerged-p ()
1465 (with-output-to-string
1466 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1468 "--quiet" "HEAD")))))
1471 (defun stgit-file-toggle-index ()
1472 "Move modified file in or out of the index.
1474 Leaves the point where it is, but moves the mark to where the
1475 file ended up. You can then jump to the file with \
1476 \\[exchange-point-and-mark]."
1479 (let* ((patched-file (or (stgit-patched-file-at-point)
1480 (error "No file on the current line")))
1481 (patched-status (stgit-file-status patched-file)))
1482 (when (eq patched-status 'unmerged)
1483 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1484 (let* ((patch (stgit-patch-at-point))
1485 (patch-name (stgit-patch-name patch))
1486 (mark-file (if (eq patched-status 'rename)
1487 (stgit-file-cr-to patched-file)
1488 (stgit-file-file patched-file)))
1489 (point-file (if (eq patched-status 'rename)
1490 (stgit-file-cr-from patched-file)
1491 (stgit-neighbour-file))))
1493 (cond ((eq patch-name :work)
1494 (stgit-move-change-to-index (stgit-file-file patched-file)
1495 (eq patched-status 'ignore)))
1496 ((eq patch-name :index)
1497 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1499 (error "Can only move files between working tree and index")))
1500 (stgit-refresh-worktree)
1501 (stgit-refresh-index)
1502 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1504 (stgit-goto-patch patch-name point-file))))
1506 (defun stgit-toggle-index ()
1507 "Move change in or out of the index.
1509 Works on index and work tree, as well as files in either.
1511 Leaves the point where it is, but moves the mark to where the
1512 file ended up. You can then jump to the file with \
1513 \\[exchange-point-and-mark]."
1516 (if (stgit-patched-file-at-point)
1517 (stgit-file-toggle-index)
1518 (let ((patch-name (stgit-patch-name-at-point)))
1519 (unless (memq patch-name '(:index :work))
1520 (error "Can only move changes between working tree and index"))
1521 (when (stgit-git-index-unmerged-p)
1522 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1523 (if (if (eq patch-name :index)
1524 (stgit-index-empty-p)
1525 (stgit-work-tree-empty-p))
1526 (message "No changes to be moved")
1527 (stgit-capture-output nil
1528 (if (eq patch-name :work)
1529 (stgit-run-git "add" "--update")
1530 (stgit-run-git "reset" "--mixed" "-q")))
1531 (stgit-refresh-worktree)
1532 (stgit-refresh-index))
1533 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1535 (defun stgit-edit ()
1536 "Edit the patch on the current line."
1539 (let ((patchsym (stgit-patch-name-at-point t t))
1540 (edit-buf (get-buffer-create "*StGit edit*"))
1541 (dir default-directory))
1542 (log-edit 'stgit-confirm-edit t nil edit-buf)
1543 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1544 (setq default-directory dir)
1545 (let ((standard-output edit-buf))
1546 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1548 (defun stgit-confirm-edit ()
1550 (let ((file (make-temp-file "stgit-edit-")))
1551 (write-region (point-min) (point-max) file)
1552 (stgit-capture-output nil
1553 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1554 (with-current-buffer log-edit-parent-buffer
1557 (defun stgit-new (add-sign &optional refresh)
1558 "Create a new patch.
1559 With a prefix argument, include a \"Signed-off-by:\" line at the
1563 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1564 (dir default-directory))
1565 (log-edit 'stgit-confirm-new t nil edit-buf)
1566 (setq default-directory dir)
1567 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1570 (let ((standard-output (current-buffer)))
1571 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1573 (defun stgit-confirm-new ()
1575 (let ((file (make-temp-file "stgit-edit-"))
1576 (refresh stgit-refresh-after-new))
1577 (write-region (point-min) (point-max) file)
1578 (stgit-capture-output nil
1579 (stgit-run "new" "-f" file))
1580 (with-current-buffer log-edit-parent-buffer
1585 (defun stgit-new-and-refresh (add-sign)
1586 "Create a new patch and refresh it with the current changes.
1588 With a prefix argument, include a \"Signed-off-by:\" line at the
1591 This works just like running `stgit-new' followed by `stgit-refresh'."
1594 (stgit-new add-sign t))
1596 (defun stgit-create-patch-name (description)
1597 "Create a patch name from a long description"
1599 (while (> (length description) 0)
1600 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1601 (setq patch (downcase (concat patch
1602 (match-string 0 description))))
1603 (setq description (substring description (match-end 0))))
1604 ((string-match "\\` +" description)
1605 (setq patch (concat patch "-"))
1606 (setq description (substring description (match-end 0))))
1607 ((string-match "\\`[^a-zA-Z_-]+" description)
1608 (setq description (substring description (match-end 0))))))
1609 (cond ((= (length patch) 0)
1611 ((> (length patch) 20)
1612 (substring patch 0 20))
1615 (defun stgit-delete (patchsyms &optional spill-p)
1616 "Delete the patches in PATCHSYMS.
1617 Interactively, delete the marked patches, or the patch at point.
1619 With a prefix argument, or SPILL-P, spill the patch contents to
1620 the work tree and index."
1621 (interactive (list (stgit-patches-marked-or-at-point)
1622 current-prefix-arg))
1625 (error "No patches to delete"))
1626 (when (memq :index patchsyms)
1627 (error "Cannot delete the index"))
1628 (when (memq :work patchsyms)
1629 (error "Cannot delete the work tree"))
1631 (let ((npatches (length patchsyms)))
1632 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1634 (if (= 1 npatches) "" "es")
1636 " (spilling contents to index)"
1638 (let ((args (if spill-p
1639 (cons "--spill" patchsyms)
1641 (stgit-capture-output nil
1642 (apply 'stgit-run "delete" args))
1645 (defun stgit-move-patches-target ()
1646 "Return the patchsym indicating a target patch for
1647 `stgit-move-patches'.
1649 This is either the patch at point, or one of :top and :bottom, if
1650 the point is after or before the applied patches."
1652 (let ((patchsym (stgit-patch-name-at-point nil t)))
1653 (cond (patchsym patchsym)
1654 ((save-excursion (re-search-backward "^>" nil t)) :top)
1657 (defun stgit-sort-patches (patchsyms)
1658 "Returns the list of patches in PATCHSYMS sorted according to
1659 their position in the patch series, bottommost first.
1661 PATCHSYMS must not contain duplicate entries."
1662 (let (sorted-patchsyms
1663 (series (with-output-to-string
1664 (with-current-buffer standard-output
1665 (stgit-run-silent "series" "--noprefix"))))
1667 (while (string-match "^\\(.+\\)" series start)
1668 (let ((patchsym (intern (match-string 1 series))))
1669 (when (memq patchsym patchsyms)
1670 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1671 (setq start (match-end 0)))
1672 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1674 (unless (= (length patchsyms) (length sorted-patchsyms))
1675 (error "Internal error"))
1679 (defun stgit-move-patches (patchsyms target-patch)
1680 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1681 If TARGET-PATCH is :bottom or :top, move the patches to the
1682 bottom or top of the stack, respectively.
1684 Interactively, move the marked patches to where the point is."
1685 (interactive (list stgit-marked-patches
1686 (stgit-move-patches-target)))
1689 (error "Need at least one patch to move"))
1691 (unless target-patch
1692 (error "Point not at a patch"))
1694 (if (eq target-patch :top)
1695 (stgit-capture-output nil
1696 (apply 'stgit-run "float" patchsyms))
1698 ;; need to have patchsyms sorted by position in the stack
1699 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1700 (while sorted-patchsyms
1701 (setq sorted-patchsyms
1702 (and (stgit-capture-output nil
1703 (if (eq target-patch :bottom)
1704 (stgit-run "sink" "--" (car sorted-patchsyms))
1705 (stgit-run "sink" "--to" target-patch "--"
1706 (car sorted-patchsyms))))
1707 (cdr sorted-patchsyms))))))
1710 (defun stgit-squash (patchsyms)
1711 "Squash the patches in PATCHSYMS.
1712 Interactively, squash the marked patches.
1714 Unless there are any conflicts, the patches will be merged into
1715 one patch, which will occupy the same spot in the series as the
1716 deepest patch had before the squash."
1717 (interactive (list stgit-marked-patches))
1719 (when (< (length patchsyms) 2)
1720 (error "Need at least two patches to squash"))
1721 (let ((stgit-buffer (current-buffer))
1722 (edit-buf (get-buffer-create "*StGit edit*"))
1723 (dir default-directory)
1724 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1725 (log-edit 'stgit-confirm-squash t nil edit-buf)
1726 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1727 (setq default-directory dir)
1728 (let ((result (let ((standard-output edit-buf))
1729 (apply 'stgit-run-silent "squash"
1730 "--save-template=-" sorted-patchsyms))))
1732 ;; stg squash may have reordered the patches or caused conflicts
1733 (with-current-buffer stgit-buffer
1736 (unless (eq 0 result)
1738 (rename-buffer "*StGit error*")
1739 (resize-temp-buffer-window)
1740 (switch-to-buffer-other-window stgit-buffer)
1741 (error "stg squash failed")))))
1743 (defun stgit-confirm-squash ()
1745 (let ((file (make-temp-file "stgit-edit-")))
1746 (write-region (point-min) (point-max) file)
1747 (stgit-capture-output nil
1748 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1749 (with-current-buffer log-edit-parent-buffer
1751 ;; Go to first marked patch and stay there
1752 (goto-char (point-min))
1753 (re-search-forward (concat "^[>+-]\\*") nil t)
1754 (move-to-column goal-column)
1755 (let ((pos (point)))
1759 (defun stgit-help ()
1760 "Display help for the StGit mode."
1762 (describe-function 'stgit-mode))
1764 (defun stgit-undo (&optional arg)
1766 With prefix argument, run it with the --hard flag.
1768 See also `stgit-redo'."
1771 (stgit-capture-output nil
1773 (stgit-run "undo" "--hard")
1774 (stgit-run "undo")))
1777 (defun stgit-redo (&optional arg)
1779 With prefix argument, run it with the --hard flag.
1781 See also `stgit-undo'."
1784 (stgit-capture-output nil
1786 (stgit-run "redo" "--hard")
1787 (stgit-run "redo")))
1790 (defun stgit-refresh (&optional arg)
1792 If the index contains any changes, only refresh from index.
1794 With prefix argument, refresh the marked patch or the patch under point."
1797 (let ((patchargs (if arg
1798 (let ((patches (stgit-patches-marked-or-at-point)))
1799 (cond ((null patches)
1800 (error "No patch to update"))
1801 ((> (length patches) 1)
1802 (error "Too many patches selected"))
1804 (cons "-p" patches))))
1806 (unless (stgit-index-empty-p)
1807 (setq patchargs (cons "--index" patchargs)))
1808 (stgit-capture-output nil
1809 (apply 'stgit-run "refresh" patchargs))
1810 (stgit-refresh-git-status))
1813 (defvar stgit-show-worktree nil
1814 "If nil, inhibit showing work tree and index in the stgit buffer.
1816 See also `stgit-show-worktree-mode'.")
1818 (defvar stgit-show-ignored nil
1819 "If nil, inhibit showing files ignored by git.")
1821 (defvar stgit-show-unknown nil
1822 "If nil, inhibit showing files not registered with git.")
1824 (defun stgit-toggle-worktree (&optional arg)
1825 "Toggle the visibility of the work tree.
1826 With ARG, show the work tree if ARG is positive.
1828 Its initial setting is controlled by `stgit-default-show-worktree'.
1830 `stgit-show-worktree-mode' controls where on screen the index and
1831 work tree will show up."
1834 (setq stgit-show-worktree
1837 (not stgit-show-worktree)))
1840 (defun stgit-toggle-ignored (&optional arg)
1841 "Toggle the visibility of files ignored by git in the work
1842 tree. With ARG, show these files if ARG is positive.
1844 Use \\[stgit-toggle-worktree] to show the work tree."
1847 (setq stgit-show-ignored
1850 (not stgit-show-ignored)))
1853 (defun stgit-toggle-unknown (&optional arg)
1854 "Toggle the visibility of files not registered with git in the
1855 work tree. With ARG, show these files if ARG is positive.
1857 Use \\[stgit-toggle-worktree] to show the work tree."
1860 (setq stgit-show-unknown
1863 (not stgit-show-unknown)))