aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Nicolaescu2007-07-28 16:04:10 +0000
committerDan Nicolaescu2007-07-28 16:04:10 +0000
commitb19c08e44dd822f0822cfe96749c7cf0dc02d40e (patch)
tree91338ad1d53a8753126a1676574a438d05827550
parent473944f358e47b45f92e8cc88444f42ffdbb49e9 (diff)
downloademacs-b19c08e44dd822f0822cfe96749c7cf0dc02d40e.tar.gz
emacs-b19c08e44dd822f0822cfe96749c7cf0dc02d40e.zip
New File.
-rw-r--r--etc/NEWS2
-rw-r--r--lisp/ChangeLog4
-rw-r--r--lisp/vc-git.el468
3 files changed, 474 insertions, 0 deletions
diff --git a/etc/NEWS b/etc/NEWS
index b964722b439..45d4819f10e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -65,6 +65,8 @@ in to make it use the scrollbars from the system theme.
65 65
66*** VC has some support for Mercurial (hg). 66*** VC has some support for Mercurial (hg).
67 67
68*** VC has some support for Git.
69
68 70
69* Installation Changes in Emacs 22.1 71* Installation Changes in Emacs 22.1
70 72
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 6b01911d189..fc21fc33ffa 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,7 @@
12007-07-28 Dan Nicolaescu <dann@ics.uci.edu>
2
3 * vc-git.el: New File.
4
12007-07-28 Alan Mackenzie <acm@muc.de> 52007-07-28 Alan Mackenzie <acm@muc.de>
2 6
3 Fix problem with modes derived from CC Mode: 7 Fix problem with modes derived from CC Mode:
diff --git a/lisp/vc-git.el b/lisp/vc-git.el
new file mode 100644
index 00000000000..4c5bf866370
--- /dev/null
+++ b/lisp/vc-git.el
@@ -0,0 +1,468 @@
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 <julliard@winehq.org>
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 3, 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;; - check if more functions could use vc-git-command instead
41;; of start-process.
42;; - changelog generation
43
44;; Implement the rest of the vc interface. See the comment at the
45;; beginning of vc.el. The current status is:
46;; ("??" means: "figure out what to do about it")
47;;
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) NOT NEEDED
55;; - dir-state (dir) OK
56;; * workfile-version (file) OK
57;; - latest-on-branch-p (file) NOT NEEDED
58;; * checkout-model (file) OK
59;; - workfile-unchanged-p (file) OK
60;; - mode-line-string (file) NOT NEEDED
61;; - dired-state-info (file) OK
62;; STATE-CHANGING FUNCTIONS
63;; * create-repo () OK
64;; * register (files &optional rev comment) OK
65;; - init-version (file) NOT NEEDED
66;; - responsible-p (file) OK
67;; - could-register (file) NOT NEEDED, DEFAULT IS GOOD
68;; - receive-file (file rev) NOT NEEDED
69;; - unregister (file) OK
70;; * checkin (files rev comment) OK
71;; * find-version (file rev buffer) OK
72;; * checkout (file &optional editable rev) OK
73;; * revert (file &optional contents-done) OK
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,
78;; so it's probably not a good idea.
79;; - merge-news (file) see `merge'
80;; - steal-lock (file &optional version) NOT NEEDED
81;; HISTORY FUNCTIONS
82;; * print-log (files &optional buffer) OK
83;; - log-view-mode () OK
84;; - show-log-entry (version) NOT NEEDED, DEFAULT IS GOOD
85;; - wash-log (file) COULD BE SUPPORTED
86;; - logentry-check () NOT NEEDED
87;; - comment-history (file) ??
88;; - update-changelog (files) COULD BE SUPPORTED
89;; * diff (file &optional rev1 rev2 buffer) OK
90;; - revision-completion-table (file) NOT SUPPORTED in emacs-22.x
91;; - diff-tree (dir &optional rev1 rev2) OK
92;; - annotate-command (file buf &optional rev) OK
93;; - annotate-time () OK
94;; - annotate-current-time () NOT NEEDED
95;; - annotate-extract-revision-at-line () OK
96;; SNAPSHOT SYSTEM
97;; - create-snapshot (dir name branchp) OK
98;; - assign-name (file name) NOT NEEDED
99;; - retrieve-snapshot (dir name update) OK, needs to update buffers
100;; MISCELLANEOUS
101;; - make-version-backups-p (file) NOT NEEDED
102;; - repository-hostname (dirname) NOT NEEDED
103;; - previous-version (file rev) OK
104;; - next-version (file rev) OK
105;; - check-headers () COULD BE SUPPORTED
106;; - clear-headers () NOT NEEDED
107;; - delete-file (file) OK
108;; - rename-file (old new) OK
109;; - find-file-hook () NOT NEEDED
110;; - find-file-not-found-hook () NOT NEEDED
111
112(eval-when-compile (require 'cl) (require 'vc))
113
114(defvar git-commits-coding-system 'utf-8
115 "Default coding system for git commits.")
116
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
131(defun vc-git-registered (file)
132 "Check whether FILE is registered with git."
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")))))))))
143
144(defun vc-git-state (file)
145 "Git-specific version of `vc-state'."
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
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
179(defun vc-git-workfile-version (file)
180 "Git-specific version of `vc-workfile-version'."
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)
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.
197
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.
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
211(defun vc-git-dired-state-info (file)
212 "Git-specific version of `vc-dired-state-info'."
213 (let ((git-state (vc-state file)))
214 (if (eq git-state 'edited)
215 "(modified)"
216 ;; fall back to the default VC representation
217 (vc-default-dired-state-info 'GIT file))))
218
219;;; STATE-CHANGING FUNCTIONS
220
221(defun vc-git-create-repo ()
222 "Create a new GIT repository."
223 (vc-git-command "init" nil 0 nil))
224
225(defun vc-git-register (files &optional rev comment)
226 "Register FILE into the git version-control system."
227 (vc-git-command nil 0 files "update-index" "--add" "--"))
228
229(defalias 'vc-git-responsible-p 'vc-git-root)
230
231(defun vc-git-unregister (file)
232 (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
233
234
235(defun vc-git-checkin (files rev comment)
236 (let ((coding-system-for-write git-commits-coding-system))
237 (vc-git-command nil 0 files "commit" "-m" comment "--only" "--")))
238
239(defun vc-git-find-version (file rev buffer)
240 (let ((coding-system-for-read 'binary)
241 (coding-system-for-write 'binary)
242 (fullname (substring
243 (vc-git--run-command-string
244 file "ls-files" "-z" "--full-name" "--")
245 0 -1)))
246 (vc-git-command
247 buffer 0
248 (concat (if rev rev "HEAD") ":" fullname) "cat-file" "blob")))
249
250(defun vc-git-checkout (file &optional editable rev)
251 (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
252
253(defun vc-git-revert (file &optional contents-done)
254 "Revert FILE to the version stored in the git repository."
255 (if contents-done
256 (vc-git-command nil 0 file "update-index" "--")
257 (vc-git-command nil 0 file "checkout" "HEAD")))
258
259;;; HISTORY FUNCTIONS
260
261(defun vc-git-print-log (files &optional buffer)
262 "Get change log associated with FILES."
263 (let ((coding-system-for-read git-commits-coding-system)
264 ;; Support both the old print-log interface that passes a
265 ;; single file, and the new one that passes a file list.
266 (flist (if (listp files) files (list files))))
267 ;; `vc-do-command' creates the buffer, but we need it before running
268 ;; the command.
269 (vc-setup-buffer buffer)
270 ;; If the buffer exists from a previous invocation it might be
271 ;; read-only.
272 (let ((inhibit-read-only t))
273 ;; XXX `log-view-mode' needs to have something to identify where
274 ;; the log for each individual file starts. It seems that by
275 ;; default git does not output this info. So loop here and call
276 ;; "git rev-list" on each file separately to make sure that each
277 ;; file gets a "File:" header before the corresponding
278 ;; log. Maybe there is a way to do this with one command...
279 (dolist (file flist)
280 (with-current-buffer
281 buffer
282 (insert "File: " (file-name-nondirectory file) "\n"))
283 (vc-git-command buffer 'async (file-relative-name file)
284 "rev-list" "--pretty" "HEAD" "--")))))
285
286(defvar log-view-message-re)
287(defvar log-view-file-re)
288(defvar log-view-font-lock-keywords)
289
290(define-derived-mode vc-git-log-view-mode log-view-mode "GIT-Log-View"
291 (require 'add-log) ;; we need the faces add-log
292 ;; Don't have file markers, so use impossible regexp.
293 (set (make-local-variable 'log-view-file-re) "^File:[ \t]+\\(.+\\)")
294 (set (make-local-variable 'log-view-message-re)
295 "^commit *\\([0-9a-z]+\\)")
296 (set (make-local-variable 'log-view-font-lock-keywords)
297 (append
298 `((,log-view-message-re (1 'change-log-acknowledgement))
299 (,log-view-file-re (1 'change-log-file-face)))
300 ;; Handle the case:
301 ;; user: foo@bar
302 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
303 (1 'change-log-email))
304 ;; Handle the case:
305 ;; user: FirstName LastName <foo@bar>
306 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
307 (1 'change-log-name)
308 (2 'change-log-email))
309 ("^Date: \\(.+\\)" (1 'change-log-date))
310 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))
311
312(defun vc-git-diff (files &optional rev1 rev2 buffer)
313 (let ((buf (or buffer "*vc-diff*")))
314 (if (and rev1 rev2)
315 (vc-git-command buf 1 files "diff-tree" "--exit-code" "-p" rev1 rev2 "--")
316 (vc-git-command buf 1 files "diff-index" "--exit-code" "-p" (or rev1 "HEAD") "--"))))
317
318(defun vc-git-revision-table (file)
319 (let ((table (list "HEAD")))
320 (with-temp-buffer
321 (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
322 (goto-char (point-min))
323 (while (re-search-forward "^refs/\\(heads\\|tags\\)/\\(.*\\)$" nil t)
324 (push (match-string 2) table)))
325 table))
326
327;; Commented out on the 22.x branch, VC here does not support it yet
328;; and when bytecompiling it max-specpdl-size is exceeded.
329;; (defun vc-git-revision-completion-table (file)
330;; (lexical-let ((file file)
331;; table)
332;; (setq table (lazy-completion-table
333;; table (lambda () (vc-git-revision-table file))))
334;; table))
335
336(defun vc-git-diff-tree (dir &optional rev1 rev2)
337 (vc-git-diff dir rev1 rev2))
338
339(defun vc-git-annotate-command (file buf &optional rev)
340 ;; FIXME: rev is ignored
341 (let ((name (file-relative-name file)))
342 (vc-git-command buf 0 name "blame" (if rev (concat "-r" rev)))))
343
344(defun vc-git-annotate-time ()
345 (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)
346 (vc-annotate-convert-time
347 (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
348
349(defun vc-git-annotate-extract-revision-at-line ()
350 (save-excursion
351 (move-beginning-of-line 1)
352 (and (looking-at "[0-9a-f]+")
353 (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))
354
355;;; SNAPSHOT SYSTEM
356
357(defun vc-git-create-snapshot (dir name branchp)
358 (let ((default-directory dir))
359 (and (vc-git-command nil 0 nil "update-index" "--refresh")
360 (if branchp
361 (vc-git-command nil 0 nil "checkout" "-b" name)
362 (vc-git-command nil 0 nil "tag" name)))))
363
364(defun vc-git-retrieve-snapshot (dir name update)
365 (let ((default-directory dir))
366 (vc-git-command nil 0 nil "checkout" name)
367 ;; FIXME: update buffers if `update' is true
368 ))
369
370
371;;; MISCELLANEOUS
372
373(defun vc-git-previous-version (file rev)
374 "Git-specific version of `vc-previous-version'."
375 (let ((default-directory (file-name-directory (expand-file-name file)))
376 (file (file-name-nondirectory file)))
377 (vc-git-symbolic-commit
378 (with-temp-buffer
379 (and
380 (zerop
381 (call-process "git" nil '(t nil) nil "rev-list"
382 "-2" rev "--" file))
383 (goto-char (point-max))
384 (bolp)
385 (zerop (forward-line -1))
386 (not (bobp))
387 (buffer-substring-no-properties
388 (point)
389 (1- (point-max))))))))
390
391(defun vc-git-next-version (file rev)
392 "Git-specific version of `vc-next-version'."
393 (let* ((default-directory (file-name-directory
394 (expand-file-name file)))
395 (file (file-name-nondirectory file))
396 (current-rev
397 (with-temp-buffer
398 (and
399 (zerop
400 (call-process "git" nil '(t nil) nil "rev-list"
401 "-1" rev "--" file))
402 (goto-char (point-max))
403 (bolp)
404 (zerop (forward-line -1))
405 (bobp)
406 (buffer-substring-no-properties
407 (point)
408 (1- (point-max)))))))
409 (and current-rev
410 (vc-git-symbolic-commit
411 (with-temp-buffer
412 (and
413 (zerop
414 (call-process "git" nil '(t nil) nil "rev-list"
415 "HEAD" "--" file))
416 (goto-char (point-min))
417 (search-forward current-rev nil t)
418 (zerop (forward-line -1))
419 (buffer-substring-no-properties
420 (point)
421 (progn (forward-line 1) (1- (point))))))))))
422
423(defun vc-git-delete-file (file)
424 (vc-git-command nil 0 file "rm" "-f" "--"))
425
426(defun vc-git-rename-file (old new)
427 (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
428
429
430;;; Internal commands
431
432(defun vc-git-root (file)
433 (vc-find-root file ".git"))
434
435(defun vc-git-command (buffer okstatus file-or-list &rest flags)
436 "A wrapper around `vc-do-command' for use in vc-git.el.
437The difference to vc-do-command is that this function always invokes `git'."
438 (apply 'vc-do-command buffer okstatus "git" file-or-list flags))
439
440(defun vc-git--run-command-string (file &rest args)
441 "Run a git command on FILE and return its output as string."
442 (let* ((ok t)
443 (str (with-output-to-string
444 (with-current-buffer standard-output
445 (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
446 (append args (list (file-relative-name file)))))
447 (setq ok nil))))))
448 (and ok str)))
449
450(defun vc-git-symbolic-commit (commit)
451 "Translate COMMIT string into symbolic form.
452Returns nil if not possible."
453 (and commit
454 (with-temp-buffer
455 (and
456 (zerop
457 (call-process "git" nil '(t nil) nil "name-rev"
458 "--name-only" "--tags"
459 commit))
460 (goto-char (point-min))
461 (= (forward-line 2) 1)
462 (bolp)
463 (buffer-substring-no-properties (point-min) (1- (point-max)))))))
464
465(provide 'vc-git)
466
467;; arch-tag: bd10664a-0e5b-48f5-a877-6c17b135be12
468;;; vc-git.el ends here