Code

Merge branch 'maint'
[git.git] / contrib / emacs / vc-git.el
1 ;;; vc-git.el --- VC backend for the git version control system
3 ;; Copyright (C) 2006 Alexandre Julliard
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation; either version 2 of
8 ;; the License, or (at your option) any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be
11 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
12 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 ;; PURPOSE.  See the GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public
16 ;; License along with this program; if not, write to the Free
17 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 ;; MA 02111-1307 USA
20 ;;; Commentary:
22 ;; This file contains a VC backend for the git version control
23 ;; system.
24 ;;
25 ;; To install: put this file on the load-path and add GIT to the list
26 ;; of supported backends in `vc-handled-backends'; the following line,
27 ;; placed in your ~/.emacs, will accomplish this:
28 ;;
29 ;;     (add-to-list 'vc-handled-backends 'GIT)
30 ;;
31 ;; TODO
32 ;;  - changelog generation
33 ;;  - working with revisions other than HEAD
34 ;;
36 (defvar git-commits-coding-system 'utf-8
37   "Default coding system for git commits.")
39 (defun vc-git--run-command-string (file &rest args)
40   "Run a git command on FILE and return its output as string."
41   (let* ((ok t)
42          (str (with-output-to-string
43                 (with-current-buffer standard-output
44                   (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
45                                        (append args (list (file-relative-name file)))))
46                     (setq ok nil))))))
47     (and ok str)))
49 (defun vc-git--run-command (file &rest args)
50   "Run a git command on FILE, discarding any output."
51   (let ((name (file-relative-name file)))
52     (eq 0 (apply #'call-process "git" nil (get-buffer "*Messages") nil (append args (list name))))))
54 (defun vc-git-registered (file)
55   "Check whether FILE is registered with git."
56   (with-temp-buffer
57     (let* ((dir (file-name-directory file))
58            (name (file-relative-name file dir)))
59       (when dir (cd dir))
60       (and (ignore-errors (eq 0 (call-process "git" nil '(t nil) nil "ls-files" "-c" "-z" "--" name)))
61            (let ((str (buffer-string)))
62              (and (> (length str) (length name))
63                   (string= (substring str 0 (1+ (length name))) (concat name "\0"))))))))
65 (defun vc-git-state (file)
66   "git-specific version of `vc-state'."
67   (let ((diff (vc-git--run-command-string file "diff-index" "-z" "HEAD" "--")))
68     (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff))
69         'edited
70       'up-to-date)))
72 (defun vc-git-workfile-version (file)
73   "git-specific version of `vc-workfile-version'."
74   (let ((str (with-output-to-string
75                (with-current-buffer standard-output
76                  (call-process "git" nil '(t nil) nil "symbolic-ref" "HEAD")))))
77     (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
78         (match-string 2 str)
79       str)))
81 (defun vc-git-revert (file &optional contents-done)
82   "Revert FILE to the version stored in the git repository."
83   (if contents-done
84       (vc-git--run-command file "update-index" "--")
85     (vc-git--run-command file "checkout" "HEAD")))
87 (defun vc-git-checkout-model (file)
88   'implicit)
90 (defun vc-git-workfile-unchanged-p (file)
91   (let ((sha1 (vc-git--run-command-string file "hash-object" "--"))
92         (head (vc-git--run-command-string file "ls-tree" "-z" "HEAD" "--")))
93     (and head
94          (string-match "[0-7]\\{6\\} blob \\([0-9a-f]\\{40\\}\\)\t[^\0]+\0" head)
95          (string= (car (split-string sha1 "\n")) (match-string 1 head)))))
97 (defun vc-git-register (file &optional rev comment)
98   "Register FILE into the git version-control system."
99   (vc-git--run-command file "update-index" "--add" "--"))
101 (defun vc-git-print-log (file &optional buffer)
102   (let ((name (file-relative-name file))
103         (coding-system-for-read git-commits-coding-system))
104     (vc-do-command buffer 'async "git" name "rev-list" "--pretty" "HEAD" "--")))
106 (defun vc-git-diff (file &optional rev1 rev2 buffer)
107   (let ((name (file-relative-name file))
108         (buf (or buffer "*vc-diff*")))
109     (if (and rev1 rev2)
110         (vc-do-command buf 0 "git" name "diff-tree" "-p" rev1 rev2 "--")
111       (vc-do-command buf 0 "git" name "diff-index" "-p" (or rev1 "HEAD") "--"))
112     ; git-diff-index doesn't set exit status like diff does
113     (if (vc-git-workfile-unchanged-p file) 0 1)))
115 (defun vc-git-checkin (file rev comment)
116   (let ((coding-system-for-write git-commits-coding-system))
117     (vc-git--run-command file "commit" "-m" comment "--only" "--")))
119 (defun vc-git-checkout (file &optional editable rev destfile)
120   (vc-git--run-command file "checkout" (or rev "HEAD")))
122 (defun vc-git-annotate-command (file buf &optional rev)
123   ; FIXME: rev is ignored
124   (let ((name (file-relative-name file)))
125     (call-process "git" nil buf nil "blame" name)))
127 (defun vc-git-annotate-time ()
128   (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)
129        (vc-annotate-convert-time
130         (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
132 ;; Not really useful since we can't do anything with the revision yet
133 ;;(defun vc-annotate-extract-revision-at-line ()
134 ;;  (save-excursion
135 ;;    (move-beginning-of-line 1)
136 ;;    (and (looking-at "[0-9a-f]+")
137 ;;         (buffer-substring (match-beginning 0) (match-end 0)))))
139 (provide 'vc-git)