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