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