Regenerate.
[emacs.git] / lisp / vc-git.el
blob64efec6a41fc3cfb5f64a83a2080cb80d985ceae
1 ;;; vc-git.el --- VC backend for the git version control system
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Alexandre Julliard <julliard@winehq.org>
6 ;; Keywords: tools
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 3, or (at your option)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;; This file contains a VC backend for the git version control
28 ;; system.
31 ;;; Installation:
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)
39 ;;; Todo:
40 ;; - check if more functions could use vc-git-command instead
41 ;; of start-process.
42 ;; - changelog generation
44 ;; Implement the rest of the vc interface. See the comment at the
45 ;; beginning of vc.el. The current status is:
46 ;; ("??" means: "figure out what to do about it")
48 ;; FUNCTION NAME STATUS
49 ;; BACKEND PROPERTIES
50 ;; * revision-granularity OK
51 ;; STATE-QUERYING FUNCTIONS
52 ;; * registered (file) OK
53 ;; * state (file) OK
54 ;; - state-heuristic (file) NOT NEEDED
55 ;; - dir-state (dir) OK
56 ;; * workfile-version (file) OK
57 ;; - latest-on-branch-p (file) NOT NEEDED
58 ;; * checkout-model (file) OK
59 ;; - workfile-unchanged-p (file) OK
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) NOT NEEDED
66 ;; - responsible-p (file) OK
67 ;; - could-register (file) NOT NEEDED, DEFAULT IS GOOD
68 ;; - receive-file (file rev) NOT NEEDED
69 ;; - unregister (file) OK
70 ;; * checkin (files rev comment) OK
71 ;; * find-version (file rev buffer) OK
72 ;; * checkout (file &optional editable rev) OK
73 ;; * revert (file &optional contents-done) OK
74 ;; - rollback (files) COULD BE SUPPORTED
75 ;; - merge (file rev1 rev2) It would be possible to merge changes into
76 ;; a single file, but when committing they
77 ;; wouldn't be identified as a merge by git,
78 ;; so it's probably not a good idea.
79 ;; - merge-news (file) see `merge'
80 ;; - steal-lock (file &optional version) NOT NEEDED
81 ;; HISTORY FUNCTIONS
82 ;; * print-log (files &optional buffer) OK
83 ;; - log-view-mode () OK
84 ;; - show-log-entry (version) NOT NEEDED, DEFAULT IS GOOD
85 ;; - wash-log (file) COULD BE SUPPORTED
86 ;; - logentry-check () NOT NEEDED
87 ;; - comment-history (file) ??
88 ;; - update-changelog (files) COULD BE SUPPORTED
89 ;; * diff (file &optional rev1 rev2 buffer) OK
90 ;; - revision-completion-table (file) NOT SUPPORTED in emacs-22.x
91 ;; - diff-tree (dir &optional rev1 rev2) OK
92 ;; - annotate-command (file buf &optional rev) OK
93 ;; - annotate-time () OK
94 ;; - annotate-current-time () NOT NEEDED
95 ;; - annotate-extract-revision-at-line () OK
96 ;; SNAPSHOT SYSTEM
97 ;; - create-snapshot (dir name branchp) OK
98 ;; - assign-name (file name) NOT NEEDED
99 ;; - retrieve-snapshot (dir name update) OK, needs to update buffers
100 ;; MISCELLANEOUS
101 ;; - make-version-backups-p (file) NOT NEEDED
102 ;; - repository-hostname (dirname) NOT NEEDED
103 ;; - previous-version (file rev) OK
104 ;; - next-version (file rev) OK
105 ;; - check-headers () COULD BE SUPPORTED
106 ;; - clear-headers () NOT NEEDED
107 ;; - delete-file (file) OK
108 ;; - rename-file (old new) OK
109 ;; - find-file-hook () NOT NEEDED
110 ;; - find-file-not-found-hook () NOT NEEDED
112 (eval-when-compile (require 'cl) (require 'vc))
114 (defvar git-commits-coding-system 'utf-8
115 "Default coding system for git commits.")
117 ;;; BACKEND PROPERTIES
119 (defun vc-git-revision-granularity ()
120 'repository)
122 ;;; STATE-QUERYING FUNCTIONS
124 ;;;###autoload (defun vc-git-registered (file)
125 ;;;###autoload "Return non-nil if FILE is registered with git."
126 ;;;###autoload (if (vc-find-root file ".git") ; short cut
127 ;;;###autoload (progn
128 ;;;###autoload (load "vc-git")
129 ;;;###autoload (vc-git-registered file))))
131 (defun vc-git-registered (file)
132 "Check whether FILE is registered with git."
133 (when (vc-git-root file)
134 (with-temp-buffer
135 (let* ((dir (file-name-directory file))
136 (name (file-relative-name file dir)))
137 (and (ignore-errors
138 (when dir (cd dir))
139 (eq 0 (call-process "git" nil '(t nil) nil "ls-files" "-c" "-z" "--" name)))
140 (let ((str (buffer-string)))
141 (and (> (length str) (length name))
142 (string= (substring str 0 (1+ (length name))) (concat name "\0")))))))))
144 (defun vc-git-state (file)
145 "Git-specific version of `vc-state'."
146 (call-process "git" nil nil nil "add" "--refresh" "--" (file-relative-name file))
147 (let ((diff (vc-git--run-command-string file "diff-index" "-z" "HEAD" "--")))
148 (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff))
149 'edited
150 'up-to-date)))
152 (defun vc-git-dir-state (dir)
153 (with-temp-buffer
154 (vc-git-command (current-buffer) nil nil "ls-files" "-t" "-c" "-m" "-o")
155 (goto-char (point-min))
156 (let ((status-char nil)
157 (file nil))
158 (while (not (eobp))
159 (setq status-char (char-after))
160 (setq file
161 (expand-file-name
162 (buffer-substring-no-properties (+ (point) 2) (line-end-position))))
163 (cond
164 ;; The rest of the possible states in "git ls-files -t" output:
165 ;; R removed/deleted
166 ;; K to be killed
167 ;; should not show up in vc-dired, so don't deal with them
168 ;; here.
169 ((eq status-char ?H)
170 (vc-file-setprop file 'vc-backend 'Git)
171 (vc-file-setprop file 'vc-state 'up-to-date))
172 ((eq status-char ?M)
173 (vc-file-setprop file 'vc-backend 'Git)
174 (vc-file-setprop file 'vc-state 'edited))
175 ((eq status-char ?C)
176 (vc-file-setprop file 'vc-backend 'Git)
177 (vc-file-setprop file 'vc-state 'edited))
178 ((eq status-char ??)
179 (vc-file-setprop file 'vc-backend 'none)
180 (vc-file-setprop file 'vc-state 'nil)))
181 (forward-line)))))
183 (defun vc-git-workfile-version (file)
184 "Git-specific version of `vc-workfile-version'."
185 (let ((str (with-output-to-string
186 (with-current-buffer standard-output
187 (call-process "git" nil '(t nil) nil "symbolic-ref" "HEAD")))))
188 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
189 (match-string 2 str)
190 str)))
192 (defun vc-git-checkout-model (file)
193 'implicit)
195 (defun vc-git-workfile-unchanged-p (file)
196 (eq 'up-to-date (vc-git-state file)))
198 (defun vc-git-dired-state-info (file)
199 "Git-specific version of `vc-dired-state-info'."
200 (let ((git-state (vc-state file)))
201 (if (eq git-state 'edited)
202 "(modified)"
203 ;; fall back to the default VC representation
204 (vc-default-dired-state-info 'Git file))))
206 ;;; STATE-CHANGING FUNCTIONS
208 (defun vc-git-create-repo ()
209 "Create a new Git repository."
210 (vc-git-command nil 0 nil "init"))
212 (defun vc-git-register (files &optional rev comment)
213 "Register FILE into the git version-control system."
214 (vc-git-command nil 0 files "update-index" "--add" "--"))
216 (defalias 'vc-git-responsible-p 'vc-git-root)
218 (defun vc-git-unregister (file)
219 (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
222 (defun vc-git-checkin (files rev comment)
223 (let ((coding-system-for-write git-commits-coding-system))
224 (vc-git-command nil 0 files "commit" "-m" comment "--only" "--")))
226 (defun vc-git-find-version (file rev buffer)
227 (let ((coding-system-for-read 'binary)
228 (coding-system-for-write 'binary)
229 (fullname (substring
230 (vc-git--run-command-string
231 file "ls-files" "-z" "--full-name" "--")
232 0 -1)))
233 (vc-git-command
234 buffer 0
235 (concat (if rev rev "HEAD") ":" fullname) "cat-file" "blob")))
237 (defun vc-git-checkout (file &optional editable rev)
238 (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
240 (defun vc-git-revert (file &optional contents-done)
241 "Revert FILE to the version stored in the git repository."
242 (if contents-done
243 (vc-git-command nil 0 file "update-index" "--")
244 (vc-git-command nil 0 file "checkout" "HEAD")))
246 ;;; HISTORY FUNCTIONS
248 (defun vc-git-print-log (files &optional buffer)
249 "Get change log associated with FILES."
250 (let ((coding-system-for-read git-commits-coding-system)
251 ;; Support both the old print-log interface that passes a
252 ;; single file, and the new one that passes a file list.
253 (flist (if (listp files) files (list files))))
254 ;; `vc-do-command' creates the buffer, but we need it before running
255 ;; the command.
256 (vc-setup-buffer buffer)
257 ;; If the buffer exists from a previous invocation it might be
258 ;; read-only.
259 (let ((inhibit-read-only t))
260 ;; XXX `log-view-mode' needs to have something to identify where
261 ;; the log for each individual file starts. It seems that by
262 ;; default git does not output this info. So loop here and call
263 ;; "git rev-list" on each file separately to make sure that each
264 ;; file gets a "File:" header before the corresponding
265 ;; log. Maybe there is a way to do this with one command...
266 (dolist (file flist)
267 (with-current-buffer
268 buffer
269 (insert "File: " (file-name-nondirectory file) "\n"))
270 (vc-git-command buffer 'async (file-relative-name file)
271 "rev-list" "--pretty" "HEAD" "--")))))
273 (defvar log-view-message-re)
274 (defvar log-view-file-re)
275 (defvar log-view-font-lock-keywords)
277 (define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View"
278 (require 'add-log) ;; we need the faces add-log
279 ;; Don't have file markers, so use impossible regexp.
280 (set (make-local-variable 'log-view-file-re) "^File:[ \t]+\\(.+\\)")
281 (set (make-local-variable 'log-view-message-re)
282 "^commit *\\([0-9a-z]+\\)")
283 (set (make-local-variable 'log-view-font-lock-keywords)
284 (append
285 `((,log-view-message-re (1 'change-log-acknowledgement))
286 (,log-view-file-re (1 'change-log-file-face)))
287 ;; Handle the case:
288 ;; user: foo@bar
289 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
290 (1 'change-log-email))
291 ;; Handle the case:
292 ;; user: FirstName LastName <foo@bar>
293 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
294 (1 'change-log-name)
295 (2 'change-log-email))
296 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
297 (1 'change-log-name))
298 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
299 (1 'change-log-name)
300 (2 'change-log-email))
301 ("^Merge: \\([0-9a-z]+\\) \\([0-9a-z]+\\)"
302 (1 'change-log-acknowledgement)
303 (2 'change-log-acknowledgement))
304 ("^Date: \\(.+\\)" (1 'change-log-date))
305 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))
307 (defun vc-git-diff (files &optional rev1 rev2 buffer)
308 (let ((buf (or buffer "*vc-diff*")))
309 (if (and rev1 rev2)
310 (vc-git-command buf 1 files "diff-tree" "--exit-code" "-p" rev1 rev2 "--")
311 (vc-git-command buf 1 files "diff-index" "--exit-code" "-p" (or rev1 "HEAD") "--"))))
313 (defun vc-git-revision-table (file)
314 (let ((table (list "HEAD")))
315 (with-temp-buffer
316 (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
317 (goto-char (point-min))
318 (while (re-search-forward "^refs/\\(heads\\|tags\\)/\\(.*\\)$" nil t)
319 (push (match-string 2) table)))
320 table))
322 (defun vc-git-revision-completion-table (file)
323 (lexical-let ((file file)
324 table)
325 (setq table (lazy-completion-table
326 table (lambda () (vc-git-revision-table file))))
327 table))
329 (defun vc-git-diff-tree (dir &optional rev1 rev2)
330 (vc-git-diff dir rev1 rev2))
332 (defun vc-git-annotate-command (file buf &optional rev)
333 ;; FIXME: rev is ignored
334 (let ((name (file-relative-name file)))
335 (vc-git-command buf 0 name "blame" (if rev (concat "-r" rev)))))
337 (defun vc-git-annotate-time ()
338 (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)
339 (vc-annotate-convert-time
340 (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
342 (defun vc-git-annotate-extract-revision-at-line ()
343 (save-excursion
344 (move-beginning-of-line 1)
345 (and (looking-at "[0-9a-f]+")
346 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
348 ;;; SNAPSHOT SYSTEM
350 (defun vc-git-create-snapshot (dir name branchp)
351 (let ((default-directory dir))
352 (and (vc-git-command nil 0 nil "update-index" "--refresh")
353 (if branchp
354 (vc-git-command nil 0 nil "checkout" "-b" name)
355 (vc-git-command nil 0 nil "tag" name)))))
357 (defun vc-git-retrieve-snapshot (dir name update)
358 (let ((default-directory dir))
359 (vc-git-command nil 0 nil "checkout" name)
360 ;; FIXME: update buffers if `update' is true
364 ;;; MISCELLANEOUS
366 (defun vc-git-previous-version (file rev)
367 "Git-specific version of `vc-previous-version'."
368 (let ((default-directory (file-name-directory (expand-file-name file)))
369 (file (file-name-nondirectory file)))
370 (vc-git-symbolic-commit
371 (with-temp-buffer
372 (and
373 (zerop
374 (call-process "git" nil '(t nil) nil "rev-list"
375 "-2" rev "--" file))
376 (goto-char (point-max))
377 (bolp)
378 (zerop (forward-line -1))
379 (not (bobp))
380 (buffer-substring-no-properties
381 (point)
382 (1- (point-max))))))))
384 (defun vc-git-next-version (file rev)
385 "Git-specific version of `vc-next-version'."
386 (let* ((default-directory (file-name-directory
387 (expand-file-name file)))
388 (file (file-name-nondirectory file))
389 (current-rev
390 (with-temp-buffer
391 (and
392 (zerop
393 (call-process "git" nil '(t nil) nil "rev-list"
394 "-1" rev "--" file))
395 (goto-char (point-max))
396 (bolp)
397 (zerop (forward-line -1))
398 (bobp)
399 (buffer-substring-no-properties
400 (point)
401 (1- (point-max)))))))
402 (and current-rev
403 (vc-git-symbolic-commit
404 (with-temp-buffer
405 (and
406 (zerop
407 (call-process "git" nil '(t nil) nil "rev-list"
408 "HEAD" "--" file))
409 (goto-char (point-min))
410 (search-forward current-rev nil t)
411 (zerop (forward-line -1))
412 (buffer-substring-no-properties
413 (point)
414 (progn (forward-line 1) (1- (point))))))))))
416 (defun vc-git-delete-file (file)
417 (vc-git-command nil 0 file "rm" "-f" "--"))
419 (defun vc-git-rename-file (old new)
420 (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
423 ;;; Internal commands
425 (defun vc-git-root (file)
426 (vc-find-root file ".git"))
428 (defun vc-git-command (buffer okstatus file-or-list &rest flags)
429 "A wrapper around `vc-do-command' for use in vc-git.el.
430 The difference to vc-do-command is that this function always invokes `git'."
431 (apply 'vc-do-command buffer okstatus "git" file-or-list flags))
433 (defun vc-git--run-command-string (file &rest args)
434 "Run a git command on FILE and return its output as string."
435 (let* ((ok t)
436 (str (with-output-to-string
437 (with-current-buffer standard-output
438 (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
439 (append args (list (file-relative-name file)))))
440 (setq ok nil))))))
441 (and ok str)))
443 (defun vc-git-symbolic-commit (commit)
444 "Translate COMMIT string into symbolic form.
445 Returns nil if not possible."
446 (and commit
447 (with-temp-buffer
448 (and
449 (zerop
450 (call-process "git" nil '(t nil) nil "name-rev"
451 "--name-only" "--tags"
452 commit))
453 (goto-char (point-min))
454 (= (forward-line 2) 1)
455 (bolp)
456 (buffer-substring-no-properties (point-min) (1- (point-max)))))))
458 (provide 'vc-git)
460 ;; arch-tag: bd10664a-0e5b-48f5-a877-6c17b135be12
461 ;;; vc-git.el ends here