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