stgit.el: Bugfix listing copied/renamed files
[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))))))
807e1273
GH
565 (goto-char (match-end 0))
566 (ewoc-enter-last ewoc file))))
567
3164eec6 568 (unless (ewoc-nth ewoc 0)
000f337c
GH
569 (ewoc-set-hf ewoc ""
570 (concat " "
571 (propertize "<no files>"
572 'face 'stgit-description-face)
573 "\n"))))
0434bec1 574 (goto-char end)))
07f464e0 575
030f0535
GH
576(defun stgit-find-file (&optional other-window)
577 (let* ((file (or (stgit-patched-file-at-point)
578 (error "No file at point")))
579 (filename (expand-file-name (stgit-file-file file))))
0de6881a
DK
580 (unless (file-exists-p filename)
581 (error "File does not exist"))
030f0535
GH
582 (funcall (if other-window 'find-file-other-window 'find-file)
583 filename)
584 (when (eq (stgit-file-status file) 'unmerged)
585 (smerge-mode 1))))
acc5652f 586
50d88c67 587(defun stgit-select-patch ()
98230edd
DK
588 (let ((patchname (stgit-patch-name-at-point)))
589 (if (memq patchname stgit-expanded-patches)
590 (setq stgit-expanded-patches (delq patchname stgit-expanded-patches))
591 (setq stgit-expanded-patches (cons patchname stgit-expanded-patches)))
592 (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc)))
593 (move-to-column (stgit-goal-column)))
acc5652f 594
378a003d 595(defun stgit-select ()
da01a29b
GH
596 "With point on a patch, toggle showing files in the patch.
597
598With point on a file, open the associated file. Opens the target
599file for (applied) copies and renames."
378a003d 600 (interactive)
50d88c67
DK
601 (case (get-text-property (point) 'entry-type)
602 ('patch
603 (stgit-select-patch))
604 ('file
030f0535 605 (stgit-find-file))
50d88c67
DK
606 (t
607 (error "No patch or file on line"))))
378a003d
GH
608
609(defun stgit-find-file-other-window ()
610 "Open file at point in other window"
611 (interactive)
030f0535 612 (stgit-find-file t))
378a003d 613
d9b954c7
GH
614(defun stgit-find-file-merge ()
615 "Open file at point and merge it using `smerge-ediff'."
616 (interactive)
617 (stgit-find-file t)
618 (smerge-ediff))
619
83327d53 620(defun stgit-quit ()
a53347d9 621 "Hide the stgit buffer."
83327d53
GH
622 (interactive)
623 (bury-buffer))
624
0f076fe6 625(defun stgit-git-status ()
a53347d9 626 "Show status using `git-status'."
0f076fe6
GH
627 (interactive)
628 (unless (fboundp 'git-status)
df283a8b 629 (error "The stgit-git-status command requires git-status"))
0f076fe6
GH
630 (let ((dir default-directory))
631 (save-selected-window
632 (pop-to-buffer nil)
633 (git-status dir))))
634
58f72f16
GH
635(defun stgit-goal-column ()
636 "Return goal column for the current line"
50d88c67
DK
637 (case (get-text-property (point) 'entry-type)
638 ('patch 2)
639 ('file 4)
640 (t 0)))
58f72f16
GH
641
642(defun stgit-next-line (&optional arg)
378a003d 643 "Move cursor vertically down ARG lines"
58f72f16
GH
644 (interactive "p")
645 (next-line arg)
646 (move-to-column (stgit-goal-column)))
378a003d 647
58f72f16 648(defun stgit-previous-line (&optional arg)
378a003d 649 "Move cursor vertically up ARG lines"
58f72f16
GH
650 (interactive "p")
651 (previous-line arg)
652 (move-to-column (stgit-goal-column)))
378a003d
GH
653
654(defun stgit-next-patch (&optional arg)
98230edd 655 "Move cursor down ARG patches."
378a003d 656 (interactive "p")
98230edd
DK
657 (ewoc-goto-next stgit-ewoc (or arg 1))
658 (move-to-column goal-column))
378a003d
GH
659
660(defun stgit-previous-patch (&optional arg)
98230edd 661 "Move cursor up ARG patches."
378a003d 662 (interactive "p")
98230edd
DK
663 (ewoc-goto-prev stgit-ewoc (or arg 1))
664 (move-to-column goal-column))
378a003d 665
56d81fe5
DK
666(defvar stgit-mode-hook nil
667 "Run after `stgit-mode' is setup.")
668
669(defvar stgit-mode-map nil
670 "Keymap for StGit major mode.")
671
672(unless stgit-mode-map
d9b954c7
GH
673 (let ((diff-map (make-keymap))
674 (toggle-map (make-keymap)))
675 (suppress-keymap diff-map)
676 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
677 '(("b" . stgit-diff-base)
678 ("c" . stgit-diff-combined)
679 ("m" . stgit-find-file-merge)
680 ("o" . stgit-diff-ours)
681 ("t" . stgit-diff-theirs)))
ce3b6130
DK
682 (suppress-keymap toggle-map)
683 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
d9473917
GH
684 '(("t" . stgit-toggle-worktree)
685 ("i" . stgit-toggle-ignored)
686 ("u" . stgit-toggle-unknown)))
ce3b6130
DK
687 (setq stgit-mode-map (make-keymap))
688 (suppress-keymap stgit-mode-map)
689 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
690 `((" " . stgit-mark)
691 ("m" . stgit-mark)
692 ("\d" . stgit-unmark-up)
693 ("u" . stgit-unmark-down)
694 ("?" . stgit-help)
695 ("h" . stgit-help)
696 ("\C-p" . stgit-previous-line)
697 ("\C-n" . stgit-next-line)
698 ([up] . stgit-previous-line)
699 ([down] . stgit-next-line)
700 ("p" . stgit-previous-patch)
701 ("n" . stgit-next-patch)
702 ("\M-{" . stgit-previous-patch)
703 ("\M-}" . stgit-next-patch)
704 ("s" . stgit-git-status)
408fa7cb 705 ("g" . stgit-reload-or-repair)
ce3b6130
DK
706 ("r" . stgit-refresh)
707 ("\C-c\C-r" . stgit-rename)
708 ("e" . stgit-edit)
709 ("M" . stgit-move-patches)
710 ("S" . stgit-squash)
711 ("N" . stgit-new)
e9fdd4ea
GH
712 ("\C-c\C-c" . stgit-commit)
713 ("\C-c\C-u" . stgit-uncommit)
3959a095 714 ("U" . stgit-revert-file)
51783171 715 ("R" . stgit-resolve-file)
ce3b6130
DK
716 ("\r" . stgit-select)
717 ("o" . stgit-find-file-other-window)
718 ("i" . stgit-file-toggle-index)
719 (">" . stgit-push-next)
720 ("<" . stgit-pop-next)
721 ("P" . stgit-push-or-pop)
722 ("G" . stgit-goto)
d9b954c7 723 ("=" . stgit-diff)
ce3b6130 724 ("D" . stgit-delete)
b8463f1d 725 ([?\C-/] . stgit-undo)
ce3b6130 726 ("\C-_" . stgit-undo)
b8463f1d
GH
727 ([?\C-c ?\C-/] . stgit-redo)
728 ("\C-c\C-_" . stgit-redo)
ce3b6130
DK
729 ("B" . stgit-branch)
730 ("t" . ,toggle-map)
d9b954c7 731 ("d" . ,diff-map)
ce3b6130 732 ("q" . stgit-quit)))))
56d81fe5
DK
733
734(defun stgit-mode ()
735 "Major mode for interacting with StGit.
736Commands:
737\\{stgit-mode-map}"
738 (kill-all-local-variables)
739 (buffer-disable-undo)
740 (setq mode-name "StGit"
741 major-mode 'stgit-mode
742 goal-column 2)
743 (use-local-map stgit-mode-map)
744 (set (make-local-variable 'list-buffers-directory) default-directory)
6df83d42 745 (set (make-local-variable 'stgit-marked-patches) nil)
6467d976 746 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
ce3b6130 747 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
2ecb05c8
GH
748 (set (make-local-variable 'stgit-index-node) nil)
749 (set (make-local-variable 'stgit-worktree-node) nil)
224ef1ec 750 (set (make-local-variable 'parse-sexp-lookup-properties) t)
2870f8b8 751 (set-variable 'truncate-lines 't)
b894e680 752 (add-hook 'after-save-hook 'stgit-update-saved-file)
56d81fe5
DK
753 (run-hooks 'stgit-mode-hook))
754
b894e680
DK
755(defun stgit-update-saved-file ()
756 (let* ((file (expand-file-name buffer-file-name))
757 (dir (file-name-directory file))
758 (gitdir (condition-case nil (git-get-top-dir dir)
759 (error nil)))
760 (buffer (and gitdir (stgit-find-buffer gitdir))))
761 (when buffer
762 (with-current-buffer buffer
210a2a52 763 (stgit-refresh-worktree)))))
b894e680 764
d51722b7
GH
765(defun stgit-add-mark (patchsym)
766 "Mark the patch PATCHSYM."
8036afdd 767 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
6df83d42 768
d51722b7
GH
769(defun stgit-remove-mark (patchsym)
770 "Unmark the patch PATCHSYM."
8036afdd 771 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
6df83d42 772
e6b1fdae 773(defun stgit-clear-marks ()
47271f41 774 "Unmark all patches."
e6b1fdae
DK
775 (setq stgit-marked-patches '()))
776
735cb7ec 777(defun stgit-patch-at-point (&optional cause-error)
2c862b07
DK
778 (get-text-property (point) 'patch-data))
779
64ada6f5 780(defun stgit-patch-name-at-point (&optional cause-error only-patches)
d51722b7 781 "Return the patch name on the current line as a symbol.
64ada6f5
GH
782If CAUSE-ERROR is not nil, signal an error if none found.
783If ONLY-PATCHES is not nil, only allow real patches, and not
784index or work tree."
2c862b07 785 (let ((patch (stgit-patch-at-point)))
64ada6f5
GH
786 (and patch
787 only-patches
788 (memq (stgit-patch-status patch) '(work index))
789 (setq patch nil))
2c862b07
DK
790 (cond (patch
791 (stgit-patch-name patch))
792 (cause-error
793 (error "No patch on this line")))))
378a003d 794
3164eec6
DK
795(defun stgit-patched-file-at-point ()
796 (get-text-property (point) 'file-data))
56d81fe5 797
7755d7f1 798(defun stgit-patches-marked-or-at-point ()
d51722b7 799 "Return the symbols of the marked patches, or the patch on the current line."
7755d7f1 800 (if stgit-marked-patches
d51722b7 801 stgit-marked-patches
2c862b07 802 (let ((patch (stgit-patch-name-at-point)))
7755d7f1
KH
803 (if patch
804 (list patch)
805 '()))))
806
a9089e68 807(defun stgit-goto-patch (patchsym &optional file)
d51722b7 808 "Move point to the line containing patch PATCHSYM.
a9089e68
GH
809If that patch cannot be found, do nothing.
810
811If the patch was found and FILE is not nil, instead move to that
812file's line. If FILE cannot be found, stay on the line of
813PATCHSYM."
f9b82d36
DK
814 (let ((node (ewoc-nth stgit-ewoc 0)))
815 (while (and node (not (eq (stgit-patch-name (ewoc-data node))
816 patchsym)))
817 (setq node (ewoc-next stgit-ewoc node)))
a9089e68
GH
818 (when (and node file)
819 (let* ((file-ewoc (stgit-patch-files-ewoc (ewoc-data node)))
820 (file-node (ewoc-nth file-ewoc 0)))
821 (while (and file-node (not (equal (stgit-file-file (ewoc-data file-node)) file)))
822 (setq file-node (ewoc-next file-ewoc file-node)))
823 (when file-node
824 (ewoc-goto-node file-ewoc file-node)
825 (move-to-column (stgit-goal-column))
826 (setq node nil))))
f9b82d36
DK
827 (when node
828 (ewoc-goto-node stgit-ewoc node)
d51722b7 829 (move-to-column goal-column))))
56d81fe5 830
1c2426dc 831(defun stgit-init ()
a53347d9 832 "Run stg init."
1c2426dc
DK
833 (interactive)
834 (stgit-capture-output nil
b0424080 835 (stgit-run "init"))
1f0bf00f 836 (stgit-reload))
1c2426dc 837
6df83d42 838(defun stgit-mark ()
a53347d9 839 "Mark the patch under point."
6df83d42 840 (interactive)
8036afdd 841 (let* ((node (ewoc-locate stgit-ewoc))
64ada6f5
GH
842 (patch (ewoc-data node))
843 (name (stgit-patch-name patch)))
844 (when (eq name :work)
845 (error "Cannot mark the work tree"))
846 (when (eq name :index)
847 (error "Cannot mark the index"))
8036afdd
DK
848 (stgit-add-mark (stgit-patch-name patch))
849 (ewoc-invalidate stgit-ewoc node))
378a003d 850 (stgit-next-patch))
6df83d42 851
9b151b27 852(defun stgit-unmark-up ()
a53347d9 853 "Remove mark from the patch on the previous line."
6df83d42 854 (interactive)
378a003d 855 (stgit-previous-patch)
8036afdd
DK
856 (let* ((node (ewoc-locate stgit-ewoc))
857 (patch (ewoc-data node)))
858 (stgit-remove-mark (stgit-patch-name patch))
859 (ewoc-invalidate stgit-ewoc node))
860 (move-to-column (stgit-goal-column)))
9b151b27
GH
861
862(defun stgit-unmark-down ()
a53347d9 863 "Remove mark from the patch on the current line."
9b151b27 864 (interactive)
8036afdd
DK
865 (let* ((node (ewoc-locate stgit-ewoc))
866 (patch (ewoc-data node)))
867 (stgit-remove-mark (stgit-patch-name patch))
868 (ewoc-invalidate stgit-ewoc node))
1288eda2 869 (stgit-next-patch))
6df83d42 870
56d81fe5 871(defun stgit-rename (name)
018fa1ac 872 "Rename the patch under point to NAME."
64ada6f5
GH
873 (interactive (list
874 (read-string "Patch name: "
875 (symbol-name (stgit-patch-name-at-point t t)))))
876 (let ((old-patchsym (stgit-patch-name-at-point t t)))
56d81fe5 877 (stgit-capture-output nil
d51722b7
GH
878 (stgit-run "rename" old-patchsym name))
879 (let ((name-sym (intern name)))
880 (when (memq old-patchsym stgit-expanded-patches)
378a003d 881 (setq stgit-expanded-patches
d51722b7
GH
882 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
883 (when (memq old-patchsym stgit-marked-patches)
378a003d 884 (setq stgit-marked-patches
d51722b7
GH
885 (cons name-sym (delq old-patchsym stgit-marked-patches))))
886 (stgit-reload)
887 (stgit-goto-patch name-sym))))
56d81fe5 888
408fa7cb
GH
889(defun stgit-reload-or-repair (repair)
890 "Update the contents of the StGit buffer (`stgit-reload').
891
892With a prefix argument, repair the StGit metadata if the branch
893was modified with git commands (`stgit-repair')."
894 (interactive "P")
895 (if repair
896 (stgit-repair)
897 (stgit-reload)))
898
26201d96 899(defun stgit-repair ()
a53347d9 900 "Run stg repair."
26201d96
DK
901 (interactive)
902 (stgit-capture-output nil
b0424080 903 (stgit-run "repair"))
1f0bf00f 904 (stgit-reload))
26201d96 905
adeef6bc
GH
906(defun stgit-available-branches ()
907 "Returns a list of the available stg branches"
908 (let ((output (with-output-to-string
909 (stgit-run "branch" "--list")))
910 (start 0)
911 result)
912 (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start)
913 (setq result (cons (match-string 1 output) result))
914 (setq start (match-end 0)))
915 result))
916
917(defun stgit-branch (branch)
918 "Switch to branch BRANCH."
919 (interactive (list (completing-read "Switch to branch: "
920 (stgit-available-branches))))
921 (stgit-capture-output nil (stgit-run "branch" "--" branch))
922 (stgit-reload))
923
41c1c59c
GH
924(defun stgit-commit (count)
925 "Run stg commit on COUNT commits.
e552cb5f
GH
926Interactively, the prefix argument is used as COUNT.
927A negative COUNT will uncommit instead."
41c1c59c 928 (interactive "p")
e552cb5f
GH
929 (if (< count 0)
930 (stgit-uncommit (- count))
931 (stgit-capture-output nil (stgit-run "commit" "-n" count))
932 (stgit-reload)))
933
934(defun stgit-uncommit (count)
935 "Run stg uncommit on COUNT commits.
936Interactively, the prefix argument is used as COUNT.
937A negative COUNT will commit instead."
938 (interactive "p")
939 (if (< count 0)
940 (stgit-commit (- count))
941 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
942 (stgit-reload)))
c4aad9a7 943
3959a095
GH
944(defun stgit-revert-file ()
945 "Revert the file at point, which must be in the index or the
946working tree."
947 (interactive)
948 (let* ((patched-file (or (stgit-patched-file-at-point)
949 (error "No file on the current line")))
950 (patch-name (stgit-patch-name-at-point))
951 (file-status (stgit-file-status patched-file))
952 (rm-file (cond ((stgit-file-copy-or-rename patched-file)
953 (stgit-file-cr-to patched-file))
954 ((eq file-status 'add)
955 (stgit-file-file patched-file))))
956 (co-file (cond ((eq file-status 'rename)
957 (stgit-file-cr-from patched-file))
958 ((not (memq file-status '(copy add)))
959 (stgit-file-file patched-file)))))
960
961 (unless (memq patch-name '(:work :index))
962 (error "No index or working tree file on this line"))
963
d9473917
GH
964 (when (eq file-status 'ignore)
965 (error "Cannot revert ignored files"))
966
967 (when (eq file-status 'unknown)
968 (error "Cannot revert unknown files"))
969
3959a095
GH
970 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
971 (when (yes-or-no-p (format "Revert %d file%s? "
972 nfiles
973 (if (= nfiles 1) "" "s")))
974 (stgit-capture-output nil
975 (when rm-file
976 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
977 (when co-file
978 (stgit-run-git "checkout" "HEAD" co-file)))
979 (stgit-reload)))))
980
51783171
GH
981(defun stgit-resolve-file ()
982 "Resolve conflict in the file at point."
983 (interactive)
984 (let* ((patched-file (stgit-patched-file-at-point))
985 (patch (stgit-patch-at-point))
986 (patch-name (and patch (stgit-patch-name patch)))
987 (status (and patched-file (stgit-file-status patched-file))))
988
989 (unless (memq patch-name '(:work :index))
990 (error "No index or working tree file on this line"))
991
992 (unless (eq status 'unmerged)
993 (error "No conflict to resolve at the current line"))
994
995 (stgit-capture-output nil
996 (stgit-move-change-to-index (stgit-file-file patched-file)))
997
998 (stgit-reload)))
999
0b661144
DK
1000(defun stgit-push-next (npatches)
1001 "Push the first unapplied patch.
1002With numeric prefix argument, push that many patches."
1003 (interactive "p")
d51722b7 1004 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
074a4fb0
GH
1005 (stgit-reload)
1006 (stgit-refresh-git-status))
56d81fe5 1007
0b661144
DK
1008(defun stgit-pop-next (npatches)
1009 "Pop the topmost applied patch.
1010With numeric prefix argument, pop that many patches."
1011 (interactive "p")
d51722b7 1012 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
074a4fb0
GH
1013 (stgit-reload)
1014 (stgit-refresh-git-status))
56d81fe5 1015
f9182fca
KH
1016(defun stgit-applied-at-point ()
1017 "Is the patch on the current line applied?"
1018 (save-excursion
1019 (beginning-of-line)
1020 (looking-at "[>+]")))
1021
1022(defun stgit-push-or-pop ()
a53347d9 1023 "Push or pop the patch on the current line."
f9182fca 1024 (interactive)
2c862b07 1025 (let ((patchsym (stgit-patch-name-at-point t))
f9182fca
KH
1026 (applied (stgit-applied-at-point)))
1027 (stgit-capture-output nil
d51722b7 1028 (stgit-run (if applied "pop" "push") patchsym))
1f0bf00f 1029 (stgit-reload)))
f9182fca 1030
c7adf5ef 1031(defun stgit-goto ()
a53347d9 1032 "Go to the patch on the current line."
c7adf5ef 1033 (interactive)
2c862b07 1034 (let ((patchsym (stgit-patch-name-at-point t)))
c7adf5ef 1035 (stgit-capture-output nil
d51722b7 1036 (stgit-run "goto" patchsym))
1f0bf00f 1037 (stgit-reload)))
c7adf5ef 1038
d51722b7 1039(defun stgit-id (patchsym)
50d88c67
DK
1040 "Return the git commit id for PATCHSYM.
1041If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1042 (if (keywordp patchsym)
1043 patchsym
1044 (let ((result (with-output-to-string
1045 (stgit-run-silent "id" patchsym))))
1046 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1047 (error "Cannot find commit id for %s" patchsym))
1048 (match-string 1 result))))
378a003d 1049
1aece5c0 1050(defun stgit-show-patch (unmerged-stage ignore-whitespace)
d9b954c7
GH
1051 "Show the patch on the current line.
1052
1053UNMERGED-STAGE is the argument to `git-diff' that that selects
1054which stage to diff against in the case of unmerged files."
1aece5c0
GH
1055 (let ((space-arg (when (numberp ignore-whitespace)
1056 (cond ((> ignore-whitespace 4)
1057 "--ignore-all-space")
1058 ((> ignore-whitespace 1)
1059 "--ignore-space-change"))))
1060 (patch-name (stgit-patch-name-at-point t)))
1061 (stgit-capture-output "*StGit patch*"
1062 (case (get-text-property (point) 'entry-type)
1063 ('file
1064 (let* ((patched-file (stgit-patched-file-at-point))
1065 (patch-id (let ((id (stgit-id patch-name)))
1066 (if (and (eq id :index)
1067 (eq (stgit-file-status patched-file)
1068 'unmerged))
1069 :work
1070 id)))
1071 (args (append (and space-arg (list space-arg))
1072 (and (stgit-file-cr-from patched-file)
1073 (list (stgit-find-copies-harder-diff-arg)))
1074 (cond ((eq patch-id :index)
1075 '("--cached"))
1076 ((eq patch-id :work)
1077 (list unmerged-stage))
1078 (t
1079 (list (concat patch-id "^") patch-id)))
1080 '("--")
3164eec6
DK
1081 (if (stgit-file-copy-or-rename patched-file)
1082 (list (stgit-file-cr-from patched-file)
1083 (stgit-file-cr-to patched-file))
1084 (list (stgit-file-file patched-file))))))
1aece5c0
GH
1085 (apply 'stgit-run-git "diff" args)))
1086 ('patch
1087 (let* ((patch-id (stgit-id patch-name)))
1088 (if (or (eq patch-id :index) (eq patch-id :work))
1089 (apply 'stgit-run-git "diff"
1090 (stgit-find-copies-harder-diff-arg)
1091 (append (and space-arg (list space-arg))
1092 (if (eq patch-id :index)
1093 '("--cached")
1094 (list unmerged-stage))))
1095 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1096 (and space-arg (list "-O" space-arg))
1097 (list (stgit-patch-name-at-point)))))
1098 (apply 'stgit-run args)))))
1099 (t
1100 (error "No patch or file at point")))
1101 (with-current-buffer standard-output
1102 (goto-char (point-min))
1103 (diff-mode)))))
1104
1105(defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1106 `(defun ,name (&optional ignore-whitespace)
1107 ,(format "Show the patch on the current line.
1108
1109%sWith a prefix argument, ignore whitespace. With a prefix argument
1110greater than four (e.g., \\[universal-argument] \
1111\\[universal-argument] \\[%s]), ignore all whitespace."
1112 (if unmerged-action
1113 (format "For unmerged files, %s.\n\n" unmerged-action)
1114 "")
1115 name)
1116 (interactive "p")
1117 (stgit-show-patch ,diff-arg ignore-whitespace)))
1118
1119(stgit-define-diff stgit-diff
1120 "--ours" nil)
1121(stgit-define-diff stgit-diff-ours
1122 "--ours"
1123 "diff against our branch")
1124(stgit-define-diff stgit-diff-theirs
1125 "--theirs"
1126 "diff against their branch")
1127(stgit-define-diff stgit-diff-base
1128 "--base"
1129 "diff against the merge base")
1130(stgit-define-diff stgit-diff-combined
1131 "--cc"
1132 "show a combined diff")
d9b954c7 1133
fd9fe574 1134(defun stgit-move-change-to-index (file)
37cb5766 1135 "Copies the workspace state of FILE to index, using git add or git rm"
306b37a6
GH
1136 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1137 '("add") '("rm" "-q"))))
37cb5766 1138 (stgit-capture-output "*git output*"
5115dea0 1139 (apply 'stgit-run-git (append op '("--") (list file))))))
37cb5766 1140
fd9fe574 1141(defun stgit-remove-change-from-index (file)
37cb5766
DK
1142 "Unstages the change in FILE from the index"
1143 (stgit-capture-output "*git output*"
1144 (stgit-run-git "reset" "-q" "--" file)))
1145
1146(defun stgit-file-toggle-index ()
a9089e68
GH
1147 "Move modified file in or out of the index.
1148
1149Leaves the point where it is, but moves the mark to where the
1150file ended up. You can then jump to the file with \
1151\\[exchange-point-and-mark]."
37cb5766
DK
1152 (interactive)
1153 (let ((patched-file (stgit-patched-file-at-point)))
1154 (unless patched-file
1155 (error "No file on the current line"))
51783171
GH
1156 (when (eq (stgit-file-status patched-file) 'unmerged)
1157 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
d9473917
GH
1158 (when (eq (stgit-file-status patched-file) 'ignore)
1159 (error "You cannot add ignored files to the index"))
a9089e68
GH
1160 (let* ((patch (stgit-patch-at-point))
1161 (patch-name (stgit-patch-name patch))
1162 (old-point (point))
1163 next-file)
1164
1165 ;; find the next file in the patch, or the previous one if this
1166 ;; was the last file
1167 (and (zerop (forward-line 1))
1168 (let ((f (stgit-patched-file-at-point)))
1169 (and f (setq next-file (stgit-file-file f)))))
1170 (goto-char old-point)
1171 (unless next-file
1172 (and (zerop (forward-line -1))
1173 (let ((f (stgit-patched-file-at-point)))
1174 (and f (setq next-file (stgit-file-file f)))))
1175 (goto-char old-point))
1176
37cb5766 1177 (cond ((eq patch-name :work)
fd9fe574 1178 (stgit-move-change-to-index (stgit-file-file patched-file)))
37cb5766 1179 ((eq patch-name :index)
fd9fe574 1180 (stgit-remove-change-from-index (stgit-file-file patched-file)))
37cb5766 1181 (t
a9089e68
GH
1182 (error "Can only move files in the working tree to index")))
1183 (stgit-refresh-worktree)
1184 (stgit-refresh-index)
1185 (stgit-goto-patch (if (eq patch-name :index) :work :index)
1186 (stgit-file-file patched-file))
1187 (push-mark nil t t)
1188 (stgit-goto-patch patch-name next-file))))
37cb5766 1189
0bca35c8 1190(defun stgit-edit ()
a53347d9 1191 "Edit the patch on the current line."
0bca35c8 1192 (interactive)
64ada6f5 1193 (let ((patchsym (stgit-patch-name-at-point t t))
0780be79 1194 (edit-buf (get-buffer-create "*StGit edit*"))
0bca35c8
DK
1195 (dir default-directory))
1196 (log-edit 'stgit-confirm-edit t nil edit-buf)
d51722b7 1197 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
0bca35c8
DK
1198 (setq default-directory dir)
1199 (let ((standard-output edit-buf))
d51722b7 1200 (stgit-run-silent "edit" "--save-template=-" patchsym))))
0bca35c8
DK
1201
1202(defun stgit-confirm-edit ()
1203 (interactive)
1204 (let ((file (make-temp-file "stgit-edit-")))
1205 (write-region (point-min) (point-max) file)
1206 (stgit-capture-output nil
d51722b7 1207 (stgit-run "edit" "-f" file stgit-edit-patchsym))
0bca35c8 1208 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1209 (stgit-reload))))
0bca35c8 1210
aa04f831
GH
1211(defun stgit-new (add-sign)
1212 "Create a new patch.
1213With a prefix argument, include a \"Signed-off-by:\" line at the
1214end of the patch."
1215 (interactive "P")
c5d45b92
GH
1216 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1217 (dir default-directory))
1218 (log-edit 'stgit-confirm-new t nil edit-buf)
aa04f831
GH
1219 (setq default-directory dir)
1220 (when add-sign
1221 (save-excursion
1222 (let ((standard-output (current-buffer)))
1223 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
64c097a0
DK
1224
1225(defun stgit-confirm-new ()
1226 (interactive)
27b0f9e4 1227 (let ((file (make-temp-file "stgit-edit-")))
64c097a0
DK
1228 (write-region (point-min) (point-max) file)
1229 (stgit-capture-output nil
27b0f9e4 1230 (stgit-run "new" "-f" file))
64c097a0 1231 (with-current-buffer log-edit-parent-buffer
1f0bf00f 1232 (stgit-reload))))
64c097a0
DK
1233
1234(defun stgit-create-patch-name (description)
1235 "Create a patch name from a long description"
1236 (let ((patch ""))
1237 (while (> (length description) 0)
1238 (cond ((string-match "\\`[a-zA-Z_-]+" description)
8439f657
GH
1239 (setq patch (downcase (concat patch
1240 (match-string 0 description))))
64c097a0
DK
1241 (setq description (substring description (match-end 0))))
1242 ((string-match "\\` +" description)
1243 (setq patch (concat patch "-"))
1244 (setq description (substring description (match-end 0))))
1245 ((string-match "\\`[^a-zA-Z_-]+" description)
1246 (setq description (substring description (match-end 0))))))
1247 (cond ((= (length patch) 0)
1248 "patch")
1249 ((> (length patch) 20)
1250 (substring patch 0 20))
1251 (t patch))))
0bca35c8 1252
9008e45b 1253(defun stgit-delete (patchsyms &optional spill-p)
d51722b7 1254 "Delete the patches in PATCHSYMS.
9008e45b
GH
1255Interactively, delete the marked patches, or the patch at point.
1256
1257With a prefix argument, or SPILL-P, spill the patch contents to
1258the work tree and index."
1259 (interactive (list (stgit-patches-marked-or-at-point)
1260 current-prefix-arg))
e7231e4f
GH
1261 (unless patchsyms
1262 (error "No patches to delete"))
64ada6f5
GH
1263 (when (memq :index patchsyms)
1264 (error "Cannot delete the index"))
1265 (when (memq :work patchsyms)
1266 (error "Cannot delete the work tree"))
1267
d51722b7 1268 (let ((npatches (length patchsyms)))
9008e45b 1269 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
e7231e4f 1270 npatches
9008e45b
GH
1271 (if (= 1 npatches) "" "es")
1272 (if spill-p
1273 " (spilling contents to index)"
1274 "")))
1275 (let ((args (if spill-p
1276 (cons "--spill" patchsyms)
1277 patchsyms)))
1278 (stgit-capture-output nil
1279 (apply 'stgit-run "delete" args))
1280 (stgit-reload)))))
d51722b7 1281
7cc45294
GH
1282(defun stgit-move-patches-target ()
1283 "Return the patchsym indicating a target patch for
1284`stgit-move-patches'.
1285
1286This is either the patch at point, or one of :top and :bottom, if
1287the point is after or before the applied patches."
1288
2c862b07 1289 (let ((patchsym (stgit-patch-name-at-point)))
7cc45294
GH
1290 (cond (patchsym patchsym)
1291 ((save-excursion (re-search-backward "^>" nil t)) :top)
1292 (t :bottom))))
1293
95369f6c
GH
1294(defun stgit-sort-patches (patchsyms)
1295 "Returns the list of patches in PATCHSYMS sorted according to
1296their position in the patch series, bottommost first.
1297
1298PATCHSYMS may not contain duplicate entries."
1299 (let (sorted-patchsyms
1300 (series (with-output-to-string
1301 (with-current-buffer standard-output
1302 (stgit-run-silent "series" "--noprefix"))))
1303 start)
1304 (while (string-match "^\\(.+\\)" series start)
1305 (let ((patchsym (intern (match-string 1 series))))
1306 (when (memq patchsym patchsyms)
1307 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
1308 (setq start (match-end 0)))
1309 (setq sorted-patchsyms (nreverse sorted-patchsyms))
1310
1311 (unless (= (length patchsyms) (length sorted-patchsyms))
1312 (error "Internal error"))
1313
1314 sorted-patchsyms))
1315
7cc45294
GH
1316(defun stgit-move-patches (patchsyms target-patch)
1317 "Move the patches in PATCHSYMS to below TARGET-PATCH.
1318If TARGET-PATCH is :bottom or :top, move the patches to the
1319bottom or top of the stack, respectively.
1320
1321Interactively, move the marked patches to where the point is."
1322 (interactive (list stgit-marked-patches
1323 (stgit-move-patches-target)))
1324 (unless patchsyms
1325 (error "Need at least one patch to move"))
1326
1327 (unless target-patch
1328 (error "Point not at a patch"))
1329
1330 (if (eq target-patch :top)
1331 (stgit-capture-output nil
1332 (apply 'stgit-run "float" patchsyms))
1333
1334 ;; need to have patchsyms sorted by position in the stack
95369f6c 1335 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
7cc45294
GH
1336 (while sorted-patchsyms
1337 (setq sorted-patchsyms
1338 (and (stgit-capture-output nil
1339 (if (eq target-patch :bottom)
1340 (stgit-run "sink" "--" (car sorted-patchsyms))
1341 (stgit-run "sink" "--to" target-patch "--"
1342 (car sorted-patchsyms))))
1343 (cdr sorted-patchsyms))))))
1344 (stgit-reload))
1345
594aa463
KH
1346(defun stgit-squash (patchsyms)
1347 "Squash the patches in PATCHSYMS.
693d179b
GH
1348Interactively, squash the marked patches.
1349
1350Unless there are any conflicts, the patches will be merged into
1351one patch, which will occupy the same spot in the series as the
1352deepest patch had before the squash."
d51722b7
GH
1353 (interactive (list stgit-marked-patches))
1354 (when (< (length patchsyms) 2)
594aa463 1355 (error "Need at least two patches to squash"))
32d7545d
GH
1356 (let ((stgit-buffer (current-buffer))
1357 (edit-buf (get-buffer-create "*StGit edit*"))
693d179b
GH
1358 (dir default-directory)
1359 (sorted-patchsyms (stgit-sort-patches patchsyms)))
594aa463 1360 (log-edit 'stgit-confirm-squash t nil edit-buf)
693d179b 1361 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
ea0def18 1362 (setq default-directory dir)
32d7545d
GH
1363 (let ((result (let ((standard-output edit-buf))
1364 (apply 'stgit-run-silent "squash"
1365 "--save-template=-" sorted-patchsyms))))
1366
1367 ;; stg squash may have reordered the patches or caused conflicts
1368 (with-current-buffer stgit-buffer
1369 (stgit-reload))
1370
1371 (unless (eq 0 result)
1372 (fundamental-mode)
1373 (rename-buffer "*StGit error*")
1374 (resize-temp-buffer-window)
1375 (switch-to-buffer-other-window stgit-buffer)
1376 (error "stg squash failed")))))
ea0def18 1377
594aa463 1378(defun stgit-confirm-squash ()
ea0def18
DK
1379 (interactive)
1380 (let ((file (make-temp-file "stgit-edit-")))
1381 (write-region (point-min) (point-max) file)
1382 (stgit-capture-output nil
594aa463 1383 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
ea0def18 1384 (with-current-buffer log-edit-parent-buffer
e6b1fdae
DK
1385 (stgit-clear-marks)
1386 ;; Go to first marked patch and stay there
1387 (goto-char (point-min))
1388 (re-search-forward (concat "^[>+-]\\*") nil t)
1389 (move-to-column goal-column)
1390 (let ((pos (point)))
1f0bf00f 1391 (stgit-reload)
e6b1fdae 1392 (goto-char pos)))))
ea0def18 1393
0663524d
KH
1394(defun stgit-help ()
1395 "Display help for the StGit mode."
1396 (interactive)
1397 (describe-function 'stgit-mode))
3a59f3db 1398
83e51dbf
DK
1399(defun stgit-undo (&optional arg)
1400 "Run stg undo.
b8463f1d
GH
1401With prefix argument, run it with the --hard flag.
1402
1403See also `stgit-redo'."
83e51dbf
DK
1404 (interactive "P")
1405 (stgit-capture-output nil
1406 (if arg
1407 (stgit-run "undo" "--hard")
1408 (stgit-run "undo")))
1f0bf00f 1409 (stgit-reload))
83e51dbf 1410
b8463f1d
GH
1411(defun stgit-redo (&optional arg)
1412 "Run stg redo.
1413With prefix argument, run it with the --hard flag.
1414
1415See also `stgit-undo'."
1416 (interactive "P")
1417 (stgit-capture-output nil
1418 (if arg
1419 (stgit-run "redo" "--hard")
1420 (stgit-run "redo")))
1421 (stgit-reload))
1422
4d73c4d8
DK
1423(defun stgit-refresh (&optional arg)
1424 "Run stg refresh.
36a4eacd
GH
1425If the index contains any changes, only refresh from index.
1426
a53347d9 1427With prefix argument, refresh the marked patch or the patch under point."
4d73c4d8
DK
1428 (interactive "P")
1429 (let ((patchargs (if arg
b0424080
GH
1430 (let ((patches (stgit-patches-marked-or-at-point)))
1431 (cond ((null patches)
df283a8b 1432 (error "No patch to update"))
b0424080 1433 ((> (length patches) 1)
df283a8b 1434 (error "Too many patches selected"))
b0424080
GH
1435 (t
1436 (cons "-p" patches))))
1437 nil)))
36a4eacd
GH
1438 (unless (stgit-index-empty-p)
1439 (setq patchargs (cons "--index" patchargs)))
4d73c4d8 1440 (stgit-capture-output nil
074a4fb0
GH
1441 (apply 'stgit-run "refresh" patchargs))
1442 (stgit-refresh-git-status))
4d73c4d8
DK
1443 (stgit-reload))
1444
8f702de4
GH
1445(defcustom stgit-show-worktree-mode 'center
1446 "This variable controls where the \"Index\" and \"Work tree\"
1447will be shown on in the buffer.
1448
1449It can be set to 'top (above all patches), 'center (show between
1450applied and unapplied patches), and 'bottom (below all patches).
1451
1452See also `stgit-show-worktree'."
1453 :type '(radio (const :tag "above all patches (top)" top)
1454 (const :tag "between applied and unapplied patches (center)"
1455 center)
1456 (const :tag "below all patches (bottom)" bottom))
1457 :group 'stgit)
1458
ce3b6130 1459(defcustom stgit-default-show-worktree
c4e4756f 1460 t
ce3b6130
DK
1461 "Set to non-nil to by default show the working tree in a new stgit buffer.
1462
1463This value is used as the default value for `stgit-show-worktree'."
1464 :type 'boolean
1465 :group 'stgit)
1466
1467(defvar stgit-show-worktree nil
8f702de4 1468 "If nil, inhibit showing work tree and index in the stgit buffer.
ce3b6130 1469
8f702de4 1470See also `stgit-show-worktree-mode'.")
ce3b6130 1471
d9473917
GH
1472(defvar stgit-show-ignored nil
1473 "If nil, inhibit showing files ignored by git.")
1474
1475(defvar stgit-show-unknown nil
1476 "If nil, inhibit showing files not registered with git.")
1477
ce3b6130
DK
1478(defun stgit-toggle-worktree (&optional arg)
1479 "Toggle the visibility of the work tree.
1480With arg, show the work tree if arg is positive.
1481
8f702de4
GH
1482Its initial setting is controlled by `stgit-default-show-worktree'.
1483
1484`stgit-show-worktree-mode' controls where on screen the index and
1485work tree will show up."
ce3b6130
DK
1486 (interactive)
1487 (setq stgit-show-worktree
1488 (if (numberp arg)
1489 (> arg 0)
1490 (not stgit-show-worktree)))
1491 (stgit-reload))
1492
d9473917
GH
1493(defun stgit-toggle-ignored (&optional arg)
1494 "Toggle the visibility of files ignored by git in the work
1495tree. With ARG, show these files if ARG is positive.
1496
1497Use \\[stgit-toggle-worktree] to show the work tree."
1498 (interactive)
1499 (setq stgit-show-ignored
1500 (if (numberp arg)
1501 (> arg 0)
1502 (not stgit-show-ignored)))
1503 (stgit-reload))
1504
1505(defun stgit-toggle-unknown (&optional arg)
1506 "Toggle the visibility of files not registered with git in the
1507work tree. With ARG, show these files if ARG is positive.
1508
1509Use \\[stgit-toggle-worktree] to show the work tree."
1510 (interactive)
1511 (setq stgit-show-unknown
1512 (if (numberp arg)
1513 (> arg 0)
1514 (not stgit-show-unknown)))
1515 (stgit-reload))
1516
3a59f3db 1517(provide 'stgit)