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