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