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