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 (unless (fboundp 'git-get-top-dir)
28 (defun git-get-top-dir (dir)
29 "Retrieve the top-level directory of a git tree."
30 (let ((cdup (with-output-to-string
31 (with-current-buffer standard-output
33 (unless (eq 0 (call-process "git" nil t nil
34 "rev-parse" "--show-cdup"))
35 (error "Cannot find top-level git tree for %s" dir))))))
36 (expand-file-name (concat (file-name-as-directory dir)
37 (car (split-string cdup "\n")))))))
39 (defun stgit-refresh-git-status (&optional dir)
40 "If it exists, refresh the `git-status' buffer belonging to
41 directory DIR or `default-directory'"
42 (when (and (fboundp 'git-find-status-buffer)
43 (fboundp 'git-refresh-status))
44 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
45 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
46 (when git-status-buffer
47 (with-current-buffer git-status-buffer
48 (git-refresh-status))))))
50 (defun stgit-find-buffer (dir)
51 "Return the buffer displaying StGit patches for DIR, or nil if none."
52 (setq dir (file-name-as-directory dir))
53 (let ((buffers (buffer-list)))
55 (not (with-current-buffer (car buffers)
56 (and (eq major-mode 'stgit-mode)
57 (string= default-directory dir)))))
58 (setq buffers (cdr buffers)))
59 (and buffers (car buffers))))
61 (defun switch-to-stgit-buffer (dir)
62 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
63 (setq dir (file-name-as-directory dir))
64 (let ((buffer (stgit-find-buffer dir)))
65 (switch-to-buffer (or buffer
66 (create-stgit-buffer dir)))))
68 (defstruct (stgit-patch)
69 status name desc empty files-ewoc)
71 (defun stgit-patch-pp (patch)
72 (let* ((status (stgit-patch-status patch))
74 (name (stgit-patch-name patch))
75 (face (cdr (assq status stgit-patch-status-face-alist))))
81 (if (memq name stgit-marked-patches)
83 (if (memq status '(index work))
84 (insert (propertize (if (eq status 'index) "Index" "Work tree")
86 (insert (format "%-30s"
87 (propertize (symbol-name name)
89 'syntax-table (string-to-syntax "w")))
91 (if (stgit-patch-empty patch) "(empty) " "")
92 (propertize (or (stgit-patch-desc patch) "")
93 'face 'stgit-description-face)))
95 (put-text-property start (point) 'entry-type 'patch)
96 (when (memq name stgit-expanded-patches)
97 (stgit-insert-patch-files patch))
98 (put-text-property start (point) 'patch-data patch)))
100 (defun create-stgit-buffer (dir)
101 "Create a buffer for showing StGit patches.
102 Argument DIR is the repository path."
103 (let ((buf (create-file-buffer (concat dir "*stgit*")))
104 (inhibit-read-only t))
105 (with-current-buffer buf
106 (setq default-directory dir)
108 (set (make-local-variable 'stgit-ewoc)
109 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
110 (setq buffer-read-only t))
113 (defmacro stgit-capture-output (name &rest body)
114 "Capture StGit output and, if there was any output, show it in a window
116 Returns nil if there was no output."
117 (declare (debug ([&or stringp null] body))
119 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
120 (stgit-dir default-directory)
121 (inhibit-read-only t))
122 (with-current-buffer output-buf
124 (setq default-directory stgit-dir)
125 (setq buffer-read-only t))
126 (let ((standard-output output-buf))
128 (with-current-buffer output-buf
129 (set-buffer-modified-p nil)
130 (setq buffer-read-only t)
131 (if (< (point-min) (point-max))
132 (display-buffer output-buf t)))))
134 (defun stgit-make-run-args (args)
135 "Return a copy of ARGS with its elements converted to strings."
137 ;; don't use (format "%s" ...) to limit type errors
138 (cond ((stringp x) x)
139 ((integerp x) (number-to-string x))
140 ((symbolp x) (symbol-name x))
142 (error "Bad element in stgit-make-run-args args: %S" x))))
145 (defun stgit-run-silent (&rest args)
146 (setq args (stgit-make-run-args args))
147 (apply 'call-process "stg" nil standard-output nil args))
149 (defun stgit-run (&rest args)
150 (setq args (stgit-make-run-args args))
151 (let ((msgcmd (mapconcat #'identity args " ")))
152 (message "Running stg %s..." msgcmd)
153 (apply 'call-process "stg" nil standard-output nil args)
154 (message "Running stg %s...done" msgcmd)))
156 (defun stgit-run-git (&rest args)
157 (setq args (stgit-make-run-args args))
158 (let ((msgcmd (mapconcat #'identity args " ")))
159 (message "Running git %s..." msgcmd)
160 (apply 'call-process "git" nil standard-output nil args)
161 (message "Running git %s...done" msgcmd)))
163 (defun stgit-run-git-silent (&rest args)
164 (setq args (stgit-make-run-args args))
165 (apply 'call-process "git" nil standard-output nil args))
167 (defun stgit-index-empty-p ()
168 "Returns non-nil if the index contains no changes from HEAD."
169 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
171 (defvar stgit-index-node)
172 (defvar stgit-worktree-node)
174 (defun stgit-refresh-index ()
175 (when stgit-index-node
176 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
178 (defun stgit-refresh-worktree ()
179 (when stgit-worktree-node
180 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
182 (defun stgit-run-series-insert-index (ewoc)
183 (setq index-node (cons ewoc (ewoc-enter-last ewoc
189 worktree-node (cons ewoc (ewoc-enter-last ewoc
196 (defun stgit-run-series (ewoc)
197 (setq stgit-index-node nil
198 stgit-worktree-node nil)
199 (let ((inserted-index (not stgit-show-worktree))
204 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
205 (goto-char (point-min))
206 (if (not (zerop exit-status))
207 (cond ((looking-at "stg series: \\(.*\\)")
208 (setq inserted-index t)
209 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
210 (substitute-command-keys
211 "-- not initialized; run \\[stgit-init]")))
213 (error "Error running stg: %s"
217 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
218 (error "Syntax error in output from stg series"))
219 (let* ((state-str (match-string 2))
220 (state (cond ((string= state-str ">") 'top)
221 ((string= state-str "+") 'applied)
222 ((string= state-str "-") 'unapplied)))
223 (name (intern (match-string 4)))
224 (desc (match-string 5))
225 (empty (string= (match-string 1) "0")))
226 (unless inserted-index
227 (when (or (eq stgit-show-worktree-mode 'top)
228 (and (eq stgit-show-worktree-mode 'center)
229 (eq state 'unapplied)))
230 (setq inserted-index t)
231 (stgit-run-series-insert-index ewoc)))
232 (setq all-patchsyms (cons name all-patchsyms))
233 (ewoc-enter-last ewoc
240 (unless inserted-index
241 (stgit-run-series-insert-index ewoc)))
242 (setq stgit-index-node index-node
243 stgit-worktree-node worktree-node
244 stgit-marked-patches (intersection stgit-marked-patches
247 (defun stgit-reload ()
248 "Update the contents of the StGit buffer."
250 (let ((inhibit-read-only t)
251 (curline (line-number-at-pos))
252 (curpatch (stgit-patch-name-at-point))
253 (curfile (stgit-patched-file-at-point)))
254 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
255 (ewoc-set-hf stgit-ewoc
259 (stgit-run-silent "branch")
260 (buffer-substring (point-min) (1- (point-max))))
261 'face 'stgit-branch-name-face)
263 (if stgit-show-worktree
266 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
267 shows the working tree\n")
268 'face 'stgit-description-face)))
269 (stgit-run-series stgit-ewoc)
271 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
272 (goto-line curline)))
273 (stgit-refresh-git-status))
276 "A user interface for the StGit patch maintenance tool.
278 See `stgit-mode' for more information."
281 (defface stgit-description-face
282 '((((background dark)) (:foreground "tan"))
283 (((background light)) (:foreground "dark red")))
284 "The face used for StGit descriptions"
287 (defface stgit-branch-name-face
289 "The face used for the StGit branch name"
292 (defface stgit-top-patch-face
293 '((((background dark)) (:weight bold :foreground "yellow"))
294 (((background light)) (:weight bold :foreground "purple"))
296 "The face used for the top patch names"
299 (defface stgit-applied-patch-face
300 '((((background dark)) (:foreground "light yellow"))
301 (((background light)) (:foreground "purple"))
303 "The face used for applied patch names"
306 (defface stgit-unapplied-patch-face
307 '((((background dark)) (:foreground "gray80"))
308 (((background light)) (:foreground "orchid"))
310 "The face used for unapplied patch names"
313 (defface stgit-modified-file-face
314 '((((class color) (background light)) (:foreground "purple"))
315 (((class color) (background dark)) (:foreground "salmon")))
316 "StGit mode face used for modified file status"
319 (defface stgit-unmerged-file-face
320 '((((class color) (background light)) (:foreground "red" :bold t))
321 (((class color) (background dark)) (:foreground "red" :bold t)))
322 "StGit mode face used for unmerged file status"
325 (defface stgit-unknown-file-face
326 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
327 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
328 "StGit mode face used for unknown file status"
331 (defface stgit-ignored-file-face
332 '((((class color) (background light)) (:foreground "grey60"))
333 (((class color) (background dark)) (:foreground "grey40")))
334 "StGit mode face used for ignored files")
336 (defface stgit-file-permission-face
337 '((((class color) (background light)) (:foreground "green" :bold t))
338 (((class color) (background dark)) (:foreground "green" :bold t)))
339 "StGit mode face used for permission changes."
342 (defface stgit-index-work-tree-title-face
343 '((((supports :slant italic)) :slant italic)
345 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
349 (defcustom stgit-find-copies-harder
351 "Try harder to find copied files when listing patches.
353 When not nil, runs git diff-tree with the --find-copies-harder
354 flag, which reduces performance."
358 (defconst stgit-file-status-code-strings
359 (mapcar (lambda (arg)
361 (propertize (cadr arg) 'face (car (cddr arg)))))
362 '((add "Added" stgit-modified-file-face)
363 (copy "Copied" stgit-modified-file-face)
364 (delete "Deleted" stgit-modified-file-face)
365 (modify "Modified" stgit-modified-file-face)
366 (rename "Renamed" stgit-modified-file-face)
367 (mode-change "Mode change" stgit-modified-file-face)
368 (unmerged "Unmerged" stgit-unmerged-file-face)
369 (unknown "Unknown" stgit-unknown-file-face)
370 (ignore "Ignored" stgit-ignored-file-face)))
371 "Alist of code symbols to description strings")
373 (defconst stgit-patch-status-face-alist
374 '((applied . stgit-applied-patch-face)
375 (top . stgit-top-patch-face)
376 (unapplied . stgit-unapplied-patch-face)
377 (index . stgit-index-work-tree-title-face)
378 (work . stgit-index-work-tree-title-face))
379 "Alist of face to use for a given patch status")
381 (defun stgit-file-status-code-as-string (file)
382 "Return stgit status code for FILE as a string"
383 (let* ((code (assq (stgit-file-status file)
384 stgit-file-status-code-strings))
385 (score (stgit-file-cr-score file)))
388 (if (and score (/= score 100))
389 (format "%s %s" (cdr code)
390 (propertize (format "%d%%" score)
391 'face 'stgit-description-face))
394 (defun stgit-file-status-code (str &optional score)
395 "Return stgit status code from git status string"
396 (let ((code (assoc str '(("A" . add)
405 (setq code (if code (cdr code) 'unknown))
406 (when (stringp score)
407 (if (> (length score) 0)
408 (setq score (string-to-number score))
410 (if score (cons code score) code)))
412 (defconst stgit-file-type-strings
415 (#o160 . "subproject"))
416 "Alist of names of file types")
418 (defun stgit-file-type-string (type)
419 "Return string describing file type TYPE (the high bits of file permission).
420 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
421 (let ((type-str (assoc type stgit-file-type-strings)))
422 (or (and type-str (cdr type-str))
423 (format "unknown type %o" type))))
425 (defun stgit-file-type-change-string (old-perm new-perm)
426 "Return string describing file type change from OLD-PERM to NEW-PERM.
427 Cf. `stgit-file-type-string'."
428 (let ((old-type (lsh old-perm -9))
429 (new-type (lsh new-perm -9)))
430 (cond ((= old-type new-type) "")
431 ((zerop new-type) "")
433 (if (= new-type #o100)
435 (format " (%s)" (stgit-file-type-string new-type))))
436 (t (format " (%s -> %s)"
437 (stgit-file-type-string old-type)
438 (stgit-file-type-string new-type))))))
440 (defun stgit-file-mode-change-string (old-perm new-perm)
441 "Return string describing file mode change from OLD-PERM to NEW-PERM.
442 Cf. `stgit-file-type-change-string'."
443 (setq old-perm (logand old-perm #o777)
444 new-perm (logand new-perm #o777))
445 (if (or (= old-perm new-perm)
449 (let* ((modified (logxor old-perm new-perm))
450 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
451 (cond ((zerop modified) "")
452 ((and (zerop not-x-modified)
453 (or (and (eq #o111 (logand old-perm #o111))
454 (propertize "-x" 'face 'stgit-file-permission-face))
455 (and (eq #o111 (logand new-perm #o111))
456 (propertize "+x" 'face
457 'stgit-file-permission-face)))))
458 (t (concat (propertize (format "%o" old-perm)
459 'face 'stgit-file-permission-face)
461 'face 'stgit-description-face)
462 (propertize (format "%o" new-perm)
463 'face 'stgit-file-permission-face)))))))
465 (defstruct (stgit-file)
466 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
468 (defun stgit-describe-copy-or-rename (file)
469 (let (from to common-head common-tail
471 (propertize "->" 'face 'stgit-description-face)
474 (when stgit-abbreviate-copies-and-renames
475 (setq from (split-string (stgit-file-cr-from file) "/")
476 to (split-string (stgit-file-cr-to file) "/"))
478 (while (and from to (cdr from) (cdr to)
479 (string-equal (car from) (car to)))
480 (setq common-head (cons (car from) common-head)
483 (setq common-head (nreverse common-head)
486 (while (and from to (cdr from) (cdr to)
487 (string-equal (car from) (car to)))
488 (setq common-tail (cons (car from) common-tail)
491 (setq from (nreverse from)
494 (if (or common-head common-tail)
495 (concat (if common-head
496 (mapconcat #'identity common-head "/")
498 (if common-head "/" "")
499 (propertize "{" 'face 'stgit-description-face)
500 (mapconcat #'identity from "/")
502 (mapconcat #'identity to "/")
503 (propertize "}" 'face 'stgit-description-face)
504 (if common-tail "/" "")
506 (mapconcat #'identity common-tail "/")
508 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
510 (defun stgit-file-pp (file)
511 (let ((status (stgit-file-status file))
512 (name (if (stgit-file-copy-or-rename file)
513 (stgit-describe-copy-or-rename file)
514 (stgit-file-file file)))
515 (mode-change (stgit-file-mode-change-string
516 (stgit-file-old-perm file)
517 (stgit-file-new-perm file)))
519 (insert (format " %-12s%s%s%s%s\n"
520 (stgit-file-status-code-as-string file)
522 (if (zerop (length mode-change)) "" " ")
524 (propertize (stgit-file-type-change-string
525 (stgit-file-old-perm file)
526 (stgit-file-new-perm file))
527 'face 'stgit-description-face)))
528 (add-text-properties start (point)
529 (list 'entry-type 'file
532 (defun stgit-find-copies-harder-diff-arg ()
533 "Return the flag to use with `git-diff' depending on the
534 `stgit-find-copies-harder' flag."
535 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
537 (defun stgit-insert-ls-files (args file-flag)
538 (let ((start (point)))
539 (apply 'stgit-run-git
540 (append '("ls-files" "--exclude-standard" "-z") args))
542 (while (looking-at "\\([^\0]*\\)\0")
543 (let ((name-len (- (match-end 0) (match-beginning 0))))
544 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
545 (forward-char name-len)))))
547 (defun stgit-insert-patch-files (patch)
548 "Expand (show modification of) the patch PATCH after the line
550 (let* ((patchsym (stgit-patch-name patch))
552 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
553 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
554 (set-marker-insertion-type end t)
555 (setf (stgit-patch-files-ewoc patch) ewoc)
557 (apply 'stgit-run-git
558 (cond ((eq patchsym :work)
559 `("diff-files" "-0" ,@args))
560 ((eq patchsym :index)
561 `("diff-index" ,@args "--cached" "HEAD"))
563 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
565 (when (and (eq patchsym :work))
566 (when stgit-show-ignored
567 (stgit-insert-ls-files '("--ignored" "--others") "I"))
568 (when stgit-show-unknown
569 (stgit-insert-ls-files '("--others") "X"))
570 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
571 (point-min) (point-max)))
573 (goto-char (point-min))
574 (unless (or (eobp) (memq patchsym '(:work :index)))
576 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
577 (let ((old-perm (string-to-number (match-string 1) 8))
578 (new-perm (string-to-number (match-string 2) 8)))
579 (goto-char (match-end 0))
582 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
583 (let* ((patch-status (stgit-patch-status patch))
584 (file-subexp (if (eq patch-status 'unapplied)
587 (file (match-string file-subexp)))
592 :cr-score (string-to-number (match-string 2))
593 :cr-from (match-string 3)
594 :cr-to (match-string 4)
595 :status (stgit-file-status-code
598 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
606 :status (stgit-file-status-code
608 :file (match-string 2))))))
609 (goto-char (match-end 0))
610 (ewoc-enter-last ewoc file))))
612 (unless (ewoc-nth ewoc 0)
615 (propertize "<no files>"
616 'face 'stgit-description-face)
620 (defun stgit-find-file (&optional other-window)
621 (let* ((file (or (stgit-patched-file-at-point)
622 (error "No file at point")))
623 (filename (expand-file-name (stgit-file-file file))))
624 (unless (file-exists-p filename)
625 (error "File does not exist"))
626 (funcall (if other-window 'find-file-other-window 'find-file)
628 (when (eq (stgit-file-status file) 'unmerged)
631 (defun stgit-select-patch ()
632 (let ((patchname (stgit-patch-name-at-point)))
633 (if (memq patchname stgit-expanded-patches)
634 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
635 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
636 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
637 (move-to-column (stgit-goal-column)))
639 (defun stgit-select ()
640 "With point on a patch, toggle showing files in the patch.
642 With point on a file, open the associated file. Opens the target
643 file for (applied) copies and renames."
645 (case (get-text-property (point) 'entry-type)
647 (stgit-select-patch))
651 (error "No patch or file on line"))))
653 (defun stgit-find-file-other-window ()
654 "Open file at point in other window"
658 (defun stgit-find-file-merge ()
659 "Open file at point and merge it using `smerge-ediff'."
665 "Hide the stgit buffer."
669 (defun stgit-git-status ()
670 "Show status using `git-status'."
672 (unless (fboundp 'git-status)
673 (error "The stgit-git-status command requires git-status"))
674 (let ((dir default-directory))
675 (save-selected-window
679 (defun stgit-goal-column ()
680 "Return goal column for the current line"
681 (case (get-text-property (point) 'entry-type)
686 (defun stgit-next-line (&optional arg)
687 "Move cursor vertically down ARG lines"
690 (move-to-column (stgit-goal-column)))
692 (defun stgit-previous-line (&optional arg)
693 "Move cursor vertically up ARG lines"
696 (move-to-column (stgit-goal-column)))
698 (defun stgit-next-patch (&optional arg)
699 "Move cursor down ARG patches."
701 (ewoc-goto-next stgit-ewoc (or arg 1))
702 (move-to-column goal-column))
704 (defun stgit-previous-patch (&optional arg)
705 "Move cursor up ARG patches."
707 (ewoc-goto-prev stgit-ewoc (or arg 1))
708 (move-to-column goal-column))
710 (defvar stgit-mode-hook nil
711 "Run after `stgit-mode' is setup.")
713 (defvar stgit-mode-map nil
714 "Keymap for StGit major mode.")
716 (unless stgit-mode-map
717 (let ((diff-map (make-keymap))
718 (toggle-map (make-keymap)))
719 (suppress-keymap diff-map)
720 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
721 '(("b" . stgit-diff-base)
722 ("c" . stgit-diff-combined)
723 ("m" . stgit-find-file-merge)
724 ("o" . stgit-diff-ours)
725 ("t" . stgit-diff-theirs)))
726 (suppress-keymap toggle-map)
727 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
728 '(("t" . stgit-toggle-worktree)
729 ("i" . stgit-toggle-ignored)
730 ("u" . stgit-toggle-unknown)))
731 (setq stgit-mode-map (make-keymap))
732 (suppress-keymap stgit-mode-map)
733 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
736 ("\d" . stgit-unmark-up)
737 ("u" . stgit-unmark-down)
740 ("\C-p" . stgit-previous-line)
741 ("\C-n" . stgit-next-line)
742 ([up] . stgit-previous-line)
743 ([down] . stgit-next-line)
744 ("p" . stgit-previous-patch)
745 ("n" . stgit-next-patch)
746 ("\M-{" . stgit-previous-patch)
747 ("\M-}" . stgit-next-patch)
748 ("s" . stgit-git-status)
749 ("g" . stgit-reload-or-repair)
750 ("r" . stgit-refresh)
751 ("\C-c\C-r" . stgit-rename)
753 ("M" . stgit-move-patches)
756 ("\C-c\C-c" . stgit-commit)
757 ("\C-c\C-u" . stgit-uncommit)
758 ("U" . stgit-revert-file)
759 ("R" . stgit-resolve-file)
760 ("\r" . stgit-select)
761 ("o" . stgit-find-file-other-window)
762 ("i" . stgit-file-toggle-index)
763 (">" . stgit-push-next)
764 ("<" . stgit-pop-next)
765 ("P" . stgit-push-or-pop)
769 ([?\C-/] . stgit-undo)
770 ("\C-_" . stgit-undo)
771 ([?\C-c ?\C-/] . stgit-redo)
772 ("\C-c\C-_" . stgit-redo)
774 ("\C-c\C-b" . stgit-rebase)
777 ("q" . stgit-quit)))))
780 "Major mode for interacting with StGit.
782 Start StGit using \\[stgit].
786 \\[stgit-help] Show this help text
787 \\[stgit-quit] Hide the StGit buffer
788 \\[describe-bindings] Show all key bindings
790 \\[stgit-reload-or-repair] Reload the StGit buffer
791 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
793 \\[stgit-undo] Undo most recent StGit operation
794 \\[stgit-redo] Undo recent undo
796 \\[stgit-git-status] Run `git-status' (if available)
799 \\[stgit-previous-line] Move to previous line
800 \\[stgit-next-line] Move to next line
801 \\[stgit-previous-patch] Move to previous patch
802 \\[stgit-next-patch] Move to next patch
804 \\[stgit-mark] Mark patch
805 \\[stgit-unmark-up] Unmark patch and move up
806 \\[stgit-unmark-down] Unmark patch and move down
808 Commands for patches:
809 \\[stgit-select] Toggle showing changed files in patch
810 \\[stgit-refresh] Refresh patch with changes in index or work tree
811 \\[stgit-diff] Show the patch log and diff
813 \\[stgit-new] Create a new, empty patch
814 \\[stgit-rename] Rename patch
815 \\[stgit-edit] Edit patch description
816 \\[stgit-delete] Delete patch(es)
818 \\[stgit-push-next] Push next patch onto stack
819 \\[stgit-pop-next] Pop current patch from stack
820 \\[stgit-push-or-pop] Push or pop patch at point
821 \\[stgit-goto] Make current patch current by popping or pushing
823 \\[stgit-squash] Squash (meld together) patches
824 \\[stgit-move-patches] Move patch(es) to point
826 \\[stgit-commit] Commit patch(es)
827 \\[stgit-uncommit] Uncommit patch(es)
830 \\[stgit-select] Open the file in this window
831 \\[stgit-find-file-other-window] Open the file in another window
832 \\[stgit-diff] Show the file's diff
834 \\[stgit-file-toggle-index] Toggle change between index and work tree
835 \\[stgit-revert-file] Revert changes to file
838 \\[stgit-toggle-worktree] Toggle showing index and work tree
839 \\[stgit-toggle-unknown] Toggle showing unknown files
840 \\[stgit-toggle-ignored] Toggle showing ignored files
843 \\[stgit-diff] Show diff of patch or file
844 \\[stgit-diff-base] Show diff against the merge base
845 \\[stgit-diff-ours] Show diff against our branch
846 \\[stgit-diff-theirs] Show diff against their branch
848 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
849 ignore space changes.
850 With two prefix arguments (e.g., \\[universal-argument] \
851 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
853 Commands for merge conflicts:
854 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
855 \\[stgit-resolve-file] Mark unmerged file as resolved
857 Commands for branches:
858 \\[stgit-branch] Switch to another branch
859 \\[stgit-rebase] Rebase the current branch
861 Customization variables:
862 `stgit-abbreviate-copies-and-renames'
863 `stgit-default-show-worktree'
864 `stgit-find-copies-harder'
865 `stgit-show-worktree-mode'
867 See also \\[customize-group] for the \"stgit\" group."
868 (kill-all-local-variables)
869 (buffer-disable-undo)
870 (setq mode-name "StGit"
871 major-mode 'stgit-mode
873 (use-local-map stgit-mode-map)
874 (set (make-local-variable 'list-buffers-directory) default-directory)
875 (set (make-local-variable 'stgit-marked-patches) nil)
876 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
877 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
878 (set (make-local-variable 'stgit-index-node) nil)
879 (set (make-local-variable 'stgit-worktree-node) nil)
880 (set (make-local-variable 'parse-sexp-lookup-properties) t)
881 (set-variable 'truncate-lines 't)
882 (add-hook 'after-save-hook 'stgit-update-saved-file)
883 (run-hooks 'stgit-mode-hook))
885 (defun stgit-update-saved-file ()
886 (let* ((file (expand-file-name buffer-file-name))
887 (dir (file-name-directory file))
888 (gitdir (condition-case nil (git-get-top-dir dir)
890 (buffer (and gitdir (stgit-find-buffer gitdir))))
892 (with-current-buffer buffer
893 (stgit-refresh-worktree)))))
895 (defun stgit-add-mark (patchsym)
896 "Mark the patch PATCHSYM."
897 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
899 (defun stgit-remove-mark (patchsym)
900 "Unmark the patch PATCHSYM."
901 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
903 (defun stgit-clear-marks ()
904 "Unmark all patches."
905 (setq stgit-marked-patches '()))
907 (defun stgit-patch-at-point (&optional cause-error)
908 (get-text-property (point) 'patch-data))
910 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
911 "Return the patch name on the current line as a symbol.
912 If CAUSE-ERROR is not nil, signal an error if none found.
913 If ONLY-PATCHES is not nil, only allow real patches, and not
915 (let ((patch (stgit-patch-at-point)))
918 (memq (stgit-patch-status patch) '(work index))
921 (stgit-patch-name patch))
923 (error "No patch on this line")))))
925 (defun stgit-patched-file-at-point ()
926 (get-text-property (point) 'file-data))
928 (defun stgit-patches-marked-or-at-point ()
929 "Return the symbols of the marked patches, or the patch on the current line."
930 (if stgit-marked-patches
932 (let ((patch (stgit-patch-name-at-point)))
937 (defun stgit-goto-patch (patchsym &optional file)
938 "Move point to the line containing patch PATCHSYM.
939 If that patch cannot be found, do nothing.
941 If the patch was found and FILE is not nil, instead move to that
942 file's line. If FILE cannot be found, stay on the line of
944 (let ((node (ewoc-nth stgit-ewoc 0)))
945 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
947 (setq node (ewoc-next stgit-ewoc node)))
948 (when (and node file)
949 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
950 (file-node (ewoc-nth file-ewoc 0)))
951 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
952 (setq file-node (ewoc-next file-ewoc file-node)))
954 (ewoc-goto-node file-ewoc file-node)
955 (move-to-column (stgit-goal-column))
958 (ewoc-goto-node stgit-ewoc node)
959 (move-to-column goal-column))))
964 (stgit-capture-output nil
969 "Mark the patch under point."
971 (let* ((node (ewoc-locate stgit-ewoc))
972 (patch (ewoc-data node))
973 (name (stgit-patch-name patch)))
974 (when (eq name :work)
975 (error "Cannot mark the work tree"))
976 (when (eq name :index)
977 (error "Cannot mark the index"))
978 (stgit-add-mark (stgit-patch-name patch))
979 (ewoc-invalidate stgit-ewoc node))
982 (defun stgit-unmark-up ()
983 "Remove mark from the patch on the previous line."
985 (stgit-previous-patch)
986 (let* ((node (ewoc-locate stgit-ewoc))
987 (patch (ewoc-data node)))
988 (stgit-remove-mark (stgit-patch-name patch))
989 (ewoc-invalidate stgit-ewoc node))
990 (move-to-column (stgit-goal-column)))
992 (defun stgit-unmark-down ()
993 "Remove mark from the patch on the current line."
995 (let* ((node (ewoc-locate stgit-ewoc))
996 (patch (ewoc-data node)))
997 (stgit-remove-mark (stgit-patch-name patch))
998 (ewoc-invalidate stgit-ewoc node))
1001 (defun stgit-rename (name)
1002 "Rename the patch under point to NAME."
1004 (read-string "Patch name: "
1005 (symbol-name (stgit-patch-name-at-point t t)))))
1006 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1007 (stgit-capture-output nil
1008 (stgit-run "rename" old-patchsym name))
1009 (let ((name-sym (intern name)))
1010 (when (memq old-patchsym stgit-expanded-patches)
1011 (setq stgit-expanded-patches
1012 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1013 (when (memq old-patchsym stgit-marked-patches)
1014 (setq stgit-marked-patches
1015 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1017 (stgit-goto-patch name-sym))))
1019 (defun stgit-reload-or-repair (repair)
1020 "Update the contents of the StGit buffer (`stgit-reload').
1022 With a prefix argument, repair the StGit metadata if the branch
1023 was modified with git commands (`stgit-repair')."
1029 (defun stgit-repair ()
1032 (stgit-capture-output nil
1033 (stgit-run "repair"))
1036 (defun stgit-available-branches ()
1037 "Returns a list of the available stg branches"
1038 (let ((output (with-output-to-string
1039 (stgit-run "branch" "--list")))
1042 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1043 (setq result (cons (match-string 1 output) result))
1044 (setq start (match-end 0)))
1047 (defun stgit-branch (branch)
1048 "Switch to branch BRANCH."
1049 (interactive (list (completing-read "Switch to branch: "
1050 (stgit-available-branches))))
1051 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1054 (defun stgit-available-refs (&optional omit-stgit)
1055 "Returns a list of the available git refs.
1056 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1057 (let* ((output (with-output-to-string
1058 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1059 "refs/tags" "refs/heads"
1061 (result (split-string output "\n" t)))
1063 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1064 (substring s (match-end 0))
1067 (delete-if (lambda (s)
1068 (string-match "^refs/heads/.*\\.stgit$" s))
1072 (defun stgit-rebase (new-base)
1073 "Rebase to NEW-BASE."
1074 (interactive (list (completing-read "Rebase to: "
1075 (stgit-available-refs t))))
1076 (stgit-capture-output nil (stgit-run "rebase" new-base))
1079 (defun stgit-commit (count)
1080 "Run stg commit on COUNT commits.
1081 Interactively, the prefix argument is used as COUNT.
1082 A negative COUNT will uncommit instead."
1085 (stgit-uncommit (- count))
1086 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1089 (defun stgit-uncommit (count)
1090 "Run stg uncommit on COUNT commits.
1091 Interactively, the prefix argument is used as COUNT.
1092 A negative COUNT will commit instead."
1095 (stgit-commit (- count))
1096 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1099 (defun stgit-neighbour-file ()
1100 "Return the file name of the next file after point, or the
1101 previous file if point is at the last file within a patch."
1102 (let ((old-point (point))
1104 (and (zerop (forward-line 1))
1105 (let ((f (stgit-patched-file-at-point)))
1106 (and f (setq neighbour-file (stgit-file-file f)))))
1107 (goto-char old-point)
1108 (unless neighbour-file
1109 (and (zerop (forward-line -1))
1110 (let ((f (stgit-patched-file-at-point)))
1111 (and f (setq neighbour-file (stgit-file-file f)))))
1112 (goto-char old-point))
1115 (defun stgit-revert-file ()
1116 "Revert the file at point, which must be in the index or the
1119 (let* ((patched-file (or (stgit-patched-file-at-point)
1120 (error "No file on the current line")))
1121 (patch-name (stgit-patch-name-at-point))
1122 (file-status (stgit-file-status patched-file))
1123 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1124 (stgit-file-cr-to patched-file))
1125 ((eq file-status 'add)
1126 (stgit-file-file patched-file))))
1127 (co-file (cond ((eq file-status 'rename)
1128 (stgit-file-cr-from patched-file))
1129 ((not (memq file-status '(copy add)))
1130 (stgit-file-file patched-file))))
1131 (next-file (stgit-neighbour-file)))
1133 (unless (memq patch-name '(:work :index))
1134 (error "No index or working tree file on this line"))
1136 (when (eq file-status 'ignore)
1137 (error "Cannot revert ignored files"))
1139 (when (eq file-status 'unknown)
1140 (error "Cannot revert unknown files"))
1142 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1143 (when (yes-or-no-p (format "Revert %d file%s? "
1145 (if (= nfiles 1) "" "s")))
1146 (stgit-capture-output nil
1148 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1150 (stgit-run-git "checkout" "HEAD" co-file)))
1152 (stgit-goto-patch patch-name next-file)))))
1154 (defun stgit-resolve-file ()
1155 "Resolve conflict in the file at point."
1157 (let* ((patched-file (stgit-patched-file-at-point))
1158 (patch (stgit-patch-at-point))
1159 (patch-name (and patch (stgit-patch-name patch)))
1160 (status (and patched-file (stgit-file-status patched-file))))
1162 (unless (memq patch-name '(:work :index))
1163 (error "No index or working tree file on this line"))
1165 (unless (eq status 'unmerged)
1166 (error "No conflict to resolve at the current line"))
1168 (stgit-capture-output nil
1169 (stgit-move-change-to-index (stgit-file-file patched-file)))
1173 (defun stgit-push-next (npatches)
1174 "Push the first unapplied patch.
1175 With numeric prefix argument, push that many patches."
1177 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1179 (stgit-refresh-git-status))
1181 (defun stgit-pop-next (npatches)
1182 "Pop the topmost applied patch.
1183 With numeric prefix argument, pop that many patches."
1185 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1187 (stgit-refresh-git-status))
1189 (defun stgit-applied-at-point ()
1190 "Is the patch on the current line applied?"
1193 (looking-at "[>+]")))
1195 (defun stgit-push-or-pop ()
1196 "Push or pop the patch on the current line."
1198 (let ((patchsym (stgit-patch-name-at-point t))
1199 (applied (stgit-applied-at-point)))
1200 (stgit-capture-output nil
1201 (stgit-run (if applied "pop" "push") patchsym))
1204 (defun stgit-goto ()
1205 "Go to the patch on the current line."
1207 (let ((patchsym (stgit-patch-name-at-point t)))
1208 (stgit-capture-output nil
1209 (stgit-run "goto" patchsym))
1212 (defun stgit-id (patchsym)
1213 "Return the git commit id for PATCHSYM.
1214 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1215 (if (keywordp patchsym)
1217 (let ((result (with-output-to-string
1218 (stgit-run-silent "id" patchsym))))
1219 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1220 (error "Cannot find commit id for %s" patchsym))
1221 (match-string 1 result))))
1223 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1224 "Show the patch on the current line.
1226 UNMERGED-STAGE is the argument to `git-diff' that that selects
1227 which stage to diff against in the case of unmerged files."
1228 (let ((space-arg (when (numberp ignore-whitespace)
1229 (cond ((> ignore-whitespace 4)
1230 "--ignore-all-space")
1231 ((> ignore-whitespace 1)
1232 "--ignore-space-change"))))
1233 (patch-name (stgit-patch-name-at-point t)))
1234 (stgit-capture-output "*StGit patch*"
1235 (case (get-text-property (point) 'entry-type)
1237 (let* ((patched-file (stgit-patched-file-at-point))
1238 (patch-id (let ((id (stgit-id patch-name)))
1239 (if (and (eq id :index)
1240 (eq (stgit-file-status patched-file)
1244 (args (append (and space-arg (list space-arg))
1245 (and (stgit-file-cr-from patched-file)
1246 (list (stgit-find-copies-harder-diff-arg)))
1247 (cond ((eq patch-id :index)
1249 ((eq patch-id :work)
1250 (list unmerged-stage))
1252 (list (concat patch-id "^") patch-id)))
1254 (if (stgit-file-copy-or-rename patched-file)
1255 (list (stgit-file-cr-from patched-file)
1256 (stgit-file-cr-to patched-file))
1257 (list (stgit-file-file patched-file))))))
1258 (apply 'stgit-run-git "diff" args)))
1260 (let* ((patch-id (stgit-id patch-name)))
1261 (if (or (eq patch-id :index) (eq patch-id :work))
1262 (apply 'stgit-run-git "diff"
1263 (stgit-find-copies-harder-diff-arg)
1264 (append (and space-arg (list space-arg))
1265 (if (eq patch-id :index)
1267 (list unmerged-stage))))
1268 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1269 (and space-arg (list "-O" space-arg))
1270 (list (stgit-patch-name-at-point)))))
1271 (apply 'stgit-run args)))))
1273 (error "No patch or file at point")))
1274 (with-current-buffer standard-output
1275 (goto-char (point-min))
1278 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1279 `(defun ,name (&optional ignore-whitespace)
1280 ,(format "Show the patch on the current line.
1282 %sWith a prefix argument, ignore whitespace. With a prefix argument
1283 greater than four (e.g., \\[universal-argument] \
1284 \\[universal-argument] \\[%s]), ignore all whitespace."
1286 (format "For unmerged files, %s.\n\n" unmerged-action)
1290 (stgit-show-patch ,diff-arg ignore-whitespace)))
1292 (stgit-define-diff stgit-diff
1294 (stgit-define-diff stgit-diff-ours
1296 "diff against our branch")
1297 (stgit-define-diff stgit-diff-theirs
1299 "diff against their branch")
1300 (stgit-define-diff stgit-diff-base
1302 "diff against the merge base")
1303 (stgit-define-diff stgit-diff-combined
1305 "show a combined diff")
1307 (defun stgit-move-change-to-index (file &optional force)
1308 "Copies the work tree state of FILE to index, using git add or git rm.
1310 If FORCE is not nil, use --force."
1311 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1312 '("add") '("rm" "-q"))))
1313 (stgit-capture-output "*git output*"
1314 (apply 'stgit-run-git (append op (and force '("--force"))
1315 '("--") (list file))))))
1317 (defun stgit-remove-change-from-index (file)
1318 "Unstages the change in FILE from the index"
1319 (stgit-capture-output "*git output*"
1320 (stgit-run-git "reset" "-q" "--" file)))
1322 (defun stgit-file-toggle-index ()
1323 "Move modified file in or out of the index.
1325 Leaves the point where it is, but moves the mark to where the
1326 file ended up. You can then jump to the file with \
1327 \\[exchange-point-and-mark]."
1329 (let* ((patched-file (or (stgit-patched-file-at-point)
1330 (error "No file on the current line")))
1331 (patched-status (stgit-file-status patched-file)))
1332 (when (eq patched-status 'unmerged)
1333 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1334 (let* ((patch (stgit-patch-at-point))
1335 (patch-name (stgit-patch-name patch))
1336 (mark-file (if (eq patched-status 'rename)
1337 (stgit-file-cr-to patched-file)
1338 (stgit-file-file patched-file)))
1339 (point-file (if (eq patched-status 'rename)
1340 (stgit-file-cr-from patched-file)
1341 (stgit-neighbour-file))))
1343 (cond ((eq patch-name :work)
1344 (stgit-move-change-to-index (stgit-file-file patched-file)
1345 (eq patched-status 'ignore)))
1346 ((eq patch-name :index)
1347 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1349 (error "Can only move files between working tree and index")))
1350 (stgit-refresh-worktree)
1351 (stgit-refresh-index)
1352 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1354 (stgit-goto-patch patch-name point-file))))
1356 (defun stgit-edit ()
1357 "Edit the patch on the current line."
1359 (let ((patchsym (stgit-patch-name-at-point t t))
1360 (edit-buf (get-buffer-create "*StGit edit*"))
1361 (dir default-directory))
1362 (log-edit 'stgit-confirm-edit t nil edit-buf)
1363 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1364 (setq default-directory dir)
1365 (let ((standard-output edit-buf))
1366 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1368 (defun stgit-confirm-edit ()
1370 (let ((file (make-temp-file "stgit-edit-")))
1371 (write-region (point-min) (point-max) file)
1372 (stgit-capture-output nil
1373 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1374 (with-current-buffer log-edit-parent-buffer
1377 (defun stgit-new (add-sign)
1378 "Create a new patch.
1379 With a prefix argument, include a \"Signed-off-by:\" line at the
1382 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1383 (dir default-directory))
1384 (log-edit 'stgit-confirm-new t nil edit-buf)
1385 (setq default-directory dir)
1388 (let ((standard-output (current-buffer)))
1389 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1391 (defun stgit-confirm-new ()
1393 (let ((file (make-temp-file "stgit-edit-")))
1394 (write-region (point-min) (point-max) file)
1395 (stgit-capture-output nil
1396 (stgit-run "new" "-f" file))
1397 (with-current-buffer log-edit-parent-buffer
1400 (defun stgit-create-patch-name (description)
1401 "Create a patch name from a long description"
1403 (while (> (length description) 0)
1404 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1405 (setq patch (downcase (concat patch
1406 (match-string 0 description))))
1407 (setq description (substring description (match-end 0))))
1408 ((string-match "\\` +" description)
1409 (setq patch (concat patch "-"))
1410 (setq description (substring description (match-end 0))))
1411 ((string-match "\\`[^a-zA-Z_-]+" description)
1412 (setq description (substring description (match-end 0))))))
1413 (cond ((= (length patch) 0)
1415 ((> (length patch) 20)
1416 (substring patch 0 20))
1419 (defun stgit-delete (patchsyms &optional spill-p)
1420 "Delete the patches in PATCHSYMS.
1421 Interactively, delete the marked patches, or the patch at point.
1423 With a prefix argument, or SPILL-P, spill the patch contents to
1424 the work tree and index."
1425 (interactive (list (stgit-patches-marked-or-at-point)
1426 current-prefix-arg))
1428 (error "No patches to delete"))
1429 (when (memq :index patchsyms)
1430 (error "Cannot delete the index"))
1431 (when (memq :work patchsyms)
1432 (error "Cannot delete the work tree"))
1434 (let ((npatches (length patchsyms)))
1435 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1437 (if (= 1 npatches) "" "es")
1439 " (spilling contents to index)"
1441 (let ((args (if spill-p
1442 (cons "--spill" patchsyms)
1444 (stgit-capture-output nil
1445 (apply 'stgit-run "delete" args))
1448 (defun stgit-move-patches-target ()
1449 "Return the patchsym indicating a target patch for
1450 `stgit-move-patches'.
1452 This is either the patch at point, or one of :top and :bottom, if
1453 the point is after or before the applied patches."
1455 (let ((patchsym (stgit-patch-name-at-point)))
1456 (cond (patchsym patchsym)
1457 ((save-excursion (re-search-backward "^>" nil t)) :top)
1460 (defun stgit-sort-patches (patchsyms)
1461 "Returns the list of patches in PATCHSYMS sorted according to
1462 their position in the patch series, bottommost first.
1464 PATCHSYMS must not contain duplicate entries."
1465 (let (sorted-patchsyms
1466 (series (with-output-to-string
1467 (with-current-buffer standard-output
1468 (stgit-run-silent "series" "--noprefix"))))
1470 (while (string-match "^\\(.+\\)" series start)
1471 (let ((patchsym (intern (match-string 1 series))))
1472 (when (memq patchsym patchsyms)
1473 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1474 (setq start (match-end 0)))
1475 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1477 (unless (= (length patchsyms) (length sorted-patchsyms))
1478 (error "Internal error"))
1482 (defun stgit-move-patches (patchsyms target-patch)
1483 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1484 If TARGET-PATCH is :bottom or :top, move the patches to the
1485 bottom or top of the stack, respectively.
1487 Interactively, move the marked patches to where the point is."
1488 (interactive (list stgit-marked-patches
1489 (stgit-move-patches-target)))
1491 (error "Need at least one patch to move"))
1493 (unless target-patch
1494 (error "Point not at a patch"))
1496 (if (eq target-patch :top)
1497 (stgit-capture-output nil
1498 (apply 'stgit-run "float" patchsyms))
1500 ;; need to have patchsyms sorted by position in the stack
1501 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1502 (while sorted-patchsyms
1503 (setq sorted-patchsyms
1504 (and (stgit-capture-output nil
1505 (if (eq target-patch :bottom)
1506 (stgit-run "sink" "--" (car sorted-patchsyms))
1507 (stgit-run "sink" "--to" target-patch "--"
1508 (car sorted-patchsyms))))
1509 (cdr sorted-patchsyms))))))
1512 (defun stgit-squash (patchsyms)
1513 "Squash the patches in PATCHSYMS.
1514 Interactively, squash the marked patches.
1516 Unless there are any conflicts, the patches will be merged into
1517 one patch, which will occupy the same spot in the series as the
1518 deepest patch had before the squash."
1519 (interactive (list stgit-marked-patches))
1520 (when (< (length patchsyms) 2)
1521 (error "Need at least two patches to squash"))
1522 (let ((stgit-buffer (current-buffer))
1523 (edit-buf (get-buffer-create "*StGit edit*"))
1524 (dir default-directory)
1525 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1526 (log-edit 'stgit-confirm-squash t nil edit-buf)
1527 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1528 (setq default-directory dir)
1529 (let ((result (let ((standard-output edit-buf))
1530 (apply 'stgit-run-silent "squash"
1531 "--save-template=-" sorted-patchsyms))))
1533 ;; stg squash may have reordered the patches or caused conflicts
1534 (with-current-buffer stgit-buffer
1537 (unless (eq 0 result)
1539 (rename-buffer "*StGit error*")
1540 (resize-temp-buffer-window)
1541 (switch-to-buffer-other-window stgit-buffer)
1542 (error "stg squash failed")))))
1544 (defun stgit-confirm-squash ()
1546 (let ((file (make-temp-file "stgit-edit-")))
1547 (write-region (point-min) (point-max) file)
1548 (stgit-capture-output nil
1549 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1550 (with-current-buffer log-edit-parent-buffer
1552 ;; Go to first marked patch and stay there
1553 (goto-char (point-min))
1554 (re-search-forward (concat "^[>+-]\\*") nil t)
1555 (move-to-column goal-column)
1556 (let ((pos (point)))
1560 (defun stgit-help ()
1561 "Display help for the StGit mode."
1563 (describe-function 'stgit-mode))
1565 (defun stgit-undo (&optional arg)
1567 With prefix argument, run it with the --hard flag.
1569 See also `stgit-redo'."
1571 (stgit-capture-output nil
1573 (stgit-run "undo" "--hard")
1574 (stgit-run "undo")))
1577 (defun stgit-redo (&optional arg)
1579 With prefix argument, run it with the --hard flag.
1581 See also `stgit-undo'."
1583 (stgit-capture-output nil
1585 (stgit-run "redo" "--hard")
1586 (stgit-run "redo")))
1589 (defun stgit-refresh (&optional arg)
1591 If the index contains any changes, only refresh from index.
1593 With prefix argument, refresh the marked patch or the patch under point."
1595 (let ((patchargs (if arg
1596 (let ((patches (stgit-patches-marked-or-at-point)))
1597 (cond ((null patches)
1598 (error "No patch to update"))
1599 ((> (length patches) 1)
1600 (error "Too many patches selected"))
1602 (cons "-p" patches))))
1604 (unless (stgit-index-empty-p)
1605 (setq patchargs (cons "--index" patchargs)))
1606 (stgit-capture-output nil
1607 (apply 'stgit-run "refresh" patchargs))
1608 (stgit-refresh-git-status))
1611 (defcustom stgit-show-worktree-mode 'center
1612 "This variable controls where the \"Index\" and \"Work tree\"
1613 will be shown on in the buffer.
1615 It can be set to 'top (above all patches), 'center (show between
1616 applied and unapplied patches), and 'bottom (below all patches).
1618 See also `stgit-show-worktree'."
1619 :type '(radio (const :tag "above all patches (top)" top)
1620 (const :tag "between applied and unapplied patches (center)"
1622 (const :tag "below all patches (bottom)" bottom))
1625 (defcustom stgit-default-show-worktree
1627 "Set to non-nil to by default show the working tree in a new stgit buffer.
1629 This value is used as the default value for `stgit-show-worktree'."
1633 (defvar stgit-show-worktree nil
1634 "If nil, inhibit showing work tree and index in the stgit buffer.
1636 See also `stgit-show-worktree-mode'.")
1638 (defvar stgit-show-ignored nil
1639 "If nil, inhibit showing files ignored by git.")
1641 (defvar stgit-show-unknown nil
1642 "If nil, inhibit showing files not registered with git.")
1644 (defun stgit-toggle-worktree (&optional arg)
1645 "Toggle the visibility of the work tree.
1646 With ARG, show the work tree if ARG is positive.
1648 Its initial setting is controlled by `stgit-default-show-worktree'.
1650 `stgit-show-worktree-mode' controls where on screen the index and
1651 work tree will show up."
1653 (setq stgit-show-worktree
1656 (not stgit-show-worktree)))
1659 (defun stgit-toggle-ignored (&optional arg)
1660 "Toggle the visibility of files ignored by git in the work
1661 tree. With ARG, show these files if ARG is positive.
1663 Use \\[stgit-toggle-worktree] to show the work tree."
1665 (setq stgit-show-ignored
1668 (not stgit-show-ignored)))
1671 (defun stgit-toggle-unknown (&optional arg)
1672 "Toggle the visibility of files not registered with git in the
1673 work tree. With ARG, show these files if ARG is positive.
1675 Use \\[stgit-toggle-worktree] to show the work tree."
1677 (setq stgit-show-unknown
1680 (not stgit-show-unknown)))
1683 (defcustom stgit-abbreviate-copies-and-renames
1685 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
1686 instead of \"dir/old/file -> dir/new/file\"."