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