stgit.el: Garbage collect selected patches on reload
[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 (require 'git nil t)
13 (require 'cl)
14 (require 'ewoc)
15
16 (defun stgit (dir)
17 "Manage StGit patches for the tree in DIR."
18 (interactive "DDirectory: \n")
19 (switch-to-stgit-buffer (git-get-top-dir dir))
20 (stgit-reload))
21
22 (unless (fboundp 'git-get-top-dir)
23 (defun git-get-top-dir (dir)
24 "Retrieve the top-level directory of a git tree."
25 (let ((cdup (with-output-to-string
26 (with-current-buffer standard-output
27 (cd dir)
28 (unless (eq 0 (call-process "git" nil t nil
29 "rev-parse" "--show-cdup"))
30 (error "Cannot find top-level git tree for %s" dir))))))
31 (expand-file-name (concat (file-name-as-directory dir)
32 (car (split-string cdup "\n")))))))
33
34 (defun stgit-refresh-git-status (&optional dir)
35 "If it exists, refresh the `git-status' buffer belonging to
36 directory DIR or `default-directory'"
37 (when (and (fboundp 'git-find-status-buffer)
38 (fboundp 'git-refresh-status))
39 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
40 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
41 (when git-status-buffer
42 (with-current-buffer git-status-buffer
43 (git-refresh-status))))))
44
45 (defun stgit-find-buffer (dir)
46 "Return the buffer displaying StGit patches for DIR, or nil if none."
47 (setq dir (file-name-as-directory dir))
48 (let ((buffers (buffer-list)))
49 (while (and buffers
50 (not (with-current-buffer (car buffers)
51 (and (eq major-mode 'stgit-mode)
52 (string= default-directory dir)))))
53 (setq buffers (cdr buffers)))
54 (and buffers (car buffers))))
55
56 (defun switch-to-stgit-buffer (dir)
57 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
58 (setq dir (file-name-as-directory dir))
59 (let ((buffer (stgit-find-buffer dir)))
60 (switch-to-buffer (or buffer
61 (create-stgit-buffer dir)))))
62
63 (defstruct (stgit-patch)
64 status name desc empty files-ewoc)
65
66 (defun stgit-patch-pp (patch)
67 (let ((status (stgit-patch-status patch))
68 (start (point))
69 (name (stgit-patch-name patch)))
70 (case name
71 (:index (insert " "
72 (propertize "Index"
73 'face 'stgit-index-work-tree-title-face)))
74 (:work (insert " "
75 (propertize "Work tree"
76 'face 'stgit-index-work-tree-title-face)))
77 (t (insert (case status
78 ('applied "+")
79 ('top ">")
80 ('unapplied "-"))
81 (if (memq name stgit-marked-patches)
82 "*" " ")
83 (propertize (format "%-30s"
84 (symbol-name name))
85 'face (case status
86 ('applied 'stgit-applied-patch-face)
87 ('top 'stgit-top-patch-face)
88 ('unapplied 'stgit-unapplied-patch-face)
89 ('index nil)
90 ('work nil)))
91 " "
92 (if (stgit-patch-empty patch) "(empty) " "")
93 (propertize (or (stgit-patch-desc patch) "")
94 'face 'stgit-description-face))))
95 (put-text-property start (point) 'entry-type 'patch)
96 (when (memq name stgit-expanded-patches)
97 (stgit-insert-patch-files patch))
98 (put-text-property start (point) 'patch-data patch)))
99
100 (defun create-stgit-buffer (dir)
101 "Create a buffer for showing StGit patches.
102 Argument DIR is the repository path."
103 (let ((buf (create-file-buffer (concat dir "*stgit*")))
104 (inhibit-read-only t))
105 (with-current-buffer buf
106 (setq default-directory dir)
107 (stgit-mode)
108 (set (make-local-variable 'stgit-ewoc)
109 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
110 (setq buffer-read-only t))
111 buf))
112
113 (defmacro stgit-capture-output (name &rest body)
114 "Capture StGit output and, if there was any output, show it in a window
115 at the end.
116 Returns nil if there was no output."
117 (declare (debug ([&or stringp null] body))
118 (indent 1))
119 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
120 (stgit-dir default-directory)
121 (inhibit-read-only t))
122 (with-current-buffer output-buf
123 (erase-buffer)
124 (setq default-directory stgit-dir)
125 (setq buffer-read-only t))
126 (let ((standard-output output-buf))
127 ,@body)
128 (with-current-buffer output-buf
129 (set-buffer-modified-p nil)
130 (setq buffer-read-only t)
131 (if (< (point-min) (point-max))
132 (display-buffer output-buf t)))))
133
134 (defun stgit-make-run-args (args)
135 "Return a copy of ARGS with its elements converted to strings."
136 (mapcar (lambda (x)
137 ;; don't use (format "%s" ...) to limit type errors
138 (cond ((stringp x) x)
139 ((integerp x) (number-to-string x))
140 ((symbolp x) (symbol-name x))
141 (t
142 (error "Bad element in stgit-make-run-args args: %S" x))))
143 args))
144
145 (defun stgit-run-silent (&rest args)
146 (setq args (stgit-make-run-args args))
147 (apply 'call-process "stg" nil standard-output nil args))
148
149 (defun stgit-run (&rest args)
150 (setq args (stgit-make-run-args args))
151 (let ((msgcmd (mapconcat #'identity args " ")))
152 (message "Running stg %s..." msgcmd)
153 (apply 'call-process "stg" nil standard-output nil args)
154 (message "Running stg %s...done" msgcmd)))
155
156 (defun stgit-run-git (&rest args)
157 (setq args (stgit-make-run-args args))
158 (let ((msgcmd (mapconcat #'identity args " ")))
159 (message "Running git %s..." msgcmd)
160 (apply 'call-process "git" nil standard-output nil args)
161 (message "Running git %s...done" msgcmd)))
162
163 (defun stgit-run-git-silent (&rest args)
164 (setq args (stgit-make-run-args args))
165 (apply 'call-process "git" nil standard-output nil args))
166
167 (defun stgit-index-empty-p ()
168 "Returns non-nil if the index contains no changes from HEAD."
169 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
170
171 (defvar stgit-index-node)
172 (defvar stgit-worktree-node)
173
174 (defun stgit-refresh-index ()
175 (when stgit-index-node
176 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
177
178 (defun stgit-refresh-worktree ()
179 (when stgit-worktree-node
180 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
181
182 (defun stgit-run-series-insert-index (ewoc)
183 (setq index-node (cons ewoc (ewoc-enter-last ewoc
184 (make-stgit-patch
185 :status 'index
186 :name :index
187 :desc nil
188 :empty nil)))
189 worktree-node (cons ewoc (ewoc-enter-last ewoc
190 (make-stgit-patch
191 :status 'work
192 :name :work
193 :desc nil
194 :empty nil)))))
195
196 (defun stgit-run-series (ewoc)
197 (setq stgit-index-node nil
198 stgit-worktree-node nil)
199 (let ((inserted-index (not stgit-show-worktree))
200 index-node
201 worktree-node
202 all-patchsyms)
203 (with-temp-buffer
204 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
205 (goto-char (point-min))
206 (if (not (zerop exit-status))
207 (cond ((looking-at "stg series: \\(.*\\)")
208 (setq inserted-index t)
209 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
210 (substitute-command-keys
211 "-- not initialized; run \\[stgit-init]")))
212 ((looking-at ".*")
213 (error "Error running stg: %s"
214 (match-string 0))))
215 (while (not (eobp))
216 (unless (looking-at
217 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
218 (error "Syntax error in output from stg series"))
219 (let* ((state-str (match-string 2))
220 (state (cond ((string= state-str ">") 'top)
221 ((string= state-str "+") 'applied)
222 ((string= state-str "-") 'unapplied)))
223 (name (intern (match-string 4)))
224 (desc (match-string 5))
225 (empty (string= (match-string 1) "0")))
226 (unless inserted-index
227 (when (or (eq stgit-show-worktree-mode 'top)
228 (and (eq stgit-show-worktree-mode 'center)
229 (eq state 'unapplied)))
230 (setq inserted-index t)
231 (stgit-run-series-insert-index ewoc)))
232 (setq all-patchsyms (cons name all-patchsyms))
233 (ewoc-enter-last ewoc
234 (make-stgit-patch
235 :status state
236 :name name
237 :desc desc
238 :empty empty)))
239 (forward-line 1))))
240 (unless inserted-index
241 (stgit-run-series-insert-index ewoc)))
242 (setq stgit-index-node index-node
243 stgit-worktree-node worktree-node
244 stgit-marked-patches (intersection stgit-marked-patches
245 all-patchsyms))))
246
247 (defun stgit-reload ()
248 "Update the contents of the StGit buffer."
249 (interactive)
250 (let ((inhibit-read-only t)
251 (curline (line-number-at-pos))
252 (curpatch (stgit-patch-name-at-point)))
253 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
254 (ewoc-set-hf stgit-ewoc
255 (concat "Branch: "
256 (propertize
257 (with-temp-buffer
258 (stgit-run-silent "branch")
259 (buffer-substring (point-min) (1- (point-max))))
260 'face 'stgit-branch-name-face)
261 "\n")
262 (if stgit-show-worktree
263 "--"
264 (propertize
265 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
266 shows the working tree\n")
267 'face 'stgit-description-face)))
268 (stgit-run-series stgit-ewoc)
269 (if curpatch
270 (stgit-goto-patch curpatch)
271 (goto-line curline)))
272 (stgit-refresh-git-status))
273
274 (defgroup stgit nil
275 "A user interface for the StGit patch maintenance tool."
276 :group 'tools)
277
278 (defface stgit-description-face
279 '((((background dark)) (:foreground "tan"))
280 (((background light)) (:foreground "dark red")))
281 "The face used for StGit descriptions"
282 :group 'stgit)
283
284 (defface stgit-branch-name-face
285 '((t :inherit bold))
286 "The face used for the StGit branch name"
287 :group 'stgit)
288
289 (defface stgit-top-patch-face
290 '((((background dark)) (:weight bold :foreground "yellow"))
291 (((background light)) (:weight bold :foreground "purple"))
292 (t (:weight bold)))
293 "The face used for the top patch names"
294 :group 'stgit)
295
296 (defface stgit-applied-patch-face
297 '((((background dark)) (:foreground "light yellow"))
298 (((background light)) (:foreground "purple"))
299 (t ()))
300 "The face used for applied patch names"
301 :group 'stgit)
302
303 (defface stgit-unapplied-patch-face
304 '((((background dark)) (:foreground "gray80"))
305 (((background light)) (:foreground "orchid"))
306 (t ()))
307 "The face used for unapplied patch names"
308 :group 'stgit)
309
310 (defface stgit-modified-file-face
311 '((((class color) (background light)) (:foreground "purple"))
312 (((class color) (background dark)) (:foreground "salmon")))
313 "StGit mode face used for modified file status"
314 :group 'stgit)
315
316 (defface stgit-unmerged-file-face
317 '((((class color) (background light)) (:foreground "red" :bold t))
318 (((class color) (background dark)) (:foreground "red" :bold t)))
319 "StGit mode face used for unmerged file status"
320 :group 'stgit)
321
322 (defface stgit-unknown-file-face
323 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
324 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
325 "StGit mode face used for unknown file status"
326 :group 'stgit)
327
328 (defface stgit-file-permission-face
329 '((((class color) (background light)) (:foreground "green" :bold t))
330 (((class color) (background dark)) (:foreground "green" :bold t)))
331 "StGit mode face used for permission changes."
332 :group 'stgit)
333
334 (defface stgit-index-work-tree-title-face
335 '((((supports :slant italic)) :slant italic)
336 (t :inherit bold))
337 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
338 :group 'stgit)
339
340
341 (defcustom stgit-expand-find-copies-harder
342 nil
343 "Try harder to find copied files when listing patches.
344
345 When not nil, runs git diff-tree with the --find-copies-harder
346 flag, which reduces performance."
347 :type 'boolean
348 :group 'stgit)
349
350 (defconst stgit-file-status-code-strings
351 (mapcar (lambda (arg)
352 (cons (car arg)
353 (propertize (cadr arg) 'face (car (cddr arg)))))
354 '((add "Added" stgit-modified-file-face)
355 (copy "Copied" stgit-modified-file-face)
356 (delete "Deleted" stgit-modified-file-face)
357 (modify "Modified" stgit-modified-file-face)
358 (rename "Renamed" stgit-modified-file-face)
359 (mode-change "Mode change" stgit-modified-file-face)
360 (unmerged "Unmerged" stgit-unmerged-file-face)
361 (unknown "Unknown" stgit-unknown-file-face)))
362 "Alist of code symbols to description strings")
363
364 (defun stgit-file-status-code-as-string (file)
365 "Return stgit status code for FILE as a string"
366 (let* ((code (assq (stgit-file-status file)
367 stgit-file-status-code-strings))
368 (score (stgit-file-cr-score file)))
369 (when code
370 (format "%-11s "
371 (if (and score (/= score 100))
372 (format "%s %s" (cdr code)
373 (propertize (format "%d%%" score)
374 'face 'stgit-description-face))
375 (cdr code))))))
376
377 (defun stgit-file-status-code (str &optional score)
378 "Return stgit status code from git status string"
379 (let ((code (assoc str '(("A" . add)
380 ("C" . copy)
381 ("D" . delete)
382 ("M" . modify)
383 ("R" . rename)
384 ("T" . mode-change)
385 ("U" . unmerged)
386 ("X" . unknown)))))
387 (setq code (if code (cdr code) 'unknown))
388 (when (stringp score)
389 (if (> (length score) 0)
390 (setq score (string-to-number score))
391 (setq score nil)))
392 (if score (cons code score) code)))
393
394 (defconst stgit-file-type-strings
395 '((#o100 . "file")
396 (#o120 . "symlink")
397 (#o160 . "subproject"))
398 "Alist of names of file types")
399
400 (defun stgit-file-type-string (type)
401 "Return string describing file type TYPE (the high bits of file permission).
402 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
403 (let ((type-str (assoc type stgit-file-type-strings)))
404 (or (and type-str (cdr type-str))
405 (format "unknown type %o" type))))
406
407 (defun stgit-file-type-change-string (old-perm new-perm)
408 "Return string describing file type change from OLD-PERM to NEW-PERM.
409 Cf. `stgit-file-type-string'."
410 (let ((old-type (lsh old-perm -9))
411 (new-type (lsh new-perm -9)))
412 (cond ((= old-type new-type) "")
413 ((zerop new-type) "")
414 ((zerop old-type)
415 (if (= new-type #o100)
416 ""
417 (format " (%s)" (stgit-file-type-string new-type))))
418 (t (format " (%s -> %s)"
419 (stgit-file-type-string old-type)
420 (stgit-file-type-string new-type))))))
421
422 (defun stgit-file-mode-change-string (old-perm new-perm)
423 "Return string describing file mode change from OLD-PERM to NEW-PERM.
424 Cf. `stgit-file-type-change-string'."
425 (setq old-perm (logand old-perm #o777)
426 new-perm (logand new-perm #o777))
427 (if (or (= old-perm new-perm)
428 (zerop old-perm)
429 (zerop new-perm))
430 ""
431 (let* ((modified (logxor old-perm new-perm))
432 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
433 (cond ((zerop modified) "")
434 ((and (zerop not-x-modified)
435 (or (and (eq #o111 (logand old-perm #o111))
436 (propertize "-x" 'face 'stgit-file-permission-face))
437 (and (eq #o111 (logand new-perm #o111))
438 (propertize "+x" 'face
439 'stgit-file-permission-face)))))
440 (t (concat (propertize (format "%o" old-perm)
441 'face 'stgit-file-permission-face)
442 (propertize " -> "
443 'face 'stgit-description-face)
444 (propertize (format "%o" new-perm)
445 'face 'stgit-file-permission-face)))))))
446
447 (defstruct (stgit-file)
448 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
449
450 (defun stgit-file-pp (file)
451 (let ((status (stgit-file-status file))
452 (name (if (stgit-file-copy-or-rename file)
453 (concat (stgit-file-cr-from file)
454 (propertize " -> "
455 'face 'stgit-description-face)
456 (stgit-file-cr-to file))
457 (stgit-file-file file)))
458 (mode-change (stgit-file-mode-change-string
459 (stgit-file-old-perm file)
460 (stgit-file-new-perm file)))
461 (start (point)))
462 (insert (format " %-12s%1s%s%s\n"
463 (stgit-file-status-code-as-string file)
464 mode-change
465 name
466 (propertize (stgit-file-type-change-string
467 (stgit-file-old-perm file)
468 (stgit-file-new-perm file))
469 'face 'stgit-description-face)))
470 (add-text-properties start (point)
471 (list 'entry-type 'file
472 'file-data file))))
473
474 (defun stgit-find-copies-harder-diff-arg ()
475 "Return the flag to use with `git-diff' depending on the
476 `stgit-expand-find-copies-harder' flag."
477 (if stgit-expand-find-copies-harder
478 "--find-copies-harder"
479 "-C"))
480
481 (defun stgit-insert-patch-files (patch)
482 "Expand (show modification of) the patch PATCH after the line
483 at point."
484 (let* ((patchsym (stgit-patch-name patch))
485 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
486 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
487 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
488 (setf (stgit-patch-files-ewoc patch) ewoc)
489 (with-temp-buffer
490 (apply 'stgit-run-git
491 (cond ((eq patchsym :work)
492 `("diff-files" ,@args))
493 ((eq patchsym :index)
494 `("diff-index" ,@args "--cached" "HEAD"))
495 (t
496 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
497 (goto-char (point-min))
498 (unless (or (eobp) (memq patchsym '(:work :index)))
499 (forward-char 41))
500 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
501 (let ((old-perm (string-to-number (match-string 1) 8))
502 (new-perm (string-to-number (match-string 2) 8)))
503 (goto-char (match-end 0))
504 (let ((file
505 (cond ((looking-at
506 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
507 (let* ((patch-status (stgit-patch-status patch))
508 (file-subexp (if (eq patch-status 'unapplied)
509 3
510 4))
511 (file (match-string file-subexp)))
512 (make-stgit-file
513 :old-perm old-perm
514 :new-perm new-perm
515 :copy-or-rename t
516 :cr-score (string-to-number (match-string 2))
517 :cr-from (match-string 3)
518 :cr-to (match-string 4)
519 :status (stgit-file-status-code
520 (match-string 1))
521 :file file)))
522 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
523 (make-stgit-file
524 :old-perm old-perm
525 :new-perm new-perm
526 :copy-or-rename nil
527 :cr-score nil
528 :cr-from nil
529 :cr-to nil
530 :status (stgit-file-status-code
531 (match-string 1))
532 :file (match-string 2))))))
533 (ewoc-enter-last ewoc file))
534 (goto-char (match-end 0))))
535 (unless (ewoc-nth ewoc 0)
536 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
537 'face 'stgit-description-face))))
538 (goto-char end)
539 (delete-char -2)))
540
541 (defun stgit-select-file ()
542 (let ((filename (expand-file-name
543 (stgit-file-file (stgit-patched-file-at-point)))))
544 (unless (file-exists-p filename)
545 (error "File does not exist"))
546 (find-file filename)))
547
548 (defun stgit-select-patch ()
549 (let ((patchname (stgit-patch-name-at-point)))
550 (if (memq patchname stgit-expanded-patches)
551 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
552 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
553 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
554 (move-to-column (stgit-goal-column)))
555
556 (defun stgit-select ()
557 "Expand or collapse the current entry"
558 (interactive)
559 (case (get-text-property (point) 'entry-type)
560 ('patch
561 (stgit-select-patch))
562 ('file
563 (stgit-select-file))
564 (t
565 (error "No patch or file on line"))))
566
567 (defun stgit-find-file-other-window ()
568 "Open file at point in other window"
569 (interactive)
570 (let ((patched-file (stgit-patched-file-at-point)))
571 (unless patched-file
572 (error "No file on the current line"))
573 (let ((filename (expand-file-name (stgit-file-file patched-file))))
574 (unless (file-exists-p filename)
575 (error "File does not exist"))
576 (find-file-other-window filename))))
577
578 (defun stgit-quit ()
579 "Hide the stgit buffer."
580 (interactive)
581 (bury-buffer))
582
583 (defun stgit-git-status ()
584 "Show status using `git-status'."
585 (interactive)
586 (unless (fboundp 'git-status)
587 (error "The stgit-git-status command requires git-status"))
588 (let ((dir default-directory))
589 (save-selected-window
590 (pop-to-buffer nil)
591 (git-status dir))))
592
593 (defun stgit-goal-column ()
594 "Return goal column for the current line"
595 (case (get-text-property (point) 'entry-type)
596 ('patch 2)
597 ('file 4)
598 (t 0)))
599
600 (defun stgit-next-line (&optional arg)
601 "Move cursor vertically down ARG lines"
602 (interactive "p")
603 (next-line arg)
604 (move-to-column (stgit-goal-column)))
605
606 (defun stgit-previous-line (&optional arg)
607 "Move cursor vertically up ARG lines"
608 (interactive "p")
609 (previous-line arg)
610 (move-to-column (stgit-goal-column)))
611
612 (defun stgit-next-patch (&optional arg)
613 "Move cursor down ARG patches."
614 (interactive "p")
615 (ewoc-goto-next stgit-ewoc (or arg 1))
616 (move-to-column goal-column))
617
618 (defun stgit-previous-patch (&optional arg)
619 "Move cursor up ARG patches."
620 (interactive "p")
621 (ewoc-goto-prev stgit-ewoc (or arg 1))
622 (move-to-column goal-column))
623
624 (defvar stgit-mode-hook nil
625 "Run after `stgit-mode' is setup.")
626
627 (defvar stgit-mode-map nil
628 "Keymap for StGit major mode.")
629
630 (unless stgit-mode-map
631 (let ((toggle-map (make-keymap)))
632 (suppress-keymap toggle-map)
633 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
634 '(("t" . stgit-toggle-worktree)))
635 (setq stgit-mode-map (make-keymap))
636 (suppress-keymap stgit-mode-map)
637 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
638 `((" " . stgit-mark)
639 ("m" . stgit-mark)
640 ("\d" . stgit-unmark-up)
641 ("u" . stgit-unmark-down)
642 ("?" . stgit-help)
643 ("h" . stgit-help)
644 ("\C-p" . stgit-previous-line)
645 ("\C-n" . stgit-next-line)
646 ([up] . stgit-previous-line)
647 ([down] . stgit-next-line)
648 ("p" . stgit-previous-patch)
649 ("n" . stgit-next-patch)
650 ("\M-{" . stgit-previous-patch)
651 ("\M-}" . stgit-next-patch)
652 ("s" . stgit-git-status)
653 ("g" . stgit-reload-or-repair)
654 ("r" . stgit-refresh)
655 ("\C-c\C-r" . stgit-rename)
656 ("e" . stgit-edit)
657 ("M" . stgit-move-patches)
658 ("S" . stgit-squash)
659 ("N" . stgit-new)
660 ("\C-c\C-c" . stgit-commit)
661 ("\C-c\C-u" . stgit-uncommit)
662 ("U" . stgit-revert-file)
663 ("R" . stgit-resolve-file)
664 ("\r" . stgit-select)
665 ("o" . stgit-find-file-other-window)
666 ("i" . stgit-file-toggle-index)
667 (">" . stgit-push-next)
668 ("<" . stgit-pop-next)
669 ("P" . stgit-push-or-pop)
670 ("G" . stgit-goto)
671 ("=" . stgit-show)
672 ("D" . stgit-delete)
673 ([(control ?/)] . stgit-undo)
674 ("\C-_" . stgit-undo)
675 ("B" . stgit-branch)
676 ("t" . ,toggle-map)
677 ("q" . stgit-quit)))))
678
679 (defun stgit-mode ()
680 "Major mode for interacting with StGit.
681 Commands:
682 \\{stgit-mode-map}"
683 (kill-all-local-variables)
684 (buffer-disable-undo)
685 (setq mode-name "StGit"
686 major-mode 'stgit-mode
687 goal-column 2)
688 (use-local-map stgit-mode-map)
689 (set (make-local-variable 'list-buffers-directory) default-directory)
690 (set (make-local-variable 'stgit-marked-patches) nil)
691 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
692 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
693 (set (make-local-variable 'stgit-index-node) nil)
694 (set (make-local-variable 'stgit-worktree-node) nil)
695 (set-variable 'truncate-lines 't)
696 (add-hook 'after-save-hook 'stgit-update-saved-file)
697 (run-hooks 'stgit-mode-hook))
698
699 (defun stgit-update-saved-file ()
700 (let* ((file (expand-file-name buffer-file-name))
701 (dir (file-name-directory file))
702 (gitdir (condition-case nil (git-get-top-dir dir)
703 (error nil)))
704 (buffer (and gitdir (stgit-find-buffer gitdir))))
705 (when buffer
706 (with-current-buffer buffer
707 (stgit-refresh-worktree)))))
708
709 (defun stgit-add-mark (patchsym)
710 "Mark the patch PATCHSYM."
711 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
712
713 (defun stgit-remove-mark (patchsym)
714 "Unmark the patch PATCHSYM."
715 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
716
717 (defun stgit-clear-marks ()
718 "Unmark all patches."
719 (setq stgit-marked-patches '()))
720
721 (defun stgit-patch-at-point (&optional cause-error)
722 (get-text-property (point) 'patch-data))
723
724 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
725 "Return the patch name on the current line as a symbol.
726 If CAUSE-ERROR is not nil, signal an error if none found.
727 If ONLY-PATCHES is not nil, only allow real patches, and not
728 index or work tree."
729 (let ((patch (stgit-patch-at-point)))
730 (and patch
731 only-patches
732 (memq (stgit-patch-status patch) '(work index))
733 (setq patch nil))
734 (cond (patch
735 (stgit-patch-name patch))
736 (cause-error
737 (error "No patch on this line")))))
738
739 (defun stgit-patched-file-at-point ()
740 (get-text-property (point) 'file-data))
741
742 (defun stgit-patches-marked-or-at-point ()
743 "Return the symbols of the marked patches, or the patch on the current line."
744 (if stgit-marked-patches
745 stgit-marked-patches
746 (let ((patch (stgit-patch-name-at-point)))
747 (if patch
748 (list patch)
749 '()))))
750
751 (defun stgit-goto-patch (patchsym)
752 "Move point to the line containing patch PATCHSYM.
753 If that patch cannot be found, do nothing."
754 (let ((node (ewoc-nth stgit-ewoc 0)))
755 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
756 patchsym)))
757 (setq node (ewoc-next stgit-ewoc node)))
758 (when node
759 (ewoc-goto-node stgit-ewoc node)
760 (move-to-column goal-column))))
761
762 (defun stgit-init ()
763 "Run stg init."
764 (interactive)
765 (stgit-capture-output nil
766 (stgit-run "init"))
767 (stgit-reload))
768
769 (defun stgit-mark ()
770 "Mark the patch under point."
771 (interactive)
772 (let* ((node (ewoc-locate stgit-ewoc))
773 (patch (ewoc-data node))
774 (name (stgit-patch-name patch)))
775 (when (eq name :work)
776 (error "Cannot mark the work tree"))
777 (when (eq name :index)
778 (error "Cannot mark the index"))
779 (stgit-add-mark (stgit-patch-name patch))
780 (ewoc-invalidate stgit-ewoc node))
781 (stgit-next-patch))
782
783 (defun stgit-unmark-up ()
784 "Remove mark from the patch on the previous line."
785 (interactive)
786 (stgit-previous-patch)
787 (let* ((node (ewoc-locate stgit-ewoc))
788 (patch (ewoc-data node)))
789 (stgit-remove-mark (stgit-patch-name patch))
790 (ewoc-invalidate stgit-ewoc node))
791 (move-to-column (stgit-goal-column)))
792
793 (defun stgit-unmark-down ()
794 "Remove mark from the patch on the current line."
795 (interactive)
796 (let* ((node (ewoc-locate stgit-ewoc))
797 (patch (ewoc-data node)))
798 (stgit-remove-mark (stgit-patch-name patch))
799 (ewoc-invalidate stgit-ewoc node))
800 (stgit-next-patch))
801
802 (defun stgit-rename (name)
803 "Rename the patch under point to NAME."
804 (interactive (list
805 (read-string "Patch name: "
806 (symbol-name (stgit-patch-name-at-point t t)))))
807 (let ((old-patchsym (stgit-patch-name-at-point t t)))
808 (stgit-capture-output nil
809 (stgit-run "rename" old-patchsym name))
810 (let ((name-sym (intern name)))
811 (when (memq old-patchsym stgit-expanded-patches)
812 (setq stgit-expanded-patches
813 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
814 (when (memq old-patchsym stgit-marked-patches)
815 (setq stgit-marked-patches
816 (cons name-sym (delq old-patchsym stgit-marked-patches))))
817 (stgit-reload)
818 (stgit-goto-patch name-sym))))
819
820 (defun stgit-reload-or-repair (repair)
821 "Update the contents of the StGit buffer (`stgit-reload').
822
823 With a prefix argument, repair the StGit metadata if the branch
824 was modified with git commands (`stgit-repair')."
825 (interactive "P")
826 (if repair
827 (stgit-repair)
828 (stgit-reload)))
829
830 (defun stgit-repair ()
831 "Run stg repair."
832 (interactive)
833 (stgit-capture-output nil
834 (stgit-run "repair"))
835 (stgit-reload))
836
837 (defun stgit-available-branches ()
838 "Returns a list of the available stg branches"
839 (let ((output (with-output-to-string
840 (stgit-run "branch" "--list")))
841 (start 0)
842 result)
843 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
844 (setq result (cons (match-string 1 output) result))
845 (setq start (match-end 0)))
846 result))
847
848 (defun stgit-branch (branch)
849 "Switch to branch BRANCH."
850 (interactive (list (completing-read "Switch to branch: "
851 (stgit-available-branches))))
852 (stgit-capture-output nil (stgit-run "branch" "--" branch))
853 (stgit-reload))
854
855 (defun stgit-commit (count)
856 "Run stg commit on COUNT commits.
857 Interactively, the prefix argument is used as COUNT."
858 (interactive "p")
859 (stgit-capture-output nil (stgit-run "commit" "-n" count))
860 (stgit-reload))
861
862 (defun stgit-revert-file ()
863 "Revert the file at point, which must be in the index or the
864 working tree."
865 (interactive)
866 (let* ((patched-file (or (stgit-patched-file-at-point)
867 (error "No file on the current line")))
868 (patch-name (stgit-patch-name-at-point))
869 (file-status (stgit-file-status patched-file))
870 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
871 (stgit-file-cr-to patched-file))
872 ((eq file-status 'add)
873 (stgit-file-file patched-file))))
874 (co-file (cond ((eq file-status 'rename)
875 (stgit-file-cr-from patched-file))
876 ((not (memq file-status '(copy add)))
877 (stgit-file-file patched-file)))))
878
879 (unless (memq patch-name '(:work :index))
880 (error "No index or working tree file on this line"))
881
882 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
883 (when (yes-or-no-p (format "Revert %d file%s? "
884 nfiles
885 (if (= nfiles 1) "" "s")))
886 (stgit-capture-output nil
887 (when rm-file
888 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
889 (when co-file
890 (stgit-run-git "checkout" "HEAD" co-file)))
891 (stgit-reload)))))
892
893 (defun stgit-resolve-file ()
894 "Resolve conflict in the file at point."
895 (interactive)
896 (let* ((patched-file (stgit-patched-file-at-point))
897 (patch (stgit-patch-at-point))
898 (patch-name (and patch (stgit-patch-name patch)))
899 (status (and patched-file (stgit-file-status patched-file))))
900
901 (unless (memq patch-name '(:work :index))
902 (error "No index or working tree file on this line"))
903
904 (unless (eq status 'unmerged)
905 (error "No conflict to resolve at the current line"))
906
907 (stgit-capture-output nil
908 (stgit-move-change-to-index (stgit-file-file patched-file)))
909
910 (stgit-reload)))
911
912 (defun stgit-uncommit (count)
913 "Run stg uncommit on COUNT commits.
914 Interactively, the prefix argument is used as COUNT."
915 (interactive "p")
916 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
917 (stgit-reload))
918
919 (defun stgit-push-next (npatches)
920 "Push the first unapplied patch.
921 With numeric prefix argument, push that many patches."
922 (interactive "p")
923 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
924 (stgit-reload)
925 (stgit-refresh-git-status))
926
927 (defun stgit-pop-next (npatches)
928 "Pop the topmost applied patch.
929 With numeric prefix argument, pop that many patches."
930 (interactive "p")
931 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
932 (stgit-reload)
933 (stgit-refresh-git-status))
934
935 (defun stgit-applied-at-point ()
936 "Is the patch on the current line applied?"
937 (save-excursion
938 (beginning-of-line)
939 (looking-at "[>+]")))
940
941 (defun stgit-push-or-pop ()
942 "Push or pop the patch on the current line."
943 (interactive)
944 (let ((patchsym (stgit-patch-name-at-point t))
945 (applied (stgit-applied-at-point)))
946 (stgit-capture-output nil
947 (stgit-run (if applied "pop" "push") patchsym))
948 (stgit-reload)))
949
950 (defun stgit-goto ()
951 "Go to the patch on the current line."
952 (interactive)
953 (let ((patchsym (stgit-patch-name-at-point t)))
954 (stgit-capture-output nil
955 (stgit-run "goto" patchsym))
956 (stgit-reload)))
957
958 (defun stgit-id (patchsym)
959 "Return the git commit id for PATCHSYM.
960 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
961 (if (keywordp patchsym)
962 patchsym
963 (let ((result (with-output-to-string
964 (stgit-run-silent "id" patchsym))))
965 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
966 (error "Cannot find commit id for %s" patchsym))
967 (match-string 1 result))))
968
969 (defun stgit-show ()
970 "Show the patch on the current line."
971 (interactive)
972 (stgit-capture-output "*StGit patch*"
973 (case (get-text-property (point) 'entry-type)
974 ('file
975 (let* ((patched-file (stgit-patched-file-at-point))
976 (patch-name (stgit-patch-name-at-point))
977 (patch-id (stgit-id patch-name))
978 (args (append (and (stgit-file-cr-from patched-file)
979 (list (stgit-find-copies-harder-diff-arg)))
980 (cond ((eq patch-id :index)
981 '("--cached"))
982 ((eq patch-id :work)
983 nil)
984 (t
985 (list (concat patch-id "^") patch-id)))
986 '("--")
987 (if (stgit-file-copy-or-rename patched-file)
988 (list (stgit-file-cr-from patched-file)
989 (stgit-file-cr-to patched-file))
990 (list (stgit-file-file patched-file))))))
991 (apply 'stgit-run-git "diff" args)))
992 ('patch
993 (let* ((patch-name (stgit-patch-name-at-point))
994 (patch-id (stgit-id patch-name)))
995 (if (or (eq patch-id :index) (eq patch-id :work))
996 (apply 'stgit-run-git "diff"
997 (stgit-find-copies-harder-diff-arg)
998 (and (eq patch-id :index)
999 '("--cached")))
1000 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
1001 (stgit-patch-name-at-point)))))
1002 (t
1003 (error "No patch or file at point")))
1004 (with-current-buffer standard-output
1005 (goto-char (point-min))
1006 (diff-mode))))
1007
1008 (defun stgit-move-change-to-index (file)
1009 "Copies the workspace state of FILE to index, using git add or git rm"
1010 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1011 '("add") '("rm" "-q"))))
1012 (stgit-capture-output "*git output*"
1013 (apply 'stgit-run-git (append op '("--") (list file))))))
1014
1015 (defun stgit-remove-change-from-index (file)
1016 "Unstages the change in FILE from the index"
1017 (stgit-capture-output "*git output*"
1018 (stgit-run-git "reset" "-q" "--" file)))
1019
1020 (defun stgit-file-toggle-index ()
1021 "Move modified file in or out of the index."
1022 (interactive)
1023 (let ((patched-file (stgit-patched-file-at-point)))
1024 (unless patched-file
1025 (error "No file on the current line"))
1026 (when (eq (stgit-file-status patched-file) 'unmerged)
1027 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1028 (let ((patch-name (stgit-patch-name-at-point)))
1029 (cond ((eq patch-name :work)
1030 (stgit-move-change-to-index (stgit-file-file patched-file)))
1031 ((eq patch-name :index)
1032 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1033 (t
1034 (error "Can only move files in the working tree to index")))))
1035 (stgit-refresh-worktree)
1036 (stgit-refresh-index))
1037
1038 (defun stgit-edit ()
1039 "Edit the patch on the current line."
1040 (interactive)
1041 (let ((patchsym (stgit-patch-name-at-point t t))
1042 (edit-buf (get-buffer-create "*StGit edit*"))
1043 (dir default-directory))
1044 (log-edit 'stgit-confirm-edit t nil edit-buf)
1045 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1046 (setq default-directory dir)
1047 (let ((standard-output edit-buf))
1048 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1049
1050 (defun stgit-confirm-edit ()
1051 (interactive)
1052 (let ((file (make-temp-file "stgit-edit-")))
1053 (write-region (point-min) (point-max) file)
1054 (stgit-capture-output nil
1055 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1056 (with-current-buffer log-edit-parent-buffer
1057 (stgit-reload))))
1058
1059 (defun stgit-new (add-sign)
1060 "Create a new patch.
1061 With a prefix argument, include a \"Signed-off-by:\" line at the
1062 end of the patch."
1063 (interactive "P")
1064 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1065 (dir default-directory))
1066 (log-edit 'stgit-confirm-new t nil edit-buf)
1067 (setq default-directory dir)
1068 (when add-sign
1069 (save-excursion
1070 (let ((standard-output (current-buffer)))
1071 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1072
1073 (defun stgit-confirm-new ()
1074 (interactive)
1075 (let ((file (make-temp-file "stgit-edit-")))
1076 (write-region (point-min) (point-max) file)
1077 (stgit-capture-output nil
1078 (stgit-run "new" "-f" file))
1079 (with-current-buffer log-edit-parent-buffer
1080 (stgit-reload))))
1081
1082 (defun stgit-create-patch-name (description)
1083 "Create a patch name from a long description"
1084 (let ((patch ""))
1085 (while (> (length description) 0)
1086 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1087 (setq patch (downcase (concat patch
1088 (match-string 0 description))))
1089 (setq description (substring description (match-end 0))))
1090 ((string-match "\\` +" description)
1091 (setq patch (concat patch "-"))
1092 (setq description (substring description (match-end 0))))
1093 ((string-match "\\`[^a-zA-Z_-]+" description)
1094 (setq description (substring description (match-end 0))))))
1095 (cond ((= (length patch) 0)
1096 "patch")
1097 ((> (length patch) 20)
1098 (substring patch 0 20))
1099 (t patch))))
1100
1101 (defun stgit-delete (patchsyms &optional spill-p)
1102 "Delete the patches in PATCHSYMS.
1103 Interactively, delete the marked patches, or the patch at point.
1104
1105 With a prefix argument, or SPILL-P, spill the patch contents to
1106 the work tree and index."
1107 (interactive (list (stgit-patches-marked-or-at-point)
1108 current-prefix-arg))
1109 (unless patchsyms
1110 (error "No patches to delete"))
1111 (when (memq :index patchsyms)
1112 (error "Cannot delete the index"))
1113 (when (memq :work patchsyms)
1114 (error "Cannot delete the work tree"))
1115
1116 (let ((npatches (length patchsyms)))
1117 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1118 npatches
1119 (if (= 1 npatches) "" "es")
1120 (if spill-p
1121 " (spilling contents to index)"
1122 "")))
1123 (let ((args (if spill-p
1124 (cons "--spill" patchsyms)
1125 patchsyms)))
1126 (stgit-capture-output nil
1127 (apply 'stgit-run "delete" args))
1128 (stgit-reload)))))
1129
1130 (defun stgit-move-patches-target ()
1131 "Return the patchsym indicating a target patch for
1132 `stgit-move-patches'.
1133
1134 This is either the patch at point, or one of :top and :bottom, if
1135 the point is after or before the applied patches."
1136
1137 (let ((patchsym (stgit-patch-name-at-point)))
1138 (cond (patchsym patchsym)
1139 ((save-excursion (re-search-backward "^>" nil t)) :top)
1140 (t :bottom))))
1141
1142 (defun stgit-sort-patches (patchsyms)
1143 "Returns the list of patches in PATCHSYMS sorted according to
1144 their position in the patch series, bottommost first.
1145
1146 PATCHSYMS may not contain duplicate entries."
1147 (let (sorted-patchsyms
1148 (series (with-output-to-string
1149 (with-current-buffer standard-output
1150 (stgit-run-silent "series" "--noprefix"))))
1151 start)
1152 (while (string-match "^\\(.+\\)" series start)
1153 (let ((patchsym (intern (match-string 1 series))))
1154 (when (memq patchsym patchsyms)
1155 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1156 (setq start (match-end 0)))
1157 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1158
1159 (unless (= (length patchsyms) (length sorted-patchsyms))
1160 (error "Internal error"))
1161
1162 sorted-patchsyms))
1163
1164 (defun stgit-move-patches (patchsyms target-patch)
1165 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1166 If TARGET-PATCH is :bottom or :top, move the patches to the
1167 bottom or top of the stack, respectively.
1168
1169 Interactively, move the marked patches to where the point is."
1170 (interactive (list stgit-marked-patches
1171 (stgit-move-patches-target)))
1172 (unless patchsyms
1173 (error "Need at least one patch to move"))
1174
1175 (unless target-patch
1176 (error "Point not at a patch"))
1177
1178 (if (eq target-patch :top)
1179 (stgit-capture-output nil
1180 (apply 'stgit-run "float" patchsyms))
1181
1182 ;; need to have patchsyms sorted by position in the stack
1183 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1184 (while sorted-patchsyms
1185 (setq sorted-patchsyms
1186 (and (stgit-capture-output nil
1187 (if (eq target-patch :bottom)
1188 (stgit-run "sink" "--" (car sorted-patchsyms))
1189 (stgit-run "sink" "--to" target-patch "--"
1190 (car sorted-patchsyms))))
1191 (cdr sorted-patchsyms))))))
1192 (stgit-reload))
1193
1194 (defun stgit-squash (patchsyms)
1195 "Squash the patches in PATCHSYMS.
1196 Interactively, squash the marked patches.
1197
1198 Unless there are any conflicts, the patches will be merged into
1199 one patch, which will occupy the same spot in the series as the
1200 deepest patch had before the squash."
1201 (interactive (list stgit-marked-patches))
1202 (when (< (length patchsyms) 2)
1203 (error "Need at least two patches to squash"))
1204 (let ((stgit-buffer (current-buffer))
1205 (edit-buf (get-buffer-create "*StGit edit*"))
1206 (dir default-directory)
1207 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1208 (log-edit 'stgit-confirm-squash t nil edit-buf)
1209 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1210 (setq default-directory dir)
1211 (let ((result (let ((standard-output edit-buf))
1212 (apply 'stgit-run-silent "squash"
1213 "--save-template=-" sorted-patchsyms))))
1214
1215 ;; stg squash may have reordered the patches or caused conflicts
1216 (with-current-buffer stgit-buffer
1217 (stgit-reload))
1218
1219 (unless (eq 0 result)
1220 (fundamental-mode)
1221 (rename-buffer "*StGit error*")
1222 (resize-temp-buffer-window)
1223 (switch-to-buffer-other-window stgit-buffer)
1224 (error "stg squash failed")))))
1225
1226 (defun stgit-confirm-squash ()
1227 (interactive)
1228 (let ((file (make-temp-file "stgit-edit-")))
1229 (write-region (point-min) (point-max) file)
1230 (stgit-capture-output nil
1231 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1232 (with-current-buffer log-edit-parent-buffer
1233 (stgit-clear-marks)
1234 ;; Go to first marked patch and stay there
1235 (goto-char (point-min))
1236 (re-search-forward (concat "^[>+-]\\*") nil t)
1237 (move-to-column goal-column)
1238 (let ((pos (point)))
1239 (stgit-reload)
1240 (goto-char pos)))))
1241
1242 (defun stgit-help ()
1243 "Display help for the StGit mode."
1244 (interactive)
1245 (describe-function 'stgit-mode))
1246
1247 (defun stgit-undo (&optional arg)
1248 "Run stg undo.
1249 With prefix argument, run it with the --hard flag."
1250 (interactive "P")
1251 (stgit-capture-output nil
1252 (if arg
1253 (stgit-run "undo" "--hard")
1254 (stgit-run "undo")))
1255 (stgit-reload))
1256
1257 (defun stgit-refresh (&optional arg)
1258 "Run stg refresh.
1259 If the index contains any changes, only refresh from index.
1260
1261 With prefix argument, refresh the marked patch or the patch under point."
1262 (interactive "P")
1263 (let ((patchargs (if arg
1264 (let ((patches (stgit-patches-marked-or-at-point)))
1265 (cond ((null patches)
1266 (error "No patch to update"))
1267 ((> (length patches) 1)
1268 (error "Too many patches selected"))
1269 (t
1270 (cons "-p" patches))))
1271 nil)))
1272 (unless (stgit-index-empty-p)
1273 (setq patchargs (cons "--index" patchargs)))
1274 (stgit-capture-output nil
1275 (apply 'stgit-run "refresh" patchargs))
1276 (stgit-refresh-git-status))
1277 (stgit-reload))
1278
1279 (defcustom stgit-show-worktree-mode 'center
1280 "This variable controls where the \"Index\" and \"Work tree\"
1281 will be shown on in the buffer.
1282
1283 It can be set to 'top (above all patches), 'center (show between
1284 applied and unapplied patches), and 'bottom (below all patches).
1285
1286 See also `stgit-show-worktree'."
1287 :type '(radio (const :tag "above all patches (top)" top)
1288 (const :tag "between applied and unapplied patches (center)"
1289 center)
1290 (const :tag "below all patches (bottom)" bottom))
1291 :group 'stgit)
1292
1293 (defcustom stgit-default-show-worktree
1294 nil
1295 "Set to non-nil to by default show the working tree in a new stgit buffer.
1296
1297 This value is used as the default value for `stgit-show-worktree'."
1298 :type 'boolean
1299 :group 'stgit)
1300
1301 (defvar stgit-show-worktree nil
1302 "If nil, inhibit showing work tree and index in the stgit buffer.
1303
1304 See also `stgit-show-worktree-mode'.")
1305
1306 (defun stgit-toggle-worktree (&optional arg)
1307 "Toggle the visibility of the work tree.
1308 With arg, show the work tree if arg is positive.
1309
1310 Its initial setting is controlled by `stgit-default-show-worktree'.
1311
1312 `stgit-show-worktree-mode' controls where on screen the index and
1313 work tree will show up."
1314 (interactive)
1315 (setq stgit-show-worktree
1316 (if (numberp arg)
1317 (> arg 0)
1318 (not stgit-show-worktree)))
1319 (stgit-reload))
1320
1321 (provide 'stgit)