Merge from emacs--rel--22
[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
625f71cf 12;; the Free Software Foundation; either version 3, or (at your option)
fff4a046
DN
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
4211679b 33;; To install: put this file on the load-path and add Git to the list
fff4a046
DN
34;; of supported backends in `vc-handled-backends'; the following line,
35;; placed in your ~/.emacs, will accomplish this:
36;;
4211679b 37;; (add-to-list 'vc-handled-backends 'Git)
fff4a046
DN
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
6f222162 60;; - mode-line-string (file) OK
53cc90ab
DN
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;;; BACKEND PROPERTIES
118
119(defun vc-git-revision-granularity ()
120 'repository)
121
122;;; STATE-QUERYING FUNCTIONS
123
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))))
130
fff4a046
DN
131(defun vc-git-registered (file)
132 "Check whether FILE is registered with git."
53cc90ab
DN
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")))))))))
108607bc 143
fff4a046 144(defun vc-git-state (file)
53cc90ab 145 "Git-specific version of `vc-state'."
fff4a046
DN
146 (let ((diff (vc-git--run-command-string file "diff-index" "-z" "HEAD" "--")))
147 (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff))
148 'edited
149 'up-to-date)))
150
53cc90ab
DN
151(defun vc-git-dir-state (dir)
152 (with-temp-buffer
153 (vc-git-command (current-buffer) nil nil "ls-files" "-t")
154 (goto-char (point-min))
155 (let ((status-char nil)
156 (file nil))
157 (while (not (eobp))
158 (setq status-char (char-after))
159 (setq file
160 (expand-file-name
161 (buffer-substring-no-properties (+ (point) 2) (line-end-position))))
162 (cond
163 ;; The rest of the possible states in "git ls-files -t" output:
164 ;; R removed/deleted
165 ;; K to be killed
166 ;; should not show up in vc-dired, so don't deal with them
167 ;; here.
168 ((eq status-char ?H)
169 (vc-file-setprop file 'vc-state 'up-to-date))
170 ((eq status-char ?M)
171 (vc-file-setprop file 'vc-state 'edited))
172 ((eq status-char ?C)
173 (vc-file-setprop file 'vc-state 'edited))
174 ((eq status-char ??)
175 (vc-file-setprop file 'vc-backend 'none)
176 (vc-file-setprop file 'vc-state 'nil)))
177 (forward-line)))))
178
fff4a046 179(defun vc-git-workfile-version (file)
53cc90ab 180 "Git-specific version of `vc-workfile-version'."
fff4a046
DN
181 (let ((str (with-output-to-string
182 (with-current-buffer standard-output
183 (call-process "git" nil '(t nil) nil "symbolic-ref" "HEAD")))))
184 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
185 (match-string 2 str)
186 str)))
187
188(defun vc-git-checkout-model (file)
189 'implicit)
190
191(defun vc-git-workfile-unchanged-p (file)
4d3ac01e
DN
192 ;; The reason this does not use the result of vc-git-state is that
193 ;; git-diff-index (used by vc-git-state) doesn't refresh the cached
194 ;; stat info, so if the file has been modified it will always show
195 ;; up as modified in vc-git-state, even if the change has been
196 ;; undone, until git-update-index --refresh is run.
108607bc 197
4d3ac01e
DN
198 ;; OTOH the vc-git-workfile-unchanged-p implementation checks the
199 ;; actual content, so it will detect the case of a file reverted
200 ;; back to its original state.
201
202 ;; The ideal implementation would be to refresh the stat cache and
203 ;; then call vc-git-state, but at the moment there's no git command
204 ;; to refresh a single file, so this will have to be added first.
fff4a046
DN
205 (let ((sha1 (vc-git--run-command-string file "hash-object" "--"))
206 (head (vc-git--run-command-string file "ls-tree" "-z" "HEAD" "--")))
207 (and head
208 (string-match "[0-7]\\{6\\} blob \\([0-9a-f]\\{40\\}\\)\t[^\0]+\0" head)
209 (string= (car (split-string sha1 "\n")) (match-string 1 head)))))
210
6f222162
DN
211(defun vc-git-mode-line-string (file)
212 "Return string for placement into the modeline for FILE."
213 (let* ((branch (vc-git-workfile-version file))
214 (def-ml (vc-default-mode-line-string 'Git file))
215 (help-echo (get-text-property 0 'help-echo def-ml)))
216 (if (zerop (length branch))
217 (propertize
218 (concat def-ml "!")
219 'help-echo (concat help-echo "\nNo current branch (detached HEAD)"))
220 (propertize def-ml
221 'help-echo (concat help-echo "\nCurrent branch: " branch)))))
222
53cc90ab
DN
223(defun vc-git-dired-state-info (file)
224 "Git-specific version of `vc-dired-state-info'."
225 (let ((git-state (vc-state file)))
226 (if (eq git-state 'edited)
227 "(modified)"
228 ;; fall back to the default VC representation
4211679b 229 (vc-default-dired-state-info 'Git file))))
53cc90ab
DN
230
231;;; STATE-CHANGING FUNCTIONS
232
233(defun vc-git-create-repo ()
4211679b 234 "Create a new Git repository."
53cc90ab
DN
235 (vc-git-command "init" nil 0 nil))
236
8b9783e0 237(defun vc-git-register (files &optional rev comment)
fff4a046 238 "Register FILE into the git version-control system."
8b9783e0 239 (vc-git-command nil 0 files "update-index" "--add" "--"))
fff4a046 240
53cc90ab
DN
241(defalias 'vc-git-responsible-p 'vc-git-root)
242
d7009f45
DN
243(defun vc-git-unregister (file)
244 (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
108607bc 245
d7009f45 246
8b9783e0 247(defun vc-git-checkin (files rev comment)
fff4a046 248 (let ((coding-system-for-write git-commits-coding-system))
8b9783e0 249 (vc-git-command nil 0 files "commit" "-m" comment "--only" "--")))
fff4a046 250
b0f90937
DN
251(defun vc-git-find-version (file rev buffer)
252 (let ((coding-system-for-read 'binary)
8b38ce20
DN
253 (coding-system-for-write 'binary)
254 (fullname (substring
108607bc 255 (vc-git--run-command-string
8b38ce20
DN
256 file "ls-files" "-z" "--full-name" "--")
257 0 -1)))
108607bc
DN
258 (vc-git-command
259 buffer 0
8b38ce20 260 (concat (if rev rev "HEAD") ":" fullname) "cat-file" "blob")))
b0f90937
DN
261
262(defun vc-git-checkout (file &optional editable rev)
7546c767 263 (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
fff4a046
DN
264
265(defun vc-git-revert (file &optional contents-done)
266 "Revert FILE to the version stored in the git repository."
267 (if contents-done
b0f90937
DN
268 (vc-git-command nil 0 file "update-index" "--")
269 (vc-git-command nil 0 file "checkout" "HEAD")))
fff4a046 270
53cc90ab
DN
271;;; HISTORY FUNCTIONS
272
8b9783e0
DN
273(defun vc-git-print-log (files &optional buffer)
274 "Get change log associated with FILES."
a0709d8d
DN
275 (let ((coding-system-for-read git-commits-coding-system)
276 ;; Support both the old print-log interface that passes a
277 ;; single file, and the new one that passes a file list.
278 (flist (if (listp files) files (list files))))
53cc90ab
DN
279 ;; `vc-do-command' creates the buffer, but we need it before running
280 ;; the command.
281 (vc-setup-buffer buffer)
282 ;; If the buffer exists from a previous invocation it might be
283 ;; read-only.
284 (let ((inhibit-read-only t))
64e3efd9
DN
285 ;; XXX `log-view-mode' needs to have something to identify where
286 ;; the log for each individual file starts. It seems that by
287 ;; default git does not output this info. So loop here and call
288 ;; "git rev-list" on each file separately to make sure that each
289 ;; file gets a "File:" header before the corresponding
290 ;; log. Maybe there is a way to do this with one command...
a0709d8d 291 (dolist (file flist)
64e3efd9
DN
292 (with-current-buffer
293 buffer
294 (insert "File: " (file-name-nondirectory file) "\n"))
108607bc 295 (vc-git-command buffer 'async (file-relative-name file)
64e3efd9 296 "rev-list" "--pretty" "HEAD" "--")))))
53cc90ab
DN
297
298(defvar log-view-message-re)
299(defvar log-view-file-re)
300(defvar log-view-font-lock-keywords)
301
4211679b 302(define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View"
53cc90ab
DN
303 (require 'add-log) ;; we need the faces add-log
304 ;; Don't have file markers, so use impossible regexp.
305 (set (make-local-variable 'log-view-file-re) "^File:[ \t]+\\(.+\\)")
306 (set (make-local-variable 'log-view-message-re)
307 "^commit *\\([0-9a-z]+\\)")
308 (set (make-local-variable 'log-view-font-lock-keywords)
309 (append
310 `((,log-view-message-re (1 'change-log-acknowledgement))
311 (,log-view-file-re (1 'change-log-file-face)))
312 ;; Handle the case:
313 ;; user: foo@bar
314 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
315 (1 'change-log-email))
316 ;; Handle the case:
317 ;; user: FirstName LastName <foo@bar>
318 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
319 (1 'change-log-name)
320 (2 'change-log-email))
321 ("^Date: \\(.+\\)" (1 'change-log-date))
322 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))
fff4a046 323
b747d346
DN
324(defun vc-git-diff (files &optional rev1 rev2 buffer)
325 (let ((buf (or buffer "*vc-diff*")))
fff4a046 326 (if (and rev1 rev2)
b747d346
DN
327 (vc-git-command buf 1 files "diff-tree" "--exit-code" "-p" rev1 rev2 "--")
328 (vc-git-command buf 1 files "diff-index" "--exit-code" "-p" (or rev1 "HEAD") "--"))))
fff4a046 329
108607bc
DN
330(defun vc-git-revision-table (file)
331 (let ((table (list "HEAD")))
332 (with-temp-buffer
333 (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
334 (goto-char (point-min))
335 (while (re-search-forward "^refs/\\(heads\\|tags\\)/\\(.*\\)$" nil t)
336 (push (match-string 2) table)))
337 table))
338
339(defun vc-git-revision-completion-table (file)
340 (lexical-let ((file file)
341 table)
342 (setq table (lazy-completion-table
343 table (lambda () (vc-git-revision-table file))))
344 table))
345
b0f90937
DN
346(defun vc-git-diff-tree (dir &optional rev1 rev2)
347 (vc-git-diff dir rev1 rev2))
348
fff4a046 349(defun vc-git-annotate-command (file buf &optional rev)
53cc90ab 350 ;; FIXME: rev is ignored
fff4a046 351 (let ((name (file-relative-name file)))
53cc90ab 352 (vc-git-command buf 0 name "blame" (if rev (concat "-r" rev)))))
fff4a046
DN
353
354(defun vc-git-annotate-time ()
355 (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)
356 (vc-annotate-convert-time
357 (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
358
53cc90ab
DN
359(defun vc-git-annotate-extract-revision-at-line ()
360 (save-excursion
361 (move-beginning-of-line 1)
362 (and (looking-at "[0-9a-f]+")
363 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
364
b747d346
DN
365;;; SNAPSHOT SYSTEM
366
367(defun vc-git-create-snapshot (dir name branchp)
368 (let ((default-directory dir))
369 (and (vc-git-command nil 0 nil "update-index" "--refresh")
370 (if branchp
371 (vc-git-command nil 0 nil "checkout" "-b" name)
372 (vc-git-command nil 0 nil "tag" name)))))
373
374(defun vc-git-retrieve-snapshot (dir name update)
375 (let ((default-directory dir))
376 (vc-git-command nil 0 nil "checkout" name)
377 ;; FIXME: update buffers if `update' is true
378 ))
379
380
53cc90ab 381;;; MISCELLANEOUS
fff4a046
DN
382
383(defun vc-git-previous-version (file rev)
53cc90ab 384 "Git-specific version of `vc-previous-version'."
fff4a046
DN
385 (let ((default-directory (file-name-directory (expand-file-name file)))
386 (file (file-name-nondirectory file)))
387 (vc-git-symbolic-commit
388 (with-temp-buffer
389 (and
390 (zerop
391 (call-process "git" nil '(t nil) nil "rev-list"
392 "-2" rev "--" file))
393 (goto-char (point-max))
394 (bolp)
395 (zerop (forward-line -1))
396 (not (bobp))
397 (buffer-substring-no-properties
398 (point)
399 (1- (point-max))))))))
400
401(defun vc-git-next-version (file rev)
53cc90ab 402 "Git-specific version of `vc-next-version'."
fff4a046
DN
403 (let* ((default-directory (file-name-directory
404 (expand-file-name file)))
405 (file (file-name-nondirectory file))
406 (current-rev
407 (with-temp-buffer
408 (and
409 (zerop
410 (call-process "git" nil '(t nil) nil "rev-list"
411 "-1" rev "--" file))
412 (goto-char (point-max))
413 (bolp)
414 (zerop (forward-line -1))
415 (bobp)
416 (buffer-substring-no-properties
417 (point)
418 (1- (point-max)))))))
419 (and current-rev
420 (vc-git-symbolic-commit
421 (with-temp-buffer
422 (and
423 (zerop
424 (call-process "git" nil '(t nil) nil "rev-list"
425 "HEAD" "--" file))
426 (goto-char (point-min))
427 (search-forward current-rev nil t)
428 (zerop (forward-line -1))
429 (buffer-substring-no-properties
430 (point)
431 (progn (forward-line 1) (1- (point))))))))))
432
8b38ce20
DN
433(defun vc-git-delete-file (file)
434 (vc-git-command nil 0 file "rm" "-f" "--"))
b0f90937 435
8b38ce20
DN
436(defun vc-git-rename-file (old new)
437 (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
b0f90937 438
fff4a046 439\f
b747d346 440;;; Internal commands
fff4a046 441
53cc90ab
DN
442(defun vc-git-root (file)
443 (vc-find-root file ".git"))
444
8b9783e0 445(defun vc-git-command (buffer okstatus file-or-list &rest flags)
53cc90ab
DN
446 "A wrapper around `vc-do-command' for use in vc-git.el.
447The difference to vc-do-command is that this function always invokes `git'."
8b9783e0 448 (apply 'vc-do-command buffer okstatus "git" file-or-list flags))
53cc90ab 449
fff4a046
DN
450(defun vc-git--run-command-string (file &rest args)
451 "Run a git command on FILE and return its output as string."
452 (let* ((ok t)
453 (str (with-output-to-string
454 (with-current-buffer standard-output
455 (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
456 (append args (list (file-relative-name file)))))
457 (setq ok nil))))))
458 (and ok str)))
459
fff4a046
DN
460(defun vc-git-symbolic-commit (commit)
461 "Translate COMMIT string into symbolic form.
462Returns nil if not possible."
463 (and commit
464 (with-temp-buffer
465 (and
466 (zerop
467 (call-process "git" nil '(t nil) nil "name-rev"
468 "--name-only" "--tags"
469 commit))
470 (goto-char (point-min))
471 (= (forward-line 2) 1)
472 (bolp)
473 (buffer-substring-no-properties (point-min) (1- (point-max)))))))
474
475(provide 'vc-git)
53cc90ab 476
328471d9 477;; arch-tag: bd10664a-0e5b-48f5-a877-6c17b135be12
53cc90ab 478;;; vc-git.el ends here