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