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