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