1 ;;; git.el --- A user interface for git
3 ;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org>
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.
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.
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,
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.
29 ;; To install: put this file on the load-path and place the following
30 ;; in your .emacs file:
34 ;; To start: `M-x git-status'
37 ;; - portability to XEmacs
38 ;; - diff against other branch
39 ;; - renaming files from the status buffer
42 ;; - switching branches
44 ;; - git-show-branch browser
48 (eval-when-compile (require 'cl))
55 ;;;; ------------------------------------------------------------
58 "A user interface for the git versioning system."
61 (defcustom git-committer-name nil
62 "User name to use for commits.
63 The default is to fall back to the repository config,
64 then to `add-log-full-name' and then to `user-full-name'."
66 :type '(choice (const :tag "Default" nil)
67 (string :tag "Name")))
69 (defcustom git-committer-email nil
70 "Email address to use for commits.
71 The default is to fall back to the git repository config,
72 then to `add-log-mailing-address' and then to `user-mail-address'."
74 :type '(choice (const :tag "Default" nil)
75 (string :tag "Email")))
77 (defcustom git-commits-coding-system nil
78 "Default coding system for the log message of git commits."
80 :type '(choice (const :tag "From repository config" nil)
83 (defcustom git-append-signed-off-by nil
84 "Whether to append a Signed-off-by line to the commit message before editing."
88 (defcustom git-reuse-status-buffer t
89 "Whether `git-status' should try to reuse an existing buffer
90 if there is already one that displays the same directory."
94 (defcustom git-per-dir-ignore-file ".gitignore"
95 "Name of the per-directory ignore file."
99 (defcustom git-show-uptodate nil
100 "Whether to display up-to-date files."
104 (defcustom git-show-ignored nil
105 "Whether to display ignored files."
109 (defcustom git-show-unknown t
110 "Whether to display unknown files."
115 (defface git-status-face
116 '((((class color) (background light)) (:foreground "purple"))
117 (((class color) (background dark)) (:foreground "salmon")))
118 "Git mode face used to highlight added and modified files."
121 (defface git-unmerged-face
122 '((((class color) (background light)) (:foreground "red" :bold t))
123 (((class color) (background dark)) (:foreground "red" :bold t)))
124 "Git mode face used to highlight unmerged files."
127 (defface git-unknown-face
128 '((((class color) (background light)) (:foreground "goldenrod" :bold t))
129 (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
130 "Git mode face used to highlight unknown files."
133 (defface git-uptodate-face
134 '((((class color) (background light)) (:foreground "grey60"))
135 (((class color) (background dark)) (:foreground "grey40")))
136 "Git mode face used to highlight up-to-date files."
139 (defface git-ignored-face
140 '((((class color) (background light)) (:foreground "grey60"))
141 (((class color) (background dark)) (:foreground "grey40")))
142 "Git mode face used to highlight ignored files."
145 (defface git-mark-face
146 '((((class color) (background light)) (:foreground "red" :bold t))
147 (((class color) (background dark)) (:foreground "tomato" :bold t)))
148 "Git mode face used for the file marks."
151 (defface git-header-face
152 '((((class color) (background light)) (:foreground "blue"))
153 (((class color) (background dark)) (:foreground "blue")))
154 "Git mode face used for commit headers."
157 (defface git-separator-face
158 '((((class color) (background light)) (:foreground "brown"))
159 (((class color) (background dark)) (:foreground "brown")))
160 "Git mode face used for commit separator."
163 (defface git-permission-face
164 '((((class color) (background light)) (:foreground "green" :bold t))
165 (((class color) (background dark)) (:foreground "green" :bold t)))
166 "Git mode face used for permission changes."
171 ;;;; ------------------------------------------------------------
173 (defconst git-log-msg-separator "--- log message follows this line ---")
175 (defvar git-log-edit-font-lock-keywords
176 `(("^\\(Author:\\|Date:\\|Parent:\\|Signed-off-by:\\)\\(.*\\)$"
177 (1 font-lock-keyword-face)
178 (2 font-lock-function-name-face))
179 (,(concat "^\\(" (regexp-quote git-log-msg-separator) "\\)$")
180 (1 font-lock-comment-face))))
182 (defun git-get-env-strings (env)
183 "Build a list of NAME=VALUE strings from a list of environment strings."
184 (mapcar (lambda (entry) (concat (car entry) "=" (cdr entry))) env))
186 (defun git-call-process-env (buffer env &rest args)
187 "Wrapper for call-process that sets environment strings."
188 (let ((process-environment (append (git-get-env-strings env)
189 process-environment)))
190 (apply #'call-process "git" nil buffer nil args)))
192 (defun git-call-process-display-error (&rest args)
193 "Wrapper for call-process that displays error messages."
194 (let* ((dir default-directory)
195 (buffer (get-buffer-create "*Git Command Output*"))
196 (ok (with-current-buffer buffer
197 (let ((default-directory dir)
198 (buffer-read-only nil))
200 (eq 0 (apply 'call-process "git" nil (list buffer t) nil args))))))
201 (unless ok (display-message-or-buffer buffer))
204 (defun git-call-process-env-string (env &rest args)
205 "Wrapper for call-process that sets environment strings,
206 and returns the process output as a string, or nil if the git failed."
208 (and (eq 0 (apply #' git-call-process-env t env args))
211 (defun git-run-process-region (buffer start end program args)
212 "Run a git process with a buffer region as input."
213 (let ((output-buffer (current-buffer))
214 (dir default-directory))
215 (with-current-buffer buffer
217 (apply #'call-process-region start end program
218 nil (list output-buffer nil) nil args))))
220 (defun git-run-command-buffer (buffer-name &rest args)
221 "Run a git command, sending the output to a buffer named BUFFER-NAME."
222 (let ((dir default-directory)
223 (buffer (get-buffer-create buffer-name)))
224 (message "Running git %s..." (car args))
225 (with-current-buffer buffer
226 (let ((default-directory dir)
227 (buffer-read-only nil))
229 (apply #'git-call-process-env buffer nil args)))
230 (message "Running git %s...done" (car args))
233 (defun git-run-command-region (buffer start end env &rest args)
234 "Run a git command with specified buffer region as input."
235 (unless (eq 0 (let ((process-environment (append (git-get-env-strings env)
236 process-environment)))
237 (git-run-process-region
238 buffer start end "git" args)))
239 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string))))
241 (defun git-run-hook (hook env &rest args)
242 "Run a git hook and display its output if any."
243 (let ((dir default-directory)
244 (hook-name (expand-file-name (concat ".git/hooks/" hook))))
245 (or (not (file-executable-p hook-name))
246 (let (status (buffer (get-buffer-create "*Git Hook Output*")))
247 (with-current-buffer buffer
251 (let ((process-environment (append (git-get-env-strings env)
252 process-environment)))
253 (apply #'call-process hook-name nil (list buffer t) nil args))))
254 (display-message-or-buffer buffer)
257 (defun git-get-string-sha1 (string)
258 "Read a SHA1 from the specified string."
260 (string-match "[0-9a-f]\\{40\\}" string)
261 (match-string 0 string)))
263 (defun git-get-committer-name ()
264 "Return the name to use as GIT_COMMITTER_NAME."
265 ; copied from log-edit
266 (or git-committer-name
267 (git-config "user.name")
268 (and (boundp 'add-log-full-name) add-log-full-name)
269 (and (fboundp 'user-full-name) (user-full-name))
270 (and (boundp 'user-full-name) user-full-name)))
272 (defun git-get-committer-email ()
273 "Return the email address to use as GIT_COMMITTER_EMAIL."
274 ; copied from log-edit
275 (or git-committer-email
276 (git-config "user.email")
277 (and (boundp 'add-log-mailing-address) add-log-mailing-address)
278 (and (fboundp 'user-mail-address) (user-mail-address))
279 (and (boundp 'user-mail-address) user-mail-address)))
281 (defun git-get-commits-coding-system ()
282 "Return the coding system to use for commits."
283 (let ((repo-config (git-config "i18n.commitencoding")))
284 (or git-commits-coding-system
286 (fboundp 'locale-charset-to-coding-system)
287 (locale-charset-to-coding-system repo-config))
290 (defun git-get-logoutput-coding-system ()
291 "Return the coding system used for git-log output."
292 (let ((repo-config (or (git-config "i18n.logoutputencoding")
293 (git-config "i18n.commitencoding"))))
294 (or git-commits-coding-system
296 (fboundp 'locale-charset-to-coding-system)
297 (locale-charset-to-coding-system repo-config))
300 (defun git-escape-file-name (name)
301 "Escape a file name if necessary."
302 (if (string-match "[\n\t\"\\]" name)
304 (mapconcat (lambda (c)
310 (t (char-to-string c))))
315 (defun git-success-message (text files)
316 "Print a success message after having handled FILES."
317 (let ((n (length files)))
319 (message "%s %s" text (car files))
320 (message "%s %d files" text n))))
322 (defun git-get-top-dir (dir)
323 "Retrieve the top-level directory of a git tree."
324 (let ((cdup (with-output-to-string
325 (with-current-buffer standard-output
327 (unless (eq 0 (call-process "git" nil t nil "rev-parse" "--show-cdup"))
328 (error "cannot find top-level git tree for %s." dir))))))
329 (expand-file-name (concat (file-name-as-directory dir)
330 (car (split-string cdup "\n"))))))
333 (defun git-append-to-ignore (file)
334 "Add a file name to the ignore file in its directory."
335 (let* ((fullname (expand-file-name file))
336 (dir (file-name-directory fullname))
337 (name (file-name-nondirectory fullname))
338 (ignore-name (expand-file-name git-per-dir-ignore-file dir))
339 (created (not (file-exists-p ignore-name))))
340 (save-window-excursion
341 (set-buffer (find-file-noselect ignore-name))
342 (goto-char (point-max))
343 (unless (zerop (current-column)) (insert "\n"))
344 (insert "/" name "\n")
345 (sort-lines nil (point-min) (point-max))
348 (git-call-process-env nil nil "update-index" "--add" "--" (file-relative-name ignore-name)))
349 (git-update-status-files (list (file-relative-name ignore-name)) 'unknown)))
351 ; propertize definition for XEmacs, stolen from erc-compat
353 (unless (fboundp 'propertize)
354 (defun propertize (string &rest props)
355 (let ((string (copy-sequence string)))
357 (put-text-property 0 (length string) (nth 0 props) (nth 1 props) string)
358 (setq props (cddr props)))
361 ;;;; Wrappers for basic git commands
362 ;;;; ------------------------------------------------------------
364 (defun git-rev-parse (rev)
365 "Parse a revision name and return its SHA1."
367 (git-call-process-env-string nil "rev-parse" rev)))
369 (defun git-config (key)
370 "Retrieve the value associated to KEY in the git repository config file."
371 (let ((str (git-call-process-env-string nil "config" key)))
372 (and str (car (split-string str "\n")))))
374 (defun git-symbolic-ref (ref)
375 "Wrapper for the git-symbolic-ref command."
376 (let ((str (git-call-process-env-string nil "symbolic-ref" ref)))
377 (and str (car (split-string str "\n")))))
379 (defun git-update-ref (ref newval &optional oldval reason)
380 "Update a reference by calling git-update-ref."
381 (let ((args (and oldval (list oldval))))
387 (apply 'git-call-process-display-error "update-ref" args)))
389 (defun git-read-tree (tree &optional index-file)
390 "Read a tree into the index file."
391 (apply #'git-call-process-env nil
392 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
393 "read-tree" (if tree (list tree))))
395 (defun git-write-tree (&optional index-file)
396 "Call git-write-tree and return the resulting tree SHA1 as a string."
398 (git-call-process-env-string (and index-file `(("GIT_INDEX_FILE" . ,index-file))) "write-tree")))
400 (defun git-commit-tree (buffer tree head)
401 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
402 (let ((author-name (git-get-committer-name))
403 (author-email (git-get-committer-email))
404 (subject "commit (initial): ")
405 author-date log-start log-end args coding-system-for-write)
407 (setq subject "commit: ")
410 (with-current-buffer buffer
411 (goto-char (point-min))
413 (setq log-start (re-search-forward (concat "^" (regexp-quote git-log-msg-separator) "\n") nil t))
415 (narrow-to-region (point-min) log-start)
416 (goto-char (point-min))
417 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t)
418 (setq author-name (match-string 1)
419 author-email (match-string 2)))
420 (goto-char (point-min))
421 (when (re-search-forward "^Date: +\\(.*\\)$" nil t)
422 (setq author-date (match-string 1)))
423 (goto-char (point-min))
424 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t)
425 (unless (string-equal head (match-string 1))
426 (setq subject "commit (merge): ")
428 (push (match-string 1) args))))
429 (setq log-start (point-min)))
430 (setq log-end (point-max))
431 (goto-char log-start)
432 (when (re-search-forward ".*$" nil t)
433 (setq subject (concat subject (match-string 0))))
434 (setq coding-system-for-write buffer-file-coding-system))
437 (with-output-to-string
438 (with-current-buffer standard-output
439 (let ((env `(("GIT_AUTHOR_NAME" . ,author-name)
440 ("GIT_AUTHOR_EMAIL" . ,author-email)
441 ("GIT_COMMITTER_NAME" . ,(git-get-committer-name))
442 ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email)))))
443 (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env))
444 (apply #'git-run-command-region
445 buffer log-start log-end env
446 "commit-tree" tree (nreverse args))))))))
447 (and (git-update-ref "HEAD" commit head subject)
450 (defun git-empty-db-p ()
451 "Check if the git db is empty (no commit done yet)."
452 (not (eq 0 (call-process "git" nil nil nil "rev-parse" "--verify" "HEAD"))))
454 (defun git-get-merge-heads ()
455 "Retrieve the merge heads from the MERGE_HEAD file if present."
457 (when (file-readable-p ".git/MERGE_HEAD")
459 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t)
460 (goto-char (point-min))
461 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t)
462 (push (match-string 0) heads))))
465 (defun git-get-commit-description (commit)
466 "Get a one-line description of COMMIT."
467 (let ((coding-system-for-read (git-get-logoutput-coding-system)))
468 (let ((descr (git-call-process-env-string nil "log" "--max-count=1" "--pretty=oneline" commit)))
469 (if (and descr (string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr))
470 (concat (substring (match-string 1 descr) 0 10) " - " (match-string 2 descr))
473 ;;;; File info structure
474 ;;;; ------------------------------------------------------------
476 ; fileinfo structure stolen from pcl-cvs
477 (defstruct (git-fileinfo
479 (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked))
480 (:conc-name git-fileinfo->))
482 state ;; current state
484 old-perm new-perm ;; permission flags
485 rename-state ;; rename or copy state
486 orig-name ;; original name for renames or copies
487 needs-refresh) ;; whether file needs to be refreshed
489 (defvar git-status nil)
491 (defun git-clear-status (status)
492 "Remove everything from the status list."
493 (ewoc-filter status (lambda (info) nil)))
495 (defun git-set-fileinfo-state (info state)
496 "Set the state of a file info."
497 (unless (eq (git-fileinfo->state info) state)
498 (setf (git-fileinfo->state info) state
499 (git-fileinfo->new-perm info) (git-fileinfo->old-perm info)
500 (git-fileinfo->rename-state info) nil
501 (git-fileinfo->orig-name info) nil
502 (git-fileinfo->needs-refresh info) t)))
504 (defun git-status-filenames-map (status func files &rest args)
505 "Apply FUNC to the status files names in the FILES list."
507 (setq files (sort files #'string-lessp))
508 (let ((file (pop files))
509 (node (ewoc-nth status 0)))
510 (while (and file node)
511 (let ((info (ewoc-data node)))
512 (if (string-lessp (git-fileinfo->name info) file)
513 (setq node (ewoc-next status node))
514 (if (string-equal (git-fileinfo->name info) file)
515 (apply func info args))
516 (setq file (pop files))))))))
518 (defun git-set-filenames-state (status files state)
519 "Set the state of a list of named files."
521 (git-status-filenames-map status #'git-set-fileinfo-state files state)
522 (unless state ;; delete files whose state has been set to nil
523 (ewoc-filter status (lambda (info) (git-fileinfo->state info))))))
525 (defun git-state-code (code)
526 "Convert from a string to a added/deleted/modified state."
527 (case (string-to-char code)
536 (defun git-status-code-as-string (code)
537 "Format a git status code as string."
539 ('modified (propertize "Modified" 'face 'git-status-face))
540 ('unknown (propertize "Unknown " 'face 'git-unknown-face))
541 ('added (propertize "Added " 'face 'git-status-face))
542 ('deleted (propertize "Deleted " 'face 'git-status-face))
543 ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face))
544 ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face))
545 ('ignored (propertize "Ignored " 'face 'git-ignored-face))
548 (defun git-file-type-as-string (old-perm new-perm)
549 "Return a string describing the file type based on its permissions."
550 (let* ((old-type (lsh (or old-perm 0) -9))
551 (new-type (lsh (or new-perm 0) -9))
556 (?\120 " (type change symlink -> file)")
557 (?\160 " (type change subproject -> file)")))
560 (?\100 " (type change file -> symlink)")
561 (?\160 " (type change subproject -> symlink)")
565 (?\100 " (type change file -> subproject)")
566 (?\120 " (type change symlink -> subproject)")
567 (t " (subproject)")))
568 (?\110 nil) ;; directory (internal, not a real git state)
569 (?\000 ;; deleted or unknown
572 (?\160 " (subproject)")))
573 (t (format " (unknown type %o)" new-type)))))
574 (cond (str (propertize str 'face 'git-status-face))
575 ((eq new-type ?\110) "/")
578 (defun git-rename-as-string (info)
579 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
580 (let ((state (git-fileinfo->rename-state info)))
584 (if (eq state 'copy) "copied from "
585 (if (eq (git-fileinfo->state info) 'added) "renamed from "
587 (git-escape-file-name (git-fileinfo->orig-name info))
588 ")") 'face 'git-status-face)
591 (defun git-permissions-as-string (old-perm new-perm)
592 "Format a permission change as string."
594 (if (or (not old-perm)
596 (eq 0 (logand ?\111 (logxor old-perm new-perm))))
598 (if (eq 0 (logand ?\111 old-perm)) "+x" "-x"))
599 'face 'git-permission-face))
601 (defun git-fileinfo-prettyprint (info)
602 "Pretty-printer for the git-fileinfo structure."
603 (let ((old-perm (git-fileinfo->old-perm info))
604 (new-perm (git-fileinfo->new-perm info)))
605 (insert (concat " " (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ")
606 " " (git-status-code-as-string (git-fileinfo->state info))
607 " " (git-permissions-as-string old-perm new-perm)
608 " " (git-escape-file-name (git-fileinfo->name info))
609 (git-file-type-as-string old-perm new-perm)
610 (git-rename-as-string info)))))
612 (defun git-insert-info-list (status infolist)
613 "Insert a list of file infos in the status buffer, replacing existing ones if any."
614 (setq infolist (sort infolist
615 (lambda (info1 info2)
616 (string-lessp (git-fileinfo->name info1)
617 (git-fileinfo->name info2)))))
618 (let ((info (pop infolist))
619 (node (ewoc-nth status 0)))
622 (setq node (ewoc-enter-last status info))
623 (setq info (pop infolist)))
624 ((string-lessp (git-fileinfo->name (ewoc-data node))
625 (git-fileinfo->name info))
626 (setq node (ewoc-next status node)))
627 ((string-equal (git-fileinfo->name (ewoc-data node))
628 (git-fileinfo->name info))
629 ;; preserve the marked flag
630 (setf (git-fileinfo->marked info) (git-fileinfo->marked (ewoc-data node)))
631 (setf (git-fileinfo->needs-refresh info) t)
632 (setf (ewoc-data node) info)
633 (setq info (pop infolist)))
635 (setq node (ewoc-enter-before status node info))
636 (setq info (pop infolist)))))))
638 (defun git-run-diff-index (status files)
639 "Run git-diff-index on FILES and parse the results into STATUS.
640 Return the list of files that haven't been handled."
641 (let ((remaining (copy-sequence files))
644 (apply #'git-call-process-env t nil "diff-index" "-z" "-M" "HEAD" "--" files)
645 (goto-char (point-min))
646 (while (re-search-forward
647 ":\\([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"
649 (let ((old-perm (string-to-number (match-string 1) 8))
650 (new-perm (string-to-number (match-string 2) 8))
651 (state (or (match-string 4) (match-string 6)))
652 (name (or (match-string 5) (match-string 7)))
653 (new-name (match-string 8)))
654 (if new-name ; copy or rename
655 (if (eq ?C (string-to-char state))
656 (push (git-create-fileinfo 'added new-name old-perm new-perm 'copy name) infolist)
657 (push (git-create-fileinfo 'deleted name 0 0 'rename new-name) infolist)
658 (push (git-create-fileinfo 'added new-name old-perm new-perm 'rename name) infolist))
659 (push (git-create-fileinfo (git-state-code state) name old-perm new-perm) infolist))
660 (setq remaining (delete name remaining))
661 (when new-name (setq remaining (delete new-name remaining))))))
662 (git-insert-info-list status infolist)
665 (defun git-find-status-file (status file)
666 "Find a given file in the status ewoc and return its node."
667 (let ((node (ewoc-nth status 0)))
668 (while (and node (not (string= file (git-fileinfo->name (ewoc-data node)))))
669 (setq node (ewoc-next status node)))
672 (defun git-run-ls-files (status files default-state &rest options)
673 "Run git-ls-files on FILES and parse the results into STATUS.
674 Return the list of files that haven't been handled."
677 (apply #'git-call-process-env t nil "ls-files" "-z" (append options (list "--") files))
678 (goto-char (point-min))
679 (while (re-search-forward "\\([^\0]*?\\)\\(/?\\)\0" nil t 1)
680 (let ((name (match-string 1)))
681 (push (git-create-fileinfo default-state name 0
682 (if (string-equal "/" (match-string 2)) (lsh ?\110 9) 0))
684 (setq files (delete name files)))))
685 (git-insert-info-list status infolist)
688 (defun git-run-ls-files-cached (status files default-state)
689 "Run git-ls-files -c on FILES and parse the results into STATUS.
690 Return the list of files that haven't been handled."
691 (let ((remaining (copy-sequence files))
694 (apply #'git-call-process-env t nil "ls-files" "-z" "-s" "-c" "--" files)
695 (goto-char (point-min))
696 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
697 (let* ((new-perm (string-to-number (match-string 1) 8))
698 (old-perm (if (eq default-state 'added) 0 new-perm))
699 (name (match-string 2)))
700 (push (git-create-fileinfo default-state name old-perm new-perm) infolist)
701 (setq remaining (delete name remaining)))))
702 (git-insert-info-list status infolist)
705 (defun git-run-ls-unmerged (status files)
706 "Run git-ls-files -u on FILES and parse the results into STATUS."
708 (apply #'git-call-process-env t nil "ls-files" "-z" "-u" "--" files)
709 (goto-char (point-min))
710 (let (unmerged-files)
711 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t)
712 (push (match-string 1) unmerged-files))
713 (git-set-filenames-state status unmerged-files 'unmerged))))
715 (defun git-get-exclude-files ()
716 "Get the list of exclude files to pass to git-ls-files."
718 (config (git-config "core.excludesfile")))
719 (when (file-readable-p ".git/info/exclude")
720 (push ".git/info/exclude" files))
721 (when (and config (file-readable-p config))
725 (defun git-run-ls-files-with-excludes (status files default-state &rest options)
726 "Run git-ls-files on FILES with appropriate --exclude-from options."
727 (let ((exclude-files (git-get-exclude-files)))
728 (apply #'git-run-ls-files status files default-state "--directory" "--no-empty-directory"
729 (concat "--exclude-per-directory=" git-per-dir-ignore-file)
730 (append options (mapcar (lambda (f) (concat "--exclude-from=" f)) exclude-files)))))
732 (defun git-update-status-files (files &optional default-state)
733 "Update the status of FILES from the index."
734 (unless git-status (error "Not in git-status buffer."))
735 (when (or git-show-uptodate files)
736 (git-run-ls-files-cached git-status files 'uptodate))
737 (let* ((remaining-files
738 (if (git-empty-db-p) ; we need some special handling for an empty db
739 (git-run-ls-files-cached git-status files 'added)
740 (git-run-diff-index git-status files))))
741 (git-run-ls-unmerged git-status files)
742 (when (or remaining-files (and git-show-unknown (not files)))
743 (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'unknown "-o")))
744 (when (or remaining-files (and git-show-ignored (not files)))
745 (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'ignored "-o" "-i")))
746 (git-set-filenames-state git-status remaining-files default-state)
748 (git-refresh-ewoc-hf git-status)))
750 (defun git-mark-files (status files)
751 "Mark all the specified FILES, and unmark the others."
752 (setq files (sort files #'string-lessp))
753 (let ((file (and files (pop files)))
754 (node (ewoc-nth status 0)))
756 (let ((info (ewoc-data node)))
757 (if (and file (string-equal (git-fileinfo->name info) file))
759 (unless (git-fileinfo->marked info)
760 (setf (git-fileinfo->marked info) t)
761 (setf (git-fileinfo->needs-refresh info) t))
762 (setq file (pop files))
763 (setq node (ewoc-next status node)))
764 (when (git-fileinfo->marked info)
765 (setf (git-fileinfo->marked info) nil)
766 (setf (git-fileinfo->needs-refresh info) t))
767 (if (and file (string-lessp file (git-fileinfo->name info)))
768 (setq file (pop files))
769 (setq node (ewoc-next status node))))))))
771 (defun git-marked-files ()
772 "Return a list of all marked files, or if none a list containing just the file at cursor position."
773 (unless git-status (error "Not in git-status buffer."))
774 (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info)))
775 (list (ewoc-data (ewoc-locate git-status)))))
777 (defun git-marked-files-state (&rest states)
778 "Return marked files that are in the specified states."
779 (let ((files (git-marked-files))
782 (when (memq (git-fileinfo->state info) states)
786 (defun git-refresh-files ()
787 "Refresh all files that need it and clear the needs-refresh flag."
788 (unless git-status (error "Not in git-status buffer."))
791 (let ((refresh (git-fileinfo->needs-refresh info)))
792 (setf (git-fileinfo->needs-refresh info) nil)
795 ; move back to goal column
796 (when goal-column (move-to-column goal-column)))
798 (defun git-refresh-ewoc-hf (status)
799 "Refresh the ewoc header and footer."
800 (let ((branch (git-symbolic-ref "HEAD"))
801 (head (if (git-empty-db-p) "Nothing committed yet"
802 (git-get-commit-description "HEAD")))
803 (merge-heads (git-get-merge-heads)))
805 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
808 (if (string-match "^refs/heads/" branch)
809 (substring branch (match-end 0))
811 "none (detached HEAD)")
814 (concat "\nMerging: "
815 (mapconcat (lambda (str) (git-get-commit-description str)) merge-heads "\n "))
817 (if (ewoc-nth status 0) "" " No changes."))))
819 (defun git-get-filenames (files)
820 (mapcar (lambda (info) (git-fileinfo->name info)) files))
822 (defun git-update-index (index-file files)
823 "Run git-update-index on a list of files."
824 (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file))))
825 added deleted modified)
827 (case (git-fileinfo->state info)
828 ('added (push info added))
829 ('deleted (push info deleted))
830 ('modified (push info modified))))
832 (apply #'git-call-process-env nil env "update-index" "--add" "--" (git-get-filenames added)))
834 (apply #'git-call-process-env nil env "update-index" "--remove" "--" (git-get-filenames deleted)))
836 (apply #'git-call-process-env nil env "update-index" "--" (git-get-filenames modified)))))
838 (defun git-run-pre-commit-hook ()
839 "Run the pre-commit hook if any."
840 (unless git-status (error "Not in git-status buffer."))
841 (let ((files (git-marked-files-state 'added 'deleted 'modified)))
843 (not (file-executable-p ".git/hooks/pre-commit"))
844 (let ((index-file (make-temp-file "gitidx")))
846 (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}"))))
847 (git-read-tree head-tree index-file)
848 (git-update-index index-file files)
849 (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" . ,index-file))))
850 (delete-file index-file))))))
852 (defun git-do-commit ()
853 "Perform the actual commit using the current buffer as log message."
855 (let ((buffer (current-buffer))
856 (index-file (make-temp-file "gitidx")))
857 (with-current-buffer log-edit-parent-buffer
858 (if (git-marked-files-state 'unmerged)
859 (message "You cannot commit unmerged files, resolve them first.")
861 (let ((files (git-marked-files-state 'added 'deleted 'modified))
863 (unless (git-empty-db-p)
864 (setq head (git-rev-parse "HEAD")
865 head-tree (git-rev-parse "HEAD^{tree}")))
868 (message "Running git commit...")
869 (git-read-tree head-tree index-file)
870 (git-update-index nil files) ;update both the default index
871 (git-update-index index-file files) ;and the temporary one
872 (let ((tree (git-write-tree index-file)))
873 (if (or (not (string-equal tree head-tree))
874 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
875 (let ((commit (git-commit-tree buffer tree head)))
877 (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil))
878 (condition-case nil (delete-file ".git/MERGE_MSG") (error nil))
879 (with-current-buffer buffer (erase-buffer))
880 (git-update-status-files (git-get-filenames files) 'uptodate)
881 (git-call-process-env nil nil "rerere")
882 (git-call-process-env nil nil "gc" "--auto")
884 (git-refresh-ewoc-hf git-status)
885 (message "Committed %s." commit)
886 (git-run-hook "post-commit" nil)))
887 (message "Commit aborted."))))
888 (message "No files to commit.")))
889 (delete-file index-file))))))
892 ;;;; Interactive functions
893 ;;;; ------------------------------------------------------------
895 (defun git-mark-file ()
896 "Mark the file that the cursor is on and move to the next one."
898 (unless git-status (error "Not in git-status buffer."))
899 (let* ((pos (ewoc-locate git-status))
900 (info (ewoc-data pos)))
901 (setf (git-fileinfo->marked info) t)
902 (ewoc-invalidate git-status pos)
903 (ewoc-goto-next git-status 1)))
905 (defun git-unmark-file ()
906 "Unmark the file that the cursor is on and move to the next one."
908 (unless git-status (error "Not in git-status buffer."))
909 (let* ((pos (ewoc-locate git-status))
910 (info (ewoc-data pos)))
911 (setf (git-fileinfo->marked info) nil)
912 (ewoc-invalidate git-status pos)
913 (ewoc-goto-next git-status 1)))
915 (defun git-unmark-file-up ()
916 "Unmark the file that the cursor is on and move to the previous one."
918 (unless git-status (error "Not in git-status buffer."))
919 (let* ((pos (ewoc-locate git-status))
920 (info (ewoc-data pos)))
921 (setf (git-fileinfo->marked info) nil)
922 (ewoc-invalidate git-status pos)
923 (ewoc-goto-prev git-status 1)))
925 (defun git-mark-all ()
928 (unless git-status (error "Not in git-status buffer."))
929 (ewoc-map (lambda (info) (unless (git-fileinfo->marked info)
930 (setf (git-fileinfo->marked info) t))) git-status)
931 ; move back to goal column after invalidate
932 (when goal-column (move-to-column goal-column)))
934 (defun git-unmark-all ()
937 (unless git-status (error "Not in git-status buffer."))
938 (ewoc-map (lambda (info) (when (git-fileinfo->marked info)
939 (setf (git-fileinfo->marked info) nil)
941 ; move back to goal column after invalidate
942 (when goal-column (move-to-column goal-column)))
944 (defun git-toggle-all-marks ()
945 "Toggle all file marks."
947 (unless git-status (error "Not in git-status buffer."))
948 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status)
949 ; move back to goal column after invalidate
950 (when goal-column (move-to-column goal-column)))
952 (defun git-next-file (&optional n)
953 "Move the selection down N files."
955 (unless git-status (error "Not in git-status buffer."))
956 (ewoc-goto-next git-status n))
958 (defun git-prev-file (&optional n)
959 "Move the selection up N files."
961 (unless git-status (error "Not in git-status buffer."))
962 (ewoc-goto-prev git-status n))
964 (defun git-next-unmerged-file (&optional n)
965 "Move the selection down N unmerged files."
967 (unless git-status (error "Not in git-status buffer."))
968 (let* ((last (ewoc-locate git-status))
969 (node (ewoc-next git-status last)))
970 (while (and node (> n 0))
971 (when (eq 'unmerged (git-fileinfo->state (ewoc-data node)))
974 (setq node (ewoc-next git-status node)))
975 (ewoc-goto-node git-status last)))
977 (defun git-prev-unmerged-file (&optional n)
978 "Move the selection up N unmerged files."
980 (unless git-status (error "Not in git-status buffer."))
981 (let* ((last (ewoc-locate git-status))
982 (node (ewoc-prev git-status last)))
983 (while (and node (> n 0))
984 (when (eq 'unmerged (git-fileinfo->state (ewoc-data node)))
987 (setq node (ewoc-prev git-status node)))
988 (ewoc-goto-node git-status last)))
990 (defun git-add-file ()
991 "Add marked file(s) to the index cache."
993 (let ((files (git-get-filenames (git-marked-files-state 'unknown 'ignored))))
994 ;; FIXME: add support for directories
996 (push (file-relative-name (read-file-name "File to add: " nil nil t)) files))
997 (when (apply 'git-call-process-display-error "update-index" "--add" "--" files)
998 (git-update-status-files files 'uptodate)
999 (git-success-message "Added" files))))
1001 (defun git-ignore-file ()
1002 "Add marked file(s) to the ignore list."
1004 (let ((files (git-get-filenames (git-marked-files-state 'unknown))))
1006 (push (file-relative-name (read-file-name "File to ignore: " nil nil t)) files))
1007 (dolist (f files) (git-append-to-ignore f))
1008 (git-update-status-files files 'ignored)
1009 (git-success-message "Ignored" files)))
1011 (defun git-remove-file ()
1012 "Remove the marked file(s)."
1014 (let ((files (git-get-filenames (git-marked-files-state 'added 'modified 'unknown 'uptodate 'ignored))))
1016 (push (file-relative-name (read-file-name "File to remove: " nil nil t)) files))
1018 (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" "")))
1020 (dolist (name files)
1022 (if (file-directory-p name)
1023 (delete-directory name)
1024 (delete-file name))))
1025 (when (apply 'git-call-process-display-error "update-index" "--remove" "--" files)
1026 (git-update-status-files files nil)
1027 (git-success-message "Removed" files)))
1028 (message "Aborting"))))
1030 (defun git-revert-file ()
1031 "Revert changes to the marked file(s)."
1033 (let ((files (git-marked-files-state 'added 'deleted 'modified 'unmerged))
1037 (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" ""))))
1038 (dolist (info files)
1039 (case (git-fileinfo->state info)
1040 ('added (push (git-fileinfo->name info) added))
1041 ('deleted (push (git-fileinfo->name info) modified))
1042 ('unmerged (push (git-fileinfo->name info) modified))
1043 ('modified (push (git-fileinfo->name info) modified))))
1044 ;; check if a buffer contains one of the files and isn't saved
1045 (dolist (file modified)
1046 (let ((buffer (get-file-buffer file)))
1047 (when (and buffer (buffer-modified-p buffer))
1048 (error "Buffer %s is modified. Please kill or save modified buffers before reverting." (buffer-name buffer)))))
1051 (apply 'git-call-process-display-error "update-index" "--force-remove" "--" added))
1053 (apply 'git-call-process-display-error "checkout" "HEAD" modified)))))
1054 (git-update-status-files (append added modified) 'uptodate)
1056 (dolist (file modified)
1057 (let ((buffer (get-file-buffer file)))
1058 (when buffer (with-current-buffer buffer (revert-buffer t t t)))))
1059 (git-success-message "Reverted" (git-get-filenames files)))))))
1061 (defun git-resolve-file ()
1062 "Resolve conflicts in marked file(s)."
1064 (let ((files (git-get-filenames (git-marked-files-state 'unmerged))))
1066 (when (apply 'git-call-process-display-error "update-index" "--" files)
1067 (git-update-status-files files 'uptodate)
1068 (git-success-message "Resolved" files)))))
1070 (defun git-remove-handled ()
1071 "Remove handled files from the status list."
1073 (ewoc-filter git-status
1075 (case (git-fileinfo->state info)
1076 ('ignored git-show-ignored)
1077 ('uptodate git-show-uptodate)
1078 ('unknown git-show-unknown)
1080 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
1081 (git-refresh-ewoc-hf git-status)))
1083 (defun git-toggle-show-uptodate ()
1084 "Toogle the option for showing up-to-date files."
1086 (if (setq git-show-uptodate (not git-show-uptodate))
1087 (git-refresh-status)
1088 (git-remove-handled)))
1090 (defun git-toggle-show-ignored ()
1091 "Toogle the option for showing ignored files."
1093 (if (setq git-show-ignored (not git-show-ignored))
1095 (message "Inserting ignored files...")
1096 (git-run-ls-files-with-excludes git-status nil 'ignored "-o" "-i")
1098 (git-refresh-ewoc-hf git-status)
1099 (message "Inserting ignored files...done"))
1100 (git-remove-handled)))
1102 (defun git-toggle-show-unknown ()
1103 "Toogle the option for showing unknown files."
1105 (if (setq git-show-unknown (not git-show-unknown))
1107 (message "Inserting unknown files...")
1108 (git-run-ls-files-with-excludes git-status nil 'unknown "-o")
1110 (git-refresh-ewoc-hf git-status)
1111 (message "Inserting unknown files...done"))
1112 (git-remove-handled)))
1114 (defun git-expand-directory (info)
1115 "Expand the directory represented by INFO to list its files."
1116 (when (eq (lsh (git-fileinfo->new-perm info) -9) ?\110)
1117 (let ((dir (git-fileinfo->name info)))
1118 (git-set-filenames-state git-status (list dir) nil)
1119 (git-run-ls-files-with-excludes git-status (list (concat dir "/")) 'unknown "-o")
1121 (git-refresh-ewoc-hf git-status)
1124 (defun git-setup-diff-buffer (buffer)
1125 "Setup a buffer for displaying a diff."
1126 (let ((dir default-directory))
1127 (with-current-buffer buffer
1129 (goto-char (point-min))
1130 (setq default-directory dir)
1131 (setq buffer-read-only t)))
1132 (display-buffer buffer)
1133 ; shrink window only if it displays the status buffer
1134 (when (eq (window-buffer) (current-buffer))
1135 (shrink-window-if-larger-than-buffer)))
1137 (defun git-diff-file ()
1138 "Diff the marked file(s) against HEAD."
1140 (let ((files (git-marked-files)))
1141 (git-setup-diff-buffer
1142 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files)))))
1144 (defun git-diff-file-merge-head (arg)
1145 "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)."
1147 (let ((files (git-marked-files))
1148 (merge-heads (git-get-merge-heads)))
1149 (unless merge-heads (error "No merge in progress"))
1150 (git-setup-diff-buffer
1151 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M"
1152 (or (nth (1- arg) merge-heads) "HEAD") "--" (git-get-filenames files)))))
1154 (defun git-diff-unmerged-file (stage)
1155 "Diff the marked unmerged file(s) against the specified stage."
1156 (let ((files (git-marked-files)))
1157 (git-setup-diff-buffer
1158 (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files)))))
1160 (defun git-diff-file-base ()
1161 "Diff the marked unmerged file(s) against the common base file."
1163 (git-diff-unmerged-file "-1"))
1165 (defun git-diff-file-mine ()
1166 "Diff the marked unmerged file(s) against my pre-merge version."
1168 (git-diff-unmerged-file "-2"))
1170 (defun git-diff-file-other ()
1171 "Diff the marked unmerged file(s) against the other's pre-merge version."
1173 (git-diff-unmerged-file "-3"))
1175 (defun git-diff-file-combined ()
1176 "Do a combined diff of the marked unmerged file(s)."
1178 (git-diff-unmerged-file "-c"))
1180 (defun git-diff-file-idiff ()
1181 "Perform an interactive diff on the current file."
1183 (let ((files (git-marked-files-state 'added 'deleted 'modified)))
1184 (unless (eq 1 (length files))
1185 (error "Cannot perform an interactive diff on multiple files."))
1186 (let* ((filename (car (git-get-filenames files)))
1187 (buff1 (find-file-noselect filename))
1188 (buff2 (git-run-command-buffer (concat filename ".~HEAD~") "cat-file" "blob" (concat "HEAD:" filename))))
1189 (ediff-buffers buff1 buff2))))
1191 (defun git-log-file ()
1192 "Display a log of changes to the marked file(s)."
1194 (let* ((files (git-marked-files))
1195 (coding-system-for-read git-commits-coding-system)
1196 (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files))))
1197 (with-current-buffer buffer
1198 ; (git-log-mode) FIXME: implement log mode
1199 (goto-char (point-min))
1200 (setq buffer-read-only t))
1201 (display-buffer buffer)))
1203 (defun git-log-edit-files ()
1204 "Return a list of marked files for use in the log-edit buffer."
1205 (with-current-buffer log-edit-parent-buffer
1206 (git-get-filenames (git-marked-files-state 'added 'deleted 'modified))))
1208 (defun git-log-edit-diff ()
1209 "Run a diff of the current files being committed from a log-edit buffer."
1210 (with-current-buffer log-edit-parent-buffer
1213 (defun git-append-sign-off (name email)
1214 "Append a Signed-off-by entry to the current buffer, avoiding duplicates."
1215 (let ((sign-off (format "Signed-off-by: %s <%s>" name email))
1216 (case-fold-search t))
1217 (goto-char (point-min))
1218 (unless (re-search-forward (concat "^" (regexp-quote sign-off)) nil t)
1219 (goto-char (point-min))
1220 (unless (re-search-forward "^Signed-off-by: " nil t)
1221 (setq sign-off (concat "\n" sign-off)))
1222 (goto-char (point-max))
1223 (insert sign-off "\n"))))
1225 (defun git-setup-log-buffer (buffer &optional author-name author-email subject date msg)
1226 "Setup the log buffer for a commit."
1227 (unless git-status (error "Not in git-status buffer."))
1228 (let ((merge-heads (git-get-merge-heads))
1229 (dir default-directory)
1230 (committer-name (git-get-committer-name))
1231 (committer-email (git-get-committer-email))
1232 (sign-off git-append-signed-off-by))
1233 (with-current-buffer buffer
1238 (format "Author: %s <%s>\n%s%s"
1239 (or author-name committer-name)
1240 (or author-email committer-email)
1241 (if date (format "Date: %s\n" date) "")
1243 (format "Parent: %s\n%s\n"
1244 (git-rev-parse "HEAD")
1245 (mapconcat (lambda (str) (concat "Parent: " str)) merge-heads "\n"))
1247 'face 'git-header-face)
1248 (propertize git-log-msg-separator 'face 'git-separator-face)
1250 (when subject (insert subject "\n\n"))
1251 (cond (msg (insert msg "\n"))
1252 ((file-readable-p ".dotest/msg")
1253 (insert-file-contents ".dotest/msg"))
1254 ((file-readable-p ".git/MERGE_MSG")
1255 (insert-file-contents ".git/MERGE_MSG")))
1256 ; delete empty lines at end
1257 (goto-char (point-min))
1258 (when (re-search-forward "\n+\\'" nil t)
1259 (replace-match "\n" t t))
1260 (when sign-off (git-append-sign-off committer-name committer-email)))
1263 (defun git-commit-file ()
1264 "Commit the marked file(s), asking for a commit message."
1266 (unless git-status (error "Not in git-status buffer."))
1267 (when (git-run-pre-commit-hook)
1268 (let ((buffer (get-buffer-create "*git-commit*"))
1269 (coding-system (git-get-commits-coding-system))
1270 author-name author-email subject date)
1271 (when (eq 0 (buffer-size buffer))
1272 (when (file-readable-p ".dotest/info")
1274 (insert-file-contents ".dotest/info")
1275 (goto-char (point-min))
1276 (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t)
1277 (setq author-name (match-string 1))
1278 (setq author-email (match-string 2)))
1279 (goto-char (point-min))
1280 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
1281 (setq subject (match-string 1)))
1282 (goto-char (point-min))
1283 (when (re-search-forward "^Date: \\(.*\\)$" nil t)
1284 (setq date (match-string 1)))))
1285 (git-setup-log-buffer buffer author-name author-email subject date))
1286 (if (boundp 'log-edit-diff-function)
1287 (log-edit 'git-do-commit nil '((log-edit-listfun . git-log-edit-files)
1288 (log-edit-diff-function . git-log-edit-diff)) buffer)
1289 (log-edit 'git-do-commit nil 'git-log-edit-files buffer))
1290 (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords))
1291 (setq buffer-file-coding-system coding-system)
1292 (re-search-forward (regexp-quote (concat git-log-msg-separator "\n")) nil t))))
1294 (defun git-setup-commit-buffer (commit)
1295 "Setup the commit buffer with the contents of COMMIT."
1296 (let (author-name author-email subject date msg)
1298 (let ((coding-system (git-get-logoutput-coding-system)))
1299 (git-call-process-env t nil "log" "-1" "--pretty=medium" commit)
1300 (goto-char (point-min))
1301 (when (re-search-forward "^Author: *\\(.*\\) <\\(.*\\)>$" nil t)
1302 (setq author-name (match-string 1))
1303 (setq author-email (match-string 2)))
1304 (when (re-search-forward "^Date: *\\(.*\\)$" nil t)
1305 (setq date (match-string 1)))
1306 (while (re-search-forward "^ \\(.*\\)$" nil t)
1307 (push (match-string 1) msg))
1308 (setq msg (nreverse msg))
1309 (setq subject (pop msg))
1310 (while (and msg (zerop (length (car msg))) (pop msg)))))
1311 (git-setup-log-buffer (get-buffer-create "*git-commit*")
1312 author-name author-email subject date
1313 (mapconcat #'identity msg "\n"))))
1315 (defun git-get-commit-files (commit)
1316 "Retrieve the list of files modified by COMMIT."
1319 (git-call-process-env t nil "diff-tree" "-r" "-z" "--name-only" "--no-commit-id" commit)
1320 (goto-char (point-min))
1321 (while (re-search-forward "\\([^\0]*\\)\0" nil t 1)
1322 (push (match-string 1) files)))
1325 (defun git-amend-commit ()
1326 "Undo the last commit on HEAD, and set things up to commit an
1327 amended version of it."
1329 (unless git-status (error "Not in git-status buffer."))
1330 (when (git-empty-db-p) (error "No commit to amend."))
1331 (let* ((commit (git-rev-parse "HEAD"))
1332 (files (git-get-commit-files commit)))
1333 (when (git-call-process-display-error "reset" "--soft" "HEAD^")
1334 (git-update-status-files (copy-sequence files) 'uptodate)
1335 (git-mark-files git-status files)
1337 (git-setup-commit-buffer commit)
1338 (git-commit-file))))
1340 (defun git-find-file ()
1341 "Visit the current file in its own buffer."
1343 (unless git-status (error "Not in git-status buffer."))
1344 (let ((info (ewoc-data (ewoc-locate git-status))))
1345 (unless (git-expand-directory info)
1346 (find-file (git-fileinfo->name info))
1347 (when (eq 'unmerged (git-fileinfo->state info))
1350 (defun git-find-file-other-window ()
1351 "Visit the current file in its own buffer in another window."
1353 (unless git-status (error "Not in git-status buffer."))
1354 (let ((info (ewoc-data (ewoc-locate git-status))))
1355 (find-file-other-window (git-fileinfo->name info))
1356 (when (eq 'unmerged (git-fileinfo->state info))
1359 (defun git-find-file-imerge ()
1360 "Visit the current file in interactive merge mode."
1362 (unless git-status (error "Not in git-status buffer."))
1363 (let ((info (ewoc-data (ewoc-locate git-status))))
1364 (find-file (git-fileinfo->name info))
1367 (defun git-view-file ()
1368 "View the current file in its own buffer."
1370 (unless git-status (error "Not in git-status buffer."))
1371 (let ((info (ewoc-data (ewoc-locate git-status))))
1372 (view-file (git-fileinfo->name info))))
1374 (defun git-refresh-status ()
1375 "Refresh the git status buffer."
1377 (let* ((status git-status)
1378 (pos (ewoc-locate status))
1379 (marked-files (git-get-filenames (ewoc-collect status (lambda (info) (git-fileinfo->marked info)))))
1380 (cur-name (and pos (git-fileinfo->name (ewoc-data pos)))))
1381 (unless status (error "Not in git-status buffer."))
1382 (message "Refreshing git status...")
1383 (git-call-process-env nil nil "update-index" "--refresh")
1384 (git-clear-status status)
1385 (git-update-status-files nil)
1386 ; restore file marks
1388 (git-status-filenames-map status
1390 (setf (git-fileinfo->marked info) t)
1391 (setf (git-fileinfo->needs-refresh info) t))
1393 (git-refresh-files))
1394 ; move point to the current file name if any
1395 (message "Refreshing git status...done")
1396 (let ((node (and cur-name (git-find-status-file status cur-name))))
1397 (when node (ewoc-goto-node status node)))))
1399 (defun git-status-quit ()
1400 "Quit git-status mode."
1405 ;;;; ------------------------------------------------------------
1407 (defvar git-status-mode-hook nil
1408 "Run after `git-status-mode' is setup.")
1410 (defvar git-status-mode-map nil
1411 "Keymap for git major mode.")
1413 (defvar git-status nil
1414 "List of all files managed by the git-status mode.")
1416 (unless git-status-mode-map
1417 (let ((map (make-keymap))
1418 (commit-map (make-sparse-keymap))
1419 (diff-map (make-sparse-keymap))
1420 (toggle-map (make-sparse-keymap)))
1421 (suppress-keymap map)
1422 (define-key map "?" 'git-help)
1423 (define-key map "h" 'git-help)
1424 (define-key map " " 'git-next-file)
1425 (define-key map "a" 'git-add-file)
1426 (define-key map "c" 'git-commit-file)
1427 (define-key map "\C-c" commit-map)
1428 (define-key map "d" diff-map)
1429 (define-key map "=" 'git-diff-file)
1430 (define-key map "f" 'git-find-file)
1431 (define-key map "\r" 'git-find-file)
1432 (define-key map "g" 'git-refresh-status)
1433 (define-key map "i" 'git-ignore-file)
1434 (define-key map "l" 'git-log-file)
1435 (define-key map "m" 'git-mark-file)
1436 (define-key map "M" 'git-mark-all)
1437 (define-key map "n" 'git-next-file)
1438 (define-key map "N" 'git-next-unmerged-file)
1439 (define-key map "o" 'git-find-file-other-window)
1440 (define-key map "p" 'git-prev-file)
1441 (define-key map "P" 'git-prev-unmerged-file)
1442 (define-key map "q" 'git-status-quit)
1443 (define-key map "r" 'git-remove-file)
1444 (define-key map "R" 'git-resolve-file)
1445 (define-key map "t" toggle-map)
1446 (define-key map "T" 'git-toggle-all-marks)
1447 (define-key map "u" 'git-unmark-file)
1448 (define-key map "U" 'git-revert-file)
1449 (define-key map "v" 'git-view-file)
1450 (define-key map "x" 'git-remove-handled)
1451 (define-key map "\C-?" 'git-unmark-file-up)
1452 (define-key map "\M-\C-?" 'git-unmark-all)
1454 (define-key commit-map "\C-a" 'git-amend-commit)
1456 (define-key diff-map "b" 'git-diff-file-base)
1457 (define-key diff-map "c" 'git-diff-file-combined)
1458 (define-key diff-map "=" 'git-diff-file)
1459 (define-key diff-map "e" 'git-diff-file-idiff)
1460 (define-key diff-map "E" 'git-find-file-imerge)
1461 (define-key diff-map "h" 'git-diff-file-merge-head)
1462 (define-key diff-map "m" 'git-diff-file-mine)
1463 (define-key diff-map "o" 'git-diff-file-other)
1465 (define-key toggle-map "u" 'git-toggle-show-uptodate)
1466 (define-key toggle-map "i" 'git-toggle-show-ignored)
1467 (define-key toggle-map "k" 'git-toggle-show-unknown)
1468 (define-key toggle-map "m" 'git-toggle-all-marks)
1469 (setq git-status-mode-map map))
1470 (easy-menu-define git-menu git-status-mode-map
1473 ["Refresh" git-refresh-status t]
1474 ["Commit" git-commit-file t]
1476 ["Next Unmerged File" git-next-unmerged-file t]
1477 ["Prev Unmerged File" git-prev-unmerged-file t]
1478 ["Mark as Resolved" git-resolve-file t]
1479 ["Interactive Merge File" git-find-file-imerge t]
1480 ["Diff Against Common Base File" git-diff-file-base t]
1481 ["Diff Combined" git-diff-file-combined t]
1482 ["Diff Against Merge Head" git-diff-file-merge-head t]
1483 ["Diff Against Mine" git-diff-file-mine t]
1484 ["Diff Against Other" git-diff-file-other t])
1486 ["Add File" git-add-file t]
1487 ["Revert File" git-revert-file t]
1488 ["Ignore File" git-ignore-file t]
1489 ["Remove File" git-remove-file t]
1491 ["Find File" git-find-file t]
1492 ["View File" git-view-file t]
1493 ["Diff File" git-diff-file t]
1494 ["Interactive Diff File" git-diff-file-idiff t]
1495 ["Log" git-log-file t]
1497 ["Mark" git-mark-file t]
1498 ["Mark All" git-mark-all t]
1499 ["Unmark" git-unmark-file t]
1500 ["Unmark All" git-unmark-all t]
1501 ["Toggle All Marks" git-toggle-all-marks t]
1502 ["Hide Handled Files" git-remove-handled t]
1504 ["Show Uptodate Files" git-toggle-show-uptodate :style toggle :selected git-show-uptodate]
1505 ["Show Ignored Files" git-toggle-show-ignored :style toggle :selected git-show-ignored]
1506 ["Show Unknown Files" git-toggle-show-unknown :style toggle :selected git-show-unknown]
1508 ["Quit" git-status-quit t])))
1511 ;; git mode should only run in the *git status* buffer
1512 (put 'git-status-mode 'mode-class 'special)
1514 (defun git-status-mode ()
1515 "Major mode for interacting with Git.
1517 \\{git-status-mode-map}"
1518 (kill-all-local-variables)
1519 (buffer-disable-undo)
1520 (setq mode-name "git status"
1521 major-mode 'git-status-mode
1524 (use-local-map git-status-mode-map)
1525 (let ((buffer-read-only nil))
1527 (let ((status (ewoc-create 'git-fileinfo-prettyprint "" "")))
1528 (set (make-local-variable 'git-status) status))
1529 (set (make-local-variable 'list-buffers-directory) default-directory)
1530 (make-local-variable 'git-show-uptodate)
1531 (make-local-variable 'git-show-ignored)
1532 (make-local-variable 'git-show-unknown)
1533 (run-hooks 'git-status-mode-hook)))
1535 (defun git-find-status-buffer (dir)
1536 "Find the git status buffer handling a specified directory."
1537 (let ((list (buffer-list))
1538 (fulldir (expand-file-name dir))
1540 (while (and list (not found))
1541 (let ((buffer (car list)))
1542 (with-current-buffer buffer
1543 (when (and list-buffers-directory
1544 (string-equal fulldir (expand-file-name list-buffers-directory))
1545 (eq major-mode 'git-status-mode))
1546 (setq found buffer))))
1547 (setq list (cdr list)))
1550 (defun git-status (dir)
1551 "Entry point into git-status mode."
1552 (interactive "DSelect directory: ")
1553 (setq dir (git-get-top-dir dir))
1554 (if (file-directory-p (concat (file-name-as-directory dir) ".git"))
1555 (let ((buffer (or (and git-reuse-status-buffer (git-find-status-buffer dir))
1556 (create-file-buffer (expand-file-name "*git-status*" dir)))))
1557 (switch-to-buffer buffer)
1560 (git-refresh-status)
1561 (goto-char (point-min))
1562 (add-hook 'after-save-hook 'git-update-saved-file))
1563 (message "%s is not a git working tree." dir)))
1565 (defun git-update-saved-file ()
1566 "Update the corresponding git-status buffer when a file is saved.
1567 Meant to be used in `after-save-hook'."
1568 (let* ((file (expand-file-name buffer-file-name))
1569 (dir (condition-case nil (git-get-top-dir (file-name-directory file)) (error nil)))
1570 (buffer (and dir (git-find-status-buffer dir))))
1572 (with-current-buffer buffer
1573 (let ((filename (file-relative-name file dir)))
1574 ; skip files located inside the .git directory
1575 (unless (string-match "^\\.git/" filename)
1576 (git-call-process-env nil nil "add" "--refresh" "--" filename)
1577 (git-update-status-files (list filename) 'uptodate)))))))
1580 "Display help for Git mode."
1582 (describe-function 'git-status-mode))
1585 ;;; git.el ends here