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