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