]>
Commit | Line | Data |
---|---|---|
711fc8f6 AJ |
1 | ;;; git.el --- A user interface for git |
2 | ||
d55552f6 | 3 | ;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org> |
711fc8f6 AJ |
4 | |
5 | ;; Version: 1.0 | |
6 | ||
7 | ;; This program is free software; you can redistribute it and/or | |
8 | ;; modify it under the terms of the GNU General Public License as | |
9 | ;; published by the Free Software Foundation; either version 2 of | |
10 | ;; the License, or (at your option) any later version. | |
11 | ;; | |
12 | ;; This program is distributed in the hope that it will be | |
13 | ;; useful, but WITHOUT ANY WARRANTY; without even the implied | |
14 | ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | ;; PURPOSE. See the GNU General Public License for more details. | |
16 | ;; | |
17 | ;; You should have received a copy of the GNU General Public | |
18 | ;; License along with this program; if not, write to the Free | |
19 | ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
20 | ;; MA 02111-1307 USA | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;; This file contains an interface for the git version control | |
25 | ;; system. It provides easy access to the most frequently used git | |
26 | ;; commands. The user interface is as far as possible identical to | |
27 | ;; that of the PCL-CVS mode. | |
28 | ;; | |
29 | ;; To install: put this file on the load-path and place the following | |
30 | ;; in your .emacs file: | |
31 | ;; | |
32 | ;; (require 'git) | |
33 | ;; | |
34 | ;; To start: `M-x git-status' | |
35 | ;; | |
36 | ;; TODO | |
37 | ;; - portability to XEmacs | |
38 | ;; - better handling of subprocess errors | |
711fc8f6 AJ |
39 | ;; - diff against other branch |
40 | ;; - renaming files from the status buffer | |
711fc8f6 AJ |
41 | ;; - creating tags |
42 | ;; - fetch/pull | |
43 | ;; - switching branches | |
44 | ;; - revlist browser | |
45 | ;; - git-show-branch browser | |
711fc8f6 AJ |
46 | ;; - menus |
47 | ;; | |
48 | ||
49 | (eval-when-compile (require 'cl)) | |
50 | (require 'ewoc) | |
9fa77a51 | 51 | (require 'log-edit) |
18ff365f | 52 | (require 'easymenu) |
711fc8f6 AJ |
53 | |
54 | ||
a79656e6 | 55 | ;;;; Customizations |
711fc8f6 AJ |
56 | ;;;; ------------------------------------------------------------ |
57 | ||
a79656e6 | 58 | (defgroup git nil |
5df52584 AJ |
59 | "A user interface for the git versioning system." |
60 | :group 'tools) | |
a79656e6 AJ |
61 | |
62 | (defcustom git-committer-name nil | |
63 | "User name to use for commits. | |
1b3a6674 JN |
64 | The default is to fall back to the repository config, |
65 | then to `add-log-full-name' and then to `user-full-name'." | |
a79656e6 AJ |
66 | :group 'git |
67 | :type '(choice (const :tag "Default" nil) | |
68 | (string :tag "Name"))) | |
69 | ||
70 | (defcustom git-committer-email nil | |
71 | "Email address to use for commits. | |
1b3a6674 JN |
72 | The default is to fall back to the git repository config, |
73 | then to `add-log-mailing-address' and then to `user-mail-address'." | |
a79656e6 AJ |
74 | :group 'git |
75 | :type '(choice (const :tag "Default" nil) | |
76 | (string :tag "Email"))) | |
77 | ||
14b4f2db | 78 | (defcustom git-commits-coding-system nil |
a79656e6 AJ |
79 | "Default coding system for the log message of git commits." |
80 | :group 'git | |
14b4f2db AJ |
81 | :type '(choice (const :tag "From repository config" nil) |
82 | (coding-system))) | |
a79656e6 AJ |
83 | |
84 | (defcustom git-append-signed-off-by nil | |
85 | "Whether to append a Signed-off-by line to the commit message before editing." | |
86 | :group 'git | |
87 | :type 'boolean) | |
88 | ||
73389f12 AJ |
89 | (defcustom git-reuse-status-buffer t |
90 | "Whether `git-status' should try to reuse an existing buffer | |
91 | if there is already one that displays the same directory." | |
92 | :group 'git | |
93 | :type 'boolean) | |
94 | ||
a79656e6 AJ |
95 | (defcustom git-per-dir-ignore-file ".gitignore" |
96 | "Name of the per-directory ignore file." | |
97 | :group 'git | |
98 | :type 'string) | |
99 | ||
98acc3fa AJ |
100 | (defcustom git-show-uptodate nil |
101 | "Whether to display up-to-date files." | |
102 | :group 'git | |
103 | :type 'boolean) | |
104 | ||
105 | (defcustom git-show-ignored nil | |
106 | "Whether to display ignored files." | |
107 | :group 'git | |
108 | :type 'boolean) | |
109 | ||
110 | (defcustom git-show-unknown t | |
111 | "Whether to display unknown files." | |
112 | :group 'git | |
113 | :type 'boolean) | |
114 | ||
1b3a6674 | 115 | |
711fc8f6 | 116 | (defface git-status-face |
1ff55ff2 DK |
117 | '((((class color) (background light)) (:foreground "purple")) |
118 | (((class color) (background dark)) (:foreground "salmon"))) | |
a79656e6 AJ |
119 | "Git mode face used to highlight added and modified files." |
120 | :group 'git) | |
711fc8f6 AJ |
121 | |
122 | (defface git-unmerged-face | |
1ff55ff2 DK |
123 | '((((class color) (background light)) (:foreground "red" :bold t)) |
124 | (((class color) (background dark)) (:foreground "red" :bold t))) | |
a79656e6 AJ |
125 | "Git mode face used to highlight unmerged files." |
126 | :group 'git) | |
711fc8f6 AJ |
127 | |
128 | (defface git-unknown-face | |
1ff55ff2 DK |
129 | '((((class color) (background light)) (:foreground "goldenrod" :bold t)) |
130 | (((class color) (background dark)) (:foreground "goldenrod" :bold t))) | |
a79656e6 AJ |
131 | "Git mode face used to highlight unknown files." |
132 | :group 'git) | |
711fc8f6 AJ |
133 | |
134 | (defface git-uptodate-face | |
1ff55ff2 DK |
135 | '((((class color) (background light)) (:foreground "grey60")) |
136 | (((class color) (background dark)) (:foreground "grey40"))) | |
a79656e6 AJ |
137 | "Git mode face used to highlight up-to-date files." |
138 | :group 'git) | |
711fc8f6 AJ |
139 | |
140 | (defface git-ignored-face | |
1ff55ff2 DK |
141 | '((((class color) (background light)) (:foreground "grey60")) |
142 | (((class color) (background dark)) (:foreground "grey40"))) | |
a79656e6 AJ |
143 | "Git mode face used to highlight ignored files." |
144 | :group 'git) | |
711fc8f6 AJ |
145 | |
146 | (defface git-mark-face | |
1ff55ff2 DK |
147 | '((((class color) (background light)) (:foreground "red" :bold t)) |
148 | (((class color) (background dark)) (:foreground "tomato" :bold t))) | |
a79656e6 AJ |
149 | "Git mode face used for the file marks." |
150 | :group 'git) | |
711fc8f6 AJ |
151 | |
152 | (defface git-header-face | |
1ff55ff2 DK |
153 | '((((class color) (background light)) (:foreground "blue")) |
154 | (((class color) (background dark)) (:foreground "blue"))) | |
a79656e6 AJ |
155 | "Git mode face used for commit headers." |
156 | :group 'git) | |
711fc8f6 AJ |
157 | |
158 | (defface git-separator-face | |
1ff55ff2 DK |
159 | '((((class color) (background light)) (:foreground "brown")) |
160 | (((class color) (background dark)) (:foreground "brown"))) | |
a79656e6 AJ |
161 | "Git mode face used for commit separator." |
162 | :group 'git) | |
711fc8f6 AJ |
163 | |
164 | (defface git-permission-face | |
1ff55ff2 DK |
165 | '((((class color) (background light)) (:foreground "green" :bold t)) |
166 | (((class color) (background dark)) (:foreground "green" :bold t))) | |
a79656e6 AJ |
167 | "Git mode face used for permission changes." |
168 | :group 'git) | |
711fc8f6 AJ |
169 | |
170 | ||
171 | ;;;; Utilities | |
172 | ;;;; ------------------------------------------------------------ | |
173 | ||
a79656e6 AJ |
174 | (defconst git-log-msg-separator "--- log message follows this line ---") |
175 | ||
9fa77a51 AJ |
176 | (defvar git-log-edit-font-lock-keywords |
177 | `(("^\\(Author:\\|Date:\\|Parent:\\|Signed-off-by:\\)\\(.*\\)$" | |
178 | (1 font-lock-keyword-face) | |
179 | (2 font-lock-function-name-face)) | |
180 | (,(concat "^\\(" (regexp-quote git-log-msg-separator) "\\)$") | |
181 | (1 font-lock-comment-face)))) | |
182 | ||
711fc8f6 AJ |
183 | (defun git-get-env-strings (env) |
184 | "Build a list of NAME=VALUE strings from a list of environment strings." | |
185 | (mapcar (lambda (entry) (concat (car entry) "=" (cdr entry))) env)) | |
186 | ||
187 | (defun git-call-process-env (buffer env &rest args) | |
188 | "Wrapper for call-process that sets environment strings." | |
189 | (if env | |
190 | (apply #'call-process "env" nil buffer nil | |
191 | (append (git-get-env-strings env) (list "git") args)) | |
192 | (apply #'call-process "git" nil buffer nil args))) | |
193 | ||
9de83169 | 194 | (defun git-call-process-env-string (env &rest args) |
1b3a6674 JN |
195 | "Wrapper for call-process that sets environment strings, |
196 | and returns the process output as a string." | |
9de83169 AJ |
197 | (with-temp-buffer |
198 | (and (eq 0 (apply #' git-call-process-env t env args)) | |
199 | (buffer-string)))) | |
200 | ||
711fc8f6 AJ |
201 | (defun git-run-process-region (buffer start end program args) |
202 | "Run a git process with a buffer region as input." | |
203 | (let ((output-buffer (current-buffer)) | |
204 | (dir default-directory)) | |
205 | (with-current-buffer buffer | |
206 | (cd dir) | |
207 | (apply #'call-process-region start end program | |
208 | nil (list output-buffer nil) nil args)))) | |
209 | ||
210 | (defun git-run-command-buffer (buffer-name &rest args) | |
211 | "Run a git command, sending the output to a buffer named BUFFER-NAME." | |
212 | (let ((dir default-directory) | |
213 | (buffer (get-buffer-create buffer-name))) | |
214 | (message "Running git %s..." (car args)) | |
215 | (with-current-buffer buffer | |
216 | (let ((default-directory dir) | |
217 | (buffer-read-only nil)) | |
218 | (erase-buffer) | |
219 | (apply #'git-call-process-env buffer nil args))) | |
220 | (message "Running git %s...done" (car args)) | |
221 | buffer)) | |
222 | ||
711fc8f6 AJ |
223 | (defun git-run-command-region (buffer start end env &rest args) |
224 | "Run a git command with specified buffer region as input." | |
711fc8f6 AJ |
225 | (unless (eq 0 (if env |
226 | (git-run-process-region | |
227 | buffer start end "env" | |
228 | (append (git-get-env-strings env) (list "git") args)) | |
229 | (git-run-process-region | |
230 | buffer start end "git" args))) | |
9f5599b9 | 231 | (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string)))) |
711fc8f6 | 232 | |
d55552f6 AJ |
233 | (defun git-run-hook (hook env &rest args) |
234 | "Run a git hook and display its output if any." | |
235 | (let ((dir default-directory) | |
236 | (hook-name (expand-file-name (concat ".git/hooks/" hook)))) | |
237 | (or (not (file-executable-p hook-name)) | |
238 | (let (status (buffer (get-buffer-create "*Git Hook Output*"))) | |
239 | (with-current-buffer buffer | |
240 | (erase-buffer) | |
241 | (cd dir) | |
242 | (setq status | |
243 | (if env | |
244 | (apply #'call-process "env" nil (list buffer t) nil | |
245 | (append (git-get-env-strings env) (list hook-name) args)) | |
246 | (apply #'call-process hook-name nil (list buffer t) nil args)))) | |
247 | (display-message-or-buffer buffer) | |
248 | (eq 0 status))))) | |
249 | ||
711fc8f6 AJ |
250 | (defun git-get-string-sha1 (string) |
251 | "Read a SHA1 from the specified string." | |
9de83169 AJ |
252 | (and string |
253 | (string-match "[0-9a-f]\\{40\\}" string) | |
254 | (match-string 0 string))) | |
711fc8f6 AJ |
255 | |
256 | (defun git-get-committer-name () | |
257 | "Return the name to use as GIT_COMMITTER_NAME." | |
258 | ; copied from log-edit | |
259 | (or git-committer-name | |
e0d10e1c | 260 | (git-config "user.name") |
711fc8f6 AJ |
261 | (and (boundp 'add-log-full-name) add-log-full-name) |
262 | (and (fboundp 'user-full-name) (user-full-name)) | |
263 | (and (boundp 'user-full-name) user-full-name))) | |
264 | ||
265 | (defun git-get-committer-email () | |
266 | "Return the email address to use as GIT_COMMITTER_EMAIL." | |
267 | ; copied from log-edit | |
268 | (or git-committer-email | |
e0d10e1c | 269 | (git-config "user.email") |
711fc8f6 AJ |
270 | (and (boundp 'add-log-mailing-address) add-log-mailing-address) |
271 | (and (fboundp 'user-mail-address) (user-mail-address)) | |
272 | (and (boundp 'user-mail-address) user-mail-address))) | |
273 | ||
14b4f2db AJ |
274 | (defun git-get-commits-coding-system () |
275 | "Return the coding system to use for commits." | |
276 | (let ((repo-config (git-config "i18n.commitencoding"))) | |
277 | (or git-commits-coding-system | |
278 | (and repo-config | |
279 | (fboundp 'locale-charset-to-coding-system) | |
280 | (locale-charset-to-coding-system repo-config)) | |
281 | 'utf-8))) | |
282 | ||
b704e589 AJ |
283 | (defun git-get-logoutput-coding-system () |
284 | "Return the coding system used for git-log output." | |
285 | (let ((repo-config (or (git-config "i18n.logoutputencoding") | |
286 | (git-config "i18n.commitencoding")))) | |
287 | (or git-commits-coding-system | |
288 | (and repo-config | |
289 | (fboundp 'locale-charset-to-coding-system) | |
290 | (locale-charset-to-coding-system repo-config)) | |
291 | 'utf-8))) | |
292 | ||
711fc8f6 AJ |
293 | (defun git-escape-file-name (name) |
294 | "Escape a file name if necessary." | |
295 | (if (string-match "[\n\t\"\\]" name) | |
296 | (concat "\"" | |
297 | (mapconcat (lambda (c) | |
298 | (case c | |
299 | (?\n "\\n") | |
300 | (?\t "\\t") | |
301 | (?\\ "\\\\") | |
302 | (?\" "\\\"") | |
303 | (t (char-to-string c)))) | |
304 | name "") | |
305 | "\"") | |
306 | name)) | |
307 | ||
9f5599b9 AJ |
308 | (defun git-success-message (text files) |
309 | "Print a success message after having handled FILES." | |
310 | (let ((n (length files))) | |
311 | (if (equal n 1) | |
312 | (message "%s %s" text (car files)) | |
313 | (message "%s %d files" text n)))) | |
314 | ||
711fc8f6 AJ |
315 | (defun git-get-top-dir (dir) |
316 | "Retrieve the top-level directory of a git tree." | |
317 | (let ((cdup (with-output-to-string | |
318 | (with-current-buffer standard-output | |
319 | (cd dir) | |
320 | (unless (eq 0 (call-process "git" nil t nil "rev-parse" "--show-cdup")) | |
321 | (error "cannot find top-level git tree for %s." dir)))))) | |
322 | (expand-file-name (concat (file-name-as-directory dir) | |
323 | (car (split-string cdup "\n")))))) | |
324 | ||
325 | ;stolen from pcl-cvs | |
326 | (defun git-append-to-ignore (file) | |
327 | "Add a file name to the ignore file in its directory." | |
328 | (let* ((fullname (expand-file-name file)) | |
329 | (dir (file-name-directory fullname)) | |
b23761d9 AJ |
330 | (name (file-name-nondirectory fullname)) |
331 | (ignore-name (expand-file-name git-per-dir-ignore-file dir)) | |
332 | (created (not (file-exists-p ignore-name)))) | |
711fc8f6 | 333 | (save-window-excursion |
b23761d9 | 334 | (set-buffer (find-file-noselect ignore-name)) |
711fc8f6 AJ |
335 | (goto-char (point-max)) |
336 | (unless (zerop (current-column)) (insert "\n")) | |
9f56a7fd | 337 | (insert "/" name "\n") |
711fc8f6 | 338 | (sort-lines nil (point-min) (point-max)) |
b23761d9 AJ |
339 | (save-buffer)) |
340 | (when created | |
9f5599b9 | 341 | (git-call-process-env nil nil "update-index" "--add" "--" (file-relative-name ignore-name))) |
93c22eeb | 342 | (git-update-status-files (list (file-relative-name ignore-name)) 'unknown))) |
711fc8f6 | 343 | |
03d311ed AJ |
344 | ; propertize definition for XEmacs, stolen from erc-compat |
345 | (eval-when-compile | |
346 | (unless (fboundp 'propertize) | |
347 | (defun propertize (string &rest props) | |
348 | (let ((string (copy-sequence string))) | |
349 | (while props | |
350 | (put-text-property 0 (length string) (nth 0 props) (nth 1 props) string) | |
351 | (setq props (cddr props))) | |
352 | string)))) | |
711fc8f6 AJ |
353 | |
354 | ;;;; Wrappers for basic git commands | |
355 | ;;;; ------------------------------------------------------------ | |
356 | ||
357 | (defun git-rev-parse (rev) | |
358 | "Parse a revision name and return its SHA1." | |
359 | (git-get-string-sha1 | |
9de83169 | 360 | (git-call-process-env-string nil "rev-parse" rev))) |
711fc8f6 | 361 | |
e0d10e1c | 362 | (defun git-config (key) |
75a8180d | 363 | "Retrieve the value associated to KEY in the git repository config file." |
e0d10e1c | 364 | (let ((str (git-call-process-env-string nil "config" key))) |
75a8180d AJ |
365 | (and str (car (split-string str "\n"))))) |
366 | ||
711fc8f6 AJ |
367 | (defun git-symbolic-ref (ref) |
368 | "Wrapper for the git-symbolic-ref command." | |
9de83169 AJ |
369 | (let ((str (git-call-process-env-string nil "symbolic-ref" ref))) |
370 | (and str (car (split-string str "\n"))))) | |
711fc8f6 | 371 | |
413689d3 | 372 | (defun git-update-ref (ref newval &optional oldval reason) |
711fc8f6 | 373 | "Update a reference by calling git-update-ref." |
413689d3 AJ |
374 | (let ((args (and oldval (list oldval)))) |
375 | (push newval args) | |
376 | (push ref args) | |
377 | (when reason | |
378 | (push reason args) | |
379 | (push "-m" args)) | |
380 | (eq 0 (apply #'git-call-process-env nil nil "update-ref" args)))) | |
711fc8f6 AJ |
381 | |
382 | (defun git-read-tree (tree &optional index-file) | |
383 | "Read a tree into the index file." | |
384 | (apply #'git-call-process-env nil | |
385 | (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil) | |
386 | "read-tree" (if tree (list tree)))) | |
387 | ||
388 | (defun git-write-tree (&optional index-file) | |
389 | "Call git-write-tree and return the resulting tree SHA1 as a string." | |
390 | (git-get-string-sha1 | |
9de83169 | 391 | (git-call-process-env-string (and index-file `(("GIT_INDEX_FILE" . ,index-file))) "write-tree"))) |
711fc8f6 AJ |
392 | |
393 | (defun git-commit-tree (buffer tree head) | |
394 | "Call git-commit-tree with buffer as input and return the resulting commit SHA1." | |
395 | (let ((author-name (git-get-committer-name)) | |
396 | (author-email (git-get-committer-email)) | |
413689d3 | 397 | (subject "commit (initial): ") |
14b4f2db | 398 | author-date log-start log-end args coding-system-for-write) |
711fc8f6 | 399 | (when head |
413689d3 | 400 | (setq subject "commit: ") |
711fc8f6 AJ |
401 | (push "-p" args) |
402 | (push head args)) | |
403 | (with-current-buffer buffer | |
404 | (goto-char (point-min)) | |
405 | (if | |
a79656e6 | 406 | (setq log-start (re-search-forward (concat "^" (regexp-quote git-log-msg-separator) "\n") nil t)) |
711fc8f6 AJ |
407 | (save-restriction |
408 | (narrow-to-region (point-min) log-start) | |
409 | (goto-char (point-min)) | |
410 | (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t) | |
411 | (setq author-name (match-string 1) | |
412 | author-email (match-string 2))) | |
413 | (goto-char (point-min)) | |
414 | (when (re-search-forward "^Date: +\\(.*\\)$" nil t) | |
415 | (setq author-date (match-string 1))) | |
416 | (goto-char (point-min)) | |
417 | (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t) | |
418 | (unless (string-equal head (match-string 1)) | |
413689d3 | 419 | (setq subject "commit (merge): ") |
711fc8f6 AJ |
420 | (push "-p" args) |
421 | (push (match-string 1) args)))) | |
422 | (setq log-start (point-min))) | |
14b4f2db | 423 | (setq log-end (point-max)) |
413689d3 AJ |
424 | (goto-char log-start) |
425 | (when (re-search-forward ".*$" nil t) | |
426 | (setq subject (concat subject (match-string 0)))) | |
14b4f2db | 427 | (setq coding-system-for-write buffer-file-coding-system)) |
413689d3 AJ |
428 | (let ((commit |
429 | (git-get-string-sha1 | |
430 | (with-output-to-string | |
431 | (with-current-buffer standard-output | |
432 | (let ((env `(("GIT_AUTHOR_NAME" . ,author-name) | |
433 | ("GIT_AUTHOR_EMAIL" . ,author-email) | |
434 | ("GIT_COMMITTER_NAME" . ,(git-get-committer-name)) | |
435 | ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email))))) | |
436 | (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env)) | |
437 | (apply #'git-run-command-region | |
438 | buffer log-start log-end env | |
439 | "commit-tree" tree (nreverse args)))))))) | |
440 | (and (git-update-ref "HEAD" commit head subject) | |
441 | commit)))) | |
711fc8f6 AJ |
442 | |
443 | (defun git-empty-db-p () | |
444 | "Check if the git db is empty (no commit done yet)." | |
445 | (not (eq 0 (call-process "git" nil nil nil "rev-parse" "--verify" "HEAD")))) | |
446 | ||
447 | (defun git-get-merge-heads () | |
448 | "Retrieve the merge heads from the MERGE_HEAD file if present." | |
449 | (let (heads) | |
450 | (when (file-readable-p ".git/MERGE_HEAD") | |
451 | (with-temp-buffer | |
452 | (insert-file-contents ".git/MERGE_HEAD" nil nil nil t) | |
453 | (goto-char (point-min)) | |
454 | (while (re-search-forward "[0-9a-f]\\{40\\}" nil t) | |
455 | (push (match-string 0) heads)))) | |
456 | (nreverse heads))) | |
457 | ||
b704e589 AJ |
458 | (defun git-get-commit-description (commit) |
459 | "Get a one-line description of COMMIT." | |
460 | (let ((coding-system-for-read (git-get-logoutput-coding-system))) | |
461 | (let ((descr (git-call-process-env-string nil "log" "--max-count=1" "--pretty=oneline" commit))) | |
462 | (if (and descr (string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr)) | |
463 | (concat (substring (match-string 1 descr) 0 10) " - " (match-string 2 descr)) | |
464 | descr)))) | |
465 | ||
711fc8f6 AJ |
466 | ;;;; File info structure |
467 | ;;;; ------------------------------------------------------------ | |
468 | ||
469 | ; fileinfo structure stolen from pcl-cvs | |
470 | (defstruct (git-fileinfo | |
471 | (:copier nil) | |
472 | (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked)) | |
473 | (:conc-name git-fileinfo->)) | |
474 | marked ;; t/nil | |
475 | state ;; current state | |
476 | name ;; file name | |
477 | old-perm new-perm ;; permission flags | |
478 | rename-state ;; rename or copy state | |
479 | orig-name ;; original name for renames or copies | |
480 | needs-refresh) ;; whether file needs to be refreshed | |
481 | ||
482 | (defvar git-status nil) | |
483 | ||
484 | (defun git-clear-status (status) | |
485 | "Remove everything from the status list." | |
486 | (ewoc-filter status (lambda (info) nil))) | |
487 | ||
72dc52bf AJ |
488 | (defun git-set-fileinfo-state (info state) |
489 | "Set the state of a file info." | |
490 | (unless (eq (git-fileinfo->state info) state) | |
491 | (setf (git-fileinfo->state info) state | |
492 | (git-fileinfo->old-perm info) 0 | |
493 | (git-fileinfo->new-perm info) 0 | |
494 | (git-fileinfo->rename-state info) nil | |
495 | (git-fileinfo->orig-name info) nil | |
496 | (git-fileinfo->needs-refresh info) t))) | |
711fc8f6 | 497 | |
b9b7bab4 AJ |
498 | (defun git-status-filenames-map (status func files &rest args) |
499 | "Apply FUNC to the status files names in the FILES list." | |
1b655040 AJ |
500 | (when files |
501 | (setq files (sort files #'string-lessp)) | |
502 | (let ((file (pop files)) | |
503 | (node (ewoc-nth status 0))) | |
504 | (while (and file node) | |
505 | (let ((info (ewoc-data node))) | |
b9b7bab4 AJ |
506 | (if (string-lessp (git-fileinfo->name info) file) |
507 | (setq node (ewoc-next status node)) | |
508 | (if (string-equal (git-fileinfo->name info) file) | |
509 | (apply func info args)) | |
510 | (setq file (pop files)))))))) | |
511 | ||
512 | (defun git-set-filenames-state (status files state) | |
513 | "Set the state of a list of named files." | |
514 | (when files | |
72dc52bf | 515 | (git-status-filenames-map status #'git-set-fileinfo-state files state) |
1b655040 AJ |
516 | (unless state ;; delete files whose state has been set to nil |
517 | (ewoc-filter status (lambda (info) (git-fileinfo->state info)))))) | |
518 | ||
711fc8f6 AJ |
519 | (defun git-state-code (code) |
520 | "Convert from a string to a added/deleted/modified state." | |
521 | (case (string-to-char code) | |
522 | (?M 'modified) | |
523 | (?? 'unknown) | |
524 | (?A 'added) | |
525 | (?D 'deleted) | |
526 | (?U 'unmerged) | |
527 | (t nil))) | |
528 | ||
529 | (defun git-status-code-as-string (code) | |
530 | "Format a git status code as string." | |
531 | (case code | |
532 | ('modified (propertize "Modified" 'face 'git-status-face)) | |
533 | ('unknown (propertize "Unknown " 'face 'git-unknown-face)) | |
534 | ('added (propertize "Added " 'face 'git-status-face)) | |
535 | ('deleted (propertize "Deleted " 'face 'git-status-face)) | |
536 | ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face)) | |
537 | ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face)) | |
538 | ('ignored (propertize "Ignored " 'face 'git-ignored-face)) | |
539 | (t "? "))) | |
540 | ||
541 | (defun git-rename-as-string (info) | |
542 | "Return a string describing the copy or rename associated with INFO, or an empty string if none." | |
543 | (let ((state (git-fileinfo->rename-state info))) | |
544 | (if state | |
545 | (propertize | |
546 | (concat " (" | |
547 | (if (eq state 'copy) "copied from " | |
c530c5aa AJ |
548 | (if (eq (git-fileinfo->state info) 'added) "renamed from " |
549 | "renamed to ")) | |
711fc8f6 AJ |
550 | (git-escape-file-name (git-fileinfo->orig-name info)) |
551 | ")") 'face 'git-status-face) | |
552 | ""))) | |
553 | ||
554 | (defun git-permissions-as-string (old-perm new-perm) | |
555 | "Format a permission change as string." | |
556 | (propertize | |
557 | (if (or (not old-perm) | |
558 | (not new-perm) | |
18e3e99e | 559 | (eq 0 (logand ?\111 (logxor old-perm new-perm)))) |
711fc8f6 | 560 | " " |
18e3e99e | 561 | (if (eq 0 (logand ?\111 old-perm)) "+x" "-x")) |
711fc8f6 AJ |
562 | 'face 'git-permission-face)) |
563 | ||
564 | (defun git-fileinfo-prettyprint (info) | |
565 | "Pretty-printer for the git-fileinfo structure." | |
03d311ed AJ |
566 | (insert (concat " " (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ") |
567 | " " (git-status-code-as-string (git-fileinfo->state info)) | |
568 | " " (git-permissions-as-string (git-fileinfo->old-perm info) (git-fileinfo->new-perm info)) | |
569 | " " (git-escape-file-name (git-fileinfo->name info)) | |
711fc8f6 AJ |
570 | (git-rename-as-string info)))) |
571 | ||
1b655040 AJ |
572 | (defun git-insert-info-list (status infolist) |
573 | "Insert a list of file infos in the status buffer, replacing existing ones if any." | |
574 | (setq infolist (sort infolist | |
575 | (lambda (info1 info2) | |
576 | (string-lessp (git-fileinfo->name info1) | |
577 | (git-fileinfo->name info2))))) | |
578 | (let ((info (pop infolist)) | |
579 | (node (ewoc-nth status 0))) | |
580 | (while info | |
581 | (setf (git-fileinfo->needs-refresh info) t) | |
582 | (cond ((not node) | |
583 | (ewoc-enter-last status info) | |
584 | (setq info (pop infolist))) | |
585 | ((string-lessp (git-fileinfo->name (ewoc-data node)) | |
586 | (git-fileinfo->name info)) | |
587 | (setq node (ewoc-next status node))) | |
588 | ((string-equal (git-fileinfo->name (ewoc-data node)) | |
589 | (git-fileinfo->name info)) | |
590 | ;; preserve the marked flag | |
591 | (setf (git-fileinfo->marked info) (git-fileinfo->marked (ewoc-data node))) | |
592 | (setf (ewoc-data node) info) | |
593 | (setq info (pop infolist))) | |
594 | (t | |
595 | (ewoc-enter-before status node info) | |
596 | (setq info (pop infolist))))))) | |
93c22eeb AJ |
597 | |
598 | (defun git-run-diff-index (status files) | |
599 | "Run git-diff-index on FILES and parse the results into STATUS. | |
600 | Return the list of files that haven't been handled." | |
1b655040 | 601 | (let (infolist) |
93c22eeb | 602 | (with-temp-buffer |
9f5599b9 | 603 | (apply #'git-call-process-env t nil "diff-index" "-z" "-M" "HEAD" "--" files) |
93c22eeb AJ |
604 | (goto-char (point-min)) |
605 | (while (re-search-forward | |
606 | ":\\([0-7]\\{6\\}\\) \\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\(\\([ADMU]\\)\0\\([^\0]+\\)\\|\\([CR]\\)[0-9]*\0\\([^\0]+\\)\0\\([^\0]+\\)\\)\0" | |
607 | nil t 1) | |
608 | (let ((old-perm (string-to-number (match-string 1) 8)) | |
609 | (new-perm (string-to-number (match-string 2) 8)) | |
610 | (state (or (match-string 4) (match-string 6))) | |
611 | (name (or (match-string 5) (match-string 7))) | |
612 | (new-name (match-string 8))) | |
613 | (if new-name ; copy or rename | |
614 | (if (eq ?C (string-to-char state)) | |
1b655040 AJ |
615 | (push (git-create-fileinfo 'added new-name old-perm new-perm 'copy name) infolist) |
616 | (push (git-create-fileinfo 'deleted name 0 0 'rename new-name) infolist) | |
617 | (push (git-create-fileinfo 'added new-name old-perm new-perm 'rename name) infolist)) | |
618 | (push (git-create-fileinfo (git-state-code state) name old-perm new-perm) infolist)) | |
93c22eeb | 619 | (setq files (delete name files)) |
1b655040 AJ |
620 | (when new-name (setq files (delete new-name files)))))) |
621 | (git-insert-info-list status infolist) | |
622 | files)) | |
711fc8f6 AJ |
623 | |
624 | (defun git-find-status-file (status file) | |
625 | "Find a given file in the status ewoc and return its node." | |
626 | (let ((node (ewoc-nth status 0))) | |
627 | (while (and node (not (string= file (git-fileinfo->name (ewoc-data node))))) | |
628 | (setq node (ewoc-next status node))) | |
629 | node)) | |
630 | ||
93c22eeb AJ |
631 | (defun git-run-ls-files (status files default-state &rest options) |
632 | "Run git-ls-files on FILES and parse the results into STATUS. | |
633 | Return the list of files that haven't been handled." | |
1b655040 | 634 | (let (infolist) |
93c22eeb | 635 | (with-temp-buffer |
9f5599b9 | 636 | (apply #'git-call-process-env t nil "ls-files" "-z" (append options (list "--") files)) |
93c22eeb | 637 | (goto-char (point-min)) |
1b655040 AJ |
638 | (while (re-search-forward "\\([^\0]*\\)\0" nil t 1) |
639 | (let ((name (match-string 1))) | |
640 | (push (git-create-fileinfo default-state name) infolist) | |
641 | (setq files (delete name files))))) | |
642 | (git-insert-info-list status infolist) | |
643 | files)) | |
93c22eeb AJ |
644 | |
645 | (defun git-run-ls-unmerged (status files) | |
646 | "Run git-ls-files -u on FILES and parse the results into STATUS." | |
647 | (with-temp-buffer | |
9f5599b9 | 648 | (apply #'git-call-process-env t nil "ls-files" "-z" "-u" "--" files) |
93c22eeb AJ |
649 | (goto-char (point-min)) |
650 | (let (unmerged-files) | |
651 | (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t) | |
1b655040 AJ |
652 | (push (match-string 1) unmerged-files)) |
653 | (git-set-filenames-state status unmerged-files 'unmerged)))) | |
93c22eeb | 654 | |
274e13e0 AJ |
655 | (defun git-get-exclude-files () |
656 | "Get the list of exclude files to pass to git-ls-files." | |
657 | (let (files | |
658 | (config (git-config "core.excludesfile"))) | |
659 | (when (file-readable-p ".git/info/exclude") | |
660 | (push ".git/info/exclude" files)) | |
661 | (when (and config (file-readable-p config)) | |
662 | (push config files)) | |
663 | files)) | |
664 | ||
98acc3fa AJ |
665 | (defun git-run-ls-files-with-excludes (status files default-state &rest options) |
666 | "Run git-ls-files on FILES with appropriate --exclude-from options." | |
667 | (let ((exclude-files (git-get-exclude-files))) | |
668 | (apply #'git-run-ls-files status files default-state | |
669 | (concat "--exclude-per-directory=" git-per-dir-ignore-file) | |
670 | (append options (mapcar (lambda (f) (concat "--exclude-from=" f)) exclude-files))))) | |
671 | ||
93c22eeb AJ |
672 | (defun git-update-status-files (files &optional default-state) |
673 | "Update the status of FILES from the index." | |
711fc8f6 | 674 | (unless git-status (error "Not in git-status buffer.")) |
98acc3fa AJ |
675 | (unless files |
676 | (when git-show-uptodate (git-run-ls-files git-status nil 'uptodate "-c"))) | |
677 | (let* ((remaining-files | |
93c22eeb | 678 | (if (git-empty-db-p) ; we need some special handling for an empty db |
98acc3fa AJ |
679 | (git-run-ls-files git-status files 'added "-c") |
680 | (git-run-diff-index git-status files)))) | |
681 | (git-run-ls-unmerged git-status files) | |
682 | (when (or remaining-files (and git-show-unknown (not files))) | |
683 | (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'unknown "-o"))) | |
684 | (when (or remaining-files (and git-show-ignored (not files))) | |
685 | (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'ignored "-o" "-i"))) | |
686 | (git-set-filenames-state git-status remaining-files default-state) | |
93c22eeb | 687 | (git-refresh-files) |
98acc3fa | 688 | (git-refresh-ewoc-hf git-status))) |
711fc8f6 AJ |
689 | |
690 | (defun git-marked-files () | |
691 | "Return a list of all marked files, or if none a list containing just the file at cursor position." | |
692 | (unless git-status (error "Not in git-status buffer.")) | |
693 | (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info))) | |
694 | (list (ewoc-data (ewoc-locate git-status))))) | |
695 | ||
696 | (defun git-marked-files-state (&rest states) | |
697 | "Return marked files that are in the specified states." | |
698 | (let ((files (git-marked-files)) | |
699 | result) | |
700 | (dolist (info files) | |
701 | (when (memq (git-fileinfo->state info) states) | |
702 | (push info result))) | |
703 | result)) | |
704 | ||
705 | (defun git-refresh-files () | |
706 | "Refresh all files that need it and clear the needs-refresh flag." | |
707 | (unless git-status (error "Not in git-status buffer.")) | |
708 | (ewoc-map | |
709 | (lambda (info) | |
710 | (let ((refresh (git-fileinfo->needs-refresh info))) | |
711 | (setf (git-fileinfo->needs-refresh info) nil) | |
712 | refresh)) | |
713 | git-status) | |
714 | ; move back to goal column | |
715 | (when goal-column (move-to-column goal-column))) | |
716 | ||
717 | (defun git-refresh-ewoc-hf (status) | |
718 | "Refresh the ewoc header and footer." | |
719 | (let ((branch (git-symbolic-ref "HEAD")) | |
720 | (head (if (git-empty-db-p) "Nothing committed yet" | |
b704e589 | 721 | (git-get-commit-description "HEAD"))) |
711fc8f6 AJ |
722 | (merge-heads (git-get-merge-heads))) |
723 | (ewoc-set-hf status | |
724 | (format "Directory: %s\nBranch: %s\nHead: %s%s\n" | |
725 | default-directory | |
ef08c149 AJ |
726 | (if branch |
727 | (if (string-match "^refs/heads/" branch) | |
728 | (substring branch (match-end 0)) | |
729 | branch) | |
730 | "none (detached HEAD)") | |
711fc8f6 AJ |
731 | head |
732 | (if merge-heads | |
733 | (concat "\nMerging: " | |
b704e589 | 734 | (mapconcat (lambda (str) (git-get-commit-description str)) merge-heads "\n ")) |
711fc8f6 AJ |
735 | "")) |
736 | (if (ewoc-nth status 0) "" " No changes.")))) | |
737 | ||
738 | (defun git-get-filenames (files) | |
739 | (mapcar (lambda (info) (git-fileinfo->name info)) files)) | |
740 | ||
741 | (defun git-update-index (index-file files) | |
742 | "Run git-update-index on a list of files." | |
743 | (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file)))) | |
744 | added deleted modified) | |
745 | (dolist (info files) | |
746 | (case (git-fileinfo->state info) | |
747 | ('added (push info added)) | |
748 | ('deleted (push info deleted)) | |
749 | ('modified (push info modified)))) | |
750 | (when added | |
9f5599b9 | 751 | (apply #'git-call-process-env nil env "update-index" "--add" "--" (git-get-filenames added))) |
711fc8f6 | 752 | (when deleted |
9f5599b9 | 753 | (apply #'git-call-process-env nil env "update-index" "--remove" "--" (git-get-filenames deleted))) |
711fc8f6 | 754 | (when modified |
9f5599b9 | 755 | (apply #'git-call-process-env nil env "update-index" "--" (git-get-filenames modified))))) |
711fc8f6 | 756 | |
d55552f6 AJ |
757 | (defun git-run-pre-commit-hook () |
758 | "Run the pre-commit hook if any." | |
759 | (unless git-status (error "Not in git-status buffer.")) | |
760 | (let ((files (git-marked-files-state 'added 'deleted 'modified))) | |
761 | (or (not files) | |
762 | (not (file-executable-p ".git/hooks/pre-commit")) | |
763 | (let ((index-file (make-temp-file "gitidx"))) | |
764 | (unwind-protect | |
765 | (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}")))) | |
766 | (git-read-tree head-tree index-file) | |
767 | (git-update-index index-file files) | |
768 | (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" . ,index-file)))) | |
769 | (delete-file index-file)))))) | |
770 | ||
711fc8f6 AJ |
771 | (defun git-do-commit () |
772 | "Perform the actual commit using the current buffer as log message." | |
773 | (interactive) | |
774 | (let ((buffer (current-buffer)) | |
775 | (index-file (make-temp-file "gitidx"))) | |
776 | (with-current-buffer log-edit-parent-buffer | |
777 | (if (git-marked-files-state 'unmerged) | |
778 | (message "You cannot commit unmerged files, resolve them first.") | |
779 | (unwind-protect | |
780 | (let ((files (git-marked-files-state 'added 'deleted 'modified)) | |
781 | head head-tree) | |
782 | (unless (git-empty-db-p) | |
783 | (setq head (git-rev-parse "HEAD") | |
784 | head-tree (git-rev-parse "HEAD^{tree}"))) | |
785 | (if files | |
786 | (progn | |
9f5599b9 | 787 | (message "Running git commit...") |
711fc8f6 AJ |
788 | (git-read-tree head-tree index-file) |
789 | (git-update-index nil files) ;update both the default index | |
790 | (git-update-index index-file files) ;and the temporary one | |
791 | (let ((tree (git-write-tree index-file))) | |
792 | (if (or (not (string-equal tree head-tree)) | |
793 | (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? ")) | |
794 | (let ((commit (git-commit-tree buffer tree head))) | |
711fc8f6 | 795 | (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil)) |
2379d61f | 796 | (condition-case nil (delete-file ".git/MERGE_MSG") (error nil)) |
711fc8f6 | 797 | (with-current-buffer buffer (erase-buffer)) |
72dc52bf | 798 | (dolist (info files) (git-set-fileinfo-state info 'uptodate)) |
9f5599b9 | 799 | (git-call-process-env nil nil "rerere") |
d53a3502 | 800 | (git-call-process-env nil nil "gc" "--auto") |
711fc8f6 AJ |
801 | (git-refresh-files) |
802 | (git-refresh-ewoc-hf git-status) | |
d55552f6 AJ |
803 | (message "Committed %s." commit) |
804 | (git-run-hook "post-commit" nil)) | |
711fc8f6 AJ |
805 | (message "Commit aborted.")))) |
806 | (message "No files to commit."))) | |
807 | (delete-file index-file)))))) | |
808 | ||
809 | ||
810 | ;;;; Interactive functions | |
811 | ;;;; ------------------------------------------------------------ | |
812 | ||
813 | (defun git-mark-file () | |
814 | "Mark the file that the cursor is on and move to the next one." | |
815 | (interactive) | |
816 | (unless git-status (error "Not in git-status buffer.")) | |
817 | (let* ((pos (ewoc-locate git-status)) | |
818 | (info (ewoc-data pos))) | |
819 | (setf (git-fileinfo->marked info) t) | |
820 | (ewoc-invalidate git-status pos) | |
821 | (ewoc-goto-next git-status 1))) | |
822 | ||
823 | (defun git-unmark-file () | |
824 | "Unmark the file that the cursor is on and move to the next one." | |
825 | (interactive) | |
826 | (unless git-status (error "Not in git-status buffer.")) | |
827 | (let* ((pos (ewoc-locate git-status)) | |
828 | (info (ewoc-data pos))) | |
829 | (setf (git-fileinfo->marked info) nil) | |
830 | (ewoc-invalidate git-status pos) | |
831 | (ewoc-goto-next git-status 1))) | |
832 | ||
833 | (defun git-unmark-file-up () | |
834 | "Unmark the file that the cursor is on and move to the previous one." | |
835 | (interactive) | |
836 | (unless git-status (error "Not in git-status buffer.")) | |
837 | (let* ((pos (ewoc-locate git-status)) | |
838 | (info (ewoc-data pos))) | |
839 | (setf (git-fileinfo->marked info) nil) | |
840 | (ewoc-invalidate git-status pos) | |
841 | (ewoc-goto-prev git-status 1))) | |
842 | ||
843 | (defun git-mark-all () | |
844 | "Mark all files." | |
845 | (interactive) | |
846 | (unless git-status (error "Not in git-status buffer.")) | |
2f6e86a8 AJ |
847 | (ewoc-map (lambda (info) (unless (git-fileinfo->marked info) |
848 | (setf (git-fileinfo->marked info) t))) git-status) | |
711fc8f6 AJ |
849 | ; move back to goal column after invalidate |
850 | (when goal-column (move-to-column goal-column))) | |
851 | ||
852 | (defun git-unmark-all () | |
853 | "Unmark all files." | |
854 | (interactive) | |
855 | (unless git-status (error "Not in git-status buffer.")) | |
2f6e86a8 AJ |
856 | (ewoc-map (lambda (info) (when (git-fileinfo->marked info) |
857 | (setf (git-fileinfo->marked info) nil) | |
858 | t)) git-status) | |
711fc8f6 AJ |
859 | ; move back to goal column after invalidate |
860 | (when goal-column (move-to-column goal-column))) | |
861 | ||
862 | (defun git-toggle-all-marks () | |
863 | "Toggle all file marks." | |
864 | (interactive) | |
865 | (unless git-status (error "Not in git-status buffer.")) | |
866 | (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status) | |
867 | ; move back to goal column after invalidate | |
868 | (when goal-column (move-to-column goal-column))) | |
869 | ||
870 | (defun git-next-file (&optional n) | |
871 | "Move the selection down N files." | |
872 | (interactive "p") | |
873 | (unless git-status (error "Not in git-status buffer.")) | |
874 | (ewoc-goto-next git-status n)) | |
875 | ||
876 | (defun git-prev-file (&optional n) | |
877 | "Move the selection up N files." | |
878 | (interactive "p") | |
879 | (unless git-status (error "Not in git-status buffer.")) | |
880 | (ewoc-goto-prev git-status n)) | |
881 | ||
8a078c3f AJ |
882 | (defun git-next-unmerged-file (&optional n) |
883 | "Move the selection down N unmerged files." | |
884 | (interactive "p") | |
885 | (unless git-status (error "Not in git-status buffer.")) | |
886 | (let* ((last (ewoc-locate git-status)) | |
887 | (node (ewoc-next git-status last))) | |
888 | (while (and node (> n 0)) | |
889 | (when (eq 'unmerged (git-fileinfo->state (ewoc-data node))) | |
890 | (setq n (1- n)) | |
891 | (setq last node)) | |
892 | (setq node (ewoc-next git-status node))) | |
893 | (ewoc-goto-node git-status last))) | |
894 | ||
895 | (defun git-prev-unmerged-file (&optional n) | |
896 | "Move the selection up N unmerged files." | |
897 | (interactive "p") | |
898 | (unless git-status (error "Not in git-status buffer.")) | |
899 | (let* ((last (ewoc-locate git-status)) | |
900 | (node (ewoc-prev git-status last))) | |
901 | (while (and node (> n 0)) | |
902 | (when (eq 'unmerged (git-fileinfo->state (ewoc-data node))) | |
903 | (setq n (1- n)) | |
904 | (setq last node)) | |
905 | (setq node (ewoc-prev git-status node))) | |
906 | (ewoc-goto-node git-status last))) | |
907 | ||
711fc8f6 AJ |
908 | (defun git-add-file () |
909 | "Add marked file(s) to the index cache." | |
910 | (interactive) | |
568d2cde | 911 | (let ((files (git-get-filenames (git-marked-files-state 'unknown 'ignored)))) |
711fc8f6 | 912 | (unless files |
93c22eeb | 913 | (push (file-relative-name (read-file-name "File to add: " nil nil t)) files)) |
9f5599b9 AJ |
914 | (apply #'git-call-process-env nil nil "update-index" "--add" "--" files) |
915 | (git-update-status-files files 'uptodate) | |
916 | (git-success-message "Added" files))) | |
711fc8f6 AJ |
917 | |
918 | (defun git-ignore-file () | |
919 | "Add marked file(s) to the ignore list." | |
920 | (interactive) | |
93c22eeb | 921 | (let ((files (git-get-filenames (git-marked-files-state 'unknown)))) |
711fc8f6 | 922 | (unless files |
93c22eeb AJ |
923 | (push (file-relative-name (read-file-name "File to ignore: " nil nil t)) files)) |
924 | (dolist (f files) (git-append-to-ignore f)) | |
9f5599b9 AJ |
925 | (git-update-status-files files 'ignored) |
926 | (git-success-message "Ignored" files))) | |
711fc8f6 AJ |
927 | |
928 | (defun git-remove-file () | |
929 | "Remove the marked file(s)." | |
930 | (interactive) | |
568d2cde | 931 | (let ((files (git-get-filenames (git-marked-files-state 'added 'modified 'unknown 'uptodate 'ignored)))) |
711fc8f6 | 932 | (unless files |
93c22eeb | 933 | (push (file-relative-name (read-file-name "File to remove: " nil nil t)) files)) |
711fc8f6 AJ |
934 | (if (yes-or-no-p |
935 | (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" ""))) | |
936 | (progn | |
93c22eeb AJ |
937 | (dolist (name files) |
938 | (when (file-exists-p name) (delete-file name))) | |
9f5599b9 AJ |
939 | (apply #'git-call-process-env nil nil "update-index" "--remove" "--" files) |
940 | (git-update-status-files files nil) | |
941 | (git-success-message "Removed" files)) | |
711fc8f6 AJ |
942 | (message "Aborting")))) |
943 | ||
944 | (defun git-revert-file () | |
945 | "Revert changes to the marked file(s)." | |
946 | (interactive) | |
947 | (let ((files (git-marked-files)) | |
948 | added modified) | |
949 | (when (and files | |
950 | (yes-or-no-p | |
951 | (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" "")))) | |
952 | (dolist (info files) | |
953 | (case (git-fileinfo->state info) | |
93c22eeb AJ |
954 | ('added (push (git-fileinfo->name info) added)) |
955 | ('deleted (push (git-fileinfo->name info) modified)) | |
956 | ('unmerged (push (git-fileinfo->name info) modified)) | |
957 | ('modified (push (git-fileinfo->name info) modified)))) | |
711fc8f6 | 958 | (when added |
9f5599b9 | 959 | (apply #'git-call-process-env nil nil "update-index" "--force-remove" "--" added)) |
711fc8f6 | 960 | (when modified |
9f5599b9 AJ |
961 | (apply #'git-call-process-env nil nil "checkout" "HEAD" modified)) |
962 | (git-update-status-files (append added modified) 'uptodate) | |
3697c5f3 | 963 | (git-success-message "Reverted" (git-get-filenames files))))) |
711fc8f6 AJ |
964 | |
965 | (defun git-resolve-file () | |
966 | "Resolve conflicts in marked file(s)." | |
967 | (interactive) | |
93c22eeb | 968 | (let ((files (git-get-filenames (git-marked-files-state 'unmerged)))) |
711fc8f6 | 969 | (when files |
9f5599b9 AJ |
970 | (apply #'git-call-process-env nil nil "update-index" "--" files) |
971 | (git-update-status-files files 'uptodate) | |
972 | (git-success-message "Resolved" files)))) | |
711fc8f6 AJ |
973 | |
974 | (defun git-remove-handled () | |
975 | "Remove handled files from the status list." | |
976 | (interactive) | |
977 | (ewoc-filter git-status | |
978 | (lambda (info) | |
98acc3fa AJ |
979 | (case (git-fileinfo->state info) |
980 | ('ignored git-show-ignored) | |
981 | ('uptodate git-show-uptodate) | |
982 | ('unknown git-show-unknown) | |
983 | (t t)))) | |
711fc8f6 AJ |
984 | (unless (ewoc-nth git-status 0) ; refresh header if list is empty |
985 | (git-refresh-ewoc-hf git-status))) | |
986 | ||
98acc3fa AJ |
987 | (defun git-toggle-show-uptodate () |
988 | "Toogle the option for showing up-to-date files." | |
989 | (interactive) | |
990 | (if (setq git-show-uptodate (not git-show-uptodate)) | |
991 | (git-refresh-status) | |
992 | (git-remove-handled))) | |
993 | ||
994 | (defun git-toggle-show-ignored () | |
995 | "Toogle the option for showing ignored files." | |
996 | (interactive) | |
997 | (if (setq git-show-ignored (not git-show-ignored)) | |
998 | (progn | |
9f5599b9 | 999 | (message "Inserting ignored files...") |
98acc3fa AJ |
1000 | (git-run-ls-files-with-excludes git-status nil 'ignored "-o" "-i") |
1001 | (git-refresh-files) | |
9f5599b9 AJ |
1002 | (git-refresh-ewoc-hf git-status) |
1003 | (message "Inserting ignored files...done")) | |
98acc3fa AJ |
1004 | (git-remove-handled))) |
1005 | ||
1006 | (defun git-toggle-show-unknown () | |
1007 | "Toogle the option for showing unknown files." | |
1008 | (interactive) | |
1009 | (if (setq git-show-unknown (not git-show-unknown)) | |
1010 | (progn | |
9f5599b9 | 1011 | (message "Inserting unknown files...") |
98acc3fa AJ |
1012 | (git-run-ls-files-with-excludes git-status nil 'unknown "-o") |
1013 | (git-refresh-files) | |
9f5599b9 AJ |
1014 | (git-refresh-ewoc-hf git-status) |
1015 | (message "Inserting unknown files...done")) | |
98acc3fa AJ |
1016 | (git-remove-handled))) |
1017 | ||
711fc8f6 AJ |
1018 | (defun git-setup-diff-buffer (buffer) |
1019 | "Setup a buffer for displaying a diff." | |
8fdc3972 AJ |
1020 | (let ((dir default-directory)) |
1021 | (with-current-buffer buffer | |
1022 | (diff-mode) | |
1023 | (goto-char (point-min)) | |
1024 | (setq default-directory dir) | |
1025 | (setq buffer-read-only t))) | |
711fc8f6 | 1026 | (display-buffer buffer) |
8b30aa50 AJ |
1027 | ; shrink window only if it displays the status buffer |
1028 | (when (eq (window-buffer) (current-buffer)) | |
1029 | (shrink-window-if-larger-than-buffer))) | |
711fc8f6 AJ |
1030 | |
1031 | (defun git-diff-file () | |
1032 | "Diff the marked file(s) against HEAD." | |
1033 | (interactive) | |
1034 | (let ((files (git-marked-files))) | |
1035 | (git-setup-diff-buffer | |
1036 | (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files))))) | |
1037 | ||
2b1c0ef2 AJ |
1038 | (defun git-diff-file-merge-head (arg) |
1039 | "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)." | |
1040 | (interactive "p") | |
1041 | (let ((files (git-marked-files)) | |
1042 | (merge-heads (git-get-merge-heads))) | |
1043 | (unless merge-heads (error "No merge in progress")) | |
1044 | (git-setup-diff-buffer | |
1045 | (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" | |
1046 | (or (nth (1- arg) merge-heads) "HEAD") "--" (git-get-filenames files))))) | |
1047 | ||
711fc8f6 AJ |
1048 | (defun git-diff-unmerged-file (stage) |
1049 | "Diff the marked unmerged file(s) against the specified stage." | |
1050 | (let ((files (git-marked-files))) | |
1051 | (git-setup-diff-buffer | |
1052 | (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files))))) | |
1053 | ||
1054 | (defun git-diff-file-base () | |
1055 | "Diff the marked unmerged file(s) against the common base file." | |
1056 | (interactive) | |
1057 | (git-diff-unmerged-file "-1")) | |
1058 | ||
1059 | (defun git-diff-file-mine () | |
1060 | "Diff the marked unmerged file(s) against my pre-merge version." | |
1061 | (interactive) | |
1062 | (git-diff-unmerged-file "-2")) | |
1063 | ||
1064 | (defun git-diff-file-other () | |
1065 | "Diff the marked unmerged file(s) against the other's pre-merge version." | |
1066 | (interactive) | |
1067 | (git-diff-unmerged-file "-3")) | |
1068 | ||
1069 | (defun git-diff-file-combined () | |
1070 | "Do a combined diff of the marked unmerged file(s)." | |
1071 | (interactive) | |
1072 | (git-diff-unmerged-file "-c")) | |
1073 | ||
1074 | (defun git-diff-file-idiff () | |
1075 | "Perform an interactive diff on the current file." | |
1076 | (interactive) | |
09afcd69 AJ |
1077 | (let ((files (git-marked-files-state 'added 'deleted 'modified))) |
1078 | (unless (eq 1 (length files)) | |
1079 | (error "Cannot perform an interactive diff on multiple files.")) | |
1080 | (let* ((filename (car (git-get-filenames files))) | |
1081 | (buff1 (find-file-noselect filename)) | |
1082 | (buff2 (git-run-command-buffer (concat filename ".~HEAD~") "cat-file" "blob" (concat "HEAD:" filename)))) | |
1083 | (ediff-buffers buff1 buff2)))) | |
711fc8f6 AJ |
1084 | |
1085 | (defun git-log-file () | |
1086 | "Display a log of changes to the marked file(s)." | |
1087 | (interactive) | |
1088 | (let* ((files (git-marked-files)) | |
1089 | (coding-system-for-read git-commits-coding-system) | |
1090 | (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files)))) | |
1091 | (with-current-buffer buffer | |
1092 | ; (git-log-mode) FIXME: implement log mode | |
1093 | (goto-char (point-min)) | |
1094 | (setq buffer-read-only t)) | |
1095 | (display-buffer buffer))) | |
1096 | ||
1097 | (defun git-log-edit-files () | |
1098 | "Return a list of marked files for use in the log-edit buffer." | |
1099 | (with-current-buffer log-edit-parent-buffer | |
1100 | (git-get-filenames (git-marked-files-state 'added 'deleted 'modified)))) | |
1101 | ||
8b30aa50 AJ |
1102 | (defun git-log-edit-diff () |
1103 | "Run a diff of the current files being committed from a log-edit buffer." | |
1104 | (with-current-buffer log-edit-parent-buffer | |
1105 | (git-diff-file))) | |
1106 | ||
38448147 AJ |
1107 | (defun git-append-sign-off (name email) |
1108 | "Append a Signed-off-by entry to the current buffer, avoiding duplicates." | |
1109 | (let ((sign-off (format "Signed-off-by: %s <%s>" name email)) | |
1110 | (case-fold-search t)) | |
1111 | (goto-char (point-min)) | |
1112 | (unless (re-search-forward (concat "^" (regexp-quote sign-off)) nil t) | |
1113 | (goto-char (point-min)) | |
1114 | (unless (re-search-forward "^Signed-off-by: " nil t) | |
1115 | (setq sign-off (concat "\n" sign-off))) | |
1116 | (goto-char (point-max)) | |
1117 | (insert sign-off "\n")))) | |
1118 | ||
60fa08ed AJ |
1119 | (defun git-setup-log-buffer (buffer &optional author-name author-email subject date msg) |
1120 | "Setup the log buffer for a commit." | |
711fc8f6 | 1121 | (unless git-status (error "Not in git-status buffer.")) |
60fa08ed | 1122 | (let ((merge-heads (git-get-merge-heads)) |
45033ad9 | 1123 | (dir default-directory) |
38448147 AJ |
1124 | (committer-name (git-get-committer-name)) |
1125 | (committer-email (git-get-committer-email)) | |
45033ad9 | 1126 | (sign-off git-append-signed-off-by)) |
711fc8f6 | 1127 | (with-current-buffer buffer |
60fa08ed AJ |
1128 | (cd dir) |
1129 | (erase-buffer) | |
1130 | (insert | |
1131 | (propertize | |
1132 | (format "Author: %s <%s>\n%s%s" | |
1133 | (or author-name committer-name) | |
1134 | (or author-email committer-email) | |
1135 | (if date (format "Date: %s\n" date) "") | |
1136 | (if merge-heads | |
1137 | (format "Parent: %s\n%s\n" | |
1138 | (git-rev-parse "HEAD") | |
1139 | (mapconcat (lambda (str) (concat "Parent: " str)) merge-heads "\n")) | |
1140 | "")) | |
1141 | 'face 'git-header-face) | |
1142 | (propertize git-log-msg-separator 'face 'git-separator-face) | |
1143 | "\n") | |
1144 | (when subject (insert subject "\n\n")) | |
1145 | (cond (msg (insert msg "\n")) | |
1146 | ((file-readable-p ".dotest/msg") | |
1147 | (insert-file-contents ".dotest/msg")) | |
1148 | ((file-readable-p ".git/MERGE_MSG") | |
1149 | (insert-file-contents ".git/MERGE_MSG"))) | |
1150 | ; delete empty lines at end | |
1151 | (goto-char (point-min)) | |
1152 | (when (re-search-forward "\n+\\'" nil t) | |
1153 | (replace-match "\n" t t)) | |
1154 | (when sign-off (git-append-sign-off committer-name committer-email))))) | |
1155 | ||
1156 | (defun git-commit-file () | |
1157 | "Commit the marked file(s), asking for a commit message." | |
1158 | (interactive) | |
1159 | (unless git-status (error "Not in git-status buffer.")) | |
d55552f6 AJ |
1160 | (when (git-run-pre-commit-hook) |
1161 | (let ((buffer (get-buffer-create "*git-commit*")) | |
1162 | (coding-system (git-get-commits-coding-system)) | |
1163 | author-name author-email subject date) | |
1164 | (when (eq 0 (buffer-size buffer)) | |
1165 | (when (file-readable-p ".dotest/info") | |
1166 | (with-temp-buffer | |
1167 | (insert-file-contents ".dotest/info") | |
1168 | (goto-char (point-min)) | |
1169 | (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t) | |
1170 | (setq author-name (match-string 1)) | |
1171 | (setq author-email (match-string 2))) | |
1172 | (goto-char (point-min)) | |
1173 | (when (re-search-forward "^Subject: \\(.*\\)$" nil t) | |
1174 | (setq subject (match-string 1))) | |
1175 | (goto-char (point-min)) | |
1176 | (when (re-search-forward "^Date: \\(.*\\)$" nil t) | |
1177 | (setq date (match-string 1))))) | |
1178 | (git-setup-log-buffer buffer author-name author-email subject date)) | |
8b30aa50 AJ |
1179 | (if (boundp 'log-edit-diff-function) |
1180 | (log-edit 'git-do-commit nil '((log-edit-listfun . git-log-edit-files) | |
1181 | (log-edit-diff-function . git-log-edit-diff)) buffer) | |
1182 | (log-edit 'git-do-commit nil 'git-log-edit-files buffer)) | |
d55552f6 AJ |
1183 | (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords)) |
1184 | (setq buffer-file-coding-system coding-system) | |
1185 | (re-search-forward (regexp-quote (concat git-log-msg-separator "\n")) nil t)))) | |
711fc8f6 AJ |
1186 | |
1187 | (defun git-find-file () | |
1188 | "Visit the current file in its own buffer." | |
1189 | (interactive) | |
1190 | (unless git-status (error "Not in git-status buffer.")) | |
1191 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1192 | (find-file (git-fileinfo->name info)) | |
1193 | (when (eq 'unmerged (git-fileinfo->state info)) | |
ceefa44f | 1194 | (smerge-mode 1)))) |
711fc8f6 | 1195 | |
b8ee5181 AJ |
1196 | (defun git-find-file-other-window () |
1197 | "Visit the current file in its own buffer in another window." | |
1198 | (interactive) | |
1199 | (unless git-status (error "Not in git-status buffer.")) | |
1200 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1201 | (find-file-other-window (git-fileinfo->name info)) | |
1202 | (when (eq 'unmerged (git-fileinfo->state info)) | |
1203 | (smerge-mode)))) | |
1204 | ||
711fc8f6 AJ |
1205 | (defun git-find-file-imerge () |
1206 | "Visit the current file in interactive merge mode." | |
1207 | (interactive) | |
1208 | (unless git-status (error "Not in git-status buffer.")) | |
1209 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1210 | (find-file (git-fileinfo->name info)) | |
1211 | (smerge-ediff))) | |
1212 | ||
1213 | (defun git-view-file () | |
1214 | "View the current file in its own buffer." | |
1215 | (interactive) | |
1216 | (unless git-status (error "Not in git-status buffer.")) | |
1217 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1218 | (view-file (git-fileinfo->name info)))) | |
1219 | ||
1220 | (defun git-refresh-status () | |
1221 | "Refresh the git status buffer." | |
1222 | (interactive) | |
1223 | (let* ((status git-status) | |
1224 | (pos (ewoc-locate status)) | |
b9b7bab4 | 1225 | (marked-files (git-get-filenames (ewoc-collect status (lambda (info) (git-fileinfo->marked info))))) |
711fc8f6 AJ |
1226 | (cur-name (and pos (git-fileinfo->name (ewoc-data pos))))) |
1227 | (unless status (error "Not in git-status buffer.")) | |
9f5599b9 AJ |
1228 | (message "Refreshing git status...") |
1229 | (git-call-process-env nil nil "update-index" "--refresh") | |
711fc8f6 | 1230 | (git-clear-status status) |
93c22eeb | 1231 | (git-update-status-files nil) |
b9b7bab4 AJ |
1232 | ; restore file marks |
1233 | (when marked-files | |
1234 | (git-status-filenames-map status | |
1235 | (lambda (info) | |
1236 | (setf (git-fileinfo->marked info) t) | |
1237 | (setf (git-fileinfo->needs-refresh info) t)) | |
1238 | marked-files) | |
1239 | (git-refresh-files)) | |
711fc8f6 | 1240 | ; move point to the current file name if any |
9f5599b9 | 1241 | (message "Refreshing git status...done") |
711fc8f6 AJ |
1242 | (let ((node (and cur-name (git-find-status-file status cur-name)))) |
1243 | (when node (ewoc-goto-node status node))))) | |
1244 | ||
1245 | (defun git-status-quit () | |
1246 | "Quit git-status mode." | |
1247 | (interactive) | |
1248 | (bury-buffer)) | |
1249 | ||
1250 | ;;;; Major Mode | |
1251 | ;;;; ------------------------------------------------------------ | |
1252 | ||
1253 | (defvar git-status-mode-hook nil | |
1254 | "Run after `git-status-mode' is setup.") | |
1255 | ||
1256 | (defvar git-status-mode-map nil | |
1257 | "Keymap for git major mode.") | |
1258 | ||
1259 | (defvar git-status nil | |
1260 | "List of all files managed by the git-status mode.") | |
1261 | ||
1262 | (unless git-status-mode-map | |
1263 | (let ((map (make-keymap)) | |
98acc3fa AJ |
1264 | (diff-map (make-sparse-keymap)) |
1265 | (toggle-map (make-sparse-keymap))) | |
711fc8f6 | 1266 | (suppress-keymap map) |
5716e794 JN |
1267 | (define-key map "?" 'git-help) |
1268 | (define-key map "h" 'git-help) | |
711fc8f6 AJ |
1269 | (define-key map " " 'git-next-file) |
1270 | (define-key map "a" 'git-add-file) | |
1271 | (define-key map "c" 'git-commit-file) | |
1272 | (define-key map "d" diff-map) | |
1273 | (define-key map "=" 'git-diff-file) | |
1274 | (define-key map "f" 'git-find-file) | |
18e3e99e | 1275 | (define-key map "\r" 'git-find-file) |
711fc8f6 AJ |
1276 | (define-key map "g" 'git-refresh-status) |
1277 | (define-key map "i" 'git-ignore-file) | |
1278 | (define-key map "l" 'git-log-file) | |
1279 | (define-key map "m" 'git-mark-file) | |
1280 | (define-key map "M" 'git-mark-all) | |
1281 | (define-key map "n" 'git-next-file) | |
8a078c3f | 1282 | (define-key map "N" 'git-next-unmerged-file) |
b8ee5181 | 1283 | (define-key map "o" 'git-find-file-other-window) |
711fc8f6 | 1284 | (define-key map "p" 'git-prev-file) |
8a078c3f | 1285 | (define-key map "P" 'git-prev-unmerged-file) |
711fc8f6 AJ |
1286 | (define-key map "q" 'git-status-quit) |
1287 | (define-key map "r" 'git-remove-file) | |
1288 | (define-key map "R" 'git-resolve-file) | |
98acc3fa | 1289 | (define-key map "t" toggle-map) |
711fc8f6 AJ |
1290 | (define-key map "T" 'git-toggle-all-marks) |
1291 | (define-key map "u" 'git-unmark-file) | |
1292 | (define-key map "U" 'git-revert-file) | |
1293 | (define-key map "v" 'git-view-file) | |
1294 | (define-key map "x" 'git-remove-handled) | |
1295 | (define-key map "\C-?" 'git-unmark-file-up) | |
1296 | (define-key map "\M-\C-?" 'git-unmark-all) | |
1297 | ; the diff submap | |
1298 | (define-key diff-map "b" 'git-diff-file-base) | |
1299 | (define-key diff-map "c" 'git-diff-file-combined) | |
1300 | (define-key diff-map "=" 'git-diff-file) | |
1301 | (define-key diff-map "e" 'git-diff-file-idiff) | |
1302 | (define-key diff-map "E" 'git-find-file-imerge) | |
2b1c0ef2 | 1303 | (define-key diff-map "h" 'git-diff-file-merge-head) |
711fc8f6 AJ |
1304 | (define-key diff-map "m" 'git-diff-file-mine) |
1305 | (define-key diff-map "o" 'git-diff-file-other) | |
98acc3fa AJ |
1306 | ; the toggle submap |
1307 | (define-key toggle-map "u" 'git-toggle-show-uptodate) | |
1308 | (define-key toggle-map "i" 'git-toggle-show-ignored) | |
1309 | (define-key toggle-map "k" 'git-toggle-show-unknown) | |
1310 | (define-key toggle-map "m" 'git-toggle-all-marks) | |
18ff365f AJ |
1311 | (setq git-status-mode-map map)) |
1312 | (easy-menu-define git-menu git-status-mode-map | |
1313 | "Git Menu" | |
1314 | `("Git" | |
1315 | ["Refresh" git-refresh-status t] | |
1316 | ["Commit" git-commit-file t] | |
1317 | ("Merge" | |
1318 | ["Next Unmerged File" git-next-unmerged-file t] | |
1319 | ["Prev Unmerged File" git-prev-unmerged-file t] | |
1320 | ["Mark as Resolved" git-resolve-file t] | |
1321 | ["Interactive Merge File" git-find-file-imerge t] | |
1322 | ["Diff Against Common Base File" git-diff-file-base t] | |
1323 | ["Diff Combined" git-diff-file-combined t] | |
1324 | ["Diff Against Merge Head" git-diff-file-merge-head t] | |
1325 | ["Diff Against Mine" git-diff-file-mine t] | |
1326 | ["Diff Against Other" git-diff-file-other t]) | |
1327 | "--------" | |
1328 | ["Add File" git-add-file t] | |
1329 | ["Revert File" git-revert-file t] | |
1330 | ["Ignore File" git-ignore-file t] | |
1331 | ["Remove File" git-remove-file t] | |
1332 | "--------" | |
1333 | ["Find File" git-find-file t] | |
1334 | ["View File" git-view-file t] | |
1335 | ["Diff File" git-diff-file t] | |
1336 | ["Interactive Diff File" git-diff-file-idiff t] | |
1337 | ["Log" git-log-file t] | |
1338 | "--------" | |
1339 | ["Mark" git-mark-file t] | |
1340 | ["Mark All" git-mark-all t] | |
1341 | ["Unmark" git-unmark-file t] | |
1342 | ["Unmark All" git-unmark-all t] | |
1343 | ["Toggle All Marks" git-toggle-all-marks t] | |
1344 | ["Hide Handled Files" git-remove-handled t] | |
1345 | "--------" | |
1346 | ["Show Uptodate Files" git-toggle-show-uptodate :style toggle :selected git-show-uptodate] | |
1347 | ["Show Ignored Files" git-toggle-show-ignored :style toggle :selected git-show-ignored] | |
1348 | ["Show Unknown Files" git-toggle-show-unknown :style toggle :selected git-show-unknown] | |
1349 | "--------" | |
1350 | ["Quit" git-status-quit t]))) | |
1351 | ||
711fc8f6 AJ |
1352 | |
1353 | ;; git mode should only run in the *git status* buffer | |
1354 | (put 'git-status-mode 'mode-class 'special) | |
1355 | ||
1356 | (defun git-status-mode () | |
1357 | "Major mode for interacting with Git. | |
1358 | Commands: | |
1359 | \\{git-status-mode-map}" | |
1360 | (kill-all-local-variables) | |
1361 | (buffer-disable-undo) | |
1362 | (setq mode-name "git status" | |
1363 | major-mode 'git-status-mode | |
1364 | goal-column 17 | |
1365 | buffer-read-only t) | |
1366 | (use-local-map git-status-mode-map) | |
1367 | (let ((buffer-read-only nil)) | |
1368 | (erase-buffer) | |
1369 | (let ((status (ewoc-create 'git-fileinfo-prettyprint "" ""))) | |
1370 | (set (make-local-variable 'git-status) status)) | |
a944652c | 1371 | (set (make-local-variable 'list-buffers-directory) default-directory) |
98acc3fa AJ |
1372 | (make-local-variable 'git-show-uptodate) |
1373 | (make-local-variable 'git-show-ignored) | |
1374 | (make-local-variable 'git-show-unknown) | |
711fc8f6 AJ |
1375 | (run-hooks 'git-status-mode-hook))) |
1376 | ||
73389f12 AJ |
1377 | (defun git-find-status-buffer (dir) |
1378 | "Find the git status buffer handling a specified directory." | |
1379 | (let ((list (buffer-list)) | |
1380 | (fulldir (expand-file-name dir)) | |
1381 | found) | |
1382 | (while (and list (not found)) | |
1383 | (let ((buffer (car list))) | |
1384 | (with-current-buffer buffer | |
1385 | (when (and list-buffers-directory | |
1386 | (string-equal fulldir (expand-file-name list-buffers-directory)) | |
1387 | (string-match "\\*git-status\\*$" (buffer-name buffer))) | |
1388 | (setq found buffer)))) | |
1389 | (setq list (cdr list))) | |
1390 | found)) | |
1391 | ||
711fc8f6 AJ |
1392 | (defun git-status (dir) |
1393 | "Entry point into git-status mode." | |
1394 | (interactive "DSelect directory: ") | |
1395 | (setq dir (git-get-top-dir dir)) | |
1396 | (if (file-directory-p (concat (file-name-as-directory dir) ".git")) | |
73389f12 AJ |
1397 | (let ((buffer (or (and git-reuse-status-buffer (git-find-status-buffer dir)) |
1398 | (create-file-buffer (expand-file-name "*git-status*" dir))))) | |
711fc8f6 | 1399 | (switch-to-buffer buffer) |
711fc8f6 | 1400 | (cd dir) |
a944652c | 1401 | (git-status-mode) |
711fc8f6 | 1402 | (git-refresh-status) |
0365d885 AJ |
1403 | (goto-char (point-min)) |
1404 | (add-hook 'after-save-hook 'git-update-saved-file)) | |
711fc8f6 AJ |
1405 | (message "%s is not a git working tree." dir))) |
1406 | ||
0365d885 AJ |
1407 | (defun git-update-saved-file () |
1408 | "Update the corresponding git-status buffer when a file is saved. | |
1409 | Meant to be used in `after-save-hook'." | |
1410 | (let* ((file (expand-file-name buffer-file-name)) | |
6df02388 | 1411 | (dir (condition-case nil (git-get-top-dir (file-name-directory file)) (error nil))) |
0365d885 AJ |
1412 | (buffer (and dir (git-find-status-buffer dir)))) |
1413 | (when buffer | |
1414 | (with-current-buffer buffer | |
1415 | (let ((filename (file-relative-name file dir))) | |
1416 | ; skip files located inside the .git directory | |
1417 | (unless (string-match "^\\.git/" filename) | |
1418 | (git-call-process-env nil nil "add" "--refresh" "--" filename) | |
1419 | (git-update-status-files (list filename) 'uptodate))))))) | |
1420 | ||
5716e794 JN |
1421 | (defun git-help () |
1422 | "Display help for Git mode." | |
1423 | (interactive) | |
1424 | (describe-function 'git-status-mode)) | |
1425 | ||
711fc8f6 AJ |
1426 | (provide 'git) |
1427 | ;;; git.el ends here |