stgit.el: Document stgit-select properly
[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 "With point on a patch, toggle showing files in the patch.
558
559 With point on a file, open the associated file. Opens the target
560 file for (applied) copies and renames."
561 (interactive)
562 (case (get-text-property (point) 'entry-type)
563 ('patch
564 (stgit-select-patch))
565 ('file
566 (stgit-select-file))
567 (t
568 (error "No patch or file on line"))))
569
570 (defun stgit-find-file-other-window ()
571 "Open file at point in other window"
572 (interactive)
573 (let ((patched-file (stgit-patched-file-at-point)))
574 (unless patched-file
575 (error "No file on the current line"))
576 (let ((filename (expand-file-name (stgit-file-file patched-file))))
577 (unless (file-exists-p filename)
578 (error "File does not exist"))
579 (find-file-other-window filename))))
580
581 (defun stgit-quit ()
582 "Hide the stgit buffer."
583 (interactive)
584 (bury-buffer))
585
586 (defun stgit-git-status ()
587 "Show status using `git-status'."
588 (interactive)
589 (unless (fboundp 'git-status)
590 (error "The stgit-git-status command requires git-status"))
591 (let ((dir default-directory))
592 (save-selected-window
593 (pop-to-buffer nil)
594 (git-status dir))))
595
596 (defun stgit-goal-column ()
597 "Return goal column for the current line"
598 (case (get-text-property (point) 'entry-type)
599 ('patch 2)
600 ('file 4)
601 (t 0)))
602
603 (defun stgit-next-line (&optional arg)
604 "Move cursor vertically down ARG lines"
605 (interactive "p")
606 (next-line arg)
607 (move-to-column (stgit-goal-column)))
608
609 (defun stgit-previous-line (&optional arg)
610 "Move cursor vertically up ARG lines"
611 (interactive "p")
612 (previous-line arg)
613 (move-to-column (stgit-goal-column)))
614
615 (defun stgit-next-patch (&optional arg)
616 "Move cursor down ARG patches."
617 (interactive "p")
618 (ewoc-goto-next stgit-ewoc (or arg 1))
619 (move-to-column goal-column))
620
621 (defun stgit-previous-patch (&optional arg)
622 "Move cursor up ARG patches."
623 (interactive "p")
624 (ewoc-goto-prev stgit-ewoc (or arg 1))
625 (move-to-column goal-column))
626
627 (defvar stgit-mode-hook nil
628 "Run after `stgit-mode' is setup.")
629
630 (defvar stgit-mode-map nil
631 "Keymap for StGit major mode.")
632
633 (unless stgit-mode-map
634 (let ((toggle-map (make-keymap)))
635 (suppress-keymap toggle-map)
636 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
637 '(("t" . stgit-toggle-worktree)))
638 (setq stgit-mode-map (make-keymap))
639 (suppress-keymap stgit-mode-map)
640 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
641 `((" " . stgit-mark)
642 ("m" . stgit-mark)
643 ("\d" . stgit-unmark-up)
644 ("u" . stgit-unmark-down)
645 ("?" . stgit-help)
646 ("h" . stgit-help)
647 ("\C-p" . stgit-previous-line)
648 ("\C-n" . stgit-next-line)
649 ([up] . stgit-previous-line)
650 ([down] . stgit-next-line)
651 ("p" . stgit-previous-patch)
652 ("n" . stgit-next-patch)
653 ("\M-{" . stgit-previous-patch)
654 ("\M-}" . stgit-next-patch)
655 ("s" . stgit-git-status)
656 ("g" . stgit-reload-or-repair)
657 ("r" . stgit-refresh)
658 ("\C-c\C-r" . stgit-rename)
659 ("e" . stgit-edit)
660 ("M" . stgit-move-patches)
661 ("S" . stgit-squash)
662 ("N" . stgit-new)
663 ("\C-c\C-c" . stgit-commit)
664 ("\C-c\C-u" . stgit-uncommit)
665 ("U" . stgit-revert-file)
666 ("R" . stgit-resolve-file)
667 ("\r" . stgit-select)
668 ("o" . stgit-find-file-other-window)
669 ("i" . stgit-file-toggle-index)
670 (">" . stgit-push-next)
671 ("<" . stgit-pop-next)
672 ("P" . stgit-push-or-pop)
673 ("G" . stgit-goto)
674 ("=" . stgit-show)
675 ("D" . stgit-delete)
676 ([(control ?/)] . stgit-undo)
677 ("\C-_" . stgit-undo)
678 ("B" . stgit-branch)
679 ("t" . ,toggle-map)
680 ("q" . stgit-quit)))))
681
682 (defun stgit-mode ()
683 "Major mode for interacting with StGit.
684 Commands:
685 \\{stgit-mode-map}"
686 (kill-all-local-variables)
687 (buffer-disable-undo)
688 (setq mode-name "StGit"
689 major-mode 'stgit-mode
690 goal-column 2)
691 (use-local-map stgit-mode-map)
692 (set (make-local-variable 'list-buffers-directory) default-directory)
693 (set (make-local-variable 'stgit-marked-patches) nil)
694 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
695 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
696 (set (make-local-variable 'stgit-index-node) nil)
697 (set (make-local-variable 'stgit-worktree-node) nil)
698 (set-variable 'truncate-lines 't)
699 (add-hook 'after-save-hook 'stgit-update-saved-file)
700 (run-hooks 'stgit-mode-hook))
701
702 (defun stgit-update-saved-file ()
703 (let* ((file (expand-file-name buffer-file-name))
704 (dir (file-name-directory file))
705 (gitdir (condition-case nil (git-get-top-dir dir)
706 (error nil)))
707 (buffer (and gitdir (stgit-find-buffer gitdir))))
708 (when buffer
709 (with-current-buffer buffer
710 (stgit-refresh-worktree)))))
711
712 (defun stgit-add-mark (patchsym)
713 "Mark the patch PATCHSYM."
714 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
715
716 (defun stgit-remove-mark (patchsym)
717 "Unmark the patch PATCHSYM."
718 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
719
720 (defun stgit-clear-marks ()
721 "Unmark all patches."
722 (setq stgit-marked-patches '()))
723
724 (defun stgit-patch-at-point (&optional cause-error)
725 (get-text-property (point) 'patch-data))
726
727 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
728 "Return the patch name on the current line as a symbol.
729 If CAUSE-ERROR is not nil, signal an error if none found.
730 If ONLY-PATCHES is not nil, only allow real patches, and not
731 index or work tree."
732 (let ((patch (stgit-patch-at-point)))
733 (and patch
734 only-patches
735 (memq (stgit-patch-status patch) '(work index))
736 (setq patch nil))
737 (cond (patch
738 (stgit-patch-name patch))
739 (cause-error
740 (error "No patch on this line")))))
741
742 (defun stgit-patched-file-at-point ()
743 (get-text-property (point) 'file-data))
744
745 (defun stgit-patches-marked-or-at-point ()
746 "Return the symbols of the marked patches, or the patch on the current line."
747 (if stgit-marked-patches
748 stgit-marked-patches
749 (let ((patch (stgit-patch-name-at-point)))
750 (if patch
751 (list patch)
752 '()))))
753
754 (defun stgit-goto-patch (patchsym)
755 "Move point to the line containing patch PATCHSYM.
756 If that patch cannot be found, do nothing."
757 (let ((node (ewoc-nth stgit-ewoc 0)))
758 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
759 patchsym)))
760 (setq node (ewoc-next stgit-ewoc node)))
761 (when node
762 (ewoc-goto-node stgit-ewoc node)
763 (move-to-column goal-column))))
764
765 (defun stgit-init ()
766 "Run stg init."
767 (interactive)
768 (stgit-capture-output nil
769 (stgit-run "init"))
770 (stgit-reload))
771
772 (defun stgit-mark ()
773 "Mark the patch under point."
774 (interactive)
775 (let* ((node (ewoc-locate stgit-ewoc))
776 (patch (ewoc-data node))
777 (name (stgit-patch-name patch)))
778 (when (eq name :work)
779 (error "Cannot mark the work tree"))
780 (when (eq name :index)
781 (error "Cannot mark the index"))
782 (stgit-add-mark (stgit-patch-name patch))
783 (ewoc-invalidate stgit-ewoc node))
784 (stgit-next-patch))
785
786 (defun stgit-unmark-up ()
787 "Remove mark from the patch on the previous line."
788 (interactive)
789 (stgit-previous-patch)
790 (let* ((node (ewoc-locate stgit-ewoc))
791 (patch (ewoc-data node)))
792 (stgit-remove-mark (stgit-patch-name patch))
793 (ewoc-invalidate stgit-ewoc node))
794 (move-to-column (stgit-goal-column)))
795
796 (defun stgit-unmark-down ()
797 "Remove mark from the patch on the current line."
798 (interactive)
799 (let* ((node (ewoc-locate stgit-ewoc))
800 (patch (ewoc-data node)))
801 (stgit-remove-mark (stgit-patch-name patch))
802 (ewoc-invalidate stgit-ewoc node))
803 (stgit-next-patch))
804
805 (defun stgit-rename (name)
806 "Rename the patch under point to NAME."
807 (interactive (list
808 (read-string "Patch name: "
809 (symbol-name (stgit-patch-name-at-point t t)))))
810 (let ((old-patchsym (stgit-patch-name-at-point t t)))
811 (stgit-capture-output nil
812 (stgit-run "rename" old-patchsym name))
813 (let ((name-sym (intern name)))
814 (when (memq old-patchsym stgit-expanded-patches)
815 (setq stgit-expanded-patches
816 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
817 (when (memq old-patchsym stgit-marked-patches)
818 (setq stgit-marked-patches
819 (cons name-sym (delq old-patchsym stgit-marked-patches))))
820 (stgit-reload)
821 (stgit-goto-patch name-sym))))
822
823 (defun stgit-reload-or-repair (repair)
824 "Update the contents of the StGit buffer (`stgit-reload').
825
826 With a prefix argument, repair the StGit metadata if the branch
827 was modified with git commands (`stgit-repair')."
828 (interactive "P")
829 (if repair
830 (stgit-repair)
831 (stgit-reload)))
832
833 (defun stgit-repair ()
834 "Run stg repair."
835 (interactive)
836 (stgit-capture-output nil
837 (stgit-run "repair"))
838 (stgit-reload))
839
840 (defun stgit-available-branches ()
841 "Returns a list of the available stg branches"
842 (let ((output (with-output-to-string
843 (stgit-run "branch" "--list")))
844 (start 0)
845 result)
846 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
847 (setq result (cons (match-string 1 output) result))
848 (setq start (match-end 0)))
849 result))
850
851 (defun stgit-branch (branch)
852 "Switch to branch BRANCH."
853 (interactive (list (completing-read "Switch to branch: "
854 (stgit-available-branches))))
855 (stgit-capture-output nil (stgit-run "branch" "--" branch))
856 (stgit-reload))
857
858 (defun stgit-commit (count)
859 "Run stg commit on COUNT commits.
860 Interactively, the prefix argument is used as COUNT."
861 (interactive "p")
862 (stgit-capture-output nil (stgit-run "commit" "-n" count))
863 (stgit-reload))
864
865 (defun stgit-revert-file ()
866 "Revert the file at point, which must be in the index or the
867 working tree."
868 (interactive)
869 (let* ((patched-file (or (stgit-patched-file-at-point)
870 (error "No file on the current line")))
871 (patch-name (stgit-patch-name-at-point))
872 (file-status (stgit-file-status patched-file))
873 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
874 (stgit-file-cr-to patched-file))
875 ((eq file-status 'add)
876 (stgit-file-file patched-file))))
877 (co-file (cond ((eq file-status 'rename)
878 (stgit-file-cr-from patched-file))
879 ((not (memq file-status '(copy add)))
880 (stgit-file-file patched-file)))))
881
882 (unless (memq patch-name '(:work :index))
883 (error "No index or working tree file on this line"))
884
885 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
886 (when (yes-or-no-p (format "Revert %d file%s? "
887 nfiles
888 (if (= nfiles 1) "" "s")))
889 (stgit-capture-output nil
890 (when rm-file
891 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
892 (when co-file
893 (stgit-run-git "checkout" "HEAD" co-file)))
894 (stgit-reload)))))
895
896 (defun stgit-resolve-file ()
897 "Resolve conflict in the file at point."
898 (interactive)
899 (let* ((patched-file (stgit-patched-file-at-point))
900 (patch (stgit-patch-at-point))
901 (patch-name (and patch (stgit-patch-name patch)))
902 (status (and patched-file (stgit-file-status patched-file))))
903
904 (unless (memq patch-name '(:work :index))
905 (error "No index or working tree file on this line"))
906
907 (unless (eq status 'unmerged)
908 (error "No conflict to resolve at the current line"))
909
910 (stgit-capture-output nil
911 (stgit-move-change-to-index (stgit-file-file patched-file)))
912
913 (stgit-reload)))
914
915 (defun stgit-uncommit (count)
916 "Run stg uncommit on COUNT commits.
917 Interactively, the prefix argument is used as COUNT."
918 (interactive "p")
919 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
920 (stgit-reload))
921
922 (defun stgit-push-next (npatches)
923 "Push the first unapplied patch.
924 With numeric prefix argument, push that many patches."
925 (interactive "p")
926 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
927 (stgit-reload)
928 (stgit-refresh-git-status))
929
930 (defun stgit-pop-next (npatches)
931 "Pop the topmost applied patch.
932 With numeric prefix argument, pop that many patches."
933 (interactive "p")
934 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
935 (stgit-reload)
936 (stgit-refresh-git-status))
937
938 (defun stgit-applied-at-point ()
939 "Is the patch on the current line applied?"
940 (save-excursion
941 (beginning-of-line)
942 (looking-at "[>+]")))
943
944 (defun stgit-push-or-pop ()
945 "Push or pop the patch on the current line."
946 (interactive)
947 (let ((patchsym (stgit-patch-name-at-point t))
948 (applied (stgit-applied-at-point)))
949 (stgit-capture-output nil
950 (stgit-run (if applied "pop" "push") patchsym))
951 (stgit-reload)))
952
953 (defun stgit-goto ()
954 "Go to the patch on the current line."
955 (interactive)
956 (let ((patchsym (stgit-patch-name-at-point t)))
957 (stgit-capture-output nil
958 (stgit-run "goto" patchsym))
959 (stgit-reload)))
960
961 (defun stgit-id (patchsym)
962 "Return the git commit id for PATCHSYM.
963 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
964 (if (keywordp patchsym)
965 patchsym
966 (let ((result (with-output-to-string
967 (stgit-run-silent "id" patchsym))))
968 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
969 (error "Cannot find commit id for %s" patchsym))
970 (match-string 1 result))))
971
972 (defun stgit-show ()
973 "Show the patch on the current line."
974 (interactive)
975 (stgit-capture-output "*StGit patch*"
976 (case (get-text-property (point) 'entry-type)
977 ('file
978 (let* ((patched-file (stgit-patched-file-at-point))
979 (patch-name (stgit-patch-name-at-point))
980 (patch-id (stgit-id patch-name))
981 (args (append (and (stgit-file-cr-from patched-file)
982 (list (stgit-find-copies-harder-diff-arg)))
983 (cond ((eq patch-id :index)
984 '("--cached"))
985 ((eq patch-id :work)
986 nil)
987 (t
988 (list (concat patch-id "^") patch-id)))
989 '("--")
990 (if (stgit-file-copy-or-rename patched-file)
991 (list (stgit-file-cr-from patched-file)
992 (stgit-file-cr-to patched-file))
993 (list (stgit-file-file patched-file))))))
994 (apply 'stgit-run-git "diff" args)))
995 ('patch
996 (let* ((patch-name (stgit-patch-name-at-point))
997 (patch-id (stgit-id patch-name)))
998 (if (or (eq patch-id :index) (eq patch-id :work))
999 (apply 'stgit-run-git "diff"
1000 (stgit-find-copies-harder-diff-arg)
1001 (and (eq patch-id :index)
1002 '("--cached")))
1003 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
1004 (stgit-patch-name-at-point)))))
1005 (t
1006 (error "No patch or file at point")))
1007 (with-current-buffer standard-output
1008 (goto-char (point-min))
1009 (diff-mode))))
1010
1011 (defun stgit-move-change-to-index (file)
1012 "Copies the workspace state of FILE to index, using git add or git rm"
1013 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1014 '("add") '("rm" "-q"))))
1015 (stgit-capture-output "*git output*"
1016 (apply 'stgit-run-git (append op '("--") (list file))))))
1017
1018 (defun stgit-remove-change-from-index (file)
1019 "Unstages the change in FILE from the index"
1020 (stgit-capture-output "*git output*"
1021 (stgit-run-git "reset" "-q" "--" file)))
1022
1023 (defun stgit-file-toggle-index ()
1024 "Move modified file in or out of the index."
1025 (interactive)
1026 (let ((patched-file (stgit-patched-file-at-point)))
1027 (unless patched-file
1028 (error "No file on the current line"))
1029 (when (eq (stgit-file-status patched-file) 'unmerged)
1030 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1031 (let ((patch-name (stgit-patch-name-at-point)))
1032 (cond ((eq patch-name :work)
1033 (stgit-move-change-to-index (stgit-file-file patched-file)))
1034 ((eq patch-name :index)
1035 (stgit-remove-change-from-index (stgit-file-file patched-file)))
1036 (t
1037 (error "Can only move files in the working tree to index")))))
1038 (stgit-refresh-worktree)
1039 (stgit-refresh-index))
1040
1041 (defun stgit-edit ()
1042 "Edit the patch on the current line."
1043 (interactive)
1044 (let ((patchsym (stgit-patch-name-at-point t t))
1045 (edit-buf (get-buffer-create "*StGit edit*"))
1046 (dir default-directory))
1047 (log-edit 'stgit-confirm-edit t nil edit-buf)
1048 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1049 (setq default-directory dir)
1050 (let ((standard-output edit-buf))
1051 (stgit-run-silent "edit" "--save-template=-" patchsym))))
1052
1053 (defun stgit-confirm-edit ()
1054 (interactive)
1055 (let ((file (make-temp-file "stgit-edit-")))
1056 (write-region (point-min) (point-max) file)
1057 (stgit-capture-output nil
1058 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1059 (with-current-buffer log-edit-parent-buffer
1060 (stgit-reload))))
1061
1062 (defun stgit-new (add-sign)
1063 "Create a new patch.
1064 With a prefix argument, include a \"Signed-off-by:\" line at the
1065 end of the patch."
1066 (interactive "P")
1067 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1068 (dir default-directory))
1069 (log-edit 'stgit-confirm-new t nil edit-buf)
1070 (setq default-directory dir)
1071 (when add-sign
1072 (save-excursion
1073 (let ((standard-output (current-buffer)))
1074 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1075
1076 (defun stgit-confirm-new ()
1077 (interactive)
1078 (let ((file (make-temp-file "stgit-edit-")))
1079 (write-region (point-min) (point-max) file)
1080 (stgit-capture-output nil
1081 (stgit-run "new" "-f" file))
1082 (with-current-buffer log-edit-parent-buffer
1083 (stgit-reload))))
1084
1085 (defun stgit-create-patch-name (description)
1086 "Create a patch name from a long description"
1087 (let ((patch ""))
1088 (while (> (length description) 0)
1089 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1090 (setq patch (downcase (concat patch
1091 (match-string 0 description))))
1092 (setq description (substring description (match-end 0))))
1093 ((string-match "\\` +" description)
1094 (setq patch (concat patch "-"))
1095 (setq description (substring description (match-end 0))))
1096 ((string-match "\\`[^a-zA-Z_-]+" description)
1097 (setq description (substring description (match-end 0))))))
1098 (cond ((= (length patch) 0)
1099 "patch")
1100 ((> (length patch) 20)
1101 (substring patch 0 20))
1102 (t patch))))
1103
1104 (defun stgit-delete (patchsyms &optional spill-p)
1105 "Delete the patches in PATCHSYMS.
1106 Interactively, delete the marked patches, or the patch at point.
1107
1108 With a prefix argument, or SPILL-P, spill the patch contents to
1109 the work tree and index."
1110 (interactive (list (stgit-patches-marked-or-at-point)
1111 current-prefix-arg))
1112 (unless patchsyms
1113 (error "No patches to delete"))
1114 (when (memq :index patchsyms)
1115 (error "Cannot delete the index"))
1116 (when (memq :work patchsyms)
1117 (error "Cannot delete the work tree"))
1118
1119 (let ((npatches (length patchsyms)))
1120 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1121 npatches
1122 (if (= 1 npatches) "" "es")
1123 (if spill-p
1124 " (spilling contents to index)"
1125 "")))
1126 (let ((args (if spill-p
1127 (cons "--spill" patchsyms)
1128 patchsyms)))
1129 (stgit-capture-output nil
1130 (apply 'stgit-run "delete" args))
1131 (stgit-reload)))))
1132
1133 (defun stgit-move-patches-target ()
1134 "Return the patchsym indicating a target patch for
1135 `stgit-move-patches'.
1136
1137 This is either the patch at point, or one of :top and :bottom, if
1138 the point is after or before the applied patches."
1139
1140 (let ((patchsym (stgit-patch-name-at-point)))
1141 (cond (patchsym patchsym)
1142 ((save-excursion (re-search-backward "^>" nil t)) :top)
1143 (t :bottom))))
1144
1145 (defun stgit-sort-patches (patchsyms)
1146 "Returns the list of patches in PATCHSYMS sorted according to
1147 their position in the patch series, bottommost first.
1148
1149 PATCHSYMS may not contain duplicate entries."
1150 (let (sorted-patchsyms
1151 (series (with-output-to-string
1152 (with-current-buffer standard-output
1153 (stgit-run-silent "series" "--noprefix"))))
1154 start)
1155 (while (string-match "^\\(.+\\)" series start)
1156 (let ((patchsym (intern (match-string 1 series))))
1157 (when (memq patchsym patchsyms)
1158 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1159 (setq start (match-end 0)))
1160 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1161
1162 (unless (= (length patchsyms) (length sorted-patchsyms))
1163 (error "Internal error"))
1164
1165 sorted-patchsyms))
1166
1167 (defun stgit-move-patches (patchsyms target-patch)
1168 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1169 If TARGET-PATCH is :bottom or :top, move the patches to the
1170 bottom or top of the stack, respectively.
1171
1172 Interactively, move the marked patches to where the point is."
1173 (interactive (list stgit-marked-patches
1174 (stgit-move-patches-target)))
1175 (unless patchsyms
1176 (error "Need at least one patch to move"))
1177
1178 (unless target-patch
1179 (error "Point not at a patch"))
1180
1181 (if (eq target-patch :top)
1182 (stgit-capture-output nil
1183 (apply 'stgit-run "float" patchsyms))
1184
1185 ;; need to have patchsyms sorted by position in the stack
1186 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1187 (while sorted-patchsyms
1188 (setq sorted-patchsyms
1189 (and (stgit-capture-output nil
1190 (if (eq target-patch :bottom)
1191 (stgit-run "sink" "--" (car sorted-patchsyms))
1192 (stgit-run "sink" "--to" target-patch "--"
1193 (car sorted-patchsyms))))
1194 (cdr sorted-patchsyms))))))
1195 (stgit-reload))
1196
1197 (defun stgit-squash (patchsyms)
1198 "Squash the patches in PATCHSYMS.
1199 Interactively, squash the marked patches.
1200
1201 Unless there are any conflicts, the patches will be merged into
1202 one patch, which will occupy the same spot in the series as the
1203 deepest patch had before the squash."
1204 (interactive (list stgit-marked-patches))
1205 (when (< (length patchsyms) 2)
1206 (error "Need at least two patches to squash"))
1207 (let ((stgit-buffer (current-buffer))
1208 (edit-buf (get-buffer-create "*StGit edit*"))
1209 (dir default-directory)
1210 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1211 (log-edit 'stgit-confirm-squash t nil edit-buf)
1212 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1213 (setq default-directory dir)
1214 (let ((result (let ((standard-output edit-buf))
1215 (apply 'stgit-run-silent "squash"
1216 "--save-template=-" sorted-patchsyms))))
1217
1218 ;; stg squash may have reordered the patches or caused conflicts
1219 (with-current-buffer stgit-buffer
1220 (stgit-reload))
1221
1222 (unless (eq 0 result)
1223 (fundamental-mode)
1224 (rename-buffer "*StGit error*")
1225 (resize-temp-buffer-window)
1226 (switch-to-buffer-other-window stgit-buffer)
1227 (error "stg squash failed")))))
1228
1229 (defun stgit-confirm-squash ()
1230 (interactive)
1231 (let ((file (make-temp-file "stgit-edit-")))
1232 (write-region (point-min) (point-max) file)
1233 (stgit-capture-output nil
1234 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1235 (with-current-buffer log-edit-parent-buffer
1236 (stgit-clear-marks)
1237 ;; Go to first marked patch and stay there
1238 (goto-char (point-min))
1239 (re-search-forward (concat "^[>+-]\\*") nil t)
1240 (move-to-column goal-column)
1241 (let ((pos (point)))
1242 (stgit-reload)
1243 (goto-char pos)))))
1244
1245 (defun stgit-help ()
1246 "Display help for the StGit mode."
1247 (interactive)
1248 (describe-function 'stgit-mode))
1249
1250 (defun stgit-undo (&optional arg)
1251 "Run stg undo.
1252 With prefix argument, run it with the --hard flag."
1253 (interactive "P")
1254 (stgit-capture-output nil
1255 (if arg
1256 (stgit-run "undo" "--hard")
1257 (stgit-run "undo")))
1258 (stgit-reload))
1259
1260 (defun stgit-refresh (&optional arg)
1261 "Run stg refresh.
1262 If the index contains any changes, only refresh from index.
1263
1264 With prefix argument, refresh the marked patch or the patch under point."
1265 (interactive "P")
1266 (let ((patchargs (if arg
1267 (let ((patches (stgit-patches-marked-or-at-point)))
1268 (cond ((null patches)
1269 (error "No patch to update"))
1270 ((> (length patches) 1)
1271 (error "Too many patches selected"))
1272 (t
1273 (cons "-p" patches))))
1274 nil)))
1275 (unless (stgit-index-empty-p)
1276 (setq patchargs (cons "--index" patchargs)))
1277 (stgit-capture-output nil
1278 (apply 'stgit-run "refresh" patchargs))
1279 (stgit-refresh-git-status))
1280 (stgit-reload))
1281
1282 (defcustom stgit-show-worktree-mode 'center
1283 "This variable controls where the \"Index\" and \"Work tree\"
1284 will be shown on in the buffer.
1285
1286 It can be set to 'top (above all patches), 'center (show between
1287 applied and unapplied patches), and 'bottom (below all patches).
1288
1289 See also `stgit-show-worktree'."
1290 :type '(radio (const :tag "above all patches (top)" top)
1291 (const :tag "between applied and unapplied patches (center)"
1292 center)
1293 (const :tag "below all patches (bottom)" bottom))
1294 :group 'stgit)
1295
1296 (defcustom stgit-default-show-worktree
1297 nil
1298 "Set to non-nil to by default show the working tree in a new stgit buffer.
1299
1300 This value is used as the default value for `stgit-show-worktree'."
1301 :type 'boolean
1302 :group 'stgit)
1303
1304 (defvar stgit-show-worktree nil
1305 "If nil, inhibit showing work tree and index in the stgit buffer.
1306
1307 See also `stgit-show-worktree-mode'.")
1308
1309 (defun stgit-toggle-worktree (&optional arg)
1310 "Toggle the visibility of the work tree.
1311 With arg, show the work tree if arg is positive.
1312
1313 Its initial setting is controlled by `stgit-default-show-worktree'.
1314
1315 `stgit-show-worktree-mode' controls where on screen the index and
1316 work tree will show up."
1317 (interactive)
1318 (setq stgit-show-worktree
1319 (if (numberp arg)
1320 (> arg 0)
1321 (not stgit-show-worktree)))
1322 (stgit-reload))
1323
1324 (provide 'stgit)