Steal Documentation/SubmittingPatches from Git
[stgit] / contrib / stgit.el
... / ...
CommitLineData
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
14(defun stgit (dir)
15 "Manage StGit patches for the tree in DIR."
16 (interactive "DDirectory: \n")
17 (switch-to-stgit-buffer (git-get-top-dir dir))
18 (stgit-reload))
19
20(unless (fboundp 'git-get-top-dir)
21 (defun git-get-top-dir (dir)
22 "Retrieve the top-level directory of a git tree."
23 (let ((cdup (with-output-to-string
24 (with-current-buffer standard-output
25 (cd dir)
26 (unless (eq 0 (call-process "git" nil t nil
27 "rev-parse" "--show-cdup"))
28 (error "cannot find top-level git tree for %s." dir))))))
29 (expand-file-name (concat (file-name-as-directory dir)
30 (car (split-string cdup "\n")))))))
31
32(defun stgit-refresh-git-status (&optional dir)
33 "If it exists, refresh the `git-status' buffer belonging to
34directory DIR or `default-directory'"
35 (when (and (fboundp 'git-find-status-buffer)
36 (fboundp 'git-refresh-status))
37 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
38 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
39 (when git-status-buffer
40 (with-current-buffer git-status-buffer
41 (git-refresh-status))))))
42
43(defun switch-to-stgit-buffer (dir)
44 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
45 (setq dir (file-name-as-directory dir))
46 (let ((buffers (buffer-list)))
47 (while (and buffers
48 (not (with-current-buffer (car buffers)
49 (and (eq major-mode 'stgit-mode)
50 (string= default-directory dir)))))
51 (setq buffers (cdr buffers)))
52 (switch-to-buffer (if buffers
53 (car buffers)
54 (create-stgit-buffer dir)))))
55
56(defun create-stgit-buffer (dir)
57 "Create a buffer for showing StGit patches.
58Argument DIR is the repository path."
59 (let ((buf (create-file-buffer (concat dir "*stgit*")))
60 (inhibit-read-only t))
61 (with-current-buffer buf
62 (setq default-directory dir)
63 (stgit-mode)
64 (setq buffer-read-only t))
65 buf))
66
67(defmacro stgit-capture-output (name &rest body)
68 "Capture StGit output and show it in a window at the end."
69 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
70 (stgit-dir default-directory)
71 (inhibit-read-only t))
72 (with-current-buffer output-buf
73 (erase-buffer)
74 (setq default-directory stgit-dir)
75 (setq buffer-read-only t))
76 (let ((standard-output output-buf))
77 ,@body)
78 (with-current-buffer output-buf
79 (set-buffer-modified-p nil)
80 (setq buffer-read-only t)
81 (if (< (point-min) (point-max))
82 (display-buffer output-buf t)))))
83(put 'stgit-capture-output 'lisp-indent-function 1)
84
85(defun stgit-run-silent (&rest args)
86 (apply 'call-process "stg" nil standard-output nil args))
87
88(defun stgit-run (&rest args)
89 (let ((msgcmd (mapconcat #'identity args " ")))
90 (message "Running stg %s..." msgcmd)
91 (apply 'call-process "stg" nil standard-output nil args)
92 (message "Running stg %s...done" msgcmd)))
93
94(defun stgit-run-git (&rest args)
95 (let ((msgcmd (mapconcat #'identity args " ")))
96 (message "Running git %s..." msgcmd)
97 (apply 'call-process "git" nil standard-output nil args)
98 (message "Running git %s...done" msgcmd)))
99
100(defun stgit-run-git-silent (&rest args)
101 (apply 'call-process "git" nil standard-output nil args))
102
103(defun stgit-reload ()
104 "Update the contents of the StGit buffer."
105 (interactive)
106 (let ((inhibit-read-only t)
107 (curline (line-number-at-pos))
108 (curpatch (stgit-patch-at-point)))
109 (erase-buffer)
110 (insert "Branch: ")
111 (stgit-run-silent "branch")
112 (stgit-run-silent "series" "--description")
113 (stgit-rescan)
114 (if curpatch
115 (stgit-goto-patch curpatch)
116 (goto-line curline)))
117 (stgit-refresh-git-status))
118
119(defgroup stgit nil
120 "A user interface for the StGit patch maintenance tool."
121 :group 'tools)
122
123(defface stgit-description-face
124 '((((background dark)) (:foreground "tan"))
125 (((background light)) (:foreground "dark red")))
126 "The face used for StGit descriptions"
127 :group 'stgit)
128
129(defface stgit-top-patch-face
130 '((((background dark)) (:weight bold :foreground "yellow"))
131 (((background light)) (:weight bold :foreground "purple"))
132 (t (:weight bold)))
133 "The face used for the top patch names"
134 :group 'stgit)
135
136(defface stgit-applied-patch-face
137 '((((background dark)) (:foreground "light yellow"))
138 (((background light)) (:foreground "purple"))
139 (t ()))
140 "The face used for applied patch names"
141 :group 'stgit)
142
143(defface stgit-unapplied-patch-face
144 '((((background dark)) (:foreground "gray80"))
145 (((background light)) (:foreground "orchid"))
146 (t ()))
147 "The face used for unapplied patch names"
148 :group 'stgit)
149
150(defface stgit-modified-file-face
151 '((((class color) (background light)) (:foreground "purple"))
152 (((class color) (background dark)) (:foreground "salmon")))
153 "StGit mode face used for modified file status"
154 :group 'stgit)
155
156(defface stgit-unmerged-file-face
157 '((((class color) (background light)) (:foreground "red" :bold t))
158 (((class color) (background dark)) (:foreground "red" :bold t)))
159 "StGit mode face used for unmerged file status"
160 :group 'stgit)
161
162(defface stgit-unknown-file-face
163 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
164 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
165 "StGit mode face used for unknown file status"
166 :group 'stgit)
167
168(defface stgit-file-permission-face
169 '((((class color) (background light)) (:foreground "green" :bold t))
170 (((class color) (background dark)) (:foreground "green" :bold t)))
171 "StGit mode face used for permission changes."
172 :group 'stgit)
173
174(defcustom stgit-expand-find-copies-harder
175 nil
176 "Try harder to find copied files when listing patches.
177
178When not nil, runs git diff-tree with the --find-copies-harder
179flag, which reduces performance."
180 :type 'boolean
181 :group 'stgit)
182
183(defconst stgit-file-status-code-strings
184 (mapcar (lambda (arg)
185 (cons (car arg)
186 (propertize (cadr arg) 'face (car (cddr arg)))))
187 '((add "Added" stgit-modified-file-face)
188 (copy "Copied" stgit-modified-file-face)
189 (delete "Deleted" stgit-modified-file-face)
190 (modify "Modified" stgit-modified-file-face)
191 (rename "Renamed" stgit-modified-file-face)
192 (mode-change "Mode change" stgit-modified-file-face)
193 (unmerged "Unmerged" stgit-unmerged-file-face)
194 (unknown "Unknown" stgit-unknown-file-face)))
195 "Alist of code symbols to description strings")
196
197(defun stgit-file-status-code-as-string (code)
198 "Return stgit status code as string"
199 (let ((str (assq (if (consp code) (car code) code)
200 stgit-file-status-code-strings)))
201 (when str
202 (format "%-11s "
203 (if (and str (consp code) (/= (cdr code) 100))
204 (format "%s %s" (cdr str)
205 (propertize (format "%d%%" (cdr code))
206 'face 'stgit-description-face))
207 (cdr str))))))
208
209(defun stgit-file-status-code (str &optional score)
210 "Return stgit status code from git status string"
211 (let ((code (assoc str '(("A" . add)
212 ("C" . copy)
213 ("D" . delete)
214 ("M" . modify)
215 ("R" . rename)
216 ("T" . mode-change)
217 ("U" . unmerged)
218 ("X" . unknown)))))
219 (setq code (if code (cdr code) 'unknown))
220 (when (stringp score)
221 (if (> (length score) 0)
222 (setq score (string-to-number score))
223 (setq score nil)))
224 (if score (cons code score) code)))
225
226(defconst stgit-file-type-strings
227 '((#o100 . "file")
228 (#o120 . "symlink")
229 (#o160 . "subproject"))
230 "Alist of names of file types")
231
232(defun stgit-file-type-string (type)
233 (let ((type-str (assoc type stgit-file-type-strings)))
234 (or (and type-str (cdr type-str))
235 (format "unknown type %o" type))))
236
237(defun stgit-file-type-change-string (old-perm new-perm)
238 (let ((old-type (lsh old-perm -9))
239 (new-type (lsh new-perm -9)))
240 (cond ((= old-type new-type) "")
241 ((zerop new-type) "")
242 ((zerop old-type)
243 (if (= new-type #o100)
244 ""
245 (format " (%s)" (stgit-file-type-string new-type))))
246 (t (format " (%s -> %s)"
247 (stgit-file-type-string old-type)
248 (stgit-file-type-string new-type))))))
249
250(defun stgit-file-mode-change-string (old-perm new-perm)
251 (setq old-perm (logand old-perm #o777)
252 new-perm (logand new-perm #o777))
253 (if (or (= old-perm new-perm)
254 (zerop old-perm)
255 (zerop new-perm))
256 ""
257 (let* ((modified (logxor old-perm new-perm))
258 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
259 (cond ((zerop modified) "")
260 ((and (zerop not-x-modified)
261 (or (and (eq #o111 (logand old-perm #o111))
262 (propertize "-x" 'face 'stgit-file-permission-face))
263 (and (eq #o111 (logand new-perm #o111))
264 (propertize "+x" 'face
265 'stgit-file-permission-face)))))
266 (t (concat (propertize (format "%o" old-perm)
267 'face 'stgit-file-permission-face)
268 (propertize " -> "
269 'face 'stgit-description-face)
270 (propertize (format "%o" new-perm)
271 'face 'stgit-file-permission-face)))))))
272
273(defun stgit-expand-patch (patchsym)
274 (save-excursion
275 (forward-line)
276 (let* ((start (point))
277 (result (with-output-to-string
278 (stgit-run-git "diff-tree" "-r" "-z"
279 (if stgit-expand-find-copies-harder
280 "--find-copies-harder"
281 "-C")
282 (stgit-id (symbol-name patchsym))))))
283 (let (mstart)
284 (while (string-match "\0:\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} \\(\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\\|\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\\)"
285 result mstart)
286 (let ((copy-or-rename (match-string 4 result))
287 (old-perm (read (format "#o%s" (match-string 1 result))))
288 (new-perm (read (format "#o%s" (match-string 2 result))))
289 (line-start (point))
290 status
291 change
292 properties)
293 (insert " ")
294 (if copy-or-rename
295 (let ((cr-score (match-string 5 result))
296 (cr-from-file (match-string 6 result))
297 (cr-to-file (match-string 7 result)))
298 (setq status (stgit-file-status-code copy-or-rename
299 cr-score)
300 properties (list 'stgit-old-file cr-from-file
301 'stgit-new-file cr-to-file)
302 change (concat
303 cr-from-file
304 (propertize " -> "
305 'face 'stgit-description-face)
306 cr-to-file)))
307 (setq status (stgit-file-status-code (match-string 8 result))
308 properties (list 'stgit-file (match-string 9 result))
309 change (match-string 9 result)))
310
311 (let ((mode-change (stgit-file-mode-change-string old-perm
312 new-perm)))
313 (insert (format "%-12s" (stgit-file-status-code-as-string
314 status))
315 mode-change
316 (if (> (length mode-change) 0) " " "")
317 change
318 (propertize (stgit-file-type-change-string old-perm
319 new-perm)
320 'face 'stgit-description-face)
321 ?\n))
322 (add-text-properties line-start (point) properties))
323 (setq mstart (match-end 0))))
324 (when (= start (point))
325 (insert " <no files>\n"))
326 (put-text-property start (point) 'stgit-patchsym patchsym))))
327
328(defun stgit-rescan ()
329 "Rescan the status buffer."
330 (save-excursion
331 (let ((marked ()))
332 (goto-char (point-min))
333 (while (not (eobp))
334 (cond ((looking-at "Branch: \\(.*\\)")
335 (put-text-property (match-beginning 1) (match-end 1)
336 'face 'bold))
337 ((looking-at "\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
338 (let ((state (match-string 1))
339 (patchsym (intern (match-string 3))))
340 (put-text-property
341 (match-beginning 3) (match-end 3) 'face
342 (cond ((string= state ">") 'stgit-top-patch-face)
343 ((string= state "+") 'stgit-applied-patch-face)
344 ((string= state "-") 'stgit-unapplied-patch-face)))
345 (put-text-property (match-beginning 4) (match-end 4)
346 'face 'stgit-description-face)
347 (when (memq patchsym stgit-marked-patches)
348 (replace-match "*" nil nil nil 2)
349 (setq marked (cons patchsym marked)))
350 (when (memq patchsym stgit-expanded-patches)
351 (stgit-expand-patch patchsym))
352 ))
353 ((or (looking-at "stg series: Branch \".*\" not initialised")
354 (looking-at "stg series: .*: branch not initialized"))
355 (forward-line 1)
356 (insert "Run M-x stgit-init to initialise")))
357 (forward-line 1))
358 (setq stgit-marked-patches (nreverse marked)))))
359
360(defun stgit-select ()
361 "Expand or collapse the current entry"
362 (interactive)
363 (let ((curpatch (stgit-patch-at-point)))
364 (if (not curpatch)
365 (let ((patched-file (stgit-patched-file-at-point)))
366 (unless patched-file
367 (error "No patch or file on the current line"))
368 (let ((filename (expand-file-name (cdr patched-file))))
369 (unless (file-exists-p filename)
370 (error "File does not exist"))
371 (find-file filename)))
372 (setq curpatch (intern curpatch))
373 (setq stgit-expanded-patches
374 (if (memq curpatch stgit-expanded-patches)
375 (delq curpatch stgit-expanded-patches)
376 (cons curpatch stgit-expanded-patches)))
377 (stgit-reload))))
378
379(defun stgit-find-file-other-window ()
380 "Open file at point in other window"
381 (interactive)
382 (let ((patched-file (stgit-patched-file-at-point)))
383 (unless patched-file
384 (error "No file on the current line"))
385 (let ((filename (expand-file-name (cdr patched-file))))
386 (unless (file-exists-p filename)
387 (error "File does not exist"))
388 (find-file-other-window filename))))
389
390(defun stgit-quit ()
391 "Hide the stgit buffer."
392 (interactive)
393 (bury-buffer))
394
395(defun stgit-git-status ()
396 "Show status using `git-status'."
397 (interactive)
398 (unless (fboundp 'git-status)
399 (error "stgit-git-status requires git-status"))
400 (let ((dir default-directory))
401 (save-selected-window
402 (pop-to-buffer nil)
403 (git-status dir))))
404
405(defun stgit-next-line (&optional arg try-vscroll)
406 "Move cursor vertically down ARG lines"
407 (interactive "p\np")
408 (next-line arg try-vscroll)
409 (when (looking-at " \\S-")
410 (forward-char 2)))
411
412(defun stgit-previous-line (&optional arg try-vscroll)
413 "Move cursor vertically up ARG lines"
414 (interactive "p\np")
415 (previous-line arg try-vscroll)
416 (when (looking-at " \\S-")
417 (forward-char 2)))
418
419(defun stgit-next-patch (&optional arg)
420 "Move cursor down ARG patches"
421 (interactive "p")
422 (unless arg
423 (setq arg 1))
424 (if (< arg 0)
425 (stgit-previous-patch (- arg))
426 (while (not (zerop arg))
427 (setq arg (1- arg))
428 (while (progn (stgit-next-line)
429 (not (stgit-patch-at-point)))))))
430
431(defun stgit-previous-patch (&optional arg)
432 "Move cursor up ARG patches"
433 (interactive "p")
434 (unless arg
435 (setq arg 1))
436 (if (< arg 0)
437 (stgit-next-patch (- arg))
438 (while (not (zerop arg))
439 (setq arg (1- arg))
440 (while (progn (stgit-previous-line)
441 (not (stgit-patch-at-point)))))))
442
443(defvar stgit-mode-hook nil
444 "Run after `stgit-mode' is setup.")
445
446(defvar stgit-mode-map nil
447 "Keymap for StGit major mode.")
448
449(unless stgit-mode-map
450 (setq stgit-mode-map (make-keymap))
451 (suppress-keymap stgit-mode-map)
452 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
453 '((" " . stgit-mark)
454 ("m" . stgit-mark)
455 ("\d" . stgit-unmark-up)
456 ("u" . stgit-unmark-down)
457 ("?" . stgit-help)
458 ("h" . stgit-help)
459 ("p" . stgit-previous-line)
460 ("n" . stgit-next-line)
461 ("\C-p" . stgit-previous-patch)
462 ("\C-n" . stgit-next-patch)
463 ("\M-{" . stgit-previous-patch)
464 ("\M-}" . stgit-next-patch)
465 ("s" . stgit-git-status)
466 ("g" . stgit-reload)
467 ("r" . stgit-refresh)
468 ("\C-c\C-r" . stgit-rename)
469 ("e" . stgit-edit)
470 ("c" . stgit-coalesce)
471 ("N" . stgit-new)
472 ("R" . stgit-repair)
473 ("C" . stgit-commit)
474 ("U" . stgit-uncommit)
475 ("\r" . stgit-select)
476 ("o" . stgit-find-file-other-window)
477 (">" . stgit-push-next)
478 ("<" . stgit-pop-next)
479 ("P" . stgit-push-or-pop)
480 ("G" . stgit-goto)
481 ("=" . stgit-show)
482 ("D" . stgit-delete)
483 ([(control ?/)] . stgit-undo)
484 ("\C-_" . stgit-undo)
485 ("q" . stgit-quit))))
486
487(defun stgit-mode ()
488 "Major mode for interacting with StGit.
489Commands:
490\\{stgit-mode-map}"
491 (kill-all-local-variables)
492 (buffer-disable-undo)
493 (setq mode-name "StGit"
494 major-mode 'stgit-mode
495 goal-column 2)
496 (use-local-map stgit-mode-map)
497 (set (make-local-variable 'list-buffers-directory) default-directory)
498 (set (make-local-variable 'stgit-marked-patches) nil)
499 (set (make-local-variable 'stgit-expanded-patches) nil)
500 (set-variable 'truncate-lines 't)
501 (run-hooks 'stgit-mode-hook))
502
503(defun stgit-add-mark (patch)
504 (let ((patchsym (intern patch)))
505 (setq stgit-marked-patches (cons patchsym stgit-marked-patches))))
506
507(defun stgit-remove-mark (patch)
508 (let ((patchsym (intern patch)))
509 (setq stgit-marked-patches (delq patchsym stgit-marked-patches))))
510
511(defun stgit-clear-marks ()
512 (setq stgit-marked-patches '()))
513
514(defun stgit-marked-patches ()
515 "Return the names of the marked patches."
516 (mapcar 'symbol-name stgit-marked-patches))
517
518(defun stgit-patch-at-point (&optional cause-error allow-file)
519 "Return the patch name on the current line.
520If CAUSE-ERROR is not nil, signal an error if none found.
521If ALLOW-FILE is not nil, also handle when point is on a file of
522a patch."
523 (or (and allow-file
524 (let ((patchsym (get-text-property (point) 'stgit-patchsym)))
525 (and patchsym
526 (symbol-name patchsym))))
527 (save-excursion
528 (beginning-of-line)
529 (and (looking-at "[>+-][ *]\\([^ ]*\\)")
530 (match-string-no-properties 1)))
531 (and cause-error
532 (error "No patch on this line"))))
533
534(defun stgit-patched-file-at-point (&optional both-files)
535 "Returns a cons of the patchsym and file name at point. For
536copies and renames, return the new file if the patch is either
537applied. If BOTH-FILES is non-nil, return a cons of the old and
538the new file names instead of just one name."
539 (let ((patchsym (get-text-property (point) 'stgit-patchsym))
540 (file (get-text-property (point) 'stgit-file)))
541 (cond ((not patchsym) nil)
542 (file (cons patchsym file))
543 (both-files
544 (cons patchsym (cons (get-text-property (point) 'stgit-old-file)
545 (get-text-property (point) 'stgit-new-file))))
546 (t
547 (let ((file-sym (save-excursion
548 (stgit-previous-patch)
549 (unless (equal (stgit-patch-at-point)
550 (symbol-name patchsym))
551 (error "Cannot find the %s patch" patchsym))
552 (beginning-of-line)
553 (if (= (char-after) ?-)
554 'stgit-old-file
555 'stgit-new-file))))
556 (cons patchsym (get-text-property (point) file-sym)))))))
557
558(defun stgit-patches-marked-or-at-point ()
559 "Return the names of the marked patches, or the patch on the current line."
560 (if stgit-marked-patches
561 (stgit-marked-patches)
562 (let ((patch (stgit-patch-at-point)))
563 (if patch
564 (list patch)
565 '()))))
566
567(defun stgit-goto-patch (patch)
568 "Move point to the line containing PATCH."
569 (let ((p (point)))
570 (goto-char (point-min))
571 (if (re-search-forward (concat "^[>+-][ *]" (regexp-quote patch) " ") nil t)
572 (progn (move-to-column goal-column)
573 t)
574 (goto-char p)
575 nil)))
576
577(defun stgit-init ()
578 "Run stg init."
579 (interactive)
580 (stgit-capture-output nil
581 (stgit-run "init"))
582 (stgit-reload))
583
584(defun stgit-mark ()
585 "Mark the patch under point."
586 (interactive)
587 (let ((patch (stgit-patch-at-point t)))
588 (stgit-add-mark patch)
589 (stgit-reload))
590 (stgit-next-patch))
591
592(defun stgit-unmark-up ()
593 "Remove mark from the patch on the previous line."
594 (interactive)
595 (stgit-previous-patch)
596 (stgit-remove-mark (stgit-patch-at-point t))
597 (stgit-reload))
598
599(defun stgit-unmark-down ()
600 "Remove mark from the patch on the current line."
601 (interactive)
602 (stgit-remove-mark (stgit-patch-at-point t))
603 (stgit-next-patch)
604 (stgit-reload))
605
606(defun stgit-rename (name)
607 "Rename the patch under point to NAME."
608 (interactive (list (read-string "Patch name: " (stgit-patch-at-point t))))
609 (let ((old-name (stgit-patch-at-point t)))
610 (stgit-capture-output nil
611 (stgit-run "rename" old-name name))
612 (let ((old-name-sym (intern old-name))
613 (name-sym (intern name)))
614 (when (memq old-name-sym stgit-expanded-patches)
615 (setq stgit-expanded-patches
616 (cons name-sym (delq old-name-sym stgit-expanded-patches))))
617 (when (memq old-name-sym stgit-marked-patches)
618 (setq stgit-marked-patches
619 (cons name-sym (delq old-name-sym stgit-marked-patches)))))
620 (stgit-reload)
621 (stgit-goto-patch name)))
622
623(defun stgit-repair ()
624 "Run stg repair."
625 (interactive)
626 (stgit-capture-output nil
627 (stgit-run "repair"))
628 (stgit-reload))
629
630(defun stgit-commit ()
631 "Run stg commit."
632 (interactive)
633 (stgit-capture-output nil (stgit-run "commit"))
634 (stgit-reload))
635
636(defun stgit-uncommit (arg)
637 "Run stg uncommit. Numeric arg determines number of patches to uncommit."
638 (interactive "p")
639 (stgit-capture-output nil (stgit-run "uncommit" "-n" (number-to-string arg)))
640 (stgit-reload))
641
642(defun stgit-push-next (npatches)
643 "Push the first unapplied patch.
644With numeric prefix argument, push that many patches."
645 (interactive "p")
646 (stgit-capture-output nil (stgit-run "push" "-n"
647 (number-to-string npatches)))
648 (stgit-reload)
649 (stgit-refresh-git-status))
650
651(defun stgit-pop-next (npatches)
652 "Pop the topmost applied patch.
653With numeric prefix argument, pop that many patches."
654 (interactive "p")
655 (stgit-capture-output nil (stgit-run "pop" "-n" (number-to-string npatches)))
656 (stgit-reload)
657 (stgit-refresh-git-status))
658
659(defun stgit-applied-at-point ()
660 "Is the patch on the current line applied?"
661 (save-excursion
662 (beginning-of-line)
663 (looking-at "[>+]")))
664
665(defun stgit-push-or-pop ()
666 "Push or pop the patch on the current line."
667 (interactive)
668 (let ((patch (stgit-patch-at-point t))
669 (applied (stgit-applied-at-point)))
670 (stgit-capture-output nil
671 (stgit-run (if applied "pop" "push") patch))
672 (stgit-reload)))
673
674(defun stgit-goto ()
675 "Go to the patch on the current line."
676 (interactive)
677 (let ((patch (stgit-patch-at-point t)))
678 (stgit-capture-output nil
679 (stgit-run "goto" patch))
680 (stgit-reload)))
681
682(defun stgit-id (patch)
683 "Return the git commit id for PATCH"
684 (let ((result (with-output-to-string
685 (stgit-run-silent "id" patch))))
686 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
687 (error "Cannot find commit id for %s" patch))
688 (match-string 1 result)))
689
690(defun stgit-show ()
691 "Show the patch on the current line."
692 (interactive)
693 (stgit-capture-output "*StGit patch*"
694 (let ((patch (stgit-patch-at-point)))
695 (if (not patch)
696 (let ((patched-file (stgit-patched-file-at-point t)))
697 (unless patched-file
698 (error "No patch or file at point"))
699 (let ((id (stgit-id (symbol-name (car patched-file)))))
700 (with-output-to-temp-buffer "*StGit diff*"
701 (if (consp (cdr patched-file))
702 ;; two files (copy or rename)
703 (stgit-run-git "diff" "-C" "-C" (concat id "^") id "--"
704 (cadr patched-file) (cddr patched-file))
705 ;; just one file
706 (stgit-run-git "diff" (concat id "^") id "--"
707 (cdr patched-file)))
708 (with-current-buffer standard-output
709 (diff-mode)))))
710 (stgit-run "show" (stgit-patch-at-point))
711 (with-current-buffer standard-output
712 (goto-char (point-min))
713 (diff-mode))))))
714
715(defun stgit-edit ()
716 "Edit the patch on the current line."
717 (interactive)
718 (let ((patch (stgit-patch-at-point t))
719 (edit-buf (get-buffer-create "*StGit edit*"))
720 (dir default-directory))
721 (log-edit 'stgit-confirm-edit t nil edit-buf)
722 (set (make-local-variable 'stgit-edit-patch) patch)
723 (setq default-directory dir)
724 (let ((standard-output edit-buf))
725 (stgit-run-silent "edit" "--save-template=-" patch))))
726
727(defun stgit-confirm-edit ()
728 (interactive)
729 (let ((file (make-temp-file "stgit-edit-")))
730 (write-region (point-min) (point-max) file)
731 (stgit-capture-output nil
732 (stgit-run "edit" "-f" file stgit-edit-patch))
733 (with-current-buffer log-edit-parent-buffer
734 (stgit-reload))))
735
736(defun stgit-new ()
737 "Create a new patch."
738 (interactive)
739 (let ((edit-buf (get-buffer-create "*StGit edit*"))
740 (dir default-directory))
741 (log-edit 'stgit-confirm-new t nil edit-buf)
742 (setq default-directory dir)))
743
744(defun stgit-confirm-new ()
745 (interactive)
746 (let ((file (make-temp-file "stgit-edit-")))
747 (write-region (point-min) (point-max) file)
748 (stgit-capture-output nil
749 (stgit-run "new" "-f" file))
750 (with-current-buffer log-edit-parent-buffer
751 (stgit-reload))))
752
753(defun stgit-create-patch-name (description)
754 "Create a patch name from a long description"
755 (let ((patch ""))
756 (while (> (length description) 0)
757 (cond ((string-match "\\`[a-zA-Z_-]+" description)
758 (setq patch (downcase (concat patch (match-string 0 description))))
759 (setq description (substring description (match-end 0))))
760 ((string-match "\\` +" description)
761 (setq patch (concat patch "-"))
762 (setq description (substring description (match-end 0))))
763 ((string-match "\\`[^a-zA-Z_-]+" description)
764 (setq description (substring description (match-end 0))))))
765 (cond ((= (length patch) 0)
766 "patch")
767 ((> (length patch) 20)
768 (substring patch 0 20))
769 (t patch))))
770
771(defun stgit-delete (patch-names)
772 "Delete the named patches."
773 (interactive (list (stgit-patches-marked-or-at-point)))
774 (if (zerop (length patch-names))
775 (error "No patches to delete")
776 (when (yes-or-no-p (format "Really delete %d patches? "
777 (length patch-names)))
778 (stgit-capture-output nil
779 (apply 'stgit-run "delete" patch-names))
780 (stgit-reload))))
781
782(defun stgit-coalesce (patch-names)
783 "Run stg coalesce on the named patches."
784 (interactive (list (stgit-marked-patches)))
785 (let ((edit-buf (get-buffer-create "*StGit edit*"))
786 (dir default-directory))
787 (log-edit 'stgit-confirm-coalesce t nil edit-buf)
788 (set (make-local-variable 'stgit-patches) patch-names)
789 (setq default-directory dir)
790 (let ((standard-output edit-buf))
791 (apply 'stgit-run-silent "coalesce" "--save-template=-" patch-names))))
792
793(defun stgit-confirm-coalesce ()
794 (interactive)
795 (let ((file (make-temp-file "stgit-edit-")))
796 (write-region (point-min) (point-max) file)
797 (stgit-capture-output nil
798 (apply 'stgit-run "coalesce" "-f" file stgit-patches))
799 (with-current-buffer log-edit-parent-buffer
800 (stgit-clear-marks)
801 ;; Go to first marked patch and stay there
802 (goto-char (point-min))
803 (re-search-forward (concat "^[>+-]\\*") nil t)
804 (move-to-column goal-column)
805 (let ((pos (point)))
806 (stgit-reload)
807 (goto-char pos)))))
808
809(defun stgit-help ()
810 "Display help for the StGit mode."
811 (interactive)
812 (describe-function 'stgit-mode))
813
814(defun stgit-undo (&optional arg)
815 "Run stg undo.
816With prefix argument, run it with the --hard flag."
817 (interactive "P")
818 (stgit-capture-output nil
819 (if arg
820 (stgit-run "undo" "--hard")
821 (stgit-run "undo")))
822 (stgit-reload))
823
824(defun stgit-refresh (&optional arg)
825 "Run stg refresh.
826With prefix argument, refresh the marked patch or the patch under point."
827 (interactive "P")
828 (let ((patchargs (if arg
829 (let ((patches (stgit-patches-marked-or-at-point)))
830 (cond ((null patches)
831 (error "no patch to update"))
832 ((> (length patches) 1)
833 (error "too many patches selected"))
834 (t
835 (cons "-p" patches))))
836 nil)))
837 (stgit-capture-output nil
838 (apply 'stgit-run "refresh" patchargs))
839 (stgit-refresh-git-status))
840 (stgit-reload))
841
842(provide 'stgit)