vc-git.el: New file.
[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
5;; Author: Alexandre Julliard
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:
40;; - changelog generation
41;; - working with revisions other than HEAD
42;;
43
44(eval-when-compile (require 'cl))
45
46(defvar git-commits-coding-system 'utf-8
47 "Default coding system for git commits.")
48
49(defun vc-git-registered (file)
50 "Check whether FILE is registered with git."
51 (with-temp-buffer
52 (let* ((dir (file-name-directory file))
53 (name (file-relative-name file dir)))
54 (and (ignore-errors
55 (when dir (cd dir))
56 (eq 0 (call-process "git" nil '(t nil) nil "ls-files" "-c" "-z" "--" name)))
57 (let ((str (buffer-string)))
58 (and (> (length str) (length name))
59 (string= (substring str 0 (1+ (length name))) (concat name "\0"))))))))
60
61(defun vc-git-state (file)
62 "git-specific version of `vc-state'."
63 (let ((diff (vc-git--run-command-string file "diff-index" "-z" "HEAD" "--")))
64 (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff))
65 'edited
66 'up-to-date)))
67
68(defun vc-git-workfile-version (file)
69 "git-specific version of `vc-workfile-version'."
70 (let ((str (with-output-to-string
71 (with-current-buffer standard-output
72 (call-process "git" nil '(t nil) nil "symbolic-ref" "HEAD")))))
73 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
74 (match-string 2 str)
75 str)))
76
77(defun vc-git-checkout-model (file)
78 'implicit)
79
80(defun vc-git-workfile-unchanged-p (file)
81 (let ((sha1 (vc-git--run-command-string file "hash-object" "--"))
82 (head (vc-git--run-command-string file "ls-tree" "-z" "HEAD" "--")))
83 (and head
84 (string-match "[0-7]\\{6\\} blob \\([0-9a-f]\\{40\\}\\)\t[^\0]+\0" head)
85 (string= (car (split-string sha1 "\n")) (match-string 1 head)))))
86
87(defun vc-git-register (file &optional rev comment)
88 "Register FILE into the git version-control system."
89 (vc-git--run-command file "update-index" "--add" "--"))
90
91(defun vc-git-checkin (file rev comment)
92 (let ((coding-system-for-write git-commits-coding-system))
93 (vc-git--run-command file "commit" "-m" comment "--only" "--")))
94
95(defun vc-git-checkout (file &optional editable rev destfile)
96 (if destfile
97 (let ((fullname (substring
98 (vc-git--run-command-string file "ls-files" "-z" "--full-name" "--")
99 0 -1))
100 (coding-system-for-read 'no-conversion)
101 (coding-system-for-write 'no-conversion))
102 (with-temp-file destfile
103 (eq 0 (call-process "git" nil t nil "cat-file" "blob"
104 (concat (or rev "HEAD") ":" fullname)))))
105 (vc-git--run-command file "checkout" (or rev "HEAD"))))
106
107(defun vc-git-revert (file &optional contents-done)
108 "Revert FILE to the version stored in the git repository."
109 (if contents-done
110 (vc-git--run-command file "update-index" "--")
111 (vc-git--run-command file "checkout" "HEAD")))
112
113(defun vc-git-print-log (file &optional buffer)
114 (let ((name (file-relative-name file))
115 (coding-system-for-read git-commits-coding-system))
116 (vc-do-command buffer 'async "git" name "rev-list" "--pretty" "HEAD" "--")))
117
118(defun vc-git-diff (file &optional rev1 rev2 buffer)
119 (let ((name (file-relative-name file))
120 (buf (or buffer "*vc-diff*")))
121 (if (and rev1 rev2)
122 (vc-do-command buf 0 "git" name "diff-tree" "-p" rev1 rev2 "--")
123 (vc-do-command buf 0 "git" name "diff-index" "-p" (or rev1 "HEAD") "--"))
124 ; git-diff-index doesn't set exit status like diff does
125 (if (vc-git-workfile-unchanged-p file) 0 1)))
126
127(defun vc-git-annotate-command (file buf &optional rev)
128 ; FIXME: rev is ignored
129 (let ((name (file-relative-name file)))
130 (call-process "git" nil buf nil "blame" name)))
131
132(defun vc-git-annotate-time ()
133 (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)
134 (vc-annotate-convert-time
135 (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
136
137;; Not really useful since we can't do anything with the revision yet
138;;(defun vc-annotate-extract-revision-at-line ()
139;; (save-excursion
140;; (move-beginning-of-line 1)
141;; (and (looking-at "[0-9a-f]+")
142;; (buffer-substring (match-beginning 0) (match-end 0)))))
143
144(defun vc-git-previous-version (file rev)
145 "git-specific version of `vc-previous-version'."
146 (let ((default-directory (file-name-directory (expand-file-name file)))
147 (file (file-name-nondirectory file)))
148 (vc-git-symbolic-commit
149 (with-temp-buffer
150 (and
151 (zerop
152 (call-process "git" nil '(t nil) nil "rev-list"
153 "-2" rev "--" file))
154 (goto-char (point-max))
155 (bolp)
156 (zerop (forward-line -1))
157 (not (bobp))
158 (buffer-substring-no-properties
159 (point)
160 (1- (point-max))))))))
161
162(defun vc-git-next-version (file rev)
163 "git-specific version of `vc-next-version'."
164 (let* ((default-directory (file-name-directory
165 (expand-file-name file)))
166 (file (file-name-nondirectory file))
167 (current-rev
168 (with-temp-buffer
169 (and
170 (zerop
171 (call-process "git" nil '(t nil) nil "rev-list"
172 "-1" rev "--" file))
173 (goto-char (point-max))
174 (bolp)
175 (zerop (forward-line -1))
176 (bobp)
177 (buffer-substring-no-properties
178 (point)
179 (1- (point-max)))))))
180 (and current-rev
181 (vc-git-symbolic-commit
182 (with-temp-buffer
183 (and
184 (zerop
185 (call-process "git" nil '(t nil) nil "rev-list"
186 "HEAD" "--" file))
187 (goto-char (point-min))
188 (search-forward current-rev nil t)
189 (zerop (forward-line -1))
190 (buffer-substring-no-properties
191 (point)
192 (progn (forward-line 1) (1- (point))))))))))
193
194\f
195;; Internal commands
196
197(defun vc-git--run-command-string (file &rest args)
198 "Run a git command on FILE and return its output as string."
199 (let* ((ok t)
200 (str (with-output-to-string
201 (with-current-buffer standard-output
202 (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
203 (append args (list (file-relative-name file)))))
204 (setq ok nil))))))
205 (and ok str)))
206
207(defun vc-git--run-command (file &rest args)
208 "Run a git command on FILE, discarding any output."
209 (let ((name (file-relative-name file)))
210 (eq 0 (apply #'call-process "git" nil (get-buffer "*Messages") nil (append args (list name))))))
211
212(defun vc-git-symbolic-commit (commit)
213 "Translate COMMIT string into symbolic form.
214Returns nil if not possible."
215 (and commit
216 (with-temp-buffer
217 (and
218 (zerop
219 (call-process "git" nil '(t nil) nil "name-rev"
220 "--name-only" "--tags"
221 commit))
222 (goto-char (point-min))
223 (= (forward-line 2) 1)
224 (bolp)
225 (buffer-substring-no-properties (point-min) (1- (point-max)))))))
226
227(provide 'vc-git)