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