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 "Git mode face used to highlight added and modified files."
106 (defface git-unmerged-face
107 '((((class color
) (background light
)) (:foreground
"red" :bold t
)))
108 "Git mode face used to highlight unmerged files."
111 (defface git-unknown-face
112 '((((class color
) (background light
)) (:foreground
"goldenrod" :bold t
)))
113 "Git mode face used to highlight unknown files."
116 (defface git-uptodate-face
117 '((((class color
) (background light
)) (:foreground
"grey60")))
118 "Git mode face used to highlight up-to-date files."
121 (defface git-ignored-face
122 '((((class color
) (background light
)) (:foreground
"grey60")))
123 "Git mode face used to highlight ignored files."
126 (defface git-mark-face
127 '((((class color
) (background light
)) (:foreground
"red" :bold t
)))
128 "Git mode face used for the file marks."
131 (defface git-header-face
132 '((((class color
) (background light
)) (:foreground
"blue")))
133 "Git mode face used for commit headers."
136 (defface git-separator-face
137 '((((class color
) (background light
)) (:foreground
"brown")))
138 "Git mode face used for commit separator."
141 (defface git-permission-face
142 '((((class color
) (background light
)) (:foreground
"green" :bold t
)))
143 "Git mode face used for permission changes."
148 ;;;; ------------------------------------------------------------
150 (defconst git-log-msg-separator
"--- log message follows this line ---")
152 (defvar git-log-edit-font-lock-keywords
153 `(("^\\(Author:\\|Date:\\|Parent:\\|Signed-off-by:\\)\\(.*\\)$"
154 (1 font-lock-keyword-face
)
155 (2 font-lock-function-name-face
))
156 (,(concat "^\\(" (regexp-quote git-log-msg-separator
) "\\)$")
157 (1 font-lock-comment-face
))))
159 (defun git-get-env-strings (env)
160 "Build a list of NAME=VALUE strings from a list of environment strings."
161 (mapcar (lambda (entry) (concat (car entry
) "=" (cdr entry
))) env
))
163 (defun git-call-process-env (buffer env
&rest args
)
164 "Wrapper for call-process that sets environment strings."
166 (apply #'call-process
"env" nil buffer nil
167 (append (git-get-env-strings env
) (list "git") args
))
168 (apply #'call-process
"git" nil buffer nil args
)))
170 (defun git-call-process-env-string (env &rest args
)
171 "Wrapper for call-process that sets environment strings,
172 and returns the process output as a string."
174 (and (eq 0 (apply #' git-call-process-env t env args
))
177 (defun git-run-process-region (buffer start end program args
)
178 "Run a git process with a buffer region as input."
179 (let ((output-buffer (current-buffer))
180 (dir default-directory
))
181 (with-current-buffer buffer
183 (apply #'call-process-region start end program
184 nil
(list output-buffer nil
) nil args
))))
186 (defun git-run-command-buffer (buffer-name &rest args
)
187 "Run a git command, sending the output to a buffer named BUFFER-NAME."
188 (let ((dir default-directory
)
189 (buffer (get-buffer-create buffer-name
)))
190 (message "Running git %s..." (car args
))
191 (with-current-buffer buffer
192 (let ((default-directory dir
)
193 (buffer-read-only nil
))
195 (apply #'git-call-process-env buffer nil args
)))
196 (message "Running git %s...done" (car args
))
199 (defun git-run-command (buffer env
&rest args
)
200 (message "Running git %s..." (car args
))
201 (apply #'git-call-process-env buffer env args
)
202 (message "Running git %s...done" (car args
)))
204 (defun git-run-command-region (buffer start end env
&rest args
)
205 "Run a git command with specified buffer region as input."
206 (message "Running git %s..." (car args
))
207 (unless (eq 0 (if env
208 (git-run-process-region
209 buffer start end
"env"
210 (append (git-get-env-strings env
) (list "git") args
))
211 (git-run-process-region
212 buffer start end
"git" args
)))
213 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x
) args
" ") (buffer-string)))
214 (message "Running git %s...done" (car args
)))
216 (defun git-run-hook (hook env
&rest args
)
217 "Run a git hook and display its output if any."
218 (let ((dir default-directory
)
219 (hook-name (expand-file-name (concat ".git/hooks/" hook
))))
220 (or (not (file-executable-p hook-name
))
221 (let (status (buffer (get-buffer-create "*Git Hook Output*")))
222 (with-current-buffer buffer
227 (apply #'call-process
"env" nil
(list buffer t
) nil
228 (append (git-get-env-strings env
) (list hook-name
) args
))
229 (apply #'call-process hook-name nil
(list buffer t
) nil args
))))
230 (display-message-or-buffer buffer
)
233 (defun git-get-string-sha1 (string)
234 "Read a SHA1 from the specified string."
236 (string-match "[0-9a-f]\\{40\\}" string
)
237 (match-string 0 string
)))
239 (defun git-get-committer-name ()
240 "Return the name to use as GIT_COMMITTER_NAME."
241 ; copied from log-edit
242 (or git-committer-name
243 (git-config "user.name")
244 (and (boundp 'add-log-full-name
) add-log-full-name
)
245 (and (fboundp 'user-full-name
) (user-full-name))
246 (and (boundp 'user-full-name
) user-full-name
)))
248 (defun git-get-committer-email ()
249 "Return the email address to use as GIT_COMMITTER_EMAIL."
250 ; copied from log-edit
251 (or git-committer-email
252 (git-config "user.email")
253 (and (boundp 'add-log-mailing-address
) add-log-mailing-address
)
254 (and (fboundp 'user-mail-address
) (user-mail-address))
255 (and (boundp 'user-mail-address
) user-mail-address
)))
257 (defun git-get-commits-coding-system ()
258 "Return the coding system to use for commits."
259 (let ((repo-config (git-config "i18n.commitencoding")))
260 (or git-commits-coding-system
262 (fboundp 'locale-charset-to-coding-system
)
263 (locale-charset-to-coding-system repo-config
))
266 (defun git-get-logoutput-coding-system ()
267 "Return the coding system used for git-log output."
268 (let ((repo-config (or (git-config "i18n.logoutputencoding")
269 (git-config "i18n.commitencoding"))))
270 (or git-commits-coding-system
272 (fboundp 'locale-charset-to-coding-system
)
273 (locale-charset-to-coding-system repo-config
))
276 (defun git-escape-file-name (name)
277 "Escape a file name if necessary."
278 (if (string-match "[\n\t\"\\]" name
)
280 (mapconcat (lambda (c)
286 (t (char-to-string c
))))
291 (defun git-get-top-dir (dir)
292 "Retrieve the top-level directory of a git tree."
293 (let ((cdup (with-output-to-string
294 (with-current-buffer standard-output
296 (unless (eq 0 (call-process "git" nil t nil
"rev-parse" "--show-cdup"))
297 (error "cannot find top-level git tree for %s." dir
))))))
298 (expand-file-name (concat (file-name-as-directory dir
)
299 (car (split-string cdup
"\n"))))))
302 (defun git-append-to-ignore (file)
303 "Add a file name to the ignore file in its directory."
304 (let* ((fullname (expand-file-name file
))
305 (dir (file-name-directory fullname
))
306 (name (file-name-nondirectory fullname
))
307 (ignore-name (expand-file-name git-per-dir-ignore-file dir
))
308 (created (not (file-exists-p ignore-name
))))
309 (save-window-excursion
310 (set-buffer (find-file-noselect ignore-name
))
311 (goto-char (point-max))
312 (unless (zerop (current-column)) (insert "\n"))
313 (insert "/" name
"\n")
314 (sort-lines nil
(point-min) (point-max))
317 (git-run-command nil nil
"update-index" "--info-only" "--add" "--" (file-relative-name ignore-name
)))
318 (git-add-status-file (if created
'added
'modified
) (file-relative-name ignore-name
))))
320 ; propertize definition for XEmacs, stolen from erc-compat
322 (unless (fboundp 'propertize
)
323 (defun propertize (string &rest props
)
324 (let ((string (copy-sequence string
)))
326 (put-text-property 0 (length string
) (nth 0 props
) (nth 1 props
) string
)
327 (setq props
(cddr props
)))
330 ;;;; Wrappers for basic git commands
331 ;;;; ------------------------------------------------------------
333 (defun git-rev-parse (rev)
334 "Parse a revision name and return its SHA1."
336 (git-call-process-env-string nil
"rev-parse" rev
)))
338 (defun git-config (key)
339 "Retrieve the value associated to KEY in the git repository config file."
340 (let ((str (git-call-process-env-string nil
"config" key
)))
341 (and str
(car (split-string str
"\n")))))
343 (defun git-symbolic-ref (ref)
344 "Wrapper for the git-symbolic-ref command."
345 (let ((str (git-call-process-env-string nil
"symbolic-ref" ref
)))
346 (and str
(car (split-string str
"\n")))))
348 (defun git-update-ref (ref val
&optional oldval
)
349 "Update a reference by calling git-update-ref."
350 (apply #'git-call-process-env nil nil
"update-ref" ref val
(if oldval
(list oldval
))))
352 (defun git-read-tree (tree &optional index-file
)
353 "Read a tree into the index file."
354 (apply #'git-call-process-env nil
355 (if index-file
`(("GIT_INDEX_FILE" .
,index-file
)) nil
)
356 "read-tree" (if tree
(list tree
))))
358 (defun git-write-tree (&optional index-file
)
359 "Call git-write-tree and return the resulting tree SHA1 as a string."
361 (git-call-process-env-string (and index-file
`(("GIT_INDEX_FILE" .
,index-file
))) "write-tree")))
363 (defun git-commit-tree (buffer tree head
)
364 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
365 (let ((author-name (git-get-committer-name))
366 (author-email (git-get-committer-email))
367 author-date log-start log-end args coding-system-for-write
)
371 (with-current-buffer buffer
372 (goto-char (point-min))
374 (setq log-start
(re-search-forward (concat "^" (regexp-quote git-log-msg-separator
) "\n") nil t
))
376 (narrow-to-region (point-min) log-start
)
377 (goto-char (point-min))
378 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t
)
379 (setq author-name
(match-string 1)
380 author-email
(match-string 2)))
381 (goto-char (point-min))
382 (when (re-search-forward "^Date: +\\(.*\\)$" nil t
)
383 (setq author-date
(match-string 1)))
384 (goto-char (point-min))
385 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t
)
386 (unless (string-equal head
(match-string 1))
388 (push (match-string 1) args
))))
389 (setq log-start
(point-min)))
390 (setq log-end
(point-max))
391 (setq coding-system-for-write buffer-file-coding-system
))
393 (with-output-to-string
394 (with-current-buffer standard-output
395 (let ((env `(("GIT_AUTHOR_NAME" .
,author-name
)
396 ("GIT_AUTHOR_EMAIL" .
,author-email
)
397 ("GIT_COMMITTER_NAME" .
,(git-get-committer-name))
398 ("GIT_COMMITTER_EMAIL" .
,(git-get-committer-email)))))
399 (when author-date
(push `("GIT_AUTHOR_DATE" .
,author-date
) env
))
400 (apply #'git-run-command-region
401 buffer log-start log-end env
402 "commit-tree" tree
(nreverse args
))))))))
404 (defun git-empty-db-p ()
405 "Check if the git db is empty (no commit done yet)."
406 (not (eq 0 (call-process "git" nil nil nil
"rev-parse" "--verify" "HEAD"))))
408 (defun git-get-merge-heads ()
409 "Retrieve the merge heads from the MERGE_HEAD file if present."
411 (when (file-readable-p ".git/MERGE_HEAD")
413 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t
)
414 (goto-char (point-min))
415 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t
)
416 (push (match-string 0) heads
))))
419 (defun git-get-commit-description (commit)
420 "Get a one-line description of COMMIT."
421 (let ((coding-system-for-read (git-get-logoutput-coding-system)))
422 (let ((descr (git-call-process-env-string nil
"log" "--max-count=1" "--pretty=oneline" commit
)))
423 (if (and descr
(string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr
))
424 (concat (substring (match-string 1 descr
) 0 10) " - " (match-string 2 descr
))
427 ;;;; File info structure
428 ;;;; ------------------------------------------------------------
430 ; fileinfo structure stolen from pcl-cvs
431 (defstruct (git-fileinfo
433 (:constructor git-create-fileinfo
(state name
&optional old-perm new-perm rename-state orig-name marked
))
434 (:conc-name git-fileinfo-
>))
436 state
;; current state
438 old-perm new-perm
;; permission flags
439 rename-state
;; rename or copy state
440 orig-name
;; original name for renames or copies
441 needs-refresh
) ;; whether file needs to be refreshed
443 (defvar git-status nil
)
445 (defun git-clear-status (status)
446 "Remove everything from the status list."
447 (ewoc-filter status
(lambda (info) nil
)))
449 (defun git-set-files-state (files state
)
450 "Set the state of a list of files."
452 (unless (eq (git-fileinfo->state info
) state
)
453 (setf (git-fileinfo->state info
) state
)
454 (setf (git-fileinfo->rename-state info
) nil
)
455 (setf (git-fileinfo->orig-name info
) nil
)
456 (setf (git-fileinfo->needs-refresh info
) t
))))
458 (defun git-state-code (code)
459 "Convert from a string to a added/deleted/modified state."
460 (case (string-to-char code
)
468 (defun git-status-code-as-string (code)
469 "Format a git status code as string."
471 ('modified
(propertize "Modified" 'face
'git-status-face
))
472 ('unknown
(propertize "Unknown " 'face
'git-unknown-face
))
473 ('added
(propertize "Added " 'face
'git-status-face
))
474 ('deleted
(propertize "Deleted " 'face
'git-status-face
))
475 ('unmerged
(propertize "Unmerged" 'face
'git-unmerged-face
))
476 ('uptodate
(propertize "Uptodate" 'face
'git-uptodate-face
))
477 ('ignored
(propertize "Ignored " 'face
'git-ignored-face
))
480 (defun git-rename-as-string (info)
481 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
482 (let ((state (git-fileinfo->rename-state info
)))
486 (if (eq state
'copy
) "copied from "
487 (if (eq (git-fileinfo->state info
) 'added
) "renamed from "
489 (git-escape-file-name (git-fileinfo->orig-name info
))
490 ")") 'face
'git-status-face
)
493 (defun git-permissions-as-string (old-perm new-perm
)
494 "Format a permission change as string."
496 (if (or (not old-perm
)
498 (eq 0 (logand ?
\111 (logxor old-perm new-perm
))))
500 (if (eq 0 (logand ?
\111 old-perm
)) "+x" "-x"))
501 'face
'git-permission-face
))
503 (defun git-fileinfo-prettyprint (info)
504 "Pretty-printer for the git-fileinfo structure."
505 (insert (concat " " (if (git-fileinfo->marked info
) (propertize "*" 'face
'git-mark-face
) " ")
506 " " (git-status-code-as-string (git-fileinfo->state info
))
507 " " (git-permissions-as-string (git-fileinfo->old-perm info
) (git-fileinfo->new-perm info
))
508 " " (git-escape-file-name (git-fileinfo->name info
))
509 (git-rename-as-string info
))))
511 (defun git-parse-status (status)
512 "Parse the output of git-diff-index in the current buffer."
513 (goto-char (point-min))
514 (while (re-search-forward
515 ":\\([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"
517 (let ((old-perm (string-to-number (match-string 1) 8))
518 (new-perm (string-to-number (match-string 2) 8))
519 (state (or (match-string 4) (match-string 6)))
520 (name (or (match-string 5) (match-string 7)))
521 (new-name (match-string 8)))
522 (if new-name
; copy or rename
523 (if (eq ?C
(string-to-char state
))
524 (ewoc-enter-last status
(git-create-fileinfo 'added new-name old-perm new-perm
'copy name
))
525 (ewoc-enter-last status
(git-create-fileinfo 'deleted name
0 0 'rename new-name
))
526 (ewoc-enter-last status
(git-create-fileinfo 'added new-name old-perm new-perm
'rename name
)))
527 (ewoc-enter-last status
(git-create-fileinfo (git-state-code state
) name old-perm new-perm
))))))
529 (defun git-find-status-file (status file
)
530 "Find a given file in the status ewoc and return its node."
531 (let ((node (ewoc-nth status
0)))
532 (while (and node
(not (string= file
(git-fileinfo->name
(ewoc-data node
)))))
533 (setq node
(ewoc-next status node
)))
536 (defun git-parse-ls-files (status default-state
&optional skip-existing
)
537 "Parse the output of git-ls-files in the current buffer."
538 (goto-char (point-min))
540 (while (re-search-forward "\\([HMRCK?]\\) \\([^\0]*\\)\0" nil t
1)
541 (let ((state (match-string 1))
542 (name (match-string 2)))
543 (unless (and skip-existing
(git-find-status-file status name
))
544 (push (git-create-fileinfo (or (git-state-code state
) default-state
) name
) infolist
))))
545 (dolist (info (nreverse infolist
))
546 (ewoc-enter-last status info
))))
548 (defun git-parse-ls-unmerged (status)
549 "Parse the output of git-ls-files -u in the current buffer."
550 (goto-char (point-min))
552 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t
)
553 (let ((node (git-find-status-file status
(match-string 1))))
554 (when node
(push (ewoc-data node
) files
))))
555 (git-set-files-state files
'unmerged
)))
557 (defun git-add-status-file (state name
)
558 "Add a new file to the status list (if not existing already) and return its node."
559 (unless git-status
(error "Not in git-status buffer."))
560 (or (git-find-status-file git-status name
)
561 (ewoc-enter-last git-status
(git-create-fileinfo state name
))))
563 (defun git-marked-files ()
564 "Return a list of all marked files, or if none a list containing just the file at cursor position."
565 (unless git-status
(error "Not in git-status buffer."))
566 (or (ewoc-collect git-status
(lambda (info) (git-fileinfo->marked info
)))
567 (list (ewoc-data (ewoc-locate git-status
)))))
569 (defun git-marked-files-state (&rest states
)
570 "Return marked files that are in the specified states."
571 (let ((files (git-marked-files))
574 (when (memq (git-fileinfo->state info
) states
)
578 (defun git-refresh-files ()
579 "Refresh all files that need it and clear the needs-refresh flag."
580 (unless git-status
(error "Not in git-status buffer."))
583 (let ((refresh (git-fileinfo->needs-refresh info
)))
584 (setf (git-fileinfo->needs-refresh info
) nil
)
587 ; move back to goal column
588 (when goal-column
(move-to-column goal-column
)))
590 (defun git-refresh-ewoc-hf (status)
591 "Refresh the ewoc header and footer."
592 (let ((branch (git-symbolic-ref "HEAD"))
593 (head (if (git-empty-db-p) "Nothing committed yet"
594 (git-get-commit-description "HEAD")))
595 (merge-heads (git-get-merge-heads)))
597 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
599 (if (string-match "^refs/heads/" branch
)
600 (substring branch
(match-end 0))
604 (concat "\nMerging: "
605 (mapconcat (lambda (str) (git-get-commit-description str
)) merge-heads
"\n "))
607 (if (ewoc-nth status
0) "" " No changes."))))
609 (defun git-get-filenames (files)
610 (mapcar (lambda (info) (git-fileinfo->name info
)) files
))
612 (defun git-update-index (index-file files
)
613 "Run git-update-index on a list of files."
614 (let ((env (and index-file
`(("GIT_INDEX_FILE" .
,index-file
))))
615 added deleted modified
)
617 (case (git-fileinfo->state info
)
618 ('added
(push info added
))
619 ('deleted
(push info deleted
))
620 ('modified
(push info modified
))))
622 (apply #'git-run-command nil env
"update-index" "--add" "--" (git-get-filenames added
)))
624 (apply #'git-run-command nil env
"update-index" "--remove" "--" (git-get-filenames deleted
)))
626 (apply #'git-run-command nil env
"update-index" "--" (git-get-filenames modified
)))))
628 (defun git-run-pre-commit-hook ()
629 "Run the pre-commit hook if any."
630 (unless git-status
(error "Not in git-status buffer."))
631 (let ((files (git-marked-files-state 'added
'deleted
'modified
)))
633 (not (file-executable-p ".git/hooks/pre-commit"))
634 (let ((index-file (make-temp-file "gitidx")))
636 (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}"))))
637 (git-read-tree head-tree index-file
)
638 (git-update-index index-file files
)
639 (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" .
,index-file
))))
640 (delete-file index-file
))))))
642 (defun git-do-commit ()
643 "Perform the actual commit using the current buffer as log message."
645 (let ((buffer (current-buffer))
646 (index-file (make-temp-file "gitidx")))
647 (with-current-buffer log-edit-parent-buffer
648 (if (git-marked-files-state 'unmerged
)
649 (message "You cannot commit unmerged files, resolve them first.")
651 (let ((files (git-marked-files-state 'added
'deleted
'modified
))
653 (unless (git-empty-db-p)
654 (setq head
(git-rev-parse "HEAD")
655 head-tree
(git-rev-parse "HEAD^{tree}")))
658 (git-read-tree head-tree index-file
)
659 (git-update-index nil files
) ;update both the default index
660 (git-update-index index-file files
) ;and the temporary one
661 (let ((tree (git-write-tree index-file
)))
662 (if (or (not (string-equal tree head-tree
))
663 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
664 (let ((commit (git-commit-tree buffer tree head
)))
665 (git-update-ref "HEAD" commit head
)
666 (condition-case nil
(delete-file ".git/MERGE_HEAD") (error nil
))
667 (condition-case nil
(delete-file ".git/MERGE_MSG") (error nil
))
668 (with-current-buffer buffer
(erase-buffer))
669 (git-set-files-state files
'uptodate
)
670 (when (file-directory-p ".git/rr-cache")
671 (git-run-command nil nil
"rerere"))
673 (git-refresh-ewoc-hf git-status
)
674 (message "Committed %s." commit
)
675 (git-run-hook "post-commit" nil
))
676 (message "Commit aborted."))))
677 (message "No files to commit.")))
678 (delete-file index-file
))))))
681 ;;;; Interactive functions
682 ;;;; ------------------------------------------------------------
684 (defun git-mark-file ()
685 "Mark the file that the cursor is on and move to the next one."
687 (unless git-status
(error "Not in git-status buffer."))
688 (let* ((pos (ewoc-locate git-status
))
689 (info (ewoc-data pos
)))
690 (setf (git-fileinfo->marked info
) t
)
691 (ewoc-invalidate git-status pos
)
692 (ewoc-goto-next git-status
1)))
694 (defun git-unmark-file ()
695 "Unmark the file that the cursor is on and move to the next one."
697 (unless git-status
(error "Not in git-status buffer."))
698 (let* ((pos (ewoc-locate git-status
))
699 (info (ewoc-data pos
)))
700 (setf (git-fileinfo->marked info
) nil
)
701 (ewoc-invalidate git-status pos
)
702 (ewoc-goto-next git-status
1)))
704 (defun git-unmark-file-up ()
705 "Unmark the file that the cursor is on and move to the previous one."
707 (unless git-status
(error "Not in git-status buffer."))
708 (let* ((pos (ewoc-locate git-status
))
709 (info (ewoc-data pos
)))
710 (setf (git-fileinfo->marked info
) nil
)
711 (ewoc-invalidate git-status pos
)
712 (ewoc-goto-prev git-status
1)))
714 (defun git-mark-all ()
717 (unless git-status
(error "Not in git-status buffer."))
718 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) t
) t
) git-status
)
719 ; move back to goal column after invalidate
720 (when goal-column
(move-to-column goal-column
)))
722 (defun git-unmark-all ()
725 (unless git-status
(error "Not in git-status buffer."))
726 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) nil
) t
) git-status
)
727 ; move back to goal column after invalidate
728 (when goal-column
(move-to-column goal-column
)))
730 (defun git-toggle-all-marks ()
731 "Toggle all file marks."
733 (unless git-status
(error "Not in git-status buffer."))
734 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) (not (git-fileinfo->marked info
))) t
) git-status
)
735 ; move back to goal column after invalidate
736 (when goal-column
(move-to-column goal-column
)))
738 (defun git-next-file (&optional n
)
739 "Move the selection down N files."
741 (unless git-status
(error "Not in git-status buffer."))
742 (ewoc-goto-next git-status n
))
744 (defun git-prev-file (&optional n
)
745 "Move the selection up N files."
747 (unless git-status
(error "Not in git-status buffer."))
748 (ewoc-goto-prev git-status n
))
750 (defun git-next-unmerged-file (&optional n
)
751 "Move the selection down N unmerged files."
753 (unless git-status
(error "Not in git-status buffer."))
754 (let* ((last (ewoc-locate git-status
))
755 (node (ewoc-next git-status last
)))
756 (while (and node
(> n
0))
757 (when (eq 'unmerged
(git-fileinfo->state
(ewoc-data node
)))
760 (setq node
(ewoc-next git-status node
)))
761 (ewoc-goto-node git-status last
)))
763 (defun git-prev-unmerged-file (&optional n
)
764 "Move the selection up N unmerged files."
766 (unless git-status
(error "Not in git-status buffer."))
767 (let* ((last (ewoc-locate git-status
))
768 (node (ewoc-prev git-status last
)))
769 (while (and node
(> n
0))
770 (when (eq 'unmerged
(git-fileinfo->state
(ewoc-data node
)))
773 (setq node
(ewoc-prev git-status node
)))
774 (ewoc-goto-node git-status last
)))
776 (defun git-add-file ()
777 "Add marked file(s) to the index cache."
779 (let ((files (git-marked-files-state 'unknown
)))
782 (git-add-status-file 'added
(file-relative-name
783 (read-file-name "File to add: " nil nil t
))))
785 (apply #'git-run-command nil nil
"update-index" "--info-only" "--add" "--" (git-get-filenames files
))
786 (git-set-files-state files
'added
)
787 (git-refresh-files)))
789 (defun git-ignore-file ()
790 "Add marked file(s) to the ignore list."
792 (let ((files (git-marked-files-state 'unknown
)))
795 (git-add-status-file 'unknown
(file-relative-name
796 (read-file-name "File to ignore: " nil nil t
))))
798 (dolist (info files
) (git-append-to-ignore (git-fileinfo->name info
)))
799 (git-set-files-state files
'ignored
)
800 (git-refresh-files)))
802 (defun git-remove-file ()
803 "Remove the marked file(s)."
805 (let ((files (git-marked-files-state 'added
'modified
'unknown
'uptodate
)))
808 (git-add-status-file 'unknown
(file-relative-name
809 (read-file-name "File to remove: " nil nil t
))))
812 (format "Remove %d file%s? " (length files
) (if (> (length files
) 1) "s" "")))
815 (let ((name (git-fileinfo->name info
)))
816 (when (file-exists-p name
) (delete-file name
))))
817 (apply #'git-run-command nil nil
"update-index" "--info-only" "--remove" "--" (git-get-filenames files
))
818 ; remove unknown files from the list, set the others to deleted
819 (ewoc-filter git-status
821 (not (and (memq info files
) (eq (git-fileinfo->state info
) 'unknown
))))
823 (git-set-files-state files
'deleted
)
825 (unless (ewoc-nth git-status
0) ; refresh header if list is empty
826 (git-refresh-ewoc-hf git-status
)))
827 (message "Aborting"))))
829 (defun git-revert-file ()
830 "Revert changes to the marked file(s)."
832 (let ((files (git-marked-files))
836 (format "Revert %d file%s? " (length files
) (if (> (length files
) 1) "s" ""))))
838 (case (git-fileinfo->state info
)
839 ('added
(push info added
))
840 ('deleted
(push info modified
))
841 ('unmerged
(push info modified
))
842 ('modified
(push info modified
))))
844 (apply #'git-run-command nil nil
"update-index" "--force-remove" "--" (git-get-filenames added
))
845 (git-set-files-state added
'unknown
))
847 (apply #'git-run-command nil nil
"checkout" "HEAD" (git-get-filenames modified
))
848 (git-set-files-state modified
'uptodate
))
849 (git-refresh-files))))
851 (defun git-resolve-file ()
852 "Resolve conflicts in marked file(s)."
854 (let ((files (git-marked-files-state 'unmerged
)))
856 (apply #'git-run-command nil nil
"update-index" "--" (git-get-filenames files
))
857 (git-set-files-state files
'modified
)
858 (git-refresh-files))))
860 (defun git-remove-handled ()
861 "Remove handled files from the status list."
863 (ewoc-filter git-status
865 (not (or (eq (git-fileinfo->state info
) 'ignored
)
866 (eq (git-fileinfo->state info
) 'uptodate
)))))
867 (unless (ewoc-nth git-status
0) ; refresh header if list is empty
868 (git-refresh-ewoc-hf git-status
)))
870 (defun git-setup-diff-buffer (buffer)
871 "Setup a buffer for displaying a diff."
872 (with-current-buffer buffer
874 (goto-char (point-min))
875 (setq buffer-read-only t
))
876 (display-buffer buffer
)
877 (shrink-window-if-larger-than-buffer))
879 (defun git-diff-file ()
880 "Diff the marked file(s) against HEAD."
882 (let ((files (git-marked-files)))
883 (git-setup-diff-buffer
884 (apply #'git-run-command-buffer
"*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files
)))))
886 (defun git-diff-file-merge-head (arg)
887 "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)."
889 (let ((files (git-marked-files))
890 (merge-heads (git-get-merge-heads)))
891 (unless merge-heads
(error "No merge in progress"))
892 (git-setup-diff-buffer
893 (apply #'git-run-command-buffer
"*git-diff*" "diff-index" "-p" "-M"
894 (or (nth (1- arg
) merge-heads
) "HEAD") "--" (git-get-filenames files
)))))
896 (defun git-diff-unmerged-file (stage)
897 "Diff the marked unmerged file(s) against the specified stage."
898 (let ((files (git-marked-files)))
899 (git-setup-diff-buffer
900 (apply #'git-run-command-buffer
"*git-diff*" "diff-files" "-p" stage
"--" (git-get-filenames files
)))))
902 (defun git-diff-file-base ()
903 "Diff the marked unmerged file(s) against the common base file."
905 (git-diff-unmerged-file "-1"))
907 (defun git-diff-file-mine ()
908 "Diff the marked unmerged file(s) against my pre-merge version."
910 (git-diff-unmerged-file "-2"))
912 (defun git-diff-file-other ()
913 "Diff the marked unmerged file(s) against the other's pre-merge version."
915 (git-diff-unmerged-file "-3"))
917 (defun git-diff-file-combined ()
918 "Do a combined diff of the marked unmerged file(s)."
920 (git-diff-unmerged-file "-c"))
922 (defun git-diff-file-idiff ()
923 "Perform an interactive diff on the current file."
925 (error "Interactive diffs not implemented yet."))
927 (defun git-log-file ()
928 "Display a log of changes to the marked file(s)."
930 (let* ((files (git-marked-files))
931 (coding-system-for-read git-commits-coding-system
)
932 (buffer (apply #'git-run-command-buffer
"*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files
))))
933 (with-current-buffer buffer
934 ; (git-log-mode) FIXME: implement log mode
935 (goto-char (point-min))
936 (setq buffer-read-only t
))
937 (display-buffer buffer
)))
939 (defun git-log-edit-files ()
940 "Return a list of marked files for use in the log-edit buffer."
941 (with-current-buffer log-edit-parent-buffer
942 (git-get-filenames (git-marked-files-state 'added
'deleted
'modified
))))
944 (defun git-append-sign-off (name email
)
945 "Append a Signed-off-by entry to the current buffer, avoiding duplicates."
946 (let ((sign-off (format "Signed-off-by: %s <%s>" name email
))
947 (case-fold-search t
))
948 (goto-char (point-min))
949 (unless (re-search-forward (concat "^" (regexp-quote sign-off
)) nil t
)
950 (goto-char (point-min))
951 (unless (re-search-forward "^Signed-off-by: " nil t
)
952 (setq sign-off
(concat "\n" sign-off
)))
953 (goto-char (point-max))
954 (insert sign-off
"\n"))))
956 (defun git-setup-log-buffer (buffer &optional author-name author-email subject date msg
)
957 "Setup the log buffer for a commit."
958 (unless git-status
(error "Not in git-status buffer."))
959 (let ((merge-heads (git-get-merge-heads))
960 (dir default-directory
)
961 (committer-name (git-get-committer-name))
962 (committer-email (git-get-committer-email))
963 (sign-off git-append-signed-off-by
))
964 (with-current-buffer buffer
969 (format "Author: %s <%s>\n%s%s"
970 (or author-name committer-name
)
971 (or author-email committer-email
)
972 (if date
(format "Date: %s\n" date
) "")
974 (format "Parent: %s\n%s\n"
975 (git-rev-parse "HEAD")
976 (mapconcat (lambda (str) (concat "Parent: " str
)) merge-heads
"\n"))
978 'face
'git-header-face
)
979 (propertize git-log-msg-separator
'face
'git-separator-face
)
981 (when subject
(insert subject
"\n\n"))
982 (cond (msg (insert msg
"\n"))
983 ((file-readable-p ".dotest/msg")
984 (insert-file-contents ".dotest/msg"))
985 ((file-readable-p ".git/MERGE_MSG")
986 (insert-file-contents ".git/MERGE_MSG")))
987 ; delete empty lines at end
988 (goto-char (point-min))
989 (when (re-search-forward "\n+\\'" nil t
)
990 (replace-match "\n" t t
))
991 (when sign-off
(git-append-sign-off committer-name committer-email
)))))
993 (defun git-commit-file ()
994 "Commit the marked file(s), asking for a commit message."
996 (unless git-status
(error "Not in git-status buffer."))
997 (when (git-run-pre-commit-hook)
998 (let ((buffer (get-buffer-create "*git-commit*"))
999 (coding-system (git-get-commits-coding-system))
1000 author-name author-email subject date
)
1001 (when (eq 0 (buffer-size buffer
))
1002 (when (file-readable-p ".dotest/info")
1004 (insert-file-contents ".dotest/info")
1005 (goto-char (point-min))
1006 (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t
)
1007 (setq author-name
(match-string 1))
1008 (setq author-email
(match-string 2)))
1009 (goto-char (point-min))
1010 (when (re-search-forward "^Subject: \\(.*\\)$" nil t
)
1011 (setq subject
(match-string 1)))
1012 (goto-char (point-min))
1013 (when (re-search-forward "^Date: \\(.*\\)$" nil t
)
1014 (setq date
(match-string 1)))))
1015 (git-setup-log-buffer buffer author-name author-email subject date
))
1016 (log-edit #'git-do-commit nil
#'git-log-edit-files buffer
)
1017 (setq font-lock-keywords
(font-lock-compile-keywords git-log-edit-font-lock-keywords
))
1018 (setq buffer-file-coding-system coding-system
)
1019 (re-search-forward (regexp-quote (concat git-log-msg-separator
"\n")) nil t
))))
1021 (defun git-find-file ()
1022 "Visit the current file in its own buffer."
1024 (unless git-status
(error "Not in git-status buffer."))
1025 (let ((info (ewoc-data (ewoc-locate git-status
))))
1026 (find-file (git-fileinfo->name info
))
1027 (when (eq 'unmerged
(git-fileinfo->state info
))
1030 (defun git-find-file-other-window ()
1031 "Visit the current file in its own buffer in another window."
1033 (unless git-status
(error "Not in git-status buffer."))
1034 (let ((info (ewoc-data (ewoc-locate git-status
))))
1035 (find-file-other-window (git-fileinfo->name info
))
1036 (when (eq 'unmerged
(git-fileinfo->state info
))
1039 (defun git-find-file-imerge ()
1040 "Visit the current file in interactive merge mode."
1042 (unless git-status
(error "Not in git-status buffer."))
1043 (let ((info (ewoc-data (ewoc-locate git-status
))))
1044 (find-file (git-fileinfo->name info
))
1047 (defun git-view-file ()
1048 "View the current file in its own buffer."
1050 (unless git-status
(error "Not in git-status buffer."))
1051 (let ((info (ewoc-data (ewoc-locate git-status
))))
1052 (view-file (git-fileinfo->name info
))))
1054 (defun git-refresh-status ()
1055 "Refresh the git status buffer."
1057 (let* ((status git-status
)
1058 (pos (ewoc-locate status
))
1059 (cur-name (and pos
(git-fileinfo->name
(ewoc-data pos
)))))
1060 (unless status
(error "Not in git-status buffer."))
1061 (git-clear-status status
)
1062 (git-run-command nil nil
"update-index" "--info-only" "--refresh")
1063 (if (git-empty-db-p)
1064 ; we need some special handling for an empty db
1066 (git-run-command t nil
"ls-files" "-z" "-t" "-c")
1067 (git-parse-ls-files status
'added
))
1069 (git-run-command t nil
"diff-index" "-z" "-M" "HEAD")
1070 (git-parse-status status
)))
1072 (git-run-command t nil
"ls-files" "-z" "-u")
1073 (git-parse-ls-unmerged status
))
1074 (when (file-readable-p ".git/info/exclude")
1076 (git-run-command t nil
"ls-files" "-z" "-t" "-o"
1077 "--exclude-from=.git/info/exclude"
1078 (concat "--exclude-per-directory=" git-per-dir-ignore-file
))
1079 (git-parse-ls-files status
'unknown
)))
1081 (git-refresh-ewoc-hf status
)
1082 ; move point to the current file name if any
1083 (let ((node (and cur-name
(git-find-status-file status cur-name
))))
1084 (when node
(ewoc-goto-node status node
)))))
1086 (defun git-status-quit ()
1087 "Quit git-status mode."
1092 ;;;; ------------------------------------------------------------
1094 (defvar git-status-mode-hook nil
1095 "Run after `git-status-mode' is setup.")
1097 (defvar git-status-mode-map nil
1098 "Keymap for git major mode.")
1100 (defvar git-status nil
1101 "List of all files managed by the git-status mode.")
1103 (unless git-status-mode-map
1104 (let ((map (make-keymap))
1105 (diff-map (make-sparse-keymap)))
1106 (suppress-keymap map
)
1107 (define-key map
"?" 'git-help
)
1108 (define-key map
"h" 'git-help
)
1109 (define-key map
" " 'git-next-file
)
1110 (define-key map
"a" 'git-add-file
)
1111 (define-key map
"c" 'git-commit-file
)
1112 (define-key map
"d" diff-map
)
1113 (define-key map
"=" 'git-diff-file
)
1114 (define-key map
"f" 'git-find-file
)
1115 (define-key map
"\r" 'git-find-file
)
1116 (define-key map
"g" 'git-refresh-status
)
1117 (define-key map
"i" 'git-ignore-file
)
1118 (define-key map
"l" 'git-log-file
)
1119 (define-key map
"m" 'git-mark-file
)
1120 (define-key map
"M" 'git-mark-all
)
1121 (define-key map
"n" 'git-next-file
)
1122 (define-key map
"N" 'git-next-unmerged-file
)
1123 (define-key map
"o" 'git-find-file-other-window
)
1124 (define-key map
"p" 'git-prev-file
)
1125 (define-key map
"P" 'git-prev-unmerged-file
)
1126 (define-key map
"q" 'git-status-quit
)
1127 (define-key map
"r" 'git-remove-file
)
1128 (define-key map
"R" 'git-resolve-file
)
1129 (define-key map
"T" 'git-toggle-all-marks
)
1130 (define-key map
"u" 'git-unmark-file
)
1131 (define-key map
"U" 'git-revert-file
)
1132 (define-key map
"v" 'git-view-file
)
1133 (define-key map
"x" 'git-remove-handled
)
1134 (define-key map
"\C-?" 'git-unmark-file-up
)
1135 (define-key map
"\M-\C-?" 'git-unmark-all
)
1137 (define-key diff-map
"b" 'git-diff-file-base
)
1138 (define-key diff-map
"c" 'git-diff-file-combined
)
1139 (define-key diff-map
"=" 'git-diff-file
)
1140 (define-key diff-map
"e" 'git-diff-file-idiff
)
1141 (define-key diff-map
"E" 'git-find-file-imerge
)
1142 (define-key diff-map
"h" 'git-diff-file-merge-head
)
1143 (define-key diff-map
"m" 'git-diff-file-mine
)
1144 (define-key diff-map
"o" 'git-diff-file-other
)
1145 (setq git-status-mode-map map
)))
1147 ;; git mode should only run in the *git status* buffer
1148 (put 'git-status-mode
'mode-class
'special
)
1150 (defun git-status-mode ()
1151 "Major mode for interacting with Git.
1153 \\{git-status-mode-map}"
1154 (kill-all-local-variables)
1155 (buffer-disable-undo)
1156 (setq mode-name
"git status"
1157 major-mode
'git-status-mode
1160 (use-local-map git-status-mode-map
)
1161 (let ((buffer-read-only nil
))
1163 (let ((status (ewoc-create 'git-fileinfo-prettyprint
"" "")))
1164 (set (make-local-variable 'git-status
) status
))
1165 (set (make-local-variable 'list-buffers-directory
) default-directory
)
1166 (run-hooks 'git-status-mode-hook
)))
1168 (defun git-find-status-buffer (dir)
1169 "Find the git status buffer handling a specified directory."
1170 (let ((list (buffer-list))
1171 (fulldir (expand-file-name dir
))
1173 (while (and list
(not found
))
1174 (let ((buffer (car list
)))
1175 (with-current-buffer buffer
1176 (when (and list-buffers-directory
1177 (string-equal fulldir
(expand-file-name list-buffers-directory
))
1178 (string-match "\\*git-status\\*$" (buffer-name buffer
)))
1179 (setq found buffer
))))
1180 (setq list
(cdr list
)))
1183 (defun git-status (dir)
1184 "Entry point into git-status mode."
1185 (interactive "DSelect directory: ")
1186 (setq dir
(git-get-top-dir dir
))
1187 (if (file-directory-p (concat (file-name-as-directory dir
) ".git"))
1188 (let ((buffer (or (and git-reuse-status-buffer
(git-find-status-buffer dir
))
1189 (create-file-buffer (expand-file-name "*git-status*" dir
)))))
1190 (switch-to-buffer buffer
)
1193 (git-refresh-status)
1194 (goto-char (point-min)))
1195 (message "%s is not a git working tree." dir
)))
1198 "Display help for Git mode."
1200 (describe-function 'git-status-mode
))
1203 ;;; git.el ends here