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