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