chiark / gitweb /
stgit.el: Default to expanding index and work tree
[stgit] / contrib / stgit.el
CommitLineData
3a59f3db
KH
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
0f076fe6 12(require 'git nil t)
50d88c67 13(require 'cl)
98230edd 14(require 'ewoc)
0f076fe6 15
56d81fe5 16(defun stgit (dir)
a53347d9 17 "Manage StGit patches for the tree in DIR."
56d81fe5 18 (interactive "DDirectory: \n")
52144ce5 19 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 20 (stgit-reload))
56d81fe5 21
074a4fb0
GH
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"))
df283a8b 30 (error "Cannot find top-level git tree for %s" dir))))))
074a4fb0
GH
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
36directory 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))))))
52144ce5 44
b894e680
DK
45(defun stgit-find-buffer (dir)
46 "Return the buffer displaying StGit patches for DIR, or nil if none."
56d81fe5
DK
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)))
b894e680
DK
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
2c862b07 63(defstruct (stgit-patch)
3164eec6 64 status name desc empty files-ewoc)
56d81fe5 65
98230edd
DK
66(defun stgit-patch-pp (patch)
67 (let ((status (stgit-patch-status patch))
68 (start (point))
69 (name (stgit-patch-name patch)))
b894e680 70 (case name
8f702de4
GH
71 (:index (insert " "
72 (propertize "Index"
46a273fd 73 'face 'stgit-index-work-tree-title-face)))
8f702de4
GH
74 (:work (insert " "
75 (propertize "Work tree"
46a273fd 76 'face 'stgit-index-work-tree-title-face)))
b894e680
DK
77 (t (insert (case status
78 ('applied "+")
79 ('top ">")
80 ('unapplied "-"))
81 (if (memq name stgit-marked-patches)
82 "*" " ")
83 (propertize (format "%-30s"
84 (symbol-name name))
85 'face (case status
86 ('applied 'stgit-applied-patch-face)
87 ('top 'stgit-top-patch-face)
88 ('unapplied 'stgit-unapplied-patch-face)
89 ('index nil)
90 ('work nil)))
91 " "
92 (if (stgit-patch-empty patch) "(empty) " "")
93 (propertize (or (stgit-patch-desc patch) "")
94 'face 'stgit-description-face))))
f9b82d36 95 (put-text-property start (point) 'entry-type 'patch)
98230edd 96 (when (memq name stgit-expanded-patches)
0de6881a 97 (stgit-insert-patch-files patch))
98230edd
DK
98 (put-text-property start (point) 'patch-data patch)))
99
56d81fe5
DK
100(defun create-stgit-buffer (dir)
101 "Create a buffer for showing StGit patches.
102Argument DIR is the repository path."
103 (let ((buf (create-file-buffer (concat dir "*stgit*")))
104 (inhibit-read-only t))
105 (with-current-buffer buf
106 (setq default-directory dir)
107 (stgit-mode)
98230edd
DK
108 (set (make-local-variable 'stgit-ewoc)
109 (ewoc-create #'stgit-patch-pp "Branch:\n" "--"))
56d81fe5
DK
110 (setq buffer-read-only t))
111 buf))
112
113(defmacro stgit-capture-output (name &rest body)
e558a4ab
GH
114 "Capture StGit output and, if there was any output, show it in a window
115at the end.
116Returns nil if there was no output."
94baef5a
DK
117 (declare (debug ([&or stringp null] body))
118 (indent 1))
34afb86c
DK
119 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
120 (stgit-dir default-directory)
121 (inhibit-read-only t))
56d81fe5 122 (with-current-buffer output-buf
34afb86c
DK
123 (erase-buffer)
124 (setq default-directory stgit-dir)
125 (setq buffer-read-only t))
56d81fe5
DK
126 (let ((standard-output output-buf))
127 ,@body)
34afb86c
DK
128 (with-current-buffer output-buf
129 (set-buffer-modified-p nil)
130 (setq buffer-read-only t)
131 (if (< (point-min) (point-max))
132 (display-buffer output-buf t)))))
56d81fe5 133
d51722b7
GH
134(defun stgit-make-run-args (args)
135 "Return a copy of ARGS with its elements converted to strings."
136 (mapcar (lambda (x)
137 ;; don't use (format "%s" ...) to limit type errors
138 (cond ((stringp x) x)
139 ((integerp x) (number-to-string x))
140 ((symbolp x) (symbol-name x))
141 (t
142 (error "Bad element in stgit-make-run-args args: %S" x))))
143 args))
144
9aecd505 145(defun stgit-run-silent (&rest args)
d51722b7 146 (setq args (stgit-make-run-args args))
56d81fe5
DK
147 (apply 'call-process "stg" nil standard-output nil args))
148
9aecd505 149(defun stgit-run (&rest args)
d51722b7 150 (setq args (stgit-make-run-args args))
9aecd505
DK
151 (let ((msgcmd (mapconcat #'identity args " ")))
152 (message "Running stg %s..." msgcmd)
153 (apply 'call-process "stg" nil standard-output nil args)
154 (message "Running stg %s...done" msgcmd)))
155
378a003d 156(defun stgit-run-git (&rest args)
d51722b7 157 (setq args (stgit-make-run-args args))
378a003d
GH
158 (let ((msgcmd (mapconcat #'identity args " ")))
159 (message "Running git %s..." msgcmd)
160 (apply 'call-process "git" nil standard-output nil args)
161 (message "Running git %s...done" msgcmd)))
162
1f60181a 163(defun stgit-run-git-silent (&rest args)
d51722b7 164 (setq args (stgit-make-run-args args))
1f60181a
GH
165 (apply 'call-process "git" nil standard-output nil args))
166
b894e680
DK
167(defun stgit-index-empty-p ()
168 "Returns non-nil if the index contains no changes from HEAD."
169 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
170
2ecb05c8
GH
171(defvar stgit-index-node)
172(defvar stgit-worktree-node)
210a2a52
DK
173
174(defun stgit-refresh-index ()
175 (when stgit-index-node
176 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
177
178(defun stgit-refresh-worktree ()
179 (when stgit-worktree-node
180 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
181
8f702de4
GH
182(defun stgit-run-series-insert-index (ewoc)
183 (setq index-node (cons ewoc (ewoc-enter-last ewoc
184 (make-stgit-patch
185 :status 'index
186 :name :index
187 :desc nil
188 :empty nil)))
189 worktree-node (cons ewoc (ewoc-enter-last ewoc
190 (make-stgit-patch
191 :status 'work
192 :name :work
193 :desc nil
194 :empty nil)))))
195
98230edd 196(defun stgit-run-series (ewoc)
8f702de4
GH
197 (setq stgit-index-node nil
198 stgit-worktree-node nil)
199 (let ((inserted-index (not stgit-show-worktree))
200 index-node
201 worktree-node)
98230edd
DK
202 (with-temp-buffer
203 (let ((exit-status (stgit-run-silent "series" "--description" "--empty")))
204 (goto-char (point-min))
205 (if (not (zerop exit-status))
206 (cond ((looking-at "stg series: \\(.*\\)")
8f702de4 207 (setq inserted-index t)
98230edd 208 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
8f702de4
GH
209 (substitute-command-keys
210 "-- not initialized; run \\[stgit-init]")))
98230edd
DK
211 ((looking-at ".*")
212 (error "Error running stg: %s"
213 (match-string 0))))
214 (while (not (eobp))
215 (unless (looking-at
216 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
217 (error "Syntax error in output from stg series"))
218 (let* ((state-str (match-string 2))
219 (state (cond ((string= state-str ">") 'top)
220 ((string= state-str "+") 'applied)
8f702de4
GH
221 ((string= state-str "-") 'unapplied)))
222 (name (intern (match-string 4)))
223 (desc (match-string 5))
224 (empty (string= (match-string 1) "0")))
225 (unless inserted-index
226 (when (or (eq stgit-show-worktree-mode 'top)
227 (and (eq stgit-show-worktree-mode 'center)
228 (eq state 'unapplied)))
229 (setq inserted-index t)
230 (stgit-run-series-insert-index ewoc)))
98230edd
DK
231 (ewoc-enter-last ewoc
232 (make-stgit-patch
233 :status state
8f702de4
GH
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)))
98230edd 242
1f0bf00f 243(defun stgit-reload ()
a53347d9 244 "Update the contents of the StGit buffer."
56d81fe5
DK
245 (interactive)
246 (let ((inhibit-read-only t)
247 (curline (line-number-at-pos))
2c862b07 248 (curpatch (stgit-patch-name-at-point)))
98230edd
DK
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))))
4f292066 256 'face 'stgit-branch-name-face)
98230edd 257 "\n")
ce3b6130
DK
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)))
98230edd 264 (stgit-run-series stgit-ewoc)
56d81fe5
DK
265 (if curpatch
266 (stgit-goto-patch curpatch)
074a4fb0
GH
267 (goto-line curline)))
268 (stgit-refresh-git-status))
56d81fe5 269
8f40753a
GH
270(defgroup stgit nil
271 "A user interface for the StGit patch maintenance tool."
272 :group 'tools)
273
07f464e0
DK
274(defface stgit-description-face
275 '((((background dark)) (:foreground "tan"))
276 (((background light)) (:foreground "dark red")))
8f40753a
GH
277 "The face used for StGit descriptions"
278 :group 'stgit)
4f292066
GH
279
280(defface stgit-branch-name-face
281 '((t :inherit bold))
282 "The face used for the StGit branch name"
283 :group 'stgit)
07f464e0
DK
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)))
8f40753a
GH
289 "The face used for the top patch names"
290 :group 'stgit)
07f464e0
DK
291
292(defface stgit-applied-patch-face
293 '((((background dark)) (:foreground "light yellow"))
294 (((background light)) (:foreground "purple"))
295 (t ()))
8f40753a
GH
296 "The face used for applied patch names"
297 :group 'stgit)
07f464e0
DK
298
299(defface stgit-unapplied-patch-face
300 '((((background dark)) (:foreground "gray80"))
301 (((background light)) (:foreground "orchid"))
302 (t ()))
8f40753a
GH
303 "The face used for unapplied patch names"
304 :group 'stgit)
07f464e0 305
1f60181a
GH
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
a6d9a852
GH
324(defface stgit-file-permission-face
325 '((((class color) (background light)) (:foreground "green" :bold t))
326 (((class color) (background dark)) (:foreground "green" :bold t)))
327 "StGit mode face used for permission changes."
328 :group 'stgit)
329
46a273fd
GH
330(defface stgit-index-work-tree-title-face
331 '((((supports :slant italic)) :slant italic)
332 (t :inherit bold))
333 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
334 :group 'stgit)
335
336
1f60181a
GH
337(defcustom stgit-expand-find-copies-harder
338 nil
339 "Try harder to find copied files when listing patches.
340
341When not nil, runs git diff-tree with the --find-copies-harder
342flag, which reduces performance."
343 :type 'boolean
344 :group 'stgit)
345
346(defconst stgit-file-status-code-strings
347 (mapcar (lambda (arg)
348 (cons (car arg)
a6d9a852
GH
349 (propertize (cadr arg) 'face (car (cddr arg)))))
350 '((add "Added" stgit-modified-file-face)
351 (copy "Copied" stgit-modified-file-face)
352 (delete "Deleted" stgit-modified-file-face)
353 (modify "Modified" stgit-modified-file-face)
354 (rename "Renamed" stgit-modified-file-face)
355 (mode-change "Mode change" stgit-modified-file-face)
356 (unmerged "Unmerged" stgit-unmerged-file-face)
357 (unknown "Unknown" stgit-unknown-file-face)))
1f60181a
GH
358 "Alist of code symbols to description strings")
359
3164eec6
DK
360(defun stgit-file-status-code-as-string (file)
361 "Return stgit status code for FILE as a string"
362 (let* ((code (assq (stgit-file-status file)
363 stgit-file-status-code-strings))
364 (score (stgit-file-cr-score file)))
365 (when code
a6d9a852 366 (format "%-11s "
3164eec6
DK
367 (if (and score (/= score 100))
368 (format "%s %s" (cdr code)
369 (propertize (format "%d%%" score)
a6d9a852 370 'face 'stgit-description-face))
3164eec6 371 (cdr code))))))
1f60181a 372
a6d9a852 373(defun stgit-file-status-code (str &optional score)
1f60181a
GH
374 "Return stgit status code from git status string"
375 (let ((code (assoc str '(("A" . add)
376 ("C" . copy)
377 ("D" . delete)
378 ("M" . modify)
379 ("R" . rename)
380 ("T" . mode-change)
381 ("U" . unmerged)
382 ("X" . unknown)))))
a6d9a852
GH
383 (setq code (if code (cdr code) 'unknown))
384 (when (stringp score)
385 (if (> (length score) 0)
386 (setq score (string-to-number score))
387 (setq score nil)))
388 (if score (cons code score) code)))
389
390(defconst stgit-file-type-strings
391 '((#o100 . "file")
392 (#o120 . "symlink")
393 (#o160 . "subproject"))
394 "Alist of names of file types")
395
396(defun stgit-file-type-string (type)
47271f41
GH
397 "Return string describing file type TYPE (the high bits of file permission).
398Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
a6d9a852
GH
399 (let ((type-str (assoc type stgit-file-type-strings)))
400 (or (and type-str (cdr type-str))
401 (format "unknown type %o" type))))
402
403(defun stgit-file-type-change-string (old-perm new-perm)
47271f41
GH
404 "Return string describing file type change from OLD-PERM to NEW-PERM.
405Cf. `stgit-file-type-string'."
a6d9a852
GH
406 (let ((old-type (lsh old-perm -9))
407 (new-type (lsh new-perm -9)))
408 (cond ((= old-type new-type) "")
409 ((zerop new-type) "")
410 ((zerop old-type)
411 (if (= new-type #o100)
412 ""
413 (format " (%s)" (stgit-file-type-string new-type))))
414 (t (format " (%s -> %s)"
415 (stgit-file-type-string old-type)
416 (stgit-file-type-string new-type))))))
417
418(defun stgit-file-mode-change-string (old-perm new-perm)
47271f41
GH
419 "Return string describing file mode change from OLD-PERM to NEW-PERM.
420Cf. `stgit-file-type-change-string'."
a6d9a852
GH
421 (setq old-perm (logand old-perm #o777)
422 new-perm (logand new-perm #o777))
423 (if (or (= old-perm new-perm)
424 (zerop old-perm)
425 (zerop new-perm))
426 ""
427 (let* ((modified (logxor old-perm new-perm))
428 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
429 (cond ((zerop modified) "")
430 ((and (zerop not-x-modified)
431 (or (and (eq #o111 (logand old-perm #o111))
432 (propertize "-x" 'face 'stgit-file-permission-face))
433 (and (eq #o111 (logand new-perm #o111))
434 (propertize "+x" 'face
435 'stgit-file-permission-face)))))
436 (t (concat (propertize (format "%o" old-perm)
437 'face 'stgit-file-permission-face)
438 (propertize " -> "
439 'face 'stgit-description-face)
440 (propertize (format "%o" new-perm)
441 'face 'stgit-file-permission-face)))))))
1f60181a 442
0de6881a
DK
443(defstruct (stgit-file)
444 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
445
3164eec6 446(defun stgit-file-pp (file)
0de6881a
DK
447 (let ((status (stgit-file-status file))
448 (name (if (stgit-file-copy-or-rename file)
449 (concat (stgit-file-cr-from file)
450 (propertize " -> "
451 'face 'stgit-description-face)
452 (stgit-file-cr-to file))
453 (stgit-file-file file)))
454 (mode-change (stgit-file-mode-change-string
455 (stgit-file-old-perm file)
456 (stgit-file-new-perm file)))
457 (start (point)))
3164eec6
DK
458 (insert (format " %-12s%1s%s%s\n"
459 (stgit-file-status-code-as-string file)
98230edd 460 mode-change
0de6881a
DK
461 name
462 (propertize (stgit-file-type-change-string
463 (stgit-file-old-perm file)
464 (stgit-file-new-perm file))
98230edd 465 'face 'stgit-description-face)))
0de6881a 466 (add-text-properties start (point)
3164eec6
DK
467 (list 'entry-type 'file
468 'file-data file))))
0de6881a 469
7567401c
GH
470(defun stgit-find-copies-harder-diff-arg ()
471 "Return the flag to use with `git-diff' depending on the
472`stgit-expand-find-copies-harder' flag."
473 (if stgit-expand-find-copies-harder
474 "--find-copies-harder"
475 "-C"))
476
0de6881a 477(defun stgit-insert-patch-files (patch)
88134ff7
GH
478 "Expand (show modification of) the patch PATCH after the line
479at point."
3164eec6
DK
480 (let* ((patchsym (stgit-patch-name patch))
481 (end (progn (insert "#") (prog1 (point-marker) (forward-char -1))))
7567401c 482 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
3164eec6
DK
483 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
484 (setf (stgit-patch-files-ewoc patch) ewoc)
0de6881a 485 (with-temp-buffer
b894e680
DK
486 (apply 'stgit-run-git
487 (cond ((eq patchsym :work)
488 `("diff-files" ,@args))
489 ((eq patchsym :index)
490 `("diff-index" ,@args "--cached" "HEAD"))
491 (t
492 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
0de6881a 493 (goto-char (point-min))
b894e680
DK
494 (unless (or (eobp) (memq patchsym '(:work :index)))
495 (forward-char 41))
0de6881a
DK
496 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
497 (let ((old-perm (string-to-number (match-string 1) 8))
498 (new-perm (string-to-number (match-string 2) 8)))
499 (goto-char (match-end 0))
500 (let ((file
501 (cond ((looking-at
502 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
027e1370
GH
503 (let* ((patch-status (stgit-patch-status patch))
504 (file-subexp (if (eq patch-status 'unapplied)
505 3
506 4))
507 (file (match-string file-subexp)))
508 (make-stgit-file
509 :old-perm old-perm
510 :new-perm new-perm
511 :copy-or-rename t
512 :cr-score (string-to-number (match-string 2))
513 :cr-from (match-string 3)
514 :cr-to (match-string 4)
515 :status (stgit-file-status-code
516 (match-string 1))
517 :file file)))
0de6881a
DK
518 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
519 (make-stgit-file
520 :old-perm old-perm
521 :new-perm new-perm
522 :copy-or-rename nil
523 :cr-score nil
524 :cr-from nil
525 :cr-to nil
027e1370
GH
526 :status (stgit-file-status-code
527 (match-string 1))
0de6881a 528 :file (match-string 2))))))
3164eec6
DK
529 (ewoc-enter-last ewoc file))
530 (goto-char (match-end 0))))
531 (unless (ewoc-nth ewoc 0)
532 (ewoc-set-hf ewoc "" (propertize " <no files>\n"
533 'face 'stgit-description-face))))
534 (goto-char end)
535 (delete-char -2)))
07f464e0 536
acc5652f 537(defun stgit-select-file ()
3164eec6
DK
538 (let ((filename (expand-file-name
539 (stgit-file-file (stgit-patched-file-at-point)))))
0de6881a
DK
540 (unless (file-exists-p filename)
541 (error "File does not exist"))
542 (find-file filename)))
acc5652f 543
50d88c67 544(defun stgit-select-patch ()
98230edd
DK
545 (let ((patchname (stgit-patch-name-at-point)))
546 (if (memq patchname stgit-expanded-patches)
547 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
548 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
549 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
550 (move-to-column (stgit-goal-column)))
acc5652f 551
378a003d
GH
552(defun stgit-select ()
553 "Expand or collapse the current entry"
554 (interactive)
50d88c67
DK
555 (case (get-text-property (point) 'entry-type)
556 ('patch
557 (stgit-select-patch))
558 ('file
559 (stgit-select-file))
560 (t
561 (error "No patch or file on line"))))
378a003d
GH
562
563(defun stgit-find-file-other-window ()
564 "Open file at point in other window"
565 (interactive)
566 (let ((patched-file (stgit-patched-file-at-point)))
567 (unless patched-file
568 (error "No file on the current line"))
3164eec6 569 (let ((filename (expand-file-name (stgit-file-file patched-file))))
378a003d
GH
570 (unless (file-exists-p filename)
571 (error "File does not exist"))
572 (find-file-other-window filename))))
573
83327d53 574(defun stgit-quit ()
a53347d9 575 "Hide the stgit buffer."
83327d53
GH
576 (interactive)
577 (bury-buffer))
578
0f076fe6 579(defun stgit-git-status ()
a53347d9 580 "Show status using `git-status'."
0f076fe6
GH
581 (interactive)
582 (unless (fboundp 'git-status)
df283a8b 583 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
584 (let ((dir default-directory))
585 (save-selected-window
586 (pop-to-buffer nil)
587 (git-status dir))))
588
58f72f16
GH
589(defun stgit-goal-column ()
590 "Return goal column for the current line"
50d88c67
DK
591 (case (get-text-property (point) 'entry-type)
592 ('patch 2)
593 ('file 4)
594 (t 0)))
58f72f16
GH
595
596(defun stgit-next-line (&optional arg)
378a003d 597 "Move cursor vertically down ARG lines"
58f72f16
GH
598 (interactive "p")
599 (next-line arg)
600 (move-to-column (stgit-goal-column)))
378a003d 601
58f72f16 602(defun stgit-previous-line (&optional arg)
378a003d 603 "Move cursor vertically up ARG lines"
58f72f16
GH
604 (interactive "p")
605 (previous-line arg)
606 (move-to-column (stgit-goal-column)))
378a003d
GH
607
608(defun stgit-next-patch (&optional arg)
98230edd 609 "Move cursor down ARG patches."
378a003d 610 (interactive "p")
98230edd
DK
611 (ewoc-goto-next stgit-ewoc (or arg 1))
612 (move-to-column goal-column))
378a003d
GH
613
614(defun stgit-previous-patch (&optional arg)
98230edd 615 "Move cursor up ARG patches."
378a003d 616 (interactive "p")
98230edd
DK
617 (ewoc-goto-prev stgit-ewoc (or arg 1))
618 (move-to-column goal-column))
378a003d 619
56d81fe5
DK
620(defvar stgit-mode-hook nil
621 "Run after `stgit-mode' is setup.")
622
623(defvar stgit-mode-map nil
624 "Keymap for StGit major mode.")
625
626(unless stgit-mode-map
ce3b6130
DK
627 (let ((toggle-map (make-keymap)))
628 (suppress-keymap toggle-map)
629 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
630 '(("t" . stgit-toggle-worktree)))
631 (setq stgit-mode-map (make-keymap))
632 (suppress-keymap stgit-mode-map)
633 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
634 `((" " . stgit-mark)
635 ("m" . stgit-mark)
636 ("\d" . stgit-unmark-up)
637 ("u" . stgit-unmark-down)
638 ("?" . stgit-help)
639 ("h" . stgit-help)
640 ("\C-p" . stgit-previous-line)
641 ("\C-n" . stgit-next-line)
642 ([up] . stgit-previous-line)
643 ([down] . stgit-next-line)
644 ("p" . stgit-previous-patch)
645 ("n" . stgit-next-patch)
646 ("\M-{" . stgit-previous-patch)
647 ("\M-}" . stgit-next-patch)
648 ("s" . stgit-git-status)
408fa7cb 649 ("g" . stgit-reload-or-repair)
ce3b6130
DK
650 ("r" . stgit-refresh)
651 ("\C-c\C-r" . stgit-rename)
652 ("e" . stgit-edit)
653 ("M" . stgit-move-patches)
654 ("S" . stgit-squash)
655 ("N" . stgit-new)
e9fdd4ea
GH
656 ("\C-c\C-c" . stgit-commit)
657 ("\C-c\C-u" . stgit-uncommit)
3959a095 658 ("U" . stgit-revert-file)
51783171 659 ("R" . stgit-resolve-file)
ce3b6130
DK
660 ("\r" . stgit-select)
661 ("o" . stgit-find-file-other-window)
662 ("i" . stgit-file-toggle-index)
663 (">" . stgit-push-next)
664 ("<" . stgit-pop-next)
665 ("P" . stgit-push-or-pop)
666 ("G" . stgit-goto)
667 ("=" . stgit-show)
668 ("D" . stgit-delete)
669 ([(control ?/)] . stgit-undo)
670 ("\C-_" . stgit-undo)
671 ("B" . stgit-branch)
672 ("t" . ,toggle-map)
673 ("q" . stgit-quit)))))
56d81fe5
DK
674
675(defun stgit-mode ()
676 "Major mode for interacting with StGit.
677Commands:
678\\{stgit-mode-map}"
679 (kill-all-local-variables)
680 (buffer-disable-undo)
681 (setq mode-name "StGit"
682 major-mode 'stgit-mode
683 goal-column 2)
684 (use-local-map stgit-mode-map)
685 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 686 (set (make-local-variable 'stgit-marked-patches) nil)
6467d976 687 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
ce3b6130 688 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
689 (set (make-local-variable 'stgit-index-node) nil)
690 (set (make-local-variable 'stgit-worktree-node) nil)
2870f8b8 691 (set-variable 'truncate-lines 't)
b894e680 692 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
693 (run-hooks 'stgit-mode-hook))
694
b894e680
DK
695(defun stgit-update-saved-file ()
696 (let* ((file (expand-file-name buffer-file-name))
697 (dir (file-name-directory file))
698 (gitdir (condition-case nil (git-get-top-dir dir)
699 (error nil)))
700 (buffer (and gitdir (stgit-find-buffer gitdir))))
701 (when buffer
702 (with-current-buffer buffer
210a2a52 703 (stgit-refresh-worktree)))))
b894e680 704
d51722b7
GH
705(defun stgit-add-mark (patchsym)
706 "Mark the patch PATCHSYM."
8036afdd 707 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 708
d51722b7
GH
709(defun stgit-remove-mark (patchsym)
710 "Unmark the patch PATCHSYM."
8036afdd 711 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 712
e6b1fdae 713(defun stgit-clear-marks ()
47271f41 714 "Unmark all patches."
e6b1fdae
DK
715 (setq stgit-marked-patches '()))
716
735cb7ec 717(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
718 (get-text-property (point) 'patch-data))
719
64ada6f5 720(defun stgit-patch-name-at-point (&optional cause-error only-patches)
d51722b7 721 "Return the patch name on the current line as a symbol.
64ada6f5
GH
722If CAUSE-ERROR is not nil, signal an error if none found.
723If ONLY-PATCHES is not nil, only allow real patches, and not
724index or work tree."
2c862b07 725 (let ((patch (stgit-patch-at-point)))
64ada6f5
GH
726 (and patch
727 only-patches
728 (memq (stgit-patch-status patch) '(work index))
729 (setq patch nil))
2c862b07
DK
730 (cond (patch
731 (stgit-patch-name patch))
732 (cause-error
733 (error "No patch on this line")))))
378a003d 734
3164eec6
DK
735(defun stgit-patched-file-at-point ()
736 (get-text-property (point) 'file-data))
56d81fe5 737
7755d7f1 738(defun stgit-patches-marked-or-at-point ()
d51722b7 739 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 740 (if stgit-marked-patches
d51722b7 741 stgit-marked-patches
2c862b07 742 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
743 (if patch
744 (list patch)
745 '()))))
746
d51722b7
GH
747(defun stgit-goto-patch (patchsym)
748 "Move point to the line containing patch PATCHSYM.
f9b82d36
DK
749If that patch cannot be found, do nothing."
750 (let ((node (ewoc-nth stgit-ewoc 0)))
751 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
752 patchsym)))
753 (setq node (ewoc-next stgit-ewoc node)))
754 (when node
755 (ewoc-goto-node stgit-ewoc node)
d51722b7 756 (move-to-column goal-column))))
56d81fe5 757
1c2426dc 758(defun stgit-init ()
a53347d9 759 "Run stg init."
1c2426dc
DK
760 (interactive)
761 (stgit-capture-output nil
b0424080 762 (stgit-run "init"))
1f0bf00f 763 (stgit-reload))
1c2426dc 764
6df83d42 765(defun stgit-mark ()
a53347d9 766 "Mark the patch under point."
6df83d42 767 (interactive)
8036afdd 768 (let* ((node (ewoc-locate stgit-ewoc))
64ada6f5
GH
769 (patch (ewoc-data node))
770 (name (stgit-patch-name patch)))
771 (when (eq name :work)
772 (error "Cannot mark the work tree"))
773 (when (eq name :index)
774 (error "Cannot mark the index"))
8036afdd
DK
775 (stgit-add-mark (stgit-patch-name patch))
776 (ewoc-invalidate stgit-ewoc node))
378a003d 777 (stgit-next-patch))
6df83d42 778
9b151b27 779(defun stgit-unmark-up ()
a53347d9 780 "Remove mark from the patch on the previous line."
6df83d42 781 (interactive)
378a003d 782 (stgit-previous-patch)
8036afdd
DK
783 (let* ((node (ewoc-locate stgit-ewoc))
784 (patch (ewoc-data node)))
785 (stgit-remove-mark (stgit-patch-name patch))
786 (ewoc-invalidate stgit-ewoc node))
787 (move-to-column (stgit-goal-column)))
9b151b27
GH
788
789(defun stgit-unmark-down ()
a53347d9 790 "Remove mark from the patch on the current line."
9b151b27 791 (interactive)
8036afdd
DK
792 (let* ((node (ewoc-locate stgit-ewoc))
793 (patch (ewoc-data node)))
794 (stgit-remove-mark (stgit-patch-name patch))
795 (ewoc-invalidate stgit-ewoc node))
1288eda2 796 (stgit-next-patch))
6df83d42 797
56d81fe5 798(defun stgit-rename (name)
018fa1ac 799 "Rename the patch under point to NAME."
64ada6f5
GH
800 (interactive (list
801 (read-string "Patch name: "
802 (symbol-name (stgit-patch-name-at-point t t)))))
803 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 804 (stgit-capture-output nil
d51722b7
GH
805 (stgit-run "rename" old-patchsym name))
806 (let ((name-sym (intern name)))
807 (when (memq old-patchsym stgit-expanded-patches)
378a003d 808 (setq stgit-expanded-patches
d51722b7
GH
809 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
810 (when (memq old-patchsym stgit-marked-patches)
378a003d 811 (setq stgit-marked-patches
d51722b7
GH
812 (cons name-sym (delq old-patchsym stgit-marked-patches))))
813 (stgit-reload)
814 (stgit-goto-patch name-sym))))
56d81fe5 815
408fa7cb
GH
816(defun stgit-reload-or-repair (repair)
817 "Update the contents of the StGit buffer (`stgit-reload').
818
819With a prefix argument, repair the StGit metadata if the branch
820was modified with git commands (`stgit-repair')."
821 (interactive "P")
822 (if repair
823 (stgit-repair)
824 (stgit-reload)))
825
26201d96 826(defun stgit-repair ()
a53347d9 827 "Run stg repair."
26201d96
DK
828 (interactive)
829 (stgit-capture-output nil
b0424080 830 (stgit-run "repair"))
1f0bf00f 831 (stgit-reload))
26201d96 832
adeef6bc
GH
833(defun stgit-available-branches ()
834 "Returns a list of the available stg branches"
835 (let ((output (with-output-to-string
836 (stgit-run "branch" "--list")))
837 (start 0)
838 result)
839 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
840 (setq result (cons (match-string 1 output) result))
841 (setq start (match-end 0)))
842 result))
843
844(defun stgit-branch (branch)
845 "Switch to branch BRANCH."
846 (interactive (list (completing-read "Switch to branch: "
847 (stgit-available-branches))))
848 (stgit-capture-output nil (stgit-run "branch" "--" branch))
849 (stgit-reload))
850
41c1c59c
GH
851(defun stgit-commit (count)
852 "Run stg commit on COUNT commits.
853Interactively, the prefix argument is used as COUNT."
854 (interactive "p")
855 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1f0bf00f 856 (stgit-reload))
c4aad9a7 857
3959a095
GH
858(defun stgit-revert-file ()
859 "Revert the file at point, which must be in the index or the
860working tree."
861 (interactive)
862 (let* ((patched-file (or (stgit-patched-file-at-point)
863 (error "No file on the current line")))
864 (patch-name (stgit-patch-name-at-point))
865 (file-status (stgit-file-status patched-file))
866 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
867 (stgit-file-cr-to patched-file))
868 ((eq file-status 'add)
869 (stgit-file-file patched-file))))
870 (co-file (cond ((eq file-status 'rename)
871 (stgit-file-cr-from patched-file))
872 ((not (memq file-status '(copy add)))
873 (stgit-file-file patched-file)))))
874
875 (unless (memq patch-name '(:work :index))
876 (error "No index or working tree file on this line"))
877
878 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
879 (when (yes-or-no-p (format "Revert %d file%s? "
880 nfiles
881 (if (= nfiles 1) "" "s")))
882 (stgit-capture-output nil
883 (when rm-file
884 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
885 (when co-file
886 (stgit-run-git "checkout" "HEAD" co-file)))
887 (stgit-reload)))))
888
51783171
GH
889(defun stgit-resolve-file ()
890 "Resolve conflict in the file at point."
891 (interactive)
892 (let* ((patched-file (stgit-patched-file-at-point))
893 (patch (stgit-patch-at-point))
894 (patch-name (and patch (stgit-patch-name patch)))
895 (status (and patched-file (stgit-file-status patched-file))))
896
897 (unless (memq patch-name '(:work :index))
898 (error "No index or working tree file on this line"))
899
900 (unless (eq status 'unmerged)
901 (error "No conflict to resolve at the current line"))
902
903 (stgit-capture-output nil
904 (stgit-move-change-to-index (stgit-file-file patched-file)))
905
906 (stgit-reload)))
907
41c1c59c
GH
908(defun stgit-uncommit (count)
909 "Run stg uncommit on COUNT commits.
910Interactively, the prefix argument is used as COUNT."
c4aad9a7 911 (interactive "p")
41c1c59c 912 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1f0bf00f 913 (stgit-reload))
c4aad9a7 914
0b661144
DK
915(defun stgit-push-next (npatches)
916 "Push the first unapplied patch.
917With numeric prefix argument, push that many patches."
918 (interactive "p")
d51722b7 919 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
920 (stgit-reload)
921 (stgit-refresh-git-status))
56d81fe5 922
0b661144
DK
923(defun stgit-pop-next (npatches)
924 "Pop the topmost applied patch.
925With numeric prefix argument, pop that many patches."
926 (interactive "p")
d51722b7 927 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
928 (stgit-reload)
929 (stgit-refresh-git-status))
56d81fe5 930
f9182fca
KH
931(defun stgit-applied-at-point ()
932 "Is the patch on the current line applied?"
933 (save-excursion
934 (beginning-of-line)
935 (looking-at "[>+]")))
936
937(defun stgit-push-or-pop ()
a53347d9 938 "Push or pop the patch on the current line."
f9182fca 939 (interactive)
2c862b07 940 (let ((patchsym (stgit-patch-name-at-point t))
f9182fca
KH
941 (applied (stgit-applied-at-point)))
942 (stgit-capture-output nil
d51722b7 943 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 944 (stgit-reload)))
f9182fca 945
c7adf5ef 946(defun stgit-goto ()
a53347d9 947 "Go to the patch on the current line."
c7adf5ef 948 (interactive)
2c862b07 949 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 950 (stgit-capture-output nil
d51722b7 951 (stgit-run "goto" patchsym))
1f0bf00f 952 (stgit-reload)))
c7adf5ef 953
d51722b7 954(defun stgit-id (patchsym)
50d88c67
DK
955 "Return the git commit id for PATCHSYM.
956If PATCHSYM is a keyword, returns PATCHSYM unmodified."
957 (if (keywordp patchsym)
958 patchsym
959 (let ((result (with-output-to-string
960 (stgit-run-silent "id" patchsym))))
961 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
962 (error "Cannot find commit id for %s" patchsym))
963 (match-string 1 result))))
378a003d 964
56d81fe5 965(defun stgit-show ()
a53347d9 966 "Show the patch on the current line."
56d81fe5
DK
967 (interactive)
968 (stgit-capture-output "*StGit patch*"
50d88c67
DK
969 (case (get-text-property (point) 'entry-type)
970 ('file
3164eec6
DK
971 (let* ((patched-file (stgit-patched-file-at-point))
972 (patch-name (stgit-patch-name-at-point))
973 (patch-id (stgit-id patch-name))
974 (args (append (and (stgit-file-cr-from patched-file)
7567401c 975 (list (stgit-find-copies-harder-diff-arg)))
b894e680
DK
976 (cond ((eq patch-id :index)
977 '("--cached"))
978 ((eq patch-id :work)
979 nil)
980 (t
981 (list (concat patch-id "^") patch-id)))
3164eec6
DK
982 '("--")
983 (if (stgit-file-copy-or-rename patched-file)
984 (list (stgit-file-cr-from patched-file)
985 (stgit-file-cr-to patched-file))
986 (list (stgit-file-file patched-file))))))
987 (apply 'stgit-run-git "diff" args)))
50d88c67 988 ('patch
4713c50e
GH
989 (let* ((patch-name (stgit-patch-name-at-point))
990 (patch-id (stgit-id patch-name)))
991 (if (or (eq patch-id :index) (eq patch-id :work))
992 (apply 'stgit-run-git "diff"
993 (stgit-find-copies-harder-diff-arg)
994 (and (eq patch-id :index)
995 '("--cached")))
996 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
997 (stgit-patch-name-at-point)))))
50d88c67
DK
998 (t
999 (error "No patch or file at point")))
1000 (with-current-buffer standard-output
1001 (goto-char (point-min))
1002 (diff-mode))))
0663524d 1003
fd9fe574 1004(defun stgit-move-change-to-index (file)
37cb5766 1005 "Copies the workspace state of FILE to index, using git add or git rm"
306b37a6
GH
1006 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1007 '("add") '("rm" "-q"))))
37cb5766 1008 (stgit-capture-output "*git output*"
5115dea0 1009 (apply 'stgit-run-git (append op '("--") (list file))))))
37cb5766 1010
fd9fe574 1011(defun stgit-remove-change-from-index (file)
37cb5766
DK
1012 "Unstages the change in FILE from the index"
1013 (stgit-capture-output "*git output*"
1014 (stgit-run-git "reset" "-q" "--" file)))
1015
1016(defun stgit-file-toggle-index ()
1017 "Move modified file in or out of the index."
1018 (interactive)
1019 (let ((patched-file (stgit-patched-file-at-point)))
1020 (unless patched-file
1021 (error "No file on the current line"))
51783171
GH
1022 (when (eq (stgit-file-status patched-file) 'unmerged)
1023 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
37cb5766
DK
1024 (let ((patch-name (stgit-patch-name-at-point)))
1025 (cond ((eq patch-name :work)
fd9fe574 1026 (stgit-move-change-to-index (stgit-file-file patched-file)))
37cb5766 1027 ((eq patch-name :index)
fd9fe574 1028 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766
DK
1029 (t
1030 (error "Can only move files in the working tree to index")))))
210a2a52
DK
1031 (stgit-refresh-worktree)
1032 (stgit-refresh-index))
37cb5766 1033
0bca35c8 1034(defun stgit-edit ()
a53347d9 1035 "Edit the patch on the current line."
0bca35c8 1036 (interactive)
64ada6f5 1037 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 1038 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
1039 (dir default-directory))
1040 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 1041 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
1042 (setq default-directory dir)
1043 (let ((standard-output edit-buf))
d51722b7 1044 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
1045
1046(defun stgit-confirm-edit ()
1047 (interactive)
1048 (let ((file (make-temp-file "stgit-edit-")))
1049 (write-region (point-min) (point-max) file)
1050 (stgit-capture-output nil
d51722b7 1051 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 1052 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1053 (stgit-reload))))
0bca35c8 1054
aa04f831
GH
1055(defun stgit-new (add-sign)
1056 "Create a new patch.
1057With a prefix argument, include a \"Signed-off-by:\" line at the
1058end of the patch."
1059 (interactive "P")
c5d45b92
GH
1060 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1061 (dir default-directory))
1062 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831
GH
1063 (setq default-directory dir)
1064 (when add-sign
1065 (save-excursion
1066 (let ((standard-output (current-buffer)))
1067 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
1068
1069(defun stgit-confirm-new ()
1070 (interactive)
27b0f9e4 1071 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
1072 (write-region (point-min) (point-max) file)
1073 (stgit-capture-output nil
27b0f9e4 1074 (stgit-run "new" "-f" file))
64c097a0 1075 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1076 (stgit-reload))))
64c097a0
DK
1077
1078(defun stgit-create-patch-name (description)
1079 "Create a patch name from a long description"
1080 (let ((patch ""))
1081 (while (> (length description) 0)
1082 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
1083 (setq patch (downcase (concat patch
1084 (match-string 0 description))))
64c097a0
DK
1085 (setq description (substring description (match-end 0))))
1086 ((string-match "\\` +" description)
1087 (setq patch (concat patch "-"))
1088 (setq description (substring description (match-end 0))))
1089 ((string-match "\\`[^a-zA-Z_-]+" description)
1090 (setq description (substring description (match-end 0))))))
1091 (cond ((= (length patch) 0)
1092 "patch")
1093 ((> (length patch) 20)
1094 (substring patch 0 20))
1095 (t patch))))
0bca35c8 1096
9008e45b 1097(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 1098 "Delete the patches in PATCHSYMS.
9008e45b
GH
1099Interactively, delete the marked patches, or the patch at point.
1100
1101With a prefix argument, or SPILL-P, spill the patch contents to
1102the work tree and index."
1103 (interactive (list (stgit-patches-marked-or-at-point)
1104 current-prefix-arg))
e7231e4f
GH
1105 (unless patchsyms
1106 (error "No patches to delete"))
64ada6f5
GH
1107 (when (memq :index patchsyms)
1108 (error "Cannot delete the index"))
1109 (when (memq :work patchsyms)
1110 (error "Cannot delete the work tree"))
1111
d51722b7 1112 (let ((npatches (length patchsyms)))
9008e45b 1113 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 1114 npatches
9008e45b
GH
1115 (if (= 1 npatches) "" "es")
1116 (if spill-p
1117 " (spilling contents to index)"
1118 "")))
1119 (let ((args (if spill-p
1120 (cons "--spill" patchsyms)
1121 patchsyms)))
1122 (stgit-capture-output nil
1123 (apply 'stgit-run "delete" args))
1124 (stgit-reload)))))
d51722b7 1125
7cc45294
GH
1126(defun stgit-move-patches-target ()
1127 "Return the patchsym indicating a target patch for
1128`stgit-move-patches'.
1129
1130This is either the patch at point, or one of :top and :bottom, if
1131the point is after or before the applied patches."
1132
2c862b07 1133 (let ((patchsym (stgit-patch-name-at-point)))
7cc45294
GH
1134 (cond (patchsym patchsym)
1135 ((save-excursion (re-search-backward "^>" nil t)) :top)
1136 (t :bottom))))
1137
95369f6c
GH
1138(defun stgit-sort-patches (patchsyms)
1139 "Returns the list of patches in PATCHSYMS sorted according to
1140their position in the patch series, bottommost first.
1141
1142PATCHSYMS may not contain duplicate entries."
1143 (let (sorted-patchsyms
1144 (series (with-output-to-string
1145 (with-current-buffer standard-output
1146 (stgit-run-silent "series" "--noprefix"))))
1147 start)
1148 (while (string-match "^\\(.+\\)" series start)
1149 (let ((patchsym (intern (match-string 1 series))))
1150 (when (memq patchsym patchsyms)
1151 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1152 (setq start (match-end 0)))
1153 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1154
1155 (unless (= (length patchsyms) (length sorted-patchsyms))
1156 (error "Internal error"))
1157
1158 sorted-patchsyms))
1159
7cc45294
GH
1160(defun stgit-move-patches (patchsyms target-patch)
1161 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1162If TARGET-PATCH is :bottom or :top, move the patches to the
1163bottom or top of the stack, respectively.
1164
1165Interactively, move the marked patches to where the point is."
1166 (interactive (list stgit-marked-patches
1167 (stgit-move-patches-target)))
1168 (unless patchsyms
1169 (error "Need at least one patch to move"))
1170
1171 (unless target-patch
1172 (error "Point not at a patch"))
1173
1174 (if (eq target-patch :top)
1175 (stgit-capture-output nil
1176 (apply 'stgit-run "float" patchsyms))
1177
1178 ;; need to have patchsyms sorted by position in the stack
95369f6c 1179 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1180 (while sorted-patchsyms
1181 (setq sorted-patchsyms
1182 (and (stgit-capture-output nil
1183 (if (eq target-patch :bottom)
1184 (stgit-run "sink" "--" (car sorted-patchsyms))
1185 (stgit-run "sink" "--to" target-patch "--"
1186 (car sorted-patchsyms))))
1187 (cdr sorted-patchsyms))))))
1188 (stgit-reload))
1189
594aa463
KH
1190(defun stgit-squash (patchsyms)
1191 "Squash the patches in PATCHSYMS.
693d179b
GH
1192Interactively, squash the marked patches.
1193
1194Unless there are any conflicts, the patches will be merged into
1195one patch, which will occupy the same spot in the series as the
1196deepest patch had before the squash."
d51722b7
GH
1197 (interactive (list stgit-marked-patches))
1198 (when (< (length patchsyms) 2)
594aa463 1199 (error "Need at least two patches to squash"))
32d7545d
GH
1200 (let ((stgit-buffer (current-buffer))
1201 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1202 (dir default-directory)
1203 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1204 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1205 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1206 (setq default-directory dir)
32d7545d
GH
1207 (let ((result (let ((standard-output edit-buf))
1208 (apply 'stgit-run-silent "squash"
1209 "--save-template=-" sorted-patchsyms))))
1210
1211 ;; stg squash may have reordered the patches or caused conflicts
1212 (with-current-buffer stgit-buffer
1213 (stgit-reload))
1214
1215 (unless (eq 0 result)
1216 (fundamental-mode)
1217 (rename-buffer "*StGit error*")
1218 (resize-temp-buffer-window)
1219 (switch-to-buffer-other-window stgit-buffer)
1220 (error "stg squash failed")))))
ea0def18 1221
594aa463 1222(defun stgit-confirm-squash ()
ea0def18
DK
1223 (interactive)
1224 (let ((file (make-temp-file "stgit-edit-")))
1225 (write-region (point-min) (point-max) file)
1226 (stgit-capture-output nil
594aa463 1227 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1228 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1229 (stgit-clear-marks)
1230 ;; Go to first marked patch and stay there
1231 (goto-char (point-min))
1232 (re-search-forward (concat "^[>+-]\\*") nil t)
1233 (move-to-column goal-column)
1234 (let ((pos (point)))
1f0bf00f 1235 (stgit-reload)
e6b1fdae 1236 (goto-char pos)))))
ea0def18 1237
0663524d
KH
1238(defun stgit-help ()
1239 "Display help for the StGit mode."
1240 (interactive)
1241 (describe-function 'stgit-mode))
3a59f3db 1242
83e51dbf
DK
1243(defun stgit-undo (&optional arg)
1244 "Run stg undo.
1245With prefix argument, run it with the --hard flag."
1246 (interactive "P")
1247 (stgit-capture-output nil
1248 (if arg
1249 (stgit-run "undo" "--hard")
1250 (stgit-run "undo")))
1f0bf00f 1251 (stgit-reload))
83e51dbf 1252
4d73c4d8
DK
1253(defun stgit-refresh (&optional arg)
1254 "Run stg refresh.
36a4eacd
GH
1255If the index contains any changes, only refresh from index.
1256
a53347d9 1257With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8
DK
1258 (interactive "P")
1259 (let ((patchargs (if arg
b0424080
GH
1260 (let ((patches (stgit-patches-marked-or-at-point)))
1261 (cond ((null patches)
df283a8b 1262 (error "No patch to update"))
b0424080 1263 ((> (length patches) 1)
df283a8b 1264 (error "Too many patches selected"))
b0424080
GH
1265 (t
1266 (cons "-p" patches))))
1267 nil)))
36a4eacd
GH
1268 (unless (stgit-index-empty-p)
1269 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 1270 (stgit-capture-output nil
074a4fb0
GH
1271 (apply 'stgit-run "refresh" patchargs))
1272 (stgit-refresh-git-status))
4d73c4d8
DK
1273 (stgit-reload))
1274
8f702de4
GH
1275(defcustom stgit-show-worktree-mode 'center
1276 "This variable controls where the \"Index\" and \"Work tree\"
1277will be shown on in the buffer.
1278
1279It can be set to 'top (above all patches), 'center (show between
1280applied and unapplied patches), and 'bottom (below all patches).
1281
1282See also `stgit-show-worktree'."
1283 :type '(radio (const :tag "above all patches (top)" top)
1284 (const :tag "between applied and unapplied patches (center)"
1285 center)
1286 (const :tag "below all patches (bottom)" bottom))
1287 :group 'stgit)
1288
ce3b6130
DK
1289(defcustom stgit-default-show-worktree
1290 nil
1291 "Set to non-nil to by default show the working tree in a new stgit buffer.
1292
1293This value is used as the default value for `stgit-show-worktree'."
1294 :type 'boolean
1295 :group 'stgit)
1296
1297(defvar stgit-show-worktree nil
8f702de4 1298 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 1299
8f702de4 1300See also `stgit-show-worktree-mode'.")
ce3b6130
DK
1301
1302(defun stgit-toggle-worktree (&optional arg)
1303 "Toggle the visibility of the work tree.
1304With arg, show the work tree if arg is positive.
1305
8f702de4
GH
1306Its initial setting is controlled by `stgit-default-show-worktree'.
1307
1308`stgit-show-worktree-mode' controls where on screen the index and
1309work tree will show up."
ce3b6130
DK
1310 (interactive)
1311 (setq stgit-show-worktree
1312 (if (numberp arg)
1313 (> arg 0)
1314 (not stgit-show-worktree)))
1315 (stgit-reload))
1316
3a59f3db 1317(provide 'stgit)