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