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