stgit.el: Use format-spec when formatting file information
[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 'ewoc)
18 (require 'easymenu)
19 (require 'format-spec)
20
21 (defun stgit-set-default (symbol value)
22 "Set default value of SYMBOL to VALUE using `set-default' and
23 reload all StGit buffers."
24 (set-default symbol value)
25 (dolist (buf (buffer-list))
26 (with-current-buffer buf
27 (when (eq major-mode 'stgit-mode)
28 (stgit-reload)))))
29
30 (defgroup stgit nil
31 "A user interface for the StGit patch maintenance tool."
32 :group 'tools
33 :link '(function-link stgit)
34 :link '(url-link "http://www.procode.org/stgit/"))
35
36 (defcustom stgit-abbreviate-copies-and-renames t
37 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
38 instead of \"dir/old/file -> dir/new/file\"."
39 :type 'boolean
40 :group 'stgit
41 :set 'stgit-set-default)
42
43 (defcustom stgit-default-show-worktree t
44 "Set to non-nil to by default show the working tree in a new stgit buffer.
45
46 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
47 :type 'boolean
48 :group 'stgit
49 :link '(variable-link stgit-show-worktree))
50
51 (defcustom stgit-find-copies-harder nil
52 "Try harder to find copied files when listing patches.
53
54 When not nil, runs git diff-tree with the --find-copies-harder
55 flag, which reduces performance."
56 :type 'boolean
57 :group 'stgit
58 :set 'stgit-set-default)
59
60 (defcustom stgit-show-worktree-mode 'center
61 "This variable controls where the \"Index\" and \"Work tree\"
62 will be shown on in the buffer.
63
64 It can be set to 'top (above all patches), 'center (show between
65 applied and unapplied patches), and 'bottom (below all patches)."
66 :type '(radio (const :tag "above all patches (top)" top)
67 (const :tag "between applied and unapplied patches (center)"
68 center)
69 (const :tag "below all patches (bottom)" bottom))
70 :group 'stgit
71 :link '(variable-link stgit-show-worktree)
72 :set 'stgit-set-default)
73
74 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
75 "The format string used to format patch lines.
76 The format string is passed to `format-spec' and the following
77 format characters are recognized:
78
79 %s - A '+', '-', '>' or space, depending on whether the patch is
80 applied, unapplied, top, or something else.
81
82 %m - An asterisk if the patch is marked, and a space otherwise.
83
84 %n - The patch name.
85
86 %e - The string \"(empty) \" if the patch is empty.
87
88 %d - The short patch description."
89 :type 'string
90 :group 'stgit
91 :set 'stgit-set-default)
92
93 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
94 "The format string used to format file lines.
95 The format string is passed to `format-spec' and the following
96 format characters are recognized:
97
98 %s - A string describing the status of the file.
99
100 %m - Mode change information
101
102 %n - The file name.
103
104 %c - A description of file changes."
105 :type 'string
106 :group 'stgit
107 :set 'stgit-set-default)
108
109 (defface stgit-branch-name-face
110 '((t :inherit bold))
111 "The face used for the StGit branch name"
112 :group 'stgit)
113
114 (defface stgit-top-patch-face
115 '((((background dark)) (:weight bold :foreground "yellow"))
116 (((background light)) (:weight bold :foreground "purple"))
117 (t (:weight bold)))
118 "The face used for the top patch names"
119 :group 'stgit)
120
121 (defface stgit-applied-patch-face
122 '((((background dark)) (:foreground "light yellow"))
123 (((background light)) (:foreground "purple"))
124 (t ()))
125 "The face used for applied patch names"
126 :group 'stgit)
127
128 (defface stgit-unapplied-patch-face
129 '((((background dark)) (:foreground "gray80"))
130 (((background light)) (:foreground "orchid"))
131 (t ()))
132 "The face used for unapplied patch names"
133 :group 'stgit)
134
135 (defface stgit-description-face
136 '((((background dark)) (:foreground "tan"))
137 (((background light)) (:foreground "dark red")))
138 "The face used for StGit descriptions"
139 :group 'stgit)
140
141 (defface stgit-index-work-tree-title-face
142 '((((supports :slant italic)) :slant italic)
143 (t :inherit bold))
144 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
145 :group 'stgit)
146
147 (defface stgit-unmerged-file-face
148 '((((class color) (background light)) (:foreground "red" :bold t))
149 (((class color) (background dark)) (:foreground "red" :bold t)))
150 "StGit mode face used for unmerged file status"
151 :group 'stgit)
152
153 (defface stgit-unknown-file-face
154 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
155 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
156 "StGit mode face used for unknown file status"
157 :group 'stgit)
158
159 (defface stgit-ignored-file-face
160 '((((class color) (background light)) (:foreground "grey60"))
161 (((class color) (background dark)) (:foreground "grey40")))
162 "StGit mode face used for ignored files")
163
164 (defface stgit-file-permission-face
165 '((((class color) (background light)) (:foreground "green" :bold t))
166 (((class color) (background dark)) (:foreground "green" :bold t)))
167 "StGit mode face used for permission changes."
168 :group 'stgit)
169
170 (defface stgit-modified-file-face
171 '((((class color) (background light)) (:foreground "purple"))
172 (((class color) (background dark)) (:foreground "salmon")))
173 "StGit mode face used for modified file status"
174 :group 'stgit)
175
176 (defun stgit (dir)
177 "Manage StGit patches for the tree in DIR.
178
179 See `stgit-mode' for commands available."
180 (interactive "DDirectory: \n")
181 (switch-to-stgit-buffer (git-get-top-dir dir))
182 (stgit-reload))
183
184 (defun stgit-assert-mode ()
185 "Signal an error if not in an StGit buffer."
186 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
187
188 (unless (fboundp 'git-get-top-dir)
189 (defun git-get-top-dir (dir)
190 "Retrieve the top-level directory of a git tree."
191 (let ((cdup (with-output-to-string
192 (with-current-buffer standard-output
193 (cd dir)
194 (unless (eq 0 (call-process "git" nil t nil
195 "rev-parse" "--show-cdup"))
196 (error "Cannot find top-level git tree for %s" dir))))))
197 (expand-file-name (concat (file-name-as-directory dir)
198 (car (split-string cdup "\n")))))))
199
200 (defun stgit-refresh-git-status (&optional dir)
201 "If it exists, refresh the `git-status' buffer belonging to
202 directory DIR or `default-directory'"
203 (when (and (fboundp 'git-find-status-buffer)
204 (fboundp 'git-refresh-status))
205 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
206 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
207 (when git-status-buffer
208 (with-current-buffer git-status-buffer
209 (git-refresh-status))))))
210
211 (defun stgit-find-buffer (dir)
212 "Return the buffer displaying StGit patches for DIR, or nil if none."
213 (setq dir (file-name-as-directory dir))
214 (let ((buffers (buffer-list)))
215 (while (and buffers
216 (not (with-current-buffer (car buffers)
217 (and (eq major-mode 'stgit-mode)
218 (string= default-directory dir)))))
219 (setq buffers (cdr buffers)))
220 (and buffers (car buffers))))
221
222 (defun switch-to-stgit-buffer (dir)
223 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
224 (setq dir (file-name-as-directory dir))
225 (let ((buffer (stgit-find-buffer dir)))
226 (switch-to-buffer (or buffer
227 (create-stgit-buffer dir)))))
228
229 (defstruct (stgit-patch)
230 status name desc empty files-ewoc)
231
232 (defun stgit-patch-display-name (patch)
233 (let ((name (stgit-patch-name patch)))
234 (case name
235 (:index "Index")
236 (:work "Work Tree")
237 (t (symbol-name name)))))
238
239 (defun stgit-patch-pp (patch)
240 (let* ((status (stgit-patch-status patch))
241 (start (point))
242 (name (stgit-patch-name patch))
243 (face (cdr (assq status stgit-patch-status-face-alist)))
244 (spec (format-spec-make
245 ?s (case status
246 ('applied "+")
247 ('top ">")
248 ('unapplied "-")
249 (t " "))
250 ?m (if (memq name stgit-marked-patches)
251 "*" " ")
252 ?n (propertize (stgit-patch-display-name patch)
253 'face face
254 'syntax-table (string-to-syntax "w"))
255 ?e (if (stgit-patch-empty patch) "(empty) " "")
256 ?d (propertize (or (stgit-patch-desc patch) "")
257 'face 'stgit-description-face))))
258
259 (insert (format-spec stgit-patch-line-format spec) "\n")
260 (put-text-property start (point) 'entry-type 'patch)
261 (when (memq name stgit-expanded-patches)
262 (stgit-insert-patch-files patch))
263 (put-text-property start (point) 'patch-data patch)))
264
265 (defun create-stgit-buffer (dir)
266 "Create a buffer for showing StGit patches.
267 Argument DIR is the repository path."
268 (let ((buf (create-file-buffer (concat dir "*stgit*")))
269 (inhibit-read-only t))
270 (with-current-buffer buf
271 (setq default-directory dir)
272 (stgit-mode)
273 (set (make-local-variable 'stgit-ewoc)
274 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
275 (setq buffer-read-only t))
276 buf))
277
278 (defmacro stgit-capture-output (name &rest body)
279 "Capture StGit output and, if there was any output, show it in a window
280 at the end.
281 Returns nil if there was no output."
282 (declare (debug ([&or stringp null] body))
283 (indent 1))
284 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
285 (stgit-dir default-directory)
286 (inhibit-read-only t))
287 (with-current-buffer output-buf
288 (erase-buffer)
289 (setq default-directory stgit-dir)
290 (setq buffer-read-only t))
291 (let ((standard-output output-buf))
292 ,@body)
293 (with-current-buffer output-buf
294 (set-buffer-modified-p nil)
295 (setq buffer-read-only t)
296 (if (< (point-min) (point-max))
297 (display-buffer output-buf t)))))
298
299 (defun stgit-make-run-args (args)
300 "Return a copy of ARGS with its elements converted to strings."
301 (mapcar (lambda (x)
302 ;; don't use (format "%s" ...) to limit type errors
303 (cond ((stringp x) x)
304 ((integerp x) (number-to-string x))
305 ((symbolp x) (symbol-name x))
306 (t
307 (error "Bad element in stgit-make-run-args args: %S" x))))
308 args))
309
310 (defun stgit-run-silent (&rest args)
311 (setq args (stgit-make-run-args args))
312 (apply 'call-process "stg" nil standard-output nil args))
313
314 (defun stgit-run (&rest args)
315 (setq args (stgit-make-run-args args))
316 (let ((msgcmd (mapconcat #'identity args " ")))
317 (message "Running stg %s..." msgcmd)
318 (apply 'call-process "stg" nil standard-output nil args)
319 (message "Running stg %s...done" msgcmd)))
320
321 (defun stgit-run-git (&rest args)
322 (setq args (stgit-make-run-args args))
323 (let ((msgcmd (mapconcat #'identity args " ")))
324 (message "Running git %s..." msgcmd)
325 (apply 'call-process "git" nil standard-output nil args)
326 (message "Running git %s...done" msgcmd)))
327
328 (defun stgit-run-git-silent (&rest args)
329 (setq args (stgit-make-run-args args))
330 (apply 'call-process "git" nil standard-output nil args))
331
332 (defun stgit-index-empty-p ()
333 "Returns non-nil if the index contains no changes from HEAD."
334 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
335
336 (defun stgit-work-tree-empty-p ()
337 "Returns non-nil if the work tree contains no changes from index."
338 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
339
340 (defvar stgit-index-node)
341 (defvar stgit-worktree-node)
342
343 (defun stgit-refresh-index ()
344 (when stgit-index-node
345 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
346
347 (defun stgit-refresh-worktree ()
348 (when stgit-worktree-node
349 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
350
351 (defun stgit-run-series-insert-index (ewoc)
352 (setq index-node (cons ewoc (ewoc-enter-last ewoc
353 (make-stgit-patch
354 :status 'index
355 :name :index
356 :desc nil
357 :empty nil)))
358 worktree-node (cons ewoc (ewoc-enter-last ewoc
359 (make-stgit-patch
360 :status 'work
361 :name :work
362 :desc nil
363 :empty nil)))))
364
365 (defun stgit-run-series (ewoc)
366 (setq stgit-index-node nil
367 stgit-worktree-node nil)
368 (let ((inserted-index (not stgit-show-worktree))
369 index-node
370 worktree-node
371 all-patchsyms)
372 (with-temp-buffer
373 (let* ((standard-output (current-buffer))
374 (exit-status (stgit-run-silent "series"
375 "--description" "--empty")))
376 (goto-char (point-min))
377 (if (not (zerop exit-status))
378 (cond ((looking-at "stg series: \\(.*\\)")
379 (setq inserted-index t)
380 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
381 (substitute-command-keys
382 "-- not initialized; run \\[stgit-init]")))
383 ((looking-at ".*")
384 (error "Error running stg: %s"
385 (match-string 0))))
386 (while (not (eobp))
387 (unless (looking-at
388 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
389 (error "Syntax error in output from stg series"))
390 (let* ((state-str (match-string 2))
391 (state (cond ((string= state-str ">") 'top)
392 ((string= state-str "+") 'applied)
393 ((string= state-str "-") 'unapplied)))
394 (name (intern (match-string 4)))
395 (desc (match-string 5))
396 (empty (string= (match-string 1) "0")))
397 (unless inserted-index
398 (when (or (eq stgit-show-worktree-mode 'top)
399 (and (eq stgit-show-worktree-mode 'center)
400 (eq state 'unapplied)))
401 (setq inserted-index t)
402 (stgit-run-series-insert-index ewoc)))
403 (setq all-patchsyms (cons name all-patchsyms))
404 (ewoc-enter-last ewoc
405 (make-stgit-patch
406 :status state
407 :name name
408 :desc desc
409 :empty empty)))
410 (forward-line 1))))
411 (unless inserted-index
412 (stgit-run-series-insert-index ewoc)))
413 (setq stgit-index-node index-node
414 stgit-worktree-node worktree-node
415 stgit-marked-patches (intersection stgit-marked-patches
416 all-patchsyms))))
417
418 (defun stgit-reload ()
419 "Update the contents of the StGit buffer."
420 (interactive)
421 (stgit-assert-mode)
422 (let ((inhibit-read-only t)
423 (curline (line-number-at-pos))
424 (curpatch (stgit-patch-name-at-point))
425 (curfile (stgit-patched-file-at-point)))
426 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
427 (ewoc-set-hf stgit-ewoc
428 (concat "Branch: "
429 (propertize
430 (substring (with-output-to-string
431 (stgit-run-silent "branch"))
432 0 -1)
433 'face 'stgit-branch-name-face)
434 "\n\n")
435 (if stgit-show-worktree
436 "--"
437 (propertize
438 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
439 shows the working tree\n")
440 'face 'stgit-description-face)))
441 (stgit-run-series stgit-ewoc)
442 (if curpatch
443 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
444 (goto-line curline)))
445 (stgit-refresh-git-status))
446
447 (defconst stgit-file-status-code-strings
448 (mapcar (lambda (arg)
449 (cons (car arg)
450 (propertize (cadr arg) 'face (car (cddr arg)))))
451 '((add "Added" stgit-modified-file-face)
452 (copy "Copied" stgit-modified-file-face)
453 (delete "Deleted" stgit-modified-file-face)
454 (modify "Modified" stgit-modified-file-face)
455 (rename "Renamed" stgit-modified-file-face)
456 (mode-change "Mode change" stgit-modified-file-face)
457 (unmerged "Unmerged" stgit-unmerged-file-face)
458 (unknown "Unknown" stgit-unknown-file-face)
459 (ignore "Ignored" stgit-ignored-file-face)))
460 "Alist of code symbols to description strings")
461
462 (defconst stgit-patch-status-face-alist
463 '((applied . stgit-applied-patch-face)
464 (top . stgit-top-patch-face)
465 (unapplied . stgit-unapplied-patch-face)
466 (index . stgit-index-work-tree-title-face)
467 (work . stgit-index-work-tree-title-face))
468 "Alist of face to use for a given patch status")
469
470 (defun stgit-file-status-code-as-string (file)
471 "Return stgit status code for FILE as a string"
472 (let* ((code (assq (stgit-file-status file)
473 stgit-file-status-code-strings))
474 (score (stgit-file-cr-score file)))
475 (when code
476 (if (and score (/= score 100))
477 (format "%s %s" (cdr code)
478 (propertize (format "%d%%" score)
479 'face 'stgit-description-face))
480 (cdr code)))))
481
482 (defun stgit-file-status-code (str &optional score)
483 "Return stgit status code from git status string"
484 (let ((code (assoc str '(("A" . add)
485 ("C" . copy)
486 ("D" . delete)
487 ("I" . ignore)
488 ("M" . modify)
489 ("R" . rename)
490 ("T" . mode-change)
491 ("U" . unmerged)
492 ("X" . unknown)))))
493 (setq code (if code (cdr code) 'unknown))
494 (when (stringp score)
495 (if (> (length score) 0)
496 (setq score (string-to-number score))
497 (setq score nil)))
498 (if score (cons code score) code)))
499
500 (defconst stgit-file-type-strings
501 '((#o100 . "file")
502 (#o120 . "symlink")
503 (#o160 . "subproject"))
504 "Alist of names of file types")
505
506 (defun stgit-file-type-string (type)
507 "Return string describing file type TYPE (the high bits of file permission).
508 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
509 (let ((type-str (assoc type stgit-file-type-strings)))
510 (or (and type-str (cdr type-str))
511 (format "unknown type %o" type))))
512
513 (defun stgit-file-type-change-string (old-perm new-perm)
514 "Return string describing file type change from OLD-PERM to NEW-PERM.
515 Cf. `stgit-file-type-string'."
516 (let ((old-type (lsh old-perm -9))
517 (new-type (lsh new-perm -9)))
518 (cond ((= old-type new-type) "")
519 ((zerop new-type) "")
520 ((zerop old-type)
521 (if (= new-type #o100)
522 ""
523 (format "(%s)" (stgit-file-type-string new-type))))
524 (t (format "(%s -> %s)"
525 (stgit-file-type-string old-type)
526 (stgit-file-type-string new-type))))))
527
528 (defun stgit-file-mode-change-string (old-perm new-perm)
529 "Return string describing file mode change from OLD-PERM to NEW-PERM.
530 Cf. `stgit-file-type-change-string'."
531 (setq old-perm (logand old-perm #o777)
532 new-perm (logand new-perm #o777))
533 (if (or (= old-perm new-perm)
534 (zerop old-perm)
535 (zerop new-perm))
536 ""
537 (let* ((modified (logxor old-perm new-perm))
538 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
539 (cond ((zerop modified) "")
540 ((and (zerop not-x-modified)
541 (or (and (eq #o111 (logand old-perm #o111))
542 (propertize "-x" 'face 'stgit-file-permission-face))
543 (and (eq #o111 (logand new-perm #o111))
544 (propertize "+x" 'face
545 'stgit-file-permission-face)))))
546 (t (concat (propertize (format "%o" old-perm)
547 'face 'stgit-file-permission-face)
548 (propertize " -> "
549 'face 'stgit-description-face)
550 (propertize (format "%o" new-perm)
551 'face 'stgit-file-permission-face)))))))
552
553 (defstruct (stgit-file)
554 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
555
556 (defun stgit-describe-copy-or-rename (file)
557 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
558 from to common-head common-tail)
559
560 (when stgit-abbreviate-copies-and-renames
561 (setq from (split-string (stgit-file-cr-from file) "/")
562 to (split-string (stgit-file-cr-to file) "/"))
563
564 (while (and from to (cdr from) (cdr to)
565 (string-equal (car from) (car to)))
566 (setq common-head (cons (car from) common-head)
567 from (cdr from)
568 to (cdr to)))
569 (setq common-head (nreverse common-head)
570 from (nreverse from)
571 to (nreverse to))
572 (while (and from to (cdr from) (cdr to)
573 (string-equal (car from) (car to)))
574 (setq common-tail (cons (car from) common-tail)
575 from (cdr from)
576 to (cdr to)))
577 (setq from (nreverse from)
578 to (nreverse to)))
579
580 (if (or common-head common-tail)
581 (concat (if common-head
582 (mapconcat #'identity common-head "/")
583 "")
584 (if common-head "/" "")
585 (propertize "{" 'face 'stgit-description-face)
586 (mapconcat #'identity from "/")
587 arrow
588 (mapconcat #'identity to "/")
589 (propertize "}" 'face 'stgit-description-face)
590 (if common-tail "/" "")
591 (if common-tail
592 (mapconcat #'identity common-tail "/")
593 ""))
594 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
595
596 (defun stgit-file-pp (file)
597 (let ((start (point))
598 (spec (format-spec-make
599 ?s (stgit-file-status-code-as-string file)
600 ?m (stgit-file-mode-change-string
601 (stgit-file-old-perm file)
602 (stgit-file-new-perm file))
603 ?n (if (stgit-file-copy-or-rename file)
604 (stgit-describe-copy-or-rename file)
605 (stgit-file-file file))
606 ?c (propertize (stgit-file-type-change-string
607 (stgit-file-old-perm file)
608 (stgit-file-new-perm file))
609 'face 'stgit-description-face))))
610 (insert (format-spec stgit-file-line-format spec) "\n")
611 (add-text-properties start (point)
612 (list 'entry-type 'file
613 'file-data file))))
614
615 (defun stgit-find-copies-harder-diff-arg ()
616 "Return the flag to use with `git-diff' depending on the
617 `stgit-find-copies-harder' flag."
618 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
619
620 (defun stgit-insert-ls-files (args file-flag)
621 (let ((start (point)))
622 (apply 'stgit-run-git
623 (append '("ls-files" "--exclude-standard" "-z") args))
624 (goto-char start)
625 (while (looking-at "\\([^\0]*\\)\0")
626 (let ((name-len (- (match-end 0) (match-beginning 0))))
627 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
628 (forward-char name-len)))))
629
630 (defun stgit-insert-patch-files (patch)
631 "Expand (show modification of) the patch PATCH after the line
632 at point."
633 (let* ((patchsym (stgit-patch-name patch))
634 (end (point-marker))
635 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
636 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
637 (set-marker-insertion-type end t)
638 (setf (stgit-patch-files-ewoc patch) ewoc)
639 (with-temp-buffer
640 (let ((standard-output (current-buffer)))
641 (apply 'stgit-run-git
642 (cond ((eq patchsym :work)
643 `("diff-files" "-0" ,@args))
644 ((eq patchsym :index)
645 `("diff-index" ,@args "--cached" "HEAD"))
646 (t
647 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
648
649 (when (and (eq patchsym :work))
650 (when stgit-show-ignored
651 (stgit-insert-ls-files '("--ignored" "--others") "I"))
652 (when stgit-show-unknown
653 (stgit-insert-ls-files '("--others") "X"))
654 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
655 (point-min) (point-max)))
656
657 (goto-char (point-min))
658 (unless (or (eobp) (memq patchsym '(:work :index)))
659 (forward-char 41))
660 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
661 (let ((old-perm (string-to-number (match-string 1) 8))
662 (new-perm (string-to-number (match-string 2) 8)))
663 (goto-char (match-end 0))
664 (let ((file
665 (cond ((looking-at
666 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
667 (let* ((patch-status (stgit-patch-status patch))
668 (file-subexp (if (eq patch-status 'unapplied)
669 3
670 4))
671 (file (match-string file-subexp)))
672 (make-stgit-file
673 :old-perm old-perm
674 :new-perm new-perm
675 :copy-or-rename t
676 :cr-score (string-to-number (match-string 2))
677 :cr-from (match-string 3)
678 :cr-to (match-string 4)
679 :status (stgit-file-status-code
680 (match-string 1))
681 :file file)))
682 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
683 (make-stgit-file
684 :old-perm old-perm
685 :new-perm new-perm
686 :copy-or-rename nil
687 :cr-score nil
688 :cr-from nil
689 :cr-to nil
690 :status (stgit-file-status-code
691 (match-string 1))
692 :file (match-string 2))))))
693 (goto-char (match-end 0))
694 (ewoc-enter-last ewoc file))))
695
696 (unless (ewoc-nth ewoc 0)
697 (ewoc-set-hf ewoc ""
698 (concat " "
699 (propertize "<no files>"
700 'face 'stgit-description-face)
701 "\n")))))
702 (goto-char end)))
703
704 (defun stgit-find-file (&optional other-window)
705 (let* ((file (or (stgit-patched-file-at-point)
706 (error "No file at point")))
707 (filename (expand-file-name (stgit-file-file file))))
708 (unless (file-exists-p filename)
709 (error "File does not exist"))
710 (funcall (if other-window 'find-file-other-window 'find-file)
711 filename)
712 (when (eq (stgit-file-status file) 'unmerged)
713 (smerge-mode 1))))
714
715 (defun stgit-expand (&optional patches collapse)
716 "Show the contents of marked patches, or the patch at point.
717
718 See also `stgit-collapse'.
719
720 Non-interactively, operate on PATCHES, and collapse instead of
721 expand if COLLAPSE is not nil."
722 (interactive (list (stgit-patches-marked-or-at-point t)))
723 (stgit-assert-mode)
724 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
725 patches stgit-expanded-patches)))
726 (setq stgit-expanded-patches
727 (if collapse
728 (set-difference stgit-expanded-patches patches-diff)
729 (append stgit-expanded-patches patches-diff)))
730 (ewoc-map #'(lambda (patch)
731 (memq (stgit-patch-name patch) patches-diff))
732 stgit-ewoc))
733 (move-to-column (stgit-goal-column)))
734
735 (defun stgit-collapse (&optional patches)
736 "Hide the contents of marked patches, or the patch at point.
737
738 See also `stgit-expand'."
739 (interactive (list (stgit-patches-marked-or-at-point t)))
740 (stgit-assert-mode)
741 (stgit-expand patches t))
742
743 (defun stgit-select-patch ()
744 (let ((patchname (stgit-patch-name-at-point)))
745 (stgit-expand (list patchname)
746 (memq patchname stgit-expanded-patches))))
747
748 (defun stgit-select ()
749 "With point on a patch, toggle showing files in the patch.
750
751 With point on a file, open the associated file. Opens the target
752 file for (applied) copies and renames."
753 (interactive)
754 (stgit-assert-mode)
755 (case (get-text-property (point) 'entry-type)
756 ('patch
757 (stgit-select-patch))
758 ('file
759 (stgit-find-file))
760 (t
761 (error "No patch or file on line"))))
762
763 (defun stgit-find-file-other-window ()
764 "Open file at point in other window"
765 (interactive)
766 (stgit-assert-mode)
767 (stgit-find-file t))
768
769 (defun stgit-find-file-merge ()
770 "Open file at point and merge it using `smerge-ediff'."
771 (interactive)
772 (stgit-assert-mode)
773 (stgit-find-file t)
774 (smerge-ediff))
775
776 (defun stgit-quit ()
777 "Hide the stgit buffer."
778 (interactive)
779 (stgit-assert-mode)
780 (bury-buffer))
781
782 (defun stgit-git-status ()
783 "Show status using `git-status'."
784 (interactive)
785 (stgit-assert-mode)
786 (unless (fboundp 'git-status)
787 (error "The stgit-git-status command requires git-status"))
788 (let ((dir default-directory))
789 (save-selected-window
790 (pop-to-buffer nil)
791 (git-status dir))))
792
793 (defun stgit-goal-column ()
794 "Return goal column for the current line"
795 (case (get-text-property (point) 'entry-type)
796 ('patch 2)
797 ('file 4)
798 (t 0)))
799
800 (defun stgit-next-line (&optional arg)
801 "Move cursor vertically down ARG lines"
802 (interactive "p")
803 (stgit-assert-mode)
804 (next-line arg)
805 (move-to-column (stgit-goal-column)))
806
807 (defun stgit-previous-line (&optional arg)
808 "Move cursor vertically up ARG lines"
809 (interactive "p")
810 (stgit-assert-mode)
811 (previous-line arg)
812 (move-to-column (stgit-goal-column)))
813
814 (defun stgit-next-patch (&optional arg)
815 "Move cursor down ARG patches."
816 (interactive "p")
817 (stgit-assert-mode)
818 (ewoc-goto-next stgit-ewoc (or arg 1))
819 (move-to-column goal-column))
820
821 (defun stgit-previous-patch (&optional arg)
822 "Move cursor up ARG patches."
823 (interactive "p")
824 (stgit-assert-mode)
825 (ewoc-goto-prev stgit-ewoc (or arg 1))
826 (move-to-column goal-column))
827
828 (defvar stgit-mode-hook nil
829 "Run after `stgit-mode' is setup.")
830
831 (defvar stgit-mode-map nil
832 "Keymap for StGit major mode.")
833
834 (unless stgit-mode-map
835 (let ((diff-map (make-sparse-keymap))
836 (toggle-map (make-sparse-keymap)))
837 (suppress-keymap diff-map)
838 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
839 '(("b" . stgit-diff-base)
840 ("c" . stgit-diff-combined)
841 ("m" . stgit-find-file-merge)
842 ("o" . stgit-diff-ours)
843 ("t" . stgit-diff-theirs)))
844 (suppress-keymap toggle-map)
845 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
846 '(("t" . stgit-toggle-worktree)
847 ("i" . stgit-toggle-ignored)
848 ("u" . stgit-toggle-unknown)))
849 (setq stgit-mode-map (make-keymap))
850 (suppress-keymap stgit-mode-map)
851 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
852 `((" " . stgit-mark-down)
853 ("m" . stgit-mark-down)
854 ("\d" . stgit-unmark-up)
855 ("u" . stgit-unmark-down)
856 ("?" . stgit-help)
857 ("h" . stgit-help)
858 ("\C-p" . stgit-previous-line)
859 ("\C-n" . stgit-next-line)
860 ([up] . stgit-previous-line)
861 ([down] . stgit-next-line)
862 ("p" . stgit-previous-patch)
863 ("n" . stgit-next-patch)
864 ("\M-{" . stgit-previous-patch)
865 ("\M-}" . stgit-next-patch)
866 ("s" . stgit-git-status)
867 ("g" . stgit-reload-or-repair)
868 ("r" . stgit-refresh)
869 ("\C-c\C-r" . stgit-rename)
870 ("e" . stgit-edit)
871 ("M" . stgit-move-patches)
872 ("S" . stgit-squash)
873 ("N" . stgit-new)
874 ("c" . stgit-new-and-refresh)
875 ("\C-c\C-c" . stgit-commit)
876 ("\C-c\C-u" . stgit-uncommit)
877 ("U" . stgit-revert)
878 ("R" . stgit-resolve-file)
879 ("\r" . stgit-select)
880 ("+" . stgit-expand)
881 ("-" . stgit-collapse)
882 ("o" . stgit-find-file-other-window)
883 ("i" . stgit-toggle-index)
884 (">" . stgit-push-next)
885 ("<" . stgit-pop-next)
886 ("P" . stgit-push-or-pop)
887 ("G" . stgit-goto)
888 ("=" . stgit-diff)
889 ("D" . stgit-delete)
890 ([?\C-/] . stgit-undo)
891 ("\C-_" . stgit-undo)
892 ([?\C-c ?\C-/] . stgit-redo)
893 ("\C-c\C-_" . stgit-redo)
894 ("B" . stgit-branch)
895 ("\C-c\C-b" . stgit-rebase)
896 ("t" . ,toggle-map)
897 ("d" . ,diff-map)
898 ("q" . stgit-quit))))
899
900 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
901 (and file (eq (stgit-file-status file)
902 'unmerged))))
903 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
904 (easy-menu-define stgit-menu stgit-mode-map
905 "StGit Menu"
906 `("StGit"
907 ["Reload" stgit-reload-or-repair
908 :help "Reload StGit status from disk"]
909 ["Repair" stgit-repair
910 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
911 :help "Repair StGit metadata"]
912 "-"
913 ["Undo" stgit-undo t]
914 ["Redo" stgit-redo t]
915 "-"
916 ["Git status" stgit-git-status :active (fboundp 'git-status)]
917 "-"
918 ["New patch" stgit-new-and-refresh
919 :help "Create a new patch from changes in index or work tree"
920 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
921 ["New empty patch" stgit-new
922 :help "Create a new, empty patch"]
923 ["(Un)mark patch" stgit-toggle-mark
924 :label (if (memq (stgit-patch-name-at-point nil t)
925 stgit-marked-patches)
926 "Unmark patch" "Mark patch")
927 :active (stgit-patch-name-at-point nil t)]
928 ["Expand/collapse patch"
929 (let ((patches (stgit-patches-marked-or-at-point)))
930 (if (member-if ,patch-collapsed-p patches)
931 (stgit-expand patches)
932 (stgit-collapse patches)))
933 :label (if (member-if ,patch-collapsed-p
934 (stgit-patches-marked-or-at-point))
935 "Expand patches"
936 "Collapse patches")
937 :active (stgit-patches-marked-or-at-point)]
938 ["Edit patch" stgit-edit
939 :help "Edit patch comment"
940 :active (stgit-patch-name-at-point nil t)]
941 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
942 ["Push/pop patch" stgit-push-or-pop
943 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
944 (stgit-applied-patchsyms t))
945 "Pop patches" "Push patches")]
946 ["Delete patches" stgit-delete
947 :active (stgit-patches-marked-or-at-point nil t)]
948 "-"
949 ["Move patches" stgit-move-patches
950 :active stgit-marked-patches
951 :help "Move marked patch(es) to point"]
952 ["Squash patches" stgit-squash
953 :active (> (length stgit-marked-patches) 1)
954 :help "Merge marked patches into one"]
955 "-"
956 ["Refresh top patch" stgit-refresh
957 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
958 :help "Refresh the top patch with changes in index or work tree"]
959 ["Refresh this patch" (stgit-refresh t)
960 :keys "\\[universal-argument] \\[stgit-refresh]"
961 :help "Refresh marked patch with changes in index or work tree"
962 :active (and (not (and (stgit-index-empty-p)
963 (stgit-work-tree-empty-p)))
964 (stgit-patch-name-at-point nil t))]
965 "-"
966 ["Find file" stgit-select
967 :active (eq (get-text-property (point) 'entry-type) 'file)]
968 ["Open file" stgit-find-file-other-window
969 :active (eq (get-text-property (point) 'entry-type) 'file)]
970 ["Toggle file index" stgit-toggle-index
971 :active (and (eq (get-text-property (point) 'entry-type) 'file)
972 (memq (stgit-patch-name-at-point) '(:work :index)))
973 :label (if (eq (stgit-patch-name-at-point) :work)
974 "Move change to index"
975 "Move change to work tree")]
976 "-"
977 ["Show diff" stgit-diff
978 :active (get-text-property (point) 'entry-type)]
979 ("Merge"
980 :active (stgit-git-index-unmerged-p)
981 ["Combined diff" stgit-diff-combined
982 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
983 ["Diff against base" stgit-diff-base
984 :help "Show diff against the common base"
985 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
986 ["Diff against ours" stgit-diff-ours
987 :help "Show diff against our branch"
988 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
989 ["Diff against theirs" stgit-diff-theirs
990 :help "Show diff against their branch"
991 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
992 "-"
993 ["Interactive merge" stgit-find-file-merge
994 :help "Interactively merge the file"
995 :active ,at-unmerged-file]
996 ["Resolve file" stgit-resolve-file
997 :help "Mark file conflict as resolved"
998 :active ,at-unmerged-file]
999 )
1000 "-"
1001 ["Show index & work tree" stgit-toggle-worktree :style toggle
1002 :selected stgit-show-worktree]
1003 ["Show unknown files" stgit-toggle-unknown :style toggle
1004 :selected stgit-show-unknown :active stgit-show-worktree]
1005 ["Show ignored files" stgit-toggle-ignored :style toggle
1006 :selected stgit-show-ignored :active stgit-show-worktree]
1007 "-"
1008 ["Switch branches" stgit-branch t
1009 :help "Switch to another branch"]
1010 ["Rebase branch" stgit-rebase t
1011 :help "Rebase the current branch"]
1012 ))))
1013
1014 ;; disable tool bar editing buttons
1015 (put 'stgit-mode 'mode-class 'special)
1016
1017 (defun stgit-mode ()
1018 "Major mode for interacting with StGit.
1019
1020 Start StGit using \\[stgit].
1021
1022 Basic commands:
1023 \\<stgit-mode-map>\
1024 \\[stgit-help] Show this help text
1025 \\[stgit-quit] Hide the StGit buffer
1026 \\[describe-bindings] Show all key bindings
1027
1028 \\[stgit-reload-or-repair] Reload the StGit buffer
1029 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1030
1031 \\[stgit-undo] Undo most recent StGit operation
1032 \\[stgit-redo] Undo recent undo
1033
1034 \\[stgit-git-status] Run `git-status' (if available)
1035
1036 Movement commands:
1037 \\[stgit-previous-line] Move to previous line
1038 \\[stgit-next-line] Move to next line
1039 \\[stgit-previous-patch] Move to previous patch
1040 \\[stgit-next-patch] Move to next patch
1041
1042 \\[stgit-mark-down] Mark patch and move down
1043 \\[stgit-unmark-up] Unmark patch and move up
1044 \\[stgit-unmark-down] Unmark patch and move down
1045
1046 Commands for patches:
1047 \\[stgit-select] Toggle showing changed files in patch
1048 \\[stgit-refresh] Refresh patch with changes in index or work tree
1049 \\[stgit-diff] Show the patch log and diff
1050
1051 \\[stgit-expand] Show changes in marked patches
1052 \\[stgit-collapse] Hide changes in marked patches
1053
1054 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1055 \\[stgit-new] Create a new, empty patch
1056
1057 \\[stgit-rename] Rename patch
1058 \\[stgit-edit] Edit patch description
1059 \\[stgit-delete] Delete patch(es)
1060
1061 \\[stgit-revert] Revert all changes in index or work tree
1062 \\[stgit-toggle-index] Toggle all changes between index and work tree
1063
1064 \\[stgit-push-next] Push next patch onto stack
1065 \\[stgit-pop-next] Pop current patch from stack
1066 \\[stgit-push-or-pop] Push or pop marked patches
1067 \\[stgit-goto] Make patch at point current by popping or pushing
1068
1069 \\[stgit-squash] Squash (meld together) patches
1070 \\[stgit-move-patches] Move marked patches to point
1071
1072 \\[stgit-commit] Commit patch(es)
1073 \\[stgit-uncommit] Uncommit patch(es)
1074
1075 Commands for files:
1076 \\[stgit-select] Open the file in this window
1077 \\[stgit-find-file-other-window] Open the file in another window
1078 \\[stgit-diff] Show the file's diff
1079
1080 \\[stgit-toggle-index] Toggle change between index and work tree
1081 \\[stgit-revert] Revert changes to file
1082
1083 Display commands:
1084 \\[stgit-toggle-worktree] Toggle showing index and work tree
1085 \\[stgit-toggle-unknown] Toggle showing unknown files
1086 \\[stgit-toggle-ignored] Toggle showing ignored files
1087
1088 Commands for diffs:
1089 \\[stgit-diff] Show diff of patch or file
1090 \\[stgit-diff-base] Show diff against the merge base
1091 \\[stgit-diff-ours] Show diff against our branch
1092 \\[stgit-diff-theirs] Show diff against their branch
1093
1094 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1095 ignore space changes.
1096 With two prefix arguments (e.g., \\[universal-argument] \
1097 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1098
1099 Commands for merge conflicts:
1100 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1101 \\[stgit-resolve-file] Mark unmerged file as resolved
1102
1103 Commands for branches:
1104 \\[stgit-branch] Switch to another branch
1105 \\[stgit-rebase] Rebase the current branch
1106
1107 Customization variables:
1108 `stgit-abbreviate-copies-and-renames'
1109 `stgit-default-show-worktree'
1110 `stgit-find-copies-harder'
1111 `stgit-show-worktree-mode'
1112
1113 See also \\[customize-group] for the \"stgit\" group."
1114 (kill-all-local-variables)
1115 (buffer-disable-undo)
1116 (setq mode-name "StGit"
1117 major-mode 'stgit-mode
1118 goal-column 2)
1119 (use-local-map stgit-mode-map)
1120 (set (make-local-variable 'list-buffers-directory) default-directory)
1121 (set (make-local-variable 'stgit-marked-patches) nil)
1122 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1123 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1124 (set (make-local-variable 'stgit-index-node) nil)
1125 (set (make-local-variable 'stgit-worktree-node) nil)
1126 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1127 (set-variable 'truncate-lines 't)
1128 (add-hook 'after-save-hook 'stgit-update-saved-file)
1129 (run-hooks 'stgit-mode-hook))
1130
1131 (defun stgit-update-saved-file ()
1132 (let* ((file (expand-file-name buffer-file-name))
1133 (dir (file-name-directory file))
1134 (gitdir (condition-case nil (git-get-top-dir dir)
1135 (error nil)))
1136 (buffer (and gitdir (stgit-find-buffer gitdir))))
1137 (when buffer
1138 (with-current-buffer buffer
1139 (stgit-refresh-worktree)))))
1140
1141 (defun stgit-add-mark (patchsym)
1142 "Mark the patch PATCHSYM."
1143 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1144
1145 (defun stgit-remove-mark (patchsym)
1146 "Unmark the patch PATCHSYM."
1147 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1148
1149 (defun stgit-clear-marks ()
1150 "Unmark all patches."
1151 (setq stgit-marked-patches '()))
1152
1153 (defun stgit-patch-at-point (&optional cause-error)
1154 (get-text-property (point) 'patch-data))
1155
1156 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1157 "Return the patch name on the current line as a symbol.
1158 If CAUSE-ERROR is not nil, signal an error if none found.
1159 If ONLY-PATCHES is not nil, only allow real patches, and not
1160 index or work tree."
1161 (let ((patch (stgit-patch-at-point)))
1162 (and patch
1163 only-patches
1164 (memq (stgit-patch-status patch) '(work index))
1165 (setq patch nil))
1166 (cond (patch
1167 (stgit-patch-name patch))
1168 (cause-error
1169 (error "No patch on this line")))))
1170
1171 (defun stgit-patched-file-at-point ()
1172 (get-text-property (point) 'file-data))
1173
1174 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1175 "Return the symbols of the marked patches, or the patch on the current line.
1176 If CAUSE-ERRROR is not nil, signal an error if none found.
1177 If ONLY-PATCHES is not nil, do not include index or work tree."
1178 (if stgit-marked-patches
1179 stgit-marked-patches
1180 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1181 (cond (patch (list patch))
1182 (cause-error (error "No patches marked or at this line"))
1183 (t nil)))))
1184
1185 (defun stgit-goto-patch (patchsym &optional file)
1186 "Move point to the line containing patch PATCHSYM.
1187 If that patch cannot be found, do nothing.
1188
1189 If the patch was found and FILE is not nil, instead move to that
1190 file's line. If FILE cannot be found, stay on the line of
1191 PATCHSYM."
1192 (let ((node (ewoc-nth stgit-ewoc 0)))
1193 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
1194 patchsym)))
1195 (setq node (ewoc-next stgit-ewoc node)))
1196 (when (and node file)
1197 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
1198 (file-node (ewoc-nth file-ewoc 0)))
1199 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
1200 (setq file-node (ewoc-next file-ewoc file-node)))
1201 (when file-node
1202 (ewoc-goto-node file-ewoc file-node)
1203 (move-to-column (stgit-goal-column))
1204 (setq node nil))))
1205 (when node
1206 (ewoc-goto-node stgit-ewoc node)
1207 (move-to-column goal-column))))
1208
1209 (defun stgit-init ()
1210 "Run stg init."
1211 (interactive)
1212 (stgit-assert-mode)
1213 (stgit-capture-output nil
1214 (stgit-run "init"))
1215 (stgit-reload))
1216
1217 (defun stgit-toggle-mark ()
1218 "Toggle mark on the patch under point."
1219 (interactive)
1220 (stgit-assert-mode)
1221 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1222 (stgit-unmark)
1223 (stgit-mark)))
1224
1225 (defun stgit-mark ()
1226 "Mark the patch under point."
1227 (interactive)
1228 (stgit-assert-mode)
1229 (let* ((node (ewoc-locate stgit-ewoc))
1230 (patch (ewoc-data node))
1231 (name (stgit-patch-name patch)))
1232 (when (eq name :work)
1233 (error "Cannot mark the work tree"))
1234 (when (eq name :index)
1235 (error "Cannot mark the index"))
1236 (stgit-add-mark (stgit-patch-name patch))
1237 (let ((column (current-column)))
1238 (ewoc-invalidate stgit-ewoc node)
1239 (move-to-column column))))
1240
1241 (defun stgit-mark-down ()
1242 "Mark the patch under point and move to the next patch."
1243 (interactive)
1244 (stgit-mark)
1245 (stgit-next-patch))
1246
1247 (defun stgit-unmark ()
1248 "Remove mark from the patch on the current line."
1249 (interactive)
1250 (stgit-assert-mode)
1251 (let* ((node (ewoc-locate stgit-ewoc))
1252 (patch (ewoc-data node)))
1253 (stgit-remove-mark (stgit-patch-name patch))
1254 (let ((column (current-column)))
1255 (ewoc-invalidate stgit-ewoc node)
1256 (move-to-column column))))
1257
1258 (defun stgit-unmark-up ()
1259 "Remove mark from the patch on the previous line."
1260 (interactive)
1261 (stgit-assert-mode)
1262 (stgit-previous-patch)
1263 (stgit-unmark))
1264
1265 (defun stgit-unmark-down ()
1266 "Remove mark from the patch on the current line."
1267 (interactive)
1268 (stgit-assert-mode)
1269 (stgit-unmark)
1270 (stgit-next-patch))
1271
1272 (defun stgit-rename (name)
1273 "Rename the patch under point to NAME."
1274 (interactive (list
1275 (read-string "Patch name: "
1276 (symbol-name (stgit-patch-name-at-point t t)))))
1277 (stgit-assert-mode)
1278 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1279 (stgit-capture-output nil
1280 (stgit-run "rename" old-patchsym name))
1281 (let ((name-sym (intern name)))
1282 (when (memq old-patchsym stgit-expanded-patches)
1283 (setq stgit-expanded-patches
1284 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1285 (when (memq old-patchsym stgit-marked-patches)
1286 (setq stgit-marked-patches
1287 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1288 (stgit-reload)
1289 (stgit-goto-patch name-sym))))
1290
1291 (defun stgit-reload-or-repair (repair)
1292 "Update the contents of the StGit buffer (`stgit-reload').
1293
1294 With a prefix argument, repair the StGit metadata if the branch
1295 was modified with git commands (`stgit-repair')."
1296 (interactive "P")
1297 (stgit-assert-mode)
1298 (if repair
1299 (stgit-repair)
1300 (stgit-reload)))
1301
1302 (defun stgit-repair ()
1303 "Run stg repair."
1304 (interactive)
1305 (stgit-assert-mode)
1306 (stgit-capture-output nil
1307 (stgit-run "repair"))
1308 (stgit-reload))
1309
1310 (defun stgit-available-branches ()
1311 "Returns a list of the available stg branches"
1312 (let ((output (with-output-to-string
1313 (stgit-run "branch" "--list")))
1314 (start 0)
1315 result)
1316 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1317 (setq result (cons (match-string 1 output) result))
1318 (setq start (match-end 0)))
1319 result))
1320
1321 (defun stgit-branch (branch)
1322 "Switch to branch BRANCH."
1323 (interactive (list (completing-read "Switch to branch: "
1324 (stgit-available-branches))))
1325 (stgit-assert-mode)
1326 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1327 (stgit-reload))
1328
1329 (defun stgit-available-refs (&optional omit-stgit)
1330 "Returns a list of the available git refs.
1331 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1332 (let* ((output (with-output-to-string
1333 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1334 "refs/tags" "refs/heads"
1335 "refs/remotes")))
1336 (result (split-string output "\n" t)))
1337 (mapcar (lambda (s)
1338 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1339 (substring s (match-end 0))
1340 s))
1341 (if omit-stgit
1342 (delete-if (lambda (s)
1343 (string-match "^refs/heads/.*\\.stgit$" s))
1344 result)
1345 result))))
1346
1347 (defun stgit-rebase (new-base)
1348 "Rebase to NEW-BASE."
1349 (interactive (list (completing-read "Rebase to: "
1350 (stgit-available-refs t))))
1351 (stgit-assert-mode)
1352 (stgit-capture-output nil (stgit-run "rebase" new-base))
1353 (stgit-reload))
1354
1355 (defun stgit-commit (count)
1356 "Run stg commit on COUNT commits.
1357 Interactively, the prefix argument is used as COUNT.
1358 A negative COUNT will uncommit instead."
1359 (interactive "p")
1360 (stgit-assert-mode)
1361 (if (< count 0)
1362 (stgit-uncommit (- count))
1363 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1364 (stgit-reload)))
1365
1366 (defun stgit-uncommit (count)
1367 "Run stg uncommit on COUNT commits.
1368 Interactively, the prefix argument is used as COUNT.
1369 A negative COUNT will commit instead."
1370 (interactive "p")
1371 (stgit-assert-mode)
1372 (if (< count 0)
1373 (stgit-commit (- count))
1374 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1375 (stgit-reload)))
1376
1377 (defun stgit-neighbour-file ()
1378 "Return the file name of the next file after point, or the
1379 previous file if point is at the last file within a patch."
1380 (let ((old-point (point))
1381 neighbour-file)
1382 (and (zerop (forward-line 1))
1383 (let ((f (stgit-patched-file-at-point)))
1384 (and f (setq neighbour-file (stgit-file-file f)))))
1385 (goto-char old-point)
1386 (unless neighbour-file
1387 (and (zerop (forward-line -1))
1388 (let ((f (stgit-patched-file-at-point)))
1389 (and f (setq neighbour-file (stgit-file-file f)))))
1390 (goto-char old-point))
1391 neighbour-file))
1392
1393 (defun stgit-revert-file ()
1394 "Revert the file at point, which must be in the index or the
1395 working tree."
1396 (interactive)
1397 (stgit-assert-mode)
1398 (let* ((patched-file (or (stgit-patched-file-at-point)
1399 (error "No file on the current line")))
1400 (patch-name (stgit-patch-name-at-point))
1401 (file-status (stgit-file-status patched-file))
1402 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1403 (stgit-file-cr-to patched-file))
1404 ((eq file-status 'add)
1405 (stgit-file-file patched-file))))
1406 (co-file (cond ((eq file-status 'rename)
1407 (stgit-file-cr-from patched-file))
1408 ((not (memq file-status '(copy add)))
1409 (stgit-file-file patched-file))))
1410 (next-file (stgit-neighbour-file)))
1411
1412 (unless (memq patch-name '(:work :index))
1413 (error "No index or working tree file on this line"))
1414
1415 (when (eq file-status 'ignore)
1416 (error "Cannot revert ignored files"))
1417
1418 (when (eq file-status 'unknown)
1419 (error "Cannot revert unknown files"))
1420
1421 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1422 (when (yes-or-no-p (format "Revert %d file%s? "
1423 nfiles
1424 (if (= nfiles 1) "" "s")))
1425 (stgit-capture-output nil
1426 (when rm-file
1427 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1428 (when co-file
1429 (stgit-run-git "checkout" "HEAD" co-file)))
1430 (stgit-reload)
1431 (stgit-goto-patch patch-name next-file)))))
1432
1433 (defun stgit-revert ()
1434 "Revert the change at point, which must be the index, the work
1435 tree, or a single change in either."
1436 (interactive)
1437 (stgit-assert-mode)
1438 (let ((patched-file (stgit-patched-file-at-point)))
1439 (if patched-file
1440 (stgit-revert-file)
1441 (let* ((patch-name (or (stgit-patch-name-at-point)
1442 (error "No patch or file at point")))
1443 (patch-desc (case patch-name
1444 (:index "index")
1445 (:work "work tree")
1446 (t (error (substitute-command-keys
1447 "Use \\[stgit-delete] to delete a patch"))))))
1448 (when (if (eq patch-name :work)
1449 (stgit-work-tree-empty-p)
1450 (stgit-index-empty-p))
1451 (error (format "There are no changes in the %s to revert"
1452 patch-desc)))
1453 (and (eq patch-name :index)
1454 (not (stgit-work-tree-empty-p))
1455 (error "Cannot revert index as work tree contains unstaged changes"))
1456
1457 (when (yes-or-no-p (format "Revert all changes in the %s? "
1458 patch-desc))
1459 (if (eq patch-name :index)
1460 (stgit-run-git-silent "reset" "--hard" "-q")
1461 (stgit-run-git-silent "checkout" "--" "."))
1462 (stgit-refresh-index)
1463 (stgit-refresh-worktree)
1464 (stgit-goto-patch patch-name))))))
1465
1466 (defun stgit-resolve-file ()
1467 "Resolve conflict in the file at point."
1468 (interactive)
1469 (stgit-assert-mode)
1470 (let* ((patched-file (stgit-patched-file-at-point))
1471 (patch (stgit-patch-at-point))
1472 (patch-name (and patch (stgit-patch-name patch)))
1473 (status (and patched-file (stgit-file-status patched-file))))
1474
1475 (unless (memq patch-name '(:work :index))
1476 (error "No index or working tree file on this line"))
1477
1478 (unless (eq status 'unmerged)
1479 (error "No conflict to resolve at the current line"))
1480
1481 (stgit-capture-output nil
1482 (stgit-move-change-to-index (stgit-file-file patched-file)))
1483
1484 (stgit-reload)))
1485
1486 (defun stgit-push-next (npatches)
1487 "Push the first unapplied patch.
1488 With numeric prefix argument, push that many patches."
1489 (interactive "p")
1490 (stgit-assert-mode)
1491 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1492 (stgit-reload)
1493 (stgit-refresh-git-status))
1494
1495 (defun stgit-pop-next (npatches)
1496 "Pop the topmost applied patch.
1497 With numeric prefix argument, pop that many patches."
1498 (interactive "p")
1499 (stgit-assert-mode)
1500 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1501 (stgit-reload)
1502 (stgit-refresh-git-status))
1503
1504 (defun stgit-applied-patches (&optional only-patches)
1505 "Return a list of the applied patches.
1506
1507 If ONLY-PATCHES is not nil, exclude index and work tree."
1508 (let ((states (if only-patches
1509 '(applied top)
1510 '(applied top index work)))
1511 result)
1512 (ewoc-map (lambda (patch) (when (memq (stgit-patch-status patch) states)
1513 (setq result (cons patch result))))
1514 stgit-ewoc)
1515 result))
1516
1517 (defun stgit-applied-patchsyms (&optional only-patches)
1518 "Return a list of the symbols of the applied patches.
1519
1520 If ONLY-PATCHES is not nil, exclude index and work tree."
1521 (mapcar #'stgit-patch-name (stgit-applied-patches only-patches)))
1522
1523 (defun stgit-push-or-pop ()
1524 "Push or pop the marked patches."
1525 (interactive)
1526 (stgit-assert-mode)
1527 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1528 (applied-syms (stgit-applied-patchsyms t))
1529 (unapplied (set-difference patchsyms applied-syms)))
1530 (stgit-capture-output nil
1531 (apply 'stgit-run
1532 (if unapplied "push" "pop")
1533 "--"
1534 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1535 (stgit-reload))
1536
1537 (defun stgit-goto ()
1538 "Go to the patch on the current line.
1539
1540 Pops or pushes patches to make this patch topmost."
1541 (interactive)
1542 (stgit-assert-mode)
1543 (let ((patchsym (stgit-patch-name-at-point t)))
1544 (stgit-capture-output nil
1545 (stgit-run "goto" patchsym))
1546 (stgit-reload)))
1547
1548 (defun stgit-id (patchsym)
1549 "Return the git commit id for PATCHSYM.
1550 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1551 (if (keywordp patchsym)
1552 patchsym
1553 (let ((result (with-output-to-string
1554 (stgit-run-silent "id" patchsym))))
1555 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1556 (error "Cannot find commit id for %s" patchsym))
1557 (match-string 1 result))))
1558
1559 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1560 "Show the patch on the current line.
1561
1562 UNMERGED-STAGE is the argument to `git-diff' that that selects
1563 which stage to diff against in the case of unmerged files."
1564 (let ((space-arg (when (numberp ignore-whitespace)
1565 (cond ((> ignore-whitespace 4)
1566 "--ignore-all-space")
1567 ((> ignore-whitespace 1)
1568 "--ignore-space-change"))))
1569 (patch-name (stgit-patch-name-at-point t)))
1570 (stgit-capture-output "*StGit patch*"
1571 (case (get-text-property (point) 'entry-type)
1572 ('file
1573 (let* ((patched-file (stgit-patched-file-at-point))
1574 (patch-id (let ((id (stgit-id patch-name)))
1575 (if (and (eq id :index)
1576 (eq (stgit-file-status patched-file)
1577 'unmerged))
1578 :work
1579 id)))
1580 (args (append (and space-arg (list space-arg))
1581 (and (stgit-file-cr-from patched-file)
1582 (list (stgit-find-copies-harder-diff-arg)))
1583 (cond ((eq patch-id :index)
1584 '("--cached"))
1585 ((eq patch-id :work)
1586 (list unmerged-stage))
1587 (t
1588 (list (concat patch-id "^") patch-id)))
1589 '("--")
1590 (if (stgit-file-copy-or-rename patched-file)
1591 (list (stgit-file-cr-from patched-file)
1592 (stgit-file-cr-to patched-file))
1593 (list (stgit-file-file patched-file))))))
1594 (apply 'stgit-run-git "diff" args)))
1595 ('patch
1596 (let* ((patch-id (stgit-id patch-name)))
1597 (if (or (eq patch-id :index) (eq patch-id :work))
1598 (apply 'stgit-run-git "diff"
1599 (stgit-find-copies-harder-diff-arg)
1600 (append (and space-arg (list space-arg))
1601 (if (eq patch-id :index)
1602 '("--cached")
1603 (list unmerged-stage))))
1604 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1605 (and space-arg (list "-O" space-arg))
1606 (list (stgit-patch-name-at-point)))))
1607 (apply 'stgit-run args)))))
1608 (t
1609 (error "No patch or file at point")))
1610 (with-current-buffer standard-output
1611 (goto-char (point-min))
1612 (diff-mode)))))
1613
1614 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1615 `(defun ,name (&optional ignore-whitespace)
1616 ,(format "Show the patch on the current line.
1617
1618 %sWith a prefix argument, ignore whitespace. With a prefix argument
1619 greater than four (e.g., \\[universal-argument] \
1620 \\[universal-argument] \\[%s]), ignore all whitespace."
1621 (if unmerged-action
1622 (format "For unmerged files, %s.\n\n" unmerged-action)
1623 "")
1624 name)
1625 (interactive "p")
1626 (stgit-assert-mode)
1627 (stgit-show-patch ,diff-arg ignore-whitespace)))
1628
1629 (stgit-define-diff stgit-diff
1630 "--ours" nil)
1631 (stgit-define-diff stgit-diff-ours
1632 "--ours"
1633 "diff against our branch")
1634 (stgit-define-diff stgit-diff-theirs
1635 "--theirs"
1636 "diff against their branch")
1637 (stgit-define-diff stgit-diff-base
1638 "--base"
1639 "diff against the merge base")
1640 (stgit-define-diff stgit-diff-combined
1641 "--cc"
1642 "show a combined diff")
1643
1644 (defun stgit-move-change-to-index (file &optional force)
1645 "Copies the work tree state of FILE to index, using git add or git rm.
1646
1647 If FORCE is not nil, use --force."
1648 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1649 '("add") '("rm" "-q"))))
1650 (stgit-capture-output "*git output*"
1651 (apply 'stgit-run-git (append op (and force '("--force"))
1652 '("--") (list file))))))
1653
1654 (defun stgit-remove-change-from-index (file)
1655 "Unstages the change in FILE from the index"
1656 (stgit-capture-output "*git output*"
1657 (stgit-run-git "reset" "-q" "--" file)))
1658
1659 (defun stgit-git-index-unmerged-p ()
1660 (let (result)
1661 (with-output-to-string
1662 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1663 "--diff-filter=U"
1664 "--quiet" "HEAD")))))
1665 result))
1666
1667 (defun stgit-file-toggle-index ()
1668 "Move modified file in or out of the index.
1669
1670 Leaves the point where it is, but moves the mark to where the
1671 file ended up. You can then jump to the file with \
1672 \\[exchange-point-and-mark]."
1673 (interactive)
1674 (stgit-assert-mode)
1675 (let* ((patched-file (or (stgit-patched-file-at-point)
1676 (error "No file on the current line")))
1677 (patched-status (stgit-file-status patched-file)))
1678 (when (eq patched-status 'unmerged)
1679 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1680 (let* ((patch (stgit-patch-at-point))
1681 (patch-name (stgit-patch-name patch))
1682 (mark-file (if (eq patched-status 'rename)
1683 (stgit-file-cr-to patched-file)
1684 (stgit-file-file patched-file)))
1685 (point-file (if (eq patched-status 'rename)
1686 (stgit-file-cr-from patched-file)
1687 (stgit-neighbour-file))))
1688
1689 (cond ((eq patch-name :work)
1690 (stgit-move-change-to-index (stgit-file-file patched-file)
1691 (eq patched-status 'ignore)))
1692 ((eq patch-name :index)
1693 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1694 (t
1695 (error "Can only move files between working tree and index")))
1696 (stgit-refresh-worktree)
1697 (stgit-refresh-index)
1698 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1699 (push-mark nil t t)
1700 (stgit-goto-patch patch-name point-file))))
1701
1702 (defun stgit-toggle-index ()
1703 "Move change in or out of the index.
1704
1705 Works on index and work tree, as well as files in either.
1706
1707 Leaves the point where it is, but moves the mark to where the
1708 file ended up. You can then jump to the file with \
1709 \\[exchange-point-and-mark]."
1710 (interactive)
1711 (stgit-assert-mode)
1712 (if (stgit-patched-file-at-point)
1713 (stgit-file-toggle-index)
1714 (let ((patch-name (stgit-patch-name-at-point)))
1715 (unless (memq patch-name '(:index :work))
1716 (error "Can only move changes between working tree and index"))
1717 (when (stgit-git-index-unmerged-p)
1718 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1719 (if (if (eq patch-name :index)
1720 (stgit-index-empty-p)
1721 (stgit-work-tree-empty-p))
1722 (message "No changes to be moved")
1723 (stgit-capture-output nil
1724 (if (eq patch-name :work)
1725 (stgit-run-git "add" "--update")
1726 (stgit-run-git "reset" "--mixed" "-q")))
1727 (stgit-refresh-worktree)
1728 (stgit-refresh-index))
1729 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1730
1731 (defun stgit-edit ()
1732 "Edit the patch on the current line."
1733 (interactive)
1734 (stgit-assert-mode)
1735 (let ((patchsym (stgit-patch-name-at-point t t))
1736 (edit-buf (get-buffer-create "*StGit edit*"))
1737 (dir default-directory))
1738 (log-edit 'stgit-confirm-edit t nil edit-buf)
1739 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1740 (setq default-directory dir)
1741 (let ((standard-output edit-buf))
1742 (save-excursion
1743 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1744
1745 (defun stgit-confirm-edit ()
1746 (interactive)
1747 (let ((file (make-temp-file "stgit-edit-")))
1748 (write-region (point-min) (point-max) file)
1749 (stgit-capture-output nil
1750 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1751 (with-current-buffer log-edit-parent-buffer
1752 (stgit-reload))))
1753
1754 (defun stgit-new (add-sign &optional refresh)
1755 "Create a new patch.
1756 With a prefix argument, include a \"Signed-off-by:\" line at the
1757 end of the patch."
1758 (interactive "P")
1759 (stgit-assert-mode)
1760 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1761 (dir default-directory))
1762 (log-edit 'stgit-confirm-new t nil edit-buf)
1763 (setq default-directory dir)
1764 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1765 (when add-sign
1766 (save-excursion
1767 (let ((standard-output (current-buffer)))
1768 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1769
1770 (defun stgit-confirm-new ()
1771 (interactive)
1772 (let ((file (make-temp-file "stgit-edit-"))
1773 (refresh stgit-refresh-after-new))
1774 (write-region (point-min) (point-max) file)
1775 (stgit-capture-output nil
1776 (stgit-run "new" "-f" file))
1777 (with-current-buffer log-edit-parent-buffer
1778 (if refresh
1779 (stgit-refresh)
1780 (stgit-reload)))))
1781
1782 (defun stgit-new-and-refresh (add-sign)
1783 "Create a new patch and refresh it with the current changes.
1784
1785 With a prefix argument, include a \"Signed-off-by:\" line at the
1786 end of the patch.
1787
1788 This works just like running `stgit-new' followed by `stgit-refresh'."
1789 (interactive "P")
1790 (stgit-assert-mode)
1791 (stgit-new add-sign t))
1792
1793 (defun stgit-create-patch-name (description)
1794 "Create a patch name from a long description"
1795 (let ((patch ""))
1796 (while (> (length description) 0)
1797 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1798 (setq patch (downcase (concat patch
1799 (match-string 0 description))))
1800 (setq description (substring description (match-end 0))))
1801 ((string-match "\\` +" description)
1802 (setq patch (concat patch "-"))
1803 (setq description (substring description (match-end 0))))
1804 ((string-match "\\`[^a-zA-Z_-]+" description)
1805 (setq description (substring description (match-end 0))))))
1806 (cond ((= (length patch) 0)
1807 "patch")
1808 ((> (length patch) 20)
1809 (substring patch 0 20))
1810 (t patch))))
1811
1812 (defun stgit-delete (patchsyms &optional spill-p)
1813 "Delete the patches in PATCHSYMS.
1814 Interactively, delete the marked patches, or the patch at point.
1815
1816 With a prefix argument, or SPILL-P, spill the patch contents to
1817 the work tree and index."
1818 (interactive (list (stgit-patches-marked-or-at-point t t)
1819 current-prefix-arg))
1820 (stgit-assert-mode)
1821 (unless patchsyms
1822 (error "No patches to delete"))
1823 (when (memq :index patchsyms)
1824 (error "Cannot delete the index"))
1825 (when (memq :work patchsyms)
1826 (error "Cannot delete the work tree"))
1827
1828 (let ((npatches (length patchsyms)))
1829 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1830 npatches
1831 (if (= 1 npatches) "" "es")
1832 (if spill-p
1833 " (spilling contents to index)"
1834 "")))
1835 (let ((args (if spill-p
1836 (cons "--spill" patchsyms)
1837 patchsyms)))
1838 (stgit-capture-output nil
1839 (apply 'stgit-run "delete" args))
1840 (stgit-reload)))))
1841
1842 (defun stgit-move-patches-target ()
1843 "Return the patchsym indicating a target patch for
1844 `stgit-move-patches'.
1845
1846 This is either the first unmarked patch at or after point, or one
1847 of :top and :bottom if the point is after or before the applied
1848 patches."
1849
1850 (save-excursion
1851 (let (result)
1852 (while (not result)
1853 (let ((patchsym (stgit-patch-name-at-point)))
1854 (cond ((memq patchsym '(:work :index)) (setq result :top))
1855 (patchsym (if (memq patchsym stgit-marked-patches)
1856 (stgit-next-patch)
1857 (setq result patchsym)))
1858 ((re-search-backward "^>" nil t) (setq result :top))
1859 (t (setq result :bottom)))))
1860 result)))
1861
1862 (defun stgit-sort-patches (patchsyms)
1863 "Returns the list of patches in PATCHSYMS sorted according to
1864 their position in the patch series, bottommost first.
1865
1866 PATCHSYMS must not contain duplicate entries."
1867 (let (sorted-patchsyms
1868 (series (with-output-to-string
1869 (with-current-buffer standard-output
1870 (stgit-run-silent "series" "--noprefix"))))
1871 start)
1872 (while (string-match "^\\(.+\\)" series start)
1873 (let ((patchsym (intern (match-string 1 series))))
1874 (when (memq patchsym patchsyms)
1875 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1876 (setq start (match-end 0)))
1877 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1878
1879 (unless (= (length patchsyms) (length sorted-patchsyms))
1880 (error "Internal error"))
1881
1882 sorted-patchsyms))
1883
1884 (defun stgit-move-patches (patchsyms target-patch)
1885 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1886 If TARGET-PATCH is :bottom or :top, move the patches to the
1887 bottom or top of the stack, respectively.
1888
1889 Interactively, move the marked patches to where the point is."
1890 (interactive (list stgit-marked-patches
1891 (stgit-move-patches-target)))
1892 (stgit-assert-mode)
1893 (unless patchsyms
1894 (error "Need at least one patch to move"))
1895
1896 (unless target-patch
1897 (error "Point not at a patch"))
1898
1899 ;; need to have patchsyms sorted by position in the stack
1900 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1901 (stgit-capture-output nil
1902 (if (eq target-patch :top)
1903 (apply 'stgit-run "float" sorted-patchsyms)
1904 (apply 'stgit-run
1905 "sink"
1906 (append (unless (eq target-patch :bottom)
1907 (list "--to" target-patch))
1908 '("--")
1909 sorted-patchsyms)))))
1910 (stgit-reload))
1911
1912 (defun stgit-squash (patchsyms)
1913 "Squash the patches in PATCHSYMS.
1914 Interactively, squash the marked patches.
1915
1916 Unless there are any conflicts, the patches will be merged into
1917 one patch, which will occupy the same spot in the series as the
1918 deepest patch had before the squash."
1919 (interactive (list stgit-marked-patches))
1920 (stgit-assert-mode)
1921 (when (< (length patchsyms) 2)
1922 (error "Need at least two patches to squash"))
1923 (let ((stgit-buffer (current-buffer))
1924 (edit-buf (get-buffer-create "*StGit edit*"))
1925 (dir default-directory)
1926 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1927 (log-edit 'stgit-confirm-squash t nil edit-buf)
1928 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1929 (setq default-directory dir)
1930 (let ((result (let ((standard-output edit-buf))
1931 (save-excursion
1932 (apply 'stgit-run-silent "squash"
1933 "--save-template=-" sorted-patchsyms)))))
1934
1935 ;; stg squash may have reordered the patches or caused conflicts
1936 (with-current-buffer stgit-buffer
1937 (stgit-reload))
1938
1939 (unless (eq 0 result)
1940 (fundamental-mode)
1941 (rename-buffer "*StGit error*")
1942 (resize-temp-buffer-window)
1943 (switch-to-buffer-other-window stgit-buffer)
1944 (error "stg squash failed")))))
1945
1946 (defun stgit-confirm-squash ()
1947 (interactive)
1948 (let ((file (make-temp-file "stgit-edit-")))
1949 (write-region (point-min) (point-max) file)
1950 (stgit-capture-output nil
1951 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1952 (with-current-buffer log-edit-parent-buffer
1953 (stgit-clear-marks)
1954 ;; Go to first marked patch and stay there
1955 (goto-char (point-min))
1956 (re-search-forward (concat "^[>+-]\\*") nil t)
1957 (move-to-column goal-column)
1958 (let ((pos (point)))
1959 (stgit-reload)
1960 (goto-char pos)))))
1961
1962 (defun stgit-help ()
1963 "Display help for the StGit mode."
1964 (interactive)
1965 (describe-function 'stgit-mode))
1966
1967 (defun stgit-undo (&optional arg)
1968 "Run stg undo.
1969 With prefix argument, run it with the --hard flag.
1970
1971 See also `stgit-redo'."
1972 (interactive "P")
1973 (stgit-assert-mode)
1974 (stgit-capture-output nil
1975 (if arg
1976 (stgit-run "undo" "--hard")
1977 (stgit-run "undo")))
1978 (stgit-reload))
1979
1980 (defun stgit-redo (&optional arg)
1981 "Run stg redo.
1982 With prefix argument, run it with the --hard flag.
1983
1984 See also `stgit-undo'."
1985 (interactive "P")
1986 (stgit-assert-mode)
1987 (stgit-capture-output nil
1988 (if arg
1989 (stgit-run "redo" "--hard")
1990 (stgit-run "redo")))
1991 (stgit-reload))
1992
1993 (defun stgit-refresh (&optional arg)
1994 "Run stg refresh.
1995 If the index contains any changes, only refresh from index.
1996
1997 With prefix argument, refresh the marked patch or the patch under point."
1998 (interactive "P")
1999 (stgit-assert-mode)
2000 (let ((patchargs (if arg
2001 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2002 (when (> (length patches) 1)
2003 (error "Too many patches marked"))
2004 (cons "-p" patches))
2005 nil)))
2006 (unless (stgit-index-empty-p)
2007 (setq patchargs (cons "--index" patchargs)))
2008 (stgit-capture-output nil
2009 (apply 'stgit-run "refresh" patchargs))
2010 (stgit-refresh-git-status))
2011 (stgit-reload))
2012
2013 (defvar stgit-show-worktree nil
2014 "If nil, inhibit showing work tree and index in the stgit buffer.
2015
2016 See also `stgit-show-worktree-mode'.")
2017
2018 (defvar stgit-show-ignored nil
2019 "If nil, inhibit showing files ignored by git.")
2020
2021 (defvar stgit-show-unknown nil
2022 "If nil, inhibit showing files not registered with git.")
2023
2024 (defun stgit-toggle-worktree (&optional arg)
2025 "Toggle the visibility of the work tree.
2026 With ARG, show the work tree if ARG is positive.
2027
2028 Its initial setting is controlled by `stgit-default-show-worktree'.
2029
2030 `stgit-show-worktree-mode' controls where on screen the index and
2031 work tree will show up."
2032 (interactive)
2033 (stgit-assert-mode)
2034 (setq stgit-show-worktree
2035 (if (numberp arg)
2036 (> arg 0)
2037 (not stgit-show-worktree)))
2038 (stgit-reload))
2039
2040 (defun stgit-toggle-ignored (&optional arg)
2041 "Toggle the visibility of files ignored by git in the work
2042 tree. With ARG, show these files if ARG is positive.
2043
2044 Use \\[stgit-toggle-worktree] to show the work tree."
2045 (interactive)
2046 (stgit-assert-mode)
2047 (setq stgit-show-ignored
2048 (if (numberp arg)
2049 (> arg 0)
2050 (not stgit-show-ignored)))
2051 (stgit-reload))
2052
2053 (defun stgit-toggle-unknown (&optional arg)
2054 "Toggle the visibility of files not registered with git in the
2055 work tree. With ARG, show these files if ARG is positive.
2056
2057 Use \\[stgit-toggle-worktree] to show the work tree."
2058 (interactive)
2059 (stgit-assert-mode)
2060 (setq stgit-show-unknown
2061 (if (numberp arg)
2062 (> arg 0)
2063 (not stgit-show-unknown)))
2064 (stgit-reload))
2065
2066 (provide 'stgit)