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