stgit.el: Run "git update-index --refresh" before redrawing work tree status
[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 (let (standard-output)
746 (stgit-run-git "update-index" "--refresh"))
747 `("diff-files" "-0" ,@args))
748 ((eq patchsym :index)
749 `("diff-index" ,@args "--cached" "HEAD"))
750 (t
751 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
752
753 (when (and (eq patchsym :work))
754 (when stgit-show-ignored
755 (stgit-insert-ls-files '("--ignored" "--others") "I"))
756 (when stgit-show-unknown
757 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
758 "--others")
759 "X"))
760 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
761 (point-min) (point-max)))
762
763 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
764
765 (unless (ewoc-nth ewoc 0)
766 (ewoc-set-hf ewoc ""
767 (concat " "
768 (propertize "<no files>"
769 'face 'stgit-description-face)
770 "\n")))))
771 (goto-char end)))
772
773 (defun stgit-find-file (&optional other-window)
774 (let* ((file (or (stgit-patched-file-at-point)
775 (error "No file at point")))
776 (filename (expand-file-name (stgit-file->file file))))
777 (unless (file-exists-p filename)
778 (error "File does not exist"))
779 (funcall (if other-window 'find-file-other-window 'find-file)
780 filename)
781 (when (eq (stgit-file->status file) 'unmerged)
782 (smerge-mode 1))))
783
784 (defun stgit-expand (&optional patches collapse)
785 "Show the contents of marked patches, or the patch at point.
786
787 See also `stgit-collapse'.
788
789 Non-interactively, operate on PATCHES, and collapse instead of
790 expand if COLLAPSE is not nil."
791 (interactive (list (stgit-patches-marked-or-at-point t)))
792 (stgit-assert-mode)
793 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
794 patches stgit-expanded-patches)))
795 (setq stgit-expanded-patches
796 (if collapse
797 (set-difference stgit-expanded-patches patches-diff)
798 (append stgit-expanded-patches patches-diff)))
799 (ewoc-map #'(lambda (patch)
800 (memq (stgit-patch->name patch) patches-diff))
801 stgit-ewoc))
802 (move-to-column (stgit-goal-column)))
803
804 (defun stgit-collapse (&optional patches)
805 "Hide the contents of marked patches, or the patch at point.
806
807 See also `stgit-expand'."
808 (interactive (list (stgit-patches-marked-or-at-point t)))
809 (stgit-assert-mode)
810 (stgit-expand patches t))
811
812 (defun stgit-select-patch ()
813 (let ((patchname (stgit-patch-name-at-point)))
814 (stgit-expand (list patchname)
815 (memq patchname stgit-expanded-patches))))
816
817 (defun stgit-expand-directory (file)
818 (let* ((patch (stgit-patch-at-point))
819 (ewoc (stgit-patch->files-ewoc patch))
820 (node (ewoc-locate ewoc))
821 (filename (stgit-file->file file))
822 (start (make-marker))
823 (end (make-marker)))
824
825 (save-excursion
826 (forward-line 1)
827 (set-marker start (point))
828 (set-marker end (point))
829 (set-marker-insertion-type end t))
830
831 (assert (string-match "/$" filename))
832 ;; remove trailing "/"
833 (setf (stgit-file->file file) (substring filename 0 -1))
834 (ewoc-invalidate ewoc node)
835
836 (with-temp-buffer
837 (let ((standard-output (current-buffer)))
838 (stgit-insert-ls-files (list "--directory" "--others"
839 "--no-empty-directory" "--"
840 filename)
841 "X")
842 (stgit-process-files (lambda (f)
843 (setq node (ewoc-enter-after ewoc node f))))))
844
845 (let ((inhibit-read-only t))
846 (put-text-property start end 'patch-data patch))))
847
848 (defun stgit-select-file ()
849 (let* ((file (or (stgit-patched-file-at-point)
850 (error "No file at point")))
851 (filename (stgit-file->file file)))
852 (if (string-match "/$" filename)
853 (stgit-expand-directory file)
854 (stgit-find-file))))
855
856 (defun stgit-select ()
857 "With point on a patch, toggle showing files in the patch.
858
859 With point on a file, open the associated file. Opens the target
860 file for (applied) copies and renames."
861 (interactive)
862 (stgit-assert-mode)
863 (case (get-text-property (point) 'entry-type)
864 ('patch
865 (stgit-select-patch))
866 ('file
867 (stgit-select-file))
868 (t
869 (error "No patch or file on line"))))
870
871 (defun stgit-find-file-other-window ()
872 "Open file at point in other window"
873 (interactive)
874 (stgit-assert-mode)
875 (stgit-find-file t))
876
877 (defun stgit-find-file-merge ()
878 "Open file at point and merge it using `smerge-ediff'."
879 (interactive)
880 (stgit-assert-mode)
881 (stgit-find-file t)
882 (smerge-ediff))
883
884 (defun stgit-quit ()
885 "Hide the stgit buffer."
886 (interactive)
887 (stgit-assert-mode)
888 (bury-buffer))
889
890 (defun stgit-git-status ()
891 "Show status using `git-status'."
892 (interactive)
893 (stgit-assert-mode)
894 (unless (fboundp 'git-status)
895 (error "The stgit-git-status command requires git-status"))
896 (let ((dir default-directory))
897 (save-selected-window
898 (pop-to-buffer nil)
899 (git-status dir))))
900
901 (defun stgit-goal-column ()
902 "Return goal column for the current line"
903 (case (get-text-property (point) 'entry-type)
904 ('patch 2)
905 ('file 4)
906 (t 0)))
907
908 (defun stgit-next-line (&optional arg)
909 "Move cursor vertically down ARG lines"
910 (interactive "p")
911 (stgit-assert-mode)
912 (next-line arg)
913 (move-to-column (stgit-goal-column)))
914
915 (defun stgit-previous-line (&optional arg)
916 "Move cursor vertically up ARG lines"
917 (interactive "p")
918 (stgit-assert-mode)
919 (previous-line arg)
920 (move-to-column (stgit-goal-column)))
921
922 (defun stgit-next-patch (&optional arg)
923 "Move cursor down ARG patches."
924 (interactive "p")
925 (stgit-assert-mode)
926 (ewoc-goto-next stgit-ewoc (or arg 1))
927 (move-to-column goal-column))
928
929 (defun stgit-previous-patch (&optional arg)
930 "Move cursor up ARG patches."
931 (interactive "p")
932 (stgit-assert-mode)
933 (ewoc-goto-prev stgit-ewoc (or arg 1))
934 (move-to-column goal-column))
935
936 (defvar stgit-mode-hook nil
937 "Run after `stgit-mode' is setup.")
938
939 (defvar stgit-mode-map nil
940 "Keymap for StGit major mode.")
941
942 (unless stgit-mode-map
943 (let ((diff-map (make-sparse-keymap))
944 (toggle-map (make-sparse-keymap)))
945 (suppress-keymap diff-map)
946 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
947 '(("b" . stgit-diff-base)
948 ("c" . stgit-diff-combined)
949 ("m" . stgit-find-file-merge)
950 ("o" . stgit-diff-ours)
951 ("r" . stgit-diff-range)
952 ("t" . stgit-diff-theirs)))
953 (suppress-keymap toggle-map)
954 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
955 '(("n" . stgit-toggle-patch-names)
956 ("t" . stgit-toggle-worktree)
957 ("i" . stgit-toggle-ignored)
958 ("u" . stgit-toggle-unknown)))
959 (setq stgit-mode-map (make-keymap))
960 (suppress-keymap stgit-mode-map)
961 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
962 `((" " . stgit-mark-down)
963 ("m" . stgit-mark-down)
964 ("\d" . stgit-unmark-up)
965 ("u" . stgit-unmark-down)
966 ("?" . stgit-help)
967 ("h" . stgit-help)
968 ("\C-p" . stgit-previous-line)
969 ("\C-n" . stgit-next-line)
970 ([up] . stgit-previous-line)
971 ([down] . stgit-next-line)
972 ("p" . stgit-previous-patch)
973 ("n" . stgit-next-patch)
974 ("\M-{" . stgit-previous-patch)
975 ("\M-}" . stgit-next-patch)
976 ("s" . stgit-git-status)
977 ("g" . stgit-reload-or-repair)
978 ("r" . stgit-refresh)
979 ("\C-c\C-r" . stgit-rename)
980 ("e" . stgit-edit)
981 ("M" . stgit-move-patches)
982 ("S" . stgit-squash)
983 ("N" . stgit-new)
984 ("c" . stgit-new-and-refresh)
985 ("\C-c\C-c" . stgit-commit)
986 ("\C-c\C-u" . stgit-uncommit)
987 ("U" . stgit-revert)
988 ("R" . stgit-resolve-file)
989 ("\r" . stgit-select)
990 ("+" . stgit-expand)
991 ("-" . stgit-collapse)
992 ("o" . stgit-find-file-other-window)
993 ("i" . stgit-toggle-index)
994 (">" . stgit-push-next)
995 ("<" . stgit-pop-next)
996 ("P" . stgit-push-or-pop)
997 ("G" . stgit-goto)
998 ("=" . stgit-diff)
999 ("D" . stgit-delete)
1000 ([?\C-/] . stgit-undo)
1001 ("\C-_" . stgit-undo)
1002 ([?\C-c ?\C-/] . stgit-redo)
1003 ("\C-c\C-_" . stgit-redo)
1004 ("B" . stgit-branch)
1005 ("\C-c\C-b" . stgit-rebase)
1006 ("t" . ,toggle-map)
1007 ("d" . ,diff-map)
1008 ("q" . stgit-quit))))
1009
1010 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1011 (and file (eq (stgit-file->status file)
1012 'unmerged))))
1013 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1014 (easy-menu-define stgit-menu stgit-mode-map
1015 "StGit Menu"
1016 `("StGit"
1017 ["Reload" stgit-reload-or-repair
1018 :help "Reload StGit status from disk"]
1019 ["Repair" stgit-repair
1020 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1021 :help "Repair StGit metadata"]
1022 "-"
1023 ["Undo" stgit-undo t]
1024 ["Redo" stgit-redo t]
1025 "-"
1026 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1027 "-"
1028 ["New patch" stgit-new-and-refresh
1029 :help "Create a new patch from changes in index or work tree"
1030 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1031 ["New empty patch" stgit-new
1032 :help "Create a new, empty patch"]
1033 ["(Un)mark patch" stgit-toggle-mark
1034 :label (if (memq (stgit-patch-name-at-point nil t)
1035 stgit-marked-patches)
1036 "Unmark patch" "Mark patch")
1037 :active (stgit-patch-name-at-point nil t)]
1038 ["Expand/collapse patch"
1039 (let ((patches (stgit-patches-marked-or-at-point)))
1040 (if (member-if ,patch-collapsed-p patches)
1041 (stgit-expand patches)
1042 (stgit-collapse patches)))
1043 :label (if (member-if ,patch-collapsed-p
1044 (stgit-patches-marked-or-at-point))
1045 "Expand patches"
1046 "Collapse patches")
1047 :active (stgit-patches-marked-or-at-point)]
1048 ["Edit patch" stgit-edit
1049 :help "Edit patch comment"
1050 :active (stgit-patch-name-at-point nil t)]
1051 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1052 ["Push/pop patch" stgit-push-or-pop
1053 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1054 (stgit-applied-patchsyms t))
1055 "Pop patches" "Push patches")]
1056 ["Delete patches" stgit-delete
1057 :active (stgit-patches-marked-or-at-point nil t)]
1058 "-"
1059 ["Move patches" stgit-move-patches
1060 :active stgit-marked-patches
1061 :help "Move marked patch(es) to point"]
1062 ["Squash patches" stgit-squash
1063 :active (> (length stgit-marked-patches) 1)
1064 :help "Merge marked patches into one"]
1065 "-"
1066 ["Refresh top patch" stgit-refresh
1067 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1068 :help "Refresh the top patch with changes in index or work tree"]
1069 ["Refresh this patch" (stgit-refresh t)
1070 :keys "\\[universal-argument] \\[stgit-refresh]"
1071 :help "Refresh marked patch with changes in index or work tree"
1072 :active (and (not (and (stgit-index-empty-p)
1073 (stgit-work-tree-empty-p)))
1074 (stgit-patch-name-at-point nil t))]
1075 "-"
1076 ["Find file" stgit-select
1077 :active (eq (get-text-property (point) 'entry-type) 'file)]
1078 ["Open file" stgit-find-file-other-window
1079 :active (eq (get-text-property (point) 'entry-type) 'file)]
1080 ["Toggle file index" stgit-toggle-index
1081 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1082 (memq (stgit-patch-name-at-point) '(:work :index)))
1083 :label (if (eq (stgit-patch-name-at-point) :work)
1084 "Move change to index"
1085 "Move change to work tree")]
1086 "-"
1087 ["Show diff" stgit-diff
1088 :active (get-text-property (point) 'entry-type)]
1089 ["Show diff for range of applied patches" stgit-diff-range
1090 :active (= (length stgit-marked-patches) 1)]
1091 ("Merge"
1092 :active (stgit-git-index-unmerged-p)
1093 ["Combined diff" stgit-diff-combined
1094 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1095 ["Diff against base" stgit-diff-base
1096 :help "Show diff against the common base"
1097 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1098 ["Diff against ours" stgit-diff-ours
1099 :help "Show diff against our branch"
1100 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1101 ["Diff against theirs" stgit-diff-theirs
1102 :help "Show diff against their branch"
1103 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1104 "-"
1105 ["Interactive merge" stgit-find-file-merge
1106 :help "Interactively merge the file"
1107 :active ,at-unmerged-file]
1108 ["Resolve file" stgit-resolve-file
1109 :help "Mark file conflict as resolved"
1110 :active ,at-unmerged-file]
1111 )
1112 "-"
1113 ["Show index & work tree" stgit-toggle-worktree :style toggle
1114 :selected stgit-show-worktree]
1115 ["Show unknown files" stgit-toggle-unknown :style toggle
1116 :selected stgit-show-unknown :active stgit-show-worktree]
1117 ["Show ignored files" stgit-toggle-ignored :style toggle
1118 :selected stgit-show-ignored :active stgit-show-worktree]
1119 ["Show patch names" stgit-toggle-patch-names :style toggle
1120 :selected stgit-show-patch-names]
1121 "-"
1122 ["Switch branches" stgit-branch t
1123 :help "Switch to or create another branch"]
1124 ["Rebase branch" stgit-rebase t
1125 :help "Rebase the current branch"]
1126 ))))
1127
1128 ;; disable tool bar editing buttons
1129 (put 'stgit-mode 'mode-class 'special)
1130
1131 (defun stgit-mode ()
1132 "Major mode for interacting with StGit.
1133
1134 Start StGit using \\[stgit].
1135
1136 Basic commands:
1137 \\<stgit-mode-map>\
1138 \\[stgit-help] Show this help text
1139 \\[stgit-quit] Hide the StGit buffer
1140 \\[describe-bindings] Show all key bindings
1141
1142 \\[stgit-reload-or-repair] Reload the StGit buffer
1143 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1144
1145 \\[stgit-undo] Undo most recent StGit operation
1146 \\[stgit-redo] Undo recent undo
1147
1148 \\[stgit-git-status] Run `git-status' (if available)
1149
1150 Movement commands:
1151 \\[stgit-previous-line] Move to previous line
1152 \\[stgit-next-line] Move to next line
1153 \\[stgit-previous-patch] Move to previous patch
1154 \\[stgit-next-patch] Move to next patch
1155
1156 \\[stgit-mark-down] Mark patch and move down
1157 \\[stgit-unmark-up] Unmark patch and move up
1158 \\[stgit-unmark-down] Unmark patch and move down
1159
1160 Commands for patches:
1161 \\[stgit-select] Toggle showing changed files in patch
1162 \\[stgit-refresh] Refresh patch with changes in index or work tree
1163 \\[stgit-diff] Show the patch log and diff
1164
1165 \\[stgit-expand] Show changes in marked patches
1166 \\[stgit-collapse] Hide changes in marked patches
1167
1168 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1169 \\[stgit-new] Create a new, empty patch
1170
1171 \\[stgit-rename] Rename patch
1172 \\[stgit-edit] Edit patch description
1173 \\[stgit-delete] Delete patch(es)
1174
1175 \\[stgit-revert] Revert all changes in index or work tree
1176 \\[stgit-toggle-index] Toggle all changes between index and work tree
1177
1178 \\[stgit-push-next] Push next patch onto stack
1179 \\[stgit-pop-next] Pop current patch from stack
1180 \\[stgit-push-or-pop] Push or pop marked patches
1181 \\[stgit-goto] Make patch at point current by popping or pushing
1182
1183 \\[stgit-squash] Squash (meld together) patches
1184 \\[stgit-move-patches] Move marked patches to point
1185
1186 \\[stgit-commit] Commit patch(es)
1187 \\[stgit-uncommit] Uncommit patch(es)
1188
1189 Commands for files:
1190 \\[stgit-select] Open the file in this window
1191 \\[stgit-find-file-other-window] Open the file in another window
1192 \\[stgit-diff] Show the file's diff
1193
1194 \\[stgit-toggle-index] Toggle change between index and work tree
1195 \\[stgit-revert] Revert changes to file
1196
1197 Display commands:
1198 \\[stgit-toggle-patch-names] Toggle showing patch names
1199 \\[stgit-toggle-worktree] Toggle showing index and work tree
1200 \\[stgit-toggle-unknown] Toggle showing unknown files
1201 \\[stgit-toggle-ignored] Toggle showing ignored files
1202
1203 Commands for diffs:
1204 \\[stgit-diff] Show diff of patch or file
1205 \\[stgit-diff-range] Show diff for range of patches
1206 \\[stgit-diff-base] Show diff against the merge base
1207 \\[stgit-diff-ours] Show diff against our branch
1208 \\[stgit-diff-theirs] Show diff against their branch
1209
1210 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1211 ignore space changes.
1212 With two prefix arguments (e.g., \\[universal-argument] \
1213 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1214
1215 Commands for merge conflicts:
1216 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1217 \\[stgit-resolve-file] Mark unmerged file as resolved
1218
1219 Commands for branches:
1220 \\[stgit-branch] Switch to or create another branch
1221 \\[stgit-rebase] Rebase the current branch
1222
1223 Customization variables:
1224 `stgit-abbreviate-copies-and-renames'
1225 `stgit-default-show-patch-names'
1226 `stgit-default-show-worktree'
1227 `stgit-find-copies-harder'
1228 `stgit-show-worktree-mode'
1229
1230 See also \\[customize-group] for the \"stgit\" group."
1231 (kill-all-local-variables)
1232 (buffer-disable-undo)
1233 (setq mode-name "StGit"
1234 major-mode 'stgit-mode
1235 goal-column 2)
1236 (use-local-map stgit-mode-map)
1237 (set (make-local-variable 'list-buffers-directory) default-directory)
1238 (set (make-local-variable 'stgit-marked-patches) nil)
1239 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1240 (set (make-local-variable 'stgit-show-patch-names)
1241 stgit-default-show-patch-names)
1242 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1243 (set (make-local-variable 'stgit-index-node) nil)
1244 (set (make-local-variable 'stgit-worktree-node) nil)
1245 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1246 (set-variable 'truncate-lines 't)
1247 (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer)
1248 (unless stgit-did-advise
1249 (stgit-advise)
1250 (setq stgit-did-advise t))
1251 (run-hooks 'stgit-mode-hook))
1252
1253 (defun stgit-advise-funlist (funlist)
1254 "Add advice to the functions in FUNLIST so we can refresh the
1255 stgit buffers as the git status of files change."
1256 (mapc (lambda (sym)
1257 (when (fboundp sym)
1258 (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer)
1259 (stgit-update-stgit-for-buffer t)))
1260 (ad-activate sym)))
1261 funlist))
1262
1263 (defun stgit-advise ()
1264 "Add advice to appropriate (non-stgit) git functions so we can
1265 refresh the stgit buffers as the git status of files change."
1266 (mapc (lambda (arg)
1267 (let ((feature (car arg))
1268 (funlist (cdr arg)))
1269 (if (featurep feature)
1270 (stgit-advise-funlist funlist)
1271 (add-to-list 'after-load-alist
1272 `(,feature (stgit-advise-funlist
1273 (quote ,funlist)))))))
1274 '((vc-git vc-git-rename-file vc-git-revert vc-git-register)
1275 (git git-add-file git-checkout git-revert-file git-remove-file))))
1276
1277 (defun stgit-update-stgit-for-buffer (&optional refresh-index)
1278 "Refresh worktree status in any `stgit-mode' buffer that shows
1279 the status of the current buffer.
1280
1281 If REFRESH-INDEX is not-nil, also update the index."
1282 (let* ((dir (cond ((eq major-mode 'git-status-mode)
1283 default-directory)
1284 (buffer-file-name
1285 (file-name-directory
1286 (expand-file-name buffer-file-name)))))
1287 (gitdir (and dir (condition-case nil (git-get-top-dir dir)
1288 (error nil))))
1289 (buffer (and gitdir (stgit-find-buffer gitdir))))
1290 (when buffer
1291 (with-current-buffer buffer
1292 (stgit-refresh-worktree)
1293 (when refresh-index (stgit-refresh-index))))))
1294
1295 (defun stgit-add-mark (patchsym)
1296 "Mark the patch PATCHSYM."
1297 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1298
1299 (defun stgit-remove-mark (patchsym)
1300 "Unmark the patch PATCHSYM."
1301 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1302
1303 (defun stgit-clear-marks ()
1304 "Unmark all patches."
1305 (setq stgit-marked-patches '()))
1306
1307 (defun stgit-patch-at-point (&optional cause-error)
1308 (get-text-property (point) 'patch-data))
1309
1310 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1311 "Return the patch name on the current line as a symbol.
1312 If CAUSE-ERROR is not nil, signal an error if none found.
1313 If ONLY-PATCHES is not nil, only allow real patches, and not
1314 index or work tree."
1315 (let ((patch (stgit-patch-at-point)))
1316 (and patch
1317 only-patches
1318 (memq (stgit-patch->status patch) '(work index))
1319 (setq patch nil))
1320 (cond (patch
1321 (stgit-patch->name patch))
1322 (cause-error
1323 (error "No patch on this line")))))
1324
1325 (defun stgit-patched-file-at-point ()
1326 (get-text-property (point) 'file-data))
1327
1328 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1329 "Return the symbols of the marked patches, or the patch on the current line.
1330 If CAUSE-ERRROR is not nil, signal an error if none found.
1331 If ONLY-PATCHES is not nil, do not include index or work tree."
1332 (if stgit-marked-patches
1333 stgit-marked-patches
1334 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1335 (cond (patch (list patch))
1336 (cause-error (error "No patches marked or at this line"))
1337 (t nil)))))
1338
1339 (defun stgit-goto-patch (patchsym &optional file)
1340 "Move point to the line containing patch PATCHSYM.
1341 If that patch cannot be found, do nothing.
1342
1343 If the patch was found and FILE is not nil, instead move to that
1344 file's line. If FILE cannot be found, stay on the line of
1345 PATCHSYM."
1346 (let ((node (ewoc-nth stgit-ewoc 0)))
1347 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1348 patchsym)))
1349 (setq node (ewoc-next stgit-ewoc node)))
1350 (when (and node file)
1351 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1352 (file-node (ewoc-nth file-ewoc 0)))
1353 (while (and file-node (not (equal (stgit-file->file (ewoc-data file-node)) file)))
1354 (setq file-node (ewoc-next file-ewoc file-node)))
1355 (when file-node
1356 (ewoc-goto-node file-ewoc file-node)
1357 (move-to-column (stgit-goal-column))
1358 (setq node nil))))
1359 (when node
1360 (ewoc-goto-node stgit-ewoc node)
1361 (move-to-column goal-column))))
1362
1363 (defun stgit-init ()
1364 "Run stg init."
1365 (interactive)
1366 (stgit-assert-mode)
1367 (stgit-capture-output nil
1368 (stgit-run "init"))
1369 (stgit-reload))
1370
1371 (defun stgit-toggle-mark ()
1372 "Toggle mark on the patch under point."
1373 (interactive)
1374 (stgit-assert-mode)
1375 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1376 (stgit-unmark)
1377 (stgit-mark)))
1378
1379 (defun stgit-mark ()
1380 "Mark the patch under point."
1381 (interactive)
1382 (stgit-assert-mode)
1383 (let* ((node (ewoc-locate stgit-ewoc))
1384 (patch (ewoc-data node))
1385 (name (stgit-patch->name patch)))
1386 (when (eq name :work)
1387 (error "Cannot mark the work tree"))
1388 (when (eq name :index)
1389 (error "Cannot mark the index"))
1390 (stgit-add-mark (stgit-patch->name patch))
1391 (let ((column (current-column)))
1392 (ewoc-invalidate stgit-ewoc node)
1393 (move-to-column column))))
1394
1395 (defun stgit-mark-down ()
1396 "Mark the patch under point and move to the next patch."
1397 (interactive)
1398 (stgit-mark)
1399 (stgit-next-patch))
1400
1401 (defun stgit-unmark ()
1402 "Remove mark from the patch on the current line."
1403 (interactive)
1404 (stgit-assert-mode)
1405 (let* ((node (ewoc-locate stgit-ewoc))
1406 (patch (ewoc-data node)))
1407 (stgit-remove-mark (stgit-patch->name patch))
1408 (let ((column (current-column)))
1409 (ewoc-invalidate stgit-ewoc node)
1410 (move-to-column column))))
1411
1412 (defun stgit-unmark-up ()
1413 "Remove mark from the patch on the previous line."
1414 (interactive)
1415 (stgit-assert-mode)
1416 (stgit-previous-patch)
1417 (stgit-unmark))
1418
1419 (defun stgit-unmark-down ()
1420 "Remove mark from the patch on the current line."
1421 (interactive)
1422 (stgit-assert-mode)
1423 (stgit-unmark)
1424 (stgit-next-patch))
1425
1426 (defun stgit-rename (name)
1427 "Rename the patch under point to NAME."
1428 (interactive (list
1429 (read-string "Patch name: "
1430 (symbol-name (stgit-patch-name-at-point t t)))))
1431 (stgit-assert-mode)
1432 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1433 (stgit-capture-output nil
1434 (stgit-run "rename" old-patchsym name))
1435 (let ((name-sym (intern name)))
1436 (when (memq old-patchsym stgit-expanded-patches)
1437 (setq stgit-expanded-patches
1438 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1439 (when (memq old-patchsym stgit-marked-patches)
1440 (setq stgit-marked-patches
1441 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1442 (stgit-reload)
1443 (stgit-goto-patch name-sym))))
1444
1445 (defun stgit-reload-or-repair (repair)
1446 "Update the contents of the StGit buffer (`stgit-reload').
1447
1448 With a prefix argument, repair the StGit metadata if the branch
1449 was modified with git commands (`stgit-repair')."
1450 (interactive "P")
1451 (stgit-assert-mode)
1452 (if repair
1453 (stgit-repair)
1454 (stgit-reload)))
1455
1456 (defun stgit-repair ()
1457 "Run stg repair."
1458 (interactive)
1459 (stgit-assert-mode)
1460 (stgit-capture-output nil
1461 (stgit-run "repair"))
1462 (stgit-reload))
1463
1464 (defun stgit-available-branches (&optional all)
1465 "Returns a list of the names of the available stg branches as strings.
1466
1467 If ALL is not nil, also return non-stgit branches."
1468 (let ((output (with-output-to-string
1469 (stgit-run "branch" "--list")))
1470 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1471 (if all ?. ?s)))
1472 (start 0)
1473 result)
1474 (while (string-match pattern output start)
1475 (setq result (cons (match-string 1 output) result))
1476 (setq start (match-end 0)))
1477 result))
1478
1479 (defun stgit-branch (branch)
1480 "Switch to or create branch BRANCH."
1481 (interactive (list (completing-read "Switch to branch: "
1482 (stgit-available-branches))))
1483 (stgit-assert-mode)
1484 (when (cond ((equal branch (stgit-current-branch))
1485 (error "Branch is already current"))
1486 ((member branch (stgit-available-branches t))
1487 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1488 t)
1489 ((not (string-match stgit-allowed-branch-name-re branch))
1490 (error "Invalid branch name"))
1491 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1492 (stgit-capture-output nil (stgit-run "branch" "--create" "--"
1493 branch))
1494 t))
1495 (stgit-reload)))
1496
1497 (defun stgit-available-refs (&optional omit-stgit)
1498 "Returns a list of the available git refs.
1499 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1500 (let* ((output (with-output-to-string
1501 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1502 "refs/tags" "refs/heads"
1503 "refs/remotes")))
1504 (result (split-string output "\n" t)))
1505 (mapcar (lambda (s)
1506 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1507 (substring s (match-end 0))
1508 s))
1509 (if omit-stgit
1510 (delete-if (lambda (s)
1511 (string-match "^refs/heads/.*\\.stgit$" s))
1512 result)
1513 result))))
1514
1515 (defun stgit-parent-branch ()
1516 "Return the parent branch of the current stg branch as per
1517 git-config setting branch.<branch>.stgit.parentbranch."
1518 (let ((output (with-output-to-string
1519 (stgit-run-git-silent "config"
1520 (format "branch.%s.stgit.parentbranch"
1521 (stgit-current-branch))))))
1522 (when (string-match ".*" output)
1523 (match-string 0 output))))
1524
1525 (defun stgit-rebase (new-base)
1526 "Rebase the current branch to NEW-BASE.
1527
1528 Interactively, first ask which branch to rebase to. Defaults to
1529 what git-config branch.<branch>.stgit.parentbranch is set to."
1530 (interactive (list (completing-read "Rebase to: "
1531 (stgit-available-refs t)
1532 nil nil
1533 (stgit-parent-branch))))
1534 (stgit-assert-mode)
1535 (stgit-capture-output nil (stgit-run "rebase" new-base))
1536 (stgit-reload))
1537
1538 (defun stgit-commit (count)
1539 "Run stg commit on COUNT commits.
1540 Interactively, the prefix argument is used as COUNT.
1541 A negative COUNT will uncommit instead."
1542 (interactive "p")
1543 (stgit-assert-mode)
1544 (if (< count 0)
1545 (stgit-uncommit (- count))
1546 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1547 (stgit-reload)))
1548
1549 (defun stgit-uncommit (count)
1550 "Run stg uncommit on COUNT commits.
1551 Interactively, the prefix argument is used as COUNT.
1552 A negative COUNT will commit instead."
1553 (interactive "p")
1554 (stgit-assert-mode)
1555 (if (< count 0)
1556 (stgit-commit (- count))
1557 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1558 (stgit-reload)))
1559
1560 (defun stgit-neighbour-file ()
1561 "Return the file name of the next file after point, or the
1562 previous file if point is at the last file within a patch."
1563 (let ((old-point (point))
1564 neighbour-file)
1565 (and (zerop (forward-line 1))
1566 (let ((f (stgit-patched-file-at-point)))
1567 (and f (setq neighbour-file (stgit-file->file f)))))
1568 (goto-char old-point)
1569 (unless neighbour-file
1570 (and (zerop (forward-line -1))
1571 (let ((f (stgit-patched-file-at-point)))
1572 (and f (setq neighbour-file (stgit-file->file f)))))
1573 (goto-char old-point))
1574 neighbour-file))
1575
1576 (defun stgit-revert-file ()
1577 "Revert the file at point, which must be in the index or the
1578 working tree."
1579 (interactive)
1580 (stgit-assert-mode)
1581 (let* ((patched-file (or (stgit-patched-file-at-point)
1582 (error "No file on the current line")))
1583 (patch-name (stgit-patch-name-at-point))
1584 (file-status (stgit-file->status patched-file))
1585 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1586 (stgit-file->cr-to patched-file))
1587 ((eq file-status 'add)
1588 (stgit-file->file patched-file))))
1589 (co-file (cond ((eq file-status 'rename)
1590 (stgit-file->cr-from patched-file))
1591 ((not (memq file-status '(copy add)))
1592 (stgit-file->file patched-file))))
1593 (next-file (stgit-neighbour-file)))
1594
1595 (unless (memq patch-name '(:work :index))
1596 (error "No index or working tree file on this line"))
1597
1598 (when (eq file-status 'ignore)
1599 (error "Cannot revert ignored files"))
1600
1601 (when (eq file-status 'unknown)
1602 (error "Cannot revert unknown files"))
1603
1604 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1605 (when (yes-or-no-p (format "Revert %d file%s? "
1606 nfiles
1607 (if (= nfiles 1) "" "s")))
1608 (stgit-capture-output nil
1609 (when rm-file
1610 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1611 (when co-file
1612 (stgit-run-git "checkout" "HEAD" co-file)))
1613 (stgit-reload)
1614 (stgit-goto-patch patch-name next-file)))))
1615
1616 (defun stgit-revert ()
1617 "Revert the change at point, which must be the index, the work
1618 tree, or a single change in either."
1619 (interactive)
1620 (stgit-assert-mode)
1621 (let ((patched-file (stgit-patched-file-at-point)))
1622 (if patched-file
1623 (stgit-revert-file)
1624 (let* ((patch-name (or (stgit-patch-name-at-point)
1625 (error "No patch or file at point")))
1626 (patch-desc (case patch-name
1627 (:index "index")
1628 (:work "work tree")
1629 (t (error (substitute-command-keys
1630 "Use \\[stgit-delete] to delete a patch"))))))
1631 (when (if (eq patch-name :work)
1632 (stgit-work-tree-empty-p)
1633 (stgit-index-empty-p))
1634 (error (format "There are no changes in the %s to revert"
1635 patch-desc)))
1636 (and (eq patch-name :index)
1637 (not (stgit-work-tree-empty-p))
1638 (error "Cannot revert index as work tree contains unstaged changes"))
1639
1640 (when (yes-or-no-p (format "Revert all changes in the %s? "
1641 patch-desc))
1642 (if (eq patch-name :index)
1643 (stgit-run-git-silent "reset" "--hard" "-q")
1644 (stgit-run-git-silent "checkout" "--" "."))
1645 (stgit-refresh-index)
1646 (stgit-refresh-worktree)
1647 (stgit-goto-patch patch-name))))))
1648
1649 (defun stgit-resolve-file ()
1650 "Resolve conflict in the file at point."
1651 (interactive)
1652 (stgit-assert-mode)
1653 (let* ((patched-file (stgit-patched-file-at-point))
1654 (patch (stgit-patch-at-point))
1655 (patch-name (and patch (stgit-patch->name patch)))
1656 (status (and patched-file (stgit-file->status patched-file))))
1657
1658 (unless (memq patch-name '(:work :index))
1659 (error "No index or working tree file on this line"))
1660
1661 (unless (eq status 'unmerged)
1662 (error "No conflict to resolve at the current line"))
1663
1664 (stgit-capture-output nil
1665 (stgit-move-change-to-index (stgit-file->file patched-file)))
1666
1667 (stgit-reload)))
1668
1669 (defun stgit-push-or-pop-patches (do-push npatches)
1670 "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil)
1671 NPATCHES patches, or all patches if NPATCHES is t."
1672 (stgit-assert-mode)
1673 (stgit-capture-output nil
1674 (apply 'stgit-run
1675 (if do-push "push" "pop")
1676 (if (eq npatches t)
1677 '("--all")
1678 (list "-n" npatches))))
1679 (stgit-reload)
1680 (stgit-refresh-git-status))
1681
1682 (defun stgit-push-next (npatches)
1683 "Push the first unapplied patch.
1684 With numeric prefix argument, push that many patches."
1685 (interactive "p")
1686 (stgit-push-or-pop-patches t npatches))
1687
1688 (defun stgit-pop-next (npatches)
1689 "Pop the topmost applied patch.
1690 With numeric prefix argument, pop that many patches.
1691
1692 If NPATCHES is t, pop all patches."
1693 (interactive "p")
1694 (stgit-push-or-pop-patches nil npatches))
1695
1696 (defun stgit-applied-patches (&optional only-patches)
1697 "Return a list of the applied patches.
1698
1699 If ONLY-PATCHES is not nil, exclude index and work tree."
1700 (let ((states (if only-patches
1701 '(applied top)
1702 '(applied top index work)))
1703 result)
1704 (ewoc-map (lambda (patch)
1705 (when (memq (stgit-patch->status patch) states)
1706 (setq result (cons patch result)))
1707 nil)
1708 stgit-ewoc)
1709 result))
1710
1711 (defun stgit-applied-patchsyms (&optional only-patches)
1712 "Return a list of the symbols of the applied patches.
1713
1714 If ONLY-PATCHES is not nil, exclude index and work tree."
1715 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1716
1717 (defun stgit-push-or-pop ()
1718 "Push or pop the marked patches."
1719 (interactive)
1720 (stgit-assert-mode)
1721 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1722 (applied-syms (stgit-applied-patchsyms t))
1723 (unapplied (set-difference patchsyms applied-syms)))
1724 (stgit-capture-output nil
1725 (apply 'stgit-run
1726 (if unapplied "push" "pop")
1727 "--"
1728 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1729 (stgit-reload))
1730
1731 (defun stgit-goto-target ()
1732 "Return the goto target a point; either a patchsym, :top,
1733 or :bottom."
1734 (let ((patchsym (stgit-patch-name-at-point)))
1735 (cond ((memq patchsym '(:work :index)) nil)
1736 (patchsym)
1737 ((not (next-single-property-change (point) 'patch-data))
1738 :top)
1739 ((not (previous-single-property-change (point) 'patch-data))
1740 :bottom))))
1741
1742 (defun stgit-goto ()
1743 "Go to the patch on the current line.
1744
1745 Push or pop patches to make this patch topmost. Push or pop all
1746 patches if used on a line after or before all patches."
1747 (interactive)
1748 (stgit-assert-mode)
1749 (let ((patchsym (stgit-goto-target)))
1750 (unless patchsym
1751 (error "No patch to go to on this line"))
1752 (case patchsym
1753 (:top (stgit-push-or-pop-patches t t))
1754 (:bottom (stgit-push-or-pop-patches nil t))
1755 (t (stgit-capture-output nil
1756 (stgit-run "goto" patchsym))
1757 (stgit-reload)))))
1758
1759 (defun stgit-id (patchsym)
1760 "Return the git commit id for PATCHSYM.
1761 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1762 (if (keywordp patchsym)
1763 patchsym
1764 (let ((result (with-output-to-string
1765 (stgit-run-silent "id" patchsym))))
1766 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1767 (error "Cannot find commit id for %s" patchsym))
1768 (match-string 1 result))))
1769
1770 (defun stgit-whitespace-diff-arg (arg)
1771 (when (numberp arg)
1772 (cond ((> arg 4) "--ignore-all-space")
1773 ((> arg 1) "--ignore-space-change"))))
1774
1775 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1776 "Show the patch on the current line.
1777
1778 UNMERGED-STAGE is the argument to `git-diff' that that selects
1779 which stage to diff against in the case of unmerged files."
1780 (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace))
1781 (patch-name (stgit-patch-name-at-point t)))
1782 (stgit-capture-output "*StGit patch*"
1783 (case (get-text-property (point) 'entry-type)
1784 ('file
1785 (let* ((patched-file (stgit-patched-file-at-point))
1786 (patch-id (let ((id (stgit-id patch-name)))
1787 (if (and (eq id :index)
1788 (eq (stgit-file->status patched-file)
1789 'unmerged))
1790 :work
1791 id)))
1792 (args (append (and space-arg (list space-arg))
1793 (and (stgit-file->cr-from patched-file)
1794 (list (stgit-find-copies-harder-diff-arg)))
1795 (cond ((eq patch-id :index)
1796 '("--cached"))
1797 ((eq patch-id :work)
1798 (list unmerged-stage))
1799 (t
1800 (list (concat patch-id "^") patch-id)))
1801 '("--")
1802 (if (stgit-file->copy-or-rename patched-file)
1803 (list (stgit-file->cr-from patched-file)
1804 (stgit-file->cr-to patched-file))
1805 (list (stgit-file->file patched-file))))))
1806 (apply 'stgit-run-git "diff" args)))
1807 ('patch
1808 (let* ((patch-id (stgit-id patch-name)))
1809 (if (or (eq patch-id :index) (eq patch-id :work))
1810 (apply 'stgit-run-git "diff"
1811 (stgit-find-copies-harder-diff-arg)
1812 (append (and space-arg (list space-arg))
1813 (if (eq patch-id :index)
1814 '("--cached")
1815 (list unmerged-stage))))
1816 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1817 (and space-arg (list "-O" space-arg))
1818 (list (stgit-patch-name-at-point)))))
1819 (apply 'stgit-run args)))))
1820 (t
1821 (error "No patch or file at point")))
1822 (with-current-buffer standard-output
1823 (goto-char (point-min))
1824 (diff-mode)))))
1825
1826 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1827 `(defun ,name (&optional ignore-whitespace)
1828 ,(format "Show the patch on the current line.
1829
1830 %sWith a prefix argument, ignore whitespace. With a prefix argument
1831 greater than four (e.g., \\[universal-argument] \
1832 \\[universal-argument] \\[%s]), ignore all whitespace."
1833 (if unmerged-action
1834 (format "For unmerged files, %s.\n\n" unmerged-action)
1835 "")
1836 name)
1837 (interactive "p")
1838 (stgit-assert-mode)
1839 (stgit-show-patch ,diff-arg ignore-whitespace)))
1840
1841 (stgit-define-diff stgit-diff
1842 "--ours" nil)
1843 (stgit-define-diff stgit-diff-ours
1844 "--ours"
1845 "diff against our branch")
1846 (stgit-define-diff stgit-diff-theirs
1847 "--theirs"
1848 "diff against their branch")
1849 (stgit-define-diff stgit-diff-base
1850 "--base"
1851 "diff against the merge base")
1852 (stgit-define-diff stgit-diff-combined
1853 "--cc"
1854 "show a combined diff")
1855
1856 (defun stgit-diff-range (&optional ignore-whitespace)
1857 "Show diff for the range of patches between point and the marked patch.
1858
1859 With a prefix argument, ignore whitespace. With a prefix argument
1860 greater than four (e.g., \\[universal-argument] \
1861 \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace."
1862 (interactive "p")
1863 (stgit-assert-mode)
1864 (unless (= (length stgit-marked-patches) 1)
1865 (error "Need exactly one patch marked"))
1866 (let* ((patches (stgit-sort-patches (cons (stgit-patch-name-at-point t t)
1867 stgit-marked-patches)
1868 t))
1869 (first-patch (car patches))
1870 (second-patch (if (cdr patches) (cadr patches) first-patch))
1871 (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace))
1872 (applied (stgit-applied-patchsyms t)))
1873 (unless (and (memq first-patch applied) (memq second-patch applied))
1874 (error "Can only show diff range for applied patches"))
1875 (stgit-capture-output (format "*StGit diff %s..%s*"
1876 first-patch second-patch)
1877 (apply 'stgit-run-git (append '("diff" "--patch-with-stat")
1878 (and whitespace-arg (list whitespace-arg))
1879 (list (format "%s^" (stgit-id first-patch))
1880 (stgit-id second-patch))))
1881 (with-current-buffer standard-output
1882 (goto-char (point-min))
1883 (diff-mode)))))
1884
1885 (defun stgit-move-change-to-index (file &optional force)
1886 "Copies the work tree state of FILE to index, using git add or git rm.
1887
1888 If FORCE is not nil, use --force."
1889 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1890 '("add") '("rm" "-q"))))
1891 (stgit-capture-output "*git output*"
1892 (apply 'stgit-run-git (append op (and force '("--force"))
1893 '("--") (list file))))))
1894
1895 (defun stgit-remove-change-from-index (file)
1896 "Unstages the change in FILE from the index"
1897 (stgit-capture-output "*git output*"
1898 (stgit-run-git "reset" "-q" "--" file)))
1899
1900 (defun stgit-git-index-unmerged-p ()
1901 (let (result)
1902 (with-output-to-string
1903 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1904 "--diff-filter=U"
1905 "--quiet" "HEAD")))))
1906 result))
1907
1908 (defun stgit-file-toggle-index ()
1909 "Move modified file in or out of the index.
1910
1911 Leaves the point where it is, but moves the mark to where the
1912 file ended up. You can then jump to the file with \
1913 \\[exchange-point-and-mark]."
1914 (interactive)
1915 (stgit-assert-mode)
1916 (let* ((patched-file (or (stgit-patched-file-at-point)
1917 (error "No file on the current line")))
1918 (patched-status (stgit-file->status patched-file)))
1919 (when (eq patched-status 'unmerged)
1920 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1921 (let* ((patch (stgit-patch-at-point))
1922 (patch-name (stgit-patch->name patch))
1923 (mark-file (if (eq patched-status 'rename)
1924 (stgit-file->cr-to patched-file)
1925 (stgit-file->file patched-file)))
1926 (point-file (if (eq patched-status 'rename)
1927 (stgit-file->cr-from patched-file)
1928 (stgit-neighbour-file))))
1929
1930 (cond ((eq patch-name :work)
1931 (stgit-move-change-to-index (stgit-file->file patched-file)
1932 (eq patched-status 'ignore)))
1933 ((eq patch-name :index)
1934 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1935 (t
1936 (error "Can only move files between working tree and index")))
1937 (stgit-refresh-worktree)
1938 (stgit-refresh-index)
1939 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1940 (push-mark nil t t)
1941 (stgit-goto-patch patch-name point-file))))
1942
1943 (defun stgit-toggle-index ()
1944 "Move change in or out of the index.
1945
1946 Works on index and work tree, as well as files in either.
1947
1948 Leaves the point where it is, but moves the mark to where the
1949 file ended up. You can then jump to the file with \
1950 \\[exchange-point-and-mark]."
1951 (interactive)
1952 (stgit-assert-mode)
1953 (if (stgit-patched-file-at-point)
1954 (stgit-file-toggle-index)
1955 (let ((patch-name (stgit-patch-name-at-point)))
1956 (unless (memq patch-name '(:index :work))
1957 (error "Can only move changes between working tree and index"))
1958 (when (stgit-git-index-unmerged-p)
1959 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1960 (if (if (eq patch-name :index)
1961 (stgit-index-empty-p)
1962 (stgit-work-tree-empty-p))
1963 (message "No changes to be moved")
1964 (stgit-capture-output nil
1965 (if (eq patch-name :work)
1966 (stgit-run-git "add" "--update")
1967 (stgit-run-git "reset" "--mixed" "-q")))
1968 (stgit-refresh-worktree)
1969 (stgit-refresh-index))
1970 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1971
1972 (defun stgit-edit ()
1973 "Edit the patch on the current line."
1974 (interactive)
1975 (stgit-assert-mode)
1976 (let ((patchsym (stgit-patch-name-at-point t t))
1977 (edit-buf (get-buffer-create "*StGit edit*"))
1978 (dir default-directory))
1979 (log-edit 'stgit-confirm-edit t nil edit-buf)
1980 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1981 (setq default-directory dir)
1982 (let ((standard-output edit-buf))
1983 (save-excursion
1984 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1985
1986 (defun stgit-confirm-edit ()
1987 (interactive)
1988 (let ((file (make-temp-file "stgit-edit-")))
1989 (write-region (point-min) (point-max) file)
1990 (stgit-capture-output nil
1991 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1992 (with-current-buffer log-edit-parent-buffer
1993 (stgit-reload))))
1994
1995 (defun stgit-new (add-sign &optional refresh)
1996 "Create a new patch.
1997 With a prefix argument, include a \"Signed-off-by:\" line at the
1998 end of the patch."
1999 (interactive "P")
2000 (stgit-assert-mode)
2001 (let ((edit-buf (get-buffer-create "*StGit edit*"))
2002 (dir default-directory))
2003 (log-edit 'stgit-confirm-new t nil edit-buf)
2004 (setq default-directory dir)
2005 (set (make-local-variable 'stgit-refresh-after-new) refresh)
2006 (when add-sign
2007 (save-excursion
2008 (let ((standard-output (current-buffer)))
2009 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
2010
2011 (defun stgit-confirm-new ()
2012 (interactive)
2013 (let ((file (make-temp-file "stgit-edit-"))
2014 (refresh stgit-refresh-after-new))
2015 (write-region (point-min) (point-max) file)
2016 (stgit-capture-output nil
2017 (stgit-run "new" "-f" file))
2018 (with-current-buffer log-edit-parent-buffer
2019 (if refresh
2020 (stgit-refresh)
2021 (stgit-reload)))))
2022
2023 (defun stgit-new-and-refresh (add-sign)
2024 "Create a new patch and refresh it with the current changes.
2025
2026 With a prefix argument, include a \"Signed-off-by:\" line at the
2027 end of the patch.
2028
2029 This works just like running `stgit-new' followed by `stgit-refresh'."
2030 (interactive "P")
2031 (stgit-assert-mode)
2032 (stgit-new add-sign t))
2033
2034 (defun stgit-create-patch-name (description)
2035 "Create a patch name from a long description"
2036 (let ((patch ""))
2037 (while (> (length description) 0)
2038 (cond ((string-match "\\`[a-zA-Z_-]+" description)
2039 (setq patch (downcase (concat patch
2040 (match-string 0 description))))
2041 (setq description (substring description (match-end 0))))
2042 ((string-match "\\` +" description)
2043 (setq patch (concat patch "-"))
2044 (setq description (substring description (match-end 0))))
2045 ((string-match "\\`[^a-zA-Z_-]+" description)
2046 (setq description (substring description (match-end 0))))))
2047 (cond ((= (length patch) 0)
2048 "patch")
2049 ((> (length patch) 20)
2050 (substring patch 0 20))
2051 (t patch))))
2052
2053 (defun stgit-delete (patchsyms &optional spill-p)
2054 "Delete the patches in PATCHSYMS.
2055 Interactively, delete the marked patches, or the patch at point.
2056
2057 With a prefix argument, or SPILL-P, spill the patch contents to
2058 the work tree and index."
2059 (interactive (list (stgit-patches-marked-or-at-point t t)
2060 current-prefix-arg))
2061 (stgit-assert-mode)
2062 (unless patchsyms
2063 (error "No patches to delete"))
2064 (when (memq :index patchsyms)
2065 (error "Cannot delete the index"))
2066 (when (memq :work patchsyms)
2067 (error "Cannot delete the work tree"))
2068
2069 (let ((npatches (length patchsyms)))
2070 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
2071 npatches
2072 (if (= 1 npatches) "" "es")
2073 (if spill-p
2074 " (spilling contents to index)"
2075 "")))
2076 (let ((args (if spill-p
2077 (cons "--spill" patchsyms)
2078 patchsyms)))
2079 (stgit-capture-output nil
2080 (apply 'stgit-run "delete" args))
2081 (stgit-reload)))))
2082
2083 (defun stgit-move-patches-target ()
2084 "Return the patchsym indicating a target patch for
2085 `stgit-move-patches'.
2086
2087 This is either the first unmarked patch at or after point, or one
2088 of :top and :bottom if the point is after or before the applied
2089 patches."
2090
2091 (save-excursion
2092 (let (result)
2093 (while (not result)
2094 (let ((patchsym (stgit-patch-name-at-point)))
2095 (cond ((memq patchsym '(:work :index)) (setq result :top))
2096 (patchsym (if (memq patchsym stgit-marked-patches)
2097 (stgit-next-patch)
2098 (setq result patchsym)))
2099 ((re-search-backward "^>" nil t) (setq result :top))
2100 (t (setq result :bottom)))))
2101 result)))
2102
2103 (defun stgit-sort-patches (patchsyms &optional allow-duplicates)
2104 "Returns the list of patches in PATCHSYMS sorted according to
2105 their position in the patch series, bottommost first.
2106
2107 PATCHSYMS must not contain duplicate entries, unless
2108 ALLOW-DUPLICATES is not nil."
2109 (let (sorted-patchsyms
2110 (series (with-output-to-string
2111 (with-current-buffer standard-output
2112 (stgit-run-silent "series" "--noprefix"))))
2113 start)
2114 (while (string-match "^\\(.+\\)" series start)
2115 (let ((patchsym (intern (match-string 1 series))))
2116 (when (memq patchsym patchsyms)
2117 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2118 (setq start (match-end 0)))
2119 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2120
2121 (unless allow-duplicates
2122 (unless (= (length patchsyms) (length sorted-patchsyms))
2123 (error "Internal error")))
2124
2125 sorted-patchsyms))
2126
2127 (defun stgit-move-patches (patchsyms target-patch)
2128 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2129 If TARGET-PATCH is :bottom or :top, move the patches to the
2130 bottom or top of the stack, respectively.
2131
2132 Interactively, move the marked patches to where the point is."
2133 (interactive (list stgit-marked-patches
2134 (stgit-move-patches-target)))
2135 (stgit-assert-mode)
2136 (unless patchsyms
2137 (error "Need at least one patch to move"))
2138
2139 (unless target-patch
2140 (error "Point not at a patch"))
2141
2142 ;; need to have patchsyms sorted by position in the stack
2143 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2144 (stgit-capture-output nil
2145 (if (eq target-patch :top)
2146 (apply 'stgit-run "float" sorted-patchsyms)
2147 (apply 'stgit-run
2148 "sink"
2149 (append (unless (eq target-patch :bottom)
2150 (list "--to" target-patch))
2151 '("--")
2152 sorted-patchsyms)))))
2153 (stgit-reload))
2154
2155 (defun stgit-squash (patchsyms)
2156 "Squash the patches in PATCHSYMS.
2157 Interactively, squash the marked patches.
2158
2159 Unless there are any conflicts, the patches will be merged into
2160 one patch, which will occupy the same spot in the series as the
2161 deepest patch had before the squash."
2162 (interactive (list stgit-marked-patches))
2163 (stgit-assert-mode)
2164 (when (< (length patchsyms) 2)
2165 (error "Need at least two patches to squash"))
2166 (let ((stgit-buffer (current-buffer))
2167 (edit-buf (get-buffer-create "*StGit edit*"))
2168 (dir default-directory)
2169 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2170 (log-edit 'stgit-confirm-squash t nil edit-buf)
2171 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2172 (setq default-directory dir)
2173 (let ((result (let ((standard-output edit-buf))
2174 (save-excursion
2175 (apply 'stgit-run-silent "squash"
2176 "--save-template=-" sorted-patchsyms)))))
2177
2178 ;; stg squash may have reordered the patches or caused conflicts
2179 (with-current-buffer stgit-buffer
2180 (stgit-reload))
2181
2182 (unless (eq 0 result)
2183 (fundamental-mode)
2184 (rename-buffer "*StGit error*")
2185 (resize-temp-buffer-window)
2186 (switch-to-buffer-other-window stgit-buffer)
2187 (error "stg squash failed")))))
2188
2189 (defun stgit-confirm-squash ()
2190 (interactive)
2191 (let ((file (make-temp-file "stgit-edit-")))
2192 (write-region (point-min) (point-max) file)
2193 (stgit-capture-output nil
2194 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2195 (with-current-buffer log-edit-parent-buffer
2196 (stgit-clear-marks)
2197 ;; Go to first marked patch and stay there
2198 (goto-char (point-min))
2199 (re-search-forward (concat "^[>+-]\\*") nil t)
2200 (move-to-column goal-column)
2201 (let ((pos (point)))
2202 (stgit-reload)
2203 (goto-char pos)))))
2204
2205 (defun stgit-help ()
2206 "Display help for the StGit mode."
2207 (interactive)
2208 (describe-function 'stgit-mode))
2209
2210 (defun stgit-undo-or-redo (redo hard)
2211 "Run stg undo or, if REDO is non-nil, stg redo.
2212
2213 If HARD is non-nil, use the --hard flag."
2214 (stgit-assert-mode)
2215 (let ((cmd (if redo "redo" "undo")))
2216 (stgit-capture-output nil
2217 (if arg
2218 (when (or (and (stgit-index-empty-p)
2219 (stgit-work-tree-empty-p))
2220 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2221 cmd)))
2222 (stgit-run cmd "--hard"))
2223 (stgit-run cmd))))
2224 (stgit-reload))
2225
2226 (defun stgit-undo (&optional arg)
2227 "Run stg undo.
2228 With prefix argument, run it with the --hard flag.
2229
2230 See also `stgit-redo'."
2231 (interactive "P")
2232 (stgit-undo-or-redo nil arg))
2233
2234 (defun stgit-redo (&optional arg)
2235 "Run stg redo.
2236 With prefix argument, run it with the --hard flag.
2237
2238 See also `stgit-undo'."
2239 (interactive "P")
2240 (stgit-undo-or-redo t arg))
2241
2242 (defun stgit-refresh (&optional arg)
2243 "Run stg refresh.
2244 If the index contains any changes, only refresh from index.
2245
2246 With prefix argument, refresh the marked patch or the patch under point."
2247 (interactive "P")
2248 (stgit-assert-mode)
2249 (let ((patchargs (if arg
2250 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2251 (when (> (length patches) 1)
2252 (error "Too many patches marked"))
2253 (cons "-p" patches))
2254 nil)))
2255 (unless (stgit-index-empty-p)
2256 (setq patchargs (cons "--index" patchargs)))
2257 (stgit-capture-output nil
2258 (apply 'stgit-run "refresh" patchargs))
2259 (stgit-refresh-git-status))
2260 (stgit-reload))
2261
2262 (defvar stgit-show-worktree nil
2263 "If nil, inhibit showing work tree and index in the stgit buffer.
2264
2265 See also `stgit-show-worktree-mode'.")
2266
2267 (defvar stgit-show-ignored nil
2268 "If nil, inhibit showing files ignored by git.")
2269
2270 (defvar stgit-show-unknown nil
2271 "If nil, inhibit showing files not registered with git.")
2272
2273 (defvar stgit-show-patch-names t
2274 "If nil, inhibit showing patch names.")
2275
2276 (defun stgit-toggle-worktree (&optional arg)
2277 "Toggle the visibility of the work tree.
2278 With ARG, show the work tree if ARG is positive.
2279
2280 Its initial setting is controlled by `stgit-default-show-worktree'.
2281
2282 `stgit-show-worktree-mode' controls where on screen the index and
2283 work tree will show up."
2284 (interactive)
2285 (stgit-assert-mode)
2286 (setq stgit-show-worktree
2287 (if (numberp arg)
2288 (> arg 0)
2289 (not stgit-show-worktree)))
2290 (stgit-reload))
2291
2292 (defun stgit-toggle-ignored (&optional arg)
2293 "Toggle the visibility of files ignored by git in the work
2294 tree. With ARG, show these files if ARG is positive.
2295
2296 Use \\[stgit-toggle-worktree] to show the work tree."
2297 (interactive)
2298 (stgit-assert-mode)
2299 (setq stgit-show-ignored
2300 (if (numberp arg)
2301 (> arg 0)
2302 (not stgit-show-ignored)))
2303 (stgit-reload))
2304
2305 (defun stgit-toggle-unknown (&optional arg)
2306 "Toggle the visibility of files not registered with git in the
2307 work tree. With ARG, show these files if ARG is positive.
2308
2309 Use \\[stgit-toggle-worktree] to show the work tree."
2310 (interactive)
2311 (stgit-assert-mode)
2312 (setq stgit-show-unknown
2313 (if (numberp arg)
2314 (> arg 0)
2315 (not stgit-show-unknown)))
2316 (stgit-reload))
2317
2318 (defun stgit-toggle-patch-names (&optional arg)
2319 "Toggle the visibility of patch names. With ARG, show patch names
2320 if ARG is positive.
2321
2322 The initial setting is controlled by `stgit-default-show-patch-names'."
2323 (interactive)
2324 (stgit-assert-mode)
2325 (setq stgit-show-patch-names
2326 (if (numberp arg)
2327 (> arg 0)
2328 (not stgit-show-patch-names)))
2329 (stgit-reload))
2330
2331 (provide 'stgit)