contrib / emacs / vc-git.elon commit Merge git://git.kernel.org/pub/scm/gitk/gitk (b4a081b)
   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'.
  27;;
  28;; TODO
  29;;  - changelog generation
  30;;  - working with revisions other than HEAD
  31;;
  32
  33(defvar git-commits-coding-system 'utf-8
  34  "Default coding system for git commits.")
  35
  36(defun vc-git--run-command-string (file &rest args)
  37  "Run a git command on FILE and return its output as string."
  38  (let* ((ok t)
  39         (str (with-output-to-string
  40                (with-current-buffer standard-output
  41                  (unless (eq 0 (apply #'call-process "git" nil '(t nil) nil
  42                                       (append args (list (file-relative-name file)))))
  43                    (setq ok nil))))))
  44    (and ok str)))
  45
  46(defun vc-git--run-command (file &rest args)
  47  "Run a git command on FILE, discarding any output."
  48  (let ((name (file-relative-name file)))
  49    (eq 0 (apply #'call-process "git" nil (get-buffer "*Messages") nil (append args (list name))))))
  50
  51(defun vc-git-registered (file)
  52  "Check whether FILE is registered with git."
  53  (with-temp-buffer
  54    (let* ((dir (file-name-directory file))
  55           (name (file-relative-name file dir)))
  56      (when dir (cd dir))
  57      (and (eq 0 (call-process "git" nil '(t nil) nil "ls-files" "-c" "-z" "--" name))
  58           (let ((str (buffer-string)))
  59             (and (> (length str) (length name))
  60                  (string= (substring str 0 (1+ (length name))) (concat name "\0"))))))))
  61
  62(defun vc-git-state (file)
  63  "git-specific version of `vc-state'."
  64  (let ((diff (vc-git--run-command-string file "diff-index" "-z" "HEAD" "--")))
  65    (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} [ADMU]\0[^\0]+\0" diff))
  66        'edited
  67      'up-to-date)))
  68
  69(defun vc-git-workfile-version (file)
  70  "git-specific version of `vc-workfile-version'."
  71  (let ((str (with-output-to-string
  72               (with-current-buffer standard-output
  73                 (call-process "git" nil '(t nil) nil "symbolic-ref" "HEAD")))))
  74    (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
  75        (match-string 2 str)
  76      str)))
  77
  78(defun vc-git-revert (file &optional contents-done)
  79  "Revert FILE to the version stored in the git repository."
  80  (if contents-done
  81      (vc-git--run-command file "update-index" "--")
  82    (vc-git--run-command file "checkout" "HEAD")))
  83
  84(defun vc-git-checkout-model (file)
  85  'implicit)
  86
  87(defun vc-git-workfile-unchanged-p (file)
  88  (let ((sha1 (vc-git--run-command-string file "hash-object" "--"))
  89        (head (vc-git--run-command-string file "ls-tree" "-z" "HEAD" "--")))
  90    (and head
  91         (string-match "[0-7]\\{6\\} blob \\([0-9a-f]\\{40\\}\\)\t[^\0]+\0" head)
  92         (string= (car (split-string sha1 "\n")) (match-string 1 head)))))
  93
  94(defun vc-git-register (file &optional rev comment)
  95  "Register FILE into the git version-control system."
  96  (vc-git--run-command file "update-index" "--add" "--"))
  97
  98(defun vc-git-print-log (file)
  99  (let ((name (file-relative-name file))
 100        (coding-system-for-read git-commits-coding-system))
 101    (vc-do-command nil 'async "git" name "rev-list" "--pretty" "HEAD" "--")))
 102
 103(defun vc-git-diff (file &optional rev1 rev2)
 104  (let ((name (file-relative-name file)))
 105    (if (and rev1 rev2)
 106        (vc-do-command "*vc-diff*" 0 "git" name "diff-tree" "-p" rev1 rev2 "--")
 107      (vc-do-command "*vc-diff*" 0 "git" name "diff-index" "-p" (or rev1 "HEAD") "--"))
 108    ; git-diff-index doesn't set exit status like diff does
 109    (if (vc-git-workfile-unchanged-p file) 0 1)))
 110
 111(defun vc-git-checkin (file rev comment)
 112  (let ((coding-system-for-write git-commits-coding-system))
 113    (vc-git--run-command file "commit" "-m" comment "--only" "--")))
 114
 115(defun vc-git-checkout (file &optional editable rev destfile)
 116  (vc-git--run-command file "checkout" (or rev "HEAD")))
 117
 118(defun vc-git-annotate-command (file buf &optional rev)
 119  ; FIXME: rev is ignored
 120  (let ((name (file-relative-name file)))
 121    (call-process "git" nil buf nil "annotate" name)))
 122
 123(defun vc-git-annotate-time ()
 124  (and (re-search-forward "[0-9a-f]+\t(.*\t\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\) \\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) \\([-+0-9]+\\)\t[0-9]+)" nil t)
 125       (vc-annotate-convert-time
 126        (apply #'encode-time (mapcar (lambda (match) (string-to-number (match-string match))) '(6 5 4 3 2 1 7))))))
 127
 128;; Not really useful since we can't do anything with the revision yet
 129;;(defun vc-annotate-extract-revision-at-line ()
 130;;  (save-excursion
 131;;    (move-beginning-of-line 1)
 132;;    (and (looking-at "[0-9a-f]+")
 133;;         (buffer-substring (match-beginning 0) (match-end 0)))))
 134
 135(provide 'vc-git)