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'
17 "Manage StGit patches for the tree in DIR."
18 (interactive "DDirectory: \n")
19 (switch-to-stgit-buffer (git-get-top-dir dir))
22 (unless (fboundp 'git-get-top-dir)
23 (defun git-get-top-dir (dir)
24 "Retrieve the top-level directory of a git tree."
25 (let ((cdup (with-output-to-string
26 (with-current-buffer standard-output
28 (unless (eq 0 (call-process "git" nil t nil
29 "rev-parse" "--show-cdup"))
30 (error "Cannot find top-level git tree for %s" dir))))))
31 (expand-file-name (concat (file-name-as-directory dir)
32 (car (split-string cdup "\n")))))))
34 (defun stgit-refresh-git-status (&optional dir)
35 "If it exists, refresh the `git-status' buffer belonging to
36 directory DIR or `default-directory'"
37 (when (and (fboundp 'git-find-status-buffer)
38 (fboundp 'git-refresh-status))
39 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
40 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
41 (when git-status-buffer
42 (with-current-buffer git-status-buffer
43 (git-refresh-status))))))
45 (defun stgit-find-buffer (dir)
46 "Return the buffer displaying StGit patches for DIR, or nil if none."
47 (setq dir (file-name-as-directory dir))
48 (let ((buffers (buffer-list)))
50 (not (with-current-buffer (car buffers)
51 (and (eq major-mode 'stgit-mode)
52 (string= default-directory dir)))))
53 (setq buffers (cdr buffers)))
54 (and buffers (car buffers))))
56 (defun switch-to-stgit-buffer (dir)
57 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
58 (setq dir (file-name-as-directory dir))
59 (let ((buffer (stgit-find-buffer dir)))
60 (switch-to-buffer (or buffer
61 (create-stgit-buffer dir)))))
63 (defstruct (stgit-patch)
64 status name desc empty files-ewoc)
66 (defun stgit-patch-pp (patch)
67 (let ((status (stgit-patch-status patch))
69 (name (stgit-patch-name patch)))
71 (:index (insert (propertize " Index" 'face 'italic)))
72 (:work (insert (propertize " Work tree" 'face 'italic)))
73 (t (insert (case status
77 (if (memq name stgit-marked-patches)
79 (propertize (format "%-30s"
82 ('applied 'stgit-applied-patch-face)
83 ('top 'stgit-top-patch-face)
84 ('unapplied 'stgit-unapplied-patch-face)
88 (if (stgit-patch-empty patch) "(empty) " "")
89 (propertize (or (stgit-patch-desc patch) "")
90 'face 'stgit-description-face))))
91 (put-text-property start (point) 'entry-type 'patch)
92 (when (memq name stgit-expanded-patches)
93 (stgit-insert-patch-files patch))
94 (put-text-property start (point) 'patch-data patch)))
96 (defun create-stgit-buffer (dir)
97 "Create a buffer for showing StGit patches.
98 Argument DIR is the repository path."
99 (let ((buf (create-file-buffer (concat dir "*stgit*")))
100 (inhibit-read-only t))
101 (with-current-buffer buf
102 (setq default-directory dir)
104 (set (make-local-variable 'stgit-ewoc)
105 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
106 (setq buffer-read-only t))
109 (defmacro stgit-capture-output (name &rest body)
110 "Capture StGit output and, if there was any output, show it in a window
112 Returns nil if there was no output."
113 (declare (debug ([&or stringp null] body))
115 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
116 (stgit-dir default-directory)
117 (inhibit-read-only t))
118 (with-current-buffer output-buf
120 (setq default-directory stgit-dir)
121 (setq buffer-read-only t))
122 (let ((standard-output output-buf))
124 (with-current-buffer output-buf
125 (set-buffer-modified-p nil)
126 (setq buffer-read-only t)
127 (if (< (point-min) (point-max))
128 (display-buffer output-buf t)))))
130 (defun stgit-make-run-args (args)
131 "Return a copy of ARGS with its elements converted to strings."
133 ;; don't use (format "%s" ...) to limit type errors
134 (cond ((stringp x) x)
135 ((integerp x) (number-to-string x))
136 ((symbolp x) (symbol-name x))
138 (error "Bad element in stgit-make-run-args args: %S" x))))
141 (defun stgit-run-silent (&rest args)
142 (setq args (stgit-make-run-args args))
143 (apply 'call-process "stg" nil standard-output nil args))
145 (defun stgit-run (&rest args)
146 (setq args (stgit-make-run-args args))
147 (let ((msgcmd (mapconcat #'identity args " ")))
148 (message "Running stg %s..." msgcmd)
149 (apply 'call-process "stg" nil standard-output nil args)
150 (message "Running stg %s...done" msgcmd)))
152 (defun stgit-run-git (&rest args)
153 (setq args (stgit-make-run-args args))
154 (let ((msgcmd (mapconcat #'identity args " ")))
155 (message "Running git %s..." msgcmd)
156 (apply 'call-process "git" nil standard-output nil args)
157 (message "Running git %s...done" msgcmd)))
159 (defun stgit-run-git-silent (&rest args)
160 (setq args (stgit-make-run-args args))
161 (apply 'call-process "git" nil standard-output nil args))
163 (defun stgit-index-empty-p ()
164 "Returns non-nil if the index contains no changes from HEAD."
165 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
167 (defvar stgit-index-node nil)
168 (defvar stgit-worktree-node nil)
170 (defun stgit-refresh-index ()
171 (when stgit-index-node
172 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
174 (defun stgit-refresh-worktree ()
175 (when stgit-worktree-node
176 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
178 (defun stgit-run-series (ewoc)
179 (let ((first-line t))
181 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
182 (goto-char (point-min))
183 (if (not (zerop exit-status))
184 (cond ((looking-at "stg series: \\(.*\\)")
185 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
186 "-- not initialized (run M-x stgit-init)"))
188 (error "Error running stg: %s"
192 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
193 (error "Syntax error in output from stg series"))
194 (let* ((state-str (match-string 2))
195 (state (cond ((string= state-str ">") 'top)
196 ((string= state-str "+") 'applied)
197 ((string= state-str "-") 'unapplied))))
198 (ewoc-enter-last ewoc
201 :name (intern (match-string 4))
202 :desc (match-string 5)
203 :empty (string= (match-string 1) "0"))))
204 (setq first-line nil)
206 (if stgit-show-worktree
207 (setq stgit-index-node (cons ewoc (ewoc-enter-last ewoc
213 stgit-worktree-node (cons ewoc (ewoc-enter-last ewoc
219 (setq stgit-worktree-node nil))))
222 (defun stgit-reload ()
223 "Update the contents of the StGit buffer."
225 (let ((inhibit-read-only t)
226 (curline (line-number-at-pos))
227 (curpatch (stgit-patch-name-at-point)))
228 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
229 (ewoc-set-hf stgit-ewoc
233 (stgit-run-silent "branch")
234 (buffer-substring (point-min) (1- (point-max))))
237 (if stgit-show-worktree
240 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
241 shows the working tree\n")
242 'face 'stgit-description-face)))
243 (stgit-run-series stgit-ewoc)
245 (stgit-goto-patch curpatch)
246 (goto-line curline)))
247 (stgit-refresh-git-status))
250 "A user interface for the StGit patch maintenance tool."
253 (defface stgit-description-face
254 '((((background dark)) (:foreground "tan"))
255 (((background light)) (:foreground "dark red")))
256 "The face used for StGit descriptions"
259 (defface stgit-top-patch-face
260 '((((background dark)) (:weight bold :foreground "yellow"))
261 (((background light)) (:weight bold :foreground "purple"))
263 "The face used for the top patch names"
266 (defface stgit-applied-patch-face
267 '((((background dark)) (:foreground "light yellow"))
268 (((background light)) (:foreground "purple"))
270 "The face used for applied patch names"
273 (defface stgit-unapplied-patch-face
274 '((((background dark)) (:foreground "gray80"))
275 (((background light)) (:foreground "orchid"))
277 "The face used for unapplied patch names"
280 (defface stgit-modified-file-face
281 '((((class color) (background light)) (:foreground "purple"))
282 (((class color) (background dark)) (:foreground "salmon")))
283 "StGit mode face used for modified file status"
286 (defface stgit-unmerged-file-face
287 '((((class color) (background light)) (:foreground "red" :bold t))
288 (((class color) (background dark)) (:foreground "red" :bold t)))
289 "StGit mode face used for unmerged file status"
292 (defface stgit-unknown-file-face
293 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
294 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
295 "StGit mode face used for unknown file status"
298 (defface stgit-file-permission-face
299 '((((class color) (background light)) (:foreground "green" :bold t))
300 (((class color) (background dark)) (:foreground "green" :bold t)))
301 "StGit mode face used for permission changes."
304 (defcustom stgit-expand-find-copies-harder
306 "Try harder to find copied files when listing patches.
308 When not nil, runs git diff-tree with the --find-copies-harder
309 flag, which reduces performance."
313 (defconst stgit-file-status-code-strings
314 (mapcar (lambda (arg)
316 (propertize (cadr arg) 'face (car (cddr arg)))))
317 '((add "Added" stgit-modified-file-face)
318 (copy "Copied" stgit-modified-file-face)
319 (delete "Deleted" stgit-modified-file-face)
320 (modify "Modified" stgit-modified-file-face)
321 (rename "Renamed" stgit-modified-file-face)
322 (mode-change "Mode change" stgit-modified-file-face)
323 (unmerged "Unmerged" stgit-unmerged-file-face)
324 (unknown "Unknown" stgit-unknown-file-face)))
325 "Alist of code symbols to description strings")
327 (defun stgit-file-status-code-as-string (file)
328 "Return stgit status code for FILE as a string"
329 (let* ((code (assq (stgit-file-status file)
330 stgit-file-status-code-strings))
331 (score (stgit-file-cr-score file)))
334 (if (and score (/= score 100))
335 (format "%s %s" (cdr code)
336 (propertize (format "%d%%" score)
337 'face 'stgit-description-face))
340 (defun stgit-file-status-code (str &optional score)
341 "Return stgit status code from git status string"
342 (let ((code (assoc str '(("A" . add)
350 (setq code (if code (cdr code) 'unknown))
351 (when (stringp score)
352 (if (> (length score) 0)
353 (setq score (string-to-number score))
355 (if score (cons code score) code)))
357 (defconst stgit-file-type-strings
360 (#o160 . "subproject"))
361 "Alist of names of file types")
363 (defun stgit-file-type-string (type)
364 "Return string describing file type TYPE (the high bits of file permission).
365 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
366 (let ((type-str (assoc type stgit-file-type-strings)))
367 (or (and type-str (cdr type-str))
368 (format "unknown type %o" type))))
370 (defun stgit-file-type-change-string (old-perm new-perm)
371 "Return string describing file type change from OLD-PERM to NEW-PERM.
372 Cf. `stgit-file-type-string'."
373 (let ((old-type (lsh old-perm -9))
374 (new-type (lsh new-perm -9)))
375 (cond ((= old-type new-type) "")
376 ((zerop new-type) "")
378 (if (= new-type #o100)
380 (format " (%s)" (stgit-file-type-string new-type))))
381 (t (format " (%s -> %s)"
382 (stgit-file-type-string old-type)
383 (stgit-file-type-string new-type))))))
385 (defun stgit-file-mode-change-string (old-perm new-perm)
386 "Return string describing file mode change from OLD-PERM to NEW-PERM.
387 Cf. `stgit-file-type-change-string'."
388 (setq old-perm (logand old-perm #o777)
389 new-perm (logand new-perm #o777))
390 (if (or (= old-perm new-perm)
394 (let* ((modified (logxor old-perm new-perm))
395 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
396 (cond ((zerop modified) "")
397 ((and (zerop not-x-modified)
398 (or (and (eq #o111 (logand old-perm #o111))
399 (propertize "-x" 'face 'stgit-file-permission-face))
400 (and (eq #o111 (logand new-perm #o111))
401 (propertize "+x" 'face
402 'stgit-file-permission-face)))))
403 (t (concat (propertize (format "%o" old-perm)
404 'face 'stgit-file-permission-face)
406 'face 'stgit-description-face)
407 (propertize (format "%o" new-perm)
408 'face 'stgit-file-permission-face)))))))
410 (defstruct (stgit-file)
411 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
413 (defun stgit-file-pp (file)
414 (let ((status (stgit-file-status file))
415 (name (if (stgit-file-copy-or-rename file)
416 (concat (stgit-file-cr-from file)
418 'face 'stgit-description-face)
419 (stgit-file-cr-to file))
420 (stgit-file-file file)))
421 (mode-change (stgit-file-mode-change-string
422 (stgit-file-old-perm file)
423 (stgit-file-new-perm file)))
425 (insert (format " %-12s%1s%s%s\n"
426 (stgit-file-status-code-as-string file)
429 (propertize (stgit-file-type-change-string
430 (stgit-file-old-perm file)
431 (stgit-file-new-perm file))
432 'face 'stgit-description-face)))
433 (add-text-properties start (point)
434 (list 'entry-type 'file
437 (defun stgit-insert-patch-files (patch)
438 "Expand (show modification of) the patch PATCH after the line
440 (let* ((patchsym (stgit-patch-name patch))
441 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
442 (args (list "-z" (if stgit-expand-find-copies-harder
443 "--find-copies-harder"
445 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
446 (setf (stgit-patch-files-ewoc patch) ewoc)
447 (when (eq patchsym :work)
448 (setq stgit-work-ewoc ewoc))
450 (apply 'stgit-run-git
451 (cond ((eq patchsym :work)
452 `("diff-files" ,@args))
453 ((eq patchsym :index)
454 `("diff-index" ,@args "--cached" "HEAD"))
456 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
457 (goto-char (point-min))
458 (unless (or (eobp) (memq patchsym '(:work :index)))
460 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
461 (let ((old-perm (string-to-number (match-string 1) 8))
462 (new-perm (string-to-number (match-string 2) 8)))
463 (goto-char (match-end 0))
466 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
471 :cr-score (string-to-number (match-string 2))
472 :cr-from (match-string 3)
473 :cr-to (match-string 4)
474 :status (stgit-file-status-code (match-string 1))
475 :file (match-string 3)))
476 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
484 :status (stgit-file-status-code (match-string 1))
485 :file (match-string 2))))))
486 (ewoc-enter-last ewoc file))
487 (goto-char (match-end 0))))
488 (unless (ewoc-nth ewoc 0)
489 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
490 'face 'stgit-description-face))))
494 (defun stgit-select-file ()
495 (let ((filename (expand-file-name
496 (stgit-file-file (stgit-patched-file-at-point)))))
497 (unless (file-exists-p filename)
498 (error "File does not exist"))
499 (find-file filename)))
501 (defun stgit-select-patch ()
502 (let ((patchname (stgit-patch-name-at-point)))
503 (if (memq patchname stgit-expanded-patches)
504 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
505 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
506 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
507 (move-to-column (stgit-goal-column)))
509 (defun stgit-select ()
510 "Expand or collapse the current entry"
512 (case (get-text-property (point) 'entry-type)
514 (stgit-select-patch))
518 (error "No patch or file on line"))))
520 (defun stgit-find-file-other-window ()
521 "Open file at point in other window"
523 (let ((patched-file (stgit-patched-file-at-point)))
525 (error "No file on the current line"))
526 (let ((filename (expand-file-name (stgit-file-file patched-file))))
527 (unless (file-exists-p filename)
528 (error "File does not exist"))
529 (find-file-other-window filename))))
532 "Hide the stgit buffer."
536 (defun stgit-git-status ()
537 "Show status using `git-status'."
539 (unless (fboundp 'git-status)
540 (error "The stgit-git-status command requires git-status"))
541 (let ((dir default-directory))
542 (save-selected-window
546 (defun stgit-goal-column ()
547 "Return goal column for the current line"
548 (case (get-text-property (point) 'entry-type)
553 (defun stgit-next-line (&optional arg)
554 "Move cursor vertically down ARG lines"
557 (move-to-column (stgit-goal-column)))
559 (defun stgit-previous-line (&optional arg)
560 "Move cursor vertically up ARG lines"
563 (move-to-column (stgit-goal-column)))
565 (defun stgit-next-patch (&optional arg)
566 "Move cursor down ARG patches."
568 (ewoc-goto-next stgit-ewoc (or arg 1))
569 (move-to-column goal-column))
571 (defun stgit-previous-patch (&optional arg)
572 "Move cursor up ARG patches."
574 (ewoc-goto-prev stgit-ewoc (or arg 1))
575 (move-to-column goal-column))
577 (defvar stgit-mode-hook nil
578 "Run after `stgit-mode' is setup.")
580 (defvar stgit-mode-map nil
581 "Keymap for StGit major mode.")
583 (unless stgit-mode-map
584 (let ((toggle-map (make-keymap)))
585 (suppress-keymap toggle-map)
586 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
587 '(("t" . stgit-toggle-worktree)))
588 (setq stgit-mode-map (make-keymap))
589 (suppress-keymap stgit-mode-map)
590 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
593 ("\d" . stgit-unmark-up)
594 ("u" . stgit-unmark-down)
597 ("\C-p" . stgit-previous-line)
598 ("\C-n" . stgit-next-line)
599 ([up] . stgit-previous-line)
600 ([down] . stgit-next-line)
601 ("p" . stgit-previous-patch)
602 ("n" . stgit-next-patch)
603 ("\M-{" . stgit-previous-patch)
604 ("\M-}" . stgit-next-patch)
605 ("s" . stgit-git-status)
607 ("r" . stgit-refresh)
608 ("\C-c\C-r" . stgit-rename)
610 ("M" . stgit-move-patches)
615 ("U" . stgit-uncommit)
616 ("\r" . stgit-select)
617 ("o" . stgit-find-file-other-window)
618 ("i" . stgit-file-toggle-index)
619 (">" . stgit-push-next)
620 ("<" . stgit-pop-next)
621 ("P" . stgit-push-or-pop)
625 ([(control ?/)] . stgit-undo)
626 ("\C-_" . stgit-undo)
629 ("q" . stgit-quit)))))
632 "Major mode for interacting with StGit.
635 (kill-all-local-variables)
636 (buffer-disable-undo)
637 (setq mode-name "StGit"
638 major-mode 'stgit-mode
640 (use-local-map stgit-mode-map)
641 (set (make-local-variable 'list-buffers-directory) default-directory)
642 (set (make-local-variable 'stgit-marked-patches) nil)
643 (set (make-local-variable 'stgit-expanded-patches) nil)
644 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
645 (set-variable 'truncate-lines 't)
646 (add-hook 'after-save-hook 'stgit-update-saved-file)
647 (run-hooks 'stgit-mode-hook))
649 (defun stgit-update-saved-file ()
650 (let* ((file (expand-file-name buffer-file-name))
651 (dir (file-name-directory file))
652 (gitdir (condition-case nil (git-get-top-dir dir)
654 (buffer (and gitdir (stgit-find-buffer gitdir))))
656 (with-current-buffer buffer
657 (stgit-refresh-worktree)))))
659 (defun stgit-add-mark (patchsym)
660 "Mark the patch PATCHSYM."
661 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
663 (defun stgit-remove-mark (patchsym)
664 "Unmark the patch PATCHSYM."
665 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
667 (defun stgit-clear-marks ()
668 "Unmark all patches."
669 (setq stgit-marked-patches '()))
671 (defun stgit-patch-at-point (&optional cause-error)
672 (get-text-property (point) 'patch-data))
674 (defun stgit-patch-name-at-point (&optional cause-error)
675 "Return the patch name on the current line as a symbol.
676 If CAUSE-ERROR is not nil, signal an error if none found."
677 (let ((patch (stgit-patch-at-point)))
679 (stgit-patch-name patch))
681 (error "No patch on this line")))))
683 (defun stgit-patched-file-at-point ()
684 (get-text-property (point) 'file-data))
686 (defun stgit-patches-marked-or-at-point ()
687 "Return the symbols of the marked patches, or the patch on the current line."
688 (if stgit-marked-patches
690 (let ((patch (stgit-patch-name-at-point)))
695 (defun stgit-goto-patch (patchsym)
696 "Move point to the line containing patch PATCHSYM.
697 If that patch cannot be found, do nothing."
698 (let ((node (ewoc-nth stgit-ewoc 0)))
699 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
701 (setq node (ewoc-next stgit-ewoc node)))
703 (ewoc-goto-node stgit-ewoc node)
704 (move-to-column goal-column))))
709 (stgit-capture-output nil
714 "Mark the patch under point."
716 (let* ((node (ewoc-locate stgit-ewoc))
717 (patch (ewoc-data node)))
718 (stgit-add-mark (stgit-patch-name patch))
719 (ewoc-invalidate stgit-ewoc node))
722 (defun stgit-unmark-up ()
723 "Remove mark from the patch on the previous line."
725 (stgit-previous-patch)
726 (let* ((node (ewoc-locate stgit-ewoc))
727 (patch (ewoc-data node)))
728 (stgit-remove-mark (stgit-patch-name patch))
729 (ewoc-invalidate stgit-ewoc node))
730 (move-to-column (stgit-goal-column)))
732 (defun stgit-unmark-down ()
733 "Remove mark from the patch on the current line."
735 (let* ((node (ewoc-locate stgit-ewoc))
736 (patch (ewoc-data node)))
737 (stgit-remove-mark (stgit-patch-name patch))
738 (ewoc-invalidate stgit-ewoc node))
741 (defun stgit-rename (name)
742 "Rename the patch under point to NAME."
743 (interactive (list (read-string "Patch name: "
744 (symbol-name (stgit-patch-name-at-point t)))))
745 (let ((old-patchsym (stgit-patch-name-at-point t)))
746 (stgit-capture-output nil
747 (stgit-run "rename" old-patchsym name))
748 (let ((name-sym (intern name)))
749 (when (memq old-patchsym stgit-expanded-patches)
750 (setq stgit-expanded-patches
751 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
752 (when (memq old-patchsym stgit-marked-patches)
753 (setq stgit-marked-patches
754 (cons name-sym (delq old-patchsym stgit-marked-patches))))
756 (stgit-goto-patch name-sym))))
758 (defun stgit-repair ()
761 (stgit-capture-output nil
762 (stgit-run "repair"))
765 (defun stgit-available-branches ()
766 "Returns a list of the available stg branches"
767 (let ((output (with-output-to-string
768 (stgit-run "branch" "--list")))
771 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
772 (setq result (cons (match-string 1 output) result))
773 (setq start (match-end 0)))
776 (defun stgit-branch (branch)
777 "Switch to branch BRANCH."
778 (interactive (list (completing-read "Switch to branch: "
779 (stgit-available-branches))))
780 (stgit-capture-output nil (stgit-run "branch" "--" branch))
783 (defun stgit-commit (count)
784 "Run stg commit on COUNT commits.
785 Interactively, the prefix argument is used as COUNT."
787 (stgit-capture-output nil (stgit-run "commit" "-n" count))
790 (defun stgit-uncommit (count)
791 "Run stg uncommit on COUNT commits.
792 Interactively, the prefix argument is used as COUNT."
794 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
797 (defun stgit-push-next (npatches)
798 "Push the first unapplied patch.
799 With numeric prefix argument, push that many patches."
801 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
803 (stgit-refresh-git-status))
805 (defun stgit-pop-next (npatches)
806 "Pop the topmost applied patch.
807 With numeric prefix argument, pop that many patches."
809 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
811 (stgit-refresh-git-status))
813 (defun stgit-applied-at-point ()
814 "Is the patch on the current line applied?"
817 (looking-at "[>+]")))
819 (defun stgit-push-or-pop ()
820 "Push or pop the patch on the current line."
822 (let ((patchsym (stgit-patch-name-at-point t))
823 (applied (stgit-applied-at-point)))
824 (stgit-capture-output nil
825 (stgit-run (if applied "pop" "push") patchsym))
829 "Go to the patch on the current line."
831 (let ((patchsym (stgit-patch-name-at-point t)))
832 (stgit-capture-output nil
833 (stgit-run "goto" patchsym))
836 (defun stgit-id (patchsym)
837 "Return the git commit id for PATCHSYM.
838 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
839 (if (keywordp patchsym)
841 (let ((result (with-output-to-string
842 (stgit-run-silent "id" patchsym))))
843 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
844 (error "Cannot find commit id for %s" patchsym))
845 (match-string 1 result))))
848 "Show the patch on the current line."
850 (stgit-capture-output "*StGit patch*"
851 (case (get-text-property (point) 'entry-type)
853 (let* ((patched-file (stgit-patched-file-at-point))
854 (patch-name (stgit-patch-name-at-point))
855 (patch-id (stgit-id patch-name))
856 (args (append (and (stgit-file-cr-from patched-file)
857 (if stgit-expand-find-copies-harder
858 '("--find-copies-harder")
860 (cond ((eq patch-id :index)
865 (list (concat patch-id "^") patch-id)))
867 (if (stgit-file-copy-or-rename patched-file)
868 (list (stgit-file-cr-from patched-file)
869 (stgit-file-cr-to patched-file))
870 (list (stgit-file-file patched-file))))))
871 (apply 'stgit-run-git "diff" args)))
873 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
874 (stgit-patch-name-at-point)))
876 (error "No patch or file at point")))
877 (with-current-buffer standard-output
878 (goto-char (point-min))
881 (defun stgit-move-change-to-index (file status)
882 "Copies the workspace state of FILE to index, using git add or git rm"
883 (let ((op (if (file-exists-p file) "add" "rm")))
884 (stgit-capture-output "*git output*"
885 (stgit-run-git op "--" file))))
887 (defun stgit-remove-change-from-index (file status)
888 "Unstages the change in FILE from the index"
889 (stgit-capture-output "*git output*"
890 (stgit-run-git "reset" "-q" "--" file)))
892 (defun stgit-file-toggle-index ()
893 "Move modified file in or out of the index."
895 (let ((patched-file (stgit-patched-file-at-point)))
897 (error "No file on the current line"))
898 (let ((patch-name (stgit-patch-name-at-point)))
899 (cond ((eq patch-name :work)
900 (stgit-move-change-to-index (stgit-file-file patched-file)
901 (stgit-file-status patched-file)))
902 ((eq patch-name :index)
903 (stgit-remove-change-from-index (stgit-file-file patched-file)
904 (stgit-file-status patched-file)))
906 (error "Can only move files in the working tree to index")))))
907 (stgit-refresh-worktree)
908 (stgit-refresh-index))
911 "Edit the patch on the current line."
913 (let ((patchsym (stgit-patch-name-at-point t))
914 (edit-buf (get-buffer-create "*StGit edit*"))
915 (dir default-directory))
916 (log-edit 'stgit-confirm-edit t nil edit-buf)
917 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
918 (setq default-directory dir)
919 (let ((standard-output edit-buf))
920 (stgit-run-silent "edit" "--save-template=-" patchsym))))
922 (defun stgit-confirm-edit ()
924 (let ((file (make-temp-file "stgit-edit-")))
925 (write-region (point-min) (point-max) file)
926 (stgit-capture-output nil
927 (stgit-run "edit" "-f" file stgit-edit-patchsym))
928 (with-current-buffer log-edit-parent-buffer
931 (defun stgit-new (add-sign)
933 With a prefix argument, include a \"Signed-off-by:\" line at the
936 (let ((edit-buf (get-buffer-create "*StGit edit*"))
937 (dir default-directory))
938 (log-edit 'stgit-confirm-new t nil edit-buf)
939 (setq default-directory dir)
942 (let ((standard-output (current-buffer)))
943 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
945 (defun stgit-confirm-new ()
947 (let ((file (make-temp-file "stgit-edit-")))
948 (write-region (point-min) (point-max) file)
949 (stgit-capture-output nil
950 (stgit-run "new" "-f" file))
951 (with-current-buffer log-edit-parent-buffer
954 (defun stgit-create-patch-name (description)
955 "Create a patch name from a long description"
957 (while (> (length description) 0)
958 (cond ((string-match "\\`[a-zA-Z_-]+" description)
959 (setq patch (downcase (concat patch
960 (match-string 0 description))))
961 (setq description (substring description (match-end 0))))
962 ((string-match "\\` +" description)
963 (setq patch (concat patch "-"))
964 (setq description (substring description (match-end 0))))
965 ((string-match "\\`[^a-zA-Z_-]+" description)
966 (setq description (substring description (match-end 0))))))
967 (cond ((= (length patch) 0)
969 ((> (length patch) 20)
970 (substring patch 0 20))
973 (defun stgit-delete (patchsyms &optional spill-p)
974 "Delete the patches in PATCHSYMS.
975 Interactively, delete the marked patches, or the patch at point.
977 With a prefix argument, or SPILL-P, spill the patch contents to
978 the work tree and index."
979 (interactive (list (stgit-patches-marked-or-at-point)
982 (error "No patches to delete"))
983 (let ((npatches (length patchsyms)))
984 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
986 (if (= 1 npatches) "" "es")
988 " (spilling contents to index)"
990 (let ((args (if spill-p
991 (cons "--spill" patchsyms)
993 (stgit-capture-output nil
994 (apply 'stgit-run "delete" args))
997 (defun stgit-move-patches-target ()
998 "Return the patchsym indicating a target patch for
999 `stgit-move-patches'.
1001 This is either the patch at point, or one of :top and :bottom, if
1002 the point is after or before the applied patches."
1004 (let ((patchsym (stgit-patch-name-at-point)))
1005 (cond (patchsym patchsym)
1006 ((save-excursion (re-search-backward "^>" nil t)) :top)
1009 (defun stgit-sort-patches (patchsyms)
1010 "Returns the list of patches in PATCHSYMS sorted according to
1011 their position in the patch series, bottommost first.
1013 PATCHSYMS may not contain duplicate entries."
1014 (let (sorted-patchsyms
1015 (series (with-output-to-string
1016 (with-current-buffer standard-output
1017 (stgit-run-silent "series" "--noprefix"))))
1019 (while (string-match "^\\(.+\\)" series start)
1020 (let ((patchsym (intern (match-string 1 series))))
1021 (when (memq patchsym patchsyms)
1022 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1023 (setq start (match-end 0)))
1024 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1026 (unless (= (length patchsyms) (length sorted-patchsyms))
1027 (error "Internal error"))
1031 (defun stgit-move-patches (patchsyms target-patch)
1032 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1033 If TARGET-PATCH is :bottom or :top, move the patches to the
1034 bottom or top of the stack, respectively.
1036 Interactively, move the marked patches to where the point is."
1037 (interactive (list stgit-marked-patches
1038 (stgit-move-patches-target)))
1040 (error "Need at least one patch to move"))
1042 (unless target-patch
1043 (error "Point not at a patch"))
1045 (if (eq target-patch :top)
1046 (stgit-capture-output nil
1047 (apply 'stgit-run "float" patchsyms))
1049 ;; need to have patchsyms sorted by position in the stack
1050 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1051 (while sorted-patchsyms
1052 (setq sorted-patchsyms
1053 (and (stgit-capture-output nil
1054 (if (eq target-patch :bottom)
1055 (stgit-run "sink" "--" (car sorted-patchsyms))
1056 (stgit-run "sink" "--to" target-patch "--"
1057 (car sorted-patchsyms))))
1058 (cdr sorted-patchsyms))))))
1061 (defun stgit-squash (patchsyms)
1062 "Squash the patches in PATCHSYMS.
1063 Interactively, squash the marked patches.
1065 Unless there are any conflicts, the patches will be merged into
1066 one patch, which will occupy the same spot in the series as the
1067 deepest patch had before the squash."
1068 (interactive (list stgit-marked-patches))
1069 (when (< (length patchsyms) 2)
1070 (error "Need at least two patches to squash"))
1071 (let ((stgit-buffer (current-buffer))
1072 (edit-buf (get-buffer-create "*StGit edit*"))
1073 (dir default-directory)
1074 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1075 (log-edit 'stgit-confirm-squash t nil edit-buf)
1076 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1077 (setq default-directory dir)
1078 (let ((result (let ((standard-output edit-buf))
1079 (apply 'stgit-run-silent "squash"
1080 "--save-template=-" sorted-patchsyms))))
1082 ;; stg squash may have reordered the patches or caused conflicts
1083 (with-current-buffer stgit-buffer
1086 (unless (eq 0 result)
1088 (rename-buffer "*StGit error*")
1089 (resize-temp-buffer-window)
1090 (switch-to-buffer-other-window stgit-buffer)
1091 (error "stg squash failed")))))
1093 (defun stgit-confirm-squash ()
1095 (let ((file (make-temp-file "stgit-edit-")))
1096 (write-region (point-min) (point-max) file)
1097 (stgit-capture-output nil
1098 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1099 (with-current-buffer log-edit-parent-buffer
1101 ;; Go to first marked patch and stay there
1102 (goto-char (point-min))
1103 (re-search-forward (concat "^[>+-]\\*") nil t)
1104 (move-to-column goal-column)
1105 (let ((pos (point)))
1109 (defun stgit-help ()
1110 "Display help for the StGit mode."
1112 (describe-function 'stgit-mode))
1114 (defun stgit-undo (&optional arg)
1116 With prefix argument, run it with the --hard flag."
1118 (stgit-capture-output nil
1120 (stgit-run "undo" "--hard")
1121 (stgit-run "undo")))
1124 (defun stgit-refresh (&optional arg)
1126 With prefix argument, refresh the marked patch or the patch under point."
1128 (let ((patchargs (if arg
1129 (let ((patches (stgit-patches-marked-or-at-point)))
1130 (cond ((null patches)
1131 (error "No patch to update"))
1132 ((> (length patches) 1)
1133 (error "Too many patches selected"))
1135 (cons "-p" patches))))
1137 (stgit-capture-output nil
1138 (apply 'stgit-run "refresh" patchargs))
1139 (stgit-refresh-git-status))
1142 (defcustom stgit-default-show-worktree
1144 "Set to non-nil to by default show the working tree in a new stgit buffer.
1146 This value is used as the default value for `stgit-show-worktree'."
1150 (defvar stgit-show-worktree nil
1151 "Show work tree and index in the stgit buffer.
1153 See `stgit-default-show-worktree' for its default value.")
1155 (defun stgit-toggle-worktree (&optional arg)
1156 "Toggle the visibility of the work tree.
1157 With arg, show the work tree if arg is positive.
1159 Its initial setting is controlled by `stgit-default-show-worktree'."
1161 (setq stgit-show-worktree
1164 (not stgit-show-worktree)))