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