stgit.el: Add a few links to some customization variables
[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 (base (stgit-id "{base}"))
512 (range (format "%s~%d..%s" base stgit-committed-count 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 range)))
522 (goto-char (point-min))
523 (if (not (zerop exit-status))
524 (message "Failed to run git log")
525 (while (not (eobp))
526 (unless (looking-at
527 "\\([0-9a-f]+\\)\\(\\.\\.\\.\\)? \\(.*\\)")
528 (error "Syntax error in output from git log"))
529 (let* ((state 'committed)
530 (name (match-string 1))
531 (desc (match-string 3))
532 (empty nil))
533
534 (when show-svn
535 (let ((svn-rev (stgit-svn-find-rev name svn-hash)))
536 (when svn-rev
537 (setq desc (format "(r%s) %s" svn-rev desc)))))
538
539 (and commit-abbrev
540 (< commit-abbrev (length name))
541 (setq name (substring name 0 commit-abbrev)))
542
543 (setq name (intern name))
544
545 (setq all-patchsyms (cons name all-patchsyms))
546 (ewoc-enter-last ewoc
547 (make-stgit-patch
548 :status state
549 :name name
550 :desc desc
551 :empty empty)))
552 (forward-line 1)))))))
553 (let ((inserted-index (not stgit-show-worktree))
554 index-node
555 worktree-node)
556 (with-temp-buffer
557 (let* ((standard-output (current-buffer))
558 (exit-status (stgit-run-silent "series"
559 "--description" "--empty")))
560 (goto-char (point-min))
561 (if (not (zerop exit-status))
562 (cond ((looking-at "stg series: \\(.*\\)")
563 (setq inserted-index t)
564 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
565 (substitute-command-keys
566 "-- not initialized; run \\[stgit-init]")))
567 ((looking-at ".*")
568 (error "Error running stg: %s"
569 (match-string 0))))
570 (while (not (eobp))
571 (unless (looking-at
572 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
573 (error "Syntax error in output from stg series"))
574 (let* ((state-str (match-string 2))
575 (state (cond ((string= state-str ">") 'top)
576 ((string= state-str "+") 'applied)
577 ((string= state-str "-") 'unapplied)))
578 (name (intern (match-string 4)))
579 (desc (match-string 5))
580 (empty (string= (match-string 1) "0")))
581 (unless inserted-index
582 (when (or (eq stgit-show-worktree-mode 'top)
583 (and (eq stgit-show-worktree-mode 'center)
584 (eq state 'unapplied)))
585 (setq inserted-index t)
586 (stgit-run-series-insert-index ewoc)))
587 (setq all-patchsyms (cons name all-patchsyms))
588 (ewoc-enter-last ewoc
589 (make-stgit-patch
590 :status state
591 :name name
592 :desc desc
593 :empty empty)))
594 (forward-line 1)))))
595 (unless inserted-index
596 (stgit-run-series-insert-index ewoc))
597 (setq stgit-index-node index-node
598 stgit-worktree-node worktree-node
599 stgit-marked-patches (intersection stgit-marked-patches
600 all-patchsyms)))))
601
602 (defun stgit-current-branch ()
603 "Return the name of the current branch."
604 (substring (with-output-to-string
605 (stgit-run-silent "branch"))
606 0 -1))
607
608 (defun stgit-reload (&optional description)
609 "Update the contents of the StGit buffer.
610
611 If DESCRIPTION is non-nil, it is displayed as a status message
612 during the operation."
613 (interactive)
614 (stgit-assert-mode)
615 (when description
616 (message "%s..." description))
617 (let ((inhibit-read-only t)
618 (curline (line-number-at-pos))
619 (curpatch (stgit-patch-name-at-point))
620 (curfile (stgit-patched-file-at-point))
621 (stgit-inhibit-messages description))
622 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
623 (ewoc-set-hf stgit-ewoc
624 (concat "Branch: "
625 (propertize (stgit-current-branch)
626 'face 'stgit-branch-name-face)
627 "\n\n")
628 (if stgit-show-worktree
629 "--"
630 (propertize
631 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
632 shows the working tree\n")
633 'face 'stgit-description-face)))
634 (stgit-run-series stgit-ewoc)
635 (unless (and curpatch
636 (stgit-goto-patch curpatch
637 (and curfile (stgit-file->file curfile))))
638 (goto-char (point-min))
639 (forward-line (1- curline))
640 (move-to-column (stgit-goal-column)))
641 (stgit-refresh-git-status))
642 (when description
643 (message "%s...done" description)))
644
645 (defconst stgit-file-status-code-strings
646 (mapcar (lambda (arg)
647 (cons (car arg)
648 (propertize (cadr arg) 'face (car (cddr arg)))))
649 '((add "Added" stgit-modified-file-face)
650 (copy "Copied" stgit-modified-file-face)
651 (delete "Deleted" stgit-modified-file-face)
652 (modify "Modified" stgit-modified-file-face)
653 (rename "Renamed" stgit-modified-file-face)
654 (mode-change "Mode change" stgit-modified-file-face)
655 (unmerged "Unmerged" stgit-unmerged-file-face)
656 (unknown "Unknown" stgit-unknown-file-face)
657 (ignore "Ignored" stgit-ignored-file-face)))
658 "Alist of code symbols to description strings")
659
660 (defconst stgit-patch-status-face-alist
661 '((applied . stgit-applied-patch-face)
662 (top . stgit-top-patch-face)
663 (unapplied . stgit-unapplied-patch-face)
664 (committed . stgit-committed-patch-face)
665 (index . stgit-index-work-tree-title-face)
666 (work . stgit-index-work-tree-title-face))
667 "Alist of face to use for a given patch status")
668
669 (defun stgit-file-status-code-as-string (file)
670 "Return stgit status code for FILE as a string"
671 (let* ((code (assq (stgit-file->status file)
672 stgit-file-status-code-strings))
673 (score (stgit-file->cr-score file)))
674 (when code
675 (if (and score (/= score 100))
676 (format "%s %s" (cdr code)
677 (propertize (format "%d%%" score)
678 'face 'stgit-description-face))
679 (cdr code)))))
680
681 (defun stgit-file-status-code (str &optional score)
682 "Return stgit status code from git status string"
683 (let ((code (assoc str '(("A" . add)
684 ("C" . copy)
685 ("D" . delete)
686 ("I" . ignore)
687 ("M" . modify)
688 ("R" . rename)
689 ("T" . mode-change)
690 ("U" . unmerged)
691 ("X" . unknown)))))
692 (setq code (if code (cdr code) 'unknown))
693 (when (stringp score)
694 (if (> (length score) 0)
695 (setq score (string-to-number score))
696 (setq score nil)))
697 (if score (cons code score) code)))
698
699 (defconst stgit-file-type-strings
700 '((#o100 . "file")
701 (#o120 . "symlink")
702 (#o160 . "subproject"))
703 "Alist of names of file types")
704
705 (defun stgit-file-type-string (type)
706 "Return string describing file type TYPE (the high bits of file permission).
707 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
708 (let ((type-str (assoc type stgit-file-type-strings)))
709 (or (and type-str (cdr type-str))
710 (format "unknown type %o" type))))
711
712 (defun stgit-file-type-change-string (old-perm new-perm)
713 "Return string describing file type change from OLD-PERM to NEW-PERM.
714 Cf. `stgit-file-type-string'."
715 (let ((old-type (lsh old-perm -9))
716 (new-type (lsh new-perm -9)))
717 (cond ((= old-type new-type) "")
718 ((zerop new-type) "")
719 ((zerop old-type)
720 (if (= new-type #o100)
721 ""
722 (format "(%s)" (stgit-file-type-string new-type))))
723 (t (format "(%s -> %s)"
724 (stgit-file-type-string old-type)
725 (stgit-file-type-string new-type))))))
726
727 (defun stgit-file-mode-change-string (old-perm new-perm)
728 "Return string describing file mode change from OLD-PERM to NEW-PERM.
729 Cf. `stgit-file-type-change-string'."
730 (setq old-perm (logand old-perm #o777)
731 new-perm (logand new-perm #o777))
732 (if (or (= old-perm new-perm)
733 (zerop old-perm)
734 (zerop new-perm))
735 ""
736 (let* ((modified (logxor old-perm new-perm))
737 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
738 (cond ((zerop modified) "")
739 ((and (zerop not-x-modified)
740 (or (and (eq #o111 (logand old-perm #o111))
741 (propertize "-x" 'face 'stgit-file-permission-face))
742 (and (eq #o111 (logand new-perm #o111))
743 (propertize "+x" 'face
744 'stgit-file-permission-face)))))
745 (t (concat (propertize (format "%o" old-perm)
746 'face 'stgit-file-permission-face)
747 (propertize " -> "
748 'face 'stgit-description-face)
749 (propertize (format "%o" new-perm)
750 'face 'stgit-file-permission-face)))))))
751
752 (defstruct (stgit-file
753 (:conc-name stgit-file->))
754 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
755
756 (defun stgit-describe-copy-or-rename (file)
757 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
758 from to common-head common-tail)
759
760 (when stgit-abbreviate-copies-and-renames
761 (setq from (split-string (stgit-file->cr-from file) "/")
762 to (split-string (stgit-file->cr-to file) "/"))
763
764 (while (and from to (cdr from) (cdr to)
765 (string-equal (car from) (car to)))
766 (setq common-head (cons (car from) common-head)
767 from (cdr from)
768 to (cdr to)))
769 (setq common-head (nreverse common-head)
770 from (nreverse from)
771 to (nreverse to))
772 (while (and from to (cdr from) (cdr to)
773 (string-equal (car from) (car to)))
774 (setq common-tail (cons (car from) common-tail)
775 from (cdr from)
776 to (cdr to)))
777 (setq from (nreverse from)
778 to (nreverse to)))
779
780 (if (or common-head common-tail)
781 (concat (if common-head
782 (mapconcat #'identity common-head "/")
783 "")
784 (if common-head "/" "")
785 (propertize "{" 'face 'stgit-description-face)
786 (mapconcat #'identity from "/")
787 arrow
788 (mapconcat #'identity to "/")
789 (propertize "}" 'face 'stgit-description-face)
790 (if common-tail "/" "")
791 (if common-tail
792 (mapconcat #'identity common-tail "/")
793 ""))
794 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
795
796 (defun stgit-file-pp (file)
797 (let ((start (point))
798 (spec (format-spec-make
799 ?s (stgit-file-status-code-as-string file)
800 ?m (stgit-file-mode-change-string
801 (stgit-file->old-perm file)
802 (stgit-file->new-perm file))
803 ?n (if (stgit-file->copy-or-rename file)
804 (stgit-describe-copy-or-rename file)
805 (stgit-file->file file))
806 ?c (propertize (stgit-file-type-change-string
807 (stgit-file->old-perm file)
808 (stgit-file->new-perm file))
809 'face 'stgit-description-face))))
810 (stgit-insert-without-trailing-whitespace
811 (format-spec stgit-file-line-format spec))
812 (add-text-properties start (point)
813 (list 'entry-type 'file
814 'file-data file))))
815
816 (defun stgit-find-copies-harder-diff-arg ()
817 "Return the flag to use with `git-diff' depending on the
818 `stgit-find-copies-harder' flag."
819 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
820
821 (defun stgit-insert-ls-files (args file-flag)
822 (let ((start (point)))
823 (apply 'stgit-run-git
824 (append '("ls-files" "--exclude-standard" "-z") args))
825 (goto-char start)
826 (while (looking-at "\\([^\0]*\\)\0")
827 (let ((name-len (- (match-end 0) (match-beginning 0))))
828 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
829 (forward-char name-len)))))
830
831 (defun stgit-process-files (callback)
832 (goto-char (point-min))
833 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
834 (goto-char (match-end 0)))
835 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
836 (let ((old-perm (string-to-number (match-string 1) 8))
837 (new-perm (string-to-number (match-string 2) 8)))
838 (goto-char (match-end 0))
839 (let ((file
840 (cond ((looking-at
841 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
842 (let* ((patch-status (stgit-patch->status patch))
843 (file-subexp (if (eq patch-status 'unapplied)
844 3
845 4))
846 (file (match-string file-subexp)))
847 (make-stgit-file
848 :old-perm old-perm
849 :new-perm new-perm
850 :copy-or-rename t
851 :cr-score (string-to-number (match-string 2))
852 :cr-from (match-string 3)
853 :cr-to (match-string 4)
854 :status (stgit-file-status-code
855 (match-string 1))
856 :file file)))
857 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
858 (make-stgit-file
859 :old-perm old-perm
860 :new-perm new-perm
861 :copy-or-rename nil
862 :cr-score nil
863 :cr-from nil
864 :cr-to nil
865 :status (stgit-file-status-code
866 (match-string 1))
867 :file (match-string 2))))))
868 (goto-char (match-end 0))
869 (funcall callback file)))))
870
871
872 (defun stgit-insert-patch-files (patch)
873 "Expand (show modification of) the patch PATCH after the line
874 at point."
875 (let* ((patchsym (stgit-patch->name patch))
876 (end (point-marker))
877 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
878 (ewoc (ewoc-create #'stgit-file-pp nil nil t))
879 (show-ignored stgit-show-ignored)
880 (show-unknown stgit-show-unknown))
881 (set-marker-insertion-type end t)
882 (setf (stgit-patch->files-ewoc patch) ewoc)
883 (with-temp-buffer
884 (let ((standard-output (current-buffer)))
885 (apply 'stgit-run-git
886 (cond ((eq patchsym :work)
887 (let (standard-output)
888 (stgit-run-git "update-index" "--refresh"))
889 `("diff-files" "-0" ,@args))
890 ((eq patchsym :index)
891 `("diff-index" ,@args "--cached" "HEAD"))
892 (t
893 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
894
895 (when (and (eq patchsym :work))
896 (when show-ignored
897 (stgit-insert-ls-files '("--ignored" "--others") "I"))
898 (when show-unknown
899 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
900 "--others")
901 "X"))
902 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
903 (point-min) (point-max)))
904
905 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
906
907 (unless (ewoc-nth ewoc 0)
908 (ewoc-set-hf ewoc ""
909 (concat " "
910 (propertize "<no files>"
911 'face 'stgit-description-face)
912 "\n")))))
913 (goto-char end)))
914
915 (defun stgit-find-file (&optional other-window)
916 (let* ((file (or (stgit-patched-file-at-point)
917 (error "No file at point")))
918 (filename (expand-file-name (stgit-file->file file))))
919 (unless (file-exists-p filename)
920 (error "File does not exist"))
921 (funcall (if other-window 'find-file-other-window 'find-file)
922 filename)
923 (when (eq (stgit-file->status file) 'unmerged)
924 (smerge-mode 1))))
925
926 (defun stgit-expand (&optional patches collapse)
927 "Show the contents of marked patches, or the patch at point.
928
929 See also `stgit-collapse'.
930
931 Non-interactively, operate on PATCHES, and collapse instead of
932 expand if COLLAPSE is not nil."
933 (interactive (list (stgit-patches-marked-or-at-point t)))
934 (stgit-assert-mode)
935 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
936 patches stgit-expanded-patches)))
937 (setq stgit-expanded-patches
938 (if collapse
939 (set-difference stgit-expanded-patches patches-diff)
940 (append stgit-expanded-patches patches-diff)))
941 (ewoc-map #'(lambda (patch)
942 (memq (stgit-patch->name patch) patches-diff))
943 stgit-ewoc))
944 (move-to-column (stgit-goal-column)))
945
946 (defun stgit-collapse (&optional patches)
947 "Hide the contents of marked patches, or the patch at point.
948
949 See also `stgit-expand'."
950 (interactive (list (stgit-patches-marked-or-at-point t)))
951 (stgit-assert-mode)
952 (stgit-expand patches t))
953
954 (defun stgit-select-patch ()
955 (let ((patchname (stgit-patch-name-at-point)))
956 (stgit-expand (list patchname)
957 (memq patchname stgit-expanded-patches))))
958
959 (defun stgit-expand-directory (file)
960 (let* ((patch (stgit-patch-at-point))
961 (ewoc (stgit-patch->files-ewoc patch))
962 (node (ewoc-locate ewoc))
963 (filename (stgit-file->file file))
964 (start (make-marker))
965 (end (make-marker)))
966
967 (save-excursion
968 (forward-line 1)
969 (set-marker start (point))
970 (set-marker end (point))
971 (set-marker-insertion-type end t))
972
973 (assert (string-match "/$" filename))
974 ;; remove trailing "/"
975 (setf (stgit-file->file file) (substring filename 0 -1))
976 (ewoc-invalidate ewoc node)
977
978 (with-temp-buffer
979 (let ((standard-output (current-buffer)))
980 (stgit-insert-ls-files (list "--directory" "--others"
981 "--no-empty-directory" "--"
982 filename)
983 "X")
984 (stgit-process-files (lambda (f)
985 (setq node (ewoc-enter-after ewoc node f))))))
986
987 (move-to-column (stgit-goal-column))
988
989 (let ((inhibit-read-only t))
990 (put-text-property start end 'patch-data patch))))
991
992 (defun stgit-select-file ()
993 (let* ((file (or (stgit-patched-file-at-point)
994 (error "No file at point")))
995 (filename (stgit-file->file file)))
996 (if (string-match "/$" filename)
997 (stgit-expand-directory file)
998 (stgit-find-file))))
999
1000 (defun stgit-select ()
1001 "With point on a patch, toggle showing files in the patch.
1002
1003 With point on a file, open the associated file. Opens the target
1004 file for (applied) copies and renames."
1005 (interactive)
1006 (stgit-assert-mode)
1007 (case (get-text-property (point) 'entry-type)
1008 ('patch
1009 (stgit-select-patch))
1010 ('file
1011 (stgit-select-file))
1012 (t
1013 (error "No patch or file on line"))))
1014
1015 (defun stgit-find-file-other-window ()
1016 "Open file at point in other window"
1017 (interactive)
1018 (stgit-assert-mode)
1019 (stgit-find-file t))
1020
1021 (defun stgit-find-file-merge ()
1022 "Open file at point and merge it using `smerge-ediff'."
1023 (interactive)
1024 (stgit-assert-mode)
1025 (stgit-find-file t)
1026 (smerge-ediff))
1027
1028 (defun stgit-quit ()
1029 "Hide the stgit buffer."
1030 (interactive)
1031 (stgit-assert-mode)
1032 (bury-buffer))
1033
1034 (defun stgit-git-status ()
1035 "Show status using `git-status'."
1036 (interactive)
1037 (stgit-assert-mode)
1038 (unless (fboundp 'git-status)
1039 (error "The stgit-git-status command requires git-status"))
1040 (let ((dir default-directory))
1041 (save-selected-window
1042 (pop-to-buffer nil)
1043 (git-status dir))))
1044
1045 (defun stgit-goal-column ()
1046 "Return goal column for the current line"
1047 (case (get-text-property (point) 'entry-type)
1048 ('patch 2)
1049 ('file 4)
1050 (t 0)))
1051
1052 (defun stgit-next-line (&optional arg)
1053 "Move cursor vertically down ARG lines"
1054 (interactive "p")
1055 (stgit-assert-mode)
1056 (next-line arg)
1057 (move-to-column (stgit-goal-column)))
1058
1059 (defun stgit-previous-line (&optional arg)
1060 "Move cursor vertically up ARG lines"
1061 (interactive "p")
1062 (stgit-assert-mode)
1063 (previous-line arg)
1064 (move-to-column (stgit-goal-column)))
1065
1066 (defun stgit-next-patch (&optional arg)
1067 "Move cursor down ARG patches."
1068 (interactive "p")
1069 (stgit-assert-mode)
1070 (ewoc-goto-next stgit-ewoc (or arg 1))
1071 (move-to-column goal-column))
1072
1073 (defun stgit-previous-patch (&optional arg)
1074 "Move cursor up ARG patches."
1075 (interactive "p")
1076 (stgit-assert-mode)
1077 (ewoc-goto-prev stgit-ewoc (or arg 1))
1078 (move-to-column goal-column))
1079
1080 (defvar stgit-mode-hook nil
1081 "Run after `stgit-mode' is setup.")
1082
1083 (defvar stgit-mode-map nil
1084 "Keymap for StGit major mode.")
1085
1086 (unless stgit-mode-map
1087 (let ((diff-map (make-sparse-keymap))
1088 (toggle-map (make-sparse-keymap)))
1089 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
1090 '(("b" . stgit-diff-base)
1091 ("c" . stgit-diff-combined)
1092 ("m" . stgit-find-file-merge)
1093 ("o" . stgit-diff-ours)
1094 ("r" . stgit-diff-range)
1095 ("t" . stgit-diff-theirs)))
1096 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
1097 '(("n" . stgit-toggle-patch-names)
1098 ("t" . stgit-toggle-worktree)
1099 ("h" . stgit-toggle-committed)
1100 ("i" . stgit-toggle-ignored)
1101 ("u" . stgit-toggle-unknown)
1102 ("s" . stgit-toggle-svn)))
1103 (setq stgit-mode-map (make-keymap))
1104 (suppress-keymap stgit-mode-map)
1105 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
1106 `((" " . stgit-mark-down)
1107 ("m" . stgit-mark-down)
1108 ("\d" . stgit-unmark-up)
1109 ("u" . stgit-unmark-down)
1110 ("?" . stgit-help)
1111 ("h" . stgit-help)
1112 ("\C-p" . stgit-previous-line)
1113 ("\C-n" . stgit-next-line)
1114 ([up] . stgit-previous-line)
1115 ([down] . stgit-next-line)
1116 ("p" . stgit-previous-patch)
1117 ("n" . stgit-next-patch)
1118 ("\M-{" . stgit-previous-patch)
1119 ("\M-}" . stgit-next-patch)
1120 ("s" . stgit-git-status)
1121 ("g" . stgit-reload-or-repair)
1122 ("r" . stgit-refresh)
1123 ("\C-c\C-r" . stgit-rename)
1124 ("e" . stgit-edit)
1125 ("M" . stgit-move-patches)
1126 ("S" . stgit-squash)
1127 ("N" . stgit-new)
1128 ("c" . stgit-new-and-refresh)
1129 ("\C-c\C-c" . stgit-commit)
1130 ("\C-c\C-u" . stgit-uncommit)
1131 ("U" . stgit-revert)
1132 ("R" . stgit-resolve-file)
1133 ("\r" . stgit-select)
1134 ("+" . stgit-expand)
1135 ("-" . stgit-collapse)
1136 ("o" . stgit-find-file-other-window)
1137 ("i" . stgit-toggle-index)
1138 (">" . stgit-push-next)
1139 ("<" . stgit-pop-next)
1140 ("P" . stgit-push-or-pop)
1141 ("G" . stgit-goto)
1142 ("=" . stgit-diff)
1143 ("D" . stgit-delete)
1144 ([?\C-/] . stgit-undo)
1145 ("\C-_" . stgit-undo)
1146 ([?\C-c ?\C-/] . stgit-redo)
1147 ("\C-c\C-_" . stgit-redo)
1148 ("B" . stgit-branch)
1149 ("\C-c\C-b" . stgit-rebase)
1150 ("t" . ,toggle-map)
1151 ("d" . ,diff-map)
1152 ("q" . stgit-quit)
1153 ("!" . stgit-execute))))
1154
1155 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1156 (and file (eq (stgit-file->status file)
1157 'unmerged))))
1158 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1159 (easy-menu-define stgit-menu stgit-mode-map
1160 "StGit Menu"
1161 `("StGit"
1162 ["Reload" stgit-reload-or-repair
1163 :help "Reload StGit status from disk"]
1164 ["Repair" stgit-repair
1165 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1166 :help "Repair StGit metadata"]
1167 "-"
1168 ["Undo" stgit-undo t]
1169 ["Redo" stgit-redo t]
1170 "-"
1171 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1172 "-"
1173 ["New patch" stgit-new-and-refresh
1174 :help "Create a new patch from changes in index or work tree"
1175 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1176 ["New empty patch" stgit-new
1177 :help "Create a new, empty patch"]
1178 ["(Un)mark patch" stgit-toggle-mark
1179 :label (if (memq (stgit-patch-name-at-point nil t)
1180 stgit-marked-patches)
1181 "Unmark patch" "Mark patch")
1182 :active (stgit-patch-name-at-point nil t)]
1183 ["Expand/collapse patch"
1184 (let ((patches (stgit-patches-marked-or-at-point)))
1185 (if (member-if ,patch-collapsed-p patches)
1186 (stgit-expand patches)
1187 (stgit-collapse patches)))
1188 :label (if (member-if ,patch-collapsed-p
1189 (stgit-patches-marked-or-at-point))
1190 "Expand patches"
1191 "Collapse patches")
1192 :active (stgit-patches-marked-or-at-point)]
1193 ["Edit patch" stgit-edit
1194 :help "Edit patch comment"
1195 :active (stgit-patch-name-at-point nil t)]
1196 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1197 ["Push/pop patch" stgit-push-or-pop
1198 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1199 (stgit-applied-patchsyms t))
1200 "Pop patches" "Push patches")]
1201 ["Delete patches" stgit-delete
1202 :active (stgit-patches-marked-or-at-point nil t)]
1203 "-"
1204 ["Move patches" stgit-move-patches
1205 :active stgit-marked-patches
1206 :help "Move marked patch(es) to point"]
1207 ["Squash patches" stgit-squash
1208 :active (> (length stgit-marked-patches) 1)
1209 :help "Merge marked patches into one"]
1210 "-"
1211 ["Refresh top patch" stgit-refresh
1212 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1213 :help "Refresh the top patch with changes in index or work tree"]
1214 ["Refresh this patch" (stgit-refresh t)
1215 :keys "\\[universal-argument] \\[stgit-refresh]"
1216 :help "Refresh marked patch with changes in index or work tree"
1217 :active (and (not (and (stgit-index-empty-p)
1218 (stgit-work-tree-empty-p)))
1219 (stgit-patch-name-at-point nil t))]
1220 "-"
1221 ["Find file" stgit-select
1222 :active (eq (get-text-property (point) 'entry-type) 'file)]
1223 ["Open file" stgit-find-file-other-window
1224 :active (eq (get-text-property (point) 'entry-type) 'file)]
1225 ["Toggle file index" stgit-toggle-index
1226 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1227 (memq (stgit-patch-name-at-point) '(:work :index)))
1228 :label (if (eq (stgit-patch-name-at-point) :work)
1229 "Move change to index"
1230 "Move change to work tree")]
1231 "-"
1232 ["Show diff" stgit-diff
1233 :active (get-text-property (point) 'entry-type)]
1234 ["Show diff for range of applied patches" stgit-diff-range
1235 :active (= (length stgit-marked-patches) 1)]
1236 ("Merge"
1237 :active (stgit-git-index-unmerged-p)
1238 ["Combined diff" stgit-diff-combined
1239 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1240 ["Diff against base" stgit-diff-base
1241 :help "Show diff against the common base"
1242 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1243 ["Diff against ours" stgit-diff-ours
1244 :help "Show diff against our branch"
1245 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1246 ["Diff against theirs" stgit-diff-theirs
1247 :help "Show diff against their branch"
1248 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1249 "-"
1250 ["Interactive merge" stgit-find-file-merge
1251 :help "Interactively merge the file"
1252 :active ,at-unmerged-file]
1253 ["Resolve file" stgit-resolve-file
1254 :help "Mark file conflict as resolved"
1255 :active ,at-unmerged-file]
1256 )
1257 "-"
1258 ["Show index & work tree" stgit-toggle-worktree :style toggle
1259 :selected stgit-show-worktree]
1260 ["Show unknown files" stgit-toggle-unknown :style toggle
1261 :selected stgit-show-unknown :active stgit-show-worktree]
1262 ["Show ignored files" stgit-toggle-ignored :style toggle
1263 :selected stgit-show-ignored :active stgit-show-worktree]
1264 ["Show patch names" stgit-toggle-patch-names :style toggle
1265 :selected stgit-show-patch-names]
1266 ["Show recent commits" stgit-toggle-committed :style toggle
1267 :selected stgit-show-committed]
1268 ["Show subversion info" stgit-toggle-svn :style toggle
1269 :selected stgit-show-svn]
1270 "-"
1271 ["Switch branches" stgit-branch t
1272 :help "Switch to or create another branch"]
1273 ["Rebase branch" stgit-rebase t
1274 :help "Rebase the current branch"]
1275 ))))
1276
1277 ;; disable tool bar editing buttons
1278 (put 'stgit-mode 'mode-class 'special)
1279
1280 (defun stgit-mode ()
1281 "Major mode for interacting with StGit.
1282
1283 Start StGit using \\[stgit].
1284
1285 Basic commands:
1286 \\<stgit-mode-map>\
1287 \\[stgit-help] Show this help text
1288 \\[stgit-quit] Hide the StGit buffer
1289 \\[describe-bindings] Show all key bindings
1290
1291 \\[stgit-reload-or-repair] Reload the StGit buffer
1292 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1293
1294 \\[stgit-undo] Undo most recent StGit operation
1295 \\[stgit-redo] Undo recent undo
1296
1297 \\[stgit-git-status] Run `git-status' (if available)
1298
1299 \\[stgit-execute] Run an stg shell command
1300
1301 Movement commands:
1302 \\[stgit-previous-line] Move to previous line
1303 \\[stgit-next-line] Move to next line
1304 \\[stgit-previous-patch] Move to previous patch
1305 \\[stgit-next-patch] Move to next patch
1306
1307 \\[stgit-mark-down] Mark patch and move down
1308 \\[stgit-unmark-up] Unmark patch and move up
1309 \\[stgit-unmark-down] Unmark patch and move down
1310
1311 Commands for patches:
1312 \\[stgit-select] Toggle showing changed files in patch
1313 \\[stgit-refresh] Refresh patch with changes in index or work tree
1314 \\[stgit-diff] Show the patch log and diff
1315
1316 \\[stgit-expand] Show changes in marked patches
1317 \\[stgit-collapse] Hide changes in marked patches
1318
1319 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1320 \\[stgit-new] Create a new, empty patch
1321
1322 \\[stgit-rename] Rename patch
1323 \\[stgit-edit] Edit patch description
1324 \\[stgit-delete] Delete patch(es)
1325
1326 \\[stgit-revert] Revert all changes in index or work tree
1327 \\[stgit-toggle-index] Toggle all changes between index and work tree
1328
1329 \\[stgit-push-next] Push next patch onto stack
1330 \\[stgit-pop-next] Pop current patch from stack
1331 \\[stgit-push-or-pop] Push or pop marked patches
1332 \\[stgit-goto] Make patch at point current by popping or pushing
1333
1334 \\[stgit-squash] Squash (meld together) patches
1335 \\[stgit-move-patches] Move marked patches to point
1336
1337 \\[stgit-commit] Commit patch(es)
1338 \\[stgit-uncommit] Uncommit patch(es)
1339
1340 Commands for files:
1341 \\[stgit-select] Open the file in this window
1342 \\[stgit-find-file-other-window] Open the file in another window
1343 \\[stgit-diff] Show the file's diff
1344
1345 \\[stgit-toggle-index] Toggle change between index and work tree
1346 \\[stgit-revert] Revert changes to file
1347
1348 Display commands:
1349 \\[stgit-toggle-patch-names] Toggle showing patch names
1350 \\[stgit-toggle-worktree] Toggle showing index and work tree
1351 \\[stgit-toggle-unknown] Toggle showing unknown files
1352 \\[stgit-toggle-ignored] Toggle showing ignored files
1353 \\[stgit-toggle-committed] Toggle showing recent commits
1354 \\[stgit-toggle-svn] Toggle showing subversion information
1355
1356 Commands for diffs:
1357 \\[stgit-diff] Show diff of patch or file
1358 \\[stgit-diff-range] Show diff for range of patches
1359 \\[stgit-diff-base] Show diff against the merge base
1360 \\[stgit-diff-ours] Show diff against our branch
1361 \\[stgit-diff-theirs] Show diff against their branch
1362
1363 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1364 ignore space changes.
1365 With two prefix arguments (e.g., \\[universal-argument] \
1366 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1367
1368 Commands for merge conflicts:
1369 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1370 \\[stgit-resolve-file] Mark unmerged file as resolved
1371
1372 Commands for branches:
1373 \\[stgit-branch] Switch to or create another branch
1374 \\[stgit-rebase] Rebase the current branch
1375
1376 Customization variables:
1377 `stgit-abbreviate-copies-and-renames'
1378 `stgit-default-show-ignored'
1379 `stgit-default-show-patch-names'
1380 `stgit-default-show-unknown'
1381 `stgit-default-show-worktree'
1382 `stgit-default-show-committed'
1383 `stgit-default-show-svn'
1384 `stgit-default-committed-count'
1385 `stgit-find-copies-harder'
1386 `stgit-show-worktree-mode'
1387
1388 See also \\[customize-group] for the \"stgit\" group."
1389 (kill-all-local-variables)
1390 (buffer-disable-undo)
1391 (setq mode-name "StGit"
1392 major-mode 'stgit-mode
1393 goal-column 2)
1394 (use-local-map stgit-mode-map)
1395 (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x)))
1396 `((list-buffers-directory . ,default-directory)
1397 (parse-sexp-lookup-properties . t)
1398 (stgit-expanded-patches . (:work :index))
1399 (stgit-index-node . nil)
1400 (stgit-worktree-node . nil)
1401 (stgit-marked-patches . nil)
1402 (stgit-svn-find-rev-hash . ,(make-hash-table :test 'equal))
1403 (stgit-committed-count . ,stgit-default-committed-count)
1404 (stgit-show-committed . ,stgit-default-show-committed)
1405 (stgit-show-ignored . ,stgit-default-show-ignored)
1406 (stgit-show-patch-names . ,stgit-default-show-patch-names)
1407 (stgit-show-svn . ,stgit-default-show-svn)
1408 (stgit-show-unknown . ,stgit-default-show-unknown)
1409 (stgit-show-worktree . ,stgit-default-show-worktree)))
1410 (set-variable 'truncate-lines 't)
1411 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1412 (unless stgit-did-advise
1413 (stgit-advise)
1414 (setq stgit-did-advise t))
1415 (run-hooks 'stgit-mode-hook))
1416
1417 (defun stgit-advise-funlist (funlist)
1418 "Add advice to the functions in FUNLIST so we can refresh the
1419 stgit buffers as the git status of files change."
1420 (mapc (lambda (sym)
1421 (when (fboundp sym)
1422 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1423 (stgit-update-stgit-for-buffer t)))
1424 (ad-activate sym)))
1425 funlist))
1426
1427 (defun stgit-advise ()
1428 "Add advice to appropriate (non-stgit) git functions so we can
1429 refresh the stgit buffers as the git status of files change."
1430 (mapc (lambda (arg)
1431 (let ((feature (car arg))
1432 (funlist (cdr arg)))
1433 (if (featurep feature)
1434 (stgit-advise-funlist funlist)
1435 (add-to-list 'after-load-alist
1436 `(,feature (stgit-advise-funlist
1437 (quote ,funlist)))))))
1438 ;; lists of (<feature> <function> <function> ...) to be advised
1439 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1440 (git git-add-file git-checkout git-revert-file git-remove-file)
1441 (dired dired-delete-file))))
1442
1443 (defvar stgit-pending-refresh-buffers nil
1444 "Alist of (cons `buffer' `refresh-index') of buffers that need
1445 to be refreshed. `refresh-index' is non-nil if both work tree
1446 and index need to be refreshed.")
1447
1448 (defun stgit-run-pending-refreshs ()
1449 "Run all pending stgit buffer updates as posted by `stgit-post-refresh'."
1450 (let ((buffers stgit-pending-refresh-buffers)
1451 (stgit-inhibit-messages t))
1452 (setq stgit-pending-refresh-buffers nil)
1453 (while buffers
1454 (let* ((elem (car buffers))
1455 (buffer (car elem))
1456 (refresh-index (cdr elem)))
1457 (when (buffer-name buffer)
1458 (with-current-buffer buffer
1459 (stgit-refresh-worktree)
1460 (when refresh-index (stgit-refresh-index)))))
1461 (setq buffers (cdr buffers)))))
1462
1463 (defun stgit-post-refresh (buffer refresh-index)
1464 "Update worktree status in BUFFER when Emacs becomes idle. If
1465 REFRESH-INDEX is non-nil, also update the index."
1466 (unless stgit-pending-refresh-buffers
1467 (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs))
1468 (let ((elem (assq buffer stgit-pending-refresh-buffers)))
1469 (if elem
1470 ;; if buffer is already present, set its refresh-index flag if
1471 ;; necessary
1472 (when refresh-index
1473 (setcdr elem t))
1474 ;; new entry
1475 (setq stgit-pending-refresh-buffers
1476 (cons (cons buffer refresh-index)
1477 stgit-pending-refresh-buffers)))))
1478
1479 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1480 "When Emacs becomes idle, refresh worktree status in any
1481 `stgit-mode' buffer that shows the status of the current buffer.
1482
1483 If REFRESH-INDEX is non-nil, also update the index."
1484 (let* ((dir (cond ((derived-mode-p 'stgit-status-mode 'dired-mode)
1485 default-directory)
1486 (buffer-file-name
1487 (file-name-directory
1488 (expand-file-name buffer-file-name)))))
1489 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1490 (error nil))))
1491 (buffer (and gitdir (stgit-find-buffer gitdir))))
1492 (when buffer
1493 (stgit-post-refresh buffer refresh-index))))
1494
1495 (defun stgit-add-mark (patchsym)
1496 "Mark the patch PATCHSYM."
1497 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1498
1499 (defun stgit-remove-mark (patchsym)
1500 "Unmark the patch PATCHSYM."
1501 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1502
1503 (defun stgit-clear-marks ()
1504 "Unmark all patches."
1505 (setq stgit-marked-patches '()))
1506
1507 (defun stgit-patch-at-point (&optional cause-error)
1508 (get-text-property (point) 'patch-data))
1509
1510 (defun stgit-patch-name-at-point (&optional cause-error types)
1511 "Return the patch name on the current line as a symbol.
1512 If CAUSE-ERROR is not nil, signal an error if none found.
1513
1514 TYPES controls which types of commits and patches can be returned.
1515 If it is t, only allow stgit patches; if 'allow-committed, also
1516 allow historical commits; if nil, also allow work tree and index."
1517 (let ((patch (stgit-patch-at-point)))
1518 (and patch
1519 (memq (stgit-patch->status patch)
1520 (case types
1521 ((nil) nil)
1522 ((allow-committed) '(work index))
1523 ((t) '(work index committed))
1524 (t (error "Bad value"))))
1525 (setq patch nil))
1526 (cond (patch
1527 (stgit-patch->name patch))
1528 (cause-error
1529 (error "No patch on this line")))))
1530
1531 (defun stgit-patched-file-at-point ()
1532 (get-text-property (point) 'file-data))
1533
1534 (defun stgit-patches-marked-or-at-point (&optional cause-error types)
1535 "Return the symbols of the marked patches, or the patch on the current line.
1536 If CAUSE-ERRROR is not nil, signal an error if none found.
1537
1538 TYPES controls which types of commits and patches can be returned.
1539 If it is t, only allow stgit patches; if 'allow-committed, also
1540 allow historical commits; if nil, also allow work tree and index."
1541 (if stgit-marked-patches
1542 stgit-marked-patches
1543 (let ((patch (stgit-patch-name-at-point nil types)))
1544 (cond (patch (list patch))
1545 (cause-error (error "No patches marked or at this line"))
1546 (t nil)))))
1547
1548 (defun stgit-goto-patch (patchsym &optional file)
1549 "Move point to the line containing patch PATCHSYM and return non-nil.
1550 If that patch cannot be found, do nothing and return nil.
1551
1552 If the patch was found and FILE is not nil, instead move to that
1553 file's line. If FILE cannot be found, stay on the line of
1554 PATCHSYM."
1555 (let ((node (ewoc-nth stgit-ewoc 0)))
1556 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1557 patchsym)))
1558 (setq node (ewoc-next stgit-ewoc node)))
1559 (when (and node file)
1560 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1561 (file-node (ewoc-nth file-ewoc 0)))
1562 (while (and file-node
1563 (not (equal (stgit-file->file (ewoc-data file-node))
1564 file)))
1565 (setq file-node (ewoc-next file-ewoc file-node)))
1566 (when file-node
1567 (ewoc-goto-node file-ewoc file-node)
1568 (move-to-column (stgit-goal-column))
1569 (setq node nil))))
1570 (when node
1571 (ewoc-goto-node stgit-ewoc node)
1572 (move-to-column goal-column))))
1573
1574 (defun stgit-init ()
1575 "Run stg init."
1576 (interactive)
1577 (stgit-assert-mode)
1578 (stgit-capture-output nil
1579 (stgit-run "init"))
1580 (stgit-reload))
1581
1582 (defun stgit-toggle-mark ()
1583 "Toggle mark on the patch under point."
1584 (interactive)
1585 (stgit-assert-mode)
1586 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1587 (stgit-unmark)
1588 (stgit-mark)))
1589
1590 (defun stgit-mark ()
1591 "Mark the patch under point."
1592 (interactive)
1593 (stgit-assert-mode)
1594 (let* ((node (ewoc-locate stgit-ewoc))
1595 (patch (ewoc-data node)))
1596 (case (stgit-patch->status patch)
1597 (work (error "Cannot mark the work tree"))
1598 (index (error "Cannot mark the index"))
1599 (committed (error "Cannot mark a committed patch")))
1600 (stgit-add-mark (stgit-patch->name patch))
1601 (let ((column (current-column)))
1602 (ewoc-invalidate stgit-ewoc node)
1603 (move-to-column column))))
1604
1605 (defun stgit-mark-down ()
1606 "Mark the patch under point and move to the next patch."
1607 (interactive)
1608 (stgit-mark)
1609 (stgit-next-patch))
1610
1611 (defun stgit-unmark ()
1612 "Remove mark from the patch on the current line."
1613 (interactive)
1614 (stgit-assert-mode)
1615 (let* ((node (ewoc-locate stgit-ewoc))
1616 (patch (ewoc-data node)))
1617 (stgit-remove-mark (stgit-patch->name patch))
1618 (let ((column (current-column)))
1619 (ewoc-invalidate stgit-ewoc node)
1620 (move-to-column column))))
1621
1622 (defun stgit-unmark-up ()
1623 "Remove mark from the patch on the previous line."
1624 (interactive)
1625 (stgit-assert-mode)
1626 (stgit-previous-patch)
1627 (stgit-unmark))
1628
1629 (defun stgit-unmark-down ()
1630 "Remove mark from the patch on the current line."
1631 (interactive)
1632 (stgit-assert-mode)
1633 (stgit-unmark)
1634 (stgit-next-patch))
1635
1636 (defun stgit-rename (name)
1637 "Rename the patch under point to NAME."
1638 (interactive (list
1639 (read-string "Patch name: "
1640 (symbol-name (stgit-patch-name-at-point t t)))))
1641 (stgit-assert-mode)
1642 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1643 (stgit-capture-output nil
1644 (stgit-run "rename" "--" old-patchsym name))
1645 (let ((name-sym (intern name)))
1646 (when (memq old-patchsym stgit-expanded-patches)
1647 (setq stgit-expanded-patches
1648 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1649 (when (memq old-patchsym stgit-marked-patches)
1650 (setq stgit-marked-patches
1651 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1652 (stgit-reload)
1653 (stgit-goto-patch name-sym))))
1654
1655 (defun stgit-reload-or-repair (repair)
1656 "Update the contents of the StGit buffer (`stgit-reload').
1657
1658 With a prefix argument, repair the StGit metadata if the branch
1659 was modified with git commands (`stgit-repair')."
1660 (interactive "P")
1661 (stgit-assert-mode)
1662 (if repair
1663 (stgit-repair)
1664 (stgit-reload)))
1665
1666 (defun stgit-repair ()
1667 "Run stg repair."
1668 (interactive)
1669 (stgit-assert-mode)
1670 (stgit-capture-output nil
1671 (stgit-run "repair"))
1672 (stgit-reload))
1673
1674 (defun stgit-available-branches (&optional all)
1675 "Returns a list of the names of the available stg branches as strings.
1676
1677 If ALL is not nil, also return non-stgit branches."
1678 (let ((output (with-output-to-string
1679 (stgit-run "branch" "--list")))
1680 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1681 (if all ?. ?s)))
1682 (start 0)
1683 result)
1684 (while (string-match pattern output start)
1685 (setq result (cons (match-string 1 output) result))
1686 (setq start (match-end 0)))
1687 result))
1688
1689 (defun stgit-branch (branch)
1690 "Switch to or create branch BRANCH."
1691 (interactive (list (completing-read "Switch to branch: "
1692 (stgit-available-branches))))
1693 (stgit-assert-mode)
1694 (when (cond ((equal branch (stgit-current-branch))
1695 (error "Branch is already current"))
1696 ((member branch (stgit-available-branches t))
1697 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1698 t)
1699 ((not (string-match stgit-allowed-branch-name-re branch))
1700 (error "Invalid branch name"))
1701 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1702 (let ((branch-point (completing-read
1703 "Branch from (default current branch): "
1704 (stgit-available-branches))))
1705 (stgit-capture-output nil
1706 (apply 'stgit-run
1707 `("branch" "--create" "--"
1708 ,branch
1709 ,@(unless (zerop (length branch-point))
1710 (list branch-point)))))
1711 t)))
1712 (stgit-reload)))
1713
1714 (defun stgit-available-refs (&optional omit-stgit)
1715 "Returns a list of the available git refs.
1716 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1717 (let* ((output (with-output-to-string
1718 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1719 "refs/tags" "refs/heads"
1720 "refs/remotes")))
1721 (result (split-string output "\n" t)))
1722 (mapcar (lambda (s)
1723 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1724 (substring s (match-end 0))
1725 s))
1726 (if omit-stgit
1727 (delete-if (lambda (s)
1728 (string-match "^refs/heads/.*\\.stgit$" s))
1729 result)
1730 result))))
1731
1732 (defun stgit-parent-branch ()
1733 "Return the parent branch of the current stg branch as per
1734 git-config setting branch.<branch>.stgit.parentbranch."
1735 (let ((output (with-output-to-string
1736 (stgit-run-git-silent "config"
1737 (format "branch.%s.stgit.parentbranch"
1738 (stgit-current-branch))))))
1739 (when (string-match ".*" output)
1740 (match-string 0 output))))
1741
1742 (defun stgit-rebase (new-base)
1743 "Rebase the current branch to NEW-BASE.
1744
1745 Interactively, first ask which branch to rebase to. Defaults to
1746 what git-config branch.<branch>.stgit.parentbranch is set to."
1747 (interactive (list (completing-read "Rebase to: "
1748 (stgit-available-refs t)
1749 nil nil
1750 (stgit-parent-branch))))
1751 (stgit-assert-mode)
1752 (stgit-capture-output nil (stgit-run "rebase" "--" new-base))
1753 (stgit-reload))
1754
1755 (defun stgit-commit (count)
1756 "Run stg commit on COUNT commits.
1757 Interactively, the prefix argument is used as COUNT.
1758 A negative COUNT will uncommit instead."
1759 (interactive "p")
1760 (stgit-assert-mode)
1761 (if (< count 0)
1762 (stgit-uncommit (- count))
1763 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1764 (stgit-reload)))
1765
1766 (defun stgit-uncommit (count)
1767 "Run stg uncommit on COUNT commits.
1768 Interactively, the prefix argument is used as COUNT.
1769 A negative COUNT will commit instead."
1770 (interactive "p")
1771 (stgit-assert-mode)
1772 (if (< count 0)
1773 (stgit-commit (- count))
1774 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1775 (stgit-reload)))
1776
1777 (defun stgit-neighbour-file ()
1778 "Return the file name of the next file after point, or the
1779 previous file if point is at the last file within a patch."
1780 (let ((old-point (point))
1781 neighbour-file)
1782 (and (zerop (forward-line 1))
1783 (let ((f (stgit-patched-file-at-point)))
1784 (and f (setq neighbour-file (stgit-file->file f)))))
1785 (goto-char old-point)
1786 (unless neighbour-file
1787 (and (zerop (forward-line -1))
1788 (let ((f (stgit-patched-file-at-point)))
1789 (and f (setq neighbour-file (stgit-file->file f)))))
1790 (goto-char old-point))
1791 neighbour-file))
1792
1793 (defun stgit-revert-file ()
1794 "Revert the file at point, which must be in the index or the
1795 working tree."
1796 (interactive)
1797 (stgit-assert-mode)
1798 (let* ((patched-file (or (stgit-patched-file-at-point)
1799 (error "No file on the current line")))
1800 (patch-name (stgit-patch-name-at-point))
1801 (file-status (stgit-file->status patched-file))
1802 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1803 (stgit-file->cr-to patched-file))
1804 ((eq file-status 'add)
1805 (stgit-file->file patched-file))))
1806 (co-file (cond ((eq file-status 'rename)
1807 (stgit-file->cr-from patched-file))
1808 ((not (memq file-status '(copy add)))
1809 (stgit-file->file patched-file))))
1810 (next-file (stgit-neighbour-file)))
1811
1812 (unless (memq patch-name '(:work :index))
1813 (error "No index or working tree file on this line"))
1814
1815 (when (eq file-status 'ignore)
1816 (error "Cannot revert ignored files"))
1817
1818 (when (eq file-status 'unknown)
1819 (error "Cannot revert unknown files"))
1820
1821 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1822 (when (yes-or-no-p (format "Revert %d file%s? "
1823 nfiles
1824 (if (= nfiles 1) "" "s")))
1825 (stgit-capture-output nil
1826 (when rm-file
1827 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1828 (when co-file
1829 (stgit-run-git "checkout" "HEAD" co-file)))
1830 (stgit-reload)
1831 (stgit-goto-patch patch-name next-file)))))
1832
1833 (defun stgit-revert ()
1834 "Revert the change at point, which must be the index, the work
1835 tree, or a single change in either."
1836 (interactive)
1837 (stgit-assert-mode)
1838 (let ((patched-file (stgit-patched-file-at-point)))
1839 (if patched-file
1840 (stgit-revert-file)
1841 (let* ((patch-name (or (stgit-patch-name-at-point)
1842 (error "No patch or file at point")))
1843 (patch-desc (case patch-name
1844 (:index "index")
1845 (:work "work tree")
1846 (t (error (substitute-command-keys
1847 "Use \\[stgit-delete] to delete a patch"))))))
1848 (when (if (eq patch-name :work)
1849 (stgit-work-tree-empty-p)
1850 (stgit-index-empty-p))
1851 (error (format "There are no changes in the %s to revert"
1852 patch-desc)))
1853 (and (eq patch-name :index)
1854 (not (stgit-work-tree-empty-p))
1855 (error "Cannot revert index as work tree contains unstaged changes"))
1856
1857 (when (yes-or-no-p (format "Revert all changes in the %s? "
1858 patch-desc))
1859 (if (eq patch-name :index)
1860 (stgit-run-git-silent "reset" "--hard" "-q")
1861 (stgit-run-git-silent "checkout" "--" "."))
1862 (stgit-refresh-index)
1863 (stgit-refresh-worktree)
1864 (stgit-goto-patch patch-name))))))
1865
1866 (defun stgit-resolve-file ()
1867 "Resolve conflict in the file at point."
1868 (interactive)
1869 (stgit-assert-mode)
1870 (let* ((patched-file (stgit-patched-file-at-point))
1871 (patch (stgit-patch-at-point))
1872 (patch-name (and patch (stgit-patch->name patch)))
1873 (status (and patched-file (stgit-file->status patched-file))))
1874
1875 (unless (memq patch-name '(:work :index))
1876 (error "No index or working tree file on this line"))
1877
1878 (unless (eq status 'unmerged)
1879 (error "No conflict to resolve at the current line"))
1880
1881 (stgit-capture-output nil
1882 (stgit-move-change-to-index (stgit-file->file patched-file)))
1883
1884 (stgit-reload)))
1885
1886 (defun stgit-push-or-pop-patches (do-push npatches)
1887 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1888 NPATCHES patches, or all patches if NPATCHES is t."
1889 (stgit-assert-mode)
1890 (stgit-capture-output nil
1891 (apply 'stgit-run
1892 (if do-push "push" "pop")
1893 (if (eq npatches t)
1894 '("--all")
1895 (list "-n" npatches))))
1896 (stgit-reload)
1897 (stgit-refresh-git-status))
1898
1899 (defun stgit-push-next (npatches)
1900 "Push the first unapplied patch.
1901 With numeric prefix argument, push that many patches."
1902 (interactive "p")
1903 (stgit-push-or-pop-patches t npatches))
1904
1905 (defun stgit-pop-next (npatches)
1906 "Pop the topmost applied patch.
1907 With numeric prefix argument, pop that many patches.
1908
1909 If NPATCHES is t, pop all patches."
1910 (interactive "p")
1911 (stgit-push-or-pop-patches nil npatches))
1912
1913 (defun stgit-applied-patches (&optional only-patches)
1914 "Return a list of the applied patches.
1915
1916 If ONLY-PATCHES is not nil, exclude index and work tree."
1917 (let ((states (if only-patches
1918 '(applied top)
1919 '(applied top index work)))
1920 result)
1921 (ewoc-map (lambda (patch)
1922 (when (memq (stgit-patch->status patch) states)
1923 (setq result (cons patch result)))
1924 nil)
1925 stgit-ewoc)
1926 result))
1927
1928 (defun stgit-applied-patchsyms (&optional only-patches)
1929 "Return a list of the symbols of the applied patches.
1930
1931 If ONLY-PATCHES is not nil, exclude index and work tree."
1932 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1933
1934 (defun stgit-push-or-pop ()
1935 "Push or pop the marked patches."
1936 (interactive)
1937 (stgit-assert-mode)
1938 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1939 (applied-syms (stgit-applied-patchsyms t))
1940 (unapplied (set-difference patchsyms applied-syms)))
1941 (stgit-capture-output nil
1942 (apply 'stgit-run
1943 (if unapplied "push" "pop")
1944 "--"
1945 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1946 (stgit-reload))
1947
1948 (defun stgit-goto-target ()
1949 "Return the goto target at point: a patchsym, :top,
1950 or :bottom."
1951 (let ((patch (stgit-patch-at-point)))
1952 (cond (patch
1953 (case (stgit-patch->status patch)
1954 ((work index) nil)
1955 ((committed) :bottom)
1956 (t (stgit-patch->name patch))))
1957 ((not (next-single-property-change (point) 'patch-data))
1958 :top)
1959 ((not (previous-single-property-change (point) 'patch-data))
1960 :bottom))))
1961
1962 (defun stgit-goto ()
1963 "Go to the patch on the current line.
1964
1965 Push or pop patches to make this patch topmost. Push or pop all
1966 patches if used on a line after or before all patches."
1967 (interactive)
1968 (stgit-assert-mode)
1969 (let ((patchsym (stgit-goto-target)))
1970 (unless patchsym
1971 (error "No patch to go to on this line"))
1972 (case patchsym
1973 (:top (stgit-push-or-pop-patches t t))
1974 (:bottom (stgit-push-or-pop-patches nil t))
1975 (t (stgit-capture-output nil
1976 (stgit-run "goto" "--" patchsym))
1977 (stgit-reload)))))
1978
1979 (defun stgit-id (patchsym)
1980 "Return the git commit id for PATCHSYM.
1981 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1982 (if (keywordp patchsym)
1983 patchsym
1984 (let ((result (with-output-to-string
1985 (stgit-run-silent "id" "--" patchsym))))
1986 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1987 (error "Cannot find commit id for %s" patchsym))
1988 (match-string 1 result))))
1989
1990 (defun stgit-whitespace-diff-arg (arg)
1991 (when (numberp arg)
1992 (cond ((> arg 4) "--ignore-all-space")
1993 ((> arg 1) "--ignore-space-change"))))
1994
1995 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1996 "Show the patch on the current line.
1997
1998 UNMERGED-STAGE is the argument to `git-diff' that that selects
1999 which stage to diff against in the case of unmerged files."
2000 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
2001 (patch-name (stgit-patch-name-at-point t)))
2002 (stgit-capture-output "*StGit patch*"
2003 (case (get-text-property (point) 'entry-type)
2004 ('file
2005 (let* ((patched-file (stgit-patched-file-at-point))
2006 (patch-id (let ((id (stgit-id patch-name)))
2007 (if (and (eq id :index)
2008 (eq (stgit-file->status patched-file)
2009 'unmerged))
2010 :work
2011 id)))
2012 (args (append (and space-arg (list space-arg))
2013 (and (stgit-file->cr-from patched-file)
2014 (list (stgit-find-copies-harder-diff-arg)))
2015 (cond ((eq patch-id :index)
2016 '("--cached"))
2017 ((eq patch-id :work)
2018 (list unmerged-stage))
2019 (t
2020 (list (concat patch-id "^") patch-id)))
2021 (and (eq (stgit-file->status patched-file) 'copy)
2022 '("--diff-filter=C"))
2023 '("--")
2024 (if (stgit-file->copy-or-rename patched-file)
2025 (list (stgit-file->cr-from patched-file)
2026 (stgit-file->cr-to patched-file))
2027 (list (stgit-file->file patched-file))))))
2028 (apply 'stgit-run-git "diff" args)))
2029 ('patch
2030 (let* ((patch-id (stgit-id patch-name)))
2031 (if (or (eq patch-id :index) (eq patch-id :work))
2032 (apply 'stgit-run-git "diff"
2033 (stgit-find-copies-harder-diff-arg)
2034 (append (and space-arg (list space-arg))
2035 (if (eq patch-id :index)
2036 '("--cached")
2037 (list unmerged-stage))))
2038 (let ((args (append '("show" "-O" "--patch-with-stat")
2039 `("-O" ,(stgit-find-copies-harder-diff-arg))
2040 (and space-arg (list "-O" space-arg))
2041 '("--")
2042 (list (stgit-patch-name-at-point)))))
2043 (apply 'stgit-run args)))))
2044 (t
2045 (error "No patch or file at point")))
2046 (with-current-buffer standard-output
2047 (goto-char (point-min))
2048 (diff-mode)))))
2049
2050 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
2051 `(defun ,name (&optional ignore-whitespace)
2052 ,(format "Show the patch on the current line.
2053
2054 %sWith a prefix argument, ignore whitespace. With a prefix argument
2055 greater than four (e.g., \\[universal-argument] \
2056 \\[universal-argument] \\[%s]), ignore all whitespace."
2057 (if unmerged-action
2058 (format "For unmerged files, %s.\n\n" unmerged-action)
2059 "")
2060 name)
2061 (interactive "p")
2062 (stgit-assert-mode)
2063 (stgit-show-patch ,diff-arg ignore-whitespace)))
2064
2065 (stgit-define-diff stgit-diff
2066 "--ours" nil)
2067 (stgit-define-diff stgit-diff-ours
2068 "--ours"
2069 "diff against our branch")
2070 (stgit-define-diff stgit-diff-theirs
2071 "--theirs"
2072 "diff against their branch")
2073 (stgit-define-diff stgit-diff-base
2074 "--base"
2075 "diff against the merge base")
2076 (stgit-define-diff stgit-diff-combined
2077 "--cc"
2078 "show a combined diff")
2079
2080 (defun stgit-diff-range (&optional ignore-whitespace)
2081 "Show diff for the range of patches between point and the marked patch.
2082
2083 With a prefix argument, ignore whitespace. With a prefix argument
2084 greater than four (e.g., \\[universal-argument] \
2085 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
2086 (interactive "p")
2087 (stgit-assert-mode)
2088 (unless (= (length stgit-marked-patches) 1)
2089 (error "Need exactly one patch marked"))
2090 (let* ((patches (stgit-sort-patches
2091 (cons (stgit-patch-name-at-point t 'allow-committed)
2092 stgit-marked-patches)
2093 t))
2094 (first-patch (car patches))
2095 (second-patch (if (cdr patches) (cadr patches) first-patch))
2096 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
2097 (applied (stgit-applied-patchsyms t)))
2098 (unless (and (memq first-patch applied) (memq second-patch applied))
2099 (error "Can only show diff range for applied patches"))
2100 (stgit-capture-output (format "*StGit diff %s..%s*"
2101 first-patch second-patch)
2102 (apply 'stgit-run-git
2103 "diff" "--patch-with-stat"
2104 (stgit-find-copies-harder-diff-arg)
2105 (append (and whitespace-arg (list whitespace-arg))
2106 (list (format "%s^" (stgit-id first-patch))
2107 (stgit-id second-patch))))
2108 (with-current-buffer standard-output
2109 (goto-char (point-min))
2110 (diff-mode)))))
2111
2112 (defun stgit-move-change-to-index (file &optional force)
2113 "Copies the work tree state of FILE to index, using git add or git rm.
2114
2115 If FORCE is not nil, use --force."
2116 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
2117 '("add") '("rm" "-q"))))
2118 (stgit-capture-output "*git output*"
2119 (apply 'stgit-run-git (append op (and force '("--force"))
2120 '("--") (list file))))))
2121
2122 (defun stgit-remove-change-from-index (file)
2123 "Unstages the change in FILE from the index"
2124 (stgit-capture-output "*git output*"
2125 (stgit-run-git "reset" "-q" "--" file)))
2126
2127 (defun stgit-git-index-unmerged-p ()
2128 (let (result)
2129 (with-output-to-string
2130 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
2131 "--diff-filter=U"
2132 "--quiet" "HEAD")))))
2133 result))
2134
2135 (defun stgit-file-toggle-index ()
2136 "Move modified file in or out of the index.
2137
2138 Leaves the point where it is, but moves the mark to where the
2139 file ended up. You can then jump to the file with \
2140 \\[exchange-point-and-mark]."
2141 (interactive)
2142 (stgit-assert-mode)
2143 (let* ((patched-file (or (stgit-patched-file-at-point)
2144 (error "No file on the current line")))
2145 (patched-status (stgit-file->status patched-file)))
2146 (when (eq patched-status 'unmerged)
2147 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
2148 (let* ((patch (stgit-patch-at-point))
2149 (patch-name (stgit-patch->name patch))
2150 (mark-file (if (eq patched-status 'rename)
2151 (stgit-file->cr-to patched-file)
2152 (stgit-file->file patched-file)))
2153 (point-file (if (eq patched-status 'rename)
2154 (stgit-file->cr-from patched-file)
2155 (stgit-neighbour-file))))
2156
2157 (cond ((eq patch-name :work)
2158 (stgit-move-change-to-index (stgit-file->file patched-file)
2159 (eq patched-status 'ignore)))
2160 ((eq patch-name :index)
2161 (stgit-remove-change-from-index (stgit-file->file patched-file)))
2162 (t
2163 (error "Can only move files between working tree and index")))
2164 (stgit-refresh-worktree)
2165 (stgit-refresh-index)
2166 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
2167 (push-mark nil t t)
2168 (stgit-goto-patch patch-name point-file))))
2169
2170 (defun stgit-toggle-index ()
2171 "Move change in or out of the index.
2172
2173 Works on index and work tree, as well as files in either.
2174
2175 Leaves the point where it is, but moves the mark to where the
2176 file ended up. You can then jump to the file with \
2177 \\[exchange-point-and-mark]."
2178 (interactive)
2179 (stgit-assert-mode)
2180 (if (stgit-patched-file-at-point)
2181 (stgit-file-toggle-index)
2182 (let ((patch-name (stgit-patch-name-at-point)))
2183 (unless (memq patch-name '(:index :work))
2184 (error "Can only move changes between working tree and index"))
2185 (when (stgit-git-index-unmerged-p)
2186 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
2187 (if (if (eq patch-name :index)
2188 (stgit-index-empty-p)
2189 (stgit-work-tree-empty-p))
2190 (message "No changes to be moved")
2191 (stgit-capture-output nil
2192 (if (eq patch-name :work)
2193 (stgit-run-git "add" "--update")
2194 (stgit-run-git "reset" "--mixed" "-q")))
2195 (stgit-refresh-worktree)
2196 (stgit-refresh-index))
2197 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
2198
2199 (defun stgit-edit ()
2200 "Edit the patch on the current line."
2201 (interactive)
2202 (stgit-assert-mode)
2203 (let ((patchsym (stgit-patch-name-at-point t t))
2204 (edit-buf (get-buffer-create "*StGit edit*"))
2205 (dir default-directory))
2206 (log-edit 'stgit-confirm-edit t nil edit-buf)
2207 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
2208 (setq default-directory dir)
2209 (let ((standard-output edit-buf))
2210 (save-excursion
2211 (stgit-run-silent "edit" "--save-template=-" "--" patchsym)))))
2212
2213 (defun stgit-confirm-edit ()
2214 (interactive)
2215 (let ((file (make-temp-file "stgit-edit-")))
2216 (write-region (point-min) (point-max) file)
2217 (stgit-capture-output nil
2218 (stgit-run "edit" "-f" file "--" stgit-edit-patchsym))
2219 (with-current-buffer log-edit-parent-buffer
2220 (stgit-reload))))
2221
2222 (defun stgit-new (add-sign &optional refresh)
2223 "Create a new patch.
2224 With a prefix argument, include a \"Signed-off-by:\" line at the
2225 end of the patch."
2226 (interactive "P")
2227 (stgit-assert-mode)
2228 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2229 (dir default-directory))
2230 (log-edit 'stgit-confirm-new t nil edit-buf)
2231 (setq default-directory dir)
2232 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2233 (when add-sign
2234 (save-excursion
2235 (let ((standard-output (current-buffer)))
2236 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2237
2238 (defun stgit-confirm-new ()
2239 (interactive)
2240 (let ((file (make-temp-file "stgit-edit-"))
2241 (refresh stgit-refresh-after-new))
2242 (write-region (point-min) (point-max) file)
2243 (stgit-capture-output nil
2244 (stgit-run "new" "-f" file))
2245 (with-current-buffer log-edit-parent-buffer
2246 (if refresh
2247 (stgit-refresh)
2248 (stgit-reload)))))
2249
2250 (defun stgit-new-and-refresh (add-sign)
2251 "Create a new patch and refresh it with the current changes.
2252
2253 With a prefix argument, include a \"Signed-off-by:\" line at the
2254 end of the patch.
2255
2256 This works just like running `stgit-new' followed by `stgit-refresh'."
2257 (interactive "P")
2258 (stgit-assert-mode)
2259 (stgit-new add-sign t))
2260
2261 (defun stgit-create-patch-name (description)
2262 "Create a patch name from a long description"
2263 (let ((patch ""))
2264 (while (> (length description) 0)
2265 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2266 (setq patch (downcase (concat patch
2267 (match-string 0 description))))
2268 (setq description (substring description (match-end 0))))
2269 ((string-match "\\` +" description)
2270 (setq patch (concat patch "-"))
2271 (setq description (substring description (match-end 0))))
2272 ((string-match "\\`[^a-zA-Z_-]+" description)
2273 (setq description (substring description (match-end 0))))))
2274 (cond ((= (length patch) 0)
2275 "patch")
2276 ((> (length patch) 20)
2277 (substring patch 0 20))
2278 (t patch))))
2279
2280 (defun stgit-delete (patchsyms &optional spill-p)
2281 "Delete the patches in PATCHSYMS.
2282 Interactively, delete the marked patches, or the patch at point.
2283
2284 With a prefix argument, or SPILL-P, spill the patch contents to
2285 the work tree and index."
2286 (interactive (list (stgit-patches-marked-or-at-point t t)
2287 current-prefix-arg))
2288 (stgit-assert-mode)
2289 (unless patchsyms
2290 (error "No patches to delete"))
2291 (when (memq :index patchsyms)
2292 (error "Cannot delete the index"))
2293 (when (memq :work patchsyms)
2294 (error "Cannot delete the work tree"))
2295
2296 (let ((npatches (length patchsyms)))
2297 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2298 npatches
2299 (if (= 1 npatches) "" "es")
2300 (if spill-p
2301 " (spilling contents to index)"
2302 "")))
2303 (let ((args (append (when spill-p '("--spill"))
2304 '("--")
2305 patchsyms)))
2306 (stgit-capture-output nil
2307 (apply 'stgit-run "delete" args))
2308 (stgit-reload)))))
2309
2310 (defun stgit-move-patches-target ()
2311 "Return the patchsym indicating a target patch for
2312 `stgit-move-patches'.
2313
2314 This is either the first unmarked patch at or after point, or one
2315 of :top and :bottom if the point is after or before the applied
2316 patches."
2317
2318 (save-excursion
2319 (let (result)
2320 (while (not result)
2321 (let ((patchsym (stgit-patch-name-at-point)))
2322 (cond ((memq patchsym '(:work :index)) (setq result :top))
2323 (patchsym (if (memq patchsym stgit-marked-patches)
2324 (stgit-next-patch)
2325 (setq result patchsym)))
2326 ((re-search-backward "^>" nil t) (setq result :top))
2327 (t (setq result :bottom)))))
2328 result)))
2329
2330 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2331 "Returns the list of patches in PATCHSYMS sorted according to
2332 their position in the patch series, bottommost first.
2333
2334 PATCHSYMS must not contain duplicate entries, unless
2335 ALLOW-DUPLICATES is not nil."
2336 (let (sorted-patchsyms)
2337 (ewoc-map #'(lambda (patch)
2338 (let ((name (stgit-patch->name patch)))
2339 (when (memq name patchsyms)
2340 (setq sorted-patchsyms (cons name sorted-patchsyms))))
2341 nil)
2342 stgit-ewoc)
2343 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2344
2345 (unless allow-duplicates
2346 (unless (= (length patchsyms) (length sorted-patchsyms))
2347 (error "Internal error")))
2348
2349 sorted-patchsyms))
2350
2351 (defun stgit-move-patches (patchsyms target-patch)
2352 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2353 If TARGET-PATCH is :bottom or :top, move the patches to the
2354 bottom or top of the stack, respectively.
2355
2356 Interactively, move the marked patches to where the point is."
2357 (interactive (list stgit-marked-patches
2358 (stgit-move-patches-target)))
2359 (stgit-assert-mode)
2360 (unless patchsyms
2361 (error "Need at least one patch to move"))
2362
2363 (unless target-patch
2364 (error "Point not at a patch"))
2365
2366 ;; need to have patchsyms sorted by position in the stack
2367 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2368 (stgit-capture-output nil
2369 (if (eq target-patch :top)
2370 (apply 'stgit-run "float" "--" sorted-patchsyms)
2371 (apply 'stgit-run
2372 "sink"
2373 (append (unless (eq target-patch :bottom)
2374 (list "--to" target-patch))
2375 '("--")
2376 sorted-patchsyms)))))
2377 (stgit-reload))
2378
2379 (defun stgit-squash (patchsyms)
2380 "Squash the patches in PATCHSYMS.
2381 Interactively, squash the marked patches.
2382
2383 Unless there are any conflicts, the patches will be merged into
2384 one patch, which will occupy the same spot in the series as the
2385 deepest patch had before the squash."
2386 (interactive (list stgit-marked-patches))
2387 (stgit-assert-mode)
2388 (when (< (length patchsyms) 2)
2389 (error "Need at least two patches to squash"))
2390 (let ((stgit-buffer (current-buffer))
2391 (edit-buf (get-buffer-create "*StGit edit*"))
2392 (dir default-directory)
2393 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2394 (log-edit 'stgit-confirm-squash t nil edit-buf)
2395 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2396 (setq default-directory dir)
2397 (let ((result (let ((standard-output edit-buf))
2398 (save-excursion
2399 (apply 'stgit-run-silent "squash"
2400 "--save-template=-" "--" sorted-patchsyms)))))
2401
2402 ;; stg squash may have reordered the patches or caused conflicts
2403 (with-current-buffer stgit-buffer
2404 (stgit-reload))
2405
2406 (unless (eq 0 result)
2407 (fundamental-mode)
2408 (rename-buffer "*StGit error*")
2409 (resize-temp-buffer-window)
2410 (switch-to-buffer-other-window stgit-buffer)
2411 (error "stg squash failed")))))
2412
2413 (defun stgit-confirm-squash ()
2414 (interactive)
2415 (let ((file (make-temp-file "stgit-edit-")))
2416 (write-region (point-min) (point-max) file)
2417 (stgit-capture-output nil
2418 (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms))
2419 (with-current-buffer log-edit-parent-buffer
2420 (stgit-clear-marks)
2421 ;; Go to first marked patch and stay there
2422 (goto-char (point-min))
2423 (re-search-forward (concat "^[>+-]\\*") nil t)
2424 (move-to-column goal-column)
2425 (let ((pos (point)))
2426 (stgit-reload)
2427 (goto-char pos)))))
2428
2429 (defun stgit-help ()
2430 "Display help for the StGit mode."
2431 (interactive)
2432 (describe-function 'stgit-mode))
2433
2434 (defun stgit-execute-process-sentinel (process sentinel)
2435 (let (old-sentinel stgit-buf)
2436 (with-current-buffer (process-buffer process)
2437 (setq old-sentinel old-process-sentinel
2438 stgit-buf stgit-buffer))
2439 (and (memq (process-status process) '(exit signal))
2440 (buffer-live-p stgit-buf)
2441 (with-current-buffer stgit-buf
2442 (stgit-reload)))
2443 (funcall old-sentinel process sentinel)))
2444
2445 (defun stgit-execute-process-filter (process output)
2446 (with-current-buffer (process-buffer process)
2447 (let* ((old-point (point))
2448 (pmark (process-mark process))
2449 (insert-at (marker-position pmark))
2450 (at-pmark (= insert-at old-point)))
2451 (goto-char insert-at)
2452 (insert-before-markers output)
2453 (comint-carriage-motion insert-at (point))
2454 (set-marker pmark (point))
2455 (unless at-pmark
2456 (goto-char old-point)))))
2457
2458 (defun stgit-execute (&optional git-mode)
2459 "Prompt for an stg command to execute in a shell.
2460
2461 The names of any marked patches or the patch at point are
2462 inserted in the command to be executed.
2463
2464 With a prefix argument, or if GIT-MODE is non-nil, insert SHA1
2465 sums of the marked patches instead, and prompt for a git command.
2466
2467 If the command ends in an ampersand, run it asynchronously.
2468
2469 When the command has finished, reload the stgit buffer."
2470 (interactive "P")
2471 (stgit-assert-mode)
2472 (let* ((patches (stgit-sort-patches
2473 (stgit-patches-marked-or-at-point nil 'allow-committed)))
2474 (patch-names (mapcar 'symbol-name patches))
2475 (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names))
2476 (defaultcmd (if patches
2477 (concat (if git-mode "git" "stg") " "
2478 (and hyphens "-- ")
2479 (mapconcat (if git-mode 'stgit-id 'identity)
2480 patch-names " "))
2481 "stg "))
2482 (cmd (read-from-minibuffer "Shell command: " (cons defaultcmd 5)
2483 nil nil 'shell-command-history))
2484 (async (string-match "&[ \t]*\\'" cmd))
2485 (buffer (get-buffer-create
2486 (if async
2487 "*Async Shell Command*"
2488 "*Shell Command Output*"))))
2489 ;; cannot use minibuffer as stgit-reload would overwrite it; if we
2490 ;; show the buffer, shell-command will not use the minibuffer
2491 (display-buffer buffer)
2492 (shell-command cmd)
2493 (if async
2494 (let ((old-buffer (current-buffer)))
2495 (with-current-buffer buffer
2496 (let ((process (get-buffer-process buffer)))
2497 (set (make-local-variable 'old-process-sentinel)
2498 (process-sentinel process))
2499 (set (make-local-variable 'stgit-buffer)
2500 old-buffer)
2501 (set-process-filter process 'stgit-execute-process-filter)
2502 (set-process-sentinel process 'stgit-execute-process-sentinel))))
2503 (with-current-buffer buffer
2504 (comint-carriage-motion (point-min) (point-max)))
2505 (shrink-window-if-larger-than-buffer (get-buffer-window buffer))
2506 (stgit-reload))))
2507
2508 (defun stgit-undo-or-redo (redo hard)
2509 "Run stg undo or, if REDO is non-nil, stg redo.
2510
2511 If HARD is non-nil, use the --hard flag."
2512 (stgit-assert-mode)
2513 (let ((cmd (if redo "redo" "undo")))
2514 (stgit-capture-output nil
2515 (if arg
2516 (when (or (and (stgit-index-empty-p)
2517 (stgit-work-tree-empty-p))
2518 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2519 cmd)))
2520 (stgit-run cmd "--hard"))
2521 (stgit-run cmd))))
2522 (stgit-reload))
2523
2524 (defun stgit-undo (&optional arg)
2525 "Run stg undo.
2526 With prefix argument, run it with the --hard flag.
2527
2528 See also `stgit-redo'."
2529 (interactive "P")
2530 (stgit-undo-or-redo nil arg))
2531
2532 (defun stgit-redo (&optional arg)
2533 "Run stg redo.
2534 With prefix argument, run it with the --hard flag.
2535
2536 See also `stgit-undo'."
2537 (interactive "P")
2538 (stgit-undo-or-redo t arg))
2539
2540 (defun stgit-refresh (&optional arg)
2541 "Run stg refresh.
2542 If the index contains any changes, only refresh from index.
2543
2544 With prefix argument, refresh the marked patch or the patch under point."
2545 (interactive "P")
2546 (stgit-assert-mode)
2547 (let ((patchargs (if arg
2548 (let ((patches (stgit-patches-marked-or-at-point t t)))
2549 (when (> (length patches) 1)
2550 (error "Too many patches marked"))
2551 (cons "-p" patches))
2552 nil)))
2553 (unless (stgit-index-empty-p)
2554 (setq patchargs (cons "--index" patchargs)))
2555 (stgit-capture-output nil
2556 (apply 'stgit-run "refresh" patchargs))
2557 (stgit-refresh-git-status))
2558 (stgit-reload))
2559
2560 (defvar stgit-show-worktree nil
2561 "If nil, inhibit showing work tree and index in the stgit buffer.
2562
2563 See also `stgit-show-worktree-mode'.")
2564
2565 (defvar stgit-show-ignored nil
2566 "If nil, inhibit showing files ignored by git.")
2567
2568 (defvar stgit-show-unknown nil
2569 "If nil, inhibit showing files not registered with git.")
2570
2571 (defvar stgit-show-patch-names t
2572 "If nil, inhibit showing patch names.")
2573
2574 (defvar stgit-show-committed nil
2575 "If nil, inhibit showing recent commits.")
2576
2577 (defvar stgit-show-svn nil
2578 "If nil, inhibit showing git svn information.")
2579
2580 (defvar stgit-committed-count nil
2581 "The number of recent commits to show.")
2582
2583 (defmacro stgit-define-toggle-view (sym desc help)
2584 (declare (indent 1))
2585 (let* ((name (symbol-name sym))
2586 (fun (intern (concat "stgit-toggle-" name)))
2587 (flag (intern (concat "stgit-show-" name))))
2588 ;; make help-follow find the correct function
2589 `(put (quote ,fun) 'definition-name 'stgit-define-toggle-view)
2590 `(defun ,fun (&optional arg)
2591 ,help
2592 (interactive "P")
2593 (stgit-assert-mode)
2594 (setq ,flag (if arg
2595 (> (prefix-numeric-value arg) 0)
2596 (not ,flag)))
2597 (stgit-reload (format "%s %s" (if ,flag "Showing" "Hiding") ,desc)))))
2598
2599 (stgit-define-toggle-view worktree
2600 "work tree and index"
2601 "Toggle the visibility of the work tree.
2602 With ARG, show the work tree if ARG is positive.
2603
2604 Its initial setting is controlled by `stgit-default-show-worktree'.
2605
2606 `stgit-show-worktree-mode' controls where on screen the index and
2607 work tree will show up.")
2608
2609 (stgit-define-toggle-view ignored
2610 "ignored files"
2611 "Toggle the visibility of files ignored by git in the work
2612 tree. With ARG, show these files if ARG is positive.
2613
2614 Its initial setting is controlled by `stgit-default-show-ignored'.
2615
2616 Use \\[stgit-toggle-worktree] to show the work tree.")
2617
2618 (stgit-define-toggle-view unknown
2619 "unknown files"
2620 "Toggle the visibility of files not registered with git in the
2621 work tree. With ARG, show these files if ARG is positive.
2622
2623 Its initial setting is controlled by `stgit-default-show-unknown'.
2624
2625 Use \\[stgit-toggle-worktree] to show the work tree.")
2626
2627 (stgit-define-toggle-view patch-names
2628 "patch names"
2629 "Toggle the visibility of patch names. With ARG, show patch names
2630 if ARG is positive.
2631
2632 The initial setting is controlled by `stgit-default-show-patch-names'.")
2633
2634 (stgit-define-toggle-view svn
2635 "subversion revisions"
2636 "Toggle showing subversion information from git svn. With ARG,
2637 show svn information if ARG is positive.
2638
2639 The initial setting is controlled by `stgit-default-show-svn'.")
2640
2641 (defun stgit-toggle-committed (&optional arg)
2642 "Toggle the visibility of historical git commits.
2643 With ARG, set the number of commits to show to ARG, and disable
2644 them if ARG is zero.
2645
2646 The initial setting is controlled by `stgit-default-show-committed'."
2647 (interactive "P")
2648 (stgit-assert-mode)
2649 (if (null arg)
2650 (setq stgit-show-committed (not stgit-show-committed))
2651 (let ((n (prefix-numeric-value arg)))
2652 (setq stgit-show-committed (> n 0))
2653 (setq stgit-committed-count n)))
2654 (stgit-reload (format "%s historical commits"
2655 (if (and stgit-show-committed
2656 (> stgit-committed-count 0))
2657 "Showing"
2658 "Hiding"))))
2659
2660 (provide 'stgit)