1 ;;; git.el --- A user interface for git
3 ;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org>
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation; either version 2 of
10 ;; the License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be
13 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
14 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 ;; PURPOSE. See the GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public
18 ;; License along with this program; if not, write to the Free
19 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 ;; This file contains an interface for the git version control
25 ;; system. It provides easy access to the most frequently used git
26 ;; commands. The user interface is as far as possible identical to
27 ;; that of the PCL-CVS mode.
29 ;; To install: put this file on the load-path and place the following
30 ;; in your .emacs file:
34 ;; To start: `M-x git-status'
37 ;; - portability to XEmacs
38 ;; - 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
44 ;; - switching branches
46 ;; - git-show-branch browser
50 (eval-when-compile (require 'cl
))
56 ;;;; ------------------------------------------------------------
59 "A user interface for the git versioning system."
62 (defcustom git-committer-name nil
63 "User name to use for commits.
64 The default is to fall back to the repository config,
65 then to `add-log-full-name' and then to `user-full-name'."
67 :type
'(choice (const :tag
"Default" nil
)
68 (string :tag
"Name")))
70 (defcustom git-committer-email nil
71 "Email address to use for commits.
72 The default is to fall back to the git repository config,
73 then to `add-log-mailing-address' and then to `user-mail-address'."
75 :type
'(choice (const :tag
"Default" nil
)
76 (string :tag
"Email")))
78 (defcustom git-commits-coding-system nil
79 "Default coding system for the log message of git commits."
81 :type
'(choice (const :tag
"From repository config" nil
)
84 (defcustom git-append-signed-off-by nil
85 "Whether to append a Signed-off-by line to the commit message before editing."
89 (defcustom git-reuse-status-buffer t
90 "Whether `git-status' should try to reuse an existing buffer
91 if there is already one that displays the same directory."
95 (defcustom git-per-dir-ignore-file
".gitignore"
96 "Name of the per-directory ignore file."
101 (defface git-status-face
102 '((((class color
) (background light
)) (:foreground
"purple"))
103 (((class color
) (background dark
)) (:foreground
"salmon")))
104 "Git mode face used to highlight added and modified files."
107 (defface git-unmerged-face
108 '((((class color
) (background light
)) (:foreground
"red" :bold t
))
109 (((class color
) (background dark
)) (:foreground
"red" :bold t
)))
110 "Git mode face used to highlight unmerged files."
113 (defface git-unknown-face
114 '((((class color
) (background light
)) (:foreground
"goldenrod" :bold t
))
115 (((class color
) (background dark
)) (:foreground
"goldenrod" :bold t
)))
116 "Git mode face used to highlight unknown files."
119 (defface git-uptodate-face
120 '((((class color
) (background light
)) (:foreground
"grey60"))
121 (((class color
) (background dark
)) (:foreground
"grey40")))
122 "Git mode face used to highlight up-to-date files."
125 (defface git-ignored-face
126 '((((class color
) (background light
)) (:foreground
"grey60"))
127 (((class color
) (background dark
)) (:foreground
"grey40")))
128 "Git mode face used to highlight ignored files."
131 (defface git-mark-face
132 '((((class color
) (background light
)) (:foreground
"red" :bold t
))
133 (((class color
) (background dark
)) (:foreground
"tomato" :bold t
)))
134 "Git mode face used for the file marks."
137 (defface git-header-face
138 '((((class color
) (background light
)) (:foreground
"blue"))
139 (((class color
) (background dark
)) (:foreground
"blue")))
140 "Git mode face used for commit headers."
143 (defface git-separator-face
144 '((((class color
) (background light
)) (:foreground
"brown"))
145 (((class color
) (background dark
)) (:foreground
"brown")))
146 "Git mode face used for commit separator."
149 (defface git-permission-face
150 '((((class color
) (background light
)) (:foreground
"green" :bold t
))
151 (((class color
) (background dark
)) (:foreground
"green" :bold t
)))
152 "Git mode face used for permission changes."
157 ;;;; ------------------------------------------------------------
159 (defconst git-log-msg-separator
"--- log message follows this line ---")
161 (defvar git-log-edit-font-lock-keywords
162 `(("^\\(Author:\\|Date:\\|Parent:\\|Signed-off-by:\\)\\(.*\\)$"
163 (1 font-lock-keyword-face
)
164 (2 font-lock-function-name-face
))
165 (,(concat "^\\(" (regexp-quote git-log-msg-separator
) "\\)$")
166 (1 font-lock-comment-face
))))
168 (defun git-get-env-strings (env)
169 "Build a list of NAME=VALUE strings from a list of environment strings."
170 (mapcar (lambda (entry) (concat (car entry
) "=" (cdr entry
))) env
))
172 (defun git-call-process-env (buffer env
&rest args
)
173 "Wrapper for call-process that sets environment strings."
175 (apply #'call-process
"env" nil buffer nil
176 (append (git-get-env-strings env
) (list "git") args
))
177 (apply #'call-process
"git" nil buffer nil args
)))
179 (defun git-call-process-env-string (env &rest args
)
180 "Wrapper for call-process that sets environment strings,
181 and returns the process output as a string."
183 (and (eq 0 (apply #' git-call-process-env t env args
))
186 (defun git-run-process-region (buffer start end program args
)
187 "Run a git process with a buffer region as input."
188 (let ((output-buffer (current-buffer))
189 (dir default-directory
))
190 (with-current-buffer buffer
192 (apply #'call-process-region start end program
193 nil
(list output-buffer nil
) nil args
))))
195 (defun git-run-command-buffer (buffer-name &rest args
)
196 "Run a git command, sending the output to a buffer named BUFFER-NAME."
197 (let ((dir default-directory
)
198 (buffer (get-buffer-create buffer-name
)))
199 (message "Running git %s..." (car args
))
200 (with-current-buffer buffer
201 (let ((default-directory dir
)
202 (buffer-read-only nil
))
204 (apply #'git-call-process-env buffer nil args
)))
205 (message "Running git %s...done" (car args
))
208 (defun git-run-command (buffer env
&rest args
)
209 (message "Running git %s..." (car args
))
210 (apply #'git-call-process-env buffer env args
)
211 (message "Running git %s...done" (car args
)))
213 (defun git-run-command-region (buffer start end env
&rest args
)
214 "Run a git command with specified buffer region as input."
215 (message "Running git %s..." (car args
))
216 (unless (eq 0 (if env
217 (git-run-process-region
218 buffer start end
"env"
219 (append (git-get-env-strings env
) (list "git") args
))
220 (git-run-process-region
221 buffer start end
"git" args
)))
222 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x
) args
" ") (buffer-string)))
223 (message "Running git %s...done" (car args
)))
225 (defun git-run-hook (hook env
&rest args
)
226 "Run a git hook and display its output if any."
227 (let ((dir default-directory
)
228 (hook-name (expand-file-name (concat ".git/hooks/" hook
))))
229 (or (not (file-executable-p hook-name
))
230 (let (status (buffer (get-buffer-create "*Git Hook Output*")))
231 (with-current-buffer buffer
236 (apply #'call-process
"env" nil
(list buffer t
) nil
237 (append (git-get-env-strings env
) (list hook-name
) args
))
238 (apply #'call-process hook-name nil
(list buffer t
) nil args
))))
239 (display-message-or-buffer buffer
)
242 (defun git-get-string-sha1 (string)
243 "Read a SHA1 from the specified string."
245 (string-match "[0-9a-f]\\{40\\}" string
)
246 (match-string 0 string
)))
248 (defun git-get-committer-name ()
249 "Return the name to use as GIT_COMMITTER_NAME."
250 ; copied from log-edit
251 (or git-committer-name
252 (git-config "user.name")
253 (and (boundp 'add-log-full-name
) add-log-full-name
)
254 (and (fboundp 'user-full-name
) (user-full-name))
255 (and (boundp 'user-full-name
) user-full-name
)))
257 (defun git-get-committer-email ()
258 "Return the email address to use as GIT_COMMITTER_EMAIL."
259 ; copied from log-edit
260 (or git-committer-email
261 (git-config "user.email")
262 (and (boundp 'add-log-mailing-address
) add-log-mailing-address
)
263 (and (fboundp 'user-mail-address
) (user-mail-address))
264 (and (boundp 'user-mail-address
) user-mail-address
)))
266 (defun git-get-commits-coding-system ()
267 "Return the coding system to use for commits."
268 (let ((repo-config (git-config "i18n.commitencoding")))
269 (or git-commits-coding-system
271 (fboundp 'locale-charset-to-coding-system
)
272 (locale-charset-to-coding-system repo-config
))
275 (defun git-get-logoutput-coding-system ()
276 "Return the coding system used for git-log output."
277 (let ((repo-config (or (git-config "i18n.logoutputencoding")
278 (git-config "i18n.commitencoding"))))
279 (or git-commits-coding-system
281 (fboundp 'locale-charset-to-coding-system
)
282 (locale-charset-to-coding-system repo-config
))
285 (defun git-escape-file-name (name)
286 "Escape a file name if necessary."
287 (if (string-match "[\n\t\"\\]" name
)
289 (mapconcat (lambda (c)
295 (t (char-to-string c
))))
300 (defun git-get-top-dir (dir)
301 "Retrieve the top-level directory of a git tree."
302 (let ((cdup (with-output-to-string
303 (with-current-buffer standard-output
305 (unless (eq 0 (call-process "git" nil t nil
"rev-parse" "--show-cdup"))
306 (error "cannot find top-level git tree for %s." dir
))))))
307 (expand-file-name (concat (file-name-as-directory dir
)
308 (car (split-string cdup
"\n"))))))
311 (defun git-append-to-ignore (file)
312 "Add a file name to the ignore file in its directory."
313 (let* ((fullname (expand-file-name file
))
314 (dir (file-name-directory fullname
))
315 (name (file-name-nondirectory fullname
))
316 (ignore-name (expand-file-name git-per-dir-ignore-file dir
))
317 (created (not (file-exists-p ignore-name
))))
318 (save-window-excursion
319 (set-buffer (find-file-noselect ignore-name
))
320 (goto-char (point-max))
321 (unless (zerop (current-column)) (insert "\n"))
322 (insert "/" name
"\n")
323 (sort-lines nil
(point-min) (point-max))
326 (git-run-command nil nil
"update-index" "--add" "--" (file-relative-name ignore-name
)))
327 (git-update-status-files (list (file-relative-name ignore-name
)) 'unknown
)))
329 ; propertize definition for XEmacs, stolen from erc-compat
331 (unless (fboundp 'propertize
)
332 (defun propertize (string &rest props
)
333 (let ((string (copy-sequence string
)))
335 (put-text-property 0 (length string
) (nth 0 props
) (nth 1 props
) string
)
336 (setq props
(cddr props
)))
339 ;;;; Wrappers for basic git commands
340 ;;;; ------------------------------------------------------------
342 (defun git-rev-parse (rev)
343 "Parse a revision name and return its SHA1."
345 (git-call-process-env-string nil
"rev-parse" rev
)))
347 (defun git-config (key)
348 "Retrieve the value associated to KEY in the git repository config file."
349 (let ((str (git-call-process-env-string nil
"config" key
)))
350 (and str
(car (split-string str
"\n")))))
352 (defun git-symbolic-ref (ref)
353 "Wrapper for the git-symbolic-ref command."
354 (let ((str (git-call-process-env-string nil
"symbolic-ref" ref
)))
355 (and str
(car (split-string str
"\n")))))
357 (defun git-update-ref (ref newval
&optional oldval reason
)
358 "Update a reference by calling git-update-ref."
359 (let ((args (and oldval
(list oldval
))))
365 (eq 0 (apply #'git-call-process-env nil nil
"update-ref" args
))))
367 (defun git-read-tree (tree &optional index-file
)
368 "Read a tree into the index file."
369 (apply #'git-call-process-env nil
370 (if index-file
`(("GIT_INDEX_FILE" .
,index-file
)) nil
)
371 "read-tree" (if tree
(list tree
))))
373 (defun git-write-tree (&optional index-file
)
374 "Call git-write-tree and return the resulting tree SHA1 as a string."
376 (git-call-process-env-string (and index-file
`(("GIT_INDEX_FILE" .
,index-file
))) "write-tree")))
378 (defun git-commit-tree (buffer tree head
)
379 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
380 (let ((author-name (git-get-committer-name))
381 (author-email (git-get-committer-email))
382 (subject "commit (initial): ")
383 author-date log-start log-end args coding-system-for-write
)
385 (setq subject
"commit: ")
388 (with-current-buffer buffer
389 (goto-char (point-min))
391 (setq log-start
(re-search-forward (concat "^" (regexp-quote git-log-msg-separator
) "\n") nil t
))
393 (narrow-to-region (point-min) log-start
)
394 (goto-char (point-min))
395 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t
)
396 (setq author-name
(match-string 1)
397 author-email
(match-string 2)))
398 (goto-char (point-min))
399 (when (re-search-forward "^Date: +\\(.*\\)$" nil t
)
400 (setq author-date
(match-string 1)))
401 (goto-char (point-min))
402 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t
)
403 (unless (string-equal head
(match-string 1))
404 (setq subject
"commit (merge): ")
406 (push (match-string 1) args
))))
407 (setq log-start
(point-min)))
408 (setq log-end
(point-max))
409 (goto-char log-start
)
410 (when (re-search-forward ".*$" nil t
)
411 (setq subject
(concat subject
(match-string 0))))
412 (setq coding-system-for-write buffer-file-coding-system
))
415 (with-output-to-string
416 (with-current-buffer standard-output
417 (let ((env `(("GIT_AUTHOR_NAME" .
,author-name
)
418 ("GIT_AUTHOR_EMAIL" .
,author-email
)
419 ("GIT_COMMITTER_NAME" .
,(git-get-committer-name))
420 ("GIT_COMMITTER_EMAIL" .
,(git-get-committer-email)))))
421 (when author-date
(push `("GIT_AUTHOR_DATE" .
,author-date
) env
))
422 (apply #'git-run-command-region
423 buffer log-start log-end env
424 "commit-tree" tree
(nreverse args
))))))))
425 (and (git-update-ref "HEAD" commit head subject
)
428 (defun git-empty-db-p ()
429 "Check if the git db is empty (no commit done yet)."
430 (not (eq 0 (call-process "git" nil nil nil
"rev-parse" "--verify" "HEAD"))))
432 (defun git-get-merge-heads ()
433 "Retrieve the merge heads from the MERGE_HEAD file if present."
435 (when (file-readable-p ".git/MERGE_HEAD")
437 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t
)
438 (goto-char (point-min))
439 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t
)
440 (push (match-string 0) heads
))))
443 (defun git-get-commit-description (commit)
444 "Get a one-line description of COMMIT."
445 (let ((coding-system-for-read (git-get-logoutput-coding-system)))
446 (let ((descr (git-call-process-env-string nil
"log" "--max-count=1" "--pretty=oneline" commit
)))
447 (if (and descr
(string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr
))
448 (concat (substring (match-string 1 descr
) 0 10) " - " (match-string 2 descr
))
451 ;;;; File info structure
452 ;;;; ------------------------------------------------------------
454 ; fileinfo structure stolen from pcl-cvs
455 (defstruct (git-fileinfo
457 (:constructor git-create-fileinfo
(state name
&optional old-perm new-perm rename-state orig-name marked
))
458 (:conc-name git-fileinfo-
>))
460 state
;; current state
462 old-perm new-perm
;; permission flags
463 rename-state
;; rename or copy state
464 orig-name
;; original name for renames or copies
465 needs-refresh
) ;; whether file needs to be refreshed
467 (defvar git-status nil
)
469 (defun git-clear-status (status)
470 "Remove everything from the status list."
471 (ewoc-filter status
(lambda (info) nil
)))
473 (defun git-set-files-state (files state
)
474 "Set the state of a list of files."
476 (unless (eq (git-fileinfo->state info
) state
)
477 (setf (git-fileinfo->state info
) state
)
478 (setf (git-fileinfo->rename-state info
) nil
)
479 (setf (git-fileinfo->orig-name info
) nil
)
480 (setf (git-fileinfo->needs-refresh info
) t
))))
482 (defun git-state-code (code)
483 "Convert from a string to a added/deleted/modified state."
484 (case (string-to-char code
)
492 (defun git-status-code-as-string (code)
493 "Format a git status code as string."
495 ('modified
(propertize "Modified" 'face
'git-status-face
))
496 ('unknown
(propertize "Unknown " 'face
'git-unknown-face
))
497 ('added
(propertize "Added " 'face
'git-status-face
))
498 ('deleted
(propertize "Deleted " 'face
'git-status-face
))
499 ('unmerged
(propertize "Unmerged" 'face
'git-unmerged-face
))
500 ('uptodate
(propertize "Uptodate" 'face
'git-uptodate-face
))
501 ('ignored
(propertize "Ignored " 'face
'git-ignored-face
))
504 (defun git-rename-as-string (info)
505 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
506 (let ((state (git-fileinfo->rename-state info
)))
510 (if (eq state
'copy
) "copied from "
511 (if (eq (git-fileinfo->state info
) 'added
) "renamed from "
513 (git-escape-file-name (git-fileinfo->orig-name info
))
514 ")") 'face
'git-status-face
)
517 (defun git-permissions-as-string (old-perm new-perm
)
518 "Format a permission change as string."
520 (if (or (not old-perm
)
522 (eq 0 (logand ?
\111 (logxor old-perm new-perm
))))
524 (if (eq 0 (logand ?
\111 old-perm
)) "+x" "-x"))
525 'face
'git-permission-face
))
527 (defun git-fileinfo-prettyprint (info)
528 "Pretty-printer for the git-fileinfo structure."
529 (insert (concat " " (if (git-fileinfo->marked info
) (propertize "*" 'face
'git-mark-face
) " ")
530 " " (git-status-code-as-string (git-fileinfo->state info
))
531 " " (git-permissions-as-string (git-fileinfo->old-perm info
) (git-fileinfo->new-perm info
))
532 " " (git-escape-file-name (git-fileinfo->name info
))
533 (git-rename-as-string info
))))
535 (defun git-insert-fileinfo (status info
&optional refresh
)
536 "Insert INFO in the status buffer, optionally refreshing an existing one."
537 (let ((node (and refresh
538 (git-find-status-file status
(git-fileinfo->name info
)))))
539 (setf (git-fileinfo->needs-refresh info
) t
)
540 (when node
;preserve the marked flag
541 (setf (git-fileinfo->marked info
) (git-fileinfo->marked
(ewoc-data node
))))
542 (if node
(setf (ewoc-data node
) info
) (ewoc-enter-last status info
))))
544 (defun git-run-diff-index (status files
)
545 "Run git-diff-index on FILES and parse the results into STATUS.
546 Return the list of files that haven't been handled."
547 (let ((refresh files
))
549 (apply #'git-run-command t nil
"diff-index" "-z" "-M" "HEAD" "--" files
)
550 (goto-char (point-min))
551 (while (re-search-forward
552 ":\\([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"
554 (let ((old-perm (string-to-number (match-string 1) 8))
555 (new-perm (string-to-number (match-string 2) 8))
556 (state (or (match-string 4) (match-string 6)))
557 (name (or (match-string 5) (match-string 7)))
558 (new-name (match-string 8)))
559 (if new-name
; copy or rename
560 (if (eq ?C
(string-to-char state
))
561 (git-insert-fileinfo status
(git-create-fileinfo 'added new-name old-perm new-perm
'copy name
) refresh
)
562 (git-insert-fileinfo status
(git-create-fileinfo 'deleted name
0 0 'rename new-name
) refresh
)
563 (git-insert-fileinfo status
(git-create-fileinfo 'added new-name old-perm new-perm
'rename name
)) refresh
)
564 (git-insert-fileinfo status
(git-create-fileinfo (git-state-code state
) name old-perm new-perm
) refresh
))
565 (setq files
(delete name files
))
566 (when new-name
(setq files
(delete new-name files
)))))))
569 (defun git-find-status-file (status file
)
570 "Find a given file in the status ewoc and return its node."
571 (let ((node (ewoc-nth status
0)))
572 (while (and node
(not (string= file
(git-fileinfo->name
(ewoc-data node
)))))
573 (setq node
(ewoc-next status node
)))
576 (defun git-run-ls-files (status files default-state
&rest options
)
577 "Run git-ls-files on FILES and parse the results into STATUS.
578 Return the list of files that haven't been handled."
579 (let ((refresh files
))
581 (apply #'git-run-command t nil
"ls-files" "-z" "-t" (append options
(list "--") files
))
582 (goto-char (point-min))
583 (while (re-search-forward "\\([HMRCK?]\\) \\([^\0]*\\)\0" nil t
1)
584 (let ((state (match-string 1))
585 (name (match-string 2)))
586 (git-insert-fileinfo status
(git-create-fileinfo (or (git-state-code state
) default-state
) name
) refresh
)
587 (setq files
(delete name files
))))))
590 (defun git-run-ls-unmerged (status files
)
591 "Run git-ls-files -u on FILES and parse the results into STATUS."
593 (apply #'git-run-command t nil
"ls-files" "-z" "-u" "--" files
)
594 (goto-char (point-min))
595 (let (unmerged-files)
596 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t
)
597 (let ((node (git-find-status-file status
(match-string 1))))
598 (when node
(push (ewoc-data node
) unmerged-files
))))
599 (git-set-files-state unmerged-files
'unmerged
))))
601 (defun git-get-exclude-files ()
602 "Get the list of exclude files to pass to git-ls-files."
604 (config (git-config "core.excludesfile")))
605 (when (file-readable-p ".git/info/exclude")
606 (push ".git/info/exclude" files
))
607 (when (and config
(file-readable-p config
))
611 (defun git-update-status-files (files &optional default-state
)
612 "Update the status of FILES from the index."
613 (unless git-status
(error "Not in git-status buffer."))
614 (let* ((status git-status
)
616 (if (git-empty-db-p) ; we need some special handling for an empty db
617 (git-run-ls-files status files
'added
"-c")
618 (git-run-diff-index status files
))))
619 (git-run-ls-unmerged status files
)
620 (when (or (not files
) remaining-files
)
621 (let ((exclude-files (git-get-exclude-files)))
622 (setq remaining-files
(apply #'git-run-ls-files status remaining-files
'unknown
"-o"
623 (concat "--exclude-per-directory=" git-per-dir-ignore-file
)
624 (mapcar (lambda (f) (concat "--exclude-from=" f
)) exclude-files
)))))
625 ; mark remaining files with the default state (or remove them if nil)
626 (when remaining-files
628 (ewoc-map (lambda (info)
629 (when (member (git-fileinfo->name info
) remaining-files
)
630 (git-set-files-state (list info
) default-state
))
635 (not (member (git-fileinfo->name info
) files
)))
638 (git-refresh-ewoc-hf status
)))
640 (defun git-marked-files ()
641 "Return a list of all marked files, or if none a list containing just the file at cursor position."
642 (unless git-status
(error "Not in git-status buffer."))
643 (or (ewoc-collect git-status
(lambda (info) (git-fileinfo->marked info
)))
644 (list (ewoc-data (ewoc-locate git-status
)))))
646 (defun git-marked-files-state (&rest states
)
647 "Return marked files that are in the specified states."
648 (let ((files (git-marked-files))
651 (when (memq (git-fileinfo->state info
) states
)
655 (defun git-refresh-files ()
656 "Refresh all files that need it and clear the needs-refresh flag."
657 (unless git-status
(error "Not in git-status buffer."))
660 (let ((refresh (git-fileinfo->needs-refresh info
)))
661 (setf (git-fileinfo->needs-refresh info
) nil
)
664 ; move back to goal column
665 (when goal-column
(move-to-column goal-column
)))
667 (defun git-refresh-ewoc-hf (status)
668 "Refresh the ewoc header and footer."
669 (let ((branch (git-symbolic-ref "HEAD"))
670 (head (if (git-empty-db-p) "Nothing committed yet"
671 (git-get-commit-description "HEAD")))
672 (merge-heads (git-get-merge-heads)))
674 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
677 (if (string-match "^refs/heads/" branch
)
678 (substring branch
(match-end 0))
680 "none (detached HEAD)")
683 (concat "\nMerging: "
684 (mapconcat (lambda (str) (git-get-commit-description str
)) merge-heads
"\n "))
686 (if (ewoc-nth status
0) "" " No changes."))))
688 (defun git-get-filenames (files)
689 (mapcar (lambda (info) (git-fileinfo->name info
)) files
))
691 (defun git-update-index (index-file files
)
692 "Run git-update-index on a list of files."
693 (let ((env (and index-file
`(("GIT_INDEX_FILE" .
,index-file
))))
694 added deleted modified
)
696 (case (git-fileinfo->state info
)
697 ('added
(push info added
))
698 ('deleted
(push info deleted
))
699 ('modified
(push info modified
))))
701 (apply #'git-run-command nil env
"update-index" "--add" "--" (git-get-filenames added
)))
703 (apply #'git-run-command nil env
"update-index" "--remove" "--" (git-get-filenames deleted
)))
705 (apply #'git-run-command nil env
"update-index" "--" (git-get-filenames modified
)))))
707 (defun git-run-pre-commit-hook ()
708 "Run the pre-commit hook if any."
709 (unless git-status
(error "Not in git-status buffer."))
710 (let ((files (git-marked-files-state 'added
'deleted
'modified
)))
712 (not (file-executable-p ".git/hooks/pre-commit"))
713 (let ((index-file (make-temp-file "gitidx")))
715 (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}"))))
716 (git-read-tree head-tree index-file
)
717 (git-update-index index-file files
)
718 (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" .
,index-file
))))
719 (delete-file index-file
))))))
721 (defun git-do-commit ()
722 "Perform the actual commit using the current buffer as log message."
724 (let ((buffer (current-buffer))
725 (index-file (make-temp-file "gitidx")))
726 (with-current-buffer log-edit-parent-buffer
727 (if (git-marked-files-state 'unmerged
)
728 (message "You cannot commit unmerged files, resolve them first.")
730 (let ((files (git-marked-files-state 'added
'deleted
'modified
))
732 (unless (git-empty-db-p)
733 (setq head
(git-rev-parse "HEAD")
734 head-tree
(git-rev-parse "HEAD^{tree}")))
737 (git-read-tree head-tree index-file
)
738 (git-update-index nil files
) ;update both the default index
739 (git-update-index index-file files
) ;and the temporary one
740 (let ((tree (git-write-tree index-file
)))
741 (if (or (not (string-equal tree head-tree
))
742 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
743 (let ((commit (git-commit-tree buffer tree head
)))
744 (condition-case nil
(delete-file ".git/MERGE_HEAD") (error nil
))
745 (condition-case nil
(delete-file ".git/MERGE_MSG") (error nil
))
746 (with-current-buffer buffer
(erase-buffer))
747 (git-set-files-state files
'uptodate
)
748 (git-run-command nil nil
"rerere")
750 (git-refresh-ewoc-hf git-status
)
751 (message "Committed %s." commit
)
752 (git-run-hook "post-commit" nil
))
753 (message "Commit aborted."))))
754 (message "No files to commit.")))
755 (delete-file index-file
))))))
758 ;;;; Interactive functions
759 ;;;; ------------------------------------------------------------
761 (defun git-mark-file ()
762 "Mark the file that the cursor is on and move to the next one."
764 (unless git-status
(error "Not in git-status buffer."))
765 (let* ((pos (ewoc-locate git-status
))
766 (info (ewoc-data pos
)))
767 (setf (git-fileinfo->marked info
) t
)
768 (ewoc-invalidate git-status pos
)
769 (ewoc-goto-next git-status
1)))
771 (defun git-unmark-file ()
772 "Unmark the file that the cursor is on and move to the next one."
774 (unless git-status
(error "Not in git-status buffer."))
775 (let* ((pos (ewoc-locate git-status
))
776 (info (ewoc-data pos
)))
777 (setf (git-fileinfo->marked info
) nil
)
778 (ewoc-invalidate git-status pos
)
779 (ewoc-goto-next git-status
1)))
781 (defun git-unmark-file-up ()
782 "Unmark the file that the cursor is on and move to the previous one."
784 (unless git-status
(error "Not in git-status buffer."))
785 (let* ((pos (ewoc-locate git-status
))
786 (info (ewoc-data pos
)))
787 (setf (git-fileinfo->marked info
) nil
)
788 (ewoc-invalidate git-status pos
)
789 (ewoc-goto-prev git-status
1)))
791 (defun git-mark-all ()
794 (unless git-status
(error "Not in git-status buffer."))
795 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) t
) t
) git-status
)
796 ; move back to goal column after invalidate
797 (when goal-column
(move-to-column goal-column
)))
799 (defun git-unmark-all ()
802 (unless git-status
(error "Not in git-status buffer."))
803 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) nil
) t
) git-status
)
804 ; move back to goal column after invalidate
805 (when goal-column
(move-to-column goal-column
)))
807 (defun git-toggle-all-marks ()
808 "Toggle all file marks."
810 (unless git-status
(error "Not in git-status buffer."))
811 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) (not (git-fileinfo->marked info
))) t
) git-status
)
812 ; move back to goal column after invalidate
813 (when goal-column
(move-to-column goal-column
)))
815 (defun git-next-file (&optional n
)
816 "Move the selection down N files."
818 (unless git-status
(error "Not in git-status buffer."))
819 (ewoc-goto-next git-status n
))
821 (defun git-prev-file (&optional n
)
822 "Move the selection up N files."
824 (unless git-status
(error "Not in git-status buffer."))
825 (ewoc-goto-prev git-status n
))
827 (defun git-next-unmerged-file (&optional n
)
828 "Move the selection down N unmerged files."
830 (unless git-status
(error "Not in git-status buffer."))
831 (let* ((last (ewoc-locate git-status
))
832 (node (ewoc-next git-status last
)))
833 (while (and node
(> n
0))
834 (when (eq 'unmerged
(git-fileinfo->state
(ewoc-data node
)))
837 (setq node
(ewoc-next git-status node
)))
838 (ewoc-goto-node git-status last
)))
840 (defun git-prev-unmerged-file (&optional n
)
841 "Move the selection up N unmerged files."
843 (unless git-status
(error "Not in git-status buffer."))
844 (let* ((last (ewoc-locate git-status
))
845 (node (ewoc-prev git-status last
)))
846 (while (and node
(> n
0))
847 (when (eq 'unmerged
(git-fileinfo->state
(ewoc-data node
)))
850 (setq node
(ewoc-prev git-status node
)))
851 (ewoc-goto-node git-status last
)))
853 (defun git-add-file ()
854 "Add marked file(s) to the index cache."
856 (let ((files (git-get-filenames (git-marked-files-state 'unknown
))))
858 (push (file-relative-name (read-file-name "File to add: " nil nil t
)) files
))
859 (apply #'git-run-command nil nil
"update-index" "--add" "--" files
)
860 (git-update-status-files files
'uptodate
)))
862 (defun git-ignore-file ()
863 "Add marked file(s) to the ignore list."
865 (let ((files (git-get-filenames (git-marked-files-state 'unknown
))))
867 (push (file-relative-name (read-file-name "File to ignore: " nil nil t
)) files
))
868 (dolist (f files
) (git-append-to-ignore f
))
869 (git-update-status-files files
'ignored
)))
871 (defun git-remove-file ()
872 "Remove the marked file(s)."
874 (let ((files (git-get-filenames (git-marked-files-state 'added
'modified
'unknown
'uptodate
))))
876 (push (file-relative-name (read-file-name "File to remove: " nil nil t
)) files
))
878 (format "Remove %d file%s? " (length files
) (if (> (length files
) 1) "s" "")))
881 (when (file-exists-p name
) (delete-file name
)))
882 (apply #'git-run-command nil nil
"update-index" "--remove" "--" files
)
883 (git-update-status-files files nil
))
884 (message "Aborting"))))
886 (defun git-revert-file ()
887 "Revert changes to the marked file(s)."
889 (let ((files (git-marked-files))
893 (format "Revert %d file%s? " (length files
) (if (> (length files
) 1) "s" ""))))
895 (case (git-fileinfo->state info
)
896 ('added
(push (git-fileinfo->name info
) added
))
897 ('deleted
(push (git-fileinfo->name info
) modified
))
898 ('unmerged
(push (git-fileinfo->name info
) modified
))
899 ('modified
(push (git-fileinfo->name info
) modified
))))
901 (apply #'git-run-command nil nil
"update-index" "--force-remove" "--" added
))
903 (apply #'git-run-command nil nil
"checkout" "HEAD" modified
))
904 (git-update-status-files (append added modified
) 'uptodate
))))
906 (defun git-resolve-file ()
907 "Resolve conflicts in marked file(s)."
909 (let ((files (git-get-filenames (git-marked-files-state 'unmerged
))))
911 (apply #'git-run-command nil nil
"update-index" "--" files
)
912 (git-update-status-files files
'uptodate
))))
914 (defun git-remove-handled ()
915 "Remove handled files from the status list."
917 (ewoc-filter git-status
919 (not (or (eq (git-fileinfo->state info
) 'ignored
)
920 (eq (git-fileinfo->state info
) 'uptodate
)))))
921 (unless (ewoc-nth git-status
0) ; refresh header if list is empty
922 (git-refresh-ewoc-hf git-status
)))
924 (defun git-setup-diff-buffer (buffer)
925 "Setup a buffer for displaying a diff."
926 (let ((dir default-directory
))
927 (with-current-buffer buffer
929 (goto-char (point-min))
930 (setq default-directory dir
)
931 (setq buffer-read-only t
)))
932 (display-buffer buffer
)
933 (shrink-window-if-larger-than-buffer))
935 (defun git-diff-file ()
936 "Diff the marked file(s) against HEAD."
938 (let ((files (git-marked-files)))
939 (git-setup-diff-buffer
940 (apply #'git-run-command-buffer
"*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files
)))))
942 (defun git-diff-file-merge-head (arg)
943 "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)."
945 (let ((files (git-marked-files))
946 (merge-heads (git-get-merge-heads)))
947 (unless merge-heads
(error "No merge in progress"))
948 (git-setup-diff-buffer
949 (apply #'git-run-command-buffer
"*git-diff*" "diff-index" "-p" "-M"
950 (or (nth (1- arg
) merge-heads
) "HEAD") "--" (git-get-filenames files
)))))
952 (defun git-diff-unmerged-file (stage)
953 "Diff the marked unmerged file(s) against the specified stage."
954 (let ((files (git-marked-files)))
955 (git-setup-diff-buffer
956 (apply #'git-run-command-buffer
"*git-diff*" "diff-files" "-p" stage
"--" (git-get-filenames files
)))))
958 (defun git-diff-file-base ()
959 "Diff the marked unmerged file(s) against the common base file."
961 (git-diff-unmerged-file "-1"))
963 (defun git-diff-file-mine ()
964 "Diff the marked unmerged file(s) against my pre-merge version."
966 (git-diff-unmerged-file "-2"))
968 (defun git-diff-file-other ()
969 "Diff the marked unmerged file(s) against the other's pre-merge version."
971 (git-diff-unmerged-file "-3"))
973 (defun git-diff-file-combined ()
974 "Do a combined diff of the marked unmerged file(s)."
976 (git-diff-unmerged-file "-c"))
978 (defun git-diff-file-idiff ()
979 "Perform an interactive diff on the current file."
981 (let ((files (git-marked-files-state 'added
'deleted
'modified
)))
982 (unless (eq 1 (length files
))
983 (error "Cannot perform an interactive diff on multiple files."))
984 (let* ((filename (car (git-get-filenames files
)))
985 (buff1 (find-file-noselect filename
))
986 (buff2 (git-run-command-buffer (concat filename
".~HEAD~") "cat-file" "blob" (concat "HEAD:" filename
))))
987 (ediff-buffers buff1 buff2
))))
989 (defun git-log-file ()
990 "Display a log of changes to the marked file(s)."
992 (let* ((files (git-marked-files))
993 (coding-system-for-read git-commits-coding-system
)
994 (buffer (apply #'git-run-command-buffer
"*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files
))))
995 (with-current-buffer buffer
996 ; (git-log-mode) FIXME: implement log mode
997 (goto-char (point-min))
998 (setq buffer-read-only t
))
999 (display-buffer buffer
)))
1001 (defun git-log-edit-files ()
1002 "Return a list of marked files for use in the log-edit buffer."
1003 (with-current-buffer log-edit-parent-buffer
1004 (git-get-filenames (git-marked-files-state 'added
'deleted
'modified
))))
1006 (defun git-append-sign-off (name email
)
1007 "Append a Signed-off-by entry to the current buffer, avoiding duplicates."
1008 (let ((sign-off (format "Signed-off-by: %s <%s>" name email
))
1009 (case-fold-search t
))
1010 (goto-char (point-min))
1011 (unless (re-search-forward (concat "^" (regexp-quote sign-off
)) nil t
)
1012 (goto-char (point-min))
1013 (unless (re-search-forward "^Signed-off-by: " nil t
)
1014 (setq sign-off
(concat "\n" sign-off
)))
1015 (goto-char (point-max))
1016 (insert sign-off
"\n"))))
1018 (defun git-setup-log-buffer (buffer &optional author-name author-email subject date msg
)
1019 "Setup the log buffer for a commit."
1020 (unless git-status
(error "Not in git-status buffer."))
1021 (let ((merge-heads (git-get-merge-heads))
1022 (dir default-directory
)
1023 (committer-name (git-get-committer-name))
1024 (committer-email (git-get-committer-email))
1025 (sign-off git-append-signed-off-by
))
1026 (with-current-buffer buffer
1031 (format "Author: %s <%s>\n%s%s"
1032 (or author-name committer-name
)
1033 (or author-email committer-email
)
1034 (if date
(format "Date: %s\n" date
) "")
1036 (format "Parent: %s\n%s\n"
1037 (git-rev-parse "HEAD")
1038 (mapconcat (lambda (str) (concat "Parent: " str
)) merge-heads
"\n"))
1040 'face
'git-header-face
)
1041 (propertize git-log-msg-separator
'face
'git-separator-face
)
1043 (when subject
(insert subject
"\n\n"))
1044 (cond (msg (insert msg
"\n"))
1045 ((file-readable-p ".dotest/msg")
1046 (insert-file-contents ".dotest/msg"))
1047 ((file-readable-p ".git/MERGE_MSG")
1048 (insert-file-contents ".git/MERGE_MSG")))
1049 ; delete empty lines at end
1050 (goto-char (point-min))
1051 (when (re-search-forward "\n+\\'" nil t
)
1052 (replace-match "\n" t t
))
1053 (when sign-off
(git-append-sign-off committer-name committer-email
)))))
1055 (defun git-commit-file ()
1056 "Commit the marked file(s), asking for a commit message."
1058 (unless git-status
(error "Not in git-status buffer."))
1059 (when (git-run-pre-commit-hook)
1060 (let ((buffer (get-buffer-create "*git-commit*"))
1061 (coding-system (git-get-commits-coding-system))
1062 author-name author-email subject date
)
1063 (when (eq 0 (buffer-size buffer
))
1064 (when (file-readable-p ".dotest/info")
1066 (insert-file-contents ".dotest/info")
1067 (goto-char (point-min))
1068 (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t
)
1069 (setq author-name
(match-string 1))
1070 (setq author-email
(match-string 2)))
1071 (goto-char (point-min))
1072 (when (re-search-forward "^Subject: \\(.*\\)$" nil t
)
1073 (setq subject
(match-string 1)))
1074 (goto-char (point-min))
1075 (when (re-search-forward "^Date: \\(.*\\)$" nil t
)
1076 (setq date
(match-string 1)))))
1077 (git-setup-log-buffer buffer author-name author-email subject date
))
1078 (log-edit #'git-do-commit nil
#'git-log-edit-files buffer
)
1079 (setq font-lock-keywords
(font-lock-compile-keywords git-log-edit-font-lock-keywords
))
1080 (setq buffer-file-coding-system coding-system
)
1081 (re-search-forward (regexp-quote (concat git-log-msg-separator
"\n")) nil t
))))
1083 (defun git-find-file ()
1084 "Visit the current file in its own buffer."
1086 (unless git-status
(error "Not in git-status buffer."))
1087 (let ((info (ewoc-data (ewoc-locate git-status
))))
1088 (find-file (git-fileinfo->name info
))
1089 (when (eq 'unmerged
(git-fileinfo->state info
))
1092 (defun git-find-file-other-window ()
1093 "Visit the current file in its own buffer in another window."
1095 (unless git-status
(error "Not in git-status buffer."))
1096 (let ((info (ewoc-data (ewoc-locate git-status
))))
1097 (find-file-other-window (git-fileinfo->name info
))
1098 (when (eq 'unmerged
(git-fileinfo->state info
))
1101 (defun git-find-file-imerge ()
1102 "Visit the current file in interactive merge mode."
1104 (unless git-status
(error "Not in git-status buffer."))
1105 (let ((info (ewoc-data (ewoc-locate git-status
))))
1106 (find-file (git-fileinfo->name info
))
1109 (defun git-view-file ()
1110 "View the current file in its own buffer."
1112 (unless git-status
(error "Not in git-status buffer."))
1113 (let ((info (ewoc-data (ewoc-locate git-status
))))
1114 (view-file (git-fileinfo->name info
))))
1116 (defun git-refresh-status ()
1117 "Refresh the git status buffer."
1119 (let* ((status git-status
)
1120 (pos (ewoc-locate status
))
1121 (cur-name (and pos
(git-fileinfo->name
(ewoc-data pos
)))))
1122 (unless status
(error "Not in git-status buffer."))
1123 (git-run-command nil nil
"update-index" "--refresh")
1124 (git-clear-status status
)
1125 (git-update-status-files nil
)
1126 ; move point to the current file name if any
1127 (let ((node (and cur-name
(git-find-status-file status cur-name
))))
1128 (when node
(ewoc-goto-node status node
)))))
1130 (defun git-status-quit ()
1131 "Quit git-status mode."
1136 ;;;; ------------------------------------------------------------
1138 (defvar git-status-mode-hook nil
1139 "Run after `git-status-mode' is setup.")
1141 (defvar git-status-mode-map nil
1142 "Keymap for git major mode.")
1144 (defvar git-status nil
1145 "List of all files managed by the git-status mode.")
1147 (unless git-status-mode-map
1148 (let ((map (make-keymap))
1149 (diff-map (make-sparse-keymap)))
1150 (suppress-keymap map
)
1151 (define-key map
"?" 'git-help
)
1152 (define-key map
"h" 'git-help
)
1153 (define-key map
" " 'git-next-file
)
1154 (define-key map
"a" 'git-add-file
)
1155 (define-key map
"c" 'git-commit-file
)
1156 (define-key map
"d" diff-map
)
1157 (define-key map
"=" 'git-diff-file
)
1158 (define-key map
"f" 'git-find-file
)
1159 (define-key map
"\r" 'git-find-file
)
1160 (define-key map
"g" 'git-refresh-status
)
1161 (define-key map
"i" 'git-ignore-file
)
1162 (define-key map
"l" 'git-log-file
)
1163 (define-key map
"m" 'git-mark-file
)
1164 (define-key map
"M" 'git-mark-all
)
1165 (define-key map
"n" 'git-next-file
)
1166 (define-key map
"N" 'git-next-unmerged-file
)
1167 (define-key map
"o" 'git-find-file-other-window
)
1168 (define-key map
"p" 'git-prev-file
)
1169 (define-key map
"P" 'git-prev-unmerged-file
)
1170 (define-key map
"q" 'git-status-quit
)
1171 (define-key map
"r" 'git-remove-file
)
1172 (define-key map
"R" 'git-resolve-file
)
1173 (define-key map
"T" 'git-toggle-all-marks
)
1174 (define-key map
"u" 'git-unmark-file
)
1175 (define-key map
"U" 'git-revert-file
)
1176 (define-key map
"v" 'git-view-file
)
1177 (define-key map
"x" 'git-remove-handled
)
1178 (define-key map
"\C-?" 'git-unmark-file-up
)
1179 (define-key map
"\M-\C-?" 'git-unmark-all
)
1181 (define-key diff-map
"b" 'git-diff-file-base
)
1182 (define-key diff-map
"c" 'git-diff-file-combined
)
1183 (define-key diff-map
"=" 'git-diff-file
)
1184 (define-key diff-map
"e" 'git-diff-file-idiff
)
1185 (define-key diff-map
"E" 'git-find-file-imerge
)
1186 (define-key diff-map
"h" 'git-diff-file-merge-head
)
1187 (define-key diff-map
"m" 'git-diff-file-mine
)
1188 (define-key diff-map
"o" 'git-diff-file-other
)
1189 (setq git-status-mode-map map
)))
1191 ;; git mode should only run in the *git status* buffer
1192 (put 'git-status-mode
'mode-class
'special
)
1194 (defun git-status-mode ()
1195 "Major mode for interacting with Git.
1197 \\{git-status-mode-map}"
1198 (kill-all-local-variables)
1199 (buffer-disable-undo)
1200 (setq mode-name
"git status"
1201 major-mode
'git-status-mode
1204 (use-local-map git-status-mode-map
)
1205 (let ((buffer-read-only nil
))
1207 (let ((status (ewoc-create 'git-fileinfo-prettyprint
"" "")))
1208 (set (make-local-variable 'git-status
) status
))
1209 (set (make-local-variable 'list-buffers-directory
) default-directory
)
1210 (run-hooks 'git-status-mode-hook
)))
1212 (defun git-find-status-buffer (dir)
1213 "Find the git status buffer handling a specified directory."
1214 (let ((list (buffer-list))
1215 (fulldir (expand-file-name dir
))
1217 (while (and list
(not found
))
1218 (let ((buffer (car list
)))
1219 (with-current-buffer buffer
1220 (when (and list-buffers-directory
1221 (string-equal fulldir
(expand-file-name list-buffers-directory
))
1222 (string-match "\\*git-status\\*$" (buffer-name buffer
)))
1223 (setq found buffer
))))
1224 (setq list
(cdr list
)))
1227 (defun git-status (dir)
1228 "Entry point into git-status mode."
1229 (interactive "DSelect directory: ")
1230 (setq dir
(git-get-top-dir dir
))
1231 (if (file-directory-p (concat (file-name-as-directory dir
) ".git"))
1232 (let ((buffer (or (and git-reuse-status-buffer
(git-find-status-buffer dir
))
1233 (create-file-buffer (expand-file-name "*git-status*" dir
)))))
1234 (switch-to-buffer buffer
)
1237 (git-refresh-status)
1238 (goto-char (point-min)))
1239 (message "%s is not a git working tree." dir
)))
1242 "Display help for Git mode."
1244 (describe-function 'git-status-mode
))
1247 ;;; git.el ends here