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* ((standard-output (current-buffer))
205 (exit-status (stgit-run-silent "series"
206 "--description" "--empty")))
207 (goto-char (point-min))
208 (if (not (zerop exit-status))
209 (cond ((looking-at "stg series: \\(.*\\)")
210 (setq inserted-index t)
211 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
212 (substitute-command-keys
213 "-- not initialized; run \\[stgit-init]")))
215 (error "Error running stg: %s"
219 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
220 (error "Syntax error in output from stg series"))
221 (let* ((state-str (match-string 2))
222 (state (cond ((string= state-str ">") 'top)
223 ((string= state-str "+") 'applied)
224 ((string= state-str "-") 'unapplied)))
225 (name (intern (match-string 4)))
226 (desc (match-string 5))
227 (empty (string= (match-string 1) "0")))
228 (unless inserted-index
229 (when (or (eq stgit-show-worktree-mode 'top)
230 (and (eq stgit-show-worktree-mode 'center)
231 (eq state 'unapplied)))
232 (setq inserted-index t)
233 (stgit-run-series-insert-index ewoc)))
234 (setq all-patchsyms (cons name all-patchsyms))
235 (ewoc-enter-last ewoc
242 (unless inserted-index
243 (stgit-run-series-insert-index ewoc)))
244 (setq stgit-index-node index-node
245 stgit-worktree-node worktree-node
246 stgit-marked-patches (intersection stgit-marked-patches
249 (defun stgit-reload ()
250 "Update the contents of the StGit buffer."
252 (let ((inhibit-read-only t)
253 (curline (line-number-at-pos))
254 (curpatch (stgit-patch-name-at-point))
255 (curfile (stgit-patched-file-at-point)))
256 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
257 (ewoc-set-hf stgit-ewoc
260 (substring (with-output-to-string
261 (stgit-run-silent "branch"))
263 'face 'stgit-branch-name-face)
265 (if stgit-show-worktree
268 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
269 shows the working tree\n")
270 'face 'stgit-description-face)))
271 (stgit-run-series stgit-ewoc)
273 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
274 (goto-line curline)))
275 (stgit-refresh-git-status))
277 (defun stgit-set-default (symbol value)
278 "Set default value of SYMBOL to VALUE using `set-default' and
279 reload all StGit buffers."
280 (set-default symbol value)
281 (dolist (buf (buffer-list))
282 (with-current-buffer buf
283 (when (eq major-mode 'stgit-mode)
287 "A user interface for the StGit patch maintenance tool."
289 :link '(function-link stgit)
290 :link '(url-link "http://www.procode.org/stgit/"))
292 (defcustom stgit-abbreviate-copies-and-renames t
293 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
294 instead of \"dir/old/file -> dir/new/file\"."
297 :set 'stgit-set-default)
299 (defcustom stgit-default-show-worktree t
300 "Set to non-nil to by default show the working tree in a new stgit buffer.
302 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
305 :link '(variable-link stgit-show-worktree))
307 (defcustom stgit-find-copies-harder nil
308 "Try harder to find copied files when listing patches.
310 When not nil, runs git diff-tree with the --find-copies-harder
311 flag, which reduces performance."
314 :set 'stgit-set-default)
316 (defcustom stgit-show-worktree-mode 'center
317 "This variable controls where the \"Index\" and \"Work tree\"
318 will be shown on in the buffer.
320 It can be set to 'top (above all patches), 'center (show between
321 applied and unapplied patches), and 'bottom (below all patches)."
322 :type '(radio (const :tag "above all patches (top)" top)
323 (const :tag "between applied and unapplied patches (center)"
325 (const :tag "below all patches (bottom)" bottom))
327 :link '(variable-link stgit-show-worktree)
328 :set 'stgit-set-default)
330 (defface stgit-branch-name-face
332 "The face used for the StGit branch name"
335 (defface stgit-top-patch-face
336 '((((background dark)) (:weight bold :foreground "yellow"))
337 (((background light)) (:weight bold :foreground "purple"))
339 "The face used for the top patch names"
342 (defface stgit-applied-patch-face
343 '((((background dark)) (:foreground "light yellow"))
344 (((background light)) (:foreground "purple"))
346 "The face used for applied patch names"
349 (defface stgit-unapplied-patch-face
350 '((((background dark)) (:foreground "gray80"))
351 (((background light)) (:foreground "orchid"))
353 "The face used for unapplied patch names"
356 (defface stgit-description-face
357 '((((background dark)) (:foreground "tan"))
358 (((background light)) (:foreground "dark red")))
359 "The face used for StGit descriptions"
362 (defface stgit-index-work-tree-title-face
363 '((((supports :slant italic)) :slant italic)
365 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
368 (defface stgit-unmerged-file-face
369 '((((class color) (background light)) (:foreground "red" :bold t))
370 (((class color) (background dark)) (:foreground "red" :bold t)))
371 "StGit mode face used for unmerged file status"
374 (defface stgit-unknown-file-face
375 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
376 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
377 "StGit mode face used for unknown file status"
380 (defface stgit-ignored-file-face
381 '((((class color) (background light)) (:foreground "grey60"))
382 (((class color) (background dark)) (:foreground "grey40")))
383 "StGit mode face used for ignored files")
385 (defface stgit-file-permission-face
386 '((((class color) (background light)) (:foreground "green" :bold t))
387 (((class color) (background dark)) (:foreground "green" :bold t)))
388 "StGit mode face used for permission changes."
391 (defface stgit-modified-file-face
392 '((((class color) (background light)) (:foreground "purple"))
393 (((class color) (background dark)) (:foreground "salmon")))
394 "StGit mode face used for modified file status"
397 (defconst stgit-file-status-code-strings
398 (mapcar (lambda (arg)
400 (propertize (cadr arg) 'face (car (cddr arg)))))
401 '((add "Added" stgit-modified-file-face)
402 (copy "Copied" stgit-modified-file-face)
403 (delete "Deleted" stgit-modified-file-face)
404 (modify "Modified" stgit-modified-file-face)
405 (rename "Renamed" stgit-modified-file-face)
406 (mode-change "Mode change" stgit-modified-file-face)
407 (unmerged "Unmerged" stgit-unmerged-file-face)
408 (unknown "Unknown" stgit-unknown-file-face)
409 (ignore "Ignored" stgit-ignored-file-face)))
410 "Alist of code symbols to description strings")
412 (defconst stgit-patch-status-face-alist
413 '((applied . stgit-applied-patch-face)
414 (top . stgit-top-patch-face)
415 (unapplied . stgit-unapplied-patch-face)
416 (index . stgit-index-work-tree-title-face)
417 (work . stgit-index-work-tree-title-face))
418 "Alist of face to use for a given patch status")
420 (defun stgit-file-status-code-as-string (file)
421 "Return stgit status code for FILE as a string"
422 (let* ((code (assq (stgit-file-status file)
423 stgit-file-status-code-strings))
424 (score (stgit-file-cr-score file)))
427 (if (and score (/= score 100))
428 (format "%s %s" (cdr code)
429 (propertize (format "%d%%" score)
430 'face 'stgit-description-face))
433 (defun stgit-file-status-code (str &optional score)
434 "Return stgit status code from git status string"
435 (let ((code (assoc str '(("A" . add)
444 (setq code (if code (cdr code) 'unknown))
445 (when (stringp score)
446 (if (> (length score) 0)
447 (setq score (string-to-number score))
449 (if score (cons code score) code)))
451 (defconst stgit-file-type-strings
454 (#o160 . "subproject"))
455 "Alist of names of file types")
457 (defun stgit-file-type-string (type)
458 "Return string describing file type TYPE (the high bits of file permission).
459 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
460 (let ((type-str (assoc type stgit-file-type-strings)))
461 (or (and type-str (cdr type-str))
462 (format "unknown type %o" type))))
464 (defun stgit-file-type-change-string (old-perm new-perm)
465 "Return string describing file type change from OLD-PERM to NEW-PERM.
466 Cf. `stgit-file-type-string'."
467 (let ((old-type (lsh old-perm -9))
468 (new-type (lsh new-perm -9)))
469 (cond ((= old-type new-type) "")
470 ((zerop new-type) "")
472 (if (= new-type #o100)
474 (format " (%s)" (stgit-file-type-string new-type))))
475 (t (format " (%s -> %s)"
476 (stgit-file-type-string old-type)
477 (stgit-file-type-string new-type))))))
479 (defun stgit-file-mode-change-string (old-perm new-perm)
480 "Return string describing file mode change from OLD-PERM to NEW-PERM.
481 Cf. `stgit-file-type-change-string'."
482 (setq old-perm (logand old-perm #o777)
483 new-perm (logand new-perm #o777))
484 (if (or (= old-perm new-perm)
488 (let* ((modified (logxor old-perm new-perm))
489 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
490 (cond ((zerop modified) "")
491 ((and (zerop not-x-modified)
492 (or (and (eq #o111 (logand old-perm #o111))
493 (propertize "-x" 'face 'stgit-file-permission-face))
494 (and (eq #o111 (logand new-perm #o111))
495 (propertize "+x" 'face
496 'stgit-file-permission-face)))))
497 (t (concat (propertize (format "%o" old-perm)
498 'face 'stgit-file-permission-face)
500 'face 'stgit-description-face)
501 (propertize (format "%o" new-perm)
502 'face 'stgit-file-permission-face)))))))
504 (defstruct (stgit-file)
505 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
507 (defun stgit-describe-copy-or-rename (file)
508 (let (from to common-head common-tail
510 (propertize "->" 'face 'stgit-description-face)
513 (when stgit-abbreviate-copies-and-renames
514 (setq from (split-string (stgit-file-cr-from file) "/")
515 to (split-string (stgit-file-cr-to file) "/"))
517 (while (and from to (cdr from) (cdr to)
518 (string-equal (car from) (car to)))
519 (setq common-head (cons (car from) common-head)
522 (setq common-head (nreverse common-head)
525 (while (and from to (cdr from) (cdr to)
526 (string-equal (car from) (car to)))
527 (setq common-tail (cons (car from) common-tail)
530 (setq from (nreverse from)
533 (if (or common-head common-tail)
534 (concat (if common-head
535 (mapconcat #'identity common-head "/")
537 (if common-head "/" "")
538 (propertize "{" 'face 'stgit-description-face)
539 (mapconcat #'identity from "/")
541 (mapconcat #'identity to "/")
542 (propertize "}" 'face 'stgit-description-face)
543 (if common-tail "/" "")
545 (mapconcat #'identity common-tail "/")
547 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
549 (defun stgit-file-pp (file)
550 (let ((status (stgit-file-status file))
551 (name (if (stgit-file-copy-or-rename file)
552 (stgit-describe-copy-or-rename file)
553 (stgit-file-file file)))
554 (mode-change (stgit-file-mode-change-string
555 (stgit-file-old-perm file)
556 (stgit-file-new-perm file)))
558 (insert (format " %-12s%s%s%s%s\n"
559 (stgit-file-status-code-as-string file)
561 (if (zerop (length mode-change)) "" " ")
563 (propertize (stgit-file-type-change-string
564 (stgit-file-old-perm file)
565 (stgit-file-new-perm file))
566 'face 'stgit-description-face)))
567 (add-text-properties start (point)
568 (list 'entry-type 'file
571 (defun stgit-find-copies-harder-diff-arg ()
572 "Return the flag to use with `git-diff' depending on the
573 `stgit-find-copies-harder' flag."
574 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
576 (defun stgit-insert-ls-files (args file-flag)
577 (let ((start (point)))
578 (apply 'stgit-run-git
579 (append '("ls-files" "--exclude-standard" "-z") args))
581 (while (looking-at "\\([^\0]*\\)\0")
582 (let ((name-len (- (match-end 0) (match-beginning 0))))
583 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
584 (forward-char name-len)))))
586 (defun stgit-insert-patch-files (patch)
587 "Expand (show modification of) the patch PATCH after the line
589 (let* ((patchsym (stgit-patch-name patch))
591 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
592 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
593 (set-marker-insertion-type end t)
594 (setf (stgit-patch-files-ewoc patch) ewoc)
596 (let ((standard-output (current-buffer)))
597 (apply 'stgit-run-git
598 (cond ((eq patchsym :work)
599 `("diff-files" "-0" ,@args))
600 ((eq patchsym :index)
601 `("diff-index" ,@args "--cached" "HEAD"))
603 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
605 (when (and (eq patchsym :work))
606 (when stgit-show-ignored
607 (stgit-insert-ls-files '("--ignored" "--others") "I"))
608 (when stgit-show-unknown
609 (stgit-insert-ls-files '("--others") "X"))
610 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
611 (point-min) (point-max)))
613 (goto-char (point-min))
614 (unless (or (eobp) (memq patchsym '(:work :index)))
616 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
617 (let ((old-perm (string-to-number (match-string 1) 8))
618 (new-perm (string-to-number (match-string 2) 8)))
619 (goto-char (match-end 0))
622 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
623 (let* ((patch-status (stgit-patch-status patch))
624 (file-subexp (if (eq patch-status 'unapplied)
627 (file (match-string file-subexp)))
632 :cr-score (string-to-number (match-string 2))
633 :cr-from (match-string 3)
634 :cr-to (match-string 4)
635 :status (stgit-file-status-code
638 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
646 :status (stgit-file-status-code
648 :file (match-string 2))))))
649 (goto-char (match-end 0))
650 (ewoc-enter-last ewoc file))))
652 (unless (ewoc-nth ewoc 0)
655 (propertize "<no files>"
656 'face 'stgit-description-face)
660 (defun stgit-find-file (&optional other-window)
661 (let* ((file (or (stgit-patched-file-at-point)
662 (error "No file at point")))
663 (filename (expand-file-name (stgit-file-file file))))
664 (unless (file-exists-p filename)
665 (error "File does not exist"))
666 (funcall (if other-window 'find-file-other-window 'find-file)
668 (when (eq (stgit-file-status file) 'unmerged)
671 (defun stgit-select-patch ()
672 (let ((patchname (stgit-patch-name-at-point)))
673 (if (memq patchname stgit-expanded-patches)
674 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
675 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
676 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
677 (move-to-column (stgit-goal-column)))
679 (defun stgit-select ()
680 "With point on a patch, toggle showing files in the patch.
682 With point on a file, open the associated file. Opens the target
683 file for (applied) copies and renames."
685 (case (get-text-property (point) 'entry-type)
687 (stgit-select-patch))
691 (error "No patch or file on line"))))
693 (defun stgit-find-file-other-window ()
694 "Open file at point in other window"
698 (defun stgit-find-file-merge ()
699 "Open file at point and merge it using `smerge-ediff'."
705 "Hide the stgit buffer."
709 (defun stgit-git-status ()
710 "Show status using `git-status'."
712 (unless (fboundp 'git-status)
713 (error "The stgit-git-status command requires git-status"))
714 (let ((dir default-directory))
715 (save-selected-window
719 (defun stgit-goal-column ()
720 "Return goal column for the current line"
721 (case (get-text-property (point) 'entry-type)
726 (defun stgit-next-line (&optional arg)
727 "Move cursor vertically down ARG lines"
730 (move-to-column (stgit-goal-column)))
732 (defun stgit-previous-line (&optional arg)
733 "Move cursor vertically up ARG lines"
736 (move-to-column (stgit-goal-column)))
738 (defun stgit-next-patch (&optional arg)
739 "Move cursor down ARG patches."
741 (ewoc-goto-next stgit-ewoc (or arg 1))
742 (move-to-column goal-column))
744 (defun stgit-previous-patch (&optional arg)
745 "Move cursor up ARG patches."
747 (ewoc-goto-prev stgit-ewoc (or arg 1))
748 (move-to-column goal-column))
750 (defvar stgit-mode-hook nil
751 "Run after `stgit-mode' is setup.")
753 (defvar stgit-mode-map nil
754 "Keymap for StGit major mode.")
756 (unless stgit-mode-map
757 (let ((diff-map (make-keymap))
758 (toggle-map (make-keymap)))
759 (suppress-keymap diff-map)
760 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
761 '(("b" . stgit-diff-base)
762 ("c" . stgit-diff-combined)
763 ("m" . stgit-find-file-merge)
764 ("o" . stgit-diff-ours)
765 ("t" . stgit-diff-theirs)))
766 (suppress-keymap toggle-map)
767 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
768 '(("t" . stgit-toggle-worktree)
769 ("i" . stgit-toggle-ignored)
770 ("u" . stgit-toggle-unknown)))
771 (setq stgit-mode-map (make-keymap))
772 (suppress-keymap stgit-mode-map)
773 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
776 ("\d" . stgit-unmark-up)
777 ("u" . stgit-unmark-down)
780 ("\C-p" . stgit-previous-line)
781 ("\C-n" . stgit-next-line)
782 ([up] . stgit-previous-line)
783 ([down] . stgit-next-line)
784 ("p" . stgit-previous-patch)
785 ("n" . stgit-next-patch)
786 ("\M-{" . stgit-previous-patch)
787 ("\M-}" . stgit-next-patch)
788 ("s" . stgit-git-status)
789 ("g" . stgit-reload-or-repair)
790 ("r" . stgit-refresh)
791 ("\C-c\C-r" . stgit-rename)
793 ("M" . stgit-move-patches)
796 ("c" . stgit-new-and-refresh)
797 ("\C-c\C-c" . stgit-commit)
798 ("\C-c\C-u" . stgit-uncommit)
799 ("U" . stgit-revert-file)
800 ("R" . stgit-resolve-file)
801 ("\r" . stgit-select)
802 ("o" . stgit-find-file-other-window)
803 ("i" . stgit-file-toggle-index)
804 (">" . stgit-push-next)
805 ("<" . stgit-pop-next)
806 ("P" . stgit-push-or-pop)
810 ([?\C-/] . stgit-undo)
811 ("\C-_" . stgit-undo)
812 ([?\C-c ?\C-/] . stgit-redo)
813 ("\C-c\C-_" . stgit-redo)
815 ("\C-c\C-b" . stgit-rebase)
818 ("q" . stgit-quit)))))
821 "Major mode for interacting with StGit.
823 Start StGit using \\[stgit].
827 \\[stgit-help] Show this help text
828 \\[stgit-quit] Hide the StGit buffer
829 \\[describe-bindings] Show all key bindings
831 \\[stgit-reload-or-repair] Reload the StGit buffer
832 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
834 \\[stgit-undo] Undo most recent StGit operation
835 \\[stgit-redo] Undo recent undo
837 \\[stgit-git-status] Run `git-status' (if available)
840 \\[stgit-previous-line] Move to previous line
841 \\[stgit-next-line] Move to next line
842 \\[stgit-previous-patch] Move to previous patch
843 \\[stgit-next-patch] Move to next patch
845 \\[stgit-mark] Mark patch
846 \\[stgit-unmark-up] Unmark patch and move up
847 \\[stgit-unmark-down] Unmark patch and move down
849 Commands for patches:
850 \\[stgit-select] Toggle showing changed files in patch
851 \\[stgit-refresh] Refresh patch with changes in index or work tree
852 \\[stgit-diff] Show the patch log and diff
854 \\[stgit-new] Create a new, empty patch
855 \\[stgit-new-and-refresh] Create a new patch from index or work tree
856 \\[stgit-rename] Rename patch
857 \\[stgit-edit] Edit patch description
858 \\[stgit-delete] Delete patch(es)
860 \\[stgit-push-next] Push next patch onto stack
861 \\[stgit-pop-next] Pop current patch from stack
862 \\[stgit-push-or-pop] Push or pop patch at point
863 \\[stgit-goto] Make current patch current by popping or pushing
865 \\[stgit-squash] Squash (meld together) patches
866 \\[stgit-move-patches] Move patch(es) to point
868 \\[stgit-commit] Commit patch(es)
869 \\[stgit-uncommit] Uncommit patch(es)
872 \\[stgit-select] Open the file in this window
873 \\[stgit-find-file-other-window] Open the file in another window
874 \\[stgit-diff] Show the file's diff
876 \\[stgit-file-toggle-index] Toggle change between index and work tree
877 \\[stgit-revert-file] Revert changes to file
880 \\[stgit-toggle-worktree] Toggle showing index and work tree
881 \\[stgit-toggle-unknown] Toggle showing unknown files
882 \\[stgit-toggle-ignored] Toggle showing ignored files
885 \\[stgit-diff] Show diff of patch or file
886 \\[stgit-diff-base] Show diff against the merge base
887 \\[stgit-diff-ours] Show diff against our branch
888 \\[stgit-diff-theirs] Show diff against their branch
890 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
891 ignore space changes.
892 With two prefix arguments (e.g., \\[universal-argument] \
893 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
895 Commands for merge conflicts:
896 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
897 \\[stgit-resolve-file] Mark unmerged file as resolved
899 Commands for branches:
900 \\[stgit-branch] Switch to another branch
901 \\[stgit-rebase] Rebase the current branch
903 Customization variables:
904 `stgit-abbreviate-copies-and-renames'
905 `stgit-default-show-worktree'
906 `stgit-find-copies-harder'
907 `stgit-show-worktree-mode'
909 See also \\[customize-group] for the \"stgit\" group."
910 (kill-all-local-variables)
911 (buffer-disable-undo)
912 (setq mode-name "StGit"
913 major-mode 'stgit-mode
915 (use-local-map stgit-mode-map)
916 (set (make-local-variable 'list-buffers-directory) default-directory)
917 (set (make-local-variable 'stgit-marked-patches) nil)
918 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
919 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
920 (set (make-local-variable 'stgit-index-node) nil)
921 (set (make-local-variable 'stgit-worktree-node) nil)
922 (set (make-local-variable 'parse-sexp-lookup-properties) t)
923 (set-variable 'truncate-lines 't)
924 (add-hook 'after-save-hook 'stgit-update-saved-file)
925 (run-hooks 'stgit-mode-hook))
927 (defun stgit-update-saved-file ()
928 (let* ((file (expand-file-name buffer-file-name))
929 (dir (file-name-directory file))
930 (gitdir (condition-case nil (git-get-top-dir dir)
932 (buffer (and gitdir (stgit-find-buffer gitdir))))
934 (with-current-buffer buffer
935 (stgit-refresh-worktree)))))
937 (defun stgit-add-mark (patchsym)
938 "Mark the patch PATCHSYM."
939 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
941 (defun stgit-remove-mark (patchsym)
942 "Unmark the patch PATCHSYM."
943 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
945 (defun stgit-clear-marks ()
946 "Unmark all patches."
947 (setq stgit-marked-patches '()))
949 (defun stgit-patch-at-point (&optional cause-error)
950 (get-text-property (point) 'patch-data))
952 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
953 "Return the patch name on the current line as a symbol.
954 If CAUSE-ERROR is not nil, signal an error if none found.
955 If ONLY-PATCHES is not nil, only allow real patches, and not
957 (let ((patch (stgit-patch-at-point)))
960 (memq (stgit-patch-status patch) '(work index))
963 (stgit-patch-name patch))
965 (error "No patch on this line")))))
967 (defun stgit-patched-file-at-point ()
968 (get-text-property (point) 'file-data))
970 (defun stgit-patches-marked-or-at-point ()
971 "Return the symbols of the marked patches, or the patch on the current line."
972 (if stgit-marked-patches
974 (let ((patch (stgit-patch-name-at-point)))
979 (defun stgit-goto-patch (patchsym &optional file)
980 "Move point to the line containing patch PATCHSYM.
981 If that patch cannot be found, do nothing.
983 If the patch was found and FILE is not nil, instead move to that
984 file's line. If FILE cannot be found, stay on the line of
986 (let ((node (ewoc-nth stgit-ewoc 0)))
987 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
989 (setq node (ewoc-next stgit-ewoc node)))
990 (when (and node file)
991 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
992 (file-node (ewoc-nth file-ewoc 0)))
993 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
994 (setq file-node (ewoc-next file-ewoc file-node)))
996 (ewoc-goto-node file-ewoc file-node)
997 (move-to-column (stgit-goal-column))
1000 (ewoc-goto-node stgit-ewoc node)
1001 (move-to-column goal-column))))
1003 (defun stgit-init ()
1006 (stgit-capture-output nil
1010 (defun stgit-mark ()
1011 "Mark the patch under point."
1013 (let* ((node (ewoc-locate stgit-ewoc))
1014 (patch (ewoc-data node))
1015 (name (stgit-patch-name patch)))
1016 (when (eq name :work)
1017 (error "Cannot mark the work tree"))
1018 (when (eq name :index)
1019 (error "Cannot mark the index"))
1020 (stgit-add-mark (stgit-patch-name patch))
1021 (ewoc-invalidate stgit-ewoc node))
1024 (defun stgit-unmark-up ()
1025 "Remove mark from the patch on the previous line."
1027 (stgit-previous-patch)
1028 (let* ((node (ewoc-locate stgit-ewoc))
1029 (patch (ewoc-data node)))
1030 (stgit-remove-mark (stgit-patch-name patch))
1031 (ewoc-invalidate stgit-ewoc node))
1032 (move-to-column (stgit-goal-column)))
1034 (defun stgit-unmark-down ()
1035 "Remove mark from the patch on the current line."
1037 (let* ((node (ewoc-locate stgit-ewoc))
1038 (patch (ewoc-data node)))
1039 (stgit-remove-mark (stgit-patch-name patch))
1040 (ewoc-invalidate stgit-ewoc node))
1043 (defun stgit-rename (name)
1044 "Rename the patch under point to NAME."
1046 (read-string "Patch name: "
1047 (symbol-name (stgit-patch-name-at-point t t)))))
1048 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1049 (stgit-capture-output nil
1050 (stgit-run "rename" old-patchsym name))
1051 (let ((name-sym (intern name)))
1052 (when (memq old-patchsym stgit-expanded-patches)
1053 (setq stgit-expanded-patches
1054 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1055 (when (memq old-patchsym stgit-marked-patches)
1056 (setq stgit-marked-patches
1057 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1059 (stgit-goto-patch name-sym))))
1061 (defun stgit-reload-or-repair (repair)
1062 "Update the contents of the StGit buffer (`stgit-reload').
1064 With a prefix argument, repair the StGit metadata if the branch
1065 was modified with git commands (`stgit-repair')."
1071 (defun stgit-repair ()
1074 (stgit-capture-output nil
1075 (stgit-run "repair"))
1078 (defun stgit-available-branches ()
1079 "Returns a list of the available stg branches"
1080 (let ((output (with-output-to-string
1081 (stgit-run "branch" "--list")))
1084 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1085 (setq result (cons (match-string 1 output) result))
1086 (setq start (match-end 0)))
1089 (defun stgit-branch (branch)
1090 "Switch to branch BRANCH."
1091 (interactive (list (completing-read "Switch to branch: "
1092 (stgit-available-branches))))
1093 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1096 (defun stgit-available-refs (&optional omit-stgit)
1097 "Returns a list of the available git refs.
1098 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1099 (let* ((output (with-output-to-string
1100 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1101 "refs/tags" "refs/heads"
1103 (result (split-string output "\n" t)))
1105 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1106 (substring s (match-end 0))
1109 (delete-if (lambda (s)
1110 (string-match "^refs/heads/.*\\.stgit$" s))
1114 (defun stgit-rebase (new-base)
1115 "Rebase to NEW-BASE."
1116 (interactive (list (completing-read "Rebase to: "
1117 (stgit-available-refs t))))
1118 (stgit-capture-output nil (stgit-run "rebase" new-base))
1121 (defun stgit-commit (count)
1122 "Run stg commit on COUNT commits.
1123 Interactively, the prefix argument is used as COUNT.
1124 A negative COUNT will uncommit instead."
1127 (stgit-uncommit (- count))
1128 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1131 (defun stgit-uncommit (count)
1132 "Run stg uncommit on COUNT commits.
1133 Interactively, the prefix argument is used as COUNT.
1134 A negative COUNT will commit instead."
1137 (stgit-commit (- count))
1138 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1141 (defun stgit-neighbour-file ()
1142 "Return the file name of the next file after point, or the
1143 previous file if point is at the last file within a patch."
1144 (let ((old-point (point))
1146 (and (zerop (forward-line 1))
1147 (let ((f (stgit-patched-file-at-point)))
1148 (and f (setq neighbour-file (stgit-file-file f)))))
1149 (goto-char old-point)
1150 (unless neighbour-file
1151 (and (zerop (forward-line -1))
1152 (let ((f (stgit-patched-file-at-point)))
1153 (and f (setq neighbour-file (stgit-file-file f)))))
1154 (goto-char old-point))
1157 (defun stgit-revert-file ()
1158 "Revert the file at point, which must be in the index or the
1161 (let* ((patched-file (or (stgit-patched-file-at-point)
1162 (error "No file on the current line")))
1163 (patch-name (stgit-patch-name-at-point))
1164 (file-status (stgit-file-status patched-file))
1165 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1166 (stgit-file-cr-to patched-file))
1167 ((eq file-status 'add)
1168 (stgit-file-file patched-file))))
1169 (co-file (cond ((eq file-status 'rename)
1170 (stgit-file-cr-from patched-file))
1171 ((not (memq file-status '(copy add)))
1172 (stgit-file-file patched-file))))
1173 (next-file (stgit-neighbour-file)))
1175 (unless (memq patch-name '(:work :index))
1176 (error "No index or working tree file on this line"))
1178 (when (eq file-status 'ignore)
1179 (error "Cannot revert ignored files"))
1181 (when (eq file-status 'unknown)
1182 (error "Cannot revert unknown files"))
1184 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1185 (when (yes-or-no-p (format "Revert %d file%s? "
1187 (if (= nfiles 1) "" "s")))
1188 (stgit-capture-output nil
1190 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1192 (stgit-run-git "checkout" "HEAD" co-file)))
1194 (stgit-goto-patch patch-name next-file)))))
1196 (defun stgit-resolve-file ()
1197 "Resolve conflict in the file at point."
1199 (let* ((patched-file (stgit-patched-file-at-point))
1200 (patch (stgit-patch-at-point))
1201 (patch-name (and patch (stgit-patch-name patch)))
1202 (status (and patched-file (stgit-file-status patched-file))))
1204 (unless (memq patch-name '(:work :index))
1205 (error "No index or working tree file on this line"))
1207 (unless (eq status 'unmerged)
1208 (error "No conflict to resolve at the current line"))
1210 (stgit-capture-output nil
1211 (stgit-move-change-to-index (stgit-file-file patched-file)))
1215 (defun stgit-push-next (npatches)
1216 "Push the first unapplied patch.
1217 With numeric prefix argument, push that many patches."
1219 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1221 (stgit-refresh-git-status))
1223 (defun stgit-pop-next (npatches)
1224 "Pop the topmost applied patch.
1225 With numeric prefix argument, pop that many patches."
1227 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1229 (stgit-refresh-git-status))
1231 (defun stgit-applied-at-point-p ()
1232 "Return non-nil if the patch at point is applied."
1233 (let ((patch (stgit-patch-at-point t)))
1234 (not (eq (stgit-patch-status patch) 'unapplied))))
1236 (defun stgit-push-or-pop ()
1237 "Push or pop the patch on the current line."
1239 (let ((patchsym (stgit-patch-name-at-point t t))
1240 (applied (stgit-applied-at-point-p)))
1241 (stgit-capture-output nil
1242 (stgit-run (if applied "pop" "push") patchsym))
1245 (defun stgit-goto ()
1246 "Go to the patch on the current line."
1248 (let ((patchsym (stgit-patch-name-at-point t)))
1249 (stgit-capture-output nil
1250 (stgit-run "goto" patchsym))
1253 (defun stgit-id (patchsym)
1254 "Return the git commit id for PATCHSYM.
1255 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1256 (if (keywordp patchsym)
1258 (let ((result (with-output-to-string
1259 (stgit-run-silent "id" patchsym))))
1260 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1261 (error "Cannot find commit id for %s" patchsym))
1262 (match-string 1 result))))
1264 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1265 "Show the patch on the current line.
1267 UNMERGED-STAGE is the argument to `git-diff' that that selects
1268 which stage to diff against in the case of unmerged files."
1269 (let ((space-arg (when (numberp ignore-whitespace)
1270 (cond ((> ignore-whitespace 4)
1271 "--ignore-all-space")
1272 ((> ignore-whitespace 1)
1273 "--ignore-space-change"))))
1274 (patch-name (stgit-patch-name-at-point t)))
1275 (stgit-capture-output "*StGit patch*"
1276 (case (get-text-property (point) 'entry-type)
1278 (let* ((patched-file (stgit-patched-file-at-point))
1279 (patch-id (let ((id (stgit-id patch-name)))
1280 (if (and (eq id :index)
1281 (eq (stgit-file-status patched-file)
1285 (args (append (and space-arg (list space-arg))
1286 (and (stgit-file-cr-from patched-file)
1287 (list (stgit-find-copies-harder-diff-arg)))
1288 (cond ((eq patch-id :index)
1290 ((eq patch-id :work)
1291 (list unmerged-stage))
1293 (list (concat patch-id "^") patch-id)))
1295 (if (stgit-file-copy-or-rename patched-file)
1296 (list (stgit-file-cr-from patched-file)
1297 (stgit-file-cr-to patched-file))
1298 (list (stgit-file-file patched-file))))))
1299 (apply 'stgit-run-git "diff" args)))
1301 (let* ((patch-id (stgit-id patch-name)))
1302 (if (or (eq patch-id :index) (eq patch-id :work))
1303 (apply 'stgit-run-git "diff"
1304 (stgit-find-copies-harder-diff-arg)
1305 (append (and space-arg (list space-arg))
1306 (if (eq patch-id :index)
1308 (list unmerged-stage))))
1309 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1310 (and space-arg (list "-O" space-arg))
1311 (list (stgit-patch-name-at-point)))))
1312 (apply 'stgit-run args)))))
1314 (error "No patch or file at point")))
1315 (with-current-buffer standard-output
1316 (goto-char (point-min))
1319 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1320 `(defun ,name (&optional ignore-whitespace)
1321 ,(format "Show the patch on the current line.
1323 %sWith a prefix argument, ignore whitespace. With a prefix argument
1324 greater than four (e.g., \\[universal-argument] \
1325 \\[universal-argument] \\[%s]), ignore all whitespace."
1327 (format "For unmerged files, %s.\n\n" unmerged-action)
1331 (stgit-show-patch ,diff-arg ignore-whitespace)))
1333 (stgit-define-diff stgit-diff
1335 (stgit-define-diff stgit-diff-ours
1337 "diff against our branch")
1338 (stgit-define-diff stgit-diff-theirs
1340 "diff against their branch")
1341 (stgit-define-diff stgit-diff-base
1343 "diff against the merge base")
1344 (stgit-define-diff stgit-diff-combined
1346 "show a combined diff")
1348 (defun stgit-move-change-to-index (file &optional force)
1349 "Copies the work tree state of FILE to index, using git add or git rm.
1351 If FORCE is not nil, use --force."
1352 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1353 '("add") '("rm" "-q"))))
1354 (stgit-capture-output "*git output*"
1355 (apply 'stgit-run-git (append op (and force '("--force"))
1356 '("--") (list file))))))
1358 (defun stgit-remove-change-from-index (file)
1359 "Unstages the change in FILE from the index"
1360 (stgit-capture-output "*git output*"
1361 (stgit-run-git "reset" "-q" "--" file)))
1363 (defun stgit-file-toggle-index ()
1364 "Move modified file in or out of the index.
1366 Leaves the point where it is, but moves the mark to where the
1367 file ended up. You can then jump to the file with \
1368 \\[exchange-point-and-mark]."
1370 (let* ((patched-file (or (stgit-patched-file-at-point)
1371 (error "No file on the current line")))
1372 (patched-status (stgit-file-status patched-file)))
1373 (when (eq patched-status 'unmerged)
1374 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1375 (let* ((patch (stgit-patch-at-point))
1376 (patch-name (stgit-patch-name patch))
1377 (mark-file (if (eq patched-status 'rename)
1378 (stgit-file-cr-to patched-file)
1379 (stgit-file-file patched-file)))
1380 (point-file (if (eq patched-status 'rename)
1381 (stgit-file-cr-from patched-file)
1382 (stgit-neighbour-file))))
1384 (cond ((eq patch-name :work)
1385 (stgit-move-change-to-index (stgit-file-file patched-file)
1386 (eq patched-status 'ignore)))
1387 ((eq patch-name :index)
1388 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1390 (error "Can only move files between working tree and index")))
1391 (stgit-refresh-worktree)
1392 (stgit-refresh-index)
1393 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1395 (stgit-goto-patch patch-name point-file))))
1397 (defun stgit-edit ()
1398 "Edit the patch on the current line."
1400 (let ((patchsym (stgit-patch-name-at-point t t))
1401 (edit-buf (get-buffer-create "*StGit edit*"))
1402 (dir default-directory))
1403 (log-edit 'stgit-confirm-edit t nil edit-buf)
1404 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1405 (setq default-directory dir)
1406 (let ((standard-output edit-buf))
1407 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1409 (defun stgit-confirm-edit ()
1411 (let ((file (make-temp-file "stgit-edit-")))
1412 (write-region (point-min) (point-max) file)
1413 (stgit-capture-output nil
1414 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1415 (with-current-buffer log-edit-parent-buffer
1418 (defun stgit-new (add-sign &optional refresh)
1419 "Create a new patch.
1420 With a prefix argument, include a \"Signed-off-by:\" line at the
1423 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1424 (dir default-directory))
1425 (log-edit 'stgit-confirm-new t nil edit-buf)
1426 (setq default-directory dir)
1427 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1430 (let ((standard-output (current-buffer)))
1431 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1433 (defun stgit-confirm-new ()
1435 (let ((file (make-temp-file "stgit-edit-"))
1436 (refresh stgit-refresh-after-new))
1437 (write-region (point-min) (point-max) file)
1438 (stgit-capture-output nil
1439 (stgit-run "new" "-f" file))
1440 (with-current-buffer log-edit-parent-buffer
1445 (defun stgit-new-and-refresh (add-sign)
1446 "Create a new patch and refresh it with the current changes.
1448 With a prefix argument, include a \"Signed-off-by:\" line at the
1451 This works just like running `stgit-new' followed by `stgit-refresh'."
1453 (stgit-new add-sign t))
1455 (defun stgit-create-patch-name (description)
1456 "Create a patch name from a long description"
1458 (while (> (length description) 0)
1459 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1460 (setq patch (downcase (concat patch
1461 (match-string 0 description))))
1462 (setq description (substring description (match-end 0))))
1463 ((string-match "\\` +" description)
1464 (setq patch (concat patch "-"))
1465 (setq description (substring description (match-end 0))))
1466 ((string-match "\\`[^a-zA-Z_-]+" description)
1467 (setq description (substring description (match-end 0))))))
1468 (cond ((= (length patch) 0)
1470 ((> (length patch) 20)
1471 (substring patch 0 20))
1474 (defun stgit-delete (patchsyms &optional spill-p)
1475 "Delete the patches in PATCHSYMS.
1476 Interactively, delete the marked patches, or the patch at point.
1478 With a prefix argument, or SPILL-P, spill the patch contents to
1479 the work tree and index."
1480 (interactive (list (stgit-patches-marked-or-at-point)
1481 current-prefix-arg))
1483 (error "No patches to delete"))
1484 (when (memq :index patchsyms)
1485 (error "Cannot delete the index"))
1486 (when (memq :work patchsyms)
1487 (error "Cannot delete the work tree"))
1489 (let ((npatches (length patchsyms)))
1490 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1492 (if (= 1 npatches) "" "es")
1494 " (spilling contents to index)"
1496 (let ((args (if spill-p
1497 (cons "--spill" patchsyms)
1499 (stgit-capture-output nil
1500 (apply 'stgit-run "delete" args))
1503 (defun stgit-move-patches-target ()
1504 "Return the patchsym indicating a target patch for
1505 `stgit-move-patches'.
1507 This is either the patch at point, or one of :top and :bottom, if
1508 the point is after or before the applied patches."
1510 (let ((patchsym (stgit-patch-name-at-point nil t)))
1511 (cond (patchsym patchsym)
1512 ((save-excursion (re-search-backward "^>" nil t)) :top)
1515 (defun stgit-sort-patches (patchsyms)
1516 "Returns the list of patches in PATCHSYMS sorted according to
1517 their position in the patch series, bottommost first.
1519 PATCHSYMS must not contain duplicate entries."
1520 (let (sorted-patchsyms
1521 (series (with-output-to-string
1522 (with-current-buffer standard-output
1523 (stgit-run-silent "series" "--noprefix"))))
1525 (while (string-match "^\\(.+\\)" series start)
1526 (let ((patchsym (intern (match-string 1 series))))
1527 (when (memq patchsym patchsyms)
1528 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1529 (setq start (match-end 0)))
1530 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1532 (unless (= (length patchsyms) (length sorted-patchsyms))
1533 (error "Internal error"))
1537 (defun stgit-move-patches (patchsyms target-patch)
1538 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1539 If TARGET-PATCH is :bottom or :top, move the patches to the
1540 bottom or top of the stack, respectively.
1542 Interactively, move the marked patches to where the point is."
1543 (interactive (list stgit-marked-patches
1544 (stgit-move-patches-target)))
1546 (error "Need at least one patch to move"))
1548 (unless target-patch
1549 (error "Point not at a patch"))
1551 (if (eq target-patch :top)
1552 (stgit-capture-output nil
1553 (apply 'stgit-run "float" patchsyms))
1555 ;; need to have patchsyms sorted by position in the stack
1556 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1557 (while sorted-patchsyms
1558 (setq sorted-patchsyms
1559 (and (stgit-capture-output nil
1560 (if (eq target-patch :bottom)
1561 (stgit-run "sink" "--" (car sorted-patchsyms))
1562 (stgit-run "sink" "--to" target-patch "--"
1563 (car sorted-patchsyms))))
1564 (cdr sorted-patchsyms))))))
1567 (defun stgit-squash (patchsyms)
1568 "Squash the patches in PATCHSYMS.
1569 Interactively, squash the marked patches.
1571 Unless there are any conflicts, the patches will be merged into
1572 one patch, which will occupy the same spot in the series as the
1573 deepest patch had before the squash."
1574 (interactive (list stgit-marked-patches))
1575 (when (< (length patchsyms) 2)
1576 (error "Need at least two patches to squash"))
1577 (let ((stgit-buffer (current-buffer))
1578 (edit-buf (get-buffer-create "*StGit edit*"))
1579 (dir default-directory)
1580 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1581 (log-edit 'stgit-confirm-squash t nil edit-buf)
1582 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1583 (setq default-directory dir)
1584 (let ((result (let ((standard-output edit-buf))
1585 (apply 'stgit-run-silent "squash"
1586 "--save-template=-" sorted-patchsyms))))
1588 ;; stg squash may have reordered the patches or caused conflicts
1589 (with-current-buffer stgit-buffer
1592 (unless (eq 0 result)
1594 (rename-buffer "*StGit error*")
1595 (resize-temp-buffer-window)
1596 (switch-to-buffer-other-window stgit-buffer)
1597 (error "stg squash failed")))))
1599 (defun stgit-confirm-squash ()
1601 (let ((file (make-temp-file "stgit-edit-")))
1602 (write-region (point-min) (point-max) file)
1603 (stgit-capture-output nil
1604 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1605 (with-current-buffer log-edit-parent-buffer
1607 ;; Go to first marked patch and stay there
1608 (goto-char (point-min))
1609 (re-search-forward (concat "^[>+-]\\*") nil t)
1610 (move-to-column goal-column)
1611 (let ((pos (point)))
1615 (defun stgit-help ()
1616 "Display help for the StGit mode."
1618 (describe-function 'stgit-mode))
1620 (defun stgit-undo (&optional arg)
1622 With prefix argument, run it with the --hard flag.
1624 See also `stgit-redo'."
1626 (stgit-capture-output nil
1628 (stgit-run "undo" "--hard")
1629 (stgit-run "undo")))
1632 (defun stgit-redo (&optional arg)
1634 With prefix argument, run it with the --hard flag.
1636 See also `stgit-undo'."
1638 (stgit-capture-output nil
1640 (stgit-run "redo" "--hard")
1641 (stgit-run "redo")))
1644 (defun stgit-refresh (&optional arg)
1646 If the index contains any changes, only refresh from index.
1648 With prefix argument, refresh the marked patch or the patch under point."
1650 (let ((patchargs (if arg
1651 (let ((patches (stgit-patches-marked-or-at-point)))
1652 (cond ((null patches)
1653 (error "No patch to update"))
1654 ((> (length patches) 1)
1655 (error "Too many patches selected"))
1657 (cons "-p" patches))))
1659 (unless (stgit-index-empty-p)
1660 (setq patchargs (cons "--index" patchargs)))
1661 (stgit-capture-output nil
1662 (apply 'stgit-run "refresh" patchargs))
1663 (stgit-refresh-git-status))
1666 (defvar stgit-show-worktree nil
1667 "If nil, inhibit showing work tree and index in the stgit buffer.
1669 See also `stgit-show-worktree-mode'.")
1671 (defvar stgit-show-ignored nil
1672 "If nil, inhibit showing files ignored by git.")
1674 (defvar stgit-show-unknown nil
1675 "If nil, inhibit showing files not registered with git.")
1677 (defun stgit-toggle-worktree (&optional arg)
1678 "Toggle the visibility of the work tree.
1679 With ARG, show the work tree if ARG is positive.
1681 Its initial setting is controlled by `stgit-default-show-worktree'.
1683 `stgit-show-worktree-mode' controls where on screen the index and
1684 work tree will show up."
1686 (setq stgit-show-worktree
1689 (not stgit-show-worktree)))
1692 (defun stgit-toggle-ignored (&optional arg)
1693 "Toggle the visibility of files ignored by git in the work
1694 tree. With ARG, show these files if ARG is positive.
1696 Use \\[stgit-toggle-worktree] to show the work tree."
1698 (setq stgit-show-ignored
1701 (not stgit-show-ignored)))
1704 (defun stgit-toggle-unknown (&optional arg)
1705 "Toggle the visibility of files not registered with git in the
1706 work tree. With ARG, show these files if ARG is positive.
1708 Use \\[stgit-toggle-worktree] to show the work tree."
1710 (setq stgit-show-unknown
1713 (not stgit-show-unknown)))