git.el: Get the default user name and email from the repository config.
[git.git] / contrib / emacs / git.el
blob5496a1bb5e121f197a4a4c6c6e0c2a589f864ef3
1 ;;; git.el --- A user interface for git
3 ;; Copyright (C) 2005, 2006 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 ;; - 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 ;; - creating tags
43 ;; - fetch/pull
44 ;; - switching branches
45 ;; - revlist browser
46 ;; - git-show-branch browser
47 ;; - menus
50 (eval-when-compile (require 'cl))
51 (require 'ewoc)
54 ;;;; Customizations
55 ;;;; ------------------------------------------------------------
57 (defgroup git nil
58 "Git user interface")
60 (defcustom git-committer-name nil
61 "User name to use for commits.
62 The default is to fall back to the repository config, then to `add-log-full-name' and then to `user-full-name'."
63 :group 'git
64 :type '(choice (const :tag "Default" nil)
65 (string :tag "Name")))
67 (defcustom git-committer-email nil
68 "Email address to use for commits.
69 The default is to fall back to the git repository config, then to `add-log-mailing-address' and then to `user-mail-address'."
70 :group 'git
71 :type '(choice (const :tag "Default" nil)
72 (string :tag "Email")))
74 (defcustom git-commits-coding-system 'utf-8
75 "Default coding system for the log message of git commits."
76 :group 'git
77 :type 'coding-system)
79 (defcustom git-append-signed-off-by nil
80 "Whether to append a Signed-off-by line to the commit message before editing."
81 :group 'git
82 :type 'boolean)
84 (defcustom git-per-dir-ignore-file ".gitignore"
85 "Name of the per-directory ignore file."
86 :group 'git
87 :type 'string)
89 (defface git-status-face
90 '((((class color) (background light)) (:foreground "purple")))
91 "Git mode face used to highlight added and modified files."
92 :group 'git)
94 (defface git-unmerged-face
95 '((((class color) (background light)) (:foreground "red" :bold t)))
96 "Git mode face used to highlight unmerged files."
97 :group 'git)
99 (defface git-unknown-face
100 '((((class color) (background light)) (:foreground "goldenrod" :bold t)))
101 "Git mode face used to highlight unknown files."
102 :group 'git)
104 (defface git-uptodate-face
105 '((((class color) (background light)) (:foreground "grey60")))
106 "Git mode face used to highlight up-to-date files."
107 :group 'git)
109 (defface git-ignored-face
110 '((((class color) (background light)) (:foreground "grey60")))
111 "Git mode face used to highlight ignored files."
112 :group 'git)
114 (defface git-mark-face
115 '((((class color) (background light)) (:foreground "red" :bold t)))
116 "Git mode face used for the file marks."
117 :group 'git)
119 (defface git-header-face
120 '((((class color) (background light)) (:foreground "blue")))
121 "Git mode face used for commit headers."
122 :group 'git)
124 (defface git-separator-face
125 '((((class color) (background light)) (:foreground "brown")))
126 "Git mode face used for commit separator."
127 :group 'git)
129 (defface git-permission-face
130 '((((class color) (background light)) (:foreground "green" :bold t)))
131 "Git mode face used for permission changes."
132 :group 'git)
135 ;;;; Utilities
136 ;;;; ------------------------------------------------------------
138 (defconst git-log-msg-separator "--- log message follows this line ---")
140 (defun git-get-env-strings (env)
141 "Build a list of NAME=VALUE strings from a list of environment strings."
142 (mapcar (lambda (entry) (concat (car entry) "=" (cdr entry))) env))
144 (defun git-call-process-env (buffer env &rest args)
145 "Wrapper for call-process that sets environment strings."
146 (if env
147 (apply #'call-process "env" nil buffer nil
148 (append (git-get-env-strings env) (list "git") args))
149 (apply #'call-process "git" nil buffer nil args)))
151 (defun git-call-process-env-string (env &rest args)
152 "Wrapper for call-process that sets environment strings, and returns the process output as a string."
153 (with-temp-buffer
154 (and (eq 0 (apply #' git-call-process-env t env args))
155 (buffer-string))))
157 (defun git-run-process-region (buffer start end program args)
158 "Run a git process with a buffer region as input."
159 (let ((output-buffer (current-buffer))
160 (dir default-directory))
161 (with-current-buffer buffer
162 (cd dir)
163 (apply #'call-process-region start end program
164 nil (list output-buffer nil) nil args))))
166 (defun git-run-command-buffer (buffer-name &rest args)
167 "Run a git command, sending the output to a buffer named BUFFER-NAME."
168 (let ((dir default-directory)
169 (buffer (get-buffer-create buffer-name)))
170 (message "Running git %s..." (car args))
171 (with-current-buffer buffer
172 (let ((default-directory dir)
173 (buffer-read-only nil))
174 (erase-buffer)
175 (apply #'git-call-process-env buffer nil args)))
176 (message "Running git %s...done" (car args))
177 buffer))
179 (defun git-run-command (buffer env &rest args)
180 (message "Running git %s..." (car args))
181 (apply #'git-call-process-env buffer env args)
182 (message "Running git %s...done" (car args)))
184 (defun git-run-command-region (buffer start end env &rest args)
185 "Run a git command with specified buffer region as input."
186 (message "Running git %s..." (car args))
187 (unless (eq 0 (if env
188 (git-run-process-region
189 buffer start end "env"
190 (append (git-get-env-strings env) (list "git") args))
191 (git-run-process-region
192 buffer start end "git" args)))
193 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string)))
194 (message "Running git %s...done" (car args)))
196 (defun git-get-string-sha1 (string)
197 "Read a SHA1 from the specified string."
198 (and string
199 (string-match "[0-9a-f]\\{40\\}" string)
200 (match-string 0 string)))
202 (defun git-get-committer-name ()
203 "Return the name to use as GIT_COMMITTER_NAME."
204 ; copied from log-edit
205 (or git-committer-name
206 (git-repo-config "user.name")
207 (and (boundp 'add-log-full-name) add-log-full-name)
208 (and (fboundp 'user-full-name) (user-full-name))
209 (and (boundp 'user-full-name) user-full-name)))
211 (defun git-get-committer-email ()
212 "Return the email address to use as GIT_COMMITTER_EMAIL."
213 ; copied from log-edit
214 (or git-committer-email
215 (git-repo-config "user.email")
216 (and (boundp 'add-log-mailing-address) add-log-mailing-address)
217 (and (fboundp 'user-mail-address) (user-mail-address))
218 (and (boundp 'user-mail-address) user-mail-address)))
220 (defun git-escape-file-name (name)
221 "Escape a file name if necessary."
222 (if (string-match "[\n\t\"\\]" name)
223 (concat "\""
224 (mapconcat (lambda (c)
225 (case c
226 (?\n "\\n")
227 (?\t "\\t")
228 (?\\ "\\\\")
229 (?\" "\\\"")
230 (t (char-to-string c))))
231 name "")
232 "\"")
233 name))
235 (defun git-get-top-dir (dir)
236 "Retrieve the top-level directory of a git tree."
237 (let ((cdup (with-output-to-string
238 (with-current-buffer standard-output
239 (cd dir)
240 (unless (eq 0 (call-process "git" nil t nil "rev-parse" "--show-cdup"))
241 (error "cannot find top-level git tree for %s." dir))))))
242 (expand-file-name (concat (file-name-as-directory dir)
243 (car (split-string cdup "\n"))))))
245 ;stolen from pcl-cvs
246 (defun git-append-to-ignore (file)
247 "Add a file name to the ignore file in its directory."
248 (let* ((fullname (expand-file-name file))
249 (dir (file-name-directory fullname))
250 (name (file-name-nondirectory fullname))
251 (ignore-name (expand-file-name git-per-dir-ignore-file dir))
252 (created (not (file-exists-p ignore-name))))
253 (save-window-excursion
254 (set-buffer (find-file-noselect ignore-name))
255 (goto-char (point-max))
256 (unless (zerop (current-column)) (insert "\n"))
257 (insert name "\n")
258 (sort-lines nil (point-min) (point-max))
259 (save-buffer))
260 (when created
261 (git-run-command nil nil "update-index" "--info-only" "--add" "--" (file-relative-name ignore-name)))
262 (git-add-status-file (if created 'added 'modified) (file-relative-name ignore-name))))
265 ;;;; Wrappers for basic git commands
266 ;;;; ------------------------------------------------------------
268 (defun git-rev-parse (rev)
269 "Parse a revision name and return its SHA1."
270 (git-get-string-sha1
271 (git-call-process-env-string nil "rev-parse" rev)))
273 (defun git-repo-config (key)
274 "Retrieve the value associated to KEY in the git repository config file."
275 (let ((str (git-call-process-env-string nil "repo-config" key)))
276 (and str (car (split-string str "\n")))))
278 (defun git-symbolic-ref (ref)
279 "Wrapper for the git-symbolic-ref command."
280 (let ((str (git-call-process-env-string nil "symbolic-ref" ref)))
281 (and str (car (split-string str "\n")))))
283 (defun git-update-ref (ref val &optional oldval)
284 "Update a reference by calling git-update-ref."
285 (apply #'git-call-process-env nil nil "update-ref" ref val (if oldval (list oldval))))
287 (defun git-read-tree (tree &optional index-file)
288 "Read a tree into the index file."
289 (apply #'git-call-process-env nil
290 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
291 "read-tree" (if tree (list tree))))
293 (defun git-write-tree (&optional index-file)
294 "Call git-write-tree and return the resulting tree SHA1 as a string."
295 (git-get-string-sha1
296 (git-call-process-env-string (and index-file `(("GIT_INDEX_FILE" . ,index-file))) "write-tree")))
298 (defun git-commit-tree (buffer tree head)
299 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
300 (let ((author-name (git-get-committer-name))
301 (author-email (git-get-committer-email))
302 author-date log-start log-end args)
303 (when head
304 (push "-p" args)
305 (push head args))
306 (with-current-buffer buffer
307 (goto-char (point-min))
309 (setq log-start (re-search-forward (concat "^" (regexp-quote git-log-msg-separator) "\n") nil t))
310 (save-restriction
311 (narrow-to-region (point-min) log-start)
312 (goto-char (point-min))
313 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t)
314 (setq author-name (match-string 1)
315 author-email (match-string 2)))
316 (goto-char (point-min))
317 (when (re-search-forward "^Date: +\\(.*\\)$" nil t)
318 (setq author-date (match-string 1)))
319 (goto-char (point-min))
320 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t)
321 (unless (string-equal head (match-string 1))
322 (push "-p" args)
323 (push (match-string 1) args))))
324 (setq log-start (point-min)))
325 (setq log-end (point-max)))
326 (git-get-string-sha1
327 (with-output-to-string
328 (with-current-buffer standard-output
329 (let ((coding-system-for-write git-commits-coding-system)
330 (env `(("GIT_AUTHOR_NAME" . ,author-name)
331 ("GIT_AUTHOR_EMAIL" . ,author-email)
332 ("GIT_COMMITTER_NAME" . ,(git-get-committer-name))
333 ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email)))))
334 (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env))
335 (apply #'git-run-command-region
336 buffer log-start log-end env
337 "commit-tree" tree (nreverse args))))))))
339 (defun git-empty-db-p ()
340 "Check if the git db is empty (no commit done yet)."
341 (not (eq 0 (call-process "git" nil nil nil "rev-parse" "--verify" "HEAD"))))
343 (defun git-get-merge-heads ()
344 "Retrieve the merge heads from the MERGE_HEAD file if present."
345 (let (heads)
346 (when (file-readable-p ".git/MERGE_HEAD")
347 (with-temp-buffer
348 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t)
349 (goto-char (point-min))
350 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t)
351 (push (match-string 0) heads))))
352 (nreverse heads)))
354 ;;;; File info structure
355 ;;;; ------------------------------------------------------------
357 ; fileinfo structure stolen from pcl-cvs
358 (defstruct (git-fileinfo
359 (:copier nil)
360 (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked))
361 (:conc-name git-fileinfo->))
362 marked ;; t/nil
363 state ;; current state
364 name ;; file name
365 old-perm new-perm ;; permission flags
366 rename-state ;; rename or copy state
367 orig-name ;; original name for renames or copies
368 needs-refresh) ;; whether file needs to be refreshed
370 (defvar git-status nil)
372 (defun git-clear-status (status)
373 "Remove everything from the status list."
374 (ewoc-filter status (lambda (info) nil)))
376 (defun git-set-files-state (files state)
377 "Set the state of a list of files."
378 (dolist (info files)
379 (unless (eq (git-fileinfo->state info) state)
380 (setf (git-fileinfo->state info) state)
381 (setf (git-fileinfo->rename-state info) nil)
382 (setf (git-fileinfo->orig-name info) nil)
383 (setf (git-fileinfo->needs-refresh info) t))))
385 (defun git-state-code (code)
386 "Convert from a string to a added/deleted/modified state."
387 (case (string-to-char code)
388 (?M 'modified)
389 (?? 'unknown)
390 (?A 'added)
391 (?D 'deleted)
392 (?U 'unmerged)
393 (t nil)))
395 (defun git-status-code-as-string (code)
396 "Format a git status code as string."
397 (case code
398 ('modified (propertize "Modified" 'face 'git-status-face))
399 ('unknown (propertize "Unknown " 'face 'git-unknown-face))
400 ('added (propertize "Added " 'face 'git-status-face))
401 ('deleted (propertize "Deleted " 'face 'git-status-face))
402 ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face))
403 ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face))
404 ('ignored (propertize "Ignored " 'face 'git-ignored-face))
405 (t "? ")))
407 (defun git-rename-as-string (info)
408 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
409 (let ((state (git-fileinfo->rename-state info)))
410 (if state
411 (propertize
412 (concat " ("
413 (if (eq state 'copy) "copied from "
414 (if (eq (git-fileinfo->state info) 'added) "renamed to "
415 "renamed from "))
416 (git-escape-file-name (git-fileinfo->orig-name info))
417 ")") 'face 'git-status-face)
418 "")))
420 (defun git-permissions-as-string (old-perm new-perm)
421 "Format a permission change as string."
422 (propertize
423 (if (or (not old-perm)
424 (not new-perm)
425 (eq 0 (logand ?\111 (logxor old-perm new-perm))))
427 (if (eq 0 (logand ?\111 old-perm)) "+x" "-x"))
428 'face 'git-permission-face))
430 (defun git-fileinfo-prettyprint (info)
431 "Pretty-printer for the git-fileinfo structure."
432 (insert (format " %s %s %s %s%s"
433 (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ")
434 (git-status-code-as-string (git-fileinfo->state info))
435 (git-permissions-as-string (git-fileinfo->old-perm info) (git-fileinfo->new-perm info))
436 (git-escape-file-name (git-fileinfo->name info))
437 (git-rename-as-string info))))
439 (defun git-parse-status (status)
440 "Parse the output of git-diff-index in the current buffer."
441 (goto-char (point-min))
442 (while (re-search-forward
443 ":\\([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"
444 nil t 1)
445 (let ((old-perm (string-to-number (match-string 1) 8))
446 (new-perm (string-to-number (match-string 2) 8))
447 (state (or (match-string 4) (match-string 6)))
448 (name (or (match-string 5) (match-string 7)))
449 (new-name (match-string 8)))
450 (if new-name ; copy or rename
451 (if (eq ?C (string-to-char state))
452 (ewoc-enter-last status (git-create-fileinfo 'added new-name old-perm new-perm 'copy name))
453 (ewoc-enter-last status (git-create-fileinfo 'deleted name 0 0 'rename new-name))
454 (ewoc-enter-last status (git-create-fileinfo 'added new-name old-perm new-perm 'rename name)))
455 (ewoc-enter-last status (git-create-fileinfo (git-state-code state) name old-perm new-perm))))))
457 (defun git-find-status-file (status file)
458 "Find a given file in the status ewoc and return its node."
459 (let ((node (ewoc-nth status 0)))
460 (while (and node (not (string= file (git-fileinfo->name (ewoc-data node)))))
461 (setq node (ewoc-next status node)))
462 node))
464 (defun git-parse-ls-files (status default-state &optional skip-existing)
465 "Parse the output of git-ls-files in the current buffer."
466 (goto-char (point-min))
467 (let (infolist)
468 (while (re-search-forward "\\([HMRCK?]\\) \\([^\0]*\\)\0" nil t 1)
469 (let ((state (match-string 1))
470 (name (match-string 2)))
471 (unless (and skip-existing (git-find-status-file status name))
472 (push (git-create-fileinfo (or (git-state-code state) default-state) name) infolist))))
473 (dolist (info (nreverse infolist))
474 (ewoc-enter-last status info))))
476 (defun git-parse-ls-unmerged (status)
477 "Parse the output of git-ls-files -u in the current buffer."
478 (goto-char (point-min))
479 (let (files)
480 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t)
481 (let ((node (git-find-status-file status (match-string 1))))
482 (when node (push (ewoc-data node) files))))
483 (git-set-files-state files 'unmerged)))
485 (defun git-add-status-file (state name)
486 "Add a new file to the status list (if not existing already) and return its node."
487 (unless git-status (error "Not in git-status buffer."))
488 (or (git-find-status-file git-status name)
489 (ewoc-enter-last git-status (git-create-fileinfo state name))))
491 (defun git-marked-files ()
492 "Return a list of all marked files, or if none a list containing just the file at cursor position."
493 (unless git-status (error "Not in git-status buffer."))
494 (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info)))
495 (list (ewoc-data (ewoc-locate git-status)))))
497 (defun git-marked-files-state (&rest states)
498 "Return marked files that are in the specified states."
499 (let ((files (git-marked-files))
500 result)
501 (dolist (info files)
502 (when (memq (git-fileinfo->state info) states)
503 (push info result)))
504 result))
506 (defun git-refresh-files ()
507 "Refresh all files that need it and clear the needs-refresh flag."
508 (unless git-status (error "Not in git-status buffer."))
509 (ewoc-map
510 (lambda (info)
511 (let ((refresh (git-fileinfo->needs-refresh info)))
512 (setf (git-fileinfo->needs-refresh info) nil)
513 refresh))
514 git-status)
515 ; move back to goal column
516 (when goal-column (move-to-column goal-column)))
518 (defun git-refresh-ewoc-hf (status)
519 "Refresh the ewoc header and footer."
520 (let ((branch (git-symbolic-ref "HEAD"))
521 (head (if (git-empty-db-p) "Nothing committed yet"
522 (substring (git-rev-parse "HEAD") 0 10)))
523 (merge-heads (git-get-merge-heads)))
524 (ewoc-set-hf status
525 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
526 default-directory
527 (if (string-match "^refs/heads/" branch)
528 (substring branch (match-end 0))
529 branch)
530 head
531 (if merge-heads
532 (concat "\nMerging: "
533 (mapconcat (lambda (str) (substring str 0 10)) merge-heads " "))
534 ""))
535 (if (ewoc-nth status 0) "" " No changes."))))
537 (defun git-get-filenames (files)
538 (mapcar (lambda (info) (git-fileinfo->name info)) files))
540 (defun git-update-index (index-file files)
541 "Run git-update-index on a list of files."
542 (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file))))
543 added deleted modified)
544 (dolist (info files)
545 (case (git-fileinfo->state info)
546 ('added (push info added))
547 ('deleted (push info deleted))
548 ('modified (push info modified))))
549 (when added
550 (apply #'git-run-command nil env "update-index" "--add" "--" (git-get-filenames added)))
551 (when deleted
552 (apply #'git-run-command nil env "update-index" "--remove" "--" (git-get-filenames deleted)))
553 (when modified
554 (apply #'git-run-command nil env "update-index" "--" (git-get-filenames modified)))))
556 (defun git-do-commit ()
557 "Perform the actual commit using the current buffer as log message."
558 (interactive)
559 (let ((buffer (current-buffer))
560 (index-file (make-temp-file "gitidx")))
561 (with-current-buffer log-edit-parent-buffer
562 (if (git-marked-files-state 'unmerged)
563 (message "You cannot commit unmerged files, resolve them first.")
564 (unwind-protect
565 (let ((files (git-marked-files-state 'added 'deleted 'modified))
566 head head-tree)
567 (unless (git-empty-db-p)
568 (setq head (git-rev-parse "HEAD")
569 head-tree (git-rev-parse "HEAD^{tree}")))
570 (if files
571 (progn
572 (git-read-tree head-tree index-file)
573 (git-update-index nil files) ;update both the default index
574 (git-update-index index-file files) ;and the temporary one
575 (let ((tree (git-write-tree index-file)))
576 (if (or (not (string-equal tree head-tree))
577 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
578 (let ((commit (git-commit-tree buffer tree head)))
579 (git-update-ref "HEAD" commit head)
580 (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil))
581 (with-current-buffer buffer (erase-buffer))
582 (git-set-files-state files 'uptodate)
583 (git-refresh-files)
584 (git-refresh-ewoc-hf git-status)
585 (message "Committed %s." commit))
586 (message "Commit aborted."))))
587 (message "No files to commit.")))
588 (delete-file index-file))))))
591 ;;;; Interactive functions
592 ;;;; ------------------------------------------------------------
594 (defun git-mark-file ()
595 "Mark the file that the cursor is on and move to the next one."
596 (interactive)
597 (unless git-status (error "Not in git-status buffer."))
598 (let* ((pos (ewoc-locate git-status))
599 (info (ewoc-data pos)))
600 (setf (git-fileinfo->marked info) t)
601 (ewoc-invalidate git-status pos)
602 (ewoc-goto-next git-status 1)))
604 (defun git-unmark-file ()
605 "Unmark the file that the cursor is on and move to the next one."
606 (interactive)
607 (unless git-status (error "Not in git-status buffer."))
608 (let* ((pos (ewoc-locate git-status))
609 (info (ewoc-data pos)))
610 (setf (git-fileinfo->marked info) nil)
611 (ewoc-invalidate git-status pos)
612 (ewoc-goto-next git-status 1)))
614 (defun git-unmark-file-up ()
615 "Unmark the file that the cursor is on and move to the previous one."
616 (interactive)
617 (unless git-status (error "Not in git-status buffer."))
618 (let* ((pos (ewoc-locate git-status))
619 (info (ewoc-data pos)))
620 (setf (git-fileinfo->marked info) nil)
621 (ewoc-invalidate git-status pos)
622 (ewoc-goto-prev git-status 1)))
624 (defun git-mark-all ()
625 "Mark all files."
626 (interactive)
627 (unless git-status (error "Not in git-status buffer."))
628 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) t) t) git-status)
629 ; move back to goal column after invalidate
630 (when goal-column (move-to-column goal-column)))
632 (defun git-unmark-all ()
633 "Unmark all files."
634 (interactive)
635 (unless git-status (error "Not in git-status buffer."))
636 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) nil) t) git-status)
637 ; move back to goal column after invalidate
638 (when goal-column (move-to-column goal-column)))
640 (defun git-toggle-all-marks ()
641 "Toggle all file marks."
642 (interactive)
643 (unless git-status (error "Not in git-status buffer."))
644 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status)
645 ; move back to goal column after invalidate
646 (when goal-column (move-to-column goal-column)))
648 (defun git-next-file (&optional n)
649 "Move the selection down N files."
650 (interactive "p")
651 (unless git-status (error "Not in git-status buffer."))
652 (ewoc-goto-next git-status n))
654 (defun git-prev-file (&optional n)
655 "Move the selection up N files."
656 (interactive "p")
657 (unless git-status (error "Not in git-status buffer."))
658 (ewoc-goto-prev git-status n))
660 (defun git-add-file ()
661 "Add marked file(s) to the index cache."
662 (interactive)
663 (let ((files (git-marked-files-state 'unknown)))
664 (unless files
665 (push (ewoc-data
666 (git-add-status-file 'added (file-relative-name
667 (read-file-name "File to add: " nil nil t))))
668 files))
669 (apply #'git-run-command nil nil "update-index" "--info-only" "--add" "--" (git-get-filenames files))
670 (git-set-files-state files 'added)
671 (git-refresh-files)))
673 (defun git-ignore-file ()
674 "Add marked file(s) to the ignore list."
675 (interactive)
676 (let ((files (git-marked-files-state 'unknown)))
677 (unless files
678 (push (ewoc-data
679 (git-add-status-file 'unknown (file-relative-name
680 (read-file-name "File to ignore: " nil nil t))))
681 files))
682 (dolist (info files) (git-append-to-ignore (git-fileinfo->name info)))
683 (git-set-files-state files 'ignored)
684 (git-refresh-files)))
686 (defun git-remove-file ()
687 "Remove the marked file(s)."
688 (interactive)
689 (let ((files (git-marked-files-state 'added 'modified 'unknown 'uptodate)))
690 (unless files
691 (push (ewoc-data
692 (git-add-status-file 'unknown (file-relative-name
693 (read-file-name "File to remove: " nil nil t))))
694 files))
695 (if (yes-or-no-p
696 (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" "")))
697 (progn
698 (dolist (info files)
699 (let ((name (git-fileinfo->name info)))
700 (when (file-exists-p name) (delete-file name))))
701 (apply #'git-run-command nil nil "update-index" "--info-only" "--remove" "--" (git-get-filenames files))
702 ; remove unknown files from the list, set the others to deleted
703 (ewoc-filter git-status
704 (lambda (info files)
705 (not (and (memq info files) (eq (git-fileinfo->state info) 'unknown))))
706 files)
707 (git-set-files-state files 'deleted)
708 (git-refresh-files)
709 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
710 (git-refresh-ewoc-hf git-status)))
711 (message "Aborting"))))
713 (defun git-revert-file ()
714 "Revert changes to the marked file(s)."
715 (interactive)
716 (let ((files (git-marked-files))
717 added modified)
718 (when (and files
719 (yes-or-no-p
720 (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" ""))))
721 (dolist (info files)
722 (case (git-fileinfo->state info)
723 ('added (push info added))
724 ('deleted (push info modified))
725 ('unmerged (push info modified))
726 ('modified (push info modified))))
727 (when added
728 (apply #'git-run-command nil nil "update-index" "--force-remove" "--" (git-get-filenames added))
729 (git-set-files-state added 'unknown))
730 (when modified
731 (apply #'git-run-command nil nil "checkout" "HEAD" (git-get-filenames modified))
732 (git-set-files-state modified 'uptodate))
733 (git-refresh-files))))
735 (defun git-resolve-file ()
736 "Resolve conflicts in marked file(s)."
737 (interactive)
738 (let ((files (git-marked-files-state 'unmerged)))
739 (when files
740 (apply #'git-run-command nil nil "update-index" "--info-only" "--" (git-get-filenames files))
741 (git-set-files-state files 'modified)
742 (git-refresh-files))))
744 (defun git-remove-handled ()
745 "Remove handled files from the status list."
746 (interactive)
747 (ewoc-filter git-status
748 (lambda (info)
749 (not (or (eq (git-fileinfo->state info) 'ignored)
750 (eq (git-fileinfo->state info) 'uptodate)))))
751 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
752 (git-refresh-ewoc-hf git-status)))
754 (defun git-setup-diff-buffer (buffer)
755 "Setup a buffer for displaying a diff."
756 (with-current-buffer buffer
757 (diff-mode)
758 (goto-char (point-min))
759 (setq buffer-read-only t))
760 (display-buffer buffer)
761 (shrink-window-if-larger-than-buffer))
763 (defun git-diff-file ()
764 "Diff the marked file(s) against HEAD."
765 (interactive)
766 (let ((files (git-marked-files)))
767 (git-setup-diff-buffer
768 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files)))))
770 (defun git-diff-unmerged-file (stage)
771 "Diff the marked unmerged file(s) against the specified stage."
772 (let ((files (git-marked-files)))
773 (git-setup-diff-buffer
774 (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files)))))
776 (defun git-diff-file-base ()
777 "Diff the marked unmerged file(s) against the common base file."
778 (interactive)
779 (git-diff-unmerged-file "-1"))
781 (defun git-diff-file-mine ()
782 "Diff the marked unmerged file(s) against my pre-merge version."
783 (interactive)
784 (git-diff-unmerged-file "-2"))
786 (defun git-diff-file-other ()
787 "Diff the marked unmerged file(s) against the other's pre-merge version."
788 (interactive)
789 (git-diff-unmerged-file "-3"))
791 (defun git-diff-file-combined ()
792 "Do a combined diff of the marked unmerged file(s)."
793 (interactive)
794 (git-diff-unmerged-file "-c"))
796 (defun git-diff-file-idiff ()
797 "Perform an interactive diff on the current file."
798 (interactive)
799 (error "Interactive diffs not implemented yet."))
801 (defun git-log-file ()
802 "Display a log of changes to the marked file(s)."
803 (interactive)
804 (let* ((files (git-marked-files))
805 (coding-system-for-read git-commits-coding-system)
806 (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files))))
807 (with-current-buffer buffer
808 ; (git-log-mode) FIXME: implement log mode
809 (goto-char (point-min))
810 (setq buffer-read-only t))
811 (display-buffer buffer)))
813 (defun git-log-edit-files ()
814 "Return a list of marked files for use in the log-edit buffer."
815 (with-current-buffer log-edit-parent-buffer
816 (git-get-filenames (git-marked-files-state 'added 'deleted 'modified))))
818 (defun git-commit-file ()
819 "Commit the marked file(s), asking for a commit message."
820 (interactive)
821 (unless git-status (error "Not in git-status buffer."))
822 (let ((buffer (get-buffer-create "*git-commit*"))
823 (merge-heads (git-get-merge-heads))
824 (dir default-directory)
825 (sign-off git-append-signed-off-by))
826 (with-current-buffer buffer
827 (when (eq 0 (buffer-size))
828 (cd dir)
829 (erase-buffer)
830 (insert
831 (propertize
832 (format "Author: %s <%s>\n%s"
833 (git-get-committer-name) (git-get-committer-email)
834 (if merge-heads
835 (format "Parent: %s\n%s\n"
836 (git-rev-parse "HEAD")
837 (mapconcat (lambda (str) (concat "Parent: " str)) merge-heads "\n"))
838 ""))
839 'face 'git-header-face)
840 (propertize git-log-msg-separator 'face 'git-separator-face)
841 "\n")
842 (cond ((and merge-heads (file-readable-p ".git/MERGE_MSG"))
843 (insert-file-contents ".git/MERGE_MSG"))
844 (sign-off
845 (insert (format "\n\nSigned-off-by: %s <%s>\n"
846 (git-get-committer-name) (git-get-committer-email)))))))
847 (let ((log-edit-font-lock-keywords
848 `(("^\\(Author:\\|Date:\\|Parent:\\|Signed-off-by:\\)\\(.*\\)"
849 (1 font-lock-keyword-face)
850 (2 font-lock-function-name-face))
851 (,(concat "^\\(" (regexp-quote git-log-msg-separator) "\\)$")
852 (1 font-lock-comment-face)))))
853 (log-edit #'git-do-commit nil #'git-log-edit-files buffer))))
855 (defun git-find-file ()
856 "Visit the current file in its own buffer."
857 (interactive)
858 (unless git-status (error "Not in git-status buffer."))
859 (let ((info (ewoc-data (ewoc-locate git-status))))
860 (find-file (git-fileinfo->name info))
861 (when (eq 'unmerged (git-fileinfo->state info))
862 (smerge-mode))))
864 (defun git-find-file-imerge ()
865 "Visit the current file in interactive merge mode."
866 (interactive)
867 (unless git-status (error "Not in git-status buffer."))
868 (let ((info (ewoc-data (ewoc-locate git-status))))
869 (find-file (git-fileinfo->name info))
870 (smerge-ediff)))
872 (defun git-view-file ()
873 "View the current file in its own buffer."
874 (interactive)
875 (unless git-status (error "Not in git-status buffer."))
876 (let ((info (ewoc-data (ewoc-locate git-status))))
877 (view-file (git-fileinfo->name info))))
879 (defun git-refresh-status ()
880 "Refresh the git status buffer."
881 (interactive)
882 (let* ((status git-status)
883 (pos (ewoc-locate status))
884 (cur-name (and pos (git-fileinfo->name (ewoc-data pos)))))
885 (unless status (error "Not in git-status buffer."))
886 (git-clear-status status)
887 (git-run-command nil nil "update-index" "--info-only" "--refresh")
888 (if (git-empty-db-p)
889 ; we need some special handling for an empty db
890 (with-temp-buffer
891 (git-run-command t nil "ls-files" "-z" "-t" "-c")
892 (git-parse-ls-files status 'added))
893 (with-temp-buffer
894 (git-run-command t nil "diff-index" "-z" "-M" "HEAD")
895 (git-parse-status status)))
896 (with-temp-buffer
897 (git-run-command t nil "ls-files" "-z" "-u")
898 (git-parse-ls-unmerged status))
899 (when (file-readable-p ".git/info/exclude")
900 (with-temp-buffer
901 (git-run-command t nil "ls-files" "-z" "-t" "-o"
902 "--exclude-from=.git/info/exclude"
903 (concat "--exclude-per-directory=" git-per-dir-ignore-file))
904 (git-parse-ls-files status 'unknown)))
905 (git-refresh-files)
906 (git-refresh-ewoc-hf status)
907 ; move point to the current file name if any
908 (let ((node (and cur-name (git-find-status-file status cur-name))))
909 (when node (ewoc-goto-node status node)))))
911 (defun git-status-quit ()
912 "Quit git-status mode."
913 (interactive)
914 (bury-buffer))
916 ;;;; Major Mode
917 ;;;; ------------------------------------------------------------
919 (defvar git-status-mode-hook nil
920 "Run after `git-status-mode' is setup.")
922 (defvar git-status-mode-map nil
923 "Keymap for git major mode.")
925 (defvar git-status nil
926 "List of all files managed by the git-status mode.")
928 (unless git-status-mode-map
929 (let ((map (make-keymap))
930 (diff-map (make-sparse-keymap)))
931 (suppress-keymap map)
932 (define-key map " " 'git-next-file)
933 (define-key map "a" 'git-add-file)
934 (define-key map "c" 'git-commit-file)
935 (define-key map "d" diff-map)
936 (define-key map "=" 'git-diff-file)
937 (define-key map "f" 'git-find-file)
938 (define-key map "\r" 'git-find-file)
939 (define-key map "g" 'git-refresh-status)
940 (define-key map "i" 'git-ignore-file)
941 (define-key map "l" 'git-log-file)
942 (define-key map "m" 'git-mark-file)
943 (define-key map "M" 'git-mark-all)
944 (define-key map "n" 'git-next-file)
945 (define-key map "p" 'git-prev-file)
946 (define-key map "q" 'git-status-quit)
947 (define-key map "r" 'git-remove-file)
948 (define-key map "R" 'git-resolve-file)
949 (define-key map "T" 'git-toggle-all-marks)
950 (define-key map "u" 'git-unmark-file)
951 (define-key map "U" 'git-revert-file)
952 (define-key map "v" 'git-view-file)
953 (define-key map "x" 'git-remove-handled)
954 (define-key map "\C-?" 'git-unmark-file-up)
955 (define-key map "\M-\C-?" 'git-unmark-all)
956 ; the diff submap
957 (define-key diff-map "b" 'git-diff-file-base)
958 (define-key diff-map "c" 'git-diff-file-combined)
959 (define-key diff-map "=" 'git-diff-file)
960 (define-key diff-map "e" 'git-diff-file-idiff)
961 (define-key diff-map "E" 'git-find-file-imerge)
962 (define-key diff-map "m" 'git-diff-file-mine)
963 (define-key diff-map "o" 'git-diff-file-other)
964 (setq git-status-mode-map map)))
966 ;; git mode should only run in the *git status* buffer
967 (put 'git-status-mode 'mode-class 'special)
969 (defun git-status-mode ()
970 "Major mode for interacting with Git.
971 Commands:
972 \\{git-status-mode-map}"
973 (kill-all-local-variables)
974 (buffer-disable-undo)
975 (setq mode-name "git status"
976 major-mode 'git-status-mode
977 goal-column 17
978 buffer-read-only t)
979 (use-local-map git-status-mode-map)
980 (let ((buffer-read-only nil))
981 (erase-buffer)
982 (let ((status (ewoc-create 'git-fileinfo-prettyprint "" "")))
983 (set (make-local-variable 'git-status) status))
984 (set (make-local-variable 'list-buffers-directory) default-directory)
985 (run-hooks 'git-status-mode-hook)))
987 (defun git-status (dir)
988 "Entry point into git-status mode."
989 (interactive "DSelect directory: ")
990 (setq dir (git-get-top-dir dir))
991 (if (file-directory-p (concat (file-name-as-directory dir) ".git"))
992 (let ((buffer (create-file-buffer (expand-file-name "*git-status*" dir))))
993 (switch-to-buffer buffer)
994 (cd dir)
995 (git-status-mode)
996 (git-refresh-status)
997 (goto-char (point-min)))
998 (message "%s is not a git working tree." dir)))
1000 (provide 'git)
1001 ;;; git.el ends here