stgit.el: Get default rebase branch from git-config in stg-rebase
[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-parent-branch ()
1458 "Return the parent branch of the current stg branch as per
1459 git-config setting branch.<branch>.stgit.parentbranch."
1460 (let ((output (with-output-to-string
1461 (stgit-run-git-silent "config"
1462 (format "branch.%s.stgit.parentbranch"
1463 (stgit-current-branch))))))
1464 (when (string-match ".*" output)
1465 (match-string 0 output))))
1466
1467 (defun stgit-rebase (new-base)
1468 "Rebase the current branch to NEW-BASE.
1469
1470 Interactively, first ask which branch to rebase to. Defaults to
1471 what git-config branch.<branch>.stgit.parentbranch is set to."
1472 (interactive (list (completing-read "Rebase to: "
1473 (stgit-available-refs t)
1474 nil nil
1475 (stgit-parent-branch))))
1476 (stgit-assert-mode)
1477 (stgit-capture-output nil (stgit-run "rebase" new-base))
1478 (stgit-reload))
1479
1480 (defun stgit-commit (count)
1481 "Run stg commit on COUNT commits.
1482 Interactively, the prefix argument is used as COUNT.
1483 A negative COUNT will uncommit instead."
1484 (interactive "p")
1485 (stgit-assert-mode)
1486 (if (< count 0)
1487 (stgit-uncommit (- count))
1488 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1489 (stgit-reload)))
1490
1491 (defun stgit-uncommit (count)
1492 "Run stg uncommit on COUNT commits.
1493 Interactively, the prefix argument is used as COUNT.
1494 A negative COUNT will commit instead."
1495 (interactive "p")
1496 (stgit-assert-mode)
1497 (if (< count 0)
1498 (stgit-commit (- count))
1499 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1500 (stgit-reload)))
1501
1502 (defun stgit-neighbour-file ()
1503 "Return the file name of the next file after point, or the
1504 previous file if point is at the last file within a patch."
1505 (let ((old-point (point))
1506 neighbour-file)
1507 (and (zerop (forward-line 1))
1508 (let ((f (stgit-patched-file-at-point)))
1509 (and f (setq neighbour-file (stgit-file->file f)))))
1510 (goto-char old-point)
1511 (unless neighbour-file
1512 (and (zerop (forward-line -1))
1513 (let ((f (stgit-patched-file-at-point)))
1514 (and f (setq neighbour-file (stgit-file->file f)))))
1515 (goto-char old-point))
1516 neighbour-file))
1517
1518 (defun stgit-revert-file ()
1519 "Revert the file at point, which must be in the index or the
1520 working tree."
1521 (interactive)
1522 (stgit-assert-mode)
1523 (let* ((patched-file (or (stgit-patched-file-at-point)
1524 (error "No file on the current line")))
1525 (patch-name (stgit-patch-name-at-point))
1526 (file-status (stgit-file->status patched-file))
1527 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1528 (stgit-file->cr-to patched-file))
1529 ((eq file-status 'add)
1530 (stgit-file->file patched-file))))
1531 (co-file (cond ((eq file-status 'rename)
1532 (stgit-file->cr-from patched-file))
1533 ((not (memq file-status '(copy add)))
1534 (stgit-file->file patched-file))))
1535 (next-file (stgit-neighbour-file)))
1536
1537 (unless (memq patch-name '(:work :index))
1538 (error "No index or working tree file on this line"))
1539
1540 (when (eq file-status 'ignore)
1541 (error "Cannot revert ignored files"))
1542
1543 (when (eq file-status 'unknown)
1544 (error "Cannot revert unknown files"))
1545
1546 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1547 (when (yes-or-no-p (format "Revert %d file%s? "
1548 nfiles
1549 (if (= nfiles 1) "" "s")))
1550 (stgit-capture-output nil
1551 (when rm-file
1552 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1553 (when co-file
1554 (stgit-run-git "checkout" "HEAD" co-file)))
1555 (stgit-reload)
1556 (stgit-goto-patch patch-name next-file)))))
1557
1558 (defun stgit-revert ()
1559 "Revert the change at point, which must be the index, the work
1560 tree, or a single change in either."
1561 (interactive)
1562 (stgit-assert-mode)
1563 (let ((patched-file (stgit-patched-file-at-point)))
1564 (if patched-file
1565 (stgit-revert-file)
1566 (let* ((patch-name (or (stgit-patch-name-at-point)
1567 (error "No patch or file at point")))
1568 (patch-desc (case patch-name
1569 (:index "index")
1570 (:work "work tree")
1571 (t (error (substitute-command-keys
1572 "Use \\[stgit-delete] to delete a patch"))))))
1573 (when (if (eq patch-name :work)
1574 (stgit-work-tree-empty-p)
1575 (stgit-index-empty-p))
1576 (error (format "There are no changes in the %s to revert"
1577 patch-desc)))
1578 (and (eq patch-name :index)
1579 (not (stgit-work-tree-empty-p))
1580 (error "Cannot revert index as work tree contains unstaged changes"))
1581
1582 (when (yes-or-no-p (format "Revert all changes in the %s? "
1583 patch-desc))
1584 (if (eq patch-name :index)
1585 (stgit-run-git-silent "reset" "--hard" "-q")
1586 (stgit-run-git-silent "checkout" "--" "."))
1587 (stgit-refresh-index)
1588 (stgit-refresh-worktree)
1589 (stgit-goto-patch patch-name))))))
1590
1591 (defun stgit-resolve-file ()
1592 "Resolve conflict in the file at point."
1593 (interactive)
1594 (stgit-assert-mode)
1595 (let* ((patched-file (stgit-patched-file-at-point))
1596 (patch (stgit-patch-at-point))
1597 (patch-name (and patch (stgit-patch->name patch)))
1598 (status (and patched-file (stgit-file->status patched-file))))
1599
1600 (unless (memq patch-name '(:work :index))
1601 (error "No index or working tree file on this line"))
1602
1603 (unless (eq status 'unmerged)
1604 (error "No conflict to resolve at the current line"))
1605
1606 (stgit-capture-output nil
1607 (stgit-move-change-to-index (stgit-file->file patched-file)))
1608
1609 (stgit-reload)))
1610
1611 (defun stgit-push-next (npatches)
1612 "Push the first unapplied patch.
1613 With numeric prefix argument, push that many patches."
1614 (interactive "p")
1615 (stgit-assert-mode)
1616 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1617 (stgit-reload)
1618 (stgit-refresh-git-status))
1619
1620 (defun stgit-pop-next (npatches)
1621 "Pop the topmost applied patch.
1622 With numeric prefix argument, pop that many patches."
1623 (interactive "p")
1624 (stgit-assert-mode)
1625 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1626 (stgit-reload)
1627 (stgit-refresh-git-status))
1628
1629 (defun stgit-applied-patches (&optional only-patches)
1630 "Return a list of the applied patches.
1631
1632 If ONLY-PATCHES is not nil, exclude index and work tree."
1633 (let ((states (if only-patches
1634 '(applied top)
1635 '(applied top index work)))
1636 result)
1637 (ewoc-map (lambda (patch) (when (memq (stgit-patch->status patch) states)
1638 (setq result (cons patch result))))
1639 stgit-ewoc)
1640 result))
1641
1642 (defun stgit-applied-patchsyms (&optional only-patches)
1643 "Return a list of the symbols of the applied patches.
1644
1645 If ONLY-PATCHES is not nil, exclude index and work tree."
1646 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1647
1648 (defun stgit-push-or-pop ()
1649 "Push or pop the marked patches."
1650 (interactive)
1651 (stgit-assert-mode)
1652 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1653 (applied-syms (stgit-applied-patchsyms t))
1654 (unapplied (set-difference patchsyms applied-syms)))
1655 (stgit-capture-output nil
1656 (apply 'stgit-run
1657 (if unapplied "push" "pop")
1658 "--"
1659 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1660 (stgit-reload))
1661
1662 (defun stgit-goto ()
1663 "Go to the patch on the current line.
1664
1665 Pops or pushes patches to make this patch topmost."
1666 (interactive)
1667 (stgit-assert-mode)
1668 (let ((patchsym (stgit-patch-name-at-point t)))
1669 (stgit-capture-output nil
1670 (stgit-run "goto" patchsym))
1671 (stgit-reload)))
1672
1673 (defun stgit-id (patchsym)
1674 "Return the git commit id for PATCHSYM.
1675 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1676 (if (keywordp patchsym)
1677 patchsym
1678 (let ((result (with-output-to-string
1679 (stgit-run-silent "id" patchsym))))
1680 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1681 (error "Cannot find commit id for %s" patchsym))
1682 (match-string 1 result))))
1683
1684 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1685 "Show the patch on the current line.
1686
1687 UNMERGED-STAGE is the argument to `git-diff' that that selects
1688 which stage to diff against in the case of unmerged files."
1689 (let ((space-arg (when (numberp ignore-whitespace)
1690 (cond ((> ignore-whitespace 4)
1691 "--ignore-all-space")
1692 ((> ignore-whitespace 1)
1693 "--ignore-space-change"))))
1694 (patch-name (stgit-patch-name-at-point t)))
1695 (stgit-capture-output "*StGit patch*"
1696 (case (get-text-property (point) 'entry-type)
1697 ('file
1698 (let* ((patched-file (stgit-patched-file-at-point))
1699 (patch-id (let ((id (stgit-id patch-name)))
1700 (if (and (eq id :index)
1701 (eq (stgit-file->status patched-file)
1702 'unmerged))
1703 :work
1704 id)))
1705 (args (append (and space-arg (list space-arg))
1706 (and (stgit-file->cr-from patched-file)
1707 (list (stgit-find-copies-harder-diff-arg)))
1708 (cond ((eq patch-id :index)
1709 '("--cached"))
1710 ((eq patch-id :work)
1711 (list unmerged-stage))
1712 (t
1713 (list (concat patch-id "^") patch-id)))
1714 '("--")
1715 (if (stgit-file->copy-or-rename patched-file)
1716 (list (stgit-file->cr-from patched-file)
1717 (stgit-file->cr-to patched-file))
1718 (list (stgit-file->file patched-file))))))
1719 (apply 'stgit-run-git "diff" args)))
1720 ('patch
1721 (let* ((patch-id (stgit-id patch-name)))
1722 (if (or (eq patch-id :index) (eq patch-id :work))
1723 (apply 'stgit-run-git "diff"
1724 (stgit-find-copies-harder-diff-arg)
1725 (append (and space-arg (list space-arg))
1726 (if (eq patch-id :index)
1727 '("--cached")
1728 (list unmerged-stage))))
1729 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1730 (and space-arg (list "-O" space-arg))
1731 (list (stgit-patch-name-at-point)))))
1732 (apply 'stgit-run args)))))
1733 (t
1734 (error "No patch or file at point")))
1735 (with-current-buffer standard-output
1736 (goto-char (point-min))
1737 (diff-mode)))))
1738
1739 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1740 `(defun ,name (&optional ignore-whitespace)
1741 ,(format "Show the patch on the current line.
1742
1743 %sWith a prefix argument, ignore whitespace. With a prefix argument
1744 greater than four (e.g., \\[universal-argument] \
1745 \\[universal-argument] \\[%s]), ignore all whitespace."
1746 (if unmerged-action
1747 (format "For unmerged files, %s.\n\n" unmerged-action)
1748 "")
1749 name)
1750 (interactive "p")
1751 (stgit-assert-mode)
1752 (stgit-show-patch ,diff-arg ignore-whitespace)))
1753
1754 (stgit-define-diff stgit-diff
1755 "--ours" nil)
1756 (stgit-define-diff stgit-diff-ours
1757 "--ours"
1758 "diff against our branch")
1759 (stgit-define-diff stgit-diff-theirs
1760 "--theirs"
1761 "diff against their branch")
1762 (stgit-define-diff stgit-diff-base
1763 "--base"
1764 "diff against the merge base")
1765 (stgit-define-diff stgit-diff-combined
1766 "--cc"
1767 "show a combined diff")
1768
1769 (defun stgit-move-change-to-index (file &optional force)
1770 "Copies the work tree state of FILE to index, using git add or git rm.
1771
1772 If FORCE is not nil, use --force."
1773 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1774 '("add") '("rm" "-q"))))
1775 (stgit-capture-output "*git output*"
1776 (apply 'stgit-run-git (append op (and force '("--force"))
1777 '("--") (list file))))))
1778
1779 (defun stgit-remove-change-from-index (file)
1780 "Unstages the change in FILE from the index"
1781 (stgit-capture-output "*git output*"
1782 (stgit-run-git "reset" "-q" "--" file)))
1783
1784 (defun stgit-git-index-unmerged-p ()
1785 (let (result)
1786 (with-output-to-string
1787 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1788 "--diff-filter=U"
1789 "--quiet" "HEAD")))))
1790 result))
1791
1792 (defun stgit-file-toggle-index ()
1793 "Move modified file in or out of the index.
1794
1795 Leaves the point where it is, but moves the mark to where the
1796 file ended up. You can then jump to the file with \
1797 \\[exchange-point-and-mark]."
1798 (interactive)
1799 (stgit-assert-mode)
1800 (let* ((patched-file (or (stgit-patched-file-at-point)
1801 (error "No file on the current line")))
1802 (patched-status (stgit-file->status patched-file)))
1803 (when (eq patched-status 'unmerged)
1804 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1805 (let* ((patch (stgit-patch-at-point))
1806 (patch-name (stgit-patch->name patch))
1807 (mark-file (if (eq patched-status 'rename)
1808 (stgit-file->cr-to patched-file)
1809 (stgit-file->file patched-file)))
1810 (point-file (if (eq patched-status 'rename)
1811 (stgit-file->cr-from patched-file)
1812 (stgit-neighbour-file))))
1813
1814 (cond ((eq patch-name :work)
1815 (stgit-move-change-to-index (stgit-file->file patched-file)
1816 (eq patched-status 'ignore)))
1817 ((eq patch-name :index)
1818 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1819 (t
1820 (error "Can only move files between working tree and index")))
1821 (stgit-refresh-worktree)
1822 (stgit-refresh-index)
1823 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1824 (push-mark nil t t)
1825 (stgit-goto-patch patch-name point-file))))
1826
1827 (defun stgit-toggle-index ()
1828 "Move change in or out of the index.
1829
1830 Works on index and work tree, as well as files in either.
1831
1832 Leaves the point where it is, but moves the mark to where the
1833 file ended up. You can then jump to the file with \
1834 \\[exchange-point-and-mark]."
1835 (interactive)
1836 (stgit-assert-mode)
1837 (if (stgit-patched-file-at-point)
1838 (stgit-file-toggle-index)
1839 (let ((patch-name (stgit-patch-name-at-point)))
1840 (unless (memq patch-name '(:index :work))
1841 (error "Can only move changes between working tree and index"))
1842 (when (stgit-git-index-unmerged-p)
1843 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1844 (if (if (eq patch-name :index)
1845 (stgit-index-empty-p)
1846 (stgit-work-tree-empty-p))
1847 (message "No changes to be moved")
1848 (stgit-capture-output nil
1849 (if (eq patch-name :work)
1850 (stgit-run-git "add" "--update")
1851 (stgit-run-git "reset" "--mixed" "-q")))
1852 (stgit-refresh-worktree)
1853 (stgit-refresh-index))
1854 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1855
1856 (defun stgit-edit ()
1857 "Edit the patch on the current line."
1858 (interactive)
1859 (stgit-assert-mode)
1860 (let ((patchsym (stgit-patch-name-at-point t t))
1861 (edit-buf (get-buffer-create "*StGit edit*"))
1862 (dir default-directory))
1863 (log-edit 'stgit-confirm-edit t nil edit-buf)
1864 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1865 (setq default-directory dir)
1866 (let ((standard-output edit-buf))
1867 (save-excursion
1868 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1869
1870 (defun stgit-confirm-edit ()
1871 (interactive)
1872 (let ((file (make-temp-file "stgit-edit-")))
1873 (write-region (point-min) (point-max) file)
1874 (stgit-capture-output nil
1875 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1876 (with-current-buffer log-edit-parent-buffer
1877 (stgit-reload))))
1878
1879 (defun stgit-new (add-sign &optional refresh)
1880 "Create a new patch.
1881 With a prefix argument, include a \"Signed-off-by:\" line at the
1882 end of the patch."
1883 (interactive "P")
1884 (stgit-assert-mode)
1885 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1886 (dir default-directory))
1887 (log-edit 'stgit-confirm-new t nil edit-buf)
1888 (setq default-directory dir)
1889 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1890 (when add-sign
1891 (save-excursion
1892 (let ((standard-output (current-buffer)))
1893 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1894
1895 (defun stgit-confirm-new ()
1896 (interactive)
1897 (let ((file (make-temp-file "stgit-edit-"))
1898 (refresh stgit-refresh-after-new))
1899 (write-region (point-min) (point-max) file)
1900 (stgit-capture-output nil
1901 (stgit-run "new" "-f" file))
1902 (with-current-buffer log-edit-parent-buffer
1903 (if refresh
1904 (stgit-refresh)
1905 (stgit-reload)))))
1906
1907 (defun stgit-new-and-refresh (add-sign)
1908 "Create a new patch and refresh it with the current changes.
1909
1910 With a prefix argument, include a \"Signed-off-by:\" line at the
1911 end of the patch.
1912
1913 This works just like running `stgit-new' followed by `stgit-refresh'."
1914 (interactive "P")
1915 (stgit-assert-mode)
1916 (stgit-new add-sign t))
1917
1918 (defun stgit-create-patch-name (description)
1919 "Create a patch name from a long description"
1920 (let ((patch ""))
1921 (while (> (length description) 0)
1922 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1923 (setq patch (downcase (concat patch
1924 (match-string 0 description))))
1925 (setq description (substring description (match-end 0))))
1926 ((string-match "\\` +" description)
1927 (setq patch (concat patch "-"))
1928 (setq description (substring description (match-end 0))))
1929 ((string-match "\\`[^a-zA-Z_-]+" description)
1930 (setq description (substring description (match-end 0))))))
1931 (cond ((= (length patch) 0)
1932 "patch")
1933 ((> (length patch) 20)
1934 (substring patch 0 20))
1935 (t patch))))
1936
1937 (defun stgit-delete (patchsyms &optional spill-p)
1938 "Delete the patches in PATCHSYMS.
1939 Interactively, delete the marked patches, or the patch at point.
1940
1941 With a prefix argument, or SPILL-P, spill the patch contents to
1942 the work tree and index."
1943 (interactive (list (stgit-patches-marked-or-at-point t t)
1944 current-prefix-arg))
1945 (stgit-assert-mode)
1946 (unless patchsyms
1947 (error "No patches to delete"))
1948 (when (memq :index patchsyms)
1949 (error "Cannot delete the index"))
1950 (when (memq :work patchsyms)
1951 (error "Cannot delete the work tree"))
1952
1953 (let ((npatches (length patchsyms)))
1954 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1955 npatches
1956 (if (= 1 npatches) "" "es")
1957 (if spill-p
1958 " (spilling contents to index)"
1959 "")))
1960 (let ((args (if spill-p
1961 (cons "--spill" patchsyms)
1962 patchsyms)))
1963 (stgit-capture-output nil
1964 (apply 'stgit-run "delete" args))
1965 (stgit-reload)))))
1966
1967 (defun stgit-move-patches-target ()
1968 "Return the patchsym indicating a target patch for
1969 `stgit-move-patches'.
1970
1971 This is either the first unmarked patch at or after point, or one
1972 of :top and :bottom if the point is after or before the applied
1973 patches."
1974
1975 (save-excursion
1976 (let (result)
1977 (while (not result)
1978 (let ((patchsym (stgit-patch-name-at-point)))
1979 (cond ((memq patchsym '(:work :index)) (setq result :top))
1980 (patchsym (if (memq patchsym stgit-marked-patches)
1981 (stgit-next-patch)
1982 (setq result patchsym)))
1983 ((re-search-backward "^>" nil t) (setq result :top))
1984 (t (setq result :bottom)))))
1985 result)))
1986
1987 (defun stgit-sort-patches (patchsyms)
1988 "Returns the list of patches in PATCHSYMS sorted according to
1989 their position in the patch series, bottommost first.
1990
1991 PATCHSYMS must not contain duplicate entries."
1992 (let (sorted-patchsyms
1993 (series (with-output-to-string
1994 (with-current-buffer standard-output
1995 (stgit-run-silent "series" "--noprefix"))))
1996 start)
1997 (while (string-match "^\\(.+\\)" series start)
1998 (let ((patchsym (intern (match-string 1 series))))
1999 (when (memq patchsym patchsyms)
2000 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2001 (setq start (match-end 0)))
2002 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2003
2004 (unless (= (length patchsyms) (length sorted-patchsyms))
2005 (error "Internal error"))
2006
2007 sorted-patchsyms))
2008
2009 (defun stgit-move-patches (patchsyms target-patch)
2010 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2011 If TARGET-PATCH is :bottom or :top, move the patches to the
2012 bottom or top of the stack, respectively.
2013
2014 Interactively, move the marked patches to where the point is."
2015 (interactive (list stgit-marked-patches
2016 (stgit-move-patches-target)))
2017 (stgit-assert-mode)
2018 (unless patchsyms
2019 (error "Need at least one patch to move"))
2020
2021 (unless target-patch
2022 (error "Point not at a patch"))
2023
2024 ;; need to have patchsyms sorted by position in the stack
2025 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2026 (stgit-capture-output nil
2027 (if (eq target-patch :top)
2028 (apply 'stgit-run "float" sorted-patchsyms)
2029 (apply 'stgit-run
2030 "sink"
2031 (append (unless (eq target-patch :bottom)
2032 (list "--to" target-patch))
2033 '("--")
2034 sorted-patchsyms)))))
2035 (stgit-reload))
2036
2037 (defun stgit-squash (patchsyms)
2038 "Squash the patches in PATCHSYMS.
2039 Interactively, squash the marked patches.
2040
2041 Unless there are any conflicts, the patches will be merged into
2042 one patch, which will occupy the same spot in the series as the
2043 deepest patch had before the squash."
2044 (interactive (list stgit-marked-patches))
2045 (stgit-assert-mode)
2046 (when (< (length patchsyms) 2)
2047 (error "Need at least two patches to squash"))
2048 (let ((stgit-buffer (current-buffer))
2049 (edit-buf (get-buffer-create "*StGit edit*"))
2050 (dir default-directory)
2051 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2052 (log-edit 'stgit-confirm-squash t nil edit-buf)
2053 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2054 (setq default-directory dir)
2055 (let ((result (let ((standard-output edit-buf))
2056 (save-excursion
2057 (apply 'stgit-run-silent "squash"
2058 "--save-template=-" sorted-patchsyms)))))
2059
2060 ;; stg squash may have reordered the patches or caused conflicts
2061 (with-current-buffer stgit-buffer
2062 (stgit-reload))
2063
2064 (unless (eq 0 result)
2065 (fundamental-mode)
2066 (rename-buffer "*StGit error*")
2067 (resize-temp-buffer-window)
2068 (switch-to-buffer-other-window stgit-buffer)
2069 (error "stg squash failed")))))
2070
2071 (defun stgit-confirm-squash ()
2072 (interactive)
2073 (let ((file (make-temp-file "stgit-edit-")))
2074 (write-region (point-min) (point-max) file)
2075 (stgit-capture-output nil
2076 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2077 (with-current-buffer log-edit-parent-buffer
2078 (stgit-clear-marks)
2079 ;; Go to first marked patch and stay there
2080 (goto-char (point-min))
2081 (re-search-forward (concat "^[>+-]\\*") nil t)
2082 (move-to-column goal-column)
2083 (let ((pos (point)))
2084 (stgit-reload)
2085 (goto-char pos)))))
2086
2087 (defun stgit-help ()
2088 "Display help for the StGit mode."
2089 (interactive)
2090 (describe-function 'stgit-mode))
2091
2092 (defun stgit-undo-or-redo (redo hard)
2093 "Run stg undo or, if REDO is non-nil, stg redo.
2094
2095 If HARD is non-nil, use the --hard flag."
2096 (stgit-assert-mode)
2097 (let ((cmd (if redo "redo" "undo")))
2098 (stgit-capture-output nil
2099 (if arg
2100 (when (or (and (stgit-index-empty-p)
2101 (stgit-work-tree-empty-p))
2102 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2103 cmd)))
2104 (stgit-run cmd "--hard"))
2105 (stgit-run cmd))))
2106 (stgit-reload))
2107
2108 (defun stgit-undo (&optional arg)
2109 "Run stg undo.
2110 With prefix argument, run it with the --hard flag.
2111
2112 See also `stgit-redo'."
2113 (interactive "P")
2114 (stgit-undo-or-redo nil arg))
2115
2116 (defun stgit-redo (&optional arg)
2117 "Run stg redo.
2118 With prefix argument, run it with the --hard flag.
2119
2120 See also `stgit-undo'."
2121 (interactive "P")
2122 (stgit-undo-or-redo t arg))
2123
2124 (defun stgit-refresh (&optional arg)
2125 "Run stg refresh.
2126 If the index contains any changes, only refresh from index.
2127
2128 With prefix argument, refresh the marked patch or the patch under point."
2129 (interactive "P")
2130 (stgit-assert-mode)
2131 (let ((patchargs (if arg
2132 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2133 (when (> (length patches) 1)
2134 (error "Too many patches marked"))
2135 (cons "-p" patches))
2136 nil)))
2137 (unless (stgit-index-empty-p)
2138 (setq patchargs (cons "--index" patchargs)))
2139 (stgit-capture-output nil
2140 (apply 'stgit-run "refresh" patchargs))
2141 (stgit-refresh-git-status))
2142 (stgit-reload))
2143
2144 (defvar stgit-show-worktree nil
2145 "If nil, inhibit showing work tree and index in the stgit buffer.
2146
2147 See also `stgit-show-worktree-mode'.")
2148
2149 (defvar stgit-show-ignored nil
2150 "If nil, inhibit showing files ignored by git.")
2151
2152 (defvar stgit-show-unknown nil
2153 "If nil, inhibit showing files not registered with git.")
2154
2155 (defvar stgit-show-patch-names t
2156 "If nil, inhibit showing patch names.")
2157
2158 (defun stgit-toggle-worktree (&optional arg)
2159 "Toggle the visibility of the work tree.
2160 With ARG, show the work tree if ARG is positive.
2161
2162 Its initial setting is controlled by `stgit-default-show-worktree'.
2163
2164 `stgit-show-worktree-mode' controls where on screen the index and
2165 work tree will show up."
2166 (interactive)
2167 (stgit-assert-mode)
2168 (setq stgit-show-worktree
2169 (if (numberp arg)
2170 (> arg 0)
2171 (not stgit-show-worktree)))
2172 (stgit-reload))
2173
2174 (defun stgit-toggle-ignored (&optional arg)
2175 "Toggle the visibility of files ignored by git in the work
2176 tree. With ARG, show these files if ARG is positive.
2177
2178 Use \\[stgit-toggle-worktree] to show the work tree."
2179 (interactive)
2180 (stgit-assert-mode)
2181 (setq stgit-show-ignored
2182 (if (numberp arg)
2183 (> arg 0)
2184 (not stgit-show-ignored)))
2185 (stgit-reload))
2186
2187 (defun stgit-toggle-unknown (&optional arg)
2188 "Toggle the visibility of files not registered with git in the
2189 work tree. With ARG, show these files if ARG is positive.
2190
2191 Use \\[stgit-toggle-worktree] to show the work tree."
2192 (interactive)
2193 (stgit-assert-mode)
2194 (setq stgit-show-unknown
2195 (if (numberp arg)
2196 (> arg 0)
2197 (not stgit-show-unknown)))
2198 (stgit-reload))
2199
2200 (defun stgit-toggle-patch-names (&optional arg)
2201 "Toggle the visibility of patch names. With ARG, show patch names
2202 if ARG is positive.
2203
2204 The initial setting is controlled by `stgit-default-show-patch-names'."
2205 (interactive)
2206 (stgit-assert-mode)
2207 (setq stgit-show-patch-names
2208 (if (numberp arg)
2209 (> arg 0)
2210 (not stgit-show-patch-names)))
2211 (stgit-reload))
2212
2213 (provide 'stgit)