stgit.el: Bugfix of stgit-move-patches to top of stack
[stgit] / contrib / stgit.el
CommitLineData
3a59f3db
KH
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
4f7efe0c
GH
12(when (< emacs-major-version 22)
13 (error "Emacs older than 22 is not supported by stgit.el"))
14
0f076fe6 15(require 'git nil t)
50d88c67 16(require 'cl)
98230edd 17(require 'ewoc)
0f076fe6 18
56d81fe5 19(defun stgit (dir)
fdf5e327
GH
20 "Manage StGit patches for the tree in DIR.
21
22See `stgit-mode' for commands available."
56d81fe5 23 (interactive "DDirectory: \n")
52144ce5 24 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 25 (stgit-reload))
56d81fe5 26
074a4fb0
GH
27(unless (fboundp 'git-get-top-dir)
28 (defun git-get-top-dir (dir)
29 "Retrieve the top-level directory of a git tree."
30 (let ((cdup (with-output-to-string
31 (with-current-buffer standard-output
32 (cd dir)
33 (unless (eq 0 (call-process "git" nil t nil
34 "rev-parse" "--show-cdup"))
df283a8b 35 (error "Cannot find top-level git tree for %s" dir))))))
074a4fb0
GH
36 (expand-file-name (concat (file-name-as-directory dir)
37 (car (split-string cdup "\n")))))))
38
39(defun stgit-refresh-git-status (&optional dir)
40 "If it exists, refresh the `git-status' buffer belonging to
41directory DIR or `default-directory'"
42 (when (and (fboundp 'git-find-status-buffer)
43 (fboundp 'git-refresh-status))
44 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
45 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
46 (when git-status-buffer
47 (with-current-buffer git-status-buffer
48 (git-refresh-status))))))
52144ce5 49
b894e680
DK
50(defun stgit-find-buffer (dir)
51 "Return the buffer displaying StGit patches for DIR, or nil if none."
56d81fe5
DK
52 (setq dir (file-name-as-directory dir))
53 (let ((buffers (buffer-list)))
54 (while (and buffers
55 (not (with-current-buffer (car buffers)
56 (and (eq major-mode 'stgit-mode)
57 (string= default-directory dir)))))
58 (setq buffers (cdr buffers)))
b894e680
DK
59 (and buffers (car buffers))))
60
61(defun switch-to-stgit-buffer (dir)
62 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
63 (setq dir (file-name-as-directory dir))
64 (let ((buffer (stgit-find-buffer dir)))
65 (switch-to-buffer (or buffer
66 (create-stgit-buffer dir)))))
67
2c862b07 68(defstruct (stgit-patch)
3164eec6 69 status name desc empty files-ewoc)
56d81fe5 70
98230edd 71(defun stgit-patch-pp (patch)
9153ce3a
GH
72 (let* ((status (stgit-patch-status patch))
73 (start (point))
74 (name (stgit-patch-name patch))
75 (face (cdr (assq status stgit-patch-status-face-alist))))
76 (insert (case status
77 ('applied "+")
78 ('top ">")
79 ('unapplied "-")
80 (t " "))
81 (if (memq name stgit-marked-patches)
82 "*" " "))
83 (if (memq status '(index work))
84 (insert (propertize (if (eq status 'index) "Index" "Work tree")
85 'face face))
86 (insert (format "%-30s"
224ef1ec
GH
87 (propertize (symbol-name name)
88 'face face
89 'syntax-table (string-to-syntax "w")))
9153ce3a
GH
90 " "
91 (if (stgit-patch-empty patch) "(empty) " "")
92 (propertize (or (stgit-patch-desc patch) "")
93 'face 'stgit-description-face)))
4f7ff561 94 (insert "\n")
f9b82d36 95 (put-text-property start (point) 'entry-type 'patch)
98230edd 96 (when (memq name stgit-expanded-patches)
0de6881a 97 (stgit-insert-patch-files patch))
98230edd
DK
98 (put-text-property start (point) 'patch-data patch)))
99
56d81fe5
DK
100(defun create-stgit-buffer (dir)
101 "Create a buffer for showing StGit patches.
102Argument 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)
98230edd 108 (set (make-local-variable 'stgit-ewoc)
4f7ff561 109 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
56d81fe5
DK
110 (setq buffer-read-only t))
111 buf))
112
113(defmacro stgit-capture-output (name &rest body)
e558a4ab
GH
114 "Capture StGit output and, if there was any output, show it in a window
115at the end.
116Returns nil if there was no output."
94baef5a
DK
117 (declare (debug ([&or stringp null] body))
118 (indent 1))
34afb86c
DK
119 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
120 (stgit-dir default-directory)
121 (inhibit-read-only t))
56d81fe5 122 (with-current-buffer output-buf
34afb86c
DK
123 (erase-buffer)
124 (setq default-directory stgit-dir)
125 (setq buffer-read-only t))
56d81fe5
DK
126 (let ((standard-output output-buf))
127 ,@body)
34afb86c
DK
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)))))
56d81fe5 133
d51722b7
GH
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
9aecd505 145(defun stgit-run-silent (&rest args)
d51722b7 146 (setq args (stgit-make-run-args args))
56d81fe5
DK
147 (apply 'call-process "stg" nil standard-output nil args))
148
9aecd505 149(defun stgit-run (&rest args)
d51722b7 150 (setq args (stgit-make-run-args args))
9aecd505
DK
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
378a003d 156(defun stgit-run-git (&rest args)
d51722b7 157 (setq args (stgit-make-run-args args))
378a003d
GH
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
1f60181a 163(defun stgit-run-git-silent (&rest args)
d51722b7 164 (setq args (stgit-make-run-args args))
1f60181a
GH
165 (apply 'call-process "git" nil standard-output nil args))
166
b894e680
DK
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
2ecb05c8
GH
171(defvar stgit-index-node)
172(defvar stgit-worktree-node)
210a2a52
DK
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
8f702de4
GH
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
98230edd 196(defun stgit-run-series (ewoc)
8f702de4
GH
197 (setq stgit-index-node nil
198 stgit-worktree-node nil)
199 (let ((inserted-index (not stgit-show-worktree))
200 index-node
03fc3b26
GH
201 worktree-node
202 all-patchsyms)
98230edd
DK
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: \\(.*\\)")
8f702de4 208 (setq inserted-index t)
98230edd 209 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
8f702de4
GH
210 (substitute-command-keys
211 "-- not initialized; run \\[stgit-init]")))
98230edd
DK
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)
8f702de4
GH
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)))
03fc3b26 232 (setq all-patchsyms (cons name all-patchsyms))
98230edd
DK
233 (ewoc-enter-last ewoc
234 (make-stgit-patch
235 :status state
8f702de4
GH
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
03fc3b26
GH
243 stgit-worktree-node worktree-node
244 stgit-marked-patches (intersection stgit-marked-patches
245 all-patchsyms))))
98230edd 246
1f0bf00f 247(defun stgit-reload ()
a53347d9 248 "Update the contents of the StGit buffer."
56d81fe5
DK
249 (interactive)
250 (let ((inhibit-read-only t)
251 (curline (line-number-at-pos))
a9089e68
GH
252 (curpatch (stgit-patch-name-at-point))
253 (curfile (stgit-patched-file-at-point)))
98230edd
DK
254 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
255 (ewoc-set-hf stgit-ewoc
256 (concat "Branch: "
257 (propertize
258 (with-temp-buffer
259 (stgit-run-silent "branch")
260 (buffer-substring (point-min) (1- (point-max))))
4f292066 261 'face 'stgit-branch-name-face)
4f7ff561 262 "\n\n")
ce3b6130
DK
263 (if stgit-show-worktree
264 "--"
265 (propertize
266 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
267 shows the working tree\n")
268 'face 'stgit-description-face)))
98230edd 269 (stgit-run-series stgit-ewoc)
56d81fe5 270 (if curpatch
a9089e68 271 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
074a4fb0
GH
272 (goto-line curline)))
273 (stgit-refresh-git-status))
56d81fe5 274
f1bcbe9c
GH
275(defun stgit-set-default (symbol value)
276 "Set default value of SYMBOL to VALUE using `set-default' and
277reload all StGit buffers."
278 (set-default symbol value)
279 (dolist (buf (buffer-list))
280 (with-current-buffer buf
281 (when (eq major-mode 'stgit-mode)
282 (stgit-reload)))))
283
8f40753a 284(defgroup stgit nil
f1bcbe9c
GH
285 "A user interface for the StGit patch maintenance tool."
286 :group 'tools
287 :link '(function-link stgit)
288 :link '(url-link "http://www.procode.org/stgit/"))
fdf5e327 289
f1bcbe9c
GH
290(defcustom stgit-abbreviate-copies-and-renames t
291 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
292instead of \"dir/old/file -> dir/new/file\"."
293 :type 'boolean
294 :group 'stgit
295 :set 'stgit-set-default)
8f40753a 296
f1bcbe9c
GH
297(defcustom stgit-default-show-worktree t
298 "Set to non-nil to by default show the working tree in a new stgit buffer.
299
300Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
301 :type 'boolean
302 :group 'stgit
303 :link '(variable-link stgit-show-worktree))
304
305(defcustom stgit-find-copies-harder nil
306 "Try harder to find copied files when listing patches.
307
308When not nil, runs git diff-tree with the --find-copies-harder
309flag, which reduces performance."
310 :type 'boolean
311 :group 'stgit
312 :set 'stgit-set-default)
313
314(defcustom stgit-show-worktree-mode 'center
315 "This variable controls where the \"Index\" and \"Work tree\"
316will be shown on in the buffer.
317
318It can be set to 'top (above all patches), 'center (show between
319applied and unapplied patches), and 'bottom (below all patches)."
320 :type '(radio (const :tag "above all patches (top)" top)
321 (const :tag "between applied and unapplied patches (center)"
322 center)
323 (const :tag "below all patches (bottom)" bottom))
324 :group 'stgit
325 :link '(variable-link stgit-show-worktree)
326 :set 'stgit-set-default)
4f292066
GH
327
328(defface stgit-branch-name-face
329 '((t :inherit bold))
330 "The face used for the StGit branch name"
331 :group 'stgit)
07f464e0
DK
332
333(defface stgit-top-patch-face
334 '((((background dark)) (:weight bold :foreground "yellow"))
335 (((background light)) (:weight bold :foreground "purple"))
336 (t (:weight bold)))
8f40753a
GH
337 "The face used for the top patch names"
338 :group 'stgit)
07f464e0
DK
339
340(defface stgit-applied-patch-face
341 '((((background dark)) (:foreground "light yellow"))
342 (((background light)) (:foreground "purple"))
343 (t ()))
8f40753a
GH
344 "The face used for applied patch names"
345 :group 'stgit)
07f464e0
DK
346
347(defface stgit-unapplied-patch-face
348 '((((background dark)) (:foreground "gray80"))
349 (((background light)) (:foreground "orchid"))
350 (t ()))
8f40753a
GH
351 "The face used for unapplied patch names"
352 :group 'stgit)
07f464e0 353
f1bcbe9c
GH
354(defface stgit-description-face
355 '((((background dark)) (:foreground "tan"))
356 (((background light)) (:foreground "dark red")))
357 "The face used for StGit descriptions"
358 :group 'stgit)
359
360(defface stgit-index-work-tree-title-face
361 '((((supports :slant italic)) :slant italic)
362 (t :inherit bold))
363 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
1f60181a
GH
364 :group 'stgit)
365
366(defface stgit-unmerged-file-face
367 '((((class color) (background light)) (:foreground "red" :bold t))
368 (((class color) (background dark)) (:foreground "red" :bold t)))
369 "StGit mode face used for unmerged file status"
370 :group 'stgit)
371
372(defface stgit-unknown-file-face
373 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
374 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
375 "StGit mode face used for unknown file status"
376 :group 'stgit)
377
d9473917
GH
378(defface stgit-ignored-file-face
379 '((((class color) (background light)) (:foreground "grey60"))
380 (((class color) (background dark)) (:foreground "grey40")))
381 "StGit mode face used for ignored files")
382
a6d9a852
GH
383(defface stgit-file-permission-face
384 '((((class color) (background light)) (:foreground "green" :bold t))
385 (((class color) (background dark)) (:foreground "green" :bold t)))
386 "StGit mode face used for permission changes."
387 :group 'stgit)
388
f1bcbe9c
GH
389(defface stgit-modified-file-face
390 '((((class color) (background light)) (:foreground "purple"))
391 (((class color) (background dark)) (:foreground "salmon")))
392 "StGit mode face used for modified file status"
1f60181a
GH
393 :group 'stgit)
394
395(defconst stgit-file-status-code-strings
396 (mapcar (lambda (arg)
397 (cons (car arg)
a6d9a852
GH
398 (propertize (cadr arg) 'face (car (cddr arg)))))
399 '((add "Added" stgit-modified-file-face)
400 (copy "Copied" stgit-modified-file-face)
401 (delete "Deleted" stgit-modified-file-face)
402 (modify "Modified" stgit-modified-file-face)
403 (rename "Renamed" stgit-modified-file-face)
404 (mode-change "Mode change" stgit-modified-file-face)
405 (unmerged "Unmerged" stgit-unmerged-file-face)
d9473917
GH
406 (unknown "Unknown" stgit-unknown-file-face)
407 (ignore "Ignored" stgit-ignored-file-face)))
1f60181a
GH
408 "Alist of code symbols to description strings")
409
000f337c
GH
410(defconst stgit-patch-status-face-alist
411 '((applied . stgit-applied-patch-face)
412 (top . stgit-top-patch-face)
413 (unapplied . stgit-unapplied-patch-face)
9153ce3a
GH
414 (index . stgit-index-work-tree-title-face)
415 (work . stgit-index-work-tree-title-face))
000f337c
GH
416 "Alist of face to use for a given patch status")
417
3164eec6
DK
418(defun stgit-file-status-code-as-string (file)
419 "Return stgit status code for FILE as a string"
420 (let* ((code (assq (stgit-file-status file)
421 stgit-file-status-code-strings))
422 (score (stgit-file-cr-score file)))
423 (when code
a6d9a852 424 (format "%-11s "
3164eec6
DK
425 (if (and score (/= score 100))
426 (format "%s %s" (cdr code)
427 (propertize (format "%d%%" score)
a6d9a852 428 'face 'stgit-description-face))
3164eec6 429 (cdr code))))))
1f60181a 430
a6d9a852 431(defun stgit-file-status-code (str &optional score)
1f60181a
GH
432 "Return stgit status code from git status string"
433 (let ((code (assoc str '(("A" . add)
434 ("C" . copy)
435 ("D" . delete)
d9473917 436 ("I" . ignore)
1f60181a
GH
437 ("M" . modify)
438 ("R" . rename)
439 ("T" . mode-change)
440 ("U" . unmerged)
441 ("X" . unknown)))))
a6d9a852
GH
442 (setq code (if code (cdr code) 'unknown))
443 (when (stringp score)
444 (if (> (length score) 0)
445 (setq score (string-to-number score))
446 (setq score nil)))
447 (if score (cons code score) code)))
448
449(defconst stgit-file-type-strings
450 '((#o100 . "file")
451 (#o120 . "symlink")
452 (#o160 . "subproject"))
453 "Alist of names of file types")
454
455(defun stgit-file-type-string (type)
47271f41
GH
456 "Return string describing file type TYPE (the high bits of file permission).
457Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
a6d9a852
GH
458 (let ((type-str (assoc type stgit-file-type-strings)))
459 (or (and type-str (cdr type-str))
460 (format "unknown type %o" type))))
461
462(defun stgit-file-type-change-string (old-perm new-perm)
47271f41
GH
463 "Return string describing file type change from OLD-PERM to NEW-PERM.
464Cf. `stgit-file-type-string'."
a6d9a852
GH
465 (let ((old-type (lsh old-perm -9))
466 (new-type (lsh new-perm -9)))
467 (cond ((= old-type new-type) "")
468 ((zerop new-type) "")
469 ((zerop old-type)
470 (if (= new-type #o100)
471 ""
472 (format " (%s)" (stgit-file-type-string new-type))))
473 (t (format " (%s -> %s)"
474 (stgit-file-type-string old-type)
475 (stgit-file-type-string new-type))))))
476
477(defun stgit-file-mode-change-string (old-perm new-perm)
47271f41
GH
478 "Return string describing file mode change from OLD-PERM to NEW-PERM.
479Cf. `stgit-file-type-change-string'."
a6d9a852
GH
480 (setq old-perm (logand old-perm #o777)
481 new-perm (logand new-perm #o777))
482 (if (or (= old-perm new-perm)
483 (zerop old-perm)
484 (zerop new-perm))
485 ""
486 (let* ((modified (logxor old-perm new-perm))
487 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
488 (cond ((zerop modified) "")
489 ((and (zerop not-x-modified)
490 (or (and (eq #o111 (logand old-perm #o111))
491 (propertize "-x" 'face 'stgit-file-permission-face))
492 (and (eq #o111 (logand new-perm #o111))
493 (propertize "+x" 'face
494 'stgit-file-permission-face)))))
495 (t (concat (propertize (format "%o" old-perm)
496 'face 'stgit-file-permission-face)
497 (propertize " -> "
498 'face 'stgit-description-face)
499 (propertize (format "%o" new-perm)
500 'face 'stgit-file-permission-face)))))))
1f60181a 501
0de6881a
DK
502(defstruct (stgit-file)
503 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
504
ca027a87
GH
505(defun stgit-describe-copy-or-rename (file)
506 (let (from to common-head common-tail
507 (arrow (concat " "
508 (propertize "->" 'face 'stgit-description-face)
509 " ")))
510
511 (when stgit-abbreviate-copies-and-renames
512 (setq from (split-string (stgit-file-cr-from file) "/")
513 to (split-string (stgit-file-cr-to file) "/"))
514
515 (while (and from to (cdr from) (cdr to)
516 (string-equal (car from) (car to)))
517 (setq common-head (cons (car from) common-head)
518 from (cdr from)
519 to (cdr to)))
520 (setq common-head (nreverse common-head)
521 from (nreverse from)
522 to (nreverse to))
523 (while (and from to (cdr from) (cdr to)
524 (string-equal (car from) (car to)))
525 (setq common-tail (cons (car from) common-tail)
526 from (cdr from)
527 to (cdr to)))
528 (setq from (nreverse from)
529 to (nreverse to)))
530
531 (if (or common-head common-tail)
532 (concat (if common-head
533 (mapconcat #'identity common-head "/")
534 "")
535 (if common-head "/" "")
536 (propertize "{" 'face 'stgit-description-face)
537 (mapconcat #'identity from "/")
538 arrow
539 (mapconcat #'identity to "/")
540 (propertize "}" 'face 'stgit-description-face)
541 (if common-tail "/" "")
542 (if common-tail
543 (mapconcat #'identity common-tail "/")
544 ""))
545 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
546
3164eec6 547(defun stgit-file-pp (file)
0de6881a
DK
548 (let ((status (stgit-file-status file))
549 (name (if (stgit-file-copy-or-rename file)
ca027a87 550 (stgit-describe-copy-or-rename file)
0de6881a
DK
551 (stgit-file-file file)))
552 (mode-change (stgit-file-mode-change-string
553 (stgit-file-old-perm file)
554 (stgit-file-new-perm file)))
555 (start (point)))
c30518fd 556 (insert (format " %-12s%s%s%s%s\n"
3164eec6 557 (stgit-file-status-code-as-string file)
98230edd 558 mode-change
c30518fd 559 (if (zerop (length mode-change)) "" " ")
0de6881a
DK
560 name
561 (propertize (stgit-file-type-change-string
562 (stgit-file-old-perm file)
563 (stgit-file-new-perm file))
98230edd 564 'face 'stgit-description-face)))
0de6881a 565 (add-text-properties start (point)
3164eec6
DK
566 (list 'entry-type 'file
567 'file-data file))))
0de6881a 568
7567401c
GH
569(defun stgit-find-copies-harder-diff-arg ()
570 "Return the flag to use with `git-diff' depending on the
b6df231c
GH
571`stgit-find-copies-harder' flag."
572 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
7567401c 573
d9473917
GH
574(defun stgit-insert-ls-files (args file-flag)
575 (let ((start (point)))
576 (apply 'stgit-run-git
577 (append '("ls-files" "--exclude-standard" "-z") args))
578 (goto-char start)
579 (while (looking-at "\\([^\0]*\\)\0")
580 (let ((name-len (- (match-end 0) (match-beginning 0))))
581 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
582 (forward-char name-len)))))
583
0de6881a 584(defun stgit-insert-patch-files (patch)
88134ff7
GH
585 "Expand (show modification of) the patch PATCH after the line
586at point."
3164eec6 587 (let* ((patchsym (stgit-patch-name patch))
0434bec1
GH
588 (end (point-marker))
589 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
590 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
591 (set-marker-insertion-type end t)
3164eec6 592 (setf (stgit-patch-files-ewoc patch) ewoc)
0de6881a 593 (with-temp-buffer
b894e680
DK
594 (apply 'stgit-run-git
595 (cond ((eq patchsym :work)
030f0535 596 `("diff-files" "-0" ,@args))
b894e680
DK
597 ((eq patchsym :index)
598 `("diff-index" ,@args "--cached" "HEAD"))
599 (t
600 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
d9473917
GH
601
602 (when (and (eq patchsym :work))
603 (when stgit-show-ignored
604 (stgit-insert-ls-files '("--ignored" "--others") "I"))
605 (when stgit-show-unknown
606 (stgit-insert-ls-files '("--others") "X"))
607 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
608 (point-min) (point-max)))
609
0de6881a 610 (goto-char (point-min))
b894e680
DK
611 (unless (or (eobp) (memq patchsym '(:work :index)))
612 (forward-char 41))
0de6881a
DK
613 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
614 (let ((old-perm (string-to-number (match-string 1) 8))
615 (new-perm (string-to-number (match-string 2) 8)))
616 (goto-char (match-end 0))
617 (let ((file
618 (cond ((looking-at
619 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
027e1370
GH
620 (let* ((patch-status (stgit-patch-status patch))
621 (file-subexp (if (eq patch-status 'unapplied)
622 3
623 4))
624 (file (match-string file-subexp)))
625 (make-stgit-file
626 :old-perm old-perm
627 :new-perm new-perm
628 :copy-or-rename t
629 :cr-score (string-to-number (match-string 2))
630 :cr-from (match-string 3)
631 :cr-to (match-string 4)
632 :status (stgit-file-status-code
633 (match-string 1))
634 :file file)))
0de6881a
DK
635 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
636 (make-stgit-file
637 :old-perm old-perm
638 :new-perm new-perm
639 :copy-or-rename nil
640 :cr-score nil
641 :cr-from nil
642 :cr-to nil
027e1370
GH
643 :status (stgit-file-status-code
644 (match-string 1))
0de6881a 645 :file (match-string 2))))))
807e1273
GH
646 (goto-char (match-end 0))
647 (ewoc-enter-last ewoc file))))
648
3164eec6 649 (unless (ewoc-nth ewoc 0)
000f337c
GH
650 (ewoc-set-hf ewoc ""
651 (concat " "
652 (propertize "<no files>"
653 'face 'stgit-description-face)
654 "\n"))))
0434bec1 655 (goto-char end)))
07f464e0 656
030f0535
GH
657(defun stgit-find-file (&optional other-window)
658 (let* ((file (or (stgit-patched-file-at-point)
659 (error "No file at point")))
660 (filename (expand-file-name (stgit-file-file file))))
0de6881a
DK
661 (unless (file-exists-p filename)
662 (error "File does not exist"))
030f0535
GH
663 (funcall (if other-window 'find-file-other-window 'find-file)
664 filename)
665 (when (eq (stgit-file-status file) 'unmerged)
666 (smerge-mode 1))))
acc5652f 667
50d88c67 668(defun stgit-select-patch ()
98230edd
DK
669 (let ((patchname (stgit-patch-name-at-point)))
670 (if (memq patchname stgit-expanded-patches)
671 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
672 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
673 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
674 (move-to-column (stgit-goal-column)))
acc5652f 675
378a003d 676(defun stgit-select ()
da01a29b
GH
677 "With point on a patch, toggle showing files in the patch.
678
679With point on a file, open the associated file. Opens the target
680file for (applied) copies and renames."
378a003d 681 (interactive)
50d88c67
DK
682 (case (get-text-property (point) 'entry-type)
683 ('patch
684 (stgit-select-patch))
685 ('file
030f0535 686 (stgit-find-file))
50d88c67
DK
687 (t
688 (error "No patch or file on line"))))
378a003d
GH
689
690(defun stgit-find-file-other-window ()
691 "Open file at point in other window"
692 (interactive)
030f0535 693 (stgit-find-file t))
378a003d 694
d9b954c7
GH
695(defun stgit-find-file-merge ()
696 "Open file at point and merge it using `smerge-ediff'."
697 (interactive)
698 (stgit-find-file t)
699 (smerge-ediff))
700
83327d53 701(defun stgit-quit ()
a53347d9 702 "Hide the stgit buffer."
83327d53
GH
703 (interactive)
704 (bury-buffer))
705
0f076fe6 706(defun stgit-git-status ()
a53347d9 707 "Show status using `git-status'."
0f076fe6
GH
708 (interactive)
709 (unless (fboundp 'git-status)
df283a8b 710 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
711 (let ((dir default-directory))
712 (save-selected-window
713 (pop-to-buffer nil)
714 (git-status dir))))
715
58f72f16
GH
716(defun stgit-goal-column ()
717 "Return goal column for the current line"
50d88c67
DK
718 (case (get-text-property (point) 'entry-type)
719 ('patch 2)
720 ('file 4)
721 (t 0)))
58f72f16
GH
722
723(defun stgit-next-line (&optional arg)
378a003d 724 "Move cursor vertically down ARG lines"
58f72f16
GH
725 (interactive "p")
726 (next-line arg)
727 (move-to-column (stgit-goal-column)))
378a003d 728
58f72f16 729(defun stgit-previous-line (&optional arg)
378a003d 730 "Move cursor vertically up ARG lines"
58f72f16
GH
731 (interactive "p")
732 (previous-line arg)
733 (move-to-column (stgit-goal-column)))
378a003d
GH
734
735(defun stgit-next-patch (&optional arg)
98230edd 736 "Move cursor down ARG patches."
378a003d 737 (interactive "p")
98230edd
DK
738 (ewoc-goto-next stgit-ewoc (or arg 1))
739 (move-to-column goal-column))
378a003d
GH
740
741(defun stgit-previous-patch (&optional arg)
98230edd 742 "Move cursor up ARG patches."
378a003d 743 (interactive "p")
98230edd
DK
744 (ewoc-goto-prev stgit-ewoc (or arg 1))
745 (move-to-column goal-column))
378a003d 746
56d81fe5
DK
747(defvar stgit-mode-hook nil
748 "Run after `stgit-mode' is setup.")
749
750(defvar stgit-mode-map nil
751 "Keymap for StGit major mode.")
752
753(unless stgit-mode-map
d9b954c7
GH
754 (let ((diff-map (make-keymap))
755 (toggle-map (make-keymap)))
756 (suppress-keymap diff-map)
757 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
758 '(("b" . stgit-diff-base)
759 ("c" . stgit-diff-combined)
760 ("m" . stgit-find-file-merge)
761 ("o" . stgit-diff-ours)
762 ("t" . stgit-diff-theirs)))
ce3b6130
DK
763 (suppress-keymap toggle-map)
764 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
d9473917
GH
765 '(("t" . stgit-toggle-worktree)
766 ("i" . stgit-toggle-ignored)
767 ("u" . stgit-toggle-unknown)))
ce3b6130
DK
768 (setq stgit-mode-map (make-keymap))
769 (suppress-keymap stgit-mode-map)
770 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
771 `((" " . stgit-mark)
772 ("m" . stgit-mark)
773 ("\d" . stgit-unmark-up)
774 ("u" . stgit-unmark-down)
775 ("?" . stgit-help)
776 ("h" . stgit-help)
777 ("\C-p" . stgit-previous-line)
778 ("\C-n" . stgit-next-line)
779 ([up] . stgit-previous-line)
780 ([down] . stgit-next-line)
781 ("p" . stgit-previous-patch)
782 ("n" . stgit-next-patch)
783 ("\M-{" . stgit-previous-patch)
784 ("\M-}" . stgit-next-patch)
785 ("s" . stgit-git-status)
408fa7cb 786 ("g" . stgit-reload-or-repair)
ce3b6130
DK
787 ("r" . stgit-refresh)
788 ("\C-c\C-r" . stgit-rename)
789 ("e" . stgit-edit)
790 ("M" . stgit-move-patches)
791 ("S" . stgit-squash)
792 ("N" . stgit-new)
e9fdd4ea
GH
793 ("\C-c\C-c" . stgit-commit)
794 ("\C-c\C-u" . stgit-uncommit)
3959a095 795 ("U" . stgit-revert-file)
51783171 796 ("R" . stgit-resolve-file)
ce3b6130
DK
797 ("\r" . stgit-select)
798 ("o" . stgit-find-file-other-window)
799 ("i" . stgit-file-toggle-index)
800 (">" . stgit-push-next)
801 ("<" . stgit-pop-next)
802 ("P" . stgit-push-or-pop)
803 ("G" . stgit-goto)
d9b954c7 804 ("=" . stgit-diff)
ce3b6130 805 ("D" . stgit-delete)
b8463f1d 806 ([?\C-/] . stgit-undo)
ce3b6130 807 ("\C-_" . stgit-undo)
b8463f1d
GH
808 ([?\C-c ?\C-/] . stgit-redo)
809 ("\C-c\C-_" . stgit-redo)
ce3b6130 810 ("B" . stgit-branch)
380a021f 811 ("\C-c\C-b" . stgit-rebase)
ce3b6130 812 ("t" . ,toggle-map)
d9b954c7 813 ("d" . ,diff-map)
ce3b6130 814 ("q" . stgit-quit)))))
56d81fe5
DK
815
816(defun stgit-mode ()
817 "Major mode for interacting with StGit.
fdf5e327
GH
818
819Start StGit using \\[stgit].
820
821Basic commands:
822\\<stgit-mode-map>\
823\\[stgit-help] Show this help text
824\\[stgit-quit] Hide the StGit buffer
825\\[describe-bindings] Show all key bindings
826
827\\[stgit-reload-or-repair] Reload the StGit buffer
828\\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
829
830\\[stgit-undo] Undo most recent StGit operation
831\\[stgit-redo] Undo recent undo
832
833\\[stgit-git-status] Run `git-status' (if available)
834
835Movement commands:
836\\[stgit-previous-line] Move to previous line
837\\[stgit-next-line] Move to next line
838\\[stgit-previous-patch] Move to previous patch
839\\[stgit-next-patch] Move to next patch
840
841\\[stgit-mark] Mark patch
842\\[stgit-unmark-up] Unmark patch and move up
843\\[stgit-unmark-down] Unmark patch and move down
844
845Commands for patches:
846\\[stgit-select] Toggle showing changed files in patch
847\\[stgit-refresh] Refresh patch with changes in index or work tree
848\\[stgit-diff] Show the patch log and diff
849
850\\[stgit-new] Create a new, empty patch
851\\[stgit-rename] Rename patch
852\\[stgit-edit] Edit patch description
853\\[stgit-delete] Delete patch(es)
854
855\\[stgit-push-next] Push next patch onto stack
856\\[stgit-pop-next] Pop current patch from stack
857\\[stgit-push-or-pop] Push or pop patch at point
858\\[stgit-goto] Make current patch current by popping or pushing
859
860\\[stgit-squash] Squash (meld together) patches
861\\[stgit-move-patches] Move patch(es) to point
862
863\\[stgit-commit] Commit patch(es)
864\\[stgit-uncommit] Uncommit patch(es)
865
866Commands for files:
867\\[stgit-select] Open the file in this window
868\\[stgit-find-file-other-window] Open the file in another window
869\\[stgit-diff] Show the file's diff
870
871\\[stgit-file-toggle-index] Toggle change between index and work tree
872\\[stgit-revert-file] Revert changes to file
873
874Display commands:
875\\[stgit-toggle-worktree] Toggle showing index and work tree
876\\[stgit-toggle-unknown] Toggle showing unknown files
877\\[stgit-toggle-ignored] Toggle showing ignored files
878
879Commands for diffs:
880\\[stgit-diff] Show diff of patch or file
881\\[stgit-diff-base] Show diff against the merge base
882\\[stgit-diff-ours] Show diff against our branch
883\\[stgit-diff-theirs] Show diff against their branch
884
885 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
886ignore space changes.
887 With two prefix arguments (e.g., \\[universal-argument] \
888\\[universal-argument] \\[stgit-diff]), ignore all space changes.
889
890Commands for merge conflicts:
891\\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
892\\[stgit-resolve-file] Mark unmerged file as resolved
893
894Commands for branches:
895\\[stgit-branch] Switch to another branch
380a021f 896\\[stgit-rebase] Rebase the current branch
fdf5e327
GH
897
898Customization variables:
899`stgit-abbreviate-copies-and-renames'
900`stgit-default-show-worktree'
901`stgit-find-copies-harder'
902`stgit-show-worktree-mode'
903
904See also \\[customize-group] for the \"stgit\" group."
56d81fe5
DK
905 (kill-all-local-variables)
906 (buffer-disable-undo)
907 (setq mode-name "StGit"
908 major-mode 'stgit-mode
909 goal-column 2)
910 (use-local-map stgit-mode-map)
911 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 912 (set (make-local-variable 'stgit-marked-patches) nil)
6467d976 913 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
ce3b6130 914 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
915 (set (make-local-variable 'stgit-index-node) nil)
916 (set (make-local-variable 'stgit-worktree-node) nil)
224ef1ec 917 (set (make-local-variable 'parse-sexp-lookup-properties) t)
2870f8b8 918 (set-variable 'truncate-lines 't)
b894e680 919 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
920 (run-hooks 'stgit-mode-hook))
921
b894e680
DK
922(defun stgit-update-saved-file ()
923 (let* ((file (expand-file-name buffer-file-name))
924 (dir (file-name-directory file))
925 (gitdir (condition-case nil (git-get-top-dir dir)
926 (error nil)))
927 (buffer (and gitdir (stgit-find-buffer gitdir))))
928 (when buffer
929 (with-current-buffer buffer
210a2a52 930 (stgit-refresh-worktree)))))
b894e680 931
d51722b7
GH
932(defun stgit-add-mark (patchsym)
933 "Mark the patch PATCHSYM."
8036afdd 934 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 935
d51722b7
GH
936(defun stgit-remove-mark (patchsym)
937 "Unmark the patch PATCHSYM."
8036afdd 938 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 939
e6b1fdae 940(defun stgit-clear-marks ()
47271f41 941 "Unmark all patches."
e6b1fdae
DK
942 (setq stgit-marked-patches '()))
943
735cb7ec 944(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
945 (get-text-property (point) 'patch-data))
946
64ada6f5 947(defun stgit-patch-name-at-point (&optional cause-error only-patches)
d51722b7 948 "Return the patch name on the current line as a symbol.
64ada6f5
GH
949If CAUSE-ERROR is not nil, signal an error if none found.
950If ONLY-PATCHES is not nil, only allow real patches, and not
951index or work tree."
2c862b07 952 (let ((patch (stgit-patch-at-point)))
64ada6f5
GH
953 (and patch
954 only-patches
955 (memq (stgit-patch-status patch) '(work index))
956 (setq patch nil))
2c862b07
DK
957 (cond (patch
958 (stgit-patch-name patch))
959 (cause-error
960 (error "No patch on this line")))))
378a003d 961
3164eec6
DK
962(defun stgit-patched-file-at-point ()
963 (get-text-property (point) 'file-data))
56d81fe5 964
7755d7f1 965(defun stgit-patches-marked-or-at-point ()
d51722b7 966 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 967 (if stgit-marked-patches
d51722b7 968 stgit-marked-patches
2c862b07 969 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
970 (if patch
971 (list patch)
972 '()))))
973
a9089e68 974(defun stgit-goto-patch (patchsym &optional file)
d51722b7 975 "Move point to the line containing patch PATCHSYM.
a9089e68
GH
976If that patch cannot be found, do nothing.
977
978If the patch was found and FILE is not nil, instead move to that
979file's line. If FILE cannot be found, stay on the line of
980PATCHSYM."
f9b82d36
DK
981 (let ((node (ewoc-nth stgit-ewoc 0)))
982 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
983 patchsym)))
984 (setq node (ewoc-next stgit-ewoc node)))
a9089e68
GH
985 (when (and node file)
986 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
987 (file-node (ewoc-nth file-ewoc 0)))
988 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
989 (setq file-node (ewoc-next file-ewoc file-node)))
990 (when file-node
991 (ewoc-goto-node file-ewoc file-node)
992 (move-to-column (stgit-goal-column))
993 (setq node nil))))
f9b82d36
DK
994 (when node
995 (ewoc-goto-node stgit-ewoc node)
d51722b7 996 (move-to-column goal-column))))
56d81fe5 997
1c2426dc 998(defun stgit-init ()
a53347d9 999 "Run stg init."
1c2426dc
DK
1000 (interactive)
1001 (stgit-capture-output nil
b0424080 1002 (stgit-run "init"))
1f0bf00f 1003 (stgit-reload))
1c2426dc 1004
6df83d42 1005(defun stgit-mark ()
a53347d9 1006 "Mark the patch under point."
6df83d42 1007 (interactive)
8036afdd 1008 (let* ((node (ewoc-locate stgit-ewoc))
64ada6f5
GH
1009 (patch (ewoc-data node))
1010 (name (stgit-patch-name patch)))
1011 (when (eq name :work)
1012 (error "Cannot mark the work tree"))
1013 (when (eq name :index)
1014 (error "Cannot mark the index"))
8036afdd
DK
1015 (stgit-add-mark (stgit-patch-name patch))
1016 (ewoc-invalidate stgit-ewoc node))
378a003d 1017 (stgit-next-patch))
6df83d42 1018
9b151b27 1019(defun stgit-unmark-up ()
a53347d9 1020 "Remove mark from the patch on the previous line."
6df83d42 1021 (interactive)
378a003d 1022 (stgit-previous-patch)
8036afdd
DK
1023 (let* ((node (ewoc-locate stgit-ewoc))
1024 (patch (ewoc-data node)))
1025 (stgit-remove-mark (stgit-patch-name patch))
1026 (ewoc-invalidate stgit-ewoc node))
1027 (move-to-column (stgit-goal-column)))
9b151b27
GH
1028
1029(defun stgit-unmark-down ()
a53347d9 1030 "Remove mark from the patch on the current line."
9b151b27 1031 (interactive)
8036afdd
DK
1032 (let* ((node (ewoc-locate stgit-ewoc))
1033 (patch (ewoc-data node)))
1034 (stgit-remove-mark (stgit-patch-name patch))
1035 (ewoc-invalidate stgit-ewoc node))
1288eda2 1036 (stgit-next-patch))
6df83d42 1037
56d81fe5 1038(defun stgit-rename (name)
018fa1ac 1039 "Rename the patch under point to NAME."
64ada6f5
GH
1040 (interactive (list
1041 (read-string "Patch name: "
1042 (symbol-name (stgit-patch-name-at-point t t)))))
1043 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 1044 (stgit-capture-output nil
d51722b7
GH
1045 (stgit-run "rename" old-patchsym name))
1046 (let ((name-sym (intern name)))
1047 (when (memq old-patchsym stgit-expanded-patches)
378a003d 1048 (setq stgit-expanded-patches
d51722b7
GH
1049 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1050 (when (memq old-patchsym stgit-marked-patches)
378a003d 1051 (setq stgit-marked-patches
d51722b7
GH
1052 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1053 (stgit-reload)
1054 (stgit-goto-patch name-sym))))
56d81fe5 1055
408fa7cb
GH
1056(defun stgit-reload-or-repair (repair)
1057 "Update the contents of the StGit buffer (`stgit-reload').
1058
1059With a prefix argument, repair the StGit metadata if the branch
1060was modified with git commands (`stgit-repair')."
1061 (interactive "P")
1062 (if repair
1063 (stgit-repair)
1064 (stgit-reload)))
1065
26201d96 1066(defun stgit-repair ()
a53347d9 1067 "Run stg repair."
26201d96
DK
1068 (interactive)
1069 (stgit-capture-output nil
b0424080 1070 (stgit-run "repair"))
1f0bf00f 1071 (stgit-reload))
26201d96 1072
adeef6bc
GH
1073(defun stgit-available-branches ()
1074 "Returns a list of the available stg branches"
1075 (let ((output (with-output-to-string
1076 (stgit-run "branch" "--list")))
1077 (start 0)
1078 result)
1079 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1080 (setq result (cons (match-string 1 output) result))
1081 (setq start (match-end 0)))
1082 result))
1083
1084(defun stgit-branch (branch)
1085 "Switch to branch BRANCH."
1086 (interactive (list (completing-read "Switch to branch: "
1087 (stgit-available-branches))))
1088 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1089 (stgit-reload))
1090
380a021f
GH
1091(defun stgit-available-refs (&optional omit-stgit)
1092 "Returns a list of the available git refs.
1093If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1094 (let* ((output (with-output-to-string
1095 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1096 "refs/tags" "refs/heads"
1097 "refs/remotes")))
1098 (result (split-string output "\n" t)))
1099 (mapcar (lambda (s)
1100 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1101 (substring s (match-end 0))
1102 s))
1103 (if omit-stgit
1104 (delete-if (lambda (s)
1105 (string-match "^refs/heads/.*\\.stgit$" s))
1106 result)
1107 result))))
1108
1109(defun stgit-rebase (new-base)
1110 "Rebase to NEW-BASE."
1111 (interactive (list (completing-read "Rebase to: "
1112 (stgit-available-refs t))))
1113 (stgit-capture-output nil (stgit-run "rebase" new-base))
1114 (stgit-reload))
1115
41c1c59c
GH
1116(defun stgit-commit (count)
1117 "Run stg commit on COUNT commits.
e552cb5f
GH
1118Interactively, the prefix argument is used as COUNT.
1119A negative COUNT will uncommit instead."
41c1c59c 1120 (interactive "p")
e552cb5f
GH
1121 (if (< count 0)
1122 (stgit-uncommit (- count))
1123 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1124 (stgit-reload)))
1125
1126(defun stgit-uncommit (count)
1127 "Run stg uncommit on COUNT commits.
1128Interactively, the prefix argument is used as COUNT.
1129A negative COUNT will commit instead."
1130 (interactive "p")
1131 (if (< count 0)
1132 (stgit-commit (- count))
1133 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1134 (stgit-reload)))
c4aad9a7 1135
556345d3
GH
1136(defun stgit-neighbour-file ()
1137 "Return the file name of the next file after point, or the
1138previous file if point is at the last file within a patch."
1139 (let ((old-point (point))
1140 neighbour-file)
1141 (and (zerop (forward-line 1))
1142 (let ((f (stgit-patched-file-at-point)))
1143 (and f (setq neighbour-file (stgit-file-file f)))))
1144 (goto-char old-point)
1145 (unless neighbour-file
1146 (and (zerop (forward-line -1))
1147 (let ((f (stgit-patched-file-at-point)))
1148 (and f (setq neighbour-file (stgit-file-file f)))))
1149 (goto-char old-point))
1150 neighbour-file))
1151
3959a095
GH
1152(defun stgit-revert-file ()
1153 "Revert the file at point, which must be in the index or the
1154working tree."
1155 (interactive)
1156 (let* ((patched-file (or (stgit-patched-file-at-point)
1157 (error "No file on the current line")))
1158 (patch-name (stgit-patch-name-at-point))
1159 (file-status (stgit-file-status patched-file))
1160 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1161 (stgit-file-cr-to patched-file))
1162 ((eq file-status 'add)
1163 (stgit-file-file patched-file))))
1164 (co-file (cond ((eq file-status 'rename)
1165 (stgit-file-cr-from patched-file))
1166 ((not (memq file-status '(copy add)))
556345d3
GH
1167 (stgit-file-file patched-file))))
1168 (next-file (stgit-neighbour-file)))
3959a095
GH
1169
1170 (unless (memq patch-name '(:work :index))
1171 (error "No index or working tree file on this line"))
1172
d9473917
GH
1173 (when (eq file-status 'ignore)
1174 (error "Cannot revert ignored files"))
1175
1176 (when (eq file-status 'unknown)
1177 (error "Cannot revert unknown files"))
1178
3959a095
GH
1179 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1180 (when (yes-or-no-p (format "Revert %d file%s? "
1181 nfiles
1182 (if (= nfiles 1) "" "s")))
1183 (stgit-capture-output nil
1184 (when rm-file
1185 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1186 (when co-file
1187 (stgit-run-git "checkout" "HEAD" co-file)))
556345d3
GH
1188 (stgit-reload)
1189 (stgit-goto-patch patch-name next-file)))))
3959a095 1190
51783171
GH
1191(defun stgit-resolve-file ()
1192 "Resolve conflict in the file at point."
1193 (interactive)
1194 (let* ((patched-file (stgit-patched-file-at-point))
1195 (patch (stgit-patch-at-point))
1196 (patch-name (and patch (stgit-patch-name patch)))
1197 (status (and patched-file (stgit-file-status patched-file))))
1198
1199 (unless (memq patch-name '(:work :index))
1200 (error "No index or working tree file on this line"))
1201
1202 (unless (eq status 'unmerged)
1203 (error "No conflict to resolve at the current line"))
1204
1205 (stgit-capture-output nil
1206 (stgit-move-change-to-index (stgit-file-file patched-file)))
1207
1208 (stgit-reload)))
1209
0b661144
DK
1210(defun stgit-push-next (npatches)
1211 "Push the first unapplied patch.
1212With numeric prefix argument, push that many patches."
1213 (interactive "p")
d51722b7 1214 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
1215 (stgit-reload)
1216 (stgit-refresh-git-status))
56d81fe5 1217
0b661144
DK
1218(defun stgit-pop-next (npatches)
1219 "Pop the topmost applied patch.
1220With numeric prefix argument, pop that many patches."
1221 (interactive "p")
d51722b7 1222 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
1223 (stgit-reload)
1224 (stgit-refresh-git-status))
56d81fe5 1225
f9182fca
KH
1226(defun stgit-applied-at-point ()
1227 "Is the patch on the current line applied?"
1228 (save-excursion
1229 (beginning-of-line)
1230 (looking-at "[>+]")))
1231
1232(defun stgit-push-or-pop ()
a53347d9 1233 "Push or pop the patch on the current line."
f9182fca 1234 (interactive)
2c862b07 1235 (let ((patchsym (stgit-patch-name-at-point t))
f9182fca
KH
1236 (applied (stgit-applied-at-point)))
1237 (stgit-capture-output nil
d51722b7 1238 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 1239 (stgit-reload)))
f9182fca 1240
c7adf5ef 1241(defun stgit-goto ()
a53347d9 1242 "Go to the patch on the current line."
c7adf5ef 1243 (interactive)
2c862b07 1244 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 1245 (stgit-capture-output nil
d51722b7 1246 (stgit-run "goto" patchsym))
1f0bf00f 1247 (stgit-reload)))
c7adf5ef 1248
d51722b7 1249(defun stgit-id (patchsym)
50d88c67
DK
1250 "Return the git commit id for PATCHSYM.
1251If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1252 (if (keywordp patchsym)
1253 patchsym
1254 (let ((result (with-output-to-string
1255 (stgit-run-silent "id" patchsym))))
1256 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1257 (error "Cannot find commit id for %s" patchsym))
1258 (match-string 1 result))))
378a003d 1259
1aece5c0 1260(defun stgit-show-patch (unmerged-stage ignore-whitespace)
d9b954c7
GH
1261 "Show the patch on the current line.
1262
1263UNMERGED-STAGE is the argument to `git-diff' that that selects
1264which stage to diff against in the case of unmerged files."
1aece5c0
GH
1265 (let ((space-arg (when (numberp ignore-whitespace)
1266 (cond ((> ignore-whitespace 4)
1267 "--ignore-all-space")
1268 ((> ignore-whitespace 1)
1269 "--ignore-space-change"))))
1270 (patch-name (stgit-patch-name-at-point t)))
1271 (stgit-capture-output "*StGit patch*"
1272 (case (get-text-property (point) 'entry-type)
1273 ('file
1274 (let* ((patched-file (stgit-patched-file-at-point))
1275 (patch-id (let ((id (stgit-id patch-name)))
1276 (if (and (eq id :index)
1277 (eq (stgit-file-status patched-file)
1278 'unmerged))
1279 :work
1280 id)))
1281 (args (append (and space-arg (list space-arg))
1282 (and (stgit-file-cr-from patched-file)
1283 (list (stgit-find-copies-harder-diff-arg)))
1284 (cond ((eq patch-id :index)
1285 '("--cached"))
1286 ((eq patch-id :work)
1287 (list unmerged-stage))
1288 (t
1289 (list (concat patch-id "^") patch-id)))
1290 '("--")
3164eec6
DK
1291 (if (stgit-file-copy-or-rename patched-file)
1292 (list (stgit-file-cr-from patched-file)
1293 (stgit-file-cr-to patched-file))
1294 (list (stgit-file-file patched-file))))))
1aece5c0
GH
1295 (apply 'stgit-run-git "diff" args)))
1296 ('patch
1297 (let* ((patch-id (stgit-id patch-name)))
1298 (if (or (eq patch-id :index) (eq patch-id :work))
1299 (apply 'stgit-run-git "diff"
1300 (stgit-find-copies-harder-diff-arg)
1301 (append (and space-arg (list space-arg))
1302 (if (eq patch-id :index)
1303 '("--cached")
1304 (list unmerged-stage))))
1305 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1306 (and space-arg (list "-O" space-arg))
1307 (list (stgit-patch-name-at-point)))))
1308 (apply 'stgit-run args)))))
1309 (t
1310 (error "No patch or file at point")))
1311 (with-current-buffer standard-output
1312 (goto-char (point-min))
1313 (diff-mode)))))
1314
1315(defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1316 `(defun ,name (&optional ignore-whitespace)
1317 ,(format "Show the patch on the current line.
1318
1319%sWith a prefix argument, ignore whitespace. With a prefix argument
1320greater than four (e.g., \\[universal-argument] \
1321\\[universal-argument] \\[%s]), ignore all whitespace."
1322 (if unmerged-action
1323 (format "For unmerged files, %s.\n\n" unmerged-action)
1324 "")
1325 name)
1326 (interactive "p")
1327 (stgit-show-patch ,diff-arg ignore-whitespace)))
1328
1329(stgit-define-diff stgit-diff
1330 "--ours" nil)
1331(stgit-define-diff stgit-diff-ours
1332 "--ours"
1333 "diff against our branch")
1334(stgit-define-diff stgit-diff-theirs
1335 "--theirs"
1336 "diff against their branch")
1337(stgit-define-diff stgit-diff-base
1338 "--base"
1339 "diff against the merge base")
1340(stgit-define-diff stgit-diff-combined
1341 "--cc"
1342 "show a combined diff")
d9b954c7 1343
f87c2e22
GH
1344(defun stgit-move-change-to-index (file &optional force)
1345 "Copies the work tree state of FILE to index, using git add or git rm.
1346
1347If FORCE is not nil, use --force."
306b37a6
GH
1348 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1349 '("add") '("rm" "-q"))))
37cb5766 1350 (stgit-capture-output "*git output*"
f87c2e22
GH
1351 (apply 'stgit-run-git (append op (and force '("--force"))
1352 '("--") (list file))))))
37cb5766 1353
fd9fe574 1354(defun stgit-remove-change-from-index (file)
37cb5766
DK
1355 "Unstages the change in FILE from the index"
1356 (stgit-capture-output "*git output*"
1357 (stgit-run-git "reset" "-q" "--" file)))
1358
1359(defun stgit-file-toggle-index ()
a9089e68
GH
1360 "Move modified file in or out of the index.
1361
1362Leaves the point where it is, but moves the mark to where the
1363file ended up. You can then jump to the file with \
1364\\[exchange-point-and-mark]."
37cb5766 1365 (interactive)
612f999a
GH
1366 (let* ((patched-file (or (stgit-patched-file-at-point)
1367 (error "No file on the current line")))
1368 (patched-status (stgit-file-status patched-file)))
1369 (when (eq patched-status 'unmerged)
51783171 1370 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
a9089e68
GH
1371 (let* ((patch (stgit-patch-at-point))
1372 (patch-name (stgit-patch-name patch))
612f999a
GH
1373 (mark-file (if (eq patched-status 'rename)
1374 (stgit-file-cr-to patched-file)
1375 (stgit-file-file patched-file)))
1376 (point-file (if (eq patched-status 'rename)
1377 (stgit-file-cr-from patched-file)
1378 (stgit-neighbour-file))))
a9089e68 1379
37cb5766 1380 (cond ((eq patch-name :work)
f87c2e22
GH
1381 (stgit-move-change-to-index (stgit-file-file patched-file)
1382 (eq patched-status 'ignore)))
37cb5766 1383 ((eq patch-name :index)
fd9fe574 1384 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766 1385 (t
612f999a 1386 (error "Can only move files between working tree and index")))
a9089e68
GH
1387 (stgit-refresh-worktree)
1388 (stgit-refresh-index)
612f999a 1389 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
a9089e68 1390 (push-mark nil t t)
612f999a 1391 (stgit-goto-patch patch-name point-file))))
37cb5766 1392
0bca35c8 1393(defun stgit-edit ()
a53347d9 1394 "Edit the patch on the current line."
0bca35c8 1395 (interactive)
64ada6f5 1396 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 1397 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
1398 (dir default-directory))
1399 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 1400 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
1401 (setq default-directory dir)
1402 (let ((standard-output edit-buf))
d51722b7 1403 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
1404
1405(defun stgit-confirm-edit ()
1406 (interactive)
1407 (let ((file (make-temp-file "stgit-edit-")))
1408 (write-region (point-min) (point-max) file)
1409 (stgit-capture-output nil
d51722b7 1410 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 1411 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1412 (stgit-reload))))
0bca35c8 1413
aa04f831
GH
1414(defun stgit-new (add-sign)
1415 "Create a new patch.
1416With a prefix argument, include a \"Signed-off-by:\" line at the
1417end of the patch."
1418 (interactive "P")
c5d45b92
GH
1419 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1420 (dir default-directory))
1421 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831
GH
1422 (setq default-directory dir)
1423 (when add-sign
1424 (save-excursion
1425 (let ((standard-output (current-buffer)))
1426 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
1427
1428(defun stgit-confirm-new ()
1429 (interactive)
27b0f9e4 1430 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
1431 (write-region (point-min) (point-max) file)
1432 (stgit-capture-output nil
27b0f9e4 1433 (stgit-run "new" "-f" file))
64c097a0 1434 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1435 (stgit-reload))))
64c097a0
DK
1436
1437(defun stgit-create-patch-name (description)
1438 "Create a patch name from a long description"
1439 (let ((patch ""))
1440 (while (> (length description) 0)
1441 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
1442 (setq patch (downcase (concat patch
1443 (match-string 0 description))))
64c097a0
DK
1444 (setq description (substring description (match-end 0))))
1445 ((string-match "\\` +" description)
1446 (setq patch (concat patch "-"))
1447 (setq description (substring description (match-end 0))))
1448 ((string-match "\\`[^a-zA-Z_-]+" description)
1449 (setq description (substring description (match-end 0))))))
1450 (cond ((= (length patch) 0)
1451 "patch")
1452 ((> (length patch) 20)
1453 (substring patch 0 20))
1454 (t patch))))
0bca35c8 1455
9008e45b 1456(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 1457 "Delete the patches in PATCHSYMS.
9008e45b
GH
1458Interactively, delete the marked patches, or the patch at point.
1459
1460With a prefix argument, or SPILL-P, spill the patch contents to
1461the work tree and index."
1462 (interactive (list (stgit-patches-marked-or-at-point)
1463 current-prefix-arg))
e7231e4f
GH
1464 (unless patchsyms
1465 (error "No patches to delete"))
64ada6f5
GH
1466 (when (memq :index patchsyms)
1467 (error "Cannot delete the index"))
1468 (when (memq :work patchsyms)
1469 (error "Cannot delete the work tree"))
1470
d51722b7 1471 (let ((npatches (length patchsyms)))
9008e45b 1472 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 1473 npatches
9008e45b
GH
1474 (if (= 1 npatches) "" "es")
1475 (if spill-p
1476 " (spilling contents to index)"
1477 "")))
1478 (let ((args (if spill-p
1479 (cons "--spill" patchsyms)
1480 patchsyms)))
1481 (stgit-capture-output nil
1482 (apply 'stgit-run "delete" args))
1483 (stgit-reload)))))
d51722b7 1484
7cc45294
GH
1485(defun stgit-move-patches-target ()
1486 "Return the patchsym indicating a target patch for
1487`stgit-move-patches'.
1488
1489This is either the patch at point, or one of :top and :bottom, if
1490the point is after or before the applied patches."
1491
980d8cfb 1492 (let ((patchsym (stgit-patch-name-at-point nil t)))
7cc45294
GH
1493 (cond (patchsym patchsym)
1494 ((save-excursion (re-search-backward "^>" nil t)) :top)
1495 (t :bottom))))
1496
95369f6c
GH
1497(defun stgit-sort-patches (patchsyms)
1498 "Returns the list of patches in PATCHSYMS sorted according to
1499their position in the patch series, bottommost first.
1500
2d7bcbd9 1501PATCHSYMS must not contain duplicate entries."
95369f6c
GH
1502 (let (sorted-patchsyms
1503 (series (with-output-to-string
1504 (with-current-buffer standard-output
1505 (stgit-run-silent "series" "--noprefix"))))
1506 start)
1507 (while (string-match "^\\(.+\\)" series start)
1508 (let ((patchsym (intern (match-string 1 series))))
1509 (when (memq patchsym patchsyms)
1510 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1511 (setq start (match-end 0)))
1512 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1513
1514 (unless (= (length patchsyms) (length sorted-patchsyms))
1515 (error "Internal error"))
1516
1517 sorted-patchsyms))
1518
7cc45294
GH
1519(defun stgit-move-patches (patchsyms target-patch)
1520 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1521If TARGET-PATCH is :bottom or :top, move the patches to the
1522bottom or top of the stack, respectively.
1523
1524Interactively, move the marked patches to where the point is."
1525 (interactive (list stgit-marked-patches
1526 (stgit-move-patches-target)))
1527 (unless patchsyms
1528 (error "Need at least one patch to move"))
1529
1530 (unless target-patch
1531 (error "Point not at a patch"))
1532
1533 (if (eq target-patch :top)
1534 (stgit-capture-output nil
1535 (apply 'stgit-run "float" patchsyms))
1536
1537 ;; need to have patchsyms sorted by position in the stack
95369f6c 1538 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1539 (while sorted-patchsyms
1540 (setq sorted-patchsyms
1541 (and (stgit-capture-output nil
1542 (if (eq target-patch :bottom)
1543 (stgit-run "sink" "--" (car sorted-patchsyms))
1544 (stgit-run "sink" "--to" target-patch "--"
1545 (car sorted-patchsyms))))
1546 (cdr sorted-patchsyms))))))
1547 (stgit-reload))
1548
594aa463
KH
1549(defun stgit-squash (patchsyms)
1550 "Squash the patches in PATCHSYMS.
693d179b
GH
1551Interactively, squash the marked patches.
1552
1553Unless there are any conflicts, the patches will be merged into
1554one patch, which will occupy the same spot in the series as the
1555deepest patch had before the squash."
d51722b7
GH
1556 (interactive (list stgit-marked-patches))
1557 (when (< (length patchsyms) 2)
594aa463 1558 (error "Need at least two patches to squash"))
32d7545d
GH
1559 (let ((stgit-buffer (current-buffer))
1560 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1561 (dir default-directory)
1562 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1563 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1564 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1565 (setq default-directory dir)
32d7545d
GH
1566 (let ((result (let ((standard-output edit-buf))
1567 (apply 'stgit-run-silent "squash"
1568 "--save-template=-" sorted-patchsyms))))
1569
1570 ;; stg squash may have reordered the patches or caused conflicts
1571 (with-current-buffer stgit-buffer
1572 (stgit-reload))
1573
1574 (unless (eq 0 result)
1575 (fundamental-mode)
1576 (rename-buffer "*StGit error*")
1577 (resize-temp-buffer-window)
1578 (switch-to-buffer-other-window stgit-buffer)
1579 (error "stg squash failed")))))
ea0def18 1580
594aa463 1581(defun stgit-confirm-squash ()
ea0def18
DK
1582 (interactive)
1583 (let ((file (make-temp-file "stgit-edit-")))
1584 (write-region (point-min) (point-max) file)
1585 (stgit-capture-output nil
594aa463 1586 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1587 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1588 (stgit-clear-marks)
1589 ;; Go to first marked patch and stay there
1590 (goto-char (point-min))
1591 (re-search-forward (concat "^[>+-]\\*") nil t)
1592 (move-to-column goal-column)
1593 (let ((pos (point)))
1f0bf00f 1594 (stgit-reload)
e6b1fdae 1595 (goto-char pos)))))
ea0def18 1596
0663524d
KH
1597(defun stgit-help ()
1598 "Display help for the StGit mode."
1599 (interactive)
1600 (describe-function 'stgit-mode))
3a59f3db 1601
83e51dbf
DK
1602(defun stgit-undo (&optional arg)
1603 "Run stg undo.
b8463f1d
GH
1604With prefix argument, run it with the --hard flag.
1605
1606See also `stgit-redo'."
83e51dbf
DK
1607 (interactive "P")
1608 (stgit-capture-output nil
1609 (if arg
1610 (stgit-run "undo" "--hard")
1611 (stgit-run "undo")))
1f0bf00f 1612 (stgit-reload))
83e51dbf 1613
b8463f1d
GH
1614(defun stgit-redo (&optional arg)
1615 "Run stg redo.
1616With prefix argument, run it with the --hard flag.
1617
1618See also `stgit-undo'."
1619 (interactive "P")
1620 (stgit-capture-output nil
1621 (if arg
1622 (stgit-run "redo" "--hard")
1623 (stgit-run "redo")))
1624 (stgit-reload))
1625
4d73c4d8
DK
1626(defun stgit-refresh (&optional arg)
1627 "Run stg refresh.
36a4eacd
GH
1628If the index contains any changes, only refresh from index.
1629
a53347d9 1630With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8
DK
1631 (interactive "P")
1632 (let ((patchargs (if arg
b0424080
GH
1633 (let ((patches (stgit-patches-marked-or-at-point)))
1634 (cond ((null patches)
df283a8b 1635 (error "No patch to update"))
b0424080 1636 ((> (length patches) 1)
df283a8b 1637 (error "Too many patches selected"))
b0424080
GH
1638 (t
1639 (cons "-p" patches))))
1640 nil)))
36a4eacd
GH
1641 (unless (stgit-index-empty-p)
1642 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 1643 (stgit-capture-output nil
074a4fb0
GH
1644 (apply 'stgit-run "refresh" patchargs))
1645 (stgit-refresh-git-status))
4d73c4d8
DK
1646 (stgit-reload))
1647
ce3b6130 1648(defvar stgit-show-worktree nil
8f702de4 1649 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 1650
8f702de4 1651See also `stgit-show-worktree-mode'.")
ce3b6130 1652
d9473917
GH
1653(defvar stgit-show-ignored nil
1654 "If nil, inhibit showing files ignored by git.")
1655
1656(defvar stgit-show-unknown nil
1657 "If nil, inhibit showing files not registered with git.")
1658
ce3b6130
DK
1659(defun stgit-toggle-worktree (&optional arg)
1660 "Toggle the visibility of the work tree.
2d7bcbd9 1661With ARG, show the work tree if ARG is positive.
ce3b6130 1662
8f702de4
GH
1663Its initial setting is controlled by `stgit-default-show-worktree'.
1664
1665`stgit-show-worktree-mode' controls where on screen the index and
1666work tree will show up."
ce3b6130
DK
1667 (interactive)
1668 (setq stgit-show-worktree
1669 (if (numberp arg)
1670 (> arg 0)
1671 (not stgit-show-worktree)))
1672 (stgit-reload))
1673
d9473917
GH
1674(defun stgit-toggle-ignored (&optional arg)
1675 "Toggle the visibility of files ignored by git in the work
1676tree. With ARG, show these files if ARG is positive.
1677
1678Use \\[stgit-toggle-worktree] to show the work tree."
1679 (interactive)
1680 (setq stgit-show-ignored
1681 (if (numberp arg)
1682 (> arg 0)
1683 (not stgit-show-ignored)))
1684 (stgit-reload))
1685
1686(defun stgit-toggle-unknown (&optional arg)
1687 "Toggle the visibility of files not registered with git in the
1688work tree. With ARG, show these files if ARG is positive.
1689
1690Use \\[stgit-toggle-worktree] to show the work tree."
1691 (interactive)
1692 (setq stgit-show-unknown
1693 (if (numberp arg)
1694 (> arg 0)
1695 (not stgit-show-unknown)))
1696 (stgit-reload))
1697
3a59f3db 1698(provide 'stgit)