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