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