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