* vc-hooks.el (vc-default-mode-line-string): Add case for added files.
[bpt/emacs.git] / lisp / vc-git.el
1 ;;; vc-git.el --- VC backend for the git version control system
2
3 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Alexandre Julliard <julliard@winehq.org>
6 ;; Keywords: tools
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; This file contains a VC backend for the git version control
28 ;; system.
29 ;;
30
31 ;;; Installation:
32
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:
36 ;;
37 ;; (add-to-list 'vc-handled-backends 'Git)
38
39 ;;; Todo:
40 ;; - check if more functions could use vc-git-command instead
41 ;; of start-process.
42 ;; - changelog generation
43
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")
47 ;;
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 ;; * working-revision (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) OK
61 ;; - dired-state-info (file) OK
62 ;; STATE-CHANGING FUNCTIONS
63 ;; * create-repo () OK
64 ;; * register (files &optional rev comment) OK
65 ;; - init-revision (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-revision (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 revision) NOT NEEDED
81 ;; HISTORY FUNCTIONS
82 ;; * print-log (files &optional buffer) OK
83 ;; - log-view-mode () OK
84 ;; - show-log-entry (revision) OK
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 (files) NEEDED?
91 ;; - annotate-command (file buf &optional rev) OK
92 ;; - annotate-time () OK
93 ;; - annotate-current-time () NOT NEEDED
94 ;; - annotate-extract-revision-at-line () OK
95 ;; SNAPSHOT SYSTEM
96 ;; - create-snapshot (dir name branchp) OK
97 ;; - assign-name (file name) NOT NEEDED
98 ;; - retrieve-snapshot (dir name update) OK, needs to update buffers
99 ;; MISCELLANEOUS
100 ;; - make-version-backups-p (file) NOT NEEDED
101 ;; - repository-hostname (dirname) NOT NEEDED
102 ;; - previous-revision (file rev) OK
103 ;; - next-revision (file rev) OK
104 ;; - check-headers () COULD BE SUPPORTED
105 ;; - clear-headers () NOT NEEDED
106 ;; - delete-file (file) OK
107 ;; - rename-file (old new) OK
108 ;; - find-file-hook () NOT NEEDED
109 ;; - find-file-not-found-hook () NOT NEEDED
110
111 (eval-when-compile (require 'cl) (require 'vc))
112
113 (defvar git-commits-coding-system 'utf-8
114 "Default coding system for git commits.")
115
116 ;;; BACKEND PROPERTIES
117
118 (defun vc-git-revision-granularity ()
119 'repository)
120
121 ;;; STATE-QUERYING FUNCTIONS
122
123 ;;;###autoload (defun vc-git-registered (file)
124 ;;;###autoload "Return non-nil if FILE is registered with git."
125 ;;;###autoload (if (vc-find-root file ".git") ; short cut
126 ;;;###autoload (progn
127 ;;;###autoload (load "vc-git")
128 ;;;###autoload (vc-git-registered file))))
129
130 (defun vc-git-registered (file)
131 "Check whether FILE is registered with git."
132 (when (vc-git-root file)
133 (with-temp-buffer
134 (let* ((dir (file-name-directory file))
135 (name (file-relative-name file dir)))
136 (and (ignore-errors
137 (when dir (cd dir))
138 (vc-git--out-ok "ls-files" "-c" "-z" "--" name))
139 (let ((str (buffer-string)))
140 (and (> (length str) (length name))
141 (string= (substring str 0 (1+ (length name)))
142 (concat name "\0")))))))))
143
144 (defun vc-git-state (file)
145 "Git-specific version of `vc-state'."
146 ;; FIXME: This can't set 'ignored yet
147 (vc-git--call nil "add" "--refresh" "--" (file-relative-name file))
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"
150 diff))
151 (if (string= (match-string 1 diff) "A") 'added 'edited)
152 'up-to-date)))
153
154 (defun vc-git--ls-files-state (state &rest args)
155 "Set state to STATE on all files found with git-ls-files ARGS."
156 (with-temp-buffer
157 (apply 'vc-git-command (current-buffer) nil nil "ls-files" "-z" args)
158 (goto-char (point-min))
159 (let ((start (point)))
160 (while (search-forward "\0" nil t)
161 (let ((file (expand-file-name
162 (buffer-substring-no-properties start (1- (point))))))
163 (vc-file-setprop file 'vc-backend (if state 'Git 'none))
164 (vc-file-setprop file 'vc-state state))
165 (setq start (point))))))
166
167 (defun vc-git-dir-state (dir)
168 "Git-specific version of `dir-state'."
169 (vc-git--ls-files-state 'up-to-date "-c")
170 (vc-git--ls-files-state 'edited "-m")
171 (vc-git--ls-files-state 'removed "-d")
172 (vc-git--ls-files-state 'ignored "-o" "-i" "--exclude-standard")
173 (vc-git--ls-files-state nil "-o" "--exclude-standard"))
174
175 (defun vc-git-working-revision (file)
176 "Git-specific version of `vc-working-revision'."
177 (let ((str (with-output-to-string
178 (with-current-buffer standard-output
179 (vc-git--out-ok "symbolic-ref" "HEAD")))))
180 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
181 (match-string 2 str)
182 str)))
183
184 (defun vc-git-checkout-model (file)
185 'implicit)
186
187 (defun vc-git-workfile-unchanged-p (file)
188 (eq 'up-to-date (vc-git-state file)))
189
190 (defun vc-git-mode-line-string (file)
191 "Return string for placement into the modeline for FILE."
192 (let* ((branch (vc-git-working-revision file))
193 (def-ml (vc-default-mode-line-string 'Git file))
194 (help-echo (get-text-property 0 'help-echo def-ml)))
195 (if (zerop (length branch))
196 (propertize
197 (concat def-ml "!")
198 'help-echo (concat help-echo "\nNo current branch (detached HEAD)"))
199 (propertize def-ml
200 'help-echo (concat help-echo "\nCurrent branch: " branch)))))
201
202 ;; Variable used to keep the intermediate results for vc-git-status.
203 (defvar vc-git-status-result nil)
204
205 (defun vc-git-after-dir-status-stage2 (update-function status-buffer)
206 (goto-char (point-min))
207 (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
208 (push (cons (match-string 1) 'unregistered) vc-git-status-result))
209 (funcall update-function (nreverse vc-git-status-result) status-buffer)
210 ;; Remove the temporary buffer.
211 (kill-buffer (current-buffer)))
212
213 (defun vc-git-after-dir-status-stage1 (update-function status-buffer)
214 (goto-char (point-min))
215 (while (re-search-forward
216 ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\([ADMUT]\\)\0\\([^\0]+\\)\0"
217 nil t 1)
218 (let ((filename (match-string 2))
219 (status (case (string-to-char( match-string 1))
220 (?M 'edited)
221 (?A 'added)
222 (?D 'removed)
223 (?U 'edited) ;; FIXME
224 (?T 'edited)))) ;; FIXME
225 (push (cons filename status) vc-git-status-result)))
226 (erase-buffer)
227 (vc-git-command (current-buffer) 'async nil "ls-files" "-z" "-o"
228 "--directory" "--no-empty-directory" "--exclude-standard")
229 (vc-exec-after
230 `(vc-git-after-dir-status-stage2 (quote ,update-function) ,status-buffer)))
231
232 (defun vc-git-dir-status (dir update-function status-buffer)
233 "Return a list of conses (file . state) for DIR."
234 ;; Further things that would have to be fixed later:
235 ;; - support for an empty repository (with no initial commit)
236 ;; - how to handle unregistered directories
237 ;; - how to support vc-status on a subdir of the project tree
238 (with-current-buffer
239 (get-buffer-create
240 (expand-file-name " *VC-Git* tmp status" dir))
241 (set (make-local-variable 'vc-git-status-result) nil)
242 (cd dir)
243 (erase-buffer)
244 (vc-git-command (current-buffer) 'async nil "diff-index" "-z" "HEAD")
245 (vc-exec-after
246 `(vc-git-after-dir-status-stage1 (quote ,update-function) ,status-buffer))
247 (current-buffer)))
248
249 ;;; STATE-CHANGING FUNCTIONS
250
251 (defun vc-git-create-repo ()
252 "Create a new Git repository."
253 (vc-git-command nil 0 nil "init"))
254
255 (defun vc-git-register (files &optional rev comment)
256 "Register FILE into the git version-control system."
257 (vc-git-command nil 0 files "update-index" "--add" "--"))
258
259 (defalias 'vc-git-responsible-p 'vc-git-root)
260
261 (defun vc-git-unregister (file)
262 (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
263
264
265 (defun vc-git-checkin (files rev comment)
266 (let ((coding-system-for-write git-commits-coding-system))
267 (vc-git-command nil 0 files "commit" "-m" comment "--only" "--")))
268
269 (defun vc-git-find-revision (file rev buffer)
270 (let ((coding-system-for-read 'binary)
271 (coding-system-for-write 'binary)
272 (fullname (substring
273 (vc-git--run-command-string
274 file "ls-files" "-z" "--full-name" "--")
275 0 -1)))
276 (vc-git-command
277 buffer 0
278 (concat (if rev rev "HEAD") ":" fullname) "cat-file" "blob")))
279
280 (defun vc-git-checkout (file &optional editable rev)
281 (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
282
283 (defun vc-git-revert (file &optional contents-done)
284 "Revert FILE to the version stored in the git repository."
285 (if contents-done
286 (vc-git-command nil 0 file "update-index" "--")
287 (vc-git-command nil 0 file "checkout" "HEAD")))
288
289 ;;; HISTORY FUNCTIONS
290
291 (defun vc-git-print-log (files &optional buffer)
292 "Get change log associated with FILES."
293 (let ((coding-system-for-read git-commits-coding-system)
294 ;; Support both the old print-log interface that passes a
295 ;; single file, and the new one that passes a file list.
296 (flist (if (listp files) files (list files))))
297 ;; `vc-do-command' creates the buffer, but we need it before running
298 ;; the command.
299 (vc-setup-buffer buffer)
300 ;; If the buffer exists from a previous invocation it might be
301 ;; read-only.
302 (let ((inhibit-read-only t))
303 ;; XXX `log-view-mode' needs to have something to identify where
304 ;; the log for each individual file starts. It seems that by
305 ;; default git does not output this info. So loop here and call
306 ;; "git rev-list" on each file separately to make sure that each
307 ;; file gets a "File:" header before the corresponding
308 ;; log. Maybe there is a way to do this with one command...
309 (dolist (file flist)
310 (with-current-buffer
311 buffer
312 (insert "File: " (file-name-nondirectory file) "\n"))
313 (vc-git-command buffer 'async (file-relative-name file)
314 "rev-list" "--pretty" "HEAD" "--")))))
315
316 (defvar log-view-message-re)
317 (defvar log-view-file-re)
318 (defvar log-view-font-lock-keywords)
319
320 (define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View"
321 (require 'add-log) ;; we need the faces add-log
322 ;; Don't have file markers, so use impossible regexp.
323 (set (make-local-variable 'log-view-file-re) "^File:[ \t]+\\(.+\\)")
324 (set (make-local-variable 'log-view-message-re)
325 "^commit *\\([0-9a-z]+\\)")
326 (set (make-local-variable 'log-view-font-lock-keywords)
327 (append
328 `((,log-view-message-re (1 'change-log-acknowledgement))
329 (,log-view-file-re (1 'change-log-file-face)))
330 ;; Handle the case:
331 ;; user: foo@bar
332 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
333 (1 'change-log-email))
334 ;; Handle the case:
335 ;; user: FirstName LastName <foo@bar>
336 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
337 (1 'change-log-name)
338 (2 'change-log-email))
339 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
340 (1 'change-log-name))
341 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
342 (1 'change-log-name)
343 (2 'change-log-email))
344 ("^Merge: \\([0-9a-z]+\\) \\([0-9a-z]+\\)"
345 (1 'change-log-acknowledgement)
346 (2 'change-log-acknowledgement))
347 ("^Date: \\(.+\\)" (1 'change-log-date))
348 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))
349
350 (defun vc-git-show-log-entry (revision)
351 "Move to the log entry for REVISION.
352 REVISION may have the form BRANCH, BRANCH~N,
353 or BRANCH^ (where \"^\" can be repeated)."
354 (goto-char (point-min))
355 (search-forward "\ncommit" nil t
356 (cond ((string-match "~\\([0-9]\\)$" revision)
357 (1+ (string-to-number (match-string 1 revision))))
358 ((string-match "\\^+$" revision)
359 (1+ (length (match-string 0 revision))))
360 (t nil)))
361 (beginning-of-line))
362
363 (defun vc-git-diff (files &optional rev1 rev2 buffer)
364 (let ((buf (or buffer "*vc-diff*")))
365 (if (and rev1 rev2)
366 (vc-git-command buf 1 files "diff-tree" "--exit-code" "-p"
367 rev1 rev2 "--")
368 (vc-git-command buf 1 files "diff-index" "--exit-code" "-p"
369 (or rev1 "HEAD") "--"))))
370
371 (defun vc-git-revision-table (files)
372 ;; What about `files'?!? --Stef
373 (let ((table (list "HEAD")))
374 (with-temp-buffer
375 (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
376 (goto-char (point-min))
377 (while (re-search-forward "^refs/\\(heads\\|tags\\)/\\(.*\\)$" nil t)
378 (push (match-string 2) table)))
379 table))
380
381 (defun vc-git-revision-completion-table (files)
382 (lexical-let ((files files)
383 table)
384 (setq table (lazy-completion-table
385 table (lambda () (vc-git-revision-table files))))
386 table))
387
388 (defun vc-git-annotate-command (file buf &optional rev)
389 ;; FIXME: rev is ignored
390 (let ((name (file-relative-name file)))
391 (vc-git-command buf 0 name "blame" (if rev (concat "-r" rev)))))
392
393 (defun vc-git-annotate-time ()
394 (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)
395 (vc-annotate-convert-time
396 (apply #'encode-time (mapcar (lambda (match)
397 (string-to-number (match-string match)))
398 '(6 5 4 3 2 1 7))))))
399
400 (defun vc-git-annotate-extract-revision-at-line ()
401 (save-excursion
402 (move-beginning-of-line 1)
403 (and (looking-at "[0-9a-f]+")
404 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
405
406 ;;; SNAPSHOT SYSTEM
407
408 (defun vc-git-create-snapshot (dir name branchp)
409 (let ((default-directory dir))
410 (and (vc-git-command nil 0 nil "update-index" "--refresh")
411 (if branchp
412 (vc-git-command nil 0 nil "checkout" "-b" name)
413 (vc-git-command nil 0 nil "tag" name)))))
414
415 (defun vc-git-retrieve-snapshot (dir name update)
416 (let ((default-directory dir))
417 (vc-git-command nil 0 nil "checkout" name)
418 ;; FIXME: update buffers if `update' is true
419 ))
420
421
422 ;;; MISCELLANEOUS
423
424 (defun vc-git-previous-revision (file rev)
425 "Git-specific version of `vc-previous-revision'."
426 (let ((default-directory (file-name-directory (expand-file-name file)))
427 (file (file-name-nondirectory file)))
428 (vc-git-symbolic-commit
429 (with-temp-buffer
430 (and
431 (vc-git--out-ok "rev-list" "-2" rev "--" file)
432 (goto-char (point-max))
433 (bolp)
434 (zerop (forward-line -1))
435 (not (bobp))
436 (buffer-substring-no-properties
437 (point)
438 (1- (point-max))))))))
439
440 (defun vc-git-next-revision (file rev)
441 "Git-specific version of `vc-next-revision'."
442 (let* ((default-directory (file-name-directory
443 (expand-file-name file)))
444 (file (file-name-nondirectory file))
445 (current-rev
446 (with-temp-buffer
447 (and
448 (vc-git--out-ok "rev-list" "-1" rev "--" file)
449 (goto-char (point-max))
450 (bolp)
451 (zerop (forward-line -1))
452 (bobp)
453 (buffer-substring-no-properties
454 (point)
455 (1- (point-max)))))))
456 (and current-rev
457 (vc-git-symbolic-commit
458 (with-temp-buffer
459 (and
460 (vc-git--out-ok "rev-list" "HEAD" "--" file)
461 (goto-char (point-min))
462 (search-forward current-rev nil t)
463 (zerop (forward-line -1))
464 (buffer-substring-no-properties
465 (point)
466 (progn (forward-line 1) (1- (point))))))))))
467
468 (defun vc-git-delete-file (file)
469 (vc-git-command nil 0 file "rm" "-f" "--"))
470
471 (defun vc-git-rename-file (old new)
472 (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
473
474 \f
475 ;;; Internal commands
476
477 (defun vc-git-root (file)
478 (vc-find-root file ".git"))
479
480 (defun vc-git-command (buffer okstatus file-or-list &rest flags)
481 "A wrapper around `vc-do-command' for use in vc-git.el.
482 The difference to vc-do-command is that this function always invokes `git'."
483 (apply 'vc-do-command buffer okstatus "git" file-or-list flags))
484
485 (defun vc-git--call (buffer command &rest args)
486 ;; We don't need to care the arguments. If there is a file name, it
487 ;; is always a relative one. This works also for remote
488 ;; directories.
489 (apply 'process-file "git" nil buffer nil command args))
490
491 (defun vc-git--out-ok (command &rest args)
492 (zerop (apply 'vc-git--call '(t nil) command args)))
493
494 (defun vc-git--run-command-string (file &rest args)
495 "Run a git command on FILE and return its output as string."
496 (let* ((ok t)
497 (str (with-output-to-string
498 (with-current-buffer standard-output
499 (unless (apply 'vc-git--out-ok
500 (append args (list (file-relative-name
501 file))))
502 (setq ok nil))))))
503 (and ok str)))
504
505 (defun vc-git-symbolic-commit (commit)
506 "Translate COMMIT string into symbolic form.
507 Returns nil if not possible."
508 (and commit
509 (with-temp-buffer
510 (and
511 (vc-git--out-ok "name-rev" "--name-only" "--tags" commit)
512 (goto-char (point-min))
513 (= (forward-line 2) 1)
514 (bolp)
515 (buffer-substring-no-properties (point-min) (1- (point-max)))))))
516
517 (provide 'vc-git)
518
519 ;; arch-tag: bd10664a-0e5b-48f5-a877-6c17b135be12
520 ;;; vc-git.el ends here