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