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