stgit.el: Add stgit-toggle-mark
[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
4f7efe0c
GH
12(when (< emacs-major-version 22)
13 (error "Emacs older than 22 is not supported by stgit.el"))
14
0f076fe6 15(require 'git nil t)
50d88c67 16(require 'cl)
98230edd 17(require 'ewoc)
0f076fe6 18
56d81fe5 19(defun stgit (dir)
fdf5e327
GH
20 "Manage StGit patches for the tree in DIR.
21
22See `stgit-mode' for commands available."
56d81fe5 23 (interactive "DDirectory: \n")
52144ce5 24 (switch-to-stgit-buffer (git-get-top-dir dir))
1f0bf00f 25 (stgit-reload))
56d81fe5 26
9d04c657
GH
27(defun stgit-assert-mode ()
28 "Signal an error if not in an StGit buffer."
29 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
30
074a4fb0
GH
31(unless (fboundp 'git-get-top-dir)
32 (defun git-get-top-dir (dir)
33 "Retrieve the top-level directory of a git tree."
34 (let ((cdup (with-output-to-string
35 (with-current-buffer standard-output
36 (cd dir)
37 (unless (eq 0 (call-process "git" nil t nil
38 "rev-parse" "--show-cdup"))
df283a8b 39 (error "Cannot find top-level git tree for %s" dir))))))
074a4fb0
GH
40 (expand-file-name (concat (file-name-as-directory dir)
41 (car (split-string cdup "\n")))))))
42
43(defun stgit-refresh-git-status (&optional dir)
44 "If it exists, refresh the `git-status' buffer belonging to
45directory DIR or `default-directory'"
46 (when (and (fboundp 'git-find-status-buffer)
47 (fboundp 'git-refresh-status))
48 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
49 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
50 (when git-status-buffer
51 (with-current-buffer git-status-buffer
52 (git-refresh-status))))))
52144ce5 53
b894e680
DK
54(defun stgit-find-buffer (dir)
55 "Return the buffer displaying StGit patches for DIR, or nil if none."
56d81fe5
DK
56 (setq dir (file-name-as-directory dir))
57 (let ((buffers (buffer-list)))
58 (while (and buffers
59 (not (with-current-buffer (car buffers)
60 (and (eq major-mode 'stgit-mode)
61 (string= default-directory dir)))))
62 (setq buffers (cdr buffers)))
b894e680
DK
63 (and buffers (car buffers))))
64
65(defun switch-to-stgit-buffer (dir)
66 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
67 (setq dir (file-name-as-directory dir))
68 (let ((buffer (stgit-find-buffer dir)))
69 (switch-to-buffer (or buffer
70 (create-stgit-buffer dir)))))
71
2c862b07 72(defstruct (stgit-patch)
3164eec6 73 status name desc empty files-ewoc)
56d81fe5 74
98230edd 75(defun stgit-patch-pp (patch)
9153ce3a
GH
76 (let* ((status (stgit-patch-status patch))
77 (start (point))
78 (name (stgit-patch-name patch))
79 (face (cdr (assq status stgit-patch-status-face-alist))))
80 (insert (case status
81 ('applied "+")
82 ('top ">")
83 ('unapplied "-")
84 (t " "))
85 (if (memq name stgit-marked-patches)
86 "*" " "))
87 (if (memq status '(index work))
88 (insert (propertize (if (eq status 'index) "Index" "Work tree")
89 'face face))
90 (insert (format "%-30s"
224ef1ec
GH
91 (propertize (symbol-name name)
92 'face face
93 'syntax-table (string-to-syntax "w")))
9153ce3a
GH
94 " "
95 (if (stgit-patch-empty patch) "(empty) " "")
96 (propertize (or (stgit-patch-desc patch) "")
97 'face 'stgit-description-face)))
4f7ff561 98 (insert "\n")
f9b82d36 99 (put-text-property start (point) 'entry-type 'patch)
98230edd 100 (when (memq name stgit-expanded-patches)
0de6881a 101 (stgit-insert-patch-files patch))
98230edd
DK
102 (put-text-property start (point) 'patch-data patch)))
103
56d81fe5
DK
104(defun create-stgit-buffer (dir)
105 "Create a buffer for showing StGit patches.
106Argument DIR is the repository path."
107 (let ((buf (create-file-buffer (concat dir "*stgit*")))
108 (inhibit-read-only t))
109 (with-current-buffer buf
110 (setq default-directory dir)
111 (stgit-mode)
98230edd 112 (set (make-local-variable 'stgit-ewoc)
4f7ff561 113 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
56d81fe5
DK
114 (setq buffer-read-only t))
115 buf))
116
117(defmacro stgit-capture-output (name &rest body)
e558a4ab
GH
118 "Capture StGit output and, if there was any output, show it in a window
119at the end.
120Returns nil if there was no output."
94baef5a
DK
121 (declare (debug ([&or stringp null] body))
122 (indent 1))
34afb86c
DK
123 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
124 (stgit-dir default-directory)
125 (inhibit-read-only t))
56d81fe5 126 (with-current-buffer output-buf
34afb86c
DK
127 (erase-buffer)
128 (setq default-directory stgit-dir)
129 (setq buffer-read-only t))
56d81fe5
DK
130 (let ((standard-output output-buf))
131 ,@body)
34afb86c
DK
132 (with-current-buffer output-buf
133 (set-buffer-modified-p nil)
134 (setq buffer-read-only t)
135 (if (< (point-min) (point-max))
136 (display-buffer output-buf t)))))
56d81fe5 137
d51722b7
GH
138(defun stgit-make-run-args (args)
139 "Return a copy of ARGS with its elements converted to strings."
140 (mapcar (lambda (x)
141 ;; don't use (format "%s" ...) to limit type errors
142 (cond ((stringp x) x)
143 ((integerp x) (number-to-string x))
144 ((symbolp x) (symbol-name x))
145 (t
146 (error "Bad element in stgit-make-run-args args: %S" x))))
147 args))
148
9aecd505 149(defun stgit-run-silent (&rest args)
d51722b7 150 (setq args (stgit-make-run-args args))
56d81fe5
DK
151 (apply 'call-process "stg" nil standard-output nil args))
152
9aecd505 153(defun stgit-run (&rest args)
d51722b7 154 (setq args (stgit-make-run-args args))
9aecd505
DK
155 (let ((msgcmd (mapconcat #'identity args " ")))
156 (message "Running stg %s..." msgcmd)
157 (apply 'call-process "stg" nil standard-output nil args)
158 (message "Running stg %s...done" msgcmd)))
159
378a003d 160(defun stgit-run-git (&rest args)
d51722b7 161 (setq args (stgit-make-run-args args))
378a003d
GH
162 (let ((msgcmd (mapconcat #'identity args " ")))
163 (message "Running git %s..." msgcmd)
164 (apply 'call-process "git" nil standard-output nil args)
165 (message "Running git %s...done" msgcmd)))
166
1f60181a 167(defun stgit-run-git-silent (&rest args)
d51722b7 168 (setq args (stgit-make-run-args args))
1f60181a
GH
169 (apply 'call-process "git" nil standard-output nil args))
170
b894e680
DK
171(defun stgit-index-empty-p ()
172 "Returns non-nil if the index contains no changes from HEAD."
173 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
174
1629f59f
GH
175(defun stgit-work-tree-empty-p ()
176 "Returns non-nil if the work tree contains no changes from index."
177 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
178
2ecb05c8
GH
179(defvar stgit-index-node)
180(defvar stgit-worktree-node)
210a2a52
DK
181
182(defun stgit-refresh-index ()
183 (when stgit-index-node
184 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
185
186(defun stgit-refresh-worktree ()
187 (when stgit-worktree-node
188 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
189
8f702de4
GH
190(defun stgit-run-series-insert-index (ewoc)
191 (setq index-node (cons ewoc (ewoc-enter-last ewoc
192 (make-stgit-patch
193 :status 'index
194 :name :index
195 :desc nil
196 :empty nil)))
197 worktree-node (cons ewoc (ewoc-enter-last ewoc
198 (make-stgit-patch
199 :status 'work
200 :name :work
201 :desc nil
202 :empty nil)))))
203
98230edd 204(defun stgit-run-series (ewoc)
8f702de4
GH
205 (setq stgit-index-node nil
206 stgit-worktree-node nil)
207 (let ((inserted-index (not stgit-show-worktree))
208 index-node
03fc3b26
GH
209 worktree-node
210 all-patchsyms)
98230edd 211 (with-temp-buffer
ea305902
GH
212 (let* ((standard-output (current-buffer))
213 (exit-status (stgit-run-silent "series"
214 "--description" "--empty")))
98230edd
DK
215 (goto-char (point-min))
216 (if (not (zerop exit-status))
217 (cond ((looking-at "stg series: \\(.*\\)")
8f702de4 218 (setq inserted-index t)
98230edd 219 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
8f702de4
GH
220 (substitute-command-keys
221 "-- not initialized; run \\[stgit-init]")))
98230edd
DK
222 ((looking-at ".*")
223 (error "Error running stg: %s"
224 (match-string 0))))
225 (while (not (eobp))
226 (unless (looking-at
227 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
228 (error "Syntax error in output from stg series"))
229 (let* ((state-str (match-string 2))
230 (state (cond ((string= state-str ">") 'top)
231 ((string= state-str "+") 'applied)
8f702de4
GH
232 ((string= state-str "-") 'unapplied)))
233 (name (intern (match-string 4)))
234 (desc (match-string 5))
235 (empty (string= (match-string 1) "0")))
236 (unless inserted-index
237 (when (or (eq stgit-show-worktree-mode 'top)
238 (and (eq stgit-show-worktree-mode 'center)
239 (eq state 'unapplied)))
240 (setq inserted-index t)
241 (stgit-run-series-insert-index ewoc)))
03fc3b26 242 (setq all-patchsyms (cons name all-patchsyms))
98230edd
DK
243 (ewoc-enter-last ewoc
244 (make-stgit-patch
245 :status state
8f702de4
GH
246 :name name
247 :desc desc
248 :empty empty)))
249 (forward-line 1))))
250 (unless inserted-index
251 (stgit-run-series-insert-index ewoc)))
252 (setq stgit-index-node index-node
03fc3b26
GH
253 stgit-worktree-node worktree-node
254 stgit-marked-patches (intersection stgit-marked-patches
255 all-patchsyms))))
98230edd 256
1f0bf00f 257(defun stgit-reload ()
a53347d9 258 "Update the contents of the StGit buffer."
56d81fe5 259 (interactive)
9d04c657 260 (stgit-assert-mode)
56d81fe5
DK
261 (let ((inhibit-read-only t)
262 (curline (line-number-at-pos))
a9089e68
GH
263 (curpatch (stgit-patch-name-at-point))
264 (curfile (stgit-patched-file-at-point)))
98230edd
DK
265 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
266 (ewoc-set-hf stgit-ewoc
267 (concat "Branch: "
268 (propertize
ea305902
GH
269 (substring (with-output-to-string
270 (stgit-run-silent "branch"))
271 0 -1)
4f292066 272 'face 'stgit-branch-name-face)
4f7ff561 273 "\n\n")
ce3b6130
DK
274 (if stgit-show-worktree
275 "--"
276 (propertize
277 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
278 shows the working tree\n")
6a73154a 279 'face 'stgit-description-face)))
98230edd 280 (stgit-run-series stgit-ewoc)
56d81fe5 281 (if curpatch
a9089e68 282 (stgit-goto-patch curpatch (and curfile (stgit-file-file curfile)))
074a4fb0
GH
283 (goto-line curline)))
284 (stgit-refresh-git-status))
56d81fe5 285
f1bcbe9c
GH
286(defun stgit-set-default (symbol value)
287 "Set default value of SYMBOL to VALUE using `set-default' and
288reload all StGit buffers."
289 (set-default symbol value)
290 (dolist (buf (buffer-list))
291 (with-current-buffer buf
292 (when (eq major-mode 'stgit-mode)
293 (stgit-reload)))))
294
8f40753a 295(defgroup stgit nil
f1bcbe9c
GH
296 "A user interface for the StGit patch maintenance tool."
297 :group 'tools
298 :link '(function-link stgit)
299 :link '(url-link "http://www.procode.org/stgit/"))
fdf5e327 300
f1bcbe9c
GH
301(defcustom stgit-abbreviate-copies-and-renames t
302 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
303instead of \"dir/old/file -> dir/new/file\"."
304 :type 'boolean
305 :group 'stgit
306 :set 'stgit-set-default)
8f40753a 307
f1bcbe9c
GH
308(defcustom stgit-default-show-worktree t
309 "Set to non-nil to by default show the working tree in a new stgit buffer.
310
311Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
312 :type 'boolean
313 :group 'stgit
314 :link '(variable-link stgit-show-worktree))
315
316(defcustom stgit-find-copies-harder nil
317 "Try harder to find copied files when listing patches.
318
319When not nil, runs git diff-tree with the --find-copies-harder
320flag, which reduces performance."
321 :type 'boolean
322 :group 'stgit
323 :set 'stgit-set-default)
324
325(defcustom stgit-show-worktree-mode 'center
326 "This variable controls where the \"Index\" and \"Work tree\"
327will be shown on in the buffer.
328
329It can be set to 'top (above all patches), 'center (show between
330applied and unapplied patches), and 'bottom (below all patches)."
331 :type '(radio (const :tag "above all patches (top)" top)
332 (const :tag "between applied and unapplied patches (center)"
333 center)
334 (const :tag "below all patches (bottom)" bottom))
335 :group 'stgit
336 :link '(variable-link stgit-show-worktree)
337 :set 'stgit-set-default)
4f292066
GH
338
339(defface stgit-branch-name-face
340 '((t :inherit bold))
341 "The face used for the StGit branch name"
342 :group 'stgit)
07f464e0
DK
343
344(defface stgit-top-patch-face
345 '((((background dark)) (:weight bold :foreground "yellow"))
346 (((background light)) (:weight bold :foreground "purple"))
347 (t (:weight bold)))
8f40753a
GH
348 "The face used for the top patch names"
349 :group 'stgit)
07f464e0
DK
350
351(defface stgit-applied-patch-face
352 '((((background dark)) (:foreground "light yellow"))
353 (((background light)) (:foreground "purple"))
354 (t ()))
8f40753a
GH
355 "The face used for applied patch names"
356 :group 'stgit)
07f464e0
DK
357
358(defface stgit-unapplied-patch-face
359 '((((background dark)) (:foreground "gray80"))
360 (((background light)) (:foreground "orchid"))
361 (t ()))
8f40753a
GH
362 "The face used for unapplied patch names"
363 :group 'stgit)
07f464e0 364
f1bcbe9c
GH
365(defface stgit-description-face
366 '((((background dark)) (:foreground "tan"))
367 (((background light)) (:foreground "dark red")))
368 "The face used for StGit descriptions"
369 :group 'stgit)
370
371(defface stgit-index-work-tree-title-face
372 '((((supports :slant italic)) :slant italic)
373 (t :inherit bold))
374 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
1f60181a
GH
375 :group 'stgit)
376
377(defface stgit-unmerged-file-face
378 '((((class color) (background light)) (:foreground "red" :bold t))
379 (((class color) (background dark)) (:foreground "red" :bold t)))
380 "StGit mode face used for unmerged file status"
381 :group 'stgit)
382
383(defface stgit-unknown-file-face
384 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
385 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
386 "StGit mode face used for unknown file status"
387 :group 'stgit)
388
d9473917
GH
389(defface stgit-ignored-file-face
390 '((((class color) (background light)) (:foreground "grey60"))
391 (((class color) (background dark)) (:foreground "grey40")))
392 "StGit mode face used for ignored files")
393
a6d9a852
GH
394(defface stgit-file-permission-face
395 '((((class color) (background light)) (:foreground "green" :bold t))
396 (((class color) (background dark)) (:foreground "green" :bold t)))
397 "StGit mode face used for permission changes."
398 :group 'stgit)
399
f1bcbe9c
GH
400(defface stgit-modified-file-face
401 '((((class color) (background light)) (:foreground "purple"))
402 (((class color) (background dark)) (:foreground "salmon")))
403 "StGit mode face used for modified file status"
1f60181a
GH
404 :group 'stgit)
405
406(defconst stgit-file-status-code-strings
407 (mapcar (lambda (arg)
408 (cons (car arg)
a6d9a852
GH
409 (propertize (cadr arg) 'face (car (cddr arg)))))
410 '((add "Added" stgit-modified-file-face)
411 (copy "Copied" stgit-modified-file-face)
412 (delete "Deleted" stgit-modified-file-face)
413 (modify "Modified" stgit-modified-file-face)
414 (rename "Renamed" stgit-modified-file-face)
415 (mode-change "Mode change" stgit-modified-file-face)
416 (unmerged "Unmerged" stgit-unmerged-file-face)
d9473917
GH
417 (unknown "Unknown" stgit-unknown-file-face)
418 (ignore "Ignored" stgit-ignored-file-face)))
1f60181a
GH
419 "Alist of code symbols to description strings")
420
000f337c
GH
421(defconst stgit-patch-status-face-alist
422 '((applied . stgit-applied-patch-face)
423 (top . stgit-top-patch-face)
424 (unapplied . stgit-unapplied-patch-face)
9153ce3a
GH
425 (index . stgit-index-work-tree-title-face)
426 (work . stgit-index-work-tree-title-face))
000f337c
GH
427 "Alist of face to use for a given patch status")
428
3164eec6
DK
429(defun stgit-file-status-code-as-string (file)
430 "Return stgit status code for FILE as a string"
431 (let* ((code (assq (stgit-file-status file)
432 stgit-file-status-code-strings))
433 (score (stgit-file-cr-score file)))
434 (when code
a6d9a852 435 (format "%-11s "
3164eec6
DK
436 (if (and score (/= score 100))
437 (format "%s %s" (cdr code)
438 (propertize (format "%d%%" score)
a6d9a852 439 'face 'stgit-description-face))
3164eec6 440 (cdr code))))))
1f60181a 441
a6d9a852 442(defun stgit-file-status-code (str &optional score)
1f60181a
GH
443 "Return stgit status code from git status string"
444 (let ((code (assoc str '(("A" . add)
445 ("C" . copy)
446 ("D" . delete)
d9473917 447 ("I" . ignore)
1f60181a
GH
448 ("M" . modify)
449 ("R" . rename)
450 ("T" . mode-change)
451 ("U" . unmerged)
452 ("X" . unknown)))))
a6d9a852
GH
453 (setq code (if code (cdr code) 'unknown))
454 (when (stringp score)
455 (if (> (length score) 0)
456 (setq score (string-to-number score))
457 (setq score nil)))
458 (if score (cons code score) code)))
459
460(defconst stgit-file-type-strings
461 '((#o100 . "file")
462 (#o120 . "symlink")
463 (#o160 . "subproject"))
464 "Alist of names of file types")
465
466(defun stgit-file-type-string (type)
47271f41
GH
467 "Return string describing file type TYPE (the high bits of file permission).
468Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
a6d9a852
GH
469 (let ((type-str (assoc type stgit-file-type-strings)))
470 (or (and type-str (cdr type-str))
471 (format "unknown type %o" type))))
472
473(defun stgit-file-type-change-string (old-perm new-perm)
47271f41
GH
474 "Return string describing file type change from OLD-PERM to NEW-PERM.
475Cf. `stgit-file-type-string'."
a6d9a852
GH
476 (let ((old-type (lsh old-perm -9))
477 (new-type (lsh new-perm -9)))
478 (cond ((= old-type new-type) "")
479 ((zerop new-type) "")
480 ((zerop old-type)
481 (if (= new-type #o100)
482 ""
483 (format " (%s)" (stgit-file-type-string new-type))))
484 (t (format " (%s -> %s)"
485 (stgit-file-type-string old-type)
486 (stgit-file-type-string new-type))))))
487
488(defun stgit-file-mode-change-string (old-perm new-perm)
47271f41
GH
489 "Return string describing file mode change from OLD-PERM to NEW-PERM.
490Cf. `stgit-file-type-change-string'."
a6d9a852
GH
491 (setq old-perm (logand old-perm #o777)
492 new-perm (logand new-perm #o777))
493 (if (or (= old-perm new-perm)
494 (zerop old-perm)
495 (zerop new-perm))
496 ""
497 (let* ((modified (logxor old-perm new-perm))
498 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
499 (cond ((zerop modified) "")
500 ((and (zerop not-x-modified)
501 (or (and (eq #o111 (logand old-perm #o111))
502 (propertize "-x" 'face 'stgit-file-permission-face))
503 (and (eq #o111 (logand new-perm #o111))
504 (propertize "+x" 'face
505 'stgit-file-permission-face)))))
506 (t (concat (propertize (format "%o" old-perm)
507 'face 'stgit-file-permission-face)
508 (propertize " -> "
509 'face 'stgit-description-face)
510 (propertize (format "%o" new-perm)
511 'face 'stgit-file-permission-face)))))))
1f60181a 512
0de6881a
DK
513(defstruct (stgit-file)
514 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
515
ca027a87 516(defun stgit-describe-copy-or-rename (file)
6a73154a
GH
517 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
518 from to common-head common-tail)
ca027a87
GH
519
520 (when stgit-abbreviate-copies-and-renames
521 (setq from (split-string (stgit-file-cr-from file) "/")
522 to (split-string (stgit-file-cr-to file) "/"))
523
524 (while (and from to (cdr from) (cdr to)
525 (string-equal (car from) (car to)))
526 (setq common-head (cons (car from) common-head)
527 from (cdr from)
528 to (cdr to)))
529 (setq common-head (nreverse common-head)
530 from (nreverse from)
531 to (nreverse to))
532 (while (and from to (cdr from) (cdr to)
533 (string-equal (car from) (car to)))
534 (setq common-tail (cons (car from) common-tail)
535 from (cdr from)
536 to (cdr to)))
537 (setq from (nreverse from)
538 to (nreverse to)))
539
540 (if (or common-head common-tail)
541 (concat (if common-head
542 (mapconcat #'identity common-head "/")
543 "")
544 (if common-head "/" "")
545 (propertize "{" 'face 'stgit-description-face)
546 (mapconcat #'identity from "/")
547 arrow
548 (mapconcat #'identity to "/")
549 (propertize "}" 'face 'stgit-description-face)
550 (if common-tail "/" "")
551 (if common-tail
552 (mapconcat #'identity common-tail "/")
553 ""))
554 (concat (stgit-file-cr-from file) arrow (stgit-file-cr-to file)))))
555
3164eec6 556(defun stgit-file-pp (file)
0de6881a
DK
557 (let ((status (stgit-file-status file))
558 (name (if (stgit-file-copy-or-rename file)
ca027a87 559 (stgit-describe-copy-or-rename file)
0de6881a
DK
560 (stgit-file-file file)))
561 (mode-change (stgit-file-mode-change-string
562 (stgit-file-old-perm file)
563 (stgit-file-new-perm file)))
564 (start (point)))
c30518fd 565 (insert (format " %-12s%s%s%s%s\n"
3164eec6 566 (stgit-file-status-code-as-string file)
98230edd 567 mode-change
c30518fd 568 (if (zerop (length mode-change)) "" " ")
0de6881a
DK
569 name
570 (propertize (stgit-file-type-change-string
571 (stgit-file-old-perm file)
572 (stgit-file-new-perm file))
98230edd 573 'face 'stgit-description-face)))
0de6881a 574 (add-text-properties start (point)
3164eec6
DK
575 (list 'entry-type 'file
576 'file-data file))))
0de6881a 577
7567401c
GH
578(defun stgit-find-copies-harder-diff-arg ()
579 "Return the flag to use with `git-diff' depending on the
b6df231c
GH
580`stgit-find-copies-harder' flag."
581 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
7567401c 582
d9473917
GH
583(defun stgit-insert-ls-files (args file-flag)
584 (let ((start (point)))
585 (apply 'stgit-run-git
586 (append '("ls-files" "--exclude-standard" "-z") args))
587 (goto-char start)
588 (while (looking-at "\\([^\0]*\\)\0")
589 (let ((name-len (- (match-end 0) (match-beginning 0))))
590 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
591 (forward-char name-len)))))
592
0de6881a 593(defun stgit-insert-patch-files (patch)
88134ff7
GH
594 "Expand (show modification of) the patch PATCH after the line
595at point."
3164eec6 596 (let* ((patchsym (stgit-patch-name patch))
0434bec1
GH
597 (end (point-marker))
598 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
599 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
600 (set-marker-insertion-type end t)
3164eec6 601 (setf (stgit-patch-files-ewoc patch) ewoc)
0de6881a 602 (with-temp-buffer
ea305902
GH
603 (let ((standard-output (current-buffer)))
604 (apply 'stgit-run-git
605 (cond ((eq patchsym :work)
606 `("diff-files" "-0" ,@args))
607 ((eq patchsym :index)
608 `("diff-index" ,@args "--cached" "HEAD"))
609 (t
610 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
611
612 (when (and (eq patchsym :work))
613 (when stgit-show-ignored
614 (stgit-insert-ls-files '("--ignored" "--others") "I"))
615 (when stgit-show-unknown
616 (stgit-insert-ls-files '("--others") "X"))
617 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
618 (point-min) (point-max)))
619
620 (goto-char (point-min))
621 (unless (or (eobp) (memq patchsym '(:work :index)))
622 (forward-char 41))
623 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
624 (let ((old-perm (string-to-number (match-string 1) 8))
625 (new-perm (string-to-number (match-string 2) 8)))
626 (goto-char (match-end 0))
627 (let ((file
628 (cond ((looking-at
629 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
630 (let* ((patch-status (stgit-patch-status patch))
631 (file-subexp (if (eq patch-status 'unapplied)
632 3
633 4))
634 (file (match-string file-subexp)))
635 (make-stgit-file
636 :old-perm old-perm
637 :new-perm new-perm
638 :copy-or-rename t
639 :cr-score (string-to-number (match-string 2))
640 :cr-from (match-string 3)
641 :cr-to (match-string 4)
642 :status (stgit-file-status-code
643 (match-string 1))
644 :file file)))
645 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
027e1370
GH
646 (make-stgit-file
647 :old-perm old-perm
648 :new-perm new-perm
ea305902
GH
649 :copy-or-rename nil
650 :cr-score nil
651 :cr-from nil
652 :cr-to nil
027e1370
GH
653 :status (stgit-file-status-code
654 (match-string 1))
ea305902
GH
655 :file (match-string 2))))))
656 (goto-char (match-end 0))
657 (ewoc-enter-last ewoc file))))
658
659 (unless (ewoc-nth ewoc 0)
660 (ewoc-set-hf ewoc ""
661 (concat " "
662 (propertize "<no files>"
663 'face 'stgit-description-face)
664 "\n")))))
0434bec1 665 (goto-char end)))
07f464e0 666
030f0535
GH
667(defun stgit-find-file (&optional other-window)
668 (let* ((file (or (stgit-patched-file-at-point)
669 (error "No file at point")))
670 (filename (expand-file-name (stgit-file-file file))))
0de6881a
DK
671 (unless (file-exists-p filename)
672 (error "File does not exist"))
030f0535
GH
673 (funcall (if other-window 'find-file-other-window 'find-file)
674 filename)
675 (when (eq (stgit-file-status file) 'unmerged)
676 (smerge-mode 1))))
acc5652f 677
afbf766b
GH
678(defun stgit-expand (&optional patches collapse)
679 "Show the contents selected patches, or the patch at point.
680
681See also `stgit-collapse'.
682
683Non-interactively, operate on PATCHES, and collapse instead of
684expand if COLLAPSE is not nil."
685 (interactive (list (stgit-patches-marked-or-at-point)))
9d04c657 686 (stgit-assert-mode)
afbf766b
GH
687 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
688 patches stgit-expanded-patches)))
689 (setq stgit-expanded-patches
690 (if collapse
691 (set-difference stgit-expanded-patches patches-diff)
692 (append stgit-expanded-patches patches-diff)))
693 (ewoc-map #'(lambda (patch)
694 (memq (stgit-patch-name patch) patches-diff))
695 stgit-ewoc))
696 (move-to-column (stgit-goal-column)))
697
698(defun stgit-collapse (&optional patches)
699 "Hide the contents selected patches, or the patch at point.
700
701See also `stgit-expand'."
702 (interactive (list (stgit-patches-marked-or-at-point)))
9d04c657 703 (stgit-assert-mode)
afbf766b
GH
704 (stgit-expand patches t))
705
50d88c67 706(defun stgit-select-patch ()
98230edd 707 (let ((patchname (stgit-patch-name-at-point)))
afbf766b
GH
708 (stgit-expand (list patchname)
709 (memq patchname stgit-expanded-patches))))
acc5652f 710
378a003d 711(defun stgit-select ()
da01a29b
GH
712 "With point on a patch, toggle showing files in the patch.
713
714With point on a file, open the associated file. Opens the target
715file for (applied) copies and renames."
378a003d 716 (interactive)
9d04c657 717 (stgit-assert-mode)
50d88c67
DK
718 (case (get-text-property (point) 'entry-type)
719 ('patch
720 (stgit-select-patch))
721 ('file
030f0535 722 (stgit-find-file))
50d88c67
DK
723 (t
724 (error "No patch or file on line"))))
378a003d
GH
725
726(defun stgit-find-file-other-window ()
727 "Open file at point in other window"
728 (interactive)
9d04c657 729 (stgit-assert-mode)
030f0535 730 (stgit-find-file t))
378a003d 731
d9b954c7
GH
732(defun stgit-find-file-merge ()
733 "Open file at point and merge it using `smerge-ediff'."
734 (interactive)
9d04c657 735 (stgit-assert-mode)
d9b954c7
GH
736 (stgit-find-file t)
737 (smerge-ediff))
738
83327d53 739(defun stgit-quit ()
a53347d9 740 "Hide the stgit buffer."
83327d53 741 (interactive)
9d04c657 742 (stgit-assert-mode)
83327d53
GH
743 (bury-buffer))
744
0f076fe6 745(defun stgit-git-status ()
a53347d9 746 "Show status using `git-status'."
0f076fe6 747 (interactive)
9d04c657 748 (stgit-assert-mode)
0f076fe6 749 (unless (fboundp 'git-status)
df283a8b 750 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
751 (let ((dir default-directory))
752 (save-selected-window
753 (pop-to-buffer nil)
754 (git-status dir))))
755
58f72f16
GH
756(defun stgit-goal-column ()
757 "Return goal column for the current line"
50d88c67
DK
758 (case (get-text-property (point) 'entry-type)
759 ('patch 2)
760 ('file 4)
761 (t 0)))
58f72f16
GH
762
763(defun stgit-next-line (&optional arg)
378a003d 764 "Move cursor vertically down ARG lines"
58f72f16 765 (interactive "p")
9d04c657 766 (stgit-assert-mode)
58f72f16
GH
767 (next-line arg)
768 (move-to-column (stgit-goal-column)))
378a003d 769
58f72f16 770(defun stgit-previous-line (&optional arg)
378a003d 771 "Move cursor vertically up ARG lines"
58f72f16 772 (interactive "p")
9d04c657 773 (stgit-assert-mode)
58f72f16
GH
774 (previous-line arg)
775 (move-to-column (stgit-goal-column)))
378a003d
GH
776
777(defun stgit-next-patch (&optional arg)
98230edd 778 "Move cursor down ARG patches."
378a003d 779 (interactive "p")
9d04c657 780 (stgit-assert-mode)
98230edd
DK
781 (ewoc-goto-next stgit-ewoc (or arg 1))
782 (move-to-column goal-column))
378a003d
GH
783
784(defun stgit-previous-patch (&optional arg)
98230edd 785 "Move cursor up ARG patches."
378a003d 786 (interactive "p")
9d04c657 787 (stgit-assert-mode)
98230edd
DK
788 (ewoc-goto-prev stgit-ewoc (or arg 1))
789 (move-to-column goal-column))
378a003d 790
56d81fe5
DK
791(defvar stgit-mode-hook nil
792 "Run after `stgit-mode' is setup.")
793
794(defvar stgit-mode-map nil
795 "Keymap for StGit major mode.")
796
797(unless stgit-mode-map
d9b954c7
GH
798 (let ((diff-map (make-keymap))
799 (toggle-map (make-keymap)))
800 (suppress-keymap diff-map)
801 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
802 '(("b" . stgit-diff-base)
803 ("c" . stgit-diff-combined)
804 ("m" . stgit-find-file-merge)
805 ("o" . stgit-diff-ours)
806 ("t" . stgit-diff-theirs)))
ce3b6130
DK
807 (suppress-keymap toggle-map)
808 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
d9473917
GH
809 '(("t" . stgit-toggle-worktree)
810 ("i" . stgit-toggle-ignored)
811 ("u" . stgit-toggle-unknown)))
ce3b6130
DK
812 (setq stgit-mode-map (make-keymap))
813 (suppress-keymap stgit-mode-map)
814 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
d11e0621
GH
815 `((" " . stgit-mark-down)
816 ("m" . stgit-mark-down)
ce3b6130
DK
817 ("\d" . stgit-unmark-up)
818 ("u" . stgit-unmark-down)
819 ("?" . stgit-help)
820 ("h" . stgit-help)
821 ("\C-p" . stgit-previous-line)
822 ("\C-n" . stgit-next-line)
823 ([up] . stgit-previous-line)
824 ([down] . stgit-next-line)
825 ("p" . stgit-previous-patch)
826 ("n" . stgit-next-patch)
827 ("\M-{" . stgit-previous-patch)
828 ("\M-}" . stgit-next-patch)
829 ("s" . stgit-git-status)
408fa7cb 830 ("g" . stgit-reload-or-repair)
ce3b6130
DK
831 ("r" . stgit-refresh)
832 ("\C-c\C-r" . stgit-rename)
833 ("e" . stgit-edit)
834 ("M" . stgit-move-patches)
835 ("S" . stgit-squash)
836 ("N" . stgit-new)
2acb7116 837 ("c" . stgit-new-and-refresh)
e9fdd4ea
GH
838 ("\C-c\C-c" . stgit-commit)
839 ("\C-c\C-u" . stgit-uncommit)
1629f59f 840 ("U" . stgit-revert)
51783171 841 ("R" . stgit-resolve-file)
ce3b6130 842 ("\r" . stgit-select)
afbf766b
GH
843 ("+" . stgit-expand)
844 ("-" . stgit-collapse)
ce3b6130 845 ("o" . stgit-find-file-other-window)
dde3ab4d 846 ("i" . stgit-toggle-index)
ce3b6130
DK
847 (">" . stgit-push-next)
848 ("<" . stgit-pop-next)
849 ("P" . stgit-push-or-pop)
850 ("G" . stgit-goto)
d9b954c7 851 ("=" . stgit-diff)
ce3b6130 852 ("D" . stgit-delete)
b8463f1d 853 ([?\C-/] . stgit-undo)
ce3b6130 854 ("\C-_" . stgit-undo)
b8463f1d
GH
855 ([?\C-c ?\C-/] . stgit-redo)
856 ("\C-c\C-_" . stgit-redo)
ce3b6130 857 ("B" . stgit-branch)
380a021f 858 ("\C-c\C-b" . stgit-rebase)
ce3b6130 859 ("t" . ,toggle-map)
d9b954c7 860 ("d" . ,diff-map)
ce3b6130 861 ("q" . stgit-quit)))))
56d81fe5
DK
862
863(defun stgit-mode ()
864 "Major mode for interacting with StGit.
fdf5e327
GH
865
866Start StGit using \\[stgit].
867
868Basic commands:
869\\<stgit-mode-map>\
870\\[stgit-help] Show this help text
871\\[stgit-quit] Hide the StGit buffer
872\\[describe-bindings] Show all key bindings
873
874\\[stgit-reload-or-repair] Reload the StGit buffer
875\\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
876
877\\[stgit-undo] Undo most recent StGit operation
878\\[stgit-redo] Undo recent undo
879
880\\[stgit-git-status] Run `git-status' (if available)
881
882Movement commands:
883\\[stgit-previous-line] Move to previous line
884\\[stgit-next-line] Move to next line
885\\[stgit-previous-patch] Move to previous patch
886\\[stgit-next-patch] Move to next patch
887
d11e0621 888\\[stgit-mark-down] Mark patch and move down
fdf5e327
GH
889\\[stgit-unmark-up] Unmark patch and move up
890\\[stgit-unmark-down] Unmark patch and move down
891
892Commands for patches:
893\\[stgit-select] Toggle showing changed files in patch
894\\[stgit-refresh] Refresh patch with changes in index or work tree
895\\[stgit-diff] Show the patch log and diff
896
afbf766b
GH
897\\[stgit-expand] Show changes in selected patches
898\\[stgit-collapse] Hide changes in selected patches
899
fdf5e327 900\\[stgit-new] Create a new, empty patch
2acb7116 901\\[stgit-new-and-refresh] Create a new patch from index or work tree
fdf5e327
GH
902\\[stgit-rename] Rename patch
903\\[stgit-edit] Edit patch description
904\\[stgit-delete] Delete patch(es)
905
1629f59f 906\\[stgit-revert] Revert all changes in index or work tree
dde3ab4d 907\\[stgit-toggle-index] Toggle all changes between index and work tree
1629f59f 908
fdf5e327
GH
909\\[stgit-push-next] Push next patch onto stack
910\\[stgit-pop-next] Pop current patch from stack
911\\[stgit-push-or-pop] Push or pop patch at point
912\\[stgit-goto] Make current patch current by popping or pushing
913
914\\[stgit-squash] Squash (meld together) patches
915\\[stgit-move-patches] Move patch(es) to point
916
917\\[stgit-commit] Commit patch(es)
918\\[stgit-uncommit] Uncommit patch(es)
919
920Commands for files:
921\\[stgit-select] Open the file in this window
922\\[stgit-find-file-other-window] Open the file in another window
923\\[stgit-diff] Show the file's diff
924
dde3ab4d 925\\[stgit-toggle-index] Toggle change between index and work tree
1629f59f 926\\[stgit-revert] Revert changes to file
fdf5e327
GH
927
928Display commands:
929\\[stgit-toggle-worktree] Toggle showing index and work tree
930\\[stgit-toggle-unknown] Toggle showing unknown files
931\\[stgit-toggle-ignored] Toggle showing ignored files
932
933Commands for diffs:
934\\[stgit-diff] Show diff of patch or file
935\\[stgit-diff-base] Show diff against the merge base
936\\[stgit-diff-ours] Show diff against our branch
937\\[stgit-diff-theirs] Show diff against their branch
938
939 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
940ignore space changes.
941 With two prefix arguments (e.g., \\[universal-argument] \
942\\[universal-argument] \\[stgit-diff]), ignore all space changes.
943
944Commands for merge conflicts:
945\\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
946\\[stgit-resolve-file] Mark unmerged file as resolved
947
948Commands for branches:
949\\[stgit-branch] Switch to another branch
380a021f 950\\[stgit-rebase] Rebase the current branch
fdf5e327
GH
951
952Customization variables:
953`stgit-abbreviate-copies-and-renames'
954`stgit-default-show-worktree'
955`stgit-find-copies-harder'
956`stgit-show-worktree-mode'
957
958See also \\[customize-group] for the \"stgit\" group."
56d81fe5
DK
959 (kill-all-local-variables)
960 (buffer-disable-undo)
961 (setq mode-name "StGit"
962 major-mode 'stgit-mode
963 goal-column 2)
964 (use-local-map stgit-mode-map)
965 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 966 (set (make-local-variable 'stgit-marked-patches) nil)
6467d976 967 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
ce3b6130 968 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
969 (set (make-local-variable 'stgit-index-node) nil)
970 (set (make-local-variable 'stgit-worktree-node) nil)
224ef1ec 971 (set (make-local-variable 'parse-sexp-lookup-properties) t)
2870f8b8 972 (set-variable 'truncate-lines 't)
b894e680 973 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
974 (run-hooks 'stgit-mode-hook))
975
b894e680
DK
976(defun stgit-update-saved-file ()
977 (let* ((file (expand-file-name buffer-file-name))
978 (dir (file-name-directory file))
979 (gitdir (condition-case nil (git-get-top-dir dir)
980 (error nil)))
981 (buffer (and gitdir (stgit-find-buffer gitdir))))
982 (when buffer
983 (with-current-buffer buffer
210a2a52 984 (stgit-refresh-worktree)))))
b894e680 985
d51722b7
GH
986(defun stgit-add-mark (patchsym)
987 "Mark the patch PATCHSYM."
8036afdd 988 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 989
d51722b7
GH
990(defun stgit-remove-mark (patchsym)
991 "Unmark the patch PATCHSYM."
8036afdd 992 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 993
e6b1fdae 994(defun stgit-clear-marks ()
47271f41 995 "Unmark all patches."
e6b1fdae
DK
996 (setq stgit-marked-patches '()))
997
735cb7ec 998(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
999 (get-text-property (point) 'patch-data))
1000
64ada6f5 1001(defun stgit-patch-name-at-point (&optional cause-error only-patches)
d51722b7 1002 "Return the patch name on the current line as a symbol.
64ada6f5
GH
1003If CAUSE-ERROR is not nil, signal an error if none found.
1004If ONLY-PATCHES is not nil, only allow real patches, and not
1005index or work tree."
2c862b07 1006 (let ((patch (stgit-patch-at-point)))
64ada6f5
GH
1007 (and patch
1008 only-patches
1009 (memq (stgit-patch-status patch) '(work index))
1010 (setq patch nil))
2c862b07
DK
1011 (cond (patch
1012 (stgit-patch-name patch))
1013 (cause-error
1014 (error "No patch on this line")))))
378a003d 1015
3164eec6
DK
1016(defun stgit-patched-file-at-point ()
1017 (get-text-property (point) 'file-data))
56d81fe5 1018
7755d7f1 1019(defun stgit-patches-marked-or-at-point ()
d51722b7 1020 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 1021 (if stgit-marked-patches
d51722b7 1022 stgit-marked-patches
2c862b07 1023 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
1024 (if patch
1025 (list patch)
1026 '()))))
1027
a9089e68 1028(defun stgit-goto-patch (patchsym &optional file)
d51722b7 1029 "Move point to the line containing patch PATCHSYM.
a9089e68
GH
1030If that patch cannot be found, do nothing.
1031
1032If the patch was found and FILE is not nil, instead move to that
1033file's line. If FILE cannot be found, stay on the line of
1034PATCHSYM."
f9b82d36
DK
1035 (let ((node (ewoc-nth stgit-ewoc 0)))
1036 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
1037 patchsym)))
1038 (setq node (ewoc-next stgit-ewoc node)))
a9089e68
GH
1039 (when (and node file)
1040 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
1041 (file-node (ewoc-nth file-ewoc 0)))
1042 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
1043 (setq file-node (ewoc-next file-ewoc file-node)))
1044 (when file-node
1045 (ewoc-goto-node file-ewoc file-node)
1046 (move-to-column (stgit-goal-column))
1047 (setq node nil))))
f9b82d36
DK
1048 (when node
1049 (ewoc-goto-node stgit-ewoc node)
d51722b7 1050 (move-to-column goal-column))))
56d81fe5 1051
1c2426dc 1052(defun stgit-init ()
a53347d9 1053 "Run stg init."
1c2426dc 1054 (interactive)
9d04c657 1055 (stgit-assert-mode)
1c2426dc 1056 (stgit-capture-output nil
b0424080 1057 (stgit-run "init"))
1f0bf00f 1058 (stgit-reload))
1c2426dc 1059
d11e0621
GH
1060(defun stgit-toggle-mark ()
1061 "Toggle mark on the patch under point."
1062 (interactive)
1063 (stgit-assert-mode)
1064 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1065 (stgit-unmark)
1066 (stgit-mark)))
1067
6df83d42 1068(defun stgit-mark ()
a53347d9 1069 "Mark the patch under point."
6df83d42 1070 (interactive)
9d04c657 1071 (stgit-assert-mode)
8036afdd 1072 (let* ((node (ewoc-locate stgit-ewoc))
64ada6f5
GH
1073 (patch (ewoc-data node))
1074 (name (stgit-patch-name patch)))
1075 (when (eq name :work)
1076 (error "Cannot mark the work tree"))
1077 (when (eq name :index)
1078 (error "Cannot mark the index"))
8036afdd 1079 (stgit-add-mark (stgit-patch-name patch))
d11e0621
GH
1080 (let ((column (current-column)))
1081 (ewoc-invalidate stgit-ewoc node)
1082 (move-to-column column))))
1083
1084(defun stgit-mark-down ()
1085 "Mark the patch under point and move to the next patch."
1086 (interactive)
1087 (stgit-mark)
378a003d 1088 (stgit-next-patch))
6df83d42 1089
d11e0621
GH
1090(defun stgit-unmark ()
1091 "Remove mark from the patch on the current line."
6df83d42 1092 (interactive)
9d04c657 1093 (stgit-assert-mode)
8036afdd
DK
1094 (let* ((node (ewoc-locate stgit-ewoc))
1095 (patch (ewoc-data node)))
1096 (stgit-remove-mark (stgit-patch-name patch))
d11e0621
GH
1097 (let ((column (current-column)))
1098 (ewoc-invalidate stgit-ewoc node)
1099 (move-to-column column))))
1100
1101(defun stgit-unmark-up ()
1102 "Remove mark from the patch on the previous line."
1103 (interactive)
1104 (stgit-assert-mode)
1105 (stgit-previous-patch)
1106 (stgit-unmark))
9b151b27
GH
1107
1108(defun stgit-unmark-down ()
a53347d9 1109 "Remove mark from the patch on the current line."
9b151b27 1110 (interactive)
9d04c657 1111 (stgit-assert-mode)
d11e0621 1112 (stgit-unmark)
1288eda2 1113 (stgit-next-patch))
6df83d42 1114
56d81fe5 1115(defun stgit-rename (name)
018fa1ac 1116 "Rename the patch under point to NAME."
64ada6f5
GH
1117 (interactive (list
1118 (read-string "Patch name: "
1119 (symbol-name (stgit-patch-name-at-point t t)))))
9d04c657 1120 (stgit-assert-mode)
64ada6f5 1121 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 1122 (stgit-capture-output nil
d51722b7
GH
1123 (stgit-run "rename" old-patchsym name))
1124 (let ((name-sym (intern name)))
1125 (when (memq old-patchsym stgit-expanded-patches)
378a003d 1126 (setq stgit-expanded-patches
6a73154a 1127 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
d51722b7 1128 (when (memq old-patchsym stgit-marked-patches)
378a003d 1129 (setq stgit-marked-patches
6a73154a 1130 (cons name-sym (delq old-patchsym stgit-marked-patches))))
d51722b7
GH
1131 (stgit-reload)
1132 (stgit-goto-patch name-sym))))
56d81fe5 1133
408fa7cb
GH
1134(defun stgit-reload-or-repair (repair)
1135 "Update the contents of the StGit buffer (`stgit-reload').
1136
1137With a prefix argument, repair the StGit metadata if the branch
1138was modified with git commands (`stgit-repair')."
1139 (interactive "P")
9d04c657 1140 (stgit-assert-mode)
408fa7cb
GH
1141 (if repair
1142 (stgit-repair)
1143 (stgit-reload)))
1144
26201d96 1145(defun stgit-repair ()
a53347d9 1146 "Run stg repair."
26201d96 1147 (interactive)
9d04c657 1148 (stgit-assert-mode)
26201d96 1149 (stgit-capture-output nil
b0424080 1150 (stgit-run "repair"))
1f0bf00f 1151 (stgit-reload))
26201d96 1152
adeef6bc
GH
1153(defun stgit-available-branches ()
1154 "Returns a list of the available stg branches"
1155 (let ((output (with-output-to-string
1156 (stgit-run "branch" "--list")))
1157 (start 0)
1158 result)
1159 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1160 (setq result (cons (match-string 1 output) result))
1161 (setq start (match-end 0)))
1162 result))
1163
1164(defun stgit-branch (branch)
1165 "Switch to branch BRANCH."
1166 (interactive (list (completing-read "Switch to branch: "
1167 (stgit-available-branches))))
9d04c657 1168 (stgit-assert-mode)
adeef6bc
GH
1169 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1170 (stgit-reload))
1171
380a021f
GH
1172(defun stgit-available-refs (&optional omit-stgit)
1173 "Returns a list of the available git refs.
1174If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1175 (let* ((output (with-output-to-string
1176 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1177 "refs/tags" "refs/heads"
1178 "refs/remotes")))
1179 (result (split-string output "\n" t)))
1180 (mapcar (lambda (s)
1181 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1182 (substring s (match-end 0))
1183 s))
1184 (if omit-stgit
1185 (delete-if (lambda (s)
1186 (string-match "^refs/heads/.*\\.stgit$" s))
1187 result)
1188 result))))
1189
1190(defun stgit-rebase (new-base)
1191 "Rebase to NEW-BASE."
1192 (interactive (list (completing-read "Rebase to: "
1193 (stgit-available-refs t))))
9d04c657 1194 (stgit-assert-mode)
380a021f
GH
1195 (stgit-capture-output nil (stgit-run "rebase" new-base))
1196 (stgit-reload))
1197
41c1c59c
GH
1198(defun stgit-commit (count)
1199 "Run stg commit on COUNT commits.
e552cb5f
GH
1200Interactively, the prefix argument is used as COUNT.
1201A negative COUNT will uncommit instead."
41c1c59c 1202 (interactive "p")
9d04c657 1203 (stgit-assert-mode)
e552cb5f
GH
1204 (if (< count 0)
1205 (stgit-uncommit (- count))
1206 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1207 (stgit-reload)))
1208
1209(defun stgit-uncommit (count)
1210 "Run stg uncommit on COUNT commits.
1211Interactively, the prefix argument is used as COUNT.
1212A negative COUNT will commit instead."
1213 (interactive "p")
9d04c657 1214 (stgit-assert-mode)
e552cb5f
GH
1215 (if (< count 0)
1216 (stgit-commit (- count))
1217 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1218 (stgit-reload)))
c4aad9a7 1219
556345d3
GH
1220(defun stgit-neighbour-file ()
1221 "Return the file name of the next file after point, or the
1222previous file if point is at the last file within a patch."
1223 (let ((old-point (point))
1224 neighbour-file)
1225 (and (zerop (forward-line 1))
1226 (let ((f (stgit-patched-file-at-point)))
1227 (and f (setq neighbour-file (stgit-file-file f)))))
1228 (goto-char old-point)
1229 (unless neighbour-file
1230 (and (zerop (forward-line -1))
1231 (let ((f (stgit-patched-file-at-point)))
1232 (and f (setq neighbour-file (stgit-file-file f)))))
1233 (goto-char old-point))
1234 neighbour-file))
1235
3959a095
GH
1236(defun stgit-revert-file ()
1237 "Revert the file at point, which must be in the index or the
1238working tree."
1239 (interactive)
9d04c657 1240 (stgit-assert-mode)
3959a095
GH
1241 (let* ((patched-file (or (stgit-patched-file-at-point)
1242 (error "No file on the current line")))
1243 (patch-name (stgit-patch-name-at-point))
1244 (file-status (stgit-file-status patched-file))
1245 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1246 (stgit-file-cr-to patched-file))
1247 ((eq file-status 'add)
1248 (stgit-file-file patched-file))))
1249 (co-file (cond ((eq file-status 'rename)
1250 (stgit-file-cr-from patched-file))
1251 ((not (memq file-status '(copy add)))
556345d3
GH
1252 (stgit-file-file patched-file))))
1253 (next-file (stgit-neighbour-file)))
3959a095
GH
1254
1255 (unless (memq patch-name '(:work :index))
1256 (error "No index or working tree file on this line"))
1257
d9473917
GH
1258 (when (eq file-status 'ignore)
1259 (error "Cannot revert ignored files"))
1260
1261 (when (eq file-status 'unknown)
1262 (error "Cannot revert unknown files"))
1263
3959a095
GH
1264 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1265 (when (yes-or-no-p (format "Revert %d file%s? "
1266 nfiles
1267 (if (= nfiles 1) "" "s")))
1268 (stgit-capture-output nil
1269 (when rm-file
1270 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1271 (when co-file
1272 (stgit-run-git "checkout" "HEAD" co-file)))
556345d3
GH
1273 (stgit-reload)
1274 (stgit-goto-patch patch-name next-file)))))
1629f59f
GH
1275
1276(defun stgit-revert ()
1277 "Revert the change at point, which must be the index, the work
1278tree, or a single change in either."
1279 (interactive)
9d04c657 1280 (stgit-assert-mode)
1629f59f
GH
1281 (let ((patched-file (stgit-patched-file-at-point)))
1282 (if patched-file
1283 (stgit-revert-file)
1284 (let* ((patch-name (or (stgit-patch-name-at-point)
1285 (error "No patch or file at point")))
1286 (patch-desc (case patch-name
1287 (:index "index")
1288 (:work "work tree")
1289 (t (error (substitute-command-keys
1290 "Use \\[stgit-delete] to delete a patch"))))))
1291 (when (if (eq patch-name :work)
1292 (stgit-work-tree-empty-p)
1293 (stgit-index-empty-p))
1294 (error (format "There are no changes in the %s to revert"
1295 patch-desc)))
1296 (and (eq patch-name :index)
1297 (not (stgit-work-tree-empty-p))
1298 (error "Cannot revert index as work tree contains unstaged changes"))
1299
1300 (when (yes-or-no-p (format "Revert all changes in the %s? "
1301 patch-desc))
1302 (if (eq patch-name :index)
1303 (stgit-run-git-silent "reset" "--hard" "-q")
1304 (stgit-run-git-silent "checkout" "--" "."))
1305 (stgit-refresh-index)
1306 (stgit-refresh-worktree)
1307 (stgit-goto-patch patch-name))))))
3959a095 1308
51783171
GH
1309(defun stgit-resolve-file ()
1310 "Resolve conflict in the file at point."
1311 (interactive)
9d04c657 1312 (stgit-assert-mode)
51783171
GH
1313 (let* ((patched-file (stgit-patched-file-at-point))
1314 (patch (stgit-patch-at-point))
1315 (patch-name (and patch (stgit-patch-name patch)))
1316 (status (and patched-file (stgit-file-status patched-file))))
1317
1318 (unless (memq patch-name '(:work :index))
1319 (error "No index or working tree file on this line"))
1320
1321 (unless (eq status 'unmerged)
1322 (error "No conflict to resolve at the current line"))
1323
1324 (stgit-capture-output nil
1325 (stgit-move-change-to-index (stgit-file-file patched-file)))
1326
1327 (stgit-reload)))
1328
0b661144
DK
1329(defun stgit-push-next (npatches)
1330 "Push the first unapplied patch.
1331With numeric prefix argument, push that many patches."
1332 (interactive "p")
9d04c657 1333 (stgit-assert-mode)
d51722b7 1334 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
1335 (stgit-reload)
1336 (stgit-refresh-git-status))
56d81fe5 1337
0b661144
DK
1338(defun stgit-pop-next (npatches)
1339 "Pop the topmost applied patch.
1340With numeric prefix argument, pop that many patches."
1341 (interactive "p")
9d04c657 1342 (stgit-assert-mode)
d51722b7 1343 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
1344 (stgit-reload)
1345 (stgit-refresh-git-status))
56d81fe5 1346
5fd965bb
GH
1347(defun stgit-applied-at-point-p ()
1348 "Return non-nil if the patch at point is applied."
1349 (let ((patch (stgit-patch-at-point t)))
1350 (not (eq (stgit-patch-status patch) 'unapplied))))
f9182fca
KH
1351
1352(defun stgit-push-or-pop ()
a53347d9 1353 "Push or pop the patch on the current line."
f9182fca 1354 (interactive)
9d04c657 1355 (stgit-assert-mode)
5fd965bb
GH
1356 (let ((patchsym (stgit-patch-name-at-point t t))
1357 (applied (stgit-applied-at-point-p)))
f9182fca 1358 (stgit-capture-output nil
d51722b7 1359 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 1360 (stgit-reload)))
f9182fca 1361
c7adf5ef 1362(defun stgit-goto ()
a53347d9 1363 "Go to the patch on the current line."
c7adf5ef 1364 (interactive)
9d04c657 1365 (stgit-assert-mode)
2c862b07 1366 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 1367 (stgit-capture-output nil
d51722b7 1368 (stgit-run "goto" patchsym))
1f0bf00f 1369 (stgit-reload)))
c7adf5ef 1370
d51722b7 1371(defun stgit-id (patchsym)
50d88c67
DK
1372 "Return the git commit id for PATCHSYM.
1373If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1374 (if (keywordp patchsym)
1375 patchsym
1376 (let ((result (with-output-to-string
1377 (stgit-run-silent "id" patchsym))))
1378 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1379 (error "Cannot find commit id for %s" patchsym))
1380 (match-string 1 result))))
378a003d 1381
1aece5c0 1382(defun stgit-show-patch (unmerged-stage ignore-whitespace)
d9b954c7
GH
1383 "Show the patch on the current line.
1384
1385UNMERGED-STAGE is the argument to `git-diff' that that selects
1386which stage to diff against in the case of unmerged files."
1aece5c0
GH
1387 (let ((space-arg (when (numberp ignore-whitespace)
1388 (cond ((> ignore-whitespace 4)
1389 "--ignore-all-space")
1390 ((> ignore-whitespace 1)
1391 "--ignore-space-change"))))
1392 (patch-name (stgit-patch-name-at-point t)))
1393 (stgit-capture-output "*StGit patch*"
1394 (case (get-text-property (point) 'entry-type)
1395 ('file
1396 (let* ((patched-file (stgit-patched-file-at-point))
1397 (patch-id (let ((id (stgit-id patch-name)))
1398 (if (and (eq id :index)
1399 (eq (stgit-file-status patched-file)
1400 'unmerged))
1401 :work
1402 id)))
1403 (args (append (and space-arg (list space-arg))
1404 (and (stgit-file-cr-from patched-file)
1405 (list (stgit-find-copies-harder-diff-arg)))
1406 (cond ((eq patch-id :index)
1407 '("--cached"))
1408 ((eq patch-id :work)
1409 (list unmerged-stage))
1410 (t
1411 (list (concat patch-id "^") patch-id)))
1412 '("--")
3164eec6
DK
1413 (if (stgit-file-copy-or-rename patched-file)
1414 (list (stgit-file-cr-from patched-file)
1415 (stgit-file-cr-to patched-file))
1416 (list (stgit-file-file patched-file))))))
1aece5c0
GH
1417 (apply 'stgit-run-git "diff" args)))
1418 ('patch
1419 (let* ((patch-id (stgit-id patch-name)))
1420 (if (or (eq patch-id :index) (eq patch-id :work))
1421 (apply 'stgit-run-git "diff"
1422 (stgit-find-copies-harder-diff-arg)
1423 (append (and space-arg (list space-arg))
1424 (if (eq patch-id :index)
1425 '("--cached")
1426 (list unmerged-stage))))
1427 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1428 (and space-arg (list "-O" space-arg))
1429 (list (stgit-patch-name-at-point)))))
1430 (apply 'stgit-run args)))))
6a73154a
GH
1431 (t
1432 (error "No patch or file at point")))
1aece5c0
GH
1433 (with-current-buffer standard-output
1434 (goto-char (point-min))
1435 (diff-mode)))))
1436
1437(defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1438 `(defun ,name (&optional ignore-whitespace)
1439 ,(format "Show the patch on the current line.
1440
1441%sWith a prefix argument, ignore whitespace. With a prefix argument
1442greater than four (e.g., \\[universal-argument] \
1443\\[universal-argument] \\[%s]), ignore all whitespace."
1444 (if unmerged-action
1445 (format "For unmerged files, %s.\n\n" unmerged-action)
1446 "")
1447 name)
1448 (interactive "p")
9d04c657 1449 (stgit-assert-mode)
1aece5c0
GH
1450 (stgit-show-patch ,diff-arg ignore-whitespace)))
1451
1452(stgit-define-diff stgit-diff
1453 "--ours" nil)
1454(stgit-define-diff stgit-diff-ours
1455 "--ours"
1456 "diff against our branch")
1457(stgit-define-diff stgit-diff-theirs
1458 "--theirs"
1459 "diff against their branch")
1460(stgit-define-diff stgit-diff-base
1461 "--base"
1462 "diff against the merge base")
1463(stgit-define-diff stgit-diff-combined
1464 "--cc"
1465 "show a combined diff")
d9b954c7 1466
f87c2e22
GH
1467(defun stgit-move-change-to-index (file &optional force)
1468 "Copies the work tree state of FILE to index, using git add or git rm.
1469
1470If FORCE is not nil, use --force."
306b37a6
GH
1471 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1472 '("add") '("rm" "-q"))))
37cb5766 1473 (stgit-capture-output "*git output*"
f87c2e22
GH
1474 (apply 'stgit-run-git (append op (and force '("--force"))
1475 '("--") (list file))))))
37cb5766 1476
fd9fe574 1477(defun stgit-remove-change-from-index (file)
37cb5766
DK
1478 "Unstages the change in FILE from the index"
1479 (stgit-capture-output "*git output*"
1480 (stgit-run-git "reset" "-q" "--" file)))
1481
dde3ab4d
GH
1482(defun stgit-git-index-unmerged-p ()
1483 (let (result)
1484 (with-output-to-string
1485 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1486 "--diff-filter=U"
1487 "--quiet" "HEAD")))))
1488 result))
1489
37cb5766 1490(defun stgit-file-toggle-index ()
a9089e68
GH
1491 "Move modified file in or out of the index.
1492
1493Leaves the point where it is, but moves the mark to where the
1494file ended up. You can then jump to the file with \
1495\\[exchange-point-and-mark]."
37cb5766 1496 (interactive)
9d04c657 1497 (stgit-assert-mode)
612f999a
GH
1498 (let* ((patched-file (or (stgit-patched-file-at-point)
1499 (error "No file on the current line")))
1500 (patched-status (stgit-file-status patched-file)))
1501 (when (eq patched-status 'unmerged)
51783171 1502 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
a9089e68
GH
1503 (let* ((patch (stgit-patch-at-point))
1504 (patch-name (stgit-patch-name patch))
612f999a
GH
1505 (mark-file (if (eq patched-status 'rename)
1506 (stgit-file-cr-to patched-file)
1507 (stgit-file-file patched-file)))
1508 (point-file (if (eq patched-status 'rename)
6a73154a
GH
1509 (stgit-file-cr-from patched-file)
1510 (stgit-neighbour-file))))
a9089e68 1511
37cb5766 1512 (cond ((eq patch-name :work)
f87c2e22
GH
1513 (stgit-move-change-to-index (stgit-file-file patched-file)
1514 (eq patched-status 'ignore)))
37cb5766 1515 ((eq patch-name :index)
fd9fe574 1516 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766 1517 (t
612f999a 1518 (error "Can only move files between working tree and index")))
a9089e68
GH
1519 (stgit-refresh-worktree)
1520 (stgit-refresh-index)
612f999a 1521 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
a9089e68 1522 (push-mark nil t t)
612f999a 1523 (stgit-goto-patch patch-name point-file))))
37cb5766 1524
dde3ab4d
GH
1525(defun stgit-toggle-index ()
1526 "Move change in or out of the index.
1527
1528Works on index and work tree, as well as files in either.
1529
1530Leaves the point where it is, but moves the mark to where the
1531file ended up. You can then jump to the file with \
1532\\[exchange-point-and-mark]."
1533 (interactive)
9d04c657 1534 (stgit-assert-mode)
dde3ab4d
GH
1535 (if (stgit-patched-file-at-point)
1536 (stgit-file-toggle-index)
1537 (let ((patch-name (stgit-patch-name-at-point)))
1538 (unless (memq patch-name '(:index :work))
1539 (error "Can only move changes between working tree and index"))
1540 (when (stgit-git-index-unmerged-p)
1541 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1542 (if (if (eq patch-name :index)
1543 (stgit-index-empty-p)
1544 (stgit-work-tree-empty-p))
1545 (message "No changes to be moved")
1546 (stgit-capture-output nil
1547 (if (eq patch-name :work)
1548 (stgit-run-git "add" "--update")
1549 (stgit-run-git "reset" "--mixed" "-q")))
1550 (stgit-refresh-worktree)
1551 (stgit-refresh-index))
1552 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1553
0bca35c8 1554(defun stgit-edit ()
a53347d9 1555 "Edit the patch on the current line."
0bca35c8 1556 (interactive)
9d04c657 1557 (stgit-assert-mode)
64ada6f5 1558 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 1559 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
1560 (dir default-directory))
1561 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 1562 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
1563 (setq default-directory dir)
1564 (let ((standard-output edit-buf))
d51722b7 1565 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
1566
1567(defun stgit-confirm-edit ()
1568 (interactive)
1569 (let ((file (make-temp-file "stgit-edit-")))
1570 (write-region (point-min) (point-max) file)
1571 (stgit-capture-output nil
d51722b7 1572 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 1573 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1574 (stgit-reload))))
0bca35c8 1575
2acb7116 1576(defun stgit-new (add-sign &optional refresh)
aa04f831
GH
1577 "Create a new patch.
1578With a prefix argument, include a \"Signed-off-by:\" line at the
1579end of the patch."
1580 (interactive "P")
9d04c657 1581 (stgit-assert-mode)
c5d45b92
GH
1582 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1583 (dir default-directory))
1584 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831 1585 (setq default-directory dir)
2acb7116 1586 (set (make-local-variable 'stgit-refresh-after-new) refresh)
aa04f831
GH
1587 (when add-sign
1588 (save-excursion
1589 (let ((standard-output (current-buffer)))
1590 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
1591
1592(defun stgit-confirm-new ()
1593 (interactive)
2acb7116
DK
1594 (let ((file (make-temp-file "stgit-edit-"))
1595 (refresh stgit-refresh-after-new))
64c097a0
DK
1596 (write-region (point-min) (point-max) file)
1597 (stgit-capture-output nil
27b0f9e4 1598 (stgit-run "new" "-f" file))
64c097a0 1599 (with-current-buffer log-edit-parent-buffer
2acb7116
DK
1600 (if refresh
1601 (stgit-refresh)
1602 (stgit-reload)))))
1603
1604(defun stgit-new-and-refresh (add-sign)
1605 "Create a new patch and refresh it with the current changes.
1606
1607With a prefix argument, include a \"Signed-off-by:\" line at the
1608end of the patch.
1609
1610This works just like running `stgit-new' followed by `stgit-refresh'."
1611 (interactive "P")
9d04c657 1612 (stgit-assert-mode)
2acb7116 1613 (stgit-new add-sign t))
64c097a0
DK
1614
1615(defun stgit-create-patch-name (description)
1616 "Create a patch name from a long description"
1617 (let ((patch ""))
1618 (while (> (length description) 0)
1619 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
1620 (setq patch (downcase (concat patch
1621 (match-string 0 description))))
64c097a0
DK
1622 (setq description (substring description (match-end 0))))
1623 ((string-match "\\` +" description)
1624 (setq patch (concat patch "-"))
1625 (setq description (substring description (match-end 0))))
1626 ((string-match "\\`[^a-zA-Z_-]+" description)
1627 (setq description (substring description (match-end 0))))))
1628 (cond ((= (length patch) 0)
1629 "patch")
1630 ((> (length patch) 20)
1631 (substring patch 0 20))
1632 (t patch))))
0bca35c8 1633
9008e45b 1634(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 1635 "Delete the patches in PATCHSYMS.
9008e45b
GH
1636Interactively, delete the marked patches, or the patch at point.
1637
1638With a prefix argument, or SPILL-P, spill the patch contents to
1639the work tree and index."
1640 (interactive (list (stgit-patches-marked-or-at-point)
1641 current-prefix-arg))
9d04c657 1642 (stgit-assert-mode)
e7231e4f
GH
1643 (unless patchsyms
1644 (error "No patches to delete"))
64ada6f5
GH
1645 (when (memq :index patchsyms)
1646 (error "Cannot delete the index"))
1647 (when (memq :work patchsyms)
1648 (error "Cannot delete the work tree"))
1649
d51722b7 1650 (let ((npatches (length patchsyms)))
9008e45b 1651 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 1652 npatches
9008e45b
GH
1653 (if (= 1 npatches) "" "es")
1654 (if spill-p
1655 " (spilling contents to index)"
1656 "")))
1657 (let ((args (if spill-p
1658 (cons "--spill" patchsyms)
1659 patchsyms)))
1660 (stgit-capture-output nil
1661 (apply 'stgit-run "delete" args))
1662 (stgit-reload)))))
d51722b7 1663
7cc45294
GH
1664(defun stgit-move-patches-target ()
1665 "Return the patchsym indicating a target patch for
1666`stgit-move-patches'.
1667
1668This is either the patch at point, or one of :top and :bottom, if
1669the point is after or before the applied patches."
1670
980d8cfb 1671 (let ((patchsym (stgit-patch-name-at-point nil t)))
7cc45294
GH
1672 (cond (patchsym patchsym)
1673 ((save-excursion (re-search-backward "^>" nil t)) :top)
1674 (t :bottom))))
1675
95369f6c
GH
1676(defun stgit-sort-patches (patchsyms)
1677 "Returns the list of patches in PATCHSYMS sorted according to
1678their position in the patch series, bottommost first.
1679
2d7bcbd9 1680PATCHSYMS must not contain duplicate entries."
95369f6c
GH
1681 (let (sorted-patchsyms
1682 (series (with-output-to-string
1683 (with-current-buffer standard-output
1684 (stgit-run-silent "series" "--noprefix"))))
1685 start)
1686 (while (string-match "^\\(.+\\)" series start)
1687 (let ((patchsym (intern (match-string 1 series))))
1688 (when (memq patchsym patchsyms)
1689 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1690 (setq start (match-end 0)))
1691 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1692
1693 (unless (= (length patchsyms) (length sorted-patchsyms))
1694 (error "Internal error"))
1695
1696 sorted-patchsyms))
1697
7cc45294
GH
1698(defun stgit-move-patches (patchsyms target-patch)
1699 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1700If TARGET-PATCH is :bottom or :top, move the patches to the
1701bottom or top of the stack, respectively.
1702
1703Interactively, move the marked patches to where the point is."
1704 (interactive (list stgit-marked-patches
1705 (stgit-move-patches-target)))
9d04c657 1706 (stgit-assert-mode)
7cc45294
GH
1707 (unless patchsyms
1708 (error "Need at least one patch to move"))
1709
1710 (unless target-patch
1711 (error "Point not at a patch"))
1712
1713 (if (eq target-patch :top)
1714 (stgit-capture-output nil
1715 (apply 'stgit-run "float" patchsyms))
1716
1717 ;; need to have patchsyms sorted by position in the stack
95369f6c 1718 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1719 (while sorted-patchsyms
1720 (setq sorted-patchsyms
1721 (and (stgit-capture-output nil
1722 (if (eq target-patch :bottom)
1723 (stgit-run "sink" "--" (car sorted-patchsyms))
1724 (stgit-run "sink" "--to" target-patch "--"
1725 (car sorted-patchsyms))))
1726 (cdr sorted-patchsyms))))))
1727 (stgit-reload))
1728
594aa463
KH
1729(defun stgit-squash (patchsyms)
1730 "Squash the patches in PATCHSYMS.
693d179b
GH
1731Interactively, squash the marked patches.
1732
1733Unless there are any conflicts, the patches will be merged into
1734one patch, which will occupy the same spot in the series as the
1735deepest patch had before the squash."
d51722b7 1736 (interactive (list stgit-marked-patches))
9d04c657 1737 (stgit-assert-mode)
d51722b7 1738 (when (< (length patchsyms) 2)
594aa463 1739 (error "Need at least two patches to squash"))
32d7545d
GH
1740 (let ((stgit-buffer (current-buffer))
1741 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1742 (dir default-directory)
1743 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1744 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1745 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1746 (setq default-directory dir)
32d7545d
GH
1747 (let ((result (let ((standard-output edit-buf))
1748 (apply 'stgit-run-silent "squash"
1749 "--save-template=-" sorted-patchsyms))))
1750
1751 ;; stg squash may have reordered the patches or caused conflicts
1752 (with-current-buffer stgit-buffer
1753 (stgit-reload))
1754
1755 (unless (eq 0 result)
1756 (fundamental-mode)
1757 (rename-buffer "*StGit error*")
1758 (resize-temp-buffer-window)
1759 (switch-to-buffer-other-window stgit-buffer)
1760 (error "stg squash failed")))))
ea0def18 1761
594aa463 1762(defun stgit-confirm-squash ()
ea0def18
DK
1763 (interactive)
1764 (let ((file (make-temp-file "stgit-edit-")))
1765 (write-region (point-min) (point-max) file)
1766 (stgit-capture-output nil
594aa463 1767 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1768 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1769 (stgit-clear-marks)
1770 ;; Go to first marked patch and stay there
1771 (goto-char (point-min))
1772 (re-search-forward (concat "^[>+-]\\*") nil t)
1773 (move-to-column goal-column)
1774 (let ((pos (point)))
1f0bf00f 1775 (stgit-reload)
e6b1fdae 1776 (goto-char pos)))))
ea0def18 1777
0663524d
KH
1778(defun stgit-help ()
1779 "Display help for the StGit mode."
1780 (interactive)
1781 (describe-function 'stgit-mode))
3a59f3db 1782
83e51dbf
DK
1783(defun stgit-undo (&optional arg)
1784 "Run stg undo.
b8463f1d
GH
1785With prefix argument, run it with the --hard flag.
1786
1787See also `stgit-redo'."
83e51dbf 1788 (interactive "P")
9d04c657 1789 (stgit-assert-mode)
83e51dbf
DK
1790 (stgit-capture-output nil
1791 (if arg
1792 (stgit-run "undo" "--hard")
1793 (stgit-run "undo")))
1f0bf00f 1794 (stgit-reload))
83e51dbf 1795
b8463f1d
GH
1796(defun stgit-redo (&optional arg)
1797 "Run stg redo.
1798With prefix argument, run it with the --hard flag.
1799
1800See also `stgit-undo'."
1801 (interactive "P")
9d04c657 1802 (stgit-assert-mode)
b8463f1d
GH
1803 (stgit-capture-output nil
1804 (if arg
1805 (stgit-run "redo" "--hard")
1806 (stgit-run "redo")))
1807 (stgit-reload))
1808
4d73c4d8
DK
1809(defun stgit-refresh (&optional arg)
1810 "Run stg refresh.
36a4eacd
GH
1811If the index contains any changes, only refresh from index.
1812
a53347d9 1813With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8 1814 (interactive "P")
9d04c657 1815 (stgit-assert-mode)
4d73c4d8 1816 (let ((patchargs (if arg
b0424080
GH
1817 (let ((patches (stgit-patches-marked-or-at-point)))
1818 (cond ((null patches)
df283a8b 1819 (error "No patch to update"))
b0424080 1820 ((> (length patches) 1)
df283a8b 1821 (error "Too many patches selected"))
b0424080
GH
1822 (t
1823 (cons "-p" patches))))
1824 nil)))
36a4eacd
GH
1825 (unless (stgit-index-empty-p)
1826 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 1827 (stgit-capture-output nil
074a4fb0
GH
1828 (apply 'stgit-run "refresh" patchargs))
1829 (stgit-refresh-git-status))
4d73c4d8
DK
1830 (stgit-reload))
1831
ce3b6130 1832(defvar stgit-show-worktree nil
8f702de4 1833 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 1834
8f702de4 1835See also `stgit-show-worktree-mode'.")
ce3b6130 1836
d9473917
GH
1837(defvar stgit-show-ignored nil
1838 "If nil, inhibit showing files ignored by git.")
1839
1840(defvar stgit-show-unknown nil
1841 "If nil, inhibit showing files not registered with git.")
1842
ce3b6130
DK
1843(defun stgit-toggle-worktree (&optional arg)
1844 "Toggle the visibility of the work tree.
2d7bcbd9 1845With ARG, show the work tree if ARG is positive.
ce3b6130 1846
8f702de4
GH
1847Its initial setting is controlled by `stgit-default-show-worktree'.
1848
1849`stgit-show-worktree-mode' controls where on screen the index and
1850work tree will show up."
ce3b6130 1851 (interactive)
9d04c657 1852 (stgit-assert-mode)
ce3b6130
DK
1853 (setq stgit-show-worktree
1854 (if (numberp arg)
1855 (> arg 0)
1856 (not stgit-show-worktree)))
1857 (stgit-reload))
1858
d9473917
GH
1859(defun stgit-toggle-ignored (&optional arg)
1860 "Toggle the visibility of files ignored by git in the work
1861tree. With ARG, show these files if ARG is positive.
1862
1863Use \\[stgit-toggle-worktree] to show the work tree."
1864 (interactive)
9d04c657 1865 (stgit-assert-mode)
d9473917
GH
1866 (setq stgit-show-ignored
1867 (if (numberp arg)
1868 (> arg 0)
1869 (not stgit-show-ignored)))
1870 (stgit-reload))
1871
1872(defun stgit-toggle-unknown (&optional arg)
1873 "Toggle the visibility of files not registered with git in the
1874work tree. With ARG, show these files if ARG is positive.
1875
1876Use \\[stgit-toggle-worktree] to show the work tree."
1877 (interactive)
9d04c657 1878 (stgit-assert-mode)
d9473917
GH
1879 (setq stgit-show-unknown
1880 (if (numberp arg)
1881 (> arg 0)
1882 (not stgit-show-unknown)))
1883 (stgit-reload))
1884
3a59f3db 1885(provide 'stgit)