chiark / gitweb /
stgit.el: Fix some incorrect indentation
[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
50d88c67 669(defun stgit-select-patch ()
98230edd
DK
670 (let ((patchname (stgit-patch-name-at-point)))
671 (if (memq patchname stgit-expanded-patches)
672 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
673 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
674 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
675 (move-to-column (stgit-goal-column)))
acc5652f 676
378a003d 677(defun stgit-select ()
da01a29b
GH
678 "With point on a patch, toggle showing files in the patch.
679
680With point on a file, open the associated file. Opens the target
681file for (applied) copies and renames."
378a003d 682 (interactive)
50d88c67
DK
683 (case (get-text-property (point) 'entry-type)
684 ('patch
685 (stgit-select-patch))
686 ('file
030f0535 687 (stgit-find-file))
50d88c67
DK
688 (t
689 (error "No patch or file on line"))))
378a003d
GH
690
691(defun stgit-find-file-other-window ()
692 "Open file at point in other window"
693 (interactive)
030f0535 694 (stgit-find-file t))
378a003d 695
d9b954c7
GH
696(defun stgit-find-file-merge ()
697 "Open file at point and merge it using `smerge-ediff'."
698 (interactive)
699 (stgit-find-file t)
700 (smerge-ediff))
701
83327d53 702(defun stgit-quit ()
a53347d9 703 "Hide the stgit buffer."
83327d53
GH
704 (interactive)
705 (bury-buffer))
706
0f076fe6 707(defun stgit-git-status ()
a53347d9 708 "Show status using `git-status'."
0f076fe6
GH
709 (interactive)
710 (unless (fboundp 'git-status)
df283a8b 711 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
712 (let ((dir default-directory))
713 (save-selected-window
714 (pop-to-buffer nil)
715 (git-status dir))))
716
58f72f16
GH
717(defun stgit-goal-column ()
718 "Return goal column for the current line"
50d88c67
DK
719 (case (get-text-property (point) 'entry-type)
720 ('patch 2)
721 ('file 4)
722 (t 0)))
58f72f16
GH
723
724(defun stgit-next-line (&optional arg)
378a003d 725 "Move cursor vertically down ARG lines"
58f72f16
GH
726 (interactive "p")
727 (next-line arg)
728 (move-to-column (stgit-goal-column)))
378a003d 729
58f72f16 730(defun stgit-previous-line (&optional arg)
378a003d 731 "Move cursor vertically up ARG lines"
58f72f16
GH
732 (interactive "p")
733 (previous-line arg)
734 (move-to-column (stgit-goal-column)))
378a003d
GH
735
736(defun stgit-next-patch (&optional arg)
98230edd 737 "Move cursor down ARG patches."
378a003d 738 (interactive "p")
98230edd
DK
739 (ewoc-goto-next stgit-ewoc (or arg 1))
740 (move-to-column goal-column))
378a003d
GH
741
742(defun stgit-previous-patch (&optional arg)
98230edd 743 "Move cursor up ARG patches."
378a003d 744 (interactive "p")
98230edd
DK
745 (ewoc-goto-prev stgit-ewoc (or arg 1))
746 (move-to-column goal-column))
378a003d 747
56d81fe5
DK
748(defvar stgit-mode-hook nil
749 "Run after `stgit-mode' is setup.")
750
751(defvar stgit-mode-map nil
752 "Keymap for StGit major mode.")
753
754(unless stgit-mode-map
d9b954c7
GH
755 (let ((diff-map (make-keymap))
756 (toggle-map (make-keymap)))
757 (suppress-keymap diff-map)
758 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
759 '(("b" . stgit-diff-base)
760 ("c" . stgit-diff-combined)
761 ("m" . stgit-find-file-merge)
762 ("o" . stgit-diff-ours)
763 ("t" . stgit-diff-theirs)))
ce3b6130
DK
764 (suppress-keymap toggle-map)
765 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
d9473917
GH
766 '(("t" . stgit-toggle-worktree)
767 ("i" . stgit-toggle-ignored)
768 ("u" . stgit-toggle-unknown)))
ce3b6130
DK
769 (setq stgit-mode-map (make-keymap))
770 (suppress-keymap stgit-mode-map)
771 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
772 `((" " . stgit-mark)
773 ("m" . stgit-mark)
774 ("\d" . stgit-unmark-up)
775 ("u" . stgit-unmark-down)
776 ("?" . stgit-help)
777 ("h" . stgit-help)
778 ("\C-p" . stgit-previous-line)
779 ("\C-n" . stgit-next-line)
780 ([up] . stgit-previous-line)
781 ([down] . stgit-next-line)
782 ("p" . stgit-previous-patch)
783 ("n" . stgit-next-patch)
784 ("\M-{" . stgit-previous-patch)
785 ("\M-}" . stgit-next-patch)
786 ("s" . stgit-git-status)
408fa7cb 787 ("g" . stgit-reload-or-repair)
ce3b6130
DK
788 ("r" . stgit-refresh)
789 ("\C-c\C-r" . stgit-rename)
790 ("e" . stgit-edit)
791 ("M" . stgit-move-patches)
792 ("S" . stgit-squash)
793 ("N" . stgit-new)
2acb7116 794 ("c" . stgit-new-and-refresh)
e9fdd4ea
GH
795 ("\C-c\C-c" . stgit-commit)
796 ("\C-c\C-u" . stgit-uncommit)
3959a095 797 ("U" . stgit-revert-file)
51783171 798 ("R" . stgit-resolve-file)
ce3b6130
DK
799 ("\r" . stgit-select)
800 ("o" . stgit-find-file-other-window)
801 ("i" . stgit-file-toggle-index)
802 (">" . stgit-push-next)
803 ("<" . stgit-pop-next)
804 ("P" . stgit-push-or-pop)
805 ("G" . stgit-goto)
d9b954c7 806 ("=" . stgit-diff)
ce3b6130 807 ("D" . stgit-delete)
b8463f1d 808 ([?\C-/] . stgit-undo)
ce3b6130 809 ("\C-_" . stgit-undo)
b8463f1d
GH
810 ([?\C-c ?\C-/] . stgit-redo)
811 ("\C-c\C-_" . stgit-redo)
ce3b6130 812 ("B" . stgit-branch)
380a021f 813 ("\C-c\C-b" . stgit-rebase)
ce3b6130 814 ("t" . ,toggle-map)
d9b954c7 815 ("d" . ,diff-map)
ce3b6130 816 ("q" . stgit-quit)))))
56d81fe5
DK
817
818(defun stgit-mode ()
819 "Major mode for interacting with StGit.
fdf5e327
GH
820
821Start StGit using \\[stgit].
822
823Basic commands:
824\\<stgit-mode-map>\
825\\[stgit-help] Show this help text
826\\[stgit-quit] Hide the StGit buffer
827\\[describe-bindings] Show all key bindings
828
829\\[stgit-reload-or-repair] Reload the StGit buffer
830\\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
831
832\\[stgit-undo] Undo most recent StGit operation
833\\[stgit-redo] Undo recent undo
834
835\\[stgit-git-status] Run `git-status' (if available)
836
837Movement commands:
838\\[stgit-previous-line] Move to previous line
839\\[stgit-next-line] Move to next line
840\\[stgit-previous-patch] Move to previous patch
841\\[stgit-next-patch] Move to next patch
842
843\\[stgit-mark] Mark patch
844\\[stgit-unmark-up] Unmark patch and move up
845\\[stgit-unmark-down] Unmark patch and move down
846
847Commands for patches:
848\\[stgit-select] Toggle showing changed files in patch
849\\[stgit-refresh] Refresh patch with changes in index or work tree
850\\[stgit-diff] Show the patch log and diff
851
852\\[stgit-new] Create a new, empty patch
2acb7116 853\\[stgit-new-and-refresh] Create a new patch from index or work tree
fdf5e327
GH
854\\[stgit-rename] Rename patch
855\\[stgit-edit] Edit patch description
856\\[stgit-delete] Delete patch(es)
857
858\\[stgit-push-next] Push next patch onto stack
859\\[stgit-pop-next] Pop current patch from stack
860\\[stgit-push-or-pop] Push or pop patch at point
861\\[stgit-goto] Make current patch current by popping or pushing
862
863\\[stgit-squash] Squash (meld together) patches
864\\[stgit-move-patches] Move patch(es) to point
865
866\\[stgit-commit] Commit patch(es)
867\\[stgit-uncommit] Uncommit patch(es)
868
869Commands for files:
870\\[stgit-select] Open the file in this window
871\\[stgit-find-file-other-window] Open the file in another window
872\\[stgit-diff] Show the file's diff
873
874\\[stgit-file-toggle-index] Toggle change between index and work tree
875\\[stgit-revert-file] Revert changes to file
876
877Display commands:
878\\[stgit-toggle-worktree] Toggle showing index and work tree
879\\[stgit-toggle-unknown] Toggle showing unknown files
880\\[stgit-toggle-ignored] Toggle showing ignored files
881
882Commands for diffs:
883\\[stgit-diff] Show diff of patch or file
884\\[stgit-diff-base] Show diff against the merge base
885\\[stgit-diff-ours] Show diff against our branch
886\\[stgit-diff-theirs] Show diff against their branch
887
888 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
889ignore space changes.
890 With two prefix arguments (e.g., \\[universal-argument] \
891\\[universal-argument] \\[stgit-diff]), ignore all space changes.
892
893Commands for merge conflicts:
894\\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
895\\[stgit-resolve-file] Mark unmerged file as resolved
896
897Commands for branches:
898\\[stgit-branch] Switch to another branch
380a021f 899\\[stgit-rebase] Rebase the current branch
fdf5e327
GH
900
901Customization variables:
902`stgit-abbreviate-copies-and-renames'
903`stgit-default-show-worktree'
904`stgit-find-copies-harder'
905`stgit-show-worktree-mode'
906
907See also \\[customize-group] for the \"stgit\" group."
56d81fe5
DK
908 (kill-all-local-variables)
909 (buffer-disable-undo)
910 (setq mode-name "StGit"
911 major-mode 'stgit-mode
912 goal-column 2)
913 (use-local-map stgit-mode-map)
914 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 915 (set (make-local-variable 'stgit-marked-patches) nil)
6467d976 916 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
ce3b6130 917 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
918 (set (make-local-variable 'stgit-index-node) nil)
919 (set (make-local-variable 'stgit-worktree-node) nil)
224ef1ec 920 (set (make-local-variable 'parse-sexp-lookup-properties) t)
2870f8b8 921 (set-variable 'truncate-lines 't)
b894e680 922 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
923 (run-hooks 'stgit-mode-hook))
924
b894e680
DK
925(defun stgit-update-saved-file ()
926 (let* ((file (expand-file-name buffer-file-name))
927 (dir (file-name-directory file))
928 (gitdir (condition-case nil (git-get-top-dir dir)
929 (error nil)))
930 (buffer (and gitdir (stgit-find-buffer gitdir))))
931 (when buffer
932 (with-current-buffer buffer
210a2a52 933 (stgit-refresh-worktree)))))
b894e680 934
d51722b7
GH
935(defun stgit-add-mark (patchsym)
936 "Mark the patch PATCHSYM."
8036afdd 937 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 938
d51722b7
GH
939(defun stgit-remove-mark (patchsym)
940 "Unmark the patch PATCHSYM."
8036afdd 941 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 942
e6b1fdae 943(defun stgit-clear-marks ()
47271f41 944 "Unmark all patches."
e6b1fdae
DK
945 (setq stgit-marked-patches '()))
946
735cb7ec 947(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
948 (get-text-property (point) 'patch-data))
949
64ada6f5 950(defun stgit-patch-name-at-point (&optional cause-error only-patches)
d51722b7 951 "Return the patch name on the current line as a symbol.
64ada6f5
GH
952If CAUSE-ERROR is not nil, signal an error if none found.
953If ONLY-PATCHES is not nil, only allow real patches, and not
954index or work tree."
2c862b07 955 (let ((patch (stgit-patch-at-point)))
64ada6f5
GH
956 (and patch
957 only-patches
958 (memq (stgit-patch-status patch) '(work index))
959 (setq patch nil))
2c862b07
DK
960 (cond (patch
961 (stgit-patch-name patch))
962 (cause-error
963 (error "No patch on this line")))))
378a003d 964
3164eec6
DK
965(defun stgit-patched-file-at-point ()
966 (get-text-property (point) 'file-data))
56d81fe5 967
7755d7f1 968(defun stgit-patches-marked-or-at-point ()
d51722b7 969 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 970 (if stgit-marked-patches
d51722b7 971 stgit-marked-patches
2c862b07 972 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
973 (if patch
974 (list patch)
975 '()))))
976
a9089e68 977(defun stgit-goto-patch (patchsym &optional file)
d51722b7 978 "Move point to the line containing patch PATCHSYM.
a9089e68
GH
979If that patch cannot be found, do nothing.
980
981If the patch was found and FILE is not nil, instead move to that
982file's line. If FILE cannot be found, stay on the line of
983PATCHSYM."
f9b82d36
DK
984 (let ((node (ewoc-nth stgit-ewoc 0)))
985 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
986 patchsym)))
987 (setq node (ewoc-next stgit-ewoc node)))
a9089e68
GH
988 (when (and node file)
989 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
990 (file-node (ewoc-nth file-ewoc 0)))
991 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
992 (setq file-node (ewoc-next file-ewoc file-node)))
993 (when file-node
994 (ewoc-goto-node file-ewoc file-node)
995 (move-to-column (stgit-goal-column))
996 (setq node nil))))
f9b82d36
DK
997 (when node
998 (ewoc-goto-node stgit-ewoc node)
d51722b7 999 (move-to-column goal-column))))
56d81fe5 1000
1c2426dc 1001(defun stgit-init ()
a53347d9 1002 "Run stg init."
1c2426dc
DK
1003 (interactive)
1004 (stgit-capture-output nil
b0424080 1005 (stgit-run "init"))
1f0bf00f 1006 (stgit-reload))
1c2426dc 1007
6df83d42 1008(defun stgit-mark ()
a53347d9 1009 "Mark the patch under point."
6df83d42 1010 (interactive)
8036afdd 1011 (let* ((node (ewoc-locate stgit-ewoc))
64ada6f5
GH
1012 (patch (ewoc-data node))
1013 (name (stgit-patch-name patch)))
1014 (when (eq name :work)
1015 (error "Cannot mark the work tree"))
1016 (when (eq name :index)
1017 (error "Cannot mark the index"))
8036afdd
DK
1018 (stgit-add-mark (stgit-patch-name patch))
1019 (ewoc-invalidate stgit-ewoc node))
378a003d 1020 (stgit-next-patch))
6df83d42 1021
9b151b27 1022(defun stgit-unmark-up ()
a53347d9 1023 "Remove mark from the patch on the previous line."
6df83d42 1024 (interactive)
378a003d 1025 (stgit-previous-patch)
8036afdd
DK
1026 (let* ((node (ewoc-locate stgit-ewoc))
1027 (patch (ewoc-data node)))
1028 (stgit-remove-mark (stgit-patch-name patch))
1029 (ewoc-invalidate stgit-ewoc node))
1030 (move-to-column (stgit-goal-column)))
9b151b27
GH
1031
1032(defun stgit-unmark-down ()
a53347d9 1033 "Remove mark from the patch on the current line."
9b151b27 1034 (interactive)
8036afdd
DK
1035 (let* ((node (ewoc-locate stgit-ewoc))
1036 (patch (ewoc-data node)))
1037 (stgit-remove-mark (stgit-patch-name patch))
1038 (ewoc-invalidate stgit-ewoc node))
1288eda2 1039 (stgit-next-patch))
6df83d42 1040
56d81fe5 1041(defun stgit-rename (name)
018fa1ac 1042 "Rename the patch under point to NAME."
64ada6f5
GH
1043 (interactive (list
1044 (read-string "Patch name: "
1045 (symbol-name (stgit-patch-name-at-point t t)))))
1046 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 1047 (stgit-capture-output nil
d51722b7
GH
1048 (stgit-run "rename" old-patchsym name))
1049 (let ((name-sym (intern name)))
1050 (when (memq old-patchsym stgit-expanded-patches)
378a003d 1051 (setq stgit-expanded-patches
6a73154a 1052 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
d51722b7 1053 (when (memq old-patchsym stgit-marked-patches)
378a003d 1054 (setq stgit-marked-patches
6a73154a 1055 (cons name-sym (delq old-patchsym stgit-marked-patches))))
d51722b7
GH
1056 (stgit-reload)
1057 (stgit-goto-patch name-sym))))
56d81fe5 1058
408fa7cb
GH
1059(defun stgit-reload-or-repair (repair)
1060 "Update the contents of the StGit buffer (`stgit-reload').
1061
1062With a prefix argument, repair the StGit metadata if the branch
1063was modified with git commands (`stgit-repair')."
1064 (interactive "P")
1065 (if repair
1066 (stgit-repair)
1067 (stgit-reload)))
1068
26201d96 1069(defun stgit-repair ()
a53347d9 1070 "Run stg repair."
26201d96
DK
1071 (interactive)
1072 (stgit-capture-output nil
b0424080 1073 (stgit-run "repair"))
1f0bf00f 1074 (stgit-reload))
26201d96 1075
adeef6bc
GH
1076(defun stgit-available-branches ()
1077 "Returns a list of the available stg branches"
1078 (let ((output (with-output-to-string
1079 (stgit-run "branch" "--list")))
1080 (start 0)
1081 result)
1082 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
1083 (setq result (cons (match-string 1 output) result))
1084 (setq start (match-end 0)))
1085 result))
1086
1087(defun stgit-branch (branch)
1088 "Switch to branch BRANCH."
1089 (interactive (list (completing-read "Switch to branch: "
1090 (stgit-available-branches))))
1091 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1092 (stgit-reload))
1093
380a021f
GH
1094(defun stgit-available-refs (&optional omit-stgit)
1095 "Returns a list of the available git refs.
1096If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1097 (let* ((output (with-output-to-string
1098 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1099 "refs/tags" "refs/heads"
1100 "refs/remotes")))
1101 (result (split-string output "\n" t)))
1102 (mapcar (lambda (s)
1103 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1104 (substring s (match-end 0))
1105 s))
1106 (if omit-stgit
1107 (delete-if (lambda (s)
1108 (string-match "^refs/heads/.*\\.stgit$" s))
1109 result)
1110 result))))
1111
1112(defun stgit-rebase (new-base)
1113 "Rebase to NEW-BASE."
1114 (interactive (list (completing-read "Rebase to: "
1115 (stgit-available-refs t))))
1116 (stgit-capture-output nil (stgit-run "rebase" new-base))
1117 (stgit-reload))
1118
41c1c59c
GH
1119(defun stgit-commit (count)
1120 "Run stg commit on COUNT commits.
e552cb5f
GH
1121Interactively, the prefix argument is used as COUNT.
1122A negative COUNT will uncommit instead."
41c1c59c 1123 (interactive "p")
e552cb5f
GH
1124 (if (< count 0)
1125 (stgit-uncommit (- count))
1126 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1127 (stgit-reload)))
1128
1129(defun stgit-uncommit (count)
1130 "Run stg uncommit on COUNT commits.
1131Interactively, the prefix argument is used as COUNT.
1132A negative COUNT will commit instead."
1133 (interactive "p")
1134 (if (< count 0)
1135 (stgit-commit (- count))
1136 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1137 (stgit-reload)))
c4aad9a7 1138
556345d3
GH
1139(defun stgit-neighbour-file ()
1140 "Return the file name of the next file after point, or the
1141previous file if point is at the last file within a patch."
1142 (let ((old-point (point))
1143 neighbour-file)
1144 (and (zerop (forward-line 1))
1145 (let ((f (stgit-patched-file-at-point)))
1146 (and f (setq neighbour-file (stgit-file-file f)))))
1147 (goto-char old-point)
1148 (unless neighbour-file
1149 (and (zerop (forward-line -1))
1150 (let ((f (stgit-patched-file-at-point)))
1151 (and f (setq neighbour-file (stgit-file-file f)))))
1152 (goto-char old-point))
1153 neighbour-file))
1154
3959a095
GH
1155(defun stgit-revert-file ()
1156 "Revert the file at point, which must be in the index or the
1157working tree."
1158 (interactive)
1159 (let* ((patched-file (or (stgit-patched-file-at-point)
1160 (error "No file on the current line")))
1161 (patch-name (stgit-patch-name-at-point))
1162 (file-status (stgit-file-status patched-file))
1163 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
1164 (stgit-file-cr-to patched-file))
1165 ((eq file-status 'add)
1166 (stgit-file-file patched-file))))
1167 (co-file (cond ((eq file-status 'rename)
1168 (stgit-file-cr-from patched-file))
1169 ((not (memq file-status '(copy add)))
556345d3
GH
1170 (stgit-file-file patched-file))))
1171 (next-file (stgit-neighbour-file)))
3959a095
GH
1172
1173 (unless (memq patch-name '(:work :index))
1174 (error "No index or working tree file on this line"))
1175
d9473917
GH
1176 (when (eq file-status 'ignore)
1177 (error "Cannot revert ignored files"))
1178
1179 (when (eq file-status 'unknown)
1180 (error "Cannot revert unknown files"))
1181
3959a095
GH
1182 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1183 (when (yes-or-no-p (format "Revert %d file%s? "
1184 nfiles
1185 (if (= nfiles 1) "" "s")))
1186 (stgit-capture-output nil
1187 (when rm-file
1188 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1189 (when co-file
1190 (stgit-run-git "checkout" "HEAD" co-file)))
556345d3
GH
1191 (stgit-reload)
1192 (stgit-goto-patch patch-name next-file)))))
3959a095 1193
51783171
GH
1194(defun stgit-resolve-file ()
1195 "Resolve conflict in the file at point."
1196 (interactive)
1197 (let* ((patched-file (stgit-patched-file-at-point))
1198 (patch (stgit-patch-at-point))
1199 (patch-name (and patch (stgit-patch-name patch)))
1200 (status (and patched-file (stgit-file-status patched-file))))
1201
1202 (unless (memq patch-name '(:work :index))
1203 (error "No index or working tree file on this line"))
1204
1205 (unless (eq status 'unmerged)
1206 (error "No conflict to resolve at the current line"))
1207
1208 (stgit-capture-output nil
1209 (stgit-move-change-to-index (stgit-file-file patched-file)))
1210
1211 (stgit-reload)))
1212
0b661144
DK
1213(defun stgit-push-next (npatches)
1214 "Push the first unapplied patch.
1215With numeric prefix argument, push that many patches."
1216 (interactive "p")
d51722b7 1217 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
1218 (stgit-reload)
1219 (stgit-refresh-git-status))
56d81fe5 1220
0b661144
DK
1221(defun stgit-pop-next (npatches)
1222 "Pop the topmost applied patch.
1223With numeric prefix argument, pop that many patches."
1224 (interactive "p")
d51722b7 1225 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
1226 (stgit-reload)
1227 (stgit-refresh-git-status))
56d81fe5 1228
5fd965bb
GH
1229(defun stgit-applied-at-point-p ()
1230 "Return non-nil if the patch at point is applied."
1231 (let ((patch (stgit-patch-at-point t)))
1232 (not (eq (stgit-patch-status patch) 'unapplied))))
f9182fca
KH
1233
1234(defun stgit-push-or-pop ()
a53347d9 1235 "Push or pop the patch on the current line."
f9182fca 1236 (interactive)
5fd965bb
GH
1237 (let ((patchsym (stgit-patch-name-at-point t t))
1238 (applied (stgit-applied-at-point-p)))
f9182fca 1239 (stgit-capture-output nil
d51722b7 1240 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 1241 (stgit-reload)))
f9182fca 1242
c7adf5ef 1243(defun stgit-goto ()
a53347d9 1244 "Go to the patch on the current line."
c7adf5ef 1245 (interactive)
2c862b07 1246 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 1247 (stgit-capture-output nil
d51722b7 1248 (stgit-run "goto" patchsym))
1f0bf00f 1249 (stgit-reload)))
c7adf5ef 1250
d51722b7 1251(defun stgit-id (patchsym)
50d88c67
DK
1252 "Return the git commit id for PATCHSYM.
1253If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1254 (if (keywordp patchsym)
1255 patchsym
1256 (let ((result (with-output-to-string
1257 (stgit-run-silent "id" patchsym))))
1258 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1259 (error "Cannot find commit id for %s" patchsym))
1260 (match-string 1 result))))
378a003d 1261
1aece5c0 1262(defun stgit-show-patch (unmerged-stage ignore-whitespace)
d9b954c7
GH
1263 "Show the patch on the current line.
1264
1265UNMERGED-STAGE is the argument to `git-diff' that that selects
1266which stage to diff against in the case of unmerged files."
1aece5c0
GH
1267 (let ((space-arg (when (numberp ignore-whitespace)
1268 (cond ((> ignore-whitespace 4)
1269 "--ignore-all-space")
1270 ((> ignore-whitespace 1)
1271 "--ignore-space-change"))))
1272 (patch-name (stgit-patch-name-at-point t)))
1273 (stgit-capture-output "*StGit patch*"
1274 (case (get-text-property (point) 'entry-type)
1275 ('file
1276 (let* ((patched-file (stgit-patched-file-at-point))
1277 (patch-id (let ((id (stgit-id patch-name)))
1278 (if (and (eq id :index)
1279 (eq (stgit-file-status patched-file)
1280 'unmerged))
1281 :work
1282 id)))
1283 (args (append (and space-arg (list space-arg))
1284 (and (stgit-file-cr-from patched-file)
1285 (list (stgit-find-copies-harder-diff-arg)))
1286 (cond ((eq patch-id :index)
1287 '("--cached"))
1288 ((eq patch-id :work)
1289 (list unmerged-stage))
1290 (t
1291 (list (concat patch-id "^") patch-id)))
1292 '("--")
3164eec6
DK
1293 (if (stgit-file-copy-or-rename patched-file)
1294 (list (stgit-file-cr-from patched-file)
1295 (stgit-file-cr-to patched-file))
1296 (list (stgit-file-file patched-file))))))
1aece5c0
GH
1297 (apply 'stgit-run-git "diff" args)))
1298 ('patch
1299 (let* ((patch-id (stgit-id patch-name)))
1300 (if (or (eq patch-id :index) (eq patch-id :work))
1301 (apply 'stgit-run-git "diff"
1302 (stgit-find-copies-harder-diff-arg)
1303 (append (and space-arg (list space-arg))
1304 (if (eq patch-id :index)
1305 '("--cached")
1306 (list unmerged-stage))))
1307 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1308 (and space-arg (list "-O" space-arg))
1309 (list (stgit-patch-name-at-point)))))
1310 (apply 'stgit-run args)))))
6a73154a
GH
1311 (t
1312 (error "No patch or file at point")))
1aece5c0
GH
1313 (with-current-buffer standard-output
1314 (goto-char (point-min))
1315 (diff-mode)))))
1316
1317(defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1318 `(defun ,name (&optional ignore-whitespace)
1319 ,(format "Show the patch on the current line.
1320
1321%sWith a prefix argument, ignore whitespace. With a prefix argument
1322greater than four (e.g., \\[universal-argument] \
1323\\[universal-argument] \\[%s]), ignore all whitespace."
1324 (if unmerged-action
1325 (format "For unmerged files, %s.\n\n" unmerged-action)
1326 "")
1327 name)
1328 (interactive "p")
1329 (stgit-show-patch ,diff-arg ignore-whitespace)))
1330
1331(stgit-define-diff stgit-diff
1332 "--ours" nil)
1333(stgit-define-diff stgit-diff-ours
1334 "--ours"
1335 "diff against our branch")
1336(stgit-define-diff stgit-diff-theirs
1337 "--theirs"
1338 "diff against their branch")
1339(stgit-define-diff stgit-diff-base
1340 "--base"
1341 "diff against the merge base")
1342(stgit-define-diff stgit-diff-combined
1343 "--cc"
1344 "show a combined diff")
d9b954c7 1345
f87c2e22
GH
1346(defun stgit-move-change-to-index (file &optional force)
1347 "Copies the work tree state of FILE to index, using git add or git rm.
1348
1349If FORCE is not nil, use --force."
306b37a6
GH
1350 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1351 '("add") '("rm" "-q"))))
37cb5766 1352 (stgit-capture-output "*git output*"
f87c2e22
GH
1353 (apply 'stgit-run-git (append op (and force '("--force"))
1354 '("--") (list file))))))
37cb5766 1355
fd9fe574 1356(defun stgit-remove-change-from-index (file)
37cb5766
DK
1357 "Unstages the change in FILE from the index"
1358 (stgit-capture-output "*git output*"
1359 (stgit-run-git "reset" "-q" "--" file)))
1360
1361(defun stgit-file-toggle-index ()
a9089e68
GH
1362 "Move modified file in or out of the index.
1363
1364Leaves the point where it is, but moves the mark to where the
1365file ended up. You can then jump to the file with \
1366\\[exchange-point-and-mark]."
37cb5766 1367 (interactive)
612f999a
GH
1368 (let* ((patched-file (or (stgit-patched-file-at-point)
1369 (error "No file on the current line")))
1370 (patched-status (stgit-file-status patched-file)))
1371 (when (eq patched-status 'unmerged)
51783171 1372 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
a9089e68
GH
1373 (let* ((patch (stgit-patch-at-point))
1374 (patch-name (stgit-patch-name patch))
612f999a
GH
1375 (mark-file (if (eq patched-status 'rename)
1376 (stgit-file-cr-to patched-file)
1377 (stgit-file-file patched-file)))
1378 (point-file (if (eq patched-status 'rename)
6a73154a
GH
1379 (stgit-file-cr-from patched-file)
1380 (stgit-neighbour-file))))
a9089e68 1381
37cb5766 1382 (cond ((eq patch-name :work)
f87c2e22
GH
1383 (stgit-move-change-to-index (stgit-file-file patched-file)
1384 (eq patched-status 'ignore)))
37cb5766 1385 ((eq patch-name :index)
fd9fe574 1386 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766 1387 (t
612f999a 1388 (error "Can only move files between working tree and index")))
a9089e68
GH
1389 (stgit-refresh-worktree)
1390 (stgit-refresh-index)
612f999a 1391 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
a9089e68 1392 (push-mark nil t t)
612f999a 1393 (stgit-goto-patch patch-name point-file))))
37cb5766 1394
0bca35c8 1395(defun stgit-edit ()
a53347d9 1396 "Edit the patch on the current line."
0bca35c8 1397 (interactive)
64ada6f5 1398 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 1399 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
1400 (dir default-directory))
1401 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 1402 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
1403 (setq default-directory dir)
1404 (let ((standard-output edit-buf))
d51722b7 1405 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
1406
1407(defun stgit-confirm-edit ()
1408 (interactive)
1409 (let ((file (make-temp-file "stgit-edit-")))
1410 (write-region (point-min) (point-max) file)
1411 (stgit-capture-output nil
d51722b7 1412 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 1413 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1414 (stgit-reload))))
0bca35c8 1415
2acb7116 1416(defun stgit-new (add-sign &optional refresh)
aa04f831
GH
1417 "Create a new patch.
1418With a prefix argument, include a \"Signed-off-by:\" line at the
1419end of the patch."
1420 (interactive "P")
c5d45b92
GH
1421 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1422 (dir default-directory))
1423 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831 1424 (setq default-directory dir)
2acb7116 1425 (set (make-local-variable 'stgit-refresh-after-new) refresh)
aa04f831
GH
1426 (when add-sign
1427 (save-excursion
1428 (let ((standard-output (current-buffer)))
1429 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
1430
1431(defun stgit-confirm-new ()
1432 (interactive)
2acb7116
DK
1433 (let ((file (make-temp-file "stgit-edit-"))
1434 (refresh stgit-refresh-after-new))
64c097a0
DK
1435 (write-region (point-min) (point-max) file)
1436 (stgit-capture-output nil
27b0f9e4 1437 (stgit-run "new" "-f" file))
64c097a0 1438 (with-current-buffer log-edit-parent-buffer
2acb7116
DK
1439 (if refresh
1440 (stgit-refresh)
1441 (stgit-reload)))))
1442
1443(defun stgit-new-and-refresh (add-sign)
1444 "Create a new patch and refresh it with the current changes.
1445
1446With a prefix argument, include a \"Signed-off-by:\" line at the
1447end of the patch.
1448
1449This works just like running `stgit-new' followed by `stgit-refresh'."
1450 (interactive "P")
1451 (stgit-new add-sign t))
64c097a0
DK
1452
1453(defun stgit-create-patch-name (description)
1454 "Create a patch name from a long description"
1455 (let ((patch ""))
1456 (while (> (length description) 0)
1457 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
1458 (setq patch (downcase (concat patch
1459 (match-string 0 description))))
64c097a0
DK
1460 (setq description (substring description (match-end 0))))
1461 ((string-match "\\` +" description)
1462 (setq patch (concat patch "-"))
1463 (setq description (substring description (match-end 0))))
1464 ((string-match "\\`[^a-zA-Z_-]+" description)
1465 (setq description (substring description (match-end 0))))))
1466 (cond ((= (length patch) 0)
1467 "patch")
1468 ((> (length patch) 20)
1469 (substring patch 0 20))
1470 (t patch))))
0bca35c8 1471
9008e45b 1472(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 1473 "Delete the patches in PATCHSYMS.
9008e45b
GH
1474Interactively, delete the marked patches, or the patch at point.
1475
1476With a prefix argument, or SPILL-P, spill the patch contents to
1477the work tree and index."
1478 (interactive (list (stgit-patches-marked-or-at-point)
1479 current-prefix-arg))
e7231e4f
GH
1480 (unless patchsyms
1481 (error "No patches to delete"))
64ada6f5
GH
1482 (when (memq :index patchsyms)
1483 (error "Cannot delete the index"))
1484 (when (memq :work patchsyms)
1485 (error "Cannot delete the work tree"))
1486
d51722b7 1487 (let ((npatches (length patchsyms)))
9008e45b 1488 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 1489 npatches
9008e45b
GH
1490 (if (= 1 npatches) "" "es")
1491 (if spill-p
1492 " (spilling contents to index)"
1493 "")))
1494 (let ((args (if spill-p
1495 (cons "--spill" patchsyms)
1496 patchsyms)))
1497 (stgit-capture-output nil
1498 (apply 'stgit-run "delete" args))
1499 (stgit-reload)))))
d51722b7 1500
7cc45294
GH
1501(defun stgit-move-patches-target ()
1502 "Return the patchsym indicating a target patch for
1503`stgit-move-patches'.
1504
1505This is either the patch at point, or one of :top and :bottom, if
1506the point is after or before the applied patches."
1507
980d8cfb 1508 (let ((patchsym (stgit-patch-name-at-point nil t)))
7cc45294
GH
1509 (cond (patchsym patchsym)
1510 ((save-excursion (re-search-backward "^>" nil t)) :top)
1511 (t :bottom))))
1512
95369f6c
GH
1513(defun stgit-sort-patches (patchsyms)
1514 "Returns the list of patches in PATCHSYMS sorted according to
1515their position in the patch series, bottommost first.
1516
2d7bcbd9 1517PATCHSYMS must not contain duplicate entries."
95369f6c
GH
1518 (let (sorted-patchsyms
1519 (series (with-output-to-string
1520 (with-current-buffer standard-output
1521 (stgit-run-silent "series" "--noprefix"))))
1522 start)
1523 (while (string-match "^\\(.+\\)" series start)
1524 (let ((patchsym (intern (match-string 1 series))))
1525 (when (memq patchsym patchsyms)
1526 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1527 (setq start (match-end 0)))
1528 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1529
1530 (unless (= (length patchsyms) (length sorted-patchsyms))
1531 (error "Internal error"))
1532
1533 sorted-patchsyms))
1534
7cc45294
GH
1535(defun stgit-move-patches (patchsyms target-patch)
1536 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1537If TARGET-PATCH is :bottom or :top, move the patches to the
1538bottom or top of the stack, respectively.
1539
1540Interactively, move the marked patches to where the point is."
1541 (interactive (list stgit-marked-patches
1542 (stgit-move-patches-target)))
1543 (unless patchsyms
1544 (error "Need at least one patch to move"))
1545
1546 (unless target-patch
1547 (error "Point not at a patch"))
1548
1549 (if (eq target-patch :top)
1550 (stgit-capture-output nil
1551 (apply 'stgit-run "float" patchsyms))
1552
1553 ;; need to have patchsyms sorted by position in the stack
95369f6c 1554 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1555 (while sorted-patchsyms
1556 (setq sorted-patchsyms
1557 (and (stgit-capture-output nil
1558 (if (eq target-patch :bottom)
1559 (stgit-run "sink" "--" (car sorted-patchsyms))
1560 (stgit-run "sink" "--to" target-patch "--"
1561 (car sorted-patchsyms))))
1562 (cdr sorted-patchsyms))))))
1563 (stgit-reload))
1564
594aa463
KH
1565(defun stgit-squash (patchsyms)
1566 "Squash the patches in PATCHSYMS.
693d179b
GH
1567Interactively, squash the marked patches.
1568
1569Unless there are any conflicts, the patches will be merged into
1570one patch, which will occupy the same spot in the series as the
1571deepest patch had before the squash."
d51722b7
GH
1572 (interactive (list stgit-marked-patches))
1573 (when (< (length patchsyms) 2)
594aa463 1574 (error "Need at least two patches to squash"))
32d7545d
GH
1575 (let ((stgit-buffer (current-buffer))
1576 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1577 (dir default-directory)
1578 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1579 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1580 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1581 (setq default-directory dir)
32d7545d
GH
1582 (let ((result (let ((standard-output edit-buf))
1583 (apply 'stgit-run-silent "squash"
1584 "--save-template=-" sorted-patchsyms))))
1585
1586 ;; stg squash may have reordered the patches or caused conflicts
1587 (with-current-buffer stgit-buffer
1588 (stgit-reload))
1589
1590 (unless (eq 0 result)
1591 (fundamental-mode)
1592 (rename-buffer "*StGit error*")
1593 (resize-temp-buffer-window)
1594 (switch-to-buffer-other-window stgit-buffer)
1595 (error "stg squash failed")))))
ea0def18 1596
594aa463 1597(defun stgit-confirm-squash ()
ea0def18
DK
1598 (interactive)
1599 (let ((file (make-temp-file "stgit-edit-")))
1600 (write-region (point-min) (point-max) file)
1601 (stgit-capture-output nil
594aa463 1602 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1603 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1604 (stgit-clear-marks)
1605 ;; Go to first marked patch and stay there
1606 (goto-char (point-min))
1607 (re-search-forward (concat "^[>+-]\\*") nil t)
1608 (move-to-column goal-column)
1609 (let ((pos (point)))
1f0bf00f 1610 (stgit-reload)
e6b1fdae 1611 (goto-char pos)))))
ea0def18 1612
0663524d
KH
1613(defun stgit-help ()
1614 "Display help for the StGit mode."
1615 (interactive)
1616 (describe-function 'stgit-mode))
3a59f3db 1617
83e51dbf
DK
1618(defun stgit-undo (&optional arg)
1619 "Run stg undo.
b8463f1d
GH
1620With prefix argument, run it with the --hard flag.
1621
1622See also `stgit-redo'."
83e51dbf
DK
1623 (interactive "P")
1624 (stgit-capture-output nil
1625 (if arg
1626 (stgit-run "undo" "--hard")
1627 (stgit-run "undo")))
1f0bf00f 1628 (stgit-reload))
83e51dbf 1629
b8463f1d
GH
1630(defun stgit-redo (&optional arg)
1631 "Run stg redo.
1632With prefix argument, run it with the --hard flag.
1633
1634See also `stgit-undo'."
1635 (interactive "P")
1636 (stgit-capture-output nil
1637 (if arg
1638 (stgit-run "redo" "--hard")
1639 (stgit-run "redo")))
1640 (stgit-reload))
1641
4d73c4d8
DK
1642(defun stgit-refresh (&optional arg)
1643 "Run stg refresh.
36a4eacd
GH
1644If the index contains any changes, only refresh from index.
1645
a53347d9 1646With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8
DK
1647 (interactive "P")
1648 (let ((patchargs (if arg
b0424080
GH
1649 (let ((patches (stgit-patches-marked-or-at-point)))
1650 (cond ((null patches)
df283a8b 1651 (error "No patch to update"))
b0424080 1652 ((> (length patches) 1)
df283a8b 1653 (error "Too many patches selected"))
b0424080
GH
1654 (t
1655 (cons "-p" patches))))
1656 nil)))
36a4eacd
GH
1657 (unless (stgit-index-empty-p)
1658 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 1659 (stgit-capture-output nil
074a4fb0
GH
1660 (apply 'stgit-run "refresh" patchargs))
1661 (stgit-refresh-git-status))
4d73c4d8
DK
1662 (stgit-reload))
1663
ce3b6130 1664(defvar stgit-show-worktree nil
8f702de4 1665 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 1666
8f702de4 1667See also `stgit-show-worktree-mode'.")
ce3b6130 1668
d9473917
GH
1669(defvar stgit-show-ignored nil
1670 "If nil, inhibit showing files ignored by git.")
1671
1672(defvar stgit-show-unknown nil
1673 "If nil, inhibit showing files not registered with git.")
1674
ce3b6130
DK
1675(defun stgit-toggle-worktree (&optional arg)
1676 "Toggle the visibility of the work tree.
2d7bcbd9 1677With ARG, show the work tree if ARG is positive.
ce3b6130 1678
8f702de4
GH
1679Its initial setting is controlled by `stgit-default-show-worktree'.
1680
1681`stgit-show-worktree-mode' controls where on screen the index and
1682work tree will show up."
ce3b6130
DK
1683 (interactive)
1684 (setq stgit-show-worktree
1685 (if (numberp arg)
1686 (> arg 0)
1687 (not stgit-show-worktree)))
1688 (stgit-reload))
1689
d9473917
GH
1690(defun stgit-toggle-ignored (&optional arg)
1691 "Toggle the visibility of files ignored by git in the work
1692tree. With ARG, show these files if ARG is positive.
1693
1694Use \\[stgit-toggle-worktree] to show the work tree."
1695 (interactive)
1696 (setq stgit-show-ignored
1697 (if (numberp arg)
1698 (> arg 0)
1699 (not stgit-show-ignored)))
1700 (stgit-reload))
1701
1702(defun stgit-toggle-unknown (&optional arg)
1703 "Toggle the visibility of files not registered with git in the
1704work tree. With ARG, show these files if ARG is positive.
1705
1706Use \\[stgit-toggle-worktree] to show the work tree."
1707 (interactive)
1708 (setq stgit-show-unknown
1709 (if (numberp arg)
1710 (> arg 0)
1711 (not stgit-show-unknown)))
1712 (stgit-reload))
1713
3a59f3db 1714(provide 'stgit)