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