Commit | Line | Data |
---|---|---|
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." | |
fff4a046 DN |
267 | (let ((name (file-relative-name file)) |
268 | (coding-system-for-read git-commits-coding-system)) | |
53cc90ab DN |
269 | ;; `log-view-mode' needs to have the file name in order to function |
270 | ;; correctly. "git log" does not print it, so we insert it here by | |
271 | ;; hand. | |
272 | ||
273 | ;; `vc-do-command' creates the buffer, but we need it before running | |
274 | ;; the command. | |
275 | (vc-setup-buffer buffer) | |
276 | ;; If the buffer exists from a previous invocation it might be | |
277 | ;; read-only. | |
278 | (let ((inhibit-read-only t)) | |
8b9783e0 DN |
279 | ;; XXX Here loop and call "git rev-list" on each file separately |
280 | ;; to make sure that each file gets a "File:" header before the | |
281 | ;; corresponding log. Maybe there is a way to do this with one | |
282 | ;; command... | |
283 | (dolist (file files) | |
53cc90ab DN |
284 | (with-current-buffer |
285 | buffer | |
286 | (insert "File: " (file-name-nondirectory file) "\n"))) | |
8b9783e0 | 287 | (vc-git-command buffer 'async name "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 DN |
314 | |
315 | (defun vc-git-diff (file &optional rev1 rev2 buffer) | |
316 | (let ((name (file-relative-name file)) | |
317 | (buf (or buffer "*vc-diff*"))) | |
318 | (if (and rev1 rev2) | |
53cc90ab DN |
319 | (vc-git-command buf 0 name "diff-tree" "-p" rev1 rev2 "--") |
320 | (vc-git-command buf 0 name "diff-index" "-p" (or rev1 "HEAD") "--")) | |
321 | ;; git-diff-index doesn't set exit status like diff does | |
fff4a046 DN |
322 | (if (vc-git-workfile-unchanged-p file) 0 1))) |
323 | ||
b0f90937 DN |
324 | (defun vc-git-diff-tree (dir &optional rev1 rev2) |
325 | (vc-git-diff dir rev1 rev2)) | |
326 | ||
fff4a046 | 327 | (defun vc-git-annotate-command (file buf &optional rev) |
53cc90ab | 328 | ;; FIXME: rev is ignored |
fff4a046 | 329 | (let ((name (file-relative-name file))) |
53cc90ab | 330 | (vc-git-command buf 0 name "blame" (if rev (concat "-r" rev))))) |
fff4a046 DN |
331 | |
332 | (defun vc-git-annotate-time () | |
333 | (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) | |
334 | (vc-annotate-convert-time | |
335 | (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7)))))) | |
336 | ||
53cc90ab DN |
337 | (defun vc-git-annotate-extract-revision-at-line () |
338 | (save-excursion | |
339 | (move-beginning-of-line 1) | |
340 | (and (looking-at "[0-9a-f]+") | |
341 | (buffer-substring-no-properties (match-beginning 0) (match-end 0))))) | |
342 | ||
343 | ;;; MISCELLANEOUS | |
fff4a046 DN |
344 | |
345 | (defun vc-git-previous-version (file rev) | |
53cc90ab | 346 | "Git-specific version of `vc-previous-version'." |
fff4a046 DN |
347 | (let ((default-directory (file-name-directory (expand-file-name file))) |
348 | (file (file-name-nondirectory file))) | |
349 | (vc-git-symbolic-commit | |
350 | (with-temp-buffer | |
351 | (and | |
352 | (zerop | |
353 | (call-process "git" nil '(t nil) nil "rev-list" | |
354 | "-2" rev "--" file)) | |
355 | (goto-char (point-max)) | |
356 | (bolp) | |
357 | (zerop (forward-line -1)) | |
358 | (not (bobp)) | |
359 | (buffer-substring-no-properties | |
360 | (point) | |
361 | (1- (point-max)))))))) | |
362 | ||
363 | (defun vc-git-next-version (file rev) | |
53cc90ab | 364 | "Git-specific version of `vc-next-version'." |
fff4a046 DN |
365 | (let* ((default-directory (file-name-directory |
366 | (expand-file-name file))) | |
367 | (file (file-name-nondirectory file)) | |
368 | (current-rev | |
369 | (with-temp-buffer | |
370 | (and | |
371 | (zerop | |
372 | (call-process "git" nil '(t nil) nil "rev-list" | |
373 | "-1" rev "--" file)) | |
374 | (goto-char (point-max)) | |
375 | (bolp) | |
376 | (zerop (forward-line -1)) | |
377 | (bobp) | |
378 | (buffer-substring-no-properties | |
379 | (point) | |
380 | (1- (point-max))))))) | |
381 | (and current-rev | |
382 | (vc-git-symbolic-commit | |
383 | (with-temp-buffer | |
384 | (and | |
385 | (zerop | |
386 | (call-process "git" nil '(t nil) nil "rev-list" | |
387 | "HEAD" "--" file)) | |
388 | (goto-char (point-min)) | |
389 | (search-forward current-rev nil t) | |
390 | (zerop (forward-line -1)) | |
391 | (buffer-substring-no-properties | |
392 | (point) | |
393 | (progn (forward-line 1) (1- (point)))))))))) | |
394 | ||
8b38ce20 DN |
395 | (defun vc-git-delete-file (file) |
396 | (vc-git-command nil 0 file "rm" "-f" "--")) | |
b0f90937 | 397 | |
8b38ce20 DN |
398 | (defun vc-git-rename-file (old new) |
399 | (vc-git-command nil 0 (list old new) "mv" "-f" "--")) | |
b0f90937 | 400 | |
fff4a046 DN |
401 | \f |
402 | ;; Internal commands | |
403 | ||
53cc90ab DN |
404 | (defun vc-git-root (file) |
405 | (vc-find-root file ".git")) | |
406 | ||
8b9783e0 | 407 | (defun vc-git-command (buffer okstatus file-or-list &rest flags) |
53cc90ab DN |
408 | "A wrapper around `vc-do-command' for use in vc-git.el. |
409 | The difference to vc-do-command is that this function always invokes `git'." | |
8b9783e0 | 410 | (apply 'vc-do-command buffer okstatus "git" file-or-list flags)) |
53cc90ab | 411 | |
fff4a046 DN |
412 | (defun vc-git--run-command-string (file &rest args) |
413 | "Run a git command on FILE and return its output as string." | |
414 | (let* ((ok t) | |
415 | (str (with-output-to-string | |
416 | (with-current-buffer standard-output | |
417 | (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil | |
418 | (append args (list (file-relative-name file))))) | |
419 | (setq ok nil)))))) | |
420 | (and ok str))) | |
421 | ||
fff4a046 DN |
422 | (defun vc-git-symbolic-commit (commit) |
423 | "Translate COMMIT string into symbolic form. | |
424 | Returns nil if not possible." | |
425 | (and commit | |
426 | (with-temp-buffer | |
427 | (and | |
428 | (zerop | |
429 | (call-process "git" nil '(t nil) nil "name-rev" | |
430 | "--name-only" "--tags" | |
431 | commit)) | |
432 | (goto-char (point-min)) | |
433 | (= (forward-line 2) 1) | |
434 | (bolp) | |
435 | (buffer-substring-no-properties (point-min) (1- (point-max))))))) | |
436 | ||
437 | (provide 'vc-git) | |
53cc90ab | 438 | |
328471d9 | 439 | ;; arch-tag: bd10664a-0e5b-48f5-a877-6c17b135be12 |
53cc90ab | 440 | ;;; vc-git.el ends here |