use cooper theme -- end of git, I am trying livemesh
[srid.dotfiles.git] / emacs / external / git-contrib-emacs / git.el
blob4fa853fae76dc2ac132e489f5f1b630b3fc0f1de
1 ;;; git.el --- A user interface for git
3 ;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org>
5 ;; Version: 1.0
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,
20 ;; MA 02111-1307 USA
22 ;;; Commentary:
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:
32 ;; (require 'git)
34 ;; To start: `M-x git-status'
36 ;; TODO
37 ;; - portability to XEmacs
38 ;; - diff against other branch
39 ;; - renaming files from the status buffer
40 ;; - creating tags
41 ;; - fetch/pull
42 ;; - switching branches
43 ;; - revlist browser
44 ;; - git-show-branch browser
45 ;; - menus
48 (eval-when-compile (require 'cl))
49 (require 'ewoc)
50 (require 'log-edit)
51 (require 'easymenu)
54 ;;;; Customizations
55 ;;;; ------------------------------------------------------------
57 (defgroup git nil
58 "A user interface for the git versioning system."
59 :group 'tools)
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'."
65 :group 'git
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'."
73 :group 'git
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."
79 :group 'git
80 :type '(choice (const :tag "From repository config" nil)
81 (coding-system)))
83 (defcustom git-append-signed-off-by nil
84 "Whether to append a Signed-off-by line to the commit message before editing."
85 :group 'git
86 :type 'boolean)
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."
91 :group 'git
92 :type 'boolean)
94 (defcustom git-per-dir-ignore-file ".gitignore"
95 "Name of the per-directory ignore file."
96 :group 'git
97 :type 'string)
99 (defcustom git-show-uptodate nil
100 "Whether to display up-to-date files."
101 :group 'git
102 :type 'boolean)
104 (defcustom git-show-ignored nil
105 "Whether to display ignored files."
106 :group 'git
107 :type 'boolean)
109 (defcustom git-show-unknown t
110 "Whether to display unknown files."
111 :group 'git
112 :type 'boolean)
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."
119 :group 'git)
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."
125 :group 'git)
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."
131 :group 'git)
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."
137 :group 'git)
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."
143 :group 'git)
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."
149 :group 'git)
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."
155 :group 'git)
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."
161 :group 'git)
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."
167 :group 'git)
170 ;;;; Utilities
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))
199 (erase-buffer)
200 (eq 0 (apply 'call-process "git" nil (list buffer t) nil args))))))
201 (unless ok (display-message-or-buffer buffer))
202 ok))
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."
207 (with-temp-buffer
208 (and (eq 0 (apply #' git-call-process-env t env args))
209 (buffer-string))))
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
216 (cd dir)
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))
228 (erase-buffer)
229 (apply #'git-call-process-env buffer nil args)))
230 (message "Running git %s...done" (car args))
231 buffer))
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 (if env
236 (git-run-process-region
237 buffer start end "env"
238 (append (git-get-env-strings env) (list "git") args))
239 (git-run-process-region
240 buffer start end "git" args)))
241 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string))))
243 (defun git-run-hook (hook env &rest args)
244 "Run a git hook and display its output if any."
245 (let ((dir default-directory)
246 (hook-name (expand-file-name (concat ".git/hooks/" hook))))
247 (or (not (file-executable-p hook-name))
248 (let (status (buffer (get-buffer-create "*Git Hook Output*")))
249 (with-current-buffer buffer
250 (erase-buffer)
251 (cd dir)
252 (setq status
253 (if env
254 (apply #'call-process "env" nil (list buffer t) nil
255 (append (git-get-env-strings env) (list hook-name) args))
256 (apply #'call-process hook-name nil (list buffer t) nil args))))
257 (display-message-or-buffer buffer)
258 (eq 0 status)))))
260 (defun git-get-string-sha1 (string)
261 "Read a SHA1 from the specified string."
262 (and string
263 (string-match "[0-9a-f]\\{40\\}" string)
264 (match-string 0 string)))
266 (defun git-get-committer-name ()
267 "Return the name to use as GIT_COMMITTER_NAME."
268 ; copied from log-edit
269 (or git-committer-name
270 (git-config "user.name")
271 (and (boundp 'add-log-full-name) add-log-full-name)
272 (and (fboundp 'user-full-name) (user-full-name))
273 (and (boundp 'user-full-name) user-full-name)))
275 (defun git-get-committer-email ()
276 "Return the email address to use as GIT_COMMITTER_EMAIL."
277 ; copied from log-edit
278 (or git-committer-email
279 (git-config "user.email")
280 (and (boundp 'add-log-mailing-address) add-log-mailing-address)
281 (and (fboundp 'user-mail-address) (user-mail-address))
282 (and (boundp 'user-mail-address) user-mail-address)))
284 (defun git-get-commits-coding-system ()
285 "Return the coding system to use for commits."
286 (let ((repo-config (git-config "i18n.commitencoding")))
287 (or git-commits-coding-system
288 (and repo-config
289 (fboundp 'locale-charset-to-coding-system)
290 (locale-charset-to-coding-system repo-config))
291 'utf-8)))
293 (defun git-get-logoutput-coding-system ()
294 "Return the coding system used for git-log output."
295 (let ((repo-config (or (git-config "i18n.logoutputencoding")
296 (git-config "i18n.commitencoding"))))
297 (or git-commits-coding-system
298 (and repo-config
299 (fboundp 'locale-charset-to-coding-system)
300 (locale-charset-to-coding-system repo-config))
301 'utf-8)))
303 (defun git-escape-file-name (name)
304 "Escape a file name if necessary."
305 (if (string-match "[\n\t\"\\]" name)
306 (concat "\""
307 (mapconcat (lambda (c)
308 (case c
309 (?\n "\\n")
310 (?\t "\\t")
311 (?\\ "\\\\")
312 (?\" "\\\"")
313 (t (char-to-string c))))
314 name "")
315 "\"")
316 name))
318 (defun git-success-message (text files)
319 "Print a success message after having handled FILES."
320 (let ((n (length files)))
321 (if (equal n 1)
322 (message "%s %s" text (car files))
323 (message "%s %d files" text n))))
325 (defun git-get-top-dir (dir)
326 "Retrieve the top-level directory of a git tree."
327 (let ((cdup (with-output-to-string
328 (with-current-buffer standard-output
329 (cd dir)
330 (unless (eq 0 (call-process "git" nil t nil "rev-parse" "--show-cdup"))
331 (error "cannot find top-level git tree for %s." dir))))))
332 (expand-file-name (concat (file-name-as-directory dir)
333 (car (split-string cdup "\n"))))))
335 ;stolen from pcl-cvs
336 (defun git-append-to-ignore (file)
337 "Add a file name to the ignore file in its directory."
338 (let* ((fullname (expand-file-name file))
339 (dir (file-name-directory fullname))
340 (name (file-name-nondirectory fullname))
341 (ignore-name (expand-file-name git-per-dir-ignore-file dir))
342 (created (not (file-exists-p ignore-name))))
343 (save-window-excursion
344 (set-buffer (find-file-noselect ignore-name))
345 (goto-char (point-max))
346 (unless (zerop (current-column)) (insert "\n"))
347 (insert "/" name "\n")
348 (sort-lines nil (point-min) (point-max))
349 (save-buffer))
350 (when created
351 (git-call-process-env nil nil "update-index" "--add" "--" (file-relative-name ignore-name)))
352 (git-update-status-files (list (file-relative-name ignore-name)) 'unknown)))
354 ; propertize definition for XEmacs, stolen from erc-compat
355 (eval-when-compile
356 (unless (fboundp 'propertize)
357 (defun propertize (string &rest props)
358 (let ((string (copy-sequence string)))
359 (while props
360 (put-text-property 0 (length string) (nth 0 props) (nth 1 props) string)
361 (setq props (cddr props)))
362 string))))
364 ;;;; Wrappers for basic git commands
365 ;;;; ------------------------------------------------------------
367 (defun git-rev-parse (rev)
368 "Parse a revision name and return its SHA1."
369 (git-get-string-sha1
370 (git-call-process-env-string nil "rev-parse" rev)))
372 (defun git-config (key)
373 "Retrieve the value associated to KEY in the git repository config file."
374 (let ((str (git-call-process-env-string nil "config" key)))
375 (and str (car (split-string str "\n")))))
377 (defun git-symbolic-ref (ref)
378 "Wrapper for the git-symbolic-ref command."
379 (let ((str (git-call-process-env-string nil "symbolic-ref" ref)))
380 (and str (car (split-string str "\n")))))
382 (defun git-update-ref (ref newval &optional oldval reason)
383 "Update a reference by calling git-update-ref."
384 (let ((args (and oldval (list oldval))))
385 (push newval args)
386 (push ref args)
387 (when reason
388 (push reason args)
389 (push "-m" args))
390 (apply 'git-call-process-display-error "update-ref" args)))
392 (defun git-read-tree (tree &optional index-file)
393 "Read a tree into the index file."
394 (apply #'git-call-process-env nil
395 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
396 "read-tree" (if tree (list tree))))
398 (defun git-write-tree (&optional index-file)
399 "Call git-write-tree and return the resulting tree SHA1 as a string."
400 (git-get-string-sha1
401 (git-call-process-env-string (and index-file `(("GIT_INDEX_FILE" . ,index-file))) "write-tree")))
403 (defun git-commit-tree (buffer tree head)
404 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
405 (let ((author-name (git-get-committer-name))
406 (author-email (git-get-committer-email))
407 (subject "commit (initial): ")
408 author-date log-start log-end args coding-system-for-write)
409 (when head
410 (setq subject "commit: ")
411 (push "-p" args)
412 (push head args))
413 (with-current-buffer buffer
414 (goto-char (point-min))
416 (setq log-start (re-search-forward (concat "^" (regexp-quote git-log-msg-separator) "\n") nil t))
417 (save-restriction
418 (narrow-to-region (point-min) log-start)
419 (goto-char (point-min))
420 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t)
421 (setq author-name (match-string 1)
422 author-email (match-string 2)))
423 (goto-char (point-min))
424 (when (re-search-forward "^Date: +\\(.*\\)$" nil t)
425 (setq author-date (match-string 1)))
426 (goto-char (point-min))
427 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t)
428 (unless (string-equal head (match-string 1))
429 (setq subject "commit (merge): ")
430 (push "-p" args)
431 (push (match-string 1) args))))
432 (setq log-start (point-min)))
433 (setq log-end (point-max))
434 (goto-char log-start)
435 (when (re-search-forward ".*$" nil t)
436 (setq subject (concat subject (match-string 0))))
437 (setq coding-system-for-write buffer-file-coding-system))
438 (let ((commit
439 (git-get-string-sha1
440 (with-output-to-string
441 (with-current-buffer standard-output
442 (let ((env `(("GIT_AUTHOR_NAME" . ,author-name)
443 ("GIT_AUTHOR_EMAIL" . ,author-email)
444 ("GIT_COMMITTER_NAME" . ,(git-get-committer-name))
445 ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email)))))
446 (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env))
447 (apply #'git-run-command-region
448 buffer log-start log-end env
449 "commit-tree" tree (nreverse args))))))))
450 (and (git-update-ref "HEAD" commit head subject)
451 commit))))
453 (defun git-empty-db-p ()
454 "Check if the git db is empty (no commit done yet)."
455 (not (eq 0 (call-process "git" nil nil nil "rev-parse" "--verify" "HEAD"))))
457 (defun git-get-merge-heads ()
458 "Retrieve the merge heads from the MERGE_HEAD file if present."
459 (let (heads)
460 (when (file-readable-p ".git/MERGE_HEAD")
461 (with-temp-buffer
462 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t)
463 (goto-char (point-min))
464 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t)
465 (push (match-string 0) heads))))
466 (nreverse heads)))
468 (defun git-get-commit-description (commit)
469 "Get a one-line description of COMMIT."
470 (let ((coding-system-for-read (git-get-logoutput-coding-system)))
471 (let ((descr (git-call-process-env-string nil "log" "--max-count=1" "--pretty=oneline" commit)))
472 (if (and descr (string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr))
473 (concat (substring (match-string 1 descr) 0 10) " - " (match-string 2 descr))
474 descr))))
476 ;;;; File info structure
477 ;;;; ------------------------------------------------------------
479 ; fileinfo structure stolen from pcl-cvs
480 (defstruct (git-fileinfo
481 (:copier nil)
482 (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked))
483 (:conc-name git-fileinfo->))
484 marked ;; t/nil
485 state ;; current state
486 name ;; file name
487 old-perm new-perm ;; permission flags
488 rename-state ;; rename or copy state
489 orig-name ;; original name for renames or copies
490 needs-refresh) ;; whether file needs to be refreshed
492 (defvar git-status nil)
494 (defun git-clear-status (status)
495 "Remove everything from the status list."
496 (ewoc-filter status (lambda (info) nil)))
498 (defun git-set-fileinfo-state (info state)
499 "Set the state of a file info."
500 (unless (eq (git-fileinfo->state info) state)
501 (setf (git-fileinfo->state info) state
502 (git-fileinfo->new-perm info) (git-fileinfo->old-perm info)
503 (git-fileinfo->rename-state info) nil
504 (git-fileinfo->orig-name info) nil
505 (git-fileinfo->needs-refresh info) t)))
507 (defun git-status-filenames-map (status func files &rest args)
508 "Apply FUNC to the status files names in the FILES list."
509 (when files
510 (setq files (sort files #'string-lessp))
511 (let ((file (pop files))
512 (node (ewoc-nth status 0)))
513 (while (and file node)
514 (let ((info (ewoc-data node)))
515 (if (string-lessp (git-fileinfo->name info) file)
516 (setq node (ewoc-next status node))
517 (if (string-equal (git-fileinfo->name info) file)
518 (apply func info args))
519 (setq file (pop files))))))))
521 (defun git-set-filenames-state (status files state)
522 "Set the state of a list of named files."
523 (when files
524 (git-status-filenames-map status #'git-set-fileinfo-state files state)
525 (unless state ;; delete files whose state has been set to nil
526 (ewoc-filter status (lambda (info) (git-fileinfo->state info))))))
528 (defun git-state-code (code)
529 "Convert from a string to a added/deleted/modified state."
530 (case (string-to-char code)
531 (?M 'modified)
532 (?? 'unknown)
533 (?A 'added)
534 (?D 'deleted)
535 (?U 'unmerged)
536 (?T 'modified)
537 (t nil)))
539 (defun git-status-code-as-string (code)
540 "Format a git status code as string."
541 (case code
542 ('modified (propertize "Modified" 'face 'git-status-face))
543 ('unknown (propertize "Unknown " 'face 'git-unknown-face))
544 ('added (propertize "Added " 'face 'git-status-face))
545 ('deleted (propertize "Deleted " 'face 'git-status-face))
546 ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face))
547 ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face))
548 ('ignored (propertize "Ignored " 'face 'git-ignored-face))
549 (t "? ")))
551 (defun git-file-type-as-string (old-perm new-perm)
552 "Return a string describing the file type based on its permissions."
553 (let* ((old-type (lsh (or old-perm 0) -9))
554 (new-type (lsh (or new-perm 0) -9))
555 (str (case new-type
556 (?\100 ;; file
557 (case old-type
558 (?\100 nil)
559 (?\120 " (type change symlink -> file)")
560 (?\160 " (type change subproject -> file)")))
561 (?\120 ;; symlink
562 (case old-type
563 (?\100 " (type change file -> symlink)")
564 (?\160 " (type change subproject -> symlink)")
565 (t " (symlink)")))
566 (?\160 ;; subproject
567 (case old-type
568 (?\100 " (type change file -> subproject)")
569 (?\120 " (type change symlink -> subproject)")
570 (t " (subproject)")))
571 (?\110 nil) ;; directory (internal, not a real git state)
572 (?\000 ;; deleted or unknown
573 (case old-type
574 (?\120 " (symlink)")
575 (?\160 " (subproject)")))
576 (t (format " (unknown type %o)" new-type)))))
577 (cond (str (propertize str 'face 'git-status-face))
578 ((eq new-type ?\110) "/")
579 (t ""))))
581 (defun git-rename-as-string (info)
582 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
583 (let ((state (git-fileinfo->rename-state info)))
584 (if state
585 (propertize
586 (concat " ("
587 (if (eq state 'copy) "copied from "
588 (if (eq (git-fileinfo->state info) 'added) "renamed from "
589 "renamed to "))
590 (git-escape-file-name (git-fileinfo->orig-name info))
591 ")") 'face 'git-status-face)
592 "")))
594 (defun git-permissions-as-string (old-perm new-perm)
595 "Format a permission change as string."
596 (propertize
597 (if (or (not old-perm)
598 (not new-perm)
599 (eq 0 (logand ?\111 (logxor old-perm new-perm))))
601 (if (eq 0 (logand ?\111 old-perm)) "+x" "-x"))
602 'face 'git-permission-face))
604 (defun git-fileinfo-prettyprint (info)
605 "Pretty-printer for the git-fileinfo structure."
606 (let ((old-perm (git-fileinfo->old-perm info))
607 (new-perm (git-fileinfo->new-perm info)))
608 (insert (concat " " (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ")
609 " " (git-status-code-as-string (git-fileinfo->state info))
610 " " (git-permissions-as-string old-perm new-perm)
611 " " (git-escape-file-name (git-fileinfo->name info))
612 (git-file-type-as-string old-perm new-perm)
613 (git-rename-as-string info)))))
615 (defun git-insert-info-list (status infolist)
616 "Insert a list of file infos in the status buffer, replacing existing ones if any."
617 (setq infolist (sort infolist
618 (lambda (info1 info2)
619 (string-lessp (git-fileinfo->name info1)
620 (git-fileinfo->name info2)))))
621 (let ((info (pop infolist))
622 (node (ewoc-nth status 0)))
623 (while info
624 (cond ((not node)
625 (setq node (ewoc-enter-last status info))
626 (setq info (pop infolist)))
627 ((string-lessp (git-fileinfo->name (ewoc-data node))
628 (git-fileinfo->name info))
629 (setq node (ewoc-next status node)))
630 ((string-equal (git-fileinfo->name (ewoc-data node))
631 (git-fileinfo->name info))
632 ;; preserve the marked flag
633 (setf (git-fileinfo->marked info) (git-fileinfo->marked (ewoc-data node)))
634 (setf (git-fileinfo->needs-refresh info) t)
635 (setf (ewoc-data node) info)
636 (setq info (pop infolist)))
638 (setq node (ewoc-enter-before status node info))
639 (setq info (pop infolist)))))))
641 (defun git-run-diff-index (status files)
642 "Run git-diff-index on FILES and parse the results into STATUS.
643 Return the list of files that haven't been handled."
644 (let ((remaining (copy-sequence files))
645 infolist)
646 (with-temp-buffer
647 (apply #'git-call-process-env t nil "diff-index" "-z" "-M" "HEAD" "--" files)
648 (goto-char (point-min))
649 (while (re-search-forward
650 ":\\([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"
651 nil t 1)
652 (let ((old-perm (string-to-number (match-string 1) 8))
653 (new-perm (string-to-number (match-string 2) 8))
654 (state (or (match-string 4) (match-string 6)))
655 (name (or (match-string 5) (match-string 7)))
656 (new-name (match-string 8)))
657 (if new-name ; copy or rename
658 (if (eq ?C (string-to-char state))
659 (push (git-create-fileinfo 'added new-name old-perm new-perm 'copy name) infolist)
660 (push (git-create-fileinfo 'deleted name 0 0 'rename new-name) infolist)
661 (push (git-create-fileinfo 'added new-name old-perm new-perm 'rename name) infolist))
662 (push (git-create-fileinfo (git-state-code state) name old-perm new-perm) infolist))
663 (setq remaining (delete name remaining))
664 (when new-name (setq remaining (delete new-name remaining))))))
665 (git-insert-info-list status infolist)
666 remaining))
668 (defun git-find-status-file (status file)
669 "Find a given file in the status ewoc and return its node."
670 (let ((node (ewoc-nth status 0)))
671 (while (and node (not (string= file (git-fileinfo->name (ewoc-data node)))))
672 (setq node (ewoc-next status node)))
673 node))
675 (defun git-run-ls-files (status files default-state &rest options)
676 "Run git-ls-files on FILES and parse the results into STATUS.
677 Return the list of files that haven't been handled."
678 (let (infolist)
679 (with-temp-buffer
680 (apply #'git-call-process-env t nil "ls-files" "-z" (append options (list "--") files))
681 (goto-char (point-min))
682 (while (re-search-forward "\\([^\0]*?\\)\\(/?\\)\0" nil t 1)
683 (let ((name (match-string 1)))
684 (push (git-create-fileinfo default-state name 0
685 (if (string-equal "/" (match-string 2)) (lsh ?\110 9) 0))
686 infolist)
687 (setq files (delete name files)))))
688 (git-insert-info-list status infolist)
689 files))
691 (defun git-run-ls-files-cached (status files default-state)
692 "Run git-ls-files -c on FILES and parse the results into STATUS.
693 Return the list of files that haven't been handled."
694 (let ((remaining (copy-sequence files))
695 infolist)
696 (with-temp-buffer
697 (apply #'git-call-process-env t nil "ls-files" "-z" "-s" "-c" "--" files)
698 (goto-char (point-min))
699 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
700 (let* ((new-perm (string-to-number (match-string 1) 8))
701 (old-perm (if (eq default-state 'added) 0 new-perm))
702 (name (match-string 2)))
703 (push (git-create-fileinfo default-state name old-perm new-perm) infolist)
704 (setq remaining (delete name remaining)))))
705 (git-insert-info-list status infolist)
706 remaining))
708 (defun git-run-ls-unmerged (status files)
709 "Run git-ls-files -u on FILES and parse the results into STATUS."
710 (with-temp-buffer
711 (apply #'git-call-process-env t nil "ls-files" "-z" "-u" "--" files)
712 (goto-char (point-min))
713 (let (unmerged-files)
714 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t)
715 (push (match-string 1) unmerged-files))
716 (git-set-filenames-state status unmerged-files 'unmerged))))
718 (defun git-get-exclude-files ()
719 "Get the list of exclude files to pass to git-ls-files."
720 (let (files
721 (config (git-config "core.excludesfile")))
722 (when (file-readable-p ".git/info/exclude")
723 (push ".git/info/exclude" files))
724 (when (and config (file-readable-p config))
725 (push config files))
726 files))
728 (defun git-run-ls-files-with-excludes (status files default-state &rest options)
729 "Run git-ls-files on FILES with appropriate --exclude-from options."
730 (let ((exclude-files (git-get-exclude-files)))
731 (apply #'git-run-ls-files status files default-state "--directory" "--no-empty-directory"
732 (concat "--exclude-per-directory=" git-per-dir-ignore-file)
733 (append options (mapcar (lambda (f) (concat "--exclude-from=" f)) exclude-files)))))
735 (defun git-update-status-files (files &optional default-state)
736 "Update the status of FILES from the index."
737 (unless git-status (error "Not in git-status buffer."))
738 (when (or git-show-uptodate files)
739 (git-run-ls-files-cached git-status files 'uptodate))
740 (let* ((remaining-files
741 (if (git-empty-db-p) ; we need some special handling for an empty db
742 (git-run-ls-files-cached git-status files 'added)
743 (git-run-diff-index git-status files))))
744 (git-run-ls-unmerged git-status files)
745 (when (or remaining-files (and git-show-unknown (not files)))
746 (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'unknown "-o")))
747 (when (or remaining-files (and git-show-ignored (not files)))
748 (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'ignored "-o" "-i")))
749 (git-set-filenames-state git-status remaining-files default-state)
750 (git-refresh-files)
751 (git-refresh-ewoc-hf git-status)))
753 (defun git-mark-files (status files)
754 "Mark all the specified FILES, and unmark the others."
755 (setq files (sort files #'string-lessp))
756 (let ((file (and files (pop files)))
757 (node (ewoc-nth status 0)))
758 (while node
759 (let ((info (ewoc-data node)))
760 (if (and file (string-equal (git-fileinfo->name info) file))
761 (progn
762 (unless (git-fileinfo->marked info)
763 (setf (git-fileinfo->marked info) t)
764 (setf (git-fileinfo->needs-refresh info) t))
765 (setq file (pop files))
766 (setq node (ewoc-next status node)))
767 (when (git-fileinfo->marked info)
768 (setf (git-fileinfo->marked info) nil)
769 (setf (git-fileinfo->needs-refresh info) t))
770 (if (and file (string-lessp file (git-fileinfo->name info)))
771 (setq file (pop files))
772 (setq node (ewoc-next status node))))))))
774 (defun git-marked-files ()
775 "Return a list of all marked files, or if none a list containing just the file at cursor position."
776 (unless git-status (error "Not in git-status buffer."))
777 (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info)))
778 (list (ewoc-data (ewoc-locate git-status)))))
780 (defun git-marked-files-state (&rest states)
781 "Return marked files that are in the specified states."
782 (let ((files (git-marked-files))
783 result)
784 (dolist (info files)
785 (when (memq (git-fileinfo->state info) states)
786 (push info result)))
787 result))
789 (defun git-refresh-files ()
790 "Refresh all files that need it and clear the needs-refresh flag."
791 (unless git-status (error "Not in git-status buffer."))
792 (ewoc-map
793 (lambda (info)
794 (let ((refresh (git-fileinfo->needs-refresh info)))
795 (setf (git-fileinfo->needs-refresh info) nil)
796 refresh))
797 git-status)
798 ; move back to goal column
799 (when goal-column (move-to-column goal-column)))
801 (defun git-refresh-ewoc-hf (status)
802 "Refresh the ewoc header and footer."
803 (let ((branch (git-symbolic-ref "HEAD"))
804 (head (if (git-empty-db-p) "Nothing committed yet"
805 (git-get-commit-description "HEAD")))
806 (merge-heads (git-get-merge-heads)))
807 (ewoc-set-hf status
808 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
809 default-directory
810 (if branch
811 (if (string-match "^refs/heads/" branch)
812 (substring branch (match-end 0))
813 branch)
814 "none (detached HEAD)")
815 head
816 (if merge-heads
817 (concat "\nMerging: "
818 (mapconcat (lambda (str) (git-get-commit-description str)) merge-heads "\n "))
819 ""))
820 (if (ewoc-nth status 0) "" " No changes."))))
822 (defun git-get-filenames (files)
823 (mapcar (lambda (info) (git-fileinfo->name info)) files))
825 (defun git-update-index (index-file files)
826 "Run git-update-index on a list of files."
827 (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file))))
828 added deleted modified)
829 (dolist (info files)
830 (case (git-fileinfo->state info)
831 ('added (push info added))
832 ('deleted (push info deleted))
833 ('modified (push info modified))))
834 (when added
835 (apply #'git-call-process-env nil env "update-index" "--add" "--" (git-get-filenames added)))
836 (when deleted
837 (apply #'git-call-process-env nil env "update-index" "--remove" "--" (git-get-filenames deleted)))
838 (when modified
839 (apply #'git-call-process-env nil env "update-index" "--" (git-get-filenames modified)))))
841 (defun git-run-pre-commit-hook ()
842 "Run the pre-commit hook if any."
843 (unless git-status (error "Not in git-status buffer."))
844 (let ((files (git-marked-files-state 'added 'deleted 'modified)))
845 (or (not files)
846 (not (file-executable-p ".git/hooks/pre-commit"))
847 (let ((index-file (make-temp-file "gitidx")))
848 (unwind-protect
849 (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}"))))
850 (git-read-tree head-tree index-file)
851 (git-update-index index-file files)
852 (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" . ,index-file))))
853 (delete-file index-file))))))
855 (defun git-do-commit ()
856 "Perform the actual commit using the current buffer as log message."
857 (interactive)
858 (let ((buffer (current-buffer))
859 (index-file (make-temp-file "gitidx")))
860 (with-current-buffer log-edit-parent-buffer
861 (if (git-marked-files-state 'unmerged)
862 (message "You cannot commit unmerged files, resolve them first.")
863 (unwind-protect
864 (let ((files (git-marked-files-state 'added 'deleted 'modified))
865 head head-tree)
866 (unless (git-empty-db-p)
867 (setq head (git-rev-parse "HEAD")
868 head-tree (git-rev-parse "HEAD^{tree}")))
869 (if files
870 (progn
871 (message "Running git commit...")
872 (git-read-tree head-tree index-file)
873 (git-update-index nil files) ;update both the default index
874 (git-update-index index-file files) ;and the temporary one
875 (let ((tree (git-write-tree index-file)))
876 (if (or (not (string-equal tree head-tree))
877 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
878 (let ((commit (git-commit-tree buffer tree head)))
879 (when commit
880 (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil))
881 (condition-case nil (delete-file ".git/MERGE_MSG") (error nil))
882 (with-current-buffer buffer (erase-buffer))
883 (git-update-status-files (git-get-filenames files) 'uptodate)
884 (git-call-process-env nil nil "rerere")
885 (git-call-process-env nil nil "gc" "--auto")
886 (git-refresh-files)
887 (git-refresh-ewoc-hf git-status)
888 (message "Committed %s." commit)
889 (git-run-hook "post-commit" nil)))
890 (message "Commit aborted."))))
891 (message "No files to commit.")))
892 (delete-file index-file))))))
895 ;;;; Interactive functions
896 ;;;; ------------------------------------------------------------
898 (defun git-mark-file ()
899 "Mark the file that the cursor is on and move to the next one."
900 (interactive)
901 (unless git-status (error "Not in git-status buffer."))
902 (let* ((pos (ewoc-locate git-status))
903 (info (ewoc-data pos)))
904 (setf (git-fileinfo->marked info) t)
905 (ewoc-invalidate git-status pos)
906 (ewoc-goto-next git-status 1)))
908 (defun git-unmark-file ()
909 "Unmark the file that the cursor is on and move to the next one."
910 (interactive)
911 (unless git-status (error "Not in git-status buffer."))
912 (let* ((pos (ewoc-locate git-status))
913 (info (ewoc-data pos)))
914 (setf (git-fileinfo->marked info) nil)
915 (ewoc-invalidate git-status pos)
916 (ewoc-goto-next git-status 1)))
918 (defun git-unmark-file-up ()
919 "Unmark the file that the cursor is on and move to the previous one."
920 (interactive)
921 (unless git-status (error "Not in git-status buffer."))
922 (let* ((pos (ewoc-locate git-status))
923 (info (ewoc-data pos)))
924 (setf (git-fileinfo->marked info) nil)
925 (ewoc-invalidate git-status pos)
926 (ewoc-goto-prev git-status 1)))
928 (defun git-mark-all ()
929 "Mark all files."
930 (interactive)
931 (unless git-status (error "Not in git-status buffer."))
932 (ewoc-map (lambda (info) (unless (git-fileinfo->marked info)
933 (setf (git-fileinfo->marked info) t))) git-status)
934 ; move back to goal column after invalidate
935 (when goal-column (move-to-column goal-column)))
937 (defun git-unmark-all ()
938 "Unmark all files."
939 (interactive)
940 (unless git-status (error "Not in git-status buffer."))
941 (ewoc-map (lambda (info) (when (git-fileinfo->marked info)
942 (setf (git-fileinfo->marked info) nil)
943 t)) git-status)
944 ; move back to goal column after invalidate
945 (when goal-column (move-to-column goal-column)))
947 (defun git-toggle-all-marks ()
948 "Toggle all file marks."
949 (interactive)
950 (unless git-status (error "Not in git-status buffer."))
951 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status)
952 ; move back to goal column after invalidate
953 (when goal-column (move-to-column goal-column)))
955 (defun git-next-file (&optional n)
956 "Move the selection down N files."
957 (interactive "p")
958 (unless git-status (error "Not in git-status buffer."))
959 (ewoc-goto-next git-status n))
961 (defun git-prev-file (&optional n)
962 "Move the selection up N files."
963 (interactive "p")
964 (unless git-status (error "Not in git-status buffer."))
965 (ewoc-goto-prev git-status n))
967 (defun git-next-unmerged-file (&optional n)
968 "Move the selection down N unmerged files."
969 (interactive "p")
970 (unless git-status (error "Not in git-status buffer."))
971 (let* ((last (ewoc-locate git-status))
972 (node (ewoc-next git-status last)))
973 (while (and node (> n 0))
974 (when (eq 'unmerged (git-fileinfo->state (ewoc-data node)))
975 (setq n (1- n))
976 (setq last node))
977 (setq node (ewoc-next git-status node)))
978 (ewoc-goto-node git-status last)))
980 (defun git-prev-unmerged-file (&optional n)
981 "Move the selection up N unmerged files."
982 (interactive "p")
983 (unless git-status (error "Not in git-status buffer."))
984 (let* ((last (ewoc-locate git-status))
985 (node (ewoc-prev git-status last)))
986 (while (and node (> n 0))
987 (when (eq 'unmerged (git-fileinfo->state (ewoc-data node)))
988 (setq n (1- n))
989 (setq last node))
990 (setq node (ewoc-prev git-status node)))
991 (ewoc-goto-node git-status last)))
993 (defun git-add-file ()
994 "Add marked file(s) to the index cache."
995 (interactive)
996 (let ((files (git-get-filenames (git-marked-files-state 'unknown 'ignored))))
997 ;; FIXME: add support for directories
998 (unless files
999 (push (file-relative-name (read-file-name "File to add: " nil nil t)) files))
1000 (when (apply 'git-call-process-display-error "update-index" "--add" "--" files)
1001 (git-update-status-files files 'uptodate)
1002 (git-success-message "Added" files))))
1004 (defun git-ignore-file ()
1005 "Add marked file(s) to the ignore list."
1006 (interactive)
1007 (let ((files (git-get-filenames (git-marked-files-state 'unknown))))
1008 (unless files
1009 (push (file-relative-name (read-file-name "File to ignore: " nil nil t)) files))
1010 (dolist (f files) (git-append-to-ignore f))
1011 (git-update-status-files files 'ignored)
1012 (git-success-message "Ignored" files)))
1014 (defun git-remove-file ()
1015 "Remove the marked file(s)."
1016 (interactive)
1017 (let ((files (git-get-filenames (git-marked-files-state 'added 'modified 'unknown 'uptodate 'ignored))))
1018 (unless files
1019 (push (file-relative-name (read-file-name "File to remove: " nil nil t)) files))
1020 (if (yes-or-no-p
1021 (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" "")))
1022 (progn
1023 (dolist (name files)
1024 (ignore-errors
1025 (if (file-directory-p name)
1026 (delete-directory name)
1027 (delete-file name))))
1028 (when (apply 'git-call-process-display-error "update-index" "--remove" "--" files)
1029 (git-update-status-files files nil)
1030 (git-success-message "Removed" files)))
1031 (message "Aborting"))))
1033 (defun git-revert-file ()
1034 "Revert changes to the marked file(s)."
1035 (interactive)
1036 (let ((files (git-marked-files-state 'added 'deleted 'modified 'unmerged))
1037 added modified)
1038 (when (and files
1039 (yes-or-no-p
1040 (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" ""))))
1041 (dolist (info files)
1042 (case (git-fileinfo->state info)
1043 ('added (push (git-fileinfo->name info) added))
1044 ('deleted (push (git-fileinfo->name info) modified))
1045 ('unmerged (push (git-fileinfo->name info) modified))
1046 ('modified (push (git-fileinfo->name info) modified))))
1047 ;; check if a buffer contains one of the files and isn't saved
1048 (dolist (file modified)
1049 (let ((buffer (get-file-buffer file)))
1050 (when (and buffer (buffer-modified-p buffer))
1051 (error "Buffer %s is modified. Please kill or save modified buffers before reverting." (buffer-name buffer)))))
1052 (let ((ok (and
1053 (or (not added)
1054 (apply 'git-call-process-display-error "update-index" "--force-remove" "--" added))
1055 (or (not modified)
1056 (apply 'git-call-process-display-error "checkout" "HEAD" modified)))))
1057 (git-update-status-files (append added modified) 'uptodate)
1058 (when ok
1059 (dolist (file modified)
1060 (let ((buffer (get-file-buffer file)))
1061 (when buffer (with-current-buffer buffer (revert-buffer t t t)))))
1062 (git-success-message "Reverted" (git-get-filenames files)))))))
1064 (defun git-resolve-file ()
1065 "Resolve conflicts in marked file(s)."
1066 (interactive)
1067 (let ((files (git-get-filenames (git-marked-files-state 'unmerged))))
1068 (when files
1069 (when (apply 'git-call-process-display-error "update-index" "--" files)
1070 (git-update-status-files files 'uptodate)
1071 (git-success-message "Resolved" files)))))
1073 (defun git-remove-handled ()
1074 "Remove handled files from the status list."
1075 (interactive)
1076 (ewoc-filter git-status
1077 (lambda (info)
1078 (case (git-fileinfo->state info)
1079 ('ignored git-show-ignored)
1080 ('uptodate git-show-uptodate)
1081 ('unknown git-show-unknown)
1082 (t t))))
1083 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
1084 (git-refresh-ewoc-hf git-status)))
1086 (defun git-toggle-show-uptodate ()
1087 "Toogle the option for showing up-to-date files."
1088 (interactive)
1089 (if (setq git-show-uptodate (not git-show-uptodate))
1090 (git-refresh-status)
1091 (git-remove-handled)))
1093 (defun git-toggle-show-ignored ()
1094 "Toogle the option for showing ignored files."
1095 (interactive)
1096 (if (setq git-show-ignored (not git-show-ignored))
1097 (progn
1098 (message "Inserting ignored files...")
1099 (git-run-ls-files-with-excludes git-status nil 'ignored "-o" "-i")
1100 (git-refresh-files)
1101 (git-refresh-ewoc-hf git-status)
1102 (message "Inserting ignored files...done"))
1103 (git-remove-handled)))
1105 (defun git-toggle-show-unknown ()
1106 "Toogle the option for showing unknown files."
1107 (interactive)
1108 (if (setq git-show-unknown (not git-show-unknown))
1109 (progn
1110 (message "Inserting unknown files...")
1111 (git-run-ls-files-with-excludes git-status nil 'unknown "-o")
1112 (git-refresh-files)
1113 (git-refresh-ewoc-hf git-status)
1114 (message "Inserting unknown files...done"))
1115 (git-remove-handled)))
1117 (defun git-expand-directory (info)
1118 "Expand the directory represented by INFO to list its files."
1119 (when (eq (lsh (git-fileinfo->new-perm info) -9) ?\110)
1120 (let ((dir (git-fileinfo->name info)))
1121 (git-set-filenames-state git-status (list dir) nil)
1122 (git-run-ls-files-with-excludes git-status (list (concat dir "/")) 'unknown "-o")
1123 (git-refresh-files)
1124 (git-refresh-ewoc-hf git-status)
1125 t)))
1127 (defun git-setup-diff-buffer (buffer)
1128 "Setup a buffer for displaying a diff."
1129 (let ((dir default-directory))
1130 (with-current-buffer buffer
1131 (diff-mode)
1132 (goto-char (point-min))
1133 (setq default-directory dir)
1134 (setq buffer-read-only t)))
1135 (display-buffer buffer)
1136 ; shrink window only if it displays the status buffer
1137 (when (eq (window-buffer) (current-buffer))
1138 (shrink-window-if-larger-than-buffer)))
1140 (defun git-diff-file ()
1141 "Diff the marked file(s) against HEAD."
1142 (interactive)
1143 (let ((files (git-marked-files)))
1144 (git-setup-diff-buffer
1145 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files)))))
1147 (defun git-diff-file-merge-head (arg)
1148 "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)."
1149 (interactive "p")
1150 (let ((files (git-marked-files))
1151 (merge-heads (git-get-merge-heads)))
1152 (unless merge-heads (error "No merge in progress"))
1153 (git-setup-diff-buffer
1154 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M"
1155 (or (nth (1- arg) merge-heads) "HEAD") "--" (git-get-filenames files)))))
1157 (defun git-diff-unmerged-file (stage)
1158 "Diff the marked unmerged file(s) against the specified stage."
1159 (let ((files (git-marked-files)))
1160 (git-setup-diff-buffer
1161 (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files)))))
1163 (defun git-diff-file-base ()
1164 "Diff the marked unmerged file(s) against the common base file."
1165 (interactive)
1166 (git-diff-unmerged-file "-1"))
1168 (defun git-diff-file-mine ()
1169 "Diff the marked unmerged file(s) against my pre-merge version."
1170 (interactive)
1171 (git-diff-unmerged-file "-2"))
1173 (defun git-diff-file-other ()
1174 "Diff the marked unmerged file(s) against the other's pre-merge version."
1175 (interactive)
1176 (git-diff-unmerged-file "-3"))
1178 (defun git-diff-file-combined ()
1179 "Do a combined diff of the marked unmerged file(s)."
1180 (interactive)
1181 (git-diff-unmerged-file "-c"))
1183 (defun git-diff-file-idiff ()
1184 "Perform an interactive diff on the current file."
1185 (interactive)
1186 (let ((files (git-marked-files-state 'added 'deleted 'modified)))
1187 (unless (eq 1 (length files))
1188 (error "Cannot perform an interactive diff on multiple files."))
1189 (let* ((filename (car (git-get-filenames files)))
1190 (buff1 (find-file-noselect filename))
1191 (buff2 (git-run-command-buffer (concat filename ".~HEAD~") "cat-file" "blob" (concat "HEAD:" filename))))
1192 (ediff-buffers buff1 buff2))))
1194 (defun git-log-file ()
1195 "Display a log of changes to the marked file(s)."
1196 (interactive)
1197 (let* ((files (git-marked-files))
1198 (coding-system-for-read git-commits-coding-system)
1199 (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files))))
1200 (with-current-buffer buffer
1201 ; (git-log-mode) FIXME: implement log mode
1202 (goto-char (point-min))
1203 (setq buffer-read-only t))
1204 (display-buffer buffer)))
1206 (defun git-log-edit-files ()
1207 "Return a list of marked files for use in the log-edit buffer."
1208 (with-current-buffer log-edit-parent-buffer
1209 (git-get-filenames (git-marked-files-state 'added 'deleted 'modified))))
1211 (defun git-log-edit-diff ()
1212 "Run a diff of the current files being committed from a log-edit buffer."
1213 (with-current-buffer log-edit-parent-buffer
1214 (git-diff-file)))
1216 (defun git-append-sign-off (name email)
1217 "Append a Signed-off-by entry to the current buffer, avoiding duplicates."
1218 (let ((sign-off (format "Signed-off-by: %s <%s>" name email))
1219 (case-fold-search t))
1220 (goto-char (point-min))
1221 (unless (re-search-forward (concat "^" (regexp-quote sign-off)) nil t)
1222 (goto-char (point-min))
1223 (unless (re-search-forward "^Signed-off-by: " nil t)
1224 (setq sign-off (concat "\n" sign-off)))
1225 (goto-char (point-max))
1226 (insert sign-off "\n"))))
1228 (defun git-setup-log-buffer (buffer &optional author-name author-email subject date msg)
1229 "Setup the log buffer for a commit."
1230 (unless git-status (error "Not in git-status buffer."))
1231 (let ((merge-heads (git-get-merge-heads))
1232 (dir default-directory)
1233 (committer-name (git-get-committer-name))
1234 (committer-email (git-get-committer-email))
1235 (sign-off git-append-signed-off-by))
1236 (with-current-buffer buffer
1237 (cd dir)
1238 (erase-buffer)
1239 (insert
1240 (propertize
1241 (format "Author: %s <%s>\n%s%s"
1242 (or author-name committer-name)
1243 (or author-email committer-email)
1244 (if date (format "Date: %s\n" date) "")
1245 (if merge-heads
1246 (format "Parent: %s\n%s\n"
1247 (git-rev-parse "HEAD")
1248 (mapconcat (lambda (str) (concat "Parent: " str)) merge-heads "\n"))
1249 ""))
1250 'face 'git-header-face)
1251 (propertize git-log-msg-separator 'face 'git-separator-face)
1252 "\n")
1253 (when subject (insert subject "\n\n"))
1254 (cond (msg (insert msg "\n"))
1255 ((file-readable-p ".dotest/msg")
1256 (insert-file-contents ".dotest/msg"))
1257 ((file-readable-p ".git/MERGE_MSG")
1258 (insert-file-contents ".git/MERGE_MSG")))
1259 ; delete empty lines at end
1260 (goto-char (point-min))
1261 (when (re-search-forward "\n+\\'" nil t)
1262 (replace-match "\n" t t))
1263 (when sign-off (git-append-sign-off committer-name committer-email)))
1264 buffer))
1266 (defun git-commit-file ()
1267 "Commit the marked file(s), asking for a commit message."
1268 (interactive)
1269 (unless git-status (error "Not in git-status buffer."))
1270 (when (git-run-pre-commit-hook)
1271 (let ((buffer (get-buffer-create "*git-commit*"))
1272 (coding-system (git-get-commits-coding-system))
1273 author-name author-email subject date)
1274 (when (eq 0 (buffer-size buffer))
1275 (when (file-readable-p ".dotest/info")
1276 (with-temp-buffer
1277 (insert-file-contents ".dotest/info")
1278 (goto-char (point-min))
1279 (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t)
1280 (setq author-name (match-string 1))
1281 (setq author-email (match-string 2)))
1282 (goto-char (point-min))
1283 (when (re-search-forward "^Subject: \\(.*\\)$" nil t)
1284 (setq subject (match-string 1)))
1285 (goto-char (point-min))
1286 (when (re-search-forward "^Date: \\(.*\\)$" nil t)
1287 (setq date (match-string 1)))))
1288 (git-setup-log-buffer buffer author-name author-email subject date))
1289 (if (boundp 'log-edit-diff-function)
1290 (log-edit 'git-do-commit nil '((log-edit-listfun . git-log-edit-files)
1291 (log-edit-diff-function . git-log-edit-diff)) buffer)
1292 (log-edit 'git-do-commit nil 'git-log-edit-files buffer))
1293 (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords))
1294 (setq buffer-file-coding-system coding-system)
1295 (re-search-forward (regexp-quote (concat git-log-msg-separator "\n")) nil t))))
1297 (defun git-setup-commit-buffer (commit)
1298 "Setup the commit buffer with the contents of COMMIT."
1299 (let (author-name author-email subject date msg)
1300 (with-temp-buffer
1301 (let ((coding-system (git-get-logoutput-coding-system)))
1302 (git-call-process-env t nil "log" "-1" "--pretty=medium" commit)
1303 (goto-char (point-min))
1304 (when (re-search-forward "^Author: *\\(.*\\) <\\(.*\\)>$" nil t)
1305 (setq author-name (match-string 1))
1306 (setq author-email (match-string 2)))
1307 (when (re-search-forward "^Date: *\\(.*\\)$" nil t)
1308 (setq date (match-string 1)))
1309 (while (re-search-forward "^ \\(.*\\)$" nil t)
1310 (push (match-string 1) msg))
1311 (setq msg (nreverse msg))
1312 (setq subject (pop msg))
1313 (while (and msg (zerop (length (car msg))) (pop msg)))))
1314 (git-setup-log-buffer (get-buffer-create "*git-commit*")
1315 author-name author-email subject date
1316 (mapconcat #'identity msg "\n"))))
1318 (defun git-get-commit-files (commit)
1319 "Retrieve the list of files modified by COMMIT."
1320 (let (files)
1321 (with-temp-buffer
1322 (git-call-process-env t nil "diff-tree" "-r" "-z" "--name-only" "--no-commit-id" commit)
1323 (goto-char (point-min))
1324 (while (re-search-forward "\\([^\0]*\\)\0" nil t 1)
1325 (push (match-string 1) files)))
1326 files))
1328 (defun git-amend-commit ()
1329 "Undo the last commit on HEAD, and set things up to commit an
1330 amended version of it."
1331 (interactive)
1332 (unless git-status (error "Not in git-status buffer."))
1333 (when (git-empty-db-p) (error "No commit to amend."))
1334 (let* ((commit (git-rev-parse "HEAD"))
1335 (files (git-get-commit-files commit)))
1336 (when (git-call-process-display-error "reset" "--soft" "HEAD^")
1337 (git-update-status-files (copy-sequence files) 'uptodate)
1338 (git-mark-files git-status files)
1339 (git-refresh-files)
1340 (git-setup-commit-buffer commit)
1341 (git-commit-file))))
1343 (defun git-find-file ()
1344 "Visit the current file in its own buffer."
1345 (interactive)
1346 (unless git-status (error "Not in git-status buffer."))
1347 (let ((info (ewoc-data (ewoc-locate git-status))))
1348 (unless (git-expand-directory info)
1349 (find-file (git-fileinfo->name info))
1350 (when (eq 'unmerged (git-fileinfo->state info))
1351 (smerge-mode 1)))))
1353 (defun git-find-file-other-window ()
1354 "Visit the current file in its own buffer in another window."
1355 (interactive)
1356 (unless git-status (error "Not in git-status buffer."))
1357 (let ((info (ewoc-data (ewoc-locate git-status))))
1358 (find-file-other-window (git-fileinfo->name info))
1359 (when (eq 'unmerged (git-fileinfo->state info))
1360 (smerge-mode))))
1362 (defun git-find-file-imerge ()
1363 "Visit the current file in interactive merge mode."
1364 (interactive)
1365 (unless git-status (error "Not in git-status buffer."))
1366 (let ((info (ewoc-data (ewoc-locate git-status))))
1367 (find-file (git-fileinfo->name info))
1368 (smerge-ediff)))
1370 (defun git-view-file ()
1371 "View the current file in its own buffer."
1372 (interactive)
1373 (unless git-status (error "Not in git-status buffer."))
1374 (let ((info (ewoc-data (ewoc-locate git-status))))
1375 (view-file (git-fileinfo->name info))))
1377 (defun git-refresh-status ()
1378 "Refresh the git status buffer."
1379 (interactive)
1380 (let* ((status git-status)
1381 (pos (ewoc-locate status))
1382 (marked-files (git-get-filenames (ewoc-collect status (lambda (info) (git-fileinfo->marked info)))))
1383 (cur-name (and pos (git-fileinfo->name (ewoc-data pos)))))
1384 (unless status (error "Not in git-status buffer."))
1385 (message "Refreshing git status...")
1386 (git-call-process-env nil nil "update-index" "--refresh")
1387 (git-clear-status status)
1388 (git-update-status-files nil)
1389 ; restore file marks
1390 (when marked-files
1391 (git-status-filenames-map status
1392 (lambda (info)
1393 (setf (git-fileinfo->marked info) t)
1394 (setf (git-fileinfo->needs-refresh info) t))
1395 marked-files)
1396 (git-refresh-files))
1397 ; move point to the current file name if any
1398 (message "Refreshing git status...done")
1399 (let ((node (and cur-name (git-find-status-file status cur-name))))
1400 (when node (ewoc-goto-node status node)))))
1402 (defun git-status-quit ()
1403 "Quit git-status mode."
1404 (interactive)
1405 (bury-buffer))
1407 ;;;; Major Mode
1408 ;;;; ------------------------------------------------------------
1410 (defvar git-status-mode-hook nil
1411 "Run after `git-status-mode' is setup.")
1413 (defvar git-status-mode-map nil
1414 "Keymap for git major mode.")
1416 (defvar git-status nil
1417 "List of all files managed by the git-status mode.")
1419 (unless git-status-mode-map
1420 (let ((map (make-keymap))
1421 (commit-map (make-sparse-keymap))
1422 (diff-map (make-sparse-keymap))
1423 (toggle-map (make-sparse-keymap)))
1424 (suppress-keymap map)
1425 (define-key map "?" 'git-help)
1426 (define-key map "h" 'git-help)
1427 (define-key map " " 'git-next-file)
1428 (define-key map "a" 'git-add-file)
1429 (define-key map "c" 'git-commit-file)
1430 (define-key map "\C-c" commit-map)
1431 (define-key map "d" diff-map)
1432 (define-key map "=" 'git-diff-file)
1433 (define-key map "f" 'git-find-file)
1434 (define-key map "\r" 'git-find-file)
1435 (define-key map "g" 'git-refresh-status)
1436 (define-key map "i" 'git-ignore-file)
1437 (define-key map "l" 'git-log-file)
1438 (define-key map "m" 'git-mark-file)
1439 (define-key map "M" 'git-mark-all)
1440 (define-key map "n" 'git-next-file)
1441 (define-key map "N" 'git-next-unmerged-file)
1442 (define-key map "o" 'git-find-file-other-window)
1443 (define-key map "p" 'git-prev-file)
1444 (define-key map "P" 'git-prev-unmerged-file)
1445 (define-key map "q" 'git-status-quit)
1446 (define-key map "r" 'git-remove-file)
1447 (define-key map "R" 'git-resolve-file)
1448 (define-key map "t" toggle-map)
1449 (define-key map "T" 'git-toggle-all-marks)
1450 (define-key map "u" 'git-unmark-file)
1451 (define-key map "U" 'git-revert-file)
1452 (define-key map "v" 'git-view-file)
1453 (define-key map "x" 'git-remove-handled)
1454 (define-key map "\C-?" 'git-unmark-file-up)
1455 (define-key map "\M-\C-?" 'git-unmark-all)
1456 ; the commit submap
1457 (define-key commit-map "\C-a" 'git-amend-commit)
1458 ; the diff submap
1459 (define-key diff-map "b" 'git-diff-file-base)
1460 (define-key diff-map "c" 'git-diff-file-combined)
1461 (define-key diff-map "=" 'git-diff-file)
1462 (define-key diff-map "e" 'git-diff-file-idiff)
1463 (define-key diff-map "E" 'git-find-file-imerge)
1464 (define-key diff-map "h" 'git-diff-file-merge-head)
1465 (define-key diff-map "m" 'git-diff-file-mine)
1466 (define-key diff-map "o" 'git-diff-file-other)
1467 ; the toggle submap
1468 (define-key toggle-map "u" 'git-toggle-show-uptodate)
1469 (define-key toggle-map "i" 'git-toggle-show-ignored)
1470 (define-key toggle-map "k" 'git-toggle-show-unknown)
1471 (define-key toggle-map "m" 'git-toggle-all-marks)
1472 (setq git-status-mode-map map))
1473 (easy-menu-define git-menu git-status-mode-map
1474 "Git Menu"
1475 `("Git"
1476 ["Refresh" git-refresh-status t]
1477 ["Commit" git-commit-file t]
1478 ("Merge"
1479 ["Next Unmerged File" git-next-unmerged-file t]
1480 ["Prev Unmerged File" git-prev-unmerged-file t]
1481 ["Mark as Resolved" git-resolve-file t]
1482 ["Interactive Merge File" git-find-file-imerge t]
1483 ["Diff Against Common Base File" git-diff-file-base t]
1484 ["Diff Combined" git-diff-file-combined t]
1485 ["Diff Against Merge Head" git-diff-file-merge-head t]
1486 ["Diff Against Mine" git-diff-file-mine t]
1487 ["Diff Against Other" git-diff-file-other t])
1488 "--------"
1489 ["Add File" git-add-file t]
1490 ["Revert File" git-revert-file t]
1491 ["Ignore File" git-ignore-file t]
1492 ["Remove File" git-remove-file t]
1493 "--------"
1494 ["Find File" git-find-file t]
1495 ["View File" git-view-file t]
1496 ["Diff File" git-diff-file t]
1497 ["Interactive Diff File" git-diff-file-idiff t]
1498 ["Log" git-log-file t]
1499 "--------"
1500 ["Mark" git-mark-file t]
1501 ["Mark All" git-mark-all t]
1502 ["Unmark" git-unmark-file t]
1503 ["Unmark All" git-unmark-all t]
1504 ["Toggle All Marks" git-toggle-all-marks t]
1505 ["Hide Handled Files" git-remove-handled t]
1506 "--------"
1507 ["Show Uptodate Files" git-toggle-show-uptodate :style toggle :selected git-show-uptodate]
1508 ["Show Ignored Files" git-toggle-show-ignored :style toggle :selected git-show-ignored]
1509 ["Show Unknown Files" git-toggle-show-unknown :style toggle :selected git-show-unknown]
1510 "--------"
1511 ["Quit" git-status-quit t])))
1514 ;; git mode should only run in the *git status* buffer
1515 (put 'git-status-mode 'mode-class 'special)
1517 (defun git-status-mode ()
1518 "Major mode for interacting with Git.
1519 Commands:
1520 \\{git-status-mode-map}"
1521 (kill-all-local-variables)
1522 (buffer-disable-undo)
1523 (setq mode-name "git status"
1524 major-mode 'git-status-mode
1525 goal-column 17
1526 buffer-read-only t)
1527 (use-local-map git-status-mode-map)
1528 (let ((buffer-read-only nil))
1529 (erase-buffer)
1530 (let ((status (ewoc-create 'git-fileinfo-prettyprint "" "")))
1531 (set (make-local-variable 'git-status) status))
1532 (set (make-local-variable 'list-buffers-directory) default-directory)
1533 (make-local-variable 'git-show-uptodate)
1534 (make-local-variable 'git-show-ignored)
1535 (make-local-variable 'git-show-unknown)
1536 (run-hooks 'git-status-mode-hook)))
1538 (defun git-find-status-buffer (dir)
1539 "Find the git status buffer handling a specified directory."
1540 (let ((list (buffer-list))
1541 (fulldir (expand-file-name dir))
1542 found)
1543 (while (and list (not found))
1544 (let ((buffer (car list)))
1545 (with-current-buffer buffer
1546 (when (and list-buffers-directory
1547 (string-equal fulldir (expand-file-name list-buffers-directory))
1548 (eq major-mode 'git-status-mode))
1549 (setq found buffer))))
1550 (setq list (cdr list)))
1551 found))
1553 (defun git-status (dir)
1554 "Entry point into git-status mode."
1555 (interactive "DSelect directory: ")
1556 (setq dir (git-get-top-dir dir))
1557 (if (file-directory-p (concat (file-name-as-directory dir) ".git"))
1558 (let ((buffer (or (and git-reuse-status-buffer (git-find-status-buffer dir))
1559 (create-file-buffer (expand-file-name "*git-status*" dir)))))
1560 (switch-to-buffer buffer)
1561 (cd dir)
1562 (git-status-mode)
1563 (git-refresh-status)
1564 (goto-char (point-min))
1565 (add-hook 'after-save-hook 'git-update-saved-file))
1566 (message "%s is not a git working tree." dir)))
1568 (defun git-update-saved-file ()
1569 "Update the corresponding git-status buffer when a file is saved.
1570 Meant to be used in `after-save-hook'."
1571 (let* ((file (expand-file-name buffer-file-name))
1572 (dir (condition-case nil (git-get-top-dir (file-name-directory file)) (error nil)))
1573 (buffer (and dir (git-find-status-buffer dir))))
1574 (when buffer
1575 (with-current-buffer buffer
1576 (let ((filename (file-relative-name file dir)))
1577 ; skip files located inside the .git directory
1578 (unless (string-match "^\\.git/" filename)
1579 (git-call-process-env nil nil "add" "--refresh" "--" filename)
1580 (git-update-status-files (list filename) 'uptodate)))))))
1582 (defun git-help ()
1583 "Display help for Git mode."
1584 (interactive)
1585 (describe-function 'git-status-mode))
1587 (provide 'git)
1588 ;;; git.el ends here