1 ;;; vc-git.el --- VC backend for the git version control system
3 ;; Copyright (C) 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: Alexandre Julliard
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This file contains a VC backend for the git version control
33 ;; To install: put this file on the load-path and add GIT to the list
34 ;; of supported backends in `vc-handled-backends'; the following line,
35 ;; placed in your ~/.emacs, will accomplish this:
37 ;; (add-to-list 'vc-handled-backends 'GIT)
40 ;; - check if more functions could use vc-git-command instead
42 ;; - changelog generation
43 ;; - working with revisions other than HEAD
45 ;; Implement the rest of the vc interface. See the comment at the
46 ;; beginning of vc.el. The current status is:
48 ;; FUNCTION NAME STATUS
50 ;; * revision-granularity OK
51 ;; STATE-QUERYING FUNCTIONS
52 ;; * registered (file) OK
54 ;; - state-heuristic (file) ?? PROBABLY NOT NEEDED
55 ;; - dir-state (dir) OK
56 ;; * workfile-version (file) OK
57 ;; - latest-on-branch-p (file) ??
58 ;; * checkout-model (file) OK
59 ;; - workfile-unchanged-p (file) MAYBE CAN BE SIMPLIFIED
60 ;; - mode-line-string (file) NOT NEEDED
61 ;; - dired-state-info (file) OK
62 ;; STATE-CHANGING FUNCTIONS
63 ;; * create-repo () OK
64 ;; * register (files &optional rev comment) OK
65 ;; - init-version (file) ??
66 ;; - responsible-p (file) OK
67 ;; - could-register (file) NEEDED
68 ;; - receive-file (file rev) ??
69 ;; - unregister (file) NEEDED
70 ;; * checkin (files rev comment) OK
71 ;; * find-version (file rev buffer) NEEDED!
72 ;; * checkout (file &optional editable rev) OK
73 ;; * revert (file &optional contents-done) OK
74 ;; - rollback (files) NEEDED
75 ;; - merge (file rev1 rev2) NEEDED
76 ;; - merge-news (file) NEEDED
77 ;; - steal-lock (file &optional version) NOT NEEDED
79 ;; * print-log (files &optional buffer) OK
80 ;; - log-view-mode () OK
81 ;; - show-log-entry (version) NOT NEEDED, DEFAULT IS GOOD
82 ;; - wash-log (file) ??
83 ;; - logentry-check () ??
84 ;; - comment-history (file) ??
85 ;; - update-changelog (files) ??
86 ;; * diff (file &optional rev1 rev2 buffer) PORT TO NEW VC INTERFACE
87 ;; - revision-completion-table (file) NEEDED?
88 ;; - diff-tree (dir &optional rev1 rev2) NEEDED
89 ;; - annotate-command (file buf &optional rev) OK
90 ;; - annotate-time () OK
91 ;; - annotate-current-time () ?? NOT NEEDED
92 ;; - annotate-extract-revision-at-line () OK
94 ;; - create-snapshot (dir name branchp) NEEDED
95 ;; - assign-name (file name) NOT NEEDED
96 ;; - retrieve-snapshot (dir name update) NEEDED
98 ;; - make-version-backups-p (file) ??
99 ;; - repository-hostname (dirname) ??
100 ;; - previous-version (file rev) ??
101 ;; - next-version (file rev) ??
102 ;; - check-headers () ??
103 ;; - clear-headers () ??
104 ;; - delete-file (file) NEEDED
105 ;; - rename-file (old new) NEEDED
106 ;; - find-file-hook () PROBABLY NOT NEEDED
107 ;; - find-file-not-found-hook () PROBABLY NOT NEEDED
109 (eval-when-compile (require 'cl
))
111 (defvar git-commits-coding-system
'utf-8
112 "Default coding system for git commits.")
114 ;; XXX when this backend is considered sufficiently reliable this
115 ;; should be moved to vc-hooks.el
116 (add-to-list 'vc-handled-backends
'GIT
)
117 (add-to-list 'vc-directory-exclusion-list
".git" t
)
119 ;;; BACKEND PROPERTIES
121 (defun vc-git-revision-granularity ()
124 ;;; STATE-QUERYING FUNCTIONS
126 ;;;###autoload (defun vc-git-registered (file)
127 ;;;###autoload "Return non-nil if FILE is registered with git."
128 ;;;###autoload (if (vc-find-root file ".git") ; short cut
129 ;;;###autoload (progn
130 ;;;###autoload (load "vc-git")
131 ;;;###autoload (vc-git-registered file))))
133 (defun vc-git-registered (file)
134 "Check whether FILE is registered with git."
135 (when (vc-git-root file
)
137 (let* ((dir (file-name-directory file
))
138 (name (file-relative-name file dir
)))
141 (eq 0 (call-process "git" nil
'(t nil
) nil
"ls-files" "-c" "-z" "--" name
)))
142 (let ((str (buffer-string)))
143 (and (> (length str
) (length name
))
144 (string= (substring str
0 (1+ (length name
))) (concat name
"\0")))))))))
146 (defun vc-git-state (file)
147 "Git-specific version of `vc-state'."
148 (let ((diff (vc-git--run-command-string file
"diff-index" "-z" "HEAD" "--")))
149 (if (and diff
(string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff
))
153 (defun vc-git-dir-state (dir)
155 (vc-git-command (current-buffer) nil nil
"ls-files" "-t")
156 (goto-char (point-min))
157 (let ((status-char nil
)
160 (setq status-char
(char-after))
163 (buffer-substring-no-properties (+ (point) 2) (line-end-position))))
165 ;; The rest of the possible states in "git ls-files -t" output:
168 ;; should not show up in vc-dired, so don't deal with them
171 (vc-file-setprop file
'vc-state
'up-to-date
))
173 (vc-file-setprop file
'vc-state
'edited
))
175 (vc-file-setprop file
'vc-state
'edited
))
177 (vc-file-setprop file
'vc-backend
'none
)
178 (vc-file-setprop file
'vc-state
'nil
)))
181 (defun vc-git-workfile-version (file)
182 "Git-specific version of `vc-workfile-version'."
183 (let ((str (with-output-to-string
184 (with-current-buffer standard-output
185 (call-process "git" nil
'(t nil
) nil
"symbolic-ref" "HEAD")))))
186 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str
)
190 (defun vc-git-checkout-model (file)
193 ;; XXX Can't this just use the result of vc-git-state?
194 (defun vc-git-workfile-unchanged-p (file)
195 (let ((sha1 (vc-git--run-command-string file
"hash-object" "--"))
196 (head (vc-git--run-command-string file
"ls-tree" "-z" "HEAD" "--")))
198 (string-match "[0-7]\\{6\\} blob \\([0-9a-f]\\{40\\}\\)\t[^\0]+\0" head
)
199 (string= (car (split-string sha1
"\n")) (match-string 1 head
)))))
201 (defun vc-git-dired-state-info (file)
202 "Git-specific version of `vc-dired-state-info'."
203 (let ((git-state (vc-state file
)))
204 (if (eq git-state
'edited
)
206 ;; fall back to the default VC representation
207 (vc-default-dired-state-info 'GIT file
))))
209 ;;; STATE-CHANGING FUNCTIONS
211 (defun vc-git-create-repo ()
212 "Create a new GIT repository."
213 (vc-git-command "init" nil
0 nil
))
215 (defun vc-git-register (files &optional rev comment
)
216 "Register FILE into the git version-control system."
217 (vc-git-command nil
0 files
"update-index" "--add" "--"))
219 (defalias 'vc-git-responsible-p
'vc-git-root
)
221 (defun vc-git-checkin (files rev comment
)
222 (let ((coding-system-for-write git-commits-coding-system
))
223 (vc-git-command nil
0 files
"commit" "-m" comment
"--only" "--")))
225 (defun vc-git-checkout (file &optional editable rev destfile
)
227 (let ((fullname (substring
228 (vc-git--run-command-string file
"ls-files" "-z" "--full-name" "--")
230 (coding-system-for-read 'no-conversion
)
231 (coding-system-for-write 'no-conversion
))
232 (with-temp-file destfile
233 (eq 0 (call-process "git" nil t nil
"cat-file" "blob"
234 (concat (or rev
"HEAD") ":" fullname
)))))
235 (vc-git--run-command file
"checkout" (or rev
"HEAD"))))
237 (defun vc-git-revert (file &optional contents-done
)
238 "Revert FILE to the version stored in the git repository."
240 (vc-git--run-command file
"update-index" "--")
241 (vc-git--run-command file
"checkout" "HEAD")))
243 ;;; HISTORY FUNCTIONS
245 (defun vc-git-print-log (files &optional buffer
)
246 "Get change log associated with FILES."
247 (let ((name (file-relative-name file
))
248 (coding-system-for-read git-commits-coding-system
))
249 ;; `log-view-mode' needs to have the file name in order to function
250 ;; correctly. "git log" does not print it, so we insert it here by
253 ;; `vc-do-command' creates the buffer, but we need it before running
255 (vc-setup-buffer buffer
)
256 ;; If the buffer exists from a previous invocation it might be
258 (let ((inhibit-read-only t
))
259 ;; XXX Here loop and call "git rev-list" on each file separately
260 ;; to make sure that each file gets a "File:" header before the
261 ;; corresponding log. Maybe there is a way to do this with one
266 (insert "File: " (file-name-nondirectory file
) "\n")))
267 (vc-git-command buffer
'async name
"rev-list" "--pretty" "HEAD" "--"))))
269 (defvar log-view-message-re
)
270 (defvar log-view-file-re
)
271 (defvar log-view-font-lock-keywords
)
273 (define-derived-mode vc-git-log-view-mode log-view-mode
"GIT-Log-View"
274 (require 'add-log
) ;; we need the faces add-log
275 ;; Don't have file markers, so use impossible regexp.
276 (set (make-local-variable 'log-view-file-re
) "^File:[ \t]+\\(.+\\)")
277 (set (make-local-variable 'log-view-message-re
)
278 "^commit *\\([0-9a-z]+\\)")
279 (set (make-local-variable 'log-view-font-lock-keywords
)
281 `((,log-view-message-re
(1 'change-log-acknowledgement
))
282 (,log-view-file-re
(1 'change-log-file-face
)))
285 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
286 (1 'change-log-email
))
288 ;; user: FirstName LastName <foo@bar>
289 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
291 (2 'change-log-email
))
292 ("^Date: \\(.+\\)" (1 'change-log-date
))
293 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message
))))))
295 (defun vc-git-diff (file &optional rev1 rev2 buffer
)
296 (let ((name (file-relative-name file
))
297 (buf (or buffer
"*vc-diff*")))
299 (vc-git-command buf
0 name
"diff-tree" "-p" rev1 rev2
"--")
300 (vc-git-command buf
0 name
"diff-index" "-p" (or rev1
"HEAD") "--"))
301 ;; git-diff-index doesn't set exit status like diff does
302 (if (vc-git-workfile-unchanged-p file
) 0 1)))
304 (defun vc-git-annotate-command (file buf
&optional rev
)
305 ;; FIXME: rev is ignored
306 (let ((name (file-relative-name file
)))
307 (vc-git-command buf
0 name
"blame" (if rev
(concat "-r" rev
)))))
309 (defun vc-git-annotate-time ()
310 (and (re-search-forward "[0-9a-f]+ (.* \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\) \\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) \\([-+0-9]+\\) +[0-9]+)" nil t
)
311 (vc-annotate-convert-time
312 (apply #'encode-time
(mapcar (lambda (match) (string-to-number (match-string match
))) '(6 5 4 3 2 1 7))))))
314 (defun vc-git-annotate-extract-revision-at-line ()
316 (move-beginning-of-line 1)
317 (and (looking-at "[0-9a-f]+")
318 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
322 (defun vc-git-previous-version (file rev
)
323 "Git-specific version of `vc-previous-version'."
324 (let ((default-directory (file-name-directory (expand-file-name file
)))
325 (file (file-name-nondirectory file
)))
326 (vc-git-symbolic-commit
330 (call-process "git" nil
'(t nil
) nil
"rev-list"
332 (goto-char (point-max))
334 (zerop (forward-line -
1))
336 (buffer-substring-no-properties
338 (1- (point-max))))))))
340 (defun vc-git-next-version (file rev
)
341 "Git-specific version of `vc-next-version'."
342 (let* ((default-directory (file-name-directory
343 (expand-file-name file
)))
344 (file (file-name-nondirectory file
))
349 (call-process "git" nil
'(t nil
) nil
"rev-list"
351 (goto-char (point-max))
353 (zerop (forward-line -
1))
355 (buffer-substring-no-properties
357 (1- (point-max)))))))
359 (vc-git-symbolic-commit
363 (call-process "git" nil
'(t nil
) nil
"rev-list"
365 (goto-char (point-min))
366 (search-forward current-rev nil t
)
367 (zerop (forward-line -
1))
368 (buffer-substring-no-properties
370 (progn (forward-line 1) (1- (point))))))))))
375 (defun vc-git-root (file)
376 (vc-find-root file
".git"))
378 (defun vc-git-command (buffer okstatus file-or-list
&rest flags
)
379 "A wrapper around `vc-do-command' for use in vc-git.el.
380 The difference to vc-do-command is that this function always invokes `git'."
381 (apply 'vc-do-command buffer okstatus
"git" file-or-list flags
))
383 (defun vc-git--run-command-string (file &rest args
)
384 "Run a git command on FILE and return its output as string."
386 (str (with-output-to-string
387 (with-current-buffer standard-output
388 (unless (eq 0 (apply #'call-process
"git" nil
'(t nil
) nil
389 (append args
(list (file-relative-name file
)))))
393 (defun vc-git--run-command (file &rest args
)
394 "Run a git command on FILE, discarding any output."
395 (let ((name (file-relative-name file
)))
396 (eq 0 (apply #'call-process
"git" nil
(get-buffer "*Messages") nil
(append args
(list name
))))))
398 (defun vc-git-symbolic-commit (commit)
399 "Translate COMMIT string into symbolic form.
400 Returns nil if not possible."
405 (call-process "git" nil
'(t nil
) nil
"name-rev"
406 "--name-only" "--tags"
408 (goto-char (point-min))
409 (= (forward-line 2) 1)
411 (buffer-substring-no-properties (point-min) (1- (point-max)))))))
415 ;;; vc-git.el ends here