stgit.el: Document stgit-select properly
[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 556(defun stgit-select ()
da01a29b
GH
557 "With point on a patch, toggle showing files in the patch.
558
559With point on a file, open the associated file. Opens the target
560file for (applied) copies and renames."
378a003d 561 (interactive)
50d88c67
DK
562 (case (get-text-property (point) 'entry-type)
563 ('patch
564 (stgit-select-patch))
565 ('file
566 (stgit-select-file))
567 (t
568 (error "No patch or file on line"))))
378a003d
GH
569
570(defun stgit-find-file-other-window ()
571 "Open file at point in other window"
572 (interactive)
573 (let ((patched-file (stgit-patched-file-at-point)))
574 (unless patched-file
575 (error "No file on the current line"))
3164eec6 576 (let ((filename (expand-file-name (stgit-file-file patched-file))))
378a003d
GH
577 (unless (file-exists-p filename)
578 (error "File does not exist"))
579 (find-file-other-window filename))))
580
83327d53 581(defun stgit-quit ()
a53347d9 582 "Hide the stgit buffer."
83327d53
GH
583 (interactive)
584 (bury-buffer))
585
0f076fe6 586(defun stgit-git-status ()
a53347d9 587 "Show status using `git-status'."
0f076fe6
GH
588 (interactive)
589 (unless (fboundp 'git-status)
df283a8b 590 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
591 (let ((dir default-directory))
592 (save-selected-window
593 (pop-to-buffer nil)
594 (git-status dir))))
595
58f72f16
GH
596(defun stgit-goal-column ()
597 "Return goal column for the current line"
50d88c67
DK
598 (case (get-text-property (point) 'entry-type)
599 ('patch 2)
600 ('file 4)
601 (t 0)))
58f72f16
GH
602
603(defun stgit-next-line (&optional arg)
378a003d 604 "Move cursor vertically down ARG lines"
58f72f16
GH
605 (interactive "p")
606 (next-line arg)
607 (move-to-column (stgit-goal-column)))
378a003d 608
58f72f16 609(defun stgit-previous-line (&optional arg)
378a003d 610 "Move cursor vertically up ARG lines"
58f72f16
GH
611 (interactive "p")
612 (previous-line arg)
613 (move-to-column (stgit-goal-column)))
378a003d
GH
614
615(defun stgit-next-patch (&optional arg)
98230edd 616 "Move cursor down ARG patches."
378a003d 617 (interactive "p")
98230edd
DK
618 (ewoc-goto-next stgit-ewoc (or arg 1))
619 (move-to-column goal-column))
378a003d
GH
620
621(defun stgit-previous-patch (&optional arg)
98230edd 622 "Move cursor up ARG patches."
378a003d 623 (interactive "p")
98230edd
DK
624 (ewoc-goto-prev stgit-ewoc (or arg 1))
625 (move-to-column goal-column))
378a003d 626
56d81fe5
DK
627(defvar stgit-mode-hook nil
628 "Run after `stgit-mode' is setup.")
629
630(defvar stgit-mode-map nil
631 "Keymap for StGit major mode.")
632
633(unless stgit-mode-map
ce3b6130
DK
634 (let ((toggle-map (make-keymap)))
635 (suppress-keymap toggle-map)
636 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
637 '(("t" . stgit-toggle-worktree)))
638 (setq stgit-mode-map (make-keymap))
639 (suppress-keymap stgit-mode-map)
640 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
641 `((" " . stgit-mark)
642 ("m" . stgit-mark)
643 ("\d" . stgit-unmark-up)
644 ("u" . stgit-unmark-down)
645 ("?" . stgit-help)
646 ("h" . stgit-help)
647 ("\C-p" . stgit-previous-line)
648 ("\C-n" . stgit-next-line)
649 ([up] . stgit-previous-line)
650 ([down] . stgit-next-line)
651 ("p" . stgit-previous-patch)
652 ("n" . stgit-next-patch)
653 ("\M-{" . stgit-previous-patch)
654 ("\M-}" . stgit-next-patch)
655 ("s" . stgit-git-status)
408fa7cb 656 ("g" . stgit-reload-or-repair)
ce3b6130
DK
657 ("r" . stgit-refresh)
658 ("\C-c\C-r" . stgit-rename)
659 ("e" . stgit-edit)
660 ("M" . stgit-move-patches)
661 ("S" . stgit-squash)
662 ("N" . stgit-new)
e9fdd4ea
GH
663 ("\C-c\C-c" . stgit-commit)
664 ("\C-c\C-u" . stgit-uncommit)
3959a095 665 ("U" . stgit-revert-file)
51783171 666 ("R" . stgit-resolve-file)
ce3b6130
DK
667 ("\r" . stgit-select)
668 ("o" . stgit-find-file-other-window)
669 ("i" . stgit-file-toggle-index)
670 (">" . stgit-push-next)
671 ("<" . stgit-pop-next)
672 ("P" . stgit-push-or-pop)
673 ("G" . stgit-goto)
674 ("=" . stgit-show)
675 ("D" . stgit-delete)
676 ([(control ?/)] . stgit-undo)
677 ("\C-_" . stgit-undo)
678 ("B" . stgit-branch)
679 ("t" . ,toggle-map)
680 ("q" . stgit-quit)))))
56d81fe5
DK
681
682(defun stgit-mode ()
683 "Major mode for interacting with StGit.
684Commands:
685\\{stgit-mode-map}"
686 (kill-all-local-variables)
687 (buffer-disable-undo)
688 (setq mode-name "StGit"
689 major-mode 'stgit-mode
690 goal-column 2)
691 (use-local-map stgit-mode-map)
692 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 693 (set (make-local-variable 'stgit-marked-patches) nil)
6467d976 694 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
ce3b6130 695 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
696 (set (make-local-variable 'stgit-index-node) nil)
697 (set (make-local-variable 'stgit-worktree-node) nil)
2870f8b8 698 (set-variable 'truncate-lines 't)
b894e680 699 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
700 (run-hooks 'stgit-mode-hook))
701
b894e680
DK
702(defun stgit-update-saved-file ()
703 (let* ((file (expand-file-name buffer-file-name))
704 (dir (file-name-directory file))
705 (gitdir (condition-case nil (git-get-top-dir dir)
706 (error nil)))
707 (buffer (and gitdir (stgit-find-buffer gitdir))))
708 (when buffer
709 (with-current-buffer buffer
210a2a52 710 (stgit-refresh-worktree)))))
b894e680 711
d51722b7
GH
712(defun stgit-add-mark (patchsym)
713 "Mark the patch PATCHSYM."
8036afdd 714 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 715
d51722b7
GH
716(defun stgit-remove-mark (patchsym)
717 "Unmark the patch PATCHSYM."
8036afdd 718 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 719
e6b1fdae 720(defun stgit-clear-marks ()
47271f41 721 "Unmark all patches."
e6b1fdae
DK
722 (setq stgit-marked-patches '()))
723
735cb7ec 724(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
725 (get-text-property (point) 'patch-data))
726
64ada6f5 727(defun stgit-patch-name-at-point (&optional cause-error only-patches)
d51722b7 728 "Return the patch name on the current line as a symbol.
64ada6f5
GH
729If CAUSE-ERROR is not nil, signal an error if none found.
730If ONLY-PATCHES is not nil, only allow real patches, and not
731index or work tree."
2c862b07 732 (let ((patch (stgit-patch-at-point)))
64ada6f5
GH
733 (and patch
734 only-patches
735 (memq (stgit-patch-status patch) '(work index))
736 (setq patch nil))
2c862b07
DK
737 (cond (patch
738 (stgit-patch-name patch))
739 (cause-error
740 (error "No patch on this line")))))
378a003d 741
3164eec6
DK
742(defun stgit-patched-file-at-point ()
743 (get-text-property (point) 'file-data))
56d81fe5 744
7755d7f1 745(defun stgit-patches-marked-or-at-point ()
d51722b7 746 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 747 (if stgit-marked-patches
d51722b7 748 stgit-marked-patches
2c862b07 749 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
750 (if patch
751 (list patch)
752 '()))))
753
d51722b7
GH
754(defun stgit-goto-patch (patchsym)
755 "Move point to the line containing patch PATCHSYM.
f9b82d36
DK
756If that patch cannot be found, do nothing."
757 (let ((node (ewoc-nth stgit-ewoc 0)))
758 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
759 patchsym)))
760 (setq node (ewoc-next stgit-ewoc node)))
761 (when node
762 (ewoc-goto-node stgit-ewoc node)
d51722b7 763 (move-to-column goal-column))))
56d81fe5 764
1c2426dc 765(defun stgit-init ()
a53347d9 766 "Run stg init."
1c2426dc
DK
767 (interactive)
768 (stgit-capture-output nil
b0424080 769 (stgit-run "init"))
1f0bf00f 770 (stgit-reload))
1c2426dc 771
6df83d42 772(defun stgit-mark ()
a53347d9 773 "Mark the patch under point."
6df83d42 774 (interactive)
8036afdd 775 (let* ((node (ewoc-locate stgit-ewoc))
64ada6f5
GH
776 (patch (ewoc-data node))
777 (name (stgit-patch-name patch)))
778 (when (eq name :work)
779 (error "Cannot mark the work tree"))
780 (when (eq name :index)
781 (error "Cannot mark the index"))
8036afdd
DK
782 (stgit-add-mark (stgit-patch-name patch))
783 (ewoc-invalidate stgit-ewoc node))
378a003d 784 (stgit-next-patch))
6df83d42 785
9b151b27 786(defun stgit-unmark-up ()
a53347d9 787 "Remove mark from the patch on the previous line."
6df83d42 788 (interactive)
378a003d 789 (stgit-previous-patch)
8036afdd
DK
790 (let* ((node (ewoc-locate stgit-ewoc))
791 (patch (ewoc-data node)))
792 (stgit-remove-mark (stgit-patch-name patch))
793 (ewoc-invalidate stgit-ewoc node))
794 (move-to-column (stgit-goal-column)))
9b151b27
GH
795
796(defun stgit-unmark-down ()
a53347d9 797 "Remove mark from the patch on the current line."
9b151b27 798 (interactive)
8036afdd
DK
799 (let* ((node (ewoc-locate stgit-ewoc))
800 (patch (ewoc-data node)))
801 (stgit-remove-mark (stgit-patch-name patch))
802 (ewoc-invalidate stgit-ewoc node))
1288eda2 803 (stgit-next-patch))
6df83d42 804
56d81fe5 805(defun stgit-rename (name)
018fa1ac 806 "Rename the patch under point to NAME."
64ada6f5
GH
807 (interactive (list
808 (read-string "Patch name: "
809 (symbol-name (stgit-patch-name-at-point t t)))))
810 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 811 (stgit-capture-output nil
d51722b7
GH
812 (stgit-run "rename" old-patchsym name))
813 (let ((name-sym (intern name)))
814 (when (memq old-patchsym stgit-expanded-patches)
378a003d 815 (setq stgit-expanded-patches
d51722b7
GH
816 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
817 (when (memq old-patchsym stgit-marked-patches)
378a003d 818 (setq stgit-marked-patches
d51722b7
GH
819 (cons name-sym (delq old-patchsym stgit-marked-patches))))
820 (stgit-reload)
821 (stgit-goto-patch name-sym))))
56d81fe5 822
408fa7cb
GH
823(defun stgit-reload-or-repair (repair)
824 "Update the contents of the StGit buffer (`stgit-reload').
825
826With a prefix argument, repair the StGit metadata if the branch
827was modified with git commands (`stgit-repair')."
828 (interactive "P")
829 (if repair
830 (stgit-repair)
831 (stgit-reload)))
832
26201d96 833(defun stgit-repair ()
a53347d9 834 "Run stg repair."
26201d96
DK
835 (interactive)
836 (stgit-capture-output nil
b0424080 837 (stgit-run "repair"))
1f0bf00f 838 (stgit-reload))
26201d96 839
adeef6bc
GH
840(defun stgit-available-branches ()
841 "Returns a list of the available stg branches"
842 (let ((output (with-output-to-string
843 (stgit-run "branch" "--list")))
844 (start 0)
845 result)
846 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
847 (setq result (cons (match-string 1 output) result))
848 (setq start (match-end 0)))
849 result))
850
851(defun stgit-branch (branch)
852 "Switch to branch BRANCH."
853 (interactive (list (completing-read "Switch to branch: "
854 (stgit-available-branches))))
855 (stgit-capture-output nil (stgit-run "branch" "--" branch))
856 (stgit-reload))
857
41c1c59c
GH
858(defun stgit-commit (count)
859 "Run stg commit on COUNT commits.
860Interactively, the prefix argument is used as COUNT."
861 (interactive "p")
862 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1f0bf00f 863 (stgit-reload))
c4aad9a7 864
3959a095
GH
865(defun stgit-revert-file ()
866 "Revert the file at point, which must be in the index or the
867working tree."
868 (interactive)
869 (let* ((patched-file (or (stgit-patched-file-at-point)
870 (error "No file on the current line")))
871 (patch-name (stgit-patch-name-at-point))
872 (file-status (stgit-file-status patched-file))
873 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
874 (stgit-file-cr-to patched-file))
875 ((eq file-status 'add)
876 (stgit-file-file patched-file))))
877 (co-file (cond ((eq file-status 'rename)
878 (stgit-file-cr-from patched-file))
879 ((not (memq file-status '(copy add)))
880 (stgit-file-file patched-file)))))
881
882 (unless (memq patch-name '(:work :index))
883 (error "No index or working tree file on this line"))
884
885 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
886 (when (yes-or-no-p (format "Revert %d file%s? "
887 nfiles
888 (if (= nfiles 1) "" "s")))
889 (stgit-capture-output nil
890 (when rm-file
891 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
892 (when co-file
893 (stgit-run-git "checkout" "HEAD" co-file)))
894 (stgit-reload)))))
895
51783171
GH
896(defun stgit-resolve-file ()
897 "Resolve conflict in the file at point."
898 (interactive)
899 (let* ((patched-file (stgit-patched-file-at-point))
900 (patch (stgit-patch-at-point))
901 (patch-name (and patch (stgit-patch-name patch)))
902 (status (and patched-file (stgit-file-status patched-file))))
903
904 (unless (memq patch-name '(:work :index))
905 (error "No index or working tree file on this line"))
906
907 (unless (eq status 'unmerged)
908 (error "No conflict to resolve at the current line"))
909
910 (stgit-capture-output nil
911 (stgit-move-change-to-index (stgit-file-file patched-file)))
912
913 (stgit-reload)))
914
41c1c59c
GH
915(defun stgit-uncommit (count)
916 "Run stg uncommit on COUNT commits.
917Interactively, the prefix argument is used as COUNT."
c4aad9a7 918 (interactive "p")
41c1c59c 919 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1f0bf00f 920 (stgit-reload))
c4aad9a7 921
0b661144
DK
922(defun stgit-push-next (npatches)
923 "Push the first unapplied patch.
924With numeric prefix argument, push that many patches."
925 (interactive "p")
d51722b7 926 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
927 (stgit-reload)
928 (stgit-refresh-git-status))
56d81fe5 929
0b661144
DK
930(defun stgit-pop-next (npatches)
931 "Pop the topmost applied patch.
932With numeric prefix argument, pop that many patches."
933 (interactive "p")
d51722b7 934 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
935 (stgit-reload)
936 (stgit-refresh-git-status))
56d81fe5 937
f9182fca
KH
938(defun stgit-applied-at-point ()
939 "Is the patch on the current line applied?"
940 (save-excursion
941 (beginning-of-line)
942 (looking-at "[>+]")))
943
944(defun stgit-push-or-pop ()
a53347d9 945 "Push or pop the patch on the current line."
f9182fca 946 (interactive)
2c862b07 947 (let ((patchsym (stgit-patch-name-at-point t))
f9182fca
KH
948 (applied (stgit-applied-at-point)))
949 (stgit-capture-output nil
d51722b7 950 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 951 (stgit-reload)))
f9182fca 952
c7adf5ef 953(defun stgit-goto ()
a53347d9 954 "Go to the patch on the current line."
c7adf5ef 955 (interactive)
2c862b07 956 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 957 (stgit-capture-output nil
d51722b7 958 (stgit-run "goto" patchsym))
1f0bf00f 959 (stgit-reload)))
c7adf5ef 960
d51722b7 961(defun stgit-id (patchsym)
50d88c67
DK
962 "Return the git commit id for PATCHSYM.
963If PATCHSYM is a keyword, returns PATCHSYM unmodified."
964 (if (keywordp patchsym)
965 patchsym
966 (let ((result (with-output-to-string
967 (stgit-run-silent "id" patchsym))))
968 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
969 (error "Cannot find commit id for %s" patchsym))
970 (match-string 1 result))))
378a003d 971
56d81fe5 972(defun stgit-show ()
a53347d9 973 "Show the patch on the current line."
56d81fe5
DK
974 (interactive)
975 (stgit-capture-output "*StGit patch*"
50d88c67
DK
976 (case (get-text-property (point) 'entry-type)
977 ('file
3164eec6
DK
978 (let* ((patched-file (stgit-patched-file-at-point))
979 (patch-name (stgit-patch-name-at-point))
980 (patch-id (stgit-id patch-name))
981 (args (append (and (stgit-file-cr-from patched-file)
7567401c 982 (list (stgit-find-copies-harder-diff-arg)))
b894e680
DK
983 (cond ((eq patch-id :index)
984 '("--cached"))
985 ((eq patch-id :work)
986 nil)
987 (t
988 (list (concat patch-id "^") patch-id)))
3164eec6
DK
989 '("--")
990 (if (stgit-file-copy-or-rename patched-file)
991 (list (stgit-file-cr-from patched-file)
992 (stgit-file-cr-to patched-file))
993 (list (stgit-file-file patched-file))))))
994 (apply 'stgit-run-git "diff" args)))
50d88c67 995 ('patch
4713c50e
GH
996 (let* ((patch-name (stgit-patch-name-at-point))
997 (patch-id (stgit-id patch-name)))
998 (if (or (eq patch-id :index) (eq patch-id :work))
999 (apply 'stgit-run-git "diff"
1000 (stgit-find-copies-harder-diff-arg)
1001 (and (eq patch-id :index)
1002 '("--cached")))
1003 (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M"
1004 (stgit-patch-name-at-point)))))
50d88c67
DK
1005 (t
1006 (error "No patch or file at point")))
1007 (with-current-buffer standard-output
1008 (goto-char (point-min))
1009 (diff-mode))))
0663524d 1010
fd9fe574 1011(defun stgit-move-change-to-index (file)
37cb5766 1012 "Copies the workspace state of FILE to index, using git add or git rm"
306b37a6
GH
1013 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1014 '("add") '("rm" "-q"))))
37cb5766 1015 (stgit-capture-output "*git output*"
5115dea0 1016 (apply 'stgit-run-git (append op '("--") (list file))))))
37cb5766 1017
fd9fe574 1018(defun stgit-remove-change-from-index (file)
37cb5766
DK
1019 "Unstages the change in FILE from the index"
1020 (stgit-capture-output "*git output*"
1021 (stgit-run-git "reset" "-q" "--" file)))
1022
1023(defun stgit-file-toggle-index ()
1024 "Move modified file in or out of the index."
1025 (interactive)
1026 (let ((patched-file (stgit-patched-file-at-point)))
1027 (unless patched-file
1028 (error "No file on the current line"))
51783171
GH
1029 (when (eq (stgit-file-status patched-file) 'unmerged)
1030 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
37cb5766
DK
1031 (let ((patch-name (stgit-patch-name-at-point)))
1032 (cond ((eq patch-name :work)
fd9fe574 1033 (stgit-move-change-to-index (stgit-file-file patched-file)))
37cb5766 1034 ((eq patch-name :index)
fd9fe574 1035 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766
DK
1036 (t
1037 (error "Can only move files in the working tree to index")))))
210a2a52
DK
1038 (stgit-refresh-worktree)
1039 (stgit-refresh-index))
37cb5766 1040
0bca35c8 1041(defun stgit-edit ()
a53347d9 1042 "Edit the patch on the current line."
0bca35c8 1043 (interactive)
64ada6f5 1044 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 1045 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
1046 (dir default-directory))
1047 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 1048 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
1049 (setq default-directory dir)
1050 (let ((standard-output edit-buf))
d51722b7 1051 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
1052
1053(defun stgit-confirm-edit ()
1054 (interactive)
1055 (let ((file (make-temp-file "stgit-edit-")))
1056 (write-region (point-min) (point-max) file)
1057 (stgit-capture-output nil
d51722b7 1058 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 1059 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1060 (stgit-reload))))
0bca35c8 1061
aa04f831
GH
1062(defun stgit-new (add-sign)
1063 "Create a new patch.
1064With a prefix argument, include a \"Signed-off-by:\" line at the
1065end of the patch."
1066 (interactive "P")
c5d45b92
GH
1067 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1068 (dir default-directory))
1069 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831
GH
1070 (setq default-directory dir)
1071 (when add-sign
1072 (save-excursion
1073 (let ((standard-output (current-buffer)))
1074 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
1075
1076(defun stgit-confirm-new ()
1077 (interactive)
27b0f9e4 1078 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
1079 (write-region (point-min) (point-max) file)
1080 (stgit-capture-output nil
27b0f9e4 1081 (stgit-run "new" "-f" file))
64c097a0 1082 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1083 (stgit-reload))))
64c097a0
DK
1084
1085(defun stgit-create-patch-name (description)
1086 "Create a patch name from a long description"
1087 (let ((patch ""))
1088 (while (> (length description) 0)
1089 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
1090 (setq patch (downcase (concat patch
1091 (match-string 0 description))))
64c097a0
DK
1092 (setq description (substring description (match-end 0))))
1093 ((string-match "\\` +" description)
1094 (setq patch (concat patch "-"))
1095 (setq description (substring description (match-end 0))))
1096 ((string-match "\\`[^a-zA-Z_-]+" description)
1097 (setq description (substring description (match-end 0))))))
1098 (cond ((= (length patch) 0)
1099 "patch")
1100 ((> (length patch) 20)
1101 (substring patch 0 20))
1102 (t patch))))
0bca35c8 1103
9008e45b 1104(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 1105 "Delete the patches in PATCHSYMS.
9008e45b
GH
1106Interactively, delete the marked patches, or the patch at point.
1107
1108With a prefix argument, or SPILL-P, spill the patch contents to
1109the work tree and index."
1110 (interactive (list (stgit-patches-marked-or-at-point)
1111 current-prefix-arg))
e7231e4f
GH
1112 (unless patchsyms
1113 (error "No patches to delete"))
64ada6f5
GH
1114 (when (memq :index patchsyms)
1115 (error "Cannot delete the index"))
1116 (when (memq :work patchsyms)
1117 (error "Cannot delete the work tree"))
1118
d51722b7 1119 (let ((npatches (length patchsyms)))
9008e45b 1120 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 1121 npatches
9008e45b
GH
1122 (if (= 1 npatches) "" "es")
1123 (if spill-p
1124 " (spilling contents to index)"
1125 "")))
1126 (let ((args (if spill-p
1127 (cons "--spill" patchsyms)
1128 patchsyms)))
1129 (stgit-capture-output nil
1130 (apply 'stgit-run "delete" args))
1131 (stgit-reload)))))
d51722b7 1132
7cc45294
GH
1133(defun stgit-move-patches-target ()
1134 "Return the patchsym indicating a target patch for
1135`stgit-move-patches'.
1136
1137This is either the patch at point, or one of :top and :bottom, if
1138the point is after or before the applied patches."
1139
2c862b07 1140 (let ((patchsym (stgit-patch-name-at-point)))
7cc45294
GH
1141 (cond (patchsym patchsym)
1142 ((save-excursion (re-search-backward "^>" nil t)) :top)
1143 (t :bottom))))
1144
95369f6c
GH
1145(defun stgit-sort-patches (patchsyms)
1146 "Returns the list of patches in PATCHSYMS sorted according to
1147their position in the patch series, bottommost first.
1148
1149PATCHSYMS may not contain duplicate entries."
1150 (let (sorted-patchsyms
1151 (series (with-output-to-string
1152 (with-current-buffer standard-output
1153 (stgit-run-silent "series" "--noprefix"))))
1154 start)
1155 (while (string-match "^\\(.+\\)" series start)
1156 (let ((patchsym (intern (match-string 1 series))))
1157 (when (memq patchsym patchsyms)
1158 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1159 (setq start (match-end 0)))
1160 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1161
1162 (unless (= (length patchsyms) (length sorted-patchsyms))
1163 (error "Internal error"))
1164
1165 sorted-patchsyms))
1166
7cc45294
GH
1167(defun stgit-move-patches (patchsyms target-patch)
1168 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1169If TARGET-PATCH is :bottom or :top, move the patches to the
1170bottom or top of the stack, respectively.
1171
1172Interactively, move the marked patches to where the point is."
1173 (interactive (list stgit-marked-patches
1174 (stgit-move-patches-target)))
1175 (unless patchsyms
1176 (error "Need at least one patch to move"))
1177
1178 (unless target-patch
1179 (error "Point not at a patch"))
1180
1181 (if (eq target-patch :top)
1182 (stgit-capture-output nil
1183 (apply 'stgit-run "float" patchsyms))
1184
1185 ;; need to have patchsyms sorted by position in the stack
95369f6c 1186 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1187 (while sorted-patchsyms
1188 (setq sorted-patchsyms
1189 (and (stgit-capture-output nil
1190 (if (eq target-patch :bottom)
1191 (stgit-run "sink" "--" (car sorted-patchsyms))
1192 (stgit-run "sink" "--to" target-patch "--"
1193 (car sorted-patchsyms))))
1194 (cdr sorted-patchsyms))))))
1195 (stgit-reload))
1196
594aa463
KH
1197(defun stgit-squash (patchsyms)
1198 "Squash the patches in PATCHSYMS.
693d179b
GH
1199Interactively, squash the marked patches.
1200
1201Unless there are any conflicts, the patches will be merged into
1202one patch, which will occupy the same spot in the series as the
1203deepest patch had before the squash."
d51722b7
GH
1204 (interactive (list stgit-marked-patches))
1205 (when (< (length patchsyms) 2)
594aa463 1206 (error "Need at least two patches to squash"))
32d7545d
GH
1207 (let ((stgit-buffer (current-buffer))
1208 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1209 (dir default-directory)
1210 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1211 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1212 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1213 (setq default-directory dir)
32d7545d
GH
1214 (let ((result (let ((standard-output edit-buf))
1215 (apply 'stgit-run-silent "squash"
1216 "--save-template=-" sorted-patchsyms))))
1217
1218 ;; stg squash may have reordered the patches or caused conflicts
1219 (with-current-buffer stgit-buffer
1220 (stgit-reload))
1221
1222 (unless (eq 0 result)
1223 (fundamental-mode)
1224 (rename-buffer "*StGit error*")
1225 (resize-temp-buffer-window)
1226 (switch-to-buffer-other-window stgit-buffer)
1227 (error "stg squash failed")))))
ea0def18 1228
594aa463 1229(defun stgit-confirm-squash ()
ea0def18
DK
1230 (interactive)
1231 (let ((file (make-temp-file "stgit-edit-")))
1232 (write-region (point-min) (point-max) file)
1233 (stgit-capture-output nil
594aa463 1234 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1235 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1236 (stgit-clear-marks)
1237 ;; Go to first marked patch and stay there
1238 (goto-char (point-min))
1239 (re-search-forward (concat "^[>+-]\\*") nil t)
1240 (move-to-column goal-column)
1241 (let ((pos (point)))
1f0bf00f 1242 (stgit-reload)
e6b1fdae 1243 (goto-char pos)))))
ea0def18 1244
0663524d
KH
1245(defun stgit-help ()
1246 "Display help for the StGit mode."
1247 (interactive)
1248 (describe-function 'stgit-mode))
3a59f3db 1249
83e51dbf
DK
1250(defun stgit-undo (&optional arg)
1251 "Run stg undo.
1252With prefix argument, run it with the --hard flag."
1253 (interactive "P")
1254 (stgit-capture-output nil
1255 (if arg
1256 (stgit-run "undo" "--hard")
1257 (stgit-run "undo")))
1f0bf00f 1258 (stgit-reload))
83e51dbf 1259
4d73c4d8
DK
1260(defun stgit-refresh (&optional arg)
1261 "Run stg refresh.
36a4eacd
GH
1262If the index contains any changes, only refresh from index.
1263
a53347d9 1264With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8
DK
1265 (interactive "P")
1266 (let ((patchargs (if arg
b0424080
GH
1267 (let ((patches (stgit-patches-marked-or-at-point)))
1268 (cond ((null patches)
df283a8b 1269 (error "No patch to update"))
b0424080 1270 ((> (length patches) 1)
df283a8b 1271 (error "Too many patches selected"))
b0424080
GH
1272 (t
1273 (cons "-p" patches))))
1274 nil)))
36a4eacd
GH
1275 (unless (stgit-index-empty-p)
1276 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 1277 (stgit-capture-output nil
074a4fb0
GH
1278 (apply 'stgit-run "refresh" patchargs))
1279 (stgit-refresh-git-status))
4d73c4d8
DK
1280 (stgit-reload))
1281
8f702de4
GH
1282(defcustom stgit-show-worktree-mode 'center
1283 "This variable controls where the \"Index\" and \"Work tree\"
1284will be shown on in the buffer.
1285
1286It can be set to 'top (above all patches), 'center (show between
1287applied and unapplied patches), and 'bottom (below all patches).
1288
1289See also `stgit-show-worktree'."
1290 :type '(radio (const :tag "above all patches (top)" top)
1291 (const :tag "between applied and unapplied patches (center)"
1292 center)
1293 (const :tag "below all patches (bottom)" bottom))
1294 :group 'stgit)
1295
ce3b6130
DK
1296(defcustom stgit-default-show-worktree
1297 nil
1298 "Set to non-nil to by default show the working tree in a new stgit buffer.
1299
1300This value is used as the default value for `stgit-show-worktree'."
1301 :type 'boolean
1302 :group 'stgit)
1303
1304(defvar stgit-show-worktree nil
8f702de4 1305 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 1306
8f702de4 1307See also `stgit-show-worktree-mode'.")
ce3b6130
DK
1308
1309(defun stgit-toggle-worktree (&optional arg)
1310 "Toggle the visibility of the work tree.
1311With arg, show the work tree if arg is positive.
1312
8f702de4
GH
1313Its initial setting is controlled by `stgit-default-show-worktree'.
1314
1315`stgit-show-worktree-mode' controls where on screen the index and
1316work tree will show up."
ce3b6130
DK
1317 (interactive)
1318 (setq stgit-show-worktree
1319 (if (numberp arg)
1320 (> arg 0)
1321 (not stgit-show-worktree)))
1322 (stgit-reload))
1323
3a59f3db 1324(provide 'stgit)