stgit.el: Repair how patches with empty descriptions are shown
[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 (require 'easymenu)
19 (require 'format-spec)
20
21 (defun stgit-set-default (symbol value)
22 "Set default value of SYMBOL to VALUE using `set-default' and
23 reload all StGit buffers."
24 (set-default symbol value)
25 (dolist (buf (buffer-list))
26 (with-current-buffer buf
27 (when (eq major-mode 'stgit-mode)
28 (stgit-reload)))))
29
30 (defgroup stgit nil
31 "A user interface for the StGit patch maintenance tool."
32 :group 'tools
33 :link '(function-link stgit)
34 :link '(url-link "http://www.procode.org/stgit/"))
35
36 (defcustom stgit-abbreviate-copies-and-renames t
37 "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\"
38 instead of \"dir/old/file -> dir/new/file\"."
39 :type 'boolean
40 :group 'stgit
41 :set 'stgit-set-default)
42
43 (defcustom stgit-default-show-worktree t
44 "Set to non-nil to by default show the working tree in a new stgit buffer.
45
46 Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle the this setting in an already-started StGit buffer."
47 :type 'boolean
48 :group 'stgit
49 :link '(variable-link stgit-show-worktree))
50
51 (defcustom stgit-find-copies-harder nil
52 "Try harder to find copied files when listing patches.
53
54 When not nil, runs git diff-tree with the --find-copies-harder
55 flag, which reduces performance."
56 :type 'boolean
57 :group 'stgit
58 :set 'stgit-set-default)
59
60 (defcustom stgit-show-worktree-mode 'center
61 "This variable controls where the \"Index\" and \"Work tree\"
62 will be shown on in the buffer.
63
64 It can be set to 'top (above all patches), 'center (show between
65 applied and unapplied patches), and 'bottom (below all patches)."
66 :type '(radio (const :tag "above all patches (top)" top)
67 (const :tag "between applied and unapplied patches (center)"
68 center)
69 (const :tag "below all patches (bottom)" bottom))
70 :group 'stgit
71 :link '(variable-link stgit-show-worktree)
72 :set 'stgit-set-default)
73
74 (defcustom stgit-patch-line-format "%s%m%-30n %e%d"
75 "The format string used to format patch lines.
76 The format string is passed to `format-spec' and the following
77 format characters are recognized:
78
79 %s - A '+', '-', '>' or space, depending on whether the patch is
80 applied, unapplied, top, or something else.
81
82 %m - An asterisk if the patch is marked, and a space otherwise.
83
84 %n - The patch name.
85
86 %e - The string \"(empty) \" if the patch is empty.
87
88 %d - The short patch description.
89
90 %D - The short patch description, or the patch name.
91
92 When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format'
93 variable is used instead."
94 :type 'string
95 :group 'stgit
96 :set 'stgit-set-default)
97
98 (defcustom stgit-noname-patch-line-format "%s%m%e%D"
99 "The alternate format string used to format patch lines.
100 It has the same semantics as `stgit-patch-line-format', and the
101 display can be toggled between the two formats using
102 \\<stgit-mode-map>>\\[stgit-toggle-patch-names].
103
104 The alternate form is used when the patch name is hidden."
105 :type 'string
106 :group 'stgit
107 :set 'stgit-set-default)
108
109 (defcustom stgit-default-show-patch-names t
110 "If non-nil, default to showing patch names in a new stgit buffer.
111
112 Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] to toggle the
113 this setting in an already-started StGit buffer."
114 :type 'boolean
115 :group 'stgit
116 :link '(variable-link stgit-show-patch-names))
117
118 (defcustom stgit-file-line-format " %-11s %-2m %n %c"
119 "The format string used to format file lines.
120 The format string is passed to `format-spec' and the following
121 format characters are recognized:
122
123 %s - A string describing the status of the file.
124
125 %m - Mode change information
126
127 %n - The file name.
128
129 %c - A description of file changes."
130 :type 'string
131 :group 'stgit
132 :set 'stgit-set-default)
133
134 (defface stgit-branch-name-face
135 '((t :inherit bold))
136 "The face used for the StGit branch name"
137 :group 'stgit)
138
139 (defface stgit-top-patch-face
140 '((((background dark)) (:weight bold :foreground "yellow"))
141 (((background light)) (:weight bold :foreground "purple"))
142 (t (:weight bold)))
143 "The face used for the top patch names"
144 :group 'stgit)
145
146 (defface stgit-applied-patch-face
147 '((((background dark)) (:foreground "light yellow"))
148 (((background light)) (:foreground "purple"))
149 (t ()))
150 "The face used for applied patch names"
151 :group 'stgit)
152
153 (defface stgit-unapplied-patch-face
154 '((((background dark)) (:foreground "gray80"))
155 (((background light)) (:foreground "orchid"))
156 (t ()))
157 "The face used for unapplied patch names"
158 :group 'stgit)
159
160 (defface stgit-description-face
161 '((((background dark)) (:foreground "tan"))
162 (((background light)) (:foreground "dark red")))
163 "The face used for StGit descriptions"
164 :group 'stgit)
165
166 (defface stgit-index-work-tree-title-face
167 '((((supports :slant italic)) :slant italic)
168 (t :inherit bold))
169 "StGit mode face used for the \"Index\" and \"Work tree\" titles"
170 :group 'stgit)
171
172 (defface stgit-unmerged-file-face
173 '((((class color) (background light)) (:foreground "red" :bold t))
174 (((class color) (background dark)) (:foreground "red" :bold t)))
175 "StGit mode face used for unmerged file status"
176 :group 'stgit)
177
178 (defface stgit-unknown-file-face
179 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
180 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
181 "StGit mode face used for unknown file status"
182 :group 'stgit)
183
184 (defface stgit-ignored-file-face
185 '((((class color) (background light)) (:foreground "grey60"))
186 (((class color) (background dark)) (:foreground "grey40")))
187 "StGit mode face used for ignored files")
188
189 (defface stgit-file-permission-face
190 '((((class color) (background light)) (:foreground "green" :bold t))
191 (((class color) (background dark)) (:foreground "green" :bold t)))
192 "StGit mode face used for permission changes."
193 :group 'stgit)
194
195 (defface stgit-modified-file-face
196 '((((class color) (background light)) (:foreground "purple"))
197 (((class color) (background dark)) (:foreground "salmon")))
198 "StGit mode face used for modified file status"
199 :group 'stgit)
200
201 (defun stgit (dir)
202 "Manage StGit patches for the tree in DIR.
203
204 See `stgit-mode' for commands available."
205 (interactive "DDirectory: \n")
206 (switch-to-stgit-buffer (git-get-top-dir dir))
207 (stgit-reload))
208
209 (defun stgit-assert-mode ()
210 "Signal an error if not in an StGit buffer."
211 (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer"))
212
213 (unless (fboundp 'git-get-top-dir)
214 (defun git-get-top-dir (dir)
215 "Retrieve the top-level directory of a git tree."
216 (let ((cdup (with-output-to-string
217 (with-current-buffer standard-output
218 (cd dir)
219 (unless (eq 0 (call-process "git" nil t nil
220 "rev-parse" "--show-cdup"))
221 (error "Cannot find top-level git tree for %s" dir))))))
222 (expand-file-name (concat (file-name-as-directory dir)
223 (car (split-string cdup "\n")))))))
224
225 (defun stgit-refresh-git-status (&optional dir)
226 "If it exists, refresh the `git-status' buffer belonging to
227 directory DIR or `default-directory'"
228 (when (and (fboundp 'git-find-status-buffer)
229 (fboundp 'git-refresh-status))
230 (let* ((top-dir (git-get-top-dir (or dir default-directory)))
231 (git-status-buffer (and top-dir (git-find-status-buffer top-dir))))
232 (when git-status-buffer
233 (with-current-buffer git-status-buffer
234 (git-refresh-status))))))
235
236 (defun stgit-find-buffer (dir)
237 "Return the buffer displaying StGit patches for DIR, or nil if none."
238 (setq dir (file-name-as-directory dir))
239 (let ((buffers (buffer-list)))
240 (while (and buffers
241 (not (with-current-buffer (car buffers)
242 (and (eq major-mode 'stgit-mode)
243 (string= default-directory dir)))))
244 (setq buffers (cdr buffers)))
245 (and buffers (car buffers))))
246
247 (defun switch-to-stgit-buffer (dir)
248 "Switch to a (possibly new) buffer displaying StGit patches for DIR."
249 (setq dir (file-name-as-directory dir))
250 (let ((buffer (stgit-find-buffer dir)))
251 (switch-to-buffer (or buffer
252 (create-stgit-buffer dir)))))
253
254 (defstruct (stgit-patch
255 (:conc-name stgit-patch->))
256 status name desc empty files-ewoc)
257
258 (defun stgit-patch-display-name (patch)
259 (let ((name (stgit-patch->name patch)))
260 (case name
261 (:index "Index")
262 (:work "Work Tree")
263 (t (symbol-name name)))))
264
265 (defun stgit-insert-without-trailing-whitespace (text)
266 "Insert TEXT in buffer using `insert', without trailing whitespace.
267 A newline is appended."
268 (unless (string-match "\\(.*?\\) *$" text)
269 (error))
270 (insert (match-string 1 text) ?\n))
271
272 (defun stgit-patch-pp (patch)
273 (let* ((status (stgit-patch->status patch))
274 (start (point))
275 (name (stgit-patch->name patch))
276 (face (cdr (assq status stgit-patch-status-face-alist)))
277 (fmt (if stgit-show-patch-names
278 stgit-patch-line-format
279 stgit-noname-patch-line-format))
280 (spec (format-spec-make
281 ?s (case status
282 ('applied "+")
283 ('top ">")
284 ('unapplied "-")
285 (t " "))
286 ?m (if (memq name stgit-marked-patches)
287 "*" " ")
288 ?n (propertize (stgit-patch-display-name patch)
289 'face face
290 'syntax-table (string-to-syntax "w"))
291 ?e (if (stgit-patch->empty patch) "(empty) " "")
292 ?d (propertize (or (stgit-patch->desc patch) "")
293 'face 'stgit-description-face)
294 ?D (propertize (let ((desc (stgit-patch->desc patch)))
295 (if (zerop (length desc))
296 (stgit-patch-display-name patch)
297 desc))
298 'face face)))
299 (text (format-spec fmt spec)))
300
301 (stgit-insert-without-trailing-whitespace text)
302 (put-text-property start (point) 'entry-type 'patch)
303 (when (memq name stgit-expanded-patches)
304 (stgit-insert-patch-files patch))
305 (put-text-property start (point) 'patch-data patch)))
306
307 (defun create-stgit-buffer (dir)
308 "Create a buffer for showing StGit patches.
309 Argument DIR is the repository path."
310 (let ((buf (create-file-buffer (concat dir "*stgit*")))
311 (inhibit-read-only t))
312 (with-current-buffer buf
313 (setq default-directory dir)
314 (stgit-mode)
315 (set (make-local-variable 'stgit-ewoc)
316 (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t))
317 (setq buffer-read-only t))
318 buf))
319
320 (defmacro stgit-capture-output (name &rest body)
321 "Capture StGit output and, if there was any output, show it in a window
322 at the end.
323 Returns nil if there was no output."
324 (declare (debug ([&or stringp null] body))
325 (indent 1))
326 `(let ((output-buf (get-buffer-create ,(or name "*StGit output*")))
327 (stgit-dir default-directory)
328 (inhibit-read-only t))
329 (with-current-buffer output-buf
330 (buffer-disable-undo)
331 (erase-buffer)
332 (setq default-directory stgit-dir)
333 (setq buffer-read-only t))
334 (let ((standard-output output-buf))
335 ,@body)
336 (with-current-buffer output-buf
337 (set-buffer-modified-p nil)
338 (setq buffer-read-only t)
339 (if (< (point-min) (point-max))
340 (display-buffer output-buf t)))))
341
342 (defun stgit-make-run-args (args)
343 "Return a copy of ARGS with its elements converted to strings."
344 (mapcar (lambda (x)
345 ;; don't use (format "%s" ...) to limit type errors
346 (cond ((stringp x) x)
347 ((integerp x) (number-to-string x))
348 ((symbolp x) (symbol-name x))
349 (t
350 (error "Bad element in stgit-make-run-args args: %S" x))))
351 args))
352
353 (defun stgit-run-silent (&rest args)
354 (setq args (stgit-make-run-args args))
355 (apply 'call-process "stg" nil standard-output nil args))
356
357 (defun stgit-run (&rest args)
358 (setq args (stgit-make-run-args args))
359 (let ((msgcmd (mapconcat #'identity args " ")))
360 (message "Running stg %s..." msgcmd)
361 (apply 'call-process "stg" nil standard-output nil args)
362 (message "Running stg %s...done" msgcmd)))
363
364 (defun stgit-run-git (&rest args)
365 (setq args (stgit-make-run-args args))
366 (let ((msgcmd (mapconcat #'identity args " ")))
367 (message "Running git %s..." msgcmd)
368 (apply 'call-process "git" nil standard-output nil args)
369 (message "Running git %s...done" msgcmd)))
370
371 (defun stgit-run-git-silent (&rest args)
372 (setq args (stgit-make-run-args args))
373 (apply 'call-process "git" nil standard-output nil args))
374
375 (defun stgit-index-empty-p ()
376 "Returns non-nil if the index contains no changes from HEAD."
377 (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD")))
378
379 (defun stgit-work-tree-empty-p ()
380 "Returns non-nil if the work tree contains no changes from index."
381 (zerop (stgit-run-git-silent "diff-files" "--quiet")))
382
383 (defvar stgit-index-node)
384 (defvar stgit-worktree-node)
385
386 (defconst stgit-allowed-branch-name-re
387 ;; Disallow control characters, space, del, and "/:@^{}~" in
388 ;; "/"-separated parts; parts may not start with a period (.)
389 "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\
390 \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$"
391 "Regular expression that (new) branch names must match.")
392
393 (defun stgit-refresh-index ()
394 (when stgit-index-node
395 (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node))))
396
397 (defun stgit-refresh-worktree ()
398 (when stgit-worktree-node
399 (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node))))
400
401 (defun stgit-run-series-insert-index (ewoc)
402 (setq index-node (cons ewoc (ewoc-enter-last ewoc
403 (make-stgit-patch
404 :status 'index
405 :name :index
406 :desc nil
407 :empty nil)))
408 worktree-node (cons ewoc (ewoc-enter-last ewoc
409 (make-stgit-patch
410 :status 'work
411 :name :work
412 :desc nil
413 :empty nil)))))
414
415 (defun stgit-run-series (ewoc)
416 (setq stgit-index-node nil
417 stgit-worktree-node nil)
418 (let ((inserted-index (not stgit-show-worktree))
419 index-node
420 worktree-node
421 all-patchsyms)
422 (with-temp-buffer
423 (let* ((standard-output (current-buffer))
424 (exit-status (stgit-run-silent "series"
425 "--description" "--empty")))
426 (goto-char (point-min))
427 (if (not (zerop exit-status))
428 (cond ((looking-at "stg series: \\(.*\\)")
429 (setq inserted-index t)
430 (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc))
431 (substitute-command-keys
432 "-- not initialized; run \\[stgit-init]")))
433 ((looking-at ".*")
434 (error "Error running stg: %s"
435 (match-string 0))))
436 (while (not (eobp))
437 (unless (looking-at
438 "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)")
439 (error "Syntax error in output from stg series"))
440 (let* ((state-str (match-string 2))
441 (state (cond ((string= state-str ">") 'top)
442 ((string= state-str "+") 'applied)
443 ((string= state-str "-") 'unapplied)))
444 (name (intern (match-string 4)))
445 (desc (match-string 5))
446 (empty (string= (match-string 1) "0")))
447 (unless inserted-index
448 (when (or (eq stgit-show-worktree-mode 'top)
449 (and (eq stgit-show-worktree-mode 'center)
450 (eq state 'unapplied)))
451 (setq inserted-index t)
452 (stgit-run-series-insert-index ewoc)))
453 (setq all-patchsyms (cons name all-patchsyms))
454 (ewoc-enter-last ewoc
455 (make-stgit-patch
456 :status state
457 :name name
458 :desc desc
459 :empty empty)))
460 (forward-line 1))))
461 (unless inserted-index
462 (stgit-run-series-insert-index ewoc)))
463 (setq stgit-index-node index-node
464 stgit-worktree-node worktree-node
465 stgit-marked-patches (intersection stgit-marked-patches
466 all-patchsyms))))
467
468 (defun stgit-current-branch ()
469 "Return the name of the current branch."
470 (substring (with-output-to-string
471 (stgit-run-silent "branch"))
472 0 -1))
473
474 (defun stgit-reload ()
475 "Update the contents of the StGit buffer."
476 (interactive)
477 (stgit-assert-mode)
478 (let ((inhibit-read-only t)
479 (curline (line-number-at-pos))
480 (curpatch (stgit-patch-name-at-point))
481 (curfile (stgit-patched-file-at-point)))
482 (ewoc-filter stgit-ewoc #'(lambda (x) nil))
483 (ewoc-set-hf stgit-ewoc
484 (concat "Branch: "
485 (propertize (stgit-current-branch)
486 'face 'stgit-branch-name-face)
487 "\n\n")
488 (if stgit-show-worktree
489 "--"
490 (propertize
491 (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\
492 shows the working tree\n")
493 'face 'stgit-description-face)))
494 (stgit-run-series stgit-ewoc)
495 (if curpatch
496 (stgit-goto-patch curpatch (and curfile (stgit-file->file curfile)))
497 (goto-line curline)))
498 (stgit-refresh-git-status))
499
500 (defconst stgit-file-status-code-strings
501 (mapcar (lambda (arg)
502 (cons (car arg)
503 (propertize (cadr arg) 'face (car (cddr arg)))))
504 '((add "Added" stgit-modified-file-face)
505 (copy "Copied" stgit-modified-file-face)
506 (delete "Deleted" stgit-modified-file-face)
507 (modify "Modified" stgit-modified-file-face)
508 (rename "Renamed" stgit-modified-file-face)
509 (mode-change "Mode change" stgit-modified-file-face)
510 (unmerged "Unmerged" stgit-unmerged-file-face)
511 (unknown "Unknown" stgit-unknown-file-face)
512 (ignore "Ignored" stgit-ignored-file-face)))
513 "Alist of code symbols to description strings")
514
515 (defconst stgit-patch-status-face-alist
516 '((applied . stgit-applied-patch-face)
517 (top . stgit-top-patch-face)
518 (unapplied . stgit-unapplied-patch-face)
519 (index . stgit-index-work-tree-title-face)
520 (work . stgit-index-work-tree-title-face))
521 "Alist of face to use for a given patch status")
522
523 (defun stgit-file-status-code-as-string (file)
524 "Return stgit status code for FILE as a string"
525 (let* ((code (assq (stgit-file->status file)
526 stgit-file-status-code-strings))
527 (score (stgit-file->cr-score file)))
528 (when code
529 (if (and score (/= score 100))
530 (format "%s %s" (cdr code)
531 (propertize (format "%d%%" score)
532 'face 'stgit-description-face))
533 (cdr code)))))
534
535 (defun stgit-file-status-code (str &optional score)
536 "Return stgit status code from git status string"
537 (let ((code (assoc str '(("A" . add)
538 ("C" . copy)
539 ("D" . delete)
540 ("I" . ignore)
541 ("M" . modify)
542 ("R" . rename)
543 ("T" . mode-change)
544 ("U" . unmerged)
545 ("X" . unknown)))))
546 (setq code (if code (cdr code) 'unknown))
547 (when (stringp score)
548 (if (> (length score) 0)
549 (setq score (string-to-number score))
550 (setq score nil)))
551 (if score (cons code score) code)))
552
553 (defconst stgit-file-type-strings
554 '((#o100 . "file")
555 (#o120 . "symlink")
556 (#o160 . "subproject"))
557 "Alist of names of file types")
558
559 (defun stgit-file-type-string (type)
560 "Return string describing file type TYPE (the high bits of file permission).
561 Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'."
562 (let ((type-str (assoc type stgit-file-type-strings)))
563 (or (and type-str (cdr type-str))
564 (format "unknown type %o" type))))
565
566 (defun stgit-file-type-change-string (old-perm new-perm)
567 "Return string describing file type change from OLD-PERM to NEW-PERM.
568 Cf. `stgit-file-type-string'."
569 (let ((old-type (lsh old-perm -9))
570 (new-type (lsh new-perm -9)))
571 (cond ((= old-type new-type) "")
572 ((zerop new-type) "")
573 ((zerop old-type)
574 (if (= new-type #o100)
575 ""
576 (format "(%s)" (stgit-file-type-string new-type))))
577 (t (format "(%s -> %s)"
578 (stgit-file-type-string old-type)
579 (stgit-file-type-string new-type))))))
580
581 (defun stgit-file-mode-change-string (old-perm new-perm)
582 "Return string describing file mode change from OLD-PERM to NEW-PERM.
583 Cf. `stgit-file-type-change-string'."
584 (setq old-perm (logand old-perm #o777)
585 new-perm (logand new-perm #o777))
586 (if (or (= old-perm new-perm)
587 (zerop old-perm)
588 (zerop new-perm))
589 ""
590 (let* ((modified (logxor old-perm new-perm))
591 (not-x-modified (logand (logxor old-perm new-perm) #o666)))
592 (cond ((zerop modified) "")
593 ((and (zerop not-x-modified)
594 (or (and (eq #o111 (logand old-perm #o111))
595 (propertize "-x" 'face 'stgit-file-permission-face))
596 (and (eq #o111 (logand new-perm #o111))
597 (propertize "+x" 'face
598 'stgit-file-permission-face)))))
599 (t (concat (propertize (format "%o" old-perm)
600 'face 'stgit-file-permission-face)
601 (propertize " -> "
602 'face 'stgit-description-face)
603 (propertize (format "%o" new-perm)
604 'face 'stgit-file-permission-face)))))))
605
606 (defstruct (stgit-file
607 (:conc-name stgit-file->))
608 old-perm new-perm copy-or-rename cr-score cr-from cr-to status file)
609
610 (defun stgit-describe-copy-or-rename (file)
611 (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " "))
612 from to common-head common-tail)
613
614 (when stgit-abbreviate-copies-and-renames
615 (setq from (split-string (stgit-file->cr-from file) "/")
616 to (split-string (stgit-file->cr-to file) "/"))
617
618 (while (and from to (cdr from) (cdr to)
619 (string-equal (car from) (car to)))
620 (setq common-head (cons (car from) common-head)
621 from (cdr from)
622 to (cdr to)))
623 (setq common-head (nreverse common-head)
624 from (nreverse from)
625 to (nreverse to))
626 (while (and from to (cdr from) (cdr to)
627 (string-equal (car from) (car to)))
628 (setq common-tail (cons (car from) common-tail)
629 from (cdr from)
630 to (cdr to)))
631 (setq from (nreverse from)
632 to (nreverse to)))
633
634 (if (or common-head common-tail)
635 (concat (if common-head
636 (mapconcat #'identity common-head "/")
637 "")
638 (if common-head "/" "")
639 (propertize "{" 'face 'stgit-description-face)
640 (mapconcat #'identity from "/")
641 arrow
642 (mapconcat #'identity to "/")
643 (propertize "}" 'face 'stgit-description-face)
644 (if common-tail "/" "")
645 (if common-tail
646 (mapconcat #'identity common-tail "/")
647 ""))
648 (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file)))))
649
650 (defun stgit-file-pp (file)
651 (let ((start (point))
652 (spec (format-spec-make
653 ?s (stgit-file-status-code-as-string file)
654 ?m (stgit-file-mode-change-string
655 (stgit-file->old-perm file)
656 (stgit-file->new-perm file))
657 ?n (if (stgit-file->copy-or-rename file)
658 (stgit-describe-copy-or-rename file)
659 (stgit-file->file file))
660 ?c (propertize (stgit-file-type-change-string
661 (stgit-file->old-perm file)
662 (stgit-file->new-perm file))
663 'face 'stgit-description-face))))
664 (stgit-insert-without-trailing-whitespace
665 (format-spec stgit-file-line-format spec))
666 (add-text-properties start (point)
667 (list 'entry-type 'file
668 'file-data file))))
669
670 (defun stgit-find-copies-harder-diff-arg ()
671 "Return the flag to use with `git-diff' depending on the
672 `stgit-find-copies-harder' flag."
673 (if stgit-find-copies-harder "--find-copies-harder" "-C"))
674
675 (defun stgit-insert-ls-files (args file-flag)
676 (let ((start (point)))
677 (apply 'stgit-run-git
678 (append '("ls-files" "--exclude-standard" "-z") args))
679 (goto-char start)
680 (while (looking-at "\\([^\0]*\\)\0")
681 (let ((name-len (- (match-end 0) (match-beginning 0))))
682 (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0")
683 (forward-char name-len)))))
684
685 (defun stgit-process-files (callback)
686 (goto-char (point-min))
687 (when (looking-at "[0-9A-Fa-f]\\{40\\}\0")
688 (goto-char (match-end 0)))
689 (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ")
690 (let ((old-perm (string-to-number (match-string 1) 8))
691 (new-perm (string-to-number (match-string 2) 8)))
692 (goto-char (match-end 0))
693 (let ((file
694 (cond ((looking-at
695 "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0")
696 (let* ((patch-status (stgit-patch->status patch))
697 (file-subexp (if (eq patch-status 'unapplied)
698 3
699 4))
700 (file (match-string file-subexp)))
701 (make-stgit-file
702 :old-perm old-perm
703 :new-perm new-perm
704 :copy-or-rename t
705 :cr-score (string-to-number (match-string 2))
706 :cr-from (match-string 3)
707 :cr-to (match-string 4)
708 :status (stgit-file-status-code
709 (match-string 1))
710 :file file)))
711 ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0")
712 (make-stgit-file
713 :old-perm old-perm
714 :new-perm new-perm
715 :copy-or-rename nil
716 :cr-score nil
717 :cr-from nil
718 :cr-to nil
719 :status (stgit-file-status-code
720 (match-string 1))
721 :file (match-string 2))))))
722 (goto-char (match-end 0))
723 (funcall callback file)))))
724
725
726 (defun stgit-insert-patch-files (patch)
727 "Expand (show modification of) the patch PATCH after the line
728 at point."
729 (let* ((patchsym (stgit-patch->name patch))
730 (end (point-marker))
731 (args (list "-z" (stgit-find-copies-harder-diff-arg)))
732 (ewoc (ewoc-create #'stgit-file-pp nil nil t)))
733 (set-marker-insertion-type end t)
734 (setf (stgit-patch->files-ewoc patch) ewoc)
735 (with-temp-buffer
736 (let ((standard-output (current-buffer)))
737 (apply 'stgit-run-git
738 (cond ((eq patchsym :work)
739 `("diff-files" "-0" ,@args))
740 ((eq patchsym :index)
741 `("diff-index" ,@args "--cached" "HEAD"))
742 (t
743 `("diff-tree" ,@args "-r" ,(stgit-id patchsym)))))
744
745 (when (and (eq patchsym :work))
746 (when stgit-show-ignored
747 (stgit-insert-ls-files '("--ignored" "--others") "I"))
748 (when stgit-show-unknown
749 (stgit-insert-ls-files '("--directory" "--no-empty-directory"
750 "--others")
751 "X"))
752 (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1"
753 (point-min) (point-max)))
754
755 (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file)))
756
757 (unless (ewoc-nth ewoc 0)
758 (ewoc-set-hf ewoc ""
759 (concat " "
760 (propertize "<no files>"
761 'face 'stgit-description-face)
762 "\n")))))
763 (goto-char end)))
764
765 (defun stgit-find-file (&optional other-window)
766 (let* ((file (or (stgit-patched-file-at-point)
767 (error "No file at point")))
768 (filename (expand-file-name (stgit-file->file file))))
769 (unless (file-exists-p filename)
770 (error "File does not exist"))
771 (funcall (if other-window 'find-file-other-window 'find-file)
772 filename)
773 (when (eq (stgit-file->status file) 'unmerged)
774 (smerge-mode 1))))
775
776 (defun stgit-expand (&optional patches collapse)
777 "Show the contents of marked patches, or the patch at point.
778
779 See also `stgit-collapse'.
780
781 Non-interactively, operate on PATCHES, and collapse instead of
782 expand if COLLAPSE is not nil."
783 (interactive (list (stgit-patches-marked-or-at-point t)))
784 (stgit-assert-mode)
785 (let ((patches-diff (funcall (if collapse #'intersection #'set-difference)
786 patches stgit-expanded-patches)))
787 (setq stgit-expanded-patches
788 (if collapse
789 (set-difference stgit-expanded-patches patches-diff)
790 (append stgit-expanded-patches patches-diff)))
791 (ewoc-map #'(lambda (patch)
792 (memq (stgit-patch->name patch) patches-diff))
793 stgit-ewoc))
794 (move-to-column (stgit-goal-column)))
795
796 (defun stgit-collapse (&optional patches)
797 "Hide the contents of marked patches, or the patch at point.
798
799 See also `stgit-expand'."
800 (interactive (list (stgit-patches-marked-or-at-point t)))
801 (stgit-assert-mode)
802 (stgit-expand patches t))
803
804 (defun stgit-select-patch ()
805 (let ((patchname (stgit-patch-name-at-point)))
806 (stgit-expand (list patchname)
807 (memq patchname stgit-expanded-patches))))
808
809 (defun stgit-expand-directory (file)
810 (let* ((patch (stgit-patch-at-point))
811 (ewoc (stgit-patch->files-ewoc patch))
812 (node (ewoc-locate ewoc))
813 (filename (stgit-file->file file))
814 (start (make-marker))
815 (end (make-marker)))
816
817 (save-excursion
818 (forward-line 1)
819 (set-marker start (point))
820 (set-marker end (point))
821 (set-marker-insertion-type end t))
822
823 (assert (string-match "/$" filename))
824 ;; remove trailing "/"
825 (setf (stgit-file->file file) (substring filename 0 -1))
826 (ewoc-invalidate ewoc node)
827
828 (with-temp-buffer
829 (let ((standard-output (current-buffer)))
830 (stgit-insert-ls-files (list "--directory" "--others"
831 "--no-empty-directory" "--"
832 filename)
833 "X")
834 (stgit-process-files (lambda (f)
835 (setq node (ewoc-enter-after ewoc node f))))))
836
837 (let ((inhibit-read-only t))
838 (put-text-property start end 'patch-data patch))))
839
840 (defun stgit-select-file ()
841 (let* ((file (or (stgit-patched-file-at-point)
842 (error "No file at point")))
843 (filename (stgit-file->file file)))
844 (if (string-match "/$" filename)
845 (stgit-expand-directory file)
846 (stgit-find-file))))
847
848 (defun stgit-select ()
849 "With point on a patch, toggle showing files in the patch.
850
851 With point on a file, open the associated file. Opens the target
852 file for (applied) copies and renames."
853 (interactive)
854 (stgit-assert-mode)
855 (case (get-text-property (point) 'entry-type)
856 ('patch
857 (stgit-select-patch))
858 ('file
859 (stgit-select-file))
860 (t
861 (error "No patch or file on line"))))
862
863 (defun stgit-find-file-other-window ()
864 "Open file at point in other window"
865 (interactive)
866 (stgit-assert-mode)
867 (stgit-find-file t))
868
869 (defun stgit-find-file-merge ()
870 "Open file at point and merge it using `smerge-ediff'."
871 (interactive)
872 (stgit-assert-mode)
873 (stgit-find-file t)
874 (smerge-ediff))
875
876 (defun stgit-quit ()
877 "Hide the stgit buffer."
878 (interactive)
879 (stgit-assert-mode)
880 (bury-buffer))
881
882 (defun stgit-git-status ()
883 "Show status using `git-status'."
884 (interactive)
885 (stgit-assert-mode)
886 (unless (fboundp 'git-status)
887 (error "The stgit-git-status command requires git-status"))
888 (let ((dir default-directory))
889 (save-selected-window
890 (pop-to-buffer nil)
891 (git-status dir))))
892
893 (defun stgit-goal-column ()
894 "Return goal column for the current line"
895 (case (get-text-property (point) 'entry-type)
896 ('patch 2)
897 ('file 4)
898 (t 0)))
899
900 (defun stgit-next-line (&optional arg)
901 "Move cursor vertically down ARG lines"
902 (interactive "p")
903 (stgit-assert-mode)
904 (next-line arg)
905 (move-to-column (stgit-goal-column)))
906
907 (defun stgit-previous-line (&optional arg)
908 "Move cursor vertically up ARG lines"
909 (interactive "p")
910 (stgit-assert-mode)
911 (previous-line arg)
912 (move-to-column (stgit-goal-column)))
913
914 (defun stgit-next-patch (&optional arg)
915 "Move cursor down ARG patches."
916 (interactive "p")
917 (stgit-assert-mode)
918 (ewoc-goto-next stgit-ewoc (or arg 1))
919 (move-to-column goal-column))
920
921 (defun stgit-previous-patch (&optional arg)
922 "Move cursor up ARG patches."
923 (interactive "p")
924 (stgit-assert-mode)
925 (ewoc-goto-prev stgit-ewoc (or arg 1))
926 (move-to-column goal-column))
927
928 (defvar stgit-mode-hook nil
929 "Run after `stgit-mode' is setup.")
930
931 (defvar stgit-mode-map nil
932 "Keymap for StGit major mode.")
933
934 (unless stgit-mode-map
935 (let ((diff-map (make-sparse-keymap))
936 (toggle-map (make-sparse-keymap)))
937 (suppress-keymap diff-map)
938 (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg)))
939 '(("b" . stgit-diff-base)
940 ("c" . stgit-diff-combined)
941 ("m" . stgit-find-file-merge)
942 ("o" . stgit-diff-ours)
943 ("t" . stgit-diff-theirs)))
944 (suppress-keymap toggle-map)
945 (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg)))
946 '(("n" . stgit-toggle-patch-names)
947 ("t" . stgit-toggle-worktree)
948 ("i" . stgit-toggle-ignored)
949 ("u" . stgit-toggle-unknown)))
950 (setq stgit-mode-map (make-keymap))
951 (suppress-keymap stgit-mode-map)
952 (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg)))
953 `((" " . stgit-mark-down)
954 ("m" . stgit-mark-down)
955 ("\d" . stgit-unmark-up)
956 ("u" . stgit-unmark-down)
957 ("?" . stgit-help)
958 ("h" . stgit-help)
959 ("\C-p" . stgit-previous-line)
960 ("\C-n" . stgit-next-line)
961 ([up] . stgit-previous-line)
962 ([down] . stgit-next-line)
963 ("p" . stgit-previous-patch)
964 ("n" . stgit-next-patch)
965 ("\M-{" . stgit-previous-patch)
966 ("\M-}" . stgit-next-patch)
967 ("s" . stgit-git-status)
968 ("g" . stgit-reload-or-repair)
969 ("r" . stgit-refresh)
970 ("\C-c\C-r" . stgit-rename)
971 ("e" . stgit-edit)
972 ("M" . stgit-move-patches)
973 ("S" . stgit-squash)
974 ("N" . stgit-new)
975 ("c" . stgit-new-and-refresh)
976 ("\C-c\C-c" . stgit-commit)
977 ("\C-c\C-u" . stgit-uncommit)
978 ("U" . stgit-revert)
979 ("R" . stgit-resolve-file)
980 ("\r" . stgit-select)
981 ("+" . stgit-expand)
982 ("-" . stgit-collapse)
983 ("o" . stgit-find-file-other-window)
984 ("i" . stgit-toggle-index)
985 (">" . stgit-push-next)
986 ("<" . stgit-pop-next)
987 ("P" . stgit-push-or-pop)
988 ("G" . stgit-goto)
989 ("=" . stgit-diff)
990 ("D" . stgit-delete)
991 ([?\C-/] . stgit-undo)
992 ("\C-_" . stgit-undo)
993 ([?\C-c ?\C-/] . stgit-redo)
994 ("\C-c\C-_" . stgit-redo)
995 ("B" . stgit-branch)
996 ("\C-c\C-b" . stgit-rebase)
997 ("t" . ,toggle-map)
998 ("d" . ,diff-map)
999 ("q" . stgit-quit))))
1000
1001 (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point)))
1002 (and file (eq (stgit-file->status file)
1003 'unmerged))))
1004 (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches)))))
1005 (easy-menu-define stgit-menu stgit-mode-map
1006 "StGit Menu"
1007 `("StGit"
1008 ["Reload" stgit-reload-or-repair
1009 :help "Reload StGit status from disk"]
1010 ["Repair" stgit-repair
1011 :keys "\\[universal-argument] \\[stgit-reload-or-repair]"
1012 :help "Repair StGit metadata"]
1013 "-"
1014 ["Undo" stgit-undo t]
1015 ["Redo" stgit-redo t]
1016 "-"
1017 ["Git status" stgit-git-status :active (fboundp 'git-status)]
1018 "-"
1019 ["New patch" stgit-new-and-refresh
1020 :help "Create a new patch from changes in index or work tree"
1021 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))]
1022 ["New empty patch" stgit-new
1023 :help "Create a new, empty patch"]
1024 ["(Un)mark patch" stgit-toggle-mark
1025 :label (if (memq (stgit-patch-name-at-point nil t)
1026 stgit-marked-patches)
1027 "Unmark patch" "Mark patch")
1028 :active (stgit-patch-name-at-point nil t)]
1029 ["Expand/collapse patch"
1030 (let ((patches (stgit-patches-marked-or-at-point)))
1031 (if (member-if ,patch-collapsed-p patches)
1032 (stgit-expand patches)
1033 (stgit-collapse patches)))
1034 :label (if (member-if ,patch-collapsed-p
1035 (stgit-patches-marked-or-at-point))
1036 "Expand patches"
1037 "Collapse patches")
1038 :active (stgit-patches-marked-or-at-point)]
1039 ["Edit patch" stgit-edit
1040 :help "Edit patch comment"
1041 :active (stgit-patch-name-at-point nil t)]
1042 ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)]
1043 ["Push/pop patch" stgit-push-or-pop
1044 :label (if (subsetp (stgit-patches-marked-or-at-point nil t)
1045 (stgit-applied-patchsyms t))
1046 "Pop patches" "Push patches")]
1047 ["Delete patches" stgit-delete
1048 :active (stgit-patches-marked-or-at-point nil t)]
1049 "-"
1050 ["Move patches" stgit-move-patches
1051 :active stgit-marked-patches
1052 :help "Move marked patch(es) to point"]
1053 ["Squash patches" stgit-squash
1054 :active (> (length stgit-marked-patches) 1)
1055 :help "Merge marked patches into one"]
1056 "-"
1057 ["Refresh top patch" stgit-refresh
1058 :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))
1059 :help "Refresh the top patch with changes in index or work tree"]
1060 ["Refresh this patch" (stgit-refresh t)
1061 :keys "\\[universal-argument] \\[stgit-refresh]"
1062 :help "Refresh marked patch with changes in index or work tree"
1063 :active (and (not (and (stgit-index-empty-p)
1064 (stgit-work-tree-empty-p)))
1065 (stgit-patch-name-at-point nil t))]
1066 "-"
1067 ["Find file" stgit-select
1068 :active (eq (get-text-property (point) 'entry-type) 'file)]
1069 ["Open file" stgit-find-file-other-window
1070 :active (eq (get-text-property (point) 'entry-type) 'file)]
1071 ["Toggle file index" stgit-toggle-index
1072 :active (and (eq (get-text-property (point) 'entry-type) 'file)
1073 (memq (stgit-patch-name-at-point) '(:work :index)))
1074 :label (if (eq (stgit-patch-name-at-point) :work)
1075 "Move change to index"
1076 "Move change to work tree")]
1077 "-"
1078 ["Show diff" stgit-diff
1079 :active (get-text-property (point) 'entry-type)]
1080 ("Merge"
1081 :active (stgit-git-index-unmerged-p)
1082 ["Combined diff" stgit-diff-combined
1083 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1084 ["Diff against base" stgit-diff-base
1085 :help "Show diff against the common base"
1086 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1087 ["Diff against ours" stgit-diff-ours
1088 :help "Show diff against our branch"
1089 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1090 ["Diff against theirs" stgit-diff-theirs
1091 :help "Show diff against their branch"
1092 :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))]
1093 "-"
1094 ["Interactive merge" stgit-find-file-merge
1095 :help "Interactively merge the file"
1096 :active ,at-unmerged-file]
1097 ["Resolve file" stgit-resolve-file
1098 :help "Mark file conflict as resolved"
1099 :active ,at-unmerged-file]
1100 )
1101 "-"
1102 ["Show index & work tree" stgit-toggle-worktree :style toggle
1103 :selected stgit-show-worktree]
1104 ["Show unknown files" stgit-toggle-unknown :style toggle
1105 :selected stgit-show-unknown :active stgit-show-worktree]
1106 ["Show ignored files" stgit-toggle-ignored :style toggle
1107 :selected stgit-show-ignored :active stgit-show-worktree]
1108 ["Show patch names" stgit-toggle-patch-names :style toggle
1109 :selected stgit-show-patch-names]
1110 "-"
1111 ["Switch branches" stgit-branch t
1112 :help "Switch to or create another branch"]
1113 ["Rebase branch" stgit-rebase t
1114 :help "Rebase the current branch"]
1115 ))))
1116
1117 ;; disable tool bar editing buttons
1118 (put 'stgit-mode 'mode-class 'special)
1119
1120 (defun stgit-mode ()
1121 "Major mode for interacting with StGit.
1122
1123 Start StGit using \\[stgit].
1124
1125 Basic commands:
1126 \\<stgit-mode-map>\
1127 \\[stgit-help] Show this help text
1128 \\[stgit-quit] Hide the StGit buffer
1129 \\[describe-bindings] Show all key bindings
1130
1131 \\[stgit-reload-or-repair] Reload the StGit buffer
1132 \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata
1133
1134 \\[stgit-undo] Undo most recent StGit operation
1135 \\[stgit-redo] Undo recent undo
1136
1137 \\[stgit-git-status] Run `git-status' (if available)
1138
1139 Movement commands:
1140 \\[stgit-previous-line] Move to previous line
1141 \\[stgit-next-line] Move to next line
1142 \\[stgit-previous-patch] Move to previous patch
1143 \\[stgit-next-patch] Move to next patch
1144
1145 \\[stgit-mark-down] Mark patch and move down
1146 \\[stgit-unmark-up] Unmark patch and move up
1147 \\[stgit-unmark-down] Unmark patch and move down
1148
1149 Commands for patches:
1150 \\[stgit-select] Toggle showing changed files in patch
1151 \\[stgit-refresh] Refresh patch with changes in index or work tree
1152 \\[stgit-diff] Show the patch log and diff
1153
1154 \\[stgit-expand] Show changes in marked patches
1155 \\[stgit-collapse] Hide changes in marked patches
1156
1157 \\[stgit-new-and-refresh] Create a new patch from index or work tree
1158 \\[stgit-new] Create a new, empty patch
1159
1160 \\[stgit-rename] Rename patch
1161 \\[stgit-edit] Edit patch description
1162 \\[stgit-delete] Delete patch(es)
1163
1164 \\[stgit-revert] Revert all changes in index or work tree
1165 \\[stgit-toggle-index] Toggle all changes between index and work tree
1166
1167 \\[stgit-push-next] Push next patch onto stack
1168 \\[stgit-pop-next] Pop current patch from stack
1169 \\[stgit-push-or-pop] Push or pop marked patches
1170 \\[stgit-goto] Make patch at point current by popping or pushing
1171
1172 \\[stgit-squash] Squash (meld together) patches
1173 \\[stgit-move-patches] Move marked patches to point
1174
1175 \\[stgit-commit] Commit patch(es)
1176 \\[stgit-uncommit] Uncommit patch(es)
1177
1178 Commands for files:
1179 \\[stgit-select] Open the file in this window
1180 \\[stgit-find-file-other-window] Open the file in another window
1181 \\[stgit-diff] Show the file's diff
1182
1183 \\[stgit-toggle-index] Toggle change between index and work tree
1184 \\[stgit-revert] Revert changes to file
1185
1186 Display commands:
1187 \\[stgit-toggle-patch-names] Toggle showing patch names
1188 \\[stgit-toggle-worktree] Toggle showing index and work tree
1189 \\[stgit-toggle-unknown] Toggle showing unknown files
1190 \\[stgit-toggle-ignored] Toggle showing ignored files
1191
1192 Commands for diffs:
1193 \\[stgit-diff] Show diff of patch or file
1194 \\[stgit-diff-base] Show diff against the merge base
1195 \\[stgit-diff-ours] Show diff against our branch
1196 \\[stgit-diff-theirs] Show diff against their branch
1197
1198 With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \
1199 ignore space changes.
1200 With two prefix arguments (e.g., \\[universal-argument] \
1201 \\[universal-argument] \\[stgit-diff]), ignore all space changes.
1202
1203 Commands for merge conflicts:
1204 \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff'
1205 \\[stgit-resolve-file] Mark unmerged file as resolved
1206
1207 Commands for branches:
1208 \\[stgit-branch] Switch to or create another branch
1209 \\[stgit-rebase] Rebase the current branch
1210
1211 Customization variables:
1212 `stgit-abbreviate-copies-and-renames'
1213 `stgit-default-show-patch-names'
1214 `stgit-default-show-worktree'
1215 `stgit-find-copies-harder'
1216 `stgit-show-worktree-mode'
1217
1218 See also \\[customize-group] for the \"stgit\" group."
1219 (kill-all-local-variables)
1220 (buffer-disable-undo)
1221 (setq mode-name "StGit"
1222 major-mode 'stgit-mode
1223 goal-column 2)
1224 (use-local-map stgit-mode-map)
1225 (set (make-local-variable 'list-buffers-directory) default-directory)
1226 (set (make-local-variable 'stgit-marked-patches) nil)
1227 (set (make-local-variable 'stgit-expanded-patches) (list :work :index))
1228 (set (make-local-variable 'stgit-show-patch-names)
1229 stgit-default-show-patch-names)
1230 (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree)
1231 (set (make-local-variable 'stgit-index-node) nil)
1232 (set (make-local-variable 'stgit-worktree-node) nil)
1233 (set (make-local-variable 'parse-sexp-lookup-properties) t)
1234 (set-variable 'truncate-lines 't)
1235 (add-hook 'after-save-hook 'stgit-update-saved-file)
1236 (run-hooks 'stgit-mode-hook))
1237
1238 (defun stgit-update-saved-file ()
1239 (let* ((file (expand-file-name buffer-file-name))
1240 (dir (file-name-directory file))
1241 (gitdir (condition-case nil (git-get-top-dir dir)
1242 (error nil)))
1243 (buffer (and gitdir (stgit-find-buffer gitdir))))
1244 (when buffer
1245 (with-current-buffer buffer
1246 (stgit-refresh-worktree)))))
1247
1248 (defun stgit-add-mark (patchsym)
1249 "Mark the patch PATCHSYM."
1250 (setq stgit-marked-patches (cons patchsym stgit-marked-patches)))
1251
1252 (defun stgit-remove-mark (patchsym)
1253 "Unmark the patch PATCHSYM."
1254 (setq stgit-marked-patches (delq patchsym stgit-marked-patches)))
1255
1256 (defun stgit-clear-marks ()
1257 "Unmark all patches."
1258 (setq stgit-marked-patches '()))
1259
1260 (defun stgit-patch-at-point (&optional cause-error)
1261 (get-text-property (point) 'patch-data))
1262
1263 (defun stgit-patch-name-at-point (&optional cause-error only-patches)
1264 "Return the patch name on the current line as a symbol.
1265 If CAUSE-ERROR is not nil, signal an error if none found.
1266 If ONLY-PATCHES is not nil, only allow real patches, and not
1267 index or work tree."
1268 (let ((patch (stgit-patch-at-point)))
1269 (and patch
1270 only-patches
1271 (memq (stgit-patch->status patch) '(work index))
1272 (setq patch nil))
1273 (cond (patch
1274 (stgit-patch->name patch))
1275 (cause-error
1276 (error "No patch on this line")))))
1277
1278 (defun stgit-patched-file-at-point ()
1279 (get-text-property (point) 'file-data))
1280
1281 (defun stgit-patches-marked-or-at-point (&optional cause-error only-patches)
1282 "Return the symbols of the marked patches, or the patch on the current line.
1283 If CAUSE-ERRROR is not nil, signal an error if none found.
1284 If ONLY-PATCHES is not nil, do not include index or work tree."
1285 (if stgit-marked-patches
1286 stgit-marked-patches
1287 (let ((patch (stgit-patch-name-at-point nil only-patches)))
1288 (cond (patch (list patch))
1289 (cause-error (error "No patches marked or at this line"))
1290 (t nil)))))
1291
1292 (defun stgit-goto-patch (patchsym &optional file)
1293 "Move point to the line containing patch PATCHSYM.
1294 If that patch cannot be found, do nothing.
1295
1296 If the patch was found and FILE is not nil, instead move to that
1297 file's line. If FILE cannot be found, stay on the line of
1298 PATCHSYM."
1299 (let ((node (ewoc-nth stgit-ewoc 0)))
1300 (while (and node (not (eq (stgit-patch->name (ewoc-data node))
1301 patchsym)))
1302 (setq node (ewoc-next stgit-ewoc node)))
1303 (when (and node file)
1304 (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node)))
1305 (file-node (ewoc-nth file-ewoc 0)))
1306 (while (and file-node (not (equal (stgit-file->file (ewoc-data file-node)) file)))
1307 (setq file-node (ewoc-next file-ewoc file-node)))
1308 (when file-node
1309 (ewoc-goto-node file-ewoc file-node)
1310 (move-to-column (stgit-goal-column))
1311 (setq node nil))))
1312 (when node
1313 (ewoc-goto-node stgit-ewoc node)
1314 (move-to-column goal-column))))
1315
1316 (defun stgit-init ()
1317 "Run stg init."
1318 (interactive)
1319 (stgit-assert-mode)
1320 (stgit-capture-output nil
1321 (stgit-run "init"))
1322 (stgit-reload))
1323
1324 (defun stgit-toggle-mark ()
1325 "Toggle mark on the patch under point."
1326 (interactive)
1327 (stgit-assert-mode)
1328 (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches)
1329 (stgit-unmark)
1330 (stgit-mark)))
1331
1332 (defun stgit-mark ()
1333 "Mark the patch under point."
1334 (interactive)
1335 (stgit-assert-mode)
1336 (let* ((node (ewoc-locate stgit-ewoc))
1337 (patch (ewoc-data node))
1338 (name (stgit-patch->name patch)))
1339 (when (eq name :work)
1340 (error "Cannot mark the work tree"))
1341 (when (eq name :index)
1342 (error "Cannot mark the index"))
1343 (stgit-add-mark (stgit-patch->name patch))
1344 (let ((column (current-column)))
1345 (ewoc-invalidate stgit-ewoc node)
1346 (move-to-column column))))
1347
1348 (defun stgit-mark-down ()
1349 "Mark the patch under point and move to the next patch."
1350 (interactive)
1351 (stgit-mark)
1352 (stgit-next-patch))
1353
1354 (defun stgit-unmark ()
1355 "Remove mark from the patch on the current line."
1356 (interactive)
1357 (stgit-assert-mode)
1358 (let* ((node (ewoc-locate stgit-ewoc))
1359 (patch (ewoc-data node)))
1360 (stgit-remove-mark (stgit-patch->name patch))
1361 (let ((column (current-column)))
1362 (ewoc-invalidate stgit-ewoc node)
1363 (move-to-column column))))
1364
1365 (defun stgit-unmark-up ()
1366 "Remove mark from the patch on the previous line."
1367 (interactive)
1368 (stgit-assert-mode)
1369 (stgit-previous-patch)
1370 (stgit-unmark))
1371
1372 (defun stgit-unmark-down ()
1373 "Remove mark from the patch on the current line."
1374 (interactive)
1375 (stgit-assert-mode)
1376 (stgit-unmark)
1377 (stgit-next-patch))
1378
1379 (defun stgit-rename (name)
1380 "Rename the patch under point to NAME."
1381 (interactive (list
1382 (read-string "Patch name: "
1383 (symbol-name (stgit-patch-name-at-point t t)))))
1384 (stgit-assert-mode)
1385 (let ((old-patchsym (stgit-patch-name-at-point t t)))
1386 (stgit-capture-output nil
1387 (stgit-run "rename" old-patchsym name))
1388 (let ((name-sym (intern name)))
1389 (when (memq old-patchsym stgit-expanded-patches)
1390 (setq stgit-expanded-patches
1391 (cons name-sym (delq old-patchsym stgit-expanded-patches))))
1392 (when (memq old-patchsym stgit-marked-patches)
1393 (setq stgit-marked-patches
1394 (cons name-sym (delq old-patchsym stgit-marked-patches))))
1395 (stgit-reload)
1396 (stgit-goto-patch name-sym))))
1397
1398 (defun stgit-reload-or-repair (repair)
1399 "Update the contents of the StGit buffer (`stgit-reload').
1400
1401 With a prefix argument, repair the StGit metadata if the branch
1402 was modified with git commands (`stgit-repair')."
1403 (interactive "P")
1404 (stgit-assert-mode)
1405 (if repair
1406 (stgit-repair)
1407 (stgit-reload)))
1408
1409 (defun stgit-repair ()
1410 "Run stg repair."
1411 (interactive)
1412 (stgit-assert-mode)
1413 (stgit-capture-output nil
1414 (stgit-run "repair"))
1415 (stgit-reload))
1416
1417 (defun stgit-available-branches (&optional all)
1418 "Returns a list of the names of the available stg branches as strings.
1419
1420 If ALL is not nil, also return non-stgit branches."
1421 (let ((output (with-output-to-string
1422 (stgit-run "branch" "--list")))
1423 (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)"
1424 (if all ?. ?s)))
1425 (start 0)
1426 result)
1427 (while (string-match pattern output start)
1428 (setq result (cons (match-string 1 output) result))
1429 (setq start (match-end 0)))
1430 result))
1431
1432 (defun stgit-branch (branch)
1433 "Switch to or create branch BRANCH."
1434 (interactive (list (completing-read "Switch to branch: "
1435 (stgit-available-branches))))
1436 (stgit-assert-mode)
1437 (when (cond ((equal branch (stgit-current-branch))
1438 (error "Branch is already current"))
1439 ((member branch (stgit-available-branches t))
1440 (stgit-capture-output nil (stgit-run "branch" "--" branch))
1441 t)
1442 ((not (string-match stgit-allowed-branch-name-re branch))
1443 (error "Invalid branch name"))
1444 ((yes-or-no-p (format "Create branch \"%s\"? " branch))
1445 (stgit-capture-output nil (stgit-run "branch" "--create" "--"
1446 branch))
1447 t))
1448 (stgit-reload)))
1449
1450 (defun stgit-available-refs (&optional omit-stgit)
1451 "Returns a list of the available git refs.
1452 If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"."
1453 (let* ((output (with-output-to-string
1454 (stgit-run-git-silent "for-each-ref" "--format=%(refname)"
1455 "refs/tags" "refs/heads"
1456 "refs/remotes")))
1457 (result (split-string output "\n" t)))
1458 (mapcar (lambda (s)
1459 (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s)
1460 (substring s (match-end 0))
1461 s))
1462 (if omit-stgit
1463 (delete-if (lambda (s)
1464 (string-match "^refs/heads/.*\\.stgit$" s))
1465 result)
1466 result))))
1467
1468 (defun stgit-parent-branch ()
1469 "Return the parent branch of the current stg branch as per
1470 git-config setting branch.<branch>.stgit.parentbranch."
1471 (let ((output (with-output-to-string
1472 (stgit-run-git-silent "config"
1473 (format "branch.%s.stgit.parentbranch"
1474 (stgit-current-branch))))))
1475 (when (string-match ".*" output)
1476 (match-string 0 output))))
1477
1478 (defun stgit-rebase (new-base)
1479 "Rebase the current branch to NEW-BASE.
1480
1481 Interactively, first ask which branch to rebase to. Defaults to
1482 what git-config branch.<branch>.stgit.parentbranch is set to."
1483 (interactive (list (completing-read "Rebase to: "
1484 (stgit-available-refs t)
1485 nil nil
1486 (stgit-parent-branch))))
1487 (stgit-assert-mode)
1488 (stgit-capture-output nil (stgit-run "rebase" new-base))
1489 (stgit-reload))
1490
1491 (defun stgit-commit (count)
1492 "Run stg commit on COUNT commits.
1493 Interactively, the prefix argument is used as COUNT.
1494 A negative COUNT will uncommit instead."
1495 (interactive "p")
1496 (stgit-assert-mode)
1497 (if (< count 0)
1498 (stgit-uncommit (- count))
1499 (stgit-capture-output nil (stgit-run "commit" "-n" count))
1500 (stgit-reload)))
1501
1502 (defun stgit-uncommit (count)
1503 "Run stg uncommit on COUNT commits.
1504 Interactively, the prefix argument is used as COUNT.
1505 A negative COUNT will commit instead."
1506 (interactive "p")
1507 (stgit-assert-mode)
1508 (if (< count 0)
1509 (stgit-commit (- count))
1510 (stgit-capture-output nil (stgit-run "uncommit" "-n" count))
1511 (stgit-reload)))
1512
1513 (defun stgit-neighbour-file ()
1514 "Return the file name of the next file after point, or the
1515 previous file if point is at the last file within a patch."
1516 (let ((old-point (point))
1517 neighbour-file)
1518 (and (zerop (forward-line 1))
1519 (let ((f (stgit-patched-file-at-point)))
1520 (and f (setq neighbour-file (stgit-file->file f)))))
1521 (goto-char old-point)
1522 (unless neighbour-file
1523 (and (zerop (forward-line -1))
1524 (let ((f (stgit-patched-file-at-point)))
1525 (and f (setq neighbour-file (stgit-file->file f)))))
1526 (goto-char old-point))
1527 neighbour-file))
1528
1529 (defun stgit-revert-file ()
1530 "Revert the file at point, which must be in the index or the
1531 working tree."
1532 (interactive)
1533 (stgit-assert-mode)
1534 (let* ((patched-file (or (stgit-patched-file-at-point)
1535 (error "No file on the current line")))
1536 (patch-name (stgit-patch-name-at-point))
1537 (file-status (stgit-file->status patched-file))
1538 (rm-file (cond ((stgit-file->copy-or-rename patched-file)
1539 (stgit-file->cr-to patched-file))
1540 ((eq file-status 'add)
1541 (stgit-file->file patched-file))))
1542 (co-file (cond ((eq file-status 'rename)
1543 (stgit-file->cr-from patched-file))
1544 ((not (memq file-status '(copy add)))
1545 (stgit-file->file patched-file))))
1546 (next-file (stgit-neighbour-file)))
1547
1548 (unless (memq patch-name '(:work :index))
1549 (error "No index or working tree file on this line"))
1550
1551 (when (eq file-status 'ignore)
1552 (error "Cannot revert ignored files"))
1553
1554 (when (eq file-status 'unknown)
1555 (error "Cannot revert unknown files"))
1556
1557 (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0))))
1558 (when (yes-or-no-p (format "Revert %d file%s? "
1559 nfiles
1560 (if (= nfiles 1) "" "s")))
1561 (stgit-capture-output nil
1562 (when rm-file
1563 (stgit-run-git "rm" "-f" "-q" "--" rm-file))
1564 (when co-file
1565 (stgit-run-git "checkout" "HEAD" co-file)))
1566 (stgit-reload)
1567 (stgit-goto-patch patch-name next-file)))))
1568
1569 (defun stgit-revert ()
1570 "Revert the change at point, which must be the index, the work
1571 tree, or a single change in either."
1572 (interactive)
1573 (stgit-assert-mode)
1574 (let ((patched-file (stgit-patched-file-at-point)))
1575 (if patched-file
1576 (stgit-revert-file)
1577 (let* ((patch-name (or (stgit-patch-name-at-point)
1578 (error "No patch or file at point")))
1579 (patch-desc (case patch-name
1580 (:index "index")
1581 (:work "work tree")
1582 (t (error (substitute-command-keys
1583 "Use \\[stgit-delete] to delete a patch"))))))
1584 (when (if (eq patch-name :work)
1585 (stgit-work-tree-empty-p)
1586 (stgit-index-empty-p))
1587 (error (format "There are no changes in the %s to revert"
1588 patch-desc)))
1589 (and (eq patch-name :index)
1590 (not (stgit-work-tree-empty-p))
1591 (error "Cannot revert index as work tree contains unstaged changes"))
1592
1593 (when (yes-or-no-p (format "Revert all changes in the %s? "
1594 patch-desc))
1595 (if (eq patch-name :index)
1596 (stgit-run-git-silent "reset" "--hard" "-q")
1597 (stgit-run-git-silent "checkout" "--" "."))
1598 (stgit-refresh-index)
1599 (stgit-refresh-worktree)
1600 (stgit-goto-patch patch-name))))))
1601
1602 (defun stgit-resolve-file ()
1603 "Resolve conflict in the file at point."
1604 (interactive)
1605 (stgit-assert-mode)
1606 (let* ((patched-file (stgit-patched-file-at-point))
1607 (patch (stgit-patch-at-point))
1608 (patch-name (and patch (stgit-patch->name patch)))
1609 (status (and patched-file (stgit-file->status patched-file))))
1610
1611 (unless (memq patch-name '(:work :index))
1612 (error "No index or working tree file on this line"))
1613
1614 (unless (eq status 'unmerged)
1615 (error "No conflict to resolve at the current line"))
1616
1617 (stgit-capture-output nil
1618 (stgit-move-change-to-index (stgit-file->file patched-file)))
1619
1620 (stgit-reload)))
1621
1622 (defun stgit-push-next (npatches)
1623 "Push the first unapplied patch.
1624 With numeric prefix argument, push that many patches."
1625 (interactive "p")
1626 (stgit-assert-mode)
1627 (stgit-capture-output nil (stgit-run "push" "-n" npatches))
1628 (stgit-reload)
1629 (stgit-refresh-git-status))
1630
1631 (defun stgit-pop-next (npatches)
1632 "Pop the topmost applied patch.
1633 With numeric prefix argument, pop that many patches."
1634 (interactive "p")
1635 (stgit-assert-mode)
1636 (stgit-capture-output nil (stgit-run "pop" "-n" npatches))
1637 (stgit-reload)
1638 (stgit-refresh-git-status))
1639
1640 (defun stgit-applied-patches (&optional only-patches)
1641 "Return a list of the applied patches.
1642
1643 If ONLY-PATCHES is not nil, exclude index and work tree."
1644 (let ((states (if only-patches
1645 '(applied top)
1646 '(applied top index work)))
1647 result)
1648 (ewoc-map (lambda (patch) (when (memq (stgit-patch->status patch) states)
1649 (setq result (cons patch result))))
1650 stgit-ewoc)
1651 result))
1652
1653 (defun stgit-applied-patchsyms (&optional only-patches)
1654 "Return a list of the symbols of the applied patches.
1655
1656 If ONLY-PATCHES is not nil, exclude index and work tree."
1657 (mapcar #'stgit-patch->name (stgit-applied-patches only-patches)))
1658
1659 (defun stgit-push-or-pop ()
1660 "Push or pop the marked patches."
1661 (interactive)
1662 (stgit-assert-mode)
1663 (let* ((patchsyms (stgit-patches-marked-or-at-point t t))
1664 (applied-syms (stgit-applied-patchsyms t))
1665 (unapplied (set-difference patchsyms applied-syms)))
1666 (stgit-capture-output nil
1667 (apply 'stgit-run
1668 (if unapplied "push" "pop")
1669 "--"
1670 (stgit-sort-patches (if unapplied unapplied patchsyms)))))
1671 (stgit-reload))
1672
1673 (defun stgit-goto ()
1674 "Go to the patch on the current line.
1675
1676 Pops or pushes patches to make this patch topmost."
1677 (interactive)
1678 (stgit-assert-mode)
1679 (let ((patchsym (stgit-patch-name-at-point t)))
1680 (stgit-capture-output nil
1681 (stgit-run "goto" patchsym))
1682 (stgit-reload)))
1683
1684 (defun stgit-id (patchsym)
1685 "Return the git commit id for PATCHSYM.
1686 If PATCHSYM is a keyword, returns PATCHSYM unmodified."
1687 (if (keywordp patchsym)
1688 patchsym
1689 (let ((result (with-output-to-string
1690 (stgit-run-silent "id" patchsym))))
1691 (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result)
1692 (error "Cannot find commit id for %s" patchsym))
1693 (match-string 1 result))))
1694
1695 (defun stgit-show-patch (unmerged-stage ignore-whitespace)
1696 "Show the patch on the current line.
1697
1698 UNMERGED-STAGE is the argument to `git-diff' that that selects
1699 which stage to diff against in the case of unmerged files."
1700 (let ((space-arg (when (numberp ignore-whitespace)
1701 (cond ((> ignore-whitespace 4)
1702 "--ignore-all-space")
1703 ((> ignore-whitespace 1)
1704 "--ignore-space-change"))))
1705 (patch-name (stgit-patch-name-at-point t)))
1706 (stgit-capture-output "*StGit patch*"
1707 (case (get-text-property (point) 'entry-type)
1708 ('file
1709 (let* ((patched-file (stgit-patched-file-at-point))
1710 (patch-id (let ((id (stgit-id patch-name)))
1711 (if (and (eq id :index)
1712 (eq (stgit-file->status patched-file)
1713 'unmerged))
1714 :work
1715 id)))
1716 (args (append (and space-arg (list space-arg))
1717 (and (stgit-file->cr-from patched-file)
1718 (list (stgit-find-copies-harder-diff-arg)))
1719 (cond ((eq patch-id :index)
1720 '("--cached"))
1721 ((eq patch-id :work)
1722 (list unmerged-stage))
1723 (t
1724 (list (concat patch-id "^") patch-id)))
1725 '("--")
1726 (if (stgit-file->copy-or-rename patched-file)
1727 (list (stgit-file->cr-from patched-file)
1728 (stgit-file->cr-to patched-file))
1729 (list (stgit-file->file patched-file))))))
1730 (apply 'stgit-run-git "diff" args)))
1731 ('patch
1732 (let* ((patch-id (stgit-id patch-name)))
1733 (if (or (eq patch-id :index) (eq patch-id :work))
1734 (apply 'stgit-run-git "diff"
1735 (stgit-find-copies-harder-diff-arg)
1736 (append (and space-arg (list space-arg))
1737 (if (eq patch-id :index)
1738 '("--cached")
1739 (list unmerged-stage))))
1740 (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M")
1741 (and space-arg (list "-O" space-arg))
1742 (list (stgit-patch-name-at-point)))))
1743 (apply 'stgit-run args)))))
1744 (t
1745 (error "No patch or file at point")))
1746 (with-current-buffer standard-output
1747 (goto-char (point-min))
1748 (diff-mode)))))
1749
1750 (defmacro stgit-define-diff (name diff-arg &optional unmerged-action)
1751 `(defun ,name (&optional ignore-whitespace)
1752 ,(format "Show the patch on the current line.
1753
1754 %sWith a prefix argument, ignore whitespace. With a prefix argument
1755 greater than four (e.g., \\[universal-argument] \
1756 \\[universal-argument] \\[%s]), ignore all whitespace."
1757 (if unmerged-action
1758 (format "For unmerged files, %s.\n\n" unmerged-action)
1759 "")
1760 name)
1761 (interactive "p")
1762 (stgit-assert-mode)
1763 (stgit-show-patch ,diff-arg ignore-whitespace)))
1764
1765 (stgit-define-diff stgit-diff
1766 "--ours" nil)
1767 (stgit-define-diff stgit-diff-ours
1768 "--ours"
1769 "diff against our branch")
1770 (stgit-define-diff stgit-diff-theirs
1771 "--theirs"
1772 "diff against their branch")
1773 (stgit-define-diff stgit-diff-base
1774 "--base"
1775 "diff against the merge base")
1776 (stgit-define-diff stgit-diff-combined
1777 "--cc"
1778 "show a combined diff")
1779
1780 (defun stgit-move-change-to-index (file &optional force)
1781 "Copies the work tree state of FILE to index, using git add or git rm.
1782
1783 If FORCE is not nil, use --force."
1784 (let ((op (if (or (file-exists-p file) (file-symlink-p file))
1785 '("add") '("rm" "-q"))))
1786 (stgit-capture-output "*git output*"
1787 (apply 'stgit-run-git (append op (and force '("--force"))
1788 '("--") (list file))))))
1789
1790 (defun stgit-remove-change-from-index (file)
1791 "Unstages the change in FILE from the index"
1792 (stgit-capture-output "*git output*"
1793 (stgit-run-git "reset" "-q" "--" file)))
1794
1795 (defun stgit-git-index-unmerged-p ()
1796 (let (result)
1797 (with-output-to-string
1798 (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached"
1799 "--diff-filter=U"
1800 "--quiet" "HEAD")))))
1801 result))
1802
1803 (defun stgit-file-toggle-index ()
1804 "Move modified file in or out of the index.
1805
1806 Leaves the point where it is, but moves the mark to where the
1807 file ended up. You can then jump to the file with \
1808 \\[exchange-point-and-mark]."
1809 (interactive)
1810 (stgit-assert-mode)
1811 (let* ((patched-file (or (stgit-patched-file-at-point)
1812 (error "No file on the current line")))
1813 (patched-status (stgit-file->status patched-file)))
1814 (when (eq patched-status 'unmerged)
1815 (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index")))
1816 (let* ((patch (stgit-patch-at-point))
1817 (patch-name (stgit-patch->name patch))
1818 (mark-file (if (eq patched-status 'rename)
1819 (stgit-file->cr-to patched-file)
1820 (stgit-file->file patched-file)))
1821 (point-file (if (eq patched-status 'rename)
1822 (stgit-file->cr-from patched-file)
1823 (stgit-neighbour-file))))
1824
1825 (cond ((eq patch-name :work)
1826 (stgit-move-change-to-index (stgit-file->file patched-file)
1827 (eq patched-status 'ignore)))
1828 ((eq patch-name :index)
1829 (stgit-remove-change-from-index (stgit-file->file patched-file)))
1830 (t
1831 (error "Can only move files between working tree and index")))
1832 (stgit-refresh-worktree)
1833 (stgit-refresh-index)
1834 (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file)
1835 (push-mark nil t t)
1836 (stgit-goto-patch patch-name point-file))))
1837
1838 (defun stgit-toggle-index ()
1839 "Move change in or out of the index.
1840
1841 Works on index and work tree, as well as files in either.
1842
1843 Leaves the point where it is, but moves the mark to where the
1844 file ended up. You can then jump to the file with \
1845 \\[exchange-point-and-mark]."
1846 (interactive)
1847 (stgit-assert-mode)
1848 (if (stgit-patched-file-at-point)
1849 (stgit-file-toggle-index)
1850 (let ((patch-name (stgit-patch-name-at-point)))
1851 (unless (memq patch-name '(:index :work))
1852 (error "Can only move changes between working tree and index"))
1853 (when (stgit-git-index-unmerged-p)
1854 (error "Resolve unmerged changes with \\[stgit-resolve-file] first"))
1855 (if (if (eq patch-name :index)
1856 (stgit-index-empty-p)
1857 (stgit-work-tree-empty-p))
1858 (message "No changes to be moved")
1859 (stgit-capture-output nil
1860 (if (eq patch-name :work)
1861 (stgit-run-git "add" "--update")
1862 (stgit-run-git "reset" "--mixed" "-q")))
1863 (stgit-refresh-worktree)
1864 (stgit-refresh-index))
1865 (stgit-goto-patch (if (eq patch-name :index) :work :index)))))
1866
1867 (defun stgit-edit ()
1868 "Edit the patch on the current line."
1869 (interactive)
1870 (stgit-assert-mode)
1871 (let ((patchsym (stgit-patch-name-at-point t t))
1872 (edit-buf (get-buffer-create "*StGit edit*"))
1873 (dir default-directory))
1874 (log-edit 'stgit-confirm-edit t nil edit-buf)
1875 (set (make-local-variable 'stgit-edit-patchsym) patchsym)
1876 (setq default-directory dir)
1877 (let ((standard-output edit-buf))
1878 (save-excursion
1879 (stgit-run-silent "edit" "--save-template=-" patchsym)))))
1880
1881 (defun stgit-confirm-edit ()
1882 (interactive)
1883 (let ((file (make-temp-file "stgit-edit-")))
1884 (write-region (point-min) (point-max) file)
1885 (stgit-capture-output nil
1886 (stgit-run "edit" "-f" file stgit-edit-patchsym))
1887 (with-current-buffer log-edit-parent-buffer
1888 (stgit-reload))))
1889
1890 (defun stgit-new (add-sign &optional refresh)
1891 "Create a new patch.
1892 With a prefix argument, include a \"Signed-off-by:\" line at the
1893 end of the patch."
1894 (interactive "P")
1895 (stgit-assert-mode)
1896 (let ((edit-buf (get-buffer-create "*StGit edit*"))
1897 (dir default-directory))
1898 (log-edit 'stgit-confirm-new t nil edit-buf)
1899 (setq default-directory dir)
1900 (set (make-local-variable 'stgit-refresh-after-new) refresh)
1901 (when add-sign
1902 (save-excursion
1903 (let ((standard-output (current-buffer)))
1904 (stgit-run-silent "new" "--sign" "--save-template=-"))))))
1905
1906 (defun stgit-confirm-new ()
1907 (interactive)
1908 (let ((file (make-temp-file "stgit-edit-"))
1909 (refresh stgit-refresh-after-new))
1910 (write-region (point-min) (point-max) file)
1911 (stgit-capture-output nil
1912 (stgit-run "new" "-f" file))
1913 (with-current-buffer log-edit-parent-buffer
1914 (if refresh
1915 (stgit-refresh)
1916 (stgit-reload)))))
1917
1918 (defun stgit-new-and-refresh (add-sign)
1919 "Create a new patch and refresh it with the current changes.
1920
1921 With a prefix argument, include a \"Signed-off-by:\" line at the
1922 end of the patch.
1923
1924 This works just like running `stgit-new' followed by `stgit-refresh'."
1925 (interactive "P")
1926 (stgit-assert-mode)
1927 (stgit-new add-sign t))
1928
1929 (defun stgit-create-patch-name (description)
1930 "Create a patch name from a long description"
1931 (let ((patch ""))
1932 (while (> (length description) 0)
1933 (cond ((string-match "\\`[a-zA-Z_-]+" description)
1934 (setq patch (downcase (concat patch
1935 (match-string 0 description))))
1936 (setq description (substring description (match-end 0))))
1937 ((string-match "\\` +" description)
1938 (setq patch (concat patch "-"))
1939 (setq description (substring description (match-end 0))))
1940 ((string-match "\\`[^a-zA-Z_-]+" description)
1941 (setq description (substring description (match-end 0))))))
1942 (cond ((= (length patch) 0)
1943 "patch")
1944 ((> (length patch) 20)
1945 (substring patch 0 20))
1946 (t patch))))
1947
1948 (defun stgit-delete (patchsyms &optional spill-p)
1949 "Delete the patches in PATCHSYMS.
1950 Interactively, delete the marked patches, or the patch at point.
1951
1952 With a prefix argument, or SPILL-P, spill the patch contents to
1953 the work tree and index."
1954 (interactive (list (stgit-patches-marked-or-at-point t t)
1955 current-prefix-arg))
1956 (stgit-assert-mode)
1957 (unless patchsyms
1958 (error "No patches to delete"))
1959 (when (memq :index patchsyms)
1960 (error "Cannot delete the index"))
1961 (when (memq :work patchsyms)
1962 (error "Cannot delete the work tree"))
1963
1964 (let ((npatches (length patchsyms)))
1965 (when (yes-or-no-p (format "Really delete %d patch%s%s? "
1966 npatches
1967 (if (= 1 npatches) "" "es")
1968 (if spill-p
1969 " (spilling contents to index)"
1970 "")))
1971 (let ((args (if spill-p
1972 (cons "--spill" patchsyms)
1973 patchsyms)))
1974 (stgit-capture-output nil
1975 (apply 'stgit-run "delete" args))
1976 (stgit-reload)))))
1977
1978 (defun stgit-move-patches-target ()
1979 "Return the patchsym indicating a target patch for
1980 `stgit-move-patches'.
1981
1982 This is either the first unmarked patch at or after point, or one
1983 of :top and :bottom if the point is after or before the applied
1984 patches."
1985
1986 (save-excursion
1987 (let (result)
1988 (while (not result)
1989 (let ((patchsym (stgit-patch-name-at-point)))
1990 (cond ((memq patchsym '(:work :index)) (setq result :top))
1991 (patchsym (if (memq patchsym stgit-marked-patches)
1992 (stgit-next-patch)
1993 (setq result patchsym)))
1994 ((re-search-backward "^>" nil t) (setq result :top))
1995 (t (setq result :bottom)))))
1996 result)))
1997
1998 (defun stgit-sort-patches (patchsyms)
1999 "Returns the list of patches in PATCHSYMS sorted according to
2000 their position in the patch series, bottommost first.
2001
2002 PATCHSYMS must not contain duplicate entries."
2003 (let (sorted-patchsyms
2004 (series (with-output-to-string
2005 (with-current-buffer standard-output
2006 (stgit-run-silent "series" "--noprefix"))))
2007 start)
2008 (while (string-match "^\\(.+\\)" series start)
2009 (let ((patchsym (intern (match-string 1 series))))
2010 (when (memq patchsym patchsyms)
2011 (setq sorted-patchsyms (cons patchsym sorted-patchsyms))))
2012 (setq start (match-end 0)))
2013 (setq sorted-patchsyms (nreverse sorted-patchsyms))
2014
2015 (unless (= (length patchsyms) (length sorted-patchsyms))
2016 (error "Internal error"))
2017
2018 sorted-patchsyms))
2019
2020 (defun stgit-move-patches (patchsyms target-patch)
2021 "Move the patches in PATCHSYMS to below TARGET-PATCH.
2022 If TARGET-PATCH is :bottom or :top, move the patches to the
2023 bottom or top of the stack, respectively.
2024
2025 Interactively, move the marked patches to where the point is."
2026 (interactive (list stgit-marked-patches
2027 (stgit-move-patches-target)))
2028 (stgit-assert-mode)
2029 (unless patchsyms
2030 (error "Need at least one patch to move"))
2031
2032 (unless target-patch
2033 (error "Point not at a patch"))
2034
2035 ;; need to have patchsyms sorted by position in the stack
2036 (let ((sorted-patchsyms (stgit-sort-patches patchsyms)))
2037 (stgit-capture-output nil
2038 (if (eq target-patch :top)
2039 (apply 'stgit-run "float" sorted-patchsyms)
2040 (apply 'stgit-run
2041 "sink"
2042 (append (unless (eq target-patch :bottom)
2043 (list "--to" target-patch))
2044 '("--")
2045 sorted-patchsyms)))))
2046 (stgit-reload))
2047
2048 (defun stgit-squash (patchsyms)
2049 "Squash the patches in PATCHSYMS.
2050 Interactively, squash the marked patches.
2051
2052 Unless there are any conflicts, the patches will be merged into
2053 one patch, which will occupy the same spot in the series as the
2054 deepest patch had before the squash."
2055 (interactive (list stgit-marked-patches))
2056 (stgit-assert-mode)
2057 (when (< (length patchsyms) 2)
2058 (error "Need at least two patches to squash"))
2059 (let ((stgit-buffer (current-buffer))
2060 (edit-buf (get-buffer-create "*StGit edit*"))
2061 (dir default-directory)
2062 (sorted-patchsyms (stgit-sort-patches patchsyms)))
2063 (log-edit 'stgit-confirm-squash t nil edit-buf)
2064 (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms)
2065 (setq default-directory dir)
2066 (let ((result (let ((standard-output edit-buf))
2067 (save-excursion
2068 (apply 'stgit-run-silent "squash"
2069 "--save-template=-" sorted-patchsyms)))))
2070
2071 ;; stg squash may have reordered the patches or caused conflicts
2072 (with-current-buffer stgit-buffer
2073 (stgit-reload))
2074
2075 (unless (eq 0 result)
2076 (fundamental-mode)
2077 (rename-buffer "*StGit error*")
2078 (resize-temp-buffer-window)
2079 (switch-to-buffer-other-window stgit-buffer)
2080 (error "stg squash failed")))))
2081
2082 (defun stgit-confirm-squash ()
2083 (interactive)
2084 (let ((file (make-temp-file "stgit-edit-")))
2085 (write-region (point-min) (point-max) file)
2086 (stgit-capture-output nil
2087 (apply 'stgit-run "squash" "-f" file stgit-patchsyms))
2088 (with-current-buffer log-edit-parent-buffer
2089 (stgit-clear-marks)
2090 ;; Go to first marked patch and stay there
2091 (goto-char (point-min))
2092 (re-search-forward (concat "^[>+-]\\*") nil t)
2093 (move-to-column goal-column)
2094 (let ((pos (point)))
2095 (stgit-reload)
2096 (goto-char pos)))))
2097
2098 (defun stgit-help ()
2099 "Display help for the StGit mode."
2100 (interactive)
2101 (describe-function 'stgit-mode))
2102
2103 (defun stgit-undo-or-redo (redo hard)
2104 "Run stg undo or, if REDO is non-nil, stg redo.
2105
2106 If HARD is non-nil, use the --hard flag."
2107 (stgit-assert-mode)
2108 (let ((cmd (if redo "redo" "undo")))
2109 (stgit-capture-output nil
2110 (if arg
2111 (when (or (and (stgit-index-empty-p)
2112 (stgit-work-tree-empty-p))
2113 (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? "
2114 cmd)))
2115 (stgit-run cmd "--hard"))
2116 (stgit-run cmd))))
2117 (stgit-reload))
2118
2119 (defun stgit-undo (&optional arg)
2120 "Run stg undo.
2121 With prefix argument, run it with the --hard flag.
2122
2123 See also `stgit-redo'."
2124 (interactive "P")
2125 (stgit-undo-or-redo nil arg))
2126
2127 (defun stgit-redo (&optional arg)
2128 "Run stg redo.
2129 With prefix argument, run it with the --hard flag.
2130
2131 See also `stgit-undo'."
2132 (interactive "P")
2133 (stgit-undo-or-redo t arg))
2134
2135 (defun stgit-refresh (&optional arg)
2136 "Run stg refresh.
2137 If the index contains any changes, only refresh from index.
2138
2139 With prefix argument, refresh the marked patch or the patch under point."
2140 (interactive "P")
2141 (stgit-assert-mode)
2142 (let ((patchargs (if arg
2143 (let ((patches (stgit-patches-marked-or-at-point nil t)))
2144 (when (> (length patches) 1)
2145 (error "Too many patches marked"))
2146 (cons "-p" patches))
2147 nil)))
2148 (unless (stgit-index-empty-p)
2149 (setq patchargs (cons "--index" patchargs)))
2150 (stgit-capture-output nil
2151 (apply 'stgit-run "refresh" patchargs))
2152 (stgit-refresh-git-status))
2153 (stgit-reload))
2154
2155 (defvar stgit-show-worktree nil
2156 "If nil, inhibit showing work tree and index in the stgit buffer.
2157
2158 See also `stgit-show-worktree-mode'.")
2159
2160 (defvar stgit-show-ignored nil
2161 "If nil, inhibit showing files ignored by git.")
2162
2163 (defvar stgit-show-unknown nil
2164 "If nil, inhibit showing files not registered with git.")
2165
2166 (defvar stgit-show-patch-names t
2167 "If nil, inhibit showing patch names.")
2168
2169 (defun stgit-toggle-worktree (&optional arg)
2170 "Toggle the visibility of the work tree.
2171 With ARG, show the work tree if ARG is positive.
2172
2173 Its initial setting is controlled by `stgit-default-show-worktree'.
2174
2175 `stgit-show-worktree-mode' controls where on screen the index and
2176 work tree will show up."
2177 (interactive)
2178 (stgit-assert-mode)
2179 (setq stgit-show-worktree
2180 (if (numberp arg)
2181 (> arg 0)
2182 (not stgit-show-worktree)))
2183 (stgit-reload))
2184
2185 (defun stgit-toggle-ignored (&optional arg)
2186 "Toggle the visibility of files ignored by git in the work
2187 tree. With ARG, show these files if ARG is positive.
2188
2189 Use \\[stgit-toggle-worktree] to show the work tree."
2190 (interactive)
2191 (stgit-assert-mode)
2192 (setq stgit-show-ignored
2193 (if (numberp arg)
2194 (> arg 0)
2195 (not stgit-show-ignored)))
2196 (stgit-reload))
2197
2198 (defun stgit-toggle-unknown (&optional arg)
2199 "Toggle the visibility of files not registered with git in the
2200 work tree. With ARG, show these files if ARG is positive.
2201
2202 Use \\[stgit-toggle-worktree] to show the work tree."
2203 (interactive)
2204 (stgit-assert-mode)
2205 (setq stgit-show-unknown
2206 (if (numberp arg)
2207 (> arg 0)
2208 (not stgit-show-unknown)))
2209 (stgit-reload))
2210
2211 (defun stgit-toggle-patch-names (&optional arg)
2212 "Toggle the visibility of patch names. With ARG, show patch names
2213 if ARG is positive.
2214
2215 The initial setting is controlled by `stgit-default-show-patch-names'."
2216 (interactive)
2217 (stgit-assert-mode)
2218 (setq stgit-show-patch-names
2219 (if (numberp arg)
2220 (> arg 0)
2221 (not stgit-show-patch-names)))
2222 (stgit-reload))
2223
2224 (provide 'stgit)