1 ;;; git.el --- A user interface for git
3 ;; Copyright (C) 2005, 2006 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 ;; - better handling of subprocess errors
39 ;; - hook into file save (after-save-hook)
40 ;; - diff against other branch
41 ;; - renaming files from the status buffer
42 ;; - support for appending signed-off-by
45 ;; - switching branches
47 ;; - git-show-branch browser
48 ;; - customize support
52 (eval-when-compile (require 'cl))
57 ;;;; ------------------------------------------------------------
59 (defface git-status-face
60 '((((class color) (background light)) (:foreground "purple")))
61 "Git mode face used to highlight added and modified files.")
63 (defface git-unmerged-face
64 '((((class color) (background light)) (:foreground "red" :bold t)))
65 "Git mode face used to highlight unmerged files.")
67 (defface git-unknown-face
68 '((((class color) (background light)) (:foreground "goldenrod" :bold t)))
69 "Git mode face used to highlight unknown files.")
71 (defface git-uptodate-face
72 '((((class color) (background light)) (:foreground "grey60")))
73 "Git mode face used to highlight up-to-date files.")
75 (defface git-ignored-face
76 '((((class color) (background light)) (:foreground "grey60")))
77 "Git mode face used to highlight ignored files.")
79 (defface git-mark-face
80 '((((class color) (background light)) (:foreground "red" :bold t)))
81 "Git mode face used for the file marks.")
83 (defface git-header-face
84 '((((class color) (background light)) (:foreground "blue")))
85 "Git mode face used for commit headers.")
87 (defface git-separator-face
88 '((((class color) (background light)) (:foreground "brown")))
89 "Git mode face used for commit separator.")
91 (defface git-permission-face
92 '((((class color) (background light)) (:foreground "green" :bold t)))
93 "Git mode face used for permission changes.")
95 (defvar git-committer-name nil
96 "*User name to use for commits.
97 If not set, fall back to `add-log-full-name' and then `user-full-name'.")
99 (defvar git-committer-email nil
100 "*Email address to use for commits.
101 If not set, fall back to `add-log-mailing-address' and then `user-mail-address'.")
103 (defvar git-commits-coding-system 'utf-8
104 "Default coding system for git commits.")
106 (defconst git-log-msg-separator "--- log message follows this line ---")
108 (defconst git-per-dir-ignore-file ".gitignore"
109 "Name of the per-directory ignore file.")
113 ;;;; ------------------------------------------------------------
115 (defun git-get-env-strings (env)
116 "Build a list of NAME=VALUE strings from a list of environment strings."
117 (mapcar (lambda (entry) (concat (car entry) "=" (cdr entry))) env))
119 (defun git-call-process-env (buffer env &rest args)
120 "Wrapper for call-process that sets environment strings."
122 (apply #'call-process "env" nil buffer nil
123 (append (git-get-env-strings env) (list "git") args))
124 (apply #'call-process "git" nil buffer nil args)))
126 (defun git-run-process-region (buffer start end program args)
127 "Run a git process with a buffer region as input."
128 (let ((output-buffer (current-buffer))
129 (dir default-directory))
130 (with-current-buffer buffer
132 (apply #'call-process-region start end program
133 nil (list output-buffer nil) nil args))))
135 (defun git-run-command-buffer (buffer-name &rest args)
136 "Run a git command, sending the output to a buffer named BUFFER-NAME."
137 (let ((dir default-directory)
138 (buffer (get-buffer-create buffer-name)))
139 (message "Running git %s..." (car args))
140 (with-current-buffer buffer
141 (let ((default-directory dir)
142 (buffer-read-only nil))
144 (apply #'git-call-process-env buffer nil args)))
145 (message "Running git %s...done" (car args))
148 (defun git-run-command (buffer env &rest args)
149 (message "Running git %s..." (car args))
150 (apply #'git-call-process-env buffer env args)
151 (message "Running git %s...done" (car args)))
153 (defun git-run-command-region (buffer start end env &rest args)
154 "Run a git command with specified buffer region as input."
155 (message "Running git %s..." (car args))
156 (unless (eq 0 (if env
157 (git-run-process-region
158 buffer start end "env"
159 (append (git-get-env-strings env) (list "git") args))
160 (git-run-process-region
161 buffer start end "git" args)))
162 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string)))
163 (message "Running git %s...done" (car args)))
165 (defun git-get-string-sha1 (string)
166 "Read a SHA1 from the specified string."
167 (let ((pos (string-match "[0-9a-f]\\{40\\}" string)))
168 (and pos (substring string pos (match-end 0)))))
170 (defun git-get-committer-name ()
171 "Return the name to use as GIT_COMMITTER_NAME."
172 ; copied from log-edit
173 (or git-committer-name
174 (and (boundp 'add-log-full-name) add-log-full-name)
175 (and (fboundp 'user-full-name) (user-full-name))
176 (and (boundp 'user-full-name) user-full-name)))
178 (defun git-get-committer-email ()
179 "Return the email address to use as GIT_COMMITTER_EMAIL."
180 ; copied from log-edit
181 (or git-committer-email
182 (and (boundp 'add-log-mailing-address) add-log-mailing-address)
183 (and (fboundp 'user-mail-address) (user-mail-address))
184 (and (boundp 'user-mail-address) user-mail-address)))
186 (defun git-escape-file-name (name)
187 "Escape a file name if necessary."
188 (if (string-match "[\n\t\"\\]" name)
190 (mapconcat (lambda (c)
196 (t (char-to-string c))))
201 (defun git-get-top-dir (dir)
202 "Retrieve the top-level directory of a git tree."
203 (let ((cdup (with-output-to-string
204 (with-current-buffer standard-output
206 (unless (eq 0 (call-process "git" nil t nil "rev-parse" "--show-cdup"))
207 (error "cannot find top-level git tree for %s." dir))))))
208 (expand-file-name (concat (file-name-as-directory dir)
209 (car (split-string cdup "\n"))))))
212 (defun git-append-to-ignore (file)
213 "Add a file name to the ignore file in its directory."
214 (let* ((fullname (expand-file-name file))
215 (dir (file-name-directory fullname))
216 (name (file-name-nondirectory fullname)))
217 (save-window-excursion
218 (set-buffer (find-file-noselect (expand-file-name git-per-dir-ignore-file dir)))
219 (goto-char (point-max))
220 (unless (zerop (current-column)) (insert "\n"))
222 (sort-lines nil (point-min) (point-max))
226 ;;;; Wrappers for basic git commands
227 ;;;; ------------------------------------------------------------
229 (defun git-rev-parse (rev)
230 "Parse a revision name and return its SHA1."
232 (with-output-to-string
233 (with-current-buffer standard-output
234 (git-call-process-env t nil "rev-parse" rev)))))
236 (defun git-symbolic-ref (ref)
237 "Wrapper for the git-symbolic-ref command."
240 (with-output-to-string
241 (with-current-buffer standard-output
242 (git-call-process-env t nil "symbolic-ref" ref)))
245 (defun git-update-ref (ref val &optional oldval)
246 "Update a reference by calling git-update-ref."
247 (apply #'git-call-process-env nil nil "update-ref" ref val (if oldval (list oldval))))
249 (defun git-read-tree (tree &optional index-file)
250 "Read a tree into the index file."
251 (apply #'git-call-process-env nil
252 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
253 "read-tree" (if tree (list tree))))
255 (defun git-write-tree (&optional index-file)
256 "Call git-write-tree and return the resulting tree SHA1 as a string."
258 (with-output-to-string
259 (with-current-buffer standard-output
260 (git-call-process-env t
261 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
264 (defun git-commit-tree (buffer tree head)
265 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
266 (let ((author-name (git-get-committer-name))
267 (author-email (git-get-committer-email))
268 author-date log-start log-end args)
272 (with-current-buffer buffer
273 (goto-char (point-min))
275 (setq log-start (re-search-forward (concat "^" git-log-msg-separator "\n") nil t))
277 (narrow-to-region (point-min) log-start)
278 (goto-char (point-min))
279 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t)
280 (setq author-name (match-string 1)
281 author-email (match-string 2)))
282 (goto-char (point-min))
283 (when (re-search-forward "^Date: +\\(.*\\)$" nil t)
284 (setq author-date (match-string 1)))
285 (goto-char (point-min))
286 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t)
287 (unless (string-equal head (match-string 1))
289 (push (match-string 1) args))))
290 (setq log-start (point-min)))
291 (setq log-end (point-max)))
293 (with-output-to-string
294 (with-current-buffer standard-output
295 (let ((coding-system-for-write git-commits-coding-system)
296 (env `(("GIT_AUTHOR_NAME" . ,author-name)
297 ("GIT_AUTHOR_EMAIL" . ,author-email)
298 ("GIT_COMMITTER_NAME" . ,(git-get-committer-name))
299 ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email)))))
300 (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env))
301 (apply #'git-run-command-region
302 buffer log-start log-end env
303 "commit-tree" tree (nreverse args))))))))
305 (defun git-empty-db-p ()
306 "Check if the git db is empty (no commit done yet)."
307 (not (eq 0 (call-process "git" nil nil nil "rev-parse" "--verify" "HEAD"))))
309 (defun git-get-merge-heads ()
310 "Retrieve the merge heads from the MERGE_HEAD file if present."
312 (when (file-readable-p ".git/MERGE_HEAD")
314 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t)
315 (goto-char (point-min))
316 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t)
317 (push (match-string 0) heads))))
320 ;;;; File info structure
321 ;;;; ------------------------------------------------------------
323 ; fileinfo structure stolen from pcl-cvs
324 (defstruct (git-fileinfo
326 (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked))
327 (:conc-name git-fileinfo->))
329 state ;; current state
331 old-perm new-perm ;; permission flags
332 rename-state ;; rename or copy state
333 orig-name ;; original name for renames or copies
334 needs-refresh) ;; whether file needs to be refreshed
336 (defvar git-status nil)
338 (defun git-clear-status (status)
339 "Remove everything from the status list."
340 (ewoc-filter status (lambda (info) nil)))
342 (defun git-set-files-state (files state)
343 "Set the state of a list of files."
345 (unless (eq (git-fileinfo->state info) state)
346 (setf (git-fileinfo->state info) state)
347 (setf (git-fileinfo->rename-state info) nil)
348 (setf (git-fileinfo->orig-name info) nil)
349 (setf (git-fileinfo->needs-refresh info) t))))
351 (defun git-state-code (code)
352 "Convert from a string to a added/deleted/modified state."
353 (case (string-to-char code)
361 (defun git-status-code-as-string (code)
362 "Format a git status code as string."
364 ('modified (propertize "Modified" 'face 'git-status-face))
365 ('unknown (propertize "Unknown " 'face 'git-unknown-face))
366 ('added (propertize "Added " 'face 'git-status-face))
367 ('deleted (propertize "Deleted " 'face 'git-status-face))
368 ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face))
369 ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face))
370 ('ignored (propertize "Ignored " 'face 'git-ignored-face))
373 (defun git-rename-as-string (info)
374 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
375 (let ((state (git-fileinfo->rename-state info)))
379 (if (eq state 'copy) "copied from "
380 (if (eq (git-fileinfo->state info) 'added) "renamed to "
382 (git-escape-file-name (git-fileinfo->orig-name info))
383 ")") 'face 'git-status-face)
386 (defun git-permissions-as-string (old-perm new-perm)
387 "Format a permission change as string."
389 (if (or (not old-perm)
391 (eq 0 (logand #O111 (logxor old-perm new-perm))))
393 (if (eq 0 (logand #O111 old-perm)) "+x" "-x"))
394 'face 'git-permission-face))
396 (defun git-fileinfo-prettyprint (info)
397 "Pretty-printer for the git-fileinfo structure."
398 (insert (format " %s %s %s %s%s"
399 (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ")
400 (git-status-code-as-string (git-fileinfo->state info))
401 (git-permissions-as-string (git-fileinfo->old-perm info) (git-fileinfo->new-perm info))
402 (git-escape-file-name (git-fileinfo->name info))
403 (git-rename-as-string info))))
405 (defun git-parse-status (status)
406 "Parse the output of git-diff-index in the current buffer."
407 (goto-char (point-min))
408 (while (re-search-forward
409 ":\\([0-7]\\{6\\}\\) \\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\(\\([ADMU]\\)\0\\([^\0]+\\)\\|\\([CR]\\)[0-9]*\0\\([^\0]+\\)\0\\([^\0]+\\)\\)\0"
411 (let ((old-perm (string-to-number (match-string 1) 8))
412 (new-perm (string-to-number (match-string 2) 8))
413 (state (or (match-string 4) (match-string 6)))
414 (name (or (match-string 5) (match-string 7)))
415 (new-name (match-string 8)))
416 (if new-name ; copy or rename
417 (if (eq ?C (string-to-char state))
418 (ewoc-enter-last status (git-create-fileinfo 'added new-name old-perm new-perm 'copy name))
419 (ewoc-enter-last status (git-create-fileinfo 'deleted name 0 0 'rename new-name))
420 (ewoc-enter-last status (git-create-fileinfo 'added new-name old-perm new-perm 'rename name)))
421 (ewoc-enter-last status (git-create-fileinfo (git-state-code state) name old-perm new-perm))))))
423 (defun git-find-status-file (status file)
424 "Find a given file in the status ewoc and return its node."
425 (let ((node (ewoc-nth status 0)))
426 (while (and node (not (string= file (git-fileinfo->name (ewoc-data node)))))
427 (setq node (ewoc-next status node)))
430 (defun git-parse-ls-files (status default-state &optional skip-existing)
431 "Parse the output of git-ls-files in the current buffer."
432 (goto-char (point-min))
434 (while (re-search-forward "\\([HMRCK?]\\) \\([^\0]*\\)\0" nil t 1)
435 (let ((state (match-string 1))
436 (name (match-string 2)))
437 (unless (and skip-existing (git-find-status-file status name))
438 (push (git-create-fileinfo (or (git-state-code state) default-state) name) infolist))))
439 (dolist (info (nreverse infolist))
440 (ewoc-enter-last status info))))
442 (defun git-parse-ls-unmerged (status)
443 "Parse the output of git-ls-files -u in the current buffer."
444 (goto-char (point-min))
446 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t)
447 (let ((node (git-find-status-file status (match-string 1))))
448 (when node (push (ewoc-data node) files))))
449 (git-set-files-state files 'unmerged)))
451 (defun git-add-status-file (state name)
452 "Add a new file to the status list (if not existing already) and return its node."
453 (unless git-status (error "Not in git-status buffer."))
454 (or (git-find-status-file git-status name)
455 (ewoc-enter-last git-status (git-create-fileinfo state name))))
457 (defun git-marked-files ()
458 "Return a list of all marked files, or if none a list containing just the file at cursor position."
459 (unless git-status (error "Not in git-status buffer."))
460 (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info)))
461 (list (ewoc-data (ewoc-locate git-status)))))
463 (defun git-marked-files-state (&rest states)
464 "Return marked files that are in the specified states."
465 (let ((files (git-marked-files))
468 (when (memq (git-fileinfo->state info) states)
472 (defun git-refresh-files ()
473 "Refresh all files that need it and clear the needs-refresh flag."
474 (unless git-status (error "Not in git-status buffer."))
477 (let ((refresh (git-fileinfo->needs-refresh info)))
478 (setf (git-fileinfo->needs-refresh info) nil)
481 ; move back to goal column
482 (when goal-column (move-to-column goal-column)))
484 (defun git-refresh-ewoc-hf (status)
485 "Refresh the ewoc header and footer."
486 (let ((branch (git-symbolic-ref "HEAD"))
487 (head (if (git-empty-db-p) "Nothing committed yet"
488 (substring (git-rev-parse "HEAD") 0 10)))
489 (merge-heads (git-get-merge-heads)))
491 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
493 (if (string-match "^refs/heads/" branch)
494 (substring branch (match-end 0))
498 (concat "\nMerging: "
499 (mapconcat (lambda (str) (substring str 0 10)) merge-heads " "))
501 (if (ewoc-nth status 0) "" " No changes."))))
503 (defun git-get-filenames (files)
504 (mapcar (lambda (info) (git-fileinfo->name info)) files))
506 (defun git-update-index (index-file files)
507 "Run git-update-index on a list of files."
508 (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file))))
509 added deleted modified)
511 (case (git-fileinfo->state info)
512 ('added (push info added))
513 ('deleted (push info deleted))
514 ('modified (push info modified))))
516 (apply #'git-run-command nil env "update-index" "--add" "--" (git-get-filenames added)))
518 (apply #'git-run-command nil env "update-index" "--remove" "--" (git-get-filenames deleted)))
520 (apply #'git-run-command nil env "update-index" "--" (git-get-filenames modified)))))
522 (defun git-do-commit ()
523 "Perform the actual commit using the current buffer as log message."
525 (let ((buffer (current-buffer))
526 (index-file (make-temp-file "gitidx")))
527 (with-current-buffer log-edit-parent-buffer
528 (if (git-marked-files-state 'unmerged)
529 (message "You cannot commit unmerged files, resolve them first.")
531 (let ((files (git-marked-files-state 'added 'deleted 'modified))
533 (unless (git-empty-db-p)
534 (setq head (git-rev-parse "HEAD")
535 head-tree (git-rev-parse "HEAD^{tree}")))
538 (git-read-tree head-tree index-file)
539 (git-update-index nil files) ;update both the default index
540 (git-update-index index-file files) ;and the temporary one
541 (let ((tree (git-write-tree index-file)))
542 (if (or (not (string-equal tree head-tree))
543 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
544 (let ((commit (git-commit-tree buffer tree head)))
545 (git-update-ref "HEAD" commit head)
546 (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil))
547 (with-current-buffer buffer (erase-buffer))
548 (git-set-files-state files 'uptodate)
550 (git-refresh-ewoc-hf git-status)
551 (message "Committed %s." commit))
552 (message "Commit aborted."))))
553 (message "No files to commit.")))
554 (delete-file index-file))))))
557 ;;;; Interactive functions
558 ;;;; ------------------------------------------------------------
560 (defun git-mark-file ()
561 "Mark the file that the cursor is on and move to the next one."
563 (unless git-status (error "Not in git-status buffer."))
564 (let* ((pos (ewoc-locate git-status))
565 (info (ewoc-data pos)))
566 (setf (git-fileinfo->marked info) t)
567 (ewoc-invalidate git-status pos)
568 (ewoc-goto-next git-status 1)))
570 (defun git-unmark-file ()
571 "Unmark the file that the cursor is on and move to the next one."
573 (unless git-status (error "Not in git-status buffer."))
574 (let* ((pos (ewoc-locate git-status))
575 (info (ewoc-data pos)))
576 (setf (git-fileinfo->marked info) nil)
577 (ewoc-invalidate git-status pos)
578 (ewoc-goto-next git-status 1)))
580 (defun git-unmark-file-up ()
581 "Unmark the file that the cursor is on and move to the previous one."
583 (unless git-status (error "Not in git-status buffer."))
584 (let* ((pos (ewoc-locate git-status))
585 (info (ewoc-data pos)))
586 (setf (git-fileinfo->marked info) nil)
587 (ewoc-invalidate git-status pos)
588 (ewoc-goto-prev git-status 1)))
590 (defun git-mark-all ()
593 (unless git-status (error "Not in git-status buffer."))
594 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) t) t) git-status)
595 ; move back to goal column after invalidate
596 (when goal-column (move-to-column goal-column)))
598 (defun git-unmark-all ()
601 (unless git-status (error "Not in git-status buffer."))
602 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) nil) t) git-status)
603 ; move back to goal column after invalidate
604 (when goal-column (move-to-column goal-column)))
606 (defun git-toggle-all-marks ()
607 "Toggle all file marks."
609 (unless git-status (error "Not in git-status buffer."))
610 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status)
611 ; move back to goal column after invalidate
612 (when goal-column (move-to-column goal-column)))
614 (defun git-next-file (&optional n)
615 "Move the selection down N files."
617 (unless git-status (error "Not in git-status buffer."))
618 (ewoc-goto-next git-status n))
620 (defun git-prev-file (&optional n)
621 "Move the selection up N files."
623 (unless git-status (error "Not in git-status buffer."))
624 (ewoc-goto-prev git-status n))
626 (defun git-add-file ()
627 "Add marked file(s) to the index cache."
629 (let ((files (git-marked-files-state 'unknown)))
632 (git-add-status-file 'added (file-relative-name
633 (read-file-name "File to add: " nil nil t))))
635 (apply #'git-run-command nil nil "update-index" "--info-only" "--add" "--" (git-get-filenames files))
636 (git-set-files-state files 'added)
637 (git-refresh-files)))
639 (defun git-ignore-file ()
640 "Add marked file(s) to the ignore list."
642 (let ((files (git-marked-files-state 'unknown)))
645 (git-add-status-file 'unknown (file-relative-name
646 (read-file-name "File to ignore: " nil nil t))))
648 (dolist (info files) (git-append-to-ignore (git-fileinfo->name info)))
649 (git-set-files-state files 'ignored)
650 (git-refresh-files)))
652 (defun git-remove-file ()
653 "Remove the marked file(s)."
655 (let ((files (git-marked-files-state 'added 'modified 'unknown 'uptodate)))
658 (git-add-status-file 'unknown (file-relative-name
659 (read-file-name "File to remove: " nil nil t))))
662 (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" "")))
665 (let ((name (git-fileinfo->name info)))
666 (when (file-exists-p name) (delete-file name))))
667 (apply #'git-run-command nil nil "update-index" "--info-only" "--remove" "--" (git-get-filenames files))
668 ; remove unknown files from the list, set the others to deleted
669 (ewoc-filter git-status
671 (not (and (memq info files) (eq (git-fileinfo->state info) 'unknown))))
673 (git-set-files-state files 'deleted)
675 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
676 (git-refresh-ewoc-hf git-status)))
677 (message "Aborting"))))
679 (defun git-revert-file ()
680 "Revert changes to the marked file(s)."
682 (let ((files (git-marked-files))
686 (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" ""))))
688 (case (git-fileinfo->state info)
689 ('added (push info added))
690 ('deleted (push info modified))
691 ('unmerged (push info modified))
692 ('modified (push info modified))))
694 (apply #'git-run-command nil nil "update-index" "--force-remove" "--" (git-get-filenames added))
695 (git-set-files-state added 'unknown))
697 (apply #'git-run-command nil nil "checkout" "HEAD" (git-get-filenames modified))
698 (git-set-files-state modified 'uptodate))
699 (git-refresh-files))))
701 (defun git-resolve-file ()
702 "Resolve conflicts in marked file(s)."
704 (let ((files (git-marked-files-state 'unmerged)))
706 (apply #'git-run-command nil nil "update-index" "--info-only" "--" (git-get-filenames files))
707 (git-set-files-state files 'modified)
708 (git-refresh-files))))
710 (defun git-remove-handled ()
711 "Remove handled files from the status list."
713 (ewoc-filter git-status
715 (not (or (eq (git-fileinfo->state info) 'ignored)
716 (eq (git-fileinfo->state info) 'uptodate)))))
717 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
718 (git-refresh-ewoc-hf git-status)))
720 (defun git-setup-diff-buffer (buffer)
721 "Setup a buffer for displaying a diff."
722 (with-current-buffer buffer
724 (goto-char (point-min))
725 (setq buffer-read-only t))
726 (display-buffer buffer)
727 (shrink-window-if-larger-than-buffer))
729 (defun git-diff-file ()
730 "Diff the marked file(s) against HEAD."
732 (let ((files (git-marked-files)))
733 (git-setup-diff-buffer
734 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files)))))
736 (defun git-diff-unmerged-file (stage)
737 "Diff the marked unmerged file(s) against the specified stage."
738 (let ((files (git-marked-files)))
739 (git-setup-diff-buffer
740 (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files)))))
742 (defun git-diff-file-base ()
743 "Diff the marked unmerged file(s) against the common base file."
745 (git-diff-unmerged-file "-1"))
747 (defun git-diff-file-mine ()
748 "Diff the marked unmerged file(s) against my pre-merge version."
750 (git-diff-unmerged-file "-2"))
752 (defun git-diff-file-other ()
753 "Diff the marked unmerged file(s) against the other's pre-merge version."
755 (git-diff-unmerged-file "-3"))
757 (defun git-diff-file-combined ()
758 "Do a combined diff of the marked unmerged file(s)."
760 (git-diff-unmerged-file "-c"))
762 (defun git-diff-file-idiff ()
763 "Perform an interactive diff on the current file."
765 (error "Interactive diffs not implemented yet."))
767 (defun git-log-file ()
768 "Display a log of changes to the marked file(s)."
770 (let* ((files (git-marked-files))
771 (coding-system-for-read git-commits-coding-system)
772 (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files))))
773 (with-current-buffer buffer
774 ; (git-log-mode) FIXME: implement log mode
775 (goto-char (point-min))
776 (setq buffer-read-only t))
777 (display-buffer buffer)))
779 (defun git-log-edit-files ()
780 "Return a list of marked files for use in the log-edit buffer."
781 (with-current-buffer log-edit-parent-buffer
782 (git-get-filenames (git-marked-files-state 'added 'deleted 'modified))))
784 (defun git-commit-file ()
785 "Commit the marked file(s), asking for a commit message."
787 (unless git-status (error "Not in git-status buffer."))
788 (let ((buffer (get-buffer-create "*git-commit*"))
789 (merge-heads (git-get-merge-heads))
790 (dir default-directory))
791 (with-current-buffer buffer
792 (when (eq 0 (buffer-size))
797 (format "Author: %s <%s>\n%s"
798 (git-get-committer-name) (git-get-committer-email)
800 (format "Parent: %s\n%s\n"
801 (git-rev-parse "HEAD")
802 (mapconcat (lambda (str) (concat "Parent: " str)) merge-heads "\n"))
804 'face 'git-header-face)
805 (propertize git-log-msg-separator 'face 'git-separator-face)
807 (when (and merge-heads (file-readable-p ".git/MERGE_MSG"))
808 (insert-file-contents ".git/MERGE_MSG"))))
809 (log-edit #'git-do-commit nil #'git-log-edit-files buffer)))
811 (defun git-find-file ()
812 "Visit the current file in its own buffer."
814 (unless git-status (error "Not in git-status buffer."))
815 (let ((info (ewoc-data (ewoc-locate git-status))))
816 (find-file (git-fileinfo->name info))
817 (when (eq 'unmerged (git-fileinfo->state info))
820 (defun git-find-file-imerge ()
821 "Visit the current file in interactive merge mode."
823 (unless git-status (error "Not in git-status buffer."))
824 (let ((info (ewoc-data (ewoc-locate git-status))))
825 (find-file (git-fileinfo->name info))
828 (defun git-view-file ()
829 "View the current file in its own buffer."
831 (unless git-status (error "Not in git-status buffer."))
832 (let ((info (ewoc-data (ewoc-locate git-status))))
833 (view-file (git-fileinfo->name info))))
835 (defun git-refresh-status ()
836 "Refresh the git status buffer."
838 (let* ((status git-status)
839 (pos (ewoc-locate status))
840 (cur-name (and pos (git-fileinfo->name (ewoc-data pos)))))
841 (unless status (error "Not in git-status buffer."))
842 (git-clear-status status)
843 (git-run-command nil nil "update-index" "--info-only" "--refresh")
845 ; we need some special handling for an empty db
847 (git-run-command t nil "ls-files" "-z" "-t" "-c")
848 (git-parse-ls-files status 'added))
850 (git-run-command t nil "diff-index" "-z" "-M" "HEAD")
851 (git-parse-status status)))
853 (git-run-command t nil "ls-files" "-z" "-u")
854 (git-parse-ls-unmerged status))
855 (when (file-readable-p ".git/info/exclude")
857 (git-run-command t nil "ls-files" "-z" "-t" "-o"
858 "--exclude-from=.git/info/exclude"
859 (concat "--exclude-per-directory=" git-per-dir-ignore-file))
860 (git-parse-ls-files status 'unknown)))
862 (git-refresh-ewoc-hf status)
863 ; move point to the current file name if any
864 (let ((node (and cur-name (git-find-status-file status cur-name))))
865 (when node (ewoc-goto-node status node)))))
867 (defun git-status-quit ()
868 "Quit git-status mode."
873 ;;;; ------------------------------------------------------------
875 (defvar git-status-mode-hook nil
876 "Run after `git-status-mode' is setup.")
878 (defvar git-status-mode-map nil
879 "Keymap for git major mode.")
881 (defvar git-status nil
882 "List of all files managed by the git-status mode.")
884 (unless git-status-mode-map
885 (let ((map (make-keymap))
886 (diff-map (make-sparse-keymap)))
887 (suppress-keymap map)
888 (define-key map " " 'git-next-file)
889 (define-key map "a" 'git-add-file)
890 (define-key map "c" 'git-commit-file)
891 (define-key map "d" diff-map)
892 (define-key map "=" 'git-diff-file)
893 (define-key map "f" 'git-find-file)
894 (define-key map [RET] 'git-find-file)
895 (define-key map "g" 'git-refresh-status)
896 (define-key map "i" 'git-ignore-file)
897 (define-key map "l" 'git-log-file)
898 (define-key map "m" 'git-mark-file)
899 (define-key map "M" 'git-mark-all)
900 (define-key map "n" 'git-next-file)
901 (define-key map "p" 'git-prev-file)
902 (define-key map "q" 'git-status-quit)
903 (define-key map "r" 'git-remove-file)
904 (define-key map "R" 'git-resolve-file)
905 (define-key map "T" 'git-toggle-all-marks)
906 (define-key map "u" 'git-unmark-file)
907 (define-key map "U" 'git-revert-file)
908 (define-key map "v" 'git-view-file)
909 (define-key map "x" 'git-remove-handled)
910 (define-key map "\C-?" 'git-unmark-file-up)
911 (define-key map "\M-\C-?" 'git-unmark-all)
913 (define-key diff-map "b" 'git-diff-file-base)
914 (define-key diff-map "c" 'git-diff-file-combined)
915 (define-key diff-map "=" 'git-diff-file)
916 (define-key diff-map "e" 'git-diff-file-idiff)
917 (define-key diff-map "E" 'git-find-file-imerge)
918 (define-key diff-map "m" 'git-diff-file-mine)
919 (define-key diff-map "o" 'git-diff-file-other)
920 (setq git-status-mode-map map)))
922 ;; git mode should only run in the *git status* buffer
923 (put 'git-status-mode 'mode-class 'special)
925 (defun git-status-mode ()
926 "Major mode for interacting with Git.
928 \\{git-status-mode-map}"
929 (kill-all-local-variables)
930 (buffer-disable-undo)
931 (setq mode-name "git status"
932 major-mode 'git-status-mode
935 (use-local-map git-status-mode-map)
936 (let ((buffer-read-only nil))
938 (let ((status (ewoc-create 'git-fileinfo-prettyprint "" "")))
939 (set (make-local-variable 'git-status) status))
940 (run-hooks 'git-status-mode-hook)))
942 (defun git-status (dir)
943 "Entry point into git-status mode."
944 (interactive "DSelect directory: ")
945 (setq dir (git-get-top-dir dir))
946 (if (file-directory-p (concat (file-name-as-directory dir) ".git"))
947 (let ((buffer (create-file-buffer (expand-file-name "*git-status*" dir))))
948 (switch-to-buffer buffer)
952 (goto-char (point-min)))
953 (message "%s is not a git working tree." dir)))