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 ;; - diff against other branch
39 ;; - renaming files from the status buffer
42 ;; - switching branches
44 ;; - git-show-branch browser
48 (eval-when-compile (require 'cl
))
55 ;;;; ------------------------------------------------------------
58 "A user interface for the git versioning system."
61 (defcustom git-committer-name nil
62 "User name to use for commits.
63 The default is to fall back to the repository config,
64 then to `add-log-full-name' and then to `user-full-name'."
66 :type
'(choice (const :tag
"Default" nil
)
67 (string :tag
"Name")))
69 (defcustom git-committer-email nil
70 "Email address to use for commits.
71 The default is to fall back to the git repository config,
72 then to `add-log-mailing-address' and then to `user-mail-address'."
74 :type
'(choice (const :tag
"Default" nil
)
75 (string :tag
"Email")))
77 (defcustom git-commits-coding-system nil
78 "Default coding system for the log message of git commits."
80 :type
'(choice (const :tag
"From repository config" nil
)
83 (defcustom git-append-signed-off-by nil
84 "Whether to append a Signed-off-by line to the commit message before editing."
88 (defcustom git-reuse-status-buffer t
89 "Whether `git-status' should try to reuse an existing buffer
90 if there is already one that displays the same directory."
94 (defcustom git-per-dir-ignore-file
".gitignore"
95 "Name of the per-directory ignore file."
99 (defcustom git-show-uptodate nil
100 "Whether to display up-to-date files."
104 (defcustom git-show-ignored nil
105 "Whether to display ignored files."
109 (defcustom git-show-unknown t
110 "Whether to display unknown files."
115 (defface git-status-face
116 '((((class color
) (background light
)) (:foreground
"purple"))
117 (((class color
) (background dark
)) (:foreground
"salmon")))
118 "Git mode face used to highlight added and modified files."
121 (defface git-unmerged-face
122 '((((class color
) (background light
)) (:foreground
"red" :bold t
))
123 (((class color
) (background dark
)) (:foreground
"red" :bold t
)))
124 "Git mode face used to highlight unmerged files."
127 (defface git-unknown-face
128 '((((class color
) (background light
)) (:foreground
"goldenrod" :bold t
))
129 (((class color
) (background dark
)) (:foreground
"goldenrod" :bold t
)))
130 "Git mode face used to highlight unknown files."
133 (defface git-uptodate-face
134 '((((class color
) (background light
)) (:foreground
"grey60"))
135 (((class color
) (background dark
)) (:foreground
"grey40")))
136 "Git mode face used to highlight up-to-date files."
139 (defface git-ignored-face
140 '((((class color
) (background light
)) (:foreground
"grey60"))
141 (((class color
) (background dark
)) (:foreground
"grey40")))
142 "Git mode face used to highlight ignored files."
145 (defface git-mark-face
146 '((((class color
) (background light
)) (:foreground
"red" :bold t
))
147 (((class color
) (background dark
)) (:foreground
"tomato" :bold t
)))
148 "Git mode face used for the file marks."
151 (defface git-header-face
152 '((((class color
) (background light
)) (:foreground
"blue"))
153 (((class color
) (background dark
)) (:foreground
"blue")))
154 "Git mode face used for commit headers."
157 (defface git-separator-face
158 '((((class color
) (background light
)) (:foreground
"brown"))
159 (((class color
) (background dark
)) (:foreground
"brown")))
160 "Git mode face used for commit separator."
163 (defface git-permission-face
164 '((((class color
) (background light
)) (:foreground
"green" :bold t
))
165 (((class color
) (background dark
)) (:foreground
"green" :bold t
)))
166 "Git mode face used for permission changes."
171 ;;;; ------------------------------------------------------------
173 (defconst git-log-msg-separator
"--- log message follows this line ---")
175 (defvar git-log-edit-font-lock-keywords
176 `(("^\\(Author:\\|Date:\\|Parent:\\|Signed-off-by:\\)\\(.*\\)$"
177 (1 font-lock-keyword-face
)
178 (2 font-lock-function-name-face
))
179 (,(concat "^\\(" (regexp-quote git-log-msg-separator
) "\\)$")
180 (1 font-lock-comment-face
))))
182 (defun git-get-env-strings (env)
183 "Build a list of NAME=VALUE strings from a list of environment strings."
184 (mapcar (lambda (entry) (concat (car entry
) "=" (cdr entry
))) env
))
186 (defun git-call-process-env (buffer env
&rest args
)
187 "Wrapper for call-process that sets environment strings."
189 (apply #'call-process
"env" nil buffer nil
190 (append (git-get-env-strings env
) (list "git") args
))
191 (apply #'call-process
"git" nil buffer nil args
)))
193 (defun git-call-process-display-error (&rest args
)
194 "Wrapper for call-process that displays error messages."
195 (let* ((dir default-directory
)
196 (buffer (get-buffer-create "*Git Command Output*"))
197 (ok (with-current-buffer buffer
198 (let ((default-directory dir
)
199 (buffer-read-only nil
))
201 (eq 0 (apply 'call-process
"git" nil
(list buffer t
) nil args
))))))
202 (unless ok
(display-message-or-buffer buffer
))
205 (defun git-call-process-env-string (env &rest args
)
206 "Wrapper for call-process that sets environment strings,
207 and returns the process output as a string."
209 (and (eq 0 (apply #' git-call-process-env t env args
))
212 (defun git-run-process-region (buffer start end program args
)
213 "Run a git process with a buffer region as input."
214 (let ((output-buffer (current-buffer))
215 (dir default-directory
))
216 (with-current-buffer buffer
218 (apply #'call-process-region start end program
219 nil
(list output-buffer nil
) nil args
))))
221 (defun git-run-command-buffer (buffer-name &rest args
)
222 "Run a git command, sending the output to a buffer named BUFFER-NAME."
223 (let ((dir default-directory
)
224 (buffer (get-buffer-create buffer-name
)))
225 (message "Running git %s..." (car args
))
226 (with-current-buffer buffer
227 (let ((default-directory dir
)
228 (buffer-read-only nil
))
230 (apply #'git-call-process-env buffer nil args
)))
231 (message "Running git %s...done" (car args
))
234 (defun git-run-command-region (buffer start end env
&rest args
)
235 "Run a git command with specified buffer region as input."
236 (unless (eq 0 (if env
237 (git-run-process-region
238 buffer start end
"env"
239 (append (git-get-env-strings env
) (list "git") args
))
240 (git-run-process-region
241 buffer start end
"git" args
)))
242 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x
) args
" ") (buffer-string))))
244 (defun git-run-hook (hook env
&rest args
)
245 "Run a git hook and display its output if any."
246 (let ((dir default-directory
)
247 (hook-name (expand-file-name (concat ".git/hooks/" hook
))))
248 (or (not (file-executable-p hook-name
))
249 (let (status (buffer (get-buffer-create "*Git Hook Output*")))
250 (with-current-buffer buffer
255 (apply #'call-process
"env" nil
(list buffer t
) nil
256 (append (git-get-env-strings env
) (list hook-name
) args
))
257 (apply #'call-process hook-name nil
(list buffer t
) nil args
))))
258 (display-message-or-buffer buffer
)
261 (defun git-get-string-sha1 (string)
262 "Read a SHA1 from the specified string."
264 (string-match "[0-9a-f]\\{40\\}" string
)
265 (match-string 0 string
)))
267 (defun git-get-committer-name ()
268 "Return the name to use as GIT_COMMITTER_NAME."
269 ; copied from log-edit
270 (or git-committer-name
271 (git-config "user.name")
272 (and (boundp 'add-log-full-name
) add-log-full-name
)
273 (and (fboundp 'user-full-name
) (user-full-name))
274 (and (boundp 'user-full-name
) user-full-name
)))
276 (defun git-get-committer-email ()
277 "Return the email address to use as GIT_COMMITTER_EMAIL."
278 ; copied from log-edit
279 (or git-committer-email
280 (git-config "user.email")
281 (and (boundp 'add-log-mailing-address
) add-log-mailing-address
)
282 (and (fboundp 'user-mail-address
) (user-mail-address))
283 (and (boundp 'user-mail-address
) user-mail-address
)))
285 (defun git-get-commits-coding-system ()
286 "Return the coding system to use for commits."
287 (let ((repo-config (git-config "i18n.commitencoding")))
288 (or git-commits-coding-system
290 (fboundp 'locale-charset-to-coding-system
)
291 (locale-charset-to-coding-system repo-config
))
294 (defun git-get-logoutput-coding-system ()
295 "Return the coding system used for git-log output."
296 (let ((repo-config (or (git-config "i18n.logoutputencoding")
297 (git-config "i18n.commitencoding"))))
298 (or git-commits-coding-system
300 (fboundp 'locale-charset-to-coding-system
)
301 (locale-charset-to-coding-system repo-config
))
304 (defun git-escape-file-name (name)
305 "Escape a file name if necessary."
306 (if (string-match "[\n\t\"\\]" name
)
308 (mapconcat (lambda (c)
314 (t (char-to-string c
))))
319 (defun git-success-message (text files
)
320 "Print a success message after having handled FILES."
321 (let ((n (length files
)))
323 (message "%s %s" text
(car files
))
324 (message "%s %d files" text n
))))
326 (defun git-get-top-dir (dir)
327 "Retrieve the top-level directory of a git tree."
328 (let ((cdup (with-output-to-string
329 (with-current-buffer standard-output
331 (unless (eq 0 (call-process "git" nil t nil
"rev-parse" "--show-cdup"))
332 (error "cannot find top-level git tree for %s." dir
))))))
333 (expand-file-name (concat (file-name-as-directory dir
)
334 (car (split-string cdup
"\n"))))))
337 (defun git-append-to-ignore (file)
338 "Add a file name to the ignore file in its directory."
339 (let* ((fullname (expand-file-name file
))
340 (dir (file-name-directory fullname
))
341 (name (file-name-nondirectory fullname
))
342 (ignore-name (expand-file-name git-per-dir-ignore-file dir
))
343 (created (not (file-exists-p ignore-name
))))
344 (save-window-excursion
345 (set-buffer (find-file-noselect ignore-name
))
346 (goto-char (point-max))
347 (unless (zerop (current-column)) (insert "\n"))
348 (insert "/" name
"\n")
349 (sort-lines nil
(point-min) (point-max))
352 (git-call-process-env nil nil
"update-index" "--add" "--" (file-relative-name ignore-name
)))
353 (git-update-status-files (list (file-relative-name ignore-name
)) 'unknown
)))
355 ; propertize definition for XEmacs, stolen from erc-compat
357 (unless (fboundp 'propertize
)
358 (defun propertize (string &rest props
)
359 (let ((string (copy-sequence string
)))
361 (put-text-property 0 (length string
) (nth 0 props
) (nth 1 props
) string
)
362 (setq props
(cddr props
)))
365 ;;;; Wrappers for basic git commands
366 ;;;; ------------------------------------------------------------
368 (defun git-rev-parse (rev)
369 "Parse a revision name and return its SHA1."
371 (git-call-process-env-string nil
"rev-parse" rev
)))
373 (defun git-config (key)
374 "Retrieve the value associated to KEY in the git repository config file."
375 (let ((str (git-call-process-env-string nil
"config" key
)))
376 (and str
(car (split-string str
"\n")))))
378 (defun git-symbolic-ref (ref)
379 "Wrapper for the git-symbolic-ref command."
380 (let ((str (git-call-process-env-string nil
"symbolic-ref" ref
)))
381 (and str
(car (split-string str
"\n")))))
383 (defun git-update-ref (ref newval
&optional oldval reason
)
384 "Update a reference by calling git-update-ref."
385 (let ((args (and oldval
(list oldval
))))
391 (apply 'git-call-process-display-error
"update-ref" args
)))
393 (defun git-read-tree (tree &optional index-file
)
394 "Read a tree into the index file."
395 (apply #'git-call-process-env nil
396 (if index-file
`(("GIT_INDEX_FILE" .
,index-file
)) nil
)
397 "read-tree" (if tree
(list tree
))))
399 (defun git-write-tree (&optional index-file
)
400 "Call git-write-tree and return the resulting tree SHA1 as a string."
402 (git-call-process-env-string (and index-file
`(("GIT_INDEX_FILE" .
,index-file
))) "write-tree")))
404 (defun git-commit-tree (buffer tree head
)
405 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
406 (let ((author-name (git-get-committer-name))
407 (author-email (git-get-committer-email))
408 (subject "commit (initial): ")
409 author-date log-start log-end args coding-system-for-write
)
411 (setq subject
"commit: ")
414 (with-current-buffer buffer
415 (goto-char (point-min))
417 (setq log-start
(re-search-forward (concat "^" (regexp-quote git-log-msg-separator
) "\n") nil t
))
419 (narrow-to-region (point-min) log-start
)
420 (goto-char (point-min))
421 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t
)
422 (setq author-name
(match-string 1)
423 author-email
(match-string 2)))
424 (goto-char (point-min))
425 (when (re-search-forward "^Date: +\\(.*\\)$" nil t
)
426 (setq author-date
(match-string 1)))
427 (goto-char (point-min))
428 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t
)
429 (unless (string-equal head
(match-string 1))
430 (setq subject
"commit (merge): ")
432 (push (match-string 1) args
))))
433 (setq log-start
(point-min)))
434 (setq log-end
(point-max))
435 (goto-char log-start
)
436 (when (re-search-forward ".*$" nil t
)
437 (setq subject
(concat subject
(match-string 0))))
438 (setq coding-system-for-write buffer-file-coding-system
))
441 (with-output-to-string
442 (with-current-buffer standard-output
443 (let ((env `(("GIT_AUTHOR_NAME" .
,author-name
)
444 ("GIT_AUTHOR_EMAIL" .
,author-email
)
445 ("GIT_COMMITTER_NAME" .
,(git-get-committer-name))
446 ("GIT_COMMITTER_EMAIL" .
,(git-get-committer-email)))))
447 (when author-date
(push `("GIT_AUTHOR_DATE" .
,author-date
) env
))
448 (apply #'git-run-command-region
449 buffer log-start log-end env
450 "commit-tree" tree
(nreverse args
))))))))
451 (and (git-update-ref "HEAD" commit head subject
)
454 (defun git-empty-db-p ()
455 "Check if the git db is empty (no commit done yet)."
456 (not (eq 0 (call-process "git" nil nil nil
"rev-parse" "--verify" "HEAD"))))
458 (defun git-get-merge-heads ()
459 "Retrieve the merge heads from the MERGE_HEAD file if present."
461 (when (file-readable-p ".git/MERGE_HEAD")
463 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t
)
464 (goto-char (point-min))
465 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t
)
466 (push (match-string 0) heads
))))
469 (defun git-get-commit-description (commit)
470 "Get a one-line description of COMMIT."
471 (let ((coding-system-for-read (git-get-logoutput-coding-system)))
472 (let ((descr (git-call-process-env-string nil
"log" "--max-count=1" "--pretty=oneline" commit
)))
473 (if (and descr
(string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr
))
474 (concat (substring (match-string 1 descr
) 0 10) " - " (match-string 2 descr
))
477 ;;;; File info structure
478 ;;;; ------------------------------------------------------------
480 ; fileinfo structure stolen from pcl-cvs
481 (defstruct (git-fileinfo
483 (:constructor git-create-fileinfo
(state name
&optional old-perm new-perm rename-state orig-name marked
))
484 (:conc-name git-fileinfo-
>))
486 state
;; current state
488 old-perm new-perm
;; permission flags
489 rename-state
;; rename or copy state
490 orig-name
;; original name for renames or copies
491 needs-refresh
) ;; whether file needs to be refreshed
493 (defvar git-status nil
)
495 (defun git-clear-status (status)
496 "Remove everything from the status list."
497 (ewoc-filter status
(lambda (info) nil
)))
499 (defun git-set-fileinfo-state (info state
)
500 "Set the state of a file info."
501 (unless (eq (git-fileinfo->state info
) state
)
502 (setf (git-fileinfo->state info
) state
503 (git-fileinfo->new-perm info
) (git-fileinfo->old-perm info
)
504 (git-fileinfo->rename-state info
) nil
505 (git-fileinfo->orig-name info
) nil
506 (git-fileinfo->needs-refresh info
) t
)))
508 (defun git-status-filenames-map (status func files
&rest args
)
509 "Apply FUNC to the status files names in the FILES list."
511 (setq files
(sort files
#'string-lessp
))
512 (let ((file (pop files
))
513 (node (ewoc-nth status
0)))
514 (while (and file node
)
515 (let ((info (ewoc-data node
)))
516 (if (string-lessp (git-fileinfo->name info
) file
)
517 (setq node
(ewoc-next status node
))
518 (if (string-equal (git-fileinfo->name info
) file
)
519 (apply func info args
))
520 (setq file
(pop files
))))))))
522 (defun git-set-filenames-state (status files state
)
523 "Set the state of a list of named files."
525 (git-status-filenames-map status
#'git-set-fileinfo-state files state
)
526 (unless state
;; delete files whose state has been set to nil
527 (ewoc-filter status
(lambda (info) (git-fileinfo->state info
))))))
529 (defun git-state-code (code)
530 "Convert from a string to a added/deleted/modified state."
531 (case (string-to-char code
)
540 (defun git-status-code-as-string (code)
541 "Format a git status code as string."
543 ('modified
(propertize "Modified" 'face
'git-status-face
))
544 ('unknown
(propertize "Unknown " 'face
'git-unknown-face
))
545 ('added
(propertize "Added " 'face
'git-status-face
))
546 ('deleted
(propertize "Deleted " 'face
'git-status-face
))
547 ('unmerged
(propertize "Unmerged" 'face
'git-unmerged-face
))
548 ('uptodate
(propertize "Uptodate" 'face
'git-uptodate-face
))
549 ('ignored
(propertize "Ignored " 'face
'git-ignored-face
))
552 (defun git-file-type-as-string (old-perm new-perm
)
553 "Return a string describing the file type based on its permissions."
554 (let* ((old-type (lsh (or old-perm
0) -
9))
555 (new-type (lsh (or new-perm
0) -
9))
560 (?
\120 " (type change symlink -> file)")
561 (?
\160 " (type change subproject -> file)")))
564 (?
\100 " (type change file -> symlink)")
565 (?
\160 " (type change subproject -> symlink)")
569 (?
\100 " (type change file -> subproject)")
570 (?
\120 " (type change symlink -> subproject)")
571 (t " (subproject)")))
572 (?
\110 nil
) ;; directory (internal, not a real git state)
573 (?
\000 ;; deleted or unknown
576 (?
\160 " (subproject)")))
577 (t (format " (unknown type %o)" new-type
)))))
578 (cond (str (propertize str
'face
'git-status-face
))
579 ((eq new-type ?
\110) "/")
582 (defun git-rename-as-string (info)
583 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
584 (let ((state (git-fileinfo->rename-state info
)))
588 (if (eq state
'copy
) "copied from "
589 (if (eq (git-fileinfo->state info
) 'added
) "renamed from "
591 (git-escape-file-name (git-fileinfo->orig-name info
))
592 ")") 'face
'git-status-face
)
595 (defun git-permissions-as-string (old-perm new-perm
)
596 "Format a permission change as string."
598 (if (or (not old-perm
)
600 (eq 0 (logand ?
\111 (logxor old-perm new-perm
))))
602 (if (eq 0 (logand ?
\111 old-perm
)) "+x" "-x"))
603 'face
'git-permission-face
))
605 (defun git-fileinfo-prettyprint (info)
606 "Pretty-printer for the git-fileinfo structure."
607 (let ((old-perm (git-fileinfo->old-perm info
))
608 (new-perm (git-fileinfo->new-perm info
)))
609 (insert (concat " " (if (git-fileinfo->marked info
) (propertize "*" 'face
'git-mark-face
) " ")
610 " " (git-status-code-as-string (git-fileinfo->state info
))
611 " " (git-permissions-as-string old-perm new-perm
)
612 " " (git-escape-file-name (git-fileinfo->name info
))
613 (git-file-type-as-string old-perm new-perm
)
614 (git-rename-as-string info
)))))
616 (defun git-insert-info-list (status infolist
)
617 "Insert a list of file infos in the status buffer, replacing existing ones if any."
618 (setq infolist
(sort infolist
619 (lambda (info1 info2
)
620 (string-lessp (git-fileinfo->name info1
)
621 (git-fileinfo->name info2
)))))
622 (let ((info (pop infolist
))
623 (node (ewoc-nth status
0)))
626 (setq node
(ewoc-enter-last status info
))
627 (setq info
(pop infolist
)))
628 ((string-lessp (git-fileinfo->name
(ewoc-data node
))
629 (git-fileinfo->name info
))
630 (setq node
(ewoc-next status node
)))
631 ((string-equal (git-fileinfo->name
(ewoc-data node
))
632 (git-fileinfo->name info
))
633 ;; preserve the marked flag
634 (setf (git-fileinfo->marked info
) (git-fileinfo->marked
(ewoc-data node
)))
635 (setf (git-fileinfo->needs-refresh info
) t
)
636 (setf (ewoc-data node
) info
)
637 (setq info
(pop infolist
)))
639 (setq node
(ewoc-enter-before status node info
))
640 (setq info
(pop infolist
)))))))
642 (defun git-run-diff-index (status files
)
643 "Run git-diff-index on FILES and parse the results into STATUS.
644 Return the list of files that haven't been handled."
645 (let ((remaining (copy-sequence files
))
648 (apply #'git-call-process-env t nil
"diff-index" "-z" "-M" "HEAD" "--" files
)
649 (goto-char (point-min))
650 (while (re-search-forward
651 ":\\([0-7]\\{6\\}\\) \\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\(\\([ADMUT]\\)\0\\([^\0]+\\)\\|\\([CR]\\)[0-9]*\0\\([^\0]+\\)\0\\([^\0]+\\)\\)\0"
653 (let ((old-perm (string-to-number (match-string 1) 8))
654 (new-perm (string-to-number (match-string 2) 8))
655 (state (or (match-string 4) (match-string 6)))
656 (name (or (match-string 5) (match-string 7)))
657 (new-name (match-string 8)))
658 (if new-name
; copy or rename
659 (if (eq ?C
(string-to-char state
))
660 (push (git-create-fileinfo 'added new-name old-perm new-perm
'copy name
) infolist
)
661 (push (git-create-fileinfo 'deleted name
0 0 'rename new-name
) infolist
)
662 (push (git-create-fileinfo 'added new-name old-perm new-perm
'rename name
) infolist
))
663 (push (git-create-fileinfo (git-state-code state
) name old-perm new-perm
) infolist
))
664 (setq remaining
(delete name remaining
))
665 (when new-name
(setq remaining
(delete new-name remaining
))))))
666 (git-insert-info-list status infolist
)
669 (defun git-find-status-file (status file
)
670 "Find a given file in the status ewoc and return its node."
671 (let ((node (ewoc-nth status
0)))
672 (while (and node
(not (string= file
(git-fileinfo->name
(ewoc-data node
)))))
673 (setq node
(ewoc-next status node
)))
676 (defun git-run-ls-files (status files default-state
&rest options
)
677 "Run git-ls-files on FILES and parse the results into STATUS.
678 Return the list of files that haven't been handled."
681 (apply #'git-call-process-env t nil
"ls-files" "-z" (append options
(list "--") files
))
682 (goto-char (point-min))
683 (while (re-search-forward "\\([^\0]*?\\)\\(/?\\)\0" nil t
1)
684 (let ((name (match-string 1)))
685 (push (git-create-fileinfo default-state name
0
686 (if (string-equal "/" (match-string 2)) (lsh ?
\110 9) 0))
688 (setq files
(delete name files
)))))
689 (git-insert-info-list status infolist
)
692 (defun git-run-ls-files-cached (status files default-state
)
693 "Run git-ls-files -c on FILES and parse the results into STATUS.
694 Return the list of files that haven't been handled."
695 (let ((remaining (copy-sequence files
))
698 (apply #'git-call-process-env t nil
"ls-files" "-z" "-s" "-c" "--" files
)
699 (goto-char (point-min))
700 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t
)
701 (let* ((new-perm (string-to-number (match-string 1) 8))
702 (old-perm (if (eq default-state
'added
) 0 new-perm
))
703 (name (match-string 2)))
704 (push (git-create-fileinfo default-state name old-perm new-perm
) infolist
)
705 (setq remaining
(delete name remaining
)))))
706 (git-insert-info-list status infolist
)
709 (defun git-run-ls-unmerged (status files
)
710 "Run git-ls-files -u on FILES and parse the results into STATUS."
712 (apply #'git-call-process-env t nil
"ls-files" "-z" "-u" "--" files
)
713 (goto-char (point-min))
714 (let (unmerged-files)
715 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t
)
716 (push (match-string 1) unmerged-files
))
717 (git-set-filenames-state status unmerged-files
'unmerged
))))
719 (defun git-get-exclude-files ()
720 "Get the list of exclude files to pass to git-ls-files."
722 (config (git-config "core.excludesfile")))
723 (when (file-readable-p ".git/info/exclude")
724 (push ".git/info/exclude" files
))
725 (when (and config
(file-readable-p config
))
729 (defun git-run-ls-files-with-excludes (status files default-state
&rest options
)
730 "Run git-ls-files on FILES with appropriate --exclude-from options."
731 (let ((exclude-files (git-get-exclude-files)))
732 (apply #'git-run-ls-files status files default-state
"--directory"
733 (concat "--exclude-per-directory=" git-per-dir-ignore-file
)
734 (append options
(mapcar (lambda (f) (concat "--exclude-from=" f
)) exclude-files
)))))
736 (defun git-update-status-files (files &optional default-state
)
737 "Update the status of FILES from the index."
738 (unless git-status
(error "Not in git-status buffer."))
739 (when (or git-show-uptodate files
)
740 (git-run-ls-files-cached git-status files
'uptodate
))
741 (let* ((remaining-files
742 (if (git-empty-db-p) ; we need some special handling for an empty db
743 (git-run-ls-files-cached git-status files
'added
)
744 (git-run-diff-index git-status files
))))
745 (git-run-ls-unmerged git-status files
)
746 (when (or remaining-files
(and git-show-unknown
(not files
)))
747 (setq remaining-files
(git-run-ls-files-with-excludes git-status remaining-files
'unknown
"-o")))
748 (when (or remaining-files
(and git-show-ignored
(not files
)))
749 (setq remaining-files
(git-run-ls-files-with-excludes git-status remaining-files
'ignored
"-o" "-i")))
750 (git-set-filenames-state git-status remaining-files default-state
)
752 (git-refresh-ewoc-hf git-status
)))
754 (defun git-mark-files (status files
)
755 "Mark all the specified FILES, and unmark the others."
756 (setq files
(sort files
#'string-lessp
))
757 (let ((file (and files
(pop files
)))
758 (node (ewoc-nth status
0)))
760 (let ((info (ewoc-data node
)))
761 (if (and file
(string-equal (git-fileinfo->name info
) file
))
763 (unless (git-fileinfo->marked info
)
764 (setf (git-fileinfo->marked info
) t
)
765 (setf (git-fileinfo->needs-refresh info
) t
))
766 (setq file
(pop files
))
767 (setq node
(ewoc-next status node
)))
768 (when (git-fileinfo->marked info
)
769 (setf (git-fileinfo->marked info
) nil
)
770 (setf (git-fileinfo->needs-refresh info
) t
))
771 (if (and file
(string-lessp file
(git-fileinfo->name info
)))
772 (setq file
(pop files
))
773 (setq node
(ewoc-next status node
))))))))
775 (defun git-marked-files ()
776 "Return a list of all marked files, or if none a list containing just the file at cursor position."
777 (unless git-status
(error "Not in git-status buffer."))
778 (or (ewoc-collect git-status
(lambda (info) (git-fileinfo->marked info
)))
779 (list (ewoc-data (ewoc-locate git-status
)))))
781 (defun git-marked-files-state (&rest states
)
782 "Return marked files that are in the specified states."
783 (let ((files (git-marked-files))
786 (when (memq (git-fileinfo->state info
) states
)
790 (defun git-refresh-files ()
791 "Refresh all files that need it and clear the needs-refresh flag."
792 (unless git-status
(error "Not in git-status buffer."))
795 (let ((refresh (git-fileinfo->needs-refresh info
)))
796 (setf (git-fileinfo->needs-refresh info
) nil
)
799 ; move back to goal column
800 (when goal-column
(move-to-column goal-column
)))
802 (defun git-refresh-ewoc-hf (status)
803 "Refresh the ewoc header and footer."
804 (let ((branch (git-symbolic-ref "HEAD"))
805 (head (if (git-empty-db-p) "Nothing committed yet"
806 (git-get-commit-description "HEAD")))
807 (merge-heads (git-get-merge-heads)))
809 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
812 (if (string-match "^refs/heads/" branch
)
813 (substring branch
(match-end 0))
815 "none (detached HEAD)")
818 (concat "\nMerging: "
819 (mapconcat (lambda (str) (git-get-commit-description str
)) merge-heads
"\n "))
821 (if (ewoc-nth status
0) "" " No changes."))))
823 (defun git-get-filenames (files)
824 (mapcar (lambda (info) (git-fileinfo->name info
)) files
))
826 (defun git-update-index (index-file files
)
827 "Run git-update-index on a list of files."
828 (let ((env (and index-file
`(("GIT_INDEX_FILE" .
,index-file
))))
829 added deleted modified
)
831 (case (git-fileinfo->state info
)
832 ('added
(push info added
))
833 ('deleted
(push info deleted
))
834 ('modified
(push info modified
))))
836 (apply #'git-call-process-env nil env
"update-index" "--add" "--" (git-get-filenames added
)))
838 (apply #'git-call-process-env nil env
"update-index" "--remove" "--" (git-get-filenames deleted
)))
840 (apply #'git-call-process-env nil env
"update-index" "--" (git-get-filenames modified
)))))
842 (defun git-run-pre-commit-hook ()
843 "Run the pre-commit hook if any."
844 (unless git-status
(error "Not in git-status buffer."))
845 (let ((files (git-marked-files-state 'added
'deleted
'modified
)))
847 (not (file-executable-p ".git/hooks/pre-commit"))
848 (let ((index-file (make-temp-file "gitidx")))
850 (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}"))))
851 (git-read-tree head-tree index-file
)
852 (git-update-index index-file files
)
853 (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" .
,index-file
))))
854 (delete-file index-file
))))))
856 (defun git-do-commit ()
857 "Perform the actual commit using the current buffer as log message."
859 (let ((buffer (current-buffer))
860 (index-file (make-temp-file "gitidx")))
861 (with-current-buffer log-edit-parent-buffer
862 (if (git-marked-files-state 'unmerged
)
863 (message "You cannot commit unmerged files, resolve them first.")
865 (let ((files (git-marked-files-state 'added
'deleted
'modified
))
867 (unless (git-empty-db-p)
868 (setq head
(git-rev-parse "HEAD")
869 head-tree
(git-rev-parse "HEAD^{tree}")))
872 (message "Running git commit...")
873 (git-read-tree head-tree index-file
)
874 (git-update-index nil files
) ;update both the default index
875 (git-update-index index-file files
) ;and the temporary one
876 (let ((tree (git-write-tree index-file
)))
877 (if (or (not (string-equal tree head-tree
))
878 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
879 (let ((commit (git-commit-tree buffer tree head
)))
881 (condition-case nil
(delete-file ".git/MERGE_HEAD") (error nil
))
882 (condition-case nil
(delete-file ".git/MERGE_MSG") (error nil
))
883 (with-current-buffer buffer
(erase-buffer))
884 (git-update-status-files (git-get-filenames files
) 'uptodate
)
885 (git-call-process-env nil nil
"rerere")
886 (git-call-process-env nil nil
"gc" "--auto")
888 (git-refresh-ewoc-hf git-status
)
889 (message "Committed %s." commit
)
890 (git-run-hook "post-commit" nil
)))
891 (message "Commit aborted."))))
892 (message "No files to commit.")))
893 (delete-file index-file
))))))
896 ;;;; Interactive functions
897 ;;;; ------------------------------------------------------------
899 (defun git-mark-file ()
900 "Mark the file that the cursor is on and move to the next one."
902 (unless git-status
(error "Not in git-status buffer."))
903 (let* ((pos (ewoc-locate git-status
))
904 (info (ewoc-data pos
)))
905 (setf (git-fileinfo->marked info
) t
)
906 (ewoc-invalidate git-status pos
)
907 (ewoc-goto-next git-status
1)))
909 (defun git-unmark-file ()
910 "Unmark the file that the cursor is on and move to the next one."
912 (unless git-status
(error "Not in git-status buffer."))
913 (let* ((pos (ewoc-locate git-status
))
914 (info (ewoc-data pos
)))
915 (setf (git-fileinfo->marked info
) nil
)
916 (ewoc-invalidate git-status pos
)
917 (ewoc-goto-next git-status
1)))
919 (defun git-unmark-file-up ()
920 "Unmark the file that the cursor is on and move to the previous one."
922 (unless git-status
(error "Not in git-status buffer."))
923 (let* ((pos (ewoc-locate git-status
))
924 (info (ewoc-data pos
)))
925 (setf (git-fileinfo->marked info
) nil
)
926 (ewoc-invalidate git-status pos
)
927 (ewoc-goto-prev git-status
1)))
929 (defun git-mark-all ()
932 (unless git-status
(error "Not in git-status buffer."))
933 (ewoc-map (lambda (info) (unless (git-fileinfo->marked info
)
934 (setf (git-fileinfo->marked info
) t
))) git-status
)
935 ; move back to goal column after invalidate
936 (when goal-column
(move-to-column goal-column
)))
938 (defun git-unmark-all ()
941 (unless git-status
(error "Not in git-status buffer."))
942 (ewoc-map (lambda (info) (when (git-fileinfo->marked info
)
943 (setf (git-fileinfo->marked info
) nil
)
945 ; move back to goal column after invalidate
946 (when goal-column
(move-to-column goal-column
)))
948 (defun git-toggle-all-marks ()
949 "Toggle all file marks."
951 (unless git-status
(error "Not in git-status buffer."))
952 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info
) (not (git-fileinfo->marked info
))) t
) git-status
)
953 ; move back to goal column after invalidate
954 (when goal-column
(move-to-column goal-column
)))
956 (defun git-next-file (&optional n
)
957 "Move the selection down N files."
959 (unless git-status
(error "Not in git-status buffer."))
960 (ewoc-goto-next git-status n
))
962 (defun git-prev-file (&optional n
)
963 "Move the selection up N files."
965 (unless git-status
(error "Not in git-status buffer."))
966 (ewoc-goto-prev git-status n
))
968 (defun git-next-unmerged-file (&optional n
)
969 "Move the selection down N unmerged files."
971 (unless git-status
(error "Not in git-status buffer."))
972 (let* ((last (ewoc-locate git-status
))
973 (node (ewoc-next git-status last
)))
974 (while (and node
(> n
0))
975 (when (eq 'unmerged
(git-fileinfo->state
(ewoc-data node
)))
978 (setq node
(ewoc-next git-status node
)))
979 (ewoc-goto-node git-status last
)))
981 (defun git-prev-unmerged-file (&optional n
)
982 "Move the selection up N unmerged files."
984 (unless git-status
(error "Not in git-status buffer."))
985 (let* ((last (ewoc-locate git-status
))
986 (node (ewoc-prev git-status last
)))
987 (while (and node
(> n
0))
988 (when (eq 'unmerged
(git-fileinfo->state
(ewoc-data node
)))
991 (setq node
(ewoc-prev git-status node
)))
992 (ewoc-goto-node git-status last
)))
994 (defun git-add-file ()
995 "Add marked file(s) to the index cache."
997 (let ((files (git-get-filenames (git-marked-files-state 'unknown
'ignored
))))
998 ;; FIXME: add support for directories
1000 (push (file-relative-name (read-file-name "File to add: " nil nil t
)) files
))
1001 (when (apply 'git-call-process-display-error
"update-index" "--add" "--" files
)
1002 (git-update-status-files files
'uptodate
)
1003 (git-success-message "Added" files
))))
1005 (defun git-ignore-file ()
1006 "Add marked file(s) to the ignore list."
1008 (let ((files (git-get-filenames (git-marked-files-state 'unknown
))))
1010 (push (file-relative-name (read-file-name "File to ignore: " nil nil t
)) files
))
1011 (dolist (f files
) (git-append-to-ignore f
))
1012 (git-update-status-files files
'ignored
)
1013 (git-success-message "Ignored" files
)))
1015 (defun git-remove-file ()
1016 "Remove the marked file(s)."
1018 (let ((files (git-get-filenames (git-marked-files-state 'added
'modified
'unknown
'uptodate
'ignored
))))
1020 (push (file-relative-name (read-file-name "File to remove: " nil nil t
)) files
))
1022 (format "Remove %d file%s? " (length files
) (if (> (length files
) 1) "s" "")))
1024 (dolist (name files
)
1026 (if (file-directory-p name
)
1027 (delete-directory name
)
1028 (delete-file name
))))
1029 (when (apply 'git-call-process-display-error
"update-index" "--remove" "--" files
)
1030 (git-update-status-files files nil
)
1031 (git-success-message "Removed" files
)))
1032 (message "Aborting"))))
1034 (defun git-revert-file ()
1035 "Revert changes to the marked file(s)."
1037 (let ((files (git-marked-files-state 'added
'deleted
'modified
'unmerged
))
1041 (format "Revert %d file%s? " (length files
) (if (> (length files
) 1) "s" ""))))
1042 (dolist (info files
)
1043 (case (git-fileinfo->state info
)
1044 ('added
(push (git-fileinfo->name info
) added
))
1045 ('deleted
(push (git-fileinfo->name info
) modified
))
1046 ('unmerged
(push (git-fileinfo->name info
) modified
))
1047 ('modified
(push (git-fileinfo->name info
) modified
))))
1048 ;; check if a buffer contains one of the files and isn't saved
1049 (dolist (file modified
)
1050 (let ((buffer (get-file-buffer file
)))
1051 (when (and buffer
(buffer-modified-p buffer
))
1052 (error "Buffer %s is modified. Please kill or save modified buffers before reverting." (buffer-name buffer
)))))
1055 (apply 'git-call-process-display-error
"update-index" "--force-remove" "--" added
))
1057 (apply 'git-call-process-display-error
"checkout" "HEAD" modified
)))))
1058 (git-update-status-files (append added modified
) 'uptodate
)
1060 (dolist (file modified
)
1061 (let ((buffer (get-file-buffer file
)))
1062 (when buffer
(with-current-buffer buffer
(revert-buffer t t t
)))))
1063 (git-success-message "Reverted" (git-get-filenames files
)))))))
1065 (defun git-resolve-file ()
1066 "Resolve conflicts in marked file(s)."
1068 (let ((files (git-get-filenames (git-marked-files-state 'unmerged
))))
1070 (when (apply 'git-call-process-display-error
"update-index" "--" files
)
1071 (git-update-status-files files
'uptodate
)
1072 (git-success-message "Resolved" files
)))))
1074 (defun git-remove-handled ()
1075 "Remove handled files from the status list."
1077 (ewoc-filter git-status
1079 (case (git-fileinfo->state info
)
1080 ('ignored git-show-ignored
)
1081 ('uptodate git-show-uptodate
)
1082 ('unknown git-show-unknown
)
1084 (unless (ewoc-nth git-status
0) ; refresh header if list is empty
1085 (git-refresh-ewoc-hf git-status
)))
1087 (defun git-toggle-show-uptodate ()
1088 "Toogle the option for showing up-to-date files."
1090 (if (setq git-show-uptodate
(not git-show-uptodate
))
1091 (git-refresh-status)
1092 (git-remove-handled)))
1094 (defun git-toggle-show-ignored ()
1095 "Toogle the option for showing ignored files."
1097 (if (setq git-show-ignored
(not git-show-ignored
))
1099 (message "Inserting ignored files...")
1100 (git-run-ls-files-with-excludes git-status nil
'ignored
"-o" "-i")
1102 (git-refresh-ewoc-hf git-status
)
1103 (message "Inserting ignored files...done"))
1104 (git-remove-handled)))
1106 (defun git-toggle-show-unknown ()
1107 "Toogle the option for showing unknown files."
1109 (if (setq git-show-unknown
(not git-show-unknown
))
1111 (message "Inserting unknown files...")
1112 (git-run-ls-files-with-excludes git-status nil
'unknown
"-o")
1114 (git-refresh-ewoc-hf git-status
)
1115 (message "Inserting unknown files...done"))
1116 (git-remove-handled)))
1118 (defun git-expand-directory (info)
1119 "Expand the directory represented by INFO to list its files."
1120 (when (eq (lsh (git-fileinfo->new-perm info
) -
9) ?
\110)
1121 (let ((dir (git-fileinfo->name info
)))
1122 (git-set-filenames-state git-status
(list dir
) nil
)
1123 (git-run-ls-files-with-excludes git-status
(list (concat dir
"/")) 'unknown
"-o")
1125 (git-refresh-ewoc-hf git-status
)
1128 (defun git-setup-diff-buffer (buffer)
1129 "Setup a buffer for displaying a diff."
1130 (let ((dir default-directory
))
1131 (with-current-buffer buffer
1133 (goto-char (point-min))
1134 (setq default-directory dir
)
1135 (setq buffer-read-only t
)))
1136 (display-buffer buffer
)
1137 ; shrink window only if it displays the status buffer
1138 (when (eq (window-buffer) (current-buffer))
1139 (shrink-window-if-larger-than-buffer)))
1141 (defun git-diff-file ()
1142 "Diff the marked file(s) against HEAD."
1144 (let ((files (git-marked-files)))
1145 (git-setup-diff-buffer
1146 (apply #'git-run-command-buffer
"*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files
)))))
1148 (defun git-diff-file-merge-head (arg)
1149 "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)."
1151 (let ((files (git-marked-files))
1152 (merge-heads (git-get-merge-heads)))
1153 (unless merge-heads
(error "No merge in progress"))
1154 (git-setup-diff-buffer
1155 (apply #'git-run-command-buffer
"*git-diff*" "diff-index" "-p" "-M"
1156 (or (nth (1- arg
) merge-heads
) "HEAD") "--" (git-get-filenames files
)))))
1158 (defun git-diff-unmerged-file (stage)
1159 "Diff the marked unmerged file(s) against the specified stage."
1160 (let ((files (git-marked-files)))
1161 (git-setup-diff-buffer
1162 (apply #'git-run-command-buffer
"*git-diff*" "diff-files" "-p" stage
"--" (git-get-filenames files
)))))
1164 (defun git-diff-file-base ()
1165 "Diff the marked unmerged file(s) against the common base file."
1167 (git-diff-unmerged-file "-1"))
1169 (defun git-diff-file-mine ()
1170 "Diff the marked unmerged file(s) against my pre-merge version."
1172 (git-diff-unmerged-file "-2"))
1174 (defun git-diff-file-other ()
1175 "Diff the marked unmerged file(s) against the other's pre-merge version."
1177 (git-diff-unmerged-file "-3"))
1179 (defun git-diff-file-combined ()
1180 "Do a combined diff of the marked unmerged file(s)."
1182 (git-diff-unmerged-file "-c"))
1184 (defun git-diff-file-idiff ()
1185 "Perform an interactive diff on the current file."
1187 (let ((files (git-marked-files-state 'added
'deleted
'modified
)))
1188 (unless (eq 1 (length files
))
1189 (error "Cannot perform an interactive diff on multiple files."))
1190 (let* ((filename (car (git-get-filenames files
)))
1191 (buff1 (find-file-noselect filename
))
1192 (buff2 (git-run-command-buffer (concat filename
".~HEAD~") "cat-file" "blob" (concat "HEAD:" filename
))))
1193 (ediff-buffers buff1 buff2
))))
1195 (defun git-log-file ()
1196 "Display a log of changes to the marked file(s)."
1198 (let* ((files (git-marked-files))
1199 (coding-system-for-read git-commits-coding-system
)
1200 (buffer (apply #'git-run-command-buffer
"*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files
))))
1201 (with-current-buffer buffer
1202 ; (git-log-mode) FIXME: implement log mode
1203 (goto-char (point-min))
1204 (setq buffer-read-only t
))
1205 (display-buffer buffer
)))
1207 (defun git-log-edit-files ()
1208 "Return a list of marked files for use in the log-edit buffer."
1209 (with-current-buffer log-edit-parent-buffer
1210 (git-get-filenames (git-marked-files-state 'added
'deleted
'modified
))))
1212 (defun git-log-edit-diff ()
1213 "Run a diff of the current files being committed from a log-edit buffer."
1214 (with-current-buffer log-edit-parent-buffer
1217 (defun git-append-sign-off (name email
)
1218 "Append a Signed-off-by entry to the current buffer, avoiding duplicates."
1219 (let ((sign-off (format "Signed-off-by: %s <%s>" name email
))
1220 (case-fold-search t
))
1221 (goto-char (point-min))
1222 (unless (re-search-forward (concat "^" (regexp-quote sign-off
)) nil t
)
1223 (goto-char (point-min))
1224 (unless (re-search-forward "^Signed-off-by: " nil t
)
1225 (setq sign-off
(concat "\n" sign-off
)))
1226 (goto-char (point-max))
1227 (insert sign-off
"\n"))))
1229 (defun git-setup-log-buffer (buffer &optional author-name author-email subject date msg
)
1230 "Setup the log buffer for a commit."
1231 (unless git-status
(error "Not in git-status buffer."))
1232 (let ((merge-heads (git-get-merge-heads))
1233 (dir default-directory
)
1234 (committer-name (git-get-committer-name))
1235 (committer-email (git-get-committer-email))
1236 (sign-off git-append-signed-off-by
))
1237 (with-current-buffer buffer
1242 (format "Author: %s <%s>\n%s%s"
1243 (or author-name committer-name
)
1244 (or author-email committer-email
)
1245 (if date
(format "Date: %s\n" date
) "")
1247 (format "Parent: %s\n%s\n"
1248 (git-rev-parse "HEAD")
1249 (mapconcat (lambda (str) (concat "Parent: " str
)) merge-heads
"\n"))
1251 'face
'git-header-face
)
1252 (propertize git-log-msg-separator
'face
'git-separator-face
)
1254 (when subject
(insert subject
"\n\n"))
1255 (cond (msg (insert msg
"\n"))
1256 ((file-readable-p ".dotest/msg")
1257 (insert-file-contents ".dotest/msg"))
1258 ((file-readable-p ".git/MERGE_MSG")
1259 (insert-file-contents ".git/MERGE_MSG")))
1260 ; delete empty lines at end
1261 (goto-char (point-min))
1262 (when (re-search-forward "\n+\\'" nil t
)
1263 (replace-match "\n" t t
))
1264 (when sign-off
(git-append-sign-off committer-name committer-email
)))
1267 (defun git-commit-file ()
1268 "Commit the marked file(s), asking for a commit message."
1270 (unless git-status
(error "Not in git-status buffer."))
1271 (when (git-run-pre-commit-hook)
1272 (let ((buffer (get-buffer-create "*git-commit*"))
1273 (coding-system (git-get-commits-coding-system))
1274 author-name author-email subject date
)
1275 (when (eq 0 (buffer-size buffer
))
1276 (when (file-readable-p ".dotest/info")
1278 (insert-file-contents ".dotest/info")
1279 (goto-char (point-min))
1280 (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t
)
1281 (setq author-name
(match-string 1))
1282 (setq author-email
(match-string 2)))
1283 (goto-char (point-min))
1284 (when (re-search-forward "^Subject: \\(.*\\)$" nil t
)
1285 (setq subject
(match-string 1)))
1286 (goto-char (point-min))
1287 (when (re-search-forward "^Date: \\(.*\\)$" nil t
)
1288 (setq date
(match-string 1)))))
1289 (git-setup-log-buffer buffer author-name author-email subject date
))
1290 (if (boundp 'log-edit-diff-function
)
1291 (log-edit 'git-do-commit nil
'((log-edit-listfun . git-log-edit-files
)
1292 (log-edit-diff-function . git-log-edit-diff
)) buffer
)
1293 (log-edit 'git-do-commit nil
'git-log-edit-files buffer
))
1294 (setq font-lock-keywords
(font-lock-compile-keywords git-log-edit-font-lock-keywords
))
1295 (setq buffer-file-coding-system coding-system
)
1296 (re-search-forward (regexp-quote (concat git-log-msg-separator
"\n")) nil t
))))
1298 (defun git-setup-commit-buffer (commit)
1299 "Setup the commit buffer with the contents of COMMIT."
1300 (let (author-name author-email subject date msg
)
1302 (let ((coding-system (git-get-logoutput-coding-system)))
1303 (git-call-process-env t nil
"log" "-1" commit
)
1304 (goto-char (point-min))
1305 (when (re-search-forward "^Author: *\\(.*\\) <\\(.*\\)>$" nil t
)
1306 (setq author-name
(match-string 1))
1307 (setq author-email
(match-string 2)))
1308 (when (re-search-forward "^Date: *\\(.*\\)$" nil t
)
1309 (setq date
(match-string 1)))
1310 (while (re-search-forward "^ \\(.*\\)$" nil t
)
1311 (push (match-string 1) msg
))
1312 (setq msg
(nreverse msg
))
1313 (setq subject
(pop msg
))
1314 (while (and msg
(zerop (length (car msg
))) (pop msg
)))))
1315 (git-setup-log-buffer (get-buffer-create "*git-commit*")
1316 author-name author-email subject date
1317 (mapconcat #'identity msg
"\n"))))
1319 (defun git-get-commit-files (commit)
1320 "Retrieve the list of files modified by COMMIT."
1323 (git-call-process-env t nil
"diff-tree" "-r" "-z" "--name-only" "--no-commit-id" commit
)
1324 (goto-char (point-min))
1325 (while (re-search-forward "\\([^\0]*\\)\0" nil t
1)
1326 (push (match-string 1) files
)))
1329 (defun git-amend-commit ()
1330 "Undo the last commit on HEAD, and set things up to commit an
1331 amended version of it."
1333 (unless git-status
(error "Not in git-status buffer."))
1334 (when (git-empty-db-p) (error "No commit to amend."))
1335 (let* ((commit (git-rev-parse "HEAD"))
1336 (files (git-get-commit-files commit
)))
1337 (when (git-call-process-display-error "reset" "--soft" "HEAD^")
1338 (git-update-status-files (copy-sequence files
) 'uptodate
)
1339 (git-mark-files git-status files
)
1341 (git-setup-commit-buffer commit
)
1342 (git-commit-file))))
1344 (defun git-find-file ()
1345 "Visit the current file in its own buffer."
1347 (unless git-status
(error "Not in git-status buffer."))
1348 (let ((info (ewoc-data (ewoc-locate git-status
))))
1349 (unless (git-expand-directory info
)
1350 (find-file (git-fileinfo->name info
))
1351 (when (eq 'unmerged
(git-fileinfo->state info
))
1354 (defun git-find-file-other-window ()
1355 "Visit the current file in its own buffer in another window."
1357 (unless git-status
(error "Not in git-status buffer."))
1358 (let ((info (ewoc-data (ewoc-locate git-status
))))
1359 (find-file-other-window (git-fileinfo->name info
))
1360 (when (eq 'unmerged
(git-fileinfo->state info
))
1363 (defun git-find-file-imerge ()
1364 "Visit the current file in interactive merge mode."
1366 (unless git-status
(error "Not in git-status buffer."))
1367 (let ((info (ewoc-data (ewoc-locate git-status
))))
1368 (find-file (git-fileinfo->name info
))
1371 (defun git-view-file ()
1372 "View the current file in its own buffer."
1374 (unless git-status
(error "Not in git-status buffer."))
1375 (let ((info (ewoc-data (ewoc-locate git-status
))))
1376 (view-file (git-fileinfo->name info
))))
1378 (defun git-refresh-status ()
1379 "Refresh the git status buffer."
1381 (let* ((status git-status
)
1382 (pos (ewoc-locate status
))
1383 (marked-files (git-get-filenames (ewoc-collect status
(lambda (info) (git-fileinfo->marked info
)))))
1384 (cur-name (and pos
(git-fileinfo->name
(ewoc-data pos
)))))
1385 (unless status
(error "Not in git-status buffer."))
1386 (message "Refreshing git status...")
1387 (git-call-process-env nil nil
"update-index" "--refresh")
1388 (git-clear-status status
)
1389 (git-update-status-files nil
)
1390 ; restore file marks
1392 (git-status-filenames-map status
1394 (setf (git-fileinfo->marked info
) t
)
1395 (setf (git-fileinfo->needs-refresh info
) t
))
1397 (git-refresh-files))
1398 ; move point to the current file name if any
1399 (message "Refreshing git status...done")
1400 (let ((node (and cur-name
(git-find-status-file status cur-name
))))
1401 (when node
(ewoc-goto-node status node
)))))
1403 (defun git-status-quit ()
1404 "Quit git-status mode."
1409 ;;;; ------------------------------------------------------------
1411 (defvar git-status-mode-hook nil
1412 "Run after `git-status-mode' is setup.")
1414 (defvar git-status-mode-map nil
1415 "Keymap for git major mode.")
1417 (defvar git-status nil
1418 "List of all files managed by the git-status mode.")
1420 (unless git-status-mode-map
1421 (let ((map (make-keymap))
1422 (commit-map (make-sparse-keymap))
1423 (diff-map (make-sparse-keymap))
1424 (toggle-map (make-sparse-keymap)))
1425 (suppress-keymap map
)
1426 (define-key map
"?" 'git-help
)
1427 (define-key map
"h" 'git-help
)
1428 (define-key map
" " 'git-next-file
)
1429 (define-key map
"a" 'git-add-file
)
1430 (define-key map
"c" 'git-commit-file
)
1431 (define-key map
"\C-c" commit-map
)
1432 (define-key map
"d" diff-map
)
1433 (define-key map
"=" 'git-diff-file
)
1434 (define-key map
"f" 'git-find-file
)
1435 (define-key map
"\r" 'git-find-file
)
1436 (define-key map
"g" 'git-refresh-status
)
1437 (define-key map
"i" 'git-ignore-file
)
1438 (define-key map
"l" 'git-log-file
)
1439 (define-key map
"m" 'git-mark-file
)
1440 (define-key map
"M" 'git-mark-all
)
1441 (define-key map
"n" 'git-next-file
)
1442 (define-key map
"N" 'git-next-unmerged-file
)
1443 (define-key map
"o" 'git-find-file-other-window
)
1444 (define-key map
"p" 'git-prev-file
)
1445 (define-key map
"P" 'git-prev-unmerged-file
)
1446 (define-key map
"q" 'git-status-quit
)
1447 (define-key map
"r" 'git-remove-file
)
1448 (define-key map
"R" 'git-resolve-file
)
1449 (define-key map
"t" toggle-map
)
1450 (define-key map
"T" 'git-toggle-all-marks
)
1451 (define-key map
"u" 'git-unmark-file
)
1452 (define-key map
"U" 'git-revert-file
)
1453 (define-key map
"v" 'git-view-file
)
1454 (define-key map
"x" 'git-remove-handled
)
1455 (define-key map
"\C-?" 'git-unmark-file-up
)
1456 (define-key map
"\M-\C-?" 'git-unmark-all
)
1458 (define-key commit-map
"\C-a" 'git-amend-commit
)
1460 (define-key diff-map
"b" 'git-diff-file-base
)
1461 (define-key diff-map
"c" 'git-diff-file-combined
)
1462 (define-key diff-map
"=" 'git-diff-file
)
1463 (define-key diff-map
"e" 'git-diff-file-idiff
)
1464 (define-key diff-map
"E" 'git-find-file-imerge
)
1465 (define-key diff-map
"h" 'git-diff-file-merge-head
)
1466 (define-key diff-map
"m" 'git-diff-file-mine
)
1467 (define-key diff-map
"o" 'git-diff-file-other
)
1469 (define-key toggle-map
"u" 'git-toggle-show-uptodate
)
1470 (define-key toggle-map
"i" 'git-toggle-show-ignored
)
1471 (define-key toggle-map
"k" 'git-toggle-show-unknown
)
1472 (define-key toggle-map
"m" 'git-toggle-all-marks
)
1473 (setq git-status-mode-map map
))
1474 (easy-menu-define git-menu git-status-mode-map
1477 ["Refresh" git-refresh-status t
]
1478 ["Commit" git-commit-file t
]
1480 ["Next Unmerged File" git-next-unmerged-file t
]
1481 ["Prev Unmerged File" git-prev-unmerged-file t
]
1482 ["Mark as Resolved" git-resolve-file t
]
1483 ["Interactive Merge File" git-find-file-imerge t
]
1484 ["Diff Against Common Base File" git-diff-file-base t
]
1485 ["Diff Combined" git-diff-file-combined t
]
1486 ["Diff Against Merge Head" git-diff-file-merge-head t
]
1487 ["Diff Against Mine" git-diff-file-mine t
]
1488 ["Diff Against Other" git-diff-file-other t
])
1490 ["Add File" git-add-file t
]
1491 ["Revert File" git-revert-file t
]
1492 ["Ignore File" git-ignore-file t
]
1493 ["Remove File" git-remove-file t
]
1495 ["Find File" git-find-file t
]
1496 ["View File" git-view-file t
]
1497 ["Diff File" git-diff-file t
]
1498 ["Interactive Diff File" git-diff-file-idiff t
]
1499 ["Log" git-log-file t
]
1501 ["Mark" git-mark-file t
]
1502 ["Mark All" git-mark-all t
]
1503 ["Unmark" git-unmark-file t
]
1504 ["Unmark All" git-unmark-all t
]
1505 ["Toggle All Marks" git-toggle-all-marks t
]
1506 ["Hide Handled Files" git-remove-handled t
]
1508 ["Show Uptodate Files" git-toggle-show-uptodate
:style toggle
:selected git-show-uptodate
]
1509 ["Show Ignored Files" git-toggle-show-ignored
:style toggle
:selected git-show-ignored
]
1510 ["Show Unknown Files" git-toggle-show-unknown
:style toggle
:selected git-show-unknown
]
1512 ["Quit" git-status-quit t
])))
1515 ;; git mode should only run in the *git status* buffer
1516 (put 'git-status-mode
'mode-class
'special
)
1518 (defun git-status-mode ()
1519 "Major mode for interacting with Git.
1521 \\{git-status-mode-map}"
1522 (kill-all-local-variables)
1523 (buffer-disable-undo)
1524 (setq mode-name
"git status"
1525 major-mode
'git-status-mode
1528 (use-local-map git-status-mode-map
)
1529 (let ((buffer-read-only nil
))
1531 (let ((status (ewoc-create 'git-fileinfo-prettyprint
"" "")))
1532 (set (make-local-variable 'git-status
) status
))
1533 (set (make-local-variable 'list-buffers-directory
) default-directory
)
1534 (make-local-variable 'git-show-uptodate
)
1535 (make-local-variable 'git-show-ignored
)
1536 (make-local-variable 'git-show-unknown
)
1537 (run-hooks 'git-status-mode-hook
)))
1539 (defun git-find-status-buffer (dir)
1540 "Find the git status buffer handling a specified directory."
1541 (let ((list (buffer-list))
1542 (fulldir (expand-file-name dir
))
1544 (while (and list
(not found
))
1545 (let ((buffer (car list
)))
1546 (with-current-buffer buffer
1547 (when (and list-buffers-directory
1548 (string-equal fulldir
(expand-file-name list-buffers-directory
))
1549 (string-match "\\*git-status\\*$" (buffer-name buffer
)))
1550 (setq found buffer
))))
1551 (setq list
(cdr list
)))
1554 (defun git-status (dir)
1555 "Entry point into git-status mode."
1556 (interactive "DSelect directory: ")
1557 (setq dir
(git-get-top-dir dir
))
1558 (if (file-directory-p (concat (file-name-as-directory dir
) ".git"))
1559 (let ((buffer (or (and git-reuse-status-buffer
(git-find-status-buffer dir
))
1560 (create-file-buffer (expand-file-name "*git-status*" dir
)))))
1561 (switch-to-buffer buffer
)
1564 (git-refresh-status)
1565 (goto-char (point-min))
1566 (add-hook 'after-save-hook
'git-update-saved-file
))
1567 (message "%s is not a git working tree." dir
)))
1569 (defun git-update-saved-file ()
1570 "Update the corresponding git-status buffer when a file is saved.
1571 Meant to be used in `after-save-hook'."
1572 (let* ((file (expand-file-name buffer-file-name
))
1573 (dir (condition-case nil
(git-get-top-dir (file-name-directory file
)) (error nil
)))
1574 (buffer (and dir
(git-find-status-buffer dir
))))
1576 (with-current-buffer buffer
1577 (let ((filename (file-relative-name file dir
)))
1578 ; skip files located inside the .git directory
1579 (unless (string-match "^\\.git/" filename
)
1580 (git-call-process-env nil nil
"add" "--refresh" "--" filename
)
1581 (git-update-status-files (list filename
) 'uptodate
)))))))
1584 "Display help for Git mode."
1586 (describe-function 'git-status-mode
))
1589 ;;; git.el ends here