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