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