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