stgit.el: Rearrange customizations a bit
[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)
a4a01630 17(require 'comint)
98230edd 18(require 'ewoc)
5038381d 19(require 'easymenu)
0b9ea6b8 20(require 'format-spec)
0f076fe6 21
4ba91e80
DK
22(defun stgit-set-default (symbol value)
23 "Set default value of SYMBOL to VALUE using `set-default' and
24reload all StGit buffers."
25 (set-default symbol value)
26 (dolist (buf (buffer-list))
27 (with-current-buffer buf
28 (when (eq major-mode 'stgit-mode)
29 (stgit-reload)))))
30
31(defgroup stgit nil
32 "A user interface for the StGit patch maintenance tool."
33 :group 'tools
34 :link '(function-link stgit)
35 :link '(url-link "http://www.procode.org/stgit/"))
36
4ba91e80
DK
37(defcustom stgit-default-show-worktree t
38 "Set to non-nil to by default show the working tree in a new stgit buffer.
39
bc11fb08
GH
40Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle this \
41setting in an already-started StGit buffer."
4ba91e80
DK
42 :type 'boolean
43 :group 'stgit
44 :link '(variable-link stgit-show-worktree))
45
bc11fb08
GH
46(defcustom stgit-default-show-unknown nil
47 "Set to non-nil to by default show unknown files a new stgit buffer.
48
49Use \\<stgit-mode-map>\\[stgit-toggle-unknown] to toggle this \
50setting in an already-started StGit buffer."
51 :type 'boolean
52 :group 'stgit
53 :link '(variable-link stgit-show-unknown))
54
55(defcustom stgit-default-show-ignored nil
56 "Set to non-nil to by default show ignored files a new stgit buffer.
57
58Use \\<stgit-mode-map>\\[stgit-toggle-ignored] to toggle this \
59setting in an already-started StGit buffer."
60 :type 'boolean
61 :group 'stgit
62 :link '(variable-link stgit-show-ignored))
63
fefc11a4
GH
64(defcustom stgit-default-show-patch-names t
65 "If non-nil, default to showing patch names in a new stgit buffer.
66
67Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] \
68to toggle the this setting in an already-started StGit buffer."
69 :type 'boolean
70 :group 'stgit
71 :link '(variable-link stgit-show-patch-names))
72
73(defcustom stgit-default-show-committed nil
74 "Set to nil to inhibit showing of historical git commits by default.
75
76Use \\<stgit-mode-map>\\[stgit-toggle-committed] \
77to toggle this setting and to control how many commits are
78shown."
79 :type 'boolean
80 :group 'stgit
81 :link '(variable-link stgit-default-committed-count)
82 :link '(variable-link stgit-show-committed))
83
84(defcustom stgit-default-committed-count 5
85 "The number of historical commits to show when `stgit-show-committed'
86is enabled."
87 :type 'number
88 :group 'stgit
89 :link '(variable-link stgit-default-show-committed)
90 :link '(variable-link stgit-committed-count))
91
137b315b
GH
92(defcustom stgit-default-show-svn t
93 "Set to non-nil to by default show subversion information in a
94new stgit buffer.
95
96Use \\<stgit-mode-map>\\[stgit-toggle-svn] to toggle this \
97setting in an already-started StGit buffer."
98 :type 'boolean
99 :group 'stgit
100 :link '(variable-link stgit-show-worktree))
101
fefc11a4
GH
102(defcustom stgit-abbreviate-copies-and-renames t
103 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
104instead of \"dir/old/file -> dir/new/file\"."
105 :type 'boolean
106 :group 'stgit
107 :set 'stgit-set-default)
108
4ba91e80
DK
109(defcustom stgit-find-copies-harder nil
110 "Try harder to find copied files when listing patches.
111
112When not nil, runs git diff-tree with the --find-copies-harder
113flag, which reduces performance."
114 :type 'boolean
115 :group 'stgit
116 :set 'stgit-set-default)
117
118(defcustom stgit-show-worktree-mode 'center
119 "This variable controls where the \"Index\" and \"Work tree\"
120will be shown on in the buffer.
121
122It can be set to 'top (above all patches), 'center (show between
123applied and unapplied patches), and 'bottom (below all patches)."
124 :type '(radio (const :tag "above all patches (top)" top)
125 (const :tag "between applied and unapplied patches (center)"
126 center)
127 (const :tag "below all patches (bottom)" bottom))
128 :group 'stgit
129 :link '(variable-link stgit-show-worktree)
130 :set 'stgit-set-default)
131
0b9ea6b8
DK
132(defcustom stgit-patch-line-format "%s%m%-30n %e%d"
133 "The format string used to format patch lines.
134The format string is passed to `format-spec' and the following
135format characters are recognized:
136
137 %s - A '+', '-', '>' or space, depending on whether the patch is
138 applied, unapplied, top, or something else.
139
140 %m - An asterisk if the patch is marked, and a space otherwise.
141
142 %n - The patch name.
143
144 %e - The string \"(empty) \" if the patch is empty.
145
a0045b87
DK
146 %d - The short patch description.
147
148 %D - The short patch description, or the patch name.
149
150When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
151variable is used instead."
152 :type 'string
153 :group 'stgit
154 :set 'stgit-set-default)
155
156(defcustom stgit-noname-patch-line-format "%s%m%e%D"
157 "The alternate format string used to format patch lines.
158It has the same semantics as `stgit-patch-line-format', and the
f3dbdbc0
GH
159display can be toggled between the two formats using \
160\\<stgit-mode-map>\\[stgit-toggle-patch-names].
a0045b87
DK
161
162The alternate form is used when the patch name is hidden."
0b9ea6b8
DK
163 :type 'string
164 :group 'stgit
165 :set 'stgit-set-default)
166
43ee50b6
DK
167(defcustom stgit-file-line-format " %-11s %-2m %n %c"
168 "The format string used to format file lines.
169The format string is passed to `format-spec' and the following
170format characters are recognized:
171
172 %s - A string describing the status of the file.
173
174 %m - Mode change information
175
176 %n - The file name.
177
178 %c - A description of file changes."
179 :type 'string
180 :group 'stgit
181 :set 'stgit-set-default)
182
fefc11a4
GH
183(defgroup stgit-faces nil
184 "Faces for `stgit-mode'."
185 :group 'stgit)
186
4ba91e80
DK
187(defface stgit-branch-name-face
188 '((t :inherit bold))
189 "The face used for the StGit branch name"
fefc11a4 190 :group 'stgit-faces)
4ba91e80
DK
191
192(defface stgit-top-patch-face
193 '((((background dark)) (:weight bold :foreground "yellow"))
194 (((background light)) (:weight bold :foreground "purple"))
195 (t (:weight bold)))
196 "The face used for the top patch names"
fefc11a4 197 :group 'stgit-faces)
4ba91e80
DK
198
199(defface stgit-applied-patch-face
200 '((((background dark)) (:foreground "light yellow"))
201 (((background light)) (:foreground "purple"))
202 (t ()))
203 "The face used for applied patch names"
fefc11a4 204 :group 'stgit-faces)
4ba91e80
DK
205
206(defface stgit-unapplied-patch-face
207 '((((background dark)) (:foreground "gray80"))
208 (((background light)) (:foreground "orchid"))
209 (t ()))
210 "The face used for unapplied patch names"
fefc11a4 211 :group 'stgit-faces)
4ba91e80 212
bf7e391c
GH
213(defface stgit-committed-patch-face
214 '((((background dark)) (:foreground "gray50"))
215 (((background light)) (:foreground "gray50"))
216 (t ()))
217 "The face used for already committed patch names"
fefc11a4 218 :group 'stgit-faces)
bf7e391c 219
4ba91e80
DK
220(defface stgit-description-face
221 '((((background dark)) (:foreground "tan"))
222 (((background light)) (:foreground "dark red")))
223 "The face used for StGit descriptions"
fefc11a4 224 :group 'stgit-faces)
4ba91e80
DK
225
226(defface stgit-index-work-tree-title-face
227 '((((supports :slant italic)) :slant italic)
228 (t :inherit bold))
229 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
fefc11a4 230 :group 'stgit-faces)
4ba91e80
DK
231
232(defface stgit-unmerged-file-face
233 '((((class color) (background light)) (:foreground "red" :bold t))
234 (((class color) (background dark)) (:foreground "red" :bold t)))
235 "StGit mode face used for unmerged file status"
fefc11a4 236 :group 'stgit-faces)
4ba91e80
DK
237
238(defface stgit-unknown-file-face
239 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
240 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
241 "StGit mode face used for unknown file status"
fefc11a4 242 :group 'stgit-faces)
4ba91e80
DK
243
244(defface stgit-ignored-file-face
245 '((((class color) (background light)) (:foreground "grey60"))
246 (((class color) (background dark)) (:foreground "grey40")))
247 "StGit mode face used for ignored files")
248
249(defface stgit-file-permission-face
250 '((((class color) (background light)) (:foreground "green" :bold t))
251 (((class color) (background dark)) (:foreground "green" :bold t)))
252 "StGit mode face used for permission changes."
fefc11a4 253 :group 'stgit-faces)
4ba91e80
DK
254
255(defface stgit-modified-file-face
256 '((((class color) (background light)) (:foreground "purple"))
257 (((class color) (background dark)) (:foreground "salmon")))
258 "StGit mode face used for modified file status"
fefc11a4 259 :group 'stgit-faces)
4ba91e80 260
56d81fe5 261(defun stgit (dir)
fdf5e327
GH
262 "Manage StGit patches for the tree in DIR.
263
264See `stgit-mode' for commands available."
56d81fe5 265 (interactive "DDirectory: \n")
52144ce5 266 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 267 (stgit-reload))
56d81fe5 268
9d04c657
GH
269(defun stgit-assert-mode ()
270 "Signal an error if not in an StGit buffer."
271 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
272
074a4fb0
GH
273(unless (fboundp 'git-get-top-dir)
274 (defun git-get-top-dir (dir)
275 "Retrieve the top-level directory of a git tree."
276 (let ((cdup (with-output-to-string
277 (with-current-buffer standard-output
278 (cd dir)
279 (unless (eq 0 (call-process "git" nil t nil
280 "rev-parse" "--show-cdup"))
df283a8b 281 (error "Cannot find top-level git tree for %s" dir))))))
074a4fb0
GH
282 (expand-file-name (concat (file-name-as-directory dir)
283 (car (split-string cdup "\n")))))))
284
285(defun stgit-refresh-git-status (&optional dir)
286 "If it exists, refresh the `git-status' buffer belonging to
287directory DIR or `default-directory'"
288 (when (and (fboundp 'git-find-status-buffer)
289 (fboundp 'git-refresh-status))
290 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
291 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
292 (when git-status-buffer
293 (with-current-buffer git-status-buffer
294 (git-refresh-status))))))
52144ce5 295
b894e680
DK
296(defun stgit-find-buffer (dir)
297 "Return the buffer displaying StGit patches for DIR, or nil if none."
56d81fe5
DK
298 (setq dir (file-name-as-directory dir))
299 (let ((buffers (buffer-list)))
300 (while (and buffers
301 (not (with-current-buffer (car buffers)
302 (and (eq major-mode 'stgit-mode)
303 (string= default-directory dir)))))
304 (setq buffers (cdr buffers)))
b894e680
DK
305 (and buffers (car buffers))))
306
307(defun switch-to-stgit-buffer (dir)
308 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
309 (setq dir (file-name-as-directory dir))
310 (let ((buffer (stgit-find-buffer dir)))
311 (switch-to-buffer (or buffer
312 (create-stgit-buffer dir)))))
313
413f9909
GH
314(defstruct (stgit-patch
315 (:conc-name stgit-patch->))
3164eec6 316 status name desc empty files-ewoc)
56d81fe5 317
0b9ea6b8 318(defun stgit-patch-display-name (patch)
413f9909 319 (let ((name (stgit-patch->name patch)))
0b9ea6b8
DK
320 (case name
321 (:index "Index")
322 (:work "Work Tree")
323 (t (symbol-name name)))))
324
da30db2a
GH
325(defun stgit-insert-without-trailing-whitespace (text)
326 "Insert TEXT in buffer using `insert', without trailing whitespace.
327A newline is appended."
328 (unless (string-match "\\(.*?\\) *$" text)
329 (error))
330 (insert (match-string 1 text) ?\n))
331
6fdc3546
GH
332(defun stgit-line-format ()
333 "Return the current line format; one of
334`stgit-patch-line-format' and `stgit-noname-patch-line-format'"
335 (if stgit-show-patch-names
336 stgit-patch-line-format
337 stgit-noname-patch-line-format))
338
98230edd 339(defun stgit-patch-pp (patch)
413f9909 340 (let* ((status (stgit-patch->status patch))
9153ce3a 341 (start (point))
413f9909 342 (name (stgit-patch->name patch))
0b9ea6b8 343 (face (cdr (assq status stgit-patch-status-face-alist)))
6fdc3546 344 (fmt (stgit-line-format))
0b9ea6b8
DK
345 (spec (format-spec-make
346 ?s (case status
347 ('applied "+")
348 ('top ">")
349 ('unapplied "-")
350 (t " "))
351 ?m (if (memq name stgit-marked-patches)
352 "*" " ")
353 ?n (propertize (stgit-patch-display-name patch)
354 'face face
355 'syntax-table (string-to-syntax "w"))
413f9909
GH
356 ?e (if (stgit-patch->empty patch) "(empty) " "")
357 ?d (propertize (or (stgit-patch->desc patch) "")
a0045b87 358 'face 'stgit-description-face)
48047215
GH
359 ?D (propertize (let ((desc (stgit-patch->desc patch)))
360 (if (zerop (length desc))
361 (stgit-patch-display-name patch)
362 desc))
da30db2a
GH
363 'face face)))
364 (text (format-spec fmt spec)))
0b9ea6b8 365
da30db2a 366 (stgit-insert-without-trailing-whitespace text)
f9b82d36 367 (put-text-property start (point) 'entry-type 'patch)
98230edd 368 (when (memq name stgit-expanded-patches)
0de6881a 369 (stgit-insert-patch-files patch))
98230edd
DK
370 (put-text-property start (point) 'patch-data patch)))
371
56d81fe5
DK
372(defun create-stgit-buffer (dir)
373 "Create a buffer for showing StGit patches.
374Argument DIR is the repository path."
375 (let ((buf (create-file-buffer (concat dir "*stgit*")))
376 (inhibit-read-only t))
377 (with-current-buffer buf
378 (setq default-directory dir)
379 (stgit-mode)
98230edd 380 (set (make-local-variable 'stgit-ewoc)
4f7ff561 381 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
56d81fe5
DK
382 (setq buffer-read-only t))
383 buf))
384
072e96c5
GH
385(def-edebug-spec stgit-capture-output
386 (form body))
56d81fe5 387(defmacro stgit-capture-output (name &rest body)
e558a4ab
GH
388 "Capture StGit output and, if there was any output, show it in a window
389at the end.
390Returns nil if there was no output."
94baef5a
DK
391 (declare (debug ([&or stringp null] body))
392 (indent 1))
34afb86c
DK
393 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
394 (stgit-dir default-directory)
395 (inhibit-read-only t))
56d81fe5 396 (with-current-buffer output-buf
277b52af 397 (buffer-disable-undo)
34afb86c
DK
398 (erase-buffer)
399 (setq default-directory stgit-dir)
400 (setq buffer-read-only t))
56d81fe5
DK
401 (let ((standard-output output-buf))
402 ,@body)
34afb86c
DK
403 (with-current-buffer output-buf
404 (set-buffer-modified-p nil)
405 (setq buffer-read-only t)
406 (if (< (point-min) (point-max))
407 (display-buffer output-buf t)))))
56d81fe5 408
d51722b7
GH
409(defun stgit-make-run-args (args)
410 "Return a copy of ARGS with its elements converted to strings."
411 (mapcar (lambda (x)
412 ;; don't use (format "%s" ...) to limit type errors
413 (cond ((stringp x) x)
414 ((integerp x) (number-to-string x))
415 ((symbolp x) (symbol-name x))
416 (t
417 (error "Bad element in stgit-make-run-args args: %S" x))))
418 args))
419
1b471044
GH
420(defvar stgit-inhibit-messages nil
421 "Set to non-nil to inhibit messages when running `stg' commands.
422See also `stgit-message'.")
423(defun stgit-message (format-spec &rest args)
424 "Call `message' on the arguments unless `stgit-inhibit-messages' is non-nil."
425 (unless stgit-inhibit-messages
426 (apply 'message format-spec args)))
56d81fe5 427
9aecd505 428(defun stgit-run (&rest args)
d51722b7 429 (setq args (stgit-make-run-args args))
9aecd505 430 (let ((msgcmd (mapconcat #'identity args " ")))
1b471044
GH
431 (stgit-message "Running stg %s..." msgcmd)
432 (prog1
433 (apply 'call-process "stg" nil standard-output nil args)
434 (stgit-message "Running stg %s...done" msgcmd))))
435
436(defun stgit-run-silent (&rest args)
437 (let ((stgit-inhibit-messages t))
438 (apply 'stgit-run args)))
9aecd505 439
378a003d 440(defun stgit-run-git (&rest args)
d51722b7 441 (setq args (stgit-make-run-args args))
378a003d 442 (let ((msgcmd (mapconcat #'identity args " ")))
1b471044
GH
443 (stgit-message "Running git %s..." msgcmd)
444 (prog1
445 (apply 'call-process "git" nil standard-output nil args)
446 (stgit-message "Running git %s...done" msgcmd))))
378a003d 447
1f60181a 448(defun stgit-run-git-silent (&rest args)
1b471044
GH
449 (let ((stgit-inhibit-messages t))
450 (apply 'stgit-run-git args)))
1f60181a 451
b894e680
DK
452(defun stgit-index-empty-p ()
453 "Returns non-nil if the index contains no changes from HEAD."
454 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
455
1629f59f
GH
456(defun stgit-work-tree-empty-p ()
457 "Returns non-nil if the work tree contains no changes from index."
458 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
459
e44674e3
GH
460(defvar stgit-did-advise nil
461 "Set to non-nil if appropriate (non-stgit) git functions have
462been advised to update the stgit status when necessary.")
463
3e528fb0
GH
464(defconst stgit-allowed-branch-name-re
465 ;; Disallow control characters, space, del, and "/:@^{}~" in
466 ;; "/"-separated parts; parts may not start with a period (.)
467 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
468\\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
469 "Regular expression that (new) branch names must match.")
470
210a2a52
DK
471(defun stgit-refresh-index ()
472 (when stgit-index-node
473 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
474
475(defun stgit-refresh-worktree ()
476 (when stgit-worktree-node
477 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
478
8f702de4
GH
479(defun stgit-run-series-insert-index (ewoc)
480 (setq index-node (cons ewoc (ewoc-enter-last ewoc
481 (make-stgit-patch
482 :status 'index
483 :name :index
484 :desc nil
485 :empty nil)))
486 worktree-node (cons ewoc (ewoc-enter-last ewoc
487 (make-stgit-patch
488 :status 'work
489 :name :work
490 :desc nil
491 :empty nil)))))
492
137b315b
GH
493(defun stgit-svn-find-rev (sha1 hash)
494 "Return the subversion revision corresponding to SHA1 as
495reported by git svn.
496
497Cached data is stored in HASH, which must have been created
498using (make-hash-table :test 'equal)."
499 (let ((result (gethash sha1 hash t)))
500 (when (eq result t)
501 (let ((svn-rev (with-output-to-string
502 (stgit-run-git-silent "svn" "find-rev"
503 "--" sha1))))
504 (setq result (when (string-match "\\`[0-9]+" svn-rev)
505 (string-to-number (match-string 0 svn-rev))))
506 (puthash sha1 result hash)))
507 result))
508
98230edd 509(defun stgit-run-series (ewoc)
8f702de4
GH
510 (setq stgit-index-node nil
511 stgit-worktree-node nil)
bf7e391c
GH
512 (let (all-patchsyms)
513 (when stgit-show-committed
137b315b
GH
514 (let* ((show-svn stgit-show-svn)
515 (svn-hash stgit-svn-find-rev-hash)
2c2b8ec2
GH
516 (nentries (format "-%s" stgit-committed-count))
517 (base (stgit-id "{base}")))
bf7e391c
GH
518 (with-temp-buffer
519 (let* ((standard-output (current-buffer))
520 (fmt (stgit-line-format))
521 (commit-abbrev (when (string-match "%-\\([0-9]+\\)n" fmt)
137b315b
GH
522 (string-to-number (match-string 1 fmt))))
523 (exit-status (stgit-run-git-silent "--no-pager" "log"
524 "--reverse"
525 "--pretty=oneline"
2c2b8ec2
GH
526 nentries
527 base)))
bf7e391c
GH
528 (goto-char (point-min))
529 (if (not (zerop exit-status))
530 (message "Failed to run git log")
531 (while (not (eobp))
532 (unless (looking-at
533 "\\([0-9a-f]+\\)\\(\\.\\.\\.\\)? \\(.*\\)")
534 (error "Syntax error in output from git log"))
535 (let* ((state 'committed)
137b315b 536 (name (match-string 1))
bf7e391c
GH
537 (desc (match-string 3))
538 (empty nil))
137b315b
GH
539
540 (when show-svn
541 (let ((svn-rev (stgit-svn-find-rev name svn-hash)))
542 (when svn-rev
543 (setq desc (format "(r%s) %s" svn-rev desc)))))
544
545 (and commit-abbrev
546 (< commit-abbrev (length name))
547 (setq name (substring name 0 commit-abbrev)))
548
549 (setq name (intern name))
550
bf7e391c
GH
551 (setq all-patchsyms (cons name all-patchsyms))
552 (ewoc-enter-last ewoc
553 (make-stgit-patch
554 :status state
555 :name name
556 :desc desc
557 :empty empty)))
558 (forward-line 1)))))))
559 (let ((inserted-index (not stgit-show-worktree))
560 index-node
561 worktree-node)
562 (with-temp-buffer
563 (let* ((standard-output (current-buffer))
564 (exit-status (stgit-run-silent "series"
565 "--description" "--empty")))
566 (goto-char (point-min))
567 (if (not (zerop exit-status))
568 (cond ((looking-at "stg series: \\(.*\\)")
569 (setq inserted-index t)
570 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
571 (substitute-command-keys
572 "-- not initialized; run \\[stgit-init]")))
573 ((looking-at ".*")
574 (error "Error running stg: %s"
575 (match-string 0))))
576 (while (not (eobp))
577 (unless (looking-at
578 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
579 (error "Syntax error in output from stg series"))
580 (let* ((state-str (match-string 2))
581 (state (cond ((string= state-str ">") 'top)
582 ((string= state-str "+") 'applied)
583 ((string= state-str "-") 'unapplied)))
584 (name (intern (match-string 4)))
585 (desc (match-string 5))
586 (empty (string= (match-string 1) "0")))
587 (unless inserted-index
588 (when (or (eq stgit-show-worktree-mode 'top)
589 (and (eq stgit-show-worktree-mode 'center)
590 (eq state 'unapplied)))
591 (setq inserted-index t)
592 (stgit-run-series-insert-index ewoc)))
593 (setq all-patchsyms (cons name all-patchsyms))
594 (ewoc-enter-last ewoc
595 (make-stgit-patch
596 :status state
597 :name name
598 :desc desc
599 :empty empty)))
600 (forward-line 1)))))
8f702de4 601 (unless inserted-index
bf7e391c
GH
602 (stgit-run-series-insert-index ewoc))
603 (setq stgit-index-node index-node
604 stgit-worktree-node worktree-node
605 stgit-marked-patches (intersection stgit-marked-patches
606 all-patchsyms)))))
98230edd 607
3e528fb0
GH
608(defun stgit-current-branch ()
609 "Return the name of the current branch."
610 (substring (with-output-to-string
611 (stgit-run-silent "branch"))
612 0 -1))
613
f9533abf
GH
614(defun stgit-reload (&optional description)
615 "Update the contents of the StGit buffer.
616
617If DESCRIPTION is non-nil, it is displayed as a status message
618during the operation."
56d81fe5 619 (interactive)
9d04c657 620 (stgit-assert-mode)
f9533abf
GH
621 (when description
622 (message "%s..." description))
56d81fe5
DK
623 (let ((inhibit-read-only t)
624 (curline (line-number-at-pos))
a9089e68 625 (curpatch (stgit-patch-name-at-point))
f9533abf
GH
626 (curfile (stgit-patched-file-at-point))
627 (stgit-inhibit-messages description))
98230edd
DK
628 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
629 (ewoc-set-hf stgit-ewoc
630 (concat "Branch: "
3e528fb0
GH
631 (propertize (stgit-current-branch)
632 'face 'stgit-branch-name-face)
4f7ff561 633 "\n\n")
ce3b6130
DK
634 (if stgit-show-worktree
635 "--"
636 (propertize
637 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
638 shows the working tree\n")
6a73154a 639 'face 'stgit-description-face)))
98230edd 640 (stgit-run-series stgit-ewoc)
2570f6e5
GH
641 (unless (and curpatch
642 (stgit-goto-patch curpatch
643 (and curfile (stgit-file->file curfile))))
79473ca5
GH
644 (goto-char (point-min))
645 (forward-line (1- curline))
f9533abf
GH
646 (move-to-column (stgit-goal-column)))
647 (stgit-refresh-git-status))
648 (when description
649 (message "%s...done" description)))
56d81fe5 650
1f60181a
GH
651(defconst stgit-file-status-code-strings
652 (mapcar (lambda (arg)
653 (cons (car arg)
a6d9a852
GH
654 (propertize (cadr arg) 'face (car (cddr arg)))))
655 '((add "Added" stgit-modified-file-face)
656 (copy "Copied" stgit-modified-file-face)
657 (delete "Deleted" stgit-modified-file-face)
658 (modify "Modified" stgit-modified-file-face)
659 (rename "Renamed" stgit-modified-file-face)
660 (mode-change "Mode change" stgit-modified-file-face)
661 (unmerged "Unmerged" stgit-unmerged-file-face)
d9473917
GH
662 (unknown "Unknown" stgit-unknown-file-face)
663 (ignore "Ignored" stgit-ignored-file-face)))
1f60181a
GH
664 "Alist of code symbols to description strings")
665
000f337c
GH
666(defconst stgit-patch-status-face-alist
667 '((applied . stgit-applied-patch-face)
668 (top . stgit-top-patch-face)
669 (unapplied . stgit-unapplied-patch-face)
bf7e391c 670 (committed . stgit-committed-patch-face)
9153ce3a
GH
671 (index . stgit-index-work-tree-title-face)
672 (work . stgit-index-work-tree-title-face))
000f337c
GH
673 "Alist of face to use for a given patch status")
674
3164eec6
DK
675(defun stgit-file-status-code-as-string (file)
676 "Return stgit status code for FILE as a string"
413f9909 677 (let* ((code (assq (stgit-file->status file)
3164eec6 678 stgit-file-status-code-strings))
413f9909 679 (score (stgit-file->cr-score file)))
3164eec6 680 (when code
43ee50b6
DK
681 (if (and score (/= score 100))
682 (format "%s %s" (cdr code)
683 (propertize (format "%d%%" score)
684 'face 'stgit-description-face))
685 (cdr code)))))
1f60181a 686
a6d9a852 687(defun stgit-file-status-code (str &optional score)
1f60181a
GH
688 "Return stgit status code from git status string"
689 (let ((code (assoc str '(("A" . add)
690 ("C" . copy)
691 ("D" . delete)
d9473917 692 ("I" . ignore)
1f60181a
GH
693 ("M" . modify)
694 ("R" . rename)
695 ("T" . mode-change)
696 ("U" . unmerged)
697 ("X" . unknown)))))
a6d9a852
GH
698 (setq code (if code (cdr code) 'unknown))
699 (when (stringp score)
700 (if (> (length score) 0)
701 (setq score (string-to-number score))
702 (setq score nil)))
703 (if score (cons code score) code)))
704
705(defconst stgit-file-type-strings
706 '((#o100 . "file")
707 (#o120 . "symlink")
708 (#o160 . "subproject"))
709 "Alist of names of file types")
710
711(defun stgit-file-type-string (type)
47271f41
GH
712 "Return string describing file type TYPE (the high bits of file permission).
713Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
a6d9a852
GH
714 (let ((type-str (assoc type stgit-file-type-strings)))
715 (or (and type-str (cdr type-str))
716 (format "unknown type %o" type))))
717
718(defun stgit-file-type-change-string (old-perm new-perm)
47271f41
GH
719 "Return string describing file type change from OLD-PERM to NEW-PERM.
720Cf. `stgit-file-type-string'."
a6d9a852
GH
721 (let ((old-type (lsh old-perm -9))
722 (new-type (lsh new-perm -9)))
723 (cond ((= old-type new-type) "")
724 ((zerop new-type) "")
725 ((zerop old-type)
726 (if (= new-type #o100)
727 ""
43ee50b6
DK
728 (format "(%s)" (stgit-file-type-string new-type))))
729 (t (format "(%s -> %s)"
a6d9a852
GH
730 (stgit-file-type-string old-type)
731 (stgit-file-type-string new-type))))))
732
733(defun stgit-file-mode-change-string (old-perm new-perm)
47271f41
GH
734 "Return string describing file mode change from OLD-PERM to NEW-PERM.
735Cf. `stgit-file-type-change-string'."
a6d9a852
GH
736 (setq old-perm (logand old-perm #o777)
737 new-perm (logand new-perm #o777))
738 (if (or (= old-perm new-perm)
739 (zerop old-perm)
740 (zerop new-perm))
741 ""
742 (let* ((modified (logxor old-perm new-perm))
743 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
744 (cond ((zerop modified) "")
745 ((and (zerop not-x-modified)
746 (or (and (eq #o111 (logand old-perm #o111))
747 (propertize "-x" 'face 'stgit-file-permission-face))
748 (and (eq #o111 (logand new-perm #o111))
749 (propertize "+x" 'face
750 'stgit-file-permission-face)))))
751 (t (concat (propertize (format "%o" old-perm)
752 'face 'stgit-file-permission-face)
753 (propertize " -> "
754 'face 'stgit-description-face)
755 (propertize (format "%o" new-perm)
756 'face 'stgit-file-permission-face)))))))
1f60181a 757
413f9909
GH
758(defstruct (stgit-file
759 (:conc-name stgit-file->))
0de6881a
DK
760 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
761
ca027a87 762(defun stgit-describe-copy-or-rename (file)
6a73154a
GH
763 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
764 from to common-head common-tail)
ca027a87
GH
765
766 (when stgit-abbreviate-copies-and-renames
413f9909
GH
767 (setq from (split-string (stgit-file->cr-from file) "/")
768 to (split-string (stgit-file->cr-to file) "/"))
ca027a87
GH
769
770 (while (and from to (cdr from) (cdr to)
771 (string-equal (car from) (car to)))
772 (setq common-head (cons (car from) common-head)
773 from (cdr from)
774 to (cdr to)))
775 (setq common-head (nreverse common-head)
776 from (nreverse from)
777 to (nreverse to))
778 (while (and from to (cdr from) (cdr to)
779 (string-equal (car from) (car to)))
780 (setq common-tail (cons (car from) common-tail)
781 from (cdr from)
782 to (cdr to)))
783 (setq from (nreverse from)
784 to (nreverse to)))
785
786 (if (or common-head common-tail)
787 (concat (if common-head
788 (mapconcat #'identity common-head "/")
789 "")
790 (if common-head "/" "")
791 (propertize "{" 'face 'stgit-description-face)
792 (mapconcat #'identity from "/")
793 arrow
794 (mapconcat #'identity to "/")
795 (propertize "}" 'face 'stgit-description-face)
796 (if common-tail "/" "")
797 (if common-tail
798 (mapconcat #'identity common-tail "/")
799 ""))
413f9909 800 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
ca027a87 801
3164eec6 802(defun stgit-file-pp (file)
43ee50b6
DK
803 (let ((start (point))
804 (spec (format-spec-make
805 ?s (stgit-file-status-code-as-string file)
806 ?m (stgit-file-mode-change-string
413f9909
GH
807 (stgit-file->old-perm file)
808 (stgit-file->new-perm file))
809 ?n (if (stgit-file->copy-or-rename file)
43ee50b6 810 (stgit-describe-copy-or-rename file)
413f9909 811 (stgit-file->file file))
43ee50b6 812 ?c (propertize (stgit-file-type-change-string
413f9909
GH
813 (stgit-file->old-perm file)
814 (stgit-file->new-perm file))
43ee50b6 815 'face 'stgit-description-face))))
da30db2a
GH
816 (stgit-insert-without-trailing-whitespace
817 (format-spec stgit-file-line-format spec))
0de6881a 818 (add-text-properties start (point)
3164eec6
DK
819 (list 'entry-type 'file
820 'file-data file))))
0de6881a 821
7567401c
GH
822(defun stgit-find-copies-harder-diff-arg ()
823 "Return the flag to use with `git-diff' depending on the
b6df231c
GH
824`stgit-find-copies-harder' flag."
825 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
7567401c 826
d9473917
GH
827(defun stgit-insert-ls-files (args file-flag)
828 (let ((start (point)))
829 (apply 'stgit-run-git
830 (append '("ls-files" "--exclude-standard" "-z") args))
831 (goto-char start)
832 (while (looking-at "\\([^\0]*\\)\0")
833 (let ((name-len (- (match-end 0) (match-beginning 0))))
834 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
835 (forward-char name-len)))))
836
7f972e9b
GH
837(defun stgit-process-files (callback)
838 (goto-char (point-min))
839 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
840 (goto-char (match-end 0)))
841 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
842 (let ((old-perm (string-to-number (match-string 1) 8))
843 (new-perm (string-to-number (match-string 2) 8)))
844 (goto-char (match-end 0))
845 (let ((file
846 (cond ((looking-at
847 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
848 (let* ((patch-status (stgit-patch->status patch))
849 (file-subexp (if (eq patch-status 'unapplied)
850 3
851 4))
852 (file (match-string file-subexp)))
853 (make-stgit-file
854 :old-perm old-perm
855 :new-perm new-perm
856 :copy-or-rename t
857 :cr-score (string-to-number (match-string 2))
858 :cr-from (match-string 3)
859 :cr-to (match-string 4)
860 :status (stgit-file-status-code
861 (match-string 1))
862 :file file)))
863 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
864 (make-stgit-file
865 :old-perm old-perm
866 :new-perm new-perm
867 :copy-or-rename nil
868 :cr-score nil
869 :cr-from nil
870 :cr-to nil
871 :status (stgit-file-status-code
872 (match-string 1))
873 :file (match-string 2))))))
874 (goto-char (match-end 0))
875 (funcall callback file)))))
876
877
0de6881a 878(defun stgit-insert-patch-files (patch)
88134ff7
GH
879 "Expand (show modification of) the patch PATCH after the line
880at point."
d2efc9d5
GH
881 (let* ((patchsym (stgit-patch->name patch))
882 (end (point-marker))
883 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
884 (ewoc (ewoc-create #'stgit-file-pp nil nil t))
885 (show-ignored stgit-show-ignored)
886 (show-unknown stgit-show-unknown))
0434bec1 887 (set-marker-insertion-type end t)
413f9909 888 (setf (stgit-patch->files-ewoc patch) ewoc)
0de6881a 889 (with-temp-buffer
ea305902
GH
890 (let ((standard-output (current-buffer)))
891 (apply 'stgit-run-git
892 (cond ((eq patchsym :work)
0b71b4dc
GH
893 (let (standard-output)
894 (stgit-run-git "update-index" "--refresh"))
ea305902
GH
895 `("diff-files" "-0" ,@args))
896 ((eq patchsym :index)
897 `("diff-index" ,@args "--cached" "HEAD"))
898 (t
899 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
900
901 (when (and (eq patchsym :work))
d2efc9d5 902 (when show-ignored
ea305902 903 (stgit-insert-ls-files '("--ignored" "--others") "I"))
d2efc9d5 904 (when show-unknown
7f972e9b
GH
905 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
906 "--others")
907 "X"))
ea305902
GH
908 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
909 (point-min) (point-max)))
910
7f972e9b 911 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
ea305902
GH
912
913 (unless (ewoc-nth ewoc 0)
914 (ewoc-set-hf ewoc ""
915 (concat " "
916 (propertize "<no files>"
917 'face 'stgit-description-face)
918 "\n")))))
0434bec1 919 (goto-char end)))
07f464e0 920
030f0535
GH
921(defun stgit-find-file (&optional other-window)
922 (let* ((file (or (stgit-patched-file-at-point)
923 (error "No file at point")))
413f9909 924 (filename (expand-file-name (stgit-file->file file))))
0de6881a
DK
925 (unless (file-exists-p filename)
926 (error "File does not exist"))
030f0535
GH
927 (funcall (if other-window 'find-file-other-window 'find-file)
928 filename)
413f9909 929 (when (eq (stgit-file->status file) 'unmerged)
030f0535 930 (smerge-mode 1))))
acc5652f 931
afbf766b 932(defun stgit-expand (&optional patches collapse)
fd64ee57 933 "Show the contents of marked patches, or the patch at point.
afbf766b
GH
934
935See also `stgit-collapse'.
936
937Non-interactively, operate on PATCHES, and collapse instead of
938expand if COLLAPSE is not nil."
beac0f14 939 (interactive (list (stgit-patches-marked-or-at-point t)))
9d04c657 940 (stgit-assert-mode)
afbf766b
GH
941 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
942 patches stgit-expanded-patches)))
943 (setq stgit-expanded-patches
944 (if collapse
945 (set-difference stgit-expanded-patches patches-diff)
946 (append stgit-expanded-patches patches-diff)))
947 (ewoc-map #'(lambda (patch)
413f9909 948 (memq (stgit-patch->name patch) patches-diff))
afbf766b
GH
949 stgit-ewoc))
950 (move-to-column (stgit-goal-column)))
951
952(defun stgit-collapse (&optional patches)
fd64ee57 953 "Hide the contents of marked patches, or the patch at point.
afbf766b
GH
954
955See also `stgit-expand'."
beac0f14 956 (interactive (list (stgit-patches-marked-or-at-point t)))
9d04c657 957 (stgit-assert-mode)
afbf766b
GH
958 (stgit-expand patches t))
959
50d88c67 960(defun stgit-select-patch ()
98230edd 961 (let ((patchname (stgit-patch-name-at-point)))
afbf766b
GH
962 (stgit-expand (list patchname)
963 (memq patchname stgit-expanded-patches))))
acc5652f 964
7f972e9b
GH
965(defun stgit-expand-directory (file)
966 (let* ((patch (stgit-patch-at-point))
967 (ewoc (stgit-patch->files-ewoc patch))
968 (node (ewoc-locate ewoc))
969 (filename (stgit-file->file file))
970 (start (make-marker))
971 (end (make-marker)))
972
973 (save-excursion
974 (forward-line 1)
975 (set-marker start (point))
976 (set-marker end (point))
977 (set-marker-insertion-type end t))
978
979 (assert (string-match "/$" filename))
980 ;; remove trailing "/"
981 (setf (stgit-file->file file) (substring filename 0 -1))
982 (ewoc-invalidate ewoc node)
983
984 (with-temp-buffer
985 (let ((standard-output (current-buffer)))
986 (stgit-insert-ls-files (list "--directory" "--others"
987 "--no-empty-directory" "--"
988 filename)
989 "X")
990 (stgit-process-files (lambda (f)
991 (setq node (ewoc-enter-after ewoc node f))))))
992
62548eec
GH
993 (move-to-column (stgit-goal-column))
994
7f972e9b
GH
995 (let ((inhibit-read-only t))
996 (put-text-property start end 'patch-data patch))))
997
998(defun stgit-select-file ()
999 (let* ((file (or (stgit-patched-file-at-point)
1000 (error "No file at point")))
1001 (filename (stgit-file->file file)))
1002 (if (string-match "/$" filename)
1003 (stgit-expand-directory file)
1004 (stgit-find-file))))
1005
378a003d 1006(defun stgit-select ()
da01a29b
GH
1007 "With point on a patch, toggle showing files in the patch.
1008
1009With point on a file, open the associated file. Opens the target
1010file for (applied) copies and renames."
378a003d 1011 (interactive)
9d04c657 1012 (stgit-assert-mode)
50d88c67
DK
1013 (case (get-text-property (point) 'entry-type)
1014 ('patch
1015 (stgit-select-patch))
1016 ('file
7f972e9b 1017 (stgit-select-file))
50d88c67
DK
1018 (t
1019 (error "No patch or file on line"))))
378a003d
GH
1020
1021(defun stgit-find-file-other-window ()
1022 "Open file at point in other window"
1023 (interactive)
9d04c657 1024 (stgit-assert-mode)
030f0535 1025 (stgit-find-file t))
378a003d 1026
d9b954c7
GH
1027(defun stgit-find-file-merge ()
1028 "Open file at point and merge it using `smerge-ediff'."
1029 (interactive)
9d04c657 1030 (stgit-assert-mode)
d9b954c7
GH
1031 (stgit-find-file t)
1032 (smerge-ediff))
1033
83327d53 1034(defun stgit-quit ()
a53347d9 1035 "Hide the stgit buffer."
83327d53 1036 (interactive)
9d04c657 1037 (stgit-assert-mode)
83327d53
GH
1038 (bury-buffer))
1039
0f076fe6 1040(defun stgit-git-status ()
a53347d9 1041 "Show status using `git-status'."
0f076fe6 1042 (interactive)
9d04c657 1043 (stgit-assert-mode)
0f076fe6 1044 (unless (fboundp 'git-status)
df283a8b 1045 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
1046 (let ((dir default-directory))
1047 (save-selected-window
1048 (pop-to-buffer nil)
1049 (git-status dir))))
1050
58f72f16
GH
1051(defun stgit-goal-column ()
1052 "Return goal column for the current line"
50d88c67
DK
1053 (case (get-text-property (point) 'entry-type)
1054 ('patch 2)
1055 ('file 4)
1056 (t 0)))
58f72f16
GH
1057
1058(defun stgit-next-line (&optional arg)
378a003d 1059 "Move cursor vertically down ARG lines"
58f72f16 1060 (interactive "p")
9d04c657 1061 (stgit-assert-mode)
58f72f16
GH
1062 (next-line arg)
1063 (move-to-column (stgit-goal-column)))
378a003d 1064
58f72f16 1065(defun stgit-previous-line (&optional arg)
378a003d 1066 "Move cursor vertically up ARG lines"
58f72f16 1067 (interactive "p")
9d04c657 1068 (stgit-assert-mode)
58f72f16
GH
1069 (previous-line arg)
1070 (move-to-column (stgit-goal-column)))
378a003d
GH
1071
1072(defun stgit-next-patch (&optional arg)
98230edd 1073 "Move cursor down ARG patches."
378a003d 1074 (interactive "p")
9d04c657 1075 (stgit-assert-mode)
006cd3e5
GH
1076 (unless arg (setq arg 1))
1077 (cond ((< arg 0)
1078 (stgit-previous-patch (- arg)))
1079 ((zerop arg)
1080 (move-to-column (stgit-goal-column)))
1081 (t
1082 (when (stgit-at-header-p)
1083 (ewoc-goto-node stgit-ewoc (ewoc-nth stgit-ewoc 0))
1084 (setq arg (1- arg)))
1085 (ewoc-goto-next stgit-ewoc arg)
1086 (move-to-column goal-column))))
378a003d
GH
1087
1088(defun stgit-previous-patch (&optional arg)
98230edd 1089 "Move cursor up ARG patches."
378a003d 1090 (interactive "p")
9d04c657 1091 (stgit-assert-mode)
006cd3e5
GH
1092 (unless arg (setq arg 1))
1093 (cond ((< arg 0)
1094 (stgit-next-patch (- arg)))
1095 ((zerop arg)
1096 (move-to-column (stgit-goal-column)))
1097 ((stgit-at-header-p)
1098 (goto-char (point-min)))
1099 (t
1100 (let ((opatch (stgit-patch-at-point)))
1101 (ewoc-goto-prev stgit-ewoc arg)
1102 (unless (zerop arg)
1103 (when (eq opatch (stgit-patch-at-point))
1104 (goto-char (point-min)))))
1105 (move-to-column (stgit-goal-column)))))
1106
1107(defun stgit-previous-patch-group (&optional arg)
1108 "Move to the previous group of patches.
1109
1110If ARG is non-nil, do this ARG times. If ARG is negative, move
1111-ARG groups forward instead; cf. `stgit-next-patch-group'."
1112 (interactive "p")
1113 (stgit-assert-mode)
1114 (if (< arg 0)
1115 (stgit-previous-patch-group (- arg))
1116 (while (and (not (bobp))
1117 (> arg 0))
1118 (stgit-previous-patch 1)
1119 (let* ((opoint (point))
1120 (patch (stgit-patch-at-point))
1121 (status (and patch (stgit-patch->status patch))))
1122 (while (and (not (bobp))
1123 (let* ((npatch (stgit-patch-at-point))
1124 (nstatus (and npatch (stgit-patch->status npatch))))
1125 (eq status nstatus)))
1126 (setq opoint (point))
1127 (stgit-previous-patch 1))
1128 (goto-char opoint))
1129 (setq arg (1- arg)))))
1130
1131(defun stgit-next-patch-group (&optional arg)
1132 "Move to the next group of patches.
1133
1134If ARG is non-nil, do this ARG times. If ARG is negative, move
1135-ARG groups backwards instead; cf. `stgit-previous-patch-group'."
1136 (interactive "p")
1137 (stgit-assert-mode)
1138 (if (< arg 0)
1139 (stgit-previous-patch-group (- arg))
1140 (while (and (not (eobp))
1141 (> arg 0))
1142 (let* ((patch (stgit-patch-at-point))
1143 (status (and patch (stgit-patch->status patch))))
1144 (while (and (not (eobp))
1145 (let* ((npatch (stgit-patch-at-point))
1146 (nstatus (and npatch (stgit-patch->status npatch))))
1147 (eq status nstatus)))
1148 (stgit-next-patch 1)))
1149 (setq arg (1- arg)))))
378a003d 1150
56d81fe5
DK
1151(defvar stgit-mode-hook nil
1152 "Run after `stgit-mode' is setup.")
1153
1154(defvar stgit-mode-map nil
1155 "Keymap for StGit major mode.")
1156
1157(unless stgit-mode-map
5038381d
GH
1158 (let ((diff-map (make-sparse-keymap))
1159 (toggle-map (make-sparse-keymap)))
d9b954c7
GH
1160 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
1161 '(("b" . stgit-diff-base)
1162 ("c" . stgit-diff-combined)
1163 ("m" . stgit-find-file-merge)
1164 ("o" . stgit-diff-ours)
e02b46e5 1165 ("r" . stgit-diff-range)
d9b954c7 1166 ("t" . stgit-diff-theirs)))
ce3b6130 1167 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
a0045b87
DK
1168 '(("n" . stgit-toggle-patch-names)
1169 ("t" . stgit-toggle-worktree)
bf7e391c 1170 ("h" . stgit-toggle-committed)
d9473917 1171 ("i" . stgit-toggle-ignored)
137b315b
GH
1172 ("u" . stgit-toggle-unknown)
1173 ("s" . stgit-toggle-svn)))
ce3b6130
DK
1174 (setq stgit-mode-map (make-keymap))
1175 (suppress-keymap stgit-mode-map)
1176 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
d11e0621
GH
1177 `((" " . stgit-mark-down)
1178 ("m" . stgit-mark-down)
ce3b6130
DK
1179 ("\d" . stgit-unmark-up)
1180 ("u" . stgit-unmark-down)
1181 ("?" . stgit-help)
1182 ("h" . stgit-help)
1183 ("\C-p" . stgit-previous-line)
1184 ("\C-n" . stgit-next-line)
1185 ([up] . stgit-previous-line)
1186 ([down] . stgit-next-line)
1187 ("p" . stgit-previous-patch)
1188 ("n" . stgit-next-patch)
006cd3e5
GH
1189 ("\M-{" . stgit-previous-patch-group)
1190 ("\M-}" . stgit-next-patch-group)
1191 ([(control up)] . stgit-previous-patch-group)
1192 ([(control down)] . stgit-next-patch-group)
ce3b6130 1193 ("s" . stgit-git-status)
408fa7cb 1194 ("g" . stgit-reload-or-repair)
ce3b6130
DK
1195 ("r" . stgit-refresh)
1196 ("\C-c\C-r" . stgit-rename)
1197 ("e" . stgit-edit)
1198 ("M" . stgit-move-patches)
1199 ("S" . stgit-squash)
1200 ("N" . stgit-new)
2acb7116 1201 ("c" . stgit-new-and-refresh)
e9fdd4ea
GH
1202 ("\C-c\C-c" . stgit-commit)
1203 ("\C-c\C-u" . stgit-uncommit)
1629f59f 1204 ("U" . stgit-revert)
51783171 1205 ("R" . stgit-resolve-file)
ce3b6130 1206 ("\r" . stgit-select)
afbf766b
GH
1207 ("+" . stgit-expand)
1208 ("-" . stgit-collapse)
ce3b6130 1209 ("o" . stgit-find-file-other-window)
dde3ab4d 1210 ("i" . stgit-toggle-index)
ce3b6130
DK
1211 (">" . stgit-push-next)
1212 ("<" . stgit-pop-next)
1213 ("P" . stgit-push-or-pop)
1214 ("G" . stgit-goto)
d9b954c7 1215 ("=" . stgit-diff)
ce3b6130 1216 ("D" . stgit-delete)
b8463f1d 1217 ([?\C-/] . stgit-undo)
ce3b6130 1218 ("\C-_" . stgit-undo)
b8463f1d
GH
1219 ([?\C-c ?\C-/] . stgit-redo)
1220 ("\C-c\C-_" . stgit-redo)
ce3b6130 1221 ("B" . stgit-branch)
380a021f 1222 ("\C-c\C-b" . stgit-rebase)
ce3b6130 1223 ("t" . ,toggle-map)
d9b954c7 1224 ("d" . ,diff-map)
e8faa44b
DK
1225 ("q" . stgit-quit)
1226 ("!" . stgit-execute))))
5038381d
GH
1227
1228 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
413f9909 1229 (and file (eq (stgit-file->status file)
5038381d
GH
1230 'unmerged))))
1231 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1232 (easy-menu-define stgit-menu stgit-mode-map
1233 "StGit Menu"
1234 `("StGit"
1235 ["Reload" stgit-reload-or-repair
1236 :help "Reload StGit status from disk"]
1237 ["Repair" stgit-repair
1238 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1239 :help "Repair StGit metadata"]
1240 "-"
1241 ["Undo" stgit-undo t]
1242 ["Redo" stgit-redo t]
1243 "-"
1244 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1245 "-"
1246 ["New patch" stgit-new-and-refresh
1247 :help "Create a new patch from changes in index or work tree"
1248 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1249 ["New empty patch" stgit-new
1250 :help "Create a new, empty patch"]
1251 ["(Un)mark patch" stgit-toggle-mark
1252 :label (if (memq (stgit-patch-name-at-point nil t)
1253 stgit-marked-patches)
1254 "Unmark patch" "Mark patch")
1255 :active (stgit-patch-name-at-point nil t)]
1256 ["Expand/collapse patch"
1257 (let ((patches (stgit-patches-marked-or-at-point)))
1258 (if (member-if ,patch-collapsed-p patches)
1259 (stgit-expand patches)
1260 (stgit-collapse patches)))
1261 :label (if (member-if ,patch-collapsed-p
1262 (stgit-patches-marked-or-at-point))
1263 "Expand patches"
1264 "Collapse patches")
1265 :active (stgit-patches-marked-or-at-point)]
1266 ["Edit patch" stgit-edit
1267 :help "Edit patch comment"
1268 :active (stgit-patch-name-at-point nil t)]
1269 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1270 ["Push/pop patch" stgit-push-or-pop
7c11b754
GH
1271 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1272 (stgit-applied-patchsyms t))
1273 "Pop patches" "Push patches")]
beac0f14
GH
1274 ["Delete patches" stgit-delete
1275 :active (stgit-patches-marked-or-at-point nil t)]
5038381d
GH
1276 "-"
1277 ["Move patches" stgit-move-patches
1278 :active stgit-marked-patches
fd64ee57 1279 :help "Move marked patch(es) to point"]
5038381d
GH
1280 ["Squash patches" stgit-squash
1281 :active (> (length stgit-marked-patches) 1)
fd64ee57 1282 :help "Merge marked patches into one"]
5038381d
GH
1283 "-"
1284 ["Refresh top patch" stgit-refresh
1285 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1286 :help "Refresh the top patch with changes in index or work tree"]
1287 ["Refresh this patch" (stgit-refresh t)
1288 :keys "\\[universal-argument] \\[stgit-refresh]"
fd64ee57 1289 :help "Refresh marked patch with changes in index or work tree"
5038381d
GH
1290 :active (and (not (and (stgit-index-empty-p)
1291 (stgit-work-tree-empty-p)))
1292 (stgit-patch-name-at-point nil t))]
1293 "-"
1294 ["Find file" stgit-select
1295 :active (eq (get-text-property (point) 'entry-type) 'file)]
1296 ["Open file" stgit-find-file-other-window
1297 :active (eq (get-text-property (point) 'entry-type) 'file)]
1298 ["Toggle file index" stgit-toggle-index
1299 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1300 (memq (stgit-patch-name-at-point) '(:work :index)))
1301 :label (if (eq (stgit-patch-name-at-point) :work)
1302 "Move change to index"
1303 "Move change to work tree")]
1304 "-"
1305 ["Show diff" stgit-diff
1306 :active (get-text-property (point) 'entry-type)]
e02b46e5
KW
1307 ["Show diff for range of applied patches" stgit-diff-range
1308 :active (= (length stgit-marked-patches) 1)]
5038381d
GH
1309 ("Merge"
1310 :active (stgit-git-index-unmerged-p)
1311 ["Combined diff" stgit-diff-combined
1312 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1313 ["Diff against base" stgit-diff-base
1314 :help "Show diff against the common base"
1315 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1316 ["Diff against ours" stgit-diff-ours
1317 :help "Show diff against our branch"
1318 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1319 ["Diff against theirs" stgit-diff-theirs
1320 :help "Show diff against their branch"
1321 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1322 "-"
1323 ["Interactive merge" stgit-find-file-merge
1324 :help "Interactively merge the file"
1325 :active ,at-unmerged-file]
1326 ["Resolve file" stgit-resolve-file
1327 :help "Mark file conflict as resolved"
1328 :active ,at-unmerged-file]
1329 )
1330 "-"
1331 ["Show index & work tree" stgit-toggle-worktree :style toggle
1332 :selected stgit-show-worktree]
1333 ["Show unknown files" stgit-toggle-unknown :style toggle
1334 :selected stgit-show-unknown :active stgit-show-worktree]
1335 ["Show ignored files" stgit-toggle-ignored :style toggle
1336 :selected stgit-show-ignored :active stgit-show-worktree]
a0045b87
DK
1337 ["Show patch names" stgit-toggle-patch-names :style toggle
1338 :selected stgit-show-patch-names]
bf7e391c
GH
1339 ["Show recent commits" stgit-toggle-committed :style toggle
1340 :selected stgit-show-committed]
137b315b
GH
1341 ["Show subversion info" stgit-toggle-svn :style toggle
1342 :selected stgit-show-svn]
5038381d
GH
1343 "-"
1344 ["Switch branches" stgit-branch t
3e528fb0 1345 :help "Switch to or create another branch"]
5038381d
GH
1346 ["Rebase branch" stgit-rebase t
1347 :help "Rebase the current branch"]
06115546
GH
1348 "-"
1349 ["Customize StGit" (customize-group 'stgit)]
5038381d
GH
1350 ))))
1351
1352;; disable tool bar editing buttons
1353(put 'stgit-mode 'mode-class 'special)
56d81fe5
DK
1354
1355(defun stgit-mode ()
1356 "Major mode for interacting with StGit.
fdf5e327
GH
1357
1358Start StGit using \\[stgit].
1359
1360Basic commands:
1361\\<stgit-mode-map>\
1362\\[stgit-help] Show this help text
1363\\[stgit-quit] Hide the StGit buffer
1364\\[describe-bindings] Show all key bindings
1365
1366\\[stgit-reload-or-repair] Reload the StGit buffer
1367\\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1368
1369\\[stgit-undo] Undo most recent StGit operation
1370\\[stgit-redo] Undo recent undo
1371
1372\\[stgit-git-status] Run `git-status' (if available)
1373
e8faa44b
DK
1374\\[stgit-execute] Run an stg shell command
1375
fdf5e327
GH
1376Movement commands:
1377\\[stgit-previous-line] Move to previous line
1378\\[stgit-next-line] Move to next line
1379\\[stgit-previous-patch] Move to previous patch
1380\\[stgit-next-patch] Move to next patch
006cd3e5
GH
1381\\[stgit-previous-patch-group] Move to previous patch group
1382\\[stgit-next-patch-group] Move to next patch group
fdf5e327 1383
d11e0621 1384\\[stgit-mark-down] Mark patch and move down
fdf5e327
GH
1385\\[stgit-unmark-up] Unmark patch and move up
1386\\[stgit-unmark-down] Unmark patch and move down
1387
1388Commands for patches:
1389\\[stgit-select] Toggle showing changed files in patch
1390\\[stgit-refresh] Refresh patch with changes in index or work tree
1391\\[stgit-diff] Show the patch log and diff
1392
fd64ee57
GH
1393\\[stgit-expand] Show changes in marked patches
1394\\[stgit-collapse] Hide changes in marked patches
afbf766b 1395
2acb7116 1396\\[stgit-new-and-refresh] Create a new patch from index or work tree
c20b20a5
GH
1397\\[stgit-new] Create a new, empty patch
1398
fdf5e327
GH
1399\\[stgit-rename] Rename patch
1400\\[stgit-edit] Edit patch description
1401\\[stgit-delete] Delete patch(es)
1402
1629f59f 1403\\[stgit-revert] Revert all changes in index or work tree
dde3ab4d 1404\\[stgit-toggle-index] Toggle all changes between index and work tree
1629f59f 1405
fdf5e327
GH
1406\\[stgit-push-next] Push next patch onto stack
1407\\[stgit-pop-next] Pop current patch from stack
c20b20a5
GH
1408\\[stgit-push-or-pop] Push or pop marked patches
1409\\[stgit-goto] Make patch at point current by popping or pushing
fdf5e327
GH
1410
1411\\[stgit-squash] Squash (meld together) patches
c20b20a5 1412\\[stgit-move-patches] Move marked patches to point
fdf5e327
GH
1413
1414\\[stgit-commit] Commit patch(es)
1415\\[stgit-uncommit] Uncommit patch(es)
1416
1417Commands for files:
1418\\[stgit-select] Open the file in this window
1419\\[stgit-find-file-other-window] Open the file in another window
1420\\[stgit-diff] Show the file's diff
1421
dde3ab4d 1422\\[stgit-toggle-index] Toggle change between index and work tree
1629f59f 1423\\[stgit-revert] Revert changes to file
fdf5e327
GH
1424
1425Display commands:
a0045b87 1426\\[stgit-toggle-patch-names] Toggle showing patch names
fdf5e327
GH
1427\\[stgit-toggle-worktree] Toggle showing index and work tree
1428\\[stgit-toggle-unknown] Toggle showing unknown files
1429\\[stgit-toggle-ignored] Toggle showing ignored files
bf7e391c 1430\\[stgit-toggle-committed] Toggle showing recent commits
137b315b 1431\\[stgit-toggle-svn] Toggle showing subversion information
fdf5e327
GH
1432
1433Commands for diffs:
1434\\[stgit-diff] Show diff of patch or file
e02b46e5 1435\\[stgit-diff-range] Show diff for range of patches
fdf5e327
GH
1436\\[stgit-diff-base] Show diff against the merge base
1437\\[stgit-diff-ours] Show diff against our branch
1438\\[stgit-diff-theirs] Show diff against their branch
1439
1440 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1441ignore space changes.
1442 With two prefix arguments (e.g., \\[universal-argument] \
1443\\[universal-argument] \\[stgit-diff]), ignore all space changes.
1444
1445Commands for merge conflicts:
1446\\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1447\\[stgit-resolve-file] Mark unmerged file as resolved
1448
1449Commands for branches:
3e528fb0 1450\\[stgit-branch] Switch to or create another branch
380a021f 1451\\[stgit-rebase] Rebase the current branch
fdf5e327
GH
1452
1453Customization variables:
1454`stgit-abbreviate-copies-and-renames'
bc11fb08 1455`stgit-default-show-ignored'
a0045b87 1456`stgit-default-show-patch-names'
bc11fb08 1457`stgit-default-show-unknown'
fdf5e327 1458`stgit-default-show-worktree'
bf7e391c 1459`stgit-default-show-committed'
137b315b 1460`stgit-default-show-svn'
bf7e391c 1461`stgit-default-committed-count'
fdf5e327
GH
1462`stgit-find-copies-harder'
1463`stgit-show-worktree-mode'
1464
1465See also \\[customize-group] for the \"stgit\" group."
56d81fe5
DK
1466 (kill-all-local-variables)
1467 (buffer-disable-undo)
1468 (setq mode-name "StGit"
1469 major-mode 'stgit-mode
1470 goal-column 2)
1471 (use-local-map stgit-mode-map)
a0689e11
GH
1472 (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x)))
1473 `((list-buffers-directory . ,default-directory)
1474 (parse-sexp-lookup-properties . t)
1475 (stgit-expanded-patches . (:work :index))
1476 (stgit-index-node . nil)
1477 (stgit-worktree-node . nil)
1478 (stgit-marked-patches . nil)
137b315b 1479 (stgit-svn-find-rev-hash . ,(make-hash-table :test 'equal))
bf7e391c
GH
1480 (stgit-committed-count . ,stgit-default-committed-count)
1481 (stgit-show-committed . ,stgit-default-show-committed)
a0689e11
GH
1482 (stgit-show-ignored . ,stgit-default-show-ignored)
1483 (stgit-show-patch-names . ,stgit-default-show-patch-names)
137b315b 1484 (stgit-show-svn . ,stgit-default-show-svn)
a0689e11
GH
1485 (stgit-show-unknown . ,stgit-default-show-unknown)
1486 (stgit-show-worktree . ,stgit-default-show-worktree)))
2870f8b8 1487 (set-variable 'truncate-lines 't)
e44674e3
GH
1488 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1489 (unless stgit-did-advise
1490 (stgit-advise)
1491 (setq stgit-did-advise t))
56d81fe5
DK
1492 (run-hooks 'stgit-mode-hook))
1493
e44674e3
GH
1494(defun stgit-advise-funlist (funlist)
1495 "Add advice to the functions in FUNLIST so we can refresh the
1496stgit buffers as the git status of files change."
1497 (mapc (lambda (sym)
1498 (when (fboundp sym)
1499 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1500 (stgit-update-stgit-for-buffer t)))
1501 (ad-activate sym)))
1502 funlist))
1503
1504(defun stgit-advise ()
1505 "Add advice to appropriate (non-stgit) git functions so we can
1506refresh the stgit buffers as the git status of files change."
1507 (mapc (lambda (arg)
1508 (let ((feature (car arg))
1509 (funlist (cdr arg)))
1510 (if (featurep feature)
1511 (stgit-advise-funlist funlist)
1512 (add-to-list 'after-load-alist
1513 `(,feature (stgit-advise-funlist
1514 (quote ,funlist)))))))
e9a9f3fa 1515 ;; lists of (<feature> <function> <function> ...) to be advised
e44674e3 1516 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
e9a9f3fa
GH
1517 (git git-add-file git-checkout git-revert-file git-remove-file)
1518 (dired dired-delete-file))))
e44674e3 1519
455c9f7e
GH
1520(defvar stgit-pending-refresh-buffers nil
1521 "Alist of (cons `buffer' `refresh-index') of buffers that need
1522to be refreshed. `refresh-index' is non-nil if both work tree
1523and index need to be refreshed.")
1524
1525(defun stgit-run-pending-refreshs ()
1526 "Run all pending stgit buffer updates as posted by `stgit-post-refresh'."
1527 (let ((buffers stgit-pending-refresh-buffers)
1528 (stgit-inhibit-messages t))
1529 (setq stgit-pending-refresh-buffers nil)
1530 (while buffers
1531 (let* ((elem (car buffers))
1532 (buffer (car elem))
1533 (refresh-index (cdr elem)))
1534 (when (buffer-name buffer)
1535 (with-current-buffer buffer
1536 (stgit-refresh-worktree)
1537 (when refresh-index (stgit-refresh-index)))))
1538 (setq buffers (cdr buffers)))))
1539
1540(defun stgit-post-refresh (buffer refresh-index)
1541 "Update worktree status in BUFFER when Emacs becomes idle. If
1542REFRESH-INDEX is non-nil, also update the index."
1543 (unless stgit-pending-refresh-buffers
1544 (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs))
1545 (let ((elem (assq buffer stgit-pending-refresh-buffers)))
1546 (if elem
1547 ;; if buffer is already present, set its refresh-index flag if
1548 ;; necessary
1549 (when refresh-index
1550 (setcdr elem t))
1551 ;; new entry
1552 (setq stgit-pending-refresh-buffers
1553 (cons (cons buffer refresh-index)
1554 stgit-pending-refresh-buffers)))))
1555
e44674e3 1556(defun stgit-update-stgit-for-buffer (&optional refresh-index)
455c9f7e
GH
1557 "When Emacs becomes idle, refresh worktree status in any
1558`stgit-mode' buffer that shows the status of the current buffer.
e44674e3 1559
455c9f7e 1560If REFRESH-INDEX is non-nil, also update the index."
e9a9f3fa 1561 (let* ((dir (cond ((derived-mode-p 'stgit-status-mode 'dired-mode)
e44674e3
GH
1562 default-directory)
1563 (buffer-file-name
1564 (file-name-directory
1565 (expand-file-name buffer-file-name)))))
1566 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1567 (error nil))))
b894e680
DK
1568 (buffer (and gitdir (stgit-find-buffer gitdir))))
1569 (when buffer
455c9f7e 1570 (stgit-post-refresh buffer refresh-index))))
b894e680 1571
d51722b7
GH
1572(defun stgit-add-mark (patchsym)
1573 "Mark the patch PATCHSYM."
8036afdd 1574 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 1575
d51722b7
GH
1576(defun stgit-remove-mark (patchsym)
1577 "Unmark the patch PATCHSYM."
8036afdd 1578 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 1579
e6b1fdae 1580(defun stgit-clear-marks ()
47271f41 1581 "Unmark all patches."
e6b1fdae
DK
1582 (setq stgit-marked-patches '()))
1583
735cb7ec 1584(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
1585 (get-text-property (point) 'patch-data))
1586
bf7e391c 1587(defun stgit-patch-name-at-point (&optional cause-error types)
d51722b7 1588 "Return the patch name on the current line as a symbol.
64ada6f5 1589If CAUSE-ERROR is not nil, signal an error if none found.
bf7e391c
GH
1590
1591TYPES controls which types of commits and patches can be returned.
1592If it is t, only allow stgit patches; if 'allow-committed, also
1593allow historical commits; if nil, also allow work tree and index."
2c862b07 1594 (let ((patch (stgit-patch-at-point)))
64ada6f5 1595 (and patch
bf7e391c
GH
1596 (memq (stgit-patch->status patch)
1597 (case types
1598 ((nil) nil)
1599 ((allow-committed) '(work index))
1600 ((t) '(work index committed))
1601 (t (error "Bad value"))))
64ada6f5 1602 (setq patch nil))
2c862b07 1603 (cond (patch
413f9909 1604 (stgit-patch->name patch))
2c862b07
DK
1605 (cause-error
1606 (error "No patch on this line")))))
378a003d 1607
3164eec6
DK
1608(defun stgit-patched-file-at-point ()
1609 (get-text-property (point) 'file-data))
56d81fe5 1610
bf7e391c 1611(defun stgit-patches-marked-or-at-point (&optional cause-error types)
beac0f14
GH
1612 "Return the symbols of the marked patches, or the patch on the current line.
1613If CAUSE-ERRROR is not nil, signal an error if none found.
bf7e391c
GH
1614
1615TYPES controls which types of commits and patches can be returned.
1616If it is t, only allow stgit patches; if 'allow-committed, also
1617allow historical commits; if nil, also allow work tree and index."
7755d7f1 1618 (if stgit-marked-patches
d51722b7 1619 stgit-marked-patches
bf7e391c 1620 (let ((patch (stgit-patch-name-at-point nil types)))
beac0f14
GH
1621 (cond (patch (list patch))
1622 (cause-error (error "No patches marked or at this line"))
1623 (t nil)))))
7755d7f1 1624
a9089e68 1625(defun stgit-goto-patch (patchsym &optional file)
2570f6e5
GH
1626 "Move point to the line containing patch PATCHSYM and return non-nil.
1627If that patch cannot be found, do nothing and return nil.
a9089e68
GH
1628
1629If the patch was found and FILE is not nil, instead move to that
1630file's line. If FILE cannot be found, stay on the line of
1631PATCHSYM."
f9b82d36 1632 (let ((node (ewoc-nth stgit-ewoc 0)))
413f9909 1633 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
f9b82d36
DK
1634 patchsym)))
1635 (setq node (ewoc-next stgit-ewoc node)))
a9089e68 1636 (when (and node file)
413f9909 1637 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
a9089e68 1638 (file-node (ewoc-nth file-ewoc 0)))
ea696de9
GH
1639 (while (and file-node
1640 (not (equal (stgit-file->file (ewoc-data file-node))
1641 file)))
a9089e68
GH
1642 (setq file-node (ewoc-next file-ewoc file-node)))
1643 (when file-node
1644 (ewoc-goto-node file-ewoc file-node)
1645 (move-to-column (stgit-goal-column))
1646 (setq node nil))))
f9b82d36
DK
1647 (when node
1648 (ewoc-goto-node stgit-ewoc node)
d51722b7 1649 (move-to-column goal-column))))
56d81fe5 1650
1c2426dc 1651(defun stgit-init ()
a53347d9 1652 "Run stg init."
1c2426dc 1653 (interactive)
9d04c657 1654 (stgit-assert-mode)
1c2426dc 1655 (stgit-capture-output nil
b0424080 1656 (stgit-run "init"))
1f0bf00f 1657 (stgit-reload))
1c2426dc 1658
d11e0621
GH
1659(defun stgit-toggle-mark ()
1660 "Toggle mark on the patch under point."
1661 (interactive)
1662 (stgit-assert-mode)
1663 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1664 (stgit-unmark)
1665 (stgit-mark)))
1666
6df83d42 1667(defun stgit-mark ()
a53347d9 1668 "Mark the patch under point."
6df83d42 1669 (interactive)
9d04c657 1670 (stgit-assert-mode)
8036afdd 1671 (let* ((node (ewoc-locate stgit-ewoc))
bf7e391c
GH
1672 (patch (ewoc-data node)))
1673 (case (stgit-patch->status patch)
1674 (work (error "Cannot mark the work tree"))
1675 (index (error "Cannot mark the index"))
1676 (committed (error "Cannot mark a committed patch")))
413f9909 1677 (stgit-add-mark (stgit-patch->name patch))
d11e0621
GH
1678 (let ((column (current-column)))
1679 (ewoc-invalidate stgit-ewoc node)
1680 (move-to-column column))))
1681
1682(defun stgit-mark-down ()
1683 "Mark the patch under point and move to the next patch."
1684 (interactive)
1685 (stgit-mark)
378a003d 1686 (stgit-next-patch))
6df83d42 1687
d11e0621
GH
1688(defun stgit-unmark ()
1689 "Remove mark from the patch on the current line."
6df83d42 1690 (interactive)
9d04c657 1691 (stgit-assert-mode)
8036afdd
DK
1692 (let* ((node (ewoc-locate stgit-ewoc))
1693 (patch (ewoc-data node)))
413f9909 1694 (stgit-remove-mark (stgit-patch->name patch))
d11e0621
GH
1695 (let ((column (current-column)))
1696 (ewoc-invalidate stgit-ewoc node)
1697 (move-to-column column))))
1698
1699(defun stgit-unmark-up ()
1700 "Remove mark from the patch on the previous line."
1701 (interactive)
1702 (stgit-assert-mode)
1703 (stgit-previous-patch)
1704 (stgit-unmark))
9b151b27
GH
1705
1706(defun stgit-unmark-down ()
a53347d9 1707 "Remove mark from the patch on the current line."
9b151b27 1708 (interactive)
9d04c657 1709 (stgit-assert-mode)
d11e0621 1710 (stgit-unmark)
1288eda2 1711 (stgit-next-patch))
6df83d42 1712
56d81fe5 1713(defun stgit-rename (name)
018fa1ac 1714 "Rename the patch under point to NAME."
64ada6f5
GH
1715 (interactive (list
1716 (read-string "Patch name: "
1717 (symbol-name (stgit-patch-name-at-point t t)))))
9d04c657 1718 (stgit-assert-mode)
64ada6f5 1719 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 1720 (stgit-capture-output nil
69db9714 1721 (stgit-run "rename" "--" old-patchsym name))
d51722b7
GH
1722 (let ((name-sym (intern name)))
1723 (when (memq old-patchsym stgit-expanded-patches)
378a003d 1724 (setq stgit-expanded-patches
6a73154a 1725 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
d51722b7 1726 (when (memq old-patchsym stgit-marked-patches)
378a003d 1727 (setq stgit-marked-patches
6a73154a 1728 (cons name-sym (delq old-patchsym stgit-marked-patches))))
d51722b7
GH
1729 (stgit-reload)
1730 (stgit-goto-patch name-sym))))
56d81fe5 1731
408fa7cb
GH
1732(defun stgit-reload-or-repair (repair)
1733 "Update the contents of the StGit buffer (`stgit-reload').
1734
1735With a prefix argument, repair the StGit metadata if the branch
1736was modified with git commands (`stgit-repair')."
1737 (interactive "P")
9d04c657 1738 (stgit-assert-mode)
408fa7cb
GH
1739 (if repair
1740 (stgit-repair)
1741 (stgit-reload)))
1742
26201d96 1743(defun stgit-repair ()
a53347d9 1744 "Run stg repair."
26201d96 1745 (interactive)
9d04c657 1746 (stgit-assert-mode)
26201d96 1747 (stgit-capture-output nil
b0424080 1748 (stgit-run "repair"))
1f0bf00f 1749 (stgit-reload))
26201d96 1750
3e528fb0
GH
1751(defun stgit-available-branches (&optional all)
1752 "Returns a list of the names of the available stg branches as strings.
1753
1754If ALL is not nil, also return non-stgit branches."
adeef6bc
GH
1755 (let ((output (with-output-to-string
1756 (stgit-run "branch" "--list")))
3e528fb0
GH
1757 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1758 (if all ?. ?s)))
adeef6bc
GH
1759 (start 0)
1760 result)
3e528fb0 1761 (while (string-match pattern output start)
adeef6bc
GH
1762 (setq result (cons (match-string 1 output) result))
1763 (setq start (match-end 0)))
1764 result))
1765
1766(defun stgit-branch (branch)
3e528fb0 1767 "Switch to or create branch BRANCH."
adeef6bc
GH
1768 (interactive (list (completing-read "Switch to branch: "
1769 (stgit-available-branches))))
9d04c657 1770 (stgit-assert-mode)
3e528fb0
GH
1771 (when (cond ((equal branch (stgit-current-branch))
1772 (error "Branch is already current"))
1773 ((member branch (stgit-available-branches t))
1774 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1775 t)
1776 ((not (string-match stgit-allowed-branch-name-re branch))
1777 (error "Invalid branch name"))
1778 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
84e1850a
GH
1779 (let ((branch-point (completing-read
1780 "Branch from (default current branch): "
1781 (stgit-available-branches))))
1782 (stgit-capture-output nil
1783 (apply 'stgit-run
1784 `("branch" "--create" "--"
1785 ,branch
1786 ,@(unless (zerop (length branch-point))
1787 (list branch-point)))))
1788 t)))
3e528fb0 1789 (stgit-reload)))
adeef6bc 1790
380a021f
GH
1791(defun stgit-available-refs (&optional omit-stgit)
1792 "Returns a list of the available git refs.
1793If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1794 (let* ((output (with-output-to-string
1795 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1796 "refs/tags" "refs/heads"
1797 "refs/remotes")))
1798 (result (split-string output "\n" t)))
1799 (mapcar (lambda (s)
1800 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1801 (substring s (match-end 0))
1802 s))
1803 (if omit-stgit
1804 (delete-if (lambda (s)
1805 (string-match "^refs/heads/.*\\.stgit$" s))
1806 result)
1807 result))))
1808
d6e17ce0
GH
1809(defun stgit-parent-branch ()
1810 "Return the parent branch of the current stg branch as per
1811git-config setting branch.<branch>.stgit.parentbranch."
1812 (let ((output (with-output-to-string
1813 (stgit-run-git-silent "config"
1814 (format "branch.%s.stgit.parentbranch"
1815 (stgit-current-branch))))))
1816 (when (string-match ".*" output)
1817 (match-string 0 output))))
1818
380a021f 1819(defun stgit-rebase (new-base)
d6e17ce0
GH
1820 "Rebase the current branch to NEW-BASE.
1821
1822Interactively, first ask which branch to rebase to. Defaults to
1823what git-config branch.<branch>.stgit.parentbranch is set to."
380a021f 1824 (interactive (list (completing-read "Rebase to: "
d6e17ce0
GH
1825 (stgit-available-refs t)
1826 nil nil
1827 (stgit-parent-branch))))
9d04c657 1828 (stgit-assert-mode)
69db9714 1829 (stgit-capture-output nil (stgit-run "rebase" "--" new-base))
380a021f
GH
1830 (stgit-reload))
1831
41c1c59c
GH
1832(defun stgit-commit (count)
1833 "Run stg commit on COUNT commits.
e552cb5f
GH
1834Interactively, the prefix argument is used as COUNT.
1835A negative COUNT will uncommit instead."
41c1c59c 1836 (interactive "p")
9d04c657 1837 (stgit-assert-mode)
e552cb5f
GH
1838 (if (< count 0)
1839 (stgit-uncommit (- count))
1840 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1841 (stgit-reload)))
1842
1843(defun stgit-uncommit (count)
1844 "Run stg uncommit on COUNT commits.
1845Interactively, the prefix argument is used as COUNT.
1846A negative COUNT will commit instead."
1847 (interactive "p")
9d04c657 1848 (stgit-assert-mode)
e552cb5f
GH
1849 (if (< count 0)
1850 (stgit-commit (- count))
1851 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1852 (stgit-reload)))
c4aad9a7 1853
556345d3
GH
1854(defun stgit-neighbour-file ()
1855 "Return the file name of the next file after point, or the
1856previous file if point is at the last file within a patch."
1857 (let ((old-point (point))
1858 neighbour-file)
1859 (and (zerop (forward-line 1))
1860 (let ((f (stgit-patched-file-at-point)))
413f9909 1861 (and f (setq neighbour-file (stgit-file->file f)))))
556345d3
GH
1862 (goto-char old-point)
1863 (unless neighbour-file
1864 (and (zerop (forward-line -1))
1865 (let ((f (stgit-patched-file-at-point)))
413f9909 1866 (and f (setq neighbour-file (stgit-file->file f)))))
556345d3
GH
1867 (goto-char old-point))
1868 neighbour-file))
1869
3959a095
GH
1870(defun stgit-revert-file ()
1871 "Revert the file at point, which must be in the index or the
1872working tree."
1873 (interactive)
9d04c657 1874 (stgit-assert-mode)
3959a095
GH
1875 (let* ((patched-file (or (stgit-patched-file-at-point)
1876 (error "No file on the current line")))
1877 (patch-name (stgit-patch-name-at-point))
413f9909
GH
1878 (file-status (stgit-file->status patched-file))
1879 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1880 (stgit-file->cr-to patched-file))
3959a095 1881 ((eq file-status 'add)
413f9909 1882 (stgit-file->file patched-file))))
3959a095 1883 (co-file (cond ((eq file-status 'rename)
413f9909 1884 (stgit-file->cr-from patched-file))
3959a095 1885 ((not (memq file-status '(copy add)))
413f9909 1886 (stgit-file->file patched-file))))
556345d3 1887 (next-file (stgit-neighbour-file)))
3959a095
GH
1888
1889 (unless (memq patch-name '(:work :index))
1890 (error "No index or working tree file on this line"))
1891
d9473917
GH
1892 (when (eq file-status 'ignore)
1893 (error "Cannot revert ignored files"))
1894
1895 (when (eq file-status 'unknown)
1896 (error "Cannot revert unknown files"))
1897
3959a095
GH
1898 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1899 (when (yes-or-no-p (format "Revert %d file%s? "
1900 nfiles
1901 (if (= nfiles 1) "" "s")))
1902 (stgit-capture-output nil
1903 (when rm-file
1904 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1905 (when co-file
1906 (stgit-run-git "checkout" "HEAD" co-file)))
556345d3
GH
1907 (stgit-reload)
1908 (stgit-goto-patch patch-name next-file)))))
1629f59f
GH
1909
1910(defun stgit-revert ()
1911 "Revert the change at point, which must be the index, the work
1912tree, or a single change in either."
1913 (interactive)
9d04c657 1914 (stgit-assert-mode)
1629f59f
GH
1915 (let ((patched-file (stgit-patched-file-at-point)))
1916 (if patched-file
1917 (stgit-revert-file)
1918 (let* ((patch-name (or (stgit-patch-name-at-point)
1919 (error "No patch or file at point")))
1920 (patch-desc (case patch-name
1921 (:index "index")
1922 (:work "work tree")
1923 (t (error (substitute-command-keys
1924 "Use \\[stgit-delete] to delete a patch"))))))
1925 (when (if (eq patch-name :work)
1926 (stgit-work-tree-empty-p)
1927 (stgit-index-empty-p))
1928 (error (format "There are no changes in the %s to revert"
1929 patch-desc)))
1930 (and (eq patch-name :index)
1931 (not (stgit-work-tree-empty-p))
1932 (error "Cannot revert index as work tree contains unstaged changes"))
1933
1934 (when (yes-or-no-p (format "Revert all changes in the %s? "
1935 patch-desc))
1936 (if (eq patch-name :index)
1937 (stgit-run-git-silent "reset" "--hard" "-q")
1938 (stgit-run-git-silent "checkout" "--" "."))
1939 (stgit-refresh-index)
1940 (stgit-refresh-worktree)
1941 (stgit-goto-patch patch-name))))))
3959a095 1942
51783171
GH
1943(defun stgit-resolve-file ()
1944 "Resolve conflict in the file at point."
1945 (interactive)
9d04c657 1946 (stgit-assert-mode)
51783171
GH
1947 (let* ((patched-file (stgit-patched-file-at-point))
1948 (patch (stgit-patch-at-point))
413f9909
GH
1949 (patch-name (and patch (stgit-patch->name patch)))
1950 (status (and patched-file (stgit-file->status patched-file))))
51783171
GH
1951
1952 (unless (memq patch-name '(:work :index))
1953 (error "No index or working tree file on this line"))
1954
1955 (unless (eq status 'unmerged)
1956 (error "No conflict to resolve at the current line"))
1957
1958 (stgit-capture-output nil
413f9909 1959 (stgit-move-change-to-index (stgit-file->file patched-file)))
51783171
GH
1960
1961 (stgit-reload)))
1962
d47ee133
GH
1963(defun stgit-push-or-pop-patches (do-push npatches)
1964 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1965NPATCHES patches, or all patches if NPATCHES is t."
1966 (stgit-assert-mode)
1967 (stgit-capture-output nil
1968 (apply 'stgit-run
1969 (if do-push "push" "pop")
1970 (if (eq npatches t)
1971 '("--all")
1972 (list "-n" npatches))))
1973 (stgit-reload)
1974 (stgit-refresh-git-status))
1975
0b661144
DK
1976(defun stgit-push-next (npatches)
1977 "Push the first unapplied patch.
1978With numeric prefix argument, push that many patches."
1979 (interactive "p")
d47ee133 1980 (stgit-push-or-pop-patches t npatches))
56d81fe5 1981
0b661144
DK
1982(defun stgit-pop-next (npatches)
1983 "Pop the topmost applied patch.
d47ee133
GH
1984With numeric prefix argument, pop that many patches.
1985
1986If NPATCHES is t, pop all patches."
0b661144 1987 (interactive "p")
d47ee133 1988 (stgit-push-or-pop-patches nil npatches))
56d81fe5 1989
7c11b754
GH
1990(defun stgit-applied-patches (&optional only-patches)
1991 "Return a list of the applied patches.
1992
1993If ONLY-PATCHES is not nil, exclude index and work tree."
1994 (let ((states (if only-patches
1995 '(applied top)
1996 '(applied top index work)))
1997 result)
9aa61946
GH
1998 (ewoc-map (lambda (patch)
1999 (when (memq (stgit-patch->status patch) states)
2000 (setq result (cons patch result)))
2001 nil)
7c11b754
GH
2002 stgit-ewoc)
2003 result))
2004
2005(defun stgit-applied-patchsyms (&optional only-patches)
2006 "Return a list of the symbols of the applied patches.
2007
2008If ONLY-PATCHES is not nil, exclude index and work tree."
413f9909 2009 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
f9182fca
KH
2010
2011(defun stgit-push-or-pop ()
7c11b754 2012 "Push or pop the marked patches."
f9182fca 2013 (interactive)
9d04c657 2014 (stgit-assert-mode)
7c11b754
GH
2015 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
2016 (applied-syms (stgit-applied-patchsyms t))
2017 (unapplied (set-difference patchsyms applied-syms)))
f9182fca 2018 (stgit-capture-output nil
7c11b754
GH
2019 (apply 'stgit-run
2020 (if unapplied "push" "pop")
2021 "--"
2022 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
2023 (stgit-reload))
f9182fca 2024
006cd3e5
GH
2025(defun stgit-at-header-p ()
2026 "Return non-nil if point is in the header area above all patches."
2027 (not (previous-single-property-change (point) 'patch-data)))
2028
2029(defun stgit-at-footer-p ()
2030 "Return non-nil if point is in the footer area below all patches."
2031 (not (next-single-property-change (point) 'patch-data)))
2032
d47ee133 2033(defun stgit-goto-target ()
bf7e391c 2034 "Return the goto target at point: a patchsym, :top,
d47ee133 2035or :bottom."
bf7e391c
GH
2036 (let ((patch (stgit-patch-at-point)))
2037 (cond (patch
2038 (case (stgit-patch->status patch)
2039 ((work index) nil)
2040 ((committed) :bottom)
2041 (t (stgit-patch->name patch))))
006cd3e5 2042 ((stgit-at-footer-p)
d47ee133 2043 :top)
006cd3e5 2044 ((stgit-at-header-p)
d47ee133
GH
2045 :bottom))))
2046
c7adf5ef 2047(defun stgit-goto ()
48d0a850
GH
2048 "Go to the patch on the current line.
2049
d47ee133
GH
2050Push or pop patches to make this patch topmost. Push or pop all
2051patches if used on a line after or before all patches."
c7adf5ef 2052 (interactive)
9d04c657 2053 (stgit-assert-mode)
d47ee133
GH
2054 (let ((patchsym (stgit-goto-target)))
2055 (unless patchsym
2056 (error "No patch to go to on this line"))
2057 (case patchsym
2058 (:top (stgit-push-or-pop-patches t t))
2059 (:bottom (stgit-push-or-pop-patches nil t))
2060 (t (stgit-capture-output nil
69db9714 2061 (stgit-run "goto" "--" patchsym))
d47ee133 2062 (stgit-reload)))))
c7adf5ef 2063
d51722b7 2064(defun stgit-id (patchsym)
50d88c67
DK
2065 "Return the git commit id for PATCHSYM.
2066If PATCHSYM is a keyword, returns PATCHSYM unmodified."
2067 (if (keywordp patchsym)
2068 patchsym
2069 (let ((result (with-output-to-string
69db9714 2070 (stgit-run-silent "id" "--" patchsym))))
50d88c67
DK
2071 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
2072 (error "Cannot find commit id for %s" patchsym))
2073 (match-string 1 result))))
378a003d 2074
015a6dfa
KW
2075(defun stgit-whitespace-diff-arg (arg)
2076 (when (numberp arg)
2077 (cond ((> arg 4) "--ignore-all-space")
2078 ((> arg 1) "--ignore-space-change"))))
2079
1aece5c0 2080(defun stgit-show-patch (unmerged-stage ignore-whitespace)
d9b954c7
GH
2081 "Show the patch on the current line.
2082
2083UNMERGED-STAGE is the argument to `git-diff' that that selects
2084which stage to diff against in the case of unmerged files."
015a6dfa 2085 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
1aece5c0
GH
2086 (patch-name (stgit-patch-name-at-point t)))
2087 (stgit-capture-output "*StGit patch*"
2088 (case (get-text-property (point) 'entry-type)
2089 ('file
2090 (let* ((patched-file (stgit-patched-file-at-point))
2091 (patch-id (let ((id (stgit-id patch-name)))
2092 (if (and (eq id :index)
413f9909 2093 (eq (stgit-file->status patched-file)
1aece5c0
GH
2094 'unmerged))
2095 :work
2096 id)))
2097 (args (append (and space-arg (list space-arg))
413f9909 2098 (and (stgit-file->cr-from patched-file)
1aece5c0
GH
2099 (list (stgit-find-copies-harder-diff-arg)))
2100 (cond ((eq patch-id :index)
2101 '("--cached"))
2102 ((eq patch-id :work)
2103 (list unmerged-stage))
2104 (t
2105 (list (concat patch-id "^") patch-id)))
108e5c89
GH
2106 (and (eq (stgit-file->status patched-file) 'copy)
2107 '("--diff-filter=C"))
1aece5c0 2108 '("--")
413f9909
GH
2109 (if (stgit-file->copy-or-rename patched-file)
2110 (list (stgit-file->cr-from patched-file)
2111 (stgit-file->cr-to patched-file))
2112 (list (stgit-file->file patched-file))))))
1aece5c0
GH
2113 (apply 'stgit-run-git "diff" args)))
2114 ('patch
2115 (let* ((patch-id (stgit-id patch-name)))
2116 (if (or (eq patch-id :index) (eq patch-id :work))
2117 (apply 'stgit-run-git "diff"
2118 (stgit-find-copies-harder-diff-arg)
2119 (append (and space-arg (list space-arg))
2120 (if (eq patch-id :index)
2121 '("--cached")
2122 (list unmerged-stage))))
a27d0b75
GH
2123 (let ((args (append '("show" "-O" "--patch-with-stat")
2124 `("-O" ,(stgit-find-copies-harder-diff-arg))
1aece5c0 2125 (and space-arg (list "-O" space-arg))
69db9714 2126 '("--")
1aece5c0
GH
2127 (list (stgit-patch-name-at-point)))))
2128 (apply 'stgit-run args)))))
6a73154a
GH
2129 (t
2130 (error "No patch or file at point")))
1aece5c0
GH
2131 (with-current-buffer standard-output
2132 (goto-char (point-min))
2133 (diff-mode)))))
2134
2135(defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
2136 `(defun ,name (&optional ignore-whitespace)
2137 ,(format "Show the patch on the current line.
2138
2139%sWith a prefix argument, ignore whitespace. With a prefix argument
2140greater than four (e.g., \\[universal-argument] \
2141\\[universal-argument] \\[%s]), ignore all whitespace."
2142 (if unmerged-action
2143 (format "For unmerged files, %s.\n\n" unmerged-action)
2144 "")
2145 name)
2146 (interactive "p")
9d04c657 2147 (stgit-assert-mode)
1aece5c0
GH
2148 (stgit-show-patch ,diff-arg ignore-whitespace)))
2149
2150(stgit-define-diff stgit-diff
2151 "--ours" nil)
2152(stgit-define-diff stgit-diff-ours
2153 "--ours"
2154 "diff against our branch")
2155(stgit-define-diff stgit-diff-theirs
2156 "--theirs"
2157 "diff against their branch")
2158(stgit-define-diff stgit-diff-base
2159 "--base"
2160 "diff against the merge base")
2161(stgit-define-diff stgit-diff-combined
2162 "--cc"
2163 "show a combined diff")
d9b954c7 2164
e02b46e5
KW
2165(defun stgit-diff-range (&optional ignore-whitespace)
2166 "Show diff for the range of patches between point and the marked patch.
2167
2168With a prefix argument, ignore whitespace. With a prefix argument
2169greater than four (e.g., \\[universal-argument] \
2170\\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
2171 (interactive "p")
2172 (stgit-assert-mode)
2173 (unless (= (length stgit-marked-patches) 1)
2174 (error "Need exactly one patch marked"))
bf7e391c
GH
2175 (let* ((patches (stgit-sort-patches
2176 (cons (stgit-patch-name-at-point t 'allow-committed)
2177 stgit-marked-patches)
2178 t))
e02b46e5
KW
2179 (first-patch (car patches))
2180 (second-patch (if (cdr patches) (cadr patches) first-patch))
2181 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
2182 (applied (stgit-applied-patchsyms t)))
2183 (unless (and (memq first-patch applied) (memq second-patch applied))
2184 (error "Can only show diff range for applied patches"))
2185 (stgit-capture-output (format "*StGit diff %s..%s*"
2186 first-patch second-patch)
de8efe1e
GH
2187 (apply 'stgit-run-git
2188 "diff" "--patch-with-stat"
2189 (stgit-find-copies-harder-diff-arg)
2190 (append (and whitespace-arg (list whitespace-arg))
2191 (list (format "%s^" (stgit-id first-patch))
2192 (stgit-id second-patch))))
e02b46e5
KW
2193 (with-current-buffer standard-output
2194 (goto-char (point-min))
2195 (diff-mode)))))
2196
f87c2e22
GH
2197(defun stgit-move-change-to-index (file &optional force)
2198 "Copies the work tree state of FILE to index, using git add or git rm.
2199
2200If FORCE is not nil, use --force."
306b37a6
GH
2201 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
2202 '("add") '("rm" "-q"))))
37cb5766 2203 (stgit-capture-output "*git output*"
f87c2e22
GH
2204 (apply 'stgit-run-git (append op (and force '("--force"))
2205 '("--") (list file))))))
37cb5766 2206
fd9fe574 2207(defun stgit-remove-change-from-index (file)
37cb5766
DK
2208 "Unstages the change in FILE from the index"
2209 (stgit-capture-output "*git output*"
2210 (stgit-run-git "reset" "-q" "--" file)))
2211
dde3ab4d
GH
2212(defun stgit-git-index-unmerged-p ()
2213 (let (result)
2214 (with-output-to-string
2215 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
2216 "--diff-filter=U"
2217 "--quiet" "HEAD")))))
2218 result))
2219
37cb5766 2220(defun stgit-file-toggle-index ()
a9089e68
GH
2221 "Move modified file in or out of the index.
2222
2223Leaves the point where it is, but moves the mark to where the
2224file ended up. You can then jump to the file with \
2225\\[exchange-point-and-mark]."
37cb5766 2226 (interactive)
9d04c657 2227 (stgit-assert-mode)
612f999a
GH
2228 (let* ((patched-file (or (stgit-patched-file-at-point)
2229 (error "No file on the current line")))
413f9909 2230 (patched-status (stgit-file->status patched-file)))
612f999a 2231 (when (eq patched-status 'unmerged)
51783171 2232 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
a9089e68 2233 (let* ((patch (stgit-patch-at-point))
413f9909 2234 (patch-name (stgit-patch->name patch))
612f999a 2235 (mark-file (if (eq patched-status 'rename)
413f9909
GH
2236 (stgit-file->cr-to patched-file)
2237 (stgit-file->file patched-file)))
612f999a 2238 (point-file (if (eq patched-status 'rename)
413f9909 2239 (stgit-file->cr-from patched-file)
6a73154a 2240 (stgit-neighbour-file))))
a9089e68 2241
37cb5766 2242 (cond ((eq patch-name :work)
413f9909 2243 (stgit-move-change-to-index (stgit-file->file patched-file)
f87c2e22 2244 (eq patched-status 'ignore)))
37cb5766 2245 ((eq patch-name :index)
413f9909 2246 (stgit-remove-change-from-index (stgit-file->file patched-file)))
37cb5766 2247 (t
612f999a 2248 (error "Can only move files between working tree and index")))
a9089e68
GH
2249 (stgit-refresh-worktree)
2250 (stgit-refresh-index)
612f999a 2251 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
a9089e68 2252 (push-mark nil t t)
612f999a 2253 (stgit-goto-patch patch-name point-file))))
37cb5766 2254
dde3ab4d
GH
2255(defun stgit-toggle-index ()
2256 "Move change in or out of the index.
2257
2258Works on index and work tree, as well as files in either.
2259
2260Leaves the point where it is, but moves the mark to where the
2261file ended up. You can then jump to the file with \
2262\\[exchange-point-and-mark]."
2263 (interactive)
9d04c657 2264 (stgit-assert-mode)
dde3ab4d
GH
2265 (if (stgit-patched-file-at-point)
2266 (stgit-file-toggle-index)
2267 (let ((patch-name (stgit-patch-name-at-point)))
2268 (unless (memq patch-name '(:index :work))
2269 (error "Can only move changes between working tree and index"))
2270 (when (stgit-git-index-unmerged-p)
2271 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
2272 (if (if (eq patch-name :index)
2273 (stgit-index-empty-p)
2274 (stgit-work-tree-empty-p))
2275 (message "No changes to be moved")
2276 (stgit-capture-output nil
2277 (if (eq patch-name :work)
2278 (stgit-run-git "add" "--update")
2279 (stgit-run-git "reset" "--mixed" "-q")))
2280 (stgit-refresh-worktree)
2281 (stgit-refresh-index))
2282 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
2283
0bca35c8 2284(defun stgit-edit ()
a53347d9 2285 "Edit the patch on the current line."
0bca35c8 2286 (interactive)
9d04c657 2287 (stgit-assert-mode)
64ada6f5 2288 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 2289 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
2290 (dir default-directory))
2291 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 2292 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
2293 (setq default-directory dir)
2294 (let ((standard-output edit-buf))
655a3977 2295 (save-excursion
69db9714 2296 (stgit-run-silent "edit" "--save-template=-" "--" patchsym)))))
0bca35c8
DK
2297
2298(defun stgit-confirm-edit ()
2299 (interactive)
2300 (let ((file (make-temp-file "stgit-edit-")))
2301 (write-region (point-min) (point-max) file)
2302 (stgit-capture-output nil
69db9714 2303 (stgit-run "edit" "-f" file "--" stgit-edit-patchsym))
0bca35c8 2304 (with-current-buffer log-edit-parent-buffer
1f0bf00f 2305 (stgit-reload))))
0bca35c8 2306
2acb7116 2307(defun stgit-new (add-sign &optional refresh)
aa04f831
GH
2308 "Create a new patch.
2309With a prefix argument, include a \"Signed-off-by:\" line at the
2310end of the patch."
2311 (interactive "P")
9d04c657 2312 (stgit-assert-mode)
c5d45b92
GH
2313 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2314 (dir default-directory))
2315 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831 2316 (setq default-directory dir)
2acb7116 2317 (set (make-local-variable 'stgit-refresh-after-new) refresh)
aa04f831
GH
2318 (when add-sign
2319 (save-excursion
2320 (let ((standard-output (current-buffer)))
2321 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
2322
2323(defun stgit-confirm-new ()
2324 (interactive)
2acb7116
DK
2325 (let ((file (make-temp-file "stgit-edit-"))
2326 (refresh stgit-refresh-after-new))
64c097a0
DK
2327 (write-region (point-min) (point-max) file)
2328 (stgit-capture-output nil
27b0f9e4 2329 (stgit-run "new" "-f" file))
64c097a0 2330 (with-current-buffer log-edit-parent-buffer
2acb7116
DK
2331 (if refresh
2332 (stgit-refresh)
2333 (stgit-reload)))))
2334
2335(defun stgit-new-and-refresh (add-sign)
2336 "Create a new patch and refresh it with the current changes.
2337
2338With a prefix argument, include a \"Signed-off-by:\" line at the
2339end of the patch.
2340
2341This works just like running `stgit-new' followed by `stgit-refresh'."
2342 (interactive "P")
9d04c657 2343 (stgit-assert-mode)
2acb7116 2344 (stgit-new add-sign t))
64c097a0
DK
2345
2346(defun stgit-create-patch-name (description)
2347 "Create a patch name from a long description"
2348 (let ((patch ""))
2349 (while (> (length description) 0)
2350 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
2351 (setq patch (downcase (concat patch
2352 (match-string 0 description))))
64c097a0
DK
2353 (setq description (substring description (match-end 0))))
2354 ((string-match "\\` +" description)
2355 (setq patch (concat patch "-"))
2356 (setq description (substring description (match-end 0))))
2357 ((string-match "\\`[^a-zA-Z_-]+" description)
2358 (setq description (substring description (match-end 0))))))
2359 (cond ((= (length patch) 0)
2360 "patch")
2361 ((> (length patch) 20)
2362 (substring patch 0 20))
2363 (t patch))))
0bca35c8 2364
9008e45b 2365(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 2366 "Delete the patches in PATCHSYMS.
9008e45b
GH
2367Interactively, delete the marked patches, or the patch at point.
2368
2369With a prefix argument, or SPILL-P, spill the patch contents to
2370the work tree and index."
beac0f14 2371 (interactive (list (stgit-patches-marked-or-at-point t t)
9008e45b 2372 current-prefix-arg))
9d04c657 2373 (stgit-assert-mode)
e7231e4f
GH
2374 (unless patchsyms
2375 (error "No patches to delete"))
64ada6f5
GH
2376 (when (memq :index patchsyms)
2377 (error "Cannot delete the index"))
2378 (when (memq :work patchsyms)
2379 (error "Cannot delete the work tree"))
2380
d51722b7 2381 (let ((npatches (length patchsyms)))
9008e45b 2382 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 2383 npatches
9008e45b
GH
2384 (if (= 1 npatches) "" "es")
2385 (if spill-p
2386 " (spilling contents to index)"
2387 "")))
69db9714
GH
2388 (let ((args (append (when spill-p '("--spill"))
2389 '("--")
2390 patchsyms)))
9008e45b
GH
2391 (stgit-capture-output nil
2392 (apply 'stgit-run "delete" args))
2393 (stgit-reload)))))
d51722b7 2394
7cc45294
GH
2395(defun stgit-move-patches-target ()
2396 "Return the patchsym indicating a target patch for
2397`stgit-move-patches'.
2398
2547179e
GH
2399This is either the first unmarked patch at or after point, or one
2400of :top and :bottom if the point is after or before the applied
2401patches."
2402
2403 (save-excursion
2404 (let (result)
2405 (while (not result)
2406 (let ((patchsym (stgit-patch-name-at-point)))
2407 (cond ((memq patchsym '(:work :index)) (setq result :top))
2408 (patchsym (if (memq patchsym stgit-marked-patches)
2409 (stgit-next-patch)
2410 (setq result patchsym)))
2411 ((re-search-backward "^>" nil t) (setq result :top))
2412 (t (setq result :bottom)))))
2413 result)))
7cc45294 2414
c1412832 2415(defun stgit-sort-patches (patchsyms &optional allow-duplicates)
95369f6c
GH
2416 "Returns the list of patches in PATCHSYMS sorted according to
2417their position in the patch series, bottommost first.
2418
c1412832
KW
2419PATCHSYMS must not contain duplicate entries, unless
2420ALLOW-DUPLICATES is not nil."
b4f12bc1
GH
2421 (let (sorted-patchsyms)
2422 (ewoc-map #'(lambda (patch)
2423 (let ((name (stgit-patch->name patch)))
2424 (when (memq name patchsyms)
2425 (setq sorted-patchsyms (cons name sorted-patchsyms))))
2426 nil)
2427 stgit-ewoc)
95369f6c
GH
2428 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2429
c1412832
KW
2430 (unless allow-duplicates
2431 (unless (= (length patchsyms) (length sorted-patchsyms))
2432 (error "Internal error")))
95369f6c
GH
2433
2434 sorted-patchsyms))
2435
7cc45294
GH
2436(defun stgit-move-patches (patchsyms target-patch)
2437 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2438If TARGET-PATCH is :bottom or :top, move the patches to the
2439bottom or top of the stack, respectively.
2440
2441Interactively, move the marked patches to where the point is."
2442 (interactive (list stgit-marked-patches
2443 (stgit-move-patches-target)))
9d04c657 2444 (stgit-assert-mode)
7cc45294
GH
2445 (unless patchsyms
2446 (error "Need at least one patch to move"))
2447
2448 (unless target-patch
2449 (error "Point not at a patch"))
2450
2547179e
GH
2451 ;; need to have patchsyms sorted by position in the stack
2452 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2453 (stgit-capture-output nil
2454 (if (eq target-patch :top)
69db9714 2455 (apply 'stgit-run "float" "--" sorted-patchsyms)
2547179e
GH
2456 (apply 'stgit-run
2457 "sink"
2458 (append (unless (eq target-patch :bottom)
2459 (list "--to" target-patch))
2460 '("--")
2461 sorted-patchsyms)))))
7cc45294
GH
2462 (stgit-reload))
2463
594aa463
KH
2464(defun stgit-squash (patchsyms)
2465 "Squash the patches in PATCHSYMS.
693d179b
GH
2466Interactively, squash the marked patches.
2467
2468Unless there are any conflicts, the patches will be merged into
2469one patch, which will occupy the same spot in the series as the
2470deepest patch had before the squash."
d51722b7 2471 (interactive (list stgit-marked-patches))
9d04c657 2472 (stgit-assert-mode)
d51722b7 2473 (when (< (length patchsyms) 2)
594aa463 2474 (error "Need at least two patches to squash"))
32d7545d
GH
2475 (let ((stgit-buffer (current-buffer))
2476 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
2477 (dir default-directory)
2478 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 2479 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 2480 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 2481 (setq default-directory dir)
32d7545d 2482 (let ((result (let ((standard-output edit-buf))
655a3977
GH
2483 (save-excursion
2484 (apply 'stgit-run-silent "squash"
69db9714 2485 "--save-template=-" "--" sorted-patchsyms)))))
32d7545d
GH
2486
2487 ;; stg squash may have reordered the patches or caused conflicts
2488 (with-current-buffer stgit-buffer
2489 (stgit-reload))
2490
2491 (unless (eq 0 result)
2492 (fundamental-mode)
2493 (rename-buffer "*StGit error*")
2494 (resize-temp-buffer-window)
2495 (switch-to-buffer-other-window stgit-buffer)
2496 (error "stg squash failed")))))
ea0def18 2497
594aa463 2498(defun stgit-confirm-squash ()
ea0def18
DK
2499 (interactive)
2500 (let ((file (make-temp-file "stgit-edit-")))
2501 (write-region (point-min) (point-max) file)
2502 (stgit-capture-output nil
69db9714 2503 (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms))
ea0def18 2504 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
2505 (stgit-clear-marks)
2506 ;; Go to first marked patch and stay there
2507 (goto-char (point-min))
2508 (re-search-forward (concat "^[>+-]\\*") nil t)
2509 (move-to-column goal-column)
2510 (let ((pos (point)))
1f0bf00f 2511 (stgit-reload)
e6b1fdae 2512 (goto-char pos)))))
ea0def18 2513
0663524d
KH
2514(defun stgit-help ()
2515 "Display help for the StGit mode."
2516 (interactive)
2517 (describe-function 'stgit-mode))
3a59f3db 2518
e8faa44b
DK
2519(defun stgit-execute-process-sentinel (process sentinel)
2520 (let (old-sentinel stgit-buf)
2521 (with-current-buffer (process-buffer process)
2522 (setq old-sentinel old-process-sentinel
2523 stgit-buf stgit-buffer))
2524 (and (memq (process-status process) '(exit signal))
2525 (buffer-live-p stgit-buf)
2526 (with-current-buffer stgit-buf
2527 (stgit-reload)))
2528 (funcall old-sentinel process sentinel)))
2529
a4a01630
GH
2530(defun stgit-execute-process-filter (process output)
2531 (with-current-buffer (process-buffer process)
2532 (let* ((old-point (point))
2533 (pmark (process-mark process))
2534 (insert-at (marker-position pmark))
2535 (at-pmark (= insert-at old-point)))
2536 (goto-char insert-at)
2537 (insert-before-markers output)
2538 (comint-carriage-motion insert-at (point))
2539 (set-marker pmark (point))
2540 (unless at-pmark
2541 (goto-char old-point)))))
2542
15d774ab 2543(defun stgit-execute (&optional git-mode)
e8faa44b
DK
2544 "Prompt for an stg command to execute in a shell.
2545
2546The names of any marked patches or the patch at point are
2547inserted in the command to be executed.
2548
15d774ab
GH
2549With a prefix argument, or if GIT-MODE is non-nil, insert SHA1
2550sums of the marked patches instead, and prompt for a git command.
2551
e8faa44b
DK
2552If the command ends in an ampersand, run it asynchronously.
2553
2554When the command has finished, reload the stgit buffer."
15d774ab 2555 (interactive "P")
e8faa44b 2556 (stgit-assert-mode)
8f489f41
GH
2557 (let* ((patches (stgit-sort-patches
2558 (stgit-patches-marked-or-at-point nil 'allow-committed)))
e8faa44b
DK
2559 (patch-names (mapcar 'symbol-name patches))
2560 (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names))
2561 (defaultcmd (if patches
15d774ab 2562 (concat (if git-mode "git" "stg") " "
e8faa44b 2563 (and hyphens "-- ")
15d774ab
GH
2564 (mapconcat (if git-mode 'stgit-id 'identity)
2565 patch-names " "))
e8faa44b
DK
2566 "stg "))
2567 (cmd (read-from-minibuffer "Shell command: " (cons defaultcmd 5)
2568 nil nil 'shell-command-history))
2569 (async (string-match "&[ \t]*\\'" cmd))
2570 (buffer (get-buffer-create
2571 (if async
2572 "*Async Shell Command*"
2573 "*Shell Command Output*"))))
2574 ;; cannot use minibuffer as stgit-reload would overwrite it; if we
2575 ;; show the buffer, shell-command will not use the minibuffer
2576 (display-buffer buffer)
2577 (shell-command cmd)
2578 (if async
2579 (let ((old-buffer (current-buffer)))
2580 (with-current-buffer buffer
2581 (let ((process (get-buffer-process buffer)))
2582 (set (make-local-variable 'old-process-sentinel)
2583 (process-sentinel process))
2584 (set (make-local-variable 'stgit-buffer)
2585 old-buffer)
a4a01630 2586 (set-process-filter process 'stgit-execute-process-filter)
e8faa44b 2587 (set-process-sentinel process 'stgit-execute-process-sentinel))))
a4a01630
GH
2588 (with-current-buffer buffer
2589 (comint-carriage-motion (point-min) (point-max)))
e8faa44b
DK
2590 (shrink-window-if-larger-than-buffer (get-buffer-window buffer))
2591 (stgit-reload))))
2592
6c2d4962
GH
2593(defun stgit-undo-or-redo (redo hard)
2594 "Run stg undo or, if REDO is non-nil, stg redo.
2595
2596If HARD is non-nil, use the --hard flag."
2597 (stgit-assert-mode)
2598 (let ((cmd (if redo "redo" "undo")))
2599 (stgit-capture-output nil
2600 (if arg
2601 (when (or (and (stgit-index-empty-p)
2602 (stgit-work-tree-empty-p))
2603 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2604 cmd)))
2605 (stgit-run cmd "--hard"))
2606 (stgit-run cmd))))
2607 (stgit-reload))
2608
83e51dbf
DK
2609(defun stgit-undo (&optional arg)
2610 "Run stg undo.
b8463f1d
GH
2611With prefix argument, run it with the --hard flag.
2612
2613See also `stgit-redo'."
83e51dbf 2614 (interactive "P")
6c2d4962 2615 (stgit-undo-or-redo nil arg))
83e51dbf 2616
b8463f1d
GH
2617(defun stgit-redo (&optional arg)
2618 "Run stg redo.
2619With prefix argument, run it with the --hard flag.
2620
2621See also `stgit-undo'."
2622 (interactive "P")
6c2d4962 2623 (stgit-undo-or-redo t arg))
b8463f1d 2624
4d73c4d8
DK
2625(defun stgit-refresh (&optional arg)
2626 "Run stg refresh.
36a4eacd
GH
2627If the index contains any changes, only refresh from index.
2628
a53347d9 2629With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8 2630 (interactive "P")
9d04c657 2631 (stgit-assert-mode)
4d73c4d8 2632 (let ((patchargs (if arg
b51a910f 2633 (let ((patches (stgit-patches-marked-or-at-point t t)))
beac0f14
GH
2634 (when (> (length patches) 1)
2635 (error "Too many patches marked"))
2636 (cons "-p" patches))
b0424080 2637 nil)))
36a4eacd
GH
2638 (unless (stgit-index-empty-p)
2639 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 2640 (stgit-capture-output nil
074a4fb0
GH
2641 (apply 'stgit-run "refresh" patchargs))
2642 (stgit-refresh-git-status))
4d73c4d8
DK
2643 (stgit-reload))
2644
ce3b6130 2645(defvar stgit-show-worktree nil
8f702de4 2646 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 2647
8f702de4 2648See also `stgit-show-worktree-mode'.")
ce3b6130 2649
d9473917
GH
2650(defvar stgit-show-ignored nil
2651 "If nil, inhibit showing files ignored by git.")
2652
2653(defvar stgit-show-unknown nil
2654 "If nil, inhibit showing files not registered with git.")
2655
a0045b87
DK
2656(defvar stgit-show-patch-names t
2657 "If nil, inhibit showing patch names.")
2658
bf7e391c
GH
2659(defvar stgit-show-committed nil
2660 "If nil, inhibit showing recent commits.")
2661
137b315b
GH
2662(defvar stgit-show-svn nil
2663 "If nil, inhibit showing git svn information.")
2664
bf7e391c
GH
2665(defvar stgit-committed-count nil
2666 "The number of recent commits to show.")
2667
f9533abf 2668(defmacro stgit-define-toggle-view (sym desc help)
8a619f55
GH
2669 (declare (indent 1))
2670 (let* ((name (symbol-name sym))
2671 (fun (intern (concat "stgit-toggle-" name)))
2672 (flag (intern (concat "stgit-show-" name))))
2673 ;; make help-follow find the correct function
2674 `(put (quote ,fun) 'definition-name 'stgit-define-toggle-view)
2675 `(defun ,fun (&optional arg)
2676 ,help
2677 (interactive "P")
2678 (stgit-assert-mode)
2679 (setq ,flag (if arg
2680 (> (prefix-numeric-value arg) 0)
2681 (not ,flag)))
f9533abf 2682 (stgit-reload (format "%s %s" (if ,flag "Showing" "Hiding") ,desc)))))
8a619f55
GH
2683
2684(stgit-define-toggle-view worktree
f9533abf 2685 "work tree and index"
ce3b6130 2686 "Toggle the visibility of the work tree.
2d7bcbd9 2687With ARG, show the work tree if ARG is positive.
ce3b6130 2688
8f702de4
GH
2689Its initial setting is controlled by `stgit-default-show-worktree'.
2690
2691`stgit-show-worktree-mode' controls where on screen the index and
8a619f55 2692work tree will show up.")
ce3b6130 2693
8a619f55 2694(stgit-define-toggle-view ignored
f9533abf 2695 "ignored files"
d9473917
GH
2696 "Toggle the visibility of files ignored by git in the work
2697tree. With ARG, show these files if ARG is positive.
2698
bc11fb08
GH
2699Its initial setting is controlled by `stgit-default-show-ignored'.
2700
8a619f55 2701Use \\[stgit-toggle-worktree] to show the work tree.")
d9473917 2702
8a619f55 2703(stgit-define-toggle-view unknown
f9533abf 2704 "unknown files"
d9473917
GH
2705 "Toggle the visibility of files not registered with git in the
2706work tree. With ARG, show these files if ARG is positive.
2707
bc11fb08
GH
2708Its initial setting is controlled by `stgit-default-show-unknown'.
2709
8a619f55 2710Use \\[stgit-toggle-worktree] to show the work tree.")
d9473917 2711
8a619f55 2712(stgit-define-toggle-view patch-names
f9533abf 2713 "patch names"
a0045b87
DK
2714 "Toggle the visibility of patch names. With ARG, show patch names
2715if ARG is positive.
2716
8a619f55 2717The initial setting is controlled by `stgit-default-show-patch-names'.")
a0045b87 2718
137b315b 2719(stgit-define-toggle-view svn
f9533abf 2720 "subversion revisions"
137b315b
GH
2721 "Toggle showing subversion information from git svn. With ARG,
2722show svn information if ARG is positive.
2723
2724The initial setting is controlled by `stgit-default-show-svn'.")
2725
bf7e391c
GH
2726(defun stgit-toggle-committed (&optional arg)
2727 "Toggle the visibility of historical git commits.
2728With ARG, set the number of commits to show to ARG, and disable
2729them if ARG is zero.
2730
2731The initial setting is controlled by `stgit-default-show-committed'."
2732 (interactive "P")
2733 (stgit-assert-mode)
2734 (if (null arg)
2735 (setq stgit-show-committed (not stgit-show-committed))
2736 (let ((n (prefix-numeric-value arg)))
2737 (setq stgit-show-committed (> n 0))
2738 (setq stgit-committed-count n)))
f9533abf
GH
2739 (stgit-reload (format "%s historical commits"
2740 (if (and stgit-show-committed
2741 (> stgit-committed-count 0))
2742 "Showing"
2743 "Hiding"))))
bf7e391c 2744
3a59f3db 2745(provide 'stgit)