stgit.el: Add the stgit-file-toggle-index command
[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 (require 'git nil t)
13 (require 'cl)
14 (require 'ewoc)
15
16 (defun stgit (dir)
17 "Manage StGit patches for the tree in DIR."
18 (interactive "DDirectory: \n")
19 (switch-to-stgit-buffer (git-get-top-dir dir))
20 (stgit-reload))
21
22 (unless (fboundp 'git-get-top-dir)
23 (defun git-get-top-dir (dir)
24 "Retrieve the top-level directory of a git tree."
25 (let ((cdup (with-output-to-string
26 (with-current-buffer standard-output
27 (cd dir)
28 (unless (eq 0 (call-process "git" nil t nil
29 "rev-parse" "--show-cdup"))
30 (error "Cannot find top-level git tree for %s" dir))))))
31 (expand-file-name (concat (file-name-as-directory dir)
32 (car (split-string cdup "\n")))))))
33
34 (defun stgit-refresh-git-status (&optional dir)
35 "If it exists, refresh the `git-status' buffer belonging to
36 directory DIR or `default-directory'"
37 (when (and (fboundp 'git-find-status-buffer)
38 (fboundp 'git-refresh-status))
39 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
40 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
41 (when git-status-buffer
42 (with-current-buffer git-status-buffer
43 (git-refresh-status))))))
44
45 (defun stgit-find-buffer (dir)
46 "Return the buffer displaying StGit patches for DIR, or nil if none."
47 (setq dir (file-name-as-directory dir))
48 (let ((buffers (buffer-list)))
49 (while (and buffers
50 (not (with-current-buffer (car buffers)
51 (and (eq major-mode 'stgit-mode)
52 (string= default-directory dir)))))
53 (setq buffers (cdr buffers)))
54 (and buffers (car buffers))))
55
56 (defun switch-to-stgit-buffer (dir)
57 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
58 (setq dir (file-name-as-directory dir))
59 (let ((buffer (stgit-find-buffer dir)))
60 (switch-to-buffer (or buffer
61 (create-stgit-buffer dir)))))
62
63 (defstruct (stgit-patch)
64 status name desc empty files-ewoc)
65
66 (defun stgit-patch-pp (patch)
67 (let ((status (stgit-patch-status patch))
68 (start (point))
69 (name (stgit-patch-name patch)))
70 (case name
71 (:index (insert (propertize " Index" 'face 'italic)))
72 (:work (insert (propertize " Work tree" 'face 'italic)))
73 (t (insert (case status
74 ('applied "+")
75 ('top ">")
76 ('unapplied "-"))
77 (if (memq name stgit-marked-patches)
78 "*" " ")
79 (propertize (format "%-30s"
80 (symbol-name name))
81 'face (case status
82 ('applied 'stgit-applied-patch-face)
83 ('top 'stgit-top-patch-face)
84 ('unapplied 'stgit-unapplied-patch-face)
85 ('index nil)
86 ('work nil)))
87 " "
88 (if (stgit-patch-empty patch) "(empty) " "")
89 (propertize (or (stgit-patch-desc patch) "")
90 'face 'stgit-description-face))))
91 (put-text-property start (point) 'entry-type 'patch)
92 (when (memq name stgit-expanded-patches)
93 (stgit-insert-patch-files patch))
94 (put-text-property start (point) 'patch-data patch)))
95
96 (defun create-stgit-buffer (dir)
97 "Create a buffer for showing StGit patches.
98 Argument DIR is the repository path."
99 (let ((buf (create-file-buffer (concat dir "*stgit*")))
100 (inhibit-read-only t))
101 (with-current-buffer buf
102 (setq default-directory dir)
103 (stgit-mode)
104 (set (make-local-variable 'stgit-ewoc)
105 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
106 (setq buffer-read-only t))
107 buf))
108
109 (defmacro stgit-capture-output (name &rest body)
110 "Capture StGit output and, if there was any output, show it in a window
111 at the end.
112 Returns nil if there was no output."
113 (declare (debug ([&or stringp null] body))
114 (indent 1))
115 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
116 (stgit-dir default-directory)
117 (inhibit-read-only t))
118 (with-current-buffer output-buf
119 (erase-buffer)
120 (setq default-directory stgit-dir)
121 (setq buffer-read-only t))
122 (let ((standard-output output-buf))
123 ,@body)
124 (with-current-buffer output-buf
125 (set-buffer-modified-p nil)
126 (setq buffer-read-only t)
127 (if (< (point-min) (point-max))
128 (display-buffer output-buf t)))))
129
130 (defun stgit-make-run-args (args)
131 "Return a copy of ARGS with its elements converted to strings."
132 (mapcar (lambda (x)
133 ;; don't use (format "%s" ...) to limit type errors
134 (cond ((stringp x) x)
135 ((integerp x) (number-to-string x))
136 ((symbolp x) (symbol-name x))
137 (t
138 (error "Bad element in stgit-make-run-args args: %S" x))))
139 args))
140
141 (defun stgit-run-silent (&rest args)
142 (setq args (stgit-make-run-args args))
143 (apply 'call-process "stg" nil standard-output nil args))
144
145 (defun stgit-run (&rest args)
146 (setq args (stgit-make-run-args args))
147 (let ((msgcmd (mapconcat #'identity args " ")))
148 (message "Running stg %s..." msgcmd)
149 (apply 'call-process "stg" nil standard-output nil args)
150 (message "Running stg %s...done" msgcmd)))
151
152 (defun stgit-run-git (&rest args)
153 (setq args (stgit-make-run-args args))
154 (let ((msgcmd (mapconcat #'identity args " ")))
155 (message "Running git %s..." msgcmd)
156 (apply 'call-process "git" nil standard-output nil args)
157 (message "Running git %s...done" msgcmd)))
158
159 (defun stgit-run-git-silent (&rest args)
160 (setq args (stgit-make-run-args args))
161 (apply 'call-process "git" nil standard-output nil args))
162
163 (defun stgit-index-empty-p ()
164 "Returns non-nil if the index contains no changes from HEAD."
165 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
166
167 (defun stgit-run-series (ewoc)
168 (let ((first-line t))
169 (with-temp-buffer
170 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
171 (goto-char (point-min))
172 (if (not (zerop exit-status))
173 (cond ((looking-at "stg series: \\(.*\\)")
174 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
175 "-- not initialized (run M-x stgit-init)"))
176 ((looking-at ".*")
177 (error "Error running stg: %s"
178 (match-string 0))))
179 (while (not (eobp))
180 (unless (looking-at
181 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
182 (error "Syntax error in output from stg series"))
183 (let* ((state-str (match-string 2))
184 (state (cond ((string= state-str ">") 'top)
185 ((string= state-str "+") 'applied)
186 ((string= state-str "-") 'unapplied))))
187 (ewoc-enter-last ewoc
188 (make-stgit-patch
189 :status state
190 :name (intern (match-string 4))
191 :desc (match-string 5)
192 :empty (string= (match-string 1) "0"))))
193 (setq first-line nil)
194 (forward-line 1))
195 (ewoc-enter-last ewoc
196 (make-stgit-patch
197 :status 'index
198 :name :index
199 :desc nil
200 :empty nil))
201 (ewoc-enter-last ewoc
202 (make-stgit-patch
203 :status 'work
204 :name :work
205 :desc nil
206 :empty nil)))))))
207
208
209 (defun stgit-reload ()
210 "Update the contents of the StGit buffer."
211 (interactive)
212 (let ((inhibit-read-only t)
213 (curline (line-number-at-pos))
214 (curpatch (stgit-patch-name-at-point)))
215 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
216 (ewoc-set-hf stgit-ewoc
217 (concat "Branch: "
218 (propertize
219 (with-temp-buffer
220 (stgit-run-silent "branch")
221 (buffer-substring (point-min) (1- (point-max))))
222 'face 'bold)
223 "\n")
224 "--")
225 (stgit-run-series stgit-ewoc)
226 (if curpatch
227 (stgit-goto-patch curpatch)
228 (goto-line curline)))
229 (stgit-refresh-git-status))
230
231 (defgroup stgit nil
232 "A user interface for the StGit patch maintenance tool."
233 :group 'tools)
234
235 (defface stgit-description-face
236 '((((background dark)) (:foreground "tan"))
237 (((background light)) (:foreground "dark red")))
238 "The face used for StGit descriptions"
239 :group 'stgit)
240
241 (defface stgit-top-patch-face
242 '((((background dark)) (:weight bold :foreground "yellow"))
243 (((background light)) (:weight bold :foreground "purple"))
244 (t (:weight bold)))
245 "The face used for the top patch names"
246 :group 'stgit)
247
248 (defface stgit-applied-patch-face
249 '((((background dark)) (:foreground "light yellow"))
250 (((background light)) (:foreground "purple"))
251 (t ()))
252 "The face used for applied patch names"
253 :group 'stgit)
254
255 (defface stgit-unapplied-patch-face
256 '((((background dark)) (:foreground "gray80"))
257 (((background light)) (:foreground "orchid"))
258 (t ()))
259 "The face used for unapplied patch names"
260 :group 'stgit)
261
262 (defface stgit-modified-file-face
263 '((((class color) (background light)) (:foreground "purple"))
264 (((class color) (background dark)) (:foreground "salmon")))
265 "StGit mode face used for modified file status"
266 :group 'stgit)
267
268 (defface stgit-unmerged-file-face
269 '((((class color) (background light)) (:foreground "red" :bold t))
270 (((class color) (background dark)) (:foreground "red" :bold t)))
271 "StGit mode face used for unmerged file status"
272 :group 'stgit)
273
274 (defface stgit-unknown-file-face
275 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
276 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
277 "StGit mode face used for unknown file status"
278 :group 'stgit)
279
280 (defface stgit-file-permission-face
281 '((((class color) (background light)) (:foreground "green" :bold t))
282 (((class color) (background dark)) (:foreground "green" :bold t)))
283 "StGit mode face used for permission changes."
284 :group 'stgit)
285
286 (defcustom stgit-expand-find-copies-harder
287 nil
288 "Try harder to find copied files when listing patches.
289
290 When not nil, runs git diff-tree with the --find-copies-harder
291 flag, which reduces performance."
292 :type 'boolean
293 :group 'stgit)
294
295 (defconst stgit-file-status-code-strings
296 (mapcar (lambda (arg)
297 (cons (car arg)
298 (propertize (cadr arg) 'face (car (cddr arg)))))
299 '((add "Added" stgit-modified-file-face)
300 (copy "Copied" stgit-modified-file-face)
301 (delete "Deleted" stgit-modified-file-face)
302 (modify "Modified" stgit-modified-file-face)
303 (rename "Renamed" stgit-modified-file-face)
304 (mode-change "Mode change" stgit-modified-file-face)
305 (unmerged "Unmerged" stgit-unmerged-file-face)
306 (unknown "Unknown" stgit-unknown-file-face)))
307 "Alist of code symbols to description strings")
308
309 (defun stgit-file-status-code-as-string (file)
310 "Return stgit status code for FILE as a string"
311 (let* ((code (assq (stgit-file-status file)
312 stgit-file-status-code-strings))
313 (score (stgit-file-cr-score file)))
314 (when code
315 (format "%-11s "
316 (if (and score (/= score 100))
317 (format "%s %s" (cdr code)
318 (propertize (format "%d%%" score)
319 'face 'stgit-description-face))
320 (cdr code))))))
321
322 (defun stgit-file-status-code (str &optional score)
323 "Return stgit status code from git status string"
324 (let ((code (assoc str '(("A" . add)
325 ("C" . copy)
326 ("D" . delete)
327 ("M" . modify)
328 ("R" . rename)
329 ("T" . mode-change)
330 ("U" . unmerged)
331 ("X" . unknown)))))
332 (setq code (if code (cdr code) 'unknown))
333 (when (stringp score)
334 (if (> (length score) 0)
335 (setq score (string-to-number score))
336 (setq score nil)))
337 (if score (cons code score) code)))
338
339 (defconst stgit-file-type-strings
340 '((#o100 . "file")
341 (#o120 . "symlink")
342 (#o160 . "subproject"))
343 "Alist of names of file types")
344
345 (defun stgit-file-type-string (type)
346 "Return string describing file type TYPE (the high bits of file permission).
347 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
348 (let ((type-str (assoc type stgit-file-type-strings)))
349 (or (and type-str (cdr type-str))
350 (format "unknown type %o" type))))
351
352 (defun stgit-file-type-change-string (old-perm new-perm)
353 "Return string describing file type change from OLD-PERM to NEW-PERM.
354 Cf. `stgit-file-type-string'."
355 (let ((old-type (lsh old-perm -9))
356 (new-type (lsh new-perm -9)))
357 (cond ((= old-type new-type) "")
358 ((zerop new-type) "")
359 ((zerop old-type)
360 (if (= new-type #o100)
361 ""
362 (format " (%s)" (stgit-file-type-string new-type))))
363 (t (format " (%s -> %s)"
364 (stgit-file-type-string old-type)
365 (stgit-file-type-string new-type))))))
366
367 (defun stgit-file-mode-change-string (old-perm new-perm)
368 "Return string describing file mode change from OLD-PERM to NEW-PERM.
369 Cf. `stgit-file-type-change-string'."
370 (setq old-perm (logand old-perm #o777)
371 new-perm (logand new-perm #o777))
372 (if (or (= old-perm new-perm)
373 (zerop old-perm)
374 (zerop new-perm))
375 ""
376 (let* ((modified (logxor old-perm new-perm))
377 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
378 (cond ((zerop modified) "")
379 ((and (zerop not-x-modified)
380 (or (and (eq #o111 (logand old-perm #o111))
381 (propertize "-x" 'face 'stgit-file-permission-face))
382 (and (eq #o111 (logand new-perm #o111))
383 (propertize "+x" 'face
384 'stgit-file-permission-face)))))
385 (t (concat (propertize (format "%o" old-perm)
386 'face 'stgit-file-permission-face)
387 (propertize " -> "
388 'face 'stgit-description-face)
389 (propertize (format "%o" new-perm)
390 'face 'stgit-file-permission-face)))))))
391
392 (defstruct (stgit-file)
393 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
394
395 (defun stgit-file-pp (file)
396 (let ((status (stgit-file-status file))
397 (name (if (stgit-file-copy-or-rename file)
398 (concat (stgit-file-cr-from file)
399 (propertize " -> "
400 'face 'stgit-description-face)
401 (stgit-file-cr-to file))
402 (stgit-file-file file)))
403 (mode-change (stgit-file-mode-change-string
404 (stgit-file-old-perm file)
405 (stgit-file-new-perm file)))
406 (start (point)))
407 (insert (format " %-12s%1s%s%s\n"
408 (stgit-file-status-code-as-string file)
409 mode-change
410 name
411 (propertize (stgit-file-type-change-string
412 (stgit-file-old-perm file)
413 (stgit-file-new-perm file))
414 'face 'stgit-description-face)))
415 (add-text-properties start (point)
416 (list 'entry-type 'file
417 'file-data file))))
418
419 (defun stgit-insert-patch-files (patch)
420 "Expand (show modification of) the patch with name PATCHSYM (a
421 symbol) after the line at point.
422 `stgit-expand-find-copies-harder' controls how hard to try to
423 find copied files."
424 (insert "\n")
425 (let* ((patchsym (stgit-patch-name patch))
426 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
427 (args (list "-z" (if stgit-expand-find-copies-harder
428 "--find-copies-harder"
429 "-C")))
430 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
431 (setf (stgit-patch-files-ewoc patch) ewoc)
432 (with-temp-buffer
433 (apply 'stgit-run-git
434 (cond ((eq patchsym :work)
435 `("diff-files" ,@args))
436 ((eq patchsym :index)
437 `("diff-index" ,@args "--cached" "HEAD"))
438 (t
439 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
440 (goto-char (point-min))
441 (unless (or (eobp) (memq patchsym '(:work :index)))
442 (forward-char 41))
443 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
444 (let ((old-perm (string-to-number (match-string 1) 8))
445 (new-perm (string-to-number (match-string 2) 8)))
446 (goto-char (match-end 0))
447 (let ((file
448 (cond ((looking-at
449 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
450 (make-stgit-file
451 :old-perm old-perm
452 :new-perm new-perm
453 :copy-or-rename t
454 :cr-score (string-to-number (match-string 2))
455 :cr-from (match-string 3)
456 :cr-to (match-string 4)
457 :status (stgit-file-status-code (match-string 1))
458 :file (match-string 3)))
459 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
460 (make-stgit-file
461 :old-perm old-perm
462 :new-perm new-perm
463 :copy-or-rename nil
464 :cr-score nil
465 :cr-from nil
466 :cr-to nil
467 :status (stgit-file-status-code (match-string 1))
468 :file (match-string 2))))))
469 (ewoc-enter-last ewoc file))
470 (goto-char (match-end 0))))
471 (unless (ewoc-nth ewoc 0)
472 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
473 'face 'stgit-description-face))))
474 (goto-char end)
475 (delete-char -2)))
476
477 (defun stgit-select-file ()
478 (let ((filename (expand-file-name
479 (stgit-file-file (stgit-patched-file-at-point)))))
480 (unless (file-exists-p filename)
481 (error "File does not exist"))
482 (find-file filename)))
483
484 (defun stgit-select-patch ()
485 (let ((patchname (stgit-patch-name-at-point)))
486 (if (memq patchname stgit-expanded-patches)
487 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
488 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
489 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
490 (move-to-column (stgit-goal-column)))
491
492 (defun stgit-select ()
493 "Expand or collapse the current entry"
494 (interactive)
495 (case (get-text-property (point) 'entry-type)
496 ('patch
497 (stgit-select-patch))
498 ('file
499 (stgit-select-file))
500 (t
501 (error "No patch or file on line"))))
502
503 (defun stgit-find-file-other-window ()
504 "Open file at point in other window"
505 (interactive)
506 (let ((patched-file (stgit-patched-file-at-point)))
507 (unless patched-file
508 (error "No file on the current line"))
509 (let ((filename (expand-file-name (stgit-file-file patched-file))))
510 (unless (file-exists-p filename)
511 (error "File does not exist"))
512 (find-file-other-window filename))))
513
514 (defun stgit-quit ()
515 "Hide the stgit buffer."
516 (interactive)
517 (bury-buffer))
518
519 (defun stgit-git-status ()
520 "Show status using `git-status'."
521 (interactive)
522 (unless (fboundp 'git-status)
523 (error "The stgit-git-status command requires git-status"))
524 (let ((dir default-directory))
525 (save-selected-window
526 (pop-to-buffer nil)
527 (git-status dir))))
528
529 (defun stgit-goal-column ()
530 "Return goal column for the current line"
531 (case (get-text-property (point) 'entry-type)
532 ('patch 2)
533 ('file 4)
534 (t 0)))
535
536 (defun stgit-next-line (&optional arg)
537 "Move cursor vertically down ARG lines"
538 (interactive "p")
539 (next-line arg)
540 (move-to-column (stgit-goal-column)))
541
542 (defun stgit-previous-line (&optional arg)
543 "Move cursor vertically up ARG lines"
544 (interactive "p")
545 (previous-line arg)
546 (move-to-column (stgit-goal-column)))
547
548 (defun stgit-next-patch (&optional arg)
549 "Move cursor down ARG patches."
550 (interactive "p")
551 (ewoc-goto-next stgit-ewoc (or arg 1))
552 (move-to-column goal-column))
553
554 (defun stgit-previous-patch (&optional arg)
555 "Move cursor up ARG patches."
556 (interactive "p")
557 (ewoc-goto-prev stgit-ewoc (or arg 1))
558 (move-to-column goal-column))
559
560 (defvar stgit-mode-hook nil
561 "Run after `stgit-mode' is setup.")
562
563 (defvar stgit-mode-map nil
564 "Keymap for StGit major mode.")
565
566 (unless stgit-mode-map
567 (setq stgit-mode-map (make-keymap))
568 (suppress-keymap stgit-mode-map)
569 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
570 '((" " . stgit-mark)
571 ("m" . stgit-mark)
572 ("\d" . stgit-unmark-up)
573 ("u" . stgit-unmark-down)
574 ("?" . stgit-help)
575 ("h" . stgit-help)
576 ("\C-p" . stgit-previous-line)
577 ("\C-n" . stgit-next-line)
578 ([up] . stgit-previous-line)
579 ([down] . stgit-next-line)
580 ("p" . stgit-previous-patch)
581 ("n" . stgit-next-patch)
582 ("\M-{" . stgit-previous-patch)
583 ("\M-}" . stgit-next-patch)
584 ("s" . stgit-git-status)
585 ("g" . stgit-reload)
586 ("r" . stgit-refresh)
587 ("\C-c\C-r" . stgit-rename)
588 ("e" . stgit-edit)
589 ("M" . stgit-move-patches)
590 ("S" . stgit-squash)
591 ("N" . stgit-new)
592 ("R" . stgit-repair)
593 ("C" . stgit-commit)
594 ("U" . stgit-uncommit)
595 ("\r" . stgit-select)
596 ("o" . stgit-find-file-other-window)
597 ("i" . stgit-file-toggle-index)
598 (">" . stgit-push-next)
599 ("<" . stgit-pop-next)
600 ("P" . stgit-push-or-pop)
601 ("G" . stgit-goto)
602 ("=" . stgit-show)
603 ("D" . stgit-delete)
604 ([(control ?/)] . stgit-undo)
605 ("\C-_" . stgit-undo)
606 ("B" . stgit-branch)
607 ("q" . stgit-quit))))
608
609 (defun stgit-mode ()
610 "Major mode for interacting with StGit.
611 Commands:
612 \\{stgit-mode-map}"
613 (kill-all-local-variables)
614 (buffer-disable-undo)
615 (setq mode-name "StGit"
616 major-mode 'stgit-mode
617 goal-column 2)
618 (use-local-map stgit-mode-map)
619 (set (make-local-variable 'list-buffers-directory) default-directory)
620 (set (make-local-variable 'stgit-marked-patches) nil)
621 (set (make-local-variable 'stgit-expanded-patches) nil)
622 (set-variable 'truncate-lines 't)
623 (add-hook 'after-save-hook 'stgit-update-saved-file)
624 (run-hooks 'stgit-mode-hook))
625
626 (defun stgit-update-saved-file ()
627 (let* ((file (expand-file-name buffer-file-name))
628 (dir (file-name-directory file))
629 (gitdir (condition-case nil (git-get-top-dir dir)
630 (error nil)))
631 (buffer (and gitdir (stgit-find-buffer gitdir))))
632 (when buffer
633 (with-current-buffer buffer
634 ;; FIXME: just invalidate ewoc node
635 (stgit-reload)))))
636
637 (defun stgit-add-mark (patchsym)
638 "Mark the patch PATCHSYM."
639 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
640
641 (defun stgit-remove-mark (patchsym)
642 "Unmark the patch PATCHSYM."
643 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
644
645 (defun stgit-clear-marks ()
646 "Unmark all patches."
647 (setq stgit-marked-patches '()))
648
649 (defun stgit-patch-at-point (&optional cause-error)
650 (get-text-property (point) 'patch-data))
651
652 (defun stgit-patch-name-at-point (&optional cause-error)
653 "Return the patch name on the current line as a symbol.
654 If CAUSE-ERROR is not nil, signal an error if none found."
655 (let ((patch (stgit-patch-at-point)))
656 (cond (patch
657 (stgit-patch-name patch))
658 (cause-error
659 (error "No patch on this line")))))
660
661 (defun stgit-patched-file-at-point ()
662 (get-text-property (point) 'file-data))
663
664 (defun stgit-patches-marked-or-at-point ()
665 "Return the symbols of the marked patches, or the patch on the current line."
666 (if stgit-marked-patches
667 stgit-marked-patches
668 (let ((patch (stgit-patch-name-at-point)))
669 (if patch
670 (list patch)
671 '()))))
672
673 (defun stgit-goto-patch (patchsym)
674 "Move point to the line containing patch PATCHSYM.
675 If that patch cannot be found, do nothing."
676 (let ((node (ewoc-nth stgit-ewoc 0)))
677 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
678 patchsym)))
679 (setq node (ewoc-next stgit-ewoc node)))
680 (when node
681 (ewoc-goto-node stgit-ewoc node)
682 (move-to-column goal-column))))
683
684 (defun stgit-init ()
685 "Run stg init."
686 (interactive)
687 (stgit-capture-output nil
688 (stgit-run "init"))
689 (stgit-reload))
690
691 (defun stgit-mark ()
692 "Mark the patch under point."
693 (interactive)
694 (let* ((node (ewoc-locate stgit-ewoc))
695 (patch (ewoc-data node)))
696 (stgit-add-mark (stgit-patch-name patch))
697 (ewoc-invalidate stgit-ewoc node))
698 (stgit-next-patch))
699
700 (defun stgit-unmark-up ()
701 "Remove mark from the patch on the previous line."
702 (interactive)
703 (stgit-previous-patch)
704 (let* ((node (ewoc-locate stgit-ewoc))
705 (patch (ewoc-data node)))
706 (stgit-remove-mark (stgit-patch-name patch))
707 (ewoc-invalidate stgit-ewoc node))
708 (move-to-column (stgit-goal-column)))
709
710 (defun stgit-unmark-down ()
711 "Remove mark from the patch on the current line."
712 (interactive)
713 (let* ((node (ewoc-locate stgit-ewoc))
714 (patch (ewoc-data node)))
715 (stgit-remove-mark (stgit-patch-name patch))
716 (ewoc-invalidate stgit-ewoc node))
717 (stgit-next-patch))
718
719 (defun stgit-rename (name)
720 "Rename the patch under point to NAME."
721 (interactive (list (read-string "Patch name: "
722 (symbol-name (stgit-patch-name-at-point t)))))
723 (let ((old-patchsym (stgit-patch-name-at-point t)))
724 (stgit-capture-output nil
725 (stgit-run "rename" old-patchsym name))
726 (let ((name-sym (intern name)))
727 (when (memq old-patchsym stgit-expanded-patches)
728 (setq stgit-expanded-patches
729 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
730 (when (memq old-patchsym stgit-marked-patches)
731 (setq stgit-marked-patches
732 (cons name-sym (delq old-patchsym stgit-marked-patches))))
733 (stgit-reload)
734 (stgit-goto-patch name-sym))))
735
736 (defun stgit-repair ()
737 "Run stg repair."
738 (interactive)
739 (stgit-capture-output nil
740 (stgit-run "repair"))
741 (stgit-reload))
742
743 (defun stgit-available-branches ()
744 "Returns a list of the available stg branches"
745 (let ((output (with-output-to-string
746 (stgit-run "branch" "--list")))
747 (start 0)
748 result)
749 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
750 (setq result (cons (match-string 1 output) result))
751 (setq start (match-end 0)))
752 result))
753
754 (defun stgit-branch (branch)
755 "Switch to branch BRANCH."
756 (interactive (list (completing-read "Switch to branch: "
757 (stgit-available-branches))))
758 (stgit-capture-output nil (stgit-run "branch" "--" branch))
759 (stgit-reload))
760
761 (defun stgit-commit (count)
762 "Run stg commit on COUNT commits.
763 Interactively, the prefix argument is used as COUNT."
764 (interactive "p")
765 (stgit-capture-output nil (stgit-run "commit" "-n" count))
766 (stgit-reload))
767
768 (defun stgit-uncommit (count)
769 "Run stg uncommit on COUNT commits.
770 Interactively, the prefix argument is used as COUNT."
771 (interactive "p")
772 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
773 (stgit-reload))
774
775 (defun stgit-push-next (npatches)
776 "Push the first unapplied patch.
777 With numeric prefix argument, push that many patches."
778 (interactive "p")
779 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
780 (stgit-reload)
781 (stgit-refresh-git-status))
782
783 (defun stgit-pop-next (npatches)
784 "Pop the topmost applied patch.
785 With numeric prefix argument, pop that many patches."
786 (interactive "p")
787 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
788 (stgit-reload)
789 (stgit-refresh-git-status))
790
791 (defun stgit-applied-at-point ()
792 "Is the patch on the current line applied?"
793 (save-excursion
794 (beginning-of-line)
795 (looking-at "[>+]")))
796
797 (defun stgit-push-or-pop ()
798 "Push or pop the patch on the current line."
799 (interactive)
800 (let ((patchsym (stgit-patch-name-at-point t))
801 (applied (stgit-applied-at-point)))
802 (stgit-capture-output nil
803 (stgit-run (if applied "pop" "push") patchsym))
804 (stgit-reload)))
805
806 (defun stgit-goto ()
807 "Go to the patch on the current line."
808 (interactive)
809 (let ((patchsym (stgit-patch-name-at-point t)))
810 (stgit-capture-output nil
811 (stgit-run "goto" patchsym))
812 (stgit-reload)))
813
814 (defun stgit-id (patchsym)
815 "Return the git commit id for PATCHSYM.
816 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
817 (if (keywordp patchsym)
818 patchsym
819 (let ((result (with-output-to-string
820 (stgit-run-silent "id" patchsym))))
821 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
822 (error "Cannot find commit id for %s" patchsym))
823 (match-string 1 result))))
824
825 (defun stgit-show ()
826 "Show the patch on the current line."
827 (interactive)
828 (stgit-capture-output "*StGit patch*"
829 (case (get-text-property (point) 'entry-type)
830 ('file
831 (let* ((patched-file (stgit-patched-file-at-point))
832 (patch-name (stgit-patch-name-at-point))
833 (patch-id (stgit-id patch-name))
834 (args (append (and (stgit-file-cr-from patched-file)
835 (if stgit-expand-find-copies-harder
836 '("--find-copies-harder")
837 '("-C")))
838 (cond ((eq patch-id :index)
839 '("--cached"))
840 ((eq patch-id :work)
841 nil)
842 (t
843 (list (concat patch-id "^") patch-id)))
844 '("--")
845 (if (stgit-file-copy-or-rename patched-file)
846 (list (stgit-file-cr-from patched-file)
847 (stgit-file-cr-to patched-file))
848 (list (stgit-file-file patched-file))))))
849 (apply 'stgit-run-git "diff" args)))
850 ('patch
851 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
852 (stgit-patch-name-at-point)))
853 (t
854 (error "No patch or file at point")))
855 (with-current-buffer standard-output
856 (goto-char (point-min))
857 (diff-mode))))
858
859 (defun stgit-move-change-to-index (file status)
860 "Copies the workspace state of FILE to index, using git add or git rm"
861 (let ((op (if (file-exists-p file) "add" "rm")))
862 (stgit-capture-output "*git output*"
863 (stgit-run-git op "--" file))))
864
865 (defun stgit-remove-change-from-index (file status)
866 "Unstages the change in FILE from the index"
867 (stgit-capture-output "*git output*"
868 (stgit-run-git "reset" "-q" "--" file)))
869
870 (defun stgit-file-toggle-index ()
871 "Move modified file in or out of the index."
872 (interactive)
873 (let ((patched-file (stgit-patched-file-at-point)))
874 (unless patched-file
875 (error "No file on the current line"))
876 (let ((patch-name (stgit-patch-name-at-point)))
877 (cond ((eq patch-name :work)
878 (stgit-move-change-to-index (stgit-file-file patched-file)
879 (stgit-file-status patched-file)))
880 ((eq patch-name :index)
881 (stgit-remove-change-from-index (stgit-file-file patched-file)
882 (stgit-file-status patched-file)))
883 (t
884 (error "Can only move files in the working tree to index")))))
885 ;; FIXME: invalidate ewoc
886 (stgit-reload))
887
888 (defun stgit-edit ()
889 "Edit the patch on the current line."
890 (interactive)
891 (let ((patchsym (stgit-patch-name-at-point t))
892 (edit-buf (get-buffer-create "*StGit edit*"))
893 (dir default-directory))
894 (log-edit 'stgit-confirm-edit t nil edit-buf)
895 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
896 (setq default-directory dir)
897 (let ((standard-output edit-buf))
898 (stgit-run-silent "edit" "--save-template=-" patchsym))))
899
900 (defun stgit-confirm-edit ()
901 (interactive)
902 (let ((file (make-temp-file "stgit-edit-")))
903 (write-region (point-min) (point-max) file)
904 (stgit-capture-output nil
905 (stgit-run "edit" "-f" file stgit-edit-patchsym))
906 (with-current-buffer log-edit-parent-buffer
907 (stgit-reload))))
908
909 (defun stgit-new (add-sign)
910 "Create a new patch.
911 With a prefix argument, include a \"Signed-off-by:\" line at the
912 end of the patch."
913 (interactive "P")
914 (let ((edit-buf (get-buffer-create "*StGit edit*"))
915 (dir default-directory))
916 (log-edit 'stgit-confirm-new t nil edit-buf)
917 (setq default-directory dir)
918 (when add-sign
919 (save-excursion
920 (let ((standard-output (current-buffer)))
921 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
922
923 (defun stgit-confirm-new ()
924 (interactive)
925 (let ((file (make-temp-file "stgit-edit-")))
926 (write-region (point-min) (point-max) file)
927 (stgit-capture-output nil
928 (stgit-run "new" "-f" file))
929 (with-current-buffer log-edit-parent-buffer
930 (stgit-reload))))
931
932 (defun stgit-create-patch-name (description)
933 "Create a patch name from a long description"
934 (let ((patch ""))
935 (while (> (length description) 0)
936 (cond ((string-match "\\`[a-zA-Z_-]+" description)
937 (setq patch (downcase (concat patch
938 (match-string 0 description))))
939 (setq description (substring description (match-end 0))))
940 ((string-match "\\` +" description)
941 (setq patch (concat patch "-"))
942 (setq description (substring description (match-end 0))))
943 ((string-match "\\`[^a-zA-Z_-]+" description)
944 (setq description (substring description (match-end 0))))))
945 (cond ((= (length patch) 0)
946 "patch")
947 ((> (length patch) 20)
948 (substring patch 0 20))
949 (t patch))))
950
951 (defun stgit-delete (patchsyms &optional spill-p)
952 "Delete the patches in PATCHSYMS.
953 Interactively, delete the marked patches, or the patch at point.
954
955 With a prefix argument, or SPILL-P, spill the patch contents to
956 the work tree and index."
957 (interactive (list (stgit-patches-marked-or-at-point)
958 current-prefix-arg))
959 (unless patchsyms
960 (error "No patches to delete"))
961 (let ((npatches (length patchsyms)))
962 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
963 npatches
964 (if (= 1 npatches) "" "es")
965 (if spill-p
966 " (spilling contents to index)"
967 "")))
968 (let ((args (if spill-p
969 (cons "--spill" patchsyms)
970 patchsyms)))
971 (stgit-capture-output nil
972 (apply 'stgit-run "delete" args))
973 (stgit-reload)))))
974
975 (defun stgit-move-patches-target ()
976 "Return the patchsym indicating a target patch for
977 `stgit-move-patches'.
978
979 This is either the patch at point, or one of :top and :bottom, if
980 the point is after or before the applied patches."
981
982 (let ((patchsym (stgit-patch-name-at-point)))
983 (cond (patchsym patchsym)
984 ((save-excursion (re-search-backward "^>" nil t)) :top)
985 (t :bottom))))
986
987 (defun stgit-sort-patches (patchsyms)
988 "Returns the list of patches in PATCHSYMS sorted according to
989 their position in the patch series, bottommost first.
990
991 PATCHSYMS may not contain duplicate entries."
992 (let (sorted-patchsyms
993 (series (with-output-to-string
994 (with-current-buffer standard-output
995 (stgit-run-silent "series" "--noprefix"))))
996 start)
997 (while (string-match "^\\(.+\\)" series start)
998 (let ((patchsym (intern (match-string 1 series))))
999 (when (memq patchsym patchsyms)
1000 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1001 (setq start (match-end 0)))
1002 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1003
1004 (unless (= (length patchsyms) (length sorted-patchsyms))
1005 (error "Internal error"))
1006
1007 sorted-patchsyms))
1008
1009 (defun stgit-move-patches (patchsyms target-patch)
1010 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1011 If TARGET-PATCH is :bottom or :top, move the patches to the
1012 bottom or top of the stack, respectively.
1013
1014 Interactively, move the marked patches to where the point is."
1015 (interactive (list stgit-marked-patches
1016 (stgit-move-patches-target)))
1017 (unless patchsyms
1018 (error "Need at least one patch to move"))
1019
1020 (unless target-patch
1021 (error "Point not at a patch"))
1022
1023 (if (eq target-patch :top)
1024 (stgit-capture-output nil
1025 (apply 'stgit-run "float" patchsyms))
1026
1027 ;; need to have patchsyms sorted by position in the stack
1028 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
1029 (while sorted-patchsyms
1030 (setq sorted-patchsyms
1031 (and (stgit-capture-output nil
1032 (if (eq target-patch :bottom)
1033 (stgit-run "sink" "--" (car sorted-patchsyms))
1034 (stgit-run "sink" "--to" target-patch "--"
1035 (car sorted-patchsyms))))
1036 (cdr sorted-patchsyms))))))
1037 (stgit-reload))
1038
1039 (defun stgit-squash (patchsyms)
1040 "Squash the patches in PATCHSYMS.
1041 Interactively, squash the marked patches.
1042
1043 Unless there are any conflicts, the patches will be merged into
1044 one patch, which will occupy the same spot in the series as the
1045 deepest patch had before the squash."
1046 (interactive (list stgit-marked-patches))
1047 (when (< (length patchsyms) 2)
1048 (error "Need at least two patches to squash"))
1049 (let ((stgit-buffer (current-buffer))
1050 (edit-buf (get-buffer-create "*StGit edit*"))
1051 (dir default-directory)
1052 (sorted-patchsyms (stgit-sort-patches patchsyms)))
1053 (log-edit 'stgit-confirm-squash t nil edit-buf)
1054 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
1055 (setq default-directory dir)
1056 (let ((result (let ((standard-output edit-buf))
1057 (apply 'stgit-run-silent "squash"
1058 "--save-template=-" sorted-patchsyms))))
1059
1060 ;; stg squash may have reordered the patches or caused conflicts
1061 (with-current-buffer stgit-buffer
1062 (stgit-reload))
1063
1064 (unless (eq 0 result)
1065 (fundamental-mode)
1066 (rename-buffer "*StGit error*")
1067 (resize-temp-buffer-window)
1068 (switch-to-buffer-other-window stgit-buffer)
1069 (error "stg squash failed")))))
1070
1071 (defun stgit-confirm-squash ()
1072 (interactive)
1073 (let ((file (make-temp-file "stgit-edit-")))
1074 (write-region (point-min) (point-max) file)
1075 (stgit-capture-output nil
1076 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
1077 (with-current-buffer log-edit-parent-buffer
1078 (stgit-clear-marks)
1079 ;; Go to first marked patch and stay there
1080 (goto-char (point-min))
1081 (re-search-forward (concat "^[>+-]\\*") nil t)
1082 (move-to-column goal-column)
1083 (let ((pos (point)))
1084 (stgit-reload)
1085 (goto-char pos)))))
1086
1087 (defun stgit-help ()
1088 "Display help for the StGit mode."
1089 (interactive)
1090 (describe-function 'stgit-mode))
1091
1092 (defun stgit-undo (&optional arg)
1093 "Run stg undo.
1094 With prefix argument, run it with the --hard flag."
1095 (interactive "P")
1096 (stgit-capture-output nil
1097 (if arg
1098 (stgit-run "undo" "--hard")
1099 (stgit-run "undo")))
1100 (stgit-reload))
1101
1102 (defun stgit-refresh (&optional arg)
1103 "Run stg refresh.
1104 With prefix argument, refresh the marked patch or the patch under point."
1105 (interactive "P")
1106 (let ((patchargs (if arg
1107 (let ((patches (stgit-patches-marked-or-at-point)))
1108 (cond ((null patches)
1109 (error "No patch to update"))
1110 ((> (length patches) 1)
1111 (error "Too many patches selected"))
1112 (t
1113 (cons "-p" patches))))
1114 nil)))
1115 (stgit-capture-output nil
1116 (apply 'stgit-run "refresh" patchargs))
1117 (stgit-refresh-git-status))
1118 (stgit-reload))
1119
1120 (provide 'stgit)