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