Fix --authdate date parsing (other formats)
[stgit/kha.git] / contrib / stgit.el
blobe25d433c2d809aa07f5ba2772cc9847031a88183
1 ;; stgit.el: An emacs mode for StGit
2 ;;
3 ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se>
4 ;;
5 ;; To install: put this file on the load-path and place the following
6 ;; in your .emacs file:
7 ;;
8 ;; (require 'stgit)
9 ;;
10 ;; To start: `M-x stgit'
12 (when (< emacs-major-version 22)
13 (error "Emacs older than 22 is not supported by stgit.el"))
15 (require 'git nil t)
16 (require 'cl)
17 (require 'comint)
18 (require 'dired)
19 (require 'ewoc)
20 (require 'easymenu)
21 (require 'format-spec)
23 (defun stgit-set-default (symbol value)
24 "Set default value of SYMBOL to VALUE using `set-default' and
25 reload all StGit buffers."
26 (set-default symbol value)
27 (dolist (buf (buffer-list))
28 (with-current-buffer buf
29 (when (derived-mode-p 'stgit-mode)
30 (stgit-post-refresh buf :reload)))))
32 (defgroup stgit nil
33 "A user interface for the StGit patch maintenance tool."
34 :group 'tools
35 :link '(function-link stgit)
36 :link '(url-link "http://www.procode.org/stgit/"))
38 (defcustom stgit-default-show-worktree t
39 "Set to non-nil to by default show the working tree in a new stgit buffer.
41 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle this \
42 setting in an already-started StGit buffer."
43 :type 'boolean
44 :group 'stgit
45 :link '(variable-link stgit-show-worktree))
47 (defcustom stgit-default-show-unknown nil
48 "Set to non-nil to by default show unknown files a new stgit buffer.
50 Use \\<stgit-mode-map>\\[stgit-toggle-unknown] to toggle this \
51 setting in an already-started StGit buffer."
52 :type 'boolean
53 :group 'stgit
54 :link '(variable-link stgit-show-unknown))
56 (defcustom stgit-default-show-ignored nil
57 "Set to non-nil to by default show ignored files a new stgit buffer.
59 Use \\<stgit-mode-map>\\[stgit-toggle-ignored] to toggle this \
60 setting in an already-started StGit buffer."
61 :type 'boolean
62 :group 'stgit
63 :link '(variable-link stgit-show-ignored))
65 (defcustom stgit-default-show-patch-names t
66 "If non-nil, default to showing patch names in a new stgit buffer.
68 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] \
69 to toggle the this setting in an already-started StGit buffer."
70 :type 'boolean
71 :group 'stgit
72 :link '(variable-link stgit-show-patch-names))
74 (defcustom stgit-default-show-committed nil
75 "Set to nil to inhibit showing of historical git commits by default.
77 Use \\<stgit-mode-map>\\[stgit-toggle-committed] \
78 to toggle this setting and to control how many commits are
79 shown."
80 :type 'boolean
81 :group 'stgit
82 :link '(variable-link stgit-default-committed-count)
83 :link '(variable-link stgit-show-committed))
85 (defcustom stgit-default-committed-count 5
86 "The number of historical commits to show when `stgit-show-committed'
87 is enabled."
88 :type 'number
89 :group 'stgit
90 :link '(variable-link stgit-default-show-committed)
91 :link '(variable-link stgit-committed-count))
93 (defcustom stgit-default-show-svn t
94 "Set to non-nil to by default show subversion information in a
95 new stgit buffer.
97 Use \\<stgit-mode-map>\\[stgit-toggle-svn] to toggle this \
98 setting in an already-started StGit buffer."
99 :type 'boolean
100 :group 'stgit
101 :link '(variable-link stgit-show-worktree))
103 (defcustom stgit-abbreviate-copies-and-renames t
104 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
105 instead of \"dir/old/file -> dir/new/file\"."
106 :type 'boolean
107 :group 'stgit
108 :set 'stgit-set-default)
110 (defcustom stgit-find-copies-harder nil
111 "Try harder to find copied files when listing patches.
113 When not nil, runs git diff-tree with the --find-copies-harder
114 flag, which reduces performance."
115 :type 'boolean
116 :group 'stgit
117 :set 'stgit-set-default)
119 (defcustom stgit-show-worktree-mode 'center
120 "This variable controls where the \"Index\" and \"Work tree\"
121 will be shown on in the buffer.
123 It can be set to 'top (above all patches), 'center (show between
124 applied and unapplied patches), and 'bottom (below all patches)."
125 :type '(radio (const :tag "above all patches (top)" top)
126 (const :tag "between applied and unapplied patches (center)"
127 center)
128 (const :tag "below all patches (bottom)" bottom))
129 :group 'stgit
130 :link '(variable-link stgit-show-worktree)
131 :set 'stgit-set-default)
133 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
134 "The format string used to format patch lines.
135 The format string is passed to `format-spec' and the following
136 format characters are recognized:
138 %s - A '+', '-', '>' or space, depending on whether the patch is
139 applied, unapplied, top, or something else.
141 %m - An asterisk if the patch is marked, and a space otherwise.
143 %n - The patch name.
145 %e - The string \"(empty) \" if the patch is empty.
147 %d - The short patch description.
149 %D - The short patch description, or the patch name.
151 When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
152 variable is used instead."
153 :type 'string
154 :group 'stgit
155 :set 'stgit-set-default)
157 (defcustom stgit-noname-patch-line-format "%s%m%e%D"
158 "The alternate format string used to format patch lines.
159 It has the same semantics as `stgit-patch-line-format', and the
160 display can be toggled between the two formats using \
161 \\<stgit-mode-map>\\[stgit-toggle-patch-names].
163 The alternate form is used when the patch name is hidden."
164 :type 'string
165 :group 'stgit
166 :set 'stgit-set-default)
168 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
169 "The format string used to format file lines.
170 The format string is passed to `format-spec' and the following
171 format characters are recognized:
173 %s - A string describing the status of the file.
175 %m - Mode change information
177 %n - The file name.
179 %c - A description of file changes."
180 :type 'string
181 :group 'stgit
182 :set 'stgit-set-default)
184 (defcustom stgit-git-program "git"
185 "The program used by `stgit-mode' to run git."
186 :type 'string
187 :group 'stgit)
189 (defcustom stgit-stg-program "stg"
190 "The program used by `stgit-mode' to run StGit."
191 :type 'string
192 :group 'stgit)
194 (defgroup stgit-faces nil
195 "Faces for `stgit-mode'."
196 :group 'stgit)
198 (defface stgit-branch-name-face
199 '((t :inherit bold))
200 "The face used for the StGit branch name"
201 :group 'stgit-faces)
203 (defface stgit-top-patch-face
204 '((((background dark)) (:weight bold :foreground "yellow"))
205 (((background light)) (:weight bold :foreground "purple"))
206 (t (:weight bold)))
207 "The face used for the top patch names"
208 :group 'stgit-faces)
210 (defface stgit-applied-patch-face
211 '((((background dark)) (:foreground "light yellow"))
212 (((background light)) (:foreground "purple"))
213 (t ()))
214 "The face used for applied patch names"
215 :group 'stgit-faces)
217 (defface stgit-unapplied-patch-face
218 '((((background dark)) (:foreground "gray80"))
219 (((background light)) (:foreground "orchid"))
220 (t ()))
221 "The face used for unapplied patch names"
222 :group 'stgit-faces)
224 (defface stgit-committed-patch-face
225 '((((background dark)) (:foreground "gray50"))
226 (((background light)) (:foreground "gray50"))
227 (t ()))
228 "The face used for already committed patch names"
229 :group 'stgit-faces)
231 (defface stgit-description-face
232 '((((background dark)) (:foreground "tan"))
233 (((background light)) (:foreground "dark red")))
234 "The face used for StGit descriptions"
235 :group 'stgit-faces)
237 (defface stgit-index-work-tree-title-face
238 '((((supports :slant italic)) :slant italic)
239 (t :inherit bold))
240 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
241 :group 'stgit-faces)
243 (defface stgit-unmerged-file-face
244 '((((class color) (background light)) (:foreground "red" :bold t))
245 (((class color) (background dark)) (:foreground "red" :bold t)))
246 "StGit mode face used for unmerged file status"
247 :group 'stgit-faces)
249 (defface stgit-unknown-file-face
250 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
251 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
252 "StGit mode face used for unknown file status"
253 :group 'stgit-faces)
255 (defface stgit-ignored-file-face
256 '((((class color) (background light)) (:foreground "grey60"))
257 (((class color) (background dark)) (:foreground "grey40")))
258 "StGit mode face used for ignored files")
260 (defface stgit-file-permission-face
261 '((((class color) (background light)) (:foreground "green" :bold t))
262 (((class color) (background dark)) (:foreground "green" :bold t)))
263 "StGit mode face used for permission changes."
264 :group 'stgit-faces)
266 (defface stgit-modified-file-face
267 '((((class color) (background light)) (:foreground "purple"))
268 (((class color) (background dark)) (:foreground "salmon")))
269 "StGit mode face used for modified file status"
270 :group 'stgit-faces)
272 (defun stgit (dir)
273 "Manage StGit patches for the tree in DIR.
275 See `stgit-mode' for commands available."
276 (interactive "DDirectory: \n")
277 (switch-to-stgit-buffer (git-get-top-dir dir))
278 (stgit-reload))
280 (defun stgit-assert-mode ()
281 "Signal an error if not in an StGit buffer."
282 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
284 (unless (fboundp 'git-get-top-dir)
285 (defun git-get-top-dir (dir)
286 "Retrieve the top-level directory of a git tree."
287 (let ((cdup (with-output-to-string
288 (with-current-buffer standard-output
289 (cd dir)
290 (unless (eq 0 (call-process stgit-git-program nil t nil
291 "rev-parse" "--show-cdup"))
292 (error "Cannot find top-level git tree for %s" dir))))))
293 (expand-file-name (concat (file-name-as-directory dir)
294 (car (split-string cdup "\n")))))))
296 (defun stgit-refresh-git-status (&optional dir)
297 "If it exists, refresh the `git-status' buffer belonging to
298 directory DIR or `default-directory'"
299 (when (and (fboundp 'git-find-status-buffer)
300 (fboundp 'git-refresh-status))
301 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
302 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
303 (when git-status-buffer
304 (with-current-buffer git-status-buffer
305 (git-refresh-status))))))
307 (defun stgit-find-buffer (dir)
308 "Return the buffer displaying StGit patches for DIR, or nil if none."
309 (setq dir (file-truename (file-name-as-directory dir)))
310 (let ((buffers (buffer-list)))
311 (while (and buffers
312 (not (with-current-buffer (car buffers)
313 (and (eq major-mode 'stgit-mode)
314 (string= (file-truename default-directory) dir)))))
315 (setq buffers (cdr buffers)))
316 (and buffers (car buffers))))
318 (defun switch-to-stgit-buffer (dir)
319 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
320 (setq dir (file-name-as-directory dir))
321 (let ((buffer (stgit-find-buffer dir)))
322 (switch-to-buffer (or buffer
323 (create-stgit-buffer dir)))))
325 (defstruct (stgit-patch
326 (:conc-name stgit-patch->))
327 status name desc empty files-ewoc)
329 (defun stgit-patch-display-name (patch)
330 (let ((name (stgit-patch->name patch)))
331 (case name
332 (:index "Index")
333 (:work "Work Tree")
334 (t (symbol-name name)))))
336 (defun stgit-insert-without-trailing-whitespace (text)
337 "Insert TEXT in buffer using `insert', without trailing whitespace.
338 A newline is appended."
339 (unless (string-match "\\(.*?\\) *$" text)
340 (error))
341 (insert (match-string 1 text) ?\n))
343 (defun stgit-line-format ()
344 "Return the current line format; one of
345 `stgit-patch-line-format' and `stgit-noname-patch-line-format'"
346 (if stgit-show-patch-names
347 stgit-patch-line-format
348 stgit-noname-patch-line-format))
350 (defun stgit-patch-pp (patch)
351 (let* ((status (stgit-patch->status patch))
352 (start (point))
353 (name (stgit-patch->name patch))
354 (face (cdr (assq status stgit-patch-status-face-alist)))
355 (fmt (stgit-line-format))
356 (spec (format-spec-make
357 ?s (case status
358 ('applied "+")
359 ('top ">")
360 ('unapplied "-")
361 (t " "))
362 ?m (if (memq name stgit-marked-patches)
363 "*" " ")
364 ?n (propertize (stgit-patch-display-name patch)
365 'face face
366 'syntax-table (string-to-syntax "w"))
367 ?e (if (stgit-patch->empty patch) "(empty) " "")
368 ?d (propertize (or (stgit-patch->desc patch) "")
369 'face 'stgit-description-face)
370 ?D (propertize (let ((desc (stgit-patch->desc patch)))
371 (if (zerop (length desc))
372 (stgit-patch-display-name patch)
373 desc))
374 'face face)))
375 (text (format-spec fmt spec)))
377 (stgit-insert-without-trailing-whitespace text)
378 (put-text-property start (point) 'entry-type 'patch)
379 (when (memq name stgit-expanded-patches)
380 (stgit-insert-patch-files patch))
381 (put-text-property start (point) 'patch-data patch)))
383 (defun create-stgit-buffer (dir)
384 "Create a buffer for showing StGit patches.
385 Argument DIR is the repository path."
386 (let ((buf (create-file-buffer (concat dir "*stgit*")))
387 (inhibit-read-only t))
388 (with-current-buffer buf
389 (setq default-directory dir)
390 (stgit-mode)
391 (set (make-local-variable 'stgit-ewoc)
392 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
393 (setq buffer-read-only t))
394 buf))
396 (def-edebug-spec stgit-capture-output
397 (form body))
398 (defmacro stgit-capture-output (name &rest body)
399 "Capture StGit output and, if there was any output, show it in a window
400 at the end.
401 Returns the result of the last form in BODY."
402 (declare (debug ([&or stringp null] body))
403 (indent 1))
404 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
405 (stgit-dir default-directory)
406 (inhibit-read-only t))
407 (set-buffer-major-mode output-buf)
408 (with-current-buffer output-buf
409 (buffer-disable-undo)
410 (erase-buffer)
411 (setq default-directory stgit-dir)
412 (setq buffer-read-only t))
413 (prog1
414 (let ((standard-output output-buf))
415 ,@body)
416 (with-current-buffer output-buf
417 (set-buffer-modified-p nil)
418 (setq buffer-read-only t)
419 (if (< (point-min) (point-max))
420 (display-buffer output-buf t))))))
422 (defun stgit-make-run-args (args)
423 "Return a copy of ARGS with its elements converted to strings."
424 (mapcar (lambda (x)
425 ;; don't use (format "%s" ...) to limit type errors
426 (cond ((stringp x) x)
427 ((integerp x) (number-to-string x))
428 ((symbolp x) (symbol-name x))
430 (error "Bad element in stgit-make-run-args args: %S" x))))
431 args))
433 (defvar stgit-inhibit-messages nil
434 "Set to non-nil to inhibit messages when running `stg' commands.
435 See also `stgit-message'.")
436 (defun stgit-message (format-spec &rest args)
437 "Call `message' on the arguments unless `stgit-inhibit-messages' is non-nil."
438 (unless stgit-inhibit-messages
439 (apply 'message format-spec args)))
441 (defmacro stgit-show-task-message (message &rest body)
442 "Display \"MESSAGE...\" before executing BODY and then display
443 \"MESSAGE...done\" when done. MESSAGE will only be evaluated if
444 necessary and no message will be shown if MESSAGE is nil.
446 If `stgit-inhibit-messages' is non-nil, messages are
447 suppressed. See also `stgit-message'. If MESSAGE is non-nil, BODY
448 will be executed with `stgit-inhibit-messages' set to `t'.
450 Returns the return value of BODY."
451 (declare (indent 1) (debug (form body)))
452 (let ((msg (make-symbol "msg")))
453 `(let ((,msg (and (not stgit-inhibit-messages)
454 ,message)))
455 (when ,msg (message "%s..." ,msg))
456 (prog1
457 (let ((stgit-inhibit-messages (or ,msg stgit-inhibit-messages)))
458 ,@body)
459 (when ,msg (message "%s...done" ,msg))))))
461 (defun stgit-run (&rest args)
462 (setq args (stgit-make-run-args args))
463 (stgit-show-task-message
464 (mapconcat #'identity `("Running" ,stgit-stg-program ,@args) " ")
465 (apply 'call-process stgit-stg-program nil standard-output nil args)))
467 (defun stgit-run-silent (&rest args)
468 (let ((stgit-inhibit-messages t))
469 (apply 'stgit-run args)))
471 (defun stgit-run-git (&rest args)
472 (setq args (stgit-make-run-args args))
473 (stgit-show-task-message
474 (mapconcat #'identity `("Running" ,stgit-git-program ,@args) " ")
475 (apply 'call-process stgit-git-program nil standard-output nil args)))
477 (defun stgit-run-git-silent (&rest args)
478 (let ((stgit-inhibit-messages t))
479 (apply 'stgit-run-git args)))
481 (defun stgit-index-empty-p ()
482 "Returns non-nil if the index contains no changes from HEAD."
483 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
485 (defun stgit-work-tree-empty-p ()
486 "Returns non-nil if the work tree contains no changes from index."
487 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
489 (defvar stgit-did-advise nil
490 "Set to non-nil if appropriate (non-stgit) git functions have
491 been advised to update the stgit status when necessary.")
493 (defconst stgit-allowed-branch-name-re
494 ;; Disallow control characters, space, del, and "/:@^{}~" in
495 ;; "/"-separated parts; parts may not start with a period (.)
496 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
497 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
498 "Regular expression that (new) branch names must match.")
500 (defun stgit-refresh-index ()
501 (when stgit-index-node
502 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
504 (defun stgit-refresh-worktree ()
505 (when stgit-worktree-node
506 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
508 (defun stgit-run-series-insert-index (ewoc)
509 (setq index-node (cons ewoc (ewoc-enter-last ewoc
510 (make-stgit-patch
511 :status 'index
512 :name :index
513 :desc nil
514 :empty nil)))
515 worktree-node (cons ewoc (ewoc-enter-last ewoc
516 (make-stgit-patch
517 :status 'work
518 :name :work
519 :desc nil
520 :empty nil)))))
522 (defun stgit-get-position (&optional position)
523 "Return `stgit-mode' position information at POSITION (point by
524 default) that can be used to restore the point using
525 `stgit-restore-position'."
526 (let ((opoint (point)))
527 (and position (goto-char position))
528 (prog1
529 (list (stgit-patch-name-at-point)
530 (let ((f (stgit-patched-file-at-point)))
531 (and f (stgit-file->file f)))
532 (line-number-at-pos)
533 (current-column))
534 (goto-char opoint))))
536 (defun stgit-restore-position (state)
537 "Move point to the position in STATE, as returned by
538 `stgit-get-position'."
539 (destructuring-bind (patch file line column) state
540 (unless (and patch (case (stgit-goto-patch patch file)
541 ((t) (move-to-column column) t)
542 ((:patch) t)))
543 (goto-char (point-min))
544 (forward-line (1- line))
545 (move-to-column (if patch
546 (stgit-goal-column)
547 column)))))
549 (defun stgit-get-window-state ()
550 "Return the state of the buffer and its windows. Use
551 `stgit-restore-window-state' to restore the state."
552 (list (current-buffer)
553 (mapcar (lambda (window)
554 (cons window
555 (stgit-get-position
556 (window-point window))))
557 (get-buffer-window-list (current-buffer)
558 t t))
559 (stgit-get-position (point))
560 (let ((mark (mark)))
561 (and mark
562 (stgit-get-position mark)))
563 mark-active
564 transient-mark-mode))
566 (defun stgit-restore-window-state (state)
567 "Restore the state of the stgit buffer and windows in STATE, as
568 obtained from `stgit-get-window-state'."
569 (destructuring-bind
570 (buffer window-states buffer-state mark-state
571 old-mark-active old-transient-mark-mode)
572 state
573 (with-current-buffer buffer
574 (mapc (lambda (x) (let ((window (car x))
575 (state (cdr x)))
576 (when (and (window-live-p window)
577 (eq (window-buffer window) buffer))
578 (stgit-restore-position state)
579 (set-window-point window (point)))))
580 window-states)
581 (let ((mark-point (when mark-state
582 (stgit-restore-position mark-state)
583 (point))))
584 (stgit-restore-position buffer-state)
585 (if (and mark-point (null old-mark-active))
586 (set-marker (mark-marker) mark-point)
587 (set-mark mark-point))
588 (setq mark-active old-mark-active
589 transient-mark-mode old-transient-mark-mode)))))
591 (defmacro stgit-save-excursion (&rest body)
592 "Execute BODY and, for each window displaying the current
593 buffer, move point and mark back to the file, patch, or line
594 where they were."
595 (declare (indent 0) (debug (body)))
596 (let ((state (make-symbol "state")))
597 `(let ((,state (stgit-get-window-state))
598 deactivate-mark)
599 ,@body
600 (stgit-restore-window-state ,state))))
602 (defun stgit-svn-find-rev (sha1 hash)
603 "Return the subversion revision corresponding to SHA1 as
604 reported by git svn.
606 Cached data is stored in HASH, which must have been created
607 using (make-hash-table :test 'equal)."
608 (let ((result (gethash sha1 hash t)))
609 (when (eq result t)
610 (let ((svn-rev (with-output-to-string
611 (stgit-run-git-silent "svn" "find-rev"
612 "--" sha1))))
613 (setq result (when (string-match "\\`[0-9]+" svn-rev)
614 (string-to-number (match-string 0 svn-rev))))
615 (puthash sha1 result hash)))
616 result))
618 (defun stgit-run-series (ewoc)
619 (setq stgit-index-node nil
620 stgit-worktree-node nil)
621 (let (all-patchsyms base)
622 (when (and stgit-show-committed
623 (> stgit-committed-count 0)
624 (setq base (condition-case nil
625 (stgit-id "{base}")
626 (error nil))))
627 (let* ((show-svn stgit-show-svn)
628 (svn-hash stgit-svn-find-rev-hash)
629 (nentries (format "-%s" stgit-committed-count)))
630 (with-temp-buffer
631 (let* ((standard-output (current-buffer))
632 (fmt (stgit-line-format))
633 (commit-abbrev (when (string-match "%-\\([0-9]+\\)n" fmt)
634 (string-to-number (match-string 1 fmt))))
635 (exit-status (stgit-run-git-silent "--no-pager" "log"
636 "--reverse"
637 "--pretty=oneline"
638 nentries
639 base)))
640 (goto-char (point-min))
641 (if (not (zerop exit-status))
642 (message "Failed to run git log")
643 (while (not (eobp))
644 (unless (looking-at
645 "\\([0-9a-f]+\\)\\(\\.\\.\\.\\)? \\(.*\\)")
646 (error "Syntax error in output from git log"))
647 (let* ((state 'committed)
648 (name (match-string 1))
649 (desc (match-string 3))
650 (empty nil))
652 (when show-svn
653 (let ((svn-rev (stgit-svn-find-rev name svn-hash)))
654 (when svn-rev
655 (setq desc (format "(r%s) %s" svn-rev desc)))))
657 (and commit-abbrev
658 (< commit-abbrev (length name))
659 (setq name (substring name 0 commit-abbrev)))
661 (setq name (intern name))
663 (setq all-patchsyms (cons name all-patchsyms))
664 (ewoc-enter-last ewoc
665 (make-stgit-patch
666 :status state
667 :name name
668 :desc desc
669 :empty empty)))
670 (forward-line 1)))))))
671 (let ((inserted-index (not stgit-show-worktree))
672 index-node
673 worktree-node)
674 (with-temp-buffer
675 (let* ((standard-output (current-buffer))
676 (exit-status (stgit-run-silent "series"
677 "--description" "--empty")))
678 (goto-char (point-min))
679 (if (not (zerop exit-status))
680 (cond ((looking-at "stg series: \\(.*\\)")
681 (setq inserted-index t)
682 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
683 (substitute-command-keys
684 "-- not initialized; run \\[stgit-init]")))
685 ((looking-at ".*")
686 (error "Error running stg: %s"
687 (match-string 0))))
688 (while (not (eobp))
689 (unless (looking-at
690 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
691 (error "Syntax error in output from stg series"))
692 (let* ((state-str (match-string 2))
693 (state (cond ((string= state-str ">") 'top)
694 ((string= state-str "+") 'applied)
695 ((string= state-str "-") 'unapplied)))
696 (name (intern (match-string 4)))
697 (desc (match-string 5))
698 (empty (string= (match-string 1) "0")))
699 (unless inserted-index
700 (when (or (eq stgit-show-worktree-mode 'top)
701 (and (eq stgit-show-worktree-mode 'center)
702 (eq state 'unapplied)))
703 (setq inserted-index t)
704 (stgit-run-series-insert-index ewoc)))
705 (setq all-patchsyms (cons name all-patchsyms))
706 (ewoc-enter-last ewoc
707 (make-stgit-patch
708 :status state
709 :name name
710 :desc desc
711 :empty empty)))
712 (forward-line 1)))))
713 (unless inserted-index
714 (stgit-run-series-insert-index ewoc))
715 (setq stgit-index-node index-node
716 stgit-worktree-node worktree-node
717 stgit-marked-patches (intersection stgit-marked-patches
718 all-patchsyms)))))
720 (defun stgit-current-branch ()
721 "Return the name of the current branch."
722 (substring (with-output-to-string
723 (stgit-run-silent "branch"))
724 0 -1))
726 (defun stgit-reload (&optional description)
727 "Update the contents of the StGit buffer.
729 If DESCRIPTION is non-nil, it is displayed as a status message
730 during the operation."
731 (interactive)
732 (stgit-assert-mode)
733 (stgit-show-task-message description
734 (let ((inhibit-read-only t))
735 (stgit-save-excursion
736 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
737 (ewoc-set-hf stgit-ewoc
738 (concat "Branch: "
739 (propertize (stgit-current-branch)
740 'face 'stgit-branch-name-face)
741 "\n\n")
742 (if stgit-show-worktree
743 "--"
744 (propertize
745 (substitute-command-keys "--\n\"\
746 \\[stgit-toggle-worktree]\" shows the working tree\n")
747 'face 'stgit-description-face)))
748 (stgit-run-series stgit-ewoc))
749 (stgit-refresh-git-status))))
751 (defconst stgit-file-status-code-strings
752 (mapcar (lambda (arg)
753 (cons (car arg)
754 (propertize (cadr arg) 'face (car (cddr arg)))))
755 '((add "Added" stgit-modified-file-face)
756 (copy "Copied" stgit-modified-file-face)
757 (delete "Deleted" stgit-modified-file-face)
758 (modify "Modified" stgit-modified-file-face)
759 (rename "Renamed" stgit-modified-file-face)
760 (mode-change "Mode change" stgit-modified-file-face)
761 (unmerged "Unmerged" stgit-unmerged-file-face)
762 (unknown "Unknown" stgit-unknown-file-face)
763 (ignore "Ignored" stgit-ignored-file-face)))
764 "Alist of code symbols to description strings")
766 (defconst stgit-patch-status-face-alist
767 '((applied . stgit-applied-patch-face)
768 (top . stgit-top-patch-face)
769 (unapplied . stgit-unapplied-patch-face)
770 (committed . stgit-committed-patch-face)
771 (index . stgit-index-work-tree-title-face)
772 (work . stgit-index-work-tree-title-face))
773 "Alist of face to use for a given patch status")
775 (defun stgit-file-status-code-as-string (file)
776 "Return stgit status code for FILE as a string"
777 (let* ((code (assq (stgit-file->status file)
778 stgit-file-status-code-strings))
779 (score (stgit-file->cr-score file)))
780 (when code
781 (if (and score (/= score 100))
782 (format "%s %s" (cdr code)
783 (propertize (format "%d%%" score)
784 'face 'stgit-description-face))
785 (cdr code)))))
787 (defun stgit-file-status-code (str &optional score)
788 "Return stgit status code from git status string"
789 (let ((code (assoc str '(("A" . add)
790 ("C" . copy)
791 ("D" . delete)
792 ("I" . ignore)
793 ("M" . modify)
794 ("R" . rename)
795 ("T" . mode-change)
796 ("U" . unmerged)
797 ("X" . unknown)))))
798 (setq code (if code (cdr code) 'unknown))
799 (when (stringp score)
800 (if (> (length score) 0)
801 (setq score (string-to-number score))
802 (setq score nil)))
803 (if score (cons code score) code)))
805 (defconst stgit-file-type-strings
806 '((#o100 . "file")
807 (#o120 . "symlink")
808 (#o160 . "subproject"))
809 "Alist of names of file types")
811 (defun stgit-file-type-string (type)
812 "Return string describing file type TYPE (the high bits of file permission).
813 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
814 (let ((type-str (assoc type stgit-file-type-strings)))
815 (or (and type-str (cdr type-str))
816 (format "unknown type %o" type))))
818 (defun stgit-file-type-change-string (old-perm new-perm)
819 "Return string describing file type change from OLD-PERM to NEW-PERM.
820 Cf. `stgit-file-type-string'."
821 (let ((old-type (lsh old-perm -9))
822 (new-type (lsh new-perm -9)))
823 (cond ((= old-type new-type) "")
824 ((zerop new-type) "")
825 ((zerop old-type)
826 (if (= new-type #o100)
828 (format "(%s)" (stgit-file-type-string new-type))))
829 (t (format "(%s -> %s)"
830 (stgit-file-type-string old-type)
831 (stgit-file-type-string new-type))))))
833 (defun stgit-file-mode-change-string (old-perm new-perm)
834 "Return string describing file mode change from OLD-PERM to NEW-PERM.
835 Cf. `stgit-file-type-change-string'."
836 (setq old-perm (logand old-perm #o777)
837 new-perm (logand new-perm #o777))
838 (if (or (= old-perm new-perm)
839 (zerop old-perm)
840 (zerop new-perm))
842 (let* ((modified (logxor old-perm new-perm))
843 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
844 (cond ((zerop modified) "")
845 ((and (zerop not-x-modified)
846 (or (and (eq #o111 (logand old-perm #o111))
847 (propertize "-x" 'face 'stgit-file-permission-face))
848 (and (eq #o111 (logand new-perm #o111))
849 (propertize "+x" 'face
850 'stgit-file-permission-face)))))
851 (t (concat (propertize (format "%o" old-perm)
852 'face 'stgit-file-permission-face)
853 (propertize " -> "
854 'face 'stgit-description-face)
855 (propertize (format "%o" new-perm)
856 'face 'stgit-file-permission-face)))))))
858 (defstruct (stgit-file
859 (:conc-name stgit-file->))
860 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
862 (defun stgit-escape-file-name-p (name)
863 "Return non-nil if NAME must be escaped."
864 (save-match-data (string-match "[\t\n\"\\]" name)))
866 (defun stgit-escape-file-name (name &optional no-quotes)
867 "Escape NAME if necessary.
869 If NO-QUOTES is non-nil, do not enclose the result in double quotes."
870 (if (stgit-escape-file-name-p name)
871 (concat (if no-quotes "" "\"")
872 (mapconcat (lambda (c)
873 (case c
874 (?\t "\\t")
875 (?\n "\\n")
876 (?\" "\\\"")
877 (?\\ "\\\\")
878 (t (char-to-string c))))
879 name "")
880 (if no-quotes "" "\""))
881 name))
883 (defun stgit-describe-copy-or-rename (file)
884 (let* ((arrow (concat " "
885 (propertize "->" 'face 'stgit-description-face)
886 " "))
887 (esc-from (stgit-file->cr-from file))
888 (esc-to (stgit-file->cr-to file))
889 (quote "")
890 from to common-head common-tail)
892 (when (or (stgit-escape-file-name-p esc-from)
893 (stgit-escape-file-name-p esc-to))
894 (setq esc-from (stgit-escape-file-name esc-from t)
895 esc-to (stgit-escape-file-name esc-to t)
896 quote "\""))
898 (when stgit-abbreviate-copies-and-renames
899 (setq from (split-string esc-from "/")
900 to (split-string esc-to "/"))
902 (while (and from to (cdr from) (cdr to)
903 (string-equal (car from) (car to)))
904 (setq common-head (cons (car from) common-head)
905 from (cdr from)
906 to (cdr to)))
907 (setq common-head (nreverse common-head)
908 from (nreverse from)
909 to (nreverse to))
910 (while (and from to (cdr from) (cdr to)
911 (string-equal (car from) (car to)))
912 (setq common-tail (cons (car from) common-tail)
913 from (cdr from)
914 to (cdr to)))
915 (setq from (nreverse from)
916 to (nreverse to)))
918 (if (or common-head common-tail)
919 (concat quote
920 (if common-head
921 (mapconcat #'identity common-head "/")
923 (if common-head "/" "")
924 (propertize "{" 'face 'stgit-description-face)
925 (mapconcat #'identity from "/")
926 arrow
927 (mapconcat #'identity to "/")
928 (propertize "}" 'face 'stgit-description-face)
929 (if common-tail "/" "")
930 (if common-tail
931 (mapconcat #'identity common-tail "/")
933 quote)
934 (concat quote esc-from arrow esc-to quote))))
936 (defun stgit-file-pp (file)
937 (let ((start (point))
938 (spec (format-spec-make
939 ?s (stgit-file-status-code-as-string file)
940 ?m (stgit-file-mode-change-string
941 (stgit-file->old-perm file)
942 (stgit-file->new-perm file))
943 ?n (if (stgit-file->copy-or-rename file)
944 (stgit-describe-copy-or-rename file)
945 (stgit-escape-file-name (stgit-file->file file)))
946 ?c (propertize (stgit-file-type-change-string
947 (stgit-file->old-perm file)
948 (stgit-file->new-perm file))
949 'face 'stgit-description-face))))
950 (stgit-insert-without-trailing-whitespace
951 (format-spec stgit-file-line-format spec))
952 (add-text-properties start (point)
953 (list 'entry-type 'file
954 'file-data file))))
956 (defun stgit-find-copies-harder-diff-arg ()
957 "Return the flag to use with `git-diff' depending on the
958 `stgit-find-copies-harder' flag."
959 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
961 (defun stgit-insert-ls-files (args file-flag)
962 (let ((start (point)))
963 (apply 'stgit-run-git
964 (append '("ls-files" "--exclude-standard" "-z") args))
965 (goto-char start)
966 (while (looking-at "\\([^\0]*\\)\0")
967 (let ((name-len (- (match-end 0) (match-beginning 0))))
968 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
969 (forward-char name-len)))))
971 (defun stgit-process-files (callback)
972 (goto-char (point-min))
973 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
974 (goto-char (match-end 0)))
975 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
976 (let ((old-perm (string-to-number (match-string 1) 8))
977 (new-perm (string-to-number (match-string 2) 8)))
978 (goto-char (match-end 0))
979 (let ((file
980 (cond ((looking-at
981 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
982 (let* ((patch-status (stgit-patch->status patch))
983 (file-subexp (if (eq patch-status 'unapplied)
986 (file (match-string file-subexp)))
987 (make-stgit-file
988 :old-perm old-perm
989 :new-perm new-perm
990 :copy-or-rename t
991 :cr-score (string-to-number (match-string 2))
992 :cr-from (match-string 3)
993 :cr-to (match-string 4)
994 :status (stgit-file-status-code
995 (match-string 1))
996 :file file)))
997 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
998 (make-stgit-file
999 :old-perm old-perm
1000 :new-perm new-perm
1001 :copy-or-rename nil
1002 :cr-score nil
1003 :cr-from nil
1004 :cr-to nil
1005 :status (stgit-file-status-code
1006 (match-string 1))
1007 :file (match-string 2))))))
1008 (goto-char (match-end 0))
1009 (funcall callback file)))))
1012 (defun stgit-insert-patch-files (patch)
1013 "Expand (show modification of) the patch PATCH after the line
1014 at point."
1015 (let* ((patchsym (stgit-patch->name patch))
1016 (end (point-marker))
1017 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
1018 (ewoc (ewoc-create #'stgit-file-pp nil nil t))
1019 (show-ignored stgit-show-ignored)
1020 (show-unknown stgit-show-unknown))
1021 (set-marker-insertion-type end t)
1022 (setf (stgit-patch->files-ewoc patch) ewoc)
1023 (with-temp-buffer
1024 (let ((standard-output (current-buffer)))
1025 (apply 'stgit-run-git
1026 (cond ((eq patchsym :work)
1027 (let (standard-output)
1028 (stgit-run-git "update-index" "--refresh"))
1029 `("diff-files" "-0" ,@args))
1030 ((eq patchsym :index)
1031 `("diff-index" ,@args "--cached" "HEAD"))
1033 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
1035 (when (and (eq patchsym :work))
1036 (when show-ignored
1037 (stgit-insert-ls-files '("--ignored" "--others") "I"))
1038 (when show-unknown
1039 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
1040 "--others")
1041 "X"))
1042 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
1043 (point-min) (point-max)))
1045 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
1047 (unless (ewoc-nth ewoc 0)
1048 (ewoc-set-hf ewoc ""
1049 (concat " "
1050 (propertize "<no files>"
1051 'face 'stgit-description-face)
1052 "\n")))))
1053 (goto-char end)))
1055 (defun stgit-find-file-revision (file patchsym &optional other-window)
1056 (let ((filename (expand-file-name (concat (file-name-nondirectory file)
1057 ".~" (symbol-name patchsym) "~")
1058 (file-name-directory file))))
1059 (let ((coding-system-for-read 'no-conversion)
1060 (coding-system-for-write 'no-conversion))
1061 (with-temp-file filename
1062 (unless (zerop (stgit-run-git-silent "cat-file"
1063 "blob"
1064 (concat (if (eq patchsym :index)
1066 (stgit-id patchsym))
1067 ":" file)))
1068 (error "git cat-file failed"))))
1069 (funcall (if other-window
1070 'switch-to-buffer-other-window
1071 'switch-to-buffer)
1072 (find-file-noselect filename))
1073 (set (make-local-variable 'vc-parent-buffer) filename)))
1075 (defun stgit-find-file (&optional other-window this-rev)
1076 (let* ((file (or (stgit-patched-file-at-point)
1077 (error "No file at point")))
1078 (filename (expand-file-name (stgit-file->file file)))
1079 (patchsym (stgit-patch-name-at-point)))
1081 (if (and this-rev (not (eq patchsym :work)))
1082 (stgit-find-file-revision (stgit-file->file file)
1083 (stgit-patch-name-at-point)
1084 other-window)
1085 (unless (file-exists-p filename)
1086 (error "File does not exist"))
1087 (funcall (if other-window 'find-file-other-window 'find-file)
1088 filename)
1089 (when (eq (stgit-file->status file) 'unmerged)
1090 (smerge-mode 1)))))
1092 (defun stgit-expand (&optional patches collapse)
1093 "Show the contents of marked patches, or the patch at point.
1095 See also `stgit-collapse'.
1097 Non-interactively, operate on PATCHES, and collapse instead of
1098 expand if COLLAPSE is not nil."
1099 (interactive (list (stgit-patches-marked-or-at-point t)))
1100 (stgit-assert-mode)
1101 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
1102 patches stgit-expanded-patches)))
1103 (setq stgit-expanded-patches
1104 (if collapse
1105 (set-difference stgit-expanded-patches patches-diff)
1106 (append stgit-expanded-patches patches-diff)))
1107 (stgit-show-task-message (concat (if collapse "Collapsing" "Expanding")
1109 (if (= 1 (length patches-diff))
1110 "patch"
1111 "patches"))
1112 (ewoc-map #'(lambda (patch)
1113 (memq (stgit-patch->name patch) patches-diff))
1114 stgit-ewoc)))
1115 (move-to-column (stgit-goal-column)))
1117 (defun stgit-collapse (&optional patches)
1118 "Hide the contents of marked patches, or the patch at point.
1120 See also `stgit-expand'."
1121 (interactive (list (stgit-patches-marked-or-at-point t)))
1122 (stgit-assert-mode)
1123 (stgit-expand patches t))
1125 (defun stgit-select-patch ()
1126 (let ((patchname (stgit-patch-name-at-point)))
1127 (stgit-expand (list patchname)
1128 (memq patchname stgit-expanded-patches))))
1130 (defun stgit-expand-directory (file)
1131 (let* ((patch (stgit-patch-at-point))
1132 (ewoc (stgit-patch->files-ewoc patch))
1133 (node (ewoc-locate ewoc))
1134 (filename (stgit-file->file file))
1135 (start (make-marker))
1136 (end (make-marker)))
1138 (save-excursion
1139 (forward-line 1)
1140 (set-marker start (point))
1141 (set-marker end (point))
1142 (set-marker-insertion-type end t))
1144 (assert (string-match "/$" filename))
1145 ;; remove trailing "/"
1146 (setf (stgit-file->file file) (substring filename 0 -1))
1147 (ewoc-invalidate ewoc node)
1149 (with-temp-buffer
1150 (let ((standard-output (current-buffer)))
1151 (stgit-insert-ls-files (list "--directory" "--others"
1152 "--no-empty-directory" "--"
1153 filename)
1154 "X")
1155 (stgit-process-files (lambda (f)
1156 (setq node (ewoc-enter-after ewoc node f))))))
1158 (move-to-column (stgit-goal-column))
1160 (let ((inhibit-read-only t))
1161 (put-text-property start end 'patch-data patch))))
1163 (defun stgit-select-file ()
1164 (let* ((file (or (stgit-patched-file-at-point)
1165 (error "No file at point")))
1166 (filename (stgit-file->file file)))
1167 (if (string-match "/$" filename)
1168 (stgit-expand-directory file)
1169 (stgit-find-file))))
1171 (defun stgit-select ()
1172 "With point on a patch, toggle showing files in the patch.
1174 With point on a file, open the associated file. Opens the target
1175 file for (applied) copies and renames."
1176 (interactive)
1177 (stgit-assert-mode)
1178 (case (get-text-property (point) 'entry-type)
1179 ('patch
1180 (stgit-select-patch))
1181 ('file
1182 (stgit-select-file))
1184 (error "No patch or file on line"))))
1186 (defun stgit-find-file-other-window (&optional this-rev)
1187 "Open file at point in other window.
1189 With prefix argument, open a buffer with that revision of the file."
1190 (interactive "p")
1191 (stgit-assert-mode)
1192 (stgit-find-file t (> this-rev 1)))
1194 (defun stgit-find-file-merge ()
1195 "Open file at point and merge it using `smerge-ediff'."
1196 (interactive)
1197 (stgit-assert-mode)
1198 (stgit-find-file t)
1199 (let ((filename (file-name-nondirectory buffer-file-name)))
1200 (smerge-ediff (concat "*" filename " GIT*")
1201 (concat "*" filename " PATCH*"))))
1203 (defun stgit-quit ()
1204 "Hide the stgit buffer."
1205 (interactive)
1206 (stgit-assert-mode)
1207 (bury-buffer))
1209 (defun stgit-git-status ()
1210 "Show status using `git-status'."
1211 (interactive)
1212 (stgit-assert-mode)
1213 (unless (fboundp 'git-status)
1214 (error "The stgit-git-status command requires git-status"))
1215 (let ((dir default-directory))
1216 (save-selected-window
1217 (pop-to-buffer nil)
1218 (git-status dir))))
1220 (defun stgit-goal-column ()
1221 "Return goal column for the current line"
1222 (case (get-text-property (point) 'entry-type)
1223 ('patch 2)
1224 ('file 4)
1225 (t 0)))
1227 (defun stgit-next-line (&optional arg)
1228 "Move cursor vertically down ARG lines"
1229 (interactive "p")
1230 (stgit-assert-mode)
1231 (next-line arg)
1232 (move-to-column (stgit-goal-column)))
1234 (defun stgit-previous-line (&optional arg)
1235 "Move cursor vertically up ARG lines"
1236 (interactive "p")
1237 (stgit-assert-mode)
1238 (previous-line arg)
1239 (move-to-column (stgit-goal-column)))
1241 (defun stgit-next-patch (&optional arg)
1242 "Move cursor down ARG patches."
1243 (interactive "p")
1244 (stgit-assert-mode)
1245 (unless arg (setq arg 1))
1246 (cond ((< arg 0)
1247 (stgit-previous-patch (- arg)))
1248 ((zerop arg)
1249 (move-to-column (stgit-goal-column)))
1251 (when (stgit-at-header-p)
1252 (ewoc-goto-node stgit-ewoc (ewoc-nth stgit-ewoc 0))
1253 (setq arg (1- arg)))
1254 (ewoc-goto-next stgit-ewoc arg)
1255 (move-to-column goal-column))))
1257 (defun stgit-previous-patch (&optional arg)
1258 "Move cursor up ARG patches."
1259 (interactive "p")
1260 (stgit-assert-mode)
1261 (unless arg (setq arg 1))
1262 (cond ((< arg 0)
1263 (stgit-next-patch (- arg)))
1264 ((zerop arg)
1265 (move-to-column (stgit-goal-column)))
1266 ((stgit-at-header-p)
1267 (goto-char (point-min)))
1269 (let ((opatch (stgit-patch-at-point)))
1270 (when (stgit-patched-file-at-point)
1271 (setq arg (1- arg)))
1272 (ewoc-goto-prev stgit-ewoc arg)
1273 (unless (zerop arg)
1274 (when (eq opatch (stgit-patch-at-point))
1275 (goto-char (point-min)))))
1276 (move-to-column (stgit-goal-column)))))
1278 (defun stgit-previous-patch-group (&optional arg)
1279 "Move to the previous group of patches.
1281 If ARG is non-nil, do this ARG times. If ARG is negative, move
1282 -ARG groups forward instead; cf. `stgit-next-patch-group'."
1283 (interactive "p")
1284 (stgit-assert-mode)
1285 (if (< arg 0)
1286 (stgit-previous-patch-group (- arg))
1287 (while (and (not (bobp))
1288 (> arg 0))
1289 (stgit-previous-patch 1)
1290 (let* ((opoint (point))
1291 (patch (stgit-patch-at-point))
1292 (status (and patch (stgit-patch->status patch))))
1293 (while (and (not (bobp))
1294 (let* ((npatch (stgit-patch-at-point))
1295 (nstatus (and npatch (stgit-patch->status npatch))))
1296 (eq status nstatus)))
1297 (setq opoint (point))
1298 (stgit-previous-patch 1))
1299 (goto-char opoint))
1300 (setq arg (1- arg)))))
1302 (defun stgit-next-patch-group (&optional arg)
1303 "Move to the next group of patches.
1305 If ARG is non-nil, do this ARG times. If ARG is negative, move
1306 -ARG groups backwards instead; cf. `stgit-previous-patch-group'."
1307 (interactive "p")
1308 (stgit-assert-mode)
1309 (if (< arg 0)
1310 (stgit-previous-patch-group (- arg))
1311 (while (and (not (eobp))
1312 (> arg 0))
1313 (let* ((patch (stgit-patch-at-point))
1314 (status (and patch (stgit-patch->status patch))))
1315 (while (and (not (eobp))
1316 (let* ((npatch (stgit-patch-at-point))
1317 (nstatus (and npatch (stgit-patch->status npatch))))
1318 (eq status nstatus)))
1319 (stgit-next-patch 1)))
1320 (setq arg (1- arg)))))
1322 (defvar stgit-mode-hook nil
1323 "Run after `stgit-mode' is setup.")
1325 (defvar stgit-mode-map nil
1326 "Keymap for StGit major mode.")
1328 (unless stgit-mode-map
1329 (let ((diff-map (make-sparse-keymap))
1330 (toggle-map (make-sparse-keymap)))
1331 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
1332 '(("b" . stgit-diff-base)
1333 ("c" . stgit-diff-combined)
1334 ("m" . stgit-find-file-merge)
1335 ("o" . stgit-diff-ours)
1336 ("r" . stgit-diff-range)
1337 ("t" . stgit-diff-theirs)))
1338 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
1339 '(("n" . stgit-toggle-patch-names)
1340 ("t" . stgit-toggle-worktree)
1341 ("h" . stgit-toggle-committed)
1342 ("i" . stgit-toggle-ignored)
1343 ("u" . stgit-toggle-unknown)
1344 ("s" . stgit-toggle-svn)))
1345 (setq stgit-mode-map (make-keymap))
1346 (suppress-keymap stgit-mode-map)
1347 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
1348 `((" " . stgit-mark-down)
1349 ("m" . stgit-mark-down)
1350 ("\d" . stgit-unmark-up)
1351 ("u" . stgit-unmark-down)
1352 ("?" . stgit-help)
1353 ("h" . stgit-help)
1354 ("\C-p" . stgit-previous-line)
1355 ("\C-n" . stgit-next-line)
1356 ([up] . stgit-previous-line)
1357 ([down] . stgit-next-line)
1358 ("p" . stgit-previous-patch)
1359 ("n" . stgit-next-patch)
1360 ("\M-{" . stgit-previous-patch-group)
1361 ("\M-}" . stgit-next-patch-group)
1362 ([(control up)] . stgit-previous-patch-group)
1363 ([(control down)] . stgit-next-patch-group)
1364 ("s" . stgit-git-status)
1365 ("g" . stgit-reload-or-repair)
1366 ("r" . stgit-refresh)
1367 ("\C-c\C-r" . stgit-rename)
1368 ("e" . stgit-edit)
1369 ("M" . stgit-move-patches)
1370 ("S" . stgit-squash)
1371 ("N" . stgit-new)
1372 ("c" . stgit-new-and-refresh)
1373 ("\C-c\C-c" . stgit-commit)
1374 ("\C-c\C-u" . stgit-uncommit)
1375 ("U" . stgit-revert)
1376 ("R" . stgit-resolve-file)
1377 ("\r" . stgit-select)
1378 ("+" . stgit-expand)
1379 ("-" . stgit-collapse)
1380 ("o" . stgit-find-file-other-window)
1381 ("\C-o" . stgit-new-here)
1382 ([insertline] . stgit-new-here)
1383 ("i" . stgit-toggle-index)
1384 (">" . stgit-push-next)
1385 ("<" . stgit-pop-next)
1386 ("P" . stgit-push-or-pop)
1387 ("G" . stgit-goto)
1388 ("=" . stgit-diff)
1389 ("D" . stgit-delete)
1390 ([?\C-/] . stgit-undo)
1391 ("\C-_" . stgit-undo)
1392 ([?\C-c ?\C-/] . stgit-redo)
1393 ("\C-c\C-_" . stgit-redo)
1394 ("B" . stgit-branch)
1395 ("\C-c\C-b" . stgit-rebase)
1396 ("t" . ,toggle-map)
1397 ("d" . ,diff-map)
1398 ("q" . stgit-quit)
1399 ("!" . stgit-execute))))
1401 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1402 (and file (eq (stgit-file->status file)
1403 'unmerged))))
1404 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1405 (easy-menu-define stgit-menu stgit-mode-map
1406 "StGit Menu"
1407 `("StGit"
1408 ["Reload" stgit-reload-or-repair
1409 :help "Reload StGit status from disk"]
1410 ["Repair" stgit-repair
1411 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1412 :help "Repair StGit metadata"]
1414 ["Undo" stgit-undo t]
1415 ["Redo" stgit-redo t]
1417 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1419 ["New patch" stgit-new-and-refresh
1420 :help "Create a new patch from changes in index or work tree"
1421 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1422 ["New empty patch" stgit-new
1423 :help "Create a new, empty patch"]
1424 ["(Un)mark patch" stgit-toggle-mark
1425 :label (if (memq (stgit-patch-name-at-point nil t)
1426 stgit-marked-patches)
1427 "Unmark patch" "Mark patch")
1428 :active (stgit-patch-name-at-point nil t)]
1429 ["Expand/collapse patch"
1430 (let ((patches (stgit-patches-marked-or-at-point)))
1431 (if (member-if ,patch-collapsed-p patches)
1432 (stgit-expand patches)
1433 (stgit-collapse patches)))
1434 :label (if (member-if ,patch-collapsed-p
1435 (stgit-patches-marked-or-at-point))
1436 "Expand patches"
1437 "Collapse patches")
1438 :active (stgit-patches-marked-or-at-point)]
1439 ["Edit patch" stgit-edit
1440 :help "Edit patch comment"
1441 :active (stgit-patch-name-at-point nil t)]
1442 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1443 ["Push/pop patch" stgit-push-or-pop
1444 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1445 (stgit-applied-patchsyms t))
1446 "Pop patches" "Push patches")]
1447 ["Delete patches" stgit-delete
1448 :active (stgit-patches-marked-or-at-point nil t)]
1450 ["Move patches" stgit-move-patches
1451 :active stgit-marked-patches
1452 :help "Move marked patch(es) to point"]
1453 ["Squash patches" stgit-squash
1454 :active (> (length stgit-marked-patches) 1)
1455 :help "Merge marked patches into one"]
1457 ["Refresh top patch" stgit-refresh
1458 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1459 :help "Refresh the top patch with changes in index or work tree"]
1460 ["Refresh this patch" (stgit-refresh t)
1461 :keys "\\[universal-argument] \\[stgit-refresh]"
1462 :help "Refresh marked patch with changes in index or work tree"
1463 :active (and (not (and (stgit-index-empty-p)
1464 (stgit-work-tree-empty-p)))
1465 (stgit-patch-name-at-point nil t))]
1467 ["Find file" stgit-select
1468 :active (eq (get-text-property (point) 'entry-type) 'file)]
1469 ["Open file" stgit-find-file-other-window
1470 :active (eq (get-text-property (point) 'entry-type) 'file)]
1471 ["Toggle file index" stgit-toggle-index
1472 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1473 (memq (stgit-patch-name-at-point) '(:work :index)))
1474 :label (if (eq (stgit-patch-name-at-point) :work)
1475 "Move change to index"
1476 "Move change to work tree")]
1478 ["Show diff" stgit-diff
1479 :active (get-text-property (point) 'entry-type)]
1480 ["Show diff for range of applied patches" stgit-diff-range
1481 :active (= (length stgit-marked-patches) 1)]
1482 ("Merge"
1483 :active (stgit-git-index-unmerged-p)
1484 ["Combined diff" stgit-diff-combined
1485 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1486 ["Diff against base" stgit-diff-base
1487 :help "Show diff against the common base"
1488 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1489 ["Diff against ours" stgit-diff-ours
1490 :help "Show diff against our branch"
1491 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1492 ["Diff against theirs" stgit-diff-theirs
1493 :help "Show diff against their branch"
1494 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1496 ["Interactive merge" stgit-find-file-merge
1497 :help "Interactively merge the file"
1498 :active ,at-unmerged-file]
1499 ["Resolve file" stgit-resolve-file
1500 :help "Mark file conflict as resolved"
1501 :active ,at-unmerged-file]
1504 ["Show index & work tree" stgit-toggle-worktree :style toggle
1505 :selected stgit-show-worktree]
1506 ["Show unknown files" stgit-toggle-unknown :style toggle
1507 :selected stgit-show-unknown :active stgit-show-worktree]
1508 ["Show ignored files" stgit-toggle-ignored :style toggle
1509 :selected stgit-show-ignored :active stgit-show-worktree]
1510 ["Show patch names" stgit-toggle-patch-names :style toggle
1511 :selected stgit-show-patch-names]
1512 ["Show recent commits" stgit-toggle-committed :style toggle
1513 :selected stgit-show-committed]
1514 ["Show subversion info" stgit-toggle-svn :style toggle
1515 :selected stgit-show-svn]
1517 ["Switch branches" stgit-branch t
1518 :help "Switch to or create another branch"]
1519 ["Rebase branch" stgit-rebase t
1520 :help "Rebase the current branch"]
1522 ["Customize StGit" (customize-group 'stgit)]
1523 ))))
1525 ;; disable tool bar editing buttons
1526 (put 'stgit-mode 'mode-class 'special)
1528 (defun stgit-mode ()
1529 "Major mode for interacting with StGit.
1531 Start StGit using \\[stgit].
1533 Basic commands:
1534 \\<stgit-mode-map>\
1535 \\[stgit-help] Show this help text
1536 \\[stgit-quit] Hide the StGit buffer
1537 \\[describe-bindings] Show all key bindings
1539 \\[stgit-reload-or-repair] Reload the StGit buffer
1540 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1542 \\[stgit-undo] Undo most recent StGit operation
1543 \\[stgit-redo] Undo recent undo
1545 \\[stgit-git-status] Run `git-status' (if available)
1547 \\[stgit-execute] Run an stg shell command
1549 Movement commands:
1550 \\[stgit-previous-line] Move to previous line
1551 \\[stgit-next-line] Move to next line
1552 \\[stgit-previous-patch] Move to previous patch
1553 \\[stgit-next-patch] Move to next patch
1554 \\[stgit-previous-patch-group] Move to previous patch group
1555 \\[stgit-next-patch-group] Move to next patch group
1557 \\[stgit-mark-down] Mark patch and move down
1558 \\[stgit-unmark-up] Unmark patch and move up
1559 \\[stgit-unmark-down] Unmark patch and move down
1561 Commands for patches:
1562 \\[stgit-select] Toggle showing changed files in patch
1563 \\[stgit-refresh] Refresh patch with changes in index or work tree
1564 \\[stgit-diff] Show the patch log and diff
1566 \\[stgit-expand] Show changes in marked patches
1567 \\[stgit-collapse] Hide changes in marked patches
1569 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1570 \\[stgit-new] Create a new, empty patch
1571 \\[stgit-new-here] Create a new, empty patch before patch at point
1573 \\[stgit-rename] Rename patch
1574 \\[stgit-edit] Edit patch description
1575 \\[stgit-delete] Delete patch(es)
1577 \\[stgit-revert] Revert all changes in index or work tree
1578 \\[stgit-toggle-index] Toggle all changes between index and work tree
1580 \\[stgit-push-next] Push next patch onto stack
1581 \\[stgit-pop-next] Pop current patch from stack
1582 \\[stgit-push-or-pop] Push or pop marked patches
1583 \\[stgit-goto] Make patch at point current by popping or pushing
1585 \\[stgit-squash] Squash (meld together) patches
1586 \\[stgit-move-patches] Move marked patches to point
1588 \\[stgit-commit] Commit patch(es)
1589 \\[stgit-uncommit] Uncommit patch(es)
1591 Commands for files:
1592 \\[stgit-select] Open the file in this window
1593 \\[stgit-find-file-other-window] Open the file in another window
1594 \\[stgit-diff] Show the file's diff
1596 \\[stgit-toggle-index] Toggle change between index and work tree
1597 \\[stgit-revert] Revert changes to file
1599 Display commands:
1600 \\[stgit-toggle-patch-names] Toggle showing patch names
1601 \\[stgit-toggle-worktree] Toggle showing index and work tree
1602 \\[stgit-toggle-unknown] Toggle showing unknown files
1603 \\[stgit-toggle-ignored] Toggle showing ignored files
1604 \\[stgit-toggle-committed] Toggle showing recent commits
1605 \\[stgit-toggle-svn] Toggle showing subversion information
1607 Commands for diffs:
1608 \\[stgit-diff] Show diff of patch or file
1609 \\[stgit-diff-range] Show diff for range of patches
1610 \\[stgit-diff-base] Show diff against the merge base
1611 \\[stgit-diff-ours] Show diff against our branch
1612 \\[stgit-diff-theirs] Show diff against their branch
1614 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1615 ignore space changes.
1616 With two prefix arguments (e.g., \\[universal-argument] \
1617 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1619 Commands for merge conflicts:
1620 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1621 \\[stgit-resolve-file] Mark unmerged file as resolved
1623 Commands for branches:
1624 \\[stgit-branch] Switch to or create another branch
1625 \\[stgit-rebase] Rebase the current branch
1627 Customization variables:
1628 `stgit-abbreviate-copies-and-renames'
1629 `stgit-default-show-ignored'
1630 `stgit-default-show-patch-names'
1631 `stgit-default-show-unknown'
1632 `stgit-default-show-worktree'
1633 `stgit-default-show-committed'
1634 `stgit-default-show-svn'
1635 `stgit-default-committed-count'
1636 `stgit-find-copies-harder'
1637 `stgit-show-worktree-mode'
1639 See also \\[customize-group] for the \"stgit\" group."
1640 (kill-all-local-variables)
1641 (buffer-disable-undo)
1642 (setq mode-name "StGit"
1643 major-mode 'stgit-mode
1644 goal-column 2)
1645 (use-local-map stgit-mode-map)
1646 (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x)))
1647 `((list-buffers-directory . ,default-directory)
1648 (parse-sexp-lookup-properties . t)
1649 (stgit-expanded-patches . (:work :index))
1650 (stgit-index-node . nil)
1651 (stgit-worktree-node . nil)
1652 (stgit-marked-patches . nil)
1653 (stgit-svn-find-rev-hash . ,(make-hash-table :test 'equal))
1654 (stgit-committed-count . ,stgit-default-committed-count)
1655 (stgit-show-committed . ,stgit-default-show-committed)
1656 (stgit-show-ignored . ,stgit-default-show-ignored)
1657 (stgit-show-patch-names . ,stgit-default-show-patch-names)
1658 (stgit-show-svn . ,stgit-default-show-svn)
1659 (stgit-show-unknown . ,stgit-default-show-unknown)
1660 (stgit-show-worktree . ,stgit-default-show-worktree)))
1661 (set-variable 'truncate-lines 't)
1662 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1663 (unless stgit-did-advise
1664 (stgit-advise)
1665 (setq stgit-did-advise t))
1666 (run-hooks 'stgit-mode-hook))
1668 (defun stgit-advise-funlist (funlist)
1669 "Add advice to the functions in FUNLIST so we can refresh the
1670 stgit buffers as the git status of files change."
1671 (mapc (lambda (sym)
1672 (when (fboundp sym)
1673 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1674 (stgit-update-stgit-for-buffer :index)))
1675 (ad-activate sym)))
1676 funlist))
1678 (defun stgit-advise ()
1679 "Add advice to appropriate (non-stgit) git functions so we can
1680 refresh the stgit buffers as the git status of files change."
1681 (mapc (lambda (arg)
1682 (let ((feature (car arg))
1683 (funlist (cdr arg)))
1684 (if (featurep feature)
1685 (stgit-advise-funlist funlist)
1686 (add-to-list 'after-load-alist
1687 `(,feature (stgit-advise-funlist
1688 (quote ,funlist)))))))
1689 ;; lists of (<feature> <function> <function> ...) to be advised
1690 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1691 (git git-add-file git-checkout git-revert-file git-remove-file)
1692 (dired dired-delete-file))))
1694 (defvar stgit-pending-refresh-buffers nil
1695 "Alist of (`buffer' . `mode') of buffers that need to be
1696 refreshed. See `stgit-post-refresh' for the different values of
1697 `mode'.")
1699 (defun stgit-run-pending-refreshs ()
1700 "Run all pending stgit buffer updates as posted by `stgit-post-refresh'."
1701 (let ((buffers stgit-pending-refresh-buffers)
1702 (stgit-inhibit-messages t))
1703 (setq stgit-pending-refresh-buffers nil)
1704 (while buffers
1705 (let* ((elem (car buffers))
1706 (buffer (car elem))
1707 (mode (cdr elem)))
1708 (when (buffer-name buffer)
1709 (with-current-buffer buffer
1710 (stgit-save-excursion
1711 (if (eq mode :reload)
1712 (stgit-reload)
1713 (stgit-refresh-worktree)
1714 (when (eq mode :index)
1715 (stgit-refresh-index)))))))
1716 (setq buffers (cdr buffers)))))
1718 (defun stgit-post-refresh (buffer mode)
1719 "Update status in BUFFER when Emacs becomes idle.
1721 MODE specifies what to do:
1722 :work only update work tree
1723 :index update work tree and index
1724 :reload reload the entire buffer"
1725 (unless (memq mode '(:work :index :reload))
1726 (error "Illegal refresh mode in stgit-post-refresh"))
1727 (unless stgit-pending-refresh-buffers
1728 (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs))
1729 (let ((elem (assq buffer stgit-pending-refresh-buffers)))
1730 (if elem
1731 ;; if buffer is already present, update its mode if necessary
1732 (let ((omode (cdr elem)))
1733 (when (case mode
1734 (:index (eq mode :work))
1735 (:reload t))
1736 (setcdr elem mode)))
1737 ;; new entry
1738 (setq stgit-pending-refresh-buffers
1739 (cons (cons buffer mode)
1740 stgit-pending-refresh-buffers)))))
1742 (defun stgit-update-stgit-for-buffer (&optional mode)
1743 "When Emacs becomes idle, update the status in any `stgit-mode'
1744 buffer that shows the status of the current buffer.
1746 MODE specifies how to update the buffer. See `stgit-post-refresh'
1747 for the different values MODE can have."
1748 (let* ((dir (cond ((derived-mode-p 'stgit-mode 'stgit-status-mode 'dired-mode)
1749 default-directory)
1750 (buffer-file-name
1751 (file-name-directory
1752 (expand-file-name buffer-file-name)))))
1753 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1754 (error nil))))
1755 (buffer (and gitdir (stgit-find-buffer gitdir))))
1756 (when buffer
1757 (stgit-post-refresh buffer (or mode :work)))))
1759 (defun stgit-add-mark (patchsym)
1760 "Mark the patch PATCHSYM."
1761 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1763 (defun stgit-remove-mark (patchsym)
1764 "Unmark the patch PATCHSYM."
1765 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1767 (defun stgit-clear-marks ()
1768 "Unmark all patches."
1769 (setq stgit-marked-patches '()))
1771 (defun stgit-patch-at-point (&optional cause-error)
1772 (get-text-property (point) 'patch-data))
1774 (defun stgit-patch-name-at-point (&optional cause-error types)
1775 "Return the patch name on the current line as a symbol.
1776 If CAUSE-ERROR is not nil, signal an error if none found.
1778 TYPES controls which types of commits and patches can be returned.
1779 If it is t, only allow stgit patches; if 'allow-committed, also
1780 allow historical commits; if nil, also allow work tree and index."
1781 (let ((patch (stgit-patch-at-point)))
1782 (and patch
1783 (memq (stgit-patch->status patch)
1784 (case types
1785 ((nil) nil)
1786 ((allow-committed) '(work index))
1787 ((t) '(work index committed))
1788 (t (error "Bad value"))))
1789 (setq patch nil))
1790 (cond (patch
1791 (stgit-patch->name patch))
1792 (cause-error
1793 (error "No patch on this line")))))
1795 (defun stgit-patched-file-at-point ()
1796 (get-text-property (point) 'file-data))
1798 (defun stgit-patches-marked-or-at-point (&optional cause-error types)
1799 "Return the symbols of the marked patches, or the patch on the current line.
1800 If CAUSE-ERRROR is not nil, signal an error if none found.
1802 TYPES controls which types of commits and patches can be returned.
1803 If it is t, only allow stgit patches; if 'allow-committed, also
1804 allow historical commits; if nil, also allow work tree and index."
1805 (if stgit-marked-patches
1806 stgit-marked-patches
1807 (let ((patch (stgit-patch-name-at-point nil types)))
1808 (cond (patch (list patch))
1809 (cause-error (error "No patches marked or at this line"))
1810 (t nil)))))
1812 (defun stgit-goto-patch (patchsym &optional file)
1813 "Move point to the line containing patch PATCHSYM and return t.
1814 If that patch cannot be found, do nothing and return nil.
1816 If the patch was found and FILE is not nil, instead move to that
1817 file's line and return t. If FILE cannot be found, stay on the
1818 line of PATCHSYM and return :patch."
1819 (let ((node (ewoc-nth stgit-ewoc 0))
1820 result)
1821 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1822 patchsym)))
1823 (setq node (ewoc-next stgit-ewoc node)))
1824 (cond ((and node file)
1825 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1826 (file-node (ewoc-nth file-ewoc 0)))
1827 (while (and file-node
1828 (not (equal (stgit-file->file
1829 (ewoc-data file-node))
1830 file)))
1831 (setq file-node (ewoc-next file-ewoc file-node)))
1832 (if file-node
1833 (progn
1834 (ewoc-goto-node file-ewoc file-node)
1835 (setq result t))
1836 (ewoc-goto-node stgit-ewoc node)
1837 (setq result :patch))))
1838 (node
1839 (ewoc-goto-node stgit-ewoc node)
1840 (setq result t)))
1841 (when result
1842 (move-to-column (stgit-goal-column)))
1843 result))
1845 (defun stgit-init ()
1846 "Run stg init to initialize the current branch for use with StGit."
1847 (interactive)
1848 (stgit-assert-mode)
1849 (unless (zerop (stgit-capture-output nil
1850 (stgit-run "init")))
1851 (error "stg init failed"))
1852 (stgit-reload))
1854 (defun stgit-toggle-mark ()
1855 "Toggle mark on the patch under point."
1856 (interactive)
1857 (stgit-assert-mode)
1858 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1859 (stgit-unmark)
1860 (stgit-mark)))
1862 (defun stgit-mark ()
1863 "Mark the patch under point."
1864 (interactive)
1865 (stgit-assert-mode)
1866 (let* ((node (ewoc-locate stgit-ewoc))
1867 (patch (ewoc-data node)))
1868 (case (stgit-patch->status patch)
1869 (work (error "Cannot mark the work tree"))
1870 (index (error "Cannot mark the index"))
1871 (committed (error "Cannot mark a committed patch")))
1872 (stgit-add-mark (stgit-patch->name patch))
1873 (let ((column (current-column)))
1874 (ewoc-invalidate stgit-ewoc node)
1875 (move-to-column column))))
1877 (defun stgit-mark-down ()
1878 "Mark the patch under point and move to the next patch."
1879 (interactive)
1880 (stgit-mark)
1881 (stgit-next-patch))
1883 (defun stgit-unmark ()
1884 "Remove mark from the patch on the current line."
1885 (interactive)
1886 (stgit-assert-mode)
1887 (let* ((node (ewoc-locate stgit-ewoc))
1888 (patch (ewoc-data node)))
1889 (stgit-remove-mark (stgit-patch->name patch))
1890 (let ((column (current-column)))
1891 (ewoc-invalidate stgit-ewoc node)
1892 (move-to-column column))))
1894 (defun stgit-unmark-up ()
1895 "Remove mark from the patch on the previous line."
1896 (interactive)
1897 (stgit-assert-mode)
1898 (stgit-previous-patch)
1899 (stgit-unmark))
1901 (defun stgit-unmark-down ()
1902 "Remove mark from the patch on the current line."
1903 (interactive)
1904 (stgit-assert-mode)
1905 (stgit-unmark)
1906 (stgit-next-patch))
1908 (defun stgit-rename (name)
1909 "Rename the patch under point to NAME."
1910 (interactive (list
1911 (read-string "Patch name: "
1912 (symbol-name (stgit-patch-name-at-point t t)))))
1913 (stgit-assert-mode)
1914 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1915 (unless (string-equal (symbol-name old-patchsym) name)
1916 (stgit-capture-output nil
1917 (stgit-run "rename" "--" old-patchsym name))
1918 (let ((name-sym (intern name)))
1919 (when (memq old-patchsym stgit-expanded-patches)
1920 (setq stgit-expanded-patches
1921 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1922 (when (memq old-patchsym stgit-marked-patches)
1923 (setq stgit-marked-patches
1924 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1925 (stgit-reload)
1926 (stgit-goto-patch name-sym)))))
1928 (defun stgit-reload-or-repair (repair)
1929 "Update the contents of the StGit buffer (`stgit-reload').
1931 With a prefix argument, repair the StGit metadata if the branch
1932 was modified with git commands (`stgit-repair')."
1933 (interactive "P")
1934 (stgit-assert-mode)
1935 (if repair
1936 (stgit-repair)
1937 (stgit-reload)))
1939 (defun stgit-repair ()
1940 "Run stg repair."
1941 (interactive)
1942 (stgit-assert-mode)
1943 (stgit-capture-output nil
1944 (stgit-run "repair"))
1945 (stgit-reload))
1947 (defun stgit-available-branches (&optional all skip-current)
1948 "Returns a list of the names of the available stg branches as strings.
1950 If ALL is not nil, also return non-stgit branches.
1951 If SKIP-CURRENT is not nil, do not include the current branch."
1952 (let ((output (with-output-to-string
1953 (stgit-run "branch" "--list")))
1954 (pattern (format "^%c\\s-+%c\\s-+\\(\\S-+\\)"
1955 (if skip-current ?\ ?.)
1956 (if all ?. ?s)))
1957 (start 0)
1958 result)
1959 (while (string-match pattern output start)
1960 (setq result (cons (match-string 1 output) result))
1961 (setq start (match-end 0)))
1962 result))
1964 (defun stgit-branch (branch)
1965 "Switch to or create branch BRANCH."
1966 (interactive (list (completing-read "Switch to branch: "
1967 (stgit-available-branches nil t))))
1968 (stgit-assert-mode)
1970 (when (equal branch (stgit-current-branch))
1971 (error "Branch is already current"))
1973 (let ((merge (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))))
1975 (when (cond ((and merge
1976 (not (yes-or-no-p
1977 "Attempt to merge uncommitted changes? ")))
1978 nil)
1980 ((member branch (stgit-available-branches t))
1981 (stgit-capture-output nil
1982 (apply 'stgit-run
1983 (append '("branch")
1984 (and merge '("--merge"))
1985 '("--")
1986 (list branch))))
1988 ((not (string-match stgit-allowed-branch-name-re branch))
1989 (error "Invalid branch name"))
1990 (merge
1991 (error "Cannot merge changes into a new branch"))
1992 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1993 (let ((branch-point (completing-read
1994 "Branch from (default current branch): "
1995 (stgit-available-branches))))
1996 (stgit-capture-output nil
1997 (apply 'stgit-run
1998 `("branch" "--create" "--"
1999 ,branch
2000 ,@(unless (zerop (length branch-point))
2001 (list branch-point)))))
2002 t)))
2004 ;; Do not expand any (normal) patches in the new branch
2005 (setq stgit-expanded-patches
2006 (remove-if-not (lambda (p) (memq p '(:work :index)))
2007 stgit-expanded-patches))
2009 (stgit-reload))))
2011 (defun stgit-available-refs (&optional omit-stgit)
2012 "Returns a list of the available git refs.
2013 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
2014 (let* ((output (with-output-to-string
2015 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
2016 "refs/tags" "refs/heads"
2017 "refs/remotes")))
2018 (result (split-string output "\n" t)))
2019 (mapcar (lambda (s)
2020 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
2021 (substring s (match-end 0))
2023 (if omit-stgit
2024 (delete-if (lambda (s)
2025 (string-match "^refs/heads/.*\\.stgit$" s))
2026 result)
2027 result))))
2029 (defun stgit-parent-branch ()
2030 "Return the parent branch of the current stg branch as per
2031 git-config setting branch.<branch>.stgit.parentbranch."
2032 (let ((output (with-output-to-string
2033 (stgit-run-git-silent "config"
2034 (format "branch.%s.stgit.parentbranch"
2035 (stgit-current-branch))))))
2036 (when (string-match ".*" output)
2037 (match-string 0 output))))
2039 (defun stgit-rebase (new-base)
2040 "Rebase the current branch to NEW-BASE.
2042 Interactively, first ask which branch to rebase to. Defaults to
2043 what git-config branch.<branch>.stgit.parentbranch is set to."
2044 (interactive (list (completing-read "Rebase to: "
2045 (stgit-available-refs t)
2046 nil nil
2047 (stgit-parent-branch))))
2048 (stgit-assert-mode)
2049 (stgit-capture-output nil (stgit-run "rebase" "--" new-base))
2050 (stgit-reload))
2052 (defun stgit-commit (count)
2053 "Run stg commit on (at most) COUNT commits.
2054 Interactively, the prefix argument is used as COUNT.
2055 A negative COUNT will uncommit using `stgit-uncommit' instead."
2056 (interactive "p")
2057 (stgit-assert-mode)
2058 (if (< count 0)
2059 (stgit-uncommit (- count))
2060 (setq count (min count (length (stgit-applied-patches t))))
2061 (stgit-capture-output nil (stgit-run "commit" "-n" count))
2062 (stgit-reload)))
2064 (defun stgit-uncommit (count)
2065 "Run stg uncommit on COUNT commits.
2066 Interactively, the prefix argument is used as COUNT.
2067 A negative COUNT will commit using `stgit-commit' instead."
2068 (interactive "p")
2069 (stgit-assert-mode)
2070 (if (< count 0)
2071 (stgit-commit (- count))
2072 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
2073 (stgit-reload)))
2075 (defun stgit-neighbour-file ()
2076 "Return the file name of the next file after point, or the
2077 previous file if point is at the last file within a patch."
2078 (let ((old-point (point))
2079 neighbour-file)
2080 (and (zerop (forward-line 1))
2081 (let ((f (stgit-patched-file-at-point)))
2082 (and f (setq neighbour-file (stgit-file->file f)))))
2083 (goto-char old-point)
2084 (unless neighbour-file
2085 (and (zerop (forward-line -1))
2086 (let ((f (stgit-patched-file-at-point)))
2087 (and f (setq neighbour-file (stgit-file->file f)))))
2088 (goto-char old-point))
2089 neighbour-file))
2091 (defun stgit-unmerged-file-stages (file)
2092 "Returns list of the merge stages that contain FILE, which
2093 must be an unmerged file.
2095 Stage 1, the common ancestor, is 'ancestor.
2096 Stage 2, HEAD, is 'head.
2097 Stage 3, MERGE_HEAD, is 'merge-head."
2098 (let ((output (with-output-to-string
2099 (stgit-run-git-silent "ls-files" "-u" "-z" "--"
2100 (stgit-file->file file))))
2101 stages
2102 start)
2103 (while (string-match "\\([0-7]*\\) \\([0-9A-Fa-f]\\{40\\}\\) \\([1-3]\\)\t\\([^\0]*\\)\0"
2104 output start)
2105 (setq stages (cons (elt [ancestor head merge-head]
2106 (1- (string-to-number (match-string 3 output))))
2107 stages)
2108 start (match-end 0)))
2109 stages))
2111 (defun stgit-revert-file ()
2112 "Revert the file at point, which must be in the index or the
2113 working tree."
2114 (interactive)
2115 (stgit-assert-mode)
2116 (let* ((patched-file (or (stgit-patched-file-at-point)
2117 (error "No file on the current line")))
2118 (patch-name (stgit-patch-name-at-point))
2119 (file-status (stgit-file->status patched-file))
2120 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
2121 (stgit-file->cr-to patched-file))
2122 ((eq file-status 'add)
2123 (stgit-file->file patched-file))))
2124 (co-file (cond ((eq file-status 'rename)
2125 (stgit-file->cr-from patched-file))
2126 ((not (memq file-status '(copy add unknown)))
2127 (stgit-file->file patched-file))))
2128 (next-file (stgit-neighbour-file))
2129 (rm-disk-file (when (memq file-status '(ignore unknown))
2130 (stgit-file->file patched-file)))
2131 add-file)
2132 (unless (memq patch-name '(:work :index))
2133 (error "No index or working tree file on this line"))
2135 (when (eq file-status 'unmerged)
2136 (let ((stages (stgit-unmerged-file-stages patched-file)))
2137 (if (memq 'head stages)
2138 (setq add-file (stgit-file->file patched-file))
2139 (setq rm-file (stgit-file->file patched-file)
2140 co-file nil))))
2142 (when (yes-or-no-p (cond (rm-disk-file
2143 (format "Delete %s? " rm-disk-file))
2144 ((and rm-file co-file)
2145 "Revert 2 files? ")
2147 (format "Revert %s? " (or rm-file co-file)))))
2148 (when rm-disk-file
2149 (dired-delete-file rm-disk-file dired-recursive-deletes))
2151 (stgit-capture-output nil
2152 (when rm-file
2153 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
2154 (when add-file
2155 (stgit-run-git "add" "--" add-file))
2156 (when co-file
2157 (let ((rev (when (or (eq file-status 'unmerged)
2158 (eq patch-name :index))
2159 '("HEAD"))))
2160 (apply #'stgit-run-git
2161 "checkout"
2162 `(,@rev "--" ,co-file)))))
2163 (stgit-reload)
2164 (stgit-goto-patch patch-name next-file))))
2166 (defun stgit-revert ()
2167 "Revert the change at point, which must be the index, the work
2168 tree, or a single change in either."
2169 (interactive)
2170 (stgit-assert-mode)
2171 (let ((patched-file (stgit-patched-file-at-point)))
2172 (if patched-file
2173 (stgit-revert-file)
2174 (let* ((patch-name (or (stgit-patch-name-at-point)
2175 (error "No patch or file at point")))
2176 (patch-desc (case patch-name
2177 (:index "index")
2178 (:work "work tree")
2179 (t (error (substitute-command-keys
2180 "Use \\[stgit-delete] to delete a patch"))))))
2181 (when (if (eq patch-name :work)
2182 (stgit-work-tree-empty-p)
2183 (stgit-index-empty-p))
2184 (error (format "There are no changes in the %s to revert"
2185 patch-desc)))
2186 (and (eq patch-name :index)
2187 (not (stgit-work-tree-empty-p))
2188 (error "Cannot revert index as work tree contains unstaged changes"))
2190 (when (yes-or-no-p (format "Revert all changes in the %s? "
2191 patch-desc))
2192 (if (eq patch-name :index)
2193 (stgit-run-git-silent "reset" "--hard" "-q")
2194 (stgit-run-git-silent "checkout" "--" "."))
2195 (stgit-save-excursion
2196 (stgit-refresh-index)
2197 (stgit-refresh-worktree)))))))
2199 (defun stgit-resolve-file ()
2200 "Resolve conflict in the file at point."
2201 (interactive)
2202 (stgit-assert-mode)
2203 (let* ((patched-file (stgit-patched-file-at-point))
2204 (patch (stgit-patch-at-point))
2205 (patch-name (and patch (stgit-patch->name patch)))
2206 (status (and patched-file (stgit-file->status patched-file))))
2208 (unless (memq patch-name '(:work :index))
2209 (error "No index or working tree file on this line"))
2211 (unless (eq status 'unmerged)
2212 (error "No conflict to resolve at the current line"))
2214 (stgit-capture-output nil
2215 (stgit-move-change-to-index (stgit-file->file patched-file)))
2217 (stgit-reload)))
2219 (defun stgit-push-or-pop-patches (do-push npatches)
2220 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
2221 NPATCHES patches, or all patches if NPATCHES is t."
2222 (stgit-assert-mode)
2223 (stgit-capture-output nil
2224 (apply 'stgit-run
2225 (if do-push "push" "pop")
2226 (if (eq npatches t)
2227 '("--all")
2228 (list "-n" npatches))))
2229 (stgit-reload)
2230 (stgit-refresh-git-status))
2232 (defun stgit-push-next (npatches)
2233 "Push the first unapplied patch.
2234 With numeric prefix argument, push that many patches."
2235 (interactive "p")
2236 (stgit-push-or-pop-patches t npatches))
2238 (defun stgit-pop-next (npatches)
2239 "Pop the topmost applied patch.
2240 With numeric prefix argument, pop that many patches.
2242 If NPATCHES is t, pop all patches."
2243 (interactive "p")
2244 (stgit-push-or-pop-patches nil npatches))
2246 (defun stgit-applied-patches (&optional only-patches)
2247 "Return a list of the applied patches.
2249 If ONLY-PATCHES is not nil, exclude index and work tree."
2250 (let ((states (if only-patches
2251 '(applied top)
2252 '(applied top index work)))
2253 result)
2254 (ewoc-map (lambda (patch)
2255 (when (memq (stgit-patch->status patch) states)
2256 (setq result (cons patch result)))
2257 nil)
2258 stgit-ewoc)
2259 result))
2261 (defun stgit-applied-patchsyms (&optional only-patches)
2262 "Return a list of the symbols of the applied patches.
2264 If ONLY-PATCHES is not nil, exclude index and work tree."
2265 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
2267 (defun stgit-push-or-pop ()
2268 "Push or pop the marked patches."
2269 (interactive)
2270 (stgit-assert-mode)
2271 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
2272 (applied-syms (stgit-applied-patchsyms t))
2273 (unapplied (set-difference patchsyms applied-syms)))
2274 (stgit-capture-output nil
2275 (apply 'stgit-run
2276 (if unapplied "push" "pop")
2277 "--"
2278 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
2279 (stgit-reload))
2281 (defun stgit-at-header-p ()
2282 "Return non-nil if point is in the header area above all patches."
2283 (not (previous-single-property-change (point) 'patch-data)))
2285 (defun stgit-at-footer-p ()
2286 "Return non-nil if point is in the footer area below all patches."
2287 (not (next-single-property-change (point) 'patch-data)))
2289 (defun stgit-goto-target ()
2290 "Return the goto target at point: a patchsym, :top,
2291 or :bottom."
2292 (let ((patch (stgit-patch-at-point)))
2293 (cond (patch
2294 (case (stgit-patch->status patch)
2295 ((work index) nil)
2296 ((committed) :bottom)
2297 (t (stgit-patch->name patch))))
2298 ((stgit-at-footer-p)
2299 :top)
2300 ((stgit-at-header-p)
2301 :bottom))))
2303 (defun stgit-goto ()
2304 "Go to the patch on the current line.
2306 Push or pop patches to make this patch topmost. Push or pop all
2307 patches if used on a line after or before all patches."
2308 (interactive)
2309 (stgit-assert-mode)
2310 (let ((patchsym (stgit-goto-target)))
2311 (unless patchsym
2312 (error "No patch to go to on this line"))
2313 (case patchsym
2314 (:top (stgit-push-or-pop-patches t t))
2315 (:bottom (stgit-push-or-pop-patches nil t))
2316 (t (stgit-capture-output nil
2317 (stgit-run "goto" "--" patchsym))
2318 (stgit-reload)))))
2320 (defun stgit-id (patchsym)
2321 "Return the git commit id for PATCHSYM.
2322 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
2323 (if (keywordp patchsym)
2324 patchsym
2325 (let ((result (with-output-to-string
2326 (stgit-run-silent "id" "--" patchsym))))
2327 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
2328 (error "Cannot find commit id for %s" patchsym))
2329 (match-string 1 result))))
2331 (defun stgit-whitespace-diff-arg (arg)
2332 (when (numberp arg)
2333 (cond ((> arg 4) "--ignore-all-space")
2334 ((> arg 1) "--ignore-space-change"))))
2336 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
2337 "Show the patch on the current line.
2339 UNMERGED-STAGE is the argument to `git-diff' that that selects
2340 which stage to diff against in the case of unmerged files."
2341 (let* ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
2342 (patch-name (stgit-patch-name-at-point t))
2343 (entry-type (get-text-property (point) 'entry-type))
2344 (diff-desc (case entry-type
2345 ('file "diff")
2346 ('patch "patch")
2347 (t (error "No patch or file at point")))))
2348 (stgit-show-task-message (concat "Showing " diff-desc)
2349 (stgit-capture-output (concat "*StGit " diff-desc "*")
2350 (case entry-type
2351 ('file
2352 (let* ((patched-file (stgit-patched-file-at-point))
2353 (patch-id (let ((id (stgit-id patch-name)))
2354 (if (and (eq id :index)
2355 (eq (stgit-file->status patched-file)
2356 'unmerged))
2357 :work
2358 id)))
2359 (args (append (and space-arg (list space-arg))
2360 (and (stgit-file->cr-from patched-file)
2361 (list (stgit-find-copies-harder-diff-arg)))
2362 (cond ((eq patch-id :index)
2363 '("--cached"))
2364 ((eq patch-id :work)
2365 (list unmerged-stage))
2367 (list (concat patch-id "^") patch-id)))
2368 (and (eq (stgit-file->status patched-file)
2369 'copy)
2370 '("--diff-filter=C"))
2371 '("--")
2372 (if (stgit-file->copy-or-rename patched-file)
2373 (list (stgit-file->cr-from patched-file)
2374 (stgit-file->cr-to patched-file))
2375 (list (stgit-file->file patched-file))))))
2376 (apply 'stgit-run-git "diff" args)))
2377 ('patch
2378 (let* ((patch-id (stgit-id patch-name)))
2379 (if (or (eq patch-id :index) (eq patch-id :work))
2380 (apply 'stgit-run-git "diff"
2381 (stgit-find-copies-harder-diff-arg)
2382 (append (and space-arg (list space-arg))
2383 (if (eq patch-id :index)
2384 '("--cached")
2385 (list unmerged-stage))))
2386 (let ((args (append '("show" "-O" "--patch-with-stat")
2387 `("-O" ,(stgit-find-copies-harder-diff-arg))
2388 (and space-arg (list "-O" space-arg))
2389 '("--")
2390 (list (stgit-patch-name-at-point)))))
2391 (apply 'stgit-run args))))))
2392 (with-current-buffer standard-output
2393 (goto-char (point-min))
2394 (diff-mode))))))
2396 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
2397 `(defun ,name (&optional ignore-whitespace)
2398 ,(format "Show the patch on the current line.
2400 %sWith a prefix argument, ignore whitespace. With a prefix argument
2401 greater than four (e.g., \\[universal-argument] \
2402 \\[universal-argument] \\[%s]), ignore all whitespace."
2403 (if unmerged-action
2404 (format "For unmerged files, %s.\n\n" unmerged-action)
2406 name)
2407 (interactive "p")
2408 (stgit-assert-mode)
2409 (stgit-show-patch ,diff-arg ignore-whitespace)))
2411 (stgit-define-diff stgit-diff
2412 "--ours" nil)
2413 (stgit-define-diff stgit-diff-ours
2414 "--ours"
2415 "diff against our branch")
2416 (stgit-define-diff stgit-diff-theirs
2417 "--theirs"
2418 "diff against their branch")
2419 (stgit-define-diff stgit-diff-base
2420 "--base"
2421 "diff against the merge base")
2422 (stgit-define-diff stgit-diff-combined
2423 "--cc"
2424 "show a combined diff")
2426 (defun stgit-diff-range (&optional ignore-whitespace)
2427 "Show diff for the range of patches between point and the marked patch.
2429 With a prefix argument, ignore whitespace. With a prefix argument
2430 greater than four (e.g., \\[universal-argument] \
2431 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
2432 (interactive "p")
2433 (stgit-assert-mode)
2434 (unless (= (length stgit-marked-patches) 1)
2435 (error "Need exactly one patch marked"))
2436 (let* ((patches (stgit-sort-patches
2437 (cons (stgit-patch-name-at-point t 'allow-committed)
2438 stgit-marked-patches)
2440 (first-patch (car patches))
2441 (second-patch (if (cdr patches) (cadr patches) first-patch))
2442 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
2443 (applied (stgit-applied-patchsyms t)))
2444 (unless (and (memq first-patch applied) (memq second-patch applied))
2445 (error "Can only show diff range for applied patches"))
2446 (stgit-capture-output (format "*StGit diff %s..%s*"
2447 first-patch second-patch)
2448 (apply 'stgit-run-git
2449 "diff" "--patch-with-stat"
2450 (stgit-find-copies-harder-diff-arg)
2451 (append (and whitespace-arg (list whitespace-arg))
2452 (list (format "%s^" (stgit-id first-patch))
2453 (stgit-id second-patch))))
2454 (with-current-buffer standard-output
2455 (goto-char (point-min))
2456 (diff-mode)))))
2458 (defun stgit-move-change-to-index (file &optional force)
2459 "Copies the work tree state of FILE to index, using git add or git rm.
2461 If FORCE is not nil, use --force."
2462 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
2463 '("add") '("rm" "-q"))))
2464 (stgit-capture-output "*git output*"
2465 (apply 'stgit-run-git (append op (and force '("--force"))
2466 '("--") (list file))))))
2468 (defun stgit-remove-change-from-index (file)
2469 "Unstages the change in FILE from the index"
2470 (stgit-capture-output "*git output*"
2471 (stgit-run-git "reset" "-q" "--" file)))
2473 (defun stgit-git-index-unmerged-p ()
2474 (let (result)
2475 (with-output-to-string
2476 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
2477 "--diff-filter=U"
2478 "--quiet" "HEAD")))))
2479 result))
2481 (defun stgit-assert-no-unmerged-changes ()
2482 "Signal an error if there are any unmerged changes in the index."
2483 (when (stgit-git-index-unmerged-p)
2484 (error (substitute-command-keys
2485 "Resolve unmerged changes with \\[stgit-resolve-file] first"))))
2487 (defun stgit-file-toggle-index ()
2488 "Move modified file in or out of the index.
2490 Leaves the point where it is, but moves the mark to where the
2491 file ended up. You can then jump to the file with \
2492 \\[exchange-point-and-mark]."
2493 (interactive)
2494 (stgit-assert-mode)
2495 (let* ((patched-file (or (stgit-patched-file-at-point)
2496 (error "No file on the current line")))
2497 (patched-status (stgit-file->status patched-file)))
2498 (when (eq patched-status 'unmerged)
2499 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
2500 (let* ((patch (stgit-patch-at-point))
2501 (patch-name (stgit-patch->name patch))
2502 (mark-file (if (eq patched-status 'rename)
2503 (stgit-file->cr-to patched-file)
2504 (stgit-file->file patched-file)))
2505 (point-file (if (eq patched-status 'rename)
2506 (stgit-file->cr-from patched-file)
2507 (stgit-neighbour-file))))
2509 (cond ((eq patch-name :work)
2510 (stgit-move-change-to-index (stgit-file->file patched-file)
2511 (eq patched-status 'ignore)))
2512 ((eq patch-name :index)
2513 (stgit-remove-change-from-index (stgit-file->file patched-file)))
2515 (error "Can only move files between working tree and index")))
2516 (stgit-save-excursion
2517 (stgit-refresh-worktree)
2518 (stgit-refresh-index))
2519 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
2520 (push-mark nil t t)
2521 (setq deactivate-mark t)
2522 (stgit-goto-patch patch-name point-file))))
2524 (defun stgit-toggle-index ()
2525 "Move change in or out of the index.
2527 Works on index and work tree, as well as files in either.
2529 Leaves the point where it is, but moves the mark to where the
2530 file ended up. You can then jump to the file with \
2531 \\[exchange-point-and-mark]."
2532 (interactive)
2533 (stgit-assert-mode)
2534 (if (stgit-patched-file-at-point)
2535 (stgit-file-toggle-index)
2536 (let ((patch-name (stgit-patch-name-at-point)))
2537 (unless (memq patch-name '(:index :work))
2538 (error "Can only move changes between working tree and index"))
2539 (stgit-assert-no-unmerged-changes)
2540 (if (if (eq patch-name :index)
2541 (stgit-index-empty-p)
2542 (stgit-work-tree-empty-p))
2543 (message "No changes to be moved")
2544 (stgit-capture-output nil
2545 (if (eq patch-name :work)
2546 (stgit-run-git "add" "--update")
2547 (stgit-run-git "reset" "--mixed" "-q")))
2548 (stgit-save-excursion
2549 (stgit-refresh-worktree)
2550 (stgit-refresh-index)))
2551 (stgit-goto-patch patch-name)
2552 (push-mark nil t t)
2553 (setq deactivate-mark t)
2554 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
2556 (defun stgit-edit ()
2557 "Edit the patch on the current line."
2558 (interactive)
2559 (stgit-assert-mode)
2560 (let ((patchsym (stgit-patch-name-at-point t t))
2561 (edit-buf (get-buffer-create "*StGit edit*"))
2562 (dir default-directory))
2563 (log-edit 'stgit-confirm-edit t nil edit-buf)
2564 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
2565 (setq default-directory dir)
2566 (let ((standard-output edit-buf))
2567 (save-excursion
2568 (stgit-run-silent "edit" "--save-template=-" "--" patchsym)))))
2570 (defun stgit-confirm-edit ()
2571 (interactive)
2572 (let ((file (make-temp-file "stgit-edit-")))
2573 (write-region (point-min) (point-max) file)
2574 (stgit-capture-output nil
2575 (stgit-run "edit" "-f" file "--" stgit-edit-patchsym))
2576 (with-current-buffer log-edit-parent-buffer
2577 (stgit-reload))))
2579 (defun stgit-new-here (add-sign)
2580 "Create a new patch before the patch at point, asking for a
2581 commit message.
2583 With a prefix argument, include a \"Signed-off-by:\" line at the
2584 end of the patch description.
2586 This works like `stgit-new' followed by `stgit-move'."
2587 (interactive "P")
2588 (stgit-assert-mode)
2589 (let ((patch (stgit-patch-at-point t)))
2590 (case (stgit-patch->status patch)
2591 ((index work) (stgit-new add-sign))
2592 ((applied top)
2593 (unless (and (stgit-index-empty-p)
2594 (stgit-work-tree-empty-p))
2595 (error "Index and worktree must not contain any changes"))
2596 (stgit-new add-sign nil (stgit-patch->name patch)))
2597 (t (error "Can only be used on applied patches")))))
2599 (defun stgit-new (add-sign &optional refresh sink-to)
2600 "Create a new patch, asking for a commit message.
2602 With a prefix argument, include a \"Signed-off-by:\" line at the
2603 end of the message.
2605 If REFRESH is non-nil, also refresh the patch after creating it.
2607 If SINK-TO is non-nil, sink the created patch to the patch with
2608 that name (a symbol)."
2609 (interactive "P")
2610 (stgit-assert-mode)
2611 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2612 (dir default-directory))
2613 (log-edit 'stgit-confirm-new t nil edit-buf)
2614 (setq default-directory dir)
2615 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2616 (set (make-local-variable 'stgit-sink-to) sink-to)
2617 (when add-sign
2618 (save-excursion
2619 (let ((standard-output (current-buffer)))
2620 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2622 (defun stgit-confirm-new ()
2623 (interactive)
2624 (let ((file (make-temp-file "stgit-edit-"))
2625 (refresh stgit-refresh-after-new)
2626 new-patch)
2627 (write-region (point-min) (point-max) file)
2628 (stgit-capture-output nil
2629 (stgit-run "new" "-f" file))
2631 (let ((top (with-output-to-string (stgit-run "top"))))
2632 (when (string-match "\\`\\(.+\\)" top)
2633 (setq new-patch (intern (match-string 1 top)))))
2635 (when stgit-sink-to
2636 (stgit-run "sink" "-t" stgit-sink-to))
2637 (with-current-buffer log-edit-parent-buffer
2638 (if refresh
2639 (stgit-refresh)
2640 (stgit-reload))
2641 (stgit-goto-patch new-patch))))
2643 (defun stgit-new-and-refresh (add-sign)
2644 "Create a new patch based on the current changes, asking for a
2645 commit message.
2647 With a prefix argument, include a \"Signed-off-by:\" line at the
2648 end of the patch.
2650 This works just like running `stgit-new' followed by `stgit-refresh'."
2651 (interactive "P")
2652 (stgit-assert-mode)
2653 (stgit-assert-no-unmerged-changes)
2654 (stgit-new add-sign t))
2656 (defun stgit-create-patch-name (description)
2657 "Create a patch name from a long description"
2658 (let ((patch ""))
2659 (while (> (length description) 0)
2660 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2661 (setq patch (downcase (concat patch
2662 (match-string 0 description))))
2663 (setq description (substring description (match-end 0))))
2664 ((string-match "\\` +" description)
2665 (setq patch (concat patch "-"))
2666 (setq description (substring description (match-end 0))))
2667 ((string-match "\\`[^a-zA-Z_-]+" description)
2668 (setq description (substring description (match-end 0))))))
2669 (cond ((= (length patch) 0)
2670 "patch")
2671 ((> (length patch) 20)
2672 (substring patch 0 20))
2673 (t patch))))
2675 (defun stgit-delete (patchsyms &optional spill-p)
2676 "Delete the patches in PATCHSYMS.
2677 Interactively, delete the marked patches, or the patch at point.
2679 With a prefix argument, or SPILL-P, spill the patch contents to
2680 the work tree and index."
2681 (interactive (list (stgit-patches-marked-or-at-point t t)
2682 current-prefix-arg))
2683 (stgit-assert-mode)
2684 (unless patchsyms
2685 (error "No patches to delete"))
2686 (when (memq :index patchsyms)
2687 (error "Cannot delete the index"))
2688 (when (memq :work patchsyms)
2689 (error "Cannot delete the work tree"))
2691 (let ((npatches (length patchsyms)))
2692 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2693 npatches
2694 (if (= 1 npatches) "" "es")
2695 (if spill-p
2696 " (spilling contents to index)"
2697 "")))
2698 (let ((args (append (when spill-p '("--spill"))
2699 '("--")
2700 patchsyms)))
2701 (stgit-capture-output nil
2702 (apply 'stgit-run "delete" args))
2703 (stgit-reload)))))
2705 (defun stgit-move-patches-target ()
2706 "Return the patchsym indicating a target patch for
2707 `stgit-move-patches'.
2709 This is either the first unmarked patch at or after point, or one
2710 of :top and :bottom if the point is after or before the applied
2711 patches."
2713 (save-excursion
2714 (let (result)
2715 (while (not result)
2716 (let ((patchsym (stgit-patch-name-at-point)))
2717 (cond ((memq patchsym '(:work :index)) (setq result :top))
2718 (patchsym (if (memq patchsym stgit-marked-patches)
2719 (stgit-next-patch)
2720 (setq result patchsym)))
2721 ((re-search-backward "^>" nil t) (setq result :top))
2722 (t (setq result :bottom)))))
2723 result)))
2725 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2726 "Returns the list of patches in PATCHSYMS sorted according to
2727 their position in the patch series, bottommost first.
2729 PATCHSYMS must not contain duplicate entries, unless
2730 ALLOW-DUPLICATES is not nil."
2731 (let (sorted-patchsyms)
2732 (ewoc-map #'(lambda (patch)
2733 (let ((name (stgit-patch->name patch)))
2734 (when (memq name patchsyms)
2735 (setq sorted-patchsyms (cons name sorted-patchsyms))))
2736 nil)
2737 stgit-ewoc)
2738 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2740 (unless allow-duplicates
2741 (unless (= (length patchsyms) (length sorted-patchsyms))
2742 (error "Internal error")))
2744 sorted-patchsyms))
2746 (defun stgit-move-patches (patchsyms target-patch)
2747 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2748 If TARGET-PATCH is :bottom or :top, move the patches to the
2749 bottom or top of the stack, respectively.
2751 Interactively, move the marked patches to where the point is."
2752 (interactive (list stgit-marked-patches
2753 (stgit-move-patches-target)))
2754 (stgit-assert-mode)
2755 (unless patchsyms
2756 (error "Need at least one patch to move"))
2758 (unless target-patch
2759 (error "Point not at a patch"))
2761 ;; need to have patchsyms sorted by position in the stack
2762 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2763 (stgit-capture-output nil
2764 (if (eq target-patch :top)
2765 (apply 'stgit-run "float" "--" sorted-patchsyms)
2766 (apply 'stgit-run
2767 "sink"
2768 (append (unless (eq target-patch :bottom)
2769 (list "--to" target-patch))
2770 '("--")
2771 sorted-patchsyms)))))
2772 (stgit-reload))
2774 (defun stgit-squash (patchsyms)
2775 "Squash the patches in PATCHSYMS.
2776 Interactively, squash the marked patches.
2778 Unless there are any conflicts, the patches will be merged into
2779 one patch, which will occupy the same spot in the series as the
2780 deepest patch had before the squash."
2781 (interactive (list stgit-marked-patches))
2782 (stgit-assert-mode)
2783 (when (< (length patchsyms) 2)
2784 (error "Need at least two patches to squash"))
2785 (let ((stgit-buffer (current-buffer))
2786 (edit-buf (get-buffer-create "*StGit edit*"))
2787 (dir default-directory)
2788 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2789 (log-edit 'stgit-confirm-squash t nil edit-buf)
2790 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2791 (setq default-directory dir)
2792 (let ((result (let ((standard-output edit-buf))
2793 (save-excursion
2794 (apply 'stgit-run-silent "squash"
2795 "--save-template=-" "--" sorted-patchsyms)))))
2797 ;; stg squash may have reordered the patches or caused conflicts
2798 (with-current-buffer stgit-buffer
2799 (stgit-reload))
2801 (unless (eq 0 result)
2802 (fundamental-mode)
2803 (rename-buffer "*StGit error*")
2804 (resize-temp-buffer-window)
2805 (switch-to-buffer-other-window stgit-buffer)
2806 (error "stg squash failed")))))
2808 (defun stgit-confirm-squash ()
2809 (interactive)
2810 (let ((file (make-temp-file "stgit-edit-")))
2811 (write-region (point-min) (point-max) file)
2812 (stgit-capture-output nil
2813 (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms))
2814 (with-current-buffer log-edit-parent-buffer
2815 (stgit-clear-marks)
2816 ;; Go to first marked patch and stay there
2817 (goto-char (point-min))
2818 (re-search-forward (concat "^[>+-]\\*") nil t)
2819 (move-to-column goal-column)
2820 (let ((pos (point)))
2821 (stgit-reload)
2822 (goto-char pos)))))
2824 (defun stgit-help ()
2825 "Display help for the StGit mode."
2826 (interactive)
2827 (describe-function 'stgit-mode))
2829 (defun stgit-execute-process-sentinel (process sentinel)
2830 (let (old-sentinel stgit-buf)
2831 (with-current-buffer (process-buffer process)
2832 (setq old-sentinel old-process-sentinel
2833 stgit-buf stgit-buffer))
2834 (and (memq (process-status process) '(exit signal))
2835 (buffer-live-p stgit-buf)
2836 (with-current-buffer stgit-buf
2837 (stgit-reload)))
2838 (funcall old-sentinel process sentinel)))
2840 (defun stgit-execute-process-filter (process output)
2841 (with-current-buffer (process-buffer process)
2842 (let* ((old-point (point))
2843 (pmark (process-mark process))
2844 (insert-at (marker-position pmark))
2845 (at-pmark (= insert-at old-point)))
2846 (goto-char insert-at)
2847 (insert-before-markers output)
2848 (comint-carriage-motion insert-at (point))
2849 (set-marker pmark (point))
2850 (unless at-pmark
2851 (goto-char old-point)))))
2853 (defun stgit-execute (&optional git-mode)
2854 "Prompt for an stg command to execute in a shell.
2856 The names of any marked patches or the patch at point are
2857 inserted in the command to be executed.
2859 With a prefix argument, or if GIT-MODE is non-nil, insert SHA1
2860 sums of the marked patches instead, and prompt for a git command.
2862 If the command ends in an ampersand, run it asynchronously.
2864 When the command has finished, reload the stgit buffer."
2865 (interactive "P")
2866 (stgit-assert-mode)
2867 (let* ((patches (stgit-sort-patches
2868 (stgit-patches-marked-or-at-point nil 'allow-committed)))
2869 (patch-names (mapcar 'symbol-name patches))
2870 (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names))
2871 (program (if git-mode stgit-git-program stgit-stg-program))
2872 (defaultcmd (concat program
2874 (and patch-names " ")
2875 (and hyphens patch-names "-- ")
2876 (mapconcat (if git-mode 'stgit-id 'identity)
2877 patch-names " ")))
2878 (cmd (read-from-minibuffer "Shell command: "
2879 (cons defaultcmd (+ (length program) 2))
2880 nil nil 'shell-command-history))
2881 (async (string-match "&[ \t]*\\'" cmd))
2882 (buffer (get-buffer-create
2883 (if async
2884 "*Async Shell Command*"
2885 "*Shell Command Output*"))))
2886 ;; cannot use minibuffer as stgit-reload would overwrite it; if we
2887 ;; show the buffer, shell-command will not use the minibuffer
2888 (display-buffer buffer)
2889 (shell-command cmd)
2890 (if async
2891 (let ((old-buffer (current-buffer)))
2892 (with-current-buffer buffer
2893 (let ((process (get-buffer-process buffer)))
2894 (set (make-local-variable 'old-process-sentinel)
2895 (process-sentinel process))
2896 (set (make-local-variable 'stgit-buffer)
2897 old-buffer)
2898 (set-process-filter process 'stgit-execute-process-filter)
2899 (set-process-sentinel process 'stgit-execute-process-sentinel))))
2900 (with-current-buffer buffer
2901 (comint-carriage-motion (point-min) (point-max)))
2902 (shrink-window-if-larger-than-buffer (get-buffer-window buffer))
2903 (stgit-reload))))
2905 (defun stgit-undo-or-redo (redo hard)
2906 "Run stg undo or, if REDO is non-nil, stg redo.
2908 If HARD is non-nil, use the --hard flag."
2909 (stgit-assert-mode)
2910 (let ((cmd (if redo "redo" "undo")))
2911 (stgit-capture-output nil
2912 (if arg
2913 (when (or (and (stgit-index-empty-p)
2914 (stgit-work-tree-empty-p))
2915 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2916 cmd)))
2917 (stgit-run cmd "--hard"))
2918 (stgit-run cmd))))
2919 (stgit-reload))
2921 (defun stgit-undo (&optional arg)
2922 "Run stg undo.
2923 With prefix argument, run it with the --hard flag.
2925 See also `stgit-redo'."
2926 (interactive "P")
2927 (stgit-undo-or-redo nil arg))
2929 (defun stgit-redo (&optional arg)
2930 "Run stg redo.
2931 With prefix argument, run it with the --hard flag.
2933 See also `stgit-undo'."
2934 (interactive "P")
2935 (stgit-undo-or-redo t arg))
2937 (defun stgit-refresh (&optional arg)
2938 "Run stg refresh.
2939 If the index contains any changes, only refresh from index.
2941 With prefix argument, refresh the marked patch or the patch under point."
2942 (interactive "P")
2943 (stgit-assert-mode)
2944 (stgit-assert-no-unmerged-changes)
2945 (let ((patchargs (if arg
2946 (let ((patches (stgit-patches-marked-or-at-point t t)))
2947 (when (> (length patches) 1)
2948 (error "Too many patches marked"))
2949 (cons "-p" patches))
2950 nil)))
2951 (unless (stgit-index-empty-p)
2952 (setq patchargs (cons "--index" patchargs)))
2953 (stgit-capture-output nil
2954 (apply 'stgit-run "refresh" patchargs))
2955 (stgit-refresh-git-status))
2956 (stgit-reload))
2958 (defvar stgit-show-worktree nil
2959 "If nil, inhibit showing work tree and index in the stgit buffer.
2961 See also `stgit-show-worktree-mode'.")
2963 (defvar stgit-show-ignored nil
2964 "If nil, inhibit showing files ignored by git.")
2966 (defvar stgit-show-unknown nil
2967 "If nil, inhibit showing files not registered with git.")
2969 (defvar stgit-show-patch-names t
2970 "If nil, inhibit showing patch names.")
2972 (defvar stgit-show-committed nil
2973 "If nil, inhibit showing recent commits.")
2975 (defvar stgit-show-svn nil
2976 "If nil, inhibit showing git svn information.")
2978 (defvar stgit-committed-count nil
2979 "The number of recent commits to show.")
2981 (defmacro stgit-define-toggle-view (sym desc help)
2982 (declare (indent 1) (debug (symbolp stringp stringp)))
2983 (let* ((name (symbol-name sym))
2984 (fun (intern (concat "stgit-toggle-" name)))
2985 (flag (intern (concat "stgit-show-" name))))
2986 `(progn
2987 ;; make help-follow find the correct function
2988 (put (quote ,fun) 'definition-name 'stgit-define-toggle-view)
2989 (defun ,fun (&optional arg)
2990 ,help
2991 (interactive "P")
2992 (stgit-assert-mode)
2993 (setq ,flag (if arg
2994 (> (prefix-numeric-value arg) 0)
2995 (not ,flag)))
2996 (stgit-reload (concat (if ,flag "Showing" "Hiding") " " ,desc))))))
2998 (stgit-define-toggle-view worktree
2999 "work tree and index"
3000 "Toggle the visibility of the work tree.
3001 With ARG, show the work tree if ARG is positive.
3003 Its initial setting is controlled by `stgit-default-show-worktree'.
3005 `stgit-show-worktree-mode' controls where on screen the index and
3006 work tree will show up.")
3008 (stgit-define-toggle-view ignored
3009 "ignored files"
3010 "Toggle the visibility of files ignored by git in the work
3011 tree. With ARG, show these files if ARG is positive.
3013 Its initial setting is controlled by `stgit-default-show-ignored'.
3015 Use \\[stgit-toggle-worktree] to show the work tree.")
3017 (stgit-define-toggle-view unknown
3018 "unknown files"
3019 "Toggle the visibility of files not registered with git in the
3020 work tree. With ARG, show these files if ARG is positive.
3022 Its initial setting is controlled by `stgit-default-show-unknown'.
3024 Use \\[stgit-toggle-worktree] to show the work tree.")
3026 (stgit-define-toggle-view patch-names
3027 "patch names"
3028 "Toggle the visibility of patch names. With ARG, show patch names
3029 if ARG is positive.
3031 The initial setting is controlled by `stgit-default-show-patch-names'.")
3033 (stgit-define-toggle-view svn
3034 "subversion revisions"
3035 "Toggle showing subversion information from git svn. With ARG,
3036 show svn information if ARG is positive.
3038 The initial setting is controlled by `stgit-default-show-svn'.")
3040 (defun stgit-toggle-committed (&optional arg)
3041 "Toggle the visibility of historical git commits.
3042 With ARG, set the number of commits to show to ARG, and disable
3043 them if ARG is zero.
3045 The initial setting is controlled by `stgit-default-show-committed'."
3046 (interactive "P")
3047 (stgit-assert-mode)
3048 (if (null arg)
3049 (setq stgit-show-committed (not stgit-show-committed))
3050 (let ((n (prefix-numeric-value arg)))
3051 (setq stgit-show-committed (> n 0))
3052 (setq stgit-committed-count n)))
3053 (stgit-reload (format "%s historical commits"
3054 (if (and stgit-show-committed
3055 (> stgit-committed-count 0))
3056 "Showing"
3057 "Hiding"))))
3059 (provide 'stgit)